1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
46 /* Nonzero if we've already printed a "missing braces around initializer"
47 message within this initializer. */
48 static int missing_braces_mentioned;
50 /* 1 if we explained undeclared var errors. */
51 static int undeclared_variable_notice;
53 static tree qualify_type PARAMS ((tree, tree));
54 static int comp_target_types PARAMS ((tree, tree));
55 static int function_types_compatible_p PARAMS ((tree, tree));
56 static int type_lists_compatible_p PARAMS ((tree, tree));
57 static tree decl_constant_value_for_broken_optimization PARAMS ((tree));
58 static tree lookup_field PARAMS ((tree, tree));
59 static tree convert_arguments PARAMS ((tree, tree, tree, tree));
60 static tree pointer_int_sum PARAMS ((enum tree_code, tree, tree));
61 static tree pointer_diff PARAMS ((tree, tree));
62 static tree unary_complex_lvalue PARAMS ((enum tree_code, tree));
63 static void pedantic_lvalue_warning PARAMS ((enum tree_code));
64 static tree internal_build_compound_expr PARAMS ((tree, int));
65 static tree convert_for_assignment PARAMS ((tree, tree, const char *,
67 static void warn_for_assignment PARAMS ((const char *, const char *,
69 static tree valid_compound_expr_initializer PARAMS ((tree, tree));
70 static void push_string PARAMS ((const char *));
71 static void push_member_name PARAMS ((tree));
72 static void push_array_bounds PARAMS ((int));
73 static int spelling_length PARAMS ((void));
74 static char *print_spelling PARAMS ((char *));
75 static void warning_init PARAMS ((const char *));
76 static tree digest_init PARAMS ((tree, tree, int, int));
77 static void output_init_element PARAMS ((tree, tree, tree, int));
78 static void output_pending_init_elements PARAMS ((int));
79 static int set_designator PARAMS ((int));
80 static void push_range_stack PARAMS ((tree));
81 static void add_pending_init PARAMS ((tree, tree));
82 static void set_nonincremental_init PARAMS ((void));
83 static void set_nonincremental_init_from_string PARAMS ((tree));
84 static tree find_init_member PARAMS ((tree));
86 /* Do `exp = require_complete_type (exp);' to make sure exp
87 does not have an incomplete type. (That includes void types.) */
90 require_complete_type (value)
93 tree type = TREE_TYPE (value);
95 if (TREE_CODE (value) == ERROR_MARK)
96 return error_mark_node;
98 /* First, detect a valid value with a complete type. */
99 if (COMPLETE_TYPE_P (type))
102 incomplete_type_error (value, type);
103 return error_mark_node;
106 /* Print an error message for invalid use of an incomplete type.
107 VALUE is the expression that was used (or 0 if that isn't known)
108 and TYPE is the type that was invalid. */
111 incomplete_type_error (value, type)
115 const char *type_code_string;
117 /* Avoid duplicate error message. */
118 if (TREE_CODE (type) == ERROR_MARK)
121 if (value != 0 && (TREE_CODE (value) == VAR_DECL
122 || TREE_CODE (value) == PARM_DECL))
123 error ("`%s' has an incomplete type",
124 IDENTIFIER_POINTER (DECL_NAME (value)));
128 /* We must print an error message. Be clever about what it says. */
130 switch (TREE_CODE (type))
133 type_code_string = "struct";
137 type_code_string = "union";
141 type_code_string = "enum";
145 error ("invalid use of void expression");
149 if (TYPE_DOMAIN (type))
151 type = TREE_TYPE (type);
154 error ("invalid use of array with unspecified bounds");
161 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
162 error ("invalid use of undefined type `%s %s'",
163 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
165 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
166 error ("invalid use of incomplete typedef `%s'",
167 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
171 /* Return a variant of TYPE which has all the type qualifiers of LIKE
172 as well as those of TYPE. */
175 qualify_type (type, like)
178 return c_build_qualified_type (type,
179 TYPE_QUALS (type) | TYPE_QUALS (like));
182 /* Return the common type of two types.
183 We assume that comptypes has already been done and returned 1;
184 if that isn't so, this may crash. In particular, we assume that qualifiers
187 This is the type for the result of most arithmetic operations
188 if the operands have the given two types. */
194 enum tree_code code1;
195 enum tree_code code2;
198 /* Save time if the two types are the same. */
200 if (t1 == t2) return t1;
202 /* If one type is nonsense, use the other. */
203 if (t1 == error_mark_node)
205 if (t2 == error_mark_node)
208 /* Merge the attributes. */
209 attributes = (*targetm.merge_type_attributes) (t1, t2);
211 /* Treat an enum type as the unsigned integer type of the same width. */
213 if (TREE_CODE (t1) == ENUMERAL_TYPE)
214 t1 = type_for_size (TYPE_PRECISION (t1), 1);
215 if (TREE_CODE (t2) == ENUMERAL_TYPE)
216 t2 = type_for_size (TYPE_PRECISION (t2), 1);
218 code1 = TREE_CODE (t1);
219 code2 = TREE_CODE (t2);
221 /* If one type is complex, form the common type of the non-complex
222 components, then make that complex. Use T1 or T2 if it is the
224 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
226 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
227 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
228 tree subtype = common_type (subtype1, subtype2);
230 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
231 return build_type_attribute_variant (t1, attributes);
232 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
233 return build_type_attribute_variant (t2, attributes);
235 return build_type_attribute_variant (build_complex_type (subtype),
243 /* If only one is real, use it as the result. */
245 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
246 return build_type_attribute_variant (t1, attributes);
248 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
249 return build_type_attribute_variant (t2, attributes);
251 /* Both real or both integers; use the one with greater precision. */
253 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
254 return build_type_attribute_variant (t1, attributes);
255 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
256 return build_type_attribute_variant (t2, attributes);
258 /* Same precision. Prefer longs to ints even when same size. */
260 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
261 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
262 return build_type_attribute_variant (long_unsigned_type_node,
265 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
266 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
268 /* But preserve unsignedness from the other type,
269 since long cannot hold all the values of an unsigned int. */
270 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
271 t1 = long_unsigned_type_node;
273 t1 = long_integer_type_node;
274 return build_type_attribute_variant (t1, attributes);
277 /* Likewise, prefer long double to double even if same size. */
278 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
279 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
280 return build_type_attribute_variant (long_double_type_node,
283 /* Otherwise prefer the unsigned one. */
285 if (TREE_UNSIGNED (t1))
286 return build_type_attribute_variant (t1, attributes);
288 return build_type_attribute_variant (t2, attributes);
291 /* For two pointers, do this recursively on the target type,
292 and combine the qualifiers of the two types' targets. */
293 /* This code was turned off; I don't know why.
294 But ANSI C specifies doing this with the qualifiers.
295 So I turned it on again. */
297 tree pointed_to_1 = TREE_TYPE (t1);
298 tree pointed_to_2 = TREE_TYPE (t2);
299 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
300 TYPE_MAIN_VARIANT (pointed_to_2));
301 t1 = build_pointer_type (c_build_qualified_type
303 TYPE_QUALS (pointed_to_1) |
304 TYPE_QUALS (pointed_to_2)));
305 return build_type_attribute_variant (t1, attributes);
308 t1 = build_pointer_type (common_type (TREE_TYPE (t1), TREE_TYPE (t2)));
309 return build_type_attribute_variant (t1, attributes);
314 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
315 /* Save space: see if the result is identical to one of the args. */
316 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
317 return build_type_attribute_variant (t1, attributes);
318 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
319 return build_type_attribute_variant (t2, attributes);
320 /* Merge the element types, and have a size if either arg has one. */
321 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
322 return build_type_attribute_variant (t1, attributes);
326 /* Function types: prefer the one that specified arg types.
327 If both do, merge the arg types. Also merge the return types. */
329 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
330 tree p1 = TYPE_ARG_TYPES (t1);
331 tree p2 = TYPE_ARG_TYPES (t2);
336 /* Save space: see if the result is identical to one of the args. */
337 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
338 return build_type_attribute_variant (t1, attributes);
339 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
340 return build_type_attribute_variant (t2, attributes);
342 /* Simple way if one arg fails to specify argument types. */
343 if (TYPE_ARG_TYPES (t1) == 0)
345 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
346 return build_type_attribute_variant (t1, attributes);
348 if (TYPE_ARG_TYPES (t2) == 0)
350 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
351 return build_type_attribute_variant (t1, attributes);
354 /* If both args specify argument types, we must merge the two
355 lists, argument by argument. */
358 declare_parm_level (1);
360 len = list_length (p1);
363 for (i = 0; i < len; i++)
364 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
369 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
371 /* A null type means arg type is not specified.
372 Take whatever the other function type has. */
373 if (TREE_VALUE (p1) == 0)
375 TREE_VALUE (n) = TREE_VALUE (p2);
378 if (TREE_VALUE (p2) == 0)
380 TREE_VALUE (n) = TREE_VALUE (p1);
384 /* Given wait (union {union wait *u; int *i} *)
385 and wait (union wait *),
386 prefer union wait * as type of parm. */
387 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
388 && TREE_VALUE (p1) != TREE_VALUE (p2))
391 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
392 memb; memb = TREE_CHAIN (memb))
393 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2)))
395 TREE_VALUE (n) = TREE_VALUE (p2);
397 pedwarn ("function types not truly compatible in ISO C");
401 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
402 && TREE_VALUE (p2) != TREE_VALUE (p1))
405 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
406 memb; memb = TREE_CHAIN (memb))
407 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1)))
409 TREE_VALUE (n) = TREE_VALUE (p1);
411 pedwarn ("function types not truly compatible in ISO C");
415 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
421 t1 = build_function_type (valtype, newargs);
422 /* ... falls through ... */
426 return build_type_attribute_variant (t1, attributes);
431 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
432 or various other operations. Return 2 if they are compatible
433 but a warning may be needed if you use them together. */
436 comptypes (type1, type2)
443 /* Suppress errors caused by previously reported errors. */
445 if (t1 == t2 || !t1 || !t2
446 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
449 /* If either type is the internal version of sizetype, return the
451 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
452 && TYPE_DOMAIN (t1) != 0)
453 t1 = TYPE_DOMAIN (t1);
455 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
456 && TYPE_DOMAIN (t2) != 0)
457 t2 = TYPE_DOMAIN (t2);
459 /* Treat an enum type as the integer type of the same width and
462 if (TREE_CODE (t1) == ENUMERAL_TYPE)
463 t1 = type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
464 if (TREE_CODE (t2) == ENUMERAL_TYPE)
465 t2 = type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
470 /* Different classes of types can't be compatible. */
472 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
474 /* Qualifiers must match. */
476 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
479 /* Allow for two different type nodes which have essentially the same
480 definition. Note that we already checked for equality of the type
481 qualifiers (just above). */
483 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
486 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
487 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
490 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
493 switch (TREE_CODE (t1))
496 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
497 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
501 val = function_types_compatible_p (t1, t2);
506 tree d1 = TYPE_DOMAIN (t1);
507 tree d2 = TYPE_DOMAIN (t2);
508 bool d1_variable, d2_variable;
509 bool d1_zero, d2_zero;
512 /* Target types must match incl. qualifiers. */
513 if (TREE_TYPE (t1) != TREE_TYPE (t2)
514 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
517 /* Sizes must match unless one is missing or variable. */
518 if (d1 == 0 || d2 == 0 || d1 == d2)
521 d1_zero = ! TYPE_MAX_VALUE (d1);
522 d2_zero = ! TYPE_MAX_VALUE (d2);
524 d1_variable = (! d1_zero
525 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
526 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
527 d2_variable = (! d2_zero
528 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
529 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
531 if (d1_variable || d2_variable)
533 if (d1_zero && d2_zero)
535 if (d1_zero || d2_zero
536 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
537 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
544 if (maybe_objc_comptypes (t1, t2, 0) == 1)
551 return attrval == 2 && val == 1 ? 2 : val;
554 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
555 ignoring their qualifiers. */
558 comp_target_types (ttl, ttr)
563 /* Give maybe_objc_comptypes a crack at letting these types through. */
564 if ((val = maybe_objc_comptypes (ttl, ttr, 1)) >= 0)
567 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
568 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
570 if (val == 2 && pedantic)
571 pedwarn ("types are not quite compatible");
575 /* Subroutines of `comptypes'. */
577 /* Return 1 if two function types F1 and F2 are compatible.
578 If either type specifies no argument types,
579 the other must specify a fixed number of self-promoting arg types.
580 Otherwise, if one type specifies only the number of arguments,
581 the other must specify that number of self-promoting arg types.
582 Otherwise, the argument types must match. */
585 function_types_compatible_p (f1, f2)
589 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
593 if (!(TREE_TYPE (f1) == TREE_TYPE (f2)
594 || (val = comptypes (TREE_TYPE (f1), TREE_TYPE (f2)))))
597 args1 = TYPE_ARG_TYPES (f1);
598 args2 = TYPE_ARG_TYPES (f2);
600 /* An unspecified parmlist matches any specified parmlist
601 whose argument types don't need default promotions. */
605 if (!self_promoting_args_p (args2))
607 /* If one of these types comes from a non-prototype fn definition,
608 compare that with the other type's arglist.
609 If they don't match, ask for a warning (but no error). */
610 if (TYPE_ACTUAL_ARG_TYPES (f1)
611 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
617 if (!self_promoting_args_p (args1))
619 if (TYPE_ACTUAL_ARG_TYPES (f2)
620 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
625 /* Both types have argument lists: compare them and propagate results. */
626 val1 = type_lists_compatible_p (args1, args2);
627 return val1 != 1 ? val1 : val;
630 /* Check two lists of types for compatibility,
631 returning 0 for incompatible, 1 for compatible,
632 or 2 for compatible with warning. */
635 type_lists_compatible_p (args1, args2)
638 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
644 if (args1 == 0 && args2 == 0)
646 /* If one list is shorter than the other,
647 they fail to match. */
648 if (args1 == 0 || args2 == 0)
650 /* A null pointer instead of a type
651 means there is supposed to be an argument
652 but nothing is specified about what type it has.
653 So match anything that self-promotes. */
654 if (TREE_VALUE (args1) == 0)
656 if (simple_type_promotes_to (TREE_VALUE (args2)) != NULL_TREE)
659 else if (TREE_VALUE (args2) == 0)
661 if (simple_type_promotes_to (TREE_VALUE (args1)) != NULL_TREE)
664 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
665 TYPE_MAIN_VARIANT (TREE_VALUE (args2)))))
667 /* Allow wait (union {union wait *u; int *i} *)
668 and wait (union wait *) to be compatible. */
669 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
670 && (TYPE_NAME (TREE_VALUE (args1)) == 0
671 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
672 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
673 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
674 TYPE_SIZE (TREE_VALUE (args2))))
677 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
678 memb; memb = TREE_CHAIN (memb))
679 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2)))
684 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
685 && (TYPE_NAME (TREE_VALUE (args2)) == 0
686 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
687 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
688 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
689 TYPE_SIZE (TREE_VALUE (args1))))
692 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
693 memb; memb = TREE_CHAIN (memb))
694 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1)))
703 /* comptypes said ok, but record if it said to warn. */
707 args1 = TREE_CHAIN (args1);
708 args2 = TREE_CHAIN (args2);
712 /* Compute the value of the `sizeof' operator. */
718 enum tree_code code = TREE_CODE (type);
721 if (code == FUNCTION_TYPE)
723 if (pedantic || warn_pointer_arith)
724 pedwarn ("sizeof applied to a function type");
725 size = size_one_node;
727 else if (code == VOID_TYPE)
729 if (pedantic || warn_pointer_arith)
730 pedwarn ("sizeof applied to a void type");
731 size = size_one_node;
733 else if (code == ERROR_MARK)
734 size = size_one_node;
735 else if (!COMPLETE_TYPE_P (type))
737 error ("sizeof applied to an incomplete type");
738 size = size_zero_node;
741 /* Convert in case a char is more than one unit. */
742 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
743 size_int (TYPE_PRECISION (char_type_node)
746 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
747 TYPE_IS_SIZETYPE means that certain things (like overflow) will
748 never happen. However, this node should really have type
749 `size_t', which is just a typedef for an ordinary integer type. */
750 return fold (build1 (NOP_EXPR, c_size_type_node, size));
754 c_sizeof_nowarn (type)
757 enum tree_code code = TREE_CODE (type);
760 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
761 size = size_one_node;
762 else if (!COMPLETE_TYPE_P (type))
763 size = size_zero_node;
765 /* Convert in case a char is more than one unit. */
766 size = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
767 size_int (TYPE_PRECISION (char_type_node)
770 /* SIZE will have an integer type with TYPE_IS_SIZETYPE set.
771 TYPE_IS_SIZETYPE means that certain things (like overflow) will
772 never happen. However, this node should really have type
773 `size_t', which is just a typedef for an ordinary integer type. */
774 return fold (build1 (NOP_EXPR, c_size_type_node, size));
777 /* Compute the size to increment a pointer by. */
780 c_size_in_bytes (type)
783 enum tree_code code = TREE_CODE (type);
785 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
786 return size_one_node;
788 if (!COMPLETE_OR_VOID_TYPE_P (type))
790 error ("arithmetic on pointer to an incomplete type");
791 return size_one_node;
794 /* Convert in case a char is more than one unit. */
795 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
796 size_int (TYPE_PRECISION (char_type_node)
800 /* Return either DECL or its known constant value (if it has one). */
803 decl_constant_value (decl)
806 if (/* Don't change a variable array bound or initial value to a constant
807 in a place where a variable is invalid. */
808 current_function_decl != 0
809 && ! TREE_THIS_VOLATILE (decl)
810 && TREE_READONLY (decl)
811 && DECL_INITIAL (decl) != 0
812 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
813 /* This is invalid if initial value is not constant.
814 If it has either a function call, a memory reference,
815 or a variable, then re-evaluating it could give different results. */
816 && TREE_CONSTANT (DECL_INITIAL (decl))
817 /* Check for cases where this is sub-optimal, even though valid. */
818 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
819 return DECL_INITIAL (decl);
823 /* Return either DECL or its known constant value (if it has one), but
824 return DECL if pedantic or DECL has mode BLKmode. This is for
825 bug-compatibility with the old behavior of decl_constant_value
826 (before GCC 3.0); every use of this function is a bug and it should
827 be removed before GCC 3.1. It is not appropriate to use pedantic
828 in a way that affects optimization, and BLKmode is probably not the
829 right test for avoiding misoptimizations either. */
832 decl_constant_value_for_broken_optimization (decl)
835 if (pedantic || DECL_MODE (decl) == BLKmode)
838 return decl_constant_value (decl);
841 /* Perform default promotions for C data used in expressions.
842 Arrays and functions are converted to pointers;
843 enumeral types or short or char, to int.
844 In addition, manifest constants symbols are replaced by their values. */
847 default_conversion (exp)
851 tree type = TREE_TYPE (exp);
852 enum tree_code code = TREE_CODE (type);
854 /* Constants can be used directly unless they're not loadable. */
855 if (TREE_CODE (exp) == CONST_DECL)
856 exp = DECL_INITIAL (exp);
858 /* Replace a nonvolatile const static variable with its value unless
859 it is an array, in which case we must be sure that taking the
860 address of the array produces consistent results. */
861 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
863 exp = decl_constant_value_for_broken_optimization (exp);
864 type = TREE_TYPE (exp);
867 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
870 Do not use STRIP_NOPS here! It will remove conversions from pointer
871 to integer and cause infinite recursion. */
873 while (TREE_CODE (exp) == NON_LVALUE_EXPR
874 || (TREE_CODE (exp) == NOP_EXPR
875 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
876 exp = TREE_OPERAND (exp, 0);
878 /* Preserve the original expression code. */
879 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
880 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
882 /* Normally convert enums to int,
883 but convert wide enums to something wider. */
884 if (code == ENUMERAL_TYPE)
886 type = type_for_size (MAX (TYPE_PRECISION (type),
887 TYPE_PRECISION (integer_type_node)),
889 || (TYPE_PRECISION (type)
890 >= TYPE_PRECISION (integer_type_node)))
891 && TREE_UNSIGNED (type)));
893 return convert (type, exp);
896 if (TREE_CODE (exp) == COMPONENT_REF
897 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
898 /* If it's thinner than an int, promote it like a
899 c_promoting_integer_type_p, otherwise leave it alone. */
900 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
901 TYPE_PRECISION (integer_type_node)))
902 return convert (flag_traditional && TREE_UNSIGNED (type)
903 ? unsigned_type_node : integer_type_node,
906 if (c_promoting_integer_type_p (type))
908 /* Traditionally, unsignedness is preserved in default promotions.
909 Also preserve unsignedness if not really getting any wider. */
910 if (TREE_UNSIGNED (type)
912 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
913 return convert (unsigned_type_node, exp);
915 return convert (integer_type_node, exp);
918 if (flag_traditional && !flag_allow_single_precision
919 && TYPE_MAIN_VARIANT (type) == float_type_node)
920 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);
939 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
941 constp = TREE_READONLY (exp);
942 volatilep = TREE_THIS_VOLATILE (exp);
945 if (TYPE_QUALS (type) || constp || volatilep)
947 = c_build_qualified_type (restype,
949 | (constp * TYPE_QUAL_CONST)
950 | (volatilep * TYPE_QUAL_VOLATILE));
952 if (TREE_CODE (exp) == INDIRECT_REF)
953 return convert (TYPE_POINTER_TO (restype),
954 TREE_OPERAND (exp, 0));
956 if (TREE_CODE (exp) == COMPOUND_EXPR)
958 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
959 return build (COMPOUND_EXPR, TREE_TYPE (op1),
960 TREE_OPERAND (exp, 0), op1);
964 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
966 error ("invalid use of non-lvalue array");
967 return error_mark_node;
970 ptrtype = build_pointer_type (restype);
972 if (TREE_CODE (exp) == VAR_DECL)
974 /* ??? This is not really quite correct
975 in that the type of the operand of ADDR_EXPR
976 is not the target type of the type of the ADDR_EXPR itself.
977 Question is, can this lossage be avoided? */
978 adr = build1 (ADDR_EXPR, ptrtype, exp);
979 if (mark_addressable (exp) == 0)
980 return error_mark_node;
981 TREE_CONSTANT (adr) = staticp (exp);
982 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
985 /* This way is better for a COMPONENT_REF since it can
986 simplify the offset for a component. */
987 adr = build_unary_op (ADDR_EXPR, exp, 1);
988 return convert (ptrtype, adr);
993 /* Look up COMPONENT in a structure or union DECL.
995 If the component name is not found, returns NULL_TREE. Otherwise,
996 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
997 stepping down the chain to the component, which is in the last
998 TREE_VALUE of the list. Normally the list is of length one, but if
999 the component is embedded within (nested) anonymous structures or
1000 unions, the list steps down the chain to the component. */
1003 lookup_field (decl, component)
1004 tree decl, component;
1006 tree type = TREE_TYPE (decl);
1009 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1010 to the field elements. Use a binary search on this array to quickly
1011 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1012 will always be set for structures which have many elements. */
1014 if (TYPE_LANG_SPECIFIC (type))
1017 tree *field_array = &TYPE_LANG_SPECIFIC (type)->elts[0];
1019 field = TYPE_FIELDS (type);
1021 top = TYPE_LANG_SPECIFIC (type)->len;
1022 while (top - bot > 1)
1024 half = (top - bot + 1) >> 1;
1025 field = field_array[bot+half];
1027 if (DECL_NAME (field) == NULL_TREE)
1029 /* Step through all anon unions in linear fashion. */
1030 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1032 field = field_array[bot++];
1033 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1034 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1036 tree anon = lookup_field (field, component);
1039 return tree_cons (NULL_TREE, field, anon);
1043 /* Entire record is only anon unions. */
1047 /* Restart the binary search, with new lower bound. */
1051 if (DECL_NAME (field) == component)
1053 if (DECL_NAME (field) < component)
1059 if (DECL_NAME (field_array[bot]) == component)
1060 field = field_array[bot];
1061 else if (DECL_NAME (field) != component)
1066 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1068 if (DECL_NAME (field) == NULL_TREE
1069 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1070 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1072 tree anon = lookup_field (field, component);
1075 return tree_cons (NULL_TREE, field, anon);
1078 if (DECL_NAME (field) == component)
1082 if (field == NULL_TREE)
1086 return tree_cons (NULL_TREE, field, NULL_TREE);
1089 /* Make an expression to refer to the COMPONENT field of
1090 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1093 build_component_ref (datum, component)
1094 tree datum, component;
1096 tree type = TREE_TYPE (datum);
1097 enum tree_code code = TREE_CODE (type);
1101 /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
1102 unless we are not to support things not strictly ANSI. */
1103 switch (TREE_CODE (datum))
1107 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1108 return build (COMPOUND_EXPR, TREE_TYPE (value),
1109 TREE_OPERAND (datum, 0), value);
1112 return build_conditional_expr
1113 (TREE_OPERAND (datum, 0),
1114 build_component_ref (TREE_OPERAND (datum, 1), component),
1115 build_component_ref (TREE_OPERAND (datum, 2), component));
1121 /* See if there is a field or component with name COMPONENT. */
1123 if (code == RECORD_TYPE || code == UNION_TYPE)
1125 if (!COMPLETE_TYPE_P (type))
1127 incomplete_type_error (NULL_TREE, type);
1128 return error_mark_node;
1131 field = lookup_field (datum, component);
1135 error ("%s has no member named `%s'",
1136 code == RECORD_TYPE ? "structure" : "union",
1137 IDENTIFIER_POINTER (component));
1138 return error_mark_node;
1141 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1142 This might be better solved in future the way the C++ front
1143 end does it - by giving the anonymous entities each a
1144 separate name and type, and then have build_component_ref
1145 recursively call itself. We can't do that here. */
1146 for (; field; field = TREE_CHAIN (field))
1148 tree subdatum = TREE_VALUE (field);
1150 if (TREE_TYPE (subdatum) == error_mark_node)
1151 return error_mark_node;
1153 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1154 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1155 TREE_READONLY (ref) = 1;
1156 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1157 TREE_THIS_VOLATILE (ref) = 1;
1163 else if (code != ERROR_MARK)
1164 error ("request for member `%s' in something not a structure or union",
1165 IDENTIFIER_POINTER (component));
1167 return error_mark_node;
1170 /* Given an expression PTR for a pointer, return an expression
1171 for the value pointed to.
1172 ERRORSTRING is the name of the operator to appear in error messages. */
1175 build_indirect_ref (ptr, errorstring)
1177 const char *errorstring;
1179 tree pointer = default_conversion (ptr);
1180 tree type = TREE_TYPE (pointer);
1182 if (TREE_CODE (type) == POINTER_TYPE)
1184 if (TREE_CODE (pointer) == ADDR_EXPR
1186 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1187 == TREE_TYPE (type)))
1188 return TREE_OPERAND (pointer, 0);
1191 tree t = TREE_TYPE (type);
1192 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1194 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1196 error ("dereferencing pointer to incomplete type");
1197 return error_mark_node;
1199 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1200 warning ("dereferencing `void *' pointer");
1202 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1203 so that we get the proper error message if the result is used
1204 to assign to. Also, &* is supposed to be a no-op.
1205 And ANSI C seems to specify that the type of the result
1206 should be the const type. */
1207 /* A de-reference of a pointer to const is not a const. It is valid
1208 to change it via some other pointer. */
1209 TREE_READONLY (ref) = TYPE_READONLY (t);
1210 TREE_SIDE_EFFECTS (ref)
1211 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer) || flag_volatile;
1212 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1216 else if (TREE_CODE (pointer) != ERROR_MARK)
1217 error ("invalid type argument of `%s'", errorstring);
1218 return error_mark_node;
1221 /* This handles expressions of the form "a[i]", which denotes
1224 This is logically equivalent in C to *(a+i), but we may do it differently.
1225 If A is a variable or a member, we generate a primitive ARRAY_REF.
1226 This avoids forcing the array out of registers, and can work on
1227 arrays that are not lvalues (for example, members of structures returned
1231 build_array_ref (array, index)
1236 error ("subscript missing in array reference");
1237 return error_mark_node;
1240 if (TREE_TYPE (array) == error_mark_node
1241 || TREE_TYPE (index) == error_mark_node)
1242 return error_mark_node;
1244 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1245 && TREE_CODE (array) != INDIRECT_REF)
1249 /* Subscripting with type char is likely to lose
1250 on a machine where chars are signed.
1251 So warn on any machine, but optionally.
1252 Don't warn for unsigned char since that type is safe.
1253 Don't warn for signed char because anyone who uses that
1254 must have done so deliberately. */
1255 if (warn_char_subscripts
1256 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1257 warning ("array subscript has type `char'");
1259 /* Apply default promotions *after* noticing character types. */
1260 index = default_conversion (index);
1262 /* Require integer *after* promotion, for sake of enums. */
1263 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1265 error ("array subscript is not an integer");
1266 return error_mark_node;
1269 /* An array that is indexed by a non-constant
1270 cannot be stored in a register; we must be able to do
1271 address arithmetic on its address.
1272 Likewise an array of elements of variable size. */
1273 if (TREE_CODE (index) != INTEGER_CST
1274 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1275 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1277 if (mark_addressable (array) == 0)
1278 return error_mark_node;
1280 /* An array that is indexed by a constant value which is not within
1281 the array bounds cannot be stored in a register either; because we
1282 would get a crash in store_bit_field/extract_bit_field when trying
1283 to access a non-existent part of the register. */
1284 if (TREE_CODE (index) == INTEGER_CST
1285 && TYPE_VALUES (TREE_TYPE (array))
1286 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1288 if (mark_addressable (array) == 0)
1289 return error_mark_node;
1295 while (TREE_CODE (foo) == COMPONENT_REF)
1296 foo = TREE_OPERAND (foo, 0);
1297 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1298 pedwarn ("ISO C forbids subscripting `register' array");
1299 else if (! flag_isoc99 && ! lvalue_p (foo))
1300 pedwarn ("ISO C89 forbids subscripting non-lvalue array");
1303 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1304 rval = build (ARRAY_REF, type, array, index);
1305 /* Array ref is const/volatile if the array elements are
1306 or if the array is. */
1307 TREE_READONLY (rval)
1308 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1309 | TREE_READONLY (array));
1310 TREE_SIDE_EFFECTS (rval)
1311 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1312 | TREE_SIDE_EFFECTS (array));
1313 TREE_THIS_VOLATILE (rval)
1314 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1315 /* This was added by rms on 16 Nov 91.
1316 It fixes vol struct foo *a; a->elts[1]
1317 in an inline function.
1318 Hope it doesn't break something else. */
1319 | TREE_THIS_VOLATILE (array));
1320 return require_complete_type (fold (rval));
1324 tree ar = default_conversion (array);
1325 tree ind = default_conversion (index);
1327 /* Do the same warning check as above, but only on the part that's
1328 syntactically the index and only if it is also semantically
1330 if (warn_char_subscripts
1331 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1332 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1333 warning ("subscript has type `char'");
1335 /* Put the integer in IND to simplify error checking. */
1336 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1343 if (ar == error_mark_node)
1346 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1347 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1349 error ("subscripted value is neither array nor pointer");
1350 return error_mark_node;
1352 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1354 error ("array subscript is not an integer");
1355 return error_mark_node;
1358 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1363 /* Build an external reference to identifier ID. FUN indicates
1364 whether this will be used for a function call. */
1366 build_external_ref (id, fun)
1371 tree decl = lookup_name (id);
1372 tree objc_ivar = lookup_objc_ivar (id);
1374 if (!decl || decl == error_mark_node || C_DECL_ANTICIPATED (decl))
1380 if (!decl || decl == error_mark_node)
1381 /* Ordinary implicit function declaration. */
1382 ref = implicitly_declare (id);
1385 /* Implicit declaration of built-in function. Don't
1386 change the built-in declaration, but don't let this
1387 go by silently, either. */
1388 implicit_decl_warning (id);
1390 /* only issue this warning once */
1391 C_DECL_ANTICIPATED (decl) = 0;
1397 /* Reference to undeclared variable, including reference to
1398 builtin outside of function-call context. */
1399 if (current_function_decl == 0)
1400 error ("`%s' undeclared here (not in a function)",
1401 IDENTIFIER_POINTER (id));
1404 if (IDENTIFIER_GLOBAL_VALUE (id) != error_mark_node
1405 || IDENTIFIER_ERROR_LOCUS (id) != current_function_decl)
1407 error ("`%s' undeclared (first use in this function)",
1408 IDENTIFIER_POINTER (id));
1410 if (! undeclared_variable_notice)
1412 error ("(Each undeclared identifier is reported only once");
1413 error ("for each function it appears in.)");
1414 undeclared_variable_notice = 1;
1417 IDENTIFIER_GLOBAL_VALUE (id) = error_mark_node;
1418 IDENTIFIER_ERROR_LOCUS (id) = current_function_decl;
1420 return error_mark_node;
1425 /* Properly declared variable or function reference. */
1428 else if (decl != objc_ivar && IDENTIFIER_LOCAL_VALUE (id))
1430 warning ("local declaration of `%s' hides instance variable",
1431 IDENTIFIER_POINTER (id));
1438 if (TREE_TYPE (ref) == error_mark_node)
1439 return error_mark_node;
1441 assemble_external (ref);
1442 TREE_USED (ref) = 1;
1444 if (TREE_CODE (ref) == CONST_DECL)
1446 ref = DECL_INITIAL (ref);
1447 TREE_CONSTANT (ref) = 1;
1453 /* Build a function call to function FUNCTION with parameters PARAMS.
1454 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1455 TREE_VALUE of each node is a parameter-expression.
1456 FUNCTION's data type may be a function type or a pointer-to-function. */
1459 build_function_call (function, params)
1460 tree function, params;
1462 tree fntype, fundecl = 0;
1463 tree coerced_params;
1464 tree name = NULL_TREE, assembler_name = NULL_TREE, result;
1466 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1467 STRIP_TYPE_NOPS (function);
1469 /* Convert anything with function type to a pointer-to-function. */
1470 if (TREE_CODE (function) == FUNCTION_DECL)
1472 name = DECL_NAME (function);
1473 assembler_name = DECL_ASSEMBLER_NAME (function);
1475 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1476 (because calling an inline function does not mean the function
1477 needs to be separately compiled). */
1478 fntype = build_type_variant (TREE_TYPE (function),
1479 TREE_READONLY (function),
1480 TREE_THIS_VOLATILE (function));
1482 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1485 function = default_conversion (function);
1487 fntype = TREE_TYPE (function);
1489 if (TREE_CODE (fntype) == ERROR_MARK)
1490 return error_mark_node;
1492 if (!(TREE_CODE (fntype) == POINTER_TYPE
1493 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1495 error ("called object is not a function");
1496 return error_mark_node;
1499 /* fntype now gets the type of function pointed to. */
1500 fntype = TREE_TYPE (fntype);
1502 /* Convert the parameters to the types declared in the
1503 function prototype, or apply default promotions. */
1506 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1508 /* Check for errors in format strings. */
1511 check_function_format (NULL, TYPE_ATTRIBUTES (fntype), coerced_params);
1513 /* Recognize certain built-in functions so we can make tree-codes
1514 other than CALL_EXPR. We do this when it enables fold-const.c
1515 to do something useful. */
1517 if (TREE_CODE (function) == ADDR_EXPR
1518 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1519 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1521 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1522 params, coerced_params);
1527 result = build (CALL_EXPR, TREE_TYPE (fntype),
1528 function, coerced_params, NULL_TREE);
1529 TREE_SIDE_EFFECTS (result) = 1;
1530 result = fold (result);
1532 if (VOID_TYPE_P (TREE_TYPE (result)))
1534 return require_complete_type (result);
1537 /* Convert the argument expressions in the list VALUES
1538 to the types in the list TYPELIST. The result is a list of converted
1539 argument expressions.
1541 If TYPELIST is exhausted, or when an element has NULL as its type,
1542 perform the default conversions.
1544 PARMLIST is the chain of parm decls for the function being called.
1545 It may be 0, if that info is not available.
1546 It is used only for generating error messages.
1548 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1550 This is also where warnings about wrong number of args are generated.
1552 Both VALUES and the returned value are chains of TREE_LIST nodes
1553 with the elements of the list in the TREE_VALUE slots of those nodes. */
1556 convert_arguments (typelist, values, name, fundecl)
1557 tree typelist, values, name, fundecl;
1559 tree typetail, valtail;
1563 /* Scan the given expressions and types, producing individual
1564 converted arguments and pushing them on RESULT in reverse order. */
1566 for (valtail = values, typetail = typelist, parmnum = 0;
1568 valtail = TREE_CHAIN (valtail), parmnum++)
1570 tree type = typetail ? TREE_VALUE (typetail) : 0;
1571 tree val = TREE_VALUE (valtail);
1573 if (type == void_type_node)
1576 error ("too many arguments to function `%s'",
1577 IDENTIFIER_POINTER (name));
1579 error ("too many arguments to function");
1583 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1584 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1585 to convert automatically to a pointer. */
1586 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1587 val = TREE_OPERAND (val, 0);
1589 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
1590 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
1591 val = default_conversion (val);
1593 val = require_complete_type (val);
1597 /* Formal parm type is specified by a function prototype. */
1600 if (!COMPLETE_TYPE_P (type))
1602 error ("type of formal parameter %d is incomplete", parmnum + 1);
1607 /* Optionally warn about conversions that
1608 differ from the default conversions. */
1609 if (warn_conversion || warn_traditional)
1611 int formal_prec = TYPE_PRECISION (type);
1613 if (INTEGRAL_TYPE_P (type)
1614 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1615 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1616 if (INTEGRAL_TYPE_P (type)
1617 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1618 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1619 else if (TREE_CODE (type) == COMPLEX_TYPE
1620 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1621 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1622 else if (TREE_CODE (type) == REAL_TYPE
1623 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1624 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1625 else if (TREE_CODE (type) == COMPLEX_TYPE
1626 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1627 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1628 else if (TREE_CODE (type) == REAL_TYPE
1629 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1630 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1631 /* ??? At some point, messages should be written about
1632 conversions between complex types, but that's too messy
1634 else if (TREE_CODE (type) == REAL_TYPE
1635 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1637 /* Warn if any argument is passed as `float',
1638 since without a prototype it would be `double'. */
1639 if (formal_prec == TYPE_PRECISION (float_type_node))
1640 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1642 /* Detect integer changing in width or signedness.
1643 These warnings are only activated with
1644 -Wconversion, not with -Wtraditional. */
1645 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1646 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1648 tree would_have_been = default_conversion (val);
1649 tree type1 = TREE_TYPE (would_have_been);
1651 if (TREE_CODE (type) == ENUMERAL_TYPE
1652 && (TYPE_MAIN_VARIANT (type)
1653 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1654 /* No warning if function asks for enum
1655 and the actual arg is that enum type. */
1657 else if (formal_prec != TYPE_PRECISION (type1))
1658 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1659 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1661 /* Don't complain if the formal parameter type
1662 is an enum, because we can't tell now whether
1663 the value was an enum--even the same enum. */
1664 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1666 else if (TREE_CODE (val) == INTEGER_CST
1667 && int_fits_type_p (val, type))
1668 /* Change in signedness doesn't matter
1669 if a constant value is unaffected. */
1671 /* Likewise for a constant in a NOP_EXPR. */
1672 else if (TREE_CODE (val) == NOP_EXPR
1673 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1674 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1676 #if 0 /* We never get such tree structure here. */
1677 else if (TREE_CODE (TREE_TYPE (val)) == ENUMERAL_TYPE
1678 && int_fits_type_p (TYPE_MIN_VALUE (TREE_TYPE (val)), type)
1679 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (val)), type))
1680 /* Change in signedness doesn't matter
1681 if an enum value is unaffected. */
1684 /* If the value is extended from a narrower
1685 unsigned type, it doesn't matter whether we
1686 pass it as signed or unsigned; the value
1687 certainly is the same either way. */
1688 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1689 && TREE_UNSIGNED (TREE_TYPE (val)))
1691 else if (TREE_UNSIGNED (type))
1692 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1694 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1698 parmval = convert_for_assignment (type, val,
1699 (char *) 0, /* arg passing */
1700 fundecl, name, parmnum + 1);
1702 if (PROMOTE_PROTOTYPES
1703 && INTEGRAL_TYPE_P (type)
1704 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1705 parmval = default_conversion (parmval);
1707 result = tree_cons (NULL_TREE, parmval, result);
1709 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1710 && (TYPE_PRECISION (TREE_TYPE (val))
1711 < TYPE_PRECISION (double_type_node)))
1712 /* Convert `float' to `double'. */
1713 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1715 /* Convert `short' and `char' to full-size `int'. */
1716 result = tree_cons (NULL_TREE, default_conversion (val), result);
1719 typetail = TREE_CHAIN (typetail);
1722 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1725 error ("too few arguments to function `%s'",
1726 IDENTIFIER_POINTER (name));
1728 error ("too few arguments to function");
1731 return nreverse (result);
1734 /* This is the entry point used by the parser
1735 for binary operators in the input.
1736 In addition to constructing the expression,
1737 we check for operands that were written with other binary operators
1738 in a way that is likely to confuse the user. */
1741 parser_build_binary_op (code, arg1, arg2)
1742 enum tree_code code;
1745 tree result = build_binary_op (code, arg1, arg2, 1);
1748 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1749 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1750 enum tree_code code1 = ERROR_MARK;
1751 enum tree_code code2 = ERROR_MARK;
1753 if (IS_EXPR_CODE_CLASS (class1))
1754 code1 = C_EXP_ORIGINAL_CODE (arg1);
1755 if (IS_EXPR_CODE_CLASS (class2))
1756 code2 = C_EXP_ORIGINAL_CODE (arg2);
1758 /* Check for cases such as x+y<<z which users are likely
1759 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1760 is cleared to prevent these warnings. */
1761 if (warn_parentheses)
1763 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1765 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1766 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1767 warning ("suggest parentheses around + or - inside shift");
1770 if (code == TRUTH_ORIF_EXPR)
1772 if (code1 == TRUTH_ANDIF_EXPR
1773 || code2 == TRUTH_ANDIF_EXPR)
1774 warning ("suggest parentheses around && within ||");
1777 if (code == BIT_IOR_EXPR)
1779 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1780 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1781 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1782 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1783 warning ("suggest parentheses around arithmetic in operand of |");
1784 /* Check cases like x|y==z */
1785 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1786 warning ("suggest parentheses around comparison in operand of |");
1789 if (code == BIT_XOR_EXPR)
1791 if (code1 == BIT_AND_EXPR
1792 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1793 || code2 == BIT_AND_EXPR
1794 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1795 warning ("suggest parentheses around arithmetic in operand of ^");
1796 /* Check cases like x^y==z */
1797 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1798 warning ("suggest parentheses around comparison in operand of ^");
1801 if (code == BIT_AND_EXPR)
1803 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1804 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1805 warning ("suggest parentheses around + or - in operand of &");
1806 /* Check cases like x&y==z */
1807 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1808 warning ("suggest parentheses around comparison in operand of &");
1812 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1813 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1814 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1815 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1817 unsigned_conversion_warning (result, arg1);
1818 unsigned_conversion_warning (result, arg2);
1819 overflow_warning (result);
1821 class = TREE_CODE_CLASS (TREE_CODE (result));
1823 /* Record the code that was specified in the source,
1824 for the sake of warnings about confusing nesting. */
1825 if (IS_EXPR_CODE_CLASS (class))
1826 C_SET_EXP_ORIGINAL_CODE (result, code);
1829 int flag = TREE_CONSTANT (result);
1830 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1831 so that convert_for_assignment wouldn't strip it.
1832 That way, we got warnings for things like p = (1 - 1).
1833 But it turns out we should not get those warnings. */
1834 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1835 C_SET_EXP_ORIGINAL_CODE (result, code);
1836 TREE_CONSTANT (result) = flag;
1842 /* Build a binary-operation expression without default conversions.
1843 CODE is the kind of expression to build.
1844 This function differs from `build' in several ways:
1845 the data type of the result is computed and recorded in it,
1846 warnings are generated if arg data types are invalid,
1847 special handling for addition and subtraction of pointers is known,
1848 and some optimization is done (operations on narrow ints
1849 are done in the narrower type when that gives the same result).
1850 Constant folding is also done before the result is returned.
1852 Note that the operands will never have enumeral types, or function
1853 or array types, because either they will have the default conversions
1854 performed or they have both just been converted to some other type in which
1855 the arithmetic is to be done. */
1858 build_binary_op (code, orig_op0, orig_op1, convert_p)
1859 enum tree_code code;
1860 tree orig_op0, orig_op1;
1864 enum tree_code code0, code1;
1867 /* Expression code to give to the expression when it is built.
1868 Normally this is CODE, which is what the caller asked for,
1869 but in some special cases we change it. */
1870 enum tree_code resultcode = code;
1872 /* Data type in which the computation is to be performed.
1873 In the simplest cases this is the common type of the arguments. */
1874 tree result_type = NULL;
1876 /* Nonzero means operands have already been type-converted
1877 in whatever way is necessary.
1878 Zero means they need to be converted to RESULT_TYPE. */
1881 /* Nonzero means create the expression with this type, rather than
1883 tree build_type = 0;
1885 /* Nonzero means after finally constructing the expression
1886 convert it to this type. */
1887 tree final_type = 0;
1889 /* Nonzero if this is an operation like MIN or MAX which can
1890 safely be computed in short if both args are promoted shorts.
1891 Also implies COMMON.
1892 -1 indicates a bitwise operation; this makes a difference
1893 in the exact conditions for when it is safe to do the operation
1894 in a narrower mode. */
1897 /* Nonzero if this is a comparison operation;
1898 if both args are promoted shorts, compare the original shorts.
1899 Also implies COMMON. */
1900 int short_compare = 0;
1902 /* Nonzero if this is a right-shift operation, which can be computed on the
1903 original short and then promoted if the operand is a promoted short. */
1904 int short_shift = 0;
1906 /* Nonzero means set RESULT_TYPE to the common type of the args. */
1911 op0 = default_conversion (orig_op0);
1912 op1 = default_conversion (orig_op1);
1920 type0 = TREE_TYPE (op0);
1921 type1 = TREE_TYPE (op1);
1923 /* The expression codes of the data types of the arguments tell us
1924 whether the arguments are integers, floating, pointers, etc. */
1925 code0 = TREE_CODE (type0);
1926 code1 = TREE_CODE (type1);
1928 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1929 STRIP_TYPE_NOPS (op0);
1930 STRIP_TYPE_NOPS (op1);
1932 /* If an error was already reported for one of the arguments,
1933 avoid reporting another error. */
1935 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
1936 return error_mark_node;
1941 /* Handle the pointer + int case. */
1942 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1943 return pointer_int_sum (PLUS_EXPR, op0, op1);
1944 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
1945 return pointer_int_sum (PLUS_EXPR, op1, op0);
1951 /* Subtraction of two similar pointers.
1952 We must subtract them as integers, then divide by object size. */
1953 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1954 && comp_target_types (type0, type1))
1955 return pointer_diff (op0, op1);
1956 /* Handle pointer minus int. Just like pointer plus int. */
1957 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
1958 return pointer_int_sum (MINUS_EXPR, op0, op1);
1967 case TRUNC_DIV_EXPR:
1969 case FLOOR_DIV_EXPR:
1970 case ROUND_DIV_EXPR:
1971 case EXACT_DIV_EXPR:
1972 /* Floating point division by zero is a legitimate way to obtain
1973 infinities and NaNs. */
1974 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
1975 warning ("division by zero");
1977 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
1978 || code0 == COMPLEX_TYPE)
1979 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
1980 || code1 == COMPLEX_TYPE))
1982 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
1983 resultcode = RDIV_EXPR;
1985 /* Although it would be tempting to shorten always here, that
1986 loses on some targets, since the modulo instruction is
1987 undefined if the quotient can't be represented in the
1988 computation mode. We shorten only if unsigned or if
1989 dividing by something we know != -1. */
1990 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
1991 || (TREE_CODE (op1) == INTEGER_CST
1992 && ! integer_all_onesp (op1)));
1998 case BIT_ANDTC_EXPR:
2001 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2003 /* If one operand is a constant, and the other is a short type
2004 that has been converted to an int,
2005 really do the work in the short type and then convert the
2006 result to int. If we are lucky, the constant will be 0 or 1
2007 in the short type, making the entire operation go away. */
2008 if (TREE_CODE (op0) == INTEGER_CST
2009 && TREE_CODE (op1) == NOP_EXPR
2010 && TYPE_PRECISION (type1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
2011 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
2013 final_type = result_type;
2014 op1 = TREE_OPERAND (op1, 0);
2015 result_type = TREE_TYPE (op1);
2017 if (TREE_CODE (op1) == INTEGER_CST
2018 && TREE_CODE (op0) == NOP_EXPR
2019 && TYPE_PRECISION (type0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
2020 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
2022 final_type = result_type;
2023 op0 = TREE_OPERAND (op0, 0);
2024 result_type = TREE_TYPE (op0);
2028 case TRUNC_MOD_EXPR:
2029 case FLOOR_MOD_EXPR:
2030 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2031 warning ("division by zero");
2033 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2035 /* Although it would be tempting to shorten always here, that loses
2036 on some targets, since the modulo instruction is undefined if the
2037 quotient can't be represented in the computation mode. We shorten
2038 only if unsigned or if dividing by something we know != -1. */
2039 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2040 || (TREE_CODE (op1) == INTEGER_CST
2041 && ! integer_all_onesp (op1)));
2046 case TRUTH_ANDIF_EXPR:
2047 case TRUTH_ORIF_EXPR:
2048 case TRUTH_AND_EXPR:
2050 case TRUTH_XOR_EXPR:
2051 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2052 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2053 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2054 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2056 /* Result of these operations is always an int,
2057 but that does not mean the operands should be
2058 converted to ints! */
2059 result_type = integer_type_node;
2060 op0 = truthvalue_conversion (op0);
2061 op1 = truthvalue_conversion (op1);
2066 /* Shift operations: result has same type as first operand;
2067 always convert second operand to int.
2068 Also set SHORT_SHIFT if shifting rightward. */
2071 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2073 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2075 if (tree_int_cst_sgn (op1) < 0)
2076 warning ("right shift count is negative");
2079 if (! integer_zerop (op1))
2082 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2083 warning ("right shift count >= width of type");
2087 /* Use the type of the value to be shifted.
2088 This is what most traditional C compilers do. */
2089 result_type = type0;
2090 /* Unless traditional, convert the shift-count to an integer,
2091 regardless of size of value being shifted. */
2092 if (! flag_traditional)
2094 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2095 op1 = convert (integer_type_node, op1);
2096 /* Avoid converting op1 to result_type later. */
2103 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2105 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2107 if (tree_int_cst_sgn (op1) < 0)
2108 warning ("left shift count is negative");
2110 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2111 warning ("left shift count >= width of type");
2114 /* Use the type of the value to be shifted.
2115 This is what most traditional C compilers do. */
2116 result_type = type0;
2117 /* Unless traditional, convert the shift-count to an integer,
2118 regardless of size of value being shifted. */
2119 if (! flag_traditional)
2121 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2122 op1 = convert (integer_type_node, op1);
2123 /* Avoid converting op1 to result_type later. */
2131 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2133 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
2135 if (tree_int_cst_sgn (op1) < 0)
2136 warning ("shift count is negative");
2137 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
2138 warning ("shift count >= width of type");
2141 /* Use the type of the value to be shifted.
2142 This is what most traditional C compilers do. */
2143 result_type = type0;
2144 /* Unless traditional, convert the shift-count to an integer,
2145 regardless of size of value being shifted. */
2146 if (! flag_traditional)
2148 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2149 op1 = convert (integer_type_node, op1);
2150 /* Avoid converting op1 to result_type later. */
2158 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2159 warning ("comparing floating point with == or != is unsafe");
2160 /* Result of comparison is always int,
2161 but don't convert the args to int! */
2162 build_type = integer_type_node;
2163 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
2164 || code0 == COMPLEX_TYPE)
2165 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
2166 || code1 == COMPLEX_TYPE))
2168 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2170 tree tt0 = TREE_TYPE (type0);
2171 tree tt1 = TREE_TYPE (type1);
2172 /* Anything compares with void *. void * compares with anything.
2173 Otherwise, the targets must be compatible
2174 and both must be object or both incomplete. */
2175 if (comp_target_types (type0, type1))
2176 result_type = common_type (type0, type1);
2177 else if (VOID_TYPE_P (tt0))
2179 /* op0 != orig_op0 detects the case of something
2180 whose value is 0 but which isn't a valid null ptr const. */
2181 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
2182 && TREE_CODE (tt1) == FUNCTION_TYPE)
2183 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2185 else if (VOID_TYPE_P (tt1))
2187 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
2188 && TREE_CODE (tt0) == FUNCTION_TYPE)
2189 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
2192 pedwarn ("comparison of distinct pointer types lacks a cast");
2194 if (result_type == NULL_TREE)
2195 result_type = ptr_type_node;
2197 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2198 && integer_zerop (op1))
2199 result_type = type0;
2200 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2201 && integer_zerop (op0))
2202 result_type = type1;
2203 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2205 result_type = type0;
2206 if (! flag_traditional)
2207 pedwarn ("comparison between pointer and integer");
2209 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2211 result_type = type1;
2212 if (! flag_traditional)
2213 pedwarn ("comparison between pointer and integer");
2219 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2220 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2222 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2224 if (comp_target_types (type0, type1))
2226 result_type = common_type (type0, type1);
2228 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2229 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2233 result_type = ptr_type_node;
2234 pedwarn ("comparison of distinct pointer types lacks a cast");
2243 build_type = integer_type_node;
2244 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2245 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
2247 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2249 if (comp_target_types (type0, type1))
2251 result_type = common_type (type0, type1);
2252 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2253 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
2254 pedwarn ("comparison of complete and incomplete pointers");
2256 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
2257 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
2261 result_type = ptr_type_node;
2262 pedwarn ("comparison of distinct pointer types lacks a cast");
2265 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2266 && integer_zerop (op1))
2268 result_type = type0;
2269 if (pedantic || extra_warnings)
2270 pedwarn ("ordered comparison of pointer with integer zero");
2272 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2273 && integer_zerop (op0))
2275 result_type = type1;
2277 pedwarn ("ordered comparison of pointer with integer zero");
2279 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2281 result_type = type0;
2282 if (! flag_traditional)
2283 pedwarn ("comparison between pointer and integer");
2285 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2287 result_type = type1;
2288 if (! flag_traditional)
2289 pedwarn ("comparison between pointer and integer");
2293 case UNORDERED_EXPR:
2300 build_type = integer_type_node;
2301 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2303 error ("unordered comparison on non-floating point argument");
2304 return error_mark_node;
2313 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2315 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
2317 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2319 if (shorten || common || short_compare)
2320 result_type = common_type (type0, type1);
2322 /* For certain operations (which identify themselves by shorten != 0)
2323 if both args were extended from the same smaller type,
2324 do the arithmetic in that type and then extend.
2326 shorten !=0 and !=1 indicates a bitwise operation.
2327 For them, this optimization is safe only if
2328 both args are zero-extended or both are sign-extended.
2329 Otherwise, we might change the result.
2330 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2331 but calculated in (unsigned short) it would be (unsigned short)-1. */
2333 if (shorten && none_complex)
2335 int unsigned0, unsigned1;
2336 tree arg0 = get_narrower (op0, &unsigned0);
2337 tree arg1 = get_narrower (op1, &unsigned1);
2338 /* UNS is 1 if the operation to be done is an unsigned one. */
2339 int uns = TREE_UNSIGNED (result_type);
2342 final_type = result_type;
2344 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2345 but it *requires* conversion to FINAL_TYPE. */
2347 if ((TYPE_PRECISION (TREE_TYPE (op0))
2348 == TYPE_PRECISION (TREE_TYPE (arg0)))
2349 && TREE_TYPE (op0) != final_type)
2350 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
2351 if ((TYPE_PRECISION (TREE_TYPE (op1))
2352 == TYPE_PRECISION (TREE_TYPE (arg1)))
2353 && TREE_TYPE (op1) != final_type)
2354 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2356 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2358 /* For bitwise operations, signedness of nominal type
2359 does not matter. Consider only how operands were extended. */
2363 /* Note that in all three cases below we refrain from optimizing
2364 an unsigned operation on sign-extended args.
2365 That would not be valid. */
2367 /* Both args variable: if both extended in same way
2368 from same width, do it in that width.
2369 Do it unsigned if args were zero-extended. */
2370 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2371 < TYPE_PRECISION (result_type))
2372 && (TYPE_PRECISION (TREE_TYPE (arg1))
2373 == TYPE_PRECISION (TREE_TYPE (arg0)))
2374 && unsigned0 == unsigned1
2375 && (unsigned0 || !uns))
2377 = signed_or_unsigned_type (unsigned0,
2378 common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2379 else if (TREE_CODE (arg0) == INTEGER_CST
2380 && (unsigned1 || !uns)
2381 && (TYPE_PRECISION (TREE_TYPE (arg1))
2382 < TYPE_PRECISION (result_type))
2383 && (type = signed_or_unsigned_type (unsigned1,
2385 int_fits_type_p (arg0, type)))
2387 else if (TREE_CODE (arg1) == INTEGER_CST
2388 && (unsigned0 || !uns)
2389 && (TYPE_PRECISION (TREE_TYPE (arg0))
2390 < TYPE_PRECISION (result_type))
2391 && (type = signed_or_unsigned_type (unsigned0,
2393 int_fits_type_p (arg1, type)))
2397 /* Shifts can be shortened if shifting right. */
2402 tree arg0 = get_narrower (op0, &unsigned_arg);
2404 final_type = result_type;
2406 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2407 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2409 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
2410 /* We can shorten only if the shift count is less than the
2411 number of bits in the smaller type size. */
2412 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
2413 /* We cannot drop an unsigned shift after sign-extension. */
2414 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
2416 /* Do an unsigned shift if the operand was zero-extended. */
2418 = signed_or_unsigned_type (unsigned_arg, TREE_TYPE (arg0));
2419 /* Convert value-to-be-shifted to that type. */
2420 if (TREE_TYPE (op0) != result_type)
2421 op0 = convert (result_type, op0);
2426 /* Comparison operations are shortened too but differently.
2427 They identify themselves by setting short_compare = 1. */
2431 /* Don't write &op0, etc., because that would prevent op0
2432 from being kept in a register.
2433 Instead, make copies of the our local variables and
2434 pass the copies by reference, then copy them back afterward. */
2435 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2436 enum tree_code xresultcode = resultcode;
2438 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
2443 op0 = xop0, op1 = xop1;
2445 resultcode = xresultcode;
2447 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare != 0)
2448 && skip_evaluation == 0)
2450 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2451 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
2452 int unsignedp0, unsignedp1;
2453 tree primop0 = get_narrower (op0, &unsignedp0);
2454 tree primop1 = get_narrower (op1, &unsignedp1);
2458 STRIP_TYPE_NOPS (xop0);
2459 STRIP_TYPE_NOPS (xop1);
2461 /* Give warnings for comparisons between signed and unsigned
2462 quantities that may fail.
2464 Do the checking based on the original operand trees, so that
2465 casts will be considered, but default promotions won't be.
2467 Do not warn if the comparison is being done in a signed type,
2468 since the signed type will only be chosen if it can represent
2469 all the values of the unsigned type. */
2470 if (! TREE_UNSIGNED (result_type))
2472 /* Do not warn if both operands are the same signedness. */
2473 else if (op0_signed == op1_signed)
2480 sop = xop0, uop = xop1;
2482 sop = xop1, uop = xop0;
2484 /* Do not warn if the signed quantity is an
2485 unsuffixed integer literal (or some static
2486 constant expression involving such literals or a
2487 conditional expression involving such literals)
2488 and it is non-negative. */
2489 if (tree_expr_nonnegative_p (sop))
2491 /* Do not warn if the comparison is an equality operation,
2492 the unsigned quantity is an integral constant, and it
2493 would fit in the result if the result were signed. */
2494 else if (TREE_CODE (uop) == INTEGER_CST
2495 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
2496 && int_fits_type_p (uop, signed_type (result_type)))
2498 /* Do not warn if the unsigned quantity is an enumeration
2499 constant and its maximum value would fit in the result
2500 if the result were signed. */
2501 else if (TREE_CODE (uop) == INTEGER_CST
2502 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
2503 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2504 signed_type (result_type)))
2507 warning ("comparison between signed and unsigned");
2510 /* Warn if two unsigned values are being compared in a size
2511 larger than their original size, and one (and only one) is the
2512 result of a `~' operator. This comparison will always fail.
2514 Also warn if one operand is a constant, and the constant
2515 does not have all bits set that are set in the ~ operand
2516 when it is extended. */
2518 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2519 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2521 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2522 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2525 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2528 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
2531 HOST_WIDE_INT constant, mask;
2532 int unsignedp, bits;
2534 if (host_integerp (primop0, 0))
2537 unsignedp = unsignedp1;
2538 constant = tree_low_cst (primop0, 0);
2543 unsignedp = unsignedp0;
2544 constant = tree_low_cst (primop1, 0);
2547 bits = TYPE_PRECISION (TREE_TYPE (primop));
2548 if (bits < TYPE_PRECISION (result_type)
2549 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
2551 mask = (~ (HOST_WIDE_INT) 0) << bits;
2552 if ((mask & constant) != mask)
2553 warning ("comparison of promoted ~unsigned with constant");
2556 else if (unsignedp0 && unsignedp1
2557 && (TYPE_PRECISION (TREE_TYPE (primop0))
2558 < TYPE_PRECISION (result_type))
2559 && (TYPE_PRECISION (TREE_TYPE (primop1))
2560 < TYPE_PRECISION (result_type)))
2561 warning ("comparison of promoted ~unsigned with unsigned");
2567 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2568 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2569 Then the expression will be built.
2570 It will be given type FINAL_TYPE if that is nonzero;
2571 otherwise, it will be given type RESULT_TYPE. */
2575 binary_op_error (code);
2576 return error_mark_node;
2581 if (TREE_TYPE (op0) != result_type)
2582 op0 = convert (result_type, op0);
2583 if (TREE_TYPE (op1) != result_type)
2584 op1 = convert (result_type, op1);
2587 if (build_type == NULL_TREE)
2588 build_type = result_type;
2591 tree result = build (resultcode, build_type, op0, op1);
2594 folded = fold (result);
2595 if (folded == result)
2596 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2597 if (final_type != 0)
2598 return convert (final_type, folded);
2603 /* Return a tree for the sum or difference (RESULTCODE says which)
2604 of pointer PTROP and integer INTOP. */
2607 pointer_int_sum (resultcode, ptrop, intop)
2608 enum tree_code resultcode;
2616 /* The result is a pointer of the same type that is being added. */
2618 tree result_type = TREE_TYPE (ptrop);
2620 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2622 if (pedantic || warn_pointer_arith)
2623 pedwarn ("pointer of type `void *' used in arithmetic");
2624 size_exp = integer_one_node;
2626 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2628 if (pedantic || warn_pointer_arith)
2629 pedwarn ("pointer to a function used in arithmetic");
2630 size_exp = integer_one_node;
2633 size_exp = c_size_in_bytes (TREE_TYPE (result_type));
2635 /* If what we are about to multiply by the size of the elements
2636 contains a constant term, apply distributive law
2637 and multiply that constant term separately.
2638 This helps produce common subexpressions. */
2640 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2641 && ! TREE_CONSTANT (intop)
2642 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2643 && TREE_CONSTANT (size_exp)
2644 /* If the constant comes from pointer subtraction,
2645 skip this optimization--it would cause an error. */
2646 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2647 /* If the constant is unsigned, and smaller than the pointer size,
2648 then we must skip this optimization. This is because it could cause
2649 an overflow error if the constant is negative but INTOP is not. */
2650 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2651 || (TYPE_PRECISION (TREE_TYPE (intop))
2652 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2654 enum tree_code subcode = resultcode;
2655 tree int_type = TREE_TYPE (intop);
2656 if (TREE_CODE (intop) == MINUS_EXPR)
2657 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2658 /* Convert both subexpression types to the type of intop,
2659 because weird cases involving pointer arithmetic
2660 can result in a sum or difference with different type args. */
2661 ptrop = build_binary_op (subcode, ptrop,
2662 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2663 intop = convert (int_type, TREE_OPERAND (intop, 0));
2666 /* Convert the integer argument to a type the same size as sizetype
2667 so the multiply won't overflow spuriously. */
2669 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2670 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2671 intop = convert (type_for_size (TYPE_PRECISION (sizetype),
2672 TREE_UNSIGNED (sizetype)), intop);
2674 /* Replace the integer argument with a suitable product by the object size.
2675 Do this multiplication as signed, then convert to the appropriate
2676 pointer type (actually unsigned integral). */
2678 intop = convert (result_type,
2679 build_binary_op (MULT_EXPR, intop,
2680 convert (TREE_TYPE (intop), size_exp), 1));
2682 /* Create the sum or difference. */
2684 result = build (resultcode, result_type, ptrop, intop);
2686 folded = fold (result);
2687 if (folded == result)
2688 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2692 /* Return a tree for the difference of pointers OP0 and OP1.
2693 The resulting tree has type int. */
2696 pointer_diff (op0, op1)
2699 tree result, folded;
2700 tree restype = ptrdiff_type_node;
2702 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2703 tree con0, con1, lit0, lit1;
2704 tree orig_op1 = op1;
2706 if (pedantic || warn_pointer_arith)
2708 if (TREE_CODE (target_type) == VOID_TYPE)
2709 pedwarn ("pointer of type `void *' used in subtraction");
2710 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2711 pedwarn ("pointer to a function used in subtraction");
2714 /* If the conversion to ptrdiff_type does anything like widening or
2715 converting a partial to an integral mode, we get a convert_expression
2716 that is in the way to do any simplifications.
2717 (fold-const.c doesn't know that the extra bits won't be needed.
2718 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2719 different mode in place.)
2720 So first try to find a common term here 'by hand'; we want to cover
2721 at least the cases that occur in legal static initializers. */
2722 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2723 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2725 if (TREE_CODE (con0) == PLUS_EXPR)
2727 lit0 = TREE_OPERAND (con0, 1);
2728 con0 = TREE_OPERAND (con0, 0);
2731 lit0 = integer_zero_node;
2733 if (TREE_CODE (con1) == PLUS_EXPR)
2735 lit1 = TREE_OPERAND (con1, 1);
2736 con1 = TREE_OPERAND (con1, 0);
2739 lit1 = integer_zero_node;
2741 if (operand_equal_p (con0, con1, 0))
2748 /* First do the subtraction as integers;
2749 then drop through to build the divide operator.
2750 Do not do default conversions on the minus operator
2751 in case restype is a short type. */
2753 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2754 convert (restype, op1), 0);
2755 /* This generates an error if op1 is pointer to incomplete type. */
2756 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2757 error ("arithmetic on pointer to an incomplete type");
2759 /* This generates an error if op0 is pointer to incomplete type. */
2760 op1 = c_size_in_bytes (target_type);
2762 /* Divide by the size, in easiest possible way. */
2764 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2766 folded = fold (result);
2767 if (folded == result)
2768 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2772 /* Construct and perhaps optimize a tree representation
2773 for a unary operation. CODE, a tree_code, specifies the operation
2774 and XARG is the operand. NOCONVERT nonzero suppresses
2775 the default promotions (such as from short to int). */
2778 build_unary_op (code, xarg, noconvert)
2779 enum tree_code code;
2783 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2786 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2789 if (typecode == ERROR_MARK)
2790 return error_mark_node;
2791 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2792 typecode = INTEGER_TYPE;
2797 /* This is used for unary plus, because a CONVERT_EXPR
2798 is enough to prevent anybody from looking inside for
2799 associativity, but won't generate any code. */
2800 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2801 || typecode == COMPLEX_TYPE))
2803 error ("wrong type argument to unary plus");
2804 return error_mark_node;
2806 else if (!noconvert)
2807 arg = default_conversion (arg);
2811 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2812 || typecode == COMPLEX_TYPE))
2814 error ("wrong type argument to unary minus");
2815 return error_mark_node;
2817 else if (!noconvert)
2818 arg = default_conversion (arg);
2822 if (typecode == COMPLEX_TYPE)
2826 pedwarn ("ISO C does not support `~' for complex conjugation");
2828 arg = default_conversion (arg);
2830 else if (typecode != INTEGER_TYPE)
2832 error ("wrong type argument to bit-complement");
2833 return error_mark_node;
2835 else if (!noconvert)
2836 arg = default_conversion (arg);
2840 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2841 || typecode == COMPLEX_TYPE))
2843 error ("wrong type argument to abs");
2844 return error_mark_node;
2846 else if (!noconvert)
2847 arg = default_conversion (arg);
2851 /* Conjugating a real value is a no-op, but allow it anyway. */
2852 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2853 || typecode == COMPLEX_TYPE))
2855 error ("wrong type argument to conjugation");
2856 return error_mark_node;
2858 else if (!noconvert)
2859 arg = default_conversion (arg);
2862 case TRUTH_NOT_EXPR:
2863 if (typecode != INTEGER_TYPE
2864 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2865 && typecode != COMPLEX_TYPE
2866 /* These will convert to a pointer. */
2867 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2869 error ("wrong type argument to unary exclamation mark");
2870 return error_mark_node;
2872 arg = truthvalue_conversion (arg);
2873 return invert_truthvalue (arg);
2879 if (TREE_CODE (arg) == COMPLEX_CST)
2880 return TREE_REALPART (arg);
2881 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2882 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2887 if (TREE_CODE (arg) == COMPLEX_CST)
2888 return TREE_IMAGPART (arg);
2889 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2890 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2892 return convert (TREE_TYPE (arg), integer_zero_node);
2894 case PREINCREMENT_EXPR:
2895 case POSTINCREMENT_EXPR:
2896 case PREDECREMENT_EXPR:
2897 case POSTDECREMENT_EXPR:
2898 /* Handle complex lvalues (when permitted)
2899 by reduction to simpler cases. */
2901 val = unary_complex_lvalue (code, arg);
2905 /* Increment or decrement the real part of the value,
2906 and don't change the imaginary part. */
2907 if (typecode == COMPLEX_TYPE)
2912 pedwarn ("ISO C does not support `++' and `--' on complex types");
2914 arg = stabilize_reference (arg);
2915 real = build_unary_op (REALPART_EXPR, arg, 1);
2916 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2917 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2918 build_unary_op (code, real, 1), imag);
2921 /* Report invalid types. */
2923 if (typecode != POINTER_TYPE
2924 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2926 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2927 error ("wrong type argument to increment");
2929 error ("wrong type argument to decrement");
2931 return error_mark_node;
2936 tree result_type = TREE_TYPE (arg);
2938 arg = get_unwidened (arg, 0);
2939 argtype = TREE_TYPE (arg);
2941 /* Compute the increment. */
2943 if (typecode == POINTER_TYPE)
2945 /* If pointer target is an undefined struct,
2946 we just cannot know how to do the arithmetic. */
2947 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2949 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2950 error ("increment of pointer to unknown structure");
2952 error ("decrement of pointer to unknown structure");
2954 else if ((pedantic || warn_pointer_arith)
2955 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2956 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2958 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2959 pedwarn ("wrong type argument to increment");
2961 pedwarn ("wrong type argument to decrement");
2964 inc = c_size_in_bytes (TREE_TYPE (result_type));
2967 inc = integer_one_node;
2969 inc = convert (argtype, inc);
2971 /* Handle incrementing a cast-expression. */
2974 switch (TREE_CODE (arg))
2979 case FIX_TRUNC_EXPR:
2980 case FIX_FLOOR_EXPR:
2981 case FIX_ROUND_EXPR:
2983 pedantic_lvalue_warning (CONVERT_EXPR);
2984 /* If the real type has the same machine representation
2985 as the type it is cast to, we can make better output
2986 by adding directly to the inside of the cast. */
2987 if ((TREE_CODE (TREE_TYPE (arg))
2988 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2989 && (TYPE_MODE (TREE_TYPE (arg))
2990 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2991 arg = TREE_OPERAND (arg, 0);
2994 tree incremented, modify, value;
2995 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2996 value = boolean_increment (code, arg);
2999 arg = stabilize_reference (arg);
3000 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3003 value = save_expr (arg);
3004 incremented = build (((code == PREINCREMENT_EXPR
3005 || code == POSTINCREMENT_EXPR)
3006 ? PLUS_EXPR : MINUS_EXPR),
3007 argtype, value, inc);
3008 TREE_SIDE_EFFECTS (incremented) = 1;
3009 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3010 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3012 TREE_USED (value) = 1;
3022 /* Complain about anything else that is not a true lvalue. */
3023 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3024 || code == POSTINCREMENT_EXPR)
3025 ? "invalid lvalue in increment"
3026 : "invalid lvalue in decrement")))
3027 return error_mark_node;
3029 /* Report a read-only lvalue. */
3030 if (TREE_READONLY (arg))
3031 readonly_warning (arg,
3032 ((code == PREINCREMENT_EXPR
3033 || code == POSTINCREMENT_EXPR)
3034 ? _("increment") : _("decrement")));
3036 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3037 val = boolean_increment (code, arg);
3039 val = build (code, TREE_TYPE (arg), arg, inc);
3040 TREE_SIDE_EFFECTS (val) = 1;
3041 val = convert (result_type, val);
3042 if (TREE_CODE (val) != code)
3043 TREE_NO_UNUSED_WARNING (val) = 1;
3048 /* Note that this operation never does default_conversion
3049 regardless of NOCONVERT. */
3051 /* Let &* cancel out to simplify resulting code. */
3052 if (TREE_CODE (arg) == INDIRECT_REF)
3054 /* Don't let this be an lvalue. */
3055 if (lvalue_p (TREE_OPERAND (arg, 0)))
3056 return non_lvalue (TREE_OPERAND (arg, 0));
3057 return TREE_OPERAND (arg, 0);
3060 /* For &x[y], return x+y */
3061 if (TREE_CODE (arg) == ARRAY_REF)
3063 if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
3064 return error_mark_node;
3065 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3066 TREE_OPERAND (arg, 1), 1);
3069 /* Handle complex lvalues (when permitted)
3070 by reduction to simpler cases. */
3071 val = unary_complex_lvalue (code, arg);
3075 #if 0 /* Turned off because inconsistent;
3076 float f; *&(int)f = 3.4 stores in int format
3077 whereas (int)f = 3.4 stores in float format. */
3078 /* Address of a cast is just a cast of the address
3079 of the operand of the cast. */
3080 switch (TREE_CODE (arg))
3085 case FIX_TRUNC_EXPR:
3086 case FIX_FLOOR_EXPR:
3087 case FIX_ROUND_EXPR:
3090 pedwarn ("ISO C forbids the address of a cast expression");
3091 return convert (build_pointer_type (TREE_TYPE (arg)),
3092 build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
3097 /* Allow the address of a constructor if all the elements
3099 if (TREE_CODE (arg) == CONSTRUCTOR && TREE_CONSTANT (arg))
3101 /* Anything not already handled and not a true memory reference
3103 else if (typecode != FUNCTION_TYPE
3104 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
3105 return error_mark_node;
3107 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3108 argtype = TREE_TYPE (arg);
3110 /* If the lvalue is const or volatile, merge that into the type
3111 to which the address will point. Note that you can't get a
3112 restricted pointer by taking the address of something, so we
3113 only have to deal with `const' and `volatile' here. */
3114 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3115 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3116 argtype = c_build_type_variant (argtype,
3117 TREE_READONLY (arg),
3118 TREE_THIS_VOLATILE (arg));
3120 argtype = build_pointer_type (argtype);
3122 if (mark_addressable (arg) == 0)
3123 return error_mark_node;
3128 if (TREE_CODE (arg) == COMPONENT_REF)
3130 tree field = TREE_OPERAND (arg, 1);
3132 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
3134 if (DECL_C_BIT_FIELD (field))
3136 error ("attempt to take address of bit-field structure member `%s'",
3137 IDENTIFIER_POINTER (DECL_NAME (field)));
3138 return error_mark_node;
3141 addr = fold (build (PLUS_EXPR, argtype,
3142 convert (argtype, addr),
3143 convert (argtype, byte_position (field))));
3146 addr = build1 (code, argtype, arg);
3148 /* Address of a static or external variable or
3149 file-scope function counts as a constant. */
3151 && ! (TREE_CODE (arg) == FUNCTION_DECL
3152 && DECL_CONTEXT (arg) != 0))
3153 TREE_CONSTANT (addr) = 1;
3162 argtype = TREE_TYPE (arg);
3163 return fold (build1 (code, argtype, arg));
3167 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
3168 convert ARG with the same conversions in the same order
3169 and return the result. */
3172 convert_sequence (conversions, arg)
3176 switch (TREE_CODE (conversions))
3181 case FIX_TRUNC_EXPR:
3182 case FIX_FLOOR_EXPR:
3183 case FIX_ROUND_EXPR:
3185 return convert (TREE_TYPE (conversions),
3186 convert_sequence (TREE_OPERAND (conversions, 0),
3195 /* Return nonzero if REF is an lvalue valid for this language.
3196 Lvalues can be assigned, unless their type has TYPE_READONLY.
3197 Lvalues can have their address taken, unless they have DECL_REGISTER. */
3203 enum tree_code code = TREE_CODE (ref);
3210 return lvalue_p (TREE_OPERAND (ref, 0));
3221 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3222 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3226 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3233 /* Return nonzero if REF is an lvalue valid for this language;
3234 otherwise, print an error message and return zero. */
3237 lvalue_or_else (ref, msgid)
3241 int win = lvalue_p (ref);
3244 error ("%s", msgid);
3249 /* Apply unary lvalue-demanding operator CODE to the expression ARG
3250 for certain kinds of expressions which are not really lvalues
3251 but which we can accept as lvalues.
3253 If ARG is not a kind of expression we can handle, return zero. */
3256 unary_complex_lvalue (code, arg)
3257 enum tree_code code;
3260 /* Handle (a, b) used as an "lvalue". */
3261 if (TREE_CODE (arg) == COMPOUND_EXPR)
3263 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
3265 /* If this returns a function type, it isn't really being used as
3266 an lvalue, so don't issue a warning about it. */
3267 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3268 pedantic_lvalue_warning (COMPOUND_EXPR);
3270 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3271 TREE_OPERAND (arg, 0), real_result);
3274 /* Handle (a ? b : c) used as an "lvalue". */
3275 if (TREE_CODE (arg) == COND_EXPR)
3277 pedantic_lvalue_warning (COND_EXPR);
3278 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE)
3279 pedantic_lvalue_warning (COMPOUND_EXPR);
3281 return (build_conditional_expr
3282 (TREE_OPERAND (arg, 0),
3283 build_unary_op (code, TREE_OPERAND (arg, 1), 0),
3284 build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
3290 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3291 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3294 pedantic_lvalue_warning (code)
3295 enum tree_code code;
3301 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
3304 pedwarn ("ISO C forbids use of compound expressions as lvalues");
3307 pedwarn ("ISO C forbids use of cast expressions as lvalues");
3312 /* Warn about storing in something that is `const'. */
3315 readonly_warning (arg, msgid)
3319 if (TREE_CODE (arg) == COMPONENT_REF)
3321 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3322 readonly_warning (TREE_OPERAND (arg, 0), msgid);
3324 pedwarn ("%s of read-only member `%s'", _(msgid),
3325 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
3327 else if (TREE_CODE (arg) == VAR_DECL)
3328 pedwarn ("%s of read-only variable `%s'", _(msgid),
3329 IDENTIFIER_POINTER (DECL_NAME (arg)));
3331 pedwarn ("%s of read-only location", _(msgid));
3334 /* Mark EXP saying that we need to be able to take the
3335 address of it; it should not be allocated in a register.
3336 Value is 1 if successful. */
3339 mark_addressable (exp)
3344 switch (TREE_CODE (x))
3347 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3349 error ("cannot take address of bitfield `%s'",
3350 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
3354 /* ... fall through ... */
3360 x = TREE_OPERAND (x, 0);
3364 TREE_ADDRESSABLE (x) = 1;
3371 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3372 && DECL_NONLOCAL (x))
3374 if (TREE_PUBLIC (x))
3376 error ("global register variable `%s' used in nested function",
3377 IDENTIFIER_POINTER (DECL_NAME (x)));
3380 pedwarn ("register variable `%s' used in nested function",
3381 IDENTIFIER_POINTER (DECL_NAME (x)));
3383 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
3385 if (TREE_PUBLIC (x))
3387 error ("address of global register variable `%s' requested",
3388 IDENTIFIER_POINTER (DECL_NAME (x)));
3392 /* If we are making this addressable due to its having
3393 volatile components, give a different error message. Also
3394 handle the case of an unnamed parameter by not trying
3395 to give the name. */
3397 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3399 error ("cannot put object with volatile field into register");
3403 pedwarn ("address of register variable `%s' requested",
3404 IDENTIFIER_POINTER (DECL_NAME (x)));
3406 put_var_into_stack (x);
3410 TREE_ADDRESSABLE (x) = 1;
3411 #if 0 /* poplevel deals with this now. */
3412 if (DECL_CONTEXT (x) == 0)
3413 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
3421 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3424 build_conditional_expr (ifexp, op1, op2)
3425 tree ifexp, op1, op2;
3429 enum tree_code code1;
3430 enum tree_code code2;
3431 tree result_type = NULL;
3432 tree orig_op1 = op1, orig_op2 = op2;
3434 ifexp = truthvalue_conversion (default_conversion (ifexp));
3436 #if 0 /* Produces wrong result if within sizeof. */
3437 /* Don't promote the operands separately if they promote
3438 the same way. Return the unpromoted type and let the combined
3439 value get promoted if necessary. */
3441 if (TREE_TYPE (op1) == TREE_TYPE (op2)
3442 && TREE_CODE (TREE_TYPE (op1)) != ARRAY_TYPE
3443 && TREE_CODE (TREE_TYPE (op1)) != ENUMERAL_TYPE
3444 && TREE_CODE (TREE_TYPE (op1)) != FUNCTION_TYPE)
3446 if (TREE_CODE (ifexp) == INTEGER_CST)
3447 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3449 return fold (build (COND_EXPR, TREE_TYPE (op1), ifexp, op1, op2));
3453 /* Promote both alternatives. */
3455 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3456 op1 = default_conversion (op1);
3457 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3458 op2 = default_conversion (op2);
3460 if (TREE_CODE (ifexp) == ERROR_MARK
3461 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3462 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3463 return error_mark_node;
3465 type1 = TREE_TYPE (op1);
3466 code1 = TREE_CODE (type1);
3467 type2 = TREE_TYPE (op2);
3468 code2 = TREE_CODE (type2);
3470 /* Quickly detect the usual case where op1 and op2 have the same type
3472 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3475 result_type = type1;
3477 result_type = TYPE_MAIN_VARIANT (type1);
3479 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3480 || code1 == COMPLEX_TYPE)
3481 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3482 || code2 == COMPLEX_TYPE))
3484 result_type = common_type (type1, type2);
3486 /* If -Wsign-compare, warn here if type1 and type2 have
3487 different signedness. We'll promote the signed to unsigned
3488 and later code won't know it used to be different.
3489 Do this check on the original types, so that explicit casts
3490 will be considered, but default promotions won't. */
3491 if ((warn_sign_compare < 0 ? extra_warnings : warn_sign_compare)
3492 && !skip_evaluation)
3494 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3495 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3497 if (unsigned_op1 ^ unsigned_op2)
3499 /* Do not warn if the result type is signed, since the
3500 signed type will only be chosen if it can represent
3501 all the values of the unsigned type. */
3502 if (! TREE_UNSIGNED (result_type))
3504 /* Do not warn if the signed quantity is an unsuffixed
3505 integer literal (or some static constant expression
3506 involving such literals) and it is non-negative. */
3507 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3508 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3511 warning ("signed and unsigned type in conditional expression");
3515 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3517 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3518 pedwarn ("ISO C forbids conditional expr with only one void side");
3519 result_type = void_type_node;
3521 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3523 if (comp_target_types (type1, type2))
3524 result_type = common_type (type1, type2);
3525 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3526 && TREE_CODE (orig_op1) != NOP_EXPR)
3527 result_type = qualify_type (type2, type1);
3528 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3529 && TREE_CODE (orig_op2) != NOP_EXPR)
3530 result_type = qualify_type (type1, type2);
3531 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3533 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3534 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3535 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3536 TREE_TYPE (type2)));
3538 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3540 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3541 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
3542 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3543 TREE_TYPE (type1)));
3547 pedwarn ("pointer type mismatch in conditional expression");
3548 result_type = build_pointer_type (void_type_node);
3551 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3553 if (! integer_zerop (op2))
3554 pedwarn ("pointer/integer type mismatch in conditional expression");
3557 op2 = null_pointer_node;
3559 result_type = type1;
3561 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3563 if (!integer_zerop (op1))
3564 pedwarn ("pointer/integer type mismatch in conditional expression");
3567 op1 = null_pointer_node;
3569 result_type = type2;
3574 if (flag_cond_mismatch)
3575 result_type = void_type_node;
3578 error ("type mismatch in conditional expression");
3579 return error_mark_node;
3583 /* Merge const and volatile flags of the incoming types. */
3585 = build_type_variant (result_type,
3586 TREE_READONLY (op1) || TREE_READONLY (op2),
3587 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3589 if (result_type != TREE_TYPE (op1))
3590 op1 = convert_and_check (result_type, op1);
3591 if (result_type != TREE_TYPE (op2))
3592 op2 = convert_and_check (result_type, op2);
3594 if (TREE_CODE (ifexp) == INTEGER_CST)
3595 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3597 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3600 /* Given a list of expressions, return a compound expression
3601 that performs them all and returns the value of the last of them. */
3604 build_compound_expr (list)
3607 return internal_build_compound_expr (list, TRUE);
3611 internal_build_compound_expr (list, first_p)
3617 if (TREE_CHAIN (list) == 0)
3619 /* Convert arrays to pointers when there really is a comma operator. */
3620 if (!first_p && TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
3621 TREE_VALUE (list) = default_conversion (TREE_VALUE (list));
3623 #if 0 /* If something inside inhibited lvalueness, we should not override. */
3624 /* Consider (x, y+0), which is not an lvalue since y+0 is not. */
3626 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3627 if (TREE_CODE (list) == NON_LVALUE_EXPR)
3628 list = TREE_OPERAND (list, 0);
3631 /* Don't let (0, 0) be null pointer constant. */
3632 if (!first_p && integer_zerop (TREE_VALUE (list)))
3633 return non_lvalue (TREE_VALUE (list));
3634 return TREE_VALUE (list);
3637 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
3639 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3641 /* The left-hand operand of a comma expression is like an expression
3642 statement: with -W or -Wunused, we should warn if it doesn't have
3643 any side-effects, unless it was explicitly cast to (void). */
3644 if ((extra_warnings || warn_unused_value)
3645 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
3646 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
3647 warning ("left-hand operand of comma expression has no effect");
3649 /* When pedantic, a compound expression can be neither an lvalue
3650 nor an integer constant expression. */
3655 /* With -Wunused, we should also warn if the left-hand operand does have
3656 side-effects, but computes a value which is not used. For example, in
3657 `foo() + bar(), baz()' the result of the `+' operator is not used,
3658 so we should issue a warning. */
3659 else if (warn_unused_value)
3660 warn_if_unused_value (TREE_VALUE (list));
3662 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3665 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3668 build_c_cast (type, expr)
3674 if (type == error_mark_node || expr == error_mark_node)
3675 return error_mark_node;
3676 type = TYPE_MAIN_VARIANT (type);
3679 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3680 if (TREE_CODE (value) == NON_LVALUE_EXPR)
3681 value = TREE_OPERAND (value, 0);
3684 if (TREE_CODE (type) == ARRAY_TYPE)
3686 error ("cast specifies array type");
3687 return error_mark_node;
3690 if (TREE_CODE (type) == FUNCTION_TYPE)
3692 error ("cast specifies function type");
3693 return error_mark_node;
3696 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3700 if (TREE_CODE (type) == RECORD_TYPE
3701 || TREE_CODE (type) == UNION_TYPE)
3702 pedwarn ("ISO C forbids casting nonscalar to the same type");
3705 else if (TREE_CODE (type) == UNION_TYPE)
3708 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
3709 || TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE)
3710 value = default_conversion (value);
3712 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3713 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3714 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3723 pedwarn ("ISO C forbids casts to union type");
3724 if (TYPE_NAME (type) != 0)
3726 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3727 name = IDENTIFIER_POINTER (TYPE_NAME (type));
3729 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
3733 t = digest_init (type, build (CONSTRUCTOR, type, NULL_TREE,
3734 build_tree_list (field, value)),
3736 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3739 error ("cast to union type from type not present in union");
3740 return error_mark_node;
3746 /* If casting to void, avoid the error that would come
3747 from default_conversion in the case of a non-lvalue array. */
3748 if (type == void_type_node)
3749 return build1 (CONVERT_EXPR, type, value);
3751 /* Convert functions and arrays to pointers,
3752 but don't convert any other types. */
3753 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
3754 || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE)
3755 value = default_conversion (value);
3756 otype = TREE_TYPE (value);
3758 /* Optionally warn about potentially worrisome casts. */
3761 && TREE_CODE (type) == POINTER_TYPE
3762 && TREE_CODE (otype) == POINTER_TYPE)
3764 tree in_type = type;
3765 tree in_otype = otype;
3768 /* Check that the qualifiers on IN_TYPE are a superset of
3769 the qualifiers of IN_OTYPE. The outermost level of
3770 POINTER_TYPE nodes is uninteresting and we stop as soon
3771 as we hit a non-POINTER_TYPE node on either type. */
3774 in_otype = TREE_TYPE (in_otype);
3775 in_type = TREE_TYPE (in_type);
3776 warn |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3778 while (TREE_CODE (in_type) == POINTER_TYPE
3779 && TREE_CODE (in_otype) == POINTER_TYPE);
3782 /* There are qualifiers present in IN_OTYPE that are not
3783 present in IN_TYPE. */
3784 warning ("cast discards qualifiers from pointer target type");
3787 /* Warn about possible alignment problems. */
3788 if (STRICT_ALIGNMENT && warn_cast_align
3789 && TREE_CODE (type) == POINTER_TYPE
3790 && TREE_CODE (otype) == POINTER_TYPE
3791 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3792 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3793 /* Don't warn about opaque types, where the actual alignment
3794 restriction is unknown. */
3795 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3796 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3797 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3798 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3799 warning ("cast increases required alignment of target type");
3801 if (TREE_CODE (type) == INTEGER_TYPE
3802 && TREE_CODE (otype) == POINTER_TYPE
3803 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3804 && !TREE_CONSTANT (value))
3805 warning ("cast from pointer to integer of different size");
3807 if (warn_bad_function_cast
3808 && TREE_CODE (value) == CALL_EXPR
3809 && TREE_CODE (type) != TREE_CODE (otype))
3810 warning ("cast does not match function type");
3812 if (TREE_CODE (type) == POINTER_TYPE
3813 && TREE_CODE (otype) == INTEGER_TYPE
3814 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3815 /* Don't warn about converting any constant. */
3816 && !TREE_CONSTANT (value))
3817 warning ("cast to pointer from integer of different size");
3820 value = convert (type, value);
3822 /* Ignore any integer overflow caused by the cast. */
3823 if (TREE_CODE (value) == INTEGER_CST)
3825 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3826 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3830 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3831 if (pedantic && TREE_CODE (value) == INTEGER_CST
3832 && TREE_CODE (expr) == INTEGER_CST
3833 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3834 value = non_lvalue (value);
3836 /* If pedantic, don't let a cast be an lvalue. */
3837 if (value == expr && pedantic)
3838 value = non_lvalue (value);
3843 /* Interpret a cast of expression EXPR to type TYPE. */
3845 c_cast_expr (type, expr)
3848 int saved_wsp = warn_strict_prototypes;
3850 /* This avoids warnings about unprototyped casts on
3851 integers. E.g. "#define SIG_DFL (void(*)())0". */
3852 if (TREE_CODE (expr) == INTEGER_CST)
3853 warn_strict_prototypes = 0;
3854 type = groktypename (type);
3855 warn_strict_prototypes = saved_wsp;
3857 return build_c_cast (type, expr);
3861 /* Build an assignment expression of lvalue LHS from value RHS.
3862 MODIFYCODE is the code for a binary operator that we use
3863 to combine the old value of LHS with RHS to get the new value.
3864 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3867 build_modify_expr (lhs, modifycode, rhs)
3869 enum tree_code modifycode;
3873 tree lhstype = TREE_TYPE (lhs);
3874 tree olhstype = lhstype;
3876 /* Types that aren't fully specified cannot be used in assignments. */
3877 lhs = require_complete_type (lhs);
3879 /* Avoid duplicate error messages from operands that had errors. */
3880 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3881 return error_mark_node;
3883 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3884 /* Do not use STRIP_NOPS here. We do not want an enumerator
3885 whose value is 0 to count as a null pointer constant. */
3886 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3887 rhs = TREE_OPERAND (rhs, 0);
3891 /* Handle control structure constructs used as "lvalues". */
3893 switch (TREE_CODE (lhs))
3895 /* Handle (a, b) used as an "lvalue". */
3897 pedantic_lvalue_warning (COMPOUND_EXPR);
3898 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3899 if (TREE_CODE (newrhs) == ERROR_MARK)
3900 return error_mark_node;
3901 return build (COMPOUND_EXPR, lhstype,
3902 TREE_OPERAND (lhs, 0), newrhs);
3904 /* Handle (a ? b : c) used as an "lvalue". */
3906 pedantic_lvalue_warning (COND_EXPR);
3907 rhs = save_expr (rhs);
3909 /* Produce (a ? (b = rhs) : (c = rhs))
3910 except that the RHS goes through a save-expr
3911 so the code to compute it is only emitted once. */
3913 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3914 build_modify_expr (TREE_OPERAND (lhs, 1),
3916 build_modify_expr (TREE_OPERAND (lhs, 2),
3918 if (TREE_CODE (cond) == ERROR_MARK)
3920 /* Make sure the code to compute the rhs comes out
3921 before the split. */
3922 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3923 /* But cast it to void to avoid an "unused" error. */
3924 convert (void_type_node, rhs), cond);
3930 /* If a binary op has been requested, combine the old LHS value with the RHS
3931 producing the value we should actually store into the LHS. */
3933 if (modifycode != NOP_EXPR)
3935 lhs = stabilize_reference (lhs);
3936 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3939 /* Handle a cast used as an "lvalue".
3940 We have already performed any binary operator using the value as cast.
3941 Now convert the result to the cast type of the lhs,
3942 and then true type of the lhs and store it there;
3943 then convert result back to the cast type to be the value
3944 of the assignment. */
3946 switch (TREE_CODE (lhs))
3951 case FIX_TRUNC_EXPR:
3952 case FIX_FLOOR_EXPR:
3953 case FIX_ROUND_EXPR:
3955 if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
3956 || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
3957 newrhs = default_conversion (newrhs);
3959 tree inner_lhs = TREE_OPERAND (lhs, 0);
3961 result = build_modify_expr (inner_lhs, NOP_EXPR,
3962 convert (TREE_TYPE (inner_lhs),
3963 convert (lhstype, newrhs)));
3964 if (TREE_CODE (result) == ERROR_MARK)
3966 pedantic_lvalue_warning (CONVERT_EXPR);
3967 return convert (TREE_TYPE (lhs), result);
3974 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3975 Reject anything strange now. */
3977 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3978 return error_mark_node;
3980 /* Warn about storing in something that is `const'. */
3982 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3983 || ((TREE_CODE (lhstype) == RECORD_TYPE
3984 || TREE_CODE (lhstype) == UNION_TYPE)
3985 && C_TYPE_FIELDS_READONLY (lhstype)))
3986 readonly_warning (lhs, "assignment");
3988 /* If storing into a structure or union member,
3989 it has probably been given type `int'.
3990 Compute the type that would go with
3991 the actual amount of storage the member occupies. */
3993 if (TREE_CODE (lhs) == COMPONENT_REF
3994 && (TREE_CODE (lhstype) == INTEGER_TYPE
3995 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3996 || TREE_CODE (lhstype) == REAL_TYPE
3997 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3998 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4000 /* If storing in a field that is in actuality a short or narrower than one,
4001 we must store in the field in its actual type. */
4003 if (lhstype != TREE_TYPE (lhs))
4005 lhs = copy_node (lhs);
4006 TREE_TYPE (lhs) = lhstype;
4009 /* Convert new value to destination type. */
4011 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
4012 NULL_TREE, NULL_TREE, 0);
4013 if (TREE_CODE (newrhs) == ERROR_MARK)
4014 return error_mark_node;
4018 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4019 TREE_SIDE_EFFECTS (result) = 1;
4021 /* If we got the LHS in a different type for storing in,
4022 convert the result back to the nominal type of LHS
4023 so that the value we return always has the same type
4024 as the LHS argument. */
4026 if (olhstype == TREE_TYPE (result))
4028 return convert_for_assignment (olhstype, result, _("assignment"),
4029 NULL_TREE, NULL_TREE, 0);
4032 /* Convert value RHS to type TYPE as preparation for an assignment
4033 to an lvalue of type TYPE.
4034 The real work of conversion is done by `convert'.
4035 The purpose of this function is to generate error messages
4036 for assignments that are not allowed in C.
4037 ERRTYPE is a string to use in error messages:
4038 "assignment", "return", etc. If it is null, this is parameter passing
4039 for a function call (and different error messages are output).
4041 FUNNAME is the name of the function being called,
4042 as an IDENTIFIER_NODE, or null.
4043 PARMNUM is the number of the argument, for printing in error messages. */
4046 convert_for_assignment (type, rhs, errtype, fundecl, funname, parmnum)
4048 const char *errtype;
4049 tree fundecl, funname;
4052 enum tree_code codel = TREE_CODE (type);
4054 enum tree_code coder;
4056 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4057 /* Do not use STRIP_NOPS here. We do not want an enumerator
4058 whose value is 0 to count as a null pointer constant. */
4059 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4060 rhs = TREE_OPERAND (rhs, 0);
4062 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4063 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4064 rhs = default_conversion (rhs);
4065 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
4066 rhs = decl_constant_value_for_broken_optimization (rhs);
4068 rhstype = TREE_TYPE (rhs);
4069 coder = TREE_CODE (rhstype);
4071 if (coder == ERROR_MARK)
4072 return error_mark_node;
4074 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4076 overflow_warning (rhs);
4077 /* Check for Objective-C protocols. This will issue a warning if
4078 there are protocol violations. No need to use the return value. */
4079 maybe_objc_comptypes (type, rhstype, 0);
4083 if (coder == VOID_TYPE)
4085 error ("void value not ignored as it ought to be");
4086 return error_mark_node;
4088 /* A type converts to a reference to it.
4089 This code doesn't fully support references, it's just for the
4090 special case of va_start and va_copy. */
4091 if (codel == REFERENCE_TYPE
4092 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4094 if (mark_addressable (rhs) == 0)
4095 return error_mark_node;
4096 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4098 /* We already know that these two types are compatible, but they
4099 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4100 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4101 likely to be va_list, a typedef to __builtin_va_list, which
4102 is different enough that it will cause problems later. */
4103 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4104 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4106 rhs = build1 (NOP_EXPR, type, rhs);
4109 /* Arithmetic types all interconvert, and enum is treated like int. */
4110 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4111 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4112 || codel == BOOLEAN_TYPE)
4113 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4114 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4115 || coder == BOOLEAN_TYPE))
4116 return convert_and_check (type, rhs);
4118 /* Conversion to a transparent union from its member types.
4119 This applies only to function arguments. */
4120 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4123 tree marginal_memb_type = 0;
4125 for (memb_types = TYPE_FIELDS (type); memb_types;
4126 memb_types = TREE_CHAIN (memb_types))
4128 tree memb_type = TREE_TYPE (memb_types);
4130 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
4131 TYPE_MAIN_VARIANT (rhstype)))
4134 if (TREE_CODE (memb_type) != POINTER_TYPE)
4137 if (coder == POINTER_TYPE)
4139 tree ttl = TREE_TYPE (memb_type);
4140 tree ttr = TREE_TYPE (rhstype);
4142 /* Any non-function converts to a [const][volatile] void *
4143 and vice versa; otherwise, targets must be the same.
4144 Meanwhile, the lhs target must have all the qualifiers of
4146 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4147 || comp_target_types (memb_type, rhstype))
4149 /* If this type won't generate any warnings, use it. */
4150 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4151 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4152 && TREE_CODE (ttl) == FUNCTION_TYPE)
4153 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4154 == TYPE_QUALS (ttr))
4155 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4156 == TYPE_QUALS (ttl))))
4159 /* Keep looking for a better type, but remember this one. */
4160 if (! marginal_memb_type)
4161 marginal_memb_type = memb_type;
4165 /* Can convert integer zero to any pointer type. */
4166 if (integer_zerop (rhs)
4167 || (TREE_CODE (rhs) == NOP_EXPR
4168 && integer_zerop (TREE_OPERAND (rhs, 0))))
4170 rhs = null_pointer_node;
4175 if (memb_types || marginal_memb_type)
4179 /* We have only a marginally acceptable member type;
4180 it needs a warning. */
4181 tree ttl = TREE_TYPE (marginal_memb_type);
4182 tree ttr = TREE_TYPE (rhstype);
4184 /* Const and volatile mean something different for function
4185 types, so the usual warnings are not appropriate. */
4186 if (TREE_CODE (ttr) == FUNCTION_TYPE
4187 && TREE_CODE (ttl) == FUNCTION_TYPE)
4189 /* Because const and volatile on functions are
4190 restrictions that say the function will not do
4191 certain things, it is okay to use a const or volatile
4192 function where an ordinary one is wanted, but not
4194 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4195 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4196 errtype, funname, parmnum);
4198 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4199 warn_for_assignment ("%s discards qualifiers from pointer target type",
4204 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
4205 pedwarn ("ISO C prohibits argument conversion to union type");
4207 return build1 (NOP_EXPR, type, rhs);
4211 /* Conversions among pointers */
4212 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
4213 && (coder == POINTER_TYPE || coder == REFERENCE_TYPE))
4215 tree ttl = TREE_TYPE (type);
4216 tree ttr = TREE_TYPE (rhstype);
4218 /* Any non-function converts to a [const][volatile] void *
4219 and vice versa; otherwise, targets must be the same.
4220 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
4221 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4222 || comp_target_types (type, rhstype)
4223 || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
4224 == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
4227 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
4230 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4231 which are not ANSI null ptr constants. */
4232 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
4233 && TREE_CODE (ttl) == FUNCTION_TYPE)))
4234 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
4235 errtype, funname, parmnum);
4236 /* Const and volatile mean something different for function types,
4237 so the usual warnings are not appropriate. */
4238 else if (TREE_CODE (ttr) != FUNCTION_TYPE
4239 && TREE_CODE (ttl) != FUNCTION_TYPE)
4241 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4242 warn_for_assignment ("%s discards qualifiers from pointer target type",
4243 errtype, funname, parmnum);
4244 /* If this is not a case of ignoring a mismatch in signedness,
4246 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4247 || comp_target_types (type, rhstype))
4249 /* If there is a mismatch, do warn. */
4251 warn_for_assignment ("pointer targets in %s differ in signedness",
4252 errtype, funname, parmnum);
4254 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4255 && TREE_CODE (ttr) == FUNCTION_TYPE)
4257 /* Because const and volatile on functions are restrictions
4258 that say the function will not do certain things,
4259 it is okay to use a const or volatile function
4260 where an ordinary one is wanted, but not vice-versa. */
4261 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4262 warn_for_assignment ("%s makes qualified function pointer from unqualified",
4263 errtype, funname, parmnum);
4267 warn_for_assignment ("%s from incompatible pointer type",
4268 errtype, funname, parmnum);
4269 return convert (type, rhs);
4271 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4273 /* An explicit constant 0 can convert to a pointer,
4274 or one that results from arithmetic, even including
4275 a cast to integer type. */
4276 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4278 ! (TREE_CODE (rhs) == NOP_EXPR
4279 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4280 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4281 && integer_zerop (TREE_OPERAND (rhs, 0))))
4283 warn_for_assignment ("%s makes pointer from integer without a cast",
4284 errtype, funname, parmnum);
4285 return convert (type, rhs);
4287 return null_pointer_node;
4289 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4291 warn_for_assignment ("%s makes integer from pointer without a cast",
4292 errtype, funname, parmnum);
4293 return convert (type, rhs);
4295 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4296 return convert (type, rhs);
4302 tree selector = maybe_building_objc_message_expr ();
4304 if (selector && parmnum > 2)
4305 error ("incompatible type for argument %d of `%s'",
4306 parmnum - 2, IDENTIFIER_POINTER (selector));
4308 error ("incompatible type for argument %d of `%s'",
4309 parmnum, IDENTIFIER_POINTER (funname));
4312 error ("incompatible type for argument %d of indirect function call",
4316 error ("incompatible types in %s", errtype);
4318 return error_mark_node;
4321 /* Print a warning using MSGID.
4322 It gets OPNAME as its one parameter.
4323 If OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
4324 FUNCTION and ARGNUM are handled specially if we are building an
4325 Objective-C selector. */
4328 warn_for_assignment (msgid, opname, function, argnum)
4336 tree selector = maybe_building_objc_message_expr ();
4339 if (selector && argnum > 2)
4341 function = selector;
4346 /* Function name is known; supply it. */
4347 const char *const argstring = _("passing arg %d of `%s'");
4348 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4349 + strlen (argstring) + 1 + 25
4351 sprintf (new_opname, argstring, argnum,
4352 IDENTIFIER_POINTER (function));
4356 /* Function name unknown (call through ptr); just give arg number.*/
4357 const char *const argnofun = _("passing arg %d of pointer to function");
4358 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4359 sprintf (new_opname, argnofun, argnum);
4361 opname = new_opname;
4363 pedwarn (msgid, opname);
4366 /* If VALUE is a compound expr all of whose expressions are constant, then
4367 return its value. Otherwise, return error_mark_node.
4369 This is for handling COMPOUND_EXPRs as initializer elements
4370 which is allowed with a warning when -pedantic is specified. */
4373 valid_compound_expr_initializer (value, endtype)
4377 if (TREE_CODE (value) == COMPOUND_EXPR)
4379 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4381 return error_mark_node;
4382 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4385 else if (! TREE_CONSTANT (value)
4386 && ! initializer_constant_valid_p (value, endtype))
4387 return error_mark_node;
4392 /* Perform appropriate conversions on the initial value of a variable,
4393 store it in the declaration DECL,
4394 and print any error messages that are appropriate.
4395 If the init is invalid, store an ERROR_MARK. */
4398 store_init_value (decl, init)
4403 /* If variable's type was invalidly declared, just ignore it. */
4405 type = TREE_TYPE (decl);
4406 if (TREE_CODE (type) == ERROR_MARK)
4409 /* Digest the specified initializer into an expression. */
4411 value = digest_init (type, init, TREE_STATIC (decl),
4412 TREE_STATIC (decl) || (pedantic && !flag_isoc99));
4414 /* Store the expression if valid; else report error. */
4417 /* Note that this is the only place we can detect the error
4418 in a case such as struct foo bar = (struct foo) { x, y };
4419 where there is one initial value which is a constructor expression. */
4420 if (value == error_mark_node)
4422 else if (TREE_STATIC (decl) && ! TREE_CONSTANT (value))
4424 error ("initializer for static variable is not constant");
4425 value = error_mark_node;
4427 else if (TREE_STATIC (decl)
4428 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
4430 error ("initializer for static variable uses complicated arithmetic");
4431 value = error_mark_node;
4435 if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
4437 if (! TREE_CONSTANT (value))
4438 pedwarn ("aggregate initializer is not constant");
4439 else if (! TREE_STATIC (value))
4440 pedwarn ("aggregate initializer uses complicated arithmetic");
4445 if (warn_traditional && !in_system_header
4446 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4447 warning ("traditional C rejects automatic aggregate initialization");
4449 DECL_INITIAL (decl) = value;
4451 /* ANSI wants warnings about out-of-range constant initializers. */
4452 STRIP_TYPE_NOPS (value);
4453 constant_expression_warning (value);
4456 /* Methods for storing and printing names for error messages. */
4458 /* Implement a spelling stack that allows components of a name to be pushed
4459 and popped. Each element on the stack is this structure. */
4471 #define SPELLING_STRING 1
4472 #define SPELLING_MEMBER 2
4473 #define SPELLING_BOUNDS 3
4475 static struct spelling *spelling; /* Next stack element (unused). */
4476 static struct spelling *spelling_base; /* Spelling stack base. */
4477 static int spelling_size; /* Size of the spelling stack. */
4479 /* Macros to save and restore the spelling stack around push_... functions.
4480 Alternative to SAVE_SPELLING_STACK. */
4482 #define SPELLING_DEPTH() (spelling - spelling_base)
4483 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4485 /* Save and restore the spelling stack around arbitrary C code. */
4487 #define SAVE_SPELLING_DEPTH(code) \
4489 int __depth = SPELLING_DEPTH (); \
4491 RESTORE_SPELLING_DEPTH (__depth); \
4494 /* Push an element on the spelling stack with type KIND and assign VALUE
4497 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4499 int depth = SPELLING_DEPTH (); \
4501 if (depth >= spelling_size) \
4503 spelling_size += 10; \
4504 if (spelling_base == 0) \
4506 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4509 = (struct spelling *) xrealloc (spelling_base, \
4510 spelling_size * sizeof (struct spelling)); \
4511 RESTORE_SPELLING_DEPTH (depth); \
4514 spelling->kind = (KIND); \
4515 spelling->MEMBER = (VALUE); \
4519 /* Push STRING on the stack. Printed literally. */
4522 push_string (string)
4525 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4528 /* Push a member name on the stack. Printed as '.' STRING. */
4531 push_member_name (decl)
4535 const char *const string
4536 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4537 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4540 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4543 push_array_bounds (bounds)
4546 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4549 /* Compute the maximum size in bytes of the printed spelling. */
4557 for (p = spelling_base; p < spelling; p++)
4559 if (p->kind == SPELLING_BOUNDS)
4562 size += strlen (p->u.s) + 1;
4568 /* Print the spelling to BUFFER and return it. */
4571 print_spelling (buffer)
4577 for (p = spelling_base; p < spelling; p++)
4578 if (p->kind == SPELLING_BOUNDS)
4580 sprintf (d, "[%d]", p->u.i);
4586 if (p->kind == SPELLING_MEMBER)
4588 for (s = p->u.s; (*d = *s++); d++)
4595 /* Issue an error message for a bad initializer component.
4596 MSGID identifies the message.
4597 The component name is taken from the spelling stack. */
4605 error ("%s", msgid);
4606 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4608 error ("(near initialization for `%s')", ofwhat);
4611 /* Issue a pedantic warning for a bad initializer component.
4612 MSGID identifies the message.
4613 The component name is taken from the spelling stack. */
4616 pedwarn_init (msgid)
4621 pedwarn ("%s", msgid);
4622 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4624 pedwarn ("(near initialization for `%s')", ofwhat);
4627 /* Issue a warning for a bad initializer component.
4628 MSGID identifies the message.
4629 The component name is taken from the spelling stack. */
4632 warning_init (msgid)
4637 warning ("%s", msgid);
4638 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4640 warning ("(near initialization for `%s')", ofwhat);
4643 /* Digest the parser output INIT as an initializer for type TYPE.
4644 Return a C expression of type TYPE to represent the initial value.
4646 The arguments REQUIRE_CONSTANT and CONSTRUCTOR_CONSTANT request errors
4647 if non-constant initializers or elements are seen. CONSTRUCTOR_CONSTANT
4648 applies only to elements of constructors. */
4651 digest_init (type, init, require_constant, constructor_constant)
4653 int require_constant, constructor_constant;
4655 enum tree_code code = TREE_CODE (type);
4656 tree inside_init = init;
4658 if (type == error_mark_node
4659 || init == error_mark_node
4660 || TREE_TYPE (init) == error_mark_node)
4661 return error_mark_node;
4663 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4664 /* Do not use STRIP_NOPS here. We do not want an enumerator
4665 whose value is 0 to count as a null pointer constant. */
4666 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4667 inside_init = TREE_OPERAND (init, 0);
4669 inside_init = fold (inside_init);
4671 /* Initialization of an array of chars from a string constant
4672 optionally enclosed in braces. */
4674 if (code == ARRAY_TYPE)
4676 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4677 if ((typ1 == char_type_node
4678 || typ1 == signed_char_type_node
4679 || typ1 == unsigned_char_type_node
4680 || typ1 == unsigned_wchar_type_node
4681 || typ1 == signed_wchar_type_node)
4682 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
4684 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4685 TYPE_MAIN_VARIANT (type)))
4688 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4690 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4692 error_init ("char-array initialized from wide string");
4693 return error_mark_node;
4695 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4697 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4699 error_init ("int-array initialized from non-wide string");
4700 return error_mark_node;
4703 TREE_TYPE (inside_init) = type;
4704 if (TYPE_DOMAIN (type) != 0
4705 && TYPE_SIZE (type) != 0
4706 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4707 /* Subtract 1 (or sizeof (wchar_t))
4708 because it's ok to ignore the terminating null char
4709 that is counted in the length of the constant. */
4710 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4711 TREE_STRING_LENGTH (inside_init)
4712 - ((TYPE_PRECISION (typ1)
4713 != TYPE_PRECISION (char_type_node))
4714 ? (TYPE_PRECISION (wchar_type_node)
4717 pedwarn_init ("initializer-string for array of chars is too long");
4723 /* Any type can be initialized
4724 from an expression of the same type, optionally with braces. */
4726 if (inside_init && TREE_TYPE (inside_init) != 0
4727 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4728 TYPE_MAIN_VARIANT (type))
4729 || (code == ARRAY_TYPE
4730 && comptypes (TREE_TYPE (inside_init), type))
4731 || (code == POINTER_TYPE
4732 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4733 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4734 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4735 TREE_TYPE (type)))))
4737 if (code == POINTER_TYPE
4738 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4739 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE))
4740 inside_init = default_conversion (inside_init);
4741 else if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4742 && TREE_CODE (inside_init) != CONSTRUCTOR)
4744 error_init ("array initialized from non-constant array expression");
4745 return error_mark_node;
4748 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4749 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4751 /* Compound expressions can only occur here if -pedantic or
4752 -pedantic-errors is specified. In the later case, we always want
4753 an error. In the former case, we simply want a warning. */
4754 if (require_constant && pedantic
4755 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4758 = valid_compound_expr_initializer (inside_init,
4759 TREE_TYPE (inside_init));
4760 if (inside_init == error_mark_node)
4761 error_init ("initializer element is not constant");
4763 pedwarn_init ("initializer element is not constant");
4764 if (flag_pedantic_errors)
4765 inside_init = error_mark_node;
4767 else if (require_constant
4768 && (!TREE_CONSTANT (inside_init)
4769 /* This test catches things like `7 / 0' which
4770 result in an expression for which TREE_CONSTANT
4771 is true, but which is not actually something
4772 that is a legal constant. We really should not
4773 be using this function, because it is a part of
4774 the back-end. Instead, the expression should
4775 already have been turned into ERROR_MARK_NODE. */
4776 || !initializer_constant_valid_p (inside_init,
4777 TREE_TYPE (inside_init))))
4779 error_init ("initializer element is not constant");
4780 inside_init = error_mark_node;
4786 /* Handle scalar types, including conversions. */
4788 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4789 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4791 /* Note that convert_for_assignment calls default_conversion
4792 for arrays and functions. We must not call it in the
4793 case where inside_init is a null pointer constant. */
4795 = convert_for_assignment (type, init, _("initialization"),
4796 NULL_TREE, NULL_TREE, 0);
4798 if (require_constant && ! TREE_CONSTANT (inside_init))
4800 error_init ("initializer element is not constant");
4801 inside_init = error_mark_node;
4803 else if (require_constant
4804 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4806 error_init ("initializer element is not computable at load time");
4807 inside_init = error_mark_node;
4813 /* Come here only for records and arrays. */
4815 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4817 error_init ("variable-sized object may not be initialized");
4818 return error_mark_node;
4821 /* Traditionally, you can write struct foo x = 0;
4822 and it initializes the first element of x to 0. */
4823 if (flag_traditional)
4825 tree top = 0, prev = 0, otype = type;
4826 while (TREE_CODE (type) == RECORD_TYPE
4827 || TREE_CODE (type) == ARRAY_TYPE
4828 || TREE_CODE (type) == QUAL_UNION_TYPE
4829 || TREE_CODE (type) == UNION_TYPE)
4831 tree temp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
4835 TREE_OPERAND (prev, 1) = build_tree_list (NULL_TREE, temp);
4837 if (TREE_CODE (type) == ARRAY_TYPE)
4838 type = TREE_TYPE (type);
4839 else if (TYPE_FIELDS (type))
4840 type = TREE_TYPE (TYPE_FIELDS (type));
4843 error_init ("invalid initializer");
4844 return error_mark_node;
4850 TREE_OPERAND (prev, 1)
4851 = build_tree_list (NULL_TREE,
4852 digest_init (type, init, require_constant,
4853 constructor_constant));
4857 return error_mark_node;
4859 error_init ("invalid initializer");
4860 return error_mark_node;
4863 /* Handle initializers that use braces. */
4865 /* Type of object we are accumulating a constructor for.
4866 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4867 static tree constructor_type;
4869 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4871 static tree constructor_fields;
4873 /* For an ARRAY_TYPE, this is the specified index
4874 at which to store the next element we get. */
4875 static tree constructor_index;
4877 /* For an ARRAY_TYPE, this is the maximum index. */
4878 static tree constructor_max_index;
4880 /* For a RECORD_TYPE, this is the first field not yet written out. */
4881 static tree constructor_unfilled_fields;
4883 /* For an ARRAY_TYPE, this is the index of the first element
4884 not yet written out. */
4885 static tree constructor_unfilled_index;
4887 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4888 This is so we can generate gaps between fields, when appropriate. */
4889 static tree constructor_bit_index;
4891 /* If we are saving up the elements rather than allocating them,
4892 this is the list of elements so far (in reverse order,
4893 most recent first). */
4894 static tree constructor_elements;
4896 /* 1 if constructor should be incrementally stored into a constructor chain,
4897 0 if all the elements should be kept in AVL tree. */
4898 static int constructor_incremental;
4900 /* 1 if so far this constructor's elements are all compile-time constants. */
4901 static int constructor_constant;
4903 /* 1 if so far this constructor's elements are all valid address constants. */
4904 static int constructor_simple;
4906 /* 1 if this constructor is erroneous so far. */
4907 static int constructor_erroneous;
4909 /* 1 if have called defer_addressed_constants. */
4910 static int constructor_subconstants_deferred;
4912 /* Structure for managing pending initializer elements, organized as an
4917 struct init_node *left, *right;
4918 struct init_node *parent;
4924 /* Tree of pending elements at this constructor level.
4925 These are elements encountered out of order
4926 which belong at places we haven't reached yet in actually
4928 Will never hold tree nodes across GC runs. */
4929 static struct init_node *constructor_pending_elts;
4931 /* The SPELLING_DEPTH of this constructor. */
4932 static int constructor_depth;
4934 /* 0 if implicitly pushing constructor levels is allowed. */
4935 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4937 static int require_constant_value;
4938 static int require_constant_elements;
4940 /* DECL node for which an initializer is being read.
4941 0 means we are reading a constructor expression
4942 such as (struct foo) {...}. */
4943 static tree constructor_decl;
4945 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4946 static const char *constructor_asmspec;
4948 /* Nonzero if this is an initializer for a top-level decl. */
4949 static int constructor_top_level;
4951 /* Nonzero if there were any member designators in this initializer. */
4952 static int constructor_designated;
4954 /* Nesting depth of designator list. */
4955 static int designator_depth;
4957 /* Nonzero if there were diagnosed errors in this designator list. */
4958 static int designator_errorneous;
4961 /* This stack has a level for each implicit or explicit level of
4962 structuring in the initializer, including the outermost one. It
4963 saves the values of most of the variables above. */
4965 struct constructor_range_stack;
4967 struct constructor_stack
4969 struct constructor_stack *next;
4974 tree unfilled_index;
4975 tree unfilled_fields;
4978 struct init_node *pending_elts;
4981 /* If nonzero, this value should replace the entire
4982 constructor at this level. */
4983 tree replacement_value;
4984 struct constructor_range_stack *range_stack;
4994 struct constructor_stack *constructor_stack;
4996 /* This stack represents designators from some range designator up to
4997 the last designator in the list. */
4999 struct constructor_range_stack
5001 struct constructor_range_stack *next, *prev;
5002 struct constructor_stack *stack;
5009 struct constructor_range_stack *constructor_range_stack;
5011 /* This stack records separate initializers that are nested.
5012 Nested initializers can't happen in ANSI C, but GNU C allows them
5013 in cases like { ... (struct foo) { ... } ... }. */
5015 struct initializer_stack
5017 struct initializer_stack *next;
5019 const char *asmspec;
5020 struct constructor_stack *constructor_stack;
5021 struct constructor_range_stack *constructor_range_stack;
5023 struct spelling *spelling;
5024 struct spelling *spelling_base;
5027 char require_constant_value;
5028 char require_constant_elements;
5032 struct initializer_stack *initializer_stack;
5034 /* Prepare to parse and output the initializer for variable DECL. */
5037 start_init (decl, asmspec_tree, top_level)
5043 struct initializer_stack *p
5044 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
5045 const char *asmspec = 0;
5048 asmspec = TREE_STRING_POINTER (asmspec_tree);
5050 p->decl = constructor_decl;
5051 p->asmspec = constructor_asmspec;
5052 p->require_constant_value = require_constant_value;
5053 p->require_constant_elements = require_constant_elements;
5054 p->constructor_stack = constructor_stack;
5055 p->constructor_range_stack = constructor_range_stack;
5056 p->elements = constructor_elements;
5057 p->spelling = spelling;
5058 p->spelling_base = spelling_base;
5059 p->spelling_size = spelling_size;
5060 p->deferred = constructor_subconstants_deferred;
5061 p->top_level = constructor_top_level;
5062 p->next = initializer_stack;
5063 initializer_stack = p;
5065 constructor_decl = decl;
5066 constructor_asmspec = asmspec;
5067 constructor_subconstants_deferred = 0;
5068 constructor_designated = 0;
5069 constructor_top_level = top_level;
5073 require_constant_value = TREE_STATIC (decl);
5074 require_constant_elements
5075 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
5076 /* For a scalar, you can always use any value to initialize,
5077 even within braces. */
5078 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5079 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5080 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5081 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
5082 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
5086 require_constant_value = 0;
5087 require_constant_elements = 0;
5088 locus = "(anonymous)";
5091 constructor_stack = 0;
5092 constructor_range_stack = 0;
5094 missing_braces_mentioned = 0;
5098 RESTORE_SPELLING_DEPTH (0);
5101 push_string (locus);
5107 struct initializer_stack *p = initializer_stack;
5109 /* Output subconstants (string constants, usually)
5110 that were referenced within this initializer and saved up.
5111 Must do this if and only if we called defer_addressed_constants. */
5112 if (constructor_subconstants_deferred)
5113 output_deferred_addressed_constants ();
5115 /* Free the whole constructor stack of this initializer. */
5116 while (constructor_stack)
5118 struct constructor_stack *q = constructor_stack;
5119 constructor_stack = q->next;
5123 if (constructor_range_stack)
5126 /* Pop back to the data of the outer initializer (if any). */
5127 constructor_decl = p->decl;
5128 constructor_asmspec = p->asmspec;
5129 require_constant_value = p->require_constant_value;
5130 require_constant_elements = p->require_constant_elements;
5131 constructor_stack = p->constructor_stack;
5132 constructor_range_stack = p->constructor_range_stack;
5133 constructor_elements = p->elements;
5134 spelling = p->spelling;
5135 spelling_base = p->spelling_base;
5136 spelling_size = p->spelling_size;
5137 constructor_subconstants_deferred = p->deferred;
5138 constructor_top_level = p->top_level;
5139 initializer_stack = p->next;
5143 /* Call here when we see the initializer is surrounded by braces.
5144 This is instead of a call to push_init_level;
5145 it is matched by a call to pop_init_level.
5147 TYPE is the type to initialize, for a constructor expression.
5148 For an initializer for a decl, TYPE is zero. */
5151 really_start_incremental_init (type)
5154 struct constructor_stack *p
5155 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5158 type = TREE_TYPE (constructor_decl);
5160 p->type = constructor_type;
5161 p->fields = constructor_fields;
5162 p->index = constructor_index;
5163 p->max_index = constructor_max_index;
5164 p->unfilled_index = constructor_unfilled_index;
5165 p->unfilled_fields = constructor_unfilled_fields;
5166 p->bit_index = constructor_bit_index;
5167 p->elements = constructor_elements;
5168 p->constant = constructor_constant;
5169 p->simple = constructor_simple;
5170 p->erroneous = constructor_erroneous;
5171 p->pending_elts = constructor_pending_elts;
5172 p->depth = constructor_depth;
5173 p->replacement_value = 0;
5177 p->incremental = constructor_incremental;
5178 p->designated = constructor_designated;
5180 constructor_stack = p;
5182 constructor_constant = 1;
5183 constructor_simple = 1;
5184 constructor_depth = SPELLING_DEPTH ();
5185 constructor_elements = 0;
5186 constructor_pending_elts = 0;
5187 constructor_type = type;
5188 constructor_incremental = 1;
5189 constructor_designated = 0;
5190 designator_depth = 0;
5191 designator_errorneous = 0;
5193 if (TREE_CODE (constructor_type) == RECORD_TYPE
5194 || TREE_CODE (constructor_type) == UNION_TYPE)
5196 constructor_fields = TYPE_FIELDS (constructor_type);
5197 /* Skip any nameless bit fields at the beginning. */
5198 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5199 && DECL_NAME (constructor_fields) == 0)
5200 constructor_fields = TREE_CHAIN (constructor_fields);
5202 constructor_unfilled_fields = constructor_fields;
5203 constructor_bit_index = bitsize_zero_node;
5205 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5207 if (TYPE_DOMAIN (constructor_type))
5209 constructor_max_index
5210 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5212 /* Detect non-empty initializations of zero-length arrays. */
5213 if (constructor_max_index == NULL_TREE
5214 && TYPE_SIZE (constructor_type))
5215 constructor_max_index = build_int_2 (-1, -1);
5218 = convert (bitsizetype,
5219 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5222 constructor_index = bitsize_zero_node;
5224 constructor_unfilled_index = constructor_index;
5228 /* Handle the case of int x = {5}; */
5229 constructor_fields = constructor_type;
5230 constructor_unfilled_fields = constructor_type;
5234 /* Push down into a subobject, for initialization.
5235 If this is for an explicit set of braces, IMPLICIT is 0.
5236 If it is because the next element belongs at a lower level,
5237 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5240 push_init_level (implicit)
5243 struct constructor_stack *p;
5244 tree value = NULL_TREE;
5246 /* If we've exhausted any levels that didn't have braces,
5248 while (constructor_stack->implicit)
5250 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5251 || TREE_CODE (constructor_type) == UNION_TYPE)
5252 && constructor_fields == 0)
5253 process_init_element (pop_init_level (1));
5254 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5255 && tree_int_cst_lt (constructor_max_index, constructor_index))
5256 process_init_element (pop_init_level (1));
5261 /* Unless this is an explicit brace, we need to preserve previous
5265 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5266 || TREE_CODE (constructor_type) == UNION_TYPE)
5267 && constructor_fields)
5268 value = find_init_member (constructor_fields);
5269 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5270 value = find_init_member (constructor_index);
5273 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5274 p->type = constructor_type;
5275 p->fields = constructor_fields;
5276 p->index = constructor_index;
5277 p->max_index = constructor_max_index;
5278 p->unfilled_index = constructor_unfilled_index;
5279 p->unfilled_fields = constructor_unfilled_fields;
5280 p->bit_index = constructor_bit_index;
5281 p->elements = constructor_elements;
5282 p->constant = constructor_constant;
5283 p->simple = constructor_simple;
5284 p->erroneous = constructor_erroneous;
5285 p->pending_elts = constructor_pending_elts;
5286 p->depth = constructor_depth;
5287 p->replacement_value = 0;
5288 p->implicit = implicit;
5290 p->incremental = constructor_incremental;
5291 p->designated = constructor_designated;
5292 p->next = constructor_stack;
5294 constructor_stack = p;
5296 constructor_constant = 1;
5297 constructor_simple = 1;
5298 constructor_depth = SPELLING_DEPTH ();
5299 constructor_elements = 0;
5300 constructor_incremental = 1;
5301 constructor_designated = 0;
5302 constructor_pending_elts = 0;
5305 p->range_stack = constructor_range_stack;
5306 constructor_range_stack = 0;
5307 designator_depth = 0;
5308 designator_errorneous = 0;
5311 /* Don't die if an entire brace-pair level is superfluous
5312 in the containing level. */
5313 if (constructor_type == 0)
5315 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5316 || TREE_CODE (constructor_type) == UNION_TYPE)
5318 /* Don't die if there are extra init elts at the end. */
5319 if (constructor_fields == 0)
5320 constructor_type = 0;
5323 constructor_type = TREE_TYPE (constructor_fields);
5324 push_member_name (constructor_fields);
5325 constructor_depth++;
5328 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5330 constructor_type = TREE_TYPE (constructor_type);
5331 push_array_bounds (tree_low_cst (constructor_index, 0));
5332 constructor_depth++;
5335 if (constructor_type == 0)
5337 error_init ("extra brace group at end of initializer");
5338 constructor_fields = 0;
5339 constructor_unfilled_fields = 0;
5343 if (value && TREE_CODE (value) == CONSTRUCTOR)
5345 constructor_constant = TREE_CONSTANT (value);
5346 constructor_simple = TREE_STATIC (value);
5347 constructor_elements = TREE_OPERAND (value, 1);
5348 if (constructor_elements
5349 && (TREE_CODE (constructor_type) == RECORD_TYPE
5350 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5351 set_nonincremental_init ();
5354 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5356 missing_braces_mentioned = 1;
5357 warning_init ("missing braces around initializer");
5360 if (TREE_CODE (constructor_type) == RECORD_TYPE
5361 || TREE_CODE (constructor_type) == UNION_TYPE)
5363 constructor_fields = TYPE_FIELDS (constructor_type);
5364 /* Skip any nameless bit fields at the beginning. */
5365 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5366 && DECL_NAME (constructor_fields) == 0)
5367 constructor_fields = TREE_CHAIN (constructor_fields);
5369 constructor_unfilled_fields = constructor_fields;
5370 constructor_bit_index = bitsize_zero_node;
5372 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5374 if (TYPE_DOMAIN (constructor_type))
5376 constructor_max_index
5377 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5379 /* Detect non-empty initializations of zero-length arrays. */
5380 if (constructor_max_index == NULL_TREE
5381 && TYPE_SIZE (constructor_type))
5382 constructor_max_index = build_int_2 (-1, -1);
5385 = convert (bitsizetype,
5386 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5389 constructor_index = bitsize_zero_node;
5391 constructor_unfilled_index = constructor_index;
5392 if (value && TREE_CODE (value) == STRING_CST)
5394 /* We need to split the char/wchar array into individual
5395 characters, so that we don't have to special case it
5397 set_nonincremental_init_from_string (value);
5402 warning_init ("braces around scalar initializer");
5403 constructor_fields = constructor_type;
5404 constructor_unfilled_fields = constructor_type;
5408 /* At the end of an implicit or explicit brace level,
5409 finish up that level of constructor.
5410 If we were outputting the elements as they are read, return 0
5411 from inner levels (process_init_element ignores that),
5412 but return error_mark_node from the outermost level
5413 (that's what we want to put in DECL_INITIAL).
5414 Otherwise, return a CONSTRUCTOR expression. */
5417 pop_init_level (implicit)
5420 struct constructor_stack *p;
5421 HOST_WIDE_INT size = 0;
5422 tree constructor = 0;
5426 /* When we come to an explicit close brace,
5427 pop any inner levels that didn't have explicit braces. */
5428 while (constructor_stack->implicit)
5429 process_init_element (pop_init_level (1));
5431 if (constructor_range_stack)
5435 p = constructor_stack;
5437 if (constructor_type != 0)
5438 size = int_size_in_bytes (constructor_type);
5440 /* Error for initializing a flexible array member, or a zero-length
5441 array member in an inappropriate context. */
5442 if (constructor_type && constructor_fields
5443 && TREE_CODE (constructor_type) == ARRAY_TYPE
5444 && TYPE_DOMAIN (constructor_type)
5445 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5447 /* Silently discard empty initializations. The parser will
5448 already have pedwarned for empty brackets. */
5449 if (integer_zerop (constructor_unfilled_index))
5450 constructor_type = NULL_TREE;
5451 else if (! TYPE_SIZE (constructor_type))
5453 if (constructor_depth > 2)
5454 error_init ("initialization of flexible array member in a nested context");
5456 pedwarn_init ("initialization of a flexible array member");
5458 /* We have already issued an error message for the existence
5459 of a flexible array member not at the end of the structure.
5460 Discard the initializer so that we do not abort later. */
5461 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5462 constructor_type = NULL_TREE;
5465 /* Zero-length arrays are no longer special, so we should no longer
5470 /* Warn when some struct elements are implicitly initialized to zero. */
5473 && TREE_CODE (constructor_type) == RECORD_TYPE
5474 && constructor_unfilled_fields)
5476 /* Do not warn for flexible array members or zero-length arrays. */
5477 while (constructor_unfilled_fields
5478 && (! DECL_SIZE (constructor_unfilled_fields)
5479 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5480 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5482 /* Do not warn if this level of the initializer uses member
5483 designators; it is likely to be deliberate. */
5484 if (constructor_unfilled_fields && !constructor_designated)
5486 push_member_name (constructor_unfilled_fields);
5487 warning_init ("missing initializer");
5488 RESTORE_SPELLING_DEPTH (constructor_depth);
5492 /* Now output all pending elements. */
5493 constructor_incremental = 1;
5494 output_pending_init_elements (1);
5496 /* Pad out the end of the structure. */
5497 if (p->replacement_value)
5498 /* If this closes a superfluous brace pair,
5499 just pass out the element between them. */
5500 constructor = p->replacement_value;
5501 else if (constructor_type == 0)
5503 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5504 && TREE_CODE (constructor_type) != UNION_TYPE
5505 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5507 /* A nonincremental scalar initializer--just return
5508 the element, after verifying there is just one. */
5509 if (constructor_elements == 0)
5511 if (!constructor_erroneous)
5512 error_init ("empty scalar initializer");
5513 constructor = error_mark_node;
5515 else if (TREE_CHAIN (constructor_elements) != 0)
5517 error_init ("extra elements in scalar initializer");
5518 constructor = TREE_VALUE (constructor_elements);
5521 constructor = TREE_VALUE (constructor_elements);
5525 if (constructor_erroneous)
5526 constructor = error_mark_node;
5529 constructor = build (CONSTRUCTOR, constructor_type, NULL_TREE,
5530 nreverse (constructor_elements));
5531 if (constructor_constant)
5532 TREE_CONSTANT (constructor) = 1;
5533 if (constructor_constant && constructor_simple)
5534 TREE_STATIC (constructor) = 1;
5538 constructor_type = p->type;
5539 constructor_fields = p->fields;
5540 constructor_index = p->index;
5541 constructor_max_index = p->max_index;
5542 constructor_unfilled_index = p->unfilled_index;
5543 constructor_unfilled_fields = p->unfilled_fields;
5544 constructor_bit_index = p->bit_index;
5545 constructor_elements = p->elements;
5546 constructor_constant = p->constant;
5547 constructor_simple = p->simple;
5548 constructor_erroneous = p->erroneous;
5549 constructor_incremental = p->incremental;
5550 constructor_designated = p->designated;
5551 constructor_pending_elts = p->pending_elts;
5552 constructor_depth = p->depth;
5554 constructor_range_stack = p->range_stack;
5555 RESTORE_SPELLING_DEPTH (constructor_depth);
5557 constructor_stack = p->next;
5560 if (constructor == 0)
5562 if (constructor_stack == 0)
5563 return error_mark_node;
5569 /* Common handling for both array range and field name designators.
5570 ARRAY argument is non-zero for array ranges. Returns zero for success. */
5573 set_designator (array)
5577 enum tree_code subcode;
5579 /* Don't die if an entire brace-pair level is superfluous
5580 in the containing level. */
5581 if (constructor_type == 0)
5584 /* If there were errors in this designator list already, bail out silently. */
5585 if (designator_errorneous)
5588 if (!designator_depth)
5590 if (constructor_range_stack)
5593 /* Designator list starts at the level of closest explicit
5595 while (constructor_stack->implicit)
5596 process_init_element (pop_init_level (1));
5597 constructor_designated = 1;
5601 if (constructor_no_implicit)
5603 error_init ("initialization designators may not nest");
5607 if (TREE_CODE (constructor_type) == RECORD_TYPE
5608 || TREE_CODE (constructor_type) == UNION_TYPE)
5610 subtype = TREE_TYPE (constructor_fields);
5611 if (subtype != error_mark_node)
5612 subtype = TYPE_MAIN_VARIANT (subtype);
5614 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5616 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5621 subcode = TREE_CODE (subtype);
5622 if (array && subcode != ARRAY_TYPE)
5624 error_init ("array index in non-array initializer");
5627 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5629 error_init ("field name not in record or union initializer");
5633 constructor_designated = 1;
5634 push_init_level (2);
5638 /* If there are range designators in designator list, push a new designator
5639 to constructor_range_stack. RANGE_END is end of such stack range or
5640 NULL_TREE if there is no range designator at this level. */
5643 push_range_stack (range_end)
5646 struct constructor_range_stack *p;
5648 p = (struct constructor_range_stack *)
5649 ggc_alloc (sizeof (struct constructor_range_stack));
5650 p->prev = constructor_range_stack;
5652 p->fields = constructor_fields;
5653 p->range_start = constructor_index;
5654 p->index = constructor_index;
5655 p->stack = constructor_stack;
5656 p->range_end = range_end;
5657 if (constructor_range_stack)
5658 constructor_range_stack->next = p;
5659 constructor_range_stack = p;
5662 /* Within an array initializer, specify the next index to be initialized.
5663 FIRST is that index. If LAST is nonzero, then initialize a range
5664 of indices, running from FIRST through LAST. */
5667 set_init_index (first, last)
5670 if (set_designator (1))
5673 designator_errorneous = 1;
5675 while ((TREE_CODE (first) == NOP_EXPR
5676 || TREE_CODE (first) == CONVERT_EXPR
5677 || TREE_CODE (first) == NON_LVALUE_EXPR)
5678 && (TYPE_MODE (TREE_TYPE (first))
5679 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5680 first = TREE_OPERAND (first, 0);
5683 while ((TREE_CODE (last) == NOP_EXPR
5684 || TREE_CODE (last) == CONVERT_EXPR
5685 || TREE_CODE (last) == NON_LVALUE_EXPR)
5686 && (TYPE_MODE (TREE_TYPE (last))
5687 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5688 last = TREE_OPERAND (last, 0);
5690 if (TREE_CODE (first) != INTEGER_CST)
5691 error_init ("nonconstant array index in initializer");
5692 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5693 error_init ("nonconstant array index in initializer");
5694 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5695 error_init ("array index in non-array initializer");
5696 else if (constructor_max_index
5697 && tree_int_cst_lt (constructor_max_index, first))
5698 error_init ("array index in initializer exceeds array bounds");
5701 constructor_index = convert (bitsizetype, first);
5705 if (tree_int_cst_equal (first, last))
5707 else if (tree_int_cst_lt (last, first))
5709 error_init ("empty index range in initializer");
5714 last = convert (bitsizetype, last);
5715 if (constructor_max_index != 0
5716 && tree_int_cst_lt (constructor_max_index, last))
5718 error_init ("array index range in initializer exceeds array bounds");
5725 designator_errorneous = 0;
5726 if (constructor_range_stack || last)
5727 push_range_stack (last);
5731 /* Within a struct initializer, specify the next field to be initialized. */
5734 set_init_label (fieldname)
5739 if (set_designator (0))
5742 designator_errorneous = 1;
5744 if (TREE_CODE (constructor_type) != RECORD_TYPE
5745 && TREE_CODE (constructor_type) != UNION_TYPE)
5747 error_init ("field name not in record or union initializer");
5751 for (tail = TYPE_FIELDS (constructor_type); tail;
5752 tail = TREE_CHAIN (tail))
5754 if (DECL_NAME (tail) == fieldname)
5759 error ("unknown field `%s' specified in initializer",
5760 IDENTIFIER_POINTER (fieldname));
5763 constructor_fields = tail;
5765 designator_errorneous = 0;
5766 if (constructor_range_stack)
5767 push_range_stack (NULL_TREE);
5771 /* Add a new initializer to the tree of pending initializers. PURPOSE
5772 identifies the initializer, either array index or field in a structure.
5773 VALUE is the value of that index or field. */
5776 add_pending_init (purpose, value)
5777 tree purpose, value;
5779 struct init_node *p, **q, *r;
5781 q = &constructor_pending_elts;
5784 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5789 if (tree_int_cst_lt (purpose, p->purpose))
5791 else if (tree_int_cst_lt (p->purpose, purpose))
5795 if (TREE_SIDE_EFFECTS (p->value))
5796 warning_init ("initialized field with side-effects overwritten");
5806 bitpos = bit_position (purpose);
5810 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5812 else if (p->purpose != purpose)
5816 if (TREE_SIDE_EFFECTS (p->value))
5817 warning_init ("initialized field with side-effects overwritten");
5824 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
5825 r->purpose = purpose;
5836 struct init_node *s;
5840 if (p->balance == 0)
5842 else if (p->balance < 0)
5849 p->left->parent = p;
5866 constructor_pending_elts = r;
5871 struct init_node *t = r->right;
5875 r->right->parent = r;
5880 p->left->parent = p;
5883 p->balance = t->balance < 0;
5884 r->balance = -(t->balance > 0);
5899 constructor_pending_elts = t;
5905 /* p->balance == +1; growth of left side balances the node. */
5910 else /* r == p->right */
5912 if (p->balance == 0)
5913 /* Growth propagation from right side. */
5915 else if (p->balance > 0)
5922 p->right->parent = p;
5939 constructor_pending_elts = r;
5941 else /* r->balance == -1 */
5944 struct init_node *t = r->left;
5948 r->left->parent = r;
5953 p->right->parent = p;
5956 r->balance = (t->balance < 0);
5957 p->balance = -(t->balance > 0);
5972 constructor_pending_elts = t;
5978 /* p->balance == -1; growth of right side balances the node. */
5989 /* Build AVL tree from a sorted chain. */
5992 set_nonincremental_init ()
5996 if (TREE_CODE (constructor_type) != RECORD_TYPE
5997 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6000 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6001 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6002 constructor_elements = 0;
6003 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6005 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6006 /* Skip any nameless bit fields at the beginning. */
6007 while (constructor_unfilled_fields != 0
6008 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6009 && DECL_NAME (constructor_unfilled_fields) == 0)
6010 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6013 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6015 if (TYPE_DOMAIN (constructor_type))
6016 constructor_unfilled_index
6017 = convert (bitsizetype,
6018 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6020 constructor_unfilled_index = bitsize_zero_node;
6022 constructor_incremental = 0;
6025 /* Build AVL tree from a string constant. */
6028 set_nonincremental_init_from_string (str)
6031 tree value, purpose, type;
6032 HOST_WIDE_INT val[2];
6033 const char *p, *end;
6034 int byte, wchar_bytes, charwidth, bitpos;
6036 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6039 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6040 == TYPE_PRECISION (char_type_node))
6042 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6043 == TYPE_PRECISION (wchar_type_node))
6044 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6048 charwidth = TYPE_PRECISION (char_type_node);
6049 type = TREE_TYPE (constructor_type);
6050 p = TREE_STRING_POINTER (str);
6051 end = p + TREE_STRING_LENGTH (str);
6053 for (purpose = bitsize_zero_node;
6054 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6055 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6057 if (wchar_bytes == 1)
6059 val[1] = (unsigned char) *p++;
6066 for (byte = 0; byte < wchar_bytes; byte++)
6068 if (BYTES_BIG_ENDIAN)
6069 bitpos = (wchar_bytes - byte - 1) * charwidth;
6071 bitpos = byte * charwidth;
6072 val[bitpos < HOST_BITS_PER_WIDE_INT]
6073 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6074 << (bitpos % HOST_BITS_PER_WIDE_INT);
6078 if (!TREE_UNSIGNED (type))
6080 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6081 if (bitpos < HOST_BITS_PER_WIDE_INT)
6083 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6085 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6089 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6094 else if (val[0] & (((HOST_WIDE_INT) 1)
6095 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6096 val[0] |= ((HOST_WIDE_INT) -1)
6097 << (bitpos - HOST_BITS_PER_WIDE_INT);
6100 value = build_int_2 (val[1], val[0]);
6101 TREE_TYPE (value) = type;
6102 add_pending_init (purpose, value);
6105 constructor_incremental = 0;
6108 /* Return value of FIELD in pending initializer or zero if the field was
6109 not initialized yet. */
6112 find_init_member (field)
6115 struct init_node *p;
6117 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6119 if (constructor_incremental
6120 && tree_int_cst_lt (field, constructor_unfilled_index))
6121 set_nonincremental_init ();
6123 p = constructor_pending_elts;
6126 if (tree_int_cst_lt (field, p->purpose))
6128 else if (tree_int_cst_lt (p->purpose, field))
6134 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6136 tree bitpos = bit_position (field);
6138 if (constructor_incremental
6139 && (!constructor_unfilled_fields
6140 || tree_int_cst_lt (bitpos,
6141 bit_position (constructor_unfilled_fields))))
6142 set_nonincremental_init ();
6144 p = constructor_pending_elts;
6147 if (field == p->purpose)
6149 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6155 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6157 if (constructor_elements
6158 && TREE_PURPOSE (constructor_elements) == field)
6159 return TREE_VALUE (constructor_elements);
6164 /* "Output" the next constructor element.
6165 At top level, really output it to assembler code now.
6166 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6167 TYPE is the data type that the containing data type wants here.
6168 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6170 PENDING if non-nil means output pending elements that belong
6171 right after this element. (PENDING is normally 1;
6172 it is 0 while outputting pending elements, to avoid recursion.) */
6175 output_init_element (value, type, field, pending)
6176 tree value, type, field;
6179 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6180 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
6181 && !(TREE_CODE (value) == STRING_CST
6182 && TREE_CODE (type) == ARRAY_TYPE
6183 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
6184 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
6185 TYPE_MAIN_VARIANT (type))))
6186 value = default_conversion (value);
6188 if (value == error_mark_node)
6189 constructor_erroneous = 1;
6190 else if (!TREE_CONSTANT (value))
6191 constructor_constant = 0;
6192 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6193 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6194 || TREE_CODE (constructor_type) == UNION_TYPE)
6195 && DECL_C_BIT_FIELD (field)
6196 && TREE_CODE (value) != INTEGER_CST))
6197 constructor_simple = 0;
6199 if (require_constant_value && ! TREE_CONSTANT (value))
6201 error_init ("initializer element is not constant");
6202 value = error_mark_node;
6204 else if (require_constant_elements
6205 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
6206 pedwarn ("initializer element is not computable at load time");
6208 /* If this field is empty (and not at the end of structure),
6209 don't do anything other than checking the initializer. */
6211 && (TREE_TYPE (field) == error_mark_node
6212 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6213 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6214 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6215 || TREE_CHAIN (field)))))
6218 if (value == error_mark_node)
6220 constructor_erroneous = 1;
6224 /* If this element doesn't come next in sequence,
6225 put it on constructor_pending_elts. */
6226 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6227 && (!constructor_incremental
6228 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6230 if (constructor_incremental
6231 && tree_int_cst_lt (field, constructor_unfilled_index))
6232 set_nonincremental_init ();
6234 add_pending_init (field,
6235 digest_init (type, value, require_constant_value,
6236 require_constant_elements));
6239 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6240 && (!constructor_incremental
6241 || field != constructor_unfilled_fields))
6243 /* We do this for records but not for unions. In a union,
6244 no matter which field is specified, it can be initialized
6245 right away since it starts at the beginning of the union. */
6246 if (constructor_incremental)
6248 if (!constructor_unfilled_fields)
6249 set_nonincremental_init ();
6252 tree bitpos, unfillpos;
6254 bitpos = bit_position (field);
6255 unfillpos = bit_position (constructor_unfilled_fields);
6257 if (tree_int_cst_lt (bitpos, unfillpos))
6258 set_nonincremental_init ();
6262 add_pending_init (field,
6263 digest_init (type, value, require_constant_value,
6264 require_constant_elements));
6267 else if (TREE_CODE (constructor_type) == UNION_TYPE
6268 && constructor_elements)
6270 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6271 warning_init ("initialized field with side-effects overwritten");
6273 /* We can have just one union field set. */
6274 constructor_elements = 0;
6277 /* Otherwise, output this element either to
6278 constructor_elements or to the assembler file. */
6280 if (field && TREE_CODE (field) == INTEGER_CST)
6281 field = copy_node (field);
6282 constructor_elements
6283 = tree_cons (field, digest_init (type, value,
6284 require_constant_value,
6285 require_constant_elements),
6286 constructor_elements);
6288 /* Advance the variable that indicates sequential elements output. */
6289 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6290 constructor_unfilled_index
6291 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6293 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6295 constructor_unfilled_fields
6296 = TREE_CHAIN (constructor_unfilled_fields);
6298 /* Skip any nameless bit fields. */
6299 while (constructor_unfilled_fields != 0
6300 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6301 && DECL_NAME (constructor_unfilled_fields) == 0)
6302 constructor_unfilled_fields =
6303 TREE_CHAIN (constructor_unfilled_fields);
6305 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6306 constructor_unfilled_fields = 0;
6308 /* Now output any pending elements which have become next. */
6310 output_pending_init_elements (0);
6313 /* Output any pending elements which have become next.
6314 As we output elements, constructor_unfilled_{fields,index}
6315 advances, which may cause other elements to become next;
6316 if so, they too are output.
6318 If ALL is 0, we return when there are
6319 no more pending elements to output now.
6321 If ALL is 1, we output space as necessary so that
6322 we can output all the pending elements. */
6325 output_pending_init_elements (all)
6328 struct init_node *elt = constructor_pending_elts;
6333 /* Look thru the whole pending tree.
6334 If we find an element that should be output now,
6335 output it. Otherwise, set NEXT to the element
6336 that comes first among those still pending. */
6341 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6343 if (tree_int_cst_equal (elt->purpose,
6344 constructor_unfilled_index))
6345 output_init_element (elt->value,
6346 TREE_TYPE (constructor_type),
6347 constructor_unfilled_index, 0);
6348 else if (tree_int_cst_lt (constructor_unfilled_index,
6351 /* Advance to the next smaller node. */
6356 /* We have reached the smallest node bigger than the
6357 current unfilled index. Fill the space first. */
6358 next = elt->purpose;
6364 /* Advance to the next bigger node. */
6369 /* We have reached the biggest node in a subtree. Find
6370 the parent of it, which is the next bigger node. */
6371 while (elt->parent && elt->parent->right == elt)
6374 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6377 next = elt->purpose;
6383 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6384 || TREE_CODE (constructor_type) == UNION_TYPE)
6386 tree ctor_unfilled_bitpos, elt_bitpos;
6388 /* If the current record is complete we are done. */
6389 if (constructor_unfilled_fields == 0)
6392 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6393 elt_bitpos = bit_position (elt->purpose);
6394 /* We can't compare fields here because there might be empty
6395 fields in between. */
6396 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6398 constructor_unfilled_fields = elt->purpose;
6399 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6402 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6404 /* Advance to the next smaller node. */
6409 /* We have reached the smallest node bigger than the
6410 current unfilled field. Fill the space first. */
6411 next = elt->purpose;
6417 /* Advance to the next bigger node. */
6422 /* We have reached the biggest node in a subtree. Find
6423 the parent of it, which is the next bigger node. */
6424 while (elt->parent && elt->parent->right == elt)
6428 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6429 bit_position (elt->purpose))))
6431 next = elt->purpose;
6439 /* Ordinarily return, but not if we want to output all
6440 and there are elements left. */
6441 if (! (all && next != 0))
6444 /* If it's not incremental, just skip over the gap, so that after
6445 jumping to retry we will output the next successive element. */
6446 if (TREE_CODE (constructor_type) == RECORD_TYPE
6447 || TREE_CODE (constructor_type) == UNION_TYPE)
6448 constructor_unfilled_fields = next;
6449 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6450 constructor_unfilled_index = next;
6452 /* ELT now points to the node in the pending tree with the next
6453 initializer to output. */
6457 /* Add one non-braced element to the current constructor level.
6458 This adjusts the current position within the constructor's type.
6459 This may also start or terminate implicit levels
6460 to handle a partly-braced initializer.
6462 Once this has found the correct level for the new element,
6463 it calls output_init_element. */
6466 process_init_element (value)
6469 tree orig_value = value;
6470 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6472 designator_depth = 0;
6473 designator_errorneous = 0;
6475 /* Handle superfluous braces around string cst as in
6476 char x[] = {"foo"}; */
6479 && TREE_CODE (constructor_type) == ARRAY_TYPE
6480 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
6481 && integer_zerop (constructor_unfilled_index))
6483 if (constructor_stack->replacement_value)
6484 error_init ("excess elements in char array initializer");
6485 constructor_stack->replacement_value = value;
6489 if (constructor_stack->replacement_value != 0)
6491 error_init ("excess elements in struct initializer");
6495 /* Ignore elements of a brace group if it is entirely superfluous
6496 and has already been diagnosed. */
6497 if (constructor_type == 0)
6500 /* If we've exhausted any levels that didn't have braces,
6502 while (constructor_stack->implicit)
6504 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6505 || TREE_CODE (constructor_type) == UNION_TYPE)
6506 && constructor_fields == 0)
6507 process_init_element (pop_init_level (1));
6508 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6509 && (constructor_max_index == 0
6510 || tree_int_cst_lt (constructor_max_index,
6511 constructor_index)))
6512 process_init_element (pop_init_level (1));
6517 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6518 if (constructor_range_stack)
6519 value = save_expr (value);
6523 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6526 enum tree_code fieldcode;
6528 if (constructor_fields == 0)
6530 pedwarn_init ("excess elements in struct initializer");
6534 fieldtype = TREE_TYPE (constructor_fields);
6535 if (fieldtype != error_mark_node)
6536 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6537 fieldcode = TREE_CODE (fieldtype);
6539 /* Accept a string constant to initialize a subarray. */
6541 && fieldcode == ARRAY_TYPE
6542 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6545 /* Otherwise, if we have come to a subaggregate,
6546 and we don't have an element of its type, push into it. */
6547 else if (value != 0 && !constructor_no_implicit
6548 && value != error_mark_node
6549 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6550 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6551 || fieldcode == UNION_TYPE))
6553 push_init_level (1);
6559 push_member_name (constructor_fields);
6560 output_init_element (value, fieldtype, constructor_fields, 1);
6561 RESTORE_SPELLING_DEPTH (constructor_depth);
6564 /* Do the bookkeeping for an element that was
6565 directly output as a constructor. */
6567 /* For a record, keep track of end position of last field. */
6568 if (DECL_SIZE (constructor_fields))
6569 constructor_bit_index
6570 = size_binop (PLUS_EXPR,
6571 bit_position (constructor_fields),
6572 DECL_SIZE (constructor_fields));
6574 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6575 /* Skip any nameless bit fields. */
6576 while (constructor_unfilled_fields != 0
6577 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6578 && DECL_NAME (constructor_unfilled_fields) == 0)
6579 constructor_unfilled_fields =
6580 TREE_CHAIN (constructor_unfilled_fields);
6583 constructor_fields = TREE_CHAIN (constructor_fields);
6584 /* Skip any nameless bit fields at the beginning. */
6585 while (constructor_fields != 0
6586 && DECL_C_BIT_FIELD (constructor_fields)
6587 && DECL_NAME (constructor_fields) == 0)
6588 constructor_fields = TREE_CHAIN (constructor_fields);
6590 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6593 enum tree_code fieldcode;
6595 if (constructor_fields == 0)
6597 pedwarn_init ("excess elements in union initializer");
6601 fieldtype = TREE_TYPE (constructor_fields);
6602 if (fieldtype != error_mark_node)
6603 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6604 fieldcode = TREE_CODE (fieldtype);
6606 /* Warn that traditional C rejects initialization of unions.
6607 We skip the warning if the value is zero. This is done
6608 under the assumption that the zero initializer in user
6609 code appears conditioned on e.g. __STDC__ to avoid
6610 "missing initializer" warnings and relies on default
6611 initialization to zero in the traditional C case.
6612 We also skip the warning if the initializer is designated,
6613 again on the assumption that this must be conditional on
6614 __STDC__ anyway (and we've already complained about the
6615 member-designator already). */
6616 if (warn_traditional && !in_system_header && !constructor_designated
6617 && !(value && (integer_zerop (value) || real_zerop (value))))
6618 warning ("traditional C rejects initialization of unions");
6620 /* Accept a string constant to initialize a subarray. */
6622 && fieldcode == ARRAY_TYPE
6623 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6626 /* Otherwise, if we have come to a subaggregate,
6627 and we don't have an element of its type, push into it. */
6628 else if (value != 0 && !constructor_no_implicit
6629 && value != error_mark_node
6630 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6631 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6632 || fieldcode == UNION_TYPE))
6634 push_init_level (1);
6640 push_member_name (constructor_fields);
6641 output_init_element (value, fieldtype, constructor_fields, 1);
6642 RESTORE_SPELLING_DEPTH (constructor_depth);
6645 /* Do the bookkeeping for an element that was
6646 directly output as a constructor. */
6648 constructor_bit_index = DECL_SIZE (constructor_fields);
6649 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6652 constructor_fields = 0;
6654 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6656 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6657 enum tree_code eltcode = TREE_CODE (elttype);
6659 /* Accept a string constant to initialize a subarray. */
6661 && eltcode == ARRAY_TYPE
6662 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6665 /* Otherwise, if we have come to a subaggregate,
6666 and we don't have an element of its type, push into it. */
6667 else if (value != 0 && !constructor_no_implicit
6668 && value != error_mark_node
6669 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6670 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6671 || eltcode == UNION_TYPE))
6673 push_init_level (1);
6677 if (constructor_max_index != 0
6678 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6679 || integer_all_onesp (constructor_max_index)))
6681 pedwarn_init ("excess elements in array initializer");
6685 /* Now output the actual element. */
6688 push_array_bounds (tree_low_cst (constructor_index, 0));
6689 output_init_element (value, elttype, constructor_index, 1);
6690 RESTORE_SPELLING_DEPTH (constructor_depth);
6694 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6697 /* If we are doing the bookkeeping for an element that was
6698 directly output as a constructor, we must update
6699 constructor_unfilled_index. */
6700 constructor_unfilled_index = constructor_index;
6703 /* Handle the sole element allowed in a braced initializer
6704 for a scalar variable. */
6705 else if (constructor_fields == 0)
6707 pedwarn_init ("excess elements in scalar initializer");
6713 output_init_element (value, constructor_type, NULL_TREE, 1);
6714 constructor_fields = 0;
6717 /* Handle range initializers either at this level or anywhere higher
6718 in the designator stack. */
6719 if (constructor_range_stack)
6721 struct constructor_range_stack *p, *range_stack;
6724 range_stack = constructor_range_stack;
6725 constructor_range_stack = 0;
6726 while (constructor_stack != range_stack->stack)
6728 if (!constructor_stack->implicit)
6730 process_init_element (pop_init_level (1));
6732 for (p = range_stack;
6733 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6736 if (!constructor_stack->implicit)
6738 process_init_element (pop_init_level (1));
6741 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6742 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6747 constructor_index = p->index;
6748 constructor_fields = p->fields;
6749 if (finish && p->range_end && p->index == p->range_start)
6757 push_init_level (2);
6758 p->stack = constructor_stack;
6759 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6760 p->index = p->range_start;
6764 constructor_range_stack = range_stack;
6771 constructor_range_stack = 0;
6774 /* Build a simple asm-statement, from one string literal. */
6776 simple_asm_stmt (expr)
6781 if (TREE_CODE (expr) == ADDR_EXPR)
6782 expr = TREE_OPERAND (expr, 0);
6784 if (TREE_CODE (expr) == STRING_CST)
6788 if (TREE_CHAIN (expr))
6789 expr = combine_strings (expr);
6790 stmt = add_stmt (build_stmt (ASM_STMT, NULL_TREE, expr,
6791 NULL_TREE, NULL_TREE,
6793 ASM_INPUT_P (stmt) = 1;
6797 error ("argument of `asm' is not a constant string");
6801 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6802 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6805 build_asm_stmt (cv_qualifier, string, outputs, inputs, clobbers)
6814 if (TREE_CHAIN (string))
6815 string = combine_strings (string);
6816 if (TREE_CODE (string) != STRING_CST)
6818 error ("asm template is not a string constant");
6822 if (cv_qualifier != NULL_TREE
6823 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6825 warning ("%s qualifier ignored on asm",
6826 IDENTIFIER_POINTER (cv_qualifier));
6827 cv_qualifier = NULL_TREE;
6830 /* We can remove output conversions that change the type,
6831 but not the mode. */
6832 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6834 tree output = TREE_VALUE (tail);
6836 STRIP_NOPS (output);
6837 TREE_VALUE (tail) = output;
6839 /* Allow conversions as LHS here. build_modify_expr as called below
6840 will do the right thing with them. */
6841 while (TREE_CODE (output) == NOP_EXPR
6842 || TREE_CODE (output) == CONVERT_EXPR
6843 || TREE_CODE (output) == FLOAT_EXPR
6844 || TREE_CODE (output) == FIX_TRUNC_EXPR
6845 || TREE_CODE (output) == FIX_FLOOR_EXPR
6846 || TREE_CODE (output) == FIX_ROUND_EXPR
6847 || TREE_CODE (output) == FIX_CEIL_EXPR)
6848 output = TREE_OPERAND (output, 0);
6850 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6853 /* Remove output conversions that change the type but not the mode. */
6854 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6856 tree output = TREE_VALUE (tail);
6857 STRIP_NOPS (output);
6858 TREE_VALUE (tail) = output;
6861 /* Perform default conversions on array and function inputs.
6862 Don't do this for other types as it would screw up operands
6863 expected to be in memory. */
6864 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6865 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == ARRAY_TYPE
6866 || TREE_CODE (TREE_TYPE (TREE_VALUE (tail))) == FUNCTION_TYPE)
6867 TREE_VALUE (tail) = default_conversion (TREE_VALUE (tail));
6869 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6870 outputs, inputs, clobbers));
6873 /* Expand an ASM statement with operands, handling output operands
6874 that are not variables or INDIRECT_REFS by transforming such
6875 cases into cases that expand_asm_operands can handle.
6877 Arguments are same as for expand_asm_operands. */
6880 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6881 tree string, outputs, inputs, clobbers;
6883 const char *filename;
6886 int noutputs = list_length (outputs);
6888 /* o[I] is the place that output number I should be written. */
6889 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6892 /* Record the contents of OUTPUTS before it is modified. */
6893 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6894 o[i] = TREE_VALUE (tail);
6896 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6897 OUTPUTS some trees for where the values were actually stored. */
6898 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6900 /* Copy all the intermediate outputs into the specified outputs. */
6901 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6903 if (o[i] != TREE_VALUE (tail))
6905 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6906 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6909 /* Restore the original value so that it's correct the next
6910 time we expand this function. */
6911 TREE_VALUE (tail) = o[i];
6913 /* Detect modification of read-only values.
6914 (Otherwise done by build_modify_expr.) */
6917 tree type = TREE_TYPE (o[i]);
6918 if (TREE_READONLY (o[i])
6919 || TYPE_READONLY (type)
6920 || ((TREE_CODE (type) == RECORD_TYPE
6921 || TREE_CODE (type) == UNION_TYPE)
6922 && C_TYPE_FIELDS_READONLY (type)))
6923 readonly_warning (o[i], "modification by `asm'");
6927 /* Those MODIFY_EXPRs could do autoincrements. */
6931 /* Expand a C `return' statement.
6932 RETVAL is the expression for what to return,
6933 or a null pointer for `return;' with no value. */
6936 c_expand_return (retval)
6939 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6941 if (TREE_THIS_VOLATILE (current_function_decl))
6942 warning ("function declared `noreturn' has a `return' statement");
6946 current_function_returns_null = 1;
6947 if ((warn_return_type || flag_isoc99)
6948 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6949 pedwarn_c99 ("`return' with no value, in function returning non-void");
6951 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6953 current_function_returns_null = 1;
6954 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6955 pedwarn ("`return' with a value, in function returning void");
6959 tree t = convert_for_assignment (valtype, retval, _("return"),
6960 NULL_TREE, NULL_TREE, 0);
6961 tree res = DECL_RESULT (current_function_decl);
6964 if (t == error_mark_node)
6967 inner = t = convert (TREE_TYPE (res), t);
6969 /* Strip any conversions, additions, and subtractions, and see if
6970 we are returning the address of a local variable. Warn if so. */
6973 switch (TREE_CODE (inner))
6975 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6977 inner = TREE_OPERAND (inner, 0);
6981 /* If the second operand of the MINUS_EXPR has a pointer
6982 type (or is converted from it), this may be valid, so
6983 don't give a warning. */
6985 tree op1 = TREE_OPERAND (inner, 1);
6987 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6988 && (TREE_CODE (op1) == NOP_EXPR
6989 || TREE_CODE (op1) == NON_LVALUE_EXPR
6990 || TREE_CODE (op1) == CONVERT_EXPR))
6991 op1 = TREE_OPERAND (op1, 0);
6993 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6996 inner = TREE_OPERAND (inner, 0);
7001 inner = TREE_OPERAND (inner, 0);
7003 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7004 inner = TREE_OPERAND (inner, 0);
7006 if (TREE_CODE (inner) == VAR_DECL
7007 && ! DECL_EXTERNAL (inner)
7008 && ! TREE_STATIC (inner)
7009 && DECL_CONTEXT (inner) == current_function_decl)
7010 warning ("function returns address of local variable");
7020 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
7021 current_function_returns_value = 1;
7024 return add_stmt (build_return_stmt (retval));
7028 /* The SWITCH_STMT being built. */
7030 /* A splay-tree mapping the low element of a case range to the high
7031 element, or NULL_TREE if there is no high element. Used to
7032 determine whether or not a new case label duplicates an old case
7033 label. We need a tree, rather than simply a hash table, because
7034 of the GNU case range extension. */
7036 /* The next node on the stack. */
7037 struct c_switch *next;
7040 /* A stack of the currently active switch statements. The innermost
7041 switch statement is on the top of the stack. There is no need to
7042 mark the stack for garbage collection because it is only active
7043 during the processing of the body of a function, and we never
7044 collect at that point. */
7046 static struct c_switch *switch_stack;
7048 /* Start a C switch statement, testing expression EXP. Return the new
7055 enum tree_code code;
7057 struct c_switch *cs;
7059 if (exp != error_mark_node)
7061 code = TREE_CODE (TREE_TYPE (exp));
7062 type = TREE_TYPE (exp);
7064 if (! INTEGRAL_TYPE_P (type)
7065 && code != ERROR_MARK)
7067 error ("switch quantity not an integer");
7068 exp = integer_zero_node;
7073 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
7075 if (warn_traditional && !in_system_header
7076 && (type == long_integer_type_node
7077 || type == long_unsigned_type_node))
7078 warning ("`long' switch expression not converted to `int' in ISO C");
7080 exp = default_conversion (exp);
7081 type = TREE_TYPE (exp);
7082 index = get_unwidened (exp, NULL_TREE);
7083 /* We can't strip a conversion from a signed type to an
7084 unsigned, because if we did, int_fits_type_p would do the
7085 wrong thing when checking case values for being in range,
7086 and it's too hard to do the right thing. */
7087 if (TREE_UNSIGNED (TREE_TYPE (exp))
7088 == TREE_UNSIGNED (TREE_TYPE (index)))
7093 /* Add this new SWITCH_STMT to the stack. */
7094 cs = (struct c_switch *) xmalloc (sizeof (*cs));
7095 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, NULL_TREE);
7096 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7097 cs->next = switch_stack;
7100 return add_stmt (switch_stack->switch_stmt);
7103 /* Process a case label. */
7106 do_case (low_value, high_value)
7110 tree label = NULL_TREE;
7114 label = c_add_case_label (switch_stack->cases,
7115 SWITCH_COND (switch_stack->switch_stmt),
7116 low_value, high_value);
7117 if (label == error_mark_node)
7121 error ("case label not within a switch statement");
7123 error ("`default' label not within a switch statement");
7128 /* Finish the switch statement. */
7133 struct c_switch *cs = switch_stack;
7135 RECHAIN_STMTS (cs->switch_stmt, SWITCH_BODY (cs->switch_stmt));
7137 /* Pop the stack. */
7138 switch_stack = switch_stack->next;
7139 splay_tree_delete (cs->cases);