4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
27 #include "expression.h"
29 struct symbol
*current_fn
;
31 static struct symbol
*degenerate(struct expression
*expr
);
32 static struct symbol
*evaluate_symbol(struct symbol
*sym
);
34 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
36 struct expression
*addr
;
37 struct symbol
*sym
= expr
->symbol
;
38 struct symbol
*base_type
;
41 expression_error(expr
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
45 examine_symbol_type(sym
);
47 base_type
= get_base_type(sym
);
49 expression_error(expr
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
53 addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
55 addr
->symbol_name
= expr
->symbol_name
;
56 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
57 expr
->type
= EXPR_PREOP
;
61 /* The type of a symbol is the symbol itself! */
66 static struct symbol
*evaluate_string(struct expression
*expr
)
68 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
69 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
70 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
71 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
72 unsigned int length
= expr
->string
->length
;
74 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
75 sym
->bit_size
= bits_in_char
* length
;
76 sym
->ctype
.alignment
= 1;
78 sym
->ctype
.modifiers
= MOD_STATIC
;
79 sym
->ctype
.base_type
= array
;
80 sym
->initializer
= initstr
;
83 initstr
->string
= expr
->string
;
85 array
->array_size
= sym
->array_size
;
86 array
->bit_size
= bits_in_char
* length
;
87 array
->ctype
.alignment
= 1;
88 array
->ctype
.modifiers
= MOD_STATIC
;
89 array
->ctype
.base_type
= &char_ctype
;
92 addr
->ctype
= &lazy_ptr_ctype
;
94 expr
->type
= EXPR_PREOP
;
101 /* type has come from classify_type and is an integer type */
102 static inline struct symbol
*integer_promotion(struct symbol
*type
)
104 struct symbol
*orig_type
= type
;
105 unsigned long mod
= type
->ctype
.modifiers
;
106 int width
= type
->bit_size
;
109 * Bitfields always promote to the base type,
110 * even if the bitfield might be bigger than
113 if (type
->type
== SYM_BITFIELD
) {
114 type
= type
->ctype
.base_type
;
117 mod
= type
->ctype
.modifiers
;
118 if (width
< bits_in_int
)
121 /* If char/short has as many bits as int, it still gets "promoted" */
122 if (mod
& (MOD_CHAR
| MOD_SHORT
)) {
123 if (mod
& MOD_UNSIGNED
)
131 * integer part of usual arithmetic conversions:
132 * integer promotions are applied
133 * if left and right are identical, we are done
134 * if signedness is the same, convert one with lower rank
135 * unless unsigned argument has rank lower than signed one, convert the
137 * if signed argument is bigger than unsigned one, convert the unsigned.
138 * otherwise, convert signed.
140 * Leaving aside the integer promotions, that is equivalent to
141 * if identical, don't convert
142 * if left is bigger than right, convert right
143 * if right is bigger than left, convert right
144 * otherwise, if signedness is the same, convert one with lower rank
145 * otherwise convert the signed one.
147 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
149 unsigned long lmod
, rmod
;
151 left
= integer_promotion(left
);
152 right
= integer_promotion(right
);
157 if (left
->bit_size
> right
->bit_size
)
160 if (right
->bit_size
> left
->bit_size
)
163 lmod
= left
->ctype
.modifiers
;
164 rmod
= right
->ctype
.modifiers
;
165 if ((lmod
^ rmod
) & MOD_UNSIGNED
) {
166 if (lmod
& MOD_UNSIGNED
)
168 } else if ((lmod
& ~rmod
) & (MOD_LONG
| MOD_LONGLONG
))
176 static int same_cast_type(struct symbol
*orig
, struct symbol
*new)
178 return orig
->bit_size
== new->bit_size
&& orig
->bit_offset
== new->bit_offset
;
181 static struct symbol
*base_type(struct symbol
*node
, unsigned long *modp
, unsigned long *asp
)
183 unsigned long mod
, as
;
187 mod
|= node
->ctype
.modifiers
;
188 as
|= node
->ctype
.as
;
189 if (node
->type
== SYM_NODE
) {
190 node
= node
->ctype
.base_type
;
195 *modp
= mod
& ~MOD_IGNORE
;
200 static int is_same_type(struct expression
*expr
, struct symbol
*new)
202 struct symbol
*old
= expr
->ctype
;
203 unsigned long oldmod
, newmod
, oldas
, newas
;
205 old
= base_type(old
, &oldmod
, &oldas
);
206 new = base_type(new, &newmod
, &newas
);
208 /* Same base type, same address space? */
209 if (old
== new && oldas
== newas
) {
210 unsigned long difmod
;
212 /* Check the modifier bits. */
213 difmod
= (oldmod
^ newmod
) & ~MOD_NOCAST
;
215 /* Exact same type? */
220 * Not the same type, but differs only in "const".
221 * Don't warn about MOD_NOCAST.
223 if (difmod
== MOD_CONST
)
226 if ((oldmod
| newmod
) & MOD_NOCAST
) {
227 const char *tofrom
= "to/from";
228 if (!(newmod
& MOD_NOCAST
))
230 if (!(oldmod
& MOD_NOCAST
))
232 warning(expr
->pos
, "implicit cast %s nocast type", tofrom
);
238 warn_for_different_enum_types (struct position pos
,
239 struct symbol
*typea
,
240 struct symbol
*typeb
)
244 if (typea
->type
== SYM_NODE
)
245 typea
= typea
->ctype
.base_type
;
246 if (typeb
->type
== SYM_NODE
)
247 typeb
= typeb
->ctype
.base_type
;
252 if (typea
->type
== SYM_ENUM
&& typeb
->type
== SYM_ENUM
) {
253 warning(pos
, "mixing different enum types");
254 info(pos
, " %s versus", show_typename(typea
));
255 info(pos
, " %s", show_typename(typeb
));
260 * This gets called for implicit casts in assignments and
261 * integer promotion. We often want to try to move the
262 * cast down, because the ops involved may have been
263 * implicitly cast up, and we can get rid of the casts
266 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
268 struct expression
*expr
;
270 warn_for_different_enum_types (old
->pos
, old
->ctype
, type
);
272 if (old
->ctype
!= &null_ctype
&& is_same_type(old
, type
))
276 * See if we can simplify the op. Move the cast down.
280 if (old
->ctype
->bit_size
< type
->bit_size
)
282 if (old
->op
== '~') {
284 old
->unop
= cast_to(old
->unop
, type
);
289 case EXPR_IMPLIED_CAST
:
290 warn_for_different_enum_types(old
->pos
, old
->ctype
, type
);
292 if (old
->ctype
->bit_size
>= type
->bit_size
) {
293 struct expression
*orig
= old
->cast_expression
;
294 if (same_cast_type(orig
->ctype
, type
))
296 if (old
->ctype
->bit_offset
== type
->bit_offset
) {
298 old
->cast_type
= type
;
308 expr
= alloc_expression(old
->pos
, EXPR_IMPLIED_CAST
);
309 expr
->flags
= old
->flags
;
311 expr
->cast_type
= type
;
312 expr
->cast_expression
= old
;
316 static int is_type_type(struct symbol
*type
)
318 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
321 int is_ptr_type(struct symbol
*type
)
323 if (type
->type
== SYM_NODE
)
324 type
= type
->ctype
.base_type
;
325 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
328 static inline int is_float_type(struct symbol
*type
)
330 if (type
->type
== SYM_NODE
)
331 type
= type
->ctype
.base_type
;
332 return type
->ctype
.base_type
== &fp_type
;
335 static inline int is_byte_type(struct symbol
*type
)
337 return type
->bit_size
== bits_in_char
&& type
->type
!= SYM_BITFIELD
;
351 static inline int classify_type(struct symbol
*type
, struct symbol
**base
)
353 static int type_class
[SYM_BAD
+ 1] = {
354 [SYM_PTR
] = TYPE_PTR
,
355 [SYM_FN
] = TYPE_PTR
| TYPE_FN
,
356 [SYM_ARRAY
] = TYPE_PTR
| TYPE_COMPOUND
,
357 [SYM_STRUCT
] = TYPE_COMPOUND
,
358 [SYM_UNION
] = TYPE_COMPOUND
,
359 [SYM_BITFIELD
] = TYPE_NUM
| TYPE_BITFIELD
,
360 [SYM_RESTRICT
] = TYPE_NUM
| TYPE_RESTRICT
,
361 [SYM_FOULED
] = TYPE_NUM
| TYPE_RESTRICT
| TYPE_FOULED
,
363 if (type
->type
== SYM_NODE
)
364 type
= type
->ctype
.base_type
;
365 if (type
->type
== SYM_ENUM
)
366 type
= type
->ctype
.base_type
;
368 if (type
->type
== SYM_BASETYPE
) {
369 if (type
->ctype
.base_type
== &int_type
)
371 if (type
->ctype
.base_type
== &fp_type
)
372 return TYPE_NUM
| TYPE_FLOAT
;
374 return type_class
[type
->type
];
377 #define is_int(class) ((class & (TYPE_NUM | TYPE_FLOAT)) == TYPE_NUM)
379 static inline int is_string_type(struct symbol
*type
)
381 if (type
->type
== SYM_NODE
)
382 type
= type
->ctype
.base_type
;
383 return type
->type
== SYM_ARRAY
&& is_byte_type(type
->ctype
.base_type
);
386 static struct symbol
*bad_expr_type(struct expression
*expr
)
388 sparse_error(expr
->pos
, "incompatible types for operation (%s)", show_special(expr
->op
));
389 switch (expr
->type
) {
392 info(expr
->pos
, " left side has type %s", show_typename(expr
->left
->ctype
));
393 info(expr
->pos
, " right side has type %s", show_typename(expr
->right
->ctype
));
397 info(expr
->pos
, " argument has type %s", show_typename(expr
->unop
->ctype
));
404 return expr
->ctype
= &bad_ctype
;
407 static int restricted_value(struct expression
*v
, struct symbol
*type
)
409 if (v
->type
!= EXPR_VALUE
)
416 static int restricted_binop(int op
, struct symbol
*type
)
421 case SPECIAL_AND_ASSIGN
:
422 case SPECIAL_OR_ASSIGN
:
423 case SPECIAL_XOR_ASSIGN
:
424 return 1; /* unfoul */
428 return 2; /* keep fouled */
430 case SPECIAL_NOTEQUAL
:
431 return 3; /* warn if fouled */
437 static int restricted_unop(int op
, struct symbol
**type
)
440 if ((*type
)->bit_size
< bits_in_int
)
441 *type
= befoul(*type
);
448 /* type should be SYM_FOULED */
449 static inline struct symbol
*unfoul(struct symbol
*type
)
451 return type
->ctype
.base_type
;
454 static struct symbol
*restricted_binop_type(int op
,
455 struct expression
*left
,
456 struct expression
*right
,
457 int lclass
, int rclass
,
458 struct symbol
*ltype
,
459 struct symbol
*rtype
)
461 struct symbol
*ctype
= NULL
;
462 if (lclass
& TYPE_RESTRICT
) {
463 if (rclass
& TYPE_RESTRICT
) {
464 if (ltype
== rtype
) {
466 } else if (lclass
& TYPE_FOULED
) {
467 if (unfoul(ltype
) == rtype
)
469 } else if (rclass
& TYPE_FOULED
) {
470 if (unfoul(rtype
) == ltype
)
474 if (!restricted_value(right
, ltype
))
477 } else if (!restricted_value(left
, rtype
))
481 switch (restricted_binop(op
, ctype
)) {
483 if ((lclass
^ rclass
) & TYPE_FOULED
)
484 ctype
= unfoul(ctype
);
487 if (!(lclass
& rclass
& TYPE_FOULED
))
499 static inline void unrestrict(struct expression
*expr
,
500 int class, struct symbol
**ctype
)
502 if (class & TYPE_RESTRICT
) {
503 if (class & TYPE_FOULED
)
504 *ctype
= unfoul(*ctype
);
505 warning(expr
->pos
, "%s degrades to integer",
506 show_typename(*ctype
));
507 *ctype
= (*ctype
)->ctype
.base_type
; /* get to arithmetic type */
511 static struct symbol
*usual_conversions(int op
,
512 struct expression
*left
,
513 struct expression
*right
,
514 int lclass
, int rclass
,
515 struct symbol
*ltype
,
516 struct symbol
*rtype
)
518 struct symbol
*ctype
;
520 warn_for_different_enum_types(right
->pos
, left
->ctype
, right
->ctype
);
522 if ((lclass
| rclass
) & TYPE_RESTRICT
)
526 if (!(lclass
& TYPE_FLOAT
)) {
527 if (!(rclass
& TYPE_FLOAT
))
528 return bigger_int_type(ltype
, rtype
);
531 } else if (rclass
& TYPE_FLOAT
) {
532 unsigned long lmod
= ltype
->ctype
.modifiers
;
533 unsigned long rmod
= rtype
->ctype
.modifiers
;
534 if (rmod
& ~lmod
& (MOD_LONG
| MOD_LONGLONG
))
542 ctype
= restricted_binop_type(op
, left
, right
,
543 lclass
, rclass
, ltype
, rtype
);
547 unrestrict(left
, lclass
, <ype
);
548 unrestrict(right
, rclass
, &rtype
);
553 static inline int lvalue_expression(struct expression
*expr
)
555 return expr
->type
== EXPR_PREOP
&& expr
->op
== '*';
558 static inline int is_function(struct symbol
*type
)
560 return type
&& type
->type
== SYM_FN
;
563 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct symbol
*itype
)
565 struct expression
*index
= expr
->right
;
566 struct symbol
*ctype
, *base
;
569 classify_type(degenerate(expr
->left
), &ctype
);
570 base
= examine_pointer_target(ctype
);
573 expression_error(expr
, "missing type information");
576 if (is_function(base
)) {
577 expression_error(expr
, "arithmetics on pointers to functions");
581 /* Get the size of whatever the pointer points to */
582 multiply
= base
->bit_size
>> 3;
584 if (ctype
== &null_ctype
)
588 if (multiply
== 1 && itype
->bit_size
>= bits_in_pointer
)
591 if (index
->type
== EXPR_VALUE
) {
592 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
593 unsigned long long v
= index
->value
, mask
;
594 mask
= 1ULL << (itype
->bit_size
- 1);
600 mask
= 1ULL << (bits_in_pointer
- 1);
601 v
&= mask
| (mask
- 1);
603 val
->ctype
= ssize_t_ctype
;
608 if (itype
->bit_size
< bits_in_pointer
)
609 index
= cast_to(index
, ssize_t_ctype
);
612 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
613 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
615 val
->ctype
= ssize_t_ctype
;
616 val
->value
= multiply
;
619 mul
->ctype
= ssize_t_ctype
;
629 static void examine_fn_arguments(struct symbol
*fn
);
631 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
633 const char *type_difference(struct ctype
*c1
, struct ctype
*c2
,
634 unsigned long mod1
, unsigned long mod2
)
636 unsigned long as1
= c1
->as
, as2
= c2
->as
;
637 struct symbol
*t1
= c1
->base_type
;
638 struct symbol
*t2
= c2
->base_type
;
639 int move1
= 1, move2
= 1;
640 mod1
|= c1
->modifiers
;
641 mod2
|= c2
->modifiers
;
645 struct symbol
*base1
= t1
->ctype
.base_type
;
646 struct symbol
*base2
= t2
->ctype
.base_type
;
649 * FIXME! Collect alignment and context too here!
652 if (t1
&& t1
->type
!= SYM_PTR
) {
653 mod1
|= t1
->ctype
.modifiers
;
660 if (t2
&& t2
->type
!= SYM_PTR
) {
661 mod2
|= t2
->ctype
.modifiers
;
670 return "different types";
672 if (t1
->type
== SYM_NODE
|| t1
->type
== SYM_ENUM
) {
680 if (t2
->type
== SYM_NODE
|| t2
->type
== SYM_ENUM
) {
690 if (type
!= t2
->type
)
691 return "different base types";
695 sparse_error(t1
->pos
,
696 "internal error: bad type in derived(%d)",
700 return "different base types";
703 /* allow definition of incomplete structs and unions */
704 if (t1
->ident
== t2
->ident
)
706 return "different base types";
708 /* XXX: we ought to compare sizes */
711 if (Waddress_space
&& as1
!= as2
)
712 return "different address spaces";
713 /* MOD_SPECIFIER is due to idiocy in parse.c */
714 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SPECIFIER
)
715 return "different modifiers";
716 /* we could be lazier here */
717 base1
= examine_pointer_target(t1
);
718 base2
= examine_pointer_target(t2
);
719 mod1
= t1
->ctype
.modifiers
;
721 mod2
= t2
->ctype
.modifiers
;
725 struct symbol
*arg1
, *arg2
;
728 if (Waddress_space
&& as1
!= as2
)
729 return "different address spaces";
730 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
731 return "different modifiers";
732 mod1
= t1
->ctype
.modifiers
;
734 mod2
= t2
->ctype
.modifiers
;
737 if (base1
->variadic
!= base2
->variadic
)
738 return "incompatible variadic arguments";
739 examine_fn_arguments(t1
);
740 examine_fn_arguments(t2
);
741 PREPARE_PTR_LIST(t1
->arguments
, arg1
);
742 PREPARE_PTR_LIST(t2
->arguments
, arg2
);
749 return "different argument counts";
750 diffstr
= type_difference(&arg1
->ctype
,
754 static char argdiff
[80];
755 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diffstr
);
762 FINISH_PTR_LIST(arg2
);
763 FINISH_PTR_LIST(arg1
);
767 if (Waddress_space
&& as1
!= as2
)
768 return "different address spaces";
770 return "different base types";
771 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
775 return "different type sizes";
776 else if (diff
& ~MOD_SIGNEDNESS
)
777 return "different modifiers";
779 return "different signedness";
784 if (Waddress_space
&& as1
!= as2
)
785 return "different address spaces";
786 if ((mod1
^ mod2
) & ~MOD_IGNORE
& ~MOD_SIGNEDNESS
)
787 return "different modifiers";
791 static void bad_null(struct expression
*expr
)
793 if (Wnon_pointer_null
)
794 warning(expr
->pos
, "Using plain integer as NULL pointer");
797 static unsigned long target_qualifiers(struct symbol
*type
)
799 unsigned long mod
= type
->ctype
.modifiers
& MOD_IGN
;
800 if (type
->ctype
.base_type
&& type
->ctype
.base_type
->type
== SYM_ARRAY
)
805 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
)
807 const char *typediff
;
808 struct symbol
*ltype
, *rtype
;
809 struct expression
*l
= expr
->left
;
810 struct expression
*r
= expr
->right
;
811 struct symbol
*lbase
, *rbase
;
813 classify_type(degenerate(l
), <ype
);
814 classify_type(degenerate(r
), &rtype
);
816 lbase
= examine_pointer_target(ltype
);
817 rbase
= examine_pointer_target(rtype
);
818 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
819 target_qualifiers(rtype
),
820 target_qualifiers(ltype
));
822 expression_error(expr
, "subtraction of different types can't work (%s)", typediff
);
824 if (is_function(lbase
)) {
825 expression_error(expr
, "subtraction of functions? Share your drugs");
829 expr
->ctype
= ssize_t_ctype
;
830 if (lbase
->bit_size
> bits_in_char
) {
831 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
832 struct expression
*div
= expr
;
833 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
834 unsigned long value
= lbase
->bit_size
>> 3;
836 val
->ctype
= size_t_ctype
;
839 if (value
& (value
-1)) {
840 if (Wptr_subtraction_blows
)
841 warning(expr
->pos
, "potentially expensive pointer subtraction");
845 sub
->ctype
= ssize_t_ctype
;
854 return ssize_t_ctype
;
857 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
859 static struct symbol
*evaluate_conditional(struct expression
*expr
, int iterator
)
861 struct symbol
*ctype
;
866 if (!iterator
&& expr
->type
== EXPR_ASSIGNMENT
&& expr
->op
== '=')
867 warning(expr
->pos
, "assignment expression in conditional");
869 ctype
= evaluate_expression(expr
);
871 if (is_safe_type(ctype
))
872 warning(expr
->pos
, "testing a 'safe expression'");
878 static struct symbol
*evaluate_logical(struct expression
*expr
)
880 if (!evaluate_conditional(expr
->left
, 0))
882 if (!evaluate_conditional(expr
->right
, 0))
885 expr
->ctype
= &bool_ctype
;
887 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
893 static struct symbol
*evaluate_binop(struct expression
*expr
)
895 struct symbol
*ltype
, *rtype
, *ctype
;
896 int lclass
= classify_type(expr
->left
->ctype
, <ype
);
897 int rclass
= classify_type(expr
->right
->ctype
, &rtype
);
901 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
905 /* number op number */
906 if (lclass
& rclass
& TYPE_NUM
) {
907 if ((lclass
| rclass
) & TYPE_FLOAT
) {
909 case '+': case '-': case '*': case '/':
912 return bad_expr_type(expr
);
916 if (op
== SPECIAL_LEFTSHIFT
|| op
== SPECIAL_RIGHTSHIFT
) {
917 // shifts do integer promotions, but that's it.
918 unrestrict(expr
->left
, lclass
, <ype
);
919 unrestrict(expr
->right
, rclass
, &rtype
);
920 ctype
= ltype
= integer_promotion(ltype
);
921 rtype
= integer_promotion(rtype
);
923 // The rest do usual conversions
924 if (op
== '&' && expr
->left
->type
== EXPR_PREOP
&&
925 expr
->left
->op
== '!')
926 warning(expr
->pos
, "dubious: !x & y");
927 ltype
= usual_conversions(op
, expr
->left
, expr
->right
,
928 lclass
, rclass
, ltype
, rtype
);
929 ctype
= rtype
= ltype
;
932 expr
->left
= cast_to(expr
->left
, ltype
);
933 expr
->right
= cast_to(expr
->right
, rtype
);
938 /* pointer (+|-) integer */
939 if (lclass
& TYPE_PTR
&& is_int(rclass
) && (op
== '+' || op
== '-')) {
940 unrestrict(expr
->right
, rclass
, &rtype
);
941 return evaluate_ptr_add(expr
, rtype
);
944 /* integer + pointer */
945 if (rclass
& TYPE_PTR
&& is_int(lclass
) && op
== '+') {
946 struct expression
*index
= expr
->left
;
947 unrestrict(index
, lclass
, <ype
);
948 expr
->left
= expr
->right
;
950 return evaluate_ptr_add(expr
, ltype
);
953 /* pointer - pointer */
954 if (lclass
& rclass
& TYPE_PTR
&& expr
->op
== '-')
955 return evaluate_ptr_sub(expr
);
957 return bad_expr_type(expr
);
960 static struct symbol
*evaluate_comma(struct expression
*expr
)
962 expr
->ctype
= degenerate(expr
->right
);
963 if (expr
->ctype
== &null_ctype
)
964 expr
->ctype
= &ptr_ctype
;
965 expr
->flags
&= expr
->left
->flags
& expr
->right
->flags
;
969 static int modify_for_unsigned(int op
)
972 op
= SPECIAL_UNSIGNED_LT
;
974 op
= SPECIAL_UNSIGNED_GT
;
975 else if (op
== SPECIAL_LTE
)
976 op
= SPECIAL_UNSIGNED_LTE
;
977 else if (op
== SPECIAL_GTE
)
978 op
= SPECIAL_UNSIGNED_GTE
;
982 static inline int is_null_pointer_constant(struct expression
*e
)
984 if (e
->ctype
== &null_ctype
)
986 if (!(e
->flags
& Int_const_expr
))
988 return is_zero_constant(e
) ? 2 : 0;
991 static struct symbol
*evaluate_compare(struct expression
*expr
)
993 struct expression
*left
= expr
->left
, *right
= expr
->right
;
994 struct symbol
*ltype
, *rtype
, *lbase
, *rbase
;
995 int lclass
= classify_type(degenerate(left
), <ype
);
996 int rclass
= classify_type(degenerate(right
), &rtype
);
997 struct symbol
*ctype
;
998 const char *typediff
;
1001 if (!(expr
->left
->flags
& expr
->right
->flags
& Int_const_expr
))
1006 if (is_type_type(ltype
) && is_type_type(rtype
))
1009 if (is_safe_type(left
->ctype
) || is_safe_type(right
->ctype
))
1010 warning(expr
->pos
, "testing a 'safe expression'");
1012 /* number on number */
1013 if (lclass
& rclass
& TYPE_NUM
) {
1014 ctype
= usual_conversions(expr
->op
, expr
->left
, expr
->right
,
1015 lclass
, rclass
, ltype
, rtype
);
1016 expr
->left
= cast_to(expr
->left
, ctype
);
1017 expr
->right
= cast_to(expr
->right
, ctype
);
1018 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
1019 expr
->op
= modify_for_unsigned(expr
->op
);
1023 /* at least one must be a pointer */
1024 if (!((lclass
| rclass
) & TYPE_PTR
))
1025 return bad_expr_type(expr
);
1027 /* equality comparisons can be with null pointer constants */
1028 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1029 int is_null1
= is_null_pointer_constant(left
);
1030 int is_null2
= is_null_pointer_constant(right
);
1035 if (is_null1
&& is_null2
) {
1036 int positive
= expr
->op
== SPECIAL_EQUAL
;
1037 expr
->type
= EXPR_VALUE
;
1038 expr
->value
= positive
;
1041 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1042 left
= cast_to(left
, rtype
);
1045 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1046 right
= cast_to(right
, ltype
);
1050 /* both should be pointers */
1051 if (!(lclass
& rclass
& TYPE_PTR
))
1052 return bad_expr_type(expr
);
1053 expr
->op
= modify_for_unsigned(expr
->op
);
1055 lbase
= examine_pointer_target(ltype
);
1056 rbase
= examine_pointer_target(rtype
);
1058 /* they also have special treatment for pointers to void */
1059 if (expr
->op
== SPECIAL_EQUAL
|| expr
->op
== SPECIAL_NOTEQUAL
) {
1060 if (ltype
->ctype
.as
== rtype
->ctype
.as
) {
1061 if (lbase
== &void_ctype
) {
1062 right
= cast_to(right
, ltype
);
1065 if (rbase
== &void_ctype
) {
1066 left
= cast_to(left
, rtype
);
1072 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1073 target_qualifiers(rtype
),
1074 target_qualifiers(ltype
));
1078 expression_error(expr
, "incompatible types in comparison expression (%s)", typediff
);
1082 expr
->ctype
= &bool_ctype
;
1087 * NOTE! The degenerate case of "x ? : y", where we don't
1088 * have a true case, this will possibly promote "x" to the
1089 * same type as "y", and thus _change_ the conditional
1090 * test in the expression. But since promotion is "safe"
1091 * for testing, that's OK.
1093 static struct symbol
*evaluate_conditional_expression(struct expression
*expr
)
1095 struct expression
**true;
1096 struct symbol
*ctype
, *ltype
, *rtype
, *lbase
, *rbase
;
1098 const char * typediff
;
1101 if (!evaluate_conditional(expr
->conditional
, 0))
1103 if (!evaluate_expression(expr
->cond_false
))
1106 ctype
= degenerate(expr
->conditional
);
1107 rtype
= degenerate(expr
->cond_false
);
1109 true = &expr
->conditional
;
1111 if (expr
->cond_true
) {
1112 if (!evaluate_expression(expr
->cond_true
))
1114 ltype
= degenerate(expr
->cond_true
);
1115 true = &expr
->cond_true
;
1119 int flags
= expr
->conditional
->flags
& Int_const_expr
;
1120 flags
&= (*true)->flags
& expr
->cond_false
->flags
;
1125 lclass
= classify_type(ltype
, <ype
);
1126 rclass
= classify_type(rtype
, &rtype
);
1127 if (lclass
& rclass
& TYPE_NUM
) {
1128 ctype
= usual_conversions('?', *true, expr
->cond_false
,
1129 lclass
, rclass
, ltype
, rtype
);
1130 *true = cast_to(*true, ctype
);
1131 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1135 if ((lclass
| rclass
) & TYPE_PTR
) {
1136 int is_null1
= is_null_pointer_constant(*true);
1137 int is_null2
= is_null_pointer_constant(expr
->cond_false
);
1139 if (is_null1
&& is_null2
) {
1140 *true = cast_to(*true, &ptr_ctype
);
1141 expr
->cond_false
= cast_to(expr
->cond_false
, &ptr_ctype
);
1145 if (is_null1
&& (rclass
& TYPE_PTR
)) {
1148 *true = cast_to(*true, rtype
);
1152 if (is_null2
&& (lclass
& TYPE_PTR
)) {
1154 bad_null(expr
->cond_false
);
1155 expr
->cond_false
= cast_to(expr
->cond_false
, ltype
);
1159 if (!(lclass
& rclass
& TYPE_PTR
)) {
1160 typediff
= "different types";
1163 /* OK, it's pointer on pointer */
1164 if (ltype
->ctype
.as
!= rtype
->ctype
.as
) {
1165 typediff
= "different address spaces";
1169 /* need to be lazier here */
1170 lbase
= examine_pointer_target(ltype
);
1171 rbase
= examine_pointer_target(rtype
);
1172 qual
= target_qualifiers(ltype
) | target_qualifiers(rtype
);
1174 if (lbase
== &void_ctype
) {
1175 /* XXX: pointers to function should warn here */
1180 if (rbase
== &void_ctype
) {
1181 /* XXX: pointers to function should warn here */
1185 /* XXX: that should be pointer to composite */
1187 typediff
= type_difference(<ype
->ctype
, &rtype
->ctype
,
1194 /* void on void, struct on same struct, union on same union */
1195 if (ltype
== rtype
) {
1199 typediff
= "different base types";
1202 expression_error(expr
, "incompatible types in conditional expression (%s)", typediff
);
1206 expr
->ctype
= ctype
;
1210 if (qual
& ~ctype
->ctype
.modifiers
) {
1211 struct symbol
*sym
= alloc_symbol(ctype
->pos
, SYM_PTR
);
1213 sym
->ctype
.modifiers
|= qual
;
1216 *true = cast_to(*true, ctype
);
1217 expr
->cond_false
= cast_to(expr
->cond_false
, ctype
);
1221 /* FP assignments can not do modulo or bit operations */
1222 static int compatible_float_op(int op
)
1224 return op
== SPECIAL_ADD_ASSIGN
||
1225 op
== SPECIAL_SUB_ASSIGN
||
1226 op
== SPECIAL_MUL_ASSIGN
||
1227 op
== SPECIAL_DIV_ASSIGN
;
1230 static int evaluate_assign_op(struct expression
*expr
)
1232 struct symbol
*target
= expr
->left
->ctype
;
1233 struct symbol
*source
= expr
->right
->ctype
;
1234 struct symbol
*t
, *s
;
1235 int tclass
= classify_type(target
, &t
);
1236 int sclass
= classify_type(source
, &s
);
1239 if (tclass
& sclass
& TYPE_NUM
) {
1240 if (tclass
& TYPE_FLOAT
&& !compatible_float_op(op
)) {
1241 expression_error(expr
, "invalid assignment");
1244 if (tclass
& TYPE_RESTRICT
) {
1245 if (!restricted_binop(op
, t
)) {
1246 warning(expr
->pos
, "bad assignment (%s) to %s",
1247 show_special(op
), show_typename(t
));
1248 expr
->right
= cast_to(expr
->right
, target
);
1251 /* allowed assignments unfoul */
1252 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1254 if (!restricted_value(expr
->right
, t
))
1256 } else if (!(sclass
& TYPE_RESTRICT
))
1258 /* source and target would better be identical restricted */
1261 warning(expr
->pos
, "invalid assignment: %s", show_special(op
));
1262 info(expr
->pos
, " left side has type %s", show_typename(t
));
1263 info(expr
->pos
, " right side has type %s", show_typename(s
));
1264 expr
->right
= cast_to(expr
->right
, target
);
1267 if (tclass
== TYPE_PTR
&& is_int(sclass
)) {
1268 if (op
== SPECIAL_ADD_ASSIGN
|| op
== SPECIAL_SUB_ASSIGN
) {
1269 unrestrict(expr
->right
, sclass
, &s
);
1270 evaluate_ptr_add(expr
, s
);
1273 expression_error(expr
, "invalid pointer assignment");
1277 expression_error(expr
, "invalid assignment");
1281 expr
->right
= cast_to(expr
->right
, target
);
1285 static int whitelist_pointers(struct symbol
*t1
, struct symbol
*t2
)
1288 return 0; /* yes, 0 - we don't want a cast_to here */
1289 if (t1
== &void_ctype
)
1291 if (t2
== &void_ctype
)
1293 if (classify_type(t1
, &t1
) != TYPE_NUM
)
1295 if (classify_type(t2
, &t2
) != TYPE_NUM
)
1299 if (t1
->ctype
.modifiers
& t2
->ctype
.modifiers
& MOD_CHAR
)
1301 if ((t1
->ctype
.modifiers
^ t2
->ctype
.modifiers
) & MOD_SIZE
)
1306 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
1307 struct expression
**rp
, const char *where
)
1309 const char *typediff
;
1310 struct symbol
*source
= degenerate(*rp
);
1311 struct symbol
*t
, *s
;
1312 int tclass
= classify_type(target
, &t
);
1313 int sclass
= classify_type(source
, &s
);
1315 if (tclass
& sclass
& TYPE_NUM
) {
1316 if (tclass
& TYPE_RESTRICT
) {
1317 /* allowed assignments unfoul */
1318 if (sclass
& TYPE_FOULED
&& unfoul(s
) == t
)
1320 if (!restricted_value(*rp
, target
))
1324 } else if (!(sclass
& TYPE_RESTRICT
))
1326 typediff
= "different base types";
1330 if (tclass
== TYPE_PTR
) {
1331 unsigned long mod1
, mod2
;
1332 struct symbol
*b1
, *b2
;
1333 // NULL pointer is always OK
1334 int is_null
= is_null_pointer_constant(*rp
);
1340 if (!(sclass
& TYPE_PTR
)) {
1341 typediff
= "different base types";
1344 b1
= examine_pointer_target(t
);
1345 b2
= examine_pointer_target(s
);
1346 mod1
= target_qualifiers(t
);
1347 mod2
= target_qualifiers(s
);
1348 if (whitelist_pointers(b1
, b2
)) {
1350 * assignments to/from void * are OK, provided that
1351 * we do not remove qualifiers from pointed to [C]
1352 * or mix address spaces [sparse].
1354 if (t
->ctype
.as
!= s
->ctype
.as
) {
1355 typediff
= "different address spaces";
1359 typediff
= "different modifiers";
1364 /* It's OK if the target is more volatile or const than the source */
1365 typediff
= type_difference(&t
->ctype
, &s
->ctype
, 0, mod1
);
1371 if ((tclass
& TYPE_COMPOUND
) && s
== t
)
1374 if (tclass
& TYPE_NUM
) {
1375 /* XXX: need to turn into comparison with NULL */
1376 if (t
== &bool_ctype
&& (sclass
& TYPE_PTR
))
1378 typediff
= "different base types";
1381 typediff
= "invalid types";
1384 warning(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
1385 info(expr
->pos
, " expected %s", show_typename(target
));
1386 info(expr
->pos
, " got %s", show_typename(source
));
1387 *rp
= cast_to(*rp
, target
);
1390 *rp
= cast_to(*rp
, target
);
1394 static void mark_assigned(struct expression
*expr
)
1400 switch (expr
->type
) {
1405 if (sym
->type
!= SYM_NODE
)
1407 sym
->ctype
.modifiers
|= MOD_ASSIGNED
;
1411 mark_assigned(expr
->left
);
1412 mark_assigned(expr
->right
);
1415 case EXPR_FORCE_CAST
:
1416 mark_assigned(expr
->cast_expression
);
1419 mark_assigned(expr
->base
);
1427 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
1429 if (type
->ctype
.modifiers
& MOD_CONST
)
1430 expression_error(left
, "assignment to const expression");
1432 /* We know left is an lvalue, so it's a "preop-*" */
1433 mark_assigned(left
->unop
);
1436 static struct symbol
*evaluate_assignment(struct expression
*expr
)
1438 struct expression
*left
= expr
->left
;
1439 struct expression
*where
= expr
;
1440 struct symbol
*ltype
;
1442 if (!lvalue_expression(left
)) {
1443 expression_error(expr
, "not an lvalue");
1447 ltype
= left
->ctype
;
1449 if (expr
->op
!= '=') {
1450 if (!evaluate_assign_op(expr
))
1453 if (!compatible_assignment_types(where
, ltype
, &expr
->right
, "assignment"))
1457 evaluate_assign_to(left
, ltype
);
1459 expr
->ctype
= ltype
;
1463 static void examine_fn_arguments(struct symbol
*fn
)
1467 FOR_EACH_PTR(fn
->arguments
, s
) {
1468 struct symbol
*arg
= evaluate_symbol(s
);
1469 /* Array/function arguments silently degenerate into pointers */
1475 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
1476 if (arg
->type
== SYM_ARRAY
)
1477 ptr
->ctype
= arg
->ctype
;
1479 ptr
->ctype
.base_type
= arg
;
1480 ptr
->ctype
.as
|= s
->ctype
.as
;
1481 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
& MOD_PTRINHERIT
;
1483 s
->ctype
.base_type
= ptr
;
1485 s
->ctype
.modifiers
&= ~MOD_PTRINHERIT
;
1488 examine_symbol_type(s
);
1495 } END_FOR_EACH_PTR(s
);
1498 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
1500 /* Take the modifiers of the pointer, and apply them to the member */
1501 mod
|= sym
->ctype
.modifiers
;
1502 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
1503 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
1505 newsym
->ctype
.as
= as
;
1506 newsym
->ctype
.modifiers
= mod
;
1512 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
1514 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1515 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1517 node
->ctype
.base_type
= ptr
;
1518 ptr
->bit_size
= bits_in_pointer
;
1519 ptr
->ctype
.alignment
= pointer_alignment
;
1521 node
->bit_size
= bits_in_pointer
;
1522 node
->ctype
.alignment
= pointer_alignment
;
1525 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1526 warning(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1527 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1529 if (sym
->type
== SYM_NODE
) {
1530 ptr
->ctype
.as
|= sym
->ctype
.as
;
1531 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1532 sym
= sym
->ctype
.base_type
;
1534 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1535 ptr
->ctype
.as
|= sym
->ctype
.as
;
1536 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
& MOD_PTRINHERIT
;
1537 sym
= sym
->ctype
.base_type
;
1539 ptr
->ctype
.base_type
= sym
;
1544 /* Arrays degenerate into pointers on pointer arithmetic */
1545 static struct symbol
*degenerate(struct expression
*expr
)
1547 struct symbol
*ctype
, *base
;
1551 ctype
= expr
->ctype
;
1554 base
= examine_symbol_type(ctype
);
1555 if (ctype
->type
== SYM_NODE
)
1556 base
= ctype
->ctype
.base_type
;
1558 * Arrays degenerate into pointers to the entries, while
1559 * functions degenerate into pointers to themselves.
1560 * If array was part of non-lvalue compound, we create a copy
1561 * of that compound first and then act as if we were dealing with
1562 * the corresponding field in there.
1564 switch (base
->type
) {
1566 if (expr
->type
== EXPR_SLICE
) {
1567 struct symbol
*a
= alloc_symbol(expr
->pos
, SYM_NODE
);
1568 struct expression
*e0
, *e1
, *e2
, *e3
, *e4
;
1570 a
->ctype
.base_type
= expr
->base
->ctype
;
1571 a
->bit_size
= expr
->base
->ctype
->bit_size
;
1572 a
->array_size
= expr
->base
->ctype
->array_size
;
1574 e0
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1576 e0
->ctype
= &lazy_ptr_ctype
;
1578 e1
= alloc_expression(expr
->pos
, EXPR_PREOP
);
1581 e1
->ctype
= expr
->base
->ctype
; /* XXX */
1583 e2
= alloc_expression(expr
->pos
, EXPR_ASSIGNMENT
);
1585 e2
->right
= expr
->base
;
1587 e2
->ctype
= expr
->base
->ctype
;
1589 if (expr
->r_bitpos
) {
1590 e3
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1593 e3
->right
= alloc_const_expression(expr
->pos
,
1594 expr
->r_bitpos
>> 3);
1595 e3
->ctype
= &lazy_ptr_ctype
;
1600 e4
= alloc_expression(expr
->pos
, EXPR_COMMA
);
1603 e4
->ctype
= &lazy_ptr_ctype
;
1606 expr
->type
= EXPR_PREOP
;
1610 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1611 expression_error(expr
, "strange non-value function or array");
1614 *expr
= *expr
->unop
;
1615 ctype
= create_pointer(expr
, ctype
, 1);
1616 expr
->ctype
= ctype
;
1623 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1625 struct expression
*op
= expr
->unop
;
1626 struct symbol
*ctype
;
1628 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1629 expression_error(expr
, "not addressable");
1636 if (expr
->type
== EXPR_SYMBOL
) {
1637 struct symbol
*sym
= expr
->symbol
;
1638 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1642 * symbol expression evaluation is lazy about the type
1643 * of the sub-expression, so we may have to generate
1644 * the type here if so..
1646 if (expr
->ctype
== &lazy_ptr_ctype
) {
1647 ctype
= create_pointer(expr
, ctype
, 0);
1648 expr
->ctype
= ctype
;
1654 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1656 struct expression
*op
= expr
->unop
;
1657 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1659 /* Simplify: *&(expr) => (expr) */
1660 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1666 /* Dereferencing a node drops all the node information. */
1667 if (ctype
->type
== SYM_NODE
)
1668 ctype
= ctype
->ctype
.base_type
;
1670 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1671 target
= ctype
->ctype
.base_type
;
1673 switch (ctype
->type
) {
1675 expression_error(expr
, "cannot dereference this type");
1678 node
->ctype
.modifiers
= target
->ctype
.modifiers
& MOD_SPECIFIER
;
1679 merge_type(node
, ctype
);
1683 if (!lvalue_expression(op
)) {
1684 expression_error(op
, "non-lvalue array??");
1688 /* Do the implied "addressof" on the array */
1692 * When an array is dereferenced, we need to pick
1693 * up the attributes of the original node too..
1695 merge_type(node
, op
->ctype
);
1696 merge_type(node
, ctype
);
1700 node
->bit_size
= target
->bit_size
;
1701 node
->array_size
= target
->array_size
;
1708 * Unary post-ops: x++ and x--
1710 static struct symbol
*evaluate_postop(struct expression
*expr
)
1712 struct expression
*op
= expr
->unop
;
1713 struct symbol
*ctype
= op
->ctype
;
1714 int class = classify_type(op
->ctype
, &ctype
);
1717 if (!lvalue_expression(expr
->unop
)) {
1718 expression_error(expr
, "need lvalue expression for ++/--");
1722 if ((class & TYPE_RESTRICT
) && restricted_unop(expr
->op
, &ctype
))
1723 return bad_expr_type(expr
);
1725 if (class & TYPE_NUM
) {
1727 } else if (class == TYPE_PTR
) {
1728 struct symbol
*target
= examine_pointer_target(ctype
);
1729 if (!is_function(target
))
1730 multiply
= target
->bit_size
>> 3;
1734 evaluate_assign_to(op
, op
->ctype
);
1735 expr
->op_value
= multiply
;
1736 expr
->ctype
= ctype
;
1740 expression_error(expr
, "bad argument type for ++/--");
1744 static struct symbol
*evaluate_sign(struct expression
*expr
)
1746 struct symbol
*ctype
= expr
->unop
->ctype
;
1747 int class = classify_type(ctype
, &ctype
);
1748 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1750 /* should be an arithmetic type */
1751 if (!(class & TYPE_NUM
))
1752 return bad_expr_type(expr
);
1753 if (!(class & (TYPE_FLOAT
|TYPE_RESTRICT
))) {
1754 struct symbol
*rtype
= integer_promotion(ctype
);
1755 expr
->unop
= cast_to(expr
->unop
, rtype
);
1757 } else if ((class & TYPE_FLOAT
) && expr
->op
!= '~') {
1758 /* no conversions needed */
1759 } else if ((class & TYPE_RESTRICT
) && !restricted_unop(expr
->op
, &ctype
)) {
1760 /* no conversions needed */
1762 return bad_expr_type(expr
);
1764 if (expr
->op
== '+')
1765 *expr
= *expr
->unop
;
1766 expr
->ctype
= ctype
;
1770 static struct symbol
*evaluate_preop(struct expression
*expr
)
1772 struct symbol
*ctype
= expr
->unop
->ctype
;
1776 *expr
= *expr
->unop
;
1782 return evaluate_sign(expr
);
1785 return evaluate_dereference(expr
);
1788 return evaluate_addressof(expr
);
1790 case SPECIAL_INCREMENT
:
1791 case SPECIAL_DECREMENT
:
1793 * From a type evaluation standpoint the preops are
1794 * the same as the postops
1796 return evaluate_postop(expr
);
1799 if (expr
->flags
&& !(expr
->unop
->flags
& Int_const_expr
))
1801 if (is_safe_type(ctype
))
1802 warning(expr
->pos
, "testing a 'safe expression'");
1803 if (is_float_type(ctype
)) {
1804 struct expression
*arg
= expr
->unop
;
1805 expr
->type
= EXPR_BINOP
;
1806 expr
->op
= SPECIAL_EQUAL
;
1808 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1809 expr
->right
->ctype
= ctype
;
1810 expr
->right
->fvalue
= 0;
1811 } else if (is_fouled_type(ctype
)) {
1812 warning(expr
->pos
, "%s degrades to integer",
1813 show_typename(ctype
->ctype
.base_type
));
1815 ctype
= &bool_ctype
;
1821 expr
->ctype
= ctype
;
1825 static struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1827 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1828 struct ptr_list
*list
= head
;
1834 for (i
= 0; i
< list
->nr
; i
++) {
1835 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1837 if (sym
->ident
!= ident
)
1839 *offset
= sym
->offset
;
1842 struct symbol
*ctype
= sym
->ctype
.base_type
;
1846 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1848 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1851 *offset
+= sym
->offset
;
1855 } while ((list
= list
->next
) != head
);
1859 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1861 struct expression
*add
;
1864 * Create a new add-expression
1866 * NOTE! Even if we just add zero, we need a new node
1867 * for the member pointer, since it has a different
1868 * type than the original pointer. We could make that
1869 * be just a cast, but the fact is, a node is a node,
1870 * so we might as well just do the "add zero" here.
1872 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1875 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1876 add
->right
->ctype
= &int_ctype
;
1877 add
->right
->value
= offset
;
1880 * The ctype of the pointer will be lazily evaluated if
1881 * we ever take the address of this member dereference..
1883 add
->ctype
= &lazy_ptr_ctype
;
1887 /* structure/union dereference */
1888 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1891 struct symbol
*ctype
, *member
;
1892 struct expression
*deref
= expr
->deref
, *add
;
1893 struct ident
*ident
= expr
->member
;
1897 if (!evaluate_expression(deref
))
1900 expression_error(expr
, "bad member name");
1904 ctype
= deref
->ctype
;
1905 examine_symbol_type(ctype
);
1906 address_space
= ctype
->ctype
.as
;
1907 mod
= ctype
->ctype
.modifiers
;
1908 if (ctype
->type
== SYM_NODE
) {
1909 ctype
= ctype
->ctype
.base_type
;
1910 address_space
|= ctype
->ctype
.as
;
1911 mod
|= ctype
->ctype
.modifiers
;
1913 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1914 expression_error(expr
, "expected structure or union");
1918 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1920 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1921 const char *name
= "<unnamed>";
1924 name
= ctype
->ident
->name
;
1925 namelen
= ctype
->ident
->len
;
1927 if (ctype
->symbol_list
)
1928 expression_error(expr
, "no member '%s' in %s %.*s",
1929 show_ident(ident
), type
, namelen
, name
);
1931 expression_error(expr
, "using member '%s' in "
1932 "incomplete %s %.*s", show_ident(ident
),
1933 type
, namelen
, name
);
1938 * The member needs to take on the address space and modifiers of
1939 * the "parent" type.
1941 member
= convert_to_as_mod(member
, address_space
, mod
);
1942 ctype
= get_base_type(member
);
1944 if (!lvalue_expression(deref
)) {
1945 if (deref
->type
!= EXPR_SLICE
) {
1949 expr
->base
= deref
->base
;
1950 expr
->r_bitpos
= deref
->r_bitpos
;
1952 expr
->r_bitpos
+= offset
<< 3;
1953 expr
->type
= EXPR_SLICE
;
1954 expr
->r_nrbits
= member
->bit_size
;
1955 expr
->r_bitpos
+= member
->bit_offset
;
1956 expr
->ctype
= member
;
1960 deref
= deref
->unop
;
1961 expr
->deref
= deref
;
1963 add
= evaluate_offset(deref
, offset
);
1964 expr
->type
= EXPR_PREOP
;
1968 expr
->ctype
= member
;
1972 static int is_promoted(struct expression
*expr
)
1975 switch (expr
->type
) {
1978 case EXPR_CONDITIONAL
:
2002 static struct symbol
*evaluate_cast(struct expression
*);
2004 static struct symbol
*evaluate_type_information(struct expression
*expr
)
2006 struct symbol
*sym
= expr
->cast_type
;
2008 sym
= evaluate_expression(expr
->cast_expression
);
2012 * Expressions of restricted types will possibly get
2013 * promoted - check that here
2015 if (is_restricted_type(sym
)) {
2016 if (sym
->bit_size
< bits_in_int
&& is_promoted(expr
))
2018 } else if (is_fouled_type(sym
)) {
2022 examine_symbol_type(sym
);
2023 if (is_bitfield_type(sym
)) {
2024 expression_error(expr
, "trying to examine bitfield type");
2030 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
2032 struct symbol
*type
;
2035 type
= evaluate_type_information(expr
);
2039 size
= type
->bit_size
;
2040 if ((size
< 0) || (size
& 7))
2041 expression_error(expr
, "cannot size expression");
2042 expr
->type
= EXPR_VALUE
;
2043 expr
->value
= size
>> 3;
2045 expr
->ctype
= size_t_ctype
;
2046 return size_t_ctype
;
2049 static struct symbol
*evaluate_ptrsizeof(struct expression
*expr
)
2051 struct symbol
*type
;
2054 type
= evaluate_type_information(expr
);
2058 if (type
->type
== SYM_NODE
)
2059 type
= type
->ctype
.base_type
;
2062 switch (type
->type
) {
2066 type
= get_base_type(type
);
2070 expression_error(expr
, "expected pointer expression");
2073 size
= type
->bit_size
;
2076 expr
->type
= EXPR_VALUE
;
2077 expr
->value
= size
>> 3;
2079 expr
->ctype
= size_t_ctype
;
2080 return size_t_ctype
;
2083 static struct symbol
*evaluate_alignof(struct expression
*expr
)
2085 struct symbol
*type
;
2087 type
= evaluate_type_information(expr
);
2091 expr
->type
= EXPR_VALUE
;
2092 expr
->value
= type
->ctype
.alignment
;
2094 expr
->ctype
= size_t_ctype
;
2095 return size_t_ctype
;
2098 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
2100 struct expression
*expr
;
2101 struct symbol_list
*argument_types
= fn
->arguments
;
2102 struct symbol
*argtype
;
2105 PREPARE_PTR_LIST(argument_types
, argtype
);
2106 FOR_EACH_PTR (head
, expr
) {
2107 struct expression
**p
= THIS_ADDRESS(expr
);
2108 struct symbol
*ctype
, *target
;
2109 ctype
= evaluate_expression(expr
);
2116 struct symbol
*type
;
2117 int class = classify_type(ctype
, &type
);
2118 if (is_int(class)) {
2119 *p
= cast_to(expr
, integer_promotion(type
));
2120 } else if (class & TYPE_FLOAT
) {
2121 unsigned long mod
= type
->ctype
.modifiers
;
2122 if (!(mod
& (MOD_LONG
|MOD_LONGLONG
)))
2123 *p
= cast_to(expr
, &double_ctype
);
2124 } else if (class & TYPE_PTR
) {
2125 if (expr
->ctype
== &null_ctype
)
2126 *p
= cast_to(expr
, &ptr_ctype
);
2131 static char where
[30];
2132 examine_symbol_type(target
);
2133 sprintf(where
, "argument %d", i
);
2134 compatible_assignment_types(expr
, target
, p
, where
);
2138 NEXT_PTR_LIST(argtype
);
2139 } END_FOR_EACH_PTR(expr
);
2140 FINISH_PTR_LIST(argtype
);
2144 static struct symbol
*find_struct_ident(struct symbol
*ctype
, struct ident
*ident
)
2148 FOR_EACH_PTR(ctype
->symbol_list
, sym
) {
2149 if (sym
->ident
== ident
)
2151 } END_FOR_EACH_PTR(sym
);
2155 static void convert_index(struct expression
*e
)
2157 struct expression
*child
= e
->idx_expression
;
2158 unsigned from
= e
->idx_from
;
2159 unsigned to
= e
->idx_to
+ 1;
2161 e
->init_offset
= from
* (e
->ctype
->bit_size
>>3);
2162 e
->init_nr
= to
- from
;
2163 e
->init_expr
= child
;
2166 static void convert_ident(struct expression
*e
)
2168 struct expression
*child
= e
->ident_expression
;
2169 struct symbol
*sym
= e
->field
;
2171 e
->init_offset
= sym
->offset
;
2173 e
->init_expr
= child
;
2176 static void convert_designators(struct expression
*e
)
2179 if (e
->type
== EXPR_INDEX
)
2181 else if (e
->type
== EXPR_IDENTIFIER
)
2189 static void excess(struct expression
*e
, const char *s
)
2191 warning(e
->pos
, "excessive elements in %s initializer", s
);
2195 * implicit designator for the first element
2197 static struct expression
*first_subobject(struct symbol
*ctype
, int class,
2198 struct expression
**v
)
2200 struct expression
*e
= *v
, *new;
2202 if (ctype
->type
== SYM_NODE
)
2203 ctype
= ctype
->ctype
.base_type
;
2205 if (class & TYPE_PTR
) { /* array */
2206 if (!ctype
->bit_size
)
2208 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2209 new->idx_expression
= e
;
2210 new->ctype
= ctype
->ctype
.base_type
;
2212 struct symbol
*field
, *p
;
2213 PREPARE_PTR_LIST(ctype
->symbol_list
, p
);
2214 while (p
&& !p
->ident
&& is_bitfield_type(p
))
2220 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2221 new->ident_expression
= e
;
2222 new->field
= new->ctype
= field
;
2229 * sanity-check explicit designators; return the innermost one or NULL
2230 * in case of error. Assign types.
2232 static struct expression
*check_designators(struct expression
*e
,
2233 struct symbol
*ctype
)
2235 struct expression
*last
= NULL
;
2238 if (ctype
->type
== SYM_NODE
)
2239 ctype
= ctype
->ctype
.base_type
;
2240 if (e
->type
== EXPR_INDEX
) {
2241 struct symbol
*type
;
2242 if (ctype
->type
!= SYM_ARRAY
) {
2243 err
= "array index in non-array";
2246 type
= ctype
->ctype
.base_type
;
2247 if (ctype
->bit_size
>= 0 && type
->bit_size
>= 0) {
2248 unsigned offset
= e
->idx_to
* type
->bit_size
;
2249 if (offset
>= ctype
->bit_size
) {
2250 err
= "index out of bounds in";
2254 e
->ctype
= ctype
= type
;
2257 if (!e
->idx_expression
) {
2261 e
= e
->idx_expression
;
2262 } else if (e
->type
== EXPR_IDENTIFIER
) {
2263 if (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
) {
2264 err
= "field name not in struct or union";
2267 ctype
= find_struct_ident(ctype
, e
->expr_ident
);
2269 err
= "unknown field name in";
2272 e
->field
= e
->ctype
= ctype
;
2274 if (!e
->ident_expression
) {
2278 e
= e
->ident_expression
;
2279 } else if (e
->type
== EXPR_POS
) {
2280 err
= "internal front-end error: EXPR_POS in";
2285 expression_error(e
, "%s initializer", err
);
2290 * choose the next subobject to initialize.
2292 * Get designators for next element, switch old ones to EXPR_POS.
2293 * Return the resulting expression or NULL if we'd run out of subobjects.
2294 * The innermost designator is returned in *v. Designators in old
2295 * are assumed to be already sanity-checked.
2297 static struct expression
*next_designators(struct expression
*old
,
2298 struct symbol
*ctype
,
2299 struct expression
*e
, struct expression
**v
)
2301 struct expression
*new = NULL
;
2305 if (old
->type
== EXPR_INDEX
) {
2306 struct expression
*copy
;
2309 copy
= next_designators(old
->idx_expression
,
2312 n
= old
->idx_to
+ 1;
2313 if (n
* old
->ctype
->bit_size
== ctype
->bit_size
) {
2318 *v
= new = alloc_expression(e
->pos
, EXPR_INDEX
);
2321 new = alloc_expression(e
->pos
, EXPR_INDEX
);
2324 new->idx_from
= new->idx_to
= n
;
2325 new->idx_expression
= copy
;
2326 new->ctype
= old
->ctype
;
2328 } else if (old
->type
== EXPR_IDENTIFIER
) {
2329 struct expression
*copy
;
2330 struct symbol
*field
;
2332 copy
= next_designators(old
->ident_expression
,
2335 field
= old
->field
->next_subobject
;
2341 *v
= new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2344 new = alloc_expression(e
->pos
, EXPR_IDENTIFIER
);
2348 new->expr_ident
= field
->ident
;
2349 new->ident_expression
= copy
;
2356 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2357 int class, struct symbol
*ctype
);
2360 * deal with traversing subobjects [6.7.8(17,18,20)]
2362 static void handle_list_initializer(struct expression
*expr
,
2363 int class, struct symbol
*ctype
)
2365 struct expression
*e
, *last
= NULL
, *top
= NULL
, *next
;
2368 FOR_EACH_PTR(expr
->expr_list
, e
) {
2369 struct expression
**v
;
2370 struct symbol
*type
;
2373 if (e
->type
!= EXPR_INDEX
&& e
->type
!= EXPR_IDENTIFIER
) {
2376 last
= first_subobject(ctype
, class, &top
);
2378 last
= next_designators(last
, ctype
, e
, &top
);
2381 excess(e
, class & TYPE_PTR
? "array" :
2383 DELETE_CURRENT_PTR(e
);
2387 warning(e
->pos
, "advancing past deep designator");
2390 REPLACE_CURRENT_PTR(e
, last
);
2392 next
= check_designators(e
, ctype
);
2394 DELETE_CURRENT_PTR(e
);
2398 /* deeper than one designator? */
2400 convert_designators(last
);
2405 lclass
= classify_type(top
->ctype
, &type
);
2406 if (top
->type
== EXPR_INDEX
)
2407 v
= &top
->idx_expression
;
2409 v
= &top
->ident_expression
;
2411 if (handle_simple_initializer(v
, 1, lclass
, top
->ctype
))
2414 if (!(lclass
& TYPE_COMPOUND
)) {
2415 warning(e
->pos
, "bogus scalar initializer");
2416 DELETE_CURRENT_PTR(e
);
2420 next
= first_subobject(type
, lclass
, v
);
2422 warning(e
->pos
, "missing braces around initializer");
2427 DELETE_CURRENT_PTR(e
);
2428 excess(e
, lclass
& TYPE_PTR
? "array" : "struct or union");
2430 } END_FOR_EACH_PTR(e
);
2432 convert_designators(last
);
2433 expr
->ctype
= ctype
;
2436 static int is_string_literal(struct expression
**v
)
2438 struct expression
*e
= *v
;
2439 while (e
&& e
->type
== EXPR_PREOP
&& e
->op
== '(')
2441 if (!e
|| e
->type
!= EXPR_STRING
)
2443 if (e
!= *v
&& Wparen_string
)
2445 "array initialized from parenthesized string constant");
2451 * We want a normal expression, possibly in one layer of braces. Warn
2452 * if the latter happens inside a list (it's legal, but likely to be
2453 * an effect of screwup). In case of anything not legal, we are definitely
2454 * having an effect of screwup, so just fail and let the caller warn.
2456 static struct expression
*handle_scalar(struct expression
*e
, int nested
)
2458 struct expression
*v
= NULL
, *p
;
2462 if (e
->type
!= EXPR_INITIALIZER
)
2465 FOR_EACH_PTR(e
->expr_list
, p
) {
2469 } END_FOR_EACH_PTR(p
);
2473 case EXPR_INITIALIZER
:
2475 case EXPR_IDENTIFIER
:
2481 warning(e
->pos
, "braces around scalar initializer");
2486 * deal with the cases that don't care about subobjects:
2487 * scalar <- assignment expression, possibly in braces [6.7.8(11)]
2488 * character array <- string literal, possibly in braces [6.7.8(14)]
2489 * struct or union <- assignment expression of compatible type [6.7.8(13)]
2490 * compound type <- initializer list in braces [6.7.8(16)]
2491 * The last one punts to handle_list_initializer() which, in turn will call
2492 * us for individual elements of the list.
2494 * We do not handle 6.7.8(15) (wide char array <- wide string literal) for
2495 * the lack of support of wide char stuff in general.
2497 * One note: we need to take care not to evaluate a string literal until
2498 * we know that we *will* handle it right here. Otherwise we would screw
2499 * the cases like struct { struct {char s[10]; ...} ...} initialized with
2500 * { "string", ...} - we need to preserve that string literal recognizable
2501 * until we dig into the inner struct.
2503 static int handle_simple_initializer(struct expression
**ep
, int nested
,
2504 int class, struct symbol
*ctype
)
2506 int is_string
= is_string_type(ctype
);
2507 struct expression
*e
= *ep
, *p
;
2508 struct symbol
*type
;
2514 if (!(class & TYPE_COMPOUND
)) {
2515 e
= handle_scalar(e
, nested
);
2519 if (!evaluate_expression(e
))
2521 compatible_assignment_types(e
, ctype
, ep
, "initializer");
2526 * sublist; either a string, or we dig in; the latter will deal with
2527 * pathologies, so we don't need anything fancy here.
2529 if (e
->type
== EXPR_INITIALIZER
) {
2531 struct expression
*v
= NULL
;
2534 FOR_EACH_PTR(e
->expr_list
, p
) {
2538 } END_FOR_EACH_PTR(p
);
2539 if (count
== 1 && is_string_literal(&v
)) {
2544 handle_list_initializer(e
, class, ctype
);
2549 if (is_string_literal(&e
)) {
2550 /* either we are doing array of char, or we'll have to dig in */
2557 /* struct or union can be initialized by compatible */
2558 if (class != TYPE_COMPOUND
)
2560 type
= evaluate_expression(e
);
2563 if (ctype
->type
== SYM_NODE
)
2564 ctype
= ctype
->ctype
.base_type
;
2565 if (type
->type
== SYM_NODE
)
2566 type
= type
->ctype
.base_type
;
2572 p
= alloc_expression(e
->pos
, EXPR_STRING
);
2574 type
= evaluate_expression(p
);
2575 if (ctype
->bit_size
!= -1 &&
2576 ctype
->bit_size
+ bits_in_char
< type
->bit_size
) {
2578 "too long initializer-string for array of char");
2584 static void evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
)
2586 struct symbol
*type
;
2587 int class = classify_type(ctype
, &type
);
2588 if (!handle_simple_initializer(ep
, 0, class, ctype
))
2589 expression_error(*ep
, "invalid initializer");
2592 static struct symbol
*evaluate_cast(struct expression
*expr
)
2594 struct expression
*target
= expr
->cast_expression
;
2595 struct symbol
*ctype
;
2596 struct symbol
*t1
, *t2
;
2598 int as1
= 0, as2
= 0;
2604 * Special case: a cast can be followed by an
2605 * initializer, in which case we need to pass
2606 * the type value down to that initializer rather
2607 * than trying to evaluate it as an expression
2609 * A more complex case is when the initializer is
2610 * dereferenced as part of a post-fix expression.
2611 * We need to produce an expression that can be dereferenced.
2613 if (target
->type
== EXPR_INITIALIZER
) {
2614 struct symbol
*sym
= expr
->cast_type
;
2615 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
2617 sym
->initializer
= target
;
2618 evaluate_symbol(sym
);
2620 addr
->ctype
= &lazy_ptr_ctype
; /* Lazy eval */
2623 expr
->type
= EXPR_PREOP
;
2631 ctype
= examine_symbol_type(expr
->cast_type
);
2632 expr
->ctype
= ctype
;
2633 expr
->cast_type
= ctype
;
2635 evaluate_expression(target
);
2638 class1
= classify_type(ctype
, &t1
);
2640 /* cast to non-integer type -> not an integer constant expression */
2641 if (!is_int(class1
))
2643 /* if argument turns out to be not an integer constant expression *and*
2644 it was not a floating literal to start with -> too bad */
2645 else if (expr
->flags
== Int_const_expr
&&
2646 !(target
->flags
& Int_const_expr
))
2649 * You can always throw a value away by casting to
2650 * "void" - that's an implicit "force". Note that
2651 * the same is _not_ true of "void *".
2653 if (t1
== &void_ctype
)
2656 if (class1
& (TYPE_COMPOUND
| TYPE_FN
))
2657 warning(expr
->pos
, "cast to non-scalar");
2661 expression_error(expr
, "cast from unknown type");
2664 class2
= classify_type(t2
, &t2
);
2666 if (class2
& TYPE_COMPOUND
)
2667 warning(expr
->pos
, "cast from non-scalar");
2669 if (expr
->type
== EXPR_FORCE_CAST
)
2672 /* allowed cast unfouls */
2673 if (class2
& TYPE_FOULED
)
2677 if (class1
& TYPE_RESTRICT
)
2678 warning(expr
->pos
, "cast to %s",
2680 if (class2
& TYPE_RESTRICT
)
2681 warning(expr
->pos
, "cast from %s",
2685 if (t1
== &ulong_ctype
)
2687 else if (class1
== TYPE_PTR
) {
2688 examine_pointer_target(t1
);
2692 if (t2
== &ulong_ctype
)
2694 else if (class2
== TYPE_PTR
) {
2695 examine_pointer_target(t2
);
2699 if (!as1
&& as2
> 0)
2700 warning(expr
->pos
, "cast removes address space of expression");
2701 if (as1
> 0 && as2
> 0 && as1
!= as2
)
2702 warning(expr
->pos
, "cast between address spaces (<asn:%d>-><asn:%d>)", as2
, as1
);
2703 if (as1
> 0 && !as2
&&
2704 !is_null_pointer_constant(target
) && Wcast_to_as
)
2706 "cast adds address space to expression (<asn:%d>)", as1
);
2708 if (!(t1
->ctype
.modifiers
& MOD_PTRINHERIT
) && class1
== TYPE_PTR
&&
2709 !as1
&& (target
->flags
& Int_const_expr
)) {
2710 if (t1
->ctype
.base_type
== &void_ctype
) {
2711 if (is_zero_constant(target
)) {
2713 expr
->type
= EXPR_VALUE
;
2714 expr
->ctype
= &null_ctype
;
2725 * Evaluate a call expression with a symbol. This
2726 * should expand inline functions, and evaluate
2729 static int evaluate_symbol_call(struct expression
*expr
)
2731 struct expression
*fn
= expr
->fn
;
2732 struct symbol
*ctype
= fn
->ctype
;
2734 if (fn
->type
!= EXPR_PREOP
)
2737 if (ctype
->op
&& ctype
->op
->evaluate
)
2738 return ctype
->op
->evaluate(expr
);
2740 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
2742 struct symbol
*curr
= current_fn
;
2743 current_fn
= ctype
->ctype
.base_type
;
2745 ret
= inline_function(expr
, ctype
);
2747 /* restore the old function */
2755 static struct symbol
*evaluate_call(struct expression
*expr
)
2758 struct symbol
*ctype
, *sym
;
2759 struct expression
*fn
= expr
->fn
;
2760 struct expression_list
*arglist
= expr
->args
;
2762 if (!evaluate_expression(fn
))
2764 sym
= ctype
= fn
->ctype
;
2765 if (ctype
->type
== SYM_NODE
)
2766 ctype
= ctype
->ctype
.base_type
;
2767 if (ctype
->type
== SYM_PTR
)
2768 ctype
= get_base_type(ctype
);
2770 if (ctype
->type
!= SYM_FN
) {
2771 struct expression
*arg
;
2772 expression_error(expr
, "not a function %s",
2773 show_ident(sym
->ident
));
2774 /* do typechecking in arguments */
2775 FOR_EACH_PTR (arglist
, arg
) {
2776 evaluate_expression(arg
);
2777 } END_FOR_EACH_PTR(arg
);
2781 examine_fn_arguments(ctype
);
2782 if (sym
->type
== SYM_NODE
&& fn
->type
== EXPR_PREOP
&&
2783 sym
->op
&& sym
->op
->args
) {
2784 if (!sym
->op
->args(expr
))
2787 if (!evaluate_arguments(sym
, ctype
, arglist
))
2789 args
= expression_list_size(expr
->args
);
2790 fnargs
= symbol_list_size(ctype
->arguments
);
2792 expression_error(expr
,
2793 "not enough arguments for function %s",
2794 show_ident(sym
->ident
));
2795 if (args
> fnargs
&& !ctype
->variadic
)
2796 expression_error(expr
,
2797 "too many arguments for function %s",
2798 show_ident(sym
->ident
));
2800 if (sym
->type
== SYM_NODE
) {
2801 if (evaluate_symbol_call(expr
))
2804 expr
->ctype
= ctype
->ctype
.base_type
;
2808 static struct symbol
*evaluate_offsetof(struct expression
*expr
)
2810 struct expression
*e
= expr
->down
;
2811 struct symbol
*ctype
= expr
->in
;
2814 if (expr
->op
== '.') {
2815 struct symbol
*field
;
2818 expression_error(expr
, "expected structure or union");
2821 examine_symbol_type(ctype
);
2822 class = classify_type(ctype
, &ctype
);
2823 if (class != TYPE_COMPOUND
) {
2824 expression_error(expr
, "expected structure or union");
2828 field
= find_identifier(expr
->ident
, ctype
->symbol_list
, &offset
);
2830 expression_error(expr
, "unknown member");
2834 expr
->type
= EXPR_VALUE
;
2835 expr
->flags
= Int_const_expr
;
2836 expr
->value
= offset
;
2838 expr
->ctype
= size_t_ctype
;
2841 expression_error(expr
, "expected structure or union");
2844 examine_symbol_type(ctype
);
2845 class = classify_type(ctype
, &ctype
);
2846 if (class != (TYPE_COMPOUND
| TYPE_PTR
)) {
2847 expression_error(expr
, "expected array");
2850 ctype
= ctype
->ctype
.base_type
;
2852 expr
->type
= EXPR_VALUE
;
2853 expr
->flags
= Int_const_expr
;
2856 expr
->ctype
= size_t_ctype
;
2858 struct expression
*idx
= expr
->index
, *m
;
2859 struct symbol
*i_type
= evaluate_expression(idx
);
2860 int i_class
= classify_type(i_type
, &i_type
);
2861 if (!is_int(i_class
)) {
2862 expression_error(expr
, "non-integer index");
2865 unrestrict(idx
, i_class
, &i_type
);
2866 idx
= cast_to(idx
, size_t_ctype
);
2867 m
= alloc_const_expression(expr
->pos
,
2868 ctype
->bit_size
>> 3);
2869 m
->ctype
= size_t_ctype
;
2870 m
->flags
= Int_const_expr
;
2871 expr
->type
= EXPR_BINOP
;
2875 expr
->ctype
= size_t_ctype
;
2876 expr
->flags
= m
->flags
& idx
->flags
& Int_const_expr
;
2880 struct expression
*copy
= __alloc_expression(0);
2882 if (e
->type
== EXPR_OFFSETOF
)
2884 if (!evaluate_expression(e
))
2886 expr
->type
= EXPR_BINOP
;
2887 expr
->flags
= e
->flags
& copy
->flags
& Int_const_expr
;
2889 expr
->ctype
= size_t_ctype
;
2893 return size_t_ctype
;
2896 struct symbol
*evaluate_expression(struct expression
*expr
)
2903 switch (expr
->type
) {
2906 expression_error(expr
, "value expression without a type");
2909 return evaluate_string(expr
);
2911 return evaluate_symbol_expression(expr
);
2913 if (!evaluate_expression(expr
->left
))
2915 if (!evaluate_expression(expr
->right
))
2917 return evaluate_binop(expr
);
2919 return evaluate_logical(expr
);
2921 evaluate_expression(expr
->left
);
2922 if (!evaluate_expression(expr
->right
))
2924 return evaluate_comma(expr
);
2926 if (!evaluate_expression(expr
->left
))
2928 if (!evaluate_expression(expr
->right
))
2930 return evaluate_compare(expr
);
2931 case EXPR_ASSIGNMENT
:
2932 if (!evaluate_expression(expr
->left
))
2934 if (!evaluate_expression(expr
->right
))
2936 return evaluate_assignment(expr
);
2938 if (!evaluate_expression(expr
->unop
))
2940 return evaluate_preop(expr
);
2942 if (!evaluate_expression(expr
->unop
))
2944 return evaluate_postop(expr
);
2946 case EXPR_FORCE_CAST
:
2947 case EXPR_IMPLIED_CAST
:
2948 return evaluate_cast(expr
);
2950 return evaluate_sizeof(expr
);
2951 case EXPR_PTRSIZEOF
:
2952 return evaluate_ptrsizeof(expr
);
2954 return evaluate_alignof(expr
);
2956 return evaluate_member_dereference(expr
);
2958 return evaluate_call(expr
);
2960 case EXPR_CONDITIONAL
:
2961 return evaluate_conditional_expression(expr
);
2962 case EXPR_STATEMENT
:
2963 expr
->ctype
= evaluate_statement(expr
->statement
);
2967 expr
->ctype
= &ptr_ctype
;
2971 /* Evaluate the type of the symbol .. */
2972 evaluate_symbol(expr
->symbol
);
2973 /* .. but the type of the _expression_ is a "type" */
2974 expr
->ctype
= &type_ctype
;
2978 return evaluate_offsetof(expr
);
2980 /* These can not exist as stand-alone expressions */
2981 case EXPR_INITIALIZER
:
2982 case EXPR_IDENTIFIER
:
2985 expression_error(expr
, "internal front-end error: initializer in expression");
2988 expression_error(expr
, "internal front-end error: SLICE re-evaluated");
2994 static void check_duplicates(struct symbol
*sym
)
2997 struct symbol
*next
= sym
;
2999 while ((next
= next
->same_symbol
) != NULL
) {
3000 const char *typediff
;
3001 evaluate_symbol(next
);
3003 typediff
= type_difference(&sym
->ctype
, &next
->ctype
, 0, 0);
3005 sparse_error(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
3006 show_ident(sym
->ident
),
3007 stream_name(next
->pos
.stream
), next
->pos
.line
, typediff
);
3012 unsigned long mod
= sym
->ctype
.modifiers
;
3013 if (mod
& (MOD_STATIC
| MOD_REGISTER
))
3015 if (!(mod
& MOD_TOPLEVEL
))
3019 if (sym
->ident
== &main_ident
)
3021 warning(sym
->pos
, "symbol '%s' was not declared. Should it be static?", show_ident(sym
->ident
));
3025 static struct symbol
*evaluate_symbol(struct symbol
*sym
)
3027 struct symbol
*base_type
;
3035 sym
= examine_symbol_type(sym
);
3036 base_type
= get_base_type(sym
);
3040 /* Evaluate the initializers */
3041 if (sym
->initializer
)
3042 evaluate_initializer(sym
, &sym
->initializer
);
3044 /* And finally, evaluate the body of the symbol too */
3045 if (base_type
->type
== SYM_FN
) {
3046 struct symbol
*curr
= current_fn
;
3048 current_fn
= base_type
;
3050 examine_fn_arguments(base_type
);
3051 if (!base_type
->stmt
&& base_type
->inline_stmt
)
3053 if (base_type
->stmt
)
3054 evaluate_statement(base_type
->stmt
);
3062 void evaluate_symbol_list(struct symbol_list
*list
)
3066 FOR_EACH_PTR(list
, sym
) {
3067 evaluate_symbol(sym
);
3068 check_duplicates(sym
);
3069 } END_FOR_EACH_PTR(sym
);
3072 static struct symbol
*evaluate_return_expression(struct statement
*stmt
)
3074 struct expression
*expr
= stmt
->expression
;
3075 struct symbol
*fntype
;
3077 evaluate_expression(expr
);
3078 fntype
= current_fn
->ctype
.base_type
;
3079 if (!fntype
|| fntype
== &void_ctype
) {
3080 if (expr
&& expr
->ctype
!= &void_ctype
)
3081 expression_error(expr
, "return expression in %s function", fntype
?"void":"typeless");
3082 if (expr
&& Wreturn_void
)
3083 warning(stmt
->pos
, "returning void-valued expression");
3088 sparse_error(stmt
->pos
, "return with no return value");
3093 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, "return expression");
3097 static void evaluate_if_statement(struct statement
*stmt
)
3099 if (!stmt
->if_conditional
)
3102 evaluate_conditional(stmt
->if_conditional
, 0);
3103 evaluate_statement(stmt
->if_true
);
3104 evaluate_statement(stmt
->if_false
);
3107 static void evaluate_iterator(struct statement
*stmt
)
3109 evaluate_conditional(stmt
->iterator_pre_condition
, 1);
3110 evaluate_conditional(stmt
->iterator_post_condition
,1);
3111 evaluate_statement(stmt
->iterator_pre_statement
);
3112 evaluate_statement(stmt
->iterator_statement
);
3113 evaluate_statement(stmt
->iterator_post_statement
);
3116 static void verify_output_constraint(struct expression
*expr
, const char *constraint
)
3118 switch (*constraint
) {
3119 case '=': /* Assignment */
3120 case '+': /* Update */
3123 expression_error(expr
, "output constraint is not an assignment constraint (\"%s\")", constraint
);
3127 static void verify_input_constraint(struct expression
*expr
, const char *constraint
)
3129 switch (*constraint
) {
3130 case '=': /* Assignment */
3131 case '+': /* Update */
3132 expression_error(expr
, "input constraint with assignment (\"%s\")", constraint
);
3136 static void evaluate_asm_statement(struct statement
*stmt
)
3138 struct expression
*expr
;
3141 expr
= stmt
->asm_string
;
3142 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3143 sparse_error(stmt
->pos
, "need constant string for inline asm");
3148 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
3149 struct ident
*ident
;
3152 case 0: /* Identifier */
3154 ident
= (struct ident
*)expr
;
3157 case 1: /* Constraint */
3159 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3160 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm output constraint is not a string");
3161 *THIS_ADDRESS(expr
) = NULL
;
3164 verify_output_constraint(expr
, expr
->string
->data
);
3167 case 2: /* Expression */
3169 if (!evaluate_expression(expr
))
3171 if (!lvalue_expression(expr
))
3172 warning(expr
->pos
, "asm output is not an lvalue");
3173 evaluate_assign_to(expr
, expr
->ctype
);
3176 } END_FOR_EACH_PTR(expr
);
3179 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
3180 struct ident
*ident
;
3183 case 0: /* Identifier */
3185 ident
= (struct ident
*)expr
;
3188 case 1: /* Constraint */
3190 if (!expr
|| expr
->type
!= EXPR_STRING
) {
3191 sparse_error(expr
? expr
->pos
: stmt
->pos
, "asm input constraint is not a string");
3192 *THIS_ADDRESS(expr
) = NULL
;
3195 verify_input_constraint(expr
, expr
->string
->data
);
3198 case 2: /* Expression */
3200 if (!evaluate_expression(expr
))
3204 } END_FOR_EACH_PTR(expr
);
3206 FOR_EACH_PTR(stmt
->asm_clobbers
, expr
) {
3208 sparse_error(stmt
->pos
, "bad asm output");
3211 if (expr
->type
== EXPR_STRING
)
3213 expression_error(expr
, "asm clobber is not a string");
3214 } END_FOR_EACH_PTR(expr
);
3217 static void evaluate_case_statement(struct statement
*stmt
)
3219 evaluate_expression(stmt
->case_expression
);
3220 evaluate_expression(stmt
->case_to
);
3221 evaluate_statement(stmt
->case_statement
);
3224 static void check_case_type(struct expression
*switch_expr
,
3225 struct expression
*case_expr
,
3226 struct expression
**enumcase
)
3228 struct symbol
*switch_type
, *case_type
;
3234 switch_type
= switch_expr
->ctype
;
3235 case_type
= evaluate_expression(case_expr
);
3237 if (!switch_type
|| !case_type
)
3241 warn_for_different_enum_types(case_expr
->pos
, case_type
, (*enumcase
)->ctype
);
3242 else if (is_enum_type(case_type
))
3243 *enumcase
= case_expr
;
3246 sclass
= classify_type(switch_type
, &switch_type
);
3247 cclass
= classify_type(case_type
, &case_type
);
3249 /* both should be arithmetic */
3250 if (!(sclass
& cclass
& TYPE_NUM
))
3253 /* neither should be floating */
3254 if ((sclass
| cclass
) & TYPE_FLOAT
)
3257 /* if neither is restricted, we are OK */
3258 if (!((sclass
| cclass
) & TYPE_RESTRICT
))
3261 if (!restricted_binop_type(SPECIAL_EQUAL
, case_expr
, switch_expr
,
3262 cclass
, sclass
, case_type
, switch_type
)) {
3263 unrestrict(case_expr
, cclass
, &case_type
);
3264 unrestrict(switch_expr
, sclass
, &switch_type
);
3269 expression_error(case_expr
, "incompatible types for 'case' statement");
3272 static void evaluate_switch_statement(struct statement
*stmt
)
3275 struct expression
*enumcase
= NULL
;
3276 struct expression
**enumcase_holder
= &enumcase
;
3277 struct expression
*sel
= stmt
->switch_expression
;
3279 evaluate_expression(sel
);
3280 evaluate_statement(stmt
->switch_statement
);
3283 if (sel
->ctype
&& is_enum_type(sel
->ctype
))
3284 enumcase_holder
= NULL
; /* Only check cases against switch */
3286 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
3287 struct statement
*case_stmt
= sym
->stmt
;
3288 check_case_type(sel
, case_stmt
->case_expression
, enumcase_holder
);
3289 check_case_type(sel
, case_stmt
->case_to
, enumcase_holder
);
3290 } END_FOR_EACH_PTR(sym
);
3293 struct symbol
*evaluate_statement(struct statement
*stmt
)
3298 switch (stmt
->type
) {
3299 case STMT_DECLARATION
: {
3301 FOR_EACH_PTR(stmt
->declaration
, s
) {
3303 } END_FOR_EACH_PTR(s
);
3308 return evaluate_return_expression(stmt
);
3310 case STMT_EXPRESSION
:
3311 if (!evaluate_expression(stmt
->expression
))
3313 if (stmt
->expression
->ctype
== &null_ctype
)
3314 stmt
->expression
= cast_to(stmt
->expression
, &ptr_ctype
);
3315 return degenerate(stmt
->expression
);
3317 case STMT_COMPOUND
: {
3318 struct statement
*s
;
3319 struct symbol
*type
= NULL
;
3321 /* Evaluate the return symbol in the compound statement */
3322 evaluate_symbol(stmt
->ret
);
3325 * Then, evaluate each statement, making the type of the
3326 * compound statement be the type of the last statement
3328 type
= evaluate_statement(stmt
->args
);
3329 FOR_EACH_PTR(stmt
->stmts
, s
) {
3330 type
= evaluate_statement(s
);
3331 } END_FOR_EACH_PTR(s
);
3337 evaluate_if_statement(stmt
);
3340 evaluate_iterator(stmt
);
3343 evaluate_switch_statement(stmt
);
3346 evaluate_case_statement(stmt
);
3349 return evaluate_statement(stmt
->label_statement
);
3351 evaluate_expression(stmt
->goto_expression
);
3356 evaluate_asm_statement(stmt
);
3359 evaluate_expression(stmt
->expression
);
3362 evaluate_expression(stmt
->range_expression
);
3363 evaluate_expression(stmt
->range_low
);
3364 evaluate_expression(stmt
->range_high
);