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 struct symbol
*bad_expr_type(struct expression
*expr
)
208 warn(expr
->pos
, "incompatible types for operation");
212 static struct symbol
*compatible_integer_binop(struct expression
**lp
, struct expression
**rp
)
214 struct expression
*left
= *lp
, *right
= *rp
;
215 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
217 if (ltype
->type
== SYM_NODE
)
218 ltype
= ltype
->ctype
.base_type
;
219 if (rtype
->type
== SYM_NODE
)
220 rtype
= rtype
->ctype
.base_type
;
221 if (is_int_type(ltype
) && is_int_type(rtype
)) {
222 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
224 /* Don't bother promoting same-size entities, it only adds clutter */
225 if (ltype
->bit_size
!= ctype
->bit_size
)
226 *lp
= cast_to(left
, ctype
);
227 if (rtype
->bit_size
!= ctype
->bit_size
)
228 *rp
= cast_to(right
, ctype
);
234 static struct symbol
*evaluate_arith(struct expression
*expr
, int float_ok
)
236 struct symbol
*ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
241 return bad_expr_type(expr
);
244 static inline int lvalue_expression(struct expression
*expr
)
246 while (expr
->type
== EXPR_CAST
)
247 expr
= expr
->cast_expression
;
248 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
251 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
253 struct symbol
*ctype
;
254 struct symbol
*ptr_type
= ptr
->ctype
;
257 if (ptr_type
->type
== SYM_NODE
)
258 ptr_type
= ptr_type
->ctype
.base_type
;
260 if (!is_int_type(i
->ctype
))
261 return bad_expr_type(expr
);
264 examine_symbol_type(ctype
);
266 ctype
= degenerate(ptr
);
267 if (!ctype
->ctype
.base_type
) {
268 warn(expr
->pos
, "missing type information");
272 /* Get the size of whatever the pointer points to */
274 if (ptr_type
->type
== SYM_NODE
)
275 ptr_type
= ptr_type
->ctype
.base_type
;
276 if (ptr_type
->type
== SYM_PTR
)
277 ptr_type
= ptr_type
->ctype
.base_type
;
278 bit_size
= ptr_type
->bit_size
;
280 /* Special case: adding zero commonly happens as a result of 'array[0]' */
281 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
283 } else if (bit_size
> bits_in_char
) {
284 struct expression
*add
= expr
;
285 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
286 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
288 val
->ctype
= size_t_ctype
;
289 val
->value
= bit_size
>> 3;
292 mul
->ctype
= size_t_ctype
;
296 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
305 static struct symbol
*evaluate_add(struct expression
*expr
)
307 struct expression
*left
= expr
->left
, *right
= expr
->right
;
308 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
310 if (is_ptr_type(ltype
))
311 return evaluate_ptr_add(expr
, left
, right
);
313 if (is_ptr_type(rtype
))
314 return evaluate_ptr_add(expr
, right
, left
);
316 // FIXME! FP promotion
317 return evaluate_arith(expr
, 1);
320 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
321 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
323 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
324 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
327 unsigned long mod1
, mod2
, diff
;
328 unsigned long as1
, as2
;
330 struct symbol
*base1
, *base2
;
332 if (target
== source
)
334 if (!target
|| !source
)
335 return "different types";
337 * Peel of per-node information.
338 * FIXME! Check alignment and context too here!
340 mod1
= target
->ctype
.modifiers
;
341 as1
= target
->ctype
.as
;
342 mod2
= source
->ctype
.modifiers
;
343 as2
= source
->ctype
.as
;
344 if (target
->type
== SYM_NODE
) {
345 target
= target
->ctype
.base_type
;
348 if (target
->type
== SYM_PTR
) {
352 mod1
|= target
->ctype
.modifiers
;
353 as1
|= target
->ctype
.as
;
355 if (source
->type
== SYM_NODE
) {
356 source
= source
->ctype
.base_type
;
359 if (source
->type
== SYM_PTR
) {
363 mod2
|= source
->ctype
.modifiers
;
364 as2
|= source
->ctype
.as
;
367 if (target
== source
)
369 if (!target
|| !source
)
370 return "different types";
372 type1
= target
->type
;
373 base1
= target
->ctype
.base_type
;
375 type2
= source
->type
;
376 base2
= source
->ctype
.base_type
;
379 * Pointers to functions compare as the function itself
381 if (type1
== SYM_PTR
&& base1
) {
382 switch (base1
->type
) {
386 base1
= base1
->ctype
.base_type
;
391 if (type2
== SYM_PTR
&& base2
) {
392 switch (base2
->type
) {
396 base2
= base2
->ctype
.base_type
;
402 /* Arrays degenerate to pointers for type comparisons */
403 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
404 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
407 return "different base types";
409 /* Must be same address space to be comparable */
411 return "different address spaces";
413 /* Ignore differences in storage types, sign, or addressability */
414 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
416 mod1
&= diff
& ~target_mod_ignore
;
417 mod2
&= diff
& ~source_mod_ignore
;
419 if ((mod1
| mod2
) & MOD_SIZE
)
420 return "different type sizes";
421 return "different modifiers";
425 if (type1
== SYM_FN
) {
427 struct symbol
*arg1
, *arg2
;
428 if (base1
->variadic
!= base2
->variadic
)
429 return "incompatible variadic arguments";
430 PREPARE_PTR_LIST(target
->arguments
, arg1
);
431 PREPARE_PTR_LIST(source
->arguments
, arg2
);
435 diff
= type_difference(arg1
, arg2
, 0, 0);
437 static char argdiff
[80];
438 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
447 FINISH_PTR_LIST(arg2
);
448 FINISH_PTR_LIST(arg1
);
457 static int is_null_ptr(struct expression
*expr
)
459 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
461 if (!is_ptr_type(expr
->ctype
))
462 warn(expr
->pos
, "Using plain integer as NULL pointer");
466 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
468 /* NULL expression? Just return the type of the "other side" */
477 * Ignore differences in "volatile" and "const"ness when
478 * subtracting pointers
480 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
482 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
484 const char *typediff
;
485 struct symbol
*ctype
;
486 struct symbol
*ltype
, *rtype
;
488 ltype
= degenerate(l
);
489 rtype
= degenerate(r
);
492 * If it is an integer subtract: the ptr add case will do the
495 if (!is_ptr_type(rtype
))
496 return evaluate_ptr_add(expr
, l
, r
);
499 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
501 ctype
= common_ptr_type(l
, r
);
503 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
507 examine_symbol_type(ctype
);
509 /* Figure out the base type we point to */
510 if (ctype
->type
== SYM_NODE
)
511 ctype
= ctype
->ctype
.base_type
;
512 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
513 warn(expr
->pos
, "subtraction of functions? Share your drugs");
516 ctype
= ctype
->ctype
.base_type
;
518 expr
->ctype
= ssize_t_ctype
;
519 if (ctype
->bit_size
> bits_in_char
) {
520 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
521 struct expression
*div
= expr
;
522 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
524 val
->ctype
= size_t_ctype
;
525 val
->value
= ctype
->bit_size
>> 3;
528 sub
->ctype
= ssize_t_ctype
;
537 return ssize_t_ctype
;
540 static struct symbol
*evaluate_sub(struct expression
*expr
)
542 struct expression
*left
= expr
->left
, *right
= expr
->right
;
543 struct symbol
*ltype
= left
->ctype
;
545 if (is_ptr_type(ltype
))
546 return evaluate_ptr_sub(expr
, left
, right
);
548 // FIXME! FP promotion
549 return evaluate_arith(expr
, 1);
552 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
554 static struct symbol
*evaluate_conditional(struct expression
**p
)
556 struct symbol
*ctype
;
557 struct expression
*expr
= *p
;
562 if (expr
->type
== EXPR_ASSIGNMENT
)
563 warn(expr
->pos
, "assignment expression in conditional");
565 ctype
= evaluate_expression(expr
);
566 if (ctype
&& is_safe_type(ctype
))
567 warn(expr
->pos
, "testing a 'safe expression'");
572 static struct symbol
*evaluate_logical(struct expression
*expr
)
574 if (!evaluate_conditional(&expr
->left
))
576 if (!evaluate_conditional(&expr
->right
))
579 expr
->ctype
= &bool_ctype
;
583 static struct symbol
*evaluate_shift(struct expression
*expr
)
585 struct expression
*left
= expr
->left
, *right
= expr
->right
;
586 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
588 if (ltype
->type
== SYM_NODE
)
589 ltype
= ltype
->ctype
.base_type
;
590 if (rtype
->type
== SYM_NODE
)
591 rtype
= rtype
->ctype
.base_type
;
592 if (is_int_type(ltype
) && is_int_type(rtype
)) {
593 struct symbol
*ctype
= integer_promotion(ltype
);
594 if (ltype
->bit_size
!= ctype
->bit_size
)
595 expr
->left
= cast_to(expr
->left
, ctype
);
597 ctype
= integer_promotion(rtype
);
598 if (rtype
->bit_size
!= ctype
->bit_size
)
599 expr
->right
= cast_to(expr
->right
, ctype
);
602 return bad_expr_type(expr
);
605 static struct symbol
*evaluate_binop(struct expression
*expr
)
608 // addition can take ptr+int, fp and int
610 return evaluate_add(expr
);
612 // subtraction can take ptr-ptr, fp and int
614 return evaluate_sub(expr
);
616 // Arithmetic operations can take fp and int
618 return evaluate_arith(expr
, 1);
620 // shifts do integer promotions, but that's it.
621 case SPECIAL_LEFTSHIFT
: case SPECIAL_RIGHTSHIFT
:
622 return evaluate_shift(expr
);
624 // The rest are integer operations
625 // '%', '&', '^', '|'
627 return evaluate_arith(expr
, 0);
631 static struct symbol
*evaluate_comma(struct expression
*expr
)
633 expr
->ctype
= expr
->right
->ctype
;
637 static int modify_for_unsigned(int op
)
640 op
= SPECIAL_UNSIGNED_LT
;
642 op
= SPECIAL_UNSIGNED_GT
;
643 else if (op
== SPECIAL_LTE
)
644 op
= SPECIAL_UNSIGNED_LTE
;
645 else if (op
== SPECIAL_GTE
)
646 op
= SPECIAL_UNSIGNED_GTE
;
650 static struct symbol
*evaluate_compare(struct expression
*expr
)
652 struct expression
*left
= expr
->left
, *right
= expr
->right
;
653 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
654 struct symbol
*ctype
;
657 if (is_type_type(ltype
) && is_type_type(rtype
)) {
658 expr
->ctype
= &bool_ctype
;
662 if (is_safe_type(ltype
) || is_safe_type(rtype
))
663 warn(expr
->pos
, "testing a 'safe expression'");
666 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
667 expr
->ctype
= &bool_ctype
;
668 // FIXME! Check the types for compatibility
672 ctype
= compatible_integer_binop(&expr
->left
, &expr
->right
);
674 if (ctype
->ctype
.modifiers
& MOD_UNSIGNED
)
675 expr
->op
= modify_for_unsigned(expr
->op
);
676 expr
->ctype
= &bool_ctype
;
680 return bad_expr_type(expr
);
683 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
685 return (is_int_type(ltype
) && is_int_type(rtype
));
689 * FIXME!! This should do casts, array degeneration etc..
691 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
693 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
695 if (ltype
->type
== SYM_NODE
)
696 ltype
= ltype
->ctype
.base_type
;
698 if (rtype
->type
== SYM_NODE
)
699 rtype
= rtype
->ctype
.base_type
;
701 if (ltype
->type
== SYM_PTR
) {
702 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
706 if (rtype
->type
== SYM_PTR
) {
707 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
713 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
715 struct expression
*cond
, *true, *false;
716 struct symbol
*ctype
, *ltype
, *rtype
;
717 const char * typediff
;
719 ctype
= degenerate(expr
->conditional
);
720 cond
= expr
->conditional
;
724 if (expr
->cond_true
) {
725 ltype
= degenerate(expr
->cond_true
);
726 true = expr
->cond_true
;
729 rtype
= degenerate(expr
->cond_false
);
730 false = expr
->cond_false
;
733 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
735 ctype
= compatible_integer_binop(&true, &expr
->cond_false
);
737 ctype
= compatible_ptr_type(true, expr
->cond_false
);
739 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
749 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
750 struct expression
**rp
, struct symbol
*source
, const char *where
)
752 const char *typediff
;
756 /* It's ok if the target is more volatile or const than the source */
757 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
761 if (compatible_integer_types(target
, source
)) {
762 if (target
->bit_size
!= source
->bit_size
)
763 *rp
= cast_to(*rp
, target
);
767 /* Pointer destination? */
769 target_as
= t
->ctype
.as
;
770 if (t
->type
== SYM_NODE
) {
771 t
= t
->ctype
.base_type
;
772 target_as
|= t
->ctype
.as
;
774 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
775 struct expression
*right
= *rp
;
776 struct symbol
*s
= source
;
779 // NULL pointer is always ok
780 if (is_null_ptr(right
))
783 /* "void *" matches anything as long as the address space is ok */
784 source_as
= s
->ctype
.as
;
785 if (s
->type
== SYM_NODE
) {
786 s
= s
->ctype
.base_type
;
787 source_as
|= s
->ctype
.as
;
789 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
790 s
= s
->ctype
.base_type
;
791 t
= t
->ctype
.base_type
;
792 if (s
== &void_ctype
|| t
== &void_ctype
)
798 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
799 info(expr
->pos
, " expected %s", show_typename(target
));
800 info(expr
->pos
, " got %s", show_typename(source
));
805 * FIXME!! This is wrong from a double evaluation standpoint. We can't
806 * just expand the expression twice, that would make any side effects
809 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
812 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
813 static const int op_trans
[] = {
814 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
815 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
816 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
817 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
818 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
819 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
820 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
821 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
822 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
823 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
826 subexpr
->left
= left
;
827 subexpr
->right
= right
;
828 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
830 expr
->right
= subexpr
;
831 return evaluate_binop(subexpr
);
834 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
836 if (type
->ctype
.modifiers
& MOD_CONST
)
837 warn(left
->pos
, "assignment to const expression");
838 if (type
->type
== SYM_NODE
)
839 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
842 static struct symbol
*evaluate_assignment(struct expression
*expr
)
844 struct expression
*left
= expr
->left
, *right
= expr
->right
;
845 struct symbol
*ltype
, *rtype
;
848 rtype
= right
->ctype
;
849 if (expr
->op
!= '=') {
850 rtype
= evaluate_binop_assignment(expr
, left
, right
);
856 if (!lvalue_expression(left
)) {
857 warn(expr
->pos
, "not an lvalue");
861 rtype
= degenerate(right
);
863 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
866 evaluate_assign_to(left
, ltype
);
872 static void examine_fn_arguments(struct symbol
*fn
)
876 FOR_EACH_PTR(fn
->arguments
, s
) {
877 struct symbol
*arg
= evaluate_symbol(s
);
878 /* Array/function arguments silently degenerate into pointers */
884 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
885 if (arg
->type
== SYM_ARRAY
)
886 ptr
->ctype
= arg
->ctype
;
888 ptr
->ctype
.base_type
= arg
;
889 ptr
->ctype
.as
|= s
->ctype
.as
;
890 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
892 s
->ctype
.base_type
= ptr
;
894 s
->ctype
.modifiers
= 0;
895 examine_symbol_type(s
);
905 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
907 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
908 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
910 newsym
->ctype
.as
= as
;
911 newsym
->ctype
.modifiers
= mod
;
917 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
919 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
920 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
922 node
->ctype
.base_type
= ptr
;
923 ptr
->bit_size
= bits_in_pointer
;
924 ptr
->ctype
.alignment
= pointer_alignment
;
926 node
->bit_size
= bits_in_pointer
;
927 node
->ctype
.alignment
= pointer_alignment
;
929 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
930 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
931 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
932 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
934 if (sym
->type
== SYM_NODE
) {
935 ptr
->ctype
.as
|= sym
->ctype
.as
;
936 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
937 sym
= sym
->ctype
.base_type
;
939 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
940 ptr
->ctype
.as
|= sym
->ctype
.as
;
941 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
942 sym
= sym
->ctype
.base_type
;
944 ptr
->ctype
.base_type
= sym
;
949 /* Arrays degenerate into pointers on pointer arithmetic */
950 static struct symbol
*degenerate(struct expression
*expr
)
952 struct symbol
*ctype
, *base
;
960 if (ctype
->type
== SYM_NODE
)
961 base
= ctype
->ctype
.base_type
;
963 * Arrays degenerate into pointers to the entries, while
964 * functions degenerate into pointers to themselves
966 switch (base
->type
) {
969 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
970 warn(expr
->pos
, "strange non-value function or array");
974 ctype
= create_pointer(expr
, ctype
, 1);
982 static struct symbol
*evaluate_addressof(struct expression
*expr
)
984 struct expression
*op
= expr
->unop
;
985 struct symbol
*ctype
;
987 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
988 warn(expr
->pos
, "not addressable");
995 * symbol expression evaluation is lazy about the type
996 * of the sub-expression, so we may have to generate
997 * the type here if so..
1000 ctype
= create_pointer(expr
, ctype
, 0);
1001 expr
->ctype
= ctype
;
1007 static struct symbol
*evaluate_dereference(struct expression
*expr
)
1009 struct expression
*op
= expr
->unop
;
1010 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
1012 /* Simplify: *&(expr) => (expr) */
1013 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
1018 /* Dereferencing a node drops all the node information. */
1019 if (ctype
->type
== SYM_NODE
)
1020 ctype
= ctype
->ctype
.base_type
;
1022 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
1023 target
= ctype
->ctype
.base_type
;
1025 switch (ctype
->type
) {
1027 warn(expr
->pos
, "cannot derefence this type");
1030 merge_type(node
, ctype
);
1031 if (ctype
->type
!= SYM_ARRAY
)
1034 * Dereferencing a pointer to an array results in a
1035 * degenerate dereference: the expression becomes
1036 * just a pointer to the entry, and the derefence
1041 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
1042 target
->bit_size
= bits_in_pointer
;
1043 target
->ctype
.alignment
= pointer_alignment
;
1044 merge_type(target
, ctype
->ctype
.base_type
);
1049 * When an array is dereferenced, we need to pick
1050 * up the attributes of the original node too..
1052 merge_type(node
, op
->ctype
);
1053 merge_type(node
, ctype
);
1057 node
->bit_size
= target
->bit_size
;
1058 node
->array_size
= target
->array_size
;
1065 * Unary post-ops: x++ and x--
1067 static struct symbol
*evaluate_postop(struct expression
*expr
)
1069 struct expression
*op
= expr
->unop
;
1070 struct symbol
*ctype
= op
->ctype
;
1072 if (!lvalue_expression(expr
->unop
)) {
1073 warn(expr
->pos
, "need lvalue expression for ++/--");
1077 evaluate_assign_to(op
, ctype
);
1079 expr
->ctype
= ctype
;
1083 static struct symbol
*evaluate_sign(struct expression
*expr
)
1085 struct symbol
*ctype
= expr
->unop
->ctype
;
1086 if (is_int_type(ctype
)) {
1087 struct symbol
*rtype
= rtype
= integer_promotion(ctype
);
1088 if (rtype
->bit_size
!= ctype
->bit_size
)
1089 expr
->unop
= cast_to(expr
->unop
, rtype
);
1092 return bad_expr_type(expr
);
1094 if (expr
->op
== '+')
1095 *expr
= *expr
->unop
;
1096 expr
->ctype
= ctype
;
1100 static struct symbol
*evaluate_preop(struct expression
*expr
)
1102 struct symbol
*ctype
= expr
->unop
->ctype
;
1106 *expr
= *expr
->unop
;
1112 return evaluate_sign(expr
);
1115 return evaluate_dereference(expr
);
1118 return evaluate_addressof(expr
);
1120 case SPECIAL_INCREMENT
:
1121 case SPECIAL_DECREMENT
:
1123 * From a type evaluation standpoint the pre-ops are
1124 * the same as the postops
1126 return evaluate_postop(expr
);
1129 if (is_safe_type(ctype
))
1130 warn(expr
->pos
, "testing a 'safe expression'");
1131 ctype
= &bool_ctype
;
1137 expr
->ctype
= ctype
;
1141 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1143 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1144 struct ptr_list
*list
= head
;
1150 for (i
= 0; i
< list
->nr
; i
++) {
1151 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1153 if (sym
->ident
!= ident
)
1155 *offset
= sym
->offset
;
1158 struct symbol
*ctype
= sym
->ctype
.base_type
;
1162 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1164 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1167 *offset
+= sym
->offset
;
1171 } while ((list
= list
->next
) != head
);
1175 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1177 struct expression
*add
;
1181 /* Create a new add-expression */
1182 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1185 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1186 add
->right
->ctype
= &int_ctype
;
1187 add
->right
->value
= offset
;
1191 * The ctype of the pointer will be lazily evaluated if
1192 * we ever take the address of this member dereference..
1198 /* structure/union dereference */
1199 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1202 struct symbol
*ctype
, *member
;
1203 struct expression
*deref
= expr
->deref
, *add
;
1204 struct ident
*ident
= expr
->member
;
1208 if (!evaluate_expression(deref
))
1211 warn(expr
->pos
, "bad member name");
1215 ctype
= deref
->ctype
;
1216 address_space
= ctype
->ctype
.as
;
1217 mod
= ctype
->ctype
.modifiers
;
1218 if (ctype
->type
== SYM_NODE
) {
1219 ctype
= ctype
->ctype
.base_type
;
1220 address_space
|= ctype
->ctype
.as
;
1221 mod
|= ctype
->ctype
.modifiers
;
1223 if (!lvalue_expression(deref
)) {
1224 warn(deref
->pos
, "expected lvalue for member dereference");
1227 deref
= deref
->unop
;
1228 expr
->deref
= deref
;
1229 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1230 warn(expr
->pos
, "expected structure or union");
1234 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1236 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1237 const char *name
= "<unnamed>";
1240 name
= ctype
->ident
->name
;
1241 namelen
= ctype
->ident
->len
;
1243 warn(expr
->pos
, "no member '%s' in %s %.*s",
1244 show_ident(ident
), type
, namelen
, name
);
1249 * The member needs to take on the address space and modifiers of
1250 * the "parent" type.
1252 member
= convert_to_as_mod(member
, address_space
, mod
);
1253 add
= evaluate_offset(deref
, offset
);
1255 ctype
= member
->ctype
.base_type
;
1256 if (ctype
->type
== SYM_BITFIELD
) {
1257 expr
->type
= EXPR_BITFIELD
;
1258 expr
->bitpos
= member
->bit_offset
;
1259 expr
->nrbits
= member
->fieldwidth
;
1260 expr
->address
= add
;
1262 expr
->type
= EXPR_PREOP
;
1267 expr
->ctype
= member
;
1271 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1275 if (expr
->cast_type
) {
1276 examine_symbol_type(expr
->cast_type
);
1277 size
= expr
->cast_type
->bit_size
;
1279 if (!evaluate_expression(expr
->cast_expression
))
1281 size
= expr
->cast_expression
->ctype
->bit_size
;
1284 warn(expr
->pos
, "cannot size expression");
1287 expr
->type
= EXPR_VALUE
;
1288 expr
->value
= size
>> 3;
1289 expr
->ctype
= size_t_ctype
;
1290 return size_t_ctype
;
1293 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1295 struct symbol
*type
= expr
->cast_type
;
1298 type
= evaluate_expression(expr
->cast_expression
);
1302 examine_symbol_type(type
);
1303 expr
->type
= EXPR_VALUE
;
1304 expr
->value
= type
->ctype
.alignment
;
1305 expr
->ctype
= size_t_ctype
;
1306 return size_t_ctype
;
1309 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1311 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1312 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1316 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1318 struct expression
*expr
;
1319 struct symbol_list
*argument_types
= fn
->arguments
;
1320 struct symbol
*argtype
;
1323 PREPARE_PTR_LIST(argument_types
, argtype
);
1324 FOR_EACH_PTR (head
, expr
) {
1325 struct expression
**p
= THIS_ADDRESS(expr
);
1326 struct symbol
*ctype
, *target
;
1327 ctype
= evaluate_expression(expr
);
1332 if (context_clash(f
, ctype
))
1333 warn(expr
->pos
, "argument %d used in wrong context", i
);
1335 ctype
= degenerate(expr
);
1338 if (!target
&& ctype
->bit_size
< bits_in_int
)
1339 target
= &int_ctype
;
1341 static char where
[30];
1342 examine_symbol_type(target
);
1343 sprintf(where
, "argument %d", i
);
1344 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1348 NEXT_PTR_LIST(argtype
);
1350 FINISH_PTR_LIST(argtype
);
1354 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1355 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1357 struct expression
*entry
;
1361 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1362 struct expression
**p
= THIS_ADDRESS(entry
);
1364 if (entry
->type
== EXPR_INDEX
) {
1365 current
= entry
->idx_to
;
1368 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1376 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1377 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1379 if (offset
|| expression_list_size(expr
->expr_list
) != 1) {
1380 warn(expr
->pos
, "unexpected compound initializer");
1383 return evaluate_array_initializer(ctype
, expr
, 0);
1386 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1388 struct expression
*entry
;
1391 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1392 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1393 struct expression
**p
= THIS_ADDRESS(entry
);
1395 if (entry
->type
== EXPR_IDENTIFIER
) {
1396 struct ident
*ident
= entry
->expr_ident
;
1397 /* We special-case the "already right place" case */
1398 if (sym
&& sym
->ident
== ident
)
1400 RESET_PTR_LIST(sym
);
1403 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1406 if (sym
->ident
== ident
)
1414 warn(expr
->pos
, "too many initializers for struct/union");
1418 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1422 FINISH_PTR_LIST(sym
);
1428 * Initializers are kind of like assignments. Except
1429 * they can be a hell of a lot more complex.
1431 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1433 struct expression
*expr
= *ep
;
1436 * Simple non-structure/array initializers are the simple
1437 * case, and look (and parse) largely like assignments.
1439 if (expr
->type
!= EXPR_INITIALIZER
) {
1441 struct symbol
*rtype
= evaluate_expression(expr
);
1443 struct expression
*pos
;
1445 // FIXME! char array[] = "string" special case
1446 // should _not_ degenerate.
1447 rtype
= degenerate(expr
);
1448 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1449 /* strings are special: char arrays */
1450 if (rtype
->type
== SYM_ARRAY
)
1451 size
= get_expression_value(rtype
->array_size
);
1453 * Don't bother creating a position expression for
1454 * the simple initializer cases that don't need it.
1456 * We need a position if the initializer has a byte
1457 * offset, _or_ if we're initializing a bitfield.
1459 if (offset
|| ctype
->fieldwidth
) {
1460 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1461 pos
->init_offset
= offset
;
1462 pos
->init_sym
= ctype
;
1463 pos
->init_expr
= *ep
;
1464 pos
->ctype
= expr
->ctype
;
1471 expr
->ctype
= ctype
;
1472 if (ctype
->type
== SYM_NODE
)
1473 ctype
= ctype
->ctype
.base_type
;
1475 switch (ctype
->type
) {
1478 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1480 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1482 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1484 return evaluate_scalar_initializer(ctype
, expr
, offset
);
1488 static int get_as(struct symbol
*sym
)
1496 mod
= sym
->ctype
.modifiers
;
1497 if (sym
->type
== SYM_NODE
) {
1498 sym
= sym
->ctype
.base_type
;
1499 as
|= sym
->ctype
.as
;
1500 mod
|= sym
->ctype
.modifiers
;
1503 * You can always throw a value away by casting to
1504 * "void" - that's an implicit "force". Note that
1505 * the same is _not_ true of "void *".
1507 if (sym
== &void_ctype
)
1511 * At least for now, allow casting to a "unsigned long".
1512 * That's how we do things like pointer arithmetic and
1513 * store pointers to registers.
1515 if (sym
== &ulong_ctype
)
1518 if (sym
&& sym
->type
== SYM_PTR
) {
1519 sym
= sym
->ctype
.base_type
;
1520 as
|= sym
->ctype
.as
;
1521 mod
|= sym
->ctype
.modifiers
;
1523 if (mod
& MOD_FORCE
)
1528 static struct symbol
*evaluate_cast(struct expression
*expr
)
1530 struct expression
*target
= expr
->cast_expression
;
1531 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1533 expr
->ctype
= ctype
;
1534 expr
->cast_type
= ctype
;
1537 * Special case: a cast can be followed by an
1538 * initializer, in which case we need to pass
1539 * the type value down to that initializer rather
1540 * than trying to evaluate it as an expression
1542 * A more complex case is when the initializer is
1543 * dereferenced as part of a post-fix expression.
1544 * We need to produce an expression that can be dereferenced.
1546 if (target
->type
== EXPR_INITIALIZER
) {
1547 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1548 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1550 sym
->ctype
.base_type
= ctype
;
1551 sym
->initializer
= expr
->cast_expression
;
1552 evaluate_symbol(sym
);
1554 addr
->ctype
= NULL
; /* Lazy eval */
1557 expr
->type
= EXPR_PREOP
;
1560 expr
->ctype
= ctype
;
1564 evaluate_expression(target
);
1567 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
1568 warn(expr
->pos
, "cast removes address space of expression");
1571 * Casts of constant values are special: they
1572 * can be NULL, and thus need to be simplified
1575 if (target
->type
== EXPR_VALUE
)
1576 cast_value(expr
, ctype
, target
, target
->ctype
);
1582 * Evaluate a call expression with a symbol. This
1583 * should expand inline functions, and evaluate
1586 static int evaluate_symbol_call(struct expression
*expr
)
1588 struct expression
*fn
= expr
->fn
;
1589 struct symbol
*ctype
= fn
->ctype
;
1591 if (fn
->type
!= EXPR_PREOP
)
1594 if (ctype
->op
&& ctype
->op
->evaluate
)
1595 return ctype
->op
->evaluate(expr
);
1597 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1599 struct symbol
*curr
= current_fn
;
1600 unsigned long context
= current_context
;
1601 unsigned long mask
= current_contextmask
;
1603 current_context
|= ctype
->ctype
.context
;
1604 current_contextmask
|= ctype
->ctype
.contextmask
;
1605 current_fn
= ctype
->ctype
.base_type
;
1606 examine_fn_arguments(current_fn
);
1608 ret
= inline_function(expr
, ctype
);
1610 /* restore the old function context */
1612 current_context
= context
;
1613 current_contextmask
= mask
;
1620 static struct symbol
*evaluate_call(struct expression
*expr
)
1623 struct symbol
*ctype
, *sym
;
1624 struct expression
*fn
= expr
->fn
;
1625 struct expression_list
*arglist
= expr
->args
;
1627 if (!evaluate_expression(fn
))
1629 sym
= ctype
= fn
->ctype
;
1630 if (ctype
->type
== SYM_NODE
)
1631 ctype
= ctype
->ctype
.base_type
;
1632 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1633 ctype
= ctype
->ctype
.base_type
;
1634 if (!evaluate_arguments(sym
, ctype
, arglist
))
1636 if (ctype
->type
!= SYM_FN
) {
1637 warn(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
1640 args
= expression_list_size(expr
->args
);
1641 fnargs
= symbol_list_size(ctype
->arguments
);
1643 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1644 if (args
> fnargs
&& !ctype
->variadic
)
1645 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1646 if (sym
->type
== SYM_NODE
) {
1647 if (evaluate_symbol_call(expr
))
1650 expr
->ctype
= ctype
->ctype
.base_type
;
1654 struct symbol
*evaluate_expression(struct expression
*expr
)
1661 switch (expr
->type
) {
1663 warn(expr
->pos
, "value expression without a type");
1666 return evaluate_string(expr
);
1668 return evaluate_symbol_expression(expr
);
1670 if (!evaluate_expression(expr
->left
))
1672 if (!evaluate_expression(expr
->right
))
1674 return evaluate_binop(expr
);
1676 return evaluate_logical(expr
);
1678 if (!evaluate_expression(expr
->left
))
1680 if (!evaluate_expression(expr
->right
))
1682 return evaluate_comma(expr
);
1684 if (!evaluate_expression(expr
->left
))
1686 if (!evaluate_expression(expr
->right
))
1688 return evaluate_compare(expr
);
1689 case EXPR_ASSIGNMENT
:
1690 if (!evaluate_expression(expr
->left
))
1692 if (!evaluate_expression(expr
->right
))
1694 return evaluate_assignment(expr
);
1696 if (!evaluate_expression(expr
->unop
))
1698 return evaluate_preop(expr
);
1700 if (!evaluate_expression(expr
->unop
))
1702 return evaluate_postop(expr
);
1704 return evaluate_cast(expr
);
1706 return evaluate_sizeof(expr
);
1708 return evaluate_alignof(expr
);
1710 return evaluate_member_dereference(expr
);
1712 return evaluate_call(expr
);
1714 warn(expr
->pos
, "bitfield generated by parser");
1716 case EXPR_CONDITIONAL
:
1717 if (!evaluate_conditional(&expr
->conditional
))
1719 if (!evaluate_expression(expr
->cond_false
))
1721 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1723 return evaluate_conditional_expression(expr
);
1724 case EXPR_STATEMENT
:
1725 expr
->ctype
= evaluate_statement(expr
->statement
);
1729 expr
->ctype
= &ptr_ctype
;
1733 /* Evaluate the type of the symbol .. */
1734 evaluate_symbol(expr
->symbol
);
1735 /* .. but the type of the _expression_ is a "type" */
1736 expr
->ctype
= &type_ctype
;
1739 /* These can not exist as stand-alone expressions */
1740 case EXPR_INITIALIZER
:
1741 case EXPR_IDENTIFIER
:
1744 warn(expr
->pos
, "internal front-end error: initializer in expression");
1750 void check_duplicates(struct symbol
*sym
)
1752 struct symbol
*next
= sym
;
1754 while ((next
= next
->same_symbol
) != NULL
) {
1755 const char *typediff
;
1756 evaluate_symbol(next
);
1757 typediff
= type_difference(sym
, next
, 0, 0);
1759 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1760 show_ident(sym
->ident
),
1761 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1767 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1769 struct symbol
*base_type
;
1774 sym
= examine_symbol_type(sym
);
1775 base_type
= sym
->ctype
.base_type
;
1779 /* Evaluate the initializers */
1780 if (sym
->initializer
) {
1781 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1782 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
1783 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1784 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
1785 base_type
->bit_size
= bit_size
;
1786 sym
->array_size
= base_type
->array_size
;
1787 sym
->bit_size
= bit_size
;
1791 /* And finally, evaluate the body of the symbol too */
1792 if (base_type
->type
== SYM_FN
) {
1793 examine_fn_arguments(base_type
);
1794 if (base_type
->stmt
) {
1795 current_fn
= base_type
;
1796 current_contextmask
= sym
->ctype
.contextmask
;
1797 current_context
= sym
->ctype
.context
;
1798 evaluate_statement(base_type
->stmt
);
1805 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1807 struct expression
*expr
= stmt
->expression
;
1808 struct symbol
*ctype
, *fntype
;
1810 evaluate_expression(expr
);
1811 ctype
= degenerate(expr
);
1812 fntype
= current_fn
->ctype
.base_type
;
1813 if (!fntype
|| fntype
== &void_ctype
) {
1814 if (expr
&& ctype
!= &void_ctype
)
1815 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1820 warn(stmt
->pos
, "return with no return value");
1825 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
1829 static void evaluate_if_statement(struct statement
*stmt
)
1831 struct symbol
*ctype
;
1833 if (!stmt
->if_conditional
)
1836 ctype
= evaluate_conditional(&stmt
->if_conditional
);
1840 evaluate_statement(stmt
->if_true
);
1841 evaluate_statement(stmt
->if_false
);
1844 struct symbol
*evaluate_statement(struct statement
*stmt
)
1849 switch (stmt
->type
) {
1851 return evaluate_return_expression(stmt
);
1853 case STMT_EXPRESSION
:
1854 evaluate_expression(stmt
->expression
);
1855 return degenerate(stmt
->expression
);
1857 case STMT_COMPOUND
: {
1858 struct statement
*s
;
1859 struct symbol
*type
= NULL
;
1862 /* Evaluate each symbol in the compound statement */
1863 FOR_EACH_PTR(stmt
->syms
, sym
) {
1864 evaluate_symbol(sym
);
1866 evaluate_symbol(stmt
->ret
);
1869 * Then, evaluate each statement, making the type of the
1870 * compound statement be the type of the last statement
1873 FOR_EACH_PTR(stmt
->stmts
, s
) {
1874 type
= evaluate_statement(s
);
1879 evaluate_if_statement(stmt
);
1882 evaluate_conditional(&stmt
->iterator_pre_condition
);
1883 evaluate_conditional(&stmt
->iterator_post_condition
);
1884 evaluate_statement(stmt
->iterator_pre_statement
);
1885 evaluate_statement(stmt
->iterator_statement
);
1886 evaluate_statement(stmt
->iterator_post_statement
);
1889 evaluate_expression(stmt
->switch_expression
);
1890 evaluate_statement(stmt
->switch_statement
);
1893 evaluate_expression(stmt
->case_expression
);
1894 evaluate_expression(stmt
->case_to
);
1895 evaluate_statement(stmt
->case_statement
);
1898 evaluate_statement(stmt
->label_statement
);
1901 evaluate_expression(stmt
->goto_expression
);
1906 /* FIXME! Do the asm parameter evaluation! */