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.
44 #include "expression.h"
46 struct symbol
*current_fn
;
48 struct ident bad_address_space
= { .len
= 6, .name
= "bad AS", };
50 static struct symbol
*degenerate(struct expression
*expr
);
51 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
53 static inline int valid_expr_type(struct expression
*expr
)
55 return expr
&& valid_type(expr
->ctype
);
58 static inline int valid_subexpr_type(struct expression
*expr
)
60 return valid_expr_type(expr
->left
)
61 && valid_expr_type(expr
->right
);
64 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
66 struct expression
*addr
;
67 struct symbol
*sym
= expr
->symbol
;
68 struct symbol
*base_type
;
71 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
75 examine_symbol_type(sym
);
77 base_type
= get_base_type(sym
);
79 expression_error(expr
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
83 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
85 addr
->symbol_name
= expr
->symbol_name
;
86 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
87 addr
->flags
= expr
->flags
;
88 expr
->type
= EXPR_PREOP
;
91 expr
->flags
= CEF_NONE
;
93 /* The type of a symbol is the symbol itself! */
98 static struct symbol
*evaluate_string(struct expression
*expr
)
100 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
101 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
102 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
103 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
104 unsigned int length
= expr
->string
->length
;
106 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
107 sym
->bit_size
= bytes_to_bits(length
);
108 sym
->ctype
.alignment
= 1;
110 sym
->ctype
.modifiers
= MOD_STATIC
;
111 sym
->ctype
.base_type
= array
;
112 sym
->initializer
= initstr
;
114 initstr
->ctype
= sym
;
115 initstr
->string
= expr
->string
;
117 array
->array_size
= sym
->array_size
;
118 array
->bit_size
= bytes_to_bits(length
);
119 array
->ctype
.alignment
= 1;
120 array
->ctype
.modifiers
= MOD_STATIC
;
121 array
->ctype
.base_type
= &char_ctype
;
124 addr
->ctype
= &lazy_ptr_ctype
;
125 addr
->flags
= CEF_ADDR
;
127 expr
->type
= EXPR_PREOP
;
134 /* type has come from classify_type and is an integer type */
135 static inline struct symbol
*integer_promotion(struct symbol
*type
)
137 unsigned long mod
= type
->ctype
.modifiers
;
138 int width
= type
->bit_size
;
141 * Bitfields always promote to the base type,
142 * even if the bitfield might be bigger than
145 if (type
->type
== SYM_BITFIELD
) {
146 type
= type
->ctype
.base_type
;
148 mod
= type
->ctype
.modifiers
;
149 if (width
< bits_in_int
)
152 /* If char/short has as many bits as int, it still gets "promoted" */
153 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
154 if (mod
& MOD_UNSIGNED
)
162 * integer part of usual arithmetic conversions:
163 * integer promotions are applied
164 * if left and right are identical, we are done
165 * if signedness is the same, convert one with lower rank
166 * unless unsigned argument has rank lower than signed one, convert the
168 * if signed argument is bigger than unsigned one, convert the unsigned.
169 * otherwise, convert signed.
171 * Leaving aside the integer promotions, that is equivalent to
172 * if identical, don't convert
173 * if left is bigger than right, convert right
174 * if right is bigger than left, convert right
175 * otherwise, if signedness is the same, convert one with lower rank
176 * otherwise convert the signed one.
178 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
180 unsigned long lmod
, rmod
;
182 left
= integer_promotion(left
);
183 right
= integer_promotion(right
);
188 if (left
->bit_size
> right
->bit_size
)
191 if (right
->bit_size
> left
->bit_size
)
194 lmod
= left
->ctype
.modifiers
;
195 rmod
= right
->ctype
.modifiers
;
196 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
197 if (lmod
& MOD_UNSIGNED
)
199 } else if ((lmod
& ~rmod
) & (MOD_LONG_ALL
))
207 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
209 return orig
->bit_size
== new->bit_size
&&
210 orig
->bit_offset
== new->bit_offset
;
213 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, struct ident
**asp
)
215 unsigned long mod
= 0;
216 struct ident
*as
= NULL
;
219 mod
|= node
->ctype
.modifiers
;
220 combine_address_space(node
->pos
, &as
, node
->ctype
.as
);
221 if (node
->type
== SYM_NODE
) {
222 node
= node
->ctype
.base_type
;
227 *modp
= mod
& ~MOD_IGNORE
;
232 static int is_same_type(struct expression
*expr
, struct symbol
*new)
234 struct symbol
*old
= expr
->ctype
;
235 unsigned long oldmod
, newmod
;
236 struct ident
*oldas
, *newas
;
238 old
= base_type(old
, &oldmod
, &oldas
);
239 new = base_type(new, &newmod
, &newas
);
241 /* Same base type, same address space? */
242 if (old
== new && oldas
== newas
) {
243 unsigned long difmod
;
245 /* Check the modifier bits. */
246 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
248 /* Exact same type? */
253 * Not the same type, but differs only in "const".
254 * Don't warn about MOD_NOCAST.
256 if (difmod
== MOD_CONST
)
259 if ((oldmod
| newmod
) & MOD_NOCAST
) {
260 const char *tofrom
= "to/from";
261 if (!(newmod
& MOD_NOCAST
))
263 if (!(oldmod
& MOD_NOCAST
))
265 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
271 warn_for_different_enum_types (struct position pos
,
272 struct symbol
*typea
,
273 struct symbol
*typeb
)
277 if (typea
->type
== SYM_NODE
)
278 typea
= typea
->ctype
.base_type
;
279 if (typeb
->type
== SYM_NODE
)
280 typeb
= typeb
->ctype
.base_type
;
285 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
286 warning(pos
, "mixing different enum types");
287 info(pos
, " %s versus", show_typename(typea
));
288 info(pos
, " %s", show_typename(typeb
));
292 static int cast_flags(struct expression
*expr
, struct expression
*target
);
293 static struct symbol
*cast_to_bool(struct expression
*expr
);
296 * This gets called for implicit casts in assignments and
297 * integer promotion. We often want to try to move the
298 * cast down, because the ops involved may have been
299 * implicitly cast up, and we can get rid of the casts
302 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
304 struct expression
*expr
;
306 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
308 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
312 * See if we can simplify the op. Move the cast down.
316 if (old
->ctype
->bit_size
< type
->bit_size
)
318 if (old
->op
== '~') {
320 old
->unop
= cast_to(old
->unop
, type
);
325 case EXPR_IMPLIED_CAST
:
326 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
328 if (old
->ctype
->bit_size
>= type
->bit_size
) {
329 struct expression
*orig
= old
->cast_expression
;
330 if (same_cast_type(orig
->ctype
, type
))
332 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
334 old
->cast_type
= type
;
344 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
346 expr
->cast_type
= type
;
347 expr
->cast_expression
= old
;
348 expr
->flags
= cast_flags(expr
, old
);
350 if (is_bool_type(type
))
367 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
369 static int type_class
[SYM_BAD
+ 1] = {
370 [SYM_PTR
] = TYPE_PTR
,
371 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
372 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
373 [SYM_STRUCT
] = TYPE_COMPOUND
,
374 [SYM_UNION
] = TYPE_COMPOUND
,
375 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
376 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
377 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
379 if (type
->type
== SYM_NODE
)
380 type
= type
->ctype
.base_type
;
381 if (type
->type
== SYM_TYPEOF
) {
382 type
= evaluate_expression(type
->initializer
);
385 else if (type
->type
== SYM_NODE
)
386 type
= type
->ctype
.base_type
;
388 if (type
->type
== SYM_ENUM
)
389 type
= type
->ctype
.base_type
;
391 if (type
->type
== SYM_BASETYPE
) {
392 if (type
->ctype
.base_type
== &int_type
)
394 if (type
->ctype
.base_type
== &fp_type
)
395 return TYPE_NUM
| TYPE_FLOAT
;
397 return type_class
[type
->type
];
400 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
402 static inline int is_string_type(struct symbol
*type
)
404 if (type
->type
== SYM_NODE
)
405 type
= type
->ctype
.base_type
;
406 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
409 static struct symbol
*bad_expr_type(struct expression
*expr
)
411 switch (expr
->type
) {
414 if (!valid_subexpr_type(expr
))
416 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
417 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
418 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
422 if (!valid_expr_type(expr
->unop
))
424 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
425 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
431 expr
->flags
= CEF_NONE
;
432 return expr
->ctype
= &bad_ctype
;
435 static int restricted_value(struct expression
*v
, struct symbol
*type
)
437 if (v
->type
!= EXPR_VALUE
)
444 static int restricted_binop(int op
, struct symbol
*type
)
449 case SPECIAL_AND_ASSIGN
:
450 case SPECIAL_OR_ASSIGN
:
451 case SPECIAL_XOR_ASSIGN
:
452 return 1; /* unfoul */
456 return 2; /* keep fouled */
458 case SPECIAL_NOTEQUAL
:
459 return 3; /* warn if fouled */
465 static int restricted_unop(int op
, struct symbol
**type
)
468 if ((*type
)->bit_size
< bits_in_int
)
469 *type
= befoul(*type
);
476 /* type should be SYM_FOULED */
477 static inline struct symbol
*unfoul(struct symbol
*type
)
479 return type
->ctype
.base_type
;
482 static struct symbol
*restricted_binop_type(int op
,
483 struct expression
*left
,
484 struct expression
*right
,
485 int lclass
, int rclass
,
486 struct symbol
*ltype
,
487 struct symbol
*rtype
)
489 struct symbol
*ctype
= NULL
;
490 if (lclass
& TYPE_RESTRICT
) {
491 if (rclass
& TYPE_RESTRICT
) {
492 if (ltype
== rtype
) {
494 } else if (lclass
& TYPE_FOULED
) {
495 if (unfoul(ltype
) == rtype
)
497 } else if (rclass
& TYPE_FOULED
) {
498 if (unfoul(rtype
) == ltype
)
502 if (!restricted_value(right
, ltype
))
505 } else if (!restricted_value(left
, rtype
))
509 switch (restricted_binop(op
, ctype
)) {
511 if ((lclass
^ rclass
) & TYPE_FOULED
)
512 ctype
= unfoul(ctype
);
515 if (!(lclass
& rclass
& TYPE_FOULED
))
527 static inline void unrestrict(struct expression
*expr
,
528 int class, struct symbol
**ctype
)
530 if (class & TYPE_RESTRICT
) {
531 if (class & TYPE_FOULED
)
532 *ctype
= unfoul(*ctype
);
533 warning(expr
->pos
, "%s degrades to integer",
534 show_typename(*ctype
));
535 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
539 static struct symbol
*usual_conversions(int op
,
540 struct expression
*left
,
541 struct expression
*right
,
542 int lclass
, int rclass
,
543 struct symbol
*ltype
,
544 struct symbol
*rtype
)
546 struct symbol
*ctype
;
548 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
550 if ((lclass
| rclass
) & TYPE_RESTRICT
)
554 if (!(lclass
& TYPE_FLOAT
)) {
555 if (!(rclass
& TYPE_FLOAT
))
556 return bigger_int_type(ltype
, rtype
);
559 } else if (rclass
& TYPE_FLOAT
) {
560 unsigned long lmod
= ltype
->ctype
.modifiers
;
561 unsigned long rmod
= rtype
->ctype
.modifiers
;
562 if (rmod
& ~lmod
& (MOD_LONG_ALL
))
570 ctype
= restricted_binop_type(op
, left
, right
,
571 lclass
, rclass
, ltype
, rtype
);
575 unrestrict(left
, lclass
, <ype
);
576 unrestrict(right
, rclass
, &rtype
);
581 static inline int lvalue_expression(struct expression
*expr
)
583 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
586 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
588 struct expression
*index
= expr
->right
;
589 struct symbol
*ctype
, *base
;
592 classify_type(degenerate(expr
->left
), &ctype
);
593 base
= examine_pointer_target(ctype
);
596 * An address constant +/- an integer constant expression
597 * yields an address constant again [6.6(7)].
599 if ((expr
->left
->flags
& CEF_ADDR
) && (expr
->right
->flags
& CEF_ICE
))
600 expr
->flags
= CEF_ADDR
;
603 expression_error(expr
, "missing type information");
606 if (is_function(base
)) {
607 expression_error(expr
, "arithmetics on pointers to functions");
611 /* Get the size of whatever the pointer points to */
612 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
614 if (ctype
== &null_ctype
)
618 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
621 if (index
->type
== EXPR_VALUE
) {
622 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
623 unsigned long long v
= index
->value
, mask
;
624 mask
= 1ULL << (itype
->bit_size
- 1);
630 mask
= 1ULL << (bits_in_pointer
- 1);
631 v
&= mask
| (mask
- 1);
633 val
->ctype
= ssize_t_ctype
;
638 if (itype
->bit_size
< bits_in_pointer
)
639 index
= cast_to(index
, ssize_t_ctype
);
642 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
643 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
645 val
->ctype
= ssize_t_ctype
;
646 val
->value
= multiply
;
649 mul
->ctype
= ssize_t_ctype
;
659 static void examine_fn_arguments(struct symbol
*fn
);
661 #define MOD_IGN (MOD_QUALIFIER | MOD_PURE)
663 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
664 unsigned long mod1
, unsigned long mod2
)
666 struct ident
*as1
= c1
->as
, *as2
= c2
->as
;
667 struct symbol
*t1
= c1
->base_type
;
668 struct symbol
*t2
= c2
->base_type
;
669 int move1
= 1, move2
= 1;
670 mod1
|= c1
->modifiers
;
671 mod2
|= c2
->modifiers
;
675 struct symbol
*base1
= t1
->ctype
.base_type
;
676 struct symbol
*base2
= t2
->ctype
.base_type
;
679 * FIXME! Collect alignment and context too here!
682 if (t1
&& t1
->type
!= SYM_PTR
) {
683 mod1
|= t1
->ctype
.modifiers
;
684 combine_address_space(t1
->pos
, &as1
, t1
->ctype
.as
);
690 if (t2
&& t2
->type
!= SYM_PTR
) {
691 mod2
|= t2
->ctype
.modifiers
;
692 combine_address_space(t2
->pos
, &as2
, t2
->ctype
.as
);
700 return "different types";
702 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
710 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
720 if (type
!= t2
->type
)
721 return "different base types";
725 sparse_error(t1
->pos
,
726 "internal error: bad type in derived(%d)",
730 return "different base types";
733 /* allow definition of incomplete structs and unions */
734 if (t1
->ident
== t2
->ident
)
736 return "different base types";
738 /* XXX: we ought to compare sizes */
742 return "different address spaces";
743 /* MOD_SPECIFIER is due to idiocy in parse.c */
744 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
745 return "different modifiers";
746 /* we could be lazier here */
747 base1
= examine_pointer_target(t1
);
748 base2
= examine_pointer_target(t2
);
749 mod1
= t1
->ctype
.modifiers
;
751 mod2
= t2
->ctype
.modifiers
;
755 struct symbol
*arg1
, *arg2
;
759 return "different address spaces";
760 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
761 return "different modifiers";
762 mod1
= t1
->ctype
.modifiers
;
764 mod2
= t2
->ctype
.modifiers
;
767 if (t1
->variadic
!= t2
->variadic
)
768 return "incompatible variadic arguments";
769 examine_fn_arguments(t1
);
770 examine_fn_arguments(t2
);
771 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
772 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
779 return "different argument counts";
780 diffstr
= type_difference(&arg1
->ctype
,
784 static char argdiff
[80];
785 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
792 FINISH_PTR_LIST(arg2
);
793 FINISH_PTR_LIST(arg1
);
798 return "different address spaces";
800 return "different base types";
801 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
805 return "different type sizes";
806 else if (diff
& ~MOD_SIGNEDNESS
)
807 return "different modifiers";
809 return "different signedness";
815 return "different address spaces";
816 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
817 return "different modifiers";
821 static void bad_null(struct expression
*expr
)
823 if (Wnon_pointer_null
)
824 warning(expr
->pos
, "Using plain integer as NULL pointer");
827 static unsigned long target_qualifiers(struct symbol
*type
)
829 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
830 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
835 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
837 const char *typediff
;
838 struct symbol
*ltype
, *rtype
;
839 struct expression
*l
= expr
->left
;
840 struct expression
*r
= expr
->right
;
841 struct symbol
*lbase
;
843 classify_type(degenerate(l
), <ype
);
844 classify_type(degenerate(r
), &rtype
);
846 lbase
= examine_pointer_target(ltype
);
847 examine_pointer_target(rtype
);
848 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
849 target_qualifiers(rtype
),
850 target_qualifiers(ltype
));
852 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
854 if (is_function(lbase
)) {
855 expression_error(expr
, "subtraction of functions? Share your drugs");
859 expr
->ctype
= ssize_t_ctype
;
860 if (lbase
->bit_size
> bits_in_char
) {
861 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
862 struct expression
*div
= expr
;
863 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
864 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
866 val
->ctype
= size_t_ctype
;
869 if (value
& (value
-1)) {
870 if (Wptr_subtraction_blows
) {
871 warning(expr
->pos
, "potentially expensive pointer subtraction");
872 info(expr
->pos
, " '%s' has a non-power-of-2 size: %lu", show_typename(lbase
), value
);
877 sub
->ctype
= ssize_t_ctype
;
886 return ssize_t_ctype
;
889 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
891 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
893 struct symbol
*ctype
;
898 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
899 warning(expr
->pos
, "assignment expression in conditional");
901 ctype
= evaluate_expression(expr
);
902 if (!valid_type(ctype
))
904 if (is_safe_type(ctype
))
905 warning(expr
->pos
, "testing a 'safe expression'");
906 if (is_func_type(ctype
)) {
908 warning(expr
->pos
, "the address of %s will always evaluate as true", "a function");
909 } else if (is_array_type(ctype
)) {
911 warning(expr
->pos
, "the address of %s will always evaluate as true", "an array");
912 } else if (!is_scalar_type(ctype
)) {
913 sparse_error(expr
->pos
, "incorrect type in conditional (non-scalar type)");
914 info(expr
->pos
, " got %s", show_typename(ctype
));
918 ctype
= degenerate(expr
);
922 static struct symbol
*evaluate_logical(struct expression
*expr
)
924 if (!evaluate_conditional(expr
->left
, 0))
926 if (!evaluate_conditional(expr
->right
, 0))
929 /* the result is int [6.5.13(3), 6.5.14(3)] */
930 expr
->ctype
= &int_ctype
;
931 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
932 expr
->flags
&= ~(CEF_CONST_MASK
| CEF_ADDR
);
936 static struct symbol
*evaluate_binop(struct expression
*expr
)
938 struct symbol
*ltype
, *rtype
, *ctype
;
939 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
940 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
943 /* number op number */
944 if (lclass
& rclass
& TYPE_NUM
) {
945 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
946 expr
->flags
&= ~CEF_CONST_MASK
;
948 if ((lclass
| rclass
) & TYPE_FLOAT
) {
950 case '+': case '-': case '*': case '/':
953 return bad_expr_type(expr
);
957 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
958 // shifts do integer promotions, but that's it.
959 unrestrict(expr
->left
, lclass
, <ype
);
960 unrestrict(expr
->right
, rclass
, &rtype
);
961 ctype
= ltype
= integer_promotion(ltype
);
962 rtype
= integer_promotion(rtype
);
964 // The rest do usual conversions
965 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
966 && expr
->left
->op
== '!';
967 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
968 && expr
->right
->op
== '!';
969 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
970 warning(expr
->pos
, "dubious: %sx %c %sy",
973 right_not
? "!" : "");
975 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
976 lclass
, rclass
, ltype
, rtype
);
977 ctype
= rtype
= ltype
;
980 expr
->left
= cast_to(expr
->left
, ltype
);
981 expr
->right
= cast_to(expr
->right
, rtype
);
986 /* pointer (+|-) integer */
987 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
988 unrestrict(expr
->right
, rclass
, &rtype
);
989 return evaluate_ptr_add(expr
, rtype
);
992 /* integer + pointer */
993 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
994 struct expression
*index
= expr
->left
;
995 unrestrict(index
, lclass
, <ype
);
996 expr
->left
= expr
->right
;
998 return evaluate_ptr_add(expr
, ltype
);
1001 /* pointer - pointer */
1002 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
1003 return evaluate_ptr_sub(expr
);
1005 return bad_expr_type(expr
);
1008 static struct symbol
*evaluate_comma(struct expression
*expr
)
1010 expr
->ctype
= degenerate(expr
->right
);
1011 if (expr
->ctype
== &null_ctype
)
1012 expr
->ctype
= &ptr_ctype
;
1013 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
1017 static int modify_for_unsigned(int op
)
1020 op
= SPECIAL_UNSIGNED_LT
;
1022 op
= SPECIAL_UNSIGNED_GT
;
1023 else if (op
== SPECIAL_LTE
)
1024 op
= SPECIAL_UNSIGNED_LTE
;
1025 else if (op
== SPECIAL_GTE
)
1026 op
= SPECIAL_UNSIGNED_GTE
;
1030 enum null_constant_type
{
1036 static inline int is_null_pointer_constant(struct expression
*e
)
1038 if (e
->ctype
== &null_ctype
)
1040 if (!(e
->flags
& CEF_ICE
))
1042 return is_zero_constant(e
) ? NULL_ZERO
: NON_NULL
;
1045 static struct symbol
*evaluate_compare(struct expression
*expr
)
1047 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1048 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
1049 int lclass
= classify_type(degenerate(left
), <ype
);
1050 int rclass
= classify_type(degenerate(right
), &rtype
);
1051 struct symbol
*ctype
;
1052 const char *typediff
;
1055 if (is_type_type(ltype
) && is_type_type(rtype
)) {
1057 * __builtin_types_compatible_p() yields an integer
1058 * constant expression
1060 expr
->flags
= CEF_SET_ICE
;
1064 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1065 warning(expr
->pos
, "testing a 'safe expression'");
1067 expr
->flags
= left
->flags
& right
->flags
& ~CEF_CONST_MASK
& ~CEF_ADDR
;
1069 /* number on number */
1070 if (lclass
& rclass
& TYPE_NUM
) {
1071 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1072 lclass
, rclass
, ltype
, rtype
);
1073 expr
->left
= cast_to(expr
->left
, ctype
);
1074 expr
->right
= cast_to(expr
->right
, ctype
);
1075 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1076 expr
->op
= modify_for_unsigned(expr
->op
);
1080 /* at least one must be a pointer */
1081 if (!((lclass
| rclass
) & TYPE_PTR
))
1082 return bad_expr_type(expr
);
1084 /* equality comparisons can be with null pointer constants */
1085 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1086 int is_null1
= is_null_pointer_constant(left
);
1087 int is_null2
= is_null_pointer_constant(right
);
1088 if (is_null1
== NULL_ZERO
)
1090 if (is_null2
== NULL_ZERO
)
1092 if (is_null1
&& is_null2
) {
1093 int positive
= expr
->op
== SPECIAL_EQUAL
;
1094 expr
->type
= EXPR_VALUE
;
1095 expr
->value
= positive
;
1098 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1099 left
= cast_to(left
, rtype
);
1102 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1103 right
= cast_to(right
, ltype
);
1107 /* both should be pointers */
1108 if (!(lclass
& rclass
& TYPE_PTR
))
1109 return bad_expr_type(expr
);
1110 expr
->op
= modify_for_unsigned(expr
->op
);
1112 lbase
= examine_pointer_target(ltype
);
1113 rbase
= examine_pointer_target(rtype
);
1115 /* they also have special treatment for pointers to void */
1116 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1117 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1118 if (lbase
== &void_ctype
) {
1119 right
= cast_to(right
, ltype
);
1122 if (rbase
== &void_ctype
) {
1123 left
= cast_to(left
, rtype
);
1129 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1130 target_qualifiers(rtype
),
1131 target_qualifiers(ltype
));
1135 expression_error(expr
, "incompatible types in comparison expression (%s):", typediff
);
1136 info(expr
->pos
, " %s", show_typename(ltype
));
1137 info(expr
->pos
, " %s", show_typename(rtype
));
1141 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1142 expr
->ctype
= &int_ctype
;
1147 * NOTE! The degenerate case of "x ? : y", where we don't
1148 * have a true case, this will possibly promote "x" to the
1149 * same type as "y", and thus _change_ the conditional
1150 * test in the expression. But since promotion is "safe"
1151 * for testing, that's OK.
1153 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1155 struct expression
**cond
;
1156 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1158 const char * typediff
;
1161 if (!evaluate_conditional(expr
->conditional
, 0))
1163 if (!evaluate_expression(expr
->cond_false
))
1166 ctype
= degenerate(expr
->conditional
);
1167 rtype
= degenerate(expr
->cond_false
);
1169 cond
= &expr
->conditional
;
1171 if (expr
->cond_true
) {
1172 if (!evaluate_expression(expr
->cond_true
))
1174 ltype
= degenerate(expr
->cond_true
);
1175 cond
= &expr
->cond_true
;
1178 expr
->flags
= (expr
->conditional
->flags
& (*cond
)->flags
&
1179 expr
->cond_false
->flags
& ~CEF_CONST_MASK
);
1181 * A conditional operator yields a particular constant
1182 * expression type only if all of its three subexpressions are
1183 * of that type [6.6(6), 6.6(8)].
1184 * As an extension, relax this restriction by allowing any
1185 * constant expression type for the condition expression.
1187 * A conditional operator never yields an address constant
1189 * However, as an extension, if the condition is any constant
1190 * expression, and the true and false expressions are both
1191 * address constants, mark the result as an address constant.
1193 if (expr
->conditional
->flags
& (CEF_ACE
| CEF_ADDR
))
1194 expr
->flags
= (*cond
)->flags
& expr
->cond_false
->flags
& ~CEF_CONST_MASK
;
1196 lclass
= classify_type(ltype
, <ype
);
1197 rclass
= classify_type(rtype
, &rtype
);
1198 if (lclass
& rclass
& TYPE_NUM
) {
1199 ctype
= usual_conversions('?', *cond
, expr
->cond_false
,
1200 lclass
, rclass
, ltype
, rtype
);
1201 *cond
= cast_to(*cond
, ctype
);
1202 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1206 if ((lclass
| rclass
) & TYPE_PTR
) {
1207 int is_null1
= is_null_pointer_constant(*cond
);
1208 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1210 if (is_null1
&& is_null2
) {
1211 *cond
= cast_to(*cond
, &ptr_ctype
);
1212 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1216 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1217 if (is_null1
== NULL_ZERO
)
1219 *cond
= cast_to(*cond
, rtype
);
1223 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1224 if (is_null2
== NULL_ZERO
)
1225 bad_null(expr
->cond_false
);
1226 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1230 if (!(lclass
& rclass
& TYPE_PTR
)) {
1231 typediff
= "different types";
1234 /* OK, it's pointer on pointer */
1235 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1236 typediff
= "different address spaces";
1240 /* need to be lazier here */
1241 lbase
= examine_pointer_target(ltype
);
1242 rbase
= examine_pointer_target(rtype
);
1243 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1245 if (lbase
== &void_ctype
) {
1246 /* XXX: pointers to function should warn here */
1251 if (rbase
== &void_ctype
) {
1252 /* XXX: pointers to function should warn here */
1256 /* XXX: that should be pointer to composite */
1258 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1265 /* void on void, struct on same struct, union on same union */
1266 if (ltype
== rtype
) {
1270 typediff
= "different base types";
1273 expression_error(expr
, "incompatible types in conditional expression (%s):", typediff
);
1274 info(expr
->pos
, " %s", show_typename(ltype
));
1275 info(expr
->pos
, " %s", show_typename(rtype
));
1277 * if the condition is constant, the type is in fact known
1278 * so use it, as gcc & clang do.
1280 switch (expr_truth_value(expr
->conditional
)) {
1281 case 1: expr
->ctype
= ltype
;
1283 case 0: expr
->ctype
= rtype
;
1291 expr
->ctype
= ctype
;
1295 if (qual
& ~ctype
->ctype
.modifiers
) {
1296 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1298 sym
->ctype
.modifiers
|= qual
;
1301 *cond
= cast_to(*cond
, ctype
);
1302 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1306 /* FP assignments can not do modulo or bit operations */
1307 static int compatible_float_op(int op
)
1309 return op
== SPECIAL_ADD_ASSIGN
||
1310 op
== SPECIAL_SUB_ASSIGN
||
1311 op
== SPECIAL_MUL_ASSIGN
||
1312 op
== SPECIAL_DIV_ASSIGN
;
1315 static int evaluate_assign_op(struct expression
*expr
)
1317 struct symbol
*target
= expr
->left
->ctype
;
1318 struct symbol
*source
= expr
->right
->ctype
;
1319 struct symbol
*t
, *s
;
1320 int tclass
= classify_type(target
, &t
);
1321 int sclass
= classify_type(source
, &s
);
1324 if (tclass
& sclass
& TYPE_NUM
) {
1325 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1326 expression_error(expr
, "invalid assignment");
1329 if (tclass
& TYPE_RESTRICT
) {
1330 if (!restricted_binop(op
, t
)) {
1331 warning(expr
->pos
, "bad assignment (%s) to %s",
1332 show_special(op
), show_typename(t
));
1333 expr
->right
= cast_to(expr
->right
, target
);
1336 /* allowed assignments unfoul */
1337 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1339 if (!restricted_value(expr
->right
, t
))
1341 } else if (op
== SPECIAL_SHR_ASSIGN
|| op
== SPECIAL_SHL_ASSIGN
) {
1342 // shifts do integer promotions, but that's it.
1343 unrestrict(expr
->right
, sclass
, &s
);
1344 target
= integer_promotion(s
);
1346 } else if (!(sclass
& TYPE_RESTRICT
))
1348 /* source and target would better be identical restricted */
1351 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1352 info(expr
->pos
, " left side has type %s", show_typename(t
));
1353 info(expr
->pos
, " right side has type %s", show_typename(s
));
1354 expr
->right
= cast_to(expr
->right
, target
);
1357 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1358 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1359 unrestrict(expr
->right
, sclass
, &s
);
1360 evaluate_ptr_add(expr
, s
);
1363 expression_error(expr
, "invalid pointer assignment");
1367 expression_error(expr
, "invalid assignment");
1371 target
= usual_conversions(op
, expr
->left
, expr
->right
,
1372 tclass
, sclass
, target
, source
);
1374 expr
->right
= cast_to(expr
->right
, target
);
1378 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1381 return 0; /* yes, 0 - we don't want a cast_to here */
1382 if (t1
== &void_ctype
)
1384 if (t2
== &void_ctype
)
1386 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1388 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1392 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1394 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1399 static int check_assignment_types(struct symbol
*target
, struct expression
**rp
,
1400 const char **typediff
)
1402 struct symbol
*source
= degenerate(*rp
);
1403 struct symbol
*t
, *s
;
1404 int tclass
= classify_type(target
, &t
);
1405 int sclass
= classify_type(source
, &s
);
1407 if (tclass
& sclass
& TYPE_NUM
) {
1408 if (tclass
& TYPE_RESTRICT
) {
1409 /* allowed assignments unfoul */
1410 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1412 if (!restricted_value(*rp
, target
))
1416 } else if (!(sclass
& TYPE_RESTRICT
))
1418 if (t
== &bool_ctype
) {
1419 if (is_fouled_type(s
))
1420 warning((*rp
)->pos
, "%s degrades to integer",
1421 show_typename(s
->ctype
.base_type
));
1424 *typediff
= "different base types";
1428 if (tclass
== TYPE_PTR
) {
1429 unsigned long mod1
, mod2
;
1430 struct symbol
*b1
, *b2
;
1431 // NULL pointer is always OK
1432 int is_null
= is_null_pointer_constant(*rp
);
1434 if (is_null
== NULL_ZERO
)
1438 if (!(sclass
& TYPE_PTR
)) {
1439 *typediff
= "different base types";
1442 b1
= examine_pointer_target(t
);
1443 b2
= examine_pointer_target(s
);
1444 mod1
= target_qualifiers(t
);
1445 mod2
= target_qualifiers(s
);
1446 if (whitelist_pointers(b1
, b2
)) {
1448 * assignments to/from void * are OK, provided that
1449 * we do not remove qualifiers from pointed to [C]
1450 * or mix address spaces [sparse].
1452 if (t
->ctype
.as
!= s
->ctype
.as
) {
1453 *typediff
= "different address spaces";
1457 * If this is a function pointer assignment, it is
1458 * actually fine to assign a pointer to const data to
1459 * it, as a function pointer points to const data
1460 * implicitly, i.e., dereferencing it does not produce
1463 if (b1
->type
== SYM_FN
)
1466 *typediff
= "different modifiers";
1471 /* It's OK if the target is more volatile or const than the source */
1472 *typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1478 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1481 if (tclass
& TYPE_NUM
) {
1482 /* XXX: need to turn into comparison with NULL */
1483 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1485 *typediff
= "different base types";
1488 *typediff
= "invalid types";
1492 *rp
= cast_to(*rp
, target
);
1496 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1497 struct expression
**rp
, const char *where
)
1499 const char *typediff
;
1500 struct symbol
*source
= degenerate(*rp
);
1502 if (!check_assignment_types(target
, rp
, &typediff
)) {
1503 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1504 info(expr
->pos
, " expected %s", show_typename(target
));
1505 info(expr
->pos
, " got %s", show_typename(source
));
1506 *rp
= cast_to(*rp
, target
);
1513 static int compatible_transparent_union(struct symbol
*target
,
1514 struct expression
**rp
)
1516 struct symbol
*t
, *member
;
1517 classify_type(target
, &t
);
1518 if (t
->type
!= SYM_UNION
|| !t
->transparent_union
)
1521 FOR_EACH_PTR(t
->symbol_list
, member
) {
1522 const char *typediff
;
1523 if (check_assignment_types(member
, rp
, &typediff
))
1525 } END_FOR_EACH_PTR(member
);
1530 static int compatible_argument_type(struct expression
*expr
, struct symbol
*target
,
1531 struct expression
**rp
, const char *where
)
1533 if (compatible_transparent_union(target
, rp
))
1536 return compatible_assignment_types(expr
, target
, rp
, where
);
1539 static void mark_assigned(struct expression
*expr
)
1545 switch (expr
->type
) {
1550 if (sym
->type
!= SYM_NODE
)
1552 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1556 mark_assigned(expr
->left
);
1557 mark_assigned(expr
->right
);
1560 case EXPR_FORCE_CAST
:
1561 mark_assigned(expr
->cast_expression
);
1564 mark_assigned(expr
->base
);
1572 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1574 if (type
->ctype
.modifiers
& MOD_CONST
)
1575 expression_error(left
, "assignment to const expression");
1577 /* We know left is an lvalue, so it's a "preop-*" */
1578 mark_assigned(left
->unop
);
1581 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1583 struct expression
*left
= expr
->left
;
1584 struct symbol
*ltype
;
1586 if (!lvalue_expression(left
)) {
1587 expression_error(expr
, "not an lvalue");
1591 ltype
= left
->ctype
;
1593 if (expr
->op
!= '=') {
1594 if (!evaluate_assign_op(expr
))
1597 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, "assignment"))
1601 evaluate_assign_to(left
, ltype
);
1603 expr
->ctype
= ltype
;
1607 static void examine_fn_arguments(struct symbol
*fn
)
1611 FOR_EACH_PTR(fn
->arguments
, s
) {
1612 struct symbol
*arg
= evaluate_symbol(s
);
1613 /* Array/function arguments silently degenerate into pointers */
1619 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1620 if (arg
->type
== SYM_ARRAY
)
1621 ptr
->ctype
= arg
->ctype
;
1623 ptr
->ctype
.base_type
= arg
;
1624 combine_address_space(s
->pos
, &ptr
->ctype
.as
, s
->ctype
.as
);
1625 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1627 s
->ctype
.base_type
= ptr
;
1629 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1632 examine_symbol_type(s
);
1639 } END_FOR_EACH_PTR(s
);
1642 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, struct ident
*as
, int mod
)
1644 /* Take the modifiers of the pointer, and apply them to the member */
1645 mod
|= sym
->ctype
.modifiers
;
1646 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1647 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1649 newsym
->ctype
.as
= as
;
1650 newsym
->ctype
.modifiers
= mod
;
1656 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1658 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1659 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1661 node
->ctype
.base_type
= ptr
;
1662 ptr
->bit_size
= bits_in_pointer
;
1663 ptr
->ctype
.alignment
= pointer_alignment
;
1665 node
->bit_size
= bits_in_pointer
;
1666 node
->ctype
.alignment
= pointer_alignment
;
1669 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1670 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1671 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1673 if (sym
->type
== SYM_NODE
) {
1674 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1675 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1676 sym
= sym
->ctype
.base_type
;
1678 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1679 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1680 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1681 sym
= sym
->ctype
.base_type
;
1683 ptr
->ctype
.base_type
= sym
;
1688 /* Arrays degenerate into pointers on pointer arithmetic */
1689 static struct symbol
*degenerate(struct expression
*expr
)
1691 struct symbol
*ctype
, *base
;
1695 ctype
= expr
->ctype
;
1698 base
= examine_symbol_type(ctype
);
1699 if (ctype
->type
== SYM_NODE
)
1700 base
= ctype
->ctype
.base_type
;
1702 * Arrays degenerate into pointers to the entries, while
1703 * functions degenerate into pointers to themselves.
1704 * If array was part of non-lvalue compound, we create a copy
1705 * of that compound first and then act as if we were dealing with
1706 * the corresponding field in there.
1708 switch (base
->type
) {
1710 if (expr
->type
== EXPR_SLICE
) {
1711 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1712 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1714 a
->ctype
.base_type
= expr
->base
->ctype
;
1715 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1716 a
->array_size
= expr
->base
->ctype
->array_size
;
1718 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1720 e0
->ctype
= &lazy_ptr_ctype
;
1722 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1725 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1727 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1729 e2
->right
= expr
->base
;
1731 e2
->ctype
= expr
->base
->ctype
;
1733 if (expr
->r_bitpos
) {
1734 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1737 e3
->right
= alloc_const_expression(expr
->pos
,
1738 bits_to_bytes(expr
->r_bitpos
));
1739 e3
->ctype
= &lazy_ptr_ctype
;
1744 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1747 e4
->ctype
= &lazy_ptr_ctype
;
1750 expr
->type
= EXPR_PREOP
;
1754 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1755 expression_error(expr
, "strange non-value function or array");
1758 *expr
= *expr
->unop
;
1759 ctype
= create_pointer(expr
, ctype
, 1);
1760 expr
->ctype
= ctype
;
1767 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1769 struct expression
*op
= expr
->unop
;
1770 struct symbol
*ctype
;
1772 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1773 expression_error(expr
, "not addressable");
1779 if (expr
->type
== EXPR_SYMBOL
) {
1780 struct symbol
*sym
= expr
->symbol
;
1781 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1785 * symbol expression evaluation is lazy about the type
1786 * of the sub-expression, so we may have to generate
1787 * the type here if so..
1789 if (expr
->ctype
== &lazy_ptr_ctype
) {
1790 ctype
= create_pointer(expr
, ctype
, 0);
1791 expr
->ctype
= ctype
;
1797 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1799 struct expression
*op
= expr
->unop
;
1800 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1802 /* Simplify: *&(expr) => (expr) */
1803 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1805 expr
->flags
= CEF_NONE
;
1809 examine_symbol_type(ctype
);
1811 /* Dereferencing a node drops all the node information. */
1812 if (ctype
->type
== SYM_NODE
)
1813 ctype
= ctype
->ctype
.base_type
;
1815 target
= ctype
->ctype
.base_type
;
1816 examine_symbol_type(target
);
1818 switch (ctype
->type
) {
1820 expression_error(expr
, "cannot dereference this type");
1826 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1827 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1828 merge_type(node
, ctype
);
1832 if (!lvalue_expression(op
)) {
1833 expression_error(op
, "non-lvalue array??");
1837 /* Do the implied "addressof" on the array */
1841 * When an array is dereferenced, we need to pick
1842 * up the attributes of the original node too..
1844 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1845 merge_type(node
, op
->ctype
);
1846 merge_type(node
, ctype
);
1850 node
->bit_size
= target
->bit_size
;
1851 node
->array_size
= target
->array_size
;
1858 * Unary post-ops: x++ and x--
1860 static struct symbol
*evaluate_postop(struct expression
*expr
)
1862 struct expression
*op
= expr
->unop
;
1863 struct symbol
*ctype
= op
->ctype
;
1864 int class = classify_type(ctype
, &ctype
);
1867 if (!class || class & TYPE_COMPOUND
) {
1868 expression_error(expr
, "need scalar for ++/--");
1871 if (!lvalue_expression(expr
->unop
)) {
1872 expression_error(expr
, "need lvalue expression for ++/--");
1876 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1877 unrestrict(expr
, class, &ctype
);
1879 if (class & TYPE_NUM
) {
1881 } else if (class == TYPE_PTR
) {
1882 struct symbol
*target
= examine_pointer_target(ctype
);
1883 if (!is_function(target
))
1884 multiply
= bits_to_bytes(target
->bit_size
);
1888 evaluate_assign_to(op
, op
->ctype
);
1889 expr
->op_value
= multiply
;
1890 expr
->ctype
= ctype
;
1894 expression_error(expr
, "bad argument type for ++/--");
1898 static struct symbol
*evaluate_sign(struct expression
*expr
)
1900 struct symbol
*ctype
= expr
->unop
->ctype
;
1901 int class = classify_type(ctype
, &ctype
);
1902 unsigned char flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1904 /* should be an arithmetic type */
1905 if (!(class & TYPE_NUM
))
1906 return bad_expr_type(expr
);
1907 if (class & TYPE_RESTRICT
)
1910 if (!(class & TYPE_FLOAT
)) {
1911 ctype
= integer_promotion(ctype
);
1912 expr
->unop
= cast_to(expr
->unop
, ctype
);
1913 } else if (expr
->op
!= '~') {
1914 /* no conversions needed */
1916 return bad_expr_type(expr
);
1918 if (expr
->op
== '+')
1919 *expr
= *expr
->unop
;
1920 expr
->flags
= flags
;
1921 expr
->ctype
= ctype
;
1924 if (restricted_unop(expr
->op
, &ctype
))
1925 unrestrict(expr
, class, &ctype
);
1929 static struct symbol
*evaluate_preop(struct expression
*expr
)
1931 struct symbol
*ctype
= expr
->unop
->ctype
;
1935 *expr
= *expr
->unop
;
1941 return evaluate_sign(expr
);
1944 return evaluate_dereference(expr
);
1947 return evaluate_addressof(expr
);
1949 case SPECIAL_INCREMENT
:
1950 case SPECIAL_DECREMENT
:
1952 * From a type evaluation standpoint the preops are
1953 * the same as the postops
1955 return evaluate_postop(expr
);
1958 ctype
= degenerate(expr
->unop
);
1959 expr
->flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1961 * A logical negation never yields an address constant
1964 expr
->flags
&= ~CEF_ADDR
;
1966 if (is_safe_type(ctype
))
1967 warning(expr
->pos
, "testing a 'safe expression'");
1968 if (is_float_type(ctype
)) {
1969 struct expression
*arg
= expr
->unop
;
1970 expr
->type
= EXPR_COMPARE
;
1971 expr
->op
= SPECIAL_EQUAL
;
1973 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1974 expr
->right
->ctype
= ctype
;
1975 expr
->right
->fvalue
= 0;
1976 } else if (is_fouled_type(ctype
)) {
1977 warning(expr
->pos
, "%s degrades to integer",
1978 show_typename(ctype
->ctype
.base_type
));
1980 /* the result is int [6.5.3.3(5)]*/
1987 expr
->ctype
= ctype
;
1991 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1993 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1994 struct ptr_list
*list
= head
;
2000 for (i
= 0; i
< list
->nr
; i
++) {
2001 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
2003 if (sym
->ident
!= ident
)
2005 *offset
= sym
->offset
;
2008 struct symbol
*ctype
= sym
->ctype
.base_type
;
2012 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
2014 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
2017 *offset
+= sym
->offset
;
2021 } while ((list
= list
->next
) != head
);
2025 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
2027 struct expression
*add
;
2030 * Create a new add-expression
2032 * NOTE! Even if we just add zero, we need a new node
2033 * for the member pointer, since it has a different
2034 * type than the original pointer. We could make that
2035 * be just a cast, but the fact is, a node is a node,
2036 * so we might as well just do the "add zero" here.
2038 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
2041 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2042 add
->right
->ctype
= &int_ctype
;
2043 add
->right
->value
= offset
;
2046 * The ctype of the pointer will be lazily evaluated if
2047 * we ever take the address of this member dereference..
2049 add
->ctype
= &lazy_ptr_ctype
;
2051 * The resulting address of a member access through an address
2052 * constant is an address constant again [6.6(9)].
2054 add
->flags
= expr
->flags
;
2059 /* structure/union dereference */
2060 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
2063 struct symbol
*ctype
, *member
;
2064 struct expression
*deref
= expr
->deref
, *add
;
2065 struct ident
*ident
= expr
->member
;
2066 struct ident
*address_space
;
2069 if (!evaluate_expression(deref
))
2072 expression_error(expr
, "bad member name");
2076 ctype
= deref
->ctype
;
2077 examine_symbol_type(ctype
);
2078 address_space
= ctype
->ctype
.as
;
2079 mod
= ctype
->ctype
.modifiers
;
2080 if (ctype
->type
== SYM_NODE
) {
2081 ctype
= ctype
->ctype
.base_type
;
2082 combine_address_space(deref
->pos
, &address_space
, ctype
->ctype
.as
);
2083 mod
|= ctype
->ctype
.modifiers
;
2085 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
2086 expression_error(expr
, "expected structure or union");
2090 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
2092 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
2093 const char *name
= "<unnamed>";
2096 name
= ctype
->ident
->name
;
2097 namelen
= ctype
->ident
->len
;
2099 if (ctype
->symbol_list
)
2100 expression_error(expr
, "no member '%s' in %s %.*s",
2101 show_ident(ident
), type
, namelen
, name
);
2103 expression_error(expr
, "using member '%s' in "
2104 "incomplete %s %.*s", show_ident(ident
),
2105 type
, namelen
, name
);
2110 * The member needs to take on the address space and modifiers of
2111 * the "parent" type.
2113 member
= convert_to_as_mod(member
, address_space
, mod
);
2114 ctype
= get_base_type(member
);
2116 if (!lvalue_expression(deref
)) {
2117 if (deref
->type
!= EXPR_SLICE
) {
2121 expr
->base
= deref
->base
;
2122 expr
->r_bitpos
= deref
->r_bitpos
;
2124 expr
->r_bitpos
+= bytes_to_bits(offset
);
2125 expr
->type
= EXPR_SLICE
;
2126 expr
->r_nrbits
= member
->bit_size
;
2127 expr
->r_bitpos
+= member
->bit_offset
;
2128 expr
->ctype
= member
;
2132 deref
= deref
->unop
;
2133 expr
->deref
= deref
;
2135 add
= evaluate_offset(deref
, offset
);
2136 expr
->type
= EXPR_PREOP
;
2140 expr
->ctype
= member
;
2144 static int is_promoted(struct expression
*expr
)
2147 switch (expr
->type
) {
2150 case EXPR_CONDITIONAL
:
2174 static struct symbol
*evaluate_cast(struct expression
*);
2176 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2178 struct symbol
*sym
= expr
->cast_type
;
2180 sym
= evaluate_expression(expr
->cast_expression
);
2184 * Expressions of restricted types will possibly get
2185 * promoted - check that here
2187 if (is_restricted_type(sym
)) {
2188 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2190 } else if (is_fouled_type(sym
)) {
2194 examine_symbol_type(sym
);
2195 if (is_bitfield_type(sym
)) {
2196 expression_error(expr
, "trying to examine bitfield type");
2202 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2204 struct symbol
*type
;
2207 type
= evaluate_type_information(expr
);
2211 size
= type
->bit_size
;
2213 if (size
< 0 && is_void_type(type
)) {
2215 warning(expr
->pos
, "expression using sizeof(void)");
2216 size
= bits_in_char
;
2219 if (is_bool_type(type
)) {
2221 warning(expr
->pos
, "expression using sizeof _Bool");
2222 size
= bits_to_bytes(bits_in_bool
) * bits_in_char
;
2225 if (is_function(type
->ctype
.base_type
)) {
2227 warning(expr
->pos
, "expression using sizeof on a function");
2228 size
= bits_in_char
;
2231 if (is_array_type(type
) && size
< 0) { // VLA, 1-dimension only
2232 struct expression
*base
, *size
;
2233 struct symbol
*base_type
;
2235 if (type
->type
== SYM_NODE
)
2236 type
= type
->ctype
.base_type
; // strip the SYM_NODE
2237 base_type
= get_base_type(type
);
2240 if (base_type
->bit_size
<= 0) {
2241 base
= alloc_expression(expr
->pos
, EXPR_SIZEOF
);
2242 base
->cast_type
= base_type
;
2243 if (!evaluate_sizeof(base
))
2246 base
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2247 base
->value
= bits_to_bytes(base_type
->bit_size
);
2248 base
->ctype
= size_t_ctype
;
2250 size
= alloc_expression(expr
->pos
, EXPR_CAST
);
2251 size
->cast_type
= size_t_ctype
;
2252 size
->cast_expression
= type
->array_size
;
2253 if (!evaluate_expression(size
))
2257 expr
->type
= EXPR_BINOP
;
2259 return expr
->ctype
= size_t_ctype
;
2263 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2264 expression_error(expr
, "cannot size expression");
2266 expr
->type
= EXPR_VALUE
;
2267 expr
->value
= bits_to_bytes(size
);
2269 expr
->ctype
= size_t_ctype
;
2270 return size_t_ctype
;
2273 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2275 struct symbol
*type
;
2278 type
= evaluate_type_information(expr
);
2282 if (type
->type
== SYM_NODE
)
2283 type
= type
->ctype
.base_type
;
2286 switch (type
->type
) {
2290 type
= get_base_type(type
);
2294 expression_error(expr
, "expected pointer expression");
2297 size
= type
->bit_size
;
2298 if (size
& (bits_in_char
-1))
2300 expr
->type
= EXPR_VALUE
;
2301 expr
->value
= bits_to_bytes(size
);
2303 expr
->ctype
= size_t_ctype
;
2304 return size_t_ctype
;
2307 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2309 struct symbol
*type
;
2311 type
= evaluate_type_information(expr
);
2315 expr
->type
= EXPR_VALUE
;
2316 expr
->value
= type
->ctype
.alignment
;
2318 expr
->ctype
= size_t_ctype
;
2319 return size_t_ctype
;
2322 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
2324 struct expression
*expr
;
2325 struct symbol_list
*argument_types
= fn
->arguments
;
2326 struct symbol
*argtype
;
2329 PREPARE_PTR_LIST(argument_types
, argtype
);
2330 FOR_EACH_PTR (head
, expr
) {
2331 struct expression
**p
= THIS_ADDRESS(expr
);
2332 struct symbol
*ctype
, *target
;
2333 ctype
= evaluate_expression(expr
);
2340 struct symbol
*type
;
2341 int class = classify_type(ctype
, &type
);
2342 if (is_int(class)) {
2343 *p
= cast_to(expr
, integer_promotion(type
));
2344 } else if (class & TYPE_FLOAT
) {
2345 unsigned long mod
= type
->ctype
.modifiers
;
2346 if (!(mod
& (MOD_LONG_ALL
)))
2347 *p
= cast_to(expr
, &double_ctype
);
2348 } else if (class & TYPE_PTR
) {
2349 if (expr
->ctype
== &null_ctype
)
2350 *p
= cast_to(expr
, &ptr_ctype
);
2354 } else if (!target
->forced_arg
){
2355 static char where
[30];
2356 examine_symbol_type(target
);
2357 sprintf(where
, "argument %d", i
);
2358 compatible_argument_type(expr
, target
, p
, where
);
2362 NEXT_PTR_LIST(argtype
);
2363 } END_FOR_EACH_PTR(expr
);
2364 FINISH_PTR_LIST(argtype
);
2368 static void convert_index(struct expression
*e
)
2370 struct expression
*child
= e
->idx_expression
;
2371 unsigned from
= e
->idx_from
;
2372 unsigned to
= e
->idx_to
+ 1;
2374 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2375 e
->init_nr
= to
- from
;
2376 e
->init_expr
= child
;
2379 static void convert_ident(struct expression
*e
)
2381 struct expression
*child
= e
->ident_expression
;
2382 int offset
= e
->offset
;
2385 e
->init_offset
= offset
;
2387 e
->init_expr
= child
;
2390 static void convert_designators(struct expression
*e
)
2393 if (e
->type
== EXPR_INDEX
)
2395 else if (e
->type
== EXPR_IDENTIFIER
)
2403 static void excess(struct expression
*e
, const char *s
)
2405 warning(e
->pos
, "excessive elements in %s initializer", s
);
2409 * implicit designator for the first element
2411 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2412 struct expression
**v
)
2414 struct expression
*e
= *v
, *new;
2416 if (ctype
->type
== SYM_NODE
)
2417 ctype
= ctype
->ctype
.base_type
;
2419 if (class & TYPE_PTR
) { /* array */
2420 if (!ctype
->bit_size
)
2422 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2423 new->idx_expression
= e
;
2424 new->ctype
= ctype
->ctype
.base_type
;
2426 struct symbol
*field
, *p
;
2427 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2428 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2434 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2435 new->ident_expression
= e
;
2436 new->field
= new->ctype
= field
;
2437 new->offset
= field
->offset
;
2444 * sanity-check explicit designators; return the innermost one or NULL
2445 * in case of error. Assign types.
2447 static struct expression
*check_designators(struct expression
*e
,
2448 struct symbol
*ctype
)
2450 struct expression
*last
= NULL
;
2453 if (ctype
->type
== SYM_NODE
)
2454 ctype
= ctype
->ctype
.base_type
;
2455 if (e
->type
== EXPR_INDEX
) {
2456 struct symbol
*type
;
2457 if (ctype
->type
!= SYM_ARRAY
) {
2458 err
= "array index in non-array";
2461 type
= ctype
->ctype
.base_type
;
2462 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2463 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2464 if (offset
>= ctype
->bit_size
) {
2465 err
= "index out of bounds in";
2469 e
->ctype
= ctype
= type
;
2472 if (!e
->idx_expression
) {
2476 e
= e
->idx_expression
;
2477 } else if (e
->type
== EXPR_IDENTIFIER
) {
2479 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2480 err
= "field name not in struct or union";
2483 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2485 err
= "unknown field name in";
2489 e
->field
= e
->ctype
= ctype
;
2491 if (!e
->ident_expression
) {
2495 e
= e
->ident_expression
;
2496 } else if (e
->type
== EXPR_POS
) {
2497 err
= "internal front-end error: EXPR_POS in";
2502 expression_error(e
, "%s initializer", err
);
2507 * choose the next subobject to initialize.
2509 * Get designators for next element, switch old ones to EXPR_POS.
2510 * Return the resulting expression or NULL if we'd run out of subobjects.
2511 * The innermost designator is returned in *v. Designators in old
2512 * are assumed to be already sanity-checked.
2514 static struct expression
*next_designators(struct expression
*old
,
2515 struct symbol
*ctype
,
2516 struct expression
*e
, struct expression
**v
)
2518 struct expression
*new = NULL
;
2522 if (old
->type
== EXPR_INDEX
) {
2523 struct expression
*copy
;
2526 copy
= next_designators(old
->idx_expression
,
2529 n
= old
->idx_to
+ 1;
2530 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2535 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2538 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2541 new->idx_from
= new->idx_to
= n
;
2542 new->idx_expression
= copy
;
2543 new->ctype
= old
->ctype
;
2545 } else if (old
->type
== EXPR_IDENTIFIER
) {
2546 struct expression
*copy
;
2547 struct symbol
*field
;
2550 copy
= next_designators(old
->ident_expression
,
2553 field
= old
->field
->next_subobject
;
2559 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2561 * We can't necessarily trust "field->offset",
2562 * because the field might be in an anonymous
2563 * union, and the field offset is then the offset
2564 * within that union.
2566 * The "old->offset - old->field->offset"
2567 * would be the offset of such an anonymous
2570 offset
= old
->offset
- old
->field
->offset
;
2573 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2577 new->expr_ident
= field
->ident
;
2578 new->ident_expression
= copy
;
2580 new->offset
= field
->offset
+ offset
;
2586 static int handle_initializer(struct expression
**ep
, int nested
,
2587 int class, struct symbol
*ctype
, unsigned long mods
);
2590 * deal with traversing subobjects [6.7.8(17,18,20)]
2592 static void handle_list_initializer(struct expression
*expr
,
2593 int class, struct symbol
*ctype
, unsigned long mods
)
2595 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2598 FOR_EACH_PTR(expr
->expr_list
, e
) {
2599 struct expression
**v
;
2600 struct symbol
*type
;
2603 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2604 struct symbol
*struct_sym
;
2607 last
= first_subobject(ctype
, class, &top
);
2609 last
= next_designators(last
, ctype
, e
, &top
);
2612 excess(e
, class & TYPE_PTR
? "array" :
2614 DELETE_CURRENT_PTR(e
);
2617 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2618 if (Wdesignated_init
&& struct_sym
->designated_init
)
2619 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2620 ctype
->ident
? "in initializer for " : "",
2621 ctype
->ident
? ctype
->ident
->len
: 0,
2622 ctype
->ident
? ctype
->ident
->name
: "",
2623 ctype
->ident
? ": " : "",
2624 get_type_name(struct_sym
->type
),
2625 show_ident(struct_sym
->ident
));
2627 warning(e
->pos
, "advancing past deep designator");
2630 REPLACE_CURRENT_PTR(e
, last
);
2632 next
= check_designators(e
, ctype
);
2634 DELETE_CURRENT_PTR(e
);
2638 /* deeper than one designator? */
2640 convert_designators(last
);
2645 lclass
= classify_type(top
->ctype
, &type
);
2646 if (top
->type
== EXPR_INDEX
)
2647 v
= &top
->idx_expression
;
2649 v
= &top
->ident_expression
;
2651 mods
|= ctype
->ctype
.modifiers
& MOD_STORAGE
;
2652 if (handle_initializer(v
, 1, lclass
, top
->ctype
, mods
))
2655 if (!(lclass
& TYPE_COMPOUND
)) {
2656 warning(e
->pos
, "bogus scalar initializer");
2657 DELETE_CURRENT_PTR(e
);
2661 next
= first_subobject(type
, lclass
, v
);
2663 warning(e
->pos
, "missing braces around initializer");
2668 DELETE_CURRENT_PTR(e
);
2669 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2671 } END_FOR_EACH_PTR(e
);
2673 convert_designators(last
);
2674 expr
->ctype
= ctype
;
2677 static int is_string_literal(struct expression
**v
)
2679 struct expression
*e
= *v
;
2680 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2682 if (!e
|| e
->type
!= EXPR_STRING
)
2684 if (e
!= *v
&& Wparen_string
)
2686 "array initialized from parenthesized string constant");
2692 * We want a normal expression, possibly in one layer of braces. Warn
2693 * if the latter happens inside a list (it's legal, but likely to be
2694 * an effect of screwup). In case of anything not legal, we are definitely
2695 * having an effect of screwup, so just fail and let the caller warn.
2697 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2699 struct expression
*v
= NULL
, *p
;
2703 if (e
->type
!= EXPR_INITIALIZER
)
2706 FOR_EACH_PTR(e
->expr_list
, p
) {
2710 } END_FOR_EACH_PTR(p
);
2714 case EXPR_INITIALIZER
:
2716 case EXPR_IDENTIFIER
:
2722 warning(e
->pos
, "braces around scalar initializer");
2727 * deal with the cases that don't care about subobjects:
2728 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2729 * character array <- string literal, possibly in braces [6.7.8(14)]
2730 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2731 * compound type <- initializer list in braces [6.7.8(16)]
2732 * The last one punts to handle_list_initializer() which, in turn will call
2733 * us for individual elements of the list.
2735 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2736 * the lack of support of wide char stuff in general.
2738 * One note: we need to take care not to evaluate a string literal until
2739 * we know that we *will* handle it right here. Otherwise we would screw
2740 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2741 * { "string", ...} - we need to preserve that string literal recognizable
2742 * until we dig into the inner struct.
2744 static int handle_initializer(struct expression
**ep
, int nested
,
2745 int class, struct symbol
*ctype
, unsigned long mods
)
2747 int is_string
= is_string_type(ctype
);
2748 struct expression
*e
= *ep
, *p
;
2749 struct symbol
*type
;
2755 if (!(class & TYPE_COMPOUND
)) {
2756 e
= handle_scalar(e
, nested
);
2760 if (!evaluate_expression(e
))
2762 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2764 * Initializers for static storage duration objects
2765 * shall be constant expressions or a string literal [6.7.8(4)].
2767 mods
|= ctype
->ctype
.modifiers
;
2768 mods
&= (MOD_TOPLEVEL
| MOD_STATIC
);
2769 if (mods
&& !(e
->flags
& (CEF_ACE
| CEF_ADDR
)))
2770 if (Wconstexpr_not_const
)
2771 warning(e
->pos
, "non-constant initializer for static object");
2777 * sublist; either a string, or we dig in; the latter will deal with
2778 * pathologies, so we don't need anything fancy here.
2780 if (e
->type
== EXPR_INITIALIZER
) {
2782 struct expression
*v
= NULL
;
2785 FOR_EACH_PTR(e
->expr_list
, p
) {
2789 } END_FOR_EACH_PTR(p
);
2790 if (count
== 1 && is_string_literal(&v
)) {
2795 handle_list_initializer(e
, class, ctype
, mods
);
2800 if (is_string_literal(&e
)) {
2801 /* either we are doing array of char, or we'll have to dig in */
2808 /* struct or union can be initialized by compatible */
2809 if (class != TYPE_COMPOUND
)
2811 type
= evaluate_expression(e
);
2814 if (ctype
->type
== SYM_NODE
)
2815 ctype
= ctype
->ctype
.base_type
;
2816 if (type
->type
== SYM_NODE
)
2817 type
= type
->ctype
.base_type
;
2823 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2825 type
= evaluate_expression(p
);
2826 if (ctype
->bit_size
!= -1) {
2827 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2829 "too long initializer-string for array of char");
2830 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2832 "too long initializer-string for array of char(no space for nul char)");
2839 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2841 struct symbol
*type
;
2842 int class = classify_type(ctype
, &type
);
2843 if (!handle_initializer(ep
, 0, class, ctype
, 0))
2844 expression_error(*ep
, "invalid initializer");
2847 static struct symbol
*cast_to_bool(struct expression
*expr
)
2849 struct expression
*old
= expr
->cast_expression
;
2850 struct expression
*zero
;
2851 struct symbol
*otype
;
2852 int oclass
= classify_type(degenerate(old
), &otype
);
2853 struct symbol
*ctype
;
2855 if (oclass
& TYPE_COMPOUND
)
2858 zero
= alloc_const_expression(expr
->pos
, 0);
2859 expr
->op
= SPECIAL_NOTEQUAL
;
2860 ctype
= usual_conversions(expr
->op
, old
, zero
,
2861 oclass
, TYPE_NUM
, otype
, zero
->ctype
);
2862 expr
->type
= EXPR_COMPARE
;
2863 expr
->left
= cast_to(old
, ctype
);
2864 expr
->right
= cast_to(zero
, ctype
);
2869 static int cast_flags(struct expression
*expr
, struct expression
*old
)
2873 int flags
= CEF_NONE
;
2875 class = classify_type(expr
->ctype
, &t
);
2876 if (class & TYPE_NUM
) {
2877 flags
= old
->flags
& ~CEF_CONST_MASK
;
2879 * Casts to numeric types never result in address
2880 * constants [6.6(9)].
2885 * As an extension, treat address constants cast to
2886 * integer type as an arithmetic constant.
2888 if (old
->flags
& CEF_ADDR
)
2892 * Cast to float type -> not an integer constant
2893 * expression [6.6(6)].
2895 if (class & TYPE_FLOAT
)
2896 flags
&= ~CEF_CLR_ICE
;
2898 * Casts of float literals to integer type results in
2899 * a constant integer expression [6.6(6)].
2901 else if (old
->flags
& CEF_FLOAT
)
2902 flags
= CEF_SET_ICE
;
2903 } else if (class & TYPE_PTR
) {
2905 * Casts of integer literals to pointer type yield
2906 * address constants [6.6(9)].
2908 * As an extension, treat address constants cast to a
2909 * different pointer type as address constants again.
2911 * As another extension, treat integer constant
2912 * expressions (in contrast to literals) cast to
2913 * pointer type as address constants.
2915 if (old
->flags
& (CEF_ICE
| CEF_ADDR
))
2922 static struct symbol
*evaluate_cast(struct expression
*expr
)
2924 struct expression
*source
= expr
->cast_expression
;
2925 struct symbol
*ctype
;
2926 struct symbol
*ttype
, *stype
;
2928 struct ident
*tas
= NULL
, *sas
= NULL
;
2934 * Special case: a cast can be followed by an
2935 * initializer, in which case we need to pass
2936 * the type value down to that initializer rather
2937 * than trying to evaluate it as an expression
2939 * A more complex case is when the initializer is
2940 * dereferenced as part of a post-fix expression.
2941 * We need to produce an expression that can be dereferenced.
2943 if (source
->type
== EXPR_INITIALIZER
) {
2944 struct symbol
*sym
= expr
->cast_type
;
2945 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2947 sym
->initializer
= source
;
2948 evaluate_symbol(sym
);
2950 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2952 if (sym
->ctype
.modifiers
& MOD_TOPLEVEL
)
2953 addr
->flags
|= CEF_ADDR
;
2955 expr
->type
= EXPR_PREOP
;
2963 ctype
= examine_symbol_type(expr
->cast_type
);
2964 expr
->ctype
= ctype
;
2965 expr
->cast_type
= ctype
;
2967 evaluate_expression(source
);
2970 tclass
= classify_type(ctype
, &ttype
);
2972 expr
->flags
= cast_flags(expr
, source
);
2975 * You can always throw a value away by casting to
2976 * "void" - that's an implicit "force". Note that
2977 * the same is _not_ true of "void *".
2979 if (ttype
== &void_ctype
)
2982 stype
= source
->ctype
;
2984 expression_error(expr
, "cast from unknown type");
2987 sclass
= classify_type(stype
, &stype
);
2989 if (expr
->type
== EXPR_FORCE_CAST
)
2992 if (tclass
& (TYPE_COMPOUND
| TYPE_FN
))
2993 warning(expr
->pos
, "cast to non-scalar");
2995 if (sclass
& TYPE_COMPOUND
)
2996 warning(expr
->pos
, "cast from non-scalar");
2998 /* allowed cast unfouls */
2999 if (sclass
& TYPE_FOULED
)
3000 stype
= unfoul(stype
);
3002 if (ttype
!= stype
) {
3003 if ((tclass
& TYPE_RESTRICT
) && restricted_value(source
, ttype
))
3004 warning(expr
->pos
, "cast to %s",
3005 show_typename(ttype
));
3006 if (sclass
& TYPE_RESTRICT
) {
3007 if (ttype
== &bool_ctype
) {
3008 if (sclass
& TYPE_FOULED
)
3009 warning(expr
->pos
, "%s degrades to integer",
3010 show_typename(stype
));
3012 warning(expr
->pos
, "cast from %s",
3013 show_typename(stype
));
3018 if ((ttype
== &ulong_ctype
|| ttype
== uintptr_ctype
) && !Wcast_from_as
)
3019 tas
= &bad_address_space
;
3020 else if (tclass
== TYPE_PTR
) {
3021 examine_pointer_target(ttype
);
3022 tas
= ttype
->ctype
.as
;
3025 if ((stype
== &ulong_ctype
|| stype
== uintptr_ctype
))
3026 sas
= &bad_address_space
;
3027 else if (sclass
== TYPE_PTR
) {
3028 examine_pointer_target(stype
);
3029 sas
= stype
->ctype
.as
;
3032 if (!tas
&& valid_as(sas
))
3033 warning(expr
->pos
, "cast removes address space '%s' of expression", show_as(sas
));
3034 if (valid_as(tas
) && valid_as(sas
) && tas
!= sas
)
3035 warning(expr
->pos
, "cast between address spaces (%s -> %s)", show_as(sas
), show_as(tas
));
3036 if (valid_as(tas
) && !sas
&&
3037 !is_null_pointer_constant(source
) && Wcast_to_as
)
3039 "cast adds address space '%s' to expression", show_as(tas
));
3041 if (!(ttype
->ctype
.modifiers
& MOD_PTRINHERIT
) && tclass
== TYPE_PTR
&&
3042 !tas
&& (source
->flags
& CEF_ICE
)) {
3043 if (ttype
->ctype
.base_type
== &void_ctype
) {
3044 if (is_zero_constant(source
)) {
3046 expr
->type
= EXPR_VALUE
;
3047 expr
->ctype
= &null_ctype
;
3054 if (ttype
== &bool_ctype
)
3057 // checks pointers to restricted
3058 while (Wbitwise_pointer
&& tclass
== TYPE_PTR
&& sclass
== TYPE_PTR
) {
3059 tclass
= classify_type(ttype
->ctype
.base_type
, &ttype
);
3060 sclass
= classify_type(stype
->ctype
.base_type
, &stype
);
3063 if (!ttype
|| !stype
)
3065 if (ttype
== &void_ctype
|| stype
== &void_ctype
)
3067 if (tclass
& TYPE_RESTRICT
) {
3068 warning(expr
->pos
, "cast to %s", show_typename(ctype
));
3071 if (sclass
& TYPE_RESTRICT
) {
3072 warning(expr
->pos
, "cast from %s", show_typename(source
->ctype
));
3081 * Evaluate a call expression with a symbol. This
3082 * should expand inline functions, and evaluate
3085 static int evaluate_symbol_call(struct expression
*expr
)
3087 struct expression
*fn
= expr
->fn
;
3088 struct symbol
*ctype
= fn
->ctype
;
3090 if (fn
->type
!= EXPR_PREOP
)
3093 if (ctype
->op
&& ctype
->op
->evaluate
)
3094 return ctype
->op
->evaluate(expr
);
3096 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
3098 struct symbol
*curr
= current_fn
;
3100 if (ctype
->definition
)
3101 ctype
= ctype
->definition
;
3103 current_fn
= ctype
->ctype
.base_type
;
3105 ret
= inline_function(expr
, ctype
);
3107 /* restore the old function */
3115 static struct symbol
*evaluate_call(struct expression
*expr
)
3118 struct symbol
*ctype
, *sym
;
3119 struct expression
*fn
= expr
->fn
;
3120 struct expression_list
*arglist
= expr
->args
;
3122 if (!evaluate_expression(fn
))
3124 sym
= ctype
= fn
->ctype
;
3125 if (ctype
->type
== SYM_NODE
)
3126 ctype
= ctype
->ctype
.base_type
;
3127 if (ctype
->type
== SYM_PTR
)
3128 ctype
= get_base_type(ctype
);
3130 if (ctype
->type
!= SYM_FN
) {
3131 struct expression
*arg
;
3132 expression_error(expr
, "not a function %s",
3133 show_ident(sym
->ident
));
3134 /* do typechecking in arguments */
3135 FOR_EACH_PTR (arglist
, arg
) {
3136 evaluate_expression(arg
);
3137 } END_FOR_EACH_PTR(arg
);
3141 examine_fn_arguments(ctype
);
3142 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
3143 sym
->op
&& sym
->op
->args
) {
3144 if (!sym
->op
->args(expr
))
3147 if (!evaluate_arguments(ctype
, arglist
))
3149 args
= expression_list_size(expr
->args
);
3150 fnargs
= symbol_list_size(ctype
->arguments
);
3151 if (args
< fnargs
) {
3152 expression_error(expr
,
3153 "not enough arguments for function %s",
3154 show_ident(sym
->ident
));
3157 if (args
> fnargs
&& !ctype
->variadic
)
3158 expression_error(expr
,
3159 "too many arguments for function %s",
3160 show_ident(sym
->ident
));
3162 expr
->ctype
= ctype
->ctype
.base_type
;
3163 if (sym
->type
== SYM_NODE
) {
3164 if (evaluate_symbol_call(expr
))
3170 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
3172 struct expression
*e
= expr
->down
;
3173 struct symbol
*ctype
= expr
->in
;
3176 if (expr
->op
== '.') {
3177 struct symbol
*field
;
3180 expression_error(expr
, "expected structure or union");
3183 examine_symbol_type(ctype
);
3184 class = classify_type(ctype
, &ctype
);
3185 if (class != TYPE_COMPOUND
) {
3186 expression_error(expr
, "expected structure or union");
3190 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
3192 expression_error(expr
, "unknown member");
3196 expr
->type
= EXPR_VALUE
;
3197 expr
->flags
= CEF_SET_ICE
;
3198 expr
->value
= offset
;
3200 expr
->ctype
= size_t_ctype
;
3203 expression_error(expr
, "expected structure or union");
3206 examine_symbol_type(ctype
);
3207 class = classify_type(ctype
, &ctype
);
3208 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
3209 expression_error(expr
, "expected array");
3212 ctype
= ctype
->ctype
.base_type
;
3214 expr
->type
= EXPR_VALUE
;
3215 expr
->flags
= CEF_SET_ICE
;
3218 expr
->ctype
= size_t_ctype
;
3220 struct expression
*idx
= expr
->index
, *m
;
3221 struct symbol
*i_type
= evaluate_expression(idx
);
3222 unsigned old_idx_flags
;
3223 int i_class
= classify_type(i_type
, &i_type
);
3225 if (!is_int(i_class
)) {
3226 expression_error(expr
, "non-integer index");
3229 unrestrict(idx
, i_class
, &i_type
);
3230 old_idx_flags
= idx
->flags
;
3231 idx
= cast_to(idx
, size_t_ctype
);
3232 idx
->flags
= old_idx_flags
;
3233 m
= alloc_const_expression(expr
->pos
,
3234 bits_to_bytes(ctype
->bit_size
));
3235 m
->ctype
= size_t_ctype
;
3236 m
->flags
= CEF_SET_INT
;
3237 expr
->type
= EXPR_BINOP
;
3241 expr
->ctype
= size_t_ctype
;
3242 expr
->flags
= m
->flags
& idx
->flags
& ~CEF_CONST_MASK
;
3246 struct expression
*copy
= __alloc_expression(0);
3248 if (e
->type
== EXPR_OFFSETOF
)
3250 if (!evaluate_expression(e
))
3252 expr
->type
= EXPR_BINOP
;
3253 expr
->flags
= e
->flags
& copy
->flags
& ~CEF_CONST_MASK
;
3255 expr
->ctype
= size_t_ctype
;
3259 return size_t_ctype
;
3262 struct symbol
*evaluate_expression(struct expression
*expr
)
3269 switch (expr
->type
) {
3272 expression_error(expr
, "value expression without a type");
3275 return evaluate_string(expr
);
3277 return evaluate_symbol_expression(expr
);
3279 evaluate_expression(expr
->left
);
3280 evaluate_expression(expr
->right
);
3281 if (!valid_subexpr_type(expr
))
3283 return evaluate_binop(expr
);
3285 return evaluate_logical(expr
);
3287 evaluate_expression(expr
->left
);
3288 if (!evaluate_expression(expr
->right
))
3290 return evaluate_comma(expr
);
3292 evaluate_expression(expr
->left
);
3293 evaluate_expression(expr
->right
);
3294 if (!valid_subexpr_type(expr
))
3296 return evaluate_compare(expr
);
3297 case EXPR_ASSIGNMENT
:
3298 evaluate_expression(expr
->left
);
3299 evaluate_expression(expr
->right
);
3300 if (!valid_subexpr_type(expr
))
3302 return evaluate_assignment(expr
);
3304 if (!evaluate_expression(expr
->unop
))
3306 return evaluate_preop(expr
);
3308 if (!evaluate_expression(expr
->unop
))
3310 return evaluate_postop(expr
);
3312 case EXPR_FORCE_CAST
:
3313 case EXPR_IMPLIED_CAST
:
3314 return evaluate_cast(expr
);
3316 return evaluate_sizeof(expr
);
3317 case EXPR_PTRSIZEOF
:
3318 return evaluate_ptrsizeof(expr
);
3320 return evaluate_alignof(expr
);
3322 return evaluate_member_dereference(expr
);
3324 return evaluate_call(expr
);
3326 case EXPR_CONDITIONAL
:
3327 return evaluate_conditional_expression(expr
);
3328 case EXPR_STATEMENT
:
3329 expr
->ctype
= evaluate_statement(expr
->statement
);
3333 expr
->ctype
= &ptr_ctype
;
3337 /* Evaluate the type of the symbol .. */
3338 evaluate_symbol(expr
->symbol
);
3339 /* .. but the type of the _expression_ is a "type" */
3340 expr
->ctype
= &type_ctype
;
3344 return evaluate_offsetof(expr
);
3346 /* These can not exist as stand-alone expressions */
3347 case EXPR_INITIALIZER
:
3348 case EXPR_IDENTIFIER
:
3351 expression_error(expr
, "internal front-end error: initializer in expression");
3354 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3356 case EXPR_ASM_OPERAND
:
3357 expression_error(expr
, "internal front-end error: ASM_OPERAND evaluated");
3363 void check_duplicates(struct symbol
*sym
)
3366 struct symbol
*next
= sym
;
3367 int initialized
= sym
->initializer
!= NULL
;
3369 while ((next
= next
->same_symbol
) != NULL
) {
3370 const char *typediff
;
3371 evaluate_symbol(next
);
3372 if (initialized
&& next
->initializer
) {
3373 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3374 show_ident(sym
->ident
),
3375 stream_name(next
->pos
.stream
), next
->pos
.line
);
3376 /* Only warn once */
3380 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3382 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3383 show_ident(sym
->ident
),
3384 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3389 unsigned long mod
= sym
->ctype
.modifiers
;
3390 if (mod
& (MOD_STATIC
| MOD_REGISTER
| MOD_EXT_VISIBLE
))
3392 if (!(mod
& MOD_TOPLEVEL
))
3396 if (sym
->ident
== &main_ident
)
3398 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3402 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3404 struct symbol
*base_type
;
3412 sym
= examine_symbol_type(sym
);
3413 base_type
= get_base_type(sym
);
3417 /* Evaluate the initializers */
3418 if (sym
->initializer
)
3419 evaluate_initializer(sym
, &sym
->initializer
);
3421 /* And finally, evaluate the body of the symbol too */
3422 if (base_type
->type
== SYM_FN
) {
3423 struct symbol
*curr
= current_fn
;
3425 if (sym
->definition
&& sym
->definition
!= sym
)
3426 return evaluate_symbol(sym
->definition
);
3428 current_fn
= base_type
;
3430 examine_fn_arguments(base_type
);
3431 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3433 if (base_type
->stmt
)
3434 evaluate_statement(base_type
->stmt
);
3442 void evaluate_symbol_list(struct symbol_list
*list
)
3446 FOR_EACH_PTR(list
, sym
) {
3447 has_error
&= ~ERROR_CURR_PHASE
;
3448 evaluate_symbol(sym
);
3449 check_duplicates(sym
);
3450 } END_FOR_EACH_PTR(sym
);
3453 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3455 struct expression
*expr
= stmt
->expression
;
3456 struct symbol
*fntype
;
3458 evaluate_expression(expr
);
3459 fntype
= current_fn
->ctype
.base_type
;
3460 if (!fntype
|| fntype
== &void_ctype
) {
3461 if (expr
&& expr
->ctype
!= &void_ctype
)
3462 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3463 if (expr
&& Wreturn_void
)
3464 warning(stmt
->pos
, "returning void-valued expression");
3469 sparse_error(stmt
->pos
, "return with no return value");
3474 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3478 static void evaluate_if_statement(struct statement
*stmt
)
3480 if (!stmt
->if_conditional
)
3483 evaluate_conditional(stmt
->if_conditional
, 0);
3484 evaluate_statement(stmt
->if_true
);
3485 evaluate_statement(stmt
->if_false
);
3488 static void evaluate_iterator(struct statement
*stmt
)
3490 evaluate_symbol_list(stmt
->iterator_syms
);
3491 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3492 evaluate_conditional(stmt
->iterator_post_condition
,1);
3493 evaluate_statement(stmt
->iterator_pre_statement
);
3494 evaluate_statement(stmt
->iterator_statement
);
3495 evaluate_statement(stmt
->iterator_post_statement
);
3498 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3500 switch (*constraint
) {
3501 case '=': /* Assignment */
3502 case '+': /* Update */
3505 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3509 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3511 switch (*constraint
) {
3512 case '=': /* Assignment */
3513 case '+': /* Update */
3514 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3518 static void evaluate_asm_statement(struct statement
*stmt
)
3520 struct expression
*expr
;
3521 struct expression
*op
;
3524 expr
= stmt
->asm_string
;
3525 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3526 sparse_error(stmt
->pos
, "need constant string for inline asm");
3530 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
3534 expr
= op
->constraint
;
3535 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3536 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3537 op
->constraint
= NULL
;
3539 verify_output_constraint(expr
, expr
->string
->data
);
3543 if (!evaluate_expression(expr
))
3545 if (!lvalue_expression(expr
))
3546 warning(expr
->pos
, "asm output is not an lvalue");
3547 evaluate_assign_to(expr
, expr
->ctype
);
3548 } END_FOR_EACH_PTR(op
);
3550 FOR_EACH_PTR(stmt
->asm_inputs
, op
) {
3554 expr
= op
->constraint
;
3555 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3556 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3557 op
->constraint
= NULL
;
3559 verify_input_constraint(expr
, expr
->string
->data
);
3562 if (!evaluate_expression(op
->expr
))
3564 } END_FOR_EACH_PTR(op
);
3566 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3568 sparse_error(stmt
->pos
, "bad asm clobbers");
3571 if (expr
->type
== EXPR_STRING
)
3573 expression_error(expr
, "asm clobber is not a string");
3574 } END_FOR_EACH_PTR(expr
);
3576 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3577 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3578 sparse_error(stmt
->pos
, "bad asm label");
3581 } END_FOR_EACH_PTR(sym
);
3584 static void evaluate_case_statement(struct statement
*stmt
)
3586 evaluate_expression(stmt
->case_expression
);
3587 evaluate_expression(stmt
->case_to
);
3588 evaluate_statement(stmt
->case_statement
);
3591 static void check_case_type(struct expression
*switch_expr
,
3592 struct expression
*case_expr
,
3593 struct expression
**enumcase
)
3595 struct symbol
*switch_type
, *case_type
;
3601 switch_type
= switch_expr
->ctype
;
3602 case_type
= evaluate_expression(case_expr
);
3604 if (!switch_type
|| !case_type
)
3608 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3609 else if (is_enum_type(case_type
))
3610 *enumcase
= case_expr
;
3613 sclass
= classify_type(switch_type
, &switch_type
);
3614 cclass
= classify_type(case_type
, &case_type
);
3616 /* both should be arithmetic */
3617 if (!(sclass
& cclass
& TYPE_NUM
))
3620 /* neither should be floating */
3621 if ((sclass
| cclass
) & TYPE_FLOAT
)
3624 /* if neither is restricted, we are OK */
3625 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3628 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3629 cclass
, sclass
, case_type
, switch_type
)) {
3630 unrestrict(case_expr
, cclass
, &case_type
);
3631 unrestrict(switch_expr
, sclass
, &switch_type
);
3636 expression_error(case_expr
, "incompatible types for 'case' statement");
3639 static void evaluate_switch_statement(struct statement
*stmt
)
3642 struct expression
*enumcase
= NULL
;
3643 struct expression
**enumcase_holder
= &enumcase
;
3644 struct expression
*sel
= stmt
->switch_expression
;
3646 evaluate_expression(sel
);
3647 evaluate_statement(stmt
->switch_statement
);
3650 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3651 enumcase_holder
= NULL
; /* Only check cases against switch */
3653 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3654 struct statement
*case_stmt
= sym
->stmt
;
3655 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3656 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3657 } END_FOR_EACH_PTR(sym
);
3660 static void evaluate_goto_statement(struct statement
*stmt
)
3662 struct symbol
*label
= stmt
->goto_label
;
3664 if (label
&& !label
->stmt
&& label
->ident
&& !lookup_keyword(label
->ident
, NS_KEYWORD
))
3665 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3667 evaluate_expression(stmt
->goto_expression
);
3670 struct symbol
*evaluate_statement(struct statement
*stmt
)
3675 switch (stmt
->type
) {
3676 case STMT_DECLARATION
: {
3678 FOR_EACH_PTR(stmt
->declaration
, s
) {
3680 } END_FOR_EACH_PTR(s
);
3685 return evaluate_return_expression(stmt
);
3687 case STMT_EXPRESSION
:
3688 if (!evaluate_expression(stmt
->expression
))
3690 if (stmt
->expression
->ctype
== &null_ctype
)
3691 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3692 return degenerate(stmt
->expression
);
3694 case STMT_COMPOUND
: {
3695 struct statement
*s
;
3696 struct symbol
*type
= NULL
;
3698 /* Evaluate the return symbol in the compound statement */
3699 evaluate_symbol(stmt
->ret
);
3702 * Then, evaluate each statement, making the type of the
3703 * compound statement be the type of the last statement
3705 type
= evaluate_statement(stmt
->args
);
3706 FOR_EACH_PTR(stmt
->stmts
, s
) {
3707 type
= evaluate_statement(s
);
3708 } END_FOR_EACH_PTR(s
);
3714 evaluate_if_statement(stmt
);
3717 evaluate_iterator(stmt
);
3720 evaluate_switch_statement(stmt
);
3723 evaluate_case_statement(stmt
);
3726 return evaluate_statement(stmt
->label_statement
);
3728 evaluate_goto_statement(stmt
);
3733 evaluate_asm_statement(stmt
);
3736 evaluate_expression(stmt
->expression
);
3739 evaluate_expression(stmt
->range_expression
);
3740 evaluate_expression(stmt
->range_low
);
3741 evaluate_expression(stmt
->range_high
);