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, 2011
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 /* Possibe cases of scalar_to_vector conversion. */
56 stv_error, /* Error occured. */
57 stv_nothing, /* Nothing happened. */
58 stv_firstarg, /* First argument must be expanded. */
59 stv_secondarg /* Second argument must be expanded. */
62 /* The level of nesting inside "__alignof__". */
65 /* The level of nesting inside "sizeof". */
68 /* The level of nesting inside "typeof". */
71 /* Nonzero if we've already printed a "missing braces around initializer"
72 message within this initializer. */
73 static int missing_braces_mentioned;
75 static int require_constant_value;
76 static int require_constant_elements;
78 static bool null_pointer_constant_p (const_tree);
79 static tree qualify_type (tree, tree);
80 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
82 static int comp_target_types (location_t, tree, tree);
83 static int function_types_compatible_p (const_tree, const_tree, bool *,
85 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
86 static tree lookup_field (tree, tree);
87 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
89 static tree pointer_diff (location_t, tree, tree);
90 static tree convert_for_assignment (location_t, tree, tree, tree,
91 enum impl_conv, bool, tree, tree, int);
92 static tree valid_compound_expr_initializer (tree, tree);
93 static void push_string (const char *);
94 static void push_member_name (tree);
95 static int spelling_length (void);
96 static char *print_spelling (char *);
97 static void warning_init (int, const char *);
98 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
99 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
101 static void output_pending_init_elements (int, struct obstack *);
102 static int set_designator (int, struct obstack *);
103 static void push_range_stack (tree, struct obstack *);
104 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
105 static void set_nonincremental_init (struct obstack *);
106 static void set_nonincremental_init_from_string (tree, struct obstack *);
107 static tree find_init_member (tree, struct obstack *);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
113 /* Return true if EXP is a null pointer constant, false otherwise. */
116 null_pointer_constant_p (const_tree expr)
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
122 && !TREE_OVERFLOW (expr)
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 /* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
136 note_integer_operands (tree expr)
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
158 remove_c_maybe_const_expr (tree expr)
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
166 \f/* This is a cache to hold if two types are compatible or not. */
168 struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
184 require_complete_type (tree value)
186 tree type = TREE_TYPE (value);
188 if (value == error_mark_node || type == error_mark_node)
189 return error_mark_node;
191 /* First, detect a valid value with a complete type. */
192 if (COMPLETE_TYPE_P (type))
195 c_incomplete_type_error (value, type);
196 return error_mark_node;
199 /* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
204 c_incomplete_type_error (const_tree value, const_tree type)
206 const char *type_code_string;
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
214 error ("%qD has an incomplete type", value);
218 /* We must print an error message. Be clever about what it says. */
220 switch (TREE_CODE (type))
223 type_code_string = "struct";
227 type_code_string = "union";
231 type_code_string = "enum";
235 error ("invalid use of void expression");
239 if (TYPE_DOMAIN (type))
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 error ("invalid use of flexible array member");
246 type = TREE_TYPE (type);
249 error ("invalid use of array with unspecified bounds");
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
269 c_type_promotes_to (tree type)
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
274 if (c_promoting_integer_type_p (type))
276 /* Preserve unsignedness if not really getting any wider. */
277 if (TYPE_UNSIGNED (type)
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
280 return integer_type_node;
286 /* Return true if between two named address spaces, whether there is a superset
287 named address space that encompasses both address spaces. If there is a
288 superset, return which address space is the superset. */
291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
298 else if (targetm.addr_space.subset_p (as1, as2))
303 else if (targetm.addr_space.subset_p (as2, as1))
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313 as well as those of TYPE. */
316 qualify_type (tree type, tree like)
318 addr_space_t as_type = TYPE_ADDR_SPACE (type);
319 addr_space_t as_like = TYPE_ADDR_SPACE (like);
320 addr_space_t as_common;
322 /* If the two named address spaces are different, determine the common
323 superset address space. If there isn't one, raise an error. */
324 if (!addr_space_superset (as_type, as_like, &as_common))
327 error ("%qT and %qT are in disjoint named address spaces",
331 return c_build_qualified_type (type,
332 TYPE_QUALS_NO_ADDR_SPACE (type)
333 | TYPE_QUALS_NO_ADDR_SPACE (like)
334 | ENCODE_QUAL_ADDR_SPACE (as_common));
337 /* Return true iff the given tree T is a variable length array. */
340 c_vla_type_p (const_tree t)
342 if (TREE_CODE (t) == ARRAY_TYPE
343 && C_TYPE_VARIABLE_SIZE (t))
348 /* Return the composite type of two compatible types.
350 We assume that comptypes has already been done and returned
351 nonzero; if that isn't so, this may crash. In particular, we
352 assume that qualifiers match. */
355 composite_type (tree t1, tree t2)
357 enum tree_code code1;
358 enum tree_code code2;
361 /* Save time if the two types are the same. */
363 if (t1 == t2) return t1;
365 /* If one type is nonsense, use the other. */
366 if (t1 == error_mark_node)
368 if (t2 == error_mark_node)
371 code1 = TREE_CODE (t1);
372 code2 = TREE_CODE (t2);
374 /* Merge the attributes. */
375 attributes = targetm.merge_type_attributes (t1, t2);
377 /* If one is an enumerated type and the other is the compatible
378 integer type, the composite type might be either of the two
379 (DR#013 question 3). For consistency, use the enumerated type as
380 the composite type. */
382 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
384 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
387 gcc_assert (code1 == code2);
392 /* For two pointers, do this recursively on the target type. */
394 tree pointed_to_1 = TREE_TYPE (t1);
395 tree pointed_to_2 = TREE_TYPE (t2);
396 tree target = composite_type (pointed_to_1, pointed_to_2);
397 t1 = build_pointer_type (target);
398 t1 = build_type_attribute_variant (t1, attributes);
399 return qualify_type (t1, t2);
404 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
407 tree d1 = TYPE_DOMAIN (t1);
408 tree d2 = TYPE_DOMAIN (t2);
409 bool d1_variable, d2_variable;
410 bool d1_zero, d2_zero;
411 bool t1_complete, t2_complete;
413 /* We should not have any type quals on arrays at all. */
414 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
417 t1_complete = COMPLETE_TYPE_P (t1);
418 t2_complete = COMPLETE_TYPE_P (t2);
420 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423 d1_variable = (!d1_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426 d2_variable = (!d2_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
432 /* Save space: see if the result is identical to one of the args. */
433 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434 && (d2_variable || d2_zero || !d1_variable))
435 return build_type_attribute_variant (t1, attributes);
436 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437 && (d1_variable || d1_zero || !d2_variable))
438 return build_type_attribute_variant (t2, attributes);
440 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441 return build_type_attribute_variant (t1, attributes);
442 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t2, attributes);
445 /* Merge the element types, and have a size if either arg has
446 one. We may have qualifiers on the element types. To set
447 up TYPE_MAIN_VARIANT correctly, we need to form the
448 composite of the unqualified types and add the qualifiers
450 quals = TYPE_QUALS (strip_array_types (elt));
451 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452 t1 = build_array_type (unqual_elt,
453 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
459 /* Ensure a composite type involving a zero-length array type
460 is a zero-length type not an incomplete type. */
461 if (d1_zero && d2_zero
462 && (t1_complete || t2_complete)
463 && !COMPLETE_TYPE_P (t1))
465 TYPE_SIZE (t1) = bitsize_zero_node;
466 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 t1 = c_build_qualified_type (t1, quals);
469 return build_type_attribute_variant (t1, attributes);
475 if (attributes != NULL)
477 /* Try harder not to create a new aggregate type. */
478 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
483 return build_type_attribute_variant (t1, attributes);
486 /* Function types: prefer the one that specified arg types.
487 If both do, merge the arg types. Also merge the return types. */
489 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490 tree p1 = TYPE_ARG_TYPES (t1);
491 tree p2 = TYPE_ARG_TYPES (t2);
496 /* Save space: see if the result is identical to one of the args. */
497 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498 return build_type_attribute_variant (t1, attributes);
499 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500 return build_type_attribute_variant (t2, attributes);
502 /* Simple way if one arg fails to specify argument types. */
503 if (TYPE_ARG_TYPES (t1) == 0)
505 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506 t1 = build_type_attribute_variant (t1, attributes);
507 return qualify_type (t1, t2);
509 if (TYPE_ARG_TYPES (t2) == 0)
511 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512 t1 = build_type_attribute_variant (t1, attributes);
513 return qualify_type (t1, t2);
516 /* If both args specify argument types, we must merge the two
517 lists, argument by argument. */
519 len = list_length (p1);
522 for (i = 0; i < len; i++)
523 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
528 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
530 /* A null type means arg type is not specified.
531 Take whatever the other function type has. */
532 if (TREE_VALUE (p1) == 0)
534 TREE_VALUE (n) = TREE_VALUE (p2);
537 if (TREE_VALUE (p2) == 0)
539 TREE_VALUE (n) = TREE_VALUE (p1);
543 /* Given wait (union {union wait *u; int *i} *)
544 and wait (union wait *),
545 prefer union wait * as type of parm. */
546 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
547 && TREE_VALUE (p1) != TREE_VALUE (p2))
550 tree mv2 = TREE_VALUE (p2);
551 if (mv2 && mv2 != error_mark_node
552 && TREE_CODE (mv2) != ARRAY_TYPE)
553 mv2 = TYPE_MAIN_VARIANT (mv2);
554 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
555 memb; memb = DECL_CHAIN (memb))
557 tree mv3 = TREE_TYPE (memb);
558 if (mv3 && mv3 != error_mark_node
559 && TREE_CODE (mv3) != ARRAY_TYPE)
560 mv3 = TYPE_MAIN_VARIANT (mv3);
561 if (comptypes (mv3, mv2))
563 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
565 pedwarn (input_location, OPT_pedantic,
566 "function types not truly compatible in ISO C");
571 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
572 && TREE_VALUE (p2) != TREE_VALUE (p1))
575 tree mv1 = TREE_VALUE (p1);
576 if (mv1 && mv1 != error_mark_node
577 && TREE_CODE (mv1) != ARRAY_TYPE)
578 mv1 = TYPE_MAIN_VARIANT (mv1);
579 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
580 memb; memb = DECL_CHAIN (memb))
582 tree mv3 = TREE_TYPE (memb);
583 if (mv3 && mv3 != error_mark_node
584 && TREE_CODE (mv3) != ARRAY_TYPE)
585 mv3 = TYPE_MAIN_VARIANT (mv3);
586 if (comptypes (mv3, mv1))
588 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
590 pedwarn (input_location, OPT_pedantic,
591 "function types not truly compatible in ISO C");
596 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
600 t1 = build_function_type (valtype, newargs);
601 t1 = qualify_type (t1, t2);
602 /* ... falls through ... */
606 return build_type_attribute_variant (t1, attributes);
611 /* Return the type of a conditional expression between pointers to
612 possibly differently qualified versions of compatible types.
614 We assume that comp_target_types has already been done and returned
615 nonzero; if that isn't so, this may crash. */
618 common_pointer_type (tree t1, tree t2)
621 tree pointed_to_1, mv1;
622 tree pointed_to_2, mv2;
624 unsigned target_quals;
625 addr_space_t as1, as2, as_common;
628 /* Save time if the two types are the same. */
630 if (t1 == t2) return t1;
632 /* If one type is nonsense, use the other. */
633 if (t1 == error_mark_node)
635 if (t2 == error_mark_node)
638 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
639 && TREE_CODE (t2) == POINTER_TYPE);
641 /* Merge the attributes. */
642 attributes = targetm.merge_type_attributes (t1, t2);
644 /* Find the composite type of the target types, and combine the
645 qualifiers of the two types' targets. Do not lose qualifiers on
646 array element types by taking the TYPE_MAIN_VARIANT. */
647 mv1 = pointed_to_1 = TREE_TYPE (t1);
648 mv2 = pointed_to_2 = TREE_TYPE (t2);
649 if (TREE_CODE (mv1) != ARRAY_TYPE)
650 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
651 if (TREE_CODE (mv2) != ARRAY_TYPE)
652 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
653 target = composite_type (mv1, mv2);
655 /* For function types do not merge const qualifiers, but drop them
656 if used inconsistently. The middle-end uses these to mark const
657 and noreturn functions. */
658 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
659 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
661 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
662 target_quals = (quals1 & quals2);
664 target_quals = (quals1 | quals2);
666 /* If the two named address spaces are different, determine the common
667 superset address space. This is guaranteed to exist due to the
668 assumption that comp_target_type returned non-zero. */
669 as1 = TYPE_ADDR_SPACE (pointed_to_1);
670 as2 = TYPE_ADDR_SPACE (pointed_to_2);
671 if (!addr_space_superset (as1, as2, &as_common))
674 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
676 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
677 return build_type_attribute_variant (t1, attributes);
680 /* Return the common type for two arithmetic types under the usual
681 arithmetic conversions. The default conversions have already been
682 applied, and enumerated types converted to their compatible integer
683 types. The resulting type is unqualified and has no attributes.
685 This is the type for the result of most arithmetic operations
686 if the operands have the given two types. */
689 c_common_type (tree t1, tree t2)
691 enum tree_code code1;
692 enum tree_code code2;
694 /* If one type is nonsense, use the other. */
695 if (t1 == error_mark_node)
697 if (t2 == error_mark_node)
700 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
701 t1 = TYPE_MAIN_VARIANT (t1);
703 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
704 t2 = TYPE_MAIN_VARIANT (t2);
706 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
707 t1 = build_type_attribute_variant (t1, NULL_TREE);
709 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
710 t2 = build_type_attribute_variant (t2, NULL_TREE);
712 /* Save time if the two types are the same. */
714 if (t1 == t2) return t1;
716 code1 = TREE_CODE (t1);
717 code2 = TREE_CODE (t2);
719 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
720 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
721 || code1 == INTEGER_TYPE);
722 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
723 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
724 || code2 == INTEGER_TYPE);
726 /* When one operand is a decimal float type, the other operand cannot be
727 a generic float type or a complex type. We also disallow vector types
729 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
730 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
732 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
734 error ("can%'t mix operands of decimal float and vector types");
735 return error_mark_node;
737 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
739 error ("can%'t mix operands of decimal float and complex types");
740 return error_mark_node;
742 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
744 error ("can%'t mix operands of decimal float and other float types");
745 return error_mark_node;
749 /* If one type is a vector type, return that type. (How the usual
750 arithmetic conversions apply to the vector types extension is not
751 precisely specified.) */
752 if (code1 == VECTOR_TYPE)
755 if (code2 == VECTOR_TYPE)
758 /* If one type is complex, form the common type of the non-complex
759 components, then make that complex. Use T1 or T2 if it is the
761 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
763 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
764 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
765 tree subtype = c_common_type (subtype1, subtype2);
767 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
769 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
772 return build_complex_type (subtype);
775 /* If only one is real, use it as the result. */
777 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
780 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
783 /* If both are real and either are decimal floating point types, use
784 the decimal floating point type with the greater precision. */
786 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
788 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
789 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
790 return dfloat128_type_node;
791 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
793 return dfloat64_type_node;
794 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
795 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
796 return dfloat32_type_node;
799 /* Deal with fixed-point types. */
800 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
802 unsigned int unsignedp = 0, satp = 0;
803 enum machine_mode m1, m2;
804 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
809 /* If one input type is saturating, the result type is saturating. */
810 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
813 /* If both fixed-point types are unsigned, the result type is unsigned.
814 When mixing fixed-point and integer types, follow the sign of the
816 Otherwise, the result type is signed. */
817 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
818 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
819 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
820 && TYPE_UNSIGNED (t1))
821 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
822 && TYPE_UNSIGNED (t2)))
825 /* The result type is signed. */
828 /* If the input type is unsigned, we need to convert to the
830 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
832 enum mode_class mclass = (enum mode_class) 0;
833 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
835 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
839 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
841 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
843 enum mode_class mclass = (enum mode_class) 0;
844 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
846 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
850 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
854 if (code1 == FIXED_POINT_TYPE)
856 fbit1 = GET_MODE_FBIT (m1);
857 ibit1 = GET_MODE_IBIT (m1);
862 /* Signed integers need to subtract one sign bit. */
863 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
866 if (code2 == FIXED_POINT_TYPE)
868 fbit2 = GET_MODE_FBIT (m2);
869 ibit2 = GET_MODE_IBIT (m2);
874 /* Signed integers need to subtract one sign bit. */
875 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
878 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
879 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
880 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
884 /* Both real or both integers; use the one with greater precision. */
886 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
888 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
891 /* Same precision. Prefer long longs to longs to ints when the
892 same precision, following the C99 rules on integer type rank
893 (which are equivalent to the C90 rules for C90 types). */
895 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
896 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
897 return long_long_unsigned_type_node;
899 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
902 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
903 return long_long_unsigned_type_node;
905 return long_long_integer_type_node;
908 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
910 return long_unsigned_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
915 /* But preserve unsignedness from the other type,
916 since long cannot hold all the values of an unsigned int. */
917 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
918 return long_unsigned_type_node;
920 return long_integer_type_node;
923 /* Likewise, prefer long double to double even if same size. */
924 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
925 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
926 return long_double_type_node;
928 /* Otherwise prefer the unsigned one. */
930 if (TYPE_UNSIGNED (t1))
936 /* Wrapper around c_common_type that is used by c-common.c and other
937 front end optimizations that remove promotions. ENUMERAL_TYPEs
938 are allowed here and are converted to their compatible integer types.
939 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
940 preferably a non-Boolean type as the common type. */
942 common_type (tree t1, tree t2)
944 if (TREE_CODE (t1) == ENUMERAL_TYPE)
945 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
946 if (TREE_CODE (t2) == ENUMERAL_TYPE)
947 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
949 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
950 if (TREE_CODE (t1) == BOOLEAN_TYPE
951 && TREE_CODE (t2) == BOOLEAN_TYPE)
952 return boolean_type_node;
954 /* If either type is BOOLEAN_TYPE, then return the other. */
955 if (TREE_CODE (t1) == BOOLEAN_TYPE)
957 if (TREE_CODE (t2) == BOOLEAN_TYPE)
960 return c_common_type (t1, t2);
963 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
964 or various other operations. Return 2 if they are compatible
965 but a warning may be needed if you use them together. */
968 comptypes (tree type1, tree type2)
970 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
973 val = comptypes_internal (type1, type2, NULL, NULL);
974 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
979 /* Like comptypes, but if it returns non-zero because enum and int are
980 compatible, it sets *ENUM_AND_INT_P to true. */
983 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
985 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
988 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
989 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
994 /* Like comptypes, but if it returns nonzero for different types, it
995 sets *DIFFERENT_TYPES_P to true. */
998 comptypes_check_different_types (tree type1, tree type2,
999 bool *different_types_p)
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1004 val = comptypes_internal (type1, type2, NULL, different_types_p);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1010 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1011 or various other operations. Return 2 if they are compatible
1012 but a warning may be needed if you use them together. If
1013 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1014 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1015 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1016 NULL, and the types are compatible but different enough not to be
1017 permitted in C1X typedef redeclarations, then this sets
1018 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1019 false, but may or may not be set if the types are incompatible.
1020 This differs from comptypes, in that we don't free the seen
1024 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1025 bool *different_types_p)
1027 const_tree t1 = type1;
1028 const_tree t2 = type2;
1031 /* Suppress errors caused by previously reported errors. */
1033 if (t1 == t2 || !t1 || !t2
1034 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1037 /* Enumerated types are compatible with integer types, but this is
1038 not transitive: two enumerated types in the same translation unit
1039 are compatible with each other only if they are the same type. */
1041 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1043 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1044 if (TREE_CODE (t2) != VOID_TYPE)
1046 if (enum_and_int_p != NULL)
1047 *enum_and_int_p = true;
1048 if (different_types_p != NULL)
1049 *different_types_p = true;
1052 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1054 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1055 if (TREE_CODE (t1) != VOID_TYPE)
1057 if (enum_and_int_p != NULL)
1058 *enum_and_int_p = true;
1059 if (different_types_p != NULL)
1060 *different_types_p = true;
1067 /* Different classes of types can't be compatible. */
1069 if (TREE_CODE (t1) != TREE_CODE (t2))
1072 /* Qualifiers must match. C99 6.7.3p9 */
1074 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1077 /* Allow for two different type nodes which have essentially the same
1078 definition. Note that we already checked for equality of the type
1079 qualifiers (just above). */
1081 if (TREE_CODE (t1) != ARRAY_TYPE
1082 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1085 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1086 if (!(attrval = comp_type_attributes (t1, t2)))
1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1092 switch (TREE_CODE (t1))
1095 /* Do not remove mode or aliasing information. */
1096 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1097 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1099 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1100 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1101 enum_and_int_p, different_types_p));
1105 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1111 tree d1 = TYPE_DOMAIN (t1);
1112 tree d2 = TYPE_DOMAIN (t2);
1113 bool d1_variable, d2_variable;
1114 bool d1_zero, d2_zero;
1117 /* Target types must match incl. qualifiers. */
1118 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1119 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1121 different_types_p)))
1124 if (different_types_p != NULL
1125 && (d1 == 0) != (d2 == 0))
1126 *different_types_p = true;
1127 /* Sizes must match unless one is missing or variable. */
1128 if (d1 == 0 || d2 == 0 || d1 == d2)
1131 d1_zero = !TYPE_MAX_VALUE (d1);
1132 d2_zero = !TYPE_MAX_VALUE (d2);
1134 d1_variable = (!d1_zero
1135 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1136 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1137 d2_variable = (!d2_zero
1138 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1139 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1140 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1141 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1143 if (different_types_p != NULL
1144 && d1_variable != d2_variable)
1145 *different_types_p = true;
1146 if (d1_variable || d2_variable)
1148 if (d1_zero && d2_zero)
1150 if (d1_zero || d2_zero
1151 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1152 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1161 if (val != 1 && !same_translation_unit_p (t1, t2))
1163 tree a1 = TYPE_ATTRIBUTES (t1);
1164 tree a2 = TYPE_ATTRIBUTES (t2);
1166 if (! attribute_list_contained (a1, a2)
1167 && ! attribute_list_contained (a2, a1))
1171 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1173 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1179 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1180 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p, different_types_p));
1187 return attrval == 2 && val == 1 ? 2 : val;
1190 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1191 their qualifiers, except for named address spaces. If the pointers point to
1192 different named addresses, then we must determine if one address space is a
1193 subset of the other. */
1196 comp_target_types (location_t location, tree ttl, tree ttr)
1199 tree mvl = TREE_TYPE (ttl);
1200 tree mvr = TREE_TYPE (ttr);
1201 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1202 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1203 addr_space_t as_common;
1204 bool enum_and_int_p;
1206 /* Fail if pointers point to incompatible address spaces. */
1207 if (!addr_space_superset (asl, asr, &as_common))
1210 /* Do not lose qualifiers on element types of array types that are
1211 pointer targets by taking their TYPE_MAIN_VARIANT. */
1212 if (TREE_CODE (mvl) != ARRAY_TYPE)
1213 mvl = TYPE_MAIN_VARIANT (mvl);
1214 if (TREE_CODE (mvr) != ARRAY_TYPE)
1215 mvr = TYPE_MAIN_VARIANT (mvr);
1216 enum_and_int_p = false;
1217 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1220 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1222 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1223 warning_at (location, OPT_Wc___compat,
1224 "pointer target types incompatible in C++");
1229 /* Subroutines of `comptypes'. */
1231 /* Determine whether two trees derive from the same translation unit.
1232 If the CONTEXT chain ends in a null, that tree's context is still
1233 being parsed, so if two trees have context chains ending in null,
1234 they're in the same translation unit. */
1236 same_translation_unit_p (const_tree t1, const_tree t2)
1238 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1239 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1241 case tcc_declaration:
1242 t1 = DECL_CONTEXT (t1); break;
1244 t1 = TYPE_CONTEXT (t1); break;
1245 case tcc_exceptional:
1246 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1247 default: gcc_unreachable ();
1250 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1251 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1253 case tcc_declaration:
1254 t2 = DECL_CONTEXT (t2); break;
1256 t2 = TYPE_CONTEXT (t2); break;
1257 case tcc_exceptional:
1258 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1259 default: gcc_unreachable ();
1265 /* Allocate the seen two types, assuming that they are compatible. */
1267 static struct tagged_tu_seen_cache *
1268 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1270 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1271 tu->next = tagged_tu_seen_base;
1275 tagged_tu_seen_base = tu;
1277 /* The C standard says that two structures in different translation
1278 units are compatible with each other only if the types of their
1279 fields are compatible (among other things). We assume that they
1280 are compatible until proven otherwise when building the cache.
1281 An example where this can occur is:
1286 If we are comparing this against a similar struct in another TU,
1287 and did not assume they were compatible, we end up with an infinite
1293 /* Free the seen types until we get to TU_TIL. */
1296 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1298 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1299 while (tu != tu_til)
1301 const struct tagged_tu_seen_cache *const tu1
1302 = (const struct tagged_tu_seen_cache *) tu;
1304 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1306 tagged_tu_seen_base = tu_til;
1309 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1310 compatible. If the two types are not the same (which has been
1311 checked earlier), this can only happen when multiple translation
1312 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1313 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1314 comptypes_internal. */
1317 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1318 bool *enum_and_int_p, bool *different_types_p)
1321 bool needs_warning = false;
1323 /* We have to verify that the tags of the types are the same. This
1324 is harder than it looks because this may be a typedef, so we have
1325 to go look at the original type. It may even be a typedef of a
1327 In the case of compiler-created builtin structs the TYPE_DECL
1328 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1329 while (TYPE_NAME (t1)
1330 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1331 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1332 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1334 while (TYPE_NAME (t2)
1335 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1336 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1337 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1339 /* C90 didn't have the requirement that the two tags be the same. */
1340 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1343 /* C90 didn't say what happened if one or both of the types were
1344 incomplete; we choose to follow C99 rules here, which is that they
1346 if (TYPE_SIZE (t1) == NULL
1347 || TYPE_SIZE (t2) == NULL)
1351 const struct tagged_tu_seen_cache * tts_i;
1352 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1353 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1357 switch (TREE_CODE (t1))
1361 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1362 /* Speed up the case where the type values are in the same order. */
1363 tree tv1 = TYPE_VALUES (t1);
1364 tree tv2 = TYPE_VALUES (t2);
1371 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1373 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1375 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1382 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1386 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1392 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1398 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1400 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1402 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1413 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1414 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1420 /* Speed up the common case where the fields are in the same order. */
1421 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1422 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1426 if (DECL_NAME (s1) != DECL_NAME (s2))
1428 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1429 enum_and_int_p, different_types_p);
1431 if (result != 1 && !DECL_NAME (s1))
1439 needs_warning = true;
1441 if (TREE_CODE (s1) == FIELD_DECL
1442 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1443 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1451 tu->val = needs_warning ? 2 : 1;
1455 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1459 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1460 if (DECL_NAME (s1) == DECL_NAME (s2))
1464 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1468 if (result != 1 && !DECL_NAME (s1))
1476 needs_warning = true;
1478 if (TREE_CODE (s1) == FIELD_DECL
1479 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1480 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1492 tu->val = needs_warning ? 2 : 10;
1498 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1500 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1505 if (TREE_CODE (s1) != TREE_CODE (s2)
1506 || DECL_NAME (s1) != DECL_NAME (s2))
1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509 enum_and_int_p, different_types_p);
1513 needs_warning = true;
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1523 tu->val = needs_warning ? 2 : 1;
1532 /* Return 1 if two function types F1 and F2 are compatible.
1533 If either type specifies no argument types,
1534 the other must specify a fixed number of self-promoting arg types.
1535 Otherwise, if one type specifies only the number of arguments,
1536 the other must specify that number of self-promoting arg types.
1537 Otherwise, the argument types must match.
1538 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1541 function_types_compatible_p (const_tree f1, const_tree f2,
1542 bool *enum_and_int_p, bool *different_types_p)
1545 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1550 ret1 = TREE_TYPE (f1);
1551 ret2 = TREE_TYPE (f2);
1553 /* 'volatile' qualifiers on a function's return type used to mean
1554 the function is noreturn. */
1555 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1556 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1557 if (TYPE_VOLATILE (ret1))
1558 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1559 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1560 if (TYPE_VOLATILE (ret2))
1561 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1562 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1563 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1567 args1 = TYPE_ARG_TYPES (f1);
1568 args2 = TYPE_ARG_TYPES (f2);
1570 if (different_types_p != NULL
1571 && (args1 == 0) != (args2 == 0))
1572 *different_types_p = true;
1574 /* An unspecified parmlist matches any specified parmlist
1575 whose argument types don't need default promotions. */
1579 if (!self_promoting_args_p (args2))
1581 /* If one of these types comes from a non-prototype fn definition,
1582 compare that with the other type's arglist.
1583 If they don't match, ask for a warning (but no error). */
1584 if (TYPE_ACTUAL_ARG_TYPES (f1)
1585 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1586 enum_and_int_p, different_types_p))
1592 if (!self_promoting_args_p (args1))
1594 if (TYPE_ACTUAL_ARG_TYPES (f2)
1595 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1596 enum_and_int_p, different_types_p))
1601 /* Both types have argument lists: compare them and propagate results. */
1602 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1604 return val1 != 1 ? val1 : val;
1607 /* Check two lists of types for compatibility, returning 0 for
1608 incompatible, 1 for compatible, or 2 for compatible with
1609 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1610 comptypes_internal. */
1613 type_lists_compatible_p (const_tree args1, const_tree args2,
1614 bool *enum_and_int_p, bool *different_types_p)
1616 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1622 tree a1, mv1, a2, mv2;
1623 if (args1 == 0 && args2 == 0)
1625 /* If one list is shorter than the other,
1626 they fail to match. */
1627 if (args1 == 0 || args2 == 0)
1629 mv1 = a1 = TREE_VALUE (args1);
1630 mv2 = a2 = TREE_VALUE (args2);
1631 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1632 mv1 = TYPE_MAIN_VARIANT (mv1);
1633 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1634 mv2 = TYPE_MAIN_VARIANT (mv2);
1635 /* A null pointer instead of a type
1636 means there is supposed to be an argument
1637 but nothing is specified about what type it has.
1638 So match anything that self-promotes. */
1639 if (different_types_p != NULL
1640 && (a1 == 0) != (a2 == 0))
1641 *different_types_p = true;
1644 if (c_type_promotes_to (a2) != a2)
1649 if (c_type_promotes_to (a1) != a1)
1652 /* If one of the lists has an error marker, ignore this arg. */
1653 else if (TREE_CODE (a1) == ERROR_MARK
1654 || TREE_CODE (a2) == ERROR_MARK)
1656 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1657 different_types_p)))
1659 if (different_types_p != NULL)
1660 *different_types_p = true;
1661 /* Allow wait (union {union wait *u; int *i} *)
1662 and wait (union wait *) to be compatible. */
1663 if (TREE_CODE (a1) == UNION_TYPE
1664 && (TYPE_NAME (a1) == 0
1665 || TYPE_TRANSPARENT_AGGR (a1))
1666 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1667 && tree_int_cst_equal (TYPE_SIZE (a1),
1671 for (memb = TYPE_FIELDS (a1);
1672 memb; memb = DECL_CHAIN (memb))
1674 tree mv3 = TREE_TYPE (memb);
1675 if (mv3 && mv3 != error_mark_node
1676 && TREE_CODE (mv3) != ARRAY_TYPE)
1677 mv3 = TYPE_MAIN_VARIANT (mv3);
1678 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1685 else if (TREE_CODE (a2) == UNION_TYPE
1686 && (TYPE_NAME (a2) == 0
1687 || TYPE_TRANSPARENT_AGGR (a2))
1688 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1689 && tree_int_cst_equal (TYPE_SIZE (a2),
1693 for (memb = TYPE_FIELDS (a2);
1694 memb; memb = DECL_CHAIN (memb))
1696 tree mv3 = TREE_TYPE (memb);
1697 if (mv3 && mv3 != error_mark_node
1698 && TREE_CODE (mv3) != ARRAY_TYPE)
1699 mv3 = TYPE_MAIN_VARIANT (mv3);
1700 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1711 /* comptypes said ok, but record if it said to warn. */
1715 args1 = TREE_CHAIN (args1);
1716 args2 = TREE_CHAIN (args2);
1720 /* Compute the size to increment a pointer by. */
1723 c_size_in_bytes (const_tree type)
1725 enum tree_code code = TREE_CODE (type);
1727 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1728 return size_one_node;
1730 if (!COMPLETE_OR_VOID_TYPE_P (type))
1732 error ("arithmetic on pointer to an incomplete type");
1733 return size_one_node;
1736 /* Convert in case a char is more than one unit. */
1737 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1738 size_int (TYPE_PRECISION (char_type_node)
1742 /* Return either DECL or its known constant value (if it has one). */
1745 decl_constant_value (tree decl)
1747 if (/* Don't change a variable array bound or initial value to a constant
1748 in a place where a variable is invalid. Note that DECL_INITIAL
1749 isn't valid for a PARM_DECL. */
1750 current_function_decl != 0
1751 && TREE_CODE (decl) != PARM_DECL
1752 && !TREE_THIS_VOLATILE (decl)
1753 && TREE_READONLY (decl)
1754 && DECL_INITIAL (decl) != 0
1755 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1756 /* This is invalid if initial value is not constant.
1757 If it has either a function call, a memory reference,
1758 or a variable, then re-evaluating it could give different results. */
1759 && TREE_CONSTANT (DECL_INITIAL (decl))
1760 /* Check for cases where this is sub-optimal, even though valid. */
1761 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1762 return DECL_INITIAL (decl);
1766 /* Convert the array expression EXP to a pointer. */
1768 array_to_pointer_conversion (location_t loc, tree exp)
1770 tree orig_exp = exp;
1771 tree type = TREE_TYPE (exp);
1773 tree restype = TREE_TYPE (type);
1776 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1778 STRIP_TYPE_NOPS (exp);
1780 if (TREE_NO_WARNING (orig_exp))
1781 TREE_NO_WARNING (exp) = 1;
1783 ptrtype = build_pointer_type (restype);
1785 if (TREE_CODE (exp) == INDIRECT_REF)
1786 return convert (ptrtype, TREE_OPERAND (exp, 0));
1788 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1789 return convert (ptrtype, adr);
1792 /* Convert the function expression EXP to a pointer. */
1794 function_to_pointer_conversion (location_t loc, tree exp)
1796 tree orig_exp = exp;
1798 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1800 STRIP_TYPE_NOPS (exp);
1802 if (TREE_NO_WARNING (orig_exp))
1803 TREE_NO_WARNING (exp) = 1;
1805 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1808 /* Mark EXP as read, not just set, for set but not used -Wunused
1809 warning purposes. */
1812 mark_exp_read (tree exp)
1814 switch (TREE_CODE (exp))
1818 DECL_READ_P (exp) = 1;
1827 mark_exp_read (TREE_OPERAND (exp, 0));
1830 case C_MAYBE_CONST_EXPR:
1831 mark_exp_read (TREE_OPERAND (exp, 1));
1838 /* Perform the default conversion of arrays and functions to pointers.
1839 Return the result of converting EXP. For any other expression, just
1842 LOC is the location of the expression. */
1845 default_function_array_conversion (location_t loc, struct c_expr exp)
1847 tree orig_exp = exp.value;
1848 tree type = TREE_TYPE (exp.value);
1849 enum tree_code code = TREE_CODE (type);
1855 bool not_lvalue = false;
1856 bool lvalue_array_p;
1858 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1859 || CONVERT_EXPR_P (exp.value))
1860 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1862 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1864 exp.value = TREE_OPERAND (exp.value, 0);
1867 if (TREE_NO_WARNING (orig_exp))
1868 TREE_NO_WARNING (exp.value) = 1;
1870 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1871 if (!flag_isoc99 && !lvalue_array_p)
1873 /* Before C99, non-lvalue arrays do not decay to pointers.
1874 Normally, using such an array would be invalid; but it can
1875 be used correctly inside sizeof or as a statement expression.
1876 Thus, do not give an error here; an error will result later. */
1880 exp.value = array_to_pointer_conversion (loc, exp.value);
1884 exp.value = function_to_pointer_conversion (loc, exp.value);
1894 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1896 mark_exp_read (exp.value);
1897 return default_function_array_conversion (loc, exp);
1900 /* EXP is an expression of integer type. Apply the integer promotions
1901 to it and return the promoted value. */
1904 perform_integral_promotions (tree exp)
1906 tree type = TREE_TYPE (exp);
1907 enum tree_code code = TREE_CODE (type);
1909 gcc_assert (INTEGRAL_TYPE_P (type));
1911 /* Normally convert enums to int,
1912 but convert wide enums to something wider. */
1913 if (code == ENUMERAL_TYPE)
1915 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1916 TYPE_PRECISION (integer_type_node)),
1917 ((TYPE_PRECISION (type)
1918 >= TYPE_PRECISION (integer_type_node))
1919 && TYPE_UNSIGNED (type)));
1921 return convert (type, exp);
1924 /* ??? This should no longer be needed now bit-fields have their
1926 if (TREE_CODE (exp) == COMPONENT_REF
1927 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1928 /* If it's thinner than an int, promote it like a
1929 c_promoting_integer_type_p, otherwise leave it alone. */
1930 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1931 TYPE_PRECISION (integer_type_node)))
1932 return convert (integer_type_node, exp);
1934 if (c_promoting_integer_type_p (type))
1936 /* Preserve unsignedness if not really getting any wider. */
1937 if (TYPE_UNSIGNED (type)
1938 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1939 return convert (unsigned_type_node, exp);
1941 return convert (integer_type_node, exp);
1948 /* Perform default promotions for C data used in expressions.
1949 Enumeral types or short or char are converted to int.
1950 In addition, manifest constants symbols are replaced by their values. */
1953 default_conversion (tree exp)
1956 tree type = TREE_TYPE (exp);
1957 enum tree_code code = TREE_CODE (type);
1960 mark_exp_read (exp);
1962 /* Functions and arrays have been converted during parsing. */
1963 gcc_assert (code != FUNCTION_TYPE);
1964 if (code == ARRAY_TYPE)
1967 /* Constants can be used directly unless they're not loadable. */
1968 if (TREE_CODE (exp) == CONST_DECL)
1969 exp = DECL_INITIAL (exp);
1971 /* Strip no-op conversions. */
1973 STRIP_TYPE_NOPS (exp);
1975 if (TREE_NO_WARNING (orig_exp))
1976 TREE_NO_WARNING (exp) = 1;
1978 if (code == VOID_TYPE)
1980 error ("void value not ignored as it ought to be");
1981 return error_mark_node;
1984 exp = require_complete_type (exp);
1985 if (exp == error_mark_node)
1986 return error_mark_node;
1988 promoted_type = targetm.promoted_type (type);
1990 return convert (promoted_type, exp);
1992 if (INTEGRAL_TYPE_P (type))
1993 return perform_integral_promotions (exp);
1998 /* Look up COMPONENT in a structure or union TYPE.
2000 If the component name is not found, returns NULL_TREE. Otherwise,
2001 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2002 stepping down the chain to the component, which is in the last
2003 TREE_VALUE of the list. Normally the list is of length one, but if
2004 the component is embedded within (nested) anonymous structures or
2005 unions, the list steps down the chain to the component. */
2008 lookup_field (tree type, tree component)
2012 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2013 to the field elements. Use a binary search on this array to quickly
2014 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2015 will always be set for structures which have many elements. */
2017 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2020 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2022 field = TYPE_FIELDS (type);
2024 top = TYPE_LANG_SPECIFIC (type)->s->len;
2025 while (top - bot > 1)
2027 half = (top - bot + 1) >> 1;
2028 field = field_array[bot+half];
2030 if (DECL_NAME (field) == NULL_TREE)
2032 /* Step through all anon unions in linear fashion. */
2033 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2035 field = field_array[bot++];
2036 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2037 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2039 tree anon = lookup_field (TREE_TYPE (field), component);
2042 return tree_cons (NULL_TREE, field, anon);
2044 /* The Plan 9 compiler permits referring
2045 directly to an anonymous struct/union field
2046 using a typedef name. */
2047 if (flag_plan9_extensions
2048 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2049 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2051 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2057 /* Entire record is only anon unions. */
2061 /* Restart the binary search, with new lower bound. */
2065 if (DECL_NAME (field) == component)
2067 if (DECL_NAME (field) < component)
2073 if (DECL_NAME (field_array[bot]) == component)
2074 field = field_array[bot];
2075 else if (DECL_NAME (field) != component)
2080 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2082 if (DECL_NAME (field) == NULL_TREE
2083 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2084 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2086 tree anon = lookup_field (TREE_TYPE (field), component);
2089 return tree_cons (NULL_TREE, field, anon);
2091 /* The Plan 9 compiler permits referring directly to an
2092 anonymous struct/union field using a typedef
2094 if (flag_plan9_extensions
2095 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2096 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2097 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2102 if (DECL_NAME (field) == component)
2106 if (field == NULL_TREE)
2110 return tree_cons (NULL_TREE, field, NULL_TREE);
2113 /* Make an expression to refer to the COMPONENT field of structure or
2114 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2115 location of the COMPONENT_REF. */
2118 build_component_ref (location_t loc, tree datum, tree component)
2120 tree type = TREE_TYPE (datum);
2121 enum tree_code code = TREE_CODE (type);
2124 bool datum_lvalue = lvalue_p (datum);
2126 if (!objc_is_public (datum, component))
2127 return error_mark_node;
2129 /* Detect Objective-C property syntax object.property. */
2130 if (c_dialect_objc ()
2131 && (ref = objc_maybe_build_component_ref (datum, component)))
2134 /* See if there is a field or component with name COMPONENT. */
2136 if (code == RECORD_TYPE || code == UNION_TYPE)
2138 if (!COMPLETE_TYPE_P (type))
2140 c_incomplete_type_error (NULL_TREE, type);
2141 return error_mark_node;
2144 field = lookup_field (type, component);
2148 error_at (loc, "%qT has no member named %qE", type, component);
2149 return error_mark_node;
2152 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2153 This might be better solved in future the way the C++ front
2154 end does it - by giving the anonymous entities each a
2155 separate name and type, and then have build_component_ref
2156 recursively call itself. We can't do that here. */
2159 tree subdatum = TREE_VALUE (field);
2162 bool use_datum_quals;
2164 if (TREE_TYPE (subdatum) == error_mark_node)
2165 return error_mark_node;
2167 /* If this is an rvalue, it does not have qualifiers in C
2168 standard terms and we must avoid propagating such
2169 qualifiers down to a non-lvalue array that is then
2170 converted to a pointer. */
2171 use_datum_quals = (datum_lvalue
2172 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2174 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2175 if (use_datum_quals)
2176 quals |= TYPE_QUALS (TREE_TYPE (datum));
2177 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2179 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2181 SET_EXPR_LOCATION (ref, loc);
2182 if (TREE_READONLY (subdatum)
2183 || (use_datum_quals && TREE_READONLY (datum)))
2184 TREE_READONLY (ref) = 1;
2185 if (TREE_THIS_VOLATILE (subdatum)
2186 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2187 TREE_THIS_VOLATILE (ref) = 1;
2189 if (TREE_DEPRECATED (subdatum))
2190 warn_deprecated_use (subdatum, NULL_TREE);
2194 field = TREE_CHAIN (field);
2200 else if (code != ERROR_MARK)
2202 "request for member %qE in something not a structure or union",
2205 return error_mark_node;
2208 /* Given an expression PTR for a pointer, return an expression
2209 for the value pointed to.
2210 ERRORSTRING is the name of the operator to appear in error messages.
2212 LOC is the location to use for the generated tree. */
2215 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2217 tree pointer = default_conversion (ptr);
2218 tree type = TREE_TYPE (pointer);
2221 if (TREE_CODE (type) == POINTER_TYPE)
2223 if (CONVERT_EXPR_P (pointer)
2224 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2226 /* If a warning is issued, mark it to avoid duplicates from
2227 the backend. This only needs to be done at
2228 warn_strict_aliasing > 2. */
2229 if (warn_strict_aliasing > 2)
2230 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2231 type, TREE_OPERAND (pointer, 0)))
2232 TREE_NO_WARNING (pointer) = 1;
2235 if (TREE_CODE (pointer) == ADDR_EXPR
2236 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2237 == TREE_TYPE (type)))
2239 ref = TREE_OPERAND (pointer, 0);
2240 protected_set_expr_location (ref, loc);
2245 tree t = TREE_TYPE (type);
2247 ref = build1 (INDIRECT_REF, t, pointer);
2249 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2251 error_at (loc, "dereferencing pointer to incomplete type");
2252 return error_mark_node;
2254 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2255 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2257 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2258 so that we get the proper error message if the result is used
2259 to assign to. Also, &* is supposed to be a no-op.
2260 And ANSI C seems to specify that the type of the result
2261 should be the const type. */
2262 /* A de-reference of a pointer to const is not a const. It is valid
2263 to change it via some other pointer. */
2264 TREE_READONLY (ref) = TYPE_READONLY (t);
2265 TREE_SIDE_EFFECTS (ref)
2266 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2267 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2268 protected_set_expr_location (ref, loc);
2272 else if (TREE_CODE (pointer) != ERROR_MARK)
2273 invalid_indirection_error (loc, type, errstring);
2275 return error_mark_node;
2278 /* This handles expressions of the form "a[i]", which denotes
2281 This is logically equivalent in C to *(a+i), but we may do it differently.
2282 If A is a variable or a member, we generate a primitive ARRAY_REF.
2283 This avoids forcing the array out of registers, and can work on
2284 arrays that are not lvalues (for example, members of structures returned
2287 For vector types, allow vector[i] but not i[vector], and create
2288 *(((type*)&vectortype) + i) for the expression.
2290 LOC is the location to use for the returned expression. */
2293 build_array_ref (location_t loc, tree array, tree index)
2296 bool swapped = false;
2297 if (TREE_TYPE (array) == error_mark_node
2298 || TREE_TYPE (index) == error_mark_node)
2299 return error_mark_node;
2301 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2302 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2303 /* Allow vector[index] but not index[vector]. */
2304 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2307 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2308 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2311 "subscripted value is neither array nor pointer nor vector");
2313 return error_mark_node;
2321 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2323 error_at (loc, "array subscript is not an integer");
2324 return error_mark_node;
2327 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2329 error_at (loc, "subscripted value is pointer to function");
2330 return error_mark_node;
2333 /* ??? Existing practice has been to warn only when the char
2334 index is syntactically the index, not for char[array]. */
2336 warn_array_subscript_with_type_char (index);
2338 /* Apply default promotions *after* noticing character types. */
2339 index = default_conversion (index);
2341 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2343 /* For vector[index], convert the vector to a
2344 pointer of the underlying type. */
2345 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2347 tree type = TREE_TYPE (array);
2350 if (TREE_CODE (index) == INTEGER_CST)
2351 if (!host_integerp (index, 1)
2352 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2353 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2354 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2356 c_common_mark_addressable_vec (array);
2357 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2358 type = build_pointer_type (type);
2359 type1 = build_pointer_type (TREE_TYPE (array));
2360 array = build1 (ADDR_EXPR, type1, array);
2361 array = convert (type, array);
2364 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2368 /* An array that is indexed by a non-constant
2369 cannot be stored in a register; we must be able to do
2370 address arithmetic on its address.
2371 Likewise an array of elements of variable size. */
2372 if (TREE_CODE (index) != INTEGER_CST
2373 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2374 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2376 if (!c_mark_addressable (array))
2377 return error_mark_node;
2379 /* An array that is indexed by a constant value which is not within
2380 the array bounds cannot be stored in a register either; because we
2381 would get a crash in store_bit_field/extract_bit_field when trying
2382 to access a non-existent part of the register. */
2383 if (TREE_CODE (index) == INTEGER_CST
2384 && TYPE_DOMAIN (TREE_TYPE (array))
2385 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2387 if (!c_mark_addressable (array))
2388 return error_mark_node;
2394 while (TREE_CODE (foo) == COMPONENT_REF)
2395 foo = TREE_OPERAND (foo, 0);
2396 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2397 pedwarn (loc, OPT_pedantic,
2398 "ISO C forbids subscripting %<register%> array");
2399 else if (!flag_isoc99 && !lvalue_p (foo))
2400 pedwarn (loc, OPT_pedantic,
2401 "ISO C90 forbids subscripting non-lvalue array");
2404 type = TREE_TYPE (TREE_TYPE (array));
2405 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2406 /* Array ref is const/volatile if the array elements are
2407 or if the array is. */
2408 TREE_READONLY (rval)
2409 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2410 | TREE_READONLY (array));
2411 TREE_SIDE_EFFECTS (rval)
2412 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2413 | TREE_SIDE_EFFECTS (array));
2414 TREE_THIS_VOLATILE (rval)
2415 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2416 /* This was added by rms on 16 Nov 91.
2417 It fixes vol struct foo *a; a->elts[1]
2418 in an inline function.
2419 Hope it doesn't break something else. */
2420 | TREE_THIS_VOLATILE (array));
2421 ret = require_complete_type (rval);
2422 protected_set_expr_location (ret, loc);
2427 tree ar = default_conversion (array);
2429 if (ar == error_mark_node)
2432 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2433 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2435 return build_indirect_ref
2436 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2441 /* Build an external reference to identifier ID. FUN indicates
2442 whether this will be used for a function call. LOC is the source
2443 location of the identifier. This sets *TYPE to the type of the
2444 identifier, which is not the same as the type of the returned value
2445 for CONST_DECLs defined as enum constants. If the type of the
2446 identifier is not available, *TYPE is set to NULL. */
2448 build_external_ref (location_t loc, tree id, int fun, tree *type)
2451 tree decl = lookup_name (id);
2453 /* In Objective-C, an instance variable (ivar) may be preferred to
2454 whatever lookup_name() found. */
2455 decl = objc_lookup_ivar (decl, id);
2458 if (decl && decl != error_mark_node)
2461 *type = TREE_TYPE (ref);
2464 /* Implicit function declaration. */
2465 ref = implicitly_declare (loc, id);
2466 else if (decl == error_mark_node)
2467 /* Don't complain about something that's already been
2468 complained about. */
2469 return error_mark_node;
2472 undeclared_variable (loc, id);
2473 return error_mark_node;
2476 if (TREE_TYPE (ref) == error_mark_node)
2477 return error_mark_node;
2479 if (TREE_DEPRECATED (ref))
2480 warn_deprecated_use (ref, NULL_TREE);
2482 /* Recursive call does not count as usage. */
2483 if (ref != current_function_decl)
2485 TREE_USED (ref) = 1;
2488 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2490 if (!in_sizeof && !in_typeof)
2491 C_DECL_USED (ref) = 1;
2492 else if (DECL_INITIAL (ref) == 0
2493 && DECL_EXTERNAL (ref)
2494 && !TREE_PUBLIC (ref))
2495 record_maybe_used_decl (ref);
2498 if (TREE_CODE (ref) == CONST_DECL)
2500 used_types_insert (TREE_TYPE (ref));
2503 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2504 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2506 warning_at (loc, OPT_Wc___compat,
2507 ("enum constant defined in struct or union "
2508 "is not visible in C++"));
2509 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2512 ref = DECL_INITIAL (ref);
2513 TREE_CONSTANT (ref) = 1;
2515 else if (current_function_decl != 0
2516 && !DECL_FILE_SCOPE_P (current_function_decl)
2517 && (TREE_CODE (ref) == VAR_DECL
2518 || TREE_CODE (ref) == PARM_DECL
2519 || TREE_CODE (ref) == FUNCTION_DECL))
2521 tree context = decl_function_context (ref);
2523 if (context != 0 && context != current_function_decl)
2524 DECL_NONLOCAL (ref) = 1;
2526 /* C99 6.7.4p3: An inline definition of a function with external
2527 linkage ... shall not contain a reference to an identifier with
2528 internal linkage. */
2529 else if (current_function_decl != 0
2530 && DECL_DECLARED_INLINE_P (current_function_decl)
2531 && DECL_EXTERNAL (current_function_decl)
2532 && VAR_OR_FUNCTION_DECL_P (ref)
2533 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2534 && ! TREE_PUBLIC (ref)
2535 && DECL_CONTEXT (ref) != current_function_decl)
2536 record_inline_static (loc, current_function_decl, ref,
2542 /* Record details of decls possibly used inside sizeof or typeof. */
2543 struct maybe_used_decl
2547 /* The level seen at (in_sizeof + in_typeof). */
2549 /* The next one at this level or above, or NULL. */
2550 struct maybe_used_decl *next;
2553 static struct maybe_used_decl *maybe_used_decls;
2555 /* Record that DECL, an undefined static function reference seen
2556 inside sizeof or typeof, might be used if the operand of sizeof is
2557 a VLA type or the operand of typeof is a variably modified
2561 record_maybe_used_decl (tree decl)
2563 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2565 t->level = in_sizeof + in_typeof;
2566 t->next = maybe_used_decls;
2567 maybe_used_decls = t;
2570 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2571 USED is false, just discard them. If it is true, mark them used
2572 (if no longer inside sizeof or typeof) or move them to the next
2573 level up (if still inside sizeof or typeof). */
2576 pop_maybe_used (bool used)
2578 struct maybe_used_decl *p = maybe_used_decls;
2579 int cur_level = in_sizeof + in_typeof;
2580 while (p && p->level > cur_level)
2585 C_DECL_USED (p->decl) = 1;
2587 p->level = cur_level;
2591 if (!used || cur_level == 0)
2592 maybe_used_decls = p;
2595 /* Return the result of sizeof applied to EXPR. */
2598 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2601 if (expr.value == error_mark_node)
2603 ret.value = error_mark_node;
2604 ret.original_code = ERROR_MARK;
2605 ret.original_type = NULL;
2606 pop_maybe_used (false);
2610 bool expr_const_operands = true;
2611 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2612 &expr_const_operands);
2613 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2614 ret.original_code = ERROR_MARK;
2615 ret.original_type = NULL;
2616 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2618 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2619 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2620 folded_expr, ret.value);
2621 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2622 SET_EXPR_LOCATION (ret.value, loc);
2624 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2629 /* Return the result of sizeof applied to T, a structure for the type
2630 name passed to sizeof (rather than the type itself). LOC is the
2631 location of the original expression. */
2634 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2638 tree type_expr = NULL_TREE;
2639 bool type_expr_const = true;
2640 type = groktypename (t, &type_expr, &type_expr_const);
2641 ret.value = c_sizeof (loc, type);
2642 ret.original_code = ERROR_MARK;
2643 ret.original_type = NULL;
2644 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2645 && c_vla_type_p (type))
2647 /* If the type is a [*] array, it is a VLA but is represented as
2648 having a size of zero. In such a case we must ensure that
2649 the result of sizeof does not get folded to a constant by
2650 c_fully_fold, because if the size is evaluated the result is
2651 not constant and so constraints on zero or negative size
2652 arrays must not be applied when this sizeof call is inside
2653 another array declarator. */
2655 type_expr = integer_zero_node;
2656 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2657 type_expr, ret.value);
2658 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2660 pop_maybe_used (type != error_mark_node
2661 ? C_TYPE_VARIABLE_SIZE (type) : false);
2665 /* Build a function call to function FUNCTION with parameters PARAMS.
2666 The function call is at LOC.
2667 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2668 TREE_VALUE of each node is a parameter-expression.
2669 FUNCTION's data type may be a function type or a pointer-to-function. */
2672 build_function_call (location_t loc, tree function, tree params)
2677 vec = VEC_alloc (tree, gc, list_length (params));
2678 for (; params; params = TREE_CHAIN (params))
2679 VEC_quick_push (tree, vec, TREE_VALUE (params));
2680 ret = build_function_call_vec (loc, function, vec, NULL);
2681 VEC_free (tree, gc, vec);
2685 /* Build a function call to function FUNCTION with parameters PARAMS.
2686 ORIGTYPES, if not NULL, is a vector of types; each element is
2687 either NULL or the original type of the corresponding element in
2688 PARAMS. The original type may differ from TREE_TYPE of the
2689 parameter for enums. FUNCTION's data type may be a function type
2690 or pointer-to-function. This function changes the elements of
2694 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2695 VEC(tree,gc) *origtypes)
2697 tree fntype, fundecl = 0;
2698 tree name = NULL_TREE, result;
2704 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2705 STRIP_TYPE_NOPS (function);
2707 /* Convert anything with function type to a pointer-to-function. */
2708 if (TREE_CODE (function) == FUNCTION_DECL)
2710 /* Implement type-directed function overloading for builtins.
2711 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2712 handle all the type checking. The result is a complete expression
2713 that implements this function call. */
2714 tem = resolve_overloaded_builtin (loc, function, params);
2718 name = DECL_NAME (function);
2720 /* Atomic functions have type checking/casting already done. They are
2721 often rewritten and don't match the original parameter list. */
2722 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2725 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2726 function = function_to_pointer_conversion (loc, function);
2728 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2729 expressions, like those used for ObjC messenger dispatches. */
2730 if (!VEC_empty (tree, params))
2731 function = objc_rewrite_function_call (function,
2732 VEC_index (tree, params, 0));
2734 function = c_fully_fold (function, false, NULL);
2736 fntype = TREE_TYPE (function);
2738 if (TREE_CODE (fntype) == ERROR_MARK)
2739 return error_mark_node;
2741 if (!(TREE_CODE (fntype) == POINTER_TYPE
2742 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2744 error_at (loc, "called object %qE is not a function", function);
2745 return error_mark_node;
2748 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2749 current_function_returns_abnormally = 1;
2751 /* fntype now gets the type of function pointed to. */
2752 fntype = TREE_TYPE (fntype);
2754 /* Convert the parameters to the types declared in the
2755 function prototype, or apply default promotions. */
2757 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2760 return error_mark_node;
2762 /* Check that the function is called through a compatible prototype.
2763 If it is not, replace the call by a trap, wrapped up in a compound
2764 expression if necessary. This has the nice side-effect to prevent
2765 the tree-inliner from generating invalid assignment trees which may
2766 blow up in the RTL expander later. */
2767 if (CONVERT_EXPR_P (function)
2768 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2769 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2770 && !comptypes (fntype, TREE_TYPE (tem)))
2772 tree return_type = TREE_TYPE (fntype);
2773 tree trap = build_function_call (loc,
2774 builtin_decl_explicit (BUILT_IN_TRAP),
2778 /* This situation leads to run-time undefined behavior. We can't,
2779 therefore, simply error unless we can prove that all possible
2780 executions of the program must execute the code. */
2781 if (warning_at (loc, 0, "function called through a non-compatible type"))
2782 /* We can, however, treat "undefined" any way we please.
2783 Call abort to encourage the user to fix the program. */
2784 inform (loc, "if this code is reached, the program will abort");
2785 /* Before the abort, allow the function arguments to exit or
2787 for (i = 0; i < nargs; i++)
2788 trap = build2 (COMPOUND_EXPR, void_type_node,
2789 VEC_index (tree, params, i), trap);
2791 if (VOID_TYPE_P (return_type))
2793 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2795 "function with qualified void return type called");
2802 if (AGGREGATE_TYPE_P (return_type))
2803 rhs = build_compound_literal (loc, return_type,
2804 build_constructor (return_type, 0),
2807 rhs = build_zero_cst (return_type);
2809 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2814 argarray = VEC_address (tree, params);
2816 /* Check that arguments to builtin functions match the expectations. */
2818 && DECL_BUILT_IN (fundecl)
2819 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2820 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2821 return error_mark_node;
2823 /* Check that the arguments to the function are valid. */
2824 check_function_arguments (fntype, nargs, argarray);
2826 if (name != NULL_TREE
2827 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2829 if (require_constant_value)
2831 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2832 function, nargs, argarray);
2834 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2835 function, nargs, argarray);
2836 if (TREE_CODE (result) == NOP_EXPR
2837 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2838 STRIP_TYPE_NOPS (result);
2841 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2842 function, nargs, argarray);
2844 if (VOID_TYPE_P (TREE_TYPE (result)))
2846 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2848 "function with qualified void return type called");
2851 return require_complete_type (result);
2854 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2855 and have vector types, V0 has the same type as V1, and the number of
2856 elements of V0, V1, MASK is the same.
2858 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2859 called with two arguments. In this case implementation passes the
2860 first argument twice in order to share the same tree code. This fact
2861 could enable the mask-values being twice the vector length. This is
2862 an implementation accident and this semantics is not guaranteed to
2865 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2869 bool maybe_const = false;
2870 bool two_arguments = false;
2872 if (v1 == NULL_TREE)
2874 two_arguments = true;
2878 if (v0 == error_mark_node || v1 == error_mark_node
2879 || mask == error_mark_node)
2880 return error_mark_node;
2882 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2883 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2885 error_at (loc, "__builtin_shuffle last argument must "
2886 "be an integer vector");
2887 return error_mark_node;
2890 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2891 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2893 error_at (loc, "__builtin_shuffle arguments must be vectors");
2894 return error_mark_node;
2897 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2899 error_at (loc, "__builtin_shuffle argument vectors must be of "
2901 return error_mark_node;
2904 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2905 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2906 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2907 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2909 error_at (loc, "__builtin_shuffle number of elements of the "
2910 "argument vector(s) and the mask vector should "
2912 return error_mark_node;
2915 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2916 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2918 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2919 "must have the same size as inner type of the mask");
2920 return error_mark_node;
2923 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2924 v0 = c_fully_fold (v0, false, &maybe_const);
2925 wrap &= maybe_const;
2928 v1 = v0 = save_expr (v0);
2931 v1 = c_fully_fold (v1, false, &maybe_const);
2932 wrap &= maybe_const;
2935 mask = c_fully_fold (mask, false, &maybe_const);
2936 wrap &= maybe_const;
2938 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2941 ret = c_wrap_maybe_const (ret, true);
2946 /* Convert the argument expressions in the vector VALUES
2947 to the types in the list TYPELIST.
2949 If TYPELIST is exhausted, or when an element has NULL as its type,
2950 perform the default conversions.
2952 ORIGTYPES is the original types of the expressions in VALUES. This
2953 holds the type of enum values which have been converted to integral
2954 types. It may be NULL.
2956 FUNCTION is a tree for the called function. It is used only for
2957 error messages, where it is formatted with %qE.
2959 This is also where warnings about wrong number of args are generated.
2961 Returns the actual number of arguments processed (which may be less
2962 than the length of VALUES in some error situations), or -1 on
2966 convert_arguments (tree typelist, VEC(tree,gc) *values,
2967 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2970 unsigned int parmnum;
2971 bool error_args = false;
2972 const bool type_generic = fundecl
2973 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2974 bool type_generic_remove_excess_precision = false;
2977 /* Change pointer to function to the function itself for
2979 if (TREE_CODE (function) == ADDR_EXPR
2980 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2981 function = TREE_OPERAND (function, 0);
2983 /* Handle an ObjC selector specially for diagnostics. */
2984 selector = objc_message_selector ();
2986 /* For type-generic built-in functions, determine whether excess
2987 precision should be removed (classification) or not
2990 && DECL_BUILT_IN (fundecl)
2991 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2993 switch (DECL_FUNCTION_CODE (fundecl))
2995 case BUILT_IN_ISFINITE:
2996 case BUILT_IN_ISINF:
2997 case BUILT_IN_ISINF_SIGN:
2998 case BUILT_IN_ISNAN:
2999 case BUILT_IN_ISNORMAL:
3000 case BUILT_IN_FPCLASSIFY:
3001 type_generic_remove_excess_precision = true;
3005 type_generic_remove_excess_precision = false;
3010 /* Scan the given expressions and types, producing individual
3011 converted arguments. */
3013 for (typetail = typelist, parmnum = 0;
3014 VEC_iterate (tree, values, parmnum, val);
3017 tree type = typetail ? TREE_VALUE (typetail) : 0;
3018 tree valtype = TREE_TYPE (val);
3019 tree rname = function;
3020 int argnum = parmnum + 1;
3021 const char *invalid_func_diag;
3022 bool excess_precision = false;
3026 if (type == void_type_node)
3029 error_at (input_location,
3030 "too many arguments to method %qE", selector);
3032 error_at (input_location,
3033 "too many arguments to function %qE", function);
3035 if (fundecl && !DECL_BUILT_IN (fundecl))
3036 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3040 if (selector && argnum > 2)
3046 npc = null_pointer_constant_p (val);
3048 /* If there is excess precision and a prototype, convert once to
3049 the required type rather than converting via the semantic
3050 type. Likewise without a prototype a float value represented
3051 as long double should be converted once to double. But for
3052 type-generic classification functions excess precision must
3054 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3055 && (type || !type_generic || !type_generic_remove_excess_precision))
3057 val = TREE_OPERAND (val, 0);
3058 excess_precision = true;
3060 val = c_fully_fold (val, false, NULL);
3061 STRIP_TYPE_NOPS (val);
3063 val = require_complete_type (val);
3067 /* Formal parm type is specified by a function prototype. */
3069 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3071 error ("type of formal parameter %d is incomplete", parmnum + 1);
3078 /* Optionally warn about conversions that
3079 differ from the default conversions. */
3080 if (warn_traditional_conversion || warn_traditional)
3082 unsigned int formal_prec = TYPE_PRECISION (type);
3084 if (INTEGRAL_TYPE_P (type)
3085 && TREE_CODE (valtype) == REAL_TYPE)
3086 warning (0, "passing argument %d of %qE as integer "
3087 "rather than floating due to prototype",
3089 if (INTEGRAL_TYPE_P (type)
3090 && TREE_CODE (valtype) == COMPLEX_TYPE)
3091 warning (0, "passing argument %d of %qE as integer "
3092 "rather than complex due to prototype",
3094 else if (TREE_CODE (type) == COMPLEX_TYPE
3095 && TREE_CODE (valtype) == REAL_TYPE)
3096 warning (0, "passing argument %d of %qE as complex "
3097 "rather than floating due to prototype",
3099 else if (TREE_CODE (type) == REAL_TYPE
3100 && INTEGRAL_TYPE_P (valtype))
3101 warning (0, "passing argument %d of %qE as floating "
3102 "rather than integer due to prototype",
3104 else if (TREE_CODE (type) == COMPLEX_TYPE
3105 && INTEGRAL_TYPE_P (valtype))
3106 warning (0, "passing argument %d of %qE as complex "
3107 "rather than integer due to prototype",
3109 else if (TREE_CODE (type) == REAL_TYPE
3110 && TREE_CODE (valtype) == COMPLEX_TYPE)
3111 warning (0, "passing argument %d of %qE as floating "
3112 "rather than complex due to prototype",
3114 /* ??? At some point, messages should be written about
3115 conversions between complex types, but that's too messy
3117 else if (TREE_CODE (type) == REAL_TYPE
3118 && TREE_CODE (valtype) == REAL_TYPE)
3120 /* Warn if any argument is passed as `float',
3121 since without a prototype it would be `double'. */
3122 if (formal_prec == TYPE_PRECISION (float_type_node)
3123 && type != dfloat32_type_node)
3124 warning (0, "passing argument %d of %qE as %<float%> "
3125 "rather than %<double%> due to prototype",
3128 /* Warn if mismatch between argument and prototype
3129 for decimal float types. Warn of conversions with
3130 binary float types and of precision narrowing due to
3132 else if (type != valtype
3133 && (type == dfloat32_type_node
3134 || type == dfloat64_type_node
3135 || type == dfloat128_type_node
3136 || valtype == dfloat32_type_node
3137 || valtype == dfloat64_type_node
3138 || valtype == dfloat128_type_node)
3140 <= TYPE_PRECISION (valtype)
3141 || (type == dfloat128_type_node
3143 != dfloat64_type_node
3145 != dfloat32_type_node)))
3146 || (type == dfloat64_type_node
3148 != dfloat32_type_node))))
3149 warning (0, "passing argument %d of %qE as %qT "
3150 "rather than %qT due to prototype",
3151 argnum, rname, type, valtype);
3154 /* Detect integer changing in width or signedness.
3155 These warnings are only activated with
3156 -Wtraditional-conversion, not with -Wtraditional. */
3157 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3158 && INTEGRAL_TYPE_P (valtype))
3160 tree would_have_been = default_conversion (val);
3161 tree type1 = TREE_TYPE (would_have_been);
3163 if (TREE_CODE (type) == ENUMERAL_TYPE
3164 && (TYPE_MAIN_VARIANT (type)
3165 == TYPE_MAIN_VARIANT (valtype)))
3166 /* No warning if function asks for enum
3167 and the actual arg is that enum type. */
3169 else if (formal_prec != TYPE_PRECISION (type1))
3170 warning (OPT_Wtraditional_conversion,
3171 "passing argument %d of %qE "
3172 "with different width due to prototype",
3174 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3176 /* Don't complain if the formal parameter type
3177 is an enum, because we can't tell now whether
3178 the value was an enum--even the same enum. */
3179 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3181 else if (TREE_CODE (val) == INTEGER_CST
3182 && int_fits_type_p (val, type))
3183 /* Change in signedness doesn't matter
3184 if a constant value is unaffected. */
3186 /* If the value is extended from a narrower
3187 unsigned type, it doesn't matter whether we
3188 pass it as signed or unsigned; the value
3189 certainly is the same either way. */
3190 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3191 && TYPE_UNSIGNED (valtype))
3193 else if (TYPE_UNSIGNED (type))
3194 warning (OPT_Wtraditional_conversion,
3195 "passing argument %d of %qE "
3196 "as unsigned due to prototype",
3199 warning (OPT_Wtraditional_conversion,
3200 "passing argument %d of %qE "
3201 "as signed due to prototype", argnum, rname);
3205 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3206 sake of better warnings from convert_and_check. */
3207 if (excess_precision)
3208 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3209 origtype = (origtypes == NULL
3211 : VEC_index (tree, origtypes, parmnum));
3212 parmval = convert_for_assignment (input_location, type, val,
3213 origtype, ic_argpass, npc,
3217 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3218 && INTEGRAL_TYPE_P (type)
3219 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3220 parmval = default_conversion (parmval);
3223 else if (TREE_CODE (valtype) == REAL_TYPE
3224 && (TYPE_PRECISION (valtype)
3225 < TYPE_PRECISION (double_type_node))
3226 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3232 /* Convert `float' to `double'. */
3233 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3234 warning (OPT_Wdouble_promotion,
3235 "implicit conversion from %qT to %qT when passing "
3236 "argument to function",
3237 valtype, double_type_node);
3238 parmval = convert (double_type_node, val);
3241 else if (excess_precision && !type_generic)
3242 /* A "double" argument with excess precision being passed
3243 without a prototype or in variable arguments. */
3244 parmval = convert (valtype, val);
3245 else if ((invalid_func_diag =
3246 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3248 error (invalid_func_diag);
3252 /* Convert `short' and `char' to full-size `int'. */
3253 parmval = default_conversion (val);
3255 VEC_replace (tree, values, parmnum, parmval);
3256 if (parmval == error_mark_node)
3260 typetail = TREE_CHAIN (typetail);
3263 gcc_assert (parmnum == VEC_length (tree, values));
3265 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3267 error_at (input_location,
3268 "too few arguments to function %qE", function);
3269 if (fundecl && !DECL_BUILT_IN (fundecl))
3270 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3274 return error_args ? -1 : (int) parmnum;
3277 /* This is the entry point used by the parser to build unary operators
3278 in the input. CODE, a tree_code, specifies the unary operator, and
3279 ARG is the operand. For unary plus, the C parser currently uses
3280 CONVERT_EXPR for code.
3282 LOC is the location to use for the tree generated.
3286 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3288 struct c_expr result;
3290 result.value = build_unary_op (loc, code, arg.value, 0);
3291 result.original_code = code;
3292 result.original_type = NULL;
3294 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3295 overflow_warning (loc, result.value);
3300 /* This is the entry point used by the parser to build binary operators
3301 in the input. CODE, a tree_code, specifies the binary operator, and
3302 ARG1 and ARG2 are the operands. In addition to constructing the
3303 expression, we check for operands that were written with other binary
3304 operators in a way that is likely to confuse the user.
3306 LOCATION is the location of the binary operator. */
3309 parser_build_binary_op (location_t location, enum tree_code code,
3310 struct c_expr arg1, struct c_expr arg2)
3312 struct c_expr result;
3314 enum tree_code code1 = arg1.original_code;
3315 enum tree_code code2 = arg2.original_code;
3316 tree type1 = (arg1.original_type
3317 ? arg1.original_type
3318 : TREE_TYPE (arg1.value));
3319 tree type2 = (arg2.original_type
3320 ? arg2.original_type
3321 : TREE_TYPE (arg2.value));
3323 result.value = build_binary_op (location, code,
3324 arg1.value, arg2.value, 1);
3325 result.original_code = code;
3326 result.original_type = NULL;
3328 if (TREE_CODE (result.value) == ERROR_MARK)
3331 if (location != UNKNOWN_LOCATION)
3332 protected_set_expr_location (result.value, location);
3334 /* Check for cases such as x+y<<z which users are likely
3336 if (warn_parentheses)
3337 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3339 if (warn_logical_op)
3340 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3341 code1, arg1.value, code2, arg2.value);
3343 /* Warn about comparisons against string literals, with the exception
3344 of testing for equality or inequality of a string literal with NULL. */
3345 if (code == EQ_EXPR || code == NE_EXPR)
3347 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3348 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3349 warning_at (location, OPT_Waddress,
3350 "comparison with string literal results in unspecified behavior");
3352 else if (TREE_CODE_CLASS (code) == tcc_comparison
3353 && (code1 == STRING_CST || code2 == STRING_CST))
3354 warning_at (location, OPT_Waddress,
3355 "comparison with string literal results in unspecified behavior");
3357 if (TREE_OVERFLOW_P (result.value)
3358 && !TREE_OVERFLOW_P (arg1.value)
3359 && !TREE_OVERFLOW_P (arg2.value))
3360 overflow_warning (location, result.value);
3362 /* Warn about comparisons of different enum types. */
3363 if (warn_enum_compare
3364 && TREE_CODE_CLASS (code) == tcc_comparison
3365 && TREE_CODE (type1) == ENUMERAL_TYPE
3366 && TREE_CODE (type2) == ENUMERAL_TYPE
3367 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3368 warning_at (location, OPT_Wenum_compare,
3369 "comparison between %qT and %qT",
3375 /* Return a tree for the difference of pointers OP0 and OP1.
3376 The resulting tree has type int. */
3379 pointer_diff (location_t loc, tree op0, tree op1)
3381 tree restype = ptrdiff_type_node;
3382 tree result, inttype;
3384 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3385 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3386 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3387 tree con0, con1, lit0, lit1;
3388 tree orig_op1 = op1;
3390 /* If the operands point into different address spaces, we need to
3391 explicitly convert them to pointers into the common address space
3392 before we can subtract the numerical address values. */
3395 addr_space_t as_common;
3398 /* Determine the common superset address space. This is guaranteed
3399 to exist because the caller verified that comp_target_types
3400 returned non-zero. */
3401 if (!addr_space_superset (as0, as1, &as_common))
3404 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3405 op0 = convert (common_type, op0);
3406 op1 = convert (common_type, op1);
3409 /* Determine integer type to perform computations in. This will usually
3410 be the same as the result type (ptrdiff_t), but may need to be a wider
3411 type if pointers for the address space are wider than ptrdiff_t. */
3412 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3413 inttype = lang_hooks.types.type_for_size
3414 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3419 if (TREE_CODE (target_type) == VOID_TYPE)
3420 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3421 "pointer of type %<void *%> used in subtraction");
3422 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3423 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3424 "pointer to a function used in subtraction");
3426 /* If the conversion to ptrdiff_type does anything like widening or
3427 converting a partial to an integral mode, we get a convert_expression
3428 that is in the way to do any simplifications.
3429 (fold-const.c doesn't know that the extra bits won't be needed.
3430 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3431 different mode in place.)
3432 So first try to find a common term here 'by hand'; we want to cover
3433 at least the cases that occur in legal static initializers. */
3434 if (CONVERT_EXPR_P (op0)
3435 && (TYPE_PRECISION (TREE_TYPE (op0))
3436 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3437 con0 = TREE_OPERAND (op0, 0);
3440 if (CONVERT_EXPR_P (op1)
3441 && (TYPE_PRECISION (TREE_TYPE (op1))
3442 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3443 con1 = TREE_OPERAND (op1, 0);
3447 if (TREE_CODE (con0) == PLUS_EXPR)
3449 lit0 = TREE_OPERAND (con0, 1);
3450 con0 = TREE_OPERAND (con0, 0);
3453 lit0 = integer_zero_node;
3455 if (TREE_CODE (con1) == PLUS_EXPR)
3457 lit1 = TREE_OPERAND (con1, 1);
3458 con1 = TREE_OPERAND (con1, 0);
3461 lit1 = integer_zero_node;
3463 if (operand_equal_p (con0, con1, 0))
3470 /* First do the subtraction as integers;
3471 then drop through to build the divide operator.
3472 Do not do default conversions on the minus operator
3473 in case restype is a short type. */
3475 op0 = build_binary_op (loc,
3476 MINUS_EXPR, convert (inttype, op0),
3477 convert (inttype, op1), 0);
3478 /* This generates an error if op1 is pointer to incomplete type. */
3479 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3480 error_at (loc, "arithmetic on pointer to an incomplete type");
3482 /* This generates an error if op0 is pointer to incomplete type. */
3483 op1 = c_size_in_bytes (target_type);
3485 /* Divide by the size, in easiest possible way. */
3486 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3487 op0, convert (inttype, op1));
3489 /* Convert to final result type if necessary. */
3490 return convert (restype, result);
3493 /* Construct and perhaps optimize a tree representation
3494 for a unary operation. CODE, a tree_code, specifies the operation
3495 and XARG is the operand.
3496 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3497 the default promotions (such as from short to int).
3498 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3499 allows non-lvalues; this is only used to handle conversion of non-lvalue
3500 arrays to pointers in C99.
3502 LOCATION is the location of the operator. */
3505 build_unary_op (location_t location,
3506 enum tree_code code, tree xarg, int flag)
3508 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3511 enum tree_code typecode;
3513 tree ret = error_mark_node;
3514 tree eptype = NULL_TREE;
3515 int noconvert = flag;
3516 const char *invalid_op_diag;
3519 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3521 arg = remove_c_maybe_const_expr (arg);
3523 if (code != ADDR_EXPR)
3524 arg = require_complete_type (arg);
3526 typecode = TREE_CODE (TREE_TYPE (arg));
3527 if (typecode == ERROR_MARK)
3528 return error_mark_node;
3529 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3530 typecode = INTEGER_TYPE;
3532 if ((invalid_op_diag
3533 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3535 error_at (location, invalid_op_diag);
3536 return error_mark_node;
3539 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3541 eptype = TREE_TYPE (arg);
3542 arg = TREE_OPERAND (arg, 0);
3548 /* This is used for unary plus, because a CONVERT_EXPR
3549 is enough to prevent anybody from looking inside for
3550 associativity, but won't generate any code. */
3551 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3552 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3553 || typecode == VECTOR_TYPE))
3555 error_at (location, "wrong type argument to unary plus");
3556 return error_mark_node;
3558 else if (!noconvert)
3559 arg = default_conversion (arg);
3560 arg = non_lvalue_loc (location, arg);
3564 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3565 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3566 || typecode == VECTOR_TYPE))
3568 error_at (location, "wrong type argument to unary minus");
3569 return error_mark_node;
3571 else if (!noconvert)
3572 arg = default_conversion (arg);
3576 /* ~ works on integer types and non float vectors. */
3577 if (typecode == INTEGER_TYPE
3578 || (typecode == VECTOR_TYPE
3579 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3582 arg = default_conversion (arg);
3584 else if (typecode == COMPLEX_TYPE)
3587 pedwarn (location, OPT_pedantic,
3588 "ISO C does not support %<~%> for complex conjugation");
3590 arg = default_conversion (arg);
3594 error_at (location, "wrong type argument to bit-complement");
3595 return error_mark_node;
3600 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3602 error_at (location, "wrong type argument to abs");
3603 return error_mark_node;
3605 else if (!noconvert)
3606 arg = default_conversion (arg);
3610 /* Conjugating a real value is a no-op, but allow it anyway. */
3611 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3612 || typecode == COMPLEX_TYPE))
3614 error_at (location, "wrong type argument to conjugation");
3615 return error_mark_node;
3617 else if (!noconvert)
3618 arg = default_conversion (arg);
3621 case TRUTH_NOT_EXPR:
3622 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3623 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3624 && typecode != COMPLEX_TYPE)
3627 "wrong type argument to unary exclamation mark");
3628 return error_mark_node;
3630 arg = c_objc_common_truthvalue_conversion (location, arg);
3631 ret = invert_truthvalue_loc (location, arg);
3632 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3633 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3634 location = EXPR_LOCATION (ret);
3635 goto return_build_unary_op;
3639 ret = build_real_imag_expr (location, code, arg);
3640 if (ret == error_mark_node)
3641 return error_mark_node;
3642 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3643 eptype = TREE_TYPE (eptype);
3644 goto return_build_unary_op;
3646 case PREINCREMENT_EXPR:
3647 case POSTINCREMENT_EXPR:
3648 case PREDECREMENT_EXPR:
3649 case POSTDECREMENT_EXPR:
3651 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3653 tree inner = build_unary_op (location, code,
3654 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3655 if (inner == error_mark_node)
3656 return error_mark_node;
3657 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3658 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3659 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3660 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3661 goto return_build_unary_op;
3664 /* Complain about anything that is not a true lvalue. In
3665 Objective-C, skip this check for property_refs. */
3666 if (!objc_is_property_ref (arg)
3667 && !lvalue_or_else (location,
3668 arg, ((code == PREINCREMENT_EXPR
3669 || code == POSTINCREMENT_EXPR)
3672 return error_mark_node;
3674 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3676 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3677 warning_at (location, OPT_Wc___compat,
3678 "increment of enumeration value is invalid in C++");
3680 warning_at (location, OPT_Wc___compat,
3681 "decrement of enumeration value is invalid in C++");
3684 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3685 arg = c_fully_fold (arg, false, NULL);
3687 /* Increment or decrement the real part of the value,
3688 and don't change the imaginary part. */
3689 if (typecode == COMPLEX_TYPE)
3693 pedwarn (location, OPT_pedantic,
3694 "ISO C does not support %<++%> and %<--%> on complex types");
3696 arg = stabilize_reference (arg);
3697 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3698 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3699 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3700 if (real == error_mark_node || imag == error_mark_node)
3701 return error_mark_node;
3702 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3704 goto return_build_unary_op;
3707 /* Report invalid types. */
3709 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3710 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3712 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3713 error_at (location, "wrong type argument to increment");
3715 error_at (location, "wrong type argument to decrement");
3717 return error_mark_node;
3723 argtype = TREE_TYPE (arg);
3725 /* Compute the increment. */
3727 if (typecode == POINTER_TYPE)
3729 /* If pointer target is an undefined struct,
3730 we just cannot know how to do the arithmetic. */
3731 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3733 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3735 "increment of pointer to unknown structure");
3738 "decrement of pointer to unknown structure");
3740 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3741 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3743 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3744 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3745 "wrong type argument to increment");
3747 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3748 "wrong type argument to decrement");
3751 inc = c_size_in_bytes (TREE_TYPE (argtype));
3752 inc = convert_to_ptrofftype_loc (location, inc);
3754 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3756 /* For signed fract types, we invert ++ to -- or
3757 -- to ++, and change inc from 1 to -1, because
3758 it is not possible to represent 1 in signed fract constants.
3759 For unsigned fract types, the result always overflows and
3760 we get an undefined (original) or the maximum value. */
3761 if (code == PREINCREMENT_EXPR)
3762 code = PREDECREMENT_EXPR;
3763 else if (code == PREDECREMENT_EXPR)
3764 code = PREINCREMENT_EXPR;
3765 else if (code == POSTINCREMENT_EXPR)
3766 code = POSTDECREMENT_EXPR;
3767 else /* code == POSTDECREMENT_EXPR */
3768 code = POSTINCREMENT_EXPR;
3770 inc = integer_minus_one_node;
3771 inc = convert (argtype, inc);
3775 inc = integer_one_node;
3776 inc = convert (argtype, inc);
3779 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3780 need to ask Objective-C to build the increment or decrement
3781 expression for it. */
3782 if (objc_is_property_ref (arg))
3783 return objc_build_incr_expr_for_property_ref (location, code,
3786 /* Report a read-only lvalue. */
3787 if (TYPE_READONLY (argtype))
3789 readonly_error (arg,
3790 ((code == PREINCREMENT_EXPR
3791 || code == POSTINCREMENT_EXPR)
3792 ? lv_increment : lv_decrement));
3793 return error_mark_node;
3795 else if (TREE_READONLY (arg))
3796 readonly_warning (arg,
3797 ((code == PREINCREMENT_EXPR
3798 || code == POSTINCREMENT_EXPR)
3799 ? lv_increment : lv_decrement));
3801 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3802 val = boolean_increment (code, arg);
3804 val = build2 (code, TREE_TYPE (arg), arg, inc);
3805 TREE_SIDE_EFFECTS (val) = 1;
3806 if (TREE_CODE (val) != code)
3807 TREE_NO_WARNING (val) = 1;
3809 goto return_build_unary_op;
3813 /* Note that this operation never does default_conversion. */
3815 /* The operand of unary '&' must be an lvalue (which excludes
3816 expressions of type void), or, in C99, the result of a [] or
3817 unary '*' operator. */
3818 if (VOID_TYPE_P (TREE_TYPE (arg))
3819 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3820 && (TREE_CODE (arg) != INDIRECT_REF
3822 pedwarn (location, 0, "taking address of expression of type %<void%>");
3824 /* Let &* cancel out to simplify resulting code. */
3825 if (TREE_CODE (arg) == INDIRECT_REF)
3827 /* Don't let this be an lvalue. */
3828 if (lvalue_p (TREE_OPERAND (arg, 0)))
3829 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3830 ret = TREE_OPERAND (arg, 0);
3831 goto return_build_unary_op;
3834 /* For &x[y], return x+y */
3835 if (TREE_CODE (arg) == ARRAY_REF)
3837 tree op0 = TREE_OPERAND (arg, 0);
3838 if (!c_mark_addressable (op0))
3839 return error_mark_node;
3842 /* Anything not already handled and not a true memory reference
3843 or a non-lvalue array is an error. */
3844 else if (typecode != FUNCTION_TYPE && !flag
3845 && !lvalue_or_else (location, arg, lv_addressof))
3846 return error_mark_node;
3848 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3850 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3852 tree inner = build_unary_op (location, code,
3853 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3854 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3855 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3856 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3857 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3858 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3859 goto return_build_unary_op;
3862 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3863 argtype = TREE_TYPE (arg);
3865 /* If the lvalue is const or volatile, merge that into the type
3866 to which the address will point. This is only needed
3867 for function types. */
3868 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3869 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3870 && TREE_CODE (argtype) == FUNCTION_TYPE)
3872 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3873 int quals = orig_quals;
3875 if (TREE_READONLY (arg))
3876 quals |= TYPE_QUAL_CONST;
3877 if (TREE_THIS_VOLATILE (arg))
3878 quals |= TYPE_QUAL_VOLATILE;
3880 argtype = c_build_qualified_type (argtype, quals);
3883 if (!c_mark_addressable (arg))
3884 return error_mark_node;
3886 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3887 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3889 argtype = build_pointer_type (argtype);
3891 /* ??? Cope with user tricks that amount to offsetof. Delete this
3892 when we have proper support for integer constant expressions. */
3893 val = get_base_address (arg);
3894 if (val && TREE_CODE (val) == INDIRECT_REF
3895 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3897 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3898 goto return_build_unary_op;
3901 val = build1 (ADDR_EXPR, argtype, arg);
3904 goto return_build_unary_op;
3911 argtype = TREE_TYPE (arg);
3912 if (TREE_CODE (arg) == INTEGER_CST)
3913 ret = (require_constant_value
3914 ? fold_build1_initializer_loc (location, code, argtype, arg)
3915 : fold_build1_loc (location, code, argtype, arg));
3917 ret = build1 (code, argtype, arg);
3918 return_build_unary_op:
3919 gcc_assert (ret != error_mark_node);
3920 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3921 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3922 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3923 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3924 ret = note_integer_operands (ret);
3926 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3927 protected_set_expr_location (ret, location);
3931 /* Return nonzero if REF is an lvalue valid for this language.
3932 Lvalues can be assigned, unless their type has TYPE_READONLY.
3933 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3936 lvalue_p (const_tree ref)
3938 const enum tree_code code = TREE_CODE (ref);
3945 return lvalue_p (TREE_OPERAND (ref, 0));
3947 case C_MAYBE_CONST_EXPR:
3948 return lvalue_p (TREE_OPERAND (ref, 1));
3950 case COMPOUND_LITERAL_EXPR:
3960 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3961 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3964 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3971 /* Give a warning for storing in something that is read-only in GCC
3972 terms but not const in ISO C terms. */
3975 readonly_warning (tree arg, enum lvalue_use use)
3980 warning (0, "assignment of read-only location %qE", arg);
3983 warning (0, "increment of read-only location %qE", arg);
3986 warning (0, "decrement of read-only location %qE", arg);
3995 /* Return nonzero if REF is an lvalue valid for this language;
3996 otherwise, print an error message and return zero. USE says
3997 how the lvalue is being used and so selects the error message.
3998 LOCATION is the location at which any error should be reported. */
4001 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4003 int win = lvalue_p (ref);
4006 lvalue_error (loc, use);
4011 /* Mark EXP saying that we need to be able to take the
4012 address of it; it should not be allocated in a register.
4013 Returns true if successful. */
4016 c_mark_addressable (tree exp)
4021 switch (TREE_CODE (x))
4024 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4027 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4031 /* ... fall through ... */
4037 x = TREE_OPERAND (x, 0);
4040 case COMPOUND_LITERAL_EXPR:
4042 TREE_ADDRESSABLE (x) = 1;
4049 if (C_DECL_REGISTER (x)
4050 && DECL_NONLOCAL (x))
4052 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4055 ("global register variable %qD used in nested function", x);
4058 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4060 else if (C_DECL_REGISTER (x))
4062 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4063 error ("address of global register variable %qD requested", x);
4065 error ("address of register variable %qD requested", x);
4071 TREE_ADDRESSABLE (x) = 1;
4078 /* Convert EXPR to TYPE, warning about conversion problems with
4079 constants. SEMANTIC_TYPE is the type this conversion would use
4080 without excess precision. If SEMANTIC_TYPE is NULL, this function
4081 is equivalent to convert_and_check. This function is a wrapper that
4082 handles conversions that may be different than
4083 the usual ones because of excess precision. */
4086 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4088 if (TREE_TYPE (expr) == type)
4092 return convert_and_check (type, expr);
4094 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4095 && TREE_TYPE (expr) != semantic_type)
4097 /* For integers, we need to check the real conversion, not
4098 the conversion to the excess precision type. */
4099 expr = convert_and_check (semantic_type, expr);
4101 /* Result type is the excess precision type, which should be
4102 large enough, so do not check. */
4103 return convert (type, expr);
4106 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4107 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4108 if folded to an integer constant then the unselected half may
4109 contain arbitrary operations not normally permitted in constant
4110 expressions. Set the location of the expression to LOC. */
4113 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4114 tree op1, tree op1_original_type, tree op2,
4115 tree op2_original_type)
4119 enum tree_code code1;
4120 enum tree_code code2;
4121 tree result_type = NULL;
4122 tree semantic_result_type = NULL;
4123 tree orig_op1 = op1, orig_op2 = op2;
4124 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4125 bool ifexp_int_operands;
4128 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4129 if (op1_int_operands)
4130 op1 = remove_c_maybe_const_expr (op1);
4131 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4132 if (op2_int_operands)
4133 op2 = remove_c_maybe_const_expr (op2);
4134 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4135 if (ifexp_int_operands)
4136 ifexp = remove_c_maybe_const_expr (ifexp);
4138 /* Promote both alternatives. */
4140 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4141 op1 = default_conversion (op1);
4142 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4143 op2 = default_conversion (op2);
4145 if (TREE_CODE (ifexp) == ERROR_MARK
4146 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4147 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4148 return error_mark_node;
4150 type1 = TREE_TYPE (op1);
4151 code1 = TREE_CODE (type1);
4152 type2 = TREE_TYPE (op2);
4153 code2 = TREE_CODE (type2);
4155 /* C90 does not permit non-lvalue arrays in conditional expressions.
4156 In C99 they will be pointers by now. */
4157 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4159 error_at (colon_loc, "non-lvalue array in conditional expression");
4160 return error_mark_node;
4163 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4164 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4165 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4166 || code1 == COMPLEX_TYPE)
4167 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4168 || code2 == COMPLEX_TYPE))
4170 semantic_result_type = c_common_type (type1, type2);
4171 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4173 op1 = TREE_OPERAND (op1, 0);
4174 type1 = TREE_TYPE (op1);
4175 gcc_assert (TREE_CODE (type1) == code1);
4177 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4179 op2 = TREE_OPERAND (op2, 0);
4180 type2 = TREE_TYPE (op2);
4181 gcc_assert (TREE_CODE (type2) == code2);
4185 if (warn_cxx_compat)
4187 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4188 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4190 if (TREE_CODE (t1) == ENUMERAL_TYPE
4191 && TREE_CODE (t2) == ENUMERAL_TYPE
4192 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4193 warning_at (colon_loc, OPT_Wc___compat,
4194 ("different enum types in conditional is "
4195 "invalid in C++: %qT vs %qT"),
4199 /* Quickly detect the usual case where op1 and op2 have the same type
4201 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4204 result_type = type1;
4206 result_type = TYPE_MAIN_VARIANT (type1);
4208 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4209 || code1 == COMPLEX_TYPE)
4210 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4211 || code2 == COMPLEX_TYPE))
4213 result_type = c_common_type (type1, type2);
4214 do_warn_double_promotion (result_type, type1, type2,
4215 "implicit conversion from %qT to %qT to "
4216 "match other result of conditional",
4219 /* If -Wsign-compare, warn here if type1 and type2 have
4220 different signedness. We'll promote the signed to unsigned
4221 and later code won't know it used to be different.
4222 Do this check on the original types, so that explicit casts
4223 will be considered, but default promotions won't. */
4224 if (c_inhibit_evaluation_warnings == 0)
4226 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4227 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4229 if (unsigned_op1 ^ unsigned_op2)
4233 /* Do not warn if the result type is signed, since the
4234 signed type will only be chosen if it can represent
4235 all the values of the unsigned type. */
4236 if (!TYPE_UNSIGNED (result_type))
4240 bool op1_maybe_const = true;
4241 bool op2_maybe_const = true;
4243 /* Do not warn if the signed quantity is an
4244 unsuffixed integer literal (or some static
4245 constant expression involving such literals) and
4246 it is non-negative. This warning requires the
4247 operands to be folded for best results, so do
4248 that folding in this case even without
4249 warn_sign_compare to avoid warning options
4250 possibly affecting code generation. */
4251 c_inhibit_evaluation_warnings
4252 += (ifexp == truthvalue_false_node);
4253 op1 = c_fully_fold (op1, require_constant_value,
4255 c_inhibit_evaluation_warnings
4256 -= (ifexp == truthvalue_false_node);
4258 c_inhibit_evaluation_warnings
4259 += (ifexp == truthvalue_true_node);
4260 op2 = c_fully_fold (op2, require_constant_value,
4262 c_inhibit_evaluation_warnings
4263 -= (ifexp == truthvalue_true_node);
4265 if (warn_sign_compare)
4268 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4270 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4273 warning_at (colon_loc, OPT_Wsign_compare,
4274 ("signed and unsigned type in "
4275 "conditional expression"));
4277 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4278 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4279 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4280 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4285 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4287 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4288 pedwarn (colon_loc, OPT_pedantic,
4289 "ISO C forbids conditional expr with only one void side");
4290 result_type = void_type_node;
4292 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4294 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4295 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4296 addr_space_t as_common;
4298 if (comp_target_types (colon_loc, type1, type2))
4299 result_type = common_pointer_type (type1, type2);
4300 else if (null_pointer_constant_p (orig_op1))
4301 result_type = type2;
4302 else if (null_pointer_constant_p (orig_op2))
4303 result_type = type1;
4304 else if (!addr_space_superset (as1, as2, &as_common))
4306 error_at (colon_loc, "pointers to disjoint address spaces "
4307 "used in conditional expression");
4308 return error_mark_node;
4310 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4312 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4313 pedwarn (colon_loc, OPT_pedantic,
4314 "ISO C forbids conditional expr between "
4315 "%<void *%> and function pointer");
4316 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4317 TREE_TYPE (type2)));
4319 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4321 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4322 pedwarn (colon_loc, OPT_pedantic,
4323 "ISO C forbids conditional expr between "
4324 "%<void *%> and function pointer");
4325 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4326 TREE_TYPE (type1)));
4328 /* Objective-C pointer comparisons are a bit more lenient. */
4329 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4330 result_type = objc_common_type (type1, type2);
4333 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4335 pedwarn (colon_loc, 0,
4336 "pointer type mismatch in conditional expression");
4337 result_type = build_pointer_type
4338 (build_qualified_type (void_type_node, qual));
4341 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4343 if (!null_pointer_constant_p (orig_op2))
4344 pedwarn (colon_loc, 0,
4345 "pointer/integer type mismatch in conditional expression");
4348 op2 = null_pointer_node;
4350 result_type = type1;
4352 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4354 if (!null_pointer_constant_p (orig_op1))
4355 pedwarn (colon_loc, 0,
4356 "pointer/integer type mismatch in conditional expression");
4359 op1 = null_pointer_node;
4361 result_type = type2;
4366 if (flag_cond_mismatch)
4367 result_type = void_type_node;
4370 error_at (colon_loc, "type mismatch in conditional expression");
4371 return error_mark_node;
4375 /* Merge const and volatile flags of the incoming types. */
4377 = build_type_variant (result_type,
4378 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4379 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4381 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4382 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4384 if (ifexp_bcp && ifexp == truthvalue_true_node)
4386 op2_int_operands = true;
4387 op1 = c_fully_fold (op1, require_constant_value, NULL);
4389 if (ifexp_bcp && ifexp == truthvalue_false_node)
4391 op1_int_operands = true;
4392 op2 = c_fully_fold (op2, require_constant_value, NULL);
4394 int_const = int_operands = (ifexp_int_operands
4396 && op2_int_operands);
4399 int_const = ((ifexp == truthvalue_true_node
4400 && TREE_CODE (orig_op1) == INTEGER_CST
4401 && !TREE_OVERFLOW (orig_op1))
4402 || (ifexp == truthvalue_false_node
4403 && TREE_CODE (orig_op2) == INTEGER_CST
4404 && !TREE_OVERFLOW (orig_op2)));
4406 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4407 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4410 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4412 ret = note_integer_operands (ret);
4414 if (semantic_result_type)
4415 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4417 protected_set_expr_location (ret, colon_loc);
4421 /* Return a compound expression that performs two expressions and
4422 returns the value of the second of them.
4424 LOC is the location of the COMPOUND_EXPR. */
4427 build_compound_expr (location_t loc, tree expr1, tree expr2)
4429 bool expr1_int_operands, expr2_int_operands;
4430 tree eptype = NULL_TREE;
4433 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4434 if (expr1_int_operands)
4435 expr1 = remove_c_maybe_const_expr (expr1);
4436 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4437 if (expr2_int_operands)
4438 expr2 = remove_c_maybe_const_expr (expr2);
4440 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4441 expr1 = TREE_OPERAND (expr1, 0);
4442 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4444 eptype = TREE_TYPE (expr2);
4445 expr2 = TREE_OPERAND (expr2, 0);
4448 if (!TREE_SIDE_EFFECTS (expr1))
4450 /* The left-hand operand of a comma expression is like an expression
4451 statement: with -Wunused, we should warn if it doesn't have
4452 any side-effects, unless it was explicitly cast to (void). */
4453 if (warn_unused_value)
4455 if (VOID_TYPE_P (TREE_TYPE (expr1))
4456 && CONVERT_EXPR_P (expr1))
4458 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4459 && TREE_CODE (expr1) == COMPOUND_EXPR
4460 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4461 ; /* (void) a, (void) b, c */
4463 warning_at (loc, OPT_Wunused_value,
4464 "left-hand operand of comma expression has no effect");
4468 /* With -Wunused, we should also warn if the left-hand operand does have
4469 side-effects, but computes a value which is not used. For example, in
4470 `foo() + bar(), baz()' the result of the `+' operator is not used,
4471 so we should issue a warning. */
4472 else if (warn_unused_value)
4473 warn_if_unused_value (expr1, loc);
4475 if (expr2 == error_mark_node)
4476 return error_mark_node;
4478 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4481 && expr1_int_operands
4482 && expr2_int_operands)
4483 ret = note_integer_operands (ret);
4486 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4488 protected_set_expr_location (ret, loc);
4492 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4493 which we are casting. OTYPE is the type of the expression being
4494 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4495 of the cast. -Wcast-qual appeared on the command line. Named
4496 address space qualifiers are not handled here, because they result
4497 in different warnings. */
4500 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4502 tree in_type = type;
4503 tree in_otype = otype;
4508 /* Check that the qualifiers on IN_TYPE are a superset of the
4509 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4510 nodes is uninteresting and we stop as soon as we hit a
4511 non-POINTER_TYPE node on either type. */
4514 in_otype = TREE_TYPE (in_otype);
4515 in_type = TREE_TYPE (in_type);
4517 /* GNU C allows cv-qualified function types. 'const' means the
4518 function is very pure, 'volatile' means it can't return. We
4519 need to warn when such qualifiers are added, not when they're
4521 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4522 && TREE_CODE (in_type) == FUNCTION_TYPE)
4523 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4524 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4526 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4527 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4529 while (TREE_CODE (in_type) == POINTER_TYPE
4530 && TREE_CODE (in_otype) == POINTER_TYPE);
4533 warning_at (loc, OPT_Wcast_qual,
4534 "cast adds %q#v qualifier to function type", added);
4537 /* There are qualifiers present in IN_OTYPE that are not present
4539 warning_at (loc, OPT_Wcast_qual,
4540 "cast discards %q#v qualifier from pointer target type",
4543 if (added || discarded)
4546 /* A cast from **T to const **T is unsafe, because it can cause a
4547 const value to be changed with no additional warning. We only
4548 issue this warning if T is the same on both sides, and we only
4549 issue the warning if there are the same number of pointers on
4550 both sides, as otherwise the cast is clearly unsafe anyhow. A
4551 cast is unsafe when a qualifier is added at one level and const
4552 is not present at all outer levels.
4554 To issue this warning, we check at each level whether the cast
4555 adds new qualifiers not already seen. We don't need to special
4556 case function types, as they won't have the same
4557 TYPE_MAIN_VARIANT. */
4559 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4561 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4566 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4569 in_type = TREE_TYPE (in_type);
4570 in_otype = TREE_TYPE (in_otype);
4571 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4574 warning_at (loc, OPT_Wcast_qual,
4575 "to be safe all intermediate pointers in cast from "
4576 "%qT to %qT must be %<const%> qualified",
4581 is_const = TYPE_READONLY (in_type);
4583 while (TREE_CODE (in_type) == POINTER_TYPE);
4586 /* Build an expression representing a cast to type TYPE of expression EXPR.
4587 LOC is the location of the cast-- typically the open paren of the cast. */
4590 build_c_cast (location_t loc, tree type, tree expr)
4594 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4595 expr = TREE_OPERAND (expr, 0);
4599 if (type == error_mark_node || expr == error_mark_node)
4600 return error_mark_node;
4602 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4603 only in <protocol> qualifications. But when constructing cast expressions,
4604 the protocols do matter and must be kept around. */
4605 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4606 return build1 (NOP_EXPR, type, expr);
4608 type = TYPE_MAIN_VARIANT (type);
4610 if (TREE_CODE (type) == ARRAY_TYPE)
4612 error_at (loc, "cast specifies array type");
4613 return error_mark_node;
4616 if (TREE_CODE (type) == FUNCTION_TYPE)
4618 error_at (loc, "cast specifies function type");
4619 return error_mark_node;
4622 if (!VOID_TYPE_P (type))
4624 value = require_complete_type (value);
4625 if (value == error_mark_node)
4626 return error_mark_node;
4629 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4631 if (TREE_CODE (type) == RECORD_TYPE
4632 || TREE_CODE (type) == UNION_TYPE)
4633 pedwarn (loc, OPT_pedantic,
4634 "ISO C forbids casting nonscalar to the same type");
4636 else if (TREE_CODE (type) == UNION_TYPE)
4640 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4641 if (TREE_TYPE (field) != error_mark_node
4642 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4643 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4649 bool maybe_const = true;
4651 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4652 t = c_fully_fold (value, false, &maybe_const);
4653 t = build_constructor_single (type, field, t);
4655 t = c_wrap_maybe_const (t, true);
4656 t = digest_init (loc, type, t,
4657 NULL_TREE, false, true, 0);
4658 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4661 error_at (loc, "cast to union type from type not present in union");
4662 return error_mark_node;
4668 if (type == void_type_node)
4670 tree t = build1 (CONVERT_EXPR, type, value);
4671 SET_EXPR_LOCATION (t, loc);
4675 otype = TREE_TYPE (value);
4677 /* Optionally warn about potentially worrisome casts. */
4679 && TREE_CODE (type) == POINTER_TYPE
4680 && TREE_CODE (otype) == POINTER_TYPE)
4681 handle_warn_cast_qual (loc, type, otype);
4683 /* Warn about conversions between pointers to disjoint
4685 if (TREE_CODE (type) == POINTER_TYPE
4686 && TREE_CODE (otype) == POINTER_TYPE
4687 && !null_pointer_constant_p (value))
4689 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4690 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4691 addr_space_t as_common;
4693 if (!addr_space_superset (as_to, as_from, &as_common))
4695 if (ADDR_SPACE_GENERIC_P (as_from))
4696 warning_at (loc, 0, "cast to %s address space pointer "
4697 "from disjoint generic address space pointer",
4698 c_addr_space_name (as_to));
4700 else if (ADDR_SPACE_GENERIC_P (as_to))
4701 warning_at (loc, 0, "cast to generic address space pointer "
4702 "from disjoint %s address space pointer",
4703 c_addr_space_name (as_from));
4706 warning_at (loc, 0, "cast to %s address space pointer "
4707 "from disjoint %s address space pointer",
4708 c_addr_space_name (as_to),
4709 c_addr_space_name (as_from));
4713 /* Warn about possible alignment problems. */
4714 if (STRICT_ALIGNMENT
4715 && TREE_CODE (type) == POINTER_TYPE
4716 && TREE_CODE (otype) == POINTER_TYPE
4717 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4718 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4719 /* Don't warn about opaque types, where the actual alignment
4720 restriction is unknown. */
4721 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4722 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4723 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4724 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4725 warning_at (loc, OPT_Wcast_align,
4726 "cast increases required alignment of target type");
4728 if (TREE_CODE (type) == INTEGER_TYPE
4729 && TREE_CODE (otype) == POINTER_TYPE
4730 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4731 /* Unlike conversion of integers to pointers, where the
4732 warning is disabled for converting constants because
4733 of cases such as SIG_*, warn about converting constant
4734 pointers to integers. In some cases it may cause unwanted
4735 sign extension, and a warning is appropriate. */
4736 warning_at (loc, OPT_Wpointer_to_int_cast,
4737 "cast from pointer to integer of different size");
4739 if (TREE_CODE (value) == CALL_EXPR
4740 && TREE_CODE (type) != TREE_CODE (otype))
4741 warning_at (loc, OPT_Wbad_function_cast,
4742 "cast from function call of type %qT "
4743 "to non-matching type %qT", otype, type);
4745 if (TREE_CODE (type) == POINTER_TYPE
4746 && TREE_CODE (otype) == INTEGER_TYPE
4747 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4748 /* Don't warn about converting any constant. */
4749 && !TREE_CONSTANT (value))
4751 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4752 "of different size");
4754 if (warn_strict_aliasing <= 2)
4755 strict_aliasing_warning (otype, type, expr);
4757 /* If pedantic, warn for conversions between function and object
4758 pointer types, except for converting a null pointer constant
4759 to function pointer type. */
4761 && TREE_CODE (type) == POINTER_TYPE
4762 && TREE_CODE (otype) == POINTER_TYPE
4763 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4764 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4765 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4766 "conversion of function pointer to object pointer type");
4769 && TREE_CODE (type) == POINTER_TYPE
4770 && TREE_CODE (otype) == POINTER_TYPE
4771 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4772 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4773 && !null_pointer_constant_p (value))
4774 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4775 "conversion of object pointer to function pointer type");
4778 value = convert (type, value);
4780 /* Ignore any integer overflow caused by the cast. */
4781 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4783 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4785 if (!TREE_OVERFLOW (value))
4787 /* Avoid clobbering a shared constant. */
4788 value = copy_node (value);
4789 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4792 else if (TREE_OVERFLOW (value))
4793 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4794 value = build_int_cst_wide (TREE_TYPE (value),
4795 TREE_INT_CST_LOW (value),
4796 TREE_INT_CST_HIGH (value));
4800 /* Don't let a cast be an lvalue. */
4802 value = non_lvalue_loc (loc, value);
4804 /* Don't allow the results of casting to floating-point or complex
4805 types be confused with actual constants, or casts involving
4806 integer and pointer types other than direct integer-to-integer
4807 and integer-to-pointer be confused with integer constant
4808 expressions and null pointer constants. */
4809 if (TREE_CODE (value) == REAL_CST
4810 || TREE_CODE (value) == COMPLEX_CST
4811 || (TREE_CODE (value) == INTEGER_CST
4812 && !((TREE_CODE (expr) == INTEGER_CST
4813 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4814 || TREE_CODE (expr) == REAL_CST
4815 || TREE_CODE (expr) == COMPLEX_CST)))
4816 value = build1 (NOP_EXPR, type, value);
4818 if (CAN_HAVE_LOCATION_P (value))
4819 SET_EXPR_LOCATION (value, loc);
4823 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4824 location of the open paren of the cast, or the position of the cast
4827 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4830 tree type_expr = NULL_TREE;
4831 bool type_expr_const = true;
4833 int saved_wsp = warn_strict_prototypes;
4835 /* This avoids warnings about unprototyped casts on
4836 integers. E.g. "#define SIG_DFL (void(*)())0". */
4837 if (TREE_CODE (expr) == INTEGER_CST)
4838 warn_strict_prototypes = 0;
4839 type = groktypename (type_name, &type_expr, &type_expr_const);
4840 warn_strict_prototypes = saved_wsp;
4842 ret = build_c_cast (loc, type, expr);
4845 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4846 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4847 SET_EXPR_LOCATION (ret, loc);
4850 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4851 SET_EXPR_LOCATION (ret, loc);
4853 /* C++ does not permits types to be defined in a cast, but it
4854 allows references to incomplete types. */
4855 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4856 warning_at (loc, OPT_Wc___compat,
4857 "defining a type in a cast is invalid in C++");
4862 /* Build an assignment expression of lvalue LHS from value RHS.
4863 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4864 may differ from TREE_TYPE (LHS) for an enum bitfield.
4865 MODIFYCODE is the code for a binary operator that we use
4866 to combine the old value of LHS with RHS to get the new value.
4867 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4868 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4869 which may differ from TREE_TYPE (RHS) for an enum value.
4871 LOCATION is the location of the MODIFYCODE operator.
4872 RHS_LOC is the location of the RHS. */
4875 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4876 enum tree_code modifycode,
4877 location_t rhs_loc, tree rhs, tree rhs_origtype)
4881 tree rhs_semantic_type = NULL_TREE;
4882 tree lhstype = TREE_TYPE (lhs);
4883 tree olhstype = lhstype;
4886 /* Types that aren't fully specified cannot be used in assignments. */
4887 lhs = require_complete_type (lhs);
4889 /* Avoid duplicate error messages from operands that had errors. */
4890 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4891 return error_mark_node;
4893 /* For ObjC properties, defer this check. */
4894 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4895 return error_mark_node;
4897 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4899 rhs_semantic_type = TREE_TYPE (rhs);
4900 rhs = TREE_OPERAND (rhs, 0);
4905 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4907 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4908 lhs_origtype, modifycode, rhs_loc, rhs,
4910 if (inner == error_mark_node)
4911 return error_mark_node;
4912 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4913 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4914 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4915 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4916 protected_set_expr_location (result, location);
4920 /* If a binary op has been requested, combine the old LHS value with the RHS
4921 producing the value we should actually store into the LHS. */
4923 if (modifycode != NOP_EXPR)
4925 lhs = c_fully_fold (lhs, false, NULL);
4926 lhs = stabilize_reference (lhs);
4927 newrhs = build_binary_op (location,
4928 modifycode, lhs, rhs, 1);
4930 /* The original type of the right hand side is no longer
4932 rhs_origtype = NULL_TREE;
4935 if (c_dialect_objc ())
4937 /* Check if we are modifying an Objective-C property reference;
4938 if so, we need to generate setter calls. */
4939 result = objc_maybe_build_modify_expr (lhs, newrhs);
4943 /* Else, do the check that we postponed for Objective-C. */
4944 if (!lvalue_or_else (location, lhs, lv_assign))
4945 return error_mark_node;
4948 /* Give an error for storing in something that is 'const'. */
4950 if (TYPE_READONLY (lhstype)
4951 || ((TREE_CODE (lhstype) == RECORD_TYPE
4952 || TREE_CODE (lhstype) == UNION_TYPE)
4953 && C_TYPE_FIELDS_READONLY (lhstype)))
4955 readonly_error (lhs, lv_assign);
4956 return error_mark_node;
4958 else if (TREE_READONLY (lhs))
4959 readonly_warning (lhs, lv_assign);
4961 /* If storing into a structure or union member,
4962 it has probably been given type `int'.
4963 Compute the type that would go with
4964 the actual amount of storage the member occupies. */
4966 if (TREE_CODE (lhs) == COMPONENT_REF
4967 && (TREE_CODE (lhstype) == INTEGER_TYPE
4968 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4969 || TREE_CODE (lhstype) == REAL_TYPE
4970 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4971 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4973 /* If storing in a field that is in actuality a short or narrower than one,
4974 we must store in the field in its actual type. */
4976 if (lhstype != TREE_TYPE (lhs))
4978 lhs = copy_node (lhs);
4979 TREE_TYPE (lhs) = lhstype;
4982 /* Issue -Wc++-compat warnings about an assignment to an enum type
4983 when LHS does not have its original type. This happens for,
4984 e.g., an enum bitfield in a struct. */
4986 && lhs_origtype != NULL_TREE
4987 && lhs_origtype != lhstype
4988 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4990 tree checktype = (rhs_origtype != NULL_TREE
4993 if (checktype != error_mark_node
4994 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4995 warning_at (location, OPT_Wc___compat,
4996 "enum conversion in assignment is invalid in C++");
4999 /* Convert new value to destination type. Fold it first, then
5000 restore any excess precision information, for the sake of
5001 conversion warnings. */
5003 npc = null_pointer_constant_p (newrhs);
5004 newrhs = c_fully_fold (newrhs, false, NULL);
5005 if (rhs_semantic_type)
5006 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5007 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5008 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
5009 if (TREE_CODE (newrhs) == ERROR_MARK)
5010 return error_mark_node;
5012 /* Emit ObjC write barrier, if necessary. */
5013 if (c_dialect_objc () && flag_objc_gc)
5015 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5018 protected_set_expr_location (result, location);
5023 /* Scan operands. */
5025 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5026 TREE_SIDE_EFFECTS (result) = 1;
5027 protected_set_expr_location (result, location);
5029 /* If we got the LHS in a different type for storing in,
5030 convert the result back to the nominal type of LHS
5031 so that the value we return always has the same type
5032 as the LHS argument. */
5034 if (olhstype == TREE_TYPE (result))
5037 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5038 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5039 protected_set_expr_location (result, location);
5043 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5044 This is used to implement -fplan9-extensions. */
5047 find_anonymous_field_with_type (tree struct_type, tree type)
5052 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5053 || TREE_CODE (struct_type) == UNION_TYPE);
5055 for (field = TYPE_FIELDS (struct_type);
5057 field = TREE_CHAIN (field))
5059 if (DECL_NAME (field) == NULL
5060 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5066 else if (DECL_NAME (field) == NULL
5067 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5068 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5069 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5079 /* RHS is an expression whose type is pointer to struct. If there is
5080 an anonymous field in RHS with type TYPE, then return a pointer to
5081 that field in RHS. This is used with -fplan9-extensions. This
5082 returns NULL if no conversion could be found. */
5085 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5087 tree rhs_struct_type, lhs_main_type;
5088 tree field, found_field;
5089 bool found_sub_field;
5092 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5093 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5094 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5095 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5097 gcc_assert (POINTER_TYPE_P (type));
5098 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5100 found_field = NULL_TREE;
5101 found_sub_field = false;
5102 for (field = TYPE_FIELDS (rhs_struct_type);
5104 field = TREE_CHAIN (field))
5106 if (DECL_NAME (field) != NULL_TREE
5107 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5108 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5110 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5112 if (found_field != NULL_TREE)
5114 found_field = field;
5116 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5119 if (found_field != NULL_TREE)
5121 found_field = field;
5122 found_sub_field = true;
5126 if (found_field == NULL_TREE)
5129 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5130 build_fold_indirect_ref (rhs), found_field,
5132 ret = build_fold_addr_expr_loc (location, ret);
5134 if (found_sub_field)
5136 ret = convert_to_anonymous_field (location, type, ret);
5137 gcc_assert (ret != NULL_TREE);
5143 /* Convert value RHS to type TYPE as preparation for an assignment to
5144 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5145 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5146 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5147 constant before any folding.
5148 The real work of conversion is done by `convert'.
5149 The purpose of this function is to generate error messages
5150 for assignments that are not allowed in C.
5151 ERRTYPE says whether it is argument passing, assignment,
5152 initialization or return.
5154 LOCATION is the location of the RHS.
5155 FUNCTION is a tree for the function being called.
5156 PARMNUM is the number of the argument, for printing in error messages. */
5159 convert_for_assignment (location_t location, tree type, tree rhs,
5160 tree origtype, enum impl_conv errtype,
5161 bool null_pointer_constant, tree fundecl,
5162 tree function, int parmnum)
5164 enum tree_code codel = TREE_CODE (type);
5165 tree orig_rhs = rhs;
5167 enum tree_code coder;
5168 tree rname = NULL_TREE;
5169 bool objc_ok = false;
5171 if (errtype == ic_argpass)
5174 /* Change pointer to function to the function itself for
5176 if (TREE_CODE (function) == ADDR_EXPR
5177 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5178 function = TREE_OPERAND (function, 0);
5180 /* Handle an ObjC selector specially for diagnostics. */
5181 selector = objc_message_selector ();
5183 if (selector && parmnum > 2)
5190 /* This macro is used to emit diagnostics to ensure that all format
5191 strings are complete sentences, visible to gettext and checked at
5193 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5198 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5199 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5200 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5201 "expected %qT but argument is of type %qT", \
5205 pedwarn (LOCATION, OPT, AS); \
5208 pedwarn_init (LOCATION, OPT, IN); \
5211 pedwarn (LOCATION, OPT, RE); \
5214 gcc_unreachable (); \
5218 /* This macro is used to emit diagnostics to ensure that all format
5219 strings are complete sentences, visible to gettext and checked at
5220 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5221 extra parameter to enumerate qualifiers. */
5223 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5228 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5229 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5230 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5231 "expected %qT but argument is of type %qT", \
5235 pedwarn (LOCATION, OPT, AS, QUALS); \
5238 pedwarn (LOCATION, OPT, IN, QUALS); \
5241 pedwarn (LOCATION, OPT, RE, QUALS); \
5244 gcc_unreachable (); \
5248 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5249 rhs = TREE_OPERAND (rhs, 0);
5251 rhstype = TREE_TYPE (rhs);
5252 coder = TREE_CODE (rhstype);
5254 if (coder == ERROR_MARK)
5255 return error_mark_node;
5257 if (c_dialect_objc ())
5280 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5283 if (warn_cxx_compat)
5285 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5286 if (checktype != error_mark_node
5287 && TREE_CODE (type) == ENUMERAL_TYPE
5288 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5290 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5291 G_("enum conversion when passing argument "
5292 "%d of %qE is invalid in C++"),
5293 G_("enum conversion in assignment is "
5295 G_("enum conversion in initialization is "
5297 G_("enum conversion in return is "
5302 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5305 if (coder == VOID_TYPE)
5307 /* Except for passing an argument to an unprototyped function,
5308 this is a constraint violation. When passing an argument to
5309 an unprototyped function, it is compile-time undefined;
5310 making it a constraint in that case was rejected in
5312 error_at (location, "void value not ignored as it ought to be");
5313 return error_mark_node;
5315 rhs = require_complete_type (rhs);
5316 if (rhs == error_mark_node)
5317 return error_mark_node;
5318 /* A type converts to a reference to it.
5319 This code doesn't fully support references, it's just for the
5320 special case of va_start and va_copy. */
5321 if (codel == REFERENCE_TYPE
5322 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5324 if (!lvalue_p (rhs))
5326 error_at (location, "cannot pass rvalue to reference parameter");
5327 return error_mark_node;
5329 if (!c_mark_addressable (rhs))
5330 return error_mark_node;
5331 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5332 SET_EXPR_LOCATION (rhs, location);
5334 /* We already know that these two types are compatible, but they
5335 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5336 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5337 likely to be va_list, a typedef to __builtin_va_list, which
5338 is different enough that it will cause problems later. */
5339 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5341 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5342 SET_EXPR_LOCATION (rhs, location);
5345 rhs = build1 (NOP_EXPR, type, rhs);
5346 SET_EXPR_LOCATION (rhs, location);
5349 /* Some types can interconvert without explicit casts. */
5350 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5351 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5352 return convert (type, rhs);
5353 /* Arithmetic types all interconvert, and enum is treated like int. */
5354 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5355 || codel == FIXED_POINT_TYPE
5356 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5357 || codel == BOOLEAN_TYPE)
5358 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5359 || coder == FIXED_POINT_TYPE
5360 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5361 || coder == BOOLEAN_TYPE))
5364 bool save = in_late_binary_op;
5365 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5366 in_late_binary_op = true;
5367 ret = convert_and_check (type, orig_rhs);
5368 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5369 in_late_binary_op = save;
5373 /* Aggregates in different TUs might need conversion. */
5374 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5376 && comptypes (type, rhstype))
5377 return convert_and_check (type, rhs);
5379 /* Conversion to a transparent union or record from its member types.
5380 This applies only to function arguments. */
5381 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5382 && TYPE_TRANSPARENT_AGGR (type))
5383 && errtype == ic_argpass)
5385 tree memb, marginal_memb = NULL_TREE;
5387 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5389 tree memb_type = TREE_TYPE (memb);
5391 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5392 TYPE_MAIN_VARIANT (rhstype)))
5395 if (TREE_CODE (memb_type) != POINTER_TYPE)
5398 if (coder == POINTER_TYPE)
5400 tree ttl = TREE_TYPE (memb_type);
5401 tree ttr = TREE_TYPE (rhstype);
5403 /* Any non-function converts to a [const][volatile] void *
5404 and vice versa; otherwise, targets must be the same.
5405 Meanwhile, the lhs target must have all the qualifiers of
5407 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5408 || comp_target_types (location, memb_type, rhstype))
5410 /* If this type won't generate any warnings, use it. */
5411 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5412 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5413 && TREE_CODE (ttl) == FUNCTION_TYPE)
5414 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5415 == TYPE_QUALS (ttr))
5416 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5417 == TYPE_QUALS (ttl))))
5420 /* Keep looking for a better type, but remember this one. */
5422 marginal_memb = memb;
5426 /* Can convert integer zero to any pointer type. */
5427 if (null_pointer_constant)
5429 rhs = null_pointer_node;
5434 if (memb || marginal_memb)
5438 /* We have only a marginally acceptable member type;
5439 it needs a warning. */
5440 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5441 tree ttr = TREE_TYPE (rhstype);
5443 /* Const and volatile mean something different for function
5444 types, so the usual warnings are not appropriate. */
5445 if (TREE_CODE (ttr) == FUNCTION_TYPE
5446 && TREE_CODE (ttl) == FUNCTION_TYPE)
5448 /* Because const and volatile on functions are
5449 restrictions that say the function will not do
5450 certain things, it is okay to use a const or volatile
5451 function where an ordinary one is wanted, but not
5453 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5454 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5455 WARN_FOR_QUALIFIERS (location, 0,
5456 G_("passing argument %d of %qE "
5457 "makes %q#v qualified function "
5458 "pointer from unqualified"),
5459 G_("assignment makes %q#v qualified "
5460 "function pointer from "
5462 G_("initialization makes %q#v qualified "
5463 "function pointer from "
5465 G_("return makes %q#v qualified function "
5466 "pointer from unqualified"),
5467 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5469 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5470 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5471 WARN_FOR_QUALIFIERS (location, 0,
5472 G_("passing argument %d of %qE discards "
5473 "%qv qualifier from pointer target type"),
5474 G_("assignment discards %qv qualifier "
5475 "from pointer target type"),
5476 G_("initialization discards %qv qualifier "
5477 "from pointer target type"),
5478 G_("return discards %qv qualifier from "
5479 "pointer target type"),
5480 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5482 memb = marginal_memb;
5485 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5486 pedwarn (location, OPT_pedantic,
5487 "ISO C prohibits argument conversion to union type");
5489 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5490 return build_constructor_single (type, memb, rhs);
5494 /* Conversions among pointers */
5495 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5496 && (coder == codel))
5498 tree ttl = TREE_TYPE (type);
5499 tree ttr = TREE_TYPE (rhstype);
5502 bool is_opaque_pointer;
5503 int target_cmp = 0; /* Cache comp_target_types () result. */
5507 if (TREE_CODE (mvl) != ARRAY_TYPE)
5508 mvl = TYPE_MAIN_VARIANT (mvl);
5509 if (TREE_CODE (mvr) != ARRAY_TYPE)
5510 mvr = TYPE_MAIN_VARIANT (mvr);
5511 /* Opaque pointers are treated like void pointers. */
5512 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5514 /* The Plan 9 compiler permits a pointer to a struct to be
5515 automatically converted into a pointer to an anonymous field
5516 within the struct. */
5517 if (flag_plan9_extensions
5518 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5519 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5522 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5523 if (new_rhs != NULL_TREE)
5526 rhstype = TREE_TYPE (rhs);
5527 coder = TREE_CODE (rhstype);
5528 ttr = TREE_TYPE (rhstype);
5529 mvr = TYPE_MAIN_VARIANT (ttr);
5533 /* C++ does not allow the implicit conversion void* -> T*. However,
5534 for the purpose of reducing the number of false positives, we
5535 tolerate the special case of
5539 where NULL is typically defined in C to be '(void *) 0'. */
5540 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5541 warning_at (location, OPT_Wc___compat,
5542 "request for implicit conversion "
5543 "from %qT to %qT not permitted in C++", rhstype, type);
5545 /* See if the pointers point to incompatible address spaces. */
5546 asl = TYPE_ADDR_SPACE (ttl);
5547 asr = TYPE_ADDR_SPACE (ttr);
5548 if (!null_pointer_constant_p (rhs)
5549 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5554 error_at (location, "passing argument %d of %qE from pointer to "
5555 "non-enclosed address space", parmnum, rname);
5558 error_at (location, "assignment from pointer to "
5559 "non-enclosed address space");
5562 error_at (location, "initialization from pointer to "
5563 "non-enclosed address space");
5566 error_at (location, "return from pointer to "
5567 "non-enclosed address space");
5572 return error_mark_node;
5575 /* Check if the right-hand side has a format attribute but the
5576 left-hand side doesn't. */
5577 if (warn_missing_format_attribute
5578 && check_missing_format_attribute (type, rhstype))
5583 warning_at (location, OPT_Wmissing_format_attribute,
5584 "argument %d of %qE might be "
5585 "a candidate for a format attribute",
5589 warning_at (location, OPT_Wmissing_format_attribute,
5590 "assignment left-hand side might be "
5591 "a candidate for a format attribute");
5594 warning_at (location, OPT_Wmissing_format_attribute,
5595 "initialization left-hand side might be "
5596 "a candidate for a format attribute");
5599 warning_at (location, OPT_Wmissing_format_attribute,
5600 "return type might be "
5601 "a candidate for a format attribute");
5608 /* Any non-function converts to a [const][volatile] void *
5609 and vice versa; otherwise, targets must be the same.
5610 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5611 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5612 || (target_cmp = comp_target_types (location, type, rhstype))
5613 || is_opaque_pointer
5614 || (c_common_unsigned_type (mvl)
5615 == c_common_unsigned_type (mvr)))
5618 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5621 && !null_pointer_constant
5622 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5623 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5624 G_("ISO C forbids passing argument %d of "
5625 "%qE between function pointer "
5627 G_("ISO C forbids assignment between "
5628 "function pointer and %<void *%>"),
5629 G_("ISO C forbids initialization between "
5630 "function pointer and %<void *%>"),
5631 G_("ISO C forbids return between function "
5632 "pointer and %<void *%>"));
5633 /* Const and volatile mean something different for function types,
5634 so the usual warnings are not appropriate. */
5635 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5636 && TREE_CODE (ttl) != FUNCTION_TYPE)
5638 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5639 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5641 WARN_FOR_QUALIFIERS (location, 0,
5642 G_("passing argument %d of %qE discards "
5643 "%qv qualifier from pointer target type"),
5644 G_("assignment discards %qv qualifier "
5645 "from pointer target type"),
5646 G_("initialization discards %qv qualifier "
5647 "from pointer target type"),
5648 G_("return discards %qv qualifier from "
5649 "pointer target type"),
5650 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5652 /* If this is not a case of ignoring a mismatch in signedness,
5654 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5657 /* If there is a mismatch, do warn. */
5658 else if (warn_pointer_sign)
5659 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5660 G_("pointer targets in passing argument "
5661 "%d of %qE differ in signedness"),
5662 G_("pointer targets in assignment "
5663 "differ in signedness"),
5664 G_("pointer targets in initialization "
5665 "differ in signedness"),
5666 G_("pointer targets in return differ "
5669 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5670 && TREE_CODE (ttr) == FUNCTION_TYPE)
5672 /* Because const and volatile on functions are restrictions
5673 that say the function will not do certain things,
5674 it is okay to use a const or volatile function
5675 where an ordinary one is wanted, but not vice-versa. */
5676 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5677 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5678 WARN_FOR_QUALIFIERS (location, 0,
5679 G_("passing argument %d of %qE makes "
5680 "%q#v qualified function pointer "
5681 "from unqualified"),
5682 G_("assignment makes %q#v qualified function "
5683 "pointer from unqualified"),
5684 G_("initialization makes %q#v qualified "
5685 "function pointer from unqualified"),
5686 G_("return makes %q#v qualified function "
5687 "pointer from unqualified"),
5688 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5692 /* Avoid warning about the volatile ObjC EH puts on decls. */
5694 WARN_FOR_ASSIGNMENT (location, 0,
5695 G_("passing argument %d of %qE from "
5696 "incompatible pointer type"),
5697 G_("assignment from incompatible pointer type"),
5698 G_("initialization from incompatible "
5700 G_("return from incompatible pointer type"));
5702 return convert (type, rhs);
5704 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5706 /* ??? This should not be an error when inlining calls to
5707 unprototyped functions. */
5708 error_at (location, "invalid use of non-lvalue array");
5709 return error_mark_node;
5711 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5713 /* An explicit constant 0 can convert to a pointer,
5714 or one that results from arithmetic, even including
5715 a cast to integer type. */
5716 if (!null_pointer_constant)
5717 WARN_FOR_ASSIGNMENT (location, 0,
5718 G_("passing argument %d of %qE makes "
5719 "pointer from integer without a cast"),
5720 G_("assignment makes pointer from integer "
5722 G_("initialization makes pointer from "
5723 "integer without a cast"),
5724 G_("return makes pointer from integer "
5727 return convert (type, rhs);
5729 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5731 WARN_FOR_ASSIGNMENT (location, 0,
5732 G_("passing argument %d of %qE makes integer "
5733 "from pointer without a cast"),
5734 G_("assignment makes integer from pointer "
5736 G_("initialization makes integer from pointer "
5738 G_("return makes integer from pointer "
5740 return convert (type, rhs);
5742 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5745 bool save = in_late_binary_op;
5746 in_late_binary_op = true;
5747 ret = convert (type, rhs);
5748 in_late_binary_op = save;
5755 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5756 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5757 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5758 "expected %qT but argument is of type %qT", type, rhstype);
5761 error_at (location, "incompatible types when assigning to type %qT from "
5762 "type %qT", type, rhstype);
5766 "incompatible types when initializing type %qT using type %qT",
5771 "incompatible types when returning type %qT but %qT was "
5772 "expected", rhstype, type);
5778 return error_mark_node;
5781 /* If VALUE is a compound expr all of whose expressions are constant, then
5782 return its value. Otherwise, return error_mark_node.
5784 This is for handling COMPOUND_EXPRs as initializer elements
5785 which is allowed with a warning when -pedantic is specified. */
5788 valid_compound_expr_initializer (tree value, tree endtype)
5790 if (TREE_CODE (value) == COMPOUND_EXPR)
5792 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5794 return error_mark_node;
5795 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5798 else if (!initializer_constant_valid_p (value, endtype))
5799 return error_mark_node;
5804 /* Perform appropriate conversions on the initial value of a variable,
5805 store it in the declaration DECL,
5806 and print any error messages that are appropriate.
5807 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5808 If the init is invalid, store an ERROR_MARK.
5810 INIT_LOC is the location of the initial value. */
5813 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5818 /* If variable's type was invalidly declared, just ignore it. */
5820 type = TREE_TYPE (decl);
5821 if (TREE_CODE (type) == ERROR_MARK)
5824 /* Digest the specified initializer into an expression. */
5827 npc = null_pointer_constant_p (init);
5828 value = digest_init (init_loc, type, init, origtype, npc,
5829 true, TREE_STATIC (decl));
5831 /* Store the expression if valid; else report error. */
5833 if (!in_system_header
5834 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5835 warning (OPT_Wtraditional, "traditional C rejects automatic "
5836 "aggregate initialization");
5838 DECL_INITIAL (decl) = value;
5840 /* ANSI wants warnings about out-of-range constant initializers. */
5841 STRIP_TYPE_NOPS (value);
5842 if (TREE_STATIC (decl))
5843 constant_expression_warning (value);
5845 /* Check if we need to set array size from compound literal size. */
5846 if (TREE_CODE (type) == ARRAY_TYPE
5847 && TYPE_DOMAIN (type) == 0
5848 && value != error_mark_node)
5850 tree inside_init = init;
5852 STRIP_TYPE_NOPS (inside_init);
5853 inside_init = fold (inside_init);
5855 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5857 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5859 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5861 /* For int foo[] = (int [3]){1}; we need to set array size
5862 now since later on array initializer will be just the
5863 brace enclosed list of the compound literal. */
5864 tree etype = strip_array_types (TREE_TYPE (decl));
5865 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5866 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5868 layout_decl (cldecl, 0);
5870 = c_build_qualified_type (type, TYPE_QUALS (etype));
5876 /* Methods for storing and printing names for error messages. */
5878 /* Implement a spelling stack that allows components of a name to be pushed
5879 and popped. Each element on the stack is this structure. */
5886 unsigned HOST_WIDE_INT i;
5891 #define SPELLING_STRING 1
5892 #define SPELLING_MEMBER 2
5893 #define SPELLING_BOUNDS 3
5895 static struct spelling *spelling; /* Next stack element (unused). */
5896 static struct spelling *spelling_base; /* Spelling stack base. */
5897 static int spelling_size; /* Size of the spelling stack. */
5899 /* Macros to save and restore the spelling stack around push_... functions.
5900 Alternative to SAVE_SPELLING_STACK. */
5902 #define SPELLING_DEPTH() (spelling - spelling_base)
5903 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5905 /* Push an element on the spelling stack with type KIND and assign VALUE
5908 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5910 int depth = SPELLING_DEPTH (); \
5912 if (depth >= spelling_size) \
5914 spelling_size += 10; \
5915 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5917 RESTORE_SPELLING_DEPTH (depth); \
5920 spelling->kind = (KIND); \
5921 spelling->MEMBER = (VALUE); \
5925 /* Push STRING on the stack. Printed literally. */
5928 push_string (const char *string)
5930 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5933 /* Push a member name on the stack. Printed as '.' STRING. */
5936 push_member_name (tree decl)
5938 const char *const string
5940 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5941 : _("<anonymous>"));
5942 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5945 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5948 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5950 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5953 /* Compute the maximum size in bytes of the printed spelling. */
5956 spelling_length (void)
5961 for (p = spelling_base; p < spelling; p++)
5963 if (p->kind == SPELLING_BOUNDS)
5966 size += strlen (p->u.s) + 1;
5972 /* Print the spelling to BUFFER and return it. */
5975 print_spelling (char *buffer)
5980 for (p = spelling_base; p < spelling; p++)
5981 if (p->kind == SPELLING_BOUNDS)
5983 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5989 if (p->kind == SPELLING_MEMBER)
5991 for (s = p->u.s; (*d = *s++); d++)
5998 /* Issue an error message for a bad initializer component.
5999 GMSGID identifies the message.
6000 The component name is taken from the spelling stack. */
6003 error_init (const char *gmsgid)
6007 /* The gmsgid may be a format string with %< and %>. */
6009 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6011 error ("(near initialization for %qs)", ofwhat);
6014 /* Issue a pedantic warning for a bad initializer component. OPT is
6015 the option OPT_* (from options.h) controlling this warning or 0 if
6016 it is unconditionally given. GMSGID identifies the message. The
6017 component name is taken from the spelling stack. */
6020 pedwarn_init (location_t location, int opt, const char *gmsgid)
6024 /* The gmsgid may be a format string with %< and %>. */
6025 pedwarn (location, opt, gmsgid);
6026 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6028 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6031 /* Issue a warning for a bad initializer component.
6033 OPT is the OPT_W* value corresponding to the warning option that
6034 controls this warning. GMSGID identifies the message. The
6035 component name is taken from the spelling stack. */
6038 warning_init (int opt, const char *gmsgid)
6042 /* The gmsgid may be a format string with %< and %>. */
6043 warning (opt, gmsgid);
6044 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6046 warning (opt, "(near initialization for %qs)", ofwhat);
6049 /* If TYPE is an array type and EXPR is a parenthesized string
6050 constant, warn if pedantic that EXPR is being used to initialize an
6051 object of type TYPE. */
6054 maybe_warn_string_init (tree type, struct c_expr expr)
6057 && TREE_CODE (type) == ARRAY_TYPE
6058 && TREE_CODE (expr.value) == STRING_CST
6059 && expr.original_code != STRING_CST)
6060 pedwarn_init (input_location, OPT_pedantic,
6061 "array initialized from parenthesized string constant");
6064 /* Digest the parser output INIT as an initializer for type TYPE.
6065 Return a C expression of type TYPE to represent the initial value.
6067 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6069 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6071 If INIT is a string constant, STRICT_STRING is true if it is
6072 unparenthesized or we should not warn here for it being parenthesized.
6073 For other types of INIT, STRICT_STRING is not used.
6075 INIT_LOC is the location of the INIT.
6077 REQUIRE_CONSTANT requests an error if non-constant initializers or
6078 elements are seen. */
6081 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6082 bool null_pointer_constant, bool strict_string,
6083 int require_constant)
6085 enum tree_code code = TREE_CODE (type);
6086 tree inside_init = init;
6087 tree semantic_type = NULL_TREE;
6088 bool maybe_const = true;
6090 if (type == error_mark_node
6092 || init == error_mark_node
6093 || TREE_TYPE (init) == error_mark_node)
6094 return error_mark_node;
6096 STRIP_TYPE_NOPS (inside_init);
6098 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6100 semantic_type = TREE_TYPE (inside_init);
6101 inside_init = TREE_OPERAND (inside_init, 0);
6103 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6104 inside_init = decl_constant_value_for_optimization (inside_init);
6106 /* Initialization of an array of chars from a string constant
6107 optionally enclosed in braces. */
6109 if (code == ARRAY_TYPE && inside_init
6110 && TREE_CODE (inside_init) == STRING_CST)
6112 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6113 /* Note that an array could be both an array of character type
6114 and an array of wchar_t if wchar_t is signed char or unsigned
6116 bool char_array = (typ1 == char_type_node
6117 || typ1 == signed_char_type_node
6118 || typ1 == unsigned_char_type_node);
6119 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6120 bool char16_array = !!comptypes (typ1, char16_type_node);
6121 bool char32_array = !!comptypes (typ1, char32_type_node);
6123 if (char_array || wchar_array || char16_array || char32_array)
6126 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6127 expr.value = inside_init;
6128 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6129 expr.original_type = NULL;
6130 maybe_warn_string_init (type, expr);
6132 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6133 pedwarn_init (init_loc, OPT_pedantic,
6134 "initialization of a flexible array member");
6136 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6137 TYPE_MAIN_VARIANT (type)))
6142 if (typ2 != char_type_node)
6144 error_init ("char-array initialized from wide string");
6145 return error_mark_node;
6150 if (typ2 == char_type_node)
6152 error_init ("wide character array initialized from non-wide "
6154 return error_mark_node;
6156 else if (!comptypes(typ1, typ2))
6158 error_init ("wide character array initialized from "
6159 "incompatible wide string");
6160 return error_mark_node;
6164 TREE_TYPE (inside_init) = type;
6165 if (TYPE_DOMAIN (type) != 0
6166 && TYPE_SIZE (type) != 0
6167 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6169 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6171 /* Subtract the size of a single (possibly wide) character
6172 because it's ok to ignore the terminating null char
6173 that is counted in the length of the constant. */
6174 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6176 - (TYPE_PRECISION (typ1)
6178 pedwarn_init (init_loc, 0,
6179 ("initializer-string for array of chars "
6181 else if (warn_cxx_compat
6182 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6183 warning_at (init_loc, OPT_Wc___compat,
6184 ("initializer-string for array chars "
6185 "is too long for C++"));
6190 else if (INTEGRAL_TYPE_P (typ1))
6192 error_init ("array of inappropriate type initialized "
6193 "from string constant");
6194 return error_mark_node;
6198 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6199 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6200 below and handle as a constructor. */
6201 if (code == VECTOR_TYPE
6202 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6203 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6204 && TREE_CONSTANT (inside_init))
6206 if (TREE_CODE (inside_init) == VECTOR_CST
6207 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6208 TYPE_MAIN_VARIANT (type)))
6211 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6213 unsigned HOST_WIDE_INT ix;
6215 bool constant_p = true;
6217 /* Iterate through elements and check if all constructor
6218 elements are *_CSTs. */
6219 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6220 if (!CONSTANT_CLASS_P (value))
6227 return build_vector_from_ctor (type,
6228 CONSTRUCTOR_ELTS (inside_init));
6232 if (warn_sequence_point)
6233 verify_sequence_points (inside_init);
6235 /* Any type can be initialized
6236 from an expression of the same type, optionally with braces. */
6238 if (inside_init && TREE_TYPE (inside_init) != 0
6239 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6240 TYPE_MAIN_VARIANT (type))
6241 || (code == ARRAY_TYPE
6242 && comptypes (TREE_TYPE (inside_init), type))
6243 || (code == VECTOR_TYPE
6244 && comptypes (TREE_TYPE (inside_init), type))
6245 || (code == POINTER_TYPE
6246 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6247 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6248 TREE_TYPE (type)))))
6250 if (code == POINTER_TYPE)
6252 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6254 if (TREE_CODE (inside_init) == STRING_CST
6255 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6256 inside_init = array_to_pointer_conversion
6257 (init_loc, inside_init);
6260 error_init ("invalid use of non-lvalue array");
6261 return error_mark_node;
6266 if (code == VECTOR_TYPE)
6267 /* Although the types are compatible, we may require a
6269 inside_init = convert (type, inside_init);
6271 if (require_constant
6272 && (code == VECTOR_TYPE || !flag_isoc99)
6273 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6275 /* As an extension, allow initializing objects with static storage
6276 duration with compound literals (which are then treated just as
6277 the brace enclosed list they contain). Also allow this for
6278 vectors, as we can only assign them with compound literals. */
6279 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6280 inside_init = DECL_INITIAL (decl);
6283 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6284 && TREE_CODE (inside_init) != CONSTRUCTOR)
6286 error_init ("array initialized from non-constant array expression");
6287 return error_mark_node;
6290 /* Compound expressions can only occur here if -pedantic or
6291 -pedantic-errors is specified. In the later case, we always want
6292 an error. In the former case, we simply want a warning. */
6293 if (require_constant && pedantic
6294 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6297 = valid_compound_expr_initializer (inside_init,
6298 TREE_TYPE (inside_init));
6299 if (inside_init == error_mark_node)
6300 error_init ("initializer element is not constant");
6302 pedwarn_init (init_loc, OPT_pedantic,
6303 "initializer element is not constant");
6304 if (flag_pedantic_errors)
6305 inside_init = error_mark_node;
6307 else if (require_constant
6308 && !initializer_constant_valid_p (inside_init,
6309 TREE_TYPE (inside_init)))
6311 error_init ("initializer element is not constant");
6312 inside_init = error_mark_node;
6314 else if (require_constant && !maybe_const)
6315 pedwarn_init (init_loc, 0,
6316 "initializer element is not a constant expression");
6318 /* Added to enable additional -Wmissing-format-attribute warnings. */
6319 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6320 inside_init = convert_for_assignment (init_loc, type, inside_init,
6322 ic_init, null_pointer_constant,
6323 NULL_TREE, NULL_TREE, 0);
6327 /* Handle scalar types, including conversions. */
6329 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6330 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6331 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6333 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6334 && (TREE_CODE (init) == STRING_CST
6335 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6336 inside_init = init = array_to_pointer_conversion (init_loc, init);
6338 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6341 = convert_for_assignment (init_loc, type, inside_init, origtype,
6342 ic_init, null_pointer_constant,
6343 NULL_TREE, NULL_TREE, 0);
6345 /* Check to see if we have already given an error message. */
6346 if (inside_init == error_mark_node)
6348 else if (require_constant && !TREE_CONSTANT (inside_init))
6350 error_init ("initializer element is not constant");
6351 inside_init = error_mark_node;
6353 else if (require_constant
6354 && !initializer_constant_valid_p (inside_init,
6355 TREE_TYPE (inside_init)))
6357 error_init ("initializer element is not computable at load time");
6358 inside_init = error_mark_node;
6360 else if (require_constant && !maybe_const)
6361 pedwarn_init (init_loc, 0,
6362 "initializer element is not a constant expression");
6367 /* Come here only for records and arrays. */
6369 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6371 error_init ("variable-sized object may not be initialized");
6372 return error_mark_node;
6375 error_init ("invalid initializer");
6376 return error_mark_node;
6379 /* Handle initializers that use braces. */
6381 /* Type of object we are accumulating a constructor for.
6382 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6383 static tree constructor_type;
6385 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6387 static tree constructor_fields;
6389 /* For an ARRAY_TYPE, this is the specified index
6390 at which to store the next element we get. */
6391 static tree constructor_index;
6393 /* For an ARRAY_TYPE, this is the maximum index. */
6394 static tree constructor_max_index;
6396 /* For a RECORD_TYPE, this is the first field not yet written out. */
6397 static tree constructor_unfilled_fields;
6399 /* For an ARRAY_TYPE, this is the index of the first element
6400 not yet written out. */
6401 static tree constructor_unfilled_index;
6403 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6404 This is so we can generate gaps between fields, when appropriate. */
6405 static tree constructor_bit_index;
6407 /* If we are saving up the elements rather than allocating them,
6408 this is the list of elements so far (in reverse order,
6409 most recent first). */
6410 static VEC(constructor_elt,gc) *constructor_elements;
6412 /* 1 if constructor should be incrementally stored into a constructor chain,
6413 0 if all the elements should be kept in AVL tree. */
6414 static int constructor_incremental;
6416 /* 1 if so far this constructor's elements are all compile-time constants. */
6417 static int constructor_constant;
6419 /* 1 if so far this constructor's elements are all valid address constants. */
6420 static int constructor_simple;
6422 /* 1 if this constructor has an element that cannot be part of a
6423 constant expression. */
6424 static int constructor_nonconst;
6426 /* 1 if this constructor is erroneous so far. */
6427 static int constructor_erroneous;
6429 /* Structure for managing pending initializer elements, organized as an
6434 struct init_node *left, *right;
6435 struct init_node *parent;
6442 /* Tree of pending elements at this constructor level.
6443 These are elements encountered out of order
6444 which belong at places we haven't reached yet in actually
6446 Will never hold tree nodes across GC runs. */
6447 static struct init_node *constructor_pending_elts;
6449 /* The SPELLING_DEPTH of this constructor. */
6450 static int constructor_depth;
6452 /* DECL node for which an initializer is being read.
6453 0 means we are reading a constructor expression
6454 such as (struct foo) {...}. */
6455 static tree constructor_decl;
6457 /* Nonzero if this is an initializer for a top-level decl. */
6458 static int constructor_top_level;
6460 /* Nonzero if there were any member designators in this initializer. */
6461 static int constructor_designated;
6463 /* Nesting depth of designator list. */
6464 static int designator_depth;
6466 /* Nonzero if there were diagnosed errors in this designator list. */
6467 static int designator_erroneous;
6470 /* This stack has a level for each implicit or explicit level of
6471 structuring in the initializer, including the outermost one. It
6472 saves the values of most of the variables above. */
6474 struct constructor_range_stack;
6476 struct constructor_stack
6478 struct constructor_stack *next;
6483 tree unfilled_index;
6484 tree unfilled_fields;
6486 VEC(constructor_elt,gc) *elements;
6487 struct init_node *pending_elts;
6490 /* If value nonzero, this value should replace the entire
6491 constructor at this level. */
6492 struct c_expr replacement_value;
6493 struct constructor_range_stack *range_stack;
6504 static struct constructor_stack *constructor_stack;
6506 /* This stack represents designators from some range designator up to
6507 the last designator in the list. */
6509 struct constructor_range_stack
6511 struct constructor_range_stack *next, *prev;
6512 struct constructor_stack *stack;
6519 static struct constructor_range_stack *constructor_range_stack;
6521 /* This stack records separate initializers that are nested.
6522 Nested initializers can't happen in ANSI C, but GNU C allows them
6523 in cases like { ... (struct foo) { ... } ... }. */
6525 struct initializer_stack
6527 struct initializer_stack *next;
6529 struct constructor_stack *constructor_stack;
6530 struct constructor_range_stack *constructor_range_stack;
6531 VEC(constructor_elt,gc) *elements;
6532 struct spelling *spelling;
6533 struct spelling *spelling_base;
6536 char require_constant_value;
6537 char require_constant_elements;
6540 static struct initializer_stack *initializer_stack;
6542 /* Prepare to parse and output the initializer for variable DECL. */
6545 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6548 struct initializer_stack *p = XNEW (struct initializer_stack);
6550 p->decl = constructor_decl;
6551 p->require_constant_value = require_constant_value;
6552 p->require_constant_elements = require_constant_elements;
6553 p->constructor_stack = constructor_stack;
6554 p->constructor_range_stack = constructor_range_stack;
6555 p->elements = constructor_elements;
6556 p->spelling = spelling;
6557 p->spelling_base = spelling_base;
6558 p->spelling_size = spelling_size;
6559 p->top_level = constructor_top_level;
6560 p->next = initializer_stack;
6561 initializer_stack = p;
6563 constructor_decl = decl;
6564 constructor_designated = 0;
6565 constructor_top_level = top_level;
6567 if (decl != 0 && decl != error_mark_node)
6569 require_constant_value = TREE_STATIC (decl);
6570 require_constant_elements
6571 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6572 /* For a scalar, you can always use any value to initialize,
6573 even within braces. */
6574 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6575 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6576 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6577 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6578 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6582 require_constant_value = 0;
6583 require_constant_elements = 0;
6584 locus = _("(anonymous)");
6587 constructor_stack = 0;
6588 constructor_range_stack = 0;
6590 missing_braces_mentioned = 0;
6594 RESTORE_SPELLING_DEPTH (0);
6597 push_string (locus);
6603 struct initializer_stack *p = initializer_stack;
6605 /* Free the whole constructor stack of this initializer. */
6606 while (constructor_stack)
6608 struct constructor_stack *q = constructor_stack;
6609 constructor_stack = q->next;
6613 gcc_assert (!constructor_range_stack);
6615 /* Pop back to the data of the outer initializer (if any). */
6616 free (spelling_base);
6618 constructor_decl = p->decl;
6619 require_constant_value = p->require_constant_value;
6620 require_constant_elements = p->require_constant_elements;
6621 constructor_stack = p->constructor_stack;
6622 constructor_range_stack = p->constructor_range_stack;
6623 constructor_elements = p->elements;
6624 spelling = p->spelling;
6625 spelling_base = p->spelling_base;
6626 spelling_size = p->spelling_size;
6627 constructor_top_level = p->top_level;
6628 initializer_stack = p->next;
6632 /* Call here when we see the initializer is surrounded by braces.
6633 This is instead of a call to push_init_level;
6634 it is matched by a call to pop_init_level.
6636 TYPE is the type to initialize, for a constructor expression.
6637 For an initializer for a decl, TYPE is zero. */
6640 really_start_incremental_init (tree type)
6642 struct constructor_stack *p = XNEW (struct constructor_stack);
6645 type = TREE_TYPE (constructor_decl);
6647 if (TREE_CODE (type) == VECTOR_TYPE
6648 && TYPE_VECTOR_OPAQUE (type))
6649 error ("opaque vector types cannot be initialized");
6651 p->type = constructor_type;
6652 p->fields = constructor_fields;
6653 p->index = constructor_index;
6654 p->max_index = constructor_max_index;
6655 p->unfilled_index = constructor_unfilled_index;
6656 p->unfilled_fields = constructor_unfilled_fields;
6657 p->bit_index = constructor_bit_index;
6658 p->elements = constructor_elements;
6659 p->constant = constructor_constant;
6660 p->simple = constructor_simple;
6661 p->nonconst = constructor_nonconst;
6662 p->erroneous = constructor_erroneous;
6663 p->pending_elts = constructor_pending_elts;
6664 p->depth = constructor_depth;
6665 p->replacement_value.value = 0;
6666 p->replacement_value.original_code = ERROR_MARK;
6667 p->replacement_value.original_type = NULL;
6671 p->incremental = constructor_incremental;
6672 p->designated = constructor_designated;
6674 constructor_stack = p;
6676 constructor_constant = 1;
6677 constructor_simple = 1;
6678 constructor_nonconst = 0;
6679 constructor_depth = SPELLING_DEPTH ();
6680 constructor_elements = 0;
6681 constructor_pending_elts = 0;
6682 constructor_type = type;
6683 constructor_incremental = 1;
6684 constructor_designated = 0;
6685 designator_depth = 0;
6686 designator_erroneous = 0;
6688 if (TREE_CODE (constructor_type) == RECORD_TYPE
6689 || TREE_CODE (constructor_type) == UNION_TYPE)
6691 constructor_fields = TYPE_FIELDS (constructor_type);
6692 /* Skip any nameless bit fields at the beginning. */
6693 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6694 && DECL_NAME (constructor_fields) == 0)
6695 constructor_fields = DECL_CHAIN (constructor_fields);
6697 constructor_unfilled_fields = constructor_fields;
6698 constructor_bit_index = bitsize_zero_node;
6700 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6702 if (TYPE_DOMAIN (constructor_type))
6704 constructor_max_index
6705 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6707 /* Detect non-empty initializations of zero-length arrays. */
6708 if (constructor_max_index == NULL_TREE
6709 && TYPE_SIZE (constructor_type))
6710 constructor_max_index = integer_minus_one_node;
6712 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6713 to initialize VLAs will cause a proper error; avoid tree
6714 checking errors as well by setting a safe value. */
6715 if (constructor_max_index
6716 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6717 constructor_max_index = integer_minus_one_node;
6720 = convert (bitsizetype,
6721 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6725 constructor_index = bitsize_zero_node;
6726 constructor_max_index = NULL_TREE;
6729 constructor_unfilled_index = constructor_index;
6731 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6733 /* Vectors are like simple fixed-size arrays. */
6734 constructor_max_index =
6735 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6736 constructor_index = bitsize_zero_node;
6737 constructor_unfilled_index = constructor_index;
6741 /* Handle the case of int x = {5}; */
6742 constructor_fields = constructor_type;
6743 constructor_unfilled_fields = constructor_type;
6747 /* Push down into a subobject, for initialization.
6748 If this is for an explicit set of braces, IMPLICIT is 0.
6749 If it is because the next element belongs at a lower level,
6750 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6753 push_init_level (int implicit, struct obstack * braced_init_obstack)
6755 struct constructor_stack *p;
6756 tree value = NULL_TREE;
6758 /* If we've exhausted any levels that didn't have braces,
6759 pop them now. If implicit == 1, this will have been done in
6760 process_init_element; do not repeat it here because in the case
6761 of excess initializers for an empty aggregate this leads to an
6762 infinite cycle of popping a level and immediately recreating
6766 while (constructor_stack->implicit)
6768 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6769 || TREE_CODE (constructor_type) == UNION_TYPE)
6770 && constructor_fields == 0)
6771 process_init_element (pop_init_level (1, braced_init_obstack),
6772 true, braced_init_obstack);
6773 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6774 && constructor_max_index
6775 && tree_int_cst_lt (constructor_max_index,
6777 process_init_element (pop_init_level (1, braced_init_obstack),
6778 true, braced_init_obstack);
6784 /* Unless this is an explicit brace, we need to preserve previous
6788 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6789 || TREE_CODE (constructor_type) == UNION_TYPE)
6790 && constructor_fields)
6791 value = find_init_member (constructor_fields, braced_init_obstack);
6792 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6793 value = find_init_member (constructor_index, braced_init_obstack);
6796 p = XNEW (struct constructor_stack);
6797 p->type = constructor_type;
6798 p->fields = constructor_fields;
6799 p->index = constructor_index;
6800 p->max_index = constructor_max_index;
6801 p->unfilled_index = constructor_unfilled_index;
6802 p->unfilled_fields = constructor_unfilled_fields;
6803 p->bit_index = constructor_bit_index;
6804 p->elements = constructor_elements;
6805 p->constant = constructor_constant;
6806 p->simple = constructor_simple;
6807 p->nonconst = constructor_nonconst;
6808 p->erroneous = constructor_erroneous;
6809 p->pending_elts = constructor_pending_elts;
6810 p->depth = constructor_depth;
6811 p->replacement_value.value = 0;
6812 p->replacement_value.original_code = ERROR_MARK;
6813 p->replacement_value.original_type = NULL;
6814 p->implicit = implicit;
6816 p->incremental = constructor_incremental;
6817 p->designated = constructor_designated;
6818 p->next = constructor_stack;
6820 constructor_stack = p;
6822 constructor_constant = 1;
6823 constructor_simple = 1;
6824 constructor_nonconst = 0;
6825 constructor_depth = SPELLING_DEPTH ();
6826 constructor_elements = 0;
6827 constructor_incremental = 1;
6828 constructor_designated = 0;
6829 constructor_pending_elts = 0;
6832 p->range_stack = constructor_range_stack;
6833 constructor_range_stack = 0;
6834 designator_depth = 0;
6835 designator_erroneous = 0;
6838 /* Don't die if an entire brace-pair level is superfluous
6839 in the containing level. */
6840 if (constructor_type == 0)
6842 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6843 || TREE_CODE (constructor_type) == UNION_TYPE)
6845 /* Don't die if there are extra init elts at the end. */
6846 if (constructor_fields == 0)
6847 constructor_type = 0;
6850 constructor_type = TREE_TYPE (constructor_fields);
6851 push_member_name (constructor_fields);
6852 constructor_depth++;
6855 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6857 constructor_type = TREE_TYPE (constructor_type);
6858 push_array_bounds (tree_low_cst (constructor_index, 1));
6859 constructor_depth++;
6862 if (constructor_type == 0)
6864 error_init ("extra brace group at end of initializer");
6865 constructor_fields = 0;
6866 constructor_unfilled_fields = 0;
6870 if (value && TREE_CODE (value) == CONSTRUCTOR)
6872 constructor_constant = TREE_CONSTANT (value);
6873 constructor_simple = TREE_STATIC (value);
6874 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6875 constructor_elements = CONSTRUCTOR_ELTS (value);
6876 if (!VEC_empty (constructor_elt, constructor_elements)
6877 && (TREE_CODE (constructor_type) == RECORD_TYPE
6878 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6879 set_nonincremental_init (braced_init_obstack);
6882 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6884 missing_braces_mentioned = 1;
6885 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6888 if (TREE_CODE (constructor_type) == RECORD_TYPE
6889 || TREE_CODE (constructor_type) == UNION_TYPE)
6891 constructor_fields = TYPE_FIELDS (constructor_type);
6892 /* Skip any nameless bit fields at the beginning. */
6893 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6894 && DECL_NAME (constructor_fields) == 0)
6895 constructor_fields = DECL_CHAIN (constructor_fields);
6897 constructor_unfilled_fields = constructor_fields;
6898 constructor_bit_index = bitsize_zero_node;
6900 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6902 /* Vectors are like simple fixed-size arrays. */
6903 constructor_max_index =
6904 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6905 constructor_index = bitsize_int (0);
6906 constructor_unfilled_index = constructor_index;
6908 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6910 if (TYPE_DOMAIN (constructor_type))
6912 constructor_max_index
6913 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6915 /* Detect non-empty initializations of zero-length arrays. */
6916 if (constructor_max_index == NULL_TREE
6917 && TYPE_SIZE (constructor_type))
6918 constructor_max_index = integer_minus_one_node;
6920 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6921 to initialize VLAs will cause a proper error; avoid tree
6922 checking errors as well by setting a safe value. */
6923 if (constructor_max_index
6924 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6925 constructor_max_index = integer_minus_one_node;
6928 = convert (bitsizetype,
6929 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6932 constructor_index = bitsize_zero_node;
6934 constructor_unfilled_index = constructor_index;
6935 if (value && TREE_CODE (value) == STRING_CST)
6937 /* We need to split the char/wchar array into individual
6938 characters, so that we don't have to special case it
6940 set_nonincremental_init_from_string (value, braced_init_obstack);
6945 if (constructor_type != error_mark_node)
6946 warning_init (0, "braces around scalar initializer");
6947 constructor_fields = constructor_type;
6948 constructor_unfilled_fields = constructor_type;
6952 /* At the end of an implicit or explicit brace level,
6953 finish up that level of constructor. If a single expression
6954 with redundant braces initialized that level, return the
6955 c_expr structure for that expression. Otherwise, the original_code
6956 element is set to ERROR_MARK.
6957 If we were outputting the elements as they are read, return 0 as the value
6958 from inner levels (process_init_element ignores that),
6959 but return error_mark_node as the value from the outermost level
6960 (that's what we want to put in DECL_INITIAL).
6961 Otherwise, return a CONSTRUCTOR expression as the value. */
6964 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6966 struct constructor_stack *p;
6969 ret.original_code = ERROR_MARK;
6970 ret.original_type = NULL;
6974 /* When we come to an explicit close brace,
6975 pop any inner levels that didn't have explicit braces. */
6976 while (constructor_stack->implicit)
6978 process_init_element (pop_init_level (1, braced_init_obstack),
6979 true, braced_init_obstack);
6981 gcc_assert (!constructor_range_stack);
6984 /* Now output all pending elements. */
6985 constructor_incremental = 1;
6986 output_pending_init_elements (1, braced_init_obstack);
6988 p = constructor_stack;
6990 /* Error for initializing a flexible array member, or a zero-length
6991 array member in an inappropriate context. */
6992 if (constructor_type && constructor_fields
6993 && TREE_CODE (constructor_type) == ARRAY_TYPE
6994 && TYPE_DOMAIN (constructor_type)
6995 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6997 /* Silently discard empty initializations. The parser will
6998 already have pedwarned for empty brackets. */
6999 if (integer_zerop (constructor_unfilled_index))
7000 constructor_type = NULL_TREE;
7003 gcc_assert (!TYPE_SIZE (constructor_type));
7005 if (constructor_depth > 2)
7006 error_init ("initialization of flexible array member in a nested context");
7008 pedwarn_init (input_location, OPT_pedantic,
7009 "initialization of a flexible array member");
7011 /* We have already issued an error message for the existence
7012 of a flexible array member not at the end of the structure.
7013 Discard the initializer so that we do not die later. */
7014 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7015 constructor_type = NULL_TREE;
7019 /* Warn when some struct elements are implicitly initialized to zero. */
7020 if (warn_missing_field_initializers
7022 && TREE_CODE (constructor_type) == RECORD_TYPE
7023 && constructor_unfilled_fields)
7025 bool constructor_zeroinit =
7026 (VEC_length (constructor_elt, constructor_elements) == 1
7028 (VEC_index (constructor_elt, constructor_elements, 0)->value));
7030 /* Do not warn for flexible array members or zero-length arrays. */
7031 while (constructor_unfilled_fields
7032 && (!DECL_SIZE (constructor_unfilled_fields)
7033 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7034 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7036 if (constructor_unfilled_fields
7037 /* Do not warn if this level of the initializer uses member
7038 designators; it is likely to be deliberate. */
7039 && !constructor_designated
7040 /* Do not warn about initializing with ` = {0}'. */
7041 && !constructor_zeroinit)
7043 push_member_name (constructor_unfilled_fields);
7044 warning_init (OPT_Wmissing_field_initializers,
7045 "missing initializer");
7046 RESTORE_SPELLING_DEPTH (constructor_depth);
7050 /* Pad out the end of the structure. */
7051 if (p->replacement_value.value)
7052 /* If this closes a superfluous brace pair,
7053 just pass out the element between them. */
7054 ret = p->replacement_value;
7055 else if (constructor_type == 0)
7057 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7058 && TREE_CODE (constructor_type) != UNION_TYPE
7059 && TREE_CODE (constructor_type) != ARRAY_TYPE
7060 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7062 /* A nonincremental scalar initializer--just return
7063 the element, after verifying there is just one. */
7064 if (VEC_empty (constructor_elt,constructor_elements))
7066 if (!constructor_erroneous)
7067 error_init ("empty scalar initializer");
7068 ret.value = error_mark_node;
7070 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7072 error_init ("extra elements in scalar initializer");
7073 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7076 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7080 if (constructor_erroneous)
7081 ret.value = error_mark_node;
7084 ret.value = build_constructor (constructor_type,
7085 constructor_elements);
7086 if (constructor_constant)
7087 TREE_CONSTANT (ret.value) = 1;
7088 if (constructor_constant && constructor_simple)
7089 TREE_STATIC (ret.value) = 1;
7090 if (constructor_nonconst)
7091 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7095 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7097 if (constructor_nonconst)
7098 ret.original_code = C_MAYBE_CONST_EXPR;
7099 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7100 ret.original_code = ERROR_MARK;
7103 constructor_type = p->type;
7104 constructor_fields = p->fields;
7105 constructor_index = p->index;
7106 constructor_max_index = p->max_index;
7107 constructor_unfilled_index = p->unfilled_index;
7108 constructor_unfilled_fields = p->unfilled_fields;
7109 constructor_bit_index = p->bit_index;
7110 constructor_elements = p->elements;
7111 constructor_constant = p->constant;
7112 constructor_simple = p->simple;
7113 constructor_nonconst = p->nonconst;
7114 constructor_erroneous = p->erroneous;
7115 constructor_incremental = p->incremental;
7116 constructor_designated = p->designated;
7117 constructor_pending_elts = p->pending_elts;
7118 constructor_depth = p->depth;
7120 constructor_range_stack = p->range_stack;
7121 RESTORE_SPELLING_DEPTH (constructor_depth);
7123 constructor_stack = p->next;
7126 if (ret.value == 0 && constructor_stack == 0)
7127 ret.value = error_mark_node;
7131 /* Common handling for both array range and field name designators.
7132 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7135 set_designator (int array, struct obstack * braced_init_obstack)
7138 enum tree_code subcode;
7140 /* Don't die if an entire brace-pair level is superfluous
7141 in the containing level. */
7142 if (constructor_type == 0)
7145 /* If there were errors in this designator list already, bail out
7147 if (designator_erroneous)
7150 if (!designator_depth)
7152 gcc_assert (!constructor_range_stack);
7154 /* Designator list starts at the level of closest explicit
7156 while (constructor_stack->implicit)
7158 process_init_element (pop_init_level (1, braced_init_obstack),
7159 true, braced_init_obstack);
7161 constructor_designated = 1;
7165 switch (TREE_CODE (constructor_type))
7169 subtype = TREE_TYPE (constructor_fields);
7170 if (subtype != error_mark_node)
7171 subtype = TYPE_MAIN_VARIANT (subtype);
7174 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7180 subcode = TREE_CODE (subtype);
7181 if (array && subcode != ARRAY_TYPE)
7183 error_init ("array index in non-array initializer");
7186 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7188 error_init ("field name not in record or union initializer");
7192 constructor_designated = 1;
7193 push_init_level (2, braced_init_obstack);
7197 /* If there are range designators in designator list, push a new designator
7198 to constructor_range_stack. RANGE_END is end of such stack range or
7199 NULL_TREE if there is no range designator at this level. */
7202 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7204 struct constructor_range_stack *p;
7206 p = (struct constructor_range_stack *)
7207 obstack_alloc (braced_init_obstack,
7208 sizeof (struct constructor_range_stack));
7209 p->prev = constructor_range_stack;
7211 p->fields = constructor_fields;
7212 p->range_start = constructor_index;
7213 p->index = constructor_index;
7214 p->stack = constructor_stack;
7215 p->range_end = range_end;
7216 if (constructor_range_stack)
7217 constructor_range_stack->next = p;
7218 constructor_range_stack = p;
7221 /* Within an array initializer, specify the next index to be initialized.
7222 FIRST is that index. If LAST is nonzero, then initialize a range
7223 of indices, running from FIRST through LAST. */
7226 set_init_index (tree first, tree last,
7227 struct obstack * braced_init_obstack)
7229 if (set_designator (1, braced_init_obstack))
7232 designator_erroneous = 1;
7234 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7235 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7237 error_init ("array index in initializer not of integer type");
7241 if (TREE_CODE (first) != INTEGER_CST)
7243 first = c_fully_fold (first, false, NULL);
7244 if (TREE_CODE (first) == INTEGER_CST)
7245 pedwarn_init (input_location, OPT_pedantic,
7246 "array index in initializer is not "
7247 "an integer constant expression");
7250 if (last && TREE_CODE (last) != INTEGER_CST)
7252 last = c_fully_fold (last, false, NULL);
7253 if (TREE_CODE (last) == INTEGER_CST)
7254 pedwarn_init (input_location, OPT_pedantic,
7255 "array index in initializer is not "
7256 "an integer constant expression");
7259 if (TREE_CODE (first) != INTEGER_CST)
7260 error_init ("nonconstant array index in initializer");
7261 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7262 error_init ("nonconstant array index in initializer");
7263 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7264 error_init ("array index in non-array initializer");
7265 else if (tree_int_cst_sgn (first) == -1)
7266 error_init ("array index in initializer exceeds array bounds");
7267 else if (constructor_max_index
7268 && tree_int_cst_lt (constructor_max_index, first))
7269 error_init ("array index in initializer exceeds array bounds");
7272 constant_expression_warning (first);
7274 constant_expression_warning (last);
7275 constructor_index = convert (bitsizetype, first);
7279 if (tree_int_cst_equal (first, last))
7281 else if (tree_int_cst_lt (last, first))
7283 error_init ("empty index range in initializer");
7288 last = convert (bitsizetype, last);
7289 if (constructor_max_index != 0
7290 && tree_int_cst_lt (constructor_max_index, last))
7292 error_init ("array index range in initializer exceeds array bounds");
7299 designator_erroneous = 0;
7300 if (constructor_range_stack || last)
7301 push_range_stack (last, braced_init_obstack);
7305 /* Within a struct initializer, specify the next field to be initialized. */
7308 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7312 if (set_designator (0, braced_init_obstack))
7315 designator_erroneous = 1;
7317 if (TREE_CODE (constructor_type) != RECORD_TYPE
7318 && TREE_CODE (constructor_type) != UNION_TYPE)
7320 error_init ("field name not in record or union initializer");
7324 field = lookup_field (constructor_type, fieldname);
7327 error ("unknown field %qE specified in initializer", fieldname);
7331 constructor_fields = TREE_VALUE (field);
7333 designator_erroneous = 0;
7334 if (constructor_range_stack)
7335 push_range_stack (NULL_TREE, braced_init_obstack);
7336 field = TREE_CHAIN (field);
7339 if (set_designator (0, braced_init_obstack))
7343 while (field != NULL_TREE);
7346 /* Add a new initializer to the tree of pending initializers. PURPOSE
7347 identifies the initializer, either array index or field in a structure.
7348 VALUE is the value of that index or field. If ORIGTYPE is not
7349 NULL_TREE, it is the original type of VALUE.
7351 IMPLICIT is true if value comes from pop_init_level (1),
7352 the new initializer has been merged with the existing one
7353 and thus no warnings should be emitted about overriding an
7354 existing initializer. */
7357 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7358 struct obstack * braced_init_obstack)
7360 struct init_node *p, **q, *r;
7362 q = &constructor_pending_elts;
7365 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7370 if (tree_int_cst_lt (purpose, p->purpose))
7372 else if (tree_int_cst_lt (p->purpose, purpose))
7378 if (TREE_SIDE_EFFECTS (p->value))
7379 warning_init (0, "initialized field with side-effects overwritten");
7380 else if (warn_override_init)
7381 warning_init (OPT_Woverride_init, "initialized field overwritten");
7384 p->origtype = origtype;
7393 bitpos = bit_position (purpose);
7397 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7399 else if (p->purpose != purpose)
7405 if (TREE_SIDE_EFFECTS (p->value))
7406 warning_init (0, "initialized field with side-effects overwritten");
7407 else if (warn_override_init)
7408 warning_init (OPT_Woverride_init, "initialized field overwritten");
7411 p->origtype = origtype;
7417 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7418 sizeof (struct init_node));
7419 r->purpose = purpose;
7421 r->origtype = origtype;
7431 struct init_node *s;
7435 if (p->balance == 0)
7437 else if (p->balance < 0)
7444 p->left->parent = p;
7461 constructor_pending_elts = r;
7466 struct init_node *t = r->right;
7470 r->right->parent = r;
7475 p->left->parent = p;
7478 p->balance = t->balance < 0;
7479 r->balance = -(t->balance > 0);
7494 constructor_pending_elts = t;
7500 /* p->balance == +1; growth of left side balances the node. */
7505 else /* r == p->right */
7507 if (p->balance == 0)
7508 /* Growth propagation from right side. */
7510 else if (p->balance > 0)
7517 p->right->parent = p;
7534 constructor_pending_elts = r;
7536 else /* r->balance == -1 */
7539 struct init_node *t = r->left;
7543 r->left->parent = r;
7548 p->right->parent = p;
7551 r->balance = (t->balance < 0);
7552 p->balance = -(t->balance > 0);
7567 constructor_pending_elts = t;
7573 /* p->balance == -1; growth of right side balances the node. */
7584 /* Build AVL tree from a sorted chain. */
7587 set_nonincremental_init (struct obstack * braced_init_obstack)
7589 unsigned HOST_WIDE_INT ix;
7592 if (TREE_CODE (constructor_type) != RECORD_TYPE
7593 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7596 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7598 add_pending_init (index, value, NULL_TREE, false,
7599 braced_init_obstack);
7601 constructor_elements = 0;
7602 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7604 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7605 /* Skip any nameless bit fields at the beginning. */
7606 while (constructor_unfilled_fields != 0
7607 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7608 && DECL_NAME (constructor_unfilled_fields) == 0)
7609 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7612 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7614 if (TYPE_DOMAIN (constructor_type))
7615 constructor_unfilled_index
7616 = convert (bitsizetype,
7617 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7619 constructor_unfilled_index = bitsize_zero_node;
7621 constructor_incremental = 0;
7624 /* Build AVL tree from a string constant. */
7627 set_nonincremental_init_from_string (tree str,
7628 struct obstack * braced_init_obstack)
7630 tree value, purpose, type;
7631 HOST_WIDE_INT val[2];
7632 const char *p, *end;
7633 int byte, wchar_bytes, charwidth, bitpos;
7635 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7637 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7638 charwidth = TYPE_PRECISION (char_type_node);
7639 type = TREE_TYPE (constructor_type);
7640 p = TREE_STRING_POINTER (str);
7641 end = p + TREE_STRING_LENGTH (str);
7643 for (purpose = bitsize_zero_node;
7644 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7645 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7647 if (wchar_bytes == 1)
7649 val[1] = (unsigned char) *p++;
7656 for (byte = 0; byte < wchar_bytes; byte++)
7658 if (BYTES_BIG_ENDIAN)
7659 bitpos = (wchar_bytes - byte - 1) * charwidth;
7661 bitpos = byte * charwidth;
7662 val[bitpos < HOST_BITS_PER_WIDE_INT]
7663 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7664 << (bitpos % HOST_BITS_PER_WIDE_INT);
7668 if (!TYPE_UNSIGNED (type))
7670 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7671 if (bitpos < HOST_BITS_PER_WIDE_INT)
7673 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7675 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7679 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7684 else if (val[0] & (((HOST_WIDE_INT) 1)
7685 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7686 val[0] |= ((HOST_WIDE_INT) -1)
7687 << (bitpos - HOST_BITS_PER_WIDE_INT);
7690 value = build_int_cst_wide (type, val[1], val[0]);
7691 add_pending_init (purpose, value, NULL_TREE, false,
7692 braced_init_obstack);
7695 constructor_incremental = 0;
7698 /* Return value of FIELD in pending initializer or zero if the field was
7699 not initialized yet. */
7702 find_init_member (tree field, struct obstack * braced_init_obstack)
7704 struct init_node *p;
7706 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7708 if (constructor_incremental
7709 && tree_int_cst_lt (field, constructor_unfilled_index))
7710 set_nonincremental_init (braced_init_obstack);
7712 p = constructor_pending_elts;
7715 if (tree_int_cst_lt (field, p->purpose))
7717 else if (tree_int_cst_lt (p->purpose, field))
7723 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7725 tree bitpos = bit_position (field);
7727 if (constructor_incremental
7728 && (!constructor_unfilled_fields
7729 || tree_int_cst_lt (bitpos,
7730 bit_position (constructor_unfilled_fields))))
7731 set_nonincremental_init (braced_init_obstack);
7733 p = constructor_pending_elts;
7736 if (field == p->purpose)
7738 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7744 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7746 if (!VEC_empty (constructor_elt, constructor_elements)
7747 && (VEC_last (constructor_elt, constructor_elements)->index
7749 return VEC_last (constructor_elt, constructor_elements)->value;
7754 /* "Output" the next constructor element.
7755 At top level, really output it to assembler code now.
7756 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7757 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7758 TYPE is the data type that the containing data type wants here.
7759 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7760 If VALUE is a string constant, STRICT_STRING is true if it is
7761 unparenthesized or we should not warn here for it being parenthesized.
7762 For other types of VALUE, STRICT_STRING is not used.
7764 PENDING if non-nil means output pending elements that belong
7765 right after this element. (PENDING is normally 1;
7766 it is 0 while outputting pending elements, to avoid recursion.)
7768 IMPLICIT is true if value comes from pop_init_level (1),
7769 the new initializer has been merged with the existing one
7770 and thus no warnings should be emitted about overriding an
7771 existing initializer. */
7774 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7775 tree field, int pending, bool implicit,
7776 struct obstack * braced_init_obstack)
7778 tree semantic_type = NULL_TREE;
7779 constructor_elt *celt;
7780 bool maybe_const = true;
7783 if (type == error_mark_node || value == error_mark_node)
7785 constructor_erroneous = 1;
7788 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7789 && (TREE_CODE (value) == STRING_CST
7790 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7791 && !(TREE_CODE (value) == STRING_CST
7792 && TREE_CODE (type) == ARRAY_TYPE
7793 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7794 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7795 TYPE_MAIN_VARIANT (type)))
7796 value = array_to_pointer_conversion (input_location, value);
7798 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7799 && require_constant_value && !flag_isoc99 && pending)
7801 /* As an extension, allow initializing objects with static storage
7802 duration with compound literals (which are then treated just as
7803 the brace enclosed list they contain). */
7804 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7805 value = DECL_INITIAL (decl);
7808 npc = null_pointer_constant_p (value);
7809 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7811 semantic_type = TREE_TYPE (value);
7812 value = TREE_OPERAND (value, 0);
7814 value = c_fully_fold (value, require_constant_value, &maybe_const);
7816 if (value == error_mark_node)
7817 constructor_erroneous = 1;
7818 else if (!TREE_CONSTANT (value))
7819 constructor_constant = 0;
7820 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7821 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7822 || TREE_CODE (constructor_type) == UNION_TYPE)
7823 && DECL_C_BIT_FIELD (field)
7824 && TREE_CODE (value) != INTEGER_CST))
7825 constructor_simple = 0;
7827 constructor_nonconst = 1;
7829 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7831 if (require_constant_value)
7833 error_init ("initializer element is not constant");
7834 value = error_mark_node;
7836 else if (require_constant_elements)
7837 pedwarn (input_location, 0,
7838 "initializer element is not computable at load time");
7840 else if (!maybe_const
7841 && (require_constant_value || require_constant_elements))
7842 pedwarn_init (input_location, 0,
7843 "initializer element is not a constant expression");
7845 /* Issue -Wc++-compat warnings about initializing a bitfield with
7848 && field != NULL_TREE
7849 && TREE_CODE (field) == FIELD_DECL
7850 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7851 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7852 != TYPE_MAIN_VARIANT (type))
7853 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7855 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7856 if (checktype != error_mark_node
7857 && (TYPE_MAIN_VARIANT (checktype)
7858 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7859 warning_init (OPT_Wc___compat,
7860 "enum conversion in initialization is invalid in C++");
7863 /* If this field is empty (and not at the end of structure),
7864 don't do anything other than checking the initializer. */
7866 && (TREE_TYPE (field) == error_mark_node
7867 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7868 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7869 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7870 || DECL_CHAIN (field)))))
7874 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7875 value = digest_init (input_location, type, value, origtype, npc,
7876 strict_string, require_constant_value);
7877 if (value == error_mark_node)
7879 constructor_erroneous = 1;
7882 if (require_constant_value || require_constant_elements)
7883 constant_expression_warning (value);
7885 /* If this element doesn't come next in sequence,
7886 put it on constructor_pending_elts. */
7887 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7888 && (!constructor_incremental
7889 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7891 if (constructor_incremental
7892 && tree_int_cst_lt (field, constructor_unfilled_index))
7893 set_nonincremental_init (braced_init_obstack);
7895 add_pending_init (field, value, origtype, implicit,
7896 braced_init_obstack);
7899 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7900 && (!constructor_incremental
7901 || field != constructor_unfilled_fields))
7903 /* We do this for records but not for unions. In a union,
7904 no matter which field is specified, it can be initialized
7905 right away since it starts at the beginning of the union. */
7906 if (constructor_incremental)
7908 if (!constructor_unfilled_fields)
7909 set_nonincremental_init (braced_init_obstack);
7912 tree bitpos, unfillpos;
7914 bitpos = bit_position (field);
7915 unfillpos = bit_position (constructor_unfilled_fields);
7917 if (tree_int_cst_lt (bitpos, unfillpos))
7918 set_nonincremental_init (braced_init_obstack);
7922 add_pending_init (field, value, origtype, implicit,
7923 braced_init_obstack);
7926 else if (TREE_CODE (constructor_type) == UNION_TYPE
7927 && !VEC_empty (constructor_elt, constructor_elements))
7931 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7932 constructor_elements)->value))
7934 "initialized field with side-effects overwritten");
7935 else if (warn_override_init)
7936 warning_init (OPT_Woverride_init, "initialized field overwritten");
7939 /* We can have just one union field set. */
7940 constructor_elements = 0;
7943 /* Otherwise, output this element either to
7944 constructor_elements or to the assembler file. */
7946 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7947 celt->index = field;
7948 celt->value = value;
7950 /* Advance the variable that indicates sequential elements output. */
7951 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7952 constructor_unfilled_index
7953 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7955 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7957 constructor_unfilled_fields
7958 = DECL_CHAIN (constructor_unfilled_fields);
7960 /* Skip any nameless bit fields. */
7961 while (constructor_unfilled_fields != 0
7962 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7963 && DECL_NAME (constructor_unfilled_fields) == 0)
7964 constructor_unfilled_fields =
7965 DECL_CHAIN (constructor_unfilled_fields);
7967 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7968 constructor_unfilled_fields = 0;
7970 /* Now output any pending elements which have become next. */
7972 output_pending_init_elements (0, braced_init_obstack);
7975 /* Output any pending elements which have become next.
7976 As we output elements, constructor_unfilled_{fields,index}
7977 advances, which may cause other elements to become next;
7978 if so, they too are output.
7980 If ALL is 0, we return when there are
7981 no more pending elements to output now.
7983 If ALL is 1, we output space as necessary so that
7984 we can output all the pending elements. */
7986 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7988 struct init_node *elt = constructor_pending_elts;
7993 /* Look through the whole pending tree.
7994 If we find an element that should be output now,
7995 output it. Otherwise, set NEXT to the element
7996 that comes first among those still pending. */
8001 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8003 if (tree_int_cst_equal (elt->purpose,
8004 constructor_unfilled_index))
8005 output_init_element (elt->value, elt->origtype, true,
8006 TREE_TYPE (constructor_type),
8007 constructor_unfilled_index, 0, false,
8008 braced_init_obstack);
8009 else if (tree_int_cst_lt (constructor_unfilled_index,
8012 /* Advance to the next smaller node. */
8017 /* We have reached the smallest node bigger than the
8018 current unfilled index. Fill the space first. */
8019 next = elt->purpose;
8025 /* Advance to the next bigger node. */
8030 /* We have reached the biggest node in a subtree. Find
8031 the parent of it, which is the next bigger node. */
8032 while (elt->parent && elt->parent->right == elt)
8035 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8038 next = elt->purpose;
8044 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8045 || TREE_CODE (constructor_type) == UNION_TYPE)
8047 tree ctor_unfilled_bitpos, elt_bitpos;
8049 /* If the current record is complete we are done. */
8050 if (constructor_unfilled_fields == 0)
8053 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8054 elt_bitpos = bit_position (elt->purpose);
8055 /* We can't compare fields here because there might be empty
8056 fields in between. */
8057 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8059 constructor_unfilled_fields = elt->purpose;
8060 output_init_element (elt->value, elt->origtype, true,
8061 TREE_TYPE (elt->purpose),
8062 elt->purpose, 0, false,
8063 braced_init_obstack);
8065 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8067 /* Advance to the next smaller node. */
8072 /* We have reached the smallest node bigger than the
8073 current unfilled field. Fill the space first. */
8074 next = elt->purpose;
8080 /* Advance to the next bigger node. */
8085 /* We have reached the biggest node in a subtree. Find
8086 the parent of it, which is the next bigger node. */
8087 while (elt->parent && elt->parent->right == elt)
8091 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8092 bit_position (elt->purpose))))
8094 next = elt->purpose;
8102 /* Ordinarily return, but not if we want to output all
8103 and there are elements left. */
8104 if (!(all && next != 0))
8107 /* If it's not incremental, just skip over the gap, so that after
8108 jumping to retry we will output the next successive element. */
8109 if (TREE_CODE (constructor_type) == RECORD_TYPE
8110 || TREE_CODE (constructor_type) == UNION_TYPE)
8111 constructor_unfilled_fields = next;
8112 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8113 constructor_unfilled_index = next;
8115 /* ELT now points to the node in the pending tree with the next
8116 initializer to output. */
8120 /* Add one non-braced element to the current constructor level.
8121 This adjusts the current position within the constructor's type.
8122 This may also start or terminate implicit levels
8123 to handle a partly-braced initializer.
8125 Once this has found the correct level for the new element,
8126 it calls output_init_element.
8128 IMPLICIT is true if value comes from pop_init_level (1),
8129 the new initializer has been merged with the existing one
8130 and thus no warnings should be emitted about overriding an
8131 existing initializer. */
8134 process_init_element (struct c_expr value, bool implicit,
8135 struct obstack * braced_init_obstack)
8137 tree orig_value = value.value;
8138 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8139 bool strict_string = value.original_code == STRING_CST;
8141 designator_depth = 0;
8142 designator_erroneous = 0;
8144 /* Handle superfluous braces around string cst as in
8145 char x[] = {"foo"}; */
8148 && TREE_CODE (constructor_type) == ARRAY_TYPE
8149 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8150 && integer_zerop (constructor_unfilled_index))
8152 if (constructor_stack->replacement_value.value)
8153 error_init ("excess elements in char array initializer");
8154 constructor_stack->replacement_value = value;
8158 if (constructor_stack->replacement_value.value != 0)
8160 error_init ("excess elements in struct initializer");
8164 /* Ignore elements of a brace group if it is entirely superfluous
8165 and has already been diagnosed. */
8166 if (constructor_type == 0)
8169 /* If we've exhausted any levels that didn't have braces,
8171 while (constructor_stack->implicit)
8173 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8174 || TREE_CODE (constructor_type) == UNION_TYPE)
8175 && constructor_fields == 0)
8176 process_init_element (pop_init_level (1, braced_init_obstack),
8177 true, braced_init_obstack);
8178 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8179 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8180 && (constructor_max_index == 0
8181 || tree_int_cst_lt (constructor_max_index,
8182 constructor_index)))
8183 process_init_element (pop_init_level (1, braced_init_obstack),
8184 true, braced_init_obstack);
8189 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8190 if (constructor_range_stack)
8192 /* If value is a compound literal and we'll be just using its
8193 content, don't put it into a SAVE_EXPR. */
8194 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8195 || !require_constant_value
8198 tree semantic_type = NULL_TREE;
8199 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8201 semantic_type = TREE_TYPE (value.value);
8202 value.value = TREE_OPERAND (value.value, 0);
8204 value.value = c_save_expr (value.value);
8206 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8213 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8216 enum tree_code fieldcode;
8218 if (constructor_fields == 0)
8220 pedwarn_init (input_location, 0,
8221 "excess elements in struct initializer");
8225 fieldtype = TREE_TYPE (constructor_fields);
8226 if (fieldtype != error_mark_node)
8227 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8228 fieldcode = TREE_CODE (fieldtype);
8230 /* Error for non-static initialization of a flexible array member. */
8231 if (fieldcode == ARRAY_TYPE
8232 && !require_constant_value
8233 && TYPE_SIZE (fieldtype) == NULL_TREE
8234 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8236 error_init ("non-static initialization of a flexible array member");
8240 /* Accept a string constant to initialize a subarray. */
8241 if (value.value != 0
8242 && fieldcode == ARRAY_TYPE
8243 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8245 value.value = orig_value;
8246 /* Otherwise, if we have come to a subaggregate,
8247 and we don't have an element of its type, push into it. */
8248 else if (value.value != 0
8249 && value.value != error_mark_node
8250 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8251 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8252 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8254 push_init_level (1, braced_init_obstack);
8260 push_member_name (constructor_fields);
8261 output_init_element (value.value, value.original_type,
8262 strict_string, fieldtype,
8263 constructor_fields, 1, implicit,
8264 braced_init_obstack);
8265 RESTORE_SPELLING_DEPTH (constructor_depth);
8268 /* Do the bookkeeping for an element that was
8269 directly output as a constructor. */
8271 /* For a record, keep track of end position of last field. */
8272 if (DECL_SIZE (constructor_fields))
8273 constructor_bit_index
8274 = size_binop_loc (input_location, PLUS_EXPR,
8275 bit_position (constructor_fields),
8276 DECL_SIZE (constructor_fields));
8278 /* If the current field was the first one not yet written out,
8279 it isn't now, so update. */
8280 if (constructor_unfilled_fields == constructor_fields)
8282 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8283 /* Skip any nameless bit fields. */
8284 while (constructor_unfilled_fields != 0
8285 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8286 && DECL_NAME (constructor_unfilled_fields) == 0)
8287 constructor_unfilled_fields =
8288 DECL_CHAIN (constructor_unfilled_fields);
8292 constructor_fields = DECL_CHAIN (constructor_fields);
8293 /* Skip any nameless bit fields at the beginning. */
8294 while (constructor_fields != 0
8295 && DECL_C_BIT_FIELD (constructor_fields)
8296 && DECL_NAME (constructor_fields) == 0)
8297 constructor_fields = DECL_CHAIN (constructor_fields);
8299 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8302 enum tree_code fieldcode;
8304 if (constructor_fields == 0)
8306 pedwarn_init (input_location, 0,
8307 "excess elements in union initializer");
8311 fieldtype = TREE_TYPE (constructor_fields);
8312 if (fieldtype != error_mark_node)
8313 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8314 fieldcode = TREE_CODE (fieldtype);
8316 /* Warn that traditional C rejects initialization of unions.
8317 We skip the warning if the value is zero. This is done
8318 under the assumption that the zero initializer in user
8319 code appears conditioned on e.g. __STDC__ to avoid
8320 "missing initializer" warnings and relies on default
8321 initialization to zero in the traditional C case.
8322 We also skip the warning if the initializer is designated,
8323 again on the assumption that this must be conditional on
8324 __STDC__ anyway (and we've already complained about the
8325 member-designator already). */
8326 if (!in_system_header && !constructor_designated
8327 && !(value.value && (integer_zerop (value.value)
8328 || real_zerop (value.value))))
8329 warning (OPT_Wtraditional, "traditional C rejects initialization "
8332 /* Accept a string constant to initialize a subarray. */
8333 if (value.value != 0
8334 && fieldcode == ARRAY_TYPE
8335 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8337 value.value = orig_value;
8338 /* Otherwise, if we have come to a subaggregate,
8339 and we don't have an element of its type, push into it. */
8340 else if (value.value != 0
8341 && value.value != error_mark_node
8342 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8343 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8344 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8346 push_init_level (1, braced_init_obstack);
8352 push_member_name (constructor_fields);
8353 output_init_element (value.value, value.original_type,
8354 strict_string, fieldtype,
8355 constructor_fields, 1, implicit,
8356 braced_init_obstack);
8357 RESTORE_SPELLING_DEPTH (constructor_depth);
8360 /* Do the bookkeeping for an element that was
8361 directly output as a constructor. */
8363 constructor_bit_index = DECL_SIZE (constructor_fields);
8364 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8367 constructor_fields = 0;
8369 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8371 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8372 enum tree_code eltcode = TREE_CODE (elttype);
8374 /* Accept a string constant to initialize a subarray. */
8375 if (value.value != 0
8376 && eltcode == ARRAY_TYPE
8377 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8379 value.value = orig_value;
8380 /* Otherwise, if we have come to a subaggregate,
8381 and we don't have an element of its type, push into it. */
8382 else if (value.value != 0
8383 && value.value != error_mark_node
8384 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8385 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8386 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8388 push_init_level (1, braced_init_obstack);
8392 if (constructor_max_index != 0
8393 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8394 || integer_all_onesp (constructor_max_index)))
8396 pedwarn_init (input_location, 0,
8397 "excess elements in array initializer");
8401 /* Now output the actual element. */
8404 push_array_bounds (tree_low_cst (constructor_index, 1));
8405 output_init_element (value.value, value.original_type,
8406 strict_string, elttype,
8407 constructor_index, 1, implicit,
8408 braced_init_obstack);
8409 RESTORE_SPELLING_DEPTH (constructor_depth);
8413 = size_binop_loc (input_location, PLUS_EXPR,
8414 constructor_index, bitsize_one_node);
8417 /* If we are doing the bookkeeping for an element that was
8418 directly output as a constructor, we must update
8419 constructor_unfilled_index. */
8420 constructor_unfilled_index = constructor_index;
8422 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8424 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8426 /* Do a basic check of initializer size. Note that vectors
8427 always have a fixed size derived from their type. */
8428 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8430 pedwarn_init (input_location, 0,
8431 "excess elements in vector initializer");
8435 /* Now output the actual element. */
8438 if (TREE_CODE (value.value) == VECTOR_CST)
8439 elttype = TYPE_MAIN_VARIANT (constructor_type);
8440 output_init_element (value.value, value.original_type,
8441 strict_string, elttype,
8442 constructor_index, 1, implicit,
8443 braced_init_obstack);
8447 = size_binop_loc (input_location,
8448 PLUS_EXPR, constructor_index, bitsize_one_node);
8451 /* If we are doing the bookkeeping for an element that was
8452 directly output as a constructor, we must update
8453 constructor_unfilled_index. */
8454 constructor_unfilled_index = constructor_index;
8457 /* Handle the sole element allowed in a braced initializer
8458 for a scalar variable. */
8459 else if (constructor_type != error_mark_node
8460 && constructor_fields == 0)
8462 pedwarn_init (input_location, 0,
8463 "excess elements in scalar initializer");
8469 output_init_element (value.value, value.original_type,
8470 strict_string, constructor_type,
8471 NULL_TREE, 1, implicit,
8472 braced_init_obstack);
8473 constructor_fields = 0;
8476 /* Handle range initializers either at this level or anywhere higher
8477 in the designator stack. */
8478 if (constructor_range_stack)
8480 struct constructor_range_stack *p, *range_stack;
8483 range_stack = constructor_range_stack;
8484 constructor_range_stack = 0;
8485 while (constructor_stack != range_stack->stack)
8487 gcc_assert (constructor_stack->implicit);
8488 process_init_element (pop_init_level (1,
8489 braced_init_obstack),
8490 true, braced_init_obstack);
8492 for (p = range_stack;
8493 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8496 gcc_assert (constructor_stack->implicit);
8497 process_init_element (pop_init_level (1, braced_init_obstack),
8498 true, braced_init_obstack);
8501 p->index = size_binop_loc (input_location,
8502 PLUS_EXPR, p->index, bitsize_one_node);
8503 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8508 constructor_index = p->index;
8509 constructor_fields = p->fields;
8510 if (finish && p->range_end && p->index == p->range_start)
8518 push_init_level (2, braced_init_obstack);
8519 p->stack = constructor_stack;
8520 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8521 p->index = p->range_start;
8525 constructor_range_stack = range_stack;
8532 constructor_range_stack = 0;
8535 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8536 (guaranteed to be 'volatile' or null) and ARGS (represented using
8537 an ASM_EXPR node). */
8539 build_asm_stmt (tree cv_qualifier, tree args)
8541 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8542 ASM_VOLATILE_P (args) = 1;
8543 return add_stmt (args);
8546 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8547 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8548 SIMPLE indicates whether there was anything at all after the
8549 string in the asm expression -- asm("blah") and asm("blah" : )
8550 are subtly different. We use a ASM_EXPR node to represent this. */
8552 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8553 tree clobbers, tree labels, bool simple)
8558 const char *constraint;
8559 const char **oconstraints;
8560 bool allows_mem, allows_reg, is_inout;
8561 int ninputs, noutputs;
8563 ninputs = list_length (inputs);
8564 noutputs = list_length (outputs);
8565 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8567 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8569 /* Remove output conversions that change the type but not the mode. */
8570 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8572 tree output = TREE_VALUE (tail);
8574 /* ??? Really, this should not be here. Users should be using a
8575 proper lvalue, dammit. But there's a long history of using casts
8576 in the output operands. In cases like longlong.h, this becomes a
8577 primitive form of typechecking -- if the cast can be removed, then
8578 the output operand had a type of the proper width; otherwise we'll
8579 get an error. Gross, but ... */
8580 STRIP_NOPS (output);
8582 if (!lvalue_or_else (loc, output, lv_asm))
8583 output = error_mark_node;
8585 if (output != error_mark_node
8586 && (TREE_READONLY (output)
8587 || TYPE_READONLY (TREE_TYPE (output))
8588 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8589 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8590 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8591 readonly_error (output, lv_asm);
8593 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8594 oconstraints[i] = constraint;
8596 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8597 &allows_mem, &allows_reg, &is_inout))
8599 /* If the operand is going to end up in memory,
8600 mark it addressable. */
8601 if (!allows_reg && !c_mark_addressable (output))
8602 output = error_mark_node;
8603 if (!(!allows_reg && allows_mem)
8604 && output != error_mark_node
8605 && VOID_TYPE_P (TREE_TYPE (output)))
8607 error_at (loc, "invalid use of void expression");
8608 output = error_mark_node;
8612 output = error_mark_node;
8614 TREE_VALUE (tail) = output;
8617 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8621 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8622 input = TREE_VALUE (tail);
8624 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8625 oconstraints, &allows_mem, &allows_reg))
8627 /* If the operand is going to end up in memory,
8628 mark it addressable. */
8629 if (!allows_reg && allows_mem)
8631 /* Strip the nops as we allow this case. FIXME, this really
8632 should be rejected or made deprecated. */
8634 if (!c_mark_addressable (input))
8635 input = error_mark_node;
8637 else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8639 error_at (loc, "invalid use of void expression");
8640 input = error_mark_node;
8644 input = error_mark_node;
8646 TREE_VALUE (tail) = input;
8649 /* ASMs with labels cannot have outputs. This should have been
8650 enforced by the parser. */
8651 gcc_assert (outputs == NULL || labels == NULL);
8653 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8655 /* asm statements without outputs, including simple ones, are treated
8657 ASM_INPUT_P (args) = simple;
8658 ASM_VOLATILE_P (args) = (noutputs == 0);
8663 /* Generate a goto statement to LABEL. LOC is the location of the
8667 c_finish_goto_label (location_t loc, tree label)
8669 tree decl = lookup_label_for_goto (loc, label);
8672 TREE_USED (decl) = 1;
8674 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8675 SET_EXPR_LOCATION (t, loc);
8676 return add_stmt (t);
8680 /* Generate a computed goto statement to EXPR. LOC is the location of
8684 c_finish_goto_ptr (location_t loc, tree expr)
8687 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8688 expr = c_fully_fold (expr, false, NULL);
8689 expr = convert (ptr_type_node, expr);
8690 t = build1 (GOTO_EXPR, void_type_node, expr);
8691 SET_EXPR_LOCATION (t, loc);
8692 return add_stmt (t);
8695 /* Generate a C `return' statement. RETVAL is the expression for what
8696 to return, or a null pointer for `return;' with no value. LOC is
8697 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8698 is the original type of RETVAL. */
8701 c_finish_return (location_t loc, tree retval, tree origtype)
8703 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8704 bool no_warning = false;
8707 if (TREE_THIS_VOLATILE (current_function_decl))
8709 "function declared %<noreturn%> has a %<return%> statement");
8713 tree semantic_type = NULL_TREE;
8714 npc = null_pointer_constant_p (retval);
8715 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8717 semantic_type = TREE_TYPE (retval);
8718 retval = TREE_OPERAND (retval, 0);
8720 retval = c_fully_fold (retval, false, NULL);
8722 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8727 current_function_returns_null = 1;
8728 if ((warn_return_type || flag_isoc99)
8729 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8731 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8732 "%<return%> with no value, in "
8733 "function returning non-void");
8737 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8739 current_function_returns_null = 1;
8740 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8742 "%<return%> with a value, in function returning void");
8744 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8745 "%<return%> with expression, in function returning void");
8749 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8751 npc, NULL_TREE, NULL_TREE, 0);
8752 tree res = DECL_RESULT (current_function_decl);
8755 current_function_returns_value = 1;
8756 if (t == error_mark_node)
8759 inner = t = convert (TREE_TYPE (res), t);
8761 /* Strip any conversions, additions, and subtractions, and see if
8762 we are returning the address of a local variable. Warn if so. */
8765 switch (TREE_CODE (inner))
8768 case NON_LVALUE_EXPR:
8770 case POINTER_PLUS_EXPR:
8771 inner = TREE_OPERAND (inner, 0);
8775 /* If the second operand of the MINUS_EXPR has a pointer
8776 type (or is converted from it), this may be valid, so
8777 don't give a warning. */
8779 tree op1 = TREE_OPERAND (inner, 1);
8781 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8782 && (CONVERT_EXPR_P (op1)
8783 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8784 op1 = TREE_OPERAND (op1, 0);
8786 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8789 inner = TREE_OPERAND (inner, 0);
8794 inner = TREE_OPERAND (inner, 0);
8796 while (REFERENCE_CLASS_P (inner)
8797 && TREE_CODE (inner) != INDIRECT_REF)
8798 inner = TREE_OPERAND (inner, 0);
8801 && !DECL_EXTERNAL (inner)
8802 && !TREE_STATIC (inner)
8803 && DECL_CONTEXT (inner) == current_function_decl)
8805 0, "function returns address of local variable");
8815 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8816 SET_EXPR_LOCATION (retval, loc);
8818 if (warn_sequence_point)
8819 verify_sequence_points (retval);
8822 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8823 TREE_NO_WARNING (ret_stmt) |= no_warning;
8824 return add_stmt (ret_stmt);
8828 /* The SWITCH_EXPR being built. */
8831 /* The original type of the testing expression, i.e. before the
8832 default conversion is applied. */
8835 /* A splay-tree mapping the low element of a case range to the high
8836 element, or NULL_TREE if there is no high element. Used to
8837 determine whether or not a new case label duplicates an old case
8838 label. We need a tree, rather than simply a hash table, because
8839 of the GNU case range extension. */
8842 /* The bindings at the point of the switch. This is used for
8843 warnings crossing decls when branching to a case label. */
8844 struct c_spot_bindings *bindings;
8846 /* The next node on the stack. */
8847 struct c_switch *next;
8850 /* A stack of the currently active switch statements. The innermost
8851 switch statement is on the top of the stack. There is no need to
8852 mark the stack for garbage collection because it is only active
8853 during the processing of the body of a function, and we never
8854 collect at that point. */
8856 struct c_switch *c_switch_stack;
8858 /* Start a C switch statement, testing expression EXP. Return the new
8859 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8860 SWITCH_COND_LOC is the location of the switch's condition. */
8863 c_start_case (location_t switch_loc,
8864 location_t switch_cond_loc,
8867 tree orig_type = error_mark_node;
8868 struct c_switch *cs;
8870 if (exp != error_mark_node)
8872 orig_type = TREE_TYPE (exp);
8874 if (!INTEGRAL_TYPE_P (orig_type))
8876 if (orig_type != error_mark_node)
8878 error_at (switch_cond_loc, "switch quantity not an integer");
8879 orig_type = error_mark_node;
8881 exp = integer_zero_node;
8885 tree type = TYPE_MAIN_VARIANT (orig_type);
8887 if (!in_system_header
8888 && (type == long_integer_type_node
8889 || type == long_unsigned_type_node))
8890 warning_at (switch_cond_loc,
8891 OPT_Wtraditional, "%<long%> switch expression not "
8892 "converted to %<int%> in ISO C");
8894 exp = c_fully_fold (exp, false, NULL);
8895 exp = default_conversion (exp);
8897 if (warn_sequence_point)
8898 verify_sequence_points (exp);
8902 /* Add this new SWITCH_EXPR to the stack. */
8903 cs = XNEW (struct c_switch);
8904 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8905 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8906 cs->orig_type = orig_type;
8907 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8908 cs->bindings = c_get_switch_bindings ();
8909 cs->next = c_switch_stack;
8910 c_switch_stack = cs;
8912 return add_stmt (cs->switch_expr);
8915 /* Process a case label at location LOC. */
8918 do_case (location_t loc, tree low_value, tree high_value)
8920 tree label = NULL_TREE;
8922 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8924 low_value = c_fully_fold (low_value, false, NULL);
8925 if (TREE_CODE (low_value) == INTEGER_CST)
8926 pedwarn (input_location, OPT_pedantic,
8927 "case label is not an integer constant expression");
8930 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8932 high_value = c_fully_fold (high_value, false, NULL);
8933 if (TREE_CODE (high_value) == INTEGER_CST)
8934 pedwarn (input_location, OPT_pedantic,
8935 "case label is not an integer constant expression");
8938 if (c_switch_stack == NULL)
8941 error_at (loc, "case label not within a switch statement");
8943 error_at (loc, "%<default%> label not within a switch statement");
8947 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8948 EXPR_LOCATION (c_switch_stack->switch_expr),
8952 label = c_add_case_label (loc, c_switch_stack->cases,
8953 SWITCH_COND (c_switch_stack->switch_expr),
8954 c_switch_stack->orig_type,
8955 low_value, high_value);
8956 if (label == error_mark_node)
8961 /* Finish the switch statement. */
8964 c_finish_case (tree body)
8966 struct c_switch *cs = c_switch_stack;
8967 location_t switch_location;
8969 SWITCH_BODY (cs->switch_expr) = body;
8971 /* Emit warnings as needed. */
8972 switch_location = EXPR_LOCATION (cs->switch_expr);
8973 c_do_switch_warnings (cs->cases, switch_location,
8974 TREE_TYPE (cs->switch_expr),
8975 SWITCH_COND (cs->switch_expr));
8977 /* Pop the stack. */
8978 c_switch_stack = cs->next;
8979 splay_tree_delete (cs->cases);
8980 c_release_switch_bindings (cs->bindings);
8984 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8985 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8986 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8987 statement, and was not surrounded with parenthesis. */
8990 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8991 tree else_block, bool nested_if)
8995 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8996 if (warn_parentheses && nested_if && else_block == NULL)
8998 tree inner_if = then_block;
9000 /* We know from the grammar productions that there is an IF nested
9001 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9002 it might not be exactly THEN_BLOCK, but should be the last
9003 non-container statement within. */
9005 switch (TREE_CODE (inner_if))
9010 inner_if = BIND_EXPR_BODY (inner_if);
9012 case STATEMENT_LIST:
9013 inner_if = expr_last (then_block);
9015 case TRY_FINALLY_EXPR:
9016 case TRY_CATCH_EXPR:
9017 inner_if = TREE_OPERAND (inner_if, 0);
9024 if (COND_EXPR_ELSE (inner_if))
9025 warning_at (if_locus, OPT_Wparentheses,
9026 "suggest explicit braces to avoid ambiguous %<else%>");
9029 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9030 SET_EXPR_LOCATION (stmt, if_locus);
9034 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9035 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9036 is false for DO loops. INCR is the FOR increment expression. BODY is
9037 the statement controlled by the loop. BLAB is the break label. CLAB is
9038 the continue label. Everything is allowed to be NULL. */
9041 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9042 tree blab, tree clab, bool cond_is_first)
9044 tree entry = NULL, exit = NULL, t;
9046 /* If the condition is zero don't generate a loop construct. */
9047 if (cond && integer_zerop (cond))
9051 t = build_and_jump (&blab);
9052 SET_EXPR_LOCATION (t, start_locus);
9058 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9060 /* If we have an exit condition, then we build an IF with gotos either
9061 out of the loop, or to the top of it. If there's no exit condition,
9062 then we just build a jump back to the top. */
9063 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9065 if (cond && !integer_nonzerop (cond))
9067 /* Canonicalize the loop condition to the end. This means
9068 generating a branch to the loop condition. Reuse the
9069 continue label, if possible. */
9074 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9075 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9078 t = build1 (GOTO_EXPR, void_type_node, clab);
9079 SET_EXPR_LOCATION (t, start_locus);
9083 t = build_and_jump (&blab);
9085 exit = fold_build3_loc (start_locus,
9086 COND_EXPR, void_type_node, cond, exit, t);
9088 exit = fold_build3_loc (input_location,
9089 COND_EXPR, void_type_node, cond, exit, t);
9098 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9106 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9110 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9113 tree label = *label_p;
9115 /* In switch statements break is sometimes stylistically used after
9116 a return statement. This can lead to spurious warnings about
9117 control reaching the end of a non-void function when it is
9118 inlined. Note that we are calling block_may_fallthru with
9119 language specific tree nodes; this works because
9120 block_may_fallthru returns true when given something it does not
9122 skip = !block_may_fallthru (cur_stmt_list);
9127 *label_p = label = create_artificial_label (loc);
9129 else if (TREE_CODE (label) == LABEL_DECL)
9131 else switch (TREE_INT_CST_LOW (label))
9135 error_at (loc, "break statement not within loop or switch");
9137 error_at (loc, "continue statement not within a loop");
9141 gcc_assert (is_break);
9142 error_at (loc, "break statement used with OpenMP for loop");
9153 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9155 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9158 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9161 emit_side_effect_warnings (location_t loc, tree expr)
9163 if (expr == error_mark_node)
9165 else if (!TREE_SIDE_EFFECTS (expr))
9167 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9168 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9171 warn_if_unused_value (expr, loc);
9174 /* Process an expression as if it were a complete statement. Emit
9175 diagnostics, but do not call ADD_STMT. LOC is the location of the
9179 c_process_expr_stmt (location_t loc, tree expr)
9186 expr = c_fully_fold (expr, false, NULL);
9188 if (warn_sequence_point)
9189 verify_sequence_points (expr);
9191 if (TREE_TYPE (expr) != error_mark_node
9192 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9193 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9194 error_at (loc, "expression statement has incomplete type");
9196 /* If we're not processing a statement expression, warn about unused values.
9197 Warnings for statement expressions will be emitted later, once we figure
9198 out which is the result. */
9199 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9200 && warn_unused_value)
9201 emit_side_effect_warnings (loc, expr);
9204 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9205 exprv = TREE_OPERAND (exprv, 1);
9206 while (CONVERT_EXPR_P (exprv))
9207 exprv = TREE_OPERAND (exprv, 0);
9209 || handled_component_p (exprv)
9210 || TREE_CODE (exprv) == ADDR_EXPR)
9211 mark_exp_read (exprv);
9213 /* If the expression is not of a type to which we cannot assign a line
9214 number, wrap the thing in a no-op NOP_EXPR. */
9215 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9217 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9218 SET_EXPR_LOCATION (expr, loc);
9224 /* Emit an expression as a statement. LOC is the location of the
9228 c_finish_expr_stmt (location_t loc, tree expr)
9231 return add_stmt (c_process_expr_stmt (loc, expr));
9236 /* Do the opposite and emit a statement as an expression. To begin,
9237 create a new binding level and return it. */
9240 c_begin_stmt_expr (void)
9244 /* We must force a BLOCK for this level so that, if it is not expanded
9245 later, there is a way to turn off the entire subtree of blocks that
9246 are contained in it. */
9248 ret = c_begin_compound_stmt (true);
9250 c_bindings_start_stmt_expr (c_switch_stack == NULL
9252 : c_switch_stack->bindings);
9254 /* Mark the current statement list as belonging to a statement list. */
9255 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9260 /* LOC is the location of the compound statement to which this body
9264 c_finish_stmt_expr (location_t loc, tree body)
9266 tree last, type, tmp, val;
9269 body = c_end_compound_stmt (loc, body, true);
9271 c_bindings_end_stmt_expr (c_switch_stack == NULL
9273 : c_switch_stack->bindings);
9275 /* Locate the last statement in BODY. See c_end_compound_stmt
9276 about always returning a BIND_EXPR. */
9277 last_p = &BIND_EXPR_BODY (body);
9278 last = BIND_EXPR_BODY (body);
9281 if (TREE_CODE (last) == STATEMENT_LIST)
9283 tree_stmt_iterator i;
9285 /* This can happen with degenerate cases like ({ }). No value. */
9286 if (!TREE_SIDE_EFFECTS (last))
9289 /* If we're supposed to generate side effects warnings, process
9290 all of the statements except the last. */
9291 if (warn_unused_value)
9293 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9296 tree t = tsi_stmt (i);
9298 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9299 emit_side_effect_warnings (tloc, t);
9303 i = tsi_last (last);
9304 last_p = tsi_stmt_ptr (i);
9308 /* If the end of the list is exception related, then the list was split
9309 by a call to push_cleanup. Continue searching. */
9310 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9311 || TREE_CODE (last) == TRY_CATCH_EXPR)
9313 last_p = &TREE_OPERAND (last, 0);
9315 goto continue_searching;
9318 if (last == error_mark_node)
9321 /* In the case that the BIND_EXPR is not necessary, return the
9322 expression out from inside it. */
9323 if (last == BIND_EXPR_BODY (body)
9324 && BIND_EXPR_VARS (body) == NULL)
9326 /* Even if this looks constant, do not allow it in a constant
9328 last = c_wrap_maybe_const (last, true);
9329 /* Do not warn if the return value of a statement expression is
9331 TREE_NO_WARNING (last) = 1;
9335 /* Extract the type of said expression. */
9336 type = TREE_TYPE (last);
9338 /* If we're not returning a value at all, then the BIND_EXPR that
9339 we already have is a fine expression to return. */
9340 if (!type || VOID_TYPE_P (type))
9343 /* Now that we've located the expression containing the value, it seems
9344 silly to make voidify_wrapper_expr repeat the process. Create a
9345 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9346 tmp = create_tmp_var_raw (type, NULL);
9348 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9349 tree_expr_nonnegative_p giving up immediately. */
9351 if (TREE_CODE (val) == NOP_EXPR
9352 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9353 val = TREE_OPERAND (val, 0);
9355 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9356 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9359 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9360 SET_EXPR_LOCATION (t, loc);
9365 /* Begin and end compound statements. This is as simple as pushing
9366 and popping new statement lists from the tree. */
9369 c_begin_compound_stmt (bool do_scope)
9371 tree stmt = push_stmt_list ();
9377 /* End a compound statement. STMT is the statement. LOC is the
9378 location of the compound statement-- this is usually the location
9379 of the opening brace. */
9382 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9388 if (c_dialect_objc ())
9389 objc_clear_super_receiver ();
9390 block = pop_scope ();
9393 stmt = pop_stmt_list (stmt);
9394 stmt = c_build_bind_expr (loc, block, stmt);
9396 /* If this compound statement is nested immediately inside a statement
9397 expression, then force a BIND_EXPR to be created. Otherwise we'll
9398 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9399 STATEMENT_LISTs merge, and thus we can lose track of what statement
9401 if (building_stmt_list_p ()
9402 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9403 && TREE_CODE (stmt) != BIND_EXPR)
9405 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9406 TREE_SIDE_EFFECTS (stmt) = 1;
9407 SET_EXPR_LOCATION (stmt, loc);
9413 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9414 when the current scope is exited. EH_ONLY is true when this is not
9415 meant to apply to normal control flow transfer. */
9418 push_cleanup (tree decl, tree cleanup, bool eh_only)
9420 enum tree_code code;
9424 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9425 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9427 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9428 list = push_stmt_list ();
9429 TREE_OPERAND (stmt, 0) = list;
9430 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9433 /* Convert scalar to vector for the range of operations. */
9434 static enum stv_conv
9435 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1)
9437 tree type0 = TREE_TYPE (op0);
9438 tree type1 = TREE_TYPE (op1);
9439 bool integer_only_op = false;
9440 enum stv_conv ret = stv_firstarg;
9442 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
9443 || TREE_CODE (type1) == VECTOR_TYPE);
9448 if (TREE_CODE (type0) == INTEGER_TYPE
9449 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9451 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9453 error_at (loc, "conversion of scalar to vector "
9454 "involves truncation");
9458 return stv_firstarg;
9465 integer_only_op = true;
9466 /* ... fall through ... */
9471 case TRUNC_DIV_EXPR:
9472 case TRUNC_MOD_EXPR:
9474 if (TREE_CODE (type0) == VECTOR_TYPE)
9477 ret = stv_secondarg;
9478 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
9479 tmp = type0; type0 = type1; type1 = tmp;
9480 tmp = op0; op0 = op1; op1 = tmp;
9483 if (TREE_CODE (type0) == INTEGER_TYPE
9484 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9486 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9488 error_at (loc, "conversion of scalar to vector "
9489 "involves truncation");
9494 else if (!integer_only_op
9495 /* Allow integer --> real conversion if safe. */
9496 && (TREE_CODE (type0) == REAL_TYPE
9497 || TREE_CODE (type0) == INTEGER_TYPE)
9498 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9500 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9502 error_at (loc, "conversion of scalar to vector "
9503 "involves truncation");
9515 /* Build a binary-operation expression without default conversions.
9516 CODE is the kind of expression to build.
9517 LOCATION is the operator's location.
9518 This function differs from `build' in several ways:
9519 the data type of the result is computed and recorded in it,
9520 warnings are generated if arg data types are invalid,
9521 special handling for addition and subtraction of pointers is known,
9522 and some optimization is done (operations on narrow ints
9523 are done in the narrower type when that gives the same result).
9524 Constant folding is also done before the result is returned.
9526 Note that the operands will never have enumeral types, or function
9527 or array types, because either they will have the default conversions
9528 performed or they have both just been converted to some other type in which
9529 the arithmetic is to be done. */
9532 build_binary_op (location_t location, enum tree_code code,
9533 tree orig_op0, tree orig_op1, int convert_p)
9535 tree type0, type1, orig_type0, orig_type1;
9537 enum tree_code code0, code1;
9539 tree ret = error_mark_node;
9540 const char *invalid_op_diag;
9541 bool op0_int_operands, op1_int_operands;
9542 bool int_const, int_const_or_overflow, int_operands;
9544 /* Expression code to give to the expression when it is built.
9545 Normally this is CODE, which is what the caller asked for,
9546 but in some special cases we change it. */
9547 enum tree_code resultcode = code;
9549 /* Data type in which the computation is to be performed.
9550 In the simplest cases this is the common type of the arguments. */
9551 tree result_type = NULL;
9553 /* When the computation is in excess precision, the type of the
9554 final EXCESS_PRECISION_EXPR. */
9555 tree semantic_result_type = NULL;
9557 /* Nonzero means operands have already been type-converted
9558 in whatever way is necessary.
9559 Zero means they need to be converted to RESULT_TYPE. */
9562 /* Nonzero means create the expression with this type, rather than
9564 tree build_type = 0;
9566 /* Nonzero means after finally constructing the expression
9567 convert it to this type. */
9568 tree final_type = 0;
9570 /* Nonzero if this is an operation like MIN or MAX which can
9571 safely be computed in short if both args are promoted shorts.
9572 Also implies COMMON.
9573 -1 indicates a bitwise operation; this makes a difference
9574 in the exact conditions for when it is safe to do the operation
9575 in a narrower mode. */
9578 /* Nonzero if this is a comparison operation;
9579 if both args are promoted shorts, compare the original shorts.
9580 Also implies COMMON. */
9581 int short_compare = 0;
9583 /* Nonzero if this is a right-shift operation, which can be computed on the
9584 original short and then promoted if the operand is a promoted short. */
9585 int short_shift = 0;
9587 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9590 /* True means types are compatible as far as ObjC is concerned. */
9593 /* True means this is an arithmetic operation that may need excess
9595 bool may_need_excess_precision;
9597 /* True means this is a boolean operation that converts both its
9598 operands to truth-values. */
9599 bool boolean_op = false;
9601 if (location == UNKNOWN_LOCATION)
9602 location = input_location;
9607 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9608 if (op0_int_operands)
9609 op0 = remove_c_maybe_const_expr (op0);
9610 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9611 if (op1_int_operands)
9612 op1 = remove_c_maybe_const_expr (op1);
9613 int_operands = (op0_int_operands && op1_int_operands);
9616 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9617 && TREE_CODE (orig_op1) == INTEGER_CST);
9618 int_const = (int_const_or_overflow
9619 && !TREE_OVERFLOW (orig_op0)
9620 && !TREE_OVERFLOW (orig_op1));
9623 int_const = int_const_or_overflow = false;
9625 /* Do not apply default conversion in mixed vector/scalar expression. */
9627 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9628 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9630 op0 = default_conversion (op0);
9631 op1 = default_conversion (op1);
9634 orig_type0 = type0 = TREE_TYPE (op0);
9635 orig_type1 = type1 = TREE_TYPE (op1);
9637 /* The expression codes of the data types of the arguments tell us
9638 whether the arguments are integers, floating, pointers, etc. */
9639 code0 = TREE_CODE (type0);
9640 code1 = TREE_CODE (type1);
9642 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9643 STRIP_TYPE_NOPS (op0);
9644 STRIP_TYPE_NOPS (op1);
9646 /* If an error was already reported for one of the arguments,
9647 avoid reporting another error. */
9649 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9650 return error_mark_node;
9652 if ((invalid_op_diag
9653 = targetm.invalid_binary_op (code, type0, type1)))
9655 error_at (location, invalid_op_diag);
9656 return error_mark_node;
9664 case TRUNC_DIV_EXPR:
9666 case FLOOR_DIV_EXPR:
9667 case ROUND_DIV_EXPR:
9668 case EXACT_DIV_EXPR:
9669 may_need_excess_precision = true;
9672 may_need_excess_precision = false;
9675 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9677 op0 = TREE_OPERAND (op0, 0);
9678 type0 = TREE_TYPE (op0);
9680 else if (may_need_excess_precision
9681 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9684 op0 = convert (eptype, op0);
9686 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9688 op1 = TREE_OPERAND (op1, 0);
9689 type1 = TREE_TYPE (op1);
9691 else if (may_need_excess_precision
9692 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9695 op1 = convert (eptype, op1);
9698 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9700 /* In case when one of the operands of the binary operation is
9701 a vector and another is a scalar -- convert scalar to vector. */
9702 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9704 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1);
9706 switch (convert_flag)
9709 return error_mark_node;
9712 bool maybe_const = true;
9714 sc = c_fully_fold (op0, false, &maybe_const);
9715 sc = save_expr (sc);
9716 sc = convert (TREE_TYPE (type1), sc);
9717 op0 = build_vector_from_val (type1, sc);
9719 op0 = c_wrap_maybe_const (op0, true);
9720 orig_type0 = type0 = TREE_TYPE (op0);
9721 code0 = TREE_CODE (type0);
9727 bool maybe_const = true;
9729 sc = c_fully_fold (op1, false, &maybe_const);
9730 sc = save_expr (sc);
9731 sc = convert (TREE_TYPE (type0), sc);
9732 op1 = build_vector_from_val (type0, sc);
9734 op0 = c_wrap_maybe_const (op1, true);
9735 orig_type1 = type1 = TREE_TYPE (op1);
9736 code1 = TREE_CODE (type1);
9748 /* Handle the pointer + int case. */
9749 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9751 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9752 goto return_build_binary_op;
9754 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9756 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9757 goto return_build_binary_op;
9764 /* Subtraction of two similar pointers.
9765 We must subtract them as integers, then divide by object size. */
9766 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9767 && comp_target_types (location, type0, type1))
9769 ret = pointer_diff (location, op0, op1);
9770 goto return_build_binary_op;
9772 /* Handle pointer minus int. Just like pointer plus int. */
9773 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9775 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9776 goto return_build_binary_op;
9786 case TRUNC_DIV_EXPR:
9788 case FLOOR_DIV_EXPR:
9789 case ROUND_DIV_EXPR:
9790 case EXACT_DIV_EXPR:
9791 warn_for_div_by_zero (location, op1);
9793 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9794 || code0 == FIXED_POINT_TYPE
9795 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9796 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9797 || code1 == FIXED_POINT_TYPE
9798 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9800 enum tree_code tcode0 = code0, tcode1 = code1;
9802 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9803 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9804 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9805 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9807 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9808 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9809 resultcode = RDIV_EXPR;
9811 /* Although it would be tempting to shorten always here, that
9812 loses on some targets, since the modulo instruction is
9813 undefined if the quotient can't be represented in the
9814 computation mode. We shorten only if unsigned or if
9815 dividing by something we know != -1. */
9816 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9817 || (TREE_CODE (op1) == INTEGER_CST
9818 && !integer_all_onesp (op1)));
9826 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9828 /* Allow vector types which are not floating point types. */
9829 else if (code0 == VECTOR_TYPE
9830 && code1 == VECTOR_TYPE
9831 && !VECTOR_FLOAT_TYPE_P (type0)
9832 && !VECTOR_FLOAT_TYPE_P (type1))
9836 case TRUNC_MOD_EXPR:
9837 case FLOOR_MOD_EXPR:
9838 warn_for_div_by_zero (location, op1);
9840 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9841 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9842 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9844 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9846 /* Although it would be tempting to shorten always here, that loses
9847 on some targets, since the modulo instruction is undefined if the
9848 quotient can't be represented in the computation mode. We shorten
9849 only if unsigned or if dividing by something we know != -1. */
9850 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9851 || (TREE_CODE (op1) == INTEGER_CST
9852 && !integer_all_onesp (op1)));
9857 case TRUTH_ANDIF_EXPR:
9858 case TRUTH_ORIF_EXPR:
9859 case TRUTH_AND_EXPR:
9861 case TRUTH_XOR_EXPR:
9862 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9863 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9864 || code0 == FIXED_POINT_TYPE)
9865 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9866 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9867 || code1 == FIXED_POINT_TYPE))
9869 /* Result of these operations is always an int,
9870 but that does not mean the operands should be
9871 converted to ints! */
9872 result_type = integer_type_node;
9873 op0 = c_common_truthvalue_conversion (location, op0);
9874 op1 = c_common_truthvalue_conversion (location, op1);
9878 if (code == TRUTH_ANDIF_EXPR)
9880 int_const_or_overflow = (int_operands
9881 && TREE_CODE (orig_op0) == INTEGER_CST
9882 && (op0 == truthvalue_false_node
9883 || TREE_CODE (orig_op1) == INTEGER_CST));
9884 int_const = (int_const_or_overflow
9885 && !TREE_OVERFLOW (orig_op0)
9886 && (op0 == truthvalue_false_node
9887 || !TREE_OVERFLOW (orig_op1)));
9889 else if (code == TRUTH_ORIF_EXPR)
9891 int_const_or_overflow = (int_operands
9892 && TREE_CODE (orig_op0) == INTEGER_CST
9893 && (op0 == truthvalue_true_node
9894 || TREE_CODE (orig_op1) == INTEGER_CST));
9895 int_const = (int_const_or_overflow
9896 && !TREE_OVERFLOW (orig_op0)
9897 && (op0 == truthvalue_true_node
9898 || !TREE_OVERFLOW (orig_op1)));
9902 /* Shift operations: result has same type as first operand;
9903 always convert second operand to int.
9904 Also set SHORT_SHIFT if shifting rightward. */
9907 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9908 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9910 result_type = type0;
9913 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9914 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9915 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9916 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9918 result_type = type0;
9921 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9922 && code1 == INTEGER_TYPE)
9924 if (TREE_CODE (op1) == INTEGER_CST)
9926 if (tree_int_cst_sgn (op1) < 0)
9929 if (c_inhibit_evaluation_warnings == 0)
9930 warning (0, "right shift count is negative");
9934 if (!integer_zerop (op1))
9937 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9940 if (c_inhibit_evaluation_warnings == 0)
9941 warning (0, "right shift count >= width of type");
9946 /* Use the type of the value to be shifted. */
9947 result_type = type0;
9948 /* Convert the non vector shift-count to an integer, regardless
9949 of size of value being shifted. */
9950 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9951 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9952 op1 = convert (integer_type_node, op1);
9953 /* Avoid converting op1 to result_type later. */
9959 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9960 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9962 result_type = type0;
9965 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9966 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9967 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9968 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9970 result_type = type0;
9973 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9974 && code1 == INTEGER_TYPE)
9976 if (TREE_CODE (op1) == INTEGER_CST)
9978 if (tree_int_cst_sgn (op1) < 0)
9981 if (c_inhibit_evaluation_warnings == 0)
9982 warning (0, "left shift count is negative");
9985 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9988 if (c_inhibit_evaluation_warnings == 0)
9989 warning (0, "left shift count >= width of type");
9993 /* Use the type of the value to be shifted. */
9994 result_type = type0;
9995 /* Convert the non vector shift-count to an integer, regardless
9996 of size of value being shifted. */
9997 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9998 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9999 op1 = convert (integer_type_node, op1);
10000 /* Avoid converting op1 to result_type later. */
10007 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10010 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10012 error_at (location, "comparing vectors with different "
10014 return error_mark_node;
10017 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10019 error_at (location, "comparing vectors with different "
10020 "number of elements");
10021 return error_mark_node;
10024 /* Always construct signed integer vector type. */
10025 intt = c_common_type_for_size (GET_MODE_BITSIZE
10026 (TYPE_MODE (TREE_TYPE (type0))), 0);
10027 result_type = build_opaque_vector_type (intt,
10028 TYPE_VECTOR_SUBPARTS (type0));
10032 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10033 warning_at (location,
10035 "comparing floating point with == or != is unsafe");
10036 /* Result of comparison is always int,
10037 but don't convert the args to int! */
10038 build_type = integer_type_node;
10039 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10040 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10041 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10042 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10044 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10046 if (TREE_CODE (op0) == ADDR_EXPR
10047 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10049 if (code == EQ_EXPR)
10050 warning_at (location,
10052 "the comparison will always evaluate as %<false%> "
10053 "for the address of %qD will never be NULL",
10054 TREE_OPERAND (op0, 0));
10056 warning_at (location,
10058 "the comparison will always evaluate as %<true%> "
10059 "for the address of %qD will never be NULL",
10060 TREE_OPERAND (op0, 0));
10062 result_type = type0;
10064 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10066 if (TREE_CODE (op1) == ADDR_EXPR
10067 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10069 if (code == EQ_EXPR)
10070 warning_at (location,
10072 "the comparison will always evaluate as %<false%> "
10073 "for the address of %qD will never be NULL",
10074 TREE_OPERAND (op1, 0));
10076 warning_at (location,
10078 "the comparison will always evaluate as %<true%> "
10079 "for the address of %qD will never be NULL",
10080 TREE_OPERAND (op1, 0));
10082 result_type = type1;
10084 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10086 tree tt0 = TREE_TYPE (type0);
10087 tree tt1 = TREE_TYPE (type1);
10088 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10089 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10090 addr_space_t as_common = ADDR_SPACE_GENERIC;
10092 /* Anything compares with void *. void * compares with anything.
10093 Otherwise, the targets must be compatible
10094 and both must be object or both incomplete. */
10095 if (comp_target_types (location, type0, type1))
10096 result_type = common_pointer_type (type0, type1);
10097 else if (!addr_space_superset (as0, as1, &as_common))
10099 error_at (location, "comparison of pointers to "
10100 "disjoint address spaces");
10101 return error_mark_node;
10103 else if (VOID_TYPE_P (tt0))
10105 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10106 pedwarn (location, OPT_pedantic, "ISO C forbids "
10107 "comparison of %<void *%> with function pointer");
10109 else if (VOID_TYPE_P (tt1))
10111 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10112 pedwarn (location, OPT_pedantic, "ISO C forbids "
10113 "comparison of %<void *%> with function pointer");
10116 /* Avoid warning about the volatile ObjC EH puts on decls. */
10118 pedwarn (location, 0,
10119 "comparison of distinct pointer types lacks a cast");
10121 if (result_type == NULL_TREE)
10123 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10124 result_type = build_pointer_type
10125 (build_qualified_type (void_type_node, qual));
10128 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10130 result_type = type0;
10131 pedwarn (location, 0, "comparison between pointer and integer");
10133 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10135 result_type = type1;
10136 pedwarn (location, 0, "comparison between pointer and integer");
10144 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10147 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10149 error_at (location, "comparing vectors with different "
10151 return error_mark_node;
10154 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10156 error_at (location, "comparing vectors with different "
10157 "number of elements");
10158 return error_mark_node;
10161 /* Always construct signed integer vector type. */
10162 intt = c_common_type_for_size (GET_MODE_BITSIZE
10163 (TYPE_MODE (TREE_TYPE (type0))), 0);
10164 result_type = build_opaque_vector_type (intt,
10165 TYPE_VECTOR_SUBPARTS (type0));
10169 build_type = integer_type_node;
10170 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10171 || code0 == FIXED_POINT_TYPE)
10172 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10173 || code1 == FIXED_POINT_TYPE))
10175 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10177 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10178 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10179 addr_space_t as_common;
10181 if (comp_target_types (location, type0, type1))
10183 result_type = common_pointer_type (type0, type1);
10184 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10185 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10186 pedwarn (location, 0,
10187 "comparison of complete and incomplete pointers");
10188 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10189 pedwarn (location, OPT_pedantic, "ISO C forbids "
10190 "ordered comparisons of pointers to functions");
10191 else if (null_pointer_constant_p (orig_op0)
10192 || null_pointer_constant_p (orig_op1))
10193 warning_at (location, OPT_Wextra,
10194 "ordered comparison of pointer with null pointer");
10197 else if (!addr_space_superset (as0, as1, &as_common))
10199 error_at (location, "comparison of pointers to "
10200 "disjoint address spaces");
10201 return error_mark_node;
10205 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10206 result_type = build_pointer_type
10207 (build_qualified_type (void_type_node, qual));
10208 pedwarn (location, 0,
10209 "comparison of distinct pointer types lacks a cast");
10212 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10214 result_type = type0;
10216 pedwarn (location, OPT_pedantic,
10217 "ordered comparison of pointer with integer zero");
10218 else if (extra_warnings)
10219 warning_at (location, OPT_Wextra,
10220 "ordered comparison of pointer with integer zero");
10222 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10224 result_type = type1;
10226 pedwarn (location, OPT_pedantic,
10227 "ordered comparison of pointer with integer zero");
10228 else if (extra_warnings)
10229 warning_at (location, OPT_Wextra,
10230 "ordered comparison of pointer with integer zero");
10232 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10234 result_type = type0;
10235 pedwarn (location, 0, "comparison between pointer and integer");
10237 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10239 result_type = type1;
10240 pedwarn (location, 0, "comparison between pointer and integer");
10245 gcc_unreachable ();
10248 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10249 return error_mark_node;
10251 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10252 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10253 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10254 TREE_TYPE (type1))))
10256 binary_op_error (location, code, type0, type1);
10257 return error_mark_node;
10260 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10261 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10263 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10264 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10266 bool first_complex = (code0 == COMPLEX_TYPE);
10267 bool second_complex = (code1 == COMPLEX_TYPE);
10268 int none_complex = (!first_complex && !second_complex);
10270 if (shorten || common || short_compare)
10272 result_type = c_common_type (type0, type1);
10273 do_warn_double_promotion (result_type, type0, type1,
10274 "implicit conversion from %qT to %qT "
10275 "to match other operand of binary "
10278 if (result_type == error_mark_node)
10279 return error_mark_node;
10282 if (first_complex != second_complex
10283 && (code == PLUS_EXPR
10284 || code == MINUS_EXPR
10285 || code == MULT_EXPR
10286 || (code == TRUNC_DIV_EXPR && first_complex))
10287 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10288 && flag_signed_zeros)
10290 /* An operation on mixed real/complex operands must be
10291 handled specially, but the language-independent code can
10292 more easily optimize the plain complex arithmetic if
10293 -fno-signed-zeros. */
10294 tree real_type = TREE_TYPE (result_type);
10296 if (type0 != orig_type0 || type1 != orig_type1)
10298 gcc_assert (may_need_excess_precision && common);
10299 semantic_result_type = c_common_type (orig_type0, orig_type1);
10303 if (TREE_TYPE (op0) != result_type)
10304 op0 = convert_and_check (result_type, op0);
10305 if (TREE_TYPE (op1) != real_type)
10306 op1 = convert_and_check (real_type, op1);
10310 if (TREE_TYPE (op0) != real_type)
10311 op0 = convert_and_check (real_type, op0);
10312 if (TREE_TYPE (op1) != result_type)
10313 op1 = convert_and_check (result_type, op1);
10315 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10316 return error_mark_node;
10319 op0 = c_save_expr (op0);
10320 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10322 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10327 case TRUNC_DIV_EXPR:
10328 op1 = c_save_expr (op1);
10329 imag = build2 (resultcode, real_type, imag, op1);
10330 /* Fall through. */
10333 real = build2 (resultcode, real_type, real, op1);
10341 op1 = c_save_expr (op1);
10342 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10344 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10349 op0 = c_save_expr (op0);
10350 imag = build2 (resultcode, real_type, op0, imag);
10351 /* Fall through. */
10353 real = build2 (resultcode, real_type, op0, real);
10356 real = build2 (resultcode, real_type, op0, real);
10357 imag = build1 (NEGATE_EXPR, real_type, imag);
10363 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10364 goto return_build_binary_op;
10367 /* For certain operations (which identify themselves by shorten != 0)
10368 if both args were extended from the same smaller type,
10369 do the arithmetic in that type and then extend.
10371 shorten !=0 and !=1 indicates a bitwise operation.
10372 For them, this optimization is safe only if
10373 both args are zero-extended or both are sign-extended.
10374 Otherwise, we might change the result.
10375 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10376 but calculated in (unsigned short) it would be (unsigned short)-1. */
10378 if (shorten && none_complex)
10380 final_type = result_type;
10381 result_type = shorten_binary_op (result_type, op0, op1,
10385 /* Shifts can be shortened if shifting right. */
10390 tree arg0 = get_narrower (op0, &unsigned_arg);
10392 final_type = result_type;
10394 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10395 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10397 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10398 && tree_int_cst_sgn (op1) > 0
10399 /* We can shorten only if the shift count is less than the
10400 number of bits in the smaller type size. */
10401 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10402 /* We cannot drop an unsigned shift after sign-extension. */
10403 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10405 /* Do an unsigned shift if the operand was zero-extended. */
10407 = c_common_signed_or_unsigned_type (unsigned_arg,
10409 /* Convert value-to-be-shifted to that type. */
10410 if (TREE_TYPE (op0) != result_type)
10411 op0 = convert (result_type, op0);
10416 /* Comparison operations are shortened too but differently.
10417 They identify themselves by setting short_compare = 1. */
10421 /* Don't write &op0, etc., because that would prevent op0
10422 from being kept in a register.
10423 Instead, make copies of the our local variables and
10424 pass the copies by reference, then copy them back afterward. */
10425 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10426 enum tree_code xresultcode = resultcode;
10428 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10433 goto return_build_binary_op;
10436 op0 = xop0, op1 = xop1;
10438 resultcode = xresultcode;
10440 if (c_inhibit_evaluation_warnings == 0)
10442 bool op0_maybe_const = true;
10443 bool op1_maybe_const = true;
10444 tree orig_op0_folded, orig_op1_folded;
10446 if (in_late_binary_op)
10448 orig_op0_folded = orig_op0;
10449 orig_op1_folded = orig_op1;
10453 /* Fold for the sake of possible warnings, as in
10454 build_conditional_expr. This requires the
10455 "original" values to be folded, not just op0 and
10457 c_inhibit_evaluation_warnings++;
10458 op0 = c_fully_fold (op0, require_constant_value,
10460 op1 = c_fully_fold (op1, require_constant_value,
10462 c_inhibit_evaluation_warnings--;
10463 orig_op0_folded = c_fully_fold (orig_op0,
10464 require_constant_value,
10466 orig_op1_folded = c_fully_fold (orig_op1,
10467 require_constant_value,
10471 if (warn_sign_compare)
10472 warn_for_sign_compare (location, orig_op0_folded,
10473 orig_op1_folded, op0, op1,
10474 result_type, resultcode);
10475 if (!in_late_binary_op && !int_operands)
10477 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10478 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10479 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10480 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10486 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10487 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10488 Then the expression will be built.
10489 It will be given type FINAL_TYPE if that is nonzero;
10490 otherwise, it will be given type RESULT_TYPE. */
10494 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10495 return error_mark_node;
10498 if (build_type == NULL_TREE)
10500 build_type = result_type;
10501 if ((type0 != orig_type0 || type1 != orig_type1)
10504 gcc_assert (may_need_excess_precision && common);
10505 semantic_result_type = c_common_type (orig_type0, orig_type1);
10511 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10512 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10514 /* This can happen if one operand has a vector type, and the other
10515 has a different type. */
10516 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10517 return error_mark_node;
10520 /* Treat expressions in initializers specially as they can't trap. */
10521 if (int_const_or_overflow)
10522 ret = (require_constant_value
10523 ? fold_build2_initializer_loc (location, resultcode, build_type,
10525 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10527 ret = build2 (resultcode, build_type, op0, op1);
10528 if (final_type != 0)
10529 ret = convert (final_type, ret);
10531 return_build_binary_op:
10532 gcc_assert (ret != error_mark_node);
10533 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10534 ret = (int_operands
10535 ? note_integer_operands (ret)
10536 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10537 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10538 && !in_late_binary_op)
10539 ret = note_integer_operands (ret);
10540 if (semantic_result_type)
10541 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10542 protected_set_expr_location (ret, location);
10547 /* Convert EXPR to be a truth-value, validating its type for this
10548 purpose. LOCATION is the source location for the expression. */
10551 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10553 bool int_const, int_operands;
10555 switch (TREE_CODE (TREE_TYPE (expr)))
10558 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10559 return error_mark_node;
10562 error_at (location, "used struct type value where scalar is required");
10563 return error_mark_node;
10566 error_at (location, "used union type value where scalar is required");
10567 return error_mark_node;
10570 error_at (location, "void value not ignored as it ought to be");
10571 return error_mark_node;
10573 case FUNCTION_TYPE:
10574 gcc_unreachable ();
10577 error_at (location, "used vector type where scalar is required");
10578 return error_mark_node;
10584 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10585 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10587 expr = remove_c_maybe_const_expr (expr);
10589 /* ??? Should we also give an error for vectors rather than leaving
10590 those to give errors later? */
10591 expr = c_common_truthvalue_conversion (location, expr);
10593 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10595 if (TREE_OVERFLOW (expr))
10598 return note_integer_operands (expr);
10600 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10601 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10606 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10610 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10612 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10614 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10615 /* Executing a compound literal inside a function reinitializes
10617 if (!TREE_STATIC (decl))
10625 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10628 c_begin_omp_parallel (void)
10632 keep_next_level ();
10633 block = c_begin_compound_stmt (true);
10638 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10639 statement. LOC is the location of the OMP_PARALLEL. */
10642 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10646 block = c_end_compound_stmt (loc, block, true);
10648 stmt = make_node (OMP_PARALLEL);
10649 TREE_TYPE (stmt) = void_type_node;
10650 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10651 OMP_PARALLEL_BODY (stmt) = block;
10652 SET_EXPR_LOCATION (stmt, loc);
10654 return add_stmt (stmt);
10657 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10660 c_begin_omp_task (void)
10664 keep_next_level ();
10665 block = c_begin_compound_stmt (true);
10670 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10671 statement. LOC is the location of the #pragma. */
10674 c_finish_omp_task (location_t loc, tree clauses, tree block)
10678 block = c_end_compound_stmt (loc, block, true);
10680 stmt = make_node (OMP_TASK);
10681 TREE_TYPE (stmt) = void_type_node;
10682 OMP_TASK_CLAUSES (stmt) = clauses;
10683 OMP_TASK_BODY (stmt) = block;
10684 SET_EXPR_LOCATION (stmt, loc);
10686 return add_stmt (stmt);
10689 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10690 Remove any elements from the list that are invalid. */
10693 c_finish_omp_clauses (tree clauses)
10695 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10696 tree c, t, *pc = &clauses;
10699 bitmap_obstack_initialize (NULL);
10700 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10701 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10702 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10704 for (pc = &clauses, c = clauses; c ; c = *pc)
10706 bool remove = false;
10707 bool need_complete = false;
10708 bool need_implicitly_determined = false;
10710 switch (OMP_CLAUSE_CODE (c))
10712 case OMP_CLAUSE_SHARED:
10714 need_implicitly_determined = true;
10715 goto check_dup_generic;
10717 case OMP_CLAUSE_PRIVATE:
10719 need_complete = true;
10720 need_implicitly_determined = true;
10721 goto check_dup_generic;
10723 case OMP_CLAUSE_REDUCTION:
10724 name = "reduction";
10725 need_implicitly_determined = true;
10726 t = OMP_CLAUSE_DECL (c);
10727 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10728 || POINTER_TYPE_P (TREE_TYPE (t)))
10730 error_at (OMP_CLAUSE_LOCATION (c),
10731 "%qE has invalid type for %<reduction%>", t);
10734 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10736 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10737 const char *r_name = NULL;
10756 case TRUTH_ANDIF_EXPR:
10759 case TRUTH_ORIF_EXPR:
10763 gcc_unreachable ();
10767 error_at (OMP_CLAUSE_LOCATION (c),
10768 "%qE has invalid type for %<reduction(%s)%>",
10773 goto check_dup_generic;
10775 case OMP_CLAUSE_COPYPRIVATE:
10776 name = "copyprivate";
10777 goto check_dup_generic;
10779 case OMP_CLAUSE_COPYIN:
10781 t = OMP_CLAUSE_DECL (c);
10782 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10784 error_at (OMP_CLAUSE_LOCATION (c),
10785 "%qE must be %<threadprivate%> for %<copyin%>", t);
10788 goto check_dup_generic;
10791 t = OMP_CLAUSE_DECL (c);
10792 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10794 error_at (OMP_CLAUSE_LOCATION (c),
10795 "%qE is not a variable in clause %qs", t, name);
10798 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10799 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10800 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10802 error_at (OMP_CLAUSE_LOCATION (c),
10803 "%qE appears more than once in data clauses", t);
10807 bitmap_set_bit (&generic_head, DECL_UID (t));
10810 case OMP_CLAUSE_FIRSTPRIVATE:
10811 name = "firstprivate";
10812 t = OMP_CLAUSE_DECL (c);
10813 need_complete = true;
10814 need_implicitly_determined = true;
10815 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10817 error_at (OMP_CLAUSE_LOCATION (c),
10818 "%qE is not a variable in clause %<firstprivate%>", t);
10821 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10822 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10824 error_at (OMP_CLAUSE_LOCATION (c),
10825 "%qE appears more than once in data clauses", t);
10829 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10832 case OMP_CLAUSE_LASTPRIVATE:
10833 name = "lastprivate";
10834 t = OMP_CLAUSE_DECL (c);
10835 need_complete = true;
10836 need_implicitly_determined = true;
10837 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10839 error_at (OMP_CLAUSE_LOCATION (c),
10840 "%qE is not a variable in clause %<lastprivate%>", t);
10843 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10844 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10846 error_at (OMP_CLAUSE_LOCATION (c),
10847 "%qE appears more than once in data clauses", t);
10851 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10854 case OMP_CLAUSE_IF:
10855 case OMP_CLAUSE_NUM_THREADS:
10856 case OMP_CLAUSE_SCHEDULE:
10857 case OMP_CLAUSE_NOWAIT:
10858 case OMP_CLAUSE_ORDERED:
10859 case OMP_CLAUSE_DEFAULT:
10860 case OMP_CLAUSE_UNTIED:
10861 case OMP_CLAUSE_COLLAPSE:
10862 case OMP_CLAUSE_FINAL:
10863 case OMP_CLAUSE_MERGEABLE:
10864 pc = &OMP_CLAUSE_CHAIN (c);
10868 gcc_unreachable ();
10873 t = OMP_CLAUSE_DECL (c);
10877 t = require_complete_type (t);
10878 if (t == error_mark_node)
10882 if (need_implicitly_determined)
10884 const char *share_name = NULL;
10886 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10887 share_name = "threadprivate";
10888 else switch (c_omp_predetermined_sharing (t))
10890 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10892 case OMP_CLAUSE_DEFAULT_SHARED:
10893 /* const vars may be specified in firstprivate clause. */
10894 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10895 && TREE_READONLY (t))
10897 share_name = "shared";
10899 case OMP_CLAUSE_DEFAULT_PRIVATE:
10900 share_name = "private";
10903 gcc_unreachable ();
10907 error_at (OMP_CLAUSE_LOCATION (c),
10908 "%qE is predetermined %qs for %qs",
10909 t, share_name, name);
10916 *pc = OMP_CLAUSE_CHAIN (c);
10918 pc = &OMP_CLAUSE_CHAIN (c);
10921 bitmap_obstack_release (NULL);
10925 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10926 down to the element type of an array. */
10929 c_build_qualified_type (tree type, int type_quals)
10931 if (type == error_mark_node)
10934 if (TREE_CODE (type) == ARRAY_TYPE)
10937 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10940 /* See if we already have an identically qualified type. */
10941 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10943 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10944 && TYPE_NAME (t) == TYPE_NAME (type)
10945 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10946 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10947 TYPE_ATTRIBUTES (type)))
10952 tree domain = TYPE_DOMAIN (type);
10954 t = build_variant_type_copy (type);
10955 TREE_TYPE (t) = element_type;
10957 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10958 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10959 SET_TYPE_STRUCTURAL_EQUALITY (t);
10960 else if (TYPE_CANONICAL (element_type) != element_type
10961 || (domain && TYPE_CANONICAL (domain) != domain))
10963 tree unqualified_canon
10964 = build_array_type (TYPE_CANONICAL (element_type),
10965 domain? TYPE_CANONICAL (domain)
10968 = c_build_qualified_type (unqualified_canon, type_quals);
10971 TYPE_CANONICAL (t) = t;
10976 /* A restrict-qualified pointer type must be a pointer to object or
10977 incomplete type. Note that the use of POINTER_TYPE_P also allows
10978 REFERENCE_TYPEs, which is appropriate for C++. */
10979 if ((type_quals & TYPE_QUAL_RESTRICT)
10980 && (!POINTER_TYPE_P (type)
10981 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10983 error ("invalid use of %<restrict%>");
10984 type_quals &= ~TYPE_QUAL_RESTRICT;
10987 return build_qualified_type (type, type_quals);
10990 /* Build a VA_ARG_EXPR for the C parser. */
10993 c_build_va_arg (location_t loc, tree expr, tree type)
10995 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10996 warning_at (loc, OPT_Wc___compat,
10997 "C++ requires promoted type, not enum type, in %<va_arg%>");
10998 return build_va_arg (loc, expr, type);