1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor, 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. */
30 #include "coretypes.h"
34 #include "langhooks.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
58 /* The level of nesting inside "__alignof__". */
61 /* The level of nesting inside "sizeof". */
64 /* The level of nesting inside "typeof". */
67 struct c_label_context_se *label_context_stack_se;
68 struct c_label_context_vm *label_context_stack_vm;
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static tree qualify_type (tree, tree);
78 static int tagged_types_tu_compatible_p (tree, tree);
79 static int comp_target_types (tree, tree);
80 static int function_types_compatible_p (tree, tree);
81 static int type_lists_compatible_p (tree, tree);
82 static tree decl_constant_value_for_broken_optimization (tree);
83 static tree lookup_field (tree, tree);
84 static tree convert_arguments (tree, tree, tree, tree);
85 static tree pointer_diff (tree, tree);
86 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
88 static tree valid_compound_expr_initializer (tree, tree);
89 static void push_string (const char *);
90 static void push_member_name (tree);
91 static int spelling_length (void);
92 static char *print_spelling (char *);
93 static void warning_init (const char *);
94 static tree digest_init (tree, tree, bool, int);
95 static void output_init_element (tree, bool, tree, tree, int);
96 static void output_pending_init_elements (int);
97 static int set_designator (int);
98 static void push_range_stack (tree);
99 static void add_pending_init (tree, tree);
100 static void set_nonincremental_init (void);
101 static void set_nonincremental_init_from_string (tree);
102 static tree find_init_member (tree);
103 static void readonly_error (tree, enum lvalue_use);
104 static int lvalue_or_else (tree, enum lvalue_use);
105 static int lvalue_p (tree);
106 static void record_maybe_used_decl (tree);
107 static int comptypes_internal (tree, tree);
108 \f/* This is a cache to hold if two types are compatible or not. */
110 struct tagged_tu_seen_cache {
111 const struct tagged_tu_seen_cache * next;
114 /* The return value of tagged_types_tu_compatible_p if we had seen
115 these two types already. */
119 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
120 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
122 /* Do `exp = require_complete_type (exp);' to make sure exp
123 does not have an incomplete type. (That includes void types.) */
126 require_complete_type (tree value)
128 tree type = TREE_TYPE (value);
130 if (value == error_mark_node || type == error_mark_node)
131 return error_mark_node;
133 /* First, detect a valid value with a complete type. */
134 if (COMPLETE_TYPE_P (type))
137 c_incomplete_type_error (value, type);
138 return error_mark_node;
141 /* Print an error message for invalid use of an incomplete type.
142 VALUE is the expression that was used (or 0 if that isn't known)
143 and TYPE is the type that was invalid. */
146 c_incomplete_type_error (tree value, tree type)
148 const char *type_code_string;
150 /* Avoid duplicate error message. */
151 if (TREE_CODE (type) == ERROR_MARK)
154 if (value != 0 && (TREE_CODE (value) == VAR_DECL
155 || TREE_CODE (value) == PARM_DECL))
156 error ("%qD has an incomplete type", value);
160 /* We must print an error message. Be clever about what it says. */
162 switch (TREE_CODE (type))
165 type_code_string = "struct";
169 type_code_string = "union";
173 type_code_string = "enum";
177 error ("invalid use of void expression");
181 if (TYPE_DOMAIN (type))
183 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
185 error ("invalid use of flexible array member");
188 type = TREE_TYPE (type);
191 error ("invalid use of array with unspecified bounds");
198 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
199 error ("invalid use of undefined type %<%s %E%>",
200 type_code_string, TYPE_NAME (type));
202 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
203 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
207 /* Given a type, apply default promotions wrt unnamed function
208 arguments and return the new type. */
211 c_type_promotes_to (tree type)
213 if (TYPE_MAIN_VARIANT (type) == float_type_node)
214 return double_type_node;
216 if (c_promoting_integer_type_p (type))
218 /* Preserve unsignedness if not really getting any wider. */
219 if (TYPE_UNSIGNED (type)
220 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
221 return unsigned_type_node;
222 return integer_type_node;
228 /* Return a variant of TYPE which has all the type qualifiers of LIKE
229 as well as those of TYPE. */
232 qualify_type (tree type, tree like)
234 return c_build_qualified_type (type,
235 TYPE_QUALS (type) | TYPE_QUALS (like));
238 /* Return the composite type of two compatible types.
240 We assume that comptypes has already been done and returned
241 nonzero; if that isn't so, this may crash. In particular, we
242 assume that qualifiers match. */
245 composite_type (tree t1, tree t2)
247 enum tree_code code1;
248 enum tree_code code2;
251 /* Save time if the two types are the same. */
253 if (t1 == t2) return t1;
255 /* If one type is nonsense, use the other. */
256 if (t1 == error_mark_node)
258 if (t2 == error_mark_node)
261 code1 = TREE_CODE (t1);
262 code2 = TREE_CODE (t2);
264 /* Merge the attributes. */
265 attributes = targetm.merge_type_attributes (t1, t2);
267 /* If one is an enumerated type and the other is the compatible
268 integer type, the composite type might be either of the two
269 (DR#013 question 3). For consistency, use the enumerated type as
270 the composite type. */
272 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
274 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
277 gcc_assert (code1 == code2);
282 /* For two pointers, do this recursively on the target type. */
284 tree pointed_to_1 = TREE_TYPE (t1);
285 tree pointed_to_2 = TREE_TYPE (t2);
286 tree target = composite_type (pointed_to_1, pointed_to_2);
287 t1 = build_pointer_type (target);
288 t1 = build_type_attribute_variant (t1, attributes);
289 return qualify_type (t1, t2);
294 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
297 tree d1 = TYPE_DOMAIN (t1);
298 tree d2 = TYPE_DOMAIN (t2);
299 bool d1_variable, d2_variable;
300 bool d1_zero, d2_zero;
302 /* We should not have any type quals on arrays at all. */
303 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
305 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
306 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
308 d1_variable = (!d1_zero
309 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
310 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
311 d2_variable = (!d2_zero
312 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
313 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
315 /* Save space: see if the result is identical to one of the args. */
316 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
317 && (d2_variable || d2_zero || !d1_variable))
318 return build_type_attribute_variant (t1, attributes);
319 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
320 && (d1_variable || d1_zero || !d2_variable))
321 return build_type_attribute_variant (t2, attributes);
323 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
324 return build_type_attribute_variant (t1, attributes);
325 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
326 return build_type_attribute_variant (t2, attributes);
328 /* Merge the element types, and have a size if either arg has
329 one. We may have qualifiers on the element types. To set
330 up TYPE_MAIN_VARIANT correctly, we need to form the
331 composite of the unqualified types and add the qualifiers
333 quals = TYPE_QUALS (strip_array_types (elt));
334 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
335 t1 = build_array_type (unqual_elt,
336 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
342 t1 = c_build_qualified_type (t1, quals);
343 return build_type_attribute_variant (t1, attributes);
347 /* Function types: prefer the one that specified arg types.
348 If both do, merge the arg types. Also merge the return types. */
350 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
351 tree p1 = TYPE_ARG_TYPES (t1);
352 tree p2 = TYPE_ARG_TYPES (t2);
357 /* Save space: see if the result is identical to one of the args. */
358 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
359 return build_type_attribute_variant (t1, attributes);
360 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
361 return build_type_attribute_variant (t2, attributes);
363 /* Simple way if one arg fails to specify argument types. */
364 if (TYPE_ARG_TYPES (t1) == 0)
366 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
367 t1 = build_type_attribute_variant (t1, attributes);
368 return qualify_type (t1, t2);
370 if (TYPE_ARG_TYPES (t2) == 0)
372 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
373 t1 = build_type_attribute_variant (t1, attributes);
374 return qualify_type (t1, t2);
377 /* If both args specify argument types, we must merge the two
378 lists, argument by argument. */
379 /* Tell global_bindings_p to return false so that variable_size
380 doesn't die on VLAs in parameter types. */
381 c_override_global_bindings_to_false = true;
383 len = list_length (p1);
386 for (i = 0; i < len; i++)
387 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
392 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
394 /* A null type means arg type is not specified.
395 Take whatever the other function type has. */
396 if (TREE_VALUE (p1) == 0)
398 TREE_VALUE (n) = TREE_VALUE (p2);
401 if (TREE_VALUE (p2) == 0)
403 TREE_VALUE (n) = TREE_VALUE (p1);
407 /* Given wait (union {union wait *u; int *i} *)
408 and wait (union wait *),
409 prefer union wait * as type of parm. */
410 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
411 && TREE_VALUE (p1) != TREE_VALUE (p2))
414 tree mv2 = TREE_VALUE (p2);
415 if (mv2 && mv2 != error_mark_node
416 && TREE_CODE (mv2) != ARRAY_TYPE)
417 mv2 = TYPE_MAIN_VARIANT (mv2);
418 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
419 memb; memb = TREE_CHAIN (memb))
421 tree mv3 = TREE_TYPE (memb);
422 if (mv3 && mv3 != error_mark_node
423 && TREE_CODE (mv3) != ARRAY_TYPE)
424 mv3 = TYPE_MAIN_VARIANT (mv3);
425 if (comptypes (mv3, mv2))
427 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
430 pedwarn ("function types not truly compatible in ISO C");
435 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
436 && TREE_VALUE (p2) != TREE_VALUE (p1))
439 tree mv1 = TREE_VALUE (p1);
440 if (mv1 && mv1 != error_mark_node
441 && TREE_CODE (mv1) != ARRAY_TYPE)
442 mv1 = TYPE_MAIN_VARIANT (mv1);
443 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
444 memb; memb = TREE_CHAIN (memb))
446 tree mv3 = TREE_TYPE (memb);
447 if (mv3 && mv3 != error_mark_node
448 && TREE_CODE (mv3) != ARRAY_TYPE)
449 mv3 = TYPE_MAIN_VARIANT (mv3);
450 if (comptypes (mv3, mv1))
452 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
455 pedwarn ("function types not truly compatible in ISO C");
460 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
464 c_override_global_bindings_to_false = false;
465 t1 = build_function_type (valtype, newargs);
466 t1 = qualify_type (t1, t2);
467 /* ... falls through ... */
471 return build_type_attribute_variant (t1, attributes);
476 /* Return the type of a conditional expression between pointers to
477 possibly differently qualified versions of compatible types.
479 We assume that comp_target_types has already been done and returned
480 nonzero; if that isn't so, this may crash. */
483 common_pointer_type (tree t1, tree t2)
486 tree pointed_to_1, mv1;
487 tree pointed_to_2, mv2;
490 /* Save time if the two types are the same. */
492 if (t1 == t2) return t1;
494 /* If one type is nonsense, use the other. */
495 if (t1 == error_mark_node)
497 if (t2 == error_mark_node)
500 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
501 && TREE_CODE (t2) == POINTER_TYPE);
503 /* Merge the attributes. */
504 attributes = targetm.merge_type_attributes (t1, t2);
506 /* Find the composite type of the target types, and combine the
507 qualifiers of the two types' targets. Do not lose qualifiers on
508 array element types by taking the TYPE_MAIN_VARIANT. */
509 mv1 = pointed_to_1 = TREE_TYPE (t1);
510 mv2 = pointed_to_2 = TREE_TYPE (t2);
511 if (TREE_CODE (mv1) != ARRAY_TYPE)
512 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
513 if (TREE_CODE (mv2) != ARRAY_TYPE)
514 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
515 target = composite_type (mv1, mv2);
516 t1 = build_pointer_type (c_build_qualified_type
518 TYPE_QUALS (pointed_to_1) |
519 TYPE_QUALS (pointed_to_2)));
520 return build_type_attribute_variant (t1, attributes);
523 /* Return the common type for two arithmetic types under the usual
524 arithmetic conversions. The default conversions have already been
525 applied, and enumerated types converted to their compatible integer
526 types. The resulting type is unqualified and has no attributes.
528 This is the type for the result of most arithmetic operations
529 if the operands have the given two types. */
532 c_common_type (tree t1, tree t2)
534 enum tree_code code1;
535 enum tree_code code2;
537 /* If one type is nonsense, use the other. */
538 if (t1 == error_mark_node)
540 if (t2 == error_mark_node)
543 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
544 t1 = TYPE_MAIN_VARIANT (t1);
546 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
547 t2 = TYPE_MAIN_VARIANT (t2);
549 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
550 t1 = build_type_attribute_variant (t1, NULL_TREE);
552 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
553 t2 = build_type_attribute_variant (t2, NULL_TREE);
555 /* Save time if the two types are the same. */
557 if (t1 == t2) return t1;
559 code1 = TREE_CODE (t1);
560 code2 = TREE_CODE (t2);
562 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
563 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
564 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
565 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
567 /* If one type is a vector type, return that type. (How the usual
568 arithmetic conversions apply to the vector types extension is not
569 precisely specified.) */
570 if (code1 == VECTOR_TYPE)
573 if (code2 == VECTOR_TYPE)
576 /* If one type is complex, form the common type of the non-complex
577 components, then make that complex. Use T1 or T2 if it is the
579 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
581 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
582 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
583 tree subtype = c_common_type (subtype1, subtype2);
585 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
587 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
590 return build_complex_type (subtype);
593 /* If only one is real, use it as the result. */
595 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
598 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
601 /* Both real or both integers; use the one with greater precision. */
603 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
605 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
608 /* Same precision. Prefer long longs to longs to ints when the
609 same precision, following the C99 rules on integer type rank
610 (which are equivalent to the C90 rules for C90 types). */
612 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
613 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
614 return long_long_unsigned_type_node;
616 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
617 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
619 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
620 return long_long_unsigned_type_node;
622 return long_long_integer_type_node;
625 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
626 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
627 return long_unsigned_type_node;
629 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
630 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
632 /* But preserve unsignedness from the other type,
633 since long cannot hold all the values of an unsigned int. */
634 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
635 return long_unsigned_type_node;
637 return long_integer_type_node;
640 /* Likewise, prefer long double to double even if same size. */
641 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
642 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
643 return long_double_type_node;
645 /* Otherwise prefer the unsigned one. */
647 if (TYPE_UNSIGNED (t1))
653 /* Wrapper around c_common_type that is used by c-common.c and other
654 front end optimizations that remove promotions. ENUMERAL_TYPEs
655 are allowed here and are converted to their compatible integer types.
656 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
657 preferably a non-Boolean type as the common type. */
659 common_type (tree t1, tree t2)
661 if (TREE_CODE (t1) == ENUMERAL_TYPE)
662 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
663 if (TREE_CODE (t2) == ENUMERAL_TYPE)
664 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
666 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
667 if (TREE_CODE (t1) == BOOLEAN_TYPE
668 && TREE_CODE (t2) == BOOLEAN_TYPE)
669 return boolean_type_node;
671 /* If either type is BOOLEAN_TYPE, then return the other. */
672 if (TREE_CODE (t1) == BOOLEAN_TYPE)
674 if (TREE_CODE (t2) == BOOLEAN_TYPE)
677 return c_common_type (t1, t2);
680 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
681 or various other operations. Return 2 if they are compatible
682 but a warning may be needed if you use them together. */
685 comptypes (tree type1, tree type2)
687 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
690 val = comptypes_internal (type1, type2);
691 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
695 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
696 or various other operations. Return 2 if they are compatible
697 but a warning may be needed if you use them together. This
698 differs from comptypes, in that we don't free the seen types. */
701 comptypes_internal (tree type1, tree type2)
707 /* Suppress errors caused by previously reported errors. */
709 if (t1 == t2 || !t1 || !t2
710 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
713 /* If either type is the internal version of sizetype, return the
715 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
716 && TYPE_ORIG_SIZE_TYPE (t1))
717 t1 = TYPE_ORIG_SIZE_TYPE (t1);
719 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
720 && TYPE_ORIG_SIZE_TYPE (t2))
721 t2 = TYPE_ORIG_SIZE_TYPE (t2);
724 /* Enumerated types are compatible with integer types, but this is
725 not transitive: two enumerated types in the same translation unit
726 are compatible with each other only if they are the same type. */
728 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
729 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
730 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
731 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
736 /* Different classes of types can't be compatible. */
738 if (TREE_CODE (t1) != TREE_CODE (t2))
741 /* Qualifiers must match. C99 6.7.3p9 */
743 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
746 /* Allow for two different type nodes which have essentially the same
747 definition. Note that we already checked for equality of the type
748 qualifiers (just above). */
750 if (TREE_CODE (t1) != ARRAY_TYPE
751 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
754 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
755 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
758 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
761 switch (TREE_CODE (t1))
764 /* Do not remove mode or aliasing information. */
765 if (TYPE_MODE (t1) != TYPE_MODE (t2)
766 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
768 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
769 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
773 val = function_types_compatible_p (t1, t2);
778 tree d1 = TYPE_DOMAIN (t1);
779 tree d2 = TYPE_DOMAIN (t2);
780 bool d1_variable, d2_variable;
781 bool d1_zero, d2_zero;
784 /* Target types must match incl. qualifiers. */
785 if (TREE_TYPE (t1) != TREE_TYPE (t2)
786 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
789 /* Sizes must match unless one is missing or variable. */
790 if (d1 == 0 || d2 == 0 || d1 == d2)
793 d1_zero = !TYPE_MAX_VALUE (d1);
794 d2_zero = !TYPE_MAX_VALUE (d2);
796 d1_variable = (!d1_zero
797 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
798 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
799 d2_variable = (!d2_zero
800 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
801 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
803 if (d1_variable || d2_variable)
805 if (d1_zero && d2_zero)
807 if (d1_zero || d2_zero
808 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
809 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
818 if (val != 1 && !same_translation_unit_p (t1, t2))
821 return tagged_types_tu_compatible_p (t1, t2);
822 val = tagged_types_tu_compatible_p (t1, t2);
827 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
828 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
834 return attrval == 2 && val == 1 ? 2 : val;
837 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
838 ignoring their qualifiers. */
841 comp_target_types (tree ttl, tree ttr)
846 /* Do not lose qualifiers on element types of array types that are
847 pointer targets by taking their TYPE_MAIN_VARIANT. */
848 mvl = TREE_TYPE (ttl);
849 mvr = TREE_TYPE (ttr);
850 if (TREE_CODE (mvl) != ARRAY_TYPE)
851 mvl = TYPE_MAIN_VARIANT (mvl);
852 if (TREE_CODE (mvr) != ARRAY_TYPE)
853 mvr = TYPE_MAIN_VARIANT (mvr);
854 val = comptypes (mvl, mvr);
856 if (val == 2 && pedantic)
857 pedwarn ("types are not quite compatible");
861 /* Subroutines of `comptypes'. */
863 /* Determine whether two trees derive from the same translation unit.
864 If the CONTEXT chain ends in a null, that tree's context is still
865 being parsed, so if two trees have context chains ending in null,
866 they're in the same translation unit. */
868 same_translation_unit_p (tree t1, tree t2)
870 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
871 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
873 case tcc_declaration:
874 t1 = DECL_CONTEXT (t1); break;
876 t1 = TYPE_CONTEXT (t1); break;
877 case tcc_exceptional:
878 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
879 default: gcc_unreachable ();
882 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
883 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
885 case tcc_declaration:
886 t2 = DECL_CONTEXT (t2); break;
888 t2 = TYPE_CONTEXT (t2); break;
889 case tcc_exceptional:
890 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
891 default: gcc_unreachable ();
897 /* Allocate the seen two types, assuming that they are compatible. */
899 static struct tagged_tu_seen_cache *
900 alloc_tagged_tu_seen_cache (tree t1, tree t2)
902 struct tagged_tu_seen_cache *tu = xmalloc (sizeof (struct tagged_tu_seen_cache));
903 tu->next = tagged_tu_seen_base;
907 tagged_tu_seen_base = tu;
909 /* The C standard says that two structures in different translation
910 units are compatible with each other only if the types of their
911 fields are compatible (among other things). We assume that they
912 are compatible until proven otherwise when building the cache.
913 An example where this can occur is:
918 If we are comparing this against a similar struct in another TU,
919 and did not assume they were compatible, we end up with an infinite
925 /* Free the seen types until we get to TU_TIL. */
928 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
930 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
933 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
937 tagged_tu_seen_base = tu_til;
940 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
941 compatible. If the two types are not the same (which has been
942 checked earlier), this can only happen when multiple translation
943 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
947 tagged_types_tu_compatible_p (tree t1, tree t2)
950 bool needs_warning = false;
952 /* We have to verify that the tags of the types are the same. This
953 is harder than it looks because this may be a typedef, so we have
954 to go look at the original type. It may even be a typedef of a
956 In the case of compiler-created builtin structs the TYPE_DECL
957 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
958 while (TYPE_NAME (t1)
959 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
960 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
961 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
963 while (TYPE_NAME (t2)
964 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
965 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
966 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
968 /* C90 didn't have the requirement that the two tags be the same. */
969 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
972 /* C90 didn't say what happened if one or both of the types were
973 incomplete; we choose to follow C99 rules here, which is that they
975 if (TYPE_SIZE (t1) == NULL
976 || TYPE_SIZE (t2) == NULL)
980 const struct tagged_tu_seen_cache * tts_i;
981 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
982 if (tts_i->t1 == t1 && tts_i->t2 == t2)
986 switch (TREE_CODE (t1))
990 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
991 /* Speed up the case where the type values are in the same order. */
992 tree tv1 = TYPE_VALUES (t1);
993 tree tv2 = TYPE_VALUES (t2);
1000 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1002 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1004 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1011 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1015 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1021 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1027 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1029 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1031 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1042 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1043 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1049 /* Speed up the common case where the fields are in the same order. */
1050 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1051 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1056 if (DECL_NAME (s1) == NULL
1057 || DECL_NAME (s1) != DECL_NAME (s2))
1059 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1066 needs_warning = true;
1068 if (TREE_CODE (s1) == FIELD_DECL
1069 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1070 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1078 tu->val = needs_warning ? 2 : 1;
1082 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1086 if (DECL_NAME (s1) != NULL)
1087 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1088 if (DECL_NAME (s1) == DECL_NAME (s2))
1091 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1098 needs_warning = true;
1100 if (TREE_CODE (s1) == FIELD_DECL
1101 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1102 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1114 tu->val = needs_warning ? 2 : 10;
1120 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1122 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1124 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1127 if (TREE_CODE (s1) != TREE_CODE (s2)
1128 || DECL_NAME (s1) != DECL_NAME (s2))
1130 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1134 needs_warning = true;
1136 if (TREE_CODE (s1) == FIELD_DECL
1137 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1138 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1144 tu->val = needs_warning ? 2 : 1;
1153 /* Return 1 if two function types F1 and F2 are compatible.
1154 If either type specifies no argument types,
1155 the other must specify a fixed number of self-promoting arg types.
1156 Otherwise, if one type specifies only the number of arguments,
1157 the other must specify that number of self-promoting arg types.
1158 Otherwise, the argument types must match. */
1161 function_types_compatible_p (tree f1, tree f2)
1164 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1169 ret1 = TREE_TYPE (f1);
1170 ret2 = TREE_TYPE (f2);
1172 /* 'volatile' qualifiers on a function's return type used to mean
1173 the function is noreturn. */
1174 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1175 pedwarn ("function return types not compatible due to %<volatile%>");
1176 if (TYPE_VOLATILE (ret1))
1177 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1178 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1179 if (TYPE_VOLATILE (ret2))
1180 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1181 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1182 val = comptypes_internal (ret1, ret2);
1186 args1 = TYPE_ARG_TYPES (f1);
1187 args2 = TYPE_ARG_TYPES (f2);
1189 /* An unspecified parmlist matches any specified parmlist
1190 whose argument types don't need default promotions. */
1194 if (!self_promoting_args_p (args2))
1196 /* If one of these types comes from a non-prototype fn definition,
1197 compare that with the other type's arglist.
1198 If they don't match, ask for a warning (but no error). */
1199 if (TYPE_ACTUAL_ARG_TYPES (f1)
1200 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1206 if (!self_promoting_args_p (args1))
1208 if (TYPE_ACTUAL_ARG_TYPES (f2)
1209 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1214 /* Both types have argument lists: compare them and propagate results. */
1215 val1 = type_lists_compatible_p (args1, args2);
1216 return val1 != 1 ? val1 : val;
1219 /* Check two lists of types for compatibility,
1220 returning 0 for incompatible, 1 for compatible,
1221 or 2 for compatible with warning. */
1224 type_lists_compatible_p (tree args1, tree args2)
1226 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1232 tree a1, mv1, a2, mv2;
1233 if (args1 == 0 && args2 == 0)
1235 /* If one list is shorter than the other,
1236 they fail to match. */
1237 if (args1 == 0 || args2 == 0)
1239 mv1 = a1 = TREE_VALUE (args1);
1240 mv2 = a2 = TREE_VALUE (args2);
1241 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1242 mv1 = TYPE_MAIN_VARIANT (mv1);
1243 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1244 mv2 = TYPE_MAIN_VARIANT (mv2);
1245 /* A null pointer instead of a type
1246 means there is supposed to be an argument
1247 but nothing is specified about what type it has.
1248 So match anything that self-promotes. */
1251 if (c_type_promotes_to (a2) != a2)
1256 if (c_type_promotes_to (a1) != a1)
1259 /* If one of the lists has an error marker, ignore this arg. */
1260 else if (TREE_CODE (a1) == ERROR_MARK
1261 || TREE_CODE (a2) == ERROR_MARK)
1263 else if (!(newval = comptypes_internal (mv1, mv2)))
1265 /* Allow wait (union {union wait *u; int *i} *)
1266 and wait (union wait *) to be compatible. */
1267 if (TREE_CODE (a1) == UNION_TYPE
1268 && (TYPE_NAME (a1) == 0
1269 || TYPE_TRANSPARENT_UNION (a1))
1270 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1271 && tree_int_cst_equal (TYPE_SIZE (a1),
1275 for (memb = TYPE_FIELDS (a1);
1276 memb; memb = TREE_CHAIN (memb))
1278 tree mv3 = TREE_TYPE (memb);
1279 if (mv3 && mv3 != error_mark_node
1280 && TREE_CODE (mv3) != ARRAY_TYPE)
1281 mv3 = TYPE_MAIN_VARIANT (mv3);
1282 if (comptypes_internal (mv3, mv2))
1288 else if (TREE_CODE (a2) == UNION_TYPE
1289 && (TYPE_NAME (a2) == 0
1290 || TYPE_TRANSPARENT_UNION (a2))
1291 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1292 && tree_int_cst_equal (TYPE_SIZE (a2),
1296 for (memb = TYPE_FIELDS (a2);
1297 memb; memb = TREE_CHAIN (memb))
1299 tree mv3 = TREE_TYPE (memb);
1300 if (mv3 && mv3 != error_mark_node
1301 && TREE_CODE (mv3) != ARRAY_TYPE)
1302 mv3 = TYPE_MAIN_VARIANT (mv3);
1303 if (comptypes_internal (mv3, mv1))
1313 /* comptypes said ok, but record if it said to warn. */
1317 args1 = TREE_CHAIN (args1);
1318 args2 = TREE_CHAIN (args2);
1322 /* Compute the size to increment a pointer by. */
1325 c_size_in_bytes (tree type)
1327 enum tree_code code = TREE_CODE (type);
1329 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1330 return size_one_node;
1332 if (!COMPLETE_OR_VOID_TYPE_P (type))
1334 error ("arithmetic on pointer to an incomplete type");
1335 return size_one_node;
1338 /* Convert in case a char is more than one unit. */
1339 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1340 size_int (TYPE_PRECISION (char_type_node)
1344 /* Return either DECL or its known constant value (if it has one). */
1347 decl_constant_value (tree decl)
1349 if (/* Don't change a variable array bound or initial value to a constant
1350 in a place where a variable is invalid. Note that DECL_INITIAL
1351 isn't valid for a PARM_DECL. */
1352 current_function_decl != 0
1353 && TREE_CODE (decl) != PARM_DECL
1354 && !TREE_THIS_VOLATILE (decl)
1355 && TREE_READONLY (decl)
1356 && DECL_INITIAL (decl) != 0
1357 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1358 /* This is invalid if initial value is not constant.
1359 If it has either a function call, a memory reference,
1360 or a variable, then re-evaluating it could give different results. */
1361 && TREE_CONSTANT (DECL_INITIAL (decl))
1362 /* Check for cases where this is sub-optimal, even though valid. */
1363 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1364 return DECL_INITIAL (decl);
1368 /* Return either DECL or its known constant value (if it has one), but
1369 return DECL if pedantic or DECL has mode BLKmode. This is for
1370 bug-compatibility with the old behavior of decl_constant_value
1371 (before GCC 3.0); every use of this function is a bug and it should
1372 be removed before GCC 3.1. It is not appropriate to use pedantic
1373 in a way that affects optimization, and BLKmode is probably not the
1374 right test for avoiding misoptimizations either. */
1377 decl_constant_value_for_broken_optimization (tree decl)
1381 if (pedantic || DECL_MODE (decl) == BLKmode)
1384 ret = decl_constant_value (decl);
1385 /* Avoid unwanted tree sharing between the initializer and current
1386 function's body where the tree can be modified e.g. by the
1388 if (ret != decl && TREE_STATIC (decl))
1389 ret = unshare_expr (ret);
1393 /* Convert the array expression EXP to a pointer. */
1395 array_to_pointer_conversion (tree exp)
1397 tree orig_exp = exp;
1398 tree type = TREE_TYPE (exp);
1400 tree restype = TREE_TYPE (type);
1403 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1405 STRIP_TYPE_NOPS (exp);
1407 if (TREE_NO_WARNING (orig_exp))
1408 TREE_NO_WARNING (exp) = 1;
1410 ptrtype = build_pointer_type (restype);
1412 if (TREE_CODE (exp) == INDIRECT_REF)
1413 return convert (ptrtype, TREE_OPERAND (exp, 0));
1415 if (TREE_CODE (exp) == VAR_DECL)
1417 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1418 ADDR_EXPR because it's the best way of representing what
1419 happens in C when we take the address of an array and place
1420 it in a pointer to the element type. */
1421 adr = build1 (ADDR_EXPR, ptrtype, exp);
1422 if (!c_mark_addressable (exp))
1423 return error_mark_node;
1424 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1428 /* This way is better for a COMPONENT_REF since it can
1429 simplify the offset for a component. */
1430 adr = build_unary_op (ADDR_EXPR, exp, 1);
1431 return convert (ptrtype, adr);
1434 /* Convert the function expression EXP to a pointer. */
1436 function_to_pointer_conversion (tree exp)
1438 tree orig_exp = exp;
1440 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1442 STRIP_TYPE_NOPS (exp);
1444 if (TREE_NO_WARNING (orig_exp))
1445 TREE_NO_WARNING (exp) = 1;
1447 return build_unary_op (ADDR_EXPR, exp, 0);
1450 /* Perform the default conversion of arrays and functions to pointers.
1451 Return the result of converting EXP. For any other expression, just
1452 return EXP after removing NOPs. */
1455 default_function_array_conversion (struct c_expr exp)
1457 tree orig_exp = exp.value;
1458 tree type = TREE_TYPE (exp.value);
1459 enum tree_code code = TREE_CODE (type);
1465 bool not_lvalue = false;
1466 bool lvalue_array_p;
1468 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1469 || TREE_CODE (exp.value) == NOP_EXPR)
1470 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1472 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1474 exp.value = TREE_OPERAND (exp.value, 0);
1477 if (TREE_NO_WARNING (orig_exp))
1478 TREE_NO_WARNING (exp.value) = 1;
1480 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1481 if (!flag_isoc99 && !lvalue_array_p)
1483 /* Before C99, non-lvalue arrays do not decay to pointers.
1484 Normally, using such an array would be invalid; but it can
1485 be used correctly inside sizeof or as a statement expression.
1486 Thus, do not give an error here; an error will result later. */
1490 exp.value = array_to_pointer_conversion (exp.value);
1494 exp.value = function_to_pointer_conversion (exp.value);
1497 STRIP_TYPE_NOPS (exp.value);
1498 if (TREE_NO_WARNING (orig_exp))
1499 TREE_NO_WARNING (exp.value) = 1;
1507 /* EXP is an expression of integer type. Apply the integer promotions
1508 to it and return the promoted value. */
1511 perform_integral_promotions (tree exp)
1513 tree type = TREE_TYPE (exp);
1514 enum tree_code code = TREE_CODE (type);
1516 gcc_assert (INTEGRAL_TYPE_P (type));
1518 /* Normally convert enums to int,
1519 but convert wide enums to something wider. */
1520 if (code == ENUMERAL_TYPE)
1522 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1523 TYPE_PRECISION (integer_type_node)),
1524 ((TYPE_PRECISION (type)
1525 >= TYPE_PRECISION (integer_type_node))
1526 && TYPE_UNSIGNED (type)));
1528 return convert (type, exp);
1531 /* ??? This should no longer be needed now bit-fields have their
1533 if (TREE_CODE (exp) == COMPONENT_REF
1534 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1535 /* If it's thinner than an int, promote it like a
1536 c_promoting_integer_type_p, otherwise leave it alone. */
1537 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1538 TYPE_PRECISION (integer_type_node)))
1539 return convert (integer_type_node, exp);
1541 if (c_promoting_integer_type_p (type))
1543 /* Preserve unsignedness if not really getting any wider. */
1544 if (TYPE_UNSIGNED (type)
1545 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1546 return convert (unsigned_type_node, exp);
1548 return convert (integer_type_node, exp);
1555 /* Perform default promotions for C data used in expressions.
1556 Enumeral types or short or char are converted to int.
1557 In addition, manifest constants symbols are replaced by their values. */
1560 default_conversion (tree exp)
1563 tree type = TREE_TYPE (exp);
1564 enum tree_code code = TREE_CODE (type);
1566 /* Functions and arrays have been converted during parsing. */
1567 gcc_assert (code != FUNCTION_TYPE);
1568 if (code == ARRAY_TYPE)
1571 /* Constants can be used directly unless they're not loadable. */
1572 if (TREE_CODE (exp) == CONST_DECL)
1573 exp = DECL_INITIAL (exp);
1575 /* Replace a nonvolatile const static variable with its value unless
1576 it is an array, in which case we must be sure that taking the
1577 address of the array produces consistent results. */
1578 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1580 exp = decl_constant_value_for_broken_optimization (exp);
1581 type = TREE_TYPE (exp);
1584 /* Strip no-op conversions. */
1586 STRIP_TYPE_NOPS (exp);
1588 if (TREE_NO_WARNING (orig_exp))
1589 TREE_NO_WARNING (exp) = 1;
1591 if (INTEGRAL_TYPE_P (type))
1592 return perform_integral_promotions (exp);
1594 if (code == VOID_TYPE)
1596 error ("void value not ignored as it ought to be");
1597 return error_mark_node;
1602 /* Look up COMPONENT in a structure or union DECL.
1604 If the component name is not found, returns NULL_TREE. Otherwise,
1605 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1606 stepping down the chain to the component, which is in the last
1607 TREE_VALUE of the list. Normally the list is of length one, but if
1608 the component is embedded within (nested) anonymous structures or
1609 unions, the list steps down the chain to the component. */
1612 lookup_field (tree decl, tree component)
1614 tree type = TREE_TYPE (decl);
1617 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1618 to the field elements. Use a binary search on this array to quickly
1619 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1620 will always be set for structures which have many elements. */
1622 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1625 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1627 field = TYPE_FIELDS (type);
1629 top = TYPE_LANG_SPECIFIC (type)->s->len;
1630 while (top - bot > 1)
1632 half = (top - bot + 1) >> 1;
1633 field = field_array[bot+half];
1635 if (DECL_NAME (field) == NULL_TREE)
1637 /* Step through all anon unions in linear fashion. */
1638 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1640 field = field_array[bot++];
1641 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1642 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1644 tree anon = lookup_field (field, component);
1647 return tree_cons (NULL_TREE, field, anon);
1651 /* Entire record is only anon unions. */
1655 /* Restart the binary search, with new lower bound. */
1659 if (DECL_NAME (field) == component)
1661 if (DECL_NAME (field) < component)
1667 if (DECL_NAME (field_array[bot]) == component)
1668 field = field_array[bot];
1669 else if (DECL_NAME (field) != component)
1674 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1676 if (DECL_NAME (field) == NULL_TREE
1677 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1678 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1680 tree anon = lookup_field (field, component);
1683 return tree_cons (NULL_TREE, field, anon);
1686 if (DECL_NAME (field) == component)
1690 if (field == NULL_TREE)
1694 return tree_cons (NULL_TREE, field, NULL_TREE);
1697 /* Make an expression to refer to the COMPONENT field of
1698 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1701 build_component_ref (tree datum, tree component)
1703 tree type = TREE_TYPE (datum);
1704 enum tree_code code = TREE_CODE (type);
1708 if (!objc_is_public (datum, component))
1709 return error_mark_node;
1711 /* See if there is a field or component with name COMPONENT. */
1713 if (code == RECORD_TYPE || code == UNION_TYPE)
1715 if (!COMPLETE_TYPE_P (type))
1717 c_incomplete_type_error (NULL_TREE, type);
1718 return error_mark_node;
1721 field = lookup_field (datum, component);
1725 error ("%qT has no member named %qE", type, component);
1726 return error_mark_node;
1729 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1730 This might be better solved in future the way the C++ front
1731 end does it - by giving the anonymous entities each a
1732 separate name and type, and then have build_component_ref
1733 recursively call itself. We can't do that here. */
1736 tree subdatum = TREE_VALUE (field);
1738 if (TREE_TYPE (subdatum) == error_mark_node)
1739 return error_mark_node;
1741 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1743 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1744 TREE_READONLY (ref) = 1;
1745 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1746 TREE_THIS_VOLATILE (ref) = 1;
1748 if (TREE_DEPRECATED (subdatum))
1749 warn_deprecated_use (subdatum);
1753 field = TREE_CHAIN (field);
1759 else if (code != ERROR_MARK)
1760 error ("request for member %qE in something not a structure or union",
1763 return error_mark_node;
1766 /* Given an expression PTR for a pointer, return an expression
1767 for the value pointed to.
1768 ERRORSTRING is the name of the operator to appear in error messages. */
1771 build_indirect_ref (tree ptr, const char *errorstring)
1773 tree pointer = default_conversion (ptr);
1774 tree type = TREE_TYPE (pointer);
1776 if (TREE_CODE (type) == POINTER_TYPE)
1778 if (TREE_CODE (pointer) == ADDR_EXPR
1779 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1780 == TREE_TYPE (type)))
1781 return TREE_OPERAND (pointer, 0);
1784 tree t = TREE_TYPE (type);
1787 ref = build1 (INDIRECT_REF, t, pointer);
1789 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1791 error ("dereferencing pointer to incomplete type");
1792 return error_mark_node;
1794 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1795 warning (0, "dereferencing %<void *%> pointer");
1797 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1798 so that we get the proper error message if the result is used
1799 to assign to. Also, &* is supposed to be a no-op.
1800 And ANSI C seems to specify that the type of the result
1801 should be the const type. */
1802 /* A de-reference of a pointer to const is not a const. It is valid
1803 to change it via some other pointer. */
1804 TREE_READONLY (ref) = TYPE_READONLY (t);
1805 TREE_SIDE_EFFECTS (ref)
1806 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1807 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1811 else if (TREE_CODE (pointer) != ERROR_MARK)
1812 error ("invalid type argument of %qs", errorstring);
1813 return error_mark_node;
1816 /* This handles expressions of the form "a[i]", which denotes
1819 This is logically equivalent in C to *(a+i), but we may do it differently.
1820 If A is a variable or a member, we generate a primitive ARRAY_REF.
1821 This avoids forcing the array out of registers, and can work on
1822 arrays that are not lvalues (for example, members of structures returned
1826 build_array_ref (tree array, tree index)
1828 bool swapped = false;
1829 if (TREE_TYPE (array) == error_mark_node
1830 || TREE_TYPE (index) == error_mark_node)
1831 return error_mark_node;
1833 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1834 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1837 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1838 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1840 error ("subscripted value is neither array nor pointer");
1841 return error_mark_node;
1849 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1851 error ("array subscript is not an integer");
1852 return error_mark_node;
1855 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1857 error ("subscripted value is pointer to function");
1858 return error_mark_node;
1861 /* Subscripting with type char is likely to lose on a machine where
1862 chars are signed. So warn on any machine, but optionally. Don't
1863 warn for unsigned char since that type is safe. Don't warn for
1864 signed char because anyone who uses that must have done so
1865 deliberately. ??? Existing practice has also been to warn only
1866 when the char index is syntactically the index, not for
1869 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1870 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
1872 /* Apply default promotions *after* noticing character types. */
1873 index = default_conversion (index);
1875 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1877 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1881 /* An array that is indexed by a non-constant
1882 cannot be stored in a register; we must be able to do
1883 address arithmetic on its address.
1884 Likewise an array of elements of variable size. */
1885 if (TREE_CODE (index) != INTEGER_CST
1886 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1887 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1889 if (!c_mark_addressable (array))
1890 return error_mark_node;
1892 /* An array that is indexed by a constant value which is not within
1893 the array bounds cannot be stored in a register either; because we
1894 would get a crash in store_bit_field/extract_bit_field when trying
1895 to access a non-existent part of the register. */
1896 if (TREE_CODE (index) == INTEGER_CST
1897 && TYPE_DOMAIN (TREE_TYPE (array))
1898 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1900 if (!c_mark_addressable (array))
1901 return error_mark_node;
1907 while (TREE_CODE (foo) == COMPONENT_REF)
1908 foo = TREE_OPERAND (foo, 0);
1909 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1910 pedwarn ("ISO C forbids subscripting %<register%> array");
1911 else if (!flag_isoc99 && !lvalue_p (foo))
1912 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1915 type = TREE_TYPE (TREE_TYPE (array));
1916 if (TREE_CODE (type) != ARRAY_TYPE)
1917 type = TYPE_MAIN_VARIANT (type);
1918 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1919 /* Array ref is const/volatile if the array elements are
1920 or if the array is. */
1921 TREE_READONLY (rval)
1922 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1923 | TREE_READONLY (array));
1924 TREE_SIDE_EFFECTS (rval)
1925 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1926 | TREE_SIDE_EFFECTS (array));
1927 TREE_THIS_VOLATILE (rval)
1928 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1929 /* This was added by rms on 16 Nov 91.
1930 It fixes vol struct foo *a; a->elts[1]
1931 in an inline function.
1932 Hope it doesn't break something else. */
1933 | TREE_THIS_VOLATILE (array));
1934 return require_complete_type (fold (rval));
1938 tree ar = default_conversion (array);
1940 if (ar == error_mark_node)
1943 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1944 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1946 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1951 /* Build an external reference to identifier ID. FUN indicates
1952 whether this will be used for a function call. LOC is the source
1953 location of the identifier. */
1955 build_external_ref (tree id, int fun, location_t loc)
1958 tree decl = lookup_name (id);
1960 /* In Objective-C, an instance variable (ivar) may be preferred to
1961 whatever lookup_name() found. */
1962 decl = objc_lookup_ivar (decl, id);
1964 if (decl && decl != error_mark_node)
1967 /* Implicit function declaration. */
1968 ref = implicitly_declare (id);
1969 else if (decl == error_mark_node)
1970 /* Don't complain about something that's already been
1971 complained about. */
1972 return error_mark_node;
1975 undeclared_variable (id, loc);
1976 return error_mark_node;
1979 if (TREE_TYPE (ref) == error_mark_node)
1980 return error_mark_node;
1982 if (TREE_DEPRECATED (ref))
1983 warn_deprecated_use (ref);
1985 if (!skip_evaluation)
1986 assemble_external (ref);
1987 TREE_USED (ref) = 1;
1989 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1991 if (!in_sizeof && !in_typeof)
1992 C_DECL_USED (ref) = 1;
1993 else if (DECL_INITIAL (ref) == 0
1994 && DECL_EXTERNAL (ref)
1995 && !TREE_PUBLIC (ref))
1996 record_maybe_used_decl (ref);
1999 if (TREE_CODE (ref) == CONST_DECL)
2001 ref = DECL_INITIAL (ref);
2002 TREE_CONSTANT (ref) = 1;
2003 TREE_INVARIANT (ref) = 1;
2005 else if (current_function_decl != 0
2006 && !DECL_FILE_SCOPE_P (current_function_decl)
2007 && (TREE_CODE (ref) == VAR_DECL
2008 || TREE_CODE (ref) == PARM_DECL
2009 || TREE_CODE (ref) == FUNCTION_DECL))
2011 tree context = decl_function_context (ref);
2013 if (context != 0 && context != current_function_decl)
2014 DECL_NONLOCAL (ref) = 1;
2020 /* Record details of decls possibly used inside sizeof or typeof. */
2021 struct maybe_used_decl
2025 /* The level seen at (in_sizeof + in_typeof). */
2027 /* The next one at this level or above, or NULL. */
2028 struct maybe_used_decl *next;
2031 static struct maybe_used_decl *maybe_used_decls;
2033 /* Record that DECL, an undefined static function reference seen
2034 inside sizeof or typeof, might be used if the operand of sizeof is
2035 a VLA type or the operand of typeof is a variably modified
2039 record_maybe_used_decl (tree decl)
2041 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2043 t->level = in_sizeof + in_typeof;
2044 t->next = maybe_used_decls;
2045 maybe_used_decls = t;
2048 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2049 USED is false, just discard them. If it is true, mark them used
2050 (if no longer inside sizeof or typeof) or move them to the next
2051 level up (if still inside sizeof or typeof). */
2054 pop_maybe_used (bool used)
2056 struct maybe_used_decl *p = maybe_used_decls;
2057 int cur_level = in_sizeof + in_typeof;
2058 while (p && p->level > cur_level)
2063 C_DECL_USED (p->decl) = 1;
2065 p->level = cur_level;
2069 if (!used || cur_level == 0)
2070 maybe_used_decls = p;
2073 /* Return the result of sizeof applied to EXPR. */
2076 c_expr_sizeof_expr (struct c_expr expr)
2079 if (expr.value == error_mark_node)
2081 ret.value = error_mark_node;
2082 ret.original_code = ERROR_MARK;
2083 pop_maybe_used (false);
2087 ret.value = c_sizeof (TREE_TYPE (expr.value));
2088 ret.original_code = ERROR_MARK;
2089 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2094 /* Return the result of sizeof applied to T, a structure for the type
2095 name passed to sizeof (rather than the type itself). */
2098 c_expr_sizeof_type (struct c_type_name *t)
2102 type = groktypename (t);
2103 ret.value = c_sizeof (type);
2104 ret.original_code = ERROR_MARK;
2105 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
2109 /* Build a function call to function FUNCTION with parameters PARAMS.
2110 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2111 TREE_VALUE of each node is a parameter-expression.
2112 FUNCTION's data type may be a function type or a pointer-to-function. */
2115 build_function_call (tree function, tree params)
2117 tree fntype, fundecl = 0;
2118 tree coerced_params;
2119 tree name = NULL_TREE, result;
2122 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2123 STRIP_TYPE_NOPS (function);
2125 /* Convert anything with function type to a pointer-to-function. */
2126 if (TREE_CODE (function) == FUNCTION_DECL)
2128 /* Implement type-directed function overloading for builtins.
2129 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2130 handle all the type checking. The result is a complete expression
2131 that implements this function call. */
2132 tem = resolve_overloaded_builtin (function, params);
2136 name = DECL_NAME (function);
2139 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2140 function = function_to_pointer_conversion (function);
2142 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2143 expressions, like those used for ObjC messenger dispatches. */
2144 function = objc_rewrite_function_call (function, params);
2146 fntype = TREE_TYPE (function);
2148 if (TREE_CODE (fntype) == ERROR_MARK)
2149 return error_mark_node;
2151 if (!(TREE_CODE (fntype) == POINTER_TYPE
2152 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2154 error ("called object %qE is not a function", function);
2155 return error_mark_node;
2158 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2159 current_function_returns_abnormally = 1;
2161 /* fntype now gets the type of function pointed to. */
2162 fntype = TREE_TYPE (fntype);
2164 /* Check that the function is called through a compatible prototype.
2165 If it is not, replace the call by a trap, wrapped up in a compound
2166 expression if necessary. This has the nice side-effect to prevent
2167 the tree-inliner from generating invalid assignment trees which may
2168 blow up in the RTL expander later. */
2169 if (TREE_CODE (function) == NOP_EXPR
2170 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2171 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2172 && !comptypes (fntype, TREE_TYPE (tem)))
2174 tree return_type = TREE_TYPE (fntype);
2175 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2178 /* This situation leads to run-time undefined behavior. We can't,
2179 therefore, simply error unless we can prove that all possible
2180 executions of the program must execute the code. */
2181 warning (0, "function called through a non-compatible type");
2183 /* We can, however, treat "undefined" any way we please.
2184 Call abort to encourage the user to fix the program. */
2185 inform ("if this code is reached, the program will abort");
2187 if (VOID_TYPE_P (return_type))
2193 if (AGGREGATE_TYPE_P (return_type))
2194 rhs = build_compound_literal (return_type,
2195 build_constructor (return_type, 0));
2197 rhs = fold_build1 (NOP_EXPR, return_type, integer_zero_node);
2199 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2203 /* Convert the parameters to the types declared in the
2204 function prototype, or apply default promotions. */
2207 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2209 if (coerced_params == error_mark_node)
2210 return error_mark_node;
2212 /* Check that the arguments to the function are valid. */
2214 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2215 TYPE_ARG_TYPES (fntype));
2217 if (require_constant_value)
2219 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2220 function, coerced_params, NULL_TREE);
2222 if (TREE_CONSTANT (result)
2223 && (name == NULL_TREE
2224 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2225 pedwarn_init ("initializer element is not constant");
2228 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2229 function, coerced_params, NULL_TREE);
2231 if (VOID_TYPE_P (TREE_TYPE (result)))
2233 return require_complete_type (result);
2236 /* Convert the argument expressions in the list VALUES
2237 to the types in the list TYPELIST. The result is a list of converted
2238 argument expressions, unless there are too few arguments in which
2239 case it is error_mark_node.
2241 If TYPELIST is exhausted, or when an element has NULL as its type,
2242 perform the default conversions.
2244 PARMLIST is the chain of parm decls for the function being called.
2245 It may be 0, if that info is not available.
2246 It is used only for generating error messages.
2248 FUNCTION is a tree for the called function. It is used only for
2249 error messages, where it is formatted with %qE.
2251 This is also where warnings about wrong number of args are generated.
2253 Both VALUES and the returned value are chains of TREE_LIST nodes
2254 with the elements of the list in the TREE_VALUE slots of those nodes. */
2257 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2259 tree typetail, valtail;
2264 /* Change pointer to function to the function itself for
2266 if (TREE_CODE (function) == ADDR_EXPR
2267 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2268 function = TREE_OPERAND (function, 0);
2270 /* Handle an ObjC selector specially for diagnostics. */
2271 selector = objc_message_selector ();
2273 /* Scan the given expressions and types, producing individual
2274 converted arguments and pushing them on RESULT in reverse order. */
2276 for (valtail = values, typetail = typelist, parmnum = 0;
2278 valtail = TREE_CHAIN (valtail), parmnum++)
2280 tree type = typetail ? TREE_VALUE (typetail) : 0;
2281 tree val = TREE_VALUE (valtail);
2282 tree rname = function;
2283 int argnum = parmnum + 1;
2284 const char *invalid_func_diag;
2286 if (type == void_type_node)
2288 error ("too many arguments to function %qE", function);
2292 if (selector && argnum > 2)
2298 STRIP_TYPE_NOPS (val);
2300 val = require_complete_type (val);
2304 /* Formal parm type is specified by a function prototype. */
2307 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2309 error ("type of formal parameter %d is incomplete", parmnum + 1);
2314 /* Optionally warn about conversions that
2315 differ from the default conversions. */
2316 if (warn_conversion || warn_traditional)
2318 unsigned int formal_prec = TYPE_PRECISION (type);
2320 if (INTEGRAL_TYPE_P (type)
2321 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2322 warning (0, "passing argument %d of %qE as integer "
2323 "rather than floating due to prototype",
2325 if (INTEGRAL_TYPE_P (type)
2326 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2327 warning (0, "passing argument %d of %qE as integer "
2328 "rather than complex due to prototype",
2330 else if (TREE_CODE (type) == COMPLEX_TYPE
2331 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2332 warning (0, "passing argument %d of %qE as complex "
2333 "rather than floating due to prototype",
2335 else if (TREE_CODE (type) == REAL_TYPE
2336 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2337 warning (0, "passing argument %d of %qE as floating "
2338 "rather than integer due to prototype",
2340 else if (TREE_CODE (type) == COMPLEX_TYPE
2341 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2342 warning (0, "passing argument %d of %qE as complex "
2343 "rather than integer due to prototype",
2345 else if (TREE_CODE (type) == REAL_TYPE
2346 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2347 warning (0, "passing argument %d of %qE as floating "
2348 "rather than complex due to prototype",
2350 /* ??? At some point, messages should be written about
2351 conversions between complex types, but that's too messy
2353 else if (TREE_CODE (type) == REAL_TYPE
2354 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2356 /* Warn if any argument is passed as `float',
2357 since without a prototype it would be `double'. */
2358 if (formal_prec == TYPE_PRECISION (float_type_node))
2359 warning (0, "passing argument %d of %qE as %<float%> "
2360 "rather than %<double%> due to prototype",
2363 /* Detect integer changing in width or signedness.
2364 These warnings are only activated with
2365 -Wconversion, not with -Wtraditional. */
2366 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2367 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2369 tree would_have_been = default_conversion (val);
2370 tree type1 = TREE_TYPE (would_have_been);
2372 if (TREE_CODE (type) == ENUMERAL_TYPE
2373 && (TYPE_MAIN_VARIANT (type)
2374 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2375 /* No warning if function asks for enum
2376 and the actual arg is that enum type. */
2378 else if (formal_prec != TYPE_PRECISION (type1))
2379 warning (OPT_Wconversion, "passing argument %d of %qE "
2380 "with different width due to prototype",
2382 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2384 /* Don't complain if the formal parameter type
2385 is an enum, because we can't tell now whether
2386 the value was an enum--even the same enum. */
2387 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2389 else if (TREE_CODE (val) == INTEGER_CST
2390 && int_fits_type_p (val, type))
2391 /* Change in signedness doesn't matter
2392 if a constant value is unaffected. */
2394 /* If the value is extended from a narrower
2395 unsigned type, it doesn't matter whether we
2396 pass it as signed or unsigned; the value
2397 certainly is the same either way. */
2398 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2399 && TYPE_UNSIGNED (TREE_TYPE (val)))
2401 else if (TYPE_UNSIGNED (type))
2402 warning (OPT_Wconversion, "passing argument %d of %qE "
2403 "as unsigned due to prototype",
2406 warning (OPT_Wconversion, "passing argument %d of %qE "
2407 "as signed due to prototype", argnum, rname);
2411 parmval = convert_for_assignment (type, val, ic_argpass,
2415 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2416 && INTEGRAL_TYPE_P (type)
2417 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2418 parmval = default_conversion (parmval);
2420 result = tree_cons (NULL_TREE, parmval, result);
2422 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2423 && (TYPE_PRECISION (TREE_TYPE (val))
2424 < TYPE_PRECISION (double_type_node)))
2425 /* Convert `float' to `double'. */
2426 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2427 else if ((invalid_func_diag =
2428 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2430 error (invalid_func_diag);
2431 return error_mark_node;
2434 /* Convert `short' and `char' to full-size `int'. */
2435 result = tree_cons (NULL_TREE, default_conversion (val), result);
2438 typetail = TREE_CHAIN (typetail);
2441 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2443 error ("too few arguments to function %qE", function);
2444 return error_mark_node;
2447 return nreverse (result);
2450 /* This is the entry point used by the parser to build unary operators
2451 in the input. CODE, a tree_code, specifies the unary operator, and
2452 ARG is the operand. For unary plus, the C parser currently uses
2453 CONVERT_EXPR for code. */
2456 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2458 struct c_expr result;
2460 result.original_code = ERROR_MARK;
2461 result.value = build_unary_op (code, arg.value, 0);
2462 overflow_warning (result.value);
2466 /* This is the entry point used by the parser to build binary operators
2467 in the input. CODE, a tree_code, specifies the binary operator, and
2468 ARG1 and ARG2 are the operands. In addition to constructing the
2469 expression, we check for operands that were written with other binary
2470 operators in a way that is likely to confuse the user. */
2473 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2476 struct c_expr result;
2478 enum tree_code code1 = arg1.original_code;
2479 enum tree_code code2 = arg2.original_code;
2481 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2482 result.original_code = code;
2484 if (TREE_CODE (result.value) == ERROR_MARK)
2487 /* Check for cases such as x+y<<z which users are likely
2489 if (warn_parentheses)
2491 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2493 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2494 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2495 warning (OPT_Wparentheses,
2496 "suggest parentheses around + or - inside shift");
2499 if (code == TRUTH_ORIF_EXPR)
2501 if (code1 == TRUTH_ANDIF_EXPR
2502 || code2 == TRUTH_ANDIF_EXPR)
2503 warning (OPT_Wparentheses,
2504 "suggest parentheses around && within ||");
2507 if (code == BIT_IOR_EXPR)
2509 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2510 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2511 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2512 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2513 warning (OPT_Wparentheses,
2514 "suggest parentheses around arithmetic in operand of |");
2515 /* Check cases like x|y==z */
2516 if (TREE_CODE_CLASS (code1) == tcc_comparison
2517 || TREE_CODE_CLASS (code2) == tcc_comparison)
2518 warning (OPT_Wparentheses,
2519 "suggest parentheses around comparison in operand of |");
2522 if (code == BIT_XOR_EXPR)
2524 if (code1 == BIT_AND_EXPR
2525 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2526 || code2 == BIT_AND_EXPR
2527 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2528 warning (OPT_Wparentheses,
2529 "suggest parentheses around arithmetic in operand of ^");
2530 /* Check cases like x^y==z */
2531 if (TREE_CODE_CLASS (code1) == tcc_comparison
2532 || TREE_CODE_CLASS (code2) == tcc_comparison)
2533 warning (OPT_Wparentheses,
2534 "suggest parentheses around comparison in operand of ^");
2537 if (code == BIT_AND_EXPR)
2539 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2540 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2541 warning (OPT_Wparentheses,
2542 "suggest parentheses around + or - in operand of &");
2543 /* Check cases like x&y==z */
2544 if (TREE_CODE_CLASS (code1) == tcc_comparison
2545 || TREE_CODE_CLASS (code2) == tcc_comparison)
2546 warning (OPT_Wparentheses,
2547 "suggest parentheses around comparison in operand of &");
2549 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2550 if (TREE_CODE_CLASS (code) == tcc_comparison
2551 && (TREE_CODE_CLASS (code1) == tcc_comparison
2552 || TREE_CODE_CLASS (code2) == tcc_comparison))
2553 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2554 "have their mathematical meaning");
2558 unsigned_conversion_warning (result.value, arg1.value);
2559 unsigned_conversion_warning (result.value, arg2.value);
2560 overflow_warning (result.value);
2565 /* Return a tree for the difference of pointers OP0 and OP1.
2566 The resulting tree has type int. */
2569 pointer_diff (tree op0, tree op1)
2571 tree restype = ptrdiff_type_node;
2573 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2574 tree con0, con1, lit0, lit1;
2575 tree orig_op1 = op1;
2577 if (pedantic || warn_pointer_arith)
2579 if (TREE_CODE (target_type) == VOID_TYPE)
2580 pedwarn ("pointer of type %<void *%> used in subtraction");
2581 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2582 pedwarn ("pointer to a function used in subtraction");
2585 /* If the conversion to ptrdiff_type does anything like widening or
2586 converting a partial to an integral mode, we get a convert_expression
2587 that is in the way to do any simplifications.
2588 (fold-const.c doesn't know that the extra bits won't be needed.
2589 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2590 different mode in place.)
2591 So first try to find a common term here 'by hand'; we want to cover
2592 at least the cases that occur in legal static initializers. */
2593 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2594 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2596 if (TREE_CODE (con0) == PLUS_EXPR)
2598 lit0 = TREE_OPERAND (con0, 1);
2599 con0 = TREE_OPERAND (con0, 0);
2602 lit0 = integer_zero_node;
2604 if (TREE_CODE (con1) == PLUS_EXPR)
2606 lit1 = TREE_OPERAND (con1, 1);
2607 con1 = TREE_OPERAND (con1, 0);
2610 lit1 = integer_zero_node;
2612 if (operand_equal_p (con0, con1, 0))
2619 /* First do the subtraction as integers;
2620 then drop through to build the divide operator.
2621 Do not do default conversions on the minus operator
2622 in case restype is a short type. */
2624 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2625 convert (restype, op1), 0);
2626 /* This generates an error if op1 is pointer to incomplete type. */
2627 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2628 error ("arithmetic on pointer to an incomplete type");
2630 /* This generates an error if op0 is pointer to incomplete type. */
2631 op1 = c_size_in_bytes (target_type);
2633 /* Divide by the size, in easiest possible way. */
2634 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2637 /* Construct and perhaps optimize a tree representation
2638 for a unary operation. CODE, a tree_code, specifies the operation
2639 and XARG is the operand.
2640 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2641 the default promotions (such as from short to int).
2642 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2643 allows non-lvalues; this is only used to handle conversion of non-lvalue
2644 arrays to pointers in C99. */
2647 build_unary_op (enum tree_code code, tree xarg, int flag)
2649 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2652 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2654 int noconvert = flag;
2655 const char *invalid_op_diag;
2657 if (typecode == ERROR_MARK)
2658 return error_mark_node;
2659 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2660 typecode = INTEGER_TYPE;
2662 if ((invalid_op_diag
2663 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2665 error (invalid_op_diag);
2666 return error_mark_node;
2672 /* This is used for unary plus, because a CONVERT_EXPR
2673 is enough to prevent anybody from looking inside for
2674 associativity, but won't generate any code. */
2675 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2676 || typecode == COMPLEX_TYPE
2677 || typecode == VECTOR_TYPE))
2679 error ("wrong type argument to unary plus");
2680 return error_mark_node;
2682 else if (!noconvert)
2683 arg = default_conversion (arg);
2684 arg = non_lvalue (arg);
2688 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2689 || typecode == COMPLEX_TYPE
2690 || typecode == VECTOR_TYPE))
2692 error ("wrong type argument to unary minus");
2693 return error_mark_node;
2695 else if (!noconvert)
2696 arg = default_conversion (arg);
2700 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2703 arg = default_conversion (arg);
2705 else if (typecode == COMPLEX_TYPE)
2709 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2711 arg = default_conversion (arg);
2715 error ("wrong type argument to bit-complement");
2716 return error_mark_node;
2721 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2723 error ("wrong type argument to abs");
2724 return error_mark_node;
2726 else if (!noconvert)
2727 arg = default_conversion (arg);
2731 /* Conjugating a real value is a no-op, but allow it anyway. */
2732 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2733 || typecode == COMPLEX_TYPE))
2735 error ("wrong type argument to conjugation");
2736 return error_mark_node;
2738 else if (!noconvert)
2739 arg = default_conversion (arg);
2742 case TRUTH_NOT_EXPR:
2743 if (typecode != INTEGER_TYPE
2744 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2745 && typecode != COMPLEX_TYPE)
2747 error ("wrong type argument to unary exclamation mark");
2748 return error_mark_node;
2750 arg = c_objc_common_truthvalue_conversion (arg);
2751 return invert_truthvalue (arg);
2757 if (TREE_CODE (arg) == COMPLEX_CST)
2758 return TREE_REALPART (arg);
2759 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2760 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2765 if (TREE_CODE (arg) == COMPLEX_CST)
2766 return TREE_IMAGPART (arg);
2767 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2768 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2770 return convert (TREE_TYPE (arg), integer_zero_node);
2772 case PREINCREMENT_EXPR:
2773 case POSTINCREMENT_EXPR:
2774 case PREDECREMENT_EXPR:
2775 case POSTDECREMENT_EXPR:
2777 /* Increment or decrement the real part of the value,
2778 and don't change the imaginary part. */
2779 if (typecode == COMPLEX_TYPE)
2784 pedwarn ("ISO C does not support %<++%> and %<--%>"
2785 " on complex types");
2787 arg = stabilize_reference (arg);
2788 real = build_unary_op (REALPART_EXPR, arg, 1);
2789 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2790 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2791 build_unary_op (code, real, 1), imag);
2794 /* Report invalid types. */
2796 if (typecode != POINTER_TYPE
2797 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2799 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2800 error ("wrong type argument to increment");
2802 error ("wrong type argument to decrement");
2804 return error_mark_node;
2809 tree result_type = TREE_TYPE (arg);
2811 arg = get_unwidened (arg, 0);
2812 argtype = TREE_TYPE (arg);
2814 /* Compute the increment. */
2816 if (typecode == POINTER_TYPE)
2818 /* If pointer target is an undefined struct,
2819 we just cannot know how to do the arithmetic. */
2820 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2822 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2823 error ("increment of pointer to unknown structure");
2825 error ("decrement of pointer to unknown structure");
2827 else if ((pedantic || warn_pointer_arith)
2828 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2829 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2831 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2832 pedwarn ("wrong type argument to increment");
2834 pedwarn ("wrong type argument to decrement");
2837 inc = c_size_in_bytes (TREE_TYPE (result_type));
2840 inc = integer_one_node;
2842 inc = convert (argtype, inc);
2844 /* Complain about anything else that is not a true lvalue. */
2845 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2846 || code == POSTINCREMENT_EXPR)
2849 return error_mark_node;
2851 /* Report a read-only lvalue. */
2852 if (TREE_READONLY (arg))
2853 readonly_error (arg,
2854 ((code == PREINCREMENT_EXPR
2855 || code == POSTINCREMENT_EXPR)
2856 ? lv_increment : lv_decrement));
2858 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2859 val = boolean_increment (code, arg);
2861 val = build2 (code, TREE_TYPE (arg), arg, inc);
2862 TREE_SIDE_EFFECTS (val) = 1;
2863 val = convert (result_type, val);
2864 if (TREE_CODE (val) != code)
2865 TREE_NO_WARNING (val) = 1;
2870 /* Note that this operation never does default_conversion. */
2872 /* Let &* cancel out to simplify resulting code. */
2873 if (TREE_CODE (arg) == INDIRECT_REF)
2875 /* Don't let this be an lvalue. */
2876 if (lvalue_p (TREE_OPERAND (arg, 0)))
2877 return non_lvalue (TREE_OPERAND (arg, 0));
2878 return TREE_OPERAND (arg, 0);
2881 /* For &x[y], return x+y */
2882 if (TREE_CODE (arg) == ARRAY_REF)
2884 tree op0 = TREE_OPERAND (arg, 0);
2885 if (!c_mark_addressable (op0))
2886 return error_mark_node;
2887 return build_binary_op (PLUS_EXPR,
2888 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
2889 ? array_to_pointer_conversion (op0)
2891 TREE_OPERAND (arg, 1), 1);
2894 /* Anything not already handled and not a true memory reference
2895 or a non-lvalue array is an error. */
2896 else if (typecode != FUNCTION_TYPE && !flag
2897 && !lvalue_or_else (arg, lv_addressof))
2898 return error_mark_node;
2900 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2901 argtype = TREE_TYPE (arg);
2903 /* If the lvalue is const or volatile, merge that into the type
2904 to which the address will point. Note that you can't get a
2905 restricted pointer by taking the address of something, so we
2906 only have to deal with `const' and `volatile' here. */
2907 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2908 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2909 argtype = c_build_type_variant (argtype,
2910 TREE_READONLY (arg),
2911 TREE_THIS_VOLATILE (arg));
2913 if (!c_mark_addressable (arg))
2914 return error_mark_node;
2916 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2917 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2919 argtype = build_pointer_type (argtype);
2921 /* ??? Cope with user tricks that amount to offsetof. Delete this
2922 when we have proper support for integer constant expressions. */
2923 val = get_base_address (arg);
2924 if (val && TREE_CODE (val) == INDIRECT_REF
2925 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
2927 tree op0 = fold_convert (argtype, fold_offsetof (arg)), op1;
2929 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
2930 return fold_build2 (PLUS_EXPR, argtype, op0, op1);
2933 val = build1 (ADDR_EXPR, argtype, arg);
2942 argtype = TREE_TYPE (arg);
2943 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
2944 : fold_build1 (code, argtype, arg);
2947 /* Return nonzero if REF is an lvalue valid for this language.
2948 Lvalues can be assigned, unless their type has TYPE_READONLY.
2949 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2954 enum tree_code code = TREE_CODE (ref);
2961 return lvalue_p (TREE_OPERAND (ref, 0));
2963 case COMPOUND_LITERAL_EXPR:
2973 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2974 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2977 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2984 /* Give an error for storing in something that is 'const'. */
2987 readonly_error (tree arg, enum lvalue_use use)
2989 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
2991 /* Using this macro rather than (for example) arrays of messages
2992 ensures that all the format strings are checked at compile
2994 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
2995 : (use == lv_increment ? (I) \
2996 : (use == lv_decrement ? (D) : (AS))))
2997 if (TREE_CODE (arg) == COMPONENT_REF)
2999 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3000 readonly_error (TREE_OPERAND (arg, 0), use);
3002 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3003 G_("increment of read-only member %qD"),
3004 G_("decrement of read-only member %qD"),
3005 G_("read-only member %qD used as %<asm%> output")),
3006 TREE_OPERAND (arg, 1));
3008 else if (TREE_CODE (arg) == VAR_DECL)
3009 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3010 G_("increment of read-only variable %qD"),
3011 G_("decrement of read-only variable %qD"),
3012 G_("read-only variable %qD used as %<asm%> output")),
3015 error (READONLY_MSG (G_("assignment of read-only location"),
3016 G_("increment of read-only location"),
3017 G_("decrement of read-only location"),
3018 G_("read-only location used as %<asm%> output")));
3022 /* Return nonzero if REF is an lvalue valid for this language;
3023 otherwise, print an error message and return zero. USE says
3024 how the lvalue is being used and so selects the error message. */
3027 lvalue_or_else (tree ref, enum lvalue_use use)
3029 int win = lvalue_p (ref);
3037 /* Mark EXP saying that we need to be able to take the
3038 address of it; it should not be allocated in a register.
3039 Returns true if successful. */
3042 c_mark_addressable (tree exp)
3047 switch (TREE_CODE (x))
3050 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3053 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3057 /* ... fall through ... */
3063 x = TREE_OPERAND (x, 0);
3066 case COMPOUND_LITERAL_EXPR:
3068 TREE_ADDRESSABLE (x) = 1;
3075 if (C_DECL_REGISTER (x)
3076 && DECL_NONLOCAL (x))
3078 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3081 ("global register variable %qD used in nested function", x);
3084 pedwarn ("register variable %qD used in nested function", x);
3086 else if (C_DECL_REGISTER (x))
3088 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3089 error ("address of global register variable %qD requested", x);
3091 error ("address of register variable %qD requested", x);
3097 TREE_ADDRESSABLE (x) = 1;
3104 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3107 build_conditional_expr (tree ifexp, tree op1, tree op2)
3111 enum tree_code code1;
3112 enum tree_code code2;
3113 tree result_type = NULL;
3114 tree orig_op1 = op1, orig_op2 = op2;
3116 /* Promote both alternatives. */
3118 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3119 op1 = default_conversion (op1);
3120 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3121 op2 = default_conversion (op2);
3123 if (TREE_CODE (ifexp) == ERROR_MARK
3124 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3125 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3126 return error_mark_node;
3128 type1 = TREE_TYPE (op1);
3129 code1 = TREE_CODE (type1);
3130 type2 = TREE_TYPE (op2);
3131 code2 = TREE_CODE (type2);
3133 /* C90 does not permit non-lvalue arrays in conditional expressions.
3134 In C99 they will be pointers by now. */
3135 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3137 error ("non-lvalue array in conditional expression");
3138 return error_mark_node;
3141 /* Quickly detect the usual case where op1 and op2 have the same type
3143 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3146 result_type = type1;
3148 result_type = TYPE_MAIN_VARIANT (type1);
3150 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3151 || code1 == COMPLEX_TYPE)
3152 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3153 || code2 == COMPLEX_TYPE))
3155 result_type = c_common_type (type1, type2);
3157 /* If -Wsign-compare, warn here if type1 and type2 have
3158 different signedness. We'll promote the signed to unsigned
3159 and later code won't know it used to be different.
3160 Do this check on the original types, so that explicit casts
3161 will be considered, but default promotions won't. */
3162 if (warn_sign_compare && !skip_evaluation)
3164 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3165 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3167 if (unsigned_op1 ^ unsigned_op2)
3169 /* Do not warn if the result type is signed, since the
3170 signed type will only be chosen if it can represent
3171 all the values of the unsigned type. */
3172 if (!TYPE_UNSIGNED (result_type))
3174 /* Do not warn if the signed quantity is an unsuffixed
3175 integer literal (or some static constant expression
3176 involving such literals) and it is non-negative. */
3177 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3178 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3181 warning (0, "signed and unsigned type in conditional expression");
3185 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3187 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3188 pedwarn ("ISO C forbids conditional expr with only one void side");
3189 result_type = void_type_node;
3191 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3193 if (comp_target_types (type1, type2))
3194 result_type = common_pointer_type (type1, type2);
3195 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3196 && TREE_CODE (orig_op1) != NOP_EXPR)
3197 result_type = qualify_type (type2, type1);
3198 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3199 && TREE_CODE (orig_op2) != NOP_EXPR)
3200 result_type = qualify_type (type1, type2);
3201 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3203 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3204 pedwarn ("ISO C forbids conditional expr between "
3205 "%<void *%> and function pointer");
3206 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3207 TREE_TYPE (type2)));
3209 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3211 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3212 pedwarn ("ISO C forbids conditional expr between "
3213 "%<void *%> and function pointer");
3214 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3215 TREE_TYPE (type1)));
3219 pedwarn ("pointer type mismatch in conditional expression");
3220 result_type = build_pointer_type (void_type_node);
3223 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3225 if (!integer_zerop (op2))
3226 pedwarn ("pointer/integer type mismatch in conditional expression");
3229 op2 = null_pointer_node;
3231 result_type = type1;
3233 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3235 if (!integer_zerop (op1))
3236 pedwarn ("pointer/integer type mismatch in conditional expression");
3239 op1 = null_pointer_node;
3241 result_type = type2;
3246 if (flag_cond_mismatch)
3247 result_type = void_type_node;
3250 error ("type mismatch in conditional expression");
3251 return error_mark_node;
3255 /* Merge const and volatile flags of the incoming types. */
3257 = build_type_variant (result_type,
3258 TREE_READONLY (op1) || TREE_READONLY (op2),
3259 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3261 if (result_type != TREE_TYPE (op1))
3262 op1 = convert_and_check (result_type, op1);
3263 if (result_type != TREE_TYPE (op2))
3264 op2 = convert_and_check (result_type, op2);
3266 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3269 /* Return a compound expression that performs two expressions and
3270 returns the value of the second of them. */
3273 build_compound_expr (tree expr1, tree expr2)
3275 if (!TREE_SIDE_EFFECTS (expr1))
3277 /* The left-hand operand of a comma expression is like an expression
3278 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3279 any side-effects, unless it was explicitly cast to (void). */
3280 if (warn_unused_value)
3282 if (VOID_TYPE_P (TREE_TYPE (expr1))
3283 && TREE_CODE (expr1) == CONVERT_EXPR)
3285 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3286 && TREE_CODE (expr1) == COMPOUND_EXPR
3287 && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
3288 ; /* (void) a, (void) b, c */
3290 warning (0, "left-hand operand of comma expression has no effect");
3294 /* With -Wunused, we should also warn if the left-hand operand does have
3295 side-effects, but computes a value which is not used. For example, in
3296 `foo() + bar(), baz()' the result of the `+' operator is not used,
3297 so we should issue a warning. */
3298 else if (warn_unused_value)
3299 warn_if_unused_value (expr1, input_location);
3301 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3304 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3307 build_c_cast (tree type, tree expr)
3311 if (type == error_mark_node || expr == error_mark_node)
3312 return error_mark_node;
3314 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3315 only in <protocol> qualifications. But when constructing cast expressions,
3316 the protocols do matter and must be kept around. */
3317 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3318 return build1 (NOP_EXPR, type, expr);
3320 type = TYPE_MAIN_VARIANT (type);
3322 if (TREE_CODE (type) == ARRAY_TYPE)
3324 error ("cast specifies array type");
3325 return error_mark_node;
3328 if (TREE_CODE (type) == FUNCTION_TYPE)
3330 error ("cast specifies function type");
3331 return error_mark_node;
3334 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3338 if (TREE_CODE (type) == RECORD_TYPE
3339 || TREE_CODE (type) == UNION_TYPE)
3340 pedwarn ("ISO C forbids casting nonscalar to the same type");
3343 else if (TREE_CODE (type) == UNION_TYPE)
3347 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3348 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3349 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3357 pedwarn ("ISO C forbids casts to union type");
3358 t = digest_init (type,
3359 build_constructor_single (type, field, value),
3361 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3362 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3365 error ("cast to union type from type not present in union");
3366 return error_mark_node;
3372 if (type == void_type_node)
3373 return build1 (CONVERT_EXPR, type, value);
3375 otype = TREE_TYPE (value);
3377 /* Optionally warn about potentially worrisome casts. */
3380 && TREE_CODE (type) == POINTER_TYPE
3381 && TREE_CODE (otype) == POINTER_TYPE)
3383 tree in_type = type;
3384 tree in_otype = otype;
3388 /* Check that the qualifiers on IN_TYPE are a superset of
3389 the qualifiers of IN_OTYPE. The outermost level of
3390 POINTER_TYPE nodes is uninteresting and we stop as soon
3391 as we hit a non-POINTER_TYPE node on either type. */
3394 in_otype = TREE_TYPE (in_otype);
3395 in_type = TREE_TYPE (in_type);
3397 /* GNU C allows cv-qualified function types. 'const'
3398 means the function is very pure, 'volatile' means it
3399 can't return. We need to warn when such qualifiers
3400 are added, not when they're taken away. */
3401 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3402 && TREE_CODE (in_type) == FUNCTION_TYPE)
3403 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3405 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3407 while (TREE_CODE (in_type) == POINTER_TYPE
3408 && TREE_CODE (in_otype) == POINTER_TYPE);
3411 warning (0, "cast adds new qualifiers to function type");
3414 /* There are qualifiers present in IN_OTYPE that are not
3415 present in IN_TYPE. */
3416 warning (0, "cast discards qualifiers from pointer target type");
3419 /* Warn about possible alignment problems. */
3420 if (STRICT_ALIGNMENT
3421 && TREE_CODE (type) == POINTER_TYPE
3422 && TREE_CODE (otype) == POINTER_TYPE
3423 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3424 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3425 /* Don't warn about opaque types, where the actual alignment
3426 restriction is unknown. */
3427 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3428 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3429 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3430 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3431 warning (OPT_Wcast_align,
3432 "cast increases required alignment of target type");
3434 if (TREE_CODE (type) == INTEGER_TYPE
3435 && TREE_CODE (otype) == POINTER_TYPE
3436 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3437 && !TREE_CONSTANT (value))
3438 warning (OPT_Wpointer_to_int_cast,
3439 "cast from pointer to integer of different size");
3441 if (TREE_CODE (value) == CALL_EXPR
3442 && TREE_CODE (type) != TREE_CODE (otype))
3443 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3444 "to non-matching type %qT", otype, type);
3446 if (TREE_CODE (type) == POINTER_TYPE
3447 && TREE_CODE (otype) == INTEGER_TYPE
3448 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3449 /* Don't warn about converting any constant. */
3450 && !TREE_CONSTANT (value))
3451 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3452 "of different size");
3454 strict_aliasing_warning (otype, type, expr);
3456 /* If pedantic, warn for conversions between function and object
3457 pointer types, except for converting a null pointer constant
3458 to function pointer type. */
3460 && TREE_CODE (type) == POINTER_TYPE
3461 && TREE_CODE (otype) == POINTER_TYPE
3462 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3463 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3464 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3467 && TREE_CODE (type) == POINTER_TYPE
3468 && TREE_CODE (otype) == POINTER_TYPE
3469 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3470 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3471 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3472 && TREE_CODE (expr) != NOP_EXPR))
3473 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3476 value = convert (type, value);
3478 /* Ignore any integer overflow caused by the cast. */
3479 if (TREE_CODE (value) == INTEGER_CST)
3481 if (CONSTANT_CLASS_P (ovalue)
3482 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3484 /* Avoid clobbering a shared constant. */
3485 value = copy_node (value);
3486 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3487 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3489 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3490 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3491 value = build_int_cst_wide (TREE_TYPE (value),
3492 TREE_INT_CST_LOW (value),
3493 TREE_INT_CST_HIGH (value));
3497 /* Don't let a cast be an lvalue. */
3499 value = non_lvalue (value);
3504 /* Interpret a cast of expression EXPR to type TYPE. */
3506 c_cast_expr (struct c_type_name *type_name, tree expr)
3509 int saved_wsp = warn_strict_prototypes;
3511 /* This avoids warnings about unprototyped casts on
3512 integers. E.g. "#define SIG_DFL (void(*)())0". */
3513 if (TREE_CODE (expr) == INTEGER_CST)
3514 warn_strict_prototypes = 0;
3515 type = groktypename (type_name);
3516 warn_strict_prototypes = saved_wsp;
3518 return build_c_cast (type, expr);
3522 /* Build an assignment expression of lvalue LHS from value RHS.
3523 MODIFYCODE is the code for a binary operator that we use
3524 to combine the old value of LHS with RHS to get the new value.
3525 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3528 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3532 tree lhstype = TREE_TYPE (lhs);
3533 tree olhstype = lhstype;
3535 /* Types that aren't fully specified cannot be used in assignments. */
3536 lhs = require_complete_type (lhs);
3538 /* Avoid duplicate error messages from operands that had errors. */
3539 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3540 return error_mark_node;
3542 STRIP_TYPE_NOPS (rhs);
3546 /* If a binary op has been requested, combine the old LHS value with the RHS
3547 producing the value we should actually store into the LHS. */
3549 if (modifycode != NOP_EXPR)
3551 lhs = stabilize_reference (lhs);
3552 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3555 if (!lvalue_or_else (lhs, lv_assign))
3556 return error_mark_node;
3558 /* Give an error for storing in something that is 'const'. */
3560 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3561 || ((TREE_CODE (lhstype) == RECORD_TYPE
3562 || TREE_CODE (lhstype) == UNION_TYPE)
3563 && C_TYPE_FIELDS_READONLY (lhstype)))
3564 readonly_error (lhs, lv_assign);
3566 /* If storing into a structure or union member,
3567 it has probably been given type `int'.
3568 Compute the type that would go with
3569 the actual amount of storage the member occupies. */
3571 if (TREE_CODE (lhs) == COMPONENT_REF
3572 && (TREE_CODE (lhstype) == INTEGER_TYPE
3573 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3574 || TREE_CODE (lhstype) == REAL_TYPE
3575 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3576 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3578 /* If storing in a field that is in actuality a short or narrower than one,
3579 we must store in the field in its actual type. */
3581 if (lhstype != TREE_TYPE (lhs))
3583 lhs = copy_node (lhs);
3584 TREE_TYPE (lhs) = lhstype;
3587 /* Convert new value to destination type. */
3589 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3590 NULL_TREE, NULL_TREE, 0);
3591 if (TREE_CODE (newrhs) == ERROR_MARK)
3592 return error_mark_node;
3594 /* Emit ObjC write barrier, if necessary. */
3595 if (c_dialect_objc () && flag_objc_gc)
3597 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3602 /* Scan operands. */
3604 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3605 TREE_SIDE_EFFECTS (result) = 1;
3607 /* If we got the LHS in a different type for storing in,
3608 convert the result back to the nominal type of LHS
3609 so that the value we return always has the same type
3610 as the LHS argument. */
3612 if (olhstype == TREE_TYPE (result))
3614 return convert_for_assignment (olhstype, result, ic_assign,
3615 NULL_TREE, NULL_TREE, 0);
3618 /* Convert value RHS to type TYPE as preparation for an assignment
3619 to an lvalue of type TYPE.
3620 The real work of conversion is done by `convert'.
3621 The purpose of this function is to generate error messages
3622 for assignments that are not allowed in C.
3623 ERRTYPE says whether it is argument passing, assignment,
3624 initialization or return.
3626 FUNCTION is a tree for the function being called.
3627 PARMNUM is the number of the argument, for printing in error messages. */
3630 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3631 tree fundecl, tree function, int parmnum)
3633 enum tree_code codel = TREE_CODE (type);
3635 enum tree_code coder;
3636 tree rname = NULL_TREE;
3637 bool objc_ok = false;
3639 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3642 /* Change pointer to function to the function itself for
3644 if (TREE_CODE (function) == ADDR_EXPR
3645 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3646 function = TREE_OPERAND (function, 0);
3648 /* Handle an ObjC selector specially for diagnostics. */
3649 selector = objc_message_selector ();
3651 if (selector && parmnum > 2)
3658 /* This macro is used to emit diagnostics to ensure that all format
3659 strings are complete sentences, visible to gettext and checked at
3661 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3666 pedwarn (AR, parmnum, rname); \
3668 case ic_argpass_nonproto: \
3669 warning (0, AR, parmnum, rname); \
3681 gcc_unreachable (); \
3685 STRIP_TYPE_NOPS (rhs);
3687 if (optimize && TREE_CODE (rhs) == VAR_DECL
3688 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3689 rhs = decl_constant_value_for_broken_optimization (rhs);
3691 rhstype = TREE_TYPE (rhs);
3692 coder = TREE_CODE (rhstype);
3694 if (coder == ERROR_MARK)
3695 return error_mark_node;
3697 if (c_dialect_objc ())
3720 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3723 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3725 overflow_warning (rhs);
3729 if (coder == VOID_TYPE)
3731 /* Except for passing an argument to an unprototyped function,
3732 this is a constraint violation. When passing an argument to
3733 an unprototyped function, it is compile-time undefined;
3734 making it a constraint in that case was rejected in
3736 error ("void value not ignored as it ought to be");
3737 return error_mark_node;
3739 /* A type converts to a reference to it.
3740 This code doesn't fully support references, it's just for the
3741 special case of va_start and va_copy. */
3742 if (codel == REFERENCE_TYPE
3743 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3745 if (!lvalue_p (rhs))
3747 error ("cannot pass rvalue to reference parameter");
3748 return error_mark_node;
3750 if (!c_mark_addressable (rhs))
3751 return error_mark_node;
3752 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3754 /* We already know that these two types are compatible, but they
3755 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3756 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3757 likely to be va_list, a typedef to __builtin_va_list, which
3758 is different enough that it will cause problems later. */
3759 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3760 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3762 rhs = build1 (NOP_EXPR, type, rhs);
3765 /* Some types can interconvert without explicit casts. */
3766 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3767 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3768 return convert (type, rhs);
3769 /* Arithmetic types all interconvert, and enum is treated like int. */
3770 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3771 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3772 || codel == BOOLEAN_TYPE)
3773 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3774 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3775 || coder == BOOLEAN_TYPE))
3776 return convert_and_check (type, rhs);
3778 /* Conversion to a transparent union from its member types.
3779 This applies only to function arguments. */
3780 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3781 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3783 tree memb, marginal_memb = NULL_TREE;
3785 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3787 tree memb_type = TREE_TYPE (memb);
3789 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3790 TYPE_MAIN_VARIANT (rhstype)))
3793 if (TREE_CODE (memb_type) != POINTER_TYPE)
3796 if (coder == POINTER_TYPE)
3798 tree ttl = TREE_TYPE (memb_type);
3799 tree ttr = TREE_TYPE (rhstype);
3801 /* Any non-function converts to a [const][volatile] void *
3802 and vice versa; otherwise, targets must be the same.
3803 Meanwhile, the lhs target must have all the qualifiers of
3805 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3806 || comp_target_types (memb_type, rhstype))
3808 /* If this type won't generate any warnings, use it. */
3809 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3810 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3811 && TREE_CODE (ttl) == FUNCTION_TYPE)
3812 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3813 == TYPE_QUALS (ttr))
3814 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3815 == TYPE_QUALS (ttl))))
3818 /* Keep looking for a better type, but remember this one. */
3820 marginal_memb = memb;
3824 /* Can convert integer zero to any pointer type. */
3825 if (integer_zerop (rhs)
3826 || (TREE_CODE (rhs) == NOP_EXPR
3827 && integer_zerop (TREE_OPERAND (rhs, 0))))
3829 rhs = null_pointer_node;
3834 if (memb || marginal_memb)
3838 /* We have only a marginally acceptable member type;
3839 it needs a warning. */
3840 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3841 tree ttr = TREE_TYPE (rhstype);
3843 /* Const and volatile mean something different for function
3844 types, so the usual warnings are not appropriate. */
3845 if (TREE_CODE (ttr) == FUNCTION_TYPE
3846 && TREE_CODE (ttl) == FUNCTION_TYPE)
3848 /* Because const and volatile on functions are
3849 restrictions that say the function will not do
3850 certain things, it is okay to use a const or volatile
3851 function where an ordinary one is wanted, but not
3853 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3854 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
3855 "makes qualified function "
3856 "pointer from unqualified"),
3857 G_("assignment makes qualified "
3858 "function pointer from "
3860 G_("initialization makes qualified "
3861 "function pointer from "
3863 G_("return makes qualified function "
3864 "pointer from unqualified"));
3866 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3867 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3868 "qualifiers from pointer target type"),
3869 G_("assignment discards qualifiers "
3870 "from pointer target type"),
3871 G_("initialization discards qualifiers "
3872 "from pointer target type"),
3873 G_("return discards qualifiers from "
3874 "pointer target type"));
3876 memb = marginal_memb;
3879 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
3880 pedwarn ("ISO C prohibits argument conversion to union type");
3882 return build_constructor_single (type, memb, rhs);
3886 /* Conversions among pointers */
3887 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3888 && (coder == codel))
3890 tree ttl = TREE_TYPE (type);
3891 tree ttr = TREE_TYPE (rhstype);
3894 bool is_opaque_pointer;
3895 int target_cmp = 0; /* Cache comp_target_types () result. */
3897 if (TREE_CODE (mvl) != ARRAY_TYPE)
3898 mvl = TYPE_MAIN_VARIANT (mvl);
3899 if (TREE_CODE (mvr) != ARRAY_TYPE)
3900 mvr = TYPE_MAIN_VARIANT (mvr);
3901 /* Opaque pointers are treated like void pointers. */
3902 is_opaque_pointer = (targetm.vector_opaque_p (type)
3903 || targetm.vector_opaque_p (rhstype))
3904 && TREE_CODE (ttl) == VECTOR_TYPE
3905 && TREE_CODE (ttr) == VECTOR_TYPE;
3907 /* C++ does not allow the implicit conversion void* -> T*. However,
3908 for the purpose of reducing the number of false positives, we
3909 tolerate the special case of
3913 where NULL is typically defined in C to be '(void *) 0'. */
3914 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
3915 warning (OPT_Wc___compat, "request for implicit conversion from "
3916 "%qT to %qT not permitted in C++", rhstype, type);
3918 /* Check if the right-hand side has a format attribute but the
3919 left-hand side doesn't. */
3920 if (warn_missing_format_attribute
3921 && check_missing_format_attribute (type, rhstype))
3926 case ic_argpass_nonproto:
3927 warning (OPT_Wmissing_format_attribute,
3928 "argument %d of %qE might be "
3929 "a candidate for a format attribute",
3933 warning (OPT_Wmissing_format_attribute,
3934 "assignment left-hand side might be "
3935 "a candidate for a format attribute");
3938 warning (OPT_Wmissing_format_attribute,
3939 "initialization left-hand side might be "
3940 "a candidate for a format attribute");
3943 warning (OPT_Wmissing_format_attribute,
3944 "return type might be "
3945 "a candidate for a format attribute");
3952 /* Any non-function converts to a [const][volatile] void *
3953 and vice versa; otherwise, targets must be the same.
3954 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3955 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3956 || (target_cmp = comp_target_types (type, rhstype))
3957 || is_opaque_pointer
3958 || (c_common_unsigned_type (mvl)
3959 == c_common_unsigned_type (mvr)))
3962 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3965 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3966 which are not ANSI null ptr constants. */
3967 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3968 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3969 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
3970 "%qE between function pointer "
3972 G_("ISO C forbids assignment between "
3973 "function pointer and %<void *%>"),
3974 G_("ISO C forbids initialization between "
3975 "function pointer and %<void *%>"),
3976 G_("ISO C forbids return between function "
3977 "pointer and %<void *%>"));
3978 /* Const and volatile mean something different for function types,
3979 so the usual warnings are not appropriate. */
3980 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3981 && TREE_CODE (ttl) != FUNCTION_TYPE)
3983 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3985 /* Types differing only by the presence of the 'volatile'
3986 qualifier are acceptable if the 'volatile' has been added
3987 in by the Objective-C EH machinery. */
3988 if (!objc_type_quals_match (ttl, ttr))
3989 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3990 "qualifiers from pointer target type"),
3991 G_("assignment discards qualifiers "
3992 "from pointer target type"),
3993 G_("initialization discards qualifiers "
3994 "from pointer target type"),
3995 G_("return discards qualifiers from "
3996 "pointer target type"));
3998 /* If this is not a case of ignoring a mismatch in signedness,
4000 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4003 /* If there is a mismatch, do warn. */
4004 else if (warn_pointer_sign)
4005 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4006 "%d of %qE differ in signedness"),
4007 G_("pointer targets in assignment "
4008 "differ in signedness"),
4009 G_("pointer targets in initialization "
4010 "differ in signedness"),
4011 G_("pointer targets in return differ "
4014 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4015 && TREE_CODE (ttr) == FUNCTION_TYPE)
4017 /* Because const and volatile on functions are restrictions
4018 that say the function will not do certain things,
4019 it is okay to use a const or volatile function
4020 where an ordinary one is wanted, but not vice-versa. */
4021 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4022 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4023 "qualified function pointer "
4024 "from unqualified"),
4025 G_("assignment makes qualified function "
4026 "pointer from unqualified"),
4027 G_("initialization makes qualified "
4028 "function pointer from unqualified"),
4029 G_("return makes qualified function "
4030 "pointer from unqualified"));
4034 /* Avoid warning about the volatile ObjC EH puts on decls. */
4036 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4037 "incompatible pointer type"),
4038 G_("assignment from incompatible pointer type"),
4039 G_("initialization from incompatible "
4041 G_("return from incompatible pointer type"));
4043 return convert (type, rhs);
4045 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4047 /* ??? This should not be an error when inlining calls to
4048 unprototyped functions. */
4049 error ("invalid use of non-lvalue array");
4050 return error_mark_node;
4052 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4054 /* An explicit constant 0 can convert to a pointer,
4055 or one that results from arithmetic, even including
4056 a cast to integer type. */
4057 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4059 !(TREE_CODE (rhs) == NOP_EXPR
4060 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4061 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4062 && integer_zerop (TREE_OPERAND (rhs, 0))))
4063 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4064 "pointer from integer without a cast"),
4065 G_("assignment makes pointer from integer "
4067 G_("initialization makes pointer from "
4068 "integer without a cast"),
4069 G_("return makes pointer from integer "
4072 return convert (type, rhs);
4074 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4076 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4077 "from pointer without a cast"),
4078 G_("assignment makes integer from pointer "
4080 G_("initialization makes integer from pointer "
4082 G_("return makes integer from pointer "
4084 return convert (type, rhs);
4086 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4087 return convert (type, rhs);
4092 case ic_argpass_nonproto:
4093 /* ??? This should not be an error when inlining calls to
4094 unprototyped functions. */
4095 error ("incompatible type for argument %d of %qE", parmnum, rname);
4098 error ("incompatible types in assignment");
4101 error ("incompatible types in initialization");
4104 error ("incompatible types in return");
4110 return error_mark_node;
4113 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
4114 is used for error and waring reporting and indicates which argument
4115 is being processed. */
4118 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
4122 /* If FN was prototyped, the value has been converted already
4123 in convert_arguments. */
4124 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4127 type = TREE_TYPE (parm);
4128 ret = convert_for_assignment (type, value,
4129 ic_argpass_nonproto, fn,
4131 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
4132 && INTEGRAL_TYPE_P (type)
4133 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4134 ret = default_conversion (ret);
4138 /* If VALUE is a compound expr all of whose expressions are constant, then
4139 return its value. Otherwise, return error_mark_node.
4141 This is for handling COMPOUND_EXPRs as initializer elements
4142 which is allowed with a warning when -pedantic is specified. */
4145 valid_compound_expr_initializer (tree value, tree endtype)
4147 if (TREE_CODE (value) == COMPOUND_EXPR)
4149 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4151 return error_mark_node;
4152 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4155 else if (!initializer_constant_valid_p (value, endtype))
4156 return error_mark_node;
4161 /* Perform appropriate conversions on the initial value of a variable,
4162 store it in the declaration DECL,
4163 and print any error messages that are appropriate.
4164 If the init is invalid, store an ERROR_MARK. */
4167 store_init_value (tree decl, tree init)
4171 /* If variable's type was invalidly declared, just ignore it. */
4173 type = TREE_TYPE (decl);
4174 if (TREE_CODE (type) == ERROR_MARK)
4177 /* Digest the specified initializer into an expression. */
4179 value = digest_init (type, init, true, TREE_STATIC (decl));
4181 /* Store the expression if valid; else report error. */
4183 if (!in_system_header
4184 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4185 warning (OPT_Wtraditional, "traditional C rejects automatic "
4186 "aggregate initialization");
4188 DECL_INITIAL (decl) = value;
4190 /* ANSI wants warnings about out-of-range constant initializers. */
4191 STRIP_TYPE_NOPS (value);
4192 constant_expression_warning (value);
4194 /* Check if we need to set array size from compound literal size. */
4195 if (TREE_CODE (type) == ARRAY_TYPE
4196 && TYPE_DOMAIN (type) == 0
4197 && value != error_mark_node)
4199 tree inside_init = init;
4201 STRIP_TYPE_NOPS (inside_init);
4202 inside_init = fold (inside_init);
4204 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4206 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4208 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4210 /* For int foo[] = (int [3]){1}; we need to set array size
4211 now since later on array initializer will be just the
4212 brace enclosed list of the compound literal. */
4213 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4215 layout_decl (decl, 0);
4221 /* Methods for storing and printing names for error messages. */
4223 /* Implement a spelling stack that allows components of a name to be pushed
4224 and popped. Each element on the stack is this structure. */
4231 unsigned HOST_WIDE_INT i;
4236 #define SPELLING_STRING 1
4237 #define SPELLING_MEMBER 2
4238 #define SPELLING_BOUNDS 3
4240 static struct spelling *spelling; /* Next stack element (unused). */
4241 static struct spelling *spelling_base; /* Spelling stack base. */
4242 static int spelling_size; /* Size of the spelling stack. */
4244 /* Macros to save and restore the spelling stack around push_... functions.
4245 Alternative to SAVE_SPELLING_STACK. */
4247 #define SPELLING_DEPTH() (spelling - spelling_base)
4248 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4250 /* Push an element on the spelling stack with type KIND and assign VALUE
4253 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4255 int depth = SPELLING_DEPTH (); \
4257 if (depth >= spelling_size) \
4259 spelling_size += 10; \
4260 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4262 RESTORE_SPELLING_DEPTH (depth); \
4265 spelling->kind = (KIND); \
4266 spelling->MEMBER = (VALUE); \
4270 /* Push STRING on the stack. Printed literally. */
4273 push_string (const char *string)
4275 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4278 /* Push a member name on the stack. Printed as '.' STRING. */
4281 push_member_name (tree decl)
4283 const char *const string
4284 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4285 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4288 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4291 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4293 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4296 /* Compute the maximum size in bytes of the printed spelling. */
4299 spelling_length (void)
4304 for (p = spelling_base; p < spelling; p++)
4306 if (p->kind == SPELLING_BOUNDS)
4309 size += strlen (p->u.s) + 1;
4315 /* Print the spelling to BUFFER and return it. */
4318 print_spelling (char *buffer)
4323 for (p = spelling_base; p < spelling; p++)
4324 if (p->kind == SPELLING_BOUNDS)
4326 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4332 if (p->kind == SPELLING_MEMBER)
4334 for (s = p->u.s; (*d = *s++); d++)
4341 /* Issue an error message for a bad initializer component.
4342 MSGID identifies the message.
4343 The component name is taken from the spelling stack. */
4346 error_init (const char *msgid)
4350 error ("%s", _(msgid));
4351 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4353 error ("(near initialization for %qs)", ofwhat);
4356 /* Issue a pedantic warning for a bad initializer component.
4357 MSGID identifies the message.
4358 The component name is taken from the spelling stack. */
4361 pedwarn_init (const char *msgid)
4365 pedwarn ("%s", _(msgid));
4366 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4368 pedwarn ("(near initialization for %qs)", ofwhat);
4371 /* Issue a warning for a bad initializer component.
4372 MSGID identifies the message.
4373 The component name is taken from the spelling stack. */
4376 warning_init (const char *msgid)
4380 warning (0, "%s", _(msgid));
4381 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4383 warning (0, "(near initialization for %qs)", ofwhat);
4386 /* If TYPE is an array type and EXPR is a parenthesized string
4387 constant, warn if pedantic that EXPR is being used to initialize an
4388 object of type TYPE. */
4391 maybe_warn_string_init (tree type, struct c_expr expr)
4394 && TREE_CODE (type) == ARRAY_TYPE
4395 && TREE_CODE (expr.value) == STRING_CST
4396 && expr.original_code != STRING_CST)
4397 pedwarn_init ("array initialized from parenthesized string constant");
4400 /* Digest the parser output INIT as an initializer for type TYPE.
4401 Return a C expression of type TYPE to represent the initial value.
4403 If INIT is a string constant, STRICT_STRING is true if it is
4404 unparenthesized or we should not warn here for it being parenthesized.
4405 For other types of INIT, STRICT_STRING is not used.
4407 REQUIRE_CONSTANT requests an error if non-constant initializers or
4408 elements are seen. */
4411 digest_init (tree type, tree init, bool strict_string, int require_constant)
4413 enum tree_code code = TREE_CODE (type);
4414 tree inside_init = init;
4416 if (type == error_mark_node
4418 || init == error_mark_node
4419 || TREE_TYPE (init) == error_mark_node)
4420 return error_mark_node;
4422 STRIP_TYPE_NOPS (inside_init);
4424 inside_init = fold (inside_init);
4426 /* Initialization of an array of chars from a string constant
4427 optionally enclosed in braces. */
4429 if (code == ARRAY_TYPE && inside_init
4430 && TREE_CODE (inside_init) == STRING_CST)
4432 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4433 /* Note that an array could be both an array of character type
4434 and an array of wchar_t if wchar_t is signed char or unsigned
4436 bool char_array = (typ1 == char_type_node
4437 || typ1 == signed_char_type_node
4438 || typ1 == unsigned_char_type_node);
4439 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4440 if (char_array || wchar_array)
4444 expr.value = inside_init;
4445 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4446 maybe_warn_string_init (type, expr);
4449 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4452 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4453 TYPE_MAIN_VARIANT (type)))
4456 if (!wchar_array && !char_string)
4458 error_init ("char-array initialized from wide string");
4459 return error_mark_node;
4461 if (char_string && !char_array)
4463 error_init ("wchar_t-array initialized from non-wide string");
4464 return error_mark_node;
4467 TREE_TYPE (inside_init) = type;
4468 if (TYPE_DOMAIN (type) != 0
4469 && TYPE_SIZE (type) != 0
4470 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4471 /* Subtract 1 (or sizeof (wchar_t))
4472 because it's ok to ignore the terminating null char
4473 that is counted in the length of the constant. */
4474 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4475 TREE_STRING_LENGTH (inside_init)
4476 - ((TYPE_PRECISION (typ1)
4477 != TYPE_PRECISION (char_type_node))
4478 ? (TYPE_PRECISION (wchar_type_node)
4481 pedwarn_init ("initializer-string for array of chars is too long");
4485 else if (INTEGRAL_TYPE_P (typ1))
4487 error_init ("array of inappropriate type initialized "
4488 "from string constant");
4489 return error_mark_node;
4493 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4494 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4495 below and handle as a constructor. */
4496 if (code == VECTOR_TYPE
4497 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4498 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4499 && TREE_CONSTANT (inside_init))
4501 if (TREE_CODE (inside_init) == VECTOR_CST
4502 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4503 TYPE_MAIN_VARIANT (type)))
4506 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4508 unsigned HOST_WIDE_INT ix;
4510 bool constant_p = true;
4512 /* Iterate through elements and check if all constructor
4513 elements are *_CSTs. */
4514 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4515 if (!CONSTANT_CLASS_P (value))
4522 return build_vector_from_ctor (type,
4523 CONSTRUCTOR_ELTS (inside_init));
4527 /* Any type can be initialized
4528 from an expression of the same type, optionally with braces. */
4530 if (inside_init && TREE_TYPE (inside_init) != 0
4531 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4532 TYPE_MAIN_VARIANT (type))
4533 || (code == ARRAY_TYPE
4534 && comptypes (TREE_TYPE (inside_init), type))
4535 || (code == VECTOR_TYPE
4536 && comptypes (TREE_TYPE (inside_init), type))
4537 || (code == POINTER_TYPE
4538 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4539 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4540 TREE_TYPE (type)))))
4542 if (code == POINTER_TYPE)
4544 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4546 if (TREE_CODE (inside_init) == STRING_CST
4547 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4548 inside_init = array_to_pointer_conversion (inside_init);
4551 error_init ("invalid use of non-lvalue array");
4552 return error_mark_node;
4557 if (code == VECTOR_TYPE)
4558 /* Although the types are compatible, we may require a
4560 inside_init = convert (type, inside_init);
4562 if (require_constant && !flag_isoc99
4563 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4565 /* As an extension, allow initializing objects with static storage
4566 duration with compound literals (which are then treated just as
4567 the brace enclosed list they contain). */
4568 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4569 inside_init = DECL_INITIAL (decl);
4572 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4573 && TREE_CODE (inside_init) != CONSTRUCTOR)
4575 error_init ("array initialized from non-constant array expression");
4576 return error_mark_node;
4579 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4580 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4582 /* Compound expressions can only occur here if -pedantic or
4583 -pedantic-errors is specified. In the later case, we always want
4584 an error. In the former case, we simply want a warning. */
4585 if (require_constant && pedantic
4586 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4589 = valid_compound_expr_initializer (inside_init,
4590 TREE_TYPE (inside_init));
4591 if (inside_init == error_mark_node)
4592 error_init ("initializer element is not constant");
4594 pedwarn_init ("initializer element is not constant");
4595 if (flag_pedantic_errors)
4596 inside_init = error_mark_node;
4598 else if (require_constant
4599 && !initializer_constant_valid_p (inside_init,
4600 TREE_TYPE (inside_init)))
4602 error_init ("initializer element is not constant");
4603 inside_init = error_mark_node;
4606 /* Added to enable additional -Wmissing-format-attribute warnings. */
4607 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4608 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4613 /* Handle scalar types, including conversions. */
4615 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4616 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4617 || code == VECTOR_TYPE)
4619 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4620 && (TREE_CODE (init) == STRING_CST
4621 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4622 init = array_to_pointer_conversion (init);
4624 = convert_for_assignment (type, init, ic_init,
4625 NULL_TREE, NULL_TREE, 0);
4627 /* Check to see if we have already given an error message. */
4628 if (inside_init == error_mark_node)
4630 else if (require_constant && !TREE_CONSTANT (inside_init))
4632 error_init ("initializer element is not constant");
4633 inside_init = error_mark_node;
4635 else if (require_constant
4636 && !initializer_constant_valid_p (inside_init,
4637 TREE_TYPE (inside_init)))
4639 error_init ("initializer element is not computable at load time");
4640 inside_init = error_mark_node;
4646 /* Come here only for records and arrays. */
4648 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4650 error_init ("variable-sized object may not be initialized");
4651 return error_mark_node;
4654 error_init ("invalid initializer");
4655 return error_mark_node;
4658 /* Handle initializers that use braces. */
4660 /* Type of object we are accumulating a constructor for.
4661 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4662 static tree constructor_type;
4664 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4666 static tree constructor_fields;
4668 /* For an ARRAY_TYPE, this is the specified index
4669 at which to store the next element we get. */
4670 static tree constructor_index;
4672 /* For an ARRAY_TYPE, this is the maximum index. */
4673 static tree constructor_max_index;
4675 /* For a RECORD_TYPE, this is the first field not yet written out. */
4676 static tree constructor_unfilled_fields;
4678 /* For an ARRAY_TYPE, this is the index of the first element
4679 not yet written out. */
4680 static tree constructor_unfilled_index;
4682 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4683 This is so we can generate gaps between fields, when appropriate. */
4684 static tree constructor_bit_index;
4686 /* If we are saving up the elements rather than allocating them,
4687 this is the list of elements so far (in reverse order,
4688 most recent first). */
4689 static VEC(constructor_elt,gc) *constructor_elements;
4691 /* 1 if constructor should be incrementally stored into a constructor chain,
4692 0 if all the elements should be kept in AVL tree. */
4693 static int constructor_incremental;
4695 /* 1 if so far this constructor's elements are all compile-time constants. */
4696 static int constructor_constant;
4698 /* 1 if so far this constructor's elements are all valid address constants. */
4699 static int constructor_simple;
4701 /* 1 if this constructor is erroneous so far. */
4702 static int constructor_erroneous;
4704 /* Structure for managing pending initializer elements, organized as an
4709 struct init_node *left, *right;
4710 struct init_node *parent;
4716 /* Tree of pending elements at this constructor level.
4717 These are elements encountered out of order
4718 which belong at places we haven't reached yet in actually
4720 Will never hold tree nodes across GC runs. */
4721 static struct init_node *constructor_pending_elts;
4723 /* The SPELLING_DEPTH of this constructor. */
4724 static int constructor_depth;
4726 /* DECL node for which an initializer is being read.
4727 0 means we are reading a constructor expression
4728 such as (struct foo) {...}. */
4729 static tree constructor_decl;
4731 /* Nonzero if this is an initializer for a top-level decl. */
4732 static int constructor_top_level;
4734 /* Nonzero if there were any member designators in this initializer. */
4735 static int constructor_designated;
4737 /* Nesting depth of designator list. */
4738 static int designator_depth;
4740 /* Nonzero if there were diagnosed errors in this designator list. */
4741 static int designator_erroneous;
4744 /* This stack has a level for each implicit or explicit level of
4745 structuring in the initializer, including the outermost one. It
4746 saves the values of most of the variables above. */
4748 struct constructor_range_stack;
4750 struct constructor_stack
4752 struct constructor_stack *next;
4757 tree unfilled_index;
4758 tree unfilled_fields;
4760 VEC(constructor_elt,gc) *elements;
4761 struct init_node *pending_elts;
4764 /* If value nonzero, this value should replace the entire
4765 constructor at this level. */
4766 struct c_expr replacement_value;
4767 struct constructor_range_stack *range_stack;
4777 static struct constructor_stack *constructor_stack;
4779 /* This stack represents designators from some range designator up to
4780 the last designator in the list. */
4782 struct constructor_range_stack
4784 struct constructor_range_stack *next, *prev;
4785 struct constructor_stack *stack;
4792 static struct constructor_range_stack *constructor_range_stack;
4794 /* This stack records separate initializers that are nested.
4795 Nested initializers can't happen in ANSI C, but GNU C allows them
4796 in cases like { ... (struct foo) { ... } ... }. */
4798 struct initializer_stack
4800 struct initializer_stack *next;
4802 struct constructor_stack *constructor_stack;
4803 struct constructor_range_stack *constructor_range_stack;
4804 VEC(constructor_elt,gc) *elements;
4805 struct spelling *spelling;
4806 struct spelling *spelling_base;
4809 char require_constant_value;
4810 char require_constant_elements;
4813 static struct initializer_stack *initializer_stack;
4815 /* Prepare to parse and output the initializer for variable DECL. */
4818 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4821 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4823 p->decl = constructor_decl;
4824 p->require_constant_value = require_constant_value;
4825 p->require_constant_elements = require_constant_elements;
4826 p->constructor_stack = constructor_stack;
4827 p->constructor_range_stack = constructor_range_stack;
4828 p->elements = constructor_elements;
4829 p->spelling = spelling;
4830 p->spelling_base = spelling_base;
4831 p->spelling_size = spelling_size;
4832 p->top_level = constructor_top_level;
4833 p->next = initializer_stack;
4834 initializer_stack = p;
4836 constructor_decl = decl;
4837 constructor_designated = 0;
4838 constructor_top_level = top_level;
4840 if (decl != 0 && decl != error_mark_node)
4842 require_constant_value = TREE_STATIC (decl);
4843 require_constant_elements
4844 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4845 /* For a scalar, you can always use any value to initialize,
4846 even within braces. */
4847 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4848 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4849 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4850 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4851 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4855 require_constant_value = 0;
4856 require_constant_elements = 0;
4857 locus = "(anonymous)";
4860 constructor_stack = 0;
4861 constructor_range_stack = 0;
4863 missing_braces_mentioned = 0;
4867 RESTORE_SPELLING_DEPTH (0);
4870 push_string (locus);
4876 struct initializer_stack *p = initializer_stack;
4878 /* Free the whole constructor stack of this initializer. */
4879 while (constructor_stack)
4881 struct constructor_stack *q = constructor_stack;
4882 constructor_stack = q->next;
4886 gcc_assert (!constructor_range_stack);
4888 /* Pop back to the data of the outer initializer (if any). */
4889 free (spelling_base);
4891 constructor_decl = p->decl;
4892 require_constant_value = p->require_constant_value;
4893 require_constant_elements = p->require_constant_elements;
4894 constructor_stack = p->constructor_stack;
4895 constructor_range_stack = p->constructor_range_stack;
4896 constructor_elements = p->elements;
4897 spelling = p->spelling;
4898 spelling_base = p->spelling_base;
4899 spelling_size = p->spelling_size;
4900 constructor_top_level = p->top_level;
4901 initializer_stack = p->next;
4905 /* Call here when we see the initializer is surrounded by braces.
4906 This is instead of a call to push_init_level;
4907 it is matched by a call to pop_init_level.
4909 TYPE is the type to initialize, for a constructor expression.
4910 For an initializer for a decl, TYPE is zero. */
4913 really_start_incremental_init (tree type)
4915 struct constructor_stack *p = XNEW (struct constructor_stack);
4918 type = TREE_TYPE (constructor_decl);
4920 if (targetm.vector_opaque_p (type))
4921 error ("opaque vector types cannot be initialized");
4923 p->type = constructor_type;
4924 p->fields = constructor_fields;
4925 p->index = constructor_index;
4926 p->max_index = constructor_max_index;
4927 p->unfilled_index = constructor_unfilled_index;
4928 p->unfilled_fields = constructor_unfilled_fields;
4929 p->bit_index = constructor_bit_index;
4930 p->elements = constructor_elements;
4931 p->constant = constructor_constant;
4932 p->simple = constructor_simple;
4933 p->erroneous = constructor_erroneous;
4934 p->pending_elts = constructor_pending_elts;
4935 p->depth = constructor_depth;
4936 p->replacement_value.value = 0;
4937 p->replacement_value.original_code = ERROR_MARK;
4941 p->incremental = constructor_incremental;
4942 p->designated = constructor_designated;
4944 constructor_stack = p;
4946 constructor_constant = 1;
4947 constructor_simple = 1;
4948 constructor_depth = SPELLING_DEPTH ();
4949 constructor_elements = 0;
4950 constructor_pending_elts = 0;
4951 constructor_type = type;
4952 constructor_incremental = 1;
4953 constructor_designated = 0;
4954 designator_depth = 0;
4955 designator_erroneous = 0;
4957 if (TREE_CODE (constructor_type) == RECORD_TYPE
4958 || TREE_CODE (constructor_type) == UNION_TYPE)
4960 constructor_fields = TYPE_FIELDS (constructor_type);
4961 /* Skip any nameless bit fields at the beginning. */
4962 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4963 && DECL_NAME (constructor_fields) == 0)
4964 constructor_fields = TREE_CHAIN (constructor_fields);
4966 constructor_unfilled_fields = constructor_fields;
4967 constructor_bit_index = bitsize_zero_node;
4969 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4971 if (TYPE_DOMAIN (constructor_type))
4973 constructor_max_index
4974 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4976 /* Detect non-empty initializations of zero-length arrays. */
4977 if (constructor_max_index == NULL_TREE
4978 && TYPE_SIZE (constructor_type))
4979 constructor_max_index = build_int_cst (NULL_TREE, -1);
4981 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4982 to initialize VLAs will cause a proper error; avoid tree
4983 checking errors as well by setting a safe value. */
4984 if (constructor_max_index
4985 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4986 constructor_max_index = build_int_cst (NULL_TREE, -1);
4989 = convert (bitsizetype,
4990 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4994 constructor_index = bitsize_zero_node;
4995 constructor_max_index = NULL_TREE;
4998 constructor_unfilled_index = constructor_index;
5000 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5002 /* Vectors are like simple fixed-size arrays. */
5003 constructor_max_index =
5004 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5005 constructor_index = bitsize_zero_node;
5006 constructor_unfilled_index = constructor_index;
5010 /* Handle the case of int x = {5}; */
5011 constructor_fields = constructor_type;
5012 constructor_unfilled_fields = constructor_type;
5016 /* Push down into a subobject, for initialization.
5017 If this is for an explicit set of braces, IMPLICIT is 0.
5018 If it is because the next element belongs at a lower level,
5019 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5022 push_init_level (int implicit)
5024 struct constructor_stack *p;
5025 tree value = NULL_TREE;
5027 /* If we've exhausted any levels that didn't have braces,
5028 pop them now. If implicit == 1, this will have been done in
5029 process_init_element; do not repeat it here because in the case
5030 of excess initializers for an empty aggregate this leads to an
5031 infinite cycle of popping a level and immediately recreating
5035 while (constructor_stack->implicit)
5037 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5038 || TREE_CODE (constructor_type) == UNION_TYPE)
5039 && constructor_fields == 0)
5040 process_init_element (pop_init_level (1));
5041 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5042 && constructor_max_index
5043 && tree_int_cst_lt (constructor_max_index,
5045 process_init_element (pop_init_level (1));
5051 /* Unless this is an explicit brace, we need to preserve previous
5055 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5056 || TREE_CODE (constructor_type) == UNION_TYPE)
5057 && constructor_fields)
5058 value = find_init_member (constructor_fields);
5059 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5060 value = find_init_member (constructor_index);
5063 p = XNEW (struct constructor_stack);
5064 p->type = constructor_type;
5065 p->fields = constructor_fields;
5066 p->index = constructor_index;
5067 p->max_index = constructor_max_index;
5068 p->unfilled_index = constructor_unfilled_index;
5069 p->unfilled_fields = constructor_unfilled_fields;
5070 p->bit_index = constructor_bit_index;
5071 p->elements = constructor_elements;
5072 p->constant = constructor_constant;
5073 p->simple = constructor_simple;
5074 p->erroneous = constructor_erroneous;
5075 p->pending_elts = constructor_pending_elts;
5076 p->depth = constructor_depth;
5077 p->replacement_value.value = 0;
5078 p->replacement_value.original_code = ERROR_MARK;
5079 p->implicit = implicit;
5081 p->incremental = constructor_incremental;
5082 p->designated = constructor_designated;
5083 p->next = constructor_stack;
5085 constructor_stack = p;
5087 constructor_constant = 1;
5088 constructor_simple = 1;
5089 constructor_depth = SPELLING_DEPTH ();
5090 constructor_elements = 0;
5091 constructor_incremental = 1;
5092 constructor_designated = 0;
5093 constructor_pending_elts = 0;
5096 p->range_stack = constructor_range_stack;
5097 constructor_range_stack = 0;
5098 designator_depth = 0;
5099 designator_erroneous = 0;
5102 /* Don't die if an entire brace-pair level is superfluous
5103 in the containing level. */
5104 if (constructor_type == 0)
5106 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5107 || TREE_CODE (constructor_type) == UNION_TYPE)
5109 /* Don't die if there are extra init elts at the end. */
5110 if (constructor_fields == 0)
5111 constructor_type = 0;
5114 constructor_type = TREE_TYPE (constructor_fields);
5115 push_member_name (constructor_fields);
5116 constructor_depth++;
5119 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5121 constructor_type = TREE_TYPE (constructor_type);
5122 push_array_bounds (tree_low_cst (constructor_index, 1));
5123 constructor_depth++;
5126 if (constructor_type == 0)
5128 error_init ("extra brace group at end of initializer");
5129 constructor_fields = 0;
5130 constructor_unfilled_fields = 0;
5134 if (value && TREE_CODE (value) == CONSTRUCTOR)
5136 constructor_constant = TREE_CONSTANT (value);
5137 constructor_simple = TREE_STATIC (value);
5138 constructor_elements = CONSTRUCTOR_ELTS (value);
5139 if (!VEC_empty (constructor_elt, constructor_elements)
5140 && (TREE_CODE (constructor_type) == RECORD_TYPE
5141 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5142 set_nonincremental_init ();
5145 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5147 missing_braces_mentioned = 1;
5148 warning_init ("missing braces around initializer");
5151 if (TREE_CODE (constructor_type) == RECORD_TYPE
5152 || TREE_CODE (constructor_type) == UNION_TYPE)
5154 constructor_fields = TYPE_FIELDS (constructor_type);
5155 /* Skip any nameless bit fields at the beginning. */
5156 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5157 && DECL_NAME (constructor_fields) == 0)
5158 constructor_fields = TREE_CHAIN (constructor_fields);
5160 constructor_unfilled_fields = constructor_fields;
5161 constructor_bit_index = bitsize_zero_node;
5163 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5165 /* Vectors are like simple fixed-size arrays. */
5166 constructor_max_index =
5167 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5168 constructor_index = convert (bitsizetype, integer_zero_node);
5169 constructor_unfilled_index = constructor_index;
5171 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5173 if (TYPE_DOMAIN (constructor_type))
5175 constructor_max_index
5176 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5178 /* Detect non-empty initializations of zero-length arrays. */
5179 if (constructor_max_index == NULL_TREE
5180 && TYPE_SIZE (constructor_type))
5181 constructor_max_index = build_int_cst (NULL_TREE, -1);
5183 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5184 to initialize VLAs will cause a proper error; avoid tree
5185 checking errors as well by setting a safe value. */
5186 if (constructor_max_index
5187 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5188 constructor_max_index = build_int_cst (NULL_TREE, -1);
5191 = convert (bitsizetype,
5192 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5195 constructor_index = bitsize_zero_node;
5197 constructor_unfilled_index = constructor_index;
5198 if (value && TREE_CODE (value) == STRING_CST)
5200 /* We need to split the char/wchar array into individual
5201 characters, so that we don't have to special case it
5203 set_nonincremental_init_from_string (value);
5208 if (constructor_type != error_mark_node)
5209 warning_init ("braces around scalar initializer");
5210 constructor_fields = constructor_type;
5211 constructor_unfilled_fields = constructor_type;
5215 /* At the end of an implicit or explicit brace level,
5216 finish up that level of constructor. If a single expression
5217 with redundant braces initialized that level, return the
5218 c_expr structure for that expression. Otherwise, the original_code
5219 element is set to ERROR_MARK.
5220 If we were outputting the elements as they are read, return 0 as the value
5221 from inner levels (process_init_element ignores that),
5222 but return error_mark_node as the value from the outermost level
5223 (that's what we want to put in DECL_INITIAL).
5224 Otherwise, return a CONSTRUCTOR expression as the value. */
5227 pop_init_level (int implicit)
5229 struct constructor_stack *p;
5232 ret.original_code = ERROR_MARK;
5236 /* When we come to an explicit close brace,
5237 pop any inner levels that didn't have explicit braces. */
5238 while (constructor_stack->implicit)
5239 process_init_element (pop_init_level (1));
5241 gcc_assert (!constructor_range_stack);
5244 /* Now output all pending elements. */
5245 constructor_incremental = 1;
5246 output_pending_init_elements (1);
5248 p = constructor_stack;
5250 /* Error for initializing a flexible array member, or a zero-length
5251 array member in an inappropriate context. */
5252 if (constructor_type && constructor_fields
5253 && TREE_CODE (constructor_type) == ARRAY_TYPE
5254 && TYPE_DOMAIN (constructor_type)
5255 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5257 /* Silently discard empty initializations. The parser will
5258 already have pedwarned for empty brackets. */
5259 if (integer_zerop (constructor_unfilled_index))
5260 constructor_type = NULL_TREE;
5263 gcc_assert (!TYPE_SIZE (constructor_type));
5265 if (constructor_depth > 2)
5266 error_init ("initialization of flexible array member in a nested context");
5268 pedwarn_init ("initialization of a flexible array member");
5270 /* We have already issued an error message for the existence
5271 of a flexible array member not at the end of the structure.
5272 Discard the initializer so that we do not die later. */
5273 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5274 constructor_type = NULL_TREE;
5278 /* Warn when some struct elements are implicitly initialized to zero. */
5279 if (warn_missing_field_initializers
5281 && TREE_CODE (constructor_type) == RECORD_TYPE
5282 && constructor_unfilled_fields)
5284 /* Do not warn for flexible array members or zero-length arrays. */
5285 while (constructor_unfilled_fields
5286 && (!DECL_SIZE (constructor_unfilled_fields)
5287 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5288 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5290 /* Do not warn if this level of the initializer uses member
5291 designators; it is likely to be deliberate. */
5292 if (constructor_unfilled_fields && !constructor_designated)
5294 push_member_name (constructor_unfilled_fields);
5295 warning_init ("missing initializer");
5296 RESTORE_SPELLING_DEPTH (constructor_depth);
5300 /* Pad out the end of the structure. */
5301 if (p->replacement_value.value)
5302 /* If this closes a superfluous brace pair,
5303 just pass out the element between them. */
5304 ret = p->replacement_value;
5305 else if (constructor_type == 0)
5307 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5308 && TREE_CODE (constructor_type) != UNION_TYPE
5309 && TREE_CODE (constructor_type) != ARRAY_TYPE
5310 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5312 /* A nonincremental scalar initializer--just return
5313 the element, after verifying there is just one. */
5314 if (VEC_empty (constructor_elt,constructor_elements))
5316 if (!constructor_erroneous)
5317 error_init ("empty scalar initializer");
5318 ret.value = error_mark_node;
5320 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5322 error_init ("extra elements in scalar initializer");
5323 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5326 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5330 if (constructor_erroneous)
5331 ret.value = error_mark_node;
5334 ret.value = build_constructor (constructor_type,
5335 constructor_elements);
5336 if (constructor_constant)
5337 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5338 if (constructor_constant && constructor_simple)
5339 TREE_STATIC (ret.value) = 1;
5343 constructor_type = p->type;
5344 constructor_fields = p->fields;
5345 constructor_index = p->index;
5346 constructor_max_index = p->max_index;
5347 constructor_unfilled_index = p->unfilled_index;
5348 constructor_unfilled_fields = p->unfilled_fields;
5349 constructor_bit_index = p->bit_index;
5350 constructor_elements = p->elements;
5351 constructor_constant = p->constant;
5352 constructor_simple = p->simple;
5353 constructor_erroneous = p->erroneous;
5354 constructor_incremental = p->incremental;
5355 constructor_designated = p->designated;
5356 constructor_pending_elts = p->pending_elts;
5357 constructor_depth = p->depth;
5359 constructor_range_stack = p->range_stack;
5360 RESTORE_SPELLING_DEPTH (constructor_depth);
5362 constructor_stack = p->next;
5367 if (constructor_stack == 0)
5369 ret.value = error_mark_node;
5377 /* Common handling for both array range and field name designators.
5378 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5381 set_designator (int array)
5384 enum tree_code subcode;
5386 /* Don't die if an entire brace-pair level is superfluous
5387 in the containing level. */
5388 if (constructor_type == 0)
5391 /* If there were errors in this designator list already, bail out
5393 if (designator_erroneous)
5396 if (!designator_depth)
5398 gcc_assert (!constructor_range_stack);
5400 /* Designator list starts at the level of closest explicit
5402 while (constructor_stack->implicit)
5403 process_init_element (pop_init_level (1));
5404 constructor_designated = 1;
5408 switch (TREE_CODE (constructor_type))
5412 subtype = TREE_TYPE (constructor_fields);
5413 if (subtype != error_mark_node)
5414 subtype = TYPE_MAIN_VARIANT (subtype);
5417 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5423 subcode = TREE_CODE (subtype);
5424 if (array && subcode != ARRAY_TYPE)
5426 error_init ("array index in non-array initializer");
5429 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5431 error_init ("field name not in record or union initializer");
5435 constructor_designated = 1;
5436 push_init_level (2);
5440 /* If there are range designators in designator list, push a new designator
5441 to constructor_range_stack. RANGE_END is end of such stack range or
5442 NULL_TREE if there is no range designator at this level. */
5445 push_range_stack (tree range_end)
5447 struct constructor_range_stack *p;
5449 p = GGC_NEW (struct constructor_range_stack);
5450 p->prev = constructor_range_stack;
5452 p->fields = constructor_fields;
5453 p->range_start = constructor_index;
5454 p->index = constructor_index;
5455 p->stack = constructor_stack;
5456 p->range_end = range_end;
5457 if (constructor_range_stack)
5458 constructor_range_stack->next = p;
5459 constructor_range_stack = p;
5462 /* Within an array initializer, specify the next index to be initialized.
5463 FIRST is that index. If LAST is nonzero, then initialize a range
5464 of indices, running from FIRST through LAST. */
5467 set_init_index (tree first, tree last)
5469 if (set_designator (1))
5472 designator_erroneous = 1;
5474 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5475 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5477 error_init ("array index in initializer not of integer type");
5481 if (TREE_CODE (first) != INTEGER_CST)
5482 error_init ("nonconstant array index in initializer");
5483 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5484 error_init ("nonconstant array index in initializer");
5485 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5486 error_init ("array index in non-array initializer");
5487 else if (tree_int_cst_sgn (first) == -1)
5488 error_init ("array index in initializer exceeds array bounds");
5489 else if (constructor_max_index
5490 && tree_int_cst_lt (constructor_max_index, first))
5491 error_init ("array index in initializer exceeds array bounds");
5494 constructor_index = convert (bitsizetype, first);
5498 if (tree_int_cst_equal (first, last))
5500 else if (tree_int_cst_lt (last, first))
5502 error_init ("empty index range in initializer");
5507 last = convert (bitsizetype, last);
5508 if (constructor_max_index != 0
5509 && tree_int_cst_lt (constructor_max_index, last))
5511 error_init ("array index range in initializer exceeds array bounds");
5518 designator_erroneous = 0;
5519 if (constructor_range_stack || last)
5520 push_range_stack (last);
5524 /* Within a struct initializer, specify the next field to be initialized. */
5527 set_init_label (tree fieldname)
5531 if (set_designator (0))
5534 designator_erroneous = 1;
5536 if (TREE_CODE (constructor_type) != RECORD_TYPE
5537 && TREE_CODE (constructor_type) != UNION_TYPE)
5539 error_init ("field name not in record or union initializer");
5543 for (tail = TYPE_FIELDS (constructor_type); tail;
5544 tail = TREE_CHAIN (tail))
5546 if (DECL_NAME (tail) == fieldname)
5551 error ("unknown field %qE specified in initializer", fieldname);
5554 constructor_fields = tail;
5556 designator_erroneous = 0;
5557 if (constructor_range_stack)
5558 push_range_stack (NULL_TREE);
5562 /* Add a new initializer to the tree of pending initializers. PURPOSE
5563 identifies the initializer, either array index or field in a structure.
5564 VALUE is the value of that index or field. */
5567 add_pending_init (tree purpose, tree value)
5569 struct init_node *p, **q, *r;
5571 q = &constructor_pending_elts;
5574 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5579 if (tree_int_cst_lt (purpose, p->purpose))
5581 else if (tree_int_cst_lt (p->purpose, purpose))
5585 if (TREE_SIDE_EFFECTS (p->value))
5586 warning_init ("initialized field with side-effects overwritten");
5596 bitpos = bit_position (purpose);
5600 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5602 else if (p->purpose != purpose)
5606 if (TREE_SIDE_EFFECTS (p->value))
5607 warning_init ("initialized field with side-effects overwritten");
5614 r = GGC_NEW (struct init_node);
5615 r->purpose = purpose;
5626 struct init_node *s;
5630 if (p->balance == 0)
5632 else if (p->balance < 0)
5639 p->left->parent = p;
5656 constructor_pending_elts = r;
5661 struct init_node *t = r->right;
5665 r->right->parent = r;
5670 p->left->parent = p;
5673 p->balance = t->balance < 0;
5674 r->balance = -(t->balance > 0);
5689 constructor_pending_elts = t;
5695 /* p->balance == +1; growth of left side balances the node. */
5700 else /* r == p->right */
5702 if (p->balance == 0)
5703 /* Growth propagation from right side. */
5705 else if (p->balance > 0)
5712 p->right->parent = p;
5729 constructor_pending_elts = r;
5731 else /* r->balance == -1 */
5734 struct init_node *t = r->left;
5738 r->left->parent = r;
5743 p->right->parent = p;
5746 r->balance = (t->balance < 0);
5747 p->balance = -(t->balance > 0);
5762 constructor_pending_elts = t;
5768 /* p->balance == -1; growth of right side balances the node. */
5779 /* Build AVL tree from a sorted chain. */
5782 set_nonincremental_init (void)
5784 unsigned HOST_WIDE_INT ix;
5787 if (TREE_CODE (constructor_type) != RECORD_TYPE
5788 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5791 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
5792 add_pending_init (index, value);
5793 constructor_elements = 0;
5794 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5796 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5797 /* Skip any nameless bit fields at the beginning. */
5798 while (constructor_unfilled_fields != 0
5799 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5800 && DECL_NAME (constructor_unfilled_fields) == 0)
5801 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5804 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5806 if (TYPE_DOMAIN (constructor_type))
5807 constructor_unfilled_index
5808 = convert (bitsizetype,
5809 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5811 constructor_unfilled_index = bitsize_zero_node;
5813 constructor_incremental = 0;
5816 /* Build AVL tree from a string constant. */
5819 set_nonincremental_init_from_string (tree str)
5821 tree value, purpose, type;
5822 HOST_WIDE_INT val[2];
5823 const char *p, *end;
5824 int byte, wchar_bytes, charwidth, bitpos;
5826 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5828 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5829 == TYPE_PRECISION (char_type_node))
5833 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5834 == TYPE_PRECISION (wchar_type_node));
5835 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5837 charwidth = TYPE_PRECISION (char_type_node);
5838 type = TREE_TYPE (constructor_type);
5839 p = TREE_STRING_POINTER (str);
5840 end = p + TREE_STRING_LENGTH (str);
5842 for (purpose = bitsize_zero_node;
5843 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5844 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5846 if (wchar_bytes == 1)
5848 val[1] = (unsigned char) *p++;
5855 for (byte = 0; byte < wchar_bytes; byte++)
5857 if (BYTES_BIG_ENDIAN)
5858 bitpos = (wchar_bytes - byte - 1) * charwidth;
5860 bitpos = byte * charwidth;
5861 val[bitpos < HOST_BITS_PER_WIDE_INT]
5862 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5863 << (bitpos % HOST_BITS_PER_WIDE_INT);
5867 if (!TYPE_UNSIGNED (type))
5869 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5870 if (bitpos < HOST_BITS_PER_WIDE_INT)
5872 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5874 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5878 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5883 else if (val[0] & (((HOST_WIDE_INT) 1)
5884 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5885 val[0] |= ((HOST_WIDE_INT) -1)
5886 << (bitpos - HOST_BITS_PER_WIDE_INT);
5889 value = build_int_cst_wide (type, val[1], val[0]);
5890 add_pending_init (purpose, value);
5893 constructor_incremental = 0;
5896 /* Return value of FIELD in pending initializer or zero if the field was
5897 not initialized yet. */
5900 find_init_member (tree field)
5902 struct init_node *p;
5904 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5906 if (constructor_incremental
5907 && tree_int_cst_lt (field, constructor_unfilled_index))
5908 set_nonincremental_init ();
5910 p = constructor_pending_elts;
5913 if (tree_int_cst_lt (field, p->purpose))
5915 else if (tree_int_cst_lt (p->purpose, field))
5921 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5923 tree bitpos = bit_position (field);
5925 if (constructor_incremental
5926 && (!constructor_unfilled_fields
5927 || tree_int_cst_lt (bitpos,
5928 bit_position (constructor_unfilled_fields))))
5929 set_nonincremental_init ();
5931 p = constructor_pending_elts;
5934 if (field == p->purpose)
5936 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5942 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5944 if (!VEC_empty (constructor_elt, constructor_elements)
5945 && (VEC_last (constructor_elt, constructor_elements)->index
5947 return VEC_last (constructor_elt, constructor_elements)->value;
5952 /* "Output" the next constructor element.
5953 At top level, really output it to assembler code now.
5954 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5955 TYPE is the data type that the containing data type wants here.
5956 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5957 If VALUE is a string constant, STRICT_STRING is true if it is
5958 unparenthesized or we should not warn here for it being parenthesized.
5959 For other types of VALUE, STRICT_STRING is not used.
5961 PENDING if non-nil means output pending elements that belong
5962 right after this element. (PENDING is normally 1;
5963 it is 0 while outputting pending elements, to avoid recursion.) */
5966 output_init_element (tree value, bool strict_string, tree type, tree field,
5969 constructor_elt *celt;
5971 if (type == error_mark_node || value == error_mark_node)
5973 constructor_erroneous = 1;
5976 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5977 && (TREE_CODE (value) == STRING_CST
5978 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
5979 && !(TREE_CODE (value) == STRING_CST
5980 && TREE_CODE (type) == ARRAY_TYPE
5981 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5982 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5983 TYPE_MAIN_VARIANT (type)))
5984 value = array_to_pointer_conversion (value);
5986 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5987 && require_constant_value && !flag_isoc99 && pending)
5989 /* As an extension, allow initializing objects with static storage
5990 duration with compound literals (which are then treated just as
5991 the brace enclosed list they contain). */
5992 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5993 value = DECL_INITIAL (decl);
5996 if (value == error_mark_node)
5997 constructor_erroneous = 1;
5998 else if (!TREE_CONSTANT (value))
5999 constructor_constant = 0;
6000 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6001 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6002 || TREE_CODE (constructor_type) == UNION_TYPE)
6003 && DECL_C_BIT_FIELD (field)
6004 && TREE_CODE (value) != INTEGER_CST))
6005 constructor_simple = 0;
6007 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6009 if (require_constant_value)
6011 error_init ("initializer element is not constant");
6012 value = error_mark_node;
6014 else if (require_constant_elements)
6015 pedwarn ("initializer element is not computable at load time");
6018 /* If this field is empty (and not at the end of structure),
6019 don't do anything other than checking the initializer. */
6021 && (TREE_TYPE (field) == error_mark_node
6022 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6023 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6024 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6025 || TREE_CHAIN (field)))))
6028 value = digest_init (type, value, strict_string, require_constant_value);
6029 if (value == error_mark_node)
6031 constructor_erroneous = 1;
6035 /* If this element doesn't come next in sequence,
6036 put it on constructor_pending_elts. */
6037 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6038 && (!constructor_incremental
6039 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6041 if (constructor_incremental
6042 && tree_int_cst_lt (field, constructor_unfilled_index))
6043 set_nonincremental_init ();
6045 add_pending_init (field, value);
6048 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6049 && (!constructor_incremental
6050 || field != constructor_unfilled_fields))
6052 /* We do this for records but not for unions. In a union,
6053 no matter which field is specified, it can be initialized
6054 right away since it starts at the beginning of the union. */
6055 if (constructor_incremental)
6057 if (!constructor_unfilled_fields)
6058 set_nonincremental_init ();
6061 tree bitpos, unfillpos;
6063 bitpos = bit_position (field);
6064 unfillpos = bit_position (constructor_unfilled_fields);
6066 if (tree_int_cst_lt (bitpos, unfillpos))
6067 set_nonincremental_init ();
6071 add_pending_init (field, value);
6074 else if (TREE_CODE (constructor_type) == UNION_TYPE
6075 && !VEC_empty (constructor_elt, constructor_elements))
6077 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6078 constructor_elements)->value))
6079 warning_init ("initialized field with side-effects overwritten");
6081 /* We can have just one union field set. */
6082 constructor_elements = 0;
6085 /* Otherwise, output this element either to
6086 constructor_elements or to the assembler file. */
6088 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6089 celt->index = field;
6090 celt->value = value;
6092 /* Advance the variable that indicates sequential elements output. */
6093 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6094 constructor_unfilled_index
6095 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6097 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6099 constructor_unfilled_fields
6100 = TREE_CHAIN (constructor_unfilled_fields);
6102 /* Skip any nameless bit fields. */
6103 while (constructor_unfilled_fields != 0
6104 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6105 && DECL_NAME (constructor_unfilled_fields) == 0)
6106 constructor_unfilled_fields =
6107 TREE_CHAIN (constructor_unfilled_fields);
6109 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6110 constructor_unfilled_fields = 0;
6112 /* Now output any pending elements which have become next. */
6114 output_pending_init_elements (0);
6117 /* Output any pending elements which have become next.
6118 As we output elements, constructor_unfilled_{fields,index}
6119 advances, which may cause other elements to become next;
6120 if so, they too are output.
6122 If ALL is 0, we return when there are
6123 no more pending elements to output now.
6125 If ALL is 1, we output space as necessary so that
6126 we can output all the pending elements. */
6129 output_pending_init_elements (int all)
6131 struct init_node *elt = constructor_pending_elts;
6136 /* Look through the whole pending tree.
6137 If we find an element that should be output now,
6138 output it. Otherwise, set NEXT to the element
6139 that comes first among those still pending. */
6144 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6146 if (tree_int_cst_equal (elt->purpose,
6147 constructor_unfilled_index))
6148 output_init_element (elt->value, true,
6149 TREE_TYPE (constructor_type),
6150 constructor_unfilled_index, 0);
6151 else if (tree_int_cst_lt (constructor_unfilled_index,
6154 /* Advance to the next smaller node. */
6159 /* We have reached the smallest node bigger than the
6160 current unfilled index. Fill the space first. */
6161 next = elt->purpose;
6167 /* Advance to the next bigger node. */
6172 /* We have reached the biggest node in a subtree. Find
6173 the parent of it, which is the next bigger node. */
6174 while (elt->parent && elt->parent->right == elt)
6177 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6180 next = elt->purpose;
6186 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6187 || TREE_CODE (constructor_type) == UNION_TYPE)
6189 tree ctor_unfilled_bitpos, elt_bitpos;
6191 /* If the current record is complete we are done. */
6192 if (constructor_unfilled_fields == 0)
6195 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6196 elt_bitpos = bit_position (elt->purpose);
6197 /* We can't compare fields here because there might be empty
6198 fields in between. */
6199 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6201 constructor_unfilled_fields = elt->purpose;
6202 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6205 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6207 /* Advance to the next smaller node. */
6212 /* We have reached the smallest node bigger than the
6213 current unfilled field. Fill the space first. */
6214 next = elt->purpose;
6220 /* Advance to the next bigger node. */
6225 /* We have reached the biggest node in a subtree. Find
6226 the parent of it, which is the next bigger node. */
6227 while (elt->parent && elt->parent->right == elt)
6231 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6232 bit_position (elt->purpose))))
6234 next = elt->purpose;
6242 /* Ordinarily return, but not if we want to output all
6243 and there are elements left. */
6244 if (!(all && next != 0))
6247 /* If it's not incremental, just skip over the gap, so that after
6248 jumping to retry we will output the next successive element. */
6249 if (TREE_CODE (constructor_type) == RECORD_TYPE
6250 || TREE_CODE (constructor_type) == UNION_TYPE)
6251 constructor_unfilled_fields = next;
6252 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6253 constructor_unfilled_index = next;
6255 /* ELT now points to the node in the pending tree with the next
6256 initializer to output. */
6260 /* Add one non-braced element to the current constructor level.
6261 This adjusts the current position within the constructor's type.
6262 This may also start or terminate implicit levels
6263 to handle a partly-braced initializer.
6265 Once this has found the correct level for the new element,
6266 it calls output_init_element. */
6269 process_init_element (struct c_expr value)
6271 tree orig_value = value.value;
6272 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6273 bool strict_string = value.original_code == STRING_CST;
6275 designator_depth = 0;
6276 designator_erroneous = 0;
6278 /* Handle superfluous braces around string cst as in
6279 char x[] = {"foo"}; */
6282 && TREE_CODE (constructor_type) == ARRAY_TYPE
6283 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6284 && integer_zerop (constructor_unfilled_index))
6286 if (constructor_stack->replacement_value.value)
6287 error_init ("excess elements in char array initializer");
6288 constructor_stack->replacement_value = value;
6292 if (constructor_stack->replacement_value.value != 0)
6294 error_init ("excess elements in struct initializer");
6298 /* Ignore elements of a brace group if it is entirely superfluous
6299 and has already been diagnosed. */
6300 if (constructor_type == 0)
6303 /* If we've exhausted any levels that didn't have braces,
6305 while (constructor_stack->implicit)
6307 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6308 || TREE_CODE (constructor_type) == UNION_TYPE)
6309 && constructor_fields == 0)
6310 process_init_element (pop_init_level (1));
6311 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6312 && (constructor_max_index == 0
6313 || tree_int_cst_lt (constructor_max_index,
6314 constructor_index)))
6315 process_init_element (pop_init_level (1));
6320 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6321 if (constructor_range_stack)
6323 /* If value is a compound literal and we'll be just using its
6324 content, don't put it into a SAVE_EXPR. */
6325 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6326 || !require_constant_value
6328 value.value = save_expr (value.value);
6333 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6336 enum tree_code fieldcode;
6338 if (constructor_fields == 0)
6340 pedwarn_init ("excess elements in struct initializer");
6344 fieldtype = TREE_TYPE (constructor_fields);
6345 if (fieldtype != error_mark_node)
6346 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6347 fieldcode = TREE_CODE (fieldtype);
6349 /* Error for non-static initialization of a flexible array member. */
6350 if (fieldcode == ARRAY_TYPE
6351 && !require_constant_value
6352 && TYPE_SIZE (fieldtype) == NULL_TREE
6353 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6355 error_init ("non-static initialization of a flexible array member");
6359 /* Accept a string constant to initialize a subarray. */
6360 if (value.value != 0
6361 && fieldcode == ARRAY_TYPE
6362 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6364 value.value = orig_value;
6365 /* Otherwise, if we have come to a subaggregate,
6366 and we don't have an element of its type, push into it. */
6367 else if (value.value != 0
6368 && value.value != error_mark_node
6369 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6370 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6371 || fieldcode == UNION_TYPE))
6373 push_init_level (1);
6379 push_member_name (constructor_fields);
6380 output_init_element (value.value, strict_string,
6381 fieldtype, constructor_fields, 1);
6382 RESTORE_SPELLING_DEPTH (constructor_depth);
6385 /* Do the bookkeeping for an element that was
6386 directly output as a constructor. */
6388 /* For a record, keep track of end position of last field. */
6389 if (DECL_SIZE (constructor_fields))
6390 constructor_bit_index
6391 = size_binop (PLUS_EXPR,
6392 bit_position (constructor_fields),
6393 DECL_SIZE (constructor_fields));
6395 /* If the current field was the first one not yet written out,
6396 it isn't now, so update. */
6397 if (constructor_unfilled_fields == constructor_fields)
6399 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6400 /* Skip any nameless bit fields. */
6401 while (constructor_unfilled_fields != 0
6402 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6403 && DECL_NAME (constructor_unfilled_fields) == 0)
6404 constructor_unfilled_fields =
6405 TREE_CHAIN (constructor_unfilled_fields);
6409 constructor_fields = TREE_CHAIN (constructor_fields);
6410 /* Skip any nameless bit fields at the beginning. */
6411 while (constructor_fields != 0
6412 && DECL_C_BIT_FIELD (constructor_fields)
6413 && DECL_NAME (constructor_fields) == 0)
6414 constructor_fields = TREE_CHAIN (constructor_fields);
6416 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6419 enum tree_code fieldcode;
6421 if (constructor_fields == 0)
6423 pedwarn_init ("excess elements in union initializer");
6427 fieldtype = TREE_TYPE (constructor_fields);
6428 if (fieldtype != error_mark_node)
6429 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6430 fieldcode = TREE_CODE (fieldtype);
6432 /* Warn that traditional C rejects initialization of unions.
6433 We skip the warning if the value is zero. This is done
6434 under the assumption that the zero initializer in user
6435 code appears conditioned on e.g. __STDC__ to avoid
6436 "missing initializer" warnings and relies on default
6437 initialization to zero in the traditional C case.
6438 We also skip the warning if the initializer is designated,
6439 again on the assumption that this must be conditional on
6440 __STDC__ anyway (and we've already complained about the
6441 member-designator already). */
6442 if (!in_system_header && !constructor_designated
6443 && !(value.value && (integer_zerop (value.value)
6444 || real_zerop (value.value))))
6445 warning (OPT_Wtraditional, "traditional C rejects initialization "
6448 /* Accept a string constant to initialize a subarray. */
6449 if (value.value != 0
6450 && fieldcode == ARRAY_TYPE
6451 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6453 value.value = orig_value;
6454 /* Otherwise, if we have come to a subaggregate,
6455 and we don't have an element of its type, push into it. */
6456 else if (value.value != 0
6457 && value.value != error_mark_node
6458 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6459 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6460 || fieldcode == UNION_TYPE))
6462 push_init_level (1);
6468 push_member_name (constructor_fields);
6469 output_init_element (value.value, strict_string,
6470 fieldtype, constructor_fields, 1);
6471 RESTORE_SPELLING_DEPTH (constructor_depth);
6474 /* Do the bookkeeping for an element that was
6475 directly output as a constructor. */
6477 constructor_bit_index = DECL_SIZE (constructor_fields);
6478 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6481 constructor_fields = 0;
6483 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6485 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6486 enum tree_code eltcode = TREE_CODE (elttype);
6488 /* Accept a string constant to initialize a subarray. */
6489 if (value.value != 0
6490 && eltcode == ARRAY_TYPE
6491 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6493 value.value = orig_value;
6494 /* Otherwise, if we have come to a subaggregate,
6495 and we don't have an element of its type, push into it. */
6496 else if (value.value != 0
6497 && value.value != error_mark_node
6498 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6499 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6500 || eltcode == UNION_TYPE))
6502 push_init_level (1);
6506 if (constructor_max_index != 0
6507 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6508 || integer_all_onesp (constructor_max_index)))
6510 pedwarn_init ("excess elements in array initializer");
6514 /* Now output the actual element. */
6517 push_array_bounds (tree_low_cst (constructor_index, 1));
6518 output_init_element (value.value, strict_string,
6519 elttype, constructor_index, 1);
6520 RESTORE_SPELLING_DEPTH (constructor_depth);
6524 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6527 /* If we are doing the bookkeeping for an element that was
6528 directly output as a constructor, we must update
6529 constructor_unfilled_index. */
6530 constructor_unfilled_index = constructor_index;
6532 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6534 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6536 /* Do a basic check of initializer size. Note that vectors
6537 always have a fixed size derived from their type. */
6538 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6540 pedwarn_init ("excess elements in vector initializer");
6544 /* Now output the actual element. */
6546 output_init_element (value.value, strict_string,
6547 elttype, constructor_index, 1);
6550 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6553 /* If we are doing the bookkeeping for an element that was
6554 directly output as a constructor, we must update
6555 constructor_unfilled_index. */
6556 constructor_unfilled_index = constructor_index;
6559 /* Handle the sole element allowed in a braced initializer
6560 for a scalar variable. */
6561 else if (constructor_type != error_mark_node
6562 && constructor_fields == 0)
6564 pedwarn_init ("excess elements in scalar initializer");
6570 output_init_element (value.value, strict_string,
6571 constructor_type, NULL_TREE, 1);
6572 constructor_fields = 0;
6575 /* Handle range initializers either at this level or anywhere higher
6576 in the designator stack. */
6577 if (constructor_range_stack)
6579 struct constructor_range_stack *p, *range_stack;
6582 range_stack = constructor_range_stack;
6583 constructor_range_stack = 0;
6584 while (constructor_stack != range_stack->stack)
6586 gcc_assert (constructor_stack->implicit);
6587 process_init_element (pop_init_level (1));
6589 for (p = range_stack;
6590 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6593 gcc_assert (constructor_stack->implicit);
6594 process_init_element (pop_init_level (1));
6597 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6598 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6603 constructor_index = p->index;
6604 constructor_fields = p->fields;
6605 if (finish && p->range_end && p->index == p->range_start)
6613 push_init_level (2);
6614 p->stack = constructor_stack;
6615 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6616 p->index = p->range_start;
6620 constructor_range_stack = range_stack;
6627 constructor_range_stack = 0;
6630 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6631 (guaranteed to be 'volatile' or null) and ARGS (represented using
6632 an ASM_EXPR node). */
6634 build_asm_stmt (tree cv_qualifier, tree args)
6636 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6637 ASM_VOLATILE_P (args) = 1;
6638 return add_stmt (args);
6641 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6642 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6643 SIMPLE indicates whether there was anything at all after the
6644 string in the asm expression -- asm("blah") and asm("blah" : )
6645 are subtly different. We use a ASM_EXPR node to represent this. */
6647 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6653 const char *constraint;
6654 const char **oconstraints;
6655 bool allows_mem, allows_reg, is_inout;
6656 int ninputs, noutputs;
6658 ninputs = list_length (inputs);
6659 noutputs = list_length (outputs);
6660 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6662 string = resolve_asm_operand_names (string, outputs, inputs);
6664 /* Remove output conversions that change the type but not the mode. */
6665 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6667 tree output = TREE_VALUE (tail);
6669 /* ??? Really, this should not be here. Users should be using a
6670 proper lvalue, dammit. But there's a long history of using casts
6671 in the output operands. In cases like longlong.h, this becomes a
6672 primitive form of typechecking -- if the cast can be removed, then
6673 the output operand had a type of the proper width; otherwise we'll
6674 get an error. Gross, but ... */
6675 STRIP_NOPS (output);
6677 if (!lvalue_or_else (output, lv_asm))
6678 output = error_mark_node;
6680 if (output != error_mark_node
6681 && (TREE_READONLY (output)
6682 || TYPE_READONLY (TREE_TYPE (output))
6683 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6684 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6685 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6686 readonly_error (output, lv_asm);
6688 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6689 oconstraints[i] = constraint;
6691 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6692 &allows_mem, &allows_reg, &is_inout))
6694 /* If the operand is going to end up in memory,
6695 mark it addressable. */
6696 if (!allows_reg && !c_mark_addressable (output))
6697 output = error_mark_node;
6700 output = error_mark_node;
6702 TREE_VALUE (tail) = output;
6705 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6709 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6710 input = TREE_VALUE (tail);
6712 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6713 oconstraints, &allows_mem, &allows_reg))
6715 /* If the operand is going to end up in memory,
6716 mark it addressable. */
6717 if (!allows_reg && allows_mem)
6719 /* Strip the nops as we allow this case. FIXME, this really
6720 should be rejected or made deprecated. */
6722 if (!c_mark_addressable (input))
6723 input = error_mark_node;
6727 input = error_mark_node;
6729 TREE_VALUE (tail) = input;
6732 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6734 /* asm statements without outputs, including simple ones, are treated
6736 ASM_INPUT_P (args) = simple;
6737 ASM_VOLATILE_P (args) = (noutputs == 0);
6742 /* Generate a goto statement to LABEL. */
6745 c_finish_goto_label (tree label)
6747 tree decl = lookup_label (label);
6751 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6753 error ("jump into statement expression");
6757 if (C_DECL_UNJUMPABLE_VM (decl))
6759 error ("jump into scope of identifier with variably modified type");
6763 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6765 /* No jump from outside this statement expression context, so
6766 record that there is a jump from within this context. */
6767 struct c_label_list *nlist;
6768 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6769 nlist->next = label_context_stack_se->labels_used;
6770 nlist->label = decl;
6771 label_context_stack_se->labels_used = nlist;
6774 if (!C_DECL_UNDEFINABLE_VM (decl))
6776 /* No jump from outside this context context of identifiers with
6777 variably modified type, so record that there is a jump from
6778 within this context. */
6779 struct c_label_list *nlist;
6780 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6781 nlist->next = label_context_stack_vm->labels_used;
6782 nlist->label = decl;
6783 label_context_stack_vm->labels_used = nlist;
6786 TREE_USED (decl) = 1;
6787 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6790 /* Generate a computed goto statement to EXPR. */
6793 c_finish_goto_ptr (tree expr)
6796 pedwarn ("ISO C forbids %<goto *expr;%>");
6797 expr = convert (ptr_type_node, expr);
6798 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6801 /* Generate a C `return' statement. RETVAL is the expression for what
6802 to return, or a null pointer for `return;' with no value. */
6805 c_finish_return (tree retval)
6807 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
6808 bool no_warning = false;
6810 if (TREE_THIS_VOLATILE (current_function_decl))
6811 warning (0, "function declared %<noreturn%> has a %<return%> statement");
6815 current_function_returns_null = 1;
6816 if ((warn_return_type || flag_isoc99)
6817 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6819 pedwarn_c99 ("%<return%> with no value, in "
6820 "function returning non-void");
6824 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6826 current_function_returns_null = 1;
6827 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6828 pedwarn ("%<return%> with a value, in function returning void");
6832 tree t = convert_for_assignment (valtype, retval, ic_return,
6833 NULL_TREE, NULL_TREE, 0);
6834 tree res = DECL_RESULT (current_function_decl);
6837 current_function_returns_value = 1;
6838 if (t == error_mark_node)
6841 inner = t = convert (TREE_TYPE (res), t);
6843 /* Strip any conversions, additions, and subtractions, and see if
6844 we are returning the address of a local variable. Warn if so. */
6847 switch (TREE_CODE (inner))
6849 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6851 inner = TREE_OPERAND (inner, 0);
6855 /* If the second operand of the MINUS_EXPR has a pointer
6856 type (or is converted from it), this may be valid, so
6857 don't give a warning. */
6859 tree op1 = TREE_OPERAND (inner, 1);
6861 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6862 && (TREE_CODE (op1) == NOP_EXPR
6863 || TREE_CODE (op1) == NON_LVALUE_EXPR
6864 || TREE_CODE (op1) == CONVERT_EXPR))
6865 op1 = TREE_OPERAND (op1, 0);
6867 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6870 inner = TREE_OPERAND (inner, 0);
6875 inner = TREE_OPERAND (inner, 0);
6877 while (REFERENCE_CLASS_P (inner)
6878 && TREE_CODE (inner) != INDIRECT_REF)
6879 inner = TREE_OPERAND (inner, 0);
6882 && !DECL_EXTERNAL (inner)
6883 && !TREE_STATIC (inner)
6884 && DECL_CONTEXT (inner) == current_function_decl)
6885 warning (0, "function returns address of local variable");
6895 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6898 ret_stmt = build_stmt (RETURN_EXPR, retval);
6899 TREE_NO_WARNING (ret_stmt) |= no_warning;
6900 return add_stmt (ret_stmt);
6904 /* The SWITCH_EXPR being built. */
6907 /* The original type of the testing expression, i.e. before the
6908 default conversion is applied. */
6911 /* A splay-tree mapping the low element of a case range to the high
6912 element, or NULL_TREE if there is no high element. Used to
6913 determine whether or not a new case label duplicates an old case
6914 label. We need a tree, rather than simply a hash table, because
6915 of the GNU case range extension. */
6918 /* Number of nested statement expressions within this switch
6919 statement; if nonzero, case and default labels may not
6921 unsigned int blocked_stmt_expr;
6923 /* Scope of outermost declarations of identifiers with variably
6924 modified type within this switch statement; if nonzero, case and
6925 default labels may not appear. */
6926 unsigned int blocked_vm;
6928 /* The next node on the stack. */
6929 struct c_switch *next;
6932 /* A stack of the currently active switch statements. The innermost
6933 switch statement is on the top of the stack. There is no need to
6934 mark the stack for garbage collection because it is only active
6935 during the processing of the body of a function, and we never
6936 collect at that point. */
6938 struct c_switch *c_switch_stack;
6940 /* Start a C switch statement, testing expression EXP. Return the new
6944 c_start_case (tree exp)
6946 enum tree_code code;
6947 tree type, orig_type = error_mark_node;
6948 struct c_switch *cs;
6950 if (exp != error_mark_node)
6952 code = TREE_CODE (TREE_TYPE (exp));
6953 orig_type = TREE_TYPE (exp);
6955 if (!INTEGRAL_TYPE_P (orig_type)
6956 && code != ERROR_MARK)
6958 error ("switch quantity not an integer");
6959 exp = integer_zero_node;
6960 orig_type = error_mark_node;
6964 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6966 if (!in_system_header
6967 && (type == long_integer_type_node
6968 || type == long_unsigned_type_node))
6969 warning (OPT_Wtraditional, "%<long%> switch expression not "
6970 "converted to %<int%> in ISO C");
6972 exp = default_conversion (exp);
6973 type = TREE_TYPE (exp);
6977 /* Add this new SWITCH_EXPR to the stack. */
6978 cs = XNEW (struct c_switch);
6979 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
6980 cs->orig_type = orig_type;
6981 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6982 cs->blocked_stmt_expr = 0;
6984 cs->next = c_switch_stack;
6985 c_switch_stack = cs;
6987 return add_stmt (cs->switch_expr);
6990 /* Process a case label. */
6993 do_case (tree low_value, tree high_value)
6995 tree label = NULL_TREE;
6997 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
6998 && !c_switch_stack->blocked_vm)
7000 label = c_add_case_label (c_switch_stack->cases,
7001 SWITCH_COND (c_switch_stack->switch_expr),
7002 c_switch_stack->orig_type,
7003 low_value, high_value);
7004 if (label == error_mark_node)
7007 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7010 error ("case label in statement expression not containing "
7011 "enclosing switch statement");
7013 error ("%<default%> label in statement expression not containing "
7014 "enclosing switch statement");
7016 else if (c_switch_stack && c_switch_stack->blocked_vm)
7019 error ("case label in scope of identifier with variably modified "
7020 "type not containing enclosing switch statement");
7022 error ("%<default%> label in scope of identifier with variably "
7023 "modified type not containing enclosing switch statement");
7026 error ("case label not within a switch statement");
7028 error ("%<default%> label not within a switch statement");
7033 /* Finish the switch statement. */
7036 c_finish_case (tree body)
7038 struct c_switch *cs = c_switch_stack;
7039 location_t switch_location;
7041 SWITCH_BODY (cs->switch_expr) = body;
7043 /* We must not be within a statement expression nested in the switch
7044 at this point; we might, however, be within the scope of an
7045 identifier with variably modified type nested in the switch. */
7046 gcc_assert (!cs->blocked_stmt_expr);
7048 /* Emit warnings as needed. */
7049 if (EXPR_HAS_LOCATION (cs->switch_expr))
7050 switch_location = EXPR_LOCATION (cs->switch_expr);
7052 switch_location = input_location;
7053 c_do_switch_warnings (cs->cases, switch_location,
7054 TREE_TYPE (cs->switch_expr),
7055 SWITCH_COND (cs->switch_expr));
7057 /* Pop the stack. */
7058 c_switch_stack = cs->next;
7059 splay_tree_delete (cs->cases);
7063 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7064 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7065 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7066 statement, and was not surrounded with parenthesis. */
7069 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7070 tree else_block, bool nested_if)
7074 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7075 if (warn_parentheses && nested_if && else_block == NULL)
7077 tree inner_if = then_block;
7079 /* We know from the grammar productions that there is an IF nested
7080 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7081 it might not be exactly THEN_BLOCK, but should be the last
7082 non-container statement within. */
7084 switch (TREE_CODE (inner_if))
7089 inner_if = BIND_EXPR_BODY (inner_if);
7091 case STATEMENT_LIST:
7092 inner_if = expr_last (then_block);
7094 case TRY_FINALLY_EXPR:
7095 case TRY_CATCH_EXPR:
7096 inner_if = TREE_OPERAND (inner_if, 0);
7103 if (COND_EXPR_ELSE (inner_if))
7104 warning (OPT_Wparentheses,
7105 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7109 /* Diagnose ";" via the special empty statement node that we create. */
7112 tree *inner_then = &then_block, *inner_else = &else_block;
7114 if (TREE_CODE (*inner_then) == STATEMENT_LIST
7115 && STATEMENT_LIST_TAIL (*inner_then))
7116 inner_then = &STATEMENT_LIST_TAIL (*inner_then)->stmt;
7117 if (*inner_else && TREE_CODE (*inner_else) == STATEMENT_LIST
7118 && STATEMENT_LIST_TAIL (*inner_else))
7119 inner_else = &STATEMENT_LIST_TAIL (*inner_else)->stmt;
7121 if (TREE_CODE (*inner_then) == NOP_EXPR && !TREE_TYPE (*inner_then))
7124 warning (0, "%Hempty body in an if-statement",
7125 EXPR_LOCUS (*inner_then));
7127 *inner_then = alloc_stmt_list ();
7130 && TREE_CODE (*inner_else) == NOP_EXPR
7131 && !TREE_TYPE (*inner_else))
7133 warning (0, "%Hempty body in an else-statement",
7134 EXPR_LOCUS (*inner_else));
7136 *inner_else = alloc_stmt_list ();
7140 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7141 SET_EXPR_LOCATION (stmt, if_locus);
7145 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7146 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7147 is false for DO loops. INCR is the FOR increment expression. BODY is
7148 the statement controlled by the loop. BLAB is the break label. CLAB is
7149 the continue label. Everything is allowed to be NULL. */
7152 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7153 tree blab, tree clab, bool cond_is_first)
7155 tree entry = NULL, exit = NULL, t;
7157 /* If the condition is zero don't generate a loop construct. */
7158 if (cond && integer_zerop (cond))
7162 t = build_and_jump (&blab);
7163 SET_EXPR_LOCATION (t, start_locus);
7169 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7171 /* If we have an exit condition, then we build an IF with gotos either
7172 out of the loop, or to the top of it. If there's no exit condition,
7173 then we just build a jump back to the top. */
7174 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7176 if (cond && !integer_nonzerop (cond))
7178 /* Canonicalize the loop condition to the end. This means
7179 generating a branch to the loop condition. Reuse the
7180 continue label, if possible. */
7185 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7186 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7189 t = build1 (GOTO_EXPR, void_type_node, clab);
7190 SET_EXPR_LOCATION (t, start_locus);
7194 t = build_and_jump (&blab);
7195 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7197 SET_EXPR_LOCATION (exit, start_locus);
7199 SET_EXPR_LOCATION (exit, input_location);
7208 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7216 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7220 c_finish_bc_stmt (tree *label_p, bool is_break)
7223 tree label = *label_p;
7225 /* In switch statements break is sometimes stylistically used after
7226 a return statement. This can lead to spurious warnings about
7227 control reaching the end of a non-void function when it is
7228 inlined. Note that we are calling block_may_fallthru with
7229 language specific tree nodes; this works because
7230 block_may_fallthru returns true when given something it does not
7232 skip = !block_may_fallthru (cur_stmt_list);
7237 *label_p = label = create_artificial_label ();
7239 else if (TREE_CODE (label) != LABEL_DECL)
7242 error ("break statement not within loop or switch");
7244 error ("continue statement not within a loop");
7251 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7254 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7257 emit_side_effect_warnings (tree expr)
7259 if (expr == error_mark_node)
7261 else if (!TREE_SIDE_EFFECTS (expr))
7263 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7264 warning (0, "%Hstatement with no effect",
7265 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7267 else if (warn_unused_value)
7268 warn_if_unused_value (expr, input_location);
7271 /* Process an expression as if it were a complete statement. Emit
7272 diagnostics, but do not call ADD_STMT. */
7275 c_process_expr_stmt (tree expr)
7280 if (warn_sequence_point)
7281 verify_sequence_points (expr);
7283 if (TREE_TYPE (expr) != error_mark_node
7284 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7285 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7286 error ("expression statement has incomplete type");
7288 /* If we're not processing a statement expression, warn about unused values.
7289 Warnings for statement expressions will be emitted later, once we figure
7290 out which is the result. */
7291 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7292 && (extra_warnings || warn_unused_value))
7293 emit_side_effect_warnings (expr);
7295 /* If the expression is not of a type to which we cannot assign a line
7296 number, wrap the thing in a no-op NOP_EXPR. */
7297 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7298 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7301 SET_EXPR_LOCATION (expr, input_location);
7306 /* Emit an expression as a statement. */
7309 c_finish_expr_stmt (tree expr)
7312 return add_stmt (c_process_expr_stmt (expr));
7317 /* Do the opposite and emit a statement as an expression. To begin,
7318 create a new binding level and return it. */
7321 c_begin_stmt_expr (void)
7324 struct c_label_context_se *nstack;
7325 struct c_label_list *glist;
7327 /* We must force a BLOCK for this level so that, if it is not expanded
7328 later, there is a way to turn off the entire subtree of blocks that
7329 are contained in it. */
7331 ret = c_begin_compound_stmt (true);
7334 c_switch_stack->blocked_stmt_expr++;
7335 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7337 for (glist = label_context_stack_se->labels_used;
7339 glist = glist->next)
7341 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7343 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7344 nstack->labels_def = NULL;
7345 nstack->labels_used = NULL;
7346 nstack->next = label_context_stack_se;
7347 label_context_stack_se = nstack;
7349 /* Mark the current statement list as belonging to a statement list. */
7350 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7356 c_finish_stmt_expr (tree body)
7358 tree last, type, tmp, val;
7360 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7362 body = c_end_compound_stmt (body, true);
7365 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7366 c_switch_stack->blocked_stmt_expr--;
7368 /* It is no longer possible to jump to labels defined within this
7369 statement expression. */
7370 for (dlist = label_context_stack_se->labels_def;
7372 dlist = dlist->next)
7374 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7376 /* It is again possible to define labels with a goto just outside
7377 this statement expression. */
7378 for (glist = label_context_stack_se->next->labels_used;
7380 glist = glist->next)
7382 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7385 if (glist_prev != NULL)
7386 glist_prev->next = label_context_stack_se->labels_used;
7388 label_context_stack_se->next->labels_used
7389 = label_context_stack_se->labels_used;
7390 label_context_stack_se = label_context_stack_se->next;
7392 /* Locate the last statement in BODY. See c_end_compound_stmt
7393 about always returning a BIND_EXPR. */
7394 last_p = &BIND_EXPR_BODY (body);
7395 last = BIND_EXPR_BODY (body);
7398 if (TREE_CODE (last) == STATEMENT_LIST)
7400 tree_stmt_iterator i;
7402 /* This can happen with degenerate cases like ({ }). No value. */
7403 if (!TREE_SIDE_EFFECTS (last))
7406 /* If we're supposed to generate side effects warnings, process
7407 all of the statements except the last. */
7408 if (extra_warnings || warn_unused_value)
7410 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7411 emit_side_effect_warnings (tsi_stmt (i));
7414 i = tsi_last (last);
7415 last_p = tsi_stmt_ptr (i);
7419 /* If the end of the list is exception related, then the list was split
7420 by a call to push_cleanup. Continue searching. */
7421 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7422 || TREE_CODE (last) == TRY_CATCH_EXPR)
7424 last_p = &TREE_OPERAND (last, 0);
7426 goto continue_searching;
7429 /* In the case that the BIND_EXPR is not necessary, return the
7430 expression out from inside it. */
7431 if (last == error_mark_node
7432 || (last == BIND_EXPR_BODY (body)
7433 && BIND_EXPR_VARS (body) == NULL))
7435 /* Do not warn if the return value of a statement expression is
7438 TREE_NO_WARNING (last) = 1;
7442 /* Extract the type of said expression. */
7443 type = TREE_TYPE (last);
7445 /* If we're not returning a value at all, then the BIND_EXPR that
7446 we already have is a fine expression to return. */
7447 if (!type || VOID_TYPE_P (type))
7450 /* Now that we've located the expression containing the value, it seems
7451 silly to make voidify_wrapper_expr repeat the process. Create a
7452 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7453 tmp = create_tmp_var_raw (type, NULL);
7455 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7456 tree_expr_nonnegative_p giving up immediately. */
7458 if (TREE_CODE (val) == NOP_EXPR
7459 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7460 val = TREE_OPERAND (val, 0);
7462 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7463 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7465 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7468 /* Begin the scope of an identifier of variably modified type, scope
7469 number SCOPE. Jumping from outside this scope to inside it is not
7473 c_begin_vm_scope (unsigned int scope)
7475 struct c_label_context_vm *nstack;
7476 struct c_label_list *glist;
7478 gcc_assert (scope > 0);
7479 if (c_switch_stack && !c_switch_stack->blocked_vm)
7480 c_switch_stack->blocked_vm = scope;
7481 for (glist = label_context_stack_vm->labels_used;
7483 glist = glist->next)
7485 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7487 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7488 nstack->labels_def = NULL;
7489 nstack->labels_used = NULL;
7490 nstack->scope = scope;
7491 nstack->next = label_context_stack_vm;
7492 label_context_stack_vm = nstack;
7495 /* End a scope which may contain identifiers of variably modified
7496 type, scope number SCOPE. */
7499 c_end_vm_scope (unsigned int scope)
7501 if (label_context_stack_vm == NULL)
7503 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7504 c_switch_stack->blocked_vm = 0;
7505 /* We may have a number of nested scopes of identifiers with
7506 variably modified type, all at this depth. Pop each in turn. */
7507 while (label_context_stack_vm->scope == scope)
7509 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7511 /* It is no longer possible to jump to labels defined within this
7513 for (dlist = label_context_stack_vm->labels_def;
7515 dlist = dlist->next)
7517 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7519 /* It is again possible to define labels with a goto just outside
7521 for (glist = label_context_stack_vm->next->labels_used;
7523 glist = glist->next)
7525 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7528 if (glist_prev != NULL)
7529 glist_prev->next = label_context_stack_vm->labels_used;
7531 label_context_stack_vm->next->labels_used
7532 = label_context_stack_vm->labels_used;
7533 label_context_stack_vm = label_context_stack_vm->next;
7537 /* Begin and end compound statements. This is as simple as pushing
7538 and popping new statement lists from the tree. */
7541 c_begin_compound_stmt (bool do_scope)
7543 tree stmt = push_stmt_list ();
7550 c_end_compound_stmt (tree stmt, bool do_scope)
7556 if (c_dialect_objc ())
7557 objc_clear_super_receiver ();
7558 block = pop_scope ();
7561 stmt = pop_stmt_list (stmt);
7562 stmt = c_build_bind_expr (block, stmt);
7564 /* If this compound statement is nested immediately inside a statement
7565 expression, then force a BIND_EXPR to be created. Otherwise we'll
7566 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7567 STATEMENT_LISTs merge, and thus we can lose track of what statement
7570 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7571 && TREE_CODE (stmt) != BIND_EXPR)
7573 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7574 TREE_SIDE_EFFECTS (stmt) = 1;
7580 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7581 when the current scope is exited. EH_ONLY is true when this is not
7582 meant to apply to normal control flow transfer. */
7585 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7587 enum tree_code code;
7591 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7592 stmt = build_stmt (code, NULL, cleanup);
7594 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7595 list = push_stmt_list ();
7596 TREE_OPERAND (stmt, 0) = list;
7597 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7600 /* Build a binary-operation expression without default conversions.
7601 CODE is the kind of expression to build.
7602 This function differs from `build' in several ways:
7603 the data type of the result is computed and recorded in it,
7604 warnings are generated if arg data types are invalid,
7605 special handling for addition and subtraction of pointers is known,
7606 and some optimization is done (operations on narrow ints
7607 are done in the narrower type when that gives the same result).
7608 Constant folding is also done before the result is returned.
7610 Note that the operands will never have enumeral types, or function
7611 or array types, because either they will have the default conversions
7612 performed or they have both just been converted to some other type in which
7613 the arithmetic is to be done. */
7616 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7620 enum tree_code code0, code1;
7622 const char *invalid_op_diag;
7624 /* Expression code to give to the expression when it is built.
7625 Normally this is CODE, which is what the caller asked for,
7626 but in some special cases we change it. */
7627 enum tree_code resultcode = code;
7629 /* Data type in which the computation is to be performed.
7630 In the simplest cases this is the common type of the arguments. */
7631 tree result_type = NULL;
7633 /* Nonzero means operands have already been type-converted
7634 in whatever way is necessary.
7635 Zero means they need to be converted to RESULT_TYPE. */
7638 /* Nonzero means create the expression with this type, rather than
7640 tree build_type = 0;
7642 /* Nonzero means after finally constructing the expression
7643 convert it to this type. */
7644 tree final_type = 0;
7646 /* Nonzero if this is an operation like MIN or MAX which can
7647 safely be computed in short if both args are promoted shorts.
7648 Also implies COMMON.
7649 -1 indicates a bitwise operation; this makes a difference
7650 in the exact conditions for when it is safe to do the operation
7651 in a narrower mode. */
7654 /* Nonzero if this is a comparison operation;
7655 if both args are promoted shorts, compare the original shorts.
7656 Also implies COMMON. */
7657 int short_compare = 0;
7659 /* Nonzero if this is a right-shift operation, which can be computed on the
7660 original short and then promoted if the operand is a promoted short. */
7661 int short_shift = 0;
7663 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7666 /* True means types are compatible as far as ObjC is concerned. */
7671 op0 = default_conversion (orig_op0);
7672 op1 = default_conversion (orig_op1);
7680 type0 = TREE_TYPE (op0);
7681 type1 = TREE_TYPE (op1);
7683 /* The expression codes of the data types of the arguments tell us
7684 whether the arguments are integers, floating, pointers, etc. */
7685 code0 = TREE_CODE (type0);
7686 code1 = TREE_CODE (type1);
7688 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7689 STRIP_TYPE_NOPS (op0);
7690 STRIP_TYPE_NOPS (op1);
7692 /* If an error was already reported for one of the arguments,
7693 avoid reporting another error. */
7695 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7696 return error_mark_node;
7698 if ((invalid_op_diag
7699 = targetm.invalid_binary_op (code, type0, type1)))
7701 error (invalid_op_diag);
7702 return error_mark_node;
7705 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7710 /* Handle the pointer + int case. */
7711 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7712 return pointer_int_sum (PLUS_EXPR, op0, op1);
7713 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7714 return pointer_int_sum (PLUS_EXPR, op1, op0);
7720 /* Subtraction of two similar pointers.
7721 We must subtract them as integers, then divide by object size. */
7722 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7723 && comp_target_types (type0, type1))
7724 return pointer_diff (op0, op1);
7725 /* Handle pointer minus int. Just like pointer plus int. */
7726 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7727 return pointer_int_sum (MINUS_EXPR, op0, op1);
7736 case TRUNC_DIV_EXPR:
7738 case FLOOR_DIV_EXPR:
7739 case ROUND_DIV_EXPR:
7740 case EXACT_DIV_EXPR:
7741 /* Floating point division by zero is a legitimate way to obtain
7742 infinities and NaNs. */
7743 if (skip_evaluation == 0 && integer_zerop (op1))
7744 warning (OPT_Wdiv_by_zero, "division by zero");
7746 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7747 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7748 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7749 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7751 enum tree_code tcode0 = code0, tcode1 = code1;
7753 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7754 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7755 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7756 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7758 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
7759 resultcode = RDIV_EXPR;
7761 /* Although it would be tempting to shorten always here, that
7762 loses on some targets, since the modulo instruction is
7763 undefined if the quotient can't be represented in the
7764 computation mode. We shorten only if unsigned or if
7765 dividing by something we know != -1. */
7766 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7767 || (TREE_CODE (op1) == INTEGER_CST
7768 && !integer_all_onesp (op1)));
7776 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7778 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7782 case TRUNC_MOD_EXPR:
7783 case FLOOR_MOD_EXPR:
7784 if (skip_evaluation == 0 && integer_zerop (op1))
7785 warning (OPT_Wdiv_by_zero, "division by zero");
7787 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7789 /* Although it would be tempting to shorten always here, that loses
7790 on some targets, since the modulo instruction is undefined if the
7791 quotient can't be represented in the computation mode. We shorten
7792 only if unsigned or if dividing by something we know != -1. */
7793 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7794 || (TREE_CODE (op1) == INTEGER_CST
7795 && !integer_all_onesp (op1)));
7800 case TRUTH_ANDIF_EXPR:
7801 case TRUTH_ORIF_EXPR:
7802 case TRUTH_AND_EXPR:
7804 case TRUTH_XOR_EXPR:
7805 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7806 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7807 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7808 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7810 /* Result of these operations is always an int,
7811 but that does not mean the operands should be
7812 converted to ints! */
7813 result_type = integer_type_node;
7814 op0 = c_common_truthvalue_conversion (op0);
7815 op1 = c_common_truthvalue_conversion (op1);
7820 /* Shift operations: result has same type as first operand;
7821 always convert second operand to int.
7822 Also set SHORT_SHIFT if shifting rightward. */
7825 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7827 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7829 if (tree_int_cst_sgn (op1) < 0)
7830 warning (0, "right shift count is negative");
7833 if (!integer_zerop (op1))
7836 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7837 warning (0, "right shift count >= width of type");
7841 /* Use the type of the value to be shifted. */
7842 result_type = type0;
7843 /* Convert the shift-count to an integer, regardless of size
7844 of value being shifted. */
7845 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7846 op1 = convert (integer_type_node, op1);
7847 /* Avoid converting op1 to result_type later. */
7853 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7855 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7857 if (tree_int_cst_sgn (op1) < 0)
7858 warning (0, "left shift count is negative");
7860 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7861 warning (0, "left shift count >= width of type");
7864 /* Use the type of the value to be shifted. */
7865 result_type = type0;
7866 /* Convert the shift-count to an integer, regardless of size
7867 of value being shifted. */
7868 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7869 op1 = convert (integer_type_node, op1);
7870 /* Avoid converting op1 to result_type later. */
7877 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
7878 warning (OPT_Wfloat_equal,
7879 "comparing floating point with == or != is unsafe");
7880 /* Result of comparison is always int,
7881 but don't convert the args to int! */
7882 build_type = integer_type_node;
7883 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7884 || code0 == COMPLEX_TYPE)
7885 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7886 || code1 == COMPLEX_TYPE))
7888 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7890 tree tt0 = TREE_TYPE (type0);
7891 tree tt1 = TREE_TYPE (type1);
7892 /* Anything compares with void *. void * compares with anything.
7893 Otherwise, the targets must be compatible
7894 and both must be object or both incomplete. */
7895 if (comp_target_types (type0, type1))
7896 result_type = common_pointer_type (type0, type1);
7897 else if (VOID_TYPE_P (tt0))
7899 /* op0 != orig_op0 detects the case of something
7900 whose value is 0 but which isn't a valid null ptr const. */
7901 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7902 && TREE_CODE (tt1) == FUNCTION_TYPE)
7903 pedwarn ("ISO C forbids comparison of %<void *%>"
7904 " with function pointer");
7906 else if (VOID_TYPE_P (tt1))
7908 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7909 && TREE_CODE (tt0) == FUNCTION_TYPE)
7910 pedwarn ("ISO C forbids comparison of %<void *%>"
7911 " with function pointer");
7914 /* Avoid warning about the volatile ObjC EH puts on decls. */
7916 pedwarn ("comparison of distinct pointer types lacks a cast");
7918 if (result_type == NULL_TREE)
7919 result_type = ptr_type_node;
7921 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7922 && integer_zerop (op1))
7923 result_type = type0;
7924 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7925 && integer_zerop (op0))
7926 result_type = type1;
7927 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7929 result_type = type0;
7930 pedwarn ("comparison between pointer and integer");
7932 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7934 result_type = type1;
7935 pedwarn ("comparison between pointer and integer");
7943 build_type = integer_type_node;
7944 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7945 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7947 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7949 if (comp_target_types (type0, type1))
7951 result_type = common_pointer_type (type0, type1);
7952 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7953 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7954 pedwarn ("comparison of complete and incomplete pointers");
7956 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7957 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7961 result_type = ptr_type_node;
7962 pedwarn ("comparison of distinct pointer types lacks a cast");
7965 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7966 && integer_zerop (op1))
7968 result_type = type0;
7969 if (pedantic || extra_warnings)
7970 pedwarn ("ordered comparison of pointer with integer zero");
7972 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7973 && integer_zerop (op0))
7975 result_type = type1;
7977 pedwarn ("ordered comparison of pointer with integer zero");
7979 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7981 result_type = type0;
7982 pedwarn ("comparison between pointer and integer");
7984 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7986 result_type = type1;
7987 pedwarn ("comparison between pointer and integer");
7995 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7996 return error_mark_node;
7998 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
7999 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8000 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8001 TREE_TYPE (type1))))
8003 binary_op_error (code);
8004 return error_mark_node;
8007 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8008 || code0 == VECTOR_TYPE)
8010 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8011 || code1 == VECTOR_TYPE))
8013 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8015 if (shorten || common || short_compare)
8016 result_type = c_common_type (type0, type1);
8018 /* For certain operations (which identify themselves by shorten != 0)
8019 if both args were extended from the same smaller type,
8020 do the arithmetic in that type and then extend.
8022 shorten !=0 and !=1 indicates a bitwise operation.
8023 For them, this optimization is safe only if
8024 both args are zero-extended or both are sign-extended.
8025 Otherwise, we might change the result.
8026 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8027 but calculated in (unsigned short) it would be (unsigned short)-1. */
8029 if (shorten && none_complex)
8031 int unsigned0, unsigned1;
8032 tree arg0 = get_narrower (op0, &unsigned0);
8033 tree arg1 = get_narrower (op1, &unsigned1);
8034 /* UNS is 1 if the operation to be done is an unsigned one. */
8035 int uns = TYPE_UNSIGNED (result_type);
8038 final_type = result_type;
8040 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8041 but it *requires* conversion to FINAL_TYPE. */
8043 if ((TYPE_PRECISION (TREE_TYPE (op0))
8044 == TYPE_PRECISION (TREE_TYPE (arg0)))
8045 && TREE_TYPE (op0) != final_type)
8046 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8047 if ((TYPE_PRECISION (TREE_TYPE (op1))
8048 == TYPE_PRECISION (TREE_TYPE (arg1)))
8049 && TREE_TYPE (op1) != final_type)
8050 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8052 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8054 /* For bitwise operations, signedness of nominal type
8055 does not matter. Consider only how operands were extended. */
8059 /* Note that in all three cases below we refrain from optimizing
8060 an unsigned operation on sign-extended args.
8061 That would not be valid. */
8063 /* Both args variable: if both extended in same way
8064 from same width, do it in that width.
8065 Do it unsigned if args were zero-extended. */
8066 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8067 < TYPE_PRECISION (result_type))
8068 && (TYPE_PRECISION (TREE_TYPE (arg1))
8069 == TYPE_PRECISION (TREE_TYPE (arg0)))
8070 && unsigned0 == unsigned1
8071 && (unsigned0 || !uns))
8073 = c_common_signed_or_unsigned_type
8074 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8075 else if (TREE_CODE (arg0) == INTEGER_CST
8076 && (unsigned1 || !uns)
8077 && (TYPE_PRECISION (TREE_TYPE (arg1))
8078 < TYPE_PRECISION (result_type))
8080 = c_common_signed_or_unsigned_type (unsigned1,
8082 int_fits_type_p (arg0, type)))
8084 else if (TREE_CODE (arg1) == INTEGER_CST
8085 && (unsigned0 || !uns)
8086 && (TYPE_PRECISION (TREE_TYPE (arg0))
8087 < TYPE_PRECISION (result_type))
8089 = c_common_signed_or_unsigned_type (unsigned0,
8091 int_fits_type_p (arg1, type)))
8095 /* Shifts can be shortened if shifting right. */
8100 tree arg0 = get_narrower (op0, &unsigned_arg);
8102 final_type = result_type;
8104 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8105 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8107 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8108 /* We can shorten only if the shift count is less than the
8109 number of bits in the smaller type size. */
8110 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8111 /* We cannot drop an unsigned shift after sign-extension. */
8112 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8114 /* Do an unsigned shift if the operand was zero-extended. */
8116 = c_common_signed_or_unsigned_type (unsigned_arg,
8118 /* Convert value-to-be-shifted to that type. */
8119 if (TREE_TYPE (op0) != result_type)
8120 op0 = convert (result_type, op0);
8125 /* Comparison operations are shortened too but differently.
8126 They identify themselves by setting short_compare = 1. */
8130 /* Don't write &op0, etc., because that would prevent op0
8131 from being kept in a register.
8132 Instead, make copies of the our local variables and
8133 pass the copies by reference, then copy them back afterward. */
8134 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8135 enum tree_code xresultcode = resultcode;
8137 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8142 op0 = xop0, op1 = xop1;
8144 resultcode = xresultcode;
8146 if (warn_sign_compare && skip_evaluation == 0)
8148 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8149 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8150 int unsignedp0, unsignedp1;
8151 tree primop0 = get_narrower (op0, &unsignedp0);
8152 tree primop1 = get_narrower (op1, &unsignedp1);
8156 STRIP_TYPE_NOPS (xop0);
8157 STRIP_TYPE_NOPS (xop1);
8159 /* Give warnings for comparisons between signed and unsigned
8160 quantities that may fail.
8162 Do the checking based on the original operand trees, so that
8163 casts will be considered, but default promotions won't be.
8165 Do not warn if the comparison is being done in a signed type,
8166 since the signed type will only be chosen if it can represent
8167 all the values of the unsigned type. */
8168 if (!TYPE_UNSIGNED (result_type))
8170 /* Do not warn if both operands are the same signedness. */
8171 else if (op0_signed == op1_signed)
8178 sop = xop0, uop = xop1;
8180 sop = xop1, uop = xop0;
8182 /* Do not warn if the signed quantity is an
8183 unsuffixed integer literal (or some static
8184 constant expression involving such literals or a
8185 conditional expression involving such literals)
8186 and it is non-negative. */
8187 if (tree_expr_nonnegative_p (sop))
8189 /* Do not warn if the comparison is an equality operation,
8190 the unsigned quantity is an integral constant, and it
8191 would fit in the result if the result were signed. */
8192 else if (TREE_CODE (uop) == INTEGER_CST
8193 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8195 (uop, c_common_signed_type (result_type)))
8197 /* Do not warn if the unsigned quantity is an enumeration
8198 constant and its maximum value would fit in the result
8199 if the result were signed. */
8200 else if (TREE_CODE (uop) == INTEGER_CST
8201 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8203 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8204 c_common_signed_type (result_type)))
8207 warning (0, "comparison between signed and unsigned");
8210 /* Warn if two unsigned values are being compared in a size
8211 larger than their original size, and one (and only one) is the
8212 result of a `~' operator. This comparison will always fail.
8214 Also warn if one operand is a constant, and the constant
8215 does not have all bits set that are set in the ~ operand
8216 when it is extended. */
8218 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8219 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8221 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8222 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8225 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8228 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8231 HOST_WIDE_INT constant, mask;
8232 int unsignedp, bits;
8234 if (host_integerp (primop0, 0))
8237 unsignedp = unsignedp1;
8238 constant = tree_low_cst (primop0, 0);
8243 unsignedp = unsignedp0;
8244 constant = tree_low_cst (primop1, 0);
8247 bits = TYPE_PRECISION (TREE_TYPE (primop));
8248 if (bits < TYPE_PRECISION (result_type)
8249 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8251 mask = (~(HOST_WIDE_INT) 0) << bits;
8252 if ((mask & constant) != mask)
8253 warning (0, "comparison of promoted ~unsigned with constant");
8256 else if (unsignedp0 && unsignedp1
8257 && (TYPE_PRECISION (TREE_TYPE (primop0))
8258 < TYPE_PRECISION (result_type))
8259 && (TYPE_PRECISION (TREE_TYPE (primop1))
8260 < TYPE_PRECISION (result_type)))
8261 warning (0, "comparison of promoted ~unsigned with unsigned");
8267 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8268 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8269 Then the expression will be built.
8270 It will be given type FINAL_TYPE if that is nonzero;
8271 otherwise, it will be given type RESULT_TYPE. */
8275 binary_op_error (code);
8276 return error_mark_node;
8281 if (TREE_TYPE (op0) != result_type)
8282 op0 = convert (result_type, op0);
8283 if (TREE_TYPE (op1) != result_type)
8284 op1 = convert (result_type, op1);
8286 /* This can happen if one operand has a vector type, and the other
8287 has a different type. */
8288 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8289 return error_mark_node;
8292 if (build_type == NULL_TREE)
8293 build_type = result_type;
8296 /* Treat expressions in initializers specially as they can't trap. */
8297 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8300 : fold_build2 (resultcode, build_type,
8303 if (final_type != 0)
8304 result = convert (final_type, result);
8310 /* Convert EXPR to be a truth-value, validating its type for this
8314 c_objc_common_truthvalue_conversion (tree expr)
8316 switch (TREE_CODE (TREE_TYPE (expr)))
8319 error ("used array that cannot be converted to pointer where scalar is required");
8320 return error_mark_node;
8323 error ("used struct type value where scalar is required");
8324 return error_mark_node;
8327 error ("used union type value where scalar is required");
8328 return error_mark_node;
8337 /* ??? Should we also give an error for void and vectors rather than
8338 leaving those to give errors later? */
8339 return c_common_truthvalue_conversion (expr);
8343 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8347 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8348 bool *ti ATTRIBUTE_UNUSED, bool *se)
8350 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8352 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8353 /* Executing a compound literal inside a function reinitializes
8355 if (!TREE_STATIC (decl))