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. */
2857 inform_declaration (tree decl)
2859 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
2860 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2863 /* Build a function call to function FUNCTION with parameters PARAMS.
2864 ORIGTYPES, if not NULL, is a vector of types; each element is
2865 either NULL or the original type of the corresponding element in
2866 PARAMS. The original type may differ from TREE_TYPE of the
2867 parameter for enums. FUNCTION's data type may be a function type
2868 or pointer-to-function. This function changes the elements of
2872 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2873 tree function, vec<tree, va_gc> *params,
2874 vec<tree, va_gc> *origtypes)
2876 tree fntype, fundecl = 0;
2877 tree name = NULL_TREE, result;
2883 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2884 STRIP_TYPE_NOPS (function);
2886 /* Convert anything with function type to a pointer-to-function. */
2887 if (TREE_CODE (function) == FUNCTION_DECL)
2889 name = DECL_NAME (function);
2892 tm_malloc_replacement (function);
2894 /* Atomic functions have type checking/casting already done. They are
2895 often rewritten and don't match the original parameter list. */
2896 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2900 && is_cilkplus_reduce_builtin (function))
2903 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2904 function = function_to_pointer_conversion (loc, function);
2906 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2907 expressions, like those used for ObjC messenger dispatches. */
2908 if (params && !params->is_empty ())
2909 function = objc_rewrite_function_call (function, (*params)[0]);
2911 function = c_fully_fold (function, false, NULL);
2913 fntype = TREE_TYPE (function);
2915 if (TREE_CODE (fntype) == ERROR_MARK)
2916 return error_mark_node;
2918 if (!(TREE_CODE (fntype) == POINTER_TYPE
2919 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2921 if (!flag_diagnostics_show_caret)
2923 "called object %qE is not a function or function pointer",
2925 else if (DECL_P (function))
2928 "called object %qD is not a function or function pointer",
2930 inform_declaration (function);
2934 "called object is not a function or function pointer");
2935 return error_mark_node;
2938 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2939 current_function_returns_abnormally = 1;
2941 /* fntype now gets the type of function pointed to. */
2942 fntype = TREE_TYPE (fntype);
2944 /* Convert the parameters to the types declared in the
2945 function prototype, or apply default promotions. */
2947 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2948 origtypes, function, fundecl);
2950 return error_mark_node;
2952 /* Check that the function is called through a compatible prototype.
2953 If it is not, warn. */
2954 if (CONVERT_EXPR_P (function)
2955 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2956 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2957 && !comptypes (fntype, TREE_TYPE (tem)))
2959 tree return_type = TREE_TYPE (fntype);
2961 /* This situation leads to run-time undefined behavior. We can't,
2962 therefore, simply error unless we can prove that all possible
2963 executions of the program must execute the code. */
2964 warning_at (loc, 0, "function called through a non-compatible type");
2966 if (VOID_TYPE_P (return_type)
2967 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2969 "function with qualified void return type called");
2972 argarray = vec_safe_address (params);
2974 /* Check that arguments to builtin functions match the expectations. */
2976 && DECL_BUILT_IN (fundecl)
2977 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2978 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2979 return error_mark_node;
2981 /* Check that the arguments to the function are valid. */
2982 check_function_arguments (fntype, nargs, argarray);
2984 if (name != NULL_TREE
2985 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2987 if (require_constant_value)
2989 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2990 function, nargs, argarray);
2992 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2993 function, nargs, argarray);
2994 if (TREE_CODE (result) == NOP_EXPR
2995 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2996 STRIP_TYPE_NOPS (result);
2999 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3000 function, nargs, argarray);
3002 if (VOID_TYPE_P (TREE_TYPE (result)))
3004 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3006 "function with qualified void return type called");
3009 return require_complete_type (result);
3012 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3015 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3016 tree function, vec<tree, va_gc> *params,
3017 vec<tree, va_gc> *origtypes)
3019 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3020 STRIP_TYPE_NOPS (function);
3022 /* Convert anything with function type to a pointer-to-function. */
3023 if (TREE_CODE (function) == FUNCTION_DECL)
3025 /* Implement type-directed function overloading for builtins.
3026 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3027 handle all the type checking. The result is a complete expression
3028 that implements this function call. */
3029 tree tem = resolve_overloaded_builtin (loc, function, params);
3033 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3036 /* Convert the argument expressions in the vector VALUES
3037 to the types in the list TYPELIST.
3039 If TYPELIST is exhausted, or when an element has NULL as its type,
3040 perform the default conversions.
3042 ORIGTYPES is the original types of the expressions in VALUES. This
3043 holds the type of enum values which have been converted to integral
3044 types. It may be NULL.
3046 FUNCTION is a tree for the called function. It is used only for
3047 error messages, where it is formatted with %qE.
3049 This is also where warnings about wrong number of args are generated.
3051 ARG_LOC are locations of function arguments (if any).
3053 Returns the actual number of arguments processed (which may be less
3054 than the length of VALUES in some error situations), or -1 on
3058 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3059 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3060 tree function, tree fundecl)
3063 unsigned int parmnum;
3064 bool error_args = false;
3065 const bool type_generic = fundecl
3066 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3067 bool type_generic_remove_excess_precision = false;
3070 /* Change pointer to function to the function itself for
3072 if (TREE_CODE (function) == ADDR_EXPR
3073 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3074 function = TREE_OPERAND (function, 0);
3076 /* Handle an ObjC selector specially for diagnostics. */
3077 selector = objc_message_selector ();
3079 /* For type-generic built-in functions, determine whether excess
3080 precision should be removed (classification) or not
3083 && DECL_BUILT_IN (fundecl)
3084 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3086 switch (DECL_FUNCTION_CODE (fundecl))
3088 case BUILT_IN_ISFINITE:
3089 case BUILT_IN_ISINF:
3090 case BUILT_IN_ISINF_SIGN:
3091 case BUILT_IN_ISNAN:
3092 case BUILT_IN_ISNORMAL:
3093 case BUILT_IN_FPCLASSIFY:
3094 type_generic_remove_excess_precision = true;
3098 type_generic_remove_excess_precision = false;
3102 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3103 return vec_safe_length (values);
3105 /* Scan the given expressions and types, producing individual
3106 converted arguments. */
3108 for (typetail = typelist, parmnum = 0;
3109 values && values->iterate (parmnum, &val);
3112 tree type = typetail ? TREE_VALUE (typetail) : 0;
3113 tree valtype = TREE_TYPE (val);
3114 tree rname = function;
3115 int argnum = parmnum + 1;
3116 const char *invalid_func_diag;
3117 bool excess_precision = false;
3120 /* Some __atomic_* builtins have additional hidden argument at
3123 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3124 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3127 if (type == void_type_node)
3130 error_at (loc, "too many arguments to method %qE", selector);
3132 error_at (loc, "too many arguments to function %qE", function);
3133 inform_declaration (fundecl);
3134 return error_args ? -1 : (int) parmnum;
3137 if (selector && argnum > 2)
3143 npc = null_pointer_constant_p (val);
3145 /* If there is excess precision and a prototype, convert once to
3146 the required type rather than converting via the semantic
3147 type. Likewise without a prototype a float value represented
3148 as long double should be converted once to double. But for
3149 type-generic classification functions excess precision must
3151 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3152 && (type || !type_generic || !type_generic_remove_excess_precision))
3154 val = TREE_OPERAND (val, 0);
3155 excess_precision = true;
3157 val = c_fully_fold (val, false, NULL);
3158 STRIP_TYPE_NOPS (val);
3160 val = require_complete_type (val);
3164 /* Formal parm type is specified by a function prototype. */
3166 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3168 error_at (ploc, "type of formal parameter %d is incomplete",
3176 /* Optionally warn about conversions that
3177 differ from the default conversions. */
3178 if (warn_traditional_conversion || warn_traditional)
3180 unsigned int formal_prec = TYPE_PRECISION (type);
3182 if (INTEGRAL_TYPE_P (type)
3183 && TREE_CODE (valtype) == REAL_TYPE)
3184 warning_at (ploc, OPT_Wtraditional_conversion,
3185 "passing argument %d of %qE as integer rather "
3186 "than floating due to prototype",
3188 if (INTEGRAL_TYPE_P (type)
3189 && TREE_CODE (valtype) == COMPLEX_TYPE)
3190 warning_at (ploc, OPT_Wtraditional_conversion,
3191 "passing argument %d of %qE as integer rather "
3192 "than complex due to prototype",
3194 else if (TREE_CODE (type) == COMPLEX_TYPE
3195 && TREE_CODE (valtype) == REAL_TYPE)
3196 warning_at (ploc, OPT_Wtraditional_conversion,
3197 "passing argument %d of %qE as complex rather "
3198 "than floating due to prototype",
3200 else if (TREE_CODE (type) == REAL_TYPE
3201 && INTEGRAL_TYPE_P (valtype))
3202 warning_at (ploc, OPT_Wtraditional_conversion,
3203 "passing argument %d of %qE as floating rather "
3204 "than integer due to prototype",
3206 else if (TREE_CODE (type) == COMPLEX_TYPE
3207 && INTEGRAL_TYPE_P (valtype))
3208 warning_at (ploc, OPT_Wtraditional_conversion,
3209 "passing argument %d of %qE as complex rather "
3210 "than integer due to prototype",
3212 else if (TREE_CODE (type) == REAL_TYPE
3213 && TREE_CODE (valtype) == COMPLEX_TYPE)
3214 warning_at (ploc, OPT_Wtraditional_conversion,
3215 "passing argument %d of %qE as floating rather "
3216 "than complex due to prototype",
3218 /* ??? At some point, messages should be written about
3219 conversions between complex types, but that's too messy
3221 else if (TREE_CODE (type) == REAL_TYPE
3222 && TREE_CODE (valtype) == REAL_TYPE)
3224 /* Warn if any argument is passed as `float',
3225 since without a prototype it would be `double'. */
3226 if (formal_prec == TYPE_PRECISION (float_type_node)
3227 && type != dfloat32_type_node)
3228 warning_at (ploc, 0,
3229 "passing argument %d of %qE as %<float%> "
3230 "rather than %<double%> due to prototype",
3233 /* Warn if mismatch between argument and prototype
3234 for decimal float types. Warn of conversions with
3235 binary float types and of precision narrowing due to
3237 else if (type != valtype
3238 && (type == dfloat32_type_node
3239 || type == dfloat64_type_node
3240 || type == dfloat128_type_node
3241 || valtype == dfloat32_type_node
3242 || valtype == dfloat64_type_node
3243 || valtype == dfloat128_type_node)
3245 <= TYPE_PRECISION (valtype)
3246 || (type == dfloat128_type_node
3248 != dfloat64_type_node
3250 != dfloat32_type_node)))
3251 || (type == dfloat64_type_node
3253 != dfloat32_type_node))))
3254 warning_at (ploc, 0,
3255 "passing argument %d of %qE as %qT "
3256 "rather than %qT due to prototype",
3257 argnum, rname, type, valtype);
3260 /* Detect integer changing in width or signedness.
3261 These warnings are only activated with
3262 -Wtraditional-conversion, not with -Wtraditional. */
3263 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3264 && INTEGRAL_TYPE_P (valtype))
3266 tree would_have_been = default_conversion (val);
3267 tree type1 = TREE_TYPE (would_have_been);
3269 if (TREE_CODE (type) == ENUMERAL_TYPE
3270 && (TYPE_MAIN_VARIANT (type)
3271 == TYPE_MAIN_VARIANT (valtype)))
3272 /* No warning if function asks for enum
3273 and the actual arg is that enum type. */
3275 else if (formal_prec != TYPE_PRECISION (type1))
3276 warning_at (ploc, OPT_Wtraditional_conversion,
3277 "passing argument %d of %qE "
3278 "with different width due to prototype",
3280 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3282 /* Don't complain if the formal parameter type
3283 is an enum, because we can't tell now whether
3284 the value was an enum--even the same enum. */
3285 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3287 else if (TREE_CODE (val) == INTEGER_CST
3288 && int_fits_type_p (val, type))
3289 /* Change in signedness doesn't matter
3290 if a constant value is unaffected. */
3292 /* If the value is extended from a narrower
3293 unsigned type, it doesn't matter whether we
3294 pass it as signed or unsigned; the value
3295 certainly is the same either way. */
3296 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3297 && TYPE_UNSIGNED (valtype))
3299 else if (TYPE_UNSIGNED (type))
3300 warning_at (ploc, OPT_Wtraditional_conversion,
3301 "passing argument %d of %qE "
3302 "as unsigned due to prototype",
3305 warning_at (ploc, OPT_Wtraditional_conversion,
3306 "passing argument %d of %qE "
3307 "as signed due to prototype",
3312 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3313 sake of better warnings from convert_and_check. */
3314 if (excess_precision)
3315 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3316 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3317 parmval = convert_for_assignment (loc, ploc, type,
3318 val, origtype, ic_argpass,
3319 npc, fundecl, function,
3322 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3323 && INTEGRAL_TYPE_P (type)
3324 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3325 parmval = default_conversion (parmval);
3328 else if (TREE_CODE (valtype) == REAL_TYPE
3329 && (TYPE_PRECISION (valtype)
3330 <= TYPE_PRECISION (double_type_node))
3331 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3332 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3333 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3339 /* Convert `float' to `double'. */
3340 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3341 warning_at (ploc, OPT_Wdouble_promotion,
3342 "implicit conversion from %qT to %qT when passing "
3343 "argument to function",
3344 valtype, double_type_node);
3345 parmval = convert (double_type_node, val);
3348 else if (excess_precision && !type_generic)
3349 /* A "double" argument with excess precision being passed
3350 without a prototype or in variable arguments. */
3351 parmval = convert (valtype, val);
3352 else if ((invalid_func_diag =
3353 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3355 error (invalid_func_diag);
3359 /* Convert `short' and `char' to full-size `int'. */
3360 parmval = default_conversion (val);
3362 (*values)[parmnum] = parmval;
3363 if (parmval == error_mark_node)
3367 typetail = TREE_CHAIN (typetail);
3370 gcc_assert (parmnum == vec_safe_length (values));
3372 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3374 error_at (loc, "too few arguments to function %qE", function);
3375 inform_declaration (fundecl);
3379 return error_args ? -1 : (int) parmnum;
3382 /* This is the entry point used by the parser to build unary operators
3383 in the input. CODE, a tree_code, specifies the unary operator, and
3384 ARG is the operand. For unary plus, the C parser currently uses
3385 CONVERT_EXPR for code.
3387 LOC is the location to use for the tree generated.
3391 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3393 struct c_expr result;
3395 result.value = build_unary_op (loc, code, arg.value, 0);
3396 result.original_code = code;
3397 result.original_type = NULL;
3399 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3400 overflow_warning (loc, result.value);
3405 /* This is the entry point used by the parser to build binary operators
3406 in the input. CODE, a tree_code, specifies the binary operator, and
3407 ARG1 and ARG2 are the operands. In addition to constructing the
3408 expression, we check for operands that were written with other binary
3409 operators in a way that is likely to confuse the user.
3411 LOCATION is the location of the binary operator. */
3414 parser_build_binary_op (location_t location, enum tree_code code,
3415 struct c_expr arg1, struct c_expr arg2)
3417 struct c_expr result;
3419 enum tree_code code1 = arg1.original_code;
3420 enum tree_code code2 = arg2.original_code;
3421 tree type1 = (arg1.original_type
3422 ? arg1.original_type
3423 : TREE_TYPE (arg1.value));
3424 tree type2 = (arg2.original_type
3425 ? arg2.original_type
3426 : TREE_TYPE (arg2.value));
3428 result.value = build_binary_op (location, code,
3429 arg1.value, arg2.value, 1);
3430 result.original_code = code;
3431 result.original_type = NULL;
3433 if (TREE_CODE (result.value) == ERROR_MARK)
3436 if (location != UNKNOWN_LOCATION)
3437 protected_set_expr_location (result.value, location);
3439 /* Check for cases such as x+y<<z which users are likely
3441 if (warn_parentheses)
3442 warn_about_parentheses (location, code, code1, arg1.value, code2,
3445 if (warn_logical_op)
3446 warn_logical_operator (location, code, TREE_TYPE (result.value),
3447 code1, arg1.value, code2, arg2.value);
3449 if (warn_logical_not_paren
3450 && TREE_CODE_CLASS (code) == tcc_comparison
3451 && code1 == TRUTH_NOT_EXPR
3452 && code2 != TRUTH_NOT_EXPR
3453 /* Avoid warning for !!x == y. */
3454 && (TREE_CODE (arg1.value) != NE_EXPR
3455 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3457 /* Avoid warning for !b == y where b has _Bool type. */
3458 tree t = integer_zero_node;
3459 if (TREE_CODE (arg1.value) == EQ_EXPR
3460 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3461 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3463 t = TREE_OPERAND (arg1.value, 0);
3466 if (TREE_TYPE (t) != integer_type_node)
3468 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3469 t = C_MAYBE_CONST_EXPR_EXPR (t);
3470 else if (CONVERT_EXPR_P (t))
3471 t = TREE_OPERAND (t, 0);
3477 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3478 warn_logical_not_parentheses (location, code, arg2.value);
3481 /* Warn about comparisons against string literals, with the exception
3482 of testing for equality or inequality of a string literal with NULL. */
3483 if (code == EQ_EXPR || code == NE_EXPR)
3485 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3486 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3487 warning_at (location, OPT_Waddress,
3488 "comparison with string literal results in unspecified behavior");
3490 else if (TREE_CODE_CLASS (code) == tcc_comparison
3491 && (code1 == STRING_CST || code2 == STRING_CST))
3492 warning_at (location, OPT_Waddress,
3493 "comparison with string literal results in unspecified behavior");
3495 if (TREE_OVERFLOW_P (result.value)
3496 && !TREE_OVERFLOW_P (arg1.value)
3497 && !TREE_OVERFLOW_P (arg2.value))
3498 overflow_warning (location, result.value);
3500 /* Warn about comparisons of different enum types. */
3501 if (warn_enum_compare
3502 && TREE_CODE_CLASS (code) == tcc_comparison
3503 && TREE_CODE (type1) == ENUMERAL_TYPE
3504 && TREE_CODE (type2) == ENUMERAL_TYPE
3505 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3506 warning_at (location, OPT_Wenum_compare,
3507 "comparison between %qT and %qT",
3513 /* Return a tree for the difference of pointers OP0 and OP1.
3514 The resulting tree has type int. */
3517 pointer_diff (location_t loc, tree op0, tree op1)
3519 tree restype = ptrdiff_type_node;
3520 tree result, inttype;
3522 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3523 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3524 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3525 tree orig_op1 = op1;
3527 /* If the operands point into different address spaces, we need to
3528 explicitly convert them to pointers into the common address space
3529 before we can subtract the numerical address values. */
3532 addr_space_t as_common;
3535 /* Determine the common superset address space. This is guaranteed
3536 to exist because the caller verified that comp_target_types
3537 returned non-zero. */
3538 if (!addr_space_superset (as0, as1, &as_common))
3541 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3542 op0 = convert (common_type, op0);
3543 op1 = convert (common_type, op1);
3546 /* Determine integer type to perform computations in. This will usually
3547 be the same as the result type (ptrdiff_t), but may need to be a wider
3548 type if pointers for the address space are wider than ptrdiff_t. */
3549 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3550 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3554 if (TREE_CODE (target_type) == VOID_TYPE)
3555 pedwarn (loc, OPT_Wpointer_arith,
3556 "pointer of type %<void *%> used in subtraction");
3557 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3558 pedwarn (loc, OPT_Wpointer_arith,
3559 "pointer to a function used in subtraction");
3561 /* First do the subtraction as integers;
3562 then drop through to build the divide operator.
3563 Do not do default conversions on the minus operator
3564 in case restype is a short type. */
3566 op0 = build_binary_op (loc,
3567 MINUS_EXPR, convert (inttype, op0),
3568 convert (inttype, op1), 0);
3569 /* This generates an error if op1 is pointer to incomplete type. */
3570 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3571 error_at (loc, "arithmetic on pointer to an incomplete type");
3573 op1 = c_size_in_bytes (target_type);
3575 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3576 error_at (loc, "arithmetic on pointer to an empty aggregate");
3578 /* Divide by the size, in easiest possible way. */
3579 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3580 op0, convert (inttype, op1));
3582 /* Convert to final result type if necessary. */
3583 return convert (restype, result);
3586 /* Expand atomic compound assignments into an approriate sequence as
3587 specified by the C11 standard section 6.5.16.2.
3593 This sequence is used for all types for which these operations are
3596 In addition, built-in versions of the 'fe' prefixed routines may
3597 need to be invoked for floating point (real, complex or vector) when
3598 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3608 __atomic_load (addr, &old, SEQ_CST);
3609 feholdexcept (&fenv);
3611 newval = old op val;
3612 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3615 feclearexcept (FE_ALL_EXCEPT);
3618 feupdateenv (&fenv);
3620 Also note that the compiler is simply issuing the generic form of
3621 the atomic operations. This requires temp(s) and has their address
3622 taken. The atomic processing is smart enough to figure out when the
3623 size of an object can utilize a lock-free version, and convert the
3624 built-in call to the appropriate lock-free routine. The optimizers
3625 will then dispose of any temps that are no longer required, and
3626 lock-free implementations are utilized as long as there is target
3627 support for the required size.
3629 If the operator is NOP_EXPR, then this is a simple assignment, and
3630 an __atomic_store is issued to perform the assignment rather than
3635 /* Build an atomic assignment at LOC, expanding into the proper
3636 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3637 the result of the operation, unless RETURN_OLD_P in which case
3638 return the old value of LHS (this is only for postincrement and
3641 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3642 tree rhs, bool return_old_p)
3644 tree fndecl, func_call;
3645 vec<tree, va_gc> *params;
3646 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3649 tree stmt, goto_stmt;
3650 tree loop_label, loop_decl, done_label, done_decl;
3652 tree lhs_type = TREE_TYPE (lhs);
3653 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3654 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3655 tree rhs_type = TREE_TYPE (rhs);
3657 gcc_assert (TYPE_ATOMIC (lhs_type));
3660 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3662 /* Allocate enough vector items for a compare_exchange. */
3663 vec_alloc (params, 6);
3665 /* Create a compound statement to hold the sequence of statements
3667 compound_stmt = c_begin_compound_stmt (false);
3669 /* Fold the RHS if it hasn't already been folded. */
3670 if (modifycode != NOP_EXPR)
3671 rhs = c_fully_fold (rhs, false, NULL);
3673 /* Remove the qualifiers for the rest of the expressions and create
3674 the VAL temp variable to hold the RHS. */
3675 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3676 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3677 val = create_tmp_var_raw (nonatomic_rhs_type);
3678 TREE_ADDRESSABLE (val) = 1;
3679 TREE_NO_WARNING (val) = 1;
3680 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3682 SET_EXPR_LOCATION (rhs, loc);
3685 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3687 if (modifycode == NOP_EXPR)
3689 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3690 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3691 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3692 params->quick_push (lhs_addr);
3693 params->quick_push (rhs);
3694 params->quick_push (seq_cst);
3695 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3696 add_stmt (func_call);
3698 /* Finish the compound statement. */
3699 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3701 /* VAL is the value which was stored, return a COMPOUND_STMT of
3702 the statement and that value. */
3703 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3706 /* Create the variables and labels required for the op= form. */
3707 old = create_tmp_var_raw (nonatomic_lhs_type);
3708 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3709 TREE_ADDRESSABLE (old) = 1;
3710 TREE_NO_WARNING (old) = 1;
3712 newval = create_tmp_var_raw (nonatomic_lhs_type);
3713 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3714 TREE_ADDRESSABLE (newval) = 1;
3716 loop_decl = create_artificial_label (loc);
3717 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3719 done_decl = create_artificial_label (loc);
3720 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3722 /* __atomic_load (addr, &old, SEQ_CST). */
3723 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3724 params->quick_push (lhs_addr);
3725 params->quick_push (old_addr);
3726 params->quick_push (seq_cst);
3727 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3728 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3731 params->truncate (0);
3733 /* Create the expressions for floating-point environment
3734 manipulation, if required. */
3735 bool need_fenv = (flag_trapping_math
3736 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3737 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3739 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3742 add_stmt (hold_call);
3745 add_stmt (loop_label);
3747 /* newval = old + val; */
3748 rhs = build_binary_op (loc, modifycode, old, val, 1);
3749 rhs = c_fully_fold (rhs, false, NULL);
3750 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3751 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
3753 if (rhs != error_mark_node)
3755 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3757 SET_EXPR_LOCATION (rhs, loc);
3761 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3763 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3764 params->quick_push (lhs_addr);
3765 params->quick_push (old_addr);
3766 params->quick_push (newval_addr);
3767 params->quick_push (integer_zero_node);
3768 params->quick_push (seq_cst);
3769 params->quick_push (seq_cst);
3770 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3772 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3773 SET_EXPR_LOCATION (goto_stmt, loc);
3775 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3776 SET_EXPR_LOCATION (stmt, loc);
3780 add_stmt (clear_call);
3783 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3784 SET_EXPR_LOCATION (goto_stmt, loc);
3785 add_stmt (goto_stmt);
3788 add_stmt (done_label);
3791 add_stmt (update_call);
3793 /* Finish the compound statement. */
3794 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3796 /* NEWVAL is the value that was successfully stored, return a
3797 COMPOUND_EXPR of the statement and the appropriate value. */
3798 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3799 return_old_p ? old : newval);
3802 /* Construct and perhaps optimize a tree representation
3803 for a unary operation. CODE, a tree_code, specifies the operation
3804 and XARG is the operand.
3805 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3806 the default promotions (such as from short to int).
3807 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3808 allows non-lvalues; this is only used to handle conversion of non-lvalue
3809 arrays to pointers in C99.
3811 LOCATION is the location of the operator. */
3814 build_unary_op (location_t location,
3815 enum tree_code code, tree xarg, int flag)
3817 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3820 enum tree_code typecode;
3822 tree ret = error_mark_node;
3823 tree eptype = NULL_TREE;
3824 int noconvert = flag;
3825 const char *invalid_op_diag;
3828 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3830 arg = remove_c_maybe_const_expr (arg);
3832 if (code != ADDR_EXPR)
3833 arg = require_complete_type (arg);
3835 typecode = TREE_CODE (TREE_TYPE (arg));
3836 if (typecode == ERROR_MARK)
3837 return error_mark_node;
3838 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3839 typecode = INTEGER_TYPE;
3841 if ((invalid_op_diag
3842 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3844 error_at (location, invalid_op_diag);
3845 return error_mark_node;
3848 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3850 eptype = TREE_TYPE (arg);
3851 arg = TREE_OPERAND (arg, 0);
3857 /* This is used for unary plus, because a CONVERT_EXPR
3858 is enough to prevent anybody from looking inside for
3859 associativity, but won't generate any code. */
3860 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3861 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3862 || typecode == VECTOR_TYPE))
3864 error_at (location, "wrong type argument to unary plus");
3865 return error_mark_node;
3867 else if (!noconvert)
3868 arg = default_conversion (arg);
3869 arg = non_lvalue_loc (location, arg);
3873 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3874 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3875 || typecode == VECTOR_TYPE))
3877 error_at (location, "wrong type argument to unary minus");
3878 return error_mark_node;
3880 else if (!noconvert)
3881 arg = default_conversion (arg);
3885 /* ~ works on integer types and non float vectors. */
3886 if (typecode == INTEGER_TYPE
3887 || (typecode == VECTOR_TYPE
3888 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3891 arg = default_conversion (arg);
3893 else if (typecode == COMPLEX_TYPE)
3896 pedwarn (location, OPT_Wpedantic,
3897 "ISO C does not support %<~%> for complex conjugation");
3899 arg = default_conversion (arg);
3903 error_at (location, "wrong type argument to bit-complement");
3904 return error_mark_node;
3909 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3911 error_at (location, "wrong type argument to abs");
3912 return error_mark_node;
3914 else if (!noconvert)
3915 arg = default_conversion (arg);
3919 /* Conjugating a real value is a no-op, but allow it anyway. */
3920 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3921 || typecode == COMPLEX_TYPE))
3923 error_at (location, "wrong type argument to conjugation");
3924 return error_mark_node;
3926 else if (!noconvert)
3927 arg = default_conversion (arg);
3930 case TRUTH_NOT_EXPR:
3931 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3932 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3933 && typecode != COMPLEX_TYPE)
3936 "wrong type argument to unary exclamation mark");
3937 return error_mark_node;
3941 arg = c_objc_common_truthvalue_conversion (location, xarg);
3942 arg = remove_c_maybe_const_expr (arg);
3945 arg = c_objc_common_truthvalue_conversion (location, arg);
3946 ret = invert_truthvalue_loc (location, arg);
3947 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3948 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3949 location = EXPR_LOCATION (ret);
3950 goto return_build_unary_op;
3954 ret = build_real_imag_expr (location, code, arg);
3955 if (ret == error_mark_node)
3956 return error_mark_node;
3957 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3958 eptype = TREE_TYPE (eptype);
3959 goto return_build_unary_op;
3961 case PREINCREMENT_EXPR:
3962 case POSTINCREMENT_EXPR:
3963 case PREDECREMENT_EXPR:
3964 case POSTDECREMENT_EXPR:
3966 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3968 tree inner = build_unary_op (location, code,
3969 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3970 if (inner == error_mark_node)
3971 return error_mark_node;
3972 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3973 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3974 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3975 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3976 goto return_build_unary_op;
3979 /* Complain about anything that is not a true lvalue. In
3980 Objective-C, skip this check for property_refs. */
3981 if (!objc_is_property_ref (arg)
3982 && !lvalue_or_else (location,
3983 arg, ((code == PREINCREMENT_EXPR
3984 || code == POSTINCREMENT_EXPR)
3987 return error_mark_node;
3989 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3991 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3992 warning_at (location, OPT_Wc___compat,
3993 "increment of enumeration value is invalid in C++");
3995 warning_at (location, OPT_Wc___compat,
3996 "decrement of enumeration value is invalid in C++");
3999 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4000 arg = c_fully_fold (arg, false, NULL);
4003 atomic_op = really_atomic_lvalue (arg);
4005 /* Increment or decrement the real part of the value,
4006 and don't change the imaginary part. */
4007 if (typecode == COMPLEX_TYPE)
4011 pedwarn (location, OPT_Wpedantic,
4012 "ISO C does not support %<++%> and %<--%> on complex types");
4016 arg = stabilize_reference (arg);
4017 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4018 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4019 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4020 if (real == error_mark_node || imag == error_mark_node)
4021 return error_mark_node;
4022 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4024 goto return_build_unary_op;
4028 /* Report invalid types. */
4030 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4031 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4032 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4034 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4035 error_at (location, "wrong type argument to increment");
4037 error_at (location, "wrong type argument to decrement");
4039 return error_mark_node;
4045 argtype = TREE_TYPE (arg);
4047 /* Compute the increment. */
4049 if (typecode == POINTER_TYPE)
4051 /* If pointer target is an incomplete type,
4052 we just cannot know how to do the arithmetic. */
4053 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4055 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4057 "increment of pointer to an incomplete type %qT",
4058 TREE_TYPE (argtype));
4061 "decrement of pointer to an incomplete type %qT",
4062 TREE_TYPE (argtype));
4064 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4065 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4067 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4068 pedwarn (location, OPT_Wpointer_arith,
4069 "wrong type argument to increment");
4071 pedwarn (location, OPT_Wpointer_arith,
4072 "wrong type argument to decrement");
4075 inc = c_size_in_bytes (TREE_TYPE (argtype));
4076 inc = convert_to_ptrofftype_loc (location, inc);
4078 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4080 /* For signed fract types, we invert ++ to -- or
4081 -- to ++, and change inc from 1 to -1, because
4082 it is not possible to represent 1 in signed fract constants.
4083 For unsigned fract types, the result always overflows and
4084 we get an undefined (original) or the maximum value. */
4085 if (code == PREINCREMENT_EXPR)
4086 code = PREDECREMENT_EXPR;
4087 else if (code == PREDECREMENT_EXPR)
4088 code = PREINCREMENT_EXPR;
4089 else if (code == POSTINCREMENT_EXPR)
4090 code = POSTDECREMENT_EXPR;
4091 else /* code == POSTDECREMENT_EXPR */
4092 code = POSTINCREMENT_EXPR;
4094 inc = integer_minus_one_node;
4095 inc = convert (argtype, inc);
4099 inc = VECTOR_TYPE_P (argtype)
4100 ? build_one_cst (argtype)
4102 inc = convert (argtype, inc);
4105 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4106 need to ask Objective-C to build the increment or decrement
4107 expression for it. */
4108 if (objc_is_property_ref (arg))
4109 return objc_build_incr_expr_for_property_ref (location, code,
4112 /* Report a read-only lvalue. */
4113 if (TYPE_READONLY (argtype))
4115 readonly_error (location, arg,
4116 ((code == PREINCREMENT_EXPR
4117 || code == POSTINCREMENT_EXPR)
4118 ? lv_increment : lv_decrement));
4119 return error_mark_node;
4121 else if (TREE_READONLY (arg))
4122 readonly_warning (arg,
4123 ((code == PREINCREMENT_EXPR
4124 || code == POSTINCREMENT_EXPR)
4125 ? lv_increment : lv_decrement));
4127 /* If the argument is atomic, use the special code sequences for
4128 atomic compound assignment. */
4131 arg = stabilize_reference (arg);
4132 ret = build_atomic_assign (location, arg,
4133 ((code == PREINCREMENT_EXPR
4134 || code == POSTINCREMENT_EXPR)
4137 (FRACT_MODE_P (TYPE_MODE (argtype))
4139 : integer_one_node),
4140 (code == POSTINCREMENT_EXPR
4141 || code == POSTDECREMENT_EXPR));
4142 goto return_build_unary_op;
4145 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4146 val = boolean_increment (code, arg);
4148 val = build2 (code, TREE_TYPE (arg), arg, inc);
4149 TREE_SIDE_EFFECTS (val) = 1;
4150 if (TREE_CODE (val) != code)
4151 TREE_NO_WARNING (val) = 1;
4153 goto return_build_unary_op;
4157 /* Note that this operation never does default_conversion. */
4159 /* The operand of unary '&' must be an lvalue (which excludes
4160 expressions of type void), or, in C99, the result of a [] or
4161 unary '*' operator. */
4162 if (VOID_TYPE_P (TREE_TYPE (arg))
4163 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4164 && (TREE_CODE (arg) != INDIRECT_REF
4166 pedwarn (location, 0, "taking address of expression of type %<void%>");
4168 /* Let &* cancel out to simplify resulting code. */
4169 if (TREE_CODE (arg) == INDIRECT_REF)
4171 /* Don't let this be an lvalue. */
4172 if (lvalue_p (TREE_OPERAND (arg, 0)))
4173 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4174 ret = TREE_OPERAND (arg, 0);
4175 goto return_build_unary_op;
4178 /* For &x[y], return x+y */
4179 if (TREE_CODE (arg) == ARRAY_REF)
4181 tree op0 = TREE_OPERAND (arg, 0);
4182 if (!c_mark_addressable (op0))
4183 return error_mark_node;
4186 /* Anything not already handled and not a true memory reference
4187 or a non-lvalue array is an error. */
4188 else if (typecode != FUNCTION_TYPE && !flag
4189 && !lvalue_or_else (location, arg, lv_addressof))
4190 return error_mark_node;
4192 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4194 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4196 tree inner = build_unary_op (location, code,
4197 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4198 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4199 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4200 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4201 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4202 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4203 goto return_build_unary_op;
4206 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4207 argtype = TREE_TYPE (arg);
4209 /* If the lvalue is const or volatile, merge that into the type
4210 to which the address will point. This is only needed
4211 for function types. */
4212 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4213 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4214 && TREE_CODE (argtype) == FUNCTION_TYPE)
4216 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4217 int quals = orig_quals;
4219 if (TREE_READONLY (arg))
4220 quals |= TYPE_QUAL_CONST;
4221 if (TREE_THIS_VOLATILE (arg))
4222 quals |= TYPE_QUAL_VOLATILE;
4224 argtype = c_build_qualified_type (argtype, quals);
4227 if (!c_mark_addressable (arg))
4228 return error_mark_node;
4230 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4231 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4233 argtype = build_pointer_type (argtype);
4235 /* ??? Cope with user tricks that amount to offsetof. Delete this
4236 when we have proper support for integer constant expressions. */
4237 val = get_base_address (arg);
4238 if (val && TREE_CODE (val) == INDIRECT_REF
4239 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4241 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
4242 goto return_build_unary_op;
4245 val = build1 (ADDR_EXPR, argtype, arg);
4248 goto return_build_unary_op;
4255 argtype = TREE_TYPE (arg);
4256 if (TREE_CODE (arg) == INTEGER_CST)
4257 ret = (require_constant_value
4258 ? fold_build1_initializer_loc (location, code, argtype, arg)
4259 : fold_build1_loc (location, code, argtype, arg));
4261 ret = build1 (code, argtype, arg);
4262 return_build_unary_op:
4263 gcc_assert (ret != error_mark_node);
4264 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4265 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4266 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4267 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4268 ret = note_integer_operands (ret);
4270 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4271 protected_set_expr_location (ret, location);
4275 /* Return nonzero if REF is an lvalue valid for this language.
4276 Lvalues can be assigned, unless their type has TYPE_READONLY.
4277 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4280 lvalue_p (const_tree ref)
4282 const enum tree_code code = TREE_CODE (ref);
4289 return lvalue_p (TREE_OPERAND (ref, 0));
4291 case C_MAYBE_CONST_EXPR:
4292 return lvalue_p (TREE_OPERAND (ref, 1));
4294 case COMPOUND_LITERAL_EXPR:
4300 case ARRAY_NOTATION_REF:
4305 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4306 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4309 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4316 /* Give a warning for storing in something that is read-only in GCC
4317 terms but not const in ISO C terms. */
4320 readonly_warning (tree arg, enum lvalue_use use)
4325 warning (0, "assignment of read-only location %qE", arg);
4328 warning (0, "increment of read-only location %qE", arg);
4331 warning (0, "decrement of read-only location %qE", arg);
4340 /* Return nonzero if REF is an lvalue valid for this language;
4341 otherwise, print an error message and return zero. USE says
4342 how the lvalue is being used and so selects the error message.
4343 LOCATION is the location at which any error should be reported. */
4346 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4348 int win = lvalue_p (ref);
4351 lvalue_error (loc, use);
4356 /* Mark EXP saying that we need to be able to take the
4357 address of it; it should not be allocated in a register.
4358 Returns true if successful. */
4361 c_mark_addressable (tree exp)
4366 switch (TREE_CODE (x))
4369 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4372 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4376 /* ... fall through ... */
4382 x = TREE_OPERAND (x, 0);
4385 case COMPOUND_LITERAL_EXPR:
4387 TREE_ADDRESSABLE (x) = 1;
4394 if (C_DECL_REGISTER (x)
4395 && DECL_NONLOCAL (x))
4397 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4400 ("global register variable %qD used in nested function", x);
4403 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4405 else if (C_DECL_REGISTER (x))
4407 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4408 error ("address of global register variable %qD requested", x);
4410 error ("address of register variable %qD requested", x);
4416 TREE_ADDRESSABLE (x) = 1;
4423 /* Convert EXPR to TYPE, warning about conversion problems with
4424 constants. SEMANTIC_TYPE is the type this conversion would use
4425 without excess precision. If SEMANTIC_TYPE is NULL, this function
4426 is equivalent to convert_and_check. This function is a wrapper that
4427 handles conversions that may be different than
4428 the usual ones because of excess precision. */
4431 ep_convert_and_check (location_t loc, tree type, tree expr,
4434 if (TREE_TYPE (expr) == type)
4438 return convert_and_check (loc, type, expr);
4440 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4441 && TREE_TYPE (expr) != semantic_type)
4443 /* For integers, we need to check the real conversion, not
4444 the conversion to the excess precision type. */
4445 expr = convert_and_check (loc, semantic_type, expr);
4447 /* Result type is the excess precision type, which should be
4448 large enough, so do not check. */
4449 return convert (type, expr);
4452 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4453 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4454 if folded to an integer constant then the unselected half may
4455 contain arbitrary operations not normally permitted in constant
4456 expressions. Set the location of the expression to LOC. */
4459 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4460 tree op1, tree op1_original_type, tree op2,
4461 tree op2_original_type)
4465 enum tree_code code1;
4466 enum tree_code code2;
4467 tree result_type = NULL;
4468 tree semantic_result_type = NULL;
4469 tree orig_op1 = op1, orig_op2 = op2;
4470 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4471 bool ifexp_int_operands;
4474 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4475 if (op1_int_operands)
4476 op1 = remove_c_maybe_const_expr (op1);
4477 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4478 if (op2_int_operands)
4479 op2 = remove_c_maybe_const_expr (op2);
4480 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4481 if (ifexp_int_operands)
4482 ifexp = remove_c_maybe_const_expr (ifexp);
4484 /* Promote both alternatives. */
4486 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4487 op1 = default_conversion (op1);
4488 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4489 op2 = default_conversion (op2);
4491 if (TREE_CODE (ifexp) == ERROR_MARK
4492 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4493 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4494 return error_mark_node;
4496 type1 = TREE_TYPE (op1);
4497 code1 = TREE_CODE (type1);
4498 type2 = TREE_TYPE (op2);
4499 code2 = TREE_CODE (type2);
4501 /* C90 does not permit non-lvalue arrays in conditional expressions.
4502 In C99 they will be pointers by now. */
4503 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4505 error_at (colon_loc, "non-lvalue array in conditional expression");
4506 return error_mark_node;
4509 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4510 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4511 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4512 || code1 == COMPLEX_TYPE)
4513 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4514 || code2 == COMPLEX_TYPE))
4516 semantic_result_type = c_common_type (type1, type2);
4517 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4519 op1 = TREE_OPERAND (op1, 0);
4520 type1 = TREE_TYPE (op1);
4521 gcc_assert (TREE_CODE (type1) == code1);
4523 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4525 op2 = TREE_OPERAND (op2, 0);
4526 type2 = TREE_TYPE (op2);
4527 gcc_assert (TREE_CODE (type2) == code2);
4531 if (warn_cxx_compat)
4533 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4534 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4536 if (TREE_CODE (t1) == ENUMERAL_TYPE
4537 && TREE_CODE (t2) == ENUMERAL_TYPE
4538 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4539 warning_at (colon_loc, OPT_Wc___compat,
4540 ("different enum types in conditional is "
4541 "invalid in C++: %qT vs %qT"),
4545 /* Quickly detect the usual case where op1 and op2 have the same type
4547 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4550 result_type = type1;
4552 result_type = TYPE_MAIN_VARIANT (type1);
4554 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4555 || code1 == COMPLEX_TYPE)
4556 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4557 || code2 == COMPLEX_TYPE))
4559 result_type = c_common_type (type1, type2);
4560 do_warn_double_promotion (result_type, type1, type2,
4561 "implicit conversion from %qT to %qT to "
4562 "match other result of conditional",
4565 /* If -Wsign-compare, warn here if type1 and type2 have
4566 different signedness. We'll promote the signed to unsigned
4567 and later code won't know it used to be different.
4568 Do this check on the original types, so that explicit casts
4569 will be considered, but default promotions won't. */
4570 if (c_inhibit_evaluation_warnings == 0)
4572 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4573 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4575 if (unsigned_op1 ^ unsigned_op2)
4579 /* Do not warn if the result type is signed, since the
4580 signed type will only be chosen if it can represent
4581 all the values of the unsigned type. */
4582 if (!TYPE_UNSIGNED (result_type))
4586 bool op1_maybe_const = true;
4587 bool op2_maybe_const = true;
4589 /* Do not warn if the signed quantity is an
4590 unsuffixed integer literal (or some static
4591 constant expression involving such literals) and
4592 it is non-negative. This warning requires the
4593 operands to be folded for best results, so do
4594 that folding in this case even without
4595 warn_sign_compare to avoid warning options
4596 possibly affecting code generation. */
4597 c_inhibit_evaluation_warnings
4598 += (ifexp == truthvalue_false_node);
4599 op1 = c_fully_fold (op1, require_constant_value,
4601 c_inhibit_evaluation_warnings
4602 -= (ifexp == truthvalue_false_node);
4604 c_inhibit_evaluation_warnings
4605 += (ifexp == truthvalue_true_node);
4606 op2 = c_fully_fold (op2, require_constant_value,
4608 c_inhibit_evaluation_warnings
4609 -= (ifexp == truthvalue_true_node);
4611 if (warn_sign_compare)
4614 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4616 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4619 warning_at (colon_loc, OPT_Wsign_compare,
4620 ("signed and unsigned type in "
4621 "conditional expression"));
4623 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4624 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4625 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4626 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4631 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4633 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4634 pedwarn (colon_loc, OPT_Wpedantic,
4635 "ISO C forbids conditional expr with only one void side");
4636 result_type = void_type_node;
4638 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4640 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4641 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4642 addr_space_t as_common;
4644 if (comp_target_types (colon_loc, type1, type2))
4645 result_type = common_pointer_type (type1, type2);
4646 else if (null_pointer_constant_p (orig_op1))
4647 result_type = type2;
4648 else if (null_pointer_constant_p (orig_op2))
4649 result_type = type1;
4650 else if (!addr_space_superset (as1, as2, &as_common))
4652 error_at (colon_loc, "pointers to disjoint address spaces "
4653 "used in conditional expression");
4654 return error_mark_node;
4656 else if (VOID_TYPE_P (TREE_TYPE (type1))
4657 && !TYPE_ATOMIC (TREE_TYPE (type1)))
4659 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4660 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4661 & ~TYPE_QUALS (TREE_TYPE (type1))))
4662 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4663 "pointer to array loses qualifier "
4664 "in conditional expression");
4666 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4667 pedwarn (colon_loc, OPT_Wpedantic,
4668 "ISO C forbids conditional expr between "
4669 "%<void *%> and function pointer");
4670 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4671 TREE_TYPE (type2)));
4673 else if (VOID_TYPE_P (TREE_TYPE (type2))
4674 && !TYPE_ATOMIC (TREE_TYPE (type2)))
4676 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4677 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4678 & ~TYPE_QUALS (TREE_TYPE (type2))))
4679 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4680 "pointer to array loses qualifier "
4681 "in conditional expression");
4683 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4684 pedwarn (colon_loc, OPT_Wpedantic,
4685 "ISO C forbids conditional expr between "
4686 "%<void *%> and function pointer");
4687 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4688 TREE_TYPE (type1)));
4690 /* Objective-C pointer comparisons are a bit more lenient. */
4691 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4692 result_type = objc_common_type (type1, type2);
4695 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4697 pedwarn (colon_loc, 0,
4698 "pointer type mismatch in conditional expression");
4699 result_type = build_pointer_type
4700 (build_qualified_type (void_type_node, qual));
4703 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4705 if (!null_pointer_constant_p (orig_op2))
4706 pedwarn (colon_loc, 0,
4707 "pointer/integer type mismatch in conditional expression");
4710 op2 = null_pointer_node;
4712 result_type = type1;
4714 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4716 if (!null_pointer_constant_p (orig_op1))
4717 pedwarn (colon_loc, 0,
4718 "pointer/integer type mismatch in conditional expression");
4721 op1 = null_pointer_node;
4723 result_type = type2;
4728 if (flag_cond_mismatch)
4729 result_type = void_type_node;
4732 error_at (colon_loc, "type mismatch in conditional expression");
4733 return error_mark_node;
4737 /* Merge const and volatile flags of the incoming types. */
4739 = build_type_variant (result_type,
4740 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4741 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4743 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4744 semantic_result_type);
4745 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4746 semantic_result_type);
4748 if (ifexp_bcp && ifexp == truthvalue_true_node)
4750 op2_int_operands = true;
4751 op1 = c_fully_fold (op1, require_constant_value, NULL);
4753 if (ifexp_bcp && ifexp == truthvalue_false_node)
4755 op1_int_operands = true;
4756 op2 = c_fully_fold (op2, require_constant_value, NULL);
4758 int_const = int_operands = (ifexp_int_operands
4760 && op2_int_operands);
4763 int_const = ((ifexp == truthvalue_true_node
4764 && TREE_CODE (orig_op1) == INTEGER_CST
4765 && !TREE_OVERFLOW (orig_op1))
4766 || (ifexp == truthvalue_false_node
4767 && TREE_CODE (orig_op2) == INTEGER_CST
4768 && !TREE_OVERFLOW (orig_op2)));
4770 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4771 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4776 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4777 nested inside of the expression. */
4778 op1 = c_fully_fold (op1, false, NULL);
4779 op2 = c_fully_fold (op2, false, NULL);
4781 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4783 ret = note_integer_operands (ret);
4785 if (semantic_result_type)
4786 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4788 protected_set_expr_location (ret, colon_loc);
4792 /* Return a compound expression that performs two expressions and
4793 returns the value of the second of them.
4795 LOC is the location of the COMPOUND_EXPR. */
4798 build_compound_expr (location_t loc, tree expr1, tree expr2)
4800 bool expr1_int_operands, expr2_int_operands;
4801 tree eptype = NULL_TREE;
4805 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4806 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4809 "spawned function call cannot be part of a comma expression");
4810 return error_mark_node;
4812 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4813 if (expr1_int_operands)
4814 expr1 = remove_c_maybe_const_expr (expr1);
4815 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4816 if (expr2_int_operands)
4817 expr2 = remove_c_maybe_const_expr (expr2);
4819 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4820 expr1 = TREE_OPERAND (expr1, 0);
4821 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4823 eptype = TREE_TYPE (expr2);
4824 expr2 = TREE_OPERAND (expr2, 0);
4827 if (!TREE_SIDE_EFFECTS (expr1))
4829 /* The left-hand operand of a comma expression is like an expression
4830 statement: with -Wunused, we should warn if it doesn't have
4831 any side-effects, unless it was explicitly cast to (void). */
4832 if (warn_unused_value)
4834 if (VOID_TYPE_P (TREE_TYPE (expr1))
4835 && CONVERT_EXPR_P (expr1))
4837 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4838 && TREE_CODE (expr1) == COMPOUND_EXPR
4839 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4840 ; /* (void) a, (void) b, c */
4842 warning_at (loc, OPT_Wunused_value,
4843 "left-hand operand of comma expression has no effect");
4846 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4847 && warn_unused_value)
4850 location_t cloc = loc;
4851 while (TREE_CODE (r) == COMPOUND_EXPR)
4853 if (EXPR_HAS_LOCATION (r))
4854 cloc = EXPR_LOCATION (r);
4855 r = TREE_OPERAND (r, 1);
4857 if (!TREE_SIDE_EFFECTS (r)
4858 && !VOID_TYPE_P (TREE_TYPE (r))
4859 && !CONVERT_EXPR_P (r))
4860 warning_at (cloc, OPT_Wunused_value,
4861 "right-hand operand of comma expression has no effect");
4864 /* With -Wunused, we should also warn if the left-hand operand does have
4865 side-effects, but computes a value which is not used. For example, in
4866 `foo() + bar(), baz()' the result of the `+' operator is not used,
4867 so we should issue a warning. */
4868 else if (warn_unused_value)
4869 warn_if_unused_value (expr1, loc);
4871 if (expr2 == error_mark_node)
4872 return error_mark_node;
4874 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4877 && expr1_int_operands
4878 && expr2_int_operands)
4879 ret = note_integer_operands (ret);
4882 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4884 protected_set_expr_location (ret, loc);
4888 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4889 which we are casting. OTYPE is the type of the expression being
4890 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4891 of the cast. -Wcast-qual appeared on the command line. Named
4892 address space qualifiers are not handled here, because they result
4893 in different warnings. */
4896 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4898 tree in_type = type;
4899 tree in_otype = otype;
4904 /* Check that the qualifiers on IN_TYPE are a superset of the
4905 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4906 nodes is uninteresting and we stop as soon as we hit a
4907 non-POINTER_TYPE node on either type. */
4910 in_otype = TREE_TYPE (in_otype);
4911 in_type = TREE_TYPE (in_type);
4913 /* GNU C allows cv-qualified function types. 'const' means the
4914 function is very pure, 'volatile' means it can't return. We
4915 need to warn when such qualifiers are added, not when they're
4917 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4918 && TREE_CODE (in_type) == FUNCTION_TYPE)
4919 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4920 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4922 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4923 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4925 while (TREE_CODE (in_type) == POINTER_TYPE
4926 && TREE_CODE (in_otype) == POINTER_TYPE);
4929 warning_at (loc, OPT_Wcast_qual,
4930 "cast adds %q#v qualifier to function type", added);
4933 /* There are qualifiers present in IN_OTYPE that are not present
4935 warning_at (loc, OPT_Wcast_qual,
4936 "cast discards %qv qualifier from pointer target type",
4939 if (added || discarded)
4942 /* A cast from **T to const **T is unsafe, because it can cause a
4943 const value to be changed with no additional warning. We only
4944 issue this warning if T is the same on both sides, and we only
4945 issue the warning if there are the same number of pointers on
4946 both sides, as otherwise the cast is clearly unsafe anyhow. A
4947 cast is unsafe when a qualifier is added at one level and const
4948 is not present at all outer levels.
4950 To issue this warning, we check at each level whether the cast
4951 adds new qualifiers not already seen. We don't need to special
4952 case function types, as they won't have the same
4953 TYPE_MAIN_VARIANT. */
4955 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4957 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4962 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4965 in_type = TREE_TYPE (in_type);
4966 in_otype = TREE_TYPE (in_otype);
4967 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4970 warning_at (loc, OPT_Wcast_qual,
4971 "to be safe all intermediate pointers in cast from "
4972 "%qT to %qT must be %<const%> qualified",
4977 is_const = TYPE_READONLY (in_type);
4979 while (TREE_CODE (in_type) == POINTER_TYPE);
4982 /* Build an expression representing a cast to type TYPE of expression EXPR.
4983 LOC is the location of the cast-- typically the open paren of the cast. */
4986 build_c_cast (location_t loc, tree type, tree expr)
4990 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4991 expr = TREE_OPERAND (expr, 0);
4995 if (type == error_mark_node || expr == error_mark_node)
4996 return error_mark_node;
4998 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4999 only in <protocol> qualifications. But when constructing cast expressions,
5000 the protocols do matter and must be kept around. */
5001 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5002 return build1 (NOP_EXPR, type, expr);
5004 type = TYPE_MAIN_VARIANT (type);
5006 if (TREE_CODE (type) == ARRAY_TYPE)
5008 error_at (loc, "cast specifies array type");
5009 return error_mark_node;
5012 if (TREE_CODE (type) == FUNCTION_TYPE)
5014 error_at (loc, "cast specifies function type");
5015 return error_mark_node;
5018 if (!VOID_TYPE_P (type))
5020 value = require_complete_type (value);
5021 if (value == error_mark_node)
5022 return error_mark_node;
5025 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5027 if (TREE_CODE (type) == RECORD_TYPE
5028 || TREE_CODE (type) == UNION_TYPE)
5029 pedwarn (loc, OPT_Wpedantic,
5030 "ISO C forbids casting nonscalar to the same type");
5032 /* Convert to remove any qualifiers from VALUE's type. */
5033 value = convert (type, value);
5035 else if (TREE_CODE (type) == UNION_TYPE)
5039 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5040 if (TREE_TYPE (field) != error_mark_node
5041 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5042 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5048 bool maybe_const = true;
5050 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5051 t = c_fully_fold (value, false, &maybe_const);
5052 t = build_constructor_single (type, field, t);
5054 t = c_wrap_maybe_const (t, true);
5055 t = digest_init (loc, type, t,
5056 NULL_TREE, false, true, 0);
5057 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5060 error_at (loc, "cast to union type from type not present in union");
5061 return error_mark_node;
5067 if (type == void_type_node)
5069 tree t = build1 (CONVERT_EXPR, type, value);
5070 SET_EXPR_LOCATION (t, loc);
5074 otype = TREE_TYPE (value);
5076 /* Optionally warn about potentially worrisome casts. */
5078 && TREE_CODE (type) == POINTER_TYPE
5079 && TREE_CODE (otype) == POINTER_TYPE)
5080 handle_warn_cast_qual (loc, type, otype);
5082 /* Warn about conversions between pointers to disjoint
5084 if (TREE_CODE (type) == POINTER_TYPE
5085 && TREE_CODE (otype) == POINTER_TYPE
5086 && !null_pointer_constant_p (value))
5088 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5089 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5090 addr_space_t as_common;
5092 if (!addr_space_superset (as_to, as_from, &as_common))
5094 if (ADDR_SPACE_GENERIC_P (as_from))
5095 warning_at (loc, 0, "cast to %s address space pointer "
5096 "from disjoint generic address space pointer",
5097 c_addr_space_name (as_to));
5099 else if (ADDR_SPACE_GENERIC_P (as_to))
5100 warning_at (loc, 0, "cast to generic address space pointer "
5101 "from disjoint %s address space pointer",
5102 c_addr_space_name (as_from));
5105 warning_at (loc, 0, "cast to %s address space pointer "
5106 "from disjoint %s address space pointer",
5107 c_addr_space_name (as_to),
5108 c_addr_space_name (as_from));
5112 /* Warn about possible alignment problems. */
5113 if (STRICT_ALIGNMENT
5114 && TREE_CODE (type) == POINTER_TYPE
5115 && TREE_CODE (otype) == POINTER_TYPE
5116 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5117 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5118 /* Don't warn about opaque types, where the actual alignment
5119 restriction is unknown. */
5120 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5121 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5122 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5123 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5124 warning_at (loc, OPT_Wcast_align,
5125 "cast increases required alignment of target type");
5127 if (TREE_CODE (type) == INTEGER_TYPE
5128 && TREE_CODE (otype) == POINTER_TYPE
5129 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5130 /* Unlike conversion of integers to pointers, where the
5131 warning is disabled for converting constants because
5132 of cases such as SIG_*, warn about converting constant
5133 pointers to integers. In some cases it may cause unwanted
5134 sign extension, and a warning is appropriate. */
5135 warning_at (loc, OPT_Wpointer_to_int_cast,
5136 "cast from pointer to integer of different size");
5138 if (TREE_CODE (value) == CALL_EXPR
5139 && TREE_CODE (type) != TREE_CODE (otype))
5140 warning_at (loc, OPT_Wbad_function_cast,
5141 "cast from function call of type %qT "
5142 "to non-matching type %qT", otype, type);
5144 if (TREE_CODE (type) == POINTER_TYPE
5145 && TREE_CODE (otype) == INTEGER_TYPE
5146 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5147 /* Don't warn about converting any constant. */
5148 && !TREE_CONSTANT (value))
5150 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5151 "of different size");
5153 if (warn_strict_aliasing <= 2)
5154 strict_aliasing_warning (otype, type, expr);
5156 /* If pedantic, warn for conversions between function and object
5157 pointer types, except for converting a null pointer constant
5158 to function pointer type. */
5160 && TREE_CODE (type) == POINTER_TYPE
5161 && TREE_CODE (otype) == POINTER_TYPE
5162 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5163 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5164 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5165 "conversion of function pointer to object pointer type");
5168 && TREE_CODE (type) == POINTER_TYPE
5169 && TREE_CODE (otype) == POINTER_TYPE
5170 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5171 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5172 && !null_pointer_constant_p (value))
5173 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5174 "conversion of object pointer to function pointer type");
5177 value = convert (type, value);
5179 /* Ignore any integer overflow caused by the cast. */
5180 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5182 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5184 if (!TREE_OVERFLOW (value))
5186 /* Avoid clobbering a shared constant. */
5187 value = copy_node (value);
5188 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5191 else if (TREE_OVERFLOW (value))
5192 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5193 value = wide_int_to_tree (TREE_TYPE (value), value);
5197 /* Don't let a cast be an lvalue. */
5199 value = non_lvalue_loc (loc, value);
5201 /* Don't allow the results of casting to floating-point or complex
5202 types be confused with actual constants, or casts involving
5203 integer and pointer types other than direct integer-to-integer
5204 and integer-to-pointer be confused with integer constant
5205 expressions and null pointer constants. */
5206 if (TREE_CODE (value) == REAL_CST
5207 || TREE_CODE (value) == COMPLEX_CST
5208 || (TREE_CODE (value) == INTEGER_CST
5209 && !((TREE_CODE (expr) == INTEGER_CST
5210 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5211 || TREE_CODE (expr) == REAL_CST
5212 || TREE_CODE (expr) == COMPLEX_CST)))
5213 value = build1 (NOP_EXPR, type, value);
5215 if (CAN_HAVE_LOCATION_P (value))
5216 SET_EXPR_LOCATION (value, loc);
5220 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5221 location of the open paren of the cast, or the position of the cast
5224 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5227 tree type_expr = NULL_TREE;
5228 bool type_expr_const = true;
5230 int saved_wsp = warn_strict_prototypes;
5232 /* This avoids warnings about unprototyped casts on
5233 integers. E.g. "#define SIG_DFL (void(*)())0". */
5234 if (TREE_CODE (expr) == INTEGER_CST)
5235 warn_strict_prototypes = 0;
5236 type = groktypename (type_name, &type_expr, &type_expr_const);
5237 warn_strict_prototypes = saved_wsp;
5239 ret = build_c_cast (loc, type, expr);
5242 bool inner_expr_const = true;
5243 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5244 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5245 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5246 && inner_expr_const);
5247 SET_EXPR_LOCATION (ret, loc);
5250 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5251 SET_EXPR_LOCATION (ret, loc);
5253 /* C++ does not permits types to be defined in a cast, but it
5254 allows references to incomplete types. */
5255 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5256 warning_at (loc, OPT_Wc___compat,
5257 "defining a type in a cast is invalid in C++");
5262 /* Build an assignment expression of lvalue LHS from value RHS.
5263 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5264 may differ from TREE_TYPE (LHS) for an enum bitfield.
5265 MODIFYCODE is the code for a binary operator that we use
5266 to combine the old value of LHS with RHS to get the new value.
5267 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5268 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5269 which may differ from TREE_TYPE (RHS) for an enum value.
5271 LOCATION is the location of the MODIFYCODE operator.
5272 RHS_LOC is the location of the RHS. */
5275 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
5276 enum tree_code modifycode,
5277 location_t rhs_loc, tree rhs, tree rhs_origtype)
5281 tree rhseval = NULL_TREE;
5282 tree rhs_semantic_type = NULL_TREE;
5283 tree lhstype = TREE_TYPE (lhs);
5284 tree olhstype = lhstype;
5288 /* Types that aren't fully specified cannot be used in assignments. */
5289 lhs = require_complete_type (lhs);
5291 /* Avoid duplicate error messages from operands that had errors. */
5292 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5293 return error_mark_node;
5295 /* Ensure an error for assigning a non-lvalue array to an array in
5297 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5299 error_at (location, "assignment to expression with array type");
5300 return error_mark_node;
5303 /* For ObjC properties, defer this check. */
5304 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
5305 return error_mark_node;
5307 is_atomic_op = really_atomic_lvalue (lhs);
5309 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5311 rhs_semantic_type = TREE_TYPE (rhs);
5312 rhs = TREE_OPERAND (rhs, 0);
5317 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5319 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
5320 lhs_origtype, modifycode, rhs_loc, rhs,
5322 if (inner == error_mark_node)
5323 return error_mark_node;
5324 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5325 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5326 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5327 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5328 protected_set_expr_location (result, location);
5332 /* If a binary op has been requested, combine the old LHS value with the RHS
5333 producing the value we should actually store into the LHS. */
5335 if (modifycode != NOP_EXPR)
5337 lhs = c_fully_fold (lhs, false, NULL);
5338 lhs = stabilize_reference (lhs);
5340 /* Construct the RHS for any non-atomic compound assignemnt. */
5343 /* If in LHS op= RHS the RHS has side-effects, ensure they
5344 are preevaluated before the rest of the assignment expression's
5345 side-effects, because RHS could contain e.g. function calls
5347 if (TREE_SIDE_EFFECTS (rhs))
5349 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5352 newrhs = build_binary_op (location,
5353 modifycode, lhs, newrhs, 1);
5355 /* The original type of the right hand side is no longer
5357 rhs_origtype = NULL_TREE;
5361 if (c_dialect_objc ())
5363 /* Check if we are modifying an Objective-C property reference;
5364 if so, we need to generate setter calls. */
5365 result = objc_maybe_build_modify_expr (lhs, newrhs);
5369 /* Else, do the check that we postponed for Objective-C. */
5370 if (!lvalue_or_else (location, lhs, lv_assign))
5371 return error_mark_node;
5374 /* Give an error for storing in something that is 'const'. */
5376 if (TYPE_READONLY (lhstype)
5377 || ((TREE_CODE (lhstype) == RECORD_TYPE
5378 || TREE_CODE (lhstype) == UNION_TYPE)
5379 && C_TYPE_FIELDS_READONLY (lhstype)))
5381 readonly_error (location, lhs, lv_assign);
5382 return error_mark_node;
5384 else if (TREE_READONLY (lhs))
5385 readonly_warning (lhs, lv_assign);
5387 /* If storing into a structure or union member,
5388 it has probably been given type `int'.
5389 Compute the type that would go with
5390 the actual amount of storage the member occupies. */
5392 if (TREE_CODE (lhs) == COMPONENT_REF
5393 && (TREE_CODE (lhstype) == INTEGER_TYPE
5394 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5395 || TREE_CODE (lhstype) == REAL_TYPE
5396 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5397 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5399 /* If storing in a field that is in actuality a short or narrower than one,
5400 we must store in the field in its actual type. */
5402 if (lhstype != TREE_TYPE (lhs))
5404 lhs = copy_node (lhs);
5405 TREE_TYPE (lhs) = lhstype;
5408 /* Issue -Wc++-compat warnings about an assignment to an enum type
5409 when LHS does not have its original type. This happens for,
5410 e.g., an enum bitfield in a struct. */
5412 && lhs_origtype != NULL_TREE
5413 && lhs_origtype != lhstype
5414 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5416 tree checktype = (rhs_origtype != NULL_TREE
5419 if (checktype != error_mark_node
5420 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5421 || (is_atomic_op && modifycode != NOP_EXPR)))
5422 warning_at (location, OPT_Wc___compat,
5423 "enum conversion in assignment is invalid in C++");
5426 /* If the lhs is atomic, remove that qualifier. */
5429 lhstype = build_qualified_type (lhstype,
5430 (TYPE_QUALS (lhstype)
5431 & ~TYPE_QUAL_ATOMIC));
5432 olhstype = build_qualified_type (olhstype,
5433 (TYPE_QUALS (lhstype)
5434 & ~TYPE_QUAL_ATOMIC));
5437 /* Convert new value to destination type. Fold it first, then
5438 restore any excess precision information, for the sake of
5439 conversion warnings. */
5441 if (!(is_atomic_op && modifycode != NOP_EXPR))
5443 npc = null_pointer_constant_p (newrhs);
5444 newrhs = c_fully_fold (newrhs, false, NULL);
5445 if (rhs_semantic_type)
5446 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5447 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5448 rhs_origtype, ic_assign, npc,
5449 NULL_TREE, NULL_TREE, 0);
5450 if (TREE_CODE (newrhs) == ERROR_MARK)
5451 return error_mark_node;
5454 /* Emit ObjC write barrier, if necessary. */
5455 if (c_dialect_objc () && flag_objc_gc)
5457 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5460 protected_set_expr_location (result, location);
5465 /* Scan operands. */
5468 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5471 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5472 TREE_SIDE_EFFECTS (result) = 1;
5473 protected_set_expr_location (result, location);
5476 /* If we got the LHS in a different type for storing in,
5477 convert the result back to the nominal type of LHS
5478 so that the value we return always has the same type
5479 as the LHS argument. */
5481 if (olhstype == TREE_TYPE (result))
5484 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5485 rhs_origtype, ic_assign, false, NULL_TREE,
5487 protected_set_expr_location (result, location);
5491 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
5495 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5496 This is used to implement -fplan9-extensions. */
5499 find_anonymous_field_with_type (tree struct_type, tree type)
5504 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5505 || TREE_CODE (struct_type) == UNION_TYPE);
5507 for (field = TYPE_FIELDS (struct_type);
5509 field = TREE_CHAIN (field))
5511 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5512 ? c_build_qualified_type (TREE_TYPE (field),
5514 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5515 if (DECL_NAME (field) == NULL
5516 && comptypes (type, fieldtype))
5522 else if (DECL_NAME (field) == NULL
5523 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5524 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5525 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5535 /* RHS is an expression whose type is pointer to struct. If there is
5536 an anonymous field in RHS with type TYPE, then return a pointer to
5537 that field in RHS. This is used with -fplan9-extensions. This
5538 returns NULL if no conversion could be found. */
5541 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5543 tree rhs_struct_type, lhs_main_type;
5544 tree field, found_field;
5545 bool found_sub_field;
5548 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5549 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5550 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5551 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5553 gcc_assert (POINTER_TYPE_P (type));
5554 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5555 ? c_build_qualified_type (TREE_TYPE (type),
5557 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
5559 found_field = NULL_TREE;
5560 found_sub_field = false;
5561 for (field = TYPE_FIELDS (rhs_struct_type);
5563 field = TREE_CHAIN (field))
5565 if (DECL_NAME (field) != NULL_TREE
5566 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5567 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5569 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5570 ? c_build_qualified_type (TREE_TYPE (field),
5572 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5573 if (comptypes (lhs_main_type, fieldtype))
5575 if (found_field != NULL_TREE)
5577 found_field = field;
5579 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5582 if (found_field != NULL_TREE)
5584 found_field = field;
5585 found_sub_field = true;
5589 if (found_field == NULL_TREE)
5592 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5593 build_fold_indirect_ref (rhs), found_field,
5595 ret = build_fold_addr_expr_loc (location, ret);
5597 if (found_sub_field)
5599 ret = convert_to_anonymous_field (location, type, ret);
5600 gcc_assert (ret != NULL_TREE);
5606 /* Issue an error message for a bad initializer component.
5607 GMSGID identifies the message.
5608 The component name is taken from the spelling stack. */
5611 error_init (location_t loc, const char *gmsgid)
5615 /* The gmsgid may be a format string with %< and %>. */
5616 error_at (loc, gmsgid);
5617 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5619 inform (loc, "(near initialization for %qs)", ofwhat);
5622 /* Issue a pedantic warning for a bad initializer component. OPT is
5623 the option OPT_* (from options.h) controlling this warning or 0 if
5624 it is unconditionally given. GMSGID identifies the message. The
5625 component name is taken from the spelling stack. */
5628 pedwarn_init (location_t location, int opt, const char *gmsgid)
5633 /* The gmsgid may be a format string with %< and %>. */
5634 warned = pedwarn (location, opt, gmsgid);
5635 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5636 if (*ofwhat && warned)
5637 inform (location, "(near initialization for %qs)", ofwhat);
5640 /* Issue a warning for a bad initializer component.
5642 OPT is the OPT_W* value corresponding to the warning option that
5643 controls this warning. GMSGID identifies the message. The
5644 component name is taken from the spelling stack. */
5647 warning_init (location_t loc, int opt, const char *gmsgid)
5652 /* The gmsgid may be a format string with %< and %>. */
5653 warned = warning_at (loc, opt, gmsgid);
5654 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5655 if (*ofwhat && warned)
5656 inform (loc, "(near initialization for %qs)", ofwhat);
5659 /* If TYPE is an array type and EXPR is a parenthesized string
5660 constant, warn if pedantic that EXPR is being used to initialize an
5661 object of type TYPE. */
5664 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
5667 && TREE_CODE (type) == ARRAY_TYPE
5668 && TREE_CODE (expr.value) == STRING_CST
5669 && expr.original_code != STRING_CST)
5670 pedwarn_init (loc, OPT_Wpedantic,
5671 "array initialized from parenthesized string constant");
5674 /* Convert value RHS to type TYPE as preparation for an assignment to
5675 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5676 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5677 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5678 constant before any folding.
5679 The real work of conversion is done by `convert'.
5680 The purpose of this function is to generate error messages
5681 for assignments that are not allowed in C.
5682 ERRTYPE says whether it is argument passing, assignment,
5683 initialization or return.
5685 LOCATION is the location of the assignment, EXPR_LOC is the location of
5686 the RHS or, for a function, location of an argument.
5687 FUNCTION is a tree for the function being called.
5688 PARMNUM is the number of the argument, for printing in error messages. */
5691 convert_for_assignment (location_t location, location_t expr_loc, tree type,
5692 tree rhs, tree origtype, enum impl_conv errtype,
5693 bool null_pointer_constant, tree fundecl,
5694 tree function, int parmnum)
5696 enum tree_code codel = TREE_CODE (type);
5697 tree orig_rhs = rhs;
5699 enum tree_code coder;
5700 tree rname = NULL_TREE;
5701 bool objc_ok = false;
5703 if (errtype == ic_argpass)
5706 /* Change pointer to function to the function itself for
5708 if (TREE_CODE (function) == ADDR_EXPR
5709 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5710 function = TREE_OPERAND (function, 0);
5712 /* Handle an ObjC selector specially for diagnostics. */
5713 selector = objc_message_selector ();
5715 if (selector && parmnum > 2)
5722 /* This macro is used to emit diagnostics to ensure that all format
5723 strings are complete sentences, visible to gettext and checked at
5725 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5730 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5731 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5732 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5733 "expected %qT but argument is of type %qT", \
5737 pedwarn (LOCATION, OPT, AS); \
5740 pedwarn_init (LOCATION, OPT, IN); \
5743 pedwarn (LOCATION, OPT, RE); \
5746 gcc_unreachable (); \
5750 /* This macro is used to emit diagnostics to ensure that all format
5751 strings are complete sentences, visible to gettext and checked at
5752 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5753 extra parameter to enumerate qualifiers. */
5754 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5759 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5760 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5761 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5762 "expected %qT but argument is of type %qT", \
5766 pedwarn (LOCATION, OPT, AS, QUALS); \
5769 pedwarn (LOCATION, OPT, IN, QUALS); \
5772 pedwarn (LOCATION, OPT, RE, QUALS); \
5775 gcc_unreachable (); \
5779 /* This macro is used to emit diagnostics to ensure that all format
5780 strings are complete sentences, visible to gettext and checked at
5781 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5782 warning_at instead of pedwarn. */
5783 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5788 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5789 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5790 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5791 "expected %qT but argument is of type %qT", \
5795 warning_at (LOCATION, OPT, AS, QUALS); \
5798 warning_at (LOCATION, OPT, IN, QUALS); \
5801 warning_at (LOCATION, OPT, RE, QUALS); \
5804 gcc_unreachable (); \
5808 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5809 rhs = TREE_OPERAND (rhs, 0);
5811 rhstype = TREE_TYPE (rhs);
5812 coder = TREE_CODE (rhstype);
5814 if (coder == ERROR_MARK)
5815 return error_mark_node;
5817 if (c_dialect_objc ())
5840 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5843 if (warn_cxx_compat)
5845 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5846 if (checktype != error_mark_node
5847 && TREE_CODE (type) == ENUMERAL_TYPE
5848 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5850 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5851 G_("enum conversion when passing argument "
5852 "%d of %qE is invalid in C++"),
5853 G_("enum conversion in assignment is "
5855 G_("enum conversion in initialization is "
5857 G_("enum conversion in return is "
5862 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5865 if (coder == VOID_TYPE)
5867 /* Except for passing an argument to an unprototyped function,
5868 this is a constraint violation. When passing an argument to
5869 an unprototyped function, it is compile-time undefined;
5870 making it a constraint in that case was rejected in
5872 error_at (location, "void value not ignored as it ought to be");
5873 return error_mark_node;
5875 rhs = require_complete_type (rhs);
5876 if (rhs == error_mark_node)
5877 return error_mark_node;
5878 /* A non-reference type can convert to a reference. This handles
5879 va_start, va_copy and possibly port built-ins. */
5880 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5882 if (!lvalue_p (rhs))
5884 error_at (location, "cannot pass rvalue to reference parameter");
5885 return error_mark_node;
5887 if (!c_mark_addressable (rhs))
5888 return error_mark_node;
5889 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5890 SET_EXPR_LOCATION (rhs, location);
5892 rhs = convert_for_assignment (location, expr_loc,
5893 build_pointer_type (TREE_TYPE (type)),
5894 rhs, origtype, errtype,
5895 null_pointer_constant, fundecl, function,
5897 if (rhs == error_mark_node)
5898 return error_mark_node;
5900 rhs = build1 (NOP_EXPR, type, rhs);
5901 SET_EXPR_LOCATION (rhs, location);
5904 /* Some types can interconvert without explicit casts. */
5905 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5906 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5907 return convert (type, rhs);
5908 /* Arithmetic types all interconvert, and enum is treated like int. */
5909 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5910 || codel == FIXED_POINT_TYPE
5911 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5912 || codel == BOOLEAN_TYPE)
5913 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5914 || coder == FIXED_POINT_TYPE
5915 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5916 || coder == BOOLEAN_TYPE))
5919 bool save = in_late_binary_op;
5920 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5921 || (coder == REAL_TYPE
5922 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5923 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
5924 in_late_binary_op = true;
5925 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5926 ? expr_loc : location, type, orig_rhs);
5927 in_late_binary_op = save;
5931 /* Aggregates in different TUs might need conversion. */
5932 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5934 && comptypes (type, rhstype))
5935 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5936 ? expr_loc : location, type, rhs);
5938 /* Conversion to a transparent union or record from its member types.
5939 This applies only to function arguments. */
5940 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5941 && TYPE_TRANSPARENT_AGGR (type))
5942 && errtype == ic_argpass)
5944 tree memb, marginal_memb = NULL_TREE;
5946 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5948 tree memb_type = TREE_TYPE (memb);
5950 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5951 TYPE_MAIN_VARIANT (rhstype)))
5954 if (TREE_CODE (memb_type) != POINTER_TYPE)
5957 if (coder == POINTER_TYPE)
5959 tree ttl = TREE_TYPE (memb_type);
5960 tree ttr = TREE_TYPE (rhstype);
5962 /* Any non-function converts to a [const][volatile] void *
5963 and vice versa; otherwise, targets must be the same.
5964 Meanwhile, the lhs target must have all the qualifiers of
5966 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5967 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
5968 || comp_target_types (location, memb_type, rhstype))
5970 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5971 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
5972 /* If this type won't generate any warnings, use it. */
5973 if (lquals == rquals
5974 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5975 && TREE_CODE (ttl) == FUNCTION_TYPE)
5976 ? ((lquals | rquals) == rquals)
5977 : ((lquals | rquals) == lquals)))
5980 /* Keep looking for a better type, but remember this one. */
5982 marginal_memb = memb;
5986 /* Can convert integer zero to any pointer type. */
5987 if (null_pointer_constant)
5989 rhs = null_pointer_node;
5994 if (memb || marginal_memb)
5998 /* We have only a marginally acceptable member type;
5999 it needs a warning. */
6000 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6001 tree ttr = TREE_TYPE (rhstype);
6003 /* Const and volatile mean something different for function
6004 types, so the usual warnings are not appropriate. */
6005 if (TREE_CODE (ttr) == FUNCTION_TYPE
6006 && TREE_CODE (ttl) == FUNCTION_TYPE)
6008 /* Because const and volatile on functions are
6009 restrictions that say the function will not do
6010 certain things, it is okay to use a const or volatile
6011 function where an ordinary one is wanted, but not
6013 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6014 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6015 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6016 OPT_Wdiscarded_qualifiers,
6017 G_("passing argument %d of %qE "
6018 "makes %q#v qualified function "
6019 "pointer from unqualified"),
6020 G_("assignment makes %q#v qualified "
6021 "function pointer from "
6023 G_("initialization makes %q#v qualified "
6024 "function pointer from "
6026 G_("return makes %q#v qualified function "
6027 "pointer from unqualified"),
6028 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6030 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6031 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6032 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6033 OPT_Wdiscarded_qualifiers,
6034 G_("passing argument %d of %qE discards "
6035 "%qv qualifier from pointer target type"),
6036 G_("assignment discards %qv qualifier "
6037 "from pointer target type"),
6038 G_("initialization discards %qv qualifier "
6039 "from pointer target type"),
6040 G_("return discards %qv qualifier from "
6041 "pointer target type"),
6042 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6044 memb = marginal_memb;
6047 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6048 pedwarn (location, OPT_Wpedantic,
6049 "ISO C prohibits argument conversion to union type");
6051 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6052 return build_constructor_single (type, memb, rhs);
6056 /* Conversions among pointers */
6057 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6058 && (coder == codel))
6060 tree ttl = TREE_TYPE (type);
6061 tree ttr = TREE_TYPE (rhstype);
6064 bool is_opaque_pointer;
6065 int target_cmp = 0; /* Cache comp_target_types () result. */
6069 if (TREE_CODE (mvl) != ARRAY_TYPE)
6070 mvl = (TYPE_ATOMIC (mvl)
6071 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6073 : TYPE_MAIN_VARIANT (mvl));
6074 if (TREE_CODE (mvr) != ARRAY_TYPE)
6075 mvr = (TYPE_ATOMIC (mvr)
6076 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6078 : TYPE_MAIN_VARIANT (mvr));
6079 /* Opaque pointers are treated like void pointers. */
6080 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6082 /* The Plan 9 compiler permits a pointer to a struct to be
6083 automatically converted into a pointer to an anonymous field
6084 within the struct. */
6085 if (flag_plan9_extensions
6086 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6087 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6090 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6091 if (new_rhs != NULL_TREE)
6094 rhstype = TREE_TYPE (rhs);
6095 coder = TREE_CODE (rhstype);
6096 ttr = TREE_TYPE (rhstype);
6097 mvr = TYPE_MAIN_VARIANT (ttr);
6101 /* C++ does not allow the implicit conversion void* -> T*. However,
6102 for the purpose of reducing the number of false positives, we
6103 tolerate the special case of
6107 where NULL is typically defined in C to be '(void *) 0'. */
6108 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6109 warning_at (errtype == ic_argpass ? expr_loc : location,
6111 "request for implicit conversion "
6112 "from %qT to %qT not permitted in C++", rhstype, type);
6114 /* See if the pointers point to incompatible address spaces. */
6115 asl = TYPE_ADDR_SPACE (ttl);
6116 asr = TYPE_ADDR_SPACE (ttr);
6117 if (!null_pointer_constant_p (rhs)
6118 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6123 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6124 "non-enclosed address space", parmnum, rname);
6127 error_at (location, "assignment from pointer to "
6128 "non-enclosed address space");
6131 error_at (location, "initialization from pointer to "
6132 "non-enclosed address space");
6135 error_at (location, "return from pointer to "
6136 "non-enclosed address space");
6141 return error_mark_node;
6144 /* Check if the right-hand side has a format attribute but the
6145 left-hand side doesn't. */
6146 if (warn_suggest_attribute_format
6147 && check_missing_format_attribute (type, rhstype))
6152 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
6153 "argument %d of %qE might be "
6154 "a candidate for a format attribute",
6158 warning_at (location, OPT_Wsuggest_attribute_format,
6159 "assignment left-hand side might be "
6160 "a candidate for a format attribute");
6163 warning_at (location, OPT_Wsuggest_attribute_format,
6164 "initialization left-hand side might be "
6165 "a candidate for a format attribute");
6168 warning_at (location, OPT_Wsuggest_attribute_format,
6169 "return type might be "
6170 "a candidate for a format attribute");
6177 /* Any non-function converts to a [const][volatile] void *
6178 and vice versa; otherwise, targets must be the same.
6179 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6180 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6181 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6182 || (target_cmp = comp_target_types (location, type, rhstype))
6183 || is_opaque_pointer
6184 || ((c_common_unsigned_type (mvl)
6185 == c_common_unsigned_type (mvr))
6186 && (c_common_signed_type (mvl)
6187 == c_common_signed_type (mvr))
6188 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
6190 /* Warn about loss of qualifers from pointers to arrays with
6191 qualifiers on the element type. */
6192 if (TREE_CODE (ttr) == ARRAY_TYPE)
6194 ttr = strip_array_types (ttr);
6195 ttl = strip_array_types (ttl);
6197 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6198 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6199 WARNING_FOR_QUALIFIERS (location, expr_loc,
6200 OPT_Wdiscarded_array_qualifiers,
6201 G_("passing argument %d of %qE discards "
6202 "%qv qualifier from pointer target type"),
6203 G_("assignment discards %qv qualifier "
6204 "from pointer target type"),
6205 G_("initialization discards %qv qualifier "
6206 "from pointer target type"),
6207 G_("return discards %qv qualifier from "
6208 "pointer target type"),
6209 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6212 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6215 && !null_pointer_constant
6216 && TREE_CODE (ttl) == FUNCTION_TYPE)))
6217 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6218 G_("ISO C forbids passing argument %d of "
6219 "%qE between function pointer "
6221 G_("ISO C forbids assignment between "
6222 "function pointer and %<void *%>"),
6223 G_("ISO C forbids initialization between "
6224 "function pointer and %<void *%>"),
6225 G_("ISO C forbids return between function "
6226 "pointer and %<void *%>"));
6227 /* Const and volatile mean something different for function types,
6228 so the usual warnings are not appropriate. */
6229 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6230 && TREE_CODE (ttl) != FUNCTION_TYPE)
6232 /* Don't warn about loss of qualifier for conversions from
6233 qualified void* to pointers to arrays with corresponding
6234 qualifier on the element type. */
6236 ttl = strip_array_types (ttl);
6238 /* Assignments between atomic and non-atomic objects are OK. */
6239 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6240 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6242 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6243 OPT_Wdiscarded_qualifiers,
6244 G_("passing argument %d of %qE discards "
6245 "%qv qualifier from pointer target type"),
6246 G_("assignment discards %qv qualifier "
6247 "from pointer target type"),
6248 G_("initialization discards %qv qualifier "
6249 "from pointer target type"),
6250 G_("return discards %qv qualifier from "
6251 "pointer target type"),
6252 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6254 /* If this is not a case of ignoring a mismatch in signedness,
6256 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
6259 /* If there is a mismatch, do warn. */
6260 else if (warn_pointer_sign)
6261 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6262 G_("pointer targets in passing argument "
6263 "%d of %qE differ in signedness"),
6264 G_("pointer targets in assignment "
6265 "differ in signedness"),
6266 G_("pointer targets in initialization "
6267 "differ in signedness"),
6268 G_("pointer targets in return differ "
6271 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6272 && TREE_CODE (ttr) == FUNCTION_TYPE)
6274 /* Because const and volatile on functions are restrictions
6275 that say the function will not do certain things,
6276 it is okay to use a const or volatile function
6277 where an ordinary one is wanted, but not vice-versa. */
6278 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6279 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6280 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6281 OPT_Wdiscarded_qualifiers,
6282 G_("passing argument %d of %qE makes "
6283 "%q#v qualified function pointer "
6284 "from unqualified"),
6285 G_("assignment makes %q#v qualified function "
6286 "pointer from unqualified"),
6287 G_("initialization makes %q#v qualified "
6288 "function pointer from unqualified"),
6289 G_("return makes %q#v qualified function "
6290 "pointer from unqualified"),
6291 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6295 /* Avoid warning about the volatile ObjC EH puts on decls. */
6297 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6298 OPT_Wincompatible_pointer_types,
6299 G_("passing argument %d of %qE from "
6300 "incompatible pointer type"),
6301 G_("assignment from incompatible pointer type"),
6302 G_("initialization from incompatible "
6304 G_("return from incompatible pointer type"));
6306 return convert (type, rhs);
6308 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6310 /* ??? This should not be an error when inlining calls to
6311 unprototyped functions. */
6312 error_at (location, "invalid use of non-lvalue array");
6313 return error_mark_node;
6315 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6317 /* An explicit constant 0 can convert to a pointer,
6318 or one that results from arithmetic, even including
6319 a cast to integer type. */
6320 if (!null_pointer_constant)
6321 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6322 OPT_Wint_conversion,
6323 G_("passing argument %d of %qE makes "
6324 "pointer from integer without a cast"),
6325 G_("assignment makes pointer from integer "
6327 G_("initialization makes pointer from "
6328 "integer without a cast"),
6329 G_("return makes pointer from integer "
6332 return convert (type, rhs);
6334 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
6336 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6337 OPT_Wint_conversion,
6338 G_("passing argument %d of %qE makes integer "
6339 "from pointer without a cast"),
6340 G_("assignment makes integer from pointer "
6342 G_("initialization makes integer from pointer "
6344 G_("return makes integer from pointer "
6346 return convert (type, rhs);
6348 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
6351 bool save = in_late_binary_op;
6352 in_late_binary_op = true;
6353 ret = convert (type, rhs);
6354 in_late_binary_op = save;
6361 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6363 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6364 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6365 "expected %qT but argument is of type %qT", type, rhstype);
6368 error_at (location, "incompatible types when assigning to type %qT from "
6369 "type %qT", type, rhstype);
6373 "incompatible types when initializing type %qT using type %qT",
6378 "incompatible types when returning type %qT but %qT was "
6379 "expected", rhstype, type);
6385 return error_mark_node;
6388 /* If VALUE is a compound expr all of whose expressions are constant, then
6389 return its value. Otherwise, return error_mark_node.
6391 This is for handling COMPOUND_EXPRs as initializer elements
6392 which is allowed with a warning when -pedantic is specified. */
6395 valid_compound_expr_initializer (tree value, tree endtype)
6397 if (TREE_CODE (value) == COMPOUND_EXPR)
6399 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6401 return error_mark_node;
6402 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6405 else if (!initializer_constant_valid_p (value, endtype))
6406 return error_mark_node;
6411 /* Perform appropriate conversions on the initial value of a variable,
6412 store it in the declaration DECL,
6413 and print any error messages that are appropriate.
6414 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6415 If the init is invalid, store an ERROR_MARK.
6417 INIT_LOC is the location of the initial value. */
6420 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
6425 /* If variable's type was invalidly declared, just ignore it. */
6427 type = TREE_TYPE (decl);
6428 if (TREE_CODE (type) == ERROR_MARK)
6431 /* Digest the specified initializer into an expression. */
6434 npc = null_pointer_constant_p (init);
6435 value = digest_init (init_loc, type, init, origtype, npc,
6436 true, TREE_STATIC (decl));
6438 /* Store the expression if valid; else report error. */
6440 if (!in_system_header_at (input_location)
6441 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6442 warning (OPT_Wtraditional, "traditional C rejects automatic "
6443 "aggregate initialization");
6445 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6446 DECL_INITIAL (decl) = value;
6448 /* ANSI wants warnings about out-of-range constant initializers. */
6449 STRIP_TYPE_NOPS (value);
6450 if (TREE_STATIC (decl))
6451 constant_expression_warning (value);
6453 /* Check if we need to set array size from compound literal size. */
6454 if (TREE_CODE (type) == ARRAY_TYPE
6455 && TYPE_DOMAIN (type) == 0
6456 && value != error_mark_node)
6458 tree inside_init = init;
6460 STRIP_TYPE_NOPS (inside_init);
6461 inside_init = fold (inside_init);
6463 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6465 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6467 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
6469 /* For int foo[] = (int [3]){1}; we need to set array size
6470 now since later on array initializer will be just the
6471 brace enclosed list of the compound literal. */
6472 tree etype = strip_array_types (TREE_TYPE (decl));
6473 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6474 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
6476 layout_decl (cldecl, 0);
6478 = c_build_qualified_type (type, TYPE_QUALS (etype));
6484 /* Methods for storing and printing names for error messages. */
6486 /* Implement a spelling stack that allows components of a name to be pushed
6487 and popped. Each element on the stack is this structure. */
6494 unsigned HOST_WIDE_INT i;
6499 #define SPELLING_STRING 1
6500 #define SPELLING_MEMBER 2
6501 #define SPELLING_BOUNDS 3
6503 static struct spelling *spelling; /* Next stack element (unused). */
6504 static struct spelling *spelling_base; /* Spelling stack base. */
6505 static int spelling_size; /* Size of the spelling stack. */
6507 /* Macros to save and restore the spelling stack around push_... functions.
6508 Alternative to SAVE_SPELLING_STACK. */
6510 #define SPELLING_DEPTH() (spelling - spelling_base)
6511 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6513 /* Push an element on the spelling stack with type KIND and assign VALUE
6516 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6518 int depth = SPELLING_DEPTH (); \
6520 if (depth >= spelling_size) \
6522 spelling_size += 10; \
6523 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6525 RESTORE_SPELLING_DEPTH (depth); \
6528 spelling->kind = (KIND); \
6529 spelling->MEMBER = (VALUE); \
6533 /* Push STRING on the stack. Printed literally. */
6536 push_string (const char *string)
6538 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6541 /* Push a member name on the stack. Printed as '.' STRING. */
6544 push_member_name (tree decl)
6546 const char *const string
6548 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6549 : _("<anonymous>"));
6550 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6553 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6556 push_array_bounds (unsigned HOST_WIDE_INT bounds)
6558 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6561 /* Compute the maximum size in bytes of the printed spelling. */
6564 spelling_length (void)
6569 for (p = spelling_base; p < spelling; p++)
6571 if (p->kind == SPELLING_BOUNDS)
6574 size += strlen (p->u.s) + 1;
6580 /* Print the spelling to BUFFER and return it. */
6583 print_spelling (char *buffer)
6588 for (p = spelling_base; p < spelling; p++)
6589 if (p->kind == SPELLING_BOUNDS)
6591 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6597 if (p->kind == SPELLING_MEMBER)
6599 for (s = p->u.s; (*d = *s++); d++)
6606 /* Digest the parser output INIT as an initializer for type TYPE.
6607 Return a C expression of type TYPE to represent the initial value.
6609 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6611 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6613 If INIT is a string constant, STRICT_STRING is true if it is
6614 unparenthesized or we should not warn here for it being parenthesized.
6615 For other types of INIT, STRICT_STRING is not used.
6617 INIT_LOC is the location of the INIT.
6619 REQUIRE_CONSTANT requests an error if non-constant initializers or
6620 elements are seen. */
6623 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6624 bool null_pointer_constant, bool strict_string,
6625 int require_constant)
6627 enum tree_code code = TREE_CODE (type);
6628 tree inside_init = init;
6629 tree semantic_type = NULL_TREE;
6630 bool maybe_const = true;
6632 if (type == error_mark_node
6634 || error_operand_p (init))
6635 return error_mark_node;
6637 STRIP_TYPE_NOPS (inside_init);
6639 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6641 semantic_type = TREE_TYPE (inside_init);
6642 inside_init = TREE_OPERAND (inside_init, 0);
6644 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6645 inside_init = decl_constant_value_for_optimization (inside_init);
6647 /* Initialization of an array of chars from a string constant
6648 optionally enclosed in braces. */
6650 if (code == ARRAY_TYPE && inside_init
6651 && TREE_CODE (inside_init) == STRING_CST)
6654 = (TYPE_ATOMIC (TREE_TYPE (type))
6655 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6657 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6658 /* Note that an array could be both an array of character type
6659 and an array of wchar_t if wchar_t is signed char or unsigned
6661 bool char_array = (typ1 == char_type_node
6662 || typ1 == signed_char_type_node
6663 || typ1 == unsigned_char_type_node);
6664 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6665 bool char16_array = !!comptypes (typ1, char16_type_node);
6666 bool char32_array = !!comptypes (typ1, char32_type_node);
6668 if (char_array || wchar_array || char16_array || char32_array)
6671 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6672 expr.value = inside_init;
6673 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6674 expr.original_type = NULL;
6675 maybe_warn_string_init (init_loc, type, expr);
6677 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6678 pedwarn_init (init_loc, OPT_Wpedantic,
6679 "initialization of a flexible array member");
6681 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6682 TYPE_MAIN_VARIANT (type)))
6687 if (typ2 != char_type_node)
6689 error_init (init_loc, "char-array initialized from wide "
6691 return error_mark_node;
6696 if (typ2 == char_type_node)
6698 error_init (init_loc, "wide character array initialized "
6699 "from non-wide string");
6700 return error_mark_node;
6702 else if (!comptypes(typ1, typ2))
6704 error_init (init_loc, "wide character array initialized "
6705 "from incompatible wide string");
6706 return error_mark_node;
6710 TREE_TYPE (inside_init) = type;
6711 if (TYPE_DOMAIN (type) != 0
6712 && TYPE_SIZE (type) != 0
6713 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6715 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6717 /* Subtract the size of a single (possibly wide) character
6718 because it's ok to ignore the terminating null char
6719 that is counted in the length of the constant. */
6720 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6722 - (TYPE_PRECISION (typ1)
6724 pedwarn_init (init_loc, 0,
6725 ("initializer-string for array of chars "
6727 else if (warn_cxx_compat
6728 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6729 warning_at (init_loc, OPT_Wc___compat,
6730 ("initializer-string for array chars "
6731 "is too long for C++"));
6736 else if (INTEGRAL_TYPE_P (typ1))
6738 error_init (init_loc, "array of inappropriate type initialized "
6739 "from string constant");
6740 return error_mark_node;
6744 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6745 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6746 below and handle as a constructor. */
6747 if (code == VECTOR_TYPE
6748 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6749 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6750 && TREE_CONSTANT (inside_init))
6752 if (TREE_CODE (inside_init) == VECTOR_CST
6753 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6754 TYPE_MAIN_VARIANT (type)))
6757 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6759 unsigned HOST_WIDE_INT ix;
6761 bool constant_p = true;
6763 /* Iterate through elements and check if all constructor
6764 elements are *_CSTs. */
6765 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6766 if (!CONSTANT_CLASS_P (value))
6773 return build_vector_from_ctor (type,
6774 CONSTRUCTOR_ELTS (inside_init));
6778 if (warn_sequence_point)
6779 verify_sequence_points (inside_init);
6781 /* Any type can be initialized
6782 from an expression of the same type, optionally with braces. */
6784 if (inside_init && TREE_TYPE (inside_init) != 0
6785 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6786 TYPE_MAIN_VARIANT (type))
6787 || (code == ARRAY_TYPE
6788 && comptypes (TREE_TYPE (inside_init), type))
6789 || (code == VECTOR_TYPE
6790 && comptypes (TREE_TYPE (inside_init), type))
6791 || (code == POINTER_TYPE
6792 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6793 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6794 TREE_TYPE (type)))))
6796 if (code == POINTER_TYPE)
6798 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6800 if (TREE_CODE (inside_init) == STRING_CST
6801 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6802 inside_init = array_to_pointer_conversion
6803 (init_loc, inside_init);
6806 error_init (init_loc, "invalid use of non-lvalue array");
6807 return error_mark_node;
6812 if (code == VECTOR_TYPE)
6813 /* Although the types are compatible, we may require a
6815 inside_init = convert (type, inside_init);
6817 if (require_constant
6818 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6820 /* As an extension, allow initializing objects with static storage
6821 duration with compound literals (which are then treated just as
6822 the brace enclosed list they contain). Also allow this for
6823 vectors, as we can only assign them with compound literals. */
6824 if (flag_isoc99 && code != VECTOR_TYPE)
6825 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6827 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6828 inside_init = DECL_INITIAL (decl);
6831 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6832 && TREE_CODE (inside_init) != CONSTRUCTOR)
6834 error_init (init_loc, "array initialized from non-constant array "
6836 return error_mark_node;
6839 /* Compound expressions can only occur here if -Wpedantic or
6840 -pedantic-errors is specified. In the later case, we always want
6841 an error. In the former case, we simply want a warning. */
6842 if (require_constant && pedantic
6843 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6846 = valid_compound_expr_initializer (inside_init,
6847 TREE_TYPE (inside_init));
6848 if (inside_init == error_mark_node)
6849 error_init (init_loc, "initializer element is not constant");
6851 pedwarn_init (init_loc, OPT_Wpedantic,
6852 "initializer element is not constant");
6853 if (flag_pedantic_errors)
6854 inside_init = error_mark_node;
6856 else if (require_constant
6857 && !initializer_constant_valid_p (inside_init,
6858 TREE_TYPE (inside_init)))
6860 error_init (init_loc, "initializer element is not constant");
6861 inside_init = error_mark_node;
6863 else if (require_constant && !maybe_const)
6864 pedwarn_init (init_loc, OPT_Wpedantic,
6865 "initializer element is not a constant expression");
6867 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6868 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6869 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6870 type, inside_init, origtype,
6871 ic_init, null_pointer_constant,
6872 NULL_TREE, NULL_TREE, 0);
6876 /* Handle scalar types, including conversions. */
6878 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6879 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6880 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6882 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6883 && (TREE_CODE (init) == STRING_CST
6884 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6885 inside_init = init = array_to_pointer_conversion (init_loc, init);
6887 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6890 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6891 inside_init, origtype, ic_init,
6892 null_pointer_constant, NULL_TREE, NULL_TREE,
6895 /* Check to see if we have already given an error message. */
6896 if (inside_init == error_mark_node)
6898 else if (require_constant && !TREE_CONSTANT (inside_init))
6900 error_init (init_loc, "initializer element is not constant");
6901 inside_init = error_mark_node;
6903 else if (require_constant
6904 && !initializer_constant_valid_p (inside_init,
6905 TREE_TYPE (inside_init)))
6907 error_init (init_loc, "initializer element is not computable at "
6909 inside_init = error_mark_node;
6911 else if (require_constant && !maybe_const)
6912 pedwarn_init (init_loc, 0,
6913 "initializer element is not a constant expression");
6918 /* Come here only for records and arrays. */
6920 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6922 error_init (init_loc, "variable-sized object may not be initialized");
6923 return error_mark_node;
6926 error_init (init_loc, "invalid initializer");
6927 return error_mark_node;
6930 /* Handle initializers that use braces. */
6932 /* Type of object we are accumulating a constructor for.
6933 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6934 static tree constructor_type;
6936 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6938 static tree constructor_fields;
6940 /* For an ARRAY_TYPE, this is the specified index
6941 at which to store the next element we get. */
6942 static tree constructor_index;
6944 /* For an ARRAY_TYPE, this is the maximum index. */
6945 static tree constructor_max_index;
6947 /* For a RECORD_TYPE, this is the first field not yet written out. */
6948 static tree constructor_unfilled_fields;
6950 /* For an ARRAY_TYPE, this is the index of the first element
6951 not yet written out. */
6952 static tree constructor_unfilled_index;
6954 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6955 This is so we can generate gaps between fields, when appropriate. */
6956 static tree constructor_bit_index;
6958 /* If we are saving up the elements rather than allocating them,
6959 this is the list of elements so far (in reverse order,
6960 most recent first). */
6961 static vec<constructor_elt, va_gc> *constructor_elements;
6963 /* 1 if constructor should be incrementally stored into a constructor chain,
6964 0 if all the elements should be kept in AVL tree. */
6965 static int constructor_incremental;
6967 /* 1 if so far this constructor's elements are all compile-time constants. */
6968 static int constructor_constant;
6970 /* 1 if so far this constructor's elements are all valid address constants. */
6971 static int constructor_simple;
6973 /* 1 if this constructor has an element that cannot be part of a
6974 constant expression. */
6975 static int constructor_nonconst;
6977 /* 1 if this constructor is erroneous so far. */
6978 static int constructor_erroneous;
6980 /* 1 if this constructor is the universal zero initializer { 0 }. */
6981 static int constructor_zeroinit;
6983 /* Structure for managing pending initializer elements, organized as an
6988 struct init_node *left, *right;
6989 struct init_node *parent;
6996 /* Tree of pending elements at this constructor level.
6997 These are elements encountered out of order
6998 which belong at places we haven't reached yet in actually
7000 Will never hold tree nodes across GC runs. */
7001 static struct init_node *constructor_pending_elts;
7003 /* The SPELLING_DEPTH of this constructor. */
7004 static int constructor_depth;
7006 /* DECL node for which an initializer is being read.
7007 0 means we are reading a constructor expression
7008 such as (struct foo) {...}. */
7009 static tree constructor_decl;
7011 /* Nonzero if this is an initializer for a top-level decl. */
7012 static int constructor_top_level;
7014 /* Nonzero if there were any member designators in this initializer. */
7015 static int constructor_designated;
7017 /* Nesting depth of designator list. */
7018 static int designator_depth;
7020 /* Nonzero if there were diagnosed errors in this designator list. */
7021 static int designator_erroneous;
7024 /* This stack has a level for each implicit or explicit level of
7025 structuring in the initializer, including the outermost one. It
7026 saves the values of most of the variables above. */
7028 struct constructor_range_stack;
7030 struct constructor_stack
7032 struct constructor_stack *next;
7037 tree unfilled_index;
7038 tree unfilled_fields;
7040 vec<constructor_elt, va_gc> *elements;
7041 struct init_node *pending_elts;
7044 /* If value nonzero, this value should replace the entire
7045 constructor at this level. */
7046 struct c_expr replacement_value;
7047 struct constructor_range_stack *range_stack;
7056 int designator_depth;
7059 static struct constructor_stack *constructor_stack;
7061 /* This stack represents designators from some range designator up to
7062 the last designator in the list. */
7064 struct constructor_range_stack
7066 struct constructor_range_stack *next, *prev;
7067 struct constructor_stack *stack;
7074 static struct constructor_range_stack *constructor_range_stack;
7076 /* This stack records separate initializers that are nested.
7077 Nested initializers can't happen in ANSI C, but GNU C allows them
7078 in cases like { ... (struct foo) { ... } ... }. */
7080 struct initializer_stack
7082 struct initializer_stack *next;
7084 struct constructor_stack *constructor_stack;
7085 struct constructor_range_stack *constructor_range_stack;
7086 vec<constructor_elt, va_gc> *elements;
7087 struct spelling *spelling;
7088 struct spelling *spelling_base;
7091 char require_constant_value;
7092 char require_constant_elements;
7095 static struct initializer_stack *initializer_stack;
7097 /* Prepare to parse and output the initializer for variable DECL. */
7100 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
7103 struct initializer_stack *p = XNEW (struct initializer_stack);
7105 p->decl = constructor_decl;
7106 p->require_constant_value = require_constant_value;
7107 p->require_constant_elements = require_constant_elements;
7108 p->constructor_stack = constructor_stack;
7109 p->constructor_range_stack = constructor_range_stack;
7110 p->elements = constructor_elements;
7111 p->spelling = spelling;
7112 p->spelling_base = spelling_base;
7113 p->spelling_size = spelling_size;
7114 p->top_level = constructor_top_level;
7115 p->next = initializer_stack;
7116 initializer_stack = p;
7118 constructor_decl = decl;
7119 constructor_designated = 0;
7120 constructor_top_level = top_level;
7122 if (decl != 0 && decl != error_mark_node)
7124 require_constant_value = TREE_STATIC (decl);
7125 require_constant_elements
7126 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7127 /* For a scalar, you can always use any value to initialize,
7128 even within braces. */
7129 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
7130 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
7134 require_constant_value = 0;
7135 require_constant_elements = 0;
7136 locus = _("(anonymous)");
7139 constructor_stack = 0;
7140 constructor_range_stack = 0;
7142 found_missing_braces = 0;
7146 RESTORE_SPELLING_DEPTH (0);
7149 push_string (locus);
7155 struct initializer_stack *p = initializer_stack;
7157 /* Free the whole constructor stack of this initializer. */
7158 while (constructor_stack)
7160 struct constructor_stack *q = constructor_stack;
7161 constructor_stack = q->next;
7165 gcc_assert (!constructor_range_stack);
7167 /* Pop back to the data of the outer initializer (if any). */
7168 free (spelling_base);
7170 constructor_decl = p->decl;
7171 require_constant_value = p->require_constant_value;
7172 require_constant_elements = p->require_constant_elements;
7173 constructor_stack = p->constructor_stack;
7174 constructor_range_stack = p->constructor_range_stack;
7175 constructor_elements = p->elements;
7176 spelling = p->spelling;
7177 spelling_base = p->spelling_base;
7178 spelling_size = p->spelling_size;
7179 constructor_top_level = p->top_level;
7180 initializer_stack = p->next;
7184 /* Call here when we see the initializer is surrounded by braces.
7185 This is instead of a call to push_init_level;
7186 it is matched by a call to pop_init_level.
7188 TYPE is the type to initialize, for a constructor expression.
7189 For an initializer for a decl, TYPE is zero. */
7192 really_start_incremental_init (tree type)
7194 struct constructor_stack *p = XNEW (struct constructor_stack);
7197 type = TREE_TYPE (constructor_decl);
7199 if (TREE_CODE (type) == VECTOR_TYPE
7200 && TYPE_VECTOR_OPAQUE (type))
7201 error ("opaque vector types cannot be initialized");
7203 p->type = constructor_type;
7204 p->fields = constructor_fields;
7205 p->index = constructor_index;
7206 p->max_index = constructor_max_index;
7207 p->unfilled_index = constructor_unfilled_index;
7208 p->unfilled_fields = constructor_unfilled_fields;
7209 p->bit_index = constructor_bit_index;
7210 p->elements = constructor_elements;
7211 p->constant = constructor_constant;
7212 p->simple = constructor_simple;
7213 p->nonconst = constructor_nonconst;
7214 p->erroneous = constructor_erroneous;
7215 p->pending_elts = constructor_pending_elts;
7216 p->depth = constructor_depth;
7217 p->replacement_value.value = 0;
7218 p->replacement_value.original_code = ERROR_MARK;
7219 p->replacement_value.original_type = NULL;
7223 p->incremental = constructor_incremental;
7224 p->designated = constructor_designated;
7225 p->designator_depth = designator_depth;
7227 constructor_stack = p;
7229 constructor_constant = 1;
7230 constructor_simple = 1;
7231 constructor_nonconst = 0;
7232 constructor_depth = SPELLING_DEPTH ();
7233 constructor_elements = NULL;
7234 constructor_pending_elts = 0;
7235 constructor_type = type;
7236 constructor_incremental = 1;
7237 constructor_designated = 0;
7238 constructor_zeroinit = 1;
7239 designator_depth = 0;
7240 designator_erroneous = 0;
7242 if (TREE_CODE (constructor_type) == RECORD_TYPE
7243 || TREE_CODE (constructor_type) == UNION_TYPE)
7245 constructor_fields = TYPE_FIELDS (constructor_type);
7246 /* Skip any nameless bit fields at the beginning. */
7247 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7248 && DECL_NAME (constructor_fields) == 0)
7249 constructor_fields = DECL_CHAIN (constructor_fields);
7251 constructor_unfilled_fields = constructor_fields;
7252 constructor_bit_index = bitsize_zero_node;
7254 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7256 if (TYPE_DOMAIN (constructor_type))
7258 constructor_max_index
7259 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7261 /* Detect non-empty initializations of zero-length arrays. */
7262 if (constructor_max_index == NULL_TREE
7263 && TYPE_SIZE (constructor_type))
7264 constructor_max_index = integer_minus_one_node;
7266 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7267 to initialize VLAs will cause a proper error; avoid tree
7268 checking errors as well by setting a safe value. */
7269 if (constructor_max_index
7270 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7271 constructor_max_index = integer_minus_one_node;
7274 = convert (bitsizetype,
7275 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7279 constructor_index = bitsize_zero_node;
7280 constructor_max_index = NULL_TREE;
7283 constructor_unfilled_index = constructor_index;
7285 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7287 /* Vectors are like simple fixed-size arrays. */
7288 constructor_max_index =
7289 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7290 constructor_index = bitsize_zero_node;
7291 constructor_unfilled_index = constructor_index;
7295 /* Handle the case of int x = {5}; */
7296 constructor_fields = constructor_type;
7297 constructor_unfilled_fields = constructor_type;
7301 /* Push down into a subobject, for initialization.
7302 If this is for an explicit set of braces, IMPLICIT is 0.
7303 If it is because the next element belongs at a lower level,
7304 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7307 push_init_level (location_t loc, int implicit,
7308 struct obstack *braced_init_obstack)
7310 struct constructor_stack *p;
7311 tree value = NULL_TREE;
7313 /* If we've exhausted any levels that didn't have braces,
7314 pop them now. If implicit == 1, this will have been done in
7315 process_init_element; do not repeat it here because in the case
7316 of excess initializers for an empty aggregate this leads to an
7317 infinite cycle of popping a level and immediately recreating
7321 while (constructor_stack->implicit)
7323 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7324 || TREE_CODE (constructor_type) == UNION_TYPE)
7325 && constructor_fields == 0)
7326 process_init_element (input_location,
7327 pop_init_level (loc, 1, braced_init_obstack),
7328 true, braced_init_obstack);
7329 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7330 && constructor_max_index
7331 && tree_int_cst_lt (constructor_max_index,
7333 process_init_element (input_location,
7334 pop_init_level (loc, 1, braced_init_obstack),
7335 true, braced_init_obstack);
7341 /* Unless this is an explicit brace, we need to preserve previous
7345 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7346 || TREE_CODE (constructor_type) == UNION_TYPE)
7347 && constructor_fields)
7348 value = find_init_member (constructor_fields, braced_init_obstack);
7349 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7350 value = find_init_member (constructor_index, braced_init_obstack);
7353 p = XNEW (struct constructor_stack);
7354 p->type = constructor_type;
7355 p->fields = constructor_fields;
7356 p->index = constructor_index;
7357 p->max_index = constructor_max_index;
7358 p->unfilled_index = constructor_unfilled_index;
7359 p->unfilled_fields = constructor_unfilled_fields;
7360 p->bit_index = constructor_bit_index;
7361 p->elements = constructor_elements;
7362 p->constant = constructor_constant;
7363 p->simple = constructor_simple;
7364 p->nonconst = constructor_nonconst;
7365 p->erroneous = constructor_erroneous;
7366 p->pending_elts = constructor_pending_elts;
7367 p->depth = constructor_depth;
7368 p->replacement_value.value = 0;
7369 p->replacement_value.original_code = ERROR_MARK;
7370 p->replacement_value.original_type = NULL;
7371 p->implicit = implicit;
7373 p->incremental = constructor_incremental;
7374 p->designated = constructor_designated;
7375 p->designator_depth = designator_depth;
7376 p->next = constructor_stack;
7378 constructor_stack = p;
7380 constructor_constant = 1;
7381 constructor_simple = 1;
7382 constructor_nonconst = 0;
7383 constructor_depth = SPELLING_DEPTH ();
7384 constructor_elements = NULL;
7385 constructor_incremental = 1;
7386 constructor_designated = 0;
7387 constructor_pending_elts = 0;
7390 p->range_stack = constructor_range_stack;
7391 constructor_range_stack = 0;
7392 designator_depth = 0;
7393 designator_erroneous = 0;
7396 /* Don't die if an entire brace-pair level is superfluous
7397 in the containing level. */
7398 if (constructor_type == 0)
7400 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7401 || TREE_CODE (constructor_type) == UNION_TYPE)
7403 /* Don't die if there are extra init elts at the end. */
7404 if (constructor_fields == 0)
7405 constructor_type = 0;
7408 constructor_type = TREE_TYPE (constructor_fields);
7409 push_member_name (constructor_fields);
7410 constructor_depth++;
7412 /* If upper initializer is designated, then mark this as
7413 designated too to prevent bogus warnings. */
7414 constructor_designated = p->designated;
7416 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7418 constructor_type = TREE_TYPE (constructor_type);
7419 push_array_bounds (tree_to_uhwi (constructor_index));
7420 constructor_depth++;
7423 if (constructor_type == 0)
7425 error_init (loc, "extra brace group at end of initializer");
7426 constructor_fields = 0;
7427 constructor_unfilled_fields = 0;
7431 if (value && TREE_CODE (value) == CONSTRUCTOR)
7433 constructor_constant = TREE_CONSTANT (value);
7434 constructor_simple = TREE_STATIC (value);
7435 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
7436 constructor_elements = CONSTRUCTOR_ELTS (value);
7437 if (!vec_safe_is_empty (constructor_elements)
7438 && (TREE_CODE (constructor_type) == RECORD_TYPE
7439 || TREE_CODE (constructor_type) == ARRAY_TYPE))
7440 set_nonincremental_init (braced_init_obstack);
7444 found_missing_braces = 1;
7446 if (TREE_CODE (constructor_type) == RECORD_TYPE
7447 || TREE_CODE (constructor_type) == UNION_TYPE)
7449 constructor_fields = TYPE_FIELDS (constructor_type);
7450 /* Skip any nameless bit fields at the beginning. */
7451 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7452 && DECL_NAME (constructor_fields) == 0)
7453 constructor_fields = DECL_CHAIN (constructor_fields);
7455 constructor_unfilled_fields = constructor_fields;
7456 constructor_bit_index = bitsize_zero_node;
7458 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7460 /* Vectors are like simple fixed-size arrays. */
7461 constructor_max_index =
7462 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7463 constructor_index = bitsize_int (0);
7464 constructor_unfilled_index = constructor_index;
7466 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7468 if (TYPE_DOMAIN (constructor_type))
7470 constructor_max_index
7471 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
7473 /* Detect non-empty initializations of zero-length arrays. */
7474 if (constructor_max_index == NULL_TREE
7475 && TYPE_SIZE (constructor_type))
7476 constructor_max_index = integer_minus_one_node;
7478 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7479 to initialize VLAs will cause a proper error; avoid tree
7480 checking errors as well by setting a safe value. */
7481 if (constructor_max_index
7482 && TREE_CODE (constructor_max_index) != INTEGER_CST)
7483 constructor_max_index = integer_minus_one_node;
7486 = convert (bitsizetype,
7487 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7490 constructor_index = bitsize_zero_node;
7492 constructor_unfilled_index = constructor_index;
7493 if (value && TREE_CODE (value) == STRING_CST)
7495 /* We need to split the char/wchar array into individual
7496 characters, so that we don't have to special case it
7498 set_nonincremental_init_from_string (value, braced_init_obstack);
7503 if (constructor_type != error_mark_node)
7504 warning_init (input_location, 0, "braces around scalar initializer");
7505 constructor_fields = constructor_type;
7506 constructor_unfilled_fields = constructor_type;
7510 /* At the end of an implicit or explicit brace level,
7511 finish up that level of constructor. If a single expression
7512 with redundant braces initialized that level, return the
7513 c_expr structure for that expression. Otherwise, the original_code
7514 element is set to ERROR_MARK.
7515 If we were outputting the elements as they are read, return 0 as the value
7516 from inner levels (process_init_element ignores that),
7517 but return error_mark_node as the value from the outermost level
7518 (that's what we want to put in DECL_INITIAL).
7519 Otherwise, return a CONSTRUCTOR expression as the value. */
7522 pop_init_level (location_t loc, int implicit,
7523 struct obstack *braced_init_obstack)
7525 struct constructor_stack *p;
7528 ret.original_code = ERROR_MARK;
7529 ret.original_type = NULL;
7533 /* When we come to an explicit close brace,
7534 pop any inner levels that didn't have explicit braces. */
7535 while (constructor_stack->implicit)
7536 process_init_element (input_location,
7537 pop_init_level (loc, 1, braced_init_obstack),
7538 true, braced_init_obstack);
7539 gcc_assert (!constructor_range_stack);
7542 /* Now output all pending elements. */
7543 constructor_incremental = 1;
7544 output_pending_init_elements (1, braced_init_obstack);
7546 p = constructor_stack;
7548 /* Error for initializing a flexible array member, or a zero-length
7549 array member in an inappropriate context. */
7550 if (constructor_type && constructor_fields
7551 && TREE_CODE (constructor_type) == ARRAY_TYPE
7552 && TYPE_DOMAIN (constructor_type)
7553 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7555 /* Silently discard empty initializations. The parser will
7556 already have pedwarned for empty brackets. */
7557 if (integer_zerop (constructor_unfilled_index))
7558 constructor_type = NULL_TREE;
7561 gcc_assert (!TYPE_SIZE (constructor_type));
7563 if (constructor_depth > 2)
7564 error_init (loc, "initialization of flexible array member in a nested context");
7566 pedwarn_init (loc, OPT_Wpedantic,
7567 "initialization of a flexible array member");
7569 /* We have already issued an error message for the existence
7570 of a flexible array member not at the end of the structure.
7571 Discard the initializer so that we do not die later. */
7572 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7573 constructor_type = NULL_TREE;
7577 switch (vec_safe_length (constructor_elements))
7580 /* Initialization with { } counts as zeroinit. */
7581 constructor_zeroinit = 1;
7584 /* This might be zeroinit as well. */
7585 if (integer_zerop ((*constructor_elements)[0].value))
7586 constructor_zeroinit = 1;
7589 /* If the constructor has more than one element, it can't be { 0 }. */
7590 constructor_zeroinit = 0;
7594 /* Warn when some structs are initialized with direct aggregation. */
7595 if (!implicit && found_missing_braces && warn_missing_braces
7596 && !constructor_zeroinit)
7597 warning_init (loc, OPT_Wmissing_braces,
7598 "missing braces around initializer");
7600 /* Warn when some struct elements are implicitly initialized to zero. */
7601 if (warn_missing_field_initializers
7603 && TREE_CODE (constructor_type) == RECORD_TYPE
7604 && constructor_unfilled_fields)
7606 /* Do not warn for flexible array members or zero-length arrays. */
7607 while (constructor_unfilled_fields
7608 && (!DECL_SIZE (constructor_unfilled_fields)
7609 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7610 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7612 if (constructor_unfilled_fields
7613 /* Do not warn if this level of the initializer uses member
7614 designators; it is likely to be deliberate. */
7615 && !constructor_designated
7616 /* Do not warn about initializing with { 0 } or with { }. */
7617 && !constructor_zeroinit)
7619 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7620 "missing initializer for field %qD of %qT",
7621 constructor_unfilled_fields,
7623 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7624 "%qD declared here", constructor_unfilled_fields);
7628 /* Pad out the end of the structure. */
7629 if (p->replacement_value.value)
7630 /* If this closes a superfluous brace pair,
7631 just pass out the element between them. */
7632 ret = p->replacement_value;
7633 else if (constructor_type == 0)
7635 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7636 && TREE_CODE (constructor_type) != UNION_TYPE
7637 && TREE_CODE (constructor_type) != ARRAY_TYPE
7638 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7640 /* A nonincremental scalar initializer--just return
7641 the element, after verifying there is just one. */
7642 if (vec_safe_is_empty (constructor_elements))
7644 if (!constructor_erroneous)
7645 error_init (loc, "empty scalar initializer");
7646 ret.value = error_mark_node;
7648 else if (vec_safe_length (constructor_elements) != 1)
7650 error_init (loc, "extra elements in scalar initializer");
7651 ret.value = (*constructor_elements)[0].value;
7654 ret.value = (*constructor_elements)[0].value;
7658 if (constructor_erroneous)
7659 ret.value = error_mark_node;
7662 ret.value = build_constructor (constructor_type,
7663 constructor_elements);
7664 if (constructor_constant)
7665 TREE_CONSTANT (ret.value) = 1;
7666 if (constructor_constant && constructor_simple)
7667 TREE_STATIC (ret.value) = 1;
7668 if (constructor_nonconst)
7669 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7673 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7675 if (constructor_nonconst)
7676 ret.original_code = C_MAYBE_CONST_EXPR;
7677 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7678 ret.original_code = ERROR_MARK;
7681 constructor_type = p->type;
7682 constructor_fields = p->fields;
7683 constructor_index = p->index;
7684 constructor_max_index = p->max_index;
7685 constructor_unfilled_index = p->unfilled_index;
7686 constructor_unfilled_fields = p->unfilled_fields;
7687 constructor_bit_index = p->bit_index;
7688 constructor_elements = p->elements;
7689 constructor_constant = p->constant;
7690 constructor_simple = p->simple;
7691 constructor_nonconst = p->nonconst;
7692 constructor_erroneous = p->erroneous;
7693 constructor_incremental = p->incremental;
7694 constructor_designated = p->designated;
7695 designator_depth = p->designator_depth;
7696 constructor_pending_elts = p->pending_elts;
7697 constructor_depth = p->depth;
7699 constructor_range_stack = p->range_stack;
7700 RESTORE_SPELLING_DEPTH (constructor_depth);
7702 constructor_stack = p->next;
7705 if (ret.value == 0 && constructor_stack == 0)
7706 ret.value = error_mark_node;
7710 /* Common handling for both array range and field name designators.
7711 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7714 set_designator (location_t loc, int array,
7715 struct obstack *braced_init_obstack)
7718 enum tree_code subcode;
7720 /* Don't die if an entire brace-pair level is superfluous
7721 in the containing level. */
7722 if (constructor_type == 0)
7725 /* If there were errors in this designator list already, bail out
7727 if (designator_erroneous)
7730 if (!designator_depth)
7732 gcc_assert (!constructor_range_stack);
7734 /* Designator list starts at the level of closest explicit
7736 while (constructor_stack->implicit)
7737 process_init_element (input_location,
7738 pop_init_level (loc, 1, braced_init_obstack),
7739 true, braced_init_obstack);
7740 constructor_designated = 1;
7744 switch (TREE_CODE (constructor_type))
7748 subtype = TREE_TYPE (constructor_fields);
7749 if (subtype != error_mark_node)
7750 subtype = TYPE_MAIN_VARIANT (subtype);
7753 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7759 subcode = TREE_CODE (subtype);
7760 if (array && subcode != ARRAY_TYPE)
7762 error_init (loc, "array index in non-array initializer");
7765 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7767 error_init (loc, "field name not in record or union initializer");
7771 constructor_designated = 1;
7772 push_init_level (loc, 2, braced_init_obstack);
7776 /* If there are range designators in designator list, push a new designator
7777 to constructor_range_stack. RANGE_END is end of such stack range or
7778 NULL_TREE if there is no range designator at this level. */
7781 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7783 struct constructor_range_stack *p;
7785 p = (struct constructor_range_stack *)
7786 obstack_alloc (braced_init_obstack,
7787 sizeof (struct constructor_range_stack));
7788 p->prev = constructor_range_stack;
7790 p->fields = constructor_fields;
7791 p->range_start = constructor_index;
7792 p->index = constructor_index;
7793 p->stack = constructor_stack;
7794 p->range_end = range_end;
7795 if (constructor_range_stack)
7796 constructor_range_stack->next = p;
7797 constructor_range_stack = p;
7800 /* Within an array initializer, specify the next index to be initialized.
7801 FIRST is that index. If LAST is nonzero, then initialize a range
7802 of indices, running from FIRST through LAST. */
7805 set_init_index (location_t loc, tree first, tree last,
7806 struct obstack *braced_init_obstack)
7808 if (set_designator (loc, 1, braced_init_obstack))
7811 designator_erroneous = 1;
7813 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7814 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7816 error_init (loc, "array index in initializer not of integer type");
7820 if (TREE_CODE (first) != INTEGER_CST)
7822 first = c_fully_fold (first, false, NULL);
7823 if (TREE_CODE (first) == INTEGER_CST)
7824 pedwarn_init (loc, OPT_Wpedantic,
7825 "array index in initializer is not "
7826 "an integer constant expression");
7829 if (last && TREE_CODE (last) != INTEGER_CST)
7831 last = c_fully_fold (last, false, NULL);
7832 if (TREE_CODE (last) == INTEGER_CST)
7833 pedwarn_init (loc, OPT_Wpedantic,
7834 "array index in initializer is not "
7835 "an integer constant expression");
7838 if (TREE_CODE (first) != INTEGER_CST)
7839 error_init (loc, "nonconstant array index in initializer");
7840 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7841 error_init (loc, "nonconstant array index in initializer");
7842 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7843 error_init (loc, "array index in non-array initializer");
7844 else if (tree_int_cst_sgn (first) == -1)
7845 error_init (loc, "array index in initializer exceeds array bounds");
7846 else if (constructor_max_index
7847 && tree_int_cst_lt (constructor_max_index, first))
7848 error_init (loc, "array index in initializer exceeds array bounds");
7851 constant_expression_warning (first);
7853 constant_expression_warning (last);
7854 constructor_index = convert (bitsizetype, first);
7855 if (tree_int_cst_lt (constructor_index, first))
7857 constructor_index = copy_node (constructor_index);
7858 TREE_OVERFLOW (constructor_index) = 1;
7863 if (tree_int_cst_equal (first, last))
7865 else if (tree_int_cst_lt (last, first))
7867 error_init (loc, "empty index range in initializer");
7872 last = convert (bitsizetype, last);
7873 if (constructor_max_index != 0
7874 && tree_int_cst_lt (constructor_max_index, last))
7876 error_init (loc, "array index range in initializer exceeds "
7884 designator_erroneous = 0;
7885 if (constructor_range_stack || last)
7886 push_range_stack (last, braced_init_obstack);
7890 /* Within a struct initializer, specify the next field to be initialized. */
7893 set_init_label (location_t loc, tree fieldname,
7894 struct obstack *braced_init_obstack)
7898 if (set_designator (loc, 0, braced_init_obstack))
7901 designator_erroneous = 1;
7903 if (TREE_CODE (constructor_type) != RECORD_TYPE
7904 && TREE_CODE (constructor_type) != UNION_TYPE)
7906 error_init (loc, "field name not in record or union initializer");
7910 field = lookup_field (constructor_type, fieldname);
7913 error_at (loc, "unknown field %qE specified in initializer", fieldname);
7917 constructor_fields = TREE_VALUE (field);
7919 designator_erroneous = 0;
7920 if (constructor_range_stack)
7921 push_range_stack (NULL_TREE, braced_init_obstack);
7922 field = TREE_CHAIN (field);
7925 if (set_designator (loc, 0, braced_init_obstack))
7929 while (field != NULL_TREE);
7932 /* Add a new initializer to the tree of pending initializers. PURPOSE
7933 identifies the initializer, either array index or field in a structure.
7934 VALUE is the value of that index or field. If ORIGTYPE is not
7935 NULL_TREE, it is the original type of VALUE.
7937 IMPLICIT is true if value comes from pop_init_level (1),
7938 the new initializer has been merged with the existing one
7939 and thus no warnings should be emitted about overriding an
7940 existing initializer. */
7943 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7944 bool implicit, struct obstack *braced_init_obstack)
7946 struct init_node *p, **q, *r;
7948 q = &constructor_pending_elts;
7951 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7956 if (tree_int_cst_lt (purpose, p->purpose))
7958 else if (tree_int_cst_lt (p->purpose, purpose))
7964 if (TREE_SIDE_EFFECTS (p->value))
7965 warning_init (loc, OPT_Woverride_init_side_effects,
7966 "initialized field with side-effects "
7968 else if (warn_override_init)
7969 warning_init (loc, OPT_Woverride_init,
7970 "initialized field overwritten");
7973 p->origtype = origtype;
7982 bitpos = bit_position (purpose);
7986 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7988 else if (p->purpose != purpose)
7994 if (TREE_SIDE_EFFECTS (p->value))
7995 warning_init (loc, OPT_Woverride_init_side_effects,
7996 "initialized field with side-effects "
7998 else if (warn_override_init)
7999 warning_init (loc, OPT_Woverride_init,
8000 "initialized field overwritten");
8003 p->origtype = origtype;
8009 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8010 sizeof (struct init_node));
8011 r->purpose = purpose;
8013 r->origtype = origtype;
8023 struct init_node *s;
8027 if (p->balance == 0)
8029 else if (p->balance < 0)
8036 p->left->parent = p;
8053 constructor_pending_elts = r;
8058 struct init_node *t = r->right;
8062 r->right->parent = r;
8067 p->left->parent = p;
8070 p->balance = t->balance < 0;
8071 r->balance = -(t->balance > 0);
8086 constructor_pending_elts = t;
8092 /* p->balance == +1; growth of left side balances the node. */
8097 else /* r == p->right */
8099 if (p->balance == 0)
8100 /* Growth propagation from right side. */
8102 else if (p->balance > 0)
8109 p->right->parent = p;
8126 constructor_pending_elts = r;
8128 else /* r->balance == -1 */
8131 struct init_node *t = r->left;
8135 r->left->parent = r;
8140 p->right->parent = p;
8143 r->balance = (t->balance < 0);
8144 p->balance = -(t->balance > 0);
8159 constructor_pending_elts = t;
8165 /* p->balance == -1; growth of right side balances the node. */
8176 /* Build AVL tree from a sorted chain. */
8179 set_nonincremental_init (struct obstack * braced_init_obstack)
8181 unsigned HOST_WIDE_INT ix;
8184 if (TREE_CODE (constructor_type) != RECORD_TYPE
8185 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8188 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
8189 add_pending_init (input_location, index, value, NULL_TREE, true,
8190 braced_init_obstack);
8191 constructor_elements = NULL;
8192 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8194 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8195 /* Skip any nameless bit fields at the beginning. */
8196 while (constructor_unfilled_fields != 0
8197 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8198 && DECL_NAME (constructor_unfilled_fields) == 0)
8199 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
8202 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8204 if (TYPE_DOMAIN (constructor_type))
8205 constructor_unfilled_index
8206 = convert (bitsizetype,
8207 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8209 constructor_unfilled_index = bitsize_zero_node;
8211 constructor_incremental = 0;
8214 /* Build AVL tree from a string constant. */
8217 set_nonincremental_init_from_string (tree str,
8218 struct obstack * braced_init_obstack)
8220 tree value, purpose, type;
8221 HOST_WIDE_INT val[2];
8222 const char *p, *end;
8223 int byte, wchar_bytes, charwidth, bitpos;
8225 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
8227 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
8228 charwidth = TYPE_PRECISION (char_type_node);
8229 type = TREE_TYPE (constructor_type);
8230 p = TREE_STRING_POINTER (str);
8231 end = p + TREE_STRING_LENGTH (str);
8233 for (purpose = bitsize_zero_node;
8235 && !(constructor_max_index
8236 && tree_int_cst_lt (constructor_max_index, purpose));
8237 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
8239 if (wchar_bytes == 1)
8241 val[0] = (unsigned char) *p++;
8248 for (byte = 0; byte < wchar_bytes; byte++)
8250 if (BYTES_BIG_ENDIAN)
8251 bitpos = (wchar_bytes - byte - 1) * charwidth;
8253 bitpos = byte * charwidth;
8254 val[bitpos % HOST_BITS_PER_WIDE_INT]
8255 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8256 << (bitpos % HOST_BITS_PER_WIDE_INT);
8260 if (!TYPE_UNSIGNED (type))
8262 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8263 if (bitpos < HOST_BITS_PER_WIDE_INT)
8265 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8267 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8271 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8276 else if (val[1] & (((HOST_WIDE_INT) 1)
8277 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8278 val[1] |= ((HOST_WIDE_INT) -1)
8279 << (bitpos - HOST_BITS_PER_WIDE_INT);
8282 value = wide_int_to_tree (type,
8283 wide_int::from_array (val, 2,
8284 HOST_BITS_PER_WIDE_INT * 2));
8285 add_pending_init (input_location, purpose, value, NULL_TREE, true,
8286 braced_init_obstack);
8289 constructor_incremental = 0;
8292 /* Return value of FIELD in pending initializer or zero if the field was
8293 not initialized yet. */
8296 find_init_member (tree field, struct obstack * braced_init_obstack)
8298 struct init_node *p;
8300 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8302 if (constructor_incremental
8303 && tree_int_cst_lt (field, constructor_unfilled_index))
8304 set_nonincremental_init (braced_init_obstack);
8306 p = constructor_pending_elts;
8309 if (tree_int_cst_lt (field, p->purpose))
8311 else if (tree_int_cst_lt (p->purpose, field))
8317 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8319 tree bitpos = bit_position (field);
8321 if (constructor_incremental
8322 && (!constructor_unfilled_fields
8323 || tree_int_cst_lt (bitpos,
8324 bit_position (constructor_unfilled_fields))))
8325 set_nonincremental_init (braced_init_obstack);
8327 p = constructor_pending_elts;
8330 if (field == p->purpose)
8332 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8338 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8340 if (!vec_safe_is_empty (constructor_elements)
8341 && (constructor_elements->last ().index == field))
8342 return constructor_elements->last ().value;
8347 /* "Output" the next constructor element.
8348 At top level, really output it to assembler code now.
8349 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8350 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8351 TYPE is the data type that the containing data type wants here.
8352 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8353 If VALUE is a string constant, STRICT_STRING is true if it is
8354 unparenthesized or we should not warn here for it being parenthesized.
8355 For other types of VALUE, STRICT_STRING is not used.
8357 PENDING if non-nil means output pending elements that belong
8358 right after this element. (PENDING is normally 1;
8359 it is 0 while outputting pending elements, to avoid recursion.)
8361 IMPLICIT is true if value comes from pop_init_level (1),
8362 the new initializer has been merged with the existing one
8363 and thus no warnings should be emitted about overriding an
8364 existing initializer. */
8367 output_init_element (location_t loc, tree value, tree origtype,
8368 bool strict_string, tree type, tree field, int pending,
8369 bool implicit, struct obstack * braced_init_obstack)
8371 tree semantic_type = NULL_TREE;
8372 bool maybe_const = true;
8375 if (type == error_mark_node || value == error_mark_node)
8377 constructor_erroneous = 1;
8380 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8381 && (TREE_CODE (value) == STRING_CST
8382 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8383 && !(TREE_CODE (value) == STRING_CST
8384 && TREE_CODE (type) == ARRAY_TYPE
8385 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8386 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8387 TYPE_MAIN_VARIANT (type)))
8388 value = array_to_pointer_conversion (input_location, value);
8390 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8391 && require_constant_value && pending)
8393 /* As an extension, allow initializing objects with static storage
8394 duration with compound literals (which are then treated just as
8395 the brace enclosed list they contain). */
8397 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8399 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8400 value = DECL_INITIAL (decl);
8403 npc = null_pointer_constant_p (value);
8404 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8406 semantic_type = TREE_TYPE (value);
8407 value = TREE_OPERAND (value, 0);
8409 value = c_fully_fold (value, require_constant_value, &maybe_const);
8411 if (value == error_mark_node)
8412 constructor_erroneous = 1;
8413 else if (!TREE_CONSTANT (value))
8414 constructor_constant = 0;
8415 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
8416 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8417 || TREE_CODE (constructor_type) == UNION_TYPE)
8418 && DECL_C_BIT_FIELD (field)
8419 && TREE_CODE (value) != INTEGER_CST))
8420 constructor_simple = 0;
8422 constructor_nonconst = 1;
8424 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8426 if (require_constant_value)
8428 error_init (loc, "initializer element is not constant");
8429 value = error_mark_node;
8431 else if (require_constant_elements)
8432 pedwarn (loc, OPT_Wpedantic,
8433 "initializer element is not computable at load time");
8435 else if (!maybe_const
8436 && (require_constant_value || require_constant_elements))
8437 pedwarn_init (loc, OPT_Wpedantic,
8438 "initializer element is not a constant expression");
8440 /* Issue -Wc++-compat warnings about initializing a bitfield with
8443 && field != NULL_TREE
8444 && TREE_CODE (field) == FIELD_DECL
8445 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8446 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8447 != TYPE_MAIN_VARIANT (type))
8448 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8450 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8451 if (checktype != error_mark_node
8452 && (TYPE_MAIN_VARIANT (checktype)
8453 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8454 warning_init (loc, OPT_Wc___compat,
8455 "enum conversion in initialization is invalid in C++");
8458 /* If this field is empty (and not at the end of structure),
8459 don't do anything other than checking the initializer. */
8461 && (TREE_TYPE (field) == error_mark_node
8462 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8463 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8464 && (TREE_CODE (constructor_type) == ARRAY_TYPE
8465 || DECL_CHAIN (field)))))
8469 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8470 value = digest_init (loc, type, value, origtype, npc, strict_string,
8471 require_constant_value);
8472 if (value == error_mark_node)
8474 constructor_erroneous = 1;
8477 if (require_constant_value || require_constant_elements)
8478 constant_expression_warning (value);
8480 /* If this element doesn't come next in sequence,
8481 put it on constructor_pending_elts. */
8482 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8483 && (!constructor_incremental
8484 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8486 if (constructor_incremental
8487 && tree_int_cst_lt (field, constructor_unfilled_index))
8488 set_nonincremental_init (braced_init_obstack);
8490 add_pending_init (loc, field, value, origtype, implicit,
8491 braced_init_obstack);
8494 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8495 && (!constructor_incremental
8496 || field != constructor_unfilled_fields))
8498 /* We do this for records but not for unions. In a union,
8499 no matter which field is specified, it can be initialized
8500 right away since it starts at the beginning of the union. */
8501 if (constructor_incremental)
8503 if (!constructor_unfilled_fields)
8504 set_nonincremental_init (braced_init_obstack);
8507 tree bitpos, unfillpos;
8509 bitpos = bit_position (field);
8510 unfillpos = bit_position (constructor_unfilled_fields);
8512 if (tree_int_cst_lt (bitpos, unfillpos))
8513 set_nonincremental_init (braced_init_obstack);
8517 add_pending_init (loc, field, value, origtype, implicit,
8518 braced_init_obstack);
8521 else if (TREE_CODE (constructor_type) == UNION_TYPE
8522 && !vec_safe_is_empty (constructor_elements))
8526 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
8527 warning_init (loc, OPT_Woverride_init_side_effects,
8528 "initialized field with side-effects overwritten");
8529 else if (warn_override_init)
8530 warning_init (loc, OPT_Woverride_init,
8531 "initialized field overwritten");
8534 /* We can have just one union field set. */
8535 constructor_elements = NULL;
8538 /* Otherwise, output this element either to
8539 constructor_elements or to the assembler file. */
8541 constructor_elt celt = {field, value};
8542 vec_safe_push (constructor_elements, celt);
8544 /* Advance the variable that indicates sequential elements output. */
8545 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8546 constructor_unfilled_index
8547 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8549 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8551 constructor_unfilled_fields
8552 = DECL_CHAIN (constructor_unfilled_fields);
8554 /* Skip any nameless bit fields. */
8555 while (constructor_unfilled_fields != 0
8556 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8557 && DECL_NAME (constructor_unfilled_fields) == 0)
8558 constructor_unfilled_fields =
8559 DECL_CHAIN (constructor_unfilled_fields);
8561 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8562 constructor_unfilled_fields = 0;
8564 /* Now output any pending elements which have become next. */
8566 output_pending_init_elements (0, braced_init_obstack);
8569 /* Output any pending elements which have become next.
8570 As we output elements, constructor_unfilled_{fields,index}
8571 advances, which may cause other elements to become next;
8572 if so, they too are output.
8574 If ALL is 0, we return when there are
8575 no more pending elements to output now.
8577 If ALL is 1, we output space as necessary so that
8578 we can output all the pending elements. */
8580 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8582 struct init_node *elt = constructor_pending_elts;
8587 /* Look through the whole pending tree.
8588 If we find an element that should be output now,
8589 output it. Otherwise, set NEXT to the element
8590 that comes first among those still pending. */
8595 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8597 if (tree_int_cst_equal (elt->purpose,
8598 constructor_unfilled_index))
8599 output_init_element (input_location, elt->value, elt->origtype,
8600 true, TREE_TYPE (constructor_type),
8601 constructor_unfilled_index, 0, false,
8602 braced_init_obstack);
8603 else if (tree_int_cst_lt (constructor_unfilled_index,
8606 /* Advance to the next smaller node. */
8611 /* We have reached the smallest node bigger than the
8612 current unfilled index. Fill the space first. */
8613 next = elt->purpose;
8619 /* Advance to the next bigger node. */
8624 /* We have reached the biggest node in a subtree. Find
8625 the parent of it, which is the next bigger node. */
8626 while (elt->parent && elt->parent->right == elt)
8629 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8632 next = elt->purpose;
8638 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8639 || TREE_CODE (constructor_type) == UNION_TYPE)
8641 tree ctor_unfilled_bitpos, elt_bitpos;
8643 /* If the current record is complete we are done. */
8644 if (constructor_unfilled_fields == 0)
8647 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8648 elt_bitpos = bit_position (elt->purpose);
8649 /* We can't compare fields here because there might be empty
8650 fields in between. */
8651 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8653 constructor_unfilled_fields = elt->purpose;
8654 output_init_element (input_location, elt->value, elt->origtype,
8655 true, TREE_TYPE (elt->purpose),
8656 elt->purpose, 0, false,
8657 braced_init_obstack);
8659 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8661 /* Advance to the next smaller node. */
8666 /* We have reached the smallest node bigger than the
8667 current unfilled field. Fill the space first. */
8668 next = elt->purpose;
8674 /* Advance to the next bigger node. */
8679 /* We have reached the biggest node in a subtree. Find
8680 the parent of it, which is the next bigger node. */
8681 while (elt->parent && elt->parent->right == elt)
8685 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8686 bit_position (elt->purpose))))
8688 next = elt->purpose;
8696 /* Ordinarily return, but not if we want to output all
8697 and there are elements left. */
8698 if (!(all && next != 0))
8701 /* If it's not incremental, just skip over the gap, so that after
8702 jumping to retry we will output the next successive element. */
8703 if (TREE_CODE (constructor_type) == RECORD_TYPE
8704 || TREE_CODE (constructor_type) == UNION_TYPE)
8705 constructor_unfilled_fields = next;
8706 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8707 constructor_unfilled_index = next;
8709 /* ELT now points to the node in the pending tree with the next
8710 initializer to output. */
8714 /* Add one non-braced element to the current constructor level.
8715 This adjusts the current position within the constructor's type.
8716 This may also start or terminate implicit levels
8717 to handle a partly-braced initializer.
8719 Once this has found the correct level for the new element,
8720 it calls output_init_element.
8722 IMPLICIT is true if value comes from pop_init_level (1),
8723 the new initializer has been merged with the existing one
8724 and thus no warnings should be emitted about overriding an
8725 existing initializer. */
8728 process_init_element (location_t loc, struct c_expr value, bool implicit,
8729 struct obstack * braced_init_obstack)
8731 tree orig_value = value.value;
8732 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8733 bool strict_string = value.original_code == STRING_CST;
8734 bool was_designated = designator_depth != 0;
8736 designator_depth = 0;
8737 designator_erroneous = 0;
8739 if (!implicit && value.value && !integer_zerop (value.value))
8740 constructor_zeroinit = 0;
8742 /* Handle superfluous braces around string cst as in
8743 char x[] = {"foo"}; */
8747 && TREE_CODE (constructor_type) == ARRAY_TYPE
8748 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8749 && integer_zerop (constructor_unfilled_index))
8751 if (constructor_stack->replacement_value.value)
8752 error_init (loc, "excess elements in char array initializer");
8753 constructor_stack->replacement_value = value;
8757 if (constructor_stack->replacement_value.value != 0)
8759 error_init (loc, "excess elements in struct initializer");
8763 /* Ignore elements of a brace group if it is entirely superfluous
8764 and has already been diagnosed. */
8765 if (constructor_type == 0)
8768 if (!implicit && warn_designated_init && !was_designated
8769 && TREE_CODE (constructor_type) == RECORD_TYPE
8770 && lookup_attribute ("designated_init",
8771 TYPE_ATTRIBUTES (constructor_type)))
8773 OPT_Wdesignated_init,
8774 "positional initialization of field "
8775 "in %<struct%> declared with %<designated_init%> attribute");
8777 /* If we've exhausted any levels that didn't have braces,
8779 while (constructor_stack->implicit)
8781 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8782 || TREE_CODE (constructor_type) == UNION_TYPE)
8783 && constructor_fields == 0)
8784 process_init_element (loc,
8785 pop_init_level (loc, 1, braced_init_obstack),
8786 true, braced_init_obstack);
8787 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8788 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8789 && constructor_max_index
8790 && tree_int_cst_lt (constructor_max_index,
8792 process_init_element (loc,
8793 pop_init_level (loc, 1, braced_init_obstack),
8794 true, braced_init_obstack);
8799 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8800 if (constructor_range_stack)
8802 /* If value is a compound literal and we'll be just using its
8803 content, don't put it into a SAVE_EXPR. */
8804 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8805 || !require_constant_value)
8807 tree semantic_type = NULL_TREE;
8808 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8810 semantic_type = TREE_TYPE (value.value);
8811 value.value = TREE_OPERAND (value.value, 0);
8813 value.value = c_save_expr (value.value);
8815 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8822 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8825 enum tree_code fieldcode;
8827 if (constructor_fields == 0)
8829 pedwarn_init (loc, 0, "excess elements in struct initializer");
8833 fieldtype = TREE_TYPE (constructor_fields);
8834 if (fieldtype != error_mark_node)
8835 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8836 fieldcode = TREE_CODE (fieldtype);
8838 /* Error for non-static initialization of a flexible array member. */
8839 if (fieldcode == ARRAY_TYPE
8840 && !require_constant_value
8841 && TYPE_SIZE (fieldtype) == NULL_TREE
8842 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8844 error_init (loc, "non-static initialization of a flexible "
8849 /* Error for initialization of a flexible array member with
8850 a string constant if the structure is in an array. E.g.:
8851 struct S { int x; char y[]; };
8852 struct S s[] = { { 1, "foo" } };
8855 && fieldcode == ARRAY_TYPE
8856 && constructor_depth > 1
8857 && TYPE_SIZE (fieldtype) == NULL_TREE
8858 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8860 bool in_array_p = false;
8861 for (struct constructor_stack *p = constructor_stack;
8862 p && p->type; p = p->next)
8863 if (TREE_CODE (p->type) == ARRAY_TYPE)
8870 error_init (loc, "initialization of flexible array "
8871 "member in a nested context");
8876 /* Accept a string constant to initialize a subarray. */
8877 if (value.value != 0
8878 && fieldcode == ARRAY_TYPE
8879 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8881 value.value = orig_value;
8882 /* Otherwise, if we have come to a subaggregate,
8883 and we don't have an element of its type, push into it. */
8884 else if (value.value != 0
8885 && value.value != error_mark_node
8886 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8887 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8888 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8890 push_init_level (loc, 1, braced_init_obstack);
8896 push_member_name (constructor_fields);
8897 output_init_element (loc, value.value, value.original_type,
8898 strict_string, fieldtype,
8899 constructor_fields, 1, implicit,
8900 braced_init_obstack);
8901 RESTORE_SPELLING_DEPTH (constructor_depth);
8904 /* Do the bookkeeping for an element that was
8905 directly output as a constructor. */
8907 /* For a record, keep track of end position of last field. */
8908 if (DECL_SIZE (constructor_fields))
8909 constructor_bit_index
8910 = size_binop_loc (input_location, PLUS_EXPR,
8911 bit_position (constructor_fields),
8912 DECL_SIZE (constructor_fields));
8914 /* If the current field was the first one not yet written out,
8915 it isn't now, so update. */
8916 if (constructor_unfilled_fields == constructor_fields)
8918 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8919 /* Skip any nameless bit fields. */
8920 while (constructor_unfilled_fields != 0
8921 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8922 && DECL_NAME (constructor_unfilled_fields) == 0)
8923 constructor_unfilled_fields =
8924 DECL_CHAIN (constructor_unfilled_fields);
8928 constructor_fields = DECL_CHAIN (constructor_fields);
8929 /* Skip any nameless bit fields at the beginning. */
8930 while (constructor_fields != 0
8931 && DECL_C_BIT_FIELD (constructor_fields)
8932 && DECL_NAME (constructor_fields) == 0)
8933 constructor_fields = DECL_CHAIN (constructor_fields);
8935 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8938 enum tree_code fieldcode;
8940 if (constructor_fields == 0)
8942 pedwarn_init (loc, 0,
8943 "excess elements in union initializer");
8947 fieldtype = TREE_TYPE (constructor_fields);
8948 if (fieldtype != error_mark_node)
8949 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8950 fieldcode = TREE_CODE (fieldtype);
8952 /* Warn that traditional C rejects initialization of unions.
8953 We skip the warning if the value is zero. This is done
8954 under the assumption that the zero initializer in user
8955 code appears conditioned on e.g. __STDC__ to avoid
8956 "missing initializer" warnings and relies on default
8957 initialization to zero in the traditional C case.
8958 We also skip the warning if the initializer is designated,
8959 again on the assumption that this must be conditional on
8960 __STDC__ anyway (and we've already complained about the
8961 member-designator already). */
8962 if (!in_system_header_at (input_location) && !constructor_designated
8963 && !(value.value && (integer_zerop (value.value)
8964 || real_zerop (value.value))))
8965 warning (OPT_Wtraditional, "traditional C rejects initialization "
8968 /* Accept a string constant to initialize a subarray. */
8969 if (value.value != 0
8970 && fieldcode == ARRAY_TYPE
8971 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8973 value.value = orig_value;
8974 /* Otherwise, if we have come to a subaggregate,
8975 and we don't have an element of its type, push into it. */
8976 else if (value.value != 0
8977 && value.value != error_mark_node
8978 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8979 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8980 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8982 push_init_level (loc, 1, braced_init_obstack);
8988 push_member_name (constructor_fields);
8989 output_init_element (loc, value.value, value.original_type,
8990 strict_string, fieldtype,
8991 constructor_fields, 1, implicit,
8992 braced_init_obstack);
8993 RESTORE_SPELLING_DEPTH (constructor_depth);
8996 /* Do the bookkeeping for an element that was
8997 directly output as a constructor. */
8999 constructor_bit_index = DECL_SIZE (constructor_fields);
9000 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
9003 constructor_fields = 0;
9005 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9007 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9008 enum tree_code eltcode = TREE_CODE (elttype);
9010 /* Accept a string constant to initialize a subarray. */
9011 if (value.value != 0
9012 && eltcode == ARRAY_TYPE
9013 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
9015 value.value = orig_value;
9016 /* Otherwise, if we have come to a subaggregate,
9017 and we don't have an element of its type, push into it. */
9018 else if (value.value != 0
9019 && value.value != error_mark_node
9020 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
9021 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
9022 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
9024 push_init_level (loc, 1, braced_init_obstack);
9028 if (constructor_max_index != 0
9029 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9030 || integer_all_onesp (constructor_max_index)))
9032 pedwarn_init (loc, 0,
9033 "excess elements in array initializer");
9037 /* Now output the actual element. */
9040 push_array_bounds (tree_to_uhwi (constructor_index));
9041 output_init_element (loc, value.value, value.original_type,
9042 strict_string, elttype,
9043 constructor_index, 1, implicit,
9044 braced_init_obstack);
9045 RESTORE_SPELLING_DEPTH (constructor_depth);
9049 = size_binop_loc (input_location, PLUS_EXPR,
9050 constructor_index, bitsize_one_node);
9053 /* If we are doing the bookkeeping for an element that was
9054 directly output as a constructor, we must update
9055 constructor_unfilled_index. */
9056 constructor_unfilled_index = constructor_index;
9058 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9060 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9062 /* Do a basic check of initializer size. Note that vectors
9063 always have a fixed size derived from their type. */
9064 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9066 pedwarn_init (loc, 0,
9067 "excess elements in vector initializer");
9071 /* Now output the actual element. */
9074 if (TREE_CODE (value.value) == VECTOR_CST)
9075 elttype = TYPE_MAIN_VARIANT (constructor_type);
9076 output_init_element (loc, value.value, value.original_type,
9077 strict_string, elttype,
9078 constructor_index, 1, implicit,
9079 braced_init_obstack);
9083 = size_binop_loc (input_location,
9084 PLUS_EXPR, constructor_index, bitsize_one_node);
9087 /* If we are doing the bookkeeping for an element that was
9088 directly output as a constructor, we must update
9089 constructor_unfilled_index. */
9090 constructor_unfilled_index = constructor_index;
9093 /* Handle the sole element allowed in a braced initializer
9094 for a scalar variable. */
9095 else if (constructor_type != error_mark_node
9096 && constructor_fields == 0)
9098 pedwarn_init (loc, 0,
9099 "excess elements in scalar initializer");
9105 output_init_element (loc, value.value, value.original_type,
9106 strict_string, constructor_type,
9107 NULL_TREE, 1, implicit,
9108 braced_init_obstack);
9109 constructor_fields = 0;
9112 /* Handle range initializers either at this level or anywhere higher
9113 in the designator stack. */
9114 if (constructor_range_stack)
9116 struct constructor_range_stack *p, *range_stack;
9119 range_stack = constructor_range_stack;
9120 constructor_range_stack = 0;
9121 while (constructor_stack != range_stack->stack)
9123 gcc_assert (constructor_stack->implicit);
9124 process_init_element (loc,
9125 pop_init_level (loc, 1,
9126 braced_init_obstack),
9127 true, braced_init_obstack);
9129 for (p = range_stack;
9130 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9133 gcc_assert (constructor_stack->implicit);
9134 process_init_element (loc,
9135 pop_init_level (loc, 1,
9136 braced_init_obstack),
9137 true, braced_init_obstack);
9140 p->index = size_binop_loc (input_location,
9141 PLUS_EXPR, p->index, bitsize_one_node);
9142 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9147 constructor_index = p->index;
9148 constructor_fields = p->fields;
9149 if (finish && p->range_end && p->index == p->range_start)
9157 push_init_level (loc, 2, braced_init_obstack);
9158 p->stack = constructor_stack;
9159 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9160 p->index = p->range_start;
9164 constructor_range_stack = range_stack;
9171 constructor_range_stack = 0;
9174 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9175 (guaranteed to be 'volatile' or null) and ARGS (represented using
9176 an ASM_EXPR node). */
9178 build_asm_stmt (tree cv_qualifier, tree args)
9180 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9181 ASM_VOLATILE_P (args) = 1;
9182 return add_stmt (args);
9185 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9186 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9187 SIMPLE indicates whether there was anything at all after the
9188 string in the asm expression -- asm("blah") and asm("blah" : )
9189 are subtly different. We use a ASM_EXPR node to represent this. */
9191 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
9192 tree clobbers, tree labels, bool simple)
9197 const char *constraint;
9198 const char **oconstraints;
9199 bool allows_mem, allows_reg, is_inout;
9200 int ninputs, noutputs;
9202 ninputs = list_length (inputs);
9203 noutputs = list_length (outputs);
9204 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9206 string = resolve_asm_operand_names (string, outputs, inputs, labels);
9208 /* Remove output conversions that change the type but not the mode. */
9209 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
9211 tree output = TREE_VALUE (tail);
9213 output = c_fully_fold (output, false, NULL);
9215 /* ??? Really, this should not be here. Users should be using a
9216 proper lvalue, dammit. But there's a long history of using casts
9217 in the output operands. In cases like longlong.h, this becomes a
9218 primitive form of typechecking -- if the cast can be removed, then
9219 the output operand had a type of the proper width; otherwise we'll
9220 get an error. Gross, but ... */
9221 STRIP_NOPS (output);
9223 if (!lvalue_or_else (loc, output, lv_asm))
9224 output = error_mark_node;
9226 if (output != error_mark_node
9227 && (TREE_READONLY (output)
9228 || TYPE_READONLY (TREE_TYPE (output))
9229 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9230 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9231 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
9232 readonly_error (loc, output, lv_asm);
9234 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9235 oconstraints[i] = constraint;
9237 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9238 &allows_mem, &allows_reg, &is_inout))
9240 /* If the operand is going to end up in memory,
9241 mark it addressable. */
9242 if (!allows_reg && !c_mark_addressable (output))
9243 output = error_mark_node;
9244 if (!(!allows_reg && allows_mem)
9245 && output != error_mark_node
9246 && VOID_TYPE_P (TREE_TYPE (output)))
9248 error_at (loc, "invalid use of void expression");
9249 output = error_mark_node;
9253 output = error_mark_node;
9255 TREE_VALUE (tail) = output;
9258 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9262 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9263 input = TREE_VALUE (tail);
9265 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9266 oconstraints, &allows_mem, &allows_reg))
9268 /* If the operand is going to end up in memory,
9269 mark it addressable. */
9270 if (!allows_reg && allows_mem)
9272 input = c_fully_fold (input, false, NULL);
9274 /* Strip the nops as we allow this case. FIXME, this really
9275 should be rejected or made deprecated. */
9277 if (!c_mark_addressable (input))
9278 input = error_mark_node;
9283 memset (&expr, 0, sizeof (expr));
9285 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9286 input = c_fully_fold (expr.value, false, NULL);
9288 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9290 error_at (loc, "invalid use of void expression");
9291 input = error_mark_node;
9296 input = error_mark_node;
9298 TREE_VALUE (tail) = input;
9301 /* ASMs with labels cannot have outputs. This should have been
9302 enforced by the parser. */
9303 gcc_assert (outputs == NULL || labels == NULL);
9305 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9307 /* asm statements without outputs, including simple ones, are treated
9309 ASM_INPUT_P (args) = simple;
9310 ASM_VOLATILE_P (args) = (noutputs == 0);
9315 /* Generate a goto statement to LABEL. LOC is the location of the
9319 c_finish_goto_label (location_t loc, tree label)
9321 tree decl = lookup_label_for_goto (loc, label);
9324 TREE_USED (decl) = 1;
9326 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9327 SET_EXPR_LOCATION (t, loc);
9328 return add_stmt (t);
9332 /* Generate a computed goto statement to EXPR. LOC is the location of
9336 c_finish_goto_ptr (location_t loc, tree expr)
9339 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
9340 expr = c_fully_fold (expr, false, NULL);
9341 expr = convert (ptr_type_node, expr);
9342 t = build1 (GOTO_EXPR, void_type_node, expr);
9343 SET_EXPR_LOCATION (t, loc);
9344 return add_stmt (t);
9347 /* Generate a C `return' statement. RETVAL is the expression for what
9348 to return, or a null pointer for `return;' with no value. LOC is
9349 the location of the return statement, or the location of the expression,
9350 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9351 is the original type of RETVAL. */
9354 c_finish_return (location_t loc, tree retval, tree origtype)
9356 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9357 bool no_warning = false;
9361 if (TREE_THIS_VOLATILE (current_function_decl))
9363 "function declared %<noreturn%> has a %<return%> statement");
9365 if (flag_cilkplus && contains_array_notation_expr (retval))
9367 /* Array notations are allowed in a return statement if it is inside a
9368 built-in array notation reduction function. */
9369 if (!find_rank (loc, retval, retval, false, &rank))
9370 return error_mark_node;
9373 error_at (loc, "array notation expression cannot be used as a "
9375 return error_mark_node;
9378 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
9380 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9382 return error_mark_node;
9386 tree semantic_type = NULL_TREE;
9387 npc = null_pointer_constant_p (retval);
9388 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9390 semantic_type = TREE_TYPE (retval);
9391 retval = TREE_OPERAND (retval, 0);
9393 retval = c_fully_fold (retval, false, NULL);
9395 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
9400 current_function_returns_null = 1;
9401 if ((warn_return_type || flag_isoc99)
9402 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
9405 pedwarn (loc, 0, "%<return%> with no value, in "
9406 "function returning non-void");
9408 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9409 "in function returning non-void");
9413 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
9415 current_function_returns_null = 1;
9416 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
9418 "%<return%> with a value, in function returning void");
9420 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
9421 "%<return%> with expression, in function returning void");
9425 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9426 retval, origtype, ic_return,
9427 npc, NULL_TREE, NULL_TREE, 0);
9428 tree res = DECL_RESULT (current_function_decl);
9432 current_function_returns_value = 1;
9433 if (t == error_mark_node)
9436 save = in_late_binary_op;
9437 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9438 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9439 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9440 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9441 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9442 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9443 in_late_binary_op = true;
9444 inner = t = convert (TREE_TYPE (res), t);
9445 in_late_binary_op = save;
9447 /* Strip any conversions, additions, and subtractions, and see if
9448 we are returning the address of a local variable. Warn if so. */
9451 switch (TREE_CODE (inner))
9454 case NON_LVALUE_EXPR:
9456 case POINTER_PLUS_EXPR:
9457 inner = TREE_OPERAND (inner, 0);
9461 /* If the second operand of the MINUS_EXPR has a pointer
9462 type (or is converted from it), this may be valid, so
9463 don't give a warning. */
9465 tree op1 = TREE_OPERAND (inner, 1);
9467 while (!POINTER_TYPE_P (TREE_TYPE (op1))
9468 && (CONVERT_EXPR_P (op1)
9469 || TREE_CODE (op1) == NON_LVALUE_EXPR))
9470 op1 = TREE_OPERAND (op1, 0);
9472 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9475 inner = TREE_OPERAND (inner, 0);
9480 inner = TREE_OPERAND (inner, 0);
9482 while (REFERENCE_CLASS_P (inner)
9483 && TREE_CODE (inner) != INDIRECT_REF)
9484 inner = TREE_OPERAND (inner, 0);
9487 && !DECL_EXTERNAL (inner)
9488 && !TREE_STATIC (inner)
9489 && DECL_CONTEXT (inner) == current_function_decl)
9491 if (TREE_CODE (inner) == LABEL_DECL)
9492 warning_at (loc, OPT_Wreturn_local_addr,
9493 "function returns address of label");
9496 warning_at (loc, OPT_Wreturn_local_addr,
9497 "function returns address of local variable");
9498 tree zero = build_zero_cst (TREE_TYPE (res));
9499 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9511 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
9512 SET_EXPR_LOCATION (retval, loc);
9514 if (warn_sequence_point)
9515 verify_sequence_points (retval);
9518 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
9519 TREE_NO_WARNING (ret_stmt) |= no_warning;
9520 return add_stmt (ret_stmt);
9524 /* The SWITCH_EXPR being built. */
9527 /* The original type of the testing expression, i.e. before the
9528 default conversion is applied. */
9531 /* A splay-tree mapping the low element of a case range to the high
9532 element, or NULL_TREE if there is no high element. Used to
9533 determine whether or not a new case label duplicates an old case
9534 label. We need a tree, rather than simply a hash table, because
9535 of the GNU case range extension. */
9538 /* The bindings at the point of the switch. This is used for
9539 warnings crossing decls when branching to a case label. */
9540 struct c_spot_bindings *bindings;
9542 /* The next node on the stack. */
9543 struct c_switch *next;
9546 /* A stack of the currently active switch statements. The innermost
9547 switch statement is on the top of the stack. There is no need to
9548 mark the stack for garbage collection because it is only active
9549 during the processing of the body of a function, and we never
9550 collect at that point. */
9552 struct c_switch *c_switch_stack;
9554 /* Start a C switch statement, testing expression EXP. Return the new
9555 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9556 SWITCH_COND_LOC is the location of the switch's condition.
9557 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9560 c_start_case (location_t switch_loc,
9561 location_t switch_cond_loc,
9562 tree exp, bool explicit_cast_p)
9564 tree orig_type = error_mark_node;
9565 struct c_switch *cs;
9567 if (exp != error_mark_node)
9569 orig_type = TREE_TYPE (exp);
9571 if (!INTEGRAL_TYPE_P (orig_type))
9573 if (orig_type != error_mark_node)
9575 error_at (switch_cond_loc, "switch quantity not an integer");
9576 orig_type = error_mark_node;
9578 exp = integer_zero_node;
9582 tree type = TYPE_MAIN_VARIANT (orig_type);
9585 /* Warn if the condition has boolean value. */
9586 while (TREE_CODE (e) == COMPOUND_EXPR)
9587 e = TREE_OPERAND (e, 1);
9589 if ((TREE_CODE (type) == BOOLEAN_TYPE
9590 || truth_value_p (TREE_CODE (e)))
9591 /* Explicit cast to int suppresses this warning. */
9592 && !(TREE_CODE (type) == INTEGER_TYPE
9593 && explicit_cast_p))
9594 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9595 "switch condition has boolean value");
9597 if (!in_system_header_at (input_location)
9598 && (type == long_integer_type_node
9599 || type == long_unsigned_type_node))
9600 warning_at (switch_cond_loc,
9601 OPT_Wtraditional, "%<long%> switch expression not "
9602 "converted to %<int%> in ISO C");
9604 exp = c_fully_fold (exp, false, NULL);
9605 exp = default_conversion (exp);
9607 if (warn_sequence_point)
9608 verify_sequence_points (exp);
9612 /* Add this new SWITCH_EXPR to the stack. */
9613 cs = XNEW (struct c_switch);
9614 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
9615 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
9616 cs->orig_type = orig_type;
9617 cs->cases = splay_tree_new (case_compare, NULL, NULL);
9618 cs->bindings = c_get_switch_bindings ();
9619 cs->next = c_switch_stack;
9620 c_switch_stack = cs;
9622 return add_stmt (cs->switch_expr);
9625 /* Process a case label at location LOC. */
9628 do_case (location_t loc, tree low_value, tree high_value)
9630 tree label = NULL_TREE;
9632 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9634 low_value = c_fully_fold (low_value, false, NULL);
9635 if (TREE_CODE (low_value) == INTEGER_CST)
9636 pedwarn (loc, OPT_Wpedantic,
9637 "case label is not an integer constant expression");
9640 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9642 high_value = c_fully_fold (high_value, false, NULL);
9643 if (TREE_CODE (high_value) == INTEGER_CST)
9644 pedwarn (input_location, OPT_Wpedantic,
9645 "case label is not an integer constant expression");
9648 if (c_switch_stack == NULL)
9651 error_at (loc, "case label not within a switch statement");
9653 error_at (loc, "%<default%> label not within a switch statement");
9657 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9658 EXPR_LOCATION (c_switch_stack->switch_expr),
9662 label = c_add_case_label (loc, c_switch_stack->cases,
9663 SWITCH_COND (c_switch_stack->switch_expr),
9664 c_switch_stack->orig_type,
9665 low_value, high_value);
9666 if (label == error_mark_node)
9671 /* Finish the switch statement. TYPE is the original type of the
9672 controlling expression of the switch, or NULL_TREE. */
9675 c_finish_case (tree body, tree type)
9677 struct c_switch *cs = c_switch_stack;
9678 location_t switch_location;
9680 SWITCH_BODY (cs->switch_expr) = body;
9682 /* Emit warnings as needed. */
9683 switch_location = EXPR_LOCATION (cs->switch_expr);
9684 c_do_switch_warnings (cs->cases, switch_location,
9685 type ? type : TREE_TYPE (cs->switch_expr),
9686 SWITCH_COND (cs->switch_expr));
9688 /* Pop the stack. */
9689 c_switch_stack = cs->next;
9690 splay_tree_delete (cs->cases);
9691 c_release_switch_bindings (cs->bindings);
9695 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9696 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9697 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9698 statement, and was not surrounded with parenthesis. */
9701 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9702 tree else_block, bool nested_if)
9706 /* If the condition has array notations, then the rank of the then_block and
9707 else_block must be either 0 or be equal to the rank of the condition. If
9708 the condition does not have array notations then break them up as it is
9709 broken up in a normal expression. */
9710 if (flag_cilkplus && contains_array_notation_expr (cond))
9712 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9713 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9716 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9719 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9721 if (cond_rank != then_rank && then_rank != 0)
9723 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9724 " and the then-block");
9727 else if (cond_rank != else_rank && else_rank != 0)
9729 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9730 " and the else-block");
9734 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9735 if (warn_parentheses && nested_if && else_block == NULL)
9737 tree inner_if = then_block;
9739 /* We know from the grammar productions that there is an IF nested
9740 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9741 it might not be exactly THEN_BLOCK, but should be the last
9742 non-container statement within. */
9744 switch (TREE_CODE (inner_if))
9749 inner_if = BIND_EXPR_BODY (inner_if);
9751 case STATEMENT_LIST:
9752 inner_if = expr_last (then_block);
9754 case TRY_FINALLY_EXPR:
9755 case TRY_CATCH_EXPR:
9756 inner_if = TREE_OPERAND (inner_if, 0);
9763 if (COND_EXPR_ELSE (inner_if))
9764 warning_at (if_locus, OPT_Wparentheses,
9765 "suggest explicit braces to avoid ambiguous %<else%>");
9768 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9769 SET_EXPR_LOCATION (stmt, if_locus);
9773 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9774 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9775 is false for DO loops. INCR is the FOR increment expression. BODY is
9776 the statement controlled by the loop. BLAB is the break label. CLAB is
9777 the continue label. Everything is allowed to be NULL. */
9780 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9781 tree blab, tree clab, bool cond_is_first)
9783 tree entry = NULL, exit = NULL, t;
9785 /* In theory could forbid cilk spawn for loop increment expression,
9786 but it should work just fine. */
9788 /* If the condition is zero don't generate a loop construct. */
9789 if (cond && integer_zerop (cond))
9793 t = build_and_jump (&blab);
9794 SET_EXPR_LOCATION (t, start_locus);
9800 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9802 /* If we have an exit condition, then we build an IF with gotos either
9803 out of the loop, or to the top of it. If there's no exit condition,
9804 then we just build a jump back to the top. */
9805 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9807 if (cond && !integer_nonzerop (cond))
9809 /* Canonicalize the loop condition to the end. This means
9810 generating a branch to the loop condition. Reuse the
9811 continue label, if possible. */
9816 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9817 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9820 t = build1 (GOTO_EXPR, void_type_node, clab);
9821 SET_EXPR_LOCATION (t, start_locus);
9825 t = build_and_jump (&blab);
9827 exit = fold_build3_loc (start_locus,
9828 COND_EXPR, void_type_node, cond, exit, t);
9830 exit = fold_build3_loc (input_location,
9831 COND_EXPR, void_type_node, cond, exit, t);
9840 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9848 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9852 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9855 tree label = *label_p;
9857 /* In switch statements break is sometimes stylistically used after
9858 a return statement. This can lead to spurious warnings about
9859 control reaching the end of a non-void function when it is
9860 inlined. Note that we are calling block_may_fallthru with
9861 language specific tree nodes; this works because
9862 block_may_fallthru returns true when given something it does not
9864 skip = !block_may_fallthru (cur_stmt_list);
9869 *label_p = label = create_artificial_label (loc);
9871 else if (TREE_CODE (label) == LABEL_DECL)
9873 else switch (TREE_INT_CST_LOW (label))
9877 error_at (loc, "break statement not within loop or switch");
9879 error_at (loc, "continue statement not within a loop");
9883 gcc_assert (is_break);
9884 error_at (loc, "break statement used with OpenMP for loop");
9889 error ("break statement within %<#pragma simd%> loop body");
9891 error ("continue statement within %<#pragma simd%> loop body");
9902 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9904 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9907 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9910 emit_side_effect_warnings (location_t loc, tree expr)
9912 if (expr == error_mark_node)
9914 else if (!TREE_SIDE_EFFECTS (expr))
9916 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9917 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9919 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9922 location_t cloc = loc;
9923 while (TREE_CODE (r) == COMPOUND_EXPR)
9925 if (EXPR_HAS_LOCATION (r))
9926 cloc = EXPR_LOCATION (r);
9927 r = TREE_OPERAND (r, 1);
9929 if (!TREE_SIDE_EFFECTS (r)
9930 && !VOID_TYPE_P (TREE_TYPE (r))
9931 && !CONVERT_EXPR_P (r)
9932 && !TREE_NO_WARNING (r)
9933 && !TREE_NO_WARNING (expr))
9934 warning_at (cloc, OPT_Wunused_value,
9935 "right-hand operand of comma expression has no effect");
9938 warn_if_unused_value (expr, loc);
9941 /* Process an expression as if it were a complete statement. Emit
9942 diagnostics, but do not call ADD_STMT. LOC is the location of the
9946 c_process_expr_stmt (location_t loc, tree expr)
9953 expr = c_fully_fold (expr, false, NULL);
9955 if (warn_sequence_point)
9956 verify_sequence_points (expr);
9958 if (TREE_TYPE (expr) != error_mark_node
9959 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9960 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9961 error_at (loc, "expression statement has incomplete type");
9963 /* If we're not processing a statement expression, warn about unused values.
9964 Warnings for statement expressions will be emitted later, once we figure
9965 out which is the result. */
9966 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9967 && warn_unused_value)
9968 emit_side_effect_warnings (loc, expr);
9971 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9972 exprv = TREE_OPERAND (exprv, 1);
9973 while (CONVERT_EXPR_P (exprv))
9974 exprv = TREE_OPERAND (exprv, 0);
9976 || handled_component_p (exprv)
9977 || TREE_CODE (exprv) == ADDR_EXPR)
9978 mark_exp_read (exprv);
9980 /* If the expression is not of a type to which we cannot assign a line
9981 number, wrap the thing in a no-op NOP_EXPR. */
9982 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9984 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9985 SET_EXPR_LOCATION (expr, loc);
9991 /* Emit an expression as a statement. LOC is the location of the
9995 c_finish_expr_stmt (location_t loc, tree expr)
9998 return add_stmt (c_process_expr_stmt (loc, expr));
10003 /* Do the opposite and emit a statement as an expression. To begin,
10004 create a new binding level and return it. */
10007 c_begin_stmt_expr (void)
10011 /* We must force a BLOCK for this level so that, if it is not expanded
10012 later, there is a way to turn off the entire subtree of blocks that
10013 are contained in it. */
10014 keep_next_level ();
10015 ret = c_begin_compound_stmt (true);
10017 c_bindings_start_stmt_expr (c_switch_stack == NULL
10019 : c_switch_stack->bindings);
10021 /* Mark the current statement list as belonging to a statement list. */
10022 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10027 /* LOC is the location of the compound statement to which this body
10031 c_finish_stmt_expr (location_t loc, tree body)
10033 tree last, type, tmp, val;
10036 body = c_end_compound_stmt (loc, body, true);
10038 c_bindings_end_stmt_expr (c_switch_stack == NULL
10040 : c_switch_stack->bindings);
10042 /* Locate the last statement in BODY. See c_end_compound_stmt
10043 about always returning a BIND_EXPR. */
10044 last_p = &BIND_EXPR_BODY (body);
10045 last = BIND_EXPR_BODY (body);
10047 continue_searching:
10048 if (TREE_CODE (last) == STATEMENT_LIST)
10050 tree_stmt_iterator i;
10052 /* This can happen with degenerate cases like ({ }). No value. */
10053 if (!TREE_SIDE_EFFECTS (last))
10056 /* If we're supposed to generate side effects warnings, process
10057 all of the statements except the last. */
10058 if (warn_unused_value)
10060 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
10063 tree t = tsi_stmt (i);
10065 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10066 emit_side_effect_warnings (tloc, t);
10070 i = tsi_last (last);
10071 last_p = tsi_stmt_ptr (i);
10075 /* If the end of the list is exception related, then the list was split
10076 by a call to push_cleanup. Continue searching. */
10077 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10078 || TREE_CODE (last) == TRY_CATCH_EXPR)
10080 last_p = &TREE_OPERAND (last, 0);
10082 goto continue_searching;
10085 if (last == error_mark_node)
10088 /* In the case that the BIND_EXPR is not necessary, return the
10089 expression out from inside it. */
10090 if (last == BIND_EXPR_BODY (body)
10091 && BIND_EXPR_VARS (body) == NULL)
10093 /* Even if this looks constant, do not allow it in a constant
10095 last = c_wrap_maybe_const (last, true);
10096 /* Do not warn if the return value of a statement expression is
10098 TREE_NO_WARNING (last) = 1;
10102 /* Extract the type of said expression. */
10103 type = TREE_TYPE (last);
10105 /* If we're not returning a value at all, then the BIND_EXPR that
10106 we already have is a fine expression to return. */
10107 if (!type || VOID_TYPE_P (type))
10110 /* Now that we've located the expression containing the value, it seems
10111 silly to make voidify_wrapper_expr repeat the process. Create a
10112 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10113 tmp = create_tmp_var_raw (type);
10115 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10116 tree_expr_nonnegative_p giving up immediately. */
10118 if (TREE_CODE (val) == NOP_EXPR
10119 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10120 val = TREE_OPERAND (val, 0);
10122 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
10123 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
10126 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10127 SET_EXPR_LOCATION (t, loc);
10132 /* Begin and end compound statements. This is as simple as pushing
10133 and popping new statement lists from the tree. */
10136 c_begin_compound_stmt (bool do_scope)
10138 tree stmt = push_stmt_list ();
10144 /* End a compound statement. STMT is the statement. LOC is the
10145 location of the compound statement-- this is usually the location
10146 of the opening brace. */
10149 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
10155 if (c_dialect_objc ())
10156 objc_clear_super_receiver ();
10157 block = pop_scope ();
10160 stmt = pop_stmt_list (stmt);
10161 stmt = c_build_bind_expr (loc, block, stmt);
10163 /* If this compound statement is nested immediately inside a statement
10164 expression, then force a BIND_EXPR to be created. Otherwise we'll
10165 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10166 STATEMENT_LISTs merge, and thus we can lose track of what statement
10167 was really last. */
10168 if (building_stmt_list_p ()
10169 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10170 && TREE_CODE (stmt) != BIND_EXPR)
10172 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
10173 TREE_SIDE_EFFECTS (stmt) = 1;
10174 SET_EXPR_LOCATION (stmt, loc);
10180 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10181 when the current scope is exited. EH_ONLY is true when this is not
10182 meant to apply to normal control flow transfer. */
10185 push_cleanup (tree decl, tree cleanup, bool eh_only)
10187 enum tree_code code;
10191 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
10192 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
10194 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10195 list = push_stmt_list ();
10196 TREE_OPERAND (stmt, 0) = list;
10197 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
10200 /* Build a binary-operation expression without default conversions.
10201 CODE is the kind of expression to build.
10202 LOCATION is the operator's location.
10203 This function differs from `build' in several ways:
10204 the data type of the result is computed and recorded in it,
10205 warnings are generated if arg data types are invalid,
10206 special handling for addition and subtraction of pointers is known,
10207 and some optimization is done (operations on narrow ints
10208 are done in the narrower type when that gives the same result).
10209 Constant folding is also done before the result is returned.
10211 Note that the operands will never have enumeral types, or function
10212 or array types, because either they will have the default conversions
10213 performed or they have both just been converted to some other type in which
10214 the arithmetic is to be done. */
10217 build_binary_op (location_t location, enum tree_code code,
10218 tree orig_op0, tree orig_op1, int convert_p)
10220 tree type0, type1, orig_type0, orig_type1;
10222 enum tree_code code0, code1;
10224 tree ret = error_mark_node;
10225 const char *invalid_op_diag;
10226 bool op0_int_operands, op1_int_operands;
10227 bool int_const, int_const_or_overflow, int_operands;
10229 /* Expression code to give to the expression when it is built.
10230 Normally this is CODE, which is what the caller asked for,
10231 but in some special cases we change it. */
10232 enum tree_code resultcode = code;
10234 /* Data type in which the computation is to be performed.
10235 In the simplest cases this is the common type of the arguments. */
10236 tree result_type = NULL;
10238 /* When the computation is in excess precision, the type of the
10239 final EXCESS_PRECISION_EXPR. */
10240 tree semantic_result_type = NULL;
10242 /* Nonzero means operands have already been type-converted
10243 in whatever way is necessary.
10244 Zero means they need to be converted to RESULT_TYPE. */
10247 /* Nonzero means create the expression with this type, rather than
10249 tree build_type = 0;
10251 /* Nonzero means after finally constructing the expression
10252 convert it to this type. */
10253 tree final_type = 0;
10255 /* Nonzero if this is an operation like MIN or MAX which can
10256 safely be computed in short if both args are promoted shorts.
10257 Also implies COMMON.
10258 -1 indicates a bitwise operation; this makes a difference
10259 in the exact conditions for when it is safe to do the operation
10260 in a narrower mode. */
10263 /* Nonzero if this is a comparison operation;
10264 if both args are promoted shorts, compare the original shorts.
10265 Also implies COMMON. */
10266 int short_compare = 0;
10268 /* Nonzero if this is a right-shift operation, which can be computed on the
10269 original short and then promoted if the operand is a promoted short. */
10270 int short_shift = 0;
10272 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10275 /* True means types are compatible as far as ObjC is concerned. */
10278 /* True means this is an arithmetic operation that may need excess
10280 bool may_need_excess_precision;
10282 /* True means this is a boolean operation that converts both its
10283 operands to truth-values. */
10284 bool boolean_op = false;
10286 /* Remember whether we're doing / or %. */
10287 bool doing_div_or_mod = false;
10289 /* Remember whether we're doing << or >>. */
10290 bool doing_shift = false;
10292 /* Tree holding instrumentation expression. */
10293 tree instrument_expr = NULL;
10295 if (location == UNKNOWN_LOCATION)
10296 location = input_location;
10301 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10302 if (op0_int_operands)
10303 op0 = remove_c_maybe_const_expr (op0);
10304 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10305 if (op1_int_operands)
10306 op1 = remove_c_maybe_const_expr (op1);
10307 int_operands = (op0_int_operands && op1_int_operands);
10310 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10311 && TREE_CODE (orig_op1) == INTEGER_CST);
10312 int_const = (int_const_or_overflow
10313 && !TREE_OVERFLOW (orig_op0)
10314 && !TREE_OVERFLOW (orig_op1));
10317 int_const = int_const_or_overflow = false;
10319 /* Do not apply default conversion in mixed vector/scalar expression. */
10321 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
10322 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
10324 op0 = default_conversion (op0);
10325 op1 = default_conversion (op1);
10328 /* When Cilk Plus is enabled and there are array notations inside op0, then
10329 we check to see if there are builtin array notation functions. If
10330 so, then we take on the type of the array notation inside it. */
10331 if (flag_cilkplus && contains_array_notation_expr (op0))
10332 orig_type0 = type0 = find_correct_array_notation_type (op0);
10334 orig_type0 = type0 = TREE_TYPE (op0);
10336 if (flag_cilkplus && contains_array_notation_expr (op1))
10337 orig_type1 = type1 = find_correct_array_notation_type (op1);
10339 orig_type1 = type1 = TREE_TYPE (op1);
10341 /* The expression codes of the data types of the arguments tell us
10342 whether the arguments are integers, floating, pointers, etc. */
10343 code0 = TREE_CODE (type0);
10344 code1 = TREE_CODE (type1);
10346 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10347 STRIP_TYPE_NOPS (op0);
10348 STRIP_TYPE_NOPS (op1);
10350 /* If an error was already reported for one of the arguments,
10351 avoid reporting another error. */
10353 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10354 return error_mark_node;
10356 if ((invalid_op_diag
10357 = targetm.invalid_binary_op (code, type0, type1)))
10359 error_at (location, invalid_op_diag);
10360 return error_mark_node;
10368 case TRUNC_DIV_EXPR:
10369 case CEIL_DIV_EXPR:
10370 case FLOOR_DIV_EXPR:
10371 case ROUND_DIV_EXPR:
10372 case EXACT_DIV_EXPR:
10373 may_need_excess_precision = true;
10376 may_need_excess_precision = false;
10379 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10381 op0 = TREE_OPERAND (op0, 0);
10382 type0 = TREE_TYPE (op0);
10384 else if (may_need_excess_precision
10385 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10388 op0 = convert (eptype, op0);
10390 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10392 op1 = TREE_OPERAND (op1, 0);
10393 type1 = TREE_TYPE (op1);
10395 else if (may_need_excess_precision
10396 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10399 op1 = convert (eptype, op1);
10402 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10404 /* In case when one of the operands of the binary operation is
10405 a vector and another is a scalar -- convert scalar to vector. */
10406 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10408 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10411 switch (convert_flag)
10414 return error_mark_node;
10417 bool maybe_const = true;
10419 sc = c_fully_fold (op0, false, &maybe_const);
10420 sc = save_expr (sc);
10421 sc = convert (TREE_TYPE (type1), sc);
10422 op0 = build_vector_from_val (type1, sc);
10424 op0 = c_wrap_maybe_const (op0, true);
10425 orig_type0 = type0 = TREE_TYPE (op0);
10426 code0 = TREE_CODE (type0);
10430 case stv_secondarg:
10432 bool maybe_const = true;
10434 sc = c_fully_fold (op1, false, &maybe_const);
10435 sc = save_expr (sc);
10436 sc = convert (TREE_TYPE (type0), sc);
10437 op1 = build_vector_from_val (type0, sc);
10439 op1 = c_wrap_maybe_const (op1, true);
10440 orig_type1 = type1 = TREE_TYPE (op1);
10441 code1 = TREE_CODE (type1);
10453 /* Handle the pointer + int case. */
10454 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10456 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
10457 goto return_build_binary_op;
10459 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
10461 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
10462 goto return_build_binary_op;
10469 /* Subtraction of two similar pointers.
10470 We must subtract them as integers, then divide by object size. */
10471 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
10472 && comp_target_types (location, type0, type1))
10474 ret = pointer_diff (location, op0, op1);
10475 goto return_build_binary_op;
10477 /* Handle pointer minus int. Just like pointer plus int. */
10478 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10480 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
10481 goto return_build_binary_op;
10491 case TRUNC_DIV_EXPR:
10492 case CEIL_DIV_EXPR:
10493 case FLOOR_DIV_EXPR:
10494 case ROUND_DIV_EXPR:
10495 case EXACT_DIV_EXPR:
10496 doing_div_or_mod = true;
10497 warn_for_div_by_zero (location, op1);
10499 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10500 || code0 == FIXED_POINT_TYPE
10501 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10502 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10503 || code1 == FIXED_POINT_TYPE
10504 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
10506 enum tree_code tcode0 = code0, tcode1 = code1;
10508 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10509 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
10510 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
10511 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
10513 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10514 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
10515 resultcode = RDIV_EXPR;
10517 /* Although it would be tempting to shorten always here, that
10518 loses on some targets, since the modulo instruction is
10519 undefined if the quotient can't be represented in the
10520 computation mode. We shorten only if unsigned or if
10521 dividing by something we know != -1. */
10522 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10523 || (TREE_CODE (op1) == INTEGER_CST
10524 && !integer_all_onesp (op1)));
10532 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10534 /* Allow vector types which are not floating point types. */
10535 else if (code0 == VECTOR_TYPE
10536 && code1 == VECTOR_TYPE
10537 && !VECTOR_FLOAT_TYPE_P (type0)
10538 && !VECTOR_FLOAT_TYPE_P (type1))
10542 case TRUNC_MOD_EXPR:
10543 case FLOOR_MOD_EXPR:
10544 doing_div_or_mod = true;
10545 warn_for_div_by_zero (location, op1);
10547 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10548 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10549 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10551 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10553 /* Although it would be tempting to shorten always here, that loses
10554 on some targets, since the modulo instruction is undefined if the
10555 quotient can't be represented in the computation mode. We shorten
10556 only if unsigned or if dividing by something we know != -1. */
10557 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
10558 || (TREE_CODE (op1) == INTEGER_CST
10559 && !integer_all_onesp (op1)));
10564 case TRUTH_ANDIF_EXPR:
10565 case TRUTH_ORIF_EXPR:
10566 case TRUTH_AND_EXPR:
10567 case TRUTH_OR_EXPR:
10568 case TRUTH_XOR_EXPR:
10569 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
10570 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10571 || code0 == FIXED_POINT_TYPE)
10572 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
10573 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10574 || code1 == FIXED_POINT_TYPE))
10576 /* Result of these operations is always an int,
10577 but that does not mean the operands should be
10578 converted to ints! */
10579 result_type = integer_type_node;
10580 if (op0_int_operands)
10582 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10583 op0 = remove_c_maybe_const_expr (op0);
10586 op0 = c_objc_common_truthvalue_conversion (location, op0);
10587 if (op1_int_operands)
10589 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10590 op1 = remove_c_maybe_const_expr (op1);
10593 op1 = c_objc_common_truthvalue_conversion (location, op1);
10597 if (code == TRUTH_ANDIF_EXPR)
10599 int_const_or_overflow = (int_operands
10600 && TREE_CODE (orig_op0) == INTEGER_CST
10601 && (op0 == truthvalue_false_node
10602 || TREE_CODE (orig_op1) == INTEGER_CST));
10603 int_const = (int_const_or_overflow
10604 && !TREE_OVERFLOW (orig_op0)
10605 && (op0 == truthvalue_false_node
10606 || !TREE_OVERFLOW (orig_op1)));
10608 else if (code == TRUTH_ORIF_EXPR)
10610 int_const_or_overflow = (int_operands
10611 && TREE_CODE (orig_op0) == INTEGER_CST
10612 && (op0 == truthvalue_true_node
10613 || TREE_CODE (orig_op1) == INTEGER_CST));
10614 int_const = (int_const_or_overflow
10615 && !TREE_OVERFLOW (orig_op0)
10616 && (op0 == truthvalue_true_node
10617 || !TREE_OVERFLOW (orig_op1)));
10621 /* Shift operations: result has same type as first operand;
10622 always convert second operand to int.
10623 Also set SHORT_SHIFT if shifting rightward. */
10626 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10627 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10629 result_type = type0;
10632 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10633 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10634 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10635 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10637 result_type = type0;
10640 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10641 && code1 == INTEGER_TYPE)
10643 doing_shift = true;
10644 if (TREE_CODE (op1) == INTEGER_CST)
10646 if (tree_int_cst_sgn (op1) < 0)
10649 if (c_inhibit_evaluation_warnings == 0)
10650 warning_at (location, OPT_Wshift_count_negative,
10651 "right shift count is negative");
10655 if (!integer_zerop (op1))
10658 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10661 if (c_inhibit_evaluation_warnings == 0)
10662 warning_at (location, OPT_Wshift_count_overflow,
10663 "right shift count >= width of type");
10668 /* Use the type of the value to be shifted. */
10669 result_type = type0;
10670 /* Avoid converting op1 to result_type later. */
10676 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10677 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10679 result_type = type0;
10682 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10683 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10684 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10685 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10687 result_type = type0;
10690 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10691 && code1 == INTEGER_TYPE)
10693 doing_shift = true;
10694 if (TREE_CODE (op0) == INTEGER_CST
10695 && tree_int_cst_sgn (op0) < 0)
10697 /* Don't reject a left shift of a negative value in a context
10698 where a constant expression is needed in C90. */
10701 if (c_inhibit_evaluation_warnings == 0)
10702 warning_at (location, OPT_Wshift_negative_value,
10703 "left shift of negative value");
10705 if (TREE_CODE (op1) == INTEGER_CST)
10707 if (tree_int_cst_sgn (op1) < 0)
10710 if (c_inhibit_evaluation_warnings == 0)
10711 warning_at (location, OPT_Wshift_count_negative,
10712 "left shift count is negative");
10715 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10718 if (c_inhibit_evaluation_warnings == 0)
10719 warning_at (location, OPT_Wshift_count_overflow,
10720 "left shift count >= width of type");
10724 /* Use the type of the value to be shifted. */
10725 result_type = type0;
10726 /* Avoid converting op1 to result_type later. */
10733 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10736 if (!vector_types_compatible_elements_p (type0, type1))
10738 error_at (location, "comparing vectors with different "
10740 return error_mark_node;
10743 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10745 error_at (location, "comparing vectors with different "
10746 "number of elements");
10747 return error_mark_node;
10750 /* Always construct signed integer vector type. */
10751 intt = c_common_type_for_size (GET_MODE_BITSIZE
10752 (TYPE_MODE (TREE_TYPE (type0))), 0);
10753 result_type = build_opaque_vector_type (intt,
10754 TYPE_VECTOR_SUBPARTS (type0));
10758 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10759 warning_at (location,
10761 "comparing floating point with == or != is unsafe");
10762 /* Result of comparison is always int,
10763 but don't convert the args to int! */
10764 build_type = integer_type_node;
10765 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10766 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10767 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10768 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10770 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10772 if (TREE_CODE (op0) == ADDR_EXPR
10773 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10775 if (code == EQ_EXPR)
10776 warning_at (location,
10778 "the comparison will always evaluate as %<false%> "
10779 "for the address of %qD will never be NULL",
10780 TREE_OPERAND (op0, 0));
10782 warning_at (location,
10784 "the comparison will always evaluate as %<true%> "
10785 "for the address of %qD will never be NULL",
10786 TREE_OPERAND (op0, 0));
10788 result_type = type0;
10790 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10792 if (TREE_CODE (op1) == ADDR_EXPR
10793 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10795 if (code == EQ_EXPR)
10796 warning_at (location,
10798 "the comparison will always evaluate as %<false%> "
10799 "for the address of %qD will never be NULL",
10800 TREE_OPERAND (op1, 0));
10802 warning_at (location,
10804 "the comparison will always evaluate as %<true%> "
10805 "for the address of %qD will never be NULL",
10806 TREE_OPERAND (op1, 0));
10808 result_type = type1;
10810 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10812 tree tt0 = TREE_TYPE (type0);
10813 tree tt1 = TREE_TYPE (type1);
10814 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10815 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10816 addr_space_t as_common = ADDR_SPACE_GENERIC;
10818 /* Anything compares with void *. void * compares with anything.
10819 Otherwise, the targets must be compatible
10820 and both must be object or both incomplete. */
10821 if (comp_target_types (location, type0, type1))
10822 result_type = common_pointer_type (type0, type1);
10823 else if (!addr_space_superset (as0, as1, &as_common))
10825 error_at (location, "comparison of pointers to "
10826 "disjoint address spaces");
10827 return error_mark_node;
10829 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
10831 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10832 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10833 "comparison of %<void *%> with function pointer");
10835 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
10837 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10838 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10839 "comparison of %<void *%> with function pointer");
10842 /* Avoid warning about the volatile ObjC EH puts on decls. */
10844 pedwarn (location, 0,
10845 "comparison of distinct pointer types lacks a cast");
10847 if (result_type == NULL_TREE)
10849 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10850 result_type = build_pointer_type
10851 (build_qualified_type (void_type_node, qual));
10854 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10856 result_type = type0;
10857 pedwarn (location, 0, "comparison between pointer and integer");
10859 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10861 result_type = type1;
10862 pedwarn (location, 0, "comparison between pointer and integer");
10864 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10865 || truth_value_p (TREE_CODE (orig_op0)))
10866 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10867 || truth_value_p (TREE_CODE (orig_op1))))
10868 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10875 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10878 if (!vector_types_compatible_elements_p (type0, type1))
10880 error_at (location, "comparing vectors with different "
10882 return error_mark_node;
10885 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10887 error_at (location, "comparing vectors with different "
10888 "number of elements");
10889 return error_mark_node;
10892 /* Always construct signed integer vector type. */
10893 intt = c_common_type_for_size (GET_MODE_BITSIZE
10894 (TYPE_MODE (TREE_TYPE (type0))), 0);
10895 result_type = build_opaque_vector_type (intt,
10896 TYPE_VECTOR_SUBPARTS (type0));
10900 build_type = integer_type_node;
10901 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10902 || code0 == FIXED_POINT_TYPE)
10903 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10904 || code1 == FIXED_POINT_TYPE))
10906 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10908 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10909 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10910 addr_space_t as_common;
10912 if (comp_target_types (location, type0, type1))
10914 result_type = common_pointer_type (type0, type1);
10915 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10916 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10917 pedwarn (location, 0,
10918 "comparison of complete and incomplete pointers");
10919 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10920 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10921 "ordered comparisons of pointers to functions");
10922 else if (null_pointer_constant_p (orig_op0)
10923 || null_pointer_constant_p (orig_op1))
10924 warning_at (location, OPT_Wextra,
10925 "ordered comparison of pointer with null pointer");
10928 else if (!addr_space_superset (as0, as1, &as_common))
10930 error_at (location, "comparison of pointers to "
10931 "disjoint address spaces");
10932 return error_mark_node;
10936 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10937 result_type = build_pointer_type
10938 (build_qualified_type (void_type_node, qual));
10939 pedwarn (location, 0,
10940 "comparison of distinct pointer types lacks a cast");
10943 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10945 result_type = type0;
10947 pedwarn (location, OPT_Wpedantic,
10948 "ordered comparison of pointer with integer zero");
10949 else if (extra_warnings)
10950 warning_at (location, OPT_Wextra,
10951 "ordered comparison of pointer with integer zero");
10953 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10955 result_type = type1;
10957 pedwarn (location, OPT_Wpedantic,
10958 "ordered comparison of pointer with integer zero");
10959 else if (extra_warnings)
10960 warning_at (location, OPT_Wextra,
10961 "ordered comparison of pointer with integer zero");
10963 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10965 result_type = type0;
10966 pedwarn (location, 0, "comparison between pointer and integer");
10968 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10970 result_type = type1;
10971 pedwarn (location, 0, "comparison between pointer and integer");
10973 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10974 || truth_value_p (TREE_CODE (orig_op0)))
10975 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10976 || truth_value_p (TREE_CODE (orig_op1))))
10977 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
10981 gcc_unreachable ();
10984 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10985 return error_mark_node;
10987 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10988 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10989 || !vector_types_compatible_elements_p (type0, type1)))
10991 binary_op_error (location, code, type0, type1);
10992 return error_mark_node;
10995 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10996 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10998 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10999 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
11001 bool first_complex = (code0 == COMPLEX_TYPE);
11002 bool second_complex = (code1 == COMPLEX_TYPE);
11003 int none_complex = (!first_complex && !second_complex);
11005 if (shorten || common || short_compare)
11007 result_type = c_common_type (type0, type1);
11008 do_warn_double_promotion (result_type, type0, type1,
11009 "implicit conversion from %qT to %qT "
11010 "to match other operand of binary "
11013 if (result_type == error_mark_node)
11014 return error_mark_node;
11017 if (first_complex != second_complex
11018 && (code == PLUS_EXPR
11019 || code == MINUS_EXPR
11020 || code == MULT_EXPR
11021 || (code == TRUNC_DIV_EXPR && first_complex))
11022 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11023 && flag_signed_zeros)
11025 /* An operation on mixed real/complex operands must be
11026 handled specially, but the language-independent code can
11027 more easily optimize the plain complex arithmetic if
11028 -fno-signed-zeros. */
11029 tree real_type = TREE_TYPE (result_type);
11031 if (type0 != orig_type0 || type1 != orig_type1)
11033 gcc_assert (may_need_excess_precision && common);
11034 semantic_result_type = c_common_type (orig_type0, orig_type1);
11038 if (TREE_TYPE (op0) != result_type)
11039 op0 = convert_and_check (location, result_type, op0);
11040 if (TREE_TYPE (op1) != real_type)
11041 op1 = convert_and_check (location, real_type, op1);
11045 if (TREE_TYPE (op0) != real_type)
11046 op0 = convert_and_check (location, real_type, op0);
11047 if (TREE_TYPE (op1) != result_type)
11048 op1 = convert_and_check (location, result_type, op1);
11050 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11051 return error_mark_node;
11054 op0 = c_save_expr (op0);
11055 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11057 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11062 case TRUNC_DIV_EXPR:
11063 op1 = c_save_expr (op1);
11064 imag = build2 (resultcode, real_type, imag, op1);
11065 /* Fall through. */
11068 real = build2 (resultcode, real_type, real, op1);
11076 op1 = c_save_expr (op1);
11077 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11079 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11084 op0 = c_save_expr (op0);
11085 imag = build2 (resultcode, real_type, op0, imag);
11086 /* Fall through. */
11088 real = build2 (resultcode, real_type, op0, real);
11091 real = build2 (resultcode, real_type, op0, real);
11092 imag = build1 (NEGATE_EXPR, real_type, imag);
11098 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11099 goto return_build_binary_op;
11102 /* For certain operations (which identify themselves by shorten != 0)
11103 if both args were extended from the same smaller type,
11104 do the arithmetic in that type and then extend.
11106 shorten !=0 and !=1 indicates a bitwise operation.
11107 For them, this optimization is safe only if
11108 both args are zero-extended or both are sign-extended.
11109 Otherwise, we might change the result.
11110 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11111 but calculated in (unsigned short) it would be (unsigned short)-1. */
11113 if (shorten && none_complex)
11115 final_type = result_type;
11116 result_type = shorten_binary_op (result_type, op0, op1,
11120 /* Shifts can be shortened if shifting right. */
11125 tree arg0 = get_narrower (op0, &unsigned_arg);
11127 final_type = result_type;
11129 if (arg0 == op0 && final_type == TREE_TYPE (op0))
11130 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
11132 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
11133 && tree_int_cst_sgn (op1) > 0
11134 /* We can shorten only if the shift count is less than the
11135 number of bits in the smaller type size. */
11136 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11137 /* We cannot drop an unsigned shift after sign-extension. */
11138 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
11140 /* Do an unsigned shift if the operand was zero-extended. */
11142 = c_common_signed_or_unsigned_type (unsigned_arg,
11144 /* Convert value-to-be-shifted to that type. */
11145 if (TREE_TYPE (op0) != result_type)
11146 op0 = convert (result_type, op0);
11151 /* Comparison operations are shortened too but differently.
11152 They identify themselves by setting short_compare = 1. */
11156 /* Don't write &op0, etc., because that would prevent op0
11157 from being kept in a register.
11158 Instead, make copies of the our local variables and
11159 pass the copies by reference, then copy them back afterward. */
11160 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11161 enum tree_code xresultcode = resultcode;
11163 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11169 goto return_build_binary_op;
11172 op0 = xop0, op1 = xop1;
11174 resultcode = xresultcode;
11176 if (c_inhibit_evaluation_warnings == 0)
11178 bool op0_maybe_const = true;
11179 bool op1_maybe_const = true;
11180 tree orig_op0_folded, orig_op1_folded;
11182 if (in_late_binary_op)
11184 orig_op0_folded = orig_op0;
11185 orig_op1_folded = orig_op1;
11189 /* Fold for the sake of possible warnings, as in
11190 build_conditional_expr. This requires the
11191 "original" values to be folded, not just op0 and
11193 c_inhibit_evaluation_warnings++;
11194 op0 = c_fully_fold (op0, require_constant_value,
11196 op1 = c_fully_fold (op1, require_constant_value,
11198 c_inhibit_evaluation_warnings--;
11199 orig_op0_folded = c_fully_fold (orig_op0,
11200 require_constant_value,
11202 orig_op1_folded = c_fully_fold (orig_op1,
11203 require_constant_value,
11207 if (warn_sign_compare)
11208 warn_for_sign_compare (location, orig_op0_folded,
11209 orig_op1_folded, op0, op1,
11210 result_type, resultcode);
11211 if (!in_late_binary_op && !int_operands)
11213 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
11214 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
11215 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
11216 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
11222 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11223 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11224 Then the expression will be built.
11225 It will be given type FINAL_TYPE if that is nonzero;
11226 otherwise, it will be given type RESULT_TYPE. */
11230 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
11231 return error_mark_node;
11234 if (build_type == NULL_TREE)
11236 build_type = result_type;
11237 if ((type0 != orig_type0 || type1 != orig_type1)
11240 gcc_assert (may_need_excess_precision && common);
11241 semantic_result_type = c_common_type (orig_type0, orig_type1);
11247 op0 = ep_convert_and_check (location, result_type, op0,
11248 semantic_result_type);
11249 op1 = ep_convert_and_check (location, result_type, op1,
11250 semantic_result_type);
11252 /* This can happen if one operand has a vector type, and the other
11253 has a different type. */
11254 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11255 return error_mark_node;
11258 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11259 | SANITIZE_FLOAT_DIVIDE))
11260 && do_ubsan_in_current_function ()
11261 && (doing_div_or_mod || doing_shift))
11263 /* OP0 and/or OP1 might have side-effects. */
11264 op0 = c_save_expr (op0);
11265 op1 = c_save_expr (op1);
11266 op0 = c_fully_fold (op0, false, NULL);
11267 op1 = c_fully_fold (op1, false, NULL);
11268 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11269 | SANITIZE_FLOAT_DIVIDE)))
11270 instrument_expr = ubsan_instrument_division (location, op0, op1);
11271 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
11272 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11275 /* Treat expressions in initializers specially as they can't trap. */
11276 if (int_const_or_overflow)
11277 ret = (require_constant_value
11278 ? fold_build2_initializer_loc (location, resultcode, build_type,
11280 : fold_build2_loc (location, resultcode, build_type, op0, op1));
11282 ret = build2 (resultcode, build_type, op0, op1);
11283 if (final_type != 0)
11284 ret = convert (final_type, ret);
11286 return_build_binary_op:
11287 gcc_assert (ret != error_mark_node);
11288 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11289 ret = (int_operands
11290 ? note_integer_operands (ret)
11291 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11292 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11293 && !in_late_binary_op)
11294 ret = note_integer_operands (ret);
11295 if (semantic_result_type)
11296 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
11297 protected_set_expr_location (ret, location);
11299 if (instrument_expr != NULL)
11300 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11301 instrument_expr, ret);
11307 /* Convert EXPR to be a truth-value, validating its type for this
11308 purpose. LOCATION is the source location for the expression. */
11311 c_objc_common_truthvalue_conversion (location_t location, tree expr)
11313 bool int_const, int_operands;
11315 switch (TREE_CODE (TREE_TYPE (expr)))
11318 error_at (location, "used array that cannot be converted to pointer where scalar is required");
11319 return error_mark_node;
11322 error_at (location, "used struct type value where scalar is required");
11323 return error_mark_node;
11326 error_at (location, "used union type value where scalar is required");
11327 return error_mark_node;
11330 error_at (location, "void value not ignored as it ought to be");
11331 return error_mark_node;
11333 case FUNCTION_TYPE:
11334 gcc_unreachable ();
11337 error_at (location, "used vector type where scalar is required");
11338 return error_mark_node;
11344 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11345 int_operands = EXPR_INT_CONST_OPERANDS (expr);
11346 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11348 expr = remove_c_maybe_const_expr (expr);
11349 expr = build2 (NE_EXPR, integer_type_node, expr,
11350 convert (TREE_TYPE (expr), integer_zero_node));
11351 expr = note_integer_operands (expr);
11354 /* ??? Should we also give an error for vectors rather than leaving
11355 those to give errors later? */
11356 expr = c_common_truthvalue_conversion (location, expr);
11358 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11360 if (TREE_OVERFLOW (expr))
11363 return note_integer_operands (expr);
11365 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11366 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11371 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11375 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
11377 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11379 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11380 /* Executing a compound literal inside a function reinitializes
11382 if (!TREE_STATIC (decl))
11390 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11391 statement. LOC is the location of the OACC_PARALLEL. */
11394 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11398 block = c_end_compound_stmt (loc, block, true);
11400 stmt = make_node (OACC_PARALLEL);
11401 TREE_TYPE (stmt) = void_type_node;
11402 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11403 OACC_PARALLEL_BODY (stmt) = block;
11404 SET_EXPR_LOCATION (stmt, loc);
11406 return add_stmt (stmt);
11409 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11410 statement. LOC is the location of the OACC_KERNELS. */
11413 c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11417 block = c_end_compound_stmt (loc, block, true);
11419 stmt = make_node (OACC_KERNELS);
11420 TREE_TYPE (stmt) = void_type_node;
11421 OACC_KERNELS_CLAUSES (stmt) = clauses;
11422 OACC_KERNELS_BODY (stmt) = block;
11423 SET_EXPR_LOCATION (stmt, loc);
11425 return add_stmt (stmt);
11428 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11429 statement. LOC is the location of the OACC_DATA. */
11432 c_finish_oacc_data (location_t loc, tree clauses, tree block)
11436 block = c_end_compound_stmt (loc, block, true);
11438 stmt = make_node (OACC_DATA);
11439 TREE_TYPE (stmt) = void_type_node;
11440 OACC_DATA_CLAUSES (stmt) = clauses;
11441 OACC_DATA_BODY (stmt) = block;
11442 SET_EXPR_LOCATION (stmt, loc);
11444 return add_stmt (stmt);
11447 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11450 c_begin_omp_parallel (void)
11454 keep_next_level ();
11455 block = c_begin_compound_stmt (true);
11460 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11461 statement. LOC is the location of the OMP_PARALLEL. */
11464 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
11468 block = c_end_compound_stmt (loc, block, true);
11470 stmt = make_node (OMP_PARALLEL);
11471 TREE_TYPE (stmt) = void_type_node;
11472 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11473 OMP_PARALLEL_BODY (stmt) = block;
11474 SET_EXPR_LOCATION (stmt, loc);
11476 return add_stmt (stmt);
11479 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11482 c_begin_omp_task (void)
11486 keep_next_level ();
11487 block = c_begin_compound_stmt (true);
11492 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11493 statement. LOC is the location of the #pragma. */
11496 c_finish_omp_task (location_t loc, tree clauses, tree block)
11500 block = c_end_compound_stmt (loc, block, true);
11502 stmt = make_node (OMP_TASK);
11503 TREE_TYPE (stmt) = void_type_node;
11504 OMP_TASK_CLAUSES (stmt) = clauses;
11505 OMP_TASK_BODY (stmt) = block;
11506 SET_EXPR_LOCATION (stmt, loc);
11508 return add_stmt (stmt);
11511 /* Generate GOMP_cancel call for #pragma omp cancel. */
11514 c_finish_omp_cancel (location_t loc, tree clauses)
11516 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11518 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11520 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11522 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11524 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11528 error_at (loc, "%<#pragma omp cancel must specify one of "
11529 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11533 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11534 if (ifc != NULL_TREE)
11536 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11537 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11538 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11539 build_zero_cst (type));
11542 ifc = boolean_true_node;
11543 tree stmt = build_call_expr_loc (loc, fn, 2,
11544 build_int_cst (integer_type_node, mask),
11549 /* Generate GOMP_cancellation_point call for
11550 #pragma omp cancellation point. */
11553 c_finish_omp_cancellation_point (location_t loc, tree clauses)
11555 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11557 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11559 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11561 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11563 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11567 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11568 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11572 tree stmt = build_call_expr_loc (loc, fn, 1,
11573 build_int_cst (integer_type_node, mask));
11577 /* Helper function for handle_omp_array_sections. Called recursively
11578 to handle multiple array-section-subscripts. C is the clause,
11579 T current expression (initially OMP_CLAUSE_DECL), which is either
11580 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11581 expression if specified, TREE_VALUE length expression if specified,
11582 TREE_CHAIN is what it has been specified after, or some decl.
11583 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11584 set to true if any of the array-section-subscript could have length
11585 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11586 first array-section-subscript which is known not to have length
11588 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11589 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11590 all are or may have length of 1, array-section-subscript [:2] is the
11591 first one knonwn not to have length 1. For array-section-subscript
11592 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11593 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11594 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11595 case though, as some lengths could be zero. */
11598 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11599 bool &maybe_zero_len, unsigned int &first_non_one)
11601 tree ret, low_bound, length, type;
11602 if (TREE_CODE (t) != TREE_LIST)
11604 if (error_operand_p (t))
11605 return error_mark_node;
11606 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11609 error_at (OMP_CLAUSE_LOCATION (c),
11610 "%qD is not a variable in %qs clause", t,
11611 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11613 error_at (OMP_CLAUSE_LOCATION (c),
11614 "%qE is not a variable in %qs clause", t,
11615 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11616 return error_mark_node;
11618 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11619 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11621 error_at (OMP_CLAUSE_LOCATION (c),
11622 "%qD is threadprivate variable in %qs clause", t,
11623 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11624 return error_mark_node;
11629 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11630 maybe_zero_len, first_non_one);
11631 if (ret == error_mark_node || ret == NULL_TREE)
11634 type = TREE_TYPE (ret);
11635 low_bound = TREE_PURPOSE (t);
11636 length = TREE_VALUE (t);
11638 if (low_bound == error_mark_node || length == error_mark_node)
11639 return error_mark_node;
11641 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11643 error_at (OMP_CLAUSE_LOCATION (c),
11644 "low bound %qE of array section does not have integral type",
11646 return error_mark_node;
11648 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11650 error_at (OMP_CLAUSE_LOCATION (c),
11651 "length %qE of array section does not have integral type",
11653 return error_mark_node;
11656 && TREE_CODE (low_bound) == INTEGER_CST
11657 && TYPE_PRECISION (TREE_TYPE (low_bound))
11658 > TYPE_PRECISION (sizetype))
11659 low_bound = fold_convert (sizetype, low_bound);
11661 && TREE_CODE (length) == INTEGER_CST
11662 && TYPE_PRECISION (TREE_TYPE (length))
11663 > TYPE_PRECISION (sizetype))
11664 length = fold_convert (sizetype, length);
11665 if (low_bound == NULL_TREE)
11666 low_bound = integer_zero_node;
11668 if (length != NULL_TREE)
11670 if (!integer_nonzerop (length))
11671 maybe_zero_len = true;
11672 if (first_non_one == types.length ()
11673 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11676 if (TREE_CODE (type) == ARRAY_TYPE)
11678 if (length == NULL_TREE
11679 && (TYPE_DOMAIN (type) == NULL_TREE
11680 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11682 error_at (OMP_CLAUSE_LOCATION (c),
11683 "for unknown bound array type length expression must "
11685 return error_mark_node;
11687 if (TREE_CODE (low_bound) == INTEGER_CST
11688 && tree_int_cst_sgn (low_bound) == -1)
11690 error_at (OMP_CLAUSE_LOCATION (c),
11691 "negative low bound in array section in %qs clause",
11692 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11693 return error_mark_node;
11695 if (length != NULL_TREE
11696 && TREE_CODE (length) == INTEGER_CST
11697 && tree_int_cst_sgn (length) == -1)
11699 error_at (OMP_CLAUSE_LOCATION (c),
11700 "negative length in array section in %qs clause",
11701 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11702 return error_mark_node;
11704 if (TYPE_DOMAIN (type)
11705 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11706 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11709 tree size = size_binop (PLUS_EXPR,
11710 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11712 if (TREE_CODE (low_bound) == INTEGER_CST)
11714 if (tree_int_cst_lt (size, low_bound))
11716 error_at (OMP_CLAUSE_LOCATION (c),
11717 "low bound %qE above array section size "
11718 "in %qs clause", low_bound,
11719 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11720 return error_mark_node;
11722 if (tree_int_cst_equal (size, low_bound))
11723 maybe_zero_len = true;
11724 else if (length == NULL_TREE
11725 && first_non_one == types.length ()
11726 && tree_int_cst_equal
11727 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11731 else if (length == NULL_TREE)
11733 maybe_zero_len = true;
11734 if (first_non_one == types.length ())
11737 if (length && TREE_CODE (length) == INTEGER_CST)
11739 if (tree_int_cst_lt (size, length))
11741 error_at (OMP_CLAUSE_LOCATION (c),
11742 "length %qE above array section size "
11743 "in %qs clause", length,
11744 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11745 return error_mark_node;
11747 if (TREE_CODE (low_bound) == INTEGER_CST)
11750 = size_binop (PLUS_EXPR,
11751 fold_convert (sizetype, low_bound),
11752 fold_convert (sizetype, length));
11753 if (TREE_CODE (lbpluslen) == INTEGER_CST
11754 && tree_int_cst_lt (size, lbpluslen))
11756 error_at (OMP_CLAUSE_LOCATION (c),
11757 "high bound %qE above array section size "
11758 "in %qs clause", lbpluslen,
11759 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11760 return error_mark_node;
11765 else if (length == NULL_TREE)
11767 maybe_zero_len = true;
11768 if (first_non_one == types.length ())
11772 /* For [lb:] we will need to evaluate lb more than once. */
11773 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11775 tree lb = c_save_expr (low_bound);
11776 if (lb != low_bound)
11778 TREE_PURPOSE (t) = lb;
11783 else if (TREE_CODE (type) == POINTER_TYPE)
11785 if (length == NULL_TREE)
11787 error_at (OMP_CLAUSE_LOCATION (c),
11788 "for pointer type length expression must be specified");
11789 return error_mark_node;
11791 /* If there is a pointer type anywhere but in the very first
11792 array-section-subscript, the array section can't be contiguous. */
11793 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11794 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11796 error_at (OMP_CLAUSE_LOCATION (c),
11797 "array section is not contiguous in %qs clause",
11798 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11799 return error_mark_node;
11804 error_at (OMP_CLAUSE_LOCATION (c),
11805 "%qE does not have pointer or array type", ret);
11806 return error_mark_node;
11808 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11809 types.safe_push (TREE_TYPE (ret));
11810 /* We will need to evaluate lb more than once. */
11811 tree lb = c_save_expr (low_bound);
11812 if (lb != low_bound)
11814 TREE_PURPOSE (t) = lb;
11817 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11821 /* Handle array sections for clause C. */
11824 handle_omp_array_sections (tree c)
11826 bool maybe_zero_len = false;
11827 unsigned int first_non_one = 0;
11828 vec<tree> types = vNULL;
11829 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11830 maybe_zero_len, first_non_one);
11831 if (first == error_mark_node)
11836 if (first == NULL_TREE)
11841 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11843 tree t = OMP_CLAUSE_DECL (c);
11844 tree tem = NULL_TREE;
11846 /* Need to evaluate side effects in the length expressions
11848 while (TREE_CODE (t) == TREE_LIST)
11850 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11852 if (tem == NULL_TREE)
11853 tem = TREE_VALUE (t);
11855 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11856 TREE_VALUE (t), tem);
11858 t = TREE_CHAIN (t);
11861 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11862 first = c_fully_fold (first, false, NULL);
11863 OMP_CLAUSE_DECL (c) = first;
11867 unsigned int num = types.length (), i;
11868 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11869 tree condition = NULL_TREE;
11871 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11872 maybe_zero_len = true;
11874 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11875 t = TREE_CHAIN (t))
11877 tree low_bound = TREE_PURPOSE (t);
11878 tree length = TREE_VALUE (t);
11882 && TREE_CODE (low_bound) == INTEGER_CST
11883 && TYPE_PRECISION (TREE_TYPE (low_bound))
11884 > TYPE_PRECISION (sizetype))
11885 low_bound = fold_convert (sizetype, low_bound);
11887 && TREE_CODE (length) == INTEGER_CST
11888 && TYPE_PRECISION (TREE_TYPE (length))
11889 > TYPE_PRECISION (sizetype))
11890 length = fold_convert (sizetype, length);
11891 if (low_bound == NULL_TREE)
11892 low_bound = integer_zero_node;
11893 if (!maybe_zero_len && i > first_non_one)
11895 if (integer_nonzerop (low_bound))
11896 goto do_warn_noncontiguous;
11897 if (length != NULL_TREE
11898 && TREE_CODE (length) == INTEGER_CST
11899 && TYPE_DOMAIN (types[i])
11900 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11901 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11905 size = size_binop (PLUS_EXPR,
11906 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11908 if (!tree_int_cst_equal (length, size))
11910 do_warn_noncontiguous:
11911 error_at (OMP_CLAUSE_LOCATION (c),
11912 "array section is not contiguous in %qs "
11914 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11919 if (length != NULL_TREE
11920 && TREE_SIDE_EFFECTS (length))
11922 if (side_effects == NULL_TREE)
11923 side_effects = length;
11925 side_effects = build2 (COMPOUND_EXPR,
11926 TREE_TYPE (side_effects),
11927 length, side_effects);
11934 if (i > first_non_one && length && integer_nonzerop (length))
11937 l = fold_convert (sizetype, length);
11940 l = size_binop (PLUS_EXPR,
11941 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11943 l = size_binop (MINUS_EXPR, l,
11944 fold_convert (sizetype, low_bound));
11946 if (i > first_non_one)
11948 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11950 if (condition == NULL_TREE)
11953 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11956 else if (size == NULL_TREE)
11958 size = size_in_bytes (TREE_TYPE (types[i]));
11959 size = size_binop (MULT_EXPR, size, l);
11961 size = fold_build3 (COND_EXPR, sizetype, condition,
11962 size, size_zero_node);
11965 size = size_binop (MULT_EXPR, size, l);
11970 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11971 first = c_fully_fold (first, false, NULL);
11972 OMP_CLAUSE_DECL (c) = first;
11974 size = c_fully_fold (size, false, NULL);
11975 OMP_CLAUSE_SIZE (c) = size;
11976 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11978 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
11979 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11980 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
11981 if (!c_mark_addressable (t))
11983 OMP_CLAUSE_DECL (c2) = t;
11984 t = build_fold_addr_expr (first);
11985 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11986 tree ptr = OMP_CLAUSE_DECL (c2);
11987 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11988 ptr = build_fold_addr_expr (ptr);
11989 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11990 ptrdiff_type_node, t,
11991 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11992 ptrdiff_type_node, ptr));
11993 t = c_fully_fold (t, false, NULL);
11994 OMP_CLAUSE_SIZE (c2) = t;
11995 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11996 OMP_CLAUSE_CHAIN (c) = c2;
12001 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12002 an inline call. But, remap
12003 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12004 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12007 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12008 tree decl, tree placeholder)
12011 hash_map<tree, tree> decl_map;
12013 decl_map.put (omp_decl1, placeholder);
12014 decl_map.put (omp_decl2, decl);
12015 memset (&id, 0, sizeof (id));
12016 id.src_fn = DECL_CONTEXT (omp_decl1);
12017 id.dst_fn = current_function_decl;
12018 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
12019 id.decl_map = &decl_map;
12021 id.copy_decl = copy_decl_no_change;
12022 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12023 id.transform_new_cfg = true;
12024 id.transform_return_to_modify = false;
12025 id.transform_lang_insert_block = NULL;
12027 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
12031 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12032 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12035 c_find_omp_placeholder_r (tree *tp, int *, void *data)
12037 if (*tp == (tree) data)
12042 /* For all elements of CLAUSES, validate them against their constraints.
12043 Remove any elements from the list that are invalid. */
12046 c_finish_omp_clauses (tree clauses)
12048 bitmap_head generic_head, firstprivate_head, lastprivate_head;
12049 bitmap_head aligned_head;
12051 bool branch_seen = false;
12052 bool copyprivate_seen = false;
12053 tree *nowait_clause = NULL;
12055 bitmap_obstack_initialize (NULL);
12056 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12057 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12058 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
12059 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
12061 for (pc = &clauses, c = clauses; c ; c = *pc)
12063 bool remove = false;
12064 bool need_complete = false;
12065 bool need_implicitly_determined = false;
12067 switch (OMP_CLAUSE_CODE (c))
12069 case OMP_CLAUSE_SHARED:
12070 need_implicitly_determined = true;
12071 goto check_dup_generic;
12073 case OMP_CLAUSE_PRIVATE:
12074 need_complete = true;
12075 need_implicitly_determined = true;
12076 goto check_dup_generic;
12078 case OMP_CLAUSE_REDUCTION:
12079 need_implicitly_determined = true;
12080 t = OMP_CLAUSE_DECL (c);
12081 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
12082 && (FLOAT_TYPE_P (TREE_TYPE (t))
12083 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
12085 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12086 const char *r_name = NULL;
12095 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12099 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12111 case TRUTH_ANDIF_EXPR:
12112 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12115 case TRUTH_ORIF_EXPR:
12116 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12120 gcc_unreachable ();
12124 error_at (OMP_CLAUSE_LOCATION (c),
12125 "%qE has invalid type for %<reduction(%s)%>",
12131 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12133 error_at (OMP_CLAUSE_LOCATION (c),
12134 "user defined reduction not found for %qD", t);
12138 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12140 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12141 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12142 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12143 VAR_DECL, NULL_TREE, type);
12144 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12145 DECL_ARTIFICIAL (placeholder) = 1;
12146 DECL_IGNORED_P (placeholder) = 1;
12147 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12148 c_mark_addressable (placeholder);
12149 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12150 c_mark_addressable (OMP_CLAUSE_DECL (c));
12151 OMP_CLAUSE_REDUCTION_MERGE (c)
12152 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12153 TREE_VEC_ELT (list, 0),
12154 TREE_VEC_ELT (list, 1),
12155 OMP_CLAUSE_DECL (c), placeholder);
12156 OMP_CLAUSE_REDUCTION_MERGE (c)
12157 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12158 void_type_node, NULL_TREE,
12159 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12160 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12161 if (TREE_VEC_LENGTH (list) == 6)
12163 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12164 c_mark_addressable (OMP_CLAUSE_DECL (c));
12165 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12166 c_mark_addressable (placeholder);
12167 tree init = TREE_VEC_ELT (list, 5);
12168 if (init == error_mark_node)
12169 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12170 OMP_CLAUSE_REDUCTION_INIT (c)
12171 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12172 TREE_VEC_ELT (list, 3),
12173 OMP_CLAUSE_DECL (c), placeholder);
12174 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12175 OMP_CLAUSE_REDUCTION_INIT (c)
12176 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12177 OMP_CLAUSE_REDUCTION_INIT (c));
12178 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12179 c_find_omp_placeholder_r,
12180 placeholder, NULL))
12181 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12186 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12187 init = build_constructor (TREE_TYPE (t), NULL);
12189 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12190 OMP_CLAUSE_REDUCTION_INIT (c)
12191 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12193 OMP_CLAUSE_REDUCTION_INIT (c)
12194 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12195 void_type_node, NULL_TREE,
12196 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12197 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12199 goto check_dup_generic;
12201 case OMP_CLAUSE_COPYPRIVATE:
12202 copyprivate_seen = true;
12205 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12206 "%<nowait%> clause must not be used together "
12207 "with %<copyprivate%>");
12208 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12209 nowait_clause = NULL;
12211 goto check_dup_generic;
12213 case OMP_CLAUSE_COPYIN:
12214 t = OMP_CLAUSE_DECL (c);
12215 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12217 error_at (OMP_CLAUSE_LOCATION (c),
12218 "%qE must be %<threadprivate%> for %<copyin%>", t);
12222 goto check_dup_generic;
12224 case OMP_CLAUSE_LINEAR:
12225 t = OMP_CLAUSE_DECL (c);
12226 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12227 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12229 error_at (OMP_CLAUSE_LOCATION (c),
12230 "linear clause applied to non-integral non-pointer "
12231 "variable with type %qT", TREE_TYPE (t));
12235 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12237 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12238 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12239 OMP_CLAUSE_DECL (c), s);
12240 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12241 sizetype, s, OMP_CLAUSE_DECL (c));
12242 if (s == error_mark_node)
12244 OMP_CLAUSE_LINEAR_STEP (c) = s;
12247 OMP_CLAUSE_LINEAR_STEP (c)
12248 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
12249 goto check_dup_generic;
12252 t = OMP_CLAUSE_DECL (c);
12253 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12255 error_at (OMP_CLAUSE_LOCATION (c),
12256 "%qE is not a variable in clause %qs", t,
12257 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12260 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12261 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12262 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12264 error_at (OMP_CLAUSE_LOCATION (c),
12265 "%qE appears more than once in data clauses", t);
12269 bitmap_set_bit (&generic_head, DECL_UID (t));
12272 case OMP_CLAUSE_FIRSTPRIVATE:
12273 t = OMP_CLAUSE_DECL (c);
12274 need_complete = true;
12275 need_implicitly_determined = true;
12276 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12278 error_at (OMP_CLAUSE_LOCATION (c),
12279 "%qE is not a variable in clause %<firstprivate%>", t);
12282 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12283 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12285 error_at (OMP_CLAUSE_LOCATION (c),
12286 "%qE appears more than once in data clauses", t);
12290 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12293 case OMP_CLAUSE_LASTPRIVATE:
12294 t = OMP_CLAUSE_DECL (c);
12295 need_complete = true;
12296 need_implicitly_determined = true;
12297 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12299 error_at (OMP_CLAUSE_LOCATION (c),
12300 "%qE is not a variable in clause %<lastprivate%>", t);
12303 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12304 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12306 error_at (OMP_CLAUSE_LOCATION (c),
12307 "%qE appears more than once in data clauses", t);
12311 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12314 case OMP_CLAUSE_ALIGNED:
12315 t = OMP_CLAUSE_DECL (c);
12316 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12318 error_at (OMP_CLAUSE_LOCATION (c),
12319 "%qE is not a variable in %<aligned%> clause", t);
12322 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12323 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12325 error_at (OMP_CLAUSE_LOCATION (c),
12326 "%qE in %<aligned%> clause is neither a pointer nor "
12330 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12332 error_at (OMP_CLAUSE_LOCATION (c),
12333 "%qE appears more than once in %<aligned%> clauses",
12338 bitmap_set_bit (&aligned_head, DECL_UID (t));
12341 case OMP_CLAUSE_DEPEND:
12342 t = OMP_CLAUSE_DECL (c);
12343 if (TREE_CODE (t) == TREE_LIST)
12345 if (handle_omp_array_sections (c))
12349 if (t == error_mark_node)
12351 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12353 error_at (OMP_CLAUSE_LOCATION (c),
12354 "%qE is not a variable in %<depend%> clause", t);
12357 else if (!c_mark_addressable (t))
12361 case OMP_CLAUSE_MAP:
12362 case OMP_CLAUSE_TO:
12363 case OMP_CLAUSE_FROM:
12364 case OMP_CLAUSE__CACHE_:
12365 t = OMP_CLAUSE_DECL (c);
12366 if (TREE_CODE (t) == TREE_LIST)
12368 if (handle_omp_array_sections (c))
12372 t = OMP_CLAUSE_DECL (c);
12373 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12375 error_at (OMP_CLAUSE_LOCATION (c),
12376 "array section does not have mappable type "
12378 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12384 if (t == error_mark_node)
12386 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12388 error_at (OMP_CLAUSE_LOCATION (c),
12389 "%qE is not a variable in %qs clause", t,
12390 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12393 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12395 error_at (OMP_CLAUSE_LOCATION (c),
12396 "%qD is threadprivate variable in %qs clause", t,
12397 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12400 else if (!c_mark_addressable (t))
12402 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12403 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12404 || (OMP_CLAUSE_MAP_KIND (c)
12405 == GOMP_MAP_FORCE_DEVICEPTR)))
12406 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12408 error_at (OMP_CLAUSE_LOCATION (c),
12409 "%qD does not have a mappable type in %qs clause", t,
12410 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12413 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12415 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12416 error ("%qD appears more than once in motion clauses", t);
12418 error ("%qD appears more than once in map clauses", t);
12422 bitmap_set_bit (&generic_head, DECL_UID (t));
12425 case OMP_CLAUSE_UNIFORM:
12426 t = OMP_CLAUSE_DECL (c);
12427 if (TREE_CODE (t) != PARM_DECL)
12430 error_at (OMP_CLAUSE_LOCATION (c),
12431 "%qD is not an argument in %<uniform%> clause", t);
12433 error_at (OMP_CLAUSE_LOCATION (c),
12434 "%qE is not an argument in %<uniform%> clause", t);
12438 goto check_dup_generic;
12440 case OMP_CLAUSE_NOWAIT:
12441 if (copyprivate_seen)
12443 error_at (OMP_CLAUSE_LOCATION (c),
12444 "%<nowait%> clause must not be used together "
12445 "with %<copyprivate%>");
12449 nowait_clause = pc;
12450 pc = &OMP_CLAUSE_CHAIN (c);
12453 case OMP_CLAUSE_IF:
12454 case OMP_CLAUSE_NUM_THREADS:
12455 case OMP_CLAUSE_NUM_TEAMS:
12456 case OMP_CLAUSE_THREAD_LIMIT:
12457 case OMP_CLAUSE_SCHEDULE:
12458 case OMP_CLAUSE_ORDERED:
12459 case OMP_CLAUSE_DEFAULT:
12460 case OMP_CLAUSE_UNTIED:
12461 case OMP_CLAUSE_COLLAPSE:
12462 case OMP_CLAUSE_FINAL:
12463 case OMP_CLAUSE_MERGEABLE:
12464 case OMP_CLAUSE_SAFELEN:
12465 case OMP_CLAUSE_SIMDLEN:
12466 case OMP_CLAUSE_DEVICE:
12467 case OMP_CLAUSE_DIST_SCHEDULE:
12468 case OMP_CLAUSE_PARALLEL:
12469 case OMP_CLAUSE_FOR:
12470 case OMP_CLAUSE_SECTIONS:
12471 case OMP_CLAUSE_TASKGROUP:
12472 case OMP_CLAUSE_PROC_BIND:
12473 case OMP_CLAUSE__CILK_FOR_COUNT_:
12474 case OMP_CLAUSE_NUM_GANGS:
12475 case OMP_CLAUSE_NUM_WORKERS:
12476 case OMP_CLAUSE_VECTOR_LENGTH:
12477 case OMP_CLAUSE_ASYNC:
12478 case OMP_CLAUSE_WAIT:
12479 case OMP_CLAUSE_AUTO:
12480 case OMP_CLAUSE_SEQ:
12481 case OMP_CLAUSE_GANG:
12482 case OMP_CLAUSE_WORKER:
12483 case OMP_CLAUSE_VECTOR:
12484 pc = &OMP_CLAUSE_CHAIN (c);
12487 case OMP_CLAUSE_INBRANCH:
12488 case OMP_CLAUSE_NOTINBRANCH:
12491 error_at (OMP_CLAUSE_LOCATION (c),
12492 "%<inbranch%> clause is incompatible with "
12493 "%<notinbranch%>");
12497 branch_seen = true;
12498 pc = &OMP_CLAUSE_CHAIN (c);
12502 gcc_unreachable ();
12507 t = OMP_CLAUSE_DECL (c);
12511 t = require_complete_type (t);
12512 if (t == error_mark_node)
12516 if (need_implicitly_determined)
12518 const char *share_name = NULL;
12520 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12521 share_name = "threadprivate";
12522 else switch (c_omp_predetermined_sharing (t))
12524 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12526 case OMP_CLAUSE_DEFAULT_SHARED:
12527 /* const vars may be specified in firstprivate clause. */
12528 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12529 && TREE_READONLY (t))
12531 share_name = "shared";
12533 case OMP_CLAUSE_DEFAULT_PRIVATE:
12534 share_name = "private";
12537 gcc_unreachable ();
12541 error_at (OMP_CLAUSE_LOCATION (c),
12542 "%qE is predetermined %qs for %qs",
12544 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12551 *pc = OMP_CLAUSE_CHAIN (c);
12553 pc = &OMP_CLAUSE_CHAIN (c);
12556 bitmap_obstack_release (NULL);
12560 /* Create a transaction node. */
12563 c_finish_transaction (location_t loc, tree block, int flags)
12565 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12566 if (flags & TM_STMT_ATTR_OUTER)
12567 TRANSACTION_EXPR_OUTER (stmt) = 1;
12568 if (flags & TM_STMT_ATTR_RELAXED)
12569 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12570 return add_stmt (stmt);
12573 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12574 down to the element type of an array. */
12577 c_build_qualified_type (tree type, int type_quals)
12579 if (type == error_mark_node)
12582 if (TREE_CODE (type) == ARRAY_TYPE)
12585 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12588 /* See if we already have an identically qualified type. */
12589 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12591 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12592 && TYPE_NAME (t) == TYPE_NAME (type)
12593 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12594 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12595 TYPE_ATTRIBUTES (type)))
12600 tree domain = TYPE_DOMAIN (type);
12602 t = build_variant_type_copy (type);
12603 TREE_TYPE (t) = element_type;
12605 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12606 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12607 SET_TYPE_STRUCTURAL_EQUALITY (t);
12608 else if (TYPE_CANONICAL (element_type) != element_type
12609 || (domain && TYPE_CANONICAL (domain) != domain))
12611 tree unqualified_canon
12612 = build_array_type (TYPE_CANONICAL (element_type),
12613 domain? TYPE_CANONICAL (domain)
12616 = c_build_qualified_type (unqualified_canon, type_quals);
12619 TYPE_CANONICAL (t) = t;
12624 /* A restrict-qualified pointer type must be a pointer to object or
12625 incomplete type. Note that the use of POINTER_TYPE_P also allows
12626 REFERENCE_TYPEs, which is appropriate for C++. */
12627 if ((type_quals & TYPE_QUAL_RESTRICT)
12628 && (!POINTER_TYPE_P (type)
12629 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12631 error ("invalid use of %<restrict%>");
12632 type_quals &= ~TYPE_QUAL_RESTRICT;
12635 return build_qualified_type (type, type_quals);
12638 /* Build a VA_ARG_EXPR for the C parser. */
12641 c_build_va_arg (location_t loc, tree expr, tree type)
12643 if (error_operand_p (type))
12644 return error_mark_node;
12645 else if (!COMPLETE_TYPE_P (type))
12647 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12649 return error_mark_node;
12651 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12652 warning_at (loc, OPT_Wc___compat,
12653 "C++ requires promoted type, not enum type, in %<va_arg%>");
12654 return build_va_arg (loc, expr, type);
12657 /* Return truthvalue of whether T1 is the same tree structure as T2.
12658 Return 1 if they are the same. Return 0 if they are different. */
12661 c_tree_equal (tree t1, tree t2)
12663 enum tree_code code1, code2;
12670 for (code1 = TREE_CODE (t1);
12671 CONVERT_EXPR_CODE_P (code1)
12672 || code1 == NON_LVALUE_EXPR;
12673 code1 = TREE_CODE (t1))
12674 t1 = TREE_OPERAND (t1, 0);
12675 for (code2 = TREE_CODE (t2);
12676 CONVERT_EXPR_CODE_P (code2)
12677 || code2 == NON_LVALUE_EXPR;
12678 code2 = TREE_CODE (t2))
12679 t2 = TREE_OPERAND (t2, 0);
12681 /* They might have become equal now. */
12685 if (code1 != code2)
12691 return wi::eq_p (t1, t2);
12694 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12697 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12698 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12699 TREE_STRING_LENGTH (t1));
12702 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12703 TREE_FIXED_CST (t2));
12706 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12707 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12710 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12713 /* We need to do this when determining whether or not two
12714 non-type pointer to member function template arguments
12716 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12717 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12722 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12724 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12725 if (!c_tree_equal (field, elt2->index)
12726 || !c_tree_equal (value, elt2->value))
12733 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12735 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12737 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12740 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12745 call_expr_arg_iterator iter1, iter2;
12746 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12748 for (arg1 = first_call_expr_arg (t1, &iter1),
12749 arg2 = first_call_expr_arg (t2, &iter2);
12751 arg1 = next_call_expr_arg (&iter1),
12752 arg2 = next_call_expr_arg (&iter2))
12753 if (!c_tree_equal (arg1, arg2))
12762 tree o1 = TREE_OPERAND (t1, 0);
12763 tree o2 = TREE_OPERAND (t2, 0);
12765 /* Special case: if either target is an unallocated VAR_DECL,
12766 it means that it's going to be unified with whatever the
12767 TARGET_EXPR is really supposed to initialize, so treat it
12768 as being equivalent to anything. */
12769 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12770 && !DECL_RTL_SET_P (o1))
12772 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12773 && !DECL_RTL_SET_P (o2))
12775 else if (!c_tree_equal (o1, o2))
12778 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12781 case COMPONENT_REF:
12782 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12784 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12790 case FUNCTION_DECL:
12791 case IDENTIFIER_NODE:
12798 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12800 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12801 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12802 TREE_VEC_ELT (t2, ix)))
12811 switch (TREE_CODE_CLASS (code1))
12815 case tcc_comparison:
12816 case tcc_expression:
12818 case tcc_reference:
12819 case tcc_statement:
12821 int i, n = TREE_OPERAND_LENGTH (t1);
12825 case PREINCREMENT_EXPR:
12826 case PREDECREMENT_EXPR:
12827 case POSTINCREMENT_EXPR:
12828 case POSTDECREMENT_EXPR:
12838 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12839 && n != TREE_OPERAND_LENGTH (t2))
12842 for (i = 0; i < n; ++i)
12843 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12850 return comptypes (t1, t2);
12852 gcc_unreachable ();
12854 /* We can get here with --disable-checking. */
12858 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12859 spawn-helper and BODY is the newly created body for FNDECL. */
12862 cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12864 tree list = alloc_stmt_list ();
12865 tree frame = make_cilk_frame (fndecl);
12866 tree dtor = create_cilk_function_exit (frame, false, true);
12867 add_local_decl (cfun, frame);
12869 DECL_SAVED_TREE (fndecl) = list;
12870 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12872 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12873 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12875 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12876 append_to_statement_list (detach_expr, &body_list);
12878 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12879 body = fold_build_cleanup_point_expr (void_type_node, body);
12881 append_to_statement_list (body, &body_list);
12882 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12883 body_list, dtor), &list);