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
)) {
2172 warning(expr
->pos
, "expression using sizeof(void)");
2173 size
= bits_in_char
;
2176 if (size
== 1 && is_bool_type(type
)) {
2178 warning(expr
->pos
, "expression using sizeof bool");
2179 size
= bits_in_char
;
2182 if (is_function(type
->ctype
.base_type
)) {
2183 warning(expr
->pos
, "expression using sizeof on a function");
2184 size
= bits_in_char
;
2187 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2188 expression_error(expr
, "cannot size expression");
2190 expr
->type
= EXPR_VALUE
;
2191 expr
->value
= bits_to_bytes(size
);
2193 expr
->ctype
= size_t_ctype
;
2194 return size_t_ctype
;
2197 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2199 struct symbol
*type
;
2202 type
= evaluate_type_information(expr
);
2206 if (type
->type
== SYM_NODE
)
2207 type
= type
->ctype
.base_type
;
2210 switch (type
->type
) {
2214 type
= get_base_type(type
);
2218 expression_error(expr
, "expected pointer expression");
2221 size
= type
->bit_size
;
2222 if (size
& (bits_in_char
-1))
2224 expr
->type
= EXPR_VALUE
;
2225 expr
->value
= bits_to_bytes(size
);
2227 expr
->ctype
= size_t_ctype
;
2228 return size_t_ctype
;
2231 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2233 struct symbol
*type
;
2235 type
= evaluate_type_information(expr
);
2239 expr
->type
= EXPR_VALUE
;
2240 expr
->value
= type
->ctype
.alignment
;
2242 expr
->ctype
= size_t_ctype
;
2243 return size_t_ctype
;
2246 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
2248 struct expression
*expr
;
2249 struct symbol_list
*argument_types
= fn
->arguments
;
2250 struct symbol
*argtype
;
2253 PREPARE_PTR_LIST(argument_types
, argtype
);
2254 FOR_EACH_PTR (head
, expr
) {
2255 struct expression
**p
= THIS_ADDRESS(expr
);
2256 struct symbol
*ctype
, *target
;
2257 ctype
= evaluate_expression(expr
);
2264 struct symbol
*type
;
2265 int class = classify_type(ctype
, &type
);
2266 if (is_int(class)) {
2267 *p
= cast_to(expr
, integer_promotion(type
));
2268 } else if (class & TYPE_FLOAT
) {
2269 unsigned long mod
= type
->ctype
.modifiers
;
2270 if (!(mod
& (MOD_LONG_ALL
)))
2271 *p
= cast_to(expr
, &double_ctype
);
2272 } else if (class & TYPE_PTR
) {
2273 if (expr
->ctype
== &null_ctype
)
2274 *p
= cast_to(expr
, &ptr_ctype
);
2278 } else if (!target
->forced_arg
){
2279 static char where
[30];
2280 examine_symbol_type(target
);
2281 sprintf(where
, "argument %d", i
);
2282 compatible_argument_type(expr
, target
, p
, where
);
2286 NEXT_PTR_LIST(argtype
);
2287 } END_FOR_EACH_PTR(expr
);
2288 FINISH_PTR_LIST(argtype
);
2292 static void convert_index(struct expression
*e
)
2294 struct expression
*child
= e
->idx_expression
;
2295 unsigned from
= e
->idx_from
;
2296 unsigned to
= e
->idx_to
+ 1;
2298 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2299 e
->init_nr
= to
- from
;
2300 e
->init_expr
= child
;
2303 static void convert_ident(struct expression
*e
)
2305 struct expression
*child
= e
->ident_expression
;
2306 int offset
= e
->offset
;
2309 e
->init_offset
= offset
;
2311 e
->init_expr
= child
;
2314 static void convert_designators(struct expression
*e
)
2317 if (e
->type
== EXPR_INDEX
)
2319 else if (e
->type
== EXPR_IDENTIFIER
)
2327 static void excess(struct expression
*e
, const char *s
)
2329 warning(e
->pos
, "excessive elements in %s initializer", s
);
2333 * implicit designator for the first element
2335 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2336 struct expression
**v
)
2338 struct expression
*e
= *v
, *new;
2340 if (ctype
->type
== SYM_NODE
)
2341 ctype
= ctype
->ctype
.base_type
;
2343 if (class & TYPE_PTR
) { /* array */
2344 if (!ctype
->bit_size
)
2346 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2347 new->idx_expression
= e
;
2348 new->ctype
= ctype
->ctype
.base_type
;
2350 struct symbol
*field
, *p
;
2351 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2352 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2358 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2359 new->ident_expression
= e
;
2360 new->field
= new->ctype
= field
;
2361 new->offset
= field
->offset
;
2368 * sanity-check explicit designators; return the innermost one or NULL
2369 * in case of error. Assign types.
2371 static struct expression
*check_designators(struct expression
*e
,
2372 struct symbol
*ctype
)
2374 struct expression
*last
= NULL
;
2377 if (ctype
->type
== SYM_NODE
)
2378 ctype
= ctype
->ctype
.base_type
;
2379 if (e
->type
== EXPR_INDEX
) {
2380 struct symbol
*type
;
2381 if (ctype
->type
!= SYM_ARRAY
) {
2382 err
= "array index in non-array";
2385 type
= ctype
->ctype
.base_type
;
2386 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2387 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2388 if (offset
>= ctype
->bit_size
) {
2389 err
= "index out of bounds in";
2393 e
->ctype
= ctype
= type
;
2396 if (!e
->idx_expression
) {
2400 e
= e
->idx_expression
;
2401 } else if (e
->type
== EXPR_IDENTIFIER
) {
2403 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2404 err
= "field name not in struct or union";
2407 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2409 err
= "unknown field name in";
2413 e
->field
= e
->ctype
= ctype
;
2415 if (!e
->ident_expression
) {
2419 e
= e
->ident_expression
;
2420 } else if (e
->type
== EXPR_POS
) {
2421 err
= "internal front-end error: EXPR_POS in";
2426 expression_error(e
, "%s initializer", err
);
2431 * choose the next subobject to initialize.
2433 * Get designators for next element, switch old ones to EXPR_POS.
2434 * Return the resulting expression or NULL if we'd run out of subobjects.
2435 * The innermost designator is returned in *v. Designators in old
2436 * are assumed to be already sanity-checked.
2438 static struct expression
*next_designators(struct expression
*old
,
2439 struct symbol
*ctype
,
2440 struct expression
*e
, struct expression
**v
)
2442 struct expression
*new = NULL
;
2446 if (old
->type
== EXPR_INDEX
) {
2447 struct expression
*copy
;
2450 copy
= next_designators(old
->idx_expression
,
2453 n
= old
->idx_to
+ 1;
2454 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2459 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2462 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2465 new->idx_from
= new->idx_to
= n
;
2466 new->idx_expression
= copy
;
2467 new->ctype
= old
->ctype
;
2469 } else if (old
->type
== EXPR_IDENTIFIER
) {
2470 struct expression
*copy
;
2471 struct symbol
*field
;
2474 copy
= next_designators(old
->ident_expression
,
2477 field
= old
->field
->next_subobject
;
2483 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2485 * We can't necessarily trust "field->offset",
2486 * because the field might be in an anonymous
2487 * union, and the field offset is then the offset
2488 * within that union.
2490 * The "old->offset - old->field->offset"
2491 * would be the offset of such an anonymous
2494 offset
= old
->offset
- old
->field
->offset
;
2497 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2501 new->expr_ident
= field
->ident
;
2502 new->ident_expression
= copy
;
2504 new->offset
= field
->offset
+ offset
;
2510 static int handle_initializer(struct expression
**ep
, int nested
,
2511 int class, struct symbol
*ctype
, unsigned long mods
);
2514 * deal with traversing subobjects [6.7.8(17,18,20)]
2516 static void handle_list_initializer(struct expression
*expr
,
2517 int class, struct symbol
*ctype
, unsigned long mods
)
2519 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2522 FOR_EACH_PTR(expr
->expr_list
, e
) {
2523 struct expression
**v
;
2524 struct symbol
*type
;
2527 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2528 struct symbol
*struct_sym
;
2531 last
= first_subobject(ctype
, class, &top
);
2533 last
= next_designators(last
, ctype
, e
, &top
);
2536 excess(e
, class & TYPE_PTR
? "array" :
2538 DELETE_CURRENT_PTR(e
);
2541 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2542 if (Wdesignated_init
&& struct_sym
->designated_init
)
2543 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2544 ctype
->ident
? "in initializer for " : "",
2545 ctype
->ident
? ctype
->ident
->len
: 0,
2546 ctype
->ident
? ctype
->ident
->name
: "",
2547 ctype
->ident
? ": " : "",
2548 get_type_name(struct_sym
->type
),
2549 show_ident(struct_sym
->ident
));
2551 warning(e
->pos
, "advancing past deep designator");
2554 REPLACE_CURRENT_PTR(e
, last
);
2556 next
= check_designators(e
, ctype
);
2558 DELETE_CURRENT_PTR(e
);
2562 /* deeper than one designator? */
2564 convert_designators(last
);
2569 lclass
= classify_type(top
->ctype
, &type
);
2570 if (top
->type
== EXPR_INDEX
)
2571 v
= &top
->idx_expression
;
2573 v
= &top
->ident_expression
;
2575 mods
|= ctype
->ctype
.modifiers
& MOD_STORAGE
;
2576 if (handle_initializer(v
, 1, lclass
, top
->ctype
, mods
))
2579 if (!(lclass
& TYPE_COMPOUND
)) {
2580 warning(e
->pos
, "bogus scalar initializer");
2581 DELETE_CURRENT_PTR(e
);
2585 next
= first_subobject(type
, lclass
, v
);
2587 warning(e
->pos
, "missing braces around initializer");
2592 DELETE_CURRENT_PTR(e
);
2593 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2595 } END_FOR_EACH_PTR(e
);
2597 convert_designators(last
);
2598 expr
->ctype
= ctype
;
2601 static int is_string_literal(struct expression
**v
)
2603 struct expression
*e
= *v
;
2604 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2606 if (!e
|| e
->type
!= EXPR_STRING
)
2608 if (e
!= *v
&& Wparen_string
)
2610 "array initialized from parenthesized string constant");
2616 * We want a normal expression, possibly in one layer of braces. Warn
2617 * if the latter happens inside a list (it's legal, but likely to be
2618 * an effect of screwup). In case of anything not legal, we are definitely
2619 * having an effect of screwup, so just fail and let the caller warn.
2621 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2623 struct expression
*v
= NULL
, *p
;
2627 if (e
->type
!= EXPR_INITIALIZER
)
2630 FOR_EACH_PTR(e
->expr_list
, p
) {
2634 } END_FOR_EACH_PTR(p
);
2638 case EXPR_INITIALIZER
:
2640 case EXPR_IDENTIFIER
:
2646 warning(e
->pos
, "braces around scalar initializer");
2651 * deal with the cases that don't care about subobjects:
2652 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2653 * character array <- string literal, possibly in braces [6.7.8(14)]
2654 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2655 * compound type <- initializer list in braces [6.7.8(16)]
2656 * The last one punts to handle_list_initializer() which, in turn will call
2657 * us for individual elements of the list.
2659 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2660 * the lack of support of wide char stuff in general.
2662 * One note: we need to take care not to evaluate a string literal until
2663 * we know that we *will* handle it right here. Otherwise we would screw
2664 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2665 * { "string", ...} - we need to preserve that string literal recognizable
2666 * until we dig into the inner struct.
2668 static int handle_initializer(struct expression
**ep
, int nested
,
2669 int class, struct symbol
*ctype
, unsigned long mods
)
2671 int is_string
= is_string_type(ctype
);
2672 struct expression
*e
= *ep
, *p
;
2673 struct symbol
*type
;
2679 if (!(class & TYPE_COMPOUND
)) {
2680 e
= handle_scalar(e
, nested
);
2684 if (!evaluate_expression(e
))
2686 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2688 * Initializers for static storage duration objects
2689 * shall be constant expressions or a string literal [6.7.8(4)].
2691 mods
|= ctype
->ctype
.modifiers
;
2692 mods
&= (MOD_TOPLEVEL
| MOD_STATIC
);
2693 if (mods
&& !(e
->flags
& (CEF_ACE
| CEF_ADDR
)))
2694 if (Wconstexpr_not_const
)
2695 warning(e
->pos
, "non-constant initializer for static object");
2701 * sublist; either a string, or we dig in; the latter will deal with
2702 * pathologies, so we don't need anything fancy here.
2704 if (e
->type
== EXPR_INITIALIZER
) {
2706 struct expression
*v
= NULL
;
2709 FOR_EACH_PTR(e
->expr_list
, p
) {
2713 } END_FOR_EACH_PTR(p
);
2714 if (count
== 1 && is_string_literal(&v
)) {
2719 handle_list_initializer(e
, class, ctype
, mods
);
2724 if (is_string_literal(&e
)) {
2725 /* either we are doing array of char, or we'll have to dig in */
2732 /* struct or union can be initialized by compatible */
2733 if (class != TYPE_COMPOUND
)
2735 type
= evaluate_expression(e
);
2738 if (ctype
->type
== SYM_NODE
)
2739 ctype
= ctype
->ctype
.base_type
;
2740 if (type
->type
== SYM_NODE
)
2741 type
= type
->ctype
.base_type
;
2747 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2749 type
= evaluate_expression(p
);
2750 if (ctype
->bit_size
!= -1) {
2751 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2753 "too long initializer-string for array of char");
2754 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2756 "too long initializer-string for array of char(no space for nul char)");
2763 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2765 struct symbol
*type
;
2766 int class = classify_type(ctype
, &type
);
2767 if (!handle_initializer(ep
, 0, class, ctype
, 0))
2768 expression_error(*ep
, "invalid initializer");
2771 static struct symbol
*cast_to_bool(struct expression
*expr
)
2773 struct expression
*old
= expr
->cast_expression
;
2774 struct expression
*zero
;
2775 struct symbol
*otype
;
2776 int oclass
= classify_type(degenerate(old
), &otype
);
2777 struct symbol
*ctype
;
2779 if (oclass
& TYPE_COMPOUND
)
2782 zero
= alloc_const_expression(expr
->pos
, 0);
2783 expr
->op
= SPECIAL_NOTEQUAL
;
2784 ctype
= usual_conversions(expr
->op
, old
, zero
,
2785 oclass
, TYPE_NUM
, otype
, zero
->ctype
);
2786 expr
->type
= EXPR_COMPARE
;
2787 expr
->left
= cast_to(old
, ctype
);
2788 expr
->right
= cast_to(zero
, ctype
);
2793 static int cast_flags(struct expression
*expr
, struct expression
*old
)
2797 int flags
= CEF_NONE
;
2799 class = classify_type(expr
->ctype
, &t
);
2800 if (class & TYPE_NUM
) {
2801 flags
= old
->flags
& ~CEF_CONST_MASK
;
2803 * Casts to numeric types never result in address
2804 * constants [6.6(9)].
2809 * As an extension, treat address constants cast to
2810 * integer type as an arithmetic constant.
2812 if (old
->flags
& CEF_ADDR
)
2816 * Cast to float type -> not an integer constant
2817 * expression [6.6(6)].
2819 if (class & TYPE_FLOAT
)
2820 flags
&= ~CEF_CLR_ICE
;
2822 * Casts of float literals to integer type results in
2823 * a constant integer expression [6.6(6)].
2825 else if (old
->flags
& CEF_FLOAT
)
2826 flags
= CEF_SET_ICE
;
2827 } else if (class & TYPE_PTR
) {
2829 * Casts of integer literals to pointer type yield
2830 * address constants [6.6(9)].
2832 * As an extension, treat address constants cast to a
2833 * different pointer type as address constants again.
2835 * As another extension, treat integer constant
2836 * expressions (in contrast to literals) cast to
2837 * pointer type as address constants.
2839 if (old
->flags
& (CEF_ICE
| CEF_ADDR
))
2846 static struct symbol
*evaluate_cast(struct expression
*expr
)
2848 struct expression
*target
= expr
->cast_expression
;
2849 struct symbol
*ctype
;
2850 struct symbol
*t1
, *t2
;
2852 int as1
= 0, as2
= 0;
2858 * Special case: a cast can be followed by an
2859 * initializer, in which case we need to pass
2860 * the type value down to that initializer rather
2861 * than trying to evaluate it as an expression
2863 * A more complex case is when the initializer is
2864 * dereferenced as part of a post-fix expression.
2865 * We need to produce an expression that can be dereferenced.
2867 if (target
->type
== EXPR_INITIALIZER
) {
2868 struct symbol
*sym
= expr
->cast_type
;
2869 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2871 sym
->initializer
= target
;
2872 evaluate_symbol(sym
);
2874 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2876 if (sym
->ctype
.modifiers
& MOD_TOPLEVEL
)
2877 addr
->flags
|= CEF_ADDR
;
2879 expr
->type
= EXPR_PREOP
;
2887 ctype
= examine_symbol_type(expr
->cast_type
);
2888 expr
->ctype
= ctype
;
2889 expr
->cast_type
= ctype
;
2891 evaluate_expression(target
);
2894 class1
= classify_type(ctype
, &t1
);
2896 expr
->flags
= cast_flags(expr
, target
);
2899 * You can always throw a value away by casting to
2900 * "void" - that's an implicit "force". Note that
2901 * the same is _not_ true of "void *".
2903 if (t1
== &void_ctype
)
2906 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2907 warning(expr
->pos
, "cast to non-scalar");
2911 expression_error(expr
, "cast from unknown type");
2914 class2
= classify_type(t2
, &t2
);
2916 if (class2
& TYPE_COMPOUND
)
2917 warning(expr
->pos
, "cast from non-scalar");
2919 if (expr
->type
== EXPR_FORCE_CAST
)
2922 /* allowed cast unfouls */
2923 if (class2
& TYPE_FOULED
)
2927 if ((class1
& TYPE_RESTRICT
) && restricted_value(target
, t1
))
2928 warning(expr
->pos
, "cast to %s",
2930 if (class2
& TYPE_RESTRICT
) {
2931 if (t1
== &bool_ctype
) {
2932 if (class2
& TYPE_FOULED
)
2933 warning(expr
->pos
, "%s degrades to integer",
2936 warning(expr
->pos
, "cast from %s",
2942 if (t1
== &ulong_ctype
)
2944 else if (class1
== TYPE_PTR
) {
2945 examine_pointer_target(t1
);
2949 if (t2
== &ulong_ctype
)
2951 else if (class2
== TYPE_PTR
) {
2952 examine_pointer_target(t2
);
2956 if (!as1
&& as2
> 0)
2957 warning(expr
->pos
, "cast removes address space of expression");
2958 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2959 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2960 if (as1
> 0 && !as2
&&
2961 !is_null_pointer_constant(target
) && Wcast_to_as
)
2963 "cast adds address space to expression (<asn:%d>)", as1
);
2965 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2966 !as1
&& (target
->flags
& CEF_ICE
)) {
2967 if (t1
->ctype
.base_type
== &void_ctype
) {
2968 if (is_zero_constant(target
)) {
2970 expr
->type
= EXPR_VALUE
;
2971 expr
->ctype
= &null_ctype
;
2978 if (t1
== &bool_ctype
)
2986 * Evaluate a call expression with a symbol. This
2987 * should expand inline functions, and evaluate
2990 static int evaluate_symbol_call(struct expression
*expr
)
2992 struct expression
*fn
= expr
->fn
;
2993 struct symbol
*ctype
= fn
->ctype
;
2995 if (fn
->type
!= EXPR_PREOP
)
2998 if (ctype
->op
&& ctype
->op
->evaluate
)
2999 return ctype
->op
->evaluate(expr
);
3001 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
3003 struct symbol
*curr
= current_fn
;
3005 if (ctype
->definition
)
3006 ctype
= ctype
->definition
;
3008 current_fn
= ctype
->ctype
.base_type
;
3010 ret
= inline_function(expr
, ctype
);
3012 /* restore the old function */
3020 static struct symbol
*evaluate_call(struct expression
*expr
)
3023 struct symbol
*ctype
, *sym
;
3024 struct expression
*fn
= expr
->fn
;
3025 struct expression_list
*arglist
= expr
->args
;
3027 if (!evaluate_expression(fn
))
3029 sym
= ctype
= fn
->ctype
;
3030 if (ctype
->type
== SYM_NODE
)
3031 ctype
= ctype
->ctype
.base_type
;
3032 if (ctype
->type
== SYM_PTR
)
3033 ctype
= get_base_type(ctype
);
3035 if (ctype
->type
!= SYM_FN
) {
3036 struct expression
*arg
;
3037 expression_error(expr
, "not a function %s",
3038 show_ident(sym
->ident
));
3039 /* do typechecking in arguments */
3040 FOR_EACH_PTR (arglist
, arg
) {
3041 evaluate_expression(arg
);
3042 } END_FOR_EACH_PTR(arg
);
3046 examine_fn_arguments(ctype
);
3047 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
3048 sym
->op
&& sym
->op
->args
) {
3049 if (!sym
->op
->args(expr
))
3052 if (!evaluate_arguments(ctype
, arglist
))
3054 args
= expression_list_size(expr
->args
);
3055 fnargs
= symbol_list_size(ctype
->arguments
);
3056 if (args
< fnargs
) {
3057 expression_error(expr
,
3058 "not enough arguments for function %s",
3059 show_ident(sym
->ident
));
3062 if (args
> fnargs
&& !ctype
->variadic
)
3063 expression_error(expr
,
3064 "too many arguments for function %s",
3065 show_ident(sym
->ident
));
3067 expr
->ctype
= ctype
->ctype
.base_type
;
3068 if (sym
->type
== SYM_NODE
) {
3069 if (evaluate_symbol_call(expr
))
3075 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
3077 struct expression
*e
= expr
->down
;
3078 struct symbol
*ctype
= expr
->in
;
3081 if (expr
->op
== '.') {
3082 struct symbol
*field
;
3085 expression_error(expr
, "expected structure or union");
3088 examine_symbol_type(ctype
);
3089 class = classify_type(ctype
, &ctype
);
3090 if (class != TYPE_COMPOUND
) {
3091 expression_error(expr
, "expected structure or union");
3095 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
3097 expression_error(expr
, "unknown member");
3101 expr
->type
= EXPR_VALUE
;
3102 expr
->flags
= CEF_SET_ICE
;
3103 expr
->value
= offset
;
3105 expr
->ctype
= size_t_ctype
;
3108 expression_error(expr
, "expected structure or union");
3111 examine_symbol_type(ctype
);
3112 class = classify_type(ctype
, &ctype
);
3113 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
3114 expression_error(expr
, "expected array");
3117 ctype
= ctype
->ctype
.base_type
;
3119 expr
->type
= EXPR_VALUE
;
3120 expr
->flags
= CEF_SET_ICE
;
3123 expr
->ctype
= size_t_ctype
;
3125 struct expression
*idx
= expr
->index
, *m
;
3126 struct symbol
*i_type
= evaluate_expression(idx
);
3127 unsigned old_idx_flags
;
3128 int i_class
= classify_type(i_type
, &i_type
);
3130 if (!is_int(i_class
)) {
3131 expression_error(expr
, "non-integer index");
3134 unrestrict(idx
, i_class
, &i_type
);
3135 old_idx_flags
= idx
->flags
;
3136 idx
= cast_to(idx
, size_t_ctype
);
3137 idx
->flags
= old_idx_flags
;
3138 m
= alloc_const_expression(expr
->pos
,
3139 bits_to_bytes(ctype
->bit_size
));
3140 m
->ctype
= size_t_ctype
;
3141 m
->flags
= CEF_SET_INT
;
3142 expr
->type
= EXPR_BINOP
;
3146 expr
->ctype
= size_t_ctype
;
3147 expr
->flags
= m
->flags
& idx
->flags
& ~CEF_CONST_MASK
;
3151 struct expression
*copy
= __alloc_expression(0);
3153 if (e
->type
== EXPR_OFFSETOF
)
3155 if (!evaluate_expression(e
))
3157 expr
->type
= EXPR_BINOP
;
3158 expr
->flags
= e
->flags
& copy
->flags
& ~CEF_CONST_MASK
;
3160 expr
->ctype
= size_t_ctype
;
3164 return size_t_ctype
;
3167 struct symbol
*evaluate_expression(struct expression
*expr
)
3174 switch (expr
->type
) {
3177 expression_error(expr
, "value expression without a type");
3180 return evaluate_string(expr
);
3182 return evaluate_symbol_expression(expr
);
3184 if (!evaluate_expression(expr
->left
))
3186 if (!evaluate_expression(expr
->right
))
3188 return evaluate_binop(expr
);
3190 return evaluate_logical(expr
);
3192 evaluate_expression(expr
->left
);
3193 if (!evaluate_expression(expr
->right
))
3195 return evaluate_comma(expr
);
3197 if (!evaluate_expression(expr
->left
))
3199 if (!evaluate_expression(expr
->right
))
3201 return evaluate_compare(expr
);
3202 case EXPR_ASSIGNMENT
:
3203 if (!evaluate_expression(expr
->left
))
3205 if (!evaluate_expression(expr
->right
))
3207 return evaluate_assignment(expr
);
3209 if (!evaluate_expression(expr
->unop
))
3211 return evaluate_preop(expr
);
3213 if (!evaluate_expression(expr
->unop
))
3215 return evaluate_postop(expr
);
3217 case EXPR_FORCE_CAST
:
3218 case EXPR_IMPLIED_CAST
:
3219 return evaluate_cast(expr
);
3221 return evaluate_sizeof(expr
);
3222 case EXPR_PTRSIZEOF
:
3223 return evaluate_ptrsizeof(expr
);
3225 return evaluate_alignof(expr
);
3227 return evaluate_member_dereference(expr
);
3229 return evaluate_call(expr
);
3231 case EXPR_CONDITIONAL
:
3232 return evaluate_conditional_expression(expr
);
3233 case EXPR_STATEMENT
:
3234 expr
->ctype
= evaluate_statement(expr
->statement
);
3238 expr
->ctype
= &ptr_ctype
;
3242 /* Evaluate the type of the symbol .. */
3243 evaluate_symbol(expr
->symbol
);
3244 /* .. but the type of the _expression_ is a "type" */
3245 expr
->ctype
= &type_ctype
;
3249 return evaluate_offsetof(expr
);
3251 /* These can not exist as stand-alone expressions */
3252 case EXPR_INITIALIZER
:
3253 case EXPR_IDENTIFIER
:
3256 expression_error(expr
, "internal front-end error: initializer in expression");
3259 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3265 static void check_duplicates(struct symbol
*sym
)
3268 struct symbol
*next
= sym
;
3269 int initialized
= sym
->initializer
!= NULL
;
3271 while ((next
= next
->same_symbol
) != NULL
) {
3272 const char *typediff
;
3273 evaluate_symbol(next
);
3274 if (initialized
&& next
->initializer
) {
3275 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3276 show_ident(sym
->ident
),
3277 stream_name(next
->pos
.stream
), next
->pos
.line
);
3278 /* Only warn once */
3282 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3284 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3285 show_ident(sym
->ident
),
3286 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3291 unsigned long mod
= sym
->ctype
.modifiers
;
3292 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3294 if (!(mod
& MOD_TOPLEVEL
))
3298 if (sym
->ident
== &main_ident
)
3300 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3304 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3306 struct symbol
*base_type
;
3314 sym
= examine_symbol_type(sym
);
3315 base_type
= get_base_type(sym
);
3319 /* Evaluate the initializers */
3320 if (sym
->initializer
)
3321 evaluate_initializer(sym
, &sym
->initializer
);
3323 /* And finally, evaluate the body of the symbol too */
3324 if (base_type
->type
== SYM_FN
) {
3325 struct symbol
*curr
= current_fn
;
3327 if (sym
->definition
&& sym
->definition
!= sym
)
3328 return evaluate_symbol(sym
->definition
);
3330 current_fn
= base_type
;
3332 examine_fn_arguments(base_type
);
3333 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3335 if (base_type
->stmt
)
3336 evaluate_statement(base_type
->stmt
);
3344 void evaluate_symbol_list(struct symbol_list
*list
)
3348 FOR_EACH_PTR(list
, sym
) {
3349 has_error
&= ~ERROR_CURR_PHASE
;
3350 evaluate_symbol(sym
);
3351 check_duplicates(sym
);
3352 } END_FOR_EACH_PTR(sym
);
3355 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3357 struct expression
*expr
= stmt
->expression
;
3358 struct symbol
*fntype
;
3360 evaluate_expression(expr
);
3361 fntype
= current_fn
->ctype
.base_type
;
3362 if (!fntype
|| fntype
== &void_ctype
) {
3363 if (expr
&& expr
->ctype
!= &void_ctype
)
3364 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3365 if (expr
&& Wreturn_void
)
3366 warning(stmt
->pos
, "returning void-valued expression");
3371 sparse_error(stmt
->pos
, "return with no return value");
3376 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3380 static void evaluate_if_statement(struct statement
*stmt
)
3382 if (!stmt
->if_conditional
)
3385 evaluate_conditional(stmt
->if_conditional
, 0);
3386 evaluate_statement(stmt
->if_true
);
3387 evaluate_statement(stmt
->if_false
);
3390 static void evaluate_iterator(struct statement
*stmt
)
3392 evaluate_symbol_list(stmt
->iterator_syms
);
3393 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3394 evaluate_conditional(stmt
->iterator_post_condition
,1);
3395 evaluate_statement(stmt
->iterator_pre_statement
);
3396 evaluate_statement(stmt
->iterator_statement
);
3397 evaluate_statement(stmt
->iterator_post_statement
);
3400 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3402 switch (*constraint
) {
3403 case '=': /* Assignment */
3404 case '+': /* Update */
3407 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3411 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3413 switch (*constraint
) {
3414 case '=': /* Assignment */
3415 case '+': /* Update */
3416 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3420 static void evaluate_asm_statement(struct statement
*stmt
)
3422 struct expression
*expr
;
3426 expr
= stmt
->asm_string
;
3427 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3428 sparse_error(stmt
->pos
, "need constant string for inline asm");
3433 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3435 case 0: /* Identifier */
3439 case 1: /* Constraint */
3441 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3442 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3443 *THIS_ADDRESS(expr
) = NULL
;
3446 verify_output_constraint(expr
, expr
->string
->data
);
3449 case 2: /* Expression */
3451 if (!evaluate_expression(expr
))
3453 if (!lvalue_expression(expr
))
3454 warning(expr
->pos
, "asm output is not an lvalue");
3455 evaluate_assign_to(expr
, expr
->ctype
);
3458 } END_FOR_EACH_PTR(expr
);
3461 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3463 case 0: /* Identifier */
3467 case 1: /* Constraint */
3469 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3470 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3471 *THIS_ADDRESS(expr
) = NULL
;
3474 verify_input_constraint(expr
, expr
->string
->data
);
3477 case 2: /* Expression */
3479 if (!evaluate_expression(expr
))
3483 } END_FOR_EACH_PTR(expr
);
3485 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3487 sparse_error(stmt
->pos
, "bad asm clobbers");
3490 if (expr
->type
== EXPR_STRING
)
3492 expression_error(expr
, "asm clobber is not a string");
3493 } END_FOR_EACH_PTR(expr
);
3495 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3496 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3497 sparse_error(stmt
->pos
, "bad asm label");
3500 } END_FOR_EACH_PTR(sym
);
3503 static void evaluate_case_statement(struct statement
*stmt
)
3505 evaluate_expression(stmt
->case_expression
);
3506 evaluate_expression(stmt
->case_to
);
3507 evaluate_statement(stmt
->case_statement
);
3510 static void check_case_type(struct expression
*switch_expr
,
3511 struct expression
*case_expr
,
3512 struct expression
**enumcase
)
3514 struct symbol
*switch_type
, *case_type
;
3520 switch_type
= switch_expr
->ctype
;
3521 case_type
= evaluate_expression(case_expr
);
3523 if (!switch_type
|| !case_type
)
3527 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3528 else if (is_enum_type(case_type
))
3529 *enumcase
= case_expr
;
3532 sclass
= classify_type(switch_type
, &switch_type
);
3533 cclass
= classify_type(case_type
, &case_type
);
3535 /* both should be arithmetic */
3536 if (!(sclass
& cclass
& TYPE_NUM
))
3539 /* neither should be floating */
3540 if ((sclass
| cclass
) & TYPE_FLOAT
)
3543 /* if neither is restricted, we are OK */
3544 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3547 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3548 cclass
, sclass
, case_type
, switch_type
)) {
3549 unrestrict(case_expr
, cclass
, &case_type
);
3550 unrestrict(switch_expr
, sclass
, &switch_type
);
3555 expression_error(case_expr
, "incompatible types for 'case' statement");
3558 static void evaluate_switch_statement(struct statement
*stmt
)
3561 struct expression
*enumcase
= NULL
;
3562 struct expression
**enumcase_holder
= &enumcase
;
3563 struct expression
*sel
= stmt
->switch_expression
;
3565 evaluate_expression(sel
);
3566 evaluate_statement(stmt
->switch_statement
);
3569 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3570 enumcase_holder
= NULL
; /* Only check cases against switch */
3572 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3573 struct statement
*case_stmt
= sym
->stmt
;
3574 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3575 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3576 } END_FOR_EACH_PTR(sym
);
3579 static void evaluate_goto_statement(struct statement
*stmt
)
3581 struct symbol
*label
= stmt
->goto_label
;
3583 if (label
&& !label
->stmt
&& !lookup_keyword(label
->ident
, NS_KEYWORD
))
3584 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3586 evaluate_expression(stmt
->goto_expression
);
3589 struct symbol
*evaluate_statement(struct statement
*stmt
)
3594 switch (stmt
->type
) {
3595 case STMT_DECLARATION
: {
3597 FOR_EACH_PTR(stmt
->declaration
, s
) {
3599 } END_FOR_EACH_PTR(s
);
3604 return evaluate_return_expression(stmt
);
3606 case STMT_EXPRESSION
:
3607 if (!evaluate_expression(stmt
->expression
))
3609 if (stmt
->expression
->ctype
== &null_ctype
)
3610 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3611 return degenerate(stmt
->expression
);
3613 case STMT_COMPOUND
: {
3614 struct statement
*s
;
3615 struct symbol
*type
= NULL
;
3617 /* Evaluate the return symbol in the compound statement */
3618 evaluate_symbol(stmt
->ret
);
3621 * Then, evaluate each statement, making the type of the
3622 * compound statement be the type of the last statement
3624 type
= evaluate_statement(stmt
->args
);
3625 FOR_EACH_PTR(stmt
->stmts
, s
) {
3626 type
= evaluate_statement(s
);
3627 } END_FOR_EACH_PTR(s
);
3633 evaluate_if_statement(stmt
);
3636 evaluate_iterator(stmt
);
3639 evaluate_switch_statement(stmt
);
3642 evaluate_case_statement(stmt
);
3645 return evaluate_statement(stmt
->label_statement
);
3647 evaluate_goto_statement(stmt
);
3652 evaluate_asm_statement(stmt
);
3655 evaluate_expression(stmt
->expression
);
3658 evaluate_expression(stmt
->range_expression
);
3659 evaluate_expression(stmt
->range_low
);
3660 evaluate_expression(stmt
->range_high
);