4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
9 * Evaluate constant expressions.
26 #include "expression.h"
28 static struct symbol
*current_fn
;
29 static int current_context
, current_contextmask
;
31 static struct symbol
*degenerate(struct expression
*expr
);
33 static struct symbol
*evaluate_symbol_expression(struct expression
*expr
)
35 struct symbol
*sym
= expr
->symbol
;
36 struct symbol
*base_type
;
40 expr
->ctype
= &int_ctype
;
43 warn(expr
->pos
, "undefined identifier '%s'", show_ident(expr
->symbol_name
));
47 examine_symbol_type(sym
);
48 if ((sym
->ctype
.context
^ current_context
) & (sym
->ctype
.contextmask
& current_contextmask
))
49 warn(expr
->pos
, "Using symbol '%s' in wrong context", show_ident(expr
->symbol_name
));
51 base_type
= sym
->ctype
.base_type
;
53 warn(expr
->pos
, "identifier '%s' has no type", show_ident(expr
->symbol_name
));
57 /* The type of a symbol is the symbol itself! */
60 /* enum's can be turned into plain values */
61 if (sym
->type
!= SYM_ENUM
) {
62 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
64 addr
->symbol_name
= expr
->symbol_name
;
65 addr
->ctype
= NULL
; /* Lazy evaluation: we need to do a proper job if somebody does &sym */
66 expr
->type
= EXPR_PREOP
;
71 expr
->type
= EXPR_VALUE
;
72 expr
->value
= sym
->value
;
73 expr
->ctype
= base_type
;
77 static struct symbol
*evaluate_string(struct expression
*expr
)
79 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
80 struct symbol
*array
= alloc_symbol(expr
->pos
, SYM_ARRAY
);
81 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
82 struct expression
*initstr
= alloc_expression(expr
->pos
, EXPR_STRING
);
83 unsigned int length
= expr
->string
->length
;
85 sym
->array_size
= alloc_const_expression(expr
->pos
, length
);
86 sym
->bit_size
= bits_in_char
* length
;
87 sym
->ctype
.alignment
= 1;
88 sym
->ctype
.modifiers
= MOD_STATIC
;
89 sym
->ctype
.base_type
= array
;
90 sym
->initializer
= initstr
;
93 initstr
->string
= expr
->string
;
95 array
->array_size
= sym
->array_size
;
96 array
->bit_size
= bits_in_char
* length
;
97 array
->ctype
.alignment
= 1;
98 array
->ctype
.modifiers
= MOD_STATIC
;
99 array
->ctype
.base_type
= &char_ctype
;
104 expr
->type
= EXPR_PREOP
;
111 static inline struct symbol
*integer_promotion(struct symbol
*type
)
113 unsigned long mod
= type
->ctype
.modifiers
;
116 if (type
->type
== SYM_ENUM
)
118 else if (type
->type
== SYM_BITFIELD
) {
119 mod
= type
->ctype
.base_type
->ctype
.modifiers
;
120 width
= type
->fieldwidth
;
121 } else if (mod
& (MOD_CHAR
| MOD_SHORT
))
122 width
= type
->bit_size
;
125 if (mod
& MOD_UNSIGNED
&& width
== bits_in_int
)
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 struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
178 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
180 expr
->cast_type
= type
;
181 expr
->cast_expression
= old
;
185 static int is_type_type(struct symbol
*type
)
187 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
190 static int is_ptr_type(struct symbol
*type
)
192 if (type
->type
== SYM_NODE
)
193 type
= type
->ctype
.base_type
;
194 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
197 static inline int is_int_type(struct symbol
*type
)
199 if (type
->type
== SYM_NODE
)
200 type
= type
->ctype
.base_type
;
201 return (type
->type
== SYM_ENUM
) ||
202 (type
->type
== SYM_BITFIELD
) ||
203 type
->ctype
.base_type
== &int_type
;
206 static inline int is_float_type(struct symbol
*type
)
208 if (type
->type
== SYM_NODE
)
209 type
= type
->ctype
.base_type
;
210 return type
->ctype
.base_type
== &fp_type
;
213 static struct symbol
*bad_expr_type(struct expression
*expr
)
215 warn(expr
->pos
, "incompatible types for operation");
219 static struct symbol
*compatible_float_binop(struct expression
**lp
, struct expression
**rp
)
221 struct expression
*left
= *lp
, *right
= *rp
;
222 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
224 if (ltype
->type
== SYM_NODE
)
225 ltype
= ltype
->ctype
.base_type
;
226 if (rtype
->type
== SYM_NODE
)
227 rtype
= rtype
->ctype
.base_type
;
228 if (is_float_type(ltype
)) {
229 if (is_int_type(rtype
))
231 if (is_float_type(rtype
)) {
232 unsigned long lmod
= ltype
->ctype
.modifiers
;
233 unsigned long rmod
= rtype
->ctype
.modifiers
;
234 lmod
&= MOD_LONG
| MOD_LONGLONG
;
235 rmod
&= MOD_LONG
| MOD_LONGLONG
;
245 if (!is_float_type(rtype
) || !is_int_type(ltype
))
248 *lp
= cast_to(left
, rtype
);
251 *rp
= cast_to(right
, ltype
);
255 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
257 struct expression
*left
= *lp
, *right
= *rp
;
258 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
260 if (ltype
->type
== SYM_NODE
)
261 ltype
= ltype
->ctype
.base_type
;
262 if (rtype
->type
== SYM_NODE
)
263 rtype
= rtype
->ctype
.base_type
;
264 if (is_int_type(ltype
) && is_int_type(rtype
)) {
265 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
267 /* Don't bother promoting same-size entities, it only adds clutter */
268 if (ltype
->bit_size
!= ctype
->bit_size
)
269 *lp
= cast_to(left
, ctype
);
270 if (rtype
->bit_size
!= ctype
->bit_size
)
271 *rp
= cast_to(right
, ctype
);
277 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
279 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
280 if (!ctype
&& float_ok
)
281 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
286 return bad_expr_type(expr
);
289 static inline int lvalue_expression(struct expression
*expr
)
291 while (expr
->type
== EXPR_CAST
)
292 expr
= expr
->cast_expression
;
293 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
296 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
298 struct symbol
*ctype
;
299 struct symbol
*ptr_type
= ptr
->ctype
;
302 if (ptr_type
->type
== SYM_NODE
)
303 ptr_type
= ptr_type
->ctype
.base_type
;
305 if (!is_int_type(i
->ctype
))
306 return bad_expr_type(expr
);
309 examine_symbol_type(ctype
);
311 ctype
= degenerate(ptr
);
312 if (!ctype
->ctype
.base_type
) {
313 warn(expr
->pos
, "missing type information");
317 /* Get the size of whatever the pointer points to */
319 if (ptr_type
->type
== SYM_NODE
)
320 ptr_type
= ptr_type
->ctype
.base_type
;
321 if (ptr_type
->type
== SYM_PTR
)
322 ptr_type
= ptr_type
->ctype
.base_type
;
323 bit_size
= ptr_type
->bit_size
;
325 /* Special case: adding zero commonly happens as a result of 'array[0]' */
326 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
328 } else if (bit_size
> bits_in_char
) {
329 struct expression
*add
= expr
;
330 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
331 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
333 val
->ctype
= size_t_ctype
;
334 val
->value
= bit_size
>> 3;
337 mul
->ctype
= size_t_ctype
;
341 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
350 static struct symbol
*evaluate_add(struct expression
*expr
)
352 struct expression
*left
= expr
->left
, *right
= expr
->right
;
353 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
355 if (is_ptr_type(ltype
))
356 return evaluate_ptr_add(expr
, left
, right
);
358 if (is_ptr_type(rtype
))
359 return evaluate_ptr_add(expr
, right
, left
);
361 return evaluate_arith(expr
, 1);
364 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
365 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
367 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
368 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
371 unsigned long mod1
, mod2
, diff
;
372 unsigned long as1
, as2
;
374 struct symbol
*base1
, *base2
;
376 if (target
== source
)
378 if (!target
|| !source
)
379 return "different types";
381 * Peel of per-node information.
382 * FIXME! Check alignment and context too here!
384 mod1
= target
->ctype
.modifiers
;
385 as1
= target
->ctype
.as
;
386 mod2
= source
->ctype
.modifiers
;
387 as2
= source
->ctype
.as
;
388 if (target
->type
== SYM_NODE
) {
389 target
= target
->ctype
.base_type
;
392 if (target
->type
== SYM_PTR
) {
396 mod1
|= target
->ctype
.modifiers
;
397 as1
|= target
->ctype
.as
;
399 if (source
->type
== SYM_NODE
) {
400 source
= source
->ctype
.base_type
;
403 if (source
->type
== SYM_PTR
) {
407 mod2
|= source
->ctype
.modifiers
;
408 as2
|= source
->ctype
.as
;
411 if (target
== source
)
413 if (!target
|| !source
)
414 return "different types";
416 type1
= target
->type
;
417 base1
= target
->ctype
.base_type
;
419 type2
= source
->type
;
420 base2
= source
->ctype
.base_type
;
423 * Pointers to functions compare as the function itself
425 if (type1
== SYM_PTR
&& base1
) {
426 switch (base1
->type
) {
430 base1
= base1
->ctype
.base_type
;
435 if (type2
== SYM_PTR
&& base2
) {
436 switch (base2
->type
) {
440 base2
= base2
->ctype
.base_type
;
446 /* Arrays degenerate to pointers for type comparisons */
447 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
448 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
451 return "different base types";
453 /* Must be same address space to be comparable */
455 return "different address spaces";
457 /* Ignore differences in storage types, sign, or addressability */
458 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
460 mod1
&= diff
& ~target_mod_ignore
;
461 mod2
&= diff
& ~source_mod_ignore
;
463 if ((mod1
| mod2
) & MOD_SIZE
)
464 return "different type sizes";
465 return "different modifiers";
469 if (type1
== SYM_FN
) {
471 struct symbol
*arg1
, *arg2
;
472 if (base1
->variadic
!= base2
->variadic
)
473 return "incompatible variadic arguments";
474 PREPARE_PTR_LIST(target
->arguments
, arg1
);
475 PREPARE_PTR_LIST(source
->arguments
, arg2
);
479 diff
= type_difference(arg1
, arg2
, 0, 0);
481 static char argdiff
[80];
482 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
491 FINISH_PTR_LIST(arg2
);
492 FINISH_PTR_LIST(arg1
);
501 static int is_null_ptr(struct expression
*expr
)
503 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
505 if (!is_ptr_type(expr
->ctype
))
506 warn(expr
->pos
, "Using plain integer as NULL pointer");
510 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
512 /* NULL expression? Just return the type of the "other side" */
521 * Ignore differences in "volatile" and "const"ness when
522 * subtracting pointers
524 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
526 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
528 const char *typediff
;
529 struct symbol
*ctype
;
530 struct symbol
*ltype
, *rtype
;
532 ltype
= degenerate(l
);
533 rtype
= degenerate(r
);
536 * If it is an integer subtract: the ptr add case will do the
539 if (!is_ptr_type(rtype
))
540 return evaluate_ptr_add(expr
, l
, r
);
543 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
545 ctype
= common_ptr_type(l
, r
);
547 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
551 examine_symbol_type(ctype
);
553 /* Figure out the base type we point to */
554 if (ctype
->type
== SYM_NODE
)
555 ctype
= ctype
->ctype
.base_type
;
556 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
557 warn(expr
->pos
, "subtraction of functions? Share your drugs");
560 ctype
= ctype
->ctype
.base_type
;
562 expr
->ctype
= ssize_t_ctype
;
563 if (ctype
->bit_size
> bits_in_char
) {
564 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
565 struct expression
*div
= expr
;
566 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
568 val
->ctype
= size_t_ctype
;
569 val
->value
= ctype
->bit_size
>> 3;
572 sub
->ctype
= ssize_t_ctype
;
581 return ssize_t_ctype
;
584 static struct symbol
*evaluate_sub(struct expression
*expr
)
586 struct expression
*left
= expr
->left
, *right
= expr
->right
;
587 struct symbol
*ltype
= left
->ctype
;
589 if (is_ptr_type(ltype
))
590 return evaluate_ptr_sub(expr
, left
, right
);
592 return evaluate_arith(expr
, 1);
595 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
597 static struct symbol
*evaluate_conditional(struct expression
**p
)
599 struct symbol
*ctype
;
600 struct expression
*expr
= *p
;
605 if (expr
->type
== EXPR_ASSIGNMENT
)
606 warn(expr
->pos
, "assignment expression in conditional");
608 ctype
= evaluate_expression(expr
);
610 if (is_safe_type(ctype
))
611 warn(expr
->pos
, "testing a 'safe expression'");
612 if (is_float_type(ctype
)) {
613 struct expression
*comp
;
615 * It's easier to handle here, rather than deal with
616 * FP all over the place. Floating point in boolean
617 * context is rare enough (and very often wrong),
618 * so price of explicit comparison with appropriate
619 * FP zero is not too high. And it simplifies things
622 comp
= alloc_expression(expr
->pos
, EXPR_BINOP
);
623 comp
->op
= SPECIAL_NOTEQUAL
;
625 comp
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
626 comp
->right
->ctype
= comp
->left
->ctype
;
627 comp
->right
->fvalue
= 0;
628 ctype
= comp
->ctype
= &bool_ctype
;
636 static struct symbol
*evaluate_logical(struct expression
*expr
)
638 if (!evaluate_conditional(&expr
->left
))
640 if (!evaluate_conditional(&expr
->right
))
643 expr
->ctype
= &bool_ctype
;
647 static struct symbol
*evaluate_shift(struct expression
*expr
)
649 struct expression
*left
= expr
->left
, *right
= expr
->right
;
650 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
652 if (ltype
->type
== SYM_NODE
)
653 ltype
= ltype
->ctype
.base_type
;
654 if (rtype
->type
== SYM_NODE
)
655 rtype
= rtype
->ctype
.base_type
;
656 if (is_int_type(ltype
) && is_int_type(rtype
)) {
657 struct symbol
*ctype
= integer_promotion(ltype
);
658 if (ltype
->bit_size
!= ctype
->bit_size
)
659 expr
->left
= cast_to(expr
->left
, ctype
);
661 ctype
= integer_promotion(rtype
);
662 if (rtype
->bit_size
!= ctype
->bit_size
)
663 expr
->right
= cast_to(expr
->right
, ctype
);
666 return bad_expr_type(expr
);
669 static struct symbol
*evaluate_binop(struct expression
*expr
)
672 // addition can take ptr+int, fp and int
674 return evaluate_add(expr
);
676 // subtraction can take ptr-ptr, fp and int
678 return evaluate_sub(expr
);
680 // Arithmetic operations can take fp and int
682 return evaluate_arith(expr
, 1);
684 // shifts do integer promotions, but that's it.
685 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
686 return evaluate_shift(expr
);
688 // The rest are integer operations
689 // '%', '&', '^', '|'
691 return evaluate_arith(expr
, 0);
695 static struct symbol
*evaluate_comma(struct expression
*expr
)
697 expr
->ctype
= expr
->right
->ctype
;
701 static int modify_for_unsigned(int op
)
704 op
= SPECIAL_UNSIGNED_LT
;
706 op
= SPECIAL_UNSIGNED_GT
;
707 else if (op
== SPECIAL_LTE
)
708 op
= SPECIAL_UNSIGNED_LTE
;
709 else if (op
== SPECIAL_GTE
)
710 op
= SPECIAL_UNSIGNED_GTE
;
714 static struct symbol
*evaluate_compare(struct expression
*expr
)
716 struct expression
*left
= expr
->left
, *right
= expr
->right
;
717 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
718 struct symbol
*ctype
;
721 if (is_type_type(ltype
) && is_type_type(rtype
)) {
722 expr
->ctype
= &bool_ctype
;
726 if (is_safe_type(ltype
) || is_safe_type(rtype
))
727 warn(expr
->pos
, "testing a 'safe expression'");
730 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
731 expr
->ctype
= &bool_ctype
;
732 // FIXME! Check the types for compatibility
736 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
738 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
739 expr
->op
= modify_for_unsigned(expr
->op
);
740 expr
->ctype
= &bool_ctype
;
743 ctype
= compatible_float_binop(&expr
->left
, &expr
->right
);
745 return bad_expr_type(expr
);
749 * FIXME!! This should do casts, array degeneration etc..
751 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
753 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
755 if (ltype
->type
== SYM_NODE
)
756 ltype
= ltype
->ctype
.base_type
;
758 if (rtype
->type
== SYM_NODE
)
759 rtype
= rtype
->ctype
.base_type
;
761 if (ltype
->type
== SYM_PTR
) {
762 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
766 if (rtype
->type
== SYM_PTR
) {
767 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
773 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
775 struct expression
*cond
, *true, *false;
776 struct symbol
*ctype
, *ltype
, *rtype
;
777 const char * typediff
;
779 ctype
= degenerate(expr
->conditional
);
780 cond
= expr
->conditional
;
784 if (expr
->cond_true
) {
785 ltype
= degenerate(expr
->cond_true
);
786 true = expr
->cond_true
;
789 rtype
= degenerate(expr
->cond_false
);
790 false = expr
->cond_false
;
793 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
797 ctype
= compatible_integer_binop(&true, &expr
->cond_false
);
800 ctype
= compatible_ptr_type(true, expr
->cond_false
);
803 ctype
= compatible_float_binop(&true, &expr
->cond_false
);
806 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
814 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
815 struct expression
**rp
, struct symbol
*source
, const char *where
)
817 const char *typediff
;
821 /* It's ok if the target is more volatile or const than the source */
822 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
826 if (is_int_type(target
)) {
827 if (is_int_type(source
)) {
828 if (target
->bit_size
!= source
->bit_size
)
832 if (is_float_type(source
))
834 } else if (is_float_type(target
)) {
835 if (is_int_type(source
))
837 if (is_float_type(source
)) {
838 if (target
->bit_size
!= source
->bit_size
)
844 /* Pointer destination? */
846 target_as
= t
->ctype
.as
;
847 if (t
->type
== SYM_NODE
) {
848 t
= t
->ctype
.base_type
;
849 target_as
|= t
->ctype
.as
;
851 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
852 struct expression
*right
= *rp
;
853 struct symbol
*s
= source
;
856 // NULL pointer is always ok
857 if (is_null_ptr(right
))
860 /* "void *" matches anything as long as the address space is ok */
861 source_as
= s
->ctype
.as
;
862 if (s
->type
== SYM_NODE
) {
863 s
= s
->ctype
.base_type
;
864 source_as
|= s
->ctype
.as
;
866 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
867 s
= s
->ctype
.base_type
;
868 t
= t
->ctype
.base_type
;
869 if (s
== &void_ctype
|| t
== &void_ctype
)
875 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
876 info(expr
->pos
, " expected %s", show_typename(target
));
877 info(expr
->pos
, " got %s", show_typename(source
));
880 *rp
= cast_to(*rp
, target
);
885 * FIXME!! This is wrong from a double evaluation standpoint. We can't
886 * just expand the expression twice, that would make any side effects
889 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
892 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
893 static const int op_trans
[] = {
894 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
895 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
896 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
897 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
898 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
899 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
900 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
901 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
902 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
903 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
906 subexpr
->left
= left
;
907 subexpr
->right
= right
;
908 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
910 expr
->right
= subexpr
;
911 return evaluate_binop(subexpr
);
914 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
916 if (type
->ctype
.modifiers
& MOD_CONST
)
917 warn(left
->pos
, "assignment to const expression");
918 if (type
->type
== SYM_NODE
)
919 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
922 static struct symbol
*evaluate_assignment(struct expression
*expr
)
924 struct expression
*left
= expr
->left
, *right
= expr
->right
;
925 struct symbol
*ltype
, *rtype
;
928 rtype
= right
->ctype
;
929 if (expr
->op
!= '=') {
930 rtype
= evaluate_binop_assignment(expr
, left
, right
);
936 if (!lvalue_expression(left
)) {
937 warn(expr
->pos
, "not an lvalue");
941 rtype
= degenerate(right
);
943 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
946 evaluate_assign_to(left
, ltype
);
952 static void examine_fn_arguments(struct symbol
*fn
)
956 FOR_EACH_PTR(fn
->arguments
, s
) {
957 struct symbol
*arg
= evaluate_symbol(s
);
958 /* Array/function arguments silently degenerate into pointers */
964 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
965 if (arg
->type
== SYM_ARRAY
)
966 ptr
->ctype
= arg
->ctype
;
968 ptr
->ctype
.base_type
= arg
;
969 ptr
->ctype
.as
|= s
->ctype
.as
;
970 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
972 s
->ctype
.base_type
= ptr
;
974 s
->ctype
.modifiers
= 0;
975 examine_symbol_type(s
);
985 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
987 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
988 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
990 newsym
->ctype
.as
= as
;
991 newsym
->ctype
.modifiers
= mod
;
997 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
999 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1000 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
1002 node
->ctype
.base_type
= ptr
;
1003 ptr
->bit_size
= bits_in_pointer
;
1004 ptr
->ctype
.alignment
= pointer_alignment
;
1006 node
->bit_size
= bits_in_pointer
;
1007 node
->ctype
.alignment
= pointer_alignment
;
1009 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
1010 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
1011 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
1012 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
1014 if (sym
->type
== SYM_NODE
) {
1015 ptr
->ctype
.as
|= sym
->ctype
.as
;
1016 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1017 sym
= sym
->ctype
.base_type
;
1019 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
1020 ptr
->ctype
.as
|= sym
->ctype
.as
;
1021 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
1022 sym
= sym
->ctype
.base_type
;
1024 ptr
->ctype
.base_type
= sym
;
1029 /* Arrays degenerate into pointers on pointer arithmetic */
1030 static struct symbol
*degenerate(struct expression
*expr
)
1032 struct symbol
*ctype
, *base
;
1036 ctype
= expr
->ctype
;
1040 if (ctype
->type
== SYM_NODE
)
1041 base
= ctype
->ctype
.base_type
;
1043 * Arrays degenerate into pointers to the entries, while
1044 * functions degenerate into pointers to themselves
1046 switch (base
->type
) {
1049 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
1050 warn(expr
->pos
, "strange non-value function or array");
1053 *expr
= *expr
->unop
;
1054 ctype
= create_pointer(expr
, ctype
, 1);
1055 expr
->ctype
= ctype
;
1062 static struct symbol
*evaluate_addressof(struct expression
*expr
)
1064 struct expression
*op
= expr
->unop
;
1065 struct symbol
*ctype
;
1067 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
1068 warn(expr
->pos
, "not addressable");
1075 * symbol expression evaluation is lazy about the type
1076 * of the sub-expression, so we may have to generate
1077 * the type here if so..
1080 ctype
= create_pointer(expr
, ctype
, 0);
1081 expr
->ctype
= ctype
;
1087 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1089 struct expression
*op
= expr
->unop
;
1090 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1092 /* Simplify: *&(expr) => (expr) */
1093 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1098 /* Dereferencing a node drops all the node information. */
1099 if (ctype
->type
== SYM_NODE
)
1100 ctype
= ctype
->ctype
.base_type
;
1102 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1103 target
= ctype
->ctype
.base_type
;
1105 switch (ctype
->type
) {
1107 warn(expr
->pos
, "cannot derefence this type");
1110 merge_type(node
, ctype
);
1111 if (ctype
->type
!= SYM_ARRAY
)
1114 * Dereferencing a pointer to an array results in a
1115 * degenerate dereference: the expression becomes
1116 * just a pointer to the entry, and the derefence
1121 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1122 target
->bit_size
= bits_in_pointer
;
1123 target
->ctype
.alignment
= pointer_alignment
;
1124 merge_type(target
, ctype
->ctype
.base_type
);
1129 * When an array is dereferenced, we need to pick
1130 * up the attributes of the original node too..
1132 merge_type(node
, op
->ctype
);
1133 merge_type(node
, ctype
);
1137 node
->bit_size
= target
->bit_size
;
1138 node
->array_size
= target
->array_size
;
1145 * Unary post-ops: x++ and x--
1147 static struct symbol
*evaluate_postop(struct expression
*expr
)
1149 struct expression
*op
= expr
->unop
;
1150 struct symbol
*ctype
= op
->ctype
;
1152 if (!lvalue_expression(expr
->unop
)) {
1153 warn(expr
->pos
, "need lvalue expression for ++/--");
1157 evaluate_assign_to(op
, ctype
);
1159 expr
->ctype
= ctype
;
1163 static struct symbol
*evaluate_sign(struct expression
*expr
)
1165 struct symbol
*ctype
= expr
->unop
->ctype
;
1166 if (is_int_type(ctype
)) {
1167 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1168 if (rtype
->bit_size
!= ctype
->bit_size
)
1169 expr
->unop
= cast_to(expr
->unop
, rtype
);
1171 } else if (is_float_type(ctype
) && expr
->op
!= '%') {
1172 /* no conversions needed */
1174 return bad_expr_type(expr
);
1176 if (expr
->op
== '+')
1177 *expr
= *expr
->unop
;
1178 expr
->ctype
= ctype
;
1182 static struct symbol
*evaluate_preop(struct expression
*expr
)
1184 struct symbol
*ctype
= expr
->unop
->ctype
;
1188 *expr
= *expr
->unop
;
1194 return evaluate_sign(expr
);
1197 return evaluate_dereference(expr
);
1200 return evaluate_addressof(expr
);
1202 case SPECIAL_INCREMENT
:
1203 case SPECIAL_DECREMENT
:
1205 * From a type evaluation standpoint the pre-ops are
1206 * the same as the postops
1208 return evaluate_postop(expr
);
1211 if (is_safe_type(ctype
))
1212 warn(expr
->pos
, "testing a 'safe expression'");
1213 if (is_float_type(ctype
)) {
1214 struct expression
*arg
= expr
->unop
;
1215 expr
->type
= EXPR_BINOP
;
1216 expr
->op
= SPECIAL_EQUAL
;
1218 expr
->right
= alloc_expression(expr
->pos
, EXPR_FVALUE
);
1219 expr
->right
->ctype
= ctype
;
1220 expr
->right
->fvalue
= 0;
1222 ctype
= &bool_ctype
;
1228 expr
->ctype
= ctype
;
1232 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1234 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1235 struct ptr_list
*list
= head
;
1241 for (i
= 0; i
< list
->nr
; i
++) {
1242 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1244 if (sym
->ident
!= ident
)
1246 *offset
= sym
->offset
;
1249 struct symbol
*ctype
= sym
->ctype
.base_type
;
1253 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1255 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1258 *offset
+= sym
->offset
;
1262 } while ((list
= list
->next
) != head
);
1266 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1268 struct expression
*add
;
1272 /* Create a new add-expression */
1273 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1276 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1277 add
->right
->ctype
= &int_ctype
;
1278 add
->right
->value
= offset
;
1282 * The ctype of the pointer will be lazily evaluated if
1283 * we ever take the address of this member dereference..
1289 /* structure/union dereference */
1290 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1293 struct symbol
*ctype
, *member
;
1294 struct expression
*deref
= expr
->deref
, *add
;
1295 struct ident
*ident
= expr
->member
;
1299 if (!evaluate_expression(deref
))
1302 warn(expr
->pos
, "bad member name");
1306 ctype
= deref
->ctype
;
1307 address_space
= ctype
->ctype
.as
;
1308 mod
= ctype
->ctype
.modifiers
;
1309 if (ctype
->type
== SYM_NODE
) {
1310 ctype
= ctype
->ctype
.base_type
;
1311 address_space
|= ctype
->ctype
.as
;
1312 mod
|= ctype
->ctype
.modifiers
;
1314 if (!lvalue_expression(deref
)) {
1315 warn(deref
->pos
, "expected lvalue for member dereference");
1318 deref
= deref
->unop
;
1319 expr
->deref
= deref
;
1320 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1321 warn(expr
->pos
, "expected structure or union");
1325 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1327 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1328 const char *name
= "<unnamed>";
1331 name
= ctype
->ident
->name
;
1332 namelen
= ctype
->ident
->len
;
1334 warn(expr
->pos
, "no member '%s' in %s %.*s",
1335 show_ident(ident
), type
, namelen
, name
);
1340 * The member needs to take on the address space and modifiers of
1341 * the "parent" type.
1343 member
= convert_to_as_mod(member
, address_space
, mod
);
1344 add
= evaluate_offset(deref
, offset
);
1346 ctype
= member
->ctype
.base_type
;
1347 if (ctype
->type
== SYM_BITFIELD
) {
1348 expr
->type
= EXPR_BITFIELD
;
1349 expr
->bitpos
= member
->bit_offset
;
1350 expr
->nrbits
= member
->fieldwidth
;
1351 expr
->address
= add
;
1353 expr
->type
= EXPR_PREOP
;
1358 expr
->ctype
= member
;
1362 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1366 if (expr
->cast_type
) {
1367 examine_symbol_type(expr
->cast_type
);
1368 size
= expr
->cast_type
->bit_size
;
1370 if (!evaluate_expression(expr
->cast_expression
))
1372 size
= expr
->cast_expression
->ctype
->bit_size
;
1375 warn(expr
->pos
, "cannot size expression");
1378 expr
->type
= EXPR_VALUE
;
1379 expr
->value
= size
>> 3;
1380 expr
->ctype
= size_t_ctype
;
1381 return size_t_ctype
;
1384 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1386 struct symbol
*type
= expr
->cast_type
;
1389 type
= evaluate_expression(expr
->cast_expression
);
1393 examine_symbol_type(type
);
1394 expr
->type
= EXPR_VALUE
;
1395 expr
->value
= type
->ctype
.alignment
;
1396 expr
->ctype
= size_t_ctype
;
1397 return size_t_ctype
;
1400 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1402 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1403 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1407 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1409 struct expression
*expr
;
1410 struct symbol_list
*argument_types
= fn
->arguments
;
1411 struct symbol
*argtype
;
1414 PREPARE_PTR_LIST(argument_types
, argtype
);
1415 FOR_EACH_PTR (head
, expr
) {
1416 struct expression
**p
= THIS_ADDRESS(expr
);
1417 struct symbol
*ctype
, *target
;
1418 ctype
= evaluate_expression(expr
);
1423 if (context_clash(f
, ctype
))
1424 warn(expr
->pos
, "argument %d used in wrong context", i
);
1426 ctype
= degenerate(expr
);
1429 if (!target
&& ctype
->bit_size
< bits_in_int
)
1430 target
= &int_ctype
;
1432 static char where
[30];
1433 examine_symbol_type(target
);
1434 sprintf(where
, "argument %d", i
);
1435 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1439 NEXT_PTR_LIST(argtype
);
1441 FINISH_PTR_LIST(argtype
);
1445 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1446 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1448 struct expression
*entry
;
1452 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1453 struct expression
**p
= THIS_ADDRESS(entry
);
1455 if (entry
->type
== EXPR_INDEX
) {
1456 current
= entry
->idx_to
;
1459 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1467 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1468 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1470 if (offset
|| expression_list_size(expr
->expr_list
) != 1) {
1471 warn(expr
->pos
, "unexpected compound initializer");
1474 return evaluate_array_initializer(ctype
, expr
, 0);
1477 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1479 struct expression
*entry
;
1482 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1483 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1484 struct expression
**p
= THIS_ADDRESS(entry
);
1486 if (entry
->type
== EXPR_IDENTIFIER
) {
1487 struct ident
*ident
= entry
->expr_ident
;
1488 /* We special-case the "already right place" case */
1489 if (sym
&& sym
->ident
== ident
)
1491 RESET_PTR_LIST(sym
);
1494 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1497 if (sym
->ident
== ident
)
1505 warn(expr
->pos
, "too many initializers for struct/union");
1509 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1513 FINISH_PTR_LIST(sym
);
1519 * Initializers are kind of like assignments. Except
1520 * they can be a hell of a lot more complex.
1522 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1524 struct expression
*expr
= *ep
;
1527 * Simple non-structure/array initializers are the simple
1528 * case, and look (and parse) largely like assignments.
1530 if (expr
->type
!= EXPR_INITIALIZER
) {
1532 struct symbol
*rtype
= evaluate_expression(expr
);
1534 struct expression
*pos
;
1536 // FIXME! char array[] = "string" special case
1537 // should _not_ degenerate.
1538 rtype
= degenerate(expr
);
1539 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1540 /* strings are special: char arrays */
1541 if (rtype
->type
== SYM_ARRAY
)
1542 size
= get_expression_value(rtype
->array_size
);
1544 * Don't bother creating a position expression for
1545 * the simple initializer cases that don't need it.
1547 * We need a position if the initializer has a byte
1548 * offset, _or_ if we're initializing a bitfield.
1550 if (offset
|| ctype
->fieldwidth
) {
1551 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1552 pos
->init_offset
= offset
;
1553 pos
->init_sym
= ctype
;
1554 pos
->init_expr
= *ep
;
1555 pos
->ctype
= expr
->ctype
;
1562 expr
->ctype
= ctype
;
1563 if (ctype
->type
== SYM_NODE
)
1564 ctype
= ctype
->ctype
.base_type
;
1566 switch (ctype
->type
) {
1569 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1571 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1573 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1575 return evaluate_scalar_initializer(ctype
, expr
, offset
);
1579 static int get_as(struct symbol
*sym
)
1587 mod
= sym
->ctype
.modifiers
;
1588 if (sym
->type
== SYM_NODE
) {
1589 sym
= sym
->ctype
.base_type
;
1590 as
|= sym
->ctype
.as
;
1591 mod
|= sym
->ctype
.modifiers
;
1594 * You can always throw a value away by casting to
1595 * "void" - that's an implicit "force". Note that
1596 * the same is _not_ true of "void *".
1598 if (sym
== &void_ctype
)
1602 * At least for now, allow casting to a "unsigned long".
1603 * That's how we do things like pointer arithmetic and
1604 * store pointers to registers.
1606 if (sym
== &ulong_ctype
)
1609 if (sym
&& sym
->type
== SYM_PTR
) {
1610 sym
= sym
->ctype
.base_type
;
1611 as
|= sym
->ctype
.as
;
1612 mod
|= sym
->ctype
.modifiers
;
1614 if (mod
& MOD_FORCE
)
1619 static struct symbol
*evaluate_cast(struct expression
*expr
)
1621 struct expression
*target
= expr
->cast_expression
;
1622 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1624 expr
->ctype
= ctype
;
1625 expr
->cast_type
= ctype
;
1628 * Special case: a cast can be followed by an
1629 * initializer, in which case we need to pass
1630 * the type value down to that initializer rather
1631 * than trying to evaluate it as an expression
1633 * A more complex case is when the initializer is
1634 * dereferenced as part of a post-fix expression.
1635 * We need to produce an expression that can be dereferenced.
1637 if (target
->type
== EXPR_INITIALIZER
) {
1638 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1639 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1641 sym
->ctype
.base_type
= ctype
;
1642 sym
->initializer
= expr
->cast_expression
;
1643 evaluate_symbol(sym
);
1645 addr
->ctype
= NULL
; /* Lazy eval */
1648 expr
->type
= EXPR_PREOP
;
1651 expr
->ctype
= ctype
;
1655 evaluate_expression(target
);
1658 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
1659 warn(expr
->pos
, "cast removes address space of expression");
1662 * Casts of constant values are special: they
1663 * can be NULL, and thus need to be simplified
1666 if (target
->type
== EXPR_VALUE
)
1667 cast_value(expr
, ctype
, target
, target
->ctype
);
1673 * Evaluate a call expression with a symbol. This
1674 * should expand inline functions, and evaluate
1677 static int evaluate_symbol_call(struct expression
*expr
)
1679 struct expression
*fn
= expr
->fn
;
1680 struct symbol
*ctype
= fn
->ctype
;
1682 if (fn
->type
!= EXPR_PREOP
)
1685 if (ctype
->op
&& ctype
->op
->evaluate
)
1686 return ctype
->op
->evaluate(expr
);
1688 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1690 struct symbol
*curr
= current_fn
;
1691 unsigned long context
= current_context
;
1692 unsigned long mask
= current_contextmask
;
1694 current_context
|= ctype
->ctype
.context
;
1695 current_contextmask
|= ctype
->ctype
.contextmask
;
1696 current_fn
= ctype
->ctype
.base_type
;
1697 examine_fn_arguments(current_fn
);
1699 ret
= inline_function(expr
, ctype
);
1701 /* restore the old function context */
1703 current_context
= context
;
1704 current_contextmask
= mask
;
1711 static struct symbol
*evaluate_call(struct expression
*expr
)
1714 struct symbol
*ctype
, *sym
;
1715 struct expression
*fn
= expr
->fn
;
1716 struct expression_list
*arglist
= expr
->args
;
1718 if (!evaluate_expression(fn
))
1720 sym
= ctype
= fn
->ctype
;
1721 if (ctype
->type
== SYM_NODE
)
1722 ctype
= ctype
->ctype
.base_type
;
1723 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1724 ctype
= ctype
->ctype
.base_type
;
1725 if (!evaluate_arguments(sym
, ctype
, arglist
))
1727 if (ctype
->type
!= SYM_FN
) {
1728 warn(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
1731 args
= expression_list_size(expr
->args
);
1732 fnargs
= symbol_list_size(ctype
->arguments
);
1734 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1735 if (args
> fnargs
&& !ctype
->variadic
)
1736 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1737 if (sym
->type
== SYM_NODE
) {
1738 if (evaluate_symbol_call(expr
))
1741 expr
->ctype
= ctype
->ctype
.base_type
;
1745 struct symbol
*evaluate_expression(struct expression
*expr
)
1752 switch (expr
->type
) {
1755 warn(expr
->pos
, "value expression without a type");
1758 return evaluate_string(expr
);
1760 return evaluate_symbol_expression(expr
);
1762 if (!evaluate_expression(expr
->left
))
1764 if (!evaluate_expression(expr
->right
))
1766 return evaluate_binop(expr
);
1768 return evaluate_logical(expr
);
1770 if (!evaluate_expression(expr
->left
))
1772 if (!evaluate_expression(expr
->right
))
1774 return evaluate_comma(expr
);
1776 if (!evaluate_expression(expr
->left
))
1778 if (!evaluate_expression(expr
->right
))
1780 return evaluate_compare(expr
);
1781 case EXPR_ASSIGNMENT
:
1782 if (!evaluate_expression(expr
->left
))
1784 if (!evaluate_expression(expr
->right
))
1786 return evaluate_assignment(expr
);
1788 if (!evaluate_expression(expr
->unop
))
1790 return evaluate_preop(expr
);
1792 if (!evaluate_expression(expr
->unop
))
1794 return evaluate_postop(expr
);
1796 return evaluate_cast(expr
);
1798 return evaluate_sizeof(expr
);
1800 return evaluate_alignof(expr
);
1802 return evaluate_member_dereference(expr
);
1804 return evaluate_call(expr
);
1806 warn(expr
->pos
, "bitfield generated by parser");
1808 case EXPR_CONDITIONAL
:
1809 if (!evaluate_conditional(&expr
->conditional
))
1811 if (!evaluate_expression(expr
->cond_false
))
1813 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1815 return evaluate_conditional_expression(expr
);
1816 case EXPR_STATEMENT
:
1817 expr
->ctype
= evaluate_statement(expr
->statement
);
1821 expr
->ctype
= &ptr_ctype
;
1825 /* Evaluate the type of the symbol .. */
1826 evaluate_symbol(expr
->symbol
);
1827 /* .. but the type of the _expression_ is a "type" */
1828 expr
->ctype
= &type_ctype
;
1831 /* These can not exist as stand-alone expressions */
1832 case EXPR_INITIALIZER
:
1833 case EXPR_IDENTIFIER
:
1836 warn(expr
->pos
, "internal front-end error: initializer in expression");
1842 void check_duplicates(struct symbol
*sym
)
1844 struct symbol
*next
= sym
;
1846 while ((next
= next
->same_symbol
) != NULL
) {
1847 const char *typediff
;
1848 evaluate_symbol(next
);
1849 typediff
= type_difference(sym
, next
, 0, 0);
1851 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1852 show_ident(sym
->ident
),
1853 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1859 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1861 struct symbol
*base_type
;
1866 sym
= examine_symbol_type(sym
);
1867 base_type
= sym
->ctype
.base_type
;
1871 /* Evaluate the initializers */
1872 if (sym
->initializer
) {
1873 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1874 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
1875 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1876 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
1877 base_type
->bit_size
= bit_size
;
1878 sym
->array_size
= base_type
->array_size
;
1879 sym
->bit_size
= bit_size
;
1883 /* And finally, evaluate the body of the symbol too */
1884 if (base_type
->type
== SYM_FN
) {
1885 examine_fn_arguments(base_type
);
1886 if (base_type
->stmt
) {
1887 current_fn
= base_type
;
1888 current_contextmask
= sym
->ctype
.contextmask
;
1889 current_context
= sym
->ctype
.context
;
1890 evaluate_statement(base_type
->stmt
);
1897 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1899 struct expression
*expr
= stmt
->expression
;
1900 struct symbol
*ctype
, *fntype
;
1902 evaluate_expression(expr
);
1903 ctype
= degenerate(expr
);
1904 fntype
= current_fn
->ctype
.base_type
;
1905 if (!fntype
|| fntype
== &void_ctype
) {
1906 if (expr
&& ctype
!= &void_ctype
)
1907 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1912 warn(stmt
->pos
, "return with no return value");
1917 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
1921 static void evaluate_if_statement(struct statement
*stmt
)
1923 struct symbol
*ctype
;
1925 if (!stmt
->if_conditional
)
1928 ctype
= evaluate_conditional(&stmt
->if_conditional
);
1932 evaluate_statement(stmt
->if_true
);
1933 evaluate_statement(stmt
->if_false
);
1936 struct symbol
*evaluate_statement(struct statement
*stmt
)
1941 switch (stmt
->type
) {
1943 return evaluate_return_expression(stmt
);
1945 case STMT_EXPRESSION
:
1946 evaluate_expression(stmt
->expression
);
1947 return degenerate(stmt
->expression
);
1949 case STMT_COMPOUND
: {
1950 struct statement
*s
;
1951 struct symbol
*type
= NULL
;
1954 /* Evaluate each symbol in the compound statement */
1955 FOR_EACH_PTR(stmt
->syms
, sym
) {
1956 evaluate_symbol(sym
);
1958 evaluate_symbol(stmt
->ret
);
1961 * Then, evaluate each statement, making the type of the
1962 * compound statement be the type of the last statement
1965 FOR_EACH_PTR(stmt
->stmts
, s
) {
1966 type
= evaluate_statement(s
);
1971 evaluate_if_statement(stmt
);
1974 evaluate_conditional(&stmt
->iterator_pre_condition
);
1975 evaluate_conditional(&stmt
->iterator_post_condition
);
1976 evaluate_statement(stmt
->iterator_pre_statement
);
1977 evaluate_statement(stmt
->iterator_statement
);
1978 evaluate_statement(stmt
->iterator_post_statement
);
1981 evaluate_expression(stmt
->switch_expression
);
1982 evaluate_statement(stmt
->switch_statement
);
1985 evaluate_expression(stmt
->case_expression
);
1986 evaluate_expression(stmt
->case_to
);
1987 evaluate_statement(stmt
->case_statement
);
1990 evaluate_statement(stmt
->label_statement
);
1993 evaluate_expression(stmt
->goto_expression
);
1998 /* FIXME! Do the asm parameter evaluation! */