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
*unqualify_type(struct symbol
*ctype
)
68 if (ctype
->type
== SYM_NODE
&& (ctype
->ctype
.modifiers
& MOD_QUALIFIER
)) {
69 struct symbol
*unqual
= alloc_symbol(ctype
->pos
, 0);
72 unqual
->ctype
.modifiers
&= ~MOD_QUALIFIER
;
78 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
80 struct expression
*addr
;
81 struct symbol
*sym
= expr
->symbol
;
82 struct symbol
*base_type
;
85 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
89 examine_symbol_type(sym
);
91 base_type
= get_base_type(sym
);
93 expression_error(expr
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
97 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
99 addr
->symbol_name
= expr
->symbol_name
;
100 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
101 addr
->flags
= expr
->flags
;
102 expr
->type
= EXPR_PREOP
;
105 expr
->flags
= CEF_NONE
;
107 /* The type of a symbol is the symbol itself! */
112 static struct symbol
*evaluate_string(struct expression
*expr
)
114 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
115 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
116 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
117 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
118 unsigned int length
= expr
->string
->length
;
119 struct symbol
*char_type
= expr
->wide
? wchar_ctype
: &char_ctype
;
121 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
122 sym
->bit_size
= length
* char_type
->bit_size
;
123 sym
->ctype
.alignment
= 1;
125 sym
->ctype
.modifiers
= MOD_STATIC
;
126 sym
->ctype
.base_type
= array
;
127 sym
->initializer
= initstr
;
131 initstr
->ctype
= sym
;
132 initstr
->string
= expr
->string
;
134 array
->array_size
= sym
->array_size
;
135 array
->bit_size
= sym
->bit_size
;
136 array
->ctype
.alignment
= char_type
->ctype
.alignment
;
137 array
->ctype
.modifiers
= MOD_STATIC
;
138 array
->ctype
.base_type
= char_type
;
140 array
->evaluated
= 1;
143 addr
->ctype
= &lazy_ptr_ctype
;
144 addr
->flags
= CEF_ADDR
;
146 expr
->type
= EXPR_PREOP
;
153 /* type has come from classify_type and is an integer type */
154 static inline struct symbol
*integer_promotion(struct symbol
*type
)
156 unsigned long mod
= type
->ctype
.modifiers
;
157 int width
= type
->bit_size
;
160 * Bitfields always promote to the base type,
161 * even if the bitfield might be bigger than
164 if (type
->type
== SYM_BITFIELD
) {
165 type
= type
->ctype
.base_type
;
167 mod
= type
->ctype
.modifiers
;
168 if (width
< bits_in_int
)
171 /* If char/short has as many bits as int, it still gets "promoted" */
172 if (type
->rank
< 0) {
173 if (mod
& MOD_UNSIGNED
)
181 * After integer promotons:
182 * If both types are the same
183 * -> no conversion needed
184 * If the types have the same signedness (their rank must be different)
185 * -> convert to the type of the highest rank
186 * If rank(unsigned type) >= rank(signed type)
187 * -> convert to the unsigned type
188 * If size(signed type) > size(unsigned type)
189 * -> convert to the signed type
191 * -> convert to the unsigned type corresponding to the signed type.
193 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
195 static struct symbol
*unsigned_types
[] = {
199 [3] = &uint128_ctype
,
201 unsigned long lmod
, rmod
;
202 struct symbol
*stype
, *utype
;
204 left
= integer_promotion(left
);
205 right
= integer_promotion(right
);
210 lmod
= left
->ctype
.modifiers
;
211 rmod
= right
->ctype
.modifiers
;
212 if (((lmod
^ rmod
) & MOD_UNSIGNED
) == 0)
213 return (left
->rank
> right
->rank
) ? left
: right
;
214 if (lmod
& MOD_UNSIGNED
) {
221 if (utype
->rank
>= stype
->rank
)
223 if (stype
->bit_size
> utype
->bit_size
)
225 utype
= unsigned_types
[stype
->rank
];
229 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, struct ident
**asp
)
231 unsigned long mod
= 0;
232 struct ident
*as
= NULL
;
235 mod
|= node
->ctype
.modifiers
;
236 combine_address_space(node
->pos
, &as
, node
->ctype
.as
);
237 if (node
->type
== SYM_NODE
) {
238 node
= node
->ctype
.base_type
;
243 *modp
= mod
& ~MOD_IGNORE
;
248 static int is_same_type(struct expression
*expr
, struct symbol
*new)
250 struct symbol
*old
= expr
->ctype
;
251 unsigned long oldmod
, newmod
;
252 struct ident
*oldas
, *newas
;
254 old
= base_type(old
, &oldmod
, &oldas
);
255 new = base_type(new, &newmod
, &newas
);
257 /* Same base type, same address space? */
258 if (old
== new && oldas
== newas
) {
259 unsigned long difmod
;
261 /* Check the modifier bits. */
262 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
264 /* Exact same type? */
269 * Not the same type, but differs only in "const".
270 * Don't warn about MOD_NOCAST.
272 if (difmod
== MOD_CONST
)
275 if ((oldmod
| newmod
) & MOD_NOCAST
) {
276 const char *tofrom
= "to/from";
277 if (!(newmod
& MOD_NOCAST
))
279 if (!(oldmod
& MOD_NOCAST
))
281 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
287 warn_for_different_enum_types (struct position pos
,
288 struct symbol
*typea
,
289 struct symbol
*typeb
)
293 if (typea
->type
== SYM_NODE
)
294 typea
= typea
->ctype
.base_type
;
295 if (typeb
->type
== SYM_NODE
)
296 typeb
= typeb
->ctype
.base_type
;
301 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
302 warning(pos
, "mixing different enum types:");
303 info(pos
, " %s", show_typename(typea
));
304 info(pos
, " %s", show_typename(typeb
));
308 static int cast_flags(struct expression
*expr
, struct expression
*target
);
309 static struct symbol
*cast_to_bool(struct expression
*expr
);
312 * This gets called for implicit casts in assignments and
315 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
317 struct expression
*expr
;
319 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
321 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
324 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
326 expr
->cast_type
= type
;
327 expr
->cast_expression
= old
;
328 expr
->flags
= cast_flags(expr
, old
);
330 if (is_bool_type(type
))
347 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
349 static int type_class
[SYM_BAD
+ 1] = {
350 [SYM_PTR
] = TYPE_PTR
,
351 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
352 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
353 [SYM_STRUCT
] = TYPE_COMPOUND
,
354 [SYM_UNION
] = TYPE_COMPOUND
,
355 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
356 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
357 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
359 if (type
->type
== SYM_NODE
)
360 type
= type
->ctype
.base_type
;
361 if (type
->type
== SYM_TYPEOF
) {
362 type
= examine_symbol_type(type
);
363 if (type
->type
== SYM_NODE
)
364 type
= type
->ctype
.base_type
;
366 if (type
->type
== SYM_ENUM
)
367 type
= type
->ctype
.base_type
;
369 if (type
->type
== SYM_BASETYPE
) {
370 if (type
->ctype
.base_type
== &int_type
)
372 if (type
->ctype
.base_type
== &fp_type
)
373 return TYPE_NUM
| TYPE_FLOAT
;
375 return type_class
[type
->type
];
378 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
380 static inline int is_string_type(struct symbol
*type
)
382 if (type
->type
== SYM_NODE
)
383 type
= type
->ctype
.base_type
;
384 if (type
->type
!= SYM_ARRAY
)
386 type
= type
->ctype
.base_type
;
387 return is_byte_type(type
) || is_wchar_type(type
);
390 static struct symbol
*bad_expr_type(struct expression
*expr
)
392 switch (expr
->type
) {
395 if (!valid_subexpr_type(expr
))
397 sparse_error(expr
->pos
, "incompatible types for operation (%s):", show_special(expr
->op
));
398 info(expr
->pos
, " %s", show_typename(expr
->left
->ctype
));
399 info(expr
->pos
, " %s", show_typename(expr
->right
->ctype
));
403 if (!valid_expr_type(expr
->unop
))
405 sparse_error(expr
->pos
, "incompatible type for operation (%s):", show_special(expr
->op
));
406 info(expr
->pos
, " %s", show_typename(expr
->unop
->ctype
));
412 expr
->flags
= CEF_NONE
;
413 return expr
->ctype
= &bad_ctype
;
416 static int restricted_value(struct expression
*v
, struct symbol
*type
)
418 if (v
->type
!= EXPR_VALUE
)
425 static int restricted_binop(int op
, struct symbol
*type
)
430 case SPECIAL_AND_ASSIGN
:
431 case SPECIAL_OR_ASSIGN
:
432 case SPECIAL_XOR_ASSIGN
:
433 return 1; /* unfoul */
437 return 2; /* keep fouled */
439 case SPECIAL_NOTEQUAL
:
440 return 3; /* warn if fouled */
446 static int restricted_unop(int op
, struct symbol
**type
)
449 if ((*type
)->bit_size
< bits_in_int
)
450 *type
= befoul(*type
);
457 /* type should be SYM_FOULED */
458 static inline struct symbol
*unfoul(struct symbol
*type
)
460 return type
->ctype
.base_type
;
463 static struct symbol
*restricted_binop_type(int op
,
464 struct expression
*left
,
465 struct expression
*right
,
466 int lclass
, int rclass
,
467 struct symbol
*ltype
,
468 struct symbol
*rtype
)
470 struct symbol
*ctype
= NULL
;
471 if (lclass
& TYPE_RESTRICT
) {
472 if (rclass
& TYPE_RESTRICT
) {
473 if (ltype
== rtype
) {
475 } else if (lclass
& TYPE_FOULED
) {
476 if (unfoul(ltype
) == rtype
)
478 } else if (rclass
& TYPE_FOULED
) {
479 if (unfoul(rtype
) == ltype
)
483 if (!restricted_value(right
, ltype
))
486 } else if (!restricted_value(left
, rtype
))
490 switch (restricted_binop(op
, ctype
)) {
492 if ((lclass
^ rclass
) & TYPE_FOULED
)
493 ctype
= unfoul(ctype
);
496 if (!(lclass
& rclass
& TYPE_FOULED
))
508 static inline void unrestrict(struct expression
*expr
,
509 int class, struct symbol
**ctype
)
511 if (class & TYPE_RESTRICT
) {
512 if (class & TYPE_FOULED
)
513 *ctype
= unfoul(*ctype
);
514 warning(expr
->pos
, "%s degrades to integer",
515 show_typename(*ctype
));
516 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
520 static struct symbol
*usual_conversions(int op
,
521 struct expression
*left
,
522 struct expression
*right
,
523 int lclass
, int rclass
,
524 struct symbol
*ltype
,
525 struct symbol
*rtype
)
527 struct symbol
*ctype
;
529 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
531 if ((lclass
| rclass
) & TYPE_RESTRICT
)
535 if (!(lclass
& TYPE_FLOAT
)) {
536 if (!(rclass
& TYPE_FLOAT
))
537 return bigger_int_type(ltype
, rtype
);
540 } else if (rclass
& TYPE_FLOAT
) {
541 if (rtype
->rank
> ltype
->rank
)
549 ctype
= restricted_binop_type(op
, left
, right
,
550 lclass
, rclass
, ltype
, rtype
);
554 unrestrict(left
, lclass
, <ype
);
555 unrestrict(right
, rclass
, &rtype
);
560 static inline int lvalue_expression(struct expression
*expr
)
562 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
565 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
567 struct expression
*index
= expr
->right
;
568 struct symbol
*ctype
, *base
;
571 classify_type(degenerate(expr
->left
), &ctype
);
572 base
= examine_pointer_target(ctype
);
575 * An address constant +/- an integer constant expression
576 * yields an address constant again [6.6(7)].
578 if ((expr
->left
->flags
& CEF_ADDR
) && (expr
->right
->flags
& CEF_ICE
))
579 expr
->flags
= CEF_ADDR
;
582 expression_error(expr
, "missing type information");
585 if (is_function(base
)) {
586 expression_error(expr
, "arithmetics on pointers to functions");
590 /* Get the size of whatever the pointer points to */
591 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
593 if (ctype
== &null_ctype
)
597 if (multiply
== 1 && itype
->bit_size
== bits_in_pointer
)
600 if (index
->type
== EXPR_VALUE
) {
601 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
602 unsigned long long v
= index
->value
, mask
;
603 mask
= 1ULL << (itype
->bit_size
- 1);
609 mask
= 1ULL << (bits_in_pointer
- 1);
610 v
&= mask
| (mask
- 1);
612 val
->ctype
= ssize_t_ctype
;
617 if (itype
->bit_size
!= bits_in_pointer
)
618 index
= cast_to(index
, ssize_t_ctype
);
621 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
622 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
624 val
->ctype
= ssize_t_ctype
;
625 val
->value
= multiply
;
628 mul
->ctype
= ssize_t_ctype
;
638 static void examine_fn_arguments(struct symbol
*fn
);
640 #define MOD_IGN (MOD_QUALIFIER | MOD_FUN_ATTR)
642 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
643 unsigned long mod1
, unsigned long mod2
)
645 struct ident
*as1
= c1
->as
, *as2
= c2
->as
;
646 struct symbol
*t1
= c1
->base_type
;
647 struct symbol
*t2
= c2
->base_type
;
648 int move1
= 1, move2
= 1;
649 mod1
|= c1
->modifiers
;
650 mod2
|= c2
->modifiers
;
654 struct symbol
*base1
= t1
->ctype
.base_type
;
655 struct symbol
*base2
= t2
->ctype
.base_type
;
658 * FIXME! Collect alignment and context too here!
661 if (t1
&& t1
->type
!= SYM_PTR
) {
662 mod1
|= t1
->ctype
.modifiers
;
663 combine_address_space(t1
->pos
, &as1
, t1
->ctype
.as
);
669 if (t2
&& t2
->type
!= SYM_PTR
) {
670 mod2
|= t2
->ctype
.modifiers
;
671 combine_address_space(t2
->pos
, &as2
, t2
->ctype
.as
);
679 return "different types";
681 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
689 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
699 if (type
!= t2
->type
)
700 return "different base types";
704 sparse_error(t1
->pos
,
705 "internal error: bad type in derived(%d)",
709 return "different base types";
712 /* allow definition of incomplete structs and unions */
713 if (t1
->ident
== t2
->ident
)
715 return "different base types";
717 /* XXX: we ought to compare sizes */
721 return "different address spaces";
722 /* MOD_SPECIFIER is due to idiocy in parse.c */
723 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
724 return "different modifiers";
725 /* we could be lazier here */
726 base1
= examine_pointer_target(t1
);
727 base2
= examine_pointer_target(t2
);
728 mod1
= t1
->ctype
.modifiers
;
730 mod2
= t2
->ctype
.modifiers
;
734 struct symbol
*arg1
, *arg2
;
738 return "different address spaces";
739 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
740 return "different modifiers";
741 mod1
= t1
->ctype
.modifiers
;
743 mod2
= t2
->ctype
.modifiers
;
746 if (t1
->variadic
!= t2
->variadic
)
747 return "incompatible variadic arguments";
748 examine_fn_arguments(t1
);
749 examine_fn_arguments(t2
);
750 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
751 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
758 return "different argument counts";
759 diffstr
= type_difference(&arg1
->ctype
,
763 static char argdiff
[80];
764 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
771 FINISH_PTR_LIST(arg2
);
772 FINISH_PTR_LIST(arg1
);
777 return "different address spaces";
779 return "different base types";
780 if (t1
->rank
!= t2
->rank
)
781 return "different type sizes";
782 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
785 else if (diff
& ~MOD_SIGNEDNESS
)
786 return "different modifiers";
788 return "different signedness";
794 return "different address spaces";
795 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
796 return "different modifiers";
800 static void bad_null(struct expression
*expr
)
802 if (Wnon_pointer_null
)
803 warning(expr
->pos
, "Using plain integer as NULL pointer");
806 static unsigned long target_qualifiers(struct symbol
*type
)
808 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
809 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
814 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
816 const char *typediff
;
817 struct symbol
*ltype
, *rtype
;
818 struct expression
*l
= expr
->left
;
819 struct expression
*r
= expr
->right
;
820 struct symbol
*lbase
;
822 classify_type(degenerate(l
), <ype
);
823 classify_type(degenerate(r
), &rtype
);
825 lbase
= examine_pointer_target(ltype
);
826 examine_pointer_target(rtype
);
827 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
828 target_qualifiers(rtype
),
829 target_qualifiers(ltype
));
831 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
833 if (is_function(lbase
)) {
834 expression_error(expr
, "subtraction of functions? Share your drugs");
838 expr
->ctype
= ssize_t_ctype
;
839 if (lbase
->bit_size
> bits_in_char
) {
840 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
841 struct expression
*div
= expr
;
842 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
843 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
845 val
->ctype
= size_t_ctype
;
848 if (value
& (value
-1)) {
849 if (Wptr_subtraction_blows
) {
850 warning(expr
->pos
, "potentially expensive pointer subtraction");
851 info(expr
->pos
, " '%s' has a non-power-of-2 size: %lu", show_typename(lbase
), value
);
856 sub
->ctype
= ssize_t_ctype
;
865 return ssize_t_ctype
;
868 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
870 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
872 struct symbol
*ctype
;
877 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
878 warning(expr
->pos
, "assignment expression in conditional");
880 ctype
= evaluate_expression(expr
);
881 if (!valid_type(ctype
))
883 if (is_safe_type(ctype
))
884 warning(expr
->pos
, "testing a 'safe expression'");
885 if (is_func_type(ctype
)) {
887 warning(expr
->pos
, "the address of %s will always evaluate as true", "a function");
888 } else if (is_array_type(ctype
)) {
890 warning(expr
->pos
, "the address of %s will always evaluate as true", "an array");
891 } else if (!is_scalar_type(ctype
)) {
892 sparse_error(expr
->pos
, "non-scalar type in conditional:");
893 info(expr
->pos
, " %s", show_typename(ctype
));
897 ctype
= degenerate(expr
);
901 static struct symbol
*evaluate_logical(struct expression
*expr
)
903 if (!evaluate_conditional(expr
->left
, 0))
905 if (!evaluate_conditional(expr
->right
, 0))
908 /* the result is int [6.5.13(3), 6.5.14(3)] */
909 expr
->ctype
= &int_ctype
;
910 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
911 expr
->flags
&= ~(CEF_CONST_MASK
| CEF_ADDR
);
915 static struct symbol
*evaluate_binop(struct expression
*expr
)
917 struct symbol
*ltype
, *rtype
, *ctype
;
918 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
919 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
922 /* number op number */
923 if (lclass
& rclass
& TYPE_NUM
) {
924 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
925 expr
->flags
&= ~CEF_CONST_MASK
;
927 if ((lclass
| rclass
) & TYPE_FLOAT
) {
929 case '+': case '-': case '*': case '/':
932 return bad_expr_type(expr
);
936 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
937 // shifts do integer promotions, but that's it.
938 unrestrict(expr
->left
, lclass
, <ype
);
939 unrestrict(expr
->right
, rclass
, &rtype
);
940 ctype
= ltype
= integer_promotion(ltype
);
941 rtype
= integer_promotion(rtype
);
943 // The rest do usual conversions
944 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
945 && expr
->left
->op
== '!';
946 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
947 && expr
->right
->op
== '!';
948 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
949 warning(expr
->pos
, "dubious: %sx %c %sy",
952 right_not
? "!" : "");
954 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
955 lclass
, rclass
, ltype
, rtype
);
956 ctype
= rtype
= ltype
;
959 expr
->left
= cast_to(expr
->left
, ltype
);
960 expr
->right
= cast_to(expr
->right
, rtype
);
965 /* pointer (+|-) integer */
966 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
967 unrestrict(expr
->right
, rclass
, &rtype
);
968 return evaluate_ptr_add(expr
, rtype
);
971 /* integer + pointer */
972 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
973 struct expression
*index
= expr
->left
;
974 unrestrict(index
, lclass
, <ype
);
975 expr
->left
= expr
->right
;
977 return evaluate_ptr_add(expr
, ltype
);
980 /* pointer - pointer */
981 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
982 return evaluate_ptr_sub(expr
);
984 return bad_expr_type(expr
);
987 static struct symbol
*evaluate_comma(struct expression
*expr
)
989 expr
->ctype
= unqualify_type(degenerate(expr
->right
));
990 if (expr
->ctype
== &null_ctype
)
991 expr
->ctype
= &ptr_ctype
;
992 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
996 static int modify_for_unsigned(int op
)
999 op
= SPECIAL_UNSIGNED_LT
;
1001 op
= SPECIAL_UNSIGNED_GT
;
1002 else if (op
== SPECIAL_LTE
)
1003 op
= SPECIAL_UNSIGNED_LTE
;
1004 else if (op
== SPECIAL_GTE
)
1005 op
= SPECIAL_UNSIGNED_GTE
;
1009 enum null_constant_type
{
1015 static inline int is_null_pointer_constant(struct expression
*e
)
1017 if (e
->ctype
== &null_ctype
)
1019 if (!(e
->flags
& CEF_ICE
))
1021 return is_zero_constant(e
) ? NULL_ZERO
: NON_NULL
;
1024 static struct symbol
*evaluate_compare(struct expression
*expr
)
1026 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1027 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
1028 int lclass
= classify_type(degenerate(left
), <ype
);
1029 int rclass
= classify_type(degenerate(right
), &rtype
);
1030 struct symbol
*ctype
;
1031 const char *typediff
;
1034 if (is_type_type(ltype
) && is_type_type(rtype
)) {
1036 * __builtin_types_compatible_p() yields an integer
1037 * constant expression
1039 expr
->flags
= CEF_SET_ICE
;
1043 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1044 warning(expr
->pos
, "testing a 'safe expression'");
1046 expr
->flags
= left
->flags
& right
->flags
& ~CEF_CONST_MASK
& ~CEF_ADDR
;
1048 /* number on number */
1049 if (lclass
& rclass
& TYPE_NUM
) {
1050 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1051 lclass
, rclass
, ltype
, rtype
);
1052 expr
->left
= cast_to(expr
->left
, ctype
);
1053 expr
->right
= cast_to(expr
->right
, ctype
);
1054 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1055 expr
->op
= modify_for_unsigned(expr
->op
);
1059 /* at least one must be a pointer */
1060 if (!((lclass
| rclass
) & TYPE_PTR
))
1061 return bad_expr_type(expr
);
1063 /* equality comparisons can be with null pointer constants */
1064 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1065 int is_null1
= is_null_pointer_constant(left
);
1066 int is_null2
= is_null_pointer_constant(right
);
1067 if (is_null1
== NULL_ZERO
)
1069 if (is_null2
== NULL_ZERO
)
1071 if (is_null1
&& is_null2
) {
1072 int positive
= expr
->op
== SPECIAL_EQUAL
;
1073 expr
->type
= EXPR_VALUE
;
1074 expr
->value
= positive
;
1077 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1078 expr
->left
= cast_to(left
, rtype
);
1081 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1082 expr
->right
= cast_to(right
, ltype
);
1086 /* both should be pointers */
1087 if (!(lclass
& rclass
& TYPE_PTR
))
1088 return bad_expr_type(expr
);
1089 expr
->op
= modify_for_unsigned(expr
->op
);
1091 lbase
= examine_pointer_target(ltype
);
1092 rbase
= examine_pointer_target(rtype
);
1094 /* they also have special treatment for pointers to void */
1095 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1096 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1097 if (lbase
== &void_ctype
) {
1098 expr
->right
= cast_to(right
, ltype
);
1101 if (rbase
== &void_ctype
) {
1102 expr
->left
= cast_to(left
, rtype
);
1108 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1109 target_qualifiers(rtype
),
1110 target_qualifiers(ltype
));
1114 expression_error(expr
, "incompatible types in comparison expression (%s):", typediff
);
1115 info(expr
->pos
, " %s", show_typename(ltype
));
1116 info(expr
->pos
, " %s", show_typename(rtype
));
1120 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1121 expr
->ctype
= &int_ctype
;
1126 * NOTE! The degenerate case of "x ? : y", where we don't
1127 * have a true case, this will possibly promote "x" to the
1128 * same type as "y", and thus _change_ the conditional
1129 * test in the expression. But since promotion is "safe"
1130 * for testing, that's OK.
1132 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1134 struct expression
**cond
;
1135 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1137 const char * typediff
;
1140 if (!evaluate_conditional(expr
->conditional
, 0))
1142 if (!evaluate_expression(expr
->cond_false
))
1145 ctype
= degenerate(expr
->conditional
);
1146 rtype
= degenerate(expr
->cond_false
);
1148 cond
= &expr
->conditional
;
1150 if (expr
->cond_true
) {
1151 if (!evaluate_expression(expr
->cond_true
))
1153 ltype
= degenerate(expr
->cond_true
);
1154 cond
= &expr
->cond_true
;
1157 expr
->flags
= (expr
->conditional
->flags
& (*cond
)->flags
&
1158 expr
->cond_false
->flags
& ~CEF_CONST_MASK
);
1160 * In the standard, it is defined that an integer constant expression
1161 * shall only have operands that are themselves constant [6.6(6)].
1162 * While this definition is very clear for expressions that need all
1163 * their operands to be evaluated, for conditional expressions with a
1164 * constant condition things are much less obvious.
1165 * So, as an extension, do the same as GCC seems to do:
1166 * Consider a conditional expression with a constant condition
1167 * as having the same constantness as the argument corresponding
1168 * to the truth value (including in the case of address constants
1169 * which are defined more stricly [6.6(9)]).
1171 if (expr
->conditional
->flags
& (CEF_ACE
| CEF_ADDR
)) {
1172 int is_true
= expr_truth_value(expr
->conditional
);
1173 struct expression
*arg
= is_true
? *cond
: expr
->cond_false
;
1174 expr
->flags
= arg
->flags
& ~CEF_CONST_MASK
;
1177 lclass
= classify_type(ltype
, <ype
);
1178 rclass
= classify_type(rtype
, &rtype
);
1179 if (lclass
& rclass
& TYPE_NUM
) {
1180 ctype
= usual_conversions('?', *cond
, expr
->cond_false
,
1181 lclass
, rclass
, ltype
, rtype
);
1182 *cond
= cast_to(*cond
, ctype
);
1183 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1187 if ((lclass
| rclass
) & TYPE_PTR
) {
1188 int is_null1
= is_null_pointer_constant(*cond
);
1189 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1191 if (is_null1
&& is_null2
) {
1192 *cond
= cast_to(*cond
, &ptr_ctype
);
1193 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1197 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1198 if (is_null1
== NULL_ZERO
)
1200 *cond
= cast_to(*cond
, rtype
);
1204 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1205 if (is_null2
== NULL_ZERO
)
1206 bad_null(expr
->cond_false
);
1207 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1211 if (!(lclass
& rclass
& TYPE_PTR
)) {
1212 typediff
= "different types";
1215 /* OK, it's pointer on pointer */
1216 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1217 typediff
= "different address spaces";
1221 /* need to be lazier here */
1222 lbase
= examine_pointer_target(ltype
);
1223 rbase
= examine_pointer_target(rtype
);
1224 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1226 if (lbase
== &void_ctype
) {
1227 /* XXX: pointers to function should warn here */
1232 if (rbase
== &void_ctype
) {
1233 /* XXX: pointers to function should warn here */
1237 /* XXX: that should be pointer to composite */
1239 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1246 /* void on void, struct on same struct, union on same union */
1247 if (ltype
== rtype
) {
1251 typediff
= "different base types";
1254 expression_error(expr
, "incompatible types in conditional expression (%s):", typediff
);
1255 info(expr
->pos
, " %s", show_typename(ltype
));
1256 info(expr
->pos
, " %s", show_typename(rtype
));
1258 * if the condition is constant, the type is in fact known
1259 * so use it, as gcc & clang do.
1261 switch (expr_truth_value(expr
->conditional
)) {
1262 case 1: expr
->ctype
= ltype
;
1264 case 0: expr
->ctype
= rtype
;
1272 expr
->ctype
= ctype
;
1276 if (qual
& ~ctype
->ctype
.modifiers
) {
1277 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1279 sym
->ctype
.modifiers
|= qual
;
1282 *cond
= cast_to(*cond
, ctype
);
1283 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1287 /* FP assignments can not do modulo or bit operations */
1288 static int compatible_float_op(int op
)
1290 return op
== SPECIAL_ADD_ASSIGN
||
1291 op
== SPECIAL_SUB_ASSIGN
||
1292 op
== SPECIAL_MUL_ASSIGN
||
1293 op
== SPECIAL_DIV_ASSIGN
;
1296 static int evaluate_assign_op(struct expression
*expr
)
1298 struct symbol
*target
= expr
->left
->ctype
;
1299 struct symbol
*source
= expr
->right
->ctype
;
1300 struct symbol
*t
, *s
;
1301 int tclass
= classify_type(target
, &t
);
1302 int sclass
= classify_type(source
, &s
);
1305 if (tclass
& sclass
& TYPE_NUM
) {
1306 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1307 expression_error(expr
, "invalid assignment");
1310 if (tclass
& TYPE_RESTRICT
) {
1311 if (!restricted_binop(op
, t
)) {
1312 warning(expr
->pos
, "bad assignment (%s) to %s",
1313 show_special(op
), show_typename(t
));
1314 expr
->right
= cast_to(expr
->right
, target
);
1317 /* allowed assignments unfoul */
1318 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1320 if (!restricted_value(expr
->right
, t
))
1322 } else if (op
== SPECIAL_SHR_ASSIGN
|| op
== SPECIAL_SHL_ASSIGN
) {
1323 // shifts do integer promotions, but that's it.
1324 unrestrict(expr
->left
, tclass
, &t
);
1325 target
= integer_promotion(t
);
1327 unrestrict(expr
->right
, sclass
, &s
);
1328 source
= integer_promotion(s
);
1329 expr
->right
= cast_to(expr
->right
, source
);
1331 // both gcc & clang seems to do this, so ...
1332 if (target
->bit_size
> source
->bit_size
)
1333 expr
->right
= cast_to(expr
->right
, &uint_ctype
);
1336 } else if (!(sclass
& TYPE_RESTRICT
))
1338 /* source and target would better be identical restricted */
1341 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1342 info(expr
->pos
, " left side has type %s", show_typename(t
));
1343 info(expr
->pos
, " right side has type %s", show_typename(s
));
1344 expr
->right
= cast_to(expr
->right
, target
);
1347 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1348 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1349 unrestrict(expr
->right
, sclass
, &s
);
1350 evaluate_ptr_add(expr
, s
);
1353 expression_error(expr
, "invalid pointer assignment");
1357 expression_error(expr
, "invalid assignment");
1361 target
= usual_conversions(op
, expr
->left
, expr
->right
,
1362 tclass
, sclass
, target
, source
);
1364 expr
->right
= cast_to(expr
->right
, target
);
1368 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1371 return 0; /* yes, 0 - we don't want a cast_to here */
1372 if (t1
== &void_ctype
)
1374 if (t2
== &void_ctype
)
1376 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1378 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1382 if (t1
->rank
== -2 && t2
->rank
== -2)
1384 if (t1
->rank
!= t2
->rank
)
1389 static int check_assignment_types(struct symbol
*target
, struct expression
**rp
,
1390 const char **typediff
)
1392 struct symbol
*source
= degenerate(*rp
);
1393 struct symbol
*t
, *s
;
1394 int tclass
= classify_type(target
, &t
);
1395 int sclass
= classify_type(source
, &s
);
1397 if (tclass
& sclass
& TYPE_NUM
) {
1398 if (tclass
& TYPE_RESTRICT
) {
1399 /* allowed assignments unfoul */
1400 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1402 if (!restricted_value(*rp
, target
))
1406 } else if (!(sclass
& TYPE_RESTRICT
))
1408 if (t
== &bool_ctype
) {
1409 if (is_fouled_type(s
))
1410 warning((*rp
)->pos
, "%s degrades to integer",
1411 show_typename(s
->ctype
.base_type
));
1414 *typediff
= "different base types";
1418 if (tclass
== TYPE_PTR
) {
1419 unsigned long mod1
, mod2
;
1420 unsigned long modl
, modr
;
1421 struct symbol
*b1
, *b2
;
1422 // NULL pointer is always OK
1423 int is_null
= is_null_pointer_constant(*rp
);
1425 if (is_null
== NULL_ZERO
)
1429 if (!(sclass
& TYPE_PTR
)) {
1430 *typediff
= "different base types";
1433 b1
= examine_pointer_target(t
);
1434 b2
= examine_pointer_target(s
);
1435 mod1
= t
->ctype
.modifiers
& MOD_IGN
;
1436 mod2
= s
->ctype
.modifiers
& MOD_IGN
;
1437 if (whitelist_pointers(b1
, b2
)) {
1439 * assignments to/from void * are OK, provided that
1440 * we do not remove qualifiers from pointed to [C]
1441 * or mix address spaces [sparse].
1443 if (t
->ctype
.as
!= s
->ctype
.as
) {
1444 *typediff
= "different address spaces";
1448 * If this is a function pointer assignment, it is
1449 * actually fine to assign a pointer to const data to
1450 * it, as a function pointer points to const data
1451 * implicitly, i.e., dereferencing it does not produce
1454 if (b1
->type
== SYM_FN
)
1456 if (mod2
& ~mod1
& ~MOD_FUN_ATTR
) {
1457 *typediff
= "different modifiers";
1462 /* It's OK if the target is more volatile or const than the source */
1463 /* It's OK if the source is more pure/noreturn than the target */
1464 modr
= mod1
& ~MOD_REV_QUAL
;
1465 modl
= mod2
& MOD_REV_QUAL
;
1466 *typediff
= type_difference(&t
->ctype
, &s
->ctype
, modl
, modr
);
1472 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1475 if (tclass
& TYPE_NUM
) {
1476 /* XXX: need to turn into comparison with NULL */
1477 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1479 *typediff
= "different base types";
1482 *typediff
= "invalid types";
1486 *rp
= cast_to(*rp
, target
);
1490 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1491 struct expression
**rp
, const char *where
)
1493 const char *typediff
;
1495 if (!check_assignment_types(target
, rp
, &typediff
)) {
1496 struct symbol
*source
= *rp
? (*rp
)->ctype
: NULL
;
1497 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1498 info(expr
->pos
, " expected %s", show_typename(target
));
1499 info(expr
->pos
, " got %s", show_typename(source
));
1500 *rp
= cast_to(*rp
, target
);
1507 static int compatible_transparent_union(struct symbol
*target
,
1508 struct expression
**rp
)
1510 struct symbol
*t
, *member
;
1511 classify_type(target
, &t
);
1512 if (t
->type
!= SYM_UNION
|| !t
->transparent_union
)
1515 FOR_EACH_PTR(t
->symbol_list
, member
) {
1516 const char *typediff
;
1517 if (check_assignment_types(member
, rp
, &typediff
))
1519 } END_FOR_EACH_PTR(member
);
1524 static int compatible_argument_type(struct expression
*expr
, struct symbol
*target
,
1525 struct expression
**rp
, const char *where
)
1527 if (compatible_transparent_union(target
, rp
))
1530 return compatible_assignment_types(expr
, target
, rp
, where
);
1533 static void mark_addressable(struct expression
*expr
)
1535 while (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
1537 if (expr
->type
== EXPR_SYMBOL
) {
1538 struct symbol
*sym
= expr
->symbol
;
1539 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1543 static void mark_assigned(struct expression
*expr
)
1549 switch (expr
->type
) {
1554 if (sym
->type
!= SYM_NODE
)
1556 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1560 mark_assigned(expr
->left
);
1561 mark_assigned(expr
->right
);
1564 case EXPR_FORCE_CAST
:
1565 mark_assigned(expr
->cast_expression
);
1568 mark_assigned(expr
->base
);
1576 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1578 if (type
->ctype
.modifiers
& MOD_CONST
)
1579 expression_error(left
, "assignment to const expression");
1581 /* We know left is an lvalue, so it's a "preop-*" */
1582 mark_assigned(left
->unop
);
1585 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1587 struct expression
*left
= expr
->left
;
1588 struct symbol
*ltype
;
1590 if (!lvalue_expression(left
)) {
1591 expression_error(expr
, "not an lvalue");
1595 ltype
= left
->ctype
;
1597 if (expr
->op
!= '=') {
1598 if (!evaluate_assign_op(expr
))
1601 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, "assignment"))
1605 evaluate_assign_to(left
, ltype
);
1607 expr
->ctype
= ltype
;
1611 static void examine_fn_arguments(struct symbol
*fn
)
1615 FOR_EACH_PTR(fn
->arguments
, s
) {
1616 struct symbol
*arg
= evaluate_symbol(s
);
1617 /* Array/function arguments silently degenerate into pointers */
1623 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1624 if (arg
->type
== SYM_ARRAY
)
1625 ptr
->ctype
= arg
->ctype
;
1627 ptr
->ctype
.base_type
= arg
;
1628 combine_address_space(s
->pos
, &ptr
->ctype
.as
, s
->ctype
.as
);
1629 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1631 s
->ctype
.base_type
= ptr
;
1633 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1636 examine_symbol_type(s
);
1643 } END_FOR_EACH_PTR(s
);
1646 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, struct ident
*as
, int mod
)
1648 /* Take the modifiers of the pointer, and apply them to the member */
1649 mod
|= sym
->ctype
.modifiers
;
1650 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1651 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1653 newsym
->ctype
.as
= as
;
1654 newsym
->ctype
.modifiers
= mod
;
1660 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1662 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1663 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1665 node
->ctype
.base_type
= ptr
;
1666 ptr
->bit_size
= bits_in_pointer
;
1667 ptr
->ctype
.alignment
= pointer_alignment
;
1669 node
->bit_size
= bits_in_pointer
;
1670 node
->ctype
.alignment
= pointer_alignment
;
1673 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1674 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1675 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1677 if (sym
->type
== SYM_NODE
) {
1678 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1679 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1680 sym
= sym
->ctype
.base_type
;
1682 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1683 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1684 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1685 sym
= sym
->ctype
.base_type
;
1687 ptr
->ctype
.base_type
= sym
;
1692 /* Arrays degenerate into pointers on pointer arithmetic */
1693 static struct symbol
*degenerate(struct expression
*expr
)
1695 struct symbol
*ctype
, *base
;
1699 ctype
= expr
->ctype
;
1702 base
= examine_symbol_type(ctype
);
1703 if (ctype
->type
== SYM_NODE
)
1704 base
= ctype
->ctype
.base_type
;
1706 * Arrays degenerate into pointers to the entries, while
1707 * functions degenerate into pointers to themselves.
1708 * If array was part of non-lvalue compound, we create a copy
1709 * of that compound first and then act as if we were dealing with
1710 * the corresponding field in there.
1712 switch (base
->type
) {
1714 if (expr
->type
== EXPR_SLICE
) {
1715 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1716 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1718 a
->ctype
.base_type
= expr
->base
->ctype
;
1719 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1720 a
->array_size
= expr
->base
->ctype
->array_size
;
1722 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1724 e0
->ctype
= &lazy_ptr_ctype
;
1726 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1729 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1731 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1733 e2
->right
= expr
->base
;
1735 e2
->ctype
= expr
->base
->ctype
;
1737 if (expr
->r_bitpos
) {
1738 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1741 e3
->right
= alloc_const_expression(expr
->pos
,
1742 bits_to_bytes(expr
->r_bitpos
));
1743 e3
->ctype
= &lazy_ptr_ctype
;
1748 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1751 e4
->ctype
= &lazy_ptr_ctype
;
1754 expr
->type
= EXPR_PREOP
;
1758 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1759 expression_error(expr
, "strange non-value function or array");
1763 sparse_error(expr
->pos
, "taking the address of built-in function '%s'", show_ident(ctype
->ident
));
1764 *expr
= *expr
->unop
;
1765 ctype
= create_pointer(expr
, ctype
, 1);
1766 expr
->ctype
= ctype
;
1767 mark_addressable(expr
);
1774 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1776 struct expression
*op
= expr
->unop
;
1777 struct symbol
*ctype
;
1779 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1780 expression_error(expr
, "not addressable");
1785 sparse_error(expr
->pos
, "taking the address of built-in function '%s'", show_ident(ctype
->ident
));
1788 mark_addressable(expr
);
1791 * symbol expression evaluation is lazy about the type
1792 * of the sub-expression, so we may have to generate
1793 * the type here if so..
1795 if (expr
->ctype
== &lazy_ptr_ctype
) {
1796 ctype
= create_pointer(expr
, ctype
, 0);
1797 expr
->ctype
= ctype
;
1803 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1805 struct expression
*op
= expr
->unop
;
1806 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1808 /* Simplify: *&(expr) => (expr) */
1809 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1811 expr
->flags
= CEF_NONE
;
1815 examine_symbol_type(ctype
);
1817 /* Dereferencing a node drops all the node information. */
1818 if (ctype
->type
== SYM_NODE
)
1819 ctype
= ctype
->ctype
.base_type
;
1821 target
= ctype
->ctype
.base_type
;
1823 switch (ctype
->type
) {
1825 expression_error(expr
, "cannot dereference this type");
1831 examine_symbol_type(target
);
1832 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1833 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1834 merge_type(node
, ctype
);
1838 if (!lvalue_expression(op
)) {
1839 expression_error(op
, "non-lvalue array??");
1843 /* Do the implied "addressof" on the array */
1847 * When an array is dereferenced, we need to pick
1848 * up the attributes of the original node too..
1850 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1851 merge_type(node
, op
->ctype
);
1852 merge_type(node
, ctype
);
1856 node
->bit_size
= target
->bit_size
;
1857 node
->array_size
= target
->array_size
;
1864 * Unary post-ops: x++ and x--
1866 static struct symbol
*evaluate_postop(struct expression
*expr
)
1868 struct expression
*op
= expr
->unop
;
1869 struct symbol
*ctype
= op
->ctype
;
1870 int class = classify_type(ctype
, &ctype
);
1873 if (!class || class & TYPE_COMPOUND
) {
1874 expression_error(expr
, "need scalar for ++/--");
1877 if (!lvalue_expression(expr
->unop
)) {
1878 expression_error(expr
, "need lvalue expression for ++/--");
1882 unrestrict(expr
, class, &ctype
);
1884 if (class & TYPE_NUM
) {
1886 } else if (class == TYPE_PTR
) {
1887 struct symbol
*target
= examine_pointer_target(ctype
);
1888 if (!is_function(target
))
1889 multiply
= bits_to_bytes(target
->bit_size
);
1893 evaluate_assign_to(op
, op
->ctype
);
1894 expr
->op_value
= multiply
;
1895 expr
->ctype
= ctype
;
1899 expression_error(expr
, "bad argument type for ++/--");
1903 static struct symbol
*evaluate_sign(struct expression
*expr
)
1905 struct symbol
*ctype
= expr
->unop
->ctype
;
1906 int class = classify_type(ctype
, &ctype
);
1907 unsigned char flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1909 /* should be an arithmetic type */
1910 if (!(class & TYPE_NUM
))
1911 return bad_expr_type(expr
);
1912 if (class & TYPE_RESTRICT
)
1915 if (!(class & TYPE_FLOAT
)) {
1916 ctype
= integer_promotion(ctype
);
1917 expr
->unop
= cast_to(expr
->unop
, ctype
);
1918 } else if (expr
->op
!= '~') {
1919 /* no conversions needed */
1921 return bad_expr_type(expr
);
1923 if (expr
->op
== '+')
1924 *expr
= *expr
->unop
;
1925 expr
->flags
= flags
;
1926 expr
->ctype
= ctype
;
1929 if (restricted_unop(expr
->op
, &ctype
))
1930 unrestrict(expr
, class, &ctype
);
1934 static struct symbol
*evaluate_preop(struct expression
*expr
)
1936 struct symbol
*ctype
= expr
->unop
->ctype
;
1940 *expr
= *expr
->unop
;
1946 return evaluate_sign(expr
);
1949 return evaluate_dereference(expr
);
1952 return evaluate_addressof(expr
);
1954 case SPECIAL_INCREMENT
:
1955 case SPECIAL_DECREMENT
:
1957 * From a type evaluation standpoint the preops are
1958 * the same as the postops
1960 return evaluate_postop(expr
);
1963 ctype
= degenerate(expr
->unop
);
1964 expr
->flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1966 * A logical negation never yields an address constant
1969 expr
->flags
&= ~CEF_ADDR
;
1971 if (is_safe_type(ctype
))
1972 warning(expr
->pos
, "testing a 'safe expression'");
1973 if (is_float_type(ctype
)) {
1974 struct expression
*arg
= expr
->unop
;
1975 expr
->type
= EXPR_COMPARE
;
1976 expr
->op
= SPECIAL_EQUAL
;
1978 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1979 expr
->right
->ctype
= ctype
;
1980 expr
->right
->fvalue
= 0;
1981 } else if (is_fouled_type(ctype
)) {
1982 warning(expr
->pos
, "%s degrades to integer",
1983 show_typename(ctype
->ctype
.base_type
));
1985 /* the result is int [6.5.3.3(5)]*/
1992 expr
->ctype
= ctype
;
1996 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1998 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1999 struct ptr_list
*list
= head
;
2005 for (i
= 0; i
< list
->nr
; i
++) {
2006 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
2008 if (sym
->ident
!= ident
)
2010 *offset
= sym
->offset
;
2013 struct symbol
*ctype
= sym
->ctype
.base_type
;
2017 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
2019 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
2022 *offset
+= sym
->offset
;
2026 } while ((list
= list
->next
) != head
);
2030 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
2032 struct expression
*add
;
2035 * Create a new add-expression
2037 * NOTE! Even if we just add zero, we need a new node
2038 * for the member pointer, since it has a different
2039 * type than the original pointer. We could make that
2040 * be just a cast, but the fact is, a node is a node,
2041 * so we might as well just do the "add zero" here.
2043 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
2046 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2047 add
->right
->ctype
= &int_ctype
;
2048 add
->right
->value
= offset
;
2051 * The ctype of the pointer will be lazily evaluated if
2052 * we ever take the address of this member dereference..
2054 add
->ctype
= &lazy_ptr_ctype
;
2056 * The resulting address of a member access through an address
2057 * constant is an address constant again [6.6(9)].
2059 add
->flags
= expr
->flags
;
2064 /* structure/union dereference */
2065 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
2068 struct symbol
*ctype
, *member
;
2069 struct expression
*deref
= expr
->deref
, *add
;
2070 struct ident
*ident
= expr
->member
;
2071 struct ident
*address_space
;
2074 if (!evaluate_expression(deref
))
2077 expression_error(expr
, "bad member name");
2081 ctype
= deref
->ctype
;
2082 examine_symbol_type(ctype
);
2083 address_space
= ctype
->ctype
.as
;
2084 mod
= ctype
->ctype
.modifiers
;
2085 if (ctype
->type
== SYM_NODE
) {
2086 ctype
= ctype
->ctype
.base_type
;
2087 combine_address_space(deref
->pos
, &address_space
, ctype
->ctype
.as
);
2088 mod
|= ctype
->ctype
.modifiers
;
2090 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
2091 expression_error(expr
, "expected structure or union");
2095 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
2097 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
2098 const char *name
= "<unnamed>";
2101 name
= ctype
->ident
->name
;
2102 namelen
= ctype
->ident
->len
;
2104 if (ctype
->symbol_list
)
2105 expression_error(expr
, "no member '%s' in %s %.*s",
2106 show_ident(ident
), type
, namelen
, name
);
2108 expression_error(expr
, "using member '%s' in "
2109 "incomplete %s %.*s", show_ident(ident
),
2110 type
, namelen
, name
);
2115 * The member needs to take on the address space and modifiers of
2116 * the "parent" type.
2118 member
= convert_to_as_mod(member
, address_space
, mod
);
2119 ctype
= get_base_type(member
);
2121 if (!lvalue_expression(deref
)) {
2122 if (deref
->type
!= EXPR_SLICE
) {
2126 expr
->base
= deref
->base
;
2127 expr
->r_bitpos
= deref
->r_bitpos
;
2129 expr
->r_bitpos
+= bytes_to_bits(offset
);
2130 expr
->type
= EXPR_SLICE
;
2131 expr
->r_bitpos
+= member
->bit_offset
;
2132 expr
->ctype
= member
;
2136 deref
= deref
->unop
;
2137 expr
->deref
= deref
;
2139 add
= evaluate_offset(deref
, offset
);
2140 expr
->type
= EXPR_PREOP
;
2144 expr
->ctype
= member
;
2148 static int is_promoted(struct expression
*expr
)
2151 switch (expr
->type
) {
2154 case EXPR_CONDITIONAL
:
2178 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2180 struct symbol
*sym
= expr
->cast_type
;
2182 sym
= evaluate_expression(expr
->cast_expression
);
2186 * Expressions of restricted types will possibly get
2187 * promoted - check that here
2189 if (is_restricted_type(sym
)) {
2190 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2192 } else if (is_fouled_type(sym
)) {
2196 examine_symbol_type(sym
);
2197 if (is_bitfield_type(sym
)) {
2198 expression_error(expr
, "trying to examine bitfield type");
2204 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2206 struct symbol
*type
;
2209 type
= evaluate_type_information(expr
);
2213 size
= type
->bit_size
;
2215 if (size
< 0 && is_void_type(type
)) {
2217 warning(expr
->pos
, "expression using sizeof(void)");
2218 size
= bits_in_char
;
2221 if (is_bool_type(type
)) {
2223 warning(expr
->pos
, "expression using sizeof _Bool");
2224 size
= bits_to_bytes(bits_in_bool
) * bits_in_char
;
2227 if (is_function(type
->ctype
.base_type
)) {
2229 warning(expr
->pos
, "expression using sizeof on a function");
2230 size
= bits_in_char
;
2233 if (has_flexible_array(type
) && Wflexible_array_sizeof
)
2234 warning(expr
->pos
, "using sizeof on a flexible structure");
2236 if (is_array_type(type
) && size
< 0) { // VLA, 1-dimension only
2237 struct expression
*base
, *size
;
2238 struct symbol
*base_type
;
2240 if (type
->type
== SYM_NODE
)
2241 type
= type
->ctype
.base_type
; // strip the SYM_NODE
2242 base_type
= get_base_type(type
);
2245 if (base_type
->bit_size
<= 0) {
2246 base
= alloc_expression(expr
->pos
, EXPR_SIZEOF
);
2247 base
->cast_type
= base_type
;
2248 if (!evaluate_sizeof(base
))
2251 base
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2252 base
->value
= bits_to_bytes(base_type
->bit_size
);
2253 base
->ctype
= size_t_ctype
;
2255 size
= alloc_expression(expr
->pos
, EXPR_CAST
);
2256 size
->cast_type
= size_t_ctype
;
2257 size
->cast_expression
= type
->array_size
;
2258 if (!evaluate_expression(size
))
2262 expr
->type
= EXPR_BINOP
;
2264 return expr
->ctype
= size_t_ctype
;
2268 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2269 expression_error(expr
, "cannot size expression");
2271 expr
->type
= EXPR_VALUE
;
2272 expr
->value
= bits_to_bytes(size
);
2274 expr
->ctype
= size_t_ctype
;
2275 return size_t_ctype
;
2278 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2280 struct symbol
*type
;
2283 type
= evaluate_type_information(expr
);
2287 if (type
->type
== SYM_NODE
)
2288 type
= type
->ctype
.base_type
;
2291 switch (type
->type
) {
2295 type
= get_base_type(type
);
2299 expression_error(expr
, "expected pointer expression");
2302 size
= type
->bit_size
;
2303 if (size
& (bits_in_char
-1))
2305 expr
->type
= EXPR_VALUE
;
2306 expr
->value
= bits_to_bytes(size
);
2308 expr
->ctype
= size_t_ctype
;
2309 return size_t_ctype
;
2312 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2314 struct symbol
*type
;
2316 type
= evaluate_type_information(expr
);
2320 expr
->type
= EXPR_VALUE
;
2321 expr
->value
= type
->ctype
.alignment
;
2323 expr
->ctype
= size_t_ctype
;
2324 return size_t_ctype
;
2327 int evaluate_arguments(struct symbol_list
*argtypes
, struct expression_list
*head
)
2329 struct expression
*expr
;
2330 struct symbol
*argtype
;
2333 PREPARE_PTR_LIST(argtypes
, argtype
);
2334 FOR_EACH_PTR (head
, expr
) {
2335 struct expression
**p
= THIS_ADDRESS(expr
);
2336 struct symbol
*ctype
, *target
;
2337 ctype
= evaluate_expression(expr
);
2344 struct symbol
*type
;
2345 int class = classify_type(ctype
, &type
);
2346 if (is_int(class)) {
2347 *p
= cast_to(expr
, integer_promotion(type
));
2348 } else if (class & TYPE_FLOAT
) {
2350 *p
= cast_to(expr
, &double_ctype
);
2351 } else if (class & TYPE_PTR
) {
2352 if (expr
->ctype
== &null_ctype
)
2353 *p
= cast_to(expr
, &ptr_ctype
);
2357 } else if (!target
->forced_arg
){
2358 static char where
[30];
2359 examine_symbol_type(target
);
2360 sprintf(where
, "argument %d", i
);
2361 compatible_argument_type(expr
, target
, p
, where
);
2365 NEXT_PTR_LIST(argtype
);
2366 } END_FOR_EACH_PTR(expr
);
2367 FINISH_PTR_LIST(argtype
);
2371 static void convert_index(struct expression
*e
)
2373 struct expression
*child
= e
->idx_expression
;
2374 unsigned from
= e
->idx_from
;
2375 unsigned to
= e
->idx_to
+ 1;
2377 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2378 e
->init_nr
= to
- from
;
2379 e
->init_expr
= child
;
2382 static void convert_ident(struct expression
*e
)
2384 struct expression
*child
= e
->ident_expression
;
2385 int offset
= e
->offset
;
2388 e
->init_offset
= offset
;
2390 e
->init_expr
= child
;
2393 static void convert_designators(struct expression
*e
)
2396 if (e
->type
== EXPR_INDEX
)
2398 else if (e
->type
== EXPR_IDENTIFIER
)
2406 static void excess(struct expression
*e
, const char *s
)
2408 warning(e
->pos
, "excessive elements in %s initializer", s
);
2412 * implicit designator for the first element
2414 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2415 struct expression
**v
)
2417 struct expression
*e
= *v
, *new;
2419 if (ctype
->type
== SYM_NODE
)
2420 ctype
= ctype
->ctype
.base_type
;
2422 if (class & TYPE_PTR
) { /* array */
2423 if (!ctype
->bit_size
)
2425 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2426 new->idx_expression
= e
;
2427 new->ctype
= ctype
->ctype
.base_type
;
2429 struct symbol
*field
, *p
;
2430 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2431 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2437 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2438 new->ident_expression
= e
;
2439 new->field
= new->ctype
= field
;
2440 new->offset
= field
->offset
;
2447 * sanity-check explicit designators; return the innermost one or NULL
2448 * in case of error. Assign types.
2450 static struct expression
*check_designators(struct expression
*e
,
2451 struct symbol
*ctype
)
2453 struct expression
*last
= NULL
;
2456 if (ctype
->type
== SYM_NODE
)
2457 ctype
= ctype
->ctype
.base_type
;
2458 if (e
->type
== EXPR_INDEX
) {
2459 struct symbol
*type
;
2460 if (ctype
->type
!= SYM_ARRAY
) {
2461 err
= "array index in non-array";
2464 type
= ctype
->ctype
.base_type
;
2465 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2466 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2467 if (offset
>= ctype
->bit_size
) {
2468 err
= "index out of bounds in";
2472 e
->ctype
= ctype
= type
;
2475 if (!e
->idx_expression
) {
2479 e
= e
->idx_expression
;
2480 } else if (e
->type
== EXPR_IDENTIFIER
) {
2482 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2483 err
= "field name not in struct or union";
2486 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2488 err
= "unknown field name in";
2492 e
->field
= e
->ctype
= ctype
;
2494 if (!e
->ident_expression
) {
2498 e
= e
->ident_expression
;
2499 } else if (e
->type
== EXPR_POS
) {
2500 err
= "internal front-end error: EXPR_POS in";
2505 expression_error(e
, "%s initializer", err
);
2510 * choose the next subobject to initialize.
2512 * Get designators for next element, switch old ones to EXPR_POS.
2513 * Return the resulting expression or NULL if we'd run out of subobjects.
2514 * The innermost designator is returned in *v. Designators in old
2515 * are assumed to be already sanity-checked.
2517 static struct expression
*next_designators(struct expression
*old
,
2518 struct symbol
*ctype
,
2519 struct expression
*e
, struct expression
**v
)
2521 struct expression
*new = NULL
;
2525 if (old
->type
== EXPR_INDEX
) {
2526 struct expression
*copy
;
2529 copy
= next_designators(old
->idx_expression
,
2532 n
= old
->idx_to
+ 1;
2533 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2538 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2541 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2544 new->idx_from
= new->idx_to
= n
;
2545 new->idx_expression
= copy
;
2546 new->ctype
= old
->ctype
;
2548 } else if (old
->type
== EXPR_IDENTIFIER
) {
2549 struct expression
*copy
;
2550 struct symbol
*field
;
2553 copy
= next_designators(old
->ident_expression
,
2556 field
= old
->field
->next_subobject
;
2562 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2564 * We can't necessarily trust "field->offset",
2565 * because the field might be in an anonymous
2566 * union, and the field offset is then the offset
2567 * within that union.
2569 * The "old->offset - old->field->offset"
2570 * would be the offset of such an anonymous
2573 offset
= old
->offset
- old
->field
->offset
;
2576 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2580 new->expr_ident
= field
->ident
;
2581 new->ident_expression
= copy
;
2583 new->offset
= field
->offset
+ offset
;
2589 static int handle_initializer(struct expression
**ep
, int nested
,
2590 int class, struct symbol
*ctype
, unsigned long mods
);
2593 * deal with traversing subobjects [6.7.8(17,18,20)]
2595 static void handle_list_initializer(struct expression
*expr
,
2596 int class, struct symbol
*ctype
, unsigned long mods
)
2598 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2599 int jumped
= 0; // has the last designator multiple levels?
2601 if (expr
->zero_init
)
2602 free_ptr_list(&expr
->expr_list
);
2604 FOR_EACH_PTR(expr
->expr_list
, e
) {
2605 struct expression
**v
;
2606 struct symbol
*type
;
2609 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2610 struct symbol
*struct_sym
;
2613 last
= first_subobject(ctype
, class, &top
);
2615 last
= next_designators(last
, ctype
, e
, &top
);
2618 excess(e
, class & TYPE_PTR
? "array" :
2620 DELETE_CURRENT_PTR(e
);
2623 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2624 if (Wdesignated_init
&& struct_sym
->designated_init
)
2625 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2626 ctype
->ident
? "in initializer for " : "",
2627 ctype
->ident
? ctype
->ident
->len
: 0,
2628 ctype
->ident
? ctype
->ident
->name
: "",
2629 ctype
->ident
? ": " : "",
2630 get_type_name(struct_sym
->type
),
2631 show_ident(struct_sym
->ident
));
2632 if (jumped
&& Wpast_deep_designator
) {
2633 warning(e
->pos
, "advancing past deep designator");
2636 REPLACE_CURRENT_PTR(e
, last
);
2638 next
= check_designators(e
, ctype
);
2640 DELETE_CURRENT_PTR(e
);
2644 /* deeper than one designator? */
2646 convert_designators(last
);
2651 lclass
= classify_type(top
->ctype
, &type
);
2652 if (top
->type
== EXPR_INDEX
)
2653 v
= &top
->idx_expression
;
2655 v
= &top
->ident_expression
;
2657 mods
|= ctype
->ctype
.modifiers
& MOD_STORAGE
;
2658 if (handle_initializer(v
, 1, lclass
, top
->ctype
, mods
))
2661 if (!(lclass
& TYPE_COMPOUND
)) {
2662 warning(e
->pos
, "bogus scalar initializer");
2663 DELETE_CURRENT_PTR(e
);
2667 next
= first_subobject(type
, lclass
, v
);
2669 warning(e
->pos
, "missing braces around initializer");
2674 DELETE_CURRENT_PTR(e
);
2675 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2677 } END_FOR_EACH_PTR(e
);
2679 convert_designators(last
);
2680 expr
->ctype
= ctype
;
2683 static int is_string_literal(struct expression
**v
)
2685 struct expression
*e
= *v
;
2686 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2688 if (!e
|| e
->type
!= EXPR_STRING
)
2690 if (e
!= *v
&& Wparen_string
)
2692 "array initialized from parenthesized string constant");
2698 * We want a normal expression, possibly in one layer of braces. Warn
2699 * if the latter happens inside a list (it's legal, but likely to be
2700 * an effect of screwup). In case of anything not legal, we are definitely
2701 * having an effect of screwup, so just fail and let the caller warn.
2703 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2705 struct expression
*v
= NULL
, *p
;
2709 if (e
->type
!= EXPR_INITIALIZER
)
2712 FOR_EACH_PTR(e
->expr_list
, p
) {
2716 } END_FOR_EACH_PTR(p
);
2720 case EXPR_INITIALIZER
:
2722 case EXPR_IDENTIFIER
:
2728 warning(e
->pos
, "braces around scalar initializer");
2733 * deal with the cases that don't care about subobjects:
2734 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2735 * character array <- string literal, possibly in braces [6.7.8(14)]
2736 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2737 * compound type <- initializer list in braces [6.7.8(16)]
2738 * The last one punts to handle_list_initializer() which, in turn will call
2739 * us for individual elements of the list.
2741 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2742 * the lack of support of wide char stuff in general.
2744 * One note: we need to take care not to evaluate a string literal until
2745 * we know that we *will* handle it right here. Otherwise we would screw
2746 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2747 * { "string", ...} - we need to preserve that string literal recognizable
2748 * until we dig into the inner struct.
2750 static int handle_initializer(struct expression
**ep
, int nested
,
2751 int class, struct symbol
*ctype
, unsigned long mods
)
2753 struct expression
*e
= *ep
, *p
;
2754 struct symbol
*type
;
2760 if (!(class & TYPE_COMPOUND
)) {
2761 e
= handle_scalar(e
, nested
);
2765 if (!evaluate_expression(e
))
2767 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2769 * Initializers for static storage duration objects
2770 * shall be constant expressions or a string literal [6.7.8(4)].
2772 mods
|= ctype
->ctype
.modifiers
;
2773 mods
&= (MOD_TOPLEVEL
| MOD_STATIC
);
2774 if (mods
&& !(e
->flags
& (CEF_ACE
| CEF_ADDR
)))
2775 if (Wconstexpr_not_const
)
2776 warning(e
->pos
, "non-constant initializer for static object");
2782 * sublist; either a string, or we dig in; the latter will deal with
2783 * pathologies, so we don't need anything fancy here.
2785 if (e
->type
== EXPR_INITIALIZER
) {
2786 if (is_string_type(ctype
)) {
2787 struct expression
*v
= NULL
;
2790 FOR_EACH_PTR(e
->expr_list
, p
) {
2794 } END_FOR_EACH_PTR(p
);
2795 if (count
== 1 && is_string_literal(&v
)) {
2800 handle_list_initializer(e
, class, ctype
, mods
);
2805 if (is_string_literal(&e
)) {
2806 /* either we are doing array of char, or we'll have to dig in */
2807 if (is_string_type(ctype
)) {
2813 /* struct or union can be initialized by compatible */
2814 if (class != TYPE_COMPOUND
)
2816 type
= evaluate_expression(e
);
2819 if (ctype
->type
== SYM_NODE
)
2820 ctype
= ctype
->ctype
.base_type
;
2821 if (type
->type
== SYM_NODE
)
2822 type
= type
->ctype
.base_type
;
2828 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2830 type
= evaluate_expression(p
);
2831 if (ctype
->bit_size
!= -1) {
2832 struct symbol
*char_type
= e
->wide
? wchar_ctype
: &char_ctype
;
2833 unsigned int size_with_null
= ctype
->bit_size
+ char_type
->bit_size
;
2834 if (size_with_null
< type
->bit_size
)
2836 "too long initializer-string for array of char");
2837 else if (Winit_cstring
&& size_with_null
== type
->bit_size
) {
2839 "too long initializer-string for array of char(no space for nul char)");
2846 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2848 struct symbol
*type
;
2849 int class = classify_type(ctype
, &type
);
2850 if (!handle_initializer(ep
, 0, class, ctype
, 0))
2851 expression_error(*ep
, "invalid initializer");
2854 static struct symbol
*cast_to_bool(struct expression
*expr
)
2856 struct expression
*old
= expr
->cast_expression
;
2857 struct expression
*zero
;
2858 struct symbol
*otype
;
2859 int oclass
= classify_type(degenerate(old
), &otype
);
2860 struct symbol
*ctype
;
2862 if (oclass
& TYPE_COMPOUND
)
2865 zero
= alloc_const_expression(expr
->pos
, 0);
2866 if (oclass
& TYPE_PTR
)
2867 zero
->ctype
= otype
;
2868 expr
->op
= SPECIAL_NOTEQUAL
;
2869 ctype
= usual_conversions(expr
->op
, old
, zero
,
2870 oclass
, TYPE_NUM
, otype
, zero
->ctype
);
2871 expr
->type
= EXPR_COMPARE
;
2872 expr
->left
= cast_to(old
, ctype
);
2873 expr
->right
= cast_to(zero
, ctype
);
2878 static int cast_flags(struct expression
*expr
, struct expression
*old
)
2882 int flags
= CEF_NONE
;
2884 class = classify_type(expr
->ctype
, &t
);
2885 if (class & TYPE_NUM
) {
2886 flags
= old
->flags
& ~CEF_CONST_MASK
;
2888 * Casts to numeric types never result in address
2889 * constants [6.6(9)].
2894 * As an extension, treat address constants cast to
2895 * integer type as an arithmetic constant.
2897 if (old
->flags
& CEF_ADDR
)
2901 * Cast to float type -> not an integer constant
2902 * expression [6.6(6)].
2904 if (class & TYPE_FLOAT
)
2905 flags
&= ~CEF_CLR_ICE
;
2907 * Casts of float literals to integer type results in
2908 * a constant integer expression [6.6(6)].
2910 else if (old
->flags
& CEF_FLOAT
)
2911 flags
= CEF_SET_ICE
;
2912 } else if (class & TYPE_PTR
) {
2914 * Casts of integer literals to pointer type yield
2915 * address constants [6.6(9)].
2917 * As an extension, treat address constants cast to a
2918 * different pointer type as address constants again.
2920 * As another extension, treat integer constant
2921 * expressions (in contrast to literals) cast to
2922 * pointer type as address constants.
2924 if (old
->flags
& (CEF_ICE
| CEF_ADDR
))
2932 // check if a type matches one of the members of a union type
2933 // @utype: the union type
2934 // @type: to type to check
2935 // @return: to identifier of the matching type in the union.
2936 static struct symbol
*find_member_type(struct symbol
*utype
, struct symbol
*type
)
2938 struct symbol
*t
, *member
;
2940 if (utype
->type
!= SYM_UNION
)
2943 FOR_EACH_PTR(utype
->symbol_list
, member
) {
2944 classify_type(member
, &t
);
2947 } END_FOR_EACH_PTR(member
);
2951 static struct symbol
*evaluate_compound_literal(struct expression
*expr
, struct expression
*source
)
2953 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2954 struct symbol
*sym
= expr
->cast_type
;
2956 sym
->initializer
= source
;
2957 evaluate_symbol(sym
);
2959 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2961 if (sym
->ctype
.modifiers
& MOD_TOPLEVEL
)
2962 addr
->flags
|= CEF_ADDR
;
2964 expr
->type
= EXPR_PREOP
;
2971 static struct symbol
*evaluate_cast(struct expression
*expr
)
2973 struct expression
*source
= expr
->cast_expression
;
2974 struct symbol
*ctype
;
2975 struct symbol
*ttype
, *stype
;
2976 struct symbol
*member
;
2978 struct ident
*tas
= NULL
, *sas
= NULL
;
2984 * Special case: a cast can be followed by an
2985 * initializer, in which case we need to pass
2986 * the type value down to that initializer rather
2987 * than trying to evaluate it as an expression
2988 * (cfr. compound literals: C99 & C11 6.5.2.5).
2990 * A more complex case is when the initializer is
2991 * dereferenced as part of a post-fix expression.
2992 * We need to produce an expression that can be dereferenced.
2994 if (source
->type
== EXPR_INITIALIZER
)
2995 return evaluate_compound_literal(expr
, source
);
2997 ctype
= examine_symbol_type(expr
->cast_type
);
2998 ctype
= unqualify_type(ctype
);
2999 expr
->ctype
= ctype
;
3000 expr
->cast_type
= ctype
;
3002 evaluate_expression(source
);
3005 tclass
= classify_type(ctype
, &ttype
);
3007 expr
->flags
= cast_flags(expr
, source
);
3010 * You can always throw a value away by casting to
3011 * "void" - that's an implicit "force". Note that
3012 * the same is _not_ true of "void *".
3014 if (ttype
== &void_ctype
)
3017 stype
= source
->ctype
;
3019 expression_error(expr
, "cast from unknown type");
3022 sclass
= classify_type(stype
, &stype
);
3024 if (expr
->type
== EXPR_FORCE_CAST
)
3027 if (tclass
& (TYPE_COMPOUND
| TYPE_FN
)) {
3029 * Special case: cast to union type (GCC extension)
3030 * The effect is similar to a compound literal except
3031 * that the result is a rvalue.
3033 if ((member
= find_member_type(ttype
, stype
))) {
3034 struct expression
*item
, *init
;
3037 warning(expr
->pos
, "cast to union type");
3039 item
= alloc_expression(source
->pos
, EXPR_IDENTIFIER
);
3040 item
->expr_ident
= member
->ident
;
3041 item
->ident_expression
= source
;
3043 init
= alloc_expression(source
->pos
, EXPR_INITIALIZER
);
3044 add_expression(&init
->expr_list
, item
);
3046 // FIXME: this should be a rvalue
3047 evaluate_compound_literal(expr
, init
);
3051 warning(expr
->pos
, "cast to non-scalar");
3054 if (sclass
& TYPE_COMPOUND
)
3055 warning(expr
->pos
, "cast from non-scalar");
3057 /* allowed cast unfouls */
3058 if (sclass
& TYPE_FOULED
)
3059 stype
= unfoul(stype
);
3061 if (ttype
!= stype
) {
3062 if ((tclass
& TYPE_RESTRICT
) && restricted_value(source
, ttype
))
3063 warning(expr
->pos
, "cast to %s",
3064 show_typename(ttype
));
3065 if (sclass
& TYPE_RESTRICT
) {
3066 if (ttype
== &bool_ctype
) {
3067 if (sclass
& TYPE_FOULED
)
3068 warning(expr
->pos
, "%s degrades to integer",
3069 show_typename(stype
));
3071 warning(expr
->pos
, "cast from %s",
3072 show_typename(stype
));
3077 if ((ttype
== &ulong_ctype
|| ttype
== uintptr_ctype
) && !Wcast_from_as
)
3078 tas
= &bad_address_space
;
3079 else if (tclass
== TYPE_PTR
) {
3080 examine_pointer_target(ttype
);
3081 tas
= ttype
->ctype
.as
;
3084 if ((stype
== &ulong_ctype
|| stype
== uintptr_ctype
))
3085 sas
= &bad_address_space
;
3086 else if (sclass
== TYPE_PTR
) {
3087 examine_pointer_target(stype
);
3088 sas
= stype
->ctype
.as
;
3091 if (!tas
&& valid_as(sas
))
3092 warning(expr
->pos
, "cast removes address space '%s' of expression", show_as(sas
));
3093 if (valid_as(tas
) && valid_as(sas
) && tas
!= sas
)
3094 warning(expr
->pos
, "cast between address spaces (%s -> %s)", show_as(sas
), show_as(tas
));
3095 if (valid_as(tas
) && !sas
&&
3096 !is_null_pointer_constant(source
) && Wcast_to_as
)
3098 "cast adds address space '%s' to expression", show_as(tas
));
3100 if (!(ttype
->ctype
.modifiers
& MOD_PTRINHERIT
) && tclass
== TYPE_PTR
&&
3101 !tas
&& (source
->flags
& CEF_ICE
)) {
3102 if (ttype
->ctype
.base_type
== &void_ctype
) {
3103 if (is_zero_constant(source
)) {
3105 expr
->type
= EXPR_VALUE
;
3106 expr
->ctype
= &null_ctype
;
3113 if (ttype
== &bool_ctype
)
3116 // checks pointers to restricted
3117 while (Wbitwise_pointer
&& tclass
== TYPE_PTR
&& sclass
== TYPE_PTR
) {
3118 tclass
= classify_type(ttype
->ctype
.base_type
, &ttype
);
3119 sclass
= classify_type(stype
->ctype
.base_type
, &stype
);
3122 if (!ttype
|| !stype
)
3124 if (ttype
== &void_ctype
|| stype
== &void_ctype
)
3126 if (tclass
& TYPE_RESTRICT
) {
3127 warning(expr
->pos
, "cast to %s", show_typename(ctype
));
3130 if (sclass
& TYPE_RESTRICT
) {
3131 warning(expr
->pos
, "cast from %s", show_typename(source
->ctype
));
3140 * Evaluate a call expression with a symbol. This
3141 * should expand inline functions, and evaluate
3144 static int evaluate_symbol_call(struct expression
*expr
)
3146 struct expression
*fn
= expr
->fn
;
3147 struct symbol
*ctype
= fn
->ctype
;
3149 if (fn
->type
!= EXPR_PREOP
)
3152 if (ctype
->op
&& ctype
->op
->evaluate
)
3153 return ctype
->op
->evaluate(expr
);
3158 static struct symbol
*evaluate_call(struct expression
*expr
)
3161 struct symbol
*ctype
, *sym
;
3162 struct expression
*fn
= expr
->fn
;
3163 struct expression_list
*arglist
= expr
->args
;
3165 if (!evaluate_expression(fn
))
3167 sym
= ctype
= fn
->ctype
;
3168 if (ctype
->type
== SYM_NODE
)
3169 ctype
= ctype
->ctype
.base_type
;
3170 if (ctype
->type
== SYM_PTR
)
3171 ctype
= get_base_type(ctype
);
3173 if (ctype
->type
!= SYM_FN
) {
3174 struct expression
*arg
;
3176 if (fn
->ctype
== &bad_ctype
)
3179 expression_error(expr
, "not a function %s",
3180 show_ident(sym
->ident
));
3181 /* do typechecking in arguments */
3182 FOR_EACH_PTR (arglist
, arg
) {
3183 evaluate_expression(arg
);
3184 } END_FOR_EACH_PTR(arg
);
3188 examine_fn_arguments(ctype
);
3189 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
3190 sym
->op
&& sym
->op
->args
) {
3191 if (!sym
->op
->args(expr
))
3194 if (!evaluate_arguments(ctype
->arguments
, arglist
))
3196 args
= expression_list_size(expr
->args
);
3197 fnargs
= symbol_list_size(ctype
->arguments
);
3198 if (args
< fnargs
) {
3199 expression_error(expr
,
3200 "not enough arguments for function %s",
3201 show_ident(sym
->ident
));
3204 if (args
> fnargs
&& !ctype
->variadic
)
3205 expression_error(expr
,
3206 "too many arguments for function %s",
3207 show_ident(sym
->ident
));
3209 expr
->ctype
= ctype
->ctype
.base_type
;
3210 if (sym
->type
== SYM_NODE
) {
3211 if (evaluate_symbol_call(expr
))
3217 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
3219 struct expression
*e
= expr
->down
;
3220 struct symbol
*ctype
= expr
->in
;
3223 if (expr
->op
== '.') {
3224 struct symbol
*field
;
3227 expression_error(expr
, "expected structure or union");
3230 examine_symbol_type(ctype
);
3231 class = classify_type(ctype
, &ctype
);
3232 if (class != TYPE_COMPOUND
) {
3233 expression_error(expr
, "expected structure or union");
3237 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
3239 expression_error(expr
, "unknown member");
3243 expr
->type
= EXPR_VALUE
;
3244 expr
->flags
= CEF_SET_ICE
;
3245 expr
->value
= offset
;
3247 expr
->ctype
= size_t_ctype
;
3250 expression_error(expr
, "expected structure or union");
3253 examine_symbol_type(ctype
);
3254 class = classify_type(ctype
, &ctype
);
3255 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
3256 expression_error(expr
, "expected array");
3259 ctype
= ctype
->ctype
.base_type
;
3261 expr
->type
= EXPR_VALUE
;
3262 expr
->flags
= CEF_SET_ICE
;
3265 expr
->ctype
= size_t_ctype
;
3267 struct expression
*idx
= expr
->index
, *m
;
3268 struct symbol
*i_type
= evaluate_expression(idx
);
3269 unsigned old_idx_flags
;
3270 int i_class
= classify_type(i_type
, &i_type
);
3272 if (!is_int(i_class
)) {
3273 expression_error(expr
, "non-integer index");
3276 unrestrict(idx
, i_class
, &i_type
);
3277 old_idx_flags
= idx
->flags
;
3278 idx
= cast_to(idx
, size_t_ctype
);
3279 idx
->flags
= old_idx_flags
;
3280 m
= alloc_const_expression(expr
->pos
,
3281 bits_to_bytes(ctype
->bit_size
));
3282 m
->ctype
= size_t_ctype
;
3283 m
->flags
= CEF_SET_INT
;
3284 expr
->type
= EXPR_BINOP
;
3288 expr
->ctype
= size_t_ctype
;
3289 expr
->flags
= m
->flags
& idx
->flags
& ~CEF_CONST_MASK
;
3293 struct expression
*copy
= __alloc_expression(0);
3295 if (e
->type
== EXPR_OFFSETOF
)
3297 if (!evaluate_expression(e
))
3299 expr
->type
= EXPR_BINOP
;
3300 expr
->flags
= e
->flags
& copy
->flags
& ~CEF_CONST_MASK
;
3302 expr
->ctype
= size_t_ctype
;
3306 return size_t_ctype
;
3309 static void check_label_declaration(struct position pos
, struct symbol
*label
)
3311 switch (label
->namespace) {
3315 sparse_error(pos
, "label '%s' was not declared", show_ident(label
->ident
));
3318 current_fn
->bogus_linear
= 1;
3324 static int type_selection(struct symbol
*ctrl
, struct symbol
*type
)
3326 struct ctype c
= { .base_type
= ctrl
};
3327 struct ctype t
= { .base_type
= type
};
3329 return !type_difference(&c
, &t
, 0, 0);
3332 static struct symbol
*evaluate_generic_selection(struct expression
*expr
)
3334 struct type_expression
*map
;
3335 struct expression
*res
;
3336 struct symbol source
;
3337 struct symbol
*ctrl
;
3339 if (!evaluate_expression(expr
->control
))
3341 if (!(ctrl
= degenerate(expr
->control
)))
3345 source
.ctype
.modifiers
&= ~(MOD_QUALIFIER
|MOD_ATOMIC
);
3346 for (map
= expr
->map
; map
; map
= map
->next
) {
3347 struct symbol
*stype
= map
->type
;
3348 struct symbol
*base
;
3350 if (!evaluate_symbol(stype
))
3353 base
= stype
->ctype
.base_type
;
3354 if (base
->type
== SYM_ARRAY
&& base
->array_size
) {
3355 get_expression_value_silent(base
->array_size
);
3356 if (base
->array_size
->type
== EXPR_VALUE
)
3358 sparse_error(stype
->pos
, "variable length array type in generic selection");
3361 if (is_func_type(stype
)) {
3362 sparse_error(stype
->pos
, "function type in generic selection");
3365 if (stype
->bit_size
<= 0 || is_void_type(stype
)) {
3366 sparse_error(stype
->pos
, "incomplete type in generic selection");
3369 if (!type_selection(&source
, stype
))
3377 sparse_error(expr
->pos
, "no generic selection for '%s'", show_typename(ctrl
));
3383 return evaluate_expression(expr
);
3386 struct symbol
*evaluate_expression(struct expression
*expr
)
3393 switch (expr
->type
) {
3396 expression_error(expr
, "value expression without a type");
3399 return evaluate_string(expr
);
3401 return evaluate_symbol_expression(expr
);
3403 evaluate_expression(expr
->left
);
3404 evaluate_expression(expr
->right
);
3405 if (!valid_subexpr_type(expr
))
3407 return evaluate_binop(expr
);
3409 return evaluate_logical(expr
);
3411 evaluate_expression(expr
->left
);
3412 if (!evaluate_expression(expr
->right
))
3414 return evaluate_comma(expr
);
3416 evaluate_expression(expr
->left
);
3417 evaluate_expression(expr
->right
);
3418 if (!valid_subexpr_type(expr
))
3420 return evaluate_compare(expr
);
3421 case EXPR_ASSIGNMENT
:
3422 evaluate_expression(expr
->left
);
3423 evaluate_expression(expr
->right
);
3424 if (!valid_subexpr_type(expr
))
3426 return evaluate_assignment(expr
);
3428 if (!evaluate_expression(expr
->unop
))
3430 return evaluate_preop(expr
);
3432 if (!evaluate_expression(expr
->unop
))
3434 return evaluate_postop(expr
);
3436 case EXPR_FORCE_CAST
:
3437 case EXPR_IMPLIED_CAST
:
3438 return evaluate_cast(expr
);
3440 return evaluate_sizeof(expr
);
3441 case EXPR_PTRSIZEOF
:
3442 return evaluate_ptrsizeof(expr
);
3444 return evaluate_alignof(expr
);
3446 return evaluate_member_dereference(expr
);
3448 return evaluate_call(expr
);
3450 case EXPR_CONDITIONAL
:
3451 return evaluate_conditional_expression(expr
);
3452 case EXPR_STATEMENT
:
3453 expr
->ctype
= evaluate_statement(expr
->statement
);
3457 expr
->ctype
= &ptr_ctype
;
3458 check_label_declaration(expr
->pos
, expr
->label_symbol
);
3462 /* Evaluate the type of the symbol .. */
3463 evaluate_symbol(expr
->symbol
);
3464 /* .. but the type of the _expression_ is a "type" */
3465 expr
->ctype
= &type_ctype
;
3469 return evaluate_offsetof(expr
);
3472 return evaluate_generic_selection(expr
);
3474 /* These can not exist as stand-alone expressions */
3475 case EXPR_INITIALIZER
:
3476 case EXPR_IDENTIFIER
:
3479 expression_error(expr
, "internal front-end error: initializer in expression");
3482 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3488 void check_duplicates(struct symbol
*sym
)
3491 struct symbol
*next
= sym
;
3492 int initialized
= sym
->initializer
!= NULL
;
3494 while ((next
= next
->same_symbol
) != NULL
) {
3495 const char *typediff
;
3496 evaluate_symbol(next
);
3497 if (initialized
&& next
->initializer
) {
3498 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3499 show_ident(sym
->ident
),
3500 stream_name(next
->pos
.stream
), next
->pos
.line
);
3501 /* Only warn once */
3505 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3507 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (%s):",
3508 show_ident(sym
->ident
), typediff
);
3509 info(sym
->pos
, " %s", show_typename(sym
));
3510 info(next
->pos
, "note: previously declared as:");
3511 info(next
->pos
, " %s", show_typename(next
));
3516 unsigned long mod
= sym
->ctype
.modifiers
;
3517 if (mod
& (MOD_STATIC
| MOD_REGISTER
| MOD_EXT_VISIBLE
))
3519 if (!(mod
& MOD_TOPLEVEL
))
3523 if (sym
->ident
== &main_ident
)
3525 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3529 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3531 struct symbol
*base_type
;
3539 sym
= examine_symbol_type(sym
);
3540 base_type
= get_base_type(sym
);
3544 /* Evaluate the initializers */
3545 if (sym
->initializer
)
3546 evaluate_initializer(sym
, &sym
->initializer
);
3548 /* And finally, evaluate the body of the symbol too */
3549 if (base_type
->type
== SYM_FN
) {
3550 struct symbol
*curr
= current_fn
;
3552 if (sym
->definition
&& sym
->definition
!= sym
)
3553 return evaluate_symbol(sym
->definition
);
3557 examine_fn_arguments(base_type
);
3558 if (!base_type
->stmt
&& base_type
->inline_stmt
&& sym
->definition
)
3560 if (base_type
->stmt
)
3561 evaluate_statement(base_type
->stmt
);
3569 void evaluate_symbol_list(struct symbol_list
*list
)
3573 FOR_EACH_PTR(list
, sym
) {
3574 has_error
&= ~ERROR_CURR_PHASE
;
3575 evaluate_symbol(sym
);
3576 check_duplicates(sym
);
3577 } END_FOR_EACH_PTR(sym
);
3580 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3582 struct expression
*expr
= stmt
->expression
;
3583 struct symbol
*fntype
, *rettype
;
3585 evaluate_expression(expr
);
3586 fntype
= current_fn
->ctype
.base_type
;
3587 rettype
= fntype
->ctype
.base_type
;
3588 if (!rettype
|| rettype
== &void_ctype
) {
3589 if (expr
&& expr
->ctype
&& !is_void_type(expr
->ctype
))
3590 expression_error(expr
, "return expression in %s function", rettype
?"void":"typeless");
3591 if (expr
&& Wreturn_void
)
3592 warning(stmt
->pos
, "returning void-valued expression");
3597 sparse_error(stmt
->pos
, "return with no return value");
3602 compatible_assignment_types(expr
, rettype
, &stmt
->expression
, "return expression");
3606 static void evaluate_if_statement(struct statement
*stmt
)
3608 if (!stmt
->if_conditional
)
3611 evaluate_conditional(stmt
->if_conditional
, 0);
3612 evaluate_statement(stmt
->if_true
);
3613 evaluate_statement(stmt
->if_false
);
3616 static void evaluate_iterator(struct statement
*stmt
)
3618 evaluate_symbol_list(stmt
->iterator_syms
);
3619 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3620 evaluate_conditional(stmt
->iterator_post_condition
,1);
3621 evaluate_statement(stmt
->iterator_pre_statement
);
3622 evaluate_statement(stmt
->iterator_statement
);
3623 evaluate_statement(stmt
->iterator_post_statement
);
3627 static void parse_asm_constraint(struct asm_operand
*op
)
3629 struct expression
*constraint
= op
->constraint
;
3630 const char *str
= constraint
->string
->data
;
3635 sparse_error(constraint
->pos
, "invalid ASM constraint (\"\")");
3638 op
->is_modify
= true;
3641 op
->is_assign
= true;
3646 while ((c
= *str
++)) {
3650 sparse_error(constraint
->pos
, "invalid ASM constraint '%c'", c
);
3654 op
->is_earlyclobber
= true;
3657 op
->is_commutative
= true;
3660 op
->is_register
= true;
3667 op
->is_memory
= true;
3672 // FIXME: ignored for now
3676 // FIXME: multiple alternative constraints
3680 // FIXME: numeric matching constraint?
3683 // FIXME: symbolic matching constraint
3687 if (arch_target
->asm_constraint
)
3688 str
= arch_target
->asm_constraint(op
, c
, str
);
3690 // FIXME: multi-letter constraints
3695 // FIXME: how to deal with multi-constraint?
3696 if (op
->is_register
)
3700 static void verify_output_constraint(struct asm_operand
*op
)
3702 struct expression
*expr
= op
->constraint
;
3703 const char *constraint
= expr
->string
->data
;
3706 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3709 static void verify_input_constraint(struct asm_operand
*op
)
3711 struct expression
*expr
= op
->constraint
;
3712 const char *constraint
= expr
->string
->data
;
3715 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3718 static void evaluate_asm_memop(struct asm_operand
*op
)
3720 if (op
->is_memory
) {
3721 struct expression
*expr
= op
->expr
;
3722 struct expression
*addr
;
3724 // implicit addressof
3725 addr
= alloc_expression(expr
->pos
, EXPR_PREOP
);
3729 evaluate_addressof(addr
);
3732 evaluate_expression(op
->expr
);
3733 degenerate(op
->expr
);
3737 static void evaluate_asm_statement(struct statement
*stmt
)
3739 struct expression
*expr
;
3740 struct asm_operand
*op
;
3743 if (!stmt
->asm_string
)
3746 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
3750 if (op
->constraint
) {
3751 parse_asm_constraint(op
);
3752 verify_output_constraint(op
);
3757 if (!evaluate_expression(expr
))
3759 if (!lvalue_expression(expr
))
3760 warning(expr
->pos
, "asm output is not an lvalue");
3761 evaluate_assign_to(expr
, expr
->ctype
);
3762 evaluate_asm_memop(op
);
3763 } END_FOR_EACH_PTR(op
);
3765 FOR_EACH_PTR(stmt
->asm_inputs
, op
) {
3769 if (op
->constraint
) {
3770 parse_asm_constraint(op
);
3771 verify_input_constraint(op
);
3775 if (!evaluate_expression(op
->expr
))
3777 evaluate_asm_memop(op
);
3778 } END_FOR_EACH_PTR(op
);
3780 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3782 sparse_error(stmt
->pos
, "bad asm clobbers");
3785 if (expr
->type
== EXPR_STRING
)
3787 expression_error(expr
, "asm clobber is not a string");
3788 } END_FOR_EACH_PTR(expr
);
3790 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3791 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3792 sparse_error(stmt
->pos
, "bad asm label");
3795 } END_FOR_EACH_PTR(sym
);
3798 static void evaluate_case_statement(struct statement
*stmt
)
3800 evaluate_expression(stmt
->case_expression
);
3801 evaluate_expression(stmt
->case_to
);
3802 evaluate_statement(stmt
->case_statement
);
3805 static void check_case_type(struct expression
*switch_expr
,
3806 struct expression
*case_expr
,
3807 struct expression
**enumcase
)
3809 struct symbol
*switch_type
, *case_type
;
3815 switch_type
= switch_expr
->ctype
;
3816 case_type
= evaluate_expression(case_expr
);
3818 if (!switch_type
|| !case_type
)
3822 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3823 else if (is_enum_type(case_type
))
3824 *enumcase
= case_expr
;
3827 sclass
= classify_type(switch_type
, &switch_type
);
3828 cclass
= classify_type(case_type
, &case_type
);
3830 /* both should be arithmetic */
3831 if (!(sclass
& cclass
& TYPE_NUM
))
3834 /* neither should be floating */
3835 if ((sclass
| cclass
) & TYPE_FLOAT
)
3838 /* if neither is restricted, we are OK */
3839 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3842 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3843 cclass
, sclass
, case_type
, switch_type
)) {
3844 unrestrict(case_expr
, cclass
, &case_type
);
3845 unrestrict(switch_expr
, sclass
, &switch_type
);
3850 expression_error(case_expr
, "incompatible types for 'case' statement");
3853 static void evaluate_switch_statement(struct statement
*stmt
)
3856 struct expression
*enumcase
= NULL
;
3857 struct expression
**enumcase_holder
= &enumcase
;
3858 struct expression
*sel
= stmt
->switch_expression
;
3860 evaluate_expression(sel
);
3861 evaluate_statement(stmt
->switch_statement
);
3864 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3865 enumcase_holder
= NULL
; /* Only check cases against switch */
3867 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3868 struct statement
*case_stmt
= sym
->stmt
;
3869 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3870 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3871 } END_FOR_EACH_PTR(sym
);
3874 static void evaluate_goto_statement(struct statement
*stmt
)
3876 struct symbol
*label
= stmt
->goto_label
;
3879 // no label associated, may be a computed goto
3880 evaluate_expression(stmt
->goto_expression
);
3884 check_label_declaration(stmt
->pos
, label
);
3887 struct symbol
*evaluate_statement(struct statement
*stmt
)
3892 switch (stmt
->type
) {
3893 case STMT_DECLARATION
: {
3895 FOR_EACH_PTR(stmt
->declaration
, s
) {
3897 } END_FOR_EACH_PTR(s
);
3902 return evaluate_return_expression(stmt
);
3904 case STMT_EXPRESSION
:
3905 if (!evaluate_expression(stmt
->expression
))
3907 if (stmt
->expression
->ctype
== &null_ctype
)
3908 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3909 return unqualify_type(degenerate(stmt
->expression
));
3911 case STMT_COMPOUND
: {
3912 struct statement
*s
;
3913 struct symbol
*type
= NULL
;
3915 /* Evaluate the return symbol in the compound statement */
3916 evaluate_symbol(stmt
->ret
);
3919 * Then, evaluate each statement, making the type of the
3920 * compound statement be the type of the last statement
3922 type
= evaluate_statement(stmt
->args
);
3923 FOR_EACH_PTR(stmt
->stmts
, s
) {
3924 type
= evaluate_statement(s
);
3925 } END_FOR_EACH_PTR(s
);
3931 evaluate_if_statement(stmt
);
3934 evaluate_iterator(stmt
);
3937 evaluate_switch_statement(stmt
);
3940 evaluate_case_statement(stmt
);
3943 return evaluate_statement(stmt
->label_statement
);
3945 evaluate_goto_statement(stmt
);
3950 evaluate_asm_statement(stmt
);
3953 evaluate_expression(stmt
->expression
);
3956 evaluate_expression(stmt
->range_expression
);
3957 evaluate_expression(stmt
->range_low
);
3958 evaluate_expression(stmt
->range_high
);