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 int same_cast_type(struct symbol
*orig
, struct symbol
*new)
231 return orig
->bit_size
== new->bit_size
&&
232 orig
->bit_offset
== new->bit_offset
;
235 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, struct ident
**asp
)
237 unsigned long mod
= 0;
238 struct ident
*as
= NULL
;
241 mod
|= node
->ctype
.modifiers
;
242 combine_address_space(node
->pos
, &as
, node
->ctype
.as
);
243 if (node
->type
== SYM_NODE
) {
244 node
= node
->ctype
.base_type
;
249 *modp
= mod
& ~MOD_IGNORE
;
254 static int is_same_type(struct expression
*expr
, struct symbol
*new)
256 struct symbol
*old
= expr
->ctype
;
257 unsigned long oldmod
, newmod
;
258 struct ident
*oldas
, *newas
;
260 old
= base_type(old
, &oldmod
, &oldas
);
261 new = base_type(new, &newmod
, &newas
);
263 /* Same base type, same address space? */
264 if (old
== new && oldas
== newas
) {
265 unsigned long difmod
;
267 /* Check the modifier bits. */
268 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
270 /* Exact same type? */
275 * Not the same type, but differs only in "const".
276 * Don't warn about MOD_NOCAST.
278 if (difmod
== MOD_CONST
)
281 if ((oldmod
| newmod
) & MOD_NOCAST
) {
282 const char *tofrom
= "to/from";
283 if (!(newmod
& MOD_NOCAST
))
285 if (!(oldmod
& MOD_NOCAST
))
287 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
293 warn_for_different_enum_types (struct position pos
,
294 struct symbol
*typea
,
295 struct symbol
*typeb
)
299 if (typea
->type
== SYM_NODE
)
300 typea
= typea
->ctype
.base_type
;
301 if (typeb
->type
== SYM_NODE
)
302 typeb
= typeb
->ctype
.base_type
;
307 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
308 warning(pos
, "mixing different enum types:");
309 info(pos
, " %s", show_typename(typea
));
310 info(pos
, " %s", show_typename(typeb
));
314 static int cast_flags(struct expression
*expr
, struct expression
*target
);
315 static struct symbol
*cast_to_bool(struct expression
*expr
);
318 * This gets called for implicit casts in assignments and
319 * integer promotion. We often want to try to move the
320 * cast down, because the ops involved may have been
321 * implicitly cast up, and we can get rid of the casts
324 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
326 struct expression
*expr
;
328 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
330 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
334 * See if we can simplify the op. Move the cast down.
338 if (old
->ctype
->bit_size
< type
->bit_size
)
340 if (old
->op
== '~') {
342 old
->unop
= cast_to(old
->unop
, type
);
347 case EXPR_IMPLIED_CAST
:
348 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
350 if (old
->ctype
->bit_size
>= type
->bit_size
) {
351 struct expression
*orig
= old
->cast_expression
;
352 if (same_cast_type(orig
->ctype
, type
))
354 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
356 old
->cast_type
= type
;
366 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
368 expr
->cast_type
= type
;
369 expr
->cast_expression
= old
;
370 expr
->flags
= cast_flags(expr
, old
);
372 if (is_bool_type(type
))
389 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
391 static int type_class
[SYM_BAD
+ 1] = {
392 [SYM_PTR
] = TYPE_PTR
,
393 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
394 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
395 [SYM_STRUCT
] = TYPE_COMPOUND
,
396 [SYM_UNION
] = TYPE_COMPOUND
,
397 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
398 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
399 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
401 if (type
->type
== SYM_NODE
)
402 type
= type
->ctype
.base_type
;
403 if (type
->type
== SYM_TYPEOF
) {
404 type
= examine_symbol_type(type
);
405 if (type
->type
== SYM_NODE
)
406 type
= type
->ctype
.base_type
;
408 if (type
->type
== SYM_ENUM
)
409 type
= type
->ctype
.base_type
;
411 if (type
->type
== SYM_BASETYPE
) {
412 if (type
->ctype
.base_type
== &int_type
)
414 if (type
->ctype
.base_type
== &fp_type
)
415 return TYPE_NUM
| TYPE_FLOAT
;
417 return type_class
[type
->type
];
420 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
422 static inline int is_string_type(struct symbol
*type
)
424 if (type
->type
== SYM_NODE
)
425 type
= type
->ctype
.base_type
;
426 if (type
->type
!= SYM_ARRAY
)
428 type
= type
->ctype
.base_type
;
429 return is_byte_type(type
) || is_wchar_type(type
);
432 static struct symbol
*bad_expr_type(struct expression
*expr
)
434 switch (expr
->type
) {
437 if (!valid_subexpr_type(expr
))
439 sparse_error(expr
->pos
, "incompatible types for operation (%s):", show_special(expr
->op
));
440 info(expr
->pos
, " %s", show_typename(expr
->left
->ctype
));
441 info(expr
->pos
, " %s", show_typename(expr
->right
->ctype
));
445 if (!valid_expr_type(expr
->unop
))
447 sparse_error(expr
->pos
, "incompatible type for operation (%s):", show_special(expr
->op
));
448 info(expr
->pos
, " %s", show_typename(expr
->unop
->ctype
));
454 expr
->flags
= CEF_NONE
;
455 return expr
->ctype
= &bad_ctype
;
458 static int restricted_value(struct expression
*v
, struct symbol
*type
)
460 if (v
->type
!= EXPR_VALUE
)
467 static int restricted_binop(int op
, struct symbol
*type
)
472 case SPECIAL_AND_ASSIGN
:
473 case SPECIAL_OR_ASSIGN
:
474 case SPECIAL_XOR_ASSIGN
:
475 return 1; /* unfoul */
479 return 2; /* keep fouled */
481 case SPECIAL_NOTEQUAL
:
482 return 3; /* warn if fouled */
488 static int restricted_unop(int op
, struct symbol
**type
)
491 if ((*type
)->bit_size
< bits_in_int
)
492 *type
= befoul(*type
);
499 /* type should be SYM_FOULED */
500 static inline struct symbol
*unfoul(struct symbol
*type
)
502 return type
->ctype
.base_type
;
505 static struct symbol
*restricted_binop_type(int op
,
506 struct expression
*left
,
507 struct expression
*right
,
508 int lclass
, int rclass
,
509 struct symbol
*ltype
,
510 struct symbol
*rtype
)
512 struct symbol
*ctype
= NULL
;
513 if (lclass
& TYPE_RESTRICT
) {
514 if (rclass
& TYPE_RESTRICT
) {
515 if (ltype
== rtype
) {
517 } else if (lclass
& TYPE_FOULED
) {
518 if (unfoul(ltype
) == rtype
)
520 } else if (rclass
& TYPE_FOULED
) {
521 if (unfoul(rtype
) == ltype
)
525 if (!restricted_value(right
, ltype
))
528 } else if (!restricted_value(left
, rtype
))
532 switch (restricted_binop(op
, ctype
)) {
534 if ((lclass
^ rclass
) & TYPE_FOULED
)
535 ctype
= unfoul(ctype
);
538 if (!(lclass
& rclass
& TYPE_FOULED
))
550 static inline void unrestrict(struct expression
*expr
,
551 int class, struct symbol
**ctype
)
553 if (class & TYPE_RESTRICT
) {
554 if (class & TYPE_FOULED
)
555 *ctype
= unfoul(*ctype
);
556 warning(expr
->pos
, "%s degrades to integer",
557 show_typename(*ctype
));
558 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
562 static struct symbol
*usual_conversions(int op
,
563 struct expression
*left
,
564 struct expression
*right
,
565 int lclass
, int rclass
,
566 struct symbol
*ltype
,
567 struct symbol
*rtype
)
569 struct symbol
*ctype
;
571 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
573 if ((lclass
| rclass
) & TYPE_RESTRICT
)
577 if (!(lclass
& TYPE_FLOAT
)) {
578 if (!(rclass
& TYPE_FLOAT
))
579 return bigger_int_type(ltype
, rtype
);
582 } else if (rclass
& TYPE_FLOAT
) {
583 if (rtype
->rank
> ltype
->rank
)
591 ctype
= restricted_binop_type(op
, left
, right
,
592 lclass
, rclass
, ltype
, rtype
);
596 unrestrict(left
, lclass
, <ype
);
597 unrestrict(right
, rclass
, &rtype
);
602 static inline int lvalue_expression(struct expression
*expr
)
604 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
607 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
609 struct expression
*index
= expr
->right
;
610 struct symbol
*ctype
, *base
;
613 classify_type(degenerate(expr
->left
), &ctype
);
614 base
= examine_pointer_target(ctype
);
617 * An address constant +/- an integer constant expression
618 * yields an address constant again [6.6(7)].
620 if ((expr
->left
->flags
& CEF_ADDR
) && (expr
->right
->flags
& CEF_ICE
))
621 expr
->flags
= CEF_ADDR
;
624 expression_error(expr
, "missing type information");
627 if (is_function(base
)) {
628 expression_error(expr
, "arithmetics on pointers to functions");
632 /* Get the size of whatever the pointer points to */
633 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
635 if (ctype
== &null_ctype
)
639 if (multiply
== 1 && itype
->bit_size
== bits_in_pointer
)
642 if (index
->type
== EXPR_VALUE
) {
643 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
644 unsigned long long v
= index
->value
, mask
;
645 mask
= 1ULL << (itype
->bit_size
- 1);
651 mask
= 1ULL << (bits_in_pointer
- 1);
652 v
&= mask
| (mask
- 1);
654 val
->ctype
= ssize_t_ctype
;
659 if (itype
->bit_size
!= bits_in_pointer
)
660 index
= cast_to(index
, ssize_t_ctype
);
663 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
664 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
666 val
->ctype
= ssize_t_ctype
;
667 val
->value
= multiply
;
670 mul
->ctype
= ssize_t_ctype
;
680 static void examine_fn_arguments(struct symbol
*fn
);
682 #define MOD_IGN (MOD_QUALIFIER | MOD_FUN_ATTR)
684 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
685 unsigned long mod1
, unsigned long mod2
)
687 struct ident
*as1
= c1
->as
, *as2
= c2
->as
;
688 struct symbol
*t1
= c1
->base_type
;
689 struct symbol
*t2
= c2
->base_type
;
690 int move1
= 1, move2
= 1;
691 mod1
|= c1
->modifiers
;
692 mod2
|= c2
->modifiers
;
696 struct symbol
*base1
= t1
->ctype
.base_type
;
697 struct symbol
*base2
= t2
->ctype
.base_type
;
700 * FIXME! Collect alignment and context too here!
703 if (t1
&& t1
->type
!= SYM_PTR
) {
704 mod1
|= t1
->ctype
.modifiers
;
705 combine_address_space(t1
->pos
, &as1
, t1
->ctype
.as
);
711 if (t2
&& t2
->type
!= SYM_PTR
) {
712 mod2
|= t2
->ctype
.modifiers
;
713 combine_address_space(t2
->pos
, &as2
, t2
->ctype
.as
);
721 return "different types";
723 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
731 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
741 if (type
!= t2
->type
)
742 return "different base types";
746 sparse_error(t1
->pos
,
747 "internal error: bad type in derived(%d)",
751 return "different base types";
754 /* allow definition of incomplete structs and unions */
755 if (t1
->ident
== t2
->ident
)
757 return "different base types";
759 /* XXX: we ought to compare sizes */
763 return "different address spaces";
764 /* MOD_SPECIFIER is due to idiocy in parse.c */
765 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
766 return "different modifiers";
767 /* we could be lazier here */
768 base1
= examine_pointer_target(t1
);
769 base2
= examine_pointer_target(t2
);
770 mod1
= t1
->ctype
.modifiers
;
772 mod2
= t2
->ctype
.modifiers
;
776 struct symbol
*arg1
, *arg2
;
780 return "different address spaces";
781 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
782 return "different modifiers";
783 mod1
= t1
->ctype
.modifiers
;
785 mod2
= t2
->ctype
.modifiers
;
788 if (t1
->variadic
!= t2
->variadic
)
789 return "incompatible variadic arguments";
790 examine_fn_arguments(t1
);
791 examine_fn_arguments(t2
);
792 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
793 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
800 return "different argument counts";
801 diffstr
= type_difference(&arg1
->ctype
,
805 static char argdiff
[80];
806 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
813 FINISH_PTR_LIST(arg2
);
814 FINISH_PTR_LIST(arg1
);
819 return "different address spaces";
821 return "different base types";
822 if (t1
->rank
!= t2
->rank
)
823 return "different type sizes";
824 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
827 else if (diff
& ~MOD_SIGNEDNESS
)
828 return "different modifiers";
830 return "different signedness";
836 return "different address spaces";
837 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
838 return "different modifiers";
842 static void bad_null(struct expression
*expr
)
844 if (Wnon_pointer_null
)
845 warning(expr
->pos
, "Using plain integer as NULL pointer");
848 static unsigned long target_qualifiers(struct symbol
*type
)
850 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
851 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
856 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
858 const char *typediff
;
859 struct symbol
*ltype
, *rtype
;
860 struct expression
*l
= expr
->left
;
861 struct expression
*r
= expr
->right
;
862 struct symbol
*lbase
;
864 classify_type(degenerate(l
), <ype
);
865 classify_type(degenerate(r
), &rtype
);
867 lbase
= examine_pointer_target(ltype
);
868 examine_pointer_target(rtype
);
869 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
870 target_qualifiers(rtype
),
871 target_qualifiers(ltype
));
873 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
875 if (is_function(lbase
)) {
876 expression_error(expr
, "subtraction of functions? Share your drugs");
880 expr
->ctype
= ssize_t_ctype
;
881 if (lbase
->bit_size
> bits_in_char
) {
882 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
883 struct expression
*div
= expr
;
884 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
885 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
887 val
->ctype
= size_t_ctype
;
890 if (value
& (value
-1)) {
891 if (Wptr_subtraction_blows
) {
892 warning(expr
->pos
, "potentially expensive pointer subtraction");
893 info(expr
->pos
, " '%s' has a non-power-of-2 size: %lu", show_typename(lbase
), value
);
898 sub
->ctype
= ssize_t_ctype
;
907 return ssize_t_ctype
;
910 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
912 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
914 struct symbol
*ctype
;
919 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
920 warning(expr
->pos
, "assignment expression in conditional");
922 ctype
= evaluate_expression(expr
);
923 if (!valid_type(ctype
))
925 if (is_safe_type(ctype
))
926 warning(expr
->pos
, "testing a 'safe expression'");
927 if (is_func_type(ctype
)) {
929 warning(expr
->pos
, "the address of %s will always evaluate as true", "a function");
930 } else if (is_array_type(ctype
)) {
932 warning(expr
->pos
, "the address of %s will always evaluate as true", "an array");
933 } else if (!is_scalar_type(ctype
)) {
934 sparse_error(expr
->pos
, "non-scalar type in conditional:");
935 info(expr
->pos
, " %s", show_typename(ctype
));
939 ctype
= degenerate(expr
);
943 static struct symbol
*evaluate_logical(struct expression
*expr
)
945 if (!evaluate_conditional(expr
->left
, 0))
947 if (!evaluate_conditional(expr
->right
, 0))
950 /* the result is int [6.5.13(3), 6.5.14(3)] */
951 expr
->ctype
= &int_ctype
;
952 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
953 expr
->flags
&= ~(CEF_CONST_MASK
| CEF_ADDR
);
957 static struct symbol
*evaluate_binop(struct expression
*expr
)
959 struct symbol
*ltype
, *rtype
, *ctype
;
960 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
961 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
964 /* number op number */
965 if (lclass
& rclass
& TYPE_NUM
) {
966 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
967 expr
->flags
&= ~CEF_CONST_MASK
;
969 if ((lclass
| rclass
) & TYPE_FLOAT
) {
971 case '+': case '-': case '*': case '/':
974 return bad_expr_type(expr
);
978 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
979 // shifts do integer promotions, but that's it.
980 unrestrict(expr
->left
, lclass
, <ype
);
981 unrestrict(expr
->right
, rclass
, &rtype
);
982 ctype
= ltype
= integer_promotion(ltype
);
983 rtype
= integer_promotion(rtype
);
985 // The rest do usual conversions
986 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
987 && expr
->left
->op
== '!';
988 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
989 && expr
->right
->op
== '!';
990 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
991 warning(expr
->pos
, "dubious: %sx %c %sy",
994 right_not
? "!" : "");
996 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
997 lclass
, rclass
, ltype
, rtype
);
998 ctype
= rtype
= ltype
;
1001 expr
->left
= cast_to(expr
->left
, ltype
);
1002 expr
->right
= cast_to(expr
->right
, rtype
);
1003 expr
->ctype
= ctype
;
1007 /* pointer (+|-) integer */
1008 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
1009 unrestrict(expr
->right
, rclass
, &rtype
);
1010 return evaluate_ptr_add(expr
, rtype
);
1013 /* integer + pointer */
1014 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
1015 struct expression
*index
= expr
->left
;
1016 unrestrict(index
, lclass
, <ype
);
1017 expr
->left
= expr
->right
;
1018 expr
->right
= index
;
1019 return evaluate_ptr_add(expr
, ltype
);
1022 /* pointer - pointer */
1023 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
1024 return evaluate_ptr_sub(expr
);
1026 return bad_expr_type(expr
);
1029 static struct symbol
*evaluate_comma(struct expression
*expr
)
1031 expr
->ctype
= unqualify_type(degenerate(expr
->right
));
1032 if (expr
->ctype
== &null_ctype
)
1033 expr
->ctype
= &ptr_ctype
;
1034 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
1038 static int modify_for_unsigned(int op
)
1041 op
= SPECIAL_UNSIGNED_LT
;
1043 op
= SPECIAL_UNSIGNED_GT
;
1044 else if (op
== SPECIAL_LTE
)
1045 op
= SPECIAL_UNSIGNED_LTE
;
1046 else if (op
== SPECIAL_GTE
)
1047 op
= SPECIAL_UNSIGNED_GTE
;
1051 enum null_constant_type
{
1057 static inline int is_null_pointer_constant(struct expression
*e
)
1059 if (e
->ctype
== &null_ctype
)
1061 if (!(e
->flags
& CEF_ICE
))
1063 return is_zero_constant(e
) ? NULL_ZERO
: NON_NULL
;
1066 static struct symbol
*evaluate_compare(struct expression
*expr
)
1068 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1069 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
1070 int lclass
= classify_type(degenerate(left
), <ype
);
1071 int rclass
= classify_type(degenerate(right
), &rtype
);
1072 struct symbol
*ctype
;
1073 const char *typediff
;
1076 if (is_type_type(ltype
) && is_type_type(rtype
)) {
1078 * __builtin_types_compatible_p() yields an integer
1079 * constant expression
1081 expr
->flags
= CEF_SET_ICE
;
1085 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1086 warning(expr
->pos
, "testing a 'safe expression'");
1088 expr
->flags
= left
->flags
& right
->flags
& ~CEF_CONST_MASK
& ~CEF_ADDR
;
1090 /* number on number */
1091 if (lclass
& rclass
& TYPE_NUM
) {
1092 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1093 lclass
, rclass
, ltype
, rtype
);
1094 expr
->left
= cast_to(expr
->left
, ctype
);
1095 expr
->right
= cast_to(expr
->right
, ctype
);
1096 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1097 expr
->op
= modify_for_unsigned(expr
->op
);
1101 /* at least one must be a pointer */
1102 if (!((lclass
| rclass
) & TYPE_PTR
))
1103 return bad_expr_type(expr
);
1105 /* equality comparisons can be with null pointer constants */
1106 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1107 int is_null1
= is_null_pointer_constant(left
);
1108 int is_null2
= is_null_pointer_constant(right
);
1109 if (is_null1
== NULL_ZERO
)
1111 if (is_null2
== NULL_ZERO
)
1113 if (is_null1
&& is_null2
) {
1114 int positive
= expr
->op
== SPECIAL_EQUAL
;
1115 expr
->type
= EXPR_VALUE
;
1116 expr
->value
= positive
;
1119 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1120 expr
->left
= cast_to(left
, rtype
);
1123 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1124 expr
->right
= cast_to(right
, ltype
);
1128 /* both should be pointers */
1129 if (!(lclass
& rclass
& TYPE_PTR
))
1130 return bad_expr_type(expr
);
1131 expr
->op
= modify_for_unsigned(expr
->op
);
1133 lbase
= examine_pointer_target(ltype
);
1134 rbase
= examine_pointer_target(rtype
);
1136 /* they also have special treatment for pointers to void */
1137 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1138 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1139 if (lbase
== &void_ctype
) {
1140 expr
->right
= cast_to(right
, ltype
);
1143 if (rbase
== &void_ctype
) {
1144 expr
->left
= cast_to(left
, rtype
);
1150 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1151 target_qualifiers(rtype
),
1152 target_qualifiers(ltype
));
1156 expression_error(expr
, "incompatible types in comparison expression (%s):", typediff
);
1157 info(expr
->pos
, " %s", show_typename(ltype
));
1158 info(expr
->pos
, " %s", show_typename(rtype
));
1162 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1163 expr
->ctype
= &int_ctype
;
1168 * NOTE! The degenerate case of "x ? : y", where we don't
1169 * have a true case, this will possibly promote "x" to the
1170 * same type as "y", and thus _change_ the conditional
1171 * test in the expression. But since promotion is "safe"
1172 * for testing, that's OK.
1174 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1176 struct expression
**cond
;
1177 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1179 const char * typediff
;
1182 if (!evaluate_conditional(expr
->conditional
, 0))
1184 if (!evaluate_expression(expr
->cond_false
))
1187 ctype
= degenerate(expr
->conditional
);
1188 rtype
= degenerate(expr
->cond_false
);
1190 cond
= &expr
->conditional
;
1192 if (expr
->cond_true
) {
1193 if (!evaluate_expression(expr
->cond_true
))
1195 ltype
= degenerate(expr
->cond_true
);
1196 cond
= &expr
->cond_true
;
1199 expr
->flags
= (expr
->conditional
->flags
& (*cond
)->flags
&
1200 expr
->cond_false
->flags
& ~CEF_CONST_MASK
);
1202 * In the standard, it is defined that an integer constant expression
1203 * shall only have operands that are themselves constant [6.6(6)].
1204 * While this definition is very clear for expressions that need all
1205 * their operands to be evaluated, for conditional expressions with a
1206 * constant condition things are much less obvious.
1207 * So, as an extension, do the same as GCC seems to do:
1208 * Consider a conditional expression with a constant condition
1209 * as having the same constantness as the argument corresponding
1210 * to the truth value (including in the case of address constants
1211 * which are defined more stricly [6.6(9)]).
1213 if (expr
->conditional
->flags
& (CEF_ACE
| CEF_ADDR
)) {
1214 int is_true
= expr_truth_value(expr
->conditional
);
1215 struct expression
*arg
= is_true
? *cond
: expr
->cond_false
;
1216 expr
->flags
= arg
->flags
& ~CEF_CONST_MASK
;
1219 lclass
= classify_type(ltype
, <ype
);
1220 rclass
= classify_type(rtype
, &rtype
);
1221 if (lclass
& rclass
& TYPE_NUM
) {
1222 ctype
= usual_conversions('?', *cond
, expr
->cond_false
,
1223 lclass
, rclass
, ltype
, rtype
);
1224 *cond
= cast_to(*cond
, ctype
);
1225 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1229 if ((lclass
| rclass
) & TYPE_PTR
) {
1230 int is_null1
= is_null_pointer_constant(*cond
);
1231 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1233 if (is_null1
&& is_null2
) {
1234 *cond
= cast_to(*cond
, &ptr_ctype
);
1235 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1239 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1240 if (is_null1
== NULL_ZERO
)
1242 *cond
= cast_to(*cond
, rtype
);
1246 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1247 if (is_null2
== NULL_ZERO
)
1248 bad_null(expr
->cond_false
);
1249 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1253 if (!(lclass
& rclass
& TYPE_PTR
)) {
1254 typediff
= "different types";
1257 /* OK, it's pointer on pointer */
1258 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1259 typediff
= "different address spaces";
1263 /* need to be lazier here */
1264 lbase
= examine_pointer_target(ltype
);
1265 rbase
= examine_pointer_target(rtype
);
1266 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1268 if (lbase
== &void_ctype
) {
1269 /* XXX: pointers to function should warn here */
1274 if (rbase
== &void_ctype
) {
1275 /* XXX: pointers to function should warn here */
1279 /* XXX: that should be pointer to composite */
1281 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1288 /* void on void, struct on same struct, union on same union */
1289 if (ltype
== rtype
) {
1293 typediff
= "different base types";
1296 expression_error(expr
, "incompatible types in conditional expression (%s):", typediff
);
1297 info(expr
->pos
, " %s", show_typename(ltype
));
1298 info(expr
->pos
, " %s", show_typename(rtype
));
1300 * if the condition is constant, the type is in fact known
1301 * so use it, as gcc & clang do.
1303 switch (expr_truth_value(expr
->conditional
)) {
1304 case 1: expr
->ctype
= ltype
;
1306 case 0: expr
->ctype
= rtype
;
1314 expr
->ctype
= ctype
;
1318 if (qual
& ~ctype
->ctype
.modifiers
) {
1319 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1321 sym
->ctype
.modifiers
|= qual
;
1324 *cond
= cast_to(*cond
, ctype
);
1325 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1329 /* FP assignments can not do modulo or bit operations */
1330 static int compatible_float_op(int op
)
1332 return op
== SPECIAL_ADD_ASSIGN
||
1333 op
== SPECIAL_SUB_ASSIGN
||
1334 op
== SPECIAL_MUL_ASSIGN
||
1335 op
== SPECIAL_DIV_ASSIGN
;
1338 static int evaluate_assign_op(struct expression
*expr
)
1340 struct symbol
*target
= expr
->left
->ctype
;
1341 struct symbol
*source
= expr
->right
->ctype
;
1342 struct symbol
*t
, *s
;
1343 int tclass
= classify_type(target
, &t
);
1344 int sclass
= classify_type(source
, &s
);
1347 if (tclass
& sclass
& TYPE_NUM
) {
1348 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1349 expression_error(expr
, "invalid assignment");
1352 if (tclass
& TYPE_RESTRICT
) {
1353 if (!restricted_binop(op
, t
)) {
1354 warning(expr
->pos
, "bad assignment (%s) to %s",
1355 show_special(op
), show_typename(t
));
1356 expr
->right
= cast_to(expr
->right
, target
);
1359 /* allowed assignments unfoul */
1360 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1362 if (!restricted_value(expr
->right
, t
))
1364 } else if (op
== SPECIAL_SHR_ASSIGN
|| op
== SPECIAL_SHL_ASSIGN
) {
1365 // shifts do integer promotions, but that's it.
1366 unrestrict(expr
->left
, tclass
, &t
);
1367 target
= integer_promotion(t
);
1369 unrestrict(expr
->right
, sclass
, &s
);
1370 source
= integer_promotion(s
);
1371 expr
->right
= cast_to(expr
->right
, source
);
1373 // both gcc & clang seems to do this, so ...
1374 if (target
->bit_size
> source
->bit_size
)
1375 expr
->right
= cast_to(expr
->right
, &uint_ctype
);
1378 } else if (!(sclass
& TYPE_RESTRICT
))
1380 /* source and target would better be identical restricted */
1383 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1384 info(expr
->pos
, " left side has type %s", show_typename(t
));
1385 info(expr
->pos
, " right side has type %s", show_typename(s
));
1386 expr
->right
= cast_to(expr
->right
, target
);
1389 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1390 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1391 unrestrict(expr
->right
, sclass
, &s
);
1392 evaluate_ptr_add(expr
, s
);
1395 expression_error(expr
, "invalid pointer assignment");
1399 expression_error(expr
, "invalid assignment");
1403 target
= usual_conversions(op
, expr
->left
, expr
->right
,
1404 tclass
, sclass
, target
, source
);
1406 expr
->right
= cast_to(expr
->right
, target
);
1410 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1413 return 0; /* yes, 0 - we don't want a cast_to here */
1414 if (t1
== &void_ctype
)
1416 if (t2
== &void_ctype
)
1418 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1420 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1424 if (t1
->rank
== -2 && t2
->rank
== -2)
1426 if (t1
->rank
!= t2
->rank
)
1431 static int check_assignment_types(struct symbol
*target
, struct expression
**rp
,
1432 const char **typediff
)
1434 struct symbol
*source
= degenerate(*rp
);
1435 struct symbol
*t
, *s
;
1436 int tclass
= classify_type(target
, &t
);
1437 int sclass
= classify_type(source
, &s
);
1439 if (tclass
& sclass
& TYPE_NUM
) {
1440 if (tclass
& TYPE_RESTRICT
) {
1441 /* allowed assignments unfoul */
1442 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1444 if (!restricted_value(*rp
, target
))
1448 } else if (!(sclass
& TYPE_RESTRICT
))
1450 if (t
== &bool_ctype
) {
1451 if (is_fouled_type(s
))
1452 warning((*rp
)->pos
, "%s degrades to integer",
1453 show_typename(s
->ctype
.base_type
));
1456 *typediff
= "different base types";
1460 if (tclass
== TYPE_PTR
) {
1461 unsigned long mod1
, mod2
;
1462 unsigned long modl
, modr
;
1463 struct symbol
*b1
, *b2
;
1464 // NULL pointer is always OK
1465 int is_null
= is_null_pointer_constant(*rp
);
1467 if (is_null
== NULL_ZERO
)
1471 if (!(sclass
& TYPE_PTR
)) {
1472 *typediff
= "different base types";
1475 b1
= examine_pointer_target(t
);
1476 b2
= examine_pointer_target(s
);
1477 mod1
= t
->ctype
.modifiers
& MOD_IGN
;
1478 mod2
= s
->ctype
.modifiers
& MOD_IGN
;
1479 if (whitelist_pointers(b1
, b2
)) {
1481 * assignments to/from void * are OK, provided that
1482 * we do not remove qualifiers from pointed to [C]
1483 * or mix address spaces [sparse].
1485 if (t
->ctype
.as
!= s
->ctype
.as
) {
1486 *typediff
= "different address spaces";
1490 * If this is a function pointer assignment, it is
1491 * actually fine to assign a pointer to const data to
1492 * it, as a function pointer points to const data
1493 * implicitly, i.e., dereferencing it does not produce
1496 if (b1
->type
== SYM_FN
)
1498 if (mod2
& ~mod1
& ~MOD_FUN_ATTR
) {
1499 *typediff
= "different modifiers";
1504 /* It's OK if the target is more volatile or const than the source */
1505 /* It's OK if the source is more pure/noreturn than the target */
1506 modr
= mod1
& ~MOD_REV_QUAL
;
1507 modl
= mod2
& MOD_REV_QUAL
;
1508 *typediff
= type_difference(&t
->ctype
, &s
->ctype
, modl
, modr
);
1514 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1517 if (tclass
& TYPE_NUM
) {
1518 /* XXX: need to turn into comparison with NULL */
1519 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1521 *typediff
= "different base types";
1524 *typediff
= "invalid types";
1528 *rp
= cast_to(*rp
, target
);
1532 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1533 struct expression
**rp
, const char *where
)
1535 const char *typediff
;
1537 if (!check_assignment_types(target
, rp
, &typediff
)) {
1538 struct symbol
*source
= *rp
? (*rp
)->ctype
: NULL
;
1539 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1540 info(expr
->pos
, " expected %s", show_typename(target
));
1541 info(expr
->pos
, " got %s", show_typename(source
));
1542 *rp
= cast_to(*rp
, target
);
1549 static int compatible_transparent_union(struct symbol
*target
,
1550 struct expression
**rp
)
1552 struct symbol
*t
, *member
;
1553 classify_type(target
, &t
);
1554 if (t
->type
!= SYM_UNION
|| !t
->transparent_union
)
1557 FOR_EACH_PTR(t
->symbol_list
, member
) {
1558 const char *typediff
;
1559 if (check_assignment_types(member
, rp
, &typediff
))
1561 } END_FOR_EACH_PTR(member
);
1566 static int compatible_argument_type(struct expression
*expr
, struct symbol
*target
,
1567 struct expression
**rp
, const char *where
)
1569 if (compatible_transparent_union(target
, rp
))
1572 return compatible_assignment_types(expr
, target
, rp
, where
);
1575 static void mark_addressable(struct expression
*expr
)
1577 while (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
1579 if (expr
->type
== EXPR_SYMBOL
) {
1580 struct symbol
*sym
= expr
->symbol
;
1581 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1585 static void mark_assigned(struct expression
*expr
)
1591 switch (expr
->type
) {
1596 if (sym
->type
!= SYM_NODE
)
1598 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1602 mark_assigned(expr
->left
);
1603 mark_assigned(expr
->right
);
1606 case EXPR_FORCE_CAST
:
1607 mark_assigned(expr
->cast_expression
);
1610 mark_assigned(expr
->base
);
1618 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1620 if (type
->ctype
.modifiers
& MOD_CONST
)
1621 expression_error(left
, "assignment to const expression");
1623 /* We know left is an lvalue, so it's a "preop-*" */
1624 mark_assigned(left
->unop
);
1627 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1629 struct expression
*left
= expr
->left
;
1630 struct symbol
*ltype
;
1632 if (!lvalue_expression(left
)) {
1633 expression_error(expr
, "not an lvalue");
1637 ltype
= left
->ctype
;
1639 if (expr
->op
!= '=') {
1640 if (!evaluate_assign_op(expr
))
1643 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, "assignment"))
1647 evaluate_assign_to(left
, ltype
);
1649 expr
->ctype
= ltype
;
1653 static void examine_fn_arguments(struct symbol
*fn
)
1657 FOR_EACH_PTR(fn
->arguments
, s
) {
1658 struct symbol
*arg
= evaluate_symbol(s
);
1659 /* Array/function arguments silently degenerate into pointers */
1665 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1666 if (arg
->type
== SYM_ARRAY
)
1667 ptr
->ctype
= arg
->ctype
;
1669 ptr
->ctype
.base_type
= arg
;
1670 combine_address_space(s
->pos
, &ptr
->ctype
.as
, s
->ctype
.as
);
1671 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1673 s
->ctype
.base_type
= ptr
;
1675 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1678 examine_symbol_type(s
);
1685 } END_FOR_EACH_PTR(s
);
1688 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, struct ident
*as
, int mod
)
1690 /* Take the modifiers of the pointer, and apply them to the member */
1691 mod
|= sym
->ctype
.modifiers
;
1692 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1693 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1695 newsym
->ctype
.as
= as
;
1696 newsym
->ctype
.modifiers
= mod
;
1702 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1704 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1705 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1707 node
->ctype
.base_type
= ptr
;
1708 ptr
->bit_size
= bits_in_pointer
;
1709 ptr
->ctype
.alignment
= pointer_alignment
;
1711 node
->bit_size
= bits_in_pointer
;
1712 node
->ctype
.alignment
= pointer_alignment
;
1715 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1716 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1717 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1719 if (sym
->type
== SYM_NODE
) {
1720 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1721 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1722 sym
= sym
->ctype
.base_type
;
1724 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1725 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1726 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1727 sym
= sym
->ctype
.base_type
;
1729 ptr
->ctype
.base_type
= sym
;
1734 /* Arrays degenerate into pointers on pointer arithmetic */
1735 static struct symbol
*degenerate(struct expression
*expr
)
1737 struct symbol
*ctype
, *base
;
1741 ctype
= expr
->ctype
;
1744 base
= examine_symbol_type(ctype
);
1745 if (ctype
->type
== SYM_NODE
)
1746 base
= ctype
->ctype
.base_type
;
1748 * Arrays degenerate into pointers to the entries, while
1749 * functions degenerate into pointers to themselves.
1750 * If array was part of non-lvalue compound, we create a copy
1751 * of that compound first and then act as if we were dealing with
1752 * the corresponding field in there.
1754 switch (base
->type
) {
1756 if (expr
->type
== EXPR_SLICE
) {
1757 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1758 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1760 a
->ctype
.base_type
= expr
->base
->ctype
;
1761 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1762 a
->array_size
= expr
->base
->ctype
->array_size
;
1764 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1766 e0
->ctype
= &lazy_ptr_ctype
;
1768 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1771 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1773 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1775 e2
->right
= expr
->base
;
1777 e2
->ctype
= expr
->base
->ctype
;
1779 if (expr
->r_bitpos
) {
1780 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1783 e3
->right
= alloc_const_expression(expr
->pos
,
1784 bits_to_bytes(expr
->r_bitpos
));
1785 e3
->ctype
= &lazy_ptr_ctype
;
1790 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1793 e4
->ctype
= &lazy_ptr_ctype
;
1796 expr
->type
= EXPR_PREOP
;
1800 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1801 expression_error(expr
, "strange non-value function or array");
1805 sparse_error(expr
->pos
, "taking the address of built-in function '%s'", show_ident(ctype
->ident
));
1806 *expr
= *expr
->unop
;
1807 ctype
= create_pointer(expr
, ctype
, 1);
1808 expr
->ctype
= ctype
;
1809 mark_addressable(expr
);
1816 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1818 struct expression
*op
= expr
->unop
;
1819 struct symbol
*ctype
;
1821 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1822 expression_error(expr
, "not addressable");
1827 sparse_error(expr
->pos
, "taking the address of built-in function '%s'", show_ident(ctype
->ident
));
1830 mark_addressable(expr
);
1833 * symbol expression evaluation is lazy about the type
1834 * of the sub-expression, so we may have to generate
1835 * the type here if so..
1837 if (expr
->ctype
== &lazy_ptr_ctype
) {
1838 ctype
= create_pointer(expr
, ctype
, 0);
1839 expr
->ctype
= ctype
;
1845 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1847 struct expression
*op
= expr
->unop
;
1848 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1850 /* Simplify: *&(expr) => (expr) */
1851 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1853 expr
->flags
= CEF_NONE
;
1857 examine_symbol_type(ctype
);
1859 /* Dereferencing a node drops all the node information. */
1860 if (ctype
->type
== SYM_NODE
)
1861 ctype
= ctype
->ctype
.base_type
;
1863 target
= ctype
->ctype
.base_type
;
1865 switch (ctype
->type
) {
1867 expression_error(expr
, "cannot dereference this type");
1873 examine_symbol_type(target
);
1874 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1875 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1876 merge_type(node
, ctype
);
1880 if (!lvalue_expression(op
)) {
1881 expression_error(op
, "non-lvalue array??");
1885 /* Do the implied "addressof" on the array */
1889 * When an array is dereferenced, we need to pick
1890 * up the attributes of the original node too..
1892 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1893 merge_type(node
, op
->ctype
);
1894 merge_type(node
, ctype
);
1898 node
->bit_size
= target
->bit_size
;
1899 node
->array_size
= target
->array_size
;
1906 * Unary post-ops: x++ and x--
1908 static struct symbol
*evaluate_postop(struct expression
*expr
)
1910 struct expression
*op
= expr
->unop
;
1911 struct symbol
*ctype
= op
->ctype
;
1912 int class = classify_type(ctype
, &ctype
);
1915 if (!class || class & TYPE_COMPOUND
) {
1916 expression_error(expr
, "need scalar for ++/--");
1919 if (!lvalue_expression(expr
->unop
)) {
1920 expression_error(expr
, "need lvalue expression for ++/--");
1924 unrestrict(expr
, class, &ctype
);
1926 if (class & TYPE_NUM
) {
1928 } else if (class == TYPE_PTR
) {
1929 struct symbol
*target
= examine_pointer_target(ctype
);
1930 if (!is_function(target
))
1931 multiply
= bits_to_bytes(target
->bit_size
);
1935 evaluate_assign_to(op
, op
->ctype
);
1936 expr
->op_value
= multiply
;
1937 expr
->ctype
= ctype
;
1941 expression_error(expr
, "bad argument type for ++/--");
1945 static struct symbol
*evaluate_sign(struct expression
*expr
)
1947 struct symbol
*ctype
= expr
->unop
->ctype
;
1948 int class = classify_type(ctype
, &ctype
);
1949 unsigned char flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1951 /* should be an arithmetic type */
1952 if (!(class & TYPE_NUM
))
1953 return bad_expr_type(expr
);
1954 if (class & TYPE_RESTRICT
)
1957 if (!(class & TYPE_FLOAT
)) {
1958 ctype
= integer_promotion(ctype
);
1959 expr
->unop
= cast_to(expr
->unop
, ctype
);
1960 } else if (expr
->op
!= '~') {
1961 /* no conversions needed */
1963 return bad_expr_type(expr
);
1965 if (expr
->op
== '+')
1966 *expr
= *expr
->unop
;
1967 expr
->flags
= flags
;
1968 expr
->ctype
= ctype
;
1971 if (restricted_unop(expr
->op
, &ctype
))
1972 unrestrict(expr
, class, &ctype
);
1976 static struct symbol
*evaluate_preop(struct expression
*expr
)
1978 struct symbol
*ctype
= expr
->unop
->ctype
;
1982 *expr
= *expr
->unop
;
1988 return evaluate_sign(expr
);
1991 return evaluate_dereference(expr
);
1994 return evaluate_addressof(expr
);
1996 case SPECIAL_INCREMENT
:
1997 case SPECIAL_DECREMENT
:
1999 * From a type evaluation standpoint the preops are
2000 * the same as the postops
2002 return evaluate_postop(expr
);
2005 ctype
= degenerate(expr
->unop
);
2006 expr
->flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
2008 * A logical negation never yields an address constant
2011 expr
->flags
&= ~CEF_ADDR
;
2013 if (is_safe_type(ctype
))
2014 warning(expr
->pos
, "testing a 'safe expression'");
2015 if (is_float_type(ctype
)) {
2016 struct expression
*arg
= expr
->unop
;
2017 expr
->type
= EXPR_COMPARE
;
2018 expr
->op
= SPECIAL_EQUAL
;
2020 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
2021 expr
->right
->ctype
= ctype
;
2022 expr
->right
->fvalue
= 0;
2023 } else if (is_fouled_type(ctype
)) {
2024 warning(expr
->pos
, "%s degrades to integer",
2025 show_typename(ctype
->ctype
.base_type
));
2027 /* the result is int [6.5.3.3(5)]*/
2034 expr
->ctype
= ctype
;
2038 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
2040 struct ptr_list
*head
= (struct ptr_list
*)_list
;
2041 struct ptr_list
*list
= head
;
2047 for (i
= 0; i
< list
->nr
; i
++) {
2048 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
2050 if (sym
->ident
!= ident
)
2052 *offset
= sym
->offset
;
2055 struct symbol
*ctype
= sym
->ctype
.base_type
;
2059 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
2061 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
2064 *offset
+= sym
->offset
;
2068 } while ((list
= list
->next
) != head
);
2072 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
2074 struct expression
*add
;
2077 * Create a new add-expression
2079 * NOTE! Even if we just add zero, we need a new node
2080 * for the member pointer, since it has a different
2081 * type than the original pointer. We could make that
2082 * be just a cast, but the fact is, a node is a node,
2083 * so we might as well just do the "add zero" here.
2085 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
2088 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2089 add
->right
->ctype
= &int_ctype
;
2090 add
->right
->value
= offset
;
2093 * The ctype of the pointer will be lazily evaluated if
2094 * we ever take the address of this member dereference..
2096 add
->ctype
= &lazy_ptr_ctype
;
2098 * The resulting address of a member access through an address
2099 * constant is an address constant again [6.6(9)].
2101 add
->flags
= expr
->flags
;
2106 /* structure/union dereference */
2107 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
2110 struct symbol
*ctype
, *member
;
2111 struct expression
*deref
= expr
->deref
, *add
;
2112 struct ident
*ident
= expr
->member
;
2113 struct ident
*address_space
;
2116 if (!evaluate_expression(deref
))
2119 expression_error(expr
, "bad member name");
2123 ctype
= deref
->ctype
;
2124 examine_symbol_type(ctype
);
2125 address_space
= ctype
->ctype
.as
;
2126 mod
= ctype
->ctype
.modifiers
;
2127 if (ctype
->type
== SYM_NODE
) {
2128 ctype
= ctype
->ctype
.base_type
;
2129 combine_address_space(deref
->pos
, &address_space
, ctype
->ctype
.as
);
2130 mod
|= ctype
->ctype
.modifiers
;
2132 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
2133 expression_error(expr
, "expected structure or union");
2137 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
2139 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
2140 const char *name
= "<unnamed>";
2143 name
= ctype
->ident
->name
;
2144 namelen
= ctype
->ident
->len
;
2146 if (ctype
->symbol_list
)
2147 expression_error(expr
, "no member '%s' in %s %.*s",
2148 show_ident(ident
), type
, namelen
, name
);
2150 expression_error(expr
, "using member '%s' in "
2151 "incomplete %s %.*s", show_ident(ident
),
2152 type
, namelen
, name
);
2157 * The member needs to take on the address space and modifiers of
2158 * the "parent" type.
2160 member
= convert_to_as_mod(member
, address_space
, mod
);
2161 ctype
= get_base_type(member
);
2163 if (!lvalue_expression(deref
)) {
2164 if (deref
->type
!= EXPR_SLICE
) {
2168 expr
->base
= deref
->base
;
2169 expr
->r_bitpos
= deref
->r_bitpos
;
2171 expr
->r_bitpos
+= bytes_to_bits(offset
);
2172 expr
->type
= EXPR_SLICE
;
2173 expr
->r_nrbits
= member
->bit_size
;
2174 expr
->r_bitpos
+= member
->bit_offset
;
2175 expr
->ctype
= member
;
2179 deref
= deref
->unop
;
2180 expr
->deref
= deref
;
2182 add
= evaluate_offset(deref
, offset
);
2183 expr
->type
= EXPR_PREOP
;
2187 expr
->ctype
= member
;
2191 static int is_promoted(struct expression
*expr
)
2194 switch (expr
->type
) {
2197 case EXPR_CONDITIONAL
:
2221 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2223 struct symbol
*sym
= expr
->cast_type
;
2225 sym
= evaluate_expression(expr
->cast_expression
);
2229 * Expressions of restricted types will possibly get
2230 * promoted - check that here
2232 if (is_restricted_type(sym
)) {
2233 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2235 } else if (is_fouled_type(sym
)) {
2239 examine_symbol_type(sym
);
2240 if (is_bitfield_type(sym
)) {
2241 expression_error(expr
, "trying to examine bitfield type");
2247 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2249 struct symbol
*type
;
2252 type
= evaluate_type_information(expr
);
2256 size
= type
->bit_size
;
2258 if (size
< 0 && is_void_type(type
)) {
2260 warning(expr
->pos
, "expression using sizeof(void)");
2261 size
= bits_in_char
;
2264 if (is_bool_type(type
)) {
2266 warning(expr
->pos
, "expression using sizeof _Bool");
2267 size
= bits_to_bytes(bits_in_bool
) * bits_in_char
;
2270 if (is_function(type
->ctype
.base_type
)) {
2272 warning(expr
->pos
, "expression using sizeof on a function");
2273 size
= bits_in_char
;
2276 if (has_flexible_array(type
) && Wflexible_array_sizeof
)
2277 warning(expr
->pos
, "using sizeof on a flexible structure");
2279 if (is_array_type(type
) && size
< 0) { // VLA, 1-dimension only
2280 struct expression
*base
, *size
;
2281 struct symbol
*base_type
;
2283 if (type
->type
== SYM_NODE
)
2284 type
= type
->ctype
.base_type
; // strip the SYM_NODE
2285 base_type
= get_base_type(type
);
2288 if (base_type
->bit_size
<= 0) {
2289 base
= alloc_expression(expr
->pos
, EXPR_SIZEOF
);
2290 base
->cast_type
= base_type
;
2291 if (!evaluate_sizeof(base
))
2294 base
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2295 base
->value
= bits_to_bytes(base_type
->bit_size
);
2296 base
->ctype
= size_t_ctype
;
2298 size
= alloc_expression(expr
->pos
, EXPR_CAST
);
2299 size
->cast_type
= size_t_ctype
;
2300 size
->cast_expression
= type
->array_size
;
2301 if (!evaluate_expression(size
))
2305 expr
->type
= EXPR_BINOP
;
2307 return expr
->ctype
= size_t_ctype
;
2311 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2312 expression_error(expr
, "cannot size expression");
2314 expr
->type
= EXPR_VALUE
;
2315 expr
->value
= bits_to_bytes(size
);
2317 expr
->ctype
= size_t_ctype
;
2318 return size_t_ctype
;
2321 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2323 struct symbol
*type
;
2326 type
= evaluate_type_information(expr
);
2330 if (type
->type
== SYM_NODE
)
2331 type
= type
->ctype
.base_type
;
2334 switch (type
->type
) {
2338 type
= get_base_type(type
);
2342 expression_error(expr
, "expected pointer expression");
2345 size
= type
->bit_size
;
2346 if (size
& (bits_in_char
-1))
2348 expr
->type
= EXPR_VALUE
;
2349 expr
->value
= bits_to_bytes(size
);
2351 expr
->ctype
= size_t_ctype
;
2352 return size_t_ctype
;
2355 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2357 struct symbol
*type
;
2359 type
= evaluate_type_information(expr
);
2363 expr
->type
= EXPR_VALUE
;
2364 expr
->value
= type
->ctype
.alignment
;
2366 expr
->ctype
= size_t_ctype
;
2367 return size_t_ctype
;
2370 int evaluate_arguments(struct symbol_list
*argtypes
, struct expression_list
*head
)
2372 struct expression
*expr
;
2373 struct symbol
*argtype
;
2376 PREPARE_PTR_LIST(argtypes
, argtype
);
2377 FOR_EACH_PTR (head
, expr
) {
2378 struct expression
**p
= THIS_ADDRESS(expr
);
2379 struct symbol
*ctype
, *target
;
2380 ctype
= evaluate_expression(expr
);
2387 struct symbol
*type
;
2388 int class = classify_type(ctype
, &type
);
2389 if (is_int(class)) {
2390 *p
= cast_to(expr
, integer_promotion(type
));
2391 } else if (class & TYPE_FLOAT
) {
2393 *p
= cast_to(expr
, &double_ctype
);
2394 } else if (class & TYPE_PTR
) {
2395 if (expr
->ctype
== &null_ctype
)
2396 *p
= cast_to(expr
, &ptr_ctype
);
2400 } else if (!target
->forced_arg
){
2401 static char where
[30];
2402 examine_symbol_type(target
);
2403 sprintf(where
, "argument %d", i
);
2404 compatible_argument_type(expr
, target
, p
, where
);
2408 NEXT_PTR_LIST(argtype
);
2409 } END_FOR_EACH_PTR(expr
);
2410 FINISH_PTR_LIST(argtype
);
2414 static void convert_index(struct expression
*e
)
2416 struct expression
*child
= e
->idx_expression
;
2417 unsigned from
= e
->idx_from
;
2418 unsigned to
= e
->idx_to
+ 1;
2420 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2421 e
->init_nr
= to
- from
;
2422 e
->init_expr
= child
;
2425 static void convert_ident(struct expression
*e
)
2427 struct expression
*child
= e
->ident_expression
;
2428 int offset
= e
->offset
;
2431 e
->init_offset
= offset
;
2433 e
->init_expr
= child
;
2436 static void convert_designators(struct expression
*e
)
2439 if (e
->type
== EXPR_INDEX
)
2441 else if (e
->type
== EXPR_IDENTIFIER
)
2449 static void excess(struct expression
*e
, const char *s
)
2451 warning(e
->pos
, "excessive elements in %s initializer", s
);
2455 * implicit designator for the first element
2457 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2458 struct expression
**v
)
2460 struct expression
*e
= *v
, *new;
2462 if (ctype
->type
== SYM_NODE
)
2463 ctype
= ctype
->ctype
.base_type
;
2465 if (class & TYPE_PTR
) { /* array */
2466 if (!ctype
->bit_size
)
2468 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2469 new->idx_expression
= e
;
2470 new->ctype
= ctype
->ctype
.base_type
;
2472 struct symbol
*field
, *p
;
2473 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2474 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2480 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2481 new->ident_expression
= e
;
2482 new->field
= new->ctype
= field
;
2483 new->offset
= field
->offset
;
2490 * sanity-check explicit designators; return the innermost one or NULL
2491 * in case of error. Assign types.
2493 static struct expression
*check_designators(struct expression
*e
,
2494 struct symbol
*ctype
)
2496 struct expression
*last
= NULL
;
2499 if (ctype
->type
== SYM_NODE
)
2500 ctype
= ctype
->ctype
.base_type
;
2501 if (e
->type
== EXPR_INDEX
) {
2502 struct symbol
*type
;
2503 if (ctype
->type
!= SYM_ARRAY
) {
2504 err
= "array index in non-array";
2507 type
= ctype
->ctype
.base_type
;
2508 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2509 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2510 if (offset
>= ctype
->bit_size
) {
2511 err
= "index out of bounds in";
2515 e
->ctype
= ctype
= type
;
2518 if (!e
->idx_expression
) {
2522 e
= e
->idx_expression
;
2523 } else if (e
->type
== EXPR_IDENTIFIER
) {
2525 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2526 err
= "field name not in struct or union";
2529 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2531 err
= "unknown field name in";
2535 e
->field
= e
->ctype
= ctype
;
2537 if (!e
->ident_expression
) {
2541 e
= e
->ident_expression
;
2542 } else if (e
->type
== EXPR_POS
) {
2543 err
= "internal front-end error: EXPR_POS in";
2548 expression_error(e
, "%s initializer", err
);
2553 * choose the next subobject to initialize.
2555 * Get designators for next element, switch old ones to EXPR_POS.
2556 * Return the resulting expression or NULL if we'd run out of subobjects.
2557 * The innermost designator is returned in *v. Designators in old
2558 * are assumed to be already sanity-checked.
2560 static struct expression
*next_designators(struct expression
*old
,
2561 struct symbol
*ctype
,
2562 struct expression
*e
, struct expression
**v
)
2564 struct expression
*new = NULL
;
2568 if (old
->type
== EXPR_INDEX
) {
2569 struct expression
*copy
;
2572 copy
= next_designators(old
->idx_expression
,
2575 n
= old
->idx_to
+ 1;
2576 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2581 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2584 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2587 new->idx_from
= new->idx_to
= n
;
2588 new->idx_expression
= copy
;
2589 new->ctype
= old
->ctype
;
2591 } else if (old
->type
== EXPR_IDENTIFIER
) {
2592 struct expression
*copy
;
2593 struct symbol
*field
;
2596 copy
= next_designators(old
->ident_expression
,
2599 field
= old
->field
->next_subobject
;
2605 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2607 * We can't necessarily trust "field->offset",
2608 * because the field might be in an anonymous
2609 * union, and the field offset is then the offset
2610 * within that union.
2612 * The "old->offset - old->field->offset"
2613 * would be the offset of such an anonymous
2616 offset
= old
->offset
- old
->field
->offset
;
2619 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2623 new->expr_ident
= field
->ident
;
2624 new->ident_expression
= copy
;
2626 new->offset
= field
->offset
+ offset
;
2632 static int handle_initializer(struct expression
**ep
, int nested
,
2633 int class, struct symbol
*ctype
, unsigned long mods
);
2636 * deal with traversing subobjects [6.7.8(17,18,20)]
2638 static void handle_list_initializer(struct expression
*expr
,
2639 int class, struct symbol
*ctype
, unsigned long mods
)
2641 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2642 int jumped
= 0; // has the last designator multiple levels?
2644 if (expr
->zero_init
)
2645 free_ptr_list(&expr
->expr_list
);
2647 FOR_EACH_PTR(expr
->expr_list
, e
) {
2648 struct expression
**v
;
2649 struct symbol
*type
;
2652 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2653 struct symbol
*struct_sym
;
2656 last
= first_subobject(ctype
, class, &top
);
2658 last
= next_designators(last
, ctype
, e
, &top
);
2661 excess(e
, class & TYPE_PTR
? "array" :
2663 DELETE_CURRENT_PTR(e
);
2666 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2667 if (Wdesignated_init
&& struct_sym
->designated_init
)
2668 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2669 ctype
->ident
? "in initializer for " : "",
2670 ctype
->ident
? ctype
->ident
->len
: 0,
2671 ctype
->ident
? ctype
->ident
->name
: "",
2672 ctype
->ident
? ": " : "",
2673 get_type_name(struct_sym
->type
),
2674 show_ident(struct_sym
->ident
));
2675 if (jumped
&& Wpast_deep_designator
) {
2676 warning(e
->pos
, "advancing past deep designator");
2679 REPLACE_CURRENT_PTR(e
, last
);
2681 next
= check_designators(e
, ctype
);
2683 DELETE_CURRENT_PTR(e
);
2687 /* deeper than one designator? */
2689 convert_designators(last
);
2694 lclass
= classify_type(top
->ctype
, &type
);
2695 if (top
->type
== EXPR_INDEX
)
2696 v
= &top
->idx_expression
;
2698 v
= &top
->ident_expression
;
2700 mods
|= ctype
->ctype
.modifiers
& MOD_STORAGE
;
2701 if (handle_initializer(v
, 1, lclass
, top
->ctype
, mods
))
2704 if (!(lclass
& TYPE_COMPOUND
)) {
2705 warning(e
->pos
, "bogus scalar initializer");
2706 DELETE_CURRENT_PTR(e
);
2710 next
= first_subobject(type
, lclass
, v
);
2712 warning(e
->pos
, "missing braces around initializer");
2717 DELETE_CURRENT_PTR(e
);
2718 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2720 } END_FOR_EACH_PTR(e
);
2722 convert_designators(last
);
2723 expr
->ctype
= ctype
;
2726 static int is_string_literal(struct expression
**v
)
2728 struct expression
*e
= *v
;
2729 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2731 if (!e
|| e
->type
!= EXPR_STRING
)
2733 if (e
!= *v
&& Wparen_string
)
2735 "array initialized from parenthesized string constant");
2741 * We want a normal expression, possibly in one layer of braces. Warn
2742 * if the latter happens inside a list (it's legal, but likely to be
2743 * an effect of screwup). In case of anything not legal, we are definitely
2744 * having an effect of screwup, so just fail and let the caller warn.
2746 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2748 struct expression
*v
= NULL
, *p
;
2752 if (e
->type
!= EXPR_INITIALIZER
)
2755 FOR_EACH_PTR(e
->expr_list
, p
) {
2759 } END_FOR_EACH_PTR(p
);
2763 case EXPR_INITIALIZER
:
2765 case EXPR_IDENTIFIER
:
2771 warning(e
->pos
, "braces around scalar initializer");
2776 * deal with the cases that don't care about subobjects:
2777 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2778 * character array <- string literal, possibly in braces [6.7.8(14)]
2779 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2780 * compound type <- initializer list in braces [6.7.8(16)]
2781 * The last one punts to handle_list_initializer() which, in turn will call
2782 * us for individual elements of the list.
2784 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2785 * the lack of support of wide char stuff in general.
2787 * One note: we need to take care not to evaluate a string literal until
2788 * we know that we *will* handle it right here. Otherwise we would screw
2789 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2790 * { "string", ...} - we need to preserve that string literal recognizable
2791 * until we dig into the inner struct.
2793 static int handle_initializer(struct expression
**ep
, int nested
,
2794 int class, struct symbol
*ctype
, unsigned long mods
)
2796 struct expression
*e
= *ep
, *p
;
2797 struct symbol
*type
;
2803 if (!(class & TYPE_COMPOUND
)) {
2804 e
= handle_scalar(e
, nested
);
2808 if (!evaluate_expression(e
))
2810 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2812 * Initializers for static storage duration objects
2813 * shall be constant expressions or a string literal [6.7.8(4)].
2815 mods
|= ctype
->ctype
.modifiers
;
2816 mods
&= (MOD_TOPLEVEL
| MOD_STATIC
);
2817 if (mods
&& !(e
->flags
& (CEF_ACE
| CEF_ADDR
)))
2818 if (Wconstexpr_not_const
)
2819 warning(e
->pos
, "non-constant initializer for static object");
2825 * sublist; either a string, or we dig in; the latter will deal with
2826 * pathologies, so we don't need anything fancy here.
2828 if (e
->type
== EXPR_INITIALIZER
) {
2829 if (is_string_type(ctype
)) {
2830 struct expression
*v
= NULL
;
2833 FOR_EACH_PTR(e
->expr_list
, p
) {
2837 } END_FOR_EACH_PTR(p
);
2838 if (count
== 1 && is_string_literal(&v
)) {
2843 handle_list_initializer(e
, class, ctype
, mods
);
2848 if (is_string_literal(&e
)) {
2849 /* either we are doing array of char, or we'll have to dig in */
2850 if (is_string_type(ctype
)) {
2856 /* struct or union can be initialized by compatible */
2857 if (class != TYPE_COMPOUND
)
2859 type
= evaluate_expression(e
);
2862 if (ctype
->type
== SYM_NODE
)
2863 ctype
= ctype
->ctype
.base_type
;
2864 if (type
->type
== SYM_NODE
)
2865 type
= type
->ctype
.base_type
;
2871 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2873 type
= evaluate_expression(p
);
2874 if (ctype
->bit_size
!= -1) {
2875 struct symbol
*char_type
= e
->wide
? wchar_ctype
: &char_ctype
;
2876 unsigned int size_with_null
= ctype
->bit_size
+ char_type
->bit_size
;
2877 if (size_with_null
< type
->bit_size
)
2879 "too long initializer-string for array of char");
2880 else if (Winit_cstring
&& size_with_null
== type
->bit_size
) {
2882 "too long initializer-string for array of char(no space for nul char)");
2889 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2891 struct symbol
*type
;
2892 int class = classify_type(ctype
, &type
);
2893 if (!handle_initializer(ep
, 0, class, ctype
, 0))
2894 expression_error(*ep
, "invalid initializer");
2897 static struct symbol
*cast_to_bool(struct expression
*expr
)
2899 struct expression
*old
= expr
->cast_expression
;
2900 struct expression
*zero
;
2901 struct symbol
*otype
;
2902 int oclass
= classify_type(degenerate(old
), &otype
);
2903 struct symbol
*ctype
;
2905 if (oclass
& TYPE_COMPOUND
)
2908 zero
= alloc_const_expression(expr
->pos
, 0);
2909 if (oclass
& TYPE_PTR
)
2910 zero
->ctype
= otype
;
2911 expr
->op
= SPECIAL_NOTEQUAL
;
2912 ctype
= usual_conversions(expr
->op
, old
, zero
,
2913 oclass
, TYPE_NUM
, otype
, zero
->ctype
);
2914 expr
->type
= EXPR_COMPARE
;
2915 expr
->left
= cast_to(old
, ctype
);
2916 expr
->right
= cast_to(zero
, ctype
);
2921 static int cast_flags(struct expression
*expr
, struct expression
*old
)
2925 int flags
= CEF_NONE
;
2927 class = classify_type(expr
->ctype
, &t
);
2928 if (class & TYPE_NUM
) {
2929 flags
= old
->flags
& ~CEF_CONST_MASK
;
2931 * Casts to numeric types never result in address
2932 * constants [6.6(9)].
2937 * As an extension, treat address constants cast to
2938 * integer type as an arithmetic constant.
2940 if (old
->flags
& CEF_ADDR
)
2944 * Cast to float type -> not an integer constant
2945 * expression [6.6(6)].
2947 if (class & TYPE_FLOAT
)
2948 flags
&= ~CEF_CLR_ICE
;
2950 * Casts of float literals to integer type results in
2951 * a constant integer expression [6.6(6)].
2953 else if (old
->flags
& CEF_FLOAT
)
2954 flags
= CEF_SET_ICE
;
2955 } else if (class & TYPE_PTR
) {
2957 * Casts of integer literals to pointer type yield
2958 * address constants [6.6(9)].
2960 * As an extension, treat address constants cast to a
2961 * different pointer type as address constants again.
2963 * As another extension, treat integer constant
2964 * expressions (in contrast to literals) cast to
2965 * pointer type as address constants.
2967 if (old
->flags
& (CEF_ICE
| CEF_ADDR
))
2975 // check if a type matches one of the members of a union type
2976 // @utype: the union type
2977 // @type: to type to check
2978 // @return: to identifier of the matching type in the union.
2979 static struct symbol
*find_member_type(struct symbol
*utype
, struct symbol
*type
)
2981 struct symbol
*t
, *member
;
2983 if (utype
->type
!= SYM_UNION
)
2986 FOR_EACH_PTR(utype
->symbol_list
, member
) {
2987 classify_type(member
, &t
);
2990 } END_FOR_EACH_PTR(member
);
2994 static struct symbol
*evaluate_compound_literal(struct expression
*expr
, struct expression
*source
)
2996 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2997 struct symbol
*sym
= expr
->cast_type
;
2999 sym
->initializer
= source
;
3000 evaluate_symbol(sym
);
3002 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
3004 if (sym
->ctype
.modifiers
& MOD_TOPLEVEL
)
3005 addr
->flags
|= CEF_ADDR
;
3007 expr
->type
= EXPR_PREOP
;
3014 static struct symbol
*evaluate_cast(struct expression
*expr
)
3016 struct expression
*source
= expr
->cast_expression
;
3017 struct symbol
*ctype
;
3018 struct symbol
*ttype
, *stype
;
3019 struct symbol
*member
;
3021 struct ident
*tas
= NULL
, *sas
= NULL
;
3027 * Special case: a cast can be followed by an
3028 * initializer, in which case we need to pass
3029 * the type value down to that initializer rather
3030 * than trying to evaluate it as an expression
3031 * (cfr. compound literals: C99 & C11 6.5.2.5).
3033 * A more complex case is when the initializer is
3034 * dereferenced as part of a post-fix expression.
3035 * We need to produce an expression that can be dereferenced.
3037 if (source
->type
== EXPR_INITIALIZER
)
3038 return evaluate_compound_literal(expr
, source
);
3040 ctype
= examine_symbol_type(expr
->cast_type
);
3041 ctype
= unqualify_type(ctype
);
3042 expr
->ctype
= ctype
;
3043 expr
->cast_type
= ctype
;
3045 evaluate_expression(source
);
3048 tclass
= classify_type(ctype
, &ttype
);
3050 expr
->flags
= cast_flags(expr
, source
);
3053 * You can always throw a value away by casting to
3054 * "void" - that's an implicit "force". Note that
3055 * the same is _not_ true of "void *".
3057 if (ttype
== &void_ctype
)
3060 stype
= source
->ctype
;
3062 expression_error(expr
, "cast from unknown type");
3065 sclass
= classify_type(stype
, &stype
);
3067 if (expr
->type
== EXPR_FORCE_CAST
)
3070 if (tclass
& (TYPE_COMPOUND
| TYPE_FN
)) {
3072 * Special case: cast to union type (GCC extension)
3073 * The effect is similar to a compound literal except
3074 * that the result is a rvalue.
3076 if ((member
= find_member_type(ttype
, stype
))) {
3077 struct expression
*item
, *init
;
3080 warning(expr
->pos
, "cast to union type");
3082 item
= alloc_expression(source
->pos
, EXPR_IDENTIFIER
);
3083 item
->expr_ident
= member
->ident
;
3084 item
->ident_expression
= source
;
3086 init
= alloc_expression(source
->pos
, EXPR_INITIALIZER
);
3087 add_expression(&init
->expr_list
, item
);
3089 // FIXME: this should be a rvalue
3090 evaluate_compound_literal(expr
, init
);
3094 warning(expr
->pos
, "cast to non-scalar");
3097 if (sclass
& TYPE_COMPOUND
)
3098 warning(expr
->pos
, "cast from non-scalar");
3100 /* allowed cast unfouls */
3101 if (sclass
& TYPE_FOULED
)
3102 stype
= unfoul(stype
);
3104 if (ttype
!= stype
) {
3105 if ((tclass
& TYPE_RESTRICT
) && restricted_value(source
, ttype
))
3106 warning(expr
->pos
, "cast to %s",
3107 show_typename(ttype
));
3108 if (sclass
& TYPE_RESTRICT
) {
3109 if (ttype
== &bool_ctype
) {
3110 if (sclass
& TYPE_FOULED
)
3111 warning(expr
->pos
, "%s degrades to integer",
3112 show_typename(stype
));
3114 warning(expr
->pos
, "cast from %s",
3115 show_typename(stype
));
3120 if ((ttype
== &ulong_ctype
|| ttype
== uintptr_ctype
) && !Wcast_from_as
)
3121 tas
= &bad_address_space
;
3122 else if (tclass
== TYPE_PTR
) {
3123 examine_pointer_target(ttype
);
3124 tas
= ttype
->ctype
.as
;
3127 if ((stype
== &ulong_ctype
|| stype
== uintptr_ctype
))
3128 sas
= &bad_address_space
;
3129 else if (sclass
== TYPE_PTR
) {
3130 examine_pointer_target(stype
);
3131 sas
= stype
->ctype
.as
;
3134 if (!tas
&& valid_as(sas
))
3135 warning(expr
->pos
, "cast removes address space '%s' of expression", show_as(sas
));
3136 if (valid_as(tas
) && valid_as(sas
) && tas
!= sas
)
3137 warning(expr
->pos
, "cast between address spaces (%s -> %s)", show_as(sas
), show_as(tas
));
3138 if (valid_as(tas
) && !sas
&&
3139 !is_null_pointer_constant(source
) && Wcast_to_as
)
3141 "cast adds address space '%s' to expression", show_as(tas
));
3143 if (!(ttype
->ctype
.modifiers
& MOD_PTRINHERIT
) && tclass
== TYPE_PTR
&&
3144 !tas
&& (source
->flags
& CEF_ICE
)) {
3145 if (ttype
->ctype
.base_type
== &void_ctype
) {
3146 if (is_zero_constant(source
)) {
3148 expr
->type
= EXPR_VALUE
;
3149 expr
->ctype
= &null_ctype
;
3156 if (ttype
== &bool_ctype
)
3159 // checks pointers to restricted
3160 while (Wbitwise_pointer
&& tclass
== TYPE_PTR
&& sclass
== TYPE_PTR
) {
3161 tclass
= classify_type(ttype
->ctype
.base_type
, &ttype
);
3162 sclass
= classify_type(stype
->ctype
.base_type
, &stype
);
3165 if (!ttype
|| !stype
)
3167 if (ttype
== &void_ctype
|| stype
== &void_ctype
)
3169 if (tclass
& TYPE_RESTRICT
) {
3170 warning(expr
->pos
, "cast to %s", show_typename(ctype
));
3173 if (sclass
& TYPE_RESTRICT
) {
3174 warning(expr
->pos
, "cast from %s", show_typename(source
->ctype
));
3183 * Evaluate a call expression with a symbol. This
3184 * should expand inline functions, and evaluate
3187 static int evaluate_symbol_call(struct expression
*expr
)
3189 struct expression
*fn
= expr
->fn
;
3190 struct symbol
*ctype
= fn
->ctype
;
3192 if (fn
->type
!= EXPR_PREOP
)
3195 if (ctype
->op
&& ctype
->op
->evaluate
)
3196 return ctype
->op
->evaluate(expr
);
3201 static struct symbol
*evaluate_call(struct expression
*expr
)
3204 struct symbol
*ctype
, *sym
;
3205 struct expression
*fn
= expr
->fn
;
3206 struct expression_list
*arglist
= expr
->args
;
3208 if (!evaluate_expression(fn
))
3210 sym
= ctype
= fn
->ctype
;
3211 if (ctype
->type
== SYM_NODE
)
3212 ctype
= ctype
->ctype
.base_type
;
3213 if (ctype
->type
== SYM_PTR
)
3214 ctype
= get_base_type(ctype
);
3216 if (ctype
->type
!= SYM_FN
) {
3217 struct expression
*arg
;
3219 if (fn
->ctype
== &bad_ctype
)
3222 expression_error(expr
, "not a function %s",
3223 show_ident(sym
->ident
));
3224 /* do typechecking in arguments */
3225 FOR_EACH_PTR (arglist
, arg
) {
3226 evaluate_expression(arg
);
3227 } END_FOR_EACH_PTR(arg
);
3231 examine_fn_arguments(ctype
);
3232 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
3233 sym
->op
&& sym
->op
->args
) {
3234 if (!sym
->op
->args(expr
))
3237 if (!evaluate_arguments(ctype
->arguments
, arglist
))
3239 args
= expression_list_size(expr
->args
);
3240 fnargs
= symbol_list_size(ctype
->arguments
);
3241 if (args
< fnargs
) {
3242 expression_error(expr
,
3243 "not enough arguments for function %s",
3244 show_ident(sym
->ident
));
3247 if (args
> fnargs
&& !ctype
->variadic
)
3248 expression_error(expr
,
3249 "too many arguments for function %s",
3250 show_ident(sym
->ident
));
3252 expr
->ctype
= ctype
->ctype
.base_type
;
3253 if (sym
->type
== SYM_NODE
) {
3254 if (evaluate_symbol_call(expr
))
3260 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
3262 struct expression
*e
= expr
->down
;
3263 struct symbol
*ctype
= expr
->in
;
3266 if (expr
->op
== '.') {
3267 struct symbol
*field
;
3270 expression_error(expr
, "expected structure or union");
3273 examine_symbol_type(ctype
);
3274 class = classify_type(ctype
, &ctype
);
3275 if (class != TYPE_COMPOUND
) {
3276 expression_error(expr
, "expected structure or union");
3280 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
3282 expression_error(expr
, "unknown member");
3286 expr
->type
= EXPR_VALUE
;
3287 expr
->flags
= CEF_SET_ICE
;
3288 expr
->value
= offset
;
3290 expr
->ctype
= size_t_ctype
;
3293 expression_error(expr
, "expected structure or union");
3296 examine_symbol_type(ctype
);
3297 class = classify_type(ctype
, &ctype
);
3298 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
3299 expression_error(expr
, "expected array");
3302 ctype
= ctype
->ctype
.base_type
;
3304 expr
->type
= EXPR_VALUE
;
3305 expr
->flags
= CEF_SET_ICE
;
3308 expr
->ctype
= size_t_ctype
;
3310 struct expression
*idx
= expr
->index
, *m
;
3311 struct symbol
*i_type
= evaluate_expression(idx
);
3312 unsigned old_idx_flags
;
3313 int i_class
= classify_type(i_type
, &i_type
);
3315 if (!is_int(i_class
)) {
3316 expression_error(expr
, "non-integer index");
3319 unrestrict(idx
, i_class
, &i_type
);
3320 old_idx_flags
= idx
->flags
;
3321 idx
= cast_to(idx
, size_t_ctype
);
3322 idx
->flags
= old_idx_flags
;
3323 m
= alloc_const_expression(expr
->pos
,
3324 bits_to_bytes(ctype
->bit_size
));
3325 m
->ctype
= size_t_ctype
;
3326 m
->flags
= CEF_SET_INT
;
3327 expr
->type
= EXPR_BINOP
;
3331 expr
->ctype
= size_t_ctype
;
3332 expr
->flags
= m
->flags
& idx
->flags
& ~CEF_CONST_MASK
;
3336 struct expression
*copy
= __alloc_expression(0);
3338 if (e
->type
== EXPR_OFFSETOF
)
3340 if (!evaluate_expression(e
))
3342 expr
->type
= EXPR_BINOP
;
3343 expr
->flags
= e
->flags
& copy
->flags
& ~CEF_CONST_MASK
;
3345 expr
->ctype
= size_t_ctype
;
3349 return size_t_ctype
;
3352 static void check_label_declaration(struct position pos
, struct symbol
*label
)
3354 switch (label
->namespace) {
3358 sparse_error(pos
, "label '%s' was not declared", show_ident(label
->ident
));
3361 current_fn
->bogus_linear
= 1;
3367 static int type_selection(struct symbol
*ctrl
, struct symbol
*type
)
3369 struct ctype c
= { .base_type
= ctrl
};
3370 struct ctype t
= { .base_type
= type
};
3372 return !type_difference(&c
, &t
, 0, 0);
3375 static struct symbol
*evaluate_generic_selection(struct expression
*expr
)
3377 struct type_expression
*map
;
3378 struct expression
*res
;
3379 struct symbol source
;
3380 struct symbol
*ctrl
;
3382 if (!evaluate_expression(expr
->control
))
3384 if (!(ctrl
= degenerate(expr
->control
)))
3388 source
.ctype
.modifiers
&= ~(MOD_QUALIFIER
|MOD_ATOMIC
);
3389 for (map
= expr
->map
; map
; map
= map
->next
) {
3390 struct symbol
*stype
= map
->type
;
3391 struct symbol
*base
;
3393 if (!evaluate_symbol(stype
))
3396 base
= stype
->ctype
.base_type
;
3397 if (base
->type
== SYM_ARRAY
&& base
->array_size
) {
3398 get_expression_value_silent(base
->array_size
);
3399 if (base
->array_size
->type
== EXPR_VALUE
)
3401 sparse_error(stype
->pos
, "variable length array type in generic selection");
3404 if (is_func_type(stype
)) {
3405 sparse_error(stype
->pos
, "function type in generic selection");
3408 if (stype
->bit_size
<= 0 || is_void_type(stype
)) {
3409 sparse_error(stype
->pos
, "incomplete type in generic selection");
3412 if (!type_selection(&source
, stype
))
3420 sparse_error(expr
->pos
, "no generic selection for '%s'", show_typename(ctrl
));
3426 return evaluate_expression(expr
);
3429 struct symbol
*evaluate_expression(struct expression
*expr
)
3436 switch (expr
->type
) {
3439 expression_error(expr
, "value expression without a type");
3442 return evaluate_string(expr
);
3444 return evaluate_symbol_expression(expr
);
3446 evaluate_expression(expr
->left
);
3447 evaluate_expression(expr
->right
);
3448 if (!valid_subexpr_type(expr
))
3450 return evaluate_binop(expr
);
3452 return evaluate_logical(expr
);
3454 evaluate_expression(expr
->left
);
3455 if (!evaluate_expression(expr
->right
))
3457 return evaluate_comma(expr
);
3459 evaluate_expression(expr
->left
);
3460 evaluate_expression(expr
->right
);
3461 if (!valid_subexpr_type(expr
))
3463 return evaluate_compare(expr
);
3464 case EXPR_ASSIGNMENT
:
3465 evaluate_expression(expr
->left
);
3466 evaluate_expression(expr
->right
);
3467 if (!valid_subexpr_type(expr
))
3469 return evaluate_assignment(expr
);
3471 if (!evaluate_expression(expr
->unop
))
3473 return evaluate_preop(expr
);
3475 if (!evaluate_expression(expr
->unop
))
3477 return evaluate_postop(expr
);
3479 case EXPR_FORCE_CAST
:
3480 case EXPR_IMPLIED_CAST
:
3481 return evaluate_cast(expr
);
3483 return evaluate_sizeof(expr
);
3484 case EXPR_PTRSIZEOF
:
3485 return evaluate_ptrsizeof(expr
);
3487 return evaluate_alignof(expr
);
3489 return evaluate_member_dereference(expr
);
3491 return evaluate_call(expr
);
3493 case EXPR_CONDITIONAL
:
3494 return evaluate_conditional_expression(expr
);
3495 case EXPR_STATEMENT
:
3496 expr
->ctype
= evaluate_statement(expr
->statement
);
3500 expr
->ctype
= &ptr_ctype
;
3501 check_label_declaration(expr
->pos
, expr
->label_symbol
);
3505 /* Evaluate the type of the symbol .. */
3506 evaluate_symbol(expr
->symbol
);
3507 /* .. but the type of the _expression_ is a "type" */
3508 expr
->ctype
= &type_ctype
;
3512 return evaluate_offsetof(expr
);
3515 return evaluate_generic_selection(expr
);
3517 /* These can not exist as stand-alone expressions */
3518 case EXPR_INITIALIZER
:
3519 case EXPR_IDENTIFIER
:
3522 expression_error(expr
, "internal front-end error: initializer in expression");
3525 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3531 void check_duplicates(struct symbol
*sym
)
3534 struct symbol
*next
= sym
;
3535 int initialized
= sym
->initializer
!= NULL
;
3537 while ((next
= next
->same_symbol
) != NULL
) {
3538 const char *typediff
;
3539 evaluate_symbol(next
);
3540 if (initialized
&& next
->initializer
) {
3541 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3542 show_ident(sym
->ident
),
3543 stream_name(next
->pos
.stream
), next
->pos
.line
);
3544 /* Only warn once */
3548 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3550 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (%s):",
3551 show_ident(sym
->ident
), typediff
);
3552 info(sym
->pos
, " %s", show_typename(sym
));
3553 info(next
->pos
, "note: previously declared as:");
3554 info(next
->pos
, " %s", show_typename(next
));
3559 unsigned long mod
= sym
->ctype
.modifiers
;
3560 if (mod
& (MOD_STATIC
| MOD_REGISTER
| MOD_EXT_VISIBLE
))
3562 if (!(mod
& MOD_TOPLEVEL
))
3566 if (sym
->ident
== &main_ident
)
3568 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3572 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3574 struct symbol
*base_type
;
3582 sym
= examine_symbol_type(sym
);
3583 base_type
= get_base_type(sym
);
3587 /* Evaluate the initializers */
3588 if (sym
->initializer
)
3589 evaluate_initializer(sym
, &sym
->initializer
);
3591 /* And finally, evaluate the body of the symbol too */
3592 if (base_type
->type
== SYM_FN
) {
3593 struct symbol
*curr
= current_fn
;
3595 if (sym
->definition
&& sym
->definition
!= sym
)
3596 return evaluate_symbol(sym
->definition
);
3600 examine_fn_arguments(base_type
);
3601 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3603 if (base_type
->stmt
)
3604 evaluate_statement(base_type
->stmt
);
3612 void evaluate_symbol_list(struct symbol_list
*list
)
3616 FOR_EACH_PTR(list
, sym
) {
3617 has_error
&= ~ERROR_CURR_PHASE
;
3618 evaluate_symbol(sym
);
3619 check_duplicates(sym
);
3620 } END_FOR_EACH_PTR(sym
);
3623 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3625 struct expression
*expr
= stmt
->expression
;
3626 struct symbol
*fntype
, *rettype
;
3628 evaluate_expression(expr
);
3629 fntype
= current_fn
->ctype
.base_type
;
3630 rettype
= fntype
->ctype
.base_type
;
3631 if (!rettype
|| rettype
== &void_ctype
) {
3632 if (expr
&& expr
->ctype
&& !is_void_type(expr
->ctype
))
3633 expression_error(expr
, "return expression in %s function", rettype
?"void":"typeless");
3634 if (expr
&& Wreturn_void
)
3635 warning(stmt
->pos
, "returning void-valued expression");
3640 sparse_error(stmt
->pos
, "return with no return value");
3645 compatible_assignment_types(expr
, rettype
, &stmt
->expression
, "return expression");
3649 static void evaluate_if_statement(struct statement
*stmt
)
3651 if (!stmt
->if_conditional
)
3654 evaluate_conditional(stmt
->if_conditional
, 0);
3655 evaluate_statement(stmt
->if_true
);
3656 evaluate_statement(stmt
->if_false
);
3659 static void evaluate_iterator(struct statement
*stmt
)
3661 evaluate_symbol_list(stmt
->iterator_syms
);
3662 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3663 evaluate_conditional(stmt
->iterator_post_condition
,1);
3664 evaluate_statement(stmt
->iterator_pre_statement
);
3665 evaluate_statement(stmt
->iterator_statement
);
3666 evaluate_statement(stmt
->iterator_post_statement
);
3670 static void parse_asm_constraint(struct asm_operand
*op
)
3672 struct expression
*constraint
= op
->constraint
;
3673 const char *str
= constraint
->string
->data
;
3678 sparse_error(constraint
->pos
, "invalid ASM constraint (\"\")");
3681 op
->is_modify
= true;
3684 op
->is_assign
= true;
3689 while ((c
= *str
++)) {
3693 sparse_error(constraint
->pos
, "invalid ASM constraint '%c'", c
);
3697 op
->is_earlyclobber
= true;
3700 op
->is_commutative
= true;
3703 op
->is_register
= true;
3710 op
->is_memory
= true;
3715 // FIXME: ignored for now
3719 // FIXME: multiple alternative constraints
3723 // FIXME: numeric matching constraint?
3726 // FIXME: symbolic matching constraint
3730 if (arch_target
->asm_constraint
)
3731 str
= arch_target
->asm_constraint(op
, c
, str
);
3733 // FIXME: multi-letter constraints
3738 // FIXME: how to deal with multi-constraint?
3739 if (op
->is_register
)
3743 static void verify_output_constraint(struct asm_operand
*op
)
3745 struct expression
*expr
= op
->constraint
;
3746 const char *constraint
= expr
->string
->data
;
3749 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3752 static void verify_input_constraint(struct asm_operand
*op
)
3754 struct expression
*expr
= op
->constraint
;
3755 const char *constraint
= expr
->string
->data
;
3758 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3761 static void evaluate_asm_memop(struct asm_operand
*op
)
3763 if (op
->is_memory
) {
3764 struct expression
*expr
= op
->expr
;
3765 struct expression
*addr
;
3767 // implicit addressof
3768 addr
= alloc_expression(expr
->pos
, EXPR_PREOP
);
3772 evaluate_addressof(addr
);
3775 evaluate_expression(op
->expr
);
3776 degenerate(op
->expr
);
3780 static void evaluate_asm_statement(struct statement
*stmt
)
3782 struct expression
*expr
;
3783 struct asm_operand
*op
;
3786 if (!stmt
->asm_string
)
3789 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
3793 if (op
->constraint
) {
3794 parse_asm_constraint(op
);
3795 verify_output_constraint(op
);
3800 if (!evaluate_expression(expr
))
3802 if (!lvalue_expression(expr
))
3803 warning(expr
->pos
, "asm output is not an lvalue");
3804 evaluate_assign_to(expr
, expr
->ctype
);
3805 evaluate_asm_memop(op
);
3806 } END_FOR_EACH_PTR(op
);
3808 FOR_EACH_PTR(stmt
->asm_inputs
, op
) {
3812 if (op
->constraint
) {
3813 parse_asm_constraint(op
);
3814 verify_input_constraint(op
);
3818 if (!evaluate_expression(op
->expr
))
3820 evaluate_asm_memop(op
);
3821 } END_FOR_EACH_PTR(op
);
3823 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3825 sparse_error(stmt
->pos
, "bad asm clobbers");
3828 if (expr
->type
== EXPR_STRING
)
3830 expression_error(expr
, "asm clobber is not a string");
3831 } END_FOR_EACH_PTR(expr
);
3833 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3834 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3835 sparse_error(stmt
->pos
, "bad asm label");
3838 } END_FOR_EACH_PTR(sym
);
3841 static void evaluate_case_statement(struct statement
*stmt
)
3843 evaluate_expression(stmt
->case_expression
);
3844 evaluate_expression(stmt
->case_to
);
3845 evaluate_statement(stmt
->case_statement
);
3848 static void check_case_type(struct expression
*switch_expr
,
3849 struct expression
*case_expr
,
3850 struct expression
**enumcase
)
3852 struct symbol
*switch_type
, *case_type
;
3858 switch_type
= switch_expr
->ctype
;
3859 case_type
= evaluate_expression(case_expr
);
3861 if (!switch_type
|| !case_type
)
3865 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3866 else if (is_enum_type(case_type
))
3867 *enumcase
= case_expr
;
3870 sclass
= classify_type(switch_type
, &switch_type
);
3871 cclass
= classify_type(case_type
, &case_type
);
3873 /* both should be arithmetic */
3874 if (!(sclass
& cclass
& TYPE_NUM
))
3877 /* neither should be floating */
3878 if ((sclass
| cclass
) & TYPE_FLOAT
)
3881 /* if neither is restricted, we are OK */
3882 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3885 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3886 cclass
, sclass
, case_type
, switch_type
)) {
3887 unrestrict(case_expr
, cclass
, &case_type
);
3888 unrestrict(switch_expr
, sclass
, &switch_type
);
3893 expression_error(case_expr
, "incompatible types for 'case' statement");
3896 static void evaluate_switch_statement(struct statement
*stmt
)
3899 struct expression
*enumcase
= NULL
;
3900 struct expression
**enumcase_holder
= &enumcase
;
3901 struct expression
*sel
= stmt
->switch_expression
;
3903 evaluate_expression(sel
);
3904 evaluate_statement(stmt
->switch_statement
);
3907 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3908 enumcase_holder
= NULL
; /* Only check cases against switch */
3910 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3911 struct statement
*case_stmt
= sym
->stmt
;
3912 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3913 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3914 } END_FOR_EACH_PTR(sym
);
3917 static void evaluate_goto_statement(struct statement
*stmt
)
3919 struct symbol
*label
= stmt
->goto_label
;
3922 // no label associated, may be a computed goto
3923 evaluate_expression(stmt
->goto_expression
);
3927 check_label_declaration(stmt
->pos
, label
);
3930 struct symbol
*evaluate_statement(struct statement
*stmt
)
3935 switch (stmt
->type
) {
3936 case STMT_DECLARATION
: {
3938 FOR_EACH_PTR(stmt
->declaration
, s
) {
3940 } END_FOR_EACH_PTR(s
);
3945 return evaluate_return_expression(stmt
);
3947 case STMT_EXPRESSION
:
3948 if (!evaluate_expression(stmt
->expression
))
3950 if (stmt
->expression
->ctype
== &null_ctype
)
3951 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3952 return unqualify_type(degenerate(stmt
->expression
));
3954 case STMT_COMPOUND
: {
3955 struct statement
*s
;
3956 struct symbol
*type
= NULL
;
3958 /* Evaluate the return symbol in the compound statement */
3959 evaluate_symbol(stmt
->ret
);
3962 * Then, evaluate each statement, making the type of the
3963 * compound statement be the type of the last statement
3965 type
= evaluate_statement(stmt
->args
);
3966 FOR_EACH_PTR(stmt
->stmts
, s
) {
3967 type
= evaluate_statement(s
);
3968 } END_FOR_EACH_PTR(s
);
3974 evaluate_if_statement(stmt
);
3977 evaluate_iterator(stmt
);
3980 evaluate_switch_statement(stmt
);
3983 evaluate_case_statement(stmt
);
3986 return evaluate_statement(stmt
->label_statement
);
3988 evaluate_goto_statement(stmt
);
3993 evaluate_asm_statement(stmt
);
3996 evaluate_expression(stmt
->expression
);
3999 evaluate_expression(stmt
->range_expression
);
4000 evaluate_expression(stmt
->range_low
);
4001 evaluate_expression(stmt
->range_high
);