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 target
= usual_conversions(op
, expr
->left
, expr
->right
,
1284 tclass
, sclass
, target
, source
);
1286 expr
->right
= cast_to(expr
->right
, target
);
1290 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1293 return 0; /* yes, 0 - we don't want a cast_to here */
1294 if (t1
== &void_ctype
)
1296 if (t2
== &void_ctype
)
1298 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1300 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1304 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1306 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1311 static int check_assignment_types(struct symbol
*target
, struct expression
**rp
,
1312 const char **typediff
)
1314 struct symbol
*source
= degenerate(*rp
);
1315 struct symbol
*t
, *s
;
1316 int tclass
= classify_type(target
, &t
);
1317 int sclass
= classify_type(source
, &s
);
1319 if (tclass
& sclass
& TYPE_NUM
) {
1320 if (tclass
& TYPE_RESTRICT
) {
1321 /* allowed assignments unfoul */
1322 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1324 if (!restricted_value(*rp
, target
))
1328 } else if (!(sclass
& TYPE_RESTRICT
))
1330 *typediff
= "different base types";
1334 if (tclass
== TYPE_PTR
) {
1335 unsigned long mod1
, mod2
;
1336 struct symbol
*b1
, *b2
;
1337 // NULL pointer is always OK
1338 int is_null
= is_null_pointer_constant(*rp
);
1344 if (!(sclass
& TYPE_PTR
)) {
1345 *typediff
= "different base types";
1348 b1
= examine_pointer_target(t
);
1349 b2
= examine_pointer_target(s
);
1350 mod1
= target_qualifiers(t
);
1351 mod2
= target_qualifiers(s
);
1352 if (whitelist_pointers(b1
, b2
)) {
1354 * assignments to/from void * are OK, provided that
1355 * we do not remove qualifiers from pointed to [C]
1356 * or mix address spaces [sparse].
1358 if (t
->ctype
.as
!= s
->ctype
.as
) {
1359 *typediff
= "different address spaces";
1363 * If this is a function pointer assignment, it is
1364 * actually fine to assign a pointer to const data to
1365 * it, as a function pointer points to const data
1366 * implicitly, i.e., dereferencing it does not produce
1369 if (b1
->type
== SYM_FN
)
1372 *typediff
= "different modifiers";
1377 /* It's OK if the target is more volatile or const than the source */
1378 *typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1384 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1387 if (tclass
& TYPE_NUM
) {
1388 /* XXX: need to turn into comparison with NULL */
1389 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1391 *typediff
= "different base types";
1394 *typediff
= "invalid types";
1398 *rp
= cast_to(*rp
, target
);
1402 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1403 struct expression
**rp
, const char *where
)
1405 const char *typediff
;
1406 struct symbol
*source
= degenerate(*rp
);
1408 if (!check_assignment_types(target
, rp
, &typediff
)) {
1409 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1410 info(expr
->pos
, " expected %s", show_typename(target
));
1411 info(expr
->pos
, " got %s", show_typename(source
));
1412 *rp
= cast_to(*rp
, target
);
1419 static int compatible_transparent_union(struct symbol
*target
,
1420 struct expression
**rp
)
1422 struct symbol
*t
, *member
;
1423 classify_type(target
, &t
);
1424 if (t
->type
!= SYM_UNION
|| !t
->transparent_union
)
1427 FOR_EACH_PTR(t
->symbol_list
, member
) {
1428 const char *typediff
;
1429 if (check_assignment_types(member
, rp
, &typediff
))
1431 } END_FOR_EACH_PTR(member
);
1436 static int compatible_argument_type(struct expression
*expr
, struct symbol
*target
,
1437 struct expression
**rp
, const char *where
)
1439 if (compatible_transparent_union(target
, rp
))
1442 return compatible_assignment_types(expr
, target
, rp
, where
);
1445 static void mark_assigned(struct expression
*expr
)
1451 switch (expr
->type
) {
1456 if (sym
->type
!= SYM_NODE
)
1458 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1462 mark_assigned(expr
->left
);
1463 mark_assigned(expr
->right
);
1466 case EXPR_FORCE_CAST
:
1467 mark_assigned(expr
->cast_expression
);
1470 mark_assigned(expr
->base
);
1478 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1480 if (type
->ctype
.modifiers
& MOD_CONST
)
1481 expression_error(left
, "assignment to const expression");
1483 /* We know left is an lvalue, so it's a "preop-*" */
1484 mark_assigned(left
->unop
);
1487 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1489 struct expression
*left
= expr
->left
;
1490 struct expression
*where
= expr
;
1491 struct symbol
*ltype
;
1493 if (!lvalue_expression(left
)) {
1494 expression_error(expr
, "not an lvalue");
1498 ltype
= left
->ctype
;
1500 if (expr
->op
!= '=') {
1501 if (!evaluate_assign_op(expr
))
1504 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1508 evaluate_assign_to(left
, ltype
);
1510 expr
->ctype
= ltype
;
1514 static void examine_fn_arguments(struct symbol
*fn
)
1518 FOR_EACH_PTR(fn
->arguments
, s
) {
1519 struct symbol
*arg
= evaluate_symbol(s
);
1520 /* Array/function arguments silently degenerate into pointers */
1526 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1527 if (arg
->type
== SYM_ARRAY
)
1528 ptr
->ctype
= arg
->ctype
;
1530 ptr
->ctype
.base_type
= arg
;
1531 ptr
->ctype
.as
|= s
->ctype
.as
;
1532 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1534 s
->ctype
.base_type
= ptr
;
1536 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1539 examine_symbol_type(s
);
1546 } END_FOR_EACH_PTR(s
);
1549 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1551 /* Take the modifiers of the pointer, and apply them to the member */
1552 mod
|= sym
->ctype
.modifiers
;
1553 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1554 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1556 newsym
->ctype
.as
= as
;
1557 newsym
->ctype
.modifiers
= mod
;
1563 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1565 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1566 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1568 node
->ctype
.base_type
= ptr
;
1569 ptr
->bit_size
= bits_in_pointer
;
1570 ptr
->ctype
.alignment
= pointer_alignment
;
1572 node
->bit_size
= bits_in_pointer
;
1573 node
->ctype
.alignment
= pointer_alignment
;
1576 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1577 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1578 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1580 if (sym
->type
== SYM_NODE
) {
1581 ptr
->ctype
.as
|= sym
->ctype
.as
;
1582 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1583 sym
= sym
->ctype
.base_type
;
1585 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1586 ptr
->ctype
.as
|= sym
->ctype
.as
;
1587 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1588 sym
= sym
->ctype
.base_type
;
1590 ptr
->ctype
.base_type
= sym
;
1595 /* Arrays degenerate into pointers on pointer arithmetic */
1596 static struct symbol
*degenerate(struct expression
*expr
)
1598 struct symbol
*ctype
, *base
;
1602 ctype
= expr
->ctype
;
1605 base
= examine_symbol_type(ctype
);
1606 if (ctype
->type
== SYM_NODE
)
1607 base
= ctype
->ctype
.base_type
;
1609 * Arrays degenerate into pointers to the entries, while
1610 * functions degenerate into pointers to themselves.
1611 * If array was part of non-lvalue compound, we create a copy
1612 * of that compound first and then act as if we were dealing with
1613 * the corresponding field in there.
1615 switch (base
->type
) {
1617 if (expr
->type
== EXPR_SLICE
) {
1618 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1619 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1621 a
->ctype
.base_type
= expr
->base
->ctype
;
1622 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1623 a
->array_size
= expr
->base
->ctype
->array_size
;
1625 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1627 e0
->ctype
= &lazy_ptr_ctype
;
1629 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1632 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1634 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1636 e2
->right
= expr
->base
;
1638 e2
->ctype
= expr
->base
->ctype
;
1640 if (expr
->r_bitpos
) {
1641 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1644 e3
->right
= alloc_const_expression(expr
->pos
,
1645 bits_to_bytes(expr
->r_bitpos
));
1646 e3
->ctype
= &lazy_ptr_ctype
;
1651 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1654 e4
->ctype
= &lazy_ptr_ctype
;
1657 expr
->type
= EXPR_PREOP
;
1661 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1662 expression_error(expr
, "strange non-value function or array");
1665 *expr
= *expr
->unop
;
1666 ctype
= create_pointer(expr
, ctype
, 1);
1667 expr
->ctype
= ctype
;
1674 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1676 struct expression
*op
= expr
->unop
;
1677 struct symbol
*ctype
;
1679 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1680 expression_error(expr
, "not addressable");
1687 if (expr
->type
== EXPR_SYMBOL
) {
1688 struct symbol
*sym
= expr
->symbol
;
1689 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1693 * symbol expression evaluation is lazy about the type
1694 * of the sub-expression, so we may have to generate
1695 * the type here if so..
1697 if (expr
->ctype
== &lazy_ptr_ctype
) {
1698 ctype
= create_pointer(expr
, ctype
, 0);
1699 expr
->ctype
= ctype
;
1705 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1707 struct expression
*op
= expr
->unop
;
1708 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1710 /* Simplify: *&(expr) => (expr) */
1711 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1717 /* Dereferencing a node drops all the node information. */
1718 if (ctype
->type
== SYM_NODE
)
1719 ctype
= ctype
->ctype
.base_type
;
1721 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1722 target
= ctype
->ctype
.base_type
;
1724 switch (ctype
->type
) {
1726 expression_error(expr
, "cannot dereference this type");
1729 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1730 merge_type(node
, ctype
);
1734 if (!lvalue_expression(op
)) {
1735 expression_error(op
, "non-lvalue array??");
1739 /* Do the implied "addressof" on the array */
1743 * When an array is dereferenced, we need to pick
1744 * up the attributes of the original node too..
1746 merge_type(node
, op
->ctype
);
1747 merge_type(node
, ctype
);
1751 node
->bit_size
= target
->bit_size
;
1752 node
->array_size
= target
->array_size
;
1759 * Unary post-ops: x++ and x--
1761 static struct symbol
*evaluate_postop(struct expression
*expr
)
1763 struct expression
*op
= expr
->unop
;
1764 struct symbol
*ctype
= op
->ctype
;
1765 int class = classify_type(ctype
, &ctype
);
1768 if (!class || class & TYPE_COMPOUND
) {
1769 expression_error(expr
, "need scalar for ++/--");
1772 if (!lvalue_expression(expr
->unop
)) {
1773 expression_error(expr
, "need lvalue expression for ++/--");
1777 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1778 unrestrict(expr
, class, &ctype
);
1780 if (class & TYPE_NUM
) {
1782 } else if (class == TYPE_PTR
) {
1783 struct symbol
*target
= examine_pointer_target(ctype
);
1784 if (!is_function(target
))
1785 multiply
= bits_to_bytes(target
->bit_size
);
1789 evaluate_assign_to(op
, op
->ctype
);
1790 expr
->op_value
= multiply
;
1791 expr
->ctype
= ctype
;
1795 expression_error(expr
, "bad argument type for ++/--");
1799 static struct symbol
*evaluate_sign(struct expression
*expr
)
1801 struct symbol
*ctype
= expr
->unop
->ctype
;
1802 int class = classify_type(ctype
, &ctype
);
1803 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1805 /* should be an arithmetic type */
1806 if (!(class & TYPE_NUM
))
1807 return bad_expr_type(expr
);
1808 if (class & TYPE_RESTRICT
)
1811 if (!(class & TYPE_FLOAT
)) {
1812 ctype
= integer_promotion(ctype
);
1813 expr
->unop
= cast_to(expr
->unop
, ctype
);
1814 } else if (expr
->op
!= '~') {
1815 /* no conversions needed */
1817 return bad_expr_type(expr
);
1819 if (expr
->op
== '+')
1820 *expr
= *expr
->unop
;
1821 expr
->ctype
= ctype
;
1824 if (restricted_unop(expr
->op
, &ctype
))
1825 unrestrict(expr
, class, &ctype
);
1829 static struct symbol
*evaluate_preop(struct expression
*expr
)
1831 struct symbol
*ctype
= expr
->unop
->ctype
;
1835 *expr
= *expr
->unop
;
1841 return evaluate_sign(expr
);
1844 return evaluate_dereference(expr
);
1847 return evaluate_addressof(expr
);
1849 case SPECIAL_INCREMENT
:
1850 case SPECIAL_DECREMENT
:
1852 * From a type evaluation standpoint the preops are
1853 * the same as the postops
1855 return evaluate_postop(expr
);
1858 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1860 if (is_safe_type(ctype
))
1861 warning(expr
->pos
, "testing a 'safe expression'");
1862 if (is_float_type(ctype
)) {
1863 struct expression
*arg
= expr
->unop
;
1864 expr
->type
= EXPR_COMPARE
;
1865 expr
->op
= SPECIAL_EQUAL
;
1867 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1868 expr
->right
->ctype
= ctype
;
1869 expr
->right
->fvalue
= 0;
1870 } else if (is_fouled_type(ctype
)) {
1871 warning(expr
->pos
, "%s degrades to integer",
1872 show_typename(ctype
->ctype
.base_type
));
1874 /* the result is int [6.5.3.3(5)]*/
1881 expr
->ctype
= ctype
;
1885 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1887 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1888 struct ptr_list
*list
= head
;
1894 for (i
= 0; i
< list
->nr
; i
++) {
1895 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1897 if (sym
->ident
!= ident
)
1899 *offset
= sym
->offset
;
1902 struct symbol
*ctype
= sym
->ctype
.base_type
;
1906 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1908 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1911 *offset
+= sym
->offset
;
1915 } while ((list
= list
->next
) != head
);
1919 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1921 struct expression
*add
;
1924 * Create a new add-expression
1926 * NOTE! Even if we just add zero, we need a new node
1927 * for the member pointer, since it has a different
1928 * type than the original pointer. We could make that
1929 * be just a cast, but the fact is, a node is a node,
1930 * so we might as well just do the "add zero" here.
1932 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1935 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1936 add
->right
->ctype
= &int_ctype
;
1937 add
->right
->value
= offset
;
1940 * The ctype of the pointer will be lazily evaluated if
1941 * we ever take the address of this member dereference..
1943 add
->ctype
= &lazy_ptr_ctype
;
1947 /* structure/union dereference */
1948 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1951 struct symbol
*ctype
, *member
;
1952 struct expression
*deref
= expr
->deref
, *add
;
1953 struct ident
*ident
= expr
->member
;
1957 if (!evaluate_expression(deref
))
1960 expression_error(expr
, "bad member name");
1964 ctype
= deref
->ctype
;
1965 examine_symbol_type(ctype
);
1966 address_space
= ctype
->ctype
.as
;
1967 mod
= ctype
->ctype
.modifiers
;
1968 if (ctype
->type
== SYM_NODE
) {
1969 ctype
= ctype
->ctype
.base_type
;
1970 address_space
|= ctype
->ctype
.as
;
1971 mod
|= ctype
->ctype
.modifiers
;
1973 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1974 expression_error(expr
, "expected structure or union");
1978 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1980 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1981 const char *name
= "<unnamed>";
1984 name
= ctype
->ident
->name
;
1985 namelen
= ctype
->ident
->len
;
1987 if (ctype
->symbol_list
)
1988 expression_error(expr
, "no member '%s' in %s %.*s",
1989 show_ident(ident
), type
, namelen
, name
);
1991 expression_error(expr
, "using member '%s' in "
1992 "incomplete %s %.*s", show_ident(ident
),
1993 type
, namelen
, name
);
1998 * The member needs to take on the address space and modifiers of
1999 * the "parent" type.
2001 member
= convert_to_as_mod(member
, address_space
, mod
);
2002 ctype
= get_base_type(member
);
2004 if (!lvalue_expression(deref
)) {
2005 if (deref
->type
!= EXPR_SLICE
) {
2009 expr
->base
= deref
->base
;
2010 expr
->r_bitpos
= deref
->r_bitpos
;
2012 expr
->r_bitpos
+= bytes_to_bits(offset
);
2013 expr
->type
= EXPR_SLICE
;
2014 expr
->r_nrbits
= member
->bit_size
;
2015 expr
->r_bitpos
+= member
->bit_offset
;
2016 expr
->ctype
= member
;
2020 deref
= deref
->unop
;
2021 expr
->deref
= deref
;
2023 add
= evaluate_offset(deref
, offset
);
2024 expr
->type
= EXPR_PREOP
;
2028 expr
->ctype
= member
;
2032 static int is_promoted(struct expression
*expr
)
2035 switch (expr
->type
) {
2038 case EXPR_CONDITIONAL
:
2062 static struct symbol
*evaluate_cast(struct expression
*);
2064 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2066 struct symbol
*sym
= expr
->cast_type
;
2068 sym
= evaluate_expression(expr
->cast_expression
);
2072 * Expressions of restricted types will possibly get
2073 * promoted - check that here
2075 if (is_restricted_type(sym
)) {
2076 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2078 } else if (is_fouled_type(sym
)) {
2082 examine_symbol_type(sym
);
2083 if (is_bitfield_type(sym
)) {
2084 expression_error(expr
, "trying to examine bitfield type");
2090 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2092 struct symbol
*type
;
2095 type
= evaluate_type_information(expr
);
2099 size
= type
->bit_size
;
2101 if (size
< 0 && is_void_type(type
)) {
2102 warning(expr
->pos
, "expression using sizeof(void)");
2103 size
= bits_in_char
;
2106 if (size
== 1 && is_bool_type(type
)) {
2108 warning(expr
->pos
, "expression using sizeof bool");
2109 size
= bits_in_char
;
2112 if (is_function(type
->ctype
.base_type
)) {
2113 warning(expr
->pos
, "expression using sizeof on a function");
2114 size
= bits_in_char
;
2117 if ((size
< 0) || (size
& (bits_in_char
- 1)))
2118 expression_error(expr
, "cannot size expression");
2120 expr
->type
= EXPR_VALUE
;
2121 expr
->value
= bits_to_bytes(size
);
2123 expr
->ctype
= size_t_ctype
;
2124 return size_t_ctype
;
2127 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2129 struct symbol
*type
;
2132 type
= evaluate_type_information(expr
);
2136 if (type
->type
== SYM_NODE
)
2137 type
= type
->ctype
.base_type
;
2140 switch (type
->type
) {
2144 type
= get_base_type(type
);
2148 expression_error(expr
, "expected pointer expression");
2151 size
= type
->bit_size
;
2152 if (size
& (bits_in_char
-1))
2154 expr
->type
= EXPR_VALUE
;
2155 expr
->value
= bits_to_bytes(size
);
2157 expr
->ctype
= size_t_ctype
;
2158 return size_t_ctype
;
2161 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2163 struct symbol
*type
;
2165 type
= evaluate_type_information(expr
);
2169 expr
->type
= EXPR_VALUE
;
2170 expr
->value
= type
->ctype
.alignment
;
2172 expr
->ctype
= size_t_ctype
;
2173 return size_t_ctype
;
2176 static int evaluate_arguments(struct symbol
*fn
, struct expression_list
*head
)
2178 struct expression
*expr
;
2179 struct symbol_list
*argument_types
= fn
->arguments
;
2180 struct symbol
*argtype
;
2183 PREPARE_PTR_LIST(argument_types
, argtype
);
2184 FOR_EACH_PTR (head
, expr
) {
2185 struct expression
**p
= THIS_ADDRESS(expr
);
2186 struct symbol
*ctype
, *target
;
2187 ctype
= evaluate_expression(expr
);
2194 struct symbol
*type
;
2195 int class = classify_type(ctype
, &type
);
2196 if (is_int(class)) {
2197 *p
= cast_to(expr
, integer_promotion(type
));
2198 } else if (class & TYPE_FLOAT
) {
2199 unsigned long mod
= type
->ctype
.modifiers
;
2200 if (!(mod
& (MOD_LONG_ALL
)))
2201 *p
= cast_to(expr
, &double_ctype
);
2202 } else if (class & TYPE_PTR
) {
2203 if (expr
->ctype
== &null_ctype
)
2204 *p
= cast_to(expr
, &ptr_ctype
);
2208 } else if (!target
->forced_arg
){
2209 static char where
[30];
2210 examine_symbol_type(target
);
2211 sprintf(where
, "argument %d", i
);
2212 compatible_argument_type(expr
, target
, p
, where
);
2216 NEXT_PTR_LIST(argtype
);
2217 } END_FOR_EACH_PTR(expr
);
2218 FINISH_PTR_LIST(argtype
);
2222 static void convert_index(struct expression
*e
)
2224 struct expression
*child
= e
->idx_expression
;
2225 unsigned from
= e
->idx_from
;
2226 unsigned to
= e
->idx_to
+ 1;
2228 e
->init_offset
= from
* bits_to_bytes(e
->ctype
->bit_size
);
2229 e
->init_nr
= to
- from
;
2230 e
->init_expr
= child
;
2233 static void convert_ident(struct expression
*e
)
2235 struct expression
*child
= e
->ident_expression
;
2236 int offset
= e
->offset
;
2239 e
->init_offset
= offset
;
2241 e
->init_expr
= child
;
2244 static void convert_designators(struct expression
*e
)
2247 if (e
->type
== EXPR_INDEX
)
2249 else if (e
->type
== EXPR_IDENTIFIER
)
2257 static void excess(struct expression
*e
, const char *s
)
2259 warning(e
->pos
, "excessive elements in %s initializer", s
);
2263 * implicit designator for the first element
2265 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2266 struct expression
**v
)
2268 struct expression
*e
= *v
, *new;
2270 if (ctype
->type
== SYM_NODE
)
2271 ctype
= ctype
->ctype
.base_type
;
2273 if (class & TYPE_PTR
) { /* array */
2274 if (!ctype
->bit_size
)
2276 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2277 new->idx_expression
= e
;
2278 new->ctype
= ctype
->ctype
.base_type
;
2280 struct symbol
*field
, *p
;
2281 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2282 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2288 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2289 new->ident_expression
= e
;
2290 new->field
= new->ctype
= field
;
2291 new->offset
= field
->offset
;
2298 * sanity-check explicit designators; return the innermost one or NULL
2299 * in case of error. Assign types.
2301 static struct expression
*check_designators(struct expression
*e
,
2302 struct symbol
*ctype
)
2304 struct expression
*last
= NULL
;
2307 if (ctype
->type
== SYM_NODE
)
2308 ctype
= ctype
->ctype
.base_type
;
2309 if (e
->type
== EXPR_INDEX
) {
2310 struct symbol
*type
;
2311 if (ctype
->type
!= SYM_ARRAY
) {
2312 err
= "array index in non-array";
2315 type
= ctype
->ctype
.base_type
;
2316 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2317 unsigned offset
= array_element_offset(type
->bit_size
, e
->idx_to
);
2318 if (offset
>= ctype
->bit_size
) {
2319 err
= "index out of bounds in";
2323 e
->ctype
= ctype
= type
;
2326 if (!e
->idx_expression
) {
2330 e
= e
->idx_expression
;
2331 } else if (e
->type
== EXPR_IDENTIFIER
) {
2333 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2334 err
= "field name not in struct or union";
2337 ctype
= find_identifier(e
->expr_ident
, ctype
->symbol_list
, &offset
);
2339 err
= "unknown field name in";
2343 e
->field
= e
->ctype
= ctype
;
2345 if (!e
->ident_expression
) {
2349 e
= e
->ident_expression
;
2350 } else if (e
->type
== EXPR_POS
) {
2351 err
= "internal front-end error: EXPR_POS in";
2356 expression_error(e
, "%s initializer", err
);
2361 * choose the next subobject to initialize.
2363 * Get designators for next element, switch old ones to EXPR_POS.
2364 * Return the resulting expression or NULL if we'd run out of subobjects.
2365 * The innermost designator is returned in *v. Designators in old
2366 * are assumed to be already sanity-checked.
2368 static struct expression
*next_designators(struct expression
*old
,
2369 struct symbol
*ctype
,
2370 struct expression
*e
, struct expression
**v
)
2372 struct expression
*new = NULL
;
2376 if (old
->type
== EXPR_INDEX
) {
2377 struct expression
*copy
;
2380 copy
= next_designators(old
->idx_expression
,
2383 n
= old
->idx_to
+ 1;
2384 if (array_element_offset(old
->ctype
->bit_size
, n
) == ctype
->bit_size
) {
2389 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2392 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2395 new->idx_from
= new->idx_to
= n
;
2396 new->idx_expression
= copy
;
2397 new->ctype
= old
->ctype
;
2399 } else if (old
->type
== EXPR_IDENTIFIER
) {
2400 struct expression
*copy
;
2401 struct symbol
*field
;
2404 copy
= next_designators(old
->ident_expression
,
2407 field
= old
->field
->next_subobject
;
2413 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2415 * We can't necessarily trust "field->offset",
2416 * because the field might be in an anonymous
2417 * union, and the field offset is then the offset
2418 * within that union.
2420 * The "old->offset - old->field->offset"
2421 * would be the offset of such an anonymous
2424 offset
= old
->offset
- old
->field
->offset
;
2427 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2431 new->expr_ident
= field
->ident
;
2432 new->ident_expression
= copy
;
2434 new->offset
= field
->offset
+ offset
;
2440 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2441 int class, struct symbol
*ctype
);
2444 * deal with traversing subobjects [6.7.8(17,18,20)]
2446 static void handle_list_initializer(struct expression
*expr
,
2447 int class, struct symbol
*ctype
)
2449 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2452 FOR_EACH_PTR(expr
->expr_list
, e
) {
2453 struct expression
**v
;
2454 struct symbol
*type
;
2457 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2458 struct symbol
*struct_sym
;
2461 last
= first_subobject(ctype
, class, &top
);
2463 last
= next_designators(last
, ctype
, e
, &top
);
2466 excess(e
, class & TYPE_PTR
? "array" :
2468 DELETE_CURRENT_PTR(e
);
2471 struct_sym
= ctype
->type
== SYM_NODE
? ctype
->ctype
.base_type
: ctype
;
2472 if (Wdesignated_init
&& struct_sym
->designated_init
)
2473 warning(e
->pos
, "%s%.*s%spositional init of field in %s %s, declared with attribute designated_init",
2474 ctype
->ident
? "in initializer for " : "",
2475 ctype
->ident
? ctype
->ident
->len
: 0,
2476 ctype
->ident
? ctype
->ident
->name
: "",
2477 ctype
->ident
? ": " : "",
2478 get_type_name(struct_sym
->type
),
2479 show_ident(struct_sym
->ident
));
2481 warning(e
->pos
, "advancing past deep designator");
2484 REPLACE_CURRENT_PTR(e
, last
);
2486 next
= check_designators(e
, ctype
);
2488 DELETE_CURRENT_PTR(e
);
2492 /* deeper than one designator? */
2494 convert_designators(last
);
2499 lclass
= classify_type(top
->ctype
, &type
);
2500 if (top
->type
== EXPR_INDEX
)
2501 v
= &top
->idx_expression
;
2503 v
= &top
->ident_expression
;
2505 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2508 if (!(lclass
& TYPE_COMPOUND
)) {
2509 warning(e
->pos
, "bogus scalar initializer");
2510 DELETE_CURRENT_PTR(e
);
2514 next
= first_subobject(type
, lclass
, v
);
2516 warning(e
->pos
, "missing braces around initializer");
2521 DELETE_CURRENT_PTR(e
);
2522 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2524 } END_FOR_EACH_PTR(e
);
2526 convert_designators(last
);
2527 expr
->ctype
= ctype
;
2530 static int is_string_literal(struct expression
**v
)
2532 struct expression
*e
= *v
;
2533 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2535 if (!e
|| e
->type
!= EXPR_STRING
)
2537 if (e
!= *v
&& Wparen_string
)
2539 "array initialized from parenthesized string constant");
2545 * We want a normal expression, possibly in one layer of braces. Warn
2546 * if the latter happens inside a list (it's legal, but likely to be
2547 * an effect of screwup). In case of anything not legal, we are definitely
2548 * having an effect of screwup, so just fail and let the caller warn.
2550 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2552 struct expression
*v
= NULL
, *p
;
2556 if (e
->type
!= EXPR_INITIALIZER
)
2559 FOR_EACH_PTR(e
->expr_list
, p
) {
2563 } END_FOR_EACH_PTR(p
);
2567 case EXPR_INITIALIZER
:
2569 case EXPR_IDENTIFIER
:
2575 warning(e
->pos
, "braces around scalar initializer");
2580 * deal with the cases that don't care about subobjects:
2581 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2582 * character array <- string literal, possibly in braces [6.7.8(14)]
2583 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2584 * compound type <- initializer list in braces [6.7.8(16)]
2585 * The last one punts to handle_list_initializer() which, in turn will call
2586 * us for individual elements of the list.
2588 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2589 * the lack of support of wide char stuff in general.
2591 * One note: we need to take care not to evaluate a string literal until
2592 * we know that we *will* handle it right here. Otherwise we would screw
2593 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2594 * { "string", ...} - we need to preserve that string literal recognizable
2595 * until we dig into the inner struct.
2597 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2598 int class, struct symbol
*ctype
)
2600 int is_string
= is_string_type(ctype
);
2601 struct expression
*e
= *ep
, *p
;
2602 struct symbol
*type
;
2608 if (!(class & TYPE_COMPOUND
)) {
2609 e
= handle_scalar(e
, nested
);
2613 if (!evaluate_expression(e
))
2615 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2620 * sublist; either a string, or we dig in; the latter will deal with
2621 * pathologies, so we don't need anything fancy here.
2623 if (e
->type
== EXPR_INITIALIZER
) {
2625 struct expression
*v
= NULL
;
2628 FOR_EACH_PTR(e
->expr_list
, p
) {
2632 } END_FOR_EACH_PTR(p
);
2633 if (count
== 1 && is_string_literal(&v
)) {
2638 handle_list_initializer(e
, class, ctype
);
2643 if (is_string_literal(&e
)) {
2644 /* either we are doing array of char, or we'll have to dig in */
2651 /* struct or union can be initialized by compatible */
2652 if (class != TYPE_COMPOUND
)
2654 type
= evaluate_expression(e
);
2657 if (ctype
->type
== SYM_NODE
)
2658 ctype
= ctype
->ctype
.base_type
;
2659 if (type
->type
== SYM_NODE
)
2660 type
= type
->ctype
.base_type
;
2666 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2668 type
= evaluate_expression(p
);
2669 if (ctype
->bit_size
!= -1) {
2670 if (ctype
->bit_size
+ bits_in_char
< type
->bit_size
)
2672 "too long initializer-string for array of char");
2673 else if (Winit_cstring
&& ctype
->bit_size
+ bits_in_char
== type
->bit_size
) {
2675 "too long initializer-string for array of char(no space for nul char)");
2682 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2684 struct symbol
*type
;
2685 int class = classify_type(ctype
, &type
);
2686 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2687 expression_error(*ep
, "invalid initializer");
2690 static struct symbol
*evaluate_cast(struct expression
*expr
)
2692 struct expression
*target
= expr
->cast_expression
;
2693 struct symbol
*ctype
;
2694 struct symbol
*t1
, *t2
;
2696 int as1
= 0, as2
= 0;
2702 * Special case: a cast can be followed by an
2703 * initializer, in which case we need to pass
2704 * the type value down to that initializer rather
2705 * than trying to evaluate it as an expression
2707 * A more complex case is when the initializer is
2708 * dereferenced as part of a post-fix expression.
2709 * We need to produce an expression that can be dereferenced.
2711 if (target
->type
== EXPR_INITIALIZER
) {
2712 struct symbol
*sym
= expr
->cast_type
;
2713 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2715 sym
->initializer
= target
;
2716 evaluate_symbol(sym
);
2718 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2721 expr
->type
= EXPR_PREOP
;
2729 ctype
= examine_symbol_type(expr
->cast_type
);
2730 expr
->ctype
= ctype
;
2731 expr
->cast_type
= ctype
;
2733 evaluate_expression(target
);
2736 class1
= classify_type(ctype
, &t1
);
2738 /* cast to non-integer type -> not an integer constant expression */
2739 if (!is_int(class1
))
2741 /* if argument turns out to be not an integer constant expression *and*
2742 it was not a floating literal to start with -> too bad */
2743 else if (expr
->flags
== Int_const_expr
&&
2744 !(target
->flags
& Int_const_expr
))
2747 * You can always throw a value away by casting to
2748 * "void" - that's an implicit "force". Note that
2749 * the same is _not_ true of "void *".
2751 if (t1
== &void_ctype
)
2754 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2755 warning(expr
->pos
, "cast to non-scalar");
2759 expression_error(expr
, "cast from unknown type");
2762 class2
= classify_type(t2
, &t2
);
2764 if (class2
& TYPE_COMPOUND
)
2765 warning(expr
->pos
, "cast from non-scalar");
2767 if (expr
->type
== EXPR_FORCE_CAST
)
2770 /* allowed cast unfouls */
2771 if (class2
& TYPE_FOULED
)
2775 if (class1
& TYPE_RESTRICT
)
2776 warning(expr
->pos
, "cast to %s",
2778 if (class2
& TYPE_RESTRICT
)
2779 warning(expr
->pos
, "cast from %s",
2783 if (t1
== &ulong_ctype
)
2785 else if (class1
== TYPE_PTR
) {
2786 examine_pointer_target(t1
);
2790 if (t2
== &ulong_ctype
)
2792 else if (class2
== TYPE_PTR
) {
2793 examine_pointer_target(t2
);
2797 if (!as1
&& as2
> 0)
2798 warning(expr
->pos
, "cast removes address space of expression");
2799 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2800 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2801 if (as1
> 0 && !as2
&&
2802 !is_null_pointer_constant(target
) && Wcast_to_as
)
2804 "cast adds address space to expression (<asn:%d>)", as1
);
2806 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2807 !as1
&& (target
->flags
& Int_const_expr
)) {
2808 if (t1
->ctype
.base_type
== &void_ctype
) {
2809 if (is_zero_constant(target
)) {
2811 expr
->type
= EXPR_VALUE
;
2812 expr
->ctype
= &null_ctype
;
2823 * Evaluate a call expression with a symbol. This
2824 * should expand inline functions, and evaluate
2827 static int evaluate_symbol_call(struct expression
*expr
)
2829 struct expression
*fn
= expr
->fn
;
2830 struct symbol
*ctype
= fn
->ctype
;
2832 if (fn
->type
!= EXPR_PREOP
)
2835 if (ctype
->op
&& ctype
->op
->evaluate
)
2836 return ctype
->op
->evaluate(expr
);
2838 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2840 struct symbol
*curr
= current_fn
;
2842 if (ctype
->definition
)
2843 ctype
= ctype
->definition
;
2845 current_fn
= ctype
->ctype
.base_type
;
2847 ret
= inline_function(expr
, ctype
);
2849 /* restore the old function */
2857 static struct symbol
*evaluate_call(struct expression
*expr
)
2860 struct symbol
*ctype
, *sym
;
2861 struct expression
*fn
= expr
->fn
;
2862 struct expression_list
*arglist
= expr
->args
;
2864 if (!evaluate_expression(fn
))
2866 sym
= ctype
= fn
->ctype
;
2867 if (ctype
->type
== SYM_NODE
)
2868 ctype
= ctype
->ctype
.base_type
;
2869 if (ctype
->type
== SYM_PTR
)
2870 ctype
= get_base_type(ctype
);
2872 if (ctype
->type
!= SYM_FN
) {
2873 struct expression
*arg
;
2874 expression_error(expr
, "not a function %s",
2875 show_ident(sym
->ident
));
2876 /* do typechecking in arguments */
2877 FOR_EACH_PTR (arglist
, arg
) {
2878 evaluate_expression(arg
);
2879 } END_FOR_EACH_PTR(arg
);
2883 examine_fn_arguments(ctype
);
2884 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2885 sym
->op
&& sym
->op
->args
) {
2886 if (!sym
->op
->args(expr
))
2889 if (!evaluate_arguments(ctype
, arglist
))
2891 args
= expression_list_size(expr
->args
);
2892 fnargs
= symbol_list_size(ctype
->arguments
);
2894 expression_error(expr
,
2895 "not enough arguments for function %s",
2896 show_ident(sym
->ident
));
2897 if (args
> fnargs
&& !ctype
->variadic
)
2898 expression_error(expr
,
2899 "too many arguments for function %s",
2900 show_ident(sym
->ident
));
2902 if (sym
->type
== SYM_NODE
) {
2903 if (evaluate_symbol_call(expr
))
2906 expr
->ctype
= ctype
->ctype
.base_type
;
2910 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2912 struct expression
*e
= expr
->down
;
2913 struct symbol
*ctype
= expr
->in
;
2916 if (expr
->op
== '.') {
2917 struct symbol
*field
;
2920 expression_error(expr
, "expected structure or union");
2923 examine_symbol_type(ctype
);
2924 class = classify_type(ctype
, &ctype
);
2925 if (class != TYPE_COMPOUND
) {
2926 expression_error(expr
, "expected structure or union");
2930 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2932 expression_error(expr
, "unknown member");
2936 expr
->type
= EXPR_VALUE
;
2937 expr
->flags
= Int_const_expr
;
2938 expr
->value
= offset
;
2940 expr
->ctype
= size_t_ctype
;
2943 expression_error(expr
, "expected structure or union");
2946 examine_symbol_type(ctype
);
2947 class = classify_type(ctype
, &ctype
);
2948 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2949 expression_error(expr
, "expected array");
2952 ctype
= ctype
->ctype
.base_type
;
2954 expr
->type
= EXPR_VALUE
;
2955 expr
->flags
= Int_const_expr
;
2958 expr
->ctype
= size_t_ctype
;
2960 struct expression
*idx
= expr
->index
, *m
;
2961 struct symbol
*i_type
= evaluate_expression(idx
);
2962 int i_class
= classify_type(i_type
, &i_type
);
2963 if (!is_int(i_class
)) {
2964 expression_error(expr
, "non-integer index");
2967 unrestrict(idx
, i_class
, &i_type
);
2968 idx
= cast_to(idx
, size_t_ctype
);
2969 m
= alloc_const_expression(expr
->pos
,
2970 bits_to_bytes(ctype
->bit_size
));
2971 m
->ctype
= size_t_ctype
;
2972 m
->flags
= Int_const_expr
;
2973 expr
->type
= EXPR_BINOP
;
2977 expr
->ctype
= size_t_ctype
;
2978 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2982 struct expression
*copy
= __alloc_expression(0);
2984 if (e
->type
== EXPR_OFFSETOF
)
2986 if (!evaluate_expression(e
))
2988 expr
->type
= EXPR_BINOP
;
2989 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2991 expr
->ctype
= size_t_ctype
;
2995 return size_t_ctype
;
2998 struct symbol
*evaluate_expression(struct expression
*expr
)
3005 switch (expr
->type
) {
3008 expression_error(expr
, "value expression without a type");
3011 return evaluate_string(expr
);
3013 return evaluate_symbol_expression(expr
);
3015 if (!evaluate_expression(expr
->left
))
3017 if (!evaluate_expression(expr
->right
))
3019 return evaluate_binop(expr
);
3021 return evaluate_logical(expr
);
3023 evaluate_expression(expr
->left
);
3024 if (!evaluate_expression(expr
->right
))
3026 return evaluate_comma(expr
);
3028 if (!evaluate_expression(expr
->left
))
3030 if (!evaluate_expression(expr
->right
))
3032 return evaluate_compare(expr
);
3033 case EXPR_ASSIGNMENT
:
3034 if (!evaluate_expression(expr
->left
))
3036 if (!evaluate_expression(expr
->right
))
3038 return evaluate_assignment(expr
);
3040 if (!evaluate_expression(expr
->unop
))
3042 return evaluate_preop(expr
);
3044 if (!evaluate_expression(expr
->unop
))
3046 return evaluate_postop(expr
);
3048 case EXPR_FORCE_CAST
:
3049 case EXPR_IMPLIED_CAST
:
3050 return evaluate_cast(expr
);
3052 return evaluate_sizeof(expr
);
3053 case EXPR_PTRSIZEOF
:
3054 return evaluate_ptrsizeof(expr
);
3056 return evaluate_alignof(expr
);
3058 return evaluate_member_dereference(expr
);
3060 return evaluate_call(expr
);
3062 case EXPR_CONDITIONAL
:
3063 return evaluate_conditional_expression(expr
);
3064 case EXPR_STATEMENT
:
3065 expr
->ctype
= evaluate_statement(expr
->statement
);
3069 expr
->ctype
= &ptr_ctype
;
3073 /* Evaluate the type of the symbol .. */
3074 evaluate_symbol(expr
->symbol
);
3075 /* .. but the type of the _expression_ is a "type" */
3076 expr
->ctype
= &type_ctype
;
3080 return evaluate_offsetof(expr
);
3082 /* These can not exist as stand-alone expressions */
3083 case EXPR_INITIALIZER
:
3084 case EXPR_IDENTIFIER
:
3087 expression_error(expr
, "internal front-end error: initializer in expression");
3090 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
3096 static void check_duplicates(struct symbol
*sym
)
3099 struct symbol
*next
= sym
;
3100 int initialized
= sym
->initializer
!= NULL
;
3102 while ((next
= next
->same_symbol
) != NULL
) {
3103 const char *typediff
;
3104 evaluate_symbol(next
);
3105 if (initialized
&& next
->initializer
) {
3106 sparse_error(sym
->pos
, "symbol '%s' has multiple initializers (originally initialized at %s:%d)",
3107 show_ident(sym
->ident
),
3108 stream_name(next
->pos
.stream
), next
->pos
.line
);
3109 /* Only warn once */
3113 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3115 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3116 show_ident(sym
->ident
),
3117 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3122 unsigned long mod
= sym
->ctype
.modifiers
;
3123 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3125 if (!(mod
& MOD_TOPLEVEL
))
3129 if (sym
->ident
== &main_ident
)
3131 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3135 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3137 struct symbol
*base_type
;
3145 sym
= examine_symbol_type(sym
);
3146 base_type
= get_base_type(sym
);
3150 /* Evaluate the initializers */
3151 if (sym
->initializer
)
3152 evaluate_initializer(sym
, &sym
->initializer
);
3154 /* And finally, evaluate the body of the symbol too */
3155 if (base_type
->type
== SYM_FN
) {
3156 struct symbol
*curr
= current_fn
;
3158 if (sym
->definition
&& sym
->definition
!= sym
)
3159 return evaluate_symbol(sym
->definition
);
3161 current_fn
= base_type
;
3163 examine_fn_arguments(base_type
);
3164 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3166 if (base_type
->stmt
)
3167 evaluate_statement(base_type
->stmt
);
3175 void evaluate_symbol_list(struct symbol_list
*list
)
3179 FOR_EACH_PTR(list
, sym
) {
3180 evaluate_symbol(sym
);
3181 check_duplicates(sym
);
3182 } END_FOR_EACH_PTR(sym
);
3185 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3187 struct expression
*expr
= stmt
->expression
;
3188 struct symbol
*fntype
;
3190 evaluate_expression(expr
);
3191 fntype
= current_fn
->ctype
.base_type
;
3192 if (!fntype
|| fntype
== &void_ctype
) {
3193 if (expr
&& expr
->ctype
!= &void_ctype
)
3194 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3195 if (expr
&& Wreturn_void
)
3196 warning(stmt
->pos
, "returning void-valued expression");
3201 sparse_error(stmt
->pos
, "return with no return value");
3206 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3210 static void evaluate_if_statement(struct statement
*stmt
)
3212 if (!stmt
->if_conditional
)
3215 evaluate_conditional(stmt
->if_conditional
, 0);
3216 evaluate_statement(stmt
->if_true
);
3217 evaluate_statement(stmt
->if_false
);
3220 static void evaluate_iterator(struct statement
*stmt
)
3222 evaluate_symbol_list(stmt
->iterator_syms
);
3223 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3224 evaluate_conditional(stmt
->iterator_post_condition
,1);
3225 evaluate_statement(stmt
->iterator_pre_statement
);
3226 evaluate_statement(stmt
->iterator_statement
);
3227 evaluate_statement(stmt
->iterator_post_statement
);
3230 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3232 switch (*constraint
) {
3233 case '=': /* Assignment */
3234 case '+': /* Update */
3237 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3241 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3243 switch (*constraint
) {
3244 case '=': /* Assignment */
3245 case '+': /* Update */
3246 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3250 static void evaluate_asm_statement(struct statement
*stmt
)
3252 struct expression
*expr
;
3256 expr
= stmt
->asm_string
;
3257 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3258 sparse_error(stmt
->pos
, "need constant string for inline asm");
3263 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3265 case 0: /* Identifier */
3269 case 1: /* Constraint */
3271 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3272 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3273 *THIS_ADDRESS(expr
) = NULL
;
3276 verify_output_constraint(expr
, expr
->string
->data
);
3279 case 2: /* Expression */
3281 if (!evaluate_expression(expr
))
3283 if (!lvalue_expression(expr
))
3284 warning(expr
->pos
, "asm output is not an lvalue");
3285 evaluate_assign_to(expr
, expr
->ctype
);
3288 } END_FOR_EACH_PTR(expr
);
3291 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3293 case 0: /* Identifier */
3297 case 1: /* Constraint */
3299 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3300 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3301 *THIS_ADDRESS(expr
) = NULL
;
3304 verify_input_constraint(expr
, expr
->string
->data
);
3307 case 2: /* Expression */
3309 if (!evaluate_expression(expr
))
3313 } END_FOR_EACH_PTR(expr
);
3315 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3317 sparse_error(stmt
->pos
, "bad asm clobbers");
3320 if (expr
->type
== EXPR_STRING
)
3322 expression_error(expr
, "asm clobber is not a string");
3323 } END_FOR_EACH_PTR(expr
);
3325 FOR_EACH_PTR(stmt
->asm_labels
, sym
) {
3326 if (!sym
|| sym
->type
!= SYM_LABEL
) {
3327 sparse_error(stmt
->pos
, "bad asm label");
3330 } END_FOR_EACH_PTR(sym
);
3333 static void evaluate_case_statement(struct statement
*stmt
)
3335 evaluate_expression(stmt
->case_expression
);
3336 evaluate_expression(stmt
->case_to
);
3337 evaluate_statement(stmt
->case_statement
);
3340 static void check_case_type(struct expression
*switch_expr
,
3341 struct expression
*case_expr
,
3342 struct expression
**enumcase
)
3344 struct symbol
*switch_type
, *case_type
;
3350 switch_type
= switch_expr
->ctype
;
3351 case_type
= evaluate_expression(case_expr
);
3353 if (!switch_type
|| !case_type
)
3357 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3358 else if (is_enum_type(case_type
))
3359 *enumcase
= case_expr
;
3362 sclass
= classify_type(switch_type
, &switch_type
);
3363 cclass
= classify_type(case_type
, &case_type
);
3365 /* both should be arithmetic */
3366 if (!(sclass
& cclass
& TYPE_NUM
))
3369 /* neither should be floating */
3370 if ((sclass
| cclass
) & TYPE_FLOAT
)
3373 /* if neither is restricted, we are OK */
3374 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3377 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3378 cclass
, sclass
, case_type
, switch_type
)) {
3379 unrestrict(case_expr
, cclass
, &case_type
);
3380 unrestrict(switch_expr
, sclass
, &switch_type
);
3385 expression_error(case_expr
, "incompatible types for 'case' statement");
3388 static void evaluate_switch_statement(struct statement
*stmt
)
3391 struct expression
*enumcase
= NULL
;
3392 struct expression
**enumcase_holder
= &enumcase
;
3393 struct expression
*sel
= stmt
->switch_expression
;
3395 evaluate_expression(sel
);
3396 evaluate_statement(stmt
->switch_statement
);
3399 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3400 enumcase_holder
= NULL
; /* Only check cases against switch */
3402 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3403 struct statement
*case_stmt
= sym
->stmt
;
3404 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3405 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3406 } END_FOR_EACH_PTR(sym
);
3409 static void evaluate_goto_statement(struct statement
*stmt
)
3411 struct symbol
*label
= stmt
->goto_label
;
3413 if (label
&& !label
->stmt
&& !lookup_keyword(label
->ident
, NS_KEYWORD
))
3414 sparse_error(stmt
->pos
, "label '%s' was not declared", show_ident(label
->ident
));
3416 evaluate_expression(stmt
->goto_expression
);
3419 struct symbol
*evaluate_statement(struct statement
*stmt
)
3424 switch (stmt
->type
) {
3425 case STMT_DECLARATION
: {
3427 FOR_EACH_PTR(stmt
->declaration
, s
) {
3429 } END_FOR_EACH_PTR(s
);
3434 return evaluate_return_expression(stmt
);
3436 case STMT_EXPRESSION
:
3437 if (!evaluate_expression(stmt
->expression
))
3439 if (stmt
->expression
->ctype
== &null_ctype
)
3440 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3441 return degenerate(stmt
->expression
);
3443 case STMT_COMPOUND
: {
3444 struct statement
*s
;
3445 struct symbol
*type
= NULL
;
3447 /* Evaluate the return symbol in the compound statement */
3448 evaluate_symbol(stmt
->ret
);
3451 * Then, evaluate each statement, making the type of the
3452 * compound statement be the type of the last statement
3454 type
= evaluate_statement(stmt
->args
);
3455 FOR_EACH_PTR(stmt
->stmts
, s
) {
3456 type
= evaluate_statement(s
);
3457 } END_FOR_EACH_PTR(s
);
3463 evaluate_if_statement(stmt
);
3466 evaluate_iterator(stmt
);
3469 evaluate_switch_statement(stmt
);
3472 evaluate_case_statement(stmt
);
3475 return evaluate_statement(stmt
->label_statement
);
3477 evaluate_goto_statement(stmt
);
3482 evaluate_asm_statement(stmt
);
3485 evaluate_expression(stmt
->expression
);
3488 evaluate_expression(stmt
->range_expression
);
3489 evaluate_expression(stmt
->range_low
);
3490 evaluate_expression(stmt
->range_high
);