1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization.
26 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
27 and to process initializations in declarations (since they work
28 like a strange sort of assignment). */
36 extern char *index ();
37 extern char *rindex ();
39 int mark_addressable ();
40 static tree
convert_for_assignment ();
41 static void warn_for_assignment ();
42 static int function_types_compatible_p ();
43 static int type_lists_compatible_p ();
44 int self_promoting_args_p ();
45 static int self_promoting_type_p ();
46 static int comp_target_types ();
47 static tree
pointer_int_sum ();
48 static tree
pointer_diff ();
49 static tree
convert_sequence ();
50 static tree
unary_complex_lvalue ();
51 static tree
process_init_constructor ();
52 static tree
convert_arguments ();
53 static char *get_spelling ();
55 static void pedantic_lvalue_warning ();
56 tree
truthvalue_conversion ();
57 void incomplete_type_error ();
58 void readonly_warning ();
60 /* Do `exp = require_complete_type (exp);' to make sure exp
61 does not have an incomplete type. (That includes void types.) */
64 require_complete_type (value
)
67 tree type
= TREE_TYPE (value
);
69 /* First, detect a valid value with a complete type. */
70 if (TYPE_SIZE (type
) != 0
71 && type
!= void_type_node
)
74 incomplete_type_error (value
, type
);
75 return error_mark_node
;
78 /* Print an error message for invalid use of an incomplete type.
79 VALUE is the expression that was used (or 0 if that isn't known)
80 and TYPE is the type that was invalid. */
83 incomplete_type_error (value
, type
)
89 /* Avoid duplicate error message. */
90 if (TREE_CODE (type
) == ERROR_MARK
)
93 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
94 || TREE_CODE (value
) == PARM_DECL
))
95 error ("`%s' has an incomplete type",
96 IDENTIFIER_POINTER (DECL_NAME (value
)));
100 /* We must print an error message. Be clever about what it says. */
102 switch (TREE_CODE (type
))
105 errmsg
= "invalid use of undefined type `struct %s'";
109 errmsg
= "invalid use of undefined type `union %s'";
113 errmsg
= "invalid use of undefined type `enum %s'";
117 error ("invalid use of void expression");
121 if (TYPE_DOMAIN (type
))
123 type
= TREE_TYPE (type
);
126 error ("invalid use of array with unspecified bounds");
133 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
134 error (errmsg
, IDENTIFIER_POINTER (TYPE_NAME (type
)));
136 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
137 error ("invalid use of incomplete typedef `%s'",
138 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
))));
142 /* Return a variant of TYPE which has all the type qualifiers of LIKE
143 as well as those of TYPE. */
146 qualify_type (type
, like
)
149 int constflag
= TYPE_READONLY (type
) || TYPE_READONLY (like
);
150 int volflag
= TYPE_VOLATILE (type
) || TYPE_VOLATILE (like
);
151 return c_build_type_variant (type
, constflag
, volflag
);
154 /* Return the common type of two types.
155 We assume that comptypes has already been done and returned 1;
156 if that isn't so, this may crash.
158 This is the type for the result of most arithmetic operations
159 if the operands have the given two types.
161 We do not deal with enumeral types here because they have already been
162 converted to integer types. */
168 register enum tree_code code1
;
169 register enum tree_code code2
;
171 /* Save time if the two types are the same. */
173 if (t1
== t2
) return t1
;
175 /* If one type is nonsense, use the other. */
176 if (t1
== error_mark_node
)
178 if (t2
== error_mark_node
)
181 /* Treat an enum type as the unsigned integer type of the same width. */
183 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
184 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
185 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
186 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
188 code1
= TREE_CODE (t1
);
189 code2
= TREE_CODE (t2
);
195 /* If only one is real, use it as the result. */
197 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
200 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
203 /* Both real or both integers; use the one with greater precision. */
205 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
207 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
210 /* Same precision. Prefer longs to ints even when same size. */
212 if (t1
== long_unsigned_type_node
213 || t2
== long_unsigned_type_node
)
214 return long_unsigned_type_node
;
216 if (t1
== long_integer_type_node
217 || t2
== long_integer_type_node
)
219 /* But preserve unsignedness from the other type,
220 since long cannot hold all the values of an unsigned int. */
221 if (TREE_UNSIGNED (t1
) || TREE_UNSIGNED (t2
))
222 return long_unsigned_type_node
;
223 return long_integer_type_node
;
226 /* Otherwise prefer the unsigned one. */
228 if (TREE_UNSIGNED (t1
))
234 /* For two pointers, do this recursively on the target type,
235 and combine the qualifiers of the two types' targets. */
237 tree target
= common_type (TYPE_MAIN_VARIANT (TREE_TYPE (t1
)),
238 TYPE_MAIN_VARIANT (TREE_TYPE (t2
)));
240 = TYPE_READONLY (TREE_TYPE (t1
)) || TYPE_READONLY (TREE_TYPE (t2
));
242 = TYPE_VOLATILE (TREE_TYPE (t1
)) || TYPE_VOLATILE (TREE_TYPE (t2
));
243 return build_pointer_type (c_build_type_variant (target
, constp
, volatilep
));
246 return build_pointer_type (common_type (TREE_TYPE (t1
), TREE_TYPE (t2
)));
250 tree elt
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
251 /* Save space: see if the result is identical to one of the args. */
252 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
254 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
256 /* Merge the element types, and have a size if either arg has one. */
257 return build_array_type (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
261 /* Function types: prefer the one that specified arg types.
262 If both do, merge the arg types. Also merge the return types. */
264 tree valtype
= common_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
265 tree p1
= TYPE_ARG_TYPES (t1
);
266 tree p2
= TYPE_ARG_TYPES (t2
);
271 /* Save space: see if the result is identical to one of the args. */
272 if (valtype
== TREE_TYPE (t1
) && ! TYPE_ARG_TYPES (t2
))
274 if (valtype
== TREE_TYPE (t2
) && ! TYPE_ARG_TYPES (t1
))
277 /* Simple way if one arg fails to specify argument types. */
278 if (TYPE_ARG_TYPES (t1
) == 0)
279 return build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
280 if (TYPE_ARG_TYPES (t2
) == 0)
281 return build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
283 /* If both args specify argument types, we must merge the two
284 lists, argument by argument. */
286 len
= list_length (p1
);
289 for (i
= 0; i
< len
; i
++)
290 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
295 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
297 /* A null type means arg type is not specified.
298 Take whatever the other function type has. */
299 if (TREE_VALUE (p1
) == 0)
301 TREE_VALUE (n
) = TREE_VALUE (p2
);
304 if (TREE_VALUE (p2
) == 0)
306 TREE_VALUE (n
) = TREE_VALUE (p1
);
310 /* Given wait (union {union wait *u; int *i} *)
311 and wait (union wait *),
312 prefer union wait * as type of parm. */
313 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
314 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
317 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
318 memb
; memb
= TREE_CHAIN (memb
))
319 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p2
)))
321 TREE_VALUE (n
) = TREE_VALUE (p2
);
323 pedwarn ("function types not truly compatible in ANSI C");
327 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
328 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
331 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
332 memb
; memb
= TREE_CHAIN (memb
))
333 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (p1
)))
335 TREE_VALUE (n
) = TREE_VALUE (p1
);
337 pedwarn ("function types not truly compatible in ANSI C");
341 TREE_VALUE (n
) = common_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
345 return build_function_type (valtype
, newargs
);
354 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
355 or various other operations. Return 2 if they are compatible
356 but a warning may be needed if you use them together. */
359 comptypes (type1
, type2
)
362 register tree t1
= type1
;
363 register tree t2
= type2
;
365 /* Suppress errors caused by previously reported errors. */
367 if (t1
== t2
|| TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
370 /* Treat an enum type as the unsigned integer type of the same width. */
372 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
373 t1
= type_for_size (TYPE_PRECISION (t1
), 1);
374 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
375 t2
= type_for_size (TYPE_PRECISION (t2
), 1);
380 /* Different classes of types can't be compatible. */
382 if (TREE_CODE (t1
) != TREE_CODE (t2
)) return 0;
384 /* Qualifiers must match. */
386 if (TYPE_READONLY (t1
) != TYPE_READONLY (t2
))
388 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
391 /* If generating auxiliary info, allow for two different type nodes which
392 have essentially the same definition. */
394 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
397 switch (TREE_CODE (t1
))
400 return (TREE_TYPE (t1
) == TREE_TYPE (t2
)
401 ? 1 : comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
)));
404 return function_types_compatible_p (t1
, t2
);
408 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
410 tree d1
= TYPE_DOMAIN (t1
);
411 tree d2
= TYPE_DOMAIN (t2
);
413 /* Target types must match incl. qualifiers. */
414 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
415 && 0 == (val
= comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))))
418 /* Sizes must match unless one is missing or variable. */
419 if (d1
== 0 || d2
== 0 || d1
== d2
420 || TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
421 || TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
422 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
423 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
)
426 return (((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1
))
427 == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2
)))
428 && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1
))
429 == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2
)))
430 && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1
))
431 == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2
)))
432 && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1
))
433 == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2
))))
438 return maybe_objc_comptypes (t1
, t2
);
443 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
444 ignoring their qualifiers. */
447 comp_target_types (ttl
, ttr
)
450 int val
= comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl
)),
451 TYPE_MAIN_VARIANT (TREE_TYPE (ttr
)));
452 if (val
== 2 && pedantic
)
453 pedwarn ("types are not quite compatible");
457 /* Subroutines of `comptypes'. */
459 /* Return 1 if two function types F1 and F2 are compatible.
460 If either type specifies no argument types,
461 the other must specify a fixed number of self-promoting arg types.
462 Otherwise, if one type specifies only the number of arguments,
463 the other must specify that number of self-promoting arg types.
464 Otherwise, the argument types must match. */
467 function_types_compatible_p (f1
, f2
)
471 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
475 if (!(TREE_TYPE (f1
) == TREE_TYPE (f2
)
476 || (val
= comptypes (TREE_TYPE (f1
), TREE_TYPE (f2
)))))
479 args1
= TYPE_ARG_TYPES (f1
);
480 args2
= TYPE_ARG_TYPES (f2
);
482 /* An unspecified parmlist matches any specified parmlist
483 whose argument types don't need default promotions. */
487 if (!self_promoting_args_p (args2
))
489 /* If one of these types comes from a non-prototype fn definition,
490 compare that with the other type's arglist.
491 If they don't match, ask for a warning (but no error). */
492 if (TYPE_ACTUAL_ARG_TYPES (f1
)
493 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
)))
499 if (!self_promoting_args_p (args1
))
501 if (TYPE_ACTUAL_ARG_TYPES (f2
)
502 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
)))
507 /* Both types have argument lists: compare them and propagate results. */
508 val1
= type_lists_compatible_p (args1
, args2
);
509 return val1
!= 1 ? val1
: val
;
512 /* Check two lists of types for compatibility,
513 returning 0 for incompatible, 1 for compatible,
514 or 2 for compatible with warning. */
517 type_lists_compatible_p (args1
, args2
)
520 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
526 if (args1
== 0 && args2
== 0)
528 /* If one list is shorter than the other,
529 they fail to match. */
530 if (args1
== 0 || args2
== 0)
532 /* A null pointer instead of a type
533 means there is supposed to be an argument
534 but nothing is specified about what type it has.
535 So match anything that self-promotes. */
536 if (TREE_VALUE (args1
) == 0)
538 if (! self_promoting_type_p (TREE_VALUE (args2
)))
541 else if (TREE_VALUE (args2
) == 0)
543 if (! self_promoting_type_p (TREE_VALUE (args1
)))
546 else if (! (newval
= comptypes (TREE_VALUE (args1
), TREE_VALUE (args2
))))
548 /* Allow wait (union {union wait *u; int *i} *)
549 and wait (union wait *) to be compatible. */
550 if (TREE_CODE (TREE_VALUE (args1
)) == UNION_TYPE
551 && TYPE_NAME (TREE_VALUE (args1
)) == 0
552 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1
))) == INTEGER_CST
553 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1
)),
554 TYPE_SIZE (TREE_VALUE (args2
))))
557 for (memb
= TYPE_FIELDS (TREE_VALUE (args1
));
558 memb
; memb
= TREE_CHAIN (memb
))
559 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args2
)))
564 else if (TREE_CODE (TREE_VALUE (args2
)) == UNION_TYPE
565 && TYPE_NAME (TREE_VALUE (args2
)) == 0
566 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2
))) == INTEGER_CST
567 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2
)),
568 TYPE_SIZE (TREE_VALUE (args1
))))
571 for (memb
= TYPE_FIELDS (TREE_VALUE (args2
));
572 memb
; memb
= TREE_CHAIN (memb
))
573 if (comptypes (TREE_TYPE (memb
), TREE_VALUE (args1
)))
582 /* comptypes said ok, but record if it said to warn. */
586 args1
= TREE_CHAIN (args1
);
587 args2
= TREE_CHAIN (args2
);
591 /* Return 1 if PARMS specifies a fixed number of parameters
592 and none of their types is affected by default promotions. */
595 self_promoting_args_p (parms
)
599 for (t
= parms
; t
; t
= TREE_CHAIN (t
))
601 register tree type
= TREE_VALUE (t
);
603 if (TREE_CHAIN (t
) == 0 && type
!= void_type_node
)
606 if (type
== float_type_node
)
610 && TREE_CODE (type
) == INTEGER_TYPE
611 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
617 /* Return 1 if TYPE is not affected by default promotions. */
620 self_promoting_type_p (type
)
623 if (type
== float_type_node
)
626 if (TREE_CODE (type
) == INTEGER_TYPE
627 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
633 /* Return an unsigned type the same as TYPE in other respects. */
639 if (type
== signed_char_type_node
|| type
== char_type_node
)
640 return unsigned_char_type_node
;
641 if (type
== integer_type_node
)
642 return unsigned_type_node
;
643 if (type
== short_integer_type_node
)
644 return short_unsigned_type_node
;
645 if (type
== long_integer_type_node
)
646 return long_unsigned_type_node
;
647 if (type
== long_long_integer_type_node
)
648 return long_long_unsigned_type_node
;
652 /* Return a signed type the same as TYPE in other respects. */
658 if (type
== unsigned_char_type_node
|| type
== char_type_node
)
659 return signed_char_type_node
;
660 if (type
== unsigned_type_node
)
661 return integer_type_node
;
662 if (type
== short_unsigned_type_node
)
663 return short_integer_type_node
;
664 if (type
== long_unsigned_type_node
)
665 return long_integer_type_node
;
666 if (type
== long_long_unsigned_type_node
)
667 return long_long_integer_type_node
;
671 /* Return a type the same as TYPE except unsigned or
672 signed according to UNSIGNEDP. */
675 signed_or_unsigned_type (unsignedp
, type
)
679 if (TREE_CODE (type
) != INTEGER_TYPE
)
681 if (TYPE_PRECISION (type
) == TYPE_PRECISION (signed_char_type_node
))
682 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
683 if (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
684 return unsignedp
? unsigned_type_node
: integer_type_node
;
685 if (TYPE_PRECISION (type
) == TYPE_PRECISION (short_integer_type_node
))
686 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
687 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_integer_type_node
))
688 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
689 if (TYPE_PRECISION (type
) == TYPE_PRECISION (long_long_integer_type_node
))
690 return (unsignedp
? long_long_unsigned_type_node
691 : long_long_integer_type_node
);
695 /* Compute the value of the `sizeof' operator. */
701 enum tree_code code
= TREE_CODE (type
);
703 if (code
== FUNCTION_TYPE
)
705 if (pedantic
|| warn_pointer_arith
)
706 pedwarn ("sizeof applied to a function type");
709 if (code
== VOID_TYPE
)
711 if (pedantic
|| warn_pointer_arith
)
712 pedwarn ("sizeof applied to a void type");
715 if (code
== ERROR_MARK
)
717 if (TYPE_SIZE (type
) == 0)
719 error ("sizeof applied to an incomplete type");
723 /* Convert in case a char is more than one unit. */
724 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
725 size_int (TYPE_PRECISION (char_type_node
)));
729 c_sizeof_nowarn (type
)
732 enum tree_code code
= TREE_CODE (type
);
734 if (code
== FUNCTION_TYPE
736 || code
== ERROR_MARK
)
738 if (TYPE_SIZE (type
) == 0)
741 /* Convert in case a char is more than one unit. */
742 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
743 size_int (TYPE_PRECISION (char_type_node
)));
746 /* Compute the size to increment a pointer by. */
749 c_size_in_bytes (type
)
752 enum tree_code code
= TREE_CODE (type
);
754 if (code
== FUNCTION_TYPE
)
756 if (code
== VOID_TYPE
)
758 if (code
== ERROR_MARK
)
760 if (TYPE_SIZE (type
) == 0)
762 error ("arithmetic on pointer to an incomplete type");
766 /* Convert in case a char is more than one unit. */
767 return size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (type
),
768 size_int (BITS_PER_UNIT
));
771 /* Implement the __alignof keyword: Return the minimum required
772 alignment of TYPE, measured in bytes. */
778 enum tree_code code
= TREE_CODE (type
);
780 if (code
== FUNCTION_TYPE
)
781 return size_int (FUNCTION_BOUNDARY
/ BITS_PER_UNIT
);
783 if (code
== VOID_TYPE
|| code
== ERROR_MARK
)
786 return size_int (TYPE_ALIGN (type
) / BITS_PER_UNIT
);
789 /* Print a warning if a constant expression had overflow in folding.
790 This doesn't really work--it is waiting for changes in fold. */
793 constant_expression_warning (value
)
796 if (TREE_CODE (value
) == NON_LVALUE_EXPR
&& TREE_CONSTANT_OVERFLOW (value
))
797 pedwarn ("overflow in constant expression");
800 /* Implement the __alignof keyword: Return the minimum required
801 alignment of EXPR, measured in bytes. For VAR_DECL's and
802 FIELD_DECL's return DECL_ALIGN (which can be set from an
803 "aligned" __attribute__ specification). */
805 c_alignof_expr (expr
)
808 if (TREE_CODE (expr
) == VAR_DECL
)
809 return size_int (DECL_ALIGN (expr
) / BITS_PER_UNIT
);
811 if (TREE_CODE (expr
) == COMPONENT_REF
812 && DECL_BIT_FIELD (TREE_OPERAND (expr
, 1)))
814 error ("`__alignof' applied to a bit-field");
817 else if (TREE_CODE (expr
) == COMPONENT_REF
818 && TREE_CODE (TREE_OPERAND (expr
, 1)) == FIELD_DECL
)
819 return size_int (DECL_ALIGN (TREE_OPERAND (expr
, 1)) / BITS_PER_UNIT
);
821 if (TREE_CODE (expr
) == INDIRECT_REF
)
823 tree t
= TREE_OPERAND (expr
, 0);
825 int bestalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
827 while (TREE_CODE (t
) == NOP_EXPR
828 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == POINTER_TYPE
)
832 t
= TREE_OPERAND (t
, 0);
833 thisalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
834 if (thisalign
> bestalign
)
835 best
= t
, bestalign
= thisalign
;
837 return c_alignof (TREE_TYPE (TREE_TYPE (best
)));
840 return c_alignof (TREE_TYPE (expr
));
842 /* Return either DECL or its known constant value (if it has one). */
845 decl_constant_value (decl
)
848 if (! TREE_PUBLIC (decl
)
849 /* Don't change a variable array bound or initial value to a constant
850 in a place where a variable is invalid. */
851 && current_function_decl
!= 0
853 && ! TREE_THIS_VOLATILE (decl
)
854 && DECL_INITIAL (decl
) != 0
855 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
856 /* This is invalid if initial value is not constant.
857 If it has either a function call, a memory reference,
858 or a variable, then re-evaluating it could give different results. */
859 && TREE_CONSTANT (DECL_INITIAL (decl
))
860 /* Check for cases where this is sub-optimal, even though valid. */
861 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
862 && DECL_MODE (decl
) != BLKmode
)
863 return DECL_INITIAL (decl
);
867 /* Perform default promotions for C data used in expressions.
868 Arrays and functions are converted to pointers;
869 enumeral types or short or char, to int.
870 In addition, manifest constants symbols are replaced by their values. */
873 default_conversion (exp
)
876 register tree type
= TREE_TYPE (exp
);
877 register enum tree_code code
= TREE_CODE (type
);
879 /* Constants can be used directly unless they're not loadable. */
880 if (TREE_CODE (exp
) == CONST_DECL
)
881 exp
= DECL_INITIAL (exp
);
882 /* Replace a nonvolatile const static variable with its value. */
884 && TREE_CODE (exp
) == VAR_DECL
885 && TREE_READONLY (exp
)
886 && DECL_MODE (exp
) != BLKmode
)
888 exp
= decl_constant_value (exp
);
889 type
= TREE_TYPE (exp
);
892 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
894 /* Do not use STRIP_NOPS here! It will remove conversions from pointer
895 to integer and cause infinite recursion. */
896 while (TREE_CODE (exp
) == NON_LVALUE_EXPR
897 || (TREE_CODE (exp
) == NOP_EXPR
898 && TREE_TYPE (TREE_OPERAND (exp
, 0)) == TREE_TYPE (exp
)))
899 exp
= TREE_OPERAND (exp
, 0);
901 /* Normally convert enums to int,
902 but convert wide enums to something wider. */
903 if (code
== ENUMERAL_TYPE
)
905 type
= type_for_size (MAX (TYPE_PRECISION (type
),
906 TYPE_PRECISION (integer_type_node
)),
907 (flag_traditional
&& TREE_UNSIGNED (type
)));
908 return convert (type
, exp
);
911 if (code
== INTEGER_TYPE
912 && (TYPE_PRECISION (type
)
913 < TYPE_PRECISION (integer_type_node
)))
915 /* Traditionally, unsignedness is preserved in default promotions. */
916 if (flag_traditional
&& TREE_UNSIGNED (type
))
917 return convert (unsigned_type_node
, exp
);
918 return convert (integer_type_node
, exp
);
920 if (flag_traditional
&& type
== float_type_node
)
921 return convert (double_type_node
, exp
);
922 if (code
== VOID_TYPE
)
924 error ("void value not ignored as it ought to be");
925 return error_mark_node
;
927 if (code
== FUNCTION_TYPE
)
929 return build_unary_op (ADDR_EXPR
, exp
, 0);
931 if (code
== ARRAY_TYPE
)
934 tree restype
= TREE_TYPE (type
);
937 if (TREE_CODE (exp
) == INDIRECT_REF
)
938 return convert (TYPE_POINTER_TO (restype
),
939 TREE_OPERAND (exp
, 0));
941 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
943 tree op1
= default_conversion (TREE_OPERAND (exp
, 1));
944 return build (COMPOUND_EXPR
, TREE_TYPE (op1
),
945 TREE_OPERAND (exp
, 0), op1
);
949 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
951 error ("invalid use of non-lvalue array");
952 return error_mark_node
;
955 if (TYPE_READONLY (type
) || TYPE_VOLATILE (type
))
956 restype
= c_build_type_variant (restype
, TYPE_READONLY (type
),
957 TYPE_VOLATILE (type
));
959 ptrtype
= build_pointer_type (restype
);
961 if (TREE_CODE (exp
) == VAR_DECL
)
963 /* ??? This is not really quite correct
964 in that the type of the operand of ADDR_EXPR
965 is not the target type of the type of the ADDR_EXPR itself.
966 Question is, can this lossage be avoided? */
967 adr
= build1 (ADDR_EXPR
, ptrtype
, exp
);
968 if (mark_addressable (exp
) == 0)
969 return error_mark_node
;
970 TREE_CONSTANT (adr
) = staticp (exp
);
971 TREE_SIDE_EFFECTS (adr
) = 0; /* Default would be, same as EXP. */
974 /* This way is better for a COMPONENT_REF since it can
975 simplify the offset for a component. */
976 adr
= build_unary_op (ADDR_EXPR
, exp
, 1);
977 return convert (ptrtype
, adr
);
982 /* Make an expression to refer to the COMPONENT field of
983 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
986 build_component_ref (datum
, component
)
987 tree datum
, component
;
989 register tree type
= TREE_TYPE (datum
);
990 register enum tree_code code
= TREE_CODE (type
);
991 register tree field
= NULL
;
994 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
995 unless we are not to support things not strictly ANSI. */
996 switch (TREE_CODE (datum
))
1000 tree value
= build_component_ref (TREE_OPERAND (datum
, 1), component
);
1001 pedantic_lvalue_warning (COMPOUND_EXPR
);
1002 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1003 TREE_OPERAND (datum
, 0), value
);
1006 pedantic_lvalue_warning (COND_EXPR
);
1007 return build_conditional_expr
1008 (TREE_OPERAND (datum
, 0),
1009 build_component_ref (TREE_OPERAND (datum
, 1), component
),
1010 build_component_ref (TREE_OPERAND (datum
, 2), component
));
1013 /* See if there is a field or component with name COMPONENT. */
1015 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
1017 if (TYPE_SIZE (type
) == 0)
1019 incomplete_type_error (NULL_TREE
, type
);
1020 return error_mark_node
;
1023 /* Look up component name in the structure type definition.
1025 If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1026 to the field elements. Use a binary search on this array to quickly
1027 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1028 will always be set for structures which have many elements. */
1030 if (TYPE_LANG_SPECIFIC (type
))
1033 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->elts
[0];
1035 field
= TYPE_FIELDS (type
);
1037 top
= TYPE_LANG_SPECIFIC (type
)->len
;
1038 while (top
- bot
> 1)
1042 half
= (top
- bot
+ 1) >> 1;
1043 field
= field_array
[bot
+half
];
1044 cmp
= (long)DECL_NAME (field
) - (long)component
;
1053 if (DECL_NAME (field_array
[bot
]) == component
)
1054 field
= field_array
[bot
];
1055 else if (DECL_NAME (field
) != component
)
1060 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1062 if (DECL_NAME (field
) == component
)
1069 error (code
== RECORD_TYPE
1070 ? "structure has no member named `%s'"
1071 : "union has no member named `%s'",
1072 IDENTIFIER_POINTER (component
));
1073 return error_mark_node
;
1075 if (TREE_TYPE (field
) == error_mark_node
)
1076 return error_mark_node
;
1078 ref
= build (COMPONENT_REF
, TREE_TYPE (field
), datum
, field
);
1080 if (TREE_READONLY (datum
) || TREE_READONLY (field
))
1081 TREE_READONLY (ref
) = 1;
1082 if (TREE_THIS_VOLATILE (datum
) || TREE_THIS_VOLATILE (field
))
1083 TREE_THIS_VOLATILE (ref
) = 1;
1087 else if (code
!= ERROR_MARK
)
1088 error ("request for member `%s' in something not a structure or union",
1089 IDENTIFIER_POINTER (component
));
1091 return error_mark_node
;
1094 /* Given an expression PTR for a pointer, return an expression
1095 for the value pointed to.
1096 ERRORSTRING is the name of the operator to appear in error messages. */
1099 build_indirect_ref (ptr
, errorstring
)
1103 register tree pointer
= default_conversion (ptr
);
1104 register tree type
= TREE_TYPE (pointer
);
1106 if (TREE_CODE (type
) == POINTER_TYPE
)
1107 if (TREE_CODE (pointer
) == ADDR_EXPR
1108 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
1109 == TREE_TYPE (type
)))
1110 return TREE_OPERAND (pointer
, 0);
1113 tree t
= TREE_TYPE (type
);
1114 register tree ref
= build1 (INDIRECT_REF
,
1115 TYPE_MAIN_VARIANT (t
), pointer
);
1117 if (TREE_CODE (t
) == VOID_TYPE
1118 || (TYPE_SIZE (t
) == 0 && TREE_CODE (t
) != ARRAY_TYPE
))
1120 error ("dereferencing pointer to incomplete type");
1121 return error_mark_node
;
1124 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1125 so that we get the proper error message if the result is used
1126 to assign to. Also, &* is supposed to be a no-op.
1127 And ANSI C seems to specify that the type of the result
1128 should be the const type. */
1129 /* A de-reference of a pointer to const is not a const. It is valid
1130 to change it via some other pointer. */
1131 TREE_READONLY (ref
) = TYPE_READONLY (t
);
1132 TREE_SIDE_EFFECTS (ref
) = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
1133 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
1136 else if (TREE_CODE (pointer
) != ERROR_MARK
)
1137 error ("invalid type argument of `%s'", errorstring
);
1138 return error_mark_node
;
1141 /* This handles expressions of the form "a[i]", which denotes
1144 This is logically equivalent in C to *(a+i), but we may do it differently.
1145 If A is a variable or a member, we generate a primitive ARRAY_REF.
1146 This avoids forcing the array out of registers, and can work on
1147 arrays that are not lvalues (for example, members of structures returned
1151 build_array_ref (array
, index
)
1156 error ("subscript missing in array reference");
1157 return error_mark_node
;
1160 if (TREE_TYPE (array
) == error_mark_node
1161 || TREE_TYPE (index
) == error_mark_node
)
1162 return error_mark_node
;
1164 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
1165 && TREE_CODE (array
) != INDIRECT_REF
)
1169 /* Subscripting with type char is likely to lose
1170 on a machine where chars are signed.
1171 So warn on any machine, but optionally.
1172 Don't warn for unsigned char since that type is safe.
1173 Don't warn for signed char because anyone who uses that
1174 must have done so deliberately. */
1175 if (warn_char_subscripts
1176 && TYPE_MAIN_VARIANT (TREE_TYPE (index
)) == char_type_node
)
1177 warning ("array subscript has type `char'");
1179 /* Apply default promotions *after* noticing character types. */
1180 index
= default_conversion (index
);
1182 /* Require integer *after* promotion, for sake of enums. */
1183 if (TREE_CODE (TREE_TYPE (index
)) != INTEGER_TYPE
)
1185 error ("array subscript is not an integer");
1186 return error_mark_node
;
1189 /* An array that is indexed by a non-constant
1190 cannot be stored in a register; we must be able to do
1191 address arithmetic on its address.
1192 Likewise an array of elements of variable size. */
1193 if (TREE_CODE (index
) != INTEGER_CST
1194 || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))) != 0
1195 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
1197 if (mark_addressable (array
) == 0)
1198 return error_mark_node
;
1201 if (pedantic
&& !lvalue_p (array
))
1203 if (DECL_REGISTER (array
))
1204 pedwarn ("ANSI C forbids subscripting `register' array");
1206 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1212 while (TREE_CODE (foo
) == COMPONENT_REF
)
1213 foo
= TREE_OPERAND (foo
, 0);
1214 if (TREE_CODE (foo
) == VAR_DECL
&& DECL_REGISTER (foo
))
1215 pedwarn ("ANSI C forbids subscripting non-lvalue array");
1218 type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array
)));
1219 rval
= build (ARRAY_REF
, type
, array
, index
);
1220 /* Array ref is const/volatile if the array elements are
1221 or if the array is. */
1222 TREE_READONLY (rval
)
1223 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
1224 | TREE_READONLY (array
));
1225 TREE_SIDE_EFFECTS (rval
)
1226 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1227 | TREE_SIDE_EFFECTS (array
));
1228 TREE_THIS_VOLATILE (rval
)
1229 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
1230 /* This was added by rms on 16 Nov 91.
1231 It fixes vol struct foo *a; a->elts[1]
1232 in an inline function.
1233 Hope it doesn't break something else. */
1234 | TREE_THIS_VOLATILE (array
));
1235 return require_complete_type (fold (rval
));
1239 tree ar
= default_conversion (array
);
1240 tree ind
= default_conversion (index
);
1242 /* Put the integer in IND to simplify error checking. */
1243 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
1250 if (ar
== error_mark_node
)
1253 if (TREE_CODE (TREE_TYPE (ar
)) != POINTER_TYPE
)
1255 error ("subscripted value is neither array nor pointer");
1256 return error_mark_node
;
1258 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
1260 error ("array subscript is not an integer");
1261 return error_mark_node
;
1264 return build_indirect_ref (build_binary_op (PLUS_EXPR
, ar
, ind
, 0),
1269 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against PARAMS. */
1271 #define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
1273 #define T_I &integer_type_node
1274 #define T_L &long_integer_type_node
1275 #define T_S &short_integer_type_node
1276 #define T_UI &unsigned_type_node
1277 #define T_UL &long_unsigned_type_node
1278 #define T_US &short_unsigned_type_node
1279 #define T_F &float_type_node
1280 #define T_D &double_type_node
1281 #define T_LD &long_double_type_node
1282 #define T_C &char_type_node
1283 #define T_V &void_type_node
1289 /* Type of argument if no length modifier is used. */
1291 /* Type of argument if length modifier for shortening is used.
1292 If NULL, then this modifier is not allowed. */
1294 /* Type of argument if length modifier `l' is used.
1295 If NULL, then this modifier is not allowed. */
1297 /* Type of argument if length modifier `L' is used.
1298 If NULL, then this modifier is not allowed. */
1300 /* List of other modifier characters allowed with these options. */
1304 static format_char_info print_table
[]
1306 { "di", 0, T_I
, T_I
, T_L
, NULL
, "-wp0 +" },
1307 { "oxX", 0, T_UI
, T_UI
, T_UL
, NULL
, "-wp0#" },
1308 { "u", 0, T_UI
, T_UI
, T_UL
, NULL
, "-wp0" },
1309 { "feEgG", 0, T_D
, NULL
, NULL
, T_LD
, "-wp0 +#" },
1310 { "c", 0, T_I
, NULL
, NULL
, NULL
, "-w" },
1311 { "s", 1, T_C
, NULL
, NULL
, NULL
, "-wp" },
1312 { "p", 1, T_V
, NULL
, NULL
, NULL
, "-" },
1313 { "n", 1, T_I
, T_S
, T_L
, NULL
, "" },
1317 static format_char_info scan_table
[]
1319 { "di", 1, T_I
, T_S
, T_L
, NULL
, "*" },
1320 { "ouxX", 1, T_UI
, T_US
, T_UL
, NULL
, "*" },
1321 { "efgEG", 1, T_F
, NULL
, T_D
, T_LD
, "*" },
1322 { "s[c", 1, T_C
, NULL
, NULL
, NULL
, "*" },
1323 { "p", 2, T_V
, NULL
, NULL
, NULL
, "*" },
1324 { "n", 1, T_I
, T_S
, T_L
, NULL
, "" },
1330 tree function_ident
; /* identifier such as "printf" */
1331 int is_scan
; /* TRUE if *scanf */
1332 int format_num
; /* number of format argument */
1333 int first_arg_num
; /* number of first arg (zero for varargs) */
1336 static unsigned int function_info_entries
= 0;
1337 static function_info
*function_info_table
= NULL
;
1339 /* Record information for argument format checking. FUNCTION_IDENT is
1340 the identifier node for the name of the function to check (its decl
1341 need not exist yet). IS_SCAN is true for scanf-type format checking;
1342 false indicates printf-style format checking. FORMAT_NUM is the number
1343 of the argument which is the format control string (starting from 1).
1344 FIRST_ARG_NUM is the number of the first actual argument to check
1345 against teh format string, or zero if no checking is not be done
1346 (e.g. for varargs such as vfprintf). */
1349 record_format_info (function_ident
, is_scan
, format_num
, first_arg_num
)
1350 tree function_ident
;
1355 function_info
*info
;
1357 function_info_entries
++;
1358 if (function_info_table
)
1360 = (function_info
*) xrealloc (function_info_table
,
1361 function_info_entries
* sizeof (function_info
));
1363 function_info_table
= (function_info
*) xmalloc (sizeof (function_info
));
1365 info
= &function_info_table
[function_info_entries
- 1];
1367 info
->function_ident
= function_ident
;
1368 info
->is_scan
= is_scan
;
1369 info
->format_num
= format_num
;
1370 info
->first_arg_num
= first_arg_num
;
1373 /* Initialize the table of functions to perform format checking on.
1374 The ANSI functions are always checked (whether <stdio.h> is
1375 included or not), since it is common to call printf without
1376 including <stdio.h>. There shouldn't be a problem with this,
1377 since ANSI reserves these function names whether you include the
1378 header file or not. In any case, the checking is harmless. */
1381 init_format_info_table ()
1383 record_format_info (get_identifier ("printf"), 0, 1, 2);
1384 record_format_info (get_identifier ("fprintf"), 0, 2, 3);
1385 record_format_info (get_identifier ("sprintf"), 0, 2, 3);
1386 record_format_info (get_identifier ("scanf"), 1, 1, 2);
1387 record_format_info (get_identifier ("fscanf"), 1, 2, 3);
1388 record_format_info (get_identifier ("sscanf"), 1, 2, 3);
1389 record_format_info (get_identifier ("vprintf"), 0, 1, 0);
1390 record_format_info (get_identifier ("vfprintf"), 0, 2, 0);
1391 record_format_info (get_identifier ("vsprintf"), 0, 2, 0);
1394 static char tfaff
[] = "too few arguments for format";
1396 /* Check the argument list of a call to printf, scanf, etc.
1397 INFO points to the element of function_info_table.
1398 PARAMS is the list of argument values. */
1401 check_format (info
, params
)
1402 function_info
*info
;
1407 int suppressed
, wide
, precise
;
1416 format_char_info
*fci
;
1417 static char message
[132];
1420 /* Skip to format argument. If the argument isn't available, there's
1421 no work for us to do; prototype checking will catch the problem. */
1422 for (arg_num
= 1; ; ++arg_num
)
1426 if (arg_num
== info
->format_num
)
1428 params
= TREE_CHAIN (params
);
1430 format_tree
= TREE_VALUE (params
);
1431 params
= TREE_CHAIN (params
);
1432 if (format_tree
== 0)
1434 /* We can only check the format if it's a string constant. */
1435 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1436 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
1437 if (format_tree
== null_pointer_node
)
1439 warning ("null format string");
1442 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1444 format_tree
= TREE_OPERAND (format_tree
, 0);
1445 if (TREE_CODE (format_tree
) != STRING_CST
)
1447 format_chars
= TREE_STRING_POINTER (format_tree
);
1448 format_length
= TREE_STRING_LENGTH (format_tree
);
1449 if (format_length
<= 1)
1450 warning ("zero-length format string");
1451 if (format_chars
[--format_length
] != 0)
1453 warning ("unterminated format string");
1456 /* Skip to first argument to check. */
1457 while (arg_num
+ 1 < info
->first_arg_num
)
1461 params
= TREE_CHAIN (params
);
1466 if (*format_chars
== 0)
1468 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
1469 warning ("embedded `\\0' in format");
1470 if (info
->first_arg_num
!= 0 && params
!= 0)
1471 warning ("too many arguments for format");
1474 if (*format_chars
++ != '%')
1476 if (*format_chars
== 0)
1478 warning ("spurious trailing `%%' in format");
1481 if (*format_chars
== '%')
1487 suppressed
= wide
= precise
= FALSE
;
1490 suppressed
= *format_chars
== '*';
1493 while (ISDIGIT (*format_chars
))
1498 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
1500 if (index (flag_chars
, *format_chars
) != 0)
1502 sprintf (message
, "repeated `%c' flag in format",
1506 i
= strlen (flag_chars
);
1507 flag_chars
[i
++] = *format_chars
++;
1510 /* "If the space and + flags both appear,
1511 the space flag will be ignored." */
1512 if (index (flag_chars
, ' ') != 0
1513 && index (flag_chars
, '+') != 0)
1514 warning ("use of both ` ' and `+' flags in format");
1515 /* "If the 0 and - flags both appear,
1516 the 0 flag will be ignored." */
1517 if (index (flag_chars
, '0') != 0
1518 && index (flag_chars
, '-') != 0)
1519 warning ("use of both `0' and `-' flags in format");
1520 if (*format_chars
== '*')
1523 /* "...a field width...may be indicated by an asterisk.
1524 In this case, an int argument supplies the field width..." */
1531 if (info
->first_arg_num
!= 0)
1533 cur_param
= TREE_VALUE (params
);
1534 params
= TREE_CHAIN (params
);
1536 /* size_t is generally not valid here.
1537 It will work on most machines, because size_t and int
1538 have the same mode. But might as well warn anyway,
1539 since it will fail on other machines. */
1540 if (TREE_TYPE (cur_param
) != integer_type_node
)
1543 "field width is not type int (arg %d)",
1551 while (ISDIGIT (*format_chars
))
1557 if (*format_chars
== '.')
1560 /* "For d, i, o, u, x, and X conversions,
1561 if a precision is specified, the 0 flag will be ignored.
1562 For other conversions, the behavior is undefined." */
1563 if (index (flag_chars
, '0') != 0)
1564 warning ("precision and `0' flag both used in one %%-sequence");
1566 if (*format_chars
!= '*' && !ISDIGIT (*format_chars
))
1567 warning ("`.' not followed by `*' or digit in format");
1568 /* "...a...precision...may be indicated by an asterisk.
1569 In this case, an int argument supplies the...precision." */
1570 if (*format_chars
== '*')
1572 if (info
->first_arg_num
!= 0)
1580 cur_param
= TREE_VALUE (params
);
1581 params
= TREE_CHAIN (params
);
1583 if (TREE_TYPE (cur_param
) != integer_type_node
)
1586 "field width is not type int (arg %d)",
1594 while (ISDIGIT (*format_chars
))
1599 if (*format_chars
== 'h' || *format_chars
== 'l' || *format_chars
== 'L')
1600 length_char
= *format_chars
++;
1603 if (suppressed
&& length_char
!= 0)
1606 "use of `*' and `%c' together in format",
1610 format_char
= *format_chars
;
1611 if (format_char
== 0)
1613 warning ("conversion lacks type at end of format");
1617 fci
= info
->is_scan
? scan_table
: print_table
;
1620 if (fci
->format_chars
== 0
1621 || index (fci
->format_chars
, format_char
) != 0)
1625 if (fci
->format_chars
== 0)
1627 if (format_char
>= 040 && format_char
<= 0177)
1629 "unknown conversion type character `%c' in format",
1633 "unknown conversion type character 0x%x in format",
1638 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
1640 sprintf (message
, "width used with `%c' format",
1644 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
1646 sprintf (message
, "precision used with `%c' format",
1652 if (index (fci
->flag_chars
, '*') == 0)
1655 "suppression of `%c' conversion in format",
1661 for (i
= 0; flag_chars
[i
] != 0; ++i
)
1663 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
1665 sprintf (message
, "flag `%c' used with type `%c'",
1666 flag_chars
[i
], format_char
);
1670 switch (length_char
)
1672 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1673 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1674 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1675 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1677 if (wanted_type
== 0)
1680 "use of `%c' length character with `%c' type character",
1681 length_char
, format_char
);
1686 ** XXX -- should kvetch about stuff such as
1690 ** scanf ("%d", &i);
1694 /* Finally. . .check type of argument against desired type! */
1695 if (info
->first_arg_num
== 0)
1702 cur_param
= TREE_VALUE (params
);
1703 params
= TREE_CHAIN (params
);
1705 cur_type
= TREE_TYPE (cur_param
);
1707 /* Check the types of any additional pointer arguments
1708 that precede the "real" argument. */
1709 for (i
= 0; i
< fci
->pointer_count
; ++i
)
1711 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1713 cur_type
= TREE_TYPE (cur_type
);
1717 "format argument is not a %s (arg %d)",
1718 ((fci
->pointer_count
== 1) ? "pointer" : "pointer to a pointer"),
1724 /* Check the type of the "real" argument, if there's a type we want. */
1725 if (i
== fci
->pointer_count
&& wanted_type
!= 0
1726 && wanted_type
!= cur_type
1727 /* If we want `void *', allow any pointer type.
1728 (Anything else would already have got a warning.) */
1729 && ! (wanted_type
== void_type_node
1730 && fci
->pointer_count
> 0)
1731 /* Don't warn about differences merely in signedness. */
1732 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1733 && TREE_CODE (cur_type
) == INTEGER_TYPE
1734 && TYPE_PRECISION (wanted_type
) == TYPE_PRECISION (cur_type
)))
1736 register char *this;
1737 register char *that
;
1739 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1741 if (TYPE_NAME (cur_type
) != 0
1742 && TREE_CODE (cur_type
) != INTEGER_TYPE
1743 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1744 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
)
1745 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1746 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1748 /* A nameless type can't possibly match what the format wants.
1749 So there will be a warning for it.
1750 Make up a string to describe vaguely what it is. */
1753 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1756 that
= "different type";
1759 if (strcmp (this, that
) != 0)
1761 sprintf (message
, "%s format, %s arg (arg %d)",
1762 this, that
, arg_num
);
1769 /* Build a function call to function FUNCTION with parameters PARAMS.
1770 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1771 TREE_VALUE of each node is a parameter-expression.
1772 FUNCTION's data type may be a function type or a pointer-to-function. */
1775 build_function_call (function
, params
)
1776 tree function
, params
;
1778 register tree fntype
;
1779 register tree coerced_params
;
1780 tree name
= NULL_TREE
;
1782 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1783 STRIP_TYPE_NOPS (function
);
1785 /* Convert anything with function type to a pointer-to-function. */
1786 if (TREE_CODE (function
) == FUNCTION_DECL
)
1788 name
= DECL_NAME (function
);
1789 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1790 (because calling an inline function does not mean the function
1791 needs to be separately compiled). */
1792 fntype
= build_type_variant (TREE_TYPE (function
),
1793 TREE_READONLY (function
),
1794 TREE_THIS_VOLATILE (function
));
1795 function
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), function
);
1798 function
= default_conversion (function
);
1800 fntype
= TREE_TYPE (function
);
1802 if (TREE_CODE (fntype
) == ERROR_MARK
)
1803 return error_mark_node
;
1805 if (!(TREE_CODE (fntype
) == POINTER_TYPE
1806 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
1808 error ("called object is not a function");
1809 return error_mark_node
;
1812 /* fntype now gets the type of function pointed to. */
1813 fntype
= TREE_TYPE (fntype
);
1815 /* Convert the parameters to the types declared in the
1816 function prototype, or apply default promotions. */
1819 = convert_arguments (TYPE_ARG_TYPES (fntype
), params
, name
);
1821 /* Check for errors in format strings. */
1822 if (warn_format
&& name
!= 0)
1826 /* See if this function is a format function. */
1827 for (i
= 0; i
< function_info_entries
; i
++)
1828 if (function_info_table
[i
].function_ident
== name
)
1830 register char *message
;
1832 /* If so, check it. */
1833 check_format (&function_info_table
[i
], coerced_params
);
1838 /* Recognize certain built-in functions so we can make tree-codes
1839 other than CALL_EXPR. We do this when it enables fold-const.c
1840 to do something useful. */
1842 if (TREE_CODE (function
) == ADDR_EXPR
1843 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
1844 && DECL_BUILT_IN (TREE_OPERAND (function
, 0)))
1845 switch (DECL_FUNCTION_CODE (TREE_OPERAND (function
, 0)))
1850 if (coerced_params
== 0)
1851 return integer_zero_node
;
1852 return build_unary_op (ABS_EXPR
, TREE_VALUE (coerced_params
), 0);
1856 register tree result
1857 = build (CALL_EXPR
, TREE_TYPE (fntype
),
1858 function
, coerced_params
, NULL_TREE
);
1860 TREE_SIDE_EFFECTS (result
) = 1;
1861 if (TREE_TYPE (result
) == void_type_node
)
1863 return require_complete_type (result
);
1867 /* Convert the argument expressions in the list VALUES
1868 to the types in the list TYPELIST. The result is a list of converted
1869 argument expressions.
1871 If TYPELIST is exhausted, or when an element has NULL as its type,
1872 perform the default conversions.
1874 PARMLIST is the chain of parm decls for the function being called.
1875 It may be 0, if that info is not available.
1876 It is used only for generating error messages.
1878 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1880 This is also where warnings about wrong number of args are generated.
1882 Both VALUES and the returned value are chains of TREE_LIST nodes
1883 with the elements of the list in the TREE_VALUE slots of those nodes. */
1886 convert_arguments (typelist
, values
, name
)
1887 tree typelist
, values
, name
;
1889 register tree typetail
, valtail
;
1890 register tree result
= NULL
;
1893 /* Scan the given expressions and types, producing individual
1894 converted arguments and pushing them on RESULT in reverse order. */
1896 for (valtail
= values
, typetail
= typelist
, parmnum
= 0;
1898 valtail
= TREE_CHAIN (valtail
), parmnum
++)
1900 register tree type
= typetail
? TREE_VALUE (typetail
) : 0;
1901 register tree val
= TREE_VALUE (valtail
);
1903 if (type
== void_type_node
)
1906 error ("too many arguments to function `%s'",
1907 IDENTIFIER_POINTER (name
));
1909 error ("too many arguments to function");
1913 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1914 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1915 to convert automatically to a pointer. */
1916 if (TREE_CODE (val
) == NON_LVALUE_EXPR
)
1917 val
= TREE_OPERAND (val
, 0);
1919 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
1920 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
)
1921 val
= default_conversion (val
);
1923 val
= require_complete_type (val
);
1927 /* Formal parm type is specified by a function prototype. */
1930 if (TYPE_SIZE (type
) == 0)
1932 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
1938 #ifdef PROMOTE_PROTOTYPES
1939 /* Rather than truncating and then reextending,
1940 convert directly to int, if that's the type we will want. */
1941 if (! flag_traditional
1942 && TREE_CODE (type
) == INTEGER_TYPE
1943 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
1944 type
= integer_type_node
;
1947 #if 0 /* This turns out not to win--there's no way to write a prototype
1948 for a function whose arg type is a union with no tag. */
1949 /* Nameless union automatically casts the types it contains. */
1950 if (TREE_CODE (type
) == UNION_TYPE
&& TYPE_NAME (type
) == 0)
1954 for (field
= TYPE_FIELDS (type
); field
;
1955 field
= TREE_CHAIN (field
))
1956 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
1957 TYPE_MAIN_VARIANT (TREE_TYPE (val
))))
1961 val
= build1 (CONVERT_EXPR
, type
, val
);
1965 /* Optionally warn about conversions that
1966 differ from the default conversions. */
1967 if (warn_conversion
)
1969 int formal_prec
= TYPE_PRECISION (type
);
1971 if (TREE_CODE (type
) != REAL_TYPE
1972 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1973 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name
, parmnum
+ 1);
1974 else if (TREE_CODE (type
) == REAL_TYPE
1975 && TREE_CODE (TREE_TYPE (val
)) != REAL_TYPE
)
1976 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name
, parmnum
+ 1);
1977 else if (TREE_CODE (type
) == REAL_TYPE
1978 && TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
)
1980 /* Warn if any argument is passed as `float',
1981 since without a prototype it would be `double'. */
1982 if (formal_prec
== TYPE_PRECISION (float_type_node
))
1983 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name
, parmnum
+ 1);
1985 /* Detect integer changing in width or signedness. */
1986 else if ((TREE_CODE (type
) == INTEGER_TYPE
1987 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1988 && (TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
1989 || TREE_CODE (TREE_TYPE (val
)) == ENUMERAL_TYPE
))
1991 tree would_have_been
= default_conversion (val
);
1992 tree type1
= TREE_TYPE (would_have_been
);
1994 if (TREE_CODE (type
) == ENUMERAL_TYPE
1995 && type
== TREE_TYPE (val
))
1996 /* No warning if function asks for enum
1997 and the actual arg is that enum type. */
1999 else if (formal_prec
!= TYPE_PRECISION (type1
))
2000 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name
, parmnum
+ 1);
2001 else if (TREE_UNSIGNED (type
) == TREE_UNSIGNED (type1
))
2003 /* Don't complain if the formal parameter type
2004 is an enum, because we can't tell now whether
2005 the value was an enum--even the same enum. */
2006 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
2008 else if (TREE_CODE (val
) == INTEGER_CST
2009 && int_fits_type_p (val
, type
))
2010 /* Change in signedness doesn't matter
2011 if a constant value is unaffected. */
2013 else if (TREE_CODE (TREE_TYPE (val
)) == ENUMERAL_TYPE
2014 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val
)), type
)
2015 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val
)), type
))
2016 /* Change in signedness doesn't matter
2017 if an enum value is unaffected. */
2019 else if (TREE_UNSIGNED (type
))
2020 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name
, parmnum
+ 1);
2022 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name
, parmnum
+ 1);
2026 parmval
= convert_for_assignment (type
, val
,
2027 (char *)0, /* arg passing */
2030 #ifdef PROMOTE_PROTOTYPES
2031 if (TREE_CODE (type
) == INTEGER_TYPE
2032 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
2033 parmval
= default_conversion (parmval
);
2036 result
= tree_cons (NULL_TREE
, parmval
, result
);
2038 else if (TREE_CODE (TREE_TYPE (val
)) == REAL_TYPE
2039 && (TYPE_PRECISION (TREE_TYPE (val
))
2040 < TYPE_PRECISION (double_type_node
)))
2041 /* Convert `float' to `double'. */
2042 result
= tree_cons (NULL_TREE
, convert (double_type_node
, val
), result
);
2044 /* Convert `short' and `char' to full-size `int'. */
2045 result
= tree_cons (NULL_TREE
, default_conversion (val
), result
);
2048 typetail
= TREE_CHAIN (typetail
);
2051 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
2054 error ("too few arguments to function `%s'",
2055 IDENTIFIER_POINTER (name
));
2057 error ("too few arguments to function");
2060 return nreverse (result
);
2063 /* This is the entry point used by the parser
2064 for binary operators in the input.
2065 In addition to constructing the expression,
2066 we check for operands that were written with other binary operators
2067 in a way that is likely to confuse the user. */
2070 parser_build_binary_op (code
, arg1
, arg2
)
2071 enum tree_code code
;
2074 tree result
= build_binary_op (code
, arg1
, arg2
, 1);
2077 char class1
= TREE_CODE_CLASS (TREE_CODE (arg1
));
2078 char class2
= TREE_CODE_CLASS (TREE_CODE (arg2
));
2079 enum tree_code code1
= ERROR_MARK
;
2080 enum tree_code code2
= ERROR_MARK
;
2082 if (class1
== 'e' || class1
== '1'
2083 || class1
== '2' || class1
== '<')
2084 code1
= C_EXP_ORIGINAL_CODE (arg1
);
2085 if (class2
== 'e' || class2
== '1'
2086 || class2
== '2' || class2
== '<')
2087 code2
= C_EXP_ORIGINAL_CODE (arg2
);
2089 /* Check for cases such as x+y<<z which users are likely
2090 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2091 is cleared to prevent these warnings. */
2092 if (warn_parentheses
)
2094 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
2096 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2097 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2098 warning ("suggest parentheses around + or - inside shift");
2101 if (code
== TRUTH_ORIF_EXPR
)
2103 if (code1
== TRUTH_ANDIF_EXPR
2104 || code2
== TRUTH_ANDIF_EXPR
)
2105 warning ("suggest parentheses around && within ||");
2108 if (code
== BIT_IOR_EXPR
)
2110 if (code1
== BIT_AND_EXPR
|| code1
== BIT_XOR_EXPR
2111 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2112 || code2
== BIT_AND_EXPR
|| code2
== BIT_XOR_EXPR
2113 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2114 warning ("suggest parentheses around arithmetic in operand of |");
2117 if (code
== BIT_XOR_EXPR
)
2119 if (code1
== BIT_AND_EXPR
2120 || code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2121 || code2
== BIT_AND_EXPR
2122 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2123 warning ("suggest parentheses around arithmetic in operand of ^");
2126 if (code
== BIT_AND_EXPR
)
2128 if (code1
== PLUS_EXPR
|| code1
== MINUS_EXPR
2129 || code2
== PLUS_EXPR
|| code2
== MINUS_EXPR
)
2130 warning ("suggest parentheses around + or - in operand of &");
2134 class = TREE_CODE_CLASS (TREE_CODE (result
));
2136 /* Record the code that was specified in the source,
2137 for the sake of warnings about confusing nesting. */
2138 if (class == 'e' || class == '1'
2139 || class == '2' || class == '<')
2140 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2143 int flag
= TREE_CONSTANT (result
);
2144 result
= build1 (NON_LVALUE_EXPR
, TREE_TYPE (result
), result
);
2145 C_SET_EXP_ORIGINAL_CODE (result
, code
);
2146 TREE_CONSTANT (result
) = flag
;
2152 /* Build a binary-operation expression without default conversions.
2153 CODE is the kind of expression to build.
2154 This function differs from `build' in several ways:
2155 the data type of the result is computed and recorded in it,
2156 warnings are generated if arg data types are invalid,
2157 special handling for addition and subtraction of pointers is known,
2158 and some optimization is done (operations on narrow ints
2159 are done in the narrower type when that gives the same result).
2160 Constant folding is also done before the result is returned.
2162 Note that the operands will never have enumeral types, or function
2163 or array types, because either they will have the default conversions
2164 performed or they have both just been converted to some other type in which
2165 the arithmetic is to be done. */
2168 build_binary_op (code
, orig_op0
, orig_op1
, convert_p
)
2169 enum tree_code code
;
2170 tree orig_op0
, orig_op1
;
2174 register enum tree_code code0
, code1
;
2177 /* Expression code to give to the expression when it is built.
2178 Normally this is CODE, which is what the caller asked for,
2179 but in some special cases we change it. */
2180 register enum tree_code resultcode
= code
;
2182 /* Data type in which the computation is to be performed.
2183 In the simplest cases this is the common type of the arguments. */
2184 register tree result_type
= NULL
;
2186 /* Nonzero means operands have already been type-converted
2187 in whatever way is necessary.
2188 Zero means they need to be converted to RESULT_TYPE. */
2191 /* Nonzero means after finally constructing the expression
2192 give it this type. Otherwise, give it type RESULT_TYPE. */
2193 tree final_type
= 0;
2195 /* Nonzero if this is an operation like MIN or MAX which can
2196 safely be computed in short if both args are promoted shorts.
2197 Also implies COMMON.
2198 -1 indicates a bitwise operation; this makes a difference
2199 in the exact conditions for when it is safe to do the operation
2200 in a narrower mode. */
2203 /* Nonzero if this is a comparison operation;
2204 if both args are promoted shorts, compare the original shorts.
2205 Also implies COMMON. */
2206 int short_compare
= 0;
2208 /* Nonzero if this is a right-shift operation, which can be computed on the
2209 original short and then promoted if the operand is a promoted short. */
2210 int short_shift
= 0;
2212 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2217 op0
= default_conversion (orig_op0
);
2218 op1
= default_conversion (orig_op1
);
2226 type0
= TREE_TYPE (op0
);
2227 type1
= TREE_TYPE (op1
);
2229 /* The expression codes of the data types of the arguments tell us
2230 whether the arguments are integers, floating, pointers, etc. */
2231 code0
= TREE_CODE (type0
);
2232 code1
= TREE_CODE (type1
);
2234 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2235 STRIP_TYPE_NOPS (op0
);
2236 STRIP_TYPE_NOPS (op1
);
2238 /* If an error was already reported for one of the arguments,
2239 avoid reporting another error. */
2241 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
2242 return error_mark_node
;
2247 /* Handle the pointer + int case. */
2248 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2249 return pointer_int_sum (PLUS_EXPR
, op0
, op1
);
2250 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
2251 return pointer_int_sum (PLUS_EXPR
, op1
, op0
);
2257 /* Subtraction of two similar pointers.
2258 We must subtract them as integers, then divide by object size. */
2259 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
2260 && comp_target_types (type0
, type1
))
2261 return pointer_diff (op0
, op1
);
2262 /* Handle pointer minus int. Just like pointer plus int. */
2263 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2264 return pointer_int_sum (MINUS_EXPR
, op0
, op1
);
2273 case TRUNC_DIV_EXPR
:
2275 case FLOOR_DIV_EXPR
:
2276 case ROUND_DIV_EXPR
:
2277 case EXACT_DIV_EXPR
:
2278 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2279 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2281 if (!(code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
))
2282 resultcode
= RDIV_EXPR
;
2290 case BIT_ANDTC_EXPR
:
2293 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2295 /* If one operand is a constant, and the other is a short type
2296 that has been converted to an int,
2297 really do the work in the short type and then convert the
2298 result to int. If we are lucky, the constant will be 0 or 1
2299 in the short type, making the entire operation go away. */
2300 if (TREE_CODE (op0
) == INTEGER_CST
2301 && TREE_CODE (op1
) == NOP_EXPR
2302 && TYPE_PRECISION (type1
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))
2303 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1
, 0))))
2305 final_type
= result_type
;
2306 op1
= TREE_OPERAND (op1
, 0);
2307 result_type
= TREE_TYPE (op1
);
2309 if (TREE_CODE (op1
) == INTEGER_CST
2310 && TREE_CODE (op0
) == NOP_EXPR
2311 && TYPE_PRECISION (type0
) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))
2312 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
2314 final_type
= result_type
;
2315 op0
= TREE_OPERAND (op0
, 0);
2316 result_type
= TREE_TYPE (op0
);
2320 case TRUNC_MOD_EXPR
:
2321 case FLOOR_MOD_EXPR
:
2322 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2326 case TRUTH_ANDIF_EXPR
:
2327 case TRUTH_ORIF_EXPR
:
2328 case TRUTH_AND_EXPR
:
2330 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
|| code0
== REAL_TYPE
)
2331 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
|| code1
== REAL_TYPE
))
2333 /* Result of these operations is always an int,
2334 but that does not mean the operands should be
2335 converted to ints! */
2336 result_type
= integer_type_node
;
2337 op0
= truthvalue_conversion (op0
);
2338 op1
= truthvalue_conversion (op1
);
2343 /* Shift operations: result has same type as first operand;
2344 always convert second operand to int.
2345 Also set SHORT_SHIFT if shifting rightward. */
2348 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2350 if (TREE_CODE (op1
) == INTEGER_CST
)
2352 if (TREE_INT_CST_LOW (op1
) > 0)
2354 else if (TREE_INT_CST_LOW (op1
) < 0)
2355 warning ("shift count is negative");
2356 if (TREE_INT_CST_LOW (op1
) >= TYPE_PRECISION (type0
))
2357 warning ("shift count exceeds width of value shifted");
2359 /* Use the type of the value to be shifted.
2360 This is what most traditional C compilers do. */
2361 result_type
= type0
;
2362 /* Unless traditional, convert the shift-count to an integer,
2363 regardless of size of value being shifted. */
2364 if (! flag_traditional
)
2366 if (TREE_TYPE (op1
) != integer_type_node
)
2367 op1
= convert (integer_type_node
, op1
);
2368 /* Avoid converting op1 to result_type later. */
2375 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2377 if (TREE_CODE (op1
) == INTEGER_CST
2378 && TREE_INT_CST_LOW (op1
) < 0)
2379 warning ("shift count is negative");
2380 if (TREE_CODE (op1
) == INTEGER_CST
2381 && TREE_INT_CST_LOW (op1
) >= TYPE_PRECISION (type0
))
2382 warning ("shift count exceeds width of value shifted");
2383 /* Use the type of the value to be shifted.
2384 This is what most traditional C compilers do. */
2385 result_type
= type0
;
2386 /* Unless traditional, convert the shift-count to an integer,
2387 regardless of size of value being shifted. */
2388 if (! flag_traditional
)
2390 if (TREE_TYPE (op1
) != integer_type_node
)
2391 op1
= convert (integer_type_node
, op1
);
2392 /* Avoid converting op1 to result_type later. */
2400 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
2402 if (TREE_CODE (op1
) == INTEGER_CST
2403 && TREE_INT_CST_LOW (op1
) < 0)
2404 warning ("shift count is negative");
2405 if (TREE_CODE (op1
) == INTEGER_CST
2406 && TREE_INT_CST_LOW (op1
) >= TYPE_PRECISION (type0
))
2407 warning ("shift count >= width of value shifted");
2408 /* Use the type of the value to be shifted.
2409 This is what most traditional C compilers do. */
2410 result_type
= type0
;
2411 /* Unless traditional, convert the shift-count to an integer,
2412 regardless of size of value being shifted. */
2413 if (! flag_traditional
)
2415 if (TREE_TYPE (op1
) != integer_type_node
)
2416 op1
= convert (integer_type_node
, op1
);
2417 /* Avoid converting op1 to result_type later. */
2425 /* Result of comparison is always int,
2426 but don't convert the args to int! */
2427 result_type
= integer_type_node
;
2429 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2430 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2432 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
2434 register tree tt0
= TREE_TYPE (type0
);
2435 register tree tt1
= TREE_TYPE (type1
);
2436 /* Anything compares with void *. void * compares with anything.
2437 Otherwise, the targets must be the same. */
2438 if (comp_target_types (type0
, type1
))
2440 else if (TYPE_MAIN_VARIANT (tt0
) == void_type_node
)
2442 if (pedantic
&& !integer_zerop (op0
)
2443 && TREE_CODE (tt1
) == FUNCTION_TYPE
)
2444 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2446 else if (TYPE_MAIN_VARIANT (tt1
) == void_type_node
)
2448 if (pedantic
&& !integer_zerop (op1
)
2449 && TREE_CODE (tt0
) == FUNCTION_TYPE
)
2450 pedwarn ("ANSI C forbids comparison of `void *' with function pointer");
2453 pedwarn ("comparison of distinct pointer types lacks a cast");
2455 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
2456 && integer_zerop (op1
))
2457 op1
= null_pointer_node
;
2458 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
2459 && integer_zerop (op0
))
2460 op0
= null_pointer_node
;
2461 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2463 if (! flag_traditional
)
2464 pedwarn ("comparison between pointer and integer");
2465 op1
= convert (TREE_TYPE (op0
), op1
);
2467 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
2469 if (! flag_traditional
)
2470 pedwarn ("comparison between pointer and integer");
2471 op0
= convert (TREE_TYPE (op1
), op0
);
2474 /* If args are not valid, clear out RESULT_TYPE
2475 to cause an error message later. */
2481 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2482 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2484 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
2486 if (! comp_target_types (type0
, type1
))
2487 pedwarn ("comparison of distinct pointer types lacks a cast");
2489 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
2490 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2491 result_type
= common_type (type0
, type1
);
2499 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2500 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2502 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
2504 if (! comp_target_types (type0
, type1
))
2505 pedwarn ("comparison of distinct pointer types lacks a cast");
2507 && TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
2508 pedwarn ("ANSI C forbids ordered comparisons of pointers to functions");
2509 result_type
= integer_type_node
;
2511 else if (code0
== POINTER_TYPE
&& TREE_CODE (op1
) == INTEGER_CST
2512 && integer_zerop (op1
))
2514 result_type
= integer_type_node
;
2515 op1
= null_pointer_node
;
2516 if (! flag_traditional
)
2517 pedwarn ("ordered comparison of pointer with integer zero");
2519 else if (code1
== POINTER_TYPE
&& TREE_CODE (op0
) == INTEGER_CST
2520 && integer_zerop (op0
))
2522 result_type
= integer_type_node
;
2523 op0
= null_pointer_node
;
2525 pedwarn ("ordered comparison of pointer with integer zero");
2527 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
2529 result_type
= integer_type_node
;
2530 if (! flag_traditional
)
2531 pedwarn ("comparison between pointer and integer");
2532 op1
= convert (TREE_TYPE (op0
), op1
);
2534 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
2536 result_type
= integer_type_node
;
2537 if (! flag_traditional
)
2538 pedwarn ("comparison between pointer and integer");
2539 op0
= convert (TREE_TYPE (op1
), op0
);
2545 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
2546 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
2548 if (shorten
|| common
|| short_compare
)
2549 result_type
= common_type (type0
, type1
);
2551 /* For certain operations (which identify themselves by shorten != 0)
2552 if both args were extended from the same smaller type,
2553 do the arithmetic in that type and then extend.
2555 shorten !=0 and !=1 indicates a bitwise operation.
2556 For them, this optimization is safe only if
2557 both args are zero-extended or both are sign-extended.
2558 Otherwise, we might change the result.
2559 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2560 but calculated in (unsigned short) it would be (unsigned short)-1. */
2564 int unsigned0
, unsigned1
;
2565 tree arg0
= get_narrower (op0
, &unsigned0
);
2566 tree arg1
= get_narrower (op1
, &unsigned1
);
2567 /* UNS is 1 if the operation to be done is an unsigned one. */
2568 int uns
= TREE_UNSIGNED (result_type
);
2571 final_type
= result_type
;
2573 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2574 but it *requires* conversion to FINAL_TYPE. */
2576 if ((TYPE_PRECISION (TREE_TYPE (op0
))
2577 == TYPE_PRECISION (TREE_TYPE (arg0
)))
2578 && TREE_TYPE (op0
) != final_type
)
2579 unsigned0
= TREE_UNSIGNED (TREE_TYPE (op0
));
2580 if ((TYPE_PRECISION (TREE_TYPE (op1
))
2581 == TYPE_PRECISION (TREE_TYPE (arg1
)))
2582 && TREE_TYPE (op1
) != final_type
)
2583 unsigned1
= TREE_UNSIGNED (TREE_TYPE (op1
));
2585 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2587 /* For bitwise operations, signedness of nominal type
2588 does not matter. Consider only how operands were extended. */
2592 /* Note that in all three cases below we refrain from optimizing
2593 an unsigned operation on sign-extended args.
2594 That would not be valid. */
2596 /* Both args variable: if both extended in same way
2597 from same width, do it in that width.
2598 Do it unsigned if args were zero-extended. */
2599 if ((TYPE_PRECISION (TREE_TYPE (arg0
))
2600 < TYPE_PRECISION (result_type
))
2601 && (TYPE_PRECISION (TREE_TYPE (arg1
))
2602 == TYPE_PRECISION (TREE_TYPE (arg0
)))
2603 && unsigned0
== unsigned1
2604 && (unsigned0
|| !uns
))
2606 = signed_or_unsigned_type (unsigned0
,
2607 common_type (TREE_TYPE (arg0
), TREE_TYPE (arg1
)));
2608 else if (TREE_CODE (arg0
) == INTEGER_CST
2609 && (unsigned1
|| !uns
)
2610 && (TYPE_PRECISION (TREE_TYPE (arg1
))
2611 < TYPE_PRECISION (result_type
))
2612 && (type
= signed_or_unsigned_type (unsigned1
,
2614 int_fits_type_p (arg0
, type
)))
2616 else if (TREE_CODE (arg1
) == INTEGER_CST
2617 && (unsigned0
|| !uns
)
2618 && (TYPE_PRECISION (TREE_TYPE (arg0
))
2619 < TYPE_PRECISION (result_type
))
2620 && (type
= signed_or_unsigned_type (unsigned0
,
2622 int_fits_type_p (arg1
, type
)))
2626 /* Shifts can be shortened if shifting right. */
2631 tree arg0
= get_narrower (op0
, &unsigned_arg
);
2633 final_type
= result_type
;
2635 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
2636 unsigned_arg
= TREE_UNSIGNED (TREE_TYPE (op0
));
2638 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
2639 /* If arg is sign-extended and then unsigned-shifted,
2640 we can simulate this with a signed shift in arg's type
2641 only if the extended result is at least twice as wide
2642 as the arg. Otherwise, the shift could use up all the
2643 ones made by sign-extension and bring in zeros.
2644 We can't optimize that case at all, but in most machines
2645 it never happens because available widths are 2**N. */
2646 && (!TREE_UNSIGNED (final_type
)
2648 || 2 * TYPE_PRECISION (TREE_TYPE (arg0
)) <= TYPE_PRECISION (result_type
)))
2650 /* Do an unsigned shift if the operand was zero-extended. */
2652 = signed_or_unsigned_type (unsigned_arg
,
2654 /* Convert value-to-be-shifted to that type. */
2655 if (TREE_TYPE (op0
) != result_type
)
2656 op0
= convert (result_type
, op0
);
2661 /* Comparison operations are shortened too but differently.
2662 They identify themselves by setting short_compare = 1. */
2666 /* Don't write &op0, etc., because that would prevent op0
2667 from being kept in a register.
2668 Instead, make copies of the our local variables and
2669 pass the copies by reference, then copy them back afterward. */
2670 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
2671 enum tree_code xresultcode
= resultcode
;
2673 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
2676 op0
= xop0
, op1
= xop1
, result_type
= xresult_type
;
2677 resultcode
= xresultcode
;
2681 tree op0_type
= TREE_TYPE (orig_op0
);
2682 tree op1_type
= TREE_TYPE (orig_op1
);
2683 int op0_unsigned
= TREE_UNSIGNED (op0_type
);
2684 int op1_unsigned
= TREE_UNSIGNED (op1_type
);
2686 /* Give warnings for comparisons between signed and unsigned
2687 quantities that will fail. Do not warn if the signed quantity
2688 is an unsuffixed integer literal (or some static constant
2689 expression involving such literals) and it is positive.
2690 Do not warn if the width of the unsigned quantity is less
2691 than that of the signed quantity, since in this case all
2692 values of the unsigned quantity fit in the signed quantity.
2693 Do not warn if the signed type is the same size as the
2694 result_type since sign extension does not cause trouble in
2696 /* Do the checking based on the original operand trees, so that
2697 casts will be considered, but default promotions won't be. */
2698 if (op0_unsigned
!= op1_unsigned
2700 && TYPE_PRECISION (op0_type
) >= TYPE_PRECISION (op1_type
)
2701 && TYPE_PRECISION (op0_type
) < TYPE_PRECISION (result_type
)
2702 && (TREE_CODE (op1
) != INTEGER_CST
2703 || (TREE_CODE (op1
) == INTEGER_CST
2704 && INT_CST_LT (op1
, integer_zero_node
))))
2707 && TYPE_PRECISION (op1_type
) >= TYPE_PRECISION (op0_type
)
2708 && TYPE_PRECISION (op1_type
) < TYPE_PRECISION (result_type
)
2709 && (TREE_CODE (op0
) != INTEGER_CST
2710 || (TREE_CODE (op0
) == INTEGER_CST
2711 && INT_CST_LT (op0
, integer_zero_node
))))))
2712 warning ("comparison between signed and unsigned");
2717 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2718 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2719 Then the expression will be built.
2720 It will be given type FINAL_TYPE if that is nonzero;
2721 otherwise, it will be given type RESULT_TYPE. */
2725 binary_op_error (code
);
2726 return error_mark_node
;
2731 if (TREE_TYPE (op0
) != result_type
)
2732 op0
= convert (result_type
, op0
);
2733 if (TREE_TYPE (op1
) != result_type
)
2734 op1
= convert (result_type
, op1
);
2738 register tree result
= build (resultcode
, result_type
, op0
, op1
);
2739 register tree folded
;
2741 folded
= fold (result
);
2742 if (folded
== result
)
2743 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
2744 if (final_type
!= 0)
2745 return convert (final_type
, folded
);
2750 /* Return a tree for the sum or difference (RESULTCODE says which)
2751 of pointer PTROP and integer INTOP. */
2754 pointer_int_sum (resultcode
, ptrop
, intop
)
2755 enum tree_code resultcode
;
2756 register tree ptrop
, intop
;
2760 register tree result
;
2761 register tree folded
;
2763 /* The result is a pointer of the same type that is being added. */
2765 register tree result_type
= TREE_TYPE (ptrop
);
2767 if (TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
)
2769 if (pedantic
|| warn_pointer_arith
)
2770 pedwarn ("pointer of type `void *' used in arithmetic");
2771 size_exp
= integer_one_node
;
2773 else if (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
)
2775 if (pedantic
|| warn_pointer_arith
)
2776 pedwarn ("pointer to a function used in arithmetic");
2777 size_exp
= integer_one_node
;
2780 size_exp
= c_size_in_bytes (TREE_TYPE (result_type
));
2782 /* If what we are about to multiply by the size of the elements
2783 contains a constant term, apply distributive law
2784 and multiply that constant term separately.
2785 This helps produce common subexpressions. */
2787 if ((TREE_CODE (intop
) == PLUS_EXPR
|| TREE_CODE (intop
) == MINUS_EXPR
)
2788 && ! TREE_CONSTANT (intop
)
2789 && TREE_CONSTANT (TREE_OPERAND (intop
, 1))
2790 && TREE_CONSTANT (size_exp
)
2791 /* If the constant comes from pointer subtraction,
2792 skip this optimization--it would cause an error. */
2793 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop
, 0))) == INTEGER_TYPE
)
2795 enum tree_code subcode
= resultcode
;
2796 tree int_type
= TREE_TYPE (intop
);
2797 if (TREE_CODE (intop
) == MINUS_EXPR
)
2798 subcode
= (subcode
== PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
);
2799 /* Convert both subexpression types to the type of intop,
2800 because weird cases involving pointer arithmetic
2801 can result in a sum or difference with different type args. */
2802 ptrop
= build_binary_op (subcode
, ptrop
,
2803 convert (int_type
, TREE_OPERAND (intop
, 1)), 1);
2804 intop
= convert (int_type
, TREE_OPERAND (intop
, 0));
2807 /* Convert the integer argument to a type the same size as a pointer
2808 so the multiply won't overflow spuriously. */
2810 if (TYPE_PRECISION (TREE_TYPE (intop
)) != POINTER_SIZE
)
2811 intop
= convert (type_for_size (POINTER_SIZE
, 0), intop
);
2813 /* Replace the integer argument
2814 with a suitable product by the object size. */
2816 intop
= build_binary_op (MULT_EXPR
, intop
, size_exp
, 1);
2818 /* Create the sum or difference. */
2820 result
= build (resultcode
, result_type
, ptrop
, intop
);
2822 folded
= fold (result
);
2823 if (folded
== result
)
2824 TREE_CONSTANT (folded
) = TREE_CONSTANT (ptrop
) & TREE_CONSTANT (intop
);
2828 /* Return a tree for the difference of pointers OP0 and OP1.
2829 The resulting tree has type int. */
2832 pointer_diff (op0
, op1
)
2833 register tree op0
, op1
;
2835 register tree result
, folded
;
2836 tree restype
= ptrdiff_type_node
;
2838 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
2840 if (pedantic
|| warn_pointer_arith
)
2842 if (TREE_CODE (target_type
) == VOID_TYPE
)
2843 pedwarn ("pointer of type `void *' used in subtraction");
2844 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
2845 pedwarn ("pointer to a function used in subtraction");
2848 /* First do the subtraction as integers;
2849 then drop through to build the divide operator. */
2851 op0
= build_binary_op (MINUS_EXPR
, convert (restype
, op0
),
2852 convert (restype
, op1
), 1);
2853 op1
= c_size_in_bytes (target_type
);
2855 /* Divide by the size, in easiest possible way. */
2857 result
= build (EXACT_DIV_EXPR
, restype
, op0
, op1
);
2859 folded
= fold (result
);
2860 if (folded
== result
)
2861 TREE_CONSTANT (folded
) = TREE_CONSTANT (op0
) & TREE_CONSTANT (op1
);
2865 /* Construct and perhaps optimize a tree representation
2866 for a unary operation. CODE, a tree_code, specifies the operation
2867 and XARG is the operand. NOCONVERT nonzero suppresses
2868 the default promotions (such as from short to int). */
2871 build_unary_op (code
, xarg
, noconvert
)
2872 enum tree_code code
;
2876 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2877 register tree arg
= xarg
;
2878 register tree argtype
= 0;
2879 register enum tree_code typecode
= TREE_CODE (TREE_TYPE (arg
));
2880 char *errstring
= NULL
;
2883 if (typecode
== ERROR_MARK
)
2884 return error_mark_node
;
2885 if (typecode
== ENUMERAL_TYPE
)
2886 typecode
= INTEGER_TYPE
;
2891 /* This is used for unary plus, because a CONVERT_EXPR
2892 is enough to prevent anybody from looking inside for
2893 associativity, but won't generate any code. */
2894 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2895 errstring
= "wrong type argument to unary plus";
2896 else if (!noconvert
)
2897 arg
= default_conversion (arg
);
2901 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2902 errstring
= "wrong type argument to unary minus";
2903 else if (!noconvert
)
2904 arg
= default_conversion (arg
);
2908 if (typecode
!= INTEGER_TYPE
)
2909 errstring
= "wrong type argument to bit-complement";
2910 else if (!noconvert
)
2911 arg
= default_conversion (arg
);
2915 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
2916 errstring
= "wrong type argument to abs";
2917 else if (!noconvert
)
2918 arg
= default_conversion (arg
);
2921 case TRUTH_NOT_EXPR
:
2922 if (typecode
!= INTEGER_TYPE
2923 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
2924 /* These will convert to a pointer. */
2925 && typecode
!= ARRAY_TYPE
&& typecode
!= FUNCTION_TYPE
)
2927 errstring
= "wrong type argument to unary exclamation mark";
2930 arg
= truthvalue_conversion (arg
);
2931 return invert_truthvalue (arg
);
2936 case PREINCREMENT_EXPR
:
2937 case POSTINCREMENT_EXPR
:
2938 case PREDECREMENT_EXPR
:
2939 case POSTDECREMENT_EXPR
:
2940 /* Handle complex lvalues (when permitted)
2941 by reduction to simpler cases. */
2943 val
= unary_complex_lvalue (code
, arg
);
2947 /* Report invalid types. */
2949 if (typecode
!= POINTER_TYPE
2950 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
)
2952 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
2953 errstring
="wrong type argument to increment";
2955 errstring
="wrong type argument to decrement";
2961 tree result_type
= TREE_TYPE (arg
);
2963 arg
= get_unwidened (arg
, 0);
2964 argtype
= TREE_TYPE (arg
);
2966 /* Compute the increment. */
2968 if (typecode
== POINTER_TYPE
)
2970 if ((pedantic
|| warn_pointer_arith
)
2971 && (TREE_CODE (TREE_TYPE (result_type
)) == FUNCTION_TYPE
2972 || TREE_CODE (TREE_TYPE (result_type
)) == VOID_TYPE
))
2973 pedwarn ("wrong type argument to %s",
2974 ((code
== PREINCREMENT_EXPR
2975 || code
== POSTINCREMENT_EXPR
)
2976 ? "increment" : "decrement"));
2977 inc
= c_sizeof_nowarn (TREE_TYPE (result_type
));
2980 inc
= integer_one_node
;
2982 inc
= convert (argtype
, inc
);
2984 /* Handle incrementing a cast-expression. */
2987 switch (TREE_CODE (arg
))
2992 case FIX_TRUNC_EXPR
:
2993 case FIX_FLOOR_EXPR
:
2994 case FIX_ROUND_EXPR
:
2996 /* If the real type has the same machine representation
2997 as the type it is cast to, we can make better output
2998 by adding directly to the inside of the cast. */
2999 if ((TREE_CODE (TREE_TYPE (arg
))
3000 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))))
3001 && (TYPE_MODE (TREE_TYPE (arg
))
3002 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg
, 0)))))
3003 arg
= TREE_OPERAND (arg
, 0);
3006 tree incremented
, modify
, value
;
3007 pedantic_lvalue_warning (CONVERT_EXPR
);
3008 arg
= stabilize_reference (arg
);
3009 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
3012 value
= save_expr (arg
);
3013 incremented
= build (((code
== PREINCREMENT_EXPR
3014 || code
== POSTINCREMENT_EXPR
)
3015 ? PLUS_EXPR
: MINUS_EXPR
),
3016 argtype
, value
, inc
);
3017 TREE_SIDE_EFFECTS (incremented
) = 1;
3018 modify
= build_modify_expr (arg
, NOP_EXPR
, incremented
);
3019 value
= build (COMPOUND_EXPR
, TREE_TYPE (arg
), modify
, value
);
3020 TREE_USED (value
) = 1;
3030 /* Complain about anything else that is not a true lvalue. */
3031 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
3032 || code
== POSTINCREMENT_EXPR
)
3033 ? "increment" : "decrement")))
3034 return error_mark_node
;
3036 /* Report a read-only lvalue. */
3037 if (TREE_READONLY (arg
))
3038 readonly_warning (arg
,
3039 ((code
== PREINCREMENT_EXPR
3040 || code
== POSTINCREMENT_EXPR
)
3041 ? "increment" : "decrement"));
3043 val
= build (code
, TREE_TYPE (arg
), arg
, inc
);
3044 TREE_SIDE_EFFECTS (val
) = 1;
3045 val
= convert (result_type
, val
);
3046 if (TREE_CODE (val
) != code
)
3047 TREE_NO_UNUSED_WARNING (val
) = 1;
3052 /* Note that this operation never does default_conversion
3053 regardless of NOCONVERT. */
3055 /* Let &* cancel out to simplify resulting code. */
3056 if (TREE_CODE (arg
) == INDIRECT_REF
)
3058 /* Don't let this be an lvalue. */
3059 if (lvalue_p (TREE_OPERAND (arg
, 0)))
3060 return non_lvalue (TREE_OPERAND (arg
, 0));
3061 return TREE_OPERAND (arg
, 0);
3064 /* For &x[y], return x+y */
3065 if (TREE_CODE (arg
) == ARRAY_REF
)
3067 if (mark_addressable (TREE_OPERAND (arg
, 0)) == 0)
3068 return error_mark_node
;
3069 return build_binary_op (PLUS_EXPR
, TREE_OPERAND (arg
, 0),
3070 TREE_OPERAND (arg
, 1), 1);
3073 /* Handle complex lvalues (when permitted)
3074 by reduction to simpler cases. */
3075 val
= unary_complex_lvalue (code
, arg
);
3079 #if 0 /* Turned off because inconsistent;
3080 float f; *&(int)f = 3.4 stores in int format
3081 whereas (int)f = 3.4 stores in float format. */
3082 /* Address of a cast is just a cast of the address
3083 of the operand of the cast. */
3084 switch (TREE_CODE (arg
))
3089 case FIX_TRUNC_EXPR
:
3090 case FIX_FLOOR_EXPR
:
3091 case FIX_ROUND_EXPR
:
3094 pedwarn ("ANSI C forbids the address of a cast expression");
3095 return convert (build_pointer_type (TREE_TYPE (arg
)),
3096 build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0),
3101 /* Allow the address of a constructor if all the elements
3103 if (TREE_CODE (arg
) == CONSTRUCTOR
&& TREE_CONSTANT (arg
))
3105 /* Anything not already handled and not a true memory reference
3107 else if (typecode
!= FUNCTION_TYPE
&& !lvalue_or_else (arg
, "unary `&'"))
3108 return error_mark_node
;
3110 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3111 argtype
= TREE_TYPE (arg
);
3112 /* If the lvalue is const or volatile,
3113 merge that into the type that the address will point to. */
3114 if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'd'
3115 || TREE_CODE_CLASS (TREE_CODE (arg
)) == 'r')
3117 if (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
3118 argtype
= c_build_type_variant (argtype
,
3119 TREE_READONLY (arg
),
3120 TREE_THIS_VOLATILE (arg
));
3123 argtype
= build_pointer_type (argtype
);
3125 if (mark_addressable (arg
) == 0)
3126 return error_mark_node
;
3131 if (TREE_CODE (arg
) == COMPONENT_REF
)
3133 tree field
= TREE_OPERAND (arg
, 1);
3135 addr
= build_unary_op (ADDR_EXPR
, TREE_OPERAND (arg
, 0), 0);
3137 if (DECL_BIT_FIELD (field
))
3139 error ("attempt to take address of bit-field structure member `%s'",
3140 IDENTIFIER_POINTER (DECL_NAME (field
)));
3141 return error_mark_node
;
3144 addr
= convert (argtype
, addr
);
3146 if (! integer_zerop (DECL_FIELD_BITPOS (field
)))
3149 = size_binop (EASY_DIV_EXPR
, DECL_FIELD_BITPOS (field
),
3150 size_int (BITS_PER_UNIT
));
3151 int flag
= TREE_CONSTANT (addr
);
3152 addr
= fold (build (PLUS_EXPR
, argtype
,
3153 addr
, convert (argtype
, offset
)));
3154 TREE_CONSTANT (addr
) = flag
;
3158 addr
= build1 (code
, argtype
, arg
);
3160 /* Address of a static or external variable or
3161 function counts as a constant */
3162 TREE_CONSTANT (addr
) = staticp (arg
);
3170 argtype
= TREE_TYPE (arg
);
3171 return fold (build1 (code
, argtype
, arg
));
3175 return error_mark_node
;
3179 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3180 convert ARG with the same conversions in the same order
3181 and return the result. */
3184 convert_sequence (conversions
, arg
)
3188 switch (TREE_CODE (conversions
))
3193 case FIX_TRUNC_EXPR
:
3194 case FIX_FLOOR_EXPR
:
3195 case FIX_ROUND_EXPR
:
3197 return convert (TREE_TYPE (conversions
),
3198 convert_sequence (TREE_OPERAND (conversions
, 0),
3207 /* Return nonzero if REF is an lvalue valid for this language.
3208 Lvalues can be assigned, unless their type has TYPE_READONLY.
3209 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3215 register enum tree_code code
= TREE_CODE (ref
);
3220 return lvalue_p (TREE_OPERAND (ref
, 0));
3231 if (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
3232 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
)
3239 /* Return nonzero if REF is an lvalue valid for this language;
3240 otherwise, print an error message and return zero. */
3243 lvalue_or_else (ref
, string
)
3247 int win
= lvalue_p (ref
);
3249 error ("invalid lvalue in %s", string
);
3253 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3254 for certain kinds of expressions which are not really lvalues
3255 but which we can accept as lvalues.
3257 If ARG is not a kind of expression we can handle, return zero. */
3260 unary_complex_lvalue (code
, arg
)
3261 enum tree_code code
;
3264 /* Handle (a, b) used as an "lvalue". */
3265 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
3267 tree real_result
= build_unary_op (code
, TREE_OPERAND (arg
, 1), 0);
3268 pedantic_lvalue_warning (COMPOUND_EXPR
);
3269 return build (COMPOUND_EXPR
, TREE_TYPE (real_result
),
3270 TREE_OPERAND (arg
, 0), real_result
);
3273 /* Handle (a ? b : c) used as an "lvalue". */
3274 if (TREE_CODE (arg
) == COND_EXPR
)
3276 pedantic_lvalue_warning (COND_EXPR
);
3277 return (build_conditional_expr
3278 (TREE_OPERAND (arg
, 0),
3279 build_unary_op (code
, TREE_OPERAND (arg
, 1), 0),
3280 build_unary_op (code
, TREE_OPERAND (arg
, 2), 0)));
3286 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3287 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3290 pedantic_lvalue_warning (code
)
3291 enum tree_code code
;
3294 pedwarn ("ANSI C forbids use of %s expressions as lvalues",
3295 code
== COND_EXPR
? "conditional"
3296 : code
== COMPOUND_EXPR
? "compound" : "cast");
3299 /* Warn about storing in something that is `const'. */
3302 readonly_warning (arg
, string
)
3307 strcpy (buf
, string
);
3309 if (TREE_CODE (arg
) == COMPONENT_REF
)
3311 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg
, 0))))
3312 readonly_warning (TREE_OPERAND (arg
, 0), string
);
3315 strcat (buf
, " of read-only member `%s'");
3316 pedwarn (buf
, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg
, 1))));
3319 else if (TREE_CODE (arg
) == VAR_DECL
)
3321 strcat (buf
, " of read-only variable `%s'");
3322 pedwarn (buf
, IDENTIFIER_POINTER (DECL_NAME (arg
)));
3326 pedwarn ("%s of read-only location", buf
);
3330 /* Mark EXP saying that we need to be able to take the
3331 address of it; it should not be allocated in a register.
3332 Value is 1 if successful. */
3335 mark_addressable (exp
)
3338 register tree x
= exp
;
3340 switch (TREE_CODE (x
))
3345 x
= TREE_OPERAND (x
, 0);
3349 TREE_ADDRESSABLE (x
) = 1;
3356 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
3357 && DECL_NONLOCAL (x
))
3359 if (TREE_PUBLIC (x
))
3361 error ("global register variable `%s' used in nested function",
3362 IDENTIFIER_POINTER (DECL_NAME (x
)));
3365 pedwarn ("register variable `%s' used in nested function",
3366 IDENTIFIER_POINTER (DECL_NAME (x
)));
3368 else if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
))
3370 if (TREE_PUBLIC (x
))
3372 error ("address of global register variable `%s' requested",
3373 IDENTIFIER_POINTER (DECL_NAME (x
)));
3376 pedwarn ("address of register variable `%s' requested",
3377 IDENTIFIER_POINTER (DECL_NAME (x
)));
3379 put_var_into_stack (x
);
3383 TREE_ADDRESSABLE (x
) = 1;
3384 #if 0 /* poplevel deals with this now. */
3385 if (DECL_CONTEXT (x
) == 0)
3386 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x
)) = 1;
3394 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3397 build_conditional_expr (ifexp
, op1
, op2
)
3398 tree ifexp
, op1
, op2
;
3400 register tree type1
;
3401 register tree type2
;
3402 register enum tree_code code1
;
3403 register enum tree_code code2
;
3404 register tree result_type
= NULL
;
3406 /* If second operand is omitted, it is the same as the first one;
3407 make sure it is calculated only once. */
3411 pedwarn ("ANSI C forbids omitting the middle term of a ?: expression");
3412 ifexp
= op1
= save_expr (ifexp
);
3415 ifexp
= truthvalue_conversion (default_conversion (ifexp
));
3417 if (TREE_CODE (ifexp
) == ERROR_MARK
3418 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
3419 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
3420 return error_mark_node
;
3422 #if 0 /* Produces wrong result if within sizeof. */
3423 /* Don't promote the operands separately if they promote
3424 the same way. Return the unpromoted type and let the combined
3425 value get promoted if necessary. */
3427 if (TREE_TYPE (op1
) == TREE_TYPE (op2
)
3428 && TREE_CODE (TREE_TYPE (op1
)) != ARRAY_TYPE
3429 && TREE_CODE (TREE_TYPE (op1
)) != ENUMERAL_TYPE
3430 && TREE_CODE (TREE_TYPE (op1
)) != FUNCTION_TYPE
)
3432 if (TREE_CODE (ifexp
) == INTEGER_CST
)
3433 return (integer_zerop (ifexp
) ? op2
: op1
);
3435 return fold (build (COND_EXPR
, TREE_TYPE (op1
), ifexp
, op1
, op2
));
3439 /* They don't match; promote them both and then try to reconcile them. */
3441 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
3442 op1
= default_conversion (op1
);
3443 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
3444 op2
= default_conversion (op2
);
3446 type1
= TREE_TYPE (op1
);
3447 code1
= TREE_CODE (type1
);
3448 type2
= TREE_TYPE (op2
);
3449 code2
= TREE_CODE (type2
);
3451 /* Quickly detect the usual case where op1 and op2 have the same type
3454 result_type
= type1
;
3455 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
)
3456 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
))
3458 result_type
= common_type (type1
, type2
);
3460 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
3462 if (pedantic
&& (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
))
3463 pedwarn ("ANSI C forbids conditional expr with only one void side");
3464 result_type
= void_type_node
;
3466 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
3468 if (comp_target_types (type1
, type2
))
3469 result_type
= common_type (type1
, type2
);
3470 else if (integer_zerop (op1
) && TREE_TYPE (type1
) == void_type_node
)
3471 result_type
= qualify_type (type2
, type1
);
3472 else if (integer_zerop (op2
) && TREE_TYPE (type2
) == void_type_node
)
3473 result_type
= qualify_type (type1
, type2
);
3474 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1
)) == void_type_node
)
3476 if (pedantic
&& TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
3477 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3478 result_type
= qualify_type (type1
, type2
);
3480 else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2
)) == void_type_node
)
3482 if (pedantic
&& TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
3483 pedwarn ("ANSI C forbids conditional expr between `void *' and function pointer");
3484 result_type
= qualify_type (type2
, type1
);
3488 pedwarn ("pointer type mismatch in conditional expression");
3489 result_type
= build_pointer_type (void_type_node
);
3492 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
3494 if (! integer_zerop (op2
))
3495 pedwarn ("pointer/integer type mismatch in conditional expression");
3498 op2
= null_pointer_node
;
3499 #if 0 /* The spec seems to say this is permitted. */
3500 if (pedantic
&& TREE_CODE (type1
) == FUNCTION_TYPE
)
3501 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3504 result_type
= type1
;
3506 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
3508 if (!integer_zerop (op1
))
3509 pedwarn ("pointer/integer type mismatch in conditional expression");
3512 op1
= null_pointer_node
;
3513 #if 0 /* The spec seems to say this is permitted. */
3514 if (pedantic
&& TREE_CODE (type2
) == FUNCTION_TYPE
)
3515 pedwarn ("ANSI C forbids conditional expr between 0 and function pointer");
3518 result_type
= type2
;
3523 if (flag_cond_mismatch
)
3524 result_type
= void_type_node
;
3527 error ("type mismatch in conditional expression");
3528 return error_mark_node
;
3532 if (result_type
!= TREE_TYPE (op1
))
3533 op1
= convert (result_type
, op1
);
3534 if (result_type
!= TREE_TYPE (op2
))
3535 op2
= convert (result_type
, op2
);
3538 if (code1
== RECORD_TYPE
|| code1
== UNION_TYPE
)
3540 result_type
= TREE_TYPE (op1
);
3541 if (TREE_CONSTANT (ifexp
))
3542 return (integer_zerop (ifexp
) ? op2
: op1
);
3544 if (TYPE_MODE (result_type
) == BLKmode
)
3546 register tree tempvar
3547 = build_decl (VAR_DECL
, NULL_TREE
, result_type
);
3548 register tree xop1
= build_modify_expr (tempvar
, op1
);
3549 register tree xop2
= build_modify_expr (tempvar
, op2
);
3550 register tree result
= fold (build (COND_EXPR
, result_type
,
3551 ifexp
, xop1
, xop2
));
3553 layout_decl (tempvar
, TYPE_ALIGN (result_type
));
3554 /* No way to handle variable-sized objects here.
3555 I fear that the entire handling of BLKmode conditional exprs
3556 needs to be redone. */
3557 if (TREE_CODE (DECL_SIZE (tempvar
)) != INTEGER_CST
)
3560 = assign_stack_local (DECL_MODE (tempvar
),
3561 (TREE_INT_CST_LOW (DECL_SIZE (tempvar
))
3562 + BITS_PER_UNIT
- 1)
3566 TREE_SIDE_EFFECTS (result
)
3567 = TREE_SIDE_EFFECTS (ifexp
) | TREE_SIDE_EFFECTS (op1
)
3568 | TREE_SIDE_EFFECTS (op2
);
3569 return build (COMPOUND_EXPR
, result_type
, result
, tempvar
);
3574 if (TREE_CODE (ifexp
) == INTEGER_CST
)
3575 return (integer_zerop (ifexp
) ? op2
: op1
);
3576 return fold (build (COND_EXPR
, result_type
, ifexp
, op1
, op2
));
3579 /* Given a list of expressions, return a compound expression
3580 that performs them all and returns the value of the last of them. */
3583 build_compound_expr (list
)
3588 if (TREE_CHAIN (list
) == 0)
3590 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3591 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3593 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3594 if (TREE_CODE (list
) == NON_LVALUE_EXPR
)
3595 list
= TREE_OPERAND (list
, 0);
3598 return TREE_VALUE (list
);
3601 if (TREE_CHAIN (list
) != 0 && TREE_CHAIN (TREE_CHAIN (list
)) == 0)
3603 /* Convert arrays to pointers when there really is a comma operator. */
3604 if (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (list
)))) == ARRAY_TYPE
)
3605 TREE_VALUE (TREE_CHAIN (list
))
3606 = default_conversion (TREE_VALUE (TREE_CHAIN (list
)));
3609 rest
= build_compound_expr (TREE_CHAIN (list
));
3611 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list
)))
3614 return build (COMPOUND_EXPR
, TREE_TYPE (rest
), TREE_VALUE (list
), rest
);
3617 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3620 build_c_cast (type
, expr
)
3624 register tree value
= expr
;
3626 if (type
== error_mark_node
|| expr
== error_mark_node
)
3627 return error_mark_node
;
3628 type
= TYPE_MAIN_VARIANT (type
);
3631 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3632 if (TREE_CODE (value
) == NON_LVALUE_EXPR
)
3633 value
= TREE_OPERAND (value
, 0);
3636 if (TREE_CODE (type
) == ARRAY_TYPE
)
3638 error ("cast specifies array type");
3639 return error_mark_node
;
3642 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3644 error ("cast specifies function type");
3645 return error_mark_node
;
3648 if (type
== TREE_TYPE (value
))
3652 if (TREE_CODE (type
) == RECORD_TYPE
3653 || TREE_CODE (type
) == UNION_TYPE
)
3654 pedwarn ("ANSI C forbids casting nonscalar to the same type");
3657 else if (TREE_CODE (type
) == UNION_TYPE
)
3660 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3661 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
3662 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
3671 pedwarn ("ANSI C forbids casts to union type");
3672 if (TYPE_NAME (type
) != 0)
3674 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
3675 name
= IDENTIFIER_POINTER (TYPE_NAME (type
));
3677 name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
)));
3681 return digest_init (type
, build_nt (CONSTRUCTOR
, NULL_TREE
,
3682 build_tree_list (field
, value
)),
3683 NULL_PTR
, 0, 0, name
);
3685 error ("cast to union type from type not present in union");
3686 return error_mark_node
;
3691 /* Convert functions and arrays to pointers,
3692 but don't convert any other types. */
3693 if (TREE_CODE (TREE_TYPE (value
)) == FUNCTION_TYPE
3694 || TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
)
3695 value
= default_conversion (value
);
3696 otype
= TREE_TYPE (value
);
3698 /* Optionally warn about potentially worrisome casts. */
3701 && TREE_CODE (type
) == POINTER_TYPE
3702 && TREE_CODE (otype
) == POINTER_TYPE
)
3704 if (TYPE_VOLATILE (TREE_TYPE (otype
))
3705 && ! TYPE_VOLATILE (TREE_TYPE (type
)))
3706 pedwarn ("cast discards `volatile' from pointer target type");
3707 if (TYPE_READONLY (TREE_TYPE (otype
))
3708 && ! TYPE_READONLY (TREE_TYPE (type
)))
3709 pedwarn ("cast discards `const' from pointer target type");
3712 /* Warn about possible alignment problems. */
3713 if (STRICT_ALIGNMENT
&& warn_cast_align
3714 && TREE_CODE (type
) == POINTER_TYPE
3715 && TREE_CODE (otype
) == POINTER_TYPE
3716 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
3717 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
3718 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
3719 warning ("cast increases required alignment of target type");
3721 if (TREE_CODE (type
) == INTEGER_TYPE
3722 && TREE_CODE (otype
) == POINTER_TYPE
3723 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
3724 warning ("cast from pointer to integer of different size");
3726 if (TREE_CODE (type
) == POINTER_TYPE
3727 && TREE_CODE (otype
) == INTEGER_TYPE
3728 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
3729 /* Don't warn about converting 0 to pointer,
3730 provided the 0 was explicit--not cast or made by folding. */
3731 && !(TREE_CODE (value
) == INTEGER_CST
&& integer_zerop (value
)))
3732 warning ("cast to pointer from integer of different size");
3734 value
= convert (type
, value
);
3737 if (value
== expr
&& pedantic
)
3739 /* If pedantic, don't let a cast be an lvalue. */
3740 return non_lvalue (value
);
3745 /* Build an assignment expression of lvalue LHS from value RHS.
3746 MODIFYCODE is the code for a binary operator that we use
3747 to combine the old value of LHS with RHS to get the new value.
3748 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3751 build_modify_expr (lhs
, modifycode
, rhs
)
3753 enum tree_code modifycode
;
3755 register tree result
;
3757 tree lhstype
= TREE_TYPE (lhs
);
3758 tree olhstype
= lhstype
;
3760 /* Types that aren't fully specified cannot be used in assignments. */
3761 lhs
= require_complete_type (lhs
);
3763 /* Avoid duplicate error messages from operands that had errors. */
3764 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
3765 return error_mark_node
;
3767 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3768 /* Do not use STRIP_NOPS here. We do not want an enumerator
3769 whose value is 0 to count as a null pointer constant. */
3770 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3771 rhs
= TREE_OPERAND (rhs
, 0);
3775 /* Handle control structure constructs used as "lvalues". */
3777 switch (TREE_CODE (lhs
))
3779 /* Handle (a, b) used as an "lvalue". */
3781 pedantic_lvalue_warning (COMPOUND_EXPR
);
3782 return build (COMPOUND_EXPR
, lhstype
,
3783 TREE_OPERAND (lhs
, 0),
3784 build_modify_expr (TREE_OPERAND (lhs
, 1),
3787 /* Handle (a ? b : c) used as an "lvalue". */
3789 pedantic_lvalue_warning (COND_EXPR
);
3790 rhs
= save_expr (rhs
);
3792 /* Produce (a ? (b = rhs) : (c = rhs))
3793 except that the RHS goes through a save-expr
3794 so the code to compute it is only emitted once. */
3796 = build_conditional_expr (TREE_OPERAND (lhs
, 0),
3797 build_modify_expr (TREE_OPERAND (lhs
, 1),
3799 build_modify_expr (TREE_OPERAND (lhs
, 2),
3801 /* Make sure the code to compute the rhs comes out
3802 before the split. */
3803 return build (COMPOUND_EXPR
, TREE_TYPE (lhs
),
3804 /* But cast it to void to avoid an "unused" error. */
3805 convert (void_type_node
, rhs
), cond
);
3809 /* If a binary op has been requested, combine the old LHS value with the RHS
3810 producing the value we should actually store into the LHS. */
3812 if (modifycode
!= NOP_EXPR
)
3814 lhs
= stabilize_reference (lhs
);
3815 newrhs
= build_binary_op (modifycode
, lhs
, rhs
, 1);
3818 /* Handle a cast used as an "lvalue".
3819 We have already performed any binary operator using the value as cast.
3820 Now convert the result to the cast type of the lhs,
3821 and then true type of the lhs and store it there;
3822 then convert result back to the cast type to be the value
3823 of the assignment. */
3825 switch (TREE_CODE (lhs
))
3830 case FIX_TRUNC_EXPR
:
3831 case FIX_FLOOR_EXPR
:
3832 case FIX_ROUND_EXPR
:
3834 if (TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
3835 || TREE_CODE (TREE_TYPE (newrhs
)) == FUNCTION_TYPE
)
3836 newrhs
= default_conversion (newrhs
);
3838 tree inner_lhs
= TREE_OPERAND (lhs
, 0);
3840 result
= build_modify_expr (inner_lhs
, NOP_EXPR
,
3841 convert (TREE_TYPE (inner_lhs
),
3842 convert (lhstype
, newrhs
)));
3843 pedantic_lvalue_warning (CONVERT_EXPR
);
3844 return convert (TREE_TYPE (lhs
), result
);
3848 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3849 Reject anything strange now. */
3851 if (!lvalue_or_else (lhs
, "assignment"))
3852 return error_mark_node
;
3854 /* Warn about storing in something that is `const'. */
3856 if (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
)
3857 || ((TREE_CODE (lhstype
) == RECORD_TYPE
3858 || TREE_CODE (lhstype
) == UNION_TYPE
)
3859 && C_TYPE_FIELDS_READONLY (lhstype
)))
3860 readonly_warning (lhs
, "assignment");
3862 /* If storing into a structure or union member,
3863 it has probably been given type `int'.
3864 Compute the type that would go with
3865 the actual amount of storage the member occupies. */
3867 if (TREE_CODE (lhs
) == COMPONENT_REF
3868 && (TREE_CODE (lhstype
) == INTEGER_TYPE
3869 || TREE_CODE (lhstype
) == REAL_TYPE
3870 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
3871 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
3873 /* If storing in a field that is in actuality a short or narrower than one,
3874 we must store in the field in its actual type. */
3876 if (lhstype
!= TREE_TYPE (lhs
))
3878 lhs
= copy_node (lhs
);
3879 TREE_TYPE (lhs
) = lhstype
;
3882 /* Convert new value to destination type. */
3884 newrhs
= convert_for_assignment (lhstype
, newrhs
, "assignment",
3886 if (TREE_CODE (newrhs
) == ERROR_MARK
)
3887 return error_mark_node
;
3889 result
= build (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
3890 TREE_SIDE_EFFECTS (result
) = 1;
3892 /* If we got the LHS in a different type for storing in,
3893 convert the result back to the nominal type of LHS
3894 so that the value we return always has the same type
3895 as the LHS argument. */
3897 if (olhstype
== TREE_TYPE (result
))
3899 return convert_for_assignment (olhstype
, result
, "assignment", NULL_TREE
, 0);
3902 /* Convert value RHS to type TYPE as preparation for an assignment
3903 to an lvalue of type TYPE.
3904 The real work of conversion is done by `convert'.
3905 The purpose of this function is to generate error messages
3906 for assignments that are not allowed in C.
3907 ERRTYPE is a string to use in error messages:
3908 "assignment", "return", etc. If it is null, this is parameter passing
3909 for a function call (and different error messages are output). Otherwise,
3910 it may be a name stored in the spelling stack and interpreted by
3913 FUNNAME is the name of the function being called,
3914 as an IDENTIFIER_NODE, or null.
3915 PARMNUM is the number of the argument, for printing in error messages. */
3918 convert_for_assignment (type
, rhs
, errtype
, funname
, parmnum
)
3924 register enum tree_code codel
= TREE_CODE (type
);
3925 register tree rhstype
;
3926 register enum tree_code coder
;
3928 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3929 /* Do not use STRIP_NOPS here. We do not want an enumerator
3930 whose value is 0 to count as a null pointer constant. */
3931 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
3932 rhs
= TREE_OPERAND (rhs
, 0);
3934 if (TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
3935 || TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
)
3936 rhs
= default_conversion (rhs
);
3938 rhstype
= TREE_TYPE (rhs
);
3939 coder
= TREE_CODE (rhstype
);
3941 if (coder
== ERROR_MARK
)
3942 return error_mark_node
;
3944 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
3947 if (coder
== VOID_TYPE
)
3949 error ("void value not ignored as it ought to be");
3950 return error_mark_node
;
3952 /* Arithmetic types all interconvert, and enum is treated like int. */
3953 if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
|| codel
== ENUMERAL_TYPE
)
3955 (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
|| coder
== ENUMERAL_TYPE
))
3957 return convert (type
, rhs
);
3959 /* Conversions among pointers */
3960 else if (codel
== POINTER_TYPE
&& coder
== POINTER_TYPE
)
3962 register tree ttl
= TREE_TYPE (type
);
3963 register tree ttr
= TREE_TYPE (rhstype
);
3965 /* Any non-function converts to a [const][volatile] void *
3966 and vice versa; otherwise, targets must be the same.
3967 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3968 if (TYPE_MAIN_VARIANT (ttl
) == void_type_node
3969 || TYPE_MAIN_VARIANT (ttr
) == void_type_node
3970 || comp_target_types (type
, rhstype
)
3971 || (!pedantic
/* Unless pedantic, mix signed and unsigned. */
3972 && TREE_CODE (ttl
) == INTEGER_TYPE
3973 && TREE_CODE (ttr
) == INTEGER_TYPE
3974 && TYPE_PRECISION (ttl
) == TYPE_PRECISION (ttr
)))
3977 && ((TYPE_MAIN_VARIANT (ttl
) == void_type_node
3978 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
3980 (TYPE_MAIN_VARIANT (ttr
) == void_type_node
3981 && !integer_zerop (rhs
)
3982 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
3983 warn_for_assignment ("ANSI forbids %s between function pointer and `void *'",
3984 get_spelling (errtype
), funname
, parmnum
);
3985 /* Const and volatile mean something different for function types,
3986 so the usual warnings are not appropriate. */
3987 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
3988 || TREE_CODE (ttl
) != FUNCTION_TYPE
)
3990 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
3991 warn_for_assignment ("%s discards `const' from pointer target type",
3992 get_spelling (errtype
), funname
, parmnum
);
3993 if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
3994 warn_for_assignment ("%s discards `volatile' from pointer target type",
3995 get_spelling (errtype
), funname
, parmnum
);
3999 /* Because const and volatile on functions are restrictions
4000 that say the function will not do certain things,
4001 it is okay to use a const or volatile function
4002 where an ordinary one is wanted, but not vice-versa. */
4003 if (TYPE_READONLY (ttl
) && ! TYPE_READONLY (ttr
))
4004 warn_for_assignment ("%s makes `const *' function pointer from non-const",
4005 get_spelling (errtype
), funname
, parmnum
);
4006 if (TYPE_VOLATILE (ttl
) && ! TYPE_VOLATILE (ttr
))
4007 warn_for_assignment ("%s makes `volatile *' function pointer from non-volatile",
4008 get_spelling (errtype
), funname
, parmnum
);
4011 else if (unsigned_type (TYPE_MAIN_VARIANT (ttl
))
4012 == unsigned_type (TYPE_MAIN_VARIANT (ttr
)))
4013 warn_for_assignment ("pointer targets in %s differ in signedness",
4014 get_spelling (errtype
), funname
, parmnum
);
4016 warn_for_assignment ("%s from incompatible pointer type",
4017 get_spelling (errtype
), funname
, parmnum
);
4018 return convert (type
, rhs
);
4020 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
4022 /* An explicit constant 0 can convert to a pointer,
4023 but not a 0 that results from casting or folding. */
4024 if (! (TREE_CODE (rhs
) == INTEGER_CST
&& integer_zerop (rhs
)))
4026 warn_for_assignment ("%s makes pointer from integer without a cast",
4027 get_spelling (errtype
), funname
, parmnum
);
4028 return convert (type
, rhs
);
4030 return null_pointer_node
;
4032 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
4034 warn_for_assignment ("%s makes integer from pointer without a cast",
4035 get_spelling (errtype
), funname
, parmnum
);
4036 return convert (type
, rhs
);
4042 error ("incompatible type for argument %d of `%s'",
4043 parmnum
, IDENTIFIER_POINTER (funname
));
4045 error ("incompatible type for argument %d of indirect function call",
4049 error ("incompatible types in %s", get_spelling (errtype
));
4051 return error_mark_node
;
4054 /* Print a warning using MSG.
4055 It gets OPNAME as its one parameter.
4056 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4057 FUNCTION and ARGNUM are handled specially if we are building an
4058 Objective-C selector. */
4061 warn_for_assignment (msg
, opname
, function
, argnum
)
4067 static char argstring
[] = "passing arg %d of `%s'";
4068 static char argnofun
[] = "passing arg %d";
4072 tree selector
= maybe_building_objc_message_expr ();
4074 if (selector
&& argnum
> 2)
4076 function
= selector
;
4081 /* Function name is known; supply it. */
4082 opname
= (char *) alloca (IDENTIFIER_LENGTH (function
)
4083 + sizeof (argstring
) + 25 /*%d*/ + 1);
4084 sprintf (opname
, argstring
, argnum
, IDENTIFIER_POINTER (function
));
4088 /* Function name unknown (call through ptr); just give arg number. */
4089 opname
= (char *) alloca (sizeof (argnofun
) + 25 /*%d*/ + 1);
4090 sprintf (opname
, argnofun
, argnum
);
4093 pedwarn (msg
, opname
);
4096 /* Return nonzero if VALUE is a valid constant-valued expression
4097 for use in initializing a static variable; one that can be an
4098 element of a "constant" initializer.
4100 Return null_pointer_node if the value is absolute;
4101 if it is relocatable, return the variable that determines the relocation.
4102 We assume that VALUE has been folded as much as possible;
4103 therefore, we do not need to check for such things as
4104 arithmetic-combinations of integers. */
4107 initializer_constant_valid_p (value
)
4110 switch (TREE_CODE (value
))
4113 return TREE_STATIC (value
) ? null_pointer_node
: 0;
4118 return null_pointer_node
;
4121 return TREE_OPERAND (value
, 0);
4123 case NON_LVALUE_EXPR
:
4124 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4128 /* Allow conversions between pointer types. */
4129 if (TREE_CODE (TREE_TYPE (value
)) == POINTER_TYPE
4130 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == POINTER_TYPE
)
4131 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4132 /* Allow conversions between real types. */
4133 if (TREE_CODE (TREE_TYPE (value
)) == REAL_TYPE
4134 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == REAL_TYPE
)
4135 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4136 /* Allow length-preserving conversions between integer types. */
4137 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
4138 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == INTEGER_TYPE
4139 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value
)),
4140 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value
, 0)))))
4141 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4142 /* Allow conversions between integer types only if explicit value. */
4143 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
4144 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == INTEGER_TYPE
)
4146 tree inner
= initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4147 if (inner
== null_pointer_node
)
4148 return null_pointer_node
;
4151 /* Allow (int) &foo. */
4152 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
4153 && TREE_CODE (TREE_TYPE (TREE_OPERAND (value
, 0))) == POINTER_TYPE
4154 && tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (value
)),
4155 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value
, 0)))))
4156 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4157 /* Allow conversions to union types if the value inside is okay. */
4158 if (TREE_CODE (TREE_TYPE (value
)) == UNION_TYPE
)
4159 return initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4164 tree valid0
= initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4165 tree valid1
= initializer_constant_valid_p (TREE_OPERAND (value
, 1));
4166 /* If either term is absolute, use the other terms relocation. */
4167 if (valid0
== null_pointer_node
)
4169 if (valid1
== null_pointer_node
)
4176 tree valid0
= initializer_constant_valid_p (TREE_OPERAND (value
, 0));
4177 tree valid1
= initializer_constant_valid_p (TREE_OPERAND (value
, 1));
4178 /* Win if second argument is absolute. */
4179 if (valid1
== null_pointer_node
)
4181 /* Win if both arguments have the same relocation.
4182 Then the value is absolute. */
4183 if (valid0
== valid1
)
4184 return null_pointer_node
;
4192 /* Perform appropriate conversions on the initial value of a variable,
4193 store it in the declaration DECL,
4194 and print any error messages that are appropriate.
4195 If the init is invalid, store an ERROR_MARK. */
4198 store_init_value (decl
, init
)
4201 register tree value
, type
;
4203 /* If variable's type was invalidly declared, just ignore it. */
4205 type
= TREE_TYPE (decl
);
4206 if (TREE_CODE (type
) == ERROR_MARK
)
4209 /* Digest the specified initializer into an expression. */
4211 value
= digest_init (type
, init
, NULL_PTR
, TREE_STATIC (decl
),
4212 TREE_STATIC (decl
) || pedantic
,
4213 IDENTIFIER_POINTER (DECL_NAME (decl
)));
4215 /* Store the expression if valid; else report error. */
4218 /* Note that this is the only place we can detect the error
4219 in a case such as struct foo bar = (struct foo) { x, y };
4220 where there is one initial value which is a constructor expression. */
4221 if (value
== error_mark_node
)
4223 else if (TREE_STATIC (decl
) && ! TREE_CONSTANT (value
))
4225 error ("initializer for static variable is not constant");
4226 value
= error_mark_node
;
4228 else if (TREE_STATIC (decl
)
4229 && initializer_constant_valid_p (value
) == 0)
4231 error ("initializer for static variable uses complicated arithmetic");
4232 value
= error_mark_node
;
4236 if (pedantic
&& TREE_CODE (value
) == CONSTRUCTOR
)
4238 if (! TREE_CONSTANT (value
))
4239 pedwarn ("aggregate initializer is not constant");
4240 else if (! TREE_STATIC (value
))
4241 pedwarn ("aggregate initializer uses complicated arithmetic");
4246 /* ANSI wants warnings about out-of-range constant initializers. */
4247 constant_expression_warning (value
);
4249 DECL_INITIAL (decl
) = value
;
4252 /* Methods for storing and printing names for error messages.
4254 /* Implement a spelling stack that allows components of a name to be pushed
4255 and popped. Each element on the stack is this structure. */
4267 #define SPELLING_STRING 1
4268 #define SPELLING_MEMBER 2
4269 #define SPELLING_BOUNDS 3
4271 static struct spelling
*spelling
; /* Next stack element (unused). */
4272 static struct spelling
*spelling_base
; /* Spelling stack base. */
4273 static int spelling_size
; /* Size of the spelling stack. */
4275 /* Macros to save and restore the spelling stack around push_... functions.
4276 Alternative to SAVE_SPELLING_STACK. */
4278 #define SPELLING_DEPTH() (spelling - spelling_base)
4279 #define RESTORE_SPELLING_DEPTH(depth) (spelling = spelling_base + depth)
4281 /* Save and restore the spelling stack around arbitrary C code. */
4283 #define SAVE_SPELLING_DEPTH(code) \
4285 int __depth = SPELLING_DEPTH (); \
4287 RESTORE_SPELLING_DEPTH (__depth); \
4290 /* Push an element on the spelling stack with type KIND and assign VALUE
4293 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4295 int depth = SPELLING_DEPTH (); \
4297 if (depth >= spelling_size) \
4299 spelling_size += 10; \
4300 if (spelling_base == 0) \
4302 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4305 = (struct spelling *) xrealloc (spelling_base, \
4306 spelling_size * sizeof (struct spelling)); \
4307 RESTORE_SPELLING_DEPTH (depth); \
4310 spelling->kind = (KIND); \
4311 spelling->MEMBER = (VALUE); \
4315 /* Push STRING on the stack. Printed literally. */
4318 push_string (string
)
4321 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
4324 /* Push a member name on the stack. Printed as '.' STRING. */
4327 push_member_name (string
)
4330 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
4333 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4336 push_array_bounds (bounds
)
4339 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
4342 /* Compute the maximum size in bytes of the printed spelling. */
4347 register int size
= 0;
4348 register struct spelling
*p
;
4350 for (p
= spelling_base
; p
< spelling
; p
++)
4352 if (p
->kind
== SPELLING_BOUNDS
)
4355 size
+= strlen (p
->u
.s
) + 1;
4361 /* Print the spelling to BUFFER and return it. */
4364 print_spelling (buffer
)
4365 register char *buffer
;
4367 register char *d
= buffer
;
4369 register struct spelling
*p
;
4371 for (p
= spelling_base
; p
< spelling
; p
++)
4372 if (p
->kind
== SPELLING_BOUNDS
)
4374 sprintf (d
, "[%d]", p
->u
.i
);
4379 if (p
->kind
== SPELLING_MEMBER
)
4381 for (s
= p
->u
.s
; *d
= *s
++; d
++)
4388 /* Provide a means to pass component names derived from the spelling stack. */
4390 char initialization_message
;
4392 /* Interpret the spelling of the given ERRTYPE message. */
4395 get_spelling (errtype
)
4398 static char *buffer
;
4399 static int size
= -1;
4401 if (errtype
== &initialization_message
)
4403 /* Avoid counting chars */
4404 static char message
[] = "initialization of `%s'";
4405 register int needed
= sizeof (message
) + spelling_length () + 1;
4409 buffer
= (char *) xmalloc (size
= needed
);
4411 buffer
= (char *) xrealloc (buffer
, size
= needed
);
4413 temp
= (char *) alloca (needed
);
4414 sprintf (buffer
, message
, print_spelling (temp
));
4421 /* Issue an error message for a bad initializer component.
4422 FORMAT describes the message. OFWHAT is the name for the component.
4423 LOCAL is a format string for formatting the insertion of the name
4426 If OFWHAT is null, the component name is stored on the spelling stack.
4427 If the component name is a null string, then LOCAL is omitted entirely. */
4430 error_init (format
, local
, ofwhat
)
4431 char *format
, *local
, *ofwhat
;
4436 ofwhat
= print_spelling (alloca (spelling_length () + 1));
4437 buffer
= (char *) alloca (strlen (local
) + strlen (ofwhat
) + 2);
4440 sprintf (buffer
, local
, ofwhat
);
4444 error (format
, buffer
);
4447 /* Issue a pedantic warning for a bad initializer component.
4448 FORMAT describes the message. OFWHAT is the name for the component.
4449 LOCAL is a format string for formatting the insertion of the name
4452 If OFWHAT is null, the component name is stored on the spelling stack.
4453 If the component name is a null string, then LOCAL is omitted entirely. */
4456 pedwarn_init (format
, local
, ofwhat
)
4457 char *format
, *local
, *ofwhat
;
4462 ofwhat
= print_spelling (alloca (spelling_length () + 1));
4463 buffer
= (char *) alloca (strlen (local
) + strlen (ofwhat
) + 2);
4466 sprintf (buffer
, local
, ofwhat
);
4470 pedwarn (format
, buffer
);
4473 /* Digest the parser output INIT as an initializer for type TYPE.
4474 Return a C expression of type TYPE to represent the initial value.
4476 If TAIL is nonzero, it points to a variable holding a list of elements
4477 of which INIT is the first. We update the list stored there by
4478 removing from the head all the elements that we use.
4479 Normally this is only one; we use more than one element only if
4480 TYPE is an aggregate and INIT is not a constructor.
4482 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4483 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4484 applies only to elements of constructors.
4486 If OFWHAT is nonnull, it specifies what we are initializing, for error
4487 messages. Examples: variable name, variable.member, array[44].
4488 If OFWHAT is null, the component name is stored on the spelling stack. */
4491 digest_init (type
, init
, tail
, require_constant
, constructor_constant
, ofwhat
)
4492 tree type
, init
, *tail
;
4493 int require_constant
, constructor_constant
;
4496 enum tree_code code
= TREE_CODE (type
);
4498 tree old_tail_contents
;
4499 /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
4500 tree node which has no TREE_TYPE. */
4502 = TREE_CODE (init
) == CONSTRUCTOR
&& TREE_TYPE (init
) == 0;
4503 tree inside_init
= init
;
4505 /* By default, assume we use one element from a list.
4506 We correct this later in the sole case where it is not true. */
4510 old_tail_contents
= *tail
;
4511 *tail
= TREE_CHAIN (*tail
);
4514 if (init
== error_mark_node
)
4517 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4518 /* Do not use STRIP_NOPS here. We do not want an enumerator
4519 whose value is 0 to count as a null pointer constant. */
4520 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
4521 inside_init
= TREE_OPERAND (init
, 0);
4523 if (inside_init
&& raw_constructor
4524 && CONSTRUCTOR_ELTS (inside_init
) != 0
4525 && TREE_CHAIN (CONSTRUCTOR_ELTS (inside_init
)) == 0)
4527 element
= TREE_VALUE (CONSTRUCTOR_ELTS (inside_init
));
4528 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4529 if (element
&& TREE_CODE (element
) == NON_LVALUE_EXPR
)
4530 element
= TREE_OPERAND (element
, 0);
4533 /* Initialization of an array of chars from a string constant
4534 optionally enclosed in braces. */
4536 if (code
== ARRAY_TYPE
)
4538 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
4539 if ((typ1
== char_type_node
4540 || typ1
== signed_char_type_node
4541 || typ1
== unsigned_char_type_node
4542 || typ1
== unsigned_wchar_type_node
4543 || typ1
== signed_wchar_type_node
)
4544 && ((inside_init
&& TREE_CODE (inside_init
) == STRING_CST
)
4545 || (element
&& TREE_CODE (element
) == STRING_CST
)))
4547 tree string
= element
? element
: inside_init
;
4549 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string
)))
4551 && TYPE_PRECISION (typ1
) == TYPE_PRECISION (char_type_node
))
4553 error_init ("char-array%s initialized from wide string",
4555 return error_mark_node
;
4557 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string
)))
4559 && TYPE_PRECISION (typ1
) != TYPE_PRECISION (char_type_node
))
4561 error_init ("int-array%s initialized from non-wide string",
4563 return error_mark_node
;
4566 TREE_TYPE (string
) = type
;
4567 if (TYPE_DOMAIN (type
) != 0
4568 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
4570 register int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
4571 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
4572 /* Subtract 1 because it's ok to ignore the terminating null char
4573 that is counted in the length of the constant. */
4574 if (size
< TREE_STRING_LENGTH (string
) - 1)
4576 "initializer-string for array of chars%s is too long",
4583 /* Any type except an array can be initialized
4584 from an expression of the same type, optionally with braces.
4585 For an array, this is allowed only for a string constant. */
4587 if (inside_init
&& (TREE_TYPE (inside_init
) == type
4588 || (code
== ARRAY_TYPE
&& TREE_TYPE (inside_init
)
4589 && comptypes (TREE_TYPE (inside_init
), type
))
4590 || (code
== POINTER_TYPE
4591 && TREE_TYPE (inside_init
) != 0
4592 && (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
4593 || TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
)
4594 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
4595 TREE_TYPE (type
)))))
4597 if (code
== POINTER_TYPE
4598 && (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
4599 || TREE_CODE (TREE_TYPE (inside_init
)) == FUNCTION_TYPE
))
4600 inside_init
= default_conversion (inside_init
);
4601 else if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
)
4603 error_init ("array%s initialized from non-constant array expression",
4605 return error_mark_node
;
4608 if (optimize
&& TREE_READONLY (inside_init
)
4609 && TREE_CODE (inside_init
) == VAR_DECL
)
4610 inside_init
= decl_constant_value (inside_init
);
4612 if (require_constant
&& ! TREE_CONSTANT (inside_init
))
4614 error_init ("initializer element%s is not constant",
4615 " for `%s'", ofwhat
);
4616 inside_init
= error_mark_node
;
4618 else if (require_constant
&& initializer_constant_valid_p (inside_init
) == 0)
4620 error_init ("initializer element%s is not computable at load time",
4621 " for `%s'", ofwhat
);
4622 inside_init
= error_mark_node
;
4628 if (element
&& (TREE_TYPE (element
) == type
4629 || (code
== ARRAY_TYPE
&& TREE_TYPE (element
)
4630 && comptypes (TREE_TYPE (element
), type
))))
4632 if (code
== ARRAY_TYPE
)
4634 error_init ("array%s initialized from non-constant array expression",
4636 return error_mark_node
;
4638 if (pedantic
&& (code
== RECORD_TYPE
|| code
== UNION_TYPE
))
4639 pedwarn ("single-expression nonscalar initializer has braces");
4640 if (optimize
&& TREE_READONLY (element
) && TREE_CODE (element
) == VAR_DECL
)
4641 element
= decl_constant_value (element
);
4643 if (require_constant
&& ! TREE_CONSTANT (element
))
4645 error_init ("initializer element%s is not constant",
4646 " for `%s'", ofwhat
);
4647 element
= error_mark_node
;
4649 else if (require_constant
&& initializer_constant_valid_p (element
) == 0)
4651 error_init ("initializer element%s is not computable at load time",
4652 " for `%s'", ofwhat
);
4653 element
= error_mark_node
;
4659 /* Check for initializing a union by its first field.
4660 Such an initializer must use braces. */
4662 if (code
== UNION_TYPE
)
4665 tree field
= TYPE_FIELDS (type
);
4667 /* Find the first named field. ANSI decided in September 1990
4668 that only named fields count here. */
4669 while (field
&& DECL_NAME (field
) == 0)
4670 field
= TREE_CHAIN (field
);
4674 error_init ("union%s with no named members cannot be initialized",
4676 return error_mark_node
;
4679 if (raw_constructor
)
4680 result
= process_init_constructor (type
, inside_init
, NULL_PTR
,
4682 constructor_constant
, ofwhat
);
4685 *tail
= old_tail_contents
;
4686 result
= process_init_constructor (type
, NULL_TREE
, tail
,
4688 constructor_constant
, ofwhat
);
4697 /* Handle scalar types, including conversions. */
4699 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== POINTER_TYPE
4700 || code
== ENUMERAL_TYPE
)
4702 if (raw_constructor
)
4707 "initializer for scalar%s requires one element",
4709 return error_mark_node
;
4713 /* Deal with extra levels of {...}. */
4714 if (TREE_CODE (element
) == CONSTRUCTOR
4715 && TREE_TYPE (element
) == 0)
4718 "initializer for scalar%s requires one element",
4720 return error_mark_node
;
4722 inside_init
= element
;
4726 #if 0 /* A non-raw constructor is an actual expression. */
4727 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
4729 error_init ("initializer for scalar%s has extra braces",
4731 return error_mark_node
;
4738 push_string (ofwhat
);
4740 = convert_for_assignment (type
,
4741 default_conversion (raw_constructor
4744 &initialization_message
, NULL_TREE
, 0);
4747 if (require_constant
&& ! TREE_CONSTANT (inside_init
))
4749 error_init ("initializer element%s is not constant",
4750 " for `%s'", ofwhat
);
4751 inside_init
= error_mark_node
;
4753 else if (require_constant
&& initializer_constant_valid_p (inside_init
) == 0)
4755 error_init ("initializer element%s is not computable at load time",
4756 " for `%s'", ofwhat
);
4757 inside_init
= error_mark_node
;
4763 /* Come here only for records and arrays. */
4765 if (TYPE_SIZE (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
4767 error_init ("variable-sized object%s may not be initialized",
4769 return error_mark_node
;
4772 if (code
== ARRAY_TYPE
|| code
== RECORD_TYPE
)
4774 if (raw_constructor
)
4775 return process_init_constructor (type
, inside_init
,
4776 NULL_PTR
, constructor_constant
,
4777 constructor_constant
, ofwhat
);
4780 *tail
= old_tail_contents
;
4781 return process_init_constructor (type
, NULL_TREE
, tail
,
4782 constructor_constant
,
4783 constructor_constant
, ofwhat
);
4785 else if (flag_traditional
)
4786 /* Traditionally one can say `char x[100] = 0;'. */
4787 return process_init_constructor (type
,
4788 build_nt (CONSTRUCTOR
, NULL_TREE
,
4789 tree_cons (NULL_TREE
,
4792 NULL_PTR
, constructor_constant
,
4793 constructor_constant
, ofwhat
);
4796 error_init ("invalid initializer%s", " for `%s'", ofwhat
);
4797 return error_mark_node
;
4800 /* Process a constructor for a variable of type TYPE.
4801 The constructor elements may be specified either with INIT or with ELTS,
4802 only one of which should be non-null.
4804 If INIT is specified, it is a CONSTRUCTOR node which is specifically
4805 and solely for initializing this datum.
4807 If ELTS is specified, it is the address of a variable containing
4808 a list of expressions. We take as many elements as we need
4809 from the head of the list and update the list.
4811 In the resulting constructor, TREE_CONSTANT is set if all elts are
4812 constant, and TREE_STATIC is set if, in addition, all elts are simple enough
4813 constants that the assembler and linker can compute them.
4815 The argument CONSTANT_VALUE says to print an error if either the
4816 value or any element is not a constant.
4818 The argument CONSTANT_ELEMENT says to print an error if an element
4819 of an aggregate is not constant. It does not apply to a value
4820 which is not a constructor.
4822 OFWHAT is a character string describing the object being initialized,
4823 for error messages. It might be "variable" or "variable.member"
4824 or "variable[17].member[5]". If OFWHAT is null, the description string
4825 is stored on the spelling stack. */
4828 process_init_constructor (type
, init
, elts
, constant_value
, constant_element
,
4830 tree type
, init
, *elts
;
4831 int constant_value
, constant_element
;
4835 /* List of the elements of the result constructor,
4836 in reverse order. */
4837 register tree members
= NULL
;
4839 int allconstant
= 1;
4842 int depth
= SPELLING_DEPTH ();
4845 push_string (ofwhat
);
4847 /* Make TAIL be the list of elements to use for the initialization,
4848 no matter how the data was given to us. */
4853 tail
= CONSTRUCTOR_ELTS (init
);
4855 /* Gobble as many elements as needed, and make a constructor or initial value
4856 for each element of this aggregate. Chain them together in result.
4857 If there are too few, use 0 for each scalar ultimate component. */
4859 if (TREE_CODE (type
) == ARRAY_TYPE
)
4861 tree min_index
, max_index
, current_index
, members_index
;
4865 /* If we have array bounds, set our bounds from that. Otherwise,
4866 we have a lower bound of zero and an unknown upper bound. Also
4867 set the type of the bounds; use "int" as default. */
4868 if (TYPE_DOMAIN (type
))
4870 min_index
= members_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (type
));
4871 max_index
= TYPE_MAX_VALUE (TYPE_DOMAIN (type
));
4872 bound_type
= TREE_TYPE (min_index
);
4876 min_index
= members_index
= integer_zero_node
;
4878 bound_type
= integer_type_node
;
4881 one
= convert (bound_type
, integer_one_node
);
4883 /* Don't leave the loop based on index if the next item has an explicit
4884 index value that will override it. */
4886 for (current_index
= min_index
; tail
!= 0;
4887 current_index
= fold (build (PLUS_EXPR
, bound_type
,
4888 current_index
, one
)))
4890 register tree next1
;
4892 /* If this element specifies an index,
4893 move to that index before storing it in the new list. */
4894 if (TREE_PURPOSE (tail
) != 0)
4897 tree index
= TREE_PURPOSE (tail
);
4899 if (index
&& TREE_CODE (index
) == NON_LVALUE_EXPR
)
4900 index
= TREE_OPERAND (index
, 0);
4902 if (TREE_CODE (index
) == IDENTIFIER_NODE
)
4903 error ("field name used as index in array initializer");
4904 else if (TREE_CODE (index
) != INTEGER_CST
)
4905 error ("non-constant array index in initializer");
4906 else if (tree_int_cst_lt (index
, min_index
)
4907 || (max_index
&& tree_int_cst_lt (max_index
, index
)))
4908 error ("array index out of range in initializer");
4910 current_index
= index
, win
= 1;
4913 TREE_VALUE (tail
) = error_mark_node
;
4916 if (max_index
&& tree_int_cst_lt (max_index
, current_index
))
4917 break; /* Stop if we've indeed run out of elements. */
4919 /* Now digest the value specified. */
4920 if (TREE_VALUE (tail
) != 0)
4924 /* Build the element of this array, with "[]" notation. For
4925 error messages, we assume that the index fits within a
4929 push_array_bounds (TREE_INT_CST_LOW (current_index
));
4930 next1
= digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
4931 TREE_VALUE (tail
), &tail1
,
4932 /* Both of these are the same because
4933 a value here is an elt overall. */
4934 constant_element
, constant_element
,
4938 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
4940 if (tail
== tail1
&& TYPE_DOMAIN (type
) == 0)
4943 "non-empty initializer for array%s of empty elements",
4945 /* Just ignore what we were supposed to use. */
4952 next1
= error_mark_node
;
4953 tail
= TREE_CHAIN (tail
);
4956 if (next1
== error_mark_node
)
4958 else if (!TREE_CONSTANT (next1
))
4960 else if (initializer_constant_valid_p (next1
) == 0)
4963 /* Now store NEXT1 in the list, I elements from the *end*.
4964 Make the list longer if necessary. */
4965 while (! tree_int_cst_lt (current_index
, members_index
))
4967 members
= tree_cons (NULL_TREE
, NULL_TREE
, members
);
4968 members_index
= fold (build (PLUS_EXPR
, bound_type
,
4969 members_index
, one
));
4977 for (idx
= fold (build (MINUS_EXPR
, bound_type
,
4978 members_index
, one
));
4979 tree_int_cst_lt (current_index
, idx
);
4980 idx
= fold (build (MINUS_EXPR
, bound_type
, idx
, one
)))
4981 temp
= TREE_CHAIN (temp
);
4982 TREE_VALUE (temp
) = next1
;
4986 if (TREE_CODE (type
) == RECORD_TYPE
)
4988 register tree field
;
4989 int members_length
= 0;
4992 /* Don't leave the loop based on field just yet; see if next item
4993 overrides the expected field first. */
4995 for (field
= TYPE_FIELDS (type
), i
= 0; tail
;
4996 field
= TREE_CHAIN (field
), i
++)
4998 register tree next1
;
5000 /* If this element specifies a field,
5001 move to that field before storing it in the new list. */
5002 if (TREE_PURPOSE (tail
) != 0)
5006 if (TREE_CODE (TREE_PURPOSE (tail
)) != IDENTIFIER_NODE
)
5007 error ("index value instead of field name in structure initializer");
5012 for (temp
= TYPE_FIELDS (type
), j
= 0;
5014 temp
= TREE_CHAIN (temp
), j
++)
5015 if (DECL_NAME (temp
) == TREE_PURPOSE (tail
))
5018 field
= temp
, i
= j
, win
= 1;
5020 error ("no field `%s' in structure being initialized",
5021 IDENTIFIER_POINTER (TREE_PURPOSE (tail
)));
5024 TREE_VALUE (tail
) = error_mark_node
;
5028 break; /* No more fields to init. */
5030 if (! DECL_NAME (field
))
5032 next1
= integer_zero_node
;
5034 else if (TREE_VALUE (tail
) != 0)
5038 /* Build the name of this member, with a "." for membership. */
5041 push_member_name (IDENTIFIER_POINTER (DECL_NAME (field
)));
5042 next1
= digest_init (TREE_TYPE (field
),
5043 TREE_VALUE (tail
), &tail1
,
5044 constant_element
, constant_element
,
5047 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
5053 next1
= error_mark_node
;
5054 tail
= TREE_CHAIN (tail
);
5057 if (next1
== error_mark_node
)
5059 else if (!TREE_CONSTANT (next1
))
5061 else if (initializer_constant_valid_p (next1
) == 0)
5064 /* Now store NEXT1 in the list, I elements from the *end*.
5065 Make the list longer if necessary. */
5066 while (i
>= members_length
)
5068 members
= tree_cons (NULL_TREE
, NULL_TREE
, members
);
5076 for (j
= members_length
- 1; j
> i
; j
--)
5077 temp
= TREE_CHAIN (temp
);
5078 TREE_VALUE (temp
) = next1
;
5079 TREE_PURPOSE (temp
) = field
;
5083 if (TREE_CODE (type
) == UNION_TYPE
)
5085 register tree field
= TYPE_FIELDS (type
);
5086 register tree next1
;
5088 /* Find the first named field. ANSI decided in September 1990
5089 that only named fields count here. */
5090 while (field
&& DECL_NAME (field
) == 0)
5091 field
= TREE_CHAIN (field
);
5093 /* For a union, get the initializer for 1 fld. */
5095 /* If this element specifies a field, initialize via that field. */
5096 if (TREE_PURPOSE (tail
) != 0)
5100 if (TREE_CODE (TREE_PURPOSE (tail
)) == FIELD_DECL
)
5101 /* Handle the case of a call by build_c_cast. */
5102 field
= TREE_PURPOSE (tail
), win
= 1;
5103 else if (TREE_CODE (TREE_PURPOSE (tail
)) != IDENTIFIER_NODE
)
5104 error ("index value instead of field name in union initializer");
5108 for (temp
= TYPE_FIELDS (type
);
5110 temp
= TREE_CHAIN (temp
))
5111 if (DECL_NAME (temp
) == TREE_PURPOSE (tail
))
5114 field
= temp
, win
= 1;
5116 error ("no field `%s' in union being initialized",
5117 IDENTIFIER_POINTER (TREE_PURPOSE (tail
)));
5120 TREE_VALUE (tail
) = error_mark_node
;
5123 if (TREE_VALUE (tail
) != 0)
5127 /* Build the name of this member, with a "." for membership. */
5130 push_member_name (IDENTIFIER_POINTER (DECL_NAME (field
)));
5131 next1
= digest_init (TREE_TYPE (field
),
5132 TREE_VALUE (tail
), &tail1
,
5133 constant_value
, constant_element
, NULL_PTR
);
5135 if (tail1
!= 0 && TREE_CODE (tail1
) != TREE_LIST
)
5141 next1
= error_mark_node
;
5142 tail
= TREE_CHAIN (tail
);
5145 if (next1
== error_mark_node
)
5147 else if (!TREE_CONSTANT (next1
))
5149 else if (initializer_constant_valid_p (next1
) == 0)
5151 members
= tree_cons (field
, next1
, members
);
5154 /* If arguments were specified as a list, just remove the ones we used. */
5157 /* If arguments were specified as a constructor,
5158 complain unless we used all the elements of the constructor. */
5161 if (TREE_CODE (type
) == UNION_TYPE
)
5163 pedwarn_init ("excess elements in union initializer%s",
5164 " after `%s'", NULL_PTR
);
5168 pedwarn_init ("excess elements in aggregate initializer%s",
5169 " after `%s'", NULL_PTR
);
5173 /* It might be possible to use SAVE_SPELLING_DEPTH, but I suspect that
5174 some preprocessor somewhere won't accept that much text as an argument.
5175 It's also likely to make debugging difficult. */
5177 RESTORE_SPELLING_DEPTH (depth
);
5180 return error_mark_node
;
5182 result
= build (CONSTRUCTOR
, type
, NULL_TREE
, nreverse (members
));
5183 if (allconstant
) TREE_CONSTANT (result
) = 1;
5184 if (allconstant
&& allsimple
) TREE_STATIC (result
) = 1;
5188 /* Expand an ASM statement with operands, handling output operands
5189 that are not variables or INDIRECT_REFS by transforming such
5190 cases into cases that expand_asm_operands can handle.
5192 Arguments are same as for expand_asm_operands. */
5195 c_expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
)
5196 tree string
, outputs
, inputs
, clobbers
;
5201 int noutputs
= list_length (outputs
);
5203 /* o[I] is the place that output number I should be written. */
5204 register tree
*o
= (tree
*) alloca (noutputs
* sizeof (tree
));
5207 if (TREE_CODE (string
) == ADDR_EXPR
)
5208 string
= TREE_OPERAND (string
, 0);
5209 if (TREE_CODE (string
) != STRING_CST
)
5211 error ("asm template is not a string constant");
5215 /* Record the contents of OUTPUTS before it is modified. */
5216 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
5217 o
[i
] = TREE_VALUE (tail
);
5219 /* Perform default conversions on array and function inputs. */
5220 /* Don't do this for other types--
5221 it would screw up operands expected to be in memory. */
5222 for (i
= 0, tail
= inputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
5223 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail
))) == ARRAY_TYPE
5224 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail
))) == FUNCTION_TYPE
)
5225 TREE_VALUE (tail
) = default_conversion (TREE_VALUE (tail
));
5227 /* Generate the ASM_OPERANDS insn;
5228 store into the TREE_VALUEs of OUTPUTS some trees for
5229 where the values were actually stored. */
5230 expand_asm_operands (string
, outputs
, inputs
, clobbers
, vol
, filename
, line
);
5232 /* Copy all the intermediate outputs into the specified outputs. */
5233 for (i
= 0, tail
= outputs
; tail
; tail
= TREE_CHAIN (tail
), i
++)
5235 if (o
[i
] != TREE_VALUE (tail
))
5237 expand_expr (build_modify_expr (o
[i
], NOP_EXPR
, TREE_VALUE (tail
)),
5241 /* Detect modification of read-only values.
5242 (Otherwise done by build_modify_expr.) */
5245 tree type
= TREE_TYPE (o
[i
]);
5246 if (TYPE_READONLY (type
)
5247 || ((TREE_CODE (type
) == RECORD_TYPE
5248 || TREE_CODE (type
) == UNION_TYPE
)
5249 && C_TYPE_FIELDS_READONLY (type
)))
5250 readonly_warning (o
[i
], "modification by `asm'");
5254 /* Those MODIFY_EXPRs could do autoincrements. */
5258 /* Expand a C `return' statement.
5259 RETVAL is the expression for what to return,
5260 or a null pointer for `return;' with no value. */
5263 c_expand_return (retval
)
5266 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
5268 if (TREE_THIS_VOLATILE (current_function_decl
))
5269 warning ("function declared `volatile' has a `return' statement");
5273 current_function_returns_null
= 1;
5274 if (warn_return_type
&& valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
5275 warning ("`return' with no value, in function returning non-void");
5276 expand_null_return ();
5278 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
5280 current_function_returns_null
= 1;
5281 if (pedantic
|| TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
5282 pedwarn ("`return' with a value, in function returning void");
5283 expand_return (retval
);
5287 tree t
= convert_for_assignment (valtype
, retval
, "return",
5289 tree res
= DECL_RESULT (current_function_decl
);
5290 t
= build (MODIFY_EXPR
, TREE_TYPE (res
),
5291 res
, convert (TREE_TYPE (res
), t
));
5293 current_function_returns_value
= 1;
5297 /* Start a C switch statement, testing expression EXP.
5298 Return EXP if it is valid, an error node otherwise. */
5301 c_expand_start_case (exp
)
5304 register enum tree_code code
= TREE_CODE (TREE_TYPE (exp
));
5305 tree type
= TREE_TYPE (exp
);
5307 if (code
!= INTEGER_TYPE
&& code
!= ENUMERAL_TYPE
&& code
!= ERROR_MARK
)
5309 error ("switch quantity not an integer");
5310 exp
= error_mark_node
;
5316 if (warn_traditional
5317 && (TREE_TYPE (exp
) == long_integer_type_node
5318 || TREE_TYPE (exp
) == long_unsigned_type_node
))
5319 pedwarn ("`long' switch expression not converted to `int' in ANSI C");
5321 exp
= default_conversion (exp
);
5322 type
= TREE_TYPE (exp
);
5323 index
= get_unwidened (exp
, NULL_TREE
);
5324 /* We can't strip a conversion from a signed type to an unsigned,
5325 because if we did, int_fits_type_p would do the wrong thing
5326 when checking case values for being in range,
5327 and it's too hard to do the right thing. */
5328 if (TREE_UNSIGNED (TREE_TYPE (exp
))
5329 == TREE_UNSIGNED (TREE_TYPE (index
)))
5333 expand_start_case (1, exp
, type
, "switch statement");