4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Evaluate constant expressions.
44 #include "expression.h"
46 struct symbol
*current_fn
;
48 struct ident bad_address_space
= { .len
= 6, .name
= "bad AS", };
50 static struct symbol
*degenerate(struct expression
*expr
);
51 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
53 static inline int valid_expr_type(struct expression
*expr
)
55 return expr
&& valid_type(expr
->ctype
);
58 static inline int valid_subexpr_type(struct expression
*expr
)
60 return valid_expr_type(expr
->left
)
61 && valid_expr_type(expr
->right
);
64 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
66 struct expression
*addr
;
67 struct symbol
*sym
= expr
->symbol
;
68 struct symbol
*base_type
;
71 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
75 examine_symbol_type(sym
);
77 base_type
= get_base_type(sym
);
79 expression_error(expr
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
83 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
85 addr
->symbol_name
= expr
->symbol_name
;
86 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
87 addr
->flags
= expr
->flags
;
88 expr
->type
= EXPR_PREOP
;
91 expr
->flags
= CEF_NONE
;
93 /* The type of a symbol is the symbol itself! */
98 static struct symbol
*evaluate_string(struct expression
*expr
)
100 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
101 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
102 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
103 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
104 unsigned int length
= expr
->string
->length
;
106 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
107 sym
->bit_size
= bytes_to_bits(length
);
108 sym
->ctype
.alignment
= 1;
110 sym
->ctype
.modifiers
= MOD_STATIC
;
111 sym
->ctype
.base_type
= array
;
112 sym
->initializer
= initstr
;
116 initstr
->ctype
= sym
;
117 initstr
->string
= expr
->string
;
119 array
->array_size
= sym
->array_size
;
120 array
->bit_size
= bytes_to_bits(length
);
121 array
->ctype
.alignment
= 1;
122 array
->ctype
.modifiers
= MOD_STATIC
;
123 array
->ctype
.base_type
= &char_ctype
;
125 array
->evaluated
= 1;
128 addr
->ctype
= &lazy_ptr_ctype
;
129 addr
->flags
= CEF_ADDR
;
131 expr
->type
= EXPR_PREOP
;
138 /* type has come from classify_type and is an integer type */
139 static inline struct symbol
*integer_promotion(struct symbol
*type
)
141 unsigned long mod
= type
->ctype
.modifiers
;
142 int width
= type
->bit_size
;
145 * Bitfields always promote to the base type,
146 * even if the bitfield might be bigger than
149 if (type
->type
== SYM_BITFIELD
) {
150 type
= type
->ctype
.base_type
;
152 mod
= type
->ctype
.modifiers
;
153 if (width
< bits_in_int
)
156 /* If char/short has as many bits as int, it still gets "promoted" */
157 if (type
->rank
< 0) {
158 if (mod
& MOD_UNSIGNED
)
166 * integer part of usual arithmetic conversions:
167 * integer promotions are applied
168 * if left and right are identical, we are done
169 * if signedness is the same, convert one with lower rank
170 * unless unsigned argument has rank lower than signed one, convert the
172 * if signed argument is bigger than unsigned one, convert the unsigned.
173 * otherwise, convert signed.
175 * Leaving aside the integer promotions, that is equivalent to
176 * if identical, don't convert
177 * if left is bigger than right, convert right
178 * if right is bigger than left, convert right
179 * otherwise, if signedness is the same, convert one with lower rank
180 * otherwise convert the signed one.
182 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
184 unsigned long lmod
, rmod
;
186 left
= integer_promotion(left
);
187 right
= integer_promotion(right
);
192 if (left
->bit_size
> right
->bit_size
)
195 if (right
->bit_size
> left
->bit_size
)
198 lmod
= left
->ctype
.modifiers
;
199 rmod
= right
->ctype
.modifiers
;
200 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
201 if (lmod
& MOD_UNSIGNED
)
203 } else if (left
->rank
> right
->rank
)
211 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
213 return orig
->bit_size
== new->bit_size
&&
214 orig
->bit_offset
== new->bit_offset
;
217 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, struct ident
**asp
)
219 unsigned long mod
= 0;
220 struct ident
*as
= NULL
;
223 mod
|= node
->ctype
.modifiers
;
224 combine_address_space(node
->pos
, &as
, node
->ctype
.as
);
225 if (node
->type
== SYM_NODE
) {
226 node
= node
->ctype
.base_type
;
231 *modp
= mod
& ~MOD_IGNORE
;
236 static int is_same_type(struct expression
*expr
, struct symbol
*new)
238 struct symbol
*old
= expr
->ctype
;
239 unsigned long oldmod
, newmod
;
240 struct ident
*oldas
, *newas
;
242 old
= base_type(old
, &oldmod
, &oldas
);
243 new = base_type(new, &newmod
, &newas
);
245 /* Same base type, same address space? */
246 if (old
== new && oldas
== newas
) {
247 unsigned long difmod
;
249 /* Check the modifier bits. */
250 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
252 /* Exact same type? */
257 * Not the same type, but differs only in "const".
258 * Don't warn about MOD_NOCAST.
260 if (difmod
== MOD_CONST
)
263 if ((oldmod
| newmod
) & MOD_NOCAST
) {
264 const char *tofrom
= "to/from";
265 if (!(newmod
& MOD_NOCAST
))
267 if (!(oldmod
& MOD_NOCAST
))
269 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
275 warn_for_different_enum_types (struct position pos
,
276 struct symbol
*typea
,
277 struct symbol
*typeb
)
281 if (typea
->type
== SYM_NODE
)
282 typea
= typea
->ctype
.base_type
;
283 if (typeb
->type
== SYM_NODE
)
284 typeb
= typeb
->ctype
.base_type
;
289 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
290 warning(pos
, "mixing different enum types:");
291 info(pos
, " %s", show_typename(typea
));
292 info(pos
, " %s", show_typename(typeb
));
296 static int cast_flags(struct expression
*expr
, struct expression
*target
);
297 static struct symbol
*cast_to_bool(struct expression
*expr
);
300 * This gets called for implicit casts in assignments and
301 * integer promotion. We often want to try to move the
302 * cast down, because the ops involved may have been
303 * implicitly cast up, and we can get rid of the casts
306 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
308 struct expression
*expr
;
310 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
312 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
316 * See if we can simplify the op. Move the cast down.
320 if (old
->ctype
->bit_size
< type
->bit_size
)
322 if (old
->op
== '~') {
324 old
->unop
= cast_to(old
->unop
, type
);
329 case EXPR_IMPLIED_CAST
:
330 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
332 if (old
->ctype
->bit_size
>= type
->bit_size
) {
333 struct expression
*orig
= old
->cast_expression
;
334 if (same_cast_type(orig
->ctype
, type
))
336 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
338 old
->cast_type
= type
;
348 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
350 expr
->cast_type
= type
;
351 expr
->cast_expression
= old
;
352 expr
->flags
= cast_flags(expr
, old
);
354 if (is_bool_type(type
))
371 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
373 static int type_class
[SYM_BAD
+ 1] = {
374 [SYM_PTR
] = TYPE_PTR
,
375 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
376 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
377 [SYM_STRUCT
] = TYPE_COMPOUND
,
378 [SYM_UNION
] = TYPE_COMPOUND
,
379 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
380 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
381 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
383 if (type
->type
== SYM_NODE
)
384 type
= type
->ctype
.base_type
;
385 if (type
->type
== SYM_TYPEOF
) {
386 type
= examine_symbol_type(type
);
387 if (type
->type
== SYM_NODE
)
388 type
= type
->ctype
.base_type
;
390 if (type
->type
== SYM_ENUM
)
391 type
= type
->ctype
.base_type
;
393 if (type
->type
== SYM_BASETYPE
) {
394 if (type
->ctype
.base_type
== &int_type
)
396 if (type
->ctype
.base_type
== &fp_type
)
397 return TYPE_NUM
| TYPE_FLOAT
;
399 return type_class
[type
->type
];
402 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
404 static inline int is_string_type(struct symbol
*type
)
406 if (type
->type
== SYM_NODE
)
407 type
= type
->ctype
.base_type
;
408 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
411 static struct symbol
*bad_expr_type(struct expression
*expr
)
413 switch (expr
->type
) {
416 if (!valid_subexpr_type(expr
))
418 sparse_error(expr
->pos
, "incompatible types for operation (%s):", show_special(expr
->op
));
419 info(expr
->pos
, " %s", show_typename(expr
->left
->ctype
));
420 info(expr
->pos
, " %s", show_typename(expr
->right
->ctype
));
424 if (!valid_expr_type(expr
->unop
))
426 sparse_error(expr
->pos
, "incompatible type for operation (%s):", show_special(expr
->op
));
427 info(expr
->pos
, " %s", show_typename(expr
->unop
->ctype
));
433 expr
->flags
= CEF_NONE
;
434 return expr
->ctype
= &bad_ctype
;
437 static int restricted_value(struct expression
*v
, struct symbol
*type
)
439 if (v
->type
!= EXPR_VALUE
)
446 static int restricted_binop(int op
, struct symbol
*type
)
451 case SPECIAL_AND_ASSIGN
:
452 case SPECIAL_OR_ASSIGN
:
453 case SPECIAL_XOR_ASSIGN
:
454 return 1; /* unfoul */
458 return 2; /* keep fouled */
460 case SPECIAL_NOTEQUAL
:
461 return 3; /* warn if fouled */
467 static int restricted_unop(int op
, struct symbol
**type
)
470 if ((*type
)->bit_size
< bits_in_int
)
471 *type
= befoul(*type
);
478 /* type should be SYM_FOULED */
479 static inline struct symbol
*unfoul(struct symbol
*type
)
481 return type
->ctype
.base_type
;
484 static struct symbol
*restricted_binop_type(int op
,
485 struct expression
*left
,
486 struct expression
*right
,
487 int lclass
, int rclass
,
488 struct symbol
*ltype
,
489 struct symbol
*rtype
)
491 struct symbol
*ctype
= NULL
;
492 if (lclass
& TYPE_RESTRICT
) {
493 if (rclass
& TYPE_RESTRICT
) {
494 if (ltype
== rtype
) {
496 } else if (lclass
& TYPE_FOULED
) {
497 if (unfoul(ltype
) == rtype
)
499 } else if (rclass
& TYPE_FOULED
) {
500 if (unfoul(rtype
) == ltype
)
504 if (!restricted_value(right
, ltype
))
507 } else if (!restricted_value(left
, rtype
))
511 switch (restricted_binop(op
, ctype
)) {
513 if ((lclass
^ rclass
) & TYPE_FOULED
)
514 ctype
= unfoul(ctype
);
517 if (!(lclass
& rclass
& TYPE_FOULED
))
529 static inline void unrestrict(struct expression
*expr
,
530 int class, struct symbol
**ctype
)
532 if (class & TYPE_RESTRICT
) {
533 if (class & TYPE_FOULED
)
534 *ctype
= unfoul(*ctype
);
535 warning(expr
->pos
, "%s degrades to integer",
536 show_typename(*ctype
));
537 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
541 static struct symbol
*usual_conversions(int op
,
542 struct expression
*left
,
543 struct expression
*right
,
544 int lclass
, int rclass
,
545 struct symbol
*ltype
,
546 struct symbol
*rtype
)
548 struct symbol
*ctype
;
550 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
552 if ((lclass
| rclass
) & TYPE_RESTRICT
)
556 if (!(lclass
& TYPE_FLOAT
)) {
557 if (!(rclass
& TYPE_FLOAT
))
558 return bigger_int_type(ltype
, rtype
);
561 } else if (rclass
& TYPE_FLOAT
) {
562 if (rtype
->rank
> ltype
->rank
)
570 ctype
= restricted_binop_type(op
, left
, right
,
571 lclass
, rclass
, ltype
, rtype
);
575 unrestrict(left
, lclass
, <ype
);
576 unrestrict(right
, rclass
, &rtype
);
581 static inline int lvalue_expression(struct expression
*expr
)
583 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
586 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
588 struct expression
*index
= expr
->right
;
589 struct symbol
*ctype
, *base
;
592 classify_type(degenerate(expr
->left
), &ctype
);
593 base
= examine_pointer_target(ctype
);
596 * An address constant +/- an integer constant expression
597 * yields an address constant again [6.6(7)].
599 if ((expr
->left
->flags
& CEF_ADDR
) && (expr
->right
->flags
& CEF_ICE
))
600 expr
->flags
= CEF_ADDR
;
603 expression_error(expr
, "missing type information");
606 if (is_function(base
)) {
607 expression_error(expr
, "arithmetics on pointers to functions");
611 /* Get the size of whatever the pointer points to */
612 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
614 if (ctype
== &null_ctype
)
618 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
621 if (index
->type
== EXPR_VALUE
) {
622 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
623 unsigned long long v
= index
->value
, mask
;
624 mask
= 1ULL << (itype
->bit_size
- 1);
630 mask
= 1ULL << (bits_in_pointer
- 1);
631 v
&= mask
| (mask
- 1);
633 val
->ctype
= ssize_t_ctype
;
638 if (itype
->bit_size
< bits_in_pointer
)
639 index
= cast_to(index
, ssize_t_ctype
);
642 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
643 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
645 val
->ctype
= ssize_t_ctype
;
646 val
->value
= multiply
;
649 mul
->ctype
= ssize_t_ctype
;
659 static void examine_fn_arguments(struct symbol
*fn
);
661 #define MOD_IGN (MOD_QUALIFIER | MOD_FUN_ATTR)
663 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
664 unsigned long mod1
, unsigned long mod2
)
666 struct ident
*as1
= c1
->as
, *as2
= c2
->as
;
667 struct symbol
*t1
= c1
->base_type
;
668 struct symbol
*t2
= c2
->base_type
;
669 int move1
= 1, move2
= 1;
670 mod1
|= c1
->modifiers
;
671 mod2
|= c2
->modifiers
;
675 struct symbol
*base1
= t1
->ctype
.base_type
;
676 struct symbol
*base2
= t2
->ctype
.base_type
;
679 * FIXME! Collect alignment and context too here!
682 if (t1
&& t1
->type
!= SYM_PTR
) {
683 mod1
|= t1
->ctype
.modifiers
;
684 combine_address_space(t1
->pos
, &as1
, t1
->ctype
.as
);
690 if (t2
&& t2
->type
!= SYM_PTR
) {
691 mod2
|= t2
->ctype
.modifiers
;
692 combine_address_space(t2
->pos
, &as2
, t2
->ctype
.as
);
700 return "different types";
702 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
710 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
720 if (type
!= t2
->type
)
721 return "different base types";
725 sparse_error(t1
->pos
,
726 "internal error: bad type in derived(%d)",
730 return "different base types";
733 /* allow definition of incomplete structs and unions */
734 if (t1
->ident
== t2
->ident
)
736 return "different base types";
738 /* XXX: we ought to compare sizes */
742 return "different address spaces";
743 /* MOD_SPECIFIER is due to idiocy in parse.c */
744 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
745 return "different modifiers";
746 /* we could be lazier here */
747 base1
= examine_pointer_target(t1
);
748 base2
= examine_pointer_target(t2
);
749 mod1
= t1
->ctype
.modifiers
;
751 mod2
= t2
->ctype
.modifiers
;
755 struct symbol
*arg1
, *arg2
;
759 return "different address spaces";
760 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
761 return "different modifiers";
762 mod1
= t1
->ctype
.modifiers
;
764 mod2
= t2
->ctype
.modifiers
;
767 if (t1
->variadic
!= t2
->variadic
)
768 return "incompatible variadic arguments";
769 examine_fn_arguments(t1
);
770 examine_fn_arguments(t2
);
771 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
772 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
779 return "different argument counts";
780 diffstr
= type_difference(&arg1
->ctype
,
784 static char argdiff
[80];
785 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
792 FINISH_PTR_LIST(arg2
);
793 FINISH_PTR_LIST(arg1
);
798 return "different address spaces";
800 return "different base types";
801 if (t1
->rank
!= t2
->rank
)
802 return "different type sizes";
803 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
806 else if (diff
& ~MOD_SIGNEDNESS
)
807 return "different modifiers";
809 return "different signedness";
815 return "different address spaces";
816 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
817 return "different modifiers";
821 static void bad_null(struct expression
*expr
)
823 if (Wnon_pointer_null
)
824 warning(expr
->pos
, "Using plain integer as NULL pointer");
827 static unsigned long target_qualifiers(struct symbol
*type
)
829 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
830 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
835 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
837 const char *typediff
;
838 struct symbol
*ltype
, *rtype
;
839 struct expression
*l
= expr
->left
;
840 struct expression
*r
= expr
->right
;
841 struct symbol
*lbase
;
843 classify_type(degenerate(l
), <ype
);
844 classify_type(degenerate(r
), &rtype
);
846 lbase
= examine_pointer_target(ltype
);
847 examine_pointer_target(rtype
);
848 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
849 target_qualifiers(rtype
),
850 target_qualifiers(ltype
));
852 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
854 if (is_function(lbase
)) {
855 expression_error(expr
, "subtraction of functions? Share your drugs");
859 expr
->ctype
= ssize_t_ctype
;
860 if (lbase
->bit_size
> bits_in_char
) {
861 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
862 struct expression
*div
= expr
;
863 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
864 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
866 val
->ctype
= size_t_ctype
;
869 if (value
& (value
-1)) {
870 if (Wptr_subtraction_blows
) {
871 warning(expr
->pos
, "potentially expensive pointer subtraction");
872 info(expr
->pos
, " '%s' has a non-power-of-2 size: %lu", show_typename(lbase
), value
);
877 sub
->ctype
= ssize_t_ctype
;
886 return ssize_t_ctype
;
889 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
891 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
893 struct symbol
*ctype
;
898 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
899 warning(expr
->pos
, "assignment expression in conditional");
901 ctype
= evaluate_expression(expr
);
902 if (!valid_type(ctype
))
904 if (is_safe_type(ctype
))
905 warning(expr
->pos
, "testing a 'safe expression'");
906 if (is_func_type(ctype
)) {
908 warning(expr
->pos
, "the address of %s will always evaluate as true", "a function");
909 } else if (is_array_type(ctype
)) {
911 warning(expr
->pos
, "the address of %s will always evaluate as true", "an array");
912 } else if (!is_scalar_type(ctype
)) {
913 sparse_error(expr
->pos
, "non-scalar type in conditional:");
914 info(expr
->pos
, " %s", show_typename(ctype
));
918 ctype
= degenerate(expr
);
922 static struct symbol
*evaluate_logical(struct expression
*expr
)
924 if (!evaluate_conditional(expr
->left
, 0))
926 if (!evaluate_conditional(expr
->right
, 0))
929 /* the result is int [6.5.13(3), 6.5.14(3)] */
930 expr
->ctype
= &int_ctype
;
931 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
932 expr
->flags
&= ~(CEF_CONST_MASK
| CEF_ADDR
);
936 static struct symbol
*evaluate_binop(struct expression
*expr
)
938 struct symbol
*ltype
, *rtype
, *ctype
;
939 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
940 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
943 /* number op number */
944 if (lclass
& rclass
& TYPE_NUM
) {
945 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
946 expr
->flags
&= ~CEF_CONST_MASK
;
948 if ((lclass
| rclass
) & TYPE_FLOAT
) {
950 case '+': case '-': case '*': case '/':
953 return bad_expr_type(expr
);
957 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
958 // shifts do integer promotions, but that's it.
959 unrestrict(expr
->left
, lclass
, <ype
);
960 unrestrict(expr
->right
, rclass
, &rtype
);
961 ctype
= ltype
= integer_promotion(ltype
);
962 rtype
= integer_promotion(rtype
);
964 // The rest do usual conversions
965 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
966 && expr
->left
->op
== '!';
967 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
968 && expr
->right
->op
== '!';
969 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
970 warning(expr
->pos
, "dubious: %sx %c %sy",
973 right_not
? "!" : "");
975 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
976 lclass
, rclass
, ltype
, rtype
);
977 ctype
= rtype
= ltype
;
980 expr
->left
= cast_to(expr
->left
, ltype
);
981 expr
->right
= cast_to(expr
->right
, rtype
);
986 /* pointer (+|-) integer */
987 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
988 unrestrict(expr
->right
, rclass
, &rtype
);
989 return evaluate_ptr_add(expr
, rtype
);
992 /* integer + pointer */
993 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
994 struct expression
*index
= expr
->left
;
995 unrestrict(index
, lclass
, <ype
);
996 expr
->left
= expr
->right
;
998 return evaluate_ptr_add(expr
, ltype
);
1001 /* pointer - pointer */
1002 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
1003 return evaluate_ptr_sub(expr
);
1005 return bad_expr_type(expr
);
1008 static struct symbol
*evaluate_comma(struct expression
*expr
)
1010 expr
->ctype
= degenerate(expr
->right
);
1011 if (expr
->ctype
== &null_ctype
)
1012 expr
->ctype
= &ptr_ctype
;
1013 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
1017 static int modify_for_unsigned(int op
)
1020 op
= SPECIAL_UNSIGNED_LT
;
1022 op
= SPECIAL_UNSIGNED_GT
;
1023 else if (op
== SPECIAL_LTE
)
1024 op
= SPECIAL_UNSIGNED_LTE
;
1025 else if (op
== SPECIAL_GTE
)
1026 op
= SPECIAL_UNSIGNED_GTE
;
1030 enum null_constant_type
{
1036 static inline int is_null_pointer_constant(struct expression
*e
)
1038 if (e
->ctype
== &null_ctype
)
1040 if (!(e
->flags
& CEF_ICE
))
1042 return is_zero_constant(e
) ? NULL_ZERO
: NON_NULL
;
1045 static struct symbol
*evaluate_compare(struct expression
*expr
)
1047 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1048 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
1049 int lclass
= classify_type(degenerate(left
), <ype
);
1050 int rclass
= classify_type(degenerate(right
), &rtype
);
1051 struct symbol
*ctype
;
1052 const char *typediff
;
1055 if (is_type_type(ltype
) && is_type_type(rtype
)) {
1057 * __builtin_types_compatible_p() yields an integer
1058 * constant expression
1060 expr
->flags
= CEF_SET_ICE
;
1064 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1065 warning(expr
->pos
, "testing a 'safe expression'");
1067 expr
->flags
= left
->flags
& right
->flags
& ~CEF_CONST_MASK
& ~CEF_ADDR
;
1069 /* number on number */
1070 if (lclass
& rclass
& TYPE_NUM
) {
1071 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1072 lclass
, rclass
, ltype
, rtype
);
1073 expr
->left
= cast_to(expr
->left
, ctype
);
1074 expr
->right
= cast_to(expr
->right
, ctype
);
1075 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1076 expr
->op
= modify_for_unsigned(expr
->op
);
1080 /* at least one must be a pointer */
1081 if (!((lclass
| rclass
) & TYPE_PTR
))
1082 return bad_expr_type(expr
);
1084 /* equality comparisons can be with null pointer constants */
1085 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1086 int is_null1
= is_null_pointer_constant(left
);
1087 int is_null2
= is_null_pointer_constant(right
);
1088 if (is_null1
== NULL_ZERO
)
1090 if (is_null2
== NULL_ZERO
)
1092 if (is_null1
&& is_null2
) {
1093 int positive
= expr
->op
== SPECIAL_EQUAL
;
1094 expr
->type
= EXPR_VALUE
;
1095 expr
->value
= positive
;
1098 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1099 expr
->left
= cast_to(left
, rtype
);
1102 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1103 expr
->right
= cast_to(right
, ltype
);
1107 /* both should be pointers */
1108 if (!(lclass
& rclass
& TYPE_PTR
))
1109 return bad_expr_type(expr
);
1110 expr
->op
= modify_for_unsigned(expr
->op
);
1112 lbase
= examine_pointer_target(ltype
);
1113 rbase
= examine_pointer_target(rtype
);
1115 /* they also have special treatment for pointers to void */
1116 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1117 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1118 if (lbase
== &void_ctype
) {
1119 expr
->right
= cast_to(right
, ltype
);
1122 if (rbase
== &void_ctype
) {
1123 expr
->left
= cast_to(left
, rtype
);
1129 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1130 target_qualifiers(rtype
),
1131 target_qualifiers(ltype
));
1135 expression_error(expr
, "incompatible types in comparison expression (%s):", typediff
);
1136 info(expr
->pos
, " %s", show_typename(ltype
));
1137 info(expr
->pos
, " %s", show_typename(rtype
));
1141 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1142 expr
->ctype
= &int_ctype
;
1147 * NOTE! The degenerate case of "x ? : y", where we don't
1148 * have a true case, this will possibly promote "x" to the
1149 * same type as "y", and thus _change_ the conditional
1150 * test in the expression. But since promotion is "safe"
1151 * for testing, that's OK.
1153 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1155 struct expression
**cond
;
1156 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1158 const char * typediff
;
1161 if (!evaluate_conditional(expr
->conditional
, 0))
1163 if (!evaluate_expression(expr
->cond_false
))
1166 ctype
= degenerate(expr
->conditional
);
1167 rtype
= degenerate(expr
->cond_false
);
1169 cond
= &expr
->conditional
;
1171 if (expr
->cond_true
) {
1172 if (!evaluate_expression(expr
->cond_true
))
1174 ltype
= degenerate(expr
->cond_true
);
1175 cond
= &expr
->cond_true
;
1178 expr
->flags
= (expr
->conditional
->flags
& (*cond
)->flags
&
1179 expr
->cond_false
->flags
& ~CEF_CONST_MASK
);
1181 * In the standard, it is defined that an integer constant expression
1182 * shall only have operands that are themselves constant [6.6(6)].
1183 * While this definition is very clear for expressions that need all
1184 * their operands to be evaluated, for conditional expressions with a
1185 * constant condition things are much less obvious.
1186 * So, as an extension, do the same as GCC seems to do:
1187 * Consider a conditional expression with a constant condition
1188 * as having the same constantness as the argument corresponding
1189 * to the truth value (including in the case of address constants
1190 * which are defined more stricly [6.6(9)]).
1192 if (expr
->conditional
->flags
& (CEF_ACE
| CEF_ADDR
)) {
1193 int is_true
= expr_truth_value(expr
->conditional
);
1194 struct expression
*arg
= is_true
? *cond
: expr
->cond_false
;
1195 expr
->flags
= arg
->flags
& ~CEF_CONST_MASK
;
1198 lclass
= classify_type(ltype
, <ype
);
1199 rclass
= classify_type(rtype
, &rtype
);
1200 if (lclass
& rclass
& TYPE_NUM
) {
1201 ctype
= usual_conversions('?', *cond
, expr
->cond_false
,
1202 lclass
, rclass
, ltype
, rtype
);
1203 *cond
= cast_to(*cond
, ctype
);
1204 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1208 if ((lclass
| rclass
) & TYPE_PTR
) {
1209 int is_null1
= is_null_pointer_constant(*cond
);
1210 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1212 if (is_null1
&& is_null2
) {
1213 *cond
= cast_to(*cond
, &ptr_ctype
);
1214 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1218 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1219 if (is_null1
== NULL_ZERO
)
1221 *cond
= cast_to(*cond
, rtype
);
1225 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1226 if (is_null2
== NULL_ZERO
)
1227 bad_null(expr
->cond_false
);
1228 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1232 if (!(lclass
& rclass
& TYPE_PTR
)) {
1233 typediff
= "different types";
1236 /* OK, it's pointer on pointer */
1237 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1238 typediff
= "different address spaces";
1242 /* need to be lazier here */
1243 lbase
= examine_pointer_target(ltype
);
1244 rbase
= examine_pointer_target(rtype
);
1245 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1247 if (lbase
== &void_ctype
) {
1248 /* XXX: pointers to function should warn here */
1253 if (rbase
== &void_ctype
) {
1254 /* XXX: pointers to function should warn here */
1258 /* XXX: that should be pointer to composite */
1260 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1267 /* void on void, struct on same struct, union on same union */
1268 if (ltype
== rtype
) {
1272 typediff
= "different base types";
1275 expression_error(expr
, "incompatible types in conditional expression (%s):", typediff
);
1276 info(expr
->pos
, " %s", show_typename(ltype
));
1277 info(expr
->pos
, " %s", show_typename(rtype
));
1279 * if the condition is constant, the type is in fact known
1280 * so use it, as gcc & clang do.
1282 switch (expr_truth_value(expr
->conditional
)) {
1283 case 1: expr
->ctype
= ltype
;
1285 case 0: expr
->ctype
= rtype
;
1293 expr
->ctype
= ctype
;
1297 if (qual
& ~ctype
->ctype
.modifiers
) {
1298 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1300 sym
->ctype
.modifiers
|= qual
;
1303 *cond
= cast_to(*cond
, ctype
);
1304 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1308 /* FP assignments can not do modulo or bit operations */
1309 static int compatible_float_op(int op
)
1311 return op
== SPECIAL_ADD_ASSIGN
||
1312 op
== SPECIAL_SUB_ASSIGN
||
1313 op
== SPECIAL_MUL_ASSIGN
||
1314 op
== SPECIAL_DIV_ASSIGN
;
1317 static int evaluate_assign_op(struct expression
*expr
)
1319 struct symbol
*target
= expr
->left
->ctype
;
1320 struct symbol
*source
= expr
->right
->ctype
;
1321 struct symbol
*t
, *s
;
1322 int tclass
= classify_type(target
, &t
);
1323 int sclass
= classify_type(source
, &s
);
1326 if (tclass
& sclass
& TYPE_NUM
) {
1327 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1328 expression_error(expr
, "invalid assignment");
1331 if (tclass
& TYPE_RESTRICT
) {
1332 if (!restricted_binop(op
, t
)) {
1333 warning(expr
->pos
, "bad assignment (%s) to %s",
1334 show_special(op
), show_typename(t
));
1335 expr
->right
= cast_to(expr
->right
, target
);
1338 /* allowed assignments unfoul */
1339 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1341 if (!restricted_value(expr
->right
, t
))
1343 } else if (op
== SPECIAL_SHR_ASSIGN
|| op
== SPECIAL_SHL_ASSIGN
) {
1344 // shifts do integer promotions, but that's it.
1345 unrestrict(expr
->right
, sclass
, &s
);
1346 target
= integer_promotion(s
);
1348 } else if (!(sclass
& TYPE_RESTRICT
))
1350 /* source and target would better be identical restricted */
1353 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1354 info(expr
->pos
, " left side has type %s", show_typename(t
));
1355 info(expr
->pos
, " right side has type %s", show_typename(s
));
1356 expr
->right
= cast_to(expr
->right
, target
);
1359 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1360 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1361 unrestrict(expr
->right
, sclass
, &s
);
1362 evaluate_ptr_add(expr
, s
);
1365 expression_error(expr
, "invalid pointer assignment");
1369 expression_error(expr
, "invalid assignment");
1373 target
= usual_conversions(op
, expr
->left
, expr
->right
,
1374 tclass
, sclass
, target
, source
);
1376 expr
->right
= cast_to(expr
->right
, target
);
1380 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1383 return 0; /* yes, 0 - we don't want a cast_to here */
1384 if (t1
== &void_ctype
)
1386 if (t2
== &void_ctype
)
1388 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1390 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1394 if (t1
->rank
== -2 && t2
->rank
== -2)
1396 if (t1
->rank
!= t2
->rank
)
1401 static int check_assignment_types(struct symbol
*target
, struct expression
**rp
,
1402 const char **typediff
)
1404 struct symbol
*source
= degenerate(*rp
);
1405 struct symbol
*t
, *s
;
1406 int tclass
= classify_type(target
, &t
);
1407 int sclass
= classify_type(source
, &s
);
1409 if (tclass
& sclass
& TYPE_NUM
) {
1410 if (tclass
& TYPE_RESTRICT
) {
1411 /* allowed assignments unfoul */
1412 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1414 if (!restricted_value(*rp
, target
))
1418 } else if (!(sclass
& TYPE_RESTRICT
))
1420 if (t
== &bool_ctype
) {
1421 if (is_fouled_type(s
))
1422 warning((*rp
)->pos
, "%s degrades to integer",
1423 show_typename(s
->ctype
.base_type
));
1426 *typediff
= "different base types";
1430 if (tclass
== TYPE_PTR
) {
1431 unsigned long mod1
, mod2
;
1432 unsigned long modl
, modr
;
1433 struct symbol
*b1
, *b2
;
1434 // NULL pointer is always OK
1435 int is_null
= is_null_pointer_constant(*rp
);
1437 if (is_null
== NULL_ZERO
)
1441 if (!(sclass
& TYPE_PTR
)) {
1442 *typediff
= "different base types";
1445 b1
= examine_pointer_target(t
);
1446 b2
= examine_pointer_target(s
);
1447 mod1
= target_qualifiers(t
);
1448 mod2
= target_qualifiers(s
);
1449 if (whitelist_pointers(b1
, b2
)) {
1451 * assignments to/from void * are OK, provided that
1452 * we do not remove qualifiers from pointed to [C]
1453 * or mix address spaces [sparse].
1455 if (t
->ctype
.as
!= s
->ctype
.as
) {
1456 *typediff
= "different address spaces";
1460 * If this is a function pointer assignment, it is
1461 * actually fine to assign a pointer to const data to
1462 * it, as a function pointer points to const data
1463 * implicitly, i.e., dereferencing it does not produce
1466 if (b1
->type
== SYM_FN
)
1468 if (mod2
& ~mod1
& ~MOD_FUN_ATTR
) {
1469 *typediff
= "different modifiers";
1474 /* It's OK if the target is more volatile or const than the source */
1475 /* It's OK if the source is more pure/noreturn than the target */
1476 modr
= mod1
& ~MOD_REV_QUAL
;
1477 modl
= mod2
& MOD_REV_QUAL
;
1478 *typediff
= type_difference(&t
->ctype
, &s
->ctype
, modl
, modr
);
1484 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1487 if (tclass
& TYPE_NUM
) {
1488 /* XXX: need to turn into comparison with NULL */
1489 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1491 *typediff
= "different base types";
1494 *typediff
= "invalid types";
1498 *rp
= cast_to(*rp
, target
);
1502 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1503 struct expression
**rp
, const char *where
)
1505 const char *typediff
;
1507 if (!check_assignment_types(target
, rp
, &typediff
)) {
1508 struct symbol
*source
= *rp
? (*rp
)->ctype
: NULL
;
1509 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1510 info(expr
->pos
, " expected %s", show_typename(target
));
1511 info(expr
->pos
, " got %s", show_typename(source
));
1512 *rp
= cast_to(*rp
, target
);
1519 static int compatible_transparent_union(struct symbol
*target
,
1520 struct expression
**rp
)
1522 struct symbol
*t
, *member
;
1523 classify_type(target
, &t
);
1524 if (t
->type
!= SYM_UNION
|| !t
->transparent_union
)
1527 FOR_EACH_PTR(t
->symbol_list
, member
) {
1528 const char *typediff
;
1529 if (check_assignment_types(member
, rp
, &typediff
))
1531 } END_FOR_EACH_PTR(member
);
1536 static int compatible_argument_type(struct expression
*expr
, struct symbol
*target
,
1537 struct expression
**rp
, const char *where
)
1539 if (compatible_transparent_union(target
, rp
))
1542 return compatible_assignment_types(expr
, target
, rp
, where
);
1545 static void mark_addressable(struct expression
*expr
)
1547 while (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
1549 if (expr
->type
== EXPR_SYMBOL
) {
1550 struct symbol
*sym
= expr
->symbol
;
1551 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1555 static void mark_assigned(struct expression
*expr
)
1561 switch (expr
->type
) {
1566 if (sym
->type
!= SYM_NODE
)
1568 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1572 mark_assigned(expr
->left
);
1573 mark_assigned(expr
->right
);
1576 case EXPR_FORCE_CAST
:
1577 mark_assigned(expr
->cast_expression
);
1580 mark_assigned(expr
->base
);
1588 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1590 if (type
->ctype
.modifiers
& MOD_CONST
)
1591 expression_error(left
, "assignment to const expression");
1593 /* We know left is an lvalue, so it's a "preop-*" */
1594 mark_assigned(left
->unop
);
1597 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1599 struct expression
*left
= expr
->left
;
1600 struct symbol
*ltype
;
1602 if (!lvalue_expression(left
)) {
1603 expression_error(expr
, "not an lvalue");
1607 ltype
= left
->ctype
;
1609 if (expr
->op
!= '=') {
1610 if (!evaluate_assign_op(expr
))
1613 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, "assignment"))
1617 evaluate_assign_to(left
, ltype
);
1619 expr
->ctype
= ltype
;
1623 static void examine_fn_arguments(struct symbol
*fn
)
1627 FOR_EACH_PTR(fn
->arguments
, s
) {
1628 struct symbol
*arg
= evaluate_symbol(s
);
1629 /* Array/function arguments silently degenerate into pointers */
1635 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1636 if (arg
->type
== SYM_ARRAY
)
1637 ptr
->ctype
= arg
->ctype
;
1639 ptr
->ctype
.base_type
= arg
;
1640 combine_address_space(s
->pos
, &ptr
->ctype
.as
, s
->ctype
.as
);
1641 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1643 s
->ctype
.base_type
= ptr
;
1645 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1648 examine_symbol_type(s
);
1655 } END_FOR_EACH_PTR(s
);
1658 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, struct ident
*as
, int mod
)
1660 /* Take the modifiers of the pointer, and apply them to the member */
1661 mod
|= sym
->ctype
.modifiers
;
1662 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1663 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1665 newsym
->ctype
.as
= as
;
1666 newsym
->ctype
.modifiers
= mod
;
1672 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1674 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1675 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1677 node
->ctype
.base_type
= ptr
;
1678 ptr
->bit_size
= bits_in_pointer
;
1679 ptr
->ctype
.alignment
= pointer_alignment
;
1681 node
->bit_size
= bits_in_pointer
;
1682 node
->ctype
.alignment
= pointer_alignment
;
1685 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1686 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1687 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1689 if (sym
->type
== SYM_NODE
) {
1690 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1691 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1692 sym
= sym
->ctype
.base_type
;
1694 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1695 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1696 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1697 sym
= sym
->ctype
.base_type
;
1699 ptr
->ctype
.base_type
= sym
;
1704 /* Arrays degenerate into pointers on pointer arithmetic */
1705 static struct symbol
*degenerate(struct expression
*expr
)
1707 struct symbol
*ctype
, *base
;
1711 ctype
= expr
->ctype
;
1714 base
= examine_symbol_type(ctype
);
1715 if (ctype
->type
== SYM_NODE
)
1716 base
= ctype
->ctype
.base_type
;
1718 * Arrays degenerate into pointers to the entries, while
1719 * functions degenerate into pointers to themselves.
1720 * If array was part of non-lvalue compound, we create a copy
1721 * of that compound first and then act as if we were dealing with
1722 * the corresponding field in there.
1724 switch (base
->type
) {
1726 if (expr
->type
== EXPR_SLICE
) {
1727 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1728 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1730 a
->ctype
.base_type
= expr
->base
->ctype
;
1731 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1732 a
->array_size
= expr
->base
->ctype
->array_size
;
1734 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1736 e0
->ctype
= &lazy_ptr_ctype
;
1738 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1741 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1743 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1745 e2
->right
= expr
->base
;
1747 e2
->ctype
= expr
->base
->ctype
;
1749 if (expr
->r_bitpos
) {
1750 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1753 e3
->right
= alloc_const_expression(expr
->pos
,
1754 bits_to_bytes(expr
->r_bitpos
));
1755 e3
->ctype
= &lazy_ptr_ctype
;
1760 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1763 e4
->ctype
= &lazy_ptr_ctype
;
1766 expr
->type
= EXPR_PREOP
;
1770 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1771 expression_error(expr
, "strange non-value function or array");
1774 *expr
= *expr
->unop
;
1775 ctype
= create_pointer(expr
, ctype
, 1);
1776 expr
->ctype
= ctype
;
1777 mark_addressable(expr
);
1784 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1786 struct expression
*op
= expr
->unop
;
1787 struct symbol
*ctype
;
1789 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1790 expression_error(expr
, "not addressable");
1796 mark_addressable(expr
);
1799 * symbol expression evaluation is lazy about the type
1800 * of the sub-expression, so we may have to generate
1801 * the type here if so..
1803 if (expr
->ctype
== &lazy_ptr_ctype
) {
1804 ctype
= create_pointer(expr
, ctype
, 0);
1805 expr
->ctype
= ctype
;
1811 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1813 struct expression
*op
= expr
->unop
;
1814 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1816 /* Simplify: *&(expr) => (expr) */
1817 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1819 expr
->flags
= CEF_NONE
;
1823 examine_symbol_type(ctype
);
1825 /* Dereferencing a node drops all the node information. */
1826 if (ctype
->type
== SYM_NODE
)
1827 ctype
= ctype
->ctype
.base_type
;
1829 target
= ctype
->ctype
.base_type
;
1831 switch (ctype
->type
) {
1833 expression_error(expr
, "cannot dereference this type");
1839 examine_symbol_type(target
);
1840 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1841 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1842 merge_type(node
, ctype
);
1846 if (!lvalue_expression(op
)) {
1847 expression_error(op
, "non-lvalue array??");
1851 /* Do the implied "addressof" on the array */
1855 * When an array is dereferenced, we need to pick
1856 * up the attributes of the original node too..
1858 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1859 merge_type(node
, op
->ctype
);
1860 merge_type(node
, ctype
);
1864 node
->bit_size
= target
->bit_size
;
1865 node
->array_size
= target
->array_size
;
1872 * Unary post-ops: x++ and x--
1874 static struct symbol
*evaluate_postop(struct expression
*expr
)
1876 struct expression
*op
= expr
->unop
;
1877 struct symbol
*ctype
= op
->ctype
;
1878 int class = classify_type(ctype
, &ctype
);
1881 if (!class || class & TYPE_COMPOUND
) {
1882 expression_error(expr
, "need scalar for ++/--");
1885 if (!lvalue_expression(expr
->unop
)) {
1886 expression_error(expr
, "need lvalue expression for ++/--");
1890 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1891 unrestrict(expr
, class, &ctype
);
1893 if (class & TYPE_NUM
) {
1895 } else if (class == TYPE_PTR
) {
1896 struct symbol
*target
= examine_pointer_target(ctype
);
1897 if (!is_function(target
))
1898 multiply
= bits_to_bytes(target
->bit_size
);
1902 evaluate_assign_to(op
, op
->ctype
);
1903 expr
->op_value
= multiply
;
1904 expr
->ctype
= ctype
;
1908 expression_error(expr
, "bad argument type for ++/--");
1912 static struct symbol
*evaluate_sign(struct expression
*expr
)
1914 struct symbol
*ctype
= expr
->unop
->ctype
;
1915 int class = classify_type(ctype
, &ctype
);
1916 unsigned char flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1918 /* should be an arithmetic type */
1919 if (!(class & TYPE_NUM
))
1920 return bad_expr_type(expr
);
1921 if (class & TYPE_RESTRICT
)
1924 if (!(class & TYPE_FLOAT
)) {
1925 ctype
= integer_promotion(ctype
);
1926 expr
->unop
= cast_to(expr
->unop
, ctype
);
1927 } else if (expr
->op
!= '~') {
1928 /* no conversions needed */
1930 return bad_expr_type(expr
);
1932 if (expr
->op
== '+')
1933 *expr
= *expr
->unop
;
1934 expr
->flags
= flags
;
1935 expr
->ctype
= ctype
;
1938 if (restricted_unop(expr
->op
, &ctype
))
1939 unrestrict(expr
, class, &ctype
);
1943 static struct symbol
*evaluate_preop(struct expression
*expr
)
1945 struct symbol
*ctype
= expr
->unop
->ctype
;
1949 *expr
= *expr
->unop
;
1955 return evaluate_sign(expr
);
1958 return evaluate_dereference(expr
);
1961 return evaluate_addressof(expr
);
1963 case SPECIAL_INCREMENT
:
1964 case SPECIAL_DECREMENT
:
1966 * From a type evaluation standpoint the preops are
1967 * the same as the postops
1969 return evaluate_postop(expr
);
1972 ctype
= degenerate(expr
->unop
);
1973 expr
->flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1975 * A logical negation never yields an address constant
1978 expr
->flags
&= ~CEF_ADDR
;
1980 if (is_safe_type(ctype
))
1981 warning(expr
->pos
, "testing a 'safe expression'");
1982 if (is_float_type(ctype
)) {
1983 struct expression
*arg
= expr
->unop
;
1984 expr
->type
= EXPR_COMPARE
;
1985 expr
->op
= SPECIAL_EQUAL
;
1987 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1988 expr
->right
->ctype
= ctype
;
1989 expr
->right
->fvalue
= 0;
1990 } else if (is_fouled_type(ctype
)) {
1991 warning(expr
->pos
, "%s degrades to integer",
1992 show_typename(ctype
->ctype
.base_type
));
1994 /* the result is int [6.5.3.3(5)]*/
2001 expr
->ctype
= ctype
;
2005 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
2007 struct ptr_list
*head
= (struct ptr_list
*)_list
;
2008 struct ptr_list
*list
= head
;
2014 for (i
= 0; i
< list
->nr
; i
++) {
2015 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
2017 if (sym
->ident
!= ident
)
2019 *offset
= sym
->offset
;
2022 struct symbol
*ctype
= sym
->ctype
.base_type
;
2026 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
2028 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
2031 *offset
+= sym
->offset
;
2035 } while ((list
= list
->next
) != head
);
2039 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
2041 struct expression
*add
;
2044 * Create a new add-expression
2046 * NOTE! Even if we just add zero, we need a new node
2047 * for the member pointer, since it has a different
2048 * type than the original pointer. We could make that
2049 * be just a cast, but the fact is, a node is a node,
2050 * so we might as well just do the "add zero" here.
2052 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
2055 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2056 add
->right
->ctype
= &int_ctype
;
2057 add
->right
->value
= offset
;
2060 * The ctype of the pointer will be lazily evaluated if
2061 * we ever take the address of this member dereference..
2063 add
->ctype
= &lazy_ptr_ctype
;
2065 * The resulting address of a member access through an address
2066 * constant is an address constant again [6.6(9)].
2068 add
->flags
= expr
->flags
;
2073 /* structure/union dereference */
2074 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
2077 struct symbol
*ctype
, *member
;
2078 struct expression
*deref
= expr
->deref
, *add
;
2079 struct ident
*ident
= expr
->member
;
2080 struct ident
*address_space
;
2083 if (!evaluate_expression(deref
))
2086 expression_error(expr
, "bad member name");
2090 ctype
= deref
->ctype
;
2091 examine_symbol_type(ctype
);
2092 address_space
= ctype
->ctype
.as
;
2093 mod
= ctype
->ctype
.modifiers
;
2094 if (ctype
->type
== SYM_NODE
) {
2095 ctype
= ctype
->ctype
.base_type
;
2096 combine_address_space(deref
->pos
, &address_space
, ctype
->ctype
.as
);
2097 mod
|= ctype
->ctype
.modifiers
;
2099 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
2100 expression_error(expr
, "expected structure or union");
2104 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
2106 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
2107 const char *name
= "<unnamed>";
2110 name
= ctype
->ident
->name
;
2111 namelen
= ctype
->ident
->len
;
2113 if (ctype
->symbol_list
)
2114 expression_error(expr
, "no member '%s' in %s %.*s",
2115 show_ident(ident
), type
, namelen
, name
);
2117 expression_error(expr
, "using member '%s' in "
2118 "incomplete %s %.*s", show_ident(ident
),
2119 type
, namelen
, name
);
2124 * The member needs to take on the address space and modifiers of
2125 * the "parent" type.
2127 member
= convert_to_as_mod(member
, address_space
, mod
);
2128 ctype
= get_base_type(member
);
2130 if (!lvalue_expression(deref
)) {
2131 if (deref
->type
!= EXPR_SLICE
) {
2135 expr
->base
= deref
->base
;
2136 expr
->r_bitpos
= deref
->r_bitpos
;
2138 expr
->r_bitpos
+= bytes_to_bits(offset
);
2139 expr
->type
= EXPR_SLICE
;
2140 expr
->r_nrbits
= member
->bit_size
;
2141 expr
->r_bitpos
+= member
->bit_offset
;
2142 expr
->ctype
= member
;
2146 deref
= deref
->unop
;
2147 expr
->deref
= deref
;
2149 add
= evaluate_offset(deref
, offset
);
2150 expr
->type
= EXPR_PREOP
;
2154 expr
->ctype
= member
;
2158 static int is_promoted(struct expression
*expr
)
2161 switch (expr
->type
) {
2164 case EXPR_CONDITIONAL
:
2188 static struct symbol
*evaluate_cast(struct expression
*);
2190 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2192 struct symbol
*sym
= expr
->cast_type
;
2194 sym
= evaluate_expression(expr
->cast_expression
);
2198 * Expressions of restricted types will possibly get
2199 * promoted - check that here
2201 if (is_restricted_type(sym
)) {
2202 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2204 } else if (is_fouled_type(sym
)) {
2208 examine_symbol_type(sym
);
2209 if (is_bitfield_type(sym
)) {
2210 expression_error(expr
, "trying to examine bitfield type");
2216 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2218 struct symbol
*type
;
2221 type
= evaluate_type_information(expr
);
2225 size
= type
->bit_size
;
2227 if (size
< 0 && is_void_type(type
)) {
2229 warning(expr
->pos
, "expression using sizeof(void)");
2230 size
= bits_in_char
;
2233 if (is_bool_type(type
)) {
2235 warning(expr
->pos
, "expression using sizeof _Bool");
2236 size
= bits_to_bytes(bits_in_bool
) * bits_in_char
;
2239 if (is_function(type
->ctype
.base_type
)) {
2241 warning(expr
->pos
, "expression using sizeof on a function");
2242 size
= bits_in_char
;
2245 if (is_array_type(type
) && size
< 0) { // VLA, 1-dimension only
2246 struct expression
*base
, *size
;
2247 struct symbol
*base_type
;
2249 if (type
->type
== SYM_NODE
)
2250 type
= type
->ctype
.base_type
; // strip the SYM_NODE
2251 base_type
= get_base_type(type
);
2254 if (base_type
->bit_size
<= 0) {
2255 base
= alloc_expression(expr
->pos
, EXPR_SIZEOF
);
2256 base
->cast_type
= base_type
;
2257 if (!evaluate_sizeof(base
))
2260 base
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2261 base
->value
= bits_to_bytes(base_type
->bit_size
);
2262 base
->ctype
= size_t_ctype
;
2264 size
= alloc_expression(expr
->pos
, EXPR_CAST
);
2265 size
->cast_type
= size_t_ctype
;
2266 size
->cast_expression
= type
->array_size
;
2267 if (!evaluate_expression(size
))
2271 expr
->type
= EXPR_BINOP
;
2273 return expr
->ctype
= size_t_ctype
;
2277 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2278 expression_error(expr
, "cannot size expression");
2280 expr
->type
= EXPR_VALUE
;
2281 expr
->value
= bits_to_bytes(size
);
2283 expr
->ctype
= size_t_ctype
;
2284 return size_t_ctype
;
2287 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2289 struct symbol
*type
;
2292 type
= evaluate_type_information(expr
);
2296 if (type
->type
== SYM_NODE
)
2297 type
= type
->ctype
.base_type
;
2300 switch (type
->type
) {
2304 type
= get_base_type(type
);
2308 expression_error(expr
, "expected pointer expression");
2311 size
= type
->bit_size
;
2312 if (size
& (bits_in_char
-1))
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_alignof(struct expression
*expr
)
2323 struct symbol
*type
;
2325 type
= evaluate_type_information(expr
);
2329 expr
->type
= EXPR_VALUE
;
2330 expr
->value
= type
->ctype
.alignment
;
2332 expr
->ctype
= size_t_ctype
;
2333 return size_t_ctype
;
2336 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
2338 struct expression
*expr
;
2339 struct symbol_list
*argument_types
= fn
->arguments
;
2340 struct symbol
*argtype
;
2343 PREPARE_PTR_LIST(argument_types
, argtype
);
2344 FOR_EACH_PTR (head
, expr
) {
2345 struct expression
**p
= THIS_ADDRESS(expr
);
2346 struct symbol
*ctype
, *target
;
2347 ctype
= evaluate_expression(expr
);
2354 struct symbol
*type
;
2355 int class = classify_type(ctype
, &type
);
2356 if (is_int(class)) {
2357 *p
= cast_to(expr
, integer_promotion(type
));
2358 } else if (class & TYPE_FLOAT
) {
2360 *p
= cast_to(expr
, &double_ctype
);
2361 } else if (class & TYPE_PTR
) {
2362 if (expr
->ctype
== &null_ctype
)
2363 *p
= cast_to(expr
, &ptr_ctype
);
2367 } else if (!target
->forced_arg
){
2368 static char where
[30];
2369 examine_symbol_type(target
);
2370 sprintf(where
, "argument %d", i
);
2371 compatible_argument_type(expr
, target
, p
, where
);
2375 NEXT_PTR_LIST(argtype
);
2376 } END_FOR_EACH_PTR(expr
);
2377 FINISH_PTR_LIST(argtype
);
2381 static void convert_index(struct expression
*e
)
2383 struct expression
*child
= e
->idx_expression
;
2384 unsigned from
= e
->idx_from
;
2385 unsigned to
= e
->idx_to
+ 1;
2387 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2388 e
->init_nr
= to
- from
;
2389 e
->init_expr
= child
;
2392 static void convert_ident(struct expression
*e
)
2394 struct expression
*child
= e
->ident_expression
;
2395 int offset
= e
->offset
;
2398 e
->init_offset
= offset
;
2400 e
->init_expr
= child
;
2403 static void convert_designators(struct expression
*e
)
2406 if (e
->type
== EXPR_INDEX
)
2408 else if (e
->type
== EXPR_IDENTIFIER
)
2416 static void excess(struct expression
*e
, const char *s
)
2418 warning(e
->pos
, "excessive elements in %s initializer", s
);
2422 * implicit designator for the first element
2424 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2425 struct expression
**v
)
2427 struct expression
*e
= *v
, *new;
2429 if (ctype
->type
== SYM_NODE
)
2430 ctype
= ctype
->ctype
.base_type
;
2432 if (class & TYPE_PTR
) { /* array */
2433 if (!ctype
->bit_size
)
2435 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2436 new->idx_expression
= e
;
2437 new->ctype
= ctype
->ctype
.base_type
;
2439 struct symbol
*field
, *p
;
2440 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2441 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2447 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2448 new->ident_expression
= e
;
2449 new->field
= new->ctype
= field
;
2450 new->offset
= field
->offset
;
2457 * sanity-check explicit designators; return the innermost one or NULL
2458 * in case of error. Assign types.
2460 static struct expression
*check_designators(struct expression
*e
,
2461 struct symbol
*ctype
)
2463 struct expression
*last
= NULL
;
2466 if (ctype
->type
== SYM_NODE
)
2467 ctype
= ctype
->ctype
.base_type
;
2468 if (e
->type
== EXPR_INDEX
) {
2469 struct symbol
*type
;
2470 if (ctype
->type
!= SYM_ARRAY
) {
2471 err
= "array index in non-array";
2474 type
= ctype
->ctype
.base_type
;
2475 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2476 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2477 if (offset
>= ctype
->bit_size
) {
2478 err
= "index out of bounds in";
2482 e
->ctype
= ctype
= type
;
2485 if (!e
->idx_expression
) {
2489 e
= e
->idx_expression
;
2490 } else if (e
->type
== EXPR_IDENTIFIER
) {
2492 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2493 err
= "field name not in struct or union";
2496 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2498 err
= "unknown field name in";
2502 e
->field
= e
->ctype
= ctype
;
2504 if (!e
->ident_expression
) {
2508 e
= e
->ident_expression
;
2509 } else if (e
->type
== EXPR_POS
) {
2510 err
= "internal front-end error: EXPR_POS in";
2515 expression_error(e
, "%s initializer", err
);
2520 * choose the next subobject to initialize.
2522 * Get designators for next element, switch old ones to EXPR_POS.
2523 * Return the resulting expression or NULL if we'd run out of subobjects.
2524 * The innermost designator is returned in *v. Designators in old
2525 * are assumed to be already sanity-checked.
2527 static struct expression
*next_designators(struct expression
*old
,
2528 struct symbol
*ctype
,
2529 struct expression
*e
, struct expression
**v
)
2531 struct expression
*new = NULL
;
2535 if (old
->type
== EXPR_INDEX
) {
2536 struct expression
*copy
;
2539 copy
= next_designators(old
->idx_expression
,
2542 n
= old
->idx_to
+ 1;
2543 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2548 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2551 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2554 new->idx_from
= new->idx_to
= n
;
2555 new->idx_expression
= copy
;
2556 new->ctype
= old
->ctype
;
2558 } else if (old
->type
== EXPR_IDENTIFIER
) {
2559 struct expression
*copy
;
2560 struct symbol
*field
;
2563 copy
= next_designators(old
->ident_expression
,
2566 field
= old
->field
->next_subobject
;
2572 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2574 * We can't necessarily trust "field->offset",
2575 * because the field might be in an anonymous
2576 * union, and the field offset is then the offset
2577 * within that union.
2579 * The "old->offset - old->field->offset"
2580 * would be the offset of such an anonymous
2583 offset
= old
->offset
- old
->field
->offset
;
2586 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2590 new->expr_ident
= field
->ident
;
2591 new->ident_expression
= copy
;
2593 new->offset
= field
->offset
+ offset
;
2599 static int handle_initializer(struct expression
**ep
, int nested
,
2600 int class, struct symbol
*ctype
, unsigned long mods
);
2603 * deal with traversing subobjects [6.7.8(17,18,20)]
2605 static void handle_list_initializer(struct expression
*expr
,
2606 int class, struct symbol
*ctype
, unsigned long mods
)
2608 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2611 if (expr
->zero_init
)
2612 free_ptr_list(&expr
->expr_list
);
2614 FOR_EACH_PTR(expr
->expr_list
, e
) {
2615 struct expression
**v
;
2616 struct symbol
*type
;
2619 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2620 struct symbol
*struct_sym
;
2623 last
= first_subobject(ctype
, class, &top
);
2625 last
= next_designators(last
, ctype
, e
, &top
);
2628 excess(e
, class & TYPE_PTR
? "array" :
2630 DELETE_CURRENT_PTR(e
);
2633 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2634 if (Wdesignated_init
&& struct_sym
->designated_init
)
2635 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2636 ctype
->ident
? "in initializer for " : "",
2637 ctype
->ident
? ctype
->ident
->len
: 0,
2638 ctype
->ident
? ctype
->ident
->name
: "",
2639 ctype
->ident
? ": " : "",
2640 get_type_name(struct_sym
->type
),
2641 show_ident(struct_sym
->ident
));
2643 warning(e
->pos
, "advancing past deep designator");
2646 REPLACE_CURRENT_PTR(e
, last
);
2648 next
= check_designators(e
, ctype
);
2650 DELETE_CURRENT_PTR(e
);
2654 /* deeper than one designator? */
2656 convert_designators(last
);
2661 lclass
= classify_type(top
->ctype
, &type
);
2662 if (top
->type
== EXPR_INDEX
)
2663 v
= &top
->idx_expression
;
2665 v
= &top
->ident_expression
;
2667 mods
|= ctype
->ctype
.modifiers
& MOD_STORAGE
;
2668 if (handle_initializer(v
, 1, lclass
, top
->ctype
, mods
))
2671 if (!(lclass
& TYPE_COMPOUND
)) {
2672 warning(e
->pos
, "bogus scalar initializer");
2673 DELETE_CURRENT_PTR(e
);
2677 next
= first_subobject(type
, lclass
, v
);
2679 warning(e
->pos
, "missing braces around initializer");
2684 DELETE_CURRENT_PTR(e
);
2685 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2687 } END_FOR_EACH_PTR(e
);
2689 convert_designators(last
);
2690 expr
->ctype
= ctype
;
2693 static int is_string_literal(struct expression
**v
)
2695 struct expression
*e
= *v
;
2696 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2698 if (!e
|| e
->type
!= EXPR_STRING
)
2700 if (e
!= *v
&& Wparen_string
)
2702 "array initialized from parenthesized string constant");
2708 * We want a normal expression, possibly in one layer of braces. Warn
2709 * if the latter happens inside a list (it's legal, but likely to be
2710 * an effect of screwup). In case of anything not legal, we are definitely
2711 * having an effect of screwup, so just fail and let the caller warn.
2713 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2715 struct expression
*v
= NULL
, *p
;
2719 if (e
->type
!= EXPR_INITIALIZER
)
2722 FOR_EACH_PTR(e
->expr_list
, p
) {
2726 } END_FOR_EACH_PTR(p
);
2730 case EXPR_INITIALIZER
:
2732 case EXPR_IDENTIFIER
:
2738 warning(e
->pos
, "braces around scalar initializer");
2743 * deal with the cases that don't care about subobjects:
2744 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2745 * character array <- string literal, possibly in braces [6.7.8(14)]
2746 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2747 * compound type <- initializer list in braces [6.7.8(16)]
2748 * The last one punts to handle_list_initializer() which, in turn will call
2749 * us for individual elements of the list.
2751 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2752 * the lack of support of wide char stuff in general.
2754 * One note: we need to take care not to evaluate a string literal until
2755 * we know that we *will* handle it right here. Otherwise we would screw
2756 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2757 * { "string", ...} - we need to preserve that string literal recognizable
2758 * until we dig into the inner struct.
2760 static int handle_initializer(struct expression
**ep
, int nested
,
2761 int class, struct symbol
*ctype
, unsigned long mods
)
2763 int is_string
= is_string_type(ctype
);
2764 struct expression
*e
= *ep
, *p
;
2765 struct symbol
*type
;
2771 if (!(class & TYPE_COMPOUND
)) {
2772 e
= handle_scalar(e
, nested
);
2776 if (!evaluate_expression(e
))
2778 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2780 * Initializers for static storage duration objects
2781 * shall be constant expressions or a string literal [6.7.8(4)].
2783 mods
|= ctype
->ctype
.modifiers
;
2784 mods
&= (MOD_TOPLEVEL
| MOD_STATIC
);
2785 if (mods
&& !(e
->flags
& (CEF_ACE
| CEF_ADDR
)))
2786 if (Wconstexpr_not_const
)
2787 warning(e
->pos
, "non-constant initializer for static object");
2793 * sublist; either a string, or we dig in; the latter will deal with
2794 * pathologies, so we don't need anything fancy here.
2796 if (e
->type
== EXPR_INITIALIZER
) {
2798 struct expression
*v
= NULL
;
2801 FOR_EACH_PTR(e
->expr_list
, p
) {
2805 } END_FOR_EACH_PTR(p
);
2806 if (count
== 1 && is_string_literal(&v
)) {
2811 handle_list_initializer(e
, class, ctype
, mods
);
2816 if (is_string_literal(&e
)) {
2817 /* either we are doing array of char, or we'll have to dig in */
2824 /* struct or union can be initialized by compatible */
2825 if (class != TYPE_COMPOUND
)
2827 type
= evaluate_expression(e
);
2830 if (ctype
->type
== SYM_NODE
)
2831 ctype
= ctype
->ctype
.base_type
;
2832 if (type
->type
== SYM_NODE
)
2833 type
= type
->ctype
.base_type
;
2839 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2841 type
= evaluate_expression(p
);
2842 if (ctype
->bit_size
!= -1) {
2843 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2845 "too long initializer-string for array of char");
2846 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2848 "too long initializer-string for array of char(no space for nul char)");
2855 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2857 struct symbol
*type
;
2858 int class = classify_type(ctype
, &type
);
2859 if (!handle_initializer(ep
, 0, class, ctype
, 0))
2860 expression_error(*ep
, "invalid initializer");
2863 static struct symbol
*cast_to_bool(struct expression
*expr
)
2865 struct expression
*old
= expr
->cast_expression
;
2866 struct expression
*zero
;
2867 struct symbol
*otype
;
2868 int oclass
= classify_type(degenerate(old
), &otype
);
2869 struct symbol
*ctype
;
2871 if (oclass
& TYPE_COMPOUND
)
2874 zero
= alloc_const_expression(expr
->pos
, 0);
2875 expr
->op
= SPECIAL_NOTEQUAL
;
2876 ctype
= usual_conversions(expr
->op
, old
, zero
,
2877 oclass
, TYPE_NUM
, otype
, zero
->ctype
);
2878 expr
->type
= EXPR_COMPARE
;
2879 expr
->left
= cast_to(old
, ctype
);
2880 expr
->right
= cast_to(zero
, ctype
);
2885 static int cast_flags(struct expression
*expr
, struct expression
*old
)
2889 int flags
= CEF_NONE
;
2891 class = classify_type(expr
->ctype
, &t
);
2892 if (class & TYPE_NUM
) {
2893 flags
= old
->flags
& ~CEF_CONST_MASK
;
2895 * Casts to numeric types never result in address
2896 * constants [6.6(9)].
2901 * As an extension, treat address constants cast to
2902 * integer type as an arithmetic constant.
2904 if (old
->flags
& CEF_ADDR
)
2908 * Cast to float type -> not an integer constant
2909 * expression [6.6(6)].
2911 if (class & TYPE_FLOAT
)
2912 flags
&= ~CEF_CLR_ICE
;
2914 * Casts of float literals to integer type results in
2915 * a constant integer expression [6.6(6)].
2917 else if (old
->flags
& CEF_FLOAT
)
2918 flags
= CEF_SET_ICE
;
2919 } else if (class & TYPE_PTR
) {
2921 * Casts of integer literals to pointer type yield
2922 * address constants [6.6(9)].
2924 * As an extension, treat address constants cast to a
2925 * different pointer type as address constants again.
2927 * As another extension, treat integer constant
2928 * expressions (in contrast to literals) cast to
2929 * pointer type as address constants.
2931 if (old
->flags
& (CEF_ICE
| CEF_ADDR
))
2938 static struct symbol
*evaluate_cast(struct expression
*expr
)
2940 struct expression
*source
= expr
->cast_expression
;
2941 struct symbol
*ctype
;
2942 struct symbol
*ttype
, *stype
;
2944 struct ident
*tas
= NULL
, *sas
= NULL
;
2950 * Special case: a cast can be followed by an
2951 * initializer, in which case we need to pass
2952 * the type value down to that initializer rather
2953 * than trying to evaluate it as an expression
2954 * (cfr. compound literals: C99 & C11 6.5.2.5).
2956 * A more complex case is when the initializer is
2957 * dereferenced as part of a post-fix expression.
2958 * We need to produce an expression that can be dereferenced.
2960 if (source
->type
== EXPR_INITIALIZER
) {
2961 struct symbol
*sym
= expr
->cast_type
;
2962 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2964 sym
->initializer
= source
;
2965 evaluate_symbol(sym
);
2967 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2969 if (sym
->ctype
.modifiers
& MOD_TOPLEVEL
)
2970 addr
->flags
|= CEF_ADDR
;
2972 expr
->type
= EXPR_PREOP
;
2980 ctype
= examine_symbol_type(expr
->cast_type
);
2981 expr
->ctype
= ctype
;
2982 expr
->cast_type
= ctype
;
2984 evaluate_expression(source
);
2987 tclass
= classify_type(ctype
, &ttype
);
2989 expr
->flags
= cast_flags(expr
, source
);
2992 * You can always throw a value away by casting to
2993 * "void" - that's an implicit "force". Note that
2994 * the same is _not_ true of "void *".
2996 if (ttype
== &void_ctype
)
2999 stype
= source
->ctype
;
3001 expression_error(expr
, "cast from unknown type");
3004 sclass
= classify_type(stype
, &stype
);
3006 if (expr
->type
== EXPR_FORCE_CAST
)
3009 if (tclass
& (TYPE_COMPOUND
| TYPE_FN
))
3010 warning(expr
->pos
, "cast to non-scalar");
3012 if (sclass
& TYPE_COMPOUND
)
3013 warning(expr
->pos
, "cast from non-scalar");
3015 /* allowed cast unfouls */
3016 if (sclass
& TYPE_FOULED
)
3017 stype
= unfoul(stype
);
3019 if (ttype
!= stype
) {
3020 if ((tclass
& TYPE_RESTRICT
) && restricted_value(source
, ttype
))
3021 warning(expr
->pos
, "cast to %s",
3022 show_typename(ttype
));
3023 if (sclass
& TYPE_RESTRICT
) {
3024 if (ttype
== &bool_ctype
) {
3025 if (sclass
& TYPE_FOULED
)
3026 warning(expr
->pos
, "%s degrades to integer",
3027 show_typename(stype
));
3029 warning(expr
->pos
, "cast from %s",
3030 show_typename(stype
));
3035 if ((ttype
== &ulong_ctype
|| ttype
== uintptr_ctype
) && !Wcast_from_as
)
3036 tas
= &bad_address_space
;
3037 else if (tclass
== TYPE_PTR
) {
3038 examine_pointer_target(ttype
);
3039 tas
= ttype
->ctype
.as
;
3042 if ((stype
== &ulong_ctype
|| stype
== uintptr_ctype
))
3043 sas
= &bad_address_space
;
3044 else if (sclass
== TYPE_PTR
) {
3045 examine_pointer_target(stype
);
3046 sas
= stype
->ctype
.as
;
3049 if (!tas
&& valid_as(sas
))
3050 warning(expr
->pos
, "cast removes address space '%s' of expression", show_as(sas
));
3051 if (valid_as(tas
) && valid_as(sas
) && tas
!= sas
)
3052 warning(expr
->pos
, "cast between address spaces (%s -> %s)", show_as(sas
), show_as(tas
));
3053 if (valid_as(tas
) && !sas
&&
3054 !is_null_pointer_constant(source
) && Wcast_to_as
)
3056 "cast adds address space '%s' to expression", show_as(tas
));
3058 if (!(ttype
->ctype
.modifiers
& MOD_PTRINHERIT
) && tclass
== TYPE_PTR
&&
3059 !tas
&& (source
->flags
& CEF_ICE
)) {
3060 if (ttype
->ctype
.base_type
== &void_ctype
) {
3061 if (is_zero_constant(source
)) {
3063 expr
->type
= EXPR_VALUE
;
3064 expr
->ctype
= &null_ctype
;
3071 if (ttype
== &bool_ctype
)
3074 // checks pointers to restricted
3075 while (Wbitwise_pointer
&& tclass
== TYPE_PTR
&& sclass
== TYPE_PTR
) {
3076 tclass
= classify_type(ttype
->ctype
.base_type
, &ttype
);
3077 sclass
= classify_type(stype
->ctype
.base_type
, &stype
);
3080 if (!ttype
|| !stype
)
3082 if (ttype
== &void_ctype
|| stype
== &void_ctype
)
3084 if (tclass
& TYPE_RESTRICT
) {
3085 warning(expr
->pos
, "cast to %s", show_typename(ctype
));
3088 if (sclass
& TYPE_RESTRICT
) {
3089 warning(expr
->pos
, "cast from %s", show_typename(source
->ctype
));
3098 * Evaluate a call expression with a symbol. This
3099 * should expand inline functions, and evaluate
3102 static int evaluate_symbol_call(struct expression
*expr
)
3104 struct expression
*fn
= expr
->fn
;
3105 struct symbol
*ctype
= fn
->ctype
;
3107 if (fn
->type
!= EXPR_PREOP
)
3110 if (ctype
->op
&& ctype
->op
->evaluate
)
3111 return ctype
->op
->evaluate(expr
);
3116 static struct symbol
*evaluate_call(struct expression
*expr
)
3119 struct symbol
*ctype
, *sym
;
3120 struct expression
*fn
= expr
->fn
;
3121 struct expression_list
*arglist
= expr
->args
;
3123 if (!evaluate_expression(fn
))
3125 sym
= ctype
= fn
->ctype
;
3126 if (ctype
->type
== SYM_NODE
)
3127 ctype
= ctype
->ctype
.base_type
;
3128 if (ctype
->type
== SYM_PTR
)
3129 ctype
= get_base_type(ctype
);
3131 if (ctype
->type
!= SYM_FN
) {
3132 struct expression
*arg
;
3134 if (fn
->ctype
== &bad_ctype
)
3137 expression_error(expr
, "not a function %s",
3138 show_ident(sym
->ident
));
3139 /* do typechecking in arguments */
3140 FOR_EACH_PTR (arglist
, arg
) {
3141 evaluate_expression(arg
);
3142 } END_FOR_EACH_PTR(arg
);
3146 examine_fn_arguments(ctype
);
3147 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
3148 sym
->op
&& sym
->op
->args
) {
3149 if (!sym
->op
->args(expr
))
3152 if (!evaluate_arguments(ctype
, arglist
))
3154 args
= expression_list_size(expr
->args
);
3155 fnargs
= symbol_list_size(ctype
->arguments
);
3156 if (args
< fnargs
) {
3157 expression_error(expr
,
3158 "not enough arguments for function %s",
3159 show_ident(sym
->ident
));
3162 if (args
> fnargs
&& !ctype
->variadic
)
3163 expression_error(expr
,
3164 "too many arguments for function %s",
3165 show_ident(sym
->ident
));
3167 expr
->ctype
= ctype
->ctype
.base_type
;
3168 if (sym
->type
== SYM_NODE
) {
3169 if (evaluate_symbol_call(expr
))
3175 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
3177 struct expression
*e
= expr
->down
;
3178 struct symbol
*ctype
= expr
->in
;
3181 if (expr
->op
== '.') {
3182 struct symbol
*field
;
3185 expression_error(expr
, "expected structure or union");
3188 examine_symbol_type(ctype
);
3189 class = classify_type(ctype
, &ctype
);
3190 if (class != TYPE_COMPOUND
) {
3191 expression_error(expr
, "expected structure or union");
3195 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
3197 expression_error(expr
, "unknown member");
3201 expr
->type
= EXPR_VALUE
;
3202 expr
->flags
= CEF_SET_ICE
;
3203 expr
->value
= offset
;
3205 expr
->ctype
= size_t_ctype
;
3208 expression_error(expr
, "expected structure or union");
3211 examine_symbol_type(ctype
);
3212 class = classify_type(ctype
, &ctype
);
3213 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
3214 expression_error(expr
, "expected array");
3217 ctype
= ctype
->ctype
.base_type
;
3219 expr
->type
= EXPR_VALUE
;
3220 expr
->flags
= CEF_SET_ICE
;
3223 expr
->ctype
= size_t_ctype
;
3225 struct expression
*idx
= expr
->index
, *m
;
3226 struct symbol
*i_type
= evaluate_expression(idx
);
3227 unsigned old_idx_flags
;
3228 int i_class
= classify_type(i_type
, &i_type
);
3230 if (!is_int(i_class
)) {
3231 expression_error(expr
, "non-integer index");
3234 unrestrict(idx
, i_class
, &i_type
);
3235 old_idx_flags
= idx
->flags
;
3236 idx
= cast_to(idx
, size_t_ctype
);
3237 idx
->flags
= old_idx_flags
;
3238 m
= alloc_const_expression(expr
->pos
,
3239 bits_to_bytes(ctype
->bit_size
));
3240 m
->ctype
= size_t_ctype
;
3241 m
->flags
= CEF_SET_INT
;
3242 expr
->type
= EXPR_BINOP
;
3246 expr
->ctype
= size_t_ctype
;
3247 expr
->flags
= m
->flags
& idx
->flags
& ~CEF_CONST_MASK
;
3251 struct expression
*copy
= __alloc_expression(0);
3253 if (e
->type
== EXPR_OFFSETOF
)
3255 if (!evaluate_expression(e
))
3257 expr
->type
= EXPR_BINOP
;
3258 expr
->flags
= e
->flags
& copy
->flags
& ~CEF_CONST_MASK
;
3260 expr
->ctype
= size_t_ctype
;
3264 return size_t_ctype
;
3267 static void check_label_declaration(struct position pos
, struct symbol
*label
)
3269 switch (label
->namespace) {
3273 sparse_error(pos
, "label '%s' was not declared", show_ident(label
->ident
));
3276 current_fn
->bogus_linear
= 1;
3282 static int type_selection(struct symbol
*ctrl
, struct symbol
*type
)
3284 struct ctype c
= { .base_type
= ctrl
};
3285 struct ctype t
= { .base_type
= type
};
3287 return !type_difference(&c
, &t
, 0, 0);
3290 static struct symbol
*evaluate_generic_selection(struct expression
*expr
)
3292 struct type_expression
*map
;
3293 struct expression
*res
;
3294 struct symbol source
;
3295 struct symbol
*ctrl
;
3297 if (!evaluate_expression(expr
->control
))
3299 if (!(ctrl
= degenerate(expr
->control
)))
3303 source
.ctype
.modifiers
&= ~(MOD_QUALIFIER
|MOD_ATOMIC
);
3304 for (map
= expr
->map
; map
; map
= map
->next
) {
3305 struct symbol
*stype
= map
->type
;
3306 struct symbol
*base
;
3308 if (!evaluate_symbol(stype
))
3311 base
= stype
->ctype
.base_type
;
3312 if (base
->type
== SYM_ARRAY
&& base
->array_size
) {
3313 get_expression_value_silent(base
->array_size
);
3314 if (base
->array_size
->type
== EXPR_VALUE
)
3316 sparse_error(stype
->pos
, "variable length array type in generic selection");
3319 if (is_func_type(stype
)) {
3320 sparse_error(stype
->pos
, "function type in generic selection");
3323 if (stype
->bit_size
<= 0 || is_void_type(stype
)) {
3324 sparse_error(stype
->pos
, "incomplete type in generic selection");
3327 if (!type_selection(&source
, stype
))
3335 sparse_error(expr
->pos
, "no generic selection for '%s'", show_typename(ctrl
));
3341 return evaluate_expression(expr
);
3344 struct symbol
*evaluate_expression(struct expression
*expr
)
3351 switch (expr
->type
) {
3354 expression_error(expr
, "value expression without a type");
3357 return evaluate_string(expr
);
3359 return evaluate_symbol_expression(expr
);
3361 evaluate_expression(expr
->left
);
3362 evaluate_expression(expr
->right
);
3363 if (!valid_subexpr_type(expr
))
3365 return evaluate_binop(expr
);
3367 return evaluate_logical(expr
);
3369 evaluate_expression(expr
->left
);
3370 if (!evaluate_expression(expr
->right
))
3372 return evaluate_comma(expr
);
3374 evaluate_expression(expr
->left
);
3375 evaluate_expression(expr
->right
);
3376 if (!valid_subexpr_type(expr
))
3378 return evaluate_compare(expr
);
3379 case EXPR_ASSIGNMENT
:
3380 evaluate_expression(expr
->left
);
3381 evaluate_expression(expr
->right
);
3382 if (!valid_subexpr_type(expr
))
3384 return evaluate_assignment(expr
);
3386 if (!evaluate_expression(expr
->unop
))
3388 return evaluate_preop(expr
);
3390 if (!evaluate_expression(expr
->unop
))
3392 return evaluate_postop(expr
);
3394 case EXPR_FORCE_CAST
:
3395 case EXPR_IMPLIED_CAST
:
3396 return evaluate_cast(expr
);
3398 return evaluate_sizeof(expr
);
3399 case EXPR_PTRSIZEOF
:
3400 return evaluate_ptrsizeof(expr
);
3402 return evaluate_alignof(expr
);
3404 return evaluate_member_dereference(expr
);
3406 return evaluate_call(expr
);
3408 case EXPR_CONDITIONAL
:
3409 return evaluate_conditional_expression(expr
);
3410 case EXPR_STATEMENT
:
3411 expr
->ctype
= evaluate_statement(expr
->statement
);
3415 expr
->ctype
= &ptr_ctype
;
3416 check_label_declaration(expr
->pos
, expr
->label_symbol
);
3420 /* Evaluate the type of the symbol .. */
3421 evaluate_symbol(expr
->symbol
);
3422 /* .. but the type of the _expression_ is a "type" */
3423 expr
->ctype
= &type_ctype
;
3427 return evaluate_offsetof(expr
);
3430 return evaluate_generic_selection(expr
);
3432 /* These can not exist as stand-alone expressions */
3433 case EXPR_INITIALIZER
:
3434 case EXPR_IDENTIFIER
:
3437 expression_error(expr
, "internal front-end error: initializer in expression");
3440 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3446 void check_duplicates(struct symbol
*sym
)
3449 struct symbol
*next
= sym
;
3450 int initialized
= sym
->initializer
!= NULL
;
3452 while ((next
= next
->same_symbol
) != NULL
) {
3453 const char *typediff
;
3454 evaluate_symbol(next
);
3455 if (initialized
&& next
->initializer
) {
3456 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3457 show_ident(sym
->ident
),
3458 stream_name(next
->pos
.stream
), next
->pos
.line
);
3459 /* Only warn once */
3463 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3465 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (%s):",
3466 show_ident(sym
->ident
), typediff
);
3467 info(sym
->pos
, " %s", show_typename(sym
));
3468 info(next
->pos
, "note: previously declared as:");
3469 info(next
->pos
, " %s", show_typename(next
));
3474 unsigned long mod
= sym
->ctype
.modifiers
;
3475 if (mod
& (MOD_STATIC
| MOD_REGISTER
| MOD_EXT_VISIBLE
))
3477 if (!(mod
& MOD_TOPLEVEL
))
3481 if (sym
->ident
== &main_ident
)
3483 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3487 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3489 struct symbol
*base_type
;
3497 sym
= examine_symbol_type(sym
);
3498 base_type
= get_base_type(sym
);
3502 /* Evaluate the initializers */
3503 if (sym
->initializer
)
3504 evaluate_initializer(sym
, &sym
->initializer
);
3506 /* And finally, evaluate the body of the symbol too */
3507 if (base_type
->type
== SYM_FN
) {
3508 struct symbol
*curr
= current_fn
;
3510 if (sym
->definition
&& sym
->definition
!= sym
)
3511 return evaluate_symbol(sym
->definition
);
3515 examine_fn_arguments(base_type
);
3516 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3518 if (base_type
->stmt
)
3519 evaluate_statement(base_type
->stmt
);
3527 void evaluate_symbol_list(struct symbol_list
*list
)
3531 FOR_EACH_PTR(list
, sym
) {
3532 has_error
&= ~ERROR_CURR_PHASE
;
3533 evaluate_symbol(sym
);
3534 check_duplicates(sym
);
3535 } END_FOR_EACH_PTR(sym
);
3538 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3540 struct expression
*expr
= stmt
->expression
;
3541 struct symbol
*fntype
, *rettype
;
3543 evaluate_expression(expr
);
3544 fntype
= current_fn
->ctype
.base_type
;
3545 rettype
= fntype
->ctype
.base_type
;
3546 if (!rettype
|| rettype
== &void_ctype
) {
3547 if (expr
&& expr
->ctype
!= &void_ctype
)
3548 expression_error(expr
, "return expression in %s function", rettype
?"void":"typeless");
3549 if (expr
&& Wreturn_void
)
3550 warning(stmt
->pos
, "returning void-valued expression");
3555 sparse_error(stmt
->pos
, "return with no return value");
3560 compatible_assignment_types(expr
, rettype
, &stmt
->expression
, "return expression");
3564 static void evaluate_if_statement(struct statement
*stmt
)
3566 if (!stmt
->if_conditional
)
3569 evaluate_conditional(stmt
->if_conditional
, 0);
3570 evaluate_statement(stmt
->if_true
);
3571 evaluate_statement(stmt
->if_false
);
3574 static void evaluate_iterator(struct statement
*stmt
)
3576 evaluate_symbol_list(stmt
->iterator_syms
);
3577 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3578 evaluate_conditional(stmt
->iterator_post_condition
,1);
3579 evaluate_statement(stmt
->iterator_pre_statement
);
3580 evaluate_statement(stmt
->iterator_statement
);
3581 evaluate_statement(stmt
->iterator_post_statement
);
3585 static void parse_asm_constraint(struct asm_operand
*op
)
3587 struct expression
*constraint
= op
->constraint
;
3588 const char *str
= constraint
->string
->data
;
3593 sparse_error(constraint
->pos
, "invalid ASM constraint (\"\")");
3596 op
->is_modify
= true;
3599 op
->is_assign
= true;
3604 while ((c
= *str
++)) {
3608 sparse_error(constraint
->pos
, "invalid ASM constraint '%c'", c
);
3612 op
->is_earlyclobber
= true;
3615 op
->is_commutative
= true;
3618 op
->is_register
= true;
3625 op
->is_memory
= true;
3630 // FIXME: ignored for now
3634 // FIXME: multiple alternative constraints
3638 // FIXME: numeric matching constraint?
3641 // FIXME: symbolic matching constraint
3645 if (arch_target
->asm_constraint
)
3646 str
= arch_target
->asm_constraint(op
, c
, str
);
3648 // FIXME: multi-letter constraints
3653 // FIXME: how to deal with multi-constraint?
3654 if (op
->is_register
)
3658 static void verify_output_constraint(struct asm_operand
*op
)
3660 struct expression
*expr
= op
->constraint
;
3661 const char *constraint
= expr
->string
->data
;
3664 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3667 static void verify_input_constraint(struct asm_operand
*op
)
3669 struct expression
*expr
= op
->constraint
;
3670 const char *constraint
= expr
->string
->data
;
3673 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3676 static void evaluate_asm_memop(struct asm_operand
*op
)
3678 if (op
->is_memory
) {
3679 struct expression
*expr
= op
->expr
;
3680 struct expression
*addr
;
3682 // implicit addressof
3683 addr
= alloc_expression(expr
->pos
, EXPR_PREOP
);
3687 evaluate_addressof(addr
);
3690 evaluate_expression(op
->expr
);
3691 degenerate(op
->expr
);
3695 static void evaluate_asm_statement(struct statement
*stmt
)
3697 struct expression
*expr
;
3698 struct asm_operand
*op
;
3701 if (!stmt
->asm_string
)
3704 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
3708 if (op
->constraint
) {
3709 parse_asm_constraint(op
);
3710 verify_output_constraint(op
);
3715 if (!evaluate_expression(expr
))
3717 if (!lvalue_expression(expr
))
3718 warning(expr
->pos
, "asm output is not an lvalue");
3719 evaluate_assign_to(expr
, expr
->ctype
);
3720 evaluate_asm_memop(op
);
3721 } END_FOR_EACH_PTR(op
);
3723 FOR_EACH_PTR(stmt
->asm_inputs
, op
) {
3727 if (op
->constraint
) {
3728 parse_asm_constraint(op
);
3729 verify_input_constraint(op
);
3733 if (!evaluate_expression(op
->expr
))
3735 evaluate_asm_memop(op
);
3736 } END_FOR_EACH_PTR(op
);
3738 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3740 sparse_error(stmt
->pos
, "bad asm clobbers");
3743 if (expr
->type
== EXPR_STRING
)
3745 expression_error(expr
, "asm clobber is not a string");
3746 } END_FOR_EACH_PTR(expr
);
3748 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3749 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3750 sparse_error(stmt
->pos
, "bad asm label");
3753 } END_FOR_EACH_PTR(sym
);
3756 static void evaluate_case_statement(struct statement
*stmt
)
3758 evaluate_expression(stmt
->case_expression
);
3759 evaluate_expression(stmt
->case_to
);
3760 evaluate_statement(stmt
->case_statement
);
3763 static void check_case_type(struct expression
*switch_expr
,
3764 struct expression
*case_expr
,
3765 struct expression
**enumcase
)
3767 struct symbol
*switch_type
, *case_type
;
3773 switch_type
= switch_expr
->ctype
;
3774 case_type
= evaluate_expression(case_expr
);
3776 if (!switch_type
|| !case_type
)
3780 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3781 else if (is_enum_type(case_type
))
3782 *enumcase
= case_expr
;
3785 sclass
= classify_type(switch_type
, &switch_type
);
3786 cclass
= classify_type(case_type
, &case_type
);
3788 /* both should be arithmetic */
3789 if (!(sclass
& cclass
& TYPE_NUM
))
3792 /* neither should be floating */
3793 if ((sclass
| cclass
) & TYPE_FLOAT
)
3796 /* if neither is restricted, we are OK */
3797 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3800 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3801 cclass
, sclass
, case_type
, switch_type
)) {
3802 unrestrict(case_expr
, cclass
, &case_type
);
3803 unrestrict(switch_expr
, sclass
, &switch_type
);
3808 expression_error(case_expr
, "incompatible types for 'case' statement");
3811 static void evaluate_switch_statement(struct statement
*stmt
)
3814 struct expression
*enumcase
= NULL
;
3815 struct expression
**enumcase_holder
= &enumcase
;
3816 struct expression
*sel
= stmt
->switch_expression
;
3818 evaluate_expression(sel
);
3819 evaluate_statement(stmt
->switch_statement
);
3822 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3823 enumcase_holder
= NULL
; /* Only check cases against switch */
3825 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3826 struct statement
*case_stmt
= sym
->stmt
;
3827 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3828 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3829 } END_FOR_EACH_PTR(sym
);
3832 static void evaluate_goto_statement(struct statement
*stmt
)
3834 struct symbol
*label
= stmt
->goto_label
;
3837 // no label associated, may be a computed goto
3838 evaluate_expression(stmt
->goto_expression
);
3842 check_label_declaration(stmt
->pos
, label
);
3845 struct symbol
*evaluate_statement(struct statement
*stmt
)
3850 switch (stmt
->type
) {
3851 case STMT_DECLARATION
: {
3853 FOR_EACH_PTR(stmt
->declaration
, s
) {
3855 } END_FOR_EACH_PTR(s
);
3860 return evaluate_return_expression(stmt
);
3862 case STMT_EXPRESSION
:
3863 if (!evaluate_expression(stmt
->expression
))
3865 if (stmt
->expression
->ctype
== &null_ctype
)
3866 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3867 return degenerate(stmt
->expression
);
3869 case STMT_COMPOUND
: {
3870 struct statement
*s
;
3871 struct symbol
*type
= NULL
;
3873 /* Evaluate the return symbol in the compound statement */
3874 evaluate_symbol(stmt
->ret
);
3877 * Then, evaluate each statement, making the type of the
3878 * compound statement be the type of the last statement
3880 type
= evaluate_statement(stmt
->args
);
3881 FOR_EACH_PTR(stmt
->stmts
, s
) {
3882 type
= evaluate_statement(s
);
3883 } END_FOR_EACH_PTR(s
);
3889 evaluate_if_statement(stmt
);
3892 evaluate_iterator(stmt
);
3895 evaluate_switch_statement(stmt
);
3898 evaluate_case_statement(stmt
);
3901 return evaluate_statement(stmt
->label_statement
);
3903 evaluate_goto_statement(stmt
);
3908 evaluate_asm_statement(stmt
);
3911 evaluate_expression(stmt
->expression
);
3914 evaluate_expression(stmt
->range_expression
);
3915 evaluate_expression(stmt
->range_low
);
3916 evaluate_expression(stmt
->range_high
);