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
));
275 * This gets called for implicit casts in assignments and
276 * integer promotion. We often want to try to move the
277 * cast down, because the ops involved may have been
278 * implicitly cast up, and we can get rid of the casts
281 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
283 struct expression
*expr
;
285 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
287 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
291 * See if we can simplify the op. Move the cast down.
295 if (old
->ctype
->bit_size
< type
->bit_size
)
297 if (old
->op
== '~') {
299 old
->unop
= cast_to(old
->unop
, type
);
304 case EXPR_IMPLIED_CAST
:
305 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
307 if (old
->ctype
->bit_size
>= type
->bit_size
) {
308 struct expression
*orig
= old
->cast_expression
;
309 if (same_cast_type(orig
->ctype
, type
))
311 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
313 old
->cast_type
= type
;
323 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
324 expr
->flags
= old
->flags
;
326 expr
->cast_type
= type
;
327 expr
->cast_expression
= old
;
342 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
344 static int type_class
[SYM_BAD
+ 1] = {
345 [SYM_PTR
] = TYPE_PTR
,
346 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
347 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
348 [SYM_STRUCT
] = TYPE_COMPOUND
,
349 [SYM_UNION
] = TYPE_COMPOUND
,
350 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
351 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
352 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
354 if (type
->type
== SYM_NODE
)
355 type
= type
->ctype
.base_type
;
356 if (type
->type
== SYM_TYPEOF
) {
357 type
= evaluate_expression(type
->initializer
);
360 else if (type
->type
== SYM_NODE
)
361 type
= type
->ctype
.base_type
;
363 if (type
->type
== SYM_ENUM
)
364 type
= type
->ctype
.base_type
;
366 if (type
->type
== SYM_BASETYPE
) {
367 if (type
->ctype
.base_type
== &int_type
)
369 if (type
->ctype
.base_type
== &fp_type
)
370 return TYPE_NUM
| TYPE_FLOAT
;
372 return type_class
[type
->type
];
375 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
377 static inline int is_string_type(struct symbol
*type
)
379 if (type
->type
== SYM_NODE
)
380 type
= type
->ctype
.base_type
;
381 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
384 static struct symbol
*bad_expr_type(struct expression
*expr
)
386 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
387 switch (expr
->type
) {
390 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
391 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
395 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
402 return expr
->ctype
= &bad_ctype
;
405 static int restricted_value(struct expression
*v
, struct symbol
*type
)
407 if (v
->type
!= EXPR_VALUE
)
414 static int restricted_binop(int op
, struct symbol
*type
)
419 case SPECIAL_AND_ASSIGN
:
420 case SPECIAL_OR_ASSIGN
:
421 case SPECIAL_XOR_ASSIGN
:
422 return 1; /* unfoul */
426 return 2; /* keep fouled */
428 case SPECIAL_NOTEQUAL
:
429 return 3; /* warn if fouled */
435 static int restricted_unop(int op
, struct symbol
**type
)
438 if ((*type
)->bit_size
< bits_in_int
)
439 *type
= befoul(*type
);
446 /* type should be SYM_FOULED */
447 static inline struct symbol
*unfoul(struct symbol
*type
)
449 return type
->ctype
.base_type
;
452 static struct symbol
*restricted_binop_type(int op
,
453 struct expression
*left
,
454 struct expression
*right
,
455 int lclass
, int rclass
,
456 struct symbol
*ltype
,
457 struct symbol
*rtype
)
459 struct symbol
*ctype
= NULL
;
460 if (lclass
& TYPE_RESTRICT
) {
461 if (rclass
& TYPE_RESTRICT
) {
462 if (ltype
== rtype
) {
464 } else if (lclass
& TYPE_FOULED
) {
465 if (unfoul(ltype
) == rtype
)
467 } else if (rclass
& TYPE_FOULED
) {
468 if (unfoul(rtype
) == ltype
)
472 if (!restricted_value(right
, ltype
))
475 } else if (!restricted_value(left
, rtype
))
479 switch (restricted_binop(op
, ctype
)) {
481 if ((lclass
^ rclass
) & TYPE_FOULED
)
482 ctype
= unfoul(ctype
);
485 if (!(lclass
& rclass
& TYPE_FOULED
))
497 static inline void unrestrict(struct expression
*expr
,
498 int class, struct symbol
**ctype
)
500 if (class & TYPE_RESTRICT
) {
501 if (class & TYPE_FOULED
)
502 *ctype
= unfoul(*ctype
);
503 warning(expr
->pos
, "%s degrades to integer",
504 show_typename(*ctype
));
505 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
509 static struct symbol
*usual_conversions(int op
,
510 struct expression
*left
,
511 struct expression
*right
,
512 int lclass
, int rclass
,
513 struct symbol
*ltype
,
514 struct symbol
*rtype
)
516 struct symbol
*ctype
;
518 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
520 if ((lclass
| rclass
) & TYPE_RESTRICT
)
524 if (!(lclass
& TYPE_FLOAT
)) {
525 if (!(rclass
& TYPE_FLOAT
))
526 return bigger_int_type(ltype
, rtype
);
529 } else if (rclass
& TYPE_FLOAT
) {
530 unsigned long lmod
= ltype
->ctype
.modifiers
;
531 unsigned long rmod
= rtype
->ctype
.modifiers
;
532 if (rmod
& ~lmod
& (MOD_LONG_ALL
))
540 ctype
= restricted_binop_type(op
, left
, right
,
541 lclass
, rclass
, ltype
, rtype
);
545 unrestrict(left
, lclass
, <ype
);
546 unrestrict(right
, rclass
, &rtype
);
551 static inline int lvalue_expression(struct expression
*expr
)
553 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
556 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
558 struct expression
*index
= expr
->right
;
559 struct symbol
*ctype
, *base
;
562 classify_type(degenerate(expr
->left
), &ctype
);
563 base
= examine_pointer_target(ctype
);
566 expression_error(expr
, "missing type information");
569 if (is_function(base
)) {
570 expression_error(expr
, "arithmetics on pointers to functions");
574 /* Get the size of whatever the pointer points to */
575 multiply
= is_void_type(base
) ? 1 : bits_to_bytes(base
->bit_size
);
577 if (ctype
== &null_ctype
)
581 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
584 if (index
->type
== EXPR_VALUE
) {
585 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
586 unsigned long long v
= index
->value
, mask
;
587 mask
= 1ULL << (itype
->bit_size
- 1);
593 mask
= 1ULL << (bits_in_pointer
- 1);
594 v
&= mask
| (mask
- 1);
596 val
->ctype
= ssize_t_ctype
;
601 if (itype
->bit_size
< bits_in_pointer
)
602 index
= cast_to(index
, ssize_t_ctype
);
605 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
606 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
608 val
->ctype
= ssize_t_ctype
;
609 val
->value
= multiply
;
612 mul
->ctype
= ssize_t_ctype
;
622 static void examine_fn_arguments(struct symbol
*fn
);
624 #define MOD_IGN (MOD_VOLATILE | MOD_CONST | MOD_PURE)
626 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
627 unsigned long mod1
, unsigned long mod2
)
629 unsigned long as1
= c1
->as
, as2
= c2
->as
;
630 struct symbol
*t1
= c1
->base_type
;
631 struct symbol
*t2
= c2
->base_type
;
632 int move1
= 1, move2
= 1;
633 mod1
|= c1
->modifiers
;
634 mod2
|= c2
->modifiers
;
638 struct symbol
*base1
= t1
->ctype
.base_type
;
639 struct symbol
*base2
= t2
->ctype
.base_type
;
642 * FIXME! Collect alignment and context too here!
645 if (t1
&& t1
->type
!= SYM_PTR
) {
646 mod1
|= t1
->ctype
.modifiers
;
653 if (t2
&& t2
->type
!= SYM_PTR
) {
654 mod2
|= t2
->ctype
.modifiers
;
663 return "different types";
665 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
673 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
683 if (type
!= t2
->type
)
684 return "different base types";
688 sparse_error(t1
->pos
,
689 "internal error: bad type in derived(%d)",
693 return "different base types";
696 /* allow definition of incomplete structs and unions */
697 if (t1
->ident
== t2
->ident
)
699 return "different base types";
701 /* XXX: we ought to compare sizes */
705 return "different address spaces";
706 /* MOD_SPECIFIER is due to idiocy in parse.c */
707 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
708 return "different modifiers";
709 /* we could be lazier here */
710 base1
= examine_pointer_target(t1
);
711 base2
= examine_pointer_target(t2
);
712 mod1
= t1
->ctype
.modifiers
;
714 mod2
= t2
->ctype
.modifiers
;
718 struct symbol
*arg1
, *arg2
;
722 return "different address spaces";
723 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
724 return "different modifiers";
725 mod1
= t1
->ctype
.modifiers
;
727 mod2
= t2
->ctype
.modifiers
;
730 if (t1
->variadic
!= t2
->variadic
)
731 return "incompatible variadic arguments";
732 examine_fn_arguments(t1
);
733 examine_fn_arguments(t2
);
734 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
735 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
742 return "different argument counts";
743 diffstr
= type_difference(&arg1
->ctype
,
747 static char argdiff
[80];
748 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
755 FINISH_PTR_LIST(arg2
);
756 FINISH_PTR_LIST(arg1
);
761 return "different address spaces";
763 return "different base types";
764 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
768 return "different type sizes";
769 else if (diff
& ~MOD_SIGNEDNESS
)
770 return "different modifiers";
772 return "different signedness";
778 return "different address spaces";
779 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
780 return "different modifiers";
784 static void bad_null(struct expression
*expr
)
786 if (Wnon_pointer_null
)
787 warning(expr
->pos
, "Using plain integer as NULL pointer");
790 static unsigned long target_qualifiers(struct symbol
*type
)
792 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
793 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
798 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
800 const char *typediff
;
801 struct symbol
*ltype
, *rtype
;
802 struct expression
*l
= expr
->left
;
803 struct expression
*r
= expr
->right
;
804 struct symbol
*lbase
;
806 classify_type(degenerate(l
), <ype
);
807 classify_type(degenerate(r
), &rtype
);
809 lbase
= examine_pointer_target(ltype
);
810 examine_pointer_target(rtype
);
811 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
812 target_qualifiers(rtype
),
813 target_qualifiers(ltype
));
815 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
817 if (is_function(lbase
)) {
818 expression_error(expr
, "subtraction of functions? Share your drugs");
822 expr
->ctype
= ssize_t_ctype
;
823 if (lbase
->bit_size
> bits_in_char
) {
824 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
825 struct expression
*div
= expr
;
826 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
827 unsigned long value
= bits_to_bytes(lbase
->bit_size
);
829 val
->ctype
= size_t_ctype
;
832 if (value
& (value
-1)) {
833 if (Wptr_subtraction_blows
)
834 warning(expr
->pos
, "potentially expensive pointer subtraction");
838 sub
->ctype
= ssize_t_ctype
;
847 return ssize_t_ctype
;
850 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
852 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
854 struct symbol
*ctype
;
859 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
860 warning(expr
->pos
, "assignment expression in conditional");
862 ctype
= evaluate_expression(expr
);
864 if (is_safe_type(ctype
))
865 warning(expr
->pos
, "testing a 'safe expression'");
871 static struct symbol
*evaluate_logical(struct expression
*expr
)
873 if (!evaluate_conditional(expr
->left
, 0))
875 if (!evaluate_conditional(expr
->right
, 0))
878 /* the result is int [6.5.13(3), 6.5.14(3)] */
879 expr
->ctype
= &int_ctype
;
881 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
887 static struct symbol
*evaluate_binop(struct expression
*expr
)
889 struct symbol
*ltype
, *rtype
, *ctype
;
890 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
891 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
895 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
899 /* number op number */
900 if (lclass
& rclass
& TYPE_NUM
) {
901 if ((lclass
| rclass
) & TYPE_FLOAT
) {
903 case '+': case '-': case '*': case '/':
906 return bad_expr_type(expr
);
910 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
911 // shifts do integer promotions, but that's it.
912 unrestrict(expr
->left
, lclass
, <ype
);
913 unrestrict(expr
->right
, rclass
, &rtype
);
914 ctype
= ltype
= integer_promotion(ltype
);
915 rtype
= integer_promotion(rtype
);
917 // The rest do usual conversions
918 const unsigned left_not
= expr
->left
->type
== EXPR_PREOP
919 && expr
->left
->op
== '!';
920 const unsigned right_not
= expr
->right
->type
== EXPR_PREOP
921 && expr
->right
->op
== '!';
922 if ((op
== '&' || op
== '|') && (left_not
|| right_not
))
923 warning(expr
->pos
, "dubious: %sx %c %sy",
926 right_not
? "!" : "");
928 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
929 lclass
, rclass
, ltype
, rtype
);
930 ctype
= rtype
= ltype
;
933 expr
->left
= cast_to(expr
->left
, ltype
);
934 expr
->right
= cast_to(expr
->right
, rtype
);
939 /* pointer (+|-) integer */
940 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
941 unrestrict(expr
->right
, rclass
, &rtype
);
942 return evaluate_ptr_add(expr
, rtype
);
945 /* integer + pointer */
946 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
947 struct expression
*index
= expr
->left
;
948 unrestrict(index
, lclass
, <ype
);
949 expr
->left
= expr
->right
;
951 return evaluate_ptr_add(expr
, ltype
);
954 /* pointer - pointer */
955 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
956 return evaluate_ptr_sub(expr
);
958 return bad_expr_type(expr
);
961 static struct symbol
*evaluate_comma(struct expression
*expr
)
963 expr
->ctype
= degenerate(expr
->right
);
964 if (expr
->ctype
== &null_ctype
)
965 expr
->ctype
= &ptr_ctype
;
966 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
970 static int modify_for_unsigned(int op
)
973 op
= SPECIAL_UNSIGNED_LT
;
975 op
= SPECIAL_UNSIGNED_GT
;
976 else if (op
== SPECIAL_LTE
)
977 op
= SPECIAL_UNSIGNED_LTE
;
978 else if (op
== SPECIAL_GTE
)
979 op
= SPECIAL_UNSIGNED_GTE
;
983 static inline int is_null_pointer_constant(struct expression
*e
)
985 if (e
->ctype
== &null_ctype
)
987 if (!(e
->flags
& Int_const_expr
))
989 return is_zero_constant(e
) ? 2 : 0;
992 static struct symbol
*evaluate_compare(struct expression
*expr
)
994 struct expression
*left
= expr
->left
, *right
= expr
->right
;
995 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
996 int lclass
= classify_type(degenerate(left
), <ype
);
997 int rclass
= classify_type(degenerate(right
), &rtype
);
998 struct symbol
*ctype
;
999 const char *typediff
;
1002 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1007 if (is_type_type(ltype
) && is_type_type(rtype
))
1010 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1011 warning(expr
->pos
, "testing a 'safe expression'");
1013 /* number on number */
1014 if (lclass
& rclass
& TYPE_NUM
) {
1015 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1016 lclass
, rclass
, ltype
, rtype
);
1017 expr
->left
= cast_to(expr
->left
, ctype
);
1018 expr
->right
= cast_to(expr
->right
, ctype
);
1019 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1020 expr
->op
= modify_for_unsigned(expr
->op
);
1024 /* at least one must be a pointer */
1025 if (!((lclass
| rclass
) & TYPE_PTR
))
1026 return bad_expr_type(expr
);
1028 /* equality comparisons can be with null pointer constants */
1029 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1030 int is_null1
= is_null_pointer_constant(left
);
1031 int is_null2
= is_null_pointer_constant(right
);
1036 if (is_null1
&& is_null2
) {
1037 int positive
= expr
->op
== SPECIAL_EQUAL
;
1038 expr
->type
= EXPR_VALUE
;
1039 expr
->value
= positive
;
1042 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1043 left
= cast_to(left
, rtype
);
1046 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1047 right
= cast_to(right
, ltype
);
1051 /* both should be pointers */
1052 if (!(lclass
& rclass
& TYPE_PTR
))
1053 return bad_expr_type(expr
);
1054 expr
->op
= modify_for_unsigned(expr
->op
);
1056 lbase
= examine_pointer_target(ltype
);
1057 rbase
= examine_pointer_target(rtype
);
1059 /* they also have special treatment for pointers to void */
1060 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1061 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1062 if (lbase
== &void_ctype
) {
1063 right
= cast_to(right
, ltype
);
1066 if (rbase
== &void_ctype
) {
1067 left
= cast_to(left
, rtype
);
1073 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1074 target_qualifiers(rtype
),
1075 target_qualifiers(ltype
));
1079 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1083 /* the result is int [6.5.8(6), 6.5.9(3)]*/
1084 expr
->ctype
= &int_ctype
;
1089 * NOTE! The degenerate case of "x ? : y", where we don't
1090 * have a true case, this will possibly promote "x" to the
1091 * same type as "y", and thus _change_ the conditional
1092 * test in the expression. But since promotion is "safe"
1093 * for testing, that's OK.
1095 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1097 struct expression
**true;
1098 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1100 const char * typediff
;
1103 if (!evaluate_conditional(expr
->conditional
, 0))
1105 if (!evaluate_expression(expr
->cond_false
))
1108 ctype
= degenerate(expr
->conditional
);
1109 rtype
= degenerate(expr
->cond_false
);
1111 true = &expr
->conditional
;
1113 if (expr
->cond_true
) {
1114 if (!evaluate_expression(expr
->cond_true
))
1116 ltype
= degenerate(expr
->cond_true
);
1117 true = &expr
->cond_true
;
1121 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1122 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1127 lclass
= classify_type(ltype
, <ype
);
1128 rclass
= classify_type(rtype
, &rtype
);
1129 if (lclass
& rclass
& TYPE_NUM
) {
1130 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1131 lclass
, rclass
, ltype
, rtype
);
1132 *true = cast_to(*true, ctype
);
1133 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1137 if ((lclass
| rclass
) & TYPE_PTR
) {
1138 int is_null1
= is_null_pointer_constant(*true);
1139 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1141 if (is_null1
&& is_null2
) {
1142 *true = cast_to(*true, &ptr_ctype
);
1143 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1147 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1150 *true = cast_to(*true, rtype
);
1154 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1156 bad_null(expr
->cond_false
);
1157 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1161 if (!(lclass
& rclass
& TYPE_PTR
)) {
1162 typediff
= "different types";
1165 /* OK, it's pointer on pointer */
1166 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1167 typediff
= "different address spaces";
1171 /* need to be lazier here */
1172 lbase
= examine_pointer_target(ltype
);
1173 rbase
= examine_pointer_target(rtype
);
1174 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1176 if (lbase
== &void_ctype
) {
1177 /* XXX: pointers to function should warn here */
1182 if (rbase
== &void_ctype
) {
1183 /* XXX: pointers to function should warn here */
1187 /* XXX: that should be pointer to composite */
1189 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1196 /* void on void, struct on same struct, union on same union */
1197 if (ltype
== rtype
) {
1201 typediff
= "different base types";
1204 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1208 expr
->ctype
= ctype
;
1212 if (qual
& ~ctype
->ctype
.modifiers
) {
1213 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1215 sym
->ctype
.modifiers
|= qual
;
1218 *true = cast_to(*true, ctype
);
1219 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1223 /* FP assignments can not do modulo or bit operations */
1224 static int compatible_float_op(int op
)
1226 return op
== SPECIAL_ADD_ASSIGN
||
1227 op
== SPECIAL_SUB_ASSIGN
||
1228 op
== SPECIAL_MUL_ASSIGN
||
1229 op
== SPECIAL_DIV_ASSIGN
;
1232 static int evaluate_assign_op(struct expression
*expr
)
1234 struct symbol
*target
= expr
->left
->ctype
;
1235 struct symbol
*source
= expr
->right
->ctype
;
1236 struct symbol
*t
, *s
;
1237 int tclass
= classify_type(target
, &t
);
1238 int sclass
= classify_type(source
, &s
);
1241 if (tclass
& sclass
& TYPE_NUM
) {
1242 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1243 expression_error(expr
, "invalid assignment");
1246 if (tclass
& TYPE_RESTRICT
) {
1247 if (!restricted_binop(op
, t
)) {
1248 warning(expr
->pos
, "bad assignment (%s) to %s",
1249 show_special(op
), show_typename(t
));
1250 expr
->right
= cast_to(expr
->right
, target
);
1253 /* allowed assignments unfoul */
1254 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1256 if (!restricted_value(expr
->right
, t
))
1258 } else if (!(sclass
& TYPE_RESTRICT
))
1260 /* source and target would better be identical restricted */
1263 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1264 info(expr
->pos
, " left side has type %s", show_typename(t
));
1265 info(expr
->pos
, " right side has type %s", show_typename(s
));
1266 expr
->right
= cast_to(expr
->right
, target
);
1269 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1270 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1271 unrestrict(expr
->right
, sclass
, &s
);
1272 evaluate_ptr_add(expr
, s
);
1275 expression_error(expr
, "invalid pointer assignment");
1279 expression_error(expr
, "invalid assignment");
1283 expr
->right
= cast_to(expr
->right
, target
);
1287 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1290 return 0; /* yes, 0 - we don't want a cast_to here */
1291 if (t1
== &void_ctype
)
1293 if (t2
== &void_ctype
)
1295 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1297 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1301 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1303 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1308 static int check_assignment_types(struct symbol
*target
, struct expression
**rp
,
1309 const char **typediff
)
1311 struct symbol
*source
= degenerate(*rp
);
1312 struct symbol
*t
, *s
;
1313 int tclass
= classify_type(target
, &t
);
1314 int sclass
= classify_type(source
, &s
);
1316 if (tclass
& sclass
& TYPE_NUM
) {
1317 if (tclass
& TYPE_RESTRICT
) {
1318 /* allowed assignments unfoul */
1319 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1321 if (!restricted_value(*rp
, target
))
1325 } else if (!(sclass
& TYPE_RESTRICT
))
1327 *typediff
= "different base types";
1331 if (tclass
== TYPE_PTR
) {
1332 unsigned long mod1
, mod2
;
1333 struct symbol
*b1
, *b2
;
1334 // NULL pointer is always OK
1335 int is_null
= is_null_pointer_constant(*rp
);
1341 if (!(sclass
& TYPE_PTR
)) {
1342 *typediff
= "different base types";
1345 b1
= examine_pointer_target(t
);
1346 b2
= examine_pointer_target(s
);
1347 mod1
= target_qualifiers(t
);
1348 mod2
= target_qualifiers(s
);
1349 if (whitelist_pointers(b1
, b2
)) {
1351 * assignments to/from void * are OK, provided that
1352 * we do not remove qualifiers from pointed to [C]
1353 * or mix address spaces [sparse].
1355 if (t
->ctype
.as
!= s
->ctype
.as
) {
1356 *typediff
= "different address spaces";
1360 * If this is a function pointer assignment, it is
1361 * actually fine to assign a pointer to const data to
1362 * it, as a function pointer points to const data
1363 * implicitly, i.e., dereferencing it does not produce
1366 if (b1
->type
== SYM_FN
)
1369 *typediff
= "different modifiers";
1374 /* It's OK if the target is more volatile or const than the source */
1375 *typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1381 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1384 if (tclass
& TYPE_NUM
) {
1385 /* XXX: need to turn into comparison with NULL */
1386 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1388 *typediff
= "different base types";
1391 *typediff
= "invalid types";
1395 *rp
= cast_to(*rp
, target
);
1399 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1400 struct expression
**rp
, const char *where
)
1402 const char *typediff
;
1403 struct symbol
*source
= degenerate(*rp
);
1405 if (!check_assignment_types(target
, rp
, &typediff
)) {
1406 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1407 info(expr
->pos
, " expected %s", show_typename(target
));
1408 info(expr
->pos
, " got %s", show_typename(source
));
1409 *rp
= cast_to(*rp
, target
);
1416 static int compatible_transparent_union(struct symbol
*target
,
1417 struct expression
**rp
)
1419 struct symbol
*t
, *member
;
1420 classify_type(target
, &t
);
1421 if (t
->type
!= SYM_UNION
|| !t
->transparent_union
)
1424 FOR_EACH_PTR(t
->symbol_list
, member
) {
1425 const char *typediff
;
1426 if (check_assignment_types(member
, rp
, &typediff
))
1428 } END_FOR_EACH_PTR(member
);
1433 static int compatible_argument_type(struct expression
*expr
, struct symbol
*target
,
1434 struct expression
**rp
, const char *where
)
1436 if (compatible_transparent_union(target
, rp
))
1439 return compatible_assignment_types(expr
, target
, rp
, where
);
1442 static void mark_assigned(struct expression
*expr
)
1448 switch (expr
->type
) {
1453 if (sym
->type
!= SYM_NODE
)
1455 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1459 mark_assigned(expr
->left
);
1460 mark_assigned(expr
->right
);
1463 case EXPR_FORCE_CAST
:
1464 mark_assigned(expr
->cast_expression
);
1467 mark_assigned(expr
->base
);
1475 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1477 if (type
->ctype
.modifiers
& MOD_CONST
)
1478 expression_error(left
, "assignment to const expression");
1480 /* We know left is an lvalue, so it's a "preop-*" */
1481 mark_assigned(left
->unop
);
1484 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1486 struct expression
*left
= expr
->left
;
1487 struct expression
*where
= expr
;
1488 struct symbol
*ltype
;
1490 if (!lvalue_expression(left
)) {
1491 expression_error(expr
, "not an lvalue");
1495 ltype
= left
->ctype
;
1497 if (expr
->op
!= '=') {
1498 if (!evaluate_assign_op(expr
))
1501 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1505 evaluate_assign_to(left
, ltype
);
1507 expr
->ctype
= ltype
;
1511 static void examine_fn_arguments(struct symbol
*fn
)
1515 FOR_EACH_PTR(fn
->arguments
, s
) {
1516 struct symbol
*arg
= evaluate_symbol(s
);
1517 /* Array/function arguments silently degenerate into pointers */
1523 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1524 if (arg
->type
== SYM_ARRAY
)
1525 ptr
->ctype
= arg
->ctype
;
1527 ptr
->ctype
.base_type
= arg
;
1528 ptr
->ctype
.as
|= s
->ctype
.as
;
1529 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1531 s
->ctype
.base_type
= ptr
;
1533 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1536 examine_symbol_type(s
);
1543 } END_FOR_EACH_PTR(s
);
1546 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1548 /* Take the modifiers of the pointer, and apply them to the member */
1549 mod
|= sym
->ctype
.modifiers
;
1550 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1551 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1553 newsym
->ctype
.as
= as
;
1554 newsym
->ctype
.modifiers
= mod
;
1560 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1562 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1563 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1565 node
->ctype
.base_type
= ptr
;
1566 ptr
->bit_size
= bits_in_pointer
;
1567 ptr
->ctype
.alignment
= pointer_alignment
;
1569 node
->bit_size
= bits_in_pointer
;
1570 node
->ctype
.alignment
= pointer_alignment
;
1573 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1574 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1575 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1577 if (sym
->type
== SYM_NODE
) {
1578 ptr
->ctype
.as
|= sym
->ctype
.as
;
1579 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1580 sym
= sym
->ctype
.base_type
;
1582 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1583 ptr
->ctype
.as
|= sym
->ctype
.as
;
1584 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1585 sym
= sym
->ctype
.base_type
;
1587 ptr
->ctype
.base_type
= sym
;
1592 /* Arrays degenerate into pointers on pointer arithmetic */
1593 static struct symbol
*degenerate(struct expression
*expr
)
1595 struct symbol
*ctype
, *base
;
1599 ctype
= expr
->ctype
;
1602 base
= examine_symbol_type(ctype
);
1603 if (ctype
->type
== SYM_NODE
)
1604 base
= ctype
->ctype
.base_type
;
1606 * Arrays degenerate into pointers to the entries, while
1607 * functions degenerate into pointers to themselves.
1608 * If array was part of non-lvalue compound, we create a copy
1609 * of that compound first and then act as if we were dealing with
1610 * the corresponding field in there.
1612 switch (base
->type
) {
1614 if (expr
->type
== EXPR_SLICE
) {
1615 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1616 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1618 a
->ctype
.base_type
= expr
->base
->ctype
;
1619 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1620 a
->array_size
= expr
->base
->ctype
->array_size
;
1622 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1624 e0
->ctype
= &lazy_ptr_ctype
;
1626 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1629 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1631 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1633 e2
->right
= expr
->base
;
1635 e2
->ctype
= expr
->base
->ctype
;
1637 if (expr
->r_bitpos
) {
1638 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1641 e3
->right
= alloc_const_expression(expr
->pos
,
1642 bits_to_bytes(expr
->r_bitpos
));
1643 e3
->ctype
= &lazy_ptr_ctype
;
1648 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1651 e4
->ctype
= &lazy_ptr_ctype
;
1654 expr
->type
= EXPR_PREOP
;
1658 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1659 expression_error(expr
, "strange non-value function or array");
1662 *expr
= *expr
->unop
;
1663 ctype
= create_pointer(expr
, ctype
, 1);
1664 expr
->ctype
= ctype
;
1671 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1673 struct expression
*op
= expr
->unop
;
1674 struct symbol
*ctype
;
1676 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1677 expression_error(expr
, "not addressable");
1684 if (expr
->type
== EXPR_SYMBOL
) {
1685 struct symbol
*sym
= expr
->symbol
;
1686 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1690 * symbol expression evaluation is lazy about the type
1691 * of the sub-expression, so we may have to generate
1692 * the type here if so..
1694 if (expr
->ctype
== &lazy_ptr_ctype
) {
1695 ctype
= create_pointer(expr
, ctype
, 0);
1696 expr
->ctype
= ctype
;
1702 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1704 struct expression
*op
= expr
->unop
;
1705 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1707 /* Simplify: *&(expr) => (expr) */
1708 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1714 /* Dereferencing a node drops all the node information. */
1715 if (ctype
->type
== SYM_NODE
)
1716 ctype
= ctype
->ctype
.base_type
;
1718 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1719 target
= ctype
->ctype
.base_type
;
1721 switch (ctype
->type
) {
1723 expression_error(expr
, "cannot dereference this type");
1726 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1727 merge_type(node
, ctype
);
1731 if (!lvalue_expression(op
)) {
1732 expression_error(op
, "non-lvalue array??");
1736 /* Do the implied "addressof" on the array */
1740 * When an array is dereferenced, we need to pick
1741 * up the attributes of the original node too..
1743 merge_type(node
, op
->ctype
);
1744 merge_type(node
, ctype
);
1748 node
->bit_size
= target
->bit_size
;
1749 node
->array_size
= target
->array_size
;
1756 * Unary post-ops: x++ and x--
1758 static struct symbol
*evaluate_postop(struct expression
*expr
)
1760 struct expression
*op
= expr
->unop
;
1761 struct symbol
*ctype
= op
->ctype
;
1762 int class = classify_type(ctype
, &ctype
);
1765 if (!class || class & TYPE_COMPOUND
) {
1766 expression_error(expr
, "need scalar for ++/--");
1769 if (!lvalue_expression(expr
->unop
)) {
1770 expression_error(expr
, "need lvalue expression for ++/--");
1774 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1775 unrestrict(expr
, class, &ctype
);
1777 if (class & TYPE_NUM
) {
1779 } else if (class == TYPE_PTR
) {
1780 struct symbol
*target
= examine_pointer_target(ctype
);
1781 if (!is_function(target
))
1782 multiply
= bits_to_bytes(target
->bit_size
);
1786 evaluate_assign_to(op
, op
->ctype
);
1787 expr
->op_value
= multiply
;
1788 expr
->ctype
= ctype
;
1792 expression_error(expr
, "bad argument type for ++/--");
1796 static struct symbol
*evaluate_sign(struct expression
*expr
)
1798 struct symbol
*ctype
= expr
->unop
->ctype
;
1799 int class = classify_type(ctype
, &ctype
);
1800 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1802 /* should be an arithmetic type */
1803 if (!(class & TYPE_NUM
))
1804 return bad_expr_type(expr
);
1805 if (class & TYPE_RESTRICT
)
1808 if (!(class & TYPE_FLOAT
)) {
1809 ctype
= integer_promotion(ctype
);
1810 expr
->unop
= cast_to(expr
->unop
, ctype
);
1811 } else if (expr
->op
!= '~') {
1812 /* no conversions needed */
1814 return bad_expr_type(expr
);
1816 if (expr
->op
== '+')
1817 *expr
= *expr
->unop
;
1818 expr
->ctype
= ctype
;
1821 if (restricted_unop(expr
->op
, &ctype
))
1822 unrestrict(expr
, class, &ctype
);
1826 static struct symbol
*evaluate_preop(struct expression
*expr
)
1828 struct symbol
*ctype
= expr
->unop
->ctype
;
1832 *expr
= *expr
->unop
;
1838 return evaluate_sign(expr
);
1841 return evaluate_dereference(expr
);
1844 return evaluate_addressof(expr
);
1846 case SPECIAL_INCREMENT
:
1847 case SPECIAL_DECREMENT
:
1849 * From a type evaluation standpoint the preops are
1850 * the same as the postops
1852 return evaluate_postop(expr
);
1855 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1857 if (is_safe_type(ctype
))
1858 warning(expr
->pos
, "testing a 'safe expression'");
1859 if (is_float_type(ctype
)) {
1860 struct expression
*arg
= expr
->unop
;
1861 expr
->type
= EXPR_COMPARE
;
1862 expr
->op
= SPECIAL_EQUAL
;
1864 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1865 expr
->right
->ctype
= ctype
;
1866 expr
->right
->fvalue
= 0;
1867 } else if (is_fouled_type(ctype
)) {
1868 warning(expr
->pos
, "%s degrades to integer",
1869 show_typename(ctype
->ctype
.base_type
));
1871 /* the result is int [6.5.3.3(5)]*/
1878 expr
->ctype
= ctype
;
1882 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1884 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1885 struct ptr_list
*list
= head
;
1891 for (i
= 0; i
< list
->nr
; i
++) {
1892 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1894 if (sym
->ident
!= ident
)
1896 *offset
= sym
->offset
;
1899 struct symbol
*ctype
= sym
->ctype
.base_type
;
1903 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1905 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1908 *offset
+= sym
->offset
;
1912 } while ((list
= list
->next
) != head
);
1916 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1918 struct expression
*add
;
1921 * Create a new add-expression
1923 * NOTE! Even if we just add zero, we need a new node
1924 * for the member pointer, since it has a different
1925 * type than the original pointer. We could make that
1926 * be just a cast, but the fact is, a node is a node,
1927 * so we might as well just do the "add zero" here.
1929 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1932 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1933 add
->right
->ctype
= &int_ctype
;
1934 add
->right
->value
= offset
;
1937 * The ctype of the pointer will be lazily evaluated if
1938 * we ever take the address of this member dereference..
1940 add
->ctype
= &lazy_ptr_ctype
;
1944 /* structure/union dereference */
1945 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1948 struct symbol
*ctype
, *member
;
1949 struct expression
*deref
= expr
->deref
, *add
;
1950 struct ident
*ident
= expr
->member
;
1954 if (!evaluate_expression(deref
))
1957 expression_error(expr
, "bad member name");
1961 ctype
= deref
->ctype
;
1962 examine_symbol_type(ctype
);
1963 address_space
= ctype
->ctype
.as
;
1964 mod
= ctype
->ctype
.modifiers
;
1965 if (ctype
->type
== SYM_NODE
) {
1966 ctype
= ctype
->ctype
.base_type
;
1967 address_space
|= ctype
->ctype
.as
;
1968 mod
|= ctype
->ctype
.modifiers
;
1970 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1971 expression_error(expr
, "expected structure or union");
1975 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1977 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1978 const char *name
= "<unnamed>";
1981 name
= ctype
->ident
->name
;
1982 namelen
= ctype
->ident
->len
;
1984 if (ctype
->symbol_list
)
1985 expression_error(expr
, "no member '%s' in %s %.*s",
1986 show_ident(ident
), type
, namelen
, name
);
1988 expression_error(expr
, "using member '%s' in "
1989 "incomplete %s %.*s", show_ident(ident
),
1990 type
, namelen
, name
);
1995 * The member needs to take on the address space and modifiers of
1996 * the "parent" type.
1998 member
= convert_to_as_mod(member
, address_space
, mod
);
1999 ctype
= get_base_type(member
);
2001 if (!lvalue_expression(deref
)) {
2002 if (deref
->type
!= EXPR_SLICE
) {
2006 expr
->base
= deref
->base
;
2007 expr
->r_bitpos
= deref
->r_bitpos
;
2009 expr
->r_bitpos
+= bytes_to_bits(offset
);
2010 expr
->type
= EXPR_SLICE
;
2011 expr
->r_nrbits
= member
->bit_size
;
2012 expr
->r_bitpos
+= member
->bit_offset
;
2013 expr
->ctype
= member
;
2017 deref
= deref
->unop
;
2018 expr
->deref
= deref
;
2020 add
= evaluate_offset(deref
, offset
);
2021 expr
->type
= EXPR_PREOP
;
2025 expr
->ctype
= member
;
2029 static int is_promoted(struct expression
*expr
)
2032 switch (expr
->type
) {
2035 case EXPR_CONDITIONAL
:
2059 static struct symbol
*evaluate_cast(struct expression
*);
2061 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2063 struct symbol
*sym
= expr
->cast_type
;
2065 sym
= evaluate_expression(expr
->cast_expression
);
2069 * Expressions of restricted types will possibly get
2070 * promoted - check that here
2072 if (is_restricted_type(sym
)) {
2073 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2075 } else if (is_fouled_type(sym
)) {
2079 examine_symbol_type(sym
);
2080 if (is_bitfield_type(sym
)) {
2081 expression_error(expr
, "trying to examine bitfield type");
2087 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2089 struct symbol
*type
;
2092 type
= evaluate_type_information(expr
);
2096 size
= type
->bit_size
;
2098 if (size
< 0 && is_void_type(type
)) {
2099 warning(expr
->pos
, "expression using sizeof(void)");
2100 size
= bits_in_char
;
2103 if (size
== 1 && is_bool_type(type
)) {
2105 warning(expr
->pos
, "expression using sizeof bool");
2106 size
= bits_in_char
;
2109 if (is_function(type
->ctype
.base_type
)) {
2110 warning(expr
->pos
, "expression using sizeof on a function");
2111 size
= bits_in_char
;
2114 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2115 expression_error(expr
, "cannot size expression");
2117 expr
->type
= EXPR_VALUE
;
2118 expr
->value
= bits_to_bytes(size
);
2120 expr
->ctype
= size_t_ctype
;
2121 return size_t_ctype
;
2124 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2126 struct symbol
*type
;
2129 type
= evaluate_type_information(expr
);
2133 if (type
->type
== SYM_NODE
)
2134 type
= type
->ctype
.base_type
;
2137 switch (type
->type
) {
2141 type
= get_base_type(type
);
2145 expression_error(expr
, "expected pointer expression");
2148 size
= type
->bit_size
;
2149 if (size
& (bits_in_char
-1))
2151 expr
->type
= EXPR_VALUE
;
2152 expr
->value
= bits_to_bytes(size
);
2154 expr
->ctype
= size_t_ctype
;
2155 return size_t_ctype
;
2158 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2160 struct symbol
*type
;
2162 type
= evaluate_type_information(expr
);
2166 expr
->type
= EXPR_VALUE
;
2167 expr
->value
= type
->ctype
.alignment
;
2169 expr
->ctype
= size_t_ctype
;
2170 return size_t_ctype
;
2173 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
2175 struct expression
*expr
;
2176 struct symbol_list
*argument_types
= fn
->arguments
;
2177 struct symbol
*argtype
;
2180 PREPARE_PTR_LIST(argument_types
, argtype
);
2181 FOR_EACH_PTR (head
, expr
) {
2182 struct expression
**p
= THIS_ADDRESS(expr
);
2183 struct symbol
*ctype
, *target
;
2184 ctype
= evaluate_expression(expr
);
2191 struct symbol
*type
;
2192 int class = classify_type(ctype
, &type
);
2193 if (is_int(class)) {
2194 *p
= cast_to(expr
, integer_promotion(type
));
2195 } else if (class & TYPE_FLOAT
) {
2196 unsigned long mod
= type
->ctype
.modifiers
;
2197 if (!(mod
& (MOD_LONG_ALL
)))
2198 *p
= cast_to(expr
, &double_ctype
);
2199 } else if (class & TYPE_PTR
) {
2200 if (expr
->ctype
== &null_ctype
)
2201 *p
= cast_to(expr
, &ptr_ctype
);
2205 } else if (!target
->forced_arg
){
2206 static char where
[30];
2207 examine_symbol_type(target
);
2208 sprintf(where
, "argument %d", i
);
2209 compatible_argument_type(expr
, target
, p
, where
);
2213 NEXT_PTR_LIST(argtype
);
2214 } END_FOR_EACH_PTR(expr
);
2215 FINISH_PTR_LIST(argtype
);
2219 static void convert_index(struct expression
*e
)
2221 struct expression
*child
= e
->idx_expression
;
2222 unsigned from
= e
->idx_from
;
2223 unsigned to
= e
->idx_to
+ 1;
2225 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2226 e
->init_nr
= to
- from
;
2227 e
->init_expr
= child
;
2230 static void convert_ident(struct expression
*e
)
2232 struct expression
*child
= e
->ident_expression
;
2233 int offset
= e
->offset
;
2236 e
->init_offset
= offset
;
2238 e
->init_expr
= child
;
2241 static void convert_designators(struct expression
*e
)
2244 if (e
->type
== EXPR_INDEX
)
2246 else if (e
->type
== EXPR_IDENTIFIER
)
2254 static void excess(struct expression
*e
, const char *s
)
2256 warning(e
->pos
, "excessive elements in %s initializer", s
);
2260 * implicit designator for the first element
2262 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2263 struct expression
**v
)
2265 struct expression
*e
= *v
, *new;
2267 if (ctype
->type
== SYM_NODE
)
2268 ctype
= ctype
->ctype
.base_type
;
2270 if (class & TYPE_PTR
) { /* array */
2271 if (!ctype
->bit_size
)
2273 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2274 new->idx_expression
= e
;
2275 new->ctype
= ctype
->ctype
.base_type
;
2277 struct symbol
*field
, *p
;
2278 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2279 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2285 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2286 new->ident_expression
= e
;
2287 new->field
= new->ctype
= field
;
2288 new->offset
= field
->offset
;
2295 * sanity-check explicit designators; return the innermost one or NULL
2296 * in case of error. Assign types.
2298 static struct expression
*check_designators(struct expression
*e
,
2299 struct symbol
*ctype
)
2301 struct expression
*last
= NULL
;
2304 if (ctype
->type
== SYM_NODE
)
2305 ctype
= ctype
->ctype
.base_type
;
2306 if (e
->type
== EXPR_INDEX
) {
2307 struct symbol
*type
;
2308 if (ctype
->type
!= SYM_ARRAY
) {
2309 err
= "array index in non-array";
2312 type
= ctype
->ctype
.base_type
;
2313 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2314 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2315 if (offset
>= ctype
->bit_size
) {
2316 err
= "index out of bounds in";
2320 e
->ctype
= ctype
= type
;
2323 if (!e
->idx_expression
) {
2327 e
= e
->idx_expression
;
2328 } else if (e
->type
== EXPR_IDENTIFIER
) {
2330 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2331 err
= "field name not in struct or union";
2334 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2336 err
= "unknown field name in";
2340 e
->field
= e
->ctype
= ctype
;
2342 if (!e
->ident_expression
) {
2346 e
= e
->ident_expression
;
2347 } else if (e
->type
== EXPR_POS
) {
2348 err
= "internal front-end error: EXPR_POS in";
2353 expression_error(e
, "%s initializer", err
);
2358 * choose the next subobject to initialize.
2360 * Get designators for next element, switch old ones to EXPR_POS.
2361 * Return the resulting expression or NULL if we'd run out of subobjects.
2362 * The innermost designator is returned in *v. Designators in old
2363 * are assumed to be already sanity-checked.
2365 static struct expression
*next_designators(struct expression
*old
,
2366 struct symbol
*ctype
,
2367 struct expression
*e
, struct expression
**v
)
2369 struct expression
*new = NULL
;
2373 if (old
->type
== EXPR_INDEX
) {
2374 struct expression
*copy
;
2377 copy
= next_designators(old
->idx_expression
,
2380 n
= old
->idx_to
+ 1;
2381 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2386 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2389 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2392 new->idx_from
= new->idx_to
= n
;
2393 new->idx_expression
= copy
;
2394 new->ctype
= old
->ctype
;
2396 } else if (old
->type
== EXPR_IDENTIFIER
) {
2397 struct expression
*copy
;
2398 struct symbol
*field
;
2401 copy
= next_designators(old
->ident_expression
,
2404 field
= old
->field
->next_subobject
;
2410 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2412 * We can't necessarily trust "field->offset",
2413 * because the field might be in an anonymous
2414 * union, and the field offset is then the offset
2415 * within that union.
2417 * The "old->offset - old->field->offset"
2418 * would be the offset of such an anonymous
2421 offset
= old
->offset
- old
->field
->offset
;
2424 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2428 new->expr_ident
= field
->ident
;
2429 new->ident_expression
= copy
;
2431 new->offset
= field
->offset
+ offset
;
2437 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2438 int class, struct symbol
*ctype
);
2441 * deal with traversing subobjects [6.7.8(17,18,20)]
2443 static void handle_list_initializer(struct expression
*expr
,
2444 int class, struct symbol
*ctype
)
2446 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2449 FOR_EACH_PTR(expr
->expr_list
, e
) {
2450 struct expression
**v
;
2451 struct symbol
*type
;
2454 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2455 struct symbol
*struct_sym
;
2458 last
= first_subobject(ctype
, class, &top
);
2460 last
= next_designators(last
, ctype
, e
, &top
);
2463 excess(e
, class & TYPE_PTR
? "array" :
2465 DELETE_CURRENT_PTR(e
);
2468 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2469 if (Wdesignated_init
&& struct_sym
->designated_init
)
2470 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2471 ctype
->ident
? "in initializer for " : "",
2472 ctype
->ident
? ctype
->ident
->len
: 0,
2473 ctype
->ident
? ctype
->ident
->name
: "",
2474 ctype
->ident
? ": " : "",
2475 get_type_name(struct_sym
->type
),
2476 show_ident(struct_sym
->ident
));
2478 warning(e
->pos
, "advancing past deep designator");
2481 REPLACE_CURRENT_PTR(e
, last
);
2483 next
= check_designators(e
, ctype
);
2485 DELETE_CURRENT_PTR(e
);
2489 /* deeper than one designator? */
2491 convert_designators(last
);
2496 lclass
= classify_type(top
->ctype
, &type
);
2497 if (top
->type
== EXPR_INDEX
)
2498 v
= &top
->idx_expression
;
2500 v
= &top
->ident_expression
;
2502 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2505 if (!(lclass
& TYPE_COMPOUND
)) {
2506 warning(e
->pos
, "bogus scalar initializer");
2507 DELETE_CURRENT_PTR(e
);
2511 next
= first_subobject(type
, lclass
, v
);
2513 warning(e
->pos
, "missing braces around initializer");
2518 DELETE_CURRENT_PTR(e
);
2519 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2521 } END_FOR_EACH_PTR(e
);
2523 convert_designators(last
);
2524 expr
->ctype
= ctype
;
2527 static int is_string_literal(struct expression
**v
)
2529 struct expression
*e
= *v
;
2530 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2532 if (!e
|| e
->type
!= EXPR_STRING
)
2534 if (e
!= *v
&& Wparen_string
)
2536 "array initialized from parenthesized string constant");
2542 * We want a normal expression, possibly in one layer of braces. Warn
2543 * if the latter happens inside a list (it's legal, but likely to be
2544 * an effect of screwup). In case of anything not legal, we are definitely
2545 * having an effect of screwup, so just fail and let the caller warn.
2547 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2549 struct expression
*v
= NULL
, *p
;
2553 if (e
->type
!= EXPR_INITIALIZER
)
2556 FOR_EACH_PTR(e
->expr_list
, p
) {
2560 } END_FOR_EACH_PTR(p
);
2564 case EXPR_INITIALIZER
:
2566 case EXPR_IDENTIFIER
:
2572 warning(e
->pos
, "braces around scalar initializer");
2577 * deal with the cases that don't care about subobjects:
2578 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2579 * character array <- string literal, possibly in braces [6.7.8(14)]
2580 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2581 * compound type <- initializer list in braces [6.7.8(16)]
2582 * The last one punts to handle_list_initializer() which, in turn will call
2583 * us for individual elements of the list.
2585 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2586 * the lack of support of wide char stuff in general.
2588 * One note: we need to take care not to evaluate a string literal until
2589 * we know that we *will* handle it right here. Otherwise we would screw
2590 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2591 * { "string", ...} - we need to preserve that string literal recognizable
2592 * until we dig into the inner struct.
2594 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2595 int class, struct symbol
*ctype
)
2597 int is_string
= is_string_type(ctype
);
2598 struct expression
*e
= *ep
, *p
;
2599 struct symbol
*type
;
2605 if (!(class & TYPE_COMPOUND
)) {
2606 e
= handle_scalar(e
, nested
);
2610 if (!evaluate_expression(e
))
2612 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2617 * sublist; either a string, or we dig in; the latter will deal with
2618 * pathologies, so we don't need anything fancy here.
2620 if (e
->type
== EXPR_INITIALIZER
) {
2622 struct expression
*v
= NULL
;
2625 FOR_EACH_PTR(e
->expr_list
, p
) {
2629 } END_FOR_EACH_PTR(p
);
2630 if (count
== 1 && is_string_literal(&v
)) {
2635 handle_list_initializer(e
, class, ctype
);
2640 if (is_string_literal(&e
)) {
2641 /* either we are doing array of char, or we'll have to dig in */
2648 /* struct or union can be initialized by compatible */
2649 if (class != TYPE_COMPOUND
)
2651 type
= evaluate_expression(e
);
2654 if (ctype
->type
== SYM_NODE
)
2655 ctype
= ctype
->ctype
.base_type
;
2656 if (type
->type
== SYM_NODE
)
2657 type
= type
->ctype
.base_type
;
2663 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2665 type
= evaluate_expression(p
);
2666 if (ctype
->bit_size
!= -1) {
2667 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2669 "too long initializer-string for array of char");
2670 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2672 "too long initializer-string for array of char(no space for nul char)");
2679 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2681 struct symbol
*type
;
2682 int class = classify_type(ctype
, &type
);
2683 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2684 expression_error(*ep
, "invalid initializer");
2687 static struct symbol
*evaluate_cast(struct expression
*expr
)
2689 struct expression
*target
= expr
->cast_expression
;
2690 struct symbol
*ctype
;
2691 struct symbol
*t1
, *t2
;
2693 int as1
= 0, as2
= 0;
2699 * Special case: a cast can be followed by an
2700 * initializer, in which case we need to pass
2701 * the type value down to that initializer rather
2702 * than trying to evaluate it as an expression
2704 * A more complex case is when the initializer is
2705 * dereferenced as part of a post-fix expression.
2706 * We need to produce an expression that can be dereferenced.
2708 if (target
->type
== EXPR_INITIALIZER
) {
2709 struct symbol
*sym
= expr
->cast_type
;
2710 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2712 sym
->initializer
= target
;
2713 evaluate_symbol(sym
);
2715 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2718 expr
->type
= EXPR_PREOP
;
2726 ctype
= examine_symbol_type(expr
->cast_type
);
2727 expr
->ctype
= ctype
;
2728 expr
->cast_type
= ctype
;
2730 evaluate_expression(target
);
2733 class1
= classify_type(ctype
, &t1
);
2735 /* cast to non-integer type -> not an integer constant expression */
2736 if (!is_int(class1
))
2738 /* if argument turns out to be not an integer constant expression *and*
2739 it was not a floating literal to start with -> too bad */
2740 else if (expr
->flags
== Int_const_expr
&&
2741 !(target
->flags
& Int_const_expr
))
2744 * You can always throw a value away by casting to
2745 * "void" - that's an implicit "force". Note that
2746 * the same is _not_ true of "void *".
2748 if (t1
== &void_ctype
)
2751 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2752 warning(expr
->pos
, "cast to non-scalar");
2756 expression_error(expr
, "cast from unknown type");
2759 class2
= classify_type(t2
, &t2
);
2761 if (class2
& TYPE_COMPOUND
)
2762 warning(expr
->pos
, "cast from non-scalar");
2764 if (expr
->type
== EXPR_FORCE_CAST
)
2767 /* allowed cast unfouls */
2768 if (class2
& TYPE_FOULED
)
2772 if (class1
& TYPE_RESTRICT
)
2773 warning(expr
->pos
, "cast to %s",
2775 if (class2
& TYPE_RESTRICT
)
2776 warning(expr
->pos
, "cast from %s",
2780 if (t1
== &ulong_ctype
)
2782 else if (class1
== TYPE_PTR
) {
2783 examine_pointer_target(t1
);
2787 if (t2
== &ulong_ctype
)
2789 else if (class2
== TYPE_PTR
) {
2790 examine_pointer_target(t2
);
2794 if (!as1
&& as2
> 0)
2795 warning(expr
->pos
, "cast removes address space of expression");
2796 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2797 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2798 if (as1
> 0 && !as2
&&
2799 !is_null_pointer_constant(target
) && Wcast_to_as
)
2801 "cast adds address space to expression (<asn:%d>)", as1
);
2803 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2804 !as1
&& (target
->flags
& Int_const_expr
)) {
2805 if (t1
->ctype
.base_type
== &void_ctype
) {
2806 if (is_zero_constant(target
)) {
2808 expr
->type
= EXPR_VALUE
;
2809 expr
->ctype
= &null_ctype
;
2820 * Evaluate a call expression with a symbol. This
2821 * should expand inline functions, and evaluate
2824 static int evaluate_symbol_call(struct expression
*expr
)
2826 struct expression
*fn
= expr
->fn
;
2827 struct symbol
*ctype
= fn
->ctype
;
2829 if (fn
->type
!= EXPR_PREOP
)
2832 if (ctype
->op
&& ctype
->op
->evaluate
)
2833 return ctype
->op
->evaluate(expr
);
2835 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2837 struct symbol
*curr
= current_fn
;
2839 if (ctype
->definition
)
2840 ctype
= ctype
->definition
;
2842 current_fn
= ctype
->ctype
.base_type
;
2844 ret
= inline_function(expr
, ctype
);
2846 /* restore the old function */
2854 static struct symbol
*evaluate_call(struct expression
*expr
)
2857 struct symbol
*ctype
, *sym
;
2858 struct expression
*fn
= expr
->fn
;
2859 struct expression_list
*arglist
= expr
->args
;
2861 if (!evaluate_expression(fn
))
2863 sym
= ctype
= fn
->ctype
;
2864 if (ctype
->type
== SYM_NODE
)
2865 ctype
= ctype
->ctype
.base_type
;
2866 if (ctype
->type
== SYM_PTR
)
2867 ctype
= get_base_type(ctype
);
2869 if (ctype
->type
!= SYM_FN
) {
2870 struct expression
*arg
;
2871 expression_error(expr
, "not a function %s",
2872 show_ident(sym
->ident
));
2873 /* do typechecking in arguments */
2874 FOR_EACH_PTR (arglist
, arg
) {
2875 evaluate_expression(arg
);
2876 } END_FOR_EACH_PTR(arg
);
2880 examine_fn_arguments(ctype
);
2881 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2882 sym
->op
&& sym
->op
->args
) {
2883 if (!sym
->op
->args(expr
))
2886 if (!evaluate_arguments(ctype
, arglist
))
2888 args
= expression_list_size(expr
->args
);
2889 fnargs
= symbol_list_size(ctype
->arguments
);
2891 expression_error(expr
,
2892 "not enough arguments for function %s",
2893 show_ident(sym
->ident
));
2894 if (args
> fnargs
&& !ctype
->variadic
)
2895 expression_error(expr
,
2896 "too many arguments for function %s",
2897 show_ident(sym
->ident
));
2899 if (sym
->type
== SYM_NODE
) {
2900 if (evaluate_symbol_call(expr
))
2903 expr
->ctype
= ctype
->ctype
.base_type
;
2907 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2909 struct expression
*e
= expr
->down
;
2910 struct symbol
*ctype
= expr
->in
;
2913 if (expr
->op
== '.') {
2914 struct symbol
*field
;
2917 expression_error(expr
, "expected structure or union");
2920 examine_symbol_type(ctype
);
2921 class = classify_type(ctype
, &ctype
);
2922 if (class != TYPE_COMPOUND
) {
2923 expression_error(expr
, "expected structure or union");
2927 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2929 expression_error(expr
, "unknown member");
2933 expr
->type
= EXPR_VALUE
;
2934 expr
->flags
= Int_const_expr
;
2935 expr
->value
= offset
;
2937 expr
->ctype
= size_t_ctype
;
2940 expression_error(expr
, "expected structure or union");
2943 examine_symbol_type(ctype
);
2944 class = classify_type(ctype
, &ctype
);
2945 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2946 expression_error(expr
, "expected array");
2949 ctype
= ctype
->ctype
.base_type
;
2951 expr
->type
= EXPR_VALUE
;
2952 expr
->flags
= Int_const_expr
;
2955 expr
->ctype
= size_t_ctype
;
2957 struct expression
*idx
= expr
->index
, *m
;
2958 struct symbol
*i_type
= evaluate_expression(idx
);
2959 int i_class
= classify_type(i_type
, &i_type
);
2960 if (!is_int(i_class
)) {
2961 expression_error(expr
, "non-integer index");
2964 unrestrict(idx
, i_class
, &i_type
);
2965 idx
= cast_to(idx
, size_t_ctype
);
2966 m
= alloc_const_expression(expr
->pos
,
2967 bits_to_bytes(ctype
->bit_size
));
2968 m
->ctype
= size_t_ctype
;
2969 m
->flags
= Int_const_expr
;
2970 expr
->type
= EXPR_BINOP
;
2974 expr
->ctype
= size_t_ctype
;
2975 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2979 struct expression
*copy
= __alloc_expression(0);
2981 if (e
->type
== EXPR_OFFSETOF
)
2983 if (!evaluate_expression(e
))
2985 expr
->type
= EXPR_BINOP
;
2986 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2988 expr
->ctype
= size_t_ctype
;
2992 return size_t_ctype
;
2995 struct symbol
*evaluate_expression(struct expression
*expr
)
3002 switch (expr
->type
) {
3005 expression_error(expr
, "value expression without a type");
3008 return evaluate_string(expr
);
3010 return evaluate_symbol_expression(expr
);
3012 if (!evaluate_expression(expr
->left
))
3014 if (!evaluate_expression(expr
->right
))
3016 return evaluate_binop(expr
);
3018 return evaluate_logical(expr
);
3020 evaluate_expression(expr
->left
);
3021 if (!evaluate_expression(expr
->right
))
3023 return evaluate_comma(expr
);
3025 if (!evaluate_expression(expr
->left
))
3027 if (!evaluate_expression(expr
->right
))
3029 return evaluate_compare(expr
);
3030 case EXPR_ASSIGNMENT
:
3031 if (!evaluate_expression(expr
->left
))
3033 if (!evaluate_expression(expr
->right
))
3035 return evaluate_assignment(expr
);
3037 if (!evaluate_expression(expr
->unop
))
3039 return evaluate_preop(expr
);
3041 if (!evaluate_expression(expr
->unop
))
3043 return evaluate_postop(expr
);
3045 case EXPR_FORCE_CAST
:
3046 case EXPR_IMPLIED_CAST
:
3047 return evaluate_cast(expr
);
3049 return evaluate_sizeof(expr
);
3050 case EXPR_PTRSIZEOF
:
3051 return evaluate_ptrsizeof(expr
);
3053 return evaluate_alignof(expr
);
3055 return evaluate_member_dereference(expr
);
3057 return evaluate_call(expr
);
3059 case EXPR_CONDITIONAL
:
3060 return evaluate_conditional_expression(expr
);
3061 case EXPR_STATEMENT
:
3062 expr
->ctype
= evaluate_statement(expr
->statement
);
3066 expr
->ctype
= &ptr_ctype
;
3070 /* Evaluate the type of the symbol .. */
3071 evaluate_symbol(expr
->symbol
);
3072 /* .. but the type of the _expression_ is a "type" */
3073 expr
->ctype
= &type_ctype
;
3077 return evaluate_offsetof(expr
);
3079 /* These can not exist as stand-alone expressions */
3080 case EXPR_INITIALIZER
:
3081 case EXPR_IDENTIFIER
:
3084 expression_error(expr
, "internal front-end error: initializer in expression");
3087 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3093 static void check_duplicates(struct symbol
*sym
)
3096 struct symbol
*next
= sym
;
3097 int initialized
= sym
->initializer
!= NULL
;
3099 while ((next
= next
->same_symbol
) != NULL
) {
3100 const char *typediff
;
3101 evaluate_symbol(next
);
3102 if (initialized
&& next
->initializer
) {
3103 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3104 show_ident(sym
->ident
),
3105 stream_name(next
->pos
.stream
), next
->pos
.line
);
3106 /* Only warn once */
3110 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3112 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3113 show_ident(sym
->ident
),
3114 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3119 unsigned long mod
= sym
->ctype
.modifiers
;
3120 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3122 if (!(mod
& MOD_TOPLEVEL
))
3126 if (sym
->ident
== &main_ident
)
3128 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3132 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3134 struct symbol
*base_type
;
3142 sym
= examine_symbol_type(sym
);
3143 base_type
= get_base_type(sym
);
3147 /* Evaluate the initializers */
3148 if (sym
->initializer
)
3149 evaluate_initializer(sym
, &sym
->initializer
);
3151 /* And finally, evaluate the body of the symbol too */
3152 if (base_type
->type
== SYM_FN
) {
3153 struct symbol
*curr
= current_fn
;
3155 if (sym
->definition
&& sym
->definition
!= sym
)
3156 return evaluate_symbol(sym
->definition
);
3158 current_fn
= base_type
;
3160 examine_fn_arguments(base_type
);
3161 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3163 if (base_type
->stmt
)
3164 evaluate_statement(base_type
->stmt
);
3172 void evaluate_symbol_list(struct symbol_list
*list
)
3176 FOR_EACH_PTR(list
, sym
) {
3177 evaluate_symbol(sym
);
3178 check_duplicates(sym
);
3179 } END_FOR_EACH_PTR(sym
);
3182 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3184 struct expression
*expr
= stmt
->expression
;
3185 struct symbol
*fntype
;
3187 evaluate_expression(expr
);
3188 fntype
= current_fn
->ctype
.base_type
;
3189 if (!fntype
|| fntype
== &void_ctype
) {
3190 if (expr
&& expr
->ctype
!= &void_ctype
)
3191 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3192 if (expr
&& Wreturn_void
)
3193 warning(stmt
->pos
, "returning void-valued expression");
3198 sparse_error(stmt
->pos
, "return with no return value");
3203 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3207 static void evaluate_if_statement(struct statement
*stmt
)
3209 if (!stmt
->if_conditional
)
3212 evaluate_conditional(stmt
->if_conditional
, 0);
3213 evaluate_statement(stmt
->if_true
);
3214 evaluate_statement(stmt
->if_false
);
3217 static void evaluate_iterator(struct statement
*stmt
)
3219 evaluate_symbol_list(stmt
->iterator_syms
);
3220 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3221 evaluate_conditional(stmt
->iterator_post_condition
,1);
3222 evaluate_statement(stmt
->iterator_pre_statement
);
3223 evaluate_statement(stmt
->iterator_statement
);
3224 evaluate_statement(stmt
->iterator_post_statement
);
3227 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3229 switch (*constraint
) {
3230 case '=': /* Assignment */
3231 case '+': /* Update */
3234 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3238 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3240 switch (*constraint
) {
3241 case '=': /* Assignment */
3242 case '+': /* Update */
3243 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3247 static void evaluate_asm_statement(struct statement
*stmt
)
3249 struct expression
*expr
;
3253 expr
= stmt
->asm_string
;
3254 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3255 sparse_error(stmt
->pos
, "need constant string for inline asm");
3260 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3262 case 0: /* Identifier */
3266 case 1: /* Constraint */
3268 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3269 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3270 *THIS_ADDRESS(expr
) = NULL
;
3273 verify_output_constraint(expr
, expr
->string
->data
);
3276 case 2: /* Expression */
3278 if (!evaluate_expression(expr
))
3280 if (!lvalue_expression(expr
))
3281 warning(expr
->pos
, "asm output is not an lvalue");
3282 evaluate_assign_to(expr
, expr
->ctype
);
3285 } END_FOR_EACH_PTR(expr
);
3288 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3290 case 0: /* Identifier */
3294 case 1: /* Constraint */
3296 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3297 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3298 *THIS_ADDRESS(expr
) = NULL
;
3301 verify_input_constraint(expr
, expr
->string
->data
);
3304 case 2: /* Expression */
3306 if (!evaluate_expression(expr
))
3310 } END_FOR_EACH_PTR(expr
);
3312 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3314 sparse_error(stmt
->pos
, "bad asm clobbers");
3317 if (expr
->type
== EXPR_STRING
)
3319 expression_error(expr
, "asm clobber is not a string");
3320 } END_FOR_EACH_PTR(expr
);
3322 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3323 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3324 sparse_error(stmt
->pos
, "bad asm label");
3327 } END_FOR_EACH_PTR(sym
);
3330 static void evaluate_case_statement(struct statement
*stmt
)
3332 evaluate_expression(stmt
->case_expression
);
3333 evaluate_expression(stmt
->case_to
);
3334 evaluate_statement(stmt
->case_statement
);
3337 static void check_case_type(struct expression
*switch_expr
,
3338 struct expression
*case_expr
,
3339 struct expression
**enumcase
)
3341 struct symbol
*switch_type
, *case_type
;
3347 switch_type
= switch_expr
->ctype
;
3348 case_type
= evaluate_expression(case_expr
);
3350 if (!switch_type
|| !case_type
)
3354 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3355 else if (is_enum_type(case_type
))
3356 *enumcase
= case_expr
;
3359 sclass
= classify_type(switch_type
, &switch_type
);
3360 cclass
= classify_type(case_type
, &case_type
);
3362 /* both should be arithmetic */
3363 if (!(sclass
& cclass
& TYPE_NUM
))
3366 /* neither should be floating */
3367 if ((sclass
| cclass
) & TYPE_FLOAT
)
3370 /* if neither is restricted, we are OK */
3371 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3374 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3375 cclass
, sclass
, case_type
, switch_type
)) {
3376 unrestrict(case_expr
, cclass
, &case_type
);
3377 unrestrict(switch_expr
, sclass
, &switch_type
);
3382 expression_error(case_expr
, "incompatible types for 'case' statement");
3385 static void evaluate_switch_statement(struct statement
*stmt
)
3388 struct expression
*enumcase
= NULL
;
3389 struct expression
**enumcase_holder
= &enumcase
;
3390 struct expression
*sel
= stmt
->switch_expression
;
3392 evaluate_expression(sel
);
3393 evaluate_statement(stmt
->switch_statement
);
3396 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3397 enumcase_holder
= NULL
; /* Only check cases against switch */
3399 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3400 struct statement
*case_stmt
= sym
->stmt
;
3401 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3402 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3403 } END_FOR_EACH_PTR(sym
);
3406 static void evaluate_goto_statement(struct statement
*stmt
)
3408 struct symbol
*label
= stmt
->goto_label
;
3410 if (label
&& !label
->stmt
&& !lookup_keyword(label
->ident
, NS_KEYWORD
))
3411 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3413 evaluate_expression(stmt
->goto_expression
);
3416 struct symbol
*evaluate_statement(struct statement
*stmt
)
3421 switch (stmt
->type
) {
3422 case STMT_DECLARATION
: {
3424 FOR_EACH_PTR(stmt
->declaration
, s
) {
3426 } END_FOR_EACH_PTR(s
);
3431 return evaluate_return_expression(stmt
);
3433 case STMT_EXPRESSION
:
3434 if (!evaluate_expression(stmt
->expression
))
3436 if (stmt
->expression
->ctype
== &null_ctype
)
3437 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3438 return degenerate(stmt
->expression
);
3440 case STMT_COMPOUND
: {
3441 struct statement
*s
;
3442 struct symbol
*type
= NULL
;
3444 /* Evaluate the return symbol in the compound statement */
3445 evaluate_symbol(stmt
->ret
);
3448 * Then, evaluate each statement, making the type of the
3449 * compound statement be the type of the last statement
3451 type
= evaluate_statement(stmt
->args
);
3452 FOR_EACH_PTR(stmt
->stmts
, s
) {
3453 type
= evaluate_statement(s
);
3454 } END_FOR_EACH_PTR(s
);
3460 evaluate_if_statement(stmt
);
3463 evaluate_iterator(stmt
);
3466 evaluate_switch_statement(stmt
);
3469 evaluate_case_statement(stmt
);
3472 return evaluate_statement(stmt
->label_statement
);
3474 evaluate_goto_statement(stmt
);
3479 evaluate_asm_statement(stmt
);
3482 evaluate_expression(stmt
->expression
);
3485 evaluate_expression(stmt
->range_expression
);
3486 evaluate_expression(stmt
->range_low
);
3487 evaluate_expression(stmt
->range_high
);