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, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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"
33 #include "langhooks.h"
40 #include "tree-iterator.h"
43 #include "c-family/c-objc.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
54 /* The level of nesting inside "__alignof__". */
57 /* The level of nesting inside "sizeof". */
60 /* The level of nesting inside "typeof". */
63 /* Nonzero if we've already printed a "missing braces around initializer"
64 message within this initializer. */
65 static int missing_braces_mentioned;
67 static int require_constant_value;
68 static int require_constant_elements;
70 static bool null_pointer_constant_p (const_tree);
71 static tree qualify_type (tree, tree);
72 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
74 static int comp_target_types (location_t, tree, tree);
75 static int function_types_compatible_p (const_tree, const_tree, bool *,
77 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
78 static tree lookup_field (tree, tree);
79 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
81 static tree pointer_diff (location_t, tree, tree);
82 static tree convert_for_assignment (location_t, tree, tree, tree,
83 enum impl_conv, bool, tree, tree, int);
84 static tree valid_compound_expr_initializer (tree, tree);
85 static void push_string (const char *);
86 static void push_member_name (tree);
87 static int spelling_length (void);
88 static char *print_spelling (char *);
89 static void warning_init (int, const char *);
90 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
91 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
93 static void output_pending_init_elements (int, struct obstack *);
94 static int set_designator (int, struct obstack *);
95 static void push_range_stack (tree, struct obstack *);
96 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
97 static void set_nonincremental_init (struct obstack *);
98 static void set_nonincremental_init_from_string (tree, struct obstack *);
99 static tree find_init_member (tree, struct obstack *);
100 static void readonly_error (tree, enum lvalue_use);
101 static void readonly_warning (tree, enum lvalue_use);
102 static int lvalue_or_else (const_tree, enum lvalue_use);
103 static void record_maybe_used_decl (tree);
104 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
106 /* Return true if EXP is a null pointer constant, false otherwise. */
109 null_pointer_constant_p (const_tree expr)
111 /* This should really operate on c_expr structures, but they aren't
112 yet available everywhere required. */
113 tree type = TREE_TYPE (expr);
114 return (TREE_CODE (expr) == INTEGER_CST
115 && !TREE_OVERFLOW (expr)
116 && integer_zerop (expr)
117 && (INTEGRAL_TYPE_P (type)
118 || (TREE_CODE (type) == POINTER_TYPE
119 && VOID_TYPE_P (TREE_TYPE (type))
120 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
123 /* EXPR may appear in an unevaluated part of an integer constant
124 expression, but not in an evaluated part. Wrap it in a
125 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
126 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
129 note_integer_operands (tree expr)
132 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
134 ret = copy_node (expr);
135 TREE_OVERFLOW (ret) = 1;
139 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
140 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
145 /* Having checked whether EXPR may appear in an unevaluated part of an
146 integer constant expression and found that it may, remove any
147 C_MAYBE_CONST_EXPR noting this fact and return the resulting
151 remove_c_maybe_const_expr (tree expr)
153 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
154 return C_MAYBE_CONST_EXPR_EXPR (expr);
159 \f/* This is a cache to hold if two types are compatible or not. */
161 struct tagged_tu_seen_cache {
162 const struct tagged_tu_seen_cache * next;
165 /* The return value of tagged_types_tu_compatible_p if we had seen
166 these two types already. */
170 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
171 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
173 /* Do `exp = require_complete_type (exp);' to make sure exp
174 does not have an incomplete type. (That includes void types.) */
177 require_complete_type (tree value)
179 tree type = TREE_TYPE (value);
181 if (value == error_mark_node || type == error_mark_node)
182 return error_mark_node;
184 /* First, detect a valid value with a complete type. */
185 if (COMPLETE_TYPE_P (type))
188 c_incomplete_type_error (value, type);
189 return error_mark_node;
192 /* Print an error message for invalid use of an incomplete type.
193 VALUE is the expression that was used (or 0 if that isn't known)
194 and TYPE is the type that was invalid. */
197 c_incomplete_type_error (const_tree value, const_tree type)
199 const char *type_code_string;
201 /* Avoid duplicate error message. */
202 if (TREE_CODE (type) == ERROR_MARK)
205 if (value != 0 && (TREE_CODE (value) == VAR_DECL
206 || TREE_CODE (value) == PARM_DECL))
207 error ("%qD has an incomplete type", value);
211 /* We must print an error message. Be clever about what it says. */
213 switch (TREE_CODE (type))
216 type_code_string = "struct";
220 type_code_string = "union";
224 type_code_string = "enum";
228 error ("invalid use of void expression");
232 if (TYPE_DOMAIN (type))
234 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
236 error ("invalid use of flexible array member");
239 type = TREE_TYPE (type);
242 error ("invalid use of array with unspecified bounds");
249 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
250 error ("invalid use of undefined type %<%s %E%>",
251 type_code_string, TYPE_NAME (type));
253 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
254 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
258 /* Given a type, apply default promotions wrt unnamed function
259 arguments and return the new type. */
262 c_type_promotes_to (tree type)
264 if (TYPE_MAIN_VARIANT (type) == float_type_node)
265 return double_type_node;
267 if (c_promoting_integer_type_p (type))
269 /* Preserve unsignedness if not really getting any wider. */
270 if (TYPE_UNSIGNED (type)
271 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
272 return unsigned_type_node;
273 return integer_type_node;
279 /* Return true if between two named address spaces, whether there is a superset
280 named address space that encompasses both address spaces. If there is a
281 superset, return which address space is the superset. */
284 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
291 else if (targetm.addr_space.subset_p (as1, as2))
296 else if (targetm.addr_space.subset_p (as2, as1))
305 /* Return a variant of TYPE which has all the type qualifiers of LIKE
306 as well as those of TYPE. */
309 qualify_type (tree type, tree like)
311 addr_space_t as_type = TYPE_ADDR_SPACE (type);
312 addr_space_t as_like = TYPE_ADDR_SPACE (like);
313 addr_space_t as_common;
315 /* If the two named address spaces are different, determine the common
316 superset address space. If there isn't one, raise an error. */
317 if (!addr_space_superset (as_type, as_like, &as_common))
320 error ("%qT and %qT are in disjoint named address spaces",
324 return c_build_qualified_type (type,
325 TYPE_QUALS_NO_ADDR_SPACE (type)
326 | TYPE_QUALS_NO_ADDR_SPACE (like)
327 | ENCODE_QUAL_ADDR_SPACE (as_common));
330 /* Return true iff the given tree T is a variable length array. */
333 c_vla_type_p (const_tree t)
335 if (TREE_CODE (t) == ARRAY_TYPE
336 && C_TYPE_VARIABLE_SIZE (t))
341 /* Return the composite type of two compatible types.
343 We assume that comptypes has already been done and returned
344 nonzero; if that isn't so, this may crash. In particular, we
345 assume that qualifiers match. */
348 composite_type (tree t1, tree t2)
350 enum tree_code code1;
351 enum tree_code code2;
354 /* Save time if the two types are the same. */
356 if (t1 == t2) return t1;
358 /* If one type is nonsense, use the other. */
359 if (t1 == error_mark_node)
361 if (t2 == error_mark_node)
364 code1 = TREE_CODE (t1);
365 code2 = TREE_CODE (t2);
367 /* Merge the attributes. */
368 attributes = targetm.merge_type_attributes (t1, t2);
370 /* If one is an enumerated type and the other is the compatible
371 integer type, the composite type might be either of the two
372 (DR#013 question 3). For consistency, use the enumerated type as
373 the composite type. */
375 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
377 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
380 gcc_assert (code1 == code2);
385 /* For two pointers, do this recursively on the target type. */
387 tree pointed_to_1 = TREE_TYPE (t1);
388 tree pointed_to_2 = TREE_TYPE (t2);
389 tree target = composite_type (pointed_to_1, pointed_to_2);
390 t1 = build_pointer_type (target);
391 t1 = build_type_attribute_variant (t1, attributes);
392 return qualify_type (t1, t2);
397 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400 tree d1 = TYPE_DOMAIN (t1);
401 tree d2 = TYPE_DOMAIN (t2);
402 bool d1_variable, d2_variable;
403 bool d1_zero, d2_zero;
404 bool t1_complete, t2_complete;
406 /* We should not have any type quals on arrays at all. */
407 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
408 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
410 t1_complete = COMPLETE_TYPE_P (t1);
411 t2_complete = COMPLETE_TYPE_P (t2);
413 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
414 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
416 d1_variable = (!d1_zero
417 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
418 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
419 d2_variable = (!d2_zero
420 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
421 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
422 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
423 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
425 /* Save space: see if the result is identical to one of the args. */
426 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
427 && (d2_variable || d2_zero || !d1_variable))
428 return build_type_attribute_variant (t1, attributes);
429 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
430 && (d1_variable || d1_zero || !d2_variable))
431 return build_type_attribute_variant (t2, attributes);
433 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
434 return build_type_attribute_variant (t1, attributes);
435 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
436 return build_type_attribute_variant (t2, attributes);
438 /* Merge the element types, and have a size if either arg has
439 one. We may have qualifiers on the element types. To set
440 up TYPE_MAIN_VARIANT correctly, we need to form the
441 composite of the unqualified types and add the qualifiers
443 quals = TYPE_QUALS (strip_array_types (elt));
444 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
445 t1 = build_array_type (unqual_elt,
446 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
452 /* Ensure a composite type involving a zero-length array type
453 is a zero-length type not an incomplete type. */
454 if (d1_zero && d2_zero
455 && (t1_complete || t2_complete)
456 && !COMPLETE_TYPE_P (t1))
458 TYPE_SIZE (t1) = bitsize_zero_node;
459 TYPE_SIZE_UNIT (t1) = size_zero_node;
461 t1 = c_build_qualified_type (t1, quals);
462 return build_type_attribute_variant (t1, attributes);
468 if (attributes != NULL)
470 /* Try harder not to create a new aggregate type. */
471 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
473 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
476 return build_type_attribute_variant (t1, attributes);
479 /* Function types: prefer the one that specified arg types.
480 If both do, merge the arg types. Also merge the return types. */
482 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
483 tree p1 = TYPE_ARG_TYPES (t1);
484 tree p2 = TYPE_ARG_TYPES (t2);
489 /* Save space: see if the result is identical to one of the args. */
490 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
491 return build_type_attribute_variant (t1, attributes);
492 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
493 return build_type_attribute_variant (t2, attributes);
495 /* Simple way if one arg fails to specify argument types. */
496 if (TYPE_ARG_TYPES (t1) == 0)
498 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
499 t1 = build_type_attribute_variant (t1, attributes);
500 return qualify_type (t1, t2);
502 if (TYPE_ARG_TYPES (t2) == 0)
504 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
505 t1 = build_type_attribute_variant (t1, attributes);
506 return qualify_type (t1, t2);
509 /* If both args specify argument types, we must merge the two
510 lists, argument by argument. */
511 /* Tell global_bindings_p to return false so that variable_size
512 doesn't die on VLAs in parameter types. */
513 c_override_global_bindings_to_false = true;
515 len = list_length (p1);
518 for (i = 0; i < len; i++)
519 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
524 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
526 /* A null type means arg type is not specified.
527 Take whatever the other function type has. */
528 if (TREE_VALUE (p1) == 0)
530 TREE_VALUE (n) = TREE_VALUE (p2);
533 if (TREE_VALUE (p2) == 0)
535 TREE_VALUE (n) = TREE_VALUE (p1);
539 /* Given wait (union {union wait *u; int *i} *)
540 and wait (union wait *),
541 prefer union wait * as type of parm. */
542 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
543 && TREE_VALUE (p1) != TREE_VALUE (p2))
546 tree mv2 = TREE_VALUE (p2);
547 if (mv2 && mv2 != error_mark_node
548 && TREE_CODE (mv2) != ARRAY_TYPE)
549 mv2 = TYPE_MAIN_VARIANT (mv2);
550 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
551 memb; memb = DECL_CHAIN (memb))
553 tree mv3 = TREE_TYPE (memb);
554 if (mv3 && mv3 != error_mark_node
555 && TREE_CODE (mv3) != ARRAY_TYPE)
556 mv3 = TYPE_MAIN_VARIANT (mv3);
557 if (comptypes (mv3, mv2))
559 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
561 pedwarn (input_location, OPT_pedantic,
562 "function types not truly compatible in ISO C");
567 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
568 && TREE_VALUE (p2) != TREE_VALUE (p1))
571 tree mv1 = TREE_VALUE (p1);
572 if (mv1 && mv1 != error_mark_node
573 && TREE_CODE (mv1) != ARRAY_TYPE)
574 mv1 = TYPE_MAIN_VARIANT (mv1);
575 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
576 memb; memb = DECL_CHAIN (memb))
578 tree mv3 = TREE_TYPE (memb);
579 if (mv3 && mv3 != error_mark_node
580 && TREE_CODE (mv3) != ARRAY_TYPE)
581 mv3 = TYPE_MAIN_VARIANT (mv3);
582 if (comptypes (mv3, mv1))
584 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
586 pedwarn (input_location, OPT_pedantic,
587 "function types not truly compatible in ISO C");
592 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
596 c_override_global_bindings_to_false = false;
597 t1 = build_function_type (valtype, newargs);
598 t1 = qualify_type (t1, t2);
599 /* ... falls through ... */
603 return build_type_attribute_variant (t1, attributes);
608 /* Return the type of a conditional expression between pointers to
609 possibly differently qualified versions of compatible types.
611 We assume that comp_target_types has already been done and returned
612 nonzero; if that isn't so, this may crash. */
615 common_pointer_type (tree t1, tree t2)
618 tree pointed_to_1, mv1;
619 tree pointed_to_2, mv2;
621 unsigned target_quals;
622 addr_space_t as1, as2, as_common;
625 /* Save time if the two types are the same. */
627 if (t1 == t2) return t1;
629 /* If one type is nonsense, use the other. */
630 if (t1 == error_mark_node)
632 if (t2 == error_mark_node)
635 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
636 && TREE_CODE (t2) == POINTER_TYPE);
638 /* Merge the attributes. */
639 attributes = targetm.merge_type_attributes (t1, t2);
641 /* Find the composite type of the target types, and combine the
642 qualifiers of the two types' targets. Do not lose qualifiers on
643 array element types by taking the TYPE_MAIN_VARIANT. */
644 mv1 = pointed_to_1 = TREE_TYPE (t1);
645 mv2 = pointed_to_2 = TREE_TYPE (t2);
646 if (TREE_CODE (mv1) != ARRAY_TYPE)
647 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
648 if (TREE_CODE (mv2) != ARRAY_TYPE)
649 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
650 target = composite_type (mv1, mv2);
652 /* For function types do not merge const qualifiers, but drop them
653 if used inconsistently. The middle-end uses these to mark const
654 and noreturn functions. */
655 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
656 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
658 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
659 target_quals = (quals1 & quals2);
661 target_quals = (quals1 | quals2);
663 /* If the two named address spaces are different, determine the common
664 superset address space. This is guaranteed to exist due to the
665 assumption that comp_target_type returned non-zero. */
666 as1 = TYPE_ADDR_SPACE (pointed_to_1);
667 as2 = TYPE_ADDR_SPACE (pointed_to_2);
668 if (!addr_space_superset (as1, as2, &as_common))
671 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
673 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
674 return build_type_attribute_variant (t1, attributes);
677 /* Return the common type for two arithmetic types under the usual
678 arithmetic conversions. The default conversions have already been
679 applied, and enumerated types converted to their compatible integer
680 types. The resulting type is unqualified and has no attributes.
682 This is the type for the result of most arithmetic operations
683 if the operands have the given two types. */
686 c_common_type (tree t1, tree t2)
688 enum tree_code code1;
689 enum tree_code code2;
691 /* If one type is nonsense, use the other. */
692 if (t1 == error_mark_node)
694 if (t2 == error_mark_node)
697 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
698 t1 = TYPE_MAIN_VARIANT (t1);
700 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
701 t2 = TYPE_MAIN_VARIANT (t2);
703 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
704 t1 = build_type_attribute_variant (t1, NULL_TREE);
706 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
707 t2 = build_type_attribute_variant (t2, NULL_TREE);
709 /* Save time if the two types are the same. */
711 if (t1 == t2) return t1;
713 code1 = TREE_CODE (t1);
714 code2 = TREE_CODE (t2);
716 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
717 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
718 || code1 == INTEGER_TYPE);
719 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
720 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
721 || code2 == INTEGER_TYPE);
723 /* When one operand is a decimal float type, the other operand cannot be
724 a generic float type or a complex type. We also disallow vector types
726 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
727 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
729 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
731 error ("can%'t mix operands of decimal float and vector types");
732 return error_mark_node;
734 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
736 error ("can%'t mix operands of decimal float and complex types");
737 return error_mark_node;
739 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
741 error ("can%'t mix operands of decimal float and other float types");
742 return error_mark_node;
746 /* If one type is a vector type, return that type. (How the usual
747 arithmetic conversions apply to the vector types extension is not
748 precisely specified.) */
749 if (code1 == VECTOR_TYPE)
752 if (code2 == VECTOR_TYPE)
755 /* If one type is complex, form the common type of the non-complex
756 components, then make that complex. Use T1 or T2 if it is the
758 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
760 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
761 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
762 tree subtype = c_common_type (subtype1, subtype2);
764 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
766 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
769 return build_complex_type (subtype);
772 /* If only one is real, use it as the result. */
774 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
777 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
780 /* If both are real and either are decimal floating point types, use
781 the decimal floating point type with the greater precision. */
783 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
785 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
786 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
787 return dfloat128_type_node;
788 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
789 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
790 return dfloat64_type_node;
791 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
793 return dfloat32_type_node;
796 /* Deal with fixed-point types. */
797 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
799 unsigned int unsignedp = 0, satp = 0;
800 enum machine_mode m1, m2;
801 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
806 /* If one input type is saturating, the result type is saturating. */
807 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
810 /* If both fixed-point types are unsigned, the result type is unsigned.
811 When mixing fixed-point and integer types, follow the sign of the
813 Otherwise, the result type is signed. */
814 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
815 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
816 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
817 && TYPE_UNSIGNED (t1))
818 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
819 && TYPE_UNSIGNED (t2)))
822 /* The result type is signed. */
825 /* If the input type is unsigned, we need to convert to the
827 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
829 enum mode_class mclass = (enum mode_class) 0;
830 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
832 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
836 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
838 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
840 enum mode_class mclass = (enum mode_class) 0;
841 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
843 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
847 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
851 if (code1 == FIXED_POINT_TYPE)
853 fbit1 = GET_MODE_FBIT (m1);
854 ibit1 = GET_MODE_IBIT (m1);
859 /* Signed integers need to subtract one sign bit. */
860 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
863 if (code2 == FIXED_POINT_TYPE)
865 fbit2 = GET_MODE_FBIT (m2);
866 ibit2 = GET_MODE_IBIT (m2);
871 /* Signed integers need to subtract one sign bit. */
872 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
875 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
876 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
877 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
881 /* Both real or both integers; use the one with greater precision. */
883 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
885 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
888 /* Same precision. Prefer long longs to longs to ints when the
889 same precision, following the C99 rules on integer type rank
890 (which are equivalent to the C90 rules for C90 types). */
892 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
893 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
894 return long_long_unsigned_type_node;
896 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
897 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
899 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
900 return long_long_unsigned_type_node;
902 return long_long_integer_type_node;
905 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
906 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
907 return long_unsigned_type_node;
909 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
912 /* But preserve unsignedness from the other type,
913 since long cannot hold all the values of an unsigned int. */
914 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
915 return long_unsigned_type_node;
917 return long_integer_type_node;
920 /* Likewise, prefer long double to double even if same size. */
921 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
923 return long_double_type_node;
925 /* Otherwise prefer the unsigned one. */
927 if (TYPE_UNSIGNED (t1))
933 /* Wrapper around c_common_type that is used by c-common.c and other
934 front end optimizations that remove promotions. ENUMERAL_TYPEs
935 are allowed here and are converted to their compatible integer types.
936 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
937 preferably a non-Boolean type as the common type. */
939 common_type (tree t1, tree t2)
941 if (TREE_CODE (t1) == ENUMERAL_TYPE)
942 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
943 if (TREE_CODE (t2) == ENUMERAL_TYPE)
944 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
946 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
947 if (TREE_CODE (t1) == BOOLEAN_TYPE
948 && TREE_CODE (t2) == BOOLEAN_TYPE)
949 return boolean_type_node;
951 /* If either type is BOOLEAN_TYPE, then return the other. */
952 if (TREE_CODE (t1) == BOOLEAN_TYPE)
954 if (TREE_CODE (t2) == BOOLEAN_TYPE)
957 return c_common_type (t1, t2);
960 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
961 or various other operations. Return 2 if they are compatible
962 but a warning may be needed if you use them together. */
965 comptypes (tree type1, tree type2)
967 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
970 val = comptypes_internal (type1, type2, NULL, NULL);
971 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
976 /* Like comptypes, but if it returns non-zero because enum and int are
977 compatible, it sets *ENUM_AND_INT_P to true. */
980 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
982 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
985 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
986 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
991 /* Like comptypes, but if it returns nonzero for different types, it
992 sets *DIFFERENT_TYPES_P to true. */
995 comptypes_check_different_types (tree type1, tree type2,
996 bool *different_types_p)
998 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1001 val = comptypes_internal (type1, type2, NULL, different_types_p);
1002 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1007 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1008 or various other operations. Return 2 if they are compatible
1009 but a warning may be needed if you use them together. If
1010 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1011 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1012 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1013 NULL, and the types are compatible but different enough not to be
1014 permitted in C1X typedef redeclarations, then this sets
1015 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1016 false, but may or may not be set if the types are incompatible.
1017 This differs from comptypes, in that we don't free the seen
1021 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1022 bool *different_types_p)
1024 const_tree t1 = type1;
1025 const_tree t2 = type2;
1028 /* Suppress errors caused by previously reported errors. */
1030 if (t1 == t2 || !t1 || !t2
1031 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1034 /* Enumerated types are compatible with integer types, but this is
1035 not transitive: two enumerated types in the same translation unit
1036 are compatible with each other only if they are the same type. */
1038 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1040 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1041 if (TREE_CODE (t2) != VOID_TYPE)
1043 if (enum_and_int_p != NULL)
1044 *enum_and_int_p = true;
1045 if (different_types_p != NULL)
1046 *different_types_p = true;
1049 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1051 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1052 if (TREE_CODE (t1) != VOID_TYPE)
1054 if (enum_and_int_p != NULL)
1055 *enum_and_int_p = true;
1056 if (different_types_p != NULL)
1057 *different_types_p = true;
1064 /* Different classes of types can't be compatible. */
1066 if (TREE_CODE (t1) != TREE_CODE (t2))
1069 /* Qualifiers must match. C99 6.7.3p9 */
1071 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1074 /* Allow for two different type nodes which have essentially the same
1075 definition. Note that we already checked for equality of the type
1076 qualifiers (just above). */
1078 if (TREE_CODE (t1) != ARRAY_TYPE
1079 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1082 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1083 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1086 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1089 switch (TREE_CODE (t1))
1092 /* Do not remove mode or aliasing information. */
1093 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1094 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1096 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1097 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1098 enum_and_int_p, different_types_p));
1102 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1108 tree d1 = TYPE_DOMAIN (t1);
1109 tree d2 = TYPE_DOMAIN (t2);
1110 bool d1_variable, d2_variable;
1111 bool d1_zero, d2_zero;
1114 /* Target types must match incl. qualifiers. */
1115 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1116 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1118 different_types_p)))
1121 if (different_types_p != NULL
1122 && (d1 == 0) != (d2 == 0))
1123 *different_types_p = true;
1124 /* Sizes must match unless one is missing or variable. */
1125 if (d1 == 0 || d2 == 0 || d1 == d2)
1128 d1_zero = !TYPE_MAX_VALUE (d1);
1129 d2_zero = !TYPE_MAX_VALUE (d2);
1131 d1_variable = (!d1_zero
1132 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1133 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1134 d2_variable = (!d2_zero
1135 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1136 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1137 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1138 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1140 if (different_types_p != NULL
1141 && d1_variable != d2_variable)
1142 *different_types_p = true;
1143 if (d1_variable || d2_variable)
1145 if (d1_zero && d2_zero)
1147 if (d1_zero || d2_zero
1148 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1149 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1158 if (val != 1 && !same_translation_unit_p (t1, t2))
1160 tree a1 = TYPE_ATTRIBUTES (t1);
1161 tree a2 = TYPE_ATTRIBUTES (t2);
1163 if (! attribute_list_contained (a1, a2)
1164 && ! attribute_list_contained (a2, a1))
1168 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1170 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1176 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1177 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1178 enum_and_int_p, different_types_p));
1184 return attrval == 2 && val == 1 ? 2 : val;
1187 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1188 their qualifiers, except for named address spaces. If the pointers point to
1189 different named addresses, then we must determine if one address space is a
1190 subset of the other. */
1193 comp_target_types (location_t location, tree ttl, tree ttr)
1196 tree mvl = TREE_TYPE (ttl);
1197 tree mvr = TREE_TYPE (ttr);
1198 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1199 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1200 addr_space_t as_common;
1201 bool enum_and_int_p;
1203 /* Fail if pointers point to incompatible address spaces. */
1204 if (!addr_space_superset (asl, asr, &as_common))
1207 /* Do not lose qualifiers on element types of array types that are
1208 pointer targets by taking their TYPE_MAIN_VARIANT. */
1209 if (TREE_CODE (mvl) != ARRAY_TYPE)
1210 mvl = TYPE_MAIN_VARIANT (mvl);
1211 if (TREE_CODE (mvr) != ARRAY_TYPE)
1212 mvr = TYPE_MAIN_VARIANT (mvr);
1213 enum_and_int_p = false;
1214 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1217 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1219 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1220 warning_at (location, OPT_Wc___compat,
1221 "pointer target types incompatible in C++");
1226 /* Subroutines of `comptypes'. */
1228 /* Determine whether two trees derive from the same translation unit.
1229 If the CONTEXT chain ends in a null, that tree's context is still
1230 being parsed, so if two trees have context chains ending in null,
1231 they're in the same translation unit. */
1233 same_translation_unit_p (const_tree t1, const_tree t2)
1235 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1236 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1238 case tcc_declaration:
1239 t1 = DECL_CONTEXT (t1); break;
1241 t1 = TYPE_CONTEXT (t1); break;
1242 case tcc_exceptional:
1243 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1244 default: gcc_unreachable ();
1247 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1248 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1250 case tcc_declaration:
1251 t2 = DECL_CONTEXT (t2); break;
1253 t2 = TYPE_CONTEXT (t2); break;
1254 case tcc_exceptional:
1255 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1256 default: gcc_unreachable ();
1262 /* Allocate the seen two types, assuming that they are compatible. */
1264 static struct tagged_tu_seen_cache *
1265 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1267 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1268 tu->next = tagged_tu_seen_base;
1272 tagged_tu_seen_base = tu;
1274 /* The C standard says that two structures in different translation
1275 units are compatible with each other only if the types of their
1276 fields are compatible (among other things). We assume that they
1277 are compatible until proven otherwise when building the cache.
1278 An example where this can occur is:
1283 If we are comparing this against a similar struct in another TU,
1284 and did not assume they were compatible, we end up with an infinite
1290 /* Free the seen types until we get to TU_TIL. */
1293 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1295 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1296 while (tu != tu_til)
1298 const struct tagged_tu_seen_cache *const tu1
1299 = (const struct tagged_tu_seen_cache *) tu;
1301 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1303 tagged_tu_seen_base = tu_til;
1306 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1307 compatible. If the two types are not the same (which has been
1308 checked earlier), this can only happen when multiple translation
1309 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1310 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1311 comptypes_internal. */
1314 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1315 bool *enum_and_int_p, bool *different_types_p)
1318 bool needs_warning = false;
1320 /* We have to verify that the tags of the types are the same. This
1321 is harder than it looks because this may be a typedef, so we have
1322 to go look at the original type. It may even be a typedef of a
1324 In the case of compiler-created builtin structs the TYPE_DECL
1325 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1326 while (TYPE_NAME (t1)
1327 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1328 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1329 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1331 while (TYPE_NAME (t2)
1332 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1333 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1334 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1336 /* C90 didn't have the requirement that the two tags be the same. */
1337 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1340 /* C90 didn't say what happened if one or both of the types were
1341 incomplete; we choose to follow C99 rules here, which is that they
1343 if (TYPE_SIZE (t1) == NULL
1344 || TYPE_SIZE (t2) == NULL)
1348 const struct tagged_tu_seen_cache * tts_i;
1349 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1350 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1354 switch (TREE_CODE (t1))
1358 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1359 /* Speed up the case where the type values are in the same order. */
1360 tree tv1 = TYPE_VALUES (t1);
1361 tree tv2 = TYPE_VALUES (t2);
1368 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1370 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1372 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1379 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1383 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1389 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1395 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1397 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1399 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1410 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1411 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1417 /* Speed up the common case where the fields are in the same order. */
1418 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1419 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1423 if (DECL_NAME (s1) != DECL_NAME (s2))
1425 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1426 enum_and_int_p, different_types_p);
1428 if (result != 1 && !DECL_NAME (s1))
1436 needs_warning = true;
1438 if (TREE_CODE (s1) == FIELD_DECL
1439 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1440 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1448 tu->val = needs_warning ? 2 : 1;
1452 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1456 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1457 if (DECL_NAME (s1) == DECL_NAME (s2))
1461 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1465 if (result != 1 && !DECL_NAME (s1))
1473 needs_warning = true;
1475 if (TREE_CODE (s1) == FIELD_DECL
1476 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1477 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1489 tu->val = needs_warning ? 2 : 10;
1495 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1497 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1499 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1502 if (TREE_CODE (s1) != TREE_CODE (s2)
1503 || DECL_NAME (s1) != DECL_NAME (s2))
1505 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1506 enum_and_int_p, different_types_p);
1510 needs_warning = true;
1512 if (TREE_CODE (s1) == FIELD_DECL
1513 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1514 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1520 tu->val = needs_warning ? 2 : 1;
1529 /* Return 1 if two function types F1 and F2 are compatible.
1530 If either type specifies no argument types,
1531 the other must specify a fixed number of self-promoting arg types.
1532 Otherwise, if one type specifies only the number of arguments,
1533 the other must specify that number of self-promoting arg types.
1534 Otherwise, the argument types must match.
1535 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1538 function_types_compatible_p (const_tree f1, const_tree f2,
1539 bool *enum_and_int_p, bool *different_types_p)
1542 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1547 ret1 = TREE_TYPE (f1);
1548 ret2 = TREE_TYPE (f2);
1550 /* 'volatile' qualifiers on a function's return type used to mean
1551 the function is noreturn. */
1552 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1553 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1554 if (TYPE_VOLATILE (ret1))
1555 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1556 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1557 if (TYPE_VOLATILE (ret2))
1558 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1559 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1560 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1564 args1 = TYPE_ARG_TYPES (f1);
1565 args2 = TYPE_ARG_TYPES (f2);
1567 if (different_types_p != NULL
1568 && (args1 == 0) != (args2 == 0))
1569 *different_types_p = true;
1571 /* An unspecified parmlist matches any specified parmlist
1572 whose argument types don't need default promotions. */
1576 if (!self_promoting_args_p (args2))
1578 /* If one of these types comes from a non-prototype fn definition,
1579 compare that with the other type's arglist.
1580 If they don't match, ask for a warning (but no error). */
1581 if (TYPE_ACTUAL_ARG_TYPES (f1)
1582 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1583 enum_and_int_p, different_types_p))
1589 if (!self_promoting_args_p (args1))
1591 if (TYPE_ACTUAL_ARG_TYPES (f2)
1592 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1593 enum_and_int_p, different_types_p))
1598 /* Both types have argument lists: compare them and propagate results. */
1599 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1601 return val1 != 1 ? val1 : val;
1604 /* Check two lists of types for compatibility, returning 0 for
1605 incompatible, 1 for compatible, or 2 for compatible with
1606 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1607 comptypes_internal. */
1610 type_lists_compatible_p (const_tree args1, const_tree args2,
1611 bool *enum_and_int_p, bool *different_types_p)
1613 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1619 tree a1, mv1, a2, mv2;
1620 if (args1 == 0 && args2 == 0)
1622 /* If one list is shorter than the other,
1623 they fail to match. */
1624 if (args1 == 0 || args2 == 0)
1626 mv1 = a1 = TREE_VALUE (args1);
1627 mv2 = a2 = TREE_VALUE (args2);
1628 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1629 mv1 = TYPE_MAIN_VARIANT (mv1);
1630 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1631 mv2 = TYPE_MAIN_VARIANT (mv2);
1632 /* A null pointer instead of a type
1633 means there is supposed to be an argument
1634 but nothing is specified about what type it has.
1635 So match anything that self-promotes. */
1636 if (different_types_p != NULL
1637 && (a1 == 0) != (a2 == 0))
1638 *different_types_p = true;
1641 if (c_type_promotes_to (a2) != a2)
1646 if (c_type_promotes_to (a1) != a1)
1649 /* If one of the lists has an error marker, ignore this arg. */
1650 else if (TREE_CODE (a1) == ERROR_MARK
1651 || TREE_CODE (a2) == ERROR_MARK)
1653 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1654 different_types_p)))
1656 if (different_types_p != NULL)
1657 *different_types_p = true;
1658 /* Allow wait (union {union wait *u; int *i} *)
1659 and wait (union wait *) to be compatible. */
1660 if (TREE_CODE (a1) == UNION_TYPE
1661 && (TYPE_NAME (a1) == 0
1662 || TYPE_TRANSPARENT_AGGR (a1))
1663 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1664 && tree_int_cst_equal (TYPE_SIZE (a1),
1668 for (memb = TYPE_FIELDS (a1);
1669 memb; memb = DECL_CHAIN (memb))
1671 tree mv3 = TREE_TYPE (memb);
1672 if (mv3 && mv3 != error_mark_node
1673 && TREE_CODE (mv3) != ARRAY_TYPE)
1674 mv3 = TYPE_MAIN_VARIANT (mv3);
1675 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1682 else if (TREE_CODE (a2) == UNION_TYPE
1683 && (TYPE_NAME (a2) == 0
1684 || TYPE_TRANSPARENT_AGGR (a2))
1685 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1686 && tree_int_cst_equal (TYPE_SIZE (a2),
1690 for (memb = TYPE_FIELDS (a2);
1691 memb; memb = DECL_CHAIN (memb))
1693 tree mv3 = TREE_TYPE (memb);
1694 if (mv3 && mv3 != error_mark_node
1695 && TREE_CODE (mv3) != ARRAY_TYPE)
1696 mv3 = TYPE_MAIN_VARIANT (mv3);
1697 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1708 /* comptypes said ok, but record if it said to warn. */
1712 args1 = TREE_CHAIN (args1);
1713 args2 = TREE_CHAIN (args2);
1717 /* Compute the size to increment a pointer by. */
1720 c_size_in_bytes (const_tree type)
1722 enum tree_code code = TREE_CODE (type);
1724 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1725 return size_one_node;
1727 if (!COMPLETE_OR_VOID_TYPE_P (type))
1729 error ("arithmetic on pointer to an incomplete type");
1730 return size_one_node;
1733 /* Convert in case a char is more than one unit. */
1734 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1735 size_int (TYPE_PRECISION (char_type_node)
1739 /* Return either DECL or its known constant value (if it has one). */
1742 decl_constant_value (tree decl)
1744 if (/* Don't change a variable array bound or initial value to a constant
1745 in a place where a variable is invalid. Note that DECL_INITIAL
1746 isn't valid for a PARM_DECL. */
1747 current_function_decl != 0
1748 && TREE_CODE (decl) != PARM_DECL
1749 && !TREE_THIS_VOLATILE (decl)
1750 && TREE_READONLY (decl)
1751 && DECL_INITIAL (decl) != 0
1752 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1753 /* This is invalid if initial value is not constant.
1754 If it has either a function call, a memory reference,
1755 or a variable, then re-evaluating it could give different results. */
1756 && TREE_CONSTANT (DECL_INITIAL (decl))
1757 /* Check for cases where this is sub-optimal, even though valid. */
1758 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1759 return DECL_INITIAL (decl);
1763 /* Convert the array expression EXP to a pointer. */
1765 array_to_pointer_conversion (location_t loc, tree exp)
1767 tree orig_exp = exp;
1768 tree type = TREE_TYPE (exp);
1770 tree restype = TREE_TYPE (type);
1773 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1775 STRIP_TYPE_NOPS (exp);
1777 if (TREE_NO_WARNING (orig_exp))
1778 TREE_NO_WARNING (exp) = 1;
1780 ptrtype = build_pointer_type (restype);
1782 if (TREE_CODE (exp) == INDIRECT_REF)
1783 return convert (ptrtype, TREE_OPERAND (exp, 0));
1785 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1786 return convert (ptrtype, adr);
1789 /* Convert the function expression EXP to a pointer. */
1791 function_to_pointer_conversion (location_t loc, tree exp)
1793 tree orig_exp = exp;
1795 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1797 STRIP_TYPE_NOPS (exp);
1799 if (TREE_NO_WARNING (orig_exp))
1800 TREE_NO_WARNING (exp) = 1;
1802 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1805 /* Mark EXP as read, not just set, for set but not used -Wunused
1806 warning purposes. */
1809 mark_exp_read (tree exp)
1811 switch (TREE_CODE (exp))
1815 DECL_READ_P (exp) = 1;
1824 mark_exp_read (TREE_OPERAND (exp, 0));
1827 case C_MAYBE_CONST_EXPR:
1828 mark_exp_read (TREE_OPERAND (exp, 1));
1835 /* Perform the default conversion of arrays and functions to pointers.
1836 Return the result of converting EXP. For any other expression, just
1839 LOC is the location of the expression. */
1842 default_function_array_conversion (location_t loc, struct c_expr exp)
1844 tree orig_exp = exp.value;
1845 tree type = TREE_TYPE (exp.value);
1846 enum tree_code code = TREE_CODE (type);
1852 bool not_lvalue = false;
1853 bool lvalue_array_p;
1855 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1856 || CONVERT_EXPR_P (exp.value))
1857 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1859 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1861 exp.value = TREE_OPERAND (exp.value, 0);
1864 if (TREE_NO_WARNING (orig_exp))
1865 TREE_NO_WARNING (exp.value) = 1;
1867 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1868 if (!flag_isoc99 && !lvalue_array_p)
1870 /* Before C99, non-lvalue arrays do not decay to pointers.
1871 Normally, using such an array would be invalid; but it can
1872 be used correctly inside sizeof or as a statement expression.
1873 Thus, do not give an error here; an error will result later. */
1877 exp.value = array_to_pointer_conversion (loc, exp.value);
1881 exp.value = function_to_pointer_conversion (loc, exp.value);
1891 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1893 mark_exp_read (exp.value);
1894 return default_function_array_conversion (loc, exp);
1897 /* EXP is an expression of integer type. Apply the integer promotions
1898 to it and return the promoted value. */
1901 perform_integral_promotions (tree exp)
1903 tree type = TREE_TYPE (exp);
1904 enum tree_code code = TREE_CODE (type);
1906 gcc_assert (INTEGRAL_TYPE_P (type));
1908 /* Normally convert enums to int,
1909 but convert wide enums to something wider. */
1910 if (code == ENUMERAL_TYPE)
1912 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1913 TYPE_PRECISION (integer_type_node)),
1914 ((TYPE_PRECISION (type)
1915 >= TYPE_PRECISION (integer_type_node))
1916 && TYPE_UNSIGNED (type)));
1918 return convert (type, exp);
1921 /* ??? This should no longer be needed now bit-fields have their
1923 if (TREE_CODE (exp) == COMPONENT_REF
1924 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1925 /* If it's thinner than an int, promote it like a
1926 c_promoting_integer_type_p, otherwise leave it alone. */
1927 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1928 TYPE_PRECISION (integer_type_node)))
1929 return convert (integer_type_node, exp);
1931 if (c_promoting_integer_type_p (type))
1933 /* Preserve unsignedness if not really getting any wider. */
1934 if (TYPE_UNSIGNED (type)
1935 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1936 return convert (unsigned_type_node, exp);
1938 return convert (integer_type_node, exp);
1945 /* Perform default promotions for C data used in expressions.
1946 Enumeral types or short or char are converted to int.
1947 In addition, manifest constants symbols are replaced by their values. */
1950 default_conversion (tree exp)
1953 tree type = TREE_TYPE (exp);
1954 enum tree_code code = TREE_CODE (type);
1957 mark_exp_read (exp);
1959 /* Functions and arrays have been converted during parsing. */
1960 gcc_assert (code != FUNCTION_TYPE);
1961 if (code == ARRAY_TYPE)
1964 /* Constants can be used directly unless they're not loadable. */
1965 if (TREE_CODE (exp) == CONST_DECL)
1966 exp = DECL_INITIAL (exp);
1968 /* Strip no-op conversions. */
1970 STRIP_TYPE_NOPS (exp);
1972 if (TREE_NO_WARNING (orig_exp))
1973 TREE_NO_WARNING (exp) = 1;
1975 if (code == VOID_TYPE)
1977 error ("void value not ignored as it ought to be");
1978 return error_mark_node;
1981 exp = require_complete_type (exp);
1982 if (exp == error_mark_node)
1983 return error_mark_node;
1985 promoted_type = targetm.promoted_type (type);
1987 return convert (promoted_type, exp);
1989 if (INTEGRAL_TYPE_P (type))
1990 return perform_integral_promotions (exp);
1995 /* Look up COMPONENT in a structure or union TYPE.
1997 If the component name is not found, returns NULL_TREE. Otherwise,
1998 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1999 stepping down the chain to the component, which is in the last
2000 TREE_VALUE of the list. Normally the list is of length one, but if
2001 the component is embedded within (nested) anonymous structures or
2002 unions, the list steps down the chain to the component. */
2005 lookup_field (tree type, tree component)
2009 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2010 to the field elements. Use a binary search on this array to quickly
2011 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2012 will always be set for structures which have many elements. */
2014 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2017 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2019 field = TYPE_FIELDS (type);
2021 top = TYPE_LANG_SPECIFIC (type)->s->len;
2022 while (top - bot > 1)
2024 half = (top - bot + 1) >> 1;
2025 field = field_array[bot+half];
2027 if (DECL_NAME (field) == NULL_TREE)
2029 /* Step through all anon unions in linear fashion. */
2030 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2032 field = field_array[bot++];
2033 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2034 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2036 tree anon = lookup_field (TREE_TYPE (field), component);
2039 return tree_cons (NULL_TREE, field, anon);
2041 /* The Plan 9 compiler permits referring
2042 directly to an anonymous struct/union field
2043 using a typedef name. */
2044 if (flag_plan9_extensions
2045 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2046 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2048 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2054 /* Entire record is only anon unions. */
2058 /* Restart the binary search, with new lower bound. */
2062 if (DECL_NAME (field) == component)
2064 if (DECL_NAME (field) < component)
2070 if (DECL_NAME (field_array[bot]) == component)
2071 field = field_array[bot];
2072 else if (DECL_NAME (field) != component)
2077 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2079 if (DECL_NAME (field) == NULL_TREE
2080 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2081 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2083 tree anon = lookup_field (TREE_TYPE (field), component);
2086 return tree_cons (NULL_TREE, field, anon);
2088 /* The Plan 9 compiler permits referring directly to an
2089 anonymous struct/union field using a typedef
2091 if (flag_plan9_extensions
2092 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2093 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2094 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2099 if (DECL_NAME (field) == component)
2103 if (field == NULL_TREE)
2107 return tree_cons (NULL_TREE, field, NULL_TREE);
2110 /* Make an expression to refer to the COMPONENT field of structure or
2111 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2112 location of the COMPONENT_REF. */
2115 build_component_ref (location_t loc, tree datum, tree component)
2117 tree type = TREE_TYPE (datum);
2118 enum tree_code code = TREE_CODE (type);
2121 bool datum_lvalue = lvalue_p (datum);
2123 if (!objc_is_public (datum, component))
2124 return error_mark_node;
2126 /* Detect Objective-C property syntax object.property. */
2127 if (c_dialect_objc ()
2128 && (ref = objc_maybe_build_component_ref (datum, component)))
2131 /* See if there is a field or component with name COMPONENT. */
2133 if (code == RECORD_TYPE || code == UNION_TYPE)
2135 if (!COMPLETE_TYPE_P (type))
2137 c_incomplete_type_error (NULL_TREE, type);
2138 return error_mark_node;
2141 field = lookup_field (type, component);
2145 error_at (loc, "%qT has no member named %qE", type, component);
2146 return error_mark_node;
2149 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2150 This might be better solved in future the way the C++ front
2151 end does it - by giving the anonymous entities each a
2152 separate name and type, and then have build_component_ref
2153 recursively call itself. We can't do that here. */
2156 tree subdatum = TREE_VALUE (field);
2159 bool use_datum_quals;
2161 if (TREE_TYPE (subdatum) == error_mark_node)
2162 return error_mark_node;
2164 /* If this is an rvalue, it does not have qualifiers in C
2165 standard terms and we must avoid propagating such
2166 qualifiers down to a non-lvalue array that is then
2167 converted to a pointer. */
2168 use_datum_quals = (datum_lvalue
2169 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2171 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2172 if (use_datum_quals)
2173 quals |= TYPE_QUALS (TREE_TYPE (datum));
2174 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2176 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2178 SET_EXPR_LOCATION (ref, loc);
2179 if (TREE_READONLY (subdatum)
2180 || (use_datum_quals && TREE_READONLY (datum)))
2181 TREE_READONLY (ref) = 1;
2182 if (TREE_THIS_VOLATILE (subdatum)
2183 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2184 TREE_THIS_VOLATILE (ref) = 1;
2186 if (TREE_DEPRECATED (subdatum))
2187 warn_deprecated_use (subdatum, NULL_TREE);
2191 field = TREE_CHAIN (field);
2197 else if (code != ERROR_MARK)
2199 "request for member %qE in something not a structure or union",
2202 return error_mark_node;
2205 /* Given an expression PTR for a pointer, return an expression
2206 for the value pointed to.
2207 ERRORSTRING is the name of the operator to appear in error messages.
2209 LOC is the location to use for the generated tree. */
2212 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2214 tree pointer = default_conversion (ptr);
2215 tree type = TREE_TYPE (pointer);
2218 if (TREE_CODE (type) == POINTER_TYPE)
2220 if (CONVERT_EXPR_P (pointer)
2221 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2223 /* If a warning is issued, mark it to avoid duplicates from
2224 the backend. This only needs to be done at
2225 warn_strict_aliasing > 2. */
2226 if (warn_strict_aliasing > 2)
2227 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2228 type, TREE_OPERAND (pointer, 0)))
2229 TREE_NO_WARNING (pointer) = 1;
2232 if (TREE_CODE (pointer) == ADDR_EXPR
2233 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2234 == TREE_TYPE (type)))
2236 ref = TREE_OPERAND (pointer, 0);
2237 protected_set_expr_location (ref, loc);
2242 tree t = TREE_TYPE (type);
2244 ref = build1 (INDIRECT_REF, t, pointer);
2246 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2248 error_at (loc, "dereferencing pointer to incomplete type");
2249 return error_mark_node;
2251 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2252 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2254 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2255 so that we get the proper error message if the result is used
2256 to assign to. Also, &* is supposed to be a no-op.
2257 And ANSI C seems to specify that the type of the result
2258 should be the const type. */
2259 /* A de-reference of a pointer to const is not a const. It is valid
2260 to change it via some other pointer. */
2261 TREE_READONLY (ref) = TYPE_READONLY (t);
2262 TREE_SIDE_EFFECTS (ref)
2263 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2264 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2265 protected_set_expr_location (ref, loc);
2269 else if (TREE_CODE (pointer) != ERROR_MARK)
2272 case RO_ARRAY_INDEXING:
2274 "invalid type argument of array indexing (have %qT)",
2279 "invalid type argument of unary %<*%> (have %qT)",
2284 "invalid type argument of %<->%> (have %qT)",
2290 return error_mark_node;
2293 /* This handles expressions of the form "a[i]", which denotes
2296 This is logically equivalent in C to *(a+i), but we may do it differently.
2297 If A is a variable or a member, we generate a primitive ARRAY_REF.
2298 This avoids forcing the array out of registers, and can work on
2299 arrays that are not lvalues (for example, members of structures returned
2302 For vector types, allow vector[i] but not i[vector], and create
2303 *(((type*)&vectortype) + i) for the expression.
2305 LOC is the location to use for the returned expression. */
2308 build_array_ref (location_t loc, tree array, tree index)
2311 bool swapped = false;
2312 if (TREE_TYPE (array) == error_mark_node
2313 || TREE_TYPE (index) == error_mark_node)
2314 return error_mark_node;
2316 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2317 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2318 /* Allow vector[index] but not index[vector]. */
2319 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2322 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2323 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2326 "subscripted value is neither array nor pointer nor vector");
2328 return error_mark_node;
2336 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2338 error_at (loc, "array subscript is not an integer");
2339 return error_mark_node;
2342 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2344 error_at (loc, "subscripted value is pointer to function");
2345 return error_mark_node;
2348 /* ??? Existing practice has been to warn only when the char
2349 index is syntactically the index, not for char[array]. */
2351 warn_array_subscript_with_type_char (index);
2353 /* Apply default promotions *after* noticing character types. */
2354 index = default_conversion (index);
2356 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2358 /* For vector[index], convert the vector to a
2359 pointer of the underlying type. */
2360 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2362 tree type = TREE_TYPE (array);
2365 if (TREE_CODE (index) == INTEGER_CST)
2366 if (!host_integerp (index, 1)
2367 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2368 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2369 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2371 c_common_mark_addressable_vec (array);
2372 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2373 type = build_pointer_type (type);
2374 type1 = build_pointer_type (TREE_TYPE (array));
2375 array = build1 (ADDR_EXPR, type1, array);
2376 array = convert (type, array);
2379 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2383 /* An array that is indexed by a non-constant
2384 cannot be stored in a register; we must be able to do
2385 address arithmetic on its address.
2386 Likewise an array of elements of variable size. */
2387 if (TREE_CODE (index) != INTEGER_CST
2388 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2389 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2391 if (!c_mark_addressable (array))
2392 return error_mark_node;
2394 /* An array that is indexed by a constant value which is not within
2395 the array bounds cannot be stored in a register either; because we
2396 would get a crash in store_bit_field/extract_bit_field when trying
2397 to access a non-existent part of the register. */
2398 if (TREE_CODE (index) == INTEGER_CST
2399 && TYPE_DOMAIN (TREE_TYPE (array))
2400 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2402 if (!c_mark_addressable (array))
2403 return error_mark_node;
2409 while (TREE_CODE (foo) == COMPONENT_REF)
2410 foo = TREE_OPERAND (foo, 0);
2411 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2412 pedwarn (loc, OPT_pedantic,
2413 "ISO C forbids subscripting %<register%> array");
2414 else if (!flag_isoc99 && !lvalue_p (foo))
2415 pedwarn (loc, OPT_pedantic,
2416 "ISO C90 forbids subscripting non-lvalue array");
2419 type = TREE_TYPE (TREE_TYPE (array));
2420 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2421 /* Array ref is const/volatile if the array elements are
2422 or if the array is. */
2423 TREE_READONLY (rval)
2424 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2425 | TREE_READONLY (array));
2426 TREE_SIDE_EFFECTS (rval)
2427 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2428 | TREE_SIDE_EFFECTS (array));
2429 TREE_THIS_VOLATILE (rval)
2430 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2431 /* This was added by rms on 16 Nov 91.
2432 It fixes vol struct foo *a; a->elts[1]
2433 in an inline function.
2434 Hope it doesn't break something else. */
2435 | TREE_THIS_VOLATILE (array));
2436 ret = require_complete_type (rval);
2437 protected_set_expr_location (ret, loc);
2442 tree ar = default_conversion (array);
2444 if (ar == error_mark_node)
2447 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2448 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2450 return build_indirect_ref
2451 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2456 /* Build an external reference to identifier ID. FUN indicates
2457 whether this will be used for a function call. LOC is the source
2458 location of the identifier. This sets *TYPE to the type of the
2459 identifier, which is not the same as the type of the returned value
2460 for CONST_DECLs defined as enum constants. If the type of the
2461 identifier is not available, *TYPE is set to NULL. */
2463 build_external_ref (location_t loc, tree id, int fun, tree *type)
2466 tree decl = lookup_name (id);
2468 /* In Objective-C, an instance variable (ivar) may be preferred to
2469 whatever lookup_name() found. */
2470 decl = objc_lookup_ivar (decl, id);
2473 if (decl && decl != error_mark_node)
2476 *type = TREE_TYPE (ref);
2479 /* Implicit function declaration. */
2480 ref = implicitly_declare (loc, id);
2481 else if (decl == error_mark_node)
2482 /* Don't complain about something that's already been
2483 complained about. */
2484 return error_mark_node;
2487 undeclared_variable (loc, id);
2488 return error_mark_node;
2491 if (TREE_TYPE (ref) == error_mark_node)
2492 return error_mark_node;
2494 if (TREE_DEPRECATED (ref))
2495 warn_deprecated_use (ref, NULL_TREE);
2497 /* Recursive call does not count as usage. */
2498 if (ref != current_function_decl)
2500 TREE_USED (ref) = 1;
2503 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2505 if (!in_sizeof && !in_typeof)
2506 C_DECL_USED (ref) = 1;
2507 else if (DECL_INITIAL (ref) == 0
2508 && DECL_EXTERNAL (ref)
2509 && !TREE_PUBLIC (ref))
2510 record_maybe_used_decl (ref);
2513 if (TREE_CODE (ref) == CONST_DECL)
2515 used_types_insert (TREE_TYPE (ref));
2518 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2519 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2521 warning_at (loc, OPT_Wc___compat,
2522 ("enum constant defined in struct or union "
2523 "is not visible in C++"));
2524 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2527 ref = DECL_INITIAL (ref);
2528 TREE_CONSTANT (ref) = 1;
2530 else if (current_function_decl != 0
2531 && !DECL_FILE_SCOPE_P (current_function_decl)
2532 && (TREE_CODE (ref) == VAR_DECL
2533 || TREE_CODE (ref) == PARM_DECL
2534 || TREE_CODE (ref) == FUNCTION_DECL))
2536 tree context = decl_function_context (ref);
2538 if (context != 0 && context != current_function_decl)
2539 DECL_NONLOCAL (ref) = 1;
2541 /* C99 6.7.4p3: An inline definition of a function with external
2542 linkage ... shall not contain a reference to an identifier with
2543 internal linkage. */
2544 else if (current_function_decl != 0
2545 && DECL_DECLARED_INLINE_P (current_function_decl)
2546 && DECL_EXTERNAL (current_function_decl)
2547 && VAR_OR_FUNCTION_DECL_P (ref)
2548 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2549 && ! TREE_PUBLIC (ref)
2550 && DECL_CONTEXT (ref) != current_function_decl)
2551 record_inline_static (loc, current_function_decl, ref,
2557 /* Record details of decls possibly used inside sizeof or typeof. */
2558 struct maybe_used_decl
2562 /* The level seen at (in_sizeof + in_typeof). */
2564 /* The next one at this level or above, or NULL. */
2565 struct maybe_used_decl *next;
2568 static struct maybe_used_decl *maybe_used_decls;
2570 /* Record that DECL, an undefined static function reference seen
2571 inside sizeof or typeof, might be used if the operand of sizeof is
2572 a VLA type or the operand of typeof is a variably modified
2576 record_maybe_used_decl (tree decl)
2578 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2580 t->level = in_sizeof + in_typeof;
2581 t->next = maybe_used_decls;
2582 maybe_used_decls = t;
2585 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2586 USED is false, just discard them. If it is true, mark them used
2587 (if no longer inside sizeof or typeof) or move them to the next
2588 level up (if still inside sizeof or typeof). */
2591 pop_maybe_used (bool used)
2593 struct maybe_used_decl *p = maybe_used_decls;
2594 int cur_level = in_sizeof + in_typeof;
2595 while (p && p->level > cur_level)
2600 C_DECL_USED (p->decl) = 1;
2602 p->level = cur_level;
2606 if (!used || cur_level == 0)
2607 maybe_used_decls = p;
2610 /* Return the result of sizeof applied to EXPR. */
2613 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2616 if (expr.value == error_mark_node)
2618 ret.value = error_mark_node;
2619 ret.original_code = ERROR_MARK;
2620 ret.original_type = NULL;
2621 pop_maybe_used (false);
2625 bool expr_const_operands = true;
2626 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2627 &expr_const_operands);
2628 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2629 ret.original_code = ERROR_MARK;
2630 ret.original_type = NULL;
2631 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2633 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2634 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2635 folded_expr, ret.value);
2636 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2637 SET_EXPR_LOCATION (ret.value, loc);
2639 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2644 /* Return the result of sizeof applied to T, a structure for the type
2645 name passed to sizeof (rather than the type itself). LOC is the
2646 location of the original expression. */
2649 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2653 tree type_expr = NULL_TREE;
2654 bool type_expr_const = true;
2655 type = groktypename (t, &type_expr, &type_expr_const);
2656 ret.value = c_sizeof (loc, type);
2657 ret.original_code = ERROR_MARK;
2658 ret.original_type = NULL;
2659 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2660 && c_vla_type_p (type))
2662 /* If the type is a [*] array, it is a VLA but is represented as
2663 having a size of zero. In such a case we must ensure that
2664 the result of sizeof does not get folded to a constant by
2665 c_fully_fold, because if the size is evaluated the result is
2666 not constant and so constraints on zero or negative size
2667 arrays must not be applied when this sizeof call is inside
2668 another array declarator. */
2670 type_expr = integer_zero_node;
2671 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2672 type_expr, ret.value);
2673 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2675 pop_maybe_used (type != error_mark_node
2676 ? C_TYPE_VARIABLE_SIZE (type) : false);
2680 /* Build a function call to function FUNCTION with parameters PARAMS.
2681 The function call is at LOC.
2682 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2683 TREE_VALUE of each node is a parameter-expression.
2684 FUNCTION's data type may be a function type or a pointer-to-function. */
2687 build_function_call (location_t loc, tree function, tree params)
2692 vec = VEC_alloc (tree, gc, list_length (params));
2693 for (; params; params = TREE_CHAIN (params))
2694 VEC_quick_push (tree, vec, TREE_VALUE (params));
2695 ret = build_function_call_vec (loc, function, vec, NULL);
2696 VEC_free (tree, gc, vec);
2700 /* Build a function call to function FUNCTION with parameters PARAMS.
2701 ORIGTYPES, if not NULL, is a vector of types; each element is
2702 either NULL or the original type of the corresponding element in
2703 PARAMS. The original type may differ from TREE_TYPE of the
2704 parameter for enums. FUNCTION's data type may be a function type
2705 or pointer-to-function. This function changes the elements of
2709 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2710 VEC(tree,gc) *origtypes)
2712 tree fntype, fundecl = 0;
2713 tree name = NULL_TREE, result;
2719 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2720 STRIP_TYPE_NOPS (function);
2722 /* Convert anything with function type to a pointer-to-function. */
2723 if (TREE_CODE (function) == FUNCTION_DECL)
2725 /* Implement type-directed function overloading for builtins.
2726 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2727 handle all the type checking. The result is a complete expression
2728 that implements this function call. */
2729 tem = resolve_overloaded_builtin (loc, function, params);
2733 name = DECL_NAME (function);
2736 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2737 function = function_to_pointer_conversion (loc, function);
2739 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2740 expressions, like those used for ObjC messenger dispatches. */
2741 if (!VEC_empty (tree, params))
2742 function = objc_rewrite_function_call (function,
2743 VEC_index (tree, params, 0));
2745 function = c_fully_fold (function, false, NULL);
2747 fntype = TREE_TYPE (function);
2749 if (TREE_CODE (fntype) == ERROR_MARK)
2750 return error_mark_node;
2752 if (!(TREE_CODE (fntype) == POINTER_TYPE
2753 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2755 error_at (loc, "called object %qE is not a function", function);
2756 return error_mark_node;
2759 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2760 current_function_returns_abnormally = 1;
2762 /* fntype now gets the type of function pointed to. */
2763 fntype = TREE_TYPE (fntype);
2765 /* Convert the parameters to the types declared in the
2766 function prototype, or apply default promotions. */
2768 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2771 return error_mark_node;
2773 /* Check that the function is called through a compatible prototype.
2774 If it is not, replace the call by a trap, wrapped up in a compound
2775 expression if necessary. This has the nice side-effect to prevent
2776 the tree-inliner from generating invalid assignment trees which may
2777 blow up in the RTL expander later. */
2778 if (CONVERT_EXPR_P (function)
2779 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2780 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2781 && !comptypes (fntype, TREE_TYPE (tem)))
2783 tree return_type = TREE_TYPE (fntype);
2784 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2788 /* This situation leads to run-time undefined behavior. We can't,
2789 therefore, simply error unless we can prove that all possible
2790 executions of the program must execute the code. */
2791 if (warning_at (loc, 0, "function called through a non-compatible type"))
2792 /* We can, however, treat "undefined" any way we please.
2793 Call abort to encourage the user to fix the program. */
2794 inform (loc, "if this code is reached, the program will abort");
2795 /* Before the abort, allow the function arguments to exit or
2797 for (i = 0; i < nargs; i++)
2798 trap = build2 (COMPOUND_EXPR, void_type_node,
2799 VEC_index (tree, params, i), trap);
2801 if (VOID_TYPE_P (return_type))
2803 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2805 "function with qualified void return type called");
2812 if (AGGREGATE_TYPE_P (return_type))
2813 rhs = build_compound_literal (loc, return_type,
2814 build_constructor (return_type, 0),
2817 rhs = build_zero_cst (return_type);
2819 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2824 argarray = VEC_address (tree, params);
2826 /* Check that arguments to builtin functions match the expectations. */
2828 && DECL_BUILT_IN (fundecl)
2829 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2830 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2831 return error_mark_node;
2833 /* Check that the arguments to the function are valid. */
2834 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2835 TYPE_ARG_TYPES (fntype));
2837 if (name != NULL_TREE
2838 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2840 if (require_constant_value)
2842 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2843 function, nargs, argarray);
2845 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2846 function, nargs, argarray);
2847 if (TREE_CODE (result) == NOP_EXPR
2848 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2849 STRIP_TYPE_NOPS (result);
2852 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2853 function, nargs, argarray);
2855 if (VOID_TYPE_P (TREE_TYPE (result)))
2857 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2859 "function with qualified void return type called");
2862 return require_complete_type (result);
2865 /* Convert the argument expressions in the vector VALUES
2866 to the types in the list TYPELIST.
2868 If TYPELIST is exhausted, or when an element has NULL as its type,
2869 perform the default conversions.
2871 ORIGTYPES is the original types of the expressions in VALUES. This
2872 holds the type of enum values which have been converted to integral
2873 types. It may be NULL.
2875 FUNCTION is a tree for the called function. It is used only for
2876 error messages, where it is formatted with %qE.
2878 This is also where warnings about wrong number of args are generated.
2880 Returns the actual number of arguments processed (which may be less
2881 than the length of VALUES in some error situations), or -1 on
2885 convert_arguments (tree typelist, VEC(tree,gc) *values,
2886 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2889 unsigned int parmnum;
2890 bool error_args = false;
2891 const bool type_generic = fundecl
2892 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2893 bool type_generic_remove_excess_precision = false;
2896 /* Change pointer to function to the function itself for
2898 if (TREE_CODE (function) == ADDR_EXPR
2899 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2900 function = TREE_OPERAND (function, 0);
2902 /* Handle an ObjC selector specially for diagnostics. */
2903 selector = objc_message_selector ();
2905 /* For type-generic built-in functions, determine whether excess
2906 precision should be removed (classification) or not
2909 && DECL_BUILT_IN (fundecl)
2910 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2912 switch (DECL_FUNCTION_CODE (fundecl))
2914 case BUILT_IN_ISFINITE:
2915 case BUILT_IN_ISINF:
2916 case BUILT_IN_ISINF_SIGN:
2917 case BUILT_IN_ISNAN:
2918 case BUILT_IN_ISNORMAL:
2919 case BUILT_IN_FPCLASSIFY:
2920 type_generic_remove_excess_precision = true;
2924 type_generic_remove_excess_precision = false;
2929 /* Scan the given expressions and types, producing individual
2930 converted arguments. */
2932 for (typetail = typelist, parmnum = 0;
2933 VEC_iterate (tree, values, parmnum, val);
2936 tree type = typetail ? TREE_VALUE (typetail) : 0;
2937 tree valtype = TREE_TYPE (val);
2938 tree rname = function;
2939 int argnum = parmnum + 1;
2940 const char *invalid_func_diag;
2941 bool excess_precision = false;
2945 if (type == void_type_node)
2948 error_at (input_location,
2949 "too many arguments to method %qE", selector);
2951 error_at (input_location,
2952 "too many arguments to function %qE", function);
2954 if (fundecl && !DECL_BUILT_IN (fundecl))
2955 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2959 if (selector && argnum > 2)
2965 npc = null_pointer_constant_p (val);
2967 /* If there is excess precision and a prototype, convert once to
2968 the required type rather than converting via the semantic
2969 type. Likewise without a prototype a float value represented
2970 as long double should be converted once to double. But for
2971 type-generic classification functions excess precision must
2973 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2974 && (type || !type_generic || !type_generic_remove_excess_precision))
2976 val = TREE_OPERAND (val, 0);
2977 excess_precision = true;
2979 val = c_fully_fold (val, false, NULL);
2980 STRIP_TYPE_NOPS (val);
2982 val = require_complete_type (val);
2986 /* Formal parm type is specified by a function prototype. */
2988 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2990 error ("type of formal parameter %d is incomplete", parmnum + 1);
2997 /* Optionally warn about conversions that
2998 differ from the default conversions. */
2999 if (warn_traditional_conversion || warn_traditional)
3001 unsigned int formal_prec = TYPE_PRECISION (type);
3003 if (INTEGRAL_TYPE_P (type)
3004 && TREE_CODE (valtype) == REAL_TYPE)
3005 warning (0, "passing argument %d of %qE as integer "
3006 "rather than floating due to prototype",
3008 if (INTEGRAL_TYPE_P (type)
3009 && TREE_CODE (valtype) == COMPLEX_TYPE)
3010 warning (0, "passing argument %d of %qE as integer "
3011 "rather than complex due to prototype",
3013 else if (TREE_CODE (type) == COMPLEX_TYPE
3014 && TREE_CODE (valtype) == REAL_TYPE)
3015 warning (0, "passing argument %d of %qE as complex "
3016 "rather than floating due to prototype",
3018 else if (TREE_CODE (type) == REAL_TYPE
3019 && INTEGRAL_TYPE_P (valtype))
3020 warning (0, "passing argument %d of %qE as floating "
3021 "rather than integer due to prototype",
3023 else if (TREE_CODE (type) == COMPLEX_TYPE
3024 && INTEGRAL_TYPE_P (valtype))
3025 warning (0, "passing argument %d of %qE as complex "
3026 "rather than integer due to prototype",
3028 else if (TREE_CODE (type) == REAL_TYPE
3029 && TREE_CODE (valtype) == COMPLEX_TYPE)
3030 warning (0, "passing argument %d of %qE as floating "
3031 "rather than complex due to prototype",
3033 /* ??? At some point, messages should be written about
3034 conversions between complex types, but that's too messy
3036 else if (TREE_CODE (type) == REAL_TYPE
3037 && TREE_CODE (valtype) == REAL_TYPE)
3039 /* Warn if any argument is passed as `float',
3040 since without a prototype it would be `double'. */
3041 if (formal_prec == TYPE_PRECISION (float_type_node)
3042 && type != dfloat32_type_node)
3043 warning (0, "passing argument %d of %qE as %<float%> "
3044 "rather than %<double%> due to prototype",
3047 /* Warn if mismatch between argument and prototype
3048 for decimal float types. Warn of conversions with
3049 binary float types and of precision narrowing due to
3051 else if (type != valtype
3052 && (type == dfloat32_type_node
3053 || type == dfloat64_type_node
3054 || type == dfloat128_type_node
3055 || valtype == dfloat32_type_node
3056 || valtype == dfloat64_type_node
3057 || valtype == dfloat128_type_node)
3059 <= TYPE_PRECISION (valtype)
3060 || (type == dfloat128_type_node
3062 != dfloat64_type_node
3064 != dfloat32_type_node)))
3065 || (type == dfloat64_type_node
3067 != dfloat32_type_node))))
3068 warning (0, "passing argument %d of %qE as %qT "
3069 "rather than %qT due to prototype",
3070 argnum, rname, type, valtype);
3073 /* Detect integer changing in width or signedness.
3074 These warnings are only activated with
3075 -Wtraditional-conversion, not with -Wtraditional. */
3076 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3077 && INTEGRAL_TYPE_P (valtype))
3079 tree would_have_been = default_conversion (val);
3080 tree type1 = TREE_TYPE (would_have_been);
3082 if (TREE_CODE (type) == ENUMERAL_TYPE
3083 && (TYPE_MAIN_VARIANT (type)
3084 == TYPE_MAIN_VARIANT (valtype)))
3085 /* No warning if function asks for enum
3086 and the actual arg is that enum type. */
3088 else if (formal_prec != TYPE_PRECISION (type1))
3089 warning (OPT_Wtraditional_conversion,
3090 "passing argument %d of %qE "
3091 "with different width due to prototype",
3093 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3095 /* Don't complain if the formal parameter type
3096 is an enum, because we can't tell now whether
3097 the value was an enum--even the same enum. */
3098 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3100 else if (TREE_CODE (val) == INTEGER_CST
3101 && int_fits_type_p (val, type))
3102 /* Change in signedness doesn't matter
3103 if a constant value is unaffected. */
3105 /* If the value is extended from a narrower
3106 unsigned type, it doesn't matter whether we
3107 pass it as signed or unsigned; the value
3108 certainly is the same either way. */
3109 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3110 && TYPE_UNSIGNED (valtype))
3112 else if (TYPE_UNSIGNED (type))
3113 warning (OPT_Wtraditional_conversion,
3114 "passing argument %d of %qE "
3115 "as unsigned due to prototype",
3118 warning (OPT_Wtraditional_conversion,
3119 "passing argument %d of %qE "
3120 "as signed due to prototype", argnum, rname);
3124 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3125 sake of better warnings from convert_and_check. */
3126 if (excess_precision)
3127 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3128 origtype = (origtypes == NULL
3130 : VEC_index (tree, origtypes, parmnum));
3131 parmval = convert_for_assignment (input_location, type, val,
3132 origtype, ic_argpass, npc,
3136 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3137 && INTEGRAL_TYPE_P (type)
3138 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3139 parmval = default_conversion (parmval);
3142 else if (TREE_CODE (valtype) == REAL_TYPE
3143 && (TYPE_PRECISION (valtype)
3144 < TYPE_PRECISION (double_type_node))
3145 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3151 /* Convert `float' to `double'. */
3152 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3153 warning (OPT_Wdouble_promotion,
3154 "implicit conversion from %qT to %qT when passing "
3155 "argument to function",
3156 valtype, double_type_node);
3157 parmval = convert (double_type_node, val);
3160 else if (excess_precision && !type_generic)
3161 /* A "double" argument with excess precision being passed
3162 without a prototype or in variable arguments. */
3163 parmval = convert (valtype, val);
3164 else if ((invalid_func_diag =
3165 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3167 error (invalid_func_diag);
3171 /* Convert `short' and `char' to full-size `int'. */
3172 parmval = default_conversion (val);
3174 VEC_replace (tree, values, parmnum, parmval);
3175 if (parmval == error_mark_node)
3179 typetail = TREE_CHAIN (typetail);
3182 gcc_assert (parmnum == VEC_length (tree, values));
3184 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3186 error_at (input_location,
3187 "too few arguments to function %qE", function);
3188 if (fundecl && !DECL_BUILT_IN (fundecl))
3189 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3193 return error_args ? -1 : (int) parmnum;
3196 /* This is the entry point used by the parser to build unary operators
3197 in the input. CODE, a tree_code, specifies the unary operator, and
3198 ARG is the operand. For unary plus, the C parser currently uses
3199 CONVERT_EXPR for code.
3201 LOC is the location to use for the tree generated.
3205 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3207 struct c_expr result;
3209 result.value = build_unary_op (loc, code, arg.value, 0);
3210 result.original_code = code;
3211 result.original_type = NULL;
3213 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3214 overflow_warning (loc, result.value);
3219 /* This is the entry point used by the parser to build binary operators
3220 in the input. CODE, a tree_code, specifies the binary operator, and
3221 ARG1 and ARG2 are the operands. In addition to constructing the
3222 expression, we check for operands that were written with other binary
3223 operators in a way that is likely to confuse the user.
3225 LOCATION is the location of the binary operator. */
3228 parser_build_binary_op (location_t location, enum tree_code code,
3229 struct c_expr arg1, struct c_expr arg2)
3231 struct c_expr result;
3233 enum tree_code code1 = arg1.original_code;
3234 enum tree_code code2 = arg2.original_code;
3235 tree type1 = (arg1.original_type
3236 ? arg1.original_type
3237 : TREE_TYPE (arg1.value));
3238 tree type2 = (arg2.original_type
3239 ? arg2.original_type
3240 : TREE_TYPE (arg2.value));
3242 result.value = build_binary_op (location, code,
3243 arg1.value, arg2.value, 1);
3244 result.original_code = code;
3245 result.original_type = NULL;
3247 if (TREE_CODE (result.value) == ERROR_MARK)
3250 if (location != UNKNOWN_LOCATION)
3251 protected_set_expr_location (result.value, location);
3253 /* Check for cases such as x+y<<z which users are likely
3255 if (warn_parentheses)
3256 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3258 if (warn_logical_op)
3259 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3260 code1, arg1.value, code2, arg2.value);
3262 /* Warn about comparisons against string literals, with the exception
3263 of testing for equality or inequality of a string literal with NULL. */
3264 if (code == EQ_EXPR || code == NE_EXPR)
3266 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3267 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3268 warning_at (location, OPT_Waddress,
3269 "comparison with string literal results in unspecified behavior");
3271 else if (TREE_CODE_CLASS (code) == tcc_comparison
3272 && (code1 == STRING_CST || code2 == STRING_CST))
3273 warning_at (location, OPT_Waddress,
3274 "comparison with string literal results in unspecified behavior");
3276 if (TREE_OVERFLOW_P (result.value)
3277 && !TREE_OVERFLOW_P (arg1.value)
3278 && !TREE_OVERFLOW_P (arg2.value))
3279 overflow_warning (location, result.value);
3281 /* Warn about comparisons of different enum types. */
3282 if (warn_enum_compare
3283 && TREE_CODE_CLASS (code) == tcc_comparison
3284 && TREE_CODE (type1) == ENUMERAL_TYPE
3285 && TREE_CODE (type2) == ENUMERAL_TYPE
3286 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3287 warning_at (location, OPT_Wenum_compare,
3288 "comparison between %qT and %qT",
3294 /* Return a tree for the difference of pointers OP0 and OP1.
3295 The resulting tree has type int. */
3298 pointer_diff (location_t loc, tree op0, tree op1)
3300 tree restype = ptrdiff_type_node;
3301 tree result, inttype;
3303 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3304 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3305 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3306 tree con0, con1, lit0, lit1;
3307 tree orig_op1 = op1;
3309 /* If the operands point into different address spaces, we need to
3310 explicitly convert them to pointers into the common address space
3311 before we can subtract the numerical address values. */
3314 addr_space_t as_common;
3317 /* Determine the common superset address space. This is guaranteed
3318 to exist because the caller verified that comp_target_types
3319 returned non-zero. */
3320 if (!addr_space_superset (as0, as1, &as_common))
3323 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3324 op0 = convert (common_type, op0);
3325 op1 = convert (common_type, op1);
3328 /* Determine integer type to perform computations in. This will usually
3329 be the same as the result type (ptrdiff_t), but may need to be a wider
3330 type if pointers for the address space are wider than ptrdiff_t. */
3331 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3332 inttype = lang_hooks.types.type_for_size
3333 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3338 if (TREE_CODE (target_type) == VOID_TYPE)
3339 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3340 "pointer of type %<void *%> used in subtraction");
3341 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3342 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3343 "pointer to a function used in subtraction");
3345 /* If the conversion to ptrdiff_type does anything like widening or
3346 converting a partial to an integral mode, we get a convert_expression
3347 that is in the way to do any simplifications.
3348 (fold-const.c doesn't know that the extra bits won't be needed.
3349 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3350 different mode in place.)
3351 So first try to find a common term here 'by hand'; we want to cover
3352 at least the cases that occur in legal static initializers. */
3353 if (CONVERT_EXPR_P (op0)
3354 && (TYPE_PRECISION (TREE_TYPE (op0))
3355 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3356 con0 = TREE_OPERAND (op0, 0);
3359 if (CONVERT_EXPR_P (op1)
3360 && (TYPE_PRECISION (TREE_TYPE (op1))
3361 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3362 con1 = TREE_OPERAND (op1, 0);
3366 if (TREE_CODE (con0) == PLUS_EXPR)
3368 lit0 = TREE_OPERAND (con0, 1);
3369 con0 = TREE_OPERAND (con0, 0);
3372 lit0 = integer_zero_node;
3374 if (TREE_CODE (con1) == PLUS_EXPR)
3376 lit1 = TREE_OPERAND (con1, 1);
3377 con1 = TREE_OPERAND (con1, 0);
3380 lit1 = integer_zero_node;
3382 if (operand_equal_p (con0, con1, 0))
3389 /* First do the subtraction as integers;
3390 then drop through to build the divide operator.
3391 Do not do default conversions on the minus operator
3392 in case restype is a short type. */
3394 op0 = build_binary_op (loc,
3395 MINUS_EXPR, convert (inttype, op0),
3396 convert (inttype, op1), 0);
3397 /* This generates an error if op1 is pointer to incomplete type. */
3398 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3399 error_at (loc, "arithmetic on pointer to an incomplete type");
3401 /* This generates an error if op0 is pointer to incomplete type. */
3402 op1 = c_size_in_bytes (target_type);
3404 /* Divide by the size, in easiest possible way. */
3405 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3406 op0, convert (inttype, op1));
3408 /* Convert to final result type if necessary. */
3409 return convert (restype, result);
3412 /* Construct and perhaps optimize a tree representation
3413 for a unary operation. CODE, a tree_code, specifies the operation
3414 and XARG is the operand.
3415 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3416 the default promotions (such as from short to int).
3417 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3418 allows non-lvalues; this is only used to handle conversion of non-lvalue
3419 arrays to pointers in C99.
3421 LOCATION is the location of the operator. */
3424 build_unary_op (location_t location,
3425 enum tree_code code, tree xarg, int flag)
3427 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3430 enum tree_code typecode;
3432 tree ret = error_mark_node;
3433 tree eptype = NULL_TREE;
3434 int noconvert = flag;
3435 const char *invalid_op_diag;
3438 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3440 arg = remove_c_maybe_const_expr (arg);
3442 if (code != ADDR_EXPR)
3443 arg = require_complete_type (arg);
3445 typecode = TREE_CODE (TREE_TYPE (arg));
3446 if (typecode == ERROR_MARK)
3447 return error_mark_node;
3448 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3449 typecode = INTEGER_TYPE;
3451 if ((invalid_op_diag
3452 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3454 error_at (location, invalid_op_diag);
3455 return error_mark_node;
3458 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3460 eptype = TREE_TYPE (arg);
3461 arg = TREE_OPERAND (arg, 0);
3467 /* This is used for unary plus, because a CONVERT_EXPR
3468 is enough to prevent anybody from looking inside for
3469 associativity, but won't generate any code. */
3470 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3471 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3472 || typecode == VECTOR_TYPE))
3474 error_at (location, "wrong type argument to unary plus");
3475 return error_mark_node;
3477 else if (!noconvert)
3478 arg = default_conversion (arg);
3479 arg = non_lvalue_loc (location, arg);
3483 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3484 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3485 || typecode == VECTOR_TYPE))
3487 error_at (location, "wrong type argument to unary minus");
3488 return error_mark_node;
3490 else if (!noconvert)
3491 arg = default_conversion (arg);
3495 /* ~ works on integer types and non float vectors. */
3496 if (typecode == INTEGER_TYPE
3497 || (typecode == VECTOR_TYPE
3498 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3501 arg = default_conversion (arg);
3503 else if (typecode == COMPLEX_TYPE)
3506 pedwarn (location, OPT_pedantic,
3507 "ISO C does not support %<~%> for complex conjugation");
3509 arg = default_conversion (arg);
3513 error_at (location, "wrong type argument to bit-complement");
3514 return error_mark_node;
3519 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3521 error_at (location, "wrong type argument to abs");
3522 return error_mark_node;
3524 else if (!noconvert)
3525 arg = default_conversion (arg);
3529 /* Conjugating a real value is a no-op, but allow it anyway. */
3530 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3531 || typecode == COMPLEX_TYPE))
3533 error_at (location, "wrong type argument to conjugation");
3534 return error_mark_node;
3536 else if (!noconvert)
3537 arg = default_conversion (arg);
3540 case TRUTH_NOT_EXPR:
3541 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3542 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3543 && typecode != COMPLEX_TYPE)
3546 "wrong type argument to unary exclamation mark");
3547 return error_mark_node;
3549 arg = c_objc_common_truthvalue_conversion (location, arg);
3550 ret = invert_truthvalue_loc (location, arg);
3551 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3552 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3553 location = EXPR_LOCATION (ret);
3554 goto return_build_unary_op;
3558 ret = build_real_imag_expr (location, code, arg);
3559 if (ret == error_mark_node)
3560 return error_mark_node;
3561 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3562 eptype = TREE_TYPE (eptype);
3563 goto return_build_unary_op;
3565 case PREINCREMENT_EXPR:
3566 case POSTINCREMENT_EXPR:
3567 case PREDECREMENT_EXPR:
3568 case POSTDECREMENT_EXPR:
3570 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3572 tree inner = build_unary_op (location, code,
3573 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3574 if (inner == error_mark_node)
3575 return error_mark_node;
3576 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3577 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3578 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3579 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3580 goto return_build_unary_op;
3583 /* Complain about anything that is not a true lvalue. In
3584 Objective-C, skip this check for property_refs. */
3585 if (!objc_is_property_ref (arg)
3586 && !lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3587 || code == POSTINCREMENT_EXPR)
3590 return error_mark_node;
3592 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3594 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3595 warning_at (location, OPT_Wc___compat,
3596 "increment of enumeration value is invalid in C++");
3598 warning_at (location, OPT_Wc___compat,
3599 "decrement of enumeration value is invalid in C++");
3602 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3603 arg = c_fully_fold (arg, false, NULL);
3605 /* Increment or decrement the real part of the value,
3606 and don't change the imaginary part. */
3607 if (typecode == COMPLEX_TYPE)
3611 pedwarn (location, OPT_pedantic,
3612 "ISO C does not support %<++%> and %<--%> on complex types");
3614 arg = stabilize_reference (arg);
3615 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3616 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3617 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3618 if (real == error_mark_node || imag == error_mark_node)
3619 return error_mark_node;
3620 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3622 goto return_build_unary_op;
3625 /* Report invalid types. */
3627 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3628 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3630 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3631 error_at (location, "wrong type argument to increment");
3633 error_at (location, "wrong type argument to decrement");
3635 return error_mark_node;
3641 argtype = TREE_TYPE (arg);
3643 /* Compute the increment. */
3645 if (typecode == POINTER_TYPE)
3647 /* If pointer target is an undefined struct,
3648 we just cannot know how to do the arithmetic. */
3649 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3651 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3653 "increment of pointer to unknown structure");
3656 "decrement of pointer to unknown structure");
3658 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3659 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3661 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3662 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3663 "wrong type argument to increment");
3665 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3666 "wrong type argument to decrement");
3669 inc = c_size_in_bytes (TREE_TYPE (argtype));
3670 inc = fold_convert_loc (location, sizetype, inc);
3672 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3674 /* For signed fract types, we invert ++ to -- or
3675 -- to ++, and change inc from 1 to -1, because
3676 it is not possible to represent 1 in signed fract constants.
3677 For unsigned fract types, the result always overflows and
3678 we get an undefined (original) or the maximum value. */
3679 if (code == PREINCREMENT_EXPR)
3680 code = PREDECREMENT_EXPR;
3681 else if (code == PREDECREMENT_EXPR)
3682 code = PREINCREMENT_EXPR;
3683 else if (code == POSTINCREMENT_EXPR)
3684 code = POSTDECREMENT_EXPR;
3685 else /* code == POSTDECREMENT_EXPR */
3686 code = POSTINCREMENT_EXPR;
3688 inc = integer_minus_one_node;
3689 inc = convert (argtype, inc);
3693 inc = integer_one_node;
3694 inc = convert (argtype, inc);
3697 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3698 need to ask Objective-C to build the increment or decrement
3699 expression for it. */
3700 if (objc_is_property_ref (arg))
3701 return objc_build_incr_expr_for_property_ref (location, code,
3704 /* Report a read-only lvalue. */
3705 if (TYPE_READONLY (argtype))
3707 readonly_error (arg,
3708 ((code == PREINCREMENT_EXPR
3709 || code == POSTINCREMENT_EXPR)
3710 ? lv_increment : lv_decrement));
3711 return error_mark_node;
3713 else if (TREE_READONLY (arg))
3714 readonly_warning (arg,
3715 ((code == PREINCREMENT_EXPR
3716 || code == POSTINCREMENT_EXPR)
3717 ? lv_increment : lv_decrement));
3719 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3720 val = boolean_increment (code, arg);
3722 val = build2 (code, TREE_TYPE (arg), arg, inc);
3723 TREE_SIDE_EFFECTS (val) = 1;
3724 if (TREE_CODE (val) != code)
3725 TREE_NO_WARNING (val) = 1;
3727 goto return_build_unary_op;
3731 /* Note that this operation never does default_conversion. */
3733 /* The operand of unary '&' must be an lvalue (which excludes
3734 expressions of type void), or, in C99, the result of a [] or
3735 unary '*' operator. */
3736 if (VOID_TYPE_P (TREE_TYPE (arg))
3737 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3738 && (TREE_CODE (arg) != INDIRECT_REF
3740 pedwarn (location, 0, "taking address of expression of type %<void%>");
3742 /* Let &* cancel out to simplify resulting code. */
3743 if (TREE_CODE (arg) == INDIRECT_REF)
3745 /* Don't let this be an lvalue. */
3746 if (lvalue_p (TREE_OPERAND (arg, 0)))
3747 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3748 ret = TREE_OPERAND (arg, 0);
3749 goto return_build_unary_op;
3752 /* For &x[y], return x+y */
3753 if (TREE_CODE (arg) == ARRAY_REF)
3755 tree op0 = TREE_OPERAND (arg, 0);
3756 if (!c_mark_addressable (op0))
3757 return error_mark_node;
3758 return build_binary_op (location, PLUS_EXPR,
3759 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3760 ? array_to_pointer_conversion (location,
3763 TREE_OPERAND (arg, 1), 1);
3766 /* Anything not already handled and not a true memory reference
3767 or a non-lvalue array is an error. */
3768 else if (typecode != FUNCTION_TYPE && !flag
3769 && !lvalue_or_else (arg, lv_addressof))
3770 return error_mark_node;
3772 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3774 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3776 tree inner = build_unary_op (location, code,
3777 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3778 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3779 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3780 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3781 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3782 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3783 goto return_build_unary_op;
3786 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3787 argtype = TREE_TYPE (arg);
3789 /* If the lvalue is const or volatile, merge that into the type
3790 to which the address will point. This should only be needed
3791 for function types. */
3792 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3793 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3795 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3796 int quals = orig_quals;
3798 if (TREE_READONLY (arg))
3799 quals |= TYPE_QUAL_CONST;
3800 if (TREE_THIS_VOLATILE (arg))
3801 quals |= TYPE_QUAL_VOLATILE;
3803 gcc_assert (quals == orig_quals
3804 || TREE_CODE (argtype) == FUNCTION_TYPE);
3806 argtype = c_build_qualified_type (argtype, quals);
3809 if (!c_mark_addressable (arg))
3810 return error_mark_node;
3812 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3813 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3815 argtype = build_pointer_type (argtype);
3817 /* ??? Cope with user tricks that amount to offsetof. Delete this
3818 when we have proper support for integer constant expressions. */
3819 val = get_base_address (arg);
3820 if (val && TREE_CODE (val) == INDIRECT_REF
3821 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3823 tree op0 = fold_convert_loc (location, sizetype,
3824 fold_offsetof (arg, val)), op1;
3826 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3827 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3828 goto return_build_unary_op;
3831 val = build1 (ADDR_EXPR, argtype, arg);
3834 goto return_build_unary_op;
3841 argtype = TREE_TYPE (arg);
3842 if (TREE_CODE (arg) == INTEGER_CST)
3843 ret = (require_constant_value
3844 ? fold_build1_initializer_loc (location, code, argtype, arg)
3845 : fold_build1_loc (location, code, argtype, arg));
3847 ret = build1 (code, argtype, arg);
3848 return_build_unary_op:
3849 gcc_assert (ret != error_mark_node);
3850 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3851 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3852 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3853 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3854 ret = note_integer_operands (ret);
3856 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3857 protected_set_expr_location (ret, location);
3861 /* Return nonzero if REF is an lvalue valid for this language.
3862 Lvalues can be assigned, unless their type has TYPE_READONLY.
3863 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3866 lvalue_p (const_tree ref)
3868 const enum tree_code code = TREE_CODE (ref);
3875 return lvalue_p (TREE_OPERAND (ref, 0));
3877 case C_MAYBE_CONST_EXPR:
3878 return lvalue_p (TREE_OPERAND (ref, 1));
3880 case COMPOUND_LITERAL_EXPR:
3890 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3891 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3894 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3901 /* Give an error for storing in something that is 'const'. */
3904 readonly_error (tree arg, enum lvalue_use use)
3906 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3908 /* Using this macro rather than (for example) arrays of messages
3909 ensures that all the format strings are checked at compile
3911 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3912 : (use == lv_increment ? (I) \
3913 : (use == lv_decrement ? (D) : (AS))))
3914 if (TREE_CODE (arg) == COMPONENT_REF)
3916 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3917 readonly_error (TREE_OPERAND (arg, 0), use);
3919 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3920 G_("increment of read-only member %qD"),
3921 G_("decrement of read-only member %qD"),
3922 G_("read-only member %qD used as %<asm%> output")),
3923 TREE_OPERAND (arg, 1));
3925 else if (TREE_CODE (arg) == VAR_DECL)
3926 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3927 G_("increment of read-only variable %qD"),
3928 G_("decrement of read-only variable %qD"),
3929 G_("read-only variable %qD used as %<asm%> output")),
3932 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3933 G_("increment of read-only location %qE"),
3934 G_("decrement of read-only location %qE"),
3935 G_("read-only location %qE used as %<asm%> output")),
3939 /* Give a warning for storing in something that is read-only in GCC
3940 terms but not const in ISO C terms. */
3943 readonly_warning (tree arg, enum lvalue_use use)
3948 warning (0, "assignment of read-only location %qE", arg);
3951 warning (0, "increment of read-only location %qE", arg);
3954 warning (0, "decrement of read-only location %qE", arg);
3963 /* Return nonzero if REF is an lvalue valid for this language;
3964 otherwise, print an error message and return zero. USE says
3965 how the lvalue is being used and so selects the error message. */
3968 lvalue_or_else (const_tree ref, enum lvalue_use use)
3970 int win = lvalue_p (ref);
3978 /* Mark EXP saying that we need to be able to take the
3979 address of it; it should not be allocated in a register.
3980 Returns true if successful. */
3983 c_mark_addressable (tree exp)
3988 switch (TREE_CODE (x))
3991 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3994 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3998 /* ... fall through ... */
4004 x = TREE_OPERAND (x, 0);
4007 case COMPOUND_LITERAL_EXPR:
4009 TREE_ADDRESSABLE (x) = 1;
4016 if (C_DECL_REGISTER (x)
4017 && DECL_NONLOCAL (x))
4019 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4022 ("global register variable %qD used in nested function", x);
4025 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4027 else if (C_DECL_REGISTER (x))
4029 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4030 error ("address of global register variable %qD requested", x);
4032 error ("address of register variable %qD requested", x);
4038 TREE_ADDRESSABLE (x) = 1;
4045 /* Convert EXPR to TYPE, warning about conversion problems with
4046 constants. SEMANTIC_TYPE is the type this conversion would use
4047 without excess precision. If SEMANTIC_TYPE is NULL, this function
4048 is equivalent to convert_and_check. This function is a wrapper that
4049 handles conversions that may be different than
4050 the usual ones because of excess precision. */
4053 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4055 if (TREE_TYPE (expr) == type)
4059 return convert_and_check (type, expr);
4061 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4062 && TREE_TYPE (expr) != semantic_type)
4064 /* For integers, we need to check the real conversion, not
4065 the conversion to the excess precision type. */
4066 expr = convert_and_check (semantic_type, expr);
4068 /* Result type is the excess precision type, which should be
4069 large enough, so do not check. */
4070 return convert (type, expr);
4073 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4074 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4075 if folded to an integer constant then the unselected half may
4076 contain arbitrary operations not normally permitted in constant
4077 expressions. Set the location of the expression to LOC. */
4080 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4081 tree op1, tree op1_original_type, tree op2,
4082 tree op2_original_type)
4086 enum tree_code code1;
4087 enum tree_code code2;
4088 tree result_type = NULL;
4089 tree semantic_result_type = NULL;
4090 tree orig_op1 = op1, orig_op2 = op2;
4091 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4092 bool ifexp_int_operands;
4095 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4096 if (op1_int_operands)
4097 op1 = remove_c_maybe_const_expr (op1);
4098 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4099 if (op2_int_operands)
4100 op2 = remove_c_maybe_const_expr (op2);
4101 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4102 if (ifexp_int_operands)
4103 ifexp = remove_c_maybe_const_expr (ifexp);
4105 /* Promote both alternatives. */
4107 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4108 op1 = default_conversion (op1);
4109 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4110 op2 = default_conversion (op2);
4112 if (TREE_CODE (ifexp) == ERROR_MARK
4113 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4114 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4115 return error_mark_node;
4117 type1 = TREE_TYPE (op1);
4118 code1 = TREE_CODE (type1);
4119 type2 = TREE_TYPE (op2);
4120 code2 = TREE_CODE (type2);
4122 /* C90 does not permit non-lvalue arrays in conditional expressions.
4123 In C99 they will be pointers by now. */
4124 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4126 error_at (colon_loc, "non-lvalue array in conditional expression");
4127 return error_mark_node;
4130 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4131 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4132 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4133 || code1 == COMPLEX_TYPE)
4134 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4135 || code2 == COMPLEX_TYPE))
4137 semantic_result_type = c_common_type (type1, type2);
4138 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4140 op1 = TREE_OPERAND (op1, 0);
4141 type1 = TREE_TYPE (op1);
4142 gcc_assert (TREE_CODE (type1) == code1);
4144 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4146 op2 = TREE_OPERAND (op2, 0);
4147 type2 = TREE_TYPE (op2);
4148 gcc_assert (TREE_CODE (type2) == code2);
4152 if (warn_cxx_compat)
4154 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4155 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4157 if (TREE_CODE (t1) == ENUMERAL_TYPE
4158 && TREE_CODE (t2) == ENUMERAL_TYPE
4159 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4160 warning_at (colon_loc, OPT_Wc___compat,
4161 ("different enum types in conditional is "
4162 "invalid in C++: %qT vs %qT"),
4166 /* Quickly detect the usual case where op1 and op2 have the same type
4168 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4171 result_type = type1;
4173 result_type = TYPE_MAIN_VARIANT (type1);
4175 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4176 || code1 == COMPLEX_TYPE)
4177 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4178 || code2 == COMPLEX_TYPE))
4180 result_type = c_common_type (type1, type2);
4181 do_warn_double_promotion (result_type, type1, type2,
4182 "implicit conversion from %qT to %qT to "
4183 "match other result of conditional",
4186 /* If -Wsign-compare, warn here if type1 and type2 have
4187 different signedness. We'll promote the signed to unsigned
4188 and later code won't know it used to be different.
4189 Do this check on the original types, so that explicit casts
4190 will be considered, but default promotions won't. */
4191 if (c_inhibit_evaluation_warnings == 0)
4193 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4194 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4196 if (unsigned_op1 ^ unsigned_op2)
4200 /* Do not warn if the result type is signed, since the
4201 signed type will only be chosen if it can represent
4202 all the values of the unsigned type. */
4203 if (!TYPE_UNSIGNED (result_type))
4207 bool op1_maybe_const = true;
4208 bool op2_maybe_const = true;
4210 /* Do not warn if the signed quantity is an
4211 unsuffixed integer literal (or some static
4212 constant expression involving such literals) and
4213 it is non-negative. This warning requires the
4214 operands to be folded for best results, so do
4215 that folding in this case even without
4216 warn_sign_compare to avoid warning options
4217 possibly affecting code generation. */
4218 c_inhibit_evaluation_warnings
4219 += (ifexp == truthvalue_false_node);
4220 op1 = c_fully_fold (op1, require_constant_value,
4222 c_inhibit_evaluation_warnings
4223 -= (ifexp == truthvalue_false_node);
4225 c_inhibit_evaluation_warnings
4226 += (ifexp == truthvalue_true_node);
4227 op2 = c_fully_fold (op2, require_constant_value,
4229 c_inhibit_evaluation_warnings
4230 -= (ifexp == truthvalue_true_node);
4232 if (warn_sign_compare)
4235 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4237 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4240 warning_at (colon_loc, OPT_Wsign_compare,
4241 ("signed and unsigned type in "
4242 "conditional expression"));
4244 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4245 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4246 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4247 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4252 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4254 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4255 pedwarn (colon_loc, OPT_pedantic,
4256 "ISO C forbids conditional expr with only one void side");
4257 result_type = void_type_node;
4259 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4261 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4262 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4263 addr_space_t as_common;
4265 if (comp_target_types (colon_loc, type1, type2))
4266 result_type = common_pointer_type (type1, type2);
4267 else if (null_pointer_constant_p (orig_op1))
4268 result_type = type2;
4269 else if (null_pointer_constant_p (orig_op2))
4270 result_type = type1;
4271 else if (!addr_space_superset (as1, as2, &as_common))
4273 error_at (colon_loc, "pointers to disjoint address spaces "
4274 "used in conditional expression");
4275 return error_mark_node;
4277 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4279 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4280 pedwarn (colon_loc, OPT_pedantic,
4281 "ISO C forbids conditional expr between "
4282 "%<void *%> and function pointer");
4283 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4284 TREE_TYPE (type2)));
4286 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4288 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4289 pedwarn (colon_loc, OPT_pedantic,
4290 "ISO C forbids conditional expr between "
4291 "%<void *%> and function pointer");
4292 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4293 TREE_TYPE (type1)));
4295 /* Objective-C pointer comparisons are a bit more lenient. */
4296 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4297 result_type = objc_common_type (type1, type2);
4300 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4302 pedwarn (colon_loc, 0,
4303 "pointer type mismatch in conditional expression");
4304 result_type = build_pointer_type
4305 (build_qualified_type (void_type_node, qual));
4308 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4310 if (!null_pointer_constant_p (orig_op2))
4311 pedwarn (colon_loc, 0,
4312 "pointer/integer type mismatch in conditional expression");
4315 op2 = null_pointer_node;
4317 result_type = type1;
4319 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4321 if (!null_pointer_constant_p (orig_op1))
4322 pedwarn (colon_loc, 0,
4323 "pointer/integer type mismatch in conditional expression");
4326 op1 = null_pointer_node;
4328 result_type = type2;
4333 if (flag_cond_mismatch)
4334 result_type = void_type_node;
4337 error_at (colon_loc, "type mismatch in conditional expression");
4338 return error_mark_node;
4342 /* Merge const and volatile flags of the incoming types. */
4344 = build_type_variant (result_type,
4345 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4346 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4348 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4349 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4351 if (ifexp_bcp && ifexp == truthvalue_true_node)
4353 op2_int_operands = true;
4354 op1 = c_fully_fold (op1, require_constant_value, NULL);
4356 if (ifexp_bcp && ifexp == truthvalue_false_node)
4358 op1_int_operands = true;
4359 op2 = c_fully_fold (op2, require_constant_value, NULL);
4361 int_const = int_operands = (ifexp_int_operands
4363 && op2_int_operands);
4366 int_const = ((ifexp == truthvalue_true_node
4367 && TREE_CODE (orig_op1) == INTEGER_CST
4368 && !TREE_OVERFLOW (orig_op1))
4369 || (ifexp == truthvalue_false_node
4370 && TREE_CODE (orig_op2) == INTEGER_CST
4371 && !TREE_OVERFLOW (orig_op2)));
4373 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4374 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4377 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4379 ret = note_integer_operands (ret);
4381 if (semantic_result_type)
4382 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4384 protected_set_expr_location (ret, colon_loc);
4388 /* Return a compound expression that performs two expressions and
4389 returns the value of the second of them.
4391 LOC is the location of the COMPOUND_EXPR. */
4394 build_compound_expr (location_t loc, tree expr1, tree expr2)
4396 bool expr1_int_operands, expr2_int_operands;
4397 tree eptype = NULL_TREE;
4400 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4401 if (expr1_int_operands)
4402 expr1 = remove_c_maybe_const_expr (expr1);
4403 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4404 if (expr2_int_operands)
4405 expr2 = remove_c_maybe_const_expr (expr2);
4407 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4408 expr1 = TREE_OPERAND (expr1, 0);
4409 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4411 eptype = TREE_TYPE (expr2);
4412 expr2 = TREE_OPERAND (expr2, 0);
4415 if (!TREE_SIDE_EFFECTS (expr1))
4417 /* The left-hand operand of a comma expression is like an expression
4418 statement: with -Wunused, we should warn if it doesn't have
4419 any side-effects, unless it was explicitly cast to (void). */
4420 if (warn_unused_value)
4422 if (VOID_TYPE_P (TREE_TYPE (expr1))
4423 && CONVERT_EXPR_P (expr1))
4425 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4426 && TREE_CODE (expr1) == COMPOUND_EXPR
4427 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4428 ; /* (void) a, (void) b, c */
4430 warning_at (loc, OPT_Wunused_value,
4431 "left-hand operand of comma expression has no effect");
4435 /* With -Wunused, we should also warn if the left-hand operand does have
4436 side-effects, but computes a value which is not used. For example, in
4437 `foo() + bar(), baz()' the result of the `+' operator is not used,
4438 so we should issue a warning. */
4439 else if (warn_unused_value)
4440 warn_if_unused_value (expr1, loc);
4442 if (expr2 == error_mark_node)
4443 return error_mark_node;
4445 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4448 && expr1_int_operands
4449 && expr2_int_operands)
4450 ret = note_integer_operands (ret);
4453 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4455 protected_set_expr_location (ret, loc);
4459 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4460 which we are casting. OTYPE is the type of the expression being
4461 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4462 of the cast. -Wcast-qual appeared on the command line. Named
4463 address space qualifiers are not handled here, because they result
4464 in different warnings. */
4467 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4469 tree in_type = type;
4470 tree in_otype = otype;
4475 /* Check that the qualifiers on IN_TYPE are a superset of the
4476 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4477 nodes is uninteresting and we stop as soon as we hit a
4478 non-POINTER_TYPE node on either type. */
4481 in_otype = TREE_TYPE (in_otype);
4482 in_type = TREE_TYPE (in_type);
4484 /* GNU C allows cv-qualified function types. 'const' means the
4485 function is very pure, 'volatile' means it can't return. We
4486 need to warn when such qualifiers are added, not when they're
4488 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4489 && TREE_CODE (in_type) == FUNCTION_TYPE)
4490 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4491 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4493 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4494 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4496 while (TREE_CODE (in_type) == POINTER_TYPE
4497 && TREE_CODE (in_otype) == POINTER_TYPE);
4500 warning_at (loc, OPT_Wcast_qual,
4501 "cast adds %q#v qualifier to function type", added);
4504 /* There are qualifiers present in IN_OTYPE that are not present
4506 warning_at (loc, OPT_Wcast_qual,
4507 "cast discards %q#v qualifier from pointer target type",
4510 if (added || discarded)
4513 /* A cast from **T to const **T is unsafe, because it can cause a
4514 const value to be changed with no additional warning. We only
4515 issue this warning if T is the same on both sides, and we only
4516 issue the warning if there are the same number of pointers on
4517 both sides, as otherwise the cast is clearly unsafe anyhow. A
4518 cast is unsafe when a qualifier is added at one level and const
4519 is not present at all outer levels.
4521 To issue this warning, we check at each level whether the cast
4522 adds new qualifiers not already seen. We don't need to special
4523 case function types, as they won't have the same
4524 TYPE_MAIN_VARIANT. */
4526 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4528 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4533 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4536 in_type = TREE_TYPE (in_type);
4537 in_otype = TREE_TYPE (in_otype);
4538 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4541 warning_at (loc, OPT_Wcast_qual,
4542 "to be safe all intermediate pointers in cast from "
4543 "%qT to %qT must be %<const%> qualified",
4548 is_const = TYPE_READONLY (in_type);
4550 while (TREE_CODE (in_type) == POINTER_TYPE);
4553 /* Build an expression representing a cast to type TYPE of expression EXPR.
4554 LOC is the location of the cast-- typically the open paren of the cast. */
4557 build_c_cast (location_t loc, tree type, tree expr)
4561 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4562 expr = TREE_OPERAND (expr, 0);
4566 if (type == error_mark_node || expr == error_mark_node)
4567 return error_mark_node;
4569 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4570 only in <protocol> qualifications. But when constructing cast expressions,
4571 the protocols do matter and must be kept around. */
4572 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4573 return build1 (NOP_EXPR, type, expr);
4575 type = TYPE_MAIN_VARIANT (type);
4577 if (TREE_CODE (type) == ARRAY_TYPE)
4579 error_at (loc, "cast specifies array type");
4580 return error_mark_node;
4583 if (TREE_CODE (type) == FUNCTION_TYPE)
4585 error_at (loc, "cast specifies function type");
4586 return error_mark_node;
4589 if (!VOID_TYPE_P (type))
4591 value = require_complete_type (value);
4592 if (value == error_mark_node)
4593 return error_mark_node;
4596 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4598 if (TREE_CODE (type) == RECORD_TYPE
4599 || TREE_CODE (type) == UNION_TYPE)
4600 pedwarn (loc, OPT_pedantic,
4601 "ISO C forbids casting nonscalar to the same type");
4603 else if (TREE_CODE (type) == UNION_TYPE)
4607 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4608 if (TREE_TYPE (field) != error_mark_node
4609 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4610 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4616 bool maybe_const = true;
4618 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4619 t = c_fully_fold (value, false, &maybe_const);
4620 t = build_constructor_single (type, field, t);
4622 t = c_wrap_maybe_const (t, true);
4623 t = digest_init (loc, type, t,
4624 NULL_TREE, false, true, 0);
4625 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4628 error_at (loc, "cast to union type from type not present in union");
4629 return error_mark_node;
4635 if (type == void_type_node)
4637 tree t = build1 (CONVERT_EXPR, type, value);
4638 SET_EXPR_LOCATION (t, loc);
4642 otype = TREE_TYPE (value);
4644 /* Optionally warn about potentially worrisome casts. */
4646 && TREE_CODE (type) == POINTER_TYPE
4647 && TREE_CODE (otype) == POINTER_TYPE)
4648 handle_warn_cast_qual (loc, type, otype);
4650 /* Warn about conversions between pointers to disjoint
4652 if (TREE_CODE (type) == POINTER_TYPE
4653 && TREE_CODE (otype) == POINTER_TYPE
4654 && !null_pointer_constant_p (value))
4656 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4657 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4658 addr_space_t as_common;
4660 if (!addr_space_superset (as_to, as_from, &as_common))
4662 if (ADDR_SPACE_GENERIC_P (as_from))
4663 warning_at (loc, 0, "cast to %s address space pointer "
4664 "from disjoint generic address space pointer",
4665 c_addr_space_name (as_to));
4667 else if (ADDR_SPACE_GENERIC_P (as_to))
4668 warning_at (loc, 0, "cast to generic address space pointer "
4669 "from disjoint %s address space pointer",
4670 c_addr_space_name (as_from));
4673 warning_at (loc, 0, "cast to %s address space pointer "
4674 "from disjoint %s address space pointer",
4675 c_addr_space_name (as_to),
4676 c_addr_space_name (as_from));
4680 /* Warn about possible alignment problems. */
4681 if (STRICT_ALIGNMENT
4682 && TREE_CODE (type) == POINTER_TYPE
4683 && TREE_CODE (otype) == POINTER_TYPE
4684 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4685 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4686 /* Don't warn about opaque types, where the actual alignment
4687 restriction is unknown. */
4688 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4689 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4690 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4691 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4692 warning_at (loc, OPT_Wcast_align,
4693 "cast increases required alignment of target type");
4695 if (TREE_CODE (type) == INTEGER_TYPE
4696 && TREE_CODE (otype) == POINTER_TYPE
4697 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4698 /* Unlike conversion of integers to pointers, where the
4699 warning is disabled for converting constants because
4700 of cases such as SIG_*, warn about converting constant
4701 pointers to integers. In some cases it may cause unwanted
4702 sign extension, and a warning is appropriate. */
4703 warning_at (loc, OPT_Wpointer_to_int_cast,
4704 "cast from pointer to integer of different size");
4706 if (TREE_CODE (value) == CALL_EXPR
4707 && TREE_CODE (type) != TREE_CODE (otype))
4708 warning_at (loc, OPT_Wbad_function_cast,
4709 "cast from function call of type %qT "
4710 "to non-matching type %qT", otype, type);
4712 if (TREE_CODE (type) == POINTER_TYPE
4713 && TREE_CODE (otype) == INTEGER_TYPE
4714 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4715 /* Don't warn about converting any constant. */
4716 && !TREE_CONSTANT (value))
4718 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4719 "of different size");
4721 if (warn_strict_aliasing <= 2)
4722 strict_aliasing_warning (otype, type, expr);
4724 /* If pedantic, warn for conversions between function and object
4725 pointer types, except for converting a null pointer constant
4726 to function pointer type. */
4728 && TREE_CODE (type) == POINTER_TYPE
4729 && TREE_CODE (otype) == POINTER_TYPE
4730 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4731 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4732 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4733 "conversion of function pointer to object pointer type");
4736 && TREE_CODE (type) == POINTER_TYPE
4737 && TREE_CODE (otype) == POINTER_TYPE
4738 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4739 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4740 && !null_pointer_constant_p (value))
4741 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4742 "conversion of object pointer to function pointer type");
4745 value = convert (type, value);
4747 /* Ignore any integer overflow caused by the cast. */
4748 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4750 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4752 if (!TREE_OVERFLOW (value))
4754 /* Avoid clobbering a shared constant. */
4755 value = copy_node (value);
4756 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4759 else if (TREE_OVERFLOW (value))
4760 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4761 value = build_int_cst_wide (TREE_TYPE (value),
4762 TREE_INT_CST_LOW (value),
4763 TREE_INT_CST_HIGH (value));
4767 /* Don't let a cast be an lvalue. */
4769 value = non_lvalue_loc (loc, value);
4771 /* Don't allow the results of casting to floating-point or complex
4772 types be confused with actual constants, or casts involving
4773 integer and pointer types other than direct integer-to-integer
4774 and integer-to-pointer be confused with integer constant
4775 expressions and null pointer constants. */
4776 if (TREE_CODE (value) == REAL_CST
4777 || TREE_CODE (value) == COMPLEX_CST
4778 || (TREE_CODE (value) == INTEGER_CST
4779 && !((TREE_CODE (expr) == INTEGER_CST
4780 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4781 || TREE_CODE (expr) == REAL_CST
4782 || TREE_CODE (expr) == COMPLEX_CST)))
4783 value = build1 (NOP_EXPR, type, value);
4785 if (CAN_HAVE_LOCATION_P (value))
4786 SET_EXPR_LOCATION (value, loc);
4790 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4791 location of the open paren of the cast, or the position of the cast
4794 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4797 tree type_expr = NULL_TREE;
4798 bool type_expr_const = true;
4800 int saved_wsp = warn_strict_prototypes;
4802 /* This avoids warnings about unprototyped casts on
4803 integers. E.g. "#define SIG_DFL (void(*)())0". */
4804 if (TREE_CODE (expr) == INTEGER_CST)
4805 warn_strict_prototypes = 0;
4806 type = groktypename (type_name, &type_expr, &type_expr_const);
4807 warn_strict_prototypes = saved_wsp;
4809 ret = build_c_cast (loc, type, expr);
4812 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4813 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4814 SET_EXPR_LOCATION (ret, loc);
4817 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4818 SET_EXPR_LOCATION (ret, loc);
4820 /* C++ does not permits types to be defined in a cast, but it
4821 allows references to incomplete types. */
4822 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4823 warning_at (loc, OPT_Wc___compat,
4824 "defining a type in a cast is invalid in C++");
4829 /* Build an assignment expression of lvalue LHS from value RHS.
4830 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4831 may differ from TREE_TYPE (LHS) for an enum bitfield.
4832 MODIFYCODE is the code for a binary operator that we use
4833 to combine the old value of LHS with RHS to get the new value.
4834 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4835 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4836 which may differ from TREE_TYPE (RHS) for an enum value.
4838 LOCATION is the location of the MODIFYCODE operator.
4839 RHS_LOC is the location of the RHS. */
4842 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4843 enum tree_code modifycode,
4844 location_t rhs_loc, tree rhs, tree rhs_origtype)
4848 tree rhs_semantic_type = NULL_TREE;
4849 tree lhstype = TREE_TYPE (lhs);
4850 tree olhstype = lhstype;
4853 /* Types that aren't fully specified cannot be used in assignments. */
4854 lhs = require_complete_type (lhs);
4856 /* Avoid duplicate error messages from operands that had errors. */
4857 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4858 return error_mark_node;
4860 /* For ObjC properties, defer this check. */
4861 if (!objc_is_property_ref (lhs) && !lvalue_or_else (lhs, lv_assign))
4862 return error_mark_node;
4864 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4866 rhs_semantic_type = TREE_TYPE (rhs);
4867 rhs = TREE_OPERAND (rhs, 0);
4872 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4874 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4875 lhs_origtype, modifycode, rhs_loc, rhs,
4877 if (inner == error_mark_node)
4878 return error_mark_node;
4879 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4880 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4881 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4882 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4883 protected_set_expr_location (result, location);
4887 /* If a binary op has been requested, combine the old LHS value with the RHS
4888 producing the value we should actually store into the LHS. */
4890 if (modifycode != NOP_EXPR)
4892 lhs = c_fully_fold (lhs, false, NULL);
4893 lhs = stabilize_reference (lhs);
4894 newrhs = build_binary_op (location,
4895 modifycode, lhs, rhs, 1);
4897 /* The original type of the right hand side is no longer
4899 rhs_origtype = NULL_TREE;
4902 if (c_dialect_objc ())
4904 /* Check if we are modifying an Objective-C property reference;
4905 if so, we need to generate setter calls. */
4906 result = objc_maybe_build_modify_expr (lhs, newrhs);
4910 /* Else, do the check that we postponed for Objective-C. */
4911 if (!lvalue_or_else (lhs, lv_assign))
4912 return error_mark_node;
4915 /* Give an error for storing in something that is 'const'. */
4917 if (TYPE_READONLY (lhstype)
4918 || ((TREE_CODE (lhstype) == RECORD_TYPE
4919 || TREE_CODE (lhstype) == UNION_TYPE)
4920 && C_TYPE_FIELDS_READONLY (lhstype)))
4922 readonly_error (lhs, lv_assign);
4923 return error_mark_node;
4925 else if (TREE_READONLY (lhs))
4926 readonly_warning (lhs, lv_assign);
4928 /* If storing into a structure or union member,
4929 it has probably been given type `int'.
4930 Compute the type that would go with
4931 the actual amount of storage the member occupies. */
4933 if (TREE_CODE (lhs) == COMPONENT_REF
4934 && (TREE_CODE (lhstype) == INTEGER_TYPE
4935 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4936 || TREE_CODE (lhstype) == REAL_TYPE
4937 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4938 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4940 /* If storing in a field that is in actuality a short or narrower than one,
4941 we must store in the field in its actual type. */
4943 if (lhstype != TREE_TYPE (lhs))
4945 lhs = copy_node (lhs);
4946 TREE_TYPE (lhs) = lhstype;
4949 /* Issue -Wc++-compat warnings about an assignment to an enum type
4950 when LHS does not have its original type. This happens for,
4951 e.g., an enum bitfield in a struct. */
4953 && lhs_origtype != NULL_TREE
4954 && lhs_origtype != lhstype
4955 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4957 tree checktype = (rhs_origtype != NULL_TREE
4960 if (checktype != error_mark_node
4961 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4962 warning_at (location, OPT_Wc___compat,
4963 "enum conversion in assignment is invalid in C++");
4966 /* Convert new value to destination type. Fold it first, then
4967 restore any excess precision information, for the sake of
4968 conversion warnings. */
4970 npc = null_pointer_constant_p (newrhs);
4971 newrhs = c_fully_fold (newrhs, false, NULL);
4972 if (rhs_semantic_type)
4973 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4974 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4975 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4976 if (TREE_CODE (newrhs) == ERROR_MARK)
4977 return error_mark_node;
4979 /* Emit ObjC write barrier, if necessary. */
4980 if (c_dialect_objc () && flag_objc_gc)
4982 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4985 protected_set_expr_location (result, location);
4990 /* Scan operands. */
4992 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4993 TREE_SIDE_EFFECTS (result) = 1;
4994 protected_set_expr_location (result, location);
4996 /* If we got the LHS in a different type for storing in,
4997 convert the result back to the nominal type of LHS
4998 so that the value we return always has the same type
4999 as the LHS argument. */
5001 if (olhstype == TREE_TYPE (result))
5004 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5005 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5006 protected_set_expr_location (result, location);
5010 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5011 This is used to implement -fplan9-extensions. */
5014 find_anonymous_field_with_type (tree struct_type, tree type)
5019 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5020 || TREE_CODE (struct_type) == UNION_TYPE);
5022 for (field = TYPE_FIELDS (struct_type);
5024 field = TREE_CHAIN (field))
5026 if (DECL_NAME (field) == NULL
5027 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5033 else if (DECL_NAME (field) == NULL
5034 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5035 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5036 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5046 /* RHS is an expression whose type is pointer to struct. If there is
5047 an anonymous field in RHS with type TYPE, then return a pointer to
5048 that field in RHS. This is used with -fplan9-extensions. This
5049 returns NULL if no conversion could be found. */
5052 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5054 tree rhs_struct_type, lhs_main_type;
5055 tree field, found_field;
5056 bool found_sub_field;
5059 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5060 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5061 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5062 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5064 gcc_assert (POINTER_TYPE_P (type));
5065 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5067 found_field = NULL_TREE;
5068 found_sub_field = false;
5069 for (field = TYPE_FIELDS (rhs_struct_type);
5071 field = TREE_CHAIN (field))
5073 if (DECL_NAME (field) != NULL_TREE
5074 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5075 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5077 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5079 if (found_field != NULL_TREE)
5081 found_field = field;
5083 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5086 if (found_field != NULL_TREE)
5088 found_field = field;
5089 found_sub_field = true;
5093 if (found_field == NULL_TREE)
5096 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5097 build_fold_indirect_ref (rhs), found_field,
5099 ret = build_fold_addr_expr_loc (location, ret);
5101 if (found_sub_field)
5103 ret = convert_to_anonymous_field (location, type, ret);
5104 gcc_assert (ret != NULL_TREE);
5110 /* Convert value RHS to type TYPE as preparation for an assignment to
5111 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5112 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5113 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5114 constant before any folding.
5115 The real work of conversion is done by `convert'.
5116 The purpose of this function is to generate error messages
5117 for assignments that are not allowed in C.
5118 ERRTYPE says whether it is argument passing, assignment,
5119 initialization or return.
5121 LOCATION is the location of the RHS.
5122 FUNCTION is a tree for the function being called.
5123 PARMNUM is the number of the argument, for printing in error messages. */
5126 convert_for_assignment (location_t location, tree type, tree rhs,
5127 tree origtype, enum impl_conv errtype,
5128 bool null_pointer_constant, tree fundecl,
5129 tree function, int parmnum)
5131 enum tree_code codel = TREE_CODE (type);
5132 tree orig_rhs = rhs;
5134 enum tree_code coder;
5135 tree rname = NULL_TREE;
5136 bool objc_ok = false;
5138 if (errtype == ic_argpass)
5141 /* Change pointer to function to the function itself for
5143 if (TREE_CODE (function) == ADDR_EXPR
5144 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5145 function = TREE_OPERAND (function, 0);
5147 /* Handle an ObjC selector specially for diagnostics. */
5148 selector = objc_message_selector ();
5150 if (selector && parmnum > 2)
5157 /* This macro is used to emit diagnostics to ensure that all format
5158 strings are complete sentences, visible to gettext and checked at
5160 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5165 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5166 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5167 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5168 "expected %qT but argument is of type %qT", \
5172 pedwarn (LOCATION, OPT, AS); \
5175 pedwarn_init (LOCATION, OPT, IN); \
5178 pedwarn (LOCATION, OPT, RE); \
5181 gcc_unreachable (); \
5185 /* This macro is used to emit diagnostics to ensure that all format
5186 strings are complete sentences, visible to gettext and checked at
5187 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5188 extra parameter to enumerate qualifiers. */
5190 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5195 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5196 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5197 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5198 "expected %qT but argument is of type %qT", \
5202 pedwarn (LOCATION, OPT, AS, QUALS); \
5205 pedwarn (LOCATION, OPT, IN, QUALS); \
5208 pedwarn (LOCATION, OPT, RE, QUALS); \
5211 gcc_unreachable (); \
5215 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5216 rhs = TREE_OPERAND (rhs, 0);
5218 rhstype = TREE_TYPE (rhs);
5219 coder = TREE_CODE (rhstype);
5221 if (coder == ERROR_MARK)
5222 return error_mark_node;
5224 if (c_dialect_objc ())
5247 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5250 if (warn_cxx_compat)
5252 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5253 if (checktype != error_mark_node
5254 && TREE_CODE (type) == ENUMERAL_TYPE
5255 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5257 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5258 G_("enum conversion when passing argument "
5259 "%d of %qE is invalid in C++"),
5260 G_("enum conversion in assignment is "
5262 G_("enum conversion in initialization is "
5264 G_("enum conversion in return is "
5269 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5272 if (coder == VOID_TYPE)
5274 /* Except for passing an argument to an unprototyped function,
5275 this is a constraint violation. When passing an argument to
5276 an unprototyped function, it is compile-time undefined;
5277 making it a constraint in that case was rejected in
5279 error_at (location, "void value not ignored as it ought to be");
5280 return error_mark_node;
5282 rhs = require_complete_type (rhs);
5283 if (rhs == error_mark_node)
5284 return error_mark_node;
5285 /* A type converts to a reference to it.
5286 This code doesn't fully support references, it's just for the
5287 special case of va_start and va_copy. */
5288 if (codel == REFERENCE_TYPE
5289 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5291 if (!lvalue_p (rhs))
5293 error_at (location, "cannot pass rvalue to reference parameter");
5294 return error_mark_node;
5296 if (!c_mark_addressable (rhs))
5297 return error_mark_node;
5298 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5299 SET_EXPR_LOCATION (rhs, location);
5301 /* We already know that these two types are compatible, but they
5302 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5303 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5304 likely to be va_list, a typedef to __builtin_va_list, which
5305 is different enough that it will cause problems later. */
5306 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5308 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5309 SET_EXPR_LOCATION (rhs, location);
5312 rhs = build1 (NOP_EXPR, type, rhs);
5313 SET_EXPR_LOCATION (rhs, location);
5316 /* Some types can interconvert without explicit casts. */
5317 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5318 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5319 return convert (type, rhs);
5320 /* Arithmetic types all interconvert, and enum is treated like int. */
5321 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5322 || codel == FIXED_POINT_TYPE
5323 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5324 || codel == BOOLEAN_TYPE)
5325 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5326 || coder == FIXED_POINT_TYPE
5327 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5328 || coder == BOOLEAN_TYPE))
5331 bool save = in_late_binary_op;
5332 if (codel == BOOLEAN_TYPE)
5333 in_late_binary_op = true;
5334 ret = convert_and_check (type, orig_rhs);
5335 if (codel == BOOLEAN_TYPE)
5336 in_late_binary_op = save;
5340 /* Aggregates in different TUs might need conversion. */
5341 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5343 && comptypes (type, rhstype))
5344 return convert_and_check (type, rhs);
5346 /* Conversion to a transparent union or record from its member types.
5347 This applies only to function arguments. */
5348 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5349 && TYPE_TRANSPARENT_AGGR (type))
5350 && errtype == ic_argpass)
5352 tree memb, marginal_memb = NULL_TREE;
5354 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5356 tree memb_type = TREE_TYPE (memb);
5358 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5359 TYPE_MAIN_VARIANT (rhstype)))
5362 if (TREE_CODE (memb_type) != POINTER_TYPE)
5365 if (coder == POINTER_TYPE)
5367 tree ttl = TREE_TYPE (memb_type);
5368 tree ttr = TREE_TYPE (rhstype);
5370 /* Any non-function converts to a [const][volatile] void *
5371 and vice versa; otherwise, targets must be the same.
5372 Meanwhile, the lhs target must have all the qualifiers of
5374 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5375 || comp_target_types (location, memb_type, rhstype))
5377 /* If this type won't generate any warnings, use it. */
5378 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5379 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5380 && TREE_CODE (ttl) == FUNCTION_TYPE)
5381 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5382 == TYPE_QUALS (ttr))
5383 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5384 == TYPE_QUALS (ttl))))
5387 /* Keep looking for a better type, but remember this one. */
5389 marginal_memb = memb;
5393 /* Can convert integer zero to any pointer type. */
5394 if (null_pointer_constant)
5396 rhs = null_pointer_node;
5401 if (memb || marginal_memb)
5405 /* We have only a marginally acceptable member type;
5406 it needs a warning. */
5407 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5408 tree ttr = TREE_TYPE (rhstype);
5410 /* Const and volatile mean something different for function
5411 types, so the usual warnings are not appropriate. */
5412 if (TREE_CODE (ttr) == FUNCTION_TYPE
5413 && TREE_CODE (ttl) == FUNCTION_TYPE)
5415 /* Because const and volatile on functions are
5416 restrictions that say the function will not do
5417 certain things, it is okay to use a const or volatile
5418 function where an ordinary one is wanted, but not
5420 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5421 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5422 WARN_FOR_QUALIFIERS (location, 0,
5423 G_("passing argument %d of %qE "
5424 "makes %q#v qualified function "
5425 "pointer from unqualified"),
5426 G_("assignment makes %q#v qualified "
5427 "function pointer from "
5429 G_("initialization makes %q#v qualified "
5430 "function pointer from "
5432 G_("return makes %q#v qualified function "
5433 "pointer from unqualified"),
5434 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5436 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5437 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5438 WARN_FOR_QUALIFIERS (location, 0,
5439 G_("passing argument %d of %qE discards "
5440 "%qv qualifier from pointer target type"),
5441 G_("assignment discards %qv qualifier "
5442 "from pointer target type"),
5443 G_("initialization discards %qv qualifier "
5444 "from pointer target type"),
5445 G_("return discards %qv qualifier from "
5446 "pointer target type"),
5447 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5449 memb = marginal_memb;
5452 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5453 pedwarn (location, OPT_pedantic,
5454 "ISO C prohibits argument conversion to union type");
5456 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5457 return build_constructor_single (type, memb, rhs);
5461 /* Conversions among pointers */
5462 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5463 && (coder == codel))
5465 tree ttl = TREE_TYPE (type);
5466 tree ttr = TREE_TYPE (rhstype);
5469 bool is_opaque_pointer;
5470 int target_cmp = 0; /* Cache comp_target_types () result. */
5474 if (TREE_CODE (mvl) != ARRAY_TYPE)
5475 mvl = TYPE_MAIN_VARIANT (mvl);
5476 if (TREE_CODE (mvr) != ARRAY_TYPE)
5477 mvr = TYPE_MAIN_VARIANT (mvr);
5478 /* Opaque pointers are treated like void pointers. */
5479 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5481 /* The Plan 9 compiler permits a pointer to a struct to be
5482 automatically converted into a pointer to an anonymous field
5483 within the struct. */
5484 if (flag_plan9_extensions
5485 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5486 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5489 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5490 if (new_rhs != NULL_TREE)
5493 rhstype = TREE_TYPE (rhs);
5494 coder = TREE_CODE (rhstype);
5495 ttr = TREE_TYPE (rhstype);
5496 mvr = TYPE_MAIN_VARIANT (ttr);
5500 /* C++ does not allow the implicit conversion void* -> T*. However,
5501 for the purpose of reducing the number of false positives, we
5502 tolerate the special case of
5506 where NULL is typically defined in C to be '(void *) 0'. */
5507 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5508 warning_at (location, OPT_Wc___compat,
5509 "request for implicit conversion "
5510 "from %qT to %qT not permitted in C++", rhstype, type);
5512 /* See if the pointers point to incompatible address spaces. */
5513 asl = TYPE_ADDR_SPACE (ttl);
5514 asr = TYPE_ADDR_SPACE (ttr);
5515 if (!null_pointer_constant_p (rhs)
5516 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5521 error_at (location, "passing argument %d of %qE from pointer to "
5522 "non-enclosed address space", parmnum, rname);
5525 error_at (location, "assignment from pointer to "
5526 "non-enclosed address space");
5529 error_at (location, "initialization from pointer to "
5530 "non-enclosed address space");
5533 error_at (location, "return from pointer to "
5534 "non-enclosed address space");
5539 return error_mark_node;
5542 /* Check if the right-hand side has a format attribute but the
5543 left-hand side doesn't. */
5544 if (warn_missing_format_attribute
5545 && check_missing_format_attribute (type, rhstype))
5550 warning_at (location, OPT_Wmissing_format_attribute,
5551 "argument %d of %qE might be "
5552 "a candidate for a format attribute",
5556 warning_at (location, OPT_Wmissing_format_attribute,
5557 "assignment left-hand side might be "
5558 "a candidate for a format attribute");
5561 warning_at (location, OPT_Wmissing_format_attribute,
5562 "initialization left-hand side might be "
5563 "a candidate for a format attribute");
5566 warning_at (location, OPT_Wmissing_format_attribute,
5567 "return type might be "
5568 "a candidate for a format attribute");
5575 /* Any non-function converts to a [const][volatile] void *
5576 and vice versa; otherwise, targets must be the same.
5577 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5578 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5579 || (target_cmp = comp_target_types (location, type, rhstype))
5580 || is_opaque_pointer
5581 || (c_common_unsigned_type (mvl)
5582 == c_common_unsigned_type (mvr)))
5585 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5588 && !null_pointer_constant
5589 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5590 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5591 G_("ISO C forbids passing argument %d of "
5592 "%qE between function pointer "
5594 G_("ISO C forbids assignment between "
5595 "function pointer and %<void *%>"),
5596 G_("ISO C forbids initialization between "
5597 "function pointer and %<void *%>"),
5598 G_("ISO C forbids return between function "
5599 "pointer and %<void *%>"));
5600 /* Const and volatile mean something different for function types,
5601 so the usual warnings are not appropriate. */
5602 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5603 && TREE_CODE (ttl) != FUNCTION_TYPE)
5605 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5606 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5608 WARN_FOR_QUALIFIERS (location, 0,
5609 G_("passing argument %d of %qE discards "
5610 "%qv qualifier from pointer target type"),
5611 G_("assignment discards %qv qualifier "
5612 "from pointer target type"),
5613 G_("initialization discards %qv qualifier "
5614 "from pointer target type"),
5615 G_("return discards %qv qualifier from "
5616 "pointer target type"),
5617 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5619 /* If this is not a case of ignoring a mismatch in signedness,
5621 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5624 /* If there is a mismatch, do warn. */
5625 else if (warn_pointer_sign)
5626 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5627 G_("pointer targets in passing argument "
5628 "%d of %qE differ in signedness"),
5629 G_("pointer targets in assignment "
5630 "differ in signedness"),
5631 G_("pointer targets in initialization "
5632 "differ in signedness"),
5633 G_("pointer targets in return differ "
5636 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5637 && TREE_CODE (ttr) == FUNCTION_TYPE)
5639 /* Because const and volatile on functions are restrictions
5640 that say the function will not do certain things,
5641 it is okay to use a const or volatile function
5642 where an ordinary one is wanted, but not vice-versa. */
5643 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5644 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5645 WARN_FOR_QUALIFIERS (location, 0,
5646 G_("passing argument %d of %qE makes "
5647 "%q#v qualified function pointer "
5648 "from unqualified"),
5649 G_("assignment makes %q#v qualified function "
5650 "pointer from unqualified"),
5651 G_("initialization makes %q#v qualified "
5652 "function pointer from unqualified"),
5653 G_("return makes %q#v qualified function "
5654 "pointer from unqualified"),
5655 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5659 /* Avoid warning about the volatile ObjC EH puts on decls. */
5661 WARN_FOR_ASSIGNMENT (location, 0,
5662 G_("passing argument %d of %qE from "
5663 "incompatible pointer type"),
5664 G_("assignment from incompatible pointer type"),
5665 G_("initialization from incompatible "
5667 G_("return from incompatible pointer type"));
5669 return convert (type, rhs);
5671 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5673 /* ??? This should not be an error when inlining calls to
5674 unprototyped functions. */
5675 error_at (location, "invalid use of non-lvalue array");
5676 return error_mark_node;
5678 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5680 /* An explicit constant 0 can convert to a pointer,
5681 or one that results from arithmetic, even including
5682 a cast to integer type. */
5683 if (!null_pointer_constant)
5684 WARN_FOR_ASSIGNMENT (location, 0,
5685 G_("passing argument %d of %qE makes "
5686 "pointer from integer without a cast"),
5687 G_("assignment makes pointer from integer "
5689 G_("initialization makes pointer from "
5690 "integer without a cast"),
5691 G_("return makes pointer from integer "
5694 return convert (type, rhs);
5696 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5698 WARN_FOR_ASSIGNMENT (location, 0,
5699 G_("passing argument %d of %qE makes integer "
5700 "from pointer without a cast"),
5701 G_("assignment makes integer from pointer "
5703 G_("initialization makes integer from pointer "
5705 G_("return makes integer from pointer "
5707 return convert (type, rhs);
5709 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5712 bool save = in_late_binary_op;
5713 in_late_binary_op = true;
5714 ret = convert (type, rhs);
5715 in_late_binary_op = save;
5722 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5723 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5724 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5725 "expected %qT but argument is of type %qT", type, rhstype);
5728 error_at (location, "incompatible types when assigning to type %qT from "
5729 "type %qT", type, rhstype);
5733 "incompatible types when initializing type %qT using type %qT",
5738 "incompatible types when returning type %qT but %qT was "
5739 "expected", rhstype, type);
5745 return error_mark_node;
5748 /* If VALUE is a compound expr all of whose expressions are constant, then
5749 return its value. Otherwise, return error_mark_node.
5751 This is for handling COMPOUND_EXPRs as initializer elements
5752 which is allowed with a warning when -pedantic is specified. */
5755 valid_compound_expr_initializer (tree value, tree endtype)
5757 if (TREE_CODE (value) == COMPOUND_EXPR)
5759 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5761 return error_mark_node;
5762 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5765 else if (!initializer_constant_valid_p (value, endtype))
5766 return error_mark_node;
5771 /* Perform appropriate conversions on the initial value of a variable,
5772 store it in the declaration DECL,
5773 and print any error messages that are appropriate.
5774 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5775 If the init is invalid, store an ERROR_MARK.
5777 INIT_LOC is the location of the initial value. */
5780 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5785 /* If variable's type was invalidly declared, just ignore it. */
5787 type = TREE_TYPE (decl);
5788 if (TREE_CODE (type) == ERROR_MARK)
5791 /* Digest the specified initializer into an expression. */
5794 npc = null_pointer_constant_p (init);
5795 value = digest_init (init_loc, type, init, origtype, npc,
5796 true, TREE_STATIC (decl));
5798 /* Store the expression if valid; else report error. */
5800 if (!in_system_header
5801 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5802 warning (OPT_Wtraditional, "traditional C rejects automatic "
5803 "aggregate initialization");
5805 DECL_INITIAL (decl) = value;
5807 /* ANSI wants warnings about out-of-range constant initializers. */
5808 STRIP_TYPE_NOPS (value);
5809 if (TREE_STATIC (decl))
5810 constant_expression_warning (value);
5812 /* Check if we need to set array size from compound literal size. */
5813 if (TREE_CODE (type) == ARRAY_TYPE
5814 && TYPE_DOMAIN (type) == 0
5815 && value != error_mark_node)
5817 tree inside_init = init;
5819 STRIP_TYPE_NOPS (inside_init);
5820 inside_init = fold (inside_init);
5822 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5824 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5826 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5828 /* For int foo[] = (int [3]){1}; we need to set array size
5829 now since later on array initializer will be just the
5830 brace enclosed list of the compound literal. */
5831 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5832 TREE_TYPE (decl) = type;
5833 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5835 layout_decl (cldecl, 0);
5841 /* Methods for storing and printing names for error messages. */
5843 /* Implement a spelling stack that allows components of a name to be pushed
5844 and popped. Each element on the stack is this structure. */
5851 unsigned HOST_WIDE_INT i;
5856 #define SPELLING_STRING 1
5857 #define SPELLING_MEMBER 2
5858 #define SPELLING_BOUNDS 3
5860 static struct spelling *spelling; /* Next stack element (unused). */
5861 static struct spelling *spelling_base; /* Spelling stack base. */
5862 static int spelling_size; /* Size of the spelling stack. */
5864 /* Macros to save and restore the spelling stack around push_... functions.
5865 Alternative to SAVE_SPELLING_STACK. */
5867 #define SPELLING_DEPTH() (spelling - spelling_base)
5868 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5870 /* Push an element on the spelling stack with type KIND and assign VALUE
5873 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5875 int depth = SPELLING_DEPTH (); \
5877 if (depth >= spelling_size) \
5879 spelling_size += 10; \
5880 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5882 RESTORE_SPELLING_DEPTH (depth); \
5885 spelling->kind = (KIND); \
5886 spelling->MEMBER = (VALUE); \
5890 /* Push STRING on the stack. Printed literally. */
5893 push_string (const char *string)
5895 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5898 /* Push a member name on the stack. Printed as '.' STRING. */
5901 push_member_name (tree decl)
5903 const char *const string
5905 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5906 : _("<anonymous>"));
5907 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5910 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5913 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5915 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5918 /* Compute the maximum size in bytes of the printed spelling. */
5921 spelling_length (void)
5926 for (p = spelling_base; p < spelling; p++)
5928 if (p->kind == SPELLING_BOUNDS)
5931 size += strlen (p->u.s) + 1;
5937 /* Print the spelling to BUFFER and return it. */
5940 print_spelling (char *buffer)
5945 for (p = spelling_base; p < spelling; p++)
5946 if (p->kind == SPELLING_BOUNDS)
5948 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5954 if (p->kind == SPELLING_MEMBER)
5956 for (s = p->u.s; (*d = *s++); d++)
5963 /* Issue an error message for a bad initializer component.
5964 GMSGID identifies the message.
5965 The component name is taken from the spelling stack. */
5968 error_init (const char *gmsgid)
5972 /* The gmsgid may be a format string with %< and %>. */
5974 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5976 error ("(near initialization for %qs)", ofwhat);
5979 /* Issue a pedantic warning for a bad initializer component. OPT is
5980 the option OPT_* (from options.h) controlling this warning or 0 if
5981 it is unconditionally given. GMSGID identifies the message. The
5982 component name is taken from the spelling stack. */
5985 pedwarn_init (location_t location, int opt, const char *gmsgid)
5989 /* The gmsgid may be a format string with %< and %>. */
5990 pedwarn (location, opt, gmsgid);
5991 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5993 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5996 /* Issue a warning for a bad initializer component.
5998 OPT is the OPT_W* value corresponding to the warning option that
5999 controls this warning. GMSGID identifies the message. The
6000 component name is taken from the spelling stack. */
6003 warning_init (int opt, const char *gmsgid)
6007 /* The gmsgid may be a format string with %< and %>. */
6008 warning (opt, gmsgid);
6009 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6011 warning (opt, "(near initialization for %qs)", ofwhat);
6014 /* If TYPE is an array type and EXPR is a parenthesized string
6015 constant, warn if pedantic that EXPR is being used to initialize an
6016 object of type TYPE. */
6019 maybe_warn_string_init (tree type, struct c_expr expr)
6022 && TREE_CODE (type) == ARRAY_TYPE
6023 && TREE_CODE (expr.value) == STRING_CST
6024 && expr.original_code != STRING_CST)
6025 pedwarn_init (input_location, OPT_pedantic,
6026 "array initialized from parenthesized string constant");
6029 /* Digest the parser output INIT as an initializer for type TYPE.
6030 Return a C expression of type TYPE to represent the initial value.
6032 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6034 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6036 If INIT is a string constant, STRICT_STRING is true if it is
6037 unparenthesized or we should not warn here for it being parenthesized.
6038 For other types of INIT, STRICT_STRING is not used.
6040 INIT_LOC is the location of the INIT.
6042 REQUIRE_CONSTANT requests an error if non-constant initializers or
6043 elements are seen. */
6046 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6047 bool null_pointer_constant, bool strict_string,
6048 int require_constant)
6050 enum tree_code code = TREE_CODE (type);
6051 tree inside_init = init;
6052 tree semantic_type = NULL_TREE;
6053 bool maybe_const = true;
6055 if (type == error_mark_node
6057 || init == error_mark_node
6058 || TREE_TYPE (init) == error_mark_node)
6059 return error_mark_node;
6061 STRIP_TYPE_NOPS (inside_init);
6063 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6065 semantic_type = TREE_TYPE (inside_init);
6066 inside_init = TREE_OPERAND (inside_init, 0);
6068 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6069 inside_init = decl_constant_value_for_optimization (inside_init);
6071 /* Initialization of an array of chars from a string constant
6072 optionally enclosed in braces. */
6074 if (code == ARRAY_TYPE && inside_init
6075 && TREE_CODE (inside_init) == STRING_CST)
6077 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6078 /* Note that an array could be both an array of character type
6079 and an array of wchar_t if wchar_t is signed char or unsigned
6081 bool char_array = (typ1 == char_type_node
6082 || typ1 == signed_char_type_node
6083 || typ1 == unsigned_char_type_node);
6084 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6085 bool char16_array = !!comptypes (typ1, char16_type_node);
6086 bool char32_array = !!comptypes (typ1, char32_type_node);
6088 if (char_array || wchar_array || char16_array || char32_array)
6091 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6092 expr.value = inside_init;
6093 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6094 expr.original_type = NULL;
6095 maybe_warn_string_init (type, expr);
6097 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6098 pedwarn_init (init_loc, OPT_pedantic,
6099 "initialization of a flexible array member");
6101 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6102 TYPE_MAIN_VARIANT (type)))
6107 if (typ2 != char_type_node)
6109 error_init ("char-array initialized from wide string");
6110 return error_mark_node;
6115 if (typ2 == char_type_node)
6117 error_init ("wide character array initialized from non-wide "
6119 return error_mark_node;
6121 else if (!comptypes(typ1, typ2))
6123 error_init ("wide character array initialized from "
6124 "incompatible wide string");
6125 return error_mark_node;
6129 TREE_TYPE (inside_init) = type;
6130 if (TYPE_DOMAIN (type) != 0
6131 && TYPE_SIZE (type) != 0
6132 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6134 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6136 /* Subtract the size of a single (possibly wide) character
6137 because it's ok to ignore the terminating null char
6138 that is counted in the length of the constant. */
6139 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6141 - (TYPE_PRECISION (typ1)
6143 pedwarn_init (init_loc, 0,
6144 ("initializer-string for array of chars "
6146 else if (warn_cxx_compat
6147 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6148 warning_at (init_loc, OPT_Wc___compat,
6149 ("initializer-string for array chars "
6150 "is too long for C++"));
6155 else if (INTEGRAL_TYPE_P (typ1))
6157 error_init ("array of inappropriate type initialized "
6158 "from string constant");
6159 return error_mark_node;
6163 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6164 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6165 below and handle as a constructor. */
6166 if (code == VECTOR_TYPE
6167 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6168 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6169 && TREE_CONSTANT (inside_init))
6171 if (TREE_CODE (inside_init) == VECTOR_CST
6172 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6173 TYPE_MAIN_VARIANT (type)))
6176 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6178 unsigned HOST_WIDE_INT ix;
6180 bool constant_p = true;
6182 /* Iterate through elements and check if all constructor
6183 elements are *_CSTs. */
6184 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6185 if (!CONSTANT_CLASS_P (value))
6192 return build_vector_from_ctor (type,
6193 CONSTRUCTOR_ELTS (inside_init));
6197 if (warn_sequence_point)
6198 verify_sequence_points (inside_init);
6200 /* Any type can be initialized
6201 from an expression of the same type, optionally with braces. */
6203 if (inside_init && TREE_TYPE (inside_init) != 0
6204 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6205 TYPE_MAIN_VARIANT (type))
6206 || (code == ARRAY_TYPE
6207 && comptypes (TREE_TYPE (inside_init), type))
6208 || (code == VECTOR_TYPE
6209 && comptypes (TREE_TYPE (inside_init), type))
6210 || (code == POINTER_TYPE
6211 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6212 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6213 TREE_TYPE (type)))))
6215 if (code == POINTER_TYPE)
6217 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6219 if (TREE_CODE (inside_init) == STRING_CST
6220 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6221 inside_init = array_to_pointer_conversion
6222 (init_loc, inside_init);
6225 error_init ("invalid use of non-lvalue array");
6226 return error_mark_node;
6231 if (code == VECTOR_TYPE)
6232 /* Although the types are compatible, we may require a
6234 inside_init = convert (type, inside_init);
6236 if (require_constant
6237 && (code == VECTOR_TYPE || !flag_isoc99)
6238 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6240 /* As an extension, allow initializing objects with static storage
6241 duration with compound literals (which are then treated just as
6242 the brace enclosed list they contain). Also allow this for
6243 vectors, as we can only assign them with compound literals. */
6244 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6245 inside_init = DECL_INITIAL (decl);
6248 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6249 && TREE_CODE (inside_init) != CONSTRUCTOR)
6251 error_init ("array initialized from non-constant array expression");
6252 return error_mark_node;
6255 /* Compound expressions can only occur here if -pedantic or
6256 -pedantic-errors is specified. In the later case, we always want
6257 an error. In the former case, we simply want a warning. */
6258 if (require_constant && pedantic
6259 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6262 = valid_compound_expr_initializer (inside_init,
6263 TREE_TYPE (inside_init));
6264 if (inside_init == error_mark_node)
6265 error_init ("initializer element is not constant");
6267 pedwarn_init (init_loc, OPT_pedantic,
6268 "initializer element is not constant");
6269 if (flag_pedantic_errors)
6270 inside_init = error_mark_node;
6272 else if (require_constant
6273 && !initializer_constant_valid_p (inside_init,
6274 TREE_TYPE (inside_init)))
6276 error_init ("initializer element is not constant");
6277 inside_init = error_mark_node;
6279 else if (require_constant && !maybe_const)
6280 pedwarn_init (init_loc, 0,
6281 "initializer element is not a constant expression");
6283 /* Added to enable additional -Wmissing-format-attribute warnings. */
6284 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6285 inside_init = convert_for_assignment (init_loc, type, inside_init,
6287 ic_init, null_pointer_constant,
6288 NULL_TREE, NULL_TREE, 0);
6292 /* Handle scalar types, including conversions. */
6294 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6295 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6296 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6298 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6299 && (TREE_CODE (init) == STRING_CST
6300 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6301 inside_init = init = array_to_pointer_conversion (init_loc, init);
6303 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6306 = convert_for_assignment (init_loc, type, inside_init, origtype,
6307 ic_init, null_pointer_constant,
6308 NULL_TREE, NULL_TREE, 0);
6310 /* Check to see if we have already given an error message. */
6311 if (inside_init == error_mark_node)
6313 else if (require_constant && !TREE_CONSTANT (inside_init))
6315 error_init ("initializer element is not constant");
6316 inside_init = error_mark_node;
6318 else if (require_constant
6319 && !initializer_constant_valid_p (inside_init,
6320 TREE_TYPE (inside_init)))
6322 error_init ("initializer element is not computable at load time");
6323 inside_init = error_mark_node;
6325 else if (require_constant && !maybe_const)
6326 pedwarn_init (init_loc, 0,
6327 "initializer element is not a constant expression");
6332 /* Come here only for records and arrays. */
6334 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6336 error_init ("variable-sized object may not be initialized");
6337 return error_mark_node;
6340 error_init ("invalid initializer");
6341 return error_mark_node;
6344 /* Handle initializers that use braces. */
6346 /* Type of object we are accumulating a constructor for.
6347 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6348 static tree constructor_type;
6350 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6352 static tree constructor_fields;
6354 /* For an ARRAY_TYPE, this is the specified index
6355 at which to store the next element we get. */
6356 static tree constructor_index;
6358 /* For an ARRAY_TYPE, this is the maximum index. */
6359 static tree constructor_max_index;
6361 /* For a RECORD_TYPE, this is the first field not yet written out. */
6362 static tree constructor_unfilled_fields;
6364 /* For an ARRAY_TYPE, this is the index of the first element
6365 not yet written out. */
6366 static tree constructor_unfilled_index;
6368 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6369 This is so we can generate gaps between fields, when appropriate. */
6370 static tree constructor_bit_index;
6372 /* If we are saving up the elements rather than allocating them,
6373 this is the list of elements so far (in reverse order,
6374 most recent first). */
6375 static VEC(constructor_elt,gc) *constructor_elements;
6377 /* 1 if constructor should be incrementally stored into a constructor chain,
6378 0 if all the elements should be kept in AVL tree. */
6379 static int constructor_incremental;
6381 /* 1 if so far this constructor's elements are all compile-time constants. */
6382 static int constructor_constant;
6384 /* 1 if so far this constructor's elements are all valid address constants. */
6385 static int constructor_simple;
6387 /* 1 if this constructor has an element that cannot be part of a
6388 constant expression. */
6389 static int constructor_nonconst;
6391 /* 1 if this constructor is erroneous so far. */
6392 static int constructor_erroneous;
6394 /* Structure for managing pending initializer elements, organized as an
6399 struct init_node *left, *right;
6400 struct init_node *parent;
6407 /* Tree of pending elements at this constructor level.
6408 These are elements encountered out of order
6409 which belong at places we haven't reached yet in actually
6411 Will never hold tree nodes across GC runs. */
6412 static struct init_node *constructor_pending_elts;
6414 /* The SPELLING_DEPTH of this constructor. */
6415 static int constructor_depth;
6417 /* DECL node for which an initializer is being read.
6418 0 means we are reading a constructor expression
6419 such as (struct foo) {...}. */
6420 static tree constructor_decl;
6422 /* Nonzero if this is an initializer for a top-level decl. */
6423 static int constructor_top_level;
6425 /* Nonzero if there were any member designators in this initializer. */
6426 static int constructor_designated;
6428 /* Nesting depth of designator list. */
6429 static int designator_depth;
6431 /* Nonzero if there were diagnosed errors in this designator list. */
6432 static int designator_erroneous;
6435 /* This stack has a level for each implicit or explicit level of
6436 structuring in the initializer, including the outermost one. It
6437 saves the values of most of the variables above. */
6439 struct constructor_range_stack;
6441 struct constructor_stack
6443 struct constructor_stack *next;
6448 tree unfilled_index;
6449 tree unfilled_fields;
6451 VEC(constructor_elt,gc) *elements;
6452 struct init_node *pending_elts;
6455 /* If value nonzero, this value should replace the entire
6456 constructor at this level. */
6457 struct c_expr replacement_value;
6458 struct constructor_range_stack *range_stack;
6469 static struct constructor_stack *constructor_stack;
6471 /* This stack represents designators from some range designator up to
6472 the last designator in the list. */
6474 struct constructor_range_stack
6476 struct constructor_range_stack *next, *prev;
6477 struct constructor_stack *stack;
6484 static struct constructor_range_stack *constructor_range_stack;
6486 /* This stack records separate initializers that are nested.
6487 Nested initializers can't happen in ANSI C, but GNU C allows them
6488 in cases like { ... (struct foo) { ... } ... }. */
6490 struct initializer_stack
6492 struct initializer_stack *next;
6494 struct constructor_stack *constructor_stack;
6495 struct constructor_range_stack *constructor_range_stack;
6496 VEC(constructor_elt,gc) *elements;
6497 struct spelling *spelling;
6498 struct spelling *spelling_base;
6501 char require_constant_value;
6502 char require_constant_elements;
6505 static struct initializer_stack *initializer_stack;
6507 /* Prepare to parse and output the initializer for variable DECL. */
6510 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6513 struct initializer_stack *p = XNEW (struct initializer_stack);
6515 p->decl = constructor_decl;
6516 p->require_constant_value = require_constant_value;
6517 p->require_constant_elements = require_constant_elements;
6518 p->constructor_stack = constructor_stack;
6519 p->constructor_range_stack = constructor_range_stack;
6520 p->elements = constructor_elements;
6521 p->spelling = spelling;
6522 p->spelling_base = spelling_base;
6523 p->spelling_size = spelling_size;
6524 p->top_level = constructor_top_level;
6525 p->next = initializer_stack;
6526 initializer_stack = p;
6528 constructor_decl = decl;
6529 constructor_designated = 0;
6530 constructor_top_level = top_level;
6532 if (decl != 0 && decl != error_mark_node)
6534 require_constant_value = TREE_STATIC (decl);
6535 require_constant_elements
6536 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6537 /* For a scalar, you can always use any value to initialize,
6538 even within braces. */
6539 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6540 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6541 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6542 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6543 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6547 require_constant_value = 0;
6548 require_constant_elements = 0;
6549 locus = _("(anonymous)");
6552 constructor_stack = 0;
6553 constructor_range_stack = 0;
6555 missing_braces_mentioned = 0;
6559 RESTORE_SPELLING_DEPTH (0);
6562 push_string (locus);
6568 struct initializer_stack *p = initializer_stack;
6570 /* Free the whole constructor stack of this initializer. */
6571 while (constructor_stack)
6573 struct constructor_stack *q = constructor_stack;
6574 constructor_stack = q->next;
6578 gcc_assert (!constructor_range_stack);
6580 /* Pop back to the data of the outer initializer (if any). */
6581 free (spelling_base);
6583 constructor_decl = p->decl;
6584 require_constant_value = p->require_constant_value;
6585 require_constant_elements = p->require_constant_elements;
6586 constructor_stack = p->constructor_stack;
6587 constructor_range_stack = p->constructor_range_stack;
6588 constructor_elements = p->elements;
6589 spelling = p->spelling;
6590 spelling_base = p->spelling_base;
6591 spelling_size = p->spelling_size;
6592 constructor_top_level = p->top_level;
6593 initializer_stack = p->next;
6597 /* Call here when we see the initializer is surrounded by braces.
6598 This is instead of a call to push_init_level;
6599 it is matched by a call to pop_init_level.
6601 TYPE is the type to initialize, for a constructor expression.
6602 For an initializer for a decl, TYPE is zero. */
6605 really_start_incremental_init (tree type)
6607 struct constructor_stack *p = XNEW (struct constructor_stack);
6610 type = TREE_TYPE (constructor_decl);
6612 if (TREE_CODE (type) == VECTOR_TYPE
6613 && TYPE_VECTOR_OPAQUE (type))
6614 error ("opaque vector types cannot be initialized");
6616 p->type = constructor_type;
6617 p->fields = constructor_fields;
6618 p->index = constructor_index;
6619 p->max_index = constructor_max_index;
6620 p->unfilled_index = constructor_unfilled_index;
6621 p->unfilled_fields = constructor_unfilled_fields;
6622 p->bit_index = constructor_bit_index;
6623 p->elements = constructor_elements;
6624 p->constant = constructor_constant;
6625 p->simple = constructor_simple;
6626 p->nonconst = constructor_nonconst;
6627 p->erroneous = constructor_erroneous;
6628 p->pending_elts = constructor_pending_elts;
6629 p->depth = constructor_depth;
6630 p->replacement_value.value = 0;
6631 p->replacement_value.original_code = ERROR_MARK;
6632 p->replacement_value.original_type = NULL;
6636 p->incremental = constructor_incremental;
6637 p->designated = constructor_designated;
6639 constructor_stack = p;
6641 constructor_constant = 1;
6642 constructor_simple = 1;
6643 constructor_nonconst = 0;
6644 constructor_depth = SPELLING_DEPTH ();
6645 constructor_elements = 0;
6646 constructor_pending_elts = 0;
6647 constructor_type = type;
6648 constructor_incremental = 1;
6649 constructor_designated = 0;
6650 designator_depth = 0;
6651 designator_erroneous = 0;
6653 if (TREE_CODE (constructor_type) == RECORD_TYPE
6654 || TREE_CODE (constructor_type) == UNION_TYPE)
6656 constructor_fields = TYPE_FIELDS (constructor_type);
6657 /* Skip any nameless bit fields at the beginning. */
6658 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6659 && DECL_NAME (constructor_fields) == 0)
6660 constructor_fields = DECL_CHAIN (constructor_fields);
6662 constructor_unfilled_fields = constructor_fields;
6663 constructor_bit_index = bitsize_zero_node;
6665 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6667 if (TYPE_DOMAIN (constructor_type))
6669 constructor_max_index
6670 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6672 /* Detect non-empty initializations of zero-length arrays. */
6673 if (constructor_max_index == NULL_TREE
6674 && TYPE_SIZE (constructor_type))
6675 constructor_max_index = integer_minus_one_node;
6677 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6678 to initialize VLAs will cause a proper error; avoid tree
6679 checking errors as well by setting a safe value. */
6680 if (constructor_max_index
6681 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6682 constructor_max_index = integer_minus_one_node;
6685 = convert (bitsizetype,
6686 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6690 constructor_index = bitsize_zero_node;
6691 constructor_max_index = NULL_TREE;
6694 constructor_unfilled_index = constructor_index;
6696 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6698 /* Vectors are like simple fixed-size arrays. */
6699 constructor_max_index =
6700 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6701 constructor_index = bitsize_zero_node;
6702 constructor_unfilled_index = constructor_index;
6706 /* Handle the case of int x = {5}; */
6707 constructor_fields = constructor_type;
6708 constructor_unfilled_fields = constructor_type;
6712 /* Push down into a subobject, for initialization.
6713 If this is for an explicit set of braces, IMPLICIT is 0.
6714 If it is because the next element belongs at a lower level,
6715 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6718 push_init_level (int implicit, struct obstack * braced_init_obstack)
6720 struct constructor_stack *p;
6721 tree value = NULL_TREE;
6723 /* If we've exhausted any levels that didn't have braces,
6724 pop them now. If implicit == 1, this will have been done in
6725 process_init_element; do not repeat it here because in the case
6726 of excess initializers for an empty aggregate this leads to an
6727 infinite cycle of popping a level and immediately recreating
6731 while (constructor_stack->implicit)
6733 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6734 || TREE_CODE (constructor_type) == UNION_TYPE)
6735 && constructor_fields == 0)
6736 process_init_element (pop_init_level (1, braced_init_obstack),
6737 true, braced_init_obstack);
6738 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6739 && constructor_max_index
6740 && tree_int_cst_lt (constructor_max_index,
6742 process_init_element (pop_init_level (1, braced_init_obstack),
6743 true, braced_init_obstack);
6749 /* Unless this is an explicit brace, we need to preserve previous
6753 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6754 || TREE_CODE (constructor_type) == UNION_TYPE)
6755 && constructor_fields)
6756 value = find_init_member (constructor_fields, braced_init_obstack);
6757 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6758 value = find_init_member (constructor_index, braced_init_obstack);
6761 p = XNEW (struct constructor_stack);
6762 p->type = constructor_type;
6763 p->fields = constructor_fields;
6764 p->index = constructor_index;
6765 p->max_index = constructor_max_index;
6766 p->unfilled_index = constructor_unfilled_index;
6767 p->unfilled_fields = constructor_unfilled_fields;
6768 p->bit_index = constructor_bit_index;
6769 p->elements = constructor_elements;
6770 p->constant = constructor_constant;
6771 p->simple = constructor_simple;
6772 p->nonconst = constructor_nonconst;
6773 p->erroneous = constructor_erroneous;
6774 p->pending_elts = constructor_pending_elts;
6775 p->depth = constructor_depth;
6776 p->replacement_value.value = 0;
6777 p->replacement_value.original_code = ERROR_MARK;
6778 p->replacement_value.original_type = NULL;
6779 p->implicit = implicit;
6781 p->incremental = constructor_incremental;
6782 p->designated = constructor_designated;
6783 p->next = constructor_stack;
6785 constructor_stack = p;
6787 constructor_constant = 1;
6788 constructor_simple = 1;
6789 constructor_nonconst = 0;
6790 constructor_depth = SPELLING_DEPTH ();
6791 constructor_elements = 0;
6792 constructor_incremental = 1;
6793 constructor_designated = 0;
6794 constructor_pending_elts = 0;
6797 p->range_stack = constructor_range_stack;
6798 constructor_range_stack = 0;
6799 designator_depth = 0;
6800 designator_erroneous = 0;
6803 /* Don't die if an entire brace-pair level is superfluous
6804 in the containing level. */
6805 if (constructor_type == 0)
6807 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6808 || TREE_CODE (constructor_type) == UNION_TYPE)
6810 /* Don't die if there are extra init elts at the end. */
6811 if (constructor_fields == 0)
6812 constructor_type = 0;
6815 constructor_type = TREE_TYPE (constructor_fields);
6816 push_member_name (constructor_fields);
6817 constructor_depth++;
6820 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6822 constructor_type = TREE_TYPE (constructor_type);
6823 push_array_bounds (tree_low_cst (constructor_index, 1));
6824 constructor_depth++;
6827 if (constructor_type == 0)
6829 error_init ("extra brace group at end of initializer");
6830 constructor_fields = 0;
6831 constructor_unfilled_fields = 0;
6835 if (value && TREE_CODE (value) == CONSTRUCTOR)
6837 constructor_constant = TREE_CONSTANT (value);
6838 constructor_simple = TREE_STATIC (value);
6839 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6840 constructor_elements = CONSTRUCTOR_ELTS (value);
6841 if (!VEC_empty (constructor_elt, constructor_elements)
6842 && (TREE_CODE (constructor_type) == RECORD_TYPE
6843 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6844 set_nonincremental_init (braced_init_obstack);
6847 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6849 missing_braces_mentioned = 1;
6850 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6853 if (TREE_CODE (constructor_type) == RECORD_TYPE
6854 || TREE_CODE (constructor_type) == UNION_TYPE)
6856 constructor_fields = TYPE_FIELDS (constructor_type);
6857 /* Skip any nameless bit fields at the beginning. */
6858 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6859 && DECL_NAME (constructor_fields) == 0)
6860 constructor_fields = DECL_CHAIN (constructor_fields);
6862 constructor_unfilled_fields = constructor_fields;
6863 constructor_bit_index = bitsize_zero_node;
6865 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6867 /* Vectors are like simple fixed-size arrays. */
6868 constructor_max_index =
6869 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6870 constructor_index = convert (bitsizetype, integer_zero_node);
6871 constructor_unfilled_index = constructor_index;
6873 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6875 if (TYPE_DOMAIN (constructor_type))
6877 constructor_max_index
6878 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6880 /* Detect non-empty initializations of zero-length arrays. */
6881 if (constructor_max_index == NULL_TREE
6882 && TYPE_SIZE (constructor_type))
6883 constructor_max_index = integer_minus_one_node;
6885 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6886 to initialize VLAs will cause a proper error; avoid tree
6887 checking errors as well by setting a safe value. */
6888 if (constructor_max_index
6889 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6890 constructor_max_index = integer_minus_one_node;
6893 = convert (bitsizetype,
6894 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6897 constructor_index = bitsize_zero_node;
6899 constructor_unfilled_index = constructor_index;
6900 if (value && TREE_CODE (value) == STRING_CST)
6902 /* We need to split the char/wchar array into individual
6903 characters, so that we don't have to special case it
6905 set_nonincremental_init_from_string (value, braced_init_obstack);
6910 if (constructor_type != error_mark_node)
6911 warning_init (0, "braces around scalar initializer");
6912 constructor_fields = constructor_type;
6913 constructor_unfilled_fields = constructor_type;
6917 /* At the end of an implicit or explicit brace level,
6918 finish up that level of constructor. If a single expression
6919 with redundant braces initialized that level, return the
6920 c_expr structure for that expression. Otherwise, the original_code
6921 element is set to ERROR_MARK.
6922 If we were outputting the elements as they are read, return 0 as the value
6923 from inner levels (process_init_element ignores that),
6924 but return error_mark_node as the value from the outermost level
6925 (that's what we want to put in DECL_INITIAL).
6926 Otherwise, return a CONSTRUCTOR expression as the value. */
6929 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6931 struct constructor_stack *p;
6934 ret.original_code = ERROR_MARK;
6935 ret.original_type = NULL;
6939 /* When we come to an explicit close brace,
6940 pop any inner levels that didn't have explicit braces. */
6941 while (constructor_stack->implicit)
6943 process_init_element (pop_init_level (1, braced_init_obstack),
6944 true, braced_init_obstack);
6946 gcc_assert (!constructor_range_stack);
6949 /* Now output all pending elements. */
6950 constructor_incremental = 1;
6951 output_pending_init_elements (1, braced_init_obstack);
6953 p = constructor_stack;
6955 /* Error for initializing a flexible array member, or a zero-length
6956 array member in an inappropriate context. */
6957 if (constructor_type && constructor_fields
6958 && TREE_CODE (constructor_type) == ARRAY_TYPE
6959 && TYPE_DOMAIN (constructor_type)
6960 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6962 /* Silently discard empty initializations. The parser will
6963 already have pedwarned for empty brackets. */
6964 if (integer_zerop (constructor_unfilled_index))
6965 constructor_type = NULL_TREE;
6968 gcc_assert (!TYPE_SIZE (constructor_type));
6970 if (constructor_depth > 2)
6971 error_init ("initialization of flexible array member in a nested context");
6973 pedwarn_init (input_location, OPT_pedantic,
6974 "initialization of a flexible array member");
6976 /* We have already issued an error message for the existence
6977 of a flexible array member not at the end of the structure.
6978 Discard the initializer so that we do not die later. */
6979 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6980 constructor_type = NULL_TREE;
6984 /* Warn when some struct elements are implicitly initialized to zero. */
6985 if (warn_missing_field_initializers
6987 && TREE_CODE (constructor_type) == RECORD_TYPE
6988 && constructor_unfilled_fields)
6990 /* Do not warn for flexible array members or zero-length arrays. */
6991 while (constructor_unfilled_fields
6992 && (!DECL_SIZE (constructor_unfilled_fields)
6993 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6994 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6996 /* Do not warn if this level of the initializer uses member
6997 designators; it is likely to be deliberate. */
6998 if (constructor_unfilled_fields && !constructor_designated)
7000 push_member_name (constructor_unfilled_fields);
7001 warning_init (OPT_Wmissing_field_initializers,
7002 "missing initializer");
7003 RESTORE_SPELLING_DEPTH (constructor_depth);
7007 /* Pad out the end of the structure. */
7008 if (p->replacement_value.value)
7009 /* If this closes a superfluous brace pair,
7010 just pass out the element between them. */
7011 ret = p->replacement_value;
7012 else if (constructor_type == 0)
7014 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7015 && TREE_CODE (constructor_type) != UNION_TYPE
7016 && TREE_CODE (constructor_type) != ARRAY_TYPE
7017 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7019 /* A nonincremental scalar initializer--just return
7020 the element, after verifying there is just one. */
7021 if (VEC_empty (constructor_elt,constructor_elements))
7023 if (!constructor_erroneous)
7024 error_init ("empty scalar initializer");
7025 ret.value = error_mark_node;
7027 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7029 error_init ("extra elements in scalar initializer");
7030 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7033 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7037 if (constructor_erroneous)
7038 ret.value = error_mark_node;
7041 ret.value = build_constructor (constructor_type,
7042 constructor_elements);
7043 if (constructor_constant)
7044 TREE_CONSTANT (ret.value) = 1;
7045 if (constructor_constant && constructor_simple)
7046 TREE_STATIC (ret.value) = 1;
7047 if (constructor_nonconst)
7048 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7052 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7054 if (constructor_nonconst)
7055 ret.original_code = C_MAYBE_CONST_EXPR;
7056 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7057 ret.original_code = ERROR_MARK;
7060 constructor_type = p->type;
7061 constructor_fields = p->fields;
7062 constructor_index = p->index;
7063 constructor_max_index = p->max_index;
7064 constructor_unfilled_index = p->unfilled_index;
7065 constructor_unfilled_fields = p->unfilled_fields;
7066 constructor_bit_index = p->bit_index;
7067 constructor_elements = p->elements;
7068 constructor_constant = p->constant;
7069 constructor_simple = p->simple;
7070 constructor_nonconst = p->nonconst;
7071 constructor_erroneous = p->erroneous;
7072 constructor_incremental = p->incremental;
7073 constructor_designated = p->designated;
7074 constructor_pending_elts = p->pending_elts;
7075 constructor_depth = p->depth;
7077 constructor_range_stack = p->range_stack;
7078 RESTORE_SPELLING_DEPTH (constructor_depth);
7080 constructor_stack = p->next;
7083 if (ret.value == 0 && constructor_stack == 0)
7084 ret.value = error_mark_node;
7088 /* Common handling for both array range and field name designators.
7089 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7092 set_designator (int array, struct obstack * braced_init_obstack)
7095 enum tree_code subcode;
7097 /* Don't die if an entire brace-pair level is superfluous
7098 in the containing level. */
7099 if (constructor_type == 0)
7102 /* If there were errors in this designator list already, bail out
7104 if (designator_erroneous)
7107 if (!designator_depth)
7109 gcc_assert (!constructor_range_stack);
7111 /* Designator list starts at the level of closest explicit
7113 while (constructor_stack->implicit)
7115 process_init_element (pop_init_level (1, braced_init_obstack),
7116 true, braced_init_obstack);
7118 constructor_designated = 1;
7122 switch (TREE_CODE (constructor_type))
7126 subtype = TREE_TYPE (constructor_fields);
7127 if (subtype != error_mark_node)
7128 subtype = TYPE_MAIN_VARIANT (subtype);
7131 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7137 subcode = TREE_CODE (subtype);
7138 if (array && subcode != ARRAY_TYPE)
7140 error_init ("array index in non-array initializer");
7143 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7145 error_init ("field name not in record or union initializer");
7149 constructor_designated = 1;
7150 push_init_level (2, braced_init_obstack);
7154 /* If there are range designators in designator list, push a new designator
7155 to constructor_range_stack. RANGE_END is end of such stack range or
7156 NULL_TREE if there is no range designator at this level. */
7159 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7161 struct constructor_range_stack *p;
7163 p = (struct constructor_range_stack *)
7164 obstack_alloc (braced_init_obstack,
7165 sizeof (struct constructor_range_stack));
7166 p->prev = constructor_range_stack;
7168 p->fields = constructor_fields;
7169 p->range_start = constructor_index;
7170 p->index = constructor_index;
7171 p->stack = constructor_stack;
7172 p->range_end = range_end;
7173 if (constructor_range_stack)
7174 constructor_range_stack->next = p;
7175 constructor_range_stack = p;
7178 /* Within an array initializer, specify the next index to be initialized.
7179 FIRST is that index. If LAST is nonzero, then initialize a range
7180 of indices, running from FIRST through LAST. */
7183 set_init_index (tree first, tree last,
7184 struct obstack * braced_init_obstack)
7186 if (set_designator (1, braced_init_obstack))
7189 designator_erroneous = 1;
7191 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7192 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7194 error_init ("array index in initializer not of integer type");
7198 if (TREE_CODE (first) != INTEGER_CST)
7200 first = c_fully_fold (first, false, NULL);
7201 if (TREE_CODE (first) == INTEGER_CST)
7202 pedwarn_init (input_location, OPT_pedantic,
7203 "array index in initializer is not "
7204 "an integer constant expression");
7207 if (last && TREE_CODE (last) != INTEGER_CST)
7209 last = c_fully_fold (last, false, NULL);
7210 if (TREE_CODE (last) == INTEGER_CST)
7211 pedwarn_init (input_location, OPT_pedantic,
7212 "array index in initializer is not "
7213 "an integer constant expression");
7216 if (TREE_CODE (first) != INTEGER_CST)
7217 error_init ("nonconstant array index in initializer");
7218 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7219 error_init ("nonconstant array index in initializer");
7220 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7221 error_init ("array index in non-array initializer");
7222 else if (tree_int_cst_sgn (first) == -1)
7223 error_init ("array index in initializer exceeds array bounds");
7224 else if (constructor_max_index
7225 && tree_int_cst_lt (constructor_max_index, first))
7226 error_init ("array index in initializer exceeds array bounds");
7229 constant_expression_warning (first);
7231 constant_expression_warning (last);
7232 constructor_index = convert (bitsizetype, first);
7236 if (tree_int_cst_equal (first, last))
7238 else if (tree_int_cst_lt (last, first))
7240 error_init ("empty index range in initializer");
7245 last = convert (bitsizetype, last);
7246 if (constructor_max_index != 0
7247 && tree_int_cst_lt (constructor_max_index, last))
7249 error_init ("array index range in initializer exceeds array bounds");
7256 designator_erroneous = 0;
7257 if (constructor_range_stack || last)
7258 push_range_stack (last, braced_init_obstack);
7262 /* Within a struct initializer, specify the next field to be initialized. */
7265 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7269 if (set_designator (0, braced_init_obstack))
7272 designator_erroneous = 1;
7274 if (TREE_CODE (constructor_type) != RECORD_TYPE
7275 && TREE_CODE (constructor_type) != UNION_TYPE)
7277 error_init ("field name not in record or union initializer");
7281 field = lookup_field (constructor_type, fieldname);
7284 error ("unknown field %qE specified in initializer", fieldname);
7288 constructor_fields = TREE_VALUE (field);
7290 designator_erroneous = 0;
7291 if (constructor_range_stack)
7292 push_range_stack (NULL_TREE, braced_init_obstack);
7293 field = TREE_CHAIN (field);
7296 if (set_designator (0, braced_init_obstack))
7300 while (field != NULL_TREE);
7303 /* Add a new initializer to the tree of pending initializers. PURPOSE
7304 identifies the initializer, either array index or field in a structure.
7305 VALUE is the value of that index or field. If ORIGTYPE is not
7306 NULL_TREE, it is the original type of VALUE.
7308 IMPLICIT is true if value comes from pop_init_level (1),
7309 the new initializer has been merged with the existing one
7310 and thus no warnings should be emitted about overriding an
7311 existing initializer. */
7314 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7315 struct obstack * braced_init_obstack)
7317 struct init_node *p, **q, *r;
7319 q = &constructor_pending_elts;
7322 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7327 if (tree_int_cst_lt (purpose, p->purpose))
7329 else if (tree_int_cst_lt (p->purpose, purpose))
7335 if (TREE_SIDE_EFFECTS (p->value))
7336 warning_init (0, "initialized field with side-effects overwritten");
7337 else if (warn_override_init)
7338 warning_init (OPT_Woverride_init, "initialized field overwritten");
7341 p->origtype = origtype;
7350 bitpos = bit_position (purpose);
7354 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7356 else if (p->purpose != purpose)
7362 if (TREE_SIDE_EFFECTS (p->value))
7363 warning_init (0, "initialized field with side-effects overwritten");
7364 else if (warn_override_init)
7365 warning_init (OPT_Woverride_init, "initialized field overwritten");
7368 p->origtype = origtype;
7374 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7375 sizeof (struct init_node));
7376 r->purpose = purpose;
7378 r->origtype = origtype;
7388 struct init_node *s;
7392 if (p->balance == 0)
7394 else if (p->balance < 0)
7401 p->left->parent = p;
7418 constructor_pending_elts = r;
7423 struct init_node *t = r->right;
7427 r->right->parent = r;
7432 p->left->parent = p;
7435 p->balance = t->balance < 0;
7436 r->balance = -(t->balance > 0);
7451 constructor_pending_elts = t;
7457 /* p->balance == +1; growth of left side balances the node. */
7462 else /* r == p->right */
7464 if (p->balance == 0)
7465 /* Growth propagation from right side. */
7467 else if (p->balance > 0)
7474 p->right->parent = p;
7491 constructor_pending_elts = r;
7493 else /* r->balance == -1 */
7496 struct init_node *t = r->left;
7500 r->left->parent = r;
7505 p->right->parent = p;
7508 r->balance = (t->balance < 0);
7509 p->balance = -(t->balance > 0);
7524 constructor_pending_elts = t;
7530 /* p->balance == -1; growth of right side balances the node. */
7541 /* Build AVL tree from a sorted chain. */
7544 set_nonincremental_init (struct obstack * braced_init_obstack)
7546 unsigned HOST_WIDE_INT ix;
7549 if (TREE_CODE (constructor_type) != RECORD_TYPE
7550 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7553 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7555 add_pending_init (index, value, NULL_TREE, false,
7556 braced_init_obstack);
7558 constructor_elements = 0;
7559 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7561 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7562 /* Skip any nameless bit fields at the beginning. */
7563 while (constructor_unfilled_fields != 0
7564 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7565 && DECL_NAME (constructor_unfilled_fields) == 0)
7566 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7569 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7571 if (TYPE_DOMAIN (constructor_type))
7572 constructor_unfilled_index
7573 = convert (bitsizetype,
7574 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7576 constructor_unfilled_index = bitsize_zero_node;
7578 constructor_incremental = 0;
7581 /* Build AVL tree from a string constant. */
7584 set_nonincremental_init_from_string (tree str,
7585 struct obstack * braced_init_obstack)
7587 tree value, purpose, type;
7588 HOST_WIDE_INT val[2];
7589 const char *p, *end;
7590 int byte, wchar_bytes, charwidth, bitpos;
7592 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7594 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7595 charwidth = TYPE_PRECISION (char_type_node);
7596 type = TREE_TYPE (constructor_type);
7597 p = TREE_STRING_POINTER (str);
7598 end = p + TREE_STRING_LENGTH (str);
7600 for (purpose = bitsize_zero_node;
7601 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7602 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7604 if (wchar_bytes == 1)
7606 val[1] = (unsigned char) *p++;
7613 for (byte = 0; byte < wchar_bytes; byte++)
7615 if (BYTES_BIG_ENDIAN)
7616 bitpos = (wchar_bytes - byte - 1) * charwidth;
7618 bitpos = byte * charwidth;
7619 val[bitpos < HOST_BITS_PER_WIDE_INT]
7620 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7621 << (bitpos % HOST_BITS_PER_WIDE_INT);
7625 if (!TYPE_UNSIGNED (type))
7627 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7628 if (bitpos < HOST_BITS_PER_WIDE_INT)
7630 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7632 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7636 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7641 else if (val[0] & (((HOST_WIDE_INT) 1)
7642 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7643 val[0] |= ((HOST_WIDE_INT) -1)
7644 << (bitpos - HOST_BITS_PER_WIDE_INT);
7647 value = build_int_cst_wide (type, val[1], val[0]);
7648 add_pending_init (purpose, value, NULL_TREE, false,
7649 braced_init_obstack);
7652 constructor_incremental = 0;
7655 /* Return value of FIELD in pending initializer or zero if the field was
7656 not initialized yet. */
7659 find_init_member (tree field, struct obstack * braced_init_obstack)
7661 struct init_node *p;
7663 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7665 if (constructor_incremental
7666 && tree_int_cst_lt (field, constructor_unfilled_index))
7667 set_nonincremental_init (braced_init_obstack);
7669 p = constructor_pending_elts;
7672 if (tree_int_cst_lt (field, p->purpose))
7674 else if (tree_int_cst_lt (p->purpose, field))
7680 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7682 tree bitpos = bit_position (field);
7684 if (constructor_incremental
7685 && (!constructor_unfilled_fields
7686 || tree_int_cst_lt (bitpos,
7687 bit_position (constructor_unfilled_fields))))
7688 set_nonincremental_init (braced_init_obstack);
7690 p = constructor_pending_elts;
7693 if (field == p->purpose)
7695 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7701 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7703 if (!VEC_empty (constructor_elt, constructor_elements)
7704 && (VEC_last (constructor_elt, constructor_elements)->index
7706 return VEC_last (constructor_elt, constructor_elements)->value;
7711 /* "Output" the next constructor element.
7712 At top level, really output it to assembler code now.
7713 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7714 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7715 TYPE is the data type that the containing data type wants here.
7716 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7717 If VALUE is a string constant, STRICT_STRING is true if it is
7718 unparenthesized or we should not warn here for it being parenthesized.
7719 For other types of VALUE, STRICT_STRING is not used.
7721 PENDING if non-nil means output pending elements that belong
7722 right after this element. (PENDING is normally 1;
7723 it is 0 while outputting pending elements, to avoid recursion.)
7725 IMPLICIT is true if value comes from pop_init_level (1),
7726 the new initializer has been merged with the existing one
7727 and thus no warnings should be emitted about overriding an
7728 existing initializer. */
7731 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7732 tree field, int pending, bool implicit,
7733 struct obstack * braced_init_obstack)
7735 tree semantic_type = NULL_TREE;
7736 constructor_elt *celt;
7737 bool maybe_const = true;
7740 if (type == error_mark_node || value == error_mark_node)
7742 constructor_erroneous = 1;
7745 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7746 && (TREE_CODE (value) == STRING_CST
7747 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7748 && !(TREE_CODE (value) == STRING_CST
7749 && TREE_CODE (type) == ARRAY_TYPE
7750 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7751 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7752 TYPE_MAIN_VARIANT (type)))
7753 value = array_to_pointer_conversion (input_location, value);
7755 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7756 && require_constant_value && !flag_isoc99 && pending)
7758 /* As an extension, allow initializing objects with static storage
7759 duration with compound literals (which are then treated just as
7760 the brace enclosed list they contain). */
7761 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7762 value = DECL_INITIAL (decl);
7765 npc = null_pointer_constant_p (value);
7766 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7768 semantic_type = TREE_TYPE (value);
7769 value = TREE_OPERAND (value, 0);
7771 value = c_fully_fold (value, require_constant_value, &maybe_const);
7773 if (value == error_mark_node)
7774 constructor_erroneous = 1;
7775 else if (!TREE_CONSTANT (value))
7776 constructor_constant = 0;
7777 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7778 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7779 || TREE_CODE (constructor_type) == UNION_TYPE)
7780 && DECL_C_BIT_FIELD (field)
7781 && TREE_CODE (value) != INTEGER_CST))
7782 constructor_simple = 0;
7784 constructor_nonconst = 1;
7786 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7788 if (require_constant_value)
7790 error_init ("initializer element is not constant");
7791 value = error_mark_node;
7793 else if (require_constant_elements)
7794 pedwarn (input_location, 0,
7795 "initializer element is not computable at load time");
7797 else if (!maybe_const
7798 && (require_constant_value || require_constant_elements))
7799 pedwarn_init (input_location, 0,
7800 "initializer element is not a constant expression");
7802 /* Issue -Wc++-compat warnings about initializing a bitfield with
7805 && field != NULL_TREE
7806 && TREE_CODE (field) == FIELD_DECL
7807 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7808 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7809 != TYPE_MAIN_VARIANT (type))
7810 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7812 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7813 if (checktype != error_mark_node
7814 && (TYPE_MAIN_VARIANT (checktype)
7815 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7816 warning_init (OPT_Wc___compat,
7817 "enum conversion in initialization is invalid in C++");
7820 /* If this field is empty (and not at the end of structure),
7821 don't do anything other than checking the initializer. */
7823 && (TREE_TYPE (field) == error_mark_node
7824 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7825 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7826 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7827 || DECL_CHAIN (field)))))
7831 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7832 value = digest_init (input_location, type, value, origtype, npc,
7833 strict_string, require_constant_value);
7834 if (value == error_mark_node)
7836 constructor_erroneous = 1;
7839 if (require_constant_value || require_constant_elements)
7840 constant_expression_warning (value);
7842 /* If this element doesn't come next in sequence,
7843 put it on constructor_pending_elts. */
7844 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7845 && (!constructor_incremental
7846 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7848 if (constructor_incremental
7849 && tree_int_cst_lt (field, constructor_unfilled_index))
7850 set_nonincremental_init (braced_init_obstack);
7852 add_pending_init (field, value, origtype, implicit,
7853 braced_init_obstack);
7856 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7857 && (!constructor_incremental
7858 || field != constructor_unfilled_fields))
7860 /* We do this for records but not for unions. In a union,
7861 no matter which field is specified, it can be initialized
7862 right away since it starts at the beginning of the union. */
7863 if (constructor_incremental)
7865 if (!constructor_unfilled_fields)
7866 set_nonincremental_init (braced_init_obstack);
7869 tree bitpos, unfillpos;
7871 bitpos = bit_position (field);
7872 unfillpos = bit_position (constructor_unfilled_fields);
7874 if (tree_int_cst_lt (bitpos, unfillpos))
7875 set_nonincremental_init (braced_init_obstack);
7879 add_pending_init (field, value, origtype, implicit,
7880 braced_init_obstack);
7883 else if (TREE_CODE (constructor_type) == UNION_TYPE
7884 && !VEC_empty (constructor_elt, constructor_elements))
7888 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7889 constructor_elements)->value))
7891 "initialized field with side-effects overwritten");
7892 else if (warn_override_init)
7893 warning_init (OPT_Woverride_init, "initialized field overwritten");
7896 /* We can have just one union field set. */
7897 constructor_elements = 0;
7900 /* Otherwise, output this element either to
7901 constructor_elements or to the assembler file. */
7903 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7904 celt->index = field;
7905 celt->value = value;
7907 /* Advance the variable that indicates sequential elements output. */
7908 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7909 constructor_unfilled_index
7910 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7912 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7914 constructor_unfilled_fields
7915 = DECL_CHAIN (constructor_unfilled_fields);
7917 /* Skip any nameless bit fields. */
7918 while (constructor_unfilled_fields != 0
7919 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7920 && DECL_NAME (constructor_unfilled_fields) == 0)
7921 constructor_unfilled_fields =
7922 DECL_CHAIN (constructor_unfilled_fields);
7924 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7925 constructor_unfilled_fields = 0;
7927 /* Now output any pending elements which have become next. */
7929 output_pending_init_elements (0, braced_init_obstack);
7932 /* Output any pending elements which have become next.
7933 As we output elements, constructor_unfilled_{fields,index}
7934 advances, which may cause other elements to become next;
7935 if so, they too are output.
7937 If ALL is 0, we return when there are
7938 no more pending elements to output now.
7940 If ALL is 1, we output space as necessary so that
7941 we can output all the pending elements. */
7943 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7945 struct init_node *elt = constructor_pending_elts;
7950 /* Look through the whole pending tree.
7951 If we find an element that should be output now,
7952 output it. Otherwise, set NEXT to the element
7953 that comes first among those still pending. */
7958 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7960 if (tree_int_cst_equal (elt->purpose,
7961 constructor_unfilled_index))
7962 output_init_element (elt->value, elt->origtype, true,
7963 TREE_TYPE (constructor_type),
7964 constructor_unfilled_index, 0, false,
7965 braced_init_obstack);
7966 else if (tree_int_cst_lt (constructor_unfilled_index,
7969 /* Advance to the next smaller node. */
7974 /* We have reached the smallest node bigger than the
7975 current unfilled index. Fill the space first. */
7976 next = elt->purpose;
7982 /* Advance to the next bigger node. */
7987 /* We have reached the biggest node in a subtree. Find
7988 the parent of it, which is the next bigger node. */
7989 while (elt->parent && elt->parent->right == elt)
7992 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7995 next = elt->purpose;
8001 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8002 || TREE_CODE (constructor_type) == UNION_TYPE)
8004 tree ctor_unfilled_bitpos, elt_bitpos;
8006 /* If the current record is complete we are done. */
8007 if (constructor_unfilled_fields == 0)
8010 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8011 elt_bitpos = bit_position (elt->purpose);
8012 /* We can't compare fields here because there might be empty
8013 fields in between. */
8014 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8016 constructor_unfilled_fields = elt->purpose;
8017 output_init_element (elt->value, elt->origtype, true,
8018 TREE_TYPE (elt->purpose),
8019 elt->purpose, 0, false,
8020 braced_init_obstack);
8022 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8024 /* Advance to the next smaller node. */
8029 /* We have reached the smallest node bigger than the
8030 current unfilled field. Fill the space first. */
8031 next = elt->purpose;
8037 /* Advance to the next bigger node. */
8042 /* We have reached the biggest node in a subtree. Find
8043 the parent of it, which is the next bigger node. */
8044 while (elt->parent && elt->parent->right == elt)
8048 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8049 bit_position (elt->purpose))))
8051 next = elt->purpose;
8059 /* Ordinarily return, but not if we want to output all
8060 and there are elements left. */
8061 if (!(all && next != 0))
8064 /* If it's not incremental, just skip over the gap, so that after
8065 jumping to retry we will output the next successive element. */
8066 if (TREE_CODE (constructor_type) == RECORD_TYPE
8067 || TREE_CODE (constructor_type) == UNION_TYPE)
8068 constructor_unfilled_fields = next;
8069 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8070 constructor_unfilled_index = next;
8072 /* ELT now points to the node in the pending tree with the next
8073 initializer to output. */
8077 /* Add one non-braced element to the current constructor level.
8078 This adjusts the current position within the constructor's type.
8079 This may also start or terminate implicit levels
8080 to handle a partly-braced initializer.
8082 Once this has found the correct level for the new element,
8083 it calls output_init_element.
8085 IMPLICIT is true if value comes from pop_init_level (1),
8086 the new initializer has been merged with the existing one
8087 and thus no warnings should be emitted about overriding an
8088 existing initializer. */
8091 process_init_element (struct c_expr value, bool implicit,
8092 struct obstack * braced_init_obstack)
8094 tree orig_value = value.value;
8095 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8096 bool strict_string = value.original_code == STRING_CST;
8098 designator_depth = 0;
8099 designator_erroneous = 0;
8101 /* Handle superfluous braces around string cst as in
8102 char x[] = {"foo"}; */
8105 && TREE_CODE (constructor_type) == ARRAY_TYPE
8106 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8107 && integer_zerop (constructor_unfilled_index))
8109 if (constructor_stack->replacement_value.value)
8110 error_init ("excess elements in char array initializer");
8111 constructor_stack->replacement_value = value;
8115 if (constructor_stack->replacement_value.value != 0)
8117 error_init ("excess elements in struct initializer");
8121 /* Ignore elements of a brace group if it is entirely superfluous
8122 and has already been diagnosed. */
8123 if (constructor_type == 0)
8126 /* If we've exhausted any levels that didn't have braces,
8128 while (constructor_stack->implicit)
8130 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8131 || TREE_CODE (constructor_type) == UNION_TYPE)
8132 && constructor_fields == 0)
8133 process_init_element (pop_init_level (1, braced_init_obstack),
8134 true, braced_init_obstack);
8135 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8136 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8137 && (constructor_max_index == 0
8138 || tree_int_cst_lt (constructor_max_index,
8139 constructor_index)))
8140 process_init_element (pop_init_level (1, braced_init_obstack),
8141 true, braced_init_obstack);
8146 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8147 if (constructor_range_stack)
8149 /* If value is a compound literal and we'll be just using its
8150 content, don't put it into a SAVE_EXPR. */
8151 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8152 || !require_constant_value
8155 tree semantic_type = NULL_TREE;
8156 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8158 semantic_type = TREE_TYPE (value.value);
8159 value.value = TREE_OPERAND (value.value, 0);
8161 value.value = c_save_expr (value.value);
8163 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8170 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8173 enum tree_code fieldcode;
8175 if (constructor_fields == 0)
8177 pedwarn_init (input_location, 0,
8178 "excess elements in struct initializer");
8182 fieldtype = TREE_TYPE (constructor_fields);
8183 if (fieldtype != error_mark_node)
8184 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8185 fieldcode = TREE_CODE (fieldtype);
8187 /* Error for non-static initialization of a flexible array member. */
8188 if (fieldcode == ARRAY_TYPE
8189 && !require_constant_value
8190 && TYPE_SIZE (fieldtype) == NULL_TREE
8191 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8193 error_init ("non-static initialization of a flexible array member");
8197 /* Accept a string constant to initialize a subarray. */
8198 if (value.value != 0
8199 && fieldcode == ARRAY_TYPE
8200 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8202 value.value = orig_value;
8203 /* Otherwise, if we have come to a subaggregate,
8204 and we don't have an element of its type, push into it. */
8205 else if (value.value != 0
8206 && value.value != error_mark_node
8207 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8208 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8209 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8211 push_init_level (1, braced_init_obstack);
8217 push_member_name (constructor_fields);
8218 output_init_element (value.value, value.original_type,
8219 strict_string, fieldtype,
8220 constructor_fields, 1, implicit,
8221 braced_init_obstack);
8222 RESTORE_SPELLING_DEPTH (constructor_depth);
8225 /* Do the bookkeeping for an element that was
8226 directly output as a constructor. */
8228 /* For a record, keep track of end position of last field. */
8229 if (DECL_SIZE (constructor_fields))
8230 constructor_bit_index
8231 = size_binop_loc (input_location, PLUS_EXPR,
8232 bit_position (constructor_fields),
8233 DECL_SIZE (constructor_fields));
8235 /* If the current field was the first one not yet written out,
8236 it isn't now, so update. */
8237 if (constructor_unfilled_fields == constructor_fields)
8239 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8240 /* Skip any nameless bit fields. */
8241 while (constructor_unfilled_fields != 0
8242 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8243 && DECL_NAME (constructor_unfilled_fields) == 0)
8244 constructor_unfilled_fields =
8245 DECL_CHAIN (constructor_unfilled_fields);
8249 constructor_fields = DECL_CHAIN (constructor_fields);
8250 /* Skip any nameless bit fields at the beginning. */
8251 while (constructor_fields != 0
8252 && DECL_C_BIT_FIELD (constructor_fields)
8253 && DECL_NAME (constructor_fields) == 0)
8254 constructor_fields = DECL_CHAIN (constructor_fields);
8256 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8259 enum tree_code fieldcode;
8261 if (constructor_fields == 0)
8263 pedwarn_init (input_location, 0,
8264 "excess elements in union initializer");
8268 fieldtype = TREE_TYPE (constructor_fields);
8269 if (fieldtype != error_mark_node)
8270 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8271 fieldcode = TREE_CODE (fieldtype);
8273 /* Warn that traditional C rejects initialization of unions.
8274 We skip the warning if the value is zero. This is done
8275 under the assumption that the zero initializer in user
8276 code appears conditioned on e.g. __STDC__ to avoid
8277 "missing initializer" warnings and relies on default
8278 initialization to zero in the traditional C case.
8279 We also skip the warning if the initializer is designated,
8280 again on the assumption that this must be conditional on
8281 __STDC__ anyway (and we've already complained about the
8282 member-designator already). */
8283 if (!in_system_header && !constructor_designated
8284 && !(value.value && (integer_zerop (value.value)
8285 || real_zerop (value.value))))
8286 warning (OPT_Wtraditional, "traditional C rejects initialization "
8289 /* Accept a string constant to initialize a subarray. */
8290 if (value.value != 0
8291 && fieldcode == ARRAY_TYPE
8292 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8294 value.value = orig_value;
8295 /* Otherwise, if we have come to a subaggregate,
8296 and we don't have an element of its type, push into it. */
8297 else if (value.value != 0
8298 && value.value != error_mark_node
8299 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8300 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8301 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8303 push_init_level (1, braced_init_obstack);
8309 push_member_name (constructor_fields);
8310 output_init_element (value.value, value.original_type,
8311 strict_string, fieldtype,
8312 constructor_fields, 1, implicit,
8313 braced_init_obstack);
8314 RESTORE_SPELLING_DEPTH (constructor_depth);
8317 /* Do the bookkeeping for an element that was
8318 directly output as a constructor. */
8320 constructor_bit_index = DECL_SIZE (constructor_fields);
8321 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8324 constructor_fields = 0;
8326 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8328 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8329 enum tree_code eltcode = TREE_CODE (elttype);
8331 /* Accept a string constant to initialize a subarray. */
8332 if (value.value != 0
8333 && eltcode == ARRAY_TYPE
8334 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8336 value.value = orig_value;
8337 /* Otherwise, if we have come to a subaggregate,
8338 and we don't have an element of its type, push into it. */
8339 else if (value.value != 0
8340 && value.value != error_mark_node
8341 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8342 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8343 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8345 push_init_level (1, braced_init_obstack);
8349 if (constructor_max_index != 0
8350 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8351 || integer_all_onesp (constructor_max_index)))
8353 pedwarn_init (input_location, 0,
8354 "excess elements in array initializer");
8358 /* Now output the actual element. */
8361 push_array_bounds (tree_low_cst (constructor_index, 1));
8362 output_init_element (value.value, value.original_type,
8363 strict_string, elttype,
8364 constructor_index, 1, implicit,
8365 braced_init_obstack);
8366 RESTORE_SPELLING_DEPTH (constructor_depth);
8370 = size_binop_loc (input_location, PLUS_EXPR,
8371 constructor_index, bitsize_one_node);
8374 /* If we are doing the bookkeeping for an element that was
8375 directly output as a constructor, we must update
8376 constructor_unfilled_index. */
8377 constructor_unfilled_index = constructor_index;
8379 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8381 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8383 /* Do a basic check of initializer size. Note that vectors
8384 always have a fixed size derived from their type. */
8385 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8387 pedwarn_init (input_location, 0,
8388 "excess elements in vector initializer");
8392 /* Now output the actual element. */
8395 if (TREE_CODE (value.value) == VECTOR_CST)
8396 elttype = TYPE_MAIN_VARIANT (constructor_type);
8397 output_init_element (value.value, value.original_type,
8398 strict_string, elttype,
8399 constructor_index, 1, implicit,
8400 braced_init_obstack);
8404 = size_binop_loc (input_location,
8405 PLUS_EXPR, constructor_index, bitsize_one_node);
8408 /* If we are doing the bookkeeping for an element that was
8409 directly output as a constructor, we must update
8410 constructor_unfilled_index. */
8411 constructor_unfilled_index = constructor_index;
8414 /* Handle the sole element allowed in a braced initializer
8415 for a scalar variable. */
8416 else if (constructor_type != error_mark_node
8417 && constructor_fields == 0)
8419 pedwarn_init (input_location, 0,
8420 "excess elements in scalar initializer");
8426 output_init_element (value.value, value.original_type,
8427 strict_string, constructor_type,
8428 NULL_TREE, 1, implicit,
8429 braced_init_obstack);
8430 constructor_fields = 0;
8433 /* Handle range initializers either at this level or anywhere higher
8434 in the designator stack. */
8435 if (constructor_range_stack)
8437 struct constructor_range_stack *p, *range_stack;
8440 range_stack = constructor_range_stack;
8441 constructor_range_stack = 0;
8442 while (constructor_stack != range_stack->stack)
8444 gcc_assert (constructor_stack->implicit);
8445 process_init_element (pop_init_level (1,
8446 braced_init_obstack),
8447 true, braced_init_obstack);
8449 for (p = range_stack;
8450 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8453 gcc_assert (constructor_stack->implicit);
8454 process_init_element (pop_init_level (1, braced_init_obstack),
8455 true, braced_init_obstack);
8458 p->index = size_binop_loc (input_location,
8459 PLUS_EXPR, p->index, bitsize_one_node);
8460 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8465 constructor_index = p->index;
8466 constructor_fields = p->fields;
8467 if (finish && p->range_end && p->index == p->range_start)
8475 push_init_level (2, braced_init_obstack);
8476 p->stack = constructor_stack;
8477 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8478 p->index = p->range_start;
8482 constructor_range_stack = range_stack;
8489 constructor_range_stack = 0;
8492 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8493 (guaranteed to be 'volatile' or null) and ARGS (represented using
8494 an ASM_EXPR node). */
8496 build_asm_stmt (tree cv_qualifier, tree args)
8498 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8499 ASM_VOLATILE_P (args) = 1;
8500 return add_stmt (args);
8503 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8504 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8505 SIMPLE indicates whether there was anything at all after the
8506 string in the asm expression -- asm("blah") and asm("blah" : )
8507 are subtly different. We use a ASM_EXPR node to represent this. */
8509 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8510 tree clobbers, tree labels, bool simple)
8515 const char *constraint;
8516 const char **oconstraints;
8517 bool allows_mem, allows_reg, is_inout;
8518 int ninputs, noutputs;
8520 ninputs = list_length (inputs);
8521 noutputs = list_length (outputs);
8522 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8524 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8526 /* Remove output conversions that change the type but not the mode. */
8527 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8529 tree output = TREE_VALUE (tail);
8531 /* ??? Really, this should not be here. Users should be using a
8532 proper lvalue, dammit. But there's a long history of using casts
8533 in the output operands. In cases like longlong.h, this becomes a
8534 primitive form of typechecking -- if the cast can be removed, then
8535 the output operand had a type of the proper width; otherwise we'll
8536 get an error. Gross, but ... */
8537 STRIP_NOPS (output);
8539 if (!lvalue_or_else (output, lv_asm))
8540 output = error_mark_node;
8542 if (output != error_mark_node
8543 && (TREE_READONLY (output)
8544 || TYPE_READONLY (TREE_TYPE (output))
8545 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8546 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8547 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8548 readonly_error (output, lv_asm);
8550 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8551 oconstraints[i] = constraint;
8553 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8554 &allows_mem, &allows_reg, &is_inout))
8556 /* If the operand is going to end up in memory,
8557 mark it addressable. */
8558 if (!allows_reg && !c_mark_addressable (output))
8559 output = error_mark_node;
8562 output = error_mark_node;
8564 TREE_VALUE (tail) = output;
8567 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8571 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8572 input = TREE_VALUE (tail);
8574 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8575 oconstraints, &allows_mem, &allows_reg))
8577 /* If the operand is going to end up in memory,
8578 mark it addressable. */
8579 if (!allows_reg && allows_mem)
8581 /* Strip the nops as we allow this case. FIXME, this really
8582 should be rejected or made deprecated. */
8584 if (!c_mark_addressable (input))
8585 input = error_mark_node;
8589 input = error_mark_node;
8591 TREE_VALUE (tail) = input;
8594 /* ASMs with labels cannot have outputs. This should have been
8595 enforced by the parser. */
8596 gcc_assert (outputs == NULL || labels == NULL);
8598 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8600 /* asm statements without outputs, including simple ones, are treated
8602 ASM_INPUT_P (args) = simple;
8603 ASM_VOLATILE_P (args) = (noutputs == 0);
8608 /* Generate a goto statement to LABEL. LOC is the location of the
8612 c_finish_goto_label (location_t loc, tree label)
8614 tree decl = lookup_label_for_goto (loc, label);
8617 TREE_USED (decl) = 1;
8619 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8620 SET_EXPR_LOCATION (t, loc);
8621 return add_stmt (t);
8625 /* Generate a computed goto statement to EXPR. LOC is the location of
8629 c_finish_goto_ptr (location_t loc, tree expr)
8632 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8633 expr = c_fully_fold (expr, false, NULL);
8634 expr = convert (ptr_type_node, expr);
8635 t = build1 (GOTO_EXPR, void_type_node, expr);
8636 SET_EXPR_LOCATION (t, loc);
8637 return add_stmt (t);
8640 /* Generate a C `return' statement. RETVAL is the expression for what
8641 to return, or a null pointer for `return;' with no value. LOC is
8642 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8643 is the original type of RETVAL. */
8646 c_finish_return (location_t loc, tree retval, tree origtype)
8648 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8649 bool no_warning = false;
8652 if (TREE_THIS_VOLATILE (current_function_decl))
8654 "function declared %<noreturn%> has a %<return%> statement");
8658 tree semantic_type = NULL_TREE;
8659 npc = null_pointer_constant_p (retval);
8660 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8662 semantic_type = TREE_TYPE (retval);
8663 retval = TREE_OPERAND (retval, 0);
8665 retval = c_fully_fold (retval, false, NULL);
8667 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8672 current_function_returns_null = 1;
8673 if ((warn_return_type || flag_isoc99)
8674 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8676 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8677 "%<return%> with no value, in "
8678 "function returning non-void");
8682 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8684 current_function_returns_null = 1;
8685 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8687 "%<return%> with a value, in function returning void");
8689 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8690 "%<return%> with expression, in function returning void");
8694 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8696 npc, NULL_TREE, NULL_TREE, 0);
8697 tree res = DECL_RESULT (current_function_decl);
8700 current_function_returns_value = 1;
8701 if (t == error_mark_node)
8704 inner = t = convert (TREE_TYPE (res), t);
8706 /* Strip any conversions, additions, and subtractions, and see if
8707 we are returning the address of a local variable. Warn if so. */
8710 switch (TREE_CODE (inner))
8713 case NON_LVALUE_EXPR:
8715 case POINTER_PLUS_EXPR:
8716 inner = TREE_OPERAND (inner, 0);
8720 /* If the second operand of the MINUS_EXPR has a pointer
8721 type (or is converted from it), this may be valid, so
8722 don't give a warning. */
8724 tree op1 = TREE_OPERAND (inner, 1);
8726 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8727 && (CONVERT_EXPR_P (op1)
8728 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8729 op1 = TREE_OPERAND (op1, 0);
8731 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8734 inner = TREE_OPERAND (inner, 0);
8739 inner = TREE_OPERAND (inner, 0);
8741 while (REFERENCE_CLASS_P (inner)
8742 && TREE_CODE (inner) != INDIRECT_REF)
8743 inner = TREE_OPERAND (inner, 0);
8746 && !DECL_EXTERNAL (inner)
8747 && !TREE_STATIC (inner)
8748 && DECL_CONTEXT (inner) == current_function_decl)
8750 0, "function returns address of local variable");
8760 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8761 SET_EXPR_LOCATION (retval, loc);
8763 if (warn_sequence_point)
8764 verify_sequence_points (retval);
8767 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8768 TREE_NO_WARNING (ret_stmt) |= no_warning;
8769 return add_stmt (ret_stmt);
8773 /* The SWITCH_EXPR being built. */
8776 /* The original type of the testing expression, i.e. before the
8777 default conversion is applied. */
8780 /* A splay-tree mapping the low element of a case range to the high
8781 element, or NULL_TREE if there is no high element. Used to
8782 determine whether or not a new case label duplicates an old case
8783 label. We need a tree, rather than simply a hash table, because
8784 of the GNU case range extension. */
8787 /* The bindings at the point of the switch. This is used for
8788 warnings crossing decls when branching to a case label. */
8789 struct c_spot_bindings *bindings;
8791 /* The next node on the stack. */
8792 struct c_switch *next;
8795 /* A stack of the currently active switch statements. The innermost
8796 switch statement is on the top of the stack. There is no need to
8797 mark the stack for garbage collection because it is only active
8798 during the processing of the body of a function, and we never
8799 collect at that point. */
8801 struct c_switch *c_switch_stack;
8803 /* Start a C switch statement, testing expression EXP. Return the new
8804 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8805 SWITCH_COND_LOC is the location of the switch's condition. */
8808 c_start_case (location_t switch_loc,
8809 location_t switch_cond_loc,
8812 tree orig_type = error_mark_node;
8813 struct c_switch *cs;
8815 if (exp != error_mark_node)
8817 orig_type = TREE_TYPE (exp);
8819 if (!INTEGRAL_TYPE_P (orig_type))
8821 if (orig_type != error_mark_node)
8823 error_at (switch_cond_loc, "switch quantity not an integer");
8824 orig_type = error_mark_node;
8826 exp = integer_zero_node;
8830 tree type = TYPE_MAIN_VARIANT (orig_type);
8832 if (!in_system_header
8833 && (type == long_integer_type_node
8834 || type == long_unsigned_type_node))
8835 warning_at (switch_cond_loc,
8836 OPT_Wtraditional, "%<long%> switch expression not "
8837 "converted to %<int%> in ISO C");
8839 exp = c_fully_fold (exp, false, NULL);
8840 exp = default_conversion (exp);
8842 if (warn_sequence_point)
8843 verify_sequence_points (exp);
8847 /* Add this new SWITCH_EXPR to the stack. */
8848 cs = XNEW (struct c_switch);
8849 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8850 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8851 cs->orig_type = orig_type;
8852 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8853 cs->bindings = c_get_switch_bindings ();
8854 cs->next = c_switch_stack;
8855 c_switch_stack = cs;
8857 return add_stmt (cs->switch_expr);
8860 /* Process a case label at location LOC. */
8863 do_case (location_t loc, tree low_value, tree high_value)
8865 tree label = NULL_TREE;
8867 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8869 low_value = c_fully_fold (low_value, false, NULL);
8870 if (TREE_CODE (low_value) == INTEGER_CST)
8871 pedwarn (input_location, OPT_pedantic,
8872 "case label is not an integer constant expression");
8875 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8877 high_value = c_fully_fold (high_value, false, NULL);
8878 if (TREE_CODE (high_value) == INTEGER_CST)
8879 pedwarn (input_location, OPT_pedantic,
8880 "case label is not an integer constant expression");
8883 if (c_switch_stack == NULL)
8886 error_at (loc, "case label not within a switch statement");
8888 error_at (loc, "%<default%> label not within a switch statement");
8892 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8893 EXPR_LOCATION (c_switch_stack->switch_expr),
8897 label = c_add_case_label (loc, c_switch_stack->cases,
8898 SWITCH_COND (c_switch_stack->switch_expr),
8899 c_switch_stack->orig_type,
8900 low_value, high_value);
8901 if (label == error_mark_node)
8906 /* Finish the switch statement. */
8909 c_finish_case (tree body)
8911 struct c_switch *cs = c_switch_stack;
8912 location_t switch_location;
8914 SWITCH_BODY (cs->switch_expr) = body;
8916 /* Emit warnings as needed. */
8917 switch_location = EXPR_LOCATION (cs->switch_expr);
8918 c_do_switch_warnings (cs->cases, switch_location,
8919 TREE_TYPE (cs->switch_expr),
8920 SWITCH_COND (cs->switch_expr));
8922 /* Pop the stack. */
8923 c_switch_stack = cs->next;
8924 splay_tree_delete (cs->cases);
8925 c_release_switch_bindings (cs->bindings);
8929 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8930 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8931 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8932 statement, and was not surrounded with parenthesis. */
8935 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8936 tree else_block, bool nested_if)
8940 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8941 if (warn_parentheses && nested_if && else_block == NULL)
8943 tree inner_if = then_block;
8945 /* We know from the grammar productions that there is an IF nested
8946 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8947 it might not be exactly THEN_BLOCK, but should be the last
8948 non-container statement within. */
8950 switch (TREE_CODE (inner_if))
8955 inner_if = BIND_EXPR_BODY (inner_if);
8957 case STATEMENT_LIST:
8958 inner_if = expr_last (then_block);
8960 case TRY_FINALLY_EXPR:
8961 case TRY_CATCH_EXPR:
8962 inner_if = TREE_OPERAND (inner_if, 0);
8969 if (COND_EXPR_ELSE (inner_if))
8970 warning_at (if_locus, OPT_Wparentheses,
8971 "suggest explicit braces to avoid ambiguous %<else%>");
8974 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8975 SET_EXPR_LOCATION (stmt, if_locus);
8979 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8980 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8981 is false for DO loops. INCR is the FOR increment expression. BODY is
8982 the statement controlled by the loop. BLAB is the break label. CLAB is
8983 the continue label. Everything is allowed to be NULL. */
8986 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8987 tree blab, tree clab, bool cond_is_first)
8989 tree entry = NULL, exit = NULL, t;
8991 /* If the condition is zero don't generate a loop construct. */
8992 if (cond && integer_zerop (cond))
8996 t = build_and_jump (&blab);
8997 SET_EXPR_LOCATION (t, start_locus);
9003 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9005 /* If we have an exit condition, then we build an IF with gotos either
9006 out of the loop, or to the top of it. If there's no exit condition,
9007 then we just build a jump back to the top. */
9008 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9010 if (cond && !integer_nonzerop (cond))
9012 /* Canonicalize the loop condition to the end. This means
9013 generating a branch to the loop condition. Reuse the
9014 continue label, if possible. */
9019 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9020 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9023 t = build1 (GOTO_EXPR, void_type_node, clab);
9024 SET_EXPR_LOCATION (t, start_locus);
9028 t = build_and_jump (&blab);
9030 exit = fold_build3_loc (start_locus,
9031 COND_EXPR, void_type_node, cond, exit, t);
9033 exit = fold_build3_loc (input_location,
9034 COND_EXPR, void_type_node, cond, exit, t);
9043 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9051 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9055 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9058 tree label = *label_p;
9060 /* In switch statements break is sometimes stylistically used after
9061 a return statement. This can lead to spurious warnings about
9062 control reaching the end of a non-void function when it is
9063 inlined. Note that we are calling block_may_fallthru with
9064 language specific tree nodes; this works because
9065 block_may_fallthru returns true when given something it does not
9067 skip = !block_may_fallthru (cur_stmt_list);
9072 *label_p = label = create_artificial_label (loc);
9074 else if (TREE_CODE (label) == LABEL_DECL)
9076 else switch (TREE_INT_CST_LOW (label))
9080 error_at (loc, "break statement not within loop or switch");
9082 error_at (loc, "continue statement not within a loop");
9086 gcc_assert (is_break);
9087 error_at (loc, "break statement used with OpenMP for loop");
9098 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9100 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9103 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9106 emit_side_effect_warnings (location_t loc, tree expr)
9108 if (expr == error_mark_node)
9110 else if (!TREE_SIDE_EFFECTS (expr))
9112 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9113 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9116 warn_if_unused_value (expr, loc);
9119 /* Process an expression as if it were a complete statement. Emit
9120 diagnostics, but do not call ADD_STMT. LOC is the location of the
9124 c_process_expr_stmt (location_t loc, tree expr)
9131 expr = c_fully_fold (expr, false, NULL);
9133 if (warn_sequence_point)
9134 verify_sequence_points (expr);
9136 if (TREE_TYPE (expr) != error_mark_node
9137 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9138 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9139 error_at (loc, "expression statement has incomplete type");
9141 /* If we're not processing a statement expression, warn about unused values.
9142 Warnings for statement expressions will be emitted later, once we figure
9143 out which is the result. */
9144 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9145 && warn_unused_value)
9146 emit_side_effect_warnings (loc, expr);
9149 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9150 exprv = TREE_OPERAND (exprv, 1);
9151 if (DECL_P (exprv) || handled_component_p (exprv))
9152 mark_exp_read (exprv);
9154 /* If the expression is not of a type to which we cannot assign a line
9155 number, wrap the thing in a no-op NOP_EXPR. */
9156 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9158 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9159 SET_EXPR_LOCATION (expr, loc);
9165 /* Emit an expression as a statement. LOC is the location of the
9169 c_finish_expr_stmt (location_t loc, tree expr)
9172 return add_stmt (c_process_expr_stmt (loc, expr));
9177 /* Do the opposite and emit a statement as an expression. To begin,
9178 create a new binding level and return it. */
9181 c_begin_stmt_expr (void)
9185 /* We must force a BLOCK for this level so that, if it is not expanded
9186 later, there is a way to turn off the entire subtree of blocks that
9187 are contained in it. */
9189 ret = c_begin_compound_stmt (true);
9191 c_bindings_start_stmt_expr (c_switch_stack == NULL
9193 : c_switch_stack->bindings);
9195 /* Mark the current statement list as belonging to a statement list. */
9196 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9201 /* LOC is the location of the compound statement to which this body
9205 c_finish_stmt_expr (location_t loc, tree body)
9207 tree last, type, tmp, val;
9210 body = c_end_compound_stmt (loc, body, true);
9212 c_bindings_end_stmt_expr (c_switch_stack == NULL
9214 : c_switch_stack->bindings);
9216 /* Locate the last statement in BODY. See c_end_compound_stmt
9217 about always returning a BIND_EXPR. */
9218 last_p = &BIND_EXPR_BODY (body);
9219 last = BIND_EXPR_BODY (body);
9222 if (TREE_CODE (last) == STATEMENT_LIST)
9224 tree_stmt_iterator i;
9226 /* This can happen with degenerate cases like ({ }). No value. */
9227 if (!TREE_SIDE_EFFECTS (last))
9230 /* If we're supposed to generate side effects warnings, process
9231 all of the statements except the last. */
9232 if (warn_unused_value)
9234 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9237 tree t = tsi_stmt (i);
9239 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9240 emit_side_effect_warnings (tloc, t);
9244 i = tsi_last (last);
9245 last_p = tsi_stmt_ptr (i);
9249 /* If the end of the list is exception related, then the list was split
9250 by a call to push_cleanup. Continue searching. */
9251 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9252 || TREE_CODE (last) == TRY_CATCH_EXPR)
9254 last_p = &TREE_OPERAND (last, 0);
9256 goto continue_searching;
9259 if (last == error_mark_node)
9262 /* In the case that the BIND_EXPR is not necessary, return the
9263 expression out from inside it. */
9264 if (last == BIND_EXPR_BODY (body)
9265 && BIND_EXPR_VARS (body) == NULL)
9267 /* Even if this looks constant, do not allow it in a constant
9269 last = c_wrap_maybe_const (last, true);
9270 /* Do not warn if the return value of a statement expression is
9272 TREE_NO_WARNING (last) = 1;
9276 /* Extract the type of said expression. */
9277 type = TREE_TYPE (last);
9279 /* If we're not returning a value at all, then the BIND_EXPR that
9280 we already have is a fine expression to return. */
9281 if (!type || VOID_TYPE_P (type))
9284 /* Now that we've located the expression containing the value, it seems
9285 silly to make voidify_wrapper_expr repeat the process. Create a
9286 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9287 tmp = create_tmp_var_raw (type, NULL);
9289 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9290 tree_expr_nonnegative_p giving up immediately. */
9292 if (TREE_CODE (val) == NOP_EXPR
9293 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9294 val = TREE_OPERAND (val, 0);
9296 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9297 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9300 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9301 SET_EXPR_LOCATION (t, loc);
9306 /* Begin and end compound statements. This is as simple as pushing
9307 and popping new statement lists from the tree. */
9310 c_begin_compound_stmt (bool do_scope)
9312 tree stmt = push_stmt_list ();
9318 /* End a compound statement. STMT is the statement. LOC is the
9319 location of the compound statement-- this is usually the location
9320 of the opening brace. */
9323 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9329 if (c_dialect_objc ())
9330 objc_clear_super_receiver ();
9331 block = pop_scope ();
9334 stmt = pop_stmt_list (stmt);
9335 stmt = c_build_bind_expr (loc, block, stmt);
9337 /* If this compound statement is nested immediately inside a statement
9338 expression, then force a BIND_EXPR to be created. Otherwise we'll
9339 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9340 STATEMENT_LISTs merge, and thus we can lose track of what statement
9343 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9344 && TREE_CODE (stmt) != BIND_EXPR)
9346 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9347 TREE_SIDE_EFFECTS (stmt) = 1;
9348 SET_EXPR_LOCATION (stmt, loc);
9354 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9355 when the current scope is exited. EH_ONLY is true when this is not
9356 meant to apply to normal control flow transfer. */
9359 push_cleanup (tree decl, tree cleanup, bool eh_only)
9361 enum tree_code code;
9365 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9366 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9368 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9369 list = push_stmt_list ();
9370 TREE_OPERAND (stmt, 0) = list;
9371 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9374 /* Build a binary-operation expression without default conversions.
9375 CODE is the kind of expression to build.
9376 LOCATION is the operator's location.
9377 This function differs from `build' in several ways:
9378 the data type of the result is computed and recorded in it,
9379 warnings are generated if arg data types are invalid,
9380 special handling for addition and subtraction of pointers is known,
9381 and some optimization is done (operations on narrow ints
9382 are done in the narrower type when that gives the same result).
9383 Constant folding is also done before the result is returned.
9385 Note that the operands will never have enumeral types, or function
9386 or array types, because either they will have the default conversions
9387 performed or they have both just been converted to some other type in which
9388 the arithmetic is to be done. */
9391 build_binary_op (location_t location, enum tree_code code,
9392 tree orig_op0, tree orig_op1, int convert_p)
9394 tree type0, type1, orig_type0, orig_type1;
9396 enum tree_code code0, code1;
9398 tree ret = error_mark_node;
9399 const char *invalid_op_diag;
9400 bool op0_int_operands, op1_int_operands;
9401 bool int_const, int_const_or_overflow, int_operands;
9403 /* Expression code to give to the expression when it is built.
9404 Normally this is CODE, which is what the caller asked for,
9405 but in some special cases we change it. */
9406 enum tree_code resultcode = code;
9408 /* Data type in which the computation is to be performed.
9409 In the simplest cases this is the common type of the arguments. */
9410 tree result_type = NULL;
9412 /* When the computation is in excess precision, the type of the
9413 final EXCESS_PRECISION_EXPR. */
9414 tree semantic_result_type = NULL;
9416 /* Nonzero means operands have already been type-converted
9417 in whatever way is necessary.
9418 Zero means they need to be converted to RESULT_TYPE. */
9421 /* Nonzero means create the expression with this type, rather than
9423 tree build_type = 0;
9425 /* Nonzero means after finally constructing the expression
9426 convert it to this type. */
9427 tree final_type = 0;
9429 /* Nonzero if this is an operation like MIN or MAX which can
9430 safely be computed in short if both args are promoted shorts.
9431 Also implies COMMON.
9432 -1 indicates a bitwise operation; this makes a difference
9433 in the exact conditions for when it is safe to do the operation
9434 in a narrower mode. */
9437 /* Nonzero if this is a comparison operation;
9438 if both args are promoted shorts, compare the original shorts.
9439 Also implies COMMON. */
9440 int short_compare = 0;
9442 /* Nonzero if this is a right-shift operation, which can be computed on the
9443 original short and then promoted if the operand is a promoted short. */
9444 int short_shift = 0;
9446 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9449 /* True means types are compatible as far as ObjC is concerned. */
9452 /* True means this is an arithmetic operation that may need excess
9454 bool may_need_excess_precision;
9456 /* True means this is a boolean operation that converts both its
9457 operands to truth-values. */
9458 bool boolean_op = false;
9460 if (location == UNKNOWN_LOCATION)
9461 location = input_location;
9466 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9467 if (op0_int_operands)
9468 op0 = remove_c_maybe_const_expr (op0);
9469 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9470 if (op1_int_operands)
9471 op1 = remove_c_maybe_const_expr (op1);
9472 int_operands = (op0_int_operands && op1_int_operands);
9475 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9476 && TREE_CODE (orig_op1) == INTEGER_CST);
9477 int_const = (int_const_or_overflow
9478 && !TREE_OVERFLOW (orig_op0)
9479 && !TREE_OVERFLOW (orig_op1));
9482 int_const = int_const_or_overflow = false;
9486 op0 = default_conversion (op0);
9487 op1 = default_conversion (op1);
9490 orig_type0 = type0 = TREE_TYPE (op0);
9491 orig_type1 = type1 = TREE_TYPE (op1);
9493 /* The expression codes of the data types of the arguments tell us
9494 whether the arguments are integers, floating, pointers, etc. */
9495 code0 = TREE_CODE (type0);
9496 code1 = TREE_CODE (type1);
9498 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9499 STRIP_TYPE_NOPS (op0);
9500 STRIP_TYPE_NOPS (op1);
9502 /* If an error was already reported for one of the arguments,
9503 avoid reporting another error. */
9505 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9506 return error_mark_node;
9508 if ((invalid_op_diag
9509 = targetm.invalid_binary_op (code, type0, type1)))
9511 error_at (location, invalid_op_diag);
9512 return error_mark_node;
9520 case TRUNC_DIV_EXPR:
9522 case FLOOR_DIV_EXPR:
9523 case ROUND_DIV_EXPR:
9524 case EXACT_DIV_EXPR:
9525 may_need_excess_precision = true;
9528 may_need_excess_precision = false;
9531 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9533 op0 = TREE_OPERAND (op0, 0);
9534 type0 = TREE_TYPE (op0);
9536 else if (may_need_excess_precision
9537 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9540 op0 = convert (eptype, op0);
9542 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9544 op1 = TREE_OPERAND (op1, 0);
9545 type1 = TREE_TYPE (op1);
9547 else if (may_need_excess_precision
9548 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9551 op1 = convert (eptype, op1);
9554 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9559 /* Handle the pointer + int case. */
9560 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9562 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9563 goto return_build_binary_op;
9565 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9567 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9568 goto return_build_binary_op;
9575 /* Subtraction of two similar pointers.
9576 We must subtract them as integers, then divide by object size. */
9577 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9578 && comp_target_types (location, type0, type1))
9580 ret = pointer_diff (location, op0, op1);
9581 goto return_build_binary_op;
9583 /* Handle pointer minus int. Just like pointer plus int. */
9584 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9586 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9587 goto return_build_binary_op;
9597 case TRUNC_DIV_EXPR:
9599 case FLOOR_DIV_EXPR:
9600 case ROUND_DIV_EXPR:
9601 case EXACT_DIV_EXPR:
9602 warn_for_div_by_zero (location, op1);
9604 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9605 || code0 == FIXED_POINT_TYPE
9606 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9607 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9608 || code1 == FIXED_POINT_TYPE
9609 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9611 enum tree_code tcode0 = code0, tcode1 = code1;
9613 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9614 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9615 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9616 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9618 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9619 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9620 resultcode = RDIV_EXPR;
9622 /* Although it would be tempting to shorten always here, that
9623 loses on some targets, since the modulo instruction is
9624 undefined if the quotient can't be represented in the
9625 computation mode. We shorten only if unsigned or if
9626 dividing by something we know != -1. */
9627 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9628 || (TREE_CODE (op1) == INTEGER_CST
9629 && !integer_all_onesp (op1)));
9637 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9639 /* Allow vector types which are not floating point types. */
9640 else if (code0 == VECTOR_TYPE
9641 && code1 == VECTOR_TYPE
9642 && !VECTOR_FLOAT_TYPE_P (type0)
9643 && !VECTOR_FLOAT_TYPE_P (type1))
9647 case TRUNC_MOD_EXPR:
9648 case FLOOR_MOD_EXPR:
9649 warn_for_div_by_zero (location, op1);
9651 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9652 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9653 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9655 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9657 /* Although it would be tempting to shorten always here, that loses
9658 on some targets, since the modulo instruction is undefined if the
9659 quotient can't be represented in the computation mode. We shorten
9660 only if unsigned or if dividing by something we know != -1. */
9661 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9662 || (TREE_CODE (op1) == INTEGER_CST
9663 && !integer_all_onesp (op1)));
9668 case TRUTH_ANDIF_EXPR:
9669 case TRUTH_ORIF_EXPR:
9670 case TRUTH_AND_EXPR:
9672 case TRUTH_XOR_EXPR:
9673 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9674 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9675 || code0 == FIXED_POINT_TYPE)
9676 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9677 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9678 || code1 == FIXED_POINT_TYPE))
9680 /* Result of these operations is always an int,
9681 but that does not mean the operands should be
9682 converted to ints! */
9683 result_type = integer_type_node;
9684 op0 = c_common_truthvalue_conversion (location, op0);
9685 op1 = c_common_truthvalue_conversion (location, op1);
9689 if (code == TRUTH_ANDIF_EXPR)
9691 int_const_or_overflow = (int_operands
9692 && TREE_CODE (orig_op0) == INTEGER_CST
9693 && (op0 == truthvalue_false_node
9694 || TREE_CODE (orig_op1) == INTEGER_CST));
9695 int_const = (int_const_or_overflow
9696 && !TREE_OVERFLOW (orig_op0)
9697 && (op0 == truthvalue_false_node
9698 || !TREE_OVERFLOW (orig_op1)));
9700 else if (code == TRUTH_ORIF_EXPR)
9702 int_const_or_overflow = (int_operands
9703 && TREE_CODE (orig_op0) == INTEGER_CST
9704 && (op0 == truthvalue_true_node
9705 || TREE_CODE (orig_op1) == INTEGER_CST));
9706 int_const = (int_const_or_overflow
9707 && !TREE_OVERFLOW (orig_op0)
9708 && (op0 == truthvalue_true_node
9709 || !TREE_OVERFLOW (orig_op1)));
9713 /* Shift operations: result has same type as first operand;
9714 always convert second operand to int.
9715 Also set SHORT_SHIFT if shifting rightward. */
9718 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9719 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9721 result_type = type0;
9724 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9725 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9726 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9727 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9729 result_type = type0;
9732 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9733 && code1 == INTEGER_TYPE)
9735 if (TREE_CODE (op1) == INTEGER_CST)
9737 if (tree_int_cst_sgn (op1) < 0)
9740 if (c_inhibit_evaluation_warnings == 0)
9741 warning (0, "right shift count is negative");
9745 if (!integer_zerop (op1))
9748 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9751 if (c_inhibit_evaluation_warnings == 0)
9752 warning (0, "right shift count >= width of type");
9757 /* Use the type of the value to be shifted. */
9758 result_type = type0;
9759 /* Convert the non vector shift-count to an integer, regardless
9760 of size of value being shifted. */
9761 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9762 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9763 op1 = convert (integer_type_node, op1);
9764 /* Avoid converting op1 to result_type later. */
9770 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9771 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9773 result_type = type0;
9776 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9777 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9778 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9779 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9781 result_type = type0;
9784 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9785 && code1 == INTEGER_TYPE)
9787 if (TREE_CODE (op1) == INTEGER_CST)
9789 if (tree_int_cst_sgn (op1) < 0)
9792 if (c_inhibit_evaluation_warnings == 0)
9793 warning (0, "left shift count is negative");
9796 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9799 if (c_inhibit_evaluation_warnings == 0)
9800 warning (0, "left shift count >= width of type");
9804 /* Use the type of the value to be shifted. */
9805 result_type = type0;
9806 /* Convert the non vector shift-count to an integer, regardless
9807 of size of value being shifted. */
9808 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9809 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9810 op1 = convert (integer_type_node, op1);
9811 /* Avoid converting op1 to result_type later. */
9818 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9819 warning_at (location,
9821 "comparing floating point with == or != is unsafe");
9822 /* Result of comparison is always int,
9823 but don't convert the args to int! */
9824 build_type = integer_type_node;
9825 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9826 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9827 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9828 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9830 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9832 if (TREE_CODE (op0) == ADDR_EXPR
9833 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9835 if (code == EQ_EXPR)
9836 warning_at (location,
9838 "the comparison will always evaluate as %<false%> "
9839 "for the address of %qD will never be NULL",
9840 TREE_OPERAND (op0, 0));
9842 warning_at (location,
9844 "the comparison will always evaluate as %<true%> "
9845 "for the address of %qD will never be NULL",
9846 TREE_OPERAND (op0, 0));
9848 result_type = type0;
9850 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9852 if (TREE_CODE (op1) == ADDR_EXPR
9853 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9855 if (code == EQ_EXPR)
9856 warning_at (location,
9858 "the comparison will always evaluate as %<false%> "
9859 "for the address of %qD will never be NULL",
9860 TREE_OPERAND (op1, 0));
9862 warning_at (location,
9864 "the comparison will always evaluate as %<true%> "
9865 "for the address of %qD will never be NULL",
9866 TREE_OPERAND (op1, 0));
9868 result_type = type1;
9870 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9872 tree tt0 = TREE_TYPE (type0);
9873 tree tt1 = TREE_TYPE (type1);
9874 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9875 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9876 addr_space_t as_common = ADDR_SPACE_GENERIC;
9878 /* Anything compares with void *. void * compares with anything.
9879 Otherwise, the targets must be compatible
9880 and both must be object or both incomplete. */
9881 if (comp_target_types (location, type0, type1))
9882 result_type = common_pointer_type (type0, type1);
9883 else if (!addr_space_superset (as0, as1, &as_common))
9885 error_at (location, "comparison of pointers to "
9886 "disjoint address spaces");
9887 return error_mark_node;
9889 else if (VOID_TYPE_P (tt0))
9891 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9892 pedwarn (location, OPT_pedantic, "ISO C forbids "
9893 "comparison of %<void *%> with function pointer");
9895 else if (VOID_TYPE_P (tt1))
9897 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9898 pedwarn (location, OPT_pedantic, "ISO C forbids "
9899 "comparison of %<void *%> with function pointer");
9902 /* Avoid warning about the volatile ObjC EH puts on decls. */
9904 pedwarn (location, 0,
9905 "comparison of distinct pointer types lacks a cast");
9907 if (result_type == NULL_TREE)
9909 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9910 result_type = build_pointer_type
9911 (build_qualified_type (void_type_node, qual));
9914 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9916 result_type = type0;
9917 pedwarn (location, 0, "comparison between pointer and integer");
9919 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9921 result_type = type1;
9922 pedwarn (location, 0, "comparison between pointer and integer");
9930 build_type = integer_type_node;
9931 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9932 || code0 == FIXED_POINT_TYPE)
9933 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9934 || code1 == FIXED_POINT_TYPE))
9936 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9938 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9939 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9940 addr_space_t as_common;
9942 if (comp_target_types (location, type0, type1))
9944 result_type = common_pointer_type (type0, type1);
9945 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9946 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9947 pedwarn (location, 0,
9948 "comparison of complete and incomplete pointers");
9949 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9950 pedwarn (location, OPT_pedantic, "ISO C forbids "
9951 "ordered comparisons of pointers to functions");
9952 else if (null_pointer_constant_p (orig_op0)
9953 || null_pointer_constant_p (orig_op1))
9954 warning_at (location, OPT_Wextra,
9955 "ordered comparison of pointer with null pointer");
9958 else if (!addr_space_superset (as0, as1, &as_common))
9960 error_at (location, "comparison of pointers to "
9961 "disjoint address spaces");
9962 return error_mark_node;
9966 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9967 result_type = build_pointer_type
9968 (build_qualified_type (void_type_node, qual));
9969 pedwarn (location, 0,
9970 "comparison of distinct pointer types lacks a cast");
9973 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9975 result_type = type0;
9977 pedwarn (location, OPT_pedantic,
9978 "ordered comparison of pointer with integer zero");
9979 else if (extra_warnings)
9980 warning_at (location, OPT_Wextra,
9981 "ordered comparison of pointer with integer zero");
9983 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9985 result_type = type1;
9987 pedwarn (location, OPT_pedantic,
9988 "ordered comparison of pointer with integer zero");
9989 else if (extra_warnings)
9990 warning_at (location, OPT_Wextra,
9991 "ordered comparison of pointer with integer zero");
9993 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9995 result_type = type0;
9996 pedwarn (location, 0, "comparison between pointer and integer");
9998 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10000 result_type = type1;
10001 pedwarn (location, 0, "comparison between pointer and integer");
10006 gcc_unreachable ();
10009 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10010 return error_mark_node;
10012 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10013 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10014 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10015 TREE_TYPE (type1))))
10017 binary_op_error (location, code, type0, type1);
10018 return error_mark_node;
10021 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10022 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10024 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10025 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10027 bool first_complex = (code0 == COMPLEX_TYPE);
10028 bool second_complex = (code1 == COMPLEX_TYPE);
10029 int none_complex = (!first_complex && !second_complex);
10031 if (shorten || common || short_compare)
10033 result_type = c_common_type (type0, type1);
10034 do_warn_double_promotion (result_type, type0, type1,
10035 "implicit conversion from %qT to %qT "
10036 "to match other operand of binary "
10039 if (result_type == error_mark_node)
10040 return error_mark_node;
10043 if (first_complex != second_complex
10044 && (code == PLUS_EXPR
10045 || code == MINUS_EXPR
10046 || code == MULT_EXPR
10047 || (code == TRUNC_DIV_EXPR && first_complex))
10048 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10049 && flag_signed_zeros)
10051 /* An operation on mixed real/complex operands must be
10052 handled specially, but the language-independent code can
10053 more easily optimize the plain complex arithmetic if
10054 -fno-signed-zeros. */
10055 tree real_type = TREE_TYPE (result_type);
10057 if (type0 != orig_type0 || type1 != orig_type1)
10059 gcc_assert (may_need_excess_precision && common);
10060 semantic_result_type = c_common_type (orig_type0, orig_type1);
10064 if (TREE_TYPE (op0) != result_type)
10065 op0 = convert_and_check (result_type, op0);
10066 if (TREE_TYPE (op1) != real_type)
10067 op1 = convert_and_check (real_type, op1);
10071 if (TREE_TYPE (op0) != real_type)
10072 op0 = convert_and_check (real_type, op0);
10073 if (TREE_TYPE (op1) != result_type)
10074 op1 = convert_and_check (result_type, op1);
10076 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10077 return error_mark_node;
10080 op0 = c_save_expr (op0);
10081 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10083 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10088 case TRUNC_DIV_EXPR:
10089 imag = build2 (resultcode, real_type, imag, op1);
10090 /* Fall through. */
10093 real = build2 (resultcode, real_type, real, op1);
10101 op1 = c_save_expr (op1);
10102 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10104 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10109 imag = build2 (resultcode, real_type, op0, imag);
10110 /* Fall through. */
10112 real = build2 (resultcode, real_type, op0, real);
10115 real = build2 (resultcode, real_type, op0, real);
10116 imag = build1 (NEGATE_EXPR, real_type, imag);
10122 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10123 goto return_build_binary_op;
10126 /* For certain operations (which identify themselves by shorten != 0)
10127 if both args were extended from the same smaller type,
10128 do the arithmetic in that type and then extend.
10130 shorten !=0 and !=1 indicates a bitwise operation.
10131 For them, this optimization is safe only if
10132 both args are zero-extended or both are sign-extended.
10133 Otherwise, we might change the result.
10134 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10135 but calculated in (unsigned short) it would be (unsigned short)-1. */
10137 if (shorten && none_complex)
10139 final_type = result_type;
10140 result_type = shorten_binary_op (result_type, op0, op1,
10144 /* Shifts can be shortened if shifting right. */
10149 tree arg0 = get_narrower (op0, &unsigned_arg);
10151 final_type = result_type;
10153 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10154 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10156 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10157 && tree_int_cst_sgn (op1) > 0
10158 /* We can shorten only if the shift count is less than the
10159 number of bits in the smaller type size. */
10160 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10161 /* We cannot drop an unsigned shift after sign-extension. */
10162 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10164 /* Do an unsigned shift if the operand was zero-extended. */
10166 = c_common_signed_or_unsigned_type (unsigned_arg,
10168 /* Convert value-to-be-shifted to that type. */
10169 if (TREE_TYPE (op0) != result_type)
10170 op0 = convert (result_type, op0);
10175 /* Comparison operations are shortened too but differently.
10176 They identify themselves by setting short_compare = 1. */
10180 /* Don't write &op0, etc., because that would prevent op0
10181 from being kept in a register.
10182 Instead, make copies of the our local variables and
10183 pass the copies by reference, then copy them back afterward. */
10184 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10185 enum tree_code xresultcode = resultcode;
10187 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10192 goto return_build_binary_op;
10195 op0 = xop0, op1 = xop1;
10197 resultcode = xresultcode;
10199 if (c_inhibit_evaluation_warnings == 0)
10201 bool op0_maybe_const = true;
10202 bool op1_maybe_const = true;
10203 tree orig_op0_folded, orig_op1_folded;
10205 if (in_late_binary_op)
10207 orig_op0_folded = orig_op0;
10208 orig_op1_folded = orig_op1;
10212 /* Fold for the sake of possible warnings, as in
10213 build_conditional_expr. This requires the
10214 "original" values to be folded, not just op0 and
10216 c_inhibit_evaluation_warnings++;
10217 op0 = c_fully_fold (op0, require_constant_value,
10219 op1 = c_fully_fold (op1, require_constant_value,
10221 c_inhibit_evaluation_warnings--;
10222 orig_op0_folded = c_fully_fold (orig_op0,
10223 require_constant_value,
10225 orig_op1_folded = c_fully_fold (orig_op1,
10226 require_constant_value,
10230 if (warn_sign_compare)
10231 warn_for_sign_compare (location, orig_op0_folded,
10232 orig_op1_folded, op0, op1,
10233 result_type, resultcode);
10234 if (!in_late_binary_op)
10236 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10237 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10238 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10239 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10245 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10246 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10247 Then the expression will be built.
10248 It will be given type FINAL_TYPE if that is nonzero;
10249 otherwise, it will be given type RESULT_TYPE. */
10253 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10254 return error_mark_node;
10257 if (build_type == NULL_TREE)
10259 build_type = result_type;
10260 if ((type0 != orig_type0 || type1 != orig_type1)
10263 gcc_assert (may_need_excess_precision && common);
10264 semantic_result_type = c_common_type (orig_type0, orig_type1);
10270 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10271 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10273 /* This can happen if one operand has a vector type, and the other
10274 has a different type. */
10275 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10276 return error_mark_node;
10279 /* Treat expressions in initializers specially as they can't trap. */
10280 if (int_const_or_overflow)
10281 ret = (require_constant_value
10282 ? fold_build2_initializer_loc (location, resultcode, build_type,
10284 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10286 ret = build2 (resultcode, build_type, op0, op1);
10287 if (final_type != 0)
10288 ret = convert (final_type, ret);
10290 return_build_binary_op:
10291 gcc_assert (ret != error_mark_node);
10292 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10293 ret = (int_operands
10294 ? note_integer_operands (ret)
10295 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10296 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10297 && !in_late_binary_op)
10298 ret = note_integer_operands (ret);
10299 if (semantic_result_type)
10300 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10301 protected_set_expr_location (ret, location);
10306 /* Convert EXPR to be a truth-value, validating its type for this
10307 purpose. LOCATION is the source location for the expression. */
10310 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10312 bool int_const, int_operands;
10314 switch (TREE_CODE (TREE_TYPE (expr)))
10317 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10318 return error_mark_node;
10321 error_at (location, "used struct type value where scalar is required");
10322 return error_mark_node;
10325 error_at (location, "used union type value where scalar is required");
10326 return error_mark_node;
10328 case FUNCTION_TYPE:
10329 gcc_unreachable ();
10335 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10336 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10338 expr = remove_c_maybe_const_expr (expr);
10340 /* ??? Should we also give an error for void and vectors rather than
10341 leaving those to give errors later? */
10342 expr = c_common_truthvalue_conversion (location, expr);
10344 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10346 if (TREE_OVERFLOW (expr))
10349 return note_integer_operands (expr);
10351 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10352 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10357 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10361 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10363 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10365 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10366 /* Executing a compound literal inside a function reinitializes
10368 if (!TREE_STATIC (decl))
10376 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10379 c_begin_omp_parallel (void)
10383 keep_next_level ();
10384 block = c_begin_compound_stmt (true);
10389 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10390 statement. LOC is the location of the OMP_PARALLEL. */
10393 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10397 block = c_end_compound_stmt (loc, block, true);
10399 stmt = make_node (OMP_PARALLEL);
10400 TREE_TYPE (stmt) = void_type_node;
10401 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10402 OMP_PARALLEL_BODY (stmt) = block;
10403 SET_EXPR_LOCATION (stmt, loc);
10405 return add_stmt (stmt);
10408 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10411 c_begin_omp_task (void)
10415 keep_next_level ();
10416 block = c_begin_compound_stmt (true);
10421 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10422 statement. LOC is the location of the #pragma. */
10425 c_finish_omp_task (location_t loc, tree clauses, tree block)
10429 block = c_end_compound_stmt (loc, block, true);
10431 stmt = make_node (OMP_TASK);
10432 TREE_TYPE (stmt) = void_type_node;
10433 OMP_TASK_CLAUSES (stmt) = clauses;
10434 OMP_TASK_BODY (stmt) = block;
10435 SET_EXPR_LOCATION (stmt, loc);
10437 return add_stmt (stmt);
10440 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10441 Remove any elements from the list that are invalid. */
10444 c_finish_omp_clauses (tree clauses)
10446 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10447 tree c, t, *pc = &clauses;
10450 bitmap_obstack_initialize (NULL);
10451 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10452 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10453 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10455 for (pc = &clauses, c = clauses; c ; c = *pc)
10457 bool remove = false;
10458 bool need_complete = false;
10459 bool need_implicitly_determined = false;
10461 switch (OMP_CLAUSE_CODE (c))
10463 case OMP_CLAUSE_SHARED:
10465 need_implicitly_determined = true;
10466 goto check_dup_generic;
10468 case OMP_CLAUSE_PRIVATE:
10470 need_complete = true;
10471 need_implicitly_determined = true;
10472 goto check_dup_generic;
10474 case OMP_CLAUSE_REDUCTION:
10475 name = "reduction";
10476 need_implicitly_determined = true;
10477 t = OMP_CLAUSE_DECL (c);
10478 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10479 || POINTER_TYPE_P (TREE_TYPE (t)))
10481 error_at (OMP_CLAUSE_LOCATION (c),
10482 "%qE has invalid type for %<reduction%>", t);
10485 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10487 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10488 const char *r_name = NULL;
10505 case TRUTH_ANDIF_EXPR:
10508 case TRUTH_ORIF_EXPR:
10512 gcc_unreachable ();
10516 error_at (OMP_CLAUSE_LOCATION (c),
10517 "%qE has invalid type for %<reduction(%s)%>",
10522 goto check_dup_generic;
10524 case OMP_CLAUSE_COPYPRIVATE:
10525 name = "copyprivate";
10526 goto check_dup_generic;
10528 case OMP_CLAUSE_COPYIN:
10530 t = OMP_CLAUSE_DECL (c);
10531 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10533 error_at (OMP_CLAUSE_LOCATION (c),
10534 "%qE must be %<threadprivate%> for %<copyin%>", t);
10537 goto check_dup_generic;
10540 t = OMP_CLAUSE_DECL (c);
10541 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10543 error_at (OMP_CLAUSE_LOCATION (c),
10544 "%qE is not a variable in clause %qs", t, name);
10547 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10548 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10549 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10551 error_at (OMP_CLAUSE_LOCATION (c),
10552 "%qE appears more than once in data clauses", t);
10556 bitmap_set_bit (&generic_head, DECL_UID (t));
10559 case OMP_CLAUSE_FIRSTPRIVATE:
10560 name = "firstprivate";
10561 t = OMP_CLAUSE_DECL (c);
10562 need_complete = true;
10563 need_implicitly_determined = true;
10564 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10566 error_at (OMP_CLAUSE_LOCATION (c),
10567 "%qE is not a variable in clause %<firstprivate%>", t);
10570 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10571 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10573 error_at (OMP_CLAUSE_LOCATION (c),
10574 "%qE appears more than once in data clauses", t);
10578 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10581 case OMP_CLAUSE_LASTPRIVATE:
10582 name = "lastprivate";
10583 t = OMP_CLAUSE_DECL (c);
10584 need_complete = true;
10585 need_implicitly_determined = true;
10586 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10588 error_at (OMP_CLAUSE_LOCATION (c),
10589 "%qE is not a variable in clause %<lastprivate%>", t);
10592 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10593 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10595 error_at (OMP_CLAUSE_LOCATION (c),
10596 "%qE appears more than once in data clauses", t);
10600 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10603 case OMP_CLAUSE_IF:
10604 case OMP_CLAUSE_NUM_THREADS:
10605 case OMP_CLAUSE_SCHEDULE:
10606 case OMP_CLAUSE_NOWAIT:
10607 case OMP_CLAUSE_ORDERED:
10608 case OMP_CLAUSE_DEFAULT:
10609 case OMP_CLAUSE_UNTIED:
10610 case OMP_CLAUSE_COLLAPSE:
10611 pc = &OMP_CLAUSE_CHAIN (c);
10615 gcc_unreachable ();
10620 t = OMP_CLAUSE_DECL (c);
10624 t = require_complete_type (t);
10625 if (t == error_mark_node)
10629 if (need_implicitly_determined)
10631 const char *share_name = NULL;
10633 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10634 share_name = "threadprivate";
10635 else switch (c_omp_predetermined_sharing (t))
10637 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10639 case OMP_CLAUSE_DEFAULT_SHARED:
10640 share_name = "shared";
10642 case OMP_CLAUSE_DEFAULT_PRIVATE:
10643 share_name = "private";
10646 gcc_unreachable ();
10650 error_at (OMP_CLAUSE_LOCATION (c),
10651 "%qE is predetermined %qs for %qs",
10652 t, share_name, name);
10659 *pc = OMP_CLAUSE_CHAIN (c);
10661 pc = &OMP_CLAUSE_CHAIN (c);
10664 bitmap_obstack_release (NULL);
10668 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10669 down to the element type of an array. */
10672 c_build_qualified_type (tree type, int type_quals)
10674 if (type == error_mark_node)
10677 if (TREE_CODE (type) == ARRAY_TYPE)
10680 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10683 /* See if we already have an identically qualified type. */
10684 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10686 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10687 && TYPE_NAME (t) == TYPE_NAME (type)
10688 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10689 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10690 TYPE_ATTRIBUTES (type)))
10695 tree domain = TYPE_DOMAIN (type);
10697 t = build_variant_type_copy (type);
10698 TREE_TYPE (t) = element_type;
10700 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10701 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10702 SET_TYPE_STRUCTURAL_EQUALITY (t);
10703 else if (TYPE_CANONICAL (element_type) != element_type
10704 || (domain && TYPE_CANONICAL (domain) != domain))
10706 tree unqualified_canon
10707 = build_array_type (TYPE_CANONICAL (element_type),
10708 domain? TYPE_CANONICAL (domain)
10711 = c_build_qualified_type (unqualified_canon, type_quals);
10714 TYPE_CANONICAL (t) = t;
10719 /* A restrict-qualified pointer type must be a pointer to object or
10720 incomplete type. Note that the use of POINTER_TYPE_P also allows
10721 REFERENCE_TYPEs, which is appropriate for C++. */
10722 if ((type_quals & TYPE_QUAL_RESTRICT)
10723 && (!POINTER_TYPE_P (type)
10724 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10726 error ("invalid use of %<restrict%>");
10727 type_quals &= ~TYPE_QUAL_RESTRICT;
10730 return build_qualified_type (type, type_quals);
10733 /* Build a VA_ARG_EXPR for the C parser. */
10736 c_build_va_arg (location_t loc, tree expr, tree type)
10738 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10739 warning_at (loc, OPT_Wc___compat,
10740 "C++ requires promoted type, not enum type, in %<va_arg%>");
10741 return build_va_arg (loc, expr, type);