1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2023 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"
34 #include "gimple-expr.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
40 #include "langhooks.h"
43 #include "tree-iterator.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
56 /* Possible cases of implicit conversions. Used to select diagnostic messages
57 and control folding initializers in convert_for_assignment. */
66 /* The level of nesting inside "__alignof__". */
69 /* The level of nesting inside "sizeof". */
72 /* The level of nesting inside "typeof". */
75 /* True when parsing OpenMP loop expressions. */
78 /* The argument of last parsed sizeof expression, only to be tested
79 if expr.original_code == SIZEOF_EXPR. */
80 tree c_last_sizeof_arg;
81 location_t c_last_sizeof_loc;
83 /* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85 static int found_missing_braces;
87 static bool require_constant_value;
88 static bool require_constant_elements;
89 static bool require_constexpr_value;
91 static bool null_pointer_constant_p (const_tree);
92 static tree qualify_type (tree, tree);
93 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
95 static int comp_target_types (location_t, tree, tree);
96 static int function_types_compatible_p (const_tree, const_tree, bool *,
98 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
99 static tree lookup_field (tree, tree);
100 static int convert_arguments (location_t, vec<location_t>, tree,
101 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
103 static tree pointer_diff (location_t, tree, tree, tree *);
104 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
105 enum impl_conv, bool, tree, tree, int,
107 static tree valid_compound_expr_initializer (tree, tree);
108 static void push_string (const char *);
109 static void push_member_name (tree);
110 static int spelling_length (void);
111 static char *print_spelling (char *);
112 static void warning_init (location_t, int, const char *);
113 static tree digest_init (location_t, tree, tree, tree, bool, bool, bool, bool,
115 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
116 bool, struct obstack *);
117 static void output_pending_init_elements (int, struct obstack *);
118 static bool set_designator (location_t, bool, struct obstack *);
119 static void push_range_stack (tree, struct obstack *);
120 static void add_pending_init (location_t, tree, tree, tree, bool,
122 static void set_nonincremental_init (struct obstack *);
123 static void set_nonincremental_init_from_string (tree, struct obstack *);
124 static tree find_init_member (tree, struct obstack *);
125 static void readonly_warning (tree, enum lvalue_use);
126 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
127 static void record_maybe_used_decl (tree);
128 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
130 /* Return true if EXP is a null pointer constant, false otherwise. */
133 null_pointer_constant_p (const_tree expr)
135 /* This should really operate on c_expr structures, but they aren't
136 yet available everywhere required. */
137 tree type = TREE_TYPE (expr);
139 /* An integer constant expression with the value 0, such an expression
140 cast to type void*, or the predefined constant nullptr, are a null
142 if (expr == nullptr_node)
145 return (TREE_CODE (expr) == INTEGER_CST
146 && !TREE_OVERFLOW (expr)
147 && integer_zerop (expr)
148 && (INTEGRAL_TYPE_P (type)
149 || (TREE_CODE (type) == POINTER_TYPE
150 && VOID_TYPE_P (TREE_TYPE (type))
151 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
154 /* EXPR may appear in an unevaluated part of an integer constant
155 expression, but not in an evaluated part. Wrap it in a
156 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
157 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
160 note_integer_operands (tree expr)
163 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
165 ret = copy_node (expr);
166 TREE_OVERFLOW (ret) = 1;
170 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
171 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
176 /* Having checked whether EXPR may appear in an unevaluated part of an
177 integer constant expression and found that it may, remove any
178 C_MAYBE_CONST_EXPR noting this fact and return the resulting
182 remove_c_maybe_const_expr (tree expr)
184 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
185 return C_MAYBE_CONST_EXPR_EXPR (expr);
190 \f/* This is a cache to hold if two types are compatible or not. */
192 struct tagged_tu_seen_cache {
193 const struct tagged_tu_seen_cache * next;
196 /* The return value of tagged_types_tu_compatible_p if we had seen
197 these two types already. */
201 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
202 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
204 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
205 does not have an incomplete type. (That includes void types.)
206 LOC is the location of the use. */
209 require_complete_type (location_t loc, tree value)
211 tree type = TREE_TYPE (value);
213 if (error_operand_p (value))
214 return error_mark_node;
216 /* First, detect a valid value with a complete type. */
217 if (COMPLETE_TYPE_P (type))
220 c_incomplete_type_error (loc, value, type);
221 return error_mark_node;
224 /* Print an error message for invalid use of an incomplete type.
225 VALUE is the expression that was used (or 0 if that isn't known)
226 and TYPE is the type that was invalid. LOC is the location for
230 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
232 /* Avoid duplicate error message. */
233 if (TREE_CODE (type) == ERROR_MARK)
236 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
237 error_at (loc, "%qD has an incomplete type %qT", value, type);
241 /* We must print an error message. Be clever about what it says. */
243 switch (TREE_CODE (type))
251 error_at (loc, "invalid use of void expression");
255 if (TYPE_DOMAIN (type))
257 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
259 error_at (loc, "invalid use of flexible array member");
262 type = TREE_TYPE (type);
265 error_at (loc, "invalid use of array with unspecified bounds");
272 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
273 error_at (loc, "invalid use of undefined type %qT", type);
275 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
276 error_at (loc, "invalid use of incomplete typedef %qT", type);
280 /* Given a type, apply default promotions wrt unnamed function
281 arguments and return the new type. */
284 c_type_promotes_to (tree type)
286 tree ret = NULL_TREE;
288 if (TYPE_MAIN_VARIANT (type) == float_type_node)
289 ret = double_type_node;
290 else if (c_promoting_integer_type_p (type))
292 /* Preserve unsignedness if not really getting any wider. */
293 if (TYPE_UNSIGNED (type)
294 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
295 ret = unsigned_type_node;
297 ret = integer_type_node;
300 if (ret != NULL_TREE)
301 return (TYPE_ATOMIC (type)
302 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
308 /* Return true if between two named address spaces, whether there is a superset
309 named address space that encompasses both address spaces. If there is a
310 superset, return which address space is the superset. */
313 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
320 else if (targetm.addr_space.subset_p (as1, as2))
325 else if (targetm.addr_space.subset_p (as2, as1))
334 /* Return a variant of TYPE which has all the type qualifiers of LIKE
335 as well as those of TYPE. */
338 qualify_type (tree type, tree like)
340 addr_space_t as_type = TYPE_ADDR_SPACE (type);
341 addr_space_t as_like = TYPE_ADDR_SPACE (like);
342 addr_space_t as_common;
344 /* If the two named address spaces are different, determine the common
345 superset address space. If there isn't one, raise an error. */
346 if (!addr_space_superset (as_type, as_like, &as_common))
349 error ("%qT and %qT are in disjoint named address spaces",
353 return c_build_qualified_type (type,
354 TYPE_QUALS_NO_ADDR_SPACE (type)
355 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
356 | ENCODE_QUAL_ADDR_SPACE (as_common));
359 /* Return true iff the given tree T is a variable length array. */
362 c_vla_type_p (const_tree t)
364 if (TREE_CODE (t) == ARRAY_TYPE
365 && C_TYPE_VARIABLE_SIZE (t))
370 /* If NTYPE is a type of a non-variadic function with a prototype
371 and OTYPE is a type of a function without a prototype and ATTRS
372 contains attribute format, diagnosess and removes it from ATTRS.
373 Returns the result of build_type_attribute_variant of NTYPE and
374 the (possibly) modified ATTRS. */
377 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
379 if (!prototype_p (otype)
380 && prototype_p (ntype)
381 && lookup_attribute ("format", attrs))
383 warning_at (input_location, OPT_Wattributes,
384 "%qs attribute cannot be applied to a function that "
385 "does not take variable arguments", "format");
386 attrs = remove_attribute ("format", attrs);
388 return build_type_attribute_variant (ntype, attrs);
391 /* Return the composite type of two compatible types.
393 We assume that comptypes has already been done and returned
394 nonzero; if that isn't so, this may crash. In particular, we
395 assume that qualifiers match. */
398 composite_type (tree t1, tree t2)
400 enum tree_code code1;
401 enum tree_code code2;
404 /* Save time if the two types are the same. */
406 if (t1 == t2) return t1;
408 /* If one type is nonsense, use the other. */
409 if (t1 == error_mark_node)
411 if (t2 == error_mark_node)
414 code1 = TREE_CODE (t1);
415 code2 = TREE_CODE (t2);
417 /* Merge the attributes. */
418 attributes = targetm.merge_type_attributes (t1, t2);
420 /* If one is an enumerated type and the other is the compatible
421 integer type, the composite type might be either of the two
422 (DR#013 question 3). For consistency, use the enumerated type as
423 the composite type. */
425 if (code1 == ENUMERAL_TYPE
426 && (code2 == INTEGER_TYPE || code2 == BOOLEAN_TYPE))
428 if (code2 == ENUMERAL_TYPE
429 && (code1 == INTEGER_TYPE || code1 == BOOLEAN_TYPE))
432 gcc_assert (code1 == code2);
437 /* For two pointers, do this recursively on the target type. */
439 tree pointed_to_1 = TREE_TYPE (t1);
440 tree pointed_to_2 = TREE_TYPE (t2);
441 tree target = composite_type (pointed_to_1, pointed_to_2);
442 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
443 t1 = build_type_attribute_variant (t1, attributes);
444 return qualify_type (t1, t2);
449 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
452 tree d1 = TYPE_DOMAIN (t1);
453 tree d2 = TYPE_DOMAIN (t2);
454 bool d1_variable, d2_variable;
455 bool d1_zero, d2_zero;
456 bool t1_complete, t2_complete;
458 /* We should not have any type quals on arrays at all. */
459 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
460 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
462 t1_complete = COMPLETE_TYPE_P (t1);
463 t2_complete = COMPLETE_TYPE_P (t2);
465 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
466 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
468 d1_variable = (!d1_zero
469 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
470 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
471 d2_variable = (!d2_zero
472 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
473 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
474 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
475 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
477 /* Save space: see if the result is identical to one of the args. */
478 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
479 && (d2_variable || d2_zero || !d1_variable))
480 return build_type_attribute_variant (t1, attributes);
481 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
482 && (d1_variable || d1_zero || !d2_variable))
483 return build_type_attribute_variant (t2, attributes);
485 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
486 return build_type_attribute_variant (t1, attributes);
487 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
488 return build_type_attribute_variant (t2, attributes);
490 /* Merge the element types, and have a size if either arg has
491 one. We may have qualifiers on the element types. To set
492 up TYPE_MAIN_VARIANT correctly, we need to form the
493 composite of the unqualified types and add the qualifiers
495 quals = TYPE_QUALS (strip_array_types (elt));
496 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
497 t1 = build_array_type (unqual_elt,
498 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
504 /* Ensure a composite type involving a zero-length array type
505 is a zero-length type not an incomplete type. */
506 if (d1_zero && d2_zero
507 && (t1_complete || t2_complete)
508 && !COMPLETE_TYPE_P (t1))
510 TYPE_SIZE (t1) = bitsize_zero_node;
511 TYPE_SIZE_UNIT (t1) = size_zero_node;
513 t1 = c_build_qualified_type (t1, quals);
514 return build_type_attribute_variant (t1, attributes);
520 if (attributes != NULL)
522 /* Try harder not to create a new aggregate type. */
523 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
525 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
528 return build_type_attribute_variant (t1, attributes);
531 /* Function types: prefer the one that specified arg types.
532 If both do, merge the arg types. Also merge the return types. */
534 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
535 tree p1 = TYPE_ARG_TYPES (t1);
536 tree p2 = TYPE_ARG_TYPES (t2);
541 /* Save space: see if the result is identical to one of the args. */
542 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
543 return build_functype_attribute_variant (t1, t2, attributes);
544 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
545 return build_functype_attribute_variant (t2, t1, attributes);
547 /* Simple way if one arg fails to specify argument types. */
548 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
550 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2),
551 TYPE_NO_NAMED_ARGS_STDARG_P (t2));
552 t1 = build_type_attribute_variant (t1, attributes);
553 return qualify_type (t1, t2);
555 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
557 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1),
558 TYPE_NO_NAMED_ARGS_STDARG_P (t1));
559 t1 = build_type_attribute_variant (t1, attributes);
560 return qualify_type (t1, t2);
563 /* If both args specify argument types, we must merge the two
564 lists, argument by argument. */
566 for (len = 0, newargs = p1;
567 newargs && newargs != void_list_node;
568 len++, newargs = TREE_CHAIN (newargs))
571 for (i = 0; i < len; i++)
572 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
576 for (; p1 && p1 != void_list_node;
577 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
579 /* A null type means arg type is not specified.
580 Take whatever the other function type has. */
581 if (TREE_VALUE (p1) == NULL_TREE)
583 TREE_VALUE (n) = TREE_VALUE (p2);
586 if (TREE_VALUE (p2) == NULL_TREE)
588 TREE_VALUE (n) = TREE_VALUE (p1);
592 /* Given wait (union {union wait *u; int *i} *)
593 and wait (union wait *),
594 prefer union wait * as type of parm. */
595 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
596 && TREE_VALUE (p1) != TREE_VALUE (p2))
599 tree mv2 = TREE_VALUE (p2);
600 if (mv2 && mv2 != error_mark_node
601 && TREE_CODE (mv2) != ARRAY_TYPE)
602 mv2 = TYPE_MAIN_VARIANT (mv2);
603 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
604 memb; memb = DECL_CHAIN (memb))
606 tree mv3 = TREE_TYPE (memb);
607 if (mv3 && mv3 != error_mark_node
608 && TREE_CODE (mv3) != ARRAY_TYPE)
609 mv3 = TYPE_MAIN_VARIANT (mv3);
610 if (comptypes (mv3, mv2))
612 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
614 pedwarn (input_location, OPT_Wpedantic,
615 "function types not truly compatible in ISO C");
620 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
621 && TREE_VALUE (p2) != TREE_VALUE (p1))
624 tree mv1 = TREE_VALUE (p1);
625 if (mv1 && mv1 != error_mark_node
626 && TREE_CODE (mv1) != ARRAY_TYPE)
627 mv1 = TYPE_MAIN_VARIANT (mv1);
628 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
629 memb; memb = DECL_CHAIN (memb))
631 tree mv3 = TREE_TYPE (memb);
632 if (mv3 && mv3 != error_mark_node
633 && TREE_CODE (mv3) != ARRAY_TYPE)
634 mv3 = TYPE_MAIN_VARIANT (mv3);
635 if (comptypes (mv3, mv1))
637 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
639 pedwarn (input_location, OPT_Wpedantic,
640 "function types not truly compatible in ISO C");
645 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
649 t1 = build_function_type (valtype, newargs);
650 t1 = qualify_type (t1, t2);
655 return build_type_attribute_variant (t1, attributes);
660 /* Return the type of a conditional expression between pointers to
661 possibly differently qualified versions of compatible types.
663 We assume that comp_target_types has already been done and returned
664 nonzero; if that isn't so, this may crash. */
667 common_pointer_type (tree t1, tree t2)
670 tree pointed_to_1, mv1;
671 tree pointed_to_2, mv2;
673 unsigned target_quals;
674 addr_space_t as1, as2, as_common;
677 /* Save time if the two types are the same. */
679 if (t1 == t2) return t1;
681 /* If one type is nonsense, use the other. */
682 if (t1 == error_mark_node)
684 if (t2 == error_mark_node)
687 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
688 && TREE_CODE (t2) == POINTER_TYPE);
690 /* Merge the attributes. */
691 attributes = targetm.merge_type_attributes (t1, t2);
693 /* Find the composite type of the target types, and combine the
694 qualifiers of the two types' targets. Do not lose qualifiers on
695 array element types by taking the TYPE_MAIN_VARIANT. */
696 mv1 = pointed_to_1 = TREE_TYPE (t1);
697 mv2 = pointed_to_2 = TREE_TYPE (t2);
698 if (TREE_CODE (mv1) != ARRAY_TYPE)
699 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
700 if (TREE_CODE (mv2) != ARRAY_TYPE)
701 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
702 target = composite_type (mv1, mv2);
704 /* Strip array types to get correct qualifier for pointers to arrays */
705 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
706 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
708 /* For function types do not merge const qualifiers, but drop them
709 if used inconsistently. The middle-end uses these to mark const
710 and noreturn functions. */
711 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
712 target_quals = (quals1 & quals2);
714 target_quals = (quals1 | quals2);
716 /* If the two named address spaces are different, determine the common
717 superset address space. This is guaranteed to exist due to the
718 assumption that comp_target_type returned non-zero. */
719 as1 = TYPE_ADDR_SPACE (pointed_to_1);
720 as2 = TYPE_ADDR_SPACE (pointed_to_2);
721 if (!addr_space_superset (as1, as2, &as_common))
724 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
726 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
727 return build_type_attribute_variant (t1, attributes);
730 /* Return the common type for two arithmetic types under the usual
731 arithmetic conversions. The default conversions have already been
732 applied, and enumerated types converted to their compatible integer
733 types. The resulting type is unqualified and has no attributes.
735 This is the type for the result of most arithmetic operations
736 if the operands have the given two types. */
739 c_common_type (tree t1, tree t2)
741 enum tree_code code1;
742 enum tree_code code2;
744 /* If one type is nonsense, use the other. */
745 if (t1 == error_mark_node)
747 if (t2 == error_mark_node)
750 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
751 t1 = TYPE_MAIN_VARIANT (t1);
753 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
754 t2 = TYPE_MAIN_VARIANT (t2);
756 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
758 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
759 t1 = build_type_attribute_variant (t1, attrs);
762 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
764 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
765 t2 = build_type_attribute_variant (t2, attrs);
768 /* Save time if the two types are the same. */
770 if (t1 == t2) return t1;
772 code1 = TREE_CODE (t1);
773 code2 = TREE_CODE (t2);
775 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
776 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
777 || code1 == INTEGER_TYPE);
778 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
779 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
780 || code2 == INTEGER_TYPE);
782 /* When one operand is a decimal float type, the other operand cannot be
783 a generic float type or a complex type. We also disallow vector types
785 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
786 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
788 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
790 error ("cannot mix operands of decimal floating and vector types");
791 return error_mark_node;
793 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
795 error ("cannot mix operands of decimal floating and complex types");
796 return error_mark_node;
798 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
800 error ("cannot mix operands of decimal floating "
801 "and other floating types");
802 return error_mark_node;
806 /* If one type is a vector type, return that type. (How the usual
807 arithmetic conversions apply to the vector types extension is not
808 precisely specified.) */
809 if (code1 == VECTOR_TYPE)
812 if (code2 == VECTOR_TYPE)
815 /* If one type is complex, form the common type of the non-complex
816 components, then make that complex. Use T1 or T2 if it is the
818 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
820 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
821 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
822 tree subtype = c_common_type (subtype1, subtype2);
824 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
826 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
829 return build_complex_type (subtype);
832 /* If only one is real, use it as the result. */
834 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
837 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
840 /* If both are real and either are decimal floating point types, use
841 the decimal floating point type with the greater precision. */
843 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
845 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
846 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
847 return dfloat128_type_node;
848 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
849 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
850 return dfloat64_type_node;
851 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
852 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
853 return dfloat32_type_node;
856 /* Deal with fixed-point types. */
857 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
859 unsigned int unsignedp = 0, satp = 0;
861 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
863 m1 = SCALAR_TYPE_MODE (t1);
864 m2 = SCALAR_TYPE_MODE (t2);
866 /* If one input type is saturating, the result type is saturating. */
867 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
870 /* If both fixed-point types are unsigned, the result type is unsigned.
871 When mixing fixed-point and integer types, follow the sign of the
873 Otherwise, the result type is signed. */
874 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
875 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
876 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
877 && TYPE_UNSIGNED (t1))
878 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
879 && TYPE_UNSIGNED (t2)))
882 /* The result type is signed. */
885 /* If the input type is unsigned, we need to convert to the
887 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
889 enum mode_class mclass = (enum mode_class) 0;
890 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
892 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
896 m1 = as_a <scalar_mode>
897 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
899 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
901 enum mode_class mclass = (enum mode_class) 0;
902 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
904 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
908 m2 = as_a <scalar_mode>
909 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
913 if (code1 == FIXED_POINT_TYPE)
915 fbit1 = GET_MODE_FBIT (m1);
916 ibit1 = GET_MODE_IBIT (m1);
921 /* Signed integers need to subtract one sign bit. */
922 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
925 if (code2 == FIXED_POINT_TYPE)
927 fbit2 = GET_MODE_FBIT (m2);
928 ibit2 = GET_MODE_IBIT (m2);
933 /* Signed integers need to subtract one sign bit. */
934 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
937 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
938 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
939 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
943 /* Both real or both integers; use the one with greater precision. */
945 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
947 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
950 /* Same precision. Prefer long longs to longs to ints when the
951 same precision, following the C99 rules on integer type rank
952 (which are equivalent to the C90 rules for C90 types). */
954 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
955 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
956 return long_long_unsigned_type_node;
958 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
959 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
961 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
962 return long_long_unsigned_type_node;
964 return long_long_integer_type_node;
967 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
968 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
969 return long_unsigned_type_node;
971 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
972 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
974 /* But preserve unsignedness from the other type,
975 since long cannot hold all the values of an unsigned int. */
976 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
977 return long_unsigned_type_node;
979 return long_integer_type_node;
982 /* For floating types of the same TYPE_PRECISION (which we here
983 assume means either the same set of values, or sets of values
984 neither a subset of the other, with behavior being undefined in
985 the latter case), follow the rules from TS 18661-3: prefer
986 interchange types _FloatN, then standard types long double,
987 double, float, then extended types _FloatNx. For extended types,
988 check them starting with _Float128x as that seems most consistent
989 in spirit with preferring long double to double; for interchange
990 types, also check in that order for consistency although it's not
991 possible for more than one of them to have the same
993 tree mv1 = TYPE_MAIN_VARIANT (t1);
994 tree mv2 = TYPE_MAIN_VARIANT (t2);
996 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
997 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
998 return FLOATN_TYPE_NODE (i);
1000 /* Likewise, prefer long double to double even if same size. */
1001 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1002 return long_double_type_node;
1004 /* Likewise, prefer double to float even if same size.
1005 We got a couple of embedded targets with 32 bit doubles, and the
1006 pdp11 might have 64 bit floats. */
1007 if (mv1 == double_type_node || mv2 == double_type_node)
1008 return double_type_node;
1010 if (mv1 == float_type_node || mv2 == float_type_node)
1011 return float_type_node;
1013 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1014 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1015 return FLOATNX_TYPE_NODE (i);
1017 /* Otherwise prefer the unsigned one. */
1019 if (TYPE_UNSIGNED (t1))
1025 /* Wrapper around c_common_type that is used by c-common.cc and other
1026 front end optimizations that remove promotions. ENUMERAL_TYPEs
1027 are allowed here and are converted to their compatible integer types.
1028 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1029 preferably a non-Boolean type as the common type. */
1031 common_type (tree t1, tree t2)
1033 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1034 t1 = ENUM_UNDERLYING_TYPE (t1);
1035 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1036 t2 = ENUM_UNDERLYING_TYPE (t2);
1038 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1039 if (TREE_CODE (t1) == BOOLEAN_TYPE
1040 && TREE_CODE (t2) == BOOLEAN_TYPE)
1041 return boolean_type_node;
1043 /* If either type is BOOLEAN_TYPE, then return the other. */
1044 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1046 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1049 return c_common_type (t1, t2);
1052 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1053 or various other operations. Return 2 if they are compatible
1054 but a warning may be needed if you use them together. */
1057 comptypes (tree type1, tree type2)
1059 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1062 val = comptypes_internal (type1, type2, NULL, NULL);
1063 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1068 /* Like comptypes, but if it returns non-zero because enum and int are
1069 compatible, it sets *ENUM_AND_INT_P to true. */
1072 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1074 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1077 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1078 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1083 /* Like comptypes, but if it returns nonzero for different types, it
1084 sets *DIFFERENT_TYPES_P to true. */
1087 comptypes_check_different_types (tree type1, tree type2,
1088 bool *different_types_p)
1090 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1093 val = comptypes_internal (type1, type2, NULL, different_types_p);
1094 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1099 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1100 or various other operations. Return 2 if they are compatible
1101 but a warning may be needed if you use them together. If
1102 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1103 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1104 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1105 NULL, and the types are compatible but different enough not to be
1106 permitted in C11 typedef redeclarations, then this sets
1107 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1108 false, but may or may not be set if the types are incompatible.
1109 This differs from comptypes, in that we don't free the seen
1113 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1114 bool *different_types_p)
1116 const_tree t1 = type1;
1117 const_tree t2 = type2;
1120 /* Suppress errors caused by previously reported errors. */
1122 if (t1 == t2 || !t1 || !t2
1123 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1126 /* Enumerated types are compatible with integer types, but this is
1127 not transitive: two enumerated types in the same translation unit
1128 are compatible with each other only if they are the same type. */
1130 if (TREE_CODE (t1) == ENUMERAL_TYPE
1131 && COMPLETE_TYPE_P (t1)
1132 && TREE_CODE (t2) != ENUMERAL_TYPE)
1134 t1 = ENUM_UNDERLYING_TYPE (t1);
1135 if (TREE_CODE (t2) != VOID_TYPE)
1137 if (enum_and_int_p != NULL)
1138 *enum_and_int_p = true;
1139 if (different_types_p != NULL)
1140 *different_types_p = true;
1143 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1144 && COMPLETE_TYPE_P (t2)
1145 && TREE_CODE (t1) != ENUMERAL_TYPE)
1147 t2 = ENUM_UNDERLYING_TYPE (t2);
1148 if (TREE_CODE (t1) != VOID_TYPE)
1150 if (enum_and_int_p != NULL)
1151 *enum_and_int_p = true;
1152 if (different_types_p != NULL)
1153 *different_types_p = true;
1160 /* Different classes of types can't be compatible. */
1162 if (TREE_CODE (t1) != TREE_CODE (t2))
1165 /* Qualifiers must match. C99 6.7.3p9 */
1167 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1170 /* Allow for two different type nodes which have essentially the same
1171 definition. Note that we already checked for equality of the type
1172 qualifiers (just above). */
1174 if (TREE_CODE (t1) != ARRAY_TYPE
1175 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1178 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1179 if (!(attrval = comp_type_attributes (t1, t2)))
1182 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1185 switch (TREE_CODE (t1))
1188 case FIXED_POINT_TYPE:
1190 /* With these nodes, we can't determine type equivalence by
1191 looking at what is stored in the nodes themselves, because
1192 two nodes might have different TYPE_MAIN_VARIANTs but still
1193 represent the same type. For example, wchar_t and int could
1194 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1195 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1196 and are distinct types. On the other hand, int and the
1199 typedef int INT __attribute((may_alias));
1201 have identical properties, different TYPE_MAIN_VARIANTs, but
1202 represent the same type. The canonical type system keeps
1203 track of equivalence in this case, so we fall back on it. */
1204 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1207 /* Do not remove mode information. */
1208 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1210 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1211 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1212 enum_and_int_p, different_types_p));
1216 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1222 tree d1 = TYPE_DOMAIN (t1);
1223 tree d2 = TYPE_DOMAIN (t2);
1224 bool d1_variable, d2_variable;
1225 bool d1_zero, d2_zero;
1228 /* Target types must match incl. qualifiers. */
1229 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1230 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1232 different_types_p)) == 0)
1235 if (different_types_p != NULL
1236 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1237 *different_types_p = true;
1238 /* Sizes must match unless one is missing or variable. */
1239 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1242 d1_zero = !TYPE_MAX_VALUE (d1);
1243 d2_zero = !TYPE_MAX_VALUE (d2);
1245 d1_variable = (!d1_zero
1246 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1247 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1248 d2_variable = (!d2_zero
1249 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1250 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1251 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1252 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1254 if (different_types_p != NULL
1255 && d1_variable != d2_variable)
1256 *different_types_p = true;
1257 if (d1_variable || d2_variable)
1259 if (d1_zero && d2_zero)
1261 if (d1_zero || d2_zero
1262 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1263 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1272 if (val != 1 && !same_translation_unit_p (t1, t2))
1274 tree a1 = TYPE_ATTRIBUTES (t1);
1275 tree a2 = TYPE_ATTRIBUTES (t2);
1277 if (! attribute_list_contained (a1, a2)
1278 && ! attribute_list_contained (a2, a1))
1282 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1284 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1290 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1291 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1292 enum_and_int_p, different_types_p));
1298 return attrval == 2 && val == 1 ? 2 : val;
1301 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1302 their qualifiers, except for named address spaces. If the pointers point to
1303 different named addresses, then we must determine if one address space is a
1304 subset of the other. */
1307 comp_target_types (location_t location, tree ttl, tree ttr)
1311 tree mvl = TREE_TYPE (ttl);
1312 tree mvr = TREE_TYPE (ttr);
1313 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1314 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1315 addr_space_t as_common;
1316 bool enum_and_int_p;
1318 /* Fail if pointers point to incompatible address spaces. */
1319 if (!addr_space_superset (asl, asr, &as_common))
1322 /* For pedantic record result of comptypes on arrays before losing
1323 qualifiers on the element type below. */
1326 if (TREE_CODE (mvl) == ARRAY_TYPE
1327 && TREE_CODE (mvr) == ARRAY_TYPE)
1328 val_ped = comptypes (mvl, mvr);
1330 /* Qualifiers on element types of array types that are
1331 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1333 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1334 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1335 : TYPE_MAIN_VARIANT (mvl));
1337 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1338 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1339 : TYPE_MAIN_VARIANT (mvr));
1341 enum_and_int_p = false;
1342 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1344 if (val == 1 && val_ped != 1)
1345 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1346 "in ISO C before C2X");
1349 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1351 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1352 warning_at (location, OPT_Wc___compat,
1353 "pointer target types incompatible in C++");
1358 /* Subroutines of `comptypes'. */
1360 /* Determine whether two trees derive from the same translation unit.
1361 If the CONTEXT chain ends in a null, that tree's context is still
1362 being parsed, so if two trees have context chains ending in null,
1363 they're in the same translation unit. */
1366 same_translation_unit_p (const_tree t1, const_tree t2)
1368 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1369 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1371 case tcc_declaration:
1372 t1 = DECL_CONTEXT (t1); break;
1374 t1 = TYPE_CONTEXT (t1); break;
1375 case tcc_exceptional:
1376 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1377 default: gcc_unreachable ();
1380 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1381 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1383 case tcc_declaration:
1384 t2 = DECL_CONTEXT (t2); break;
1386 t2 = TYPE_CONTEXT (t2); break;
1387 case tcc_exceptional:
1388 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1389 default: gcc_unreachable ();
1395 /* Allocate the seen two types, assuming that they are compatible. */
1397 static struct tagged_tu_seen_cache *
1398 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1400 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1401 tu->next = tagged_tu_seen_base;
1405 tagged_tu_seen_base = tu;
1407 /* The C standard says that two structures in different translation
1408 units are compatible with each other only if the types of their
1409 fields are compatible (among other things). We assume that they
1410 are compatible until proven otherwise when building the cache.
1411 An example where this can occur is:
1416 If we are comparing this against a similar struct in another TU,
1417 and did not assume they were compatible, we end up with an infinite
1423 /* Free the seen types until we get to TU_TIL. */
1426 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1428 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1429 while (tu != tu_til)
1431 const struct tagged_tu_seen_cache *const tu1
1432 = (const struct tagged_tu_seen_cache *) tu;
1434 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1436 tagged_tu_seen_base = tu_til;
1439 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1440 compatible. If the two types are not the same (which has been
1441 checked earlier), this can only happen when multiple translation
1442 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1443 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1444 comptypes_internal. */
1447 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1448 bool *enum_and_int_p, bool *different_types_p)
1451 bool needs_warning = false;
1453 /* We have to verify that the tags of the types are the same. This
1454 is harder than it looks because this may be a typedef, so we have
1455 to go look at the original type. It may even be a typedef of a
1457 In the case of compiler-created builtin structs the TYPE_DECL
1458 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1459 while (TYPE_NAME (t1)
1460 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1461 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1462 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1464 while (TYPE_NAME (t2)
1465 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1466 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1467 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1469 /* C90 didn't have the requirement that the two tags be the same. */
1470 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1473 /* C90 didn't say what happened if one or both of the types were
1474 incomplete; we choose to follow C99 rules here, which is that they
1476 if (TYPE_SIZE (t1) == NULL
1477 || TYPE_SIZE (t2) == NULL)
1481 const struct tagged_tu_seen_cache * tts_i;
1482 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1483 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1487 switch (TREE_CODE (t1))
1491 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1492 /* Speed up the case where the type values are in the same order. */
1493 tree tv1 = TYPE_VALUES (t1);
1494 tree tv2 = TYPE_VALUES (t2);
1501 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1503 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1505 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1512 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1516 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1522 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1528 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1530 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1532 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1543 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1544 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1550 /* Speed up the common case where the fields are in the same order. */
1551 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1552 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1556 if (DECL_NAME (s1) != DECL_NAME (s2))
1558 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1559 enum_and_int_p, different_types_p);
1561 if (result != 1 && !DECL_NAME (s1))
1569 needs_warning = true;
1571 if (TREE_CODE (s1) == FIELD_DECL
1572 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1573 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1581 tu->val = needs_warning ? 2 : 1;
1585 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1589 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1590 if (DECL_NAME (s1) == DECL_NAME (s2))
1594 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1598 if (result != 1 && !DECL_NAME (s1))
1606 needs_warning = true;
1608 if (TREE_CODE (s1) == FIELD_DECL
1609 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1610 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1622 tu->val = needs_warning ? 2 : 10;
1628 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1630 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1632 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1635 if (TREE_CODE (s1) != TREE_CODE (s2)
1636 || DECL_NAME (s1) != DECL_NAME (s2))
1638 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1639 enum_and_int_p, different_types_p);
1643 needs_warning = true;
1645 if (TREE_CODE (s1) == FIELD_DECL
1646 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1647 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1653 tu->val = needs_warning ? 2 : 1;
1662 /* Return 1 if two function types F1 and F2 are compatible.
1663 If either type specifies no argument types,
1664 the other must specify a fixed number of self-promoting arg types.
1665 Otherwise, if one type specifies only the number of arguments,
1666 the other must specify that number of self-promoting arg types.
1667 Otherwise, the argument types must match.
1668 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1671 function_types_compatible_p (const_tree f1, const_tree f2,
1672 bool *enum_and_int_p, bool *different_types_p)
1675 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1680 ret1 = TREE_TYPE (f1);
1681 ret2 = TREE_TYPE (f2);
1683 /* 'volatile' qualifiers on a function's return type used to mean
1684 the function is noreturn. */
1685 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1686 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1687 if (TYPE_VOLATILE (ret1))
1688 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1689 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1690 if (TYPE_VOLATILE (ret2))
1691 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1692 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1693 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1697 args1 = TYPE_ARG_TYPES (f1);
1698 args2 = TYPE_ARG_TYPES (f2);
1700 if (different_types_p != NULL
1701 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1702 *different_types_p = true;
1704 /* An unspecified parmlist matches any specified parmlist
1705 whose argument types don't need default promotions. */
1707 if (args1 == NULL_TREE)
1709 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1711 if (!self_promoting_args_p (args2))
1713 /* If one of these types comes from a non-prototype fn definition,
1714 compare that with the other type's arglist.
1715 If they don't match, ask for a warning (but no error). */
1716 if (TYPE_ACTUAL_ARG_TYPES (f1)
1717 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1718 enum_and_int_p, different_types_p) != 1)
1722 if (args2 == NULL_TREE)
1724 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1726 if (!self_promoting_args_p (args1))
1728 if (TYPE_ACTUAL_ARG_TYPES (f2)
1729 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1730 enum_and_int_p, different_types_p) != 1)
1735 /* Both types have argument lists: compare them and propagate results. */
1736 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1738 return val1 != 1 ? val1 : val;
1741 /* Check two lists of types for compatibility, returning 0 for
1742 incompatible, 1 for compatible, or 2 for compatible with
1743 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1744 comptypes_internal. */
1747 type_lists_compatible_p (const_tree args1, const_tree args2,
1748 bool *enum_and_int_p, bool *different_types_p)
1750 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1756 tree a1, mv1, a2, mv2;
1757 if (args1 == NULL_TREE && args2 == NULL_TREE)
1759 /* If one list is shorter than the other,
1760 they fail to match. */
1761 if (args1 == NULL_TREE || args2 == NULL_TREE)
1763 mv1 = a1 = TREE_VALUE (args1);
1764 mv2 = a2 = TREE_VALUE (args2);
1765 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1766 mv1 = (TYPE_ATOMIC (mv1)
1767 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1769 : TYPE_MAIN_VARIANT (mv1));
1770 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1771 mv2 = (TYPE_ATOMIC (mv2)
1772 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1774 : TYPE_MAIN_VARIANT (mv2));
1775 /* A null pointer instead of a type
1776 means there is supposed to be an argument
1777 but nothing is specified about what type it has.
1778 So match anything that self-promotes. */
1779 if (different_types_p != NULL
1780 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1781 *different_types_p = true;
1782 if (a1 == NULL_TREE)
1784 if (c_type_promotes_to (a2) != a2)
1787 else if (a2 == NULL_TREE)
1789 if (c_type_promotes_to (a1) != a1)
1792 /* If one of the lists has an error marker, ignore this arg. */
1793 else if (TREE_CODE (a1) == ERROR_MARK
1794 || TREE_CODE (a2) == ERROR_MARK)
1796 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1797 different_types_p)))
1799 if (different_types_p != NULL)
1800 *different_types_p = true;
1801 /* Allow wait (union {union wait *u; int *i} *)
1802 and wait (union wait *) to be compatible. */
1803 if (TREE_CODE (a1) == UNION_TYPE
1804 && (TYPE_NAME (a1) == NULL_TREE
1805 || TYPE_TRANSPARENT_AGGR (a1))
1806 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1807 && tree_int_cst_equal (TYPE_SIZE (a1),
1811 for (memb = TYPE_FIELDS (a1);
1812 memb; memb = DECL_CHAIN (memb))
1814 tree mv3 = TREE_TYPE (memb);
1815 if (mv3 && mv3 != error_mark_node
1816 && TREE_CODE (mv3) != ARRAY_TYPE)
1817 mv3 = (TYPE_ATOMIC (mv3)
1818 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1820 : TYPE_MAIN_VARIANT (mv3));
1821 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1825 if (memb == NULL_TREE)
1828 else if (TREE_CODE (a2) == UNION_TYPE
1829 && (TYPE_NAME (a2) == NULL_TREE
1830 || TYPE_TRANSPARENT_AGGR (a2))
1831 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1832 && tree_int_cst_equal (TYPE_SIZE (a2),
1836 for (memb = TYPE_FIELDS (a2);
1837 memb; memb = DECL_CHAIN (memb))
1839 tree mv3 = TREE_TYPE (memb);
1840 if (mv3 && mv3 != error_mark_node
1841 && TREE_CODE (mv3) != ARRAY_TYPE)
1842 mv3 = (TYPE_ATOMIC (mv3)
1843 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1845 : TYPE_MAIN_VARIANT (mv3));
1846 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1850 if (memb == NULL_TREE)
1857 /* comptypes said ok, but record if it said to warn. */
1861 args1 = TREE_CHAIN (args1);
1862 args2 = TREE_CHAIN (args2);
1866 /* Compute the size to increment a pointer by. When a function type or void
1867 type or incomplete type is passed, size_one_node is returned.
1868 This function does not emit any diagnostics; the caller is responsible
1872 c_size_in_bytes (const_tree type)
1874 enum tree_code code = TREE_CODE (type);
1876 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1877 || !COMPLETE_TYPE_P (type))
1878 return size_one_node;
1880 /* Convert in case a char is more than one unit. */
1881 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1882 size_int (TYPE_PRECISION (char_type_node)
1886 /* Return either DECL or its known constant value (if it has one). */
1889 decl_constant_value_1 (tree decl, bool in_init)
1891 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1892 TREE_CODE (decl) != PARM_DECL
1893 && !TREE_THIS_VOLATILE (decl)
1894 && TREE_READONLY (decl)
1895 && DECL_INITIAL (decl) != NULL_TREE
1896 && !error_operand_p (DECL_INITIAL (decl))
1897 /* This is invalid if initial value is not constant.
1898 If it has either a function call, a memory reference,
1899 or a variable, then re-evaluating it could give different results. */
1900 && TREE_CONSTANT (DECL_INITIAL (decl))
1901 /* Check for cases where this is sub-optimal, even though valid. */
1902 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1903 return DECL_INITIAL (decl);
1907 /* Return either DECL or its known constant value (if it has one).
1908 Like the above, but always return decl outside of functions. */
1911 decl_constant_value (tree decl)
1913 /* Don't change a variable array bound or initial value to a constant
1914 in a place where a variable is invalid. */
1915 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1918 /* Convert the array expression EXP to a pointer. */
1920 array_to_pointer_conversion (location_t loc, tree exp)
1922 tree orig_exp = exp;
1923 tree type = TREE_TYPE (exp);
1925 tree restype = TREE_TYPE (type);
1928 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1930 STRIP_TYPE_NOPS (exp);
1932 copy_warning (exp, orig_exp);
1934 ptrtype = build_pointer_type (restype);
1936 if (INDIRECT_REF_P (exp))
1937 return convert (ptrtype, TREE_OPERAND (exp, 0));
1939 /* In C++ array compound literals are temporary objects unless they are
1940 const or appear in namespace scope, so they are destroyed too soon
1941 to use them for much of anything (c++/53220). */
1942 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1944 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1945 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1946 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1947 "converting an array compound literal to a pointer "
1948 "is ill-formed in C++");
1951 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1952 return convert (ptrtype, adr);
1955 /* Convert the function expression EXP to a pointer. */
1957 function_to_pointer_conversion (location_t loc, tree exp)
1959 tree orig_exp = exp;
1961 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1963 STRIP_TYPE_NOPS (exp);
1965 copy_warning (exp, orig_exp);
1967 return build_unary_op (loc, ADDR_EXPR, exp, false);
1970 /* Mark EXP as read, not just set, for set but not used -Wunused
1971 warning purposes. */
1974 mark_exp_read (tree exp)
1976 switch (TREE_CODE (exp))
1980 DECL_READ_P (exp) = 1;
1989 case VIEW_CONVERT_EXPR:
1990 mark_exp_read (TREE_OPERAND (exp, 0));
1993 /* Pattern match what build_atomic_assign produces with modifycode
1995 if (VAR_P (TREE_OPERAND (exp, 1))
1996 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1997 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1999 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2000 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2001 if (TREE_CODE (t1) == TARGET_EXPR
2002 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2003 && TREE_CODE (t2) == CALL_EXPR)
2005 tree fndecl = get_callee_fndecl (t2);
2006 tree arg = NULL_TREE;
2008 && TREE_CODE (fndecl) == FUNCTION_DECL
2009 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2010 && call_expr_nargs (t2) >= 2)
2011 switch (DECL_FUNCTION_CODE (fndecl))
2013 case BUILT_IN_ATOMIC_STORE:
2014 arg = CALL_EXPR_ARG (t2, 1);
2016 case BUILT_IN_ATOMIC_STORE_1:
2017 case BUILT_IN_ATOMIC_STORE_2:
2018 case BUILT_IN_ATOMIC_STORE_4:
2019 case BUILT_IN_ATOMIC_STORE_8:
2020 case BUILT_IN_ATOMIC_STORE_16:
2021 arg = CALL_EXPR_ARG (t2, 0);
2029 if (TREE_CODE (arg) == ADDR_EXPR
2030 && DECL_P (TREE_OPERAND (arg, 0))
2031 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2032 mark_exp_read (TREE_OPERAND (arg, 0));
2037 case C_MAYBE_CONST_EXPR:
2038 mark_exp_read (TREE_OPERAND (exp, 1));
2045 /* Perform the default conversion of arrays and functions to pointers.
2046 Return the result of converting EXP. For any other expression, just
2049 LOC is the location of the expression. */
2052 default_function_array_conversion (location_t loc, struct c_expr exp)
2054 tree orig_exp = exp.value;
2055 tree type = TREE_TYPE (exp.value);
2056 enum tree_code code = TREE_CODE (type);
2062 bool not_lvalue = false;
2063 bool lvalue_array_p;
2065 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2066 || CONVERT_EXPR_P (exp.value))
2067 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2069 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2071 exp.value = TREE_OPERAND (exp.value, 0);
2074 copy_warning (exp.value, orig_exp);
2076 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2077 if (!flag_isoc99 && !lvalue_array_p)
2079 /* Before C99, non-lvalue arrays do not decay to pointers.
2080 Normally, using such an array would be invalid; but it can
2081 be used correctly inside sizeof or as a statement expression.
2082 Thus, do not give an error here; an error will result later. */
2086 exp.value = array_to_pointer_conversion (loc, exp.value);
2090 exp.value = function_to_pointer_conversion (loc, exp.value);
2100 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2102 mark_exp_read (exp.value);
2103 return default_function_array_conversion (loc, exp);
2106 /* Return whether EXPR should be treated as an atomic lvalue for the
2107 purposes of load and store handling. */
2110 really_atomic_lvalue (tree expr)
2112 if (error_operand_p (expr))
2114 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2116 if (!lvalue_p (expr))
2119 /* Ignore _Atomic on register variables, since their addresses can't
2120 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2121 sequences wouldn't work. Ignore _Atomic on structures containing
2122 bit-fields, since accessing elements of atomic structures or
2123 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2124 it's undefined at translation time or execution time, and the
2125 normal atomic sequences again wouldn't work. */
2126 while (handled_component_p (expr))
2128 if (TREE_CODE (expr) == COMPONENT_REF
2129 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2131 expr = TREE_OPERAND (expr, 0);
2133 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2138 /* If EXPR is a named constant (C2x) derived from a constexpr variable
2139 - that is, a reference to such a variable, or a member extracted by
2140 a sequence of structure and union (but not array) member accesses
2141 (where union member accesses must access the same member as
2142 initialized) - then return the corresponding initializer;
2143 otherwise, return NULL_TREE. */
2146 maybe_get_constexpr_init (tree expr)
2148 tree decl = NULL_TREE;
2149 if (TREE_CODE (expr) == VAR_DECL)
2151 else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2152 decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2154 && C_DECL_DECLARED_CONSTEXPR (decl)
2155 && DECL_INITIAL (decl) != NULL_TREE
2156 && !error_operand_p (DECL_INITIAL (decl)))
2157 return DECL_INITIAL (decl);
2158 if (TREE_CODE (expr) != COMPONENT_REF)
2160 tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2161 if (inner == NULL_TREE)
2163 while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2164 && !error_operand_p (inner)
2165 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2166 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2167 inner = TREE_OPERAND (inner, 0);
2168 if (TREE_CODE (inner) != CONSTRUCTOR)
2170 tree field = TREE_OPERAND (expr, 1);
2171 unsigned HOST_WIDE_INT cidx;
2172 tree cfield, cvalue;
2173 bool have_other_init = false;
2174 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2176 if (cfield == field)
2178 have_other_init = true;
2180 if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2181 && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2183 /* Return a default initializer. */
2184 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2185 return build_constructor (TREE_TYPE (expr), NULL);
2186 return build_zero_cst (TREE_TYPE (expr));
2189 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2190 including converting functions and arrays to pointers if CONVERT_P.
2191 If READ_P, also mark the expression as having been read. If
2192 FOR_INIT, constexpr expressions of structure and union type should
2193 be replaced by the corresponding CONSTRUCTOR; otherwise, only
2194 constexpr scalars (including elements of structures and unions) are
2195 replaced by their initializers. */
2198 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2199 bool convert_p, bool read_p, bool for_init)
2201 bool force_non_npc = false;
2203 mark_exp_read (exp.value);
2205 exp = default_function_array_conversion (loc, exp);
2206 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2207 exp.value = require_complete_type (loc, exp.value);
2208 if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2210 tree init = maybe_get_constexpr_init (exp.value);
2211 if (init != NULL_TREE)
2213 /* A named constant of pointer type or type nullptr_t is not
2214 a null pointer constant even if the initializer is
2216 if (TREE_CODE (init) == INTEGER_CST
2217 && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2218 && integer_zerop (init))
2219 force_non_npc = true;
2223 if (really_atomic_lvalue (exp.value))
2225 vec<tree, va_gc> *params;
2226 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2227 tree expr_type = TREE_TYPE (exp.value);
2228 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2229 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2231 gcc_assert (TYPE_ATOMIC (expr_type));
2233 /* Expansion of a generic atomic load may require an addition
2234 element, so allocate enough to prevent a resize. */
2235 vec_alloc (params, 4);
2237 /* Remove the qualifiers for the rest of the expressions and
2238 create the VAL temp variable to hold the RHS. */
2239 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2240 tmp = create_tmp_var_raw (nonatomic_type);
2241 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2242 TREE_ADDRESSABLE (tmp) = 1;
2243 /* Do not disable warnings for TMP even though it's artificial.
2244 -Winvalid-memory-model depends on it. */
2246 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2247 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2248 params->quick_push (expr_addr);
2249 params->quick_push (tmp_addr);
2250 params->quick_push (seq_cst);
2251 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2253 /* EXPR is always read. */
2254 mark_exp_read (exp.value);
2256 /* Return tmp which contains the value loaded. */
2257 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2258 NULL_TREE, NULL_TREE);
2260 if (convert_p && !error_operand_p (exp.value)
2261 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2262 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2264 exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2268 /* EXP is an expression of integer type. Apply the integer promotions
2269 to it and return the promoted value. */
2272 perform_integral_promotions (tree exp)
2274 tree type = TREE_TYPE (exp);
2275 enum tree_code code = TREE_CODE (type);
2277 gcc_assert (INTEGRAL_TYPE_P (type));
2279 /* Convert enums to the result of applying the integer promotions to
2280 their underlying type. */
2281 if (code == ENUMERAL_TYPE)
2283 type = ENUM_UNDERLYING_TYPE (type);
2284 if (c_promoting_integer_type_p (type))
2286 if (TYPE_UNSIGNED (type)
2287 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2288 type = unsigned_type_node;
2290 type = integer_type_node;
2293 return convert (type, exp);
2296 /* ??? This should no longer be needed now bit-fields have their
2298 if (TREE_CODE (exp) == COMPONENT_REF
2299 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2300 /* If it's thinner than an int, promote it like a
2301 c_promoting_integer_type_p, otherwise leave it alone. */
2302 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2303 TYPE_PRECISION (integer_type_node)) < 0)
2304 return convert (integer_type_node, exp);
2306 if (c_promoting_integer_type_p (type))
2308 /* Preserve unsignedness if not really getting any wider. */
2309 if (TYPE_UNSIGNED (type)
2310 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2311 return convert (unsigned_type_node, exp);
2313 return convert (integer_type_node, exp);
2320 /* Perform default promotions for C data used in expressions.
2321 Enumeral types or short or char are converted to int.
2322 In addition, manifest constants symbols are replaced by their values. */
2325 default_conversion (tree exp)
2328 tree type = TREE_TYPE (exp);
2329 enum tree_code code = TREE_CODE (type);
2332 mark_exp_read (exp);
2334 /* Functions and arrays have been converted during parsing. */
2335 gcc_assert (code != FUNCTION_TYPE);
2336 if (code == ARRAY_TYPE)
2339 /* Constants can be used directly unless they're not loadable. */
2340 if (TREE_CODE (exp) == CONST_DECL)
2341 exp = DECL_INITIAL (exp);
2343 /* Strip no-op conversions. */
2345 STRIP_TYPE_NOPS (exp);
2347 copy_warning (exp, orig_exp);
2349 if (code == VOID_TYPE)
2351 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2352 "void value not ignored as it ought to be");
2353 return error_mark_node;
2356 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2357 if (exp == error_mark_node)
2358 return error_mark_node;
2360 promoted_type = targetm.promoted_type (type);
2362 return convert (promoted_type, exp);
2364 if (INTEGRAL_TYPE_P (type))
2365 return perform_integral_promotions (exp);
2370 /* Look up COMPONENT in a structure or union TYPE.
2372 If the component name is not found, returns NULL_TREE. Otherwise,
2373 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2374 stepping down the chain to the component, which is in the last
2375 TREE_VALUE of the list. Normally the list is of length one, but if
2376 the component is embedded within (nested) anonymous structures or
2377 unions, the list steps down the chain to the component. */
2380 lookup_field (tree type, tree component)
2384 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2385 to the field elements. Use a binary search on this array to quickly
2386 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2387 will always be set for structures which have many elements.
2389 Duplicate field checking replaces duplicates with NULL_TREE so
2390 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2391 case just iterate using DECL_CHAIN. */
2393 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2397 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2399 field = TYPE_FIELDS (type);
2401 top = TYPE_LANG_SPECIFIC (type)->s->len;
2402 while (top - bot > 1)
2404 half = (top - bot + 1) >> 1;
2405 field = field_array[bot+half];
2407 if (DECL_NAME (field) == NULL_TREE)
2409 /* Step through all anon unions in linear fashion. */
2410 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2412 field = field_array[bot++];
2413 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2415 tree anon = lookup_field (TREE_TYPE (field), component);
2418 return tree_cons (NULL_TREE, field, anon);
2420 /* The Plan 9 compiler permits referring
2421 directly to an anonymous struct/union field
2422 using a typedef name. */
2423 if (flag_plan9_extensions
2424 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2425 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2427 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2433 /* Entire record is only anon unions. */
2437 /* Restart the binary search, with new lower bound. */
2441 if (DECL_NAME (field) == component)
2443 if (DECL_NAME (field) < component)
2449 if (DECL_NAME (field_array[bot]) == component)
2450 field = field_array[bot];
2451 else if (DECL_NAME (field) != component)
2456 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2458 if (DECL_NAME (field) == NULL_TREE
2459 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2461 tree anon = lookup_field (TREE_TYPE (field), component);
2464 return tree_cons (NULL_TREE, field, anon);
2466 /* The Plan 9 compiler permits referring directly to an
2467 anonymous struct/union field using a typedef
2469 if (flag_plan9_extensions
2470 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2471 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2472 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2477 if (DECL_NAME (field) == component)
2481 if (field == NULL_TREE)
2485 return tree_cons (NULL_TREE, field, NULL_TREE);
2488 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2491 lookup_field_fuzzy_find_candidates (tree type, tree component,
2492 vec<tree> *candidates)
2495 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2497 if (DECL_NAME (field) == NULL_TREE
2498 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2499 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2502 if (DECL_NAME (field))
2503 candidates->safe_push (DECL_NAME (field));
2507 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2508 rather than returning a TREE_LIST for an exact match. */
2511 lookup_field_fuzzy (tree type, tree component)
2513 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2515 /* First, gather a list of candidates. */
2516 auto_vec <tree> candidates;
2518 lookup_field_fuzzy_find_candidates (type, component,
2521 return find_closest_identifier (component, &candidates);
2524 /* Support function for build_component_ref's error-handling.
2526 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2527 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2530 should_suggest_deref_p (tree datum_type)
2532 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2533 allows "." for ptrs; we could be handling a failed attempt
2534 to access a property. */
2535 if (c_dialect_objc ())
2538 /* Only suggest it for pointers... */
2539 if (TREE_CODE (datum_type) != POINTER_TYPE)
2542 /* ...to structs/unions. */
2543 tree underlying_type = TREE_TYPE (datum_type);
2544 enum tree_code code = TREE_CODE (underlying_type);
2545 if (code == RECORD_TYPE || code == UNION_TYPE)
2551 /* Make an expression to refer to the COMPONENT field of structure or
2552 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2553 location of the COMPONENT_REF. COMPONENT_LOC is the location
2554 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2555 it is from -> operator. */
2558 build_component_ref (location_t loc, tree datum, tree component,
2559 location_t component_loc, location_t arrow_loc)
2561 tree type = TREE_TYPE (datum);
2562 enum tree_code code = TREE_CODE (type);
2565 bool datum_lvalue = lvalue_p (datum);
2567 if (!objc_is_public (datum, component))
2568 return error_mark_node;
2570 /* Detect Objective-C property syntax object.property. */
2571 if (c_dialect_objc ()
2572 && (ref = objc_maybe_build_component_ref (datum, component)))
2575 /* See if there is a field or component with name COMPONENT. */
2577 if (code == RECORD_TYPE || code == UNION_TYPE)
2579 if (!COMPLETE_TYPE_P (type))
2581 c_incomplete_type_error (loc, NULL_TREE, type);
2582 return error_mark_node;
2585 field = lookup_field (type, component);
2589 tree guessed_id = lookup_field_fuzzy (type, component);
2592 /* Attempt to provide a fixit replacement hint, if
2593 we have a valid range for the component. */
2594 location_t reported_loc
2595 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2596 gcc_rich_location rich_loc (reported_loc);
2597 if (component_loc != UNKNOWN_LOCATION)
2598 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2599 error_at (&rich_loc,
2600 "%qT has no member named %qE; did you mean %qE?",
2601 type, component, guessed_id);
2604 error_at (loc, "%qT has no member named %qE", type, component);
2605 return error_mark_node;
2608 /* Accessing elements of atomic structures or unions is undefined
2609 behavior (C11 6.5.2.3#5). */
2610 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2612 if (code == RECORD_TYPE)
2613 warning_at (loc, 0, "accessing a member %qE of an atomic "
2614 "structure %qE", component, datum);
2616 warning_at (loc, 0, "accessing a member %qE of an atomic "
2617 "union %qE", component, datum);
2620 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2621 This might be better solved in future the way the C++ front
2622 end does it - by giving the anonymous entities each a
2623 separate name and type, and then have build_component_ref
2624 recursively call itself. We can't do that here. */
2627 tree subdatum = TREE_VALUE (field);
2630 bool use_datum_quals;
2632 if (TREE_TYPE (subdatum) == error_mark_node)
2633 return error_mark_node;
2635 /* If this is an rvalue, it does not have qualifiers in C
2636 standard terms and we must avoid propagating such
2637 qualifiers down to a non-lvalue array that is then
2638 converted to a pointer. */
2639 use_datum_quals = (datum_lvalue
2640 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2642 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2643 if (use_datum_quals)
2644 quals |= TYPE_QUALS (TREE_TYPE (datum));
2645 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2647 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2649 SET_EXPR_LOCATION (ref, loc);
2650 if (TREE_READONLY (subdatum)
2651 || (use_datum_quals && TREE_READONLY (datum)))
2652 TREE_READONLY (ref) = 1;
2653 if (TREE_THIS_VOLATILE (subdatum)
2654 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2655 TREE_THIS_VOLATILE (ref) = 1;
2657 if (TREE_UNAVAILABLE (subdatum))
2658 error_unavailable_use (subdatum, NULL_TREE);
2659 else if (TREE_DEPRECATED (subdatum))
2660 warn_deprecated_use (subdatum, NULL_TREE);
2664 field = TREE_CHAIN (field);
2670 else if (should_suggest_deref_p (type))
2672 /* Special-case the error message for "ptr.field" for the case
2673 where the user has confused "." vs "->". */
2674 rich_location richloc (line_table, loc);
2675 if (TREE_CODE (datum) == INDIRECT_REF && arrow_loc != UNKNOWN_LOCATION)
2677 richloc.add_fixit_insert_before (arrow_loc, "(*");
2678 richloc.add_fixit_insert_after (arrow_loc, ")");
2680 "%qE is a pointer to pointer; did you mean to dereference "
2681 "it before applying %<->%> to it?",
2682 TREE_OPERAND (datum, 0));
2686 /* "loc" should be the "." token. */
2687 richloc.add_fixit_replace ("->");
2689 "%qE is a pointer; did you mean to use %<->%>?",
2692 return error_mark_node;
2694 else if (code != ERROR_MARK)
2696 "request for member %qE in something not a structure or union",
2699 return error_mark_node;
2702 /* Given an expression PTR for a pointer, return an expression
2703 for the value pointed to.
2704 ERRORSTRING is the name of the operator to appear in error messages.
2706 LOC is the location to use for the generated tree. */
2709 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2711 tree pointer = default_conversion (ptr);
2712 tree type = TREE_TYPE (pointer);
2715 if (TREE_CODE (type) == POINTER_TYPE)
2717 if (CONVERT_EXPR_P (pointer)
2718 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2720 /* If a warning is issued, mark it to avoid duplicates from
2721 the backend. This only needs to be done at
2722 warn_strict_aliasing > 2. */
2723 if (warn_strict_aliasing > 2)
2724 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2725 type, TREE_OPERAND (pointer, 0)))
2726 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2729 if (TREE_CODE (pointer) == ADDR_EXPR
2730 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2731 == TREE_TYPE (type)))
2733 ref = TREE_OPERAND (pointer, 0);
2734 protected_set_expr_location (ref, loc);
2739 tree t = TREE_TYPE (type);
2741 ref = build1 (INDIRECT_REF, t, pointer);
2743 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2744 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2746 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2747 so that we get the proper error message if the result is used
2748 to assign to. Also, &* is supposed to be a no-op.
2749 And ANSI C seems to specify that the type of the result
2750 should be the const type. */
2751 /* A de-reference of a pointer to const is not a const. It is valid
2752 to change it via some other pointer. */
2753 TREE_READONLY (ref) = TYPE_READONLY (t);
2754 TREE_SIDE_EFFECTS (ref)
2755 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2756 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2757 protected_set_expr_location (ref, loc);
2761 else if (TREE_CODE (pointer) != ERROR_MARK)
2762 invalid_indirection_error (loc, type, errstring);
2764 return error_mark_node;
2767 /* This handles expressions of the form "a[i]", which denotes
2770 This is logically equivalent in C to *(a+i), but we may do it differently.
2771 If A is a variable or a member, we generate a primitive ARRAY_REF.
2772 This avoids forcing the array out of registers, and can work on
2773 arrays that are not lvalues (for example, members of structures returned
2776 For vector types, allow vector[i] but not i[vector], and create
2777 *(((type*)&vectortype) + i) for the expression.
2779 LOC is the location to use for the returned expression. */
2782 build_array_ref (location_t loc, tree array, tree index)
2785 bool swapped = false;
2786 if (TREE_TYPE (array) == error_mark_node
2787 || TREE_TYPE (index) == error_mark_node)
2788 return error_mark_node;
2790 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2791 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2792 /* Allow vector[index] but not index[vector]. */
2793 && !gnu_vector_type_p (TREE_TYPE (array)))
2795 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2796 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2799 "subscripted value is neither array nor pointer nor vector");
2801 return error_mark_node;
2803 std::swap (array, index);
2807 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2809 error_at (loc, "array subscript is not an integer");
2810 return error_mark_node;
2813 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2815 error_at (loc, "subscripted value is pointer to function");
2816 return error_mark_node;
2819 /* ??? Existing practice has been to warn only when the char
2820 index is syntactically the index, not for char[array]. */
2822 warn_array_subscript_with_type_char (loc, index);
2824 /* Apply default promotions *after* noticing character types. */
2825 index = default_conversion (index);
2826 if (index == error_mark_node)
2827 return error_mark_node;
2829 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2831 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2832 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2834 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2838 /* An array that is indexed by a non-constant
2839 cannot be stored in a register; we must be able to do
2840 address arithmetic on its address.
2841 Likewise an array of elements of variable size. */
2842 if (TREE_CODE (index) != INTEGER_CST
2843 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2844 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2846 if (!c_mark_addressable (array, true))
2847 return error_mark_node;
2849 /* An array that is indexed by a constant value which is not within
2850 the array bounds cannot be stored in a register either; because we
2851 would get a crash in store_bit_field/extract_bit_field when trying
2852 to access a non-existent part of the register. */
2853 if (TREE_CODE (index) == INTEGER_CST
2854 && TYPE_DOMAIN (TREE_TYPE (array))
2855 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2857 if (!c_mark_addressable (array))
2858 return error_mark_node;
2861 if ((pedantic || warn_c90_c99_compat)
2865 while (TREE_CODE (foo) == COMPONENT_REF)
2866 foo = TREE_OPERAND (foo, 0);
2867 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2868 pedwarn (loc, OPT_Wpedantic,
2869 "ISO C forbids subscripting %<register%> array");
2870 else if (!lvalue_p (foo))
2871 pedwarn_c90 (loc, OPT_Wpedantic,
2872 "ISO C90 forbids subscripting non-lvalue "
2876 type = TREE_TYPE (TREE_TYPE (array));
2877 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2878 /* Array ref is const/volatile if the array elements are
2879 or if the array is. */
2880 TREE_READONLY (rval)
2881 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2882 | TREE_READONLY (array));
2883 TREE_SIDE_EFFECTS (rval)
2884 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2885 | TREE_SIDE_EFFECTS (array));
2886 TREE_THIS_VOLATILE (rval)
2887 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2888 /* This was added by rms on 16 Nov 91.
2889 It fixes vol struct foo *a; a->elts[1]
2890 in an inline function.
2891 Hope it doesn't break something else. */
2892 | TREE_THIS_VOLATILE (array));
2893 ret = require_complete_type (loc, rval);
2894 protected_set_expr_location (ret, loc);
2896 ret = non_lvalue_loc (loc, ret);
2901 tree ar = default_conversion (array);
2903 if (ar == error_mark_node)
2906 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2907 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2909 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2913 ret = non_lvalue_loc (loc, ret);
2918 /* Build an external reference to identifier ID. FUN indicates
2919 whether this will be used for a function call. LOC is the source
2920 location of the identifier. This sets *TYPE to the type of the
2921 identifier, which is not the same as the type of the returned value
2922 for CONST_DECLs defined as enum constants. If the type of the
2923 identifier is not available, *TYPE is set to NULL. */
2925 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2928 tree decl = lookup_name (id);
2930 /* In Objective-C, an instance variable (ivar) may be preferred to
2931 whatever lookup_name() found. */
2932 decl = objc_lookup_ivar (decl, id);
2935 if (decl && decl != error_mark_node)
2938 *type = TREE_TYPE (ref);
2939 if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
2940 error_at (loc, "underspecified %qD referenced in its initializer",
2944 /* Implicit function declaration. */
2945 ref = implicitly_declare (loc, id);
2946 else if (decl == error_mark_node)
2947 /* Don't complain about something that's already been
2948 complained about. */
2949 return error_mark_node;
2952 undeclared_variable (loc, id);
2953 return error_mark_node;
2956 if (TREE_TYPE (ref) == error_mark_node)
2957 return error_mark_node;
2959 if (TREE_UNAVAILABLE (ref))
2960 error_unavailable_use (ref, NULL_TREE);
2961 else if (TREE_DEPRECATED (ref))
2962 warn_deprecated_use (ref, NULL_TREE);
2964 /* Recursive call does not count as usage. */
2965 if (ref != current_function_decl)
2967 TREE_USED (ref) = 1;
2970 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2972 if (!in_sizeof && !in_typeof)
2973 C_DECL_USED (ref) = 1;
2974 else if (DECL_INITIAL (ref) == NULL_TREE
2975 && DECL_EXTERNAL (ref)
2976 && !TREE_PUBLIC (ref))
2977 record_maybe_used_decl (ref);
2980 if (TREE_CODE (ref) == CONST_DECL)
2982 used_types_insert (TREE_TYPE (ref));
2985 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2986 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2988 warning_at (loc, OPT_Wc___compat,
2989 ("enum constant defined in struct or union "
2990 "is not visible in C++"));
2991 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2994 ref = DECL_INITIAL (ref);
2995 TREE_CONSTANT (ref) = 1;
2997 else if (current_function_decl != NULL_TREE
2998 && !DECL_FILE_SCOPE_P (current_function_decl)
2999 && (VAR_OR_FUNCTION_DECL_P (ref)
3000 || TREE_CODE (ref) == PARM_DECL))
3002 tree context = decl_function_context (ref);
3004 if (context != NULL_TREE && context != current_function_decl)
3005 DECL_NONLOCAL (ref) = 1;
3007 /* C99 6.7.4p3: An inline definition of a function with external
3008 linkage ... shall not contain a reference to an identifier with
3009 internal linkage. */
3010 else if (current_function_decl != NULL_TREE
3011 && DECL_DECLARED_INLINE_P (current_function_decl)
3012 && DECL_EXTERNAL (current_function_decl)
3013 && VAR_OR_FUNCTION_DECL_P (ref)
3014 && (!VAR_P (ref) || TREE_STATIC (ref))
3015 && ! TREE_PUBLIC (ref)
3016 && DECL_CONTEXT (ref) != current_function_decl)
3017 record_inline_static (loc, current_function_decl, ref,
3023 /* Record details of decls possibly used inside sizeof or typeof. */
3024 struct maybe_used_decl
3028 /* The level seen at (in_sizeof + in_typeof). */
3030 /* The next one at this level or above, or NULL. */
3031 struct maybe_used_decl *next;
3034 static struct maybe_used_decl *maybe_used_decls;
3036 /* Record that DECL, an undefined static function reference seen
3037 inside sizeof or typeof, might be used if the operand of sizeof is
3038 a VLA type or the operand of typeof is a variably modified
3042 record_maybe_used_decl (tree decl)
3044 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3046 t->level = in_sizeof + in_typeof;
3047 t->next = maybe_used_decls;
3048 maybe_used_decls = t;
3051 /* Pop the stack of decls possibly used inside sizeof or typeof. If
3052 USED is false, just discard them. If it is true, mark them used
3053 (if no longer inside sizeof or typeof) or move them to the next
3054 level up (if still inside sizeof or typeof). */
3057 pop_maybe_used (bool used)
3059 struct maybe_used_decl *p = maybe_used_decls;
3060 int cur_level = in_sizeof + in_typeof;
3061 while (p && p->level > cur_level)
3066 C_DECL_USED (p->decl) = 1;
3068 p->level = cur_level;
3072 if (!used || cur_level == 0)
3073 maybe_used_decls = p;
3076 /* Return the result of sizeof applied to EXPR. */
3079 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3082 if (expr.value == error_mark_node)
3084 ret.value = error_mark_node;
3085 ret.original_code = ERROR_MARK;
3086 ret.original_type = NULL;
3088 pop_maybe_used (false);
3092 bool expr_const_operands = true;
3094 if (TREE_CODE (expr.value) == PARM_DECL
3095 && C_ARRAY_PARAMETER (expr.value))
3097 auto_diagnostic_group d;
3098 if (warning_at (loc, OPT_Wsizeof_array_argument,
3099 "%<sizeof%> on array function parameter %qE will "
3100 "return size of %qT", expr.value,
3101 TREE_TYPE (expr.value)))
3102 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3104 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3105 &expr_const_operands);
3106 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3107 c_last_sizeof_arg = expr.value;
3108 c_last_sizeof_loc = loc;
3109 ret.original_code = SIZEOF_EXPR;
3110 ret.original_type = NULL;
3112 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3114 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3115 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3116 folded_expr, ret.value);
3117 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3118 SET_EXPR_LOCATION (ret.value, loc);
3120 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3125 /* Return the result of sizeof applied to T, a structure for the type
3126 name passed to sizeof (rather than the type itself). LOC is the
3127 location of the original expression. */
3130 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3134 tree type_expr = NULL_TREE;
3135 bool type_expr_const = true;
3136 type = groktypename (t, &type_expr, &type_expr_const);
3137 ret.value = c_sizeof (loc, type);
3138 c_last_sizeof_arg = type;
3139 c_last_sizeof_loc = loc;
3140 ret.original_code = SIZEOF_EXPR;
3141 ret.original_type = NULL;
3143 if (type == error_mark_node)
3145 ret.value = error_mark_node;
3146 ret.original_code = ERROR_MARK;
3149 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3150 && C_TYPE_VARIABLE_SIZE (type))
3152 /* If the type is a [*] array, it is a VLA but is represented as
3153 having a size of zero. In such a case we must ensure that
3154 the result of sizeof does not get folded to a constant by
3155 c_fully_fold, because if the size is evaluated the result is
3156 not constant and so constraints on zero or negative size
3157 arrays must not be applied when this sizeof call is inside
3158 another array declarator. */
3160 type_expr = integer_zero_node;
3161 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3162 type_expr, ret.value);
3163 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3165 pop_maybe_used (type != error_mark_node
3166 ? C_TYPE_VARIABLE_SIZE (type) : false);
3170 /* Build a function call to function FUNCTION with parameters PARAMS.
3171 The function call is at LOC.
3172 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3173 TREE_VALUE of each node is a parameter-expression.
3174 FUNCTION's data type may be a function type or a pointer-to-function. */
3177 build_function_call (location_t loc, tree function, tree params)
3179 vec<tree, va_gc> *v;
3182 vec_alloc (v, list_length (params));
3183 for (; params; params = TREE_CHAIN (params))
3184 v->quick_push (TREE_VALUE (params));
3185 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3190 /* Give a note about the location of the declaration of DECL. */
3193 inform_declaration (tree decl)
3195 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3196 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3197 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3200 /* Build a function call to function FUNCTION with parameters PARAMS.
3201 If FUNCTION is the result of resolving an overloaded target built-in,
3202 ORIG_FUNDECL is the original function decl, otherwise it is null.
3203 ORIGTYPES, if not NULL, is a vector of types; each element is
3204 either NULL or the original type of the corresponding element in
3205 PARAMS. The original type may differ from TREE_TYPE of the
3206 parameter for enums. FUNCTION's data type may be a function type
3207 or pointer-to-function. This function changes the elements of
3211 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3212 tree function, vec<tree, va_gc> *params,
3213 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3215 tree fntype, fundecl = NULL_TREE;
3216 tree name = NULL_TREE, result;
3222 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3223 STRIP_TYPE_NOPS (function);
3225 /* Convert anything with function type to a pointer-to-function. */
3226 if (TREE_CODE (function) == FUNCTION_DECL)
3228 name = DECL_NAME (function);
3231 tm_malloc_replacement (function);
3234 orig_fundecl = fundecl;
3235 /* Atomic functions have type checking/casting already done. They are
3236 often rewritten and don't match the original parameter list. */
3237 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3240 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3241 function = function_to_pointer_conversion (loc, function);
3243 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3244 expressions, like those used for ObjC messenger dispatches. */
3245 if (params && !params->is_empty ())
3246 function = objc_rewrite_function_call (function, (*params)[0]);
3248 function = c_fully_fold (function, false, NULL);
3250 fntype = TREE_TYPE (function);
3252 if (TREE_CODE (fntype) == ERROR_MARK)
3253 return error_mark_node;
3255 if (!(TREE_CODE (fntype) == POINTER_TYPE
3256 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3258 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3260 "called object %qE is not a function or function pointer",
3262 else if (DECL_P (function))
3265 "called object %qD is not a function or function pointer",
3267 inform_declaration (function);
3271 "called object is not a function or function pointer");
3272 return error_mark_node;
3275 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3276 current_function_returns_abnormally = 1;
3278 /* fntype now gets the type of function pointed to. */
3279 fntype = TREE_TYPE (fntype);
3280 tree return_type = TREE_TYPE (fntype);
3282 /* Convert the parameters to the types declared in the
3283 function prototype, or apply default promotions. */
3285 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3286 origtypes, function, fundecl);
3288 return error_mark_node;
3290 /* Check that the function is called through a compatible prototype.
3291 If it is not, warn. */
3292 if (CONVERT_EXPR_P (function)
3293 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3294 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3295 && !comptypes (fntype, TREE_TYPE (tem)))
3297 /* This situation leads to run-time undefined behavior. We can't,
3298 therefore, simply error unless we can prove that all possible
3299 executions of the program must execute the code. */
3300 warning_at (loc, 0, "function called through a non-compatible type");
3302 if (VOID_TYPE_P (return_type)
3303 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3305 "function with qualified void return type called");
3308 argarray = vec_safe_address (params);
3310 /* Check that arguments to builtin functions match the expectations. */
3312 && fndecl_built_in_p (fundecl)
3313 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3314 orig_fundecl, nargs, argarray))
3315 return error_mark_node;
3317 /* Check that the arguments to the function are valid. */
3318 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3319 nargs, argarray, &arg_loc);
3321 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
3322 && !VOID_TYPE_P (return_type))
3323 return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
3324 if (name != NULL_TREE
3325 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3327 if (require_constant_value)
3329 = fold_build_call_array_initializer_loc (loc, return_type,
3330 function, nargs, argarray);
3332 result = fold_build_call_array_loc (loc, return_type,
3333 function, nargs, argarray);
3334 if (TREE_CODE (result) == NOP_EXPR
3335 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3336 STRIP_TYPE_NOPS (result);
3339 result = build_call_array_loc (loc, return_type,
3340 function, nargs, argarray);
3341 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3343 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3344 suppress_warning (result, OPT_Wnonnull);
3346 /* In this improbable scenario, a nested function returns a VM type.
3347 Create a TARGET_EXPR so that the call always has a LHS, much as
3348 what the C++ FE does for functions returning non-PODs. */
3349 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3351 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3352 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3353 NULL_TREE, NULL_TREE);
3356 if (VOID_TYPE_P (TREE_TYPE (result)))
3358 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3360 "function with qualified void return type called");
3363 return require_complete_type (loc, result);
3366 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3369 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3370 tree function, vec<tree, va_gc> *params,
3371 vec<tree, va_gc> *origtypes)
3373 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3374 STRIP_TYPE_NOPS (function);
3376 /* Convert anything with function type to a pointer-to-function. */
3377 if (TREE_CODE (function) == FUNCTION_DECL)
3379 /* Implement type-directed function overloading for builtins.
3380 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3381 handle all the type checking. The result is a complete expression
3382 that implements this function call. */
3383 tree tem = resolve_overloaded_builtin (loc, function, params);
3387 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3390 /* Helper for convert_arguments called to convert the VALue of argument
3391 number ARGNUM from ORIGTYPE to the corresponding parameter number
3393 PLOC is the location where the conversion is being performed.
3394 FUNCTION and FUNDECL are the same as in convert_arguments.
3395 VALTYPE is the original type of VAL before the conversion and,
3396 for EXCESS_PRECISION_EXPR, the operand of the expression.
3397 NPC is true if VAL represents the null pointer constant (VAL itself
3398 will have been folded to an integer constant).
3399 RNAME is the same as FUNCTION except in Objective C when it's
3400 the function selector.
3401 EXCESS_PRECISION is true when VAL was originally represented
3402 as EXCESS_PRECISION_EXPR.
3403 WARNOPT is the same as in convert_for_assignment. */
3406 convert_argument (location_t ploc, tree function, tree fundecl,
3407 tree type, tree origtype, tree val, tree valtype,
3408 bool npc, tree rname, int parmnum, int argnum,
3409 bool excess_precision, int warnopt)
3411 /* Formal parm type is specified by a function prototype. */
3413 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3415 error_at (ploc, "type of formal parameter %d is incomplete",
3420 /* Optionally warn about conversions that differ from the default
3422 if (warn_traditional_conversion || warn_traditional)
3424 unsigned int formal_prec = TYPE_PRECISION (type);
3426 if (INTEGRAL_TYPE_P (type)
3427 && TREE_CODE (valtype) == REAL_TYPE)
3428 warning_at (ploc, OPT_Wtraditional_conversion,
3429 "passing argument %d of %qE as integer rather "
3430 "than floating due to prototype",
3432 if (INTEGRAL_TYPE_P (type)
3433 && TREE_CODE (valtype) == COMPLEX_TYPE)
3434 warning_at (ploc, OPT_Wtraditional_conversion,
3435 "passing argument %d of %qE as integer rather "
3436 "than complex due to prototype",
3438 else if (TREE_CODE (type) == COMPLEX_TYPE
3439 && TREE_CODE (valtype) == REAL_TYPE)
3440 warning_at (ploc, OPT_Wtraditional_conversion,
3441 "passing argument %d of %qE as complex rather "
3442 "than floating due to prototype",
3444 else if (TREE_CODE (type) == REAL_TYPE
3445 && INTEGRAL_TYPE_P (valtype))
3446 warning_at (ploc, OPT_Wtraditional_conversion,
3447 "passing argument %d of %qE as floating rather "
3448 "than integer due to prototype",
3450 else if (TREE_CODE (type) == COMPLEX_TYPE
3451 && INTEGRAL_TYPE_P (valtype))
3452 warning_at (ploc, OPT_Wtraditional_conversion,
3453 "passing argument %d of %qE as complex rather "
3454 "than integer due to prototype",
3456 else if (TREE_CODE (type) == REAL_TYPE
3457 && TREE_CODE (valtype) == COMPLEX_TYPE)
3458 warning_at (ploc, OPT_Wtraditional_conversion,
3459 "passing argument %d of %qE as floating rather "
3460 "than complex due to prototype",
3462 /* ??? At some point, messages should be written about
3463 conversions between complex types, but that's too messy
3465 else if (TREE_CODE (type) == REAL_TYPE
3466 && TREE_CODE (valtype) == REAL_TYPE)
3468 /* Warn if any argument is passed as `float',
3469 since without a prototype it would be `double'. */
3470 if (formal_prec == TYPE_PRECISION (float_type_node)
3471 && type != dfloat32_type_node)
3472 warning_at (ploc, 0,
3473 "passing argument %d of %qE as %<float%> "
3474 "rather than %<double%> due to prototype",
3477 /* Warn if mismatch between argument and prototype
3478 for decimal float types. Warn of conversions with
3479 binary float types and of precision narrowing due to
3481 else if (type != valtype
3482 && (type == dfloat32_type_node
3483 || type == dfloat64_type_node
3484 || type == dfloat128_type_node
3485 || valtype == dfloat32_type_node
3486 || valtype == dfloat64_type_node
3487 || valtype == dfloat128_type_node)
3489 <= TYPE_PRECISION (valtype)
3490 || (type == dfloat128_type_node
3492 != dfloat64_type_node
3494 != dfloat32_type_node)))
3495 || (type == dfloat64_type_node
3497 != dfloat32_type_node))))
3498 warning_at (ploc, 0,
3499 "passing argument %d of %qE as %qT "
3500 "rather than %qT due to prototype",
3501 argnum, rname, type, valtype);
3504 /* Detect integer changing in width or signedness.
3505 These warnings are only activated with
3506 -Wtraditional-conversion, not with -Wtraditional. */
3507 else if (warn_traditional_conversion
3508 && INTEGRAL_TYPE_P (type)
3509 && INTEGRAL_TYPE_P (valtype))
3511 tree would_have_been = default_conversion (val);
3512 tree type1 = TREE_TYPE (would_have_been);
3514 if (val == error_mark_node)
3515 /* VAL could have been of incomplete type. */;
3516 else if (TREE_CODE (type) == ENUMERAL_TYPE
3517 && (TYPE_MAIN_VARIANT (type)
3518 == TYPE_MAIN_VARIANT (valtype)))
3519 /* No warning if function asks for enum
3520 and the actual arg is that enum type. */
3522 else if (formal_prec != TYPE_PRECISION (type1))
3523 warning_at (ploc, OPT_Wtraditional_conversion,
3524 "passing argument %d of %qE "
3525 "with different width due to prototype",
3527 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3529 /* Don't complain if the formal parameter type
3530 is an enum, because we can't tell now whether
3531 the value was an enum--even the same enum. */
3532 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3534 else if (TREE_CODE (val) == INTEGER_CST
3535 && int_fits_type_p (val, type))
3536 /* Change in signedness doesn't matter
3537 if a constant value is unaffected. */
3539 /* If the value is extended from a narrower
3540 unsigned type, it doesn't matter whether we
3541 pass it as signed or unsigned; the value
3542 certainly is the same either way. */
3543 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3544 && TYPE_UNSIGNED (valtype))
3546 else if (TYPE_UNSIGNED (type))
3547 warning_at (ploc, OPT_Wtraditional_conversion,
3548 "passing argument %d of %qE "
3549 "as unsigned due to prototype",
3552 warning_at (ploc, OPT_Wtraditional_conversion,
3553 "passing argument %d of %qE "
3554 "as signed due to prototype",
3559 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3560 sake of better warnings from convert_and_check. */
3561 if (excess_precision)
3562 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3564 tree parmval = convert_for_assignment (ploc, ploc, type,
3565 val, origtype, ic_argpass,
3566 npc, fundecl, function,
3567 parmnum + 1, warnopt);
3569 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3570 && INTEGRAL_TYPE_P (type)
3571 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3572 parmval = default_conversion (parmval);
3577 /* Convert the argument expressions in the vector VALUES
3578 to the types in the list TYPELIST.
3580 If TYPELIST is exhausted, or when an element has NULL as its type,
3581 perform the default conversions.
3583 ORIGTYPES is the original types of the expressions in VALUES. This
3584 holds the type of enum values which have been converted to integral
3585 types. It may be NULL.
3587 FUNCTION is a tree for the called function. It is used only for
3588 error messages, where it is formatted with %qE.
3590 This is also where warnings about wrong number of args are generated.
3592 ARG_LOC are locations of function arguments (if any).
3594 Returns the actual number of arguments processed (which may be less
3595 than the length of VALUES in some error situations), or -1 on
3599 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3600 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3601 tree function, tree fundecl)
3603 unsigned int parmnum;
3604 bool error_args = false;
3605 const bool type_generic = fundecl
3606 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3607 bool type_generic_remove_excess_precision = false;
3608 bool type_generic_overflow_p = false;
3611 /* Change pointer to function to the function itself for
3613 if (TREE_CODE (function) == ADDR_EXPR
3614 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3615 function = TREE_OPERAND (function, 0);
3617 /* Handle an ObjC selector specially for diagnostics. */
3618 selector = objc_message_selector ();
3620 /* For a call to a built-in function declared without a prototype,
3621 set to the built-in function's argument list. */
3622 tree builtin_typelist = NULL_TREE;
3624 /* For type-generic built-in functions, determine whether excess
3625 precision should be removed (classification) or not
3628 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3630 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3631 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3633 /* For a call to a built-in function declared without a prototype
3634 use the types of the parameters of the internal built-in to
3635 match those of the arguments to. */
3636 if (tree bdecl = builtin_decl_explicit (code))
3637 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3640 /* For type-generic built-in functions, determine whether excess
3641 precision should be removed (classification) or not
3646 case BUILT_IN_ISFINITE:
3647 case BUILT_IN_ISINF:
3648 case BUILT_IN_ISINF_SIGN:
3649 case BUILT_IN_ISNAN:
3650 case BUILT_IN_ISNORMAL:
3651 case BUILT_IN_ISSIGNALING:
3652 case BUILT_IN_FPCLASSIFY:
3653 type_generic_remove_excess_precision = true;
3656 case BUILT_IN_ADD_OVERFLOW_P:
3657 case BUILT_IN_SUB_OVERFLOW_P:
3658 case BUILT_IN_MUL_OVERFLOW_P:
3659 /* The last argument of these type-generic builtins
3660 should not be promoted. */
3661 type_generic_overflow_p = true;
3669 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3670 individual converted arguments. */
3672 tree typetail, builtin_typetail, val;
3673 for (typetail = typelist,
3674 builtin_typetail = builtin_typelist,
3676 values && values->iterate (parmnum, &val);
3679 /* The type of the function parameter (if it was declared with one). */
3680 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3681 /* The type of the built-in function parameter (if the function
3682 is a built-in). Used to detect type incompatibilities in
3683 calls to built-ins declared without a prototype. */
3684 tree builtin_type = (builtin_typetail
3685 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3686 /* The original type of the argument being passed to the function. */
3687 tree valtype = TREE_TYPE (val);
3688 /* The called function (or function selector in Objective C). */
3689 tree rname = function;
3690 int argnum = parmnum + 1;
3691 const char *invalid_func_diag;
3692 /* Set for EXCESS_PRECISION_EXPR arguments. */
3693 bool excess_precision = false;
3694 /* The value of the argument after conversion to the type
3695 of the function parameter it is passed to. */
3697 /* Some __atomic_* builtins have additional hidden argument at
3700 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3701 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3704 if (type == void_type_node)
3707 error_at (loc, "too many arguments to method %qE", selector);
3709 error_at (loc, "too many arguments to function %qE", function);
3710 inform_declaration (fundecl);
3711 return error_args ? -1 : (int) parmnum;
3714 if (builtin_type == void_type_node)
3716 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3717 "too many arguments to built-in function %qE "
3718 "expecting %d", function, parmnum))
3719 inform_declaration (fundecl);
3720 builtin_typetail = NULL_TREE;
3723 if (selector && argnum > 2)
3729 /* Determine if VAL is a null pointer constant before folding it. */
3730 bool npc = null_pointer_constant_p (val);
3732 /* If there is excess precision and a prototype, convert once to
3733 the required type rather than converting via the semantic
3734 type. Likewise without a prototype a float value represented
3735 as long double should be converted once to double. But for
3736 type-generic classification functions excess precision must
3738 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3739 && (type || !type_generic || !type_generic_remove_excess_precision))
3741 val = TREE_OPERAND (val, 0);
3742 excess_precision = true;
3744 val = c_fully_fold (val, false, NULL);
3745 STRIP_TYPE_NOPS (val);
3747 val = require_complete_type (ploc, val);
3749 /* Some floating-point arguments must be promoted to double when
3750 no type is specified by a prototype. This applies to
3751 arguments of type float, and to architecture-specific types
3752 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3753 bool promote_float_arg = false;
3754 if (type == NULL_TREE
3755 && TREE_CODE (valtype) == REAL_TYPE
3756 && (TYPE_PRECISION (valtype)
3757 <= TYPE_PRECISION (double_type_node))
3758 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3759 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3760 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3762 /* Promote this argument, unless it has a _FloatN or
3764 promote_float_arg = true;
3765 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3766 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3768 promote_float_arg = false;
3771 /* Don't promote __bf16 either. */
3772 if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
3773 promote_float_arg = false;
3776 if (type != NULL_TREE)
3778 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3779 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3780 val, valtype, npc, rname, parmnum, argnum,
3781 excess_precision, 0);
3783 else if (promote_float_arg)
3789 /* Convert `float' to `double'. */
3790 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3791 warning_at (ploc, OPT_Wdouble_promotion,
3792 "implicit conversion from %qT to %qT when passing "
3793 "argument to function",
3794 valtype, double_type_node);
3795 parmval = convert (double_type_node, val);
3798 else if ((excess_precision && !type_generic)
3799 || (type_generic_overflow_p && parmnum == 2))
3800 /* A "double" argument with excess precision being passed
3801 without a prototype or in variable arguments.
3802 The last argument of __builtin_*_overflow_p should not be
3804 parmval = convert (valtype, val);
3805 else if ((invalid_func_diag =
3806 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3808 error (invalid_func_diag);
3811 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3816 /* Convert `short' and `char' to full-size `int'. */
3817 parmval = default_conversion (val);
3819 (*values)[parmnum] = parmval;
3820 if (parmval == error_mark_node)
3823 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3825 /* For a call to a built-in function declared without a prototype,
3826 perform the conversions from the argument to the expected type
3827 but issue warnings rather than errors for any mismatches.
3828 Ignore the converted argument and use the PARMVAL obtained
3829 above by applying default conversions instead. */
3830 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3831 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3832 val, valtype, npc, rname, parmnum, argnum,
3834 OPT_Wbuiltin_declaration_mismatch);
3838 typetail = TREE_CHAIN (typetail);
3840 if (builtin_typetail)
3841 builtin_typetail = TREE_CHAIN (builtin_typetail);
3844 gcc_assert (parmnum == vec_safe_length (values));
3846 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3848 error_at (loc, "too few arguments to function %qE", function);
3849 inform_declaration (fundecl);
3853 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3855 unsigned nargs = parmnum;
3856 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3859 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3860 "too few arguments to built-in function %qE "
3861 "expecting %u", function, nargs - 1))
3862 inform_declaration (fundecl);
3865 return error_args ? -1 : (int) parmnum;
3868 /* This is the entry point used by the parser to build unary operators
3869 in the input. CODE, a tree_code, specifies the unary operator, and
3870 ARG is the operand. For unary plus, the C parser currently uses
3871 CONVERT_EXPR for code.
3873 LOC is the location to use for the tree generated.
3877 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3879 struct c_expr result;
3881 result.original_code = code;
3882 result.original_type = NULL;
3883 result.m_decimal = 0;
3885 if (reject_gcc_builtin (arg.value))
3887 result.value = error_mark_node;
3891 result.value = build_unary_op (loc, code, arg.value, false);
3893 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3894 overflow_warning (loc, result.value, arg.value);
3897 /* We are typically called when parsing a prefix token at LOC acting on
3898 ARG. Reflect this by updating the source range of the result to
3899 start at LOC and end at the end of ARG. */
3900 set_c_expr_source_range (&result,
3901 loc, arg.get_finish ());
3906 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3909 char_type_p (tree type)
3911 return (type == char_type_node
3912 || type == unsigned_char_type_node
3913 || type == signed_char_type_node
3914 || type == char16_type_node
3915 || type == char32_type_node);
3918 /* This is the entry point used by the parser to build binary operators
3919 in the input. CODE, a tree_code, specifies the binary operator, and
3920 ARG1 and ARG2 are the operands. In addition to constructing the
3921 expression, we check for operands that were written with other binary
3922 operators in a way that is likely to confuse the user.
3924 LOCATION is the location of the binary operator. */
3927 parser_build_binary_op (location_t location, enum tree_code code,
3928 struct c_expr arg1, struct c_expr arg2)
3930 struct c_expr result;
3931 result.m_decimal = 0;
3933 enum tree_code code1 = arg1.original_code;
3934 enum tree_code code2 = arg2.original_code;
3935 tree type1 = (arg1.original_type
3936 ? arg1.original_type
3937 : TREE_TYPE (arg1.value));
3938 tree type2 = (arg2.original_type
3939 ? arg2.original_type
3940 : TREE_TYPE (arg2.value));
3942 result.value = build_binary_op (location, code,
3943 arg1.value, arg2.value, true);
3944 result.original_code = code;
3945 result.original_type = NULL;
3946 result.m_decimal = 0;
3948 if (TREE_CODE (result.value) == ERROR_MARK)
3950 set_c_expr_source_range (&result,
3952 arg2.get_finish ());
3956 if (location != UNKNOWN_LOCATION)
3957 protected_set_expr_location (result.value, location);
3959 set_c_expr_source_range (&result,
3961 arg2.get_finish ());
3963 /* Check for cases such as x+y<<z which users are likely
3965 if (warn_parentheses)
3966 warn_about_parentheses (location, code, code1, arg1.value, code2,
3969 if (warn_logical_op)
3970 warn_logical_operator (location, code, TREE_TYPE (result.value),
3971 code1, arg1.value, code2, arg2.value);
3973 if (warn_tautological_compare)
3975 tree lhs = arg1.value;
3976 tree rhs = arg2.value;
3977 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3979 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3980 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3983 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3985 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3987 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3988 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3991 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3993 if (lhs != NULL_TREE && rhs != NULL_TREE)
3994 warn_tautological_cmp (location, code, lhs, rhs);
3997 if (warn_logical_not_paren
3998 && TREE_CODE_CLASS (code) == tcc_comparison
3999 && code1 == TRUTH_NOT_EXPR
4000 && code2 != TRUTH_NOT_EXPR
4001 /* Avoid warning for !!x == y. */
4002 && (TREE_CODE (arg1.value) != NE_EXPR
4003 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
4005 /* Avoid warning for !b == y where b has _Bool type. */
4006 tree t = integer_zero_node;
4007 if (TREE_CODE (arg1.value) == EQ_EXPR
4008 && integer_zerop (TREE_OPERAND (arg1.value, 1))
4009 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
4011 t = TREE_OPERAND (arg1.value, 0);
4014 if (TREE_TYPE (t) != integer_type_node)
4016 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
4017 t = C_MAYBE_CONST_EXPR_EXPR (t);
4018 else if (CONVERT_EXPR_P (t))
4019 t = TREE_OPERAND (t, 0);
4025 if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
4026 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
4029 /* Warn about comparisons against string literals, with the exception
4030 of testing for equality or inequality of a string literal with NULL. */
4031 if (code == EQ_EXPR || code == NE_EXPR)
4033 if ((code1 == STRING_CST
4034 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
4035 || (code2 == STRING_CST
4036 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
4037 warning_at (location, OPT_Waddress,
4038 "comparison with string literal results in unspecified behavior");
4039 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
4040 if (POINTER_TYPE_P (type1)
4041 && null_pointer_constant_p (arg2.value)
4042 && char_type_p (type2))
4044 auto_diagnostic_group d;
4045 if (warning_at (location, OPT_Wpointer_compare,
4046 "comparison between pointer and zero character "
4048 inform (arg1.get_start (),
4049 "did you mean to dereference the pointer?");
4051 else if (POINTER_TYPE_P (type2)
4052 && null_pointer_constant_p (arg1.value)
4053 && char_type_p (type1))
4055 auto_diagnostic_group d;
4056 if (warning_at (location, OPT_Wpointer_compare,
4057 "comparison between pointer and zero character "
4059 inform (arg2.get_start (),
4060 "did you mean to dereference the pointer?");
4063 else if (TREE_CODE_CLASS (code) == tcc_comparison
4064 && (code1 == STRING_CST || code2 == STRING_CST))
4065 warning_at (location, OPT_Waddress,
4066 "comparison with string literal results in unspecified "
4069 if (warn_array_compare
4070 && TREE_CODE_CLASS (code) == tcc_comparison
4071 && TREE_CODE (type1) == ARRAY_TYPE
4072 && TREE_CODE (type2) == ARRAY_TYPE)
4073 do_warn_array_compare (location, code, arg1.value, arg2.value);
4075 if (TREE_OVERFLOW_P (result.value)
4076 && !TREE_OVERFLOW_P (arg1.value)
4077 && !TREE_OVERFLOW_P (arg2.value))
4078 overflow_warning (location, result.value);
4080 /* Warn about comparisons of different enum types. */
4081 if (warn_enum_compare
4082 && TREE_CODE_CLASS (code) == tcc_comparison
4083 && TREE_CODE (type1) == ENUMERAL_TYPE
4084 && TREE_CODE (type2) == ENUMERAL_TYPE
4085 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
4086 warning_at (location, OPT_Wenum_compare,
4087 "comparison between %qT and %qT",
4090 if (warn_xor_used_as_pow
4091 && code == BIT_XOR_EXPR
4094 check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
4101 /* Return a tree for the difference of pointers OP0 and OP1.
4102 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
4103 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
4106 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
4108 tree restype = ptrdiff_type_node;
4109 tree result, inttype;
4111 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
4112 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
4113 tree target_type = TREE_TYPE (TREE_TYPE (op0));
4114 tree orig_op0 = op0;
4115 tree orig_op1 = op1;
4117 /* If the operands point into different address spaces, we need to
4118 explicitly convert them to pointers into the common address space
4119 before we can subtract the numerical address values. */
4122 addr_space_t as_common;
4125 /* Determine the common superset address space. This is guaranteed
4126 to exist because the caller verified that comp_target_types
4127 returned non-zero. */
4128 if (!addr_space_superset (as0, as1, &as_common))
4131 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4132 op0 = convert (common_type, op0);
4133 op1 = convert (common_type, op1);
4136 /* Determine integer type result of the subtraction. This will usually
4137 be the same as the result type (ptrdiff_t), but may need to be a wider
4138 type if pointers for the address space are wider than ptrdiff_t. */
4139 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4140 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4144 if (TREE_CODE (target_type) == VOID_TYPE)
4145 pedwarn (loc, OPT_Wpointer_arith,
4146 "pointer of type %<void *%> used in subtraction");
4147 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4148 pedwarn (loc, OPT_Wpointer_arith,
4149 "pointer to a function used in subtraction");
4151 if (current_function_decl != NULL_TREE
4152 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4154 op0 = save_expr (op0);
4155 op1 = save_expr (op1);
4157 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4158 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4161 /* First do the subtraction, then build the divide operator
4162 and only convert at the very end.
4163 Do not do default conversions in case restype is a short type. */
4165 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4166 pointers. If some platform cannot provide that, or has a larger
4167 ptrdiff_type to support differences larger than half the address
4168 space, cast the pointers to some larger integer type and do the
4169 computations in that type. */
4170 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4171 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4172 convert (inttype, op1), false);
4175 /* Cast away qualifiers. */
4176 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4177 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4178 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4181 /* This generates an error if op1 is pointer to incomplete type. */
4182 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4183 error_at (loc, "arithmetic on pointer to an incomplete type");
4184 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4185 TREE_TYPE (TREE_TYPE (orig_op0))))
4186 verify_type_context (loc, TCTX_POINTER_ARITH,
4187 TREE_TYPE (TREE_TYPE (orig_op1)));
4189 op1 = c_size_in_bytes (target_type);
4191 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4192 error_at (loc, "arithmetic on pointer to an empty aggregate");
4194 /* Divide by the size, in easiest possible way. */
4195 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4196 op0, convert (inttype, op1));
4198 /* Convert to final result type if necessary. */
4199 return convert (restype, result);
4202 /* Expand atomic compound assignments into an appropriate sequence as
4203 specified by the C11 standard section 6.5.16.2.
4209 This sequence is used for all types for which these operations are
4212 In addition, built-in versions of the 'fe' prefixed routines may
4213 need to be invoked for floating point (real, complex or vector) when
4214 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4224 __atomic_load (addr, &old, SEQ_CST);
4225 feholdexcept (&fenv);
4227 newval = old op val;
4228 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4231 feclearexcept (FE_ALL_EXCEPT);
4234 feupdateenv (&fenv);
4236 The compiler will issue the __atomic_fetch_* built-in when possible,
4237 otherwise it will generate the generic form of the atomic operations.
4238 This requires temp(s) and has their address taken. The atomic processing
4239 is smart enough to figure out when the size of an object can utilize
4240 a lock-free version, and convert the built-in call to the appropriate
4241 lock-free routine. The optimizers will then dispose of any temps that
4242 are no longer required, and lock-free implementations are utilized as
4243 long as there is target support for the required size.
4245 If the operator is NOP_EXPR, then this is a simple assignment, and
4246 an __atomic_store is issued to perform the assignment rather than
4249 /* Build an atomic assignment at LOC, expanding into the proper
4250 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4251 the result of the operation, unless RETURN_OLD_P, in which case
4252 return the old value of LHS (this is only for postincrement and
4256 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4257 tree rhs, bool return_old_p)
4259 tree fndecl, func_call;
4260 vec<tree, va_gc> *params;
4261 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4263 tree compound_stmt = NULL_TREE;
4264 tree stmt, goto_stmt;
4265 tree loop_label, loop_decl, done_label, done_decl;
4267 tree lhs_type = TREE_TYPE (lhs);
4268 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4269 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4270 tree rhs_semantic_type = TREE_TYPE (rhs);
4271 tree nonatomic_rhs_semantic_type;
4274 gcc_assert (TYPE_ATOMIC (lhs_type));
4277 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4279 /* Allocate enough vector items for a compare_exchange. */
4280 vec_alloc (params, 6);
4282 /* Create a compound statement to hold the sequence of statements
4284 if (modifycode != NOP_EXPR)
4286 compound_stmt = c_begin_compound_stmt (false);
4288 /* For consistency with build_modify_expr on non-_Atomic,
4289 mark the lhs as read. Also, it would be very hard to match
4290 such expressions in mark_exp_read. */
4291 mark_exp_read (lhs);
4294 /* Remove any excess precision (which is only present here in the
4295 case of compound assignments). */
4296 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4298 gcc_assert (modifycode != NOP_EXPR);
4299 rhs = TREE_OPERAND (rhs, 0);
4301 rhs_type = TREE_TYPE (rhs);
4303 /* Fold the RHS if it hasn't already been folded. */
4304 if (modifycode != NOP_EXPR)
4305 rhs = c_fully_fold (rhs, false, NULL);
4307 /* Remove the qualifiers for the rest of the expressions and create
4308 the VAL temp variable to hold the RHS. */
4309 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4310 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4311 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4313 val = create_tmp_var_raw (nonatomic_rhs_type);
4314 TREE_ADDRESSABLE (val) = 1;
4315 suppress_warning (val);
4316 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4318 TREE_SIDE_EFFECTS (rhs) = 1;
4319 SET_EXPR_LOCATION (rhs, loc);
4320 if (modifycode != NOP_EXPR)
4323 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4325 if (modifycode == NOP_EXPR)
4327 compound_stmt = rhs;
4328 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4329 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4330 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4331 params->quick_push (lhs_addr);
4332 params->quick_push (rhs);
4333 params->quick_push (seq_cst);
4334 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4336 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4337 compound_stmt, func_call);
4339 /* VAL is the value which was stored, return a COMPOUND_STMT of
4340 the statement and that value. */
4341 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4344 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4345 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4346 isn't applicable for such builtins. ??? Do we want to handle enums? */
4347 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4348 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4350 built_in_function fncode;
4354 case POINTER_PLUS_EXPR:
4355 fncode = (return_old_p
4356 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4357 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4360 fncode = (return_old_p
4361 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4362 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4365 fncode = (return_old_p
4366 ? BUILT_IN_ATOMIC_FETCH_AND_N
4367 : BUILT_IN_ATOMIC_AND_FETCH_N);
4370 fncode = (return_old_p
4371 ? BUILT_IN_ATOMIC_FETCH_OR_N
4372 : BUILT_IN_ATOMIC_OR_FETCH_N);
4375 fncode = (return_old_p
4376 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4377 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4383 /* We can only use "_1" through "_16" variants of the atomic fetch
4385 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4386 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4389 /* If this is a pointer type, we need to multiply by the size of
4390 the pointer target type. */
4391 if (POINTER_TYPE_P (lhs_type))
4393 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4394 /* ??? This would introduce -Wdiscarded-qualifiers
4395 warning: __atomic_fetch_* expect volatile void *
4396 type as the first argument. (Assignments between
4397 atomic and non-atomic objects are OK.) */
4398 || TYPE_RESTRICT (lhs_type))
4400 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4401 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4402 convert (ptrdiff_type_node, rhs),
4403 convert (ptrdiff_type_node, sz));
4406 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4407 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4408 fndecl = builtin_decl_explicit (fncode);
4409 params->quick_push (lhs_addr);
4410 params->quick_push (rhs);
4411 params->quick_push (seq_cst);
4412 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4414 newval = create_tmp_var_raw (nonatomic_lhs_type);
4415 TREE_ADDRESSABLE (newval) = 1;
4416 suppress_warning (newval);
4417 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4418 NULL_TREE, NULL_TREE);
4419 SET_EXPR_LOCATION (rhs, loc);
4422 /* Finish the compound statement. */
4423 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4425 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4426 the statement and that value. */
4427 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4431 /* Create the variables and labels required for the op= form. */
4432 old = create_tmp_var_raw (nonatomic_lhs_type);
4433 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4434 TREE_ADDRESSABLE (old) = 1;
4435 suppress_warning (old);
4437 newval = create_tmp_var_raw (nonatomic_lhs_type);
4438 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4439 TREE_ADDRESSABLE (newval) = 1;
4440 suppress_warning (newval);
4442 loop_decl = create_artificial_label (loc);
4443 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4445 done_decl = create_artificial_label (loc);
4446 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4448 /* __atomic_load (addr, &old, SEQ_CST). */
4449 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4450 params->quick_push (lhs_addr);
4451 params->quick_push (old_addr);
4452 params->quick_push (seq_cst);
4453 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4454 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4457 params->truncate (0);
4459 /* Create the expressions for floating-point environment
4460 manipulation, if required. */
4461 bool need_fenv = (flag_trapping_math
4462 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4463 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4465 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4468 add_stmt (hold_call);
4471 add_stmt (loop_label);
4473 /* newval = old + val; */
4474 if (rhs_type != rhs_semantic_type)
4475 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4476 rhs = build_binary_op (loc, modifycode, old, val, true);
4477 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4479 tree eptype = TREE_TYPE (rhs);
4480 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4481 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4484 rhs = c_fully_fold (rhs, false, NULL);
4485 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4486 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4488 if (rhs != error_mark_node)
4490 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4492 SET_EXPR_LOCATION (rhs, loc);
4496 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4498 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4499 params->quick_push (lhs_addr);
4500 params->quick_push (old_addr);
4501 params->quick_push (newval_addr);
4502 params->quick_push (integer_zero_node);
4503 params->quick_push (seq_cst);
4504 params->quick_push (seq_cst);
4505 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4507 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4508 SET_EXPR_LOCATION (goto_stmt, loc);
4510 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4511 SET_EXPR_LOCATION (stmt, loc);
4515 add_stmt (clear_call);
4518 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4519 SET_EXPR_LOCATION (goto_stmt, loc);
4520 add_stmt (goto_stmt);
4523 add_stmt (done_label);
4526 add_stmt (update_call);
4528 /* Finish the compound statement. */
4529 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4531 /* NEWVAL is the value that was successfully stored, return a
4532 COMPOUND_EXPR of the statement and the appropriate value. */
4533 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4534 return_old_p ? old : newval);
4537 /* Construct and perhaps optimize a tree representation
4538 for a unary operation. CODE, a tree_code, specifies the operation
4539 and XARG is the operand.
4540 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4541 promotions (such as from short to int).
4542 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4543 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4546 LOCATION is the location of the operator. */
4549 build_unary_op (location_t location, enum tree_code code, tree xarg,
4552 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4554 tree argtype = NULL_TREE;
4555 enum tree_code typecode;
4557 tree ret = error_mark_node;
4558 tree eptype = NULL_TREE;
4559 const char *invalid_op_diag;
4562 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4564 arg = remove_c_maybe_const_expr (arg);
4566 if (code != ADDR_EXPR)
4567 arg = require_complete_type (location, arg);
4569 typecode = TREE_CODE (TREE_TYPE (arg));
4570 if (typecode == ERROR_MARK)
4571 return error_mark_node;
4572 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4573 typecode = INTEGER_TYPE;
4575 if ((invalid_op_diag
4576 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4578 error_at (location, invalid_op_diag);
4579 return error_mark_node;
4582 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4584 eptype = TREE_TYPE (arg);
4585 arg = TREE_OPERAND (arg, 0);
4591 /* This is used for unary plus, because a CONVERT_EXPR
4592 is enough to prevent anybody from looking inside for
4593 associativity, but won't generate any code. */
4594 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4595 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4596 || gnu_vector_type_p (TREE_TYPE (arg))))
4598 error_at (location, "wrong type argument to unary plus");
4599 return error_mark_node;
4601 else if (!noconvert)
4602 arg = default_conversion (arg);
4603 arg = non_lvalue_loc (location, arg);
4607 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4608 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4609 || gnu_vector_type_p (TREE_TYPE (arg))))
4611 error_at (location, "wrong type argument to unary minus");
4612 return error_mark_node;
4614 else if (!noconvert)
4615 arg = default_conversion (arg);
4619 /* ~ works on integer types and non float vectors. */
4620 if (typecode == INTEGER_TYPE
4621 || (gnu_vector_type_p (TREE_TYPE (arg))
4622 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4626 /* Warn if the expression has boolean value. */
4627 while (TREE_CODE (e) == COMPOUND_EXPR)
4628 e = TREE_OPERAND (e, 1);
4630 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
4631 || truth_value_p (TREE_CODE (e))))
4633 auto_diagnostic_group d;
4634 if (warning_at (location, OPT_Wbool_operation,
4635 "%<~%> on a boolean expression"))
4637 gcc_rich_location richloc (location);
4638 richloc.add_fixit_insert_before (location, "!");
4639 inform (&richloc, "did you mean to use logical not?");
4643 arg = default_conversion (arg);
4645 else if (typecode == COMPLEX_TYPE)
4648 pedwarn (location, OPT_Wpedantic,
4649 "ISO C does not support %<~%> for complex conjugation");
4651 arg = default_conversion (arg);
4655 error_at (location, "wrong type argument to bit-complement");
4656 return error_mark_node;
4661 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4663 error_at (location, "wrong type argument to abs");
4664 return error_mark_node;
4666 else if (!noconvert)
4667 arg = default_conversion (arg);
4671 if (!(typecode == INTEGER_TYPE))
4673 error_at (location, "wrong type argument to absu");
4674 return error_mark_node;
4676 else if (!noconvert)
4677 arg = default_conversion (arg);
4681 /* Conjugating a real value is a no-op, but allow it anyway. */
4682 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4683 || typecode == COMPLEX_TYPE))
4685 error_at (location, "wrong type argument to conjugation");
4686 return error_mark_node;
4688 else if (!noconvert)
4689 arg = default_conversion (arg);
4692 case TRUTH_NOT_EXPR:
4693 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4694 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4695 && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE)
4698 "wrong type argument to unary exclamation mark");
4699 return error_mark_node;
4703 arg = c_objc_common_truthvalue_conversion (location, xarg);
4704 arg = remove_c_maybe_const_expr (arg);
4707 arg = c_objc_common_truthvalue_conversion (location, arg);
4708 ret = invert_truthvalue_loc (location, arg);
4709 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4710 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4711 location = EXPR_LOCATION (ret);
4712 goto return_build_unary_op;
4716 ret = build_real_imag_expr (location, code, arg);
4717 if (ret == error_mark_node)
4718 return error_mark_node;
4719 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4720 eptype = TREE_TYPE (eptype);
4721 goto return_build_unary_op;
4723 case PREINCREMENT_EXPR:
4724 case POSTINCREMENT_EXPR:
4725 case PREDECREMENT_EXPR:
4726 case POSTDECREMENT_EXPR:
4728 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4730 tree inner = build_unary_op (location, code,
4731 C_MAYBE_CONST_EXPR_EXPR (arg),
4733 if (inner == error_mark_node)
4734 return error_mark_node;
4735 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4736 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4737 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4738 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4739 goto return_build_unary_op;
4742 /* Complain about anything that is not a true lvalue. In
4743 Objective-C, skip this check for property_refs. */
4744 if (!objc_is_property_ref (arg)
4745 && !lvalue_or_else (location,
4746 arg, ((code == PREINCREMENT_EXPR
4747 || code == POSTINCREMENT_EXPR)
4750 return error_mark_node;
4752 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4754 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4755 warning_at (location, OPT_Wc___compat,
4756 "increment of enumeration value is invalid in C++");
4758 warning_at (location, OPT_Wc___compat,
4759 "decrement of enumeration value is invalid in C++");
4762 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4764 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4765 warning_at (location, OPT_Wbool_operation,
4766 "increment of a boolean expression");
4768 warning_at (location, OPT_Wbool_operation,
4769 "decrement of a boolean expression");
4772 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4773 arg = c_fully_fold (arg, false, NULL, true);
4776 atomic_op = really_atomic_lvalue (arg);
4778 /* Increment or decrement the real part of the value,
4779 and don't change the imaginary part. */
4780 if (typecode == COMPLEX_TYPE)
4784 pedwarn (location, OPT_Wpedantic,
4785 "ISO C does not support %<++%> and %<--%> on complex types");
4789 arg = stabilize_reference (arg);
4790 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4792 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4794 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4795 if (real == error_mark_node || imag == error_mark_node)
4796 return error_mark_node;
4797 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4799 goto return_build_unary_op;
4803 /* Report invalid types. */
4805 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4806 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4807 && typecode != COMPLEX_TYPE
4808 && !gnu_vector_type_p (TREE_TYPE (arg)))
4810 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4811 error_at (location, "wrong type argument to increment");
4813 error_at (location, "wrong type argument to decrement");
4815 return error_mark_node;
4821 argtype = TREE_TYPE (arg);
4823 /* Compute the increment. */
4825 if (typecode == POINTER_TYPE)
4827 /* If pointer target is an incomplete type,
4828 we just cannot know how to do the arithmetic. */
4829 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4831 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4833 "increment of pointer to an incomplete type %qT",
4834 TREE_TYPE (argtype));
4837 "decrement of pointer to an incomplete type %qT",
4838 TREE_TYPE (argtype));
4840 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4841 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4843 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4844 pedwarn (location, OPT_Wpointer_arith,
4845 "wrong type argument to increment");
4847 pedwarn (location, OPT_Wpointer_arith,
4848 "wrong type argument to decrement");
4851 verify_type_context (location, TCTX_POINTER_ARITH,
4852 TREE_TYPE (argtype));
4854 inc = c_size_in_bytes (TREE_TYPE (argtype));
4855 inc = convert_to_ptrofftype_loc (location, inc);
4857 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4859 /* For signed fract types, we invert ++ to -- or
4860 -- to ++, and change inc from 1 to -1, because
4861 it is not possible to represent 1 in signed fract constants.
4862 For unsigned fract types, the result always overflows and
4863 we get an undefined (original) or the maximum value. */
4864 if (code == PREINCREMENT_EXPR)
4865 code = PREDECREMENT_EXPR;
4866 else if (code == PREDECREMENT_EXPR)
4867 code = PREINCREMENT_EXPR;
4868 else if (code == POSTINCREMENT_EXPR)
4869 code = POSTDECREMENT_EXPR;
4870 else /* code == POSTDECREMENT_EXPR */
4871 code = POSTINCREMENT_EXPR;
4873 inc = integer_minus_one_node;
4874 inc = convert (argtype, inc);
4878 inc = VECTOR_TYPE_P (argtype)
4879 ? build_one_cst (argtype)
4881 inc = convert (argtype, inc);
4884 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4885 need to ask Objective-C to build the increment or decrement
4886 expression for it. */
4887 if (objc_is_property_ref (arg))
4888 return objc_build_incr_expr_for_property_ref (location, code,
4891 /* Report a read-only lvalue. */
4892 if (TYPE_READONLY (argtype))
4894 readonly_error (location, arg,
4895 ((code == PREINCREMENT_EXPR
4896 || code == POSTINCREMENT_EXPR)
4897 ? lv_increment : lv_decrement));
4898 return error_mark_node;
4900 else if (TREE_READONLY (arg))
4901 readonly_warning (arg,
4902 ((code == PREINCREMENT_EXPR
4903 || code == POSTINCREMENT_EXPR)
4904 ? lv_increment : lv_decrement));
4906 /* If the argument is atomic, use the special code sequences for
4907 atomic compound assignment. */
4910 arg = stabilize_reference (arg);
4911 ret = build_atomic_assign (location, arg,
4912 ((code == PREINCREMENT_EXPR
4913 || code == POSTINCREMENT_EXPR)
4916 (FRACT_MODE_P (TYPE_MODE (argtype))
4918 : integer_one_node),
4919 (code == POSTINCREMENT_EXPR
4920 || code == POSTDECREMENT_EXPR));
4921 goto return_build_unary_op;
4924 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4925 val = boolean_increment (code, arg);
4927 val = build2 (code, TREE_TYPE (arg), arg, inc);
4928 TREE_SIDE_EFFECTS (val) = 1;
4929 if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
4930 TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
4933 goto return_build_unary_op;
4937 /* Note that this operation never does default_conversion. */
4939 /* The operand of unary '&' must be an lvalue (which excludes
4940 expressions of type void), or, in C99, the result of a [] or
4941 unary '*' operator. */
4942 if (VOID_TYPE_P (TREE_TYPE (arg))
4943 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4944 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4945 pedwarn (location, 0, "taking address of expression of type %<void%>");
4947 /* Let &* cancel out to simplify resulting code. */
4948 if (INDIRECT_REF_P (arg))
4950 /* Don't let this be an lvalue. */
4951 if (lvalue_p (TREE_OPERAND (arg, 0)))
4952 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4953 ret = TREE_OPERAND (arg, 0);
4954 goto return_build_unary_op;
4957 /* Anything not already handled and not a true memory reference
4958 or a non-lvalue array is an error. */
4959 if (typecode != FUNCTION_TYPE && !noconvert
4960 && !lvalue_or_else (location, arg, lv_addressof))
4961 return error_mark_node;
4963 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4965 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4967 tree inner = build_unary_op (location, code,
4968 C_MAYBE_CONST_EXPR_EXPR (arg),
4970 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4971 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4972 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4973 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4974 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4975 goto return_build_unary_op;
4978 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4979 argtype = TREE_TYPE (arg);
4981 /* If the lvalue is const or volatile, merge that into the type
4982 to which the address will point. This is only needed
4983 for function types. */
4984 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4985 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4986 && TREE_CODE (argtype) == FUNCTION_TYPE)
4988 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4989 int quals = orig_quals;
4991 if (TREE_READONLY (arg))
4992 quals |= TYPE_QUAL_CONST;
4993 if (TREE_THIS_VOLATILE (arg))
4994 quals |= TYPE_QUAL_VOLATILE;
4996 argtype = c_build_qualified_type (argtype, quals);
4999 switch (TREE_CODE (arg))
5002 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5004 error_at (location, "cannot take address of bit-field %qD",
5005 TREE_OPERAND (arg, 1));
5006 return error_mark_node;
5012 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
5014 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
5015 && !POINTER_TYPE_P (TREE_TYPE (arg))
5016 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
5018 error_at (location, "cannot take address of scalar with "
5019 "reverse storage order");
5020 return error_mark_node;
5023 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
5024 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
5025 warning_at (location, OPT_Wscalar_storage_order,
5026 "address of array with reverse scalar storage "
5034 if (!c_mark_addressable (arg))
5035 return error_mark_node;
5037 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
5038 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
5040 argtype = build_pointer_type (argtype);
5042 /* ??? Cope with user tricks that amount to offsetof. Delete this
5043 when we have proper support for integer constant expressions. */
5044 val = get_base_address (arg);
5045 if (val && INDIRECT_REF_P (val)
5046 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5048 ret = fold_offsetof (arg, argtype);
5049 goto return_build_unary_op;
5052 val = build1 (ADDR_EXPR, argtype, arg);
5055 goto return_build_unary_op;
5058 ret = build1 (code, TREE_TYPE (arg), arg);
5059 goto return_build_unary_op;
5065 if (argtype == NULL_TREE)
5066 argtype = TREE_TYPE (arg);
5067 if (TREE_CODE (arg) == INTEGER_CST)
5068 ret = (require_constant_value
5069 ? fold_build1_initializer_loc (location, code, argtype, arg)
5070 : fold_build1_loc (location, code, argtype, arg));
5072 ret = build1 (code, argtype, arg);
5073 return_build_unary_op:
5074 gcc_assert (ret != error_mark_node);
5075 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
5076 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
5077 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
5078 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
5079 ret = note_integer_operands (ret);
5081 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5082 protected_set_expr_location (ret, location);
5086 /* Return nonzero if REF is an lvalue valid for this language.
5087 Lvalues can be assigned, unless their type has TYPE_READONLY.
5088 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
5091 lvalue_p (const_tree ref)
5093 const enum tree_code code = TREE_CODE (ref);
5100 return lvalue_p (TREE_OPERAND (ref, 0));
5102 case C_MAYBE_CONST_EXPR:
5103 return lvalue_p (TREE_OPERAND (ref, 1));
5105 case COMPOUND_LITERAL_EXPR:
5110 case TARGET_MEM_REF:
5111 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5119 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5120 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5123 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5130 /* Give a warning for storing in something that is read-only in GCC
5131 terms but not const in ISO C terms. */
5134 readonly_warning (tree arg, enum lvalue_use use)
5139 warning (0, "assignment of read-only location %qE", arg);
5142 warning (0, "increment of read-only location %qE", arg);
5145 warning (0, "decrement of read-only location %qE", arg);
5154 /* Return nonzero if REF is an lvalue valid for this language;
5155 otherwise, print an error message and return zero. USE says
5156 how the lvalue is being used and so selects the error message.
5157 LOCATION is the location at which any error should be reported. */
5160 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5162 int win = lvalue_p (ref);
5165 lvalue_error (loc, use);
5170 /* Mark EXP saying that we need to be able to take the
5171 address of it; it should not be allocated in a register.
5172 Returns true if successful. ARRAY_REF_P is true if this
5173 is for ARRAY_REF construction - in that case we don't want
5174 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5175 it is fine to use ARRAY_REFs for vector subscripts on vector
5176 register variables. */
5179 c_mark_addressable (tree exp, bool array_ref_p)
5184 switch (TREE_CODE (x))
5186 case VIEW_CONVERT_EXPR:
5188 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5189 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5191 x = TREE_OPERAND (x, 0);
5195 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5197 error ("cannot take address of bit-field %qD",
5198 TREE_OPERAND (x, 1));
5206 x = TREE_OPERAND (x, 0);
5209 case COMPOUND_LITERAL_EXPR:
5210 if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
5212 error ("address of register compound literal requested");
5215 TREE_ADDRESSABLE (x) = 1;
5216 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5220 TREE_ADDRESSABLE (x) = 1;
5227 if (C_DECL_REGISTER (x)
5228 && DECL_NONLOCAL (x))
5230 if (TREE_PUBLIC (x) || is_global_var (x))
5233 ("global register variable %qD used in nested function", x);
5236 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5238 else if (C_DECL_REGISTER (x))
5240 if (TREE_PUBLIC (x) || is_global_var (x))
5241 error ("address of global register variable %qD requested", x);
5243 error ("address of register variable %qD requested", x);
5249 TREE_ADDRESSABLE (x) = 1;
5256 /* Convert EXPR to TYPE, warning about conversion problems with
5257 constants. SEMANTIC_TYPE is the type this conversion would use
5258 without excess precision. If SEMANTIC_TYPE is NULL, this function
5259 is equivalent to convert_and_check. This function is a wrapper that
5260 handles conversions that may be different than
5261 the usual ones because of excess precision. */
5264 ep_convert_and_check (location_t loc, tree type, tree expr,
5267 if (TREE_TYPE (expr) == type)
5270 /* For C11, integer conversions may have results with excess
5272 if (flag_isoc11 || !semantic_type)
5273 return convert_and_check (loc, type, expr);
5275 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5276 && TREE_TYPE (expr) != semantic_type)
5278 /* For integers, we need to check the real conversion, not
5279 the conversion to the excess precision type. */
5280 expr = convert_and_check (loc, semantic_type, expr);
5282 /* Result type is the excess precision type, which should be
5283 large enough, so do not check. */
5284 return convert (type, expr);
5287 /* If EXPR refers to a built-in declared without a prototype returns
5288 the actual type of the built-in and, if non-null, set *BLTIN to
5289 a pointer to the built-in. Otherwise return the type of EXPR
5290 and clear *BLTIN if non-null. */
5293 type_or_builtin_type (tree expr, tree *bltin = NULL)
5301 tree type = TREE_TYPE (expr);
5302 if (TREE_CODE (expr) != ADDR_EXPR)
5305 tree oper = TREE_OPERAND (expr, 0);
5307 || TREE_CODE (oper) != FUNCTION_DECL
5308 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5311 built_in_function code = DECL_FUNCTION_CODE (oper);
5312 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5315 if ((*bltin = builtin_decl_implicit (code)))
5316 type = build_pointer_type (TREE_TYPE (*bltin));
5321 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5322 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5323 if folded to an integer constant then the unselected half may
5324 contain arbitrary operations not normally permitted in constant
5325 expressions. Set the location of the expression to LOC. */
5328 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5329 tree op1, tree op1_original_type, location_t op1_loc,
5330 tree op2, tree op2_original_type, location_t op2_loc)
5334 enum tree_code code1;
5335 enum tree_code code2;
5336 tree result_type = NULL;
5337 tree semantic_result_type = NULL;
5338 tree orig_op1 = op1, orig_op2 = op2;
5339 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5340 bool ifexp_int_operands;
5343 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5344 if (op1_int_operands)
5345 op1 = remove_c_maybe_const_expr (op1);
5346 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5347 if (op2_int_operands)
5348 op2 = remove_c_maybe_const_expr (op2);
5349 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5350 if (ifexp_int_operands)
5351 ifexp = remove_c_maybe_const_expr (ifexp);
5353 /* Promote both alternatives. */
5355 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5356 op1 = default_conversion (op1);
5357 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5358 op2 = default_conversion (op2);
5360 if (TREE_CODE (ifexp) == ERROR_MARK
5361 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5362 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5363 return error_mark_node;
5365 tree bltin1 = NULL_TREE;
5366 tree bltin2 = NULL_TREE;
5367 type1 = type_or_builtin_type (op1, &bltin1);
5368 code1 = TREE_CODE (type1);
5369 type2 = type_or_builtin_type (op2, &bltin2);
5370 code2 = TREE_CODE (type2);
5372 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5373 return error_mark_node;
5375 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5376 return error_mark_node;
5378 /* C90 does not permit non-lvalue arrays in conditional expressions.
5379 In C99 they will be pointers by now. */
5380 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5382 error_at (colon_loc, "non-lvalue array in conditional expression");
5383 return error_mark_node;
5386 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5387 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5388 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5389 || code1 == COMPLEX_TYPE)
5390 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5391 || code2 == COMPLEX_TYPE))
5393 semantic_result_type = c_common_type (type1, type2);
5394 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5396 op1 = TREE_OPERAND (op1, 0);
5397 type1 = TREE_TYPE (op1);
5398 gcc_assert (TREE_CODE (type1) == code1);
5400 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5402 op2 = TREE_OPERAND (op2, 0);
5403 type2 = TREE_TYPE (op2);
5404 gcc_assert (TREE_CODE (type2) == code2);
5408 if (warn_cxx_compat)
5410 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5411 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5413 if (TREE_CODE (t1) == ENUMERAL_TYPE
5414 && TREE_CODE (t2) == ENUMERAL_TYPE
5415 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5416 warning_at (colon_loc, OPT_Wc___compat,
5417 ("different enum types in conditional is "
5418 "invalid in C++: %qT vs %qT"),
5422 /* Quickly detect the usual case where op1 and op2 have the same type
5424 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5427 result_type = type1;
5429 result_type = TYPE_MAIN_VARIANT (type1);
5431 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5432 || code1 == COMPLEX_TYPE)
5433 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5434 || code2 == COMPLEX_TYPE))
5436 /* In C11, a conditional expression between a floating-point
5437 type and an integer type should convert the integer type to
5438 the evaluation format of the floating-point type, with
5439 possible excess precision. */
5440 tree eptype1 = type1;
5441 tree eptype2 = type2;
5445 if (ANY_INTEGRAL_TYPE_P (type1)
5446 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5449 if (!semantic_result_type)
5450 semantic_result_type = c_common_type (type1, type2);
5452 else if (ANY_INTEGRAL_TYPE_P (type2)
5453 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5456 if (!semantic_result_type)
5457 semantic_result_type = c_common_type (type1, type2);
5460 result_type = c_common_type (eptype1, eptype2);
5461 if (result_type == error_mark_node)
5462 return error_mark_node;
5463 do_warn_double_promotion (result_type, type1, type2,
5464 "implicit conversion from %qT to %qT to "
5465 "match other result of conditional",
5468 /* If -Wsign-compare, warn here if type1 and type2 have
5469 different signedness. We'll promote the signed to unsigned
5470 and later code won't know it used to be different.
5471 Do this check on the original types, so that explicit casts
5472 will be considered, but default promotions won't. */
5473 if (c_inhibit_evaluation_warnings == 0)
5475 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5476 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5478 if (unsigned_op1 ^ unsigned_op2)
5482 /* Do not warn if the result type is signed, since the
5483 signed type will only be chosen if it can represent
5484 all the values of the unsigned type. */
5485 if (!TYPE_UNSIGNED (result_type))
5489 bool op1_maybe_const = true;
5490 bool op2_maybe_const = true;
5492 /* Do not warn if the signed quantity is an
5493 unsuffixed integer literal (or some static
5494 constant expression involving such literals) and
5495 it is non-negative. This warning requires the
5496 operands to be folded for best results, so do
5497 that folding in this case even without
5498 warn_sign_compare to avoid warning options
5499 possibly affecting code generation. */
5500 c_inhibit_evaluation_warnings
5501 += (ifexp == truthvalue_false_node);
5502 op1 = c_fully_fold (op1, require_constant_value,
5504 c_inhibit_evaluation_warnings
5505 -= (ifexp == truthvalue_false_node);
5507 c_inhibit_evaluation_warnings
5508 += (ifexp == truthvalue_true_node);
5509 op2 = c_fully_fold (op2, require_constant_value,
5511 c_inhibit_evaluation_warnings
5512 -= (ifexp == truthvalue_true_node);
5514 if (warn_sign_compare)
5517 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5519 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5521 else if (unsigned_op2)
5522 warning_at (op1_loc, OPT_Wsign_compare,
5523 "operand of %<?:%> changes signedness from "
5524 "%qT to %qT due to unsignedness of other "
5525 "operand", TREE_TYPE (orig_op1),
5526 TREE_TYPE (orig_op2));
5528 warning_at (op2_loc, OPT_Wsign_compare,
5529 "operand of %<?:%> changes signedness from "
5530 "%qT to %qT due to unsignedness of other "
5531 "operand", TREE_TYPE (orig_op2),
5532 TREE_TYPE (orig_op1));
5534 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5535 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5536 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5537 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5542 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5544 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5545 pedwarn (colon_loc, OPT_Wpedantic,
5546 "ISO C forbids conditional expr with only one void side");
5547 result_type = void_type_node;
5549 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5551 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5552 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5553 addr_space_t as_common;
5555 if (comp_target_types (colon_loc, type1, type2))
5556 result_type = common_pointer_type (type1, type2);
5557 else if (null_pointer_constant_p (orig_op1))
5558 result_type = type2;
5559 else if (null_pointer_constant_p (orig_op2))
5560 result_type = type1;
5561 else if (!addr_space_superset (as1, as2, &as_common))
5563 error_at (colon_loc, "pointers to disjoint address spaces "
5564 "used in conditional expression");
5565 return error_mark_node;
5567 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5568 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5569 || (VOID_TYPE_P (TREE_TYPE (type2))
5570 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5572 tree t1 = TREE_TYPE (type1);
5573 tree t2 = TREE_TYPE (type2);
5574 if (!(VOID_TYPE_P (t1)
5575 && !TYPE_ATOMIC (t1)))
5577 /* roles are swapped */
5579 t2 = TREE_TYPE (type1);
5581 tree t2_stripped = strip_array_types (t2);
5582 if ((TREE_CODE (t2) == ARRAY_TYPE)
5583 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5586 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5587 "pointer to array loses qualifier "
5588 "in conditional expression");
5589 else if (warn_c11_c2x_compat > 0)
5590 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5591 "pointer to array loses qualifier "
5592 "in conditional expression in ISO C before C2X");
5594 if (TREE_CODE (t2) == FUNCTION_TYPE)
5595 pedwarn (colon_loc, OPT_Wpedantic,
5596 "ISO C forbids conditional expr between "
5597 "%<void *%> and function pointer");
5598 /* for array, use qualifiers of element type */
5601 result_type = build_pointer_type (qualify_type (t1, t2));
5603 /* Objective-C pointer comparisons are a bit more lenient. */
5604 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5605 result_type = objc_common_type (type1, type2);
5608 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5609 if (bltin1 && bltin2)
5610 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5611 "pointer type mismatch between %qT and %qT "
5612 "of %qD and %qD in conditional expression",
5613 type1, type2, bltin1, bltin2);
5615 pedwarn (colon_loc, 0,
5616 "pointer type mismatch in conditional expression");
5617 result_type = build_pointer_type
5618 (build_qualified_type (void_type_node, qual));
5621 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5623 if (!null_pointer_constant_p (orig_op2))
5624 pedwarn (colon_loc, 0,
5625 "pointer/integer type mismatch in conditional expression");
5628 op2 = null_pointer_node;
5630 result_type = type1;
5632 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5634 if (!null_pointer_constant_p (orig_op1))
5635 pedwarn (colon_loc, 0,
5636 "pointer/integer type mismatch in conditional expression");
5639 op1 = null_pointer_node;
5641 result_type = type2;
5643 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
5644 type nullptr_t and the other is a pointer, the result type is the pointer
5646 else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
5647 result_type = type2;
5648 else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
5649 result_type = type1;
5653 if (flag_cond_mismatch)
5654 result_type = void_type_node;
5657 error_at (colon_loc, "type mismatch in conditional expression");
5658 return error_mark_node;
5662 /* Merge const and volatile flags of the incoming types. */
5664 = build_type_variant (result_type,
5665 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5666 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5668 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5669 semantic_result_type);
5670 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5671 semantic_result_type);
5673 if (ifexp_bcp && ifexp == truthvalue_true_node)
5675 op2_int_operands = true;
5676 op1 = c_fully_fold (op1, require_constant_value, NULL);
5678 if (ifexp_bcp && ifexp == truthvalue_false_node)
5680 op1_int_operands = true;
5681 op2 = c_fully_fold (op2, require_constant_value, NULL);
5683 int_const = int_operands = (ifexp_int_operands
5685 && op2_int_operands);
5688 int_const = ((ifexp == truthvalue_true_node
5689 && TREE_CODE (orig_op1) == INTEGER_CST
5690 && !TREE_OVERFLOW (orig_op1))
5691 || (ifexp == truthvalue_false_node
5692 && TREE_CODE (orig_op2) == INTEGER_CST
5693 && !TREE_OVERFLOW (orig_op2)));
5696 /* Need to convert condition operand into a vector mask. */
5697 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5699 tree vectype = TREE_TYPE (ifexp);
5700 tree elem_type = TREE_TYPE (vectype);
5701 tree zero = build_int_cst (elem_type, 0);
5702 tree zero_vec = build_vector_from_val (vectype, zero);
5703 tree cmp_type = truth_type_for (vectype);
5704 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5707 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5708 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5713 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5714 nested inside of the expression. */
5715 op1 = c_fully_fold (op1, false, NULL);
5716 op2 = c_fully_fold (op2, false, NULL);
5718 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5720 ret = note_integer_operands (ret);
5722 if (semantic_result_type)
5723 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5725 protected_set_expr_location (ret, colon_loc);
5727 /* If the OP1 and OP2 are the same and don't have side-effects,
5728 warn here, because the COND_EXPR will be turned into OP1. */
5729 if (warn_duplicated_branches
5730 && TREE_CODE (ret) == COND_EXPR
5731 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5732 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5733 "this condition has identical branches");
5738 /* EXPR is an expression, location LOC, whose result is discarded.
5739 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5740 whose right-hand operand is such a call, possibly recursively). */
5743 maybe_warn_nodiscard (location_t loc, tree expr)
5745 if (VOID_TYPE_P (TREE_TYPE (expr)))
5747 while (TREE_CODE (expr) == COMPOUND_EXPR)
5749 expr = TREE_OPERAND (expr, 1);
5750 if (EXPR_HAS_LOCATION (expr))
5751 loc = EXPR_LOCATION (expr);
5753 if (TREE_CODE (expr) != CALL_EXPR)
5755 tree fn = CALL_EXPR_FN (expr);
5759 if (TREE_CODE (fn) == ADDR_EXPR
5760 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5761 && (attr = lookup_attribute ("nodiscard",
5762 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5764 fn = TREE_OPERAND (fn, 0);
5765 tree args = TREE_VALUE (attr);
5767 args = TREE_VALUE (args);
5768 auto_diagnostic_group d;
5771 warned = warning_at (loc, OPT_Wunused_result,
5772 "ignoring return value of %qD, declared with "
5773 "attribute %<nodiscard%>: %E", fn, args);
5775 warned = warning_at (loc, OPT_Wunused_result,
5776 "ignoring return value of %qD, declared with "
5777 "attribute %<nodiscard%>", fn);
5779 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5783 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5784 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5787 tree args = TREE_VALUE (attr);
5789 args = TREE_VALUE (args);
5790 auto_diagnostic_group d;
5793 warned = warning_at (loc, OPT_Wunused_result,
5794 "ignoring return value of type %qT, declared "
5795 "with attribute %<nodiscard%>: %E",
5798 warned = warning_at (loc, OPT_Wunused_result,
5799 "ignoring return value of type %qT, declared "
5800 "with attribute %<nodiscard%>", rettype);
5803 if (TREE_CODE (fn) == ADDR_EXPR)
5805 fn = TREE_OPERAND (fn, 0);
5806 if (TREE_CODE (fn) == FUNCTION_DECL)
5807 inform (DECL_SOURCE_LOCATION (fn),
5808 "in call to %qD, declared here", fn);
5814 /* Return a compound expression that performs two expressions and
5815 returns the value of the second of them.
5817 LOC is the location of the COMPOUND_EXPR. */
5820 build_compound_expr (location_t loc, tree expr1, tree expr2)
5822 bool expr1_int_operands, expr2_int_operands;
5823 tree eptype = NULL_TREE;
5826 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5827 if (expr1_int_operands)
5828 expr1 = remove_c_maybe_const_expr (expr1);
5829 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5830 if (expr2_int_operands)
5831 expr2 = remove_c_maybe_const_expr (expr2);
5833 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5834 expr1 = TREE_OPERAND (expr1, 0);
5835 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5837 eptype = TREE_TYPE (expr2);
5838 expr2 = TREE_OPERAND (expr2, 0);
5841 if (!TREE_SIDE_EFFECTS (expr1))
5843 /* The left-hand operand of a comma expression is like an expression
5844 statement: with -Wunused, we should warn if it doesn't have
5845 any side-effects, unless it was explicitly cast to (void). */
5846 if (warn_unused_value)
5848 if (VOID_TYPE_P (TREE_TYPE (expr1))
5849 && CONVERT_EXPR_P (expr1))
5851 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5852 && TREE_CODE (expr1) == COMPOUND_EXPR
5853 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5854 ; /* (void) a, (void) b, c */
5856 warning_at (loc, OPT_Wunused_value,
5857 "left-hand operand of comma expression has no effect");
5860 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5861 && warn_unused_value)
5864 location_t cloc = loc;
5865 while (TREE_CODE (r) == COMPOUND_EXPR)
5867 if (EXPR_HAS_LOCATION (r))
5868 cloc = EXPR_LOCATION (r);
5869 r = TREE_OPERAND (r, 1);
5871 if (!TREE_SIDE_EFFECTS (r)
5872 && !VOID_TYPE_P (TREE_TYPE (r))
5873 && !CONVERT_EXPR_P (r))
5874 warning_at (cloc, OPT_Wunused_value,
5875 "right-hand operand of comma expression has no effect");
5878 /* With -Wunused, we should also warn if the left-hand operand does have
5879 side-effects, but computes a value which is not used. For example, in
5880 `foo() + bar(), baz()' the result of the `+' operator is not used,
5881 so we should issue a warning. */
5882 else if (warn_unused_value)
5883 warn_if_unused_value (expr1, loc);
5885 maybe_warn_nodiscard (loc, expr1);
5887 if (expr2 == error_mark_node)
5888 return error_mark_node;
5890 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5893 && expr1_int_operands
5894 && expr2_int_operands)
5895 ret = note_integer_operands (ret);
5898 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5900 protected_set_expr_location (ret, loc);
5904 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5905 which we are casting. OTYPE is the type of the expression being
5906 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5907 of the cast. -Wcast-qual appeared on the command line. Named
5908 address space qualifiers are not handled here, because they result
5909 in different warnings. */
5912 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5914 tree in_type = type;
5915 tree in_otype = otype;
5920 /* Check that the qualifiers on IN_TYPE are a superset of the
5921 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5922 nodes is uninteresting and we stop as soon as we hit a
5923 non-POINTER_TYPE node on either type. */
5926 in_otype = TREE_TYPE (in_otype);
5927 in_type = TREE_TYPE (in_type);
5929 /* GNU C allows cv-qualified function types. 'const' means the
5930 function is very pure, 'volatile' means it can't return. We
5931 need to warn when such qualifiers are added, not when they're
5933 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5934 && TREE_CODE (in_type) == FUNCTION_TYPE)
5935 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5936 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5938 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5939 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5941 while (TREE_CODE (in_type) == POINTER_TYPE
5942 && TREE_CODE (in_otype) == POINTER_TYPE);
5945 warning_at (loc, OPT_Wcast_qual,
5946 "cast adds %q#v qualifier to function type", added);
5949 /* There are qualifiers present in IN_OTYPE that are not present
5951 warning_at (loc, OPT_Wcast_qual,
5952 "cast discards %qv qualifier from pointer target type",
5955 if (added || discarded)
5958 /* A cast from **T to const **T is unsafe, because it can cause a
5959 const value to be changed with no additional warning. We only
5960 issue this warning if T is the same on both sides, and we only
5961 issue the warning if there are the same number of pointers on
5962 both sides, as otherwise the cast is clearly unsafe anyhow. A
5963 cast is unsafe when a qualifier is added at one level and const
5964 is not present at all outer levels.
5966 To issue this warning, we check at each level whether the cast
5967 adds new qualifiers not already seen. We don't need to special
5968 case function types, as they won't have the same
5969 TYPE_MAIN_VARIANT. */
5971 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5973 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5978 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5981 in_type = TREE_TYPE (in_type);
5982 in_otype = TREE_TYPE (in_otype);
5983 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5986 warning_at (loc, OPT_Wcast_qual,
5987 "to be safe all intermediate pointers in cast from "
5988 "%qT to %qT must be %<const%> qualified",
5993 is_const = TYPE_READONLY (in_type);
5995 while (TREE_CODE (in_type) == POINTER_TYPE);
5998 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
6001 c_safe_arg_type_equiv_p (tree t1, tree t2)
6003 t1 = TYPE_MAIN_VARIANT (t1);
6004 t2 = TYPE_MAIN_VARIANT (t2);
6006 if (TREE_CODE (t1) == POINTER_TYPE
6007 && TREE_CODE (t2) == POINTER_TYPE)
6010 /* The signedness of the parameter matters only when an integral
6011 type smaller than int is promoted to int, otherwise only the
6012 precision of the parameter matters.
6013 This check should make sure that the callee does not see
6014 undefined values in argument registers. */
6015 if (INTEGRAL_TYPE_P (t1)
6016 && INTEGRAL_TYPE_P (t2)
6017 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
6018 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
6019 || !targetm.calls.promote_prototypes (NULL_TREE)
6020 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
6023 return comptypes (t1, t2);
6026 /* Check if a type cast between two function types can be considered safe. */
6029 c_safe_function_type_cast_p (tree t1, tree t2)
6031 if (TREE_TYPE (t1) == void_type_node &&
6032 TYPE_ARG_TYPES (t1) == void_list_node)
6035 if (TREE_TYPE (t2) == void_type_node &&
6036 TYPE_ARG_TYPES (t2) == void_list_node)
6039 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
6042 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
6044 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6045 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
6051 /* Build an expression representing a cast to type TYPE of expression EXPR.
6052 LOC is the location of the cast-- typically the open paren of the cast. */
6055 build_c_cast (location_t loc, tree type, tree expr)
6059 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
6061 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
6062 expr = TREE_OPERAND (expr, 0);
6066 value = remove_c_maybe_const_expr (value);
6068 if (type == error_mark_node || expr == error_mark_node)
6069 return error_mark_node;
6071 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
6072 only in <protocol> qualifications. But when constructing cast expressions,
6073 the protocols do matter and must be kept around. */
6074 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
6075 return build1 (NOP_EXPR, type, expr);
6077 type = TYPE_MAIN_VARIANT (type);
6079 if (TREE_CODE (type) == ARRAY_TYPE)
6081 error_at (loc, "cast specifies array type");
6082 return error_mark_node;
6085 if (TREE_CODE (type) == FUNCTION_TYPE)
6087 error_at (loc, "cast specifies function type");
6088 return error_mark_node;
6091 if (!VOID_TYPE_P (type))
6093 value = require_complete_type (loc, value);
6094 if (value == error_mark_node)
6095 return error_mark_node;
6098 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
6100 if (RECORD_OR_UNION_TYPE_P (type))
6101 pedwarn (loc, OPT_Wpedantic,
6102 "ISO C forbids casting nonscalar to the same type");
6104 /* Convert to remove any qualifiers from VALUE's type. */
6105 value = convert (type, value);
6107 else if (TREE_CODE (type) == UNION_TYPE)
6111 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6112 if (TREE_TYPE (field) != error_mark_node
6113 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
6114 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
6120 bool maybe_const = true;
6122 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
6123 t = c_fully_fold (value, false, &maybe_const);
6124 t = build_constructor_single (type, field, t);
6126 t = c_wrap_maybe_const (t, true);
6127 t = digest_init (loc, type, t,
6128 NULL_TREE, false, false, false, true, false, false);
6129 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6132 error_at (loc, "cast to union type from type not present in union");
6133 return error_mark_node;
6139 if (type == void_type_node)
6141 tree t = build1 (CONVERT_EXPR, type, value);
6142 SET_EXPR_LOCATION (t, loc);
6146 otype = TREE_TYPE (value);
6148 /* Optionally warn about potentially worrisome casts. */
6150 && TREE_CODE (type) == POINTER_TYPE
6151 && TREE_CODE (otype) == POINTER_TYPE)
6152 handle_warn_cast_qual (loc, type, otype);
6154 /* Warn about conversions between pointers to disjoint
6156 if (TREE_CODE (type) == POINTER_TYPE
6157 && TREE_CODE (otype) == POINTER_TYPE
6158 && !null_pointer_constant_p (value))
6160 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6161 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6162 addr_space_t as_common;
6164 if (!addr_space_superset (as_to, as_from, &as_common))
6166 if (ADDR_SPACE_GENERIC_P (as_from))
6167 warning_at (loc, 0, "cast to %qs address space pointer "
6168 "from disjoint generic address space pointer",
6169 c_addr_space_name (as_to));
6171 else if (ADDR_SPACE_GENERIC_P (as_to))
6172 warning_at (loc, 0, "cast to generic address space pointer "
6173 "from disjoint %qs address space pointer",
6174 c_addr_space_name (as_from));
6177 warning_at (loc, 0, "cast to %qs address space pointer "
6178 "from disjoint %qs address space pointer",
6179 c_addr_space_name (as_to),
6180 c_addr_space_name (as_from));
6184 /* Warn about possible alignment problems. */
6185 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6186 && TREE_CODE (type) == POINTER_TYPE
6187 && TREE_CODE (otype) == POINTER_TYPE
6188 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6189 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6190 /* Don't warn about opaque types, where the actual alignment
6191 restriction is unknown. */
6192 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6193 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6194 && min_align_of_type (TREE_TYPE (type))
6195 > min_align_of_type (TREE_TYPE (otype)))
6196 warning_at (loc, OPT_Wcast_align,
6197 "cast increases required alignment of target type");
6199 if (TREE_CODE (type) == INTEGER_TYPE
6200 && TREE_CODE (otype) == POINTER_TYPE
6201 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6202 /* Unlike conversion of integers to pointers, where the
6203 warning is disabled for converting constants because
6204 of cases such as SIG_*, warn about converting constant
6205 pointers to integers. In some cases it may cause unwanted
6206 sign extension, and a warning is appropriate. */
6207 warning_at (loc, OPT_Wpointer_to_int_cast,
6208 "cast from pointer to integer of different size");
6210 if (TREE_CODE (value) == CALL_EXPR
6211 && TREE_CODE (type) != TREE_CODE (otype))
6212 warning_at (loc, OPT_Wbad_function_cast,
6213 "cast from function call of type %qT "
6214 "to non-matching type %qT", otype, type);
6216 if (TREE_CODE (type) == POINTER_TYPE
6217 && TREE_CODE (otype) == INTEGER_TYPE
6218 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6219 /* Don't warn about converting any constant. */
6220 && !TREE_CONSTANT (value))
6222 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6223 "of different size");
6225 if (warn_strict_aliasing <= 2)
6226 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6228 /* If pedantic, warn for conversions between function and object
6229 pointer types, except for converting a null pointer constant
6230 to function pointer type. */
6232 && TREE_CODE (type) == POINTER_TYPE
6233 && TREE_CODE (otype) == POINTER_TYPE
6234 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6235 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6236 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6237 "conversion of function pointer to object pointer type");
6240 && TREE_CODE (type) == POINTER_TYPE
6241 && TREE_CODE (otype) == POINTER_TYPE
6242 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6243 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6244 && !null_pointer_constant_p (value))
6245 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6246 "conversion of object pointer to function pointer type");
6248 if (TREE_CODE (type) == POINTER_TYPE
6249 && TREE_CODE (otype) == POINTER_TYPE
6250 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6251 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6252 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6254 warning_at (loc, OPT_Wcast_function_type,
6255 "cast between incompatible function types"
6256 " from %qT to %qT", otype, type);
6259 value = convert (type, value);
6261 /* Ignore any integer overflow caused by the cast. */
6262 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6264 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6266 if (!TREE_OVERFLOW (value))
6268 /* Avoid clobbering a shared constant. */
6269 value = copy_node (value);
6270 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6273 else if (TREE_OVERFLOW (value))
6274 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6275 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6279 /* Don't let a cast be an lvalue. */
6280 if (lvalue_p (value))
6281 value = non_lvalue_loc (loc, value);
6283 /* Don't allow the results of casting to floating-point or complex
6284 types be confused with actual constants, or casts involving
6285 integer and pointer types other than direct integer-to-integer
6286 and integer-to-pointer be confused with integer constant
6287 expressions and null pointer constants. */
6288 if (TREE_CODE (value) == REAL_CST
6289 || TREE_CODE (value) == COMPLEX_CST
6290 || (TREE_CODE (value) == INTEGER_CST
6291 && !((TREE_CODE (expr) == INTEGER_CST
6292 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6293 || TREE_CODE (expr) == REAL_CST
6294 || TREE_CODE (expr) == COMPLEX_CST)))
6295 value = build1 (NOP_EXPR, type, value);
6297 /* If the expression has integer operands and so can occur in an
6298 unevaluated part of an integer constant expression, ensure the
6299 return value reflects this. */
6301 && INTEGRAL_TYPE_P (type)
6302 && value != error_mark_node
6303 && !EXPR_INT_CONST_OPERANDS (value))
6304 value = note_integer_operands (value);
6306 protected_set_expr_location (value, loc);
6310 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6311 location of the open paren of the cast, or the position of the cast
6314 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6317 tree type_expr = NULL_TREE;
6318 bool type_expr_const = true;
6320 int saved_wsp = warn_strict_prototypes;
6322 /* This avoids warnings about unprototyped casts on
6323 integers. E.g. "#define SIG_DFL (void(*)())0". */
6324 if (TREE_CODE (expr) == INTEGER_CST)
6325 warn_strict_prototypes = 0;
6326 type = groktypename (type_name, &type_expr, &type_expr_const);
6327 warn_strict_prototypes = saved_wsp;
6329 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6330 && reject_gcc_builtin (expr))
6331 return error_mark_node;
6333 ret = build_c_cast (loc, type, expr);
6336 bool inner_expr_const = true;
6337 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6338 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6339 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6340 && inner_expr_const);
6341 SET_EXPR_LOCATION (ret, loc);
6344 if (!EXPR_HAS_LOCATION (ret))
6345 protected_set_expr_location (ret, loc);
6347 /* C++ does not permits types to be defined in a cast, but it
6348 allows references to incomplete types. */
6349 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6350 warning_at (loc, OPT_Wc___compat,
6351 "defining a type in a cast is invalid in C++");
6356 /* Build an assignment expression of lvalue LHS from value RHS.
6357 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6358 may differ from TREE_TYPE (LHS) for an enum bitfield.
6359 MODIFYCODE is the code for a binary operator that we use
6360 to combine the old value of LHS with RHS to get the new value.
6361 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6362 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6363 which may differ from TREE_TYPE (RHS) for an enum value.
6365 LOCATION is the location of the MODIFYCODE operator.
6366 RHS_LOC is the location of the RHS. */
6369 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6370 enum tree_code modifycode,
6371 location_t rhs_loc, tree rhs, tree rhs_origtype)
6375 tree rhseval = NULL_TREE;
6376 tree lhstype = TREE_TYPE (lhs);
6377 tree olhstype = lhstype;
6381 /* Types that aren't fully specified cannot be used in assignments. */
6382 lhs = require_complete_type (location, lhs);
6384 /* Avoid duplicate error messages from operands that had errors. */
6385 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6386 return error_mark_node;
6388 /* Ensure an error for assigning a non-lvalue array to an array in
6390 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6392 error_at (location, "assignment to expression with array type");
6393 return error_mark_node;
6396 /* For ObjC properties, defer this check. */
6397 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6398 return error_mark_node;
6400 is_atomic_op = really_atomic_lvalue (lhs);
6404 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6406 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6407 lhs_origtype, modifycode, rhs_loc, rhs,
6409 if (inner == error_mark_node)
6410 return error_mark_node;
6411 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6412 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6413 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6414 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6415 protected_set_expr_location (result, location);
6419 /* If a binary op has been requested, combine the old LHS value with the RHS
6420 producing the value we should actually store into the LHS. */
6422 if (modifycode != NOP_EXPR)
6424 lhs = c_fully_fold (lhs, false, NULL, true);
6425 lhs = stabilize_reference (lhs);
6427 /* Construct the RHS for any non-atomic compound assignemnt. */
6430 /* If in LHS op= RHS the RHS has side-effects, ensure they
6431 are preevaluated before the rest of the assignment expression's
6432 side-effects, because RHS could contain e.g. function calls
6434 if (TREE_SIDE_EFFECTS (rhs))
6436 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6437 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6439 newrhs = save_expr (rhs);
6441 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6442 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6445 newrhs = build_binary_op (location,
6446 modifycode, lhs, newrhs, true);
6448 /* The original type of the right hand side is no longer
6450 rhs_origtype = NULL_TREE;
6454 if (c_dialect_objc ())
6456 /* Check if we are modifying an Objective-C property reference;
6457 if so, we need to generate setter calls. */
6458 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6459 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6461 result = objc_maybe_build_modify_expr (lhs, newrhs);
6465 /* Else, do the check that we postponed for Objective-C. */
6466 if (!lvalue_or_else (location, lhs, lv_assign))
6467 return error_mark_node;
6470 /* Give an error for storing in something that is 'const'. */
6472 if (TYPE_READONLY (lhstype)
6473 || (RECORD_OR_UNION_TYPE_P (lhstype)
6474 && C_TYPE_FIELDS_READONLY (lhstype)))
6476 readonly_error (location, lhs, lv_assign);
6477 return error_mark_node;
6479 else if (TREE_READONLY (lhs))
6480 readonly_warning (lhs, lv_assign);
6482 /* If storing into a structure or union member,
6483 it has probably been given type `int'.
6484 Compute the type that would go with
6485 the actual amount of storage the member occupies. */
6487 if (TREE_CODE (lhs) == COMPONENT_REF
6488 && (TREE_CODE (lhstype) == INTEGER_TYPE
6489 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6490 || TREE_CODE (lhstype) == REAL_TYPE
6491 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6492 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6494 /* If storing in a field that is in actuality a short or narrower than one,
6495 we must store in the field in its actual type. */
6497 if (lhstype != TREE_TYPE (lhs))
6499 lhs = copy_node (lhs);
6500 TREE_TYPE (lhs) = lhstype;
6503 /* Issue -Wc++-compat warnings about an assignment to an enum type
6504 when LHS does not have its original type. This happens for,
6505 e.g., an enum bitfield in a struct. */
6507 && lhs_origtype != NULL_TREE
6508 && lhs_origtype != lhstype
6509 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6511 tree checktype = (rhs_origtype != NULL_TREE
6514 if (checktype != error_mark_node
6515 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6516 || (is_atomic_op && modifycode != NOP_EXPR)))
6517 warning_at (location, OPT_Wc___compat,
6518 "enum conversion in assignment is invalid in C++");
6521 /* Remove qualifiers. */
6522 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6523 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6525 /* Convert new value to destination type. Fold it first, then
6526 restore any excess precision information, for the sake of
6527 conversion warnings. */
6529 if (!(is_atomic_op && modifycode != NOP_EXPR))
6531 tree rhs_semantic_type = NULL_TREE;
6534 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6536 rhs_semantic_type = TREE_TYPE (newrhs);
6537 newrhs = TREE_OPERAND (newrhs, 0);
6539 npc = null_pointer_constant_p (newrhs);
6540 newrhs = c_fully_fold (newrhs, false, NULL);
6541 if (rhs_semantic_type)
6542 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6545 npc = null_pointer_constant_p (newrhs);
6546 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6547 rhs_origtype, ic_assign, npc,
6548 NULL_TREE, NULL_TREE, 0);
6549 if (TREE_CODE (newrhs) == ERROR_MARK)
6550 return error_mark_node;
6553 /* Emit ObjC write barrier, if necessary. */
6554 if (c_dialect_objc () && flag_objc_gc)
6556 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6559 protected_set_expr_location (result, location);
6564 /* Scan operands. */
6567 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6570 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6571 TREE_SIDE_EFFECTS (result) = 1;
6572 protected_set_expr_location (result, location);
6575 /* If we got the LHS in a different type for storing in,
6576 convert the result back to the nominal type of LHS
6577 so that the value we return always has the same type
6578 as the LHS argument. */
6580 if (olhstype == TREE_TYPE (result))
6583 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6584 rhs_origtype, ic_assign, false, NULL_TREE,
6586 protected_set_expr_location (result, location);
6590 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6594 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6595 This is used to implement -fplan9-extensions. */
6598 find_anonymous_field_with_type (tree struct_type, tree type)
6603 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6605 for (field = TYPE_FIELDS (struct_type);
6607 field = TREE_CHAIN (field))
6609 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6610 ? c_build_qualified_type (TREE_TYPE (field),
6612 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6613 if (DECL_NAME (field) == NULL
6614 && comptypes (type, fieldtype))
6620 else if (DECL_NAME (field) == NULL
6621 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6622 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6632 /* RHS is an expression whose type is pointer to struct. If there is
6633 an anonymous field in RHS with type TYPE, then return a pointer to
6634 that field in RHS. This is used with -fplan9-extensions. This
6635 returns NULL if no conversion could be found. */
6638 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6640 tree rhs_struct_type, lhs_main_type;
6641 tree field, found_field;
6642 bool found_sub_field;
6645 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6646 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6647 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6649 gcc_assert (POINTER_TYPE_P (type));
6650 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6651 ? c_build_qualified_type (TREE_TYPE (type),
6653 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6655 found_field = NULL_TREE;
6656 found_sub_field = false;
6657 for (field = TYPE_FIELDS (rhs_struct_type);
6659 field = TREE_CHAIN (field))
6661 if (DECL_NAME (field) != NULL_TREE
6662 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6664 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6665 ? c_build_qualified_type (TREE_TYPE (field),
6667 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6668 if (comptypes (lhs_main_type, fieldtype))
6670 if (found_field != NULL_TREE)
6672 found_field = field;
6674 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6677 if (found_field != NULL_TREE)
6679 found_field = field;
6680 found_sub_field = true;
6684 if (found_field == NULL_TREE)
6687 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6688 build_fold_indirect_ref (rhs), found_field,
6690 ret = build_fold_addr_expr_loc (location, ret);
6692 if (found_sub_field)
6694 ret = convert_to_anonymous_field (location, type, ret);
6695 gcc_assert (ret != NULL_TREE);
6701 /* Issue an error message for a bad initializer component.
6702 GMSGID identifies the message.
6703 The component name is taken from the spelling stack. */
6705 static void ATTRIBUTE_GCC_DIAG (2,0)
6706 error_init (location_t loc, const char *gmsgid, ...)
6710 auto_diagnostic_group d;
6712 /* The gmsgid may be a format string with %< and %>. */
6714 va_start (ap, gmsgid);
6715 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6718 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6719 if (*ofwhat && warned)
6720 inform (loc, "(near initialization for %qs)", ofwhat);
6723 /* Issue a pedantic warning for a bad initializer component. OPT is
6724 the option OPT_* (from options.h) controlling this warning or 0 if
6725 it is unconditionally given. GMSGID identifies the message. The
6726 component name is taken from the spelling stack. */
6728 static void ATTRIBUTE_GCC_DIAG (3,0)
6729 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6731 /* Use the location where a macro was expanded rather than where
6732 it was defined to make sure macros defined in system headers
6733 but used incorrectly elsewhere are diagnosed. */
6734 location_t exploc = expansion_point_location_if_in_system_header (loc);
6735 auto_diagnostic_group d;
6737 va_start (ap, gmsgid);
6738 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6740 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6741 if (*ofwhat && warned)
6742 inform (exploc, "(near initialization for %qs)", ofwhat);
6745 /* Issue a warning for a bad initializer component.
6747 OPT is the OPT_W* value corresponding to the warning option that
6748 controls this warning. GMSGID identifies the message. The
6749 component name is taken from the spelling stack. */
6752 warning_init (location_t loc, int opt, const char *gmsgid)
6757 auto_diagnostic_group d;
6759 /* Use the location where a macro was expanded rather than where
6760 it was defined to make sure macros defined in system headers
6761 but used incorrectly elsewhere are diagnosed. */
6762 location_t exploc = expansion_point_location_if_in_system_header (loc);
6764 /* The gmsgid may be a format string with %< and %>. */
6765 warned = warning_at (exploc, opt, gmsgid);
6766 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6767 if (*ofwhat && warned)
6768 inform (exploc, "(near initialization for %qs)", ofwhat);
6771 /* If TYPE is an array type and EXPR is a parenthesized string
6772 constant, warn if pedantic that EXPR is being used to initialize an
6773 object of type TYPE. */
6776 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6779 && TREE_CODE (type) == ARRAY_TYPE
6780 && TREE_CODE (expr.value) == STRING_CST
6781 && expr.original_code != STRING_CST)
6782 pedwarn_init (loc, OPT_Wpedantic,
6783 "array initialized from parenthesized string constant");
6786 /* Attempt to locate the parameter with the given index within FNDECL,
6787 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6790 get_fndecl_argument_location (tree fndecl, int argnum)
6795 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6796 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6797 i < argnum && param;
6798 i++, param = TREE_CHAIN (param))
6801 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6802 return DECL_SOURCE_LOCATION (FNDECL). */
6804 return DECL_SOURCE_LOCATION (fndecl);
6806 return DECL_SOURCE_LOCATION (param);
6809 /* Issue a note about a mismatching argument for parameter PARMNUM
6810 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6811 Attempt to issue the note at the pertinent parameter of the decl;
6812 failing that issue it at the location of FUNDECL; failing that
6813 issue it at PLOC. */
6816 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6817 tree expected_type, tree actual_type)
6820 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6821 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6826 "expected %qT but argument is of type %qT",
6827 expected_type, actual_type);
6830 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6831 function FUNDECL declared without prototype to parameter PARMNUM of
6832 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6835 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6836 tree parmtype, tree argtype)
6838 tree_code parmcode = TREE_CODE (parmtype);
6839 tree_code argcode = TREE_CODE (argtype);
6840 tree promoted = c_type_promotes_to (argtype);
6842 /* Avoid warning for enum arguments that promote to an integer type
6843 of the same size/mode. */
6844 if (parmcode == INTEGER_TYPE
6845 && argcode == ENUMERAL_TYPE
6846 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6849 if ((parmcode == argcode
6850 || (parmcode == INTEGER_TYPE
6851 && argcode == ENUMERAL_TYPE))
6852 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6855 /* This diagnoses even signed/unsigned mismatches. Those might be
6856 safe in many cases but GCC may emit suboptimal code for them so
6857 warning on those cases drives efficiency improvements. */
6858 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6859 TYPE_MAIN_VARIANT (promoted) == argtype
6860 ? G_("%qD argument %d type is %qT where %qT is expected "
6861 "in a call to built-in function declared without "
6863 : G_("%qD argument %d promotes to %qT where %qT is expected "
6864 "in a call to built-in function declared without "
6866 fundecl, parmnum, promoted, parmtype))
6867 inform (DECL_SOURCE_LOCATION (fundecl),
6868 "built-in %qD declared here",
6872 /* Convert value RHS to type TYPE as preparation for an assignment to
6873 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6874 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6875 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6876 constant before any folding.
6877 The real work of conversion is done by `convert'.
6878 The purpose of this function is to generate error messages
6879 for assignments that are not allowed in C.
6880 ERRTYPE says whether it is argument passing, assignment,
6881 initialization or return.
6883 In the following example, '~' denotes where EXPR_LOC and '^' where
6886 f (var); [ic_argpass]
6888 x = var; [ic_assign]
6890 int x = var; [ic_init]
6892 return x; [ic_return]
6895 FUNCTION is a tree for the function being called.
6896 PARMNUM is the number of the argument, for printing in error messages.
6897 WARNOPT may be set to a warning option to issue the corresponding warning
6898 rather than an error for invalid conversions. Used for calls to built-in
6899 functions declared without a prototype. */
6902 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6903 tree rhs, tree origtype, enum impl_conv errtype,
6904 bool null_pointer_constant, tree fundecl,
6905 tree function, int parmnum, int warnopt /* = 0 */)
6907 enum tree_code codel = TREE_CODE (type);
6908 tree orig_rhs = rhs;
6910 enum tree_code coder;
6911 tree rname = NULL_TREE;
6912 bool objc_ok = false;
6914 /* Use the expansion point location to handle cases such as user's
6915 function returning a wrong-type macro defined in a system header. */
6916 location = expansion_point_location_if_in_system_header (location);
6918 if (errtype == ic_argpass)
6921 /* Change pointer to function to the function itself for
6923 if (TREE_CODE (function) == ADDR_EXPR
6924 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6925 function = TREE_OPERAND (function, 0);
6927 /* Handle an ObjC selector specially for diagnostics. */
6928 selector = objc_message_selector ();
6930 if (selector && parmnum > 2)
6937 /* This macro is used to emit diagnostics to ensure that all format
6938 strings are complete sentences, visible to gettext and checked at
6940 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6946 auto_diagnostic_group d; \
6947 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6948 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6952 pedwarn (LOCATION, OPT, AS); \
6955 case ic_init_const: \
6956 pedwarn_init (LOCATION, OPT, IN); \
6959 pedwarn (LOCATION, OPT, RE); \
6962 gcc_unreachable (); \
6966 /* This macro is used to emit diagnostics to ensure that all format
6967 strings are complete sentences, visible to gettext and checked at
6968 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6969 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6975 auto_diagnostic_group d; \
6977 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6978 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6980 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6981 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6987 pedwarn (LOCATION, OPT, AS, QUALS); \
6989 warning_at (LOCATION, OPT, AS, QUALS); \
6992 case ic_init_const: \
6994 pedwarn (LOCATION, OPT, IN, QUALS); \
6996 warning_at (LOCATION, OPT, IN, QUALS); \
7000 pedwarn (LOCATION, OPT, RE, QUALS); \
7002 warning_at (LOCATION, OPT, RE, QUALS); \
7005 gcc_unreachable (); \
7009 /* This macro is used to emit diagnostics to ensure that all format
7010 strings are complete sentences, visible to gettext and checked at
7011 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
7012 extra parameter to enumerate qualifiers. */
7013 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7014 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
7017 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7018 rhs = TREE_OPERAND (rhs, 0);
7020 rhstype = TREE_TYPE (rhs);
7021 coder = TREE_CODE (rhstype);
7023 if (coder == ERROR_MARK)
7024 return error_mark_node;
7026 if (c_dialect_objc ())
7050 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
7053 if (warn_cxx_compat)
7055 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7056 if (checktype != error_mark_node
7057 && TREE_CODE (type) == ENUMERAL_TYPE
7058 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7062 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
7063 "passing argument %d of %qE is invalid in C++",
7065 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
7066 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
7067 "expected %qT but argument is of type %qT",
7071 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7072 "%qT in assignment is invalid in C++", rhstype, type);
7076 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
7077 "%qT to %qT in initialization is invalid in C++",
7081 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7082 "%qT in return is invalid in C++", rhstype, type);
7089 if (warn_enum_conversion)
7091 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7092 if (checktype != error_mark_node
7093 && TREE_CODE (checktype) == ENUMERAL_TYPE
7094 && TREE_CODE (type) == ENUMERAL_TYPE
7095 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7097 gcc_rich_location loc (location);
7098 warning_at (&loc, OPT_Wenum_conversion,
7099 "implicit conversion from %qT to %qT",
7104 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
7106 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7110 if (coder == VOID_TYPE)
7112 /* Except for passing an argument to an unprototyped function,
7113 this is a constraint violation. When passing an argument to
7114 an unprototyped function, it is compile-time undefined;
7115 making it a constraint in that case was rejected in
7117 const char msg[] = "void value not ignored as it ought to be";
7119 warning_at (location, warnopt, msg);
7121 error_at (location, msg);
7122 return error_mark_node;
7124 rhs = require_complete_type (location, rhs);
7125 if (rhs == error_mark_node)
7126 return error_mark_node;
7128 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
7129 return error_mark_node;
7131 /* A non-reference type can convert to a reference. This handles
7132 va_start, va_copy and possibly port built-ins. */
7133 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7135 if (!lvalue_p (rhs))
7137 const char msg[] = "cannot pass rvalue to reference parameter";
7139 warning_at (location, warnopt, msg);
7141 error_at (location, msg);
7142 return error_mark_node;
7144 if (!c_mark_addressable (rhs))
7145 return error_mark_node;
7146 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7147 SET_EXPR_LOCATION (rhs, location);
7149 rhs = convert_for_assignment (location, expr_loc,
7150 build_pointer_type (TREE_TYPE (type)),
7151 rhs, origtype, errtype,
7152 null_pointer_constant, fundecl, function,
7154 if (rhs == error_mark_node)
7155 return error_mark_node;
7157 rhs = build1 (NOP_EXPR, type, rhs);
7158 SET_EXPR_LOCATION (rhs, location);
7161 /* Some types can interconvert without explicit casts. */
7162 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7163 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7164 return convert (type, rhs);
7165 /* Arithmetic types all interconvert, and enum is treated like int. */
7166 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7167 || codel == FIXED_POINT_TYPE
7168 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7169 || codel == BOOLEAN_TYPE)
7170 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7171 || coder == FIXED_POINT_TYPE
7172 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7173 || coder == BOOLEAN_TYPE))
7175 if (warnopt && errtype == ic_argpass)
7176 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7179 bool save = in_late_binary_op;
7180 if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
7181 || (coder == REAL_TYPE
7182 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7183 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7184 in_late_binary_op = true;
7185 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7186 ? expr_loc : location, type, orig_rhs,
7187 errtype == ic_init_const);
7188 in_late_binary_op = save;
7192 /* Aggregates in different TUs might need conversion. */
7193 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7195 && comptypes (type, rhstype))
7196 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7197 ? expr_loc : location, type, rhs);
7199 /* Conversion to a transparent union or record from its member types.
7200 This applies only to function arguments. */
7201 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7202 && TYPE_TRANSPARENT_AGGR (type))
7203 && errtype == ic_argpass)
7205 tree memb, marginal_memb = NULL_TREE;
7207 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7209 tree memb_type = TREE_TYPE (memb);
7211 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7212 TYPE_MAIN_VARIANT (rhstype)))
7215 if (TREE_CODE (memb_type) != POINTER_TYPE)
7218 if (coder == POINTER_TYPE)
7220 tree ttl = TREE_TYPE (memb_type);
7221 tree ttr = TREE_TYPE (rhstype);
7223 /* Any non-function converts to a [const][volatile] void *
7224 and vice versa; otherwise, targets must be the same.
7225 Meanwhile, the lhs target must have all the qualifiers of
7227 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7228 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7229 || comp_target_types (location, memb_type, rhstype))
7231 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7232 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7233 /* If this type won't generate any warnings, use it. */
7234 if (lquals == rquals
7235 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7236 && TREE_CODE (ttl) == FUNCTION_TYPE)
7237 ? ((lquals | rquals) == rquals)
7238 : ((lquals | rquals) == lquals)))
7241 /* Keep looking for a better type, but remember this one. */
7243 marginal_memb = memb;
7247 /* Can convert integer zero to any pointer type. */
7248 if (null_pointer_constant)
7250 rhs = null_pointer_node;
7255 if (memb || marginal_memb)
7259 /* We have only a marginally acceptable member type;
7260 it needs a warning. */
7261 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7262 tree ttr = TREE_TYPE (rhstype);
7264 /* Const and volatile mean something different for function
7265 types, so the usual warnings are not appropriate. */
7266 if (TREE_CODE (ttr) == FUNCTION_TYPE
7267 && TREE_CODE (ttl) == FUNCTION_TYPE)
7269 /* Because const and volatile on functions are
7270 restrictions that say the function will not do
7271 certain things, it is okay to use a const or volatile
7272 function where an ordinary one is wanted, but not
7274 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7275 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7276 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7277 OPT_Wdiscarded_qualifiers,
7278 G_("passing argument %d of %qE "
7279 "makes %q#v qualified function "
7280 "pointer from unqualified"),
7281 G_("assignment makes %q#v qualified "
7282 "function pointer from "
7284 G_("initialization makes %q#v qualified "
7285 "function pointer from "
7287 G_("return makes %q#v qualified function "
7288 "pointer from unqualified"),
7289 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7291 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7292 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7293 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7294 OPT_Wdiscarded_qualifiers,
7295 G_("passing argument %d of %qE discards "
7296 "%qv qualifier from pointer target type"),
7297 G_("assignment discards %qv qualifier "
7298 "from pointer target type"),
7299 G_("initialization discards %qv qualifier "
7300 "from pointer target type"),
7301 G_("return discards %qv qualifier from "
7302 "pointer target type"),
7303 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7305 memb = marginal_memb;
7308 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7309 pedwarn (location, OPT_Wpedantic,
7310 "ISO C prohibits argument conversion to union type");
7312 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7313 return build_constructor_single (type, memb, rhs);
7317 /* Conversions among pointers */
7318 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7319 && (coder == codel))
7321 /* If RHS refers to a built-in declared without a prototype
7322 BLTIN is the declaration of the built-in with a prototype
7323 and RHSTYPE is set to the actual type of the built-in. */
7325 rhstype = type_or_builtin_type (rhs, &bltin);
7327 tree ttl = TREE_TYPE (type);
7328 tree ttr = TREE_TYPE (rhstype);
7331 bool is_opaque_pointer;
7332 int target_cmp = 0; /* Cache comp_target_types () result. */
7336 if (TREE_CODE (mvl) != ARRAY_TYPE)
7337 mvl = (TYPE_ATOMIC (mvl)
7338 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7340 : TYPE_MAIN_VARIANT (mvl));
7341 if (TREE_CODE (mvr) != ARRAY_TYPE)
7342 mvr = (TYPE_ATOMIC (mvr)
7343 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7345 : TYPE_MAIN_VARIANT (mvr));
7346 /* Opaque pointers are treated like void pointers. */
7347 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7349 /* The Plan 9 compiler permits a pointer to a struct to be
7350 automatically converted into a pointer to an anonymous field
7351 within the struct. */
7352 if (flag_plan9_extensions
7353 && RECORD_OR_UNION_TYPE_P (mvl)
7354 && RECORD_OR_UNION_TYPE_P (mvr)
7357 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7358 if (new_rhs != NULL_TREE)
7361 rhstype = TREE_TYPE (rhs);
7362 coder = TREE_CODE (rhstype);
7363 ttr = TREE_TYPE (rhstype);
7364 mvr = TYPE_MAIN_VARIANT (ttr);
7368 /* C++ does not allow the implicit conversion void* -> T*. However,
7369 for the purpose of reducing the number of false positives, we
7370 tolerate the special case of
7374 where NULL is typically defined in C to be '(void *) 0'. */
7375 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7376 warning_at (errtype == ic_argpass ? expr_loc : location,
7378 "request for implicit conversion "
7379 "from %qT to %qT not permitted in C++", rhstype, type);
7381 /* See if the pointers point to incompatible address spaces. */
7382 asl = TYPE_ADDR_SPACE (ttl);
7383 asr = TYPE_ADDR_SPACE (ttr);
7384 if (!null_pointer_constant_p (rhs)
7385 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7387 auto_diagnostic_group d;
7388 bool diagnosed = true;
7393 const char msg[] = G_("passing argument %d of %qE from "
7394 "pointer to non-enclosed address space");
7397 = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7399 error_at (expr_loc, msg, parmnum, rname);
7404 const char msg[] = G_("assignment from pointer to "
7405 "non-enclosed address space");
7407 diagnosed = warning_at (location, warnopt, msg);
7409 error_at (location, msg);
7415 const char msg[] = G_("initialization from pointer to "
7416 "non-enclosed address space");
7418 diagnosed = warning_at (location, warnopt, msg);
7420 error_at (location, msg);
7425 const char msg[] = G_("return from pointer to "
7426 "non-enclosed address space");
7428 diagnosed = warning_at (location, warnopt, msg);
7430 error_at (location, msg);
7438 if (errtype == ic_argpass)
7439 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7441 inform (location, "expected %qT but pointer is of type %qT",
7444 return error_mark_node;
7447 /* Check if the right-hand side has a format attribute but the
7448 left-hand side doesn't. */
7449 if (warn_suggest_attribute_format
7450 && check_missing_format_attribute (type, rhstype))
7455 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7456 "argument %d of %qE might be "
7457 "a candidate for a format attribute",
7461 warning_at (location, OPT_Wsuggest_attribute_format,
7462 "assignment left-hand side might be "
7463 "a candidate for a format attribute");
7467 warning_at (location, OPT_Wsuggest_attribute_format,
7468 "initialization left-hand side might be "
7469 "a candidate for a format attribute");
7472 warning_at (location, OPT_Wsuggest_attribute_format,
7473 "return type might be "
7474 "a candidate for a format attribute");
7481 /* See if the pointers point to incompatible scalar storage orders. */
7482 if (warn_scalar_storage_order
7483 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7484 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7491 /* Do not warn for built-in functions, for example memcpy, since we
7492 control how they behave and they can be useful in this area. */
7493 if (TREE_CODE (rname) != FUNCTION_DECL
7494 || !fndecl_built_in_p (rname))
7495 warning_at (location, OPT_Wscalar_storage_order,
7496 "passing argument %d of %qE from incompatible "
7497 "scalar storage order", parmnum, rname);
7500 /* Do not warn if the RHS is a call to a function that returns a
7501 pointer that is not an alias. */
7502 if (TREE_CODE (rhs) != CALL_EXPR
7503 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7504 || !DECL_IS_MALLOC (t))
7505 warning_at (location, OPT_Wscalar_storage_order,
7506 "assignment to %qT from pointer type %qT with "
7507 "incompatible scalar storage order", type, rhstype);
7512 if (TREE_CODE (rhs) != CALL_EXPR
7513 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7514 || !DECL_IS_MALLOC (t))
7515 warning_at (location, OPT_Wscalar_storage_order,
7516 "initialization of %qT from pointer type %qT with "
7517 "incompatible scalar storage order", type, rhstype);
7520 warning_at (location, OPT_Wscalar_storage_order,
7521 "returning %qT from pointer type with incompatible "
7522 "scalar storage order %qT", rhstype, type);
7529 /* Any non-function converts to a [const][volatile] void *
7530 and vice versa; otherwise, targets must be the same.
7531 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7532 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7533 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7534 || (target_cmp = comp_target_types (location, type, rhstype))
7535 || is_opaque_pointer
7536 || ((c_common_unsigned_type (mvl)
7537 == c_common_unsigned_type (mvr))
7538 && (c_common_signed_type (mvl)
7539 == c_common_signed_type (mvr))
7540 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7542 /* Warn about loss of qualifers from pointers to arrays with
7543 qualifiers on the element type. */
7544 if (TREE_CODE (ttr) == ARRAY_TYPE)
7546 ttr = strip_array_types (ttr);
7547 ttl = strip_array_types (ttl);
7549 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7550 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7551 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7553 OPT_Wdiscarded_array_qualifiers,
7554 G_("passing argument %d of %qE discards "
7555 "%qv qualifier from pointer target type"),
7556 G_("assignment discards %qv qualifier "
7557 "from pointer target type"),
7558 G_("initialization discards %qv qualifier "
7559 "from pointer target type"),
7560 G_("return discards %qv qualifier from "
7561 "pointer target type"),
7562 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7565 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7568 && !null_pointer_constant
7569 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7570 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7571 G_("ISO C forbids passing argument %d of "
7572 "%qE between function pointer "
7574 G_("ISO C forbids assignment between "
7575 "function pointer and %<void *%>"),
7576 G_("ISO C forbids initialization between "
7577 "function pointer and %<void *%>"),
7578 G_("ISO C forbids return between function "
7579 "pointer and %<void *%>"));
7580 /* Const and volatile mean something different for function types,
7581 so the usual warnings are not appropriate. */
7582 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7583 && TREE_CODE (ttl) != FUNCTION_TYPE)
7585 /* Assignments between atomic and non-atomic objects are OK. */
7586 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7587 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7588 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7589 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7591 /* Don't warn about loss of qualifier for conversions from
7592 qualified void* to pointers to arrays with corresponding
7593 qualifier on the element type (except for pedantic before C23). */
7594 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7595 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7596 OPT_Wdiscarded_qualifiers,
7597 G_("passing argument %d of %qE discards "
7598 "%qv qualifier from pointer target type"),
7599 G_("assignment discards %qv qualifier "
7600 "from pointer target type"),
7601 G_("initialization discards %qv qualifier "
7602 "from pointer target type"),
7603 G_("return discards %qv qualifier from "
7604 "pointer target type"),
7605 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7606 else if (warn_quals_ped)
7607 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7608 "array with qualifier on the element is not qualified before C2X");
7610 /* If this is not a case of ignoring a mismatch in signedness,
7612 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7615 /* If there is a mismatch, do warn. */
7616 else if (warn_pointer_sign)
7621 auto_diagnostic_group d;
7622 range_label_for_type_mismatch rhs_label (rhstype, type);
7623 gcc_rich_location richloc (expr_loc, &rhs_label);
7624 if (pedwarn (&richloc, OPT_Wpointer_sign,
7625 "pointer targets in passing argument %d of "
7626 "%qE differ in signedness", parmnum, rname))
7627 inform_for_arg (fundecl, expr_loc, parmnum, type,
7632 pedwarn (location, OPT_Wpointer_sign,
7633 "pointer targets in assignment from %qT to %qT "
7634 "differ in signedness", rhstype, type);
7638 pedwarn_init (location, OPT_Wpointer_sign,
7639 "pointer targets in initialization of %qT "
7640 "from %qT differ in signedness", type,
7644 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7645 "returning %qT from a function with return type "
7646 "%qT differ in signedness", rhstype, type);
7652 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7653 && TREE_CODE (ttr) == FUNCTION_TYPE)
7655 /* Because const and volatile on functions are restrictions
7656 that say the function will not do certain things,
7657 it is okay to use a const or volatile function
7658 where an ordinary one is wanted, but not vice-versa. */
7659 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7660 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7661 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7662 OPT_Wdiscarded_qualifiers,
7663 G_("passing argument %d of %qE makes "
7664 "%q#v qualified function pointer "
7665 "from unqualified"),
7666 G_("assignment makes %q#v qualified function "
7667 "pointer from unqualified"),
7668 G_("initialization makes %q#v qualified "
7669 "function pointer from unqualified"),
7670 G_("return makes %q#v qualified function "
7671 "pointer from unqualified"),
7672 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7675 /* Avoid warning about the volatile ObjC EH puts on decls. */
7682 auto_diagnostic_group d;
7683 range_label_for_type_mismatch rhs_label (rhstype, type);
7684 gcc_rich_location richloc (expr_loc, &rhs_label);
7685 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7686 "passing argument %d of %qE from incompatible "
7687 "pointer type", parmnum, rname))
7688 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7693 pedwarn (location, OPT_Wincompatible_pointer_types,
7694 "assignment to %qT from pointer to "
7695 "%qD with incompatible type %qT",
7696 type, bltin, rhstype);
7698 pedwarn (location, OPT_Wincompatible_pointer_types,
7699 "assignment to %qT from incompatible pointer type %qT",
7705 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7706 "initialization of %qT from pointer to "
7707 "%qD with incompatible type %qT",
7708 type, bltin, rhstype);
7710 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7711 "initialization of %qT from incompatible "
7717 pedwarn (location, OPT_Wincompatible_pointer_types,
7718 "returning pointer to %qD of type %qT from "
7719 "a function with incompatible type %qT",
7720 bltin, rhstype, type);
7722 pedwarn (location, OPT_Wincompatible_pointer_types,
7723 "returning %qT from a function with incompatible "
7724 "return type %qT", rhstype, type);
7731 /* If RHS isn't an address, check pointer or array of packed
7733 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7735 return convert (type, rhs);
7737 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7739 /* ??? This should not be an error when inlining calls to
7740 unprototyped functions. */
7741 const char msg[] = "invalid use of non-lvalue array";
7743 warning_at (location, warnopt, msg);
7745 error_at (location, msg);
7746 return error_mark_node;
7748 else if (codel == POINTER_TYPE
7749 && (coder == INTEGER_TYPE || coder == NULLPTR_TYPE))
7751 /* An explicit constant 0 or type nullptr_t can convert to a pointer,
7752 or one that results from arithmetic, even including a cast to
7754 if (!null_pointer_constant && coder != NULLPTR_TYPE)
7759 auto_diagnostic_group d;
7760 range_label_for_type_mismatch rhs_label (rhstype, type);
7761 gcc_rich_location richloc (expr_loc, &rhs_label);
7762 if (pedwarn (&richloc, OPT_Wint_conversion,
7763 "passing argument %d of %qE makes pointer from "
7764 "integer without a cast", parmnum, rname))
7765 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7769 pedwarn (location, OPT_Wint_conversion,
7770 "assignment to %qT from %qT makes pointer from integer "
7771 "without a cast", type, rhstype);
7775 pedwarn_init (location, OPT_Wint_conversion,
7776 "initialization of %qT from %qT makes pointer from "
7777 "integer without a cast", type, rhstype);
7780 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7781 "function with return type %qT makes pointer from "
7782 "integer without a cast", rhstype, type);
7788 return convert (type, rhs);
7790 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7796 auto_diagnostic_group d;
7797 range_label_for_type_mismatch rhs_label (rhstype, type);
7798 gcc_rich_location richloc (expr_loc, &rhs_label);
7799 if (pedwarn (&richloc, OPT_Wint_conversion,
7800 "passing argument %d of %qE makes integer from "
7801 "pointer without a cast", parmnum, rname))
7802 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7806 pedwarn (location, OPT_Wint_conversion,
7807 "assignment to %qT from %qT makes integer from pointer "
7808 "without a cast", type, rhstype);
7812 pedwarn_init (location, OPT_Wint_conversion,
7813 "initialization of %qT from %qT makes integer from "
7814 "pointer without a cast", type, rhstype);
7817 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7818 "function with return type %qT makes integer from "
7819 "pointer without a cast", rhstype, type);
7825 return convert (type, rhs);
7827 else if (C_BOOLEAN_TYPE_P (type)
7828 /* The type nullptr_t may be converted to bool. The
7830 && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
7833 bool save = in_late_binary_op;
7834 in_late_binary_op = true;
7835 ret = convert (type, rhs);
7836 in_late_binary_op = save;
7844 auto_diagnostic_group d;
7845 range_label_for_type_mismatch rhs_label (rhstype, type);
7846 gcc_rich_location richloc (expr_loc, &rhs_label);
7847 const char msg[] = G_("incompatible type for argument %d of %qE");
7849 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7851 error_at (&richloc, msg, parmnum, rname);
7852 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7858 = G_("incompatible types when assigning to type %qT from type %qT");
7860 warning_at (expr_loc, 0, msg, type, rhstype);
7862 error_at (expr_loc, msg, type, rhstype);
7869 = G_("incompatible types when initializing type %qT using type %qT");
7871 warning_at (location, 0, msg, type, rhstype);
7873 error_at (location, msg, type, rhstype);
7879 = G_("incompatible types when returning type %qT but %qT was expected");
7881 warning_at (location, 0, msg, rhstype, type);
7883 error_at (location, msg, rhstype, type);
7890 return error_mark_node;
7893 /* If VALUE is a compound expr all of whose expressions are constant, then
7894 return its value. Otherwise, return error_mark_node.
7896 This is for handling COMPOUND_EXPRs as initializer elements
7897 which is allowed with a warning when -pedantic is specified. */
7900 valid_compound_expr_initializer (tree value, tree endtype)
7902 if (TREE_CODE (value) == COMPOUND_EXPR)
7904 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7906 return error_mark_node;
7907 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7910 else if (!initializer_constant_valid_p (value, endtype))
7911 return error_mark_node;
7916 /* Perform appropriate conversions on the initial value of a variable,
7917 store it in the declaration DECL,
7918 and print any error messages that are appropriate.
7919 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7920 If the init is invalid, store an ERROR_MARK.
7922 INIT_LOC is the location of the initial value. */
7925 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7929 bool int_const_expr = false;
7930 bool arith_const_expr = false;
7932 /* If variable's type was invalidly declared, just ignore it. */
7934 type = TREE_TYPE (decl);
7935 if (TREE_CODE (type) == ERROR_MARK)
7938 /* Digest the specified initializer into an expression. */
7942 npc = null_pointer_constant_p (init);
7943 int_const_expr = (TREE_CODE (init) == INTEGER_CST
7944 && !TREE_OVERFLOW (init)
7945 && INTEGRAL_TYPE_P (TREE_TYPE (init)));
7946 /* Not fully determined before folding. */
7947 arith_const_expr = true;
7949 bool constexpr_p = (TREE_CODE (decl) == VAR_DECL
7950 && C_DECL_DECLARED_CONSTEXPR (decl));
7951 value = digest_init (init_loc, type, init, origtype, npc, int_const_expr,
7952 arith_const_expr, true,
7953 TREE_STATIC (decl) || constexpr_p, constexpr_p);
7955 /* Store the expression if valid; else report error. */
7957 if (!in_system_header_at (input_location)
7958 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7959 warning (OPT_Wtraditional, "traditional C rejects automatic "
7960 "aggregate initialization");
7962 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7963 DECL_INITIAL (decl) = value;
7965 /* ANSI wants warnings about out-of-range constant initializers. */
7966 STRIP_TYPE_NOPS (value);
7967 if (TREE_STATIC (decl))
7968 constant_expression_warning (value);
7970 /* Check if we need to set array size from compound literal size. */
7971 if (TREE_CODE (type) == ARRAY_TYPE
7972 && TYPE_DOMAIN (type) == NULL_TREE
7973 && value != error_mark_node)
7975 tree inside_init = init;
7977 STRIP_TYPE_NOPS (inside_init);
7978 inside_init = fold (inside_init);
7980 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7982 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7984 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7986 /* For int foo[] = (int [3]){1}; we need to set array size
7987 now since later on array initializer will be just the
7988 brace enclosed list of the compound literal. */
7989 tree etype = strip_array_types (TREE_TYPE (decl));
7990 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7991 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7993 layout_decl (cldecl, 0);
7995 = c_build_qualified_type (type, TYPE_QUALS (etype));
8001 /* Methods for storing and printing names for error messages. */
8003 /* Implement a spelling stack that allows components of a name to be pushed
8004 and popped. Each element on the stack is this structure. */
8011 unsigned HOST_WIDE_INT i;
8016 #define SPELLING_STRING 1
8017 #define SPELLING_MEMBER 2
8018 #define SPELLING_BOUNDS 3
8020 static struct spelling *spelling; /* Next stack element (unused). */
8021 static struct spelling *spelling_base; /* Spelling stack base. */
8022 static int spelling_size; /* Size of the spelling stack. */
8024 /* Macros to save and restore the spelling stack around push_... functions.
8025 Alternative to SAVE_SPELLING_STACK. */
8027 #define SPELLING_DEPTH() (spelling - spelling_base)
8028 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
8030 /* Push an element on the spelling stack with type KIND and assign VALUE
8033 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
8035 int depth = SPELLING_DEPTH (); \
8037 if (depth >= spelling_size) \
8039 spelling_size += 10; \
8040 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
8042 RESTORE_SPELLING_DEPTH (depth); \
8045 spelling->kind = (KIND); \
8046 spelling->MEMBER = (VALUE); \
8050 /* Push STRING on the stack. Printed literally. */
8053 push_string (const char *string)
8055 PUSH_SPELLING (SPELLING_STRING, string, u.s);
8058 /* Push a member name on the stack. Printed as '.' STRING. */
8061 push_member_name (tree decl)
8063 const char *const string
8065 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
8066 : _("<anonymous>"));
8067 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
8070 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
8073 push_array_bounds (unsigned HOST_WIDE_INT bounds)
8075 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
8078 /* Compute the maximum size in bytes of the printed spelling. */
8081 spelling_length (void)
8086 for (p = spelling_base; p < spelling; p++)
8088 if (p->kind == SPELLING_BOUNDS)
8091 size += strlen (p->u.s) + 1;
8097 /* Print the spelling to BUFFER and return it. */
8100 print_spelling (char *buffer)
8105 for (p = spelling_base; p < spelling; p++)
8106 if (p->kind == SPELLING_BOUNDS)
8108 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
8114 if (p->kind == SPELLING_MEMBER)
8116 for (s = p->u.s; (*d = *s++); d++)
8123 /* Check whether INIT, a floating or integer constant, is
8124 representable in TYPE, a real floating type with the same radix.
8125 Return true if OK, false if not. */
8127 constexpr_init_fits_real_type (tree type, tree init)
8129 gcc_assert (TREE_CODE (type) == REAL_TYPE);
8130 gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
8131 if (TREE_CODE (init) == REAL_CST
8132 && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
8133 /* Same mode, no conversion required. */
8135 if (TREE_CODE (init) == INTEGER_CST)
8137 tree converted = build_real_from_int_cst (type, init);
8139 wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
8140 TYPE_PRECISION (TREE_TYPE (init)));
8141 return !fail && wi::eq_p (w, wi::to_wide (init));
8143 /* exact_real_truncate is not quite right here, since it doesn't
8144 allow even an exact conversion to subnormal values. */
8146 real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8147 return real_identical (&t, &TREE_REAL_CST (init));
8150 /* Check whether INIT (location LOC) is valid as a 'constexpr'
8151 initializer for type TYPE, and give an error if not. INIT has
8152 already been folded and verified to be constant.
8153 NULL_POINTER_CONSTANT, INT_CONST_EXPR and ARITH_CONST_EXPR say
8154 whether it is a null pointer constant, integer constant expression
8155 or arithmetic constant expression, respectively. If TYPE is not a
8156 scalar type, this function does nothing. */
8159 check_constexpr_init (location_t loc, tree type, tree init,
8160 bool null_pointer_constant, bool int_const_expr,
8161 bool arith_const_expr)
8163 if (POINTER_TYPE_P (type))
8165 /* The initializer must be a null pointer constant. */
8166 if (!null_pointer_constant)
8167 error_at (loc, "%<constexpr%> pointer initializer is not a "
8168 "null pointer constant");
8171 if (INTEGRAL_TYPE_P (type))
8173 /* The initializer must be an integer constant expression,
8174 representable in the target type. */
8175 if (!int_const_expr)
8176 error_at (loc, "%<constexpr%> integer initializer is not an "
8177 "integer constant expression");
8178 if (!int_fits_type_p (init, type))
8179 error_at (loc, "%<constexpr%> initializer not representable in "
8183 /* We don't apply any extra checks to extension types such as vector
8184 or fixed-point types. */
8185 if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
8187 if (!arith_const_expr)
8189 error_at (loc, "%<constexpr%> initializer is not an arithmetic "
8190 "constant expression");
8193 /* We don't apply any extra checks to complex integers. */
8194 if (TREE_CODE (type) == COMPLEX_TYPE
8195 && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8197 /* Both the normative text and the relevant footnote are unclear, as
8198 of the C2x CD, about what exactly counts as a change of value in
8199 floating-point cases. Here, we consider all conversions between
8200 binary and decimal types (even of infinities and NaNs, where
8201 quantum exponents are not involved) as involving a change of
8202 value, and likewise for conversions between real and complex
8203 types (even when the complex constant has imaginary part positive
8204 zero), and conversions of signaling NaN to a different machine
8205 mode. But we allow exact conversions of integers to binary or
8206 decimal floating types, and exact conversions between different
8207 binary types or different decimal types, where "exact" in the
8208 decimal case requires the quantum exponent to be preserved. */
8209 if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
8210 && TREE_CODE (type) == REAL_TYPE)
8212 error_at (loc, "%<constexpr%> initializer for a real type is of "
8216 if (TREE_CODE (type) == COMPLEX_TYPE
8217 && TREE_CODE (TREE_TYPE (init)) != COMPLEX_TYPE)
8219 error_at (loc, "%<constexpr%> initializer for a complex type is of "
8223 if (TREE_CODE (type) == REAL_TYPE
8224 && TREE_CODE (TREE_TYPE (init)) == REAL_TYPE)
8226 if (DECIMAL_FLOAT_TYPE_P (type)
8227 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
8229 error_at (loc, "%<constexpr%> initializer for a decimal "
8230 "floating-point type is of binary type");
8233 else if (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
8234 && !DECIMAL_FLOAT_TYPE_P (type))
8236 error_at (loc, "%<constexpr%> initializer for a binary "
8237 "floating-point type is of decimal type");
8242 if (TREE_CODE (type) == COMPLEX_TYPE)
8244 gcc_assert (TREE_CODE (init) == COMPLEX_CST);
8245 fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
8246 TREE_REALPART (init))
8247 && constexpr_init_fits_real_type (TREE_TYPE (type),
8248 TREE_IMAGPART (init)));
8251 fits = constexpr_init_fits_real_type (type, init);
8253 error_at (loc, "%<constexpr%> initializer not representable in "
8257 /* Digest the parser output INIT as an initializer for type TYPE.
8258 Return a C expression of type TYPE to represent the initial value.
8260 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8262 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
8263 INT_CONST_EXPR is true if INIT is an integer constant expression,
8264 and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
8265 constant expression, false if it has already been determined in the
8266 caller that it is not (but folding may have made the value passed here
8267 indistinguishable from an arithmetic constant expression).
8269 If INIT is a string constant, STRICT_STRING is true if it is
8270 unparenthesized or we should not warn here for it being parenthesized.
8271 For other types of INIT, STRICT_STRING is not used.
8273 INIT_LOC is the location of the INIT.
8275 REQUIRE_CONSTANT requests an error if non-constant initializers or
8276 elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
8277 on initializers for 'constexpr' objects apply. */
8280 digest_init (location_t init_loc, tree type, tree init, tree origtype,
8281 bool null_pointer_constant, bool int_const_expr,
8282 bool arith_const_expr, bool strict_string,
8283 bool require_constant, bool require_constexpr)
8285 enum tree_code code = TREE_CODE (type);
8286 tree inside_init = init;
8287 tree semantic_type = NULL_TREE;
8288 bool maybe_const = true;
8290 if (type == error_mark_node
8292 || error_operand_p (init))
8293 return error_mark_node;
8295 STRIP_TYPE_NOPS (inside_init);
8299 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8301 semantic_type = TREE_TYPE (inside_init);
8302 inside_init = TREE_OPERAND (inside_init, 0);
8304 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8306 /* TODO: this may not detect all cases of expressions folding to
8307 constants that are not arithmetic constant expressions. */
8309 arith_const_expr = false;
8310 else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
8311 && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
8312 && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
8313 arith_const_expr = false;
8314 else if (TREE_CODE (inside_init) != INTEGER_CST
8315 && TREE_CODE (inside_init) != REAL_CST
8316 && TREE_CODE (inside_init) != COMPLEX_CST)
8317 arith_const_expr = false;
8318 else if (TREE_OVERFLOW (inside_init))
8319 arith_const_expr = false;
8321 /* Initialization of an array of chars from a string constant
8322 optionally enclosed in braces. */
8324 if (code == ARRAY_TYPE && inside_init
8325 && TREE_CODE (inside_init) == STRING_CST)
8328 = (TYPE_ATOMIC (TREE_TYPE (type))
8329 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8331 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8332 /* Note that an array could be both an array of character type
8333 and an array of wchar_t if wchar_t is signed char or unsigned
8335 bool char_array = (typ1 == char_type_node
8336 || typ1 == signed_char_type_node
8337 || typ1 == unsigned_char_type_node);
8338 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8339 bool char16_array = !!comptypes (typ1, char16_type_node);
8340 bool char32_array = !!comptypes (typ1, char32_type_node);
8342 if (char_array || wchar_array || char16_array || char32_array)
8345 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8346 bool incompat_string_cst = false;
8347 expr.value = inside_init;
8348 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8349 expr.original_type = NULL;
8351 maybe_warn_string_init (init_loc, type, expr);
8353 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8354 pedwarn_init (init_loc, OPT_Wpedantic,
8355 "initialization of a flexible array member");
8357 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8358 TYPE_MAIN_VARIANT (type)))
8363 if (typ2 != char_type_node && typ2 != char8_type_node)
8364 incompat_string_cst = true;
8366 else if (!comptypes (typ1, typ2))
8367 incompat_string_cst = true;
8369 if (incompat_string_cst)
8371 error_init (init_loc, "cannot initialize array of %qT from "
8372 "a string literal with type array of %qT",
8374 return error_mark_node;
8377 if (require_constexpr
8378 && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
8380 /* Check if all characters of the string can be
8381 represented in the type of the constexpr object being
8383 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8384 const unsigned char *p =
8385 (const unsigned char *) TREE_STRING_POINTER (inside_init);
8386 gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
8387 for (unsigned i = 0; i < len; i++)
8390 error_init (init_loc, "%<constexpr%> initializer not "
8391 "representable in type of object");
8396 if (TYPE_DOMAIN (type) != NULL_TREE
8397 && TYPE_SIZE (type) != NULL_TREE
8398 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8400 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8401 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8403 /* Subtract the size of a single (possibly wide) character
8404 because it's ok to ignore the terminating null char
8405 that is counted in the length of the constant. */
8406 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8407 pedwarn_init (init_loc, 0,
8408 ("initializer-string for array of %qT "
8409 "is too long"), typ1);
8410 else if (warn_cxx_compat
8411 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8412 warning_at (init_loc, OPT_Wc___compat,
8413 ("initializer-string for array of %qT "
8414 "is too long for C++"), typ1);
8415 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8417 unsigned HOST_WIDE_INT size
8418 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8419 const char *p = TREE_STRING_POINTER (inside_init);
8421 inside_init = build_string (size, p);
8425 TREE_TYPE (inside_init) = type;
8428 else if (INTEGRAL_TYPE_P (typ1))
8430 error_init (init_loc, "array of inappropriate type initialized "
8431 "from string constant");
8432 return error_mark_node;
8436 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8437 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8438 below and handle as a constructor. */
8439 if (code == VECTOR_TYPE
8440 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8441 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8442 && TREE_CONSTANT (inside_init))
8444 if (TREE_CODE (inside_init) == VECTOR_CST
8445 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8446 TYPE_MAIN_VARIANT (type)))
8449 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8451 unsigned HOST_WIDE_INT ix;
8453 bool constant_p = true;
8455 /* Iterate through elements and check if all constructor
8456 elements are *_CSTs. */
8457 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8458 if (!CONSTANT_CLASS_P (value))
8465 return build_vector_from_ctor (type,
8466 CONSTRUCTOR_ELTS (inside_init));
8470 if (warn_sequence_point)
8471 verify_sequence_points (inside_init);
8473 /* Any type can be initialized
8474 from an expression of the same type, optionally with braces. */
8476 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8477 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8478 TYPE_MAIN_VARIANT (type))
8479 || (code == ARRAY_TYPE
8480 && comptypes (TREE_TYPE (inside_init), type))
8481 || (gnu_vector_type_p (type)
8482 && comptypes (TREE_TYPE (inside_init), type))
8483 || (code == POINTER_TYPE
8484 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8485 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8486 TREE_TYPE (type)))))
8488 if (code == POINTER_TYPE)
8490 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8492 if (TREE_CODE (inside_init) == STRING_CST
8493 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8494 inside_init = array_to_pointer_conversion
8495 (init_loc, inside_init);
8498 error_init (init_loc, "invalid use of non-lvalue array");
8499 return error_mark_node;
8504 if (code == VECTOR_TYPE)
8505 /* Although the types are compatible, we may require a
8507 inside_init = convert (type, inside_init);
8509 if (require_constant
8510 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8512 /* As an extension, allow initializing objects with static storage
8513 duration with compound literals (which are then treated just as
8514 the brace enclosed list they contain). Also allow this for
8515 vectors, as we can only assign them with compound literals. */
8516 if (flag_isoc99 && code != VECTOR_TYPE)
8517 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8519 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8520 inside_init = DECL_INITIAL (decl);
8523 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8524 && TREE_CODE (inside_init) != CONSTRUCTOR)
8526 error_init (init_loc, "array initialized from non-constant array "
8528 return error_mark_node;
8531 /* Compound expressions can only occur here if -Wpedantic or
8532 -pedantic-errors is specified. In the later case, we always want
8533 an error. In the former case, we simply want a warning. */
8534 if (require_constant && pedantic
8535 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8538 = valid_compound_expr_initializer (inside_init,
8539 TREE_TYPE (inside_init));
8540 if (inside_init == error_mark_node)
8541 error_init (init_loc, "initializer element is not constant");
8543 pedwarn_init (init_loc, OPT_Wpedantic,
8544 "initializer element is not constant");
8545 if (flag_pedantic_errors)
8546 inside_init = error_mark_node;
8548 else if (require_constant
8549 && !initializer_constant_valid_p (inside_init,
8550 TREE_TYPE (inside_init)))
8552 error_init (init_loc, "initializer element is not constant");
8553 inside_init = error_mark_node;
8555 else if (require_constant && !maybe_const)
8556 pedwarn_init (init_loc, OPT_Wpedantic,
8557 "initializer element is not a constant expression");
8558 else if (require_constexpr)
8559 check_constexpr_init (init_loc, type, inside_init,
8560 null_pointer_constant, int_const_expr,
8563 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8564 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8565 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8566 type, inside_init, origtype,
8569 : ic_init), null_pointer_constant,
8570 NULL_TREE, NULL_TREE, 0);
8574 /* Handle scalar types, including conversions. */
8576 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8577 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8578 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8580 tree unconverted_init = inside_init;
8581 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8582 && (TREE_CODE (init) == STRING_CST
8583 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8584 inside_init = init = array_to_pointer_conversion (init_loc, init);
8586 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8589 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8590 inside_init, origtype,
8591 require_constant ? ic_init_const : ic_init,
8592 null_pointer_constant, NULL_TREE, NULL_TREE,
8595 /* Check to see if we have already given an error message. */
8596 if (inside_init == error_mark_node)
8598 else if (require_constant && !TREE_CONSTANT (inside_init))
8600 error_init (init_loc, "initializer element is not constant");
8601 inside_init = error_mark_node;
8603 else if (require_constant
8604 && !initializer_constant_valid_p (inside_init,
8605 TREE_TYPE (inside_init)))
8607 error_init (init_loc, "initializer element is not computable at "
8609 inside_init = error_mark_node;
8611 else if (require_constant && !maybe_const)
8612 pedwarn_init (init_loc, OPT_Wpedantic,
8613 "initializer element is not a constant expression");
8614 else if (require_constexpr)
8615 check_constexpr_init (init_loc, type, unconverted_init,
8616 null_pointer_constant, int_const_expr,
8622 /* Come here only for records and arrays. */
8624 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8626 error_init (init_loc,
8627 "variable-sized object may not be initialized except "
8628 "with an empty initializer");
8629 return error_mark_node;
8632 error_init (init_loc, "invalid initializer");
8633 return error_mark_node;
8636 /* Handle initializers that use braces. */
8638 /* Type of object we are accumulating a constructor for.
8639 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8640 static tree constructor_type;
8642 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8644 static tree constructor_fields;
8646 /* For an ARRAY_TYPE, this is the specified index
8647 at which to store the next element we get. */
8648 static tree constructor_index;
8650 /* For an ARRAY_TYPE, this is the maximum index. */
8651 static tree constructor_max_index;
8653 /* For a RECORD_TYPE, this is the first field not yet written out. */
8654 static tree constructor_unfilled_fields;
8656 /* For an ARRAY_TYPE, this is the index of the first element
8657 not yet written out. */
8658 static tree constructor_unfilled_index;
8660 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8661 This is so we can generate gaps between fields, when appropriate. */
8662 static tree constructor_bit_index;
8664 /* If we are saving up the elements rather than allocating them,
8665 this is the list of elements so far (in reverse order,
8666 most recent first). */
8667 static vec<constructor_elt, va_gc> *constructor_elements;
8669 /* 1 if constructor should be incrementally stored into a constructor chain,
8670 0 if all the elements should be kept in AVL tree. */
8671 static int constructor_incremental;
8673 /* 1 if so far this constructor's elements are all compile-time constants. */
8674 static int constructor_constant;
8676 /* 1 if so far this constructor's elements are all valid address constants. */
8677 static int constructor_simple;
8679 /* 1 if this constructor has an element that cannot be part of a
8680 constant expression. */
8681 static int constructor_nonconst;
8683 /* 1 if this constructor is erroneous so far. */
8684 static int constructor_erroneous;
8686 /* 1 if this constructor is the universal zero initializer { 0 }. */
8687 static int constructor_zeroinit;
8689 /* Structure for managing pending initializer elements, organized as an
8694 struct init_node *left, *right;
8695 struct init_node *parent;
8702 /* Tree of pending elements at this constructor level.
8703 These are elements encountered out of order
8704 which belong at places we haven't reached yet in actually
8706 Will never hold tree nodes across GC runs. */
8707 static struct init_node *constructor_pending_elts;
8709 /* The SPELLING_DEPTH of this constructor. */
8710 static int constructor_depth;
8712 /* DECL node for which an initializer is being read.
8713 0 means we are reading a constructor expression
8714 such as (struct foo) {...}. */
8715 static tree constructor_decl;
8717 /* Nonzero if there were any member designators in this initializer. */
8718 static int constructor_designated;
8720 /* Nesting depth of designator list. */
8721 static int designator_depth;
8723 /* Nonzero if there were diagnosed errors in this designator list. */
8724 static int designator_erroneous;
8727 /* This stack has a level for each implicit or explicit level of
8728 structuring in the initializer, including the outermost one. It
8729 saves the values of most of the variables above. */
8731 struct constructor_range_stack;
8733 struct constructor_stack
8735 struct constructor_stack *next;
8740 tree unfilled_index;
8741 tree unfilled_fields;
8743 vec<constructor_elt, va_gc> *elements;
8744 struct init_node *pending_elts;
8747 /* If value nonzero, this value should replace the entire
8748 constructor at this level. */
8749 struct c_expr replacement_value;
8750 struct constructor_range_stack *range_stack;
8759 int designator_depth;
8762 static struct constructor_stack *constructor_stack;
8764 /* This stack represents designators from some range designator up to
8765 the last designator in the list. */
8767 struct constructor_range_stack
8769 struct constructor_range_stack *next, *prev;
8770 struct constructor_stack *stack;
8777 static struct constructor_range_stack *constructor_range_stack;
8779 /* This stack records separate initializers that are nested.
8780 Nested initializers can't happen in ANSI C, but GNU C allows them
8781 in cases like { ... (struct foo) { ... } ... }. */
8783 struct initializer_stack
8785 struct initializer_stack *next;
8787 struct constructor_stack *constructor_stack;
8788 struct constructor_range_stack *constructor_range_stack;
8789 vec<constructor_elt, va_gc> *elements;
8790 struct spelling *spelling;
8791 struct spelling *spelling_base;
8793 char require_constant_value;
8794 char require_constant_elements;
8795 char require_constexpr_value;
8797 rich_location *missing_brace_richloc;
8800 static struct initializer_stack *initializer_stack;
8802 /* Prepare to parse and output the initializer for variable DECL. */
8805 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
8806 bool init_require_constant, bool init_require_constexpr,
8807 rich_location *richloc)
8810 struct initializer_stack *p = XNEW (struct initializer_stack);
8812 p->decl = constructor_decl;
8813 p->require_constant_value = require_constant_value;
8814 p->require_constant_elements = require_constant_elements;
8815 p->require_constexpr_value = require_constexpr_value;
8816 p->constructor_stack = constructor_stack;
8817 p->constructor_range_stack = constructor_range_stack;
8818 p->elements = constructor_elements;
8819 p->spelling = spelling;
8820 p->spelling_base = spelling_base;
8821 p->spelling_size = spelling_size;
8822 p->next = initializer_stack;
8823 p->missing_brace_richloc = richloc;
8824 p->designated = constructor_designated;
8825 initializer_stack = p;
8827 constructor_decl = decl;
8828 constructor_designated = 0;
8830 require_constant_value = init_require_constant;
8831 require_constexpr_value = init_require_constexpr;
8832 if (decl != NULL_TREE && decl != error_mark_node)
8834 require_constant_elements
8835 = ((init_require_constant || (pedantic && !flag_isoc99))
8836 /* For a scalar, you can always use any value to initialize,
8837 even within braces. */
8838 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8839 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8843 require_constant_elements = false;
8844 locus = _("(anonymous)");
8847 constructor_stack = 0;
8848 constructor_range_stack = 0;
8850 found_missing_braces = 0;
8854 RESTORE_SPELLING_DEPTH (0);
8857 push_string (locus);
8863 struct initializer_stack *p = initializer_stack;
8865 /* Free the whole constructor stack of this initializer. */
8866 while (constructor_stack)
8868 struct constructor_stack *q = constructor_stack;
8869 constructor_stack = q->next;
8873 gcc_assert (!constructor_range_stack);
8875 /* Pop back to the data of the outer initializer (if any). */
8876 XDELETE (spelling_base);
8878 constructor_decl = p->decl;
8879 require_constant_value = p->require_constant_value;
8880 require_constant_elements = p->require_constant_elements;
8881 require_constexpr_value = p->require_constexpr_value;
8882 constructor_stack = p->constructor_stack;
8883 constructor_designated = p->designated;
8884 constructor_range_stack = p->constructor_range_stack;
8885 constructor_elements = p->elements;
8886 spelling = p->spelling;
8887 spelling_base = p->spelling_base;
8888 spelling_size = p->spelling_size;
8889 initializer_stack = p->next;
8893 /* Call here when we see the initializer is surrounded by braces.
8894 This is instead of a call to push_init_level;
8895 it is matched by a call to pop_init_level.
8897 TYPE is the type to initialize, for a constructor expression.
8898 For an initializer for a decl, TYPE is zero. */
8901 really_start_incremental_init (tree type)
8903 struct constructor_stack *p = XNEW (struct constructor_stack);
8905 if (type == NULL_TREE)
8906 type = TREE_TYPE (constructor_decl);
8908 if (VECTOR_TYPE_P (type)
8909 && TYPE_VECTOR_OPAQUE (type))
8910 error ("opaque vector types cannot be initialized");
8912 p->type = constructor_type;
8913 p->fields = constructor_fields;
8914 p->index = constructor_index;
8915 p->max_index = constructor_max_index;
8916 p->unfilled_index = constructor_unfilled_index;
8917 p->unfilled_fields = constructor_unfilled_fields;
8918 p->bit_index = constructor_bit_index;
8919 p->elements = constructor_elements;
8920 p->constant = constructor_constant;
8921 p->simple = constructor_simple;
8922 p->nonconst = constructor_nonconst;
8923 p->erroneous = constructor_erroneous;
8924 p->pending_elts = constructor_pending_elts;
8925 p->depth = constructor_depth;
8926 p->replacement_value.value = 0;
8927 p->replacement_value.original_code = ERROR_MARK;
8928 p->replacement_value.original_type = NULL;
8932 p->incremental = constructor_incremental;
8933 p->designated = constructor_designated;
8934 p->designator_depth = designator_depth;
8936 constructor_stack = p;
8938 constructor_constant = 1;
8939 constructor_simple = 1;
8940 constructor_nonconst = 0;
8941 constructor_depth = SPELLING_DEPTH ();
8942 constructor_elements = NULL;
8943 constructor_pending_elts = 0;
8944 constructor_type = type;
8945 constructor_incremental = 1;
8946 constructor_designated = 0;
8947 constructor_zeroinit = 1;
8948 designator_depth = 0;
8949 designator_erroneous = 0;
8951 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8953 constructor_fields = TYPE_FIELDS (constructor_type);
8954 /* Skip any nameless bit fields at the beginning. */
8955 while (constructor_fields != NULL_TREE
8956 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8957 constructor_fields = DECL_CHAIN (constructor_fields);
8959 constructor_unfilled_fields = constructor_fields;
8960 constructor_bit_index = bitsize_zero_node;
8962 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8964 if (TYPE_DOMAIN (constructor_type))
8966 constructor_max_index
8967 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8969 /* Detect non-empty initializations of zero-length arrays. */
8970 if (constructor_max_index == NULL_TREE
8971 && TYPE_SIZE (constructor_type))
8972 constructor_max_index = integer_minus_one_node;
8974 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8975 to initialize VLAs with a nonempty initializer will cause a
8976 proper error; avoid tree checking errors as well by setting a
8978 if (constructor_max_index
8979 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8980 constructor_max_index = integer_minus_one_node;
8983 = convert (bitsizetype,
8984 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8988 constructor_index = bitsize_zero_node;
8989 constructor_max_index = NULL_TREE;
8992 constructor_unfilled_index = constructor_index;
8994 else if (gnu_vector_type_p (constructor_type))
8996 /* Vectors are like simple fixed-size arrays. */
8997 constructor_max_index =
8998 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8999 constructor_index = bitsize_zero_node;
9000 constructor_unfilled_index = constructor_index;
9004 /* Handle the case of int x = {5}; */
9005 constructor_fields = constructor_type;
9006 constructor_unfilled_fields = constructor_type;
9010 extern location_t last_init_list_comma;
9012 /* Called when we see an open brace for a nested initializer. Finish
9013 off any pending levels with implicit braces. */
9015 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
9017 while (constructor_stack->implicit)
9019 if (RECORD_OR_UNION_TYPE_P (constructor_type)
9020 && constructor_fields == NULL_TREE)
9021 process_init_element (input_location,
9022 pop_init_level (loc, 1, braced_init_obstack,
9023 last_init_list_comma),
9024 true, braced_init_obstack);
9025 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
9026 && constructor_max_index
9027 && tree_int_cst_lt (constructor_max_index,
9029 process_init_element (input_location,
9030 pop_init_level (loc, 1, braced_init_obstack,
9031 last_init_list_comma),
9032 true, braced_init_obstack);
9038 /* Push down into a subobject, for initialization.
9039 If this is for an explicit set of braces, IMPLICIT is 0.
9040 If it is because the next element belongs at a lower level,
9041 IMPLICIT is 1 (or 2 if the push is because of designator list). */
9044 push_init_level (location_t loc, int implicit,
9045 struct obstack *braced_init_obstack)
9047 struct constructor_stack *p;
9048 tree value = NULL_TREE;
9050 /* Unless this is an explicit brace, we need to preserve previous
9054 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
9055 value = find_init_member (constructor_fields, braced_init_obstack);
9056 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9057 value = find_init_member (constructor_index, braced_init_obstack);
9060 p = XNEW (struct constructor_stack);
9061 p->type = constructor_type;
9062 p->fields = constructor_fields;
9063 p->index = constructor_index;
9064 p->max_index = constructor_max_index;
9065 p->unfilled_index = constructor_unfilled_index;
9066 p->unfilled_fields = constructor_unfilled_fields;
9067 p->bit_index = constructor_bit_index;
9068 p->elements = constructor_elements;
9069 p->constant = constructor_constant;
9070 p->simple = constructor_simple;
9071 p->nonconst = constructor_nonconst;
9072 p->erroneous = constructor_erroneous;
9073 p->pending_elts = constructor_pending_elts;
9074 p->depth = constructor_depth;
9075 p->replacement_value.value = NULL_TREE;
9076 p->replacement_value.original_code = ERROR_MARK;
9077 p->replacement_value.original_type = NULL;
9078 p->implicit = implicit;
9080 p->incremental = constructor_incremental;
9081 p->designated = constructor_designated;
9082 p->designator_depth = designator_depth;
9083 p->next = constructor_stack;
9085 constructor_stack = p;
9087 constructor_constant = 1;
9088 constructor_simple = 1;
9089 constructor_nonconst = 0;
9090 constructor_depth = SPELLING_DEPTH ();
9091 constructor_elements = NULL;
9092 constructor_incremental = 1;
9093 /* If the upper initializer is designated, then mark this as
9094 designated too to prevent bogus warnings. */
9095 constructor_designated = p->designated;
9096 constructor_pending_elts = 0;
9099 p->range_stack = constructor_range_stack;
9100 constructor_range_stack = 0;
9101 designator_depth = 0;
9102 designator_erroneous = 0;
9105 /* Don't die if an entire brace-pair level is superfluous
9106 in the containing level. */
9107 if (constructor_type == NULL_TREE)
9109 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9111 /* Don't die if there are extra init elts at the end. */
9112 if (constructor_fields == NULL_TREE)
9113 constructor_type = NULL_TREE;
9116 constructor_type = TREE_TYPE (constructor_fields);
9117 push_member_name (constructor_fields);
9118 constructor_depth++;
9121 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9123 constructor_type = TREE_TYPE (constructor_type);
9124 push_array_bounds (tree_to_uhwi (constructor_index));
9125 constructor_depth++;
9128 if (constructor_type == NULL_TREE)
9130 error_init (loc, "extra brace group at end of initializer");
9131 constructor_fields = NULL_TREE;
9132 constructor_unfilled_fields = NULL_TREE;
9136 if (value && TREE_CODE (value) == CONSTRUCTOR)
9138 constructor_constant = TREE_CONSTANT (value);
9139 constructor_simple = TREE_STATIC (value);
9140 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
9141 constructor_elements = CONSTRUCTOR_ELTS (value);
9142 if (!vec_safe_is_empty (constructor_elements)
9143 && (TREE_CODE (constructor_type) == RECORD_TYPE
9144 || TREE_CODE (constructor_type) == ARRAY_TYPE))
9145 set_nonincremental_init (braced_init_obstack);
9150 found_missing_braces = 1;
9151 if (initializer_stack->missing_brace_richloc)
9152 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9156 if (RECORD_OR_UNION_TYPE_P (constructor_type))
9158 constructor_fields = TYPE_FIELDS (constructor_type);
9159 /* Skip any nameless bit fields at the beginning. */
9160 while (constructor_fields != NULL_TREE
9161 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9162 constructor_fields = DECL_CHAIN (constructor_fields);
9164 constructor_unfilled_fields = constructor_fields;
9165 constructor_bit_index = bitsize_zero_node;
9167 else if (gnu_vector_type_p (constructor_type))
9169 /* Vectors are like simple fixed-size arrays. */
9170 constructor_max_index =
9171 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9172 constructor_index = bitsize_int (0);
9173 constructor_unfilled_index = constructor_index;
9175 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9177 if (TYPE_DOMAIN (constructor_type))
9179 constructor_max_index
9180 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
9182 /* Detect non-empty initializations of zero-length arrays. */
9183 if (constructor_max_index == NULL_TREE
9184 && TYPE_SIZE (constructor_type))
9185 constructor_max_index = integer_minus_one_node;
9187 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9188 to initialize VLAs will cause a proper error; avoid tree
9189 checking errors as well by setting a safe value. */
9190 if (constructor_max_index
9191 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9192 constructor_max_index = integer_minus_one_node;
9195 = convert (bitsizetype,
9196 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9199 constructor_index = bitsize_zero_node;
9201 constructor_unfilled_index = constructor_index;
9202 if (value && TREE_CODE (value) == STRING_CST)
9204 /* We need to split the char/wchar array into individual
9205 characters, so that we don't have to special case it
9207 set_nonincremental_init_from_string (value, braced_init_obstack);
9212 if (constructor_type != error_mark_node)
9213 warning_init (input_location, 0, "braces around scalar initializer");
9214 constructor_fields = constructor_type;
9215 constructor_unfilled_fields = constructor_type;
9219 /* At the end of an implicit or explicit brace level,
9220 finish up that level of constructor. If a single expression
9221 with redundant braces initialized that level, return the
9222 c_expr structure for that expression. Otherwise, the original_code
9223 element is set to ERROR_MARK.
9224 If we were outputting the elements as they are read, return 0 as the value
9225 from inner levels (process_init_element ignores that),
9226 but return error_mark_node as the value from the outermost level
9227 (that's what we want to put in DECL_INITIAL).
9228 Otherwise, return a CONSTRUCTOR expression as the value. */
9231 pop_init_level (location_t loc, int implicit,
9232 struct obstack *braced_init_obstack,
9233 location_t insert_before)
9235 struct constructor_stack *p;
9237 ret.value = NULL_TREE;
9238 ret.original_code = ERROR_MARK;
9239 ret.original_type = NULL;
9244 /* When we come to an explicit close brace,
9245 pop any inner levels that didn't have explicit braces. */
9246 while (constructor_stack->implicit)
9247 process_init_element (input_location,
9248 pop_init_level (loc, 1, braced_init_obstack,
9250 true, braced_init_obstack);
9251 gcc_assert (!constructor_range_stack);
9254 if (initializer_stack->missing_brace_richloc)
9255 initializer_stack->missing_brace_richloc->add_fixit_insert_before
9256 (insert_before, "}");
9258 /* Now output all pending elements. */
9259 constructor_incremental = 1;
9260 output_pending_init_elements (1, braced_init_obstack);
9262 p = constructor_stack;
9264 /* Error for initializing a flexible array member, or a zero-length
9265 array member in an inappropriate context. */
9266 if (constructor_type && constructor_fields
9267 && TREE_CODE (constructor_type) == ARRAY_TYPE
9268 && TYPE_DOMAIN (constructor_type)
9269 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
9271 /* Silently discard empty initializations. The parser will
9272 already have pedwarned for empty brackets. */
9273 if (integer_zerop (constructor_unfilled_index))
9274 constructor_type = NULL_TREE;
9277 gcc_assert (!TYPE_SIZE (constructor_type));
9279 if (constructor_depth > 2)
9280 error_init (loc, "initialization of flexible array member in a nested context");
9282 pedwarn_init (loc, OPT_Wpedantic,
9283 "initialization of a flexible array member");
9285 /* We have already issued an error message for the existence
9286 of a flexible array member not at the end of the structure.
9287 Discard the initializer so that we do not die later. */
9288 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
9289 constructor_type = NULL_TREE;
9293 switch (vec_safe_length (constructor_elements))
9296 /* Initialization with { } counts as zeroinit. */
9297 constructor_zeroinit = 1;
9300 /* This might be zeroinit as well. */
9301 if (integer_zerop ((*constructor_elements)[0].value))
9302 constructor_zeroinit = 1;
9305 /* If the constructor has more than one element, it can't be { 0 }. */
9306 constructor_zeroinit = 0;
9310 /* Warn when some structs are initialized with direct aggregation. */
9311 if (!implicit && found_missing_braces && warn_missing_braces
9312 && !constructor_zeroinit)
9314 gcc_assert (initializer_stack->missing_brace_richloc);
9315 warning_at (initializer_stack->missing_brace_richloc,
9316 OPT_Wmissing_braces,
9317 "missing braces around initializer");
9320 /* Warn when some struct elements are implicitly initialized to zero. */
9321 if (warn_missing_field_initializers
9323 && TREE_CODE (constructor_type) == RECORD_TYPE
9324 && constructor_unfilled_fields)
9326 /* Do not warn for flexible array members or zero-length arrays. */
9327 while (constructor_unfilled_fields
9328 && (!DECL_SIZE (constructor_unfilled_fields)
9329 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9330 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9332 if (constructor_unfilled_fields
9333 /* Do not warn if this level of the initializer uses member
9334 designators; it is likely to be deliberate. */
9335 && !constructor_designated
9336 /* Do not warn about initializing with { 0 } or with { }. */
9337 && !constructor_zeroinit)
9339 if (warning_at (input_location, OPT_Wmissing_field_initializers,
9340 "missing initializer for field %qD of %qT",
9341 constructor_unfilled_fields,
9343 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9344 "%qD declared here", constructor_unfilled_fields);
9348 /* Pad out the end of the structure. */
9349 if (p->replacement_value.value)
9350 /* If this closes a superfluous brace pair,
9351 just pass out the element between them. */
9352 ret = p->replacement_value;
9353 else if (constructor_type == NULL_TREE)
9355 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9356 && TREE_CODE (constructor_type) != ARRAY_TYPE
9357 && !gnu_vector_type_p (constructor_type))
9359 /* A nonincremental scalar initializer--just return
9360 the element, after verifying there is just one.
9361 Empty scalar initializers are supported in C2X. */
9362 if (vec_safe_is_empty (constructor_elements))
9364 if (constructor_erroneous || constructor_type == error_mark_node)
9365 ret.value = error_mark_node;
9366 else if (TREE_CODE (constructor_type) == POINTER_TYPE)
9367 /* Ensure this is a null pointer constant in the case of a
9368 'constexpr' object initialized with {}. */
9369 ret.value = build_zero_cst (ptr_type_node);
9371 ret.value = build_zero_cst (constructor_type);
9373 else if (vec_safe_length (constructor_elements) != 1)
9375 error_init (loc, "extra elements in scalar initializer");
9376 ret.value = (*constructor_elements)[0].value;
9379 ret.value = (*constructor_elements)[0].value;
9383 if (constructor_erroneous)
9384 ret.value = error_mark_node;
9387 ret.value = build_constructor (constructor_type,
9388 constructor_elements);
9389 if (constructor_constant)
9390 TREE_CONSTANT (ret.value) = 1;
9391 if (constructor_constant && constructor_simple)
9392 TREE_STATIC (ret.value) = 1;
9393 if (constructor_nonconst)
9394 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9398 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9400 if (constructor_nonconst)
9401 ret.original_code = C_MAYBE_CONST_EXPR;
9402 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9403 ret.original_code = ERROR_MARK;
9406 constructor_type = p->type;
9407 constructor_fields = p->fields;
9408 constructor_index = p->index;
9409 constructor_max_index = p->max_index;
9410 constructor_unfilled_index = p->unfilled_index;
9411 constructor_unfilled_fields = p->unfilled_fields;
9412 constructor_bit_index = p->bit_index;
9413 constructor_elements = p->elements;
9414 constructor_constant = p->constant;
9415 constructor_simple = p->simple;
9416 constructor_nonconst = p->nonconst;
9417 constructor_erroneous = p->erroneous;
9418 constructor_incremental = p->incremental;
9419 constructor_designated = p->designated;
9420 designator_depth = p->designator_depth;
9421 constructor_pending_elts = p->pending_elts;
9422 constructor_depth = p->depth;
9424 constructor_range_stack = p->range_stack;
9425 RESTORE_SPELLING_DEPTH (constructor_depth);
9427 constructor_stack = p->next;
9430 if (ret.value == NULL_TREE && constructor_stack == 0)
9431 ret.value = error_mark_node;
9435 /* Common handling for both array range and field name designators.
9436 ARRAY argument is nonzero for array ranges. Returns false for success. */
9439 set_designator (location_t loc, bool array,
9440 struct obstack *braced_init_obstack)
9443 enum tree_code subcode;
9445 /* Don't die if an entire brace-pair level is superfluous
9446 in the containing level, or for an erroneous type. */
9447 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9450 /* If there were errors in this designator list already, bail out
9452 if (designator_erroneous)
9455 /* Likewise for an initializer for a variable-size type. Those are
9456 diagnosed in the parser, except for empty initializer braces. */
9457 if (COMPLETE_TYPE_P (constructor_type)
9458 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9461 if (!designator_depth)
9463 gcc_assert (!constructor_range_stack);
9465 /* Designator list starts at the level of closest explicit
9467 while (constructor_stack->implicit)
9468 process_init_element (input_location,
9469 pop_init_level (loc, 1, braced_init_obstack,
9470 last_init_list_comma),
9471 true, braced_init_obstack);
9472 constructor_designated = 1;
9476 switch (TREE_CODE (constructor_type))
9480 subtype = TREE_TYPE (constructor_fields);
9481 if (subtype != error_mark_node)
9482 subtype = TYPE_MAIN_VARIANT (subtype);
9485 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9491 subcode = TREE_CODE (subtype);
9492 if (array && subcode != ARRAY_TYPE)
9494 error_init (loc, "array index in non-array initializer");
9497 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9499 error_init (loc, "field name not in record or union initializer");
9503 constructor_designated = 1;
9504 finish_implicit_inits (loc, braced_init_obstack);
9505 push_init_level (loc, 2, braced_init_obstack);
9509 /* If there are range designators in designator list, push a new designator
9510 to constructor_range_stack. RANGE_END is end of such stack range or
9511 NULL_TREE if there is no range designator at this level. */
9514 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9516 struct constructor_range_stack *p;
9518 p = (struct constructor_range_stack *)
9519 obstack_alloc (braced_init_obstack,
9520 sizeof (struct constructor_range_stack));
9521 p->prev = constructor_range_stack;
9523 p->fields = constructor_fields;
9524 p->range_start = constructor_index;
9525 p->index = constructor_index;
9526 p->stack = constructor_stack;
9527 p->range_end = range_end;
9528 if (constructor_range_stack)
9529 constructor_range_stack->next = p;
9530 constructor_range_stack = p;
9533 /* Within an array initializer, specify the next index to be initialized.
9534 FIRST is that index. If LAST is nonzero, then initialize a range
9535 of indices, running from FIRST through LAST. */
9538 set_init_index (location_t loc, tree first, tree last,
9539 struct obstack *braced_init_obstack)
9541 if (set_designator (loc, true, braced_init_obstack))
9544 designator_erroneous = 1;
9546 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9547 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9549 error_init (loc, "array index in initializer not of integer type");
9553 if (TREE_CODE (first) != INTEGER_CST)
9555 first = c_fully_fold (first, false, NULL);
9556 if (TREE_CODE (first) == INTEGER_CST)
9557 pedwarn_init (loc, OPT_Wpedantic,
9558 "array index in initializer is not "
9559 "an integer constant expression");
9562 if (last && TREE_CODE (last) != INTEGER_CST)
9564 last = c_fully_fold (last, false, NULL);
9565 if (TREE_CODE (last) == INTEGER_CST)
9566 pedwarn_init (loc, OPT_Wpedantic,
9567 "array index in initializer is not "
9568 "an integer constant expression");
9571 if (TREE_CODE (first) != INTEGER_CST)
9572 error_init (loc, "nonconstant array index in initializer");
9573 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9574 error_init (loc, "nonconstant array index in initializer");
9575 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9576 error_init (loc, "array index in non-array initializer");
9577 else if (tree_int_cst_sgn (first) == -1)
9578 error_init (loc, "array index in initializer exceeds array bounds");
9579 else if (constructor_max_index
9580 && tree_int_cst_lt (constructor_max_index, first))
9581 error_init (loc, "array index in initializer exceeds array bounds");
9584 constant_expression_warning (first);
9586 constant_expression_warning (last);
9587 constructor_index = convert (bitsizetype, first);
9588 if (tree_int_cst_lt (constructor_index, first))
9590 constructor_index = copy_node (constructor_index);
9591 TREE_OVERFLOW (constructor_index) = 1;
9596 if (tree_int_cst_equal (first, last))
9598 else if (tree_int_cst_lt (last, first))
9600 error_init (loc, "empty index range in initializer");
9605 last = convert (bitsizetype, last);
9606 if (constructor_max_index != NULL_TREE
9607 && tree_int_cst_lt (constructor_max_index, last))
9609 error_init (loc, "array index range in initializer exceeds "
9617 designator_erroneous = 0;
9618 if (constructor_range_stack || last)
9619 push_range_stack (last, braced_init_obstack);
9623 /* Within a struct initializer, specify the next field to be initialized. */
9626 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9627 struct obstack *braced_init_obstack)
9631 if (set_designator (loc, false, braced_init_obstack))
9634 designator_erroneous = 1;
9636 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9638 error_init (loc, "field name not in record or union initializer");
9642 field = lookup_field (constructor_type, fieldname);
9644 if (field == NULL_TREE)
9646 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9649 gcc_rich_location rich_loc (fieldname_loc);
9650 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9651 error_at (&rich_loc,
9652 "%qT has no member named %qE; did you mean %qE?",
9653 constructor_type, fieldname, guessed_id);
9656 error_at (fieldname_loc, "%qT has no member named %qE",
9657 constructor_type, fieldname);
9662 constructor_fields = TREE_VALUE (field);
9664 designator_erroneous = 0;
9665 if (constructor_range_stack)
9666 push_range_stack (NULL_TREE, braced_init_obstack);
9667 field = TREE_CHAIN (field);
9670 if (set_designator (loc, false, braced_init_obstack))
9674 while (field != NULL_TREE);
9677 /* Add a new initializer to the tree of pending initializers. PURPOSE
9678 identifies the initializer, either array index or field in a structure.
9679 VALUE is the value of that index or field. If ORIGTYPE is not
9680 NULL_TREE, it is the original type of VALUE.
9682 IMPLICIT is true if value comes from pop_init_level (1),
9683 the new initializer has been merged with the existing one
9684 and thus no warnings should be emitted about overriding an
9685 existing initializer. */
9688 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9689 bool implicit, struct obstack *braced_init_obstack)
9691 struct init_node *p, **q, *r;
9693 q = &constructor_pending_elts;
9696 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9701 if (tree_int_cst_lt (purpose, p->purpose))
9703 else if (tree_int_cst_lt (p->purpose, purpose))
9709 if (TREE_SIDE_EFFECTS (p->value))
9710 warning_init (loc, OPT_Woverride_init_side_effects,
9711 "initialized field with side-effects "
9713 else if (warn_override_init)
9714 warning_init (loc, OPT_Woverride_init,
9715 "initialized field overwritten");
9718 p->origtype = origtype;
9727 bitpos = bit_position (purpose);
9731 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9733 else if (p->purpose != purpose)
9739 if (TREE_SIDE_EFFECTS (p->value))
9740 warning_init (loc, OPT_Woverride_init_side_effects,
9741 "initialized field with side-effects "
9743 else if (warn_override_init)
9744 warning_init (loc, OPT_Woverride_init,
9745 "initialized field overwritten");
9748 p->origtype = origtype;
9754 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9755 sizeof (struct init_node));
9756 r->purpose = purpose;
9758 r->origtype = origtype;
9768 struct init_node *s;
9772 if (p->balance == 0)
9774 else if (p->balance < 0)
9781 p->left->parent = p;
9798 constructor_pending_elts = r;
9803 struct init_node *t = r->right;
9807 r->right->parent = r;
9812 p->left->parent = p;
9815 p->balance = t->balance < 0;
9816 r->balance = -(t->balance > 0);
9831 constructor_pending_elts = t;
9837 /* p->balance == +1; growth of left side balances the node. */
9842 else /* r == p->right */
9844 if (p->balance == 0)
9845 /* Growth propagation from right side. */
9847 else if (p->balance > 0)
9854 p->right->parent = p;
9871 constructor_pending_elts = r;
9873 else /* r->balance == -1 */
9876 struct init_node *t = r->left;
9880 r->left->parent = r;
9885 p->right->parent = p;
9888 r->balance = (t->balance < 0);
9889 p->balance = -(t->balance > 0);
9904 constructor_pending_elts = t;
9910 /* p->balance == -1; growth of right side balances the node. */
9921 /* Build AVL tree from a sorted chain. */
9924 set_nonincremental_init (struct obstack * braced_init_obstack)
9926 unsigned HOST_WIDE_INT ix;
9929 if (TREE_CODE (constructor_type) != RECORD_TYPE
9930 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9933 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9934 add_pending_init (input_location, index, value, NULL_TREE, true,
9935 braced_init_obstack);
9936 constructor_elements = NULL;
9937 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9939 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9940 /* Skip any nameless bit fields at the beginning. */
9941 while (constructor_unfilled_fields != NULL_TREE
9942 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9943 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9946 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9948 if (TYPE_DOMAIN (constructor_type))
9949 constructor_unfilled_index
9950 = convert (bitsizetype,
9951 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9953 constructor_unfilled_index = bitsize_zero_node;
9955 constructor_incremental = 0;
9958 /* Build AVL tree from a string constant. */
9961 set_nonincremental_init_from_string (tree str,
9962 struct obstack * braced_init_obstack)
9964 tree value, purpose, type;
9965 HOST_WIDE_INT val[2];
9966 const char *p, *end;
9967 int byte, wchar_bytes, charwidth, bitpos;
9969 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9971 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9972 charwidth = TYPE_PRECISION (char_type_node);
9973 gcc_assert ((size_t) wchar_bytes * charwidth
9974 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9975 type = TREE_TYPE (constructor_type);
9976 p = TREE_STRING_POINTER (str);
9977 end = p + TREE_STRING_LENGTH (str);
9979 for (purpose = bitsize_zero_node;
9981 && !(constructor_max_index
9982 && tree_int_cst_lt (constructor_max_index, purpose));
9983 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9985 if (wchar_bytes == 1)
9987 val[0] = (unsigned char) *p++;
9994 for (byte = 0; byte < wchar_bytes; byte++)
9996 if (BYTES_BIG_ENDIAN)
9997 bitpos = (wchar_bytes - byte - 1) * charwidth;
9999 bitpos = byte * charwidth;
10000 val[bitpos / HOST_BITS_PER_WIDE_INT]
10001 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
10002 << (bitpos % HOST_BITS_PER_WIDE_INT);
10006 if (!TYPE_UNSIGNED (type))
10008 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
10009 if (bitpos < HOST_BITS_PER_WIDE_INT)
10011 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
10013 val[0] |= HOST_WIDE_INT_M1U << bitpos;
10017 else if (bitpos == HOST_BITS_PER_WIDE_INT)
10022 else if (val[1] & (HOST_WIDE_INT_1
10023 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
10024 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
10027 value = wide_int_to_tree (type,
10028 wide_int::from_array (val, 2,
10029 HOST_BITS_PER_WIDE_INT * 2));
10030 add_pending_init (input_location, purpose, value, NULL_TREE, true,
10031 braced_init_obstack);
10034 constructor_incremental = 0;
10037 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
10038 not initialized yet. */
10041 find_init_member (tree field, struct obstack * braced_init_obstack)
10043 struct init_node *p;
10045 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10047 if (constructor_incremental
10048 && tree_int_cst_lt (field, constructor_unfilled_index))
10049 set_nonincremental_init (braced_init_obstack);
10051 p = constructor_pending_elts;
10054 if (tree_int_cst_lt (field, p->purpose))
10056 else if (tree_int_cst_lt (p->purpose, field))
10062 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10064 tree bitpos = bit_position (field);
10066 if (constructor_incremental
10067 && (!constructor_unfilled_fields
10068 || tree_int_cst_lt (bitpos,
10069 bit_position (constructor_unfilled_fields))))
10070 set_nonincremental_init (braced_init_obstack);
10072 p = constructor_pending_elts;
10075 if (field == p->purpose)
10077 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
10083 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10085 if (!vec_safe_is_empty (constructor_elements)
10086 && (constructor_elements->last ().index == field))
10087 return constructor_elements->last ().value;
10092 /* "Output" the next constructor element.
10093 At top level, really output it to assembler code now.
10094 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
10095 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
10096 TYPE is the data type that the containing data type wants here.
10097 FIELD is the field (a FIELD_DECL) or the index that this element fills.
10098 If VALUE is a string constant, STRICT_STRING is true if it is
10099 unparenthesized or we should not warn here for it being parenthesized.
10100 For other types of VALUE, STRICT_STRING is not used.
10102 PENDING if true means output pending elements that belong
10103 right after this element. (PENDING is normally true;
10104 it is false while outputting pending elements, to avoid recursion.)
10106 IMPLICIT is true if value comes from pop_init_level (1),
10107 the new initializer has been merged with the existing one
10108 and thus no warnings should be emitted about overriding an
10109 existing initializer. */
10112 output_init_element (location_t loc, tree value, tree origtype,
10113 bool strict_string, tree type, tree field, bool pending,
10114 bool implicit, struct obstack * braced_init_obstack)
10116 tree semantic_type = NULL_TREE;
10117 bool maybe_const = true;
10118 bool npc, int_const_expr, arith_const_expr;
10120 if (type == error_mark_node || value == error_mark_node)
10122 constructor_erroneous = 1;
10125 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
10126 && (TREE_CODE (value) == STRING_CST
10127 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
10128 && !(TREE_CODE (value) == STRING_CST
10129 && TREE_CODE (type) == ARRAY_TYPE
10130 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
10131 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
10132 TYPE_MAIN_VARIANT (type)))
10133 value = array_to_pointer_conversion (input_location, value);
10135 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
10136 && require_constant_value && pending)
10138 /* As an extension, allow initializing objects with static storage
10139 duration with compound literals (which are then treated just as
10140 the brace enclosed list they contain). */
10142 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
10144 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
10145 value = DECL_INITIAL (decl);
10148 npc = null_pointer_constant_p (value);
10149 int_const_expr = (TREE_CODE (value) == INTEGER_CST
10150 && !TREE_OVERFLOW (value)
10151 && INTEGRAL_TYPE_P (TREE_TYPE (value)));
10152 /* Not fully determined before folding. */
10153 arith_const_expr = true;
10154 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
10156 semantic_type = TREE_TYPE (value);
10157 value = TREE_OPERAND (value, 0);
10159 value = c_fully_fold (value, require_constant_value, &maybe_const);
10160 /* TODO: this may not detect all cases of expressions folding to
10161 constants that are not arithmetic constant expressions. */
10163 arith_const_expr = false;
10164 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
10165 && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
10166 && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
10167 arith_const_expr = false;
10168 else if (TREE_CODE (value) != INTEGER_CST
10169 && TREE_CODE (value) != REAL_CST
10170 && TREE_CODE (value) != COMPLEX_CST)
10171 arith_const_expr = false;
10172 else if (TREE_OVERFLOW (value))
10173 arith_const_expr = false;
10175 if (value == error_mark_node)
10176 constructor_erroneous = 1;
10177 else if (!TREE_CONSTANT (value))
10178 constructor_constant = 0;
10179 else if (!initializer_constant_valid_p (value,
10181 AGGREGATE_TYPE_P (constructor_type)
10182 && TYPE_REVERSE_STORAGE_ORDER
10183 (constructor_type))
10184 || (RECORD_OR_UNION_TYPE_P (constructor_type)
10185 && DECL_C_BIT_FIELD (field)
10186 && TREE_CODE (value) != INTEGER_CST))
10187 constructor_simple = 0;
10189 constructor_nonconst = 1;
10191 /* Digest the initializer and issue any errors about incompatible
10192 types before issuing errors about non-constant initializers. */
10193 tree new_value = value;
10195 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
10196 /* In the case of braces around a scalar initializer, the result of
10197 this initializer processing goes through digest_init again at the
10198 outer level. In the case of a constexpr initializer for a
10199 pointer, avoid converting a null pointer constant to something
10200 that is not a null pointer constant to avoid a spurious error
10201 from that second processing. */
10202 if (!require_constexpr_value
10204 || TREE_CODE (constructor_type) != POINTER_TYPE)
10205 new_value = digest_init (loc, type, new_value, origtype, npc,
10206 int_const_expr, arith_const_expr, strict_string,
10207 require_constant_value, require_constexpr_value);
10208 if (new_value == error_mark_node)
10210 constructor_erroneous = 1;
10213 if (require_constant_value || require_constant_elements)
10214 constant_expression_warning (new_value);
10216 /* Proceed to check the constness of the original initializer. */
10217 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
10219 if (require_constant_value)
10221 error_init (loc, "initializer element is not constant");
10222 value = error_mark_node;
10224 else if (require_constant_elements)
10225 pedwarn (loc, OPT_Wpedantic,
10226 "initializer element is not computable at load time");
10228 else if (!maybe_const
10229 && (require_constant_value || require_constant_elements))
10230 pedwarn_init (loc, OPT_Wpedantic,
10231 "initializer element is not a constant expression");
10232 /* digest_init has already carried out the additional checks
10233 required for 'constexpr' initializers (using the information
10234 passed to it about whether the original initializer was certain
10235 kinds of constant expression), so that check does not need to be
10238 /* Issue -Wc++-compat warnings about initializing a bitfield with
10240 if (warn_cxx_compat
10241 && field != NULL_TREE
10242 && TREE_CODE (field) == FIELD_DECL
10243 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
10244 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
10245 != TYPE_MAIN_VARIANT (type))
10246 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
10248 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
10249 if (checktype != error_mark_node
10250 && (TYPE_MAIN_VARIANT (checktype)
10251 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
10252 warning_init (loc, OPT_Wc___compat,
10253 "enum conversion in initialization is invalid in C++");
10256 /* If this field is empty and does not have side effects (and is not at
10257 the end of structure), don't do anything other than checking the
10260 && (TREE_TYPE (field) == error_mark_node
10261 || (COMPLETE_TYPE_P (TREE_TYPE (field))
10262 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
10263 && !TREE_SIDE_EFFECTS (new_value)
10264 && (TREE_CODE (constructor_type) == ARRAY_TYPE
10265 || DECL_CHAIN (field)))))
10268 /* Finally, set VALUE to the initializer value digested above. */
10271 /* If this element doesn't come next in sequence,
10272 put it on constructor_pending_elts. */
10273 if (TREE_CODE (constructor_type) == ARRAY_TYPE
10274 && (!constructor_incremental
10275 || !tree_int_cst_equal (field, constructor_unfilled_index)))
10277 if (constructor_incremental
10278 && tree_int_cst_lt (field, constructor_unfilled_index))
10279 set_nonincremental_init (braced_init_obstack);
10281 add_pending_init (loc, field, value, origtype, implicit,
10282 braced_init_obstack);
10285 else if (TREE_CODE (constructor_type) == RECORD_TYPE
10286 && (!constructor_incremental
10287 || field != constructor_unfilled_fields))
10289 /* We do this for records but not for unions. In a union,
10290 no matter which field is specified, it can be initialized
10291 right away since it starts at the beginning of the union. */
10292 if (constructor_incremental)
10294 if (!constructor_unfilled_fields)
10295 set_nonincremental_init (braced_init_obstack);
10298 tree bitpos, unfillpos;
10300 bitpos = bit_position (field);
10301 unfillpos = bit_position (constructor_unfilled_fields);
10303 if (tree_int_cst_lt (bitpos, unfillpos))
10304 set_nonincremental_init (braced_init_obstack);
10308 add_pending_init (loc, field, value, origtype, implicit,
10309 braced_init_obstack);
10312 else if (TREE_CODE (constructor_type) == UNION_TYPE
10313 && !vec_safe_is_empty (constructor_elements))
10317 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
10318 warning_init (loc, OPT_Woverride_init_side_effects,
10319 "initialized field with side-effects overwritten");
10320 else if (warn_override_init)
10321 warning_init (loc, OPT_Woverride_init,
10322 "initialized field overwritten");
10325 /* We can have just one union field set. */
10326 constructor_elements = NULL;
10329 /* Otherwise, output this element either to
10330 constructor_elements or to the assembler file. */
10332 constructor_elt celt = {field, value};
10333 vec_safe_push (constructor_elements, celt);
10335 /* Advance the variable that indicates sequential elements output. */
10336 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10337 constructor_unfilled_index
10338 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
10340 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10342 constructor_unfilled_fields
10343 = DECL_CHAIN (constructor_unfilled_fields);
10345 /* Skip any nameless bit fields. */
10346 while (constructor_unfilled_fields != NULL_TREE
10347 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10348 constructor_unfilled_fields =
10349 DECL_CHAIN (constructor_unfilled_fields);
10351 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10352 constructor_unfilled_fields = NULL_TREE;
10354 /* Now output any pending elements which have become next. */
10356 output_pending_init_elements (0, braced_init_obstack);
10359 /* For two FIELD_DECLs in the same chain, return -1 if field1
10360 comes before field2, 1 if field1 comes after field2 and
10361 0 if field1 == field2. */
10364 init_field_decl_cmp (tree field1, tree field2)
10366 if (field1 == field2)
10369 tree bitpos1 = bit_position (field1);
10370 tree bitpos2 = bit_position (field2);
10371 if (tree_int_cst_equal (bitpos1, bitpos2))
10373 /* If one of the fields has non-zero bitsize, then that
10374 field must be the last one in a sequence of zero
10375 sized fields, fields after it will have bigger
10377 if (TREE_TYPE (field1) != error_mark_node
10378 && COMPLETE_TYPE_P (TREE_TYPE (field1))
10379 && integer_nonzerop (TREE_TYPE (field1)))
10381 if (TREE_TYPE (field2) != error_mark_node
10382 && COMPLETE_TYPE_P (TREE_TYPE (field2))
10383 && integer_nonzerop (TREE_TYPE (field2)))
10385 /* Otherwise, fallback to DECL_CHAIN walk to find out
10386 which field comes earlier. Walk chains of both
10387 fields, so that if field1 and field2 are close to each
10388 other in either order, it is found soon even for large
10389 sequences of zero sized fields. */
10390 tree f1 = field1, f2 = field2;
10393 f1 = DECL_CHAIN (f1);
10394 f2 = DECL_CHAIN (f2);
10395 if (f1 == NULL_TREE)
10400 if (f2 == NULL_TREE)
10406 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10408 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10412 else if (tree_int_cst_lt (bitpos1, bitpos2))
10418 /* Output any pending elements which have become next.
10419 As we output elements, constructor_unfilled_{fields,index}
10420 advances, which may cause other elements to become next;
10421 if so, they too are output.
10423 If ALL is 0, we return when there are
10424 no more pending elements to output now.
10426 If ALL is 1, we output space as necessary so that
10427 we can output all the pending elements. */
10429 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10431 struct init_node *elt = constructor_pending_elts;
10436 /* Look through the whole pending tree.
10437 If we find an element that should be output now,
10438 output it. Otherwise, set NEXT to the element
10439 that comes first among those still pending. */
10444 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10446 if (tree_int_cst_equal (elt->purpose,
10447 constructor_unfilled_index))
10448 output_init_element (input_location, elt->value, elt->origtype,
10449 true, TREE_TYPE (constructor_type),
10450 constructor_unfilled_index, false, false,
10451 braced_init_obstack);
10452 else if (tree_int_cst_lt (constructor_unfilled_index,
10455 /* Advance to the next smaller node. */
10460 /* We have reached the smallest node bigger than the
10461 current unfilled index. Fill the space first. */
10462 next = elt->purpose;
10468 /* Advance to the next bigger node. */
10473 /* We have reached the biggest node in a subtree. Find
10474 the parent of it, which is the next bigger node. */
10475 while (elt->parent && elt->parent->right == elt)
10478 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10481 next = elt->purpose;
10487 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10489 /* If the current record is complete we are done. */
10490 if (constructor_unfilled_fields == NULL_TREE)
10493 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10496 output_init_element (input_location, elt->value, elt->origtype,
10497 true, TREE_TYPE (elt->purpose),
10498 elt->purpose, false, false,
10499 braced_init_obstack);
10502 /* Advance to the next smaller node. */
10507 /* We have reached the smallest node bigger than the
10508 current unfilled field. Fill the space first. */
10509 next = elt->purpose;
10515 /* Advance to the next bigger node. */
10520 /* We have reached the biggest node in a subtree. Find
10521 the parent of it, which is the next bigger node. */
10522 while (elt->parent && elt->parent->right == elt)
10526 && init_field_decl_cmp (constructor_unfilled_fields,
10529 next = elt->purpose;
10537 /* Ordinarily return, but not if we want to output all
10538 and there are elements left. */
10539 if (!(all && next != NULL_TREE))
10542 /* If it's not incremental, just skip over the gap, so that after
10543 jumping to retry we will output the next successive element. */
10544 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10545 constructor_unfilled_fields = next;
10546 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10547 constructor_unfilled_index = next;
10549 /* ELT now points to the node in the pending tree with the next
10550 initializer to output. */
10554 /* Expression VALUE coincides with the start of type TYPE in a braced
10555 initializer. Return true if we should treat VALUE as initializing
10556 the first element of TYPE, false if we should treat it as initializing
10559 If the initializer is clearly invalid, the question becomes:
10560 which choice gives the best error message? */
10563 initialize_elementwise_p (tree type, tree value)
10565 if (type == error_mark_node || value == error_mark_node)
10568 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10570 tree value_type = TREE_TYPE (value);
10571 if (value_type == error_mark_node)
10574 /* GNU vectors can be initialized elementwise. However, treat any
10575 kind of vector value as initializing the vector type as a whole,
10576 regardless of whether the value is a GNU vector. Such initializers
10577 are valid if and only if they would have been valid in a non-braced
10582 so recursing into the vector type would be at best confusing or at
10583 worst wrong. For example, when -flax-vector-conversions is in effect,
10584 it's possible to initialize a V8HI from a V4SI, even though the vectors
10585 have different element types and different numbers of elements. */
10586 if (gnu_vector_type_p (type))
10587 return !VECTOR_TYPE_P (value_type);
10589 if (AGGREGATE_TYPE_P (type))
10590 return type != TYPE_MAIN_VARIANT (value_type);
10595 /* Add one non-braced element to the current constructor level.
10596 This adjusts the current position within the constructor's type.
10597 This may also start or terminate implicit levels
10598 to handle a partly-braced initializer.
10600 Once this has found the correct level for the new element,
10601 it calls output_init_element.
10603 IMPLICIT is true if value comes from pop_init_level (1),
10604 the new initializer has been merged with the existing one
10605 and thus no warnings should be emitted about overriding an
10606 existing initializer. */
10609 process_init_element (location_t loc, struct c_expr value, bool implicit,
10610 struct obstack * braced_init_obstack)
10612 tree orig_value = value.value;
10614 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10615 bool strict_string = value.original_code == STRING_CST;
10616 bool was_designated = designator_depth != 0;
10618 designator_depth = 0;
10619 designator_erroneous = 0;
10621 if (!implicit && value.value && !integer_zerop (value.value))
10622 constructor_zeroinit = 0;
10624 /* Handle superfluous braces around string cst as in
10625 char x[] = {"foo"}; */
10627 && constructor_type
10629 && TREE_CODE (constructor_type) == ARRAY_TYPE
10630 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10631 && integer_zerop (constructor_unfilled_index))
10633 if (constructor_stack->replacement_value.value)
10634 error_init (loc, "excess elements in %<char%> array initializer");
10635 constructor_stack->replacement_value = value;
10639 if (constructor_stack->replacement_value.value != NULL_TREE)
10641 error_init (loc, "excess elements in struct initializer");
10645 /* Ignore elements of a brace group if it is entirely superfluous
10646 and has already been diagnosed, or if the type is erroneous. */
10647 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10650 /* Ignore elements of an initializer for a variable-size type.
10651 Those are diagnosed in the parser (empty initializer braces are OK). */
10652 if (COMPLETE_TYPE_P (constructor_type)
10653 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10656 if (!implicit && warn_designated_init && !was_designated
10657 && TREE_CODE (constructor_type) == RECORD_TYPE
10658 && lookup_attribute ("designated_init",
10659 TYPE_ATTRIBUTES (constructor_type)))
10661 OPT_Wdesignated_init,
10662 "positional initialization of field "
10663 "in %<struct%> declared with %<designated_init%> attribute");
10665 /* If we've exhausted any levels that didn't have braces,
10667 while (constructor_stack->implicit)
10669 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10670 && constructor_fields == NULL_TREE)
10671 process_init_element (loc,
10672 pop_init_level (loc, 1, braced_init_obstack,
10673 last_init_list_comma),
10674 true, braced_init_obstack);
10675 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10676 || gnu_vector_type_p (constructor_type))
10677 && constructor_max_index
10678 && tree_int_cst_lt (constructor_max_index,
10679 constructor_index))
10680 process_init_element (loc,
10681 pop_init_level (loc, 1, braced_init_obstack,
10682 last_init_list_comma),
10683 true, braced_init_obstack);
10688 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10689 if (constructor_range_stack)
10691 /* If value is a compound literal and we'll be just using its
10692 content, don't put it into a SAVE_EXPR. */
10693 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10694 || !require_constant_value)
10696 tree semantic_type = NULL_TREE;
10697 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10699 semantic_type = TREE_TYPE (value.value);
10700 value.value = TREE_OPERAND (value.value, 0);
10702 value.value = save_expr (value.value);
10704 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10711 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10714 enum tree_code fieldcode;
10716 if (constructor_fields == NULL_TREE)
10718 pedwarn_init (loc, 0, "excess elements in struct initializer");
10722 fieldtype = TREE_TYPE (constructor_fields);
10723 if (fieldtype != error_mark_node)
10724 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10725 fieldcode = TREE_CODE (fieldtype);
10727 /* Error for non-static initialization of a flexible array member. */
10728 if (fieldcode == ARRAY_TYPE
10729 && !require_constant_value
10730 && TYPE_SIZE (fieldtype) == NULL_TREE
10731 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10733 error_init (loc, "non-static initialization of a flexible "
10738 /* Error for initialization of a flexible array member with
10739 a string constant if the structure is in an array. E.g.:
10740 struct S { int x; char y[]; };
10741 struct S s[] = { { 1, "foo" } };
10744 && fieldcode == ARRAY_TYPE
10745 && constructor_depth > 1
10746 && TYPE_SIZE (fieldtype) == NULL_TREE
10747 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10749 bool in_array_p = false;
10750 for (struct constructor_stack *p = constructor_stack;
10751 p && p->type; p = p->next)
10752 if (TREE_CODE (p->type) == ARRAY_TYPE)
10759 error_init (loc, "initialization of flexible array "
10760 "member in a nested context");
10765 /* Accept a string constant to initialize a subarray. */
10766 if (value.value != NULL_TREE
10767 && fieldcode == ARRAY_TYPE
10768 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10770 value.value = orig_value;
10771 /* Otherwise, if we have come to a subaggregate,
10772 and we don't have an element of its type, push into it. */
10773 else if (value.value != NULL_TREE
10774 && initialize_elementwise_p (fieldtype, value.value))
10776 push_init_level (loc, 1, braced_init_obstack);
10782 push_member_name (constructor_fields);
10783 output_init_element (loc, value.value, value.original_type,
10784 strict_string, fieldtype,
10785 constructor_fields, true, implicit,
10786 braced_init_obstack);
10787 RESTORE_SPELLING_DEPTH (constructor_depth);
10790 /* Do the bookkeeping for an element that was
10791 directly output as a constructor. */
10793 /* For a record, keep track of end position of last field. */
10794 if (DECL_SIZE (constructor_fields))
10795 constructor_bit_index
10796 = size_binop_loc (input_location, PLUS_EXPR,
10797 bit_position (constructor_fields),
10798 DECL_SIZE (constructor_fields));
10800 /* If the current field was the first one not yet written out,
10801 it isn't now, so update. */
10802 if (constructor_unfilled_fields == constructor_fields)
10804 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10805 /* Skip any nameless bit fields. */
10806 while (constructor_unfilled_fields != 0
10807 && (DECL_UNNAMED_BIT_FIELD
10808 (constructor_unfilled_fields)))
10809 constructor_unfilled_fields =
10810 DECL_CHAIN (constructor_unfilled_fields);
10814 constructor_fields = DECL_CHAIN (constructor_fields);
10815 /* Skip any nameless bit fields at the beginning. */
10816 while (constructor_fields != NULL_TREE
10817 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10818 constructor_fields = DECL_CHAIN (constructor_fields);
10820 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10823 enum tree_code fieldcode;
10825 if (constructor_fields == NULL_TREE)
10827 pedwarn_init (loc, 0,
10828 "excess elements in union initializer");
10832 fieldtype = TREE_TYPE (constructor_fields);
10833 if (fieldtype != error_mark_node)
10834 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10835 fieldcode = TREE_CODE (fieldtype);
10837 /* Warn that traditional C rejects initialization of unions.
10838 We skip the warning if the value is zero. This is done
10839 under the assumption that the zero initializer in user
10840 code appears conditioned on e.g. __STDC__ to avoid
10841 "missing initializer" warnings and relies on default
10842 initialization to zero in the traditional C case.
10843 We also skip the warning if the initializer is designated,
10844 again on the assumption that this must be conditional on
10845 __STDC__ anyway (and we've already complained about the
10846 member-designator already). */
10847 if (!in_system_header_at (input_location) && !constructor_designated
10848 && !(value.value && (integer_zerop (value.value)
10849 || real_zerop (value.value))))
10850 warning (OPT_Wtraditional, "traditional C rejects initialization "
10853 /* Accept a string constant to initialize a subarray. */
10854 if (value.value != NULL_TREE
10855 && fieldcode == ARRAY_TYPE
10856 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10858 value.value = orig_value;
10859 /* Otherwise, if we have come to a subaggregate,
10860 and we don't have an element of its type, push into it. */
10861 else if (value.value != NULL_TREE
10862 && initialize_elementwise_p (fieldtype, value.value))
10864 push_init_level (loc, 1, braced_init_obstack);
10870 push_member_name (constructor_fields);
10871 output_init_element (loc, value.value, value.original_type,
10872 strict_string, fieldtype,
10873 constructor_fields, true, implicit,
10874 braced_init_obstack);
10875 RESTORE_SPELLING_DEPTH (constructor_depth);
10878 /* Do the bookkeeping for an element that was
10879 directly output as a constructor. */
10881 constructor_bit_index = DECL_SIZE (constructor_fields);
10882 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10885 constructor_fields = NULL_TREE;
10887 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10889 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10890 enum tree_code eltcode = TREE_CODE (elttype);
10892 /* Accept a string constant to initialize a subarray. */
10893 if (value.value != NULL_TREE
10894 && eltcode == ARRAY_TYPE
10895 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10897 value.value = orig_value;
10898 /* Otherwise, if we have come to a subaggregate,
10899 and we don't have an element of its type, push into it. */
10900 else if (value.value != NULL_TREE
10901 && initialize_elementwise_p (elttype, value.value))
10903 push_init_level (loc, 1, braced_init_obstack);
10907 if (constructor_max_index != NULL_TREE
10908 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10909 || integer_all_onesp (constructor_max_index)))
10911 pedwarn_init (loc, 0,
10912 "excess elements in array initializer");
10916 /* Now output the actual element. */
10919 push_array_bounds (tree_to_uhwi (constructor_index));
10920 output_init_element (loc, value.value, value.original_type,
10921 strict_string, elttype,
10922 constructor_index, true, implicit,
10923 braced_init_obstack);
10924 RESTORE_SPELLING_DEPTH (constructor_depth);
10928 = size_binop_loc (input_location, PLUS_EXPR,
10929 constructor_index, bitsize_one_node);
10932 /* If we are doing the bookkeeping for an element that was
10933 directly output as a constructor, we must update
10934 constructor_unfilled_index. */
10935 constructor_unfilled_index = constructor_index;
10937 else if (gnu_vector_type_p (constructor_type))
10939 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10941 /* Do a basic check of initializer size. Note that vectors
10942 always have a fixed size derived from their type. */
10943 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10945 pedwarn_init (loc, 0,
10946 "excess elements in vector initializer");
10950 /* Now output the actual element. */
10953 if (TREE_CODE (value.value) == VECTOR_CST)
10954 elttype = TYPE_MAIN_VARIANT (constructor_type);
10955 output_init_element (loc, value.value, value.original_type,
10956 strict_string, elttype,
10957 constructor_index, true, implicit,
10958 braced_init_obstack);
10962 = size_binop_loc (input_location,
10963 PLUS_EXPR, constructor_index, bitsize_one_node);
10966 /* If we are doing the bookkeeping for an element that was
10967 directly output as a constructor, we must update
10968 constructor_unfilled_index. */
10969 constructor_unfilled_index = constructor_index;
10972 /* Handle the sole element allowed in a braced initializer
10973 for a scalar variable. */
10974 else if (constructor_type != error_mark_node
10975 && constructor_fields == NULL_TREE)
10977 pedwarn_init (loc, 0,
10978 "excess elements in scalar initializer");
10984 output_init_element (loc, value.value, value.original_type,
10985 strict_string, constructor_type,
10986 NULL_TREE, true, implicit,
10987 braced_init_obstack);
10988 constructor_fields = NULL_TREE;
10991 /* Handle range initializers either at this level or anywhere higher
10992 in the designator stack. */
10993 if (constructor_range_stack)
10995 struct constructor_range_stack *p, *range_stack;
10998 range_stack = constructor_range_stack;
10999 constructor_range_stack = 0;
11000 while (constructor_stack != range_stack->stack)
11002 gcc_assert (constructor_stack->implicit);
11003 process_init_element (loc,
11004 pop_init_level (loc, 1,
11005 braced_init_obstack,
11006 last_init_list_comma),
11007 true, braced_init_obstack);
11009 for (p = range_stack;
11010 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
11013 gcc_assert (constructor_stack->implicit);
11014 process_init_element (loc,
11015 pop_init_level (loc, 1,
11016 braced_init_obstack,
11017 last_init_list_comma),
11018 true, braced_init_obstack);
11021 p->index = size_binop_loc (input_location,
11022 PLUS_EXPR, p->index, bitsize_one_node);
11023 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
11028 constructor_index = p->index;
11029 constructor_fields = p->fields;
11030 if (finish && p->range_end && p->index == p->range_start)
11038 finish_implicit_inits (loc, braced_init_obstack);
11039 push_init_level (loc, 2, braced_init_obstack);
11040 p->stack = constructor_stack;
11041 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
11042 p->index = p->range_start;
11046 constructor_range_stack = range_stack;
11053 constructor_range_stack = 0;
11056 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
11057 (guaranteed to be 'volatile' or null) and ARGS (represented using
11058 an ASM_EXPR node). */
11060 build_asm_stmt (bool is_volatile, tree args)
11063 ASM_VOLATILE_P (args) = 1;
11064 return add_stmt (args);
11067 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
11068 some INPUTS, and some CLOBBERS. The latter three may be NULL.
11069 SIMPLE indicates whether there was anything at all after the
11070 string in the asm expression -- asm("blah") and asm("blah" : )
11071 are subtly different. We use a ASM_EXPR node to represent this.
11072 LOC is the location of the asm, and IS_INLINE says whether this
11075 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
11076 tree clobbers, tree labels, bool simple, bool is_inline)
11081 const char *constraint;
11082 const char **oconstraints;
11083 bool allows_mem, allows_reg, is_inout;
11084 int ninputs, noutputs;
11086 ninputs = list_length (inputs);
11087 noutputs = list_length (outputs);
11088 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
11090 string = resolve_asm_operand_names (string, outputs, inputs, labels);
11092 /* Remove output conversions that change the type but not the mode. */
11093 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
11095 tree output = TREE_VALUE (tail);
11097 output = c_fully_fold (output, false, NULL, true);
11099 /* ??? Really, this should not be here. Users should be using a
11100 proper lvalue, dammit. But there's a long history of using casts
11101 in the output operands. In cases like longlong.h, this becomes a
11102 primitive form of typechecking -- if the cast can be removed, then
11103 the output operand had a type of the proper width; otherwise we'll
11104 get an error. Gross, but ... */
11105 STRIP_NOPS (output);
11107 if (!lvalue_or_else (loc, output, lv_asm))
11108 output = error_mark_node;
11110 if (output != error_mark_node
11111 && (TREE_READONLY (output)
11112 || TYPE_READONLY (TREE_TYPE (output))
11113 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
11114 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
11115 readonly_error (loc, output, lv_asm);
11117 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11118 oconstraints[i] = constraint;
11120 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
11121 &allows_mem, &allows_reg, &is_inout))
11123 /* If the operand is going to end up in memory,
11124 mark it addressable. */
11125 if (!allows_reg && !c_mark_addressable (output))
11126 output = error_mark_node;
11127 if (!(!allows_reg && allows_mem)
11128 && output != error_mark_node
11129 && VOID_TYPE_P (TREE_TYPE (output)))
11131 error_at (loc, "invalid use of void expression");
11132 output = error_mark_node;
11136 output = error_mark_node;
11138 TREE_VALUE (tail) = output;
11141 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
11145 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11146 input = TREE_VALUE (tail);
11148 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
11149 oconstraints, &allows_mem, &allows_reg))
11151 /* If the operand is going to end up in memory,
11152 mark it addressable. */
11153 if (!allows_reg && allows_mem)
11155 input = c_fully_fold (input, false, NULL, true);
11157 /* Strip the nops as we allow this case. FIXME, this really
11158 should be rejected or made deprecated. */
11159 STRIP_NOPS (input);
11160 if (!c_mark_addressable (input))
11161 input = error_mark_node;
11165 struct c_expr expr;
11166 memset (&expr, 0, sizeof (expr));
11167 expr.value = input;
11168 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
11169 input = c_fully_fold (expr.value, false, NULL);
11171 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
11173 error_at (loc, "invalid use of void expression");
11174 input = error_mark_node;
11179 input = error_mark_node;
11181 TREE_VALUE (tail) = input;
11184 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
11186 /* asm statements without outputs, including simple ones, are treated
11188 ASM_INPUT_P (args) = simple;
11189 ASM_VOLATILE_P (args) = (noutputs == 0);
11190 ASM_INLINE_P (args) = is_inline;
11195 /* Generate a goto statement to LABEL. LOC is the location of the
11199 c_finish_goto_label (location_t loc, tree label)
11201 tree decl = lookup_label_for_goto (loc, label);
11204 TREE_USED (decl) = 1;
11206 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
11207 tree t = build1 (GOTO_EXPR, void_type_node, decl);
11208 SET_EXPR_LOCATION (t, loc);
11209 return add_stmt (t);
11213 /* Generate a computed goto statement to EXPR. LOC is the location of
11217 c_finish_goto_ptr (location_t loc, c_expr val)
11219 tree expr = val.value;
11221 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
11222 if (expr != error_mark_node
11223 && !POINTER_TYPE_P (TREE_TYPE (expr))
11224 && !null_pointer_constant_p (expr))
11226 error_at (val.get_location (),
11227 "computed goto must be pointer type");
11228 expr = build_zero_cst (ptr_type_node);
11230 expr = c_fully_fold (expr, false, NULL);
11231 expr = convert (ptr_type_node, expr);
11232 t = build1 (GOTO_EXPR, void_type_node, expr);
11233 SET_EXPR_LOCATION (t, loc);
11234 return add_stmt (t);
11237 /* Generate a C `return' statement. RETVAL is the expression for what
11238 to return, or a null pointer for `return;' with no value. LOC is
11239 the location of the return statement, or the location of the expression,
11240 if the statement has any. If ORIGTYPE is not NULL_TREE, it
11241 is the original type of RETVAL. */
11244 c_finish_return (location_t loc, tree retval, tree origtype)
11246 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
11247 bool no_warning = false;
11250 /* Use the expansion point to handle cases such as returning NULL
11251 in a function returning void. */
11252 location_t xloc = expansion_point_location_if_in_system_header (loc);
11254 if (TREE_THIS_VOLATILE (current_function_decl))
11255 warning_at (xloc, 0,
11256 "function declared %<noreturn%> has a %<return%> statement");
11260 tree semantic_type = NULL_TREE;
11261 npc = null_pointer_constant_p (retval);
11262 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
11264 semantic_type = TREE_TYPE (retval);
11265 retval = TREE_OPERAND (retval, 0);
11267 retval = c_fully_fold (retval, false, NULL);
11269 && valtype != NULL_TREE
11270 && TREE_CODE (valtype) != VOID_TYPE)
11271 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
11276 current_function_returns_null = 1;
11277 if ((warn_return_type >= 0 || flag_isoc99)
11278 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
11282 warned_here = pedwarn
11283 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11284 "%<return%> with no value, in function returning non-void");
11286 warned_here = warning_at
11287 (loc, OPT_Wreturn_type,
11288 "%<return%> with no value, in function returning non-void");
11291 inform (DECL_SOURCE_LOCATION (current_function_decl),
11295 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
11297 current_function_returns_null = 1;
11299 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
11300 warned_here = pedwarn
11301 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11302 "%<return%> with a value, in function returning void");
11304 warned_here = pedwarn
11305 (xloc, OPT_Wpedantic, "ISO C forbids "
11306 "%<return%> with expression, in function returning void");
11308 inform (DECL_SOURCE_LOCATION (current_function_decl),
11313 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
11314 retval, origtype, ic_return,
11315 npc, NULL_TREE, NULL_TREE, 0);
11316 tree res = DECL_RESULT (current_function_decl);
11320 current_function_returns_value = 1;
11321 if (t == error_mark_node)
11324 save = in_late_binary_op;
11325 if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
11326 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
11327 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
11328 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
11329 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
11330 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
11331 in_late_binary_op = true;
11332 inner = t = convert (TREE_TYPE (res), t);
11333 in_late_binary_op = save;
11335 /* Strip any conversions, additions, and subtractions, and see if
11336 we are returning the address of a local variable. Warn if so. */
11339 switch (TREE_CODE (inner))
11342 case NON_LVALUE_EXPR:
11344 case POINTER_PLUS_EXPR:
11345 inner = TREE_OPERAND (inner, 0);
11349 /* If the second operand of the MINUS_EXPR has a pointer
11350 type (or is converted from it), this may be valid, so
11351 don't give a warning. */
11353 tree op1 = TREE_OPERAND (inner, 1);
11355 while (!POINTER_TYPE_P (TREE_TYPE (op1))
11356 && (CONVERT_EXPR_P (op1)
11357 || TREE_CODE (op1) == NON_LVALUE_EXPR))
11358 op1 = TREE_OPERAND (op1, 0);
11360 if (POINTER_TYPE_P (TREE_TYPE (op1)))
11363 inner = TREE_OPERAND (inner, 0);
11368 inner = TREE_OPERAND (inner, 0);
11370 while (REFERENCE_CLASS_P (inner)
11371 && !INDIRECT_REF_P (inner))
11372 inner = TREE_OPERAND (inner, 0);
11375 && !DECL_EXTERNAL (inner)
11376 && !TREE_STATIC (inner)
11377 && DECL_CONTEXT (inner) == current_function_decl
11378 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11380 if (TREE_CODE (inner) == LABEL_DECL)
11381 warning_at (loc, OPT_Wreturn_local_addr,
11382 "function returns address of label");
11385 warning_at (loc, OPT_Wreturn_local_addr,
11386 "function returns address of local variable");
11387 tree zero = build_zero_cst (TREE_TYPE (res));
11388 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11400 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11401 SET_EXPR_LOCATION (retval, loc);
11403 if (warn_sequence_point)
11404 verify_sequence_points (retval);
11407 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11409 suppress_warning (ret_stmt, OPT_Wreturn_type);
11410 return add_stmt (ret_stmt);
11414 /* The SWITCH_STMT being built. */
11417 /* The original type of the testing expression, i.e. before the
11418 default conversion is applied. */
11421 /* A splay-tree mapping the low element of a case range to the high
11422 element, or NULL_TREE if there is no high element. Used to
11423 determine whether or not a new case label duplicates an old case
11424 label. We need a tree, rather than simply a hash table, because
11425 of the GNU case range extension. */
11428 /* The bindings at the point of the switch. This is used for
11429 warnings crossing decls when branching to a case label. */
11430 struct c_spot_bindings *bindings;
11432 /* Whether the switch includes any break statements. */
11433 bool break_stmt_seen_p;
11435 /* The next node on the stack. */
11436 struct c_switch *next;
11438 /* Remember whether the controlling expression had boolean type
11439 before integer promotions for the sake of -Wswitch-bool. */
11443 /* A stack of the currently active switch statements. The innermost
11444 switch statement is on the top of the stack. There is no need to
11445 mark the stack for garbage collection because it is only active
11446 during the processing of the body of a function, and we never
11447 collect at that point. */
11449 struct c_switch *c_switch_stack;
11451 /* Start a C switch statement, testing expression EXP. Return the new
11452 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11453 SWITCH_COND_LOC is the location of the switch's condition.
11454 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11457 c_start_switch (location_t switch_loc,
11458 location_t switch_cond_loc,
11459 tree exp, bool explicit_cast_p)
11461 tree orig_type = error_mark_node;
11462 bool bool_cond_p = false;
11463 struct c_switch *cs;
11465 if (exp != error_mark_node)
11467 orig_type = TREE_TYPE (exp);
11469 if (!INTEGRAL_TYPE_P (orig_type))
11471 if (orig_type != error_mark_node)
11473 error_at (switch_cond_loc, "switch quantity not an integer");
11474 orig_type = error_mark_node;
11476 exp = integer_zero_node;
11480 tree type = TYPE_MAIN_VARIANT (orig_type);
11483 /* Warn if the condition has boolean value. */
11484 while (TREE_CODE (e) == COMPOUND_EXPR)
11485 e = TREE_OPERAND (e, 1);
11487 if ((C_BOOLEAN_TYPE_P (type)
11488 || truth_value_p (TREE_CODE (e)))
11489 /* Explicit cast to int suppresses this warning. */
11490 && !(TREE_CODE (type) == INTEGER_TYPE
11491 && explicit_cast_p))
11492 bool_cond_p = true;
11494 if (!in_system_header_at (input_location)
11495 && (type == long_integer_type_node
11496 || type == long_unsigned_type_node))
11497 warning_at (switch_cond_loc,
11498 OPT_Wtraditional, "%<long%> switch expression not "
11499 "converted to %<int%> in ISO C");
11501 exp = c_fully_fold (exp, false, NULL);
11502 exp = default_conversion (exp);
11504 if (warn_sequence_point)
11505 verify_sequence_points (exp);
11509 /* Add this new SWITCH_STMT to the stack. */
11510 cs = XNEW (struct c_switch);
11511 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11512 NULL_TREE, orig_type, NULL_TREE);
11513 cs->orig_type = orig_type;
11514 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11515 cs->bindings = c_get_switch_bindings ();
11516 cs->break_stmt_seen_p = false;
11517 cs->bool_cond_p = bool_cond_p;
11518 cs->next = c_switch_stack;
11519 c_switch_stack = cs;
11521 return add_stmt (cs->switch_stmt);
11524 /* Process a case label at location LOC, with attributes ATTRS. */
11527 do_case (location_t loc, tree low_value, tree high_value, tree attrs)
11529 tree label = NULL_TREE;
11531 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11533 low_value = c_fully_fold (low_value, false, NULL);
11534 if (TREE_CODE (low_value) == INTEGER_CST)
11535 pedwarn (loc, OPT_Wpedantic,
11536 "case label is not an integer constant expression");
11539 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11541 high_value = c_fully_fold (high_value, false, NULL);
11542 if (TREE_CODE (high_value) == INTEGER_CST)
11543 pedwarn (input_location, OPT_Wpedantic,
11544 "case label is not an integer constant expression");
11547 if (c_switch_stack == NULL)
11550 error_at (loc, "case label not within a switch statement");
11552 error_at (loc, "%<default%> label not within a switch statement");
11556 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11557 EXPR_LOCATION (c_switch_stack->switch_stmt),
11561 label = c_add_case_label (loc, c_switch_stack->cases,
11562 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11563 low_value, high_value, attrs);
11564 if (label == error_mark_node)
11569 /* Finish the switch statement. TYPE is the original type of the
11570 controlling expression of the switch, or NULL_TREE. */
11573 c_finish_switch (tree body, tree type)
11575 struct c_switch *cs = c_switch_stack;
11576 location_t switch_location;
11578 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11580 /* Emit warnings as needed. */
11581 switch_location = EXPR_LOCATION (cs->switch_stmt);
11582 c_do_switch_warnings (cs->cases, switch_location,
11583 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11584 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11585 if (c_switch_covers_all_cases_p (cs->cases,
11586 SWITCH_STMT_TYPE (cs->switch_stmt)))
11587 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11588 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11590 /* Pop the stack. */
11591 c_switch_stack = cs->next;
11592 splay_tree_delete (cs->cases);
11593 c_release_switch_bindings (cs->bindings);
11597 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11598 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11602 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11607 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11608 SET_EXPR_LOCATION (stmt, if_locus);
11613 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11615 /* In switch statements break is sometimes stylistically used after
11616 a return statement. This can lead to spurious warnings about
11617 control reaching the end of a non-void function when it is
11618 inlined. Note that we are calling block_may_fallthru with
11619 language specific tree nodes; this works because
11620 block_may_fallthru returns true when given something it does not
11622 bool skip = !block_may_fallthru (cur_stmt_list);
11625 switch (in_statement)
11628 error_at (loc, "break statement not within loop or switch");
11631 error_at (loc, "invalid exit from OpenMP structured block");
11634 error_at (loc, "break statement used with OpenMP for loop");
11636 case IN_ITERATION_STMT:
11637 case IN_OBJC_FOREACH:
11640 gcc_assert (in_statement & IN_SWITCH_STMT);
11641 c_switch_stack->break_stmt_seen_p = true;
11645 switch (in_statement & ~IN_SWITCH_STMT)
11648 error_at (loc, "continue statement not within a loop");
11651 error_at (loc, "invalid exit from OpenMP structured block");
11653 case IN_ITERATION_STMT:
11655 case IN_OBJC_FOREACH:
11658 gcc_unreachable ();
11663 else if ((in_statement & IN_OBJC_FOREACH)
11664 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11666 /* The foreach expander produces low-level code using gotos instead
11667 of a structured loop construct. */
11668 gcc_assert (label);
11669 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11671 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11674 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11677 emit_side_effect_warnings (location_t loc, tree expr)
11679 maybe_warn_nodiscard (loc, expr);
11680 if (!warn_unused_value)
11682 if (expr == error_mark_node)
11684 else if (!TREE_SIDE_EFFECTS (expr))
11686 if (!VOID_TYPE_P (TREE_TYPE (expr))
11687 && !warning_suppressed_p (expr, OPT_Wunused_value))
11688 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11690 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11693 location_t cloc = loc;
11694 while (TREE_CODE (r) == COMPOUND_EXPR)
11696 if (EXPR_HAS_LOCATION (r))
11697 cloc = EXPR_LOCATION (r);
11698 r = TREE_OPERAND (r, 1);
11700 if (!TREE_SIDE_EFFECTS (r)
11701 && !VOID_TYPE_P (TREE_TYPE (r))
11702 && !CONVERT_EXPR_P (r)
11703 && !warning_suppressed_p (r, OPT_Wunused_value)
11704 && !warning_suppressed_p (expr, OPT_Wunused_value))
11705 warning_at (cloc, OPT_Wunused_value,
11706 "right-hand operand of comma expression has no effect");
11709 warn_if_unused_value (expr, loc);
11712 /* Process an expression as if it were a complete statement. Emit
11713 diagnostics, but do not call ADD_STMT. LOC is the location of the
11717 c_process_expr_stmt (location_t loc, tree expr)
11724 expr = c_fully_fold (expr, false, NULL);
11726 if (warn_sequence_point)
11727 verify_sequence_points (expr);
11729 if (TREE_TYPE (expr) != error_mark_node
11730 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11731 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11732 error_at (loc, "expression statement has incomplete type");
11734 /* If we're not processing a statement expression, warn about unused values.
11735 Warnings for statement expressions will be emitted later, once we figure
11736 out which is the result. */
11737 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11738 && (warn_unused_value || warn_unused_result))
11739 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11742 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11743 exprv = TREE_OPERAND (exprv, 1);
11744 while (CONVERT_EXPR_P (exprv))
11745 exprv = TREE_OPERAND (exprv, 0);
11747 || handled_component_p (exprv)
11748 || TREE_CODE (exprv) == ADDR_EXPR)
11749 mark_exp_read (exprv);
11751 /* If the expression is not of a type to which we cannot assign a line
11752 number, wrap the thing in a no-op NOP_EXPR. */
11753 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11755 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11756 SET_EXPR_LOCATION (expr, loc);
11762 /* Emit an expression as a statement. LOC is the location of the
11766 c_finish_expr_stmt (location_t loc, tree expr)
11769 return add_stmt (c_process_expr_stmt (loc, expr));
11774 /* Do the opposite and emit a statement as an expression. To begin,
11775 create a new binding level and return it. */
11778 c_begin_stmt_expr (void)
11782 /* We must force a BLOCK for this level so that, if it is not expanded
11783 later, there is a way to turn off the entire subtree of blocks that
11784 are contained in it. */
11785 keep_next_level ();
11786 ret = c_begin_compound_stmt (true);
11788 c_bindings_start_stmt_expr (c_switch_stack == NULL
11790 : c_switch_stack->bindings);
11792 /* Mark the current statement list as belonging to a statement list. */
11793 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11798 /* LOC is the location of the compound statement to which this body
11802 c_finish_stmt_expr (location_t loc, tree body)
11804 tree last, type, tmp, val;
11807 body = c_end_compound_stmt (loc, body, true);
11809 c_bindings_end_stmt_expr (c_switch_stack == NULL
11811 : c_switch_stack->bindings);
11813 /* Locate the last statement in BODY. See c_end_compound_stmt
11814 about always returning a BIND_EXPR. */
11815 last_p = &BIND_EXPR_BODY (body);
11816 last = BIND_EXPR_BODY (body);
11818 continue_searching:
11819 if (TREE_CODE (last) == STATEMENT_LIST)
11821 tree_stmt_iterator l = tsi_last (last);
11823 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11826 /* This can happen with degenerate cases like ({ }). No value. */
11830 /* If we're supposed to generate side effects warnings, process
11831 all of the statements except the last. */
11832 if (warn_unused_value || warn_unused_result)
11834 for (tree_stmt_iterator i = tsi_start (last);
11835 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11838 tree t = tsi_stmt (i);
11840 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11841 emit_side_effect_warnings (tloc, t);
11844 last_p = tsi_stmt_ptr (l);
11848 /* If the end of the list is exception related, then the list was split
11849 by a call to push_cleanup. Continue searching. */
11850 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11851 || TREE_CODE (last) == TRY_CATCH_EXPR)
11853 last_p = &TREE_OPERAND (last, 0);
11855 goto continue_searching;
11858 if (last == error_mark_node)
11861 /* In the case that the BIND_EXPR is not necessary, return the
11862 expression out from inside it. */
11863 if ((last == BIND_EXPR_BODY (body)
11864 /* Skip nested debug stmts. */
11865 || last == expr_first (BIND_EXPR_BODY (body)))
11866 && BIND_EXPR_VARS (body) == NULL)
11868 /* Even if this looks constant, do not allow it in a constant
11870 last = c_wrap_maybe_const (last, true);
11871 /* Do not warn if the return value of a statement expression is
11873 suppress_warning (last, OPT_Wunused);
11877 /* Extract the type of said expression. */
11878 type = TREE_TYPE (last);
11880 /* If we're not returning a value at all, then the BIND_EXPR that
11881 we already have is a fine expression to return. */
11882 if (!type || VOID_TYPE_P (type))
11885 /* Now that we've located the expression containing the value, it seems
11886 silly to make voidify_wrapper_expr repeat the process. Create a
11887 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11888 tmp = create_tmp_var_raw (type);
11890 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11891 tree_expr_nonnegative_p giving up immediately. */
11893 if (TREE_CODE (val) == NOP_EXPR
11894 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11895 val = TREE_OPERAND (val, 0);
11897 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11898 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11901 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11902 SET_EXPR_LOCATION (t, loc);
11907 /* Begin and end compound statements. This is as simple as pushing
11908 and popping new statement lists from the tree. */
11911 c_begin_compound_stmt (bool do_scope)
11913 tree stmt = push_stmt_list ();
11919 /* End a compound statement. STMT is the statement. LOC is the
11920 location of the compound statement-- this is usually the location
11921 of the opening brace. */
11924 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11930 if (c_dialect_objc ())
11931 objc_clear_super_receiver ();
11932 block = pop_scope ();
11935 stmt = pop_stmt_list (stmt);
11936 stmt = c_build_bind_expr (loc, block, stmt);
11938 /* If this compound statement is nested immediately inside a statement
11939 expression, then force a BIND_EXPR to be created. Otherwise we'll
11940 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11941 STATEMENT_LISTs merge, and thus we can lose track of what statement
11942 was really last. */
11943 if (building_stmt_list_p ()
11944 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11945 && TREE_CODE (stmt) != BIND_EXPR)
11947 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11948 TREE_SIDE_EFFECTS (stmt) = 1;
11949 SET_EXPR_LOCATION (stmt, loc);
11955 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11956 when the current scope is exited. EH_ONLY is true when this is not
11957 meant to apply to normal control flow transfer. */
11960 push_cleanup (tree decl, tree cleanup, bool eh_only)
11962 enum tree_code code;
11966 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11967 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11969 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11970 list = push_stmt_list ();
11971 TREE_OPERAND (stmt, 0) = list;
11972 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11975 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11976 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11979 build_vec_cmp (tree_code code, tree type,
11980 tree arg0, tree arg1)
11982 tree zero_vec = build_zero_cst (type);
11983 tree minus_one_vec = build_minus_one_cst (type);
11984 tree cmp_type = truth_type_for (type);
11985 tree cmp = build2 (code, cmp_type, arg0, arg1);
11986 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11989 /* Possibly warn about an address of OP never being NULL in a comparison
11990 operation CODE involving null. */
11993 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11995 /* Prevent warnings issued for macro expansion. */
11997 || warning_suppressed_p (op, OPT_Waddress)
11998 || from_macro_expansion_at (loc))
12001 if (TREE_CODE (op) == NOP_EXPR)
12003 /* Allow casts to intptr_t to suppress the warning. */
12004 tree type = TREE_TYPE (op);
12005 if (TREE_CODE (type) == INTEGER_TYPE)
12007 op = TREE_OPERAND (op, 0);
12010 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
12012 /* Allow a cast to void* to suppress the warning. */
12013 tree type = TREE_TYPE (TREE_TYPE (op));
12014 if (VOID_TYPE_P (type))
12017 /* Adding any value to a null pointer, including zero, is undefined
12018 in C. This includes the expression &p[0] where p is the null
12019 pointer, although &p[0] will have been folded to p by this point
12020 and so not diagnosed. */
12021 if (code == EQ_EXPR)
12022 warning_at (loc, OPT_Waddress,
12023 "the comparison will always evaluate as %<false%> "
12024 "for the pointer operand in %qE must not be NULL",
12027 warning_at (loc, OPT_Waddress,
12028 "the comparison will always evaluate as %<true%> "
12029 "for the pointer operand in %qE must not be NULL",
12035 if (TREE_CODE (op) != ADDR_EXPR)
12038 op = TREE_OPERAND (op, 0);
12040 if (TREE_CODE (op) == IMAGPART_EXPR
12041 || TREE_CODE (op) == REALPART_EXPR)
12043 /* The address of either complex part may not be null. */
12044 if (code == EQ_EXPR)
12045 warning_at (loc, OPT_Waddress,
12046 "the comparison will always evaluate as %<false%> "
12047 "for the address of %qE will never be NULL",
12050 warning_at (loc, OPT_Waddress,
12051 "the comparison will always evaluate as %<true%> "
12052 "for the address of %qE will never be NULL",
12057 /* Set to true in the loop below if OP dereferences is operand.
12058 In such a case the ultimate target need not be a decl for
12059 the null [in]equality test to be constant. */
12060 bool deref = false;
12062 /* Get the outermost array or object, or member. */
12063 while (handled_component_p (op))
12065 if (TREE_CODE (op) == COMPONENT_REF)
12067 /* Get the member (its address is never null). */
12068 op = TREE_OPERAND (op, 1);
12072 /* Get the outer array/object to refer to in the warning. */
12073 op = TREE_OPERAND (op, 0);
12077 if ((!deref && !decl_with_nonnull_addr_p (op))
12078 || from_macro_expansion_at (loc))
12082 if (code == EQ_EXPR)
12083 w = warning_at (loc, OPT_Waddress,
12084 "the comparison will always evaluate as %<false%> "
12085 "for the address of %qE will never be NULL",
12088 w = warning_at (loc, OPT_Waddress,
12089 "the comparison will always evaluate as %<true%> "
12090 "for the address of %qE will never be NULL",
12093 if (w && DECL_P (op))
12094 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
12097 /* Build a binary-operation expression without default conversions.
12098 CODE is the kind of expression to build.
12099 LOCATION is the operator's location.
12100 This function differs from `build' in several ways:
12101 the data type of the result is computed and recorded in it,
12102 warnings are generated if arg data types are invalid,
12103 special handling for addition and subtraction of pointers is known,
12104 and some optimization is done (operations on narrow ints
12105 are done in the narrower type when that gives the same result).
12106 Constant folding is also done before the result is returned.
12108 Note that the operands will never have enumeral types, or function
12109 or array types, because either they will have the default conversions
12110 performed or they have both just been converted to some other type in which
12111 the arithmetic is to be done. */
12114 build_binary_op (location_t location, enum tree_code code,
12115 tree orig_op0, tree orig_op1, bool convert_p)
12117 tree type0, type1, orig_type0, orig_type1;
12119 enum tree_code code0, code1;
12121 tree ret = error_mark_node;
12122 const char *invalid_op_diag;
12123 bool op0_int_operands, op1_int_operands;
12124 bool int_const, int_const_or_overflow, int_operands;
12126 /* Expression code to give to the expression when it is built.
12127 Normally this is CODE, which is what the caller asked for,
12128 but in some special cases we change it. */
12129 enum tree_code resultcode = code;
12131 /* Data type in which the computation is to be performed.
12132 In the simplest cases this is the common type of the arguments. */
12133 tree result_type = NULL;
12135 /* When the computation is in excess precision, the type of the
12136 final EXCESS_PRECISION_EXPR. */
12137 tree semantic_result_type = NULL;
12139 /* Nonzero means operands have already been type-converted
12140 in whatever way is necessary.
12141 Zero means they need to be converted to RESULT_TYPE. */
12144 /* Nonzero means create the expression with this type, rather than
12146 tree build_type = NULL_TREE;
12148 /* Nonzero means after finally constructing the expression
12149 convert it to this type. */
12150 tree final_type = NULL_TREE;
12152 /* Nonzero if this is an operation like MIN or MAX which can
12153 safely be computed in short if both args are promoted shorts.
12154 Also implies COMMON.
12155 -1 indicates a bitwise operation; this makes a difference
12156 in the exact conditions for when it is safe to do the operation
12157 in a narrower mode. */
12160 /* Nonzero if this is a comparison operation;
12161 if both args are promoted shorts, compare the original shorts.
12162 Also implies COMMON. */
12163 int short_compare = 0;
12165 /* Nonzero if this is a right-shift operation, which can be computed on the
12166 original short and then promoted if the operand is a promoted short. */
12167 int short_shift = 0;
12169 /* Nonzero means set RESULT_TYPE to the common type of the args. */
12172 /* True means types are compatible as far as ObjC is concerned. */
12175 /* True means this is an arithmetic operation that may need excess
12177 bool may_need_excess_precision;
12179 /* True means this is a boolean operation that converts both its
12180 operands to truth-values. */
12181 bool boolean_op = false;
12183 /* Remember whether we're doing / or %. */
12184 bool doing_div_or_mod = false;
12186 /* Remember whether we're doing << or >>. */
12187 bool doing_shift = false;
12189 /* Tree holding instrumentation expression. */
12190 tree instrument_expr = NULL;
12192 if (location == UNKNOWN_LOCATION)
12193 location = input_location;
12198 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
12199 if (op0_int_operands)
12200 op0 = remove_c_maybe_const_expr (op0);
12201 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
12202 if (op1_int_operands)
12203 op1 = remove_c_maybe_const_expr (op1);
12204 int_operands = (op0_int_operands && op1_int_operands);
12207 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
12208 && TREE_CODE (orig_op1) == INTEGER_CST);
12209 int_const = (int_const_or_overflow
12210 && !TREE_OVERFLOW (orig_op0)
12211 && !TREE_OVERFLOW (orig_op1));
12214 int_const = int_const_or_overflow = false;
12216 /* Do not apply default conversion in mixed vector/scalar expression. */
12218 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
12220 op0 = default_conversion (op0);
12221 op1 = default_conversion (op1);
12224 orig_type0 = type0 = TREE_TYPE (op0);
12226 orig_type1 = type1 = TREE_TYPE (op1);
12228 /* The expression codes of the data types of the arguments tell us
12229 whether the arguments are integers, floating, pointers, etc. */
12230 code0 = TREE_CODE (type0);
12231 code1 = TREE_CODE (type1);
12233 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
12234 STRIP_TYPE_NOPS (op0);
12235 STRIP_TYPE_NOPS (op1);
12237 /* If an error was already reported for one of the arguments,
12238 avoid reporting another error. */
12240 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12241 return error_mark_node;
12243 if (code0 == POINTER_TYPE
12244 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
12245 return error_mark_node;
12247 if (code1 == POINTER_TYPE
12248 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
12249 return error_mark_node;
12251 if ((invalid_op_diag
12252 = targetm.invalid_binary_op (code, type0, type1)))
12254 error_at (location, invalid_op_diag);
12255 return error_mark_node;
12263 case TRUNC_DIV_EXPR:
12264 case CEIL_DIV_EXPR:
12265 case FLOOR_DIV_EXPR:
12266 case ROUND_DIV_EXPR:
12267 case EXACT_DIV_EXPR:
12268 may_need_excess_precision = true;
12277 /* Excess precision for implicit conversions of integers to
12278 floating point in C11 and later. */
12279 may_need_excess_precision = (flag_isoc11
12280 && (ANY_INTEGRAL_TYPE_P (type0)
12281 || ANY_INTEGRAL_TYPE_P (type1)));
12285 may_need_excess_precision = false;
12288 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
12290 op0 = TREE_OPERAND (op0, 0);
12291 type0 = TREE_TYPE (op0);
12293 else if (may_need_excess_precision
12294 && (eptype = excess_precision_type (type0)) != NULL_TREE)
12297 op0 = convert (eptype, op0);
12299 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
12301 op1 = TREE_OPERAND (op1, 0);
12302 type1 = TREE_TYPE (op1);
12304 else if (may_need_excess_precision
12305 && (eptype = excess_precision_type (type1)) != NULL_TREE)
12308 op1 = convert (eptype, op1);
12311 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
12313 /* In case when one of the operands of the binary operation is
12314 a vector and another is a scalar -- convert scalar to vector. */
12315 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
12316 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
12318 enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
12321 switch (convert_flag)
12324 return error_mark_node;
12327 bool maybe_const = true;
12329 sc = c_fully_fold (op0, false, &maybe_const);
12330 sc = save_expr (sc);
12331 sc = convert (TREE_TYPE (type1), sc);
12332 op0 = build_vector_from_val (type1, sc);
12334 op0 = c_wrap_maybe_const (op0, true);
12335 orig_type0 = type0 = TREE_TYPE (op0);
12336 code0 = TREE_CODE (type0);
12340 case stv_secondarg:
12342 bool maybe_const = true;
12344 sc = c_fully_fold (op1, false, &maybe_const);
12345 sc = save_expr (sc);
12346 sc = convert (TREE_TYPE (type0), sc);
12347 op1 = build_vector_from_val (type0, sc);
12349 op1 = c_wrap_maybe_const (op1, true);
12350 orig_type1 = type1 = TREE_TYPE (op1);
12351 code1 = TREE_CODE (type1);
12363 /* Handle the pointer + int case. */
12364 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12366 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
12367 goto return_build_binary_op;
12369 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
12371 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12372 goto return_build_binary_op;
12379 /* Subtraction of two similar pointers.
12380 We must subtract them as integers, then divide by object size. */
12381 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12382 && comp_target_types (location, type0, type1))
12384 ret = pointer_diff (location, op0, op1, &instrument_expr);
12385 goto return_build_binary_op;
12387 /* Handle pointer minus int. Just like pointer plus int. */
12388 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12390 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12391 goto return_build_binary_op;
12401 case TRUNC_DIV_EXPR:
12402 case CEIL_DIV_EXPR:
12403 case FLOOR_DIV_EXPR:
12404 case ROUND_DIV_EXPR:
12405 case EXACT_DIV_EXPR:
12406 doing_div_or_mod = true;
12407 warn_for_div_by_zero (location, op1);
12409 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12410 || code0 == FIXED_POINT_TYPE
12411 || code0 == COMPLEX_TYPE
12412 || gnu_vector_type_p (type0))
12413 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12414 || code1 == FIXED_POINT_TYPE
12415 || code1 == COMPLEX_TYPE
12416 || gnu_vector_type_p (type1)))
12418 enum tree_code tcode0 = code0, tcode1 = code1;
12420 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12421 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12422 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12423 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12425 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12426 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12427 resultcode = RDIV_EXPR;
12429 /* Although it would be tempting to shorten always here, that
12430 loses on some targets, since the modulo instruction is
12431 undefined if the quotient can't be represented in the
12432 computation mode. We shorten only if unsigned or if
12433 dividing by something we know != -1. */
12434 shorten = may_shorten_divmod (op0, op1);
12442 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12444 /* Allow vector types which are not floating point types. */
12445 else if (gnu_vector_type_p (type0)
12446 && gnu_vector_type_p (type1)
12447 && !VECTOR_FLOAT_TYPE_P (type0)
12448 && !VECTOR_FLOAT_TYPE_P (type1))
12452 case TRUNC_MOD_EXPR:
12453 case FLOOR_MOD_EXPR:
12454 doing_div_or_mod = true;
12455 warn_for_div_by_zero (location, op1);
12457 if (gnu_vector_type_p (type0)
12458 && gnu_vector_type_p (type1)
12459 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12460 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12462 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12464 /* Although it would be tempting to shorten always here, that loses
12465 on some targets, since the modulo instruction is undefined if the
12466 quotient can't be represented in the computation mode. We shorten
12467 only if unsigned or if dividing by something we know != -1. */
12468 shorten = may_shorten_divmod (op0, op1);
12473 case TRUTH_ANDIF_EXPR:
12474 case TRUTH_ORIF_EXPR:
12475 case TRUTH_AND_EXPR:
12476 case TRUTH_OR_EXPR:
12477 case TRUTH_XOR_EXPR:
12478 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12479 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12480 || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE)
12481 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12482 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12483 || code1 == FIXED_POINT_TYPE || code1 == NULLPTR_TYPE))
12485 /* Result of these operations is always an int,
12486 but that does not mean the operands should be
12487 converted to ints! */
12488 result_type = integer_type_node;
12489 if (op0_int_operands)
12491 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12492 op0 = remove_c_maybe_const_expr (op0);
12495 op0 = c_objc_common_truthvalue_conversion (location, op0);
12496 if (op1_int_operands)
12498 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12499 op1 = remove_c_maybe_const_expr (op1);
12502 op1 = c_objc_common_truthvalue_conversion (location, op1);
12506 if (code == TRUTH_ANDIF_EXPR)
12508 int_const_or_overflow = (int_operands
12509 && TREE_CODE (orig_op0) == INTEGER_CST
12510 && (op0 == truthvalue_false_node
12511 || TREE_CODE (orig_op1) == INTEGER_CST));
12512 int_const = (int_const_or_overflow
12513 && !TREE_OVERFLOW (orig_op0)
12514 && (op0 == truthvalue_false_node
12515 || !TREE_OVERFLOW (orig_op1)));
12517 else if (code == TRUTH_ORIF_EXPR)
12519 int_const_or_overflow = (int_operands
12520 && TREE_CODE (orig_op0) == INTEGER_CST
12521 && (op0 == truthvalue_true_node
12522 || TREE_CODE (orig_op1) == INTEGER_CST));
12523 int_const = (int_const_or_overflow
12524 && !TREE_OVERFLOW (orig_op0)
12525 && (op0 == truthvalue_true_node
12526 || !TREE_OVERFLOW (orig_op1)));
12530 /* Shift operations: result has same type as first operand;
12531 always convert second operand to int.
12532 Also set SHORT_SHIFT if shifting rightward. */
12535 if (gnu_vector_type_p (type0)
12536 && gnu_vector_type_p (type1)
12537 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12538 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12539 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12540 TYPE_VECTOR_SUBPARTS (type1)))
12542 result_type = type0;
12545 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12546 || (gnu_vector_type_p (type0)
12547 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12548 && code1 == INTEGER_TYPE)
12550 doing_shift = true;
12551 if (TREE_CODE (op1) == INTEGER_CST)
12553 if (tree_int_cst_sgn (op1) < 0)
12556 if (c_inhibit_evaluation_warnings == 0)
12557 warning_at (location, OPT_Wshift_count_negative,
12558 "right shift count is negative");
12560 else if (code0 == VECTOR_TYPE)
12562 if (compare_tree_int (op1,
12563 TYPE_PRECISION (TREE_TYPE (type0)))
12567 if (c_inhibit_evaluation_warnings == 0)
12568 warning_at (location, OPT_Wshift_count_overflow,
12569 "right shift count >= width of vector element");
12574 if (!integer_zerop (op1))
12577 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12580 if (c_inhibit_evaluation_warnings == 0)
12581 warning_at (location, OPT_Wshift_count_overflow,
12582 "right shift count >= width of type");
12587 /* Use the type of the value to be shifted. */
12588 result_type = type0;
12589 /* Avoid converting op1 to result_type later. */
12595 if (gnu_vector_type_p (type0)
12596 && gnu_vector_type_p (type1)
12597 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12598 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12599 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12600 TYPE_VECTOR_SUBPARTS (type1)))
12602 result_type = type0;
12605 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12606 || (gnu_vector_type_p (type0)
12607 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12608 && code1 == INTEGER_TYPE)
12610 doing_shift = true;
12611 if (TREE_CODE (op0) == INTEGER_CST
12612 && tree_int_cst_sgn (op0) < 0
12613 && !TYPE_OVERFLOW_WRAPS (type0))
12615 /* Don't reject a left shift of a negative value in a context
12616 where a constant expression is needed in C90. */
12619 if (c_inhibit_evaluation_warnings == 0)
12620 warning_at (location, OPT_Wshift_negative_value,
12621 "left shift of negative value");
12623 if (TREE_CODE (op1) == INTEGER_CST)
12625 if (tree_int_cst_sgn (op1) < 0)
12628 if (c_inhibit_evaluation_warnings == 0)
12629 warning_at (location, OPT_Wshift_count_negative,
12630 "left shift count is negative");
12632 else if (code0 == VECTOR_TYPE)
12634 if (compare_tree_int (op1,
12635 TYPE_PRECISION (TREE_TYPE (type0)))
12639 if (c_inhibit_evaluation_warnings == 0)
12640 warning_at (location, OPT_Wshift_count_overflow,
12641 "left shift count >= width of vector element");
12644 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12647 if (c_inhibit_evaluation_warnings == 0)
12648 warning_at (location, OPT_Wshift_count_overflow,
12649 "left shift count >= width of type");
12651 else if (TREE_CODE (op0) == INTEGER_CST
12652 && maybe_warn_shift_overflow (location, op0, op1)
12657 /* Use the type of the value to be shifted. */
12658 result_type = type0;
12659 /* Avoid converting op1 to result_type later. */
12666 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12669 if (!vector_types_compatible_elements_p (type0, type1))
12671 error_at (location, "comparing vectors with different "
12673 return error_mark_node;
12676 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12677 TYPE_VECTOR_SUBPARTS (type1)))
12679 error_at (location, "comparing vectors with different "
12680 "number of elements");
12681 return error_mark_node;
12684 /* It's not precisely specified how the usual arithmetic
12685 conversions apply to the vector types. Here, we use
12686 the unsigned type if one of the operands is signed and
12687 the other one is unsigned. */
12688 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12690 if (!TYPE_UNSIGNED (type0))
12691 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12693 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12694 warning_at (location, OPT_Wsign_compare, "comparison between "
12695 "types %qT and %qT", type0, type1);
12698 /* Always construct signed integer vector type. */
12699 intt = c_common_type_for_size (GET_MODE_BITSIZE
12701 (TREE_TYPE (type0))), 0);
12704 error_at (location, "could not find an integer type "
12705 "of the same size as %qT",
12706 TREE_TYPE (type0));
12707 return error_mark_node;
12709 result_type = build_opaque_vector_type (intt,
12710 TYPE_VECTOR_SUBPARTS (type0));
12712 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12713 goto return_build_binary_op;
12715 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12716 warning_at (location,
12718 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12719 /* Result of comparison is always int,
12720 but don't convert the args to int! */
12721 build_type = integer_type_node;
12722 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12723 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12724 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12725 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12727 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12729 maybe_warn_for_null_address (location, op0, code);
12730 result_type = type0;
12732 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12734 maybe_warn_for_null_address (location, op1, code);
12735 result_type = type1;
12737 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12739 tree tt0 = TREE_TYPE (type0);
12740 tree tt1 = TREE_TYPE (type1);
12741 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12742 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12743 addr_space_t as_common = ADDR_SPACE_GENERIC;
12745 /* Anything compares with void *. void * compares with anything.
12746 Otherwise, the targets must be compatible
12747 and both must be object or both incomplete. */
12748 if (comp_target_types (location, type0, type1))
12749 result_type = common_pointer_type (type0, type1);
12750 else if (!addr_space_superset (as0, as1, &as_common))
12752 error_at (location, "comparison of pointers to "
12753 "disjoint address spaces");
12754 return error_mark_node;
12756 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12758 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12759 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12760 "comparison of %<void *%> with function pointer");
12762 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12764 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12765 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12766 "comparison of %<void *%> with function pointer");
12769 /* Avoid warning about the volatile ObjC EH puts on decls. */
12771 pedwarn (location, 0,
12772 "comparison of distinct pointer types lacks a cast");
12774 if (result_type == NULL_TREE)
12776 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12777 result_type = build_pointer_type
12778 (build_qualified_type (void_type_node, qual));
12781 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12783 result_type = type0;
12784 pedwarn (location, 0, "comparison between pointer and integer");
12786 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12788 result_type = type1;
12789 pedwarn (location, 0, "comparison between pointer and integer");
12791 /* 6.5.9: One of the following shall hold:
12792 -- both operands have type nullptr_t; */
12793 else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
12795 result_type = nullptr_type_node;
12796 /* No need to convert the operands to result_type later. */
12799 /* -- one operand has type nullptr_t and the other is a null pointer
12800 constant. We will have to convert the former to the type of the
12801 latter, because during gimplification we can't have mismatching
12802 comparison operand type. We convert from nullptr_t to the other
12803 type, since only nullptr_t can be converted to nullptr_t. Also,
12804 even a constant 0 is a null pointer constant, so we may have to
12805 create a pointer type from its type. */
12806 else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
12807 result_type = (INTEGRAL_TYPE_P (type1)
12808 ? build_pointer_type (type1) : type1);
12809 else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
12810 result_type = (INTEGRAL_TYPE_P (type0)
12811 ? build_pointer_type (type0) : type0);
12812 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12813 || truth_value_p (TREE_CODE (orig_op0)))
12814 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12815 || truth_value_p (TREE_CODE (orig_op1))))
12816 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12823 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12826 if (!vector_types_compatible_elements_p (type0, type1))
12828 error_at (location, "comparing vectors with different "
12830 return error_mark_node;
12833 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12834 TYPE_VECTOR_SUBPARTS (type1)))
12836 error_at (location, "comparing vectors with different "
12837 "number of elements");
12838 return error_mark_node;
12841 /* It's not precisely specified how the usual arithmetic
12842 conversions apply to the vector types. Here, we use
12843 the unsigned type if one of the operands is signed and
12844 the other one is unsigned. */
12845 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12847 if (!TYPE_UNSIGNED (type0))
12848 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12850 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12851 warning_at (location, OPT_Wsign_compare, "comparison between "
12852 "types %qT and %qT", type0, type1);
12855 /* Always construct signed integer vector type. */
12856 intt = c_common_type_for_size (GET_MODE_BITSIZE
12858 (TREE_TYPE (type0))), 0);
12861 error_at (location, "could not find an integer type "
12862 "of the same size as %qT",
12863 TREE_TYPE (type0));
12864 return error_mark_node;
12866 result_type = build_opaque_vector_type (intt,
12867 TYPE_VECTOR_SUBPARTS (type0));
12869 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12870 goto return_build_binary_op;
12872 build_type = integer_type_node;
12873 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12874 || code0 == FIXED_POINT_TYPE)
12875 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12876 || code1 == FIXED_POINT_TYPE))
12878 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12880 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12881 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12882 addr_space_t as_common;
12884 if (comp_target_types (location, type0, type1))
12886 result_type = common_pointer_type (type0, type1);
12887 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12888 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12889 pedwarn_c99 (location, OPT_Wpedantic,
12890 "comparison of complete and incomplete pointers");
12891 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12892 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12893 "ordered comparisons of pointers to functions");
12894 else if (null_pointer_constant_p (orig_op0)
12895 || null_pointer_constant_p (orig_op1))
12896 warning_at (location, OPT_Wextra,
12897 "ordered comparison of pointer with null pointer");
12900 else if (!addr_space_superset (as0, as1, &as_common))
12902 error_at (location, "comparison of pointers to "
12903 "disjoint address spaces");
12904 return error_mark_node;
12908 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12909 result_type = build_pointer_type
12910 (build_qualified_type (void_type_node, qual));
12911 pedwarn (location, 0,
12912 "comparison of distinct pointer types lacks a cast");
12915 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12917 result_type = type0;
12919 pedwarn (location, OPT_Wpedantic,
12920 "ordered comparison of pointer with integer zero");
12921 else if (extra_warnings)
12922 warning_at (location, OPT_Wextra,
12923 "ordered comparison of pointer with integer zero");
12925 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12927 result_type = type1;
12929 pedwarn (location, OPT_Wpedantic,
12930 "ordered comparison of pointer with integer zero");
12931 else if (extra_warnings)
12932 warning_at (location, OPT_Wextra,
12933 "ordered comparison of pointer with integer zero");
12935 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12937 result_type = type0;
12938 pedwarn (location, 0, "comparison between pointer and integer");
12940 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12942 result_type = type1;
12943 pedwarn (location, 0, "comparison between pointer and integer");
12946 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12947 && current_function_decl != NULL_TREE
12948 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12950 op0 = save_expr (op0);
12951 op1 = save_expr (op1);
12953 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12954 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12957 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12958 || truth_value_p (TREE_CODE (orig_op0)))
12959 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12960 || truth_value_p (TREE_CODE (orig_op1))))
12961 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12966 /* Used for OpenMP atomics. */
12967 gcc_assert (flag_openmp);
12972 gcc_unreachable ();
12975 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12976 return error_mark_node;
12978 if (gnu_vector_type_p (type0)
12979 && gnu_vector_type_p (type1)
12980 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12981 || !vector_types_compatible_elements_p (type0, type1)))
12983 gcc_rich_location richloc (location);
12984 maybe_range_label_for_tree_type_mismatch
12985 label_for_op0 (orig_op0, orig_op1),
12986 label_for_op1 (orig_op1, orig_op0);
12987 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12988 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12989 binary_op_error (&richloc, code, type0, type1);
12990 return error_mark_node;
12993 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12994 || code0 == FIXED_POINT_TYPE
12995 || gnu_vector_type_p (type0))
12997 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12998 || code1 == FIXED_POINT_TYPE
12999 || gnu_vector_type_p (type1)))
13001 bool first_complex = (code0 == COMPLEX_TYPE);
13002 bool second_complex = (code1 == COMPLEX_TYPE);
13003 int none_complex = (!first_complex && !second_complex);
13005 if (shorten || common || short_compare)
13007 result_type = c_common_type (type0, type1);
13008 do_warn_double_promotion (result_type, type0, type1,
13009 "implicit conversion from %qT to %qT "
13010 "to match other operand of binary "
13013 if (result_type == error_mark_node)
13014 return error_mark_node;
13017 if (first_complex != second_complex
13018 && (code == PLUS_EXPR
13019 || code == MINUS_EXPR
13020 || code == MULT_EXPR
13021 || (code == TRUNC_DIV_EXPR && first_complex))
13022 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
13023 && flag_signed_zeros)
13025 /* An operation on mixed real/complex operands must be
13026 handled specially, but the language-independent code can
13027 more easily optimize the plain complex arithmetic if
13028 -fno-signed-zeros. */
13029 tree real_type = TREE_TYPE (result_type);
13031 if (type0 != orig_type0 || type1 != orig_type1)
13033 gcc_assert (may_need_excess_precision && common);
13034 semantic_result_type = c_common_type (orig_type0, orig_type1);
13038 if (TREE_TYPE (op0) != result_type)
13039 op0 = convert_and_check (location, result_type, op0);
13040 if (TREE_TYPE (op1) != real_type)
13041 op1 = convert_and_check (location, real_type, op1);
13045 if (TREE_TYPE (op0) != real_type)
13046 op0 = convert_and_check (location, real_type, op0);
13047 if (TREE_TYPE (op1) != result_type)
13048 op1 = convert_and_check (location, result_type, op1);
13050 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13051 return error_mark_node;
13054 op0 = save_expr (op0);
13055 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
13057 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
13062 case TRUNC_DIV_EXPR:
13063 op1 = save_expr (op1);
13064 imag = build2 (resultcode, real_type, imag, op1);
13065 /* Fall through. */
13068 real = build2 (resultcode, real_type, real, op1);
13076 op1 = save_expr (op1);
13077 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
13079 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
13084 op0 = save_expr (op0);
13085 imag = build2 (resultcode, real_type, op0, imag);
13086 /* Fall through. */
13088 real = build2 (resultcode, real_type, op0, real);
13091 real = build2 (resultcode, real_type, op0, real);
13092 imag = build1 (NEGATE_EXPR, real_type, imag);
13098 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
13099 goto return_build_binary_op;
13102 /* For certain operations (which identify themselves by shorten != 0)
13103 if both args were extended from the same smaller type,
13104 do the arithmetic in that type and then extend.
13106 shorten !=0 and !=1 indicates a bitwise operation.
13107 For them, this optimization is safe only if
13108 both args are zero-extended or both are sign-extended.
13109 Otherwise, we might change the result.
13110 Eg, (short)-1 | (unsigned short)-1 is (int)-1
13111 but calculated in (unsigned short) it would be (unsigned short)-1. */
13113 if (shorten && none_complex)
13115 final_type = result_type;
13116 result_type = shorten_binary_op (result_type, op0, op1,
13120 /* Shifts can be shortened if shifting right. */
13125 tree arg0 = get_narrower (op0, &unsigned_arg);
13127 final_type = result_type;
13129 if (arg0 == op0 && final_type == TREE_TYPE (op0))
13130 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
13132 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
13133 && tree_int_cst_sgn (op1) > 0
13134 /* We can shorten only if the shift count is less than the
13135 number of bits in the smaller type size. */
13136 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
13137 /* We cannot drop an unsigned shift after sign-extension. */
13138 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
13140 /* Do an unsigned shift if the operand was zero-extended. */
13142 = c_common_signed_or_unsigned_type (unsigned_arg,
13144 /* Convert value-to-be-shifted to that type. */
13145 if (TREE_TYPE (op0) != result_type)
13146 op0 = convert (result_type, op0);
13151 /* Comparison operations are shortened too but differently.
13152 They identify themselves by setting short_compare = 1. */
13156 /* Don't write &op0, etc., because that would prevent op0
13157 from being kept in a register.
13158 Instead, make copies of the our local variables and
13159 pass the copies by reference, then copy them back afterward. */
13160 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
13161 enum tree_code xresultcode = resultcode;
13163 = shorten_compare (location, &xop0, &xop1, &xresult_type,
13166 if (val != NULL_TREE)
13169 goto return_build_binary_op;
13172 op0 = xop0, op1 = xop1;
13174 resultcode = xresultcode;
13176 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
13178 bool op0_maybe_const = true;
13179 bool op1_maybe_const = true;
13180 tree orig_op0_folded, orig_op1_folded;
13182 if (in_late_binary_op)
13184 orig_op0_folded = orig_op0;
13185 orig_op1_folded = orig_op1;
13189 /* Fold for the sake of possible warnings, as in
13190 build_conditional_expr. This requires the
13191 "original" values to be folded, not just op0 and
13193 c_inhibit_evaluation_warnings++;
13194 op0 = c_fully_fold (op0, require_constant_value,
13196 op1 = c_fully_fold (op1, require_constant_value,
13198 c_inhibit_evaluation_warnings--;
13199 orig_op0_folded = c_fully_fold (orig_op0,
13200 require_constant_value,
13202 orig_op1_folded = c_fully_fold (orig_op1,
13203 require_constant_value,
13207 if (warn_sign_compare)
13208 warn_for_sign_compare (location, orig_op0_folded,
13209 orig_op1_folded, op0, op1,
13210 result_type, resultcode);
13211 if (!in_late_binary_op && !int_operands)
13213 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
13214 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
13215 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
13216 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
13222 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
13223 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
13224 Then the expression will be built.
13225 It will be given type FINAL_TYPE if that is nonzero;
13226 otherwise, it will be given type RESULT_TYPE. */
13230 /* Favor showing any expression locations that are available. */
13231 op_location_t oploc (location, UNKNOWN_LOCATION);
13232 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
13233 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
13234 return error_mark_node;
13237 if (build_type == NULL_TREE)
13239 build_type = result_type;
13240 if ((type0 != orig_type0 || type1 != orig_type1)
13243 gcc_assert (may_need_excess_precision && common);
13244 semantic_result_type = c_common_type (orig_type0, orig_type1);
13250 op0 = ep_convert_and_check (location, result_type, op0,
13251 semantic_result_type);
13252 op1 = ep_convert_and_check (location, result_type, op1,
13253 semantic_result_type);
13255 /* This can happen if one operand has a vector type, and the other
13256 has a different type. */
13257 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13258 return error_mark_node;
13261 if (sanitize_flags_p ((SANITIZE_SHIFT
13263 | SANITIZE_FLOAT_DIVIDE
13264 | SANITIZE_SI_OVERFLOW))
13265 && current_function_decl != NULL_TREE
13266 && (doing_div_or_mod || doing_shift)
13267 && !require_constant_value)
13269 /* OP0 and/or OP1 might have side-effects. */
13270 op0 = save_expr (op0);
13271 op1 = save_expr (op1);
13272 op0 = c_fully_fold (op0, false, NULL);
13273 op1 = c_fully_fold (op1, false, NULL);
13274 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
13275 | SANITIZE_FLOAT_DIVIDE
13276 | SANITIZE_SI_OVERFLOW))))
13277 instrument_expr = ubsan_instrument_division (location, op0, op1);
13278 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
13279 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
13282 /* Treat expressions in initializers specially as they can't trap. */
13283 if (int_const_or_overflow)
13284 ret = (require_constant_value
13285 ? fold_build2_initializer_loc (location, resultcode, build_type,
13287 : fold_build2_loc (location, resultcode, build_type, op0, op1));
13289 ret = build2 (resultcode, build_type, op0, op1);
13290 if (final_type != NULL_TREE)
13291 ret = convert (final_type, ret);
13293 return_build_binary_op:
13294 gcc_assert (ret != error_mark_node);
13295 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
13296 ret = (int_operands
13297 ? note_integer_operands (ret)
13298 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
13299 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
13300 && !in_late_binary_op)
13301 ret = note_integer_operands (ret);
13302 protected_set_expr_location (ret, location);
13304 if (instrument_expr != NULL)
13305 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
13306 instrument_expr, ret);
13308 if (semantic_result_type)
13309 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
13310 semantic_result_type, ret);
13316 /* Convert EXPR to be a truth-value, validating its type for this
13317 purpose. LOCATION is the source location for the expression. */
13320 c_objc_common_truthvalue_conversion (location_t location, tree expr)
13322 bool int_const, int_operands;
13324 switch (TREE_CODE (TREE_TYPE (expr)))
13327 error_at (location, "used array that cannot be converted to pointer where scalar is required");
13328 return error_mark_node;
13331 error_at (location, "used struct type value where scalar is required");
13332 return error_mark_node;
13335 error_at (location, "used union type value where scalar is required");
13336 return error_mark_node;
13339 error_at (location, "void value not ignored as it ought to be");
13340 return error_mark_node;
13343 if (reject_gcc_builtin (expr))
13344 return error_mark_node;
13347 case FUNCTION_TYPE:
13348 gcc_unreachable ();
13351 error_at (location, "used vector type where scalar is required");
13352 return error_mark_node;
13358 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
13359 int_operands = EXPR_INT_CONST_OPERANDS (expr);
13360 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
13362 expr = remove_c_maybe_const_expr (expr);
13363 expr = build2 (NE_EXPR, integer_type_node, expr,
13364 convert (TREE_TYPE (expr), integer_zero_node));
13365 expr = note_integer_operands (expr);
13368 /* ??? Should we also give an error for vectors rather than leaving
13369 those to give errors later? */
13370 expr = c_common_truthvalue_conversion (location, expr);
13372 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
13374 if (TREE_OVERFLOW (expr))
13377 return note_integer_operands (expr);
13379 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
13380 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13385 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13389 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13391 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13393 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13394 /* Executing a compound literal inside a function reinitializes
13396 if (!TREE_STATIC (decl))
13404 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13405 statement. LOC is the location of the construct. */
13408 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13411 body = c_end_compound_stmt (loc, body, true);
13413 tree stmt = make_node (code);
13414 TREE_TYPE (stmt) = void_type_node;
13415 OMP_BODY (stmt) = body;
13416 OMP_CLAUSES (stmt) = clauses;
13417 SET_EXPR_LOCATION (stmt, loc);
13419 return add_stmt (stmt);
13422 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13423 statement. LOC is the location of the OACC_DATA. */
13426 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13430 block = c_end_compound_stmt (loc, block, true);
13432 stmt = make_node (OACC_DATA);
13433 TREE_TYPE (stmt) = void_type_node;
13434 OACC_DATA_CLAUSES (stmt) = clauses;
13435 OACC_DATA_BODY (stmt) = block;
13436 SET_EXPR_LOCATION (stmt, loc);
13438 return add_stmt (stmt);
13441 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13442 statement. LOC is the location of the OACC_HOST_DATA. */
13445 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13449 block = c_end_compound_stmt (loc, block, true);
13451 stmt = make_node (OACC_HOST_DATA);
13452 TREE_TYPE (stmt) = void_type_node;
13453 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13454 OACC_HOST_DATA_BODY (stmt) = block;
13455 SET_EXPR_LOCATION (stmt, loc);
13457 return add_stmt (stmt);
13460 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13463 c_begin_omp_parallel (void)
13467 keep_next_level ();
13468 block = c_begin_compound_stmt (true);
13473 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13474 statement. LOC is the location of the OMP_PARALLEL. */
13477 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13481 block = c_end_compound_stmt (loc, block, true);
13483 stmt = make_node (OMP_PARALLEL);
13484 TREE_TYPE (stmt) = void_type_node;
13485 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13486 OMP_PARALLEL_BODY (stmt) = block;
13487 SET_EXPR_LOCATION (stmt, loc);
13489 return add_stmt (stmt);
13492 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13495 c_begin_omp_task (void)
13499 keep_next_level ();
13500 block = c_begin_compound_stmt (true);
13505 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13506 statement. LOC is the location of the #pragma. */
13509 c_finish_omp_task (location_t loc, tree clauses, tree block)
13513 block = c_end_compound_stmt (loc, block, true);
13515 stmt = make_node (OMP_TASK);
13516 TREE_TYPE (stmt) = void_type_node;
13517 OMP_TASK_CLAUSES (stmt) = clauses;
13518 OMP_TASK_BODY (stmt) = block;
13519 SET_EXPR_LOCATION (stmt, loc);
13521 return add_stmt (stmt);
13524 /* Generate GOMP_cancel call for #pragma omp cancel. */
13527 c_finish_omp_cancel (location_t loc, tree clauses)
13529 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13531 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13533 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13535 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13537 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13541 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13542 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13546 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13547 if (ifc != NULL_TREE)
13549 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13550 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13551 error_at (OMP_CLAUSE_LOCATION (ifc),
13552 "expected %<cancel%> %<if%> clause modifier");
13555 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13556 if (ifc2 != NULL_TREE)
13558 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13559 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13560 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13561 error_at (OMP_CLAUSE_LOCATION (ifc2),
13562 "expected %<cancel%> %<if%> clause modifier");
13566 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13567 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13568 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13569 build_zero_cst (type));
13572 ifc = boolean_true_node;
13573 tree stmt = build_call_expr_loc (loc, fn, 2,
13574 build_int_cst (integer_type_node, mask),
13579 /* Generate GOMP_cancellation_point call for
13580 #pragma omp cancellation point. */
13583 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13585 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13587 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13589 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13591 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13593 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13597 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13598 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13602 tree stmt = build_call_expr_loc (loc, fn, 1,
13603 build_int_cst (integer_type_node, mask));
13607 /* Helper function for handle_omp_array_sections. Called recursively
13608 to handle multiple array-section-subscripts. C is the clause,
13609 T current expression (initially OMP_CLAUSE_DECL), which is either
13610 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13611 expression if specified, TREE_VALUE length expression if specified,
13612 TREE_CHAIN is what it has been specified after, or some decl.
13613 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13614 set to true if any of the array-section-subscript could have length
13615 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13616 first array-section-subscript which is known not to have length
13618 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13619 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13620 all are or may have length of 1, array-section-subscript [:2] is the
13621 first one known not to have length 1. For array-section-subscript
13622 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13623 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13624 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13625 case though, as some lengths could be zero. */
13628 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13629 bool &maybe_zero_len, unsigned int &first_non_one,
13630 enum c_omp_region_type ort)
13632 tree ret, low_bound, length, type;
13633 if (TREE_CODE (t) != TREE_LIST)
13635 if (error_operand_p (t))
13636 return error_mark_node;
13638 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13639 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13640 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13642 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13643 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13644 return error_mark_node;
13646 while (TREE_CODE (t) == INDIRECT_REF)
13648 t = TREE_OPERAND (t, 0);
13650 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13651 t = TREE_OPERAND (t, 0);
13653 while (TREE_CODE (t) == COMPOUND_EXPR)
13655 t = TREE_OPERAND (t, 1);
13658 if (TREE_CODE (t) == COMPONENT_REF
13659 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13660 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13661 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13663 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13665 error_at (OMP_CLAUSE_LOCATION (c),
13666 "bit-field %qE in %qs clause",
13667 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13668 return error_mark_node;
13670 while (TREE_CODE (t) == COMPONENT_REF)
13672 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13674 error_at (OMP_CLAUSE_LOCATION (c),
13675 "%qE is a member of a union", t);
13676 return error_mark_node;
13678 t = TREE_OPERAND (t, 0);
13679 while (TREE_CODE (t) == MEM_REF
13680 || TREE_CODE (t) == INDIRECT_REF
13681 || TREE_CODE (t) == ARRAY_REF)
13683 t = TREE_OPERAND (t, 0);
13685 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13686 t = TREE_OPERAND (t, 0);
13688 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13690 if (maybe_ne (mem_ref_offset (t), 0))
13691 error_at (OMP_CLAUSE_LOCATION (c),
13692 "cannot dereference %qE in %qs clause", t,
13693 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13695 t = TREE_OPERAND (t, 0);
13699 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13702 error_at (OMP_CLAUSE_LOCATION (c),
13703 "%qD is not a variable in %qs clause", t,
13704 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13706 error_at (OMP_CLAUSE_LOCATION (c),
13707 "%qE is not a variable in %qs clause", t,
13708 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13709 return error_mark_node;
13711 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13712 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13713 && TYPE_ATOMIC (TREE_TYPE (t)))
13715 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13716 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13717 return error_mark_node;
13719 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13720 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13722 && DECL_THREAD_LOCAL_P (t))
13724 error_at (OMP_CLAUSE_LOCATION (c),
13725 "%qD is threadprivate variable in %qs clause", t,
13726 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13727 return error_mark_node;
13729 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13730 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13731 && TYPE_ATOMIC (TREE_TYPE (t))
13732 && POINTER_TYPE_P (TREE_TYPE (t)))
13734 /* If the array section is pointer based and the pointer
13735 itself is _Atomic qualified, we need to atomically load
13738 memset (&expr, 0, sizeof (expr));
13740 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13741 expr, false, false);
13747 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13748 maybe_zero_len, first_non_one, ort);
13749 if (ret == error_mark_node || ret == NULL_TREE)
13752 type = TREE_TYPE (ret);
13753 low_bound = TREE_PURPOSE (t);
13754 length = TREE_VALUE (t);
13756 if (low_bound == error_mark_node || length == error_mark_node)
13757 return error_mark_node;
13759 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13761 error_at (OMP_CLAUSE_LOCATION (c),
13762 "low bound %qE of array section does not have integral type",
13764 return error_mark_node;
13766 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13768 error_at (OMP_CLAUSE_LOCATION (c),
13769 "length %qE of array section does not have integral type",
13771 return error_mark_node;
13774 && TREE_CODE (low_bound) == INTEGER_CST
13775 && TYPE_PRECISION (TREE_TYPE (low_bound))
13776 > TYPE_PRECISION (sizetype))
13777 low_bound = fold_convert (sizetype, low_bound);
13779 && TREE_CODE (length) == INTEGER_CST
13780 && TYPE_PRECISION (TREE_TYPE (length))
13781 > TYPE_PRECISION (sizetype))
13782 length = fold_convert (sizetype, length);
13783 if (low_bound == NULL_TREE)
13784 low_bound = integer_zero_node;
13785 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13786 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13787 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13789 if (length != integer_one_node)
13791 error_at (OMP_CLAUSE_LOCATION (c),
13792 "expected single pointer in %qs clause",
13793 user_omp_clause_code_name (c, ort == C_ORT_ACC));
13794 return error_mark_node;
13797 if (length != NULL_TREE)
13799 if (!integer_nonzerop (length))
13801 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13802 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13803 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13804 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13805 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13807 if (integer_zerop (length))
13809 error_at (OMP_CLAUSE_LOCATION (c),
13810 "zero length array section in %qs clause",
13811 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13812 return error_mark_node;
13816 maybe_zero_len = true;
13818 if (first_non_one == types.length ()
13819 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13822 if (TREE_CODE (type) == ARRAY_TYPE)
13824 if (length == NULL_TREE
13825 && (TYPE_DOMAIN (type) == NULL_TREE
13826 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13828 error_at (OMP_CLAUSE_LOCATION (c),
13829 "for unknown bound array type length expression must "
13831 return error_mark_node;
13833 if (TREE_CODE (low_bound) == INTEGER_CST
13834 && tree_int_cst_sgn (low_bound) == -1)
13836 error_at (OMP_CLAUSE_LOCATION (c),
13837 "negative low bound in array section in %qs clause",
13838 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13839 return error_mark_node;
13841 if (length != NULL_TREE
13842 && TREE_CODE (length) == INTEGER_CST
13843 && tree_int_cst_sgn (length) == -1)
13845 error_at (OMP_CLAUSE_LOCATION (c),
13846 "negative length in array section in %qs clause",
13847 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13848 return error_mark_node;
13850 if (TYPE_DOMAIN (type)
13851 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13852 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13856 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13857 size = size_binop (PLUS_EXPR, size, size_one_node);
13858 if (TREE_CODE (low_bound) == INTEGER_CST)
13860 if (tree_int_cst_lt (size, low_bound))
13862 error_at (OMP_CLAUSE_LOCATION (c),
13863 "low bound %qE above array section size "
13864 "in %qs clause", low_bound,
13865 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13866 return error_mark_node;
13868 if (tree_int_cst_equal (size, low_bound))
13870 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13871 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13872 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13873 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13874 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13876 error_at (OMP_CLAUSE_LOCATION (c),
13877 "zero length array section in %qs clause",
13878 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13879 return error_mark_node;
13881 maybe_zero_len = true;
13883 else if (length == NULL_TREE
13884 && first_non_one == types.length ()
13885 && tree_int_cst_equal
13886 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13890 else if (length == NULL_TREE)
13892 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13893 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13894 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13895 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13896 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13897 maybe_zero_len = true;
13898 if (first_non_one == types.length ())
13901 if (length && TREE_CODE (length) == INTEGER_CST)
13903 if (tree_int_cst_lt (size, length))
13905 error_at (OMP_CLAUSE_LOCATION (c),
13906 "length %qE above array section size "
13907 "in %qs clause", length,
13908 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13909 return error_mark_node;
13911 if (TREE_CODE (low_bound) == INTEGER_CST)
13914 = size_binop (PLUS_EXPR,
13915 fold_convert (sizetype, low_bound),
13916 fold_convert (sizetype, length));
13917 if (TREE_CODE (lbpluslen) == INTEGER_CST
13918 && tree_int_cst_lt (size, lbpluslen))
13920 error_at (OMP_CLAUSE_LOCATION (c),
13921 "high bound %qE above array section size "
13922 "in %qs clause", lbpluslen,
13923 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13924 return error_mark_node;
13929 else if (length == NULL_TREE)
13931 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13932 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13933 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13934 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13935 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13936 maybe_zero_len = true;
13937 if (first_non_one == types.length ())
13941 /* For [lb:] we will need to evaluate lb more than once. */
13942 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13944 tree lb = save_expr (low_bound);
13945 if (lb != low_bound)
13947 TREE_PURPOSE (t) = lb;
13952 else if (TREE_CODE (type) == POINTER_TYPE)
13954 if (length == NULL_TREE)
13956 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13957 error_at (OMP_CLAUSE_LOCATION (c),
13958 "for array function parameter length expression "
13959 "must be specified");
13961 error_at (OMP_CLAUSE_LOCATION (c),
13962 "for pointer type length expression must be specified");
13963 return error_mark_node;
13965 if (length != NULL_TREE
13966 && TREE_CODE (length) == INTEGER_CST
13967 && tree_int_cst_sgn (length) == -1)
13969 error_at (OMP_CLAUSE_LOCATION (c),
13970 "negative length in array section in %qs clause",
13971 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13972 return error_mark_node;
13974 /* If there is a pointer type anywhere but in the very first
13975 array-section-subscript, the array section could be non-contiguous. */
13976 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13977 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13978 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13980 /* If any prior dimension has a non-one length, then deem this
13981 array section as non-contiguous. */
13982 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13983 d = TREE_CHAIN (d))
13985 tree d_length = TREE_VALUE (d);
13986 if (d_length == NULL_TREE || !integer_onep (d_length))
13988 error_at (OMP_CLAUSE_LOCATION (c),
13989 "array section is not contiguous in %qs clause",
13990 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13991 return error_mark_node;
13998 error_at (OMP_CLAUSE_LOCATION (c),
13999 "%qE does not have pointer or array type", ret);
14000 return error_mark_node;
14002 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
14003 types.safe_push (TREE_TYPE (ret));
14004 /* We will need to evaluate lb more than once. */
14005 tree lb = save_expr (low_bound);
14006 if (lb != low_bound)
14008 TREE_PURPOSE (t) = lb;
14011 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
14015 /* Handle array sections for clause C. */
14018 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
14020 bool maybe_zero_len = false;
14021 unsigned int first_non_one = 0;
14022 auto_vec<tree, 10> types;
14023 tree *tp = &OMP_CLAUSE_DECL (c);
14024 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14025 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14026 && TREE_CODE (*tp) == TREE_LIST
14027 && TREE_PURPOSE (*tp)
14028 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
14029 tp = &TREE_VALUE (*tp);
14030 tree first = handle_omp_array_sections_1 (c, *tp, types,
14031 maybe_zero_len, first_non_one,
14033 if (first == error_mark_node)
14035 if (first == NULL_TREE)
14037 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14038 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14041 tree tem = NULL_TREE;
14042 /* Need to evaluate side effects in the length expressions
14044 while (TREE_CODE (t) == TREE_LIST)
14046 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
14048 if (tem == NULL_TREE)
14049 tem = TREE_VALUE (t);
14051 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
14052 TREE_VALUE (t), tem);
14054 t = TREE_CHAIN (t);
14057 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
14058 first = c_fully_fold (first, false, NULL, true);
14063 unsigned int num = types.length (), i;
14064 tree t, side_effects = NULL_TREE, size = NULL_TREE;
14065 tree condition = NULL_TREE;
14067 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
14068 maybe_zero_len = true;
14070 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
14071 t = TREE_CHAIN (t))
14073 tree low_bound = TREE_PURPOSE (t);
14074 tree length = TREE_VALUE (t);
14078 && TREE_CODE (low_bound) == INTEGER_CST
14079 && TYPE_PRECISION (TREE_TYPE (low_bound))
14080 > TYPE_PRECISION (sizetype))
14081 low_bound = fold_convert (sizetype, low_bound);
14083 && TREE_CODE (length) == INTEGER_CST
14084 && TYPE_PRECISION (TREE_TYPE (length))
14085 > TYPE_PRECISION (sizetype))
14086 length = fold_convert (sizetype, length);
14087 if (low_bound == NULL_TREE)
14088 low_bound = integer_zero_node;
14089 if (!maybe_zero_len && i > first_non_one)
14091 if (integer_nonzerop (low_bound))
14092 goto do_warn_noncontiguous;
14093 if (length != NULL_TREE
14094 && TREE_CODE (length) == INTEGER_CST
14095 && TYPE_DOMAIN (types[i])
14096 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
14097 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
14101 size = size_binop (PLUS_EXPR,
14102 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14104 if (!tree_int_cst_equal (length, size))
14106 do_warn_noncontiguous:
14107 error_at (OMP_CLAUSE_LOCATION (c),
14108 "array section is not contiguous in %qs "
14110 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14114 if (length != NULL_TREE
14115 && TREE_SIDE_EFFECTS (length))
14117 if (side_effects == NULL_TREE)
14118 side_effects = length;
14120 side_effects = build2 (COMPOUND_EXPR,
14121 TREE_TYPE (side_effects),
14122 length, side_effects);
14129 if (i > first_non_one
14130 && ((length && integer_nonzerop (length))
14131 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14132 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14133 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
14136 l = fold_convert (sizetype, length);
14139 l = size_binop (PLUS_EXPR,
14140 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14142 l = size_binop (MINUS_EXPR, l,
14143 fold_convert (sizetype, low_bound));
14145 if (i > first_non_one)
14147 l = fold_build2 (NE_EXPR, boolean_type_node, l,
14149 if (condition == NULL_TREE)
14152 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
14155 else if (size == NULL_TREE)
14157 size = size_in_bytes (TREE_TYPE (types[i]));
14158 tree eltype = TREE_TYPE (types[num - 1]);
14159 while (TREE_CODE (eltype) == ARRAY_TYPE)
14160 eltype = TREE_TYPE (eltype);
14161 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14162 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14163 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14165 if (integer_zerop (size)
14166 || integer_zerop (size_in_bytes (eltype)))
14168 error_at (OMP_CLAUSE_LOCATION (c),
14169 "zero length array section in %qs clause",
14170 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14171 return error_mark_node;
14173 size = size_binop (EXACT_DIV_EXPR, size,
14174 size_in_bytes (eltype));
14176 size = size_binop (MULT_EXPR, size, l);
14178 size = fold_build3 (COND_EXPR, sizetype, condition,
14179 size, size_zero_node);
14182 size = size_binop (MULT_EXPR, size, l);
14186 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
14187 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14188 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14189 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14191 size = size_binop (MINUS_EXPR, size, size_one_node);
14192 size = c_fully_fold (size, false, NULL);
14193 size = save_expr (size);
14194 tree index_type = build_index_type (size);
14195 tree eltype = TREE_TYPE (first);
14196 while (TREE_CODE (eltype) == ARRAY_TYPE)
14197 eltype = TREE_TYPE (eltype);
14198 tree type = build_array_type (eltype, index_type);
14199 tree ptype = build_pointer_type (eltype);
14200 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14201 t = build_fold_addr_expr (t);
14202 tree t2 = build_fold_addr_expr (first);
14203 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14204 ptrdiff_type_node, t2);
14205 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14206 ptrdiff_type_node, t2,
14207 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14208 ptrdiff_type_node, t));
14209 t2 = c_fully_fold (t2, false, NULL);
14210 if (tree_fits_shwi_p (t2))
14211 t = build2 (MEM_REF, type, t,
14212 build_int_cst (ptype, tree_to_shwi (t2)));
14215 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
14216 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
14217 TREE_TYPE (t), t, t2);
14218 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
14220 OMP_CLAUSE_DECL (c) = t;
14223 first = c_fully_fold (first, false, NULL);
14224 OMP_CLAUSE_DECL (c) = first;
14225 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
14228 size = c_fully_fold (size, false, NULL);
14229 OMP_CLAUSE_SIZE (c) = size;
14230 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14231 || (TREE_CODE (t) == COMPONENT_REF
14232 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
14234 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
14235 switch (OMP_CLAUSE_MAP_KIND (c))
14237 case GOMP_MAP_ALLOC:
14238 case GOMP_MAP_IF_PRESENT:
14240 case GOMP_MAP_FROM:
14241 case GOMP_MAP_TOFROM:
14242 case GOMP_MAP_ALWAYS_TO:
14243 case GOMP_MAP_ALWAYS_FROM:
14244 case GOMP_MAP_ALWAYS_TOFROM:
14245 case GOMP_MAP_RELEASE:
14246 case GOMP_MAP_DELETE:
14247 case GOMP_MAP_FORCE_TO:
14248 case GOMP_MAP_FORCE_FROM:
14249 case GOMP_MAP_FORCE_TOFROM:
14250 case GOMP_MAP_FORCE_PRESENT:
14251 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
14256 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
14257 if (TREE_CODE (t) == COMPONENT_REF)
14258 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
14260 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
14261 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
14262 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
14263 && !c_mark_addressable (t))
14265 OMP_CLAUSE_DECL (c2) = t;
14266 t = build_fold_addr_expr (first);
14267 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
14268 tree ptr = OMP_CLAUSE_DECL (c2);
14269 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
14270 ptr = build_fold_addr_expr (ptr);
14271 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14272 ptrdiff_type_node, t,
14273 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14274 ptrdiff_type_node, ptr));
14275 t = c_fully_fold (t, false, NULL);
14276 OMP_CLAUSE_SIZE (c2) = t;
14277 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
14278 OMP_CLAUSE_CHAIN (c) = c2;
14283 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
14284 an inline call. But, remap
14285 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
14286 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
14289 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
14290 tree decl, tree placeholder)
14293 hash_map<tree, tree> decl_map;
14295 decl_map.put (omp_decl1, placeholder);
14296 decl_map.put (omp_decl2, decl);
14297 memset (&id, 0, sizeof (id));
14298 id.src_fn = DECL_CONTEXT (omp_decl1);
14299 id.dst_fn = current_function_decl;
14300 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
14301 id.decl_map = &decl_map;
14303 id.copy_decl = copy_decl_no_change;
14304 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
14305 id.transform_new_cfg = true;
14306 id.transform_return_to_modify = false;
14308 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
14312 /* Helper function of c_finish_omp_clauses, called via walk_tree.
14313 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
14316 c_find_omp_placeholder_r (tree *tp, int *, void *data)
14318 if (*tp == (tree) data)
14323 /* Similarly, but also walk aggregate fields. */
14325 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
14328 c_find_omp_var_r (tree *tp, int *, void *data)
14330 if (*tp == ((struct c_find_omp_var_s *) data)->var)
14332 if (RECORD_OR_UNION_TYPE_P (*tp))
14335 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
14337 for (field = TYPE_FIELDS (*tp); field;
14338 field = DECL_CHAIN (field))
14339 if (TREE_CODE (field) == FIELD_DECL)
14341 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
14342 c_find_omp_var_r, data, pset);
14345 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
14348 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
14352 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
14357 else if (INTEGRAL_TYPE_P (*tp))
14358 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
14359 ((struct c_find_omp_var_s *) data)->pset);
14363 /* Finish OpenMP iterators ITER. Return true if they are errorneous
14364 and clauses containing them should be removed. */
14367 c_omp_finish_iterators (tree iter)
14370 for (tree it = iter; it; it = TREE_CHAIN (it))
14372 tree var = TREE_VEC_ELT (it, 0);
14373 tree begin = TREE_VEC_ELT (it, 1);
14374 tree end = TREE_VEC_ELT (it, 2);
14375 tree step = TREE_VEC_ELT (it, 3);
14377 tree type = TREE_TYPE (var);
14378 location_t loc = DECL_SOURCE_LOCATION (var);
14379 if (type == error_mark_node)
14384 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14386 error_at (loc, "iterator %qD has neither integral nor pointer type",
14391 else if (TYPE_ATOMIC (type))
14393 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14397 else if (TYPE_READONLY (type))
14399 error_at (loc, "iterator %qD has const qualified type", var);
14403 else if (step == error_mark_node
14404 || TREE_TYPE (step) == error_mark_node)
14409 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14411 error_at (EXPR_LOC_OR_LOC (step, loc),
14412 "iterator step with non-integral type");
14416 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14417 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14418 orig_step = save_expr (c_fully_fold (step, false, NULL));
14419 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14420 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14421 if (POINTER_TYPE_P (type))
14423 begin = save_expr (begin);
14424 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14425 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14426 fold_convert (sizetype, step),
14427 fold_convert (sizetype, begin));
14428 step = fold_convert (ssizetype, step);
14430 if (integer_zerop (step))
14432 error_at (loc, "iterator %qD has zero step", var);
14437 if (begin == error_mark_node
14438 || end == error_mark_node
14439 || step == error_mark_node
14440 || orig_step == error_mark_node)
14445 hash_set<tree> pset;
14447 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14449 tree var2 = TREE_VEC_ELT (it2, 0);
14450 tree begin2 = TREE_VEC_ELT (it2, 1);
14451 tree end2 = TREE_VEC_ELT (it2, 2);
14452 tree step2 = TREE_VEC_ELT (it2, 3);
14453 tree type2 = TREE_TYPE (var2);
14454 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14455 struct c_find_omp_var_s data = { var, &pset };
14456 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14459 "type of iterator %qD refers to outer iterator %qD",
14463 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14465 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14466 "begin expression refers to outer iterator %qD", var);
14469 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14471 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14472 "end expression refers to outer iterator %qD", var);
14475 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14477 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14478 "step expression refers to outer iterator %qD", var);
14487 TREE_VEC_ELT (it, 1) = begin;
14488 TREE_VEC_ELT (it, 2) = end;
14489 TREE_VEC_ELT (it, 3) = step;
14490 TREE_VEC_ELT (it, 4) = orig_step;
14495 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14496 Return true if an error has been detected. */
14499 c_oacc_check_attachments (tree c)
14501 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14504 /* OpenACC attach / detach clauses must be pointers. */
14505 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14506 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14508 tree t = OMP_CLAUSE_DECL (c);
14510 while (TREE_CODE (t) == TREE_LIST)
14511 t = TREE_CHAIN (t);
14513 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14515 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14516 user_omp_clause_code_name (c, true));
14524 /* For all elements of CLAUSES, validate them against their constraints.
14525 Remove any elements from the list that are invalid. */
14528 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14530 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14531 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14532 bitmap_head oacc_reduction_head, is_on_device_head;
14533 tree c, t, type, *pc;
14534 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14535 bool branch_seen = false;
14536 bool copyprivate_seen = false;
14537 bool mergeable_seen = false;
14538 tree *detach_seen = NULL;
14539 bool linear_variable_step_check = false;
14540 tree *nowait_clause = NULL;
14541 tree ordered_clause = NULL_TREE;
14542 tree schedule_clause = NULL_TREE;
14543 bool oacc_async = false;
14544 bool indir_component_ref_p = false;
14545 tree last_iterators = NULL_TREE;
14546 bool last_iterators_remove = false;
14547 tree *nogroup_seen = NULL;
14548 tree *order_clause = NULL;
14549 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14550 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14551 int reduction_seen = 0;
14552 bool allocate_seen = false;
14553 bool implicit_moved = false;
14554 bool target_in_reduction_seen = false;
14556 bitmap_obstack_initialize (NULL);
14557 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14558 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14559 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14560 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14561 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14562 bitmap_initialize (&map_head, &bitmap_default_obstack);
14563 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14564 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14565 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14566 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14567 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14568 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14570 if (ort & C_ORT_ACC)
14571 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14572 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14578 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
14580 for (pc = &clauses, c = clauses; c ; c = *pc)
14582 bool remove = false;
14583 bool need_complete = false;
14584 bool need_implicitly_determined = false;
14586 /* We've reached the end of a list of expanded nodes. Reset the group
14588 if (c == grp_sentinel)
14589 grp_start_p = NULL;
14591 switch (OMP_CLAUSE_CODE (c))
14593 case OMP_CLAUSE_SHARED:
14594 need_implicitly_determined = true;
14595 goto check_dup_generic;
14597 case OMP_CLAUSE_PRIVATE:
14598 need_complete = true;
14599 need_implicitly_determined = true;
14600 goto check_dup_generic;
14602 case OMP_CLAUSE_REDUCTION:
14603 if (reduction_seen == 0)
14604 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14605 else if (reduction_seen != -2
14606 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14609 error_at (OMP_CLAUSE_LOCATION (c),
14610 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14611 "on the same construct");
14612 reduction_seen = -2;
14615 case OMP_CLAUSE_IN_REDUCTION:
14616 case OMP_CLAUSE_TASK_REDUCTION:
14617 need_implicitly_determined = true;
14618 t = OMP_CLAUSE_DECL (c);
14619 if (TREE_CODE (t) == TREE_LIST)
14621 if (handle_omp_array_sections (c, ort))
14627 t = OMP_CLAUSE_DECL (c);
14628 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14629 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14631 error_at (OMP_CLAUSE_LOCATION (c),
14632 "%<inscan%> %<reduction%> clause with array "
14638 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14639 if (t == error_mark_node)
14645 c_mark_addressable (t);
14646 type = TREE_TYPE (t);
14647 if (TREE_CODE (t) == MEM_REF)
14648 type = TREE_TYPE (type);
14649 if (TREE_CODE (type) == ARRAY_TYPE)
14651 tree oatype = type;
14652 gcc_assert (TREE_CODE (t) != MEM_REF);
14653 while (TREE_CODE (type) == ARRAY_TYPE)
14654 type = TREE_TYPE (type);
14655 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14657 error_at (OMP_CLAUSE_LOCATION (c),
14658 "%qD in %<reduction%> clause is a zero size array",
14663 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14664 TYPE_SIZE_UNIT (type));
14665 if (integer_zerop (size))
14667 error_at (OMP_CLAUSE_LOCATION (c),
14668 "%qD in %<reduction%> clause is a zero size array",
14673 size = size_binop (MINUS_EXPR, size, size_one_node);
14674 size = save_expr (size);
14675 tree index_type = build_index_type (size);
14676 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14678 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14679 tree ptype = build_pointer_type (type);
14680 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14681 t = build_fold_addr_expr (t);
14682 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14683 OMP_CLAUSE_DECL (c) = t;
14685 if (TYPE_ATOMIC (type))
14687 error_at (OMP_CLAUSE_LOCATION (c),
14688 "%<_Atomic%> %qE in %<reduction%> clause", t);
14692 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14693 || OMP_CLAUSE_REDUCTION_TASK (c))
14695 /* Disallow zero sized or potentially zero sized task
14697 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14699 error_at (OMP_CLAUSE_LOCATION (c),
14700 "zero sized type %qT in %qs clause", type,
14701 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14705 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14707 error_at (OMP_CLAUSE_LOCATION (c),
14708 "variable sized type %qT in %qs clause", type,
14709 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14714 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14715 && (FLOAT_TYPE_P (type)
14716 || TREE_CODE (type) == COMPLEX_TYPE))
14718 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14719 const char *r_name = NULL;
14726 case TRUTH_ANDIF_EXPR:
14727 case TRUTH_ORIF_EXPR:
14730 if (TREE_CODE (type) == COMPLEX_TYPE)
14734 if (TREE_CODE (type) == COMPLEX_TYPE)
14747 gcc_unreachable ();
14751 error_at (OMP_CLAUSE_LOCATION (c),
14752 "%qE has invalid type for %<reduction(%s)%>",
14758 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14760 error_at (OMP_CLAUSE_LOCATION (c),
14761 "user defined reduction not found for %qE", t);
14765 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14767 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14768 type = TYPE_MAIN_VARIANT (type);
14769 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14770 VAR_DECL, NULL_TREE, type);
14771 tree decl_placeholder = NULL_TREE;
14772 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14773 DECL_ARTIFICIAL (placeholder) = 1;
14774 DECL_IGNORED_P (placeholder) = 1;
14775 if (TREE_CODE (t) == MEM_REF)
14777 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14778 VAR_DECL, NULL_TREE, type);
14779 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14780 DECL_ARTIFICIAL (decl_placeholder) = 1;
14781 DECL_IGNORED_P (decl_placeholder) = 1;
14783 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14784 c_mark_addressable (placeholder);
14785 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14786 c_mark_addressable (decl_placeholder ? decl_placeholder
14787 : OMP_CLAUSE_DECL (c));
14788 OMP_CLAUSE_REDUCTION_MERGE (c)
14789 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14790 TREE_VEC_ELT (list, 0),
14791 TREE_VEC_ELT (list, 1),
14792 decl_placeholder ? decl_placeholder
14793 : OMP_CLAUSE_DECL (c), placeholder);
14794 OMP_CLAUSE_REDUCTION_MERGE (c)
14795 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14796 void_type_node, NULL_TREE,
14797 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14798 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14799 if (TREE_VEC_LENGTH (list) == 6)
14801 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14802 c_mark_addressable (decl_placeholder ? decl_placeholder
14803 : OMP_CLAUSE_DECL (c));
14804 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14805 c_mark_addressable (placeholder);
14806 tree init = TREE_VEC_ELT (list, 5);
14807 if (init == error_mark_node)
14808 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14809 OMP_CLAUSE_REDUCTION_INIT (c)
14810 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14811 TREE_VEC_ELT (list, 3),
14812 decl_placeholder ? decl_placeholder
14813 : OMP_CLAUSE_DECL (c), placeholder);
14814 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14816 tree v = decl_placeholder ? decl_placeholder : t;
14817 OMP_CLAUSE_REDUCTION_INIT (c)
14818 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14819 OMP_CLAUSE_REDUCTION_INIT (c));
14821 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14822 c_find_omp_placeholder_r,
14823 placeholder, NULL))
14824 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14829 tree v = decl_placeholder ? decl_placeholder : t;
14830 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14831 init = build_constructor (TREE_TYPE (v), NULL);
14833 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14834 OMP_CLAUSE_REDUCTION_INIT (c)
14835 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14837 OMP_CLAUSE_REDUCTION_INIT (c)
14838 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14839 void_type_node, NULL_TREE,
14840 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14841 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14843 if (TREE_CODE (t) == MEM_REF)
14845 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14846 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14849 sorry ("variable length element type in array "
14850 "%<reduction%> clause");
14854 t = TREE_OPERAND (t, 0);
14855 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14856 t = TREE_OPERAND (t, 0);
14857 if (TREE_CODE (t) == ADDR_EXPR)
14858 t = TREE_OPERAND (t, 0);
14860 goto check_dup_generic_t;
14862 case OMP_CLAUSE_COPYPRIVATE:
14863 copyprivate_seen = true;
14866 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14867 "%<nowait%> clause must not be used together "
14868 "with %<copyprivate%>");
14869 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14870 nowait_clause = NULL;
14872 goto check_dup_generic;
14874 case OMP_CLAUSE_COPYIN:
14875 t = OMP_CLAUSE_DECL (c);
14876 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14878 error_at (OMP_CLAUSE_LOCATION (c),
14879 "%qE must be %<threadprivate%> for %<copyin%>", t);
14883 goto check_dup_generic;
14885 case OMP_CLAUSE_LINEAR:
14886 if (ort != C_ORT_OMP_DECLARE_SIMD)
14887 need_implicitly_determined = true;
14888 t = OMP_CLAUSE_DECL (c);
14889 if (ort != C_ORT_OMP_DECLARE_SIMD
14890 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14891 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14893 error_at (OMP_CLAUSE_LOCATION (c),
14894 "modifier should not be specified in %<linear%> "
14895 "clause on %<simd%> or %<for%> constructs when not "
14896 "using OpenMP 5.2 modifiers");
14897 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14899 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14900 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14902 error_at (OMP_CLAUSE_LOCATION (c),
14903 "linear clause applied to non-integral non-pointer "
14904 "variable with type %qT", TREE_TYPE (t));
14908 if (TYPE_ATOMIC (TREE_TYPE (t)))
14910 error_at (OMP_CLAUSE_LOCATION (c),
14911 "%<_Atomic%> %qD in %<linear%> clause", t);
14915 if (ort == C_ORT_OMP_DECLARE_SIMD)
14917 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14918 if (TREE_CODE (s) == PARM_DECL)
14920 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14921 /* map_head bitmap is used as uniform_head if
14923 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14924 linear_variable_step_check = true;
14925 goto check_dup_generic;
14927 if (TREE_CODE (s) != INTEGER_CST)
14929 error_at (OMP_CLAUSE_LOCATION (c),
14930 "%<linear%> clause step %qE is neither constant "
14931 "nor a parameter", s);
14936 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14938 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14939 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14940 OMP_CLAUSE_DECL (c), s);
14941 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14942 sizetype, fold_convert (sizetype, s),
14944 (sizetype, OMP_CLAUSE_DECL (c)));
14945 if (s == error_mark_node)
14947 OMP_CLAUSE_LINEAR_STEP (c) = s;
14950 OMP_CLAUSE_LINEAR_STEP (c)
14951 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14952 goto check_dup_generic;
14955 t = OMP_CLAUSE_DECL (c);
14956 check_dup_generic_t:
14957 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14959 error_at (OMP_CLAUSE_LOCATION (c),
14960 "%qE is not a variable in clause %qs", t,
14961 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14964 else if ((ort == C_ORT_ACC
14965 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14966 || (ort == C_ORT_OMP
14967 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14968 || (OMP_CLAUSE_CODE (c)
14969 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14970 || (ort == C_ORT_OMP_TARGET
14971 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14973 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14974 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14975 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14977 error_at (OMP_CLAUSE_LOCATION (c),
14978 "%qD appears more than once in data-sharing "
14983 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14984 target_in_reduction_seen = true;
14985 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14987 error_at (OMP_CLAUSE_LOCATION (c),
14989 ? "%qD appears more than once in reduction clauses"
14990 : "%qD appears more than once in data clauses",
14995 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14997 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14998 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14999 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
15000 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15002 error_at (OMP_CLAUSE_LOCATION (c),
15003 "%qE appears more than once in data clauses", t);
15006 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15007 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
15008 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15009 && bitmap_bit_p (&map_head, DECL_UID (t)))
15011 if (ort == C_ORT_ACC)
15012 error_at (OMP_CLAUSE_LOCATION (c),
15013 "%qD appears more than once in data clauses", t);
15015 error_at (OMP_CLAUSE_LOCATION (c),
15016 "%qD appears both in data and map clauses", t);
15020 bitmap_set_bit (&generic_head, DECL_UID (t));
15023 case OMP_CLAUSE_FIRSTPRIVATE:
15024 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
15027 implicit_moved = true;
15028 /* Move firstprivate and map clauses with
15029 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
15031 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
15032 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
15034 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
15035 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
15038 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
15039 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15041 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
15042 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
15045 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
15046 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15049 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
15055 t = OMP_CLAUSE_DECL (c);
15056 need_complete = true;
15057 need_implicitly_determined = true;
15058 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15060 error_at (OMP_CLAUSE_LOCATION (c),
15061 "%qE is not a variable in clause %<firstprivate%>", t);
15064 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15065 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
15066 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15068 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15069 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15070 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15072 error_at (OMP_CLAUSE_LOCATION (c),
15073 "%qE appears more than once in data clauses", t);
15076 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
15078 if (ort == C_ORT_ACC)
15079 error_at (OMP_CLAUSE_LOCATION (c),
15080 "%qD appears more than once in data clauses", t);
15081 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15082 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
15083 /* Silently drop the clause. */;
15085 error_at (OMP_CLAUSE_LOCATION (c),
15086 "%qD appears both in data and map clauses", t);
15090 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
15093 case OMP_CLAUSE_LASTPRIVATE:
15094 t = OMP_CLAUSE_DECL (c);
15095 need_complete = true;
15096 need_implicitly_determined = true;
15097 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15099 error_at (OMP_CLAUSE_LOCATION (c),
15100 "%qE is not a variable in clause %<lastprivate%>", t);
15103 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15104 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
15106 error_at (OMP_CLAUSE_LOCATION (c),
15107 "%qE appears more than once in data clauses", t);
15111 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
15114 case OMP_CLAUSE_ALIGNED:
15115 t = OMP_CLAUSE_DECL (c);
15116 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15118 error_at (OMP_CLAUSE_LOCATION (c),
15119 "%qE is not a variable in %<aligned%> clause", t);
15122 else if (!POINTER_TYPE_P (TREE_TYPE (t))
15123 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15125 error_at (OMP_CLAUSE_LOCATION (c),
15126 "%qE in %<aligned%> clause is neither a pointer nor "
15130 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15132 error_at (OMP_CLAUSE_LOCATION (c),
15133 "%<_Atomic%> %qD in %<aligned%> clause", t);
15137 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15139 error_at (OMP_CLAUSE_LOCATION (c),
15140 "%qE appears more than once in %<aligned%> clauses",
15145 bitmap_set_bit (&aligned_head, DECL_UID (t));
15148 case OMP_CLAUSE_NONTEMPORAL:
15149 t = OMP_CLAUSE_DECL (c);
15150 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15152 error_at (OMP_CLAUSE_LOCATION (c),
15153 "%qE is not a variable in %<nontemporal%> clause", t);
15156 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15158 error_at (OMP_CLAUSE_LOCATION (c),
15159 "%qE appears more than once in %<nontemporal%> "
15164 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15167 case OMP_CLAUSE_ALLOCATE:
15168 t = OMP_CLAUSE_DECL (c);
15169 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15171 error_at (OMP_CLAUSE_LOCATION (c),
15172 "%qE is not a variable in %<allocate%> clause", t);
15175 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15177 warning_at (OMP_CLAUSE_LOCATION (c), 0,
15178 "%qE appears more than once in %<allocate%> clauses",
15184 bitmap_set_bit (&aligned_head, DECL_UID (t));
15185 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
15186 allocate_seen = true;
15190 case OMP_CLAUSE_DOACROSS:
15191 t = OMP_CLAUSE_DECL (c);
15192 if (t == NULL_TREE)
15194 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
15196 gcc_assert (TREE_CODE (t) == TREE_LIST);
15197 for (; t; t = TREE_CHAIN (t))
15199 tree decl = TREE_VALUE (t);
15200 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15202 tree offset = TREE_PURPOSE (t);
15203 bool neg = wi::neg_p (wi::to_wide (offset));
15204 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
15205 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
15206 neg ? MINUS_EXPR : PLUS_EXPR,
15208 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15210 fold_convert (sizetype, t2),
15211 fold_convert (sizetype, decl));
15212 if (t2 == error_mark_node)
15217 TREE_PURPOSE (t) = t2;
15222 gcc_unreachable ();
15223 case OMP_CLAUSE_DEPEND:
15224 case OMP_CLAUSE_AFFINITY:
15225 t = OMP_CLAUSE_DECL (c);
15226 if (TREE_CODE (t) == TREE_LIST
15227 && TREE_PURPOSE (t)
15228 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
15230 if (TREE_PURPOSE (t) != last_iterators)
15231 last_iterators_remove
15232 = c_omp_finish_iterators (TREE_PURPOSE (t));
15233 last_iterators = TREE_PURPOSE (t);
15234 t = TREE_VALUE (t);
15235 if (last_iterators_remove)
15236 t = error_mark_node;
15239 last_iterators = NULL_TREE;
15240 if (TREE_CODE (t) == TREE_LIST)
15242 if (handle_omp_array_sections (c, ort))
15244 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15245 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15247 error_at (OMP_CLAUSE_LOCATION (c),
15248 "%<depend%> clause with %<depobj%> dependence "
15249 "type on array section");
15254 if (t == error_mark_node)
15256 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15257 && t == ridpointers[RID_OMP_ALL_MEMORY])
15259 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
15260 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
15262 error_at (OMP_CLAUSE_LOCATION (c),
15263 "%<omp_all_memory%> used with %<depend%> kind "
15264 "other than %<out%> or %<inout%>");
15268 else if (!lvalue_p (t))
15270 error_at (OMP_CLAUSE_LOCATION (c),
15271 "%qE is not lvalue expression nor array section in "
15273 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15276 else if (TREE_CODE (t) == COMPONENT_REF
15277 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
15279 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15280 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
15281 error_at (OMP_CLAUSE_LOCATION (c),
15282 "bit-field %qE in %qs clause", t,
15283 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15286 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15287 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15289 if (!c_omp_depend_t_p (TREE_TYPE (t)))
15291 error_at (OMP_CLAUSE_LOCATION (c),
15292 "%qE does not have %<omp_depend_t%> type in "
15293 "%<depend%> clause with %<depobj%> dependence "
15298 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15299 && c_omp_depend_t_p (TREE_TYPE (t)))
15301 error_at (OMP_CLAUSE_LOCATION (c),
15302 "%qE should not have %<omp_depend_t%> type in "
15303 "%<depend%> clause with dependence type other than "
15309 if (t == ridpointers[RID_OMP_ALL_MEMORY])
15310 t = null_pointer_node;
15313 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
15314 ADDR_EXPR, t, false);
15315 if (addr == error_mark_node)
15320 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
15322 if (t == error_mark_node)
15328 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
15329 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
15330 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
15332 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
15334 OMP_CLAUSE_DECL (c) = t;
15338 case OMP_CLAUSE_MAP:
15339 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
15340 goto move_implicit;
15342 case OMP_CLAUSE_TO:
15343 case OMP_CLAUSE_FROM:
15344 case OMP_CLAUSE__CACHE_:
15345 t = OMP_CLAUSE_DECL (c);
15346 if (TREE_CODE (t) == TREE_LIST)
15349 grp_sentinel = OMP_CLAUSE_CHAIN (c);
15351 if (handle_omp_array_sections (c, ort))
15355 t = OMP_CLAUSE_DECL (c);
15356 if (!omp_mappable_type (TREE_TYPE (t)))
15358 error_at (OMP_CLAUSE_LOCATION (c),
15359 "array section does not have mappable type "
15361 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15364 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15366 error_at (OMP_CLAUSE_LOCATION (c),
15367 "%<_Atomic%> %qE in %qs clause", t,
15368 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15371 while (TREE_CODE (t) == ARRAY_REF)
15372 t = TREE_OPERAND (t, 0);
15373 if (TREE_CODE (t) == COMPONENT_REF
15374 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
15378 t = TREE_OPERAND (t, 0);
15379 if (TREE_CODE (t) == MEM_REF
15380 || TREE_CODE (t) == INDIRECT_REF)
15382 t = TREE_OPERAND (t, 0);
15384 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15385 t = TREE_OPERAND (t, 0);
15388 while (TREE_CODE (t) == COMPONENT_REF
15389 || TREE_CODE (t) == ARRAY_REF);
15391 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15392 && OMP_CLAUSE_MAP_IMPLICIT (c)
15393 && (bitmap_bit_p (&map_head, DECL_UID (t))
15394 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15395 || bitmap_bit_p (&map_firstprivate_head,
15401 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15403 if (bitmap_bit_p (&map_head, DECL_UID (t)))
15405 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15406 error_at (OMP_CLAUSE_LOCATION (c),
15407 "%qD appears more than once in motion "
15409 else if (ort == C_ORT_ACC)
15410 error_at (OMP_CLAUSE_LOCATION (c),
15411 "%qD appears more than once in data "
15414 error_at (OMP_CLAUSE_LOCATION (c),
15415 "%qD appears more than once in map "
15421 bitmap_set_bit (&map_head, DECL_UID (t));
15422 bitmap_set_bit (&map_field_head, DECL_UID (t));
15426 if (c_oacc_check_attachments (c))
15428 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15429 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15430 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15431 /* In this case, we have a single array element which is a
15432 pointer, and we already set OMP_CLAUSE_SIZE in
15433 handle_omp_array_sections above. For attach/detach clauses,
15434 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15436 OMP_CLAUSE_SIZE (c) = size_zero_node;
15439 if (t == error_mark_node)
15444 /* OpenACC attach / detach clauses must be pointers. */
15445 if (c_oacc_check_attachments (c))
15450 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15451 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15452 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15453 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15454 bias) to zero here, so it is not set erroneously to the pointer
15455 size later on in gimplify.cc. */
15456 OMP_CLAUSE_SIZE (c) = size_zero_node;
15457 while (TREE_CODE (t) == INDIRECT_REF
15458 || TREE_CODE (t) == ARRAY_REF)
15460 t = TREE_OPERAND (t, 0);
15462 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15463 t = TREE_OPERAND (t, 0);
15465 while (TREE_CODE (t) == COMPOUND_EXPR)
15467 t = TREE_OPERAND (t, 1);
15470 indir_component_ref_p = false;
15471 if (TREE_CODE (t) == COMPONENT_REF
15472 && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15473 || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15474 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15476 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15477 indir_component_ref_p = true;
15479 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15480 t = TREE_OPERAND (t, 0);
15483 if (TREE_CODE (t) == COMPONENT_REF
15484 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15486 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15488 error_at (OMP_CLAUSE_LOCATION (c),
15489 "bit-field %qE in %qs clause",
15490 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15493 else if (!omp_mappable_type (TREE_TYPE (t)))
15495 error_at (OMP_CLAUSE_LOCATION (c),
15496 "%qE does not have a mappable type in %qs clause",
15497 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15500 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15502 error_at (OMP_CLAUSE_LOCATION (c),
15503 "%<_Atomic%> %qE in %qs clause", t,
15504 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15507 while (TREE_CODE (t) == COMPONENT_REF)
15509 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15512 error_at (OMP_CLAUSE_LOCATION (c),
15513 "%qE is a member of a union", t);
15517 t = TREE_OPERAND (t, 0);
15518 if (TREE_CODE (t) == MEM_REF)
15520 if (maybe_ne (mem_ref_offset (t), 0))
15521 error_at (OMP_CLAUSE_LOCATION (c),
15522 "cannot dereference %qE in %qs clause", t,
15523 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15525 t = TREE_OPERAND (t, 0);
15527 while (TREE_CODE (t) == MEM_REF
15528 || TREE_CODE (t) == INDIRECT_REF
15529 || TREE_CODE (t) == ARRAY_REF)
15531 t = TREE_OPERAND (t, 0);
15533 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15534 t = TREE_OPERAND (t, 0);
15539 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15541 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15542 || (ort != C_ORT_ACC
15543 && bitmap_bit_p (&map_head, DECL_UID (t))))
15547 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15549 error_at (OMP_CLAUSE_LOCATION (c),
15550 "%qE is not a variable in %qs clause", t,
15551 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15554 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15556 error_at (OMP_CLAUSE_LOCATION (c),
15557 "%qD is threadprivate variable in %qs clause", t,
15558 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15561 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15562 || (OMP_CLAUSE_MAP_KIND (c)
15563 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15564 && !indir_component_ref_p
15565 && !c_mark_addressable (t))
15567 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15568 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15569 || (OMP_CLAUSE_MAP_KIND (c)
15570 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15571 || (OMP_CLAUSE_MAP_KIND (c)
15572 == GOMP_MAP_FORCE_DEVICEPTR)))
15573 && t == OMP_CLAUSE_DECL (c)
15574 && !omp_mappable_type (TREE_TYPE (t)))
15576 error_at (OMP_CLAUSE_LOCATION (c),
15577 "%qD does not have a mappable type in %qs clause", t,
15578 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15581 else if (TREE_TYPE (t) == error_mark_node)
15583 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15585 error_at (OMP_CLAUSE_LOCATION (c),
15586 "%<_Atomic%> %qE in %qs clause", t,
15587 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15590 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15591 && OMP_CLAUSE_MAP_IMPLICIT (c)
15592 && (bitmap_bit_p (&map_head, DECL_UID (t))
15593 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15594 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15596 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15597 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15599 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15600 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15601 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15603 error_at (OMP_CLAUSE_LOCATION (c),
15604 "%qD appears more than once in data clauses", t);
15607 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15608 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15610 if (ort == C_ORT_ACC)
15611 error_at (OMP_CLAUSE_LOCATION (c),
15612 "%qD appears more than once in data clauses", t);
15614 error_at (OMP_CLAUSE_LOCATION (c),
15615 "%qD appears both in data and map clauses", t);
15619 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15621 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15622 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15624 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15625 error_at (OMP_CLAUSE_LOCATION (c),
15626 "%qD appears more than once in motion clauses", t);
15627 else if (ort == C_ORT_ACC)
15628 error_at (OMP_CLAUSE_LOCATION (c),
15629 "%qD appears more than once in data clauses", t);
15631 error_at (OMP_CLAUSE_LOCATION (c),
15632 "%qD appears more than once in map clauses", t);
15635 else if (ort == C_ORT_ACC
15636 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15638 error_at (OMP_CLAUSE_LOCATION (c),
15639 "%qD appears more than once in data clauses", t);
15642 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15643 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15645 if (ort == C_ORT_ACC)
15646 error_at (OMP_CLAUSE_LOCATION (c),
15647 "%qD appears more than once in data clauses", t);
15649 error_at (OMP_CLAUSE_LOCATION (c),
15650 "%qD appears both in data and map clauses", t);
15655 bitmap_set_bit (&map_head, DECL_UID (t));
15656 if (t != OMP_CLAUSE_DECL (c)
15657 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15658 bitmap_set_bit (&map_field_head, DECL_UID (t));
15662 case OMP_CLAUSE_ENTER:
15663 case OMP_CLAUSE_LINK:
15664 t = OMP_CLAUSE_DECL (c);
15666 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15667 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15668 && OMP_CLAUSE_ENTER_TO (c))
15670 if (TREE_CODE (t) == FUNCTION_DECL
15671 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15673 else if (!VAR_P (t))
15675 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15676 error_at (OMP_CLAUSE_LOCATION (c),
15677 "%qE is neither a variable nor a function name in "
15678 "clause %qs", t, cname);
15680 error_at (OMP_CLAUSE_LOCATION (c),
15681 "%qE is not a variable in clause %qs", t, cname);
15684 else if (DECL_THREAD_LOCAL_P (t))
15686 error_at (OMP_CLAUSE_LOCATION (c),
15687 "%qD is threadprivate variable in %qs clause", t,
15691 else if (!omp_mappable_type (TREE_TYPE (t)))
15693 error_at (OMP_CLAUSE_LOCATION (c),
15694 "%qD does not have a mappable type in %qs clause", t,
15700 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15702 error_at (OMP_CLAUSE_LOCATION (c),
15703 "%qE appears more than once on the same "
15704 "%<declare target%> directive", t);
15708 bitmap_set_bit (&generic_head, DECL_UID (t));
15711 case OMP_CLAUSE_UNIFORM:
15712 t = OMP_CLAUSE_DECL (c);
15713 if (TREE_CODE (t) != PARM_DECL)
15716 error_at (OMP_CLAUSE_LOCATION (c),
15717 "%qD is not an argument in %<uniform%> clause", t);
15719 error_at (OMP_CLAUSE_LOCATION (c),
15720 "%qE is not an argument in %<uniform%> clause", t);
15724 /* map_head bitmap is used as uniform_head if declare_simd. */
15725 bitmap_set_bit (&map_head, DECL_UID (t));
15726 goto check_dup_generic;
15728 case OMP_CLAUSE_IS_DEVICE_PTR:
15729 case OMP_CLAUSE_USE_DEVICE_PTR:
15730 t = OMP_CLAUSE_DECL (c);
15731 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15732 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15733 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15735 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15736 && ort != C_ORT_ACC)
15738 error_at (OMP_CLAUSE_LOCATION (c),
15739 "%qs variable is not a pointer",
15740 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15743 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15745 error_at (OMP_CLAUSE_LOCATION (c),
15746 "%qs variable is neither a pointer nor an array",
15747 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15751 goto check_dup_generic;
15753 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15754 t = OMP_CLAUSE_DECL (c);
15755 if (TREE_CODE (t) == TREE_LIST)
15757 if (handle_omp_array_sections (c, ort))
15761 t = OMP_CLAUSE_DECL (c);
15762 while (TREE_CODE (t) == ARRAY_REF)
15763 t = TREE_OPERAND (t, 0);
15766 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15767 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15768 c_mark_addressable (t);
15769 goto check_dup_generic_t;
15771 case OMP_CLAUSE_USE_DEVICE_ADDR:
15772 t = OMP_CLAUSE_DECL (c);
15773 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15774 c_mark_addressable (t);
15775 goto check_dup_generic;
15777 case OMP_CLAUSE_NOWAIT:
15778 if (copyprivate_seen)
15780 error_at (OMP_CLAUSE_LOCATION (c),
15781 "%<nowait%> clause must not be used together "
15782 "with %<copyprivate%>");
15786 nowait_clause = pc;
15787 pc = &OMP_CLAUSE_CHAIN (c);
15790 case OMP_CLAUSE_ORDER:
15791 if (ordered_clause)
15793 error_at (OMP_CLAUSE_LOCATION (c),
15794 "%<order%> clause must not be used together "
15795 "with %<ordered%>");
15799 else if (order_clause)
15801 /* Silently remove duplicates. */
15806 pc = &OMP_CLAUSE_CHAIN (c);
15809 case OMP_CLAUSE_DETACH:
15810 t = OMP_CLAUSE_DECL (c);
15813 error_at (OMP_CLAUSE_LOCATION (c),
15814 "too many %qs clauses on a task construct",
15820 pc = &OMP_CLAUSE_CHAIN (c);
15821 c_mark_addressable (t);
15824 case OMP_CLAUSE_IF:
15825 case OMP_CLAUSE_NUM_THREADS:
15826 case OMP_CLAUSE_NUM_TEAMS:
15827 case OMP_CLAUSE_THREAD_LIMIT:
15828 case OMP_CLAUSE_DEFAULT:
15829 case OMP_CLAUSE_UNTIED:
15830 case OMP_CLAUSE_COLLAPSE:
15831 case OMP_CLAUSE_FINAL:
15832 case OMP_CLAUSE_DEVICE:
15833 case OMP_CLAUSE_DIST_SCHEDULE:
15834 case OMP_CLAUSE_PARALLEL:
15835 case OMP_CLAUSE_FOR:
15836 case OMP_CLAUSE_SECTIONS:
15837 case OMP_CLAUSE_TASKGROUP:
15838 case OMP_CLAUSE_PROC_BIND:
15839 case OMP_CLAUSE_DEVICE_TYPE:
15840 case OMP_CLAUSE_PRIORITY:
15841 case OMP_CLAUSE_GRAINSIZE:
15842 case OMP_CLAUSE_NUM_TASKS:
15843 case OMP_CLAUSE_THREADS:
15844 case OMP_CLAUSE_SIMD:
15845 case OMP_CLAUSE_HINT:
15846 case OMP_CLAUSE_FILTER:
15847 case OMP_CLAUSE_DEFAULTMAP:
15848 case OMP_CLAUSE_BIND:
15849 case OMP_CLAUSE_NUM_GANGS:
15850 case OMP_CLAUSE_NUM_WORKERS:
15851 case OMP_CLAUSE_VECTOR_LENGTH:
15852 case OMP_CLAUSE_ASYNC:
15853 case OMP_CLAUSE_WAIT:
15854 case OMP_CLAUSE_AUTO:
15855 case OMP_CLAUSE_INDEPENDENT:
15856 case OMP_CLAUSE_SEQ:
15857 case OMP_CLAUSE_GANG:
15858 case OMP_CLAUSE_WORKER:
15859 case OMP_CLAUSE_VECTOR:
15860 case OMP_CLAUSE_TILE:
15861 case OMP_CLAUSE_IF_PRESENT:
15862 case OMP_CLAUSE_FINALIZE:
15863 case OMP_CLAUSE_NOHOST:
15864 pc = &OMP_CLAUSE_CHAIN (c);
15867 case OMP_CLAUSE_MERGEABLE:
15868 mergeable_seen = true;
15869 pc = &OMP_CLAUSE_CHAIN (c);
15872 case OMP_CLAUSE_NOGROUP:
15874 pc = &OMP_CLAUSE_CHAIN (c);
15877 case OMP_CLAUSE_SCHEDULE:
15878 schedule_clause = c;
15879 pc = &OMP_CLAUSE_CHAIN (c);
15882 case OMP_CLAUSE_ORDERED:
15883 ordered_clause = c;
15886 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15887 "%<order%> clause must not be used together "
15888 "with %<ordered%>");
15889 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15890 order_clause = NULL;
15892 pc = &OMP_CLAUSE_CHAIN (c);
15895 case OMP_CLAUSE_SAFELEN:
15897 pc = &OMP_CLAUSE_CHAIN (c);
15899 case OMP_CLAUSE_SIMDLEN:
15901 pc = &OMP_CLAUSE_CHAIN (c);
15904 case OMP_CLAUSE_INBRANCH:
15905 case OMP_CLAUSE_NOTINBRANCH:
15908 error_at (OMP_CLAUSE_LOCATION (c),
15909 "%<inbranch%> clause is incompatible with "
15910 "%<notinbranch%>");
15914 branch_seen = true;
15915 pc = &OMP_CLAUSE_CHAIN (c);
15918 case OMP_CLAUSE_INCLUSIVE:
15919 case OMP_CLAUSE_EXCLUSIVE:
15920 need_complete = true;
15921 need_implicitly_determined = true;
15922 t = OMP_CLAUSE_DECL (c);
15923 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15925 error_at (OMP_CLAUSE_LOCATION (c),
15926 "%qE is not a variable in clause %qs", t,
15927 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15933 gcc_unreachable ();
15938 t = OMP_CLAUSE_DECL (c);
15942 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15943 if (t == error_mark_node)
15947 if (need_implicitly_determined)
15949 const char *share_name = NULL;
15951 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15952 share_name = "threadprivate";
15953 else switch (c_omp_predetermined_sharing (t))
15955 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15957 case OMP_CLAUSE_DEFAULT_SHARED:
15958 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15959 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15960 && c_omp_predefined_variable (t))
15961 /* The __func__ variable and similar function-local
15962 predefined variables may be listed in a shared or
15963 firstprivate clause. */
15965 share_name = "shared";
15967 case OMP_CLAUSE_DEFAULT_PRIVATE:
15968 share_name = "private";
15971 gcc_unreachable ();
15975 error_at (OMP_CLAUSE_LOCATION (c),
15976 "%qE is predetermined %qs for %qs",
15978 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15981 else if (TREE_READONLY (t)
15982 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15983 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15985 error_at (OMP_CLAUSE_LOCATION (c),
15986 "%<const%> qualified %qE may appear only in "
15987 "%<shared%> or %<firstprivate%> clauses", t);
15997 /* If we found a clause to remove, we want to remove the whole
15998 expanded group, otherwise gimplify
15999 (omp_resolve_clause_dependencies) can get confused. */
16000 *grp_start_p = grp_sentinel;
16002 grp_start_p = NULL;
16005 *pc = OMP_CLAUSE_CHAIN (c);
16008 pc = &OMP_CLAUSE_CHAIN (c);
16013 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
16014 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
16016 error_at (OMP_CLAUSE_LOCATION (simdlen),
16017 "%<simdlen%> clause value is bigger than "
16018 "%<safelen%> clause value");
16019 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
16020 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
16025 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16026 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
16028 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16029 "%<nonmonotonic%> schedule modifier specified together "
16030 "with %<ordered%> clause");
16031 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16032 = (enum omp_clause_schedule_kind)
16033 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16034 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
16037 if (reduction_seen < 0 && ordered_clause)
16039 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
16040 "%qs clause specified together with %<inscan%> "
16041 "%<reduction%> clause", "ordered");
16042 reduction_seen = -2;
16045 if (reduction_seen < 0 && schedule_clause)
16047 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16048 "%qs clause specified together with %<inscan%> "
16049 "%<reduction%> clause", "schedule");
16050 reduction_seen = -2;
16053 if (linear_variable_step_check
16054 || reduction_seen == -2
16056 || target_in_reduction_seen)
16057 for (pc = &clauses, c = clauses; c ; c = *pc)
16059 bool remove = false;
16061 switch (OMP_CLAUSE_CODE (c))
16063 case OMP_CLAUSE_REDUCTION:
16064 case OMP_CLAUSE_IN_REDUCTION:
16065 case OMP_CLAUSE_TASK_REDUCTION:
16066 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
16068 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
16069 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16070 t = TREE_OPERAND (t, 0);
16071 if (TREE_CODE (t) == ADDR_EXPR
16072 || TREE_CODE (t) == INDIRECT_REF)
16073 t = TREE_OPERAND (t, 0);
16075 bitmap_clear_bit (&aligned_head, DECL_UID (t));
16079 case OMP_CLAUSE_PRIVATE:
16080 case OMP_CLAUSE_FIRSTPRIVATE:
16081 case OMP_CLAUSE_LASTPRIVATE:
16082 case OMP_CLAUSE_LINEAR:
16083 if (DECL_P (OMP_CLAUSE_DECL (c)))
16084 bitmap_clear_bit (&aligned_head,
16085 DECL_UID (OMP_CLAUSE_DECL (c)));
16090 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16091 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
16092 && !bitmap_bit_p (&map_head,
16093 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
16095 error_at (OMP_CLAUSE_LOCATION (c),
16096 "%<linear%> clause step is a parameter %qD not "
16097 "specified in %<uniform%> clause",
16098 OMP_CLAUSE_LINEAR_STEP (c));
16101 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16102 && reduction_seen == -2)
16103 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
16104 if (target_in_reduction_seen
16105 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
16107 tree t = OMP_CLAUSE_DECL (c);
16108 while (handled_component_p (t)
16109 || TREE_CODE (t) == INDIRECT_REF
16110 || TREE_CODE (t) == ADDR_EXPR
16111 || TREE_CODE (t) == MEM_REF
16112 || TREE_CODE (t) == NON_LVALUE_EXPR)
16113 t = TREE_OPERAND (t, 0);
16115 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16116 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
16120 *pc = OMP_CLAUSE_CHAIN (c);
16122 pc = &OMP_CLAUSE_CHAIN (c);
16126 for (pc = &clauses, c = clauses; c ; c = *pc)
16128 bool remove = false;
16129 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
16130 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
16131 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
16133 error_at (OMP_CLAUSE_LOCATION (c),
16134 "%qD specified in %<allocate%> clause but not in "
16135 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
16139 *pc = OMP_CLAUSE_CHAIN (c);
16141 pc = &OMP_CLAUSE_CHAIN (c);
16144 if (nogroup_seen && reduction_seen)
16146 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
16147 "%<nogroup%> clause must not be used together with "
16148 "%<reduction%> clause");
16149 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
16154 if (mergeable_seen)
16156 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
16157 "%<detach%> clause must not be used together with "
16158 "%<mergeable%> clause");
16159 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
16163 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
16165 for (pc = &clauses, c = clauses; c ; c = *pc)
16167 bool remove = false;
16168 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
16169 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16170 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
16171 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
16172 && OMP_CLAUSE_DECL (c) == detach_decl)
16174 error_at (OMP_CLAUSE_LOCATION (c),
16175 "the event handle of a %<detach%> clause "
16176 "should not be in a data-sharing clause");
16180 *pc = OMP_CLAUSE_CHAIN (c);
16182 pc = &OMP_CLAUSE_CHAIN (c);
16187 bitmap_obstack_release (NULL);
16191 /* Return code to initialize DST with a copy constructor from SRC.
16192 C doesn't have copy constructors nor assignment operators, only for
16193 _Atomic vars we need to perform __atomic_load from src into a temporary
16194 followed by __atomic_store of the temporary to dst. */
16197 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
16199 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
16200 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
16202 location_t loc = OMP_CLAUSE_LOCATION (clause);
16203 tree type = TREE_TYPE (dst);
16204 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
16205 tree tmp = create_tmp_var (nonatomic_type);
16206 tree tmp_addr = build_fold_addr_expr (tmp);
16207 TREE_ADDRESSABLE (tmp) = 1;
16208 suppress_warning (tmp);
16209 tree src_addr = build_fold_addr_expr (src);
16210 tree dst_addr = build_fold_addr_expr (dst);
16211 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
16212 vec<tree, va_gc> *params;
16213 /* Expansion of a generic atomic load may require an addition
16214 element, so allocate enough to prevent a resize. */
16215 vec_alloc (params, 4);
16217 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
16218 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
16219 params->quick_push (src_addr);
16220 params->quick_push (tmp_addr);
16221 params->quick_push (seq_cst);
16222 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16224 vec_alloc (params, 4);
16226 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
16227 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
16228 params->quick_push (dst_addr);
16229 params->quick_push (tmp_addr);
16230 params->quick_push (seq_cst);
16231 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16232 return build2 (COMPOUND_EXPR, void_type_node, load, store);
16235 /* Create a transaction node. */
16238 c_finish_transaction (location_t loc, tree block, int flags)
16240 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
16241 if (flags & TM_STMT_ATTR_OUTER)
16242 TRANSACTION_EXPR_OUTER (stmt) = 1;
16243 if (flags & TM_STMT_ATTR_RELAXED)
16244 TRANSACTION_EXPR_RELAXED (stmt) = 1;
16245 return add_stmt (stmt);
16248 /* Make a variant type in the proper way for C/C++, propagating qualifiers
16249 down to the element type of an array. If ORIG_QUAL_TYPE is not
16250 NULL, then it should be used as the qualified type
16251 ORIG_QUAL_INDIRECT levels down in array type derivation (to
16252 preserve information about the typedef name from which an array
16253 type was derived). */
16256 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
16257 size_t orig_qual_indirect)
16259 if (type == error_mark_node)
16262 if (TREE_CODE (type) == ARRAY_TYPE)
16265 tree element_type = c_build_qualified_type (TREE_TYPE (type),
16266 type_quals, orig_qual_type,
16267 orig_qual_indirect - 1);
16269 /* See if we already have an identically qualified type. */
16270 if (orig_qual_type && orig_qual_indirect == 0)
16271 t = orig_qual_type;
16273 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
16275 if (TYPE_QUALS (strip_array_types (t)) == type_quals
16276 && TYPE_NAME (t) == TYPE_NAME (type)
16277 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
16278 && attribute_list_equal (TYPE_ATTRIBUTES (t),
16279 TYPE_ATTRIBUTES (type)))
16284 tree domain = TYPE_DOMAIN (type);
16286 t = build_variant_type_copy (type);
16287 TREE_TYPE (t) = element_type;
16289 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
16290 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
16291 SET_TYPE_STRUCTURAL_EQUALITY (t);
16292 else if (TYPE_CANONICAL (element_type) != element_type
16293 || (domain && TYPE_CANONICAL (domain) != domain))
16295 tree unqualified_canon
16296 = build_array_type (TYPE_CANONICAL (element_type),
16297 domain? TYPE_CANONICAL (domain)
16299 if (TYPE_REVERSE_STORAGE_ORDER (type))
16302 = build_distinct_type_copy (unqualified_canon);
16303 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
16306 = c_build_qualified_type (unqualified_canon, type_quals);
16309 TYPE_CANONICAL (t) = t;
16314 /* A restrict-qualified pointer type must be a pointer to object or
16315 incomplete type. Note that the use of POINTER_TYPE_P also allows
16316 REFERENCE_TYPEs, which is appropriate for C++. */
16317 if ((type_quals & TYPE_QUAL_RESTRICT)
16318 && (!POINTER_TYPE_P (type)
16319 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
16321 error ("invalid use of %<restrict%>");
16322 type_quals &= ~TYPE_QUAL_RESTRICT;
16325 tree var_type = (orig_qual_type && orig_qual_indirect == 0
16327 : build_qualified_type (type, type_quals));
16328 /* A variant type does not inherit the list of incomplete vars from the
16329 type main variant. */
16330 if ((RECORD_OR_UNION_TYPE_P (var_type)
16331 || TREE_CODE (var_type) == ENUMERAL_TYPE)
16332 && TYPE_MAIN_VARIANT (var_type) != var_type)
16333 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
16337 /* Build a VA_ARG_EXPR for the C parser. */
16340 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
16342 if (error_operand_p (type))
16343 return error_mark_node;
16344 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
16345 order because it takes the address of the expression. */
16346 else if (handled_component_p (expr)
16347 && reverse_storage_order_for_component_p (expr))
16349 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
16350 return error_mark_node;
16352 else if (!COMPLETE_TYPE_P (type))
16354 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
16356 return error_mark_node;
16358 else if (TREE_CODE (type) == FUNCTION_TYPE)
16360 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
16362 return error_mark_node;
16364 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
16365 warning_at (loc2, OPT_Wc___compat,
16366 "C++ requires promoted type, not enum type, in %<va_arg%>");
16367 return build_va_arg (loc2, expr, type);
16370 /* Return truthvalue of whether T1 is the same tree structure as T2.
16371 Return 1 if they are the same. Return false if they are different. */
16374 c_tree_equal (tree t1, tree t2)
16376 enum tree_code code1, code2;
16383 for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
16384 code1 = TREE_CODE (t1))
16385 t1 = TREE_OPERAND (t1, 0);
16386 for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
16387 code2 = TREE_CODE (t2))
16388 t2 = TREE_OPERAND (t2, 0);
16390 /* They might have become equal now. */
16394 if (code1 != code2)
16397 if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16403 return wi::to_wide (t1) == wi::to_wide (t2);
16406 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16409 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16410 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16411 TREE_STRING_LENGTH (t1));
16414 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16415 TREE_FIXED_CST (t2));
16418 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16419 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16422 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16425 /* We need to do this when determining whether or not two
16426 non-type pointer to member function template arguments
16428 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16429 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16434 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16436 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16437 if (!c_tree_equal (field, elt2->index)
16438 || !c_tree_equal (value, elt2->value))
16445 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16447 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16449 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16452 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16457 call_expr_arg_iterator iter1, iter2;
16458 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16460 for (arg1 = first_call_expr_arg (t1, &iter1),
16461 arg2 = first_call_expr_arg (t2, &iter2);
16463 arg1 = next_call_expr_arg (&iter1),
16464 arg2 = next_call_expr_arg (&iter2))
16465 if (!c_tree_equal (arg1, arg2))
16474 tree o1 = TREE_OPERAND (t1, 0);
16475 tree o2 = TREE_OPERAND (t2, 0);
16477 /* Special case: if either target is an unallocated VAR_DECL,
16478 it means that it's going to be unified with whatever the
16479 TARGET_EXPR is really supposed to initialize, so treat it
16480 as being equivalent to anything. */
16481 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16482 && !DECL_RTL_SET_P (o1))
16484 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16485 && !DECL_RTL_SET_P (o2))
16487 else if (!c_tree_equal (o1, o2))
16490 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16493 case COMPONENT_REF:
16494 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16496 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16502 case FUNCTION_DECL:
16503 case IDENTIFIER_NODE:
16510 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16512 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16513 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16514 TREE_VEC_ELT (t2, ix)))
16520 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16528 switch (TREE_CODE_CLASS (code1))
16532 case tcc_comparison:
16533 case tcc_expression:
16535 case tcc_reference:
16536 case tcc_statement:
16538 int i, n = TREE_OPERAND_LENGTH (t1);
16542 case PREINCREMENT_EXPR:
16543 case PREDECREMENT_EXPR:
16544 case POSTINCREMENT_EXPR:
16545 case POSTDECREMENT_EXPR:
16555 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16556 && n != TREE_OPERAND_LENGTH (t2))
16559 for (i = 0; i < n; ++i)
16560 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16567 return comptypes (t1, t2);
16569 gcc_unreachable ();
16573 /* Returns true when the function declaration FNDECL is implicit,
16574 introduced as a result of a call to an otherwise undeclared
16575 function, and false otherwise. */
16578 c_decl_implicit (const_tree fndecl)
16580 return C_DECL_IMPLICIT (fndecl);