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
&& orig
->bit_offset
== orig
->bit_offset
;
197 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
199 unsigned long mod
, as
;
203 mod
|= node
->ctype
.modifiers
;
204 as
|= node
->ctype
.as
;
205 if (node
->type
== SYM_NODE
) {
206 node
= node
->ctype
.base_type
;
211 *modp
= mod
& ~MOD_IGNORE
;
216 static int is_same_type(struct expression
*expr
, struct symbol
*new)
218 struct symbol
*old
= expr
->ctype
;
219 unsigned long oldmod
, newmod
, oldas
, newas
;
221 old
= base_type(old
, &oldmod
, &oldas
);
222 new = base_type(new, &newmod
, &newas
);
224 /* Same base type, same address space? */
225 if (old
== new && oldas
== newas
) {
226 unsigned long difmod
;
228 /* Check the modifier bits. */
229 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
231 /* Exact same type? */
236 * Not the same type, but differs only in "const".
237 * Don't warn about MOD_NOCAST.
239 if (difmod
== MOD_CONST
)
242 if ((oldmod
| newmod
) & MOD_NOCAST
) {
243 const char *tofrom
= "to/from";
244 if (!(newmod
& MOD_NOCAST
))
246 if (!(oldmod
& MOD_NOCAST
))
248 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
254 warn_for_different_enum_types (struct position pos
,
255 struct symbol
*typea
,
256 struct symbol
*typeb
)
260 if (typea
->type
== SYM_NODE
)
261 typea
= typea
->ctype
.base_type
;
262 if (typeb
->type
== SYM_NODE
)
263 typeb
= typeb
->ctype
.base_type
;
268 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
269 warning(pos
, "mixing different enum types");
270 info(pos
, " %s versus", show_typename(typea
));
271 info(pos
, " %s", show_typename(typeb
));
276 * This gets called for implicit casts in assignments and
277 * integer promotion. We often want to try to move the
278 * cast down, because the ops involved may have been
279 * implicitly cast up, and we can get rid of the casts
282 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
284 struct expression
*expr
;
286 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
288 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
292 * See if we can simplify the op. Move the cast down.
296 if (old
->ctype
->bit_size
< type
->bit_size
)
298 if (old
->op
== '~') {
300 old
->unop
= cast_to(old
->unop
, type
);
305 case EXPR_IMPLIED_CAST
:
306 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
308 if (old
->ctype
->bit_size
>= type
->bit_size
) {
309 struct expression
*orig
= old
->cast_expression
;
310 if (same_cast_type(orig
->ctype
, type
))
312 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
314 old
->cast_type
= type
;
324 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
325 expr
->flags
= old
->flags
;
327 expr
->cast_type
= type
;
328 expr
->cast_expression
= old
;
343 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
345 static int type_class
[SYM_BAD
+ 1] = {
346 [SYM_PTR
] = TYPE_PTR
,
347 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
348 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
349 [SYM_STRUCT
] = TYPE_COMPOUND
,
350 [SYM_UNION
] = TYPE_COMPOUND
,
351 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
352 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
353 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
355 if (type
->type
== SYM_NODE
)
356 type
= type
->ctype
.base_type
;
357 if (type
->type
== SYM_TYPEOF
) {
358 type
= evaluate_expression(type
->initializer
);
361 else if (type
->type
== SYM_NODE
)
362 type
= type
->ctype
.base_type
;
364 if (type
->type
== SYM_ENUM
)
365 type
= type
->ctype
.base_type
;
367 if (type
->type
== SYM_BASETYPE
) {
368 if (type
->ctype
.base_type
== &int_type
)
370 if (type
->ctype
.base_type
== &fp_type
)
371 return TYPE_NUM
| TYPE_FLOAT
;
373 return type_class
[type
->type
];
376 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
378 static inline int is_string_type(struct symbol
*type
)
380 if (type
->type
== SYM_NODE
)
381 type
= type
->ctype
.base_type
;
382 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
385 static struct symbol
*bad_expr_type(struct expression
*expr
)
387 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
388 switch (expr
->type
) {
391 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
392 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
396 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
403 return expr
->ctype
= &bad_ctype
;
406 static int restricted_value(struct expression
*v
, struct symbol
*type
)
408 if (v
->type
!= EXPR_VALUE
)
415 static int restricted_binop(int op
, struct symbol
*type
)
420 case SPECIAL_AND_ASSIGN
:
421 case SPECIAL_OR_ASSIGN
:
422 case SPECIAL_XOR_ASSIGN
:
423 return 1; /* unfoul */
427 return 2; /* keep fouled */
429 case SPECIAL_NOTEQUAL
:
430 return 3; /* warn if fouled */
436 static int restricted_unop(int op
, struct symbol
**type
)
439 if ((*type
)->bit_size
< bits_in_int
)
440 *type
= befoul(*type
);
447 /* type should be SYM_FOULED */
448 static inline struct symbol
*unfoul(struct symbol
*type
)
450 return type
->ctype
.base_type
;
453 static struct symbol
*restricted_binop_type(int op
,
454 struct expression
*left
,
455 struct expression
*right
,
456 int lclass
, int rclass
,
457 struct symbol
*ltype
,
458 struct symbol
*rtype
)
460 struct symbol
*ctype
= NULL
;
461 if (lclass
& TYPE_RESTRICT
) {
462 if (rclass
& TYPE_RESTRICT
) {
463 if (ltype
== rtype
) {
465 } else if (lclass
& TYPE_FOULED
) {
466 if (unfoul(ltype
) == rtype
)
468 } else if (rclass
& TYPE_FOULED
) {
469 if (unfoul(rtype
) == ltype
)
473 if (!restricted_value(right
, ltype
))
476 } else if (!restricted_value(left
, rtype
))
480 switch (restricted_binop(op
, ctype
)) {
482 if ((lclass
^ rclass
) & TYPE_FOULED
)
483 ctype
= unfoul(ctype
);
486 if (!(lclass
& rclass
& TYPE_FOULED
))
498 static inline void unrestrict(struct expression
*expr
,
499 int class, struct symbol
**ctype
)
501 if (class & TYPE_RESTRICT
) {
502 if (class & TYPE_FOULED
)
503 *ctype
= unfoul(*ctype
);
504 warning(expr
->pos
, "%s degrades to integer",
505 show_typename(*ctype
));
506 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
510 static struct symbol
*usual_conversions(int op
,
511 struct expression
*left
,
512 struct expression
*right
,
513 int lclass
, int rclass
,
514 struct symbol
*ltype
,
515 struct symbol
*rtype
)
517 struct symbol
*ctype
;
519 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
521 if ((lclass
| rclass
) & TYPE_RESTRICT
)
525 if (!(lclass
& TYPE_FLOAT
)) {
526 if (!(rclass
& TYPE_FLOAT
))
527 return bigger_int_type(ltype
, rtype
);
530 } else if (rclass
& TYPE_FLOAT
) {
531 unsigned long lmod
= ltype
->ctype
.modifiers
;
532 unsigned long rmod
= rtype
->ctype
.modifiers
;
533 if (rmod
& ~lmod
& (MOD_LONG_ALL
))
541 ctype
= restricted_binop_type(op
, left
, right
,
542 lclass
, rclass
, ltype
, rtype
);
546 unrestrict(left
, lclass
, <ype
);
547 unrestrict(right
, rclass
, &rtype
);
552 static inline int lvalue_expression(struct expression
*expr
)
554 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
557 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
559 struct expression
*index
= expr
->right
;
560 struct symbol
*ctype
, *base
;
563 classify_type(degenerate(expr
->left
), &ctype
);
564 base
= examine_pointer_target(ctype
);
567 expression_error(expr
, "missing type information");
570 if (is_function(base
)) {
571 expression_error(expr
, "arithmetics on pointers to functions");
575 /* Get the size of whatever the pointer points to */
576 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
578 if (ctype
== &null_ctype
)
582 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
585 if (index
->type
== EXPR_VALUE
) {
586 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
587 unsigned long long v
= index
->value
, mask
;
588 mask
= 1ULL << (itype
->bit_size
- 1);
594 mask
= 1ULL << (bits_in_pointer
- 1);
595 v
&= mask
| (mask
- 1);
597 val
->ctype
= ssize_t_ctype
;
602 if (itype
->bit_size
< bits_in_pointer
)
603 index
= cast_to(index
, ssize_t_ctype
);
606 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
607 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
609 val
->ctype
= ssize_t_ctype
;
610 val
->value
= multiply
;
613 mul
->ctype
= ssize_t_ctype
;
623 static void examine_fn_arguments(struct symbol
*fn
);
625 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
627 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
628 unsigned long mod1
, unsigned long mod2
)
630 unsigned long as1
= c1
->as
, as2
= c2
->as
;
631 struct symbol
*t1
= c1
->base_type
;
632 struct symbol
*t2
= c2
->base_type
;
633 int move1
= 1, move2
= 1;
634 mod1
|= c1
->modifiers
;
635 mod2
|= c2
->modifiers
;
639 struct symbol
*base1
= t1
->ctype
.base_type
;
640 struct symbol
*base2
= t2
->ctype
.base_type
;
643 * FIXME! Collect alignment and context too here!
646 if (t1
&& t1
->type
!= SYM_PTR
) {
647 mod1
|= t1
->ctype
.modifiers
;
654 if (t2
&& t2
->type
!= SYM_PTR
) {
655 mod2
|= t2
->ctype
.modifiers
;
664 return "different types";
666 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
674 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
684 if (type
!= t2
->type
)
685 return "different base types";
689 sparse_error(t1
->pos
,
690 "internal error: bad type in derived(%d)",
694 return "different base types";
697 /* allow definition of incomplete structs and unions */
698 if (t1
->ident
== t2
->ident
)
700 return "different base types";
702 /* XXX: we ought to compare sizes */
706 return "different address spaces";
707 /* MOD_SPECIFIER is due to idiocy in parse.c */
708 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
709 return "different modifiers";
710 /* we could be lazier here */
711 base1
= examine_pointer_target(t1
);
712 base2
= examine_pointer_target(t2
);
713 mod1
= t1
->ctype
.modifiers
;
715 mod2
= t2
->ctype
.modifiers
;
719 struct symbol
*arg1
, *arg2
;
723 return "different address spaces";
724 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
725 return "different modifiers";
726 mod1
= t1
->ctype
.modifiers
;
728 mod2
= t2
->ctype
.modifiers
;
731 if (base1
->variadic
!= base2
->variadic
)
732 return "incompatible variadic arguments";
733 examine_fn_arguments(t1
);
734 examine_fn_arguments(t2
);
735 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
736 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
743 return "different argument counts";
744 diffstr
= type_difference(&arg1
->ctype
,
748 static char argdiff
[80];
749 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
756 FINISH_PTR_LIST(arg2
);
757 FINISH_PTR_LIST(arg1
);
762 return "different address spaces";
764 return "different base types";
765 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
769 return "different type sizes";
770 else if (diff
& ~MOD_SIGNEDNESS
)
771 return "different modifiers";
773 return "different signedness";
779 return "different address spaces";
780 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
781 return "different modifiers";
785 static void bad_null(struct expression
*expr
)
787 if (Wnon_pointer_null
)
788 warning(expr
->pos
, "Using plain integer as NULL pointer");
791 static unsigned long target_qualifiers(struct symbol
*type
)
793 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
794 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
799 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
801 const char *typediff
;
802 struct symbol
*ltype
, *rtype
;
803 struct expression
*l
= expr
->left
;
804 struct expression
*r
= expr
->right
;
805 struct symbol
*lbase
;
807 classify_type(degenerate(l
), <ype
);
808 classify_type(degenerate(r
), &rtype
);
810 lbase
= examine_pointer_target(ltype
);
811 examine_pointer_target(rtype
);
812 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
813 target_qualifiers(rtype
),
814 target_qualifiers(ltype
));
816 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
818 if (is_function(lbase
)) {
819 expression_error(expr
, "subtraction of functions? Share your drugs");
823 expr
->ctype
= ssize_t_ctype
;
824 if (lbase
->bit_size
> bits_in_char
) {
825 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
826 struct expression
*div
= expr
;
827 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
828 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
830 val
->ctype
= size_t_ctype
;
833 if (value
& (value
-1)) {
834 if (Wptr_subtraction_blows
)
835 warning(expr
->pos
, "potentially expensive pointer subtraction");
839 sub
->ctype
= ssize_t_ctype
;
848 return ssize_t_ctype
;
851 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
853 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
855 struct symbol
*ctype
;
860 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
861 warning(expr
->pos
, "assignment expression in conditional");
863 ctype
= evaluate_expression(expr
);
865 if (is_safe_type(ctype
))
866 warning(expr
->pos
, "testing a 'safe expression'");
872 static struct symbol
*evaluate_logical(struct expression
*expr
)
874 if (!evaluate_conditional(expr
->left
, 0))
876 if (!evaluate_conditional(expr
->right
, 0))
879 /* the result is int [6.5.13(3), 6.5.14(3)] */
880 expr
->ctype
= &int_ctype
;
882 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
888 static struct symbol
*evaluate_binop(struct expression
*expr
)
890 struct symbol
*ltype
, *rtype
, *ctype
;
891 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
892 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
896 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
900 /* number op number */
901 if (lclass
& rclass
& TYPE_NUM
) {
902 if ((lclass
| rclass
) & TYPE_FLOAT
) {
904 case '+': case '-': case '*': case '/':
907 return bad_expr_type(expr
);
911 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
912 // shifts do integer promotions, but that's it.
913 unrestrict(expr
->left
, lclass
, <ype
);
914 unrestrict(expr
->right
, rclass
, &rtype
);
915 ctype
= ltype
= integer_promotion(ltype
);
916 rtype
= integer_promotion(rtype
);
918 // The rest do usual conversions
919 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
920 && expr
->left
->op
== '!';
921 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
922 && expr
->right
->op
== '!';
923 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
924 warning(expr
->pos
, "dubious: %sx %c %sy",
927 right_not
? "!" : "");
929 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
930 lclass
, rclass
, ltype
, rtype
);
931 ctype
= rtype
= ltype
;
934 expr
->left
= cast_to(expr
->left
, ltype
);
935 expr
->right
= cast_to(expr
->right
, rtype
);
940 /* pointer (+|-) integer */
941 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
942 unrestrict(expr
->right
, rclass
, &rtype
);
943 return evaluate_ptr_add(expr
, rtype
);
946 /* integer + pointer */
947 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
948 struct expression
*index
= expr
->left
;
949 unrestrict(index
, lclass
, <ype
);
950 expr
->left
= expr
->right
;
952 return evaluate_ptr_add(expr
, ltype
);
955 /* pointer - pointer */
956 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
957 return evaluate_ptr_sub(expr
);
959 return bad_expr_type(expr
);
962 static struct symbol
*evaluate_comma(struct expression
*expr
)
964 expr
->ctype
= degenerate(expr
->right
);
965 if (expr
->ctype
== &null_ctype
)
966 expr
->ctype
= &ptr_ctype
;
967 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
971 static int modify_for_unsigned(int op
)
974 op
= SPECIAL_UNSIGNED_LT
;
976 op
= SPECIAL_UNSIGNED_GT
;
977 else if (op
== SPECIAL_LTE
)
978 op
= SPECIAL_UNSIGNED_LTE
;
979 else if (op
== SPECIAL_GTE
)
980 op
= SPECIAL_UNSIGNED_GTE
;
984 static inline int is_null_pointer_constant(struct expression
*e
)
986 if (e
->ctype
== &null_ctype
)
988 if (!(e
->flags
& Int_const_expr
))
990 return is_zero_constant(e
) ? 2 : 0;
993 static struct symbol
*evaluate_compare(struct expression
*expr
)
995 struct expression
*left
= expr
->left
, *right
= expr
->right
;
996 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
997 int lclass
= classify_type(degenerate(left
), <ype
);
998 int rclass
= classify_type(degenerate(right
), &rtype
);
999 struct symbol
*ctype
;
1000 const char *typediff
;
1003 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1008 if (is_type_type(ltype
) && is_type_type(rtype
))
1011 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1012 warning(expr
->pos
, "testing a 'safe expression'");
1014 /* number on number */
1015 if (lclass
& rclass
& TYPE_NUM
) {
1016 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1017 lclass
, rclass
, ltype
, rtype
);
1018 expr
->left
= cast_to(expr
->left
, ctype
);
1019 expr
->right
= cast_to(expr
->right
, ctype
);
1020 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1021 expr
->op
= modify_for_unsigned(expr
->op
);
1025 /* at least one must be a pointer */
1026 if (!((lclass
| rclass
) & TYPE_PTR
))
1027 return bad_expr_type(expr
);
1029 /* equality comparisons can be with null pointer constants */
1030 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1031 int is_null1
= is_null_pointer_constant(left
);
1032 int is_null2
= is_null_pointer_constant(right
);
1037 if (is_null1
&& is_null2
) {
1038 int positive
= expr
->op
== SPECIAL_EQUAL
;
1039 expr
->type
= EXPR_VALUE
;
1040 expr
->value
= positive
;
1043 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1044 left
= cast_to(left
, rtype
);
1047 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1048 right
= cast_to(right
, ltype
);
1052 /* both should be pointers */
1053 if (!(lclass
& rclass
& TYPE_PTR
))
1054 return bad_expr_type(expr
);
1055 expr
->op
= modify_for_unsigned(expr
->op
);
1057 lbase
= examine_pointer_target(ltype
);
1058 rbase
= examine_pointer_target(rtype
);
1060 /* they also have special treatment for pointers to void */
1061 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1062 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1063 if (lbase
== &void_ctype
) {
1064 right
= cast_to(right
, ltype
);
1067 if (rbase
== &void_ctype
) {
1068 left
= cast_to(left
, rtype
);
1074 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1075 target_qualifiers(rtype
),
1076 target_qualifiers(ltype
));
1080 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1084 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1085 expr
->ctype
= &int_ctype
;
1090 * NOTE! The degenerate case of "x ? : y", where we don't
1091 * have a true case, this will possibly promote "x" to the
1092 * same type as "y", and thus _change_ the conditional
1093 * test in the expression. But since promotion is "safe"
1094 * for testing, that's OK.
1096 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1098 struct expression
**true;
1099 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1101 const char * typediff
;
1104 if (!evaluate_conditional(expr
->conditional
, 0))
1106 if (!evaluate_expression(expr
->cond_false
))
1109 ctype
= degenerate(expr
->conditional
);
1110 rtype
= degenerate(expr
->cond_false
);
1112 true = &expr
->conditional
;
1114 if (expr
->cond_true
) {
1115 if (!evaluate_expression(expr
->cond_true
))
1117 ltype
= degenerate(expr
->cond_true
);
1118 true = &expr
->cond_true
;
1122 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1123 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1128 lclass
= classify_type(ltype
, <ype
);
1129 rclass
= classify_type(rtype
, &rtype
);
1130 if (lclass
& rclass
& TYPE_NUM
) {
1131 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1132 lclass
, rclass
, ltype
, rtype
);
1133 *true = cast_to(*true, ctype
);
1134 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1138 if ((lclass
| rclass
) & TYPE_PTR
) {
1139 int is_null1
= is_null_pointer_constant(*true);
1140 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1142 if (is_null1
&& is_null2
) {
1143 *true = cast_to(*true, &ptr_ctype
);
1144 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1148 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1151 *true = cast_to(*true, rtype
);
1155 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1157 bad_null(expr
->cond_false
);
1158 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1162 if (!(lclass
& rclass
& TYPE_PTR
)) {
1163 typediff
= "different types";
1166 /* OK, it's pointer on pointer */
1167 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1168 typediff
= "different address spaces";
1172 /* need to be lazier here */
1173 lbase
= examine_pointer_target(ltype
);
1174 rbase
= examine_pointer_target(rtype
);
1175 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1177 if (lbase
== &void_ctype
) {
1178 /* XXX: pointers to function should warn here */
1183 if (rbase
== &void_ctype
) {
1184 /* XXX: pointers to function should warn here */
1188 /* XXX: that should be pointer to composite */
1190 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1197 /* void on void, struct on same struct, union on same union */
1198 if (ltype
== rtype
) {
1202 typediff
= "different base types";
1205 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1209 expr
->ctype
= ctype
;
1213 if (qual
& ~ctype
->ctype
.modifiers
) {
1214 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1216 sym
->ctype
.modifiers
|= qual
;
1219 *true = cast_to(*true, ctype
);
1220 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1224 /* FP assignments can not do modulo or bit operations */
1225 static int compatible_float_op(int op
)
1227 return op
== SPECIAL_ADD_ASSIGN
||
1228 op
== SPECIAL_SUB_ASSIGN
||
1229 op
== SPECIAL_MUL_ASSIGN
||
1230 op
== SPECIAL_DIV_ASSIGN
;
1233 static int evaluate_assign_op(struct expression
*expr
)
1235 struct symbol
*target
= expr
->left
->ctype
;
1236 struct symbol
*source
= expr
->right
->ctype
;
1237 struct symbol
*t
, *s
;
1238 int tclass
= classify_type(target
, &t
);
1239 int sclass
= classify_type(source
, &s
);
1242 if (tclass
& sclass
& TYPE_NUM
) {
1243 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1244 expression_error(expr
, "invalid assignment");
1247 if (tclass
& TYPE_RESTRICT
) {
1248 if (!restricted_binop(op
, t
)) {
1249 warning(expr
->pos
, "bad assignment (%s) to %s",
1250 show_special(op
), show_typename(t
));
1251 expr
->right
= cast_to(expr
->right
, target
);
1254 /* allowed assignments unfoul */
1255 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1257 if (!restricted_value(expr
->right
, t
))
1259 } else if (!(sclass
& TYPE_RESTRICT
))
1261 /* source and target would better be identical restricted */
1264 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1265 info(expr
->pos
, " left side has type %s", show_typename(t
));
1266 info(expr
->pos
, " right side has type %s", show_typename(s
));
1267 expr
->right
= cast_to(expr
->right
, target
);
1270 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1271 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1272 unrestrict(expr
->right
, sclass
, &s
);
1273 evaluate_ptr_add(expr
, s
);
1276 expression_error(expr
, "invalid pointer assignment");
1280 expression_error(expr
, "invalid assignment");
1284 expr
->right
= cast_to(expr
->right
, target
);
1288 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1291 return 0; /* yes, 0 - we don't want a cast_to here */
1292 if (t1
== &void_ctype
)
1294 if (t2
== &void_ctype
)
1296 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1298 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1302 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1304 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1309 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1310 struct expression
**rp
, const char *where
)
1312 const char *typediff
;
1313 struct symbol
*source
= degenerate(*rp
);
1314 struct symbol
*t
, *s
;
1315 int tclass
= classify_type(target
, &t
);
1316 int sclass
= classify_type(source
, &s
);
1318 if (tclass
& sclass
& TYPE_NUM
) {
1319 if (tclass
& TYPE_RESTRICT
) {
1320 /* allowed assignments unfoul */
1321 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1323 if (!restricted_value(*rp
, target
))
1327 } else if (!(sclass
& TYPE_RESTRICT
))
1329 typediff
= "different base types";
1333 if (tclass
== TYPE_PTR
) {
1334 unsigned long mod1
, mod2
;
1335 struct symbol
*b1
, *b2
;
1336 // NULL pointer is always OK
1337 int is_null
= is_null_pointer_constant(*rp
);
1343 if (!(sclass
& TYPE_PTR
)) {
1344 typediff
= "different base types";
1347 b1
= examine_pointer_target(t
);
1348 b2
= examine_pointer_target(s
);
1349 mod1
= target_qualifiers(t
);
1350 mod2
= target_qualifiers(s
);
1351 if (whitelist_pointers(b1
, b2
)) {
1353 * assignments to/from void * are OK, provided that
1354 * we do not remove qualifiers from pointed to [C]
1355 * or mix address spaces [sparse].
1357 if (t
->ctype
.as
!= s
->ctype
.as
) {
1358 typediff
= "different address spaces";
1362 typediff
= "different modifiers";
1367 /* It's OK if the target is more volatile or const than the source */
1368 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1374 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1377 if (tclass
& TYPE_NUM
) {
1378 /* XXX: need to turn into comparison with NULL */
1379 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1381 typediff
= "different base types";
1384 typediff
= "invalid types";
1387 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1388 info(expr
->pos
, " expected %s", show_typename(target
));
1389 info(expr
->pos
, " got %s", show_typename(source
));
1390 *rp
= cast_to(*rp
, target
);
1393 *rp
= cast_to(*rp
, target
);
1397 static void mark_assigned(struct expression
*expr
)
1403 switch (expr
->type
) {
1408 if (sym
->type
!= SYM_NODE
)
1410 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1414 mark_assigned(expr
->left
);
1415 mark_assigned(expr
->right
);
1418 case EXPR_FORCE_CAST
:
1419 mark_assigned(expr
->cast_expression
);
1422 mark_assigned(expr
->base
);
1430 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1432 if (type
->ctype
.modifiers
& MOD_CONST
)
1433 expression_error(left
, "assignment to const expression");
1435 /* We know left is an lvalue, so it's a "preop-*" */
1436 mark_assigned(left
->unop
);
1439 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1441 struct expression
*left
= expr
->left
;
1442 struct expression
*where
= expr
;
1443 struct symbol
*ltype
;
1445 if (!lvalue_expression(left
)) {
1446 expression_error(expr
, "not an lvalue");
1450 ltype
= left
->ctype
;
1452 if (expr
->op
!= '=') {
1453 if (!evaluate_assign_op(expr
))
1456 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1460 evaluate_assign_to(left
, ltype
);
1462 expr
->ctype
= ltype
;
1466 static void examine_fn_arguments(struct symbol
*fn
)
1470 FOR_EACH_PTR(fn
->arguments
, s
) {
1471 struct symbol
*arg
= evaluate_symbol(s
);
1472 /* Array/function arguments silently degenerate into pointers */
1478 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1479 if (arg
->type
== SYM_ARRAY
)
1480 ptr
->ctype
= arg
->ctype
;
1482 ptr
->ctype
.base_type
= arg
;
1483 ptr
->ctype
.as
|= s
->ctype
.as
;
1484 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1486 s
->ctype
.base_type
= ptr
;
1488 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1491 examine_symbol_type(s
);
1498 } END_FOR_EACH_PTR(s
);
1501 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1503 /* Take the modifiers of the pointer, and apply them to the member */
1504 mod
|= sym
->ctype
.modifiers
;
1505 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1506 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1508 newsym
->ctype
.as
= as
;
1509 newsym
->ctype
.modifiers
= mod
;
1515 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1517 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1518 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1520 node
->ctype
.base_type
= ptr
;
1521 ptr
->bit_size
= bits_in_pointer
;
1522 ptr
->ctype
.alignment
= pointer_alignment
;
1524 node
->bit_size
= bits_in_pointer
;
1525 node
->ctype
.alignment
= pointer_alignment
;
1528 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1529 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1530 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1532 if (sym
->type
== SYM_NODE
) {
1533 ptr
->ctype
.as
|= sym
->ctype
.as
;
1534 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1535 sym
= sym
->ctype
.base_type
;
1537 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1538 ptr
->ctype
.as
|= sym
->ctype
.as
;
1539 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1540 sym
= sym
->ctype
.base_type
;
1542 ptr
->ctype
.base_type
= sym
;
1547 /* Arrays degenerate into pointers on pointer arithmetic */
1548 static struct symbol
*degenerate(struct expression
*expr
)
1550 struct symbol
*ctype
, *base
;
1554 ctype
= expr
->ctype
;
1557 base
= examine_symbol_type(ctype
);
1558 if (ctype
->type
== SYM_NODE
)
1559 base
= ctype
->ctype
.base_type
;
1561 * Arrays degenerate into pointers to the entries, while
1562 * functions degenerate into pointers to themselves.
1563 * If array was part of non-lvalue compound, we create a copy
1564 * of that compound first and then act as if we were dealing with
1565 * the corresponding field in there.
1567 switch (base
->type
) {
1569 if (expr
->type
== EXPR_SLICE
) {
1570 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1571 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1573 a
->ctype
.base_type
= expr
->base
->ctype
;
1574 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1575 a
->array_size
= expr
->base
->ctype
->array_size
;
1577 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1579 e0
->ctype
= &lazy_ptr_ctype
;
1581 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1584 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1586 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1588 e2
->right
= expr
->base
;
1590 e2
->ctype
= expr
->base
->ctype
;
1592 if (expr
->r_bitpos
) {
1593 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1596 e3
->right
= alloc_const_expression(expr
->pos
,
1597 bits_to_bytes(expr
->r_bitpos
));
1598 e3
->ctype
= &lazy_ptr_ctype
;
1603 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1606 e4
->ctype
= &lazy_ptr_ctype
;
1609 expr
->type
= EXPR_PREOP
;
1613 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1614 expression_error(expr
, "strange non-value function or array");
1617 *expr
= *expr
->unop
;
1618 ctype
= create_pointer(expr
, ctype
, 1);
1619 expr
->ctype
= ctype
;
1626 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1628 struct expression
*op
= expr
->unop
;
1629 struct symbol
*ctype
;
1631 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1632 expression_error(expr
, "not addressable");
1639 if (expr
->type
== EXPR_SYMBOL
) {
1640 struct symbol
*sym
= expr
->symbol
;
1641 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1645 * symbol expression evaluation is lazy about the type
1646 * of the sub-expression, so we may have to generate
1647 * the type here if so..
1649 if (expr
->ctype
== &lazy_ptr_ctype
) {
1650 ctype
= create_pointer(expr
, ctype
, 0);
1651 expr
->ctype
= ctype
;
1657 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1659 struct expression
*op
= expr
->unop
;
1660 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1662 /* Simplify: *&(expr) => (expr) */
1663 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1669 /* Dereferencing a node drops all the node information. */
1670 if (ctype
->type
== SYM_NODE
)
1671 ctype
= ctype
->ctype
.base_type
;
1673 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1674 target
= ctype
->ctype
.base_type
;
1676 switch (ctype
->type
) {
1678 expression_error(expr
, "cannot dereference this type");
1681 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1682 merge_type(node
, ctype
);
1686 if (!lvalue_expression(op
)) {
1687 expression_error(op
, "non-lvalue array??");
1691 /* Do the implied "addressof" on the array */
1695 * When an array is dereferenced, we need to pick
1696 * up the attributes of the original node too..
1698 merge_type(node
, op
->ctype
);
1699 merge_type(node
, ctype
);
1703 node
->bit_size
= target
->bit_size
;
1704 node
->array_size
= target
->array_size
;
1711 * Unary post-ops: x++ and x--
1713 static struct symbol
*evaluate_postop(struct expression
*expr
)
1715 struct expression
*op
= expr
->unop
;
1716 struct symbol
*ctype
= op
->ctype
;
1717 int class = classify_type(ctype
, &ctype
);
1720 if (!class || class & TYPE_COMPOUND
) {
1721 expression_error(expr
, "need scalar for ++/--");
1724 if (!lvalue_expression(expr
->unop
)) {
1725 expression_error(expr
, "need lvalue expression for ++/--");
1729 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1730 unrestrict(expr
, class, &ctype
);
1732 if (class & TYPE_NUM
) {
1734 } else if (class == TYPE_PTR
) {
1735 struct symbol
*target
= examine_pointer_target(ctype
);
1736 if (!is_function(target
))
1737 multiply
= bits_to_bytes(target
->bit_size
);
1741 evaluate_assign_to(op
, op
->ctype
);
1742 expr
->op_value
= multiply
;
1743 expr
->ctype
= ctype
;
1747 expression_error(expr
, "bad argument type for ++/--");
1751 static struct symbol
*evaluate_sign(struct expression
*expr
)
1753 struct symbol
*ctype
= expr
->unop
->ctype
;
1754 int class = classify_type(ctype
, &ctype
);
1755 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1757 /* should be an arithmetic type */
1758 if (!(class & TYPE_NUM
))
1759 return bad_expr_type(expr
);
1760 if (class & TYPE_RESTRICT
)
1763 if (!(class & TYPE_FLOAT
)) {
1764 ctype
= integer_promotion(ctype
);
1765 expr
->unop
= cast_to(expr
->unop
, ctype
);
1766 } else if (expr
->op
!= '~') {
1767 /* no conversions needed */
1769 return bad_expr_type(expr
);
1771 if (expr
->op
== '+')
1772 *expr
= *expr
->unop
;
1773 expr
->ctype
= ctype
;
1776 if (restricted_unop(expr
->op
, &ctype
))
1777 unrestrict(expr
, class, &ctype
);
1781 static struct symbol
*evaluate_preop(struct expression
*expr
)
1783 struct symbol
*ctype
= expr
->unop
->ctype
;
1787 *expr
= *expr
->unop
;
1793 return evaluate_sign(expr
);
1796 return evaluate_dereference(expr
);
1799 return evaluate_addressof(expr
);
1801 case SPECIAL_INCREMENT
:
1802 case SPECIAL_DECREMENT
:
1804 * From a type evaluation standpoint the preops are
1805 * the same as the postops
1807 return evaluate_postop(expr
);
1810 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1812 if (is_safe_type(ctype
))
1813 warning(expr
->pos
, "testing a 'safe expression'");
1814 if (is_float_type(ctype
)) {
1815 struct expression
*arg
= expr
->unop
;
1816 expr
->type
= EXPR_COMPARE
;
1817 expr
->op
= SPECIAL_EQUAL
;
1819 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1820 expr
->right
->ctype
= ctype
;
1821 expr
->right
->fvalue
= 0;
1822 } else if (is_fouled_type(ctype
)) {
1823 warning(expr
->pos
, "%s degrades to integer",
1824 show_typename(ctype
->ctype
.base_type
));
1826 /* the result is int [6.5.3.3(5)]*/
1833 expr
->ctype
= ctype
;
1837 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1839 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1840 struct ptr_list
*list
= head
;
1846 for (i
= 0; i
< list
->nr
; i
++) {
1847 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1849 if (sym
->ident
!= ident
)
1851 *offset
= sym
->offset
;
1854 struct symbol
*ctype
= sym
->ctype
.base_type
;
1858 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1860 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1863 *offset
+= sym
->offset
;
1867 } while ((list
= list
->next
) != head
);
1871 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1873 struct expression
*add
;
1876 * Create a new add-expression
1878 * NOTE! Even if we just add zero, we need a new node
1879 * for the member pointer, since it has a different
1880 * type than the original pointer. We could make that
1881 * be just a cast, but the fact is, a node is a node,
1882 * so we might as well just do the "add zero" here.
1884 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1887 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1888 add
->right
->ctype
= &int_ctype
;
1889 add
->right
->value
= offset
;
1892 * The ctype of the pointer will be lazily evaluated if
1893 * we ever take the address of this member dereference..
1895 add
->ctype
= &lazy_ptr_ctype
;
1899 /* structure/union dereference */
1900 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1903 struct symbol
*ctype
, *member
;
1904 struct expression
*deref
= expr
->deref
, *add
;
1905 struct ident
*ident
= expr
->member
;
1909 if (!evaluate_expression(deref
))
1912 expression_error(expr
, "bad member name");
1916 ctype
= deref
->ctype
;
1917 examine_symbol_type(ctype
);
1918 address_space
= ctype
->ctype
.as
;
1919 mod
= ctype
->ctype
.modifiers
;
1920 if (ctype
->type
== SYM_NODE
) {
1921 ctype
= ctype
->ctype
.base_type
;
1922 address_space
|= ctype
->ctype
.as
;
1923 mod
|= ctype
->ctype
.modifiers
;
1925 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1926 expression_error(expr
, "expected structure or union");
1930 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1932 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1933 const char *name
= "<unnamed>";
1936 name
= ctype
->ident
->name
;
1937 namelen
= ctype
->ident
->len
;
1939 if (ctype
->symbol_list
)
1940 expression_error(expr
, "no member '%s' in %s %.*s",
1941 show_ident(ident
), type
, namelen
, name
);
1943 expression_error(expr
, "using member '%s' in "
1944 "incomplete %s %.*s", show_ident(ident
),
1945 type
, namelen
, name
);
1950 * The member needs to take on the address space and modifiers of
1951 * the "parent" type.
1953 member
= convert_to_as_mod(member
, address_space
, mod
);
1954 ctype
= get_base_type(member
);
1956 if (!lvalue_expression(deref
)) {
1957 if (deref
->type
!= EXPR_SLICE
) {
1961 expr
->base
= deref
->base
;
1962 expr
->r_bitpos
= deref
->r_bitpos
;
1964 expr
->r_bitpos
+= bytes_to_bits(offset
);
1965 expr
->type
= EXPR_SLICE
;
1966 expr
->r_nrbits
= member
->bit_size
;
1967 expr
->r_bitpos
+= member
->bit_offset
;
1968 expr
->ctype
= member
;
1972 deref
= deref
->unop
;
1973 expr
->deref
= deref
;
1975 add
= evaluate_offset(deref
, offset
);
1976 expr
->type
= EXPR_PREOP
;
1980 expr
->ctype
= member
;
1984 static int is_promoted(struct expression
*expr
)
1987 switch (expr
->type
) {
1990 case EXPR_CONDITIONAL
:
2014 static struct symbol
*evaluate_cast(struct expression
*);
2016 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2018 struct symbol
*sym
= expr
->cast_type
;
2020 sym
= evaluate_expression(expr
->cast_expression
);
2024 * Expressions of restricted types will possibly get
2025 * promoted - check that here
2027 if (is_restricted_type(sym
)) {
2028 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2030 } else if (is_fouled_type(sym
)) {
2034 examine_symbol_type(sym
);
2035 if (is_bitfield_type(sym
)) {
2036 expression_error(expr
, "trying to examine bitfield type");
2042 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2044 struct symbol
*type
;
2047 type
= evaluate_type_information(expr
);
2051 size
= type
->bit_size
;
2053 if (size
< 0 && is_void_type(type
)) {
2054 warning(expr
->pos
, "expression using sizeof(void)");
2055 size
= bits_in_char
;
2058 if (size
== 1 && is_bool_type(type
)) {
2059 warning(expr
->pos
, "expression using sizeof bool");
2060 size
= bits_in_char
;
2063 if (is_function(type
->ctype
.base_type
)) {
2064 warning(expr
->pos
, "expression using sizeof on a function");
2065 size
= bits_in_char
;
2068 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2069 expression_error(expr
, "cannot size expression");
2071 expr
->type
= EXPR_VALUE
;
2072 expr
->value
= bits_to_bytes(size
);
2074 expr
->ctype
= size_t_ctype
;
2075 return size_t_ctype
;
2078 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2080 struct symbol
*type
;
2083 type
= evaluate_type_information(expr
);
2087 if (type
->type
== SYM_NODE
)
2088 type
= type
->ctype
.base_type
;
2091 switch (type
->type
) {
2095 type
= get_base_type(type
);
2099 expression_error(expr
, "expected pointer expression");
2102 size
= type
->bit_size
;
2103 if (size
& (bits_in_char
-1))
2105 expr
->type
= EXPR_VALUE
;
2106 expr
->value
= bits_to_bytes(size
);
2108 expr
->ctype
= size_t_ctype
;
2109 return size_t_ctype
;
2112 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2114 struct symbol
*type
;
2116 type
= evaluate_type_information(expr
);
2120 expr
->type
= EXPR_VALUE
;
2121 expr
->value
= type
->ctype
.alignment
;
2123 expr
->ctype
= size_t_ctype
;
2124 return size_t_ctype
;
2127 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2129 struct expression
*expr
;
2130 struct symbol_list
*argument_types
= fn
->arguments
;
2131 struct symbol
*argtype
;
2134 PREPARE_PTR_LIST(argument_types
, argtype
);
2135 FOR_EACH_PTR (head
, expr
) {
2136 struct expression
**p
= THIS_ADDRESS(expr
);
2137 struct symbol
*ctype
, *target
;
2138 ctype
= evaluate_expression(expr
);
2145 struct symbol
*type
;
2146 int class = classify_type(ctype
, &type
);
2147 if (is_int(class)) {
2148 *p
= cast_to(expr
, integer_promotion(type
));
2149 } else if (class & TYPE_FLOAT
) {
2150 unsigned long mod
= type
->ctype
.modifiers
;
2151 if (!(mod
& (MOD_LONG_ALL
)))
2152 *p
= cast_to(expr
, &double_ctype
);
2153 } else if (class & TYPE_PTR
) {
2154 if (expr
->ctype
== &null_ctype
)
2155 *p
= cast_to(expr
, &ptr_ctype
);
2159 } else if (!target
->forced_arg
){
2160 static char where
[30];
2161 examine_symbol_type(target
);
2162 sprintf(where
, "argument %d", i
);
2163 compatible_assignment_types(expr
, target
, p
, where
);
2167 NEXT_PTR_LIST(argtype
);
2168 } END_FOR_EACH_PTR(expr
);
2169 FINISH_PTR_LIST(argtype
);
2173 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2177 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2178 if (sym
->ident
== ident
)
2180 } END_FOR_EACH_PTR(sym
);
2184 static void convert_index(struct expression
*e
)
2186 struct expression
*child
= e
->idx_expression
;
2187 unsigned from
= e
->idx_from
;
2188 unsigned to
= e
->idx_to
+ 1;
2190 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2191 e
->init_nr
= to
- from
;
2192 e
->init_expr
= child
;
2195 static void convert_ident(struct expression
*e
)
2197 struct expression
*child
= e
->ident_expression
;
2198 struct symbol
*sym
= e
->field
;
2200 e
->init_offset
= sym
->offset
;
2202 e
->init_expr
= child
;
2205 static void convert_designators(struct expression
*e
)
2208 if (e
->type
== EXPR_INDEX
)
2210 else if (e
->type
== EXPR_IDENTIFIER
)
2218 static void excess(struct expression
*e
, const char *s
)
2220 warning(e
->pos
, "excessive elements in %s initializer", s
);
2224 * implicit designator for the first element
2226 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2227 struct expression
**v
)
2229 struct expression
*e
= *v
, *new;
2231 if (ctype
->type
== SYM_NODE
)
2232 ctype
= ctype
->ctype
.base_type
;
2234 if (class & TYPE_PTR
) { /* array */
2235 if (!ctype
->bit_size
)
2237 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2238 new->idx_expression
= e
;
2239 new->ctype
= ctype
->ctype
.base_type
;
2241 struct symbol
*field
, *p
;
2242 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2243 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2249 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2250 new->ident_expression
= e
;
2251 new->field
= new->ctype
= field
;
2258 * sanity-check explicit designators; return the innermost one or NULL
2259 * in case of error. Assign types.
2261 static struct expression
*check_designators(struct expression
*e
,
2262 struct symbol
*ctype
)
2264 struct expression
*last
= NULL
;
2267 if (ctype
->type
== SYM_NODE
)
2268 ctype
= ctype
->ctype
.base_type
;
2269 if (e
->type
== EXPR_INDEX
) {
2270 struct symbol
*type
;
2271 if (ctype
->type
!= SYM_ARRAY
) {
2272 err
= "array index in non-array";
2275 type
= ctype
->ctype
.base_type
;
2276 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2277 unsigned offset
= e
->idx_to
* type
->bit_size
;
2278 if (offset
>= ctype
->bit_size
) {
2279 err
= "index out of bounds in";
2283 e
->ctype
= ctype
= type
;
2286 if (!e
->idx_expression
) {
2290 e
= e
->idx_expression
;
2291 } else if (e
->type
== EXPR_IDENTIFIER
) {
2292 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2293 err
= "field name not in struct or union";
2296 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2298 err
= "unknown field name in";
2301 e
->field
= e
->ctype
= ctype
;
2303 if (!e
->ident_expression
) {
2307 e
= e
->ident_expression
;
2308 } else if (e
->type
== EXPR_POS
) {
2309 err
= "internal front-end error: EXPR_POS in";
2314 expression_error(e
, "%s initializer", err
);
2319 * choose the next subobject to initialize.
2321 * Get designators for next element, switch old ones to EXPR_POS.
2322 * Return the resulting expression or NULL if we'd run out of subobjects.
2323 * The innermost designator is returned in *v. Designators in old
2324 * are assumed to be already sanity-checked.
2326 static struct expression
*next_designators(struct expression
*old
,
2327 struct symbol
*ctype
,
2328 struct expression
*e
, struct expression
**v
)
2330 struct expression
*new = NULL
;
2334 if (old
->type
== EXPR_INDEX
) {
2335 struct expression
*copy
;
2338 copy
= next_designators(old
->idx_expression
,
2341 n
= old
->idx_to
+ 1;
2342 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2347 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2350 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2353 new->idx_from
= new->idx_to
= n
;
2354 new->idx_expression
= copy
;
2355 new->ctype
= old
->ctype
;
2357 } else if (old
->type
== EXPR_IDENTIFIER
) {
2358 struct expression
*copy
;
2359 struct symbol
*field
;
2361 copy
= next_designators(old
->ident_expression
,
2364 field
= old
->field
->next_subobject
;
2370 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2373 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2377 new->expr_ident
= field
->ident
;
2378 new->ident_expression
= copy
;
2385 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2386 int class, struct symbol
*ctype
);
2389 * deal with traversing subobjects [6.7.8(17,18,20)]
2391 static void handle_list_initializer(struct expression
*expr
,
2392 int class, struct symbol
*ctype
)
2394 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2397 FOR_EACH_PTR(expr
->expr_list
, e
) {
2398 struct expression
**v
;
2399 struct symbol
*type
;
2402 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2403 struct symbol
*struct_sym
;
2406 last
= first_subobject(ctype
, class, &top
);
2408 last
= next_designators(last
, ctype
, e
, &top
);
2411 excess(e
, class & TYPE_PTR
? "array" :
2413 DELETE_CURRENT_PTR(e
);
2416 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2417 if (Wdesignated_init
&& struct_sym
->designated_init
)
2418 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2419 ctype
->ident
? "in initializer for " : "",
2420 ctype
->ident
? ctype
->ident
->len
: 0,
2421 ctype
->ident
? ctype
->ident
->name
: "",
2422 ctype
->ident
? ": " : "",
2423 get_type_name(struct_sym
->type
),
2424 show_ident(struct_sym
->ident
));
2426 warning(e
->pos
, "advancing past deep designator");
2429 REPLACE_CURRENT_PTR(e
, last
);
2431 next
= check_designators(e
, ctype
);
2433 DELETE_CURRENT_PTR(e
);
2437 /* deeper than one designator? */
2439 convert_designators(last
);
2444 lclass
= classify_type(top
->ctype
, &type
);
2445 if (top
->type
== EXPR_INDEX
)
2446 v
= &top
->idx_expression
;
2448 v
= &top
->ident_expression
;
2450 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2453 if (!(lclass
& TYPE_COMPOUND
)) {
2454 warning(e
->pos
, "bogus scalar initializer");
2455 DELETE_CURRENT_PTR(e
);
2459 next
= first_subobject(type
, lclass
, v
);
2461 warning(e
->pos
, "missing braces around initializer");
2466 DELETE_CURRENT_PTR(e
);
2467 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2469 } END_FOR_EACH_PTR(e
);
2471 convert_designators(last
);
2472 expr
->ctype
= ctype
;
2475 static int is_string_literal(struct expression
**v
)
2477 struct expression
*e
= *v
;
2478 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2480 if (!e
|| e
->type
!= EXPR_STRING
)
2482 if (e
!= *v
&& Wparen_string
)
2484 "array initialized from parenthesized string constant");
2490 * We want a normal expression, possibly in one layer of braces. Warn
2491 * if the latter happens inside a list (it's legal, but likely to be
2492 * an effect of screwup). In case of anything not legal, we are definitely
2493 * having an effect of screwup, so just fail and let the caller warn.
2495 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2497 struct expression
*v
= NULL
, *p
;
2501 if (e
->type
!= EXPR_INITIALIZER
)
2504 FOR_EACH_PTR(e
->expr_list
, p
) {
2508 } END_FOR_EACH_PTR(p
);
2512 case EXPR_INITIALIZER
:
2514 case EXPR_IDENTIFIER
:
2520 warning(e
->pos
, "braces around scalar initializer");
2525 * deal with the cases that don't care about subobjects:
2526 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2527 * character array <- string literal, possibly in braces [6.7.8(14)]
2528 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2529 * compound type <- initializer list in braces [6.7.8(16)]
2530 * The last one punts to handle_list_initializer() which, in turn will call
2531 * us for individual elements of the list.
2533 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2534 * the lack of support of wide char stuff in general.
2536 * One note: we need to take care not to evaluate a string literal until
2537 * we know that we *will* handle it right here. Otherwise we would screw
2538 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2539 * { "string", ...} - we need to preserve that string literal recognizable
2540 * until we dig into the inner struct.
2542 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2543 int class, struct symbol
*ctype
)
2545 int is_string
= is_string_type(ctype
);
2546 struct expression
*e
= *ep
, *p
;
2547 struct symbol
*type
;
2553 if (!(class & TYPE_COMPOUND
)) {
2554 e
= handle_scalar(e
, nested
);
2558 if (!evaluate_expression(e
))
2560 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2565 * sublist; either a string, or we dig in; the latter will deal with
2566 * pathologies, so we don't need anything fancy here.
2568 if (e
->type
== EXPR_INITIALIZER
) {
2570 struct expression
*v
= NULL
;
2573 FOR_EACH_PTR(e
->expr_list
, p
) {
2577 } END_FOR_EACH_PTR(p
);
2578 if (count
== 1 && is_string_literal(&v
)) {
2583 handle_list_initializer(e
, class, ctype
);
2588 if (is_string_literal(&e
)) {
2589 /* either we are doing array of char, or we'll have to dig in */
2596 /* struct or union can be initialized by compatible */
2597 if (class != TYPE_COMPOUND
)
2599 type
= evaluate_expression(e
);
2602 if (ctype
->type
== SYM_NODE
)
2603 ctype
= ctype
->ctype
.base_type
;
2604 if (type
->type
== SYM_NODE
)
2605 type
= type
->ctype
.base_type
;
2611 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2613 type
= evaluate_expression(p
);
2614 if (ctype
->bit_size
!= -1) {
2615 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2617 "too long initializer-string for array of char");
2618 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2620 "too long initializer-string for array of char(no space for nul char)");
2627 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2629 struct symbol
*type
;
2630 int class = classify_type(ctype
, &type
);
2631 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2632 expression_error(*ep
, "invalid initializer");
2635 static struct symbol
*evaluate_cast(struct expression
*expr
)
2637 struct expression
*target
= expr
->cast_expression
;
2638 struct symbol
*ctype
;
2639 struct symbol
*t1
, *t2
;
2641 int as1
= 0, as2
= 0;
2647 * Special case: a cast can be followed by an
2648 * initializer, in which case we need to pass
2649 * the type value down to that initializer rather
2650 * than trying to evaluate it as an expression
2652 * A more complex case is when the initializer is
2653 * dereferenced as part of a post-fix expression.
2654 * We need to produce an expression that can be dereferenced.
2656 if (target
->type
== EXPR_INITIALIZER
) {
2657 struct symbol
*sym
= expr
->cast_type
;
2658 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2660 sym
->initializer
= target
;
2661 evaluate_symbol(sym
);
2663 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2666 expr
->type
= EXPR_PREOP
;
2674 ctype
= examine_symbol_type(expr
->cast_type
);
2675 expr
->ctype
= ctype
;
2676 expr
->cast_type
= ctype
;
2678 evaluate_expression(target
);
2681 class1
= classify_type(ctype
, &t1
);
2683 /* cast to non-integer type -> not an integer constant expression */
2684 if (!is_int(class1
))
2686 /* if argument turns out to be not an integer constant expression *and*
2687 it was not a floating literal to start with -> too bad */
2688 else if (expr
->flags
== Int_const_expr
&&
2689 !(target
->flags
& Int_const_expr
))
2692 * You can always throw a value away by casting to
2693 * "void" - that's an implicit "force". Note that
2694 * the same is _not_ true of "void *".
2696 if (t1
== &void_ctype
)
2699 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2700 warning(expr
->pos
, "cast to non-scalar");
2704 expression_error(expr
, "cast from unknown type");
2707 class2
= classify_type(t2
, &t2
);
2709 if (class2
& TYPE_COMPOUND
)
2710 warning(expr
->pos
, "cast from non-scalar");
2712 if (expr
->type
== EXPR_FORCE_CAST
)
2715 /* allowed cast unfouls */
2716 if (class2
& TYPE_FOULED
)
2720 if (class1
& TYPE_RESTRICT
)
2721 warning(expr
->pos
, "cast to %s",
2723 if (class2
& TYPE_RESTRICT
)
2724 warning(expr
->pos
, "cast from %s",
2728 if (t1
== &ulong_ctype
)
2730 else if (class1
== TYPE_PTR
) {
2731 examine_pointer_target(t1
);
2735 if (t2
== &ulong_ctype
)
2737 else if (class2
== TYPE_PTR
) {
2738 examine_pointer_target(t2
);
2742 if (!as1
&& as2
> 0)
2743 warning(expr
->pos
, "cast removes address space of expression");
2744 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2745 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2746 if (as1
> 0 && !as2
&&
2747 !is_null_pointer_constant(target
) && Wcast_to_as
)
2749 "cast adds address space to expression (<asn:%d>)", as1
);
2751 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2752 !as1
&& (target
->flags
& Int_const_expr
)) {
2753 if (t1
->ctype
.base_type
== &void_ctype
) {
2754 if (is_zero_constant(target
)) {
2756 expr
->type
= EXPR_VALUE
;
2757 expr
->ctype
= &null_ctype
;
2768 * Evaluate a call expression with a symbol. This
2769 * should expand inline functions, and evaluate
2772 static int evaluate_symbol_call(struct expression
*expr
)
2774 struct expression
*fn
= expr
->fn
;
2775 struct symbol
*ctype
= fn
->ctype
;
2777 if (fn
->type
!= EXPR_PREOP
)
2780 if (ctype
->op
&& ctype
->op
->evaluate
)
2781 return ctype
->op
->evaluate(expr
);
2783 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2785 struct symbol
*curr
= current_fn
;
2787 if (ctype
->definition
)
2788 ctype
= ctype
->definition
;
2790 current_fn
= ctype
->ctype
.base_type
;
2792 ret
= inline_function(expr
, ctype
);
2794 /* restore the old function */
2802 static struct symbol
*evaluate_call(struct expression
*expr
)
2805 struct symbol
*ctype
, *sym
;
2806 struct expression
*fn
= expr
->fn
;
2807 struct expression_list
*arglist
= expr
->args
;
2809 if (!evaluate_expression(fn
))
2811 sym
= ctype
= fn
->ctype
;
2812 if (ctype
->type
== SYM_NODE
)
2813 ctype
= ctype
->ctype
.base_type
;
2814 if (ctype
->type
== SYM_PTR
)
2815 ctype
= get_base_type(ctype
);
2817 if (ctype
->type
!= SYM_FN
) {
2818 struct expression
*arg
;
2819 expression_error(expr
, "not a function %s",
2820 show_ident(sym
->ident
));
2821 /* do typechecking in arguments */
2822 FOR_EACH_PTR (arglist
, arg
) {
2823 evaluate_expression(arg
);
2824 } END_FOR_EACH_PTR(arg
);
2828 examine_fn_arguments(ctype
);
2829 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2830 sym
->op
&& sym
->op
->args
) {
2831 if (!sym
->op
->args(expr
))
2834 if (!evaluate_arguments(sym
, ctype
, arglist
))
2836 args
= expression_list_size(expr
->args
);
2837 fnargs
= symbol_list_size(ctype
->arguments
);
2839 expression_error(expr
,
2840 "not enough arguments for function %s",
2841 show_ident(sym
->ident
));
2842 if (args
> fnargs
&& !ctype
->variadic
)
2843 expression_error(expr
,
2844 "too many arguments for function %s",
2845 show_ident(sym
->ident
));
2847 if (sym
->type
== SYM_NODE
) {
2848 if (evaluate_symbol_call(expr
))
2851 expr
->ctype
= ctype
->ctype
.base_type
;
2855 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2857 struct expression
*e
= expr
->down
;
2858 struct symbol
*ctype
= expr
->in
;
2861 if (expr
->op
== '.') {
2862 struct symbol
*field
;
2865 expression_error(expr
, "expected structure or union");
2868 examine_symbol_type(ctype
);
2869 class = classify_type(ctype
, &ctype
);
2870 if (class != TYPE_COMPOUND
) {
2871 expression_error(expr
, "expected structure or union");
2875 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2877 expression_error(expr
, "unknown member");
2881 expr
->type
= EXPR_VALUE
;
2882 expr
->flags
= Int_const_expr
;
2883 expr
->value
= offset
;
2885 expr
->ctype
= size_t_ctype
;
2888 expression_error(expr
, "expected structure or union");
2891 examine_symbol_type(ctype
);
2892 class = classify_type(ctype
, &ctype
);
2893 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2894 expression_error(expr
, "expected array");
2897 ctype
= ctype
->ctype
.base_type
;
2899 expr
->type
= EXPR_VALUE
;
2900 expr
->flags
= Int_const_expr
;
2903 expr
->ctype
= size_t_ctype
;
2905 struct expression
*idx
= expr
->index
, *m
;
2906 struct symbol
*i_type
= evaluate_expression(idx
);
2907 int i_class
= classify_type(i_type
, &i_type
);
2908 if (!is_int(i_class
)) {
2909 expression_error(expr
, "non-integer index");
2912 unrestrict(idx
, i_class
, &i_type
);
2913 idx
= cast_to(idx
, size_t_ctype
);
2914 m
= alloc_const_expression(expr
->pos
,
2915 bits_to_bytes(ctype
->bit_size
));
2916 m
->ctype
= size_t_ctype
;
2917 m
->flags
= Int_const_expr
;
2918 expr
->type
= EXPR_BINOP
;
2922 expr
->ctype
= size_t_ctype
;
2923 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2927 struct expression
*copy
= __alloc_expression(0);
2929 if (e
->type
== EXPR_OFFSETOF
)
2931 if (!evaluate_expression(e
))
2933 expr
->type
= EXPR_BINOP
;
2934 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2936 expr
->ctype
= size_t_ctype
;
2940 return size_t_ctype
;
2943 struct symbol
*evaluate_expression(struct expression
*expr
)
2950 switch (expr
->type
) {
2953 expression_error(expr
, "value expression without a type");
2956 return evaluate_string(expr
);
2958 return evaluate_symbol_expression(expr
);
2960 if (!evaluate_expression(expr
->left
))
2962 if (!evaluate_expression(expr
->right
))
2964 return evaluate_binop(expr
);
2966 return evaluate_logical(expr
);
2968 evaluate_expression(expr
->left
);
2969 if (!evaluate_expression(expr
->right
))
2971 return evaluate_comma(expr
);
2973 if (!evaluate_expression(expr
->left
))
2975 if (!evaluate_expression(expr
->right
))
2977 return evaluate_compare(expr
);
2978 case EXPR_ASSIGNMENT
:
2979 if (!evaluate_expression(expr
->left
))
2981 if (!evaluate_expression(expr
->right
))
2983 return evaluate_assignment(expr
);
2985 if (!evaluate_expression(expr
->unop
))
2987 return evaluate_preop(expr
);
2989 if (!evaluate_expression(expr
->unop
))
2991 return evaluate_postop(expr
);
2993 case EXPR_FORCE_CAST
:
2994 case EXPR_IMPLIED_CAST
:
2995 return evaluate_cast(expr
);
2997 return evaluate_sizeof(expr
);
2998 case EXPR_PTRSIZEOF
:
2999 return evaluate_ptrsizeof(expr
);
3001 return evaluate_alignof(expr
);
3003 return evaluate_member_dereference(expr
);
3005 return evaluate_call(expr
);
3007 case EXPR_CONDITIONAL
:
3008 return evaluate_conditional_expression(expr
);
3009 case EXPR_STATEMENT
:
3010 expr
->ctype
= evaluate_statement(expr
->statement
);
3014 expr
->ctype
= &ptr_ctype
;
3018 /* Evaluate the type of the symbol .. */
3019 evaluate_symbol(expr
->symbol
);
3020 /* .. but the type of the _expression_ is a "type" */
3021 expr
->ctype
= &type_ctype
;
3025 return evaluate_offsetof(expr
);
3027 /* These can not exist as stand-alone expressions */
3028 case EXPR_INITIALIZER
:
3029 case EXPR_IDENTIFIER
:
3032 expression_error(expr
, "internal front-end error: initializer in expression");
3035 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3041 static void check_duplicates(struct symbol
*sym
)
3044 struct symbol
*next
= sym
;
3046 while ((next
= next
->same_symbol
) != NULL
) {
3047 const char *typediff
;
3048 evaluate_symbol(next
);
3050 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3052 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3053 show_ident(sym
->ident
),
3054 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3059 unsigned long mod
= sym
->ctype
.modifiers
;
3060 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3062 if (!(mod
& MOD_TOPLEVEL
))
3066 if (sym
->ident
== &main_ident
)
3068 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3072 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3074 struct symbol
*base_type
;
3082 sym
= examine_symbol_type(sym
);
3083 base_type
= get_base_type(sym
);
3087 /* Evaluate the initializers */
3088 if (sym
->initializer
)
3089 evaluate_initializer(sym
, &sym
->initializer
);
3091 /* And finally, evaluate the body of the symbol too */
3092 if (base_type
->type
== SYM_FN
) {
3093 struct symbol
*curr
= current_fn
;
3095 if (sym
->definition
&& sym
->definition
!= sym
)
3096 return evaluate_symbol(sym
->definition
);
3098 current_fn
= base_type
;
3100 examine_fn_arguments(base_type
);
3101 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3103 if (base_type
->stmt
)
3104 evaluate_statement(base_type
->stmt
);
3112 void evaluate_symbol_list(struct symbol_list
*list
)
3116 FOR_EACH_PTR(list
, sym
) {
3117 evaluate_symbol(sym
);
3118 check_duplicates(sym
);
3119 } END_FOR_EACH_PTR(sym
);
3122 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3124 struct expression
*expr
= stmt
->expression
;
3125 struct symbol
*fntype
;
3127 evaluate_expression(expr
);
3128 fntype
= current_fn
->ctype
.base_type
;
3129 if (!fntype
|| fntype
== &void_ctype
) {
3130 if (expr
&& expr
->ctype
!= &void_ctype
)
3131 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3132 if (expr
&& Wreturn_void
)
3133 warning(stmt
->pos
, "returning void-valued expression");
3138 sparse_error(stmt
->pos
, "return with no return value");
3143 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3147 static void evaluate_if_statement(struct statement
*stmt
)
3149 if (!stmt
->if_conditional
)
3152 evaluate_conditional(stmt
->if_conditional
, 0);
3153 evaluate_statement(stmt
->if_true
);
3154 evaluate_statement(stmt
->if_false
);
3157 static void evaluate_iterator(struct statement
*stmt
)
3159 evaluate_symbol_list(stmt
->iterator_syms
);
3160 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3161 evaluate_conditional(stmt
->iterator_post_condition
,1);
3162 evaluate_statement(stmt
->iterator_pre_statement
);
3163 evaluate_statement(stmt
->iterator_statement
);
3164 evaluate_statement(stmt
->iterator_post_statement
);
3167 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3169 switch (*constraint
) {
3170 case '=': /* Assignment */
3171 case '+': /* Update */
3174 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3178 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3180 switch (*constraint
) {
3181 case '=': /* Assignment */
3182 case '+': /* Update */
3183 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3187 static void evaluate_asm_statement(struct statement
*stmt
)
3189 struct expression
*expr
;
3193 expr
= stmt
->asm_string
;
3194 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3195 sparse_error(stmt
->pos
, "need constant string for inline asm");
3200 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3202 case 0: /* Identifier */
3206 case 1: /* Constraint */
3208 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3209 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3210 *THIS_ADDRESS(expr
) = NULL
;
3213 verify_output_constraint(expr
, expr
->string
->data
);
3216 case 2: /* Expression */
3218 if (!evaluate_expression(expr
))
3220 if (!lvalue_expression(expr
))
3221 warning(expr
->pos
, "asm output is not an lvalue");
3222 evaluate_assign_to(expr
, expr
->ctype
);
3225 } END_FOR_EACH_PTR(expr
);
3228 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3230 case 0: /* Identifier */
3234 case 1: /* Constraint */
3236 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3237 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3238 *THIS_ADDRESS(expr
) = NULL
;
3241 verify_input_constraint(expr
, expr
->string
->data
);
3244 case 2: /* Expression */
3246 if (!evaluate_expression(expr
))
3250 } END_FOR_EACH_PTR(expr
);
3252 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3254 sparse_error(stmt
->pos
, "bad asm clobbers");
3257 if (expr
->type
== EXPR_STRING
)
3259 expression_error(expr
, "asm clobber is not a string");
3260 } END_FOR_EACH_PTR(expr
);
3262 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3263 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3264 sparse_error(stmt
->pos
, "bad asm label");
3267 } END_FOR_EACH_PTR(sym
);
3270 static void evaluate_case_statement(struct statement
*stmt
)
3272 evaluate_expression(stmt
->case_expression
);
3273 evaluate_expression(stmt
->case_to
);
3274 evaluate_statement(stmt
->case_statement
);
3277 static void check_case_type(struct expression
*switch_expr
,
3278 struct expression
*case_expr
,
3279 struct expression
**enumcase
)
3281 struct symbol
*switch_type
, *case_type
;
3287 switch_type
= switch_expr
->ctype
;
3288 case_type
= evaluate_expression(case_expr
);
3290 if (!switch_type
|| !case_type
)
3294 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3295 else if (is_enum_type(case_type
))
3296 *enumcase
= case_expr
;
3299 sclass
= classify_type(switch_type
, &switch_type
);
3300 cclass
= classify_type(case_type
, &case_type
);
3302 /* both should be arithmetic */
3303 if (!(sclass
& cclass
& TYPE_NUM
))
3306 /* neither should be floating */
3307 if ((sclass
| cclass
) & TYPE_FLOAT
)
3310 /* if neither is restricted, we are OK */
3311 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3314 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3315 cclass
, sclass
, case_type
, switch_type
)) {
3316 unrestrict(case_expr
, cclass
, &case_type
);
3317 unrestrict(switch_expr
, sclass
, &switch_type
);
3322 expression_error(case_expr
, "incompatible types for 'case' statement");
3325 static void evaluate_switch_statement(struct statement
*stmt
)
3328 struct expression
*enumcase
= NULL
;
3329 struct expression
**enumcase_holder
= &enumcase
;
3330 struct expression
*sel
= stmt
->switch_expression
;
3332 evaluate_expression(sel
);
3333 evaluate_statement(stmt
->switch_statement
);
3336 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3337 enumcase_holder
= NULL
; /* Only check cases against switch */
3339 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3340 struct statement
*case_stmt
= sym
->stmt
;
3341 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3342 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3343 } END_FOR_EACH_PTR(sym
);
3346 static void evaluate_goto_statement(struct statement
*stmt
)
3348 struct symbol
*label
= stmt
->goto_label
;
3350 if (label
&& !label
->stmt
&& !lookup_keyword(label
->ident
, NS_KEYWORD
))
3351 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3353 evaluate_expression(stmt
->goto_expression
);
3356 struct symbol
*evaluate_statement(struct statement
*stmt
)
3361 switch (stmt
->type
) {
3362 case STMT_DECLARATION
: {
3364 FOR_EACH_PTR(stmt
->declaration
, s
) {
3366 } END_FOR_EACH_PTR(s
);
3371 return evaluate_return_expression(stmt
);
3373 case STMT_EXPRESSION
:
3374 if (!evaluate_expression(stmt
->expression
))
3376 if (stmt
->expression
->ctype
== &null_ctype
)
3377 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3378 return degenerate(stmt
->expression
);
3380 case STMT_COMPOUND
: {
3381 struct statement
*s
;
3382 struct symbol
*type
= NULL
;
3384 /* Evaluate the return symbol in the compound statement */
3385 evaluate_symbol(stmt
->ret
);
3388 * Then, evaluate each statement, making the type of the
3389 * compound statement be the type of the last statement
3391 type
= evaluate_statement(stmt
->args
);
3392 FOR_EACH_PTR(stmt
->stmts
, s
) {
3393 type
= evaluate_statement(s
);
3394 } END_FOR_EACH_PTR(s
);
3400 evaluate_if_statement(stmt
);
3403 evaluate_iterator(stmt
);
3406 evaluate_switch_statement(stmt
);
3409 evaluate_case_statement(stmt
);
3412 return evaluate_statement(stmt
->label_statement
);
3414 evaluate_goto_statement(stmt
);
3419 evaluate_asm_statement(stmt
);
3422 evaluate_expression(stmt
->expression
);
3425 evaluate_expression(stmt
->range_expression
);
3426 evaluate_expression(stmt
->range_low
);
3427 evaluate_expression(stmt
->range_high
);