1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
28 #include "coretypes.h"
35 #include "double-int.h"
39 #include "fixed-value.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
43 #include "trans-mem.h"
46 #include "langhooks.h"
52 #include "tree-iterator.h"
59 #include "hard-reg-set.h"
62 #include "gimple-expr.h"
64 #include "tree-inline.h"
66 #include "c-family/c-objc.h"
67 #include "c-family/c-common.h"
68 #include "c-family/c-ubsan.h"
71 #include "gomp-constants.h"
73 /* Possible cases of implicit bad conversions. Used to select
74 diagnostic messages in convert_for_assignment. */
82 /* The level of nesting inside "__alignof__". */
85 /* The level of nesting inside "sizeof". */
88 /* The level of nesting inside "typeof". */
91 /* The argument of last parsed sizeof expression, only to be tested
92 if expr.original_code == SIZEOF_EXPR. */
93 tree c_last_sizeof_arg;
95 /* Nonzero if we might need to print a "missing braces around
96 initializer" message within this initializer. */
97 static int found_missing_braces;
99 static int require_constant_value;
100 static int require_constant_elements;
102 static bool null_pointer_constant_p (const_tree);
103 static tree qualify_type (tree, tree);
104 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
106 static int comp_target_types (location_t, tree, tree);
107 static int function_types_compatible_p (const_tree, const_tree, bool *,
109 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
110 static tree lookup_field (tree, tree);
111 static int convert_arguments (location_t, vec<location_t>, tree,
112 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
114 static tree pointer_diff (location_t, tree, tree);
115 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
116 enum impl_conv, bool, tree, tree, int);
117 static tree valid_compound_expr_initializer (tree, tree);
118 static void push_string (const char *);
119 static void push_member_name (tree);
120 static int spelling_length (void);
121 static char *print_spelling (char *);
122 static void warning_init (location_t, int, const char *);
123 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
124 static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
125 bool, struct obstack *);
126 static void output_pending_init_elements (int, struct obstack *);
127 static int set_designator (location_t, int, struct obstack *);
128 static void push_range_stack (tree, struct obstack *);
129 static void add_pending_init (location_t, tree, tree, tree, bool,
131 static void set_nonincremental_init (struct obstack *);
132 static void set_nonincremental_init_from_string (tree, struct obstack *);
133 static tree find_init_member (tree, struct obstack *);
134 static void readonly_warning (tree, enum lvalue_use);
135 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
136 static void record_maybe_used_decl (tree);
137 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
139 /* Return true if EXP is a null pointer constant, false otherwise. */
142 null_pointer_constant_p (const_tree expr)
144 /* This should really operate on c_expr structures, but they aren't
145 yet available everywhere required. */
146 tree type = TREE_TYPE (expr);
147 return (TREE_CODE (expr) == INTEGER_CST
148 && !TREE_OVERFLOW (expr)
149 && integer_zerop (expr)
150 && (INTEGRAL_TYPE_P (type)
151 || (TREE_CODE (type) == POINTER_TYPE
152 && VOID_TYPE_P (TREE_TYPE (type))
153 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
156 /* EXPR may appear in an unevaluated part of an integer constant
157 expression, but not in an evaluated part. Wrap it in a
158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
162 note_integer_operands (tree expr)
165 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
167 ret = copy_node (expr);
168 TREE_OVERFLOW (ret) = 1;
172 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
178 /* Having checked whether EXPR may appear in an unevaluated part of an
179 integer constant expression and found that it may, remove any
180 C_MAYBE_CONST_EXPR noting this fact and return the resulting
184 remove_c_maybe_const_expr (tree expr)
186 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
187 return C_MAYBE_CONST_EXPR_EXPR (expr);
192 \f/* This is a cache to hold if two types are compatible or not. */
194 struct tagged_tu_seen_cache {
195 const struct tagged_tu_seen_cache * next;
198 /* The return value of tagged_types_tu_compatible_p if we had seen
199 these two types already. */
203 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
204 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
206 /* Do `exp = require_complete_type (exp);' to make sure exp
207 does not have an incomplete type. (That includes void types.) */
210 require_complete_type (tree value)
212 tree type = TREE_TYPE (value);
214 if (error_operand_p (value))
215 return error_mark_node;
217 /* First, detect a valid value with a complete type. */
218 if (COMPLETE_TYPE_P (type))
221 c_incomplete_type_error (value, type);
222 return error_mark_node;
225 /* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. */
230 c_incomplete_type_error (const_tree value, const_tree type)
232 /* Avoid duplicate error message. */
233 if (TREE_CODE (type) == ERROR_MARK)
236 if (value != 0 && (TREE_CODE (value) == VAR_DECL
237 || TREE_CODE (value) == PARM_DECL))
238 error ("%qD has an incomplete type %qT", value, type);
242 /* We must print an error message. Be clever about what it says. */
244 switch (TREE_CODE (type))
252 error ("invalid use of void expression");
256 if (TYPE_DOMAIN (type))
258 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
260 error ("invalid use of flexible array member");
263 type = TREE_TYPE (type);
266 error ("invalid use of array with unspecified bounds");
273 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
274 error ("invalid use of undefined type %qT", type);
276 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
277 error ("invalid use of incomplete typedef %qT", type);
281 /* Given a type, apply default promotions wrt unnamed function
282 arguments and return the new type. */
285 c_type_promotes_to (tree type)
287 tree ret = NULL_TREE;
289 if (TYPE_MAIN_VARIANT (type) == float_type_node)
290 ret = double_type_node;
291 else if (c_promoting_integer_type_p (type))
293 /* Preserve unsignedness if not really getting any wider. */
294 if (TYPE_UNSIGNED (type)
295 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
296 ret = unsigned_type_node;
298 ret = integer_type_node;
301 if (ret != NULL_TREE)
302 return (TYPE_ATOMIC (type)
303 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
309 /* Return true if between two named address spaces, whether there is a superset
310 named address space that encompasses both address spaces. If there is a
311 superset, return which address space is the superset. */
314 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
321 else if (targetm.addr_space.subset_p (as1, as2))
326 else if (targetm.addr_space.subset_p (as2, as1))
335 /* Return a variant of TYPE which has all the type qualifiers of LIKE
336 as well as those of TYPE. */
339 qualify_type (tree type, tree like)
341 addr_space_t as_type = TYPE_ADDR_SPACE (type);
342 addr_space_t as_like = TYPE_ADDR_SPACE (like);
343 addr_space_t as_common;
345 /* If the two named address spaces are different, determine the common
346 superset address space. If there isn't one, raise an error. */
347 if (!addr_space_superset (as_type, as_like, &as_common))
350 error ("%qT and %qT are in disjoint named address spaces",
354 return c_build_qualified_type (type,
355 TYPE_QUALS_NO_ADDR_SPACE (type)
356 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
357 | ENCODE_QUAL_ADDR_SPACE (as_common));
360 /* Return true iff the given tree T is a variable length array. */
363 c_vla_type_p (const_tree t)
365 if (TREE_CODE (t) == ARRAY_TYPE
366 && C_TYPE_VARIABLE_SIZE (t))
371 /* Return the composite type of two compatible types.
373 We assume that comptypes has already been done and returned
374 nonzero; if that isn't so, this may crash. In particular, we
375 assume that qualifiers match. */
378 composite_type (tree t1, tree t2)
380 enum tree_code code1;
381 enum tree_code code2;
384 /* Save time if the two types are the same. */
386 if (t1 == t2) return t1;
388 /* If one type is nonsense, use the other. */
389 if (t1 == error_mark_node)
391 if (t2 == error_mark_node)
394 code1 = TREE_CODE (t1);
395 code2 = TREE_CODE (t2);
397 /* Merge the attributes. */
398 attributes = targetm.merge_type_attributes (t1, t2);
400 /* If one is an enumerated type and the other is the compatible
401 integer type, the composite type might be either of the two
402 (DR#013 question 3). For consistency, use the enumerated type as
403 the composite type. */
405 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
407 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
410 gcc_assert (code1 == code2);
415 /* For two pointers, do this recursively on the target type. */
417 tree pointed_to_1 = TREE_TYPE (t1);
418 tree pointed_to_2 = TREE_TYPE (t2);
419 tree target = composite_type (pointed_to_1, pointed_to_2);
420 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
421 t1 = build_type_attribute_variant (t1, attributes);
422 return qualify_type (t1, t2);
427 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
430 tree d1 = TYPE_DOMAIN (t1);
431 tree d2 = TYPE_DOMAIN (t2);
432 bool d1_variable, d2_variable;
433 bool d1_zero, d2_zero;
434 bool t1_complete, t2_complete;
436 /* We should not have any type quals on arrays at all. */
437 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
438 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
440 t1_complete = COMPLETE_TYPE_P (t1);
441 t2_complete = COMPLETE_TYPE_P (t2);
443 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
444 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
446 d1_variable = (!d1_zero
447 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
448 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
449 d2_variable = (!d2_zero
450 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
451 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
452 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
453 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
455 /* Save space: see if the result is identical to one of the args. */
456 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
457 && (d2_variable || d2_zero || !d1_variable))
458 return build_type_attribute_variant (t1, attributes);
459 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
460 && (d1_variable || d1_zero || !d2_variable))
461 return build_type_attribute_variant (t2, attributes);
463 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
464 return build_type_attribute_variant (t1, attributes);
465 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
466 return build_type_attribute_variant (t2, attributes);
468 /* Merge the element types, and have a size if either arg has
469 one. We may have qualifiers on the element types. To set
470 up TYPE_MAIN_VARIANT correctly, we need to form the
471 composite of the unqualified types and add the qualifiers
473 quals = TYPE_QUALS (strip_array_types (elt));
474 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
475 t1 = build_array_type (unqual_elt,
476 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
482 /* Ensure a composite type involving a zero-length array type
483 is a zero-length type not an incomplete type. */
484 if (d1_zero && d2_zero
485 && (t1_complete || t2_complete)
486 && !COMPLETE_TYPE_P (t1))
488 TYPE_SIZE (t1) = bitsize_zero_node;
489 TYPE_SIZE_UNIT (t1) = size_zero_node;
491 t1 = c_build_qualified_type (t1, quals);
492 return build_type_attribute_variant (t1, attributes);
498 if (attributes != NULL)
500 /* Try harder not to create a new aggregate type. */
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
503 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
506 return build_type_attribute_variant (t1, attributes);
509 /* Function types: prefer the one that specified arg types.
510 If both do, merge the arg types. Also merge the return types. */
512 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
513 tree p1 = TYPE_ARG_TYPES (t1);
514 tree p2 = TYPE_ARG_TYPES (t2);
519 /* Save space: see if the result is identical to one of the args. */
520 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
521 return build_type_attribute_variant (t1, attributes);
522 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
523 return build_type_attribute_variant (t2, attributes);
525 /* Simple way if one arg fails to specify argument types. */
526 if (TYPE_ARG_TYPES (t1) == 0)
528 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
529 t1 = build_type_attribute_variant (t1, attributes);
530 return qualify_type (t1, t2);
532 if (TYPE_ARG_TYPES (t2) == 0)
534 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
535 t1 = build_type_attribute_variant (t1, attributes);
536 return qualify_type (t1, t2);
539 /* If both args specify argument types, we must merge the two
540 lists, argument by argument. */
542 len = list_length (p1);
545 for (i = 0; i < len; i++)
546 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
551 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
553 /* A null type means arg type is not specified.
554 Take whatever the other function type has. */
555 if (TREE_VALUE (p1) == 0)
557 TREE_VALUE (n) = TREE_VALUE (p2);
560 if (TREE_VALUE (p2) == 0)
562 TREE_VALUE (n) = TREE_VALUE (p1);
566 /* Given wait (union {union wait *u; int *i} *)
567 and wait (union wait *),
568 prefer union wait * as type of parm. */
569 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
570 && TREE_VALUE (p1) != TREE_VALUE (p2))
573 tree mv2 = TREE_VALUE (p2);
574 if (mv2 && mv2 != error_mark_node
575 && TREE_CODE (mv2) != ARRAY_TYPE)
576 mv2 = TYPE_MAIN_VARIANT (mv2);
577 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
578 memb; memb = DECL_CHAIN (memb))
580 tree mv3 = TREE_TYPE (memb);
581 if (mv3 && mv3 != error_mark_node
582 && TREE_CODE (mv3) != ARRAY_TYPE)
583 mv3 = TYPE_MAIN_VARIANT (mv3);
584 if (comptypes (mv3, mv2))
586 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
588 pedwarn (input_location, OPT_Wpedantic,
589 "function types not truly compatible in ISO C");
594 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
595 && TREE_VALUE (p2) != TREE_VALUE (p1))
598 tree mv1 = TREE_VALUE (p1);
599 if (mv1 && mv1 != error_mark_node
600 && TREE_CODE (mv1) != ARRAY_TYPE)
601 mv1 = TYPE_MAIN_VARIANT (mv1);
602 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
603 memb; memb = DECL_CHAIN (memb))
605 tree mv3 = TREE_TYPE (memb);
606 if (mv3 && mv3 != error_mark_node
607 && TREE_CODE (mv3) != ARRAY_TYPE)
608 mv3 = TYPE_MAIN_VARIANT (mv3);
609 if (comptypes (mv3, mv1))
611 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
613 pedwarn (input_location, OPT_Wpedantic,
614 "function types not truly compatible in ISO C");
619 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
623 t1 = build_function_type (valtype, newargs);
624 t1 = qualify_type (t1, t2);
625 /* ... falls through ... */
629 return build_type_attribute_variant (t1, attributes);
634 /* Return the type of a conditional expression between pointers to
635 possibly differently qualified versions of compatible types.
637 We assume that comp_target_types has already been done and returned
638 nonzero; if that isn't so, this may crash. */
641 common_pointer_type (tree t1, tree t2)
644 tree pointed_to_1, mv1;
645 tree pointed_to_2, mv2;
647 unsigned target_quals;
648 addr_space_t as1, as2, as_common;
651 /* Save time if the two types are the same. */
653 if (t1 == t2) return t1;
655 /* If one type is nonsense, use the other. */
656 if (t1 == error_mark_node)
658 if (t2 == error_mark_node)
661 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
662 && TREE_CODE (t2) == POINTER_TYPE);
664 /* Merge the attributes. */
665 attributes = targetm.merge_type_attributes (t1, t2);
667 /* Find the composite type of the target types, and combine the
668 qualifiers of the two types' targets. Do not lose qualifiers on
669 array element types by taking the TYPE_MAIN_VARIANT. */
670 mv1 = pointed_to_1 = TREE_TYPE (t1);
671 mv2 = pointed_to_2 = TREE_TYPE (t2);
672 if (TREE_CODE (mv1) != ARRAY_TYPE)
673 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
674 if (TREE_CODE (mv2) != ARRAY_TYPE)
675 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
676 target = composite_type (mv1, mv2);
678 /* Strip array types to get correct qualifier for pointers to arrays */
679 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
680 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
682 /* For function types do not merge const qualifiers, but drop them
683 if used inconsistently. The middle-end uses these to mark const
684 and noreturn functions. */
685 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
686 target_quals = (quals1 & quals2);
688 target_quals = (quals1 | quals2);
690 /* If the two named address spaces are different, determine the common
691 superset address space. This is guaranteed to exist due to the
692 assumption that comp_target_type returned non-zero. */
693 as1 = TYPE_ADDR_SPACE (pointed_to_1);
694 as2 = TYPE_ADDR_SPACE (pointed_to_2);
695 if (!addr_space_superset (as1, as2, &as_common))
698 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
700 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
701 return build_type_attribute_variant (t1, attributes);
704 /* Return the common type for two arithmetic types under the usual
705 arithmetic conversions. The default conversions have already been
706 applied, and enumerated types converted to their compatible integer
707 types. The resulting type is unqualified and has no attributes.
709 This is the type for the result of most arithmetic operations
710 if the operands have the given two types. */
713 c_common_type (tree t1, tree t2)
715 enum tree_code code1;
716 enum tree_code code2;
718 /* If one type is nonsense, use the other. */
719 if (t1 == error_mark_node)
721 if (t2 == error_mark_node)
724 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
725 t1 = TYPE_MAIN_VARIANT (t1);
727 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
728 t2 = TYPE_MAIN_VARIANT (t2);
730 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
731 t1 = build_type_attribute_variant (t1, NULL_TREE);
733 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
734 t2 = build_type_attribute_variant (t2, NULL_TREE);
736 /* Save time if the two types are the same. */
738 if (t1 == t2) return t1;
740 code1 = TREE_CODE (t1);
741 code2 = TREE_CODE (t2);
743 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
744 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
745 || code1 == INTEGER_TYPE);
746 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
747 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
748 || code2 == INTEGER_TYPE);
750 /* When one operand is a decimal float type, the other operand cannot be
751 a generic float type or a complex type. We also disallow vector types
753 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
754 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
756 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
758 error ("can%'t mix operands of decimal float and vector types");
759 return error_mark_node;
761 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
763 error ("can%'t mix operands of decimal float and complex types");
764 return error_mark_node;
766 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
768 error ("can%'t mix operands of decimal float and other float types");
769 return error_mark_node;
773 /* If one type is a vector type, return that type. (How the usual
774 arithmetic conversions apply to the vector types extension is not
775 precisely specified.) */
776 if (code1 == VECTOR_TYPE)
779 if (code2 == VECTOR_TYPE)
782 /* If one type is complex, form the common type of the non-complex
783 components, then make that complex. Use T1 or T2 if it is the
785 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
787 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
788 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
789 tree subtype = c_common_type (subtype1, subtype2);
791 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
793 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
796 return build_complex_type (subtype);
799 /* If only one is real, use it as the result. */
801 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
804 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
807 /* If both are real and either are decimal floating point types, use
808 the decimal floating point type with the greater precision. */
810 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
812 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
813 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
814 return dfloat128_type_node;
815 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
816 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
817 return dfloat64_type_node;
818 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
819 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
820 return dfloat32_type_node;
823 /* Deal with fixed-point types. */
824 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
826 unsigned int unsignedp = 0, satp = 0;
828 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
833 /* If one input type is saturating, the result type is saturating. */
834 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
837 /* If both fixed-point types are unsigned, the result type is unsigned.
838 When mixing fixed-point and integer types, follow the sign of the
840 Otherwise, the result type is signed. */
841 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
842 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
843 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
844 && TYPE_UNSIGNED (t1))
845 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
846 && TYPE_UNSIGNED (t2)))
849 /* The result type is signed. */
852 /* If the input type is unsigned, we need to convert to the
854 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
856 enum mode_class mclass = (enum mode_class) 0;
857 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
859 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
863 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
865 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
867 enum mode_class mclass = (enum mode_class) 0;
868 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
870 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
874 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
878 if (code1 == FIXED_POINT_TYPE)
880 fbit1 = GET_MODE_FBIT (m1);
881 ibit1 = GET_MODE_IBIT (m1);
886 /* Signed integers need to subtract one sign bit. */
887 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
890 if (code2 == FIXED_POINT_TYPE)
892 fbit2 = GET_MODE_FBIT (m2);
893 ibit2 = GET_MODE_IBIT (m2);
898 /* Signed integers need to subtract one sign bit. */
899 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
902 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
903 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
904 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
908 /* Both real or both integers; use the one with greater precision. */
910 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
912 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
915 /* Same precision. Prefer long longs to longs to ints when the
916 same precision, following the C99 rules on integer type rank
917 (which are equivalent to the C90 rules for C90 types). */
919 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
920 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
921 return long_long_unsigned_type_node;
923 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
924 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
926 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
927 return long_long_unsigned_type_node;
929 return long_long_integer_type_node;
932 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
933 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
934 return long_unsigned_type_node;
936 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
937 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
939 /* But preserve unsignedness from the other type,
940 since long cannot hold all the values of an unsigned int. */
941 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
942 return long_unsigned_type_node;
944 return long_integer_type_node;
947 /* Likewise, prefer long double to double even if same size. */
948 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
949 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
950 return long_double_type_node;
952 /* Likewise, prefer double to float even if same size.
953 We got a couple of embedded targets with 32 bit doubles, and the
954 pdp11 might have 64 bit floats. */
955 if (TYPE_MAIN_VARIANT (t1) == double_type_node
956 || TYPE_MAIN_VARIANT (t2) == double_type_node)
957 return double_type_node;
959 /* Otherwise prefer the unsigned one. */
961 if (TYPE_UNSIGNED (t1))
967 /* Wrapper around c_common_type that is used by c-common.c and other
968 front end optimizations that remove promotions. ENUMERAL_TYPEs
969 are allowed here and are converted to their compatible integer types.
970 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
971 preferably a non-Boolean type as the common type. */
973 common_type (tree t1, tree t2)
975 if (TREE_CODE (t1) == ENUMERAL_TYPE)
976 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
977 if (TREE_CODE (t2) == ENUMERAL_TYPE)
978 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
980 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
981 if (TREE_CODE (t1) == BOOLEAN_TYPE
982 && TREE_CODE (t2) == BOOLEAN_TYPE)
983 return boolean_type_node;
985 /* If either type is BOOLEAN_TYPE, then return the other. */
986 if (TREE_CODE (t1) == BOOLEAN_TYPE)
988 if (TREE_CODE (t2) == BOOLEAN_TYPE)
991 return c_common_type (t1, t2);
994 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
995 or various other operations. Return 2 if they are compatible
996 but a warning may be needed if you use them together. */
999 comptypes (tree type1, tree type2)
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1004 val = comptypes_internal (type1, type2, NULL, NULL);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1010 /* Like comptypes, but if it returns non-zero because enum and int are
1011 compatible, it sets *ENUM_AND_INT_P to true. */
1014 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1016 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1019 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1020 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1025 /* Like comptypes, but if it returns nonzero for different types, it
1026 sets *DIFFERENT_TYPES_P to true. */
1029 comptypes_check_different_types (tree type1, tree type2,
1030 bool *different_types_p)
1032 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1035 val = comptypes_internal (type1, type2, NULL, different_types_p);
1036 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1041 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1042 or various other operations. Return 2 if they are compatible
1043 but a warning may be needed if you use them together. If
1044 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1045 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1046 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1047 NULL, and the types are compatible but different enough not to be
1048 permitted in C11 typedef redeclarations, then this sets
1049 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1050 false, but may or may not be set if the types are incompatible.
1051 This differs from comptypes, in that we don't free the seen
1055 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1056 bool *different_types_p)
1058 const_tree t1 = type1;
1059 const_tree t2 = type2;
1062 /* Suppress errors caused by previously reported errors. */
1064 if (t1 == t2 || !t1 || !t2
1065 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1068 /* Enumerated types are compatible with integer types, but this is
1069 not transitive: two enumerated types in the same translation unit
1070 are compatible with each other only if they are the same type. */
1072 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1074 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1075 if (TREE_CODE (t2) != VOID_TYPE)
1077 if (enum_and_int_p != NULL)
1078 *enum_and_int_p = true;
1079 if (different_types_p != NULL)
1080 *different_types_p = true;
1083 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1085 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1086 if (TREE_CODE (t1) != VOID_TYPE)
1088 if (enum_and_int_p != NULL)
1089 *enum_and_int_p = true;
1090 if (different_types_p != NULL)
1091 *different_types_p = true;
1098 /* Different classes of types can't be compatible. */
1100 if (TREE_CODE (t1) != TREE_CODE (t2))
1103 /* Qualifiers must match. C99 6.7.3p9 */
1105 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1108 /* Allow for two different type nodes which have essentially the same
1109 definition. Note that we already checked for equality of the type
1110 qualifiers (just above). */
1112 if (TREE_CODE (t1) != ARRAY_TYPE
1113 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1116 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1117 if (!(attrval = comp_type_attributes (t1, t2)))
1120 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1123 switch (TREE_CODE (t1))
1126 /* Do not remove mode or aliasing information. */
1127 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1128 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1130 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1131 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1132 enum_and_int_p, different_types_p));
1136 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1142 tree d1 = TYPE_DOMAIN (t1);
1143 tree d2 = TYPE_DOMAIN (t2);
1144 bool d1_variable, d2_variable;
1145 bool d1_zero, d2_zero;
1148 /* Target types must match incl. qualifiers. */
1149 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1150 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1152 different_types_p)))
1155 if (different_types_p != NULL
1156 && (d1 == 0) != (d2 == 0))
1157 *different_types_p = true;
1158 /* Sizes must match unless one is missing or variable. */
1159 if (d1 == 0 || d2 == 0 || d1 == d2)
1162 d1_zero = !TYPE_MAX_VALUE (d1);
1163 d2_zero = !TYPE_MAX_VALUE (d2);
1165 d1_variable = (!d1_zero
1166 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1167 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1168 d2_variable = (!d2_zero
1169 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1170 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1171 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1172 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1174 if (different_types_p != NULL
1175 && d1_variable != d2_variable)
1176 *different_types_p = true;
1177 if (d1_variable || d2_variable)
1179 if (d1_zero && d2_zero)
1181 if (d1_zero || d2_zero
1182 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1183 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1192 if (val != 1 && !same_translation_unit_p (t1, t2))
1194 tree a1 = TYPE_ATTRIBUTES (t1);
1195 tree a2 = TYPE_ATTRIBUTES (t2);
1197 if (! attribute_list_contained (a1, a2)
1198 && ! attribute_list_contained (a2, a1))
1202 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1204 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1210 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1211 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1212 enum_and_int_p, different_types_p));
1218 return attrval == 2 && val == 1 ? 2 : val;
1221 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1222 their qualifiers, except for named address spaces. If the pointers point to
1223 different named addresses, then we must determine if one address space is a
1224 subset of the other. */
1227 comp_target_types (location_t location, tree ttl, tree ttr)
1231 tree mvl = TREE_TYPE (ttl);
1232 tree mvr = TREE_TYPE (ttr);
1233 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1234 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1235 addr_space_t as_common;
1236 bool enum_and_int_p;
1238 /* Fail if pointers point to incompatible address spaces. */
1239 if (!addr_space_superset (asl, asr, &as_common))
1242 /* For pedantic record result of comptypes on arrays before losing
1243 qualifiers on the element type below. */
1246 if (TREE_CODE (mvl) == ARRAY_TYPE
1247 && TREE_CODE (mvr) == ARRAY_TYPE)
1248 val_ped = comptypes (mvl, mvr);
1250 /* Qualifiers on element types of array types that are
1251 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1253 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1254 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1255 : TYPE_MAIN_VARIANT (mvl));
1257 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1258 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1259 : TYPE_MAIN_VARIANT (mvr));
1261 enum_and_int_p = false;
1262 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1264 if (val == 1 && val_ped != 1)
1265 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1266 "are incompatible in ISO C");
1269 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1271 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1272 warning_at (location, OPT_Wc___compat,
1273 "pointer target types incompatible in C++");
1278 /* Subroutines of `comptypes'. */
1280 /* Determine whether two trees derive from the same translation unit.
1281 If the CONTEXT chain ends in a null, that tree's context is still
1282 being parsed, so if two trees have context chains ending in null,
1283 they're in the same translation unit. */
1285 same_translation_unit_p (const_tree t1, const_tree t2)
1287 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1288 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1290 case tcc_declaration:
1291 t1 = DECL_CONTEXT (t1); break;
1293 t1 = TYPE_CONTEXT (t1); break;
1294 case tcc_exceptional:
1295 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1296 default: gcc_unreachable ();
1299 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1300 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1302 case tcc_declaration:
1303 t2 = DECL_CONTEXT (t2); break;
1305 t2 = TYPE_CONTEXT (t2); break;
1306 case tcc_exceptional:
1307 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1308 default: gcc_unreachable ();
1314 /* Allocate the seen two types, assuming that they are compatible. */
1316 static struct tagged_tu_seen_cache *
1317 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1319 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1320 tu->next = tagged_tu_seen_base;
1324 tagged_tu_seen_base = tu;
1326 /* The C standard says that two structures in different translation
1327 units are compatible with each other only if the types of their
1328 fields are compatible (among other things). We assume that they
1329 are compatible until proven otherwise when building the cache.
1330 An example where this can occur is:
1335 If we are comparing this against a similar struct in another TU,
1336 and did not assume they were compatible, we end up with an infinite
1342 /* Free the seen types until we get to TU_TIL. */
1345 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1347 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1348 while (tu != tu_til)
1350 const struct tagged_tu_seen_cache *const tu1
1351 = (const struct tagged_tu_seen_cache *) tu;
1353 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1355 tagged_tu_seen_base = tu_til;
1358 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1359 compatible. If the two types are not the same (which has been
1360 checked earlier), this can only happen when multiple translation
1361 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1362 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1363 comptypes_internal. */
1366 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1367 bool *enum_and_int_p, bool *different_types_p)
1370 bool needs_warning = false;
1372 /* We have to verify that the tags of the types are the same. This
1373 is harder than it looks because this may be a typedef, so we have
1374 to go look at the original type. It may even be a typedef of a
1376 In the case of compiler-created builtin structs the TYPE_DECL
1377 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1378 while (TYPE_NAME (t1)
1379 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1380 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1381 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1383 while (TYPE_NAME (t2)
1384 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1385 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1386 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1388 /* C90 didn't have the requirement that the two tags be the same. */
1389 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1392 /* C90 didn't say what happened if one or both of the types were
1393 incomplete; we choose to follow C99 rules here, which is that they
1395 if (TYPE_SIZE (t1) == NULL
1396 || TYPE_SIZE (t2) == NULL)
1400 const struct tagged_tu_seen_cache * tts_i;
1401 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1402 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1406 switch (TREE_CODE (t1))
1410 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1411 /* Speed up the case where the type values are in the same order. */
1412 tree tv1 = TYPE_VALUES (t1);
1413 tree tv2 = TYPE_VALUES (t2);
1420 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1422 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1424 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1431 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1435 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1441 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1447 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1449 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1451 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1462 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1463 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1469 /* Speed up the common case where the fields are in the same order. */
1470 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1471 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1475 if (DECL_NAME (s1) != DECL_NAME (s2))
1477 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1478 enum_and_int_p, different_types_p);
1480 if (result != 1 && !DECL_NAME (s1))
1488 needs_warning = true;
1490 if (TREE_CODE (s1) == FIELD_DECL
1491 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1492 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1500 tu->val = needs_warning ? 2 : 1;
1504 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1508 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1509 if (DECL_NAME (s1) == DECL_NAME (s2))
1513 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1517 if (result != 1 && !DECL_NAME (s1))
1525 needs_warning = true;
1527 if (TREE_CODE (s1) == FIELD_DECL
1528 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1529 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1541 tu->val = needs_warning ? 2 : 10;
1547 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1549 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1551 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1554 if (TREE_CODE (s1) != TREE_CODE (s2)
1555 || DECL_NAME (s1) != DECL_NAME (s2))
1557 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1558 enum_and_int_p, different_types_p);
1562 needs_warning = true;
1564 if (TREE_CODE (s1) == FIELD_DECL
1565 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1566 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1572 tu->val = needs_warning ? 2 : 1;
1581 /* Return 1 if two function types F1 and F2 are compatible.
1582 If either type specifies no argument types,
1583 the other must specify a fixed number of self-promoting arg types.
1584 Otherwise, if one type specifies only the number of arguments,
1585 the other must specify that number of self-promoting arg types.
1586 Otherwise, the argument types must match.
1587 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1590 function_types_compatible_p (const_tree f1, const_tree f2,
1591 bool *enum_and_int_p, bool *different_types_p)
1594 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1599 ret1 = TREE_TYPE (f1);
1600 ret2 = TREE_TYPE (f2);
1602 /* 'volatile' qualifiers on a function's return type used to mean
1603 the function is noreturn. */
1604 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1605 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1606 if (TYPE_VOLATILE (ret1))
1607 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1608 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1609 if (TYPE_VOLATILE (ret2))
1610 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1611 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1612 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1616 args1 = TYPE_ARG_TYPES (f1);
1617 args2 = TYPE_ARG_TYPES (f2);
1619 if (different_types_p != NULL
1620 && (args1 == 0) != (args2 == 0))
1621 *different_types_p = true;
1623 /* An unspecified parmlist matches any specified parmlist
1624 whose argument types don't need default promotions. */
1628 if (!self_promoting_args_p (args2))
1630 /* If one of these types comes from a non-prototype fn definition,
1631 compare that with the other type's arglist.
1632 If they don't match, ask for a warning (but no error). */
1633 if (TYPE_ACTUAL_ARG_TYPES (f1)
1634 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1635 enum_and_int_p, different_types_p))
1641 if (!self_promoting_args_p (args1))
1643 if (TYPE_ACTUAL_ARG_TYPES (f2)
1644 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1645 enum_and_int_p, different_types_p))
1650 /* Both types have argument lists: compare them and propagate results. */
1651 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1653 return val1 != 1 ? val1 : val;
1656 /* Check two lists of types for compatibility, returning 0 for
1657 incompatible, 1 for compatible, or 2 for compatible with
1658 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1659 comptypes_internal. */
1662 type_lists_compatible_p (const_tree args1, const_tree args2,
1663 bool *enum_and_int_p, bool *different_types_p)
1665 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1671 tree a1, mv1, a2, mv2;
1672 if (args1 == 0 && args2 == 0)
1674 /* If one list is shorter than the other,
1675 they fail to match. */
1676 if (args1 == 0 || args2 == 0)
1678 mv1 = a1 = TREE_VALUE (args1);
1679 mv2 = a2 = TREE_VALUE (args2);
1680 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1681 mv1 = (TYPE_ATOMIC (mv1)
1682 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1684 : TYPE_MAIN_VARIANT (mv1));
1685 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1686 mv2 = (TYPE_ATOMIC (mv2)
1687 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1689 : TYPE_MAIN_VARIANT (mv2));
1690 /* A null pointer instead of a type
1691 means there is supposed to be an argument
1692 but nothing is specified about what type it has.
1693 So match anything that self-promotes. */
1694 if (different_types_p != NULL
1695 && (a1 == 0) != (a2 == 0))
1696 *different_types_p = true;
1699 if (c_type_promotes_to (a2) != a2)
1704 if (c_type_promotes_to (a1) != a1)
1707 /* If one of the lists has an error marker, ignore this arg. */
1708 else if (TREE_CODE (a1) == ERROR_MARK
1709 || TREE_CODE (a2) == ERROR_MARK)
1711 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1712 different_types_p)))
1714 if (different_types_p != NULL)
1715 *different_types_p = true;
1716 /* Allow wait (union {union wait *u; int *i} *)
1717 and wait (union wait *) to be compatible. */
1718 if (TREE_CODE (a1) == UNION_TYPE
1719 && (TYPE_NAME (a1) == 0
1720 || TYPE_TRANSPARENT_AGGR (a1))
1721 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1722 && tree_int_cst_equal (TYPE_SIZE (a1),
1726 for (memb = TYPE_FIELDS (a1);
1727 memb; memb = DECL_CHAIN (memb))
1729 tree mv3 = TREE_TYPE (memb);
1730 if (mv3 && mv3 != error_mark_node
1731 && TREE_CODE (mv3) != ARRAY_TYPE)
1732 mv3 = (TYPE_ATOMIC (mv3)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1735 : TYPE_MAIN_VARIANT (mv3));
1736 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1743 else if (TREE_CODE (a2) == UNION_TYPE
1744 && (TYPE_NAME (a2) == 0
1745 || TYPE_TRANSPARENT_AGGR (a2))
1746 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a2),
1751 for (memb = TYPE_FIELDS (a2);
1752 memb; memb = DECL_CHAIN (memb))
1754 tree mv3 = TREE_TYPE (memb);
1755 if (mv3 && mv3 != error_mark_node
1756 && TREE_CODE (mv3) != ARRAY_TYPE)
1757 mv3 = (TYPE_ATOMIC (mv3)
1758 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1760 : TYPE_MAIN_VARIANT (mv3));
1761 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1772 /* comptypes said ok, but record if it said to warn. */
1776 args1 = TREE_CHAIN (args1);
1777 args2 = TREE_CHAIN (args2);
1781 /* Compute the size to increment a pointer by. When a function type or void
1782 type or incomplete type is passed, size_one_node is returned.
1783 This function does not emit any diagnostics; the caller is responsible
1787 c_size_in_bytes (const_tree type)
1789 enum tree_code code = TREE_CODE (type);
1791 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1792 || !COMPLETE_TYPE_P (type))
1793 return size_one_node;
1795 /* Convert in case a char is more than one unit. */
1796 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1797 size_int (TYPE_PRECISION (char_type_node)
1801 /* Return either DECL or its known constant value (if it has one). */
1804 decl_constant_value (tree decl)
1806 if (/* Don't change a variable array bound or initial value to a constant
1807 in a place where a variable is invalid. Note that DECL_INITIAL
1808 isn't valid for a PARM_DECL. */
1809 current_function_decl != 0
1810 && TREE_CODE (decl) != PARM_DECL
1811 && !TREE_THIS_VOLATILE (decl)
1812 && TREE_READONLY (decl)
1813 && DECL_INITIAL (decl) != 0
1814 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1815 /* This is invalid if initial value is not constant.
1816 If it has either a function call, a memory reference,
1817 or a variable, then re-evaluating it could give different results. */
1818 && TREE_CONSTANT (DECL_INITIAL (decl))
1819 /* Check for cases where this is sub-optimal, even though valid. */
1820 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1821 return DECL_INITIAL (decl);
1825 /* Convert the array expression EXP to a pointer. */
1827 array_to_pointer_conversion (location_t loc, tree exp)
1829 tree orig_exp = exp;
1830 tree type = TREE_TYPE (exp);
1832 tree restype = TREE_TYPE (type);
1835 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1837 STRIP_TYPE_NOPS (exp);
1839 if (TREE_NO_WARNING (orig_exp))
1840 TREE_NO_WARNING (exp) = 1;
1842 ptrtype = build_pointer_type (restype);
1844 if (TREE_CODE (exp) == INDIRECT_REF)
1845 return convert (ptrtype, TREE_OPERAND (exp, 0));
1847 /* In C++ array compound literals are temporary objects unless they are
1848 const or appear in namespace scope, so they are destroyed too soon
1849 to use them for much of anything (c++/53220). */
1850 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1852 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1853 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1854 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1855 "converting an array compound literal to a pointer "
1856 "is ill-formed in C++");
1859 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1860 return convert (ptrtype, adr);
1863 /* Convert the function expression EXP to a pointer. */
1865 function_to_pointer_conversion (location_t loc, tree exp)
1867 tree orig_exp = exp;
1869 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1871 STRIP_TYPE_NOPS (exp);
1873 if (TREE_NO_WARNING (orig_exp))
1874 TREE_NO_WARNING (exp) = 1;
1876 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1879 /* Mark EXP as read, not just set, for set but not used -Wunused
1880 warning purposes. */
1883 mark_exp_read (tree exp)
1885 switch (TREE_CODE (exp))
1889 DECL_READ_P (exp) = 1;
1898 mark_exp_read (TREE_OPERAND (exp, 0));
1901 case C_MAYBE_CONST_EXPR:
1902 mark_exp_read (TREE_OPERAND (exp, 1));
1909 /* Perform the default conversion of arrays and functions to pointers.
1910 Return the result of converting EXP. For any other expression, just
1913 LOC is the location of the expression. */
1916 default_function_array_conversion (location_t loc, struct c_expr exp)
1918 tree orig_exp = exp.value;
1919 tree type = TREE_TYPE (exp.value);
1920 enum tree_code code = TREE_CODE (type);
1926 bool not_lvalue = false;
1927 bool lvalue_array_p;
1929 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1930 || CONVERT_EXPR_P (exp.value))
1931 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1933 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1935 exp.value = TREE_OPERAND (exp.value, 0);
1938 if (TREE_NO_WARNING (orig_exp))
1939 TREE_NO_WARNING (exp.value) = 1;
1941 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1942 if (!flag_isoc99 && !lvalue_array_p)
1944 /* Before C99, non-lvalue arrays do not decay to pointers.
1945 Normally, using such an array would be invalid; but it can
1946 be used correctly inside sizeof or as a statement expression.
1947 Thus, do not give an error here; an error will result later. */
1951 exp.value = array_to_pointer_conversion (loc, exp.value);
1955 exp.value = function_to_pointer_conversion (loc, exp.value);
1965 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1967 mark_exp_read (exp.value);
1968 return default_function_array_conversion (loc, exp);
1971 /* Return whether EXPR should be treated as an atomic lvalue for the
1972 purposes of load and store handling. */
1975 really_atomic_lvalue (tree expr)
1977 if (error_operand_p (expr))
1979 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1981 if (!lvalue_p (expr))
1984 /* Ignore _Atomic on register variables, since their addresses can't
1985 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1986 sequences wouldn't work. Ignore _Atomic on structures containing
1987 bit-fields, since accessing elements of atomic structures or
1988 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1989 it's undefined at translation time or execution time, and the
1990 normal atomic sequences again wouldn't work. */
1991 while (handled_component_p (expr))
1993 if (TREE_CODE (expr) == COMPONENT_REF
1994 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1996 expr = TREE_OPERAND (expr, 0);
1998 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2003 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2004 including converting functions and arrays to pointers if CONVERT_P.
2005 If READ_P, also mark the expression as having been read. */
2008 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2009 bool convert_p, bool read_p)
2012 mark_exp_read (exp.value);
2014 exp = default_function_array_conversion (loc, exp);
2015 if (really_atomic_lvalue (exp.value))
2017 vec<tree, va_gc> *params;
2018 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2019 tree expr_type = TREE_TYPE (exp.value);
2020 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2021 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2023 gcc_assert (TYPE_ATOMIC (expr_type));
2025 /* Expansion of a generic atomic load may require an addition
2026 element, so allocate enough to prevent a resize. */
2027 vec_alloc (params, 4);
2029 /* Remove the qualifiers for the rest of the expressions and
2030 create the VAL temp variable to hold the RHS. */
2031 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2032 tmp = create_tmp_var_raw (nonatomic_type);
2033 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2034 TREE_ADDRESSABLE (tmp) = 1;
2035 TREE_NO_WARNING (tmp) = 1;
2037 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2038 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2039 params->quick_push (expr_addr);
2040 params->quick_push (tmp_addr);
2041 params->quick_push (seq_cst);
2042 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2044 /* EXPR is always read. */
2045 mark_exp_read (exp.value);
2047 /* Return tmp which contains the value loaded. */
2048 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2049 NULL_TREE, NULL_TREE);
2054 /* EXP is an expression of integer type. Apply the integer promotions
2055 to it and return the promoted value. */
2058 perform_integral_promotions (tree exp)
2060 tree type = TREE_TYPE (exp);
2061 enum tree_code code = TREE_CODE (type);
2063 gcc_assert (INTEGRAL_TYPE_P (type));
2065 /* Normally convert enums to int,
2066 but convert wide enums to something wider. */
2067 if (code == ENUMERAL_TYPE)
2069 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2070 TYPE_PRECISION (integer_type_node)),
2071 ((TYPE_PRECISION (type)
2072 >= TYPE_PRECISION (integer_type_node))
2073 && TYPE_UNSIGNED (type)));
2075 return convert (type, exp);
2078 /* ??? This should no longer be needed now bit-fields have their
2080 if (TREE_CODE (exp) == COMPONENT_REF
2081 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2082 /* If it's thinner than an int, promote it like a
2083 c_promoting_integer_type_p, otherwise leave it alone. */
2084 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2085 TYPE_PRECISION (integer_type_node)))
2086 return convert (integer_type_node, exp);
2088 if (c_promoting_integer_type_p (type))
2090 /* Preserve unsignedness if not really getting any wider. */
2091 if (TYPE_UNSIGNED (type)
2092 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2093 return convert (unsigned_type_node, exp);
2095 return convert (integer_type_node, exp);
2102 /* Perform default promotions for C data used in expressions.
2103 Enumeral types or short or char are converted to int.
2104 In addition, manifest constants symbols are replaced by their values. */
2107 default_conversion (tree exp)
2110 tree type = TREE_TYPE (exp);
2111 enum tree_code code = TREE_CODE (type);
2114 mark_exp_read (exp);
2116 /* Functions and arrays have been converted during parsing. */
2117 gcc_assert (code != FUNCTION_TYPE);
2118 if (code == ARRAY_TYPE)
2121 /* Constants can be used directly unless they're not loadable. */
2122 if (TREE_CODE (exp) == CONST_DECL)
2123 exp = DECL_INITIAL (exp);
2125 /* Strip no-op conversions. */
2127 STRIP_TYPE_NOPS (exp);
2129 if (TREE_NO_WARNING (orig_exp))
2130 TREE_NO_WARNING (exp) = 1;
2132 if (code == VOID_TYPE)
2134 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2135 "void value not ignored as it ought to be");
2136 return error_mark_node;
2139 exp = require_complete_type (exp);
2140 if (exp == error_mark_node)
2141 return error_mark_node;
2143 promoted_type = targetm.promoted_type (type);
2145 return convert (promoted_type, exp);
2147 if (INTEGRAL_TYPE_P (type))
2148 return perform_integral_promotions (exp);
2153 /* Look up COMPONENT in a structure or union TYPE.
2155 If the component name is not found, returns NULL_TREE. Otherwise,
2156 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2157 stepping down the chain to the component, which is in the last
2158 TREE_VALUE of the list. Normally the list is of length one, but if
2159 the component is embedded within (nested) anonymous structures or
2160 unions, the list steps down the chain to the component. */
2163 lookup_field (tree type, tree component)
2167 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2168 to the field elements. Use a binary search on this array to quickly
2169 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2170 will always be set for structures which have many elements. */
2172 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2175 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2177 field = TYPE_FIELDS (type);
2179 top = TYPE_LANG_SPECIFIC (type)->s->len;
2180 while (top - bot > 1)
2182 half = (top - bot + 1) >> 1;
2183 field = field_array[bot+half];
2185 if (DECL_NAME (field) == NULL_TREE)
2187 /* Step through all anon unions in linear fashion. */
2188 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2190 field = field_array[bot++];
2191 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2192 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2194 tree anon = lookup_field (TREE_TYPE (field), component);
2197 return tree_cons (NULL_TREE, field, anon);
2199 /* The Plan 9 compiler permits referring
2200 directly to an anonymous struct/union field
2201 using a typedef name. */
2202 if (flag_plan9_extensions
2203 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2204 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2206 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2212 /* Entire record is only anon unions. */
2216 /* Restart the binary search, with new lower bound. */
2220 if (DECL_NAME (field) == component)
2222 if (DECL_NAME (field) < component)
2228 if (DECL_NAME (field_array[bot]) == component)
2229 field = field_array[bot];
2230 else if (DECL_NAME (field) != component)
2235 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2237 if (DECL_NAME (field) == NULL_TREE
2238 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2239 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2241 tree anon = lookup_field (TREE_TYPE (field), component);
2244 return tree_cons (NULL_TREE, field, anon);
2246 /* The Plan 9 compiler permits referring directly to an
2247 anonymous struct/union field using a typedef
2249 if (flag_plan9_extensions
2250 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2251 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2252 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2257 if (DECL_NAME (field) == component)
2261 if (field == NULL_TREE)
2265 return tree_cons (NULL_TREE, field, NULL_TREE);
2268 /* Make an expression to refer to the COMPONENT field of structure or
2269 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2270 location of the COMPONENT_REF. */
2273 build_component_ref (location_t loc, tree datum, tree component)
2275 tree type = TREE_TYPE (datum);
2276 enum tree_code code = TREE_CODE (type);
2279 bool datum_lvalue = lvalue_p (datum);
2281 if (!objc_is_public (datum, component))
2282 return error_mark_node;
2284 /* Detect Objective-C property syntax object.property. */
2285 if (c_dialect_objc ()
2286 && (ref = objc_maybe_build_component_ref (datum, component)))
2289 /* See if there is a field or component with name COMPONENT. */
2291 if (code == RECORD_TYPE || code == UNION_TYPE)
2293 if (!COMPLETE_TYPE_P (type))
2295 c_incomplete_type_error (NULL_TREE, type);
2296 return error_mark_node;
2299 field = lookup_field (type, component);
2303 error_at (loc, "%qT has no member named %qE", type, component);
2304 return error_mark_node;
2307 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2308 This might be better solved in future the way the C++ front
2309 end does it - by giving the anonymous entities each a
2310 separate name and type, and then have build_component_ref
2311 recursively call itself. We can't do that here. */
2314 tree subdatum = TREE_VALUE (field);
2317 bool use_datum_quals;
2319 if (TREE_TYPE (subdatum) == error_mark_node)
2320 return error_mark_node;
2322 /* If this is an rvalue, it does not have qualifiers in C
2323 standard terms and we must avoid propagating such
2324 qualifiers down to a non-lvalue array that is then
2325 converted to a pointer. */
2326 use_datum_quals = (datum_lvalue
2327 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2329 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2330 if (use_datum_quals)
2331 quals |= TYPE_QUALS (TREE_TYPE (datum));
2332 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2334 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2336 SET_EXPR_LOCATION (ref, loc);
2337 if (TREE_READONLY (subdatum)
2338 || (use_datum_quals && TREE_READONLY (datum)))
2339 TREE_READONLY (ref) = 1;
2340 if (TREE_THIS_VOLATILE (subdatum)
2341 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2342 TREE_THIS_VOLATILE (ref) = 1;
2344 if (TREE_DEPRECATED (subdatum))
2345 warn_deprecated_use (subdatum, NULL_TREE);
2349 field = TREE_CHAIN (field);
2355 else if (code != ERROR_MARK)
2357 "request for member %qE in something not a structure or union",
2360 return error_mark_node;
2363 /* Given an expression PTR for a pointer, return an expression
2364 for the value pointed to.
2365 ERRORSTRING is the name of the operator to appear in error messages.
2367 LOC is the location to use for the generated tree. */
2370 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2372 tree pointer = default_conversion (ptr);
2373 tree type = TREE_TYPE (pointer);
2376 if (TREE_CODE (type) == POINTER_TYPE)
2378 if (CONVERT_EXPR_P (pointer)
2379 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2381 /* If a warning is issued, mark it to avoid duplicates from
2382 the backend. This only needs to be done at
2383 warn_strict_aliasing > 2. */
2384 if (warn_strict_aliasing > 2)
2385 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2386 type, TREE_OPERAND (pointer, 0)))
2387 TREE_NO_WARNING (pointer) = 1;
2390 if (TREE_CODE (pointer) == ADDR_EXPR
2391 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2392 == TREE_TYPE (type)))
2394 ref = TREE_OPERAND (pointer, 0);
2395 protected_set_expr_location (ref, loc);
2400 tree t = TREE_TYPE (type);
2402 ref = build1 (INDIRECT_REF, t, pointer);
2404 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2406 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2408 error_at (loc, "dereferencing pointer to incomplete type "
2410 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2412 return error_mark_node;
2414 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2415 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2417 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2418 so that we get the proper error message if the result is used
2419 to assign to. Also, &* is supposed to be a no-op.
2420 And ANSI C seems to specify that the type of the result
2421 should be the const type. */
2422 /* A de-reference of a pointer to const is not a const. It is valid
2423 to change it via some other pointer. */
2424 TREE_READONLY (ref) = TYPE_READONLY (t);
2425 TREE_SIDE_EFFECTS (ref)
2426 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2427 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2428 protected_set_expr_location (ref, loc);
2432 else if (TREE_CODE (pointer) != ERROR_MARK)
2433 invalid_indirection_error (loc, type, errstring);
2435 return error_mark_node;
2438 /* This handles expressions of the form "a[i]", which denotes
2441 This is logically equivalent in C to *(a+i), but we may do it differently.
2442 If A is a variable or a member, we generate a primitive ARRAY_REF.
2443 This avoids forcing the array out of registers, and can work on
2444 arrays that are not lvalues (for example, members of structures returned
2447 For vector types, allow vector[i] but not i[vector], and create
2448 *(((type*)&vectortype) + i) for the expression.
2450 LOC is the location to use for the returned expression. */
2453 build_array_ref (location_t loc, tree array, tree index)
2456 bool swapped = false;
2457 if (TREE_TYPE (array) == error_mark_node
2458 || TREE_TYPE (index) == error_mark_node)
2459 return error_mark_node;
2461 if (flag_cilkplus && contains_array_notation_expr (index))
2464 if (!find_rank (loc, index, index, true, &rank))
2465 return error_mark_node;
2468 error_at (loc, "rank of the array's index is greater than 1");
2469 return error_mark_node;
2472 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2473 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2474 /* Allow vector[index] but not index[vector]. */
2475 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2477 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2478 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2481 "subscripted value is neither array nor pointer nor vector");
2483 return error_mark_node;
2485 std::swap (array, index);
2489 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2491 error_at (loc, "array subscript is not an integer");
2492 return error_mark_node;
2495 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2497 error_at (loc, "subscripted value is pointer to function");
2498 return error_mark_node;
2501 /* ??? Existing practice has been to warn only when the char
2502 index is syntactically the index, not for char[array]. */
2504 warn_array_subscript_with_type_char (loc, index);
2506 /* Apply default promotions *after* noticing character types. */
2507 index = default_conversion (index);
2508 if (index == error_mark_node)
2509 return error_mark_node;
2511 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2514 = convert_vector_to_pointer_for_subscript (loc, &array, index);
2516 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2520 /* An array that is indexed by a non-constant
2521 cannot be stored in a register; we must be able to do
2522 address arithmetic on its address.
2523 Likewise an array of elements of variable size. */
2524 if (TREE_CODE (index) != INTEGER_CST
2525 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2526 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2528 if (!c_mark_addressable (array))
2529 return error_mark_node;
2531 /* An array that is indexed by a constant value which is not within
2532 the array bounds cannot be stored in a register either; because we
2533 would get a crash in store_bit_field/extract_bit_field when trying
2534 to access a non-existent part of the register. */
2535 if (TREE_CODE (index) == INTEGER_CST
2536 && TYPE_DOMAIN (TREE_TYPE (array))
2537 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2539 if (!c_mark_addressable (array))
2540 return error_mark_node;
2543 if (pedantic || warn_c90_c99_compat)
2546 while (TREE_CODE (foo) == COMPONENT_REF)
2547 foo = TREE_OPERAND (foo, 0);
2548 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2549 pedwarn (loc, OPT_Wpedantic,
2550 "ISO C forbids subscripting %<register%> array");
2551 else if (!lvalue_p (foo))
2552 pedwarn_c90 (loc, OPT_Wpedantic,
2553 "ISO C90 forbids subscripting non-lvalue "
2557 type = TREE_TYPE (TREE_TYPE (array));
2558 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2559 /* Array ref is const/volatile if the array elements are
2560 or if the array is. */
2561 TREE_READONLY (rval)
2562 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2563 | TREE_READONLY (array));
2564 TREE_SIDE_EFFECTS (rval)
2565 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2566 | TREE_SIDE_EFFECTS (array));
2567 TREE_THIS_VOLATILE (rval)
2568 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2569 /* This was added by rms on 16 Nov 91.
2570 It fixes vol struct foo *a; a->elts[1]
2571 in an inline function.
2572 Hope it doesn't break something else. */
2573 | TREE_THIS_VOLATILE (array));
2574 ret = require_complete_type (rval);
2575 protected_set_expr_location (ret, loc);
2577 ret = non_lvalue_loc (loc, ret);
2582 tree ar = default_conversion (array);
2584 if (ar == error_mark_node)
2587 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2588 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2590 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2594 ret = non_lvalue_loc (loc, ret);
2599 /* Build an external reference to identifier ID. FUN indicates
2600 whether this will be used for a function call. LOC is the source
2601 location of the identifier. This sets *TYPE to the type of the
2602 identifier, which is not the same as the type of the returned value
2603 for CONST_DECLs defined as enum constants. If the type of the
2604 identifier is not available, *TYPE is set to NULL. */
2606 build_external_ref (location_t loc, tree id, int fun, tree *type)
2609 tree decl = lookup_name (id);
2611 /* In Objective-C, an instance variable (ivar) may be preferred to
2612 whatever lookup_name() found. */
2613 decl = objc_lookup_ivar (decl, id);
2616 if (decl && decl != error_mark_node)
2619 *type = TREE_TYPE (ref);
2622 /* Implicit function declaration. */
2623 ref = implicitly_declare (loc, id);
2624 else if (decl == error_mark_node)
2625 /* Don't complain about something that's already been
2626 complained about. */
2627 return error_mark_node;
2630 undeclared_variable (loc, id);
2631 return error_mark_node;
2634 if (TREE_TYPE (ref) == error_mark_node)
2635 return error_mark_node;
2637 if (TREE_DEPRECATED (ref))
2638 warn_deprecated_use (ref, NULL_TREE);
2640 /* Recursive call does not count as usage. */
2641 if (ref != current_function_decl)
2643 TREE_USED (ref) = 1;
2646 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2648 if (!in_sizeof && !in_typeof)
2649 C_DECL_USED (ref) = 1;
2650 else if (DECL_INITIAL (ref) == 0
2651 && DECL_EXTERNAL (ref)
2652 && !TREE_PUBLIC (ref))
2653 record_maybe_used_decl (ref);
2656 if (TREE_CODE (ref) == CONST_DECL)
2658 used_types_insert (TREE_TYPE (ref));
2661 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2662 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2664 warning_at (loc, OPT_Wc___compat,
2665 ("enum constant defined in struct or union "
2666 "is not visible in C++"));
2667 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2670 ref = DECL_INITIAL (ref);
2671 TREE_CONSTANT (ref) = 1;
2673 else if (current_function_decl != 0
2674 && !DECL_FILE_SCOPE_P (current_function_decl)
2675 && (VAR_OR_FUNCTION_DECL_P (ref)
2676 || TREE_CODE (ref) == PARM_DECL))
2678 tree context = decl_function_context (ref);
2680 if (context != 0 && context != current_function_decl)
2681 DECL_NONLOCAL (ref) = 1;
2683 /* C99 6.7.4p3: An inline definition of a function with external
2684 linkage ... shall not contain a reference to an identifier with
2685 internal linkage. */
2686 else if (current_function_decl != 0
2687 && DECL_DECLARED_INLINE_P (current_function_decl)
2688 && DECL_EXTERNAL (current_function_decl)
2689 && VAR_OR_FUNCTION_DECL_P (ref)
2690 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2691 && ! TREE_PUBLIC (ref)
2692 && DECL_CONTEXT (ref) != current_function_decl)
2693 record_inline_static (loc, current_function_decl, ref,
2699 /* Record details of decls possibly used inside sizeof or typeof. */
2700 struct maybe_used_decl
2704 /* The level seen at (in_sizeof + in_typeof). */
2706 /* The next one at this level or above, or NULL. */
2707 struct maybe_used_decl *next;
2710 static struct maybe_used_decl *maybe_used_decls;
2712 /* Record that DECL, an undefined static function reference seen
2713 inside sizeof or typeof, might be used if the operand of sizeof is
2714 a VLA type or the operand of typeof is a variably modified
2718 record_maybe_used_decl (tree decl)
2720 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2722 t->level = in_sizeof + in_typeof;
2723 t->next = maybe_used_decls;
2724 maybe_used_decls = t;
2727 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2728 USED is false, just discard them. If it is true, mark them used
2729 (if no longer inside sizeof or typeof) or move them to the next
2730 level up (if still inside sizeof or typeof). */
2733 pop_maybe_used (bool used)
2735 struct maybe_used_decl *p = maybe_used_decls;
2736 int cur_level = in_sizeof + in_typeof;
2737 while (p && p->level > cur_level)
2742 C_DECL_USED (p->decl) = 1;
2744 p->level = cur_level;
2748 if (!used || cur_level == 0)
2749 maybe_used_decls = p;
2752 /* Return the result of sizeof applied to EXPR. */
2755 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2758 if (expr.value == error_mark_node)
2760 ret.value = error_mark_node;
2761 ret.original_code = ERROR_MARK;
2762 ret.original_type = NULL;
2763 pop_maybe_used (false);
2767 bool expr_const_operands = true;
2769 if (TREE_CODE (expr.value) == PARM_DECL
2770 && C_ARRAY_PARAMETER (expr.value))
2772 if (warning_at (loc, OPT_Wsizeof_array_argument,
2773 "%<sizeof%> on array function parameter %qE will "
2774 "return size of %qT", expr.value,
2775 expr.original_type))
2776 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2778 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2779 &expr_const_operands);
2780 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2781 c_last_sizeof_arg = expr.value;
2782 ret.original_code = SIZEOF_EXPR;
2783 ret.original_type = NULL;
2784 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2786 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2787 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2788 folded_expr, ret.value);
2789 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2790 SET_EXPR_LOCATION (ret.value, loc);
2792 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2797 /* Return the result of sizeof applied to T, a structure for the type
2798 name passed to sizeof (rather than the type itself). LOC is the
2799 location of the original expression. */
2802 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2806 tree type_expr = NULL_TREE;
2807 bool type_expr_const = true;
2808 type = groktypename (t, &type_expr, &type_expr_const);
2809 ret.value = c_sizeof (loc, type);
2810 c_last_sizeof_arg = type;
2811 ret.original_code = SIZEOF_EXPR;
2812 ret.original_type = NULL;
2813 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2814 && c_vla_type_p (type))
2816 /* If the type is a [*] array, it is a VLA but is represented as
2817 having a size of zero. In such a case we must ensure that
2818 the result of sizeof does not get folded to a constant by
2819 c_fully_fold, because if the size is evaluated the result is
2820 not constant and so constraints on zero or negative size
2821 arrays must not be applied when this sizeof call is inside
2822 another array declarator. */
2824 type_expr = integer_zero_node;
2825 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2826 type_expr, ret.value);
2827 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2829 pop_maybe_used (type != error_mark_node
2830 ? C_TYPE_VARIABLE_SIZE (type) : false);
2834 /* Build a function call to function FUNCTION with parameters PARAMS.
2835 The function call is at LOC.
2836 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2837 TREE_VALUE of each node is a parameter-expression.
2838 FUNCTION's data type may be a function type or a pointer-to-function. */
2841 build_function_call (location_t loc, tree function, tree params)
2843 vec<tree, va_gc> *v;
2846 vec_alloc (v, list_length (params));
2847 for (; params; params = TREE_CHAIN (params))
2848 v->quick_push (TREE_VALUE (params));
2849 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2854 /* Give a note about the location of the declaration of DECL. */
2856 static void inform_declaration (tree decl)
2858 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2859 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2862 /* Build a function call to function FUNCTION with parameters PARAMS.
2863 ORIGTYPES, if not NULL, is a vector of types; each element is
2864 either NULL or the original type of the corresponding element in
2865 PARAMS. The original type may differ from TREE_TYPE of the
2866 parameter for enums. FUNCTION's data type may be a function type
2867 or pointer-to-function. This function changes the elements of
2871 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2872 tree function, vec<tree, va_gc> *params,
2873 vec<tree, va_gc> *origtypes)
2875 tree fntype, fundecl = 0;
2876 tree name = NULL_TREE, result;
2882 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2883 STRIP_TYPE_NOPS (function);
2885 /* Convert anything with function type to a pointer-to-function. */
2886 if (TREE_CODE (function) == FUNCTION_DECL)
2888 name = DECL_NAME (function);
2891 tm_malloc_replacement (function);
2893 /* Atomic functions have type checking/casting already done. They are
2894 often rewritten and don't match the original parameter list. */
2895 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2899 && is_cilkplus_reduce_builtin (function))
2902 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2903 function = function_to_pointer_conversion (loc, function);
2905 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2906 expressions, like those used for ObjC messenger dispatches. */
2907 if (params && !params->is_empty ())
2908 function = objc_rewrite_function_call (function, (*params)[0]);
2910 function = c_fully_fold (function, false, NULL);
2912 fntype = TREE_TYPE (function);
2914 if (TREE_CODE (fntype) == ERROR_MARK)
2915 return error_mark_node;
2917 if (!(TREE_CODE (fntype) == POINTER_TYPE
2918 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2920 if (!flag_diagnostics_show_caret)
2922 "called object %qE is not a function or function pointer",
2924 else if (DECL_P (function))
2927 "called object %qD is not a function or function pointer",
2929 inform_declaration (function);
2933 "called object is not a function or function pointer");
2934 return error_mark_node;
2937 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2938 current_function_returns_abnormally = 1;
2940 /* fntype now gets the type of function pointed to. */
2941 fntype = TREE_TYPE (fntype);
2943 /* Convert the parameters to the types declared in the
2944 function prototype, or apply default promotions. */
2946 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2947 origtypes, function, fundecl);
2949 return error_mark_node;
2951 /* Check that the function is called through a compatible prototype.
2952 If it is not, warn. */
2953 if (CONVERT_EXPR_P (function)
2954 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2955 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2956 && !comptypes (fntype, TREE_TYPE (tem)))
2958 tree return_type = TREE_TYPE (fntype);
2960 /* This situation leads to run-time undefined behavior. We can't,
2961 therefore, simply error unless we can prove that all possible
2962 executions of the program must execute the code. */
2963 warning_at (loc, 0, "function called through a non-compatible type");
2965 if (VOID_TYPE_P (return_type)
2966 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2968 "function with qualified void return type called");
2971 argarray = vec_safe_address (params);
2973 /* Check that arguments to builtin functions match the expectations. */
2975 && DECL_BUILT_IN (fundecl)
2976 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2977 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2978 return error_mark_node;
2980 /* Check that the arguments to the function are valid. */
2981 check_function_arguments (fntype, nargs, argarray);
2983 if (name != NULL_TREE
2984 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2986 if (require_constant_value)
2988 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2989 function, nargs, argarray);
2991 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2992 function, nargs, argarray);
2993 if (TREE_CODE (result) == NOP_EXPR
2994 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2995 STRIP_TYPE_NOPS (result);
2998 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2999 function, nargs, argarray);
3001 if (VOID_TYPE_P (TREE_TYPE (result)))
3003 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3005 "function with qualified void return type called");
3008 return require_complete_type (result);
3011 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3014 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3015 tree function, vec<tree, va_gc> *params,
3016 vec<tree, va_gc> *origtypes)
3018 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3019 STRIP_TYPE_NOPS (function);
3021 /* Convert anything with function type to a pointer-to-function. */
3022 if (TREE_CODE (function) == FUNCTION_DECL)
3024 /* Implement type-directed function overloading for builtins.
3025 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3026 handle all the type checking. The result is a complete expression
3027 that implements this function call. */
3028 tree tem = resolve_overloaded_builtin (loc, function, params);
3032 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3035 /* Convert the argument expressions in the vector VALUES
3036 to the types in the list TYPELIST.
3038 If TYPELIST is exhausted, or when an element has NULL as its type,
3039 perform the default conversions.
3041 ORIGTYPES is the original types of the expressions in VALUES. This
3042 holds the type of enum values which have been converted to integral
3043 types. It may be NULL.
3045 FUNCTION is a tree for the called function. It is used only for
3046 error messages, where it is formatted with %qE.
3048 This is also where warnings about wrong number of args are generated.
3050 ARG_LOC are locations of function arguments (if any).
3052 Returns the actual number of arguments processed (which may be less
3053 than the length of VALUES in some error situations), or -1 on
3057 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3058 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3059 tree function, tree fundecl)
3062 unsigned int parmnum;
3063 bool error_args = false;
3064 const bool type_generic = fundecl
3065 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3066 bool type_generic_remove_excess_precision = false;
3069 /* Change pointer to function to the function itself for
3071 if (TREE_CODE (function) == ADDR_EXPR
3072 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3073 function = TREE_OPERAND (function, 0);
3075 /* Handle an ObjC selector specially for diagnostics. */
3076 selector = objc_message_selector ();
3078 /* For type-generic built-in functions, determine whether excess
3079 precision should be removed (classification) or not
3082 && DECL_BUILT_IN (fundecl)
3083 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3085 switch (DECL_FUNCTION_CODE (fundecl))
3087 case BUILT_IN_ISFINITE:
3088 case BUILT_IN_ISINF:
3089 case BUILT_IN_ISINF_SIGN:
3090 case BUILT_IN_ISNAN:
3091 case BUILT_IN_ISNORMAL:
3092 case BUILT_IN_FPCLASSIFY:
3093 type_generic_remove_excess_precision = true;
3097 type_generic_remove_excess_precision = false;
3101 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3102 return vec_safe_length (values);
3104 /* Scan the given expressions and types, producing individual
3105 converted arguments. */
3107 for (typetail = typelist, parmnum = 0;
3108 values && values->iterate (parmnum, &val);
3111 tree type = typetail ? TREE_VALUE (typetail) : 0;
3112 tree valtype = TREE_TYPE (val);
3113 tree rname = function;
3114 int argnum = parmnum + 1;
3115 const char *invalid_func_diag;
3116 bool excess_precision = false;
3119 /* Some __atomic_* builtins have additional hidden argument at
3122 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3123 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3126 if (type == void_type_node)
3129 error_at (loc, "too many arguments to method %qE", selector);
3131 error_at (loc, "too many arguments to function %qE", function);
3132 inform_declaration (fundecl);
3133 return error_args ? -1 : (int) parmnum;
3136 if (selector && argnum > 2)
3142 npc = null_pointer_constant_p (val);
3144 /* If there is excess precision and a prototype, convert once to
3145 the required type rather than converting via the semantic
3146 type. Likewise without a prototype a float value represented
3147 as long double should be converted once to double. But for
3148 type-generic classification functions excess precision must
3150 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3151 && (type || !type_generic || !type_generic_remove_excess_precision))
3153 val = TREE_OPERAND (val, 0);
3154 excess_precision = true;
3156 val = c_fully_fold (val, false, NULL);
3157 STRIP_TYPE_NOPS (val);
3159 val = require_complete_type (val);
3163 /* Formal parm type is specified by a function prototype. */
3165 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3167 error_at (ploc, "type of formal parameter %d is incomplete",
3175 /* Optionally warn about conversions that
3176 differ from the default conversions. */
3177 if (warn_traditional_conversion || warn_traditional)
3179 unsigned int formal_prec = TYPE_PRECISION (type);
3181 if (INTEGRAL_TYPE_P (type)
3182 && TREE_CODE (valtype) == REAL_TYPE)
3183 warning_at (ploc, OPT_Wtraditional_conversion,
3184 "passing argument %d of %qE as integer rather "
3185 "than floating due to prototype",
3187 if (INTEGRAL_TYPE_P (type)
3188 && TREE_CODE (valtype) == COMPLEX_TYPE)
3189 warning_at (ploc, OPT_Wtraditional_conversion,
3190 "passing argument %d of %qE as integer rather "
3191 "than complex due to prototype",
3193 else if (TREE_CODE (type) == COMPLEX_TYPE
3194 && TREE_CODE (valtype) == REAL_TYPE)
3195 warning_at (ploc, OPT_Wtraditional_conversion,
3196 "passing argument %d of %qE as complex rather "
3197 "than floating due to prototype",
3199 else if (TREE_CODE (type) == REAL_TYPE
3200 && INTEGRAL_TYPE_P (valtype))
3201 warning_at (ploc, OPT_Wtraditional_conversion,
3202 "passing argument %d of %qE as floating rather "
3203 "than integer due to prototype",
3205 else if (TREE_CODE (type) == COMPLEX_TYPE
3206 && INTEGRAL_TYPE_P (valtype))
3207 warning_at (ploc, OPT_Wtraditional_conversion,
3208 "passing argument %d of %qE as complex rather "
3209 "than integer due to prototype",
3211 else if (TREE_CODE (type) == REAL_TYPE
3212 && TREE_CODE (valtype) == COMPLEX_TYPE)
3213 warning_at (ploc, OPT_Wtraditional_conversion,
3214 "passing argument %d of %qE as floating rather "
3215 "than complex due to prototype",
3217 /* ??? At some point, messages should be written about
3218 conversions between complex types, but that's too messy
3220 else if (TREE_CODE (type) == REAL_TYPE
3221 && TREE_CODE (valtype) == REAL_TYPE)
3223 /* Warn if any argument is passed as `float',
3224 since without a prototype it would be `double'. */
3225 if (formal_prec == TYPE_PRECISION (float_type_node)
3226 && type != dfloat32_type_node)
3227 warning_at (ploc, 0,
3228 "passing argument %d of %qE as %<float%> "
3229 "rather than %<double%> due to prototype",
3232 /* Warn if mismatch between argument and prototype
3233 for decimal float types. Warn of conversions with
3234 binary float types and of precision narrowing due to
3236 else if (type != valtype
3237 && (type == dfloat32_type_node
3238 || type == dfloat64_type_node
3239 || type == dfloat128_type_node
3240 || valtype == dfloat32_type_node
3241 || valtype == dfloat64_type_node
3242 || valtype == dfloat128_type_node)
3244 <= TYPE_PRECISION (valtype)
3245 || (type == dfloat128_type_node
3247 != dfloat64_type_node
3249 != dfloat32_type_node)))
3250 || (type == dfloat64_type_node
3252 != dfloat32_type_node))))
3253 warning_at (ploc, 0,
3254 "passing argument %d of %qE as %qT "
3255 "rather than %qT due to prototype",
3256 argnum, rname, type, valtype);
3259 /* Detect integer changing in width or signedness.
3260 These warnings are only activated with
3261 -Wtraditional-conversion, not with -Wtraditional. */
3262 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3263 && INTEGRAL_TYPE_P (valtype))
3265 tree would_have_been = default_conversion (val);
3266 tree type1 = TREE_TYPE (would_have_been);
3268 if (TREE_CODE (type) == ENUMERAL_TYPE
3269 && (TYPE_MAIN_VARIANT (type)
3270 == TYPE_MAIN_VARIANT (valtype)))
3271 /* No warning if function asks for enum
3272 and the actual arg is that enum type. */
3274 else if (formal_prec != TYPE_PRECISION (type1))
3275 warning_at (ploc, OPT_Wtraditional_conversion,
3276 "passing argument %d of %qE "
3277 "with different width due to prototype",
3279 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3281 /* Don't complain if the formal parameter type
3282 is an enum, because we can't tell now whether
3283 the value was an enum--even the same enum. */
3284 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3286 else if (TREE_CODE (val) == INTEGER_CST
3287 && int_fits_type_p (val, type))
3288 /* Change in signedness doesn't matter
3289 if a constant value is unaffected. */
3291 /* If the value is extended from a narrower
3292 unsigned type, it doesn't matter whether we
3293 pass it as signed or unsigned; the value
3294 certainly is the same either way. */
3295 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3296 && TYPE_UNSIGNED (valtype))
3298 else if (TYPE_UNSIGNED (type))
3299 warning_at (ploc, OPT_Wtraditional_conversion,
3300 "passing argument %d of %qE "
3301 "as unsigned due to prototype",
3304 warning_at (ploc, OPT_Wtraditional_conversion,
3305 "passing argument %d of %qE "
3306 "as signed due to prototype",
3311 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3312 sake of better warnings from convert_and_check. */
3313 if (excess_precision)
3314 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3315 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3316 parmval = convert_for_assignment (loc, ploc, type,
3317 val, origtype, ic_argpass,
3318 npc, fundecl, function,
3321 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3322 && INTEGRAL_TYPE_P (type)
3323 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3324 parmval = default_conversion (parmval);
3327 else if (TREE_CODE (valtype) == REAL_TYPE
3328 && (TYPE_PRECISION (valtype)
3329 <= TYPE_PRECISION (double_type_node))
3330 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3331 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3332 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3338 /* Convert `float' to `double'. */
3339 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3340 warning_at (ploc, OPT_Wdouble_promotion,
3341 "implicit conversion from %qT to %qT when passing "
3342 "argument to function",
3343 valtype, double_type_node);
3344 parmval = convert (double_type_node, val);
3347 else if (excess_precision && !type_generic)
3348 /* A "double" argument with excess precision being passed
3349 without a prototype or in variable arguments. */
3350 parmval = convert (valtype, val);
3351 else if ((invalid_func_diag =
3352 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3354 error (invalid_func_diag);
3358 /* Convert `short' and `char' to full-size `int'. */
3359 parmval = default_conversion (val);
3361 (*values)[parmnum] = parmval;
3362 if (parmval == error_mark_node)
3366 typetail = TREE_CHAIN (typetail);
3369 gcc_assert (parmnum == vec_safe_length (values));
3371 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3373 error_at (loc, "too few arguments to function %qE", function);
3374 inform_declaration (fundecl);
3378 return error_args ? -1 : (int) parmnum;
3381 /* This is the entry point used by the parser to build unary operators
3382 in the input. CODE, a tree_code, specifies the unary operator, and
3383 ARG is the operand. For unary plus, the C parser currently uses
3384 CONVERT_EXPR for code.
3386 LOC is the location to use for the tree generated.
3390 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3392 struct c_expr result;
3394 result.value = build_unary_op (loc, code, arg.value, 0);
3395 result.original_code = code;
3396 result.original_type = NULL;
3398 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3399 overflow_warning (loc, result.value);
3404 /* This is the entry point used by the parser to build binary operators
3405 in the input. CODE, a tree_code, specifies the binary operator, and
3406 ARG1 and ARG2 are the operands. In addition to constructing the
3407 expression, we check for operands that were written with other binary
3408 operators in a way that is likely to confuse the user.
3410 LOCATION is the location of the binary operator. */
3413 parser_build_binary_op (location_t location, enum tree_code code,
3414 struct c_expr arg1, struct c_expr arg2)
3416 struct c_expr result;
3418 enum tree_code code1 = arg1.original_code;
3419 enum tree_code code2 = arg2.original_code;
3420 tree type1 = (arg1.original_type
3421 ? arg1.original_type
3422 : TREE_TYPE (arg1.value));
3423 tree type2 = (arg2.original_type
3424 ? arg2.original_type
3425 : TREE_TYPE (arg2.value));
3427 result.value = build_binary_op (location, code,
3428 arg1.value, arg2.value, 1);
3429 result.original_code = code;
3430 result.original_type = NULL;
3432 if (TREE_CODE (result.value) == ERROR_MARK)
3435 if (location != UNKNOWN_LOCATION)
3436 protected_set_expr_location (result.value, location);
3438 /* Check for cases such as x+y<<z which users are likely
3440 if (warn_parentheses)
3441 warn_about_parentheses (location, code, code1, arg1.value, code2,
3444 if (warn_logical_op)
3445 warn_logical_operator (location, code, TREE_TYPE (result.value),
3446 code1, arg1.value, code2, arg2.value);
3448 if (warn_logical_not_paren
3449 && TREE_CODE_CLASS (code) == tcc_comparison
3450 && code1 == TRUTH_NOT_EXPR
3451 && code2 != TRUTH_NOT_EXPR
3452 /* Avoid warning for !!x == y. */
3453 && (TREE_CODE (arg1.value) != NE_EXPR
3454 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3456 /* Avoid warning for !b == y where b has _Bool type. */
3457 tree t = integer_zero_node;
3458 if (TREE_CODE (arg1.value) == EQ_EXPR
3459 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3460 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3462 t = TREE_OPERAND (arg1.value, 0);
3465 if (TREE_TYPE (t) != integer_type_node)
3467 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3468 t = C_MAYBE_CONST_EXPR_EXPR (t);
3469 else if (CONVERT_EXPR_P (t))
3470 t = TREE_OPERAND (t, 0);
3476 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3477 warn_logical_not_parentheses (location, code, arg2.value);
3480 /* Warn about comparisons against string literals, with the exception
3481 of testing for equality or inequality of a string literal with NULL. */
3482 if (code == EQ_EXPR || code == NE_EXPR)
3484 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3485 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3486 warning_at (location, OPT_Waddress,
3487 "comparison with string literal results in unspecified behavior");
3489 else if (TREE_CODE_CLASS (code) == tcc_comparison
3490 && (code1 == STRING_CST || code2 == STRING_CST))
3491 warning_at (location, OPT_Waddress,
3492 "comparison with string literal results in unspecified behavior");
3494 if (TREE_OVERFLOW_P (result.value)
3495 && !TREE_OVERFLOW_P (arg1.value)
3496 && !TREE_OVERFLOW_P (arg2.value))
3497 overflow_warning (location, result.value);
3499 /* Warn about comparisons of different enum types. */
3500 if (warn_enum_compare
3501 && TREE_CODE_CLASS (code) == tcc_comparison
3502 && TREE_CODE (type1) == ENUMERAL_TYPE
3503 && TREE_CODE (type2) == ENUMERAL_TYPE
3504 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3505 warning_at (location, OPT_Wenum_compare,
3506 "comparison between %qT and %qT",
3512 /* Return a tree for the difference of pointers OP0 and OP1.
3513 The resulting tree has type int. */
3516 pointer_diff (location_t loc, tree op0, tree op1)
3518 tree restype = ptrdiff_type_node;
3519 tree result, inttype;
3521 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3522 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3523 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3524 tree orig_op1 = op1;
3526 /* If the operands point into different address spaces, we need to
3527 explicitly convert them to pointers into the common address space
3528 before we can subtract the numerical address values. */
3531 addr_space_t as_common;
3534 /* Determine the common superset address space. This is guaranteed
3535 to exist because the caller verified that comp_target_types
3536 returned non-zero. */
3537 if (!addr_space_superset (as0, as1, &as_common))
3540 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3541 op0 = convert (common_type, op0);
3542 op1 = convert (common_type, op1);
3545 /* Determine integer type to perform computations in. This will usually
3546 be the same as the result type (ptrdiff_t), but may need to be a wider
3547 type if pointers for the address space are wider than ptrdiff_t. */
3548 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3549 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3553 if (TREE_CODE (target_type) == VOID_TYPE)
3554 pedwarn (loc, OPT_Wpointer_arith,
3555 "pointer of type %<void *%> used in subtraction");
3556 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3557 pedwarn (loc, OPT_Wpointer_arith,
3558 "pointer to a function used in subtraction");
3560 /* First do the subtraction as integers;
3561 then drop through to build the divide operator.
3562 Do not do default conversions on the minus operator
3563 in case restype is a short type. */
3565 op0 = build_binary_op (loc,
3566 MINUS_EXPR, convert (inttype, op0),
3567 convert (inttype, op1), 0);
3568 /* This generates an error if op1 is pointer to incomplete type. */
3569 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3570 error_at (loc, "arithmetic on pointer to an incomplete type");
3572 op1 = c_size_in_bytes (target_type);
3574 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3575 error_at (loc, "arithmetic on pointer to an empty aggregate");
3577 /* Divide by the size, in easiest possible way. */
3578 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3579 op0, convert (inttype, op1));
3581 /* Convert to final result type if necessary. */
3582 return convert (restype, result);
3585 /* Expand atomic compound assignments into an approriate sequence as
3586 specified by the C11 standard section 6.5.16.2.
3592 This sequence is used for all types for which these operations are
3595 In addition, built-in versions of the 'fe' prefixed routines may
3596 need to be invoked for floating point (real, complex or vector) when
3597 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3607 __atomic_load (addr, &old, SEQ_CST);
3608 feholdexcept (&fenv);
3610 newval = old op val;
3611 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3614 feclearexcept (FE_ALL_EXCEPT);
3617 feupdateenv (&fenv);
3619 Also note that the compiler is simply issuing the generic form of
3620 the atomic operations. This requires temp(s) and has their address
3621 taken. The atomic processing is smart enough to figure out when the
3622 size of an object can utilize a lock-free version, and convert the
3623 built-in call to the appropriate lock-free routine. The optimizers
3624 will then dispose of any temps that are no longer required, and
3625 lock-free implementations are utilized as long as there is target
3626 support for the required size.
3628 If the operator is NOP_EXPR, then this is a simple assignment, and
3629 an __atomic_store is issued to perform the assignment rather than
3634 /* Build an atomic assignment at LOC, expanding into the proper
3635 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3636 the result of the operation, unless RETURN_OLD_P in which case
3637 return the old value of LHS (this is only for postincrement and
3640 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3641 tree rhs, bool return_old_p)
3643 tree fndecl, func_call;
3644 vec<tree, va_gc> *params;
3645 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3648 tree stmt, goto_stmt;
3649 tree loop_label, loop_decl, done_label, done_decl;
3651 tree lhs_type = TREE_TYPE (lhs);
3652 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3653 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3654 tree rhs_type = TREE_TYPE (rhs);
3656 gcc_assert (TYPE_ATOMIC (lhs_type));
3659 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3661 /* Allocate enough vector items for a compare_exchange. */
3662 vec_alloc (params, 6);
3664 /* Create a compound statement to hold the sequence of statements
3666 compound_stmt = c_begin_compound_stmt (false);
3668 /* Fold the RHS if it hasn't already been folded. */
3669 if (modifycode != NOP_EXPR)
3670 rhs = c_fully_fold (rhs, false, NULL);
3672 /* Remove the qualifiers for the rest of the expressions and create
3673 the VAL temp variable to hold the RHS. */
3674 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3675 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3676 val = create_tmp_var_raw (nonatomic_rhs_type);
3677 TREE_ADDRESSABLE (val) = 1;
3678 TREE_NO_WARNING (val) = 1;
3679 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3681 SET_EXPR_LOCATION (rhs, loc);
3684 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3686 if (modifycode == NOP_EXPR)
3688 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3689 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3690 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3691 params->quick_push (lhs_addr);
3692 params->quick_push (rhs);
3693 params->quick_push (seq_cst);
3694 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3695 add_stmt (func_call);
3697 /* Finish the compound statement. */
3698 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3700 /* VAL is the value which was stored, return a COMPOUND_STMT of
3701 the statement and that value. */
3702 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3705 /* Create the variables and labels required for the op= form. */
3706 old = create_tmp_var_raw (nonatomic_lhs_type);
3707 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3708 TREE_ADDRESSABLE (old) = 1;
3709 TREE_NO_WARNING (old) = 1;
3711 newval = create_tmp_var_raw (nonatomic_lhs_type);
3712 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3713 TREE_ADDRESSABLE (newval) = 1;
3715 loop_decl = create_artificial_label (loc);
3716 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3718 done_decl = create_artificial_label (loc);
3719 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3721 /* __atomic_load (addr, &old, SEQ_CST). */
3722 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3723 params->quick_push (lhs_addr);
3724 params->quick_push (old_addr);
3725 params->quick_push (seq_cst);
3726 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3727 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3730 params->truncate (0);
3732 /* Create the expressions for floating-point environment
3733 manipulation, if required. */
3734 bool need_fenv = (flag_trapping_math
3735 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3736 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3738 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3741 add_stmt (hold_call);
3744 add_stmt (loop_label);
3746 /* newval = old + val; */
3747 rhs = build_binary_op (loc, modifycode, old, val, 1);
3748 rhs = c_fully_fold (rhs, false, NULL);
3749 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3750 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3752 if (rhs != error_mark_node)
3754 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3756 SET_EXPR_LOCATION (rhs, loc);
3760 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3762 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3763 params->quick_push (lhs_addr);
3764 params->quick_push (old_addr);
3765 params->quick_push (newval_addr);
3766 params->quick_push (integer_zero_node);
3767 params->quick_push (seq_cst);
3768 params->quick_push (seq_cst);
3769 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3771 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3772 SET_EXPR_LOCATION (goto_stmt, loc);
3774 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3775 SET_EXPR_LOCATION (stmt, loc);
3779 add_stmt (clear_call);
3782 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3783 SET_EXPR_LOCATION (goto_stmt, loc);
3784 add_stmt (goto_stmt);
3787 add_stmt (done_label);
3790 add_stmt (update_call);
3792 /* Finish the compound statement. */
3793 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3795 /* NEWVAL is the value that was successfully stored, return a
3796 COMPOUND_EXPR of the statement and the appropriate value. */
3797 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3798 return_old_p ? old : newval);
3801 /* Construct and perhaps optimize a tree representation
3802 for a unary operation. CODE, a tree_code, specifies the operation
3803 and XARG is the operand.
3804 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3805 the default promotions (such as from short to int).
3806 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3807 allows non-lvalues; this is only used to handle conversion of non-lvalue
3808 arrays to pointers in C99.
3810 LOCATION is the location of the operator. */
3813 build_unary_op (location_t location,
3814 enum tree_code code, tree xarg, int flag)
3816 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3819 enum tree_code typecode;
3821 tree ret = error_mark_node;
3822 tree eptype = NULL_TREE;
3823 int noconvert = flag;
3824 const char *invalid_op_diag;
3827 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3829 arg = remove_c_maybe_const_expr (arg);
3831 if (code != ADDR_EXPR)
3832 arg = require_complete_type (arg);
3834 typecode = TREE_CODE (TREE_TYPE (arg));
3835 if (typecode == ERROR_MARK)
3836 return error_mark_node;
3837 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3838 typecode = INTEGER_TYPE;
3840 if ((invalid_op_diag
3841 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3843 error_at (location, invalid_op_diag);
3844 return error_mark_node;
3847 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3849 eptype = TREE_TYPE (arg);
3850 arg = TREE_OPERAND (arg, 0);
3856 /* This is used for unary plus, because a CONVERT_EXPR
3857 is enough to prevent anybody from looking inside for
3858 associativity, but won't generate any code. */
3859 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3860 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3861 || typecode == VECTOR_TYPE))
3863 error_at (location, "wrong type argument to unary plus");
3864 return error_mark_node;
3866 else if (!noconvert)
3867 arg = default_conversion (arg);
3868 arg = non_lvalue_loc (location, arg);
3872 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3873 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3874 || typecode == VECTOR_TYPE))
3876 error_at (location, "wrong type argument to unary minus");
3877 return error_mark_node;
3879 else if (!noconvert)
3880 arg = default_conversion (arg);
3884 /* ~ works on integer types and non float vectors. */
3885 if (typecode == INTEGER_TYPE
3886 || (typecode == VECTOR_TYPE
3887 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3890 arg = default_conversion (arg);
3892 else if (typecode == COMPLEX_TYPE)
3895 pedwarn (location, OPT_Wpedantic,
3896 "ISO C does not support %<~%> for complex conjugation");
3898 arg = default_conversion (arg);
3902 error_at (location, "wrong type argument to bit-complement");
3903 return error_mark_node;
3908 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3910 error_at (location, "wrong type argument to abs");
3911 return error_mark_node;
3913 else if (!noconvert)
3914 arg = default_conversion (arg);
3918 /* Conjugating a real value is a no-op, but allow it anyway. */
3919 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3920 || typecode == COMPLEX_TYPE))
3922 error_at (location, "wrong type argument to conjugation");
3923 return error_mark_node;
3925 else if (!noconvert)
3926 arg = default_conversion (arg);
3929 case TRUTH_NOT_EXPR:
3930 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3931 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3932 && typecode != COMPLEX_TYPE)
3935 "wrong type argument to unary exclamation mark");
3936 return error_mark_node;
3940 arg = c_objc_common_truthvalue_conversion (location, xarg);
3941 arg = remove_c_maybe_const_expr (arg);
3944 arg = c_objc_common_truthvalue_conversion (location, arg);
3945 ret = invert_truthvalue_loc (location, arg);
3946 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3947 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3948 location = EXPR_LOCATION (ret);
3949 goto return_build_unary_op;
3953 ret = build_real_imag_expr (location, code, arg);
3954 if (ret == error_mark_node)
3955 return error_mark_node;
3956 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3957 eptype = TREE_TYPE (eptype);
3958 goto return_build_unary_op;
3960 case PREINCREMENT_EXPR:
3961 case POSTINCREMENT_EXPR:
3962 case PREDECREMENT_EXPR:
3963 case POSTDECREMENT_EXPR:
3965 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3967 tree inner = build_unary_op (location, code,
3968 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3969 if (inner == error_mark_node)
3970 return error_mark_node;
3971 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3972 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3973 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3974 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3975 goto return_build_unary_op;
3978 /* Complain about anything that is not a true lvalue. In
3979 Objective-C, skip this check for property_refs. */
3980 if (!objc_is_property_ref (arg)
3981 && !lvalue_or_else (location,
3982 arg, ((code == PREINCREMENT_EXPR
3983 || code == POSTINCREMENT_EXPR)
3986 return error_mark_node;
3988 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3990 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3991 warning_at (location, OPT_Wc___compat,
3992 "increment of enumeration value is invalid in C++");
3994 warning_at (location, OPT_Wc___compat,
3995 "decrement of enumeration value is invalid in C++");
3998 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3999 arg = c_fully_fold (arg, false, NULL);
4002 atomic_op = really_atomic_lvalue (arg);
4004 /* Increment or decrement the real part of the value,
4005 and don't change the imaginary part. */
4006 if (typecode == COMPLEX_TYPE)
4010 pedwarn (location, OPT_Wpedantic,
4011 "ISO C does not support %<++%> and %<--%> on complex types");
4015 arg = stabilize_reference (arg);
4016 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4017 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4018 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4019 if (real == error_mark_node || imag == error_mark_node)
4020 return error_mark_node;
4021 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4023 goto return_build_unary_op;
4027 /* Report invalid types. */
4029 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4030 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4031 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4033 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4034 error_at (location, "wrong type argument to increment");
4036 error_at (location, "wrong type argument to decrement");
4038 return error_mark_node;
4044 argtype = TREE_TYPE (arg);
4046 /* Compute the increment. */
4048 if (typecode == POINTER_TYPE)
4050 /* If pointer target is an incomplete type,
4051 we just cannot know how to do the arithmetic. */
4052 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4054 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4056 "increment of pointer to an incomplete type %qT",
4057 TREE_TYPE (argtype));
4060 "decrement of pointer to an incomplete type %qT",
4061 TREE_TYPE (argtype));
4063 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4064 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4066 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4067 pedwarn (location, OPT_Wpointer_arith,
4068 "wrong type argument to increment");
4070 pedwarn (location, OPT_Wpointer_arith,
4071 "wrong type argument to decrement");
4074 inc = c_size_in_bytes (TREE_TYPE (argtype));
4075 inc = convert_to_ptrofftype_loc (location, inc);
4077 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4079 /* For signed fract types, we invert ++ to -- or
4080 -- to ++, and change inc from 1 to -1, because
4081 it is not possible to represent 1 in signed fract constants.
4082 For unsigned fract types, the result always overflows and
4083 we get an undefined (original) or the maximum value. */
4084 if (code == PREINCREMENT_EXPR)
4085 code = PREDECREMENT_EXPR;
4086 else if (code == PREDECREMENT_EXPR)
4087 code = PREINCREMENT_EXPR;
4088 else if (code == POSTINCREMENT_EXPR)
4089 code = POSTDECREMENT_EXPR;
4090 else /* code == POSTDECREMENT_EXPR */
4091 code = POSTINCREMENT_EXPR;
4093 inc = integer_minus_one_node;
4094 inc = convert (argtype, inc);
4098 inc = VECTOR_TYPE_P (argtype)
4099 ? build_one_cst (argtype)
4101 inc = convert (argtype, inc);
4104 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4105 need to ask Objective-C to build the increment or decrement
4106 expression for it. */
4107 if (objc_is_property_ref (arg))
4108 return objc_build_incr_expr_for_property_ref (location, code,
4111 /* Report a read-only lvalue. */
4112 if (TYPE_READONLY (argtype))
4114 readonly_error (location, arg,
4115 ((code == PREINCREMENT_EXPR
4116 || code == POSTINCREMENT_EXPR)
4117 ? lv_increment : lv_decrement));
4118 return error_mark_node;
4120 else if (TREE_READONLY (arg))
4121 readonly_warning (arg,
4122 ((code == PREINCREMENT_EXPR
4123 || code == POSTINCREMENT_EXPR)
4124 ? lv_increment : lv_decrement));
4126 /* If the argument is atomic, use the special code sequences for
4127 atomic compound assignment. */
4130 arg = stabilize_reference (arg);
4131 ret = build_atomic_assign (location, arg,
4132 ((code == PREINCREMENT_EXPR
4133 || code == POSTINCREMENT_EXPR)
4136 (FRACT_MODE_P (TYPE_MODE (argtype))
4138 : integer_one_node),
4139 (code == POSTINCREMENT_EXPR
4140 || code == POSTDECREMENT_EXPR));
4141 goto return_build_unary_op;
4144 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4145 val = boolean_increment (code, arg);
4147 val = build2 (code, TREE_TYPE (arg), arg, inc);
4148 TREE_SIDE_EFFECTS (val) = 1;
4149 if (TREE_CODE (val) != code)
4150 TREE_NO_WARNING (val) = 1;
4152 goto return_build_unary_op;
4156 /* Note that this operation never does default_conversion. */
4158 /* The operand of unary '&' must be an lvalue (which excludes
4159 expressions of type void), or, in C99, the result of a [] or
4160 unary '*' operator. */
4161 if (VOID_TYPE_P (TREE_TYPE (arg))
4162 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4163 && (TREE_CODE (arg) != INDIRECT_REF
4165 pedwarn (location, 0, "taking address of expression of type %<void%>");
4167 /* Let &* cancel out to simplify resulting code. */
4168 if (TREE_CODE (arg) == INDIRECT_REF)
4170 /* Don't let this be an lvalue. */
4171 if (lvalue_p (TREE_OPERAND (arg, 0)))
4172 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4173 ret = TREE_OPERAND (arg, 0);
4174 goto return_build_unary_op;
4177 /* For &x[y], return x+y */
4178 if (TREE_CODE (arg) == ARRAY_REF)
4180 tree op0 = TREE_OPERAND (arg, 0);
4181 if (!c_mark_addressable (op0))
4182 return error_mark_node;
4185 /* Anything not already handled and not a true memory reference
4186 or a non-lvalue array is an error. */
4187 else if (typecode != FUNCTION_TYPE && !flag
4188 && !lvalue_or_else (location, arg, lv_addressof))
4189 return error_mark_node;
4191 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4193 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4195 tree inner = build_unary_op (location, code,
4196 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4197 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4198 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4199 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4200 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4201 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4202 goto return_build_unary_op;
4205 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4206 argtype = TREE_TYPE (arg);
4208 /* If the lvalue is const or volatile, merge that into the type
4209 to which the address will point. This is only needed
4210 for function types. */
4211 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4212 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4213 && TREE_CODE (argtype) == FUNCTION_TYPE)
4215 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4216 int quals = orig_quals;
4218 if (TREE_READONLY (arg))
4219 quals |= TYPE_QUAL_CONST;
4220 if (TREE_THIS_VOLATILE (arg))
4221 quals |= TYPE_QUAL_VOLATILE;
4223 argtype = c_build_qualified_type (argtype, quals);
4226 if (!c_mark_addressable (arg))
4227 return error_mark_node;
4229 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4230 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4232 argtype = build_pointer_type (argtype);
4234 /* ??? Cope with user tricks that amount to offsetof. Delete this
4235 when we have proper support for integer constant expressions. */
4236 val = get_base_address (arg);
4237 if (val && TREE_CODE (val) == INDIRECT_REF
4238 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4240 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4241 goto return_build_unary_op;
4244 val = build1 (ADDR_EXPR, argtype, arg);
4247 goto return_build_unary_op;
4254 argtype = TREE_TYPE (arg);
4255 if (TREE_CODE (arg) == INTEGER_CST)
4256 ret = (require_constant_value
4257 ? fold_build1_initializer_loc (location, code, argtype, arg)
4258 : fold_build1_loc (location, code, argtype, arg));
4260 ret = build1 (code, argtype, arg);
4261 return_build_unary_op:
4262 gcc_assert (ret != error_mark_node);
4263 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4264 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4265 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4266 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4267 ret = note_integer_operands (ret);
4269 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4270 protected_set_expr_location (ret, location);
4274 /* Return nonzero if REF is an lvalue valid for this language.
4275 Lvalues can be assigned, unless their type has TYPE_READONLY.
4276 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4279 lvalue_p (const_tree ref)
4281 const enum tree_code code = TREE_CODE (ref);
4288 return lvalue_p (TREE_OPERAND (ref, 0));
4290 case C_MAYBE_CONST_EXPR:
4291 return lvalue_p (TREE_OPERAND (ref, 1));
4293 case COMPOUND_LITERAL_EXPR:
4299 case ARRAY_NOTATION_REF:
4304 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4305 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4308 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4315 /* Give a warning for storing in something that is read-only in GCC
4316 terms but not const in ISO C terms. */
4319 readonly_warning (tree arg, enum lvalue_use use)
4324 warning (0, "assignment of read-only location %qE", arg);
4327 warning (0, "increment of read-only location %qE", arg);
4330 warning (0, "decrement of read-only location %qE", arg);
4339 /* Return nonzero if REF is an lvalue valid for this language;
4340 otherwise, print an error message and return zero. USE says
4341 how the lvalue is being used and so selects the error message.
4342 LOCATION is the location at which any error should be reported. */
4345 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4347 int win = lvalue_p (ref);
4350 lvalue_error (loc, use);
4355 /* Mark EXP saying that we need to be able to take the
4356 address of it; it should not be allocated in a register.
4357 Returns true if successful. */
4360 c_mark_addressable (tree exp)
4365 switch (TREE_CODE (x))
4368 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4371 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4375 /* ... fall through ... */
4381 x = TREE_OPERAND (x, 0);
4384 case COMPOUND_LITERAL_EXPR:
4386 TREE_ADDRESSABLE (x) = 1;
4393 if (C_DECL_REGISTER (x)
4394 && DECL_NONLOCAL (x))
4396 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4399 ("global register variable %qD used in nested function", x);
4402 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4404 else if (C_DECL_REGISTER (x))
4406 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4407 error ("address of global register variable %qD requested", x);
4409 error ("address of register variable %qD requested", x);
4415 TREE_ADDRESSABLE (x) = 1;
4422 /* Convert EXPR to TYPE, warning about conversion problems with
4423 constants. SEMANTIC_TYPE is the type this conversion would use
4424 without excess precision. If SEMANTIC_TYPE is NULL, this function
4425 is equivalent to convert_and_check. This function is a wrapper that
4426 handles conversions that may be different than
4427 the usual ones because of excess precision. */
4430 ep_convert_and_check (location_t loc, tree type, tree expr,
4433 if (TREE_TYPE (expr) == type)
4437 return convert_and_check (loc, type, expr);
4439 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4440 && TREE_TYPE (expr) != semantic_type)
4442 /* For integers, we need to check the real conversion, not
4443 the conversion to the excess precision type. */
4444 expr = convert_and_check (loc, semantic_type, expr);
4446 /* Result type is the excess precision type, which should be
4447 large enough, so do not check. */
4448 return convert (type, expr);
4451 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4452 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4453 if folded to an integer constant then the unselected half may
4454 contain arbitrary operations not normally permitted in constant
4455 expressions. Set the location of the expression to LOC. */
4458 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4459 tree op1, tree op1_original_type, tree op2,
4460 tree op2_original_type)
4464 enum tree_code code1;
4465 enum tree_code code2;
4466 tree result_type = NULL;
4467 tree semantic_result_type = NULL;
4468 tree orig_op1 = op1, orig_op2 = op2;
4469 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4470 bool ifexp_int_operands;
4473 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4474 if (op1_int_operands)
4475 op1 = remove_c_maybe_const_expr (op1);
4476 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4477 if (op2_int_operands)
4478 op2 = remove_c_maybe_const_expr (op2);
4479 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4480 if (ifexp_int_operands)
4481 ifexp = remove_c_maybe_const_expr (ifexp);
4483 /* Promote both alternatives. */
4485 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4486 op1 = default_conversion (op1);
4487 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4488 op2 = default_conversion (op2);
4490 if (TREE_CODE (ifexp) == ERROR_MARK
4491 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4492 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4493 return error_mark_node;
4495 type1 = TREE_TYPE (op1);
4496 code1 = TREE_CODE (type1);
4497 type2 = TREE_TYPE (op2);
4498 code2 = TREE_CODE (type2);
4500 /* C90 does not permit non-lvalue arrays in conditional expressions.
4501 In C99 they will be pointers by now. */
4502 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4504 error_at (colon_loc, "non-lvalue array in conditional expression");
4505 return error_mark_node;
4508 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4509 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4510 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4511 || code1 == COMPLEX_TYPE)
4512 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4513 || code2 == COMPLEX_TYPE))
4515 semantic_result_type = c_common_type (type1, type2);
4516 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4518 op1 = TREE_OPERAND (op1, 0);
4519 type1 = TREE_TYPE (op1);
4520 gcc_assert (TREE_CODE (type1) == code1);
4522 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4524 op2 = TREE_OPERAND (op2, 0);
4525 type2 = TREE_TYPE (op2);
4526 gcc_assert (TREE_CODE (type2) == code2);
4530 if (warn_cxx_compat)
4532 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4533 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4535 if (TREE_CODE (t1) == ENUMERAL_TYPE
4536 && TREE_CODE (t2) == ENUMERAL_TYPE
4537 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4538 warning_at (colon_loc, OPT_Wc___compat,
4539 ("different enum types in conditional is "
4540 "invalid in C++: %qT vs %qT"),
4544 /* Quickly detect the usual case where op1 and op2 have the same type
4546 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4549 result_type = type1;
4551 result_type = TYPE_MAIN_VARIANT (type1);
4553 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4554 || code1 == COMPLEX_TYPE)
4555 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4556 || code2 == COMPLEX_TYPE))
4558 result_type = c_common_type (type1, type2);
4559 do_warn_double_promotion (result_type, type1, type2,
4560 "implicit conversion from %qT to %qT to "
4561 "match other result of conditional",
4564 /* If -Wsign-compare, warn here if type1 and type2 have
4565 different signedness. We'll promote the signed to unsigned
4566 and later code won't know it used to be different.
4567 Do this check on the original types, so that explicit casts
4568 will be considered, but default promotions won't. */
4569 if (c_inhibit_evaluation_warnings == 0)
4571 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4572 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4574 if (unsigned_op1 ^ unsigned_op2)
4578 /* Do not warn if the result type is signed, since the
4579 signed type will only be chosen if it can represent
4580 all the values of the unsigned type. */
4581 if (!TYPE_UNSIGNED (result_type))
4585 bool op1_maybe_const = true;
4586 bool op2_maybe_const = true;
4588 /* Do not warn if the signed quantity is an
4589 unsuffixed integer literal (or some static
4590 constant expression involving such literals) and
4591 it is non-negative. This warning requires the
4592 operands to be folded for best results, so do
4593 that folding in this case even without
4594 warn_sign_compare to avoid warning options
4595 possibly affecting code generation. */
4596 c_inhibit_evaluation_warnings
4597 += (ifexp == truthvalue_false_node);
4598 op1 = c_fully_fold (op1, require_constant_value,
4600 c_inhibit_evaluation_warnings
4601 -= (ifexp == truthvalue_false_node);
4603 c_inhibit_evaluation_warnings
4604 += (ifexp == truthvalue_true_node);
4605 op2 = c_fully_fold (op2, require_constant_value,
4607 c_inhibit_evaluation_warnings
4608 -= (ifexp == truthvalue_true_node);
4610 if (warn_sign_compare)
4613 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4615 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4618 warning_at (colon_loc, OPT_Wsign_compare,
4619 ("signed and unsigned type in "
4620 "conditional expression"));
4622 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4623 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4624 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4625 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4630 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4632 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4633 pedwarn (colon_loc, OPT_Wpedantic,
4634 "ISO C forbids conditional expr with only one void side");
4635 result_type = void_type_node;
4637 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4639 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4640 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4641 addr_space_t as_common;
4643 if (comp_target_types (colon_loc, type1, type2))
4644 result_type = common_pointer_type (type1, type2);
4645 else if (null_pointer_constant_p (orig_op1))
4646 result_type = type2;
4647 else if (null_pointer_constant_p (orig_op2))
4648 result_type = type1;
4649 else if (!addr_space_superset (as1, as2, &as_common))
4651 error_at (colon_loc, "pointers to disjoint address spaces "
4652 "used in conditional expression");
4653 return error_mark_node;
4655 else if (VOID_TYPE_P (TREE_TYPE (type1))
4656 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4658 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4659 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4660 & ~TYPE_QUALS (TREE_TYPE (type1))))
4661 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4662 "pointer to array loses qualifier "
4663 "in conditional expression");
4665 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4666 pedwarn (colon_loc, OPT_Wpedantic,
4667 "ISO C forbids conditional expr between "
4668 "%<void *%> and function pointer");
4669 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4670 TREE_TYPE (type2)));
4672 else if (VOID_TYPE_P (TREE_TYPE (type2))
4673 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4675 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4676 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4677 & ~TYPE_QUALS (TREE_TYPE (type2))))
4678 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4679 "pointer to array loses qualifier "
4680 "in conditional expression");
4682 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4683 pedwarn (colon_loc, OPT_Wpedantic,
4684 "ISO C forbids conditional expr between "
4685 "%<void *%> and function pointer");
4686 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4687 TREE_TYPE (type1)));
4689 /* Objective-C pointer comparisons are a bit more lenient. */
4690 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4691 result_type = objc_common_type (type1, type2);
4694 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4696 pedwarn (colon_loc, 0,
4697 "pointer type mismatch in conditional expression");
4698 result_type = build_pointer_type
4699 (build_qualified_type (void_type_node, qual));
4702 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4704 if (!null_pointer_constant_p (orig_op2))
4705 pedwarn (colon_loc, 0,
4706 "pointer/integer type mismatch in conditional expression");
4709 op2 = null_pointer_node;
4711 result_type = type1;
4713 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4715 if (!null_pointer_constant_p (orig_op1))
4716 pedwarn (colon_loc, 0,
4717 "pointer/integer type mismatch in conditional expression");
4720 op1 = null_pointer_node;
4722 result_type = type2;
4727 if (flag_cond_mismatch)
4728 result_type = void_type_node;
4731 error_at (colon_loc, "type mismatch in conditional expression");
4732 return error_mark_node;
4736 /* Merge const and volatile flags of the incoming types. */
4738 = build_type_variant (result_type,
4739 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4740 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4742 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4743 semantic_result_type);
4744 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4745 semantic_result_type);
4747 if (ifexp_bcp && ifexp == truthvalue_true_node)
4749 op2_int_operands = true;
4750 op1 = c_fully_fold (op1, require_constant_value, NULL);
4752 if (ifexp_bcp && ifexp == truthvalue_false_node)
4754 op1_int_operands = true;
4755 op2 = c_fully_fold (op2, require_constant_value, NULL);
4757 int_const = int_operands = (ifexp_int_operands
4759 && op2_int_operands);
4762 int_const = ((ifexp == truthvalue_true_node
4763 && TREE_CODE (orig_op1) == INTEGER_CST
4764 && !TREE_OVERFLOW (orig_op1))
4765 || (ifexp == truthvalue_false_node
4766 && TREE_CODE (orig_op2) == INTEGER_CST
4767 && !TREE_OVERFLOW (orig_op2)));
4769 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4770 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4775 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4776 nested inside of the expression. */
4777 op1 = c_fully_fold (op1, false, NULL);
4778 op2 = c_fully_fold (op2, false, NULL);
4780 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4782 ret = note_integer_operands (ret);
4784 if (semantic_result_type)
4785 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4787 protected_set_expr_location (ret, colon_loc);
4791 /* Return a compound expression that performs two expressions and
4792 returns the value of the second of them.
4794 LOC is the location of the COMPOUND_EXPR. */
4797 build_compound_expr (location_t loc, tree expr1, tree expr2)
4799 bool expr1_int_operands, expr2_int_operands;
4800 tree eptype = NULL_TREE;
4804 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4805 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4808 "spawned function call cannot be part of a comma expression");
4809 return error_mark_node;
4811 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4812 if (expr1_int_operands)
4813 expr1 = remove_c_maybe_const_expr (expr1);
4814 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4815 if (expr2_int_operands)
4816 expr2 = remove_c_maybe_const_expr (expr2);
4818 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4819 expr1 = TREE_OPERAND (expr1, 0);
4820 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4822 eptype = TREE_TYPE (expr2);
4823 expr2 = TREE_OPERAND (expr2, 0);
4826 if (!TREE_SIDE_EFFECTS (expr1))
4828 /* The left-hand operand of a comma expression is like an expression
4829 statement: with -Wunused, we should warn if it doesn't have
4830 any side-effects, unless it was explicitly cast to (void). */
4831 if (warn_unused_value)
4833 if (VOID_TYPE_P (TREE_TYPE (expr1))
4834 && CONVERT_EXPR_P (expr1))
4836 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4837 && TREE_CODE (expr1) == COMPOUND_EXPR
4838 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4839 ; /* (void) a, (void) b, c */
4841 warning_at (loc, OPT_Wunused_value,
4842 "left-hand operand of comma expression has no effect");
4845 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4846 && warn_unused_value)
4849 location_t cloc = loc;
4850 while (TREE_CODE (r) == COMPOUND_EXPR)
4852 if (EXPR_HAS_LOCATION (r))
4853 cloc = EXPR_LOCATION (r);
4854 r = TREE_OPERAND (r, 1);
4856 if (!TREE_SIDE_EFFECTS (r)
4857 && !VOID_TYPE_P (TREE_TYPE (r))
4858 && !CONVERT_EXPR_P (r))
4859 warning_at (cloc, OPT_Wunused_value,
4860 "right-hand operand of comma expression has no effect");
4863 /* With -Wunused, we should also warn if the left-hand operand does have
4864 side-effects, but computes a value which is not used. For example, in
4865 `foo() + bar(), baz()' the result of the `+' operator is not used,
4866 so we should issue a warning. */
4867 else if (warn_unused_value)
4868 warn_if_unused_value (expr1, loc);
4870 if (expr2 == error_mark_node)
4871 return error_mark_node;
4873 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4876 && expr1_int_operands
4877 && expr2_int_operands)
4878 ret = note_integer_operands (ret);
4881 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4883 protected_set_expr_location (ret, loc);
4887 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4888 which we are casting. OTYPE is the type of the expression being
4889 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4890 of the cast. -Wcast-qual appeared on the command line. Named
4891 address space qualifiers are not handled here, because they result
4892 in different warnings. */
4895 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4897 tree in_type = type;
4898 tree in_otype = otype;
4903 /* Check that the qualifiers on IN_TYPE are a superset of the
4904 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4905 nodes is uninteresting and we stop as soon as we hit a
4906 non-POINTER_TYPE node on either type. */
4909 in_otype = TREE_TYPE (in_otype);
4910 in_type = TREE_TYPE (in_type);
4912 /* GNU C allows cv-qualified function types. 'const' means the
4913 function is very pure, 'volatile' means it can't return. We
4914 need to warn when such qualifiers are added, not when they're
4916 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4917 && TREE_CODE (in_type) == FUNCTION_TYPE)
4918 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4919 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4921 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4922 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4924 while (TREE_CODE (in_type) == POINTER_TYPE
4925 && TREE_CODE (in_otype) == POINTER_TYPE);
4928 warning_at (loc, OPT_Wcast_qual,
4929 "cast adds %q#v qualifier to function type", added);
4932 /* There are qualifiers present in IN_OTYPE that are not present
4934 warning_at (loc, OPT_Wcast_qual,
4935 "cast discards %qv qualifier from pointer target type",
4938 if (added || discarded)
4941 /* A cast from **T to const **T is unsafe, because it can cause a
4942 const value to be changed with no additional warning. We only
4943 issue this warning if T is the same on both sides, and we only
4944 issue the warning if there are the same number of pointers on
4945 both sides, as otherwise the cast is clearly unsafe anyhow. A
4946 cast is unsafe when a qualifier is added at one level and const
4947 is not present at all outer levels.
4949 To issue this warning, we check at each level whether the cast
4950 adds new qualifiers not already seen. We don't need to special
4951 case function types, as they won't have the same
4952 TYPE_MAIN_VARIANT. */
4954 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4956 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4961 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4964 in_type = TREE_TYPE (in_type);
4965 in_otype = TREE_TYPE (in_otype);
4966 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4969 warning_at (loc, OPT_Wcast_qual,
4970 "to be safe all intermediate pointers in cast from "
4971 "%qT to %qT must be %<const%> qualified",
4976 is_const = TYPE_READONLY (in_type);
4978 while (TREE_CODE (in_type) == POINTER_TYPE);
4981 /* Build an expression representing a cast to type TYPE of expression EXPR.
4982 LOC is the location of the cast-- typically the open paren of the cast. */
4985 build_c_cast (location_t loc, tree type, tree expr)
4989 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4990 expr = TREE_OPERAND (expr, 0);
4994 if (type == error_mark_node || expr == error_mark_node)
4995 return error_mark_node;
4997 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4998 only in <protocol> qualifications. But when constructing cast expressions,
4999 the protocols do matter and must be kept around. */
5000 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5001 return build1 (NOP_EXPR, type, expr);
5003 type = TYPE_MAIN_VARIANT (type);
5005 if (TREE_CODE (type) == ARRAY_TYPE)
5007 error_at (loc, "cast specifies array type");
5008 return error_mark_node;
5011 if (TREE_CODE (type) == FUNCTION_TYPE)
5013 error_at (loc, "cast specifies function type");
5014 return error_mark_node;
5017 if (!VOID_TYPE_P (type))
5019 value = require_complete_type (value);
5020 if (value == error_mark_node)
5021 return error_mark_node;
5024 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5026 if (TREE_CODE (type) == RECORD_TYPE
5027 || TREE_CODE (type) == UNION_TYPE)
5028 pedwarn (loc, OPT_Wpedantic,
5029 "ISO C forbids casting nonscalar to the same type");
5031 /* Convert to remove any qualifiers from VALUE's type. */
5032 value = convert (type, value);
5034 else if (TREE_CODE (type) == UNION_TYPE)
5038 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5039 if (TREE_TYPE (field) != error_mark_node
5040 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5041 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5047 bool maybe_const = true;
5049 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5050 t = c_fully_fold (value, false, &maybe_const);
5051 t = build_constructor_single (type, field, t);
5053 t = c_wrap_maybe_const (t, true);
5054 t = digest_init (loc, type, t,
5055 NULL_TREE, false, true, 0);
5056 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5059 error_at (loc, "cast to union type from type not present in union");
5060 return error_mark_node;
5066 if (type == void_type_node)
5068 tree t = build1 (CONVERT_EXPR, type, value);
5069 SET_EXPR_LOCATION (t, loc);
5073 otype = TREE_TYPE (value);
5075 /* Optionally warn about potentially worrisome casts. */
5077 && TREE_CODE (type) == POINTER_TYPE
5078 && TREE_CODE (otype) == POINTER_TYPE)
5079 handle_warn_cast_qual (loc, type, otype);
5081 /* Warn about conversions between pointers to disjoint
5083 if (TREE_CODE (type) == POINTER_TYPE
5084 && TREE_CODE (otype) == POINTER_TYPE
5085 && !null_pointer_constant_p (value))
5087 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5088 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5089 addr_space_t as_common;
5091 if (!addr_space_superset (as_to, as_from, &as_common))
5093 if (ADDR_SPACE_GENERIC_P (as_from))
5094 warning_at (loc, 0, "cast to %s address space pointer "
5095 "from disjoint generic address space pointer",
5096 c_addr_space_name (as_to));
5098 else if (ADDR_SPACE_GENERIC_P (as_to))
5099 warning_at (loc, 0, "cast to generic address space pointer "
5100 "from disjoint %s address space pointer",
5101 c_addr_space_name (as_from));
5104 warning_at (loc, 0, "cast to %s address space pointer "
5105 "from disjoint %s address space pointer",
5106 c_addr_space_name (as_to),
5107 c_addr_space_name (as_from));
5111 /* Warn about possible alignment problems. */
5112 if (STRICT_ALIGNMENT
5113 && TREE_CODE (type) == POINTER_TYPE
5114 && TREE_CODE (otype) == POINTER_TYPE
5115 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5116 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5117 /* Don't warn about opaque types, where the actual alignment
5118 restriction is unknown. */
5119 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5120 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5121 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5122 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5123 warning_at (loc, OPT_Wcast_align,
5124 "cast increases required alignment of target type");
5126 if (TREE_CODE (type) == INTEGER_TYPE
5127 && TREE_CODE (otype) == POINTER_TYPE
5128 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5129 /* Unlike conversion of integers to pointers, where the
5130 warning is disabled for converting constants because
5131 of cases such as SIG_*, warn about converting constant
5132 pointers to integers. In some cases it may cause unwanted
5133 sign extension, and a warning is appropriate. */
5134 warning_at (loc, OPT_Wpointer_to_int_cast,
5135 "cast from pointer to integer of different size");
5137 if (TREE_CODE (value) == CALL_EXPR
5138 && TREE_CODE (type) != TREE_CODE (otype))
5139 warning_at (loc, OPT_Wbad_function_cast,
5140 "cast from function call of type %qT "
5141 "to non-matching type %qT", otype, type);
5143 if (TREE_CODE (type) == POINTER_TYPE
5144 && TREE_CODE (otype) == INTEGER_TYPE
5145 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5146 /* Don't warn about converting any constant. */
5147 && !TREE_CONSTANT (value))
5149 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5150 "of different size");
5152 if (warn_strict_aliasing <= 2)
5153 strict_aliasing_warning (otype, type, expr);
5155 /* If pedantic, warn for conversions between function and object
5156 pointer types, except for converting a null pointer constant
5157 to function pointer type. */
5159 && TREE_CODE (type) == POINTER_TYPE
5160 && TREE_CODE (otype) == POINTER_TYPE
5161 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5162 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5163 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5164 "conversion of function pointer to object pointer type");
5167 && TREE_CODE (type) == POINTER_TYPE
5168 && TREE_CODE (otype) == POINTER_TYPE
5169 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5170 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5171 && !null_pointer_constant_p (value))
5172 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5173 "conversion of object pointer to function pointer type");
5176 value = convert (type, value);
5178 /* Ignore any integer overflow caused by the cast. */
5179 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5181 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5183 if (!TREE_OVERFLOW (value))
5185 /* Avoid clobbering a shared constant. */
5186 value = copy_node (value);
5187 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5190 else if (TREE_OVERFLOW (value))
5191 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5192 value = wide_int_to_tree (TREE_TYPE (value), value);
5196 /* Don't let a cast be an lvalue. */
5198 value = non_lvalue_loc (loc, value);
5200 /* Don't allow the results of casting to floating-point or complex
5201 types be confused with actual constants, or casts involving
5202 integer and pointer types other than direct integer-to-integer
5203 and integer-to-pointer be confused with integer constant
5204 expressions and null pointer constants. */
5205 if (TREE_CODE (value) == REAL_CST
5206 || TREE_CODE (value) == COMPLEX_CST
5207 || (TREE_CODE (value) == INTEGER_CST
5208 && !((TREE_CODE (expr) == INTEGER_CST
5209 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5210 || TREE_CODE (expr) == REAL_CST
5211 || TREE_CODE (expr) == COMPLEX_CST)))
5212 value = build1 (NOP_EXPR, type, value);
5214 if (CAN_HAVE_LOCATION_P (value))
5215 SET_EXPR_LOCATION (value, loc);
5219 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5220 location of the open paren of the cast, or the position of the cast
5223 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5226 tree type_expr = NULL_TREE;
5227 bool type_expr_const = true;
5229 int saved_wsp = warn_strict_prototypes;
5231 /* This avoids warnings about unprototyped casts on
5232 integers. E.g. "#define SIG_DFL (void(*)())0". */
5233 if (TREE_CODE (expr) == INTEGER_CST)
5234 warn_strict_prototypes = 0;
5235 type = groktypename (type_name, &type_expr, &type_expr_const);
5236 warn_strict_prototypes = saved_wsp;
5238 ret = build_c_cast (loc, type, expr);
5241 bool inner_expr_const = true;
5242 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5243 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5244 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5245 && inner_expr_const);
5246 SET_EXPR_LOCATION (ret, loc);
5249 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5250 SET_EXPR_LOCATION (ret, loc);
5252 /* C++ does not permits types to be defined in a cast, but it
5253 allows references to incomplete types. */
5254 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5255 warning_at (loc, OPT_Wc___compat,
5256 "defining a type in a cast is invalid in C++");
5261 /* Build an assignment expression of lvalue LHS from value RHS.
5262 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5263 may differ from TREE_TYPE (LHS) for an enum bitfield.
5264 MODIFYCODE is the code for a binary operator that we use
5265 to combine the old value of LHS with RHS to get the new value.
5266 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5267 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5268 which may differ from TREE_TYPE (RHS) for an enum value.
5270 LOCATION is the location of the MODIFYCODE operator.
5271 RHS_LOC is the location of the RHS. */
5274 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5275 enum tree_code modifycode,
5276 location_t rhs_loc, tree rhs, tree rhs_origtype)
5280 tree rhseval = NULL_TREE;
5281 tree rhs_semantic_type = NULL_TREE;
5282 tree lhstype = TREE_TYPE (lhs);
5283 tree olhstype = lhstype;
5287 /* Types that aren't fully specified cannot be used in assignments. */
5288 lhs = require_complete_type (lhs);
5290 /* Avoid duplicate error messages from operands that had errors. */
5291 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5292 return error_mark_node;
5294 /* Ensure an error for assigning a non-lvalue array to an array in
5296 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5298 error_at (location, "assignment to expression with array type");
5299 return error_mark_node;
5302 /* For ObjC properties, defer this check. */
5303 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5304 return error_mark_node;
5306 is_atomic_op = really_atomic_lvalue (lhs);
5308 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5310 rhs_semantic_type = TREE_TYPE (rhs);
5311 rhs = TREE_OPERAND (rhs, 0);
5316 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5318 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5319 lhs_origtype, modifycode, rhs_loc, rhs,
5321 if (inner == error_mark_node)
5322 return error_mark_node;
5323 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5324 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5325 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5326 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5327 protected_set_expr_location (result, location);
5331 /* If a binary op has been requested, combine the old LHS value with the RHS
5332 producing the value we should actually store into the LHS. */
5334 if (modifycode != NOP_EXPR)
5336 lhs = c_fully_fold (lhs, false, NULL);
5337 lhs = stabilize_reference (lhs);
5339 /* Construct the RHS for any non-atomic compound assignemnt. */
5342 /* If in LHS op= RHS the RHS has side-effects, ensure they
5343 are preevaluated before the rest of the assignment expression's
5344 side-effects, because RHS could contain e.g. function calls
5346 if (TREE_SIDE_EFFECTS (rhs))
5348 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5351 newrhs = build_binary_op (location,
5352 modifycode, lhs, newrhs, 1);
5354 /* The original type of the right hand side is no longer
5356 rhs_origtype = NULL_TREE;
5360 if (c_dialect_objc ())
5362 /* Check if we are modifying an Objective-C property reference;
5363 if so, we need to generate setter calls. */
5364 result = objc_maybe_build_modify_expr (lhs, newrhs);
5368 /* Else, do the check that we postponed for Objective-C. */
5369 if (!lvalue_or_else (location, lhs, lv_assign))
5370 return error_mark_node;
5373 /* Give an error for storing in something that is 'const'. */
5375 if (TYPE_READONLY (lhstype)
5376 || ((TREE_CODE (lhstype) == RECORD_TYPE
5377 || TREE_CODE (lhstype) == UNION_TYPE)
5378 && C_TYPE_FIELDS_READONLY (lhstype)))
5380 readonly_error (location, lhs, lv_assign);
5381 return error_mark_node;
5383 else if (TREE_READONLY (lhs))
5384 readonly_warning (lhs, lv_assign);
5386 /* If storing into a structure or union member,
5387 it has probably been given type `int'.
5388 Compute the type that would go with
5389 the actual amount of storage the member occupies. */
5391 if (TREE_CODE (lhs) == COMPONENT_REF
5392 && (TREE_CODE (lhstype) == INTEGER_TYPE
5393 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5394 || TREE_CODE (lhstype) == REAL_TYPE
5395 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5396 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5398 /* If storing in a field that is in actuality a short or narrower than one,
5399 we must store in the field in its actual type. */
5401 if (lhstype != TREE_TYPE (lhs))
5403 lhs = copy_node (lhs);
5404 TREE_TYPE (lhs) = lhstype;
5407 /* Issue -Wc++-compat warnings about an assignment to an enum type
5408 when LHS does not have its original type. This happens for,
5409 e.g., an enum bitfield in a struct. */
5411 && lhs_origtype != NULL_TREE
5412 && lhs_origtype != lhstype
5413 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5415 tree checktype = (rhs_origtype != NULL_TREE
5418 if (checktype != error_mark_node
5419 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5420 || (is_atomic_op && modifycode != NOP_EXPR)))
5421 warning_at (location, OPT_Wc___compat,
5422 "enum conversion in assignment is invalid in C++");
5425 /* If the lhs is atomic, remove that qualifier. */
5428 lhstype = build_qualified_type (lhstype,
5429 (TYPE_QUALS (lhstype)
5430 & ~TYPE_QUAL_ATOMIC));
5431 olhstype = build_qualified_type (olhstype,
5432 (TYPE_QUALS (lhstype)
5433 & ~TYPE_QUAL_ATOMIC));
5436 /* Convert new value to destination type. Fold it first, then
5437 restore any excess precision information, for the sake of
5438 conversion warnings. */
5440 if (!(is_atomic_op && modifycode != NOP_EXPR))
5442 npc = null_pointer_constant_p (newrhs);
5443 newrhs = c_fully_fold (newrhs, false, NULL);
5444 if (rhs_semantic_type)
5445 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5446 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5447 rhs_origtype, ic_assign, npc,
5448 NULL_TREE, NULL_TREE, 0);
5449 if (TREE_CODE (newrhs) == ERROR_MARK)
5450 return error_mark_node;
5453 /* Emit ObjC write barrier, if necessary. */
5454 if (c_dialect_objc () && flag_objc_gc)
5456 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5459 protected_set_expr_location (result, location);
5464 /* Scan operands. */
5467 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5470 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5471 TREE_SIDE_EFFECTS (result) = 1;
5472 protected_set_expr_location (result, location);
5475 /* If we got the LHS in a different type for storing in,
5476 convert the result back to the nominal type of LHS
5477 so that the value we return always has the same type
5478 as the LHS argument. */
5480 if (olhstype == TREE_TYPE (result))
5483 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5484 rhs_origtype, ic_assign, false, NULL_TREE,
5486 protected_set_expr_location (result, location);
5490 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5494 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5495 This is used to implement -fplan9-extensions. */
5498 find_anonymous_field_with_type (tree struct_type, tree type)
5503 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5504 || TREE_CODE (struct_type) == UNION_TYPE);
5506 for (field = TYPE_FIELDS (struct_type);
5508 field = TREE_CHAIN (field))
5510 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5511 ? c_build_qualified_type (TREE_TYPE (field),
5513 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5514 if (DECL_NAME (field) == NULL
5515 && comptypes (type, fieldtype))
5521 else if (DECL_NAME (field) == NULL
5522 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5523 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5524 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5534 /* RHS is an expression whose type is pointer to struct. If there is
5535 an anonymous field in RHS with type TYPE, then return a pointer to
5536 that field in RHS. This is used with -fplan9-extensions. This
5537 returns NULL if no conversion could be found. */
5540 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5542 tree rhs_struct_type, lhs_main_type;
5543 tree field, found_field;
5544 bool found_sub_field;
5547 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5548 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5549 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5550 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5552 gcc_assert (POINTER_TYPE_P (type));
5553 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5554 ? c_build_qualified_type (TREE_TYPE (type),
5556 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5558 found_field = NULL_TREE;
5559 found_sub_field = false;
5560 for (field = TYPE_FIELDS (rhs_struct_type);
5562 field = TREE_CHAIN (field))
5564 if (DECL_NAME (field) != NULL_TREE
5565 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5566 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5568 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5569 ? c_build_qualified_type (TREE_TYPE (field),
5571 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5572 if (comptypes (lhs_main_type, fieldtype))
5574 if (found_field != NULL_TREE)
5576 found_field = field;
5578 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5581 if (found_field != NULL_TREE)
5583 found_field = field;
5584 found_sub_field = true;
5588 if (found_field == NULL_TREE)
5591 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5592 build_fold_indirect_ref (rhs), found_field,
5594 ret = build_fold_addr_expr_loc (location, ret);
5596 if (found_sub_field)
5598 ret = convert_to_anonymous_field (location, type, ret);
5599 gcc_assert (ret != NULL_TREE);
5605 /* Issue an error message for a bad initializer component.
5606 GMSGID identifies the message.
5607 The component name is taken from the spelling stack. */
5610 error_init (location_t loc, const char *gmsgid)
5614 /* The gmsgid may be a format string with %< and %>. */
5615 error_at (loc, gmsgid);
5616 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5618 inform (loc, "(near initialization for %qs)", ofwhat);
5621 /* Issue a pedantic warning for a bad initializer component. OPT is
5622 the option OPT_* (from options.h) controlling this warning or 0 if
5623 it is unconditionally given. GMSGID identifies the message. The
5624 component name is taken from the spelling stack. */
5627 pedwarn_init (location_t location, int opt, const char *gmsgid)
5632 /* The gmsgid may be a format string with %< and %>. */
5633 warned = pedwarn (location, opt, gmsgid);
5634 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5635 if (*ofwhat && warned)
5636 inform (location, "(near initialization for %qs)", ofwhat);
5639 /* Issue a warning for a bad initializer component.
5641 OPT is the OPT_W* value corresponding to the warning option that
5642 controls this warning. GMSGID identifies the message. The
5643 component name is taken from the spelling stack. */
5646 warning_init (location_t loc, int opt, const char *gmsgid)
5651 /* The gmsgid may be a format string with %< and %>. */
5652 warned = warning_at (loc, opt, gmsgid);
5653 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5654 if (*ofwhat && warned)
5655 inform (loc, "(near initialization for %qs)", ofwhat);
5658 /* If TYPE is an array type and EXPR is a parenthesized string
5659 constant, warn if pedantic that EXPR is being used to initialize an
5660 object of type TYPE. */
5663 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5666 && TREE_CODE (type) == ARRAY_TYPE
5667 && TREE_CODE (expr.value) == STRING_CST
5668 && expr.original_code != STRING_CST)
5669 pedwarn_init (loc, OPT_Wpedantic,
5670 "array initialized from parenthesized string constant");
5673 /* Convert value RHS to type TYPE as preparation for an assignment to
5674 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5675 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5676 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5677 constant before any folding.
5678 The real work of conversion is done by `convert'.
5679 The purpose of this function is to generate error messages
5680 for assignments that are not allowed in C.
5681 ERRTYPE says whether it is argument passing, assignment,
5682 initialization or return.
5684 LOCATION is the location of the assignment, EXPR_LOC is the location of
5685 the RHS or, for a function, location of an argument.
5686 FUNCTION is a tree for the function being called.
5687 PARMNUM is the number of the argument, for printing in error messages. */
5690 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5691 tree rhs, tree origtype, enum impl_conv errtype,
5692 bool null_pointer_constant, tree fundecl,
5693 tree function, int parmnum)
5695 enum tree_code codel = TREE_CODE (type);
5696 tree orig_rhs = rhs;
5698 enum tree_code coder;
5699 tree rname = NULL_TREE;
5700 bool objc_ok = false;
5702 if (errtype == ic_argpass)
5705 /* Change pointer to function to the function itself for
5707 if (TREE_CODE (function) == ADDR_EXPR
5708 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5709 function = TREE_OPERAND (function, 0);
5711 /* Handle an ObjC selector specially for diagnostics. */
5712 selector = objc_message_selector ();
5714 if (selector && parmnum > 2)
5721 /* This macro is used to emit diagnostics to ensure that all format
5722 strings are complete sentences, visible to gettext and checked at
5724 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5729 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5730 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5731 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5732 "expected %qT but argument is of type %qT", \
5736 pedwarn (LOCATION, OPT, AS); \
5739 pedwarn_init (LOCATION, OPT, IN); \
5742 pedwarn (LOCATION, OPT, RE); \
5745 gcc_unreachable (); \
5749 /* This macro is used to emit diagnostics to ensure that all format
5750 strings are complete sentences, visible to gettext and checked at
5751 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5752 extra parameter to enumerate qualifiers. */
5753 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5758 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5759 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5760 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5761 "expected %qT but argument is of type %qT", \
5765 pedwarn (LOCATION, OPT, AS, QUALS); \
5768 pedwarn (LOCATION, OPT, IN, QUALS); \
5771 pedwarn (LOCATION, OPT, RE, QUALS); \
5774 gcc_unreachable (); \
5778 /* This macro is used to emit diagnostics to ensure that all format
5779 strings are complete sentences, visible to gettext and checked at
5780 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5781 warning_at instead of pedwarn. */
5782 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5787 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5788 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5789 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5790 "expected %qT but argument is of type %qT", \
5794 warning_at (LOCATION, OPT, AS, QUALS); \
5797 warning_at (LOCATION, OPT, IN, QUALS); \
5800 warning_at (LOCATION, OPT, RE, QUALS); \
5803 gcc_unreachable (); \
5807 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5808 rhs = TREE_OPERAND (rhs, 0);
5810 rhstype = TREE_TYPE (rhs);
5811 coder = TREE_CODE (rhstype);
5813 if (coder == ERROR_MARK)
5814 return error_mark_node;
5816 if (c_dialect_objc ())
5839 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5842 if (warn_cxx_compat)
5844 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5845 if (checktype != error_mark_node
5846 && TREE_CODE (type) == ENUMERAL_TYPE
5847 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5849 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5850 G_("enum conversion when passing argument "
5851 "%d of %qE is invalid in C++"),
5852 G_("enum conversion in assignment is "
5854 G_("enum conversion in initialization is "
5856 G_("enum conversion in return is "
5861 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5864 if (coder == VOID_TYPE)
5866 /* Except for passing an argument to an unprototyped function,
5867 this is a constraint violation. When passing an argument to
5868 an unprototyped function, it is compile-time undefined;
5869 making it a constraint in that case was rejected in
5871 error_at (location, "void value not ignored as it ought to be");
5872 return error_mark_node;
5874 rhs = require_complete_type (rhs);
5875 if (rhs == error_mark_node)
5876 return error_mark_node;
5877 /* A non-reference type can convert to a reference. This handles
5878 va_start, va_copy and possibly port built-ins. */
5879 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5881 if (!lvalue_p (rhs))
5883 error_at (location, "cannot pass rvalue to reference parameter");
5884 return error_mark_node;
5886 if (!c_mark_addressable (rhs))
5887 return error_mark_node;
5888 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5889 SET_EXPR_LOCATION (rhs, location);
5891 rhs = convert_for_assignment (location, expr_loc,
5892 build_pointer_type (TREE_TYPE (type)),
5893 rhs, origtype, errtype,
5894 null_pointer_constant, fundecl, function,
5896 if (rhs == error_mark_node)
5897 return error_mark_node;
5899 rhs = build1 (NOP_EXPR, type, rhs);
5900 SET_EXPR_LOCATION (rhs, location);
5903 /* Some types can interconvert without explicit casts. */
5904 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5905 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5906 return convert (type, rhs);
5907 /* Arithmetic types all interconvert, and enum is treated like int. */
5908 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5909 || codel == FIXED_POINT_TYPE
5910 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5911 || codel == BOOLEAN_TYPE)
5912 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5913 || coder == FIXED_POINT_TYPE
5914 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5915 || coder == BOOLEAN_TYPE))
5918 bool save = in_late_binary_op;
5919 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5920 || (coder == REAL_TYPE
5921 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5922 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5923 in_late_binary_op = true;
5924 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5925 ? expr_loc : location, type, orig_rhs);
5926 in_late_binary_op = save;
5930 /* Aggregates in different TUs might need conversion. */
5931 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5933 && comptypes (type, rhstype))
5934 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5935 ? expr_loc : location, type, rhs);
5937 /* Conversion to a transparent union or record from its member types.
5938 This applies only to function arguments. */
5939 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5940 && TYPE_TRANSPARENT_AGGR (type))
5941 && errtype == ic_argpass)
5943 tree memb, marginal_memb = NULL_TREE;
5945 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5947 tree memb_type = TREE_TYPE (memb);
5949 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5950 TYPE_MAIN_VARIANT (rhstype)))
5953 if (TREE_CODE (memb_type) != POINTER_TYPE)
5956 if (coder == POINTER_TYPE)
5958 tree ttl = TREE_TYPE (memb_type);
5959 tree ttr = TREE_TYPE (rhstype);
5961 /* Any non-function converts to a [const][volatile] void *
5962 and vice versa; otherwise, targets must be the same.
5963 Meanwhile, the lhs target must have all the qualifiers of
5965 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5966 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5967 || comp_target_types (location, memb_type, rhstype))
5969 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5970 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5971 /* If this type won't generate any warnings, use it. */
5972 if (lquals == rquals
5973 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5974 && TREE_CODE (ttl) == FUNCTION_TYPE)
5975 ? ((lquals | rquals) == rquals)
5976 : ((lquals | rquals) == lquals)))
5979 /* Keep looking for a better type, but remember this one. */
5981 marginal_memb = memb;
5985 /* Can convert integer zero to any pointer type. */
5986 if (null_pointer_constant)
5988 rhs = null_pointer_node;
5993 if (memb || marginal_memb)
5997 /* We have only a marginally acceptable member type;
5998 it needs a warning. */
5999 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6000 tree ttr = TREE_TYPE (rhstype);
6002 /* Const and volatile mean something different for function
6003 types, so the usual warnings are not appropriate. */
6004 if (TREE_CODE (ttr) == FUNCTION_TYPE
6005 && TREE_CODE (ttl) == FUNCTION_TYPE)
6007 /* Because const and volatile on functions are
6008 restrictions that say the function will not do
6009 certain things, it is okay to use a const or volatile
6010 function where an ordinary one is wanted, but not
6012 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6013 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6014 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6015 OPT_Wdiscarded_qualifiers,
6016 G_("passing argument %d of %qE "
6017 "makes %q#v qualified function "
6018 "pointer from unqualified"),
6019 G_("assignment makes %q#v qualified "
6020 "function pointer from "
6022 G_("initialization makes %q#v qualified "
6023 "function pointer from "
6025 G_("return makes %q#v qualified function "
6026 "pointer from unqualified"),
6027 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6029 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6030 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6031 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6032 OPT_Wdiscarded_qualifiers,
6033 G_("passing argument %d of %qE discards "
6034 "%qv qualifier from pointer target type"),
6035 G_("assignment discards %qv qualifier "
6036 "from pointer target type"),
6037 G_("initialization discards %qv qualifier "
6038 "from pointer target type"),
6039 G_("return discards %qv qualifier from "
6040 "pointer target type"),
6041 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6043 memb = marginal_memb;
6046 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6047 pedwarn (location, OPT_Wpedantic,
6048 "ISO C prohibits argument conversion to union type");
6050 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6051 return build_constructor_single (type, memb, rhs);
6055 /* Conversions among pointers */
6056 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6057 && (coder == codel))
6059 tree ttl = TREE_TYPE (type);
6060 tree ttr = TREE_TYPE (rhstype);
6063 bool is_opaque_pointer;
6064 int target_cmp = 0; /* Cache comp_target_types () result. */
6068 if (TREE_CODE (mvl) != ARRAY_TYPE)
6069 mvl = (TYPE_ATOMIC (mvl)
6070 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6072 : TYPE_MAIN_VARIANT (mvl));
6073 if (TREE_CODE (mvr) != ARRAY_TYPE)
6074 mvr = (TYPE_ATOMIC (mvr)
6075 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6077 : TYPE_MAIN_VARIANT (mvr));
6078 /* Opaque pointers are treated like void pointers. */
6079 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6081 /* The Plan 9 compiler permits a pointer to a struct to be
6082 automatically converted into a pointer to an anonymous field
6083 within the struct. */
6084 if (flag_plan9_extensions
6085 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6086 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6089 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6090 if (new_rhs != NULL_TREE)
6093 rhstype = TREE_TYPE (rhs);
6094 coder = TREE_CODE (rhstype);
6095 ttr = TREE_TYPE (rhstype);
6096 mvr = TYPE_MAIN_VARIANT (ttr);
6100 /* C++ does not allow the implicit conversion void* -> T*. However,
6101 for the purpose of reducing the number of false positives, we
6102 tolerate the special case of
6106 where NULL is typically defined in C to be '(void *) 0'. */
6107 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6108 warning_at (errtype == ic_argpass ? expr_loc : location,
6110 "request for implicit conversion "
6111 "from %qT to %qT not permitted in C++", rhstype, type);
6113 /* See if the pointers point to incompatible address spaces. */
6114 asl = TYPE_ADDR_SPACE (ttl);
6115 asr = TYPE_ADDR_SPACE (ttr);
6116 if (!null_pointer_constant_p (rhs)
6117 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6122 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6123 "non-enclosed address space", parmnum, rname);
6126 error_at (location, "assignment from pointer to "
6127 "non-enclosed address space");
6130 error_at (location, "initialization from pointer to "
6131 "non-enclosed address space");
6134 error_at (location, "return from pointer to "
6135 "non-enclosed address space");
6140 return error_mark_node;
6143 /* Check if the right-hand side has a format attribute but the
6144 left-hand side doesn't. */
6145 if (warn_suggest_attribute_format
6146 && check_missing_format_attribute (type, rhstype))
6151 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6152 "argument %d of %qE might be "
6153 "a candidate for a format attribute",
6157 warning_at (location, OPT_Wsuggest_attribute_format,
6158 "assignment left-hand side might be "
6159 "a candidate for a format attribute");
6162 warning_at (location, OPT_Wsuggest_attribute_format,
6163 "initialization left-hand side might be "
6164 "a candidate for a format attribute");
6167 warning_at (location, OPT_Wsuggest_attribute_format,
6168 "return type might be "
6169 "a candidate for a format attribute");
6176 /* Any non-function converts to a [const][volatile] void *
6177 and vice versa; otherwise, targets must be the same.
6178 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6179 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6180 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6181 || (target_cmp = comp_target_types (location, type, rhstype))
6182 || is_opaque_pointer
6183 || ((c_common_unsigned_type (mvl)
6184 == c_common_unsigned_type (mvr))
6185 && (c_common_signed_type (mvl)
6186 == c_common_signed_type (mvr))
6187 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6189 /* Warn about loss of qualifers from pointers to arrays with
6190 qualifiers on the element type. */
6191 if (TREE_CODE (ttr) == ARRAY_TYPE)
6193 ttr = strip_array_types (ttr);
6194 ttl = strip_array_types (ttl);
6196 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6197 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6198 WARNING_FOR_QUALIFIERS (location, expr_loc,
6199 OPT_Wdiscarded_array_qualifiers,
6200 G_("passing argument %d of %qE discards "
6201 "%qv qualifier from pointer target type"),
6202 G_("assignment discards %qv qualifier "
6203 "from pointer target type"),
6204 G_("initialization discards %qv qualifier "
6205 "from pointer target type"),
6206 G_("return discards %qv qualifier from "
6207 "pointer target type"),
6208 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6211 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6214 && !null_pointer_constant
6215 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6216 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6217 G_("ISO C forbids passing argument %d of "
6218 "%qE between function pointer "
6220 G_("ISO C forbids assignment between "
6221 "function pointer and %<void *%>"),
6222 G_("ISO C forbids initialization between "
6223 "function pointer and %<void *%>"),
6224 G_("ISO C forbids return between function "
6225 "pointer and %<void *%>"));
6226 /* Const and volatile mean something different for function types,
6227 so the usual warnings are not appropriate. */
6228 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6229 && TREE_CODE (ttl) != FUNCTION_TYPE)
6231 /* Don't warn about loss of qualifier for conversions from
6232 qualified void* to pointers to arrays with corresponding
6233 qualifier on the element type. */
6235 ttl = strip_array_types (ttl);
6237 /* Assignments between atomic and non-atomic objects are OK. */
6238 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6239 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6241 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6242 OPT_Wdiscarded_qualifiers,
6243 G_("passing argument %d of %qE discards "
6244 "%qv qualifier from pointer target type"),
6245 G_("assignment discards %qv qualifier "
6246 "from pointer target type"),
6247 G_("initialization discards %qv qualifier "
6248 "from pointer target type"),
6249 G_("return discards %qv qualifier from "
6250 "pointer target type"),
6251 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6253 /* If this is not a case of ignoring a mismatch in signedness,
6255 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6258 /* If there is a mismatch, do warn. */
6259 else if (warn_pointer_sign)
6260 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6261 G_("pointer targets in passing argument "
6262 "%d of %qE differ in signedness"),
6263 G_("pointer targets in assignment "
6264 "differ in signedness"),
6265 G_("pointer targets in initialization "
6266 "differ in signedness"),
6267 G_("pointer targets in return differ "
6270 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6271 && TREE_CODE (ttr) == FUNCTION_TYPE)
6273 /* Because const and volatile on functions are restrictions
6274 that say the function will not do certain things,
6275 it is okay to use a const or volatile function
6276 where an ordinary one is wanted, but not vice-versa. */
6277 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6278 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6279 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6280 OPT_Wdiscarded_qualifiers,
6281 G_("passing argument %d of %qE makes "
6282 "%q#v qualified function pointer "
6283 "from unqualified"),
6284 G_("assignment makes %q#v qualified function "
6285 "pointer from unqualified"),
6286 G_("initialization makes %q#v qualified "
6287 "function pointer from unqualified"),
6288 G_("return makes %q#v qualified function "
6289 "pointer from unqualified"),
6290 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6294 /* Avoid warning about the volatile ObjC EH puts on decls. */
6296 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6297 OPT_Wincompatible_pointer_types,
6298 G_("passing argument %d of %qE from "
6299 "incompatible pointer type"),
6300 G_("assignment from incompatible pointer type"),
6301 G_("initialization from incompatible "
6303 G_("return from incompatible pointer type"));
6305 return convert (type, rhs);
6307 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6309 /* ??? This should not be an error when inlining calls to
6310 unprototyped functions. */
6311 error_at (location, "invalid use of non-lvalue array");
6312 return error_mark_node;
6314 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6316 /* An explicit constant 0 can convert to a pointer,
6317 or one that results from arithmetic, even including
6318 a cast to integer type. */
6319 if (!null_pointer_constant)
6320 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6321 OPT_Wint_conversion,
6322 G_("passing argument %d of %qE makes "
6323 "pointer from integer without a cast"),
6324 G_("assignment makes pointer from integer "
6326 G_("initialization makes pointer from "
6327 "integer without a cast"),
6328 G_("return makes pointer from integer "
6331 return convert (type, rhs);
6333 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6335 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6336 OPT_Wint_conversion,
6337 G_("passing argument %d of %qE makes integer "
6338 "from pointer without a cast"),
6339 G_("assignment makes integer from pointer "
6341 G_("initialization makes integer from pointer "
6343 G_("return makes integer from pointer "
6345 return convert (type, rhs);
6347 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6350 bool save = in_late_binary_op;
6351 in_late_binary_op = true;
6352 ret = convert (type, rhs);
6353 in_late_binary_op = save;
6360 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6362 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6363 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6364 "expected %qT but argument is of type %qT", type, rhstype);
6367 error_at (location, "incompatible types when assigning to type %qT from "
6368 "type %qT", type, rhstype);
6372 "incompatible types when initializing type %qT using type %qT",
6377 "incompatible types when returning type %qT but %qT was "
6378 "expected", rhstype, type);
6384 return error_mark_node;
6387 /* If VALUE is a compound expr all of whose expressions are constant, then
6388 return its value. Otherwise, return error_mark_node.
6390 This is for handling COMPOUND_EXPRs as initializer elements
6391 which is allowed with a warning when -pedantic is specified. */
6394 valid_compound_expr_initializer (tree value, tree endtype)
6396 if (TREE_CODE (value) == COMPOUND_EXPR)
6398 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6400 return error_mark_node;
6401 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6404 else if (!initializer_constant_valid_p (value, endtype))
6405 return error_mark_node;
6410 /* Perform appropriate conversions on the initial value of a variable,
6411 store it in the declaration DECL,
6412 and print any error messages that are appropriate.
6413 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6414 If the init is invalid, store an ERROR_MARK.
6416 INIT_LOC is the location of the initial value. */
6419 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6424 /* If variable's type was invalidly declared, just ignore it. */
6426 type = TREE_TYPE (decl);
6427 if (TREE_CODE (type) == ERROR_MARK)
6430 /* Digest the specified initializer into an expression. */
6433 npc = null_pointer_constant_p (init);
6434 value = digest_init (init_loc, type, init, origtype, npc,
6435 true, TREE_STATIC (decl));
6437 /* Store the expression if valid; else report error. */
6439 if (!in_system_header_at (input_location)
6440 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6441 warning (OPT_Wtraditional, "traditional C rejects automatic "
6442 "aggregate initialization");
6444 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6445 DECL_INITIAL (decl) = value;
6447 /* ANSI wants warnings about out-of-range constant initializers. */
6448 STRIP_TYPE_NOPS (value);
6449 if (TREE_STATIC (decl))
6450 constant_expression_warning (value);
6452 /* Check if we need to set array size from compound literal size. */
6453 if (TREE_CODE (type) == ARRAY_TYPE
6454 && TYPE_DOMAIN (type) == 0
6455 && value != error_mark_node)
6457 tree inside_init = init;
6459 STRIP_TYPE_NOPS (inside_init);
6460 inside_init = fold (inside_init);
6462 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6464 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6466 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6468 /* For int foo[] = (int [3]){1}; we need to set array size
6469 now since later on array initializer will be just the
6470 brace enclosed list of the compound literal. */
6471 tree etype = strip_array_types (TREE_TYPE (decl));
6472 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6473 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6475 layout_decl (cldecl, 0);
6477 = c_build_qualified_type (type, TYPE_QUALS (etype));
6483 /* Methods for storing and printing names for error messages. */
6485 /* Implement a spelling stack that allows components of a name to be pushed
6486 and popped. Each element on the stack is this structure. */
6493 unsigned HOST_WIDE_INT i;
6498 #define SPELLING_STRING 1
6499 #define SPELLING_MEMBER 2
6500 #define SPELLING_BOUNDS 3
6502 static struct spelling *spelling; /* Next stack element (unused). */
6503 static struct spelling *spelling_base; /* Spelling stack base. */
6504 static int spelling_size; /* Size of the spelling stack. */
6506 /* Macros to save and restore the spelling stack around push_... functions.
6507 Alternative to SAVE_SPELLING_STACK. */
6509 #define SPELLING_DEPTH() (spelling - spelling_base)
6510 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6512 /* Push an element on the spelling stack with type KIND and assign VALUE
6515 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6517 int depth = SPELLING_DEPTH (); \
6519 if (depth >= spelling_size) \
6521 spelling_size += 10; \
6522 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6524 RESTORE_SPELLING_DEPTH (depth); \
6527 spelling->kind = (KIND); \
6528 spelling->MEMBER = (VALUE); \
6532 /* Push STRING on the stack. Printed literally. */
6535 push_string (const char *string)
6537 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6540 /* Push a member name on the stack. Printed as '.' STRING. */
6543 push_member_name (tree decl)
6545 const char *const string
6547 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6548 : _("<anonymous>"));
6549 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6552 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6555 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6557 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6560 /* Compute the maximum size in bytes of the printed spelling. */
6563 spelling_length (void)
6568 for (p = spelling_base; p < spelling; p++)
6570 if (p->kind == SPELLING_BOUNDS)
6573 size += strlen (p->u.s) + 1;
6579 /* Print the spelling to BUFFER and return it. */
6582 print_spelling (char *buffer)
6587 for (p = spelling_base; p < spelling; p++)
6588 if (p->kind == SPELLING_BOUNDS)
6590 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6596 if (p->kind == SPELLING_MEMBER)
6598 for (s = p->u.s; (*d = *s++); d++)
6605 /* Digest the parser output INIT as an initializer for type TYPE.
6606 Return a C expression of type TYPE to represent the initial value.
6608 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6610 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6612 If INIT is a string constant, STRICT_STRING is true if it is
6613 unparenthesized or we should not warn here for it being parenthesized.
6614 For other types of INIT, STRICT_STRING is not used.
6616 INIT_LOC is the location of the INIT.
6618 REQUIRE_CONSTANT requests an error if non-constant initializers or
6619 elements are seen. */
6622 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6623 bool null_pointer_constant, bool strict_string,
6624 int require_constant)
6626 enum tree_code code = TREE_CODE (type);
6627 tree inside_init = init;
6628 tree semantic_type = NULL_TREE;
6629 bool maybe_const = true;
6631 if (type == error_mark_node
6633 || error_operand_p (init))
6634 return error_mark_node;
6636 STRIP_TYPE_NOPS (inside_init);
6638 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6640 semantic_type = TREE_TYPE (inside_init);
6641 inside_init = TREE_OPERAND (inside_init, 0);
6643 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6644 inside_init = decl_constant_value_for_optimization (inside_init);
6646 /* Initialization of an array of chars from a string constant
6647 optionally enclosed in braces. */
6649 if (code == ARRAY_TYPE && inside_init
6650 && TREE_CODE (inside_init) == STRING_CST)
6653 = (TYPE_ATOMIC (TREE_TYPE (type))
6654 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6656 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6657 /* Note that an array could be both an array of character type
6658 and an array of wchar_t if wchar_t is signed char or unsigned
6660 bool char_array = (typ1 == char_type_node
6661 || typ1 == signed_char_type_node
6662 || typ1 == unsigned_char_type_node);
6663 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6664 bool char16_array = !!comptypes (typ1, char16_type_node);
6665 bool char32_array = !!comptypes (typ1, char32_type_node);
6667 if (char_array || wchar_array || char16_array || char32_array)
6670 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6671 expr.value = inside_init;
6672 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6673 expr.original_type = NULL;
6674 maybe_warn_string_init (init_loc, type, expr);
6676 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6677 pedwarn_init (init_loc, OPT_Wpedantic,
6678 "initialization of a flexible array member");
6680 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6681 TYPE_MAIN_VARIANT (type)))
6686 if (typ2 != char_type_node)
6688 error_init (init_loc, "char-array initialized from wide "
6690 return error_mark_node;
6695 if (typ2 == char_type_node)
6697 error_init (init_loc, "wide character array initialized "
6698 "from non-wide string");
6699 return error_mark_node;
6701 else if (!comptypes(typ1, typ2))
6703 error_init (init_loc, "wide character array initialized "
6704 "from incompatible wide string");
6705 return error_mark_node;
6709 TREE_TYPE (inside_init) = type;
6710 if (TYPE_DOMAIN (type) != 0
6711 && TYPE_SIZE (type) != 0
6712 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6714 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6716 /* Subtract the size of a single (possibly wide) character
6717 because it's ok to ignore the terminating null char
6718 that is counted in the length of the constant. */
6719 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6721 - (TYPE_PRECISION (typ1)
6723 pedwarn_init (init_loc, 0,
6724 ("initializer-string for array of chars "
6726 else if (warn_cxx_compat
6727 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6728 warning_at (init_loc, OPT_Wc___compat,
6729 ("initializer-string for array chars "
6730 "is too long for C++"));
6735 else if (INTEGRAL_TYPE_P (typ1))
6737 error_init (init_loc, "array of inappropriate type initialized "
6738 "from string constant");
6739 return error_mark_node;
6743 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6744 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6745 below and handle as a constructor. */
6746 if (code == VECTOR_TYPE
6747 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6748 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6749 && TREE_CONSTANT (inside_init))
6751 if (TREE_CODE (inside_init) == VECTOR_CST
6752 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6753 TYPE_MAIN_VARIANT (type)))
6756 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6758 unsigned HOST_WIDE_INT ix;
6760 bool constant_p = true;
6762 /* Iterate through elements and check if all constructor
6763 elements are *_CSTs. */
6764 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6765 if (!CONSTANT_CLASS_P (value))
6772 return build_vector_from_ctor (type,
6773 CONSTRUCTOR_ELTS (inside_init));
6777 if (warn_sequence_point)
6778 verify_sequence_points (inside_init);
6780 /* Any type can be initialized
6781 from an expression of the same type, optionally with braces. */
6783 if (inside_init && TREE_TYPE (inside_init) != 0
6784 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6785 TYPE_MAIN_VARIANT (type))
6786 || (code == ARRAY_TYPE
6787 && comptypes (TREE_TYPE (inside_init), type))
6788 || (code == VECTOR_TYPE
6789 && comptypes (TREE_TYPE (inside_init), type))
6790 || (code == POINTER_TYPE
6791 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6792 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6793 TREE_TYPE (type)))))
6795 if (code == POINTER_TYPE)
6797 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6799 if (TREE_CODE (inside_init) == STRING_CST
6800 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6801 inside_init = array_to_pointer_conversion
6802 (init_loc, inside_init);
6805 error_init (init_loc, "invalid use of non-lvalue array");
6806 return error_mark_node;
6811 if (code == VECTOR_TYPE)
6812 /* Although the types are compatible, we may require a
6814 inside_init = convert (type, inside_init);
6816 if (require_constant
6817 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6819 /* As an extension, allow initializing objects with static storage
6820 duration with compound literals (which are then treated just as
6821 the brace enclosed list they contain). Also allow this for
6822 vectors, as we can only assign them with compound literals. */
6823 if (flag_isoc99 && code != VECTOR_TYPE)
6824 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6826 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6827 inside_init = DECL_INITIAL (decl);
6830 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6831 && TREE_CODE (inside_init) != CONSTRUCTOR)
6833 error_init (init_loc, "array initialized from non-constant array "
6835 return error_mark_node;
6838 /* Compound expressions can only occur here if -Wpedantic or
6839 -pedantic-errors is specified. In the later case, we always want
6840 an error. In the former case, we simply want a warning. */
6841 if (require_constant && pedantic
6842 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6845 = valid_compound_expr_initializer (inside_init,
6846 TREE_TYPE (inside_init));
6847 if (inside_init == error_mark_node)
6848 error_init (init_loc, "initializer element is not constant");
6850 pedwarn_init (init_loc, OPT_Wpedantic,
6851 "initializer element is not constant");
6852 if (flag_pedantic_errors)
6853 inside_init = error_mark_node;
6855 else if (require_constant
6856 && !initializer_constant_valid_p (inside_init,
6857 TREE_TYPE (inside_init)))
6859 error_init (init_loc, "initializer element is not constant");
6860 inside_init = error_mark_node;
6862 else if (require_constant && !maybe_const)
6863 pedwarn_init (init_loc, OPT_Wpedantic,
6864 "initializer element is not a constant expression");
6866 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6867 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6868 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6869 type, inside_init, origtype,
6870 ic_init, null_pointer_constant,
6871 NULL_TREE, NULL_TREE, 0);
6875 /* Handle scalar types, including conversions. */
6877 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6878 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6879 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6881 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6882 && (TREE_CODE (init) == STRING_CST
6883 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6884 inside_init = init = array_to_pointer_conversion (init_loc, init);
6886 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6889 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6890 inside_init, origtype, ic_init,
6891 null_pointer_constant, NULL_TREE, NULL_TREE,
6894 /* Check to see if we have already given an error message. */
6895 if (inside_init == error_mark_node)
6897 else if (require_constant && !TREE_CONSTANT (inside_init))
6899 error_init (init_loc, "initializer element is not constant");
6900 inside_init = error_mark_node;
6902 else if (require_constant
6903 && !initializer_constant_valid_p (inside_init,
6904 TREE_TYPE (inside_init)))
6906 error_init (init_loc, "initializer element is not computable at "
6908 inside_init = error_mark_node;
6910 else if (require_constant && !maybe_const)
6911 pedwarn_init (init_loc, 0,
6912 "initializer element is not a constant expression");
6917 /* Come here only for records and arrays. */
6919 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6921 error_init (init_loc, "variable-sized object may not be initialized");
6922 return error_mark_node;
6925 error_init (init_loc, "invalid initializer");
6926 return error_mark_node;
6929 /* Handle initializers that use braces. */
6931 /* Type of object we are accumulating a constructor for.
6932 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6933 static tree constructor_type;
6935 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6937 static tree constructor_fields;
6939 /* For an ARRAY_TYPE, this is the specified index
6940 at which to store the next element we get. */
6941 static tree constructor_index;
6943 /* For an ARRAY_TYPE, this is the maximum index. */
6944 static tree constructor_max_index;
6946 /* For a RECORD_TYPE, this is the first field not yet written out. */
6947 static tree constructor_unfilled_fields;
6949 /* For an ARRAY_TYPE, this is the index of the first element
6950 not yet written out. */
6951 static tree constructor_unfilled_index;
6953 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6954 This is so we can generate gaps between fields, when appropriate. */
6955 static tree constructor_bit_index;
6957 /* If we are saving up the elements rather than allocating them,
6958 this is the list of elements so far (in reverse order,
6959 most recent first). */
6960 static vec<constructor_elt, va_gc> *constructor_elements;
6962 /* 1 if constructor should be incrementally stored into a constructor chain,
6963 0 if all the elements should be kept in AVL tree. */
6964 static int constructor_incremental;
6966 /* 1 if so far this constructor's elements are all compile-time constants. */
6967 static int constructor_constant;
6969 /* 1 if so far this constructor's elements are all valid address constants. */
6970 static int constructor_simple;
6972 /* 1 if this constructor has an element that cannot be part of a
6973 constant expression. */
6974 static int constructor_nonconst;
6976 /* 1 if this constructor is erroneous so far. */
6977 static int constructor_erroneous;
6979 /* 1 if this constructor is the universal zero initializer { 0 }. */
6980 static int constructor_zeroinit;
6982 /* Structure for managing pending initializer elements, organized as an
6987 struct init_node *left, *right;
6988 struct init_node *parent;
6995 /* Tree of pending elements at this constructor level.
6996 These are elements encountered out of order
6997 which belong at places we haven't reached yet in actually
6999 Will never hold tree nodes across GC runs. */
7000 static struct init_node *constructor_pending_elts;
7002 /* The SPELLING_DEPTH of this constructor. */
7003 static int constructor_depth;
7005 /* DECL node for which an initializer is being read.
7006 0 means we are reading a constructor expression
7007 such as (struct foo) {...}. */
7008 static tree constructor_decl;
7010 /* Nonzero if this is an initializer for a top-level decl. */
7011 static int constructor_top_level;
7013 /* Nonzero if there were any member designators in this initializer. */
7014 static int constructor_designated;
7016 /* Nesting depth of designator list. */
7017 static int designator_depth;
7019 /* Nonzero if there were diagnosed errors in this designator list. */
7020 static int designator_erroneous;
7023 /* This stack has a level for each implicit or explicit level of
7024 structuring in the initializer, including the outermost one. It
7025 saves the values of most of the variables above. */
7027 struct constructor_range_stack;
7029 struct constructor_stack
7031 struct constructor_stack *next;
7036 tree unfilled_index;
7037 tree unfilled_fields;
7039 vec<constructor_elt, va_gc> *elements;
7040 struct init_node *pending_elts;
7043 /* If value nonzero, this value should replace the entire
7044 constructor at this level. */
7045 struct c_expr replacement_value;
7046 struct constructor_range_stack *range_stack;
7055 int designator_depth;
7058 static struct constructor_stack *constructor_stack;
7060 /* This stack represents designators from some range designator up to
7061 the last designator in the list. */
7063 struct constructor_range_stack
7065 struct constructor_range_stack *next, *prev;
7066 struct constructor_stack *stack;
7073 static struct constructor_range_stack *constructor_range_stack;
7075 /* This stack records separate initializers that are nested.
7076 Nested initializers can't happen in ANSI C, but GNU C allows them
7077 in cases like { ... (struct foo) { ... } ... }. */
7079 struct initializer_stack
7081 struct initializer_stack *next;
7083 struct constructor_stack *constructor_stack;
7084 struct constructor_range_stack *constructor_range_stack;
7085 vec<constructor_elt, va_gc> *elements;
7086 struct spelling *spelling;
7087 struct spelling *spelling_base;
7090 char require_constant_value;
7091 char require_constant_elements;
7094 static struct initializer_stack *initializer_stack;
7096 /* Prepare to parse and output the initializer for variable DECL. */
7099 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7102 struct initializer_stack *p = XNEW (struct initializer_stack);
7104 p->decl = constructor_decl;
7105 p->require_constant_value = require_constant_value;
7106 p->require_constant_elements = require_constant_elements;
7107 p->constructor_stack = constructor_stack;
7108 p->constructor_range_stack = constructor_range_stack;
7109 p->elements = constructor_elements;
7110 p->spelling = spelling;
7111 p->spelling_base = spelling_base;
7112 p->spelling_size = spelling_size;
7113 p->top_level = constructor_top_level;
7114 p->next = initializer_stack;
7115 initializer_stack = p;
7117 constructor_decl = decl;
7118 constructor_designated = 0;
7119 constructor_top_level = top_level;
7121 if (decl != 0 && decl != error_mark_node)
7123 require_constant_value = TREE_STATIC (decl);
7124 require_constant_elements
7125 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7126 /* For a scalar, you can always use any value to initialize,
7127 even within braces. */
7128 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7129 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7133 require_constant_value = 0;
7134 require_constant_elements = 0;
7135 locus = _("(anonymous)");
7138 constructor_stack = 0;
7139 constructor_range_stack = 0;
7141 found_missing_braces = 0;
7145 RESTORE_SPELLING_DEPTH (0);
7148 push_string (locus);
7154 struct initializer_stack *p = initializer_stack;
7156 /* Free the whole constructor stack of this initializer. */
7157 while (constructor_stack)
7159 struct constructor_stack *q = constructor_stack;
7160 constructor_stack = q->next;
7164 gcc_assert (!constructor_range_stack);
7166 /* Pop back to the data of the outer initializer (if any). */
7167 free (spelling_base);
7169 constructor_decl = p->decl;
7170 require_constant_value = p->require_constant_value;
7171 require_constant_elements = p->require_constant_elements;
7172 constructor_stack = p->constructor_stack;
7173 constructor_range_stack = p->constructor_range_stack;
7174 constructor_elements = p->elements;
7175 spelling = p->spelling;
7176 spelling_base = p->spelling_base;
7177 spelling_size = p->spelling_size;
7178 constructor_top_level = p->top_level;
7179 initializer_stack = p->next;
7183 /* Call here when we see the initializer is surrounded by braces.
7184 This is instead of a call to push_init_level;
7185 it is matched by a call to pop_init_level.
7187 TYPE is the type to initialize, for a constructor expression.
7188 For an initializer for a decl, TYPE is zero. */
7191 really_start_incremental_init (tree type)
7193 struct constructor_stack *p = XNEW (struct constructor_stack);
7196 type = TREE_TYPE (constructor_decl);
7198 if (TREE_CODE (type) == VECTOR_TYPE
7199 && TYPE_VECTOR_OPAQUE (type))
7200 error ("opaque vector types cannot be initialized");
7202 p->type = constructor_type;
7203 p->fields = constructor_fields;
7204 p->index = constructor_index;
7205 p->max_index = constructor_max_index;
7206 p->unfilled_index = constructor_unfilled_index;
7207 p->unfilled_fields = constructor_unfilled_fields;
7208 p->bit_index = constructor_bit_index;
7209 p->elements = constructor_elements;
7210 p->constant = constructor_constant;
7211 p->simple = constructor_simple;
7212 p->nonconst = constructor_nonconst;
7213 p->erroneous = constructor_erroneous;
7214 p->pending_elts = constructor_pending_elts;
7215 p->depth = constructor_depth;
7216 p->replacement_value.value = 0;
7217 p->replacement_value.original_code = ERROR_MARK;
7218 p->replacement_value.original_type = NULL;
7222 p->incremental = constructor_incremental;
7223 p->designated = constructor_designated;
7224 p->designator_depth = designator_depth;
7226 constructor_stack = p;
7228 constructor_constant = 1;
7229 constructor_simple = 1;
7230 constructor_nonconst = 0;
7231 constructor_depth = SPELLING_DEPTH ();
7232 constructor_elements = NULL;
7233 constructor_pending_elts = 0;
7234 constructor_type = type;
7235 constructor_incremental = 1;
7236 constructor_designated = 0;
7237 constructor_zeroinit = 1;
7238 designator_depth = 0;
7239 designator_erroneous = 0;
7241 if (TREE_CODE (constructor_type) == RECORD_TYPE
7242 || TREE_CODE (constructor_type) == UNION_TYPE)
7244 constructor_fields = TYPE_FIELDS (constructor_type);
7245 /* Skip any nameless bit fields at the beginning. */
7246 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7247 && DECL_NAME (constructor_fields) == 0)
7248 constructor_fields = DECL_CHAIN (constructor_fields);
7250 constructor_unfilled_fields = constructor_fields;
7251 constructor_bit_index = bitsize_zero_node;
7253 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7255 if (TYPE_DOMAIN (constructor_type))
7257 constructor_max_index
7258 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7260 /* Detect non-empty initializations of zero-length arrays. */
7261 if (constructor_max_index == NULL_TREE
7262 && TYPE_SIZE (constructor_type))
7263 constructor_max_index = integer_minus_one_node;
7265 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7266 to initialize VLAs will cause a proper error; avoid tree
7267 checking errors as well by setting a safe value. */
7268 if (constructor_max_index
7269 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7270 constructor_max_index = integer_minus_one_node;
7273 = convert (bitsizetype,
7274 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7278 constructor_index = bitsize_zero_node;
7279 constructor_max_index = NULL_TREE;
7282 constructor_unfilled_index = constructor_index;
7284 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7286 /* Vectors are like simple fixed-size arrays. */
7287 constructor_max_index =
7288 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7289 constructor_index = bitsize_zero_node;
7290 constructor_unfilled_index = constructor_index;
7294 /* Handle the case of int x = {5}; */
7295 constructor_fields = constructor_type;
7296 constructor_unfilled_fields = constructor_type;
7300 /* Push down into a subobject, for initialization.
7301 If this is for an explicit set of braces, IMPLICIT is 0.
7302 If it is because the next element belongs at a lower level,
7303 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7306 push_init_level (location_t loc, int implicit,
7307 struct obstack *braced_init_obstack)
7309 struct constructor_stack *p;
7310 tree value = NULL_TREE;
7312 /* If we've exhausted any levels that didn't have braces,
7313 pop them now. If implicit == 1, this will have been done in
7314 process_init_element; do not repeat it here because in the case
7315 of excess initializers for an empty aggregate this leads to an
7316 infinite cycle of popping a level and immediately recreating
7320 while (constructor_stack->implicit)
7322 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7323 || TREE_CODE (constructor_type) == UNION_TYPE)
7324 && constructor_fields == 0)
7325 process_init_element (input_location,
7326 pop_init_level (loc, 1, braced_init_obstack),
7327 true, braced_init_obstack);
7328 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7329 && constructor_max_index
7330 && tree_int_cst_lt (constructor_max_index,
7332 process_init_element (input_location,
7333 pop_init_level (loc, 1, braced_init_obstack),
7334 true, braced_init_obstack);
7340 /* Unless this is an explicit brace, we need to preserve previous
7344 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7345 || TREE_CODE (constructor_type) == UNION_TYPE)
7346 && constructor_fields)
7347 value = find_init_member (constructor_fields, braced_init_obstack);
7348 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7349 value = find_init_member (constructor_index, braced_init_obstack);
7352 p = XNEW (struct constructor_stack);
7353 p->type = constructor_type;
7354 p->fields = constructor_fields;
7355 p->index = constructor_index;
7356 p->max_index = constructor_max_index;
7357 p->unfilled_index = constructor_unfilled_index;
7358 p->unfilled_fields = constructor_unfilled_fields;
7359 p->bit_index = constructor_bit_index;
7360 p->elements = constructor_elements;
7361 p->constant = constructor_constant;
7362 p->simple = constructor_simple;
7363 p->nonconst = constructor_nonconst;
7364 p->erroneous = constructor_erroneous;
7365 p->pending_elts = constructor_pending_elts;
7366 p->depth = constructor_depth;
7367 p->replacement_value.value = 0;
7368 p->replacement_value.original_code = ERROR_MARK;
7369 p->replacement_value.original_type = NULL;
7370 p->implicit = implicit;
7372 p->incremental = constructor_incremental;
7373 p->designated = constructor_designated;
7374 p->designator_depth = designator_depth;
7375 p->next = constructor_stack;
7377 constructor_stack = p;
7379 constructor_constant = 1;
7380 constructor_simple = 1;
7381 constructor_nonconst = 0;
7382 constructor_depth = SPELLING_DEPTH ();
7383 constructor_elements = NULL;
7384 constructor_incremental = 1;
7385 constructor_designated = 0;
7386 constructor_pending_elts = 0;
7389 p->range_stack = constructor_range_stack;
7390 constructor_range_stack = 0;
7391 designator_depth = 0;
7392 designator_erroneous = 0;
7395 /* Don't die if an entire brace-pair level is superfluous
7396 in the containing level. */
7397 if (constructor_type == 0)
7399 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7400 || TREE_CODE (constructor_type) == UNION_TYPE)
7402 /* Don't die if there are extra init elts at the end. */
7403 if (constructor_fields == 0)
7404 constructor_type = 0;
7407 constructor_type = TREE_TYPE (constructor_fields);
7408 push_member_name (constructor_fields);
7409 constructor_depth++;
7411 /* If upper initializer is designated, then mark this as
7412 designated too to prevent bogus warnings. */
7413 constructor_designated = p->designated;
7415 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7417 constructor_type = TREE_TYPE (constructor_type);
7418 push_array_bounds (tree_to_uhwi (constructor_index));
7419 constructor_depth++;
7422 if (constructor_type == 0)
7424 error_init (loc, "extra brace group at end of initializer");
7425 constructor_fields = 0;
7426 constructor_unfilled_fields = 0;
7430 if (value && TREE_CODE (value) == CONSTRUCTOR)
7432 constructor_constant = TREE_CONSTANT (value);
7433 constructor_simple = TREE_STATIC (value);
7434 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7435 constructor_elements = CONSTRUCTOR_ELTS (value);
7436 if (!vec_safe_is_empty (constructor_elements)
7437 && (TREE_CODE (constructor_type) == RECORD_TYPE
7438 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7439 set_nonincremental_init (braced_init_obstack);
7443 found_missing_braces = 1;
7445 if (TREE_CODE (constructor_type) == RECORD_TYPE
7446 || TREE_CODE (constructor_type) == UNION_TYPE)
7448 constructor_fields = TYPE_FIELDS (constructor_type);
7449 /* Skip any nameless bit fields at the beginning. */
7450 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7451 && DECL_NAME (constructor_fields) == 0)
7452 constructor_fields = DECL_CHAIN (constructor_fields);
7454 constructor_unfilled_fields = constructor_fields;
7455 constructor_bit_index = bitsize_zero_node;
7457 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7459 /* Vectors are like simple fixed-size arrays. */
7460 constructor_max_index =
7461 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7462 constructor_index = bitsize_int (0);
7463 constructor_unfilled_index = constructor_index;
7465 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7467 if (TYPE_DOMAIN (constructor_type))
7469 constructor_max_index
7470 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7472 /* Detect non-empty initializations of zero-length arrays. */
7473 if (constructor_max_index == NULL_TREE
7474 && TYPE_SIZE (constructor_type))
7475 constructor_max_index = integer_minus_one_node;
7477 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7478 to initialize VLAs will cause a proper error; avoid tree
7479 checking errors as well by setting a safe value. */
7480 if (constructor_max_index
7481 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7482 constructor_max_index = integer_minus_one_node;
7485 = convert (bitsizetype,
7486 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7489 constructor_index = bitsize_zero_node;
7491 constructor_unfilled_index = constructor_index;
7492 if (value && TREE_CODE (value) == STRING_CST)
7494 /* We need to split the char/wchar array into individual
7495 characters, so that we don't have to special case it
7497 set_nonincremental_init_from_string (value, braced_init_obstack);
7502 if (constructor_type != error_mark_node)
7503 warning_init (input_location, 0, "braces around scalar initializer");
7504 constructor_fields = constructor_type;
7505 constructor_unfilled_fields = constructor_type;
7509 /* At the end of an implicit or explicit brace level,
7510 finish up that level of constructor. If a single expression
7511 with redundant braces initialized that level, return the
7512 c_expr structure for that expression. Otherwise, the original_code
7513 element is set to ERROR_MARK.
7514 If we were outputting the elements as they are read, return 0 as the value
7515 from inner levels (process_init_element ignores that),
7516 but return error_mark_node as the value from the outermost level
7517 (that's what we want to put in DECL_INITIAL).
7518 Otherwise, return a CONSTRUCTOR expression as the value. */
7521 pop_init_level (location_t loc, int implicit,
7522 struct obstack *braced_init_obstack)
7524 struct constructor_stack *p;
7527 ret.original_code = ERROR_MARK;
7528 ret.original_type = NULL;
7532 /* When we come to an explicit close brace,
7533 pop any inner levels that didn't have explicit braces. */
7534 while (constructor_stack->implicit)
7535 process_init_element (input_location,
7536 pop_init_level (loc, 1, braced_init_obstack),
7537 true, braced_init_obstack);
7538 gcc_assert (!constructor_range_stack);
7541 /* Now output all pending elements. */
7542 constructor_incremental = 1;
7543 output_pending_init_elements (1, braced_init_obstack);
7545 p = constructor_stack;
7547 /* Error for initializing a flexible array member, or a zero-length
7548 array member in an inappropriate context. */
7549 if (constructor_type && constructor_fields
7550 && TREE_CODE (constructor_type) == ARRAY_TYPE
7551 && TYPE_DOMAIN (constructor_type)
7552 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7554 /* Silently discard empty initializations. The parser will
7555 already have pedwarned for empty brackets. */
7556 if (integer_zerop (constructor_unfilled_index))
7557 constructor_type = NULL_TREE;
7560 gcc_assert (!TYPE_SIZE (constructor_type));
7562 if (constructor_depth > 2)
7563 error_init (loc, "initialization of flexible array member in a nested context");
7565 pedwarn_init (loc, OPT_Wpedantic,
7566 "initialization of a flexible array member");
7568 /* We have already issued an error message for the existence
7569 of a flexible array member not at the end of the structure.
7570 Discard the initializer so that we do not die later. */
7571 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7572 constructor_type = NULL_TREE;
7576 switch (vec_safe_length (constructor_elements))
7579 /* Initialization with { } counts as zeroinit. */
7580 constructor_zeroinit = 1;
7583 /* This might be zeroinit as well. */
7584 if (integer_zerop ((*constructor_elements)[0].value))
7585 constructor_zeroinit = 1;
7588 /* If the constructor has more than one element, it can't be { 0 }. */
7589 constructor_zeroinit = 0;
7593 /* Warn when some structs are initialized with direct aggregation. */
7594 if (!implicit && found_missing_braces && warn_missing_braces
7595 && !constructor_zeroinit)
7596 warning_init (loc, OPT_Wmissing_braces,
7597 "missing braces around initializer");
7599 /* Warn when some struct elements are implicitly initialized to zero. */
7600 if (warn_missing_field_initializers
7602 && TREE_CODE (constructor_type) == RECORD_TYPE
7603 && constructor_unfilled_fields)
7605 /* Do not warn for flexible array members or zero-length arrays. */
7606 while (constructor_unfilled_fields
7607 && (!DECL_SIZE (constructor_unfilled_fields)
7608 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7609 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7611 if (constructor_unfilled_fields
7612 /* Do not warn if this level of the initializer uses member
7613 designators; it is likely to be deliberate. */
7614 && !constructor_designated
7615 /* Do not warn about initializing with { 0 } or with { }. */
7616 && !constructor_zeroinit)
7618 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7619 "missing initializer for field %qD of %qT",
7620 constructor_unfilled_fields,
7622 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7623 "%qD declared here", constructor_unfilled_fields);
7627 /* Pad out the end of the structure. */
7628 if (p->replacement_value.value)
7629 /* If this closes a superfluous brace pair,
7630 just pass out the element between them. */
7631 ret = p->replacement_value;
7632 else if (constructor_type == 0)
7634 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7635 && TREE_CODE (constructor_type) != UNION_TYPE
7636 && TREE_CODE (constructor_type) != ARRAY_TYPE
7637 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7639 /* A nonincremental scalar initializer--just return
7640 the element, after verifying there is just one. */
7641 if (vec_safe_is_empty (constructor_elements))
7643 if (!constructor_erroneous)
7644 error_init (loc, "empty scalar initializer");
7645 ret.value = error_mark_node;
7647 else if (vec_safe_length (constructor_elements) != 1)
7649 error_init (loc, "extra elements in scalar initializer");
7650 ret.value = (*constructor_elements)[0].value;
7653 ret.value = (*constructor_elements)[0].value;
7657 if (constructor_erroneous)
7658 ret.value = error_mark_node;
7661 ret.value = build_constructor (constructor_type,
7662 constructor_elements);
7663 if (constructor_constant)
7664 TREE_CONSTANT (ret.value) = 1;
7665 if (constructor_constant && constructor_simple)
7666 TREE_STATIC (ret.value) = 1;
7667 if (constructor_nonconst)
7668 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7672 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7674 if (constructor_nonconst)
7675 ret.original_code = C_MAYBE_CONST_EXPR;
7676 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7677 ret.original_code = ERROR_MARK;
7680 constructor_type = p->type;
7681 constructor_fields = p->fields;
7682 constructor_index = p->index;
7683 constructor_max_index = p->max_index;
7684 constructor_unfilled_index = p->unfilled_index;
7685 constructor_unfilled_fields = p->unfilled_fields;
7686 constructor_bit_index = p->bit_index;
7687 constructor_elements = p->elements;
7688 constructor_constant = p->constant;
7689 constructor_simple = p->simple;
7690 constructor_nonconst = p->nonconst;
7691 constructor_erroneous = p->erroneous;
7692 constructor_incremental = p->incremental;
7693 constructor_designated = p->designated;
7694 designator_depth = p->designator_depth;
7695 constructor_pending_elts = p->pending_elts;
7696 constructor_depth = p->depth;
7698 constructor_range_stack = p->range_stack;
7699 RESTORE_SPELLING_DEPTH (constructor_depth);
7701 constructor_stack = p->next;
7704 if (ret.value == 0 && constructor_stack == 0)
7705 ret.value = error_mark_node;
7709 /* Common handling for both array range and field name designators.
7710 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7713 set_designator (location_t loc, int array,
7714 struct obstack *braced_init_obstack)
7717 enum tree_code subcode;
7719 /* Don't die if an entire brace-pair level is superfluous
7720 in the containing level. */
7721 if (constructor_type == 0)
7724 /* If there were errors in this designator list already, bail out
7726 if (designator_erroneous)
7729 if (!designator_depth)
7731 gcc_assert (!constructor_range_stack);
7733 /* Designator list starts at the level of closest explicit
7735 while (constructor_stack->implicit)
7736 process_init_element (input_location,
7737 pop_init_level (loc, 1, braced_init_obstack),
7738 true, braced_init_obstack);
7739 constructor_designated = 1;
7743 switch (TREE_CODE (constructor_type))
7747 subtype = TREE_TYPE (constructor_fields);
7748 if (subtype != error_mark_node)
7749 subtype = TYPE_MAIN_VARIANT (subtype);
7752 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7758 subcode = TREE_CODE (subtype);
7759 if (array && subcode != ARRAY_TYPE)
7761 error_init (loc, "array index in non-array initializer");
7764 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7766 error_init (loc, "field name not in record or union initializer");
7770 constructor_designated = 1;
7771 push_init_level (loc, 2, braced_init_obstack);
7775 /* If there are range designators in designator list, push a new designator
7776 to constructor_range_stack. RANGE_END is end of such stack range or
7777 NULL_TREE if there is no range designator at this level. */
7780 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7782 struct constructor_range_stack *p;
7784 p = (struct constructor_range_stack *)
7785 obstack_alloc (braced_init_obstack,
7786 sizeof (struct constructor_range_stack));
7787 p->prev = constructor_range_stack;
7789 p->fields = constructor_fields;
7790 p->range_start = constructor_index;
7791 p->index = constructor_index;
7792 p->stack = constructor_stack;
7793 p->range_end = range_end;
7794 if (constructor_range_stack)
7795 constructor_range_stack->next = p;
7796 constructor_range_stack = p;
7799 /* Within an array initializer, specify the next index to be initialized.
7800 FIRST is that index. If LAST is nonzero, then initialize a range
7801 of indices, running from FIRST through LAST. */
7804 set_init_index (location_t loc, tree first, tree last,
7805 struct obstack *braced_init_obstack)
7807 if (set_designator (loc, 1, braced_init_obstack))
7810 designator_erroneous = 1;
7812 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7813 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7815 error_init (loc, "array index in initializer not of integer type");
7819 if (TREE_CODE (first) != INTEGER_CST)
7821 first = c_fully_fold (first, false, NULL);
7822 if (TREE_CODE (first) == INTEGER_CST)
7823 pedwarn_init (loc, OPT_Wpedantic,
7824 "array index in initializer is not "
7825 "an integer constant expression");
7828 if (last && TREE_CODE (last) != INTEGER_CST)
7830 last = c_fully_fold (last, false, NULL);
7831 if (TREE_CODE (last) == INTEGER_CST)
7832 pedwarn_init (loc, OPT_Wpedantic,
7833 "array index in initializer is not "
7834 "an integer constant expression");
7837 if (TREE_CODE (first) != INTEGER_CST)
7838 error_init (loc, "nonconstant array index in initializer");
7839 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7840 error_init (loc, "nonconstant array index in initializer");
7841 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7842 error_init (loc, "array index in non-array initializer");
7843 else if (tree_int_cst_sgn (first) == -1)
7844 error_init (loc, "array index in initializer exceeds array bounds");
7845 else if (constructor_max_index
7846 && tree_int_cst_lt (constructor_max_index, first))
7847 error_init (loc, "array index in initializer exceeds array bounds");
7850 constant_expression_warning (first);
7852 constant_expression_warning (last);
7853 constructor_index = convert (bitsizetype, first);
7854 if (tree_int_cst_lt (constructor_index, first))
7856 constructor_index = copy_node (constructor_index);
7857 TREE_OVERFLOW (constructor_index) = 1;
7862 if (tree_int_cst_equal (first, last))
7864 else if (tree_int_cst_lt (last, first))
7866 error_init (loc, "empty index range in initializer");
7871 last = convert (bitsizetype, last);
7872 if (constructor_max_index != 0
7873 && tree_int_cst_lt (constructor_max_index, last))
7875 error_init (loc, "array index range in initializer exceeds "
7883 designator_erroneous = 0;
7884 if (constructor_range_stack || last)
7885 push_range_stack (last, braced_init_obstack);
7889 /* Within a struct initializer, specify the next field to be initialized. */
7892 set_init_label (location_t loc, tree fieldname,
7893 struct obstack *braced_init_obstack)
7897 if (set_designator (loc, 0, braced_init_obstack))
7900 designator_erroneous = 1;
7902 if (TREE_CODE (constructor_type) != RECORD_TYPE
7903 && TREE_CODE (constructor_type) != UNION_TYPE)
7905 error_init (loc, "field name not in record or union initializer");
7909 field = lookup_field (constructor_type, fieldname);
7912 error_at (loc, "unknown field %qE specified in initializer", fieldname);
7916 constructor_fields = TREE_VALUE (field);
7918 designator_erroneous = 0;
7919 if (constructor_range_stack)
7920 push_range_stack (NULL_TREE, braced_init_obstack);
7921 field = TREE_CHAIN (field);
7924 if (set_designator (loc, 0, braced_init_obstack))
7928 while (field != NULL_TREE);
7931 /* Add a new initializer to the tree of pending initializers. PURPOSE
7932 identifies the initializer, either array index or field in a structure.
7933 VALUE is the value of that index or field. If ORIGTYPE is not
7934 NULL_TREE, it is the original type of VALUE.
7936 IMPLICIT is true if value comes from pop_init_level (1),
7937 the new initializer has been merged with the existing one
7938 and thus no warnings should be emitted about overriding an
7939 existing initializer. */
7942 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7943 bool implicit, struct obstack *braced_init_obstack)
7945 struct init_node *p, **q, *r;
7947 q = &constructor_pending_elts;
7950 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7955 if (tree_int_cst_lt (purpose, p->purpose))
7957 else if (tree_int_cst_lt (p->purpose, purpose))
7963 if (TREE_SIDE_EFFECTS (p->value))
7964 warning_init (loc, OPT_Woverride_init_side_effects,
7965 "initialized field with side-effects "
7967 else if (warn_override_init)
7968 warning_init (loc, OPT_Woverride_init,
7969 "initialized field overwritten");
7972 p->origtype = origtype;
7981 bitpos = bit_position (purpose);
7985 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7987 else if (p->purpose != purpose)
7993 if (TREE_SIDE_EFFECTS (p->value))
7994 warning_init (loc, OPT_Woverride_init_side_effects,
7995 "initialized field with side-effects "
7997 else if (warn_override_init)
7998 warning_init (loc, OPT_Woverride_init,
7999 "initialized field overwritten");
8002 p->origtype = origtype;
8008 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8009 sizeof (struct init_node));
8010 r->purpose = purpose;
8012 r->origtype = origtype;
8022 struct init_node *s;
8026 if (p->balance == 0)
8028 else if (p->balance < 0)
8035 p->left->parent = p;
8052 constructor_pending_elts = r;
8057 struct init_node *t = r->right;
8061 r->right->parent = r;
8066 p->left->parent = p;
8069 p->balance = t->balance < 0;
8070 r->balance = -(t->balance > 0);
8085 constructor_pending_elts = t;
8091 /* p->balance == +1; growth of left side balances the node. */
8096 else /* r == p->right */
8098 if (p->balance == 0)
8099 /* Growth propagation from right side. */
8101 else if (p->balance > 0)
8108 p->right->parent = p;
8125 constructor_pending_elts = r;
8127 else /* r->balance == -1 */
8130 struct init_node *t = r->left;
8134 r->left->parent = r;
8139 p->right->parent = p;
8142 r->balance = (t->balance < 0);
8143 p->balance = -(t->balance > 0);
8158 constructor_pending_elts = t;
8164 /* p->balance == -1; growth of right side balances the node. */
8175 /* Build AVL tree from a sorted chain. */
8178 set_nonincremental_init (struct obstack * braced_init_obstack)
8180 unsigned HOST_WIDE_INT ix;
8183 if (TREE_CODE (constructor_type) != RECORD_TYPE
8184 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8187 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8188 add_pending_init (input_location, index, value, NULL_TREE, true,
8189 braced_init_obstack);
8190 constructor_elements = NULL;
8191 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8193 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8194 /* Skip any nameless bit fields at the beginning. */
8195 while (constructor_unfilled_fields != 0
8196 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8197 && DECL_NAME (constructor_unfilled_fields) == 0)
8198 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8201 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8203 if (TYPE_DOMAIN (constructor_type))
8204 constructor_unfilled_index
8205 = convert (bitsizetype,
8206 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8208 constructor_unfilled_index = bitsize_zero_node;
8210 constructor_incremental = 0;
8213 /* Build AVL tree from a string constant. */
8216 set_nonincremental_init_from_string (tree str,
8217 struct obstack * braced_init_obstack)
8219 tree value, purpose, type;
8220 HOST_WIDE_INT val[2];
8221 const char *p, *end;
8222 int byte, wchar_bytes, charwidth, bitpos;
8224 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8226 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8227 charwidth = TYPE_PRECISION (char_type_node);
8228 type = TREE_TYPE (constructor_type);
8229 p = TREE_STRING_POINTER (str);
8230 end = p + TREE_STRING_LENGTH (str);
8232 for (purpose = bitsize_zero_node;
8234 && !(constructor_max_index
8235 && tree_int_cst_lt (constructor_max_index, purpose));
8236 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8238 if (wchar_bytes == 1)
8240 val[0] = (unsigned char) *p++;
8247 for (byte = 0; byte < wchar_bytes; byte++)
8249 if (BYTES_BIG_ENDIAN)
8250 bitpos = (wchar_bytes - byte - 1) * charwidth;
8252 bitpos = byte * charwidth;
8253 val[bitpos % HOST_BITS_PER_WIDE_INT]
8254 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8255 << (bitpos % HOST_BITS_PER_WIDE_INT);
8259 if (!TYPE_UNSIGNED (type))
8261 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8262 if (bitpos < HOST_BITS_PER_WIDE_INT)
8264 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8266 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8270 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8275 else if (val[1] & (((HOST_WIDE_INT) 1)
8276 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8277 val[1] |= ((HOST_WIDE_INT) -1)
8278 << (bitpos - HOST_BITS_PER_WIDE_INT);
8281 value = wide_int_to_tree (type,
8282 wide_int::from_array (val, 2,
8283 HOST_BITS_PER_WIDE_INT * 2));
8284 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8285 braced_init_obstack);
8288 constructor_incremental = 0;
8291 /* Return value of FIELD in pending initializer or zero if the field was
8292 not initialized yet. */
8295 find_init_member (tree field, struct obstack * braced_init_obstack)
8297 struct init_node *p;
8299 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8301 if (constructor_incremental
8302 && tree_int_cst_lt (field, constructor_unfilled_index))
8303 set_nonincremental_init (braced_init_obstack);
8305 p = constructor_pending_elts;
8308 if (tree_int_cst_lt (field, p->purpose))
8310 else if (tree_int_cst_lt (p->purpose, field))
8316 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8318 tree bitpos = bit_position (field);
8320 if (constructor_incremental
8321 && (!constructor_unfilled_fields
8322 || tree_int_cst_lt (bitpos,
8323 bit_position (constructor_unfilled_fields))))
8324 set_nonincremental_init (braced_init_obstack);
8326 p = constructor_pending_elts;
8329 if (field == p->purpose)
8331 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8337 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8339 if (!vec_safe_is_empty (constructor_elements)
8340 && (constructor_elements->last ().index == field))
8341 return constructor_elements->last ().value;
8346 /* "Output" the next constructor element.
8347 At top level, really output it to assembler code now.
8348 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8349 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8350 TYPE is the data type that the containing data type wants here.
8351 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8352 If VALUE is a string constant, STRICT_STRING is true if it is
8353 unparenthesized or we should not warn here for it being parenthesized.
8354 For other types of VALUE, STRICT_STRING is not used.
8356 PENDING if non-nil means output pending elements that belong
8357 right after this element. (PENDING is normally 1;
8358 it is 0 while outputting pending elements, to avoid recursion.)
8360 IMPLICIT is true if value comes from pop_init_level (1),
8361 the new initializer has been merged with the existing one
8362 and thus no warnings should be emitted about overriding an
8363 existing initializer. */
8366 output_init_element (location_t loc, tree value, tree origtype,
8367 bool strict_string, tree type, tree field, int pending,
8368 bool implicit, struct obstack * braced_init_obstack)
8370 tree semantic_type = NULL_TREE;
8371 bool maybe_const = true;
8374 if (type == error_mark_node || value == error_mark_node)
8376 constructor_erroneous = 1;
8379 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8380 && (TREE_CODE (value) == STRING_CST
8381 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8382 && !(TREE_CODE (value) == STRING_CST
8383 && TREE_CODE (type) == ARRAY_TYPE
8384 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8385 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8386 TYPE_MAIN_VARIANT (type)))
8387 value = array_to_pointer_conversion (input_location, value);
8389 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8390 && require_constant_value && pending)
8392 /* As an extension, allow initializing objects with static storage
8393 duration with compound literals (which are then treated just as
8394 the brace enclosed list they contain). */
8396 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8398 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8399 value = DECL_INITIAL (decl);
8402 npc = null_pointer_constant_p (value);
8403 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8405 semantic_type = TREE_TYPE (value);
8406 value = TREE_OPERAND (value, 0);
8408 value = c_fully_fold (value, require_constant_value, &maybe_const);
8410 if (value == error_mark_node)
8411 constructor_erroneous = 1;
8412 else if (!TREE_CONSTANT (value))
8413 constructor_constant = 0;
8414 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8415 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8416 || TREE_CODE (constructor_type) == UNION_TYPE)
8417 && DECL_C_BIT_FIELD (field)
8418 && TREE_CODE (value) != INTEGER_CST))
8419 constructor_simple = 0;
8421 constructor_nonconst = 1;
8423 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8425 if (require_constant_value)
8427 error_init (loc, "initializer element is not constant");
8428 value = error_mark_node;
8430 else if (require_constant_elements)
8431 pedwarn (loc, OPT_Wpedantic,
8432 "initializer element is not computable at load time");
8434 else if (!maybe_const
8435 && (require_constant_value || require_constant_elements))
8436 pedwarn_init (loc, OPT_Wpedantic,
8437 "initializer element is not a constant expression");
8439 /* Issue -Wc++-compat warnings about initializing a bitfield with
8442 && field != NULL_TREE
8443 && TREE_CODE (field) == FIELD_DECL
8444 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8445 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8446 != TYPE_MAIN_VARIANT (type))
8447 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8449 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8450 if (checktype != error_mark_node
8451 && (TYPE_MAIN_VARIANT (checktype)
8452 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8453 warning_init (loc, OPT_Wc___compat,
8454 "enum conversion in initialization is invalid in C++");
8457 /* If this field is empty (and not at the end of structure),
8458 don't do anything other than checking the initializer. */
8460 && (TREE_TYPE (field) == error_mark_node
8461 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8462 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8463 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8464 || DECL_CHAIN (field)))))
8468 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8469 value = digest_init (loc, type, value, origtype, npc, strict_string,
8470 require_constant_value);
8471 if (value == error_mark_node)
8473 constructor_erroneous = 1;
8476 if (require_constant_value || require_constant_elements)
8477 constant_expression_warning (value);
8479 /* If this element doesn't come next in sequence,
8480 put it on constructor_pending_elts. */
8481 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8482 && (!constructor_incremental
8483 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8485 if (constructor_incremental
8486 && tree_int_cst_lt (field, constructor_unfilled_index))
8487 set_nonincremental_init (braced_init_obstack);
8489 add_pending_init (loc, field, value, origtype, implicit,
8490 braced_init_obstack);
8493 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8494 && (!constructor_incremental
8495 || field != constructor_unfilled_fields))
8497 /* We do this for records but not for unions. In a union,
8498 no matter which field is specified, it can be initialized
8499 right away since it starts at the beginning of the union. */
8500 if (constructor_incremental)
8502 if (!constructor_unfilled_fields)
8503 set_nonincremental_init (braced_init_obstack);
8506 tree bitpos, unfillpos;
8508 bitpos = bit_position (field);
8509 unfillpos = bit_position (constructor_unfilled_fields);
8511 if (tree_int_cst_lt (bitpos, unfillpos))
8512 set_nonincremental_init (braced_init_obstack);
8516 add_pending_init (loc, field, value, origtype, implicit,
8517 braced_init_obstack);
8520 else if (TREE_CODE (constructor_type) == UNION_TYPE
8521 && !vec_safe_is_empty (constructor_elements))
8525 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8526 warning_init (loc, OPT_Woverride_init_side_effects,
8527 "initialized field with side-effects overwritten");
8528 else if (warn_override_init)
8529 warning_init (loc, OPT_Woverride_init,
8530 "initialized field overwritten");
8533 /* We can have just one union field set. */
8534 constructor_elements = NULL;
8537 /* Otherwise, output this element either to
8538 constructor_elements or to the assembler file. */
8540 constructor_elt celt = {field, value};
8541 vec_safe_push (constructor_elements, celt);
8543 /* Advance the variable that indicates sequential elements output. */
8544 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8545 constructor_unfilled_index
8546 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8548 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8550 constructor_unfilled_fields
8551 = DECL_CHAIN (constructor_unfilled_fields);
8553 /* Skip any nameless bit fields. */
8554 while (constructor_unfilled_fields != 0
8555 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8556 && DECL_NAME (constructor_unfilled_fields) == 0)
8557 constructor_unfilled_fields =
8558 DECL_CHAIN (constructor_unfilled_fields);
8560 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8561 constructor_unfilled_fields = 0;
8563 /* Now output any pending elements which have become next. */
8565 output_pending_init_elements (0, braced_init_obstack);
8568 /* Output any pending elements which have become next.
8569 As we output elements, constructor_unfilled_{fields,index}
8570 advances, which may cause other elements to become next;
8571 if so, they too are output.
8573 If ALL is 0, we return when there are
8574 no more pending elements to output now.
8576 If ALL is 1, we output space as necessary so that
8577 we can output all the pending elements. */
8579 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8581 struct init_node *elt = constructor_pending_elts;
8586 /* Look through the whole pending tree.
8587 If we find an element that should be output now,
8588 output it. Otherwise, set NEXT to the element
8589 that comes first among those still pending. */
8594 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8596 if (tree_int_cst_equal (elt->purpose,
8597 constructor_unfilled_index))
8598 output_init_element (input_location, elt->value, elt->origtype,
8599 true, TREE_TYPE (constructor_type),
8600 constructor_unfilled_index, 0, false,
8601 braced_init_obstack);
8602 else if (tree_int_cst_lt (constructor_unfilled_index,
8605 /* Advance to the next smaller node. */
8610 /* We have reached the smallest node bigger than the
8611 current unfilled index. Fill the space first. */
8612 next = elt->purpose;
8618 /* Advance to the next bigger node. */
8623 /* We have reached the biggest node in a subtree. Find
8624 the parent of it, which is the next bigger node. */
8625 while (elt->parent && elt->parent->right == elt)
8628 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8631 next = elt->purpose;
8637 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8638 || TREE_CODE (constructor_type) == UNION_TYPE)
8640 tree ctor_unfilled_bitpos, elt_bitpos;
8642 /* If the current record is complete we are done. */
8643 if (constructor_unfilled_fields == 0)
8646 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8647 elt_bitpos = bit_position (elt->purpose);
8648 /* We can't compare fields here because there might be empty
8649 fields in between. */
8650 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8652 constructor_unfilled_fields = elt->purpose;
8653 output_init_element (input_location, elt->value, elt->origtype,
8654 true, TREE_TYPE (elt->purpose),
8655 elt->purpose, 0, false,
8656 braced_init_obstack);
8658 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8660 /* Advance to the next smaller node. */
8665 /* We have reached the smallest node bigger than the
8666 current unfilled field. Fill the space first. */
8667 next = elt->purpose;
8673 /* Advance to the next bigger node. */
8678 /* We have reached the biggest node in a subtree. Find
8679 the parent of it, which is the next bigger node. */
8680 while (elt->parent && elt->parent->right == elt)
8684 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8685 bit_position (elt->purpose))))
8687 next = elt->purpose;
8695 /* Ordinarily return, but not if we want to output all
8696 and there are elements left. */
8697 if (!(all && next != 0))
8700 /* If it's not incremental, just skip over the gap, so that after
8701 jumping to retry we will output the next successive element. */
8702 if (TREE_CODE (constructor_type) == RECORD_TYPE
8703 || TREE_CODE (constructor_type) == UNION_TYPE)
8704 constructor_unfilled_fields = next;
8705 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8706 constructor_unfilled_index = next;
8708 /* ELT now points to the node in the pending tree with the next
8709 initializer to output. */
8713 /* Add one non-braced element to the current constructor level.
8714 This adjusts the current position within the constructor's type.
8715 This may also start or terminate implicit levels
8716 to handle a partly-braced initializer.
8718 Once this has found the correct level for the new element,
8719 it calls output_init_element.
8721 IMPLICIT is true if value comes from pop_init_level (1),
8722 the new initializer has been merged with the existing one
8723 and thus no warnings should be emitted about overriding an
8724 existing initializer. */
8727 process_init_element (location_t loc, struct c_expr value, bool implicit,
8728 struct obstack * braced_init_obstack)
8730 tree orig_value = value.value;
8731 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8732 bool strict_string = value.original_code == STRING_CST;
8733 bool was_designated = designator_depth != 0;
8735 designator_depth = 0;
8736 designator_erroneous = 0;
8738 if (!implicit && value.value && !integer_zerop (value.value))
8739 constructor_zeroinit = 0;
8741 /* Handle superfluous braces around string cst as in
8742 char x[] = {"foo"}; */
8746 && TREE_CODE (constructor_type) == ARRAY_TYPE
8747 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8748 && integer_zerop (constructor_unfilled_index))
8750 if (constructor_stack->replacement_value.value)
8751 error_init (loc, "excess elements in char array initializer");
8752 constructor_stack->replacement_value = value;
8756 if (constructor_stack->replacement_value.value != 0)
8758 error_init (loc, "excess elements in struct initializer");
8762 /* Ignore elements of a brace group if it is entirely superfluous
8763 and has already been diagnosed. */
8764 if (constructor_type == 0)
8767 if (!implicit && warn_designated_init && !was_designated
8768 && TREE_CODE (constructor_type) == RECORD_TYPE
8769 && lookup_attribute ("designated_init",
8770 TYPE_ATTRIBUTES (constructor_type)))
8772 OPT_Wdesignated_init,
8773 "positional initialization of field "
8774 "in %<struct%> declared with %<designated_init%> attribute");
8776 /* If we've exhausted any levels that didn't have braces,
8778 while (constructor_stack->implicit)
8780 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8781 || TREE_CODE (constructor_type) == UNION_TYPE)
8782 && constructor_fields == 0)
8783 process_init_element (loc,
8784 pop_init_level (loc, 1, braced_init_obstack),
8785 true, braced_init_obstack);
8786 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8787 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8788 && constructor_max_index
8789 && tree_int_cst_lt (constructor_max_index,
8791 process_init_element (loc,
8792 pop_init_level (loc, 1, braced_init_obstack),
8793 true, braced_init_obstack);
8798 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8799 if (constructor_range_stack)
8801 /* If value is a compound literal and we'll be just using its
8802 content, don't put it into a SAVE_EXPR. */
8803 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8804 || !require_constant_value)
8806 tree semantic_type = NULL_TREE;
8807 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8809 semantic_type = TREE_TYPE (value.value);
8810 value.value = TREE_OPERAND (value.value, 0);
8812 value.value = c_save_expr (value.value);
8814 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8821 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8824 enum tree_code fieldcode;
8826 if (constructor_fields == 0)
8828 pedwarn_init (loc, 0, "excess elements in struct initializer");
8832 fieldtype = TREE_TYPE (constructor_fields);
8833 if (fieldtype != error_mark_node)
8834 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8835 fieldcode = TREE_CODE (fieldtype);
8837 /* Error for non-static initialization of a flexible array member. */
8838 if (fieldcode == ARRAY_TYPE
8839 && !require_constant_value
8840 && TYPE_SIZE (fieldtype) == NULL_TREE
8841 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8843 error_init (loc, "non-static initialization of a flexible "
8848 /* Error for initialization of a flexible array member with
8849 a string constant if the structure is in an array. E.g.:
8850 struct S { int x; char y[]; };
8851 struct S s[] = { { 1, "foo" } };
8854 && fieldcode == ARRAY_TYPE
8855 && constructor_depth > 1
8856 && TYPE_SIZE (fieldtype) == NULL_TREE
8857 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8859 bool in_array_p = false;
8860 for (struct constructor_stack *p = constructor_stack;
8861 p && p->type; p = p->next)
8862 if (TREE_CODE (p->type) == ARRAY_TYPE)
8869 error_init (loc, "initialization of flexible array "
8870 "member in a nested context");
8875 /* Accept a string constant to initialize a subarray. */
8876 if (value.value != 0
8877 && fieldcode == ARRAY_TYPE
8878 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8880 value.value = orig_value;
8881 /* Otherwise, if we have come to a subaggregate,
8882 and we don't have an element of its type, push into it. */
8883 else if (value.value != 0
8884 && value.value != error_mark_node
8885 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8886 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8887 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8889 push_init_level (loc, 1, braced_init_obstack);
8895 push_member_name (constructor_fields);
8896 output_init_element (loc, value.value, value.original_type,
8897 strict_string, fieldtype,
8898 constructor_fields, 1, implicit,
8899 braced_init_obstack);
8900 RESTORE_SPELLING_DEPTH (constructor_depth);
8903 /* Do the bookkeeping for an element that was
8904 directly output as a constructor. */
8906 /* For a record, keep track of end position of last field. */
8907 if (DECL_SIZE (constructor_fields))
8908 constructor_bit_index
8909 = size_binop_loc (input_location, PLUS_EXPR,
8910 bit_position (constructor_fields),
8911 DECL_SIZE (constructor_fields));
8913 /* If the current field was the first one not yet written out,
8914 it isn't now, so update. */
8915 if (constructor_unfilled_fields == constructor_fields)
8917 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8918 /* Skip any nameless bit fields. */
8919 while (constructor_unfilled_fields != 0
8920 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8921 && DECL_NAME (constructor_unfilled_fields) == 0)
8922 constructor_unfilled_fields =
8923 DECL_CHAIN (constructor_unfilled_fields);
8927 constructor_fields = DECL_CHAIN (constructor_fields);
8928 /* Skip any nameless bit fields at the beginning. */
8929 while (constructor_fields != 0
8930 && DECL_C_BIT_FIELD (constructor_fields)
8931 && DECL_NAME (constructor_fields) == 0)
8932 constructor_fields = DECL_CHAIN (constructor_fields);
8934 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8937 enum tree_code fieldcode;
8939 if (constructor_fields == 0)
8941 pedwarn_init (loc, 0,
8942 "excess elements in union initializer");
8946 fieldtype = TREE_TYPE (constructor_fields);
8947 if (fieldtype != error_mark_node)
8948 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8949 fieldcode = TREE_CODE (fieldtype);
8951 /* Warn that traditional C rejects initialization of unions.
8952 We skip the warning if the value is zero. This is done
8953 under the assumption that the zero initializer in user
8954 code appears conditioned on e.g. __STDC__ to avoid
8955 "missing initializer" warnings and relies on default
8956 initialization to zero in the traditional C case.
8957 We also skip the warning if the initializer is designated,
8958 again on the assumption that this must be conditional on
8959 __STDC__ anyway (and we've already complained about the
8960 member-designator already). */
8961 if (!in_system_header_at (input_location) && !constructor_designated
8962 && !(value.value && (integer_zerop (value.value)
8963 || real_zerop (value.value))))
8964 warning (OPT_Wtraditional, "traditional C rejects initialization "
8967 /* Accept a string constant to initialize a subarray. */
8968 if (value.value != 0
8969 && fieldcode == ARRAY_TYPE
8970 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8972 value.value = orig_value;
8973 /* Otherwise, if we have come to a subaggregate,
8974 and we don't have an element of its type, push into it. */
8975 else if (value.value != 0
8976 && value.value != error_mark_node
8977 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8978 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8979 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8981 push_init_level (loc, 1, braced_init_obstack);
8987 push_member_name (constructor_fields);
8988 output_init_element (loc, value.value, value.original_type,
8989 strict_string, fieldtype,
8990 constructor_fields, 1, implicit,
8991 braced_init_obstack);
8992 RESTORE_SPELLING_DEPTH (constructor_depth);
8995 /* Do the bookkeeping for an element that was
8996 directly output as a constructor. */
8998 constructor_bit_index = DECL_SIZE (constructor_fields);
8999 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9002 constructor_fields = 0;
9004 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9006 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9007 enum tree_code eltcode = TREE_CODE (elttype);
9009 /* Accept a string constant to initialize a subarray. */
9010 if (value.value != 0
9011 && eltcode == ARRAY_TYPE
9012 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9014 value.value = orig_value;
9015 /* Otherwise, if we have come to a subaggregate,
9016 and we don't have an element of its type, push into it. */
9017 else if (value.value != 0
9018 && value.value != error_mark_node
9019 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9020 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9021 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9023 push_init_level (loc, 1, braced_init_obstack);
9027 if (constructor_max_index != 0
9028 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9029 || integer_all_onesp (constructor_max_index)))
9031 pedwarn_init (loc, 0,
9032 "excess elements in array initializer");
9036 /* Now output the actual element. */
9039 push_array_bounds (tree_to_uhwi (constructor_index));
9040 output_init_element (loc, value.value, value.original_type,
9041 strict_string, elttype,
9042 constructor_index, 1, implicit,
9043 braced_init_obstack);
9044 RESTORE_SPELLING_DEPTH (constructor_depth);
9048 = size_binop_loc (input_location, PLUS_EXPR,
9049 constructor_index, bitsize_one_node);
9052 /* If we are doing the bookkeeping for an element that was
9053 directly output as a constructor, we must update
9054 constructor_unfilled_index. */
9055 constructor_unfilled_index = constructor_index;
9057 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9059 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9061 /* Do a basic check of initializer size. Note that vectors
9062 always have a fixed size derived from their type. */
9063 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9065 pedwarn_init (loc, 0,
9066 "excess elements in vector initializer");
9070 /* Now output the actual element. */
9073 if (TREE_CODE (value.value) == VECTOR_CST)
9074 elttype = TYPE_MAIN_VARIANT (constructor_type);
9075 output_init_element (loc, value.value, value.original_type,
9076 strict_string, elttype,
9077 constructor_index, 1, implicit,
9078 braced_init_obstack);
9082 = size_binop_loc (input_location,
9083 PLUS_EXPR, constructor_index, bitsize_one_node);
9086 /* If we are doing the bookkeeping for an element that was
9087 directly output as a constructor, we must update
9088 constructor_unfilled_index. */
9089 constructor_unfilled_index = constructor_index;
9092 /* Handle the sole element allowed in a braced initializer
9093 for a scalar variable. */
9094 else if (constructor_type != error_mark_node
9095 && constructor_fields == 0)
9097 pedwarn_init (loc, 0,
9098 "excess elements in scalar initializer");
9104 output_init_element (loc, value.value, value.original_type,
9105 strict_string, constructor_type,
9106 NULL_TREE, 1, implicit,
9107 braced_init_obstack);
9108 constructor_fields = 0;
9111 /* Handle range initializers either at this level or anywhere higher
9112 in the designator stack. */
9113 if (constructor_range_stack)
9115 struct constructor_range_stack *p, *range_stack;
9118 range_stack = constructor_range_stack;
9119 constructor_range_stack = 0;
9120 while (constructor_stack != range_stack->stack)
9122 gcc_assert (constructor_stack->implicit);
9123 process_init_element (loc,
9124 pop_init_level (loc, 1,
9125 braced_init_obstack),
9126 true, braced_init_obstack);
9128 for (p = range_stack;
9129 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9132 gcc_assert (constructor_stack->implicit);
9133 process_init_element (loc,
9134 pop_init_level (loc, 1,
9135 braced_init_obstack),
9136 true, braced_init_obstack);
9139 p->index = size_binop_loc (input_location,
9140 PLUS_EXPR, p->index, bitsize_one_node);
9141 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9146 constructor_index = p->index;
9147 constructor_fields = p->fields;
9148 if (finish && p->range_end && p->index == p->range_start)
9156 push_init_level (loc, 2, braced_init_obstack);
9157 p->stack = constructor_stack;
9158 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9159 p->index = p->range_start;
9163 constructor_range_stack = range_stack;
9170 constructor_range_stack = 0;
9173 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9174 (guaranteed to be 'volatile' or null) and ARGS (represented using
9175 an ASM_EXPR node). */
9177 build_asm_stmt (tree cv_qualifier, tree args)
9179 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9180 ASM_VOLATILE_P (args) = 1;
9181 return add_stmt (args);
9184 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9185 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9186 SIMPLE indicates whether there was anything at all after the
9187 string in the asm expression -- asm("blah") and asm("blah" : )
9188 are subtly different. We use a ASM_EXPR node to represent this. */
9190 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9191 tree clobbers, tree labels, bool simple)
9196 const char *constraint;
9197 const char **oconstraints;
9198 bool allows_mem, allows_reg, is_inout;
9199 int ninputs, noutputs;
9201 ninputs = list_length (inputs);
9202 noutputs = list_length (outputs);
9203 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9205 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9207 /* Remove output conversions that change the type but not the mode. */
9208 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9210 tree output = TREE_VALUE (tail);
9212 output = c_fully_fold (output, false, NULL);
9214 /* ??? Really, this should not be here. Users should be using a
9215 proper lvalue, dammit. But there's a long history of using casts
9216 in the output operands. In cases like longlong.h, this becomes a
9217 primitive form of typechecking -- if the cast can be removed, then
9218 the output operand had a type of the proper width; otherwise we'll
9219 get an error. Gross, but ... */
9220 STRIP_NOPS (output);
9222 if (!lvalue_or_else (loc, output, lv_asm))
9223 output = error_mark_node;
9225 if (output != error_mark_node
9226 && (TREE_READONLY (output)
9227 || TYPE_READONLY (TREE_TYPE (output))
9228 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9229 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9230 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9231 readonly_error (loc, output, lv_asm);
9233 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9234 oconstraints[i] = constraint;
9236 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9237 &allows_mem, &allows_reg, &is_inout))
9239 /* If the operand is going to end up in memory,
9240 mark it addressable. */
9241 if (!allows_reg && !c_mark_addressable (output))
9242 output = error_mark_node;
9243 if (!(!allows_reg && allows_mem)
9244 && output != error_mark_node
9245 && VOID_TYPE_P (TREE_TYPE (output)))
9247 error_at (loc, "invalid use of void expression");
9248 output = error_mark_node;
9252 output = error_mark_node;
9254 TREE_VALUE (tail) = output;
9257 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9261 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9262 input = TREE_VALUE (tail);
9264 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9265 oconstraints, &allows_mem, &allows_reg))
9267 /* If the operand is going to end up in memory,
9268 mark it addressable. */
9269 if (!allows_reg && allows_mem)
9271 input = c_fully_fold (input, false, NULL);
9273 /* Strip the nops as we allow this case. FIXME, this really
9274 should be rejected or made deprecated. */
9276 if (!c_mark_addressable (input))
9277 input = error_mark_node;
9282 memset (&expr, 0, sizeof (expr));
9284 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9285 input = c_fully_fold (expr.value, false, NULL);
9287 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9289 error_at (loc, "invalid use of void expression");
9290 input = error_mark_node;
9295 input = error_mark_node;
9297 TREE_VALUE (tail) = input;
9300 /* ASMs with labels cannot have outputs. This should have been
9301 enforced by the parser. */
9302 gcc_assert (outputs == NULL || labels == NULL);
9304 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9306 /* asm statements without outputs, including simple ones, are treated
9308 ASM_INPUT_P (args) = simple;
9309 ASM_VOLATILE_P (args) = (noutputs == 0);
9314 /* Generate a goto statement to LABEL. LOC is the location of the
9318 c_finish_goto_label (location_t loc, tree label)
9320 tree decl = lookup_label_for_goto (loc, label);
9323 TREE_USED (decl) = 1;
9325 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9326 SET_EXPR_LOCATION (t, loc);
9327 return add_stmt (t);
9331 /* Generate a computed goto statement to EXPR. LOC is the location of
9335 c_finish_goto_ptr (location_t loc, tree expr)
9338 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9339 expr = c_fully_fold (expr, false, NULL);
9340 expr = convert (ptr_type_node, expr);
9341 t = build1 (GOTO_EXPR, void_type_node, expr);
9342 SET_EXPR_LOCATION (t, loc);
9343 return add_stmt (t);
9346 /* Generate a C `return' statement. RETVAL is the expression for what
9347 to return, or a null pointer for `return;' with no value. LOC is
9348 the location of the return statement, or the location of the expression,
9349 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9350 is the original type of RETVAL. */
9353 c_finish_return (location_t loc, tree retval, tree origtype)
9355 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9356 bool no_warning = false;
9360 if (TREE_THIS_VOLATILE (current_function_decl))
9362 "function declared %<noreturn%> has a %<return%> statement");
9364 if (flag_cilkplus && contains_array_notation_expr (retval))
9366 /* Array notations are allowed in a return statement if it is inside a
9367 built-in array notation reduction function. */
9368 if (!find_rank (loc, retval, retval, false, &rank))
9369 return error_mark_node;
9372 error_at (loc, "array notation expression cannot be used as a "
9374 return error_mark_node;
9377 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9379 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9381 return error_mark_node;
9385 tree semantic_type = NULL_TREE;
9386 npc = null_pointer_constant_p (retval);
9387 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9389 semantic_type = TREE_TYPE (retval);
9390 retval = TREE_OPERAND (retval, 0);
9392 retval = c_fully_fold (retval, false, NULL);
9394 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9399 current_function_returns_null = 1;
9400 if ((warn_return_type || flag_isoc99)
9401 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9404 pedwarn (loc, 0, "%<return%> with no value, in "
9405 "function returning non-void");
9407 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9408 "in function returning non-void");
9412 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9414 current_function_returns_null = 1;
9415 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9417 "%<return%> with a value, in function returning void");
9419 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9420 "%<return%> with expression, in function returning void");
9424 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9425 retval, origtype, ic_return,
9426 npc, NULL_TREE, NULL_TREE, 0);
9427 tree res = DECL_RESULT (current_function_decl);
9431 current_function_returns_value = 1;
9432 if (t == error_mark_node)
9435 save = in_late_binary_op;
9436 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9437 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9438 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9439 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9440 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9441 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9442 in_late_binary_op = true;
9443 inner = t = convert (TREE_TYPE (res), t);
9444 in_late_binary_op = save;
9446 /* Strip any conversions, additions, and subtractions, and see if
9447 we are returning the address of a local variable. Warn if so. */
9450 switch (TREE_CODE (inner))
9453 case NON_LVALUE_EXPR:
9455 case POINTER_PLUS_EXPR:
9456 inner = TREE_OPERAND (inner, 0);
9460 /* If the second operand of the MINUS_EXPR has a pointer
9461 type (or is converted from it), this may be valid, so
9462 don't give a warning. */
9464 tree op1 = TREE_OPERAND (inner, 1);
9466 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9467 && (CONVERT_EXPR_P (op1)
9468 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9469 op1 = TREE_OPERAND (op1, 0);
9471 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9474 inner = TREE_OPERAND (inner, 0);
9479 inner = TREE_OPERAND (inner, 0);
9481 while (REFERENCE_CLASS_P (inner)
9482 && TREE_CODE (inner) != INDIRECT_REF)
9483 inner = TREE_OPERAND (inner, 0);
9486 && !DECL_EXTERNAL (inner)
9487 && !TREE_STATIC (inner)
9488 && DECL_CONTEXT (inner) == current_function_decl)
9490 if (TREE_CODE (inner) == LABEL_DECL)
9491 warning_at (loc, OPT_Wreturn_local_addr,
9492 "function returns address of label");
9495 warning_at (loc, OPT_Wreturn_local_addr,
9496 "function returns address of local variable");
9497 tree zero = build_zero_cst (TREE_TYPE (res));
9498 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9510 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9511 SET_EXPR_LOCATION (retval, loc);
9513 if (warn_sequence_point)
9514 verify_sequence_points (retval);
9517 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9518 TREE_NO_WARNING (ret_stmt) |= no_warning;
9519 return add_stmt (ret_stmt);
9523 /* The SWITCH_EXPR being built. */
9526 /* The original type of the testing expression, i.e. before the
9527 default conversion is applied. */
9530 /* A splay-tree mapping the low element of a case range to the high
9531 element, or NULL_TREE if there is no high element. Used to
9532 determine whether or not a new case label duplicates an old case
9533 label. We need a tree, rather than simply a hash table, because
9534 of the GNU case range extension. */
9537 /* The bindings at the point of the switch. This is used for
9538 warnings crossing decls when branching to a case label. */
9539 struct c_spot_bindings *bindings;
9541 /* The next node on the stack. */
9542 struct c_switch *next;
9545 /* A stack of the currently active switch statements. The innermost
9546 switch statement is on the top of the stack. There is no need to
9547 mark the stack for garbage collection because it is only active
9548 during the processing of the body of a function, and we never
9549 collect at that point. */
9551 struct c_switch *c_switch_stack;
9553 /* Start a C switch statement, testing expression EXP. Return the new
9554 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9555 SWITCH_COND_LOC is the location of the switch's condition.
9556 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9559 c_start_case (location_t switch_loc,
9560 location_t switch_cond_loc,
9561 tree exp, bool explicit_cast_p)
9563 tree orig_type = error_mark_node;
9564 struct c_switch *cs;
9566 if (exp != error_mark_node)
9568 orig_type = TREE_TYPE (exp);
9570 if (!INTEGRAL_TYPE_P (orig_type))
9572 if (orig_type != error_mark_node)
9574 error_at (switch_cond_loc, "switch quantity not an integer");
9575 orig_type = error_mark_node;
9577 exp = integer_zero_node;
9581 tree type = TYPE_MAIN_VARIANT (orig_type);
9584 /* Warn if the condition has boolean value. */
9585 while (TREE_CODE (e) == COMPOUND_EXPR)
9586 e = TREE_OPERAND (e, 1);
9588 if ((TREE_CODE (type) == BOOLEAN_TYPE
9589 || truth_value_p (TREE_CODE (e)))
9590 /* Explicit cast to int suppresses this warning. */
9591 && !(TREE_CODE (type) == INTEGER_TYPE
9592 && explicit_cast_p))
9593 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9594 "switch condition has boolean value");
9596 if (!in_system_header_at (input_location)
9597 && (type == long_integer_type_node
9598 || type == long_unsigned_type_node))
9599 warning_at (switch_cond_loc,
9600 OPT_Wtraditional, "%<long%> switch expression not "
9601 "converted to %<int%> in ISO C");
9603 exp = c_fully_fold (exp, false, NULL);
9604 exp = default_conversion (exp);
9606 if (warn_sequence_point)
9607 verify_sequence_points (exp);
9611 /* Add this new SWITCH_EXPR to the stack. */
9612 cs = XNEW (struct c_switch);
9613 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9614 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9615 cs->orig_type = orig_type;
9616 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9617 cs->bindings = c_get_switch_bindings ();
9618 cs->next = c_switch_stack;
9619 c_switch_stack = cs;
9621 return add_stmt (cs->switch_expr);
9624 /* Process a case label at location LOC. */
9627 do_case (location_t loc, tree low_value, tree high_value)
9629 tree label = NULL_TREE;
9631 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9633 low_value = c_fully_fold (low_value, false, NULL);
9634 if (TREE_CODE (low_value) == INTEGER_CST)
9635 pedwarn (loc, OPT_Wpedantic,
9636 "case label is not an integer constant expression");
9639 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9641 high_value = c_fully_fold (high_value, false, NULL);
9642 if (TREE_CODE (high_value) == INTEGER_CST)
9643 pedwarn (input_location, OPT_Wpedantic,
9644 "case label is not an integer constant expression");
9647 if (c_switch_stack == NULL)
9650 error_at (loc, "case label not within a switch statement");
9652 error_at (loc, "%<default%> label not within a switch statement");
9656 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9657 EXPR_LOCATION (c_switch_stack->switch_expr),
9661 label = c_add_case_label (loc, c_switch_stack->cases,
9662 SWITCH_COND (c_switch_stack->switch_expr),
9663 c_switch_stack->orig_type,
9664 low_value, high_value);
9665 if (label == error_mark_node)
9670 /* Finish the switch statement. TYPE is the original type of the
9671 controlling expression of the switch, or NULL_TREE. */
9674 c_finish_case (tree body, tree type)
9676 struct c_switch *cs = c_switch_stack;
9677 location_t switch_location;
9679 SWITCH_BODY (cs->switch_expr) = body;
9681 /* Emit warnings as needed. */
9682 switch_location = EXPR_LOCATION (cs->switch_expr);
9683 c_do_switch_warnings (cs->cases, switch_location,
9684 type ? type : TREE_TYPE (cs->switch_expr),
9685 SWITCH_COND (cs->switch_expr));
9687 /* Pop the stack. */
9688 c_switch_stack = cs->next;
9689 splay_tree_delete (cs->cases);
9690 c_release_switch_bindings (cs->bindings);
9694 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9695 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9696 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9697 statement, and was not surrounded with parenthesis. */
9700 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9701 tree else_block, bool nested_if)
9705 /* If the condition has array notations, then the rank of the then_block and
9706 else_block must be either 0 or be equal to the rank of the condition. If
9707 the condition does not have array notations then break them up as it is
9708 broken up in a normal expression. */
9709 if (flag_cilkplus && contains_array_notation_expr (cond))
9711 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9712 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9715 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9718 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9720 if (cond_rank != then_rank && then_rank != 0)
9722 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9723 " and the then-block");
9726 else if (cond_rank != else_rank && else_rank != 0)
9728 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9729 " and the else-block");
9733 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9734 if (warn_parentheses && nested_if && else_block == NULL)
9736 tree inner_if = then_block;
9738 /* We know from the grammar productions that there is an IF nested
9739 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9740 it might not be exactly THEN_BLOCK, but should be the last
9741 non-container statement within. */
9743 switch (TREE_CODE (inner_if))
9748 inner_if = BIND_EXPR_BODY (inner_if);
9750 case STATEMENT_LIST:
9751 inner_if = expr_last (then_block);
9753 case TRY_FINALLY_EXPR:
9754 case TRY_CATCH_EXPR:
9755 inner_if = TREE_OPERAND (inner_if, 0);
9762 if (COND_EXPR_ELSE (inner_if))
9763 warning_at (if_locus, OPT_Wparentheses,
9764 "suggest explicit braces to avoid ambiguous %<else%>");
9767 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9768 SET_EXPR_LOCATION (stmt, if_locus);
9772 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9773 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9774 is false for DO loops. INCR is the FOR increment expression. BODY is
9775 the statement controlled by the loop. BLAB is the break label. CLAB is
9776 the continue label. Everything is allowed to be NULL. */
9779 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9780 tree blab, tree clab, bool cond_is_first)
9782 tree entry = NULL, exit = NULL, t;
9784 /* In theory could forbid cilk spawn for loop increment expression,
9785 but it should work just fine. */
9787 /* If the condition is zero don't generate a loop construct. */
9788 if (cond && integer_zerop (cond))
9792 t = build_and_jump (&blab);
9793 SET_EXPR_LOCATION (t, start_locus);
9799 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9801 /* If we have an exit condition, then we build an IF with gotos either
9802 out of the loop, or to the top of it. If there's no exit condition,
9803 then we just build a jump back to the top. */
9804 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9806 if (cond && !integer_nonzerop (cond))
9808 /* Canonicalize the loop condition to the end. This means
9809 generating a branch to the loop condition. Reuse the
9810 continue label, if possible. */
9815 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9816 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9819 t = build1 (GOTO_EXPR, void_type_node, clab);
9820 SET_EXPR_LOCATION (t, start_locus);
9824 t = build_and_jump (&blab);
9826 exit = fold_build3_loc (start_locus,
9827 COND_EXPR, void_type_node, cond, exit, t);
9829 exit = fold_build3_loc (input_location,
9830 COND_EXPR, void_type_node, cond, exit, t);
9839 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9847 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9851 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9854 tree label = *label_p;
9856 /* In switch statements break is sometimes stylistically used after
9857 a return statement. This can lead to spurious warnings about
9858 control reaching the end of a non-void function when it is
9859 inlined. Note that we are calling block_may_fallthru with
9860 language specific tree nodes; this works because
9861 block_may_fallthru returns true when given something it does not
9863 skip = !block_may_fallthru (cur_stmt_list);
9868 *label_p = label = create_artificial_label (loc);
9870 else if (TREE_CODE (label) == LABEL_DECL)
9872 else switch (TREE_INT_CST_LOW (label))
9876 error_at (loc, "break statement not within loop or switch");
9878 error_at (loc, "continue statement not within a loop");
9882 gcc_assert (is_break);
9883 error_at (loc, "break statement used with OpenMP for loop");
9888 error ("break statement within %<#pragma simd%> loop body");
9890 error ("continue statement within %<#pragma simd%> loop body");
9901 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9903 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9906 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9909 emit_side_effect_warnings (location_t loc, tree expr)
9911 if (expr == error_mark_node)
9913 else if (!TREE_SIDE_EFFECTS (expr))
9915 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9916 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9918 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9921 location_t cloc = loc;
9922 while (TREE_CODE (r) == COMPOUND_EXPR)
9924 if (EXPR_HAS_LOCATION (r))
9925 cloc = EXPR_LOCATION (r);
9926 r = TREE_OPERAND (r, 1);
9928 if (!TREE_SIDE_EFFECTS (r)
9929 && !VOID_TYPE_P (TREE_TYPE (r))
9930 && !CONVERT_EXPR_P (r)
9931 && !TREE_NO_WARNING (r)
9932 && !TREE_NO_WARNING (expr))
9933 warning_at (cloc, OPT_Wunused_value,
9934 "right-hand operand of comma expression has no effect");
9937 warn_if_unused_value (expr, loc);
9940 /* Process an expression as if it were a complete statement. Emit
9941 diagnostics, but do not call ADD_STMT. LOC is the location of the
9945 c_process_expr_stmt (location_t loc, tree expr)
9952 expr = c_fully_fold (expr, false, NULL);
9954 if (warn_sequence_point)
9955 verify_sequence_points (expr);
9957 if (TREE_TYPE (expr) != error_mark_node
9958 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9959 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9960 error_at (loc, "expression statement has incomplete type");
9962 /* If we're not processing a statement expression, warn about unused values.
9963 Warnings for statement expressions will be emitted later, once we figure
9964 out which is the result. */
9965 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9966 && warn_unused_value)
9967 emit_side_effect_warnings (loc, expr);
9970 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9971 exprv = TREE_OPERAND (exprv, 1);
9972 while (CONVERT_EXPR_P (exprv))
9973 exprv = TREE_OPERAND (exprv, 0);
9975 || handled_component_p (exprv)
9976 || TREE_CODE (exprv) == ADDR_EXPR)
9977 mark_exp_read (exprv);
9979 /* If the expression is not of a type to which we cannot assign a line
9980 number, wrap the thing in a no-op NOP_EXPR. */
9981 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9983 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9984 SET_EXPR_LOCATION (expr, loc);
9990 /* Emit an expression as a statement. LOC is the location of the
9994 c_finish_expr_stmt (location_t loc, tree expr)
9997 return add_stmt (c_process_expr_stmt (loc, expr));
10002 /* Do the opposite and emit a statement as an expression. To begin,
10003 create a new binding level and return it. */
10006 c_begin_stmt_expr (void)
10010 /* We must force a BLOCK for this level so that, if it is not expanded
10011 later, there is a way to turn off the entire subtree of blocks that
10012 are contained in it. */
10013 keep_next_level ();
10014 ret = c_begin_compound_stmt (true);
10016 c_bindings_start_stmt_expr (c_switch_stack == NULL
10018 : c_switch_stack->bindings);
10020 /* Mark the current statement list as belonging to a statement list. */
10021 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10026 /* LOC is the location of the compound statement to which this body
10030 c_finish_stmt_expr (location_t loc, tree body)
10032 tree last, type, tmp, val;
10035 body = c_end_compound_stmt (loc, body, true);
10037 c_bindings_end_stmt_expr (c_switch_stack == NULL
10039 : c_switch_stack->bindings);
10041 /* Locate the last statement in BODY. See c_end_compound_stmt
10042 about always returning a BIND_EXPR. */
10043 last_p = &BIND_EXPR_BODY (body);
10044 last = BIND_EXPR_BODY (body);
10046 continue_searching:
10047 if (TREE_CODE (last) == STATEMENT_LIST)
10049 tree_stmt_iterator i;
10051 /* This can happen with degenerate cases like ({ }). No value. */
10052 if (!TREE_SIDE_EFFECTS (last))
10055 /* If we're supposed to generate side effects warnings, process
10056 all of the statements except the last. */
10057 if (warn_unused_value)
10059 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10062 tree t = tsi_stmt (i);
10064 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10065 emit_side_effect_warnings (tloc, t);
10069 i = tsi_last (last);
10070 last_p = tsi_stmt_ptr (i);
10074 /* If the end of the list is exception related, then the list was split
10075 by a call to push_cleanup. Continue searching. */
10076 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10077 || TREE_CODE (last) == TRY_CATCH_EXPR)
10079 last_p = &TREE_OPERAND (last, 0);
10081 goto continue_searching;
10084 if (last == error_mark_node)
10087 /* In the case that the BIND_EXPR is not necessary, return the
10088 expression out from inside it. */
10089 if (last == BIND_EXPR_BODY (body)
10090 && BIND_EXPR_VARS (body) == NULL)
10092 /* Even if this looks constant, do not allow it in a constant
10094 last = c_wrap_maybe_const (last, true);
10095 /* Do not warn if the return value of a statement expression is
10097 TREE_NO_WARNING (last) = 1;
10101 /* Extract the type of said expression. */
10102 type = TREE_TYPE (last);
10104 /* If we're not returning a value at all, then the BIND_EXPR that
10105 we already have is a fine expression to return. */
10106 if (!type || VOID_TYPE_P (type))
10109 /* Now that we've located the expression containing the value, it seems
10110 silly to make voidify_wrapper_expr repeat the process. Create a
10111 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10112 tmp = create_tmp_var_raw (type);
10114 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10115 tree_expr_nonnegative_p giving up immediately. */
10117 if (TREE_CODE (val) == NOP_EXPR
10118 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10119 val = TREE_OPERAND (val, 0);
10121 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10122 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10125 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10126 SET_EXPR_LOCATION (t, loc);
10131 /* Begin and end compound statements. This is as simple as pushing
10132 and popping new statement lists from the tree. */
10135 c_begin_compound_stmt (bool do_scope)
10137 tree stmt = push_stmt_list ();
10143 /* End a compound statement. STMT is the statement. LOC is the
10144 location of the compound statement-- this is usually the location
10145 of the opening brace. */
10148 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10154 if (c_dialect_objc ())
10155 objc_clear_super_receiver ();
10156 block = pop_scope ();
10159 stmt = pop_stmt_list (stmt);
10160 stmt = c_build_bind_expr (loc, block, stmt);
10162 /* If this compound statement is nested immediately inside a statement
10163 expression, then force a BIND_EXPR to be created. Otherwise we'll
10164 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10165 STATEMENT_LISTs merge, and thus we can lose track of what statement
10166 was really last. */
10167 if (building_stmt_list_p ()
10168 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10169 && TREE_CODE (stmt) != BIND_EXPR)
10171 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10172 TREE_SIDE_EFFECTS (stmt) = 1;
10173 SET_EXPR_LOCATION (stmt, loc);
10179 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10180 when the current scope is exited. EH_ONLY is true when this is not
10181 meant to apply to normal control flow transfer. */
10184 push_cleanup (tree decl, tree cleanup, bool eh_only)
10186 enum tree_code code;
10190 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10191 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10193 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10194 list = push_stmt_list ();
10195 TREE_OPERAND (stmt, 0) = list;
10196 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10199 /* Build a binary-operation expression without default conversions.
10200 CODE is the kind of expression to build.
10201 LOCATION is the operator's location.
10202 This function differs from `build' in several ways:
10203 the data type of the result is computed and recorded in it,
10204 warnings are generated if arg data types are invalid,
10205 special handling for addition and subtraction of pointers is known,
10206 and some optimization is done (operations on narrow ints
10207 are done in the narrower type when that gives the same result).
10208 Constant folding is also done before the result is returned.
10210 Note that the operands will never have enumeral types, or function
10211 or array types, because either they will have the default conversions
10212 performed or they have both just been converted to some other type in which
10213 the arithmetic is to be done. */
10216 build_binary_op (location_t location, enum tree_code code,
10217 tree orig_op0, tree orig_op1, int convert_p)
10219 tree type0, type1, orig_type0, orig_type1;
10221 enum tree_code code0, code1;
10223 tree ret = error_mark_node;
10224 const char *invalid_op_diag;
10225 bool op0_int_operands, op1_int_operands;
10226 bool int_const, int_const_or_overflow, int_operands;
10228 /* Expression code to give to the expression when it is built.
10229 Normally this is CODE, which is what the caller asked for,
10230 but in some special cases we change it. */
10231 enum tree_code resultcode = code;
10233 /* Data type in which the computation is to be performed.
10234 In the simplest cases this is the common type of the arguments. */
10235 tree result_type = NULL;
10237 /* When the computation is in excess precision, the type of the
10238 final EXCESS_PRECISION_EXPR. */
10239 tree semantic_result_type = NULL;
10241 /* Nonzero means operands have already been type-converted
10242 in whatever way is necessary.
10243 Zero means they need to be converted to RESULT_TYPE. */
10246 /* Nonzero means create the expression with this type, rather than
10248 tree build_type = 0;
10250 /* Nonzero means after finally constructing the expression
10251 convert it to this type. */
10252 tree final_type = 0;
10254 /* Nonzero if this is an operation like MIN or MAX which can
10255 safely be computed in short if both args are promoted shorts.
10256 Also implies COMMON.
10257 -1 indicates a bitwise operation; this makes a difference
10258 in the exact conditions for when it is safe to do the operation
10259 in a narrower mode. */
10262 /* Nonzero if this is a comparison operation;
10263 if both args are promoted shorts, compare the original shorts.
10264 Also implies COMMON. */
10265 int short_compare = 0;
10267 /* Nonzero if this is a right-shift operation, which can be computed on the
10268 original short and then promoted if the operand is a promoted short. */
10269 int short_shift = 0;
10271 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10274 /* True means types are compatible as far as ObjC is concerned. */
10277 /* True means this is an arithmetic operation that may need excess
10279 bool may_need_excess_precision;
10281 /* True means this is a boolean operation that converts both its
10282 operands to truth-values. */
10283 bool boolean_op = false;
10285 /* Remember whether we're doing / or %. */
10286 bool doing_div_or_mod = false;
10288 /* Remember whether we're doing << or >>. */
10289 bool doing_shift = false;
10291 /* Tree holding instrumentation expression. */
10292 tree instrument_expr = NULL;
10294 if (location == UNKNOWN_LOCATION)
10295 location = input_location;
10300 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10301 if (op0_int_operands)
10302 op0 = remove_c_maybe_const_expr (op0);
10303 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10304 if (op1_int_operands)
10305 op1 = remove_c_maybe_const_expr (op1);
10306 int_operands = (op0_int_operands && op1_int_operands);
10309 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10310 && TREE_CODE (orig_op1) == INTEGER_CST);
10311 int_const = (int_const_or_overflow
10312 && !TREE_OVERFLOW (orig_op0)
10313 && !TREE_OVERFLOW (orig_op1));
10316 int_const = int_const_or_overflow = false;
10318 /* Do not apply default conversion in mixed vector/scalar expression. */
10320 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10321 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10323 op0 = default_conversion (op0);
10324 op1 = default_conversion (op1);
10327 /* When Cilk Plus is enabled and there are array notations inside op0, then
10328 we check to see if there are builtin array notation functions. If
10329 so, then we take on the type of the array notation inside it. */
10330 if (flag_cilkplus && contains_array_notation_expr (op0))
10331 orig_type0 = type0 = find_correct_array_notation_type (op0);
10333 orig_type0 = type0 = TREE_TYPE (op0);
10335 if (flag_cilkplus && contains_array_notation_expr (op1))
10336 orig_type1 = type1 = find_correct_array_notation_type (op1);
10338 orig_type1 = type1 = TREE_TYPE (op1);
10340 /* The expression codes of the data types of the arguments tell us
10341 whether the arguments are integers, floating, pointers, etc. */
10342 code0 = TREE_CODE (type0);
10343 code1 = TREE_CODE (type1);
10345 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10346 STRIP_TYPE_NOPS (op0);
10347 STRIP_TYPE_NOPS (op1);
10349 /* If an error was already reported for one of the arguments,
10350 avoid reporting another error. */
10352 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10353 return error_mark_node;
10355 if ((invalid_op_diag
10356 = targetm.invalid_binary_op (code, type0, type1)))
10358 error_at (location, invalid_op_diag);
10359 return error_mark_node;
10367 case TRUNC_DIV_EXPR:
10368 case CEIL_DIV_EXPR:
10369 case FLOOR_DIV_EXPR:
10370 case ROUND_DIV_EXPR:
10371 case EXACT_DIV_EXPR:
10372 may_need_excess_precision = true;
10375 may_need_excess_precision = false;
10378 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10380 op0 = TREE_OPERAND (op0, 0);
10381 type0 = TREE_TYPE (op0);
10383 else if (may_need_excess_precision
10384 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10387 op0 = convert (eptype, op0);
10389 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10391 op1 = TREE_OPERAND (op1, 0);
10392 type1 = TREE_TYPE (op1);
10394 else if (may_need_excess_precision
10395 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10398 op1 = convert (eptype, op1);
10401 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10403 /* In case when one of the operands of the binary operation is
10404 a vector and another is a scalar -- convert scalar to vector. */
10405 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10407 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10410 switch (convert_flag)
10413 return error_mark_node;
10416 bool maybe_const = true;
10418 sc = c_fully_fold (op0, false, &maybe_const);
10419 sc = save_expr (sc);
10420 sc = convert (TREE_TYPE (type1), sc);
10421 op0 = build_vector_from_val (type1, sc);
10423 op0 = c_wrap_maybe_const (op0, true);
10424 orig_type0 = type0 = TREE_TYPE (op0);
10425 code0 = TREE_CODE (type0);
10429 case stv_secondarg:
10431 bool maybe_const = true;
10433 sc = c_fully_fold (op1, false, &maybe_const);
10434 sc = save_expr (sc);
10435 sc = convert (TREE_TYPE (type0), sc);
10436 op1 = build_vector_from_val (type0, sc);
10438 op1 = c_wrap_maybe_const (op1, true);
10439 orig_type1 = type1 = TREE_TYPE (op1);
10440 code1 = TREE_CODE (type1);
10452 /* Handle the pointer + int case. */
10453 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10455 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10456 goto return_build_binary_op;
10458 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10460 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10461 goto return_build_binary_op;
10468 /* Subtraction of two similar pointers.
10469 We must subtract them as integers, then divide by object size. */
10470 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10471 && comp_target_types (location, type0, type1))
10473 ret = pointer_diff (location, op0, op1);
10474 goto return_build_binary_op;
10476 /* Handle pointer minus int. Just like pointer plus int. */
10477 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10479 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10480 goto return_build_binary_op;
10490 case TRUNC_DIV_EXPR:
10491 case CEIL_DIV_EXPR:
10492 case FLOOR_DIV_EXPR:
10493 case ROUND_DIV_EXPR:
10494 case EXACT_DIV_EXPR:
10495 doing_div_or_mod = true;
10496 warn_for_div_by_zero (location, op1);
10498 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10499 || code0 == FIXED_POINT_TYPE
10500 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10501 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10502 || code1 == FIXED_POINT_TYPE
10503 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10505 enum tree_code tcode0 = code0, tcode1 = code1;
10507 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10508 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10509 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10510 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10512 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10513 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10514 resultcode = RDIV_EXPR;
10516 /* Although it would be tempting to shorten always here, that
10517 loses on some targets, since the modulo instruction is
10518 undefined if the quotient can't be represented in the
10519 computation mode. We shorten only if unsigned or if
10520 dividing by something we know != -1. */
10521 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10522 || (TREE_CODE (op1) == INTEGER_CST
10523 && !integer_all_onesp (op1)));
10531 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10533 /* Allow vector types which are not floating point types. */
10534 else if (code0 == VECTOR_TYPE
10535 && code1 == VECTOR_TYPE
10536 && !VECTOR_FLOAT_TYPE_P (type0)
10537 && !VECTOR_FLOAT_TYPE_P (type1))
10541 case TRUNC_MOD_EXPR:
10542 case FLOOR_MOD_EXPR:
10543 doing_div_or_mod = true;
10544 warn_for_div_by_zero (location, op1);
10546 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10547 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10548 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10550 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10552 /* Although it would be tempting to shorten always here, that loses
10553 on some targets, since the modulo instruction is undefined if the
10554 quotient can't be represented in the computation mode. We shorten
10555 only if unsigned or if dividing by something we know != -1. */
10556 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10557 || (TREE_CODE (op1) == INTEGER_CST
10558 && !integer_all_onesp (op1)));
10563 case TRUTH_ANDIF_EXPR:
10564 case TRUTH_ORIF_EXPR:
10565 case TRUTH_AND_EXPR:
10566 case TRUTH_OR_EXPR:
10567 case TRUTH_XOR_EXPR:
10568 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10569 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10570 || code0 == FIXED_POINT_TYPE)
10571 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10572 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10573 || code1 == FIXED_POINT_TYPE))
10575 /* Result of these operations is always an int,
10576 but that does not mean the operands should be
10577 converted to ints! */
10578 result_type = integer_type_node;
10579 if (op0_int_operands)
10581 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10582 op0 = remove_c_maybe_const_expr (op0);
10585 op0 = c_objc_common_truthvalue_conversion (location, op0);
10586 if (op1_int_operands)
10588 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10589 op1 = remove_c_maybe_const_expr (op1);
10592 op1 = c_objc_common_truthvalue_conversion (location, op1);
10596 if (code == TRUTH_ANDIF_EXPR)
10598 int_const_or_overflow = (int_operands
10599 && TREE_CODE (orig_op0) == INTEGER_CST
10600 && (op0 == truthvalue_false_node
10601 || TREE_CODE (orig_op1) == INTEGER_CST));
10602 int_const = (int_const_or_overflow
10603 && !TREE_OVERFLOW (orig_op0)
10604 && (op0 == truthvalue_false_node
10605 || !TREE_OVERFLOW (orig_op1)));
10607 else if (code == TRUTH_ORIF_EXPR)
10609 int_const_or_overflow = (int_operands
10610 && TREE_CODE (orig_op0) == INTEGER_CST
10611 && (op0 == truthvalue_true_node
10612 || TREE_CODE (orig_op1) == INTEGER_CST));
10613 int_const = (int_const_or_overflow
10614 && !TREE_OVERFLOW (orig_op0)
10615 && (op0 == truthvalue_true_node
10616 || !TREE_OVERFLOW (orig_op1)));
10620 /* Shift operations: result has same type as first operand;
10621 always convert second operand to int.
10622 Also set SHORT_SHIFT if shifting rightward. */
10625 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10626 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10628 result_type = type0;
10631 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10632 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10633 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10634 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10636 result_type = type0;
10639 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10640 && code1 == INTEGER_TYPE)
10642 doing_shift = true;
10643 if (TREE_CODE (op1) == INTEGER_CST)
10645 if (tree_int_cst_sgn (op1) < 0)
10648 if (c_inhibit_evaluation_warnings == 0)
10649 warning_at (location, OPT_Wshift_count_negative,
10650 "right shift count is negative");
10654 if (!integer_zerop (op1))
10657 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10660 if (c_inhibit_evaluation_warnings == 0)
10661 warning_at (location, OPT_Wshift_count_overflow,
10662 "right shift count >= width of type");
10667 /* Use the type of the value to be shifted. */
10668 result_type = type0;
10669 /* Avoid converting op1 to result_type later. */
10675 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10676 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10678 result_type = type0;
10681 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10682 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10683 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10684 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10686 result_type = type0;
10689 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10690 && code1 == INTEGER_TYPE)
10692 doing_shift = true;
10693 if (TREE_CODE (op0) == INTEGER_CST
10694 && tree_int_cst_sgn (op0) < 0)
10696 /* Don't reject a left shift of a negative value in a context
10697 where a constant expression is needed in C90. */
10700 if (c_inhibit_evaluation_warnings == 0)
10701 warning_at (location, OPT_Wshift_negative_value,
10702 "left shift of negative value");
10704 if (TREE_CODE (op1) == INTEGER_CST)
10706 if (tree_int_cst_sgn (op1) < 0)
10709 if (c_inhibit_evaluation_warnings == 0)
10710 warning_at (location, OPT_Wshift_count_negative,
10711 "left shift count is negative");
10714 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10717 if (c_inhibit_evaluation_warnings == 0)
10718 warning_at (location, OPT_Wshift_count_overflow,
10719 "left shift count >= width of type");
10723 /* Use the type of the value to be shifted. */
10724 result_type = type0;
10725 /* Avoid converting op1 to result_type later. */
10732 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10735 if (!vector_types_compatible_elements_p (type0, type1))
10737 error_at (location, "comparing vectors with different "
10739 return error_mark_node;
10742 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10744 error_at (location, "comparing vectors with different "
10745 "number of elements");
10746 return error_mark_node;
10749 /* Always construct signed integer vector type. */
10750 intt = c_common_type_for_size (GET_MODE_BITSIZE
10751 (TYPE_MODE (TREE_TYPE (type0))), 0);
10752 result_type = build_opaque_vector_type (intt,
10753 TYPE_VECTOR_SUBPARTS (type0));
10757 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10758 warning_at (location,
10760 "comparing floating point with == or != is unsafe");
10761 /* Result of comparison is always int,
10762 but don't convert the args to int! */
10763 build_type = integer_type_node;
10764 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10765 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10766 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10767 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10769 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10771 if (TREE_CODE (op0) == ADDR_EXPR
10772 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10774 if (code == EQ_EXPR)
10775 warning_at (location,
10777 "the comparison will always evaluate as %<false%> "
10778 "for the address of %qD will never be NULL",
10779 TREE_OPERAND (op0, 0));
10781 warning_at (location,
10783 "the comparison will always evaluate as %<true%> "
10784 "for the address of %qD will never be NULL",
10785 TREE_OPERAND (op0, 0));
10787 result_type = type0;
10789 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10791 if (TREE_CODE (op1) == ADDR_EXPR
10792 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10794 if (code == EQ_EXPR)
10795 warning_at (location,
10797 "the comparison will always evaluate as %<false%> "
10798 "for the address of %qD will never be NULL",
10799 TREE_OPERAND (op1, 0));
10801 warning_at (location,
10803 "the comparison will always evaluate as %<true%> "
10804 "for the address of %qD will never be NULL",
10805 TREE_OPERAND (op1, 0));
10807 result_type = type1;
10809 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10811 tree tt0 = TREE_TYPE (type0);
10812 tree tt1 = TREE_TYPE (type1);
10813 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10814 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10815 addr_space_t as_common = ADDR_SPACE_GENERIC;
10817 /* Anything compares with void *. void * compares with anything.
10818 Otherwise, the targets must be compatible
10819 and both must be object or both incomplete. */
10820 if (comp_target_types (location, type0, type1))
10821 result_type = common_pointer_type (type0, type1);
10822 else if (!addr_space_superset (as0, as1, &as_common))
10824 error_at (location, "comparison of pointers to "
10825 "disjoint address spaces");
10826 return error_mark_node;
10828 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10830 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10831 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10832 "comparison of %<void *%> with function pointer");
10834 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10836 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10837 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10838 "comparison of %<void *%> with function pointer");
10841 /* Avoid warning about the volatile ObjC EH puts on decls. */
10843 pedwarn (location, 0,
10844 "comparison of distinct pointer types lacks a cast");
10846 if (result_type == NULL_TREE)
10848 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10849 result_type = build_pointer_type
10850 (build_qualified_type (void_type_node, qual));
10853 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10855 result_type = type0;
10856 pedwarn (location, 0, "comparison between pointer and integer");
10858 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10860 result_type = type1;
10861 pedwarn (location, 0, "comparison between pointer and integer");
10863 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10864 || truth_value_p (TREE_CODE (orig_op0)))
10865 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10866 || truth_value_p (TREE_CODE (orig_op1))))
10867 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10874 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10877 if (!vector_types_compatible_elements_p (type0, type1))
10879 error_at (location, "comparing vectors with different "
10881 return error_mark_node;
10884 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10886 error_at (location, "comparing vectors with different "
10887 "number of elements");
10888 return error_mark_node;
10891 /* Always construct signed integer vector type. */
10892 intt = c_common_type_for_size (GET_MODE_BITSIZE
10893 (TYPE_MODE (TREE_TYPE (type0))), 0);
10894 result_type = build_opaque_vector_type (intt,
10895 TYPE_VECTOR_SUBPARTS (type0));
10899 build_type = integer_type_node;
10900 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10901 || code0 == FIXED_POINT_TYPE)
10902 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10903 || code1 == FIXED_POINT_TYPE))
10905 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10907 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10908 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10909 addr_space_t as_common;
10911 if (comp_target_types (location, type0, type1))
10913 result_type = common_pointer_type (type0, type1);
10914 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10915 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10916 pedwarn (location, 0,
10917 "comparison of complete and incomplete pointers");
10918 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10919 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10920 "ordered comparisons of pointers to functions");
10921 else if (null_pointer_constant_p (orig_op0)
10922 || null_pointer_constant_p (orig_op1))
10923 warning_at (location, OPT_Wextra,
10924 "ordered comparison of pointer with null pointer");
10927 else if (!addr_space_superset (as0, as1, &as_common))
10929 error_at (location, "comparison of pointers to "
10930 "disjoint address spaces");
10931 return error_mark_node;
10935 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10936 result_type = build_pointer_type
10937 (build_qualified_type (void_type_node, qual));
10938 pedwarn (location, 0,
10939 "comparison of distinct pointer types lacks a cast");
10942 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10944 result_type = type0;
10946 pedwarn (location, OPT_Wpedantic,
10947 "ordered comparison of pointer with integer zero");
10948 else if (extra_warnings)
10949 warning_at (location, OPT_Wextra,
10950 "ordered comparison of pointer with integer zero");
10952 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10954 result_type = type1;
10956 pedwarn (location, OPT_Wpedantic,
10957 "ordered comparison of pointer with integer zero");
10958 else if (extra_warnings)
10959 warning_at (location, OPT_Wextra,
10960 "ordered comparison of pointer with integer zero");
10962 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10964 result_type = type0;
10965 pedwarn (location, 0, "comparison between pointer and integer");
10967 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10969 result_type = type1;
10970 pedwarn (location, 0, "comparison between pointer and integer");
10972 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10973 || truth_value_p (TREE_CODE (orig_op0)))
10974 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10975 || truth_value_p (TREE_CODE (orig_op1))))
10976 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10980 gcc_unreachable ();
10983 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10984 return error_mark_node;
10986 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10987 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10988 || !vector_types_compatible_elements_p (type0, type1)))
10990 binary_op_error (location, code, type0, type1);
10991 return error_mark_node;
10994 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10995 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10997 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10998 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11000 bool first_complex = (code0 == COMPLEX_TYPE);
11001 bool second_complex = (code1 == COMPLEX_TYPE);
11002 int none_complex = (!first_complex && !second_complex);
11004 if (shorten || common || short_compare)
11006 result_type = c_common_type (type0, type1);
11007 do_warn_double_promotion (result_type, type0, type1,
11008 "implicit conversion from %qT to %qT "
11009 "to match other operand of binary "
11012 if (result_type == error_mark_node)
11013 return error_mark_node;
11016 if (first_complex != second_complex
11017 && (code == PLUS_EXPR
11018 || code == MINUS_EXPR
11019 || code == MULT_EXPR
11020 || (code == TRUNC_DIV_EXPR && first_complex))
11021 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11022 && flag_signed_zeros)
11024 /* An operation on mixed real/complex operands must be
11025 handled specially, but the language-independent code can
11026 more easily optimize the plain complex arithmetic if
11027 -fno-signed-zeros. */
11028 tree real_type = TREE_TYPE (result_type);
11030 if (type0 != orig_type0 || type1 != orig_type1)
11032 gcc_assert (may_need_excess_precision && common);
11033 semantic_result_type = c_common_type (orig_type0, orig_type1);
11037 if (TREE_TYPE (op0) != result_type)
11038 op0 = convert_and_check (location, result_type, op0);
11039 if (TREE_TYPE (op1) != real_type)
11040 op1 = convert_and_check (location, real_type, op1);
11044 if (TREE_TYPE (op0) != real_type)
11045 op0 = convert_and_check (location, real_type, op0);
11046 if (TREE_TYPE (op1) != result_type)
11047 op1 = convert_and_check (location, result_type, op1);
11049 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11050 return error_mark_node;
11053 op0 = c_save_expr (op0);
11054 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11056 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11061 case TRUNC_DIV_EXPR:
11062 op1 = c_save_expr (op1);
11063 imag = build2 (resultcode, real_type, imag, op1);
11064 /* Fall through. */
11067 real = build2 (resultcode, real_type, real, op1);
11075 op1 = c_save_expr (op1);
11076 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11078 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11083 op0 = c_save_expr (op0);
11084 imag = build2 (resultcode, real_type, op0, imag);
11085 /* Fall through. */
11087 real = build2 (resultcode, real_type, op0, real);
11090 real = build2 (resultcode, real_type, op0, real);
11091 imag = build1 (NEGATE_EXPR, real_type, imag);
11097 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11098 goto return_build_binary_op;
11101 /* For certain operations (which identify themselves by shorten != 0)
11102 if both args were extended from the same smaller type,
11103 do the arithmetic in that type and then extend.
11105 shorten !=0 and !=1 indicates a bitwise operation.
11106 For them, this optimization is safe only if
11107 both args are zero-extended or both are sign-extended.
11108 Otherwise, we might change the result.
11109 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11110 but calculated in (unsigned short) it would be (unsigned short)-1. */
11112 if (shorten && none_complex)
11114 final_type = result_type;
11115 result_type = shorten_binary_op (result_type, op0, op1,
11119 /* Shifts can be shortened if shifting right. */
11124 tree arg0 = get_narrower (op0, &unsigned_arg);
11126 final_type = result_type;
11128 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11129 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11131 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11132 && tree_int_cst_sgn (op1) > 0
11133 /* We can shorten only if the shift count is less than the
11134 number of bits in the smaller type size. */
11135 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11136 /* We cannot drop an unsigned shift after sign-extension. */
11137 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11139 /* Do an unsigned shift if the operand was zero-extended. */
11141 = c_common_signed_or_unsigned_type (unsigned_arg,
11143 /* Convert value-to-be-shifted to that type. */
11144 if (TREE_TYPE (op0) != result_type)
11145 op0 = convert (result_type, op0);
11150 /* Comparison operations are shortened too but differently.
11151 They identify themselves by setting short_compare = 1. */
11155 /* Don't write &op0, etc., because that would prevent op0
11156 from being kept in a register.
11157 Instead, make copies of the our local variables and
11158 pass the copies by reference, then copy them back afterward. */
11159 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11160 enum tree_code xresultcode = resultcode;
11162 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11168 goto return_build_binary_op;
11171 op0 = xop0, op1 = xop1;
11173 resultcode = xresultcode;
11175 if (c_inhibit_evaluation_warnings == 0)
11177 bool op0_maybe_const = true;
11178 bool op1_maybe_const = true;
11179 tree orig_op0_folded, orig_op1_folded;
11181 if (in_late_binary_op)
11183 orig_op0_folded = orig_op0;
11184 orig_op1_folded = orig_op1;
11188 /* Fold for the sake of possible warnings, as in
11189 build_conditional_expr. This requires the
11190 "original" values to be folded, not just op0 and
11192 c_inhibit_evaluation_warnings++;
11193 op0 = c_fully_fold (op0, require_constant_value,
11195 op1 = c_fully_fold (op1, require_constant_value,
11197 c_inhibit_evaluation_warnings--;
11198 orig_op0_folded = c_fully_fold (orig_op0,
11199 require_constant_value,
11201 orig_op1_folded = c_fully_fold (orig_op1,
11202 require_constant_value,
11206 if (warn_sign_compare)
11207 warn_for_sign_compare (location, orig_op0_folded,
11208 orig_op1_folded, op0, op1,
11209 result_type, resultcode);
11210 if (!in_late_binary_op && !int_operands)
11212 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11213 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11214 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11215 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11221 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11222 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11223 Then the expression will be built.
11224 It will be given type FINAL_TYPE if that is nonzero;
11225 otherwise, it will be given type RESULT_TYPE. */
11229 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11230 return error_mark_node;
11233 if (build_type == NULL_TREE)
11235 build_type = result_type;
11236 if ((type0 != orig_type0 || type1 != orig_type1)
11239 gcc_assert (may_need_excess_precision && common);
11240 semantic_result_type = c_common_type (orig_type0, orig_type1);
11246 op0 = ep_convert_and_check (location, result_type, op0,
11247 semantic_result_type);
11248 op1 = ep_convert_and_check (location, result_type, op1,
11249 semantic_result_type);
11251 /* This can happen if one operand has a vector type, and the other
11252 has a different type. */
11253 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11254 return error_mark_node;
11257 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11258 | SANITIZE_FLOAT_DIVIDE))
11259 && do_ubsan_in_current_function ()
11260 && (doing_div_or_mod || doing_shift))
11262 /* OP0 and/or OP1 might have side-effects. */
11263 op0 = c_save_expr (op0);
11264 op1 = c_save_expr (op1);
11265 op0 = c_fully_fold (op0, false, NULL);
11266 op1 = c_fully_fold (op1, false, NULL);
11267 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11268 | SANITIZE_FLOAT_DIVIDE)))
11269 instrument_expr = ubsan_instrument_division (location, op0, op1);
11270 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11271 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11274 /* Treat expressions in initializers specially as they can't trap. */
11275 if (int_const_or_overflow)
11276 ret = (require_constant_value
11277 ? fold_build2_initializer_loc (location, resultcode, build_type,
11279 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11281 ret = build2 (resultcode, build_type, op0, op1);
11282 if (final_type != 0)
11283 ret = convert (final_type, ret);
11285 return_build_binary_op:
11286 gcc_assert (ret != error_mark_node);
11287 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11288 ret = (int_operands
11289 ? note_integer_operands (ret)
11290 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11291 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11292 && !in_late_binary_op)
11293 ret = note_integer_operands (ret);
11294 if (semantic_result_type)
11295 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11296 protected_set_expr_location (ret, location);
11298 if (instrument_expr != NULL)
11299 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11300 instrument_expr, ret);
11306 /* Convert EXPR to be a truth-value, validating its type for this
11307 purpose. LOCATION is the source location for the expression. */
11310 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11312 bool int_const, int_operands;
11314 switch (TREE_CODE (TREE_TYPE (expr)))
11317 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11318 return error_mark_node;
11321 error_at (location, "used struct type value where scalar is required");
11322 return error_mark_node;
11325 error_at (location, "used union type value where scalar is required");
11326 return error_mark_node;
11329 error_at (location, "void value not ignored as it ought to be");
11330 return error_mark_node;
11332 case FUNCTION_TYPE:
11333 gcc_unreachable ();
11336 error_at (location, "used vector type where scalar is required");
11337 return error_mark_node;
11343 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11344 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11345 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11347 expr = remove_c_maybe_const_expr (expr);
11348 expr = build2 (NE_EXPR, integer_type_node, expr,
11349 convert (TREE_TYPE (expr), integer_zero_node));
11350 expr = note_integer_operands (expr);
11353 /* ??? Should we also give an error for vectors rather than leaving
11354 those to give errors later? */
11355 expr = c_common_truthvalue_conversion (location, expr);
11357 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11359 if (TREE_OVERFLOW (expr))
11362 return note_integer_operands (expr);
11364 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11365 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11370 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11374 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11376 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11378 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11379 /* Executing a compound literal inside a function reinitializes
11381 if (!TREE_STATIC (decl))
11389 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11390 statement. LOC is the location of the OACC_PARALLEL. */
11393 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11397 block = c_end_compound_stmt (loc, block, true);
11399 stmt = make_node (OACC_PARALLEL);
11400 TREE_TYPE (stmt) = void_type_node;
11401 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11402 OACC_PARALLEL_BODY (stmt) = block;
11403 SET_EXPR_LOCATION (stmt, loc);
11405 return add_stmt (stmt);
11408 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11409 statement. LOC is the location of the OACC_KERNELS. */
11412 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11416 block = c_end_compound_stmt (loc, block, true);
11418 stmt = make_node (OACC_KERNELS);
11419 TREE_TYPE (stmt) = void_type_node;
11420 OACC_KERNELS_CLAUSES (stmt) = clauses;
11421 OACC_KERNELS_BODY (stmt) = block;
11422 SET_EXPR_LOCATION (stmt, loc);
11424 return add_stmt (stmt);
11427 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11428 statement. LOC is the location of the OACC_DATA. */
11431 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11435 block = c_end_compound_stmt (loc, block, true);
11437 stmt = make_node (OACC_DATA);
11438 TREE_TYPE (stmt) = void_type_node;
11439 OACC_DATA_CLAUSES (stmt) = clauses;
11440 OACC_DATA_BODY (stmt) = block;
11441 SET_EXPR_LOCATION (stmt, loc);
11443 return add_stmt (stmt);
11446 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11449 c_begin_omp_parallel (void)
11453 keep_next_level ();
11454 block = c_begin_compound_stmt (true);
11459 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11460 statement. LOC is the location of the OMP_PARALLEL. */
11463 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11467 block = c_end_compound_stmt (loc, block, true);
11469 stmt = make_node (OMP_PARALLEL);
11470 TREE_TYPE (stmt) = void_type_node;
11471 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11472 OMP_PARALLEL_BODY (stmt) = block;
11473 SET_EXPR_LOCATION (stmt, loc);
11475 return add_stmt (stmt);
11478 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11481 c_begin_omp_task (void)
11485 keep_next_level ();
11486 block = c_begin_compound_stmt (true);
11491 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11492 statement. LOC is the location of the #pragma. */
11495 c_finish_omp_task (location_t loc, tree clauses, tree block)
11499 block = c_end_compound_stmt (loc, block, true);
11501 stmt = make_node (OMP_TASK);
11502 TREE_TYPE (stmt) = void_type_node;
11503 OMP_TASK_CLAUSES (stmt) = clauses;
11504 OMP_TASK_BODY (stmt) = block;
11505 SET_EXPR_LOCATION (stmt, loc);
11507 return add_stmt (stmt);
11510 /* Generate GOMP_cancel call for #pragma omp cancel. */
11513 c_finish_omp_cancel (location_t loc, tree clauses)
11515 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11517 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11519 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11521 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11523 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11527 error_at (loc, "%<#pragma omp cancel must specify one of "
11528 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11532 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11533 if (ifc != NULL_TREE)
11535 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11536 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11537 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11538 build_zero_cst (type));
11541 ifc = boolean_true_node;
11542 tree stmt = build_call_expr_loc (loc, fn, 2,
11543 build_int_cst (integer_type_node, mask),
11548 /* Generate GOMP_cancellation_point call for
11549 #pragma omp cancellation point. */
11552 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11554 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11556 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11558 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11560 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11562 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11566 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11567 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11571 tree stmt = build_call_expr_loc (loc, fn, 1,
11572 build_int_cst (integer_type_node, mask));
11576 /* Helper function for handle_omp_array_sections. Called recursively
11577 to handle multiple array-section-subscripts. C is the clause,
11578 T current expression (initially OMP_CLAUSE_DECL), which is either
11579 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11580 expression if specified, TREE_VALUE length expression if specified,
11581 TREE_CHAIN is what it has been specified after, or some decl.
11582 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11583 set to true if any of the array-section-subscript could have length
11584 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11585 first array-section-subscript which is known not to have length
11587 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11588 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11589 all are or may have length of 1, array-section-subscript [:2] is the
11590 first one knonwn not to have length 1. For array-section-subscript
11591 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11592 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11593 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11594 case though, as some lengths could be zero. */
11597 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11598 bool &maybe_zero_len, unsigned int &first_non_one)
11600 tree ret, low_bound, length, type;
11601 if (TREE_CODE (t) != TREE_LIST)
11603 if (error_operand_p (t))
11604 return error_mark_node;
11605 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11608 error_at (OMP_CLAUSE_LOCATION (c),
11609 "%qD is not a variable in %qs clause", t,
11610 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11612 error_at (OMP_CLAUSE_LOCATION (c),
11613 "%qE is not a variable in %qs clause", t,
11614 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11615 return error_mark_node;
11617 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11618 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11620 error_at (OMP_CLAUSE_LOCATION (c),
11621 "%qD is threadprivate variable in %qs clause", t,
11622 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11623 return error_mark_node;
11628 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11629 maybe_zero_len, first_non_one);
11630 if (ret == error_mark_node || ret == NULL_TREE)
11633 type = TREE_TYPE (ret);
11634 low_bound = TREE_PURPOSE (t);
11635 length = TREE_VALUE (t);
11637 if (low_bound == error_mark_node || length == error_mark_node)
11638 return error_mark_node;
11640 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11642 error_at (OMP_CLAUSE_LOCATION (c),
11643 "low bound %qE of array section does not have integral type",
11645 return error_mark_node;
11647 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11649 error_at (OMP_CLAUSE_LOCATION (c),
11650 "length %qE of array section does not have integral type",
11652 return error_mark_node;
11655 && TREE_CODE (low_bound) == INTEGER_CST
11656 && TYPE_PRECISION (TREE_TYPE (low_bound))
11657 > TYPE_PRECISION (sizetype))
11658 low_bound = fold_convert (sizetype, low_bound);
11660 && TREE_CODE (length) == INTEGER_CST
11661 && TYPE_PRECISION (TREE_TYPE (length))
11662 > TYPE_PRECISION (sizetype))
11663 length = fold_convert (sizetype, length);
11664 if (low_bound == NULL_TREE)
11665 low_bound = integer_zero_node;
11667 if (length != NULL_TREE)
11669 if (!integer_nonzerop (length))
11670 maybe_zero_len = true;
11671 if (first_non_one == types.length ()
11672 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11675 if (TREE_CODE (type) == ARRAY_TYPE)
11677 if (length == NULL_TREE
11678 && (TYPE_DOMAIN (type) == NULL_TREE
11679 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11681 error_at (OMP_CLAUSE_LOCATION (c),
11682 "for unknown bound array type length expression must "
11684 return error_mark_node;
11686 if (TREE_CODE (low_bound) == INTEGER_CST
11687 && tree_int_cst_sgn (low_bound) == -1)
11689 error_at (OMP_CLAUSE_LOCATION (c),
11690 "negative low bound in array section in %qs clause",
11691 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11692 return error_mark_node;
11694 if (length != NULL_TREE
11695 && TREE_CODE (length) == INTEGER_CST
11696 && tree_int_cst_sgn (length) == -1)
11698 error_at (OMP_CLAUSE_LOCATION (c),
11699 "negative length in array section in %qs clause",
11700 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11701 return error_mark_node;
11703 if (TYPE_DOMAIN (type)
11704 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11705 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11708 tree size = size_binop (PLUS_EXPR,
11709 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11711 if (TREE_CODE (low_bound) == INTEGER_CST)
11713 if (tree_int_cst_lt (size, low_bound))
11715 error_at (OMP_CLAUSE_LOCATION (c),
11716 "low bound %qE above array section size "
11717 "in %qs clause", low_bound,
11718 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11719 return error_mark_node;
11721 if (tree_int_cst_equal (size, low_bound))
11722 maybe_zero_len = true;
11723 else if (length == NULL_TREE
11724 && first_non_one == types.length ()
11725 && tree_int_cst_equal
11726 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11730 else if (length == NULL_TREE)
11732 maybe_zero_len = true;
11733 if (first_non_one == types.length ())
11736 if (length && TREE_CODE (length) == INTEGER_CST)
11738 if (tree_int_cst_lt (size, length))
11740 error_at (OMP_CLAUSE_LOCATION (c),
11741 "length %qE above array section size "
11742 "in %qs clause", length,
11743 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11744 return error_mark_node;
11746 if (TREE_CODE (low_bound) == INTEGER_CST)
11749 = size_binop (PLUS_EXPR,
11750 fold_convert (sizetype, low_bound),
11751 fold_convert (sizetype, length));
11752 if (TREE_CODE (lbpluslen) == INTEGER_CST
11753 && tree_int_cst_lt (size, lbpluslen))
11755 error_at (OMP_CLAUSE_LOCATION (c),
11756 "high bound %qE above array section size "
11757 "in %qs clause", lbpluslen,
11758 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11759 return error_mark_node;
11764 else if (length == NULL_TREE)
11766 maybe_zero_len = true;
11767 if (first_non_one == types.length ())
11771 /* For [lb:] we will need to evaluate lb more than once. */
11772 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11774 tree lb = c_save_expr (low_bound);
11775 if (lb != low_bound)
11777 TREE_PURPOSE (t) = lb;
11782 else if (TREE_CODE (type) == POINTER_TYPE)
11784 if (length == NULL_TREE)
11786 error_at (OMP_CLAUSE_LOCATION (c),
11787 "for pointer type length expression must be specified");
11788 return error_mark_node;
11790 /* If there is a pointer type anywhere but in the very first
11791 array-section-subscript, the array section can't be contiguous. */
11792 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11793 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11795 error_at (OMP_CLAUSE_LOCATION (c),
11796 "array section is not contiguous in %qs clause",
11797 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11798 return error_mark_node;
11803 error_at (OMP_CLAUSE_LOCATION (c),
11804 "%qE does not have pointer or array type", ret);
11805 return error_mark_node;
11807 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11808 types.safe_push (TREE_TYPE (ret));
11809 /* We will need to evaluate lb more than once. */
11810 tree lb = c_save_expr (low_bound);
11811 if (lb != low_bound)
11813 TREE_PURPOSE (t) = lb;
11816 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11820 /* Handle array sections for clause C. */
11823 handle_omp_array_sections (tree c)
11825 bool maybe_zero_len = false;
11826 unsigned int first_non_one = 0;
11827 vec<tree> types = vNULL;
11828 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11829 maybe_zero_len, first_non_one);
11830 if (first == error_mark_node)
11835 if (first == NULL_TREE)
11840 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11842 tree t = OMP_CLAUSE_DECL (c);
11843 tree tem = NULL_TREE;
11845 /* Need to evaluate side effects in the length expressions
11847 while (TREE_CODE (t) == TREE_LIST)
11849 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11851 if (tem == NULL_TREE)
11852 tem = TREE_VALUE (t);
11854 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11855 TREE_VALUE (t), tem);
11857 t = TREE_CHAIN (t);
11860 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11861 first = c_fully_fold (first, false, NULL);
11862 OMP_CLAUSE_DECL (c) = first;
11866 unsigned int num = types.length (), i;
11867 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11868 tree condition = NULL_TREE;
11870 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11871 maybe_zero_len = true;
11873 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11874 t = TREE_CHAIN (t))
11876 tree low_bound = TREE_PURPOSE (t);
11877 tree length = TREE_VALUE (t);
11881 && TREE_CODE (low_bound) == INTEGER_CST
11882 && TYPE_PRECISION (TREE_TYPE (low_bound))
11883 > TYPE_PRECISION (sizetype))
11884 low_bound = fold_convert (sizetype, low_bound);
11886 && TREE_CODE (length) == INTEGER_CST
11887 && TYPE_PRECISION (TREE_TYPE (length))
11888 > TYPE_PRECISION (sizetype))
11889 length = fold_convert (sizetype, length);
11890 if (low_bound == NULL_TREE)
11891 low_bound = integer_zero_node;
11892 if (!maybe_zero_len && i > first_non_one)
11894 if (integer_nonzerop (low_bound))
11895 goto do_warn_noncontiguous;
11896 if (length != NULL_TREE
11897 && TREE_CODE (length) == INTEGER_CST
11898 && TYPE_DOMAIN (types[i])
11899 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11900 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11904 size = size_binop (PLUS_EXPR,
11905 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11907 if (!tree_int_cst_equal (length, size))
11909 do_warn_noncontiguous:
11910 error_at (OMP_CLAUSE_LOCATION (c),
11911 "array section is not contiguous in %qs "
11913 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11918 if (length != NULL_TREE
11919 && TREE_SIDE_EFFECTS (length))
11921 if (side_effects == NULL_TREE)
11922 side_effects = length;
11924 side_effects = build2 (COMPOUND_EXPR,
11925 TREE_TYPE (side_effects),
11926 length, side_effects);
11933 if (i > first_non_one && length && integer_nonzerop (length))
11936 l = fold_convert (sizetype, length);
11939 l = size_binop (PLUS_EXPR,
11940 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11942 l = size_binop (MINUS_EXPR, l,
11943 fold_convert (sizetype, low_bound));
11945 if (i > first_non_one)
11947 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11949 if (condition == NULL_TREE)
11952 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11955 else if (size == NULL_TREE)
11957 size = size_in_bytes (TREE_TYPE (types[i]));
11958 size = size_binop (MULT_EXPR, size, l);
11960 size = fold_build3 (COND_EXPR, sizetype, condition,
11961 size, size_zero_node);
11964 size = size_binop (MULT_EXPR, size, l);
11969 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11970 first = c_fully_fold (first, false, NULL);
11971 OMP_CLAUSE_DECL (c) = first;
11973 size = c_fully_fold (size, false, NULL);
11974 OMP_CLAUSE_SIZE (c) = size;
11975 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11977 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
11978 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11979 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
11980 if (!c_mark_addressable (t))
11982 OMP_CLAUSE_DECL (c2) = t;
11983 t = build_fold_addr_expr (first);
11984 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11985 tree ptr = OMP_CLAUSE_DECL (c2);
11986 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11987 ptr = build_fold_addr_expr (ptr);
11988 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11989 ptrdiff_type_node, t,
11990 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11991 ptrdiff_type_node, ptr));
11992 t = c_fully_fold (t, false, NULL);
11993 OMP_CLAUSE_SIZE (c2) = t;
11994 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11995 OMP_CLAUSE_CHAIN (c) = c2;
12000 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12001 an inline call. But, remap
12002 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12003 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12006 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12007 tree decl, tree placeholder)
12010 hash_map<tree, tree> decl_map;
12012 decl_map.put (omp_decl1, placeholder);
12013 decl_map.put (omp_decl2, decl);
12014 memset (&id, 0, sizeof (id));
12015 id.src_fn = DECL_CONTEXT (omp_decl1);
12016 id.dst_fn = current_function_decl;
12017 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12018 id.decl_map = &decl_map;
12020 id.copy_decl = copy_decl_no_change;
12021 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12022 id.transform_new_cfg = true;
12023 id.transform_return_to_modify = false;
12024 id.transform_lang_insert_block = NULL;
12026 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12030 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12031 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12034 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12036 if (*tp == (tree) data)
12041 /* For all elements of CLAUSES, validate them against their constraints.
12042 Remove any elements from the list that are invalid. */
12045 c_finish_omp_clauses (tree clauses)
12047 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12048 bitmap_head aligned_head;
12050 bool branch_seen = false;
12051 bool copyprivate_seen = false;
12052 tree *nowait_clause = NULL;
12054 bitmap_obstack_initialize (NULL);
12055 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12056 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12057 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12058 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12060 for (pc = &clauses, c = clauses; c ; c = *pc)
12062 bool remove = false;
12063 bool need_complete = false;
12064 bool need_implicitly_determined = false;
12066 switch (OMP_CLAUSE_CODE (c))
12068 case OMP_CLAUSE_SHARED:
12069 need_implicitly_determined = true;
12070 goto check_dup_generic;
12072 case OMP_CLAUSE_PRIVATE:
12073 need_complete = true;
12074 need_implicitly_determined = true;
12075 goto check_dup_generic;
12077 case OMP_CLAUSE_REDUCTION:
12078 need_implicitly_determined = true;
12079 t = OMP_CLAUSE_DECL (c);
12080 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12081 && (FLOAT_TYPE_P (TREE_TYPE (t))
12082 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12084 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12085 const char *r_name = NULL;
12094 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12098 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12110 case TRUTH_ANDIF_EXPR:
12111 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12114 case TRUTH_ORIF_EXPR:
12115 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12119 gcc_unreachable ();
12123 error_at (OMP_CLAUSE_LOCATION (c),
12124 "%qE has invalid type for %<reduction(%s)%>",
12130 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12132 error_at (OMP_CLAUSE_LOCATION (c),
12133 "user defined reduction not found for %qD", t);
12137 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12139 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12140 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12141 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12142 VAR_DECL, NULL_TREE, type);
12143 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12144 DECL_ARTIFICIAL (placeholder) = 1;
12145 DECL_IGNORED_P (placeholder) = 1;
12146 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12147 c_mark_addressable (placeholder);
12148 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12149 c_mark_addressable (OMP_CLAUSE_DECL (c));
12150 OMP_CLAUSE_REDUCTION_MERGE (c)
12151 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12152 TREE_VEC_ELT (list, 0),
12153 TREE_VEC_ELT (list, 1),
12154 OMP_CLAUSE_DECL (c), placeholder);
12155 OMP_CLAUSE_REDUCTION_MERGE (c)
12156 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12157 void_type_node, NULL_TREE,
12158 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12159 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12160 if (TREE_VEC_LENGTH (list) == 6)
12162 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12163 c_mark_addressable (OMP_CLAUSE_DECL (c));
12164 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12165 c_mark_addressable (placeholder);
12166 tree init = TREE_VEC_ELT (list, 5);
12167 if (init == error_mark_node)
12168 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12169 OMP_CLAUSE_REDUCTION_INIT (c)
12170 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12171 TREE_VEC_ELT (list, 3),
12172 OMP_CLAUSE_DECL (c), placeholder);
12173 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12174 OMP_CLAUSE_REDUCTION_INIT (c)
12175 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12176 OMP_CLAUSE_REDUCTION_INIT (c));
12177 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12178 c_find_omp_placeholder_r,
12179 placeholder, NULL))
12180 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12185 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12186 init = build_constructor (TREE_TYPE (t), NULL);
12188 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12189 OMP_CLAUSE_REDUCTION_INIT (c)
12190 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12192 OMP_CLAUSE_REDUCTION_INIT (c)
12193 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12194 void_type_node, NULL_TREE,
12195 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12196 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12198 goto check_dup_generic;
12200 case OMP_CLAUSE_COPYPRIVATE:
12201 copyprivate_seen = true;
12204 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12205 "%<nowait%> clause must not be used together "
12206 "with %<copyprivate%>");
12207 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12208 nowait_clause = NULL;
12210 goto check_dup_generic;
12212 case OMP_CLAUSE_COPYIN:
12213 t = OMP_CLAUSE_DECL (c);
12214 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12216 error_at (OMP_CLAUSE_LOCATION (c),
12217 "%qE must be %<threadprivate%> for %<copyin%>", t);
12221 goto check_dup_generic;
12223 case OMP_CLAUSE_LINEAR:
12224 t = OMP_CLAUSE_DECL (c);
12225 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12226 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12228 error_at (OMP_CLAUSE_LOCATION (c),
12229 "linear clause applied to non-integral non-pointer "
12230 "variable with type %qT", TREE_TYPE (t));
12234 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12236 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12237 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12238 OMP_CLAUSE_DECL (c), s);
12239 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12240 sizetype, s, OMP_CLAUSE_DECL (c));
12241 if (s == error_mark_node)
12243 OMP_CLAUSE_LINEAR_STEP (c) = s;
12246 OMP_CLAUSE_LINEAR_STEP (c)
12247 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12248 goto check_dup_generic;
12251 t = OMP_CLAUSE_DECL (c);
12252 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12254 error_at (OMP_CLAUSE_LOCATION (c),
12255 "%qE is not a variable in clause %qs", t,
12256 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12259 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12260 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12261 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12263 error_at (OMP_CLAUSE_LOCATION (c),
12264 "%qE appears more than once in data clauses", t);
12268 bitmap_set_bit (&generic_head, DECL_UID (t));
12271 case OMP_CLAUSE_FIRSTPRIVATE:
12272 t = OMP_CLAUSE_DECL (c);
12273 need_complete = true;
12274 need_implicitly_determined = true;
12275 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12277 error_at (OMP_CLAUSE_LOCATION (c),
12278 "%qE is not a variable in clause %<firstprivate%>", t);
12281 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12282 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12284 error_at (OMP_CLAUSE_LOCATION (c),
12285 "%qE appears more than once in data clauses", t);
12289 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12292 case OMP_CLAUSE_LASTPRIVATE:
12293 t = OMP_CLAUSE_DECL (c);
12294 need_complete = true;
12295 need_implicitly_determined = true;
12296 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12298 error_at (OMP_CLAUSE_LOCATION (c),
12299 "%qE is not a variable in clause %<lastprivate%>", t);
12302 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12303 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12305 error_at (OMP_CLAUSE_LOCATION (c),
12306 "%qE appears more than once in data clauses", t);
12310 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12313 case OMP_CLAUSE_ALIGNED:
12314 t = OMP_CLAUSE_DECL (c);
12315 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12317 error_at (OMP_CLAUSE_LOCATION (c),
12318 "%qE is not a variable in %<aligned%> clause", t);
12321 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12322 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12324 error_at (OMP_CLAUSE_LOCATION (c),
12325 "%qE in %<aligned%> clause is neither a pointer nor "
12329 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12331 error_at (OMP_CLAUSE_LOCATION (c),
12332 "%qE appears more than once in %<aligned%> clauses",
12337 bitmap_set_bit (&aligned_head, DECL_UID (t));
12340 case OMP_CLAUSE_DEPEND:
12341 t = OMP_CLAUSE_DECL (c);
12342 if (TREE_CODE (t) == TREE_LIST)
12344 if (handle_omp_array_sections (c))
12348 if (t == error_mark_node)
12350 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12352 error_at (OMP_CLAUSE_LOCATION (c),
12353 "%qE is not a variable in %<depend%> clause", t);
12356 else if (!c_mark_addressable (t))
12360 case OMP_CLAUSE_MAP:
12361 case OMP_CLAUSE_TO:
12362 case OMP_CLAUSE_FROM:
12363 case OMP_CLAUSE__CACHE_:
12364 t = OMP_CLAUSE_DECL (c);
12365 if (TREE_CODE (t) == TREE_LIST)
12367 if (handle_omp_array_sections (c))
12371 t = OMP_CLAUSE_DECL (c);
12372 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12374 error_at (OMP_CLAUSE_LOCATION (c),
12375 "array section does not have mappable type "
12377 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12383 if (t == error_mark_node)
12385 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12387 error_at (OMP_CLAUSE_LOCATION (c),
12388 "%qE is not a variable in %qs clause", t,
12389 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12392 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12394 error_at (OMP_CLAUSE_LOCATION (c),
12395 "%qD is threadprivate variable in %qs clause", t,
12396 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12399 else if (!c_mark_addressable (t))
12401 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12402 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12403 || (OMP_CLAUSE_MAP_KIND (c)
12404 == GOMP_MAP_FORCE_DEVICEPTR)))
12405 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12407 error_at (OMP_CLAUSE_LOCATION (c),
12408 "%qD does not have a mappable type in %qs clause", t,
12409 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12412 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12414 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12415 error ("%qD appears more than once in motion clauses", t);
12417 error ("%qD appears more than once in map clauses", t);
12421 bitmap_set_bit (&generic_head, DECL_UID (t));
12424 case OMP_CLAUSE_UNIFORM:
12425 t = OMP_CLAUSE_DECL (c);
12426 if (TREE_CODE (t) != PARM_DECL)
12429 error_at (OMP_CLAUSE_LOCATION (c),
12430 "%qD is not an argument in %<uniform%> clause", t);
12432 error_at (OMP_CLAUSE_LOCATION (c),
12433 "%qE is not an argument in %<uniform%> clause", t);
12437 goto check_dup_generic;
12439 case OMP_CLAUSE_NOWAIT:
12440 if (copyprivate_seen)
12442 error_at (OMP_CLAUSE_LOCATION (c),
12443 "%<nowait%> clause must not be used together "
12444 "with %<copyprivate%>");
12448 nowait_clause = pc;
12449 pc = &OMP_CLAUSE_CHAIN (c);
12452 case OMP_CLAUSE_IF:
12453 case OMP_CLAUSE_NUM_THREADS:
12454 case OMP_CLAUSE_NUM_TEAMS:
12455 case OMP_CLAUSE_THREAD_LIMIT:
12456 case OMP_CLAUSE_SCHEDULE:
12457 case OMP_CLAUSE_ORDERED:
12458 case OMP_CLAUSE_DEFAULT:
12459 case OMP_CLAUSE_UNTIED:
12460 case OMP_CLAUSE_COLLAPSE:
12461 case OMP_CLAUSE_FINAL:
12462 case OMP_CLAUSE_MERGEABLE:
12463 case OMP_CLAUSE_SAFELEN:
12464 case OMP_CLAUSE_SIMDLEN:
12465 case OMP_CLAUSE_DEVICE:
12466 case OMP_CLAUSE_DIST_SCHEDULE:
12467 case OMP_CLAUSE_PARALLEL:
12468 case OMP_CLAUSE_FOR:
12469 case OMP_CLAUSE_SECTIONS:
12470 case OMP_CLAUSE_TASKGROUP:
12471 case OMP_CLAUSE_PROC_BIND:
12472 case OMP_CLAUSE__CILK_FOR_COUNT_:
12473 case OMP_CLAUSE_NUM_GANGS:
12474 case OMP_CLAUSE_NUM_WORKERS:
12475 case OMP_CLAUSE_VECTOR_LENGTH:
12476 case OMP_CLAUSE_ASYNC:
12477 case OMP_CLAUSE_WAIT:
12478 case OMP_CLAUSE_AUTO:
12479 case OMP_CLAUSE_SEQ:
12480 case OMP_CLAUSE_GANG:
12481 case OMP_CLAUSE_WORKER:
12482 case OMP_CLAUSE_VECTOR:
12483 pc = &OMP_CLAUSE_CHAIN (c);
12486 case OMP_CLAUSE_INBRANCH:
12487 case OMP_CLAUSE_NOTINBRANCH:
12490 error_at (OMP_CLAUSE_LOCATION (c),
12491 "%<inbranch%> clause is incompatible with "
12492 "%<notinbranch%>");
12496 branch_seen = true;
12497 pc = &OMP_CLAUSE_CHAIN (c);
12501 gcc_unreachable ();
12506 t = OMP_CLAUSE_DECL (c);
12510 t = require_complete_type (t);
12511 if (t == error_mark_node)
12515 if (need_implicitly_determined)
12517 const char *share_name = NULL;
12519 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12520 share_name = "threadprivate";
12521 else switch (c_omp_predetermined_sharing (t))
12523 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12525 case OMP_CLAUSE_DEFAULT_SHARED:
12526 /* const vars may be specified in firstprivate clause. */
12527 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12528 && TREE_READONLY (t))
12530 share_name = "shared";
12532 case OMP_CLAUSE_DEFAULT_PRIVATE:
12533 share_name = "private";
12536 gcc_unreachable ();
12540 error_at (OMP_CLAUSE_LOCATION (c),
12541 "%qE is predetermined %qs for %qs",
12543 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12550 *pc = OMP_CLAUSE_CHAIN (c);
12552 pc = &OMP_CLAUSE_CHAIN (c);
12555 bitmap_obstack_release (NULL);
12559 /* Create a transaction node. */
12562 c_finish_transaction (location_t loc, tree block, int flags)
12564 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12565 if (flags & TM_STMT_ATTR_OUTER)
12566 TRANSACTION_EXPR_OUTER (stmt) = 1;
12567 if (flags & TM_STMT_ATTR_RELAXED)
12568 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12569 return add_stmt (stmt);
12572 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12573 down to the element type of an array. */
12576 c_build_qualified_type (tree type, int type_quals)
12578 if (type == error_mark_node)
12581 if (TREE_CODE (type) == ARRAY_TYPE)
12584 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12587 /* See if we already have an identically qualified type. */
12588 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12590 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12591 && TYPE_NAME (t) == TYPE_NAME (type)
12592 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12593 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12594 TYPE_ATTRIBUTES (type)))
12599 tree domain = TYPE_DOMAIN (type);
12601 t = build_variant_type_copy (type);
12602 TREE_TYPE (t) = element_type;
12604 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12605 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12606 SET_TYPE_STRUCTURAL_EQUALITY (t);
12607 else if (TYPE_CANONICAL (element_type) != element_type
12608 || (domain && TYPE_CANONICAL (domain) != domain))
12610 tree unqualified_canon
12611 = build_array_type (TYPE_CANONICAL (element_type),
12612 domain? TYPE_CANONICAL (domain)
12615 = c_build_qualified_type (unqualified_canon, type_quals);
12618 TYPE_CANONICAL (t) = t;
12623 /* A restrict-qualified pointer type must be a pointer to object or
12624 incomplete type. Note that the use of POINTER_TYPE_P also allows
12625 REFERENCE_TYPEs, which is appropriate for C++. */
12626 if ((type_quals & TYPE_QUAL_RESTRICT)
12627 && (!POINTER_TYPE_P (type)
12628 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12630 error ("invalid use of %<restrict%>");
12631 type_quals &= ~TYPE_QUAL_RESTRICT;
12634 return build_qualified_type (type, type_quals);
12637 /* Build a VA_ARG_EXPR for the C parser. */
12640 c_build_va_arg (location_t loc, tree expr, tree type)
12642 if (error_operand_p (type))
12643 return error_mark_node;
12644 else if (!COMPLETE_TYPE_P (type))
12646 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12648 return error_mark_node;
12650 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12651 warning_at (loc, OPT_Wc___compat,
12652 "C++ requires promoted type, not enum type, in %<va_arg%>");
12653 return build_va_arg (loc, expr, type);
12656 /* Return truthvalue of whether T1 is the same tree structure as T2.
12657 Return 1 if they are the same. Return 0 if they are different. */
12660 c_tree_equal (tree t1, tree t2)
12662 enum tree_code code1, code2;
12669 for (code1 = TREE_CODE (t1);
12670 CONVERT_EXPR_CODE_P (code1)
12671 || code1 == NON_LVALUE_EXPR;
12672 code1 = TREE_CODE (t1))
12673 t1 = TREE_OPERAND (t1, 0);
12674 for (code2 = TREE_CODE (t2);
12675 CONVERT_EXPR_CODE_P (code2)
12676 || code2 == NON_LVALUE_EXPR;
12677 code2 = TREE_CODE (t2))
12678 t2 = TREE_OPERAND (t2, 0);
12680 /* They might have become equal now. */
12684 if (code1 != code2)
12690 return wi::eq_p (t1, t2);
12693 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12696 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12697 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12698 TREE_STRING_LENGTH (t1));
12701 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12702 TREE_FIXED_CST (t2));
12705 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12706 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12709 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12712 /* We need to do this when determining whether or not two
12713 non-type pointer to member function template arguments
12715 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12716 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12721 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12723 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12724 if (!c_tree_equal (field, elt2->index)
12725 || !c_tree_equal (value, elt2->value))
12732 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12734 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12736 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12739 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12744 call_expr_arg_iterator iter1, iter2;
12745 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12747 for (arg1 = first_call_expr_arg (t1, &iter1),
12748 arg2 = first_call_expr_arg (t2, &iter2);
12750 arg1 = next_call_expr_arg (&iter1),
12751 arg2 = next_call_expr_arg (&iter2))
12752 if (!c_tree_equal (arg1, arg2))
12761 tree o1 = TREE_OPERAND (t1, 0);
12762 tree o2 = TREE_OPERAND (t2, 0);
12764 /* Special case: if either target is an unallocated VAR_DECL,
12765 it means that it's going to be unified with whatever the
12766 TARGET_EXPR is really supposed to initialize, so treat it
12767 as being equivalent to anything. */
12768 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12769 && !DECL_RTL_SET_P (o1))
12771 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12772 && !DECL_RTL_SET_P (o2))
12774 else if (!c_tree_equal (o1, o2))
12777 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12780 case COMPONENT_REF:
12781 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12783 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12789 case FUNCTION_DECL:
12790 case IDENTIFIER_NODE:
12797 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12799 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12800 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12801 TREE_VEC_ELT (t2, ix)))
12810 switch (TREE_CODE_CLASS (code1))
12814 case tcc_comparison:
12815 case tcc_expression:
12817 case tcc_reference:
12818 case tcc_statement:
12820 int i, n = TREE_OPERAND_LENGTH (t1);
12824 case PREINCREMENT_EXPR:
12825 case PREDECREMENT_EXPR:
12826 case POSTINCREMENT_EXPR:
12827 case POSTDECREMENT_EXPR:
12837 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12838 && n != TREE_OPERAND_LENGTH (t2))
12841 for (i = 0; i < n; ++i)
12842 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12849 return comptypes (t1, t2);
12851 gcc_unreachable ();
12853 /* We can get here with --disable-checking. */
12857 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12858 spawn-helper and BODY is the newly created body for FNDECL. */
12861 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12863 tree list = alloc_stmt_list ();
12864 tree frame = make_cilk_frame (fndecl);
12865 tree dtor = create_cilk_function_exit (frame, false, true);
12866 add_local_decl (cfun, frame);
12868 DECL_SAVED_TREE (fndecl) = list;
12869 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12871 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12872 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12874 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12875 append_to_statement_list (detach_expr, &body_list);
12877 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12878 body = fold_build_cleanup_point_expr (void_type_node, body);
12880 append_to_statement_list (body, &body_list);
12881 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12882 body_list, dtor), &list);