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.
43 #include "expression.h"
45 struct symbol
*current_fn
;
47 static struct symbol
*degenerate(struct expression
*expr
);
48 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
50 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
52 struct expression
*addr
;
53 struct symbol
*sym
= expr
->symbol
;
54 struct symbol
*base_type
;
57 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
61 examine_symbol_type(sym
);
63 base_type
= get_base_type(sym
);
65 expression_error(expr
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
69 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
71 addr
->symbol_name
= expr
->symbol_name
;
72 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
73 expr
->type
= EXPR_PREOP
;
77 /* The type of a symbol is the symbol itself! */
82 static struct symbol
*evaluate_string(struct expression
*expr
)
84 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
85 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
86 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
87 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
88 unsigned int length
= expr
->string
->length
;
90 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
91 sym
->bit_size
= bytes_to_bits(length
);
92 sym
->ctype
.alignment
= 1;
94 sym
->ctype
.modifiers
= MOD_STATIC
;
95 sym
->ctype
.base_type
= array
;
96 sym
->initializer
= initstr
;
99 initstr
->string
= expr
->string
;
101 array
->array_size
= sym
->array_size
;
102 array
->bit_size
= bytes_to_bits(length
);
103 array
->ctype
.alignment
= 1;
104 array
->ctype
.modifiers
= MOD_STATIC
;
105 array
->ctype
.base_type
= &char_ctype
;
108 addr
->ctype
= &lazy_ptr_ctype
;
110 expr
->type
= EXPR_PREOP
;
117 /* type has come from classify_type and is an integer type */
118 static inline struct symbol
*integer_promotion(struct symbol
*type
)
120 unsigned long mod
= type
->ctype
.modifiers
;
121 int width
= type
->bit_size
;
124 * Bitfields always promote to the base type,
125 * even if the bitfield might be bigger than
128 if (type
->type
== SYM_BITFIELD
) {
129 type
= type
->ctype
.base_type
;
131 mod
= type
->ctype
.modifiers
;
132 if (width
< bits_in_int
)
135 /* If char/short has as many bits as int, it still gets "promoted" */
136 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
137 if (mod
& MOD_UNSIGNED
)
145 * integer part of usual arithmetic conversions:
146 * integer promotions are applied
147 * if left and right are identical, we are done
148 * if signedness is the same, convert one with lower rank
149 * unless unsigned argument has rank lower than signed one, convert the
151 * if signed argument is bigger than unsigned one, convert the unsigned.
152 * otherwise, convert signed.
154 * Leaving aside the integer promotions, that is equivalent to
155 * if identical, don't convert
156 * if left is bigger than right, convert right
157 * if right is bigger than left, convert right
158 * otherwise, if signedness is the same, convert one with lower rank
159 * otherwise convert the signed one.
161 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
163 unsigned long lmod
, rmod
;
165 left
= integer_promotion(left
);
166 right
= integer_promotion(right
);
171 if (left
->bit_size
> right
->bit_size
)
174 if (right
->bit_size
> left
->bit_size
)
177 lmod
= left
->ctype
.modifiers
;
178 rmod
= right
->ctype
.modifiers
;
179 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
180 if (lmod
& MOD_UNSIGNED
)
182 } else if ((lmod
& ~rmod
) & (MOD_LONG_ALL
))
190 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
192 return orig
->bit_size
== new->bit_size
&&
193 orig
->bit_offset
== new->bit_offset
;
196 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
198 unsigned long mod
, as
;
202 mod
|= node
->ctype
.modifiers
;
203 as
|= node
->ctype
.as
;
204 if (node
->type
== SYM_NODE
) {
205 node
= node
->ctype
.base_type
;
210 *modp
= mod
& ~MOD_IGNORE
;
215 static int is_same_type(struct expression
*expr
, struct symbol
*new)
217 struct symbol
*old
= expr
->ctype
;
218 unsigned long oldmod
, newmod
, oldas
, newas
;
220 old
= base_type(old
, &oldmod
, &oldas
);
221 new = base_type(new, &newmod
, &newas
);
223 /* Same base type, same address space? */
224 if (old
== new && oldas
== newas
) {
225 unsigned long difmod
;
227 /* Check the modifier bits. */
228 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
230 /* Exact same type? */
235 * Not the same type, but differs only in "const".
236 * Don't warn about MOD_NOCAST.
238 if (difmod
== MOD_CONST
)
241 if ((oldmod
| newmod
) & MOD_NOCAST
) {
242 const char *tofrom
= "to/from";
243 if (!(newmod
& MOD_NOCAST
))
245 if (!(oldmod
& MOD_NOCAST
))
247 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
253 warn_for_different_enum_types (struct position pos
,
254 struct symbol
*typea
,
255 struct symbol
*typeb
)
259 if (typea
->type
== SYM_NODE
)
260 typea
= typea
->ctype
.base_type
;
261 if (typeb
->type
== SYM_NODE
)
262 typeb
= typeb
->ctype
.base_type
;
267 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
268 warning(pos
, "mixing different enum types");
269 info(pos
, " %s versus", show_typename(typea
));
270 info(pos
, " %s", show_typename(typeb
));
274 static struct symbol
*cast_to_bool(struct expression
*expr
);
277 * This gets called for implicit casts in assignments and
278 * integer promotion. We often want to try to move the
279 * cast down, because the ops involved may have been
280 * implicitly cast up, and we can get rid of the casts
283 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
285 struct expression
*expr
;
287 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
289 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
293 * See if we can simplify the op. Move the cast down.
297 if (old
->ctype
->bit_size
< type
->bit_size
)
299 if (old
->op
== '~') {
301 old
->unop
= cast_to(old
->unop
, type
);
306 case EXPR_IMPLIED_CAST
:
307 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
309 if (old
->ctype
->bit_size
>= type
->bit_size
) {
310 struct expression
*orig
= old
->cast_expression
;
311 if (same_cast_type(orig
->ctype
, type
))
313 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
315 old
->cast_type
= type
;
325 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
326 expr
->flags
= old
->flags
;
328 expr
->cast_type
= type
;
329 expr
->cast_expression
= old
;
331 if (is_bool_type(type
))
348 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
350 static int type_class
[SYM_BAD
+ 1] = {
351 [SYM_PTR
] = TYPE_PTR
,
352 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
353 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
354 [SYM_STRUCT
] = TYPE_COMPOUND
,
355 [SYM_UNION
] = TYPE_COMPOUND
,
356 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
357 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
358 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
360 if (type
->type
== SYM_NODE
)
361 type
= type
->ctype
.base_type
;
362 if (type
->type
== SYM_TYPEOF
) {
363 type
= evaluate_expression(type
->initializer
);
366 else if (type
->type
== SYM_NODE
)
367 type
= type
->ctype
.base_type
;
369 if (type
->type
== SYM_ENUM
)
370 type
= type
->ctype
.base_type
;
372 if (type
->type
== SYM_BASETYPE
) {
373 if (type
->ctype
.base_type
== &int_type
)
375 if (type
->ctype
.base_type
== &fp_type
)
376 return TYPE_NUM
| TYPE_FLOAT
;
378 return type_class
[type
->type
];
381 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
383 static inline int is_string_type(struct symbol
*type
)
385 if (type
->type
== SYM_NODE
)
386 type
= type
->ctype
.base_type
;
387 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
390 static struct symbol
*bad_expr_type(struct expression
*expr
)
392 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
393 switch (expr
->type
) {
396 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
397 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
401 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
408 return expr
->ctype
= &bad_ctype
;
411 static int restricted_value(struct expression
*v
, struct symbol
*type
)
413 if (v
->type
!= EXPR_VALUE
)
420 static int restricted_binop(int op
, struct symbol
*type
)
425 case SPECIAL_AND_ASSIGN
:
426 case SPECIAL_OR_ASSIGN
:
427 case SPECIAL_XOR_ASSIGN
:
428 return 1; /* unfoul */
432 return 2; /* keep fouled */
434 case SPECIAL_NOTEQUAL
:
435 return 3; /* warn if fouled */
441 static int restricted_unop(int op
, struct symbol
**type
)
444 if ((*type
)->bit_size
< bits_in_int
)
445 *type
= befoul(*type
);
452 /* type should be SYM_FOULED */
453 static inline struct symbol
*unfoul(struct symbol
*type
)
455 return type
->ctype
.base_type
;
458 static struct symbol
*restricted_binop_type(int op
,
459 struct expression
*left
,
460 struct expression
*right
,
461 int lclass
, int rclass
,
462 struct symbol
*ltype
,
463 struct symbol
*rtype
)
465 struct symbol
*ctype
= NULL
;
466 if (lclass
& TYPE_RESTRICT
) {
467 if (rclass
& TYPE_RESTRICT
) {
468 if (ltype
== rtype
) {
470 } else if (lclass
& TYPE_FOULED
) {
471 if (unfoul(ltype
) == rtype
)
473 } else if (rclass
& TYPE_FOULED
) {
474 if (unfoul(rtype
) == ltype
)
478 if (!restricted_value(right
, ltype
))
481 } else if (!restricted_value(left
, rtype
))
485 switch (restricted_binop(op
, ctype
)) {
487 if ((lclass
^ rclass
) & TYPE_FOULED
)
488 ctype
= unfoul(ctype
);
491 if (!(lclass
& rclass
& TYPE_FOULED
))
503 static inline void unrestrict(struct expression
*expr
,
504 int class, struct symbol
**ctype
)
506 if (class & TYPE_RESTRICT
) {
507 if (class & TYPE_FOULED
)
508 *ctype
= unfoul(*ctype
);
509 warning(expr
->pos
, "%s degrades to integer",
510 show_typename(*ctype
));
511 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
515 static struct symbol
*usual_conversions(int op
,
516 struct expression
*left
,
517 struct expression
*right
,
518 int lclass
, int rclass
,
519 struct symbol
*ltype
,
520 struct symbol
*rtype
)
522 struct symbol
*ctype
;
524 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
526 if ((lclass
| rclass
) & TYPE_RESTRICT
)
530 if (!(lclass
& TYPE_FLOAT
)) {
531 if (!(rclass
& TYPE_FLOAT
))
532 return bigger_int_type(ltype
, rtype
);
535 } else if (rclass
& TYPE_FLOAT
) {
536 unsigned long lmod
= ltype
->ctype
.modifiers
;
537 unsigned long rmod
= rtype
->ctype
.modifiers
;
538 if (rmod
& ~lmod
& (MOD_LONG_ALL
))
546 ctype
= restricted_binop_type(op
, left
, right
,
547 lclass
, rclass
, ltype
, rtype
);
551 unrestrict(left
, lclass
, <ype
);
552 unrestrict(right
, rclass
, &rtype
);
557 static inline int lvalue_expression(struct expression
*expr
)
559 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
562 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
564 struct expression
*index
= expr
->right
;
565 struct symbol
*ctype
, *base
;
568 classify_type(degenerate(expr
->left
), &ctype
);
569 base
= examine_pointer_target(ctype
);
572 expression_error(expr
, "missing type information");
575 if (is_function(base
)) {
576 expression_error(expr
, "arithmetics on pointers to functions");
580 /* Get the size of whatever the pointer points to */
581 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
583 if (ctype
== &null_ctype
)
587 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
590 if (index
->type
== EXPR_VALUE
) {
591 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
592 unsigned long long v
= index
->value
, mask
;
593 mask
= 1ULL << (itype
->bit_size
- 1);
599 mask
= 1ULL << (bits_in_pointer
- 1);
600 v
&= mask
| (mask
- 1);
602 val
->ctype
= ssize_t_ctype
;
607 if (itype
->bit_size
< bits_in_pointer
)
608 index
= cast_to(index
, ssize_t_ctype
);
611 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
612 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
614 val
->ctype
= ssize_t_ctype
;
615 val
->value
= multiply
;
618 mul
->ctype
= ssize_t_ctype
;
628 static void examine_fn_arguments(struct symbol
*fn
);
630 #define MOD_IGN (MOD_VOLATILE | MOD_CONST | MOD_PURE)
632 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
633 unsigned long mod1
, unsigned long mod2
)
635 unsigned long as1
= c1
->as
, as2
= c2
->as
;
636 struct symbol
*t1
= c1
->base_type
;
637 struct symbol
*t2
= c2
->base_type
;
638 int move1
= 1, move2
= 1;
639 mod1
|= c1
->modifiers
;
640 mod2
|= c2
->modifiers
;
644 struct symbol
*base1
= t1
->ctype
.base_type
;
645 struct symbol
*base2
= t2
->ctype
.base_type
;
648 * FIXME! Collect alignment and context too here!
651 if (t1
&& t1
->type
!= SYM_PTR
) {
652 mod1
|= t1
->ctype
.modifiers
;
659 if (t2
&& t2
->type
!= SYM_PTR
) {
660 mod2
|= t2
->ctype
.modifiers
;
669 return "different types";
671 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
679 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
689 if (type
!= t2
->type
)
690 return "different base types";
694 sparse_error(t1
->pos
,
695 "internal error: bad type in derived(%d)",
699 return "different base types";
702 /* allow definition of incomplete structs and unions */
703 if (t1
->ident
== t2
->ident
)
705 return "different base types";
707 /* XXX: we ought to compare sizes */
711 return "different address spaces";
712 /* MOD_SPECIFIER is due to idiocy in parse.c */
713 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
714 return "different modifiers";
715 /* we could be lazier here */
716 base1
= examine_pointer_target(t1
);
717 base2
= examine_pointer_target(t2
);
718 mod1
= t1
->ctype
.modifiers
;
720 mod2
= t2
->ctype
.modifiers
;
724 struct symbol
*arg1
, *arg2
;
728 return "different address spaces";
729 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
730 return "different modifiers";
731 mod1
= t1
->ctype
.modifiers
;
733 mod2
= t2
->ctype
.modifiers
;
736 if (t1
->variadic
!= t2
->variadic
)
737 return "incompatible variadic arguments";
738 examine_fn_arguments(t1
);
739 examine_fn_arguments(t2
);
740 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
741 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
748 return "different argument counts";
749 diffstr
= type_difference(&arg1
->ctype
,
753 static char argdiff
[80];
754 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
761 FINISH_PTR_LIST(arg2
);
762 FINISH_PTR_LIST(arg1
);
767 return "different address spaces";
769 return "different base types";
770 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
774 return "different type sizes";
775 else if (diff
& ~MOD_SIGNEDNESS
)
776 return "different modifiers";
778 return "different signedness";
784 return "different address spaces";
785 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
786 return "different modifiers";
790 static void bad_null(struct expression
*expr
)
792 if (Wnon_pointer_null
)
793 warning(expr
->pos
, "Using plain integer as NULL pointer");
796 static unsigned long target_qualifiers(struct symbol
*type
)
798 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
799 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
804 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
806 const char *typediff
;
807 struct symbol
*ltype
, *rtype
;
808 struct expression
*l
= expr
->left
;
809 struct expression
*r
= expr
->right
;
810 struct symbol
*lbase
;
812 classify_type(degenerate(l
), <ype
);
813 classify_type(degenerate(r
), &rtype
);
815 lbase
= examine_pointer_target(ltype
);
816 examine_pointer_target(rtype
);
817 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
818 target_qualifiers(rtype
),
819 target_qualifiers(ltype
));
821 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
823 if (is_function(lbase
)) {
824 expression_error(expr
, "subtraction of functions? Share your drugs");
828 expr
->ctype
= ssize_t_ctype
;
829 if (lbase
->bit_size
> bits_in_char
) {
830 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
831 struct expression
*div
= expr
;
832 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
833 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
835 val
->ctype
= size_t_ctype
;
838 if (value
& (value
-1)) {
839 if (Wptr_subtraction_blows
)
840 warning(expr
->pos
, "potentially expensive pointer subtraction");
844 sub
->ctype
= ssize_t_ctype
;
853 return ssize_t_ctype
;
856 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
858 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
860 struct symbol
*ctype
;
865 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
866 warning(expr
->pos
, "assignment expression in conditional");
868 ctype
= evaluate_expression(expr
);
870 if (is_safe_type(ctype
))
871 warning(expr
->pos
, "testing a 'safe expression'");
872 if (is_func_type(ctype
)) {
874 warning(expr
->pos
, "the address of %s will always evaluate as true", "a function");
875 } else if (is_array_type(ctype
)) {
877 warning(expr
->pos
, "the address of %s will always evaluate as true", "an array");
878 } else if (!is_scalar_type(ctype
)) {
879 sparse_error(expr
->pos
, "incorrect type in conditional");
880 info(expr
->pos
, " got %s", show_typename(ctype
));
884 ctype
= degenerate(expr
);
889 static struct symbol
*evaluate_logical(struct expression
*expr
)
891 if (!evaluate_conditional(expr
->left
, 0))
893 if (!evaluate_conditional(expr
->right
, 0))
896 /* the result is int [6.5.13(3), 6.5.14(3)] */
897 expr
->ctype
= &int_ctype
;
899 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
905 static struct symbol
*evaluate_binop(struct expression
*expr
)
907 struct symbol
*ltype
, *rtype
, *ctype
;
908 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
909 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
913 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
917 /* number op number */
918 if (lclass
& rclass
& TYPE_NUM
) {
919 if ((lclass
| rclass
) & TYPE_FLOAT
) {
921 case '+': case '-': case '*': case '/':
924 return bad_expr_type(expr
);
928 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
929 // shifts do integer promotions, but that's it.
930 unrestrict(expr
->left
, lclass
, <ype
);
931 unrestrict(expr
->right
, rclass
, &rtype
);
932 ctype
= ltype
= integer_promotion(ltype
);
933 rtype
= integer_promotion(rtype
);
935 // The rest do usual conversions
936 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
937 && expr
->left
->op
== '!';
938 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
939 && expr
->right
->op
== '!';
940 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
941 warning(expr
->pos
, "dubious: %sx %c %sy",
944 right_not
? "!" : "");
946 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
947 lclass
, rclass
, ltype
, rtype
);
948 ctype
= rtype
= ltype
;
951 expr
->left
= cast_to(expr
->left
, ltype
);
952 expr
->right
= cast_to(expr
->right
, rtype
);
957 /* pointer (+|-) integer */
958 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
959 unrestrict(expr
->right
, rclass
, &rtype
);
960 return evaluate_ptr_add(expr
, rtype
);
963 /* integer + pointer */
964 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
965 struct expression
*index
= expr
->left
;
966 unrestrict(index
, lclass
, <ype
);
967 expr
->left
= expr
->right
;
969 return evaluate_ptr_add(expr
, ltype
);
972 /* pointer - pointer */
973 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
974 return evaluate_ptr_sub(expr
);
976 return bad_expr_type(expr
);
979 static struct symbol
*evaluate_comma(struct expression
*expr
)
981 expr
->ctype
= degenerate(expr
->right
);
982 if (expr
->ctype
== &null_ctype
)
983 expr
->ctype
= &ptr_ctype
;
984 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
988 static int modify_for_unsigned(int op
)
991 op
= SPECIAL_UNSIGNED_LT
;
993 op
= SPECIAL_UNSIGNED_GT
;
994 else if (op
== SPECIAL_LTE
)
995 op
= SPECIAL_UNSIGNED_LTE
;
996 else if (op
== SPECIAL_GTE
)
997 op
= SPECIAL_UNSIGNED_GTE
;
1001 static inline int is_null_pointer_constant(struct expression
*e
)
1003 if (e
->ctype
== &null_ctype
)
1005 if (!(e
->flags
& Int_const_expr
))
1007 return is_zero_constant(e
) ? 2 : 0;
1010 static struct symbol
*evaluate_compare(struct expression
*expr
)
1012 struct expression
*left
= expr
->left
, *right
= expr
->right
;
1013 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
1014 int lclass
= classify_type(degenerate(left
), <ype
);
1015 int rclass
= classify_type(degenerate(right
), &rtype
);
1016 struct symbol
*ctype
;
1017 const char *typediff
;
1020 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1025 if (is_type_type(ltype
) && is_type_type(rtype
))
1028 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1029 warning(expr
->pos
, "testing a 'safe expression'");
1031 /* number on number */
1032 if (lclass
& rclass
& TYPE_NUM
) {
1033 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1034 lclass
, rclass
, ltype
, rtype
);
1035 expr
->left
= cast_to(expr
->left
, ctype
);
1036 expr
->right
= cast_to(expr
->right
, ctype
);
1037 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1038 expr
->op
= modify_for_unsigned(expr
->op
);
1042 /* at least one must be a pointer */
1043 if (!((lclass
| rclass
) & TYPE_PTR
))
1044 return bad_expr_type(expr
);
1046 /* equality comparisons can be with null pointer constants */
1047 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1048 int is_null1
= is_null_pointer_constant(left
);
1049 int is_null2
= is_null_pointer_constant(right
);
1054 if (is_null1
&& is_null2
) {
1055 int positive
= expr
->op
== SPECIAL_EQUAL
;
1056 expr
->type
= EXPR_VALUE
;
1057 expr
->value
= positive
;
1060 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1061 left
= cast_to(left
, rtype
);
1064 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1065 right
= cast_to(right
, ltype
);
1069 /* both should be pointers */
1070 if (!(lclass
& rclass
& TYPE_PTR
))
1071 return bad_expr_type(expr
);
1072 expr
->op
= modify_for_unsigned(expr
->op
);
1074 lbase
= examine_pointer_target(ltype
);
1075 rbase
= examine_pointer_target(rtype
);
1077 /* they also have special treatment for pointers to void */
1078 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1079 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1080 if (lbase
== &void_ctype
) {
1081 right
= cast_to(right
, ltype
);
1084 if (rbase
== &void_ctype
) {
1085 left
= cast_to(left
, rtype
);
1091 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1092 target_qualifiers(rtype
),
1093 target_qualifiers(ltype
));
1097 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1101 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1102 expr
->ctype
= &int_ctype
;
1107 * NOTE! The degenerate case of "x ? : y", where we don't
1108 * have a true case, this will possibly promote "x" to the
1109 * same type as "y", and thus _change_ the conditional
1110 * test in the expression. But since promotion is "safe"
1111 * for testing, that's OK.
1113 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1115 struct expression
**true;
1116 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1118 const char * typediff
;
1121 if (!evaluate_conditional(expr
->conditional
, 0))
1123 if (!evaluate_expression(expr
->cond_false
))
1126 ctype
= degenerate(expr
->conditional
);
1127 rtype
= degenerate(expr
->cond_false
);
1129 true = &expr
->conditional
;
1131 if (expr
->cond_true
) {
1132 if (!evaluate_expression(expr
->cond_true
))
1134 ltype
= degenerate(expr
->cond_true
);
1135 true = &expr
->cond_true
;
1139 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1140 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1145 lclass
= classify_type(ltype
, <ype
);
1146 rclass
= classify_type(rtype
, &rtype
);
1147 if (lclass
& rclass
& TYPE_NUM
) {
1148 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1149 lclass
, rclass
, ltype
, rtype
);
1150 *true = cast_to(*true, ctype
);
1151 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1155 if ((lclass
| rclass
) & TYPE_PTR
) {
1156 int is_null1
= is_null_pointer_constant(*true);
1157 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1159 if (is_null1
&& is_null2
) {
1160 *true = cast_to(*true, &ptr_ctype
);
1161 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1165 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1168 *true = cast_to(*true, rtype
);
1172 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1174 bad_null(expr
->cond_false
);
1175 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1179 if (!(lclass
& rclass
& TYPE_PTR
)) {
1180 typediff
= "different types";
1183 /* OK, it's pointer on pointer */
1184 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1185 typediff
= "different address spaces";
1189 /* need to be lazier here */
1190 lbase
= examine_pointer_target(ltype
);
1191 rbase
= examine_pointer_target(rtype
);
1192 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1194 if (lbase
== &void_ctype
) {
1195 /* XXX: pointers to function should warn here */
1200 if (rbase
== &void_ctype
) {
1201 /* XXX: pointers to function should warn here */
1205 /* XXX: that should be pointer to composite */
1207 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1214 /* void on void, struct on same struct, union on same union */
1215 if (ltype
== rtype
) {
1219 typediff
= "different base types";
1222 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1226 expr
->ctype
= ctype
;
1230 if (qual
& ~ctype
->ctype
.modifiers
) {
1231 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1233 sym
->ctype
.modifiers
|= qual
;
1236 *true = cast_to(*true, ctype
);
1237 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1241 /* FP assignments can not do modulo or bit operations */
1242 static int compatible_float_op(int op
)
1244 return op
== SPECIAL_ADD_ASSIGN
||
1245 op
== SPECIAL_SUB_ASSIGN
||
1246 op
== SPECIAL_MUL_ASSIGN
||
1247 op
== SPECIAL_DIV_ASSIGN
;
1250 static int evaluate_assign_op(struct expression
*expr
)
1252 struct symbol
*target
= expr
->left
->ctype
;
1253 struct symbol
*source
= expr
->right
->ctype
;
1254 struct symbol
*t
, *s
;
1255 int tclass
= classify_type(target
, &t
);
1256 int sclass
= classify_type(source
, &s
);
1259 if (tclass
& sclass
& TYPE_NUM
) {
1260 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1261 expression_error(expr
, "invalid assignment");
1264 if (tclass
& TYPE_RESTRICT
) {
1265 if (!restricted_binop(op
, t
)) {
1266 warning(expr
->pos
, "bad assignment (%s) to %s",
1267 show_special(op
), show_typename(t
));
1268 expr
->right
= cast_to(expr
->right
, target
);
1271 /* allowed assignments unfoul */
1272 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1274 if (!restricted_value(expr
->right
, t
))
1276 } else if (!(sclass
& TYPE_RESTRICT
))
1278 /* source and target would better be identical restricted */
1281 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1282 info(expr
->pos
, " left side has type %s", show_typename(t
));
1283 info(expr
->pos
, " right side has type %s", show_typename(s
));
1284 expr
->right
= cast_to(expr
->right
, target
);
1287 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1288 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1289 unrestrict(expr
->right
, sclass
, &s
);
1290 evaluate_ptr_add(expr
, s
);
1293 expression_error(expr
, "invalid pointer assignment");
1297 expression_error(expr
, "invalid assignment");
1301 target
= usual_conversions(op
, expr
->left
, expr
->right
,
1302 tclass
, sclass
, target
, source
);
1304 expr
->right
= cast_to(expr
->right
, target
);
1308 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1311 return 0; /* yes, 0 - we don't want a cast_to here */
1312 if (t1
== &void_ctype
)
1314 if (t2
== &void_ctype
)
1316 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1318 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1322 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1324 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1329 static int check_assignment_types(struct symbol
*target
, struct expression
**rp
,
1330 const char **typediff
)
1332 struct symbol
*source
= degenerate(*rp
);
1333 struct symbol
*t
, *s
;
1334 int tclass
= classify_type(target
, &t
);
1335 int sclass
= classify_type(source
, &s
);
1337 if (tclass
& sclass
& TYPE_NUM
) {
1338 if (tclass
& TYPE_RESTRICT
) {
1339 /* allowed assignments unfoul */
1340 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1342 if (!restricted_value(*rp
, target
))
1346 } else if (!(sclass
& TYPE_RESTRICT
))
1348 if (t
== &bool_ctype
) {
1349 if (is_fouled_type(s
))
1350 warning((*rp
)->pos
, "%s degrades to integer",
1351 show_typename(s
->ctype
.base_type
));
1354 *typediff
= "different base types";
1358 if (tclass
== TYPE_PTR
) {
1359 unsigned long mod1
, mod2
;
1360 struct symbol
*b1
, *b2
;
1361 // NULL pointer is always OK
1362 int is_null
= is_null_pointer_constant(*rp
);
1368 if (!(sclass
& TYPE_PTR
)) {
1369 *typediff
= "different base types";
1372 b1
= examine_pointer_target(t
);
1373 b2
= examine_pointer_target(s
);
1374 mod1
= target_qualifiers(t
);
1375 mod2
= target_qualifiers(s
);
1376 if (whitelist_pointers(b1
, b2
)) {
1378 * assignments to/from void * are OK, provided that
1379 * we do not remove qualifiers from pointed to [C]
1380 * or mix address spaces [sparse].
1382 if (t
->ctype
.as
!= s
->ctype
.as
) {
1383 *typediff
= "different address spaces";
1387 * If this is a function pointer assignment, it is
1388 * actually fine to assign a pointer to const data to
1389 * it, as a function pointer points to const data
1390 * implicitly, i.e., dereferencing it does not produce
1393 if (b1
->type
== SYM_FN
)
1396 *typediff
= "different modifiers";
1401 /* It's OK if the target is more volatile or const than the source */
1402 *typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1408 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1411 if (tclass
& TYPE_NUM
) {
1412 /* XXX: need to turn into comparison with NULL */
1413 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1415 *typediff
= "different base types";
1418 *typediff
= "invalid types";
1422 *rp
= cast_to(*rp
, target
);
1426 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1427 struct expression
**rp
, const char *where
)
1429 const char *typediff
;
1430 struct symbol
*source
= degenerate(*rp
);
1432 if (!check_assignment_types(target
, rp
, &typediff
)) {
1433 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1434 info(expr
->pos
, " expected %s", show_typename(target
));
1435 info(expr
->pos
, " got %s", show_typename(source
));
1436 *rp
= cast_to(*rp
, target
);
1443 static int compatible_transparent_union(struct symbol
*target
,
1444 struct expression
**rp
)
1446 struct symbol
*t
, *member
;
1447 classify_type(target
, &t
);
1448 if (t
->type
!= SYM_UNION
|| !t
->transparent_union
)
1451 FOR_EACH_PTR(t
->symbol_list
, member
) {
1452 const char *typediff
;
1453 if (check_assignment_types(member
, rp
, &typediff
))
1455 } END_FOR_EACH_PTR(member
);
1460 static int compatible_argument_type(struct expression
*expr
, struct symbol
*target
,
1461 struct expression
**rp
, const char *where
)
1463 if (compatible_transparent_union(target
, rp
))
1466 return compatible_assignment_types(expr
, target
, rp
, where
);
1469 static void mark_assigned(struct expression
*expr
)
1475 switch (expr
->type
) {
1480 if (sym
->type
!= SYM_NODE
)
1482 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1486 mark_assigned(expr
->left
);
1487 mark_assigned(expr
->right
);
1490 case EXPR_FORCE_CAST
:
1491 mark_assigned(expr
->cast_expression
);
1494 mark_assigned(expr
->base
);
1502 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1504 if (type
->ctype
.modifiers
& MOD_CONST
)
1505 expression_error(left
, "assignment to const expression");
1507 /* We know left is an lvalue, so it's a "preop-*" */
1508 mark_assigned(left
->unop
);
1511 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1513 struct expression
*left
= expr
->left
;
1514 struct expression
*where
= expr
;
1515 struct symbol
*ltype
;
1517 if (!lvalue_expression(left
)) {
1518 expression_error(expr
, "not an lvalue");
1522 ltype
= left
->ctype
;
1524 if (expr
->op
!= '=') {
1525 if (!evaluate_assign_op(expr
))
1528 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1532 evaluate_assign_to(left
, ltype
);
1534 expr
->ctype
= ltype
;
1538 static void examine_fn_arguments(struct symbol
*fn
)
1542 FOR_EACH_PTR(fn
->arguments
, s
) {
1543 struct symbol
*arg
= evaluate_symbol(s
);
1544 /* Array/function arguments silently degenerate into pointers */
1550 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1551 if (arg
->type
== SYM_ARRAY
)
1552 ptr
->ctype
= arg
->ctype
;
1554 ptr
->ctype
.base_type
= arg
;
1555 ptr
->ctype
.as
|= s
->ctype
.as
;
1556 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1558 s
->ctype
.base_type
= ptr
;
1560 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1563 examine_symbol_type(s
);
1570 } END_FOR_EACH_PTR(s
);
1573 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1575 /* Take the modifiers of the pointer, and apply them to the member */
1576 mod
|= sym
->ctype
.modifiers
;
1577 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1578 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1580 newsym
->ctype
.as
= as
;
1581 newsym
->ctype
.modifiers
= mod
;
1587 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1589 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1590 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1592 node
->ctype
.base_type
= ptr
;
1593 ptr
->bit_size
= bits_in_pointer
;
1594 ptr
->ctype
.alignment
= pointer_alignment
;
1596 node
->bit_size
= bits_in_pointer
;
1597 node
->ctype
.alignment
= pointer_alignment
;
1600 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1601 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1602 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1604 if (sym
->type
== SYM_NODE
) {
1605 ptr
->ctype
.as
|= sym
->ctype
.as
;
1606 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1607 sym
= sym
->ctype
.base_type
;
1609 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1610 ptr
->ctype
.as
|= sym
->ctype
.as
;
1611 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1612 sym
= sym
->ctype
.base_type
;
1614 ptr
->ctype
.base_type
= sym
;
1619 /* Arrays degenerate into pointers on pointer arithmetic */
1620 static struct symbol
*degenerate(struct expression
*expr
)
1622 struct symbol
*ctype
, *base
;
1626 ctype
= expr
->ctype
;
1629 base
= examine_symbol_type(ctype
);
1630 if (ctype
->type
== SYM_NODE
)
1631 base
= ctype
->ctype
.base_type
;
1633 * Arrays degenerate into pointers to the entries, while
1634 * functions degenerate into pointers to themselves.
1635 * If array was part of non-lvalue compound, we create a copy
1636 * of that compound first and then act as if we were dealing with
1637 * the corresponding field in there.
1639 switch (base
->type
) {
1641 if (expr
->type
== EXPR_SLICE
) {
1642 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1643 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1645 a
->ctype
.base_type
= expr
->base
->ctype
;
1646 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1647 a
->array_size
= expr
->base
->ctype
->array_size
;
1649 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1651 e0
->ctype
= &lazy_ptr_ctype
;
1653 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1656 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1658 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1660 e2
->right
= expr
->base
;
1662 e2
->ctype
= expr
->base
->ctype
;
1664 if (expr
->r_bitpos
) {
1665 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1668 e3
->right
= alloc_const_expression(expr
->pos
,
1669 bits_to_bytes(expr
->r_bitpos
));
1670 e3
->ctype
= &lazy_ptr_ctype
;
1675 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1678 e4
->ctype
= &lazy_ptr_ctype
;
1681 expr
->type
= EXPR_PREOP
;
1685 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1686 expression_error(expr
, "strange non-value function or array");
1689 *expr
= *expr
->unop
;
1690 ctype
= create_pointer(expr
, ctype
, 1);
1691 expr
->ctype
= ctype
;
1698 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1700 struct expression
*op
= expr
->unop
;
1701 struct symbol
*ctype
;
1703 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1704 expression_error(expr
, "not addressable");
1711 if (expr
->type
== EXPR_SYMBOL
) {
1712 struct symbol
*sym
= expr
->symbol
;
1713 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1717 * symbol expression evaluation is lazy about the type
1718 * of the sub-expression, so we may have to generate
1719 * the type here if so..
1721 if (expr
->ctype
== &lazy_ptr_ctype
) {
1722 ctype
= create_pointer(expr
, ctype
, 0);
1723 expr
->ctype
= ctype
;
1729 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1731 struct expression
*op
= expr
->unop
;
1732 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1734 /* Simplify: *&(expr) => (expr) */
1735 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1741 examine_symbol_type(ctype
);
1743 /* Dereferencing a node drops all the node information. */
1744 if (ctype
->type
== SYM_NODE
)
1745 ctype
= ctype
->ctype
.base_type
;
1747 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1748 target
= ctype
->ctype
.base_type
;
1750 switch (ctype
->type
) {
1752 expression_error(expr
, "cannot dereference this type");
1755 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1756 merge_type(node
, ctype
);
1760 if (!lvalue_expression(op
)) {
1761 expression_error(op
, "non-lvalue array??");
1765 /* Do the implied "addressof" on the array */
1769 * When an array is dereferenced, we need to pick
1770 * up the attributes of the original node too..
1772 merge_type(node
, op
->ctype
);
1773 merge_type(node
, ctype
);
1777 node
->bit_size
= target
->bit_size
;
1778 node
->array_size
= target
->array_size
;
1785 * Unary post-ops: x++ and x--
1787 static struct symbol
*evaluate_postop(struct expression
*expr
)
1789 struct expression
*op
= expr
->unop
;
1790 struct symbol
*ctype
= op
->ctype
;
1791 int class = classify_type(ctype
, &ctype
);
1794 if (!class || class & TYPE_COMPOUND
) {
1795 expression_error(expr
, "need scalar for ++/--");
1798 if (!lvalue_expression(expr
->unop
)) {
1799 expression_error(expr
, "need lvalue expression for ++/--");
1803 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1804 unrestrict(expr
, class, &ctype
);
1806 if (class & TYPE_NUM
) {
1808 } else if (class == TYPE_PTR
) {
1809 struct symbol
*target
= examine_pointer_target(ctype
);
1810 if (!is_function(target
))
1811 multiply
= bits_to_bytes(target
->bit_size
);
1815 evaluate_assign_to(op
, op
->ctype
);
1816 expr
->op_value
= multiply
;
1817 expr
->ctype
= ctype
;
1821 expression_error(expr
, "bad argument type for ++/--");
1825 static struct symbol
*evaluate_sign(struct expression
*expr
)
1827 struct symbol
*ctype
= expr
->unop
->ctype
;
1828 int class = classify_type(ctype
, &ctype
);
1829 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1831 /* should be an arithmetic type */
1832 if (!(class & TYPE_NUM
))
1833 return bad_expr_type(expr
);
1834 if (class & TYPE_RESTRICT
)
1837 if (!(class & TYPE_FLOAT
)) {
1838 ctype
= integer_promotion(ctype
);
1839 expr
->unop
= cast_to(expr
->unop
, ctype
);
1840 } else if (expr
->op
!= '~') {
1841 /* no conversions needed */
1843 return bad_expr_type(expr
);
1845 if (expr
->op
== '+')
1846 *expr
= *expr
->unop
;
1847 expr
->ctype
= ctype
;
1850 if (restricted_unop(expr
->op
, &ctype
))
1851 unrestrict(expr
, class, &ctype
);
1855 static struct symbol
*evaluate_preop(struct expression
*expr
)
1857 struct symbol
*ctype
= expr
->unop
->ctype
;
1861 *expr
= *expr
->unop
;
1867 return evaluate_sign(expr
);
1870 return evaluate_dereference(expr
);
1873 return evaluate_addressof(expr
);
1875 case SPECIAL_INCREMENT
:
1876 case SPECIAL_DECREMENT
:
1878 * From a type evaluation standpoint the preops are
1879 * the same as the postops
1881 return evaluate_postop(expr
);
1884 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1886 if (is_safe_type(ctype
))
1887 warning(expr
->pos
, "testing a 'safe expression'");
1888 if (is_float_type(ctype
)) {
1889 struct expression
*arg
= expr
->unop
;
1890 expr
->type
= EXPR_COMPARE
;
1891 expr
->op
= SPECIAL_EQUAL
;
1893 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1894 expr
->right
->ctype
= ctype
;
1895 expr
->right
->fvalue
= 0;
1896 } else if (is_fouled_type(ctype
)) {
1897 warning(expr
->pos
, "%s degrades to integer",
1898 show_typename(ctype
->ctype
.base_type
));
1900 /* the result is int [6.5.3.3(5)]*/
1907 expr
->ctype
= ctype
;
1911 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1913 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1914 struct ptr_list
*list
= head
;
1920 for (i
= 0; i
< list
->nr
; i
++) {
1921 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1923 if (sym
->ident
!= ident
)
1925 *offset
= sym
->offset
;
1928 struct symbol
*ctype
= sym
->ctype
.base_type
;
1932 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1934 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1937 *offset
+= sym
->offset
;
1941 } while ((list
= list
->next
) != head
);
1945 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1947 struct expression
*add
;
1950 * Create a new add-expression
1952 * NOTE! Even if we just add zero, we need a new node
1953 * for the member pointer, since it has a different
1954 * type than the original pointer. We could make that
1955 * be just a cast, but the fact is, a node is a node,
1956 * so we might as well just do the "add zero" here.
1958 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1961 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1962 add
->right
->ctype
= &int_ctype
;
1963 add
->right
->value
= offset
;
1966 * The ctype of the pointer will be lazily evaluated if
1967 * we ever take the address of this member dereference..
1969 add
->ctype
= &lazy_ptr_ctype
;
1973 /* structure/union dereference */
1974 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1977 struct symbol
*ctype
, *member
;
1978 struct expression
*deref
= expr
->deref
, *add
;
1979 struct ident
*ident
= expr
->member
;
1983 if (!evaluate_expression(deref
))
1986 expression_error(expr
, "bad member name");
1990 ctype
= deref
->ctype
;
1991 examine_symbol_type(ctype
);
1992 address_space
= ctype
->ctype
.as
;
1993 mod
= ctype
->ctype
.modifiers
;
1994 if (ctype
->type
== SYM_NODE
) {
1995 ctype
= ctype
->ctype
.base_type
;
1996 address_space
|= ctype
->ctype
.as
;
1997 mod
|= ctype
->ctype
.modifiers
;
1999 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
2000 expression_error(expr
, "expected structure or union");
2004 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
2006 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
2007 const char *name
= "<unnamed>";
2010 name
= ctype
->ident
->name
;
2011 namelen
= ctype
->ident
->len
;
2013 if (ctype
->symbol_list
)
2014 expression_error(expr
, "no member '%s' in %s %.*s",
2015 show_ident(ident
), type
, namelen
, name
);
2017 expression_error(expr
, "using member '%s' in "
2018 "incomplete %s %.*s", show_ident(ident
),
2019 type
, namelen
, name
);
2024 * The member needs to take on the address space and modifiers of
2025 * the "parent" type.
2027 member
= convert_to_as_mod(member
, address_space
, mod
);
2028 ctype
= get_base_type(member
);
2030 if (!lvalue_expression(deref
)) {
2031 if (deref
->type
!= EXPR_SLICE
) {
2035 expr
->base
= deref
->base
;
2036 expr
->r_bitpos
= deref
->r_bitpos
;
2038 expr
->r_bitpos
+= bytes_to_bits(offset
);
2039 expr
->type
= EXPR_SLICE
;
2040 expr
->r_nrbits
= member
->bit_size
;
2041 expr
->r_bitpos
+= member
->bit_offset
;
2042 expr
->ctype
= member
;
2046 deref
= deref
->unop
;
2047 expr
->deref
= deref
;
2049 add
= evaluate_offset(deref
, offset
);
2050 expr
->type
= EXPR_PREOP
;
2054 expr
->ctype
= member
;
2058 static int is_promoted(struct expression
*expr
)
2061 switch (expr
->type
) {
2064 case EXPR_CONDITIONAL
:
2088 static struct symbol
*evaluate_cast(struct expression
*);
2090 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2092 struct symbol
*sym
= expr
->cast_type
;
2094 sym
= evaluate_expression(expr
->cast_expression
);
2098 * Expressions of restricted types will possibly get
2099 * promoted - check that here
2101 if (is_restricted_type(sym
)) {
2102 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2104 } else if (is_fouled_type(sym
)) {
2108 examine_symbol_type(sym
);
2109 if (is_bitfield_type(sym
)) {
2110 expression_error(expr
, "trying to examine bitfield type");
2116 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2118 struct symbol
*type
;
2121 type
= evaluate_type_information(expr
);
2125 size
= type
->bit_size
;
2127 if (size
< 0 && is_void_type(type
)) {
2128 warning(expr
->pos
, "expression using sizeof(void)");
2129 size
= bits_in_char
;
2132 if (size
== 1 && is_bool_type(type
)) {
2134 warning(expr
->pos
, "expression using sizeof bool");
2135 size
= bits_in_char
;
2138 if (is_function(type
->ctype
.base_type
)) {
2139 warning(expr
->pos
, "expression using sizeof on a function");
2140 size
= bits_in_char
;
2143 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2144 expression_error(expr
, "cannot size expression");
2146 expr
->type
= EXPR_VALUE
;
2147 expr
->value
= bits_to_bytes(size
);
2149 expr
->ctype
= size_t_ctype
;
2150 return size_t_ctype
;
2153 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2155 struct symbol
*type
;
2158 type
= evaluate_type_information(expr
);
2162 if (type
->type
== SYM_NODE
)
2163 type
= type
->ctype
.base_type
;
2166 switch (type
->type
) {
2170 type
= get_base_type(type
);
2174 expression_error(expr
, "expected pointer expression");
2177 size
= type
->bit_size
;
2178 if (size
& (bits_in_char
-1))
2180 expr
->type
= EXPR_VALUE
;
2181 expr
->value
= bits_to_bytes(size
);
2183 expr
->ctype
= size_t_ctype
;
2184 return size_t_ctype
;
2187 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2189 struct symbol
*type
;
2191 type
= evaluate_type_information(expr
);
2195 expr
->type
= EXPR_VALUE
;
2196 expr
->value
= type
->ctype
.alignment
;
2198 expr
->ctype
= size_t_ctype
;
2199 return size_t_ctype
;
2202 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
2204 struct expression
*expr
;
2205 struct symbol_list
*argument_types
= fn
->arguments
;
2206 struct symbol
*argtype
;
2209 PREPARE_PTR_LIST(argument_types
, argtype
);
2210 FOR_EACH_PTR (head
, expr
) {
2211 struct expression
**p
= THIS_ADDRESS(expr
);
2212 struct symbol
*ctype
, *target
;
2213 ctype
= evaluate_expression(expr
);
2220 struct symbol
*type
;
2221 int class = classify_type(ctype
, &type
);
2222 if (is_int(class)) {
2223 *p
= cast_to(expr
, integer_promotion(type
));
2224 } else if (class & TYPE_FLOAT
) {
2225 unsigned long mod
= type
->ctype
.modifiers
;
2226 if (!(mod
& (MOD_LONG_ALL
)))
2227 *p
= cast_to(expr
, &double_ctype
);
2228 } else if (class & TYPE_PTR
) {
2229 if (expr
->ctype
== &null_ctype
)
2230 *p
= cast_to(expr
, &ptr_ctype
);
2234 } else if (!target
->forced_arg
){
2235 static char where
[30];
2236 examine_symbol_type(target
);
2237 sprintf(where
, "argument %d", i
);
2238 compatible_argument_type(expr
, target
, p
, where
);
2242 NEXT_PTR_LIST(argtype
);
2243 } END_FOR_EACH_PTR(expr
);
2244 FINISH_PTR_LIST(argtype
);
2248 static void convert_index(struct expression
*e
)
2250 struct expression
*child
= e
->idx_expression
;
2251 unsigned from
= e
->idx_from
;
2252 unsigned to
= e
->idx_to
+ 1;
2254 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2255 e
->init_nr
= to
- from
;
2256 e
->init_expr
= child
;
2259 static void convert_ident(struct expression
*e
)
2261 struct expression
*child
= e
->ident_expression
;
2262 int offset
= e
->offset
;
2265 e
->init_offset
= offset
;
2267 e
->init_expr
= child
;
2270 static void convert_designators(struct expression
*e
)
2273 if (e
->type
== EXPR_INDEX
)
2275 else if (e
->type
== EXPR_IDENTIFIER
)
2283 static void excess(struct expression
*e
, const char *s
)
2285 warning(e
->pos
, "excessive elements in %s initializer", s
);
2289 * implicit designator for the first element
2291 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2292 struct expression
**v
)
2294 struct expression
*e
= *v
, *new;
2296 if (ctype
->type
== SYM_NODE
)
2297 ctype
= ctype
->ctype
.base_type
;
2299 if (class & TYPE_PTR
) { /* array */
2300 if (!ctype
->bit_size
)
2302 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2303 new->idx_expression
= e
;
2304 new->ctype
= ctype
->ctype
.base_type
;
2306 struct symbol
*field
, *p
;
2307 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2308 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2314 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2315 new->ident_expression
= e
;
2316 new->field
= new->ctype
= field
;
2317 new->offset
= field
->offset
;
2324 * sanity-check explicit designators; return the innermost one or NULL
2325 * in case of error. Assign types.
2327 static struct expression
*check_designators(struct expression
*e
,
2328 struct symbol
*ctype
)
2330 struct expression
*last
= NULL
;
2333 if (ctype
->type
== SYM_NODE
)
2334 ctype
= ctype
->ctype
.base_type
;
2335 if (e
->type
== EXPR_INDEX
) {
2336 struct symbol
*type
;
2337 if (ctype
->type
!= SYM_ARRAY
) {
2338 err
= "array index in non-array";
2341 type
= ctype
->ctype
.base_type
;
2342 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2343 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2344 if (offset
>= ctype
->bit_size
) {
2345 err
= "index out of bounds in";
2349 e
->ctype
= ctype
= type
;
2352 if (!e
->idx_expression
) {
2356 e
= e
->idx_expression
;
2357 } else if (e
->type
== EXPR_IDENTIFIER
) {
2359 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2360 err
= "field name not in struct or union";
2363 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2365 err
= "unknown field name in";
2369 e
->field
= e
->ctype
= ctype
;
2371 if (!e
->ident_expression
) {
2375 e
= e
->ident_expression
;
2376 } else if (e
->type
== EXPR_POS
) {
2377 err
= "internal front-end error: EXPR_POS in";
2382 expression_error(e
, "%s initializer", err
);
2387 * choose the next subobject to initialize.
2389 * Get designators for next element, switch old ones to EXPR_POS.
2390 * Return the resulting expression or NULL if we'd run out of subobjects.
2391 * The innermost designator is returned in *v. Designators in old
2392 * are assumed to be already sanity-checked.
2394 static struct expression
*next_designators(struct expression
*old
,
2395 struct symbol
*ctype
,
2396 struct expression
*e
, struct expression
**v
)
2398 struct expression
*new = NULL
;
2402 if (old
->type
== EXPR_INDEX
) {
2403 struct expression
*copy
;
2406 copy
= next_designators(old
->idx_expression
,
2409 n
= old
->idx_to
+ 1;
2410 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2415 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2418 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2421 new->idx_from
= new->idx_to
= n
;
2422 new->idx_expression
= copy
;
2423 new->ctype
= old
->ctype
;
2425 } else if (old
->type
== EXPR_IDENTIFIER
) {
2426 struct expression
*copy
;
2427 struct symbol
*field
;
2430 copy
= next_designators(old
->ident_expression
,
2433 field
= old
->field
->next_subobject
;
2439 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2441 * We can't necessarily trust "field->offset",
2442 * because the field might be in an anonymous
2443 * union, and the field offset is then the offset
2444 * within that union.
2446 * The "old->offset - old->field->offset"
2447 * would be the offset of such an anonymous
2450 offset
= old
->offset
- old
->field
->offset
;
2453 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2457 new->expr_ident
= field
->ident
;
2458 new->ident_expression
= copy
;
2460 new->offset
= field
->offset
+ offset
;
2466 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2467 int class, struct symbol
*ctype
);
2470 * deal with traversing subobjects [6.7.8(17,18,20)]
2472 static void handle_list_initializer(struct expression
*expr
,
2473 int class, struct symbol
*ctype
)
2475 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2478 FOR_EACH_PTR(expr
->expr_list
, e
) {
2479 struct expression
**v
;
2480 struct symbol
*type
;
2483 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2484 struct symbol
*struct_sym
;
2487 last
= first_subobject(ctype
, class, &top
);
2489 last
= next_designators(last
, ctype
, e
, &top
);
2492 excess(e
, class & TYPE_PTR
? "array" :
2494 DELETE_CURRENT_PTR(e
);
2497 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2498 if (Wdesignated_init
&& struct_sym
->designated_init
)
2499 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2500 ctype
->ident
? "in initializer for " : "",
2501 ctype
->ident
? ctype
->ident
->len
: 0,
2502 ctype
->ident
? ctype
->ident
->name
: "",
2503 ctype
->ident
? ": " : "",
2504 get_type_name(struct_sym
->type
),
2505 show_ident(struct_sym
->ident
));
2507 warning(e
->pos
, "advancing past deep designator");
2510 REPLACE_CURRENT_PTR(e
, last
);
2512 next
= check_designators(e
, ctype
);
2514 DELETE_CURRENT_PTR(e
);
2518 /* deeper than one designator? */
2520 convert_designators(last
);
2525 lclass
= classify_type(top
->ctype
, &type
);
2526 if (top
->type
== EXPR_INDEX
)
2527 v
= &top
->idx_expression
;
2529 v
= &top
->ident_expression
;
2531 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2534 if (!(lclass
& TYPE_COMPOUND
)) {
2535 warning(e
->pos
, "bogus scalar initializer");
2536 DELETE_CURRENT_PTR(e
);
2540 next
= first_subobject(type
, lclass
, v
);
2542 warning(e
->pos
, "missing braces around initializer");
2547 DELETE_CURRENT_PTR(e
);
2548 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2550 } END_FOR_EACH_PTR(e
);
2552 convert_designators(last
);
2553 expr
->ctype
= ctype
;
2556 static int is_string_literal(struct expression
**v
)
2558 struct expression
*e
= *v
;
2559 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2561 if (!e
|| e
->type
!= EXPR_STRING
)
2563 if (e
!= *v
&& Wparen_string
)
2565 "array initialized from parenthesized string constant");
2571 * We want a normal expression, possibly in one layer of braces. Warn
2572 * if the latter happens inside a list (it's legal, but likely to be
2573 * an effect of screwup). In case of anything not legal, we are definitely
2574 * having an effect of screwup, so just fail and let the caller warn.
2576 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2578 struct expression
*v
= NULL
, *p
;
2582 if (e
->type
!= EXPR_INITIALIZER
)
2585 FOR_EACH_PTR(e
->expr_list
, p
) {
2589 } END_FOR_EACH_PTR(p
);
2593 case EXPR_INITIALIZER
:
2595 case EXPR_IDENTIFIER
:
2601 warning(e
->pos
, "braces around scalar initializer");
2606 * deal with the cases that don't care about subobjects:
2607 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2608 * character array <- string literal, possibly in braces [6.7.8(14)]
2609 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2610 * compound type <- initializer list in braces [6.7.8(16)]
2611 * The last one punts to handle_list_initializer() which, in turn will call
2612 * us for individual elements of the list.
2614 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2615 * the lack of support of wide char stuff in general.
2617 * One note: we need to take care not to evaluate a string literal until
2618 * we know that we *will* handle it right here. Otherwise we would screw
2619 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2620 * { "string", ...} - we need to preserve that string literal recognizable
2621 * until we dig into the inner struct.
2623 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2624 int class, struct symbol
*ctype
)
2626 int is_string
= is_string_type(ctype
);
2627 struct expression
*e
= *ep
, *p
;
2628 struct symbol
*type
;
2634 if (!(class & TYPE_COMPOUND
)) {
2635 e
= handle_scalar(e
, nested
);
2639 if (!evaluate_expression(e
))
2641 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2646 * sublist; either a string, or we dig in; the latter will deal with
2647 * pathologies, so we don't need anything fancy here.
2649 if (e
->type
== EXPR_INITIALIZER
) {
2651 struct expression
*v
= NULL
;
2654 FOR_EACH_PTR(e
->expr_list
, p
) {
2658 } END_FOR_EACH_PTR(p
);
2659 if (count
== 1 && is_string_literal(&v
)) {
2664 handle_list_initializer(e
, class, ctype
);
2669 if (is_string_literal(&e
)) {
2670 /* either we are doing array of char, or we'll have to dig in */
2677 /* struct or union can be initialized by compatible */
2678 if (class != TYPE_COMPOUND
)
2680 type
= evaluate_expression(e
);
2683 if (ctype
->type
== SYM_NODE
)
2684 ctype
= ctype
->ctype
.base_type
;
2685 if (type
->type
== SYM_NODE
)
2686 type
= type
->ctype
.base_type
;
2692 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2694 type
= evaluate_expression(p
);
2695 if (ctype
->bit_size
!= -1) {
2696 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2698 "too long initializer-string for array of char");
2699 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2701 "too long initializer-string for array of char(no space for nul char)");
2708 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2710 struct symbol
*type
;
2711 int class = classify_type(ctype
, &type
);
2712 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2713 expression_error(*ep
, "invalid initializer");
2716 static struct symbol
*cast_to_bool(struct expression
*expr
)
2718 struct expression
*old
= expr
->cast_expression
;
2719 struct expression
*zero
;
2720 struct symbol
*otype
;
2721 int oclass
= classify_type(degenerate(old
), &otype
);
2722 struct symbol
*ctype
;
2724 if (oclass
& TYPE_COMPOUND
)
2727 zero
= alloc_const_expression(expr
->pos
, 0);
2728 expr
->op
= SPECIAL_NOTEQUAL
;
2729 ctype
= usual_conversions(expr
->op
, old
, zero
,
2730 oclass
, TYPE_NUM
, otype
, zero
->ctype
);
2731 expr
->type
= EXPR_COMPARE
;
2732 expr
->left
= cast_to(old
, ctype
);
2733 expr
->right
= cast_to(zero
, ctype
);
2738 static struct symbol
*evaluate_cast(struct expression
*expr
)
2740 struct expression
*target
= expr
->cast_expression
;
2741 struct symbol
*ctype
;
2742 struct symbol
*t1
, *t2
;
2744 int as1
= 0, as2
= 0;
2750 * Special case: a cast can be followed by an
2751 * initializer, in which case we need to pass
2752 * the type value down to that initializer rather
2753 * than trying to evaluate it as an expression
2755 * A more complex case is when the initializer is
2756 * dereferenced as part of a post-fix expression.
2757 * We need to produce an expression that can be dereferenced.
2759 if (target
->type
== EXPR_INITIALIZER
) {
2760 struct symbol
*sym
= expr
->cast_type
;
2761 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2763 sym
->initializer
= target
;
2764 evaluate_symbol(sym
);
2766 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2769 expr
->type
= EXPR_PREOP
;
2777 ctype
= examine_symbol_type(expr
->cast_type
);
2778 expr
->ctype
= ctype
;
2779 expr
->cast_type
= ctype
;
2781 evaluate_expression(target
);
2784 class1
= classify_type(ctype
, &t1
);
2786 /* cast to non-integer type -> not an integer constant expression */
2787 if (!is_int(class1
))
2789 /* if argument turns out to be not an integer constant expression *and*
2790 it was not a floating literal to start with -> too bad */
2791 else if (expr
->flags
== Int_const_expr
&&
2792 !(target
->flags
& Int_const_expr
))
2795 * You can always throw a value away by casting to
2796 * "void" - that's an implicit "force". Note that
2797 * the same is _not_ true of "void *".
2799 if (t1
== &void_ctype
)
2802 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2803 warning(expr
->pos
, "cast to non-scalar");
2807 expression_error(expr
, "cast from unknown type");
2810 class2
= classify_type(t2
, &t2
);
2812 if (class2
& TYPE_COMPOUND
)
2813 warning(expr
->pos
, "cast from non-scalar");
2815 if (expr
->type
== EXPR_FORCE_CAST
)
2818 /* allowed cast unfouls */
2819 if (class2
& TYPE_FOULED
)
2823 if ((class1
& TYPE_RESTRICT
) && restricted_value(target
, t1
))
2824 warning(expr
->pos
, "cast to %s",
2826 if (class2
& TYPE_RESTRICT
) {
2827 if (t1
== &bool_ctype
) {
2828 if (class2
& TYPE_FOULED
)
2829 warning(expr
->pos
, "%s degrades to integer",
2832 warning(expr
->pos
, "cast from %s",
2838 if (t1
== &ulong_ctype
)
2840 else if (class1
== TYPE_PTR
) {
2841 examine_pointer_target(t1
);
2845 if (t2
== &ulong_ctype
)
2847 else if (class2
== TYPE_PTR
) {
2848 examine_pointer_target(t2
);
2852 if (!as1
&& as2
> 0)
2853 warning(expr
->pos
, "cast removes address space of expression");
2854 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2855 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2856 if (as1
> 0 && !as2
&&
2857 !is_null_pointer_constant(target
) && Wcast_to_as
)
2859 "cast adds address space to expression (<asn:%d>)", as1
);
2861 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2862 !as1
&& (target
->flags
& Int_const_expr
)) {
2863 if (t1
->ctype
.base_type
== &void_ctype
) {
2864 if (is_zero_constant(target
)) {
2866 expr
->type
= EXPR_VALUE
;
2867 expr
->ctype
= &null_ctype
;
2874 if (t1
== &bool_ctype
)
2882 * Evaluate a call expression with a symbol. This
2883 * should expand inline functions, and evaluate
2886 static int evaluate_symbol_call(struct expression
*expr
)
2888 struct expression
*fn
= expr
->fn
;
2889 struct symbol
*ctype
= fn
->ctype
;
2891 if (fn
->type
!= EXPR_PREOP
)
2894 if (ctype
->op
&& ctype
->op
->evaluate
)
2895 return ctype
->op
->evaluate(expr
);
2897 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2899 struct symbol
*curr
= current_fn
;
2901 if (ctype
->definition
)
2902 ctype
= ctype
->definition
;
2904 current_fn
= ctype
->ctype
.base_type
;
2906 ret
= inline_function(expr
, ctype
);
2908 /* restore the old function */
2916 static struct symbol
*evaluate_call(struct expression
*expr
)
2919 struct symbol
*ctype
, *sym
;
2920 struct expression
*fn
= expr
->fn
;
2921 struct expression_list
*arglist
= expr
->args
;
2923 if (!evaluate_expression(fn
))
2925 sym
= ctype
= fn
->ctype
;
2926 if (ctype
->type
== SYM_NODE
)
2927 ctype
= ctype
->ctype
.base_type
;
2928 if (ctype
->type
== SYM_PTR
)
2929 ctype
= get_base_type(ctype
);
2931 if (ctype
->type
!= SYM_FN
) {
2932 struct expression
*arg
;
2933 expression_error(expr
, "not a function %s",
2934 show_ident(sym
->ident
));
2935 /* do typechecking in arguments */
2936 FOR_EACH_PTR (arglist
, arg
) {
2937 evaluate_expression(arg
);
2938 } END_FOR_EACH_PTR(arg
);
2942 examine_fn_arguments(ctype
);
2943 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2944 sym
->op
&& sym
->op
->args
) {
2945 if (!sym
->op
->args(expr
))
2948 if (!evaluate_arguments(ctype
, arglist
))
2950 args
= expression_list_size(expr
->args
);
2951 fnargs
= symbol_list_size(ctype
->arguments
);
2953 expression_error(expr
,
2954 "not enough arguments for function %s",
2955 show_ident(sym
->ident
));
2956 if (args
> fnargs
&& !ctype
->variadic
)
2957 expression_error(expr
,
2958 "too many arguments for function %s",
2959 show_ident(sym
->ident
));
2961 if (sym
->type
== SYM_NODE
) {
2962 if (evaluate_symbol_call(expr
))
2965 expr
->ctype
= ctype
->ctype
.base_type
;
2969 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2971 struct expression
*e
= expr
->down
;
2972 struct symbol
*ctype
= expr
->in
;
2975 if (expr
->op
== '.') {
2976 struct symbol
*field
;
2979 expression_error(expr
, "expected structure or union");
2982 examine_symbol_type(ctype
);
2983 class = classify_type(ctype
, &ctype
);
2984 if (class != TYPE_COMPOUND
) {
2985 expression_error(expr
, "expected structure or union");
2989 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2991 expression_error(expr
, "unknown member");
2995 expr
->type
= EXPR_VALUE
;
2996 expr
->flags
= Int_const_expr
;
2997 expr
->value
= offset
;
2999 expr
->ctype
= size_t_ctype
;
3002 expression_error(expr
, "expected structure or union");
3005 examine_symbol_type(ctype
);
3006 class = classify_type(ctype
, &ctype
);
3007 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
3008 expression_error(expr
, "expected array");
3011 ctype
= ctype
->ctype
.base_type
;
3013 expr
->type
= EXPR_VALUE
;
3014 expr
->flags
= Int_const_expr
;
3017 expr
->ctype
= size_t_ctype
;
3019 struct expression
*idx
= expr
->index
, *m
;
3020 struct symbol
*i_type
= evaluate_expression(idx
);
3021 int i_class
= classify_type(i_type
, &i_type
);
3022 if (!is_int(i_class
)) {
3023 expression_error(expr
, "non-integer index");
3026 unrestrict(idx
, i_class
, &i_type
);
3027 idx
= cast_to(idx
, size_t_ctype
);
3028 m
= alloc_const_expression(expr
->pos
,
3029 bits_to_bytes(ctype
->bit_size
));
3030 m
->ctype
= size_t_ctype
;
3031 m
->flags
= Int_const_expr
;
3032 expr
->type
= EXPR_BINOP
;
3036 expr
->ctype
= size_t_ctype
;
3037 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
3041 struct expression
*copy
= __alloc_expression(0);
3043 if (e
->type
== EXPR_OFFSETOF
)
3045 if (!evaluate_expression(e
))
3047 expr
->type
= EXPR_BINOP
;
3048 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
3050 expr
->ctype
= size_t_ctype
;
3054 return size_t_ctype
;
3057 struct symbol
*evaluate_expression(struct expression
*expr
)
3064 switch (expr
->type
) {
3067 expression_error(expr
, "value expression without a type");
3070 return evaluate_string(expr
);
3072 return evaluate_symbol_expression(expr
);
3074 if (!evaluate_expression(expr
->left
))
3076 if (!evaluate_expression(expr
->right
))
3078 return evaluate_binop(expr
);
3080 return evaluate_logical(expr
);
3082 evaluate_expression(expr
->left
);
3083 if (!evaluate_expression(expr
->right
))
3085 return evaluate_comma(expr
);
3087 if (!evaluate_expression(expr
->left
))
3089 if (!evaluate_expression(expr
->right
))
3091 return evaluate_compare(expr
);
3092 case EXPR_ASSIGNMENT
:
3093 if (!evaluate_expression(expr
->left
))
3095 if (!evaluate_expression(expr
->right
))
3097 return evaluate_assignment(expr
);
3099 if (!evaluate_expression(expr
->unop
))
3101 return evaluate_preop(expr
);
3103 if (!evaluate_expression(expr
->unop
))
3105 return evaluate_postop(expr
);
3107 case EXPR_FORCE_CAST
:
3108 case EXPR_IMPLIED_CAST
:
3109 return evaluate_cast(expr
);
3111 return evaluate_sizeof(expr
);
3112 case EXPR_PTRSIZEOF
:
3113 return evaluate_ptrsizeof(expr
);
3115 return evaluate_alignof(expr
);
3117 return evaluate_member_dereference(expr
);
3119 return evaluate_call(expr
);
3121 case EXPR_CONDITIONAL
:
3122 return evaluate_conditional_expression(expr
);
3123 case EXPR_STATEMENT
:
3124 expr
->ctype
= evaluate_statement(expr
->statement
);
3128 expr
->ctype
= &ptr_ctype
;
3132 /* Evaluate the type of the symbol .. */
3133 evaluate_symbol(expr
->symbol
);
3134 /* .. but the type of the _expression_ is a "type" */
3135 expr
->ctype
= &type_ctype
;
3139 return evaluate_offsetof(expr
);
3141 /* These can not exist as stand-alone expressions */
3142 case EXPR_INITIALIZER
:
3143 case EXPR_IDENTIFIER
:
3146 expression_error(expr
, "internal front-end error: initializer in expression");
3149 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3155 static void check_duplicates(struct symbol
*sym
)
3158 struct symbol
*next
= sym
;
3159 int initialized
= sym
->initializer
!= NULL
;
3161 while ((next
= next
->same_symbol
) != NULL
) {
3162 const char *typediff
;
3163 evaluate_symbol(next
);
3164 if (initialized
&& next
->initializer
) {
3165 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3166 show_ident(sym
->ident
),
3167 stream_name(next
->pos
.stream
), next
->pos
.line
);
3168 /* Only warn once */
3172 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3174 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3175 show_ident(sym
->ident
),
3176 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3181 unsigned long mod
= sym
->ctype
.modifiers
;
3182 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3184 if (!(mod
& MOD_TOPLEVEL
))
3188 if (sym
->ident
== &main_ident
)
3190 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3194 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3196 struct symbol
*base_type
;
3204 sym
= examine_symbol_type(sym
);
3205 base_type
= get_base_type(sym
);
3209 /* Evaluate the initializers */
3210 if (sym
->initializer
)
3211 evaluate_initializer(sym
, &sym
->initializer
);
3213 /* And finally, evaluate the body of the symbol too */
3214 if (base_type
->type
== SYM_FN
) {
3215 struct symbol
*curr
= current_fn
;
3217 if (sym
->definition
&& sym
->definition
!= sym
)
3218 return evaluate_symbol(sym
->definition
);
3220 current_fn
= base_type
;
3222 examine_fn_arguments(base_type
);
3223 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3225 if (base_type
->stmt
)
3226 evaluate_statement(base_type
->stmt
);
3234 void evaluate_symbol_list(struct symbol_list
*list
)
3238 FOR_EACH_PTR(list
, sym
) {
3239 has_error
&= ~ERROR_CURR_PHASE
;
3240 evaluate_symbol(sym
);
3241 check_duplicates(sym
);
3242 } END_FOR_EACH_PTR(sym
);
3245 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3247 struct expression
*expr
= stmt
->expression
;
3248 struct symbol
*fntype
;
3250 evaluate_expression(expr
);
3251 fntype
= current_fn
->ctype
.base_type
;
3252 if (!fntype
|| fntype
== &void_ctype
) {
3253 if (expr
&& expr
->ctype
!= &void_ctype
)
3254 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3255 if (expr
&& Wreturn_void
)
3256 warning(stmt
->pos
, "returning void-valued expression");
3261 sparse_error(stmt
->pos
, "return with no return value");
3266 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3270 static void evaluate_if_statement(struct statement
*stmt
)
3272 if (!stmt
->if_conditional
)
3275 evaluate_conditional(stmt
->if_conditional
, 0);
3276 evaluate_statement(stmt
->if_true
);
3277 evaluate_statement(stmt
->if_false
);
3280 static void evaluate_iterator(struct statement
*stmt
)
3282 evaluate_symbol_list(stmt
->iterator_syms
);
3283 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3284 evaluate_conditional(stmt
->iterator_post_condition
,1);
3285 evaluate_statement(stmt
->iterator_pre_statement
);
3286 evaluate_statement(stmt
->iterator_statement
);
3287 evaluate_statement(stmt
->iterator_post_statement
);
3290 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3292 switch (*constraint
) {
3293 case '=': /* Assignment */
3294 case '+': /* Update */
3297 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3301 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3303 switch (*constraint
) {
3304 case '=': /* Assignment */
3305 case '+': /* Update */
3306 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3310 static void evaluate_asm_statement(struct statement
*stmt
)
3312 struct expression
*expr
;
3316 expr
= stmt
->asm_string
;
3317 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3318 sparse_error(stmt
->pos
, "need constant string for inline asm");
3323 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3325 case 0: /* Identifier */
3329 case 1: /* Constraint */
3331 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3332 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3333 *THIS_ADDRESS(expr
) = NULL
;
3336 verify_output_constraint(expr
, expr
->string
->data
);
3339 case 2: /* Expression */
3341 if (!evaluate_expression(expr
))
3343 if (!lvalue_expression(expr
))
3344 warning(expr
->pos
, "asm output is not an lvalue");
3345 evaluate_assign_to(expr
, expr
->ctype
);
3348 } END_FOR_EACH_PTR(expr
);
3351 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3353 case 0: /* Identifier */
3357 case 1: /* Constraint */
3359 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3360 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3361 *THIS_ADDRESS(expr
) = NULL
;
3364 verify_input_constraint(expr
, expr
->string
->data
);
3367 case 2: /* Expression */
3369 if (!evaluate_expression(expr
))
3373 } END_FOR_EACH_PTR(expr
);
3375 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3377 sparse_error(stmt
->pos
, "bad asm clobbers");
3380 if (expr
->type
== EXPR_STRING
)
3382 expression_error(expr
, "asm clobber is not a string");
3383 } END_FOR_EACH_PTR(expr
);
3385 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3386 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3387 sparse_error(stmt
->pos
, "bad asm label");
3390 } END_FOR_EACH_PTR(sym
);
3393 static void evaluate_case_statement(struct statement
*stmt
)
3395 evaluate_expression(stmt
->case_expression
);
3396 evaluate_expression(stmt
->case_to
);
3397 evaluate_statement(stmt
->case_statement
);
3400 static void check_case_type(struct expression
*switch_expr
,
3401 struct expression
*case_expr
,
3402 struct expression
**enumcase
)
3404 struct symbol
*switch_type
, *case_type
;
3410 switch_type
= switch_expr
->ctype
;
3411 case_type
= evaluate_expression(case_expr
);
3413 if (!switch_type
|| !case_type
)
3417 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3418 else if (is_enum_type(case_type
))
3419 *enumcase
= case_expr
;
3422 sclass
= classify_type(switch_type
, &switch_type
);
3423 cclass
= classify_type(case_type
, &case_type
);
3425 /* both should be arithmetic */
3426 if (!(sclass
& cclass
& TYPE_NUM
))
3429 /* neither should be floating */
3430 if ((sclass
| cclass
) & TYPE_FLOAT
)
3433 /* if neither is restricted, we are OK */
3434 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3437 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3438 cclass
, sclass
, case_type
, switch_type
)) {
3439 unrestrict(case_expr
, cclass
, &case_type
);
3440 unrestrict(switch_expr
, sclass
, &switch_type
);
3445 expression_error(case_expr
, "incompatible types for 'case' statement");
3448 static void evaluate_switch_statement(struct statement
*stmt
)
3451 struct expression
*enumcase
= NULL
;
3452 struct expression
**enumcase_holder
= &enumcase
;
3453 struct expression
*sel
= stmt
->switch_expression
;
3455 evaluate_expression(sel
);
3456 evaluate_statement(stmt
->switch_statement
);
3459 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3460 enumcase_holder
= NULL
; /* Only check cases against switch */
3462 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3463 struct statement
*case_stmt
= sym
->stmt
;
3464 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3465 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3466 } END_FOR_EACH_PTR(sym
);
3469 static void evaluate_goto_statement(struct statement
*stmt
)
3471 struct symbol
*label
= stmt
->goto_label
;
3473 if (label
&& !label
->stmt
&& !lookup_keyword(label
->ident
, NS_KEYWORD
))
3474 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3476 evaluate_expression(stmt
->goto_expression
);
3479 struct symbol
*evaluate_statement(struct statement
*stmt
)
3484 switch (stmt
->type
) {
3485 case STMT_DECLARATION
: {
3487 FOR_EACH_PTR(stmt
->declaration
, s
) {
3489 } END_FOR_EACH_PTR(s
);
3494 return evaluate_return_expression(stmt
);
3496 case STMT_EXPRESSION
:
3497 if (!evaluate_expression(stmt
->expression
))
3499 if (stmt
->expression
->ctype
== &null_ctype
)
3500 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3501 return degenerate(stmt
->expression
);
3503 case STMT_COMPOUND
: {
3504 struct statement
*s
;
3505 struct symbol
*type
= NULL
;
3507 /* Evaluate the return symbol in the compound statement */
3508 evaluate_symbol(stmt
->ret
);
3511 * Then, evaluate each statement, making the type of the
3512 * compound statement be the type of the last statement
3514 type
= evaluate_statement(stmt
->args
);
3515 FOR_EACH_PTR(stmt
->stmts
, s
) {
3516 type
= evaluate_statement(s
);
3517 } END_FOR_EACH_PTR(s
);
3523 evaluate_if_statement(stmt
);
3526 evaluate_iterator(stmt
);
3529 evaluate_switch_statement(stmt
);
3532 evaluate_case_statement(stmt
);
3535 return evaluate_statement(stmt
->label_statement
);
3537 evaluate_goto_statement(stmt
);
3542 evaluate_asm_statement(stmt
);
3545 evaluate_expression(stmt
->expression
);
3548 evaluate_expression(stmt
->range_expression
);
3549 evaluate_expression(stmt
->range_low
);
3550 evaluate_expression(stmt
->range_high
);