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
.attribute
->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
->attribute
->as
, as2
= c2
->attribute
->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
;
641 struct symbol
*base1
= t1
->ctype
.base_type
;
642 struct symbol
*base2
= t2
->ctype
.base_type
;
645 * FIXME! Collect alignment and context too here!
648 if (t1
&& t1
->type
!= SYM_PTR
) {
649 mod1
|= t1
->ctype
.modifiers
;
650 as1
|= t1
->ctype
.attribute
->as
;
656 if (t2
&& t2
->type
!= SYM_PTR
) {
657 mod2
|= t2
->ctype
.modifiers
;
658 as2
|= t2
->ctype
.attribute
->as
;
666 return "different types";
668 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
676 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
686 if (type
!= t2
->type
)
687 return "different base types";
691 sparse_error(t1
->pos
,
692 "internal error: bad type in derived(%d)",
696 return "different base types";
699 /* allow definition of incomplete structs and unions */
700 if (t1
->ident
== t2
->ident
)
702 return "different base types";
704 /* XXX: we ought to compare sizes */
708 return "different address spaces";
709 /* MOD_SPECIFIER is due to idiocy in parse.c */
710 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
711 return "different modifiers";
712 /* we could be lazier here */
713 base1
= examine_pointer_target(t1
);
714 base2
= examine_pointer_target(t2
);
715 mod1
= t1
->ctype
.modifiers
;
716 as1
= t1
->ctype
.attribute
->as
;
717 mod2
= t2
->ctype
.modifiers
;
718 as2
= t2
->ctype
.attribute
->as
;
721 struct symbol
*arg1
, *arg2
;
725 return "different address spaces";
726 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
727 return "different modifiers";
728 mod1
= t1
->ctype
.modifiers
;
729 as1
= t1
->ctype
.attribute
->as
;
730 mod2
= t2
->ctype
.modifiers
;
731 as2
= t2
->ctype
.attribute
->as
;
733 if (base1
->variadic
!= base2
->variadic
)
734 return "incompatible variadic arguments";
735 examine_fn_arguments(t1
);
736 examine_fn_arguments(t2
);
737 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
738 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
745 return "different argument counts";
746 diffstr
= type_difference(&arg1
->ctype
,
750 static char argdiff
[80];
751 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
758 FINISH_PTR_LIST(arg2
);
759 FINISH_PTR_LIST(arg1
);
764 return "different address spaces";
766 return "different base types";
767 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
771 return "different type sizes";
772 else if (diff
& ~MOD_SIGNEDNESS
)
773 return "different modifiers";
775 return "different signedness";
781 return "different address spaces";
782 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
783 return "different modifiers";
787 static void bad_null(struct expression
*expr
)
789 if (Wnon_pointer_null
)
790 warning(expr
->pos
, "Using plain integer as NULL pointer");
793 static unsigned long target_qualifiers(struct symbol
*type
)
795 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
796 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
801 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
803 const char *typediff
;
804 struct symbol
*ltype
, *rtype
;
805 struct expression
*l
= expr
->left
;
806 struct expression
*r
= expr
->right
;
807 struct symbol
*lbase
;
809 classify_type(degenerate(l
), <ype
);
810 classify_type(degenerate(r
), &rtype
);
812 lbase
= examine_pointer_target(ltype
);
813 examine_pointer_target(rtype
);
814 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
815 target_qualifiers(rtype
),
816 target_qualifiers(ltype
));
818 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
820 if (is_function(lbase
)) {
821 expression_error(expr
, "subtraction of functions? Share your drugs");
825 expr
->ctype
= ssize_t_ctype
;
826 if (lbase
->bit_size
> bits_in_char
) {
827 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
828 struct expression
*div
= expr
;
829 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
830 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
832 val
->ctype
= size_t_ctype
;
835 if (value
& (value
-1)) {
836 if (Wptr_subtraction_blows
)
837 warning(expr
->pos
, "potentially expensive pointer subtraction");
841 sub
->ctype
= ssize_t_ctype
;
850 return ssize_t_ctype
;
853 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
855 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
857 struct symbol
*ctype
;
862 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
863 warning(expr
->pos
, "assignment expression in conditional");
865 ctype
= evaluate_expression(expr
);
867 if (is_safe_type(ctype
))
868 warning(expr
->pos
, "testing a 'safe expression'");
874 static struct symbol
*evaluate_logical(struct expression
*expr
)
876 if (!evaluate_conditional(expr
->left
, 0))
878 if (!evaluate_conditional(expr
->right
, 0))
881 /* the result is int [6.5.13(3), 6.5.14(3)] */
882 expr
->ctype
= &int_ctype
;
884 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
890 static struct symbol
*evaluate_binop(struct expression
*expr
)
892 struct symbol
*ltype
, *rtype
, *ctype
;
893 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
894 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
898 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
902 /* number op number */
903 if (lclass
& rclass
& TYPE_NUM
) {
904 if ((lclass
| rclass
) & TYPE_FLOAT
) {
906 case '+': case '-': case '*': case '/':
909 return bad_expr_type(expr
);
913 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
914 // shifts do integer promotions, but that's it.
915 unrestrict(expr
->left
, lclass
, <ype
);
916 unrestrict(expr
->right
, rclass
, &rtype
);
917 ctype
= ltype
= integer_promotion(ltype
);
918 rtype
= integer_promotion(rtype
);
920 // The rest do usual conversions
921 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
922 && expr
->left
->op
== '!';
923 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
924 && expr
->right
->op
== '!';
925 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
926 warning(expr
->pos
, "dubious: %sx %c %sy",
929 right_not
? "!" : "");
931 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
932 lclass
, rclass
, ltype
, rtype
);
933 ctype
= rtype
= ltype
;
936 expr
->left
= cast_to(expr
->left
, ltype
);
937 expr
->right
= cast_to(expr
->right
, rtype
);
942 /* pointer (+|-) integer */
943 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
944 unrestrict(expr
->right
, rclass
, &rtype
);
945 return evaluate_ptr_add(expr
, rtype
);
948 /* integer + pointer */
949 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
950 struct expression
*index
= expr
->left
;
951 unrestrict(index
, lclass
, <ype
);
952 expr
->left
= expr
->right
;
954 return evaluate_ptr_add(expr
, ltype
);
957 /* pointer - pointer */
958 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
959 return evaluate_ptr_sub(expr
);
961 return bad_expr_type(expr
);
964 static struct symbol
*evaluate_comma(struct expression
*expr
)
966 expr
->ctype
= degenerate(expr
->right
);
967 if (expr
->ctype
== &null_ctype
)
968 expr
->ctype
= &ptr_ctype
;
969 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
973 static int modify_for_unsigned(int op
)
976 op
= SPECIAL_UNSIGNED_LT
;
978 op
= SPECIAL_UNSIGNED_GT
;
979 else if (op
== SPECIAL_LTE
)
980 op
= SPECIAL_UNSIGNED_LTE
;
981 else if (op
== SPECIAL_GTE
)
982 op
= SPECIAL_UNSIGNED_GTE
;
986 static inline int is_null_pointer_constant(struct expression
*e
)
988 if (e
->ctype
== &null_ctype
)
990 if (!(e
->flags
& Int_const_expr
))
992 return is_zero_constant(e
) ? 2 : 0;
995 static struct symbol
*evaluate_compare(struct expression
*expr
)
997 struct expression
*left
= expr
->left
, *right
= expr
->right
;
998 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
999 int lclass
= classify_type(degenerate(left
), <ype
);
1000 int rclass
= classify_type(degenerate(right
), &rtype
);
1001 struct symbol
*ctype
;
1002 const char *typediff
;
1005 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1010 if (is_type_type(ltype
) && is_type_type(rtype
))
1013 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1014 warning(expr
->pos
, "testing a 'safe expression'");
1016 /* number on number */
1017 if (lclass
& rclass
& TYPE_NUM
) {
1018 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1019 lclass
, rclass
, ltype
, rtype
);
1020 expr
->left
= cast_to(expr
->left
, ctype
);
1021 expr
->right
= cast_to(expr
->right
, ctype
);
1022 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1023 expr
->op
= modify_for_unsigned(expr
->op
);
1027 /* at least one must be a pointer */
1028 if (!((lclass
| rclass
) & TYPE_PTR
))
1029 return bad_expr_type(expr
);
1031 /* equality comparisons can be with null pointer constants */
1032 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1033 int is_null1
= is_null_pointer_constant(left
);
1034 int is_null2
= is_null_pointer_constant(right
);
1039 if (is_null1
&& is_null2
) {
1040 int positive
= expr
->op
== SPECIAL_EQUAL
;
1041 expr
->type
= EXPR_VALUE
;
1042 expr
->value
= positive
;
1045 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1046 left
= cast_to(left
, rtype
);
1049 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1050 right
= cast_to(right
, ltype
);
1054 /* both should be pointers */
1055 if (!(lclass
& rclass
& TYPE_PTR
))
1056 return bad_expr_type(expr
);
1057 expr
->op
= modify_for_unsigned(expr
->op
);
1059 lbase
= examine_pointer_target(ltype
);
1060 rbase
= examine_pointer_target(rtype
);
1062 /* they also have special treatment for pointers to void */
1063 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1064 if (ltype
->ctype
.attribute
->as
== rtype
->ctype
.attribute
->as
) {
1065 if (lbase
== &void_ctype
) {
1066 right
= cast_to(right
, ltype
);
1069 if (rbase
== &void_ctype
) {
1070 left
= cast_to(left
, rtype
);
1076 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1077 target_qualifiers(rtype
),
1078 target_qualifiers(ltype
));
1082 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1086 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1087 expr
->ctype
= &int_ctype
;
1092 * NOTE! The degenerate case of "x ? : y", where we don't
1093 * have a true case, this will possibly promote "x" to the
1094 * same type as "y", and thus _change_ the conditional
1095 * test in the expression. But since promotion is "safe"
1096 * for testing, that's OK.
1098 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1100 struct expression
**true;
1101 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1103 const char * typediff
;
1106 if (!evaluate_conditional(expr
->conditional
, 0))
1108 if (!evaluate_expression(expr
->cond_false
))
1111 ctype
= degenerate(expr
->conditional
);
1112 rtype
= degenerate(expr
->cond_false
);
1114 true = &expr
->conditional
;
1116 if (expr
->cond_true
) {
1117 if (!evaluate_expression(expr
->cond_true
))
1119 ltype
= degenerate(expr
->cond_true
);
1120 true = &expr
->cond_true
;
1124 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1125 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1130 lclass
= classify_type(ltype
, <ype
);
1131 rclass
= classify_type(rtype
, &rtype
);
1132 if (lclass
& rclass
& TYPE_NUM
) {
1133 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1134 lclass
, rclass
, ltype
, rtype
);
1135 *true = cast_to(*true, ctype
);
1136 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1140 if ((lclass
| rclass
) & TYPE_PTR
) {
1141 int is_null1
= is_null_pointer_constant(*true);
1142 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1144 if (is_null1
&& is_null2
) {
1145 *true = cast_to(*true, &ptr_ctype
);
1146 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1150 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1153 *true = cast_to(*true, rtype
);
1157 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1159 bad_null(expr
->cond_false
);
1160 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1164 if (!(lclass
& rclass
& TYPE_PTR
)) {
1165 typediff
= "different types";
1168 /* OK, it's pointer on pointer */
1169 if (ltype
->ctype
.attribute
->as
!= rtype
->ctype
.attribute
->as
) {
1170 typediff
= "different address spaces";
1174 /* need to be lazier here */
1175 lbase
= examine_pointer_target(ltype
);
1176 rbase
= examine_pointer_target(rtype
);
1177 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1179 if (lbase
== &void_ctype
) {
1180 /* XXX: pointers to function should warn here */
1185 if (rbase
== &void_ctype
) {
1186 /* XXX: pointers to function should warn here */
1190 /* XXX: that should be pointer to composite */
1192 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1199 /* void on void, struct on same struct, union on same union */
1200 if (ltype
== rtype
) {
1204 typediff
= "different base types";
1207 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1211 expr
->ctype
= ctype
;
1215 if (qual
& ~ctype
->ctype
.modifiers
) {
1216 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1218 sym
->ctype
.modifiers
|= qual
;
1221 *true = cast_to(*true, ctype
);
1222 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1226 /* FP assignments can not do modulo or bit operations */
1227 static int compatible_float_op(int op
)
1229 return op
== SPECIAL_ADD_ASSIGN
||
1230 op
== SPECIAL_SUB_ASSIGN
||
1231 op
== SPECIAL_MUL_ASSIGN
||
1232 op
== SPECIAL_DIV_ASSIGN
;
1235 static int evaluate_assign_op(struct expression
*expr
)
1237 struct symbol
*target
= expr
->left
->ctype
;
1238 struct symbol
*source
= expr
->right
->ctype
;
1239 struct symbol
*t
, *s
;
1240 int tclass
= classify_type(target
, &t
);
1241 int sclass
= classify_type(source
, &s
);
1244 if (tclass
& sclass
& TYPE_NUM
) {
1245 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1246 expression_error(expr
, "invalid assignment");
1249 if (tclass
& TYPE_RESTRICT
) {
1250 if (!restricted_binop(op
, t
)) {
1251 warning(expr
->pos
, "bad assignment (%s) to %s",
1252 show_special(op
), show_typename(t
));
1253 expr
->right
= cast_to(expr
->right
, target
);
1256 /* allowed assignments unfoul */
1257 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1259 if (!restricted_value(expr
->right
, t
))
1261 } else if (!(sclass
& TYPE_RESTRICT
))
1263 /* source and target would better be identical restricted */
1266 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1267 info(expr
->pos
, " left side has type %s", show_typename(t
));
1268 info(expr
->pos
, " right side has type %s", show_typename(s
));
1269 expr
->right
= cast_to(expr
->right
, target
);
1272 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1273 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1274 unrestrict(expr
->right
, sclass
, &s
);
1275 evaluate_ptr_add(expr
, s
);
1278 expression_error(expr
, "invalid pointer assignment");
1282 expression_error(expr
, "invalid assignment");
1286 expr
->right
= cast_to(expr
->right
, target
);
1290 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1293 return 0; /* yes, 0 - we don't want a cast_to here */
1294 if (t1
== &void_ctype
)
1296 if (t2
== &void_ctype
)
1298 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1300 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1304 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1306 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1311 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1312 struct expression
**rp
, const char *where
)
1314 const char *typediff
;
1315 struct symbol
*source
= degenerate(*rp
);
1316 struct symbol
*t
, *s
;
1317 int tclass
= classify_type(target
, &t
);
1318 int sclass
= classify_type(source
, &s
);
1320 if (tclass
& sclass
& TYPE_NUM
) {
1321 if (tclass
& TYPE_RESTRICT
) {
1322 /* allowed assignments unfoul */
1323 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1325 if (!restricted_value(*rp
, target
))
1329 } else if (!(sclass
& TYPE_RESTRICT
))
1331 typediff
= "different base types";
1335 if (tclass
== TYPE_PTR
) {
1336 unsigned long mod1
, mod2
;
1337 struct symbol
*b1
, *b2
;
1338 // NULL pointer is always OK
1339 int is_null
= is_null_pointer_constant(*rp
);
1345 if (!(sclass
& TYPE_PTR
)) {
1346 typediff
= "different base types";
1349 b1
= examine_pointer_target(t
);
1350 b2
= examine_pointer_target(s
);
1351 mod1
= target_qualifiers(t
);
1352 mod2
= target_qualifiers(s
);
1353 if (whitelist_pointers(b1
, b2
)) {
1355 * assignments to/from void * are OK, provided that
1356 * we do not remove qualifiers from pointed to [C]
1357 * or mix address spaces [sparse].
1359 if (t
->ctype
.attribute
->as
!= s
->ctype
.attribute
->as
) {
1360 typediff
= "different address spaces";
1364 typediff
= "different modifiers";
1369 /* It's OK if the target is more volatile or const than the source */
1370 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1376 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1379 if (tclass
& TYPE_NUM
) {
1380 /* XXX: need to turn into comparison with NULL */
1381 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1383 typediff
= "different base types";
1386 typediff
= "invalid types";
1389 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1390 info(expr
->pos
, " expected %s", show_typename(target
));
1391 info(expr
->pos
, " got %s", show_typename(source
));
1392 *rp
= cast_to(*rp
, target
);
1395 *rp
= cast_to(*rp
, target
);
1399 static void mark_assigned(struct expression
*expr
)
1405 switch (expr
->type
) {
1410 if (sym
->type
!= SYM_NODE
)
1412 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1416 mark_assigned(expr
->left
);
1417 mark_assigned(expr
->right
);
1420 case EXPR_FORCE_CAST
:
1421 mark_assigned(expr
->cast_expression
);
1424 mark_assigned(expr
->base
);
1432 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1434 if (type
->ctype
.modifiers
& MOD_CONST
)
1435 expression_error(left
, "assignment to const expression");
1437 /* We know left is an lvalue, so it's a "preop-*" */
1438 mark_assigned(left
->unop
);
1441 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1443 struct expression
*left
= expr
->left
;
1444 struct expression
*where
= expr
;
1445 struct symbol
*ltype
;
1447 if (!lvalue_expression(left
)) {
1448 expression_error(expr
, "not an lvalue");
1452 ltype
= left
->ctype
;
1454 if (expr
->op
!= '=') {
1455 if (!evaluate_assign_op(expr
))
1458 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1462 evaluate_assign_to(left
, ltype
);
1464 expr
->ctype
= ltype
;
1468 static void examine_fn_arguments(struct symbol
*fn
)
1472 FOR_EACH_PTR(fn
->arguments
, s
) {
1473 struct symbol
*arg
= evaluate_symbol(s
);
1474 /* Array/function arguments silently degenerate into pointers */
1480 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1481 if (arg
->type
== SYM_ARRAY
)
1482 ptr
->ctype
= arg
->ctype
;
1484 ptr
->ctype
.base_type
= arg
;
1485 merge_attr(&ptr
->ctype
, &s
->ctype
);
1486 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1488 s
->ctype
.base_type
= ptr
;
1489 s
->ctype
.attribute
= &null_attr
;
1490 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1493 examine_symbol_type(s
);
1500 } END_FOR_EACH_PTR(s
);
1503 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1505 /* Take the modifiers of the pointer, and apply them to the member */
1506 mod
|= sym
->ctype
.modifiers
;
1507 if (sym
->ctype
.attribute
->as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1508 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1510 attr_set_as(&newsym
->ctype
, as
);
1511 newsym
->ctype
.modifiers
= mod
;
1517 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1519 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1520 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1522 node
->ctype
.base_type
= ptr
;
1523 ptr
->bit_size
= bits_in_pointer
;
1524 ptr
->ctype
.alignment
= pointer_alignment
;
1526 node
->bit_size
= bits_in_pointer
;
1527 node
->ctype
.alignment
= pointer_alignment
;
1530 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1531 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1532 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1534 if (sym
->type
== SYM_NODE
) {
1535 merge_attr(&ptr
->ctype
, &sym
->ctype
);
1536 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1537 sym
= sym
->ctype
.base_type
;
1539 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1540 merge_attr(&ptr
->ctype
, &sym
->ctype
);
1541 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1542 sym
= sym
->ctype
.base_type
;
1544 ptr
->ctype
.base_type
= sym
;
1549 /* Arrays degenerate into pointers on pointer arithmetic */
1550 static struct symbol
*degenerate(struct expression
*expr
)
1552 struct symbol
*ctype
, *base
;
1556 ctype
= expr
->ctype
;
1559 base
= examine_symbol_type(ctype
);
1560 if (ctype
->type
== SYM_NODE
)
1561 base
= ctype
->ctype
.base_type
;
1563 * Arrays degenerate into pointers to the entries, while
1564 * functions degenerate into pointers to themselves.
1565 * If array was part of non-lvalue compound, we create a copy
1566 * of that compound first and then act as if we were dealing with
1567 * the corresponding field in there.
1569 switch (base
->type
) {
1571 if (expr
->type
== EXPR_SLICE
) {
1572 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1573 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1575 a
->ctype
.base_type
= expr
->base
->ctype
;
1576 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1577 a
->array_size
= expr
->base
->ctype
->array_size
;
1579 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1581 e0
->ctype
= &lazy_ptr_ctype
;
1583 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1586 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1588 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1590 e2
->right
= expr
->base
;
1592 e2
->ctype
= expr
->base
->ctype
;
1594 if (expr
->r_bitpos
) {
1595 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1598 e3
->right
= alloc_const_expression(expr
->pos
,
1599 bits_to_bytes(expr
->r_bitpos
));
1600 e3
->ctype
= &lazy_ptr_ctype
;
1605 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1608 e4
->ctype
= &lazy_ptr_ctype
;
1611 expr
->type
= EXPR_PREOP
;
1615 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1616 expression_error(expr
, "strange non-value function or array");
1619 *expr
= *expr
->unop
;
1620 ctype
= create_pointer(expr
, ctype
, 1);
1621 expr
->ctype
= ctype
;
1628 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1630 struct expression
*op
= expr
->unop
;
1631 struct symbol
*ctype
;
1633 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1634 expression_error(expr
, "not addressable");
1641 if (expr
->type
== EXPR_SYMBOL
) {
1642 struct symbol
*sym
= expr
->symbol
;
1643 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1647 * symbol expression evaluation is lazy about the type
1648 * of the sub-expression, so we may have to generate
1649 * the type here if so..
1651 if (expr
->ctype
== &lazy_ptr_ctype
) {
1652 ctype
= create_pointer(expr
, ctype
, 0);
1653 expr
->ctype
= ctype
;
1659 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1661 struct expression
*op
= expr
->unop
;
1662 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1664 /* Simplify: *&(expr) => (expr) */
1665 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1671 /* Dereferencing a node drops all the node information. */
1672 if (ctype
->type
== SYM_NODE
)
1673 ctype
= ctype
->ctype
.base_type
;
1675 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1676 target
= ctype
->ctype
.base_type
;
1678 switch (ctype
->type
) {
1680 expression_error(expr
, "cannot dereference this type");
1683 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1684 merge_type(node
, ctype
);
1688 if (!lvalue_expression(op
)) {
1689 expression_error(op
, "non-lvalue array??");
1693 /* Do the implied "addressof" on the array */
1697 * When an array is dereferenced, we need to pick
1698 * up the attributes of the original node too..
1700 merge_type(node
, op
->ctype
);
1701 merge_type(node
, ctype
);
1705 node
->bit_size
= target
->bit_size
;
1706 node
->array_size
= target
->array_size
;
1713 * Unary post-ops: x++ and x--
1715 static struct symbol
*evaluate_postop(struct expression
*expr
)
1717 struct expression
*op
= expr
->unop
;
1718 struct symbol
*ctype
= op
->ctype
;
1719 int class = classify_type(ctype
, &ctype
);
1722 if (!class || class & TYPE_COMPOUND
) {
1723 expression_error(expr
, "need scalar for ++/--");
1726 if (!lvalue_expression(expr
->unop
)) {
1727 expression_error(expr
, "need lvalue expression for ++/--");
1731 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1732 unrestrict(expr
, class, &ctype
);
1734 if (class & TYPE_NUM
) {
1736 } else if (class == TYPE_PTR
) {
1737 struct symbol
*target
= examine_pointer_target(ctype
);
1738 if (!is_function(target
))
1739 multiply
= bits_to_bytes(target
->bit_size
);
1743 evaluate_assign_to(op
, op
->ctype
);
1744 expr
->op_value
= multiply
;
1745 expr
->ctype
= ctype
;
1749 expression_error(expr
, "bad argument type for ++/--");
1753 static struct symbol
*evaluate_sign(struct expression
*expr
)
1755 struct symbol
*ctype
= expr
->unop
->ctype
;
1756 int class = classify_type(ctype
, &ctype
);
1757 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1759 /* should be an arithmetic type */
1760 if (!(class & TYPE_NUM
))
1761 return bad_expr_type(expr
);
1762 if (class & TYPE_RESTRICT
)
1765 if (!(class & TYPE_FLOAT
)) {
1766 ctype
= integer_promotion(ctype
);
1767 expr
->unop
= cast_to(expr
->unop
, ctype
);
1768 } else if (expr
->op
!= '~') {
1769 /* no conversions needed */
1771 return bad_expr_type(expr
);
1773 if (expr
->op
== '+')
1774 *expr
= *expr
->unop
;
1775 expr
->ctype
= ctype
;
1778 if (restricted_unop(expr
->op
, &ctype
))
1779 unrestrict(expr
, class, &ctype
);
1783 static struct symbol
*evaluate_preop(struct expression
*expr
)
1785 struct symbol
*ctype
= expr
->unop
->ctype
;
1789 *expr
= *expr
->unop
;
1795 return evaluate_sign(expr
);
1798 return evaluate_dereference(expr
);
1801 return evaluate_addressof(expr
);
1803 case SPECIAL_INCREMENT
:
1804 case SPECIAL_DECREMENT
:
1806 * From a type evaluation standpoint the preops are
1807 * the same as the postops
1809 return evaluate_postop(expr
);
1812 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1814 if (is_safe_type(ctype
))
1815 warning(expr
->pos
, "testing a 'safe expression'");
1816 if (is_float_type(ctype
)) {
1817 struct expression
*arg
= expr
->unop
;
1818 expr
->type
= EXPR_COMPARE
;
1819 expr
->op
= SPECIAL_EQUAL
;
1821 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1822 expr
->right
->ctype
= ctype
;
1823 expr
->right
->fvalue
= 0;
1824 } else if (is_fouled_type(ctype
)) {
1825 warning(expr
->pos
, "%s degrades to integer",
1826 show_typename(ctype
->ctype
.base_type
));
1828 /* the result is int [6.5.3.3(5)]*/
1835 expr
->ctype
= ctype
;
1839 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1841 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1842 struct ptr_list
*list
= head
;
1848 for (i
= 0; i
< list
->nr
; i
++) {
1849 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1851 if (sym
->ident
!= ident
)
1853 *offset
= sym
->offset
;
1856 struct symbol
*ctype
= sym
->ctype
.base_type
;
1860 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1862 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1865 *offset
+= sym
->offset
;
1869 } while ((list
= list
->next
) != head
);
1873 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1875 struct expression
*add
;
1878 * Create a new add-expression
1880 * NOTE! Even if we just add zero, we need a new node
1881 * for the member pointer, since it has a different
1882 * type than the original pointer. We could make that
1883 * be just a cast, but the fact is, a node is a node,
1884 * so we might as well just do the "add zero" here.
1886 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1889 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1890 add
->right
->ctype
= &int_ctype
;
1891 add
->right
->value
= offset
;
1894 * The ctype of the pointer will be lazily evaluated if
1895 * we ever take the address of this member dereference..
1897 add
->ctype
= &lazy_ptr_ctype
;
1901 /* structure/union dereference */
1902 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1905 struct symbol
*ctype
, *member
;
1906 struct expression
*deref
= expr
->deref
, *add
;
1907 struct ident
*ident
= expr
->member
;
1911 if (!evaluate_expression(deref
))
1914 expression_error(expr
, "bad member name");
1918 ctype
= deref
->ctype
;
1919 examine_symbol_type(ctype
);
1920 address_space
= ctype
->ctype
.attribute
->as
;
1921 mod
= ctype
->ctype
.modifiers
;
1922 if (ctype
->type
== SYM_NODE
) {
1923 ctype
= ctype
->ctype
.base_type
;
1924 address_space
|= ctype
->ctype
.attribute
->as
;
1925 mod
|= ctype
->ctype
.modifiers
;
1927 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1928 expression_error(expr
, "expected structure or union");
1932 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1934 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1935 const char *name
= "<unnamed>";
1938 name
= ctype
->ident
->name
;
1939 namelen
= ctype
->ident
->len
;
1941 if (ctype
->symbol_list
)
1942 expression_error(expr
, "no member '%s' in %s %.*s",
1943 show_ident(ident
), type
, namelen
, name
);
1945 expression_error(expr
, "using member '%s' in "
1946 "incomplete %s %.*s", show_ident(ident
),
1947 type
, namelen
, name
);
1952 * The member needs to take on the address space and modifiers of
1953 * the "parent" type.
1955 member
= convert_to_as_mod(member
, address_space
, mod
);
1956 ctype
= get_base_type(member
);
1958 if (!lvalue_expression(deref
)) {
1959 if (deref
->type
!= EXPR_SLICE
) {
1963 expr
->base
= deref
->base
;
1964 expr
->r_bitpos
= deref
->r_bitpos
;
1966 expr
->r_bitpos
+= bytes_to_bits(offset
);
1967 expr
->type
= EXPR_SLICE
;
1968 expr
->r_nrbits
= member
->bit_size
;
1969 expr
->r_bitpos
+= member
->bit_offset
;
1970 expr
->ctype
= member
;
1974 deref
= deref
->unop
;
1975 expr
->deref
= deref
;
1977 add
= evaluate_offset(deref
, offset
);
1978 expr
->type
= EXPR_PREOP
;
1982 expr
->ctype
= member
;
1986 static int is_promoted(struct expression
*expr
)
1989 switch (expr
->type
) {
1992 case EXPR_CONDITIONAL
:
2016 static struct symbol
*evaluate_cast(struct expression
*);
2018 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2020 struct symbol
*sym
= expr
->cast_type
;
2022 sym
= evaluate_expression(expr
->cast_expression
);
2026 * Expressions of restricted types will possibly get
2027 * promoted - check that here
2029 if (is_restricted_type(sym
)) {
2030 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2032 } else if (is_fouled_type(sym
)) {
2036 examine_symbol_type(sym
);
2037 if (is_bitfield_type(sym
)) {
2038 expression_error(expr
, "trying to examine bitfield type");
2044 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2046 struct symbol
*type
;
2049 type
= evaluate_type_information(expr
);
2053 size
= type
->bit_size
;
2055 if (size
< 0 && is_void_type(type
)) {
2056 warning(expr
->pos
, "expression using sizeof(void)");
2057 size
= bits_in_char
;
2060 if (size
== 1 && is_bool_type(type
)) {
2061 warning(expr
->pos
, "expression using sizeof bool");
2062 size
= bits_in_char
;
2065 if (is_function(type
->ctype
.base_type
)) {
2066 warning(expr
->pos
, "expression using sizeof on a function");
2067 size
= bits_in_char
;
2070 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2071 expression_error(expr
, "cannot size expression");
2073 expr
->type
= EXPR_VALUE
;
2074 expr
->value
= bits_to_bytes(size
);
2076 expr
->ctype
= size_t_ctype
;
2077 return size_t_ctype
;
2080 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2082 struct symbol
*type
;
2085 type
= evaluate_type_information(expr
);
2089 if (type
->type
== SYM_NODE
)
2090 type
= type
->ctype
.base_type
;
2093 switch (type
->type
) {
2097 type
= get_base_type(type
);
2101 expression_error(expr
, "expected pointer expression");
2104 size
= type
->bit_size
;
2105 if (size
& (bits_in_char
-1))
2107 expr
->type
= EXPR_VALUE
;
2108 expr
->value
= bits_to_bytes(size
);
2110 expr
->ctype
= size_t_ctype
;
2111 return size_t_ctype
;
2114 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2116 struct symbol
*type
;
2118 type
= evaluate_type_information(expr
);
2122 expr
->type
= EXPR_VALUE
;
2123 expr
->value
= type
->ctype
.alignment
;
2125 expr
->ctype
= size_t_ctype
;
2126 return size_t_ctype
;
2129 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2131 struct expression
*expr
;
2132 struct symbol_list
*argument_types
= fn
->arguments
;
2133 struct symbol
*argtype
;
2136 PREPARE_PTR_LIST(argument_types
, argtype
);
2137 FOR_EACH_PTR (head
, expr
) {
2138 struct expression
**p
= THIS_ADDRESS(expr
);
2139 struct symbol
*ctype
, *target
;
2140 ctype
= evaluate_expression(expr
);
2147 struct symbol
*type
;
2148 int class = classify_type(ctype
, &type
);
2149 if (is_int(class)) {
2150 *p
= cast_to(expr
, integer_promotion(type
));
2151 } else if (class & TYPE_FLOAT
) {
2152 unsigned long mod
= type
->ctype
.modifiers
;
2153 if (!(mod
& (MOD_LONG_ALL
)))
2154 *p
= cast_to(expr
, &double_ctype
);
2155 } else if (class & TYPE_PTR
) {
2156 if (expr
->ctype
== &null_ctype
)
2157 *p
= cast_to(expr
, &ptr_ctype
);
2161 } else if (!target
->forced_arg
){
2162 static char where
[30];
2163 examine_symbol_type(target
);
2164 sprintf(where
, "argument %d", i
);
2165 compatible_assignment_types(expr
, target
, p
, where
);
2169 NEXT_PTR_LIST(argtype
);
2170 } END_FOR_EACH_PTR(expr
);
2171 FINISH_PTR_LIST(argtype
);
2175 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2179 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2180 if (sym
->ident
== ident
)
2182 } END_FOR_EACH_PTR(sym
);
2186 static void convert_index(struct expression
*e
)
2188 struct expression
*child
= e
->idx_expression
;
2189 unsigned from
= e
->idx_from
;
2190 unsigned to
= e
->idx_to
+ 1;
2192 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2193 e
->init_nr
= to
- from
;
2194 e
->init_expr
= child
;
2197 static void convert_ident(struct expression
*e
)
2199 struct expression
*child
= e
->ident_expression
;
2200 struct symbol
*sym
= e
->field
;
2202 e
->init_offset
= sym
->offset
;
2204 e
->init_expr
= child
;
2207 static void convert_designators(struct expression
*e
)
2210 if (e
->type
== EXPR_INDEX
)
2212 else if (e
->type
== EXPR_IDENTIFIER
)
2220 static void excess(struct expression
*e
, const char *s
)
2222 warning(e
->pos
, "excessive elements in %s initializer", s
);
2226 * implicit designator for the first element
2228 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2229 struct expression
**v
)
2231 struct expression
*e
= *v
, *new;
2233 if (ctype
->type
== SYM_NODE
)
2234 ctype
= ctype
->ctype
.base_type
;
2236 if (class & TYPE_PTR
) { /* array */
2237 if (!ctype
->bit_size
)
2239 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2240 new->idx_expression
= e
;
2241 new->ctype
= ctype
->ctype
.base_type
;
2243 struct symbol
*field
, *p
;
2244 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2245 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2251 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2252 new->ident_expression
= e
;
2253 new->field
= new->ctype
= field
;
2260 * sanity-check explicit designators; return the innermost one or NULL
2261 * in case of error. Assign types.
2263 static struct expression
*check_designators(struct expression
*e
,
2264 struct symbol
*ctype
)
2266 struct expression
*last
= NULL
;
2269 if (ctype
->type
== SYM_NODE
)
2270 ctype
= ctype
->ctype
.base_type
;
2271 if (e
->type
== EXPR_INDEX
) {
2272 struct symbol
*type
;
2273 if (ctype
->type
!= SYM_ARRAY
) {
2274 err
= "array index in non-array";
2277 type
= ctype
->ctype
.base_type
;
2278 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2279 unsigned offset
= e
->idx_to
* type
->bit_size
;
2280 if (offset
>= ctype
->bit_size
) {
2281 err
= "index out of bounds in";
2285 e
->ctype
= ctype
= type
;
2288 if (!e
->idx_expression
) {
2292 e
= e
->idx_expression
;
2293 } else if (e
->type
== EXPR_IDENTIFIER
) {
2294 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2295 err
= "field name not in struct or union";
2298 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2300 err
= "unknown field name in";
2303 e
->field
= e
->ctype
= ctype
;
2305 if (!e
->ident_expression
) {
2309 e
= e
->ident_expression
;
2310 } else if (e
->type
== EXPR_POS
) {
2311 err
= "internal front-end error: EXPR_POS in";
2316 expression_error(e
, "%s initializer", err
);
2321 * choose the next subobject to initialize.
2323 * Get designators for next element, switch old ones to EXPR_POS.
2324 * Return the resulting expression or NULL if we'd run out of subobjects.
2325 * The innermost designator is returned in *v. Designators in old
2326 * are assumed to be already sanity-checked.
2328 static struct expression
*next_designators(struct expression
*old
,
2329 struct symbol
*ctype
,
2330 struct expression
*e
, struct expression
**v
)
2332 struct expression
*new = NULL
;
2336 if (old
->type
== EXPR_INDEX
) {
2337 struct expression
*copy
;
2340 copy
= next_designators(old
->idx_expression
,
2343 n
= old
->idx_to
+ 1;
2344 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2349 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2352 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2355 new->idx_from
= new->idx_to
= n
;
2356 new->idx_expression
= copy
;
2357 new->ctype
= old
->ctype
;
2359 } else if (old
->type
== EXPR_IDENTIFIER
) {
2360 struct expression
*copy
;
2361 struct symbol
*field
;
2363 copy
= next_designators(old
->ident_expression
,
2366 field
= old
->field
->next_subobject
;
2372 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2375 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2379 new->expr_ident
= field
->ident
;
2380 new->ident_expression
= copy
;
2387 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2388 int class, struct symbol
*ctype
);
2391 * deal with traversing subobjects [6.7.8(17,18,20)]
2393 static void handle_list_initializer(struct expression
*expr
,
2394 int class, struct symbol
*ctype
)
2396 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2399 FOR_EACH_PTR(expr
->expr_list
, e
) {
2400 struct expression
**v
;
2401 struct symbol
*type
;
2404 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2405 struct symbol
*struct_sym
;
2408 last
= first_subobject(ctype
, class, &top
);
2410 last
= next_designators(last
, ctype
, e
, &top
);
2413 excess(e
, class & TYPE_PTR
? "array" :
2415 DELETE_CURRENT_PTR(e
);
2418 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2419 if (Wdesignated_init
&& struct_sym
->designated_init
)
2420 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2421 ctype
->ident
? "in initializer for " : "",
2422 ctype
->ident
? ctype
->ident
->len
: 0,
2423 ctype
->ident
? ctype
->ident
->name
: "",
2424 ctype
->ident
? ": " : "",
2425 get_type_name(struct_sym
->type
),
2426 show_ident(struct_sym
->ident
));
2428 warning(e
->pos
, "advancing past deep designator");
2431 REPLACE_CURRENT_PTR(e
, last
);
2433 next
= check_designators(e
, ctype
);
2435 DELETE_CURRENT_PTR(e
);
2439 /* deeper than one designator? */
2441 convert_designators(last
);
2446 lclass
= classify_type(top
->ctype
, &type
);
2447 if (top
->type
== EXPR_INDEX
)
2448 v
= &top
->idx_expression
;
2450 v
= &top
->ident_expression
;
2452 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2455 if (!(lclass
& TYPE_COMPOUND
)) {
2456 warning(e
->pos
, "bogus scalar initializer");
2457 DELETE_CURRENT_PTR(e
);
2461 next
= first_subobject(type
, lclass
, v
);
2463 warning(e
->pos
, "missing braces around initializer");
2468 DELETE_CURRENT_PTR(e
);
2469 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2471 } END_FOR_EACH_PTR(e
);
2473 convert_designators(last
);
2474 expr
->ctype
= ctype
;
2477 static int is_string_literal(struct expression
**v
)
2479 struct expression
*e
= *v
;
2480 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2482 if (!e
|| e
->type
!= EXPR_STRING
)
2484 if (e
!= *v
&& Wparen_string
)
2486 "array initialized from parenthesized string constant");
2492 * We want a normal expression, possibly in one layer of braces. Warn
2493 * if the latter happens inside a list (it's legal, but likely to be
2494 * an effect of screwup). In case of anything not legal, we are definitely
2495 * having an effect of screwup, so just fail and let the caller warn.
2497 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2499 struct expression
*v
= NULL
, *p
;
2503 if (e
->type
!= EXPR_INITIALIZER
)
2506 FOR_EACH_PTR(e
->expr_list
, p
) {
2510 } END_FOR_EACH_PTR(p
);
2514 case EXPR_INITIALIZER
:
2516 case EXPR_IDENTIFIER
:
2522 warning(e
->pos
, "braces around scalar initializer");
2527 * deal with the cases that don't care about subobjects:
2528 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2529 * character array <- string literal, possibly in braces [6.7.8(14)]
2530 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2531 * compound type <- initializer list in braces [6.7.8(16)]
2532 * The last one punts to handle_list_initializer() which, in turn will call
2533 * us for individual elements of the list.
2535 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2536 * the lack of support of wide char stuff in general.
2538 * One note: we need to take care not to evaluate a string literal until
2539 * we know that we *will* handle it right here. Otherwise we would screw
2540 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2541 * { "string", ...} - we need to preserve that string literal recognizable
2542 * until we dig into the inner struct.
2544 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2545 int class, struct symbol
*ctype
)
2547 int is_string
= is_string_type(ctype
);
2548 struct expression
*e
= *ep
, *p
;
2549 struct symbol
*type
;
2555 if (!(class & TYPE_COMPOUND
)) {
2556 e
= handle_scalar(e
, nested
);
2560 if (!evaluate_expression(e
))
2562 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2567 * sublist; either a string, or we dig in; the latter will deal with
2568 * pathologies, so we don't need anything fancy here.
2570 if (e
->type
== EXPR_INITIALIZER
) {
2572 struct expression
*v
= NULL
;
2575 FOR_EACH_PTR(e
->expr_list
, p
) {
2579 } END_FOR_EACH_PTR(p
);
2580 if (count
== 1 && is_string_literal(&v
)) {
2585 handle_list_initializer(e
, class, ctype
);
2590 if (is_string_literal(&e
)) {
2591 /* either we are doing array of char, or we'll have to dig in */
2598 /* struct or union can be initialized by compatible */
2599 if (class != TYPE_COMPOUND
)
2601 type
= evaluate_expression(e
);
2604 if (ctype
->type
== SYM_NODE
)
2605 ctype
= ctype
->ctype
.base_type
;
2606 if (type
->type
== SYM_NODE
)
2607 type
= type
->ctype
.base_type
;
2613 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2615 type
= evaluate_expression(p
);
2616 if (ctype
->bit_size
!= -1) {
2617 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2619 "too long initializer-string for array of char");
2620 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2622 "too long initializer-string for array of char(no space for nul char)");
2629 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2631 struct symbol
*type
;
2632 int class = classify_type(ctype
, &type
);
2633 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2634 expression_error(*ep
, "invalid initializer");
2637 static struct symbol
*evaluate_cast(struct expression
*expr
)
2639 struct expression
*target
= expr
->cast_expression
;
2640 struct symbol
*ctype
;
2641 struct symbol
*t1
, *t2
;
2643 int as1
= 0, as2
= 0;
2649 * Special case: a cast can be followed by an
2650 * initializer, in which case we need to pass
2651 * the type value down to that initializer rather
2652 * than trying to evaluate it as an expression
2654 * A more complex case is when the initializer is
2655 * dereferenced as part of a post-fix expression.
2656 * We need to produce an expression that can be dereferenced.
2658 if (target
->type
== EXPR_INITIALIZER
) {
2659 struct symbol
*sym
= expr
->cast_type
;
2660 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2662 sym
->initializer
= target
;
2663 evaluate_symbol(sym
);
2665 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2668 expr
->type
= EXPR_PREOP
;
2676 ctype
= examine_symbol_type(expr
->cast_type
);
2677 expr
->ctype
= ctype
;
2678 expr
->cast_type
= ctype
;
2680 evaluate_expression(target
);
2683 class1
= classify_type(ctype
, &t1
);
2685 /* cast to non-integer type -> not an integer constant expression */
2686 if (!is_int(class1
))
2688 /* if argument turns out to be not an integer constant expression *and*
2689 it was not a floating literal to start with -> too bad */
2690 else if (expr
->flags
== Int_const_expr
&&
2691 !(target
->flags
& Int_const_expr
))
2694 * You can always throw a value away by casting to
2695 * "void" - that's an implicit "force". Note that
2696 * the same is _not_ true of "void *".
2698 if (t1
== &void_ctype
)
2701 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2702 warning(expr
->pos
, "cast to non-scalar");
2706 expression_error(expr
, "cast from unknown type");
2709 class2
= classify_type(t2
, &t2
);
2711 if (class2
& TYPE_COMPOUND
)
2712 warning(expr
->pos
, "cast from non-scalar");
2714 if (expr
->type
== EXPR_FORCE_CAST
)
2717 /* allowed cast unfouls */
2718 if (class2
& TYPE_FOULED
)
2722 if (class1
& TYPE_RESTRICT
)
2723 warning(expr
->pos
, "cast to %s",
2725 if (class2
& TYPE_RESTRICT
)
2726 warning(expr
->pos
, "cast from %s",
2730 if (t1
== &ulong_ctype
)
2732 else if (class1
== TYPE_PTR
) {
2733 examine_pointer_target(t1
);
2734 as1
= t1
->ctype
.attribute
->as
;
2737 if (t2
== &ulong_ctype
)
2739 else if (class2
== TYPE_PTR
) {
2740 examine_pointer_target(t2
);
2741 as2
= t2
->ctype
.attribute
->as
;
2744 if (!as1
&& as2
> 0)
2745 warning(expr
->pos
, "cast removes address space of expression");
2746 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2747 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2748 if (as1
> 0 && !as2
&&
2749 !is_null_pointer_constant(target
) && Wcast_to_as
)
2751 "cast adds address space to expression (<asn:%d>)", as1
);
2753 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2754 !as1
&& (target
->flags
& Int_const_expr
)) {
2755 if (t1
->ctype
.base_type
== &void_ctype
) {
2756 if (is_zero_constant(target
)) {
2758 expr
->type
= EXPR_VALUE
;
2759 expr
->ctype
= &null_ctype
;
2770 * Evaluate a call expression with a symbol. This
2771 * should expand inline functions, and evaluate
2774 static int evaluate_symbol_call(struct expression
*expr
)
2776 struct expression
*fn
= expr
->fn
;
2777 struct symbol
*ctype
= fn
->ctype
;
2779 if (fn
->type
!= EXPR_PREOP
)
2782 if (ctype
->op
&& ctype
->op
->evaluate
)
2783 return ctype
->op
->evaluate(expr
);
2785 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2787 struct symbol
*curr
= current_fn
;
2789 if (ctype
->definition
)
2790 ctype
= ctype
->definition
;
2792 current_fn
= ctype
->ctype
.base_type
;
2794 ret
= inline_function(expr
, ctype
);
2796 /* restore the old function */
2804 static struct symbol
*evaluate_call(struct expression
*expr
)
2807 struct symbol
*ctype
, *sym
;
2808 struct expression
*fn
= expr
->fn
;
2809 struct expression_list
*arglist
= expr
->args
;
2811 if (!evaluate_expression(fn
))
2813 sym
= ctype
= fn
->ctype
;
2814 if (ctype
->type
== SYM_NODE
)
2815 ctype
= ctype
->ctype
.base_type
;
2816 if (ctype
->type
== SYM_PTR
)
2817 ctype
= get_base_type(ctype
);
2819 if (ctype
->type
!= SYM_FN
) {
2820 struct expression
*arg
;
2821 expression_error(expr
, "not a function %s",
2822 show_ident(sym
->ident
));
2823 /* do typechecking in arguments */
2824 FOR_EACH_PTR (arglist
, arg
) {
2825 evaluate_expression(arg
);
2826 } END_FOR_EACH_PTR(arg
);
2830 examine_fn_arguments(ctype
);
2831 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2832 sym
->op
&& sym
->op
->args
) {
2833 if (!sym
->op
->args(expr
))
2836 if (!evaluate_arguments(sym
, ctype
, arglist
))
2838 args
= expression_list_size(expr
->args
);
2839 fnargs
= symbol_list_size(ctype
->arguments
);
2841 expression_error(expr
,
2842 "not enough arguments for function %s",
2843 show_ident(sym
->ident
));
2844 if (args
> fnargs
&& !ctype
->variadic
)
2845 expression_error(expr
,
2846 "too many arguments for function %s",
2847 show_ident(sym
->ident
));
2849 if (sym
->type
== SYM_NODE
) {
2850 if (evaluate_symbol_call(expr
))
2853 expr
->ctype
= ctype
->ctype
.base_type
;
2857 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2859 struct expression
*e
= expr
->down
;
2860 struct symbol
*ctype
= expr
->in
;
2863 if (expr
->op
== '.') {
2864 struct symbol
*field
;
2867 expression_error(expr
, "expected structure or union");
2870 examine_symbol_type(ctype
);
2871 class = classify_type(ctype
, &ctype
);
2872 if (class != TYPE_COMPOUND
) {
2873 expression_error(expr
, "expected structure or union");
2877 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2879 expression_error(expr
, "unknown member");
2883 expr
->type
= EXPR_VALUE
;
2884 expr
->flags
= Int_const_expr
;
2885 expr
->value
= offset
;
2887 expr
->ctype
= size_t_ctype
;
2890 expression_error(expr
, "expected structure or union");
2893 examine_symbol_type(ctype
);
2894 class = classify_type(ctype
, &ctype
);
2895 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2896 expression_error(expr
, "expected array");
2899 ctype
= ctype
->ctype
.base_type
;
2901 expr
->type
= EXPR_VALUE
;
2902 expr
->flags
= Int_const_expr
;
2905 expr
->ctype
= size_t_ctype
;
2907 struct expression
*idx
= expr
->index
, *m
;
2908 struct symbol
*i_type
= evaluate_expression(idx
);
2909 int i_class
= classify_type(i_type
, &i_type
);
2910 if (!is_int(i_class
)) {
2911 expression_error(expr
, "non-integer index");
2914 unrestrict(idx
, i_class
, &i_type
);
2915 idx
= cast_to(idx
, size_t_ctype
);
2916 m
= alloc_const_expression(expr
->pos
,
2917 bits_to_bytes(ctype
->bit_size
));
2918 m
->ctype
= size_t_ctype
;
2919 m
->flags
= Int_const_expr
;
2920 expr
->type
= EXPR_BINOP
;
2924 expr
->ctype
= size_t_ctype
;
2925 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2929 struct expression
*copy
= __alloc_expression(0);
2931 if (e
->type
== EXPR_OFFSETOF
)
2933 if (!evaluate_expression(e
))
2935 expr
->type
= EXPR_BINOP
;
2936 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2938 expr
->ctype
= size_t_ctype
;
2942 return size_t_ctype
;
2945 struct symbol
*evaluate_expression(struct expression
*expr
)
2952 switch (expr
->type
) {
2955 expression_error(expr
, "value expression without a type");
2958 return evaluate_string(expr
);
2960 return evaluate_symbol_expression(expr
);
2962 if (!evaluate_expression(expr
->left
))
2964 if (!evaluate_expression(expr
->right
))
2966 return evaluate_binop(expr
);
2968 return evaluate_logical(expr
);
2970 evaluate_expression(expr
->left
);
2971 if (!evaluate_expression(expr
->right
))
2973 return evaluate_comma(expr
);
2975 if (!evaluate_expression(expr
->left
))
2977 if (!evaluate_expression(expr
->right
))
2979 return evaluate_compare(expr
);
2980 case EXPR_ASSIGNMENT
:
2981 if (!evaluate_expression(expr
->left
))
2983 if (!evaluate_expression(expr
->right
))
2985 return evaluate_assignment(expr
);
2987 if (!evaluate_expression(expr
->unop
))
2989 return evaluate_preop(expr
);
2991 if (!evaluate_expression(expr
->unop
))
2993 return evaluate_postop(expr
);
2995 case EXPR_FORCE_CAST
:
2996 case EXPR_IMPLIED_CAST
:
2997 return evaluate_cast(expr
);
2999 return evaluate_sizeof(expr
);
3000 case EXPR_PTRSIZEOF
:
3001 return evaluate_ptrsizeof(expr
);
3003 return evaluate_alignof(expr
);
3005 return evaluate_member_dereference(expr
);
3007 return evaluate_call(expr
);
3009 case EXPR_CONDITIONAL
:
3010 return evaluate_conditional_expression(expr
);
3011 case EXPR_STATEMENT
:
3012 expr
->ctype
= evaluate_statement(expr
->statement
);
3016 expr
->ctype
= &ptr_ctype
;
3020 /* Evaluate the type of the symbol .. */
3021 evaluate_symbol(expr
->symbol
);
3022 /* .. but the type of the _expression_ is a "type" */
3023 expr
->ctype
= &type_ctype
;
3027 return evaluate_offsetof(expr
);
3029 /* These can not exist as stand-alone expressions */
3030 case EXPR_INITIALIZER
:
3031 case EXPR_IDENTIFIER
:
3034 expression_error(expr
, "internal front-end error: initializer in expression");
3037 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3043 static void check_duplicates(struct symbol
*sym
)
3046 struct symbol
*next
= sym
;
3048 while ((next
= next
->same_symbol
) != NULL
) {
3049 const char *typediff
;
3050 evaluate_symbol(next
);
3052 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3054 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3055 show_ident(sym
->ident
),
3056 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3061 unsigned long mod
= sym
->ctype
.modifiers
;
3062 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3064 if (!(mod
& MOD_TOPLEVEL
))
3068 if (sym
->ident
== &main_ident
)
3070 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3074 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3076 struct symbol
*base_type
;
3084 sym
= examine_symbol_type(sym
);
3085 base_type
= get_base_type(sym
);
3089 /* Evaluate the initializers */
3090 if (sym
->initializer
)
3091 evaluate_initializer(sym
, &sym
->initializer
);
3093 /* And finally, evaluate the body of the symbol too */
3094 if (base_type
->type
== SYM_FN
) {
3095 struct symbol
*curr
= current_fn
;
3097 if (sym
->definition
&& sym
->definition
!= sym
)
3098 return evaluate_symbol(sym
->definition
);
3100 current_fn
= base_type
;
3102 examine_fn_arguments(base_type
);
3103 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3105 if (base_type
->stmt
)
3106 evaluate_statement(base_type
->stmt
);
3114 void evaluate_symbol_list(struct symbol_list
*list
)
3118 FOR_EACH_PTR(list
, sym
) {
3119 evaluate_symbol(sym
);
3120 check_duplicates(sym
);
3121 } END_FOR_EACH_PTR(sym
);
3124 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3126 struct expression
*expr
= stmt
->expression
;
3127 struct symbol
*fntype
;
3129 evaluate_expression(expr
);
3130 fntype
= current_fn
->ctype
.base_type
;
3131 if (!fntype
|| fntype
== &void_ctype
) {
3132 if (expr
&& expr
->ctype
!= &void_ctype
)
3133 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3134 if (expr
&& Wreturn_void
)
3135 warning(stmt
->pos
, "returning void-valued expression");
3140 sparse_error(stmt
->pos
, "return with no return value");
3145 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3149 static void evaluate_if_statement(struct statement
*stmt
)
3151 if (!stmt
->if_conditional
)
3154 evaluate_conditional(stmt
->if_conditional
, 0);
3155 evaluate_statement(stmt
->if_true
);
3156 evaluate_statement(stmt
->if_false
);
3159 static void evaluate_iterator(struct statement
*stmt
)
3161 evaluate_symbol_list(stmt
->iterator_syms
);
3162 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3163 evaluate_conditional(stmt
->iterator_post_condition
,1);
3164 evaluate_statement(stmt
->iterator_pre_statement
);
3165 evaluate_statement(stmt
->iterator_statement
);
3166 evaluate_statement(stmt
->iterator_post_statement
);
3169 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3171 switch (*constraint
) {
3172 case '=': /* Assignment */
3173 case '+': /* Update */
3176 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3180 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3182 switch (*constraint
) {
3183 case '=': /* Assignment */
3184 case '+': /* Update */
3185 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3189 static void evaluate_asm_statement(struct statement
*stmt
)
3191 struct expression
*expr
;
3195 expr
= stmt
->asm_string
;
3196 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3197 sparse_error(stmt
->pos
, "need constant string for inline asm");
3202 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3204 case 0: /* Identifier */
3208 case 1: /* Constraint */
3210 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3211 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3212 *THIS_ADDRESS(expr
) = NULL
;
3215 verify_output_constraint(expr
, expr
->string
->data
);
3218 case 2: /* Expression */
3220 if (!evaluate_expression(expr
))
3222 if (!lvalue_expression(expr
))
3223 warning(expr
->pos
, "asm output is not an lvalue");
3224 evaluate_assign_to(expr
, expr
->ctype
);
3227 } END_FOR_EACH_PTR(expr
);
3230 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3232 case 0: /* Identifier */
3236 case 1: /* Constraint */
3238 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3239 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3240 *THIS_ADDRESS(expr
) = NULL
;
3243 verify_input_constraint(expr
, expr
->string
->data
);
3246 case 2: /* Expression */
3248 if (!evaluate_expression(expr
))
3252 } END_FOR_EACH_PTR(expr
);
3254 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3256 sparse_error(stmt
->pos
, "bad asm clobbers");
3259 if (expr
->type
== EXPR_STRING
)
3261 expression_error(expr
, "asm clobber is not a string");
3262 } END_FOR_EACH_PTR(expr
);
3264 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3265 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3266 sparse_error(stmt
->pos
, "bad asm label");
3269 } END_FOR_EACH_PTR(sym
);
3272 static void evaluate_case_statement(struct statement
*stmt
)
3274 evaluate_expression(stmt
->case_expression
);
3275 evaluate_expression(stmt
->case_to
);
3276 evaluate_statement(stmt
->case_statement
);
3279 static void check_case_type(struct expression
*switch_expr
,
3280 struct expression
*case_expr
,
3281 struct expression
**enumcase
)
3283 struct symbol
*switch_type
, *case_type
;
3289 switch_type
= switch_expr
->ctype
;
3290 case_type
= evaluate_expression(case_expr
);
3292 if (!switch_type
|| !case_type
)
3296 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3297 else if (is_enum_type(case_type
))
3298 *enumcase
= case_expr
;
3301 sclass
= classify_type(switch_type
, &switch_type
);
3302 cclass
= classify_type(case_type
, &case_type
);
3304 /* both should be arithmetic */
3305 if (!(sclass
& cclass
& TYPE_NUM
))
3308 /* neither should be floating */
3309 if ((sclass
| cclass
) & TYPE_FLOAT
)
3312 /* if neither is restricted, we are OK */
3313 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3316 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3317 cclass
, sclass
, case_type
, switch_type
)) {
3318 unrestrict(case_expr
, cclass
, &case_type
);
3319 unrestrict(switch_expr
, sclass
, &switch_type
);
3324 expression_error(case_expr
, "incompatible types for 'case' statement");
3327 static void evaluate_switch_statement(struct statement
*stmt
)
3330 struct expression
*enumcase
= NULL
;
3331 struct expression
**enumcase_holder
= &enumcase
;
3332 struct expression
*sel
= stmt
->switch_expression
;
3334 evaluate_expression(sel
);
3335 evaluate_statement(stmt
->switch_statement
);
3338 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3339 enumcase_holder
= NULL
; /* Only check cases against switch */
3341 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3342 struct statement
*case_stmt
= sym
->stmt
;
3343 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3344 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3345 } END_FOR_EACH_PTR(sym
);
3348 static void evaluate_goto_statement(struct statement
*stmt
)
3350 struct symbol
*label
= stmt
->goto_label
;
3352 if (label
&& !label
->stmt
)
3353 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3355 evaluate_expression(stmt
->goto_expression
);
3358 struct symbol
*evaluate_statement(struct statement
*stmt
)
3363 switch (stmt
->type
) {
3364 case STMT_DECLARATION
: {
3366 FOR_EACH_PTR(stmt
->declaration
, s
) {
3368 } END_FOR_EACH_PTR(s
);
3373 return evaluate_return_expression(stmt
);
3375 case STMT_EXPRESSION
:
3376 if (!evaluate_expression(stmt
->expression
))
3378 if (stmt
->expression
->ctype
== &null_ctype
)
3379 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3380 return degenerate(stmt
->expression
);
3382 case STMT_COMPOUND
: {
3383 struct statement
*s
;
3384 struct symbol
*type
= NULL
;
3386 /* Evaluate the return symbol in the compound statement */
3387 evaluate_symbol(stmt
->ret
);
3390 * Then, evaluate each statement, making the type of the
3391 * compound statement be the type of the last statement
3393 type
= evaluate_statement(stmt
->args
);
3394 FOR_EACH_PTR(stmt
->stmts
, s
) {
3395 type
= evaluate_statement(s
);
3396 } END_FOR_EACH_PTR(s
);
3402 evaluate_if_statement(stmt
);
3405 evaluate_iterator(stmt
);
3408 evaluate_switch_statement(stmt
);
3411 evaluate_case_statement(stmt
);
3414 return evaluate_statement(stmt
->label_statement
);
3416 evaluate_goto_statement(stmt
);
3421 evaluate_asm_statement(stmt
);
3424 evaluate_expression(stmt
->expression
);
3427 evaluate_expression(stmt
->range_expression
);
3428 evaluate_expression(stmt
->range_low
);
3429 evaluate_expression(stmt
->range_high
);