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 addr
->flags
= expr
->flags
;
74 expr
->type
= EXPR_PREOP
;
77 expr
->flags
= CEF_NONE
;
79 /* The type of a symbol is the symbol itself! */
84 static struct symbol
*evaluate_string(struct expression
*expr
)
86 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
87 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
88 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
89 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
90 unsigned int length
= expr
->string
->length
;
92 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
93 sym
->bit_size
= bytes_to_bits(length
);
94 sym
->ctype
.alignment
= 1;
96 sym
->ctype
.modifiers
= MOD_STATIC
;
97 sym
->ctype
.base_type
= array
;
98 sym
->initializer
= initstr
;
100 initstr
->ctype
= sym
;
101 initstr
->string
= expr
->string
;
103 array
->array_size
= sym
->array_size
;
104 array
->bit_size
= bytes_to_bits(length
);
105 array
->ctype
.alignment
= 1;
106 array
->ctype
.modifiers
= MOD_STATIC
;
107 array
->ctype
.base_type
= &char_ctype
;
110 addr
->ctype
= &lazy_ptr_ctype
;
111 addr
->flags
= CEF_ADDR
;
113 expr
->type
= EXPR_PREOP
;
120 /* type has come from classify_type and is an integer type */
121 static inline struct symbol
*integer_promotion(struct symbol
*type
)
123 unsigned long mod
= type
->ctype
.modifiers
;
124 int width
= type
->bit_size
;
127 * Bitfields always promote to the base type,
128 * even if the bitfield might be bigger than
131 if (type
->type
== SYM_BITFIELD
) {
132 type
= type
->ctype
.base_type
;
134 mod
= type
->ctype
.modifiers
;
135 if (width
< bits_in_int
)
138 /* If char/short has as many bits as int, it still gets "promoted" */
139 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
140 if (mod
& MOD_UNSIGNED
)
148 * integer part of usual arithmetic conversions:
149 * integer promotions are applied
150 * if left and right are identical, we are done
151 * if signedness is the same, convert one with lower rank
152 * unless unsigned argument has rank lower than signed one, convert the
154 * if signed argument is bigger than unsigned one, convert the unsigned.
155 * otherwise, convert signed.
157 * Leaving aside the integer promotions, that is equivalent to
158 * if identical, don't convert
159 * if left is bigger than right, convert right
160 * if right is bigger than left, convert right
161 * otherwise, if signedness is the same, convert one with lower rank
162 * otherwise convert the signed one.
164 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
166 unsigned long lmod
, rmod
;
168 left
= integer_promotion(left
);
169 right
= integer_promotion(right
);
174 if (left
->bit_size
> right
->bit_size
)
177 if (right
->bit_size
> left
->bit_size
)
180 lmod
= left
->ctype
.modifiers
;
181 rmod
= right
->ctype
.modifiers
;
182 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
183 if (lmod
& MOD_UNSIGNED
)
185 } else if ((lmod
& ~rmod
) & (MOD_LONG_ALL
))
193 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
195 return orig
->bit_size
== new->bit_size
&&
196 orig
->bit_offset
== new->bit_offset
;
199 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
201 unsigned long mod
, as
;
205 mod
|= node
->ctype
.modifiers
;
206 as
|= node
->ctype
.as
;
207 if (node
->type
== SYM_NODE
) {
208 node
= node
->ctype
.base_type
;
213 *modp
= mod
& ~MOD_IGNORE
;
218 static int is_same_type(struct expression
*expr
, struct symbol
*new)
220 struct symbol
*old
= expr
->ctype
;
221 unsigned long oldmod
, newmod
, oldas
, newas
;
223 old
= base_type(old
, &oldmod
, &oldas
);
224 new = base_type(new, &newmod
, &newas
);
226 /* Same base type, same address space? */
227 if (old
== new && oldas
== newas
) {
228 unsigned long difmod
;
230 /* Check the modifier bits. */
231 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
233 /* Exact same type? */
238 * Not the same type, but differs only in "const".
239 * Don't warn about MOD_NOCAST.
241 if (difmod
== MOD_CONST
)
244 if ((oldmod
| newmod
) & MOD_NOCAST
) {
245 const char *tofrom
= "to/from";
246 if (!(newmod
& MOD_NOCAST
))
248 if (!(oldmod
& MOD_NOCAST
))
250 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
256 warn_for_different_enum_types (struct position pos
,
257 struct symbol
*typea
,
258 struct symbol
*typeb
)
262 if (typea
->type
== SYM_NODE
)
263 typea
= typea
->ctype
.base_type
;
264 if (typeb
->type
== SYM_NODE
)
265 typeb
= typeb
->ctype
.base_type
;
270 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
271 warning(pos
, "mixing different enum types");
272 info(pos
, " %s versus", show_typename(typea
));
273 info(pos
, " %s", show_typename(typeb
));
277 static int cast_flags(struct expression
*expr
, struct expression
*target
);
278 static struct symbol
*cast_to_bool(struct expression
*expr
);
281 * This gets called for implicit casts in assignments and
282 * integer promotion. We often want to try to move the
283 * cast down, because the ops involved may have been
284 * implicitly cast up, and we can get rid of the casts
287 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
289 struct expression
*expr
;
291 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
293 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
297 * See if we can simplify the op. Move the cast down.
301 if (old
->ctype
->bit_size
< type
->bit_size
)
303 if (old
->op
== '~') {
305 old
->unop
= cast_to(old
->unop
, type
);
310 case EXPR_IMPLIED_CAST
:
311 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
313 if (old
->ctype
->bit_size
>= type
->bit_size
) {
314 struct expression
*orig
= old
->cast_expression
;
315 if (same_cast_type(orig
->ctype
, type
))
317 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
319 old
->cast_type
= type
;
329 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
331 expr
->cast_type
= type
;
332 expr
->cast_expression
= old
;
333 expr
->flags
= cast_flags(expr
, old
);
335 if (is_bool_type(type
))
352 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
354 static int type_class
[SYM_BAD
+ 1] = {
355 [SYM_PTR
] = TYPE_PTR
,
356 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
357 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
358 [SYM_STRUCT
] = TYPE_COMPOUND
,
359 [SYM_UNION
] = TYPE_COMPOUND
,
360 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
361 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
362 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
364 if (type
->type
== SYM_NODE
)
365 type
= type
->ctype
.base_type
;
366 if (type
->type
== SYM_TYPEOF
) {
367 type
= evaluate_expression(type
->initializer
);
370 else if (type
->type
== SYM_NODE
)
371 type
= type
->ctype
.base_type
;
373 if (type
->type
== SYM_ENUM
)
374 type
= type
->ctype
.base_type
;
376 if (type
->type
== SYM_BASETYPE
) {
377 if (type
->ctype
.base_type
== &int_type
)
379 if (type
->ctype
.base_type
== &fp_type
)
380 return TYPE_NUM
| TYPE_FLOAT
;
382 return type_class
[type
->type
];
385 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
387 static inline int is_string_type(struct symbol
*type
)
389 if (type
->type
== SYM_NODE
)
390 type
= type
->ctype
.base_type
;
391 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
394 static struct symbol
*bad_expr_type(struct expression
*expr
)
396 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
397 switch (expr
->type
) {
400 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
401 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
405 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
411 expr
->flags
= CEF_NONE
;
412 return expr
->ctype
= &bad_ctype
;
415 static int restricted_value(struct expression
*v
, struct symbol
*type
)
417 if (v
->type
!= EXPR_VALUE
)
424 static int restricted_binop(int op
, struct symbol
*type
)
429 case SPECIAL_AND_ASSIGN
:
430 case SPECIAL_OR_ASSIGN
:
431 case SPECIAL_XOR_ASSIGN
:
432 return 1; /* unfoul */
436 return 2; /* keep fouled */
438 case SPECIAL_NOTEQUAL
:
439 return 3; /* warn if fouled */
445 static int restricted_unop(int op
, struct symbol
**type
)
448 if ((*type
)->bit_size
< bits_in_int
)
449 *type
= befoul(*type
);
456 /* type should be SYM_FOULED */
457 static inline struct symbol
*unfoul(struct symbol
*type
)
459 return type
->ctype
.base_type
;
462 static struct symbol
*restricted_binop_type(int op
,
463 struct expression
*left
,
464 struct expression
*right
,
465 int lclass
, int rclass
,
466 struct symbol
*ltype
,
467 struct symbol
*rtype
)
469 struct symbol
*ctype
= NULL
;
470 if (lclass
& TYPE_RESTRICT
) {
471 if (rclass
& TYPE_RESTRICT
) {
472 if (ltype
== rtype
) {
474 } else if (lclass
& TYPE_FOULED
) {
475 if (unfoul(ltype
) == rtype
)
477 } else if (rclass
& TYPE_FOULED
) {
478 if (unfoul(rtype
) == ltype
)
482 if (!restricted_value(right
, ltype
))
485 } else if (!restricted_value(left
, rtype
))
489 switch (restricted_binop(op
, ctype
)) {
491 if ((lclass
^ rclass
) & TYPE_FOULED
)
492 ctype
= unfoul(ctype
);
495 if (!(lclass
& rclass
& TYPE_FOULED
))
507 static inline void unrestrict(struct expression
*expr
,
508 int class, struct symbol
**ctype
)
510 if (class & TYPE_RESTRICT
) {
511 if (class & TYPE_FOULED
)
512 *ctype
= unfoul(*ctype
);
513 warning(expr
->pos
, "%s degrades to integer",
514 show_typename(*ctype
));
515 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
519 static struct symbol
*usual_conversions(int op
,
520 struct expression
*left
,
521 struct expression
*right
,
522 int lclass
, int rclass
,
523 struct symbol
*ltype
,
524 struct symbol
*rtype
)
526 struct symbol
*ctype
;
528 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
530 if ((lclass
| rclass
) & TYPE_RESTRICT
)
534 if (!(lclass
& TYPE_FLOAT
)) {
535 if (!(rclass
& TYPE_FLOAT
))
536 return bigger_int_type(ltype
, rtype
);
539 } else if (rclass
& TYPE_FLOAT
) {
540 unsigned long lmod
= ltype
->ctype
.modifiers
;
541 unsigned long rmod
= rtype
->ctype
.modifiers
;
542 if (rmod
& ~lmod
& (MOD_LONG_ALL
))
550 ctype
= restricted_binop_type(op
, left
, right
,
551 lclass
, rclass
, ltype
, rtype
);
555 unrestrict(left
, lclass
, <ype
);
556 unrestrict(right
, rclass
, &rtype
);
561 static inline int lvalue_expression(struct expression
*expr
)
563 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
566 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
568 struct expression
*index
= expr
->right
;
569 struct symbol
*ctype
, *base
;
572 classify_type(degenerate(expr
->left
), &ctype
);
573 base
= examine_pointer_target(ctype
);
576 * An address constant +/- an integer constant expression
577 * yields an address constant again [6.6(7)].
579 if ((expr
->left
->flags
& CEF_ADDR
) && (expr
->right
->flags
& CEF_ICE
))
580 expr
->flags
= CEF_ADDR
;
583 expression_error(expr
, "missing type information");
586 if (is_function(base
)) {
587 expression_error(expr
, "arithmetics on pointers to functions");
591 /* Get the size of whatever the pointer points to */
592 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
594 if (ctype
== &null_ctype
)
598 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
601 if (index
->type
== EXPR_VALUE
) {
602 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
603 unsigned long long v
= index
->value
, mask
;
604 mask
= 1ULL << (itype
->bit_size
- 1);
610 mask
= 1ULL << (bits_in_pointer
- 1);
611 v
&= mask
| (mask
- 1);
613 val
->ctype
= ssize_t_ctype
;
618 if (itype
->bit_size
< bits_in_pointer
)
619 index
= cast_to(index
, ssize_t_ctype
);
622 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
623 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
625 val
->ctype
= ssize_t_ctype
;
626 val
->value
= multiply
;
629 mul
->ctype
= ssize_t_ctype
;
639 static void examine_fn_arguments(struct symbol
*fn
);
641 #define MOD_IGN (MOD_VOLATILE | MOD_CONST | MOD_PURE)
643 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
644 unsigned long mod1
, unsigned long mod2
)
646 unsigned long as1
= c1
->as
, as2
= c2
->as
;
647 struct symbol
*t1
= c1
->base_type
;
648 struct symbol
*t2
= c2
->base_type
;
649 int move1
= 1, move2
= 1;
650 mod1
|= c1
->modifiers
;
651 mod2
|= c2
->modifiers
;
655 struct symbol
*base1
= t1
->ctype
.base_type
;
656 struct symbol
*base2
= t2
->ctype
.base_type
;
659 * FIXME! Collect alignment and context too here!
662 if (t1
&& t1
->type
!= SYM_PTR
) {
663 mod1
|= t1
->ctype
.modifiers
;
670 if (t2
&& t2
->type
!= SYM_PTR
) {
671 mod2
|= t2
->ctype
.modifiers
;
680 return "different types";
682 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
690 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
700 if (type
!= t2
->type
)
701 return "different base types";
705 sparse_error(t1
->pos
,
706 "internal error: bad type in derived(%d)",
710 return "different base types";
713 /* allow definition of incomplete structs and unions */
714 if (t1
->ident
== t2
->ident
)
716 return "different base types";
718 /* XXX: we ought to compare sizes */
722 return "different address spaces";
723 /* MOD_SPECIFIER is due to idiocy in parse.c */
724 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
725 return "different modifiers";
726 /* we could be lazier here */
727 base1
= examine_pointer_target(t1
);
728 base2
= examine_pointer_target(t2
);
729 mod1
= t1
->ctype
.modifiers
;
731 mod2
= t2
->ctype
.modifiers
;
735 struct symbol
*arg1
, *arg2
;
739 return "different address spaces";
740 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
741 return "different modifiers";
742 mod1
= t1
->ctype
.modifiers
;
744 mod2
= t2
->ctype
.modifiers
;
747 if (t1
->variadic
!= t2
->variadic
)
748 return "incompatible variadic arguments";
749 examine_fn_arguments(t1
);
750 examine_fn_arguments(t2
);
751 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
752 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
759 return "different argument counts";
760 diffstr
= type_difference(&arg1
->ctype
,
764 static char argdiff
[80];
765 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
772 FINISH_PTR_LIST(arg2
);
773 FINISH_PTR_LIST(arg1
);
778 return "different address spaces";
780 return "different base types";
781 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
785 return "different type sizes";
786 else if (diff
& ~MOD_SIGNEDNESS
)
787 return "different modifiers";
789 return "different signedness";
795 return "different address spaces";
796 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
797 return "different modifiers";
801 static void bad_null(struct expression
*expr
)
803 if (Wnon_pointer_null
)
804 warning(expr
->pos
, "Using plain integer as NULL pointer");
807 static unsigned long target_qualifiers(struct symbol
*type
)
809 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
810 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
815 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
817 const char *typediff
;
818 struct symbol
*ltype
, *rtype
;
819 struct expression
*l
= expr
->left
;
820 struct expression
*r
= expr
->right
;
821 struct symbol
*lbase
;
823 classify_type(degenerate(l
), <ype
);
824 classify_type(degenerate(r
), &rtype
);
826 lbase
= examine_pointer_target(ltype
);
827 examine_pointer_target(rtype
);
828 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
829 target_qualifiers(rtype
),
830 target_qualifiers(ltype
));
832 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
834 if (is_function(lbase
)) {
835 expression_error(expr
, "subtraction of functions? Share your drugs");
839 expr
->ctype
= ssize_t_ctype
;
840 if (lbase
->bit_size
> bits_in_char
) {
841 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
842 struct expression
*div
= expr
;
843 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
844 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
846 val
->ctype
= size_t_ctype
;
849 if (value
& (value
-1)) {
850 if (Wptr_subtraction_blows
)
851 warning(expr
->pos
, "potentially expensive pointer subtraction");
855 sub
->ctype
= ssize_t_ctype
;
864 return ssize_t_ctype
;
867 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
869 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
871 struct symbol
*ctype
;
876 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
877 warning(expr
->pos
, "assignment expression in conditional");
879 ctype
= evaluate_expression(expr
);
881 if (is_safe_type(ctype
))
882 warning(expr
->pos
, "testing a 'safe expression'");
883 if (is_func_type(ctype
)) {
885 warning(expr
->pos
, "the address of %s will always evaluate as true", "a function");
886 } else if (is_array_type(ctype
)) {
888 warning(expr
->pos
, "the address of %s will always evaluate as true", "an array");
889 } else if (!is_scalar_type(ctype
)) {
890 sparse_error(expr
->pos
, "incorrect type in conditional");
891 info(expr
->pos
, " got %s", show_typename(ctype
));
895 ctype
= degenerate(expr
);
900 static struct symbol
*evaluate_logical(struct expression
*expr
)
902 if (!evaluate_conditional(expr
->left
, 0))
904 if (!evaluate_conditional(expr
->right
, 0))
907 /* the result is int [6.5.13(3), 6.5.14(3)] */
908 expr
->ctype
= &int_ctype
;
909 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
910 expr
->flags
&= ~(CEF_CONST_MASK
| CEF_ADDR
);
914 static struct symbol
*evaluate_binop(struct expression
*expr
)
916 struct symbol
*ltype
, *rtype
, *ctype
;
917 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
918 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
921 /* number op number */
922 if (lclass
& rclass
& TYPE_NUM
) {
923 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
924 expr
->flags
&= ~CEF_CONST_MASK
;
926 if ((lclass
| rclass
) & TYPE_FLOAT
) {
928 case '+': case '-': case '*': case '/':
931 return bad_expr_type(expr
);
935 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
936 // shifts do integer promotions, but that's it.
937 unrestrict(expr
->left
, lclass
, <ype
);
938 unrestrict(expr
->right
, rclass
, &rtype
);
939 ctype
= ltype
= integer_promotion(ltype
);
940 rtype
= integer_promotion(rtype
);
942 // The rest do usual conversions
943 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
944 && expr
->left
->op
== '!';
945 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
946 && expr
->right
->op
== '!';
947 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
948 warning(expr
->pos
, "dubious: %sx %c %sy",
951 right_not
? "!" : "");
953 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
954 lclass
, rclass
, ltype
, rtype
);
955 ctype
= rtype
= ltype
;
958 expr
->left
= cast_to(expr
->left
, ltype
);
959 expr
->right
= cast_to(expr
->right
, rtype
);
964 /* pointer (+|-) integer */
965 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
966 unrestrict(expr
->right
, rclass
, &rtype
);
967 return evaluate_ptr_add(expr
, rtype
);
970 /* integer + pointer */
971 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
972 struct expression
*index
= expr
->left
;
973 unrestrict(index
, lclass
, <ype
);
974 expr
->left
= expr
->right
;
976 return evaluate_ptr_add(expr
, ltype
);
979 /* pointer - pointer */
980 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
981 return evaluate_ptr_sub(expr
);
983 return bad_expr_type(expr
);
986 static struct symbol
*evaluate_comma(struct expression
*expr
)
988 expr
->ctype
= degenerate(expr
->right
);
989 if (expr
->ctype
== &null_ctype
)
990 expr
->ctype
= &ptr_ctype
;
991 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
995 static int modify_for_unsigned(int op
)
998 op
= SPECIAL_UNSIGNED_LT
;
1000 op
= SPECIAL_UNSIGNED_GT
;
1001 else if (op
== SPECIAL_LTE
)
1002 op
= SPECIAL_UNSIGNED_LTE
;
1003 else if (op
== SPECIAL_GTE
)
1004 op
= SPECIAL_UNSIGNED_GTE
;
1008 static inline int is_null_pointer_constant(struct expression
*e
)
1010 if (e
->ctype
== &null_ctype
)
1012 if (!(e
->flags
& CEF_ICE
))
1014 return is_zero_constant(e
) ? 2 : 0;
1017 static struct symbol
*evaluate_compare(struct expression
*expr
)
1019 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1020 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
1021 int lclass
= classify_type(degenerate(left
), <ype
);
1022 int rclass
= classify_type(degenerate(right
), &rtype
);
1023 struct symbol
*ctype
;
1024 const char *typediff
;
1027 if (is_type_type(ltype
) && is_type_type(rtype
)) {
1029 * __builtin_types_compatible_p() yields an integer
1030 * constant expression
1032 expr
->flags
= CEF_SET_ICE
;
1036 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1037 warning(expr
->pos
, "testing a 'safe expression'");
1039 expr
->flags
= left
->flags
& right
->flags
& ~CEF_CONST_MASK
& ~CEF_ADDR
;
1041 /* number on number */
1042 if (lclass
& rclass
& TYPE_NUM
) {
1043 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1044 lclass
, rclass
, ltype
, rtype
);
1045 expr
->left
= cast_to(expr
->left
, ctype
);
1046 expr
->right
= cast_to(expr
->right
, ctype
);
1047 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1048 expr
->op
= modify_for_unsigned(expr
->op
);
1052 /* at least one must be a pointer */
1053 if (!((lclass
| rclass
) & TYPE_PTR
))
1054 return bad_expr_type(expr
);
1056 /* equality comparisons can be with null pointer constants */
1057 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1058 int is_null1
= is_null_pointer_constant(left
);
1059 int is_null2
= is_null_pointer_constant(right
);
1064 if (is_null1
&& is_null2
) {
1065 int positive
= expr
->op
== SPECIAL_EQUAL
;
1066 expr
->type
= EXPR_VALUE
;
1067 expr
->value
= positive
;
1070 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1071 left
= cast_to(left
, rtype
);
1074 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1075 right
= cast_to(right
, ltype
);
1079 /* both should be pointers */
1080 if (!(lclass
& rclass
& TYPE_PTR
))
1081 return bad_expr_type(expr
);
1082 expr
->op
= modify_for_unsigned(expr
->op
);
1084 lbase
= examine_pointer_target(ltype
);
1085 rbase
= examine_pointer_target(rtype
);
1087 /* they also have special treatment for pointers to void */
1088 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1089 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1090 if (lbase
== &void_ctype
) {
1091 right
= cast_to(right
, ltype
);
1094 if (rbase
== &void_ctype
) {
1095 left
= cast_to(left
, rtype
);
1101 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1102 target_qualifiers(rtype
),
1103 target_qualifiers(ltype
));
1107 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1111 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1112 expr
->ctype
= &int_ctype
;
1117 * NOTE! The degenerate case of "x ? : y", where we don't
1118 * have a true case, this will possibly promote "x" to the
1119 * same type as "y", and thus _change_ the conditional
1120 * test in the expression. But since promotion is "safe"
1121 * for testing, that's OK.
1123 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1125 struct expression
**true;
1126 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1128 const char * typediff
;
1131 if (!evaluate_conditional(expr
->conditional
, 0))
1133 if (!evaluate_expression(expr
->cond_false
))
1136 ctype
= degenerate(expr
->conditional
);
1137 rtype
= degenerate(expr
->cond_false
);
1139 true = &expr
->conditional
;
1141 if (expr
->cond_true
) {
1142 if (!evaluate_expression(expr
->cond_true
))
1144 ltype
= degenerate(expr
->cond_true
);
1145 true = &expr
->cond_true
;
1148 expr
->flags
= (expr
->conditional
->flags
& (*true)->flags
&
1149 expr
->cond_false
->flags
& ~CEF_CONST_MASK
);
1151 * A conditional operator yields a particular constant
1152 * expression type only if all of its three subexpressions are
1153 * of that type [6.6(6), 6.6(8)].
1154 * As an extension, relax this restriction by allowing any
1155 * constant expression type for the condition expression.
1157 * A conditional operator never yields an address constant
1159 * However, as an extension, if the condition is any constant
1160 * expression, and the true and false expressions are both
1161 * address constants, mark the result as an address constant.
1163 if (expr
->conditional
->flags
& (CEF_ACE
| CEF_ADDR
))
1164 expr
->flags
= (*true)->flags
& expr
->cond_false
->flags
& ~CEF_CONST_MASK
;
1166 lclass
= classify_type(ltype
, <ype
);
1167 rclass
= classify_type(rtype
, &rtype
);
1168 if (lclass
& rclass
& TYPE_NUM
) {
1169 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1170 lclass
, rclass
, ltype
, rtype
);
1171 *true = cast_to(*true, ctype
);
1172 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1176 if ((lclass
| rclass
) & TYPE_PTR
) {
1177 int is_null1
= is_null_pointer_constant(*true);
1178 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1180 if (is_null1
&& is_null2
) {
1181 *true = cast_to(*true, &ptr_ctype
);
1182 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1186 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1189 *true = cast_to(*true, rtype
);
1193 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1195 bad_null(expr
->cond_false
);
1196 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1200 if (!(lclass
& rclass
& TYPE_PTR
)) {
1201 typediff
= "different types";
1204 /* OK, it's pointer on pointer */
1205 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1206 typediff
= "different address spaces";
1210 /* need to be lazier here */
1211 lbase
= examine_pointer_target(ltype
);
1212 rbase
= examine_pointer_target(rtype
);
1213 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1215 if (lbase
== &void_ctype
) {
1216 /* XXX: pointers to function should warn here */
1221 if (rbase
== &void_ctype
) {
1222 /* XXX: pointers to function should warn here */
1226 /* XXX: that should be pointer to composite */
1228 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1235 /* void on void, struct on same struct, union on same union */
1236 if (ltype
== rtype
) {
1240 typediff
= "different base types";
1243 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1245 * if the condition is constant, the type is in fact known
1246 * so use it, as gcc & clang do.
1248 switch (expr_truth_value(expr
->conditional
)) {
1249 case 1: expr
->ctype
= ltype
;
1251 case 0: expr
->ctype
= rtype
;
1259 expr
->ctype
= ctype
;
1263 if (qual
& ~ctype
->ctype
.modifiers
) {
1264 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1266 sym
->ctype
.modifiers
|= qual
;
1269 *true = cast_to(*true, ctype
);
1270 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1274 /* FP assignments can not do modulo or bit operations */
1275 static int compatible_float_op(int op
)
1277 return op
== SPECIAL_ADD_ASSIGN
||
1278 op
== SPECIAL_SUB_ASSIGN
||
1279 op
== SPECIAL_MUL_ASSIGN
||
1280 op
== SPECIAL_DIV_ASSIGN
;
1283 static int evaluate_assign_op(struct expression
*expr
)
1285 struct symbol
*target
= expr
->left
->ctype
;
1286 struct symbol
*source
= expr
->right
->ctype
;
1287 struct symbol
*t
, *s
;
1288 int tclass
= classify_type(target
, &t
);
1289 int sclass
= classify_type(source
, &s
);
1292 if (tclass
& sclass
& TYPE_NUM
) {
1293 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1294 expression_error(expr
, "invalid assignment");
1297 if (tclass
& TYPE_RESTRICT
) {
1298 if (!restricted_binop(op
, t
)) {
1299 warning(expr
->pos
, "bad assignment (%s) to %s",
1300 show_special(op
), show_typename(t
));
1301 expr
->right
= cast_to(expr
->right
, target
);
1304 /* allowed assignments unfoul */
1305 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1307 if (!restricted_value(expr
->right
, t
))
1309 } else if (!(sclass
& TYPE_RESTRICT
))
1311 /* source and target would better be identical restricted */
1314 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1315 info(expr
->pos
, " left side has type %s", show_typename(t
));
1316 info(expr
->pos
, " right side has type %s", show_typename(s
));
1317 expr
->right
= cast_to(expr
->right
, target
);
1320 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1321 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1322 unrestrict(expr
->right
, sclass
, &s
);
1323 evaluate_ptr_add(expr
, s
);
1326 expression_error(expr
, "invalid pointer assignment");
1330 expression_error(expr
, "invalid assignment");
1334 target
= usual_conversions(op
, expr
->left
, expr
->right
,
1335 tclass
, sclass
, target
, source
);
1337 expr
->right
= cast_to(expr
->right
, target
);
1341 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1344 return 0; /* yes, 0 - we don't want a cast_to here */
1345 if (t1
== &void_ctype
)
1347 if (t2
== &void_ctype
)
1349 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1351 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1355 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1357 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1362 static int check_assignment_types(struct symbol
*target
, struct expression
**rp
,
1363 const char **typediff
)
1365 struct symbol
*source
= degenerate(*rp
);
1366 struct symbol
*t
, *s
;
1367 int tclass
= classify_type(target
, &t
);
1368 int sclass
= classify_type(source
, &s
);
1370 if (tclass
& sclass
& TYPE_NUM
) {
1371 if (tclass
& TYPE_RESTRICT
) {
1372 /* allowed assignments unfoul */
1373 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1375 if (!restricted_value(*rp
, target
))
1379 } else if (!(sclass
& TYPE_RESTRICT
))
1381 if (t
== &bool_ctype
) {
1382 if (is_fouled_type(s
))
1383 warning((*rp
)->pos
, "%s degrades to integer",
1384 show_typename(s
->ctype
.base_type
));
1387 *typediff
= "different base types";
1391 if (tclass
== TYPE_PTR
) {
1392 unsigned long mod1
, mod2
;
1393 struct symbol
*b1
, *b2
;
1394 // NULL pointer is always OK
1395 int is_null
= is_null_pointer_constant(*rp
);
1401 if (!(sclass
& TYPE_PTR
)) {
1402 *typediff
= "different base types";
1405 b1
= examine_pointer_target(t
);
1406 b2
= examine_pointer_target(s
);
1407 mod1
= target_qualifiers(t
);
1408 mod2
= target_qualifiers(s
);
1409 if (whitelist_pointers(b1
, b2
)) {
1411 * assignments to/from void * are OK, provided that
1412 * we do not remove qualifiers from pointed to [C]
1413 * or mix address spaces [sparse].
1415 if (t
->ctype
.as
!= s
->ctype
.as
) {
1416 *typediff
= "different address spaces";
1420 * If this is a function pointer assignment, it is
1421 * actually fine to assign a pointer to const data to
1422 * it, as a function pointer points to const data
1423 * implicitly, i.e., dereferencing it does not produce
1426 if (b1
->type
== SYM_FN
)
1429 *typediff
= "different modifiers";
1434 /* It's OK if the target is more volatile or const than the source */
1435 *typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1441 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1444 if (tclass
& TYPE_NUM
) {
1445 /* XXX: need to turn into comparison with NULL */
1446 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1448 *typediff
= "different base types";
1451 *typediff
= "invalid types";
1455 *rp
= cast_to(*rp
, target
);
1459 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1460 struct expression
**rp
, const char *where
)
1462 const char *typediff
;
1463 struct symbol
*source
= degenerate(*rp
);
1465 if (!check_assignment_types(target
, rp
, &typediff
)) {
1466 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1467 info(expr
->pos
, " expected %s", show_typename(target
));
1468 info(expr
->pos
, " got %s", show_typename(source
));
1469 *rp
= cast_to(*rp
, target
);
1476 static int compatible_transparent_union(struct symbol
*target
,
1477 struct expression
**rp
)
1479 struct symbol
*t
, *member
;
1480 classify_type(target
, &t
);
1481 if (t
->type
!= SYM_UNION
|| !t
->transparent_union
)
1484 FOR_EACH_PTR(t
->symbol_list
, member
) {
1485 const char *typediff
;
1486 if (check_assignment_types(member
, rp
, &typediff
))
1488 } END_FOR_EACH_PTR(member
);
1493 static int compatible_argument_type(struct expression
*expr
, struct symbol
*target
,
1494 struct expression
**rp
, const char *where
)
1496 if (compatible_transparent_union(target
, rp
))
1499 return compatible_assignment_types(expr
, target
, rp
, where
);
1502 static void mark_assigned(struct expression
*expr
)
1508 switch (expr
->type
) {
1513 if (sym
->type
!= SYM_NODE
)
1515 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1519 mark_assigned(expr
->left
);
1520 mark_assigned(expr
->right
);
1523 case EXPR_FORCE_CAST
:
1524 mark_assigned(expr
->cast_expression
);
1527 mark_assigned(expr
->base
);
1535 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1537 if (type
->ctype
.modifiers
& MOD_CONST
)
1538 expression_error(left
, "assignment to const expression");
1540 /* We know left is an lvalue, so it's a "preop-*" */
1541 mark_assigned(left
->unop
);
1544 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1546 struct expression
*left
= expr
->left
;
1547 struct expression
*where
= expr
;
1548 struct symbol
*ltype
;
1550 if (!lvalue_expression(left
)) {
1551 expression_error(expr
, "not an lvalue");
1555 ltype
= left
->ctype
;
1557 if (expr
->op
!= '=') {
1558 if (!evaluate_assign_op(expr
))
1561 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1565 evaluate_assign_to(left
, ltype
);
1567 expr
->ctype
= ltype
;
1571 static void examine_fn_arguments(struct symbol
*fn
)
1575 FOR_EACH_PTR(fn
->arguments
, s
) {
1576 struct symbol
*arg
= evaluate_symbol(s
);
1577 /* Array/function arguments silently degenerate into pointers */
1583 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1584 if (arg
->type
== SYM_ARRAY
)
1585 ptr
->ctype
= arg
->ctype
;
1587 ptr
->ctype
.base_type
= arg
;
1588 ptr
->ctype
.as
|= s
->ctype
.as
;
1589 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1591 s
->ctype
.base_type
= ptr
;
1593 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1596 examine_symbol_type(s
);
1603 } END_FOR_EACH_PTR(s
);
1606 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1608 /* Take the modifiers of the pointer, and apply them to the member */
1609 mod
|= sym
->ctype
.modifiers
;
1610 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1611 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1613 newsym
->ctype
.as
= as
;
1614 newsym
->ctype
.modifiers
= mod
;
1620 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1622 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1623 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1625 node
->ctype
.base_type
= ptr
;
1626 ptr
->bit_size
= bits_in_pointer
;
1627 ptr
->ctype
.alignment
= pointer_alignment
;
1629 node
->bit_size
= bits_in_pointer
;
1630 node
->ctype
.alignment
= pointer_alignment
;
1633 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1634 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1635 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1637 if (sym
->type
== SYM_NODE
) {
1638 ptr
->ctype
.as
|= sym
->ctype
.as
;
1639 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1640 sym
= sym
->ctype
.base_type
;
1642 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1643 ptr
->ctype
.as
|= sym
->ctype
.as
;
1644 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1645 sym
= sym
->ctype
.base_type
;
1647 ptr
->ctype
.base_type
= sym
;
1652 /* Arrays degenerate into pointers on pointer arithmetic */
1653 static struct symbol
*degenerate(struct expression
*expr
)
1655 struct symbol
*ctype
, *base
;
1659 ctype
= expr
->ctype
;
1662 base
= examine_symbol_type(ctype
);
1663 if (ctype
->type
== SYM_NODE
)
1664 base
= ctype
->ctype
.base_type
;
1666 * Arrays degenerate into pointers to the entries, while
1667 * functions degenerate into pointers to themselves.
1668 * If array was part of non-lvalue compound, we create a copy
1669 * of that compound first and then act as if we were dealing with
1670 * the corresponding field in there.
1672 switch (base
->type
) {
1674 if (expr
->type
== EXPR_SLICE
) {
1675 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1676 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1678 a
->ctype
.base_type
= expr
->base
->ctype
;
1679 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1680 a
->array_size
= expr
->base
->ctype
->array_size
;
1682 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1684 e0
->ctype
= &lazy_ptr_ctype
;
1686 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1689 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1691 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1693 e2
->right
= expr
->base
;
1695 e2
->ctype
= expr
->base
->ctype
;
1697 if (expr
->r_bitpos
) {
1698 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1701 e3
->right
= alloc_const_expression(expr
->pos
,
1702 bits_to_bytes(expr
->r_bitpos
));
1703 e3
->ctype
= &lazy_ptr_ctype
;
1708 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1711 e4
->ctype
= &lazy_ptr_ctype
;
1714 expr
->type
= EXPR_PREOP
;
1718 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1719 expression_error(expr
, "strange non-value function or array");
1722 *expr
= *expr
->unop
;
1723 ctype
= create_pointer(expr
, ctype
, 1);
1724 expr
->ctype
= ctype
;
1731 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1733 struct expression
*op
= expr
->unop
;
1734 struct symbol
*ctype
;
1736 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1737 expression_error(expr
, "not addressable");
1743 if (expr
->type
== EXPR_SYMBOL
) {
1744 struct symbol
*sym
= expr
->symbol
;
1745 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1749 * symbol expression evaluation is lazy about the type
1750 * of the sub-expression, so we may have to generate
1751 * the type here if so..
1753 if (expr
->ctype
== &lazy_ptr_ctype
) {
1754 ctype
= create_pointer(expr
, ctype
, 0);
1755 expr
->ctype
= ctype
;
1761 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1763 struct expression
*op
= expr
->unop
;
1764 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1766 /* Simplify: *&(expr) => (expr) */
1767 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1769 expr
->flags
= CEF_NONE
;
1773 examine_symbol_type(ctype
);
1775 /* Dereferencing a node drops all the node information. */
1776 if (ctype
->type
== SYM_NODE
)
1777 ctype
= ctype
->ctype
.base_type
;
1779 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1780 target
= ctype
->ctype
.base_type
;
1782 switch (ctype
->type
) {
1784 expression_error(expr
, "cannot dereference this type");
1787 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1788 merge_type(node
, ctype
);
1792 if (!lvalue_expression(op
)) {
1793 expression_error(op
, "non-lvalue array??");
1797 /* Do the implied "addressof" on the array */
1801 * When an array is dereferenced, we need to pick
1802 * up the attributes of the original node too..
1804 merge_type(node
, op
->ctype
);
1805 merge_type(node
, ctype
);
1809 node
->bit_size
= target
->bit_size
;
1810 node
->array_size
= target
->array_size
;
1817 * Unary post-ops: x++ and x--
1819 static struct symbol
*evaluate_postop(struct expression
*expr
)
1821 struct expression
*op
= expr
->unop
;
1822 struct symbol
*ctype
= op
->ctype
;
1823 int class = classify_type(ctype
, &ctype
);
1826 if (!class || class & TYPE_COMPOUND
) {
1827 expression_error(expr
, "need scalar for ++/--");
1830 if (!lvalue_expression(expr
->unop
)) {
1831 expression_error(expr
, "need lvalue expression for ++/--");
1835 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1836 unrestrict(expr
, class, &ctype
);
1838 if (class & TYPE_NUM
) {
1840 } else if (class == TYPE_PTR
) {
1841 struct symbol
*target
= examine_pointer_target(ctype
);
1842 if (!is_function(target
))
1843 multiply
= bits_to_bytes(target
->bit_size
);
1847 evaluate_assign_to(op
, op
->ctype
);
1848 expr
->op_value
= multiply
;
1849 expr
->ctype
= ctype
;
1853 expression_error(expr
, "bad argument type for ++/--");
1857 static struct symbol
*evaluate_sign(struct expression
*expr
)
1859 struct symbol
*ctype
= expr
->unop
->ctype
;
1860 int class = classify_type(ctype
, &ctype
);
1861 unsigned char flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1863 /* should be an arithmetic type */
1864 if (!(class & TYPE_NUM
))
1865 return bad_expr_type(expr
);
1866 if (class & TYPE_RESTRICT
)
1869 if (!(class & TYPE_FLOAT
)) {
1870 ctype
= integer_promotion(ctype
);
1871 expr
->unop
= cast_to(expr
->unop
, ctype
);
1872 } else if (expr
->op
!= '~') {
1873 /* no conversions needed */
1875 return bad_expr_type(expr
);
1877 if (expr
->op
== '+')
1878 *expr
= *expr
->unop
;
1879 expr
->flags
= flags
;
1880 expr
->ctype
= ctype
;
1883 if (restricted_unop(expr
->op
, &ctype
))
1884 unrestrict(expr
, class, &ctype
);
1888 static struct symbol
*evaluate_preop(struct expression
*expr
)
1890 struct symbol
*ctype
= expr
->unop
->ctype
;
1894 *expr
= *expr
->unop
;
1900 return evaluate_sign(expr
);
1903 return evaluate_dereference(expr
);
1906 return evaluate_addressof(expr
);
1908 case SPECIAL_INCREMENT
:
1909 case SPECIAL_DECREMENT
:
1911 * From a type evaluation standpoint the preops are
1912 * the same as the postops
1914 return evaluate_postop(expr
);
1917 expr
->flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1919 * A logical negation never yields an address constant
1922 expr
->flags
&= ~CEF_ADDR
;
1924 if (is_safe_type(ctype
))
1925 warning(expr
->pos
, "testing a 'safe expression'");
1926 if (is_float_type(ctype
)) {
1927 struct expression
*arg
= expr
->unop
;
1928 expr
->type
= EXPR_COMPARE
;
1929 expr
->op
= SPECIAL_EQUAL
;
1931 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1932 expr
->right
->ctype
= ctype
;
1933 expr
->right
->fvalue
= 0;
1934 } else if (is_fouled_type(ctype
)) {
1935 warning(expr
->pos
, "%s degrades to integer",
1936 show_typename(ctype
->ctype
.base_type
));
1938 /* the result is int [6.5.3.3(5)]*/
1945 expr
->ctype
= ctype
;
1949 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1951 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1952 struct ptr_list
*list
= head
;
1958 for (i
= 0; i
< list
->nr
; i
++) {
1959 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1961 if (sym
->ident
!= ident
)
1963 *offset
= sym
->offset
;
1966 struct symbol
*ctype
= sym
->ctype
.base_type
;
1970 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1972 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1975 *offset
+= sym
->offset
;
1979 } while ((list
= list
->next
) != head
);
1983 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1985 struct expression
*add
;
1988 * Create a new add-expression
1990 * NOTE! Even if we just add zero, we need a new node
1991 * for the member pointer, since it has a different
1992 * type than the original pointer. We could make that
1993 * be just a cast, but the fact is, a node is a node,
1994 * so we might as well just do the "add zero" here.
1996 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1999 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2000 add
->right
->ctype
= &int_ctype
;
2001 add
->right
->value
= offset
;
2004 * The ctype of the pointer will be lazily evaluated if
2005 * we ever take the address of this member dereference..
2007 add
->ctype
= &lazy_ptr_ctype
;
2009 * The resulting address of a member access through an address
2010 * constant is an address constant again [6.6(9)].
2012 add
->flags
= expr
->flags
;
2017 /* structure/union dereference */
2018 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
2021 struct symbol
*ctype
, *member
;
2022 struct expression
*deref
= expr
->deref
, *add
;
2023 struct ident
*ident
= expr
->member
;
2027 if (!evaluate_expression(deref
))
2030 expression_error(expr
, "bad member name");
2034 ctype
= deref
->ctype
;
2035 examine_symbol_type(ctype
);
2036 address_space
= ctype
->ctype
.as
;
2037 mod
= ctype
->ctype
.modifiers
;
2038 if (ctype
->type
== SYM_NODE
) {
2039 ctype
= ctype
->ctype
.base_type
;
2040 address_space
|= ctype
->ctype
.as
;
2041 mod
|= ctype
->ctype
.modifiers
;
2043 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
2044 expression_error(expr
, "expected structure or union");
2048 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
2050 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
2051 const char *name
= "<unnamed>";
2054 name
= ctype
->ident
->name
;
2055 namelen
= ctype
->ident
->len
;
2057 if (ctype
->symbol_list
)
2058 expression_error(expr
, "no member '%s' in %s %.*s",
2059 show_ident(ident
), type
, namelen
, name
);
2061 expression_error(expr
, "using member '%s' in "
2062 "incomplete %s %.*s", show_ident(ident
),
2063 type
, namelen
, name
);
2068 * The member needs to take on the address space and modifiers of
2069 * the "parent" type.
2071 member
= convert_to_as_mod(member
, address_space
, mod
);
2072 ctype
= get_base_type(member
);
2074 if (!lvalue_expression(deref
)) {
2075 if (deref
->type
!= EXPR_SLICE
) {
2079 expr
->base
= deref
->base
;
2080 expr
->r_bitpos
= deref
->r_bitpos
;
2082 expr
->r_bitpos
+= bytes_to_bits(offset
);
2083 expr
->type
= EXPR_SLICE
;
2084 expr
->r_nrbits
= member
->bit_size
;
2085 expr
->r_bitpos
+= member
->bit_offset
;
2086 expr
->ctype
= member
;
2090 deref
= deref
->unop
;
2091 expr
->deref
= deref
;
2093 add
= evaluate_offset(deref
, offset
);
2094 expr
->type
= EXPR_PREOP
;
2098 expr
->ctype
= member
;
2102 static int is_promoted(struct expression
*expr
)
2105 switch (expr
->type
) {
2108 case EXPR_CONDITIONAL
:
2132 static struct symbol
*evaluate_cast(struct expression
*);
2134 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2136 struct symbol
*sym
= expr
->cast_type
;
2138 sym
= evaluate_expression(expr
->cast_expression
);
2142 * Expressions of restricted types will possibly get
2143 * promoted - check that here
2145 if (is_restricted_type(sym
)) {
2146 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2148 } else if (is_fouled_type(sym
)) {
2152 examine_symbol_type(sym
);
2153 if (is_bitfield_type(sym
)) {
2154 expression_error(expr
, "trying to examine bitfield type");
2160 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2162 struct symbol
*type
;
2165 type
= evaluate_type_information(expr
);
2169 size
= type
->bit_size
;
2171 if (size
< 0 && is_void_type(type
)) {
2173 warning(expr
->pos
, "expression using sizeof(void)");
2174 size
= bits_in_char
;
2177 if (size
== 1 && is_bool_type(type
)) {
2179 warning(expr
->pos
, "expression using sizeof bool");
2180 size
= bits_in_char
;
2183 if (is_function(type
->ctype
.base_type
)) {
2185 warning(expr
->pos
, "expression using sizeof on a function");
2186 size
= bits_in_char
;
2189 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2190 expression_error(expr
, "cannot size expression");
2192 expr
->type
= EXPR_VALUE
;
2193 expr
->value
= bits_to_bytes(size
);
2195 expr
->ctype
= size_t_ctype
;
2196 return size_t_ctype
;
2199 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2201 struct symbol
*type
;
2204 type
= evaluate_type_information(expr
);
2208 if (type
->type
== SYM_NODE
)
2209 type
= type
->ctype
.base_type
;
2212 switch (type
->type
) {
2216 type
= get_base_type(type
);
2220 expression_error(expr
, "expected pointer expression");
2223 size
= type
->bit_size
;
2224 if (size
& (bits_in_char
-1))
2226 expr
->type
= EXPR_VALUE
;
2227 expr
->value
= bits_to_bytes(size
);
2229 expr
->ctype
= size_t_ctype
;
2230 return size_t_ctype
;
2233 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2235 struct symbol
*type
;
2237 type
= evaluate_type_information(expr
);
2241 expr
->type
= EXPR_VALUE
;
2242 expr
->value
= type
->ctype
.alignment
;
2244 expr
->ctype
= size_t_ctype
;
2245 return size_t_ctype
;
2248 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
2250 struct expression
*expr
;
2251 struct symbol_list
*argument_types
= fn
->arguments
;
2252 struct symbol
*argtype
;
2255 PREPARE_PTR_LIST(argument_types
, argtype
);
2256 FOR_EACH_PTR (head
, expr
) {
2257 struct expression
**p
= THIS_ADDRESS(expr
);
2258 struct symbol
*ctype
, *target
;
2259 ctype
= evaluate_expression(expr
);
2266 struct symbol
*type
;
2267 int class = classify_type(ctype
, &type
);
2268 if (is_int(class)) {
2269 *p
= cast_to(expr
, integer_promotion(type
));
2270 } else if (class & TYPE_FLOAT
) {
2271 unsigned long mod
= type
->ctype
.modifiers
;
2272 if (!(mod
& (MOD_LONG_ALL
)))
2273 *p
= cast_to(expr
, &double_ctype
);
2274 } else if (class & TYPE_PTR
) {
2275 if (expr
->ctype
== &null_ctype
)
2276 *p
= cast_to(expr
, &ptr_ctype
);
2280 } else if (!target
->forced_arg
){
2281 static char where
[30];
2282 examine_symbol_type(target
);
2283 sprintf(where
, "argument %d", i
);
2284 compatible_argument_type(expr
, target
, p
, where
);
2288 NEXT_PTR_LIST(argtype
);
2289 } END_FOR_EACH_PTR(expr
);
2290 FINISH_PTR_LIST(argtype
);
2294 static void convert_index(struct expression
*e
)
2296 struct expression
*child
= e
->idx_expression
;
2297 unsigned from
= e
->idx_from
;
2298 unsigned to
= e
->idx_to
+ 1;
2300 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2301 e
->init_nr
= to
- from
;
2302 e
->init_expr
= child
;
2305 static void convert_ident(struct expression
*e
)
2307 struct expression
*child
= e
->ident_expression
;
2308 int offset
= e
->offset
;
2311 e
->init_offset
= offset
;
2313 e
->init_expr
= child
;
2316 static void convert_designators(struct expression
*e
)
2319 if (e
->type
== EXPR_INDEX
)
2321 else if (e
->type
== EXPR_IDENTIFIER
)
2329 static void excess(struct expression
*e
, const char *s
)
2331 warning(e
->pos
, "excessive elements in %s initializer", s
);
2335 * implicit designator for the first element
2337 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2338 struct expression
**v
)
2340 struct expression
*e
= *v
, *new;
2342 if (ctype
->type
== SYM_NODE
)
2343 ctype
= ctype
->ctype
.base_type
;
2345 if (class & TYPE_PTR
) { /* array */
2346 if (!ctype
->bit_size
)
2348 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2349 new->idx_expression
= e
;
2350 new->ctype
= ctype
->ctype
.base_type
;
2352 struct symbol
*field
, *p
;
2353 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2354 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2360 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2361 new->ident_expression
= e
;
2362 new->field
= new->ctype
= field
;
2363 new->offset
= field
->offset
;
2370 * sanity-check explicit designators; return the innermost one or NULL
2371 * in case of error. Assign types.
2373 static struct expression
*check_designators(struct expression
*e
,
2374 struct symbol
*ctype
)
2376 struct expression
*last
= NULL
;
2379 if (ctype
->type
== SYM_NODE
)
2380 ctype
= ctype
->ctype
.base_type
;
2381 if (e
->type
== EXPR_INDEX
) {
2382 struct symbol
*type
;
2383 if (ctype
->type
!= SYM_ARRAY
) {
2384 err
= "array index in non-array";
2387 type
= ctype
->ctype
.base_type
;
2388 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2389 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2390 if (offset
>= ctype
->bit_size
) {
2391 err
= "index out of bounds in";
2395 e
->ctype
= ctype
= type
;
2398 if (!e
->idx_expression
) {
2402 e
= e
->idx_expression
;
2403 } else if (e
->type
== EXPR_IDENTIFIER
) {
2405 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2406 err
= "field name not in struct or union";
2409 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2411 err
= "unknown field name in";
2415 e
->field
= e
->ctype
= ctype
;
2417 if (!e
->ident_expression
) {
2421 e
= e
->ident_expression
;
2422 } else if (e
->type
== EXPR_POS
) {
2423 err
= "internal front-end error: EXPR_POS in";
2428 expression_error(e
, "%s initializer", err
);
2433 * choose the next subobject to initialize.
2435 * Get designators for next element, switch old ones to EXPR_POS.
2436 * Return the resulting expression or NULL if we'd run out of subobjects.
2437 * The innermost designator is returned in *v. Designators in old
2438 * are assumed to be already sanity-checked.
2440 static struct expression
*next_designators(struct expression
*old
,
2441 struct symbol
*ctype
,
2442 struct expression
*e
, struct expression
**v
)
2444 struct expression
*new = NULL
;
2448 if (old
->type
== EXPR_INDEX
) {
2449 struct expression
*copy
;
2452 copy
= next_designators(old
->idx_expression
,
2455 n
= old
->idx_to
+ 1;
2456 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2461 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2464 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2467 new->idx_from
= new->idx_to
= n
;
2468 new->idx_expression
= copy
;
2469 new->ctype
= old
->ctype
;
2471 } else if (old
->type
== EXPR_IDENTIFIER
) {
2472 struct expression
*copy
;
2473 struct symbol
*field
;
2476 copy
= next_designators(old
->ident_expression
,
2479 field
= old
->field
->next_subobject
;
2485 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2487 * We can't necessarily trust "field->offset",
2488 * because the field might be in an anonymous
2489 * union, and the field offset is then the offset
2490 * within that union.
2492 * The "old->offset - old->field->offset"
2493 * would be the offset of such an anonymous
2496 offset
= old
->offset
- old
->field
->offset
;
2499 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2503 new->expr_ident
= field
->ident
;
2504 new->ident_expression
= copy
;
2506 new->offset
= field
->offset
+ offset
;
2512 static int handle_initializer(struct expression
**ep
, int nested
,
2513 int class, struct symbol
*ctype
, unsigned long mods
);
2516 * deal with traversing subobjects [6.7.8(17,18,20)]
2518 static void handle_list_initializer(struct expression
*expr
,
2519 int class, struct symbol
*ctype
, unsigned long mods
)
2521 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2524 FOR_EACH_PTR(expr
->expr_list
, e
) {
2525 struct expression
**v
;
2526 struct symbol
*type
;
2529 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2530 struct symbol
*struct_sym
;
2533 last
= first_subobject(ctype
, class, &top
);
2535 last
= next_designators(last
, ctype
, e
, &top
);
2538 excess(e
, class & TYPE_PTR
? "array" :
2540 DELETE_CURRENT_PTR(e
);
2543 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2544 if (Wdesignated_init
&& struct_sym
->designated_init
)
2545 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2546 ctype
->ident
? "in initializer for " : "",
2547 ctype
->ident
? ctype
->ident
->len
: 0,
2548 ctype
->ident
? ctype
->ident
->name
: "",
2549 ctype
->ident
? ": " : "",
2550 get_type_name(struct_sym
->type
),
2551 show_ident(struct_sym
->ident
));
2553 warning(e
->pos
, "advancing past deep designator");
2556 REPLACE_CURRENT_PTR(e
, last
);
2558 next
= check_designators(e
, ctype
);
2560 DELETE_CURRENT_PTR(e
);
2564 /* deeper than one designator? */
2566 convert_designators(last
);
2571 lclass
= classify_type(top
->ctype
, &type
);
2572 if (top
->type
== EXPR_INDEX
)
2573 v
= &top
->idx_expression
;
2575 v
= &top
->ident_expression
;
2577 mods
|= ctype
->ctype
.modifiers
& MOD_STORAGE
;
2578 if (handle_initializer(v
, 1, lclass
, top
->ctype
, mods
))
2581 if (!(lclass
& TYPE_COMPOUND
)) {
2582 warning(e
->pos
, "bogus scalar initializer");
2583 DELETE_CURRENT_PTR(e
);
2587 next
= first_subobject(type
, lclass
, v
);
2589 warning(e
->pos
, "missing braces around initializer");
2594 DELETE_CURRENT_PTR(e
);
2595 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2597 } END_FOR_EACH_PTR(e
);
2599 convert_designators(last
);
2600 expr
->ctype
= ctype
;
2603 static int is_string_literal(struct expression
**v
)
2605 struct expression
*e
= *v
;
2606 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2608 if (!e
|| e
->type
!= EXPR_STRING
)
2610 if (e
!= *v
&& Wparen_string
)
2612 "array initialized from parenthesized string constant");
2618 * We want a normal expression, possibly in one layer of braces. Warn
2619 * if the latter happens inside a list (it's legal, but likely to be
2620 * an effect of screwup). In case of anything not legal, we are definitely
2621 * having an effect of screwup, so just fail and let the caller warn.
2623 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2625 struct expression
*v
= NULL
, *p
;
2629 if (e
->type
!= EXPR_INITIALIZER
)
2632 FOR_EACH_PTR(e
->expr_list
, p
) {
2636 } END_FOR_EACH_PTR(p
);
2640 case EXPR_INITIALIZER
:
2642 case EXPR_IDENTIFIER
:
2648 warning(e
->pos
, "braces around scalar initializer");
2653 * deal with the cases that don't care about subobjects:
2654 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2655 * character array <- string literal, possibly in braces [6.7.8(14)]
2656 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2657 * compound type <- initializer list in braces [6.7.8(16)]
2658 * The last one punts to handle_list_initializer() which, in turn will call
2659 * us for individual elements of the list.
2661 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2662 * the lack of support of wide char stuff in general.
2664 * One note: we need to take care not to evaluate a string literal until
2665 * we know that we *will* handle it right here. Otherwise we would screw
2666 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2667 * { "string", ...} - we need to preserve that string literal recognizable
2668 * until we dig into the inner struct.
2670 static int handle_initializer(struct expression
**ep
, int nested
,
2671 int class, struct symbol
*ctype
, unsigned long mods
)
2673 int is_string
= is_string_type(ctype
);
2674 struct expression
*e
= *ep
, *p
;
2675 struct symbol
*type
;
2681 if (!(class & TYPE_COMPOUND
)) {
2682 e
= handle_scalar(e
, nested
);
2686 if (!evaluate_expression(e
))
2688 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2690 * Initializers for static storage duration objects
2691 * shall be constant expressions or a string literal [6.7.8(4)].
2693 mods
|= ctype
->ctype
.modifiers
;
2694 mods
&= (MOD_TOPLEVEL
| MOD_STATIC
);
2695 if (mods
&& !(e
->flags
& (CEF_ACE
| CEF_ADDR
)))
2696 if (Wconstexpr_not_const
)
2697 warning(e
->pos
, "non-constant initializer for static object");
2703 * sublist; either a string, or we dig in; the latter will deal with
2704 * pathologies, so we don't need anything fancy here.
2706 if (e
->type
== EXPR_INITIALIZER
) {
2708 struct expression
*v
= NULL
;
2711 FOR_EACH_PTR(e
->expr_list
, p
) {
2715 } END_FOR_EACH_PTR(p
);
2716 if (count
== 1 && is_string_literal(&v
)) {
2721 handle_list_initializer(e
, class, ctype
, mods
);
2726 if (is_string_literal(&e
)) {
2727 /* either we are doing array of char, or we'll have to dig in */
2734 /* struct or union can be initialized by compatible */
2735 if (class != TYPE_COMPOUND
)
2737 type
= evaluate_expression(e
);
2740 if (ctype
->type
== SYM_NODE
)
2741 ctype
= ctype
->ctype
.base_type
;
2742 if (type
->type
== SYM_NODE
)
2743 type
= type
->ctype
.base_type
;
2749 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2751 type
= evaluate_expression(p
);
2752 if (ctype
->bit_size
!= -1) {
2753 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2755 "too long initializer-string for array of char");
2756 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2758 "too long initializer-string for array of char(no space for nul char)");
2765 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2767 struct symbol
*type
;
2768 int class = classify_type(ctype
, &type
);
2769 if (!handle_initializer(ep
, 0, class, ctype
, 0))
2770 expression_error(*ep
, "invalid initializer");
2773 static struct symbol
*cast_to_bool(struct expression
*expr
)
2775 struct expression
*old
= expr
->cast_expression
;
2776 struct expression
*zero
;
2777 struct symbol
*otype
;
2778 int oclass
= classify_type(degenerate(old
), &otype
);
2779 struct symbol
*ctype
;
2781 if (oclass
& TYPE_COMPOUND
)
2784 zero
= alloc_const_expression(expr
->pos
, 0);
2785 expr
->op
= SPECIAL_NOTEQUAL
;
2786 ctype
= usual_conversions(expr
->op
, old
, zero
,
2787 oclass
, TYPE_NUM
, otype
, zero
->ctype
);
2788 expr
->type
= EXPR_COMPARE
;
2789 expr
->left
= cast_to(old
, ctype
);
2790 expr
->right
= cast_to(zero
, ctype
);
2795 static int cast_flags(struct expression
*expr
, struct expression
*old
)
2799 int flags
= CEF_NONE
;
2801 class = classify_type(expr
->ctype
, &t
);
2802 if (class & TYPE_NUM
) {
2803 flags
= old
->flags
& ~CEF_CONST_MASK
;
2805 * Casts to numeric types never result in address
2806 * constants [6.6(9)].
2811 * As an extension, treat address constants cast to
2812 * integer type as an arithmetic constant.
2814 if (old
->flags
& CEF_ADDR
)
2818 * Cast to float type -> not an integer constant
2819 * expression [6.6(6)].
2821 if (class & TYPE_FLOAT
)
2822 flags
&= ~CEF_CLR_ICE
;
2824 * Casts of float literals to integer type results in
2825 * a constant integer expression [6.6(6)].
2827 else if (old
->flags
& CEF_FLOAT
)
2828 flags
= CEF_SET_ICE
;
2829 } else if (class & TYPE_PTR
) {
2831 * Casts of integer literals to pointer type yield
2832 * address constants [6.6(9)].
2834 * As an extension, treat address constants cast to a
2835 * different pointer type as address constants again.
2837 * As another extension, treat integer constant
2838 * expressions (in contrast to literals) cast to
2839 * pointer type as address constants.
2841 if (old
->flags
& (CEF_ICE
| CEF_ADDR
))
2848 static struct symbol
*evaluate_cast(struct expression
*expr
)
2850 struct expression
*target
= expr
->cast_expression
;
2851 struct symbol
*ctype
;
2852 struct symbol
*t1
, *t2
;
2854 int as1
= 0, as2
= 0;
2860 * Special case: a cast can be followed by an
2861 * initializer, in which case we need to pass
2862 * the type value down to that initializer rather
2863 * than trying to evaluate it as an expression
2865 * A more complex case is when the initializer is
2866 * dereferenced as part of a post-fix expression.
2867 * We need to produce an expression that can be dereferenced.
2869 if (target
->type
== EXPR_INITIALIZER
) {
2870 struct symbol
*sym
= expr
->cast_type
;
2871 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2873 sym
->initializer
= target
;
2874 evaluate_symbol(sym
);
2876 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2878 if (sym
->ctype
.modifiers
& MOD_TOPLEVEL
)
2879 addr
->flags
|= CEF_ADDR
;
2881 expr
->type
= EXPR_PREOP
;
2889 ctype
= examine_symbol_type(expr
->cast_type
);
2890 expr
->ctype
= ctype
;
2891 expr
->cast_type
= ctype
;
2893 evaluate_expression(target
);
2896 class1
= classify_type(ctype
, &t1
);
2898 expr
->flags
= cast_flags(expr
, target
);
2901 * You can always throw a value away by casting to
2902 * "void" - that's an implicit "force". Note that
2903 * the same is _not_ true of "void *".
2905 if (t1
== &void_ctype
)
2908 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2909 warning(expr
->pos
, "cast to non-scalar");
2913 expression_error(expr
, "cast from unknown type");
2916 class2
= classify_type(t2
, &t2
);
2918 if (class2
& TYPE_COMPOUND
)
2919 warning(expr
->pos
, "cast from non-scalar");
2921 if (expr
->type
== EXPR_FORCE_CAST
)
2924 /* allowed cast unfouls */
2925 if (class2
& TYPE_FOULED
)
2929 if ((class1
& TYPE_RESTRICT
) && restricted_value(target
, t1
))
2930 warning(expr
->pos
, "cast to %s",
2932 if (class2
& TYPE_RESTRICT
) {
2933 if (t1
== &bool_ctype
) {
2934 if (class2
& TYPE_FOULED
)
2935 warning(expr
->pos
, "%s degrades to integer",
2938 warning(expr
->pos
, "cast from %s",
2944 if (t1
== &ulong_ctype
)
2946 else if (class1
== TYPE_PTR
) {
2947 examine_pointer_target(t1
);
2951 if (t2
== &ulong_ctype
)
2953 else if (class2
== TYPE_PTR
) {
2954 examine_pointer_target(t2
);
2958 if (!as1
&& as2
> 0)
2959 warning(expr
->pos
, "cast removes address space of expression");
2960 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2961 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2962 if (as1
> 0 && !as2
&&
2963 !is_null_pointer_constant(target
) && Wcast_to_as
)
2965 "cast adds address space to expression (<asn:%d>)", as1
);
2967 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2968 !as1
&& (target
->flags
& CEF_ICE
)) {
2969 if (t1
->ctype
.base_type
== &void_ctype
) {
2970 if (is_zero_constant(target
)) {
2972 expr
->type
= EXPR_VALUE
;
2973 expr
->ctype
= &null_ctype
;
2980 if (t1
== &bool_ctype
)
2988 * Evaluate a call expression with a symbol. This
2989 * should expand inline functions, and evaluate
2992 static int evaluate_symbol_call(struct expression
*expr
)
2994 struct expression
*fn
= expr
->fn
;
2995 struct symbol
*ctype
= fn
->ctype
;
2997 if (fn
->type
!= EXPR_PREOP
)
3000 if (ctype
->op
&& ctype
->op
->evaluate
)
3001 return ctype
->op
->evaluate(expr
);
3003 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
3005 struct symbol
*curr
= current_fn
;
3007 if (ctype
->definition
)
3008 ctype
= ctype
->definition
;
3010 current_fn
= ctype
->ctype
.base_type
;
3012 ret
= inline_function(expr
, ctype
);
3014 /* restore the old function */
3022 static struct symbol
*evaluate_call(struct expression
*expr
)
3025 struct symbol
*ctype
, *sym
;
3026 struct expression
*fn
= expr
->fn
;
3027 struct expression_list
*arglist
= expr
->args
;
3029 if (!evaluate_expression(fn
))
3031 sym
= ctype
= fn
->ctype
;
3032 if (ctype
->type
== SYM_NODE
)
3033 ctype
= ctype
->ctype
.base_type
;
3034 if (ctype
->type
== SYM_PTR
)
3035 ctype
= get_base_type(ctype
);
3037 if (ctype
->type
!= SYM_FN
) {
3038 struct expression
*arg
;
3039 expression_error(expr
, "not a function %s",
3040 show_ident(sym
->ident
));
3041 /* do typechecking in arguments */
3042 FOR_EACH_PTR (arglist
, arg
) {
3043 evaluate_expression(arg
);
3044 } END_FOR_EACH_PTR(arg
);
3048 examine_fn_arguments(ctype
);
3049 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
3050 sym
->op
&& sym
->op
->args
) {
3051 if (!sym
->op
->args(expr
))
3054 if (!evaluate_arguments(ctype
, arglist
))
3056 args
= expression_list_size(expr
->args
);
3057 fnargs
= symbol_list_size(ctype
->arguments
);
3058 if (args
< fnargs
) {
3059 expression_error(expr
,
3060 "not enough arguments for function %s",
3061 show_ident(sym
->ident
));
3064 if (args
> fnargs
&& !ctype
->variadic
)
3065 expression_error(expr
,
3066 "too many arguments for function %s",
3067 show_ident(sym
->ident
));
3069 expr
->ctype
= ctype
->ctype
.base_type
;
3070 if (sym
->type
== SYM_NODE
) {
3071 if (evaluate_symbol_call(expr
))
3077 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
3079 struct expression
*e
= expr
->down
;
3080 struct symbol
*ctype
= expr
->in
;
3083 if (expr
->op
== '.') {
3084 struct symbol
*field
;
3087 expression_error(expr
, "expected structure or union");
3090 examine_symbol_type(ctype
);
3091 class = classify_type(ctype
, &ctype
);
3092 if (class != TYPE_COMPOUND
) {
3093 expression_error(expr
, "expected structure or union");
3097 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
3099 expression_error(expr
, "unknown member");
3103 expr
->type
= EXPR_VALUE
;
3104 expr
->flags
= CEF_SET_ICE
;
3105 expr
->value
= offset
;
3107 expr
->ctype
= size_t_ctype
;
3110 expression_error(expr
, "expected structure or union");
3113 examine_symbol_type(ctype
);
3114 class = classify_type(ctype
, &ctype
);
3115 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
3116 expression_error(expr
, "expected array");
3119 ctype
= ctype
->ctype
.base_type
;
3121 expr
->type
= EXPR_VALUE
;
3122 expr
->flags
= CEF_SET_ICE
;
3125 expr
->ctype
= size_t_ctype
;
3127 struct expression
*idx
= expr
->index
, *m
;
3128 struct symbol
*i_type
= evaluate_expression(idx
);
3129 unsigned old_idx_flags
;
3130 int i_class
= classify_type(i_type
, &i_type
);
3132 if (!is_int(i_class
)) {
3133 expression_error(expr
, "non-integer index");
3136 unrestrict(idx
, i_class
, &i_type
);
3137 old_idx_flags
= idx
->flags
;
3138 idx
= cast_to(idx
, size_t_ctype
);
3139 idx
->flags
= old_idx_flags
;
3140 m
= alloc_const_expression(expr
->pos
,
3141 bits_to_bytes(ctype
->bit_size
));
3142 m
->ctype
= size_t_ctype
;
3143 m
->flags
= CEF_SET_INT
;
3144 expr
->type
= EXPR_BINOP
;
3148 expr
->ctype
= size_t_ctype
;
3149 expr
->flags
= m
->flags
& idx
->flags
& ~CEF_CONST_MASK
;
3153 struct expression
*copy
= __alloc_expression(0);
3155 if (e
->type
== EXPR_OFFSETOF
)
3157 if (!evaluate_expression(e
))
3159 expr
->type
= EXPR_BINOP
;
3160 expr
->flags
= e
->flags
& copy
->flags
& ~CEF_CONST_MASK
;
3162 expr
->ctype
= size_t_ctype
;
3166 return size_t_ctype
;
3169 struct symbol
*evaluate_expression(struct expression
*expr
)
3176 switch (expr
->type
) {
3179 expression_error(expr
, "value expression without a type");
3182 return evaluate_string(expr
);
3184 return evaluate_symbol_expression(expr
);
3186 if (!evaluate_expression(expr
->left
))
3188 if (!evaluate_expression(expr
->right
))
3190 return evaluate_binop(expr
);
3192 return evaluate_logical(expr
);
3194 evaluate_expression(expr
->left
);
3195 if (!evaluate_expression(expr
->right
))
3197 return evaluate_comma(expr
);
3199 if (!evaluate_expression(expr
->left
))
3201 if (!evaluate_expression(expr
->right
))
3203 return evaluate_compare(expr
);
3204 case EXPR_ASSIGNMENT
:
3205 if (!evaluate_expression(expr
->left
))
3207 if (!evaluate_expression(expr
->right
))
3209 return evaluate_assignment(expr
);
3211 if (!evaluate_expression(expr
->unop
))
3213 return evaluate_preop(expr
);
3215 if (!evaluate_expression(expr
->unop
))
3217 return evaluate_postop(expr
);
3219 case EXPR_FORCE_CAST
:
3220 case EXPR_IMPLIED_CAST
:
3221 return evaluate_cast(expr
);
3223 return evaluate_sizeof(expr
);
3224 case EXPR_PTRSIZEOF
:
3225 return evaluate_ptrsizeof(expr
);
3227 return evaluate_alignof(expr
);
3229 return evaluate_member_dereference(expr
);
3231 return evaluate_call(expr
);
3233 case EXPR_CONDITIONAL
:
3234 return evaluate_conditional_expression(expr
);
3235 case EXPR_STATEMENT
:
3236 expr
->ctype
= evaluate_statement(expr
->statement
);
3240 expr
->ctype
= &ptr_ctype
;
3244 /* Evaluate the type of the symbol .. */
3245 evaluate_symbol(expr
->symbol
);
3246 /* .. but the type of the _expression_ is a "type" */
3247 expr
->ctype
= &type_ctype
;
3251 return evaluate_offsetof(expr
);
3253 /* These can not exist as stand-alone expressions */
3254 case EXPR_INITIALIZER
:
3255 case EXPR_IDENTIFIER
:
3258 expression_error(expr
, "internal front-end error: initializer in expression");
3261 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3267 static void check_duplicates(struct symbol
*sym
)
3270 struct symbol
*next
= sym
;
3271 int initialized
= sym
->initializer
!= NULL
;
3273 while ((next
= next
->same_symbol
) != NULL
) {
3274 const char *typediff
;
3275 evaluate_symbol(next
);
3276 if (initialized
&& next
->initializer
) {
3277 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3278 show_ident(sym
->ident
),
3279 stream_name(next
->pos
.stream
), next
->pos
.line
);
3280 /* Only warn once */
3284 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3286 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3287 show_ident(sym
->ident
),
3288 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3293 unsigned long mod
= sym
->ctype
.modifiers
;
3294 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3296 if (!(mod
& MOD_TOPLEVEL
))
3300 if (sym
->ident
== &main_ident
)
3302 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3306 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3308 struct symbol
*base_type
;
3316 sym
= examine_symbol_type(sym
);
3317 base_type
= get_base_type(sym
);
3321 /* Evaluate the initializers */
3322 if (sym
->initializer
)
3323 evaluate_initializer(sym
, &sym
->initializer
);
3325 /* And finally, evaluate the body of the symbol too */
3326 if (base_type
->type
== SYM_FN
) {
3327 struct symbol
*curr
= current_fn
;
3329 if (sym
->definition
&& sym
->definition
!= sym
)
3330 return evaluate_symbol(sym
->definition
);
3332 current_fn
= base_type
;
3334 examine_fn_arguments(base_type
);
3335 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3337 if (base_type
->stmt
)
3338 evaluate_statement(base_type
->stmt
);
3346 void evaluate_symbol_list(struct symbol_list
*list
)
3350 FOR_EACH_PTR(list
, sym
) {
3351 has_error
&= ~ERROR_CURR_PHASE
;
3352 evaluate_symbol(sym
);
3353 check_duplicates(sym
);
3354 } END_FOR_EACH_PTR(sym
);
3357 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3359 struct expression
*expr
= stmt
->expression
;
3360 struct symbol
*fntype
;
3362 evaluate_expression(expr
);
3363 fntype
= current_fn
->ctype
.base_type
;
3364 if (!fntype
|| fntype
== &void_ctype
) {
3365 if (expr
&& expr
->ctype
!= &void_ctype
)
3366 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3367 if (expr
&& Wreturn_void
)
3368 warning(stmt
->pos
, "returning void-valued expression");
3373 sparse_error(stmt
->pos
, "return with no return value");
3378 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3382 static void evaluate_if_statement(struct statement
*stmt
)
3384 if (!stmt
->if_conditional
)
3387 evaluate_conditional(stmt
->if_conditional
, 0);
3388 evaluate_statement(stmt
->if_true
);
3389 evaluate_statement(stmt
->if_false
);
3392 static void evaluate_iterator(struct statement
*stmt
)
3394 evaluate_symbol_list(stmt
->iterator_syms
);
3395 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3396 evaluate_conditional(stmt
->iterator_post_condition
,1);
3397 evaluate_statement(stmt
->iterator_pre_statement
);
3398 evaluate_statement(stmt
->iterator_statement
);
3399 evaluate_statement(stmt
->iterator_post_statement
);
3402 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3404 switch (*constraint
) {
3405 case '=': /* Assignment */
3406 case '+': /* Update */
3409 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3413 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3415 switch (*constraint
) {
3416 case '=': /* Assignment */
3417 case '+': /* Update */
3418 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3422 static void evaluate_asm_statement(struct statement
*stmt
)
3424 struct expression
*expr
;
3428 expr
= stmt
->asm_string
;
3429 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3430 sparse_error(stmt
->pos
, "need constant string for inline asm");
3435 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3437 case 0: /* Identifier */
3441 case 1: /* Constraint */
3443 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3444 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3445 *THIS_ADDRESS(expr
) = NULL
;
3448 verify_output_constraint(expr
, expr
->string
->data
);
3451 case 2: /* Expression */
3453 if (!evaluate_expression(expr
))
3455 if (!lvalue_expression(expr
))
3456 warning(expr
->pos
, "asm output is not an lvalue");
3457 evaluate_assign_to(expr
, expr
->ctype
);
3460 } END_FOR_EACH_PTR(expr
);
3463 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3465 case 0: /* Identifier */
3469 case 1: /* Constraint */
3471 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3472 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3473 *THIS_ADDRESS(expr
) = NULL
;
3476 verify_input_constraint(expr
, expr
->string
->data
);
3479 case 2: /* Expression */
3481 if (!evaluate_expression(expr
))
3485 } END_FOR_EACH_PTR(expr
);
3487 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3489 sparse_error(stmt
->pos
, "bad asm clobbers");
3492 if (expr
->type
== EXPR_STRING
)
3494 expression_error(expr
, "asm clobber is not a string");
3495 } END_FOR_EACH_PTR(expr
);
3497 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3498 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3499 sparse_error(stmt
->pos
, "bad asm label");
3502 } END_FOR_EACH_PTR(sym
);
3505 static void evaluate_case_statement(struct statement
*stmt
)
3507 evaluate_expression(stmt
->case_expression
);
3508 evaluate_expression(stmt
->case_to
);
3509 evaluate_statement(stmt
->case_statement
);
3512 static void check_case_type(struct expression
*switch_expr
,
3513 struct expression
*case_expr
,
3514 struct expression
**enumcase
)
3516 struct symbol
*switch_type
, *case_type
;
3522 switch_type
= switch_expr
->ctype
;
3523 case_type
= evaluate_expression(case_expr
);
3525 if (!switch_type
|| !case_type
)
3529 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3530 else if (is_enum_type(case_type
))
3531 *enumcase
= case_expr
;
3534 sclass
= classify_type(switch_type
, &switch_type
);
3535 cclass
= classify_type(case_type
, &case_type
);
3537 /* both should be arithmetic */
3538 if (!(sclass
& cclass
& TYPE_NUM
))
3541 /* neither should be floating */
3542 if ((sclass
| cclass
) & TYPE_FLOAT
)
3545 /* if neither is restricted, we are OK */
3546 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3549 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3550 cclass
, sclass
, case_type
, switch_type
)) {
3551 unrestrict(case_expr
, cclass
, &case_type
);
3552 unrestrict(switch_expr
, sclass
, &switch_type
);
3557 expression_error(case_expr
, "incompatible types for 'case' statement");
3560 static void evaluate_switch_statement(struct statement
*stmt
)
3563 struct expression
*enumcase
= NULL
;
3564 struct expression
**enumcase_holder
= &enumcase
;
3565 struct expression
*sel
= stmt
->switch_expression
;
3567 evaluate_expression(sel
);
3568 evaluate_statement(stmt
->switch_statement
);
3571 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3572 enumcase_holder
= NULL
; /* Only check cases against switch */
3574 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3575 struct statement
*case_stmt
= sym
->stmt
;
3576 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3577 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3578 } END_FOR_EACH_PTR(sym
);
3581 static void evaluate_goto_statement(struct statement
*stmt
)
3583 struct symbol
*label
= stmt
->goto_label
;
3585 if (label
&& !label
->stmt
&& !lookup_keyword(label
->ident
, NS_KEYWORD
))
3586 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3588 evaluate_expression(stmt
->goto_expression
);
3591 struct symbol
*evaluate_statement(struct statement
*stmt
)
3596 switch (stmt
->type
) {
3597 case STMT_DECLARATION
: {
3599 FOR_EACH_PTR(stmt
->declaration
, s
) {
3601 } END_FOR_EACH_PTR(s
);
3606 return evaluate_return_expression(stmt
);
3608 case STMT_EXPRESSION
:
3609 if (!evaluate_expression(stmt
->expression
))
3611 if (stmt
->expression
->ctype
== &null_ctype
)
3612 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3613 return degenerate(stmt
->expression
);
3615 case STMT_COMPOUND
: {
3616 struct statement
*s
;
3617 struct symbol
*type
= NULL
;
3619 /* Evaluate the return symbol in the compound statement */
3620 evaluate_symbol(stmt
->ret
);
3623 * Then, evaluate each statement, making the type of the
3624 * compound statement be the type of the last statement
3626 type
= evaluate_statement(stmt
->args
);
3627 FOR_EACH_PTR(stmt
->stmts
, s
) {
3628 type
= evaluate_statement(s
);
3629 } END_FOR_EACH_PTR(s
);
3635 evaluate_if_statement(stmt
);
3638 evaluate_iterator(stmt
);
3641 evaluate_switch_statement(stmt
);
3644 evaluate_case_statement(stmt
);
3647 return evaluate_statement(stmt
->label_statement
);
3649 evaluate_goto_statement(stmt
);
3654 evaluate_asm_statement(stmt
);
3657 evaluate_expression(stmt
->expression
);
3660 evaluate_expression(stmt
->range_expression
);
3661 evaluate_expression(stmt
->range_low
);
3662 evaluate_expression(stmt
->range_high
);