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
;
105 struct symbol
*char_type
= expr
->wide
? wchar_ctype
: &char_ctype
;
107 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
108 sym
->bit_size
= length
* char_type
->bit_size
;
109 sym
->ctype
.alignment
= 1;
111 sym
->ctype
.modifiers
= MOD_STATIC
;
112 sym
->ctype
.base_type
= array
;
113 sym
->initializer
= initstr
;
117 initstr
->ctype
= sym
;
118 initstr
->string
= expr
->string
;
120 array
->array_size
= sym
->array_size
;
121 array
->bit_size
= sym
->bit_size
;
122 array
->ctype
.alignment
= char_type
->ctype
.alignment
;
123 array
->ctype
.modifiers
= MOD_STATIC
;
124 array
->ctype
.base_type
= char_type
;
126 array
->evaluated
= 1;
129 addr
->ctype
= &lazy_ptr_ctype
;
130 addr
->flags
= CEF_ADDR
;
132 expr
->type
= EXPR_PREOP
;
139 /* type has come from classify_type and is an integer type */
140 static inline struct symbol
*integer_promotion(struct symbol
*type
)
142 unsigned long mod
= type
->ctype
.modifiers
;
143 int width
= type
->bit_size
;
146 * Bitfields always promote to the base type,
147 * even if the bitfield might be bigger than
150 if (type
->type
== SYM_BITFIELD
) {
151 type
= type
->ctype
.base_type
;
153 mod
= type
->ctype
.modifiers
;
154 if (width
< bits_in_int
)
157 /* If char/short has as many bits as int, it still gets "promoted" */
158 if (type
->rank
< 0) {
159 if (mod
& MOD_UNSIGNED
)
167 * integer part of usual arithmetic conversions:
168 * integer promotions are applied
169 * if left and right are identical, we are done
170 * if signedness is the same, convert one with lower rank
171 * unless unsigned argument has rank lower than signed one, convert the
173 * if signed argument is bigger than unsigned one, convert the unsigned.
174 * otherwise, convert signed.
176 * Leaving aside the integer promotions, that is equivalent to
177 * if identical, don't convert
178 * if left is bigger than right, convert right
179 * if right is bigger than left, convert right
180 * otherwise, if signedness is the same, convert one with lower rank
181 * otherwise convert the signed one.
183 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
185 unsigned long lmod
, rmod
;
187 left
= integer_promotion(left
);
188 right
= integer_promotion(right
);
193 if (left
->bit_size
> right
->bit_size
)
196 if (right
->bit_size
> left
->bit_size
)
199 lmod
= left
->ctype
.modifiers
;
200 rmod
= right
->ctype
.modifiers
;
201 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
202 if (lmod
& MOD_UNSIGNED
)
204 } else if (left
->rank
> right
->rank
)
212 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
214 return orig
->bit_size
== new->bit_size
&&
215 orig
->bit_offset
== new->bit_offset
;
218 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, struct ident
**asp
)
220 unsigned long mod
= 0;
221 struct ident
*as
= NULL
;
224 mod
|= node
->ctype
.modifiers
;
225 combine_address_space(node
->pos
, &as
, node
->ctype
.as
);
226 if (node
->type
== SYM_NODE
) {
227 node
= node
->ctype
.base_type
;
232 *modp
= mod
& ~MOD_IGNORE
;
237 static int is_same_type(struct expression
*expr
, struct symbol
*new)
239 struct symbol
*old
= expr
->ctype
;
240 unsigned long oldmod
, newmod
;
241 struct ident
*oldas
, *newas
;
243 old
= base_type(old
, &oldmod
, &oldas
);
244 new = base_type(new, &newmod
, &newas
);
246 /* Same base type, same address space? */
247 if (old
== new && oldas
== newas
) {
248 unsigned long difmod
;
250 /* Check the modifier bits. */
251 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
253 /* Exact same type? */
258 * Not the same type, but differs only in "const".
259 * Don't warn about MOD_NOCAST.
261 if (difmod
== MOD_CONST
)
264 if ((oldmod
| newmod
) & MOD_NOCAST
) {
265 const char *tofrom
= "to/from";
266 if (!(newmod
& MOD_NOCAST
))
268 if (!(oldmod
& MOD_NOCAST
))
270 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
276 warn_for_different_enum_types (struct position pos
,
277 struct symbol
*typea
,
278 struct symbol
*typeb
)
282 if (typea
->type
== SYM_NODE
)
283 typea
= typea
->ctype
.base_type
;
284 if (typeb
->type
== SYM_NODE
)
285 typeb
= typeb
->ctype
.base_type
;
290 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
291 warning(pos
, "mixing different enum types:");
292 info(pos
, " %s", show_typename(typea
));
293 info(pos
, " %s", show_typename(typeb
));
297 static int cast_flags(struct expression
*expr
, struct expression
*target
);
298 static struct symbol
*cast_to_bool(struct expression
*expr
);
301 * This gets called for implicit casts in assignments and
302 * integer promotion. We often want to try to move the
303 * cast down, because the ops involved may have been
304 * implicitly cast up, and we can get rid of the casts
307 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
309 struct expression
*expr
;
311 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
313 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
317 * See if we can simplify the op. Move the cast down.
321 if (old
->ctype
->bit_size
< type
->bit_size
)
323 if (old
->op
== '~') {
325 old
->unop
= cast_to(old
->unop
, type
);
330 case EXPR_IMPLIED_CAST
:
331 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
333 if (old
->ctype
->bit_size
>= type
->bit_size
) {
334 struct expression
*orig
= old
->cast_expression
;
335 if (same_cast_type(orig
->ctype
, type
))
337 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
339 old
->cast_type
= type
;
349 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
351 expr
->cast_type
= type
;
352 expr
->cast_expression
= old
;
353 expr
->flags
= cast_flags(expr
, old
);
355 if (is_bool_type(type
))
372 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
374 static int type_class
[SYM_BAD
+ 1] = {
375 [SYM_PTR
] = TYPE_PTR
,
376 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
377 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
378 [SYM_STRUCT
] = TYPE_COMPOUND
,
379 [SYM_UNION
] = TYPE_COMPOUND
,
380 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
381 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
382 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
384 if (type
->type
== SYM_NODE
)
385 type
= type
->ctype
.base_type
;
386 if (type
->type
== SYM_TYPEOF
) {
387 type
= examine_symbol_type(type
);
388 if (type
->type
== SYM_NODE
)
389 type
= type
->ctype
.base_type
;
391 if (type
->type
== SYM_ENUM
)
392 type
= type
->ctype
.base_type
;
394 if (type
->type
== SYM_BASETYPE
) {
395 if (type
->ctype
.base_type
== &int_type
)
397 if (type
->ctype
.base_type
== &fp_type
)
398 return TYPE_NUM
| TYPE_FLOAT
;
400 return type_class
[type
->type
];
403 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
405 static inline int is_string_type(struct symbol
*type
)
407 if (type
->type
== SYM_NODE
)
408 type
= type
->ctype
.base_type
;
409 if (type
->type
!= SYM_ARRAY
)
411 type
= type
->ctype
.base_type
;
412 return is_byte_type(type
) || is_wchar_type(type
);
415 static struct symbol
*bad_expr_type(struct expression
*expr
)
417 switch (expr
->type
) {
420 if (!valid_subexpr_type(expr
))
422 sparse_error(expr
->pos
, "incompatible types for operation (%s):", show_special(expr
->op
));
423 info(expr
->pos
, " %s", show_typename(expr
->left
->ctype
));
424 info(expr
->pos
, " %s", show_typename(expr
->right
->ctype
));
428 if (!valid_expr_type(expr
->unop
))
430 sparse_error(expr
->pos
, "incompatible type for operation (%s):", show_special(expr
->op
));
431 info(expr
->pos
, " %s", show_typename(expr
->unop
->ctype
));
437 expr
->flags
= CEF_NONE
;
438 return expr
->ctype
= &bad_ctype
;
441 static int restricted_value(struct expression
*v
, struct symbol
*type
)
443 if (v
->type
!= EXPR_VALUE
)
450 static int restricted_binop(int op
, struct symbol
*type
)
455 case SPECIAL_AND_ASSIGN
:
456 case SPECIAL_OR_ASSIGN
:
457 case SPECIAL_XOR_ASSIGN
:
458 return 1; /* unfoul */
462 return 2; /* keep fouled */
464 case SPECIAL_NOTEQUAL
:
465 return 3; /* warn if fouled */
471 static int restricted_unop(int op
, struct symbol
**type
)
474 if ((*type
)->bit_size
< bits_in_int
)
475 *type
= befoul(*type
);
482 /* type should be SYM_FOULED */
483 static inline struct symbol
*unfoul(struct symbol
*type
)
485 return type
->ctype
.base_type
;
488 static struct symbol
*restricted_binop_type(int op
,
489 struct expression
*left
,
490 struct expression
*right
,
491 int lclass
, int rclass
,
492 struct symbol
*ltype
,
493 struct symbol
*rtype
)
495 struct symbol
*ctype
= NULL
;
496 if (lclass
& TYPE_RESTRICT
) {
497 if (rclass
& TYPE_RESTRICT
) {
498 if (ltype
== rtype
) {
500 } else if (lclass
& TYPE_FOULED
) {
501 if (unfoul(ltype
) == rtype
)
503 } else if (rclass
& TYPE_FOULED
) {
504 if (unfoul(rtype
) == ltype
)
508 if (!restricted_value(right
, ltype
))
511 } else if (!restricted_value(left
, rtype
))
515 switch (restricted_binop(op
, ctype
)) {
517 if ((lclass
^ rclass
) & TYPE_FOULED
)
518 ctype
= unfoul(ctype
);
521 if (!(lclass
& rclass
& TYPE_FOULED
))
533 static inline void unrestrict(struct expression
*expr
,
534 int class, struct symbol
**ctype
)
536 if (class & TYPE_RESTRICT
) {
537 if (class & TYPE_FOULED
)
538 *ctype
= unfoul(*ctype
);
539 warning(expr
->pos
, "%s degrades to integer",
540 show_typename(*ctype
));
541 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
545 static struct symbol
*usual_conversions(int op
,
546 struct expression
*left
,
547 struct expression
*right
,
548 int lclass
, int rclass
,
549 struct symbol
*ltype
,
550 struct symbol
*rtype
)
552 struct symbol
*ctype
;
554 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
556 if ((lclass
| rclass
) & TYPE_RESTRICT
)
560 if (!(lclass
& TYPE_FLOAT
)) {
561 if (!(rclass
& TYPE_FLOAT
))
562 return bigger_int_type(ltype
, rtype
);
565 } else if (rclass
& TYPE_FLOAT
) {
566 if (rtype
->rank
> ltype
->rank
)
574 ctype
= restricted_binop_type(op
, left
, right
,
575 lclass
, rclass
, ltype
, rtype
);
579 unrestrict(left
, lclass
, <ype
);
580 unrestrict(right
, rclass
, &rtype
);
585 static inline int lvalue_expression(struct expression
*expr
)
587 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
590 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
592 struct expression
*index
= expr
->right
;
593 struct symbol
*ctype
, *base
;
596 classify_type(degenerate(expr
->left
), &ctype
);
597 base
= examine_pointer_target(ctype
);
600 * An address constant +/- an integer constant expression
601 * yields an address constant again [6.6(7)].
603 if ((expr
->left
->flags
& CEF_ADDR
) && (expr
->right
->flags
& CEF_ICE
))
604 expr
->flags
= CEF_ADDR
;
607 expression_error(expr
, "missing type information");
610 if (is_function(base
)) {
611 expression_error(expr
, "arithmetics on pointers to functions");
615 /* Get the size of whatever the pointer points to */
616 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
618 if (ctype
== &null_ctype
)
622 if (multiply
== 1 && itype
->bit_size
== bits_in_pointer
)
625 if (index
->type
== EXPR_VALUE
) {
626 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
627 unsigned long long v
= index
->value
, mask
;
628 mask
= 1ULL << (itype
->bit_size
- 1);
634 mask
= 1ULL << (bits_in_pointer
- 1);
635 v
&= mask
| (mask
- 1);
637 val
->ctype
= ssize_t_ctype
;
642 if (itype
->bit_size
!= bits_in_pointer
)
643 index
= cast_to(index
, ssize_t_ctype
);
646 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
647 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
649 val
->ctype
= ssize_t_ctype
;
650 val
->value
= multiply
;
653 mul
->ctype
= ssize_t_ctype
;
663 static void examine_fn_arguments(struct symbol
*fn
);
665 #define MOD_IGN (MOD_QUALIFIER | MOD_FUN_ATTR)
667 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
668 unsigned long mod1
, unsigned long mod2
)
670 struct ident
*as1
= c1
->as
, *as2
= c2
->as
;
671 struct symbol
*t1
= c1
->base_type
;
672 struct symbol
*t2
= c2
->base_type
;
673 int move1
= 1, move2
= 1;
674 mod1
|= c1
->modifiers
;
675 mod2
|= c2
->modifiers
;
679 struct symbol
*base1
= t1
->ctype
.base_type
;
680 struct symbol
*base2
= t2
->ctype
.base_type
;
683 * FIXME! Collect alignment and context too here!
686 if (t1
&& t1
->type
!= SYM_PTR
) {
687 mod1
|= t1
->ctype
.modifiers
;
688 combine_address_space(t1
->pos
, &as1
, t1
->ctype
.as
);
694 if (t2
&& t2
->type
!= SYM_PTR
) {
695 mod2
|= t2
->ctype
.modifiers
;
696 combine_address_space(t2
->pos
, &as2
, t2
->ctype
.as
);
704 return "different types";
706 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
714 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
724 if (type
!= t2
->type
)
725 return "different base types";
729 sparse_error(t1
->pos
,
730 "internal error: bad type in derived(%d)",
734 return "different base types";
737 /* allow definition of incomplete structs and unions */
738 if (t1
->ident
== t2
->ident
)
740 return "different base types";
742 /* XXX: we ought to compare sizes */
746 return "different address spaces";
747 /* MOD_SPECIFIER is due to idiocy in parse.c */
748 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
749 return "different modifiers";
750 /* we could be lazier here */
751 base1
= examine_pointer_target(t1
);
752 base2
= examine_pointer_target(t2
);
753 mod1
= t1
->ctype
.modifiers
;
755 mod2
= t2
->ctype
.modifiers
;
759 struct symbol
*arg1
, *arg2
;
763 return "different address spaces";
764 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
765 return "different modifiers";
766 mod1
= t1
->ctype
.modifiers
;
768 mod2
= t2
->ctype
.modifiers
;
771 if (t1
->variadic
!= t2
->variadic
)
772 return "incompatible variadic arguments";
773 examine_fn_arguments(t1
);
774 examine_fn_arguments(t2
);
775 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
776 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
783 return "different argument counts";
784 diffstr
= type_difference(&arg1
->ctype
,
788 static char argdiff
[80];
789 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
796 FINISH_PTR_LIST(arg2
);
797 FINISH_PTR_LIST(arg1
);
802 return "different address spaces";
804 return "different base types";
805 if (t1
->rank
!= t2
->rank
)
806 return "different type sizes";
807 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
810 else if (diff
& ~MOD_SIGNEDNESS
)
811 return "different modifiers";
813 return "different signedness";
819 return "different address spaces";
820 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
821 return "different modifiers";
825 static void bad_null(struct expression
*expr
)
827 if (Wnon_pointer_null
)
828 warning(expr
->pos
, "Using plain integer as NULL pointer");
831 static unsigned long target_qualifiers(struct symbol
*type
)
833 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
834 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
839 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
841 const char *typediff
;
842 struct symbol
*ltype
, *rtype
;
843 struct expression
*l
= expr
->left
;
844 struct expression
*r
= expr
->right
;
845 struct symbol
*lbase
;
847 classify_type(degenerate(l
), <ype
);
848 classify_type(degenerate(r
), &rtype
);
850 lbase
= examine_pointer_target(ltype
);
851 examine_pointer_target(rtype
);
852 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
853 target_qualifiers(rtype
),
854 target_qualifiers(ltype
));
856 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
858 if (is_function(lbase
)) {
859 expression_error(expr
, "subtraction of functions? Share your drugs");
863 expr
->ctype
= ssize_t_ctype
;
864 if (lbase
->bit_size
> bits_in_char
) {
865 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
866 struct expression
*div
= expr
;
867 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
868 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
870 val
->ctype
= size_t_ctype
;
873 if (value
& (value
-1)) {
874 if (Wptr_subtraction_blows
) {
875 warning(expr
->pos
, "potentially expensive pointer subtraction");
876 info(expr
->pos
, " '%s' has a non-power-of-2 size: %lu", show_typename(lbase
), value
);
881 sub
->ctype
= ssize_t_ctype
;
890 return ssize_t_ctype
;
893 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
895 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
897 struct symbol
*ctype
;
902 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
903 warning(expr
->pos
, "assignment expression in conditional");
905 ctype
= evaluate_expression(expr
);
906 if (!valid_type(ctype
))
908 if (is_safe_type(ctype
))
909 warning(expr
->pos
, "testing a 'safe expression'");
910 if (is_func_type(ctype
)) {
912 warning(expr
->pos
, "the address of %s will always evaluate as true", "a function");
913 } else if (is_array_type(ctype
)) {
915 warning(expr
->pos
, "the address of %s will always evaluate as true", "an array");
916 } else if (!is_scalar_type(ctype
)) {
917 sparse_error(expr
->pos
, "non-scalar type in conditional:");
918 info(expr
->pos
, " %s", show_typename(ctype
));
922 ctype
= degenerate(expr
);
926 static struct symbol
*evaluate_logical(struct expression
*expr
)
928 if (!evaluate_conditional(expr
->left
, 0))
930 if (!evaluate_conditional(expr
->right
, 0))
933 /* the result is int [6.5.13(3), 6.5.14(3)] */
934 expr
->ctype
= &int_ctype
;
935 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
936 expr
->flags
&= ~(CEF_CONST_MASK
| CEF_ADDR
);
940 static struct symbol
*evaluate_binop(struct expression
*expr
)
942 struct symbol
*ltype
, *rtype
, *ctype
;
943 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
944 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
947 /* number op number */
948 if (lclass
& rclass
& TYPE_NUM
) {
949 expr
->flags
= expr
->left
->flags
& expr
->right
->flags
;
950 expr
->flags
&= ~CEF_CONST_MASK
;
952 if ((lclass
| rclass
) & TYPE_FLOAT
) {
954 case '+': case '-': case '*': case '/':
957 return bad_expr_type(expr
);
961 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
962 // shifts do integer promotions, but that's it.
963 unrestrict(expr
->left
, lclass
, <ype
);
964 unrestrict(expr
->right
, rclass
, &rtype
);
965 ctype
= ltype
= integer_promotion(ltype
);
966 rtype
= integer_promotion(rtype
);
968 // The rest do usual conversions
969 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
970 && expr
->left
->op
== '!';
971 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
972 && expr
->right
->op
== '!';
973 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
974 warning(expr
->pos
, "dubious: %sx %c %sy",
977 right_not
? "!" : "");
979 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
980 lclass
, rclass
, ltype
, rtype
);
981 ctype
= rtype
= ltype
;
984 expr
->left
= cast_to(expr
->left
, ltype
);
985 expr
->right
= cast_to(expr
->right
, rtype
);
990 /* pointer (+|-) integer */
991 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
992 unrestrict(expr
->right
, rclass
, &rtype
);
993 return evaluate_ptr_add(expr
, rtype
);
996 /* integer + pointer */
997 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
998 struct expression
*index
= expr
->left
;
999 unrestrict(index
, lclass
, <ype
);
1000 expr
->left
= expr
->right
;
1001 expr
->right
= index
;
1002 return evaluate_ptr_add(expr
, ltype
);
1005 /* pointer - pointer */
1006 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
1007 return evaluate_ptr_sub(expr
);
1009 return bad_expr_type(expr
);
1012 static struct symbol
*evaluate_comma(struct expression
*expr
)
1014 expr
->ctype
= degenerate(expr
->right
);
1015 if (expr
->ctype
== &null_ctype
)
1016 expr
->ctype
= &ptr_ctype
;
1017 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
1021 static int modify_for_unsigned(int op
)
1024 op
= SPECIAL_UNSIGNED_LT
;
1026 op
= SPECIAL_UNSIGNED_GT
;
1027 else if (op
== SPECIAL_LTE
)
1028 op
= SPECIAL_UNSIGNED_LTE
;
1029 else if (op
== SPECIAL_GTE
)
1030 op
= SPECIAL_UNSIGNED_GTE
;
1034 enum null_constant_type
{
1040 static inline int is_null_pointer_constant(struct expression
*e
)
1042 if (e
->ctype
== &null_ctype
)
1044 if (!(e
->flags
& CEF_ICE
))
1046 return is_zero_constant(e
) ? NULL_ZERO
: NON_NULL
;
1049 static struct symbol
*evaluate_compare(struct expression
*expr
)
1051 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1052 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
1053 int lclass
= classify_type(degenerate(left
), <ype
);
1054 int rclass
= classify_type(degenerate(right
), &rtype
);
1055 struct symbol
*ctype
;
1056 const char *typediff
;
1059 if (is_type_type(ltype
) && is_type_type(rtype
)) {
1061 * __builtin_types_compatible_p() yields an integer
1062 * constant expression
1064 expr
->flags
= CEF_SET_ICE
;
1068 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1069 warning(expr
->pos
, "testing a 'safe expression'");
1071 expr
->flags
= left
->flags
& right
->flags
& ~CEF_CONST_MASK
& ~CEF_ADDR
;
1073 /* number on number */
1074 if (lclass
& rclass
& TYPE_NUM
) {
1075 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1076 lclass
, rclass
, ltype
, rtype
);
1077 expr
->left
= cast_to(expr
->left
, ctype
);
1078 expr
->right
= cast_to(expr
->right
, ctype
);
1079 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1080 expr
->op
= modify_for_unsigned(expr
->op
);
1084 /* at least one must be a pointer */
1085 if (!((lclass
| rclass
) & TYPE_PTR
))
1086 return bad_expr_type(expr
);
1088 /* equality comparisons can be with null pointer constants */
1089 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1090 int is_null1
= is_null_pointer_constant(left
);
1091 int is_null2
= is_null_pointer_constant(right
);
1092 if (is_null1
== NULL_ZERO
)
1094 if (is_null2
== NULL_ZERO
)
1096 if (is_null1
&& is_null2
) {
1097 int positive
= expr
->op
== SPECIAL_EQUAL
;
1098 expr
->type
= EXPR_VALUE
;
1099 expr
->value
= positive
;
1102 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1103 expr
->left
= cast_to(left
, rtype
);
1106 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1107 expr
->right
= cast_to(right
, ltype
);
1111 /* both should be pointers */
1112 if (!(lclass
& rclass
& TYPE_PTR
))
1113 return bad_expr_type(expr
);
1114 expr
->op
= modify_for_unsigned(expr
->op
);
1116 lbase
= examine_pointer_target(ltype
);
1117 rbase
= examine_pointer_target(rtype
);
1119 /* they also have special treatment for pointers to void */
1120 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1121 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1122 if (lbase
== &void_ctype
) {
1123 expr
->right
= cast_to(right
, ltype
);
1126 if (rbase
== &void_ctype
) {
1127 expr
->left
= cast_to(left
, rtype
);
1133 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1134 target_qualifiers(rtype
),
1135 target_qualifiers(ltype
));
1139 expression_error(expr
, "incompatible types in comparison expression (%s):", typediff
);
1140 info(expr
->pos
, " %s", show_typename(ltype
));
1141 info(expr
->pos
, " %s", show_typename(rtype
));
1145 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1146 expr
->ctype
= &int_ctype
;
1151 * NOTE! The degenerate case of "x ? : y", where we don't
1152 * have a true case, this will possibly promote "x" to the
1153 * same type as "y", and thus _change_ the conditional
1154 * test in the expression. But since promotion is "safe"
1155 * for testing, that's OK.
1157 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1159 struct expression
**cond
;
1160 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1162 const char * typediff
;
1165 if (!evaluate_conditional(expr
->conditional
, 0))
1167 if (!evaluate_expression(expr
->cond_false
))
1170 ctype
= degenerate(expr
->conditional
);
1171 rtype
= degenerate(expr
->cond_false
);
1173 cond
= &expr
->conditional
;
1175 if (expr
->cond_true
) {
1176 if (!evaluate_expression(expr
->cond_true
))
1178 ltype
= degenerate(expr
->cond_true
);
1179 cond
= &expr
->cond_true
;
1182 expr
->flags
= (expr
->conditional
->flags
& (*cond
)->flags
&
1183 expr
->cond_false
->flags
& ~CEF_CONST_MASK
);
1185 * In the standard, it is defined that an integer constant expression
1186 * shall only have operands that are themselves constant [6.6(6)].
1187 * While this definition is very clear for expressions that need all
1188 * their operands to be evaluated, for conditional expressions with a
1189 * constant condition things are much less obvious.
1190 * So, as an extension, do the same as GCC seems to do:
1191 * Consider a conditional expression with a constant condition
1192 * as having the same constantness as the argument corresponding
1193 * to the truth value (including in the case of address constants
1194 * which are defined more stricly [6.6(9)]).
1196 if (expr
->conditional
->flags
& (CEF_ACE
| CEF_ADDR
)) {
1197 int is_true
= expr_truth_value(expr
->conditional
);
1198 struct expression
*arg
= is_true
? *cond
: expr
->cond_false
;
1199 expr
->flags
= arg
->flags
& ~CEF_CONST_MASK
;
1202 lclass
= classify_type(ltype
, <ype
);
1203 rclass
= classify_type(rtype
, &rtype
);
1204 if (lclass
& rclass
& TYPE_NUM
) {
1205 ctype
= usual_conversions('?', *cond
, expr
->cond_false
,
1206 lclass
, rclass
, ltype
, rtype
);
1207 *cond
= cast_to(*cond
, ctype
);
1208 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1212 if ((lclass
| rclass
) & TYPE_PTR
) {
1213 int is_null1
= is_null_pointer_constant(*cond
);
1214 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1216 if (is_null1
&& is_null2
) {
1217 *cond
= cast_to(*cond
, &ptr_ctype
);
1218 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1222 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1223 if (is_null1
== NULL_ZERO
)
1225 *cond
= cast_to(*cond
, rtype
);
1229 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1230 if (is_null2
== NULL_ZERO
)
1231 bad_null(expr
->cond_false
);
1232 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1236 if (!(lclass
& rclass
& TYPE_PTR
)) {
1237 typediff
= "different types";
1240 /* OK, it's pointer on pointer */
1241 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1242 typediff
= "different address spaces";
1246 /* need to be lazier here */
1247 lbase
= examine_pointer_target(ltype
);
1248 rbase
= examine_pointer_target(rtype
);
1249 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1251 if (lbase
== &void_ctype
) {
1252 /* XXX: pointers to function should warn here */
1257 if (rbase
== &void_ctype
) {
1258 /* XXX: pointers to function should warn here */
1262 /* XXX: that should be pointer to composite */
1264 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1271 /* void on void, struct on same struct, union on same union */
1272 if (ltype
== rtype
) {
1276 typediff
= "different base types";
1279 expression_error(expr
, "incompatible types in conditional expression (%s):", typediff
);
1280 info(expr
->pos
, " %s", show_typename(ltype
));
1281 info(expr
->pos
, " %s", show_typename(rtype
));
1283 * if the condition is constant, the type is in fact known
1284 * so use it, as gcc & clang do.
1286 switch (expr_truth_value(expr
->conditional
)) {
1287 case 1: expr
->ctype
= ltype
;
1289 case 0: expr
->ctype
= rtype
;
1297 expr
->ctype
= ctype
;
1301 if (qual
& ~ctype
->ctype
.modifiers
) {
1302 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1304 sym
->ctype
.modifiers
|= qual
;
1307 *cond
= cast_to(*cond
, ctype
);
1308 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1312 /* FP assignments can not do modulo or bit operations */
1313 static int compatible_float_op(int op
)
1315 return op
== SPECIAL_ADD_ASSIGN
||
1316 op
== SPECIAL_SUB_ASSIGN
||
1317 op
== SPECIAL_MUL_ASSIGN
||
1318 op
== SPECIAL_DIV_ASSIGN
;
1321 static int evaluate_assign_op(struct expression
*expr
)
1323 struct symbol
*target
= expr
->left
->ctype
;
1324 struct symbol
*source
= expr
->right
->ctype
;
1325 struct symbol
*t
, *s
;
1326 int tclass
= classify_type(target
, &t
);
1327 int sclass
= classify_type(source
, &s
);
1330 if (tclass
& sclass
& TYPE_NUM
) {
1331 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1332 expression_error(expr
, "invalid assignment");
1335 if (tclass
& TYPE_RESTRICT
) {
1336 if (!restricted_binop(op
, t
)) {
1337 warning(expr
->pos
, "bad assignment (%s) to %s",
1338 show_special(op
), show_typename(t
));
1339 expr
->right
= cast_to(expr
->right
, target
);
1342 /* allowed assignments unfoul */
1343 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1345 if (!restricted_value(expr
->right
, t
))
1347 } else if (op
== SPECIAL_SHR_ASSIGN
|| op
== SPECIAL_SHL_ASSIGN
) {
1348 // shifts do integer promotions, but that's it.
1349 unrestrict(expr
->left
, tclass
, &t
);
1350 target
= integer_promotion(t
);
1352 unrestrict(expr
->right
, sclass
, &s
);
1353 source
= integer_promotion(s
);
1354 expr
->right
= cast_to(expr
->right
, source
);
1356 // both gcc & clang seems to do this, so ...
1357 if (target
->bit_size
> source
->bit_size
)
1358 expr
->right
= cast_to(expr
->right
, &uint_ctype
);
1361 } else if (!(sclass
& TYPE_RESTRICT
))
1363 /* source and target would better be identical restricted */
1366 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1367 info(expr
->pos
, " left side has type %s", show_typename(t
));
1368 info(expr
->pos
, " right side has type %s", show_typename(s
));
1369 expr
->right
= cast_to(expr
->right
, target
);
1372 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1373 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1374 unrestrict(expr
->right
, sclass
, &s
);
1375 evaluate_ptr_add(expr
, s
);
1378 expression_error(expr
, "invalid pointer assignment");
1382 expression_error(expr
, "invalid assignment");
1386 target
= usual_conversions(op
, expr
->left
, expr
->right
,
1387 tclass
, sclass
, target
, source
);
1389 expr
->right
= cast_to(expr
->right
, target
);
1393 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1396 return 0; /* yes, 0 - we don't want a cast_to here */
1397 if (t1
== &void_ctype
)
1399 if (t2
== &void_ctype
)
1401 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1403 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1407 if (t1
->rank
== -2 && t2
->rank
== -2)
1409 if (t1
->rank
!= t2
->rank
)
1414 static int check_assignment_types(struct symbol
*target
, struct expression
**rp
,
1415 const char **typediff
)
1417 struct symbol
*source
= degenerate(*rp
);
1418 struct symbol
*t
, *s
;
1419 int tclass
= classify_type(target
, &t
);
1420 int sclass
= classify_type(source
, &s
);
1422 if (tclass
& sclass
& TYPE_NUM
) {
1423 if (tclass
& TYPE_RESTRICT
) {
1424 /* allowed assignments unfoul */
1425 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1427 if (!restricted_value(*rp
, target
))
1431 } else if (!(sclass
& TYPE_RESTRICT
))
1433 if (t
== &bool_ctype
) {
1434 if (is_fouled_type(s
))
1435 warning((*rp
)->pos
, "%s degrades to integer",
1436 show_typename(s
->ctype
.base_type
));
1439 *typediff
= "different base types";
1443 if (tclass
== TYPE_PTR
) {
1444 unsigned long mod1
, mod2
;
1445 unsigned long modl
, modr
;
1446 struct symbol
*b1
, *b2
;
1447 // NULL pointer is always OK
1448 int is_null
= is_null_pointer_constant(*rp
);
1450 if (is_null
== NULL_ZERO
)
1454 if (!(sclass
& TYPE_PTR
)) {
1455 *typediff
= "different base types";
1458 b1
= examine_pointer_target(t
);
1459 b2
= examine_pointer_target(s
);
1460 mod1
= t
->ctype
.modifiers
& MOD_IGN
;
1461 mod2
= s
->ctype
.modifiers
& MOD_IGN
;
1462 if (whitelist_pointers(b1
, b2
)) {
1464 * assignments to/from void * are OK, provided that
1465 * we do not remove qualifiers from pointed to [C]
1466 * or mix address spaces [sparse].
1468 if (t
->ctype
.as
!= s
->ctype
.as
) {
1469 *typediff
= "different address spaces";
1473 * If this is a function pointer assignment, it is
1474 * actually fine to assign a pointer to const data to
1475 * it, as a function pointer points to const data
1476 * implicitly, i.e., dereferencing it does not produce
1479 if (b1
->type
== SYM_FN
)
1481 if (mod2
& ~mod1
& ~MOD_FUN_ATTR
) {
1482 *typediff
= "different modifiers";
1487 /* It's OK if the target is more volatile or const than the source */
1488 /* It's OK if the source is more pure/noreturn than the target */
1489 modr
= mod1
& ~MOD_REV_QUAL
;
1490 modl
= mod2
& MOD_REV_QUAL
;
1491 *typediff
= type_difference(&t
->ctype
, &s
->ctype
, modl
, modr
);
1497 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1500 if (tclass
& TYPE_NUM
) {
1501 /* XXX: need to turn into comparison with NULL */
1502 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1504 *typediff
= "different base types";
1507 *typediff
= "invalid types";
1511 *rp
= cast_to(*rp
, target
);
1515 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1516 struct expression
**rp
, const char *where
)
1518 const char *typediff
;
1520 if (!check_assignment_types(target
, rp
, &typediff
)) {
1521 struct symbol
*source
= *rp
? (*rp
)->ctype
: NULL
;
1522 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1523 info(expr
->pos
, " expected %s", show_typename(target
));
1524 info(expr
->pos
, " got %s", show_typename(source
));
1525 *rp
= cast_to(*rp
, target
);
1532 static int compatible_transparent_union(struct symbol
*target
,
1533 struct expression
**rp
)
1535 struct symbol
*t
, *member
;
1536 classify_type(target
, &t
);
1537 if (t
->type
!= SYM_UNION
|| !t
->transparent_union
)
1540 FOR_EACH_PTR(t
->symbol_list
, member
) {
1541 const char *typediff
;
1542 if (check_assignment_types(member
, rp
, &typediff
))
1544 } END_FOR_EACH_PTR(member
);
1549 static int compatible_argument_type(struct expression
*expr
, struct symbol
*target
,
1550 struct expression
**rp
, const char *where
)
1552 if (compatible_transparent_union(target
, rp
))
1555 return compatible_assignment_types(expr
, target
, rp
, where
);
1558 static void mark_addressable(struct expression
*expr
)
1560 while (expr
->type
== EXPR_BINOP
&& expr
->op
== '+')
1562 if (expr
->type
== EXPR_SYMBOL
) {
1563 struct symbol
*sym
= expr
->symbol
;
1564 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1568 static void mark_assigned(struct expression
*expr
)
1574 switch (expr
->type
) {
1579 if (sym
->type
!= SYM_NODE
)
1581 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1585 mark_assigned(expr
->left
);
1586 mark_assigned(expr
->right
);
1589 case EXPR_FORCE_CAST
:
1590 mark_assigned(expr
->cast_expression
);
1593 mark_assigned(expr
->base
);
1601 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1603 if (type
->ctype
.modifiers
& MOD_CONST
)
1604 expression_error(left
, "assignment to const expression");
1606 /* We know left is an lvalue, so it's a "preop-*" */
1607 mark_assigned(left
->unop
);
1610 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1612 struct expression
*left
= expr
->left
;
1613 struct symbol
*ltype
;
1615 if (!lvalue_expression(left
)) {
1616 expression_error(expr
, "not an lvalue");
1620 ltype
= left
->ctype
;
1622 if (expr
->op
!= '=') {
1623 if (!evaluate_assign_op(expr
))
1626 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, "assignment"))
1630 evaluate_assign_to(left
, ltype
);
1632 expr
->ctype
= ltype
;
1636 static void examine_fn_arguments(struct symbol
*fn
)
1640 FOR_EACH_PTR(fn
->arguments
, s
) {
1641 struct symbol
*arg
= evaluate_symbol(s
);
1642 /* Array/function arguments silently degenerate into pointers */
1648 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1649 if (arg
->type
== SYM_ARRAY
)
1650 ptr
->ctype
= arg
->ctype
;
1652 ptr
->ctype
.base_type
= arg
;
1653 combine_address_space(s
->pos
, &ptr
->ctype
.as
, s
->ctype
.as
);
1654 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1656 s
->ctype
.base_type
= ptr
;
1658 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1661 examine_symbol_type(s
);
1668 } END_FOR_EACH_PTR(s
);
1671 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, struct ident
*as
, int mod
)
1673 /* Take the modifiers of the pointer, and apply them to the member */
1674 mod
|= sym
->ctype
.modifiers
;
1675 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1676 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1678 newsym
->ctype
.as
= as
;
1679 newsym
->ctype
.modifiers
= mod
;
1685 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1687 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1688 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1690 node
->ctype
.base_type
= ptr
;
1691 ptr
->bit_size
= bits_in_pointer
;
1692 ptr
->ctype
.alignment
= pointer_alignment
;
1694 node
->bit_size
= bits_in_pointer
;
1695 node
->ctype
.alignment
= pointer_alignment
;
1698 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1699 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1700 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1702 if (sym
->type
== SYM_NODE
) {
1703 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1704 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1705 sym
= sym
->ctype
.base_type
;
1707 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1708 combine_address_space(sym
->pos
, &ptr
->ctype
.as
, sym
->ctype
.as
);
1709 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1710 sym
= sym
->ctype
.base_type
;
1712 ptr
->ctype
.base_type
= sym
;
1717 /* Arrays degenerate into pointers on pointer arithmetic */
1718 static struct symbol
*degenerate(struct expression
*expr
)
1720 struct symbol
*ctype
, *base
;
1724 ctype
= expr
->ctype
;
1727 base
= examine_symbol_type(ctype
);
1728 if (ctype
->type
== SYM_NODE
)
1729 base
= ctype
->ctype
.base_type
;
1731 * Arrays degenerate into pointers to the entries, while
1732 * functions degenerate into pointers to themselves.
1733 * If array was part of non-lvalue compound, we create a copy
1734 * of that compound first and then act as if we were dealing with
1735 * the corresponding field in there.
1737 switch (base
->type
) {
1739 if (expr
->type
== EXPR_SLICE
) {
1740 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1741 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1743 a
->ctype
.base_type
= expr
->base
->ctype
;
1744 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1745 a
->array_size
= expr
->base
->ctype
->array_size
;
1747 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1749 e0
->ctype
= &lazy_ptr_ctype
;
1751 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1754 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1756 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1758 e2
->right
= expr
->base
;
1760 e2
->ctype
= expr
->base
->ctype
;
1762 if (expr
->r_bitpos
) {
1763 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1766 e3
->right
= alloc_const_expression(expr
->pos
,
1767 bits_to_bytes(expr
->r_bitpos
));
1768 e3
->ctype
= &lazy_ptr_ctype
;
1773 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1776 e4
->ctype
= &lazy_ptr_ctype
;
1779 expr
->type
= EXPR_PREOP
;
1783 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1784 expression_error(expr
, "strange non-value function or array");
1787 *expr
= *expr
->unop
;
1788 ctype
= create_pointer(expr
, ctype
, 1);
1789 expr
->ctype
= ctype
;
1790 mark_addressable(expr
);
1797 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1799 struct expression
*op
= expr
->unop
;
1800 struct symbol
*ctype
;
1802 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1803 expression_error(expr
, "not addressable");
1809 mark_addressable(expr
);
1812 * symbol expression evaluation is lazy about the type
1813 * of the sub-expression, so we may have to generate
1814 * the type here if so..
1816 if (expr
->ctype
== &lazy_ptr_ctype
) {
1817 ctype
= create_pointer(expr
, ctype
, 0);
1818 expr
->ctype
= ctype
;
1824 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1826 struct expression
*op
= expr
->unop
;
1827 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1829 /* Simplify: *&(expr) => (expr) */
1830 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1832 expr
->flags
= CEF_NONE
;
1836 examine_symbol_type(ctype
);
1838 /* Dereferencing a node drops all the node information. */
1839 if (ctype
->type
== SYM_NODE
)
1840 ctype
= ctype
->ctype
.base_type
;
1842 target
= ctype
->ctype
.base_type
;
1844 switch (ctype
->type
) {
1846 expression_error(expr
, "cannot dereference this type");
1852 examine_symbol_type(target
);
1853 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1854 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1855 merge_type(node
, ctype
);
1859 if (!lvalue_expression(op
)) {
1860 expression_error(op
, "non-lvalue array??");
1864 /* Do the implied "addressof" on the array */
1868 * When an array is dereferenced, we need to pick
1869 * up the attributes of the original node too..
1871 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1872 merge_type(node
, op
->ctype
);
1873 merge_type(node
, ctype
);
1877 node
->bit_size
= target
->bit_size
;
1878 node
->array_size
= target
->array_size
;
1885 * Unary post-ops: x++ and x--
1887 static struct symbol
*evaluate_postop(struct expression
*expr
)
1889 struct expression
*op
= expr
->unop
;
1890 struct symbol
*ctype
= op
->ctype
;
1891 int class = classify_type(ctype
, &ctype
);
1894 if (!class || class & TYPE_COMPOUND
) {
1895 expression_error(expr
, "need scalar for ++/--");
1898 if (!lvalue_expression(expr
->unop
)) {
1899 expression_error(expr
, "need lvalue expression for ++/--");
1903 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1904 unrestrict(expr
, class, &ctype
);
1906 if (class & TYPE_NUM
) {
1908 } else if (class == TYPE_PTR
) {
1909 struct symbol
*target
= examine_pointer_target(ctype
);
1910 if (!is_function(target
))
1911 multiply
= bits_to_bytes(target
->bit_size
);
1915 evaluate_assign_to(op
, op
->ctype
);
1916 expr
->op_value
= multiply
;
1917 expr
->ctype
= ctype
;
1921 expression_error(expr
, "bad argument type for ++/--");
1925 static struct symbol
*evaluate_sign(struct expression
*expr
)
1927 struct symbol
*ctype
= expr
->unop
->ctype
;
1928 int class = classify_type(ctype
, &ctype
);
1929 unsigned char flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1931 /* should be an arithmetic type */
1932 if (!(class & TYPE_NUM
))
1933 return bad_expr_type(expr
);
1934 if (class & TYPE_RESTRICT
)
1937 if (!(class & TYPE_FLOAT
)) {
1938 ctype
= integer_promotion(ctype
);
1939 expr
->unop
= cast_to(expr
->unop
, ctype
);
1940 } else if (expr
->op
!= '~') {
1941 /* no conversions needed */
1943 return bad_expr_type(expr
);
1945 if (expr
->op
== '+')
1946 *expr
= *expr
->unop
;
1947 expr
->flags
= flags
;
1948 expr
->ctype
= ctype
;
1951 if (restricted_unop(expr
->op
, &ctype
))
1952 unrestrict(expr
, class, &ctype
);
1956 static struct symbol
*evaluate_preop(struct expression
*expr
)
1958 struct symbol
*ctype
= expr
->unop
->ctype
;
1962 *expr
= *expr
->unop
;
1968 return evaluate_sign(expr
);
1971 return evaluate_dereference(expr
);
1974 return evaluate_addressof(expr
);
1976 case SPECIAL_INCREMENT
:
1977 case SPECIAL_DECREMENT
:
1979 * From a type evaluation standpoint the preops are
1980 * the same as the postops
1982 return evaluate_postop(expr
);
1985 ctype
= degenerate(expr
->unop
);
1986 expr
->flags
= expr
->unop
->flags
& ~CEF_CONST_MASK
;
1988 * A logical negation never yields an address constant
1991 expr
->flags
&= ~CEF_ADDR
;
1993 if (is_safe_type(ctype
))
1994 warning(expr
->pos
, "testing a 'safe expression'");
1995 if (is_float_type(ctype
)) {
1996 struct expression
*arg
= expr
->unop
;
1997 expr
->type
= EXPR_COMPARE
;
1998 expr
->op
= SPECIAL_EQUAL
;
2000 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
2001 expr
->right
->ctype
= ctype
;
2002 expr
->right
->fvalue
= 0;
2003 } else if (is_fouled_type(ctype
)) {
2004 warning(expr
->pos
, "%s degrades to integer",
2005 show_typename(ctype
->ctype
.base_type
));
2007 /* the result is int [6.5.3.3(5)]*/
2014 expr
->ctype
= ctype
;
2018 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
2020 struct ptr_list
*head
= (struct ptr_list
*)_list
;
2021 struct ptr_list
*list
= head
;
2027 for (i
= 0; i
< list
->nr
; i
++) {
2028 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
2030 if (sym
->ident
!= ident
)
2032 *offset
= sym
->offset
;
2035 struct symbol
*ctype
= sym
->ctype
.base_type
;
2039 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
2041 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
2044 *offset
+= sym
->offset
;
2048 } while ((list
= list
->next
) != head
);
2052 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
2054 struct expression
*add
;
2057 * Create a new add-expression
2059 * NOTE! Even if we just add zero, we need a new node
2060 * for the member pointer, since it has a different
2061 * type than the original pointer. We could make that
2062 * be just a cast, but the fact is, a node is a node,
2063 * so we might as well just do the "add zero" here.
2065 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
2068 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2069 add
->right
->ctype
= &int_ctype
;
2070 add
->right
->value
= offset
;
2073 * The ctype of the pointer will be lazily evaluated if
2074 * we ever take the address of this member dereference..
2076 add
->ctype
= &lazy_ptr_ctype
;
2078 * The resulting address of a member access through an address
2079 * constant is an address constant again [6.6(9)].
2081 add
->flags
= expr
->flags
;
2086 /* structure/union dereference */
2087 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
2090 struct symbol
*ctype
, *member
;
2091 struct expression
*deref
= expr
->deref
, *add
;
2092 struct ident
*ident
= expr
->member
;
2093 struct ident
*address_space
;
2096 if (!evaluate_expression(deref
))
2099 expression_error(expr
, "bad member name");
2103 ctype
= deref
->ctype
;
2104 examine_symbol_type(ctype
);
2105 address_space
= ctype
->ctype
.as
;
2106 mod
= ctype
->ctype
.modifiers
;
2107 if (ctype
->type
== SYM_NODE
) {
2108 ctype
= ctype
->ctype
.base_type
;
2109 combine_address_space(deref
->pos
, &address_space
, ctype
->ctype
.as
);
2110 mod
|= ctype
->ctype
.modifiers
;
2112 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
2113 expression_error(expr
, "expected structure or union");
2117 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
2119 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
2120 const char *name
= "<unnamed>";
2123 name
= ctype
->ident
->name
;
2124 namelen
= ctype
->ident
->len
;
2126 if (ctype
->symbol_list
)
2127 expression_error(expr
, "no member '%s' in %s %.*s",
2128 show_ident(ident
), type
, namelen
, name
);
2130 expression_error(expr
, "using member '%s' in "
2131 "incomplete %s %.*s", show_ident(ident
),
2132 type
, namelen
, name
);
2137 * The member needs to take on the address space and modifiers of
2138 * the "parent" type.
2140 member
= convert_to_as_mod(member
, address_space
, mod
);
2141 ctype
= get_base_type(member
);
2143 if (!lvalue_expression(deref
)) {
2144 if (deref
->type
!= EXPR_SLICE
) {
2148 expr
->base
= deref
->base
;
2149 expr
->r_bitpos
= deref
->r_bitpos
;
2151 expr
->r_bitpos
+= bytes_to_bits(offset
);
2152 expr
->type
= EXPR_SLICE
;
2153 expr
->r_nrbits
= member
->bit_size
;
2154 expr
->r_bitpos
+= member
->bit_offset
;
2155 expr
->ctype
= member
;
2159 deref
= deref
->unop
;
2160 expr
->deref
= deref
;
2162 add
= evaluate_offset(deref
, offset
);
2163 expr
->type
= EXPR_PREOP
;
2167 expr
->ctype
= member
;
2171 static int is_promoted(struct expression
*expr
)
2174 switch (expr
->type
) {
2177 case EXPR_CONDITIONAL
:
2201 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2203 struct symbol
*sym
= expr
->cast_type
;
2205 sym
= evaluate_expression(expr
->cast_expression
);
2209 * Expressions of restricted types will possibly get
2210 * promoted - check that here
2212 if (is_restricted_type(sym
)) {
2213 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2215 } else if (is_fouled_type(sym
)) {
2219 examine_symbol_type(sym
);
2220 if (is_bitfield_type(sym
)) {
2221 expression_error(expr
, "trying to examine bitfield type");
2227 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2229 struct symbol
*type
;
2232 type
= evaluate_type_information(expr
);
2236 size
= type
->bit_size
;
2238 if (size
< 0 && is_void_type(type
)) {
2240 warning(expr
->pos
, "expression using sizeof(void)");
2241 size
= bits_in_char
;
2244 if (is_bool_type(type
)) {
2246 warning(expr
->pos
, "expression using sizeof _Bool");
2247 size
= bits_to_bytes(bits_in_bool
) * bits_in_char
;
2250 if (is_function(type
->ctype
.base_type
)) {
2252 warning(expr
->pos
, "expression using sizeof on a function");
2253 size
= bits_in_char
;
2256 if (is_array_type(type
) && size
< 0) { // VLA, 1-dimension only
2257 struct expression
*base
, *size
;
2258 struct symbol
*base_type
;
2260 if (type
->type
== SYM_NODE
)
2261 type
= type
->ctype
.base_type
; // strip the SYM_NODE
2262 base_type
= get_base_type(type
);
2265 if (base_type
->bit_size
<= 0) {
2266 base
= alloc_expression(expr
->pos
, EXPR_SIZEOF
);
2267 base
->cast_type
= base_type
;
2268 if (!evaluate_sizeof(base
))
2271 base
= alloc_expression(expr
->pos
, EXPR_VALUE
);
2272 base
->value
= bits_to_bytes(base_type
->bit_size
);
2273 base
->ctype
= size_t_ctype
;
2275 size
= alloc_expression(expr
->pos
, EXPR_CAST
);
2276 size
->cast_type
= size_t_ctype
;
2277 size
->cast_expression
= type
->array_size
;
2278 if (!evaluate_expression(size
))
2282 expr
->type
= EXPR_BINOP
;
2284 return expr
->ctype
= size_t_ctype
;
2288 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2289 expression_error(expr
, "cannot size expression");
2291 expr
->type
= EXPR_VALUE
;
2292 expr
->value
= bits_to_bytes(size
);
2294 expr
->ctype
= size_t_ctype
;
2295 return size_t_ctype
;
2298 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2300 struct symbol
*type
;
2303 type
= evaluate_type_information(expr
);
2307 if (type
->type
== SYM_NODE
)
2308 type
= type
->ctype
.base_type
;
2311 switch (type
->type
) {
2315 type
= get_base_type(type
);
2319 expression_error(expr
, "expected pointer expression");
2322 size
= type
->bit_size
;
2323 if (size
& (bits_in_char
-1))
2325 expr
->type
= EXPR_VALUE
;
2326 expr
->value
= bits_to_bytes(size
);
2328 expr
->ctype
= size_t_ctype
;
2329 return size_t_ctype
;
2332 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2334 struct symbol
*type
;
2336 type
= evaluate_type_information(expr
);
2340 expr
->type
= EXPR_VALUE
;
2341 expr
->value
= type
->ctype
.alignment
;
2343 expr
->ctype
= size_t_ctype
;
2344 return size_t_ctype
;
2347 int evaluate_arguments(struct symbol_list
*argtypes
, struct expression_list
*head
)
2349 struct expression
*expr
;
2350 struct symbol
*argtype
;
2353 PREPARE_PTR_LIST(argtypes
, argtype
);
2354 FOR_EACH_PTR (head
, expr
) {
2355 struct expression
**p
= THIS_ADDRESS(expr
);
2356 struct symbol
*ctype
, *target
;
2357 ctype
= evaluate_expression(expr
);
2364 struct symbol
*type
;
2365 int class = classify_type(ctype
, &type
);
2366 if (is_int(class)) {
2367 *p
= cast_to(expr
, integer_promotion(type
));
2368 } else if (class & TYPE_FLOAT
) {
2370 *p
= cast_to(expr
, &double_ctype
);
2371 } else if (class & TYPE_PTR
) {
2372 if (expr
->ctype
== &null_ctype
)
2373 *p
= cast_to(expr
, &ptr_ctype
);
2377 } else if (!target
->forced_arg
){
2378 static char where
[30];
2379 examine_symbol_type(target
);
2380 sprintf(where
, "argument %d", i
);
2381 compatible_argument_type(expr
, target
, p
, where
);
2385 NEXT_PTR_LIST(argtype
);
2386 } END_FOR_EACH_PTR(expr
);
2387 FINISH_PTR_LIST(argtype
);
2391 static void convert_index(struct expression
*e
)
2393 struct expression
*child
= e
->idx_expression
;
2394 unsigned from
= e
->idx_from
;
2395 unsigned to
= e
->idx_to
+ 1;
2397 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2398 e
->init_nr
= to
- from
;
2399 e
->init_expr
= child
;
2402 static void convert_ident(struct expression
*e
)
2404 struct expression
*child
= e
->ident_expression
;
2405 int offset
= e
->offset
;
2408 e
->init_offset
= offset
;
2410 e
->init_expr
= child
;
2413 static void convert_designators(struct expression
*e
)
2416 if (e
->type
== EXPR_INDEX
)
2418 else if (e
->type
== EXPR_IDENTIFIER
)
2426 static void excess(struct expression
*e
, const char *s
)
2428 warning(e
->pos
, "excessive elements in %s initializer", s
);
2432 * implicit designator for the first element
2434 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2435 struct expression
**v
)
2437 struct expression
*e
= *v
, *new;
2439 if (ctype
->type
== SYM_NODE
)
2440 ctype
= ctype
->ctype
.base_type
;
2442 if (class & TYPE_PTR
) { /* array */
2443 if (!ctype
->bit_size
)
2445 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2446 new->idx_expression
= e
;
2447 new->ctype
= ctype
->ctype
.base_type
;
2449 struct symbol
*field
, *p
;
2450 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2451 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2457 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2458 new->ident_expression
= e
;
2459 new->field
= new->ctype
= field
;
2460 new->offset
= field
->offset
;
2467 * sanity-check explicit designators; return the innermost one or NULL
2468 * in case of error. Assign types.
2470 static struct expression
*check_designators(struct expression
*e
,
2471 struct symbol
*ctype
)
2473 struct expression
*last
= NULL
;
2476 if (ctype
->type
== SYM_NODE
)
2477 ctype
= ctype
->ctype
.base_type
;
2478 if (e
->type
== EXPR_INDEX
) {
2479 struct symbol
*type
;
2480 if (ctype
->type
!= SYM_ARRAY
) {
2481 err
= "array index in non-array";
2484 type
= ctype
->ctype
.base_type
;
2485 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2486 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2487 if (offset
>= ctype
->bit_size
) {
2488 err
= "index out of bounds in";
2492 e
->ctype
= ctype
= type
;
2495 if (!e
->idx_expression
) {
2499 e
= e
->idx_expression
;
2500 } else if (e
->type
== EXPR_IDENTIFIER
) {
2502 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2503 err
= "field name not in struct or union";
2506 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2508 err
= "unknown field name in";
2512 e
->field
= e
->ctype
= ctype
;
2514 if (!e
->ident_expression
) {
2518 e
= e
->ident_expression
;
2519 } else if (e
->type
== EXPR_POS
) {
2520 err
= "internal front-end error: EXPR_POS in";
2525 expression_error(e
, "%s initializer", err
);
2530 * choose the next subobject to initialize.
2532 * Get designators for next element, switch old ones to EXPR_POS.
2533 * Return the resulting expression or NULL if we'd run out of subobjects.
2534 * The innermost designator is returned in *v. Designators in old
2535 * are assumed to be already sanity-checked.
2537 static struct expression
*next_designators(struct expression
*old
,
2538 struct symbol
*ctype
,
2539 struct expression
*e
, struct expression
**v
)
2541 struct expression
*new = NULL
;
2545 if (old
->type
== EXPR_INDEX
) {
2546 struct expression
*copy
;
2549 copy
= next_designators(old
->idx_expression
,
2552 n
= old
->idx_to
+ 1;
2553 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2558 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2561 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2564 new->idx_from
= new->idx_to
= n
;
2565 new->idx_expression
= copy
;
2566 new->ctype
= old
->ctype
;
2568 } else if (old
->type
== EXPR_IDENTIFIER
) {
2569 struct expression
*copy
;
2570 struct symbol
*field
;
2573 copy
= next_designators(old
->ident_expression
,
2576 field
= old
->field
->next_subobject
;
2582 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2584 * We can't necessarily trust "field->offset",
2585 * because the field might be in an anonymous
2586 * union, and the field offset is then the offset
2587 * within that union.
2589 * The "old->offset - old->field->offset"
2590 * would be the offset of such an anonymous
2593 offset
= old
->offset
- old
->field
->offset
;
2596 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2600 new->expr_ident
= field
->ident
;
2601 new->ident_expression
= copy
;
2603 new->offset
= field
->offset
+ offset
;
2609 static int handle_initializer(struct expression
**ep
, int nested
,
2610 int class, struct symbol
*ctype
, unsigned long mods
);
2613 * deal with traversing subobjects [6.7.8(17,18,20)]
2615 static void handle_list_initializer(struct expression
*expr
,
2616 int class, struct symbol
*ctype
, unsigned long mods
)
2618 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2619 int jumped
= 0; // has the last designator multiple levels?
2621 if (expr
->zero_init
)
2622 free_ptr_list(&expr
->expr_list
);
2624 FOR_EACH_PTR(expr
->expr_list
, e
) {
2625 struct expression
**v
;
2626 struct symbol
*type
;
2629 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2630 struct symbol
*struct_sym
;
2633 last
= first_subobject(ctype
, class, &top
);
2635 last
= next_designators(last
, ctype
, e
, &top
);
2638 excess(e
, class & TYPE_PTR
? "array" :
2640 DELETE_CURRENT_PTR(e
);
2643 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2644 if (Wdesignated_init
&& struct_sym
->designated_init
)
2645 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2646 ctype
->ident
? "in initializer for " : "",
2647 ctype
->ident
? ctype
->ident
->len
: 0,
2648 ctype
->ident
? ctype
->ident
->name
: "",
2649 ctype
->ident
? ": " : "",
2650 get_type_name(struct_sym
->type
),
2651 show_ident(struct_sym
->ident
));
2652 if (jumped
&& Wpast_deep_designator
) {
2653 warning(e
->pos
, "advancing past deep designator");
2656 REPLACE_CURRENT_PTR(e
, last
);
2658 next
= check_designators(e
, ctype
);
2660 DELETE_CURRENT_PTR(e
);
2664 /* deeper than one designator? */
2666 convert_designators(last
);
2671 lclass
= classify_type(top
->ctype
, &type
);
2672 if (top
->type
== EXPR_INDEX
)
2673 v
= &top
->idx_expression
;
2675 v
= &top
->ident_expression
;
2677 mods
|= ctype
->ctype
.modifiers
& MOD_STORAGE
;
2678 if (handle_initializer(v
, 1, lclass
, top
->ctype
, mods
))
2681 if (!(lclass
& TYPE_COMPOUND
)) {
2682 warning(e
->pos
, "bogus scalar initializer");
2683 DELETE_CURRENT_PTR(e
);
2687 next
= first_subobject(type
, lclass
, v
);
2689 warning(e
->pos
, "missing braces around initializer");
2694 DELETE_CURRENT_PTR(e
);
2695 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2697 } END_FOR_EACH_PTR(e
);
2699 convert_designators(last
);
2700 expr
->ctype
= ctype
;
2703 static int is_string_literal(struct expression
**v
)
2705 struct expression
*e
= *v
;
2706 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2708 if (!e
|| e
->type
!= EXPR_STRING
)
2710 if (e
!= *v
&& Wparen_string
)
2712 "array initialized from parenthesized string constant");
2718 * We want a normal expression, possibly in one layer of braces. Warn
2719 * if the latter happens inside a list (it's legal, but likely to be
2720 * an effect of screwup). In case of anything not legal, we are definitely
2721 * having an effect of screwup, so just fail and let the caller warn.
2723 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2725 struct expression
*v
= NULL
, *p
;
2729 if (e
->type
!= EXPR_INITIALIZER
)
2732 FOR_EACH_PTR(e
->expr_list
, p
) {
2736 } END_FOR_EACH_PTR(p
);
2740 case EXPR_INITIALIZER
:
2742 case EXPR_IDENTIFIER
:
2748 warning(e
->pos
, "braces around scalar initializer");
2753 * deal with the cases that don't care about subobjects:
2754 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2755 * character array <- string literal, possibly in braces [6.7.8(14)]
2756 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2757 * compound type <- initializer list in braces [6.7.8(16)]
2758 * The last one punts to handle_list_initializer() which, in turn will call
2759 * us for individual elements of the list.
2761 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2762 * the lack of support of wide char stuff in general.
2764 * One note: we need to take care not to evaluate a string literal until
2765 * we know that we *will* handle it right here. Otherwise we would screw
2766 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2767 * { "string", ...} - we need to preserve that string literal recognizable
2768 * until we dig into the inner struct.
2770 static int handle_initializer(struct expression
**ep
, int nested
,
2771 int class, struct symbol
*ctype
, unsigned long mods
)
2773 struct expression
*e
= *ep
, *p
;
2774 struct symbol
*type
;
2780 if (!(class & TYPE_COMPOUND
)) {
2781 e
= handle_scalar(e
, nested
);
2785 if (!evaluate_expression(e
))
2787 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2789 * Initializers for static storage duration objects
2790 * shall be constant expressions or a string literal [6.7.8(4)].
2792 mods
|= ctype
->ctype
.modifiers
;
2793 mods
&= (MOD_TOPLEVEL
| MOD_STATIC
);
2794 if (mods
&& !(e
->flags
& (CEF_ACE
| CEF_ADDR
)))
2795 if (Wconstexpr_not_const
)
2796 warning(e
->pos
, "non-constant initializer for static object");
2802 * sublist; either a string, or we dig in; the latter will deal with
2803 * pathologies, so we don't need anything fancy here.
2805 if (e
->type
== EXPR_INITIALIZER
) {
2806 if (is_string_type(ctype
)) {
2807 struct expression
*v
= NULL
;
2810 FOR_EACH_PTR(e
->expr_list
, p
) {
2814 } END_FOR_EACH_PTR(p
);
2815 if (count
== 1 && is_string_literal(&v
)) {
2820 handle_list_initializer(e
, class, ctype
, mods
);
2825 if (is_string_literal(&e
)) {
2826 /* either we are doing array of char, or we'll have to dig in */
2827 if (is_string_type(ctype
)) {
2833 /* struct or union can be initialized by compatible */
2834 if (class != TYPE_COMPOUND
)
2836 type
= evaluate_expression(e
);
2839 if (ctype
->type
== SYM_NODE
)
2840 ctype
= ctype
->ctype
.base_type
;
2841 if (type
->type
== SYM_NODE
)
2842 type
= type
->ctype
.base_type
;
2848 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2850 type
= evaluate_expression(p
);
2851 if (ctype
->bit_size
!= -1) {
2852 struct symbol
*char_type
= e
->wide
? wchar_ctype
: &char_ctype
;
2853 unsigned int size_with_null
= ctype
->bit_size
+ char_type
->bit_size
;
2854 if (size_with_null
< type
->bit_size
)
2856 "too long initializer-string for array of char");
2857 else if (Winit_cstring
&& size_with_null
== type
->bit_size
) {
2859 "too long initializer-string for array of char(no space for nul char)");
2866 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2868 struct symbol
*type
;
2869 int class = classify_type(ctype
, &type
);
2870 if (!handle_initializer(ep
, 0, class, ctype
, 0))
2871 expression_error(*ep
, "invalid initializer");
2874 static struct symbol
*cast_to_bool(struct expression
*expr
)
2876 struct expression
*old
= expr
->cast_expression
;
2877 struct expression
*zero
;
2878 struct symbol
*otype
;
2879 int oclass
= classify_type(degenerate(old
), &otype
);
2880 struct symbol
*ctype
;
2882 if (oclass
& TYPE_COMPOUND
)
2885 zero
= alloc_const_expression(expr
->pos
, 0);
2886 expr
->op
= SPECIAL_NOTEQUAL
;
2887 ctype
= usual_conversions(expr
->op
, old
, zero
,
2888 oclass
, TYPE_NUM
, otype
, zero
->ctype
);
2889 expr
->type
= EXPR_COMPARE
;
2890 expr
->left
= cast_to(old
, ctype
);
2891 expr
->right
= cast_to(zero
, ctype
);
2896 static int cast_flags(struct expression
*expr
, struct expression
*old
)
2900 int flags
= CEF_NONE
;
2902 class = classify_type(expr
->ctype
, &t
);
2903 if (class & TYPE_NUM
) {
2904 flags
= old
->flags
& ~CEF_CONST_MASK
;
2906 * Casts to numeric types never result in address
2907 * constants [6.6(9)].
2912 * As an extension, treat address constants cast to
2913 * integer type as an arithmetic constant.
2915 if (old
->flags
& CEF_ADDR
)
2919 * Cast to float type -> not an integer constant
2920 * expression [6.6(6)].
2922 if (class & TYPE_FLOAT
)
2923 flags
&= ~CEF_CLR_ICE
;
2925 * Casts of float literals to integer type results in
2926 * a constant integer expression [6.6(6)].
2928 else if (old
->flags
& CEF_FLOAT
)
2929 flags
= CEF_SET_ICE
;
2930 } else if (class & TYPE_PTR
) {
2932 * Casts of integer literals to pointer type yield
2933 * address constants [6.6(9)].
2935 * As an extension, treat address constants cast to a
2936 * different pointer type as address constants again.
2938 * As another extension, treat integer constant
2939 * expressions (in contrast to literals) cast to
2940 * pointer type as address constants.
2942 if (old
->flags
& (CEF_ICE
| CEF_ADDR
))
2950 // check if a type matches one of the members of a union type
2951 // @utype: the union type
2952 // @type: to type to check
2953 // @return: to identifier of the matching type in the union.
2954 static struct symbol
*find_member_type(struct symbol
*utype
, struct symbol
*type
)
2956 struct symbol
*t
, *member
;
2958 if (utype
->type
!= SYM_UNION
)
2961 FOR_EACH_PTR(utype
->symbol_list
, member
) {
2962 classify_type(member
, &t
);
2965 } END_FOR_EACH_PTR(member
);
2969 static struct symbol
*evaluate_compound_literal(struct expression
*expr
, struct expression
*source
)
2971 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2972 struct symbol
*sym
= expr
->cast_type
;
2974 sym
->initializer
= source
;
2975 evaluate_symbol(sym
);
2977 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2979 if (sym
->ctype
.modifiers
& MOD_TOPLEVEL
)
2980 addr
->flags
|= CEF_ADDR
;
2982 expr
->type
= EXPR_PREOP
;
2989 static struct symbol
*evaluate_cast(struct expression
*expr
)
2991 struct expression
*source
= expr
->cast_expression
;
2992 struct symbol
*ctype
;
2993 struct symbol
*ttype
, *stype
;
2994 struct symbol
*member
;
2996 struct ident
*tas
= NULL
, *sas
= NULL
;
3002 * Special case: a cast can be followed by an
3003 * initializer, in which case we need to pass
3004 * the type value down to that initializer rather
3005 * than trying to evaluate it as an expression
3006 * (cfr. compound literals: C99 & C11 6.5.2.5).
3008 * A more complex case is when the initializer is
3009 * dereferenced as part of a post-fix expression.
3010 * We need to produce an expression that can be dereferenced.
3012 if (source
->type
== EXPR_INITIALIZER
)
3013 return evaluate_compound_literal(expr
, source
);
3015 ctype
= examine_symbol_type(expr
->cast_type
);
3016 expr
->ctype
= ctype
;
3017 expr
->cast_type
= ctype
;
3019 evaluate_expression(source
);
3022 tclass
= classify_type(ctype
, &ttype
);
3024 expr
->flags
= cast_flags(expr
, source
);
3027 * You can always throw a value away by casting to
3028 * "void" - that's an implicit "force". Note that
3029 * the same is _not_ true of "void *".
3031 if (ttype
== &void_ctype
)
3034 stype
= source
->ctype
;
3036 expression_error(expr
, "cast from unknown type");
3039 sclass
= classify_type(stype
, &stype
);
3041 if (expr
->type
== EXPR_FORCE_CAST
)
3044 if (tclass
& (TYPE_COMPOUND
| TYPE_FN
)) {
3046 * Special case: cast to union type (GCC extension)
3047 * The effect is similar to a compound literal except
3048 * that the result is a rvalue.
3050 if ((member
= find_member_type(ttype
, stype
))) {
3051 struct expression
*item
, *init
;
3054 warning(expr
->pos
, "cast to union type");
3056 item
= alloc_expression(source
->pos
, EXPR_IDENTIFIER
);
3057 item
->expr_ident
= member
->ident
;
3058 item
->ident_expression
= source
;
3060 init
= alloc_expression(source
->pos
, EXPR_INITIALIZER
);
3061 add_expression(&init
->expr_list
, item
);
3063 // FIXME: this should be a rvalue
3064 evaluate_compound_literal(expr
, init
);
3068 warning(expr
->pos
, "cast to non-scalar");
3071 if (sclass
& TYPE_COMPOUND
)
3072 warning(expr
->pos
, "cast from non-scalar");
3074 /* allowed cast unfouls */
3075 if (sclass
& TYPE_FOULED
)
3076 stype
= unfoul(stype
);
3078 if (ttype
!= stype
) {
3079 if ((tclass
& TYPE_RESTRICT
) && restricted_value(source
, ttype
))
3080 warning(expr
->pos
, "cast to %s",
3081 show_typename(ttype
));
3082 if (sclass
& TYPE_RESTRICT
) {
3083 if (ttype
== &bool_ctype
) {
3084 if (sclass
& TYPE_FOULED
)
3085 warning(expr
->pos
, "%s degrades to integer",
3086 show_typename(stype
));
3088 warning(expr
->pos
, "cast from %s",
3089 show_typename(stype
));
3094 if ((ttype
== &ulong_ctype
|| ttype
== uintptr_ctype
) && !Wcast_from_as
)
3095 tas
= &bad_address_space
;
3096 else if (tclass
== TYPE_PTR
) {
3097 examine_pointer_target(ttype
);
3098 tas
= ttype
->ctype
.as
;
3101 if ((stype
== &ulong_ctype
|| stype
== uintptr_ctype
))
3102 sas
= &bad_address_space
;
3103 else if (sclass
== TYPE_PTR
) {
3104 examine_pointer_target(stype
);
3105 sas
= stype
->ctype
.as
;
3108 if (!tas
&& valid_as(sas
))
3109 warning(expr
->pos
, "cast removes address space '%s' of expression", show_as(sas
));
3110 if (valid_as(tas
) && valid_as(sas
) && tas
!= sas
)
3111 warning(expr
->pos
, "cast between address spaces (%s -> %s)", show_as(sas
), show_as(tas
));
3112 if (valid_as(tas
) && !sas
&&
3113 !is_null_pointer_constant(source
) && Wcast_to_as
)
3115 "cast adds address space '%s' to expression", show_as(tas
));
3117 if (!(ttype
->ctype
.modifiers
& MOD_PTRINHERIT
) && tclass
== TYPE_PTR
&&
3118 !tas
&& (source
->flags
& CEF_ICE
)) {
3119 if (ttype
->ctype
.base_type
== &void_ctype
) {
3120 if (is_zero_constant(source
)) {
3122 expr
->type
= EXPR_VALUE
;
3123 expr
->ctype
= &null_ctype
;
3130 if (ttype
== &bool_ctype
)
3133 // checks pointers to restricted
3134 while (Wbitwise_pointer
&& tclass
== TYPE_PTR
&& sclass
== TYPE_PTR
) {
3135 tclass
= classify_type(ttype
->ctype
.base_type
, &ttype
);
3136 sclass
= classify_type(stype
->ctype
.base_type
, &stype
);
3139 if (!ttype
|| !stype
)
3141 if (ttype
== &void_ctype
|| stype
== &void_ctype
)
3143 if (tclass
& TYPE_RESTRICT
) {
3144 warning(expr
->pos
, "cast to %s", show_typename(ctype
));
3147 if (sclass
& TYPE_RESTRICT
) {
3148 warning(expr
->pos
, "cast from %s", show_typename(source
->ctype
));
3157 * Evaluate a call expression with a symbol. This
3158 * should expand inline functions, and evaluate
3161 static int evaluate_symbol_call(struct expression
*expr
)
3163 struct expression
*fn
= expr
->fn
;
3164 struct symbol
*ctype
= fn
->ctype
;
3166 if (fn
->type
!= EXPR_PREOP
)
3169 if (ctype
->op
&& ctype
->op
->evaluate
)
3170 return ctype
->op
->evaluate(expr
);
3175 static struct symbol
*evaluate_call(struct expression
*expr
)
3178 struct symbol
*ctype
, *sym
;
3179 struct expression
*fn
= expr
->fn
;
3180 struct expression_list
*arglist
= expr
->args
;
3182 if (!evaluate_expression(fn
))
3184 sym
= ctype
= fn
->ctype
;
3185 if (ctype
->type
== SYM_NODE
)
3186 ctype
= ctype
->ctype
.base_type
;
3187 if (ctype
->type
== SYM_PTR
)
3188 ctype
= get_base_type(ctype
);
3190 if (ctype
->type
!= SYM_FN
) {
3191 struct expression
*arg
;
3193 if (fn
->ctype
== &bad_ctype
)
3196 expression_error(expr
, "not a function %s",
3197 show_ident(sym
->ident
));
3198 /* do typechecking in arguments */
3199 FOR_EACH_PTR (arglist
, arg
) {
3200 evaluate_expression(arg
);
3201 } END_FOR_EACH_PTR(arg
);
3205 examine_fn_arguments(ctype
);
3206 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
3207 sym
->op
&& sym
->op
->args
) {
3208 if (!sym
->op
->args(expr
))
3211 if (!evaluate_arguments(ctype
->arguments
, arglist
))
3213 args
= expression_list_size(expr
->args
);
3214 fnargs
= symbol_list_size(ctype
->arguments
);
3215 if (args
< fnargs
) {
3216 expression_error(expr
,
3217 "not enough arguments for function %s",
3218 show_ident(sym
->ident
));
3221 if (args
> fnargs
&& !ctype
->variadic
)
3222 expression_error(expr
,
3223 "too many arguments for function %s",
3224 show_ident(sym
->ident
));
3226 expr
->ctype
= ctype
->ctype
.base_type
;
3227 if (sym
->type
== SYM_NODE
) {
3228 if (evaluate_symbol_call(expr
))
3234 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
3236 struct expression
*e
= expr
->down
;
3237 struct symbol
*ctype
= expr
->in
;
3240 if (expr
->op
== '.') {
3241 struct symbol
*field
;
3244 expression_error(expr
, "expected structure or union");
3247 examine_symbol_type(ctype
);
3248 class = classify_type(ctype
, &ctype
);
3249 if (class != TYPE_COMPOUND
) {
3250 expression_error(expr
, "expected structure or union");
3254 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
3256 expression_error(expr
, "unknown member");
3260 expr
->type
= EXPR_VALUE
;
3261 expr
->flags
= CEF_SET_ICE
;
3262 expr
->value
= offset
;
3264 expr
->ctype
= size_t_ctype
;
3267 expression_error(expr
, "expected structure or union");
3270 examine_symbol_type(ctype
);
3271 class = classify_type(ctype
, &ctype
);
3272 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
3273 expression_error(expr
, "expected array");
3276 ctype
= ctype
->ctype
.base_type
;
3278 expr
->type
= EXPR_VALUE
;
3279 expr
->flags
= CEF_SET_ICE
;
3282 expr
->ctype
= size_t_ctype
;
3284 struct expression
*idx
= expr
->index
, *m
;
3285 struct symbol
*i_type
= evaluate_expression(idx
);
3286 unsigned old_idx_flags
;
3287 int i_class
= classify_type(i_type
, &i_type
);
3289 if (!is_int(i_class
)) {
3290 expression_error(expr
, "non-integer index");
3293 unrestrict(idx
, i_class
, &i_type
);
3294 old_idx_flags
= idx
->flags
;
3295 idx
= cast_to(idx
, size_t_ctype
);
3296 idx
->flags
= old_idx_flags
;
3297 m
= alloc_const_expression(expr
->pos
,
3298 bits_to_bytes(ctype
->bit_size
));
3299 m
->ctype
= size_t_ctype
;
3300 m
->flags
= CEF_SET_INT
;
3301 expr
->type
= EXPR_BINOP
;
3305 expr
->ctype
= size_t_ctype
;
3306 expr
->flags
= m
->flags
& idx
->flags
& ~CEF_CONST_MASK
;
3310 struct expression
*copy
= __alloc_expression(0);
3312 if (e
->type
== EXPR_OFFSETOF
)
3314 if (!evaluate_expression(e
))
3316 expr
->type
= EXPR_BINOP
;
3317 expr
->flags
= e
->flags
& copy
->flags
& ~CEF_CONST_MASK
;
3319 expr
->ctype
= size_t_ctype
;
3323 return size_t_ctype
;
3326 static void check_label_declaration(struct position pos
, struct symbol
*label
)
3328 switch (label
->namespace) {
3332 sparse_error(pos
, "label '%s' was not declared", show_ident(label
->ident
));
3335 current_fn
->bogus_linear
= 1;
3341 static int type_selection(struct symbol
*ctrl
, struct symbol
*type
)
3343 struct ctype c
= { .base_type
= ctrl
};
3344 struct ctype t
= { .base_type
= type
};
3346 return !type_difference(&c
, &t
, 0, 0);
3349 static struct symbol
*evaluate_generic_selection(struct expression
*expr
)
3351 struct type_expression
*map
;
3352 struct expression
*res
;
3353 struct symbol source
;
3354 struct symbol
*ctrl
;
3356 if (!evaluate_expression(expr
->control
))
3358 if (!(ctrl
= degenerate(expr
->control
)))
3362 source
.ctype
.modifiers
&= ~(MOD_QUALIFIER
|MOD_ATOMIC
);
3363 for (map
= expr
->map
; map
; map
= map
->next
) {
3364 struct symbol
*stype
= map
->type
;
3365 struct symbol
*base
;
3367 if (!evaluate_symbol(stype
))
3370 base
= stype
->ctype
.base_type
;
3371 if (base
->type
== SYM_ARRAY
&& base
->array_size
) {
3372 get_expression_value_silent(base
->array_size
);
3373 if (base
->array_size
->type
== EXPR_VALUE
)
3375 sparse_error(stype
->pos
, "variable length array type in generic selection");
3378 if (is_func_type(stype
)) {
3379 sparse_error(stype
->pos
, "function type in generic selection");
3382 if (stype
->bit_size
<= 0 || is_void_type(stype
)) {
3383 sparse_error(stype
->pos
, "incomplete type in generic selection");
3386 if (!type_selection(&source
, stype
))
3394 sparse_error(expr
->pos
, "no generic selection for '%s'", show_typename(ctrl
));
3400 return evaluate_expression(expr
);
3403 struct symbol
*evaluate_expression(struct expression
*expr
)
3410 switch (expr
->type
) {
3413 expression_error(expr
, "value expression without a type");
3416 return evaluate_string(expr
);
3418 return evaluate_symbol_expression(expr
);
3420 evaluate_expression(expr
->left
);
3421 evaluate_expression(expr
->right
);
3422 if (!valid_subexpr_type(expr
))
3424 return evaluate_binop(expr
);
3426 return evaluate_logical(expr
);
3428 evaluate_expression(expr
->left
);
3429 if (!evaluate_expression(expr
->right
))
3431 return evaluate_comma(expr
);
3433 evaluate_expression(expr
->left
);
3434 evaluate_expression(expr
->right
);
3435 if (!valid_subexpr_type(expr
))
3437 return evaluate_compare(expr
);
3438 case EXPR_ASSIGNMENT
:
3439 evaluate_expression(expr
->left
);
3440 evaluate_expression(expr
->right
);
3441 if (!valid_subexpr_type(expr
))
3443 return evaluate_assignment(expr
);
3445 if (!evaluate_expression(expr
->unop
))
3447 return evaluate_preop(expr
);
3449 if (!evaluate_expression(expr
->unop
))
3451 return evaluate_postop(expr
);
3453 case EXPR_FORCE_CAST
:
3454 case EXPR_IMPLIED_CAST
:
3455 return evaluate_cast(expr
);
3457 return evaluate_sizeof(expr
);
3458 case EXPR_PTRSIZEOF
:
3459 return evaluate_ptrsizeof(expr
);
3461 return evaluate_alignof(expr
);
3463 return evaluate_member_dereference(expr
);
3465 return evaluate_call(expr
);
3467 case EXPR_CONDITIONAL
:
3468 return evaluate_conditional_expression(expr
);
3469 case EXPR_STATEMENT
:
3470 expr
->ctype
= evaluate_statement(expr
->statement
);
3474 expr
->ctype
= &ptr_ctype
;
3475 check_label_declaration(expr
->pos
, expr
->label_symbol
);
3479 /* Evaluate the type of the symbol .. */
3480 evaluate_symbol(expr
->symbol
);
3481 /* .. but the type of the _expression_ is a "type" */
3482 expr
->ctype
= &type_ctype
;
3486 return evaluate_offsetof(expr
);
3489 return evaluate_generic_selection(expr
);
3491 /* These can not exist as stand-alone expressions */
3492 case EXPR_INITIALIZER
:
3493 case EXPR_IDENTIFIER
:
3496 expression_error(expr
, "internal front-end error: initializer in expression");
3499 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3505 void check_duplicates(struct symbol
*sym
)
3508 struct symbol
*next
= sym
;
3509 int initialized
= sym
->initializer
!= NULL
;
3511 while ((next
= next
->same_symbol
) != NULL
) {
3512 const char *typediff
;
3513 evaluate_symbol(next
);
3514 if (initialized
&& next
->initializer
) {
3515 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3516 show_ident(sym
->ident
),
3517 stream_name(next
->pos
.stream
), next
->pos
.line
);
3518 /* Only warn once */
3522 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3524 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (%s):",
3525 show_ident(sym
->ident
), typediff
);
3526 info(sym
->pos
, " %s", show_typename(sym
));
3527 info(next
->pos
, "note: previously declared as:");
3528 info(next
->pos
, " %s", show_typename(next
));
3533 unsigned long mod
= sym
->ctype
.modifiers
;
3534 if (mod
& (MOD_STATIC
| MOD_REGISTER
| MOD_EXT_VISIBLE
))
3536 if (!(mod
& MOD_TOPLEVEL
))
3540 if (sym
->ident
== &main_ident
)
3542 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3546 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3548 struct symbol
*base_type
;
3556 sym
= examine_symbol_type(sym
);
3557 base_type
= get_base_type(sym
);
3561 /* Evaluate the initializers */
3562 if (sym
->initializer
)
3563 evaluate_initializer(sym
, &sym
->initializer
);
3565 /* And finally, evaluate the body of the symbol too */
3566 if (base_type
->type
== SYM_FN
) {
3567 struct symbol
*curr
= current_fn
;
3569 if (sym
->definition
&& sym
->definition
!= sym
)
3570 return evaluate_symbol(sym
->definition
);
3574 examine_fn_arguments(base_type
);
3575 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3577 if (base_type
->stmt
)
3578 evaluate_statement(base_type
->stmt
);
3586 void evaluate_symbol_list(struct symbol_list
*list
)
3590 FOR_EACH_PTR(list
, sym
) {
3591 has_error
&= ~ERROR_CURR_PHASE
;
3592 evaluate_symbol(sym
);
3593 check_duplicates(sym
);
3594 } END_FOR_EACH_PTR(sym
);
3597 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3599 struct expression
*expr
= stmt
->expression
;
3600 struct symbol
*fntype
, *rettype
;
3602 evaluate_expression(expr
);
3603 fntype
= current_fn
->ctype
.base_type
;
3604 rettype
= fntype
->ctype
.base_type
;
3605 if (!rettype
|| rettype
== &void_ctype
) {
3606 if (expr
&& !is_void_type(expr
->ctype
))
3607 expression_error(expr
, "return expression in %s function", rettype
?"void":"typeless");
3608 if (expr
&& Wreturn_void
)
3609 warning(stmt
->pos
, "returning void-valued expression");
3614 sparse_error(stmt
->pos
, "return with no return value");
3619 compatible_assignment_types(expr
, rettype
, &stmt
->expression
, "return expression");
3623 static void evaluate_if_statement(struct statement
*stmt
)
3625 if (!stmt
->if_conditional
)
3628 evaluate_conditional(stmt
->if_conditional
, 0);
3629 evaluate_statement(stmt
->if_true
);
3630 evaluate_statement(stmt
->if_false
);
3633 static void evaluate_iterator(struct statement
*stmt
)
3635 evaluate_symbol_list(stmt
->iterator_syms
);
3636 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3637 evaluate_conditional(stmt
->iterator_post_condition
,1);
3638 evaluate_statement(stmt
->iterator_pre_statement
);
3639 evaluate_statement(stmt
->iterator_statement
);
3640 evaluate_statement(stmt
->iterator_post_statement
);
3644 static void parse_asm_constraint(struct asm_operand
*op
)
3646 struct expression
*constraint
= op
->constraint
;
3647 const char *str
= constraint
->string
->data
;
3652 sparse_error(constraint
->pos
, "invalid ASM constraint (\"\")");
3655 op
->is_modify
= true;
3658 op
->is_assign
= true;
3663 while ((c
= *str
++)) {
3667 sparse_error(constraint
->pos
, "invalid ASM constraint '%c'", c
);
3671 op
->is_earlyclobber
= true;
3674 op
->is_commutative
= true;
3677 op
->is_register
= true;
3684 op
->is_memory
= true;
3689 // FIXME: ignored for now
3693 // FIXME: multiple alternative constraints
3697 // FIXME: numeric matching constraint?
3700 // FIXME: symbolic matching constraint
3704 if (arch_target
->asm_constraint
)
3705 str
= arch_target
->asm_constraint(op
, c
, str
);
3707 // FIXME: multi-letter constraints
3712 // FIXME: how to deal with multi-constraint?
3713 if (op
->is_register
)
3717 static void verify_output_constraint(struct asm_operand
*op
)
3719 struct expression
*expr
= op
->constraint
;
3720 const char *constraint
= expr
->string
->data
;
3723 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3726 static void verify_input_constraint(struct asm_operand
*op
)
3728 struct expression
*expr
= op
->constraint
;
3729 const char *constraint
= expr
->string
->data
;
3732 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3735 static void evaluate_asm_memop(struct asm_operand
*op
)
3737 if (op
->is_memory
) {
3738 struct expression
*expr
= op
->expr
;
3739 struct expression
*addr
;
3741 // implicit addressof
3742 addr
= alloc_expression(expr
->pos
, EXPR_PREOP
);
3746 evaluate_addressof(addr
);
3749 evaluate_expression(op
->expr
);
3750 degenerate(op
->expr
);
3754 static void evaluate_asm_statement(struct statement
*stmt
)
3756 struct expression
*expr
;
3757 struct asm_operand
*op
;
3760 if (!stmt
->asm_string
)
3763 FOR_EACH_PTR(stmt
->asm_outputs
, op
) {
3767 if (op
->constraint
) {
3768 parse_asm_constraint(op
);
3769 verify_output_constraint(op
);
3774 if (!evaluate_expression(expr
))
3776 if (!lvalue_expression(expr
))
3777 warning(expr
->pos
, "asm output is not an lvalue");
3778 evaluate_assign_to(expr
, expr
->ctype
);
3779 evaluate_asm_memop(op
);
3780 } END_FOR_EACH_PTR(op
);
3782 FOR_EACH_PTR(stmt
->asm_inputs
, op
) {
3786 if (op
->constraint
) {
3787 parse_asm_constraint(op
);
3788 verify_input_constraint(op
);
3792 if (!evaluate_expression(op
->expr
))
3794 evaluate_asm_memop(op
);
3795 } END_FOR_EACH_PTR(op
);
3797 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3799 sparse_error(stmt
->pos
, "bad asm clobbers");
3802 if (expr
->type
== EXPR_STRING
)
3804 expression_error(expr
, "asm clobber is not a string");
3805 } END_FOR_EACH_PTR(expr
);
3807 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3808 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3809 sparse_error(stmt
->pos
, "bad asm label");
3812 } END_FOR_EACH_PTR(sym
);
3815 static void evaluate_case_statement(struct statement
*stmt
)
3817 evaluate_expression(stmt
->case_expression
);
3818 evaluate_expression(stmt
->case_to
);
3819 evaluate_statement(stmt
->case_statement
);
3822 static void check_case_type(struct expression
*switch_expr
,
3823 struct expression
*case_expr
,
3824 struct expression
**enumcase
)
3826 struct symbol
*switch_type
, *case_type
;
3832 switch_type
= switch_expr
->ctype
;
3833 case_type
= evaluate_expression(case_expr
);
3835 if (!switch_type
|| !case_type
)
3839 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3840 else if (is_enum_type(case_type
))
3841 *enumcase
= case_expr
;
3844 sclass
= classify_type(switch_type
, &switch_type
);
3845 cclass
= classify_type(case_type
, &case_type
);
3847 /* both should be arithmetic */
3848 if (!(sclass
& cclass
& TYPE_NUM
))
3851 /* neither should be floating */
3852 if ((sclass
| cclass
) & TYPE_FLOAT
)
3855 /* if neither is restricted, we are OK */
3856 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3859 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3860 cclass
, sclass
, case_type
, switch_type
)) {
3861 unrestrict(case_expr
, cclass
, &case_type
);
3862 unrestrict(switch_expr
, sclass
, &switch_type
);
3867 expression_error(case_expr
, "incompatible types for 'case' statement");
3870 static void evaluate_switch_statement(struct statement
*stmt
)
3873 struct expression
*enumcase
= NULL
;
3874 struct expression
**enumcase_holder
= &enumcase
;
3875 struct expression
*sel
= stmt
->switch_expression
;
3877 evaluate_expression(sel
);
3878 evaluate_statement(stmt
->switch_statement
);
3881 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3882 enumcase_holder
= NULL
; /* Only check cases against switch */
3884 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3885 struct statement
*case_stmt
= sym
->stmt
;
3886 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3887 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3888 } END_FOR_EACH_PTR(sym
);
3891 static void evaluate_goto_statement(struct statement
*stmt
)
3893 struct symbol
*label
= stmt
->goto_label
;
3896 // no label associated, may be a computed goto
3897 evaluate_expression(stmt
->goto_expression
);
3901 check_label_declaration(stmt
->pos
, label
);
3904 struct symbol
*evaluate_statement(struct statement
*stmt
)
3909 switch (stmt
->type
) {
3910 case STMT_DECLARATION
: {
3912 FOR_EACH_PTR(stmt
->declaration
, s
) {
3914 } END_FOR_EACH_PTR(s
);
3919 return evaluate_return_expression(stmt
);
3921 case STMT_EXPRESSION
:
3922 if (!evaluate_expression(stmt
->expression
))
3924 if (stmt
->expression
->ctype
== &null_ctype
)
3925 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3926 return degenerate(stmt
->expression
);
3928 case STMT_COMPOUND
: {
3929 struct statement
*s
;
3930 struct symbol
*type
= NULL
;
3932 /* Evaluate the return symbol in the compound statement */
3933 evaluate_symbol(stmt
->ret
);
3936 * Then, evaluate each statement, making the type of the
3937 * compound statement be the type of the last statement
3939 type
= evaluate_statement(stmt
->args
);
3940 FOR_EACH_PTR(stmt
->stmts
, s
) {
3941 type
= evaluate_statement(s
);
3942 } END_FOR_EACH_PTR(s
);
3948 evaluate_if_statement(stmt
);
3951 evaluate_iterator(stmt
);
3954 evaluate_switch_statement(stmt
);
3957 evaluate_case_statement(stmt
);
3960 return evaluate_statement(stmt
->label_statement
);
3962 evaluate_goto_statement(stmt
);
3967 evaluate_asm_statement(stmt
);
3970 evaluate_expression(stmt
->expression
);
3973 evaluate_expression(stmt
->range_expression
);
3974 evaluate_expression(stmt
->range_low
);
3975 evaluate_expression(stmt
->range_high
);