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
)
130 static struct symbol
*bigger_int_type(struct symbol
*left
, struct symbol
*right
)
132 unsigned long lmod
, rmod
, mod
;
134 left
= integer_promotion(left
);
135 right
= integer_promotion(right
);
140 if (left
->bit_size
> right
->bit_size
)
143 if (right
->bit_size
> left
->bit_size
)
146 /* Same size integers - promote to unsigned, promote to long */
147 lmod
= left
->ctype
.modifiers
;
148 rmod
= right
->ctype
.modifiers
;
154 return ctype_integer(mod
);
162 static struct expression
* cast_to(struct expression
*old
, struct symbol
*type
)
164 struct expression
*expr
= alloc_expression(old
->pos
, EXPR_CAST
);
166 expr
->cast_type
= type
;
167 expr
->cast_expression
= old
;
171 static int is_type_type(struct symbol
*type
)
173 return (type
->ctype
.modifiers
& MOD_TYPE
) != 0;
176 static int is_ptr_type(struct symbol
*type
)
178 if (type
->type
== SYM_NODE
)
179 type
= type
->ctype
.base_type
;
180 return type
->type
== SYM_PTR
|| type
->type
== SYM_ARRAY
|| type
->type
== SYM_FN
;
183 static inline int is_int_type(struct symbol
*type
)
185 if (type
->type
== SYM_NODE
)
186 type
= type
->ctype
.base_type
;
187 return (type
->type
== SYM_ENUM
) ||
188 (type
->type
== SYM_BITFIELD
) ||
189 type
->ctype
.base_type
== &int_type
;
192 static struct symbol
*bad_expr_type(struct expression
*expr
)
194 warn(expr
->pos
, "incompatible types for operation");
198 static struct symbol
* compatible_integer_binop(struct expression
*expr
, struct expression
**lp
, struct expression
**rp
)
200 struct expression
*left
= *lp
, *right
= *rp
;
201 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
203 if (ltype
->type
== SYM_NODE
)
204 ltype
= ltype
->ctype
.base_type
;
205 if (rtype
->type
== SYM_NODE
)
206 rtype
= rtype
->ctype
.base_type
;
207 if (is_int_type(ltype
) && is_int_type(rtype
)) {
208 struct symbol
*ctype
= bigger_int_type(ltype
, rtype
);
210 /* Don't bother promoting same-size entities, it only adds clutter */
211 if (ltype
->bit_size
!= ctype
->bit_size
)
212 *lp
= cast_to(left
, ctype
);
213 if (rtype
->bit_size
!= ctype
->bit_size
)
214 *rp
= cast_to(right
, ctype
);
220 static struct symbol
*evaluate_int_binop(struct expression
*expr
)
222 struct symbol
*ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
227 return bad_expr_type(expr
);
230 static inline int lvalue_expression(struct expression
*expr
)
232 while (expr
->type
== EXPR_CAST
)
233 expr
= expr
->cast_expression
;
234 return (expr
->type
== EXPR_PREOP
&& expr
->op
== '*') || expr
->type
== EXPR_BITFIELD
;
237 static struct symbol
*evaluate_ptr_add(struct expression
*expr
, struct expression
*ptr
, struct expression
*i
)
239 struct symbol
*ctype
;
240 struct symbol
*ptr_type
= ptr
->ctype
;
243 if (ptr_type
->type
== SYM_NODE
)
244 ptr_type
= ptr_type
->ctype
.base_type
;
246 if (!is_int_type(i
->ctype
))
247 return bad_expr_type(expr
);
250 examine_symbol_type(ctype
);
252 ctype
= degenerate(ptr
);
253 if (!ctype
->ctype
.base_type
) {
254 warn(expr
->pos
, "missing type information");
258 /* Get the size of whatever the pointer points to */
260 if (ptr_type
->type
== SYM_NODE
)
261 ptr_type
= ptr_type
->ctype
.base_type
;
262 if (ptr_type
->type
== SYM_PTR
)
263 ptr_type
= ptr_type
->ctype
.base_type
;
264 bit_size
= ptr_type
->bit_size
;
266 /* Special case: adding zero commonly happens as a result of 'array[0]' */
267 if (i
->type
== EXPR_VALUE
&& !i
->value
) {
269 } else if (bit_size
> bits_in_char
) {
270 struct expression
*add
= expr
;
271 struct expression
*mul
= alloc_expression(expr
->pos
, EXPR_BINOP
);
272 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
274 val
->ctype
= size_t_ctype
;
275 val
->value
= bit_size
>> 3;
278 mul
->ctype
= size_t_ctype
;
282 /* Leave 'add->op' as 'expr->op' - either '+' or '-' */
291 static struct symbol
*evaluate_add(struct expression
*expr
)
293 struct expression
*left
= expr
->left
, *right
= expr
->right
;
294 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
296 if (is_ptr_type(ltype
))
297 return evaluate_ptr_add(expr
, left
, right
);
299 if (is_ptr_type(rtype
))
300 return evaluate_ptr_add(expr
, right
, left
);
302 // FIXME! FP promotion
303 return evaluate_int_binop(expr
);
306 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
307 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | MOD_SIGNED | MOD_UNSIGNED | MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE)
309 const char * type_difference(struct symbol
*target
, struct symbol
*source
,
310 unsigned long target_mod_ignore
, unsigned long source_mod_ignore
)
313 unsigned long mod1
, mod2
, diff
;
314 unsigned long as1
, as2
;
316 struct symbol
*base1
, *base2
;
318 if (target
== source
)
320 if (!target
|| !source
)
321 return "different types";
323 * Peel of per-node information.
324 * FIXME! Check alignment and context too here!
326 mod1
= target
->ctype
.modifiers
;
327 as1
= target
->ctype
.as
;
328 mod2
= source
->ctype
.modifiers
;
329 as2
= source
->ctype
.as
;
330 if (target
->type
== SYM_NODE
) {
331 target
= target
->ctype
.base_type
;
334 if (target
->type
== SYM_PTR
) {
338 mod1
|= target
->ctype
.modifiers
;
339 as1
|= target
->ctype
.as
;
341 if (source
->type
== SYM_NODE
) {
342 source
= source
->ctype
.base_type
;
345 if (source
->type
== SYM_PTR
) {
349 mod2
|= source
->ctype
.modifiers
;
350 as2
|= source
->ctype
.as
;
353 if (target
== source
)
355 if (!target
|| !source
)
356 return "different types";
358 type1
= target
->type
;
359 base1
= target
->ctype
.base_type
;
361 type2
= source
->type
;
362 base2
= source
->ctype
.base_type
;
365 * Pointers to functions compare as the function itself
367 if (type1
== SYM_PTR
&& base1
) {
368 switch (base1
->type
) {
372 base1
= base1
->ctype
.base_type
;
377 if (type2
== SYM_PTR
&& base2
) {
378 switch (base2
->type
) {
382 base2
= base2
->ctype
.base_type
;
388 /* Arrays degenerate to pointers for type comparisons */
389 type1
= (type1
== SYM_ARRAY
) ? SYM_PTR
: type1
;
390 type2
= (type2
== SYM_ARRAY
) ? SYM_PTR
: type2
;
393 return "different base types";
395 /* Must be same address space to be comparable */
397 return "different address spaces";
399 /* Ignore differences in storage types, sign, or addressability */
400 diff
= (mod1
^ mod2
) & ~MOD_IGNORE
;
402 mod1
&= diff
& ~target_mod_ignore
;
403 mod2
&= diff
& ~source_mod_ignore
;
405 if ((mod1
| mod2
) & MOD_SIZE
)
406 return "different type sizes";
407 return "different modifiers";
411 if (type1
== SYM_FN
) {
413 struct symbol
*arg1
, *arg2
;
414 if (base1
->variadic
!= base2
->variadic
)
415 return "incompatible variadic arguments";
416 PREPARE_PTR_LIST(target
->arguments
, arg1
);
417 PREPARE_PTR_LIST(source
->arguments
, arg2
);
421 diff
= type_difference(arg1
, arg2
, 0, 0);
423 static char argdiff
[80];
424 sprintf(argdiff
, "incompatible argument %d (%s)", i
, diff
);
433 FINISH_PTR_LIST(arg2
);
434 FINISH_PTR_LIST(arg1
);
443 static int is_null_ptr(struct expression
*expr
)
445 if (expr
->type
!= EXPR_VALUE
|| expr
->value
)
447 if (!is_ptr_type(expr
->ctype
))
448 warn(expr
->pos
, "Using plain integer as NULL pointer");
452 static struct symbol
*common_ptr_type(struct expression
*l
, struct expression
*r
)
454 /* NULL expression? Just return the type of the "other side" */
463 * Ignore differences in "volatile" and "const"ness when
464 * subtracting pointers
466 #define MOD_IGN (MOD_VOLATILE | MOD_CONST)
468 static struct symbol
*evaluate_ptr_sub(struct expression
*expr
, struct expression
*l
, struct expression
*r
)
470 const char *typediff
;
471 struct symbol
*ctype
;
472 struct symbol
*ltype
, *rtype
;
474 ltype
= degenerate(l
);
475 rtype
= degenerate(r
);
478 * If it is an integer subtract: the ptr add case will do the
481 if (!is_ptr_type(rtype
))
482 return evaluate_ptr_add(expr
, l
, r
);
485 typediff
= type_difference(ltype
, rtype
, ~MOD_SIZE
, ~MOD_SIZE
);
487 ctype
= common_ptr_type(l
, r
);
489 warn(expr
->pos
, "subtraction of different types can't work (%s)", typediff
);
493 examine_symbol_type(ctype
);
495 /* Figure out the base type we point to */
496 if (ctype
->type
== SYM_NODE
)
497 ctype
= ctype
->ctype
.base_type
;
498 if (ctype
->type
!= SYM_PTR
&& ctype
->type
!= SYM_ARRAY
) {
499 warn(expr
->pos
, "subtraction of functions? Share your drugs");
502 ctype
= ctype
->ctype
.base_type
;
504 expr
->ctype
= ssize_t_ctype
;
505 if (ctype
->bit_size
> bits_in_char
) {
506 struct expression
*sub
= alloc_expression(expr
->pos
, EXPR_BINOP
);
507 struct expression
*div
= expr
;
508 struct expression
*val
= alloc_expression(expr
->pos
, EXPR_VALUE
);
510 val
->ctype
= size_t_ctype
;
511 val
->value
= ctype
->bit_size
>> 3;
514 sub
->ctype
= ssize_t_ctype
;
523 return ssize_t_ctype
;
526 static struct symbol
*evaluate_sub(struct expression
*expr
)
528 struct expression
*left
= expr
->left
, *right
= expr
->right
;
529 struct symbol
*ltype
= left
->ctype
;
531 if (is_ptr_type(ltype
))
532 return evaluate_ptr_sub(expr
, left
, right
);
534 // FIXME! FP promotion
535 return evaluate_int_binop(expr
);
538 #define is_safe_type(type) ((type)->ctype.modifiers & MOD_SAFE)
540 static struct symbol
*evaluate_conditional(struct expression
*expr
)
542 struct symbol
*ctype
;
547 if (expr
->type
== EXPR_ASSIGNMENT
)
548 warn(expr
->pos
, "assignment expression in conditional");
550 ctype
= evaluate_expression(expr
);
551 if (ctype
&& is_safe_type(ctype
))
552 warn(expr
->pos
, "testing a 'safe expression'");
557 static struct symbol
*evaluate_logical(struct expression
*expr
)
559 if (!evaluate_conditional(expr
->left
))
561 if (!evaluate_conditional(expr
->right
))
564 expr
->ctype
= &bool_ctype
;
568 static struct symbol
*evaluate_arithmetic(struct expression
*expr
)
570 // FIXME! Floating-point promotion!
571 return evaluate_int_binop(expr
);
574 static struct symbol
*evaluate_binop(struct expression
*expr
)
577 // addition can take ptr+int, fp and int
579 return evaluate_add(expr
);
581 // subtraction can take ptr-ptr, fp and int
583 return evaluate_sub(expr
);
585 // Arithmetic operations can take fp and int
586 case '*': case '/': case '%':
587 return evaluate_arithmetic(expr
);
589 // The rest are integer operations (bitops)
590 // SPECIAL_LEFTSHIFT, SPECIAL_RIGHTSHIFT
593 return evaluate_int_binop(expr
);
597 static struct symbol
*evaluate_comma(struct expression
*expr
)
599 expr
->ctype
= expr
->right
->ctype
;
603 static struct symbol
*evaluate_compare(struct expression
*expr
)
605 struct expression
*left
= expr
->left
, *right
= expr
->right
;
606 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
607 struct symbol
*ctype
;
610 if (is_type_type(ltype
) && is_type_type(rtype
)) {
611 expr
->ctype
= &bool_ctype
;
615 if (is_safe_type(ltype
) || is_safe_type(rtype
))
616 warn(expr
->pos
, "testing a 'safe expression'");
619 if (is_ptr_type(ltype
) || is_ptr_type(rtype
)) {
620 expr
->ctype
= &bool_ctype
;
621 // FIXME! Check the types for compatibility
625 ctype
= compatible_integer_binop(expr
, &expr
->left
, &expr
->right
);
627 expr
->ctype
= &bool_ctype
;
631 return bad_expr_type(expr
);
634 static int compatible_integer_types(struct symbol
*ltype
, struct symbol
*rtype
)
636 return (is_int_type(ltype
) && is_int_type(rtype
));
640 * FIXME!! This should do casts, array degeneration etc..
642 static struct symbol
*compatible_ptr_type(struct expression
*left
, struct expression
*right
)
644 struct symbol
*ltype
= left
->ctype
, *rtype
= right
->ctype
;
646 if (ltype
->type
== SYM_NODE
)
647 ltype
= ltype
->ctype
.base_type
;
649 if (rtype
->type
== SYM_NODE
)
650 rtype
= rtype
->ctype
.base_type
;
652 if (ltype
->type
== SYM_PTR
) {
653 if (is_null_ptr(right
) || rtype
->ctype
.base_type
== &void_ctype
)
657 if (rtype
->type
== SYM_PTR
) {
658 if (is_null_ptr(left
) || ltype
->ctype
.base_type
== &void_ctype
)
664 static struct symbol
* evaluate_conditional_expression(struct expression
*expr
)
666 struct expression
*cond
, *true, *false;
667 struct symbol
*ctype
, *ltype
, *rtype
;
668 const char * typediff
;
670 ctype
= degenerate(expr
->conditional
);
671 cond
= expr
->conditional
;
675 if (expr
->cond_true
) {
676 ltype
= degenerate(expr
->cond_true
);
677 true = expr
->cond_true
;
680 rtype
= degenerate(expr
->cond_false
);
681 false = expr
->cond_false
;
684 typediff
= type_difference(ltype
, rtype
, MOD_IGN
, MOD_IGN
);
686 ctype
= compatible_integer_binop(expr
, &true, &expr
->cond_false
);
688 ctype
= compatible_ptr_type(true, expr
->cond_false
);
690 warn(expr
->pos
, "incompatible types in conditional expression (%s)", typediff
);
700 static int compatible_assignment_types(struct expression
*expr
, struct symbol
*target
,
701 struct expression
**rp
, struct symbol
*source
, const char *where
)
703 const char *typediff
;
707 /* It's ok if the target is more volatile or const than the source */
708 typediff
= type_difference(target
, source
, MOD_VOLATILE
| MOD_CONST
, 0);
712 if (compatible_integer_types(target
, source
)) {
713 if (target
->bit_size
!= source
->bit_size
)
714 *rp
= cast_to(*rp
, target
);
718 /* Pointer destination? */
720 target_as
= t
->ctype
.as
;
721 if (t
->type
== SYM_NODE
) {
722 t
= t
->ctype
.base_type
;
723 target_as
|= t
->ctype
.as
;
725 if (t
->type
== SYM_PTR
|| t
->type
== SYM_FN
|| t
->type
== SYM_ARRAY
) {
726 struct expression
*right
= *rp
;
727 struct symbol
*s
= source
;
730 // NULL pointer is always ok
731 if (is_null_ptr(right
))
734 /* "void *" matches anything as long as the address space is ok */
735 source_as
= s
->ctype
.as
;
736 if (s
->type
== SYM_NODE
) {
737 s
= s
->ctype
.base_type
;
738 source_as
|= s
->ctype
.as
;
740 if (source_as
== target_as
&& (s
->type
== SYM_PTR
|| s
->type
== SYM_ARRAY
)) {
741 s
= s
->ctype
.base_type
;
742 t
= t
->ctype
.base_type
;
743 if (s
== &void_ctype
|| t
== &void_ctype
)
749 warn(expr
->pos
, "incorrect type in %s (%s)", where
, typediff
);
750 info(expr
->pos
, " expected %s", show_typename(target
));
751 info(expr
->pos
, " got %s", show_typename(source
));
756 * FIXME!! This is wrong from a double evaluation standpoint. We can't
757 * just expand the expression twice, that would make any side effects
760 static struct symbol
*evaluate_binop_assignment(struct expression
*expr
, struct expression
*left
, struct expression
*right
)
763 struct expression
*subexpr
= alloc_expression(expr
->pos
, EXPR_BINOP
);
764 static const int op_trans
[] = {
765 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = '+',
766 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = '-',
767 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = '*',
768 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = '/',
769 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = '%',
770 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = SPECIAL_LEFTSHIFT
,
771 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = SPECIAL_RIGHTSHIFT
,
772 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = '&',
773 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = '|',
774 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = '^'
777 subexpr
->left
= left
;
778 subexpr
->right
= right
;
779 subexpr
->op
= op_trans
[op
- SPECIAL_BASE
];
781 expr
->right
= subexpr
;
782 return evaluate_binop(subexpr
);
785 static void evaluate_assign_to(struct expression
*left
, struct symbol
*type
)
787 if (type
->ctype
.modifiers
& MOD_CONST
)
788 warn(left
->pos
, "assignment to const expression");
789 if (type
->type
== SYM_NODE
)
790 type
->ctype
.modifiers
|= MOD_ASSIGNED
;
793 static struct symbol
*evaluate_assignment(struct expression
*expr
)
795 struct expression
*left
= expr
->left
, *right
= expr
->right
;
796 struct symbol
*ltype
, *rtype
;
799 rtype
= right
->ctype
;
800 if (expr
->op
!= '=') {
801 rtype
= evaluate_binop_assignment(expr
, left
, right
);
807 if (!lvalue_expression(left
)) {
808 warn(expr
->pos
, "not an lvalue");
812 rtype
= degenerate(right
);
814 if (!compatible_assignment_types(expr
, ltype
, &expr
->right
, rtype
, "assignment"))
817 evaluate_assign_to(left
, ltype
);
823 static void examine_fn_arguments(struct symbol
*fn
)
827 FOR_EACH_PTR(fn
->arguments
, s
) {
828 struct symbol
*arg
= evaluate_symbol(s
);
829 /* Array/function arguments silently degenerate into pointers */
835 ptr
= alloc_symbol(s
->pos
, SYM_PTR
);
836 if (arg
->type
== SYM_ARRAY
)
837 ptr
->ctype
= arg
->ctype
;
839 ptr
->ctype
.base_type
= arg
;
840 ptr
->ctype
.as
|= s
->ctype
.as
;
841 ptr
->ctype
.modifiers
|= s
->ctype
.modifiers
;
843 s
->ctype
.base_type
= ptr
;
845 s
->ctype
.modifiers
= 0;
846 examine_symbol_type(s
);
856 static struct symbol
*convert_to_as_mod(struct symbol
*sym
, int as
, int mod
)
858 if (sym
->ctype
.as
!= as
|| sym
->ctype
.modifiers
!= mod
) {
859 struct symbol
*newsym
= alloc_symbol(sym
->pos
, SYM_NODE
);
861 newsym
->ctype
.as
= as
;
862 newsym
->ctype
.modifiers
= mod
;
868 static struct symbol
*create_pointer(struct expression
*expr
, struct symbol
*sym
, int degenerate
)
870 struct symbol
*node
= alloc_symbol(expr
->pos
, SYM_NODE
);
871 struct symbol
*ptr
= alloc_symbol(expr
->pos
, SYM_PTR
);
873 node
->ctype
.base_type
= ptr
;
874 ptr
->bit_size
= bits_in_pointer
;
875 ptr
->ctype
.alignment
= pointer_alignment
;
877 node
->bit_size
= bits_in_pointer
;
878 node
->ctype
.alignment
= pointer_alignment
;
880 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
881 if (sym
->ctype
.modifiers
& MOD_REGISTER
) {
882 warn(expr
->pos
, "taking address of 'register' variable '%s'", show_ident(sym
->ident
));
883 sym
->ctype
.modifiers
&= ~MOD_REGISTER
;
885 if (sym
->type
== SYM_NODE
) {
886 ptr
->ctype
.as
|= sym
->ctype
.as
;
887 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
888 sym
= sym
->ctype
.base_type
;
890 if (degenerate
&& sym
->type
== SYM_ARRAY
) {
891 ptr
->ctype
.as
|= sym
->ctype
.as
;
892 ptr
->ctype
.modifiers
|= sym
->ctype
.modifiers
;
893 sym
= sym
->ctype
.base_type
;
895 ptr
->ctype
.base_type
= sym
;
900 /* Arrays degenerate into pointers on pointer arithmetic */
901 static struct symbol
*degenerate(struct expression
*expr
)
903 struct symbol
*ctype
, *base
;
911 if (ctype
->type
== SYM_NODE
)
912 base
= ctype
->ctype
.base_type
;
914 * Arrays degenerate into pointers to the entries, while
915 * functions degenerate into pointers to themselves
917 switch (base
->type
) {
920 if (expr
->op
!= '*' || expr
->type
!= EXPR_PREOP
) {
921 warn(expr
->pos
, "strange non-value function or array");
925 ctype
= create_pointer(expr
, ctype
, 1);
933 static struct symbol
*evaluate_addressof(struct expression
*expr
)
935 struct expression
*op
= expr
->unop
;
936 struct symbol
*ctype
;
938 if (op
->op
!= '*' || op
->type
!= EXPR_PREOP
) {
939 warn(expr
->pos
, "not addressable");
946 * symbol expression evaluation is lazy about the type
947 * of the sub-expression, so we may have to generate
948 * the type here if so..
951 ctype
= create_pointer(expr
, ctype
, 0);
958 static struct symbol
*evaluate_dereference(struct expression
*expr
)
960 struct expression
*op
= expr
->unop
;
961 struct symbol
*ctype
= op
->ctype
, *node
, *target
;
963 /* Simplify: *&(expr) => (expr) */
964 if (op
->type
== EXPR_PREOP
&& op
->op
== '&') {
969 /* Dereferencing a node drops all the node information. */
970 if (ctype
->type
== SYM_NODE
)
971 ctype
= ctype
->ctype
.base_type
;
973 node
= alloc_symbol(expr
->pos
, SYM_NODE
);
974 target
= ctype
->ctype
.base_type
;
976 switch (ctype
->type
) {
978 warn(expr
->pos
, "cannot derefence this type");
981 merge_type(node
, ctype
);
982 if (ctype
->type
!= SYM_ARRAY
)
985 * Dereferencing a pointer to an array results in a
986 * degenerate dereference: the expression becomes
987 * just a pointer to the entry, and the derefence
992 target
= alloc_symbol(expr
->pos
, SYM_PTR
);
993 target
->bit_size
= bits_in_pointer
;
994 target
->ctype
.alignment
= pointer_alignment
;
995 merge_type(target
, ctype
->ctype
.base_type
);
1000 * When an array is dereferenced, we need to pick
1001 * up the attributes of the original node too..
1003 merge_type(node
, op
->ctype
);
1004 merge_type(node
, ctype
);
1008 node
->bit_size
= target
->bit_size
;
1009 node
->array_size
= target
->array_size
;
1016 * Unary post-ops: x++ and x--
1018 static struct symbol
*evaluate_postop(struct expression
*expr
)
1020 struct expression
*op
= expr
->unop
;
1021 struct symbol
*ctype
= op
->ctype
;
1023 if (!lvalue_expression(expr
->unop
)) {
1024 warn(expr
->pos
, "need lvalue expression for ++/--");
1028 evaluate_assign_to(op
, ctype
);
1030 expr
->ctype
= ctype
;
1034 static struct symbol
*evaluate_preop(struct expression
*expr
)
1036 struct symbol
*ctype
= expr
->unop
->ctype
;
1041 *expr
= *expr
->unop
;
1045 return evaluate_dereference(expr
);
1048 return evaluate_addressof(expr
);
1050 case SPECIAL_INCREMENT
:
1051 case SPECIAL_DECREMENT
:
1053 * From a type evaluation standpoint the pre-ops are
1054 * the same as the postops
1056 return evaluate_postop(expr
);
1059 if (is_safe_type(ctype
))
1060 warn(expr
->pos
, "testing a 'safe expression'");
1061 ctype
= &bool_ctype
;
1067 expr
->ctype
= ctype
;
1071 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
)
1073 struct ptr_list
*head
= (struct ptr_list
*)_list
;
1074 struct ptr_list
*list
= head
;
1080 for (i
= 0; i
< list
->nr
; i
++) {
1081 struct symbol
*sym
= (struct symbol
*) list
->list
[i
];
1083 if (sym
->ident
!= ident
)
1085 *offset
= sym
->offset
;
1088 struct symbol
*ctype
= sym
->ctype
.base_type
;
1092 if (ctype
->type
!= SYM_UNION
&& ctype
->type
!= SYM_STRUCT
)
1094 sub
= find_identifier(ident
, ctype
->symbol_list
, offset
);
1097 *offset
+= sym
->offset
;
1101 } while ((list
= list
->next
) != head
);
1105 static struct expression
*evaluate_offset(struct expression
*expr
, unsigned long offset
)
1107 struct expression
*add
;
1111 /* Create a new add-expression */
1112 add
= alloc_expression(expr
->pos
, EXPR_BINOP
);
1115 add
->right
= alloc_expression(expr
->pos
, EXPR_VALUE
);
1116 add
->right
->ctype
= &int_ctype
;
1117 add
->right
->value
= offset
;
1121 * The ctype of the pointer will be lazily evaluated if
1122 * we ever take the address of this member dereference..
1128 /* structure/union dereference */
1129 static struct symbol
*evaluate_member_dereference(struct expression
*expr
)
1132 struct symbol
*ctype
, *member
;
1133 struct expression
*deref
= expr
->deref
, *add
;
1134 struct ident
*ident
= expr
->member
;
1138 if (!evaluate_expression(deref
))
1141 warn(expr
->pos
, "bad member name");
1145 ctype
= deref
->ctype
;
1146 address_space
= ctype
->ctype
.as
;
1147 mod
= ctype
->ctype
.modifiers
;
1148 if (ctype
->type
== SYM_NODE
) {
1149 ctype
= ctype
->ctype
.base_type
;
1150 address_space
|= ctype
->ctype
.as
;
1151 mod
|= ctype
->ctype
.modifiers
;
1153 if (!lvalue_expression(deref
)) {
1154 warn(deref
->pos
, "expected lvalue for member dereference");
1157 deref
= deref
->unop
;
1158 expr
->deref
= deref
;
1159 if (!ctype
|| (ctype
->type
!= SYM_STRUCT
&& ctype
->type
!= SYM_UNION
)) {
1160 warn(expr
->pos
, "expected structure or union");
1164 member
= find_identifier(ident
, ctype
->symbol_list
, &offset
);
1166 const char *type
= ctype
->type
== SYM_STRUCT
? "struct" : "union";
1167 const char *name
= "<unnamed>";
1170 name
= ctype
->ident
->name
;
1171 namelen
= ctype
->ident
->len
;
1173 warn(expr
->pos
, "no member '%s' in %s %.*s",
1174 show_ident(ident
), type
, namelen
, name
);
1179 * The member needs to take on the address space and modifiers of
1180 * the "parent" type.
1182 member
= convert_to_as_mod(member
, address_space
, mod
);
1183 add
= evaluate_offset(deref
, offset
);
1185 ctype
= member
->ctype
.base_type
;
1186 if (ctype
->type
== SYM_BITFIELD
) {
1187 expr
->type
= EXPR_BITFIELD
;
1188 expr
->bitpos
= member
->bit_offset
;
1189 expr
->nrbits
= member
->fieldwidth
;
1190 expr
->address
= add
;
1192 expr
->type
= EXPR_PREOP
;
1197 expr
->ctype
= member
;
1201 static struct symbol
*evaluate_sizeof(struct expression
*expr
)
1205 if (expr
->cast_type
) {
1206 examine_symbol_type(expr
->cast_type
);
1207 size
= expr
->cast_type
->bit_size
;
1209 if (!evaluate_expression(expr
->cast_expression
))
1211 size
= expr
->cast_expression
->ctype
->bit_size
;
1214 warn(expr
->pos
, "cannot size expression");
1217 expr
->type
= EXPR_VALUE
;
1218 expr
->value
= size
>> 3;
1219 expr
->ctype
= size_t_ctype
;
1220 return size_t_ctype
;
1223 static struct symbol
*evaluate_alignof(struct expression
*expr
)
1225 struct symbol
*type
= expr
->cast_type
;
1228 type
= evaluate_expression(expr
->cast_expression
);
1232 examine_symbol_type(type
);
1233 expr
->type
= EXPR_VALUE
;
1234 expr
->value
= type
->ctype
.alignment
;
1235 expr
->ctype
= size_t_ctype
;
1236 return size_t_ctype
;
1239 static int context_clash(struct symbol
*sym1
, struct symbol
*sym2
)
1241 unsigned long clash
= (sym1
->ctype
.context
^ sym2
->ctype
.context
);
1242 clash
&= (sym1
->ctype
.contextmask
& sym2
->ctype
.contextmask
);
1246 static int evaluate_arguments(struct symbol
*f
, struct symbol
*fn
, struct expression_list
*head
)
1248 struct expression
*expr
;
1249 struct symbol_list
*argument_types
= fn
->arguments
;
1250 struct symbol
*argtype
;
1253 PREPARE_PTR_LIST(argument_types
, argtype
);
1254 FOR_EACH_PTR (head
, expr
) {
1255 struct expression
**p
= THIS_ADDRESS(expr
);
1256 struct symbol
*ctype
, *target
;
1257 ctype
= evaluate_expression(expr
);
1262 if (context_clash(f
, ctype
))
1263 warn(expr
->pos
, "argument %d used in wrong context", i
);
1265 ctype
= degenerate(expr
);
1268 if (!target
&& ctype
->bit_size
< bits_in_int
)
1269 target
= &int_ctype
;
1271 static char where
[30];
1272 examine_symbol_type(target
);
1273 sprintf(where
, "argument %d", i
);
1274 compatible_assignment_types(expr
, target
, p
, ctype
, where
);
1278 NEXT_PTR_LIST(argtype
);
1280 FINISH_PTR_LIST(argtype
);
1284 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
);
1285 static int evaluate_array_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1287 struct expression
*entry
;
1291 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1292 struct expression
**p
= THIS_ADDRESS(entry
);
1294 if (entry
->type
== EXPR_INDEX
) {
1295 current
= entry
->idx_to
;
1298 evaluate_initializer(ctype
, p
, offset
+ current
*(ctype
->bit_size
>>3));
1306 /* A scalar initializer is allowed, and acts pretty much like an array of one */
1307 static int evaluate_scalar_initializer(struct symbol
*ctype
, struct expression
*expr
, unsigned long offset
)
1309 if (offset
|| expression_list_size(expr
->expr_list
) != 1) {
1310 warn(expr
->pos
, "unexpected compound initializer");
1313 return evaluate_array_initializer(ctype
, expr
, 0);
1316 static int evaluate_struct_or_union_initializer(struct symbol
*ctype
, struct expression
*expr
, int multiple
, unsigned long offset
)
1318 struct expression
*entry
;
1321 PREPARE_PTR_LIST(ctype
->symbol_list
, sym
);
1322 FOR_EACH_PTR(expr
->expr_list
, entry
) {
1323 struct expression
**p
= THIS_ADDRESS(entry
);
1325 if (entry
->type
== EXPR_IDENTIFIER
) {
1326 struct ident
*ident
= entry
->expr_ident
;
1327 /* We special-case the "already right place" case */
1328 if (sym
&& sym
->ident
== ident
)
1330 RESET_PTR_LIST(sym
);
1333 warn(entry
->pos
, "unknown named initializer '%s'", show_ident(ident
));
1336 if (sym
->ident
== ident
)
1344 warn(expr
->pos
, "too many initializers for struct/union");
1348 evaluate_initializer(sym
, p
, offset
+ sym
->offset
);
1352 FINISH_PTR_LIST(sym
);
1358 * Initializers are kind of like assignments. Except
1359 * they can be a hell of a lot more complex.
1361 static int evaluate_initializer(struct symbol
*ctype
, struct expression
**ep
, unsigned long offset
)
1363 struct expression
*expr
= *ep
;
1366 * Simple non-structure/array initializers are the simple
1367 * case, and look (and parse) largely like assignments.
1369 if (expr
->type
!= EXPR_INITIALIZER
) {
1371 struct symbol
*rtype
= evaluate_expression(expr
);
1373 struct expression
*pos
;
1375 // FIXME! char array[] = "string" special case
1376 // should _not_ degenerate.
1377 rtype
= degenerate(expr
);
1378 compatible_assignment_types(expr
, ctype
, ep
, rtype
, "initializer");
1379 /* strings are special: char arrays */
1380 if (rtype
->type
== SYM_ARRAY
)
1381 size
= get_expression_value(rtype
->array_size
);
1383 * Don't bother creating a position expression for
1384 * the simple initializer cases that don't need it.
1386 * We need a position if the initializer has a byte
1387 * offset, _or_ if we're initializing a bitfield.
1389 if (offset
|| ctype
->fieldwidth
) {
1390 pos
= alloc_expression(expr
->pos
, EXPR_POS
);
1391 pos
->init_offset
= offset
;
1392 pos
->init_sym
= ctype
;
1393 pos
->init_expr
= *ep
;
1394 pos
->ctype
= expr
->ctype
;
1401 expr
->ctype
= ctype
;
1402 if (ctype
->type
== SYM_NODE
)
1403 ctype
= ctype
->ctype
.base_type
;
1405 switch (ctype
->type
) {
1408 return evaluate_array_initializer(ctype
->ctype
.base_type
, expr
, offset
);
1410 return evaluate_struct_or_union_initializer(ctype
, expr
, 0, offset
);
1412 return evaluate_struct_or_union_initializer(ctype
, expr
, 1, offset
);
1414 return evaluate_scalar_initializer(ctype
, expr
, offset
);
1418 static int get_as(struct symbol
*sym
)
1426 mod
= sym
->ctype
.modifiers
;
1427 if (sym
->type
== SYM_NODE
) {
1428 sym
= sym
->ctype
.base_type
;
1429 as
|= sym
->ctype
.as
;
1430 mod
|= sym
->ctype
.modifiers
;
1433 * You can always throw a value away by casting to
1434 * "void" - that's an implicit "force". Note that
1435 * the same is _not_ true of "void *".
1437 if (sym
== &void_ctype
)
1441 * At least for now, allow casting to a "unsigned long".
1442 * That's how we do things like pointer arithmetic and
1443 * store pointers to registers.
1445 if (sym
== &ulong_ctype
)
1448 if (sym
&& sym
->type
== SYM_PTR
) {
1449 sym
= sym
->ctype
.base_type
;
1450 as
|= sym
->ctype
.as
;
1451 mod
|= sym
->ctype
.modifiers
;
1453 if (mod
& MOD_FORCE
)
1458 static struct symbol
*evaluate_cast(struct expression
*expr
)
1460 struct expression
*target
= expr
->cast_expression
;
1461 struct symbol
*ctype
= examine_symbol_type(expr
->cast_type
);
1463 expr
->ctype
= ctype
;
1464 expr
->cast_type
= ctype
;
1467 * Special case: a cast can be followed by an
1468 * initializer, in which case we need to pass
1469 * the type value down to that initializer rather
1470 * than trying to evaluate it as an expression
1472 * A more complex case is when the initializer is
1473 * dereferenced as part of a post-fix expression.
1474 * We need to produce an expression that can be dereferenced.
1476 if (target
->type
== EXPR_INITIALIZER
) {
1477 struct symbol
*sym
= alloc_symbol(expr
->pos
, SYM_NODE
);
1478 struct expression
*addr
= alloc_expression(expr
->pos
, EXPR_SYMBOL
);
1480 sym
->ctype
.base_type
= ctype
;
1481 sym
->initializer
= expr
->cast_expression
;
1482 evaluate_symbol(sym
);
1484 addr
->ctype
= NULL
; /* Lazy eval */
1487 expr
->type
= EXPR_PREOP
;
1490 expr
->ctype
= ctype
;
1494 evaluate_expression(target
);
1497 if (!get_as(ctype
) && get_as(target
->ctype
) > 0)
1498 warn(expr
->pos
, "cast removes address space of expression");
1501 * Casts of constant values are special: they
1502 * can be NULL, and thus need to be simplified
1505 if (target
->type
== EXPR_VALUE
)
1506 cast_value(expr
, ctype
, target
, target
->ctype
);
1512 * Evaluate a call expression with a symbol. This
1513 * should expand inline functions, and evaluate
1516 static int evaluate_symbol_call(struct expression
*expr
)
1518 struct expression
*fn
= expr
->fn
;
1519 struct symbol
*ctype
= fn
->ctype
;
1521 if (fn
->type
!= EXPR_PREOP
)
1524 if (ctype
->op
&& ctype
->op
->evaluate
)
1525 return ctype
->op
->evaluate(expr
);
1527 if (ctype
->ctype
.modifiers
& MOD_INLINE
) {
1529 struct symbol
*curr
= current_fn
;
1530 unsigned long context
= current_context
;
1531 unsigned long mask
= current_contextmask
;
1533 current_context
|= ctype
->ctype
.context
;
1534 current_contextmask
|= ctype
->ctype
.contextmask
;
1535 current_fn
= ctype
->ctype
.base_type
;
1536 examine_fn_arguments(current_fn
);
1538 ret
= inline_function(expr
, ctype
);
1540 /* restore the old function context */
1542 current_context
= context
;
1543 current_contextmask
= mask
;
1550 static struct symbol
*evaluate_call(struct expression
*expr
)
1553 struct symbol
*ctype
, *sym
;
1554 struct expression
*fn
= expr
->fn
;
1555 struct expression_list
*arglist
= expr
->args
;
1557 if (!evaluate_expression(fn
))
1559 sym
= ctype
= fn
->ctype
;
1560 if (ctype
->type
== SYM_NODE
)
1561 ctype
= ctype
->ctype
.base_type
;
1562 if (ctype
->type
== SYM_PTR
|| ctype
->type
== SYM_ARRAY
)
1563 ctype
= ctype
->ctype
.base_type
;
1564 if (!evaluate_arguments(sym
, ctype
, arglist
))
1566 if (ctype
->type
!= SYM_FN
) {
1567 warn(expr
->pos
, "not a function %s", show_ident(sym
->ident
));
1570 args
= expression_list_size(expr
->args
);
1571 fnargs
= symbol_list_size(ctype
->arguments
);
1573 warn(expr
->pos
, "not enough arguments for function %s", show_ident(sym
->ident
));
1574 if (args
> fnargs
&& !ctype
->variadic
)
1575 warn(expr
->pos
, "too many arguments for function %s", show_ident(sym
->ident
));
1576 if (sym
->type
== SYM_NODE
) {
1577 if (evaluate_symbol_call(expr
))
1580 expr
->ctype
= ctype
->ctype
.base_type
;
1584 struct symbol
*evaluate_expression(struct expression
*expr
)
1591 switch (expr
->type
) {
1593 warn(expr
->pos
, "value expression without a type");
1596 return evaluate_string(expr
);
1598 return evaluate_symbol_expression(expr
);
1600 if (!evaluate_expression(expr
->left
))
1602 if (!evaluate_expression(expr
->right
))
1604 return evaluate_binop(expr
);
1606 return evaluate_logical(expr
);
1608 if (!evaluate_expression(expr
->left
))
1610 if (!evaluate_expression(expr
->right
))
1612 return evaluate_comma(expr
);
1614 if (!evaluate_expression(expr
->left
))
1616 if (!evaluate_expression(expr
->right
))
1618 return evaluate_compare(expr
);
1619 case EXPR_ASSIGNMENT
:
1620 if (!evaluate_expression(expr
->left
))
1622 if (!evaluate_expression(expr
->right
))
1624 return evaluate_assignment(expr
);
1626 if (!evaluate_expression(expr
->unop
))
1628 return evaluate_preop(expr
);
1630 if (!evaluate_expression(expr
->unop
))
1632 return evaluate_postop(expr
);
1634 return evaluate_cast(expr
);
1636 return evaluate_sizeof(expr
);
1638 return evaluate_alignof(expr
);
1640 return evaluate_member_dereference(expr
);
1642 return evaluate_call(expr
);
1644 warn(expr
->pos
, "bitfield generated by parser");
1646 case EXPR_CONDITIONAL
:
1647 if (!evaluate_conditional(expr
->conditional
))
1649 if (!evaluate_expression(expr
->cond_false
))
1651 if (expr
->cond_true
&& !evaluate_expression(expr
->cond_true
))
1653 return evaluate_conditional_expression(expr
);
1654 case EXPR_STATEMENT
:
1655 expr
->ctype
= evaluate_statement(expr
->statement
);
1659 expr
->ctype
= &ptr_ctype
;
1663 /* Evaluate the type of the symbol .. */
1664 evaluate_symbol(expr
->symbol
);
1665 /* .. but the type of the _expression_ is a "type" */
1666 expr
->ctype
= &type_ctype
;
1669 /* These can not exist as stand-alone expressions */
1670 case EXPR_INITIALIZER
:
1671 case EXPR_IDENTIFIER
:
1674 warn(expr
->pos
, "internal front-end error: initializer in expression");
1680 void check_duplicates(struct symbol
*sym
)
1682 struct symbol
*next
= sym
;
1684 while ((next
= next
->same_symbol
) != NULL
) {
1685 const char *typediff
;
1686 evaluate_symbol(next
);
1687 typediff
= type_difference(sym
, next
, 0, 0);
1689 warn(sym
->pos
, "symbol '%s' redeclared with different type (originally declared at %s:%d) - %s",
1690 show_ident(sym
->ident
),
1691 input_streams
[next
->pos
.stream
].name
, next
->pos
.line
, typediff
);
1697 struct symbol
*evaluate_symbol(struct symbol
*sym
)
1699 struct symbol
*base_type
;
1704 sym
= examine_symbol_type(sym
);
1705 base_type
= sym
->ctype
.base_type
;
1709 /* Evaluate the initializers */
1710 if (sym
->initializer
) {
1711 int count
= evaluate_initializer(sym
, &sym
->initializer
, 0);
1712 if (base_type
->type
== SYM_ARRAY
&& !base_type
->array_size
) {
1713 int bit_size
= count
* base_type
->ctype
.base_type
->bit_size
;
1714 base_type
->array_size
= alloc_const_expression(sym
->pos
, count
);
1715 base_type
->bit_size
= bit_size
;
1716 sym
->array_size
= base_type
->array_size
;
1717 sym
->bit_size
= bit_size
;
1721 /* And finally, evaluate the body of the symbol too */
1722 if (base_type
->type
== SYM_FN
) {
1723 examine_fn_arguments(base_type
);
1724 if (base_type
->stmt
) {
1725 current_fn
= base_type
;
1726 current_contextmask
= sym
->ctype
.contextmask
;
1727 current_context
= sym
->ctype
.context
;
1728 evaluate_statement(base_type
->stmt
);
1735 struct symbol
*evaluate_return_expression(struct statement
*stmt
)
1737 struct expression
*expr
= stmt
->expression
;
1738 struct symbol
*ctype
, *fntype
;
1740 evaluate_expression(expr
);
1741 ctype
= degenerate(expr
);
1742 fntype
= current_fn
->ctype
.base_type
;
1743 if (!fntype
|| fntype
== &void_ctype
) {
1744 if (expr
&& ctype
!= &void_ctype
)
1745 warn(expr
->pos
, "return expression in %s function", fntype
?"void":"typeless");
1750 warn(stmt
->pos
, "return with no return value");
1755 compatible_assignment_types(expr
, fntype
, &stmt
->expression
, ctype
, "return expression");
1759 static void evaluate_if_statement(struct statement
*stmt
)
1761 struct expression
*expr
= stmt
->if_conditional
;
1762 struct symbol
*ctype
;
1767 ctype
= evaluate_conditional(expr
);
1771 evaluate_statement(stmt
->if_true
);
1772 evaluate_statement(stmt
->if_false
);
1775 struct symbol
*evaluate_statement(struct statement
*stmt
)
1780 switch (stmt
->type
) {
1782 return evaluate_return_expression(stmt
);
1784 case STMT_EXPRESSION
:
1785 evaluate_expression(stmt
->expression
);
1786 return degenerate(stmt
->expression
);
1788 case STMT_COMPOUND
: {
1789 struct statement
*s
;
1790 struct symbol
*type
= NULL
;
1793 /* Evaluate each symbol in the compound statement */
1794 FOR_EACH_PTR(stmt
->syms
, sym
) {
1795 evaluate_symbol(sym
);
1797 evaluate_symbol(stmt
->ret
);
1800 * Then, evaluate each statement, making the type of the
1801 * compound statement be the type of the last statement
1804 FOR_EACH_PTR(stmt
->stmts
, s
) {
1805 type
= evaluate_statement(s
);
1810 evaluate_if_statement(stmt
);
1813 evaluate_conditional(stmt
->iterator_pre_condition
);
1814 evaluate_conditional(stmt
->iterator_post_condition
);
1815 evaluate_statement(stmt
->iterator_pre_statement
);
1816 evaluate_statement(stmt
->iterator_statement
);
1817 evaluate_statement(stmt
->iterator_post_statement
);
1820 evaluate_expression(stmt
->switch_expression
);
1821 evaluate_statement(stmt
->switch_statement
);
1824 evaluate_expression(stmt
->case_expression
);
1825 evaluate_expression(stmt
->case_to
);
1826 evaluate_statement(stmt
->case_statement
);
1829 evaluate_statement(stmt
->label_statement
);
1832 evaluate_expression(stmt
->goto_expression
);
1837 /* FIXME! Do the asm parameter evaluation! */