1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2022 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 int require_constant_value;
88 static int require_constant_elements;
90 static bool null_pointer_constant_p (const_tree);
91 static tree qualify_type (tree, tree);
92 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
94 static int comp_target_types (location_t, tree, tree);
95 static int function_types_compatible_p (const_tree, const_tree, bool *,
97 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
98 static tree lookup_field (tree, tree);
99 static int convert_arguments (location_t, vec<location_t>, tree,
100 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
102 static tree pointer_diff (location_t, tree, tree, tree *);
103 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
104 enum impl_conv, bool, tree, tree, int,
106 static tree valid_compound_expr_initializer (tree, tree);
107 static void push_string (const char *);
108 static void push_member_name (tree);
109 static int spelling_length (void);
110 static char *print_spelling (char *);
111 static void warning_init (location_t, int, const char *);
112 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
113 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
114 bool, struct obstack *);
115 static void output_pending_init_elements (int, struct obstack *);
116 static bool set_designator (location_t, bool, struct obstack *);
117 static void push_range_stack (tree, struct obstack *);
118 static void add_pending_init (location_t, tree, tree, tree, bool,
120 static void set_nonincremental_init (struct obstack *);
121 static void set_nonincremental_init_from_string (tree, struct obstack *);
122 static tree find_init_member (tree, struct obstack *);
123 static void readonly_warning (tree, enum lvalue_use);
124 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
125 static void record_maybe_used_decl (tree);
126 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
128 /* Return true if EXP is a null pointer constant, false otherwise. */
131 null_pointer_constant_p (const_tree expr)
133 /* This should really operate on c_expr structures, but they aren't
134 yet available everywhere required. */
135 tree type = TREE_TYPE (expr);
136 return (TREE_CODE (expr) == INTEGER_CST
137 && !TREE_OVERFLOW (expr)
138 && integer_zerop (expr)
139 && (INTEGRAL_TYPE_P (type)
140 || (TREE_CODE (type) == POINTER_TYPE
141 && VOID_TYPE_P (TREE_TYPE (type))
142 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
145 /* EXPR may appear in an unevaluated part of an integer constant
146 expression, but not in an evaluated part. Wrap it in a
147 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
148 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
151 note_integer_operands (tree expr)
154 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
156 ret = copy_node (expr);
157 TREE_OVERFLOW (ret) = 1;
161 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
162 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
167 /* Having checked whether EXPR may appear in an unevaluated part of an
168 integer constant expression and found that it may, remove any
169 C_MAYBE_CONST_EXPR noting this fact and return the resulting
173 remove_c_maybe_const_expr (tree expr)
175 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
176 return C_MAYBE_CONST_EXPR_EXPR (expr);
181 \f/* This is a cache to hold if two types are compatible or not. */
183 struct tagged_tu_seen_cache {
184 const struct tagged_tu_seen_cache * next;
187 /* The return value of tagged_types_tu_compatible_p if we had seen
188 these two types already. */
192 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
193 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
195 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
196 does not have an incomplete type. (That includes void types.)
197 LOC is the location of the use. */
200 require_complete_type (location_t loc, tree value)
202 tree type = TREE_TYPE (value);
204 if (error_operand_p (value))
205 return error_mark_node;
207 /* First, detect a valid value with a complete type. */
208 if (COMPLETE_TYPE_P (type))
211 c_incomplete_type_error (loc, value, type);
212 return error_mark_node;
215 /* Print an error message for invalid use of an incomplete type.
216 VALUE is the expression that was used (or 0 if that isn't known)
217 and TYPE is the type that was invalid. LOC is the location for
221 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
223 /* Avoid duplicate error message. */
224 if (TREE_CODE (type) == ERROR_MARK)
227 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
228 error_at (loc, "%qD has an incomplete type %qT", value, type);
232 /* We must print an error message. Be clever about what it says. */
234 switch (TREE_CODE (type))
242 error_at (loc, "invalid use of void expression");
246 if (TYPE_DOMAIN (type))
248 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
250 error_at (loc, "invalid use of flexible array member");
253 type = TREE_TYPE (type);
256 error_at (loc, "invalid use of array with unspecified bounds");
263 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
264 error_at (loc, "invalid use of undefined type %qT", type);
266 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
267 error_at (loc, "invalid use of incomplete typedef %qT", type);
271 /* Given a type, apply default promotions wrt unnamed function
272 arguments and return the new type. */
275 c_type_promotes_to (tree type)
277 tree ret = NULL_TREE;
279 if (TYPE_MAIN_VARIANT (type) == float_type_node)
280 ret = double_type_node;
281 else if (c_promoting_integer_type_p (type))
283 /* Preserve unsignedness if not really getting any wider. */
284 if (TYPE_UNSIGNED (type)
285 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
286 ret = unsigned_type_node;
288 ret = integer_type_node;
291 if (ret != NULL_TREE)
292 return (TYPE_ATOMIC (type)
293 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
299 /* Return true if between two named address spaces, whether there is a superset
300 named address space that encompasses both address spaces. If there is a
301 superset, return which address space is the superset. */
304 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
311 else if (targetm.addr_space.subset_p (as1, as2))
316 else if (targetm.addr_space.subset_p (as2, as1))
325 /* Return a variant of TYPE which has all the type qualifiers of LIKE
326 as well as those of TYPE. */
329 qualify_type (tree type, tree like)
331 addr_space_t as_type = TYPE_ADDR_SPACE (type);
332 addr_space_t as_like = TYPE_ADDR_SPACE (like);
333 addr_space_t as_common;
335 /* If the two named address spaces are different, determine the common
336 superset address space. If there isn't one, raise an error. */
337 if (!addr_space_superset (as_type, as_like, &as_common))
340 error ("%qT and %qT are in disjoint named address spaces",
344 return c_build_qualified_type (type,
345 TYPE_QUALS_NO_ADDR_SPACE (type)
346 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
347 | ENCODE_QUAL_ADDR_SPACE (as_common));
350 /* Return true iff the given tree T is a variable length array. */
353 c_vla_type_p (const_tree t)
355 if (TREE_CODE (t) == ARRAY_TYPE
356 && C_TYPE_VARIABLE_SIZE (t))
361 /* If NTYPE is a type of a non-variadic function with a prototype
362 and OTYPE is a type of a function without a prototype and ATTRS
363 contains attribute format, diagnosess and removes it from ATTRS.
364 Returns the result of build_type_attribute_variant of NTYPE and
365 the (possibly) modified ATTRS. */
368 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
370 if (!prototype_p (otype)
371 && prototype_p (ntype)
372 && lookup_attribute ("format", attrs))
374 warning_at (input_location, OPT_Wattributes,
375 "%qs attribute cannot be applied to a function that "
376 "does not take variable arguments", "format");
377 attrs = remove_attribute ("format", attrs);
379 return build_type_attribute_variant (ntype, attrs);
382 /* Return the composite type of two compatible types.
384 We assume that comptypes has already been done and returned
385 nonzero; if that isn't so, this may crash. In particular, we
386 assume that qualifiers match. */
389 composite_type (tree t1, tree t2)
391 enum tree_code code1;
392 enum tree_code code2;
395 /* Save time if the two types are the same. */
397 if (t1 == t2) return t1;
399 /* If one type is nonsense, use the other. */
400 if (t1 == error_mark_node)
402 if (t2 == error_mark_node)
405 code1 = TREE_CODE (t1);
406 code2 = TREE_CODE (t2);
408 /* Merge the attributes. */
409 attributes = targetm.merge_type_attributes (t1, t2);
411 /* If one is an enumerated type and the other is the compatible
412 integer type, the composite type might be either of the two
413 (DR#013 question 3). For consistency, use the enumerated type as
414 the composite type. */
416 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
418 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
421 gcc_assert (code1 == code2);
426 /* For two pointers, do this recursively on the target type. */
428 tree pointed_to_1 = TREE_TYPE (t1);
429 tree pointed_to_2 = TREE_TYPE (t2);
430 tree target = composite_type (pointed_to_1, pointed_to_2);
431 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
432 t1 = build_type_attribute_variant (t1, attributes);
433 return qualify_type (t1, t2);
438 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
441 tree d1 = TYPE_DOMAIN (t1);
442 tree d2 = TYPE_DOMAIN (t2);
443 bool d1_variable, d2_variable;
444 bool d1_zero, d2_zero;
445 bool t1_complete, t2_complete;
447 /* We should not have any type quals on arrays at all. */
448 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
449 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
451 t1_complete = COMPLETE_TYPE_P (t1);
452 t2_complete = COMPLETE_TYPE_P (t2);
454 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
455 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
457 d1_variable = (!d1_zero
458 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
459 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
460 d2_variable = (!d2_zero
461 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
462 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
463 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
464 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
466 /* Save space: see if the result is identical to one of the args. */
467 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
468 && (d2_variable || d2_zero || !d1_variable))
469 return build_type_attribute_variant (t1, attributes);
470 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
471 && (d1_variable || d1_zero || !d2_variable))
472 return build_type_attribute_variant (t2, attributes);
474 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
475 return build_type_attribute_variant (t1, attributes);
476 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
477 return build_type_attribute_variant (t2, attributes);
479 /* Merge the element types, and have a size if either arg has
480 one. We may have qualifiers on the element types. To set
481 up TYPE_MAIN_VARIANT correctly, we need to form the
482 composite of the unqualified types and add the qualifiers
484 quals = TYPE_QUALS (strip_array_types (elt));
485 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
486 t1 = build_array_type (unqual_elt,
487 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
493 /* Ensure a composite type involving a zero-length array type
494 is a zero-length type not an incomplete type. */
495 if (d1_zero && d2_zero
496 && (t1_complete || t2_complete)
497 && !COMPLETE_TYPE_P (t1))
499 TYPE_SIZE (t1) = bitsize_zero_node;
500 TYPE_SIZE_UNIT (t1) = size_zero_node;
502 t1 = c_build_qualified_type (t1, quals);
503 return build_type_attribute_variant (t1, attributes);
509 if (attributes != NULL)
511 /* Try harder not to create a new aggregate type. */
512 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
514 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
517 return build_type_attribute_variant (t1, attributes);
520 /* Function types: prefer the one that specified arg types.
521 If both do, merge the arg types. Also merge the return types. */
523 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
524 tree p1 = TYPE_ARG_TYPES (t1);
525 tree p2 = TYPE_ARG_TYPES (t2);
530 /* Save space: see if the result is identical to one of the args. */
531 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
532 return build_functype_attribute_variant (t1, t2, attributes);
533 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
534 return build_functype_attribute_variant (t2, t1, attributes);
536 /* Simple way if one arg fails to specify argument types. */
537 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
539 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
540 t1 = build_type_attribute_variant (t1, attributes);
541 return qualify_type (t1, t2);
543 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
545 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
546 t1 = build_type_attribute_variant (t1, attributes);
547 return qualify_type (t1, t2);
550 /* If both args specify argument types, we must merge the two
551 lists, argument by argument. */
553 for (len = 0, newargs = p1;
554 newargs && newargs != void_list_node;
555 len++, newargs = TREE_CHAIN (newargs))
558 for (i = 0; i < len; i++)
559 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
563 for (; p1 && p1 != void_list_node;
564 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
566 /* A null type means arg type is not specified.
567 Take whatever the other function type has. */
568 if (TREE_VALUE (p1) == NULL_TREE)
570 TREE_VALUE (n) = TREE_VALUE (p2);
573 if (TREE_VALUE (p2) == NULL_TREE)
575 TREE_VALUE (n) = TREE_VALUE (p1);
579 /* Given wait (union {union wait *u; int *i} *)
580 and wait (union wait *),
581 prefer union wait * as type of parm. */
582 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
583 && TREE_VALUE (p1) != TREE_VALUE (p2))
586 tree mv2 = TREE_VALUE (p2);
587 if (mv2 && mv2 != error_mark_node
588 && TREE_CODE (mv2) != ARRAY_TYPE)
589 mv2 = TYPE_MAIN_VARIANT (mv2);
590 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
591 memb; memb = DECL_CHAIN (memb))
593 tree mv3 = TREE_TYPE (memb);
594 if (mv3 && mv3 != error_mark_node
595 && TREE_CODE (mv3) != ARRAY_TYPE)
596 mv3 = TYPE_MAIN_VARIANT (mv3);
597 if (comptypes (mv3, mv2))
599 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
601 pedwarn (input_location, OPT_Wpedantic,
602 "function types not truly compatible in ISO C");
607 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
608 && TREE_VALUE (p2) != TREE_VALUE (p1))
611 tree mv1 = TREE_VALUE (p1);
612 if (mv1 && mv1 != error_mark_node
613 && TREE_CODE (mv1) != ARRAY_TYPE)
614 mv1 = TYPE_MAIN_VARIANT (mv1);
615 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
616 memb; memb = DECL_CHAIN (memb))
618 tree mv3 = TREE_TYPE (memb);
619 if (mv3 && mv3 != error_mark_node
620 && TREE_CODE (mv3) != ARRAY_TYPE)
621 mv3 = TYPE_MAIN_VARIANT (mv3);
622 if (comptypes (mv3, mv1))
624 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
626 pedwarn (input_location, OPT_Wpedantic,
627 "function types not truly compatible in ISO C");
632 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
636 t1 = build_function_type (valtype, newargs);
637 t1 = qualify_type (t1, t2);
642 return build_type_attribute_variant (t1, attributes);
647 /* Return the type of a conditional expression between pointers to
648 possibly differently qualified versions of compatible types.
650 We assume that comp_target_types has already been done and returned
651 nonzero; if that isn't so, this may crash. */
654 common_pointer_type (tree t1, tree t2)
657 tree pointed_to_1, mv1;
658 tree pointed_to_2, mv2;
660 unsigned target_quals;
661 addr_space_t as1, as2, as_common;
664 /* Save time if the two types are the same. */
666 if (t1 == t2) return t1;
668 /* If one type is nonsense, use the other. */
669 if (t1 == error_mark_node)
671 if (t2 == error_mark_node)
674 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
675 && TREE_CODE (t2) == POINTER_TYPE);
677 /* Merge the attributes. */
678 attributes = targetm.merge_type_attributes (t1, t2);
680 /* Find the composite type of the target types, and combine the
681 qualifiers of the two types' targets. Do not lose qualifiers on
682 array element types by taking the TYPE_MAIN_VARIANT. */
683 mv1 = pointed_to_1 = TREE_TYPE (t1);
684 mv2 = pointed_to_2 = TREE_TYPE (t2);
685 if (TREE_CODE (mv1) != ARRAY_TYPE)
686 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
687 if (TREE_CODE (mv2) != ARRAY_TYPE)
688 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
689 target = composite_type (mv1, mv2);
691 /* Strip array types to get correct qualifier for pointers to arrays */
692 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
693 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
695 /* For function types do not merge const qualifiers, but drop them
696 if used inconsistently. The middle-end uses these to mark const
697 and noreturn functions. */
698 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
699 target_quals = (quals1 & quals2);
701 target_quals = (quals1 | quals2);
703 /* If the two named address spaces are different, determine the common
704 superset address space. This is guaranteed to exist due to the
705 assumption that comp_target_type returned non-zero. */
706 as1 = TYPE_ADDR_SPACE (pointed_to_1);
707 as2 = TYPE_ADDR_SPACE (pointed_to_2);
708 if (!addr_space_superset (as1, as2, &as_common))
711 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
713 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
714 return build_type_attribute_variant (t1, attributes);
717 /* Return the common type for two arithmetic types under the usual
718 arithmetic conversions. The default conversions have already been
719 applied, and enumerated types converted to their compatible integer
720 types. The resulting type is unqualified and has no attributes.
722 This is the type for the result of most arithmetic operations
723 if the operands have the given two types. */
726 c_common_type (tree t1, tree t2)
728 enum tree_code code1;
729 enum tree_code code2;
731 /* If one type is nonsense, use the other. */
732 if (t1 == error_mark_node)
734 if (t2 == error_mark_node)
737 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
738 t1 = TYPE_MAIN_VARIANT (t1);
740 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
741 t2 = TYPE_MAIN_VARIANT (t2);
743 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
745 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
746 t1 = build_type_attribute_variant (t1, attrs);
749 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
751 tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
752 t2 = build_type_attribute_variant (t2, attrs);
755 /* Save time if the two types are the same. */
757 if (t1 == t2) return t1;
759 code1 = TREE_CODE (t1);
760 code2 = TREE_CODE (t2);
762 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
763 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
764 || code1 == INTEGER_TYPE);
765 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
766 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
767 || code2 == INTEGER_TYPE);
769 /* When one operand is a decimal float type, the other operand cannot be
770 a generic float type or a complex type. We also disallow vector types
772 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
773 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
775 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
777 error ("cannot mix operands of decimal floating and vector types");
778 return error_mark_node;
780 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
782 error ("cannot mix operands of decimal floating and complex types");
783 return error_mark_node;
785 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
787 error ("cannot mix operands of decimal floating "
788 "and other floating types");
789 return error_mark_node;
793 /* If one type is a vector type, return that type. (How the usual
794 arithmetic conversions apply to the vector types extension is not
795 precisely specified.) */
796 if (code1 == VECTOR_TYPE)
799 if (code2 == VECTOR_TYPE)
802 /* If one type is complex, form the common type of the non-complex
803 components, then make that complex. Use T1 or T2 if it is the
805 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
807 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
808 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
809 tree subtype = c_common_type (subtype1, subtype2);
811 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
813 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
816 return build_complex_type (subtype);
819 /* If only one is real, use it as the result. */
821 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
824 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
827 /* If both are real and either are decimal floating point types, use
828 the decimal floating point type with the greater precision. */
830 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
832 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
833 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
834 return dfloat128_type_node;
835 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
836 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
837 return dfloat64_type_node;
838 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
839 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
840 return dfloat32_type_node;
843 /* Deal with fixed-point types. */
844 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
846 unsigned int unsignedp = 0, satp = 0;
848 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
850 m1 = SCALAR_TYPE_MODE (t1);
851 m2 = SCALAR_TYPE_MODE (t2);
853 /* If one input type is saturating, the result type is saturating. */
854 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
857 /* If both fixed-point types are unsigned, the result type is unsigned.
858 When mixing fixed-point and integer types, follow the sign of the
860 Otherwise, the result type is signed. */
861 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
862 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
863 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
864 && TYPE_UNSIGNED (t1))
865 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
866 && TYPE_UNSIGNED (t2)))
869 /* The result type is signed. */
872 /* If the input type is unsigned, we need to convert to the
874 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
876 enum mode_class mclass = (enum mode_class) 0;
877 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
879 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
883 m1 = as_a <scalar_mode>
884 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
886 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
888 enum mode_class mclass = (enum mode_class) 0;
889 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
891 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
895 m2 = as_a <scalar_mode>
896 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
900 if (code1 == FIXED_POINT_TYPE)
902 fbit1 = GET_MODE_FBIT (m1);
903 ibit1 = GET_MODE_IBIT (m1);
908 /* Signed integers need to subtract one sign bit. */
909 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
912 if (code2 == FIXED_POINT_TYPE)
914 fbit2 = GET_MODE_FBIT (m2);
915 ibit2 = GET_MODE_IBIT (m2);
920 /* Signed integers need to subtract one sign bit. */
921 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
924 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
925 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
926 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
930 /* Both real or both integers; use the one with greater precision. */
932 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
934 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
937 /* Same precision. Prefer long longs to longs to ints when the
938 same precision, following the C99 rules on integer type rank
939 (which are equivalent to the C90 rules for C90 types). */
941 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
942 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
943 return long_long_unsigned_type_node;
945 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
946 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
948 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
949 return long_long_unsigned_type_node;
951 return long_long_integer_type_node;
954 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
955 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
956 return long_unsigned_type_node;
958 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
959 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
961 /* But preserve unsignedness from the other type,
962 since long cannot hold all the values of an unsigned int. */
963 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
964 return long_unsigned_type_node;
966 return long_integer_type_node;
969 /* For floating types of the same TYPE_PRECISION (which we here
970 assume means either the same set of values, or sets of values
971 neither a subset of the other, with behavior being undefined in
972 the latter case), follow the rules from TS 18661-3: prefer
973 interchange types _FloatN, then standard types long double,
974 double, float, then extended types _FloatNx. For extended types,
975 check them starting with _Float128x as that seems most consistent
976 in spirit with preferring long double to double; for interchange
977 types, also check in that order for consistency although it's not
978 possible for more than one of them to have the same
980 tree mv1 = TYPE_MAIN_VARIANT (t1);
981 tree mv2 = TYPE_MAIN_VARIANT (t2);
983 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
984 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
985 return FLOATN_TYPE_NODE (i);
987 /* Likewise, prefer long double to double even if same size. */
988 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
989 return long_double_type_node;
991 /* Likewise, prefer double to float even if same size.
992 We got a couple of embedded targets with 32 bit doubles, and the
993 pdp11 might have 64 bit floats. */
994 if (mv1 == double_type_node || mv2 == double_type_node)
995 return double_type_node;
997 if (mv1 == float_type_node || mv2 == float_type_node)
998 return float_type_node;
1000 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1001 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1002 return FLOATNX_TYPE_NODE (i);
1004 /* Otherwise prefer the unsigned one. */
1006 if (TYPE_UNSIGNED (t1))
1012 /* Wrapper around c_common_type that is used by c-common.cc and other
1013 front end optimizations that remove promotions. ENUMERAL_TYPEs
1014 are allowed here and are converted to their compatible integer types.
1015 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1016 preferably a non-Boolean type as the common type. */
1018 common_type (tree t1, tree t2)
1020 if (TREE_CODE (t1) == ENUMERAL_TYPE)
1021 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1022 if (TREE_CODE (t2) == ENUMERAL_TYPE)
1023 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1025 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1026 if (TREE_CODE (t1) == BOOLEAN_TYPE
1027 && TREE_CODE (t2) == BOOLEAN_TYPE)
1028 return boolean_type_node;
1030 /* If either type is BOOLEAN_TYPE, then return the other. */
1031 if (TREE_CODE (t1) == BOOLEAN_TYPE)
1033 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1036 return c_common_type (t1, t2);
1039 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1040 or various other operations. Return 2 if they are compatible
1041 but a warning may be needed if you use them together. */
1044 comptypes (tree type1, tree type2)
1046 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1049 val = comptypes_internal (type1, type2, NULL, NULL);
1050 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1055 /* Like comptypes, but if it returns non-zero because enum and int are
1056 compatible, it sets *ENUM_AND_INT_P to true. */
1059 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1061 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1064 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1065 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1070 /* Like comptypes, but if it returns nonzero for different types, it
1071 sets *DIFFERENT_TYPES_P to true. */
1074 comptypes_check_different_types (tree type1, tree type2,
1075 bool *different_types_p)
1077 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1080 val = comptypes_internal (type1, type2, NULL, different_types_p);
1081 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1086 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1087 or various other operations. Return 2 if they are compatible
1088 but a warning may be needed if you use them together. If
1089 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1090 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1091 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1092 NULL, and the types are compatible but different enough not to be
1093 permitted in C11 typedef redeclarations, then this sets
1094 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1095 false, but may or may not be set if the types are incompatible.
1096 This differs from comptypes, in that we don't free the seen
1100 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1101 bool *different_types_p)
1103 const_tree t1 = type1;
1104 const_tree t2 = type2;
1107 /* Suppress errors caused by previously reported errors. */
1109 if (t1 == t2 || !t1 || !t2
1110 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1113 /* Enumerated types are compatible with integer types, but this is
1114 not transitive: two enumerated types in the same translation unit
1115 are compatible with each other only if they are the same type. */
1117 if (TREE_CODE (t1) == ENUMERAL_TYPE
1118 && COMPLETE_TYPE_P (t1)
1119 && TREE_CODE (t2) != ENUMERAL_TYPE)
1121 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1122 if (TREE_CODE (t2) != VOID_TYPE)
1124 if (enum_and_int_p != NULL)
1125 *enum_and_int_p = true;
1126 if (different_types_p != NULL)
1127 *different_types_p = true;
1130 else if (TREE_CODE (t2) == ENUMERAL_TYPE
1131 && COMPLETE_TYPE_P (t2)
1132 && TREE_CODE (t1) != ENUMERAL_TYPE)
1134 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1135 if (TREE_CODE (t1) != 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;
1147 /* Different classes of types can't be compatible. */
1149 if (TREE_CODE (t1) != TREE_CODE (t2))
1152 /* Qualifiers must match. C99 6.7.3p9 */
1154 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1157 /* Allow for two different type nodes which have essentially the same
1158 definition. Note that we already checked for equality of the type
1159 qualifiers (just above). */
1161 if (TREE_CODE (t1) != ARRAY_TYPE
1162 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1165 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1166 if (!(attrval = comp_type_attributes (t1, t2)))
1169 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1172 switch (TREE_CODE (t1))
1175 case FIXED_POINT_TYPE:
1177 /* With these nodes, we can't determine type equivalence by
1178 looking at what is stored in the nodes themselves, because
1179 two nodes might have different TYPE_MAIN_VARIANTs but still
1180 represent the same type. For example, wchar_t and int could
1181 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1182 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1183 and are distinct types. On the other hand, int and the
1186 typedef int INT __attribute((may_alias));
1188 have identical properties, different TYPE_MAIN_VARIANTs, but
1189 represent the same type. The canonical type system keeps
1190 track of equivalence in this case, so we fall back on it. */
1191 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1194 /* Do not remove mode information. */
1195 if (TYPE_MODE (t1) != TYPE_MODE (t2))
1197 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1198 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1199 enum_and_int_p, different_types_p));
1203 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1209 tree d1 = TYPE_DOMAIN (t1);
1210 tree d2 = TYPE_DOMAIN (t2);
1211 bool d1_variable, d2_variable;
1212 bool d1_zero, d2_zero;
1215 /* Target types must match incl. qualifiers. */
1216 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1217 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1219 different_types_p)) == 0)
1222 if (different_types_p != NULL
1223 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1224 *different_types_p = true;
1225 /* Sizes must match unless one is missing or variable. */
1226 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1229 d1_zero = !TYPE_MAX_VALUE (d1);
1230 d2_zero = !TYPE_MAX_VALUE (d2);
1232 d1_variable = (!d1_zero
1233 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1234 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1235 d2_variable = (!d2_zero
1236 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1237 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1238 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1239 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1241 if (different_types_p != NULL
1242 && d1_variable != d2_variable)
1243 *different_types_p = true;
1244 if (d1_variable || d2_variable)
1246 if (d1_zero && d2_zero)
1248 if (d1_zero || d2_zero
1249 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1250 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1259 if (val != 1 && !same_translation_unit_p (t1, t2))
1261 tree a1 = TYPE_ATTRIBUTES (t1);
1262 tree a2 = TYPE_ATTRIBUTES (t2);
1264 if (! attribute_list_contained (a1, a2)
1265 && ! attribute_list_contained (a2, a1))
1269 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1271 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1277 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1278 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1279 enum_and_int_p, different_types_p));
1285 return attrval == 2 && val == 1 ? 2 : val;
1288 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1289 their qualifiers, except for named address spaces. If the pointers point to
1290 different named addresses, then we must determine if one address space is a
1291 subset of the other. */
1294 comp_target_types (location_t location, tree ttl, tree ttr)
1298 tree mvl = TREE_TYPE (ttl);
1299 tree mvr = TREE_TYPE (ttr);
1300 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1301 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1302 addr_space_t as_common;
1303 bool enum_and_int_p;
1305 /* Fail if pointers point to incompatible address spaces. */
1306 if (!addr_space_superset (asl, asr, &as_common))
1309 /* For pedantic record result of comptypes on arrays before losing
1310 qualifiers on the element type below. */
1313 if (TREE_CODE (mvl) == ARRAY_TYPE
1314 && TREE_CODE (mvr) == ARRAY_TYPE)
1315 val_ped = comptypes (mvl, mvr);
1317 /* Qualifiers on element types of array types that are
1318 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1320 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1321 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1322 : TYPE_MAIN_VARIANT (mvl));
1324 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1325 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1326 : TYPE_MAIN_VARIANT (mvr));
1328 enum_and_int_p = false;
1329 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1331 if (val == 1 && val_ped != 1)
1332 pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1333 "in ISO C before C2X");
1336 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1338 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1339 warning_at (location, OPT_Wc___compat,
1340 "pointer target types incompatible in C++");
1345 /* Subroutines of `comptypes'. */
1347 /* Determine whether two trees derive from the same translation unit.
1348 If the CONTEXT chain ends in a null, that tree's context is still
1349 being parsed, so if two trees have context chains ending in null,
1350 they're in the same translation unit. */
1353 same_translation_unit_p (const_tree t1, const_tree t2)
1355 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1356 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1358 case tcc_declaration:
1359 t1 = DECL_CONTEXT (t1); break;
1361 t1 = TYPE_CONTEXT (t1); break;
1362 case tcc_exceptional:
1363 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1364 default: gcc_unreachable ();
1367 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1368 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1370 case tcc_declaration:
1371 t2 = DECL_CONTEXT (t2); break;
1373 t2 = TYPE_CONTEXT (t2); break;
1374 case tcc_exceptional:
1375 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1376 default: gcc_unreachable ();
1382 /* Allocate the seen two types, assuming that they are compatible. */
1384 static struct tagged_tu_seen_cache *
1385 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1387 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1388 tu->next = tagged_tu_seen_base;
1392 tagged_tu_seen_base = tu;
1394 /* The C standard says that two structures in different translation
1395 units are compatible with each other only if the types of their
1396 fields are compatible (among other things). We assume that they
1397 are compatible until proven otherwise when building the cache.
1398 An example where this can occur is:
1403 If we are comparing this against a similar struct in another TU,
1404 and did not assume they were compatible, we end up with an infinite
1410 /* Free the seen types until we get to TU_TIL. */
1413 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1415 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1416 while (tu != tu_til)
1418 const struct tagged_tu_seen_cache *const tu1
1419 = (const struct tagged_tu_seen_cache *) tu;
1421 XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1423 tagged_tu_seen_base = tu_til;
1426 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1427 compatible. If the two types are not the same (which has been
1428 checked earlier), this can only happen when multiple translation
1429 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1430 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1431 comptypes_internal. */
1434 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1435 bool *enum_and_int_p, bool *different_types_p)
1438 bool needs_warning = false;
1440 /* We have to verify that the tags of the types are the same. This
1441 is harder than it looks because this may be a typedef, so we have
1442 to go look at the original type. It may even be a typedef of a
1444 In the case of compiler-created builtin structs the TYPE_DECL
1445 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1446 while (TYPE_NAME (t1)
1447 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1448 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1449 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1451 while (TYPE_NAME (t2)
1452 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1453 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1454 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1456 /* C90 didn't have the requirement that the two tags be the same. */
1457 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1460 /* C90 didn't say what happened if one or both of the types were
1461 incomplete; we choose to follow C99 rules here, which is that they
1463 if (TYPE_SIZE (t1) == NULL
1464 || TYPE_SIZE (t2) == NULL)
1468 const struct tagged_tu_seen_cache * tts_i;
1469 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1470 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1474 switch (TREE_CODE (t1))
1478 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1479 /* Speed up the case where the type values are in the same order. */
1480 tree tv1 = TYPE_VALUES (t1);
1481 tree tv2 = TYPE_VALUES (t2);
1488 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1490 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1492 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1499 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1503 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1509 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1515 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1517 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1519 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1530 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1531 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1537 /* Speed up the common case where the fields are in the same order. */
1538 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1539 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1543 if (DECL_NAME (s1) != DECL_NAME (s2))
1545 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1546 enum_and_int_p, different_types_p);
1548 if (result != 1 && !DECL_NAME (s1))
1556 needs_warning = true;
1558 if (TREE_CODE (s1) == FIELD_DECL
1559 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1560 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1568 tu->val = needs_warning ? 2 : 1;
1572 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1576 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1577 if (DECL_NAME (s1) == DECL_NAME (s2))
1581 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1585 if (result != 1 && !DECL_NAME (s1))
1593 needs_warning = true;
1595 if (TREE_CODE (s1) == FIELD_DECL
1596 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1597 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1609 tu->val = needs_warning ? 2 : 10;
1615 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1617 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1619 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1622 if (TREE_CODE (s1) != TREE_CODE (s2)
1623 || DECL_NAME (s1) != DECL_NAME (s2))
1625 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1626 enum_and_int_p, different_types_p);
1630 needs_warning = true;
1632 if (TREE_CODE (s1) == FIELD_DECL
1633 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1634 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1640 tu->val = needs_warning ? 2 : 1;
1649 /* Return 1 if two function types F1 and F2 are compatible.
1650 If either type specifies no argument types,
1651 the other must specify a fixed number of self-promoting arg types.
1652 Otherwise, if one type specifies only the number of arguments,
1653 the other must specify that number of self-promoting arg types.
1654 Otherwise, the argument types must match.
1655 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1658 function_types_compatible_p (const_tree f1, const_tree f2,
1659 bool *enum_and_int_p, bool *different_types_p)
1662 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1667 ret1 = TREE_TYPE (f1);
1668 ret2 = TREE_TYPE (f2);
1670 /* 'volatile' qualifiers on a function's return type used to mean
1671 the function is noreturn. */
1672 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1673 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1674 if (TYPE_VOLATILE (ret1))
1675 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1676 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1677 if (TYPE_VOLATILE (ret2))
1678 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1679 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1680 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1684 args1 = TYPE_ARG_TYPES (f1);
1685 args2 = TYPE_ARG_TYPES (f2);
1687 if (different_types_p != NULL
1688 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1689 *different_types_p = true;
1691 /* An unspecified parmlist matches any specified parmlist
1692 whose argument types don't need default promotions. */
1694 if (args1 == NULL_TREE)
1696 if (!self_promoting_args_p (args2))
1698 /* If one of these types comes from a non-prototype fn definition,
1699 compare that with the other type's arglist.
1700 If they don't match, ask for a warning (but no error). */
1701 if (TYPE_ACTUAL_ARG_TYPES (f1)
1702 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1703 enum_and_int_p, different_types_p) != 1)
1707 if (args2 == NULL_TREE)
1709 if (!self_promoting_args_p (args1))
1711 if (TYPE_ACTUAL_ARG_TYPES (f2)
1712 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1713 enum_and_int_p, different_types_p) != 1)
1718 /* Both types have argument lists: compare them and propagate results. */
1719 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1721 return val1 != 1 ? val1 : val;
1724 /* Check two lists of types for compatibility, returning 0 for
1725 incompatible, 1 for compatible, or 2 for compatible with
1726 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1727 comptypes_internal. */
1730 type_lists_compatible_p (const_tree args1, const_tree args2,
1731 bool *enum_and_int_p, bool *different_types_p)
1733 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1739 tree a1, mv1, a2, mv2;
1740 if (args1 == NULL_TREE && args2 == NULL_TREE)
1742 /* If one list is shorter than the other,
1743 they fail to match. */
1744 if (args1 == NULL_TREE || args2 == NULL_TREE)
1746 mv1 = a1 = TREE_VALUE (args1);
1747 mv2 = a2 = TREE_VALUE (args2);
1748 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1749 mv1 = (TYPE_ATOMIC (mv1)
1750 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1752 : TYPE_MAIN_VARIANT (mv1));
1753 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1754 mv2 = (TYPE_ATOMIC (mv2)
1755 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1757 : TYPE_MAIN_VARIANT (mv2));
1758 /* A null pointer instead of a type
1759 means there is supposed to be an argument
1760 but nothing is specified about what type it has.
1761 So match anything that self-promotes. */
1762 if (different_types_p != NULL
1763 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1764 *different_types_p = true;
1765 if (a1 == NULL_TREE)
1767 if (c_type_promotes_to (a2) != a2)
1770 else if (a2 == NULL_TREE)
1772 if (c_type_promotes_to (a1) != a1)
1775 /* If one of the lists has an error marker, ignore this arg. */
1776 else if (TREE_CODE (a1) == ERROR_MARK
1777 || TREE_CODE (a2) == ERROR_MARK)
1779 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1780 different_types_p)))
1782 if (different_types_p != NULL)
1783 *different_types_p = true;
1784 /* Allow wait (union {union wait *u; int *i} *)
1785 and wait (union wait *) to be compatible. */
1786 if (TREE_CODE (a1) == UNION_TYPE
1787 && (TYPE_NAME (a1) == NULL_TREE
1788 || TYPE_TRANSPARENT_AGGR (a1))
1789 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1790 && tree_int_cst_equal (TYPE_SIZE (a1),
1794 for (memb = TYPE_FIELDS (a1);
1795 memb; memb = DECL_CHAIN (memb))
1797 tree mv3 = TREE_TYPE (memb);
1798 if (mv3 && mv3 != error_mark_node
1799 && TREE_CODE (mv3) != ARRAY_TYPE)
1800 mv3 = (TYPE_ATOMIC (mv3)
1801 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1803 : TYPE_MAIN_VARIANT (mv3));
1804 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1808 if (memb == NULL_TREE)
1811 else if (TREE_CODE (a2) == UNION_TYPE
1812 && (TYPE_NAME (a2) == NULL_TREE
1813 || TYPE_TRANSPARENT_AGGR (a2))
1814 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1815 && tree_int_cst_equal (TYPE_SIZE (a2),
1819 for (memb = TYPE_FIELDS (a2);
1820 memb; memb = DECL_CHAIN (memb))
1822 tree mv3 = TREE_TYPE (memb);
1823 if (mv3 && mv3 != error_mark_node
1824 && TREE_CODE (mv3) != ARRAY_TYPE)
1825 mv3 = (TYPE_ATOMIC (mv3)
1826 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1828 : TYPE_MAIN_VARIANT (mv3));
1829 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1833 if (memb == NULL_TREE)
1840 /* comptypes said ok, but record if it said to warn. */
1844 args1 = TREE_CHAIN (args1);
1845 args2 = TREE_CHAIN (args2);
1849 /* Compute the size to increment a pointer by. When a function type or void
1850 type or incomplete type is passed, size_one_node is returned.
1851 This function does not emit any diagnostics; the caller is responsible
1855 c_size_in_bytes (const_tree type)
1857 enum tree_code code = TREE_CODE (type);
1859 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1860 || !COMPLETE_TYPE_P (type))
1861 return size_one_node;
1863 /* Convert in case a char is more than one unit. */
1864 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1865 size_int (TYPE_PRECISION (char_type_node)
1869 /* Return either DECL or its known constant value (if it has one). */
1872 decl_constant_value_1 (tree decl, bool in_init)
1874 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1875 TREE_CODE (decl) != PARM_DECL
1876 && !TREE_THIS_VOLATILE (decl)
1877 && TREE_READONLY (decl)
1878 && DECL_INITIAL (decl) != NULL_TREE
1879 && !error_operand_p (DECL_INITIAL (decl))
1880 /* This is invalid if initial value is not constant.
1881 If it has either a function call, a memory reference,
1882 or a variable, then re-evaluating it could give different results. */
1883 && TREE_CONSTANT (DECL_INITIAL (decl))
1884 /* Check for cases where this is sub-optimal, even though valid. */
1885 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1886 return DECL_INITIAL (decl);
1890 /* Return either DECL or its known constant value (if it has one).
1891 Like the above, but always return decl outside of functions. */
1894 decl_constant_value (tree decl)
1896 /* Don't change a variable array bound or initial value to a constant
1897 in a place where a variable is invalid. */
1898 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1901 /* Convert the array expression EXP to a pointer. */
1903 array_to_pointer_conversion (location_t loc, tree exp)
1905 tree orig_exp = exp;
1906 tree type = TREE_TYPE (exp);
1908 tree restype = TREE_TYPE (type);
1911 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1913 STRIP_TYPE_NOPS (exp);
1915 copy_warning (exp, orig_exp);
1917 ptrtype = build_pointer_type (restype);
1919 if (INDIRECT_REF_P (exp))
1920 return convert (ptrtype, TREE_OPERAND (exp, 0));
1922 /* In C++ array compound literals are temporary objects unless they are
1923 const or appear in namespace scope, so they are destroyed too soon
1924 to use them for much of anything (c++/53220). */
1925 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1927 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1928 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1929 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1930 "converting an array compound literal to a pointer "
1931 "is ill-formed in C++");
1934 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1935 return convert (ptrtype, adr);
1938 /* Convert the function expression EXP to a pointer. */
1940 function_to_pointer_conversion (location_t loc, tree exp)
1942 tree orig_exp = exp;
1944 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1946 STRIP_TYPE_NOPS (exp);
1948 copy_warning (exp, orig_exp);
1950 return build_unary_op (loc, ADDR_EXPR, exp, false);
1953 /* Mark EXP as read, not just set, for set but not used -Wunused
1954 warning purposes. */
1957 mark_exp_read (tree exp)
1959 switch (TREE_CODE (exp))
1963 DECL_READ_P (exp) = 1;
1972 case VIEW_CONVERT_EXPR:
1973 mark_exp_read (TREE_OPERAND (exp, 0));
1976 /* Pattern match what build_atomic_assign produces with modifycode
1978 if (VAR_P (TREE_OPERAND (exp, 1))
1979 && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1980 && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1982 tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1983 tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1984 if (TREE_CODE (t1) == TARGET_EXPR
1985 && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1986 && TREE_CODE (t2) == CALL_EXPR)
1988 tree fndecl = get_callee_fndecl (t2);
1989 tree arg = NULL_TREE;
1991 && TREE_CODE (fndecl) == FUNCTION_DECL
1992 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1993 && call_expr_nargs (t2) >= 2)
1994 switch (DECL_FUNCTION_CODE (fndecl))
1996 case BUILT_IN_ATOMIC_STORE:
1997 arg = CALL_EXPR_ARG (t2, 1);
1999 case BUILT_IN_ATOMIC_STORE_1:
2000 case BUILT_IN_ATOMIC_STORE_2:
2001 case BUILT_IN_ATOMIC_STORE_4:
2002 case BUILT_IN_ATOMIC_STORE_8:
2003 case BUILT_IN_ATOMIC_STORE_16:
2004 arg = CALL_EXPR_ARG (t2, 0);
2012 if (TREE_CODE (arg) == ADDR_EXPR
2013 && DECL_P (TREE_OPERAND (arg, 0))
2014 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2015 mark_exp_read (TREE_OPERAND (arg, 0));
2020 case C_MAYBE_CONST_EXPR:
2021 mark_exp_read (TREE_OPERAND (exp, 1));
2028 /* Perform the default conversion of arrays and functions to pointers.
2029 Return the result of converting EXP. For any other expression, just
2032 LOC is the location of the expression. */
2035 default_function_array_conversion (location_t loc, struct c_expr exp)
2037 tree orig_exp = exp.value;
2038 tree type = TREE_TYPE (exp.value);
2039 enum tree_code code = TREE_CODE (type);
2045 bool not_lvalue = false;
2046 bool lvalue_array_p;
2048 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2049 || CONVERT_EXPR_P (exp.value))
2050 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2052 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2054 exp.value = TREE_OPERAND (exp.value, 0);
2057 copy_warning (exp.value, orig_exp);
2059 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2060 if (!flag_isoc99 && !lvalue_array_p)
2062 /* Before C99, non-lvalue arrays do not decay to pointers.
2063 Normally, using such an array would be invalid; but it can
2064 be used correctly inside sizeof or as a statement expression.
2065 Thus, do not give an error here; an error will result later. */
2069 exp.value = array_to_pointer_conversion (loc, exp.value);
2073 exp.value = function_to_pointer_conversion (loc, exp.value);
2083 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2085 mark_exp_read (exp.value);
2086 return default_function_array_conversion (loc, exp);
2089 /* Return whether EXPR should be treated as an atomic lvalue for the
2090 purposes of load and store handling. */
2093 really_atomic_lvalue (tree expr)
2095 if (error_operand_p (expr))
2097 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2099 if (!lvalue_p (expr))
2102 /* Ignore _Atomic on register variables, since their addresses can't
2103 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2104 sequences wouldn't work. Ignore _Atomic on structures containing
2105 bit-fields, since accessing elements of atomic structures or
2106 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2107 it's undefined at translation time or execution time, and the
2108 normal atomic sequences again wouldn't work. */
2109 while (handled_component_p (expr))
2111 if (TREE_CODE (expr) == COMPONENT_REF
2112 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2114 expr = TREE_OPERAND (expr, 0);
2116 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2121 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2122 including converting functions and arrays to pointers if CONVERT_P.
2123 If READ_P, also mark the expression as having been read. */
2126 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2127 bool convert_p, bool read_p)
2130 mark_exp_read (exp.value);
2132 exp = default_function_array_conversion (loc, exp);
2133 if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2134 exp.value = require_complete_type (loc, exp.value);
2135 if (really_atomic_lvalue (exp.value))
2137 vec<tree, va_gc> *params;
2138 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2139 tree expr_type = TREE_TYPE (exp.value);
2140 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2141 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2143 gcc_assert (TYPE_ATOMIC (expr_type));
2145 /* Expansion of a generic atomic load may require an addition
2146 element, so allocate enough to prevent a resize. */
2147 vec_alloc (params, 4);
2149 /* Remove the qualifiers for the rest of the expressions and
2150 create the VAL temp variable to hold the RHS. */
2151 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2152 tmp = create_tmp_var_raw (nonatomic_type);
2153 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2154 TREE_ADDRESSABLE (tmp) = 1;
2155 /* Do not disable warnings for TMP even though it's artificial.
2156 -Winvalid-memory-model depends on it. */
2158 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2159 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2160 params->quick_push (expr_addr);
2161 params->quick_push (tmp_addr);
2162 params->quick_push (seq_cst);
2163 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2165 /* EXPR is always read. */
2166 mark_exp_read (exp.value);
2168 /* Return tmp which contains the value loaded. */
2169 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2170 NULL_TREE, NULL_TREE);
2172 if (convert_p && !error_operand_p (exp.value)
2173 && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2174 exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2178 /* EXP is an expression of integer type. Apply the integer promotions
2179 to it and return the promoted value. */
2182 perform_integral_promotions (tree exp)
2184 tree type = TREE_TYPE (exp);
2185 enum tree_code code = TREE_CODE (type);
2187 gcc_assert (INTEGRAL_TYPE_P (type));
2189 /* Normally convert enums to int,
2190 but convert wide enums to something wider. */
2191 if (code == ENUMERAL_TYPE)
2193 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2194 TYPE_PRECISION (integer_type_node)),
2195 ((TYPE_PRECISION (type)
2196 >= TYPE_PRECISION (integer_type_node))
2197 && TYPE_UNSIGNED (type)));
2199 return convert (type, exp);
2202 /* ??? This should no longer be needed now bit-fields have their
2204 if (TREE_CODE (exp) == COMPONENT_REF
2205 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2206 /* If it's thinner than an int, promote it like a
2207 c_promoting_integer_type_p, otherwise leave it alone. */
2208 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2209 TYPE_PRECISION (integer_type_node)) < 0)
2210 return convert (integer_type_node, exp);
2212 if (c_promoting_integer_type_p (type))
2214 /* Preserve unsignedness if not really getting any wider. */
2215 if (TYPE_UNSIGNED (type)
2216 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2217 return convert (unsigned_type_node, exp);
2219 return convert (integer_type_node, exp);
2226 /* Perform default promotions for C data used in expressions.
2227 Enumeral types or short or char are converted to int.
2228 In addition, manifest constants symbols are replaced by their values. */
2231 default_conversion (tree exp)
2234 tree type = TREE_TYPE (exp);
2235 enum tree_code code = TREE_CODE (type);
2238 mark_exp_read (exp);
2240 /* Functions and arrays have been converted during parsing. */
2241 gcc_assert (code != FUNCTION_TYPE);
2242 if (code == ARRAY_TYPE)
2245 /* Constants can be used directly unless they're not loadable. */
2246 if (TREE_CODE (exp) == CONST_DECL)
2247 exp = DECL_INITIAL (exp);
2249 /* Strip no-op conversions. */
2251 STRIP_TYPE_NOPS (exp);
2253 copy_warning (exp, orig_exp);
2255 if (code == VOID_TYPE)
2257 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2258 "void value not ignored as it ought to be");
2259 return error_mark_node;
2262 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2263 if (exp == error_mark_node)
2264 return error_mark_node;
2266 promoted_type = targetm.promoted_type (type);
2268 return convert (promoted_type, exp);
2270 if (INTEGRAL_TYPE_P (type))
2271 return perform_integral_promotions (exp);
2276 /* Look up COMPONENT in a structure or union TYPE.
2278 If the component name is not found, returns NULL_TREE. Otherwise,
2279 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2280 stepping down the chain to the component, which is in the last
2281 TREE_VALUE of the list. Normally the list is of length one, but if
2282 the component is embedded within (nested) anonymous structures or
2283 unions, the list steps down the chain to the component. */
2286 lookup_field (tree type, tree component)
2290 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2291 to the field elements. Use a binary search on this array to quickly
2292 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2293 will always be set for structures which have many elements.
2295 Duplicate field checking replaces duplicates with NULL_TREE so
2296 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2297 case just iterate using DECL_CHAIN. */
2299 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2303 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2305 field = TYPE_FIELDS (type);
2307 top = TYPE_LANG_SPECIFIC (type)->s->len;
2308 while (top - bot > 1)
2310 half = (top - bot + 1) >> 1;
2311 field = field_array[bot+half];
2313 if (DECL_NAME (field) == NULL_TREE)
2315 /* Step through all anon unions in linear fashion. */
2316 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2318 field = field_array[bot++];
2319 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2321 tree anon = lookup_field (TREE_TYPE (field), component);
2324 return tree_cons (NULL_TREE, field, anon);
2326 /* The Plan 9 compiler permits referring
2327 directly to an anonymous struct/union field
2328 using a typedef name. */
2329 if (flag_plan9_extensions
2330 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2331 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2333 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2339 /* Entire record is only anon unions. */
2343 /* Restart the binary search, with new lower bound. */
2347 if (DECL_NAME (field) == component)
2349 if (DECL_NAME (field) < component)
2355 if (DECL_NAME (field_array[bot]) == component)
2356 field = field_array[bot];
2357 else if (DECL_NAME (field) != component)
2362 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2364 if (DECL_NAME (field) == NULL_TREE
2365 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2367 tree anon = lookup_field (TREE_TYPE (field), component);
2370 return tree_cons (NULL_TREE, field, anon);
2372 /* The Plan 9 compiler permits referring directly to an
2373 anonymous struct/union field using a typedef
2375 if (flag_plan9_extensions
2376 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2377 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2378 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2383 if (DECL_NAME (field) == component)
2387 if (field == NULL_TREE)
2391 return tree_cons (NULL_TREE, field, NULL_TREE);
2394 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2397 lookup_field_fuzzy_find_candidates (tree type, tree component,
2398 vec<tree> *candidates)
2401 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2403 if (DECL_NAME (field) == NULL_TREE
2404 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2405 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2408 if (DECL_NAME (field))
2409 candidates->safe_push (DECL_NAME (field));
2413 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2414 rather than returning a TREE_LIST for an exact match. */
2417 lookup_field_fuzzy (tree type, tree component)
2419 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2421 /* First, gather a list of candidates. */
2422 auto_vec <tree> candidates;
2424 lookup_field_fuzzy_find_candidates (type, component,
2427 return find_closest_identifier (component, &candidates);
2430 /* Support function for build_component_ref's error-handling.
2432 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2433 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2436 should_suggest_deref_p (tree datum_type)
2438 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2439 allows "." for ptrs; we could be handling a failed attempt
2440 to access a property. */
2441 if (c_dialect_objc ())
2444 /* Only suggest it for pointers... */
2445 if (TREE_CODE (datum_type) != POINTER_TYPE)
2448 /* ...to structs/unions. */
2449 tree underlying_type = TREE_TYPE (datum_type);
2450 enum tree_code code = TREE_CODE (underlying_type);
2451 if (code == RECORD_TYPE || code == UNION_TYPE)
2457 /* Make an expression to refer to the COMPONENT field of structure or
2458 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2459 location of the COMPONENT_REF. COMPONENT_LOC is the location
2460 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2461 it is from -> operator. */
2464 build_component_ref (location_t loc, tree datum, tree component,
2465 location_t component_loc, location_t arrow_loc)
2467 tree type = TREE_TYPE (datum);
2468 enum tree_code code = TREE_CODE (type);
2471 bool datum_lvalue = lvalue_p (datum);
2473 if (!objc_is_public (datum, component))
2474 return error_mark_node;
2476 /* Detect Objective-C property syntax object.property. */
2477 if (c_dialect_objc ()
2478 && (ref = objc_maybe_build_component_ref (datum, component)))
2481 /* See if there is a field or component with name COMPONENT. */
2483 if (code == RECORD_TYPE || code == UNION_TYPE)
2485 if (!COMPLETE_TYPE_P (type))
2487 c_incomplete_type_error (loc, NULL_TREE, type);
2488 return error_mark_node;
2491 field = lookup_field (type, component);
2495 tree guessed_id = lookup_field_fuzzy (type, component);
2498 /* Attempt to provide a fixit replacement hint, if
2499 we have a valid range for the component. */
2500 location_t reported_loc
2501 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2502 gcc_rich_location rich_loc (reported_loc);
2503 if (component_loc != UNKNOWN_LOCATION)
2504 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2505 error_at (&rich_loc,
2506 "%qT has no member named %qE; did you mean %qE?",
2507 type, component, guessed_id);
2510 error_at (loc, "%qT has no member named %qE", type, component);
2511 return error_mark_node;
2514 /* Accessing elements of atomic structures or unions is undefined
2515 behavior (C11 6.5.2.3#5). */
2516 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2518 if (code == RECORD_TYPE)
2519 warning_at (loc, 0, "accessing a member %qE of an atomic "
2520 "structure %qE", component, datum);
2522 warning_at (loc, 0, "accessing a member %qE of an atomic "
2523 "union %qE", component, datum);
2526 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2527 This might be better solved in future the way the C++ front
2528 end does it - by giving the anonymous entities each a
2529 separate name and type, and then have build_component_ref
2530 recursively call itself. We can't do that here. */
2533 tree subdatum = TREE_VALUE (field);
2536 bool use_datum_quals;
2538 if (TREE_TYPE (subdatum) == error_mark_node)
2539 return error_mark_node;
2541 /* If this is an rvalue, it does not have qualifiers in C
2542 standard terms and we must avoid propagating such
2543 qualifiers down to a non-lvalue array that is then
2544 converted to a pointer. */
2545 use_datum_quals = (datum_lvalue
2546 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2548 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2549 if (use_datum_quals)
2550 quals |= TYPE_QUALS (TREE_TYPE (datum));
2551 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2553 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2555 SET_EXPR_LOCATION (ref, loc);
2556 if (TREE_READONLY (subdatum)
2557 || (use_datum_quals && TREE_READONLY (datum)))
2558 TREE_READONLY (ref) = 1;
2559 if (TREE_THIS_VOLATILE (subdatum)
2560 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2561 TREE_THIS_VOLATILE (ref) = 1;
2563 if (TREE_UNAVAILABLE (subdatum))
2564 error_unavailable_use (subdatum, NULL_TREE);
2565 else if (TREE_DEPRECATED (subdatum))
2566 warn_deprecated_use (subdatum, NULL_TREE);
2570 field = TREE_CHAIN (field);
2576 else if (should_suggest_deref_p (type))
2578 /* Special-case the error message for "ptr.field" for the case
2579 where the user has confused "." vs "->". */
2580 rich_location richloc (line_table, loc);
2581 if (TREE_CODE (datum) == INDIRECT_REF && arrow_loc != UNKNOWN_LOCATION)
2583 richloc.add_fixit_insert_before (arrow_loc, "(*");
2584 richloc.add_fixit_insert_after (arrow_loc, ")");
2586 "%qE is a pointer to pointer; did you mean to dereference "
2587 "it before applying %<->%> to it?",
2588 TREE_OPERAND (datum, 0));
2592 /* "loc" should be the "." token. */
2593 richloc.add_fixit_replace ("->");
2595 "%qE is a pointer; did you mean to use %<->%>?",
2598 return error_mark_node;
2600 else if (code != ERROR_MARK)
2602 "request for member %qE in something not a structure or union",
2605 return error_mark_node;
2608 /* Given an expression PTR for a pointer, return an expression
2609 for the value pointed to.
2610 ERRORSTRING is the name of the operator to appear in error messages.
2612 LOC is the location to use for the generated tree. */
2615 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2617 tree pointer = default_conversion (ptr);
2618 tree type = TREE_TYPE (pointer);
2621 if (TREE_CODE (type) == POINTER_TYPE)
2623 if (CONVERT_EXPR_P (pointer)
2624 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2626 /* If a warning is issued, mark it to avoid duplicates from
2627 the backend. This only needs to be done at
2628 warn_strict_aliasing > 2. */
2629 if (warn_strict_aliasing > 2)
2630 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2631 type, TREE_OPERAND (pointer, 0)))
2632 suppress_warning (pointer, OPT_Wstrict_aliasing_);
2635 if (TREE_CODE (pointer) == ADDR_EXPR
2636 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2637 == TREE_TYPE (type)))
2639 ref = TREE_OPERAND (pointer, 0);
2640 protected_set_expr_location (ref, loc);
2645 tree t = TREE_TYPE (type);
2647 ref = build1 (INDIRECT_REF, t, pointer);
2649 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2650 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2652 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2653 so that we get the proper error message if the result is used
2654 to assign to. Also, &* is supposed to be a no-op.
2655 And ANSI C seems to specify that the type of the result
2656 should be the const type. */
2657 /* A de-reference of a pointer to const is not a const. It is valid
2658 to change it via some other pointer. */
2659 TREE_READONLY (ref) = TYPE_READONLY (t);
2660 TREE_SIDE_EFFECTS (ref)
2661 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2662 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2663 protected_set_expr_location (ref, loc);
2667 else if (TREE_CODE (pointer) != ERROR_MARK)
2668 invalid_indirection_error (loc, type, errstring);
2670 return error_mark_node;
2673 /* This handles expressions of the form "a[i]", which denotes
2676 This is logically equivalent in C to *(a+i), but we may do it differently.
2677 If A is a variable or a member, we generate a primitive ARRAY_REF.
2678 This avoids forcing the array out of registers, and can work on
2679 arrays that are not lvalues (for example, members of structures returned
2682 For vector types, allow vector[i] but not i[vector], and create
2683 *(((type*)&vectortype) + i) for the expression.
2685 LOC is the location to use for the returned expression. */
2688 build_array_ref (location_t loc, tree array, tree index)
2691 bool swapped = false;
2692 if (TREE_TYPE (array) == error_mark_node
2693 || TREE_TYPE (index) == error_mark_node)
2694 return error_mark_node;
2696 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2697 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2698 /* Allow vector[index] but not index[vector]. */
2699 && !gnu_vector_type_p (TREE_TYPE (array)))
2701 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2702 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2705 "subscripted value is neither array nor pointer nor vector");
2707 return error_mark_node;
2709 std::swap (array, index);
2713 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2715 error_at (loc, "array subscript is not an integer");
2716 return error_mark_node;
2719 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2721 error_at (loc, "subscripted value is pointer to function");
2722 return error_mark_node;
2725 /* ??? Existing practice has been to warn only when the char
2726 index is syntactically the index, not for char[array]. */
2728 warn_array_subscript_with_type_char (loc, index);
2730 /* Apply default promotions *after* noticing character types. */
2731 index = default_conversion (index);
2732 if (index == error_mark_node)
2733 return error_mark_node;
2735 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2737 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2738 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2740 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2744 /* An array that is indexed by a non-constant
2745 cannot be stored in a register; we must be able to do
2746 address arithmetic on its address.
2747 Likewise an array of elements of variable size. */
2748 if (TREE_CODE (index) != INTEGER_CST
2749 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2750 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2752 if (!c_mark_addressable (array, true))
2753 return error_mark_node;
2755 /* An array that is indexed by a constant value which is not within
2756 the array bounds cannot be stored in a register either; because we
2757 would get a crash in store_bit_field/extract_bit_field when trying
2758 to access a non-existent part of the register. */
2759 if (TREE_CODE (index) == INTEGER_CST
2760 && TYPE_DOMAIN (TREE_TYPE (array))
2761 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2763 if (!c_mark_addressable (array))
2764 return error_mark_node;
2767 if ((pedantic || warn_c90_c99_compat)
2771 while (TREE_CODE (foo) == COMPONENT_REF)
2772 foo = TREE_OPERAND (foo, 0);
2773 if (VAR_P (foo) && C_DECL_REGISTER (foo))
2774 pedwarn (loc, OPT_Wpedantic,
2775 "ISO C forbids subscripting %<register%> array");
2776 else if (!lvalue_p (foo))
2777 pedwarn_c90 (loc, OPT_Wpedantic,
2778 "ISO C90 forbids subscripting non-lvalue "
2782 type = TREE_TYPE (TREE_TYPE (array));
2783 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2784 /* Array ref is const/volatile if the array elements are
2785 or if the array is. */
2786 TREE_READONLY (rval)
2787 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2788 | TREE_READONLY (array));
2789 TREE_SIDE_EFFECTS (rval)
2790 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2791 | TREE_SIDE_EFFECTS (array));
2792 TREE_THIS_VOLATILE (rval)
2793 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2794 /* This was added by rms on 16 Nov 91.
2795 It fixes vol struct foo *a; a->elts[1]
2796 in an inline function.
2797 Hope it doesn't break something else. */
2798 | TREE_THIS_VOLATILE (array));
2799 ret = require_complete_type (loc, rval);
2800 protected_set_expr_location (ret, loc);
2802 ret = non_lvalue_loc (loc, ret);
2807 tree ar = default_conversion (array);
2809 if (ar == error_mark_node)
2812 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2813 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2815 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2819 ret = non_lvalue_loc (loc, ret);
2824 /* Build an external reference to identifier ID. FUN indicates
2825 whether this will be used for a function call. LOC is the source
2826 location of the identifier. This sets *TYPE to the type of the
2827 identifier, which is not the same as the type of the returned value
2828 for CONST_DECLs defined as enum constants. If the type of the
2829 identifier is not available, *TYPE is set to NULL. */
2831 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2834 tree decl = lookup_name (id);
2836 /* In Objective-C, an instance variable (ivar) may be preferred to
2837 whatever lookup_name() found. */
2838 decl = objc_lookup_ivar (decl, id);
2841 if (decl && decl != error_mark_node)
2844 *type = TREE_TYPE (ref);
2847 /* Implicit function declaration. */
2848 ref = implicitly_declare (loc, id);
2849 else if (decl == error_mark_node)
2850 /* Don't complain about something that's already been
2851 complained about. */
2852 return error_mark_node;
2855 undeclared_variable (loc, id);
2856 return error_mark_node;
2859 if (TREE_TYPE (ref) == error_mark_node)
2860 return error_mark_node;
2862 if (TREE_UNAVAILABLE (ref))
2863 error_unavailable_use (ref, NULL_TREE);
2864 else if (TREE_DEPRECATED (ref))
2865 warn_deprecated_use (ref, NULL_TREE);
2867 /* Recursive call does not count as usage. */
2868 if (ref != current_function_decl)
2870 TREE_USED (ref) = 1;
2873 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2875 if (!in_sizeof && !in_typeof)
2876 C_DECL_USED (ref) = 1;
2877 else if (DECL_INITIAL (ref) == NULL_TREE
2878 && DECL_EXTERNAL (ref)
2879 && !TREE_PUBLIC (ref))
2880 record_maybe_used_decl (ref);
2883 if (TREE_CODE (ref) == CONST_DECL)
2885 used_types_insert (TREE_TYPE (ref));
2888 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2889 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2891 warning_at (loc, OPT_Wc___compat,
2892 ("enum constant defined in struct or union "
2893 "is not visible in C++"));
2894 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2897 ref = DECL_INITIAL (ref);
2898 TREE_CONSTANT (ref) = 1;
2900 else if (current_function_decl != NULL_TREE
2901 && !DECL_FILE_SCOPE_P (current_function_decl)
2902 && (VAR_OR_FUNCTION_DECL_P (ref)
2903 || TREE_CODE (ref) == PARM_DECL))
2905 tree context = decl_function_context (ref);
2907 if (context != NULL_TREE && context != current_function_decl)
2908 DECL_NONLOCAL (ref) = 1;
2910 /* C99 6.7.4p3: An inline definition of a function with external
2911 linkage ... shall not contain a reference to an identifier with
2912 internal linkage. */
2913 else if (current_function_decl != NULL_TREE
2914 && DECL_DECLARED_INLINE_P (current_function_decl)
2915 && DECL_EXTERNAL (current_function_decl)
2916 && VAR_OR_FUNCTION_DECL_P (ref)
2917 && (!VAR_P (ref) || TREE_STATIC (ref))
2918 && ! TREE_PUBLIC (ref)
2919 && DECL_CONTEXT (ref) != current_function_decl)
2920 record_inline_static (loc, current_function_decl, ref,
2926 /* Record details of decls possibly used inside sizeof or typeof. */
2927 struct maybe_used_decl
2931 /* The level seen at (in_sizeof + in_typeof). */
2933 /* The next one at this level or above, or NULL. */
2934 struct maybe_used_decl *next;
2937 static struct maybe_used_decl *maybe_used_decls;
2939 /* Record that DECL, an undefined static function reference seen
2940 inside sizeof or typeof, might be used if the operand of sizeof is
2941 a VLA type or the operand of typeof is a variably modified
2945 record_maybe_used_decl (tree decl)
2947 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2949 t->level = in_sizeof + in_typeof;
2950 t->next = maybe_used_decls;
2951 maybe_used_decls = t;
2954 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2955 USED is false, just discard them. If it is true, mark them used
2956 (if no longer inside sizeof or typeof) or move them to the next
2957 level up (if still inside sizeof or typeof). */
2960 pop_maybe_used (bool used)
2962 struct maybe_used_decl *p = maybe_used_decls;
2963 int cur_level = in_sizeof + in_typeof;
2964 while (p && p->level > cur_level)
2969 C_DECL_USED (p->decl) = 1;
2971 p->level = cur_level;
2975 if (!used || cur_level == 0)
2976 maybe_used_decls = p;
2979 /* Return the result of sizeof applied to EXPR. */
2982 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2985 if (expr.value == error_mark_node)
2987 ret.value = error_mark_node;
2988 ret.original_code = ERROR_MARK;
2989 ret.original_type = NULL;
2990 pop_maybe_used (false);
2994 bool expr_const_operands = true;
2996 if (TREE_CODE (expr.value) == PARM_DECL
2997 && C_ARRAY_PARAMETER (expr.value))
2999 auto_diagnostic_group d;
3000 if (warning_at (loc, OPT_Wsizeof_array_argument,
3001 "%<sizeof%> on array function parameter %qE will "
3002 "return size of %qT", expr.value,
3003 TREE_TYPE (expr.value)))
3004 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3006 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3007 &expr_const_operands);
3008 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3009 c_last_sizeof_arg = expr.value;
3010 c_last_sizeof_loc = loc;
3011 ret.original_code = SIZEOF_EXPR;
3012 ret.original_type = NULL;
3013 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3015 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3016 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3017 folded_expr, ret.value);
3018 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3019 SET_EXPR_LOCATION (ret.value, loc);
3021 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3026 /* Return the result of sizeof applied to T, a structure for the type
3027 name passed to sizeof (rather than the type itself). LOC is the
3028 location of the original expression. */
3031 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3035 tree type_expr = NULL_TREE;
3036 bool type_expr_const = true;
3037 type = groktypename (t, &type_expr, &type_expr_const);
3038 ret.value = c_sizeof (loc, type);
3039 c_last_sizeof_arg = type;
3040 c_last_sizeof_loc = loc;
3041 ret.original_code = SIZEOF_EXPR;
3042 ret.original_type = NULL;
3043 if (type == error_mark_node)
3045 ret.value = error_mark_node;
3046 ret.original_code = ERROR_MARK;
3049 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3050 && C_TYPE_VARIABLE_SIZE (type))
3052 /* If the type is a [*] array, it is a VLA but is represented as
3053 having a size of zero. In such a case we must ensure that
3054 the result of sizeof does not get folded to a constant by
3055 c_fully_fold, because if the size is evaluated the result is
3056 not constant and so constraints on zero or negative size
3057 arrays must not be applied when this sizeof call is inside
3058 another array declarator. */
3060 type_expr = integer_zero_node;
3061 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3062 type_expr, ret.value);
3063 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3065 pop_maybe_used (type != error_mark_node
3066 ? C_TYPE_VARIABLE_SIZE (type) : false);
3070 /* Build a function call to function FUNCTION with parameters PARAMS.
3071 The function call is at LOC.
3072 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3073 TREE_VALUE of each node is a parameter-expression.
3074 FUNCTION's data type may be a function type or a pointer-to-function. */
3077 build_function_call (location_t loc, tree function, tree params)
3079 vec<tree, va_gc> *v;
3082 vec_alloc (v, list_length (params));
3083 for (; params; params = TREE_CHAIN (params))
3084 v->quick_push (TREE_VALUE (params));
3085 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3090 /* Give a note about the location of the declaration of DECL. */
3093 inform_declaration (tree decl)
3095 if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3096 || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3097 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3100 /* Build a function call to function FUNCTION with parameters PARAMS.
3101 If FUNCTION is the result of resolving an overloaded target built-in,
3102 ORIG_FUNDECL is the original function decl, otherwise it is null.
3103 ORIGTYPES, if not NULL, is a vector of types; each element is
3104 either NULL or the original type of the corresponding element in
3105 PARAMS. The original type may differ from TREE_TYPE of the
3106 parameter for enums. FUNCTION's data type may be a function type
3107 or pointer-to-function. This function changes the elements of
3111 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3112 tree function, vec<tree, va_gc> *params,
3113 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3115 tree fntype, fundecl = NULL_TREE;
3116 tree name = NULL_TREE, result;
3122 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3123 STRIP_TYPE_NOPS (function);
3125 /* Convert anything with function type to a pointer-to-function. */
3126 if (TREE_CODE (function) == FUNCTION_DECL)
3128 name = DECL_NAME (function);
3131 tm_malloc_replacement (function);
3134 orig_fundecl = fundecl;
3135 /* Atomic functions have type checking/casting already done. They are
3136 often rewritten and don't match the original parameter list. */
3137 if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3140 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3141 function = function_to_pointer_conversion (loc, function);
3143 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3144 expressions, like those used for ObjC messenger dispatches. */
3145 if (params && !params->is_empty ())
3146 function = objc_rewrite_function_call (function, (*params)[0]);
3148 function = c_fully_fold (function, false, NULL);
3150 fntype = TREE_TYPE (function);
3152 if (TREE_CODE (fntype) == ERROR_MARK)
3153 return error_mark_node;
3155 if (!(TREE_CODE (fntype) == POINTER_TYPE
3156 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3158 if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3160 "called object %qE is not a function or function pointer",
3162 else if (DECL_P (function))
3165 "called object %qD is not a function or function pointer",
3167 inform_declaration (function);
3171 "called object is not a function or function pointer");
3172 return error_mark_node;
3175 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3176 current_function_returns_abnormally = 1;
3178 /* fntype now gets the type of function pointed to. */
3179 fntype = TREE_TYPE (fntype);
3181 /* Convert the parameters to the types declared in the
3182 function prototype, or apply default promotions. */
3184 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3185 origtypes, function, fundecl);
3187 return error_mark_node;
3189 /* Check that the function is called through a compatible prototype.
3190 If it is not, warn. */
3191 if (CONVERT_EXPR_P (function)
3192 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3193 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3194 && !comptypes (fntype, TREE_TYPE (tem)))
3196 tree return_type = TREE_TYPE (fntype);
3198 /* This situation leads to run-time undefined behavior. We can't,
3199 therefore, simply error unless we can prove that all possible
3200 executions of the program must execute the code. */
3201 warning_at (loc, 0, "function called through a non-compatible type");
3203 if (VOID_TYPE_P (return_type)
3204 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3206 "function with qualified void return type called");
3209 argarray = vec_safe_address (params);
3211 /* Check that arguments to builtin functions match the expectations. */
3213 && fndecl_built_in_p (fundecl)
3214 && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3215 orig_fundecl, nargs, argarray))
3216 return error_mark_node;
3218 /* Check that the arguments to the function are valid. */
3219 bool warned_p = check_function_arguments (loc, fundecl, fntype,
3220 nargs, argarray, &arg_loc);
3222 if (name != NULL_TREE
3223 && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3225 if (require_constant_value)
3227 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3228 function, nargs, argarray);
3230 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3231 function, nargs, argarray);
3232 if (TREE_CODE (result) == NOP_EXPR
3233 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3234 STRIP_TYPE_NOPS (result);
3237 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3238 function, nargs, argarray);
3239 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3241 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3242 suppress_warning (result, OPT_Wnonnull);
3244 /* In this improbable scenario, a nested function returns a VM type.
3245 Create a TARGET_EXPR so that the call always has a LHS, much as
3246 what the C++ FE does for functions returning non-PODs. */
3247 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3249 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3250 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3251 NULL_TREE, NULL_TREE);
3254 if (VOID_TYPE_P (TREE_TYPE (result)))
3256 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3258 "function with qualified void return type called");
3261 return require_complete_type (loc, result);
3264 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3267 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3268 tree function, vec<tree, va_gc> *params,
3269 vec<tree, va_gc> *origtypes)
3271 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3272 STRIP_TYPE_NOPS (function);
3274 /* Convert anything with function type to a pointer-to-function. */
3275 if (TREE_CODE (function) == FUNCTION_DECL)
3277 /* Implement type-directed function overloading for builtins.
3278 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3279 handle all the type checking. The result is a complete expression
3280 that implements this function call. */
3281 tree tem = resolve_overloaded_builtin (loc, function, params);
3285 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3288 /* Helper for convert_arguments called to convert the VALue of argument
3289 number ARGNUM from ORIGTYPE to the corresponding parameter number
3291 PLOC is the location where the conversion is being performed.
3292 FUNCTION and FUNDECL are the same as in convert_arguments.
3293 VALTYPE is the original type of VAL before the conversion and,
3294 for EXCESS_PRECISION_EXPR, the operand of the expression.
3295 NPC is true if VAL represents the null pointer constant (VAL itself
3296 will have been folded to an integer constant).
3297 RNAME is the same as FUNCTION except in Objective C when it's
3298 the function selector.
3299 EXCESS_PRECISION is true when VAL was originally represented
3300 as EXCESS_PRECISION_EXPR.
3301 WARNOPT is the same as in convert_for_assignment. */
3304 convert_argument (location_t ploc, tree function, tree fundecl,
3305 tree type, tree origtype, tree val, tree valtype,
3306 bool npc, tree rname, int parmnum, int argnum,
3307 bool excess_precision, int warnopt)
3309 /* Formal parm type is specified by a function prototype. */
3311 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3313 error_at (ploc, "type of formal parameter %d is incomplete",
3318 /* Optionally warn about conversions that differ from the default
3320 if (warn_traditional_conversion || warn_traditional)
3322 unsigned int formal_prec = TYPE_PRECISION (type);
3324 if (INTEGRAL_TYPE_P (type)
3325 && TREE_CODE (valtype) == REAL_TYPE)
3326 warning_at (ploc, OPT_Wtraditional_conversion,
3327 "passing argument %d of %qE as integer rather "
3328 "than floating due to prototype",
3330 if (INTEGRAL_TYPE_P (type)
3331 && TREE_CODE (valtype) == COMPLEX_TYPE)
3332 warning_at (ploc, OPT_Wtraditional_conversion,
3333 "passing argument %d of %qE as integer rather "
3334 "than complex due to prototype",
3336 else if (TREE_CODE (type) == COMPLEX_TYPE
3337 && TREE_CODE (valtype) == REAL_TYPE)
3338 warning_at (ploc, OPT_Wtraditional_conversion,
3339 "passing argument %d of %qE as complex rather "
3340 "than floating due to prototype",
3342 else if (TREE_CODE (type) == REAL_TYPE
3343 && INTEGRAL_TYPE_P (valtype))
3344 warning_at (ploc, OPT_Wtraditional_conversion,
3345 "passing argument %d of %qE as floating rather "
3346 "than integer due to prototype",
3348 else if (TREE_CODE (type) == COMPLEX_TYPE
3349 && INTEGRAL_TYPE_P (valtype))
3350 warning_at (ploc, OPT_Wtraditional_conversion,
3351 "passing argument %d of %qE as complex rather "
3352 "than integer due to prototype",
3354 else if (TREE_CODE (type) == REAL_TYPE
3355 && TREE_CODE (valtype) == COMPLEX_TYPE)
3356 warning_at (ploc, OPT_Wtraditional_conversion,
3357 "passing argument %d of %qE as floating rather "
3358 "than complex due to prototype",
3360 /* ??? At some point, messages should be written about
3361 conversions between complex types, but that's too messy
3363 else if (TREE_CODE (type) == REAL_TYPE
3364 && TREE_CODE (valtype) == REAL_TYPE)
3366 /* Warn if any argument is passed as `float',
3367 since without a prototype it would be `double'. */
3368 if (formal_prec == TYPE_PRECISION (float_type_node)
3369 && type != dfloat32_type_node)
3370 warning_at (ploc, 0,
3371 "passing argument %d of %qE as %<float%> "
3372 "rather than %<double%> due to prototype",
3375 /* Warn if mismatch between argument and prototype
3376 for decimal float types. Warn of conversions with
3377 binary float types and of precision narrowing due to
3379 else if (type != valtype
3380 && (type == dfloat32_type_node
3381 || type == dfloat64_type_node
3382 || type == dfloat128_type_node
3383 || valtype == dfloat32_type_node
3384 || valtype == dfloat64_type_node
3385 || valtype == dfloat128_type_node)
3387 <= TYPE_PRECISION (valtype)
3388 || (type == dfloat128_type_node
3390 != dfloat64_type_node
3392 != dfloat32_type_node)))
3393 || (type == dfloat64_type_node
3395 != dfloat32_type_node))))
3396 warning_at (ploc, 0,
3397 "passing argument %d of %qE as %qT "
3398 "rather than %qT due to prototype",
3399 argnum, rname, type, valtype);
3402 /* Detect integer changing in width or signedness.
3403 These warnings are only activated with
3404 -Wtraditional-conversion, not with -Wtraditional. */
3405 else if (warn_traditional_conversion
3406 && INTEGRAL_TYPE_P (type)
3407 && INTEGRAL_TYPE_P (valtype))
3409 tree would_have_been = default_conversion (val);
3410 tree type1 = TREE_TYPE (would_have_been);
3412 if (val == error_mark_node)
3413 /* VAL could have been of incomplete type. */;
3414 else if (TREE_CODE (type) == ENUMERAL_TYPE
3415 && (TYPE_MAIN_VARIANT (type)
3416 == TYPE_MAIN_VARIANT (valtype)))
3417 /* No warning if function asks for enum
3418 and the actual arg is that enum type. */
3420 else if (formal_prec != TYPE_PRECISION (type1))
3421 warning_at (ploc, OPT_Wtraditional_conversion,
3422 "passing argument %d of %qE "
3423 "with different width due to prototype",
3425 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3427 /* Don't complain if the formal parameter type
3428 is an enum, because we can't tell now whether
3429 the value was an enum--even the same enum. */
3430 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3432 else if (TREE_CODE (val) == INTEGER_CST
3433 && int_fits_type_p (val, type))
3434 /* Change in signedness doesn't matter
3435 if a constant value is unaffected. */
3437 /* If the value is extended from a narrower
3438 unsigned type, it doesn't matter whether we
3439 pass it as signed or unsigned; the value
3440 certainly is the same either way. */
3441 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3442 && TYPE_UNSIGNED (valtype))
3444 else if (TYPE_UNSIGNED (type))
3445 warning_at (ploc, OPT_Wtraditional_conversion,
3446 "passing argument %d of %qE "
3447 "as unsigned due to prototype",
3450 warning_at (ploc, OPT_Wtraditional_conversion,
3451 "passing argument %d of %qE "
3452 "as signed due to prototype",
3457 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3458 sake of better warnings from convert_and_check. */
3459 if (excess_precision)
3460 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3462 tree parmval = convert_for_assignment (ploc, ploc, type,
3463 val, origtype, ic_argpass,
3464 npc, fundecl, function,
3465 parmnum + 1, warnopt);
3467 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3468 && INTEGRAL_TYPE_P (type)
3469 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3470 parmval = default_conversion (parmval);
3475 /* Convert the argument expressions in the vector VALUES
3476 to the types in the list TYPELIST.
3478 If TYPELIST is exhausted, or when an element has NULL as its type,
3479 perform the default conversions.
3481 ORIGTYPES is the original types of the expressions in VALUES. This
3482 holds the type of enum values which have been converted to integral
3483 types. It may be NULL.
3485 FUNCTION is a tree for the called function. It is used only for
3486 error messages, where it is formatted with %qE.
3488 This is also where warnings about wrong number of args are generated.
3490 ARG_LOC are locations of function arguments (if any).
3492 Returns the actual number of arguments processed (which may be less
3493 than the length of VALUES in some error situations), or -1 on
3497 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3498 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3499 tree function, tree fundecl)
3501 unsigned int parmnum;
3502 bool error_args = false;
3503 const bool type_generic = fundecl
3504 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3505 bool type_generic_remove_excess_precision = false;
3506 bool type_generic_overflow_p = false;
3509 /* Change pointer to function to the function itself for
3511 if (TREE_CODE (function) == ADDR_EXPR
3512 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3513 function = TREE_OPERAND (function, 0);
3515 /* Handle an ObjC selector specially for diagnostics. */
3516 selector = objc_message_selector ();
3518 /* For a call to a built-in function declared without a prototype,
3519 set to the built-in function's argument list. */
3520 tree builtin_typelist = NULL_TREE;
3522 /* For type-generic built-in functions, determine whether excess
3523 precision should be removed (classification) or not
3526 && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3528 built_in_function code = DECL_FUNCTION_CODE (fundecl);
3529 if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3531 /* For a call to a built-in function declared without a prototype
3532 use the types of the parameters of the internal built-in to
3533 match those of the arguments to. */
3534 if (tree bdecl = builtin_decl_explicit (code))
3535 builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3538 /* For type-generic built-in functions, determine whether excess
3539 precision should be removed (classification) or not
3544 case BUILT_IN_ISFINITE:
3545 case BUILT_IN_ISINF:
3546 case BUILT_IN_ISINF_SIGN:
3547 case BUILT_IN_ISNAN:
3548 case BUILT_IN_ISNORMAL:
3549 case BUILT_IN_FPCLASSIFY:
3550 type_generic_remove_excess_precision = true;
3553 case BUILT_IN_ADD_OVERFLOW_P:
3554 case BUILT_IN_SUB_OVERFLOW_P:
3555 case BUILT_IN_MUL_OVERFLOW_P:
3556 /* The last argument of these type-generic builtins
3557 should not be promoted. */
3558 type_generic_overflow_p = true;
3566 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3567 individual converted arguments. */
3569 tree typetail, builtin_typetail, val;
3570 for (typetail = typelist,
3571 builtin_typetail = builtin_typelist,
3573 values && values->iterate (parmnum, &val);
3576 /* The type of the function parameter (if it was declared with one). */
3577 tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3578 /* The type of the built-in function parameter (if the function
3579 is a built-in). Used to detect type incompatibilities in
3580 calls to built-ins declared without a prototype. */
3581 tree builtin_type = (builtin_typetail
3582 ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3583 /* The original type of the argument being passed to the function. */
3584 tree valtype = TREE_TYPE (val);
3585 /* The called function (or function selector in Objective C). */
3586 tree rname = function;
3587 int argnum = parmnum + 1;
3588 const char *invalid_func_diag;
3589 /* Set for EXCESS_PRECISION_EXPR arguments. */
3590 bool excess_precision = false;
3591 /* The value of the argument after conversion to the type
3592 of the function parameter it is passed to. */
3594 /* Some __atomic_* builtins have additional hidden argument at
3597 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3598 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3601 if (type == void_type_node)
3604 error_at (loc, "too many arguments to method %qE", selector);
3606 error_at (loc, "too many arguments to function %qE", function);
3607 inform_declaration (fundecl);
3608 return error_args ? -1 : (int) parmnum;
3611 if (builtin_type == void_type_node)
3613 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3614 "too many arguments to built-in function %qE "
3615 "expecting %d", function, parmnum))
3616 inform_declaration (fundecl);
3617 builtin_typetail = NULL_TREE;
3620 if (selector && argnum > 2)
3626 /* Determine if VAL is a null pointer constant before folding it. */
3627 bool npc = null_pointer_constant_p (val);
3629 /* If there is excess precision and a prototype, convert once to
3630 the required type rather than converting via the semantic
3631 type. Likewise without a prototype a float value represented
3632 as long double should be converted once to double. But for
3633 type-generic classification functions excess precision must
3635 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3636 && (type || !type_generic || !type_generic_remove_excess_precision))
3638 val = TREE_OPERAND (val, 0);
3639 excess_precision = true;
3641 val = c_fully_fold (val, false, NULL);
3642 STRIP_TYPE_NOPS (val);
3644 val = require_complete_type (ploc, val);
3646 /* Some floating-point arguments must be promoted to double when
3647 no type is specified by a prototype. This applies to
3648 arguments of type float, and to architecture-specific types
3649 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3650 bool promote_float_arg = false;
3651 if (type == NULL_TREE
3652 && TREE_CODE (valtype) == REAL_TYPE
3653 && (TYPE_PRECISION (valtype)
3654 <= TYPE_PRECISION (double_type_node))
3655 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3656 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3657 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3659 /* Promote this argument, unless it has a _FloatN or
3661 promote_float_arg = true;
3662 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3663 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3665 promote_float_arg = false;
3670 if (type != NULL_TREE)
3672 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3673 parmval = convert_argument (ploc, function, fundecl, type, origtype,
3674 val, valtype, npc, rname, parmnum, argnum,
3675 excess_precision, 0);
3677 else if (promote_float_arg)
3683 /* Convert `float' to `double'. */
3684 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3685 warning_at (ploc, OPT_Wdouble_promotion,
3686 "implicit conversion from %qT to %qT when passing "
3687 "argument to function",
3688 valtype, double_type_node);
3689 parmval = convert (double_type_node, val);
3692 else if ((excess_precision && !type_generic)
3693 || (type_generic_overflow_p && parmnum == 2))
3694 /* A "double" argument with excess precision being passed
3695 without a prototype or in variable arguments.
3696 The last argument of __builtin_*_overflow_p should not be
3698 parmval = convert (valtype, val);
3699 else if ((invalid_func_diag =
3700 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3702 error (invalid_func_diag);
3705 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3710 /* Convert `short' and `char' to full-size `int'. */
3711 parmval = default_conversion (val);
3713 (*values)[parmnum] = parmval;
3714 if (parmval == error_mark_node)
3717 if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3719 /* For a call to a built-in function declared without a prototype,
3720 perform the conversions from the argument to the expected type
3721 but issue warnings rather than errors for any mismatches.
3722 Ignore the converted argument and use the PARMVAL obtained
3723 above by applying default conversions instead. */
3724 tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3725 convert_argument (ploc, function, fundecl, builtin_type, origtype,
3726 val, valtype, npc, rname, parmnum, argnum,
3728 OPT_Wbuiltin_declaration_mismatch);
3732 typetail = TREE_CHAIN (typetail);
3734 if (builtin_typetail)
3735 builtin_typetail = TREE_CHAIN (builtin_typetail);
3738 gcc_assert (parmnum == vec_safe_length (values));
3740 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3742 error_at (loc, "too few arguments to function %qE", function);
3743 inform_declaration (fundecl);
3747 if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3749 unsigned nargs = parmnum;
3750 for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3753 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3754 "too few arguments to built-in function %qE "
3755 "expecting %u", function, nargs - 1))
3756 inform_declaration (fundecl);
3759 return error_args ? -1 : (int) parmnum;
3762 /* This is the entry point used by the parser to build unary operators
3763 in the input. CODE, a tree_code, specifies the unary operator, and
3764 ARG is the operand. For unary plus, the C parser currently uses
3765 CONVERT_EXPR for code.
3767 LOC is the location to use for the tree generated.
3771 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3773 struct c_expr result;
3775 result.original_code = code;
3776 result.original_type = NULL;
3778 if (reject_gcc_builtin (arg.value))
3780 result.value = error_mark_node;
3784 result.value = build_unary_op (loc, code, arg.value, false);
3786 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3787 overflow_warning (loc, result.value, arg.value);
3790 /* We are typically called when parsing a prefix token at LOC acting on
3791 ARG. Reflect this by updating the source range of the result to
3792 start at LOC and end at the end of ARG. */
3793 set_c_expr_source_range (&result,
3794 loc, arg.get_finish ());
3799 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3802 char_type_p (tree type)
3804 return (type == char_type_node
3805 || type == unsigned_char_type_node
3806 || type == signed_char_type_node
3807 || type == char16_type_node
3808 || type == char32_type_node);
3811 /* This is the entry point used by the parser to build binary operators
3812 in the input. CODE, a tree_code, specifies the binary operator, and
3813 ARG1 and ARG2 are the operands. In addition to constructing the
3814 expression, we check for operands that were written with other binary
3815 operators in a way that is likely to confuse the user.
3817 LOCATION is the location of the binary operator. */
3820 parser_build_binary_op (location_t location, enum tree_code code,
3821 struct c_expr arg1, struct c_expr arg2)
3823 struct c_expr result;
3825 enum tree_code code1 = arg1.original_code;
3826 enum tree_code code2 = arg2.original_code;
3827 tree type1 = (arg1.original_type
3828 ? arg1.original_type
3829 : TREE_TYPE (arg1.value));
3830 tree type2 = (arg2.original_type
3831 ? arg2.original_type
3832 : TREE_TYPE (arg2.value));
3834 result.value = build_binary_op (location, code,
3835 arg1.value, arg2.value, true);
3836 result.original_code = code;
3837 result.original_type = NULL;
3839 if (TREE_CODE (result.value) == ERROR_MARK)
3841 set_c_expr_source_range (&result,
3843 arg2.get_finish ());
3847 if (location != UNKNOWN_LOCATION)
3848 protected_set_expr_location (result.value, location);
3850 set_c_expr_source_range (&result,
3852 arg2.get_finish ());
3854 /* Check for cases such as x+y<<z which users are likely
3856 if (warn_parentheses)
3857 warn_about_parentheses (location, code, code1, arg1.value, code2,
3860 if (warn_logical_op)
3861 warn_logical_operator (location, code, TREE_TYPE (result.value),
3862 code1, arg1.value, code2, arg2.value);
3864 if (warn_tautological_compare)
3866 tree lhs = arg1.value;
3867 tree rhs = arg2.value;
3868 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3870 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3871 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3874 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3876 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3878 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3879 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3882 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3884 if (lhs != NULL_TREE && rhs != NULL_TREE)
3885 warn_tautological_cmp (location, code, lhs, rhs);
3888 if (warn_logical_not_paren
3889 && TREE_CODE_CLASS (code) == tcc_comparison
3890 && code1 == TRUTH_NOT_EXPR
3891 && code2 != TRUTH_NOT_EXPR
3892 /* Avoid warning for !!x == y. */
3893 && (TREE_CODE (arg1.value) != NE_EXPR
3894 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3896 /* Avoid warning for !b == y where b has _Bool type. */
3897 tree t = integer_zero_node;
3898 if (TREE_CODE (arg1.value) == EQ_EXPR
3899 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3900 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3902 t = TREE_OPERAND (arg1.value, 0);
3905 if (TREE_TYPE (t) != integer_type_node)
3907 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3908 t = C_MAYBE_CONST_EXPR_EXPR (t);
3909 else if (CONVERT_EXPR_P (t))
3910 t = TREE_OPERAND (t, 0);
3916 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3917 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3920 /* Warn about comparisons against string literals, with the exception
3921 of testing for equality or inequality of a string literal with NULL. */
3922 if (code == EQ_EXPR || code == NE_EXPR)
3924 if ((code1 == STRING_CST
3925 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3926 || (code2 == STRING_CST
3927 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3928 warning_at (location, OPT_Waddress,
3929 "comparison with string literal results in unspecified behavior");
3930 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3931 if (POINTER_TYPE_P (type1)
3932 && null_pointer_constant_p (arg2.value)
3933 && char_type_p (type2))
3935 auto_diagnostic_group d;
3936 if (warning_at (location, OPT_Wpointer_compare,
3937 "comparison between pointer and zero character "
3939 inform (arg1.get_start (),
3940 "did you mean to dereference the pointer?");
3942 else if (POINTER_TYPE_P (type2)
3943 && null_pointer_constant_p (arg1.value)
3944 && char_type_p (type1))
3946 auto_diagnostic_group d;
3947 if (warning_at (location, OPT_Wpointer_compare,
3948 "comparison between pointer and zero character "
3950 inform (arg2.get_start (),
3951 "did you mean to dereference the pointer?");
3954 else if (TREE_CODE_CLASS (code) == tcc_comparison
3955 && (code1 == STRING_CST || code2 == STRING_CST))
3956 warning_at (location, OPT_Waddress,
3957 "comparison with string literal results in unspecified "
3960 if (warn_array_compare
3961 && TREE_CODE_CLASS (code) == tcc_comparison
3962 && TREE_CODE (type1) == ARRAY_TYPE
3963 && TREE_CODE (type2) == ARRAY_TYPE)
3964 do_warn_array_compare (location, code, arg1.value, arg2.value);
3966 if (TREE_OVERFLOW_P (result.value)
3967 && !TREE_OVERFLOW_P (arg1.value)
3968 && !TREE_OVERFLOW_P (arg2.value))
3969 overflow_warning (location, result.value);
3971 /* Warn about comparisons of different enum types. */
3972 if (warn_enum_compare
3973 && TREE_CODE_CLASS (code) == tcc_comparison
3974 && TREE_CODE (type1) == ENUMERAL_TYPE
3975 && TREE_CODE (type2) == ENUMERAL_TYPE
3976 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3977 warning_at (location, OPT_Wenum_compare,
3978 "comparison between %qT and %qT",
3984 /* Return a tree for the difference of pointers OP0 and OP1.
3985 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3986 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3989 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3991 tree restype = ptrdiff_type_node;
3992 tree result, inttype;
3994 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3995 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3996 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3997 tree orig_op0 = op0;
3998 tree orig_op1 = op1;
4000 /* If the operands point into different address spaces, we need to
4001 explicitly convert them to pointers into the common address space
4002 before we can subtract the numerical address values. */
4005 addr_space_t as_common;
4008 /* Determine the common superset address space. This is guaranteed
4009 to exist because the caller verified that comp_target_types
4010 returned non-zero. */
4011 if (!addr_space_superset (as0, as1, &as_common))
4014 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4015 op0 = convert (common_type, op0);
4016 op1 = convert (common_type, op1);
4019 /* Determine integer type result of the subtraction. This will usually
4020 be the same as the result type (ptrdiff_t), but may need to be a wider
4021 type if pointers for the address space are wider than ptrdiff_t. */
4022 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4023 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4027 if (TREE_CODE (target_type) == VOID_TYPE)
4028 pedwarn (loc, OPT_Wpointer_arith,
4029 "pointer of type %<void *%> used in subtraction");
4030 if (TREE_CODE (target_type) == FUNCTION_TYPE)
4031 pedwarn (loc, OPT_Wpointer_arith,
4032 "pointer to a function used in subtraction");
4034 if (current_function_decl != NULL_TREE
4035 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4037 op0 = save_expr (op0);
4038 op1 = save_expr (op1);
4040 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4041 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4044 /* First do the subtraction, then build the divide operator
4045 and only convert at the very end.
4046 Do not do default conversions in case restype is a short type. */
4048 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4049 pointers. If some platform cannot provide that, or has a larger
4050 ptrdiff_type to support differences larger than half the address
4051 space, cast the pointers to some larger integer type and do the
4052 computations in that type. */
4053 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4054 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4055 convert (inttype, op1), false);
4058 /* Cast away qualifiers. */
4059 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4060 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4061 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4064 /* This generates an error if op1 is pointer to incomplete type. */
4065 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4066 error_at (loc, "arithmetic on pointer to an incomplete type");
4067 else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4068 TREE_TYPE (TREE_TYPE (orig_op0))))
4069 verify_type_context (loc, TCTX_POINTER_ARITH,
4070 TREE_TYPE (TREE_TYPE (orig_op1)));
4072 op1 = c_size_in_bytes (target_type);
4074 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4075 error_at (loc, "arithmetic on pointer to an empty aggregate");
4077 /* Divide by the size, in easiest possible way. */
4078 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4079 op0, convert (inttype, op1));
4081 /* Convert to final result type if necessary. */
4082 return convert (restype, result);
4085 /* Expand atomic compound assignments into an appropriate sequence as
4086 specified by the C11 standard section 6.5.16.2.
4092 This sequence is used for all types for which these operations are
4095 In addition, built-in versions of the 'fe' prefixed routines may
4096 need to be invoked for floating point (real, complex or vector) when
4097 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4107 __atomic_load (addr, &old, SEQ_CST);
4108 feholdexcept (&fenv);
4110 newval = old op val;
4111 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4114 feclearexcept (FE_ALL_EXCEPT);
4117 feupdateenv (&fenv);
4119 The compiler will issue the __atomic_fetch_* built-in when possible,
4120 otherwise it will generate the generic form of the atomic operations.
4121 This requires temp(s) and has their address taken. The atomic processing
4122 is smart enough to figure out when the size of an object can utilize
4123 a lock-free version, and convert the built-in call to the appropriate
4124 lock-free routine. The optimizers will then dispose of any temps that
4125 are no longer required, and lock-free implementations are utilized as
4126 long as there is target support for the required size.
4128 If the operator is NOP_EXPR, then this is a simple assignment, and
4129 an __atomic_store is issued to perform the assignment rather than
4132 /* Build an atomic assignment at LOC, expanding into the proper
4133 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4134 the result of the operation, unless RETURN_OLD_P, in which case
4135 return the old value of LHS (this is only for postincrement and
4139 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4140 tree rhs, bool return_old_p)
4142 tree fndecl, func_call;
4143 vec<tree, va_gc> *params;
4144 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4146 tree compound_stmt = NULL_TREE;
4147 tree stmt, goto_stmt;
4148 tree loop_label, loop_decl, done_label, done_decl;
4150 tree lhs_type = TREE_TYPE (lhs);
4151 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4152 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4153 tree rhs_semantic_type = TREE_TYPE (rhs);
4154 tree nonatomic_rhs_semantic_type;
4157 gcc_assert (TYPE_ATOMIC (lhs_type));
4160 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4162 /* Allocate enough vector items for a compare_exchange. */
4163 vec_alloc (params, 6);
4165 /* Create a compound statement to hold the sequence of statements
4167 if (modifycode != NOP_EXPR)
4169 compound_stmt = c_begin_compound_stmt (false);
4171 /* For consistency with build_modify_expr on non-_Atomic,
4172 mark the lhs as read. Also, it would be very hard to match
4173 such expressions in mark_exp_read. */
4174 mark_exp_read (lhs);
4177 /* Remove any excess precision (which is only present here in the
4178 case of compound assignments). */
4179 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4181 gcc_assert (modifycode != NOP_EXPR);
4182 rhs = TREE_OPERAND (rhs, 0);
4184 rhs_type = TREE_TYPE (rhs);
4186 /* Fold the RHS if it hasn't already been folded. */
4187 if (modifycode != NOP_EXPR)
4188 rhs = c_fully_fold (rhs, false, NULL);
4190 /* Remove the qualifiers for the rest of the expressions and create
4191 the VAL temp variable to hold the RHS. */
4192 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4193 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4194 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4196 val = create_tmp_var_raw (nonatomic_rhs_type);
4197 TREE_ADDRESSABLE (val) = 1;
4198 suppress_warning (val);
4199 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4201 TREE_SIDE_EFFECTS (rhs) = 1;
4202 SET_EXPR_LOCATION (rhs, loc);
4203 if (modifycode != NOP_EXPR)
4206 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4208 if (modifycode == NOP_EXPR)
4210 compound_stmt = rhs;
4211 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4212 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4213 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4214 params->quick_push (lhs_addr);
4215 params->quick_push (rhs);
4216 params->quick_push (seq_cst);
4217 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4219 compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4220 compound_stmt, func_call);
4222 /* VAL is the value which was stored, return a COMPOUND_STMT of
4223 the statement and that value. */
4224 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4227 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4228 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4229 isn't applicable for such builtins. ??? Do we want to handle enums? */
4230 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4231 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4233 built_in_function fncode;
4237 case POINTER_PLUS_EXPR:
4238 fncode = (return_old_p
4239 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4240 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4243 fncode = (return_old_p
4244 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4245 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4248 fncode = (return_old_p
4249 ? BUILT_IN_ATOMIC_FETCH_AND_N
4250 : BUILT_IN_ATOMIC_AND_FETCH_N);
4253 fncode = (return_old_p
4254 ? BUILT_IN_ATOMIC_FETCH_OR_N
4255 : BUILT_IN_ATOMIC_OR_FETCH_N);
4258 fncode = (return_old_p
4259 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4260 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4266 /* We can only use "_1" through "_16" variants of the atomic fetch
4268 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4269 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4272 /* If this is a pointer type, we need to multiply by the size of
4273 the pointer target type. */
4274 if (POINTER_TYPE_P (lhs_type))
4276 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4277 /* ??? This would introduce -Wdiscarded-qualifiers
4278 warning: __atomic_fetch_* expect volatile void *
4279 type as the first argument. (Assignments between
4280 atomic and non-atomic objects are OK.) */
4281 || TYPE_RESTRICT (lhs_type))
4283 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4284 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4285 convert (ptrdiff_type_node, rhs),
4286 convert (ptrdiff_type_node, sz));
4289 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4290 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4291 fndecl = builtin_decl_explicit (fncode);
4292 params->quick_push (lhs_addr);
4293 params->quick_push (rhs);
4294 params->quick_push (seq_cst);
4295 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4297 newval = create_tmp_var_raw (nonatomic_lhs_type);
4298 TREE_ADDRESSABLE (newval) = 1;
4299 suppress_warning (newval);
4300 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4301 NULL_TREE, NULL_TREE);
4302 SET_EXPR_LOCATION (rhs, loc);
4305 /* Finish the compound statement. */
4306 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4308 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4309 the statement and that value. */
4310 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4314 /* Create the variables and labels required for the op= form. */
4315 old = create_tmp_var_raw (nonatomic_lhs_type);
4316 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4317 TREE_ADDRESSABLE (old) = 1;
4318 suppress_warning (old);
4320 newval = create_tmp_var_raw (nonatomic_lhs_type);
4321 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4322 TREE_ADDRESSABLE (newval) = 1;
4323 suppress_warning (newval);
4325 loop_decl = create_artificial_label (loc);
4326 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4328 done_decl = create_artificial_label (loc);
4329 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4331 /* __atomic_load (addr, &old, SEQ_CST). */
4332 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4333 params->quick_push (lhs_addr);
4334 params->quick_push (old_addr);
4335 params->quick_push (seq_cst);
4336 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4337 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4340 params->truncate (0);
4342 /* Create the expressions for floating-point environment
4343 manipulation, if required. */
4344 bool need_fenv = (flag_trapping_math
4345 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4346 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4348 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4351 add_stmt (hold_call);
4354 add_stmt (loop_label);
4356 /* newval = old + val; */
4357 if (rhs_type != rhs_semantic_type)
4358 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4359 rhs = build_binary_op (loc, modifycode, old, val, true);
4360 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4362 tree eptype = TREE_TYPE (rhs);
4363 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4364 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4367 rhs = c_fully_fold (rhs, false, NULL);
4368 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4369 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4371 if (rhs != error_mark_node)
4373 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4375 SET_EXPR_LOCATION (rhs, loc);
4379 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4381 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4382 params->quick_push (lhs_addr);
4383 params->quick_push (old_addr);
4384 params->quick_push (newval_addr);
4385 params->quick_push (integer_zero_node);
4386 params->quick_push (seq_cst);
4387 params->quick_push (seq_cst);
4388 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4390 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4391 SET_EXPR_LOCATION (goto_stmt, loc);
4393 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4394 SET_EXPR_LOCATION (stmt, loc);
4398 add_stmt (clear_call);
4401 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4402 SET_EXPR_LOCATION (goto_stmt, loc);
4403 add_stmt (goto_stmt);
4406 add_stmt (done_label);
4409 add_stmt (update_call);
4411 /* Finish the compound statement. */
4412 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4414 /* NEWVAL is the value that was successfully stored, return a
4415 COMPOUND_EXPR of the statement and the appropriate value. */
4416 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4417 return_old_p ? old : newval);
4420 /* Construct and perhaps optimize a tree representation
4421 for a unary operation. CODE, a tree_code, specifies the operation
4422 and XARG is the operand.
4423 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4424 promotions (such as from short to int).
4425 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4426 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4429 LOCATION is the location of the operator. */
4432 build_unary_op (location_t location, enum tree_code code, tree xarg,
4435 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4437 tree argtype = NULL_TREE;
4438 enum tree_code typecode;
4440 tree ret = error_mark_node;
4441 tree eptype = NULL_TREE;
4442 const char *invalid_op_diag;
4445 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4447 arg = remove_c_maybe_const_expr (arg);
4449 if (code != ADDR_EXPR)
4450 arg = require_complete_type (location, arg);
4452 typecode = TREE_CODE (TREE_TYPE (arg));
4453 if (typecode == ERROR_MARK)
4454 return error_mark_node;
4455 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4456 typecode = INTEGER_TYPE;
4458 if ((invalid_op_diag
4459 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4461 error_at (location, invalid_op_diag);
4462 return error_mark_node;
4465 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4467 eptype = TREE_TYPE (arg);
4468 arg = TREE_OPERAND (arg, 0);
4474 /* This is used for unary plus, because a CONVERT_EXPR
4475 is enough to prevent anybody from looking inside for
4476 associativity, but won't generate any code. */
4477 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4478 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4479 || gnu_vector_type_p (TREE_TYPE (arg))))
4481 error_at (location, "wrong type argument to unary plus");
4482 return error_mark_node;
4484 else if (!noconvert)
4485 arg = default_conversion (arg);
4486 arg = non_lvalue_loc (location, arg);
4490 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4491 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4492 || gnu_vector_type_p (TREE_TYPE (arg))))
4494 error_at (location, "wrong type argument to unary minus");
4495 return error_mark_node;
4497 else if (!noconvert)
4498 arg = default_conversion (arg);
4502 /* ~ works on integer types and non float vectors. */
4503 if (typecode == INTEGER_TYPE
4504 || (gnu_vector_type_p (TREE_TYPE (arg))
4505 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4509 /* Warn if the expression has boolean value. */
4510 while (TREE_CODE (e) == COMPOUND_EXPR)
4511 e = TREE_OPERAND (e, 1);
4513 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4514 || truth_value_p (TREE_CODE (e))))
4516 auto_diagnostic_group d;
4517 if (warning_at (location, OPT_Wbool_operation,
4518 "%<~%> on a boolean expression"))
4520 gcc_rich_location richloc (location);
4521 richloc.add_fixit_insert_before (location, "!");
4522 inform (&richloc, "did you mean to use logical not?");
4526 arg = default_conversion (arg);
4528 else if (typecode == COMPLEX_TYPE)
4531 pedwarn (location, OPT_Wpedantic,
4532 "ISO C does not support %<~%> for complex conjugation");
4534 arg = default_conversion (arg);
4538 error_at (location, "wrong type argument to bit-complement");
4539 return error_mark_node;
4544 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4546 error_at (location, "wrong type argument to abs");
4547 return error_mark_node;
4549 else if (!noconvert)
4550 arg = default_conversion (arg);
4554 if (!(typecode == INTEGER_TYPE))
4556 error_at (location, "wrong type argument to absu");
4557 return error_mark_node;
4559 else if (!noconvert)
4560 arg = default_conversion (arg);
4564 /* Conjugating a real value is a no-op, but allow it anyway. */
4565 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4566 || typecode == COMPLEX_TYPE))
4568 error_at (location, "wrong type argument to conjugation");
4569 return error_mark_node;
4571 else if (!noconvert)
4572 arg = default_conversion (arg);
4575 case TRUTH_NOT_EXPR:
4576 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4577 && typecode != REAL_TYPE && typecode != POINTER_TYPE
4578 && typecode != COMPLEX_TYPE)
4581 "wrong type argument to unary exclamation mark");
4582 return error_mark_node;
4586 arg = c_objc_common_truthvalue_conversion (location, xarg);
4587 arg = remove_c_maybe_const_expr (arg);
4590 arg = c_objc_common_truthvalue_conversion (location, arg);
4591 ret = invert_truthvalue_loc (location, arg);
4592 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4593 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4594 location = EXPR_LOCATION (ret);
4595 goto return_build_unary_op;
4599 ret = build_real_imag_expr (location, code, arg);
4600 if (ret == error_mark_node)
4601 return error_mark_node;
4602 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4603 eptype = TREE_TYPE (eptype);
4604 goto return_build_unary_op;
4606 case PREINCREMENT_EXPR:
4607 case POSTINCREMENT_EXPR:
4608 case PREDECREMENT_EXPR:
4609 case POSTDECREMENT_EXPR:
4611 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4613 tree inner = build_unary_op (location, code,
4614 C_MAYBE_CONST_EXPR_EXPR (arg),
4616 if (inner == error_mark_node)
4617 return error_mark_node;
4618 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4619 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4620 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4621 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4622 goto return_build_unary_op;
4625 /* Complain about anything that is not a true lvalue. In
4626 Objective-C, skip this check for property_refs. */
4627 if (!objc_is_property_ref (arg)
4628 && !lvalue_or_else (location,
4629 arg, ((code == PREINCREMENT_EXPR
4630 || code == POSTINCREMENT_EXPR)
4633 return error_mark_node;
4635 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4637 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4638 warning_at (location, OPT_Wc___compat,
4639 "increment of enumeration value is invalid in C++");
4641 warning_at (location, OPT_Wc___compat,
4642 "decrement of enumeration value is invalid in C++");
4645 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4647 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4648 warning_at (location, OPT_Wbool_operation,
4649 "increment of a boolean expression");
4651 warning_at (location, OPT_Wbool_operation,
4652 "decrement of a boolean expression");
4655 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4656 arg = c_fully_fold (arg, false, NULL, true);
4659 atomic_op = really_atomic_lvalue (arg);
4661 /* Increment or decrement the real part of the value,
4662 and don't change the imaginary part. */
4663 if (typecode == COMPLEX_TYPE)
4667 pedwarn (location, OPT_Wpedantic,
4668 "ISO C does not support %<++%> and %<--%> on complex types");
4672 arg = stabilize_reference (arg);
4673 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4675 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4677 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4678 if (real == error_mark_node || imag == error_mark_node)
4679 return error_mark_node;
4680 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4682 goto return_build_unary_op;
4686 /* Report invalid types. */
4688 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4689 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4690 && typecode != COMPLEX_TYPE
4691 && !gnu_vector_type_p (TREE_TYPE (arg)))
4693 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4694 error_at (location, "wrong type argument to increment");
4696 error_at (location, "wrong type argument to decrement");
4698 return error_mark_node;
4704 argtype = TREE_TYPE (arg);
4706 /* Compute the increment. */
4708 if (typecode == POINTER_TYPE)
4710 /* If pointer target is an incomplete type,
4711 we just cannot know how to do the arithmetic. */
4712 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4714 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4716 "increment of pointer to an incomplete type %qT",
4717 TREE_TYPE (argtype));
4720 "decrement of pointer to an incomplete type %qT",
4721 TREE_TYPE (argtype));
4723 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4724 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4726 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4727 pedwarn (location, OPT_Wpointer_arith,
4728 "wrong type argument to increment");
4730 pedwarn (location, OPT_Wpointer_arith,
4731 "wrong type argument to decrement");
4734 verify_type_context (location, TCTX_POINTER_ARITH,
4735 TREE_TYPE (argtype));
4737 inc = c_size_in_bytes (TREE_TYPE (argtype));
4738 inc = convert_to_ptrofftype_loc (location, inc);
4740 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4742 /* For signed fract types, we invert ++ to -- or
4743 -- to ++, and change inc from 1 to -1, because
4744 it is not possible to represent 1 in signed fract constants.
4745 For unsigned fract types, the result always overflows and
4746 we get an undefined (original) or the maximum value. */
4747 if (code == PREINCREMENT_EXPR)
4748 code = PREDECREMENT_EXPR;
4749 else if (code == PREDECREMENT_EXPR)
4750 code = PREINCREMENT_EXPR;
4751 else if (code == POSTINCREMENT_EXPR)
4752 code = POSTDECREMENT_EXPR;
4753 else /* code == POSTDECREMENT_EXPR */
4754 code = POSTINCREMENT_EXPR;
4756 inc = integer_minus_one_node;
4757 inc = convert (argtype, inc);
4761 inc = VECTOR_TYPE_P (argtype)
4762 ? build_one_cst (argtype)
4764 inc = convert (argtype, inc);
4767 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4768 need to ask Objective-C to build the increment or decrement
4769 expression for it. */
4770 if (objc_is_property_ref (arg))
4771 return objc_build_incr_expr_for_property_ref (location, code,
4774 /* Report a read-only lvalue. */
4775 if (TYPE_READONLY (argtype))
4777 readonly_error (location, arg,
4778 ((code == PREINCREMENT_EXPR
4779 || code == POSTINCREMENT_EXPR)
4780 ? lv_increment : lv_decrement));
4781 return error_mark_node;
4783 else if (TREE_READONLY (arg))
4784 readonly_warning (arg,
4785 ((code == PREINCREMENT_EXPR
4786 || code == POSTINCREMENT_EXPR)
4787 ? lv_increment : lv_decrement));
4789 /* If the argument is atomic, use the special code sequences for
4790 atomic compound assignment. */
4793 arg = stabilize_reference (arg);
4794 ret = build_atomic_assign (location, arg,
4795 ((code == PREINCREMENT_EXPR
4796 || code == POSTINCREMENT_EXPR)
4799 (FRACT_MODE_P (TYPE_MODE (argtype))
4801 : integer_one_node),
4802 (code == POSTINCREMENT_EXPR
4803 || code == POSTDECREMENT_EXPR));
4804 goto return_build_unary_op;
4807 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4808 val = boolean_increment (code, arg);
4810 val = build2 (code, TREE_TYPE (arg), arg, inc);
4811 TREE_SIDE_EFFECTS (val) = 1;
4813 goto return_build_unary_op;
4817 /* Note that this operation never does default_conversion. */
4819 /* The operand of unary '&' must be an lvalue (which excludes
4820 expressions of type void), or, in C99, the result of a [] or
4821 unary '*' operator. */
4822 if (VOID_TYPE_P (TREE_TYPE (arg))
4823 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4824 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4825 pedwarn (location, 0, "taking address of expression of type %<void%>");
4827 /* Let &* cancel out to simplify resulting code. */
4828 if (INDIRECT_REF_P (arg))
4830 /* Don't let this be an lvalue. */
4831 if (lvalue_p (TREE_OPERAND (arg, 0)))
4832 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4833 ret = TREE_OPERAND (arg, 0);
4834 goto return_build_unary_op;
4837 /* Anything not already handled and not a true memory reference
4838 or a non-lvalue array is an error. */
4839 if (typecode != FUNCTION_TYPE && !noconvert
4840 && !lvalue_or_else (location, arg, lv_addressof))
4841 return error_mark_node;
4843 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4845 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4847 tree inner = build_unary_op (location, code,
4848 C_MAYBE_CONST_EXPR_EXPR (arg),
4850 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4851 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4852 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4853 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4854 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4855 goto return_build_unary_op;
4858 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4859 argtype = TREE_TYPE (arg);
4861 /* If the lvalue is const or volatile, merge that into the type
4862 to which the address will point. This is only needed
4863 for function types. */
4864 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4865 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4866 && TREE_CODE (argtype) == FUNCTION_TYPE)
4868 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4869 int quals = orig_quals;
4871 if (TREE_READONLY (arg))
4872 quals |= TYPE_QUAL_CONST;
4873 if (TREE_THIS_VOLATILE (arg))
4874 quals |= TYPE_QUAL_VOLATILE;
4876 argtype = c_build_qualified_type (argtype, quals);
4879 switch (TREE_CODE (arg))
4882 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4884 error_at (location, "cannot take address of bit-field %qD",
4885 TREE_OPERAND (arg, 1));
4886 return error_mark_node;
4892 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4894 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4895 && !POINTER_TYPE_P (TREE_TYPE (arg))
4896 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4898 error_at (location, "cannot take address of scalar with "
4899 "reverse storage order");
4900 return error_mark_node;
4903 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4904 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4905 warning_at (location, OPT_Wscalar_storage_order,
4906 "address of array with reverse scalar storage "
4914 if (!c_mark_addressable (arg))
4915 return error_mark_node;
4917 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4918 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4920 argtype = build_pointer_type (argtype);
4922 /* ??? Cope with user tricks that amount to offsetof. Delete this
4923 when we have proper support for integer constant expressions. */
4924 val = get_base_address (arg);
4925 if (val && INDIRECT_REF_P (val)
4926 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4928 ret = fold_offsetof (arg, argtype);
4929 goto return_build_unary_op;
4932 val = build1 (ADDR_EXPR, argtype, arg);
4935 goto return_build_unary_op;
4938 ret = build1 (code, TREE_TYPE (arg), arg);
4939 goto return_build_unary_op;
4945 if (argtype == NULL_TREE)
4946 argtype = TREE_TYPE (arg);
4947 if (TREE_CODE (arg) == INTEGER_CST)
4948 ret = (require_constant_value
4949 ? fold_build1_initializer_loc (location, code, argtype, arg)
4950 : fold_build1_loc (location, code, argtype, arg));
4952 ret = build1 (code, argtype, arg);
4953 return_build_unary_op:
4954 gcc_assert (ret != error_mark_node);
4955 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4956 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4957 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4958 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4959 ret = note_integer_operands (ret);
4961 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4962 protected_set_expr_location (ret, location);
4966 /* Return nonzero if REF is an lvalue valid for this language.
4967 Lvalues can be assigned, unless their type has TYPE_READONLY.
4968 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4971 lvalue_p (const_tree ref)
4973 const enum tree_code code = TREE_CODE (ref);
4980 return lvalue_p (TREE_OPERAND (ref, 0));
4982 case C_MAYBE_CONST_EXPR:
4983 return lvalue_p (TREE_OPERAND (ref, 1));
4985 case COMPOUND_LITERAL_EXPR:
4990 case TARGET_MEM_REF:
4991 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
4999 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5000 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5003 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5010 /* Give a warning for storing in something that is read-only in GCC
5011 terms but not const in ISO C terms. */
5014 readonly_warning (tree arg, enum lvalue_use use)
5019 warning (0, "assignment of read-only location %qE", arg);
5022 warning (0, "increment of read-only location %qE", arg);
5025 warning (0, "decrement of read-only location %qE", arg);
5034 /* Return nonzero if REF is an lvalue valid for this language;
5035 otherwise, print an error message and return zero. USE says
5036 how the lvalue is being used and so selects the error message.
5037 LOCATION is the location at which any error should be reported. */
5040 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5042 int win = lvalue_p (ref);
5045 lvalue_error (loc, use);
5050 /* Mark EXP saying that we need to be able to take the
5051 address of it; it should not be allocated in a register.
5052 Returns true if successful. ARRAY_REF_P is true if this
5053 is for ARRAY_REF construction - in that case we don't want
5054 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5055 it is fine to use ARRAY_REFs for vector subscripts on vector
5056 register variables. */
5059 c_mark_addressable (tree exp, bool array_ref_p)
5064 switch (TREE_CODE (x))
5066 case VIEW_CONVERT_EXPR:
5068 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5069 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5071 x = TREE_OPERAND (x, 0);
5075 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5077 error ("cannot take address of bit-field %qD",
5078 TREE_OPERAND (x, 1));
5086 x = TREE_OPERAND (x, 0);
5089 case COMPOUND_LITERAL_EXPR:
5090 TREE_ADDRESSABLE (x) = 1;
5091 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5095 TREE_ADDRESSABLE (x) = 1;
5102 if (C_DECL_REGISTER (x)
5103 && DECL_NONLOCAL (x))
5105 if (TREE_PUBLIC (x) || is_global_var (x))
5108 ("global register variable %qD used in nested function", x);
5111 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5113 else if (C_DECL_REGISTER (x))
5115 if (TREE_PUBLIC (x) || is_global_var (x))
5116 error ("address of global register variable %qD requested", x);
5118 error ("address of register variable %qD requested", x);
5124 TREE_ADDRESSABLE (x) = 1;
5131 /* Convert EXPR to TYPE, warning about conversion problems with
5132 constants. SEMANTIC_TYPE is the type this conversion would use
5133 without excess precision. If SEMANTIC_TYPE is NULL, this function
5134 is equivalent to convert_and_check. This function is a wrapper that
5135 handles conversions that may be different than
5136 the usual ones because of excess precision. */
5139 ep_convert_and_check (location_t loc, tree type, tree expr,
5142 if (TREE_TYPE (expr) == type)
5145 /* For C11, integer conversions may have results with excess
5147 if (flag_isoc11 || !semantic_type)
5148 return convert_and_check (loc, type, expr);
5150 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5151 && TREE_TYPE (expr) != semantic_type)
5153 /* For integers, we need to check the real conversion, not
5154 the conversion to the excess precision type. */
5155 expr = convert_and_check (loc, semantic_type, expr);
5157 /* Result type is the excess precision type, which should be
5158 large enough, so do not check. */
5159 return convert (type, expr);
5162 /* If EXPR refers to a built-in declared without a prototype returns
5163 the actual type of the built-in and, if non-null, set *BLTIN to
5164 a pointer to the built-in. Otherwise return the type of EXPR
5165 and clear *BLTIN if non-null. */
5168 type_or_builtin_type (tree expr, tree *bltin = NULL)
5176 tree type = TREE_TYPE (expr);
5177 if (TREE_CODE (expr) != ADDR_EXPR)
5180 tree oper = TREE_OPERAND (expr, 0);
5182 || TREE_CODE (oper) != FUNCTION_DECL
5183 || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5186 built_in_function code = DECL_FUNCTION_CODE (oper);
5187 if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5190 if ((*bltin = builtin_decl_implicit (code)))
5191 type = build_pointer_type (TREE_TYPE (*bltin));
5196 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5197 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5198 if folded to an integer constant then the unselected half may
5199 contain arbitrary operations not normally permitted in constant
5200 expressions. Set the location of the expression to LOC. */
5203 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5204 tree op1, tree op1_original_type, location_t op1_loc,
5205 tree op2, tree op2_original_type, location_t op2_loc)
5209 enum tree_code code1;
5210 enum tree_code code2;
5211 tree result_type = NULL;
5212 tree semantic_result_type = NULL;
5213 tree orig_op1 = op1, orig_op2 = op2;
5214 bool int_const, op1_int_operands, op2_int_operands, int_operands;
5215 bool ifexp_int_operands;
5218 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5219 if (op1_int_operands)
5220 op1 = remove_c_maybe_const_expr (op1);
5221 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5222 if (op2_int_operands)
5223 op2 = remove_c_maybe_const_expr (op2);
5224 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5225 if (ifexp_int_operands)
5226 ifexp = remove_c_maybe_const_expr (ifexp);
5228 /* Promote both alternatives. */
5230 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5231 op1 = default_conversion (op1);
5232 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5233 op2 = default_conversion (op2);
5235 if (TREE_CODE (ifexp) == ERROR_MARK
5236 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5237 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5238 return error_mark_node;
5240 tree bltin1 = NULL_TREE;
5241 tree bltin2 = NULL_TREE;
5242 type1 = type_or_builtin_type (op1, &bltin1);
5243 code1 = TREE_CODE (type1);
5244 type2 = type_or_builtin_type (op2, &bltin2);
5245 code2 = TREE_CODE (type2);
5247 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5248 return error_mark_node;
5250 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5251 return error_mark_node;
5253 /* C90 does not permit non-lvalue arrays in conditional expressions.
5254 In C99 they will be pointers by now. */
5255 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5257 error_at (colon_loc, "non-lvalue array in conditional expression");
5258 return error_mark_node;
5261 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5262 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5263 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5264 || code1 == COMPLEX_TYPE)
5265 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5266 || code2 == COMPLEX_TYPE))
5268 semantic_result_type = c_common_type (type1, type2);
5269 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5271 op1 = TREE_OPERAND (op1, 0);
5272 type1 = TREE_TYPE (op1);
5273 gcc_assert (TREE_CODE (type1) == code1);
5275 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5277 op2 = TREE_OPERAND (op2, 0);
5278 type2 = TREE_TYPE (op2);
5279 gcc_assert (TREE_CODE (type2) == code2);
5283 if (warn_cxx_compat)
5285 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5286 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5288 if (TREE_CODE (t1) == ENUMERAL_TYPE
5289 && TREE_CODE (t2) == ENUMERAL_TYPE
5290 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5291 warning_at (colon_loc, OPT_Wc___compat,
5292 ("different enum types in conditional is "
5293 "invalid in C++: %qT vs %qT"),
5297 /* Quickly detect the usual case where op1 and op2 have the same type
5299 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5302 result_type = type1;
5304 result_type = TYPE_MAIN_VARIANT (type1);
5306 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5307 || code1 == COMPLEX_TYPE)
5308 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5309 || code2 == COMPLEX_TYPE))
5311 /* In C11, a conditional expression between a floating-point
5312 type and an integer type should convert the integer type to
5313 the evaluation format of the floating-point type, with
5314 possible excess precision. */
5315 tree eptype1 = type1;
5316 tree eptype2 = type2;
5320 if (ANY_INTEGRAL_TYPE_P (type1)
5321 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5324 if (!semantic_result_type)
5325 semantic_result_type = c_common_type (type1, type2);
5327 else if (ANY_INTEGRAL_TYPE_P (type2)
5328 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5331 if (!semantic_result_type)
5332 semantic_result_type = c_common_type (type1, type2);
5335 result_type = c_common_type (eptype1, eptype2);
5336 if (result_type == error_mark_node)
5337 return error_mark_node;
5338 do_warn_double_promotion (result_type, type1, type2,
5339 "implicit conversion from %qT to %qT to "
5340 "match other result of conditional",
5343 /* If -Wsign-compare, warn here if type1 and type2 have
5344 different signedness. We'll promote the signed to unsigned
5345 and later code won't know it used to be different.
5346 Do this check on the original types, so that explicit casts
5347 will be considered, but default promotions won't. */
5348 if (c_inhibit_evaluation_warnings == 0)
5350 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5351 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5353 if (unsigned_op1 ^ unsigned_op2)
5357 /* Do not warn if the result type is signed, since the
5358 signed type will only be chosen if it can represent
5359 all the values of the unsigned type. */
5360 if (!TYPE_UNSIGNED (result_type))
5364 bool op1_maybe_const = true;
5365 bool op2_maybe_const = true;
5367 /* Do not warn if the signed quantity is an
5368 unsuffixed integer literal (or some static
5369 constant expression involving such literals) and
5370 it is non-negative. This warning requires the
5371 operands to be folded for best results, so do
5372 that folding in this case even without
5373 warn_sign_compare to avoid warning options
5374 possibly affecting code generation. */
5375 c_inhibit_evaluation_warnings
5376 += (ifexp == truthvalue_false_node);
5377 op1 = c_fully_fold (op1, require_constant_value,
5379 c_inhibit_evaluation_warnings
5380 -= (ifexp == truthvalue_false_node);
5382 c_inhibit_evaluation_warnings
5383 += (ifexp == truthvalue_true_node);
5384 op2 = c_fully_fold (op2, require_constant_value,
5386 c_inhibit_evaluation_warnings
5387 -= (ifexp == truthvalue_true_node);
5389 if (warn_sign_compare)
5392 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5394 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5396 else if (unsigned_op2)
5397 warning_at (op1_loc, OPT_Wsign_compare,
5398 "operand of %<?:%> changes signedness from "
5399 "%qT to %qT due to unsignedness of other "
5400 "operand", TREE_TYPE (orig_op1),
5401 TREE_TYPE (orig_op2));
5403 warning_at (op2_loc, OPT_Wsign_compare,
5404 "operand of %<?:%> changes signedness from "
5405 "%qT to %qT due to unsignedness of other "
5406 "operand", TREE_TYPE (orig_op2),
5407 TREE_TYPE (orig_op1));
5409 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5410 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5411 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5412 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5417 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5419 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5420 pedwarn (colon_loc, OPT_Wpedantic,
5421 "ISO C forbids conditional expr with only one void side");
5422 result_type = void_type_node;
5424 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5426 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5427 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5428 addr_space_t as_common;
5430 if (comp_target_types (colon_loc, type1, type2))
5431 result_type = common_pointer_type (type1, type2);
5432 else if (null_pointer_constant_p (orig_op1))
5433 result_type = type2;
5434 else if (null_pointer_constant_p (orig_op2))
5435 result_type = type1;
5436 else if (!addr_space_superset (as1, as2, &as_common))
5438 error_at (colon_loc, "pointers to disjoint address spaces "
5439 "used in conditional expression");
5440 return error_mark_node;
5442 else if ((VOID_TYPE_P (TREE_TYPE (type1))
5443 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5444 || (VOID_TYPE_P (TREE_TYPE (type2))
5445 && !TYPE_ATOMIC (TREE_TYPE (type2))))
5447 tree t1 = TREE_TYPE (type1);
5448 tree t2 = TREE_TYPE (type2);
5449 if (!(VOID_TYPE_P (t1)
5450 && !TYPE_ATOMIC (t1)))
5452 /* roles are swapped */
5454 t2 = TREE_TYPE (type1);
5456 tree t2_stripped = strip_array_types (t2);
5457 if ((TREE_CODE (t2) == ARRAY_TYPE)
5458 && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5461 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5462 "pointer to array loses qualifier "
5463 "in conditional expression");
5464 else if (warn_c11_c2x_compat > 0)
5465 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5466 "pointer to array loses qualifier "
5467 "in conditional expression in ISO C before C2X");
5469 if (TREE_CODE (t2) == FUNCTION_TYPE)
5470 pedwarn (colon_loc, OPT_Wpedantic,
5471 "ISO C forbids conditional expr between "
5472 "%<void *%> and function pointer");
5473 /* for array, use qualifiers of element type */
5476 result_type = build_pointer_type (qualify_type (t1, t2));
5478 /* Objective-C pointer comparisons are a bit more lenient. */
5479 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5480 result_type = objc_common_type (type1, type2);
5483 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5484 if (bltin1 && bltin2)
5485 warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5486 "pointer type mismatch between %qT and %qT "
5487 "of %qD and %qD in conditional expression",
5488 type1, type2, bltin1, bltin2);
5490 pedwarn (colon_loc, 0,
5491 "pointer type mismatch in conditional expression");
5492 result_type = build_pointer_type
5493 (build_qualified_type (void_type_node, qual));
5496 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5498 if (!null_pointer_constant_p (orig_op2))
5499 pedwarn (colon_loc, 0,
5500 "pointer/integer type mismatch in conditional expression");
5503 op2 = null_pointer_node;
5505 result_type = type1;
5507 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5509 if (!null_pointer_constant_p (orig_op1))
5510 pedwarn (colon_loc, 0,
5511 "pointer/integer type mismatch in conditional expression");
5514 op1 = null_pointer_node;
5516 result_type = type2;
5521 if (flag_cond_mismatch)
5522 result_type = void_type_node;
5525 error_at (colon_loc, "type mismatch in conditional expression");
5526 return error_mark_node;
5530 /* Merge const and volatile flags of the incoming types. */
5532 = build_type_variant (result_type,
5533 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5534 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5536 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5537 semantic_result_type);
5538 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5539 semantic_result_type);
5541 if (ifexp_bcp && ifexp == truthvalue_true_node)
5543 op2_int_operands = true;
5544 op1 = c_fully_fold (op1, require_constant_value, NULL);
5546 if (ifexp_bcp && ifexp == truthvalue_false_node)
5548 op1_int_operands = true;
5549 op2 = c_fully_fold (op2, require_constant_value, NULL);
5551 int_const = int_operands = (ifexp_int_operands
5553 && op2_int_operands);
5556 int_const = ((ifexp == truthvalue_true_node
5557 && TREE_CODE (orig_op1) == INTEGER_CST
5558 && !TREE_OVERFLOW (orig_op1))
5559 || (ifexp == truthvalue_false_node
5560 && TREE_CODE (orig_op2) == INTEGER_CST
5561 && !TREE_OVERFLOW (orig_op2)));
5564 /* Need to convert condition operand into a vector mask. */
5565 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5567 tree vectype = TREE_TYPE (ifexp);
5568 tree elem_type = TREE_TYPE (vectype);
5569 tree zero = build_int_cst (elem_type, 0);
5570 tree zero_vec = build_vector_from_val (vectype, zero);
5571 tree cmp_type = truth_type_for (vectype);
5572 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5575 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5576 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5581 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5582 nested inside of the expression. */
5583 op1 = c_fully_fold (op1, false, NULL);
5584 op2 = c_fully_fold (op2, false, NULL);
5586 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5588 ret = note_integer_operands (ret);
5590 if (semantic_result_type)
5591 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5593 protected_set_expr_location (ret, colon_loc);
5595 /* If the OP1 and OP2 are the same and don't have side-effects,
5596 warn here, because the COND_EXPR will be turned into OP1. */
5597 if (warn_duplicated_branches
5598 && TREE_CODE (ret) == COND_EXPR
5599 && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5600 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5601 "this condition has identical branches");
5606 /* EXPR is an expression, location LOC, whose result is discarded.
5607 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5608 whose right-hand operand is such a call, possibly recursively). */
5611 maybe_warn_nodiscard (location_t loc, tree expr)
5613 if (VOID_TYPE_P (TREE_TYPE (expr)))
5615 while (TREE_CODE (expr) == COMPOUND_EXPR)
5617 expr = TREE_OPERAND (expr, 1);
5618 if (EXPR_HAS_LOCATION (expr))
5619 loc = EXPR_LOCATION (expr);
5621 if (TREE_CODE (expr) != CALL_EXPR)
5623 tree fn = CALL_EXPR_FN (expr);
5627 if (TREE_CODE (fn) == ADDR_EXPR
5628 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5629 && (attr = lookup_attribute ("nodiscard",
5630 DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5632 fn = TREE_OPERAND (fn, 0);
5633 tree args = TREE_VALUE (attr);
5635 args = TREE_VALUE (args);
5636 auto_diagnostic_group d;
5639 warned = warning_at (loc, OPT_Wunused_result,
5640 "ignoring return value of %qD, declared with "
5641 "attribute %<nodiscard%>: %E", fn, args);
5643 warned = warning_at (loc, OPT_Wunused_result,
5644 "ignoring return value of %qD, declared with "
5645 "attribute %<nodiscard%>", fn);
5647 inform (DECL_SOURCE_LOCATION (fn), "declared here");
5651 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5652 attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5655 tree args = TREE_VALUE (attr);
5657 args = TREE_VALUE (args);
5658 auto_diagnostic_group d;
5661 warned = warning_at (loc, OPT_Wunused_result,
5662 "ignoring return value of type %qT, declared "
5663 "with attribute %<nodiscard%>: %E",
5666 warned = warning_at (loc, OPT_Wunused_result,
5667 "ignoring return value of type %qT, declared "
5668 "with attribute %<nodiscard%>", rettype);
5671 if (TREE_CODE (fn) == ADDR_EXPR)
5673 fn = TREE_OPERAND (fn, 0);
5674 if (TREE_CODE (fn) == FUNCTION_DECL)
5675 inform (DECL_SOURCE_LOCATION (fn),
5676 "in call to %qD, declared here", fn);
5682 /* Return a compound expression that performs two expressions and
5683 returns the value of the second of them.
5685 LOC is the location of the COMPOUND_EXPR. */
5688 build_compound_expr (location_t loc, tree expr1, tree expr2)
5690 bool expr1_int_operands, expr2_int_operands;
5691 tree eptype = NULL_TREE;
5694 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5695 if (expr1_int_operands)
5696 expr1 = remove_c_maybe_const_expr (expr1);
5697 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5698 if (expr2_int_operands)
5699 expr2 = remove_c_maybe_const_expr (expr2);
5701 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5702 expr1 = TREE_OPERAND (expr1, 0);
5703 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5705 eptype = TREE_TYPE (expr2);
5706 expr2 = TREE_OPERAND (expr2, 0);
5709 if (!TREE_SIDE_EFFECTS (expr1))
5711 /* The left-hand operand of a comma expression is like an expression
5712 statement: with -Wunused, we should warn if it doesn't have
5713 any side-effects, unless it was explicitly cast to (void). */
5714 if (warn_unused_value)
5716 if (VOID_TYPE_P (TREE_TYPE (expr1))
5717 && CONVERT_EXPR_P (expr1))
5719 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5720 && TREE_CODE (expr1) == COMPOUND_EXPR
5721 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5722 ; /* (void) a, (void) b, c */
5724 warning_at (loc, OPT_Wunused_value,
5725 "left-hand operand of comma expression has no effect");
5728 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5729 && warn_unused_value)
5732 location_t cloc = loc;
5733 while (TREE_CODE (r) == COMPOUND_EXPR)
5735 if (EXPR_HAS_LOCATION (r))
5736 cloc = EXPR_LOCATION (r);
5737 r = TREE_OPERAND (r, 1);
5739 if (!TREE_SIDE_EFFECTS (r)
5740 && !VOID_TYPE_P (TREE_TYPE (r))
5741 && !CONVERT_EXPR_P (r))
5742 warning_at (cloc, OPT_Wunused_value,
5743 "right-hand operand of comma expression has no effect");
5746 /* With -Wunused, we should also warn if the left-hand operand does have
5747 side-effects, but computes a value which is not used. For example, in
5748 `foo() + bar(), baz()' the result of the `+' operator is not used,
5749 so we should issue a warning. */
5750 else if (warn_unused_value)
5751 warn_if_unused_value (expr1, loc);
5753 maybe_warn_nodiscard (loc, expr1);
5755 if (expr2 == error_mark_node)
5756 return error_mark_node;
5758 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5761 && expr1_int_operands
5762 && expr2_int_operands)
5763 ret = note_integer_operands (ret);
5766 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5768 protected_set_expr_location (ret, loc);
5772 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5773 which we are casting. OTYPE is the type of the expression being
5774 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5775 of the cast. -Wcast-qual appeared on the command line. Named
5776 address space qualifiers are not handled here, because they result
5777 in different warnings. */
5780 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5782 tree in_type = type;
5783 tree in_otype = otype;
5788 /* Check that the qualifiers on IN_TYPE are a superset of the
5789 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5790 nodes is uninteresting and we stop as soon as we hit a
5791 non-POINTER_TYPE node on either type. */
5794 in_otype = TREE_TYPE (in_otype);
5795 in_type = TREE_TYPE (in_type);
5797 /* GNU C allows cv-qualified function types. 'const' means the
5798 function is very pure, 'volatile' means it can't return. We
5799 need to warn when such qualifiers are added, not when they're
5801 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5802 && TREE_CODE (in_type) == FUNCTION_TYPE)
5803 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5804 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5806 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5807 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5809 while (TREE_CODE (in_type) == POINTER_TYPE
5810 && TREE_CODE (in_otype) == POINTER_TYPE);
5813 warning_at (loc, OPT_Wcast_qual,
5814 "cast adds %q#v qualifier to function type", added);
5817 /* There are qualifiers present in IN_OTYPE that are not present
5819 warning_at (loc, OPT_Wcast_qual,
5820 "cast discards %qv qualifier from pointer target type",
5823 if (added || discarded)
5826 /* A cast from **T to const **T is unsafe, because it can cause a
5827 const value to be changed with no additional warning. We only
5828 issue this warning if T is the same on both sides, and we only
5829 issue the warning if there are the same number of pointers on
5830 both sides, as otherwise the cast is clearly unsafe anyhow. A
5831 cast is unsafe when a qualifier is added at one level and const
5832 is not present at all outer levels.
5834 To issue this warning, we check at each level whether the cast
5835 adds new qualifiers not already seen. We don't need to special
5836 case function types, as they won't have the same
5837 TYPE_MAIN_VARIANT. */
5839 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5841 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5846 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5849 in_type = TREE_TYPE (in_type);
5850 in_otype = TREE_TYPE (in_otype);
5851 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5854 warning_at (loc, OPT_Wcast_qual,
5855 "to be safe all intermediate pointers in cast from "
5856 "%qT to %qT must be %<const%> qualified",
5861 is_const = TYPE_READONLY (in_type);
5863 while (TREE_CODE (in_type) == POINTER_TYPE);
5866 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5869 c_safe_arg_type_equiv_p (tree t1, tree t2)
5871 t1 = TYPE_MAIN_VARIANT (t1);
5872 t2 = TYPE_MAIN_VARIANT (t2);
5874 if (TREE_CODE (t1) == POINTER_TYPE
5875 && TREE_CODE (t2) == POINTER_TYPE)
5878 /* The signedness of the parameter matters only when an integral
5879 type smaller than int is promoted to int, otherwise only the
5880 precision of the parameter matters.
5881 This check should make sure that the callee does not see
5882 undefined values in argument registers. */
5883 if (INTEGRAL_TYPE_P (t1)
5884 && INTEGRAL_TYPE_P (t2)
5885 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5886 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5887 || !targetm.calls.promote_prototypes (NULL_TREE)
5888 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5891 return comptypes (t1, t2);
5894 /* Check if a type cast between two function types can be considered safe. */
5897 c_safe_function_type_cast_p (tree t1, tree t2)
5899 if (TREE_TYPE (t1) == void_type_node &&
5900 TYPE_ARG_TYPES (t1) == void_list_node)
5903 if (TREE_TYPE (t2) == void_type_node &&
5904 TYPE_ARG_TYPES (t2) == void_list_node)
5907 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5910 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5912 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5913 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5919 /* Build an expression representing a cast to type TYPE of expression EXPR.
5920 LOC is the location of the cast-- typically the open paren of the cast. */
5923 build_c_cast (location_t loc, tree type, tree expr)
5927 bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5929 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5930 expr = TREE_OPERAND (expr, 0);
5934 value = remove_c_maybe_const_expr (value);
5936 if (type == error_mark_node || expr == error_mark_node)
5937 return error_mark_node;
5939 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5940 only in <protocol> qualifications. But when constructing cast expressions,
5941 the protocols do matter and must be kept around. */
5942 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5943 return build1 (NOP_EXPR, type, expr);
5945 type = TYPE_MAIN_VARIANT (type);
5947 if (TREE_CODE (type) == ARRAY_TYPE)
5949 error_at (loc, "cast specifies array type");
5950 return error_mark_node;
5953 if (TREE_CODE (type) == FUNCTION_TYPE)
5955 error_at (loc, "cast specifies function type");
5956 return error_mark_node;
5959 if (!VOID_TYPE_P (type))
5961 value = require_complete_type (loc, value);
5962 if (value == error_mark_node)
5963 return error_mark_node;
5966 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5968 if (RECORD_OR_UNION_TYPE_P (type))
5969 pedwarn (loc, OPT_Wpedantic,
5970 "ISO C forbids casting nonscalar to the same type");
5972 /* Convert to remove any qualifiers from VALUE's type. */
5973 value = convert (type, value);
5975 else if (TREE_CODE (type) == UNION_TYPE)
5979 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5980 if (TREE_TYPE (field) != error_mark_node
5981 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5982 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5988 bool maybe_const = true;
5990 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5991 t = c_fully_fold (value, false, &maybe_const);
5992 t = build_constructor_single (type, field, t);
5994 t = c_wrap_maybe_const (t, true);
5995 t = digest_init (loc, type, t,
5996 NULL_TREE, false, true, 0);
5997 TREE_CONSTANT (t) = TREE_CONSTANT (value);
6000 error_at (loc, "cast to union type from type not present in union");
6001 return error_mark_node;
6007 if (type == void_type_node)
6009 tree t = build1 (CONVERT_EXPR, type, value);
6010 SET_EXPR_LOCATION (t, loc);
6014 otype = TREE_TYPE (value);
6016 /* Optionally warn about potentially worrisome casts. */
6018 && TREE_CODE (type) == POINTER_TYPE
6019 && TREE_CODE (otype) == POINTER_TYPE)
6020 handle_warn_cast_qual (loc, type, otype);
6022 /* Warn about conversions between pointers to disjoint
6024 if (TREE_CODE (type) == POINTER_TYPE
6025 && TREE_CODE (otype) == POINTER_TYPE
6026 && !null_pointer_constant_p (value))
6028 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6029 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6030 addr_space_t as_common;
6032 if (!addr_space_superset (as_to, as_from, &as_common))
6034 if (ADDR_SPACE_GENERIC_P (as_from))
6035 warning_at (loc, 0, "cast to %qs address space pointer "
6036 "from disjoint generic address space pointer",
6037 c_addr_space_name (as_to));
6039 else if (ADDR_SPACE_GENERIC_P (as_to))
6040 warning_at (loc, 0, "cast to generic address space pointer "
6041 "from disjoint %qs address space pointer",
6042 c_addr_space_name (as_from));
6045 warning_at (loc, 0, "cast to %qs address space pointer "
6046 "from disjoint %qs address space pointer",
6047 c_addr_space_name (as_to),
6048 c_addr_space_name (as_from));
6052 /* Warn about possible alignment problems. */
6053 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6054 && TREE_CODE (type) == POINTER_TYPE
6055 && TREE_CODE (otype) == POINTER_TYPE
6056 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6057 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6058 /* Don't warn about opaque types, where the actual alignment
6059 restriction is unknown. */
6060 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6061 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6062 && min_align_of_type (TREE_TYPE (type))
6063 > min_align_of_type (TREE_TYPE (otype)))
6064 warning_at (loc, OPT_Wcast_align,
6065 "cast increases required alignment of target type");
6067 if (TREE_CODE (type) == INTEGER_TYPE
6068 && TREE_CODE (otype) == POINTER_TYPE
6069 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6070 /* Unlike conversion of integers to pointers, where the
6071 warning is disabled for converting constants because
6072 of cases such as SIG_*, warn about converting constant
6073 pointers to integers. In some cases it may cause unwanted
6074 sign extension, and a warning is appropriate. */
6075 warning_at (loc, OPT_Wpointer_to_int_cast,
6076 "cast from pointer to integer of different size");
6078 if (TREE_CODE (value) == CALL_EXPR
6079 && TREE_CODE (type) != TREE_CODE (otype))
6080 warning_at (loc, OPT_Wbad_function_cast,
6081 "cast from function call of type %qT "
6082 "to non-matching type %qT", otype, type);
6084 if (TREE_CODE (type) == POINTER_TYPE
6085 && TREE_CODE (otype) == INTEGER_TYPE
6086 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6087 /* Don't warn about converting any constant. */
6088 && !TREE_CONSTANT (value))
6090 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6091 "of different size");
6093 if (warn_strict_aliasing <= 2)
6094 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6096 /* If pedantic, warn for conversions between function and object
6097 pointer types, except for converting a null pointer constant
6098 to function pointer type. */
6100 && TREE_CODE (type) == POINTER_TYPE
6101 && TREE_CODE (otype) == POINTER_TYPE
6102 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6103 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6104 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6105 "conversion of function pointer to object pointer type");
6108 && TREE_CODE (type) == POINTER_TYPE
6109 && TREE_CODE (otype) == POINTER_TYPE
6110 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6111 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6112 && !null_pointer_constant_p (value))
6113 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6114 "conversion of object pointer to function pointer type");
6116 if (TREE_CODE (type) == POINTER_TYPE
6117 && TREE_CODE (otype) == POINTER_TYPE
6118 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6119 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6120 && !c_safe_function_type_cast_p (TREE_TYPE (type),
6122 warning_at (loc, OPT_Wcast_function_type,
6123 "cast between incompatible function types"
6124 " from %qT to %qT", otype, type);
6127 value = convert (type, value);
6129 /* Ignore any integer overflow caused by the cast. */
6130 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6132 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6134 if (!TREE_OVERFLOW (value))
6136 /* Avoid clobbering a shared constant. */
6137 value = copy_node (value);
6138 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6141 else if (TREE_OVERFLOW (value))
6142 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6143 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6147 /* Don't let a cast be an lvalue. */
6148 if (lvalue_p (value))
6149 value = non_lvalue_loc (loc, value);
6151 /* Don't allow the results of casting to floating-point or complex
6152 types be confused with actual constants, or casts involving
6153 integer and pointer types other than direct integer-to-integer
6154 and integer-to-pointer be confused with integer constant
6155 expressions and null pointer constants. */
6156 if (TREE_CODE (value) == REAL_CST
6157 || TREE_CODE (value) == COMPLEX_CST
6158 || (TREE_CODE (value) == INTEGER_CST
6159 && !((TREE_CODE (expr) == INTEGER_CST
6160 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6161 || TREE_CODE (expr) == REAL_CST
6162 || TREE_CODE (expr) == COMPLEX_CST)))
6163 value = build1 (NOP_EXPR, type, value);
6165 /* If the expression has integer operands and so can occur in an
6166 unevaluated part of an integer constant expression, ensure the
6167 return value reflects this. */
6169 && INTEGRAL_TYPE_P (type)
6170 && value != error_mark_node
6171 && !EXPR_INT_CONST_OPERANDS (value))
6172 value = note_integer_operands (value);
6174 protected_set_expr_location (value, loc);
6178 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6179 location of the open paren of the cast, or the position of the cast
6182 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6185 tree type_expr = NULL_TREE;
6186 bool type_expr_const = true;
6188 int saved_wsp = warn_strict_prototypes;
6190 /* This avoids warnings about unprototyped casts on
6191 integers. E.g. "#define SIG_DFL (void(*)())0". */
6192 if (TREE_CODE (expr) == INTEGER_CST)
6193 warn_strict_prototypes = 0;
6194 type = groktypename (type_name, &type_expr, &type_expr_const);
6195 warn_strict_prototypes = saved_wsp;
6197 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6198 && reject_gcc_builtin (expr))
6199 return error_mark_node;
6201 ret = build_c_cast (loc, type, expr);
6204 bool inner_expr_const = true;
6205 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6206 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6207 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6208 && inner_expr_const);
6209 SET_EXPR_LOCATION (ret, loc);
6212 if (!EXPR_HAS_LOCATION (ret))
6213 protected_set_expr_location (ret, loc);
6215 /* C++ does not permits types to be defined in a cast, but it
6216 allows references to incomplete types. */
6217 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6218 warning_at (loc, OPT_Wc___compat,
6219 "defining a type in a cast is invalid in C++");
6224 /* Build an assignment expression of lvalue LHS from value RHS.
6225 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6226 may differ from TREE_TYPE (LHS) for an enum bitfield.
6227 MODIFYCODE is the code for a binary operator that we use
6228 to combine the old value of LHS with RHS to get the new value.
6229 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6230 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6231 which may differ from TREE_TYPE (RHS) for an enum value.
6233 LOCATION is the location of the MODIFYCODE operator.
6234 RHS_LOC is the location of the RHS. */
6237 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6238 enum tree_code modifycode,
6239 location_t rhs_loc, tree rhs, tree rhs_origtype)
6243 tree rhseval = NULL_TREE;
6244 tree lhstype = TREE_TYPE (lhs);
6245 tree olhstype = lhstype;
6249 /* Types that aren't fully specified cannot be used in assignments. */
6250 lhs = require_complete_type (location, lhs);
6252 /* Avoid duplicate error messages from operands that had errors. */
6253 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6254 return error_mark_node;
6256 /* Ensure an error for assigning a non-lvalue array to an array in
6258 if (TREE_CODE (lhstype) == ARRAY_TYPE)
6260 error_at (location, "assignment to expression with array type");
6261 return error_mark_node;
6264 /* For ObjC properties, defer this check. */
6265 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6266 return error_mark_node;
6268 is_atomic_op = really_atomic_lvalue (lhs);
6272 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6274 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6275 lhs_origtype, modifycode, rhs_loc, rhs,
6277 if (inner == error_mark_node)
6278 return error_mark_node;
6279 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6280 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6281 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6282 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6283 protected_set_expr_location (result, location);
6287 /* If a binary op has been requested, combine the old LHS value with the RHS
6288 producing the value we should actually store into the LHS. */
6290 if (modifycode != NOP_EXPR)
6292 lhs = c_fully_fold (lhs, false, NULL, true);
6293 lhs = stabilize_reference (lhs);
6295 /* Construct the RHS for any non-atomic compound assignemnt. */
6298 /* If in LHS op= RHS the RHS has side-effects, ensure they
6299 are preevaluated before the rest of the assignment expression's
6300 side-effects, because RHS could contain e.g. function calls
6302 if (TREE_SIDE_EFFECTS (rhs))
6304 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6305 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6307 newrhs = save_expr (rhs);
6309 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6310 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6313 newrhs = build_binary_op (location,
6314 modifycode, lhs, newrhs, true);
6316 /* The original type of the right hand side is no longer
6318 rhs_origtype = NULL_TREE;
6322 if (c_dialect_objc ())
6324 /* Check if we are modifying an Objective-C property reference;
6325 if so, we need to generate setter calls. */
6326 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6327 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6329 result = objc_maybe_build_modify_expr (lhs, newrhs);
6333 /* Else, do the check that we postponed for Objective-C. */
6334 if (!lvalue_or_else (location, lhs, lv_assign))
6335 return error_mark_node;
6338 /* Give an error for storing in something that is 'const'. */
6340 if (TYPE_READONLY (lhstype)
6341 || (RECORD_OR_UNION_TYPE_P (lhstype)
6342 && C_TYPE_FIELDS_READONLY (lhstype)))
6344 readonly_error (location, lhs, lv_assign);
6345 return error_mark_node;
6347 else if (TREE_READONLY (lhs))
6348 readonly_warning (lhs, lv_assign);
6350 /* If storing into a structure or union member,
6351 it has probably been given type `int'.
6352 Compute the type that would go with
6353 the actual amount of storage the member occupies. */
6355 if (TREE_CODE (lhs) == COMPONENT_REF
6356 && (TREE_CODE (lhstype) == INTEGER_TYPE
6357 || TREE_CODE (lhstype) == BOOLEAN_TYPE
6358 || TREE_CODE (lhstype) == REAL_TYPE
6359 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6360 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6362 /* If storing in a field that is in actuality a short or narrower than one,
6363 we must store in the field in its actual type. */
6365 if (lhstype != TREE_TYPE (lhs))
6367 lhs = copy_node (lhs);
6368 TREE_TYPE (lhs) = lhstype;
6371 /* Issue -Wc++-compat warnings about an assignment to an enum type
6372 when LHS does not have its original type. This happens for,
6373 e.g., an enum bitfield in a struct. */
6375 && lhs_origtype != NULL_TREE
6376 && lhs_origtype != lhstype
6377 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6379 tree checktype = (rhs_origtype != NULL_TREE
6382 if (checktype != error_mark_node
6383 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6384 || (is_atomic_op && modifycode != NOP_EXPR)))
6385 warning_at (location, OPT_Wc___compat,
6386 "enum conversion in assignment is invalid in C++");
6389 /* Remove qualifiers. */
6390 lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6391 olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6393 /* Convert new value to destination type. Fold it first, then
6394 restore any excess precision information, for the sake of
6395 conversion warnings. */
6397 if (!(is_atomic_op && modifycode != NOP_EXPR))
6399 tree rhs_semantic_type = NULL_TREE;
6402 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6404 rhs_semantic_type = TREE_TYPE (newrhs);
6405 newrhs = TREE_OPERAND (newrhs, 0);
6407 npc = null_pointer_constant_p (newrhs);
6408 newrhs = c_fully_fold (newrhs, false, NULL);
6409 if (rhs_semantic_type)
6410 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6413 npc = null_pointer_constant_p (newrhs);
6414 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6415 rhs_origtype, ic_assign, npc,
6416 NULL_TREE, NULL_TREE, 0);
6417 if (TREE_CODE (newrhs) == ERROR_MARK)
6418 return error_mark_node;
6421 /* Emit ObjC write barrier, if necessary. */
6422 if (c_dialect_objc () && flag_objc_gc)
6424 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6427 protected_set_expr_location (result, location);
6432 /* Scan operands. */
6435 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6438 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6439 TREE_SIDE_EFFECTS (result) = 1;
6440 protected_set_expr_location (result, location);
6443 /* If we got the LHS in a different type for storing in,
6444 convert the result back to the nominal type of LHS
6445 so that the value we return always has the same type
6446 as the LHS argument. */
6448 if (olhstype == TREE_TYPE (result))
6451 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6452 rhs_origtype, ic_assign, false, NULL_TREE,
6454 protected_set_expr_location (result, location);
6458 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6462 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6463 This is used to implement -fplan9-extensions. */
6466 find_anonymous_field_with_type (tree struct_type, tree type)
6471 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6473 for (field = TYPE_FIELDS (struct_type);
6475 field = TREE_CHAIN (field))
6477 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6478 ? c_build_qualified_type (TREE_TYPE (field),
6480 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6481 if (DECL_NAME (field) == NULL
6482 && comptypes (type, fieldtype))
6488 else if (DECL_NAME (field) == NULL
6489 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6490 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6500 /* RHS is an expression whose type is pointer to struct. If there is
6501 an anonymous field in RHS with type TYPE, then return a pointer to
6502 that field in RHS. This is used with -fplan9-extensions. This
6503 returns NULL if no conversion could be found. */
6506 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6508 tree rhs_struct_type, lhs_main_type;
6509 tree field, found_field;
6510 bool found_sub_field;
6513 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6514 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6515 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6517 gcc_assert (POINTER_TYPE_P (type));
6518 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6519 ? c_build_qualified_type (TREE_TYPE (type),
6521 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6523 found_field = NULL_TREE;
6524 found_sub_field = false;
6525 for (field = TYPE_FIELDS (rhs_struct_type);
6527 field = TREE_CHAIN (field))
6529 if (DECL_NAME (field) != NULL_TREE
6530 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6532 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6533 ? c_build_qualified_type (TREE_TYPE (field),
6535 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6536 if (comptypes (lhs_main_type, fieldtype))
6538 if (found_field != NULL_TREE)
6540 found_field = field;
6542 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6545 if (found_field != NULL_TREE)
6547 found_field = field;
6548 found_sub_field = true;
6552 if (found_field == NULL_TREE)
6555 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6556 build_fold_indirect_ref (rhs), found_field,
6558 ret = build_fold_addr_expr_loc (location, ret);
6560 if (found_sub_field)
6562 ret = convert_to_anonymous_field (location, type, ret);
6563 gcc_assert (ret != NULL_TREE);
6569 /* Issue an error message for a bad initializer component.
6570 GMSGID identifies the message.
6571 The component name is taken from the spelling stack. */
6573 static void ATTRIBUTE_GCC_DIAG (2,0)
6574 error_init (location_t loc, const char *gmsgid, ...)
6578 auto_diagnostic_group d;
6580 /* The gmsgid may be a format string with %< and %>. */
6582 va_start (ap, gmsgid);
6583 bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6586 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6587 if (*ofwhat && warned)
6588 inform (loc, "(near initialization for %qs)", ofwhat);
6591 /* Issue a pedantic warning for a bad initializer component. OPT is
6592 the option OPT_* (from options.h) controlling this warning or 0 if
6593 it is unconditionally given. GMSGID identifies the message. The
6594 component name is taken from the spelling stack. */
6596 static void ATTRIBUTE_GCC_DIAG (3,0)
6597 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6599 /* Use the location where a macro was expanded rather than where
6600 it was defined to make sure macros defined in system headers
6601 but used incorrectly elsewhere are diagnosed. */
6602 location_t exploc = expansion_point_location_if_in_system_header (loc);
6603 auto_diagnostic_group d;
6605 va_start (ap, gmsgid);
6606 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6608 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6609 if (*ofwhat && warned)
6610 inform (exploc, "(near initialization for %qs)", ofwhat);
6613 /* Issue a warning for a bad initializer component.
6615 OPT is the OPT_W* value corresponding to the warning option that
6616 controls this warning. GMSGID identifies the message. The
6617 component name is taken from the spelling stack. */
6620 warning_init (location_t loc, int opt, const char *gmsgid)
6625 auto_diagnostic_group d;
6627 /* Use the location where a macro was expanded rather than where
6628 it was defined to make sure macros defined in system headers
6629 but used incorrectly elsewhere are diagnosed. */
6630 location_t exploc = expansion_point_location_if_in_system_header (loc);
6632 /* The gmsgid may be a format string with %< and %>. */
6633 warned = warning_at (exploc, opt, gmsgid);
6634 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6635 if (*ofwhat && warned)
6636 inform (exploc, "(near initialization for %qs)", ofwhat);
6639 /* If TYPE is an array type and EXPR is a parenthesized string
6640 constant, warn if pedantic that EXPR is being used to initialize an
6641 object of type TYPE. */
6644 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6647 && TREE_CODE (type) == ARRAY_TYPE
6648 && TREE_CODE (expr.value) == STRING_CST
6649 && expr.original_code != STRING_CST)
6650 pedwarn_init (loc, OPT_Wpedantic,
6651 "array initialized from parenthesized string constant");
6654 /* Attempt to locate the parameter with the given index within FNDECL,
6655 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6658 get_fndecl_argument_location (tree fndecl, int argnum)
6663 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6664 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6665 i < argnum && param;
6666 i++, param = TREE_CHAIN (param))
6669 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6670 return DECL_SOURCE_LOCATION (FNDECL). */
6672 return DECL_SOURCE_LOCATION (fndecl);
6674 return DECL_SOURCE_LOCATION (param);
6677 /* Issue a note about a mismatching argument for parameter PARMNUM
6678 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6679 Attempt to issue the note at the pertinent parameter of the decl;
6680 failing that issue it at the location of FUNDECL; failing that
6681 issue it at PLOC. */
6684 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6685 tree expected_type, tree actual_type)
6688 if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6689 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6694 "expected %qT but argument is of type %qT",
6695 expected_type, actual_type);
6698 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6699 function FUNDECL declared without prototype to parameter PARMNUM of
6700 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6703 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6704 tree parmtype, tree argtype)
6706 tree_code parmcode = TREE_CODE (parmtype);
6707 tree_code argcode = TREE_CODE (argtype);
6708 tree promoted = c_type_promotes_to (argtype);
6710 /* Avoid warning for enum arguments that promote to an integer type
6711 of the same size/mode. */
6712 if (parmcode == INTEGER_TYPE
6713 && argcode == ENUMERAL_TYPE
6714 && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6717 if ((parmcode == argcode
6718 || (parmcode == INTEGER_TYPE
6719 && argcode == ENUMERAL_TYPE))
6720 && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6723 /* This diagnoses even signed/unsigned mismatches. Those might be
6724 safe in many cases but GCC may emit suboptimal code for them so
6725 warning on those cases drives efficiency improvements. */
6726 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6727 TYPE_MAIN_VARIANT (promoted) == argtype
6728 ? G_("%qD argument %d type is %qT where %qT is expected "
6729 "in a call to built-in function declared without "
6731 : G_("%qD argument %d promotes to %qT where %qT is expected "
6732 "in a call to built-in function declared without "
6734 fundecl, parmnum, promoted, parmtype))
6735 inform (DECL_SOURCE_LOCATION (fundecl),
6736 "built-in %qD declared here",
6740 /* Convert value RHS to type TYPE as preparation for an assignment to
6741 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6742 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6743 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6744 constant before any folding.
6745 The real work of conversion is done by `convert'.
6746 The purpose of this function is to generate error messages
6747 for assignments that are not allowed in C.
6748 ERRTYPE says whether it is argument passing, assignment,
6749 initialization or return.
6751 In the following example, '~' denotes where EXPR_LOC and '^' where
6754 f (var); [ic_argpass]
6756 x = var; [ic_assign]
6758 int x = var; [ic_init]
6760 return x; [ic_return]
6763 FUNCTION is a tree for the function being called.
6764 PARMNUM is the number of the argument, for printing in error messages.
6765 WARNOPT may be set to a warning option to issue the corresponding warning
6766 rather than an error for invalid conversions. Used for calls to built-in
6767 functions declared without a prototype. */
6770 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6771 tree rhs, tree origtype, enum impl_conv errtype,
6772 bool null_pointer_constant, tree fundecl,
6773 tree function, int parmnum, int warnopt /* = 0 */)
6775 enum tree_code codel = TREE_CODE (type);
6776 tree orig_rhs = rhs;
6778 enum tree_code coder;
6779 tree rname = NULL_TREE;
6780 bool objc_ok = false;
6782 /* Use the expansion point location to handle cases such as user's
6783 function returning a wrong-type macro defined in a system header. */
6784 location = expansion_point_location_if_in_system_header (location);
6786 if (errtype == ic_argpass)
6789 /* Change pointer to function to the function itself for
6791 if (TREE_CODE (function) == ADDR_EXPR
6792 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6793 function = TREE_OPERAND (function, 0);
6795 /* Handle an ObjC selector specially for diagnostics. */
6796 selector = objc_message_selector ();
6798 if (selector && parmnum > 2)
6805 /* This macro is used to emit diagnostics to ensure that all format
6806 strings are complete sentences, visible to gettext and checked at
6808 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6814 auto_diagnostic_group d; \
6815 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6816 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6820 pedwarn (LOCATION, OPT, AS); \
6823 case ic_init_const: \
6824 pedwarn_init (LOCATION, OPT, IN); \
6827 pedwarn (LOCATION, OPT, RE); \
6830 gcc_unreachable (); \
6834 /* This macro is used to emit diagnostics to ensure that all format
6835 strings are complete sentences, visible to gettext and checked at
6836 compile time. It can be called with 'pedwarn' or 'warning_at'. */
6837 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6843 auto_diagnostic_group d; \
6845 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6846 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6848 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6849 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6855 pedwarn (LOCATION, OPT, AS, QUALS); \
6857 warning_at (LOCATION, OPT, AS, QUALS); \
6860 case ic_init_const: \
6862 pedwarn (LOCATION, OPT, IN, QUALS); \
6864 warning_at (LOCATION, OPT, IN, QUALS); \
6868 pedwarn (LOCATION, OPT, RE, QUALS); \
6870 warning_at (LOCATION, OPT, RE, QUALS); \
6873 gcc_unreachable (); \
6877 /* This macro is used to emit diagnostics to ensure that all format
6878 strings are complete sentences, visible to gettext and checked at
6879 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6880 extra parameter to enumerate qualifiers. */
6881 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6882 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
6885 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6886 rhs = TREE_OPERAND (rhs, 0);
6888 rhstype = TREE_TYPE (rhs);
6889 coder = TREE_CODE (rhstype);
6891 if (coder == ERROR_MARK)
6892 return error_mark_node;
6894 if (c_dialect_objc ())
6918 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6921 if (warn_cxx_compat)
6923 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6924 if (checktype != error_mark_node
6925 && TREE_CODE (type) == ENUMERAL_TYPE
6926 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6930 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6931 "passing argument %d of %qE is invalid in C++",
6933 inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6934 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6935 "expected %qT but argument is of type %qT",
6939 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6940 "%qT in assignment is invalid in C++", rhstype, type);
6944 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6945 "%qT to %qT in initialization is invalid in C++",
6949 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6950 "%qT in return is invalid in C++", rhstype, type);
6957 if (warn_enum_conversion)
6959 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6960 if (checktype != error_mark_node
6961 && TREE_CODE (checktype) == ENUMERAL_TYPE
6962 && TREE_CODE (type) == ENUMERAL_TYPE
6963 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6965 gcc_rich_location loc (location);
6966 warning_at (&loc, OPT_Wenum_conversion,
6967 "implicit conversion from %qT to %qT",
6972 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6974 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6978 if (coder == VOID_TYPE)
6980 /* Except for passing an argument to an unprototyped function,
6981 this is a constraint violation. When passing an argument to
6982 an unprototyped function, it is compile-time undefined;
6983 making it a constraint in that case was rejected in
6985 const char msg[] = "void value not ignored as it ought to be";
6987 warning_at (location, warnopt, msg);
6989 error_at (location, msg);
6990 return error_mark_node;
6992 rhs = require_complete_type (location, rhs);
6993 if (rhs == error_mark_node)
6994 return error_mark_node;
6996 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6997 return error_mark_node;
6999 /* A non-reference type can convert to a reference. This handles
7000 va_start, va_copy and possibly port built-ins. */
7001 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7003 if (!lvalue_p (rhs))
7005 const char msg[] = "cannot pass rvalue to reference parameter";
7007 warning_at (location, warnopt, msg);
7009 error_at (location, msg);
7010 return error_mark_node;
7012 if (!c_mark_addressable (rhs))
7013 return error_mark_node;
7014 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7015 SET_EXPR_LOCATION (rhs, location);
7017 rhs = convert_for_assignment (location, expr_loc,
7018 build_pointer_type (TREE_TYPE (type)),
7019 rhs, origtype, errtype,
7020 null_pointer_constant, fundecl, function,
7022 if (rhs == error_mark_node)
7023 return error_mark_node;
7025 rhs = build1 (NOP_EXPR, type, rhs);
7026 SET_EXPR_LOCATION (rhs, location);
7029 /* Some types can interconvert without explicit casts. */
7030 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7031 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7032 return convert (type, rhs);
7033 /* Arithmetic types all interconvert, and enum is treated like int. */
7034 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7035 || codel == FIXED_POINT_TYPE
7036 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7037 || codel == BOOLEAN_TYPE)
7038 && (coder == INTEGER_TYPE || coder == REAL_TYPE
7039 || coder == FIXED_POINT_TYPE
7040 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7041 || coder == BOOLEAN_TYPE))
7043 if (warnopt && errtype == ic_argpass)
7044 maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7047 bool save = in_late_binary_op;
7048 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
7049 || (coder == REAL_TYPE
7050 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7051 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7052 in_late_binary_op = true;
7053 tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7054 ? expr_loc : location, type, orig_rhs,
7055 errtype == ic_init_const);
7056 in_late_binary_op = save;
7060 /* Aggregates in different TUs might need conversion. */
7061 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7063 && comptypes (type, rhstype))
7064 return convert_and_check (expr_loc != UNKNOWN_LOCATION
7065 ? expr_loc : location, type, rhs);
7067 /* Conversion to a transparent union or record from its member types.
7068 This applies only to function arguments. */
7069 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7070 && TYPE_TRANSPARENT_AGGR (type))
7071 && errtype == ic_argpass)
7073 tree memb, marginal_memb = NULL_TREE;
7075 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7077 tree memb_type = TREE_TYPE (memb);
7079 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7080 TYPE_MAIN_VARIANT (rhstype)))
7083 if (TREE_CODE (memb_type) != POINTER_TYPE)
7086 if (coder == POINTER_TYPE)
7088 tree ttl = TREE_TYPE (memb_type);
7089 tree ttr = TREE_TYPE (rhstype);
7091 /* Any non-function converts to a [const][volatile] void *
7092 and vice versa; otherwise, targets must be the same.
7093 Meanwhile, the lhs target must have all the qualifiers of
7095 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7096 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7097 || comp_target_types (location, memb_type, rhstype))
7099 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7100 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7101 /* If this type won't generate any warnings, use it. */
7102 if (lquals == rquals
7103 || ((TREE_CODE (ttr) == FUNCTION_TYPE
7104 && TREE_CODE (ttl) == FUNCTION_TYPE)
7105 ? ((lquals | rquals) == rquals)
7106 : ((lquals | rquals) == lquals)))
7109 /* Keep looking for a better type, but remember this one. */
7111 marginal_memb = memb;
7115 /* Can convert integer zero to any pointer type. */
7116 if (null_pointer_constant)
7118 rhs = null_pointer_node;
7123 if (memb || marginal_memb)
7127 /* We have only a marginally acceptable member type;
7128 it needs a warning. */
7129 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7130 tree ttr = TREE_TYPE (rhstype);
7132 /* Const and volatile mean something different for function
7133 types, so the usual warnings are not appropriate. */
7134 if (TREE_CODE (ttr) == FUNCTION_TYPE
7135 && TREE_CODE (ttl) == FUNCTION_TYPE)
7137 /* Because const and volatile on functions are
7138 restrictions that say the function will not do
7139 certain things, it is okay to use a const or volatile
7140 function where an ordinary one is wanted, but not
7142 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7143 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7144 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7145 OPT_Wdiscarded_qualifiers,
7146 G_("passing argument %d of %qE "
7147 "makes %q#v qualified function "
7148 "pointer from unqualified"),
7149 G_("assignment makes %q#v qualified "
7150 "function pointer from "
7152 G_("initialization makes %q#v qualified "
7153 "function pointer from "
7155 G_("return makes %q#v qualified function "
7156 "pointer from unqualified"),
7157 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7159 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7160 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7161 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7162 OPT_Wdiscarded_qualifiers,
7163 G_("passing argument %d of %qE discards "
7164 "%qv qualifier from pointer target type"),
7165 G_("assignment discards %qv qualifier "
7166 "from pointer target type"),
7167 G_("initialization discards %qv qualifier "
7168 "from pointer target type"),
7169 G_("return discards %qv qualifier from "
7170 "pointer target type"),
7171 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7173 memb = marginal_memb;
7176 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7177 pedwarn (location, OPT_Wpedantic,
7178 "ISO C prohibits argument conversion to union type");
7180 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7181 return build_constructor_single (type, memb, rhs);
7185 /* Conversions among pointers */
7186 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7187 && (coder == codel))
7189 /* If RHS refers to a built-in declared without a prototype
7190 BLTIN is the declaration of the built-in with a prototype
7191 and RHSTYPE is set to the actual type of the built-in. */
7193 rhstype = type_or_builtin_type (rhs, &bltin);
7195 tree ttl = TREE_TYPE (type);
7196 tree ttr = TREE_TYPE (rhstype);
7199 bool is_opaque_pointer;
7200 int target_cmp = 0; /* Cache comp_target_types () result. */
7204 if (TREE_CODE (mvl) != ARRAY_TYPE)
7205 mvl = (TYPE_ATOMIC (mvl)
7206 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7208 : TYPE_MAIN_VARIANT (mvl));
7209 if (TREE_CODE (mvr) != ARRAY_TYPE)
7210 mvr = (TYPE_ATOMIC (mvr)
7211 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7213 : TYPE_MAIN_VARIANT (mvr));
7214 /* Opaque pointers are treated like void pointers. */
7215 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7217 /* The Plan 9 compiler permits a pointer to a struct to be
7218 automatically converted into a pointer to an anonymous field
7219 within the struct. */
7220 if (flag_plan9_extensions
7221 && RECORD_OR_UNION_TYPE_P (mvl)
7222 && RECORD_OR_UNION_TYPE_P (mvr)
7225 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7226 if (new_rhs != NULL_TREE)
7229 rhstype = TREE_TYPE (rhs);
7230 coder = TREE_CODE (rhstype);
7231 ttr = TREE_TYPE (rhstype);
7232 mvr = TYPE_MAIN_VARIANT (ttr);
7236 /* C++ does not allow the implicit conversion void* -> T*. However,
7237 for the purpose of reducing the number of false positives, we
7238 tolerate the special case of
7242 where NULL is typically defined in C to be '(void *) 0'. */
7243 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7244 warning_at (errtype == ic_argpass ? expr_loc : location,
7246 "request for implicit conversion "
7247 "from %qT to %qT not permitted in C++", rhstype, type);
7249 /* See if the pointers point to incompatible address spaces. */
7250 asl = TYPE_ADDR_SPACE (ttl);
7251 asr = TYPE_ADDR_SPACE (ttr);
7252 if (!null_pointer_constant_p (rhs)
7253 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7255 auto_diagnostic_group d;
7256 bool diagnosed = true;
7261 const char msg[] = G_("passing argument %d of %qE from "
7262 "pointer to non-enclosed address space");
7265 = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7267 error_at (expr_loc, msg, parmnum, rname);
7272 const char msg[] = G_("assignment from pointer to "
7273 "non-enclosed address space");
7275 diagnosed = warning_at (location, warnopt, msg);
7277 error_at (location, msg);
7283 const char msg[] = G_("initialization from pointer to "
7284 "non-enclosed address space");
7286 diagnosed = warning_at (location, warnopt, msg);
7288 error_at (location, msg);
7293 const char msg[] = G_("return from pointer to "
7294 "non-enclosed address space");
7296 diagnosed = warning_at (location, warnopt, msg);
7298 error_at (location, msg);
7306 if (errtype == ic_argpass)
7307 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7309 inform (location, "expected %qT but pointer is of type %qT",
7312 return error_mark_node;
7315 /* Check if the right-hand side has a format attribute but the
7316 left-hand side doesn't. */
7317 if (warn_suggest_attribute_format
7318 && check_missing_format_attribute (type, rhstype))
7323 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7324 "argument %d of %qE might be "
7325 "a candidate for a format attribute",
7329 warning_at (location, OPT_Wsuggest_attribute_format,
7330 "assignment left-hand side might be "
7331 "a candidate for a format attribute");
7335 warning_at (location, OPT_Wsuggest_attribute_format,
7336 "initialization left-hand side might be "
7337 "a candidate for a format attribute");
7340 warning_at (location, OPT_Wsuggest_attribute_format,
7341 "return type might be "
7342 "a candidate for a format attribute");
7349 /* See if the pointers point to incompatible scalar storage orders. */
7350 if (warn_scalar_storage_order
7351 && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7352 != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7359 /* Do not warn for built-in functions, for example memcpy, since we
7360 control how they behave and they can be useful in this area. */
7361 if (TREE_CODE (rname) != FUNCTION_DECL
7362 || !fndecl_built_in_p (rname))
7363 warning_at (location, OPT_Wscalar_storage_order,
7364 "passing argument %d of %qE from incompatible "
7365 "scalar storage order", parmnum, rname);
7368 /* Do not warn if the RHS is a call to a function that returns a
7369 pointer that is not an alias. */
7370 if (TREE_CODE (rhs) != CALL_EXPR
7371 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7372 || !DECL_IS_MALLOC (t))
7373 warning_at (location, OPT_Wscalar_storage_order,
7374 "assignment to %qT from pointer type %qT with "
7375 "incompatible scalar storage order", type, rhstype);
7380 if (TREE_CODE (rhs) != CALL_EXPR
7381 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7382 || !DECL_IS_MALLOC (t))
7383 warning_at (location, OPT_Wscalar_storage_order,
7384 "initialization of %qT from pointer type %qT with "
7385 "incompatible scalar storage order", type, rhstype);
7388 warning_at (location, OPT_Wscalar_storage_order,
7389 "returning %qT from pointer type with incompatible "
7390 "scalar storage order %qT", rhstype, type);
7397 /* Any non-function converts to a [const][volatile] void *
7398 and vice versa; otherwise, targets must be the same.
7399 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7400 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7401 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7402 || (target_cmp = comp_target_types (location, type, rhstype))
7403 || is_opaque_pointer
7404 || ((c_common_unsigned_type (mvl)
7405 == c_common_unsigned_type (mvr))
7406 && (c_common_signed_type (mvl)
7407 == c_common_signed_type (mvr))
7408 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7410 /* Warn about loss of qualifers from pointers to arrays with
7411 qualifiers on the element type. */
7412 if (TREE_CODE (ttr) == ARRAY_TYPE)
7414 ttr = strip_array_types (ttr);
7415 ttl = strip_array_types (ttl);
7417 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7418 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7419 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7421 OPT_Wdiscarded_array_qualifiers,
7422 G_("passing argument %d of %qE discards "
7423 "%qv qualifier from pointer target type"),
7424 G_("assignment discards %qv qualifier "
7425 "from pointer target type"),
7426 G_("initialization discards %qv qualifier "
7427 "from pointer target type"),
7428 G_("return discards %qv qualifier from "
7429 "pointer target type"),
7430 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7433 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7436 && !null_pointer_constant
7437 && TREE_CODE (ttl) == FUNCTION_TYPE)))
7438 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7439 G_("ISO C forbids passing argument %d of "
7440 "%qE between function pointer "
7442 G_("ISO C forbids assignment between "
7443 "function pointer and %<void *%>"),
7444 G_("ISO C forbids initialization between "
7445 "function pointer and %<void *%>"),
7446 G_("ISO C forbids return between function "
7447 "pointer and %<void *%>"));
7448 /* Const and volatile mean something different for function types,
7449 so the usual warnings are not appropriate. */
7450 else if (TREE_CODE (ttr) != FUNCTION_TYPE
7451 && TREE_CODE (ttl) != FUNCTION_TYPE)
7453 /* Assignments between atomic and non-atomic objects are OK. */
7454 bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7455 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7456 bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7457 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7459 /* Don't warn about loss of qualifier for conversions from
7460 qualified void* to pointers to arrays with corresponding
7461 qualifier on the element type (except for pedantic before C23). */
7462 if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7463 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7464 OPT_Wdiscarded_qualifiers,
7465 G_("passing argument %d of %qE discards "
7466 "%qv qualifier from pointer target type"),
7467 G_("assignment discards %qv qualifier "
7468 "from pointer target type"),
7469 G_("initialization discards %qv qualifier "
7470 "from pointer target type"),
7471 G_("return discards %qv qualifier from "
7472 "pointer target type"),
7473 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7474 else if (warn_quals_ped)
7475 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7476 "array with qualifier on the element is not qualified before C2X");
7478 /* If this is not a case of ignoring a mismatch in signedness,
7480 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7483 /* If there is a mismatch, do warn. */
7484 else if (warn_pointer_sign)
7489 auto_diagnostic_group d;
7490 range_label_for_type_mismatch rhs_label (rhstype, type);
7491 gcc_rich_location richloc (expr_loc, &rhs_label);
7492 if (pedwarn (&richloc, OPT_Wpointer_sign,
7493 "pointer targets in passing argument %d of "
7494 "%qE differ in signedness", parmnum, rname))
7495 inform_for_arg (fundecl, expr_loc, parmnum, type,
7500 pedwarn (location, OPT_Wpointer_sign,
7501 "pointer targets in assignment from %qT to %qT "
7502 "differ in signedness", rhstype, type);
7506 pedwarn_init (location, OPT_Wpointer_sign,
7507 "pointer targets in initialization of %qT "
7508 "from %qT differ in signedness", type,
7512 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7513 "returning %qT from a function with return type "
7514 "%qT differ in signedness", rhstype, type);
7520 else if (TREE_CODE (ttl) == FUNCTION_TYPE
7521 && TREE_CODE (ttr) == FUNCTION_TYPE)
7523 /* Because const and volatile on functions are restrictions
7524 that say the function will not do certain things,
7525 it is okay to use a const or volatile function
7526 where an ordinary one is wanted, but not vice-versa. */
7527 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7528 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7529 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7530 OPT_Wdiscarded_qualifiers,
7531 G_("passing argument %d of %qE makes "
7532 "%q#v qualified function pointer "
7533 "from unqualified"),
7534 G_("assignment makes %q#v qualified function "
7535 "pointer from unqualified"),
7536 G_("initialization makes %q#v qualified "
7537 "function pointer from unqualified"),
7538 G_("return makes %q#v qualified function "
7539 "pointer from unqualified"),
7540 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7543 /* Avoid warning about the volatile ObjC EH puts on decls. */
7550 auto_diagnostic_group d;
7551 range_label_for_type_mismatch rhs_label (rhstype, type);
7552 gcc_rich_location richloc (expr_loc, &rhs_label);
7553 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7554 "passing argument %d of %qE from incompatible "
7555 "pointer type", parmnum, rname))
7556 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7561 pedwarn (location, OPT_Wincompatible_pointer_types,
7562 "assignment to %qT from pointer to "
7563 "%qD with incompatible type %qT",
7564 type, bltin, rhstype);
7566 pedwarn (location, OPT_Wincompatible_pointer_types,
7567 "assignment to %qT from incompatible pointer type %qT",
7573 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7574 "initialization of %qT from pointer to "
7575 "%qD with incompatible type %qT",
7576 type, bltin, rhstype);
7578 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7579 "initialization of %qT from incompatible "
7585 pedwarn (location, OPT_Wincompatible_pointer_types,
7586 "returning pointer to %qD of type %qT from "
7587 "a function with incompatible type %qT",
7588 bltin, rhstype, type);
7590 pedwarn (location, OPT_Wincompatible_pointer_types,
7591 "returning %qT from a function with incompatible "
7592 "return type %qT", rhstype, type);
7599 /* If RHS isn't an address, check pointer or array of packed
7601 warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7603 return convert (type, rhs);
7605 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7607 /* ??? This should not be an error when inlining calls to
7608 unprototyped functions. */
7609 const char msg[] = "invalid use of non-lvalue array";
7611 warning_at (location, warnopt, msg);
7613 error_at (location, msg);
7614 return error_mark_node;
7616 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7618 /* An explicit constant 0 can convert to a pointer,
7619 or one that results from arithmetic, even including
7620 a cast to integer type. */
7621 if (!null_pointer_constant)
7626 auto_diagnostic_group d;
7627 range_label_for_type_mismatch rhs_label (rhstype, type);
7628 gcc_rich_location richloc (expr_loc, &rhs_label);
7629 if (pedwarn (&richloc, OPT_Wint_conversion,
7630 "passing argument %d of %qE makes pointer from "
7631 "integer without a cast", parmnum, rname))
7632 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7636 pedwarn (location, OPT_Wint_conversion,
7637 "assignment to %qT from %qT makes pointer from integer "
7638 "without a cast", type, rhstype);
7642 pedwarn_init (location, OPT_Wint_conversion,
7643 "initialization of %qT from %qT makes pointer from "
7644 "integer without a cast", type, rhstype);
7647 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7648 "function with return type %qT makes pointer from "
7649 "integer without a cast", rhstype, type);
7655 return convert (type, rhs);
7657 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7663 auto_diagnostic_group d;
7664 range_label_for_type_mismatch rhs_label (rhstype, type);
7665 gcc_rich_location richloc (expr_loc, &rhs_label);
7666 if (pedwarn (&richloc, OPT_Wint_conversion,
7667 "passing argument %d of %qE makes integer from "
7668 "pointer without a cast", parmnum, rname))
7669 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7673 pedwarn (location, OPT_Wint_conversion,
7674 "assignment to %qT from %qT makes integer from pointer "
7675 "without a cast", type, rhstype);
7679 pedwarn_init (location, OPT_Wint_conversion,
7680 "initialization of %qT from %qT makes integer from "
7681 "pointer without a cast", type, rhstype);
7684 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7685 "function with return type %qT makes integer from "
7686 "pointer without a cast", rhstype, type);
7692 return convert (type, rhs);
7694 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7697 bool save = in_late_binary_op;
7698 in_late_binary_op = true;
7699 ret = convert (type, rhs);
7700 in_late_binary_op = save;
7708 auto_diagnostic_group d;
7709 range_label_for_type_mismatch rhs_label (rhstype, type);
7710 gcc_rich_location richloc (expr_loc, &rhs_label);
7711 const char msg[] = G_("incompatible type for argument %d of %qE");
7713 warning_at (expr_loc, warnopt, msg, parmnum, rname);
7715 error_at (&richloc, msg, parmnum, rname);
7716 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7722 = G_("incompatible types when assigning to type %qT from type %qT");
7724 warning_at (expr_loc, 0, msg, type, rhstype);
7726 error_at (expr_loc, msg, type, rhstype);
7733 = G_("incompatible types when initializing type %qT using type %qT");
7735 warning_at (location, 0, msg, type, rhstype);
7737 error_at (location, msg, type, rhstype);
7743 = G_("incompatible types when returning type %qT but %qT was expected");
7745 warning_at (location, 0, msg, rhstype, type);
7747 error_at (location, msg, rhstype, type);
7754 return error_mark_node;
7757 /* If VALUE is a compound expr all of whose expressions are constant, then
7758 return its value. Otherwise, return error_mark_node.
7760 This is for handling COMPOUND_EXPRs as initializer elements
7761 which is allowed with a warning when -pedantic is specified. */
7764 valid_compound_expr_initializer (tree value, tree endtype)
7766 if (TREE_CODE (value) == COMPOUND_EXPR)
7768 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7770 return error_mark_node;
7771 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7774 else if (!initializer_constant_valid_p (value, endtype))
7775 return error_mark_node;
7780 /* Perform appropriate conversions on the initial value of a variable,
7781 store it in the declaration DECL,
7782 and print any error messages that are appropriate.
7783 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7784 If the init is invalid, store an ERROR_MARK.
7786 INIT_LOC is the location of the initial value. */
7789 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7794 /* If variable's type was invalidly declared, just ignore it. */
7796 type = TREE_TYPE (decl);
7797 if (TREE_CODE (type) == ERROR_MARK)
7800 /* Digest the specified initializer into an expression. */
7803 npc = null_pointer_constant_p (init);
7804 value = digest_init (init_loc, type, init, origtype, npc,
7805 true, TREE_STATIC (decl));
7807 /* Store the expression if valid; else report error. */
7809 if (!in_system_header_at (input_location)
7810 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7811 warning (OPT_Wtraditional, "traditional C rejects automatic "
7812 "aggregate initialization");
7814 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7815 DECL_INITIAL (decl) = value;
7817 /* ANSI wants warnings about out-of-range constant initializers. */
7818 STRIP_TYPE_NOPS (value);
7819 if (TREE_STATIC (decl))
7820 constant_expression_warning (value);
7822 /* Check if we need to set array size from compound literal size. */
7823 if (TREE_CODE (type) == ARRAY_TYPE
7824 && TYPE_DOMAIN (type) == NULL_TREE
7825 && value != error_mark_node)
7827 tree inside_init = init;
7829 STRIP_TYPE_NOPS (inside_init);
7830 inside_init = fold (inside_init);
7832 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7834 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7836 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7838 /* For int foo[] = (int [3]){1}; we need to set array size
7839 now since later on array initializer will be just the
7840 brace enclosed list of the compound literal. */
7841 tree etype = strip_array_types (TREE_TYPE (decl));
7842 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7843 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7845 layout_decl (cldecl, 0);
7847 = c_build_qualified_type (type, TYPE_QUALS (etype));
7853 /* Methods for storing and printing names for error messages. */
7855 /* Implement a spelling stack that allows components of a name to be pushed
7856 and popped. Each element on the stack is this structure. */
7863 unsigned HOST_WIDE_INT i;
7868 #define SPELLING_STRING 1
7869 #define SPELLING_MEMBER 2
7870 #define SPELLING_BOUNDS 3
7872 static struct spelling *spelling; /* Next stack element (unused). */
7873 static struct spelling *spelling_base; /* Spelling stack base. */
7874 static int spelling_size; /* Size of the spelling stack. */
7876 /* Macros to save and restore the spelling stack around push_... functions.
7877 Alternative to SAVE_SPELLING_STACK. */
7879 #define SPELLING_DEPTH() (spelling - spelling_base)
7880 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7882 /* Push an element on the spelling stack with type KIND and assign VALUE
7885 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7887 int depth = SPELLING_DEPTH (); \
7889 if (depth >= spelling_size) \
7891 spelling_size += 10; \
7892 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7894 RESTORE_SPELLING_DEPTH (depth); \
7897 spelling->kind = (KIND); \
7898 spelling->MEMBER = (VALUE); \
7902 /* Push STRING on the stack. Printed literally. */
7905 push_string (const char *string)
7907 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7910 /* Push a member name on the stack. Printed as '.' STRING. */
7913 push_member_name (tree decl)
7915 const char *const string
7917 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7918 : _("<anonymous>"));
7919 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7922 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7925 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7927 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7930 /* Compute the maximum size in bytes of the printed spelling. */
7933 spelling_length (void)
7938 for (p = spelling_base; p < spelling; p++)
7940 if (p->kind == SPELLING_BOUNDS)
7943 size += strlen (p->u.s) + 1;
7949 /* Print the spelling to BUFFER and return it. */
7952 print_spelling (char *buffer)
7957 for (p = spelling_base; p < spelling; p++)
7958 if (p->kind == SPELLING_BOUNDS)
7960 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7966 if (p->kind == SPELLING_MEMBER)
7968 for (s = p->u.s; (*d = *s++); d++)
7975 /* Digest the parser output INIT as an initializer for type TYPE.
7976 Return a C expression of type TYPE to represent the initial value.
7978 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7980 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7982 If INIT is a string constant, STRICT_STRING is true if it is
7983 unparenthesized or we should not warn here for it being parenthesized.
7984 For other types of INIT, STRICT_STRING is not used.
7986 INIT_LOC is the location of the INIT.
7988 REQUIRE_CONSTANT requests an error if non-constant initializers or
7989 elements are seen. */
7992 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7993 bool null_pointer_constant, bool strict_string,
7994 int require_constant)
7996 enum tree_code code = TREE_CODE (type);
7997 tree inside_init = init;
7998 tree semantic_type = NULL_TREE;
7999 bool maybe_const = true;
8001 if (type == error_mark_node
8003 || error_operand_p (init))
8004 return error_mark_node;
8006 STRIP_TYPE_NOPS (inside_init);
8010 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8012 semantic_type = TREE_TYPE (inside_init);
8013 inside_init = TREE_OPERAND (inside_init, 0);
8015 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8018 /* Initialization of an array of chars from a string constant
8019 optionally enclosed in braces. */
8021 if (code == ARRAY_TYPE && inside_init
8022 && TREE_CODE (inside_init) == STRING_CST)
8025 = (TYPE_ATOMIC (TREE_TYPE (type))
8026 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8028 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8029 /* Note that an array could be both an array of character type
8030 and an array of wchar_t if wchar_t is signed char or unsigned
8032 bool char_array = (typ1 == char_type_node
8033 || typ1 == signed_char_type_node
8034 || typ1 == unsigned_char_type_node);
8035 bool wchar_array = !!comptypes (typ1, wchar_type_node);
8036 bool char16_array = !!comptypes (typ1, char16_type_node);
8037 bool char32_array = !!comptypes (typ1, char32_type_node);
8039 if (char_array || wchar_array || char16_array || char32_array)
8042 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8043 bool incompat_string_cst = false;
8044 expr.value = inside_init;
8045 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8046 expr.original_type = NULL;
8047 maybe_warn_string_init (init_loc, type, expr);
8049 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8050 pedwarn_init (init_loc, OPT_Wpedantic,
8051 "initialization of a flexible array member");
8053 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8054 TYPE_MAIN_VARIANT (type)))
8059 if (typ2 != char_type_node && typ2 != char8_type_node)
8060 incompat_string_cst = true;
8062 else if (!comptypes (typ1, typ2))
8063 incompat_string_cst = true;
8065 if (incompat_string_cst)
8067 error_init (init_loc, "cannot initialize array of %qT from "
8068 "a string literal with type array of %qT",
8070 return error_mark_node;
8073 if (TYPE_DOMAIN (type) != NULL_TREE
8074 && TYPE_SIZE (type) != NULL_TREE
8075 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8077 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8078 unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8080 /* Subtract the size of a single (possibly wide) character
8081 because it's ok to ignore the terminating null char
8082 that is counted in the length of the constant. */
8083 if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8084 pedwarn_init (init_loc, 0,
8085 ("initializer-string for array of %qT "
8086 "is too long"), typ1);
8087 else if (warn_cxx_compat
8088 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8089 warning_at (init_loc, OPT_Wc___compat,
8090 ("initializer-string for array of %qT "
8091 "is too long for C++"), typ1);
8092 if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8094 unsigned HOST_WIDE_INT size
8095 = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8096 const char *p = TREE_STRING_POINTER (inside_init);
8098 inside_init = build_string (size, p);
8102 TREE_TYPE (inside_init) = type;
8105 else if (INTEGRAL_TYPE_P (typ1))
8107 error_init (init_loc, "array of inappropriate type initialized "
8108 "from string constant");
8109 return error_mark_node;
8113 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8114 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8115 below and handle as a constructor. */
8116 if (code == VECTOR_TYPE
8117 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8118 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8119 && TREE_CONSTANT (inside_init))
8121 if (TREE_CODE (inside_init) == VECTOR_CST
8122 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8123 TYPE_MAIN_VARIANT (type)))
8126 if (TREE_CODE (inside_init) == CONSTRUCTOR)
8128 unsigned HOST_WIDE_INT ix;
8130 bool constant_p = true;
8132 /* Iterate through elements and check if all constructor
8133 elements are *_CSTs. */
8134 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8135 if (!CONSTANT_CLASS_P (value))
8142 return build_vector_from_ctor (type,
8143 CONSTRUCTOR_ELTS (inside_init));
8147 if (warn_sequence_point)
8148 verify_sequence_points (inside_init);
8150 /* Any type can be initialized
8151 from an expression of the same type, optionally with braces. */
8153 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8154 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8155 TYPE_MAIN_VARIANT (type))
8156 || (code == ARRAY_TYPE
8157 && comptypes (TREE_TYPE (inside_init), type))
8158 || (gnu_vector_type_p (type)
8159 && comptypes (TREE_TYPE (inside_init), type))
8160 || (code == POINTER_TYPE
8161 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8162 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8163 TREE_TYPE (type)))))
8165 if (code == POINTER_TYPE)
8167 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8169 if (TREE_CODE (inside_init) == STRING_CST
8170 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8171 inside_init = array_to_pointer_conversion
8172 (init_loc, inside_init);
8175 error_init (init_loc, "invalid use of non-lvalue array");
8176 return error_mark_node;
8181 if (code == VECTOR_TYPE)
8182 /* Although the types are compatible, we may require a
8184 inside_init = convert (type, inside_init);
8186 if (require_constant
8187 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8189 /* As an extension, allow initializing objects with static storage
8190 duration with compound literals (which are then treated just as
8191 the brace enclosed list they contain). Also allow this for
8192 vectors, as we can only assign them with compound literals. */
8193 if (flag_isoc99 && code != VECTOR_TYPE)
8194 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8196 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8197 inside_init = DECL_INITIAL (decl);
8200 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8201 && TREE_CODE (inside_init) != CONSTRUCTOR)
8203 error_init (init_loc, "array initialized from non-constant array "
8205 return error_mark_node;
8208 /* Compound expressions can only occur here if -Wpedantic or
8209 -pedantic-errors is specified. In the later case, we always want
8210 an error. In the former case, we simply want a warning. */
8211 if (require_constant && pedantic
8212 && TREE_CODE (inside_init) == COMPOUND_EXPR)
8215 = valid_compound_expr_initializer (inside_init,
8216 TREE_TYPE (inside_init));
8217 if (inside_init == error_mark_node)
8218 error_init (init_loc, "initializer element is not constant");
8220 pedwarn_init (init_loc, OPT_Wpedantic,
8221 "initializer element is not constant");
8222 if (flag_pedantic_errors)
8223 inside_init = error_mark_node;
8225 else if (require_constant
8226 && !initializer_constant_valid_p (inside_init,
8227 TREE_TYPE (inside_init)))
8229 error_init (init_loc, "initializer element is not constant");
8230 inside_init = error_mark_node;
8232 else if (require_constant && !maybe_const)
8233 pedwarn_init (init_loc, OPT_Wpedantic,
8234 "initializer element is not a constant expression");
8236 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8237 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8238 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8239 type, inside_init, origtype,
8242 : ic_init), null_pointer_constant,
8243 NULL_TREE, NULL_TREE, 0);
8247 /* Handle scalar types, including conversions. */
8249 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8250 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8251 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8253 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8254 && (TREE_CODE (init) == STRING_CST
8255 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8256 inside_init = init = array_to_pointer_conversion (init_loc, init);
8258 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8261 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8262 inside_init, origtype,
8263 require_constant ? ic_init_const : ic_init,
8264 null_pointer_constant, NULL_TREE, NULL_TREE,
8267 /* Check to see if we have already given an error message. */
8268 if (inside_init == error_mark_node)
8270 else if (require_constant && !TREE_CONSTANT (inside_init))
8272 error_init (init_loc, "initializer element is not constant");
8273 inside_init = error_mark_node;
8275 else if (require_constant
8276 && !initializer_constant_valid_p (inside_init,
8277 TREE_TYPE (inside_init)))
8279 error_init (init_loc, "initializer element is not computable at "
8281 inside_init = error_mark_node;
8283 else if (require_constant && !maybe_const)
8284 pedwarn_init (init_loc, OPT_Wpedantic,
8285 "initializer element is not a constant expression");
8290 /* Come here only for records and arrays. */
8292 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8294 error_init (init_loc,
8295 "variable-sized object may not be initialized except "
8296 "with an empty initializer");
8297 return error_mark_node;
8300 error_init (init_loc, "invalid initializer");
8301 return error_mark_node;
8304 /* Handle initializers that use braces. */
8306 /* Type of object we are accumulating a constructor for.
8307 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8308 static tree constructor_type;
8310 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8312 static tree constructor_fields;
8314 /* For an ARRAY_TYPE, this is the specified index
8315 at which to store the next element we get. */
8316 static tree constructor_index;
8318 /* For an ARRAY_TYPE, this is the maximum index. */
8319 static tree constructor_max_index;
8321 /* For a RECORD_TYPE, this is the first field not yet written out. */
8322 static tree constructor_unfilled_fields;
8324 /* For an ARRAY_TYPE, this is the index of the first element
8325 not yet written out. */
8326 static tree constructor_unfilled_index;
8328 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8329 This is so we can generate gaps between fields, when appropriate. */
8330 static tree constructor_bit_index;
8332 /* If we are saving up the elements rather than allocating them,
8333 this is the list of elements so far (in reverse order,
8334 most recent first). */
8335 static vec<constructor_elt, va_gc> *constructor_elements;
8337 /* 1 if constructor should be incrementally stored into a constructor chain,
8338 0 if all the elements should be kept in AVL tree. */
8339 static int constructor_incremental;
8341 /* 1 if so far this constructor's elements are all compile-time constants. */
8342 static int constructor_constant;
8344 /* 1 if so far this constructor's elements are all valid address constants. */
8345 static int constructor_simple;
8347 /* 1 if this constructor has an element that cannot be part of a
8348 constant expression. */
8349 static int constructor_nonconst;
8351 /* 1 if this constructor is erroneous so far. */
8352 static int constructor_erroneous;
8354 /* 1 if this constructor is the universal zero initializer { 0 }. */
8355 static int constructor_zeroinit;
8357 /* Structure for managing pending initializer elements, organized as an
8362 struct init_node *left, *right;
8363 struct init_node *parent;
8370 /* Tree of pending elements at this constructor level.
8371 These are elements encountered out of order
8372 which belong at places we haven't reached yet in actually
8374 Will never hold tree nodes across GC runs. */
8375 static struct init_node *constructor_pending_elts;
8377 /* The SPELLING_DEPTH of this constructor. */
8378 static int constructor_depth;
8380 /* DECL node for which an initializer is being read.
8381 0 means we are reading a constructor expression
8382 such as (struct foo) {...}. */
8383 static tree constructor_decl;
8385 /* Nonzero if this is an initializer for a top-level decl. */
8386 static int constructor_top_level;
8388 /* Nonzero if there were any member designators in this initializer. */
8389 static int constructor_designated;
8391 /* Nesting depth of designator list. */
8392 static int designator_depth;
8394 /* Nonzero if there were diagnosed errors in this designator list. */
8395 static int designator_erroneous;
8398 /* This stack has a level for each implicit or explicit level of
8399 structuring in the initializer, including the outermost one. It
8400 saves the values of most of the variables above. */
8402 struct constructor_range_stack;
8404 struct constructor_stack
8406 struct constructor_stack *next;
8411 tree unfilled_index;
8412 tree unfilled_fields;
8414 vec<constructor_elt, va_gc> *elements;
8415 struct init_node *pending_elts;
8418 /* If value nonzero, this value should replace the entire
8419 constructor at this level. */
8420 struct c_expr replacement_value;
8421 struct constructor_range_stack *range_stack;
8430 int designator_depth;
8433 static struct constructor_stack *constructor_stack;
8435 /* This stack represents designators from some range designator up to
8436 the last designator in the list. */
8438 struct constructor_range_stack
8440 struct constructor_range_stack *next, *prev;
8441 struct constructor_stack *stack;
8448 static struct constructor_range_stack *constructor_range_stack;
8450 /* This stack records separate initializers that are nested.
8451 Nested initializers can't happen in ANSI C, but GNU C allows them
8452 in cases like { ... (struct foo) { ... } ... }. */
8454 struct initializer_stack
8456 struct initializer_stack *next;
8458 struct constructor_stack *constructor_stack;
8459 struct constructor_range_stack *constructor_range_stack;
8460 vec<constructor_elt, va_gc> *elements;
8461 struct spelling *spelling;
8462 struct spelling *spelling_base;
8465 char require_constant_value;
8466 char require_constant_elements;
8468 rich_location *missing_brace_richloc;
8471 static struct initializer_stack *initializer_stack;
8473 /* Prepare to parse and output the initializer for variable DECL. */
8476 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8477 rich_location *richloc)
8480 struct initializer_stack *p = XNEW (struct initializer_stack);
8482 p->decl = constructor_decl;
8483 p->require_constant_value = require_constant_value;
8484 p->require_constant_elements = require_constant_elements;
8485 p->constructor_stack = constructor_stack;
8486 p->constructor_range_stack = constructor_range_stack;
8487 p->elements = constructor_elements;
8488 p->spelling = spelling;
8489 p->spelling_base = spelling_base;
8490 p->spelling_size = spelling_size;
8491 p->top_level = constructor_top_level;
8492 p->next = initializer_stack;
8493 p->missing_brace_richloc = richloc;
8494 p->designated = constructor_designated;
8495 initializer_stack = p;
8497 constructor_decl = decl;
8498 constructor_designated = 0;
8499 constructor_top_level = top_level;
8501 if (decl != NULL_TREE && decl != error_mark_node)
8503 require_constant_value = TREE_STATIC (decl);
8504 require_constant_elements
8505 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8506 /* For a scalar, you can always use any value to initialize,
8507 even within braces. */
8508 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8509 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8513 require_constant_value = 0;
8514 require_constant_elements = 0;
8515 locus = _("(anonymous)");
8518 constructor_stack = 0;
8519 constructor_range_stack = 0;
8521 found_missing_braces = 0;
8525 RESTORE_SPELLING_DEPTH (0);
8528 push_string (locus);
8534 struct initializer_stack *p = initializer_stack;
8536 /* Free the whole constructor stack of this initializer. */
8537 while (constructor_stack)
8539 struct constructor_stack *q = constructor_stack;
8540 constructor_stack = q->next;
8544 gcc_assert (!constructor_range_stack);
8546 /* Pop back to the data of the outer initializer (if any). */
8547 XDELETE (spelling_base);
8549 constructor_decl = p->decl;
8550 require_constant_value = p->require_constant_value;
8551 require_constant_elements = p->require_constant_elements;
8552 constructor_stack = p->constructor_stack;
8553 constructor_designated = p->designated;
8554 constructor_range_stack = p->constructor_range_stack;
8555 constructor_elements = p->elements;
8556 spelling = p->spelling;
8557 spelling_base = p->spelling_base;
8558 spelling_size = p->spelling_size;
8559 constructor_top_level = p->top_level;
8560 initializer_stack = p->next;
8564 /* Call here when we see the initializer is surrounded by braces.
8565 This is instead of a call to push_init_level;
8566 it is matched by a call to pop_init_level.
8568 TYPE is the type to initialize, for a constructor expression.
8569 For an initializer for a decl, TYPE is zero. */
8572 really_start_incremental_init (tree type)
8574 struct constructor_stack *p = XNEW (struct constructor_stack);
8576 if (type == NULL_TREE)
8577 type = TREE_TYPE (constructor_decl);
8579 if (VECTOR_TYPE_P (type)
8580 && TYPE_VECTOR_OPAQUE (type))
8581 error ("opaque vector types cannot be initialized");
8583 p->type = constructor_type;
8584 p->fields = constructor_fields;
8585 p->index = constructor_index;
8586 p->max_index = constructor_max_index;
8587 p->unfilled_index = constructor_unfilled_index;
8588 p->unfilled_fields = constructor_unfilled_fields;
8589 p->bit_index = constructor_bit_index;
8590 p->elements = constructor_elements;
8591 p->constant = constructor_constant;
8592 p->simple = constructor_simple;
8593 p->nonconst = constructor_nonconst;
8594 p->erroneous = constructor_erroneous;
8595 p->pending_elts = constructor_pending_elts;
8596 p->depth = constructor_depth;
8597 p->replacement_value.value = 0;
8598 p->replacement_value.original_code = ERROR_MARK;
8599 p->replacement_value.original_type = NULL;
8603 p->incremental = constructor_incremental;
8604 p->designated = constructor_designated;
8605 p->designator_depth = designator_depth;
8607 constructor_stack = p;
8609 constructor_constant = 1;
8610 constructor_simple = 1;
8611 constructor_nonconst = 0;
8612 constructor_depth = SPELLING_DEPTH ();
8613 constructor_elements = NULL;
8614 constructor_pending_elts = 0;
8615 constructor_type = type;
8616 constructor_incremental = 1;
8617 constructor_designated = 0;
8618 constructor_zeroinit = 1;
8619 designator_depth = 0;
8620 designator_erroneous = 0;
8622 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8624 constructor_fields = TYPE_FIELDS (constructor_type);
8625 /* Skip any nameless bit fields at the beginning. */
8626 while (constructor_fields != NULL_TREE
8627 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8628 constructor_fields = DECL_CHAIN (constructor_fields);
8630 constructor_unfilled_fields = constructor_fields;
8631 constructor_bit_index = bitsize_zero_node;
8633 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8635 if (TYPE_DOMAIN (constructor_type))
8637 constructor_max_index
8638 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8640 /* Detect non-empty initializations of zero-length arrays. */
8641 if (constructor_max_index == NULL_TREE
8642 && TYPE_SIZE (constructor_type))
8643 constructor_max_index = integer_minus_one_node;
8645 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8646 to initialize VLAs with a nonempty initializer will cause a
8647 proper error; avoid tree checking errors as well by setting a
8649 if (constructor_max_index
8650 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8651 constructor_max_index = integer_minus_one_node;
8654 = convert (bitsizetype,
8655 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8659 constructor_index = bitsize_zero_node;
8660 constructor_max_index = NULL_TREE;
8663 constructor_unfilled_index = constructor_index;
8665 else if (gnu_vector_type_p (constructor_type))
8667 /* Vectors are like simple fixed-size arrays. */
8668 constructor_max_index =
8669 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8670 constructor_index = bitsize_zero_node;
8671 constructor_unfilled_index = constructor_index;
8675 /* Handle the case of int x = {5}; */
8676 constructor_fields = constructor_type;
8677 constructor_unfilled_fields = constructor_type;
8681 extern location_t last_init_list_comma;
8683 /* Called when we see an open brace for a nested initializer. Finish
8684 off any pending levels with implicit braces. */
8686 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8688 while (constructor_stack->implicit)
8690 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8691 && constructor_fields == NULL_TREE)
8692 process_init_element (input_location,
8693 pop_init_level (loc, 1, braced_init_obstack,
8694 last_init_list_comma),
8695 true, braced_init_obstack);
8696 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8697 && constructor_max_index
8698 && tree_int_cst_lt (constructor_max_index,
8700 process_init_element (input_location,
8701 pop_init_level (loc, 1, braced_init_obstack,
8702 last_init_list_comma),
8703 true, braced_init_obstack);
8709 /* Push down into a subobject, for initialization.
8710 If this is for an explicit set of braces, IMPLICIT is 0.
8711 If it is because the next element belongs at a lower level,
8712 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8715 push_init_level (location_t loc, int implicit,
8716 struct obstack *braced_init_obstack)
8718 struct constructor_stack *p;
8719 tree value = NULL_TREE;
8721 /* Unless this is an explicit brace, we need to preserve previous
8725 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8726 value = find_init_member (constructor_fields, braced_init_obstack);
8727 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8728 value = find_init_member (constructor_index, braced_init_obstack);
8731 p = XNEW (struct constructor_stack);
8732 p->type = constructor_type;
8733 p->fields = constructor_fields;
8734 p->index = constructor_index;
8735 p->max_index = constructor_max_index;
8736 p->unfilled_index = constructor_unfilled_index;
8737 p->unfilled_fields = constructor_unfilled_fields;
8738 p->bit_index = constructor_bit_index;
8739 p->elements = constructor_elements;
8740 p->constant = constructor_constant;
8741 p->simple = constructor_simple;
8742 p->nonconst = constructor_nonconst;
8743 p->erroneous = constructor_erroneous;
8744 p->pending_elts = constructor_pending_elts;
8745 p->depth = constructor_depth;
8746 p->replacement_value.value = NULL_TREE;
8747 p->replacement_value.original_code = ERROR_MARK;
8748 p->replacement_value.original_type = NULL;
8749 p->implicit = implicit;
8751 p->incremental = constructor_incremental;
8752 p->designated = constructor_designated;
8753 p->designator_depth = designator_depth;
8754 p->next = constructor_stack;
8756 constructor_stack = p;
8758 constructor_constant = 1;
8759 constructor_simple = 1;
8760 constructor_nonconst = 0;
8761 constructor_depth = SPELLING_DEPTH ();
8762 constructor_elements = NULL;
8763 constructor_incremental = 1;
8764 /* If the upper initializer is designated, then mark this as
8765 designated too to prevent bogus warnings. */
8766 constructor_designated = p->designated;
8767 constructor_pending_elts = 0;
8770 p->range_stack = constructor_range_stack;
8771 constructor_range_stack = 0;
8772 designator_depth = 0;
8773 designator_erroneous = 0;
8776 /* Don't die if an entire brace-pair level is superfluous
8777 in the containing level. */
8778 if (constructor_type == NULL_TREE)
8780 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8782 /* Don't die if there are extra init elts at the end. */
8783 if (constructor_fields == NULL_TREE)
8784 constructor_type = NULL_TREE;
8787 constructor_type = TREE_TYPE (constructor_fields);
8788 push_member_name (constructor_fields);
8789 constructor_depth++;
8792 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8794 constructor_type = TREE_TYPE (constructor_type);
8795 push_array_bounds (tree_to_uhwi (constructor_index));
8796 constructor_depth++;
8799 if (constructor_type == NULL_TREE)
8801 error_init (loc, "extra brace group at end of initializer");
8802 constructor_fields = NULL_TREE;
8803 constructor_unfilled_fields = NULL_TREE;
8807 if (value && TREE_CODE (value) == CONSTRUCTOR)
8809 constructor_constant = TREE_CONSTANT (value);
8810 constructor_simple = TREE_STATIC (value);
8811 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8812 constructor_elements = CONSTRUCTOR_ELTS (value);
8813 if (!vec_safe_is_empty (constructor_elements)
8814 && (TREE_CODE (constructor_type) == RECORD_TYPE
8815 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8816 set_nonincremental_init (braced_init_obstack);
8821 found_missing_braces = 1;
8822 if (initializer_stack->missing_brace_richloc)
8823 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8827 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8829 constructor_fields = TYPE_FIELDS (constructor_type);
8830 /* Skip any nameless bit fields at the beginning. */
8831 while (constructor_fields != NULL_TREE
8832 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8833 constructor_fields = DECL_CHAIN (constructor_fields);
8835 constructor_unfilled_fields = constructor_fields;
8836 constructor_bit_index = bitsize_zero_node;
8838 else if (gnu_vector_type_p (constructor_type))
8840 /* Vectors are like simple fixed-size arrays. */
8841 constructor_max_index =
8842 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8843 constructor_index = bitsize_int (0);
8844 constructor_unfilled_index = constructor_index;
8846 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8848 if (TYPE_DOMAIN (constructor_type))
8850 constructor_max_index
8851 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8853 /* Detect non-empty initializations of zero-length arrays. */
8854 if (constructor_max_index == NULL_TREE
8855 && TYPE_SIZE (constructor_type))
8856 constructor_max_index = integer_minus_one_node;
8858 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8859 to initialize VLAs will cause a proper error; avoid tree
8860 checking errors as well by setting a safe value. */
8861 if (constructor_max_index
8862 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8863 constructor_max_index = integer_minus_one_node;
8866 = convert (bitsizetype,
8867 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8870 constructor_index = bitsize_zero_node;
8872 constructor_unfilled_index = constructor_index;
8873 if (value && TREE_CODE (value) == STRING_CST)
8875 /* We need to split the char/wchar array into individual
8876 characters, so that we don't have to special case it
8878 set_nonincremental_init_from_string (value, braced_init_obstack);
8883 if (constructor_type != error_mark_node)
8884 warning_init (input_location, 0, "braces around scalar initializer");
8885 constructor_fields = constructor_type;
8886 constructor_unfilled_fields = constructor_type;
8890 /* At the end of an implicit or explicit brace level,
8891 finish up that level of constructor. If a single expression
8892 with redundant braces initialized that level, return the
8893 c_expr structure for that expression. Otherwise, the original_code
8894 element is set to ERROR_MARK.
8895 If we were outputting the elements as they are read, return 0 as the value
8896 from inner levels (process_init_element ignores that),
8897 but return error_mark_node as the value from the outermost level
8898 (that's what we want to put in DECL_INITIAL).
8899 Otherwise, return a CONSTRUCTOR expression as the value. */
8902 pop_init_level (location_t loc, int implicit,
8903 struct obstack *braced_init_obstack,
8904 location_t insert_before)
8906 struct constructor_stack *p;
8908 ret.value = NULL_TREE;
8909 ret.original_code = ERROR_MARK;
8910 ret.original_type = NULL;
8914 /* When we come to an explicit close brace,
8915 pop any inner levels that didn't have explicit braces. */
8916 while (constructor_stack->implicit)
8917 process_init_element (input_location,
8918 pop_init_level (loc, 1, braced_init_obstack,
8920 true, braced_init_obstack);
8921 gcc_assert (!constructor_range_stack);
8924 if (initializer_stack->missing_brace_richloc)
8925 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8926 (insert_before, "}");
8928 /* Now output all pending elements. */
8929 constructor_incremental = 1;
8930 output_pending_init_elements (1, braced_init_obstack);
8932 p = constructor_stack;
8934 /* Error for initializing a flexible array member, or a zero-length
8935 array member in an inappropriate context. */
8936 if (constructor_type && constructor_fields
8937 && TREE_CODE (constructor_type) == ARRAY_TYPE
8938 && TYPE_DOMAIN (constructor_type)
8939 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8941 /* Silently discard empty initializations. The parser will
8942 already have pedwarned for empty brackets. */
8943 if (integer_zerop (constructor_unfilled_index))
8944 constructor_type = NULL_TREE;
8947 gcc_assert (!TYPE_SIZE (constructor_type));
8949 if (constructor_depth > 2)
8950 error_init (loc, "initialization of flexible array member in a nested context");
8952 pedwarn_init (loc, OPT_Wpedantic,
8953 "initialization of a flexible array member");
8955 /* We have already issued an error message for the existence
8956 of a flexible array member not at the end of the structure.
8957 Discard the initializer so that we do not die later. */
8958 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8959 constructor_type = NULL_TREE;
8963 switch (vec_safe_length (constructor_elements))
8966 /* Initialization with { } counts as zeroinit. */
8967 constructor_zeroinit = 1;
8970 /* This might be zeroinit as well. */
8971 if (integer_zerop ((*constructor_elements)[0].value))
8972 constructor_zeroinit = 1;
8975 /* If the constructor has more than one element, it can't be { 0 }. */
8976 constructor_zeroinit = 0;
8980 /* Warn when some structs are initialized with direct aggregation. */
8981 if (!implicit && found_missing_braces && warn_missing_braces
8982 && !constructor_zeroinit)
8984 gcc_assert (initializer_stack->missing_brace_richloc);
8985 warning_at (initializer_stack->missing_brace_richloc,
8986 OPT_Wmissing_braces,
8987 "missing braces around initializer");
8990 /* Warn when some struct elements are implicitly initialized to zero. */
8991 if (warn_missing_field_initializers
8993 && TREE_CODE (constructor_type) == RECORD_TYPE
8994 && constructor_unfilled_fields)
8996 /* Do not warn for flexible array members or zero-length arrays. */
8997 while (constructor_unfilled_fields
8998 && (!DECL_SIZE (constructor_unfilled_fields)
8999 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9000 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9002 if (constructor_unfilled_fields
9003 /* Do not warn if this level of the initializer uses member
9004 designators; it is likely to be deliberate. */
9005 && !constructor_designated
9006 /* Do not warn about initializing with { 0 } or with { }. */
9007 && !constructor_zeroinit)
9009 if (warning_at (input_location, OPT_Wmissing_field_initializers,
9010 "missing initializer for field %qD of %qT",
9011 constructor_unfilled_fields,
9013 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9014 "%qD declared here", constructor_unfilled_fields);
9018 /* Pad out the end of the structure. */
9019 if (p->replacement_value.value)
9020 /* If this closes a superfluous brace pair,
9021 just pass out the element between them. */
9022 ret = p->replacement_value;
9023 else if (constructor_type == NULL_TREE)
9025 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9026 && TREE_CODE (constructor_type) != ARRAY_TYPE
9027 && !gnu_vector_type_p (constructor_type))
9029 /* A nonincremental scalar initializer--just return
9030 the element, after verifying there is just one.
9031 Empty scalar initializers are supported in C2X. */
9032 if (vec_safe_is_empty (constructor_elements))
9034 if (constructor_erroneous || constructor_type == error_mark_node)
9035 ret.value = error_mark_node;
9037 ret.value = build_zero_cst (constructor_type);
9039 else if (vec_safe_length (constructor_elements) != 1)
9041 error_init (loc, "extra elements in scalar initializer");
9042 ret.value = (*constructor_elements)[0].value;
9045 ret.value = (*constructor_elements)[0].value;
9049 if (constructor_erroneous)
9050 ret.value = error_mark_node;
9053 ret.value = build_constructor (constructor_type,
9054 constructor_elements);
9055 if (constructor_constant)
9056 TREE_CONSTANT (ret.value) = 1;
9057 if (constructor_constant && constructor_simple)
9058 TREE_STATIC (ret.value) = 1;
9059 if (constructor_nonconst)
9060 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9064 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9066 if (constructor_nonconst)
9067 ret.original_code = C_MAYBE_CONST_EXPR;
9068 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9069 ret.original_code = ERROR_MARK;
9072 constructor_type = p->type;
9073 constructor_fields = p->fields;
9074 constructor_index = p->index;
9075 constructor_max_index = p->max_index;
9076 constructor_unfilled_index = p->unfilled_index;
9077 constructor_unfilled_fields = p->unfilled_fields;
9078 constructor_bit_index = p->bit_index;
9079 constructor_elements = p->elements;
9080 constructor_constant = p->constant;
9081 constructor_simple = p->simple;
9082 constructor_nonconst = p->nonconst;
9083 constructor_erroneous = p->erroneous;
9084 constructor_incremental = p->incremental;
9085 constructor_designated = p->designated;
9086 designator_depth = p->designator_depth;
9087 constructor_pending_elts = p->pending_elts;
9088 constructor_depth = p->depth;
9090 constructor_range_stack = p->range_stack;
9091 RESTORE_SPELLING_DEPTH (constructor_depth);
9093 constructor_stack = p->next;
9096 if (ret.value == NULL_TREE && constructor_stack == 0)
9097 ret.value = error_mark_node;
9101 /* Common handling for both array range and field name designators.
9102 ARRAY argument is nonzero for array ranges. Returns false for success. */
9105 set_designator (location_t loc, bool array,
9106 struct obstack *braced_init_obstack)
9109 enum tree_code subcode;
9111 /* Don't die if an entire brace-pair level is superfluous
9112 in the containing level, or for an erroneous type. */
9113 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9116 /* If there were errors in this designator list already, bail out
9118 if (designator_erroneous)
9121 /* Likewise for an initializer for a variable-size type. Those are
9122 diagnosed in the parser, except for empty initializer braces. */
9123 if (COMPLETE_TYPE_P (constructor_type)
9124 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9127 if (!designator_depth)
9129 gcc_assert (!constructor_range_stack);
9131 /* Designator list starts at the level of closest explicit
9133 while (constructor_stack->implicit)
9134 process_init_element (input_location,
9135 pop_init_level (loc, 1, braced_init_obstack,
9136 last_init_list_comma),
9137 true, braced_init_obstack);
9138 constructor_designated = 1;
9142 switch (TREE_CODE (constructor_type))
9146 subtype = TREE_TYPE (constructor_fields);
9147 if (subtype != error_mark_node)
9148 subtype = TYPE_MAIN_VARIANT (subtype);
9151 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9157 subcode = TREE_CODE (subtype);
9158 if (array && subcode != ARRAY_TYPE)
9160 error_init (loc, "array index in non-array initializer");
9163 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9165 error_init (loc, "field name not in record or union initializer");
9169 constructor_designated = 1;
9170 finish_implicit_inits (loc, braced_init_obstack);
9171 push_init_level (loc, 2, braced_init_obstack);
9175 /* If there are range designators in designator list, push a new designator
9176 to constructor_range_stack. RANGE_END is end of such stack range or
9177 NULL_TREE if there is no range designator at this level. */
9180 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9182 struct constructor_range_stack *p;
9184 p = (struct constructor_range_stack *)
9185 obstack_alloc (braced_init_obstack,
9186 sizeof (struct constructor_range_stack));
9187 p->prev = constructor_range_stack;
9189 p->fields = constructor_fields;
9190 p->range_start = constructor_index;
9191 p->index = constructor_index;
9192 p->stack = constructor_stack;
9193 p->range_end = range_end;
9194 if (constructor_range_stack)
9195 constructor_range_stack->next = p;
9196 constructor_range_stack = p;
9199 /* Within an array initializer, specify the next index to be initialized.
9200 FIRST is that index. If LAST is nonzero, then initialize a range
9201 of indices, running from FIRST through LAST. */
9204 set_init_index (location_t loc, tree first, tree last,
9205 struct obstack *braced_init_obstack)
9207 if (set_designator (loc, true, braced_init_obstack))
9210 designator_erroneous = 1;
9212 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9213 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9215 error_init (loc, "array index in initializer not of integer type");
9219 if (TREE_CODE (first) != INTEGER_CST)
9221 first = c_fully_fold (first, false, NULL);
9222 if (TREE_CODE (first) == INTEGER_CST)
9223 pedwarn_init (loc, OPT_Wpedantic,
9224 "array index in initializer is not "
9225 "an integer constant expression");
9228 if (last && TREE_CODE (last) != INTEGER_CST)
9230 last = c_fully_fold (last, false, NULL);
9231 if (TREE_CODE (last) == INTEGER_CST)
9232 pedwarn_init (loc, OPT_Wpedantic,
9233 "array index in initializer is not "
9234 "an integer constant expression");
9237 if (TREE_CODE (first) != INTEGER_CST)
9238 error_init (loc, "nonconstant array index in initializer");
9239 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9240 error_init (loc, "nonconstant array index in initializer");
9241 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9242 error_init (loc, "array index in non-array initializer");
9243 else if (tree_int_cst_sgn (first) == -1)
9244 error_init (loc, "array index in initializer exceeds array bounds");
9245 else if (constructor_max_index
9246 && tree_int_cst_lt (constructor_max_index, first))
9247 error_init (loc, "array index in initializer exceeds array bounds");
9250 constant_expression_warning (first);
9252 constant_expression_warning (last);
9253 constructor_index = convert (bitsizetype, first);
9254 if (tree_int_cst_lt (constructor_index, first))
9256 constructor_index = copy_node (constructor_index);
9257 TREE_OVERFLOW (constructor_index) = 1;
9262 if (tree_int_cst_equal (first, last))
9264 else if (tree_int_cst_lt (last, first))
9266 error_init (loc, "empty index range in initializer");
9271 last = convert (bitsizetype, last);
9272 if (constructor_max_index != NULL_TREE
9273 && tree_int_cst_lt (constructor_max_index, last))
9275 error_init (loc, "array index range in initializer exceeds "
9283 designator_erroneous = 0;
9284 if (constructor_range_stack || last)
9285 push_range_stack (last, braced_init_obstack);
9289 /* Within a struct initializer, specify the next field to be initialized. */
9292 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9293 struct obstack *braced_init_obstack)
9297 if (set_designator (loc, false, braced_init_obstack))
9300 designator_erroneous = 1;
9302 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9304 error_init (loc, "field name not in record or union initializer");
9308 field = lookup_field (constructor_type, fieldname);
9310 if (field == NULL_TREE)
9312 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9315 gcc_rich_location rich_loc (fieldname_loc);
9316 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9317 error_at (&rich_loc,
9318 "%qT has no member named %qE; did you mean %qE?",
9319 constructor_type, fieldname, guessed_id);
9322 error_at (fieldname_loc, "%qT has no member named %qE",
9323 constructor_type, fieldname);
9328 constructor_fields = TREE_VALUE (field);
9330 designator_erroneous = 0;
9331 if (constructor_range_stack)
9332 push_range_stack (NULL_TREE, braced_init_obstack);
9333 field = TREE_CHAIN (field);
9336 if (set_designator (loc, false, braced_init_obstack))
9340 while (field != NULL_TREE);
9343 /* Add a new initializer to the tree of pending initializers. PURPOSE
9344 identifies the initializer, either array index or field in a structure.
9345 VALUE is the value of that index or field. If ORIGTYPE is not
9346 NULL_TREE, it is the original type of VALUE.
9348 IMPLICIT is true if value comes from pop_init_level (1),
9349 the new initializer has been merged with the existing one
9350 and thus no warnings should be emitted about overriding an
9351 existing initializer. */
9354 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9355 bool implicit, struct obstack *braced_init_obstack)
9357 struct init_node *p, **q, *r;
9359 q = &constructor_pending_elts;
9362 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9367 if (tree_int_cst_lt (purpose, p->purpose))
9369 else if (tree_int_cst_lt (p->purpose, purpose))
9375 if (TREE_SIDE_EFFECTS (p->value))
9376 warning_init (loc, OPT_Woverride_init_side_effects,
9377 "initialized field with side-effects "
9379 else if (warn_override_init)
9380 warning_init (loc, OPT_Woverride_init,
9381 "initialized field overwritten");
9384 p->origtype = origtype;
9393 bitpos = bit_position (purpose);
9397 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9399 else if (p->purpose != purpose)
9405 if (TREE_SIDE_EFFECTS (p->value))
9406 warning_init (loc, OPT_Woverride_init_side_effects,
9407 "initialized field with side-effects "
9409 else if (warn_override_init)
9410 warning_init (loc, OPT_Woverride_init,
9411 "initialized field overwritten");
9414 p->origtype = origtype;
9420 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9421 sizeof (struct init_node));
9422 r->purpose = purpose;
9424 r->origtype = origtype;
9434 struct init_node *s;
9438 if (p->balance == 0)
9440 else if (p->balance < 0)
9447 p->left->parent = p;
9464 constructor_pending_elts = r;
9469 struct init_node *t = r->right;
9473 r->right->parent = r;
9478 p->left->parent = p;
9481 p->balance = t->balance < 0;
9482 r->balance = -(t->balance > 0);
9497 constructor_pending_elts = t;
9503 /* p->balance == +1; growth of left side balances the node. */
9508 else /* r == p->right */
9510 if (p->balance == 0)
9511 /* Growth propagation from right side. */
9513 else if (p->balance > 0)
9520 p->right->parent = p;
9537 constructor_pending_elts = r;
9539 else /* r->balance == -1 */
9542 struct init_node *t = r->left;
9546 r->left->parent = r;
9551 p->right->parent = p;
9554 r->balance = (t->balance < 0);
9555 p->balance = -(t->balance > 0);
9570 constructor_pending_elts = t;
9576 /* p->balance == -1; growth of right side balances the node. */
9587 /* Build AVL tree from a sorted chain. */
9590 set_nonincremental_init (struct obstack * braced_init_obstack)
9592 unsigned HOST_WIDE_INT ix;
9595 if (TREE_CODE (constructor_type) != RECORD_TYPE
9596 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9599 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9600 add_pending_init (input_location, index, value, NULL_TREE, true,
9601 braced_init_obstack);
9602 constructor_elements = NULL;
9603 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9605 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9606 /* Skip any nameless bit fields at the beginning. */
9607 while (constructor_unfilled_fields != NULL_TREE
9608 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9609 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9612 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9614 if (TYPE_DOMAIN (constructor_type))
9615 constructor_unfilled_index
9616 = convert (bitsizetype,
9617 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9619 constructor_unfilled_index = bitsize_zero_node;
9621 constructor_incremental = 0;
9624 /* Build AVL tree from a string constant. */
9627 set_nonincremental_init_from_string (tree str,
9628 struct obstack * braced_init_obstack)
9630 tree value, purpose, type;
9631 HOST_WIDE_INT val[2];
9632 const char *p, *end;
9633 int byte, wchar_bytes, charwidth, bitpos;
9635 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9637 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9638 charwidth = TYPE_PRECISION (char_type_node);
9639 gcc_assert ((size_t) wchar_bytes * charwidth
9640 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9641 type = TREE_TYPE (constructor_type);
9642 p = TREE_STRING_POINTER (str);
9643 end = p + TREE_STRING_LENGTH (str);
9645 for (purpose = bitsize_zero_node;
9647 && !(constructor_max_index
9648 && tree_int_cst_lt (constructor_max_index, purpose));
9649 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9651 if (wchar_bytes == 1)
9653 val[0] = (unsigned char) *p++;
9660 for (byte = 0; byte < wchar_bytes; byte++)
9662 if (BYTES_BIG_ENDIAN)
9663 bitpos = (wchar_bytes - byte - 1) * charwidth;
9665 bitpos = byte * charwidth;
9666 val[bitpos / HOST_BITS_PER_WIDE_INT]
9667 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9668 << (bitpos % HOST_BITS_PER_WIDE_INT);
9672 if (!TYPE_UNSIGNED (type))
9674 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9675 if (bitpos < HOST_BITS_PER_WIDE_INT)
9677 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9679 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9683 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9688 else if (val[1] & (HOST_WIDE_INT_1
9689 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9690 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9693 value = wide_int_to_tree (type,
9694 wide_int::from_array (val, 2,
9695 HOST_BITS_PER_WIDE_INT * 2));
9696 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9697 braced_init_obstack);
9700 constructor_incremental = 0;
9703 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9704 not initialized yet. */
9707 find_init_member (tree field, struct obstack * braced_init_obstack)
9709 struct init_node *p;
9711 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9713 if (constructor_incremental
9714 && tree_int_cst_lt (field, constructor_unfilled_index))
9715 set_nonincremental_init (braced_init_obstack);
9717 p = constructor_pending_elts;
9720 if (tree_int_cst_lt (field, p->purpose))
9722 else if (tree_int_cst_lt (p->purpose, field))
9728 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9730 tree bitpos = bit_position (field);
9732 if (constructor_incremental
9733 && (!constructor_unfilled_fields
9734 || tree_int_cst_lt (bitpos,
9735 bit_position (constructor_unfilled_fields))))
9736 set_nonincremental_init (braced_init_obstack);
9738 p = constructor_pending_elts;
9741 if (field == p->purpose)
9743 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9749 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9751 if (!vec_safe_is_empty (constructor_elements)
9752 && (constructor_elements->last ().index == field))
9753 return constructor_elements->last ().value;
9758 /* "Output" the next constructor element.
9759 At top level, really output it to assembler code now.
9760 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9761 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9762 TYPE is the data type that the containing data type wants here.
9763 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9764 If VALUE is a string constant, STRICT_STRING is true if it is
9765 unparenthesized or we should not warn here for it being parenthesized.
9766 For other types of VALUE, STRICT_STRING is not used.
9768 PENDING if true means output pending elements that belong
9769 right after this element. (PENDING is normally true;
9770 it is false while outputting pending elements, to avoid recursion.)
9772 IMPLICIT is true if value comes from pop_init_level (1),
9773 the new initializer has been merged with the existing one
9774 and thus no warnings should be emitted about overriding an
9775 existing initializer. */
9778 output_init_element (location_t loc, tree value, tree origtype,
9779 bool strict_string, tree type, tree field, bool pending,
9780 bool implicit, struct obstack * braced_init_obstack)
9782 tree semantic_type = NULL_TREE;
9783 bool maybe_const = true;
9786 if (type == error_mark_node || value == error_mark_node)
9788 constructor_erroneous = 1;
9791 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9792 && (TREE_CODE (value) == STRING_CST
9793 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9794 && !(TREE_CODE (value) == STRING_CST
9795 && TREE_CODE (type) == ARRAY_TYPE
9796 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9797 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9798 TYPE_MAIN_VARIANT (type)))
9799 value = array_to_pointer_conversion (input_location, value);
9801 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9802 && require_constant_value && pending)
9804 /* As an extension, allow initializing objects with static storage
9805 duration with compound literals (which are then treated just as
9806 the brace enclosed list they contain). */
9808 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9810 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9811 value = DECL_INITIAL (decl);
9814 npc = null_pointer_constant_p (value);
9815 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9817 semantic_type = TREE_TYPE (value);
9818 value = TREE_OPERAND (value, 0);
9820 value = c_fully_fold (value, require_constant_value, &maybe_const);
9822 if (value == error_mark_node)
9823 constructor_erroneous = 1;
9824 else if (!TREE_CONSTANT (value))
9825 constructor_constant = 0;
9826 else if (!initializer_constant_valid_p (value,
9828 AGGREGATE_TYPE_P (constructor_type)
9829 && TYPE_REVERSE_STORAGE_ORDER
9831 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9832 && DECL_C_BIT_FIELD (field)
9833 && TREE_CODE (value) != INTEGER_CST))
9834 constructor_simple = 0;
9836 constructor_nonconst = 1;
9838 /* Digest the initializer and issue any errors about incompatible
9839 types before issuing errors about non-constant initializers. */
9840 tree new_value = value;
9842 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9843 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9844 require_constant_value);
9845 if (new_value == error_mark_node)
9847 constructor_erroneous = 1;
9850 if (require_constant_value || require_constant_elements)
9851 constant_expression_warning (new_value);
9853 /* Proceed to check the constness of the original initializer. */
9854 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9856 if (require_constant_value)
9858 error_init (loc, "initializer element is not constant");
9859 value = error_mark_node;
9861 else if (require_constant_elements)
9862 pedwarn (loc, OPT_Wpedantic,
9863 "initializer element is not computable at load time");
9865 else if (!maybe_const
9866 && (require_constant_value || require_constant_elements))
9867 pedwarn_init (loc, OPT_Wpedantic,
9868 "initializer element is not a constant expression");
9870 /* Issue -Wc++-compat warnings about initializing a bitfield with
9873 && field != NULL_TREE
9874 && TREE_CODE (field) == FIELD_DECL
9875 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9876 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9877 != TYPE_MAIN_VARIANT (type))
9878 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9880 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9881 if (checktype != error_mark_node
9882 && (TYPE_MAIN_VARIANT (checktype)
9883 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9884 warning_init (loc, OPT_Wc___compat,
9885 "enum conversion in initialization is invalid in C++");
9888 /* If this field is empty and does not have side effects (and is not at
9889 the end of structure), don't do anything other than checking the
9892 && (TREE_TYPE (field) == error_mark_node
9893 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9894 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9895 && !TREE_SIDE_EFFECTS (new_value)
9896 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9897 || DECL_CHAIN (field)))))
9900 /* Finally, set VALUE to the initializer value digested above. */
9903 /* If this element doesn't come next in sequence,
9904 put it on constructor_pending_elts. */
9905 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9906 && (!constructor_incremental
9907 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9909 if (constructor_incremental
9910 && tree_int_cst_lt (field, constructor_unfilled_index))
9911 set_nonincremental_init (braced_init_obstack);
9913 add_pending_init (loc, field, value, origtype, implicit,
9914 braced_init_obstack);
9917 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9918 && (!constructor_incremental
9919 || field != constructor_unfilled_fields))
9921 /* We do this for records but not for unions. In a union,
9922 no matter which field is specified, it can be initialized
9923 right away since it starts at the beginning of the union. */
9924 if (constructor_incremental)
9926 if (!constructor_unfilled_fields)
9927 set_nonincremental_init (braced_init_obstack);
9930 tree bitpos, unfillpos;
9932 bitpos = bit_position (field);
9933 unfillpos = bit_position (constructor_unfilled_fields);
9935 if (tree_int_cst_lt (bitpos, unfillpos))
9936 set_nonincremental_init (braced_init_obstack);
9940 add_pending_init (loc, field, value, origtype, implicit,
9941 braced_init_obstack);
9944 else if (TREE_CODE (constructor_type) == UNION_TYPE
9945 && !vec_safe_is_empty (constructor_elements))
9949 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9950 warning_init (loc, OPT_Woverride_init_side_effects,
9951 "initialized field with side-effects overwritten");
9952 else if (warn_override_init)
9953 warning_init (loc, OPT_Woverride_init,
9954 "initialized field overwritten");
9957 /* We can have just one union field set. */
9958 constructor_elements = NULL;
9961 /* Otherwise, output this element either to
9962 constructor_elements or to the assembler file. */
9964 constructor_elt celt = {field, value};
9965 vec_safe_push (constructor_elements, celt);
9967 /* Advance the variable that indicates sequential elements output. */
9968 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9969 constructor_unfilled_index
9970 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9972 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9974 constructor_unfilled_fields
9975 = DECL_CHAIN (constructor_unfilled_fields);
9977 /* Skip any nameless bit fields. */
9978 while (constructor_unfilled_fields != NULL_TREE
9979 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9980 constructor_unfilled_fields =
9981 DECL_CHAIN (constructor_unfilled_fields);
9983 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9984 constructor_unfilled_fields = NULL_TREE;
9986 /* Now output any pending elements which have become next. */
9988 output_pending_init_elements (0, braced_init_obstack);
9991 /* For two FIELD_DECLs in the same chain, return -1 if field1
9992 comes before field2, 1 if field1 comes after field2 and
9993 0 if field1 == field2. */
9996 init_field_decl_cmp (tree field1, tree field2)
9998 if (field1 == field2)
10001 tree bitpos1 = bit_position (field1);
10002 tree bitpos2 = bit_position (field2);
10003 if (tree_int_cst_equal (bitpos1, bitpos2))
10005 /* If one of the fields has non-zero bitsize, then that
10006 field must be the last one in a sequence of zero
10007 sized fields, fields after it will have bigger
10009 if (TREE_TYPE (field1) != error_mark_node
10010 && COMPLETE_TYPE_P (TREE_TYPE (field1))
10011 && integer_nonzerop (TREE_TYPE (field1)))
10013 if (TREE_TYPE (field2) != error_mark_node
10014 && COMPLETE_TYPE_P (TREE_TYPE (field2))
10015 && integer_nonzerop (TREE_TYPE (field2)))
10017 /* Otherwise, fallback to DECL_CHAIN walk to find out
10018 which field comes earlier. Walk chains of both
10019 fields, so that if field1 and field2 are close to each
10020 other in either order, it is found soon even for large
10021 sequences of zero sized fields. */
10022 tree f1 = field1, f2 = field2;
10025 f1 = DECL_CHAIN (f1);
10026 f2 = DECL_CHAIN (f2);
10027 if (f1 == NULL_TREE)
10032 if (f2 == NULL_TREE)
10038 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10040 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10044 else if (tree_int_cst_lt (bitpos1, bitpos2))
10050 /* Output any pending elements which have become next.
10051 As we output elements, constructor_unfilled_{fields,index}
10052 advances, which may cause other elements to become next;
10053 if so, they too are output.
10055 If ALL is 0, we return when there are
10056 no more pending elements to output now.
10058 If ALL is 1, we output space as necessary so that
10059 we can output all the pending elements. */
10061 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10063 struct init_node *elt = constructor_pending_elts;
10068 /* Look through the whole pending tree.
10069 If we find an element that should be output now,
10070 output it. Otherwise, set NEXT to the element
10071 that comes first among those still pending. */
10076 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10078 if (tree_int_cst_equal (elt->purpose,
10079 constructor_unfilled_index))
10080 output_init_element (input_location, elt->value, elt->origtype,
10081 true, TREE_TYPE (constructor_type),
10082 constructor_unfilled_index, false, false,
10083 braced_init_obstack);
10084 else if (tree_int_cst_lt (constructor_unfilled_index,
10087 /* Advance to the next smaller node. */
10092 /* We have reached the smallest node bigger than the
10093 current unfilled index. Fill the space first. */
10094 next = elt->purpose;
10100 /* Advance to the next bigger node. */
10105 /* We have reached the biggest node in a subtree. Find
10106 the parent of it, which is the next bigger node. */
10107 while (elt->parent && elt->parent->right == elt)
10110 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10113 next = elt->purpose;
10119 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10121 /* If the current record is complete we are done. */
10122 if (constructor_unfilled_fields == NULL_TREE)
10125 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10128 output_init_element (input_location, elt->value, elt->origtype,
10129 true, TREE_TYPE (elt->purpose),
10130 elt->purpose, false, false,
10131 braced_init_obstack);
10134 /* Advance to the next smaller node. */
10139 /* We have reached the smallest node bigger than the
10140 current unfilled field. Fill the space first. */
10141 next = elt->purpose;
10147 /* Advance to the next bigger node. */
10152 /* We have reached the biggest node in a subtree. Find
10153 the parent of it, which is the next bigger node. */
10154 while (elt->parent && elt->parent->right == elt)
10158 && init_field_decl_cmp (constructor_unfilled_fields,
10161 next = elt->purpose;
10169 /* Ordinarily return, but not if we want to output all
10170 and there are elements left. */
10171 if (!(all && next != NULL_TREE))
10174 /* If it's not incremental, just skip over the gap, so that after
10175 jumping to retry we will output the next successive element. */
10176 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10177 constructor_unfilled_fields = next;
10178 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10179 constructor_unfilled_index = next;
10181 /* ELT now points to the node in the pending tree with the next
10182 initializer to output. */
10186 /* Expression VALUE coincides with the start of type TYPE in a braced
10187 initializer. Return true if we should treat VALUE as initializing
10188 the first element of TYPE, false if we should treat it as initializing
10191 If the initializer is clearly invalid, the question becomes:
10192 which choice gives the best error message? */
10195 initialize_elementwise_p (tree type, tree value)
10197 if (type == error_mark_node || value == error_mark_node)
10200 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10202 tree value_type = TREE_TYPE (value);
10203 if (value_type == error_mark_node)
10206 /* GNU vectors can be initialized elementwise. However, treat any
10207 kind of vector value as initializing the vector type as a whole,
10208 regardless of whether the value is a GNU vector. Such initializers
10209 are valid if and only if they would have been valid in a non-braced
10214 so recursing into the vector type would be at best confusing or at
10215 worst wrong. For example, when -flax-vector-conversions is in effect,
10216 it's possible to initialize a V8HI from a V4SI, even though the vectors
10217 have different element types and different numbers of elements. */
10218 if (gnu_vector_type_p (type))
10219 return !VECTOR_TYPE_P (value_type);
10221 if (AGGREGATE_TYPE_P (type))
10222 return type != TYPE_MAIN_VARIANT (value_type);
10227 /* Add one non-braced element to the current constructor level.
10228 This adjusts the current position within the constructor's type.
10229 This may also start or terminate implicit levels
10230 to handle a partly-braced initializer.
10232 Once this has found the correct level for the new element,
10233 it calls output_init_element.
10235 IMPLICIT is true if value comes from pop_init_level (1),
10236 the new initializer has been merged with the existing one
10237 and thus no warnings should be emitted about overriding an
10238 existing initializer. */
10241 process_init_element (location_t loc, struct c_expr value, bool implicit,
10242 struct obstack * braced_init_obstack)
10244 tree orig_value = value.value;
10246 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10247 bool strict_string = value.original_code == STRING_CST;
10248 bool was_designated = designator_depth != 0;
10250 designator_depth = 0;
10251 designator_erroneous = 0;
10253 if (!implicit && value.value && !integer_zerop (value.value))
10254 constructor_zeroinit = 0;
10256 /* Handle superfluous braces around string cst as in
10257 char x[] = {"foo"}; */
10259 && constructor_type
10261 && TREE_CODE (constructor_type) == ARRAY_TYPE
10262 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10263 && integer_zerop (constructor_unfilled_index))
10265 if (constructor_stack->replacement_value.value)
10266 error_init (loc, "excess elements in %<char%> array initializer");
10267 constructor_stack->replacement_value = value;
10271 if (constructor_stack->replacement_value.value != NULL_TREE)
10273 error_init (loc, "excess elements in struct initializer");
10277 /* Ignore elements of a brace group if it is entirely superfluous
10278 and has already been diagnosed, or if the type is erroneous. */
10279 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10282 /* Ignore elements of an initializer for a variable-size type.
10283 Those are diagnosed in the parser (empty initializer braces are OK). */
10284 if (COMPLETE_TYPE_P (constructor_type)
10285 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10288 if (!implicit && warn_designated_init && !was_designated
10289 && TREE_CODE (constructor_type) == RECORD_TYPE
10290 && lookup_attribute ("designated_init",
10291 TYPE_ATTRIBUTES (constructor_type)))
10293 OPT_Wdesignated_init,
10294 "positional initialization of field "
10295 "in %<struct%> declared with %<designated_init%> attribute");
10297 /* If we've exhausted any levels that didn't have braces,
10299 while (constructor_stack->implicit)
10301 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10302 && constructor_fields == NULL_TREE)
10303 process_init_element (loc,
10304 pop_init_level (loc, 1, braced_init_obstack,
10305 last_init_list_comma),
10306 true, braced_init_obstack);
10307 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10308 || gnu_vector_type_p (constructor_type))
10309 && constructor_max_index
10310 && tree_int_cst_lt (constructor_max_index,
10311 constructor_index))
10312 process_init_element (loc,
10313 pop_init_level (loc, 1, braced_init_obstack,
10314 last_init_list_comma),
10315 true, braced_init_obstack);
10320 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10321 if (constructor_range_stack)
10323 /* If value is a compound literal and we'll be just using its
10324 content, don't put it into a SAVE_EXPR. */
10325 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10326 || !require_constant_value)
10328 tree semantic_type = NULL_TREE;
10329 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10331 semantic_type = TREE_TYPE (value.value);
10332 value.value = TREE_OPERAND (value.value, 0);
10334 value.value = save_expr (value.value);
10336 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10343 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10346 enum tree_code fieldcode;
10348 if (constructor_fields == NULL_TREE)
10350 pedwarn_init (loc, 0, "excess elements in struct initializer");
10354 fieldtype = TREE_TYPE (constructor_fields);
10355 if (fieldtype != error_mark_node)
10356 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10357 fieldcode = TREE_CODE (fieldtype);
10359 /* Error for non-static initialization of a flexible array member. */
10360 if (fieldcode == ARRAY_TYPE
10361 && !require_constant_value
10362 && TYPE_SIZE (fieldtype) == NULL_TREE
10363 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10365 error_init (loc, "non-static initialization of a flexible "
10370 /* Error for initialization of a flexible array member with
10371 a string constant if the structure is in an array. E.g.:
10372 struct S { int x; char y[]; };
10373 struct S s[] = { { 1, "foo" } };
10376 && fieldcode == ARRAY_TYPE
10377 && constructor_depth > 1
10378 && TYPE_SIZE (fieldtype) == NULL_TREE
10379 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10381 bool in_array_p = false;
10382 for (struct constructor_stack *p = constructor_stack;
10383 p && p->type; p = p->next)
10384 if (TREE_CODE (p->type) == ARRAY_TYPE)
10391 error_init (loc, "initialization of flexible array "
10392 "member in a nested context");
10397 /* Accept a string constant to initialize a subarray. */
10398 if (value.value != NULL_TREE
10399 && fieldcode == ARRAY_TYPE
10400 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10402 value.value = orig_value;
10403 /* Otherwise, if we have come to a subaggregate,
10404 and we don't have an element of its type, push into it. */
10405 else if (value.value != NULL_TREE
10406 && initialize_elementwise_p (fieldtype, value.value))
10408 push_init_level (loc, 1, braced_init_obstack);
10414 push_member_name (constructor_fields);
10415 output_init_element (loc, value.value, value.original_type,
10416 strict_string, fieldtype,
10417 constructor_fields, true, implicit,
10418 braced_init_obstack);
10419 RESTORE_SPELLING_DEPTH (constructor_depth);
10422 /* Do the bookkeeping for an element that was
10423 directly output as a constructor. */
10425 /* For a record, keep track of end position of last field. */
10426 if (DECL_SIZE (constructor_fields))
10427 constructor_bit_index
10428 = size_binop_loc (input_location, PLUS_EXPR,
10429 bit_position (constructor_fields),
10430 DECL_SIZE (constructor_fields));
10432 /* If the current field was the first one not yet written out,
10433 it isn't now, so update. */
10434 if (constructor_unfilled_fields == constructor_fields)
10436 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10437 /* Skip any nameless bit fields. */
10438 while (constructor_unfilled_fields != 0
10439 && (DECL_UNNAMED_BIT_FIELD
10440 (constructor_unfilled_fields)))
10441 constructor_unfilled_fields =
10442 DECL_CHAIN (constructor_unfilled_fields);
10446 constructor_fields = DECL_CHAIN (constructor_fields);
10447 /* Skip any nameless bit fields at the beginning. */
10448 while (constructor_fields != NULL_TREE
10449 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10450 constructor_fields = DECL_CHAIN (constructor_fields);
10452 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10455 enum tree_code fieldcode;
10457 if (constructor_fields == NULL_TREE)
10459 pedwarn_init (loc, 0,
10460 "excess elements in union initializer");
10464 fieldtype = TREE_TYPE (constructor_fields);
10465 if (fieldtype != error_mark_node)
10466 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10467 fieldcode = TREE_CODE (fieldtype);
10469 /* Warn that traditional C rejects initialization of unions.
10470 We skip the warning if the value is zero. This is done
10471 under the assumption that the zero initializer in user
10472 code appears conditioned on e.g. __STDC__ to avoid
10473 "missing initializer" warnings and relies on default
10474 initialization to zero in the traditional C case.
10475 We also skip the warning if the initializer is designated,
10476 again on the assumption that this must be conditional on
10477 __STDC__ anyway (and we've already complained about the
10478 member-designator already). */
10479 if (!in_system_header_at (input_location) && !constructor_designated
10480 && !(value.value && (integer_zerop (value.value)
10481 || real_zerop (value.value))))
10482 warning (OPT_Wtraditional, "traditional C rejects initialization "
10485 /* Accept a string constant to initialize a subarray. */
10486 if (value.value != NULL_TREE
10487 && fieldcode == ARRAY_TYPE
10488 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10490 value.value = orig_value;
10491 /* Otherwise, if we have come to a subaggregate,
10492 and we don't have an element of its type, push into it. */
10493 else if (value.value != NULL_TREE
10494 && initialize_elementwise_p (fieldtype, value.value))
10496 push_init_level (loc, 1, braced_init_obstack);
10502 push_member_name (constructor_fields);
10503 output_init_element (loc, value.value, value.original_type,
10504 strict_string, fieldtype,
10505 constructor_fields, true, implicit,
10506 braced_init_obstack);
10507 RESTORE_SPELLING_DEPTH (constructor_depth);
10510 /* Do the bookkeeping for an element that was
10511 directly output as a constructor. */
10513 constructor_bit_index = DECL_SIZE (constructor_fields);
10514 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10517 constructor_fields = NULL_TREE;
10519 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10521 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10522 enum tree_code eltcode = TREE_CODE (elttype);
10524 /* Accept a string constant to initialize a subarray. */
10525 if (value.value != NULL_TREE
10526 && eltcode == ARRAY_TYPE
10527 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10529 value.value = orig_value;
10530 /* Otherwise, if we have come to a subaggregate,
10531 and we don't have an element of its type, push into it. */
10532 else if (value.value != NULL_TREE
10533 && initialize_elementwise_p (elttype, value.value))
10535 push_init_level (loc, 1, braced_init_obstack);
10539 if (constructor_max_index != NULL_TREE
10540 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10541 || integer_all_onesp (constructor_max_index)))
10543 pedwarn_init (loc, 0,
10544 "excess elements in array initializer");
10548 /* Now output the actual element. */
10551 push_array_bounds (tree_to_uhwi (constructor_index));
10552 output_init_element (loc, value.value, value.original_type,
10553 strict_string, elttype,
10554 constructor_index, true, implicit,
10555 braced_init_obstack);
10556 RESTORE_SPELLING_DEPTH (constructor_depth);
10560 = size_binop_loc (input_location, PLUS_EXPR,
10561 constructor_index, bitsize_one_node);
10564 /* If we are doing the bookkeeping for an element that was
10565 directly output as a constructor, we must update
10566 constructor_unfilled_index. */
10567 constructor_unfilled_index = constructor_index;
10569 else if (gnu_vector_type_p (constructor_type))
10571 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10573 /* Do a basic check of initializer size. Note that vectors
10574 always have a fixed size derived from their type. */
10575 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10577 pedwarn_init (loc, 0,
10578 "excess elements in vector initializer");
10582 /* Now output the actual element. */
10585 if (TREE_CODE (value.value) == VECTOR_CST)
10586 elttype = TYPE_MAIN_VARIANT (constructor_type);
10587 output_init_element (loc, value.value, value.original_type,
10588 strict_string, elttype,
10589 constructor_index, true, implicit,
10590 braced_init_obstack);
10594 = size_binop_loc (input_location,
10595 PLUS_EXPR, constructor_index, bitsize_one_node);
10598 /* If we are doing the bookkeeping for an element that was
10599 directly output as a constructor, we must update
10600 constructor_unfilled_index. */
10601 constructor_unfilled_index = constructor_index;
10604 /* Handle the sole element allowed in a braced initializer
10605 for a scalar variable. */
10606 else if (constructor_type != error_mark_node
10607 && constructor_fields == NULL_TREE)
10609 pedwarn_init (loc, 0,
10610 "excess elements in scalar initializer");
10616 output_init_element (loc, value.value, value.original_type,
10617 strict_string, constructor_type,
10618 NULL_TREE, true, implicit,
10619 braced_init_obstack);
10620 constructor_fields = NULL_TREE;
10623 /* Handle range initializers either at this level or anywhere higher
10624 in the designator stack. */
10625 if (constructor_range_stack)
10627 struct constructor_range_stack *p, *range_stack;
10630 range_stack = constructor_range_stack;
10631 constructor_range_stack = 0;
10632 while (constructor_stack != range_stack->stack)
10634 gcc_assert (constructor_stack->implicit);
10635 process_init_element (loc,
10636 pop_init_level (loc, 1,
10637 braced_init_obstack,
10638 last_init_list_comma),
10639 true, braced_init_obstack);
10641 for (p = range_stack;
10642 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10645 gcc_assert (constructor_stack->implicit);
10646 process_init_element (loc,
10647 pop_init_level (loc, 1,
10648 braced_init_obstack,
10649 last_init_list_comma),
10650 true, braced_init_obstack);
10653 p->index = size_binop_loc (input_location,
10654 PLUS_EXPR, p->index, bitsize_one_node);
10655 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10660 constructor_index = p->index;
10661 constructor_fields = p->fields;
10662 if (finish && p->range_end && p->index == p->range_start)
10670 finish_implicit_inits (loc, braced_init_obstack);
10671 push_init_level (loc, 2, braced_init_obstack);
10672 p->stack = constructor_stack;
10673 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10674 p->index = p->range_start;
10678 constructor_range_stack = range_stack;
10685 constructor_range_stack = 0;
10688 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10689 (guaranteed to be 'volatile' or null) and ARGS (represented using
10690 an ASM_EXPR node). */
10692 build_asm_stmt (bool is_volatile, tree args)
10695 ASM_VOLATILE_P (args) = 1;
10696 return add_stmt (args);
10699 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10700 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10701 SIMPLE indicates whether there was anything at all after the
10702 string in the asm expression -- asm("blah") and asm("blah" : )
10703 are subtly different. We use a ASM_EXPR node to represent this.
10704 LOC is the location of the asm, and IS_INLINE says whether this
10707 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10708 tree clobbers, tree labels, bool simple, bool is_inline)
10713 const char *constraint;
10714 const char **oconstraints;
10715 bool allows_mem, allows_reg, is_inout;
10716 int ninputs, noutputs;
10718 ninputs = list_length (inputs);
10719 noutputs = list_length (outputs);
10720 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10722 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10724 /* Remove output conversions that change the type but not the mode. */
10725 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10727 tree output = TREE_VALUE (tail);
10729 output = c_fully_fold (output, false, NULL, true);
10731 /* ??? Really, this should not be here. Users should be using a
10732 proper lvalue, dammit. But there's a long history of using casts
10733 in the output operands. In cases like longlong.h, this becomes a
10734 primitive form of typechecking -- if the cast can be removed, then
10735 the output operand had a type of the proper width; otherwise we'll
10736 get an error. Gross, but ... */
10737 STRIP_NOPS (output);
10739 if (!lvalue_or_else (loc, output, lv_asm))
10740 output = error_mark_node;
10742 if (output != error_mark_node
10743 && (TREE_READONLY (output)
10744 || TYPE_READONLY (TREE_TYPE (output))
10745 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10746 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10747 readonly_error (loc, output, lv_asm);
10749 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10750 oconstraints[i] = constraint;
10752 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10753 &allows_mem, &allows_reg, &is_inout))
10755 /* If the operand is going to end up in memory,
10756 mark it addressable. */
10757 if (!allows_reg && !c_mark_addressable (output))
10758 output = error_mark_node;
10759 if (!(!allows_reg && allows_mem)
10760 && output != error_mark_node
10761 && VOID_TYPE_P (TREE_TYPE (output)))
10763 error_at (loc, "invalid use of void expression");
10764 output = error_mark_node;
10768 output = error_mark_node;
10770 TREE_VALUE (tail) = output;
10773 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10777 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10778 input = TREE_VALUE (tail);
10780 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10781 oconstraints, &allows_mem, &allows_reg))
10783 /* If the operand is going to end up in memory,
10784 mark it addressable. */
10785 if (!allows_reg && allows_mem)
10787 input = c_fully_fold (input, false, NULL, true);
10789 /* Strip the nops as we allow this case. FIXME, this really
10790 should be rejected or made deprecated. */
10791 STRIP_NOPS (input);
10792 if (!c_mark_addressable (input))
10793 input = error_mark_node;
10797 struct c_expr expr;
10798 memset (&expr, 0, sizeof (expr));
10799 expr.value = input;
10800 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10801 input = c_fully_fold (expr.value, false, NULL);
10803 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10805 error_at (loc, "invalid use of void expression");
10806 input = error_mark_node;
10811 input = error_mark_node;
10813 TREE_VALUE (tail) = input;
10816 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10818 /* asm statements without outputs, including simple ones, are treated
10820 ASM_INPUT_P (args) = simple;
10821 ASM_VOLATILE_P (args) = (noutputs == 0);
10822 ASM_INLINE_P (args) = is_inline;
10827 /* Generate a goto statement to LABEL. LOC is the location of the
10831 c_finish_goto_label (location_t loc, tree label)
10833 tree decl = lookup_label_for_goto (loc, label);
10836 TREE_USED (decl) = 1;
10838 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10839 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10840 SET_EXPR_LOCATION (t, loc);
10841 return add_stmt (t);
10845 /* Generate a computed goto statement to EXPR. LOC is the location of
10849 c_finish_goto_ptr (location_t loc, c_expr val)
10851 tree expr = val.value;
10853 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10854 if (expr != error_mark_node
10855 && !POINTER_TYPE_P (TREE_TYPE (expr))
10856 && !null_pointer_constant_p (expr))
10858 error_at (val.get_location (),
10859 "computed goto must be pointer type");
10860 expr = build_zero_cst (ptr_type_node);
10862 expr = c_fully_fold (expr, false, NULL);
10863 expr = convert (ptr_type_node, expr);
10864 t = build1 (GOTO_EXPR, void_type_node, expr);
10865 SET_EXPR_LOCATION (t, loc);
10866 return add_stmt (t);
10869 /* Generate a C `return' statement. RETVAL is the expression for what
10870 to return, or a null pointer for `return;' with no value. LOC is
10871 the location of the return statement, or the location of the expression,
10872 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10873 is the original type of RETVAL. */
10876 c_finish_return (location_t loc, tree retval, tree origtype)
10878 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10879 bool no_warning = false;
10882 /* Use the expansion point to handle cases such as returning NULL
10883 in a function returning void. */
10884 location_t xloc = expansion_point_location_if_in_system_header (loc);
10886 if (TREE_THIS_VOLATILE (current_function_decl))
10887 warning_at (xloc, 0,
10888 "function declared %<noreturn%> has a %<return%> statement");
10892 tree semantic_type = NULL_TREE;
10893 npc = null_pointer_constant_p (retval);
10894 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10896 semantic_type = TREE_TYPE (retval);
10897 retval = TREE_OPERAND (retval, 0);
10899 retval = c_fully_fold (retval, false, NULL);
10901 && valtype != NULL_TREE
10902 && TREE_CODE (valtype) != VOID_TYPE)
10903 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10908 current_function_returns_null = 1;
10909 if ((warn_return_type >= 0 || flag_isoc99)
10910 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10914 warned_here = pedwarn
10915 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10916 "%<return%> with no value, in function returning non-void");
10918 warned_here = warning_at
10919 (loc, OPT_Wreturn_type,
10920 "%<return%> with no value, in function returning non-void");
10923 inform (DECL_SOURCE_LOCATION (current_function_decl),
10927 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10929 current_function_returns_null = 1;
10931 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10932 warned_here = pedwarn
10933 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10934 "%<return%> with a value, in function returning void");
10936 warned_here = pedwarn
10937 (xloc, OPT_Wpedantic, "ISO C forbids "
10938 "%<return%> with expression, in function returning void");
10940 inform (DECL_SOURCE_LOCATION (current_function_decl),
10945 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10946 retval, origtype, ic_return,
10947 npc, NULL_TREE, NULL_TREE, 0);
10948 tree res = DECL_RESULT (current_function_decl);
10952 current_function_returns_value = 1;
10953 if (t == error_mark_node)
10956 save = in_late_binary_op;
10957 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10958 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10959 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10960 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10961 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10962 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10963 in_late_binary_op = true;
10964 inner = t = convert (TREE_TYPE (res), t);
10965 in_late_binary_op = save;
10967 /* Strip any conversions, additions, and subtractions, and see if
10968 we are returning the address of a local variable. Warn if so. */
10971 switch (TREE_CODE (inner))
10974 case NON_LVALUE_EXPR:
10976 case POINTER_PLUS_EXPR:
10977 inner = TREE_OPERAND (inner, 0);
10981 /* If the second operand of the MINUS_EXPR has a pointer
10982 type (or is converted from it), this may be valid, so
10983 don't give a warning. */
10985 tree op1 = TREE_OPERAND (inner, 1);
10987 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10988 && (CONVERT_EXPR_P (op1)
10989 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10990 op1 = TREE_OPERAND (op1, 0);
10992 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10995 inner = TREE_OPERAND (inner, 0);
11000 inner = TREE_OPERAND (inner, 0);
11002 while (REFERENCE_CLASS_P (inner)
11003 && !INDIRECT_REF_P (inner))
11004 inner = TREE_OPERAND (inner, 0);
11007 && !DECL_EXTERNAL (inner)
11008 && !TREE_STATIC (inner)
11009 && DECL_CONTEXT (inner) == current_function_decl
11010 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11012 if (TREE_CODE (inner) == LABEL_DECL)
11013 warning_at (loc, OPT_Wreturn_local_addr,
11014 "function returns address of label");
11017 warning_at (loc, OPT_Wreturn_local_addr,
11018 "function returns address of local variable");
11019 tree zero = build_zero_cst (TREE_TYPE (res));
11020 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11032 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11033 SET_EXPR_LOCATION (retval, loc);
11035 if (warn_sequence_point)
11036 verify_sequence_points (retval);
11039 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11041 suppress_warning (ret_stmt, OPT_Wreturn_type);
11042 return add_stmt (ret_stmt);
11046 /* The SWITCH_STMT being built. */
11049 /* The original type of the testing expression, i.e. before the
11050 default conversion is applied. */
11053 /* A splay-tree mapping the low element of a case range to the high
11054 element, or NULL_TREE if there is no high element. Used to
11055 determine whether or not a new case label duplicates an old case
11056 label. We need a tree, rather than simply a hash table, because
11057 of the GNU case range extension. */
11060 /* The bindings at the point of the switch. This is used for
11061 warnings crossing decls when branching to a case label. */
11062 struct c_spot_bindings *bindings;
11064 /* Whether the switch includes any break statements. */
11065 bool break_stmt_seen_p;
11067 /* The next node on the stack. */
11068 struct c_switch *next;
11070 /* Remember whether the controlling expression had boolean type
11071 before integer promotions for the sake of -Wswitch-bool. */
11075 /* A stack of the currently active switch statements. The innermost
11076 switch statement is on the top of the stack. There is no need to
11077 mark the stack for garbage collection because it is only active
11078 during the processing of the body of a function, and we never
11079 collect at that point. */
11081 struct c_switch *c_switch_stack;
11083 /* Start a C switch statement, testing expression EXP. Return the new
11084 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11085 SWITCH_COND_LOC is the location of the switch's condition.
11086 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11089 c_start_switch (location_t switch_loc,
11090 location_t switch_cond_loc,
11091 tree exp, bool explicit_cast_p)
11093 tree orig_type = error_mark_node;
11094 bool bool_cond_p = false;
11095 struct c_switch *cs;
11097 if (exp != error_mark_node)
11099 orig_type = TREE_TYPE (exp);
11101 if (!INTEGRAL_TYPE_P (orig_type))
11103 if (orig_type != error_mark_node)
11105 error_at (switch_cond_loc, "switch quantity not an integer");
11106 orig_type = error_mark_node;
11108 exp = integer_zero_node;
11112 tree type = TYPE_MAIN_VARIANT (orig_type);
11115 /* Warn if the condition has boolean value. */
11116 while (TREE_CODE (e) == COMPOUND_EXPR)
11117 e = TREE_OPERAND (e, 1);
11119 if ((TREE_CODE (type) == BOOLEAN_TYPE
11120 || truth_value_p (TREE_CODE (e)))
11121 /* Explicit cast to int suppresses this warning. */
11122 && !(TREE_CODE (type) == INTEGER_TYPE
11123 && explicit_cast_p))
11124 bool_cond_p = true;
11126 if (!in_system_header_at (input_location)
11127 && (type == long_integer_type_node
11128 || type == long_unsigned_type_node))
11129 warning_at (switch_cond_loc,
11130 OPT_Wtraditional, "%<long%> switch expression not "
11131 "converted to %<int%> in ISO C");
11133 exp = c_fully_fold (exp, false, NULL);
11134 exp = default_conversion (exp);
11136 if (warn_sequence_point)
11137 verify_sequence_points (exp);
11141 /* Add this new SWITCH_STMT to the stack. */
11142 cs = XNEW (struct c_switch);
11143 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11144 NULL_TREE, orig_type, NULL_TREE);
11145 cs->orig_type = orig_type;
11146 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11147 cs->bindings = c_get_switch_bindings ();
11148 cs->break_stmt_seen_p = false;
11149 cs->bool_cond_p = bool_cond_p;
11150 cs->next = c_switch_stack;
11151 c_switch_stack = cs;
11153 return add_stmt (cs->switch_stmt);
11156 /* Process a case label at location LOC. */
11159 do_case (location_t loc, tree low_value, tree high_value)
11161 tree label = NULL_TREE;
11163 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11165 low_value = c_fully_fold (low_value, false, NULL);
11166 if (TREE_CODE (low_value) == INTEGER_CST)
11167 pedwarn (loc, OPT_Wpedantic,
11168 "case label is not an integer constant expression");
11171 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11173 high_value = c_fully_fold (high_value, false, NULL);
11174 if (TREE_CODE (high_value) == INTEGER_CST)
11175 pedwarn (input_location, OPT_Wpedantic,
11176 "case label is not an integer constant expression");
11179 if (c_switch_stack == NULL)
11182 error_at (loc, "case label not within a switch statement");
11184 error_at (loc, "%<default%> label not within a switch statement");
11188 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11189 EXPR_LOCATION (c_switch_stack->switch_stmt),
11193 label = c_add_case_label (loc, c_switch_stack->cases,
11194 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11195 low_value, high_value);
11196 if (label == error_mark_node)
11201 /* Finish the switch statement. TYPE is the original type of the
11202 controlling expression of the switch, or NULL_TREE. */
11205 c_finish_switch (tree body, tree type)
11207 struct c_switch *cs = c_switch_stack;
11208 location_t switch_location;
11210 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11212 /* Emit warnings as needed. */
11213 switch_location = EXPR_LOCATION (cs->switch_stmt);
11214 c_do_switch_warnings (cs->cases, switch_location,
11215 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11216 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11217 if (c_switch_covers_all_cases_p (cs->cases,
11218 SWITCH_STMT_TYPE (cs->switch_stmt)))
11219 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11220 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11222 /* Pop the stack. */
11223 c_switch_stack = cs->next;
11224 splay_tree_delete (cs->cases);
11225 c_release_switch_bindings (cs->bindings);
11229 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11230 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11234 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11239 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11240 SET_EXPR_LOCATION (stmt, if_locus);
11245 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11247 /* In switch statements break is sometimes stylistically used after
11248 a return statement. This can lead to spurious warnings about
11249 control reaching the end of a non-void function when it is
11250 inlined. Note that we are calling block_may_fallthru with
11251 language specific tree nodes; this works because
11252 block_may_fallthru returns true when given something it does not
11254 bool skip = !block_may_fallthru (cur_stmt_list);
11257 switch (in_statement)
11260 error_at (loc, "break statement not within loop or switch");
11263 error_at (loc, "invalid exit from OpenMP structured block");
11266 error_at (loc, "break statement used with OpenMP for loop");
11268 case IN_ITERATION_STMT:
11269 case IN_OBJC_FOREACH:
11272 gcc_assert (in_statement & IN_SWITCH_STMT);
11273 c_switch_stack->break_stmt_seen_p = true;
11277 switch (in_statement & ~IN_SWITCH_STMT)
11280 error_at (loc, "continue statement not within a loop");
11283 error_at (loc, "invalid exit from OpenMP structured block");
11285 case IN_ITERATION_STMT:
11287 case IN_OBJC_FOREACH:
11290 gcc_unreachable ();
11295 else if ((in_statement & IN_OBJC_FOREACH)
11296 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11298 /* The foreach expander produces low-level code using gotos instead
11299 of a structured loop construct. */
11300 gcc_assert (label);
11301 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11303 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11306 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11309 emit_side_effect_warnings (location_t loc, tree expr)
11311 maybe_warn_nodiscard (loc, expr);
11312 if (!warn_unused_value)
11314 if (expr == error_mark_node)
11316 else if (!TREE_SIDE_EFFECTS (expr))
11318 if (!VOID_TYPE_P (TREE_TYPE (expr))
11319 && !warning_suppressed_p (expr, OPT_Wunused_value))
11320 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11322 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11325 location_t cloc = loc;
11326 while (TREE_CODE (r) == COMPOUND_EXPR)
11328 if (EXPR_HAS_LOCATION (r))
11329 cloc = EXPR_LOCATION (r);
11330 r = TREE_OPERAND (r, 1);
11332 if (!TREE_SIDE_EFFECTS (r)
11333 && !VOID_TYPE_P (TREE_TYPE (r))
11334 && !CONVERT_EXPR_P (r)
11335 && !warning_suppressed_p (r, OPT_Wunused_value)
11336 && !warning_suppressed_p (expr, OPT_Wunused_value))
11337 warning_at (cloc, OPT_Wunused_value,
11338 "right-hand operand of comma expression has no effect");
11341 warn_if_unused_value (expr, loc);
11344 /* Process an expression as if it were a complete statement. Emit
11345 diagnostics, but do not call ADD_STMT. LOC is the location of the
11349 c_process_expr_stmt (location_t loc, tree expr)
11356 expr = c_fully_fold (expr, false, NULL);
11358 if (warn_sequence_point)
11359 verify_sequence_points (expr);
11361 if (TREE_TYPE (expr) != error_mark_node
11362 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11363 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11364 error_at (loc, "expression statement has incomplete type");
11366 /* If we're not processing a statement expression, warn about unused values.
11367 Warnings for statement expressions will be emitted later, once we figure
11368 out which is the result. */
11369 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11370 && (warn_unused_value || warn_unused_result))
11371 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11374 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11375 exprv = TREE_OPERAND (exprv, 1);
11376 while (CONVERT_EXPR_P (exprv))
11377 exprv = TREE_OPERAND (exprv, 0);
11379 || handled_component_p (exprv)
11380 || TREE_CODE (exprv) == ADDR_EXPR)
11381 mark_exp_read (exprv);
11383 /* If the expression is not of a type to which we cannot assign a line
11384 number, wrap the thing in a no-op NOP_EXPR. */
11385 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11387 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11388 SET_EXPR_LOCATION (expr, loc);
11394 /* Emit an expression as a statement. LOC is the location of the
11398 c_finish_expr_stmt (location_t loc, tree expr)
11401 return add_stmt (c_process_expr_stmt (loc, expr));
11406 /* Do the opposite and emit a statement as an expression. To begin,
11407 create a new binding level and return it. */
11410 c_begin_stmt_expr (void)
11414 /* We must force a BLOCK for this level so that, if it is not expanded
11415 later, there is a way to turn off the entire subtree of blocks that
11416 are contained in it. */
11417 keep_next_level ();
11418 ret = c_begin_compound_stmt (true);
11420 c_bindings_start_stmt_expr (c_switch_stack == NULL
11422 : c_switch_stack->bindings);
11424 /* Mark the current statement list as belonging to a statement list. */
11425 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11430 /* LOC is the location of the compound statement to which this body
11434 c_finish_stmt_expr (location_t loc, tree body)
11436 tree last, type, tmp, val;
11439 body = c_end_compound_stmt (loc, body, true);
11441 c_bindings_end_stmt_expr (c_switch_stack == NULL
11443 : c_switch_stack->bindings);
11445 /* Locate the last statement in BODY. See c_end_compound_stmt
11446 about always returning a BIND_EXPR. */
11447 last_p = &BIND_EXPR_BODY (body);
11448 last = BIND_EXPR_BODY (body);
11450 continue_searching:
11451 if (TREE_CODE (last) == STATEMENT_LIST)
11453 tree_stmt_iterator l = tsi_last (last);
11455 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11458 /* This can happen with degenerate cases like ({ }). No value. */
11462 /* If we're supposed to generate side effects warnings, process
11463 all of the statements except the last. */
11464 if (warn_unused_value || warn_unused_result)
11466 for (tree_stmt_iterator i = tsi_start (last);
11467 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11470 tree t = tsi_stmt (i);
11472 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11473 emit_side_effect_warnings (tloc, t);
11476 last_p = tsi_stmt_ptr (l);
11480 /* If the end of the list is exception related, then the list was split
11481 by a call to push_cleanup. Continue searching. */
11482 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11483 || TREE_CODE (last) == TRY_CATCH_EXPR)
11485 last_p = &TREE_OPERAND (last, 0);
11487 goto continue_searching;
11490 if (last == error_mark_node)
11493 /* In the case that the BIND_EXPR is not necessary, return the
11494 expression out from inside it. */
11495 if ((last == BIND_EXPR_BODY (body)
11496 /* Skip nested debug stmts. */
11497 || last == expr_first (BIND_EXPR_BODY (body)))
11498 && BIND_EXPR_VARS (body) == NULL)
11500 /* Even if this looks constant, do not allow it in a constant
11502 last = c_wrap_maybe_const (last, true);
11503 /* Do not warn if the return value of a statement expression is
11505 suppress_warning (last, OPT_Wunused);
11509 /* Extract the type of said expression. */
11510 type = TREE_TYPE (last);
11512 /* If we're not returning a value at all, then the BIND_EXPR that
11513 we already have is a fine expression to return. */
11514 if (!type || VOID_TYPE_P (type))
11517 /* Now that we've located the expression containing the value, it seems
11518 silly to make voidify_wrapper_expr repeat the process. Create a
11519 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11520 tmp = create_tmp_var_raw (type);
11522 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11523 tree_expr_nonnegative_p giving up immediately. */
11525 if (TREE_CODE (val) == NOP_EXPR
11526 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11527 val = TREE_OPERAND (val, 0);
11529 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11530 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11533 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11534 SET_EXPR_LOCATION (t, loc);
11539 /* Begin and end compound statements. This is as simple as pushing
11540 and popping new statement lists from the tree. */
11543 c_begin_compound_stmt (bool do_scope)
11545 tree stmt = push_stmt_list ();
11551 /* End a compound statement. STMT is the statement. LOC is the
11552 location of the compound statement-- this is usually the location
11553 of the opening brace. */
11556 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11562 if (c_dialect_objc ())
11563 objc_clear_super_receiver ();
11564 block = pop_scope ();
11567 stmt = pop_stmt_list (stmt);
11568 stmt = c_build_bind_expr (loc, block, stmt);
11570 /* If this compound statement is nested immediately inside a statement
11571 expression, then force a BIND_EXPR to be created. Otherwise we'll
11572 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11573 STATEMENT_LISTs merge, and thus we can lose track of what statement
11574 was really last. */
11575 if (building_stmt_list_p ()
11576 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11577 && TREE_CODE (stmt) != BIND_EXPR)
11579 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11580 TREE_SIDE_EFFECTS (stmt) = 1;
11581 SET_EXPR_LOCATION (stmt, loc);
11587 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11588 when the current scope is exited. EH_ONLY is true when this is not
11589 meant to apply to normal control flow transfer. */
11592 push_cleanup (tree decl, tree cleanup, bool eh_only)
11594 enum tree_code code;
11598 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11599 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11601 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11602 list = push_stmt_list ();
11603 TREE_OPERAND (stmt, 0) = list;
11604 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11607 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11608 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11611 build_vec_cmp (tree_code code, tree type,
11612 tree arg0, tree arg1)
11614 tree zero_vec = build_zero_cst (type);
11615 tree minus_one_vec = build_minus_one_cst (type);
11616 tree cmp_type = truth_type_for (type);
11617 tree cmp = build2 (code, cmp_type, arg0, arg1);
11618 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11621 /* Possibly warn about an address of OP never being NULL in a comparison
11622 operation CODE involving null. */
11625 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11627 /* Prevent warnings issued for macro expansion. */
11629 || warning_suppressed_p (op, OPT_Waddress)
11630 || from_macro_expansion_at (loc))
11633 if (TREE_CODE (op) == NOP_EXPR)
11635 /* Allow casts to intptr_t to suppress the warning. */
11636 tree type = TREE_TYPE (op);
11637 if (TREE_CODE (type) == INTEGER_TYPE)
11639 op = TREE_OPERAND (op, 0);
11642 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
11644 /* Allow a cast to void* to suppress the warning. */
11645 tree type = TREE_TYPE (TREE_TYPE (op));
11646 if (VOID_TYPE_P (type))
11649 /* Adding any value to a null pointer, including zero, is undefined
11650 in C. This includes the expression &p[0] where p is the null
11651 pointer, although &p[0] will have been folded to p by this point
11652 and so not diagnosed. */
11653 if (code == EQ_EXPR)
11654 warning_at (loc, OPT_Waddress,
11655 "the comparison will always evaluate as %<false%> "
11656 "for the pointer operand in %qE must not be NULL",
11659 warning_at (loc, OPT_Waddress,
11660 "the comparison will always evaluate as %<true%> "
11661 "for the pointer operand in %qE must not be NULL",
11667 if (TREE_CODE (op) != ADDR_EXPR)
11670 op = TREE_OPERAND (op, 0);
11672 if (TREE_CODE (op) == IMAGPART_EXPR
11673 || TREE_CODE (op) == REALPART_EXPR)
11675 /* The address of either complex part may not be null. */
11676 if (code == EQ_EXPR)
11677 warning_at (loc, OPT_Waddress,
11678 "the comparison will always evaluate as %<false%> "
11679 "for the address of %qE will never be NULL",
11682 warning_at (loc, OPT_Waddress,
11683 "the comparison will always evaluate as %<true%> "
11684 "for the address of %qE will never be NULL",
11689 /* Set to true in the loop below if OP dereferences is operand.
11690 In such a case the ultimate target need not be a decl for
11691 the null [in]equality test to be constant. */
11692 bool deref = false;
11694 /* Get the outermost array or object, or member. */
11695 while (handled_component_p (op))
11697 if (TREE_CODE (op) == COMPONENT_REF)
11699 /* Get the member (its address is never null). */
11700 op = TREE_OPERAND (op, 1);
11704 /* Get the outer array/object to refer to in the warning. */
11705 op = TREE_OPERAND (op, 0);
11709 if ((!deref && !decl_with_nonnull_addr_p (op))
11710 || from_macro_expansion_at (loc))
11713 if (code == EQ_EXPR)
11714 warning_at (loc, OPT_Waddress,
11715 "the comparison will always evaluate as %<false%> "
11716 "for the address of %qE will never be NULL",
11719 warning_at (loc, OPT_Waddress,
11720 "the comparison will always evaluate as %<true%> "
11721 "for the address of %qE will never be NULL",
11725 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
11728 /* Build a binary-operation expression without default conversions.
11729 CODE is the kind of expression to build.
11730 LOCATION is the operator's location.
11731 This function differs from `build' in several ways:
11732 the data type of the result is computed and recorded in it,
11733 warnings are generated if arg data types are invalid,
11734 special handling for addition and subtraction of pointers is known,
11735 and some optimization is done (operations on narrow ints
11736 are done in the narrower type when that gives the same result).
11737 Constant folding is also done before the result is returned.
11739 Note that the operands will never have enumeral types, or function
11740 or array types, because either they will have the default conversions
11741 performed or they have both just been converted to some other type in which
11742 the arithmetic is to be done. */
11745 build_binary_op (location_t location, enum tree_code code,
11746 tree orig_op0, tree orig_op1, bool convert_p)
11748 tree type0, type1, orig_type0, orig_type1;
11750 enum tree_code code0, code1;
11752 tree ret = error_mark_node;
11753 const char *invalid_op_diag;
11754 bool op0_int_operands, op1_int_operands;
11755 bool int_const, int_const_or_overflow, int_operands;
11757 /* Expression code to give to the expression when it is built.
11758 Normally this is CODE, which is what the caller asked for,
11759 but in some special cases we change it. */
11760 enum tree_code resultcode = code;
11762 /* Data type in which the computation is to be performed.
11763 In the simplest cases this is the common type of the arguments. */
11764 tree result_type = NULL;
11766 /* When the computation is in excess precision, the type of the
11767 final EXCESS_PRECISION_EXPR. */
11768 tree semantic_result_type = NULL;
11770 /* Nonzero means operands have already been type-converted
11771 in whatever way is necessary.
11772 Zero means they need to be converted to RESULT_TYPE. */
11775 /* Nonzero means create the expression with this type, rather than
11777 tree build_type = NULL_TREE;
11779 /* Nonzero means after finally constructing the expression
11780 convert it to this type. */
11781 tree final_type = NULL_TREE;
11783 /* Nonzero if this is an operation like MIN or MAX which can
11784 safely be computed in short if both args are promoted shorts.
11785 Also implies COMMON.
11786 -1 indicates a bitwise operation; this makes a difference
11787 in the exact conditions for when it is safe to do the operation
11788 in a narrower mode. */
11791 /* Nonzero if this is a comparison operation;
11792 if both args are promoted shorts, compare the original shorts.
11793 Also implies COMMON. */
11794 int short_compare = 0;
11796 /* Nonzero if this is a right-shift operation, which can be computed on the
11797 original short and then promoted if the operand is a promoted short. */
11798 int short_shift = 0;
11800 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11803 /* True means types are compatible as far as ObjC is concerned. */
11806 /* True means this is an arithmetic operation that may need excess
11808 bool may_need_excess_precision;
11810 /* True means this is a boolean operation that converts both its
11811 operands to truth-values. */
11812 bool boolean_op = false;
11814 /* Remember whether we're doing / or %. */
11815 bool doing_div_or_mod = false;
11817 /* Remember whether we're doing << or >>. */
11818 bool doing_shift = false;
11820 /* Tree holding instrumentation expression. */
11821 tree instrument_expr = NULL;
11823 if (location == UNKNOWN_LOCATION)
11824 location = input_location;
11829 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11830 if (op0_int_operands)
11831 op0 = remove_c_maybe_const_expr (op0);
11832 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11833 if (op1_int_operands)
11834 op1 = remove_c_maybe_const_expr (op1);
11835 int_operands = (op0_int_operands && op1_int_operands);
11838 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11839 && TREE_CODE (orig_op1) == INTEGER_CST);
11840 int_const = (int_const_or_overflow
11841 && !TREE_OVERFLOW (orig_op0)
11842 && !TREE_OVERFLOW (orig_op1));
11845 int_const = int_const_or_overflow = false;
11847 /* Do not apply default conversion in mixed vector/scalar expression. */
11849 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11851 op0 = default_conversion (op0);
11852 op1 = default_conversion (op1);
11855 orig_type0 = type0 = TREE_TYPE (op0);
11857 orig_type1 = type1 = TREE_TYPE (op1);
11859 /* The expression codes of the data types of the arguments tell us
11860 whether the arguments are integers, floating, pointers, etc. */
11861 code0 = TREE_CODE (type0);
11862 code1 = TREE_CODE (type1);
11864 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11865 STRIP_TYPE_NOPS (op0);
11866 STRIP_TYPE_NOPS (op1);
11868 /* If an error was already reported for one of the arguments,
11869 avoid reporting another error. */
11871 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11872 return error_mark_node;
11874 if (code0 == POINTER_TYPE
11875 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11876 return error_mark_node;
11878 if (code1 == POINTER_TYPE
11879 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11880 return error_mark_node;
11882 if ((invalid_op_diag
11883 = targetm.invalid_binary_op (code, type0, type1)))
11885 error_at (location, invalid_op_diag);
11886 return error_mark_node;
11894 case TRUNC_DIV_EXPR:
11895 case CEIL_DIV_EXPR:
11896 case FLOOR_DIV_EXPR:
11897 case ROUND_DIV_EXPR:
11898 case EXACT_DIV_EXPR:
11899 may_need_excess_precision = true;
11908 /* Excess precision for implicit conversions of integers to
11909 floating point in C11 and later. */
11910 may_need_excess_precision = (flag_isoc11
11911 && (ANY_INTEGRAL_TYPE_P (type0)
11912 || ANY_INTEGRAL_TYPE_P (type1)));
11916 may_need_excess_precision = false;
11919 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11921 op0 = TREE_OPERAND (op0, 0);
11922 type0 = TREE_TYPE (op0);
11924 else if (may_need_excess_precision
11925 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11928 op0 = convert (eptype, op0);
11930 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11932 op1 = TREE_OPERAND (op1, 0);
11933 type1 = TREE_TYPE (op1);
11935 else if (may_need_excess_precision
11936 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11939 op1 = convert (eptype, op1);
11942 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11944 /* In case when one of the operands of the binary operation is
11945 a vector and another is a scalar -- convert scalar to vector. */
11946 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11947 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11949 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11952 switch (convert_flag)
11955 return error_mark_node;
11958 bool maybe_const = true;
11960 sc = c_fully_fold (op0, false, &maybe_const);
11961 sc = save_expr (sc);
11962 sc = convert (TREE_TYPE (type1), sc);
11963 op0 = build_vector_from_val (type1, sc);
11965 op0 = c_wrap_maybe_const (op0, true);
11966 orig_type0 = type0 = TREE_TYPE (op0);
11967 code0 = TREE_CODE (type0);
11971 case stv_secondarg:
11973 bool maybe_const = true;
11975 sc = c_fully_fold (op1, false, &maybe_const);
11976 sc = save_expr (sc);
11977 sc = convert (TREE_TYPE (type0), sc);
11978 op1 = build_vector_from_val (type0, sc);
11980 op1 = c_wrap_maybe_const (op1, true);
11981 orig_type1 = type1 = TREE_TYPE (op1);
11982 code1 = TREE_CODE (type1);
11994 /* Handle the pointer + int case. */
11995 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11997 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11998 goto return_build_binary_op;
12000 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
12002 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12003 goto return_build_binary_op;
12010 /* Subtraction of two similar pointers.
12011 We must subtract them as integers, then divide by object size. */
12012 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12013 && comp_target_types (location, type0, type1))
12015 ret = pointer_diff (location, op0, op1, &instrument_expr);
12016 goto return_build_binary_op;
12018 /* Handle pointer minus int. Just like pointer plus int. */
12019 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12021 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12022 goto return_build_binary_op;
12032 case TRUNC_DIV_EXPR:
12033 case CEIL_DIV_EXPR:
12034 case FLOOR_DIV_EXPR:
12035 case ROUND_DIV_EXPR:
12036 case EXACT_DIV_EXPR:
12037 doing_div_or_mod = true;
12038 warn_for_div_by_zero (location, op1);
12040 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12041 || code0 == FIXED_POINT_TYPE
12042 || code0 == COMPLEX_TYPE
12043 || gnu_vector_type_p (type0))
12044 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12045 || code1 == FIXED_POINT_TYPE
12046 || code1 == COMPLEX_TYPE
12047 || gnu_vector_type_p (type1)))
12049 enum tree_code tcode0 = code0, tcode1 = code1;
12051 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12052 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12053 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12054 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12056 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12057 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12058 resultcode = RDIV_EXPR;
12060 /* Although it would be tempting to shorten always here, that
12061 loses on some targets, since the modulo instruction is
12062 undefined if the quotient can't be represented in the
12063 computation mode. We shorten only if unsigned or if
12064 dividing by something we know != -1. */
12065 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12066 || (TREE_CODE (op1) == INTEGER_CST
12067 && !integer_all_onesp (op1)));
12075 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12077 /* Allow vector types which are not floating point types. */
12078 else if (gnu_vector_type_p (type0)
12079 && gnu_vector_type_p (type1)
12080 && !VECTOR_FLOAT_TYPE_P (type0)
12081 && !VECTOR_FLOAT_TYPE_P (type1))
12085 case TRUNC_MOD_EXPR:
12086 case FLOOR_MOD_EXPR:
12087 doing_div_or_mod = true;
12088 warn_for_div_by_zero (location, op1);
12090 if (gnu_vector_type_p (type0)
12091 && gnu_vector_type_p (type1)
12092 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12093 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12095 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12097 /* Although it would be tempting to shorten always here, that loses
12098 on some targets, since the modulo instruction is undefined if the
12099 quotient can't be represented in the computation mode. We shorten
12100 only if unsigned or if dividing by something we know != -1. */
12101 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12102 || (TREE_CODE (op1) == INTEGER_CST
12103 && !integer_all_onesp (op1)));
12108 case TRUTH_ANDIF_EXPR:
12109 case TRUTH_ORIF_EXPR:
12110 case TRUTH_AND_EXPR:
12111 case TRUTH_OR_EXPR:
12112 case TRUTH_XOR_EXPR:
12113 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12114 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12115 || code0 == FIXED_POINT_TYPE)
12116 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12117 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12118 || code1 == FIXED_POINT_TYPE))
12120 /* Result of these operations is always an int,
12121 but that does not mean the operands should be
12122 converted to ints! */
12123 result_type = integer_type_node;
12124 if (op0_int_operands)
12126 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12127 op0 = remove_c_maybe_const_expr (op0);
12130 op0 = c_objc_common_truthvalue_conversion (location, op0);
12131 if (op1_int_operands)
12133 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12134 op1 = remove_c_maybe_const_expr (op1);
12137 op1 = c_objc_common_truthvalue_conversion (location, op1);
12141 if (code == TRUTH_ANDIF_EXPR)
12143 int_const_or_overflow = (int_operands
12144 && TREE_CODE (orig_op0) == INTEGER_CST
12145 && (op0 == truthvalue_false_node
12146 || TREE_CODE (orig_op1) == INTEGER_CST));
12147 int_const = (int_const_or_overflow
12148 && !TREE_OVERFLOW (orig_op0)
12149 && (op0 == truthvalue_false_node
12150 || !TREE_OVERFLOW (orig_op1)));
12152 else if (code == TRUTH_ORIF_EXPR)
12154 int_const_or_overflow = (int_operands
12155 && TREE_CODE (orig_op0) == INTEGER_CST
12156 && (op0 == truthvalue_true_node
12157 || TREE_CODE (orig_op1) == INTEGER_CST));
12158 int_const = (int_const_or_overflow
12159 && !TREE_OVERFLOW (orig_op0)
12160 && (op0 == truthvalue_true_node
12161 || !TREE_OVERFLOW (orig_op1)));
12165 /* Shift operations: result has same type as first operand;
12166 always convert second operand to int.
12167 Also set SHORT_SHIFT if shifting rightward. */
12170 if (gnu_vector_type_p (type0)
12171 && gnu_vector_type_p (type1)
12172 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12173 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12174 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12175 TYPE_VECTOR_SUBPARTS (type1)))
12177 result_type = type0;
12180 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12181 || (gnu_vector_type_p (type0)
12182 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12183 && code1 == INTEGER_TYPE)
12185 doing_shift = true;
12186 if (TREE_CODE (op1) == INTEGER_CST)
12188 if (tree_int_cst_sgn (op1) < 0)
12191 if (c_inhibit_evaluation_warnings == 0)
12192 warning_at (location, OPT_Wshift_count_negative,
12193 "right shift count is negative");
12195 else if (code0 == VECTOR_TYPE)
12197 if (compare_tree_int (op1,
12198 TYPE_PRECISION (TREE_TYPE (type0)))
12202 if (c_inhibit_evaluation_warnings == 0)
12203 warning_at (location, OPT_Wshift_count_overflow,
12204 "right shift count >= width of vector element");
12209 if (!integer_zerop (op1))
12212 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12215 if (c_inhibit_evaluation_warnings == 0)
12216 warning_at (location, OPT_Wshift_count_overflow,
12217 "right shift count >= width of type");
12222 /* Use the type of the value to be shifted. */
12223 result_type = type0;
12224 /* Avoid converting op1 to result_type later. */
12230 if (gnu_vector_type_p (type0)
12231 && gnu_vector_type_p (type1)
12232 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12233 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12234 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12235 TYPE_VECTOR_SUBPARTS (type1)))
12237 result_type = type0;
12240 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12241 || (gnu_vector_type_p (type0)
12242 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12243 && code1 == INTEGER_TYPE)
12245 doing_shift = true;
12246 if (TREE_CODE (op0) == INTEGER_CST
12247 && tree_int_cst_sgn (op0) < 0
12248 && !TYPE_OVERFLOW_WRAPS (type0))
12250 /* Don't reject a left shift of a negative value in a context
12251 where a constant expression is needed in C90. */
12254 if (c_inhibit_evaluation_warnings == 0)
12255 warning_at (location, OPT_Wshift_negative_value,
12256 "left shift of negative value");
12258 if (TREE_CODE (op1) == INTEGER_CST)
12260 if (tree_int_cst_sgn (op1) < 0)
12263 if (c_inhibit_evaluation_warnings == 0)
12264 warning_at (location, OPT_Wshift_count_negative,
12265 "left shift count is negative");
12267 else if (code0 == VECTOR_TYPE)
12269 if (compare_tree_int (op1,
12270 TYPE_PRECISION (TREE_TYPE (type0)))
12274 if (c_inhibit_evaluation_warnings == 0)
12275 warning_at (location, OPT_Wshift_count_overflow,
12276 "left shift count >= width of vector element");
12279 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12282 if (c_inhibit_evaluation_warnings == 0)
12283 warning_at (location, OPT_Wshift_count_overflow,
12284 "left shift count >= width of type");
12286 else if (TREE_CODE (op0) == INTEGER_CST
12287 && maybe_warn_shift_overflow (location, op0, op1)
12292 /* Use the type of the value to be shifted. */
12293 result_type = type0;
12294 /* Avoid converting op1 to result_type later. */
12301 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12304 if (!vector_types_compatible_elements_p (type0, type1))
12306 error_at (location, "comparing vectors with different "
12308 return error_mark_node;
12311 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12312 TYPE_VECTOR_SUBPARTS (type1)))
12314 error_at (location, "comparing vectors with different "
12315 "number of elements");
12316 return error_mark_node;
12319 /* It's not precisely specified how the usual arithmetic
12320 conversions apply to the vector types. Here, we use
12321 the unsigned type if one of the operands is signed and
12322 the other one is unsigned. */
12323 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12325 if (!TYPE_UNSIGNED (type0))
12326 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12328 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12329 warning_at (location, OPT_Wsign_compare, "comparison between "
12330 "types %qT and %qT", type0, type1);
12333 /* Always construct signed integer vector type. */
12334 intt = c_common_type_for_size (GET_MODE_BITSIZE
12336 (TREE_TYPE (type0))), 0);
12339 error_at (location, "could not find an integer type "
12340 "of the same size as %qT",
12341 TREE_TYPE (type0));
12342 return error_mark_node;
12344 result_type = build_opaque_vector_type (intt,
12345 TYPE_VECTOR_SUBPARTS (type0));
12347 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12348 goto return_build_binary_op;
12350 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12351 warning_at (location,
12353 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12354 /* Result of comparison is always int,
12355 but don't convert the args to int! */
12356 build_type = integer_type_node;
12357 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12358 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12359 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12360 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12362 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12364 maybe_warn_for_null_address (location, op0, code);
12365 result_type = type0;
12367 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12369 maybe_warn_for_null_address (location, op1, code);
12370 result_type = type1;
12372 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12374 tree tt0 = TREE_TYPE (type0);
12375 tree tt1 = TREE_TYPE (type1);
12376 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12377 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12378 addr_space_t as_common = ADDR_SPACE_GENERIC;
12380 /* Anything compares with void *. void * compares with anything.
12381 Otherwise, the targets must be compatible
12382 and both must be object or both incomplete. */
12383 if (comp_target_types (location, type0, type1))
12384 result_type = common_pointer_type (type0, type1);
12385 else if (!addr_space_superset (as0, as1, &as_common))
12387 error_at (location, "comparison of pointers to "
12388 "disjoint address spaces");
12389 return error_mark_node;
12391 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12393 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12394 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12395 "comparison of %<void *%> with function pointer");
12397 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12399 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12400 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12401 "comparison of %<void *%> with function pointer");
12404 /* Avoid warning about the volatile ObjC EH puts on decls. */
12406 pedwarn (location, 0,
12407 "comparison of distinct pointer types lacks a cast");
12409 if (result_type == NULL_TREE)
12411 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12412 result_type = build_pointer_type
12413 (build_qualified_type (void_type_node, qual));
12416 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12418 result_type = type0;
12419 pedwarn (location, 0, "comparison between pointer and integer");
12421 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12423 result_type = type1;
12424 pedwarn (location, 0, "comparison between pointer and integer");
12426 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12427 || truth_value_p (TREE_CODE (orig_op0)))
12428 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12429 || truth_value_p (TREE_CODE (orig_op1))))
12430 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12437 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12440 if (!vector_types_compatible_elements_p (type0, type1))
12442 error_at (location, "comparing vectors with different "
12444 return error_mark_node;
12447 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12448 TYPE_VECTOR_SUBPARTS (type1)))
12450 error_at (location, "comparing vectors with different "
12451 "number of elements");
12452 return error_mark_node;
12455 /* It's not precisely specified how the usual arithmetic
12456 conversions apply to the vector types. Here, we use
12457 the unsigned type if one of the operands is signed and
12458 the other one is unsigned. */
12459 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12461 if (!TYPE_UNSIGNED (type0))
12462 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12464 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12465 warning_at (location, OPT_Wsign_compare, "comparison between "
12466 "types %qT and %qT", type0, type1);
12469 /* Always construct signed integer vector type. */
12470 intt = c_common_type_for_size (GET_MODE_BITSIZE
12472 (TREE_TYPE (type0))), 0);
12475 error_at (location, "could not find an integer type "
12476 "of the same size as %qT",
12477 TREE_TYPE (type0));
12478 return error_mark_node;
12480 result_type = build_opaque_vector_type (intt,
12481 TYPE_VECTOR_SUBPARTS (type0));
12483 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12484 goto return_build_binary_op;
12486 build_type = integer_type_node;
12487 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12488 || code0 == FIXED_POINT_TYPE)
12489 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12490 || code1 == FIXED_POINT_TYPE))
12492 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12494 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12495 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12496 addr_space_t as_common;
12498 if (comp_target_types (location, type0, type1))
12500 result_type = common_pointer_type (type0, type1);
12501 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12502 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12503 pedwarn_c99 (location, OPT_Wpedantic,
12504 "comparison of complete and incomplete pointers");
12505 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12506 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12507 "ordered comparisons of pointers to functions");
12508 else if (null_pointer_constant_p (orig_op0)
12509 || null_pointer_constant_p (orig_op1))
12510 warning_at (location, OPT_Wextra,
12511 "ordered comparison of pointer with null pointer");
12514 else if (!addr_space_superset (as0, as1, &as_common))
12516 error_at (location, "comparison of pointers to "
12517 "disjoint address spaces");
12518 return error_mark_node;
12522 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12523 result_type = build_pointer_type
12524 (build_qualified_type (void_type_node, qual));
12525 pedwarn (location, 0,
12526 "comparison of distinct pointer types lacks a cast");
12529 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12531 result_type = type0;
12533 pedwarn (location, OPT_Wpedantic,
12534 "ordered comparison of pointer with integer zero");
12535 else if (extra_warnings)
12536 warning_at (location, OPT_Wextra,
12537 "ordered comparison of pointer with integer zero");
12539 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12541 result_type = type1;
12543 pedwarn (location, OPT_Wpedantic,
12544 "ordered comparison of pointer with integer zero");
12545 else if (extra_warnings)
12546 warning_at (location, OPT_Wextra,
12547 "ordered comparison of pointer with integer zero");
12549 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12551 result_type = type0;
12552 pedwarn (location, 0, "comparison between pointer and integer");
12554 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12556 result_type = type1;
12557 pedwarn (location, 0, "comparison between pointer and integer");
12560 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12561 && current_function_decl != NULL_TREE
12562 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12564 op0 = save_expr (op0);
12565 op1 = save_expr (op1);
12567 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12568 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12571 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12572 || truth_value_p (TREE_CODE (orig_op0)))
12573 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12574 || truth_value_p (TREE_CODE (orig_op1))))
12575 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12580 /* Used for OpenMP atomics. */
12581 gcc_assert (flag_openmp);
12586 gcc_unreachable ();
12589 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12590 return error_mark_node;
12592 if (gnu_vector_type_p (type0)
12593 && gnu_vector_type_p (type1)
12594 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12595 || !vector_types_compatible_elements_p (type0, type1)))
12597 gcc_rich_location richloc (location);
12598 maybe_range_label_for_tree_type_mismatch
12599 label_for_op0 (orig_op0, orig_op1),
12600 label_for_op1 (orig_op1, orig_op0);
12601 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12602 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12603 binary_op_error (&richloc, code, type0, type1);
12604 return error_mark_node;
12607 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12608 || code0 == FIXED_POINT_TYPE
12609 || gnu_vector_type_p (type0))
12611 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12612 || code1 == FIXED_POINT_TYPE
12613 || gnu_vector_type_p (type1)))
12615 bool first_complex = (code0 == COMPLEX_TYPE);
12616 bool second_complex = (code1 == COMPLEX_TYPE);
12617 int none_complex = (!first_complex && !second_complex);
12619 if (shorten || common || short_compare)
12621 result_type = c_common_type (type0, type1);
12622 do_warn_double_promotion (result_type, type0, type1,
12623 "implicit conversion from %qT to %qT "
12624 "to match other operand of binary "
12627 if (result_type == error_mark_node)
12628 return error_mark_node;
12631 if (first_complex != second_complex
12632 && (code == PLUS_EXPR
12633 || code == MINUS_EXPR
12634 || code == MULT_EXPR
12635 || (code == TRUNC_DIV_EXPR && first_complex))
12636 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12637 && flag_signed_zeros)
12639 /* An operation on mixed real/complex operands must be
12640 handled specially, but the language-independent code can
12641 more easily optimize the plain complex arithmetic if
12642 -fno-signed-zeros. */
12643 tree real_type = TREE_TYPE (result_type);
12645 if (type0 != orig_type0 || type1 != orig_type1)
12647 gcc_assert (may_need_excess_precision && common);
12648 semantic_result_type = c_common_type (orig_type0, orig_type1);
12652 if (TREE_TYPE (op0) != result_type)
12653 op0 = convert_and_check (location, result_type, op0);
12654 if (TREE_TYPE (op1) != real_type)
12655 op1 = convert_and_check (location, real_type, op1);
12659 if (TREE_TYPE (op0) != real_type)
12660 op0 = convert_and_check (location, real_type, op0);
12661 if (TREE_TYPE (op1) != result_type)
12662 op1 = convert_and_check (location, result_type, op1);
12664 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12665 return error_mark_node;
12668 op0 = save_expr (op0);
12669 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12671 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12676 case TRUNC_DIV_EXPR:
12677 op1 = save_expr (op1);
12678 imag = build2 (resultcode, real_type, imag, op1);
12679 /* Fall through. */
12682 real = build2 (resultcode, real_type, real, op1);
12690 op1 = save_expr (op1);
12691 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12693 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12698 op0 = save_expr (op0);
12699 imag = build2 (resultcode, real_type, op0, imag);
12700 /* Fall through. */
12702 real = build2 (resultcode, real_type, op0, real);
12705 real = build2 (resultcode, real_type, op0, real);
12706 imag = build1 (NEGATE_EXPR, real_type, imag);
12712 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12713 goto return_build_binary_op;
12716 /* For certain operations (which identify themselves by shorten != 0)
12717 if both args were extended from the same smaller type,
12718 do the arithmetic in that type and then extend.
12720 shorten !=0 and !=1 indicates a bitwise operation.
12721 For them, this optimization is safe only if
12722 both args are zero-extended or both are sign-extended.
12723 Otherwise, we might change the result.
12724 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12725 but calculated in (unsigned short) it would be (unsigned short)-1. */
12727 if (shorten && none_complex)
12729 final_type = result_type;
12730 result_type = shorten_binary_op (result_type, op0, op1,
12734 /* Shifts can be shortened if shifting right. */
12739 tree arg0 = get_narrower (op0, &unsigned_arg);
12741 final_type = result_type;
12743 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12744 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12746 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12747 && tree_int_cst_sgn (op1) > 0
12748 /* We can shorten only if the shift count is less than the
12749 number of bits in the smaller type size. */
12750 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12751 /* We cannot drop an unsigned shift after sign-extension. */
12752 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12754 /* Do an unsigned shift if the operand was zero-extended. */
12756 = c_common_signed_or_unsigned_type (unsigned_arg,
12758 /* Convert value-to-be-shifted to that type. */
12759 if (TREE_TYPE (op0) != result_type)
12760 op0 = convert (result_type, op0);
12765 /* Comparison operations are shortened too but differently.
12766 They identify themselves by setting short_compare = 1. */
12770 /* Don't write &op0, etc., because that would prevent op0
12771 from being kept in a register.
12772 Instead, make copies of the our local variables and
12773 pass the copies by reference, then copy them back afterward. */
12774 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12775 enum tree_code xresultcode = resultcode;
12777 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12780 if (val != NULL_TREE)
12783 goto return_build_binary_op;
12786 op0 = xop0, op1 = xop1;
12788 resultcode = xresultcode;
12790 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12792 bool op0_maybe_const = true;
12793 bool op1_maybe_const = true;
12794 tree orig_op0_folded, orig_op1_folded;
12796 if (in_late_binary_op)
12798 orig_op0_folded = orig_op0;
12799 orig_op1_folded = orig_op1;
12803 /* Fold for the sake of possible warnings, as in
12804 build_conditional_expr. This requires the
12805 "original" values to be folded, not just op0 and
12807 c_inhibit_evaluation_warnings++;
12808 op0 = c_fully_fold (op0, require_constant_value,
12810 op1 = c_fully_fold (op1, require_constant_value,
12812 c_inhibit_evaluation_warnings--;
12813 orig_op0_folded = c_fully_fold (orig_op0,
12814 require_constant_value,
12816 orig_op1_folded = c_fully_fold (orig_op1,
12817 require_constant_value,
12821 if (warn_sign_compare)
12822 warn_for_sign_compare (location, orig_op0_folded,
12823 orig_op1_folded, op0, op1,
12824 result_type, resultcode);
12825 if (!in_late_binary_op && !int_operands)
12827 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12828 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12829 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12830 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12836 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12837 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12838 Then the expression will be built.
12839 It will be given type FINAL_TYPE if that is nonzero;
12840 otherwise, it will be given type RESULT_TYPE. */
12844 /* Favor showing any expression locations that are available. */
12845 op_location_t oploc (location, UNKNOWN_LOCATION);
12846 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12847 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12848 return error_mark_node;
12851 if (build_type == NULL_TREE)
12853 build_type = result_type;
12854 if ((type0 != orig_type0 || type1 != orig_type1)
12857 gcc_assert (may_need_excess_precision && common);
12858 semantic_result_type = c_common_type (orig_type0, orig_type1);
12864 op0 = ep_convert_and_check (location, result_type, op0,
12865 semantic_result_type);
12866 op1 = ep_convert_and_check (location, result_type, op1,
12867 semantic_result_type);
12869 /* This can happen if one operand has a vector type, and the other
12870 has a different type. */
12871 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12872 return error_mark_node;
12875 if (sanitize_flags_p ((SANITIZE_SHIFT
12877 | SANITIZE_FLOAT_DIVIDE
12878 | SANITIZE_SI_OVERFLOW))
12879 && current_function_decl != NULL_TREE
12880 && (doing_div_or_mod || doing_shift)
12881 && !require_constant_value)
12883 /* OP0 and/or OP1 might have side-effects. */
12884 op0 = save_expr (op0);
12885 op1 = save_expr (op1);
12886 op0 = c_fully_fold (op0, false, NULL);
12887 op1 = c_fully_fold (op1, false, NULL);
12888 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12889 | SANITIZE_FLOAT_DIVIDE
12890 | SANITIZE_SI_OVERFLOW))))
12891 instrument_expr = ubsan_instrument_division (location, op0, op1);
12892 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12893 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12896 /* Treat expressions in initializers specially as they can't trap. */
12897 if (int_const_or_overflow)
12898 ret = (require_constant_value
12899 ? fold_build2_initializer_loc (location, resultcode, build_type,
12901 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12903 ret = build2 (resultcode, build_type, op0, op1);
12904 if (final_type != NULL_TREE)
12905 ret = convert (final_type, ret);
12907 return_build_binary_op:
12908 gcc_assert (ret != error_mark_node);
12909 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12910 ret = (int_operands
12911 ? note_integer_operands (ret)
12912 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12913 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12914 && !in_late_binary_op)
12915 ret = note_integer_operands (ret);
12916 protected_set_expr_location (ret, location);
12918 if (instrument_expr != NULL)
12919 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12920 instrument_expr, ret);
12922 if (semantic_result_type)
12923 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12924 semantic_result_type, ret);
12930 /* Convert EXPR to be a truth-value, validating its type for this
12931 purpose. LOCATION is the source location for the expression. */
12934 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12936 bool int_const, int_operands;
12938 switch (TREE_CODE (TREE_TYPE (expr)))
12941 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12942 return error_mark_node;
12945 error_at (location, "used struct type value where scalar is required");
12946 return error_mark_node;
12949 error_at (location, "used union type value where scalar is required");
12950 return error_mark_node;
12953 error_at (location, "void value not ignored as it ought to be");
12954 return error_mark_node;
12957 if (reject_gcc_builtin (expr))
12958 return error_mark_node;
12961 case FUNCTION_TYPE:
12962 gcc_unreachable ();
12965 error_at (location, "used vector type where scalar is required");
12966 return error_mark_node;
12972 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12973 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12974 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12976 expr = remove_c_maybe_const_expr (expr);
12977 expr = build2 (NE_EXPR, integer_type_node, expr,
12978 convert (TREE_TYPE (expr), integer_zero_node));
12979 expr = note_integer_operands (expr);
12982 /* ??? Should we also give an error for vectors rather than leaving
12983 those to give errors later? */
12984 expr = c_common_truthvalue_conversion (location, expr);
12986 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12988 if (TREE_OVERFLOW (expr))
12991 return note_integer_operands (expr);
12993 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12994 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12999 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13003 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13005 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13007 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13008 /* Executing a compound literal inside a function reinitializes
13010 if (!TREE_STATIC (decl))
13018 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13019 statement. LOC is the location of the construct. */
13022 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13025 body = c_end_compound_stmt (loc, body, true);
13027 tree stmt = make_node (code);
13028 TREE_TYPE (stmt) = void_type_node;
13029 OMP_BODY (stmt) = body;
13030 OMP_CLAUSES (stmt) = clauses;
13031 SET_EXPR_LOCATION (stmt, loc);
13033 return add_stmt (stmt);
13036 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13037 statement. LOC is the location of the OACC_DATA. */
13040 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13044 block = c_end_compound_stmt (loc, block, true);
13046 stmt = make_node (OACC_DATA);
13047 TREE_TYPE (stmt) = void_type_node;
13048 OACC_DATA_CLAUSES (stmt) = clauses;
13049 OACC_DATA_BODY (stmt) = block;
13050 SET_EXPR_LOCATION (stmt, loc);
13052 return add_stmt (stmt);
13055 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13056 statement. LOC is the location of the OACC_HOST_DATA. */
13059 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13063 block = c_end_compound_stmt (loc, block, true);
13065 stmt = make_node (OACC_HOST_DATA);
13066 TREE_TYPE (stmt) = void_type_node;
13067 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13068 OACC_HOST_DATA_BODY (stmt) = block;
13069 SET_EXPR_LOCATION (stmt, loc);
13071 return add_stmt (stmt);
13074 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13077 c_begin_omp_parallel (void)
13081 keep_next_level ();
13082 block = c_begin_compound_stmt (true);
13087 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13088 statement. LOC is the location of the OMP_PARALLEL. */
13091 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13095 block = c_end_compound_stmt (loc, block, true);
13097 stmt = make_node (OMP_PARALLEL);
13098 TREE_TYPE (stmt) = void_type_node;
13099 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13100 OMP_PARALLEL_BODY (stmt) = block;
13101 SET_EXPR_LOCATION (stmt, loc);
13103 return add_stmt (stmt);
13106 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13109 c_begin_omp_task (void)
13113 keep_next_level ();
13114 block = c_begin_compound_stmt (true);
13119 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13120 statement. LOC is the location of the #pragma. */
13123 c_finish_omp_task (location_t loc, tree clauses, tree block)
13127 block = c_end_compound_stmt (loc, block, true);
13129 stmt = make_node (OMP_TASK);
13130 TREE_TYPE (stmt) = void_type_node;
13131 OMP_TASK_CLAUSES (stmt) = clauses;
13132 OMP_TASK_BODY (stmt) = block;
13133 SET_EXPR_LOCATION (stmt, loc);
13135 return add_stmt (stmt);
13138 /* Generate GOMP_cancel call for #pragma omp cancel. */
13141 c_finish_omp_cancel (location_t loc, tree clauses)
13143 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13145 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13147 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13149 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13151 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13155 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13156 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13160 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13161 if (ifc != NULL_TREE)
13163 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13164 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13165 error_at (OMP_CLAUSE_LOCATION (ifc),
13166 "expected %<cancel%> %<if%> clause modifier");
13169 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13170 if (ifc2 != NULL_TREE)
13172 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13173 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13174 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13175 error_at (OMP_CLAUSE_LOCATION (ifc2),
13176 "expected %<cancel%> %<if%> clause modifier");
13180 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13181 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13182 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13183 build_zero_cst (type));
13186 ifc = boolean_true_node;
13187 tree stmt = build_call_expr_loc (loc, fn, 2,
13188 build_int_cst (integer_type_node, mask),
13193 /* Generate GOMP_cancellation_point call for
13194 #pragma omp cancellation point. */
13197 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13199 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13201 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13203 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13205 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13207 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13211 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13212 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13216 tree stmt = build_call_expr_loc (loc, fn, 1,
13217 build_int_cst (integer_type_node, mask));
13221 /* Helper function for handle_omp_array_sections. Called recursively
13222 to handle multiple array-section-subscripts. C is the clause,
13223 T current expression (initially OMP_CLAUSE_DECL), which is either
13224 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13225 expression if specified, TREE_VALUE length expression if specified,
13226 TREE_CHAIN is what it has been specified after, or some decl.
13227 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13228 set to true if any of the array-section-subscript could have length
13229 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13230 first array-section-subscript which is known not to have length
13232 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13233 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13234 all are or may have length of 1, array-section-subscript [:2] is the
13235 first one known not to have length 1. For array-section-subscript
13236 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13237 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13238 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13239 case though, as some lengths could be zero. */
13242 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13243 bool &maybe_zero_len, unsigned int &first_non_one,
13244 enum c_omp_region_type ort)
13246 tree ret, low_bound, length, type;
13247 if (TREE_CODE (t) != TREE_LIST)
13249 if (error_operand_p (t))
13250 return error_mark_node;
13252 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13253 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13254 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13256 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13257 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13258 return error_mark_node;
13260 while (TREE_CODE (t) == INDIRECT_REF)
13262 t = TREE_OPERAND (t, 0);
13264 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13265 t = TREE_OPERAND (t, 0);
13267 while (TREE_CODE (t) == COMPOUND_EXPR)
13269 t = TREE_OPERAND (t, 1);
13272 if (TREE_CODE (t) == COMPONENT_REF
13273 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13274 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13275 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13277 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13279 error_at (OMP_CLAUSE_LOCATION (c),
13280 "bit-field %qE in %qs clause",
13281 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13282 return error_mark_node;
13284 while (TREE_CODE (t) == COMPONENT_REF)
13286 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13288 error_at (OMP_CLAUSE_LOCATION (c),
13289 "%qE is a member of a union", t);
13290 return error_mark_node;
13292 t = TREE_OPERAND (t, 0);
13293 while (TREE_CODE (t) == MEM_REF
13294 || TREE_CODE (t) == INDIRECT_REF
13295 || TREE_CODE (t) == ARRAY_REF)
13297 t = TREE_OPERAND (t, 0);
13299 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13300 t = TREE_OPERAND (t, 0);
13302 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13304 if (maybe_ne (mem_ref_offset (t), 0))
13305 error_at (OMP_CLAUSE_LOCATION (c),
13306 "cannot dereference %qE in %qs clause", t,
13307 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13309 t = TREE_OPERAND (t, 0);
13313 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13316 error_at (OMP_CLAUSE_LOCATION (c),
13317 "%qD is not a variable in %qs clause", t,
13318 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13320 error_at (OMP_CLAUSE_LOCATION (c),
13321 "%qE is not a variable in %qs clause", t,
13322 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13323 return error_mark_node;
13325 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13326 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13327 && TYPE_ATOMIC (TREE_TYPE (t)))
13329 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13330 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13331 return error_mark_node;
13333 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13334 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13336 && DECL_THREAD_LOCAL_P (t))
13338 error_at (OMP_CLAUSE_LOCATION (c),
13339 "%qD is threadprivate variable in %qs clause", t,
13340 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13341 return error_mark_node;
13343 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13344 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13345 && TYPE_ATOMIC (TREE_TYPE (t))
13346 && POINTER_TYPE_P (TREE_TYPE (t)))
13348 /* If the array section is pointer based and the pointer
13349 itself is _Atomic qualified, we need to atomically load
13352 memset (&expr, 0, sizeof (expr));
13354 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13355 expr, false, false);
13361 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13362 maybe_zero_len, first_non_one, ort);
13363 if (ret == error_mark_node || ret == NULL_TREE)
13366 type = TREE_TYPE (ret);
13367 low_bound = TREE_PURPOSE (t);
13368 length = TREE_VALUE (t);
13370 if (low_bound == error_mark_node || length == error_mark_node)
13371 return error_mark_node;
13373 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13375 error_at (OMP_CLAUSE_LOCATION (c),
13376 "low bound %qE of array section does not have integral type",
13378 return error_mark_node;
13380 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13382 error_at (OMP_CLAUSE_LOCATION (c),
13383 "length %qE of array section does not have integral type",
13385 return error_mark_node;
13388 && TREE_CODE (low_bound) == INTEGER_CST
13389 && TYPE_PRECISION (TREE_TYPE (low_bound))
13390 > TYPE_PRECISION (sizetype))
13391 low_bound = fold_convert (sizetype, low_bound);
13393 && TREE_CODE (length) == INTEGER_CST
13394 && TYPE_PRECISION (TREE_TYPE (length))
13395 > TYPE_PRECISION (sizetype))
13396 length = fold_convert (sizetype, length);
13397 if (low_bound == NULL_TREE)
13398 low_bound = integer_zero_node;
13399 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13400 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13401 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13403 if (length != integer_one_node)
13405 error_at (OMP_CLAUSE_LOCATION (c),
13406 "expected single pointer in %qs clause",
13407 user_omp_clause_code_name (c, ort == C_ORT_ACC));
13408 return error_mark_node;
13411 if (length != NULL_TREE)
13413 if (!integer_nonzerop (length))
13415 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13416 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13417 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13418 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13419 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13421 if (integer_zerop (length))
13423 error_at (OMP_CLAUSE_LOCATION (c),
13424 "zero length array section in %qs clause",
13425 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13426 return error_mark_node;
13430 maybe_zero_len = true;
13432 if (first_non_one == types.length ()
13433 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13436 if (TREE_CODE (type) == ARRAY_TYPE)
13438 if (length == NULL_TREE
13439 && (TYPE_DOMAIN (type) == NULL_TREE
13440 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13442 error_at (OMP_CLAUSE_LOCATION (c),
13443 "for unknown bound array type length expression must "
13445 return error_mark_node;
13447 if (TREE_CODE (low_bound) == INTEGER_CST
13448 && tree_int_cst_sgn (low_bound) == -1)
13450 error_at (OMP_CLAUSE_LOCATION (c),
13451 "negative low bound in array section in %qs clause",
13452 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13453 return error_mark_node;
13455 if (length != NULL_TREE
13456 && TREE_CODE (length) == INTEGER_CST
13457 && tree_int_cst_sgn (length) == -1)
13459 error_at (OMP_CLAUSE_LOCATION (c),
13460 "negative length in array section in %qs clause",
13461 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13462 return error_mark_node;
13464 if (TYPE_DOMAIN (type)
13465 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13466 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13470 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13471 size = size_binop (PLUS_EXPR, size, size_one_node);
13472 if (TREE_CODE (low_bound) == INTEGER_CST)
13474 if (tree_int_cst_lt (size, low_bound))
13476 error_at (OMP_CLAUSE_LOCATION (c),
13477 "low bound %qE above array section size "
13478 "in %qs clause", low_bound,
13479 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13480 return error_mark_node;
13482 if (tree_int_cst_equal (size, low_bound))
13484 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13485 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13486 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13487 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13488 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13490 error_at (OMP_CLAUSE_LOCATION (c),
13491 "zero length array section in %qs clause",
13492 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13493 return error_mark_node;
13495 maybe_zero_len = true;
13497 else if (length == NULL_TREE
13498 && first_non_one == types.length ()
13499 && tree_int_cst_equal
13500 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13504 else if (length == NULL_TREE)
13506 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13507 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13508 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13509 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13510 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13511 maybe_zero_len = true;
13512 if (first_non_one == types.length ())
13515 if (length && TREE_CODE (length) == INTEGER_CST)
13517 if (tree_int_cst_lt (size, length))
13519 error_at (OMP_CLAUSE_LOCATION (c),
13520 "length %qE above array section size "
13521 "in %qs clause", length,
13522 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13523 return error_mark_node;
13525 if (TREE_CODE (low_bound) == INTEGER_CST)
13528 = size_binop (PLUS_EXPR,
13529 fold_convert (sizetype, low_bound),
13530 fold_convert (sizetype, length));
13531 if (TREE_CODE (lbpluslen) == INTEGER_CST
13532 && tree_int_cst_lt (size, lbpluslen))
13534 error_at (OMP_CLAUSE_LOCATION (c),
13535 "high bound %qE above array section size "
13536 "in %qs clause", lbpluslen,
13537 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13538 return error_mark_node;
13543 else if (length == NULL_TREE)
13545 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13546 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13547 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13548 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13549 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13550 maybe_zero_len = true;
13551 if (first_non_one == types.length ())
13555 /* For [lb:] we will need to evaluate lb more than once. */
13556 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13558 tree lb = save_expr (low_bound);
13559 if (lb != low_bound)
13561 TREE_PURPOSE (t) = lb;
13566 else if (TREE_CODE (type) == POINTER_TYPE)
13568 if (length == NULL_TREE)
13570 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13571 error_at (OMP_CLAUSE_LOCATION (c),
13572 "for array function parameter length expression "
13573 "must be specified");
13575 error_at (OMP_CLAUSE_LOCATION (c),
13576 "for pointer type length expression must be specified");
13577 return error_mark_node;
13579 if (length != NULL_TREE
13580 && TREE_CODE (length) == INTEGER_CST
13581 && tree_int_cst_sgn (length) == -1)
13583 error_at (OMP_CLAUSE_LOCATION (c),
13584 "negative length in array section in %qs clause",
13585 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13586 return error_mark_node;
13588 /* If there is a pointer type anywhere but in the very first
13589 array-section-subscript, the array section could be non-contiguous. */
13590 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13591 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13592 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13594 /* If any prior dimension has a non-one length, then deem this
13595 array section as non-contiguous. */
13596 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13597 d = TREE_CHAIN (d))
13599 tree d_length = TREE_VALUE (d);
13600 if (d_length == NULL_TREE || !integer_onep (d_length))
13602 error_at (OMP_CLAUSE_LOCATION (c),
13603 "array section is not contiguous in %qs clause",
13604 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13605 return error_mark_node;
13612 error_at (OMP_CLAUSE_LOCATION (c),
13613 "%qE does not have pointer or array type", ret);
13614 return error_mark_node;
13616 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13617 types.safe_push (TREE_TYPE (ret));
13618 /* We will need to evaluate lb more than once. */
13619 tree lb = save_expr (low_bound);
13620 if (lb != low_bound)
13622 TREE_PURPOSE (t) = lb;
13625 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13629 /* Handle array sections for clause C. */
13632 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13634 bool maybe_zero_len = false;
13635 unsigned int first_non_one = 0;
13636 auto_vec<tree, 10> types;
13637 tree *tp = &OMP_CLAUSE_DECL (c);
13638 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13639 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13640 && TREE_CODE (*tp) == TREE_LIST
13641 && TREE_PURPOSE (*tp)
13642 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13643 tp = &TREE_VALUE (*tp);
13644 tree first = handle_omp_array_sections_1 (c, *tp, types,
13645 maybe_zero_len, first_non_one,
13647 if (first == error_mark_node)
13649 if (first == NULL_TREE)
13651 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13652 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13655 tree tem = NULL_TREE;
13656 /* Need to evaluate side effects in the length expressions
13658 while (TREE_CODE (t) == TREE_LIST)
13660 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13662 if (tem == NULL_TREE)
13663 tem = TREE_VALUE (t);
13665 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13666 TREE_VALUE (t), tem);
13668 t = TREE_CHAIN (t);
13671 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13672 first = c_fully_fold (first, false, NULL, true);
13677 unsigned int num = types.length (), i;
13678 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13679 tree condition = NULL_TREE;
13681 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13682 maybe_zero_len = true;
13684 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13685 t = TREE_CHAIN (t))
13687 tree low_bound = TREE_PURPOSE (t);
13688 tree length = TREE_VALUE (t);
13692 && TREE_CODE (low_bound) == INTEGER_CST
13693 && TYPE_PRECISION (TREE_TYPE (low_bound))
13694 > TYPE_PRECISION (sizetype))
13695 low_bound = fold_convert (sizetype, low_bound);
13697 && TREE_CODE (length) == INTEGER_CST
13698 && TYPE_PRECISION (TREE_TYPE (length))
13699 > TYPE_PRECISION (sizetype))
13700 length = fold_convert (sizetype, length);
13701 if (low_bound == NULL_TREE)
13702 low_bound = integer_zero_node;
13703 if (!maybe_zero_len && i > first_non_one)
13705 if (integer_nonzerop (low_bound))
13706 goto do_warn_noncontiguous;
13707 if (length != NULL_TREE
13708 && TREE_CODE (length) == INTEGER_CST
13709 && TYPE_DOMAIN (types[i])
13710 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13711 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13715 size = size_binop (PLUS_EXPR,
13716 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13718 if (!tree_int_cst_equal (length, size))
13720 do_warn_noncontiguous:
13721 error_at (OMP_CLAUSE_LOCATION (c),
13722 "array section is not contiguous in %qs "
13724 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13728 if (length != NULL_TREE
13729 && TREE_SIDE_EFFECTS (length))
13731 if (side_effects == NULL_TREE)
13732 side_effects = length;
13734 side_effects = build2 (COMPOUND_EXPR,
13735 TREE_TYPE (side_effects),
13736 length, side_effects);
13743 if (i > first_non_one
13744 && ((length && integer_nonzerop (length))
13745 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13746 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13747 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13750 l = fold_convert (sizetype, length);
13753 l = size_binop (PLUS_EXPR,
13754 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13756 l = size_binop (MINUS_EXPR, l,
13757 fold_convert (sizetype, low_bound));
13759 if (i > first_non_one)
13761 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13763 if (condition == NULL_TREE)
13766 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13769 else if (size == NULL_TREE)
13771 size = size_in_bytes (TREE_TYPE (types[i]));
13772 tree eltype = TREE_TYPE (types[num - 1]);
13773 while (TREE_CODE (eltype) == ARRAY_TYPE)
13774 eltype = TREE_TYPE (eltype);
13775 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13776 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13777 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13779 if (integer_zerop (size)
13780 || integer_zerop (size_in_bytes (eltype)))
13782 error_at (OMP_CLAUSE_LOCATION (c),
13783 "zero length array section in %qs clause",
13784 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13785 return error_mark_node;
13787 size = size_binop (EXACT_DIV_EXPR, size,
13788 size_in_bytes (eltype));
13790 size = size_binop (MULT_EXPR, size, l);
13792 size = fold_build3 (COND_EXPR, sizetype, condition,
13793 size, size_zero_node);
13796 size = size_binop (MULT_EXPR, size, l);
13800 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13801 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13802 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13803 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13805 size = size_binop (MINUS_EXPR, size, size_one_node);
13806 size = c_fully_fold (size, false, NULL);
13807 size = save_expr (size);
13808 tree index_type = build_index_type (size);
13809 tree eltype = TREE_TYPE (first);
13810 while (TREE_CODE (eltype) == ARRAY_TYPE)
13811 eltype = TREE_TYPE (eltype);
13812 tree type = build_array_type (eltype, index_type);
13813 tree ptype = build_pointer_type (eltype);
13814 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13815 t = build_fold_addr_expr (t);
13816 tree t2 = build_fold_addr_expr (first);
13817 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13818 ptrdiff_type_node, t2);
13819 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13820 ptrdiff_type_node, t2,
13821 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13822 ptrdiff_type_node, t));
13823 t2 = c_fully_fold (t2, false, NULL);
13824 if (tree_fits_shwi_p (t2))
13825 t = build2 (MEM_REF, type, t,
13826 build_int_cst (ptype, tree_to_shwi (t2)));
13829 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13830 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13831 TREE_TYPE (t), t, t2);
13832 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13834 OMP_CLAUSE_DECL (c) = t;
13837 first = c_fully_fold (first, false, NULL);
13838 OMP_CLAUSE_DECL (c) = first;
13839 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13842 size = c_fully_fold (size, false, NULL);
13843 OMP_CLAUSE_SIZE (c) = size;
13844 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13845 || (TREE_CODE (t) == COMPONENT_REF
13846 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13848 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13849 switch (OMP_CLAUSE_MAP_KIND (c))
13851 case GOMP_MAP_ALLOC:
13852 case GOMP_MAP_IF_PRESENT:
13854 case GOMP_MAP_FROM:
13855 case GOMP_MAP_TOFROM:
13856 case GOMP_MAP_ALWAYS_TO:
13857 case GOMP_MAP_ALWAYS_FROM:
13858 case GOMP_MAP_ALWAYS_TOFROM:
13859 case GOMP_MAP_RELEASE:
13860 case GOMP_MAP_DELETE:
13861 case GOMP_MAP_FORCE_TO:
13862 case GOMP_MAP_FORCE_FROM:
13863 case GOMP_MAP_FORCE_TOFROM:
13864 case GOMP_MAP_FORCE_PRESENT:
13865 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13870 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13871 if (TREE_CODE (t) == COMPONENT_REF)
13872 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13874 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13875 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13876 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13877 && !c_mark_addressable (t))
13879 OMP_CLAUSE_DECL (c2) = t;
13880 t = build_fold_addr_expr (first);
13881 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13882 tree ptr = OMP_CLAUSE_DECL (c2);
13883 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13884 ptr = build_fold_addr_expr (ptr);
13885 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13886 ptrdiff_type_node, t,
13887 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13888 ptrdiff_type_node, ptr));
13889 t = c_fully_fold (t, false, NULL);
13890 OMP_CLAUSE_SIZE (c2) = t;
13891 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13892 OMP_CLAUSE_CHAIN (c) = c2;
13897 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13898 an inline call. But, remap
13899 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13900 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13903 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13904 tree decl, tree placeholder)
13907 hash_map<tree, tree> decl_map;
13909 decl_map.put (omp_decl1, placeholder);
13910 decl_map.put (omp_decl2, decl);
13911 memset (&id, 0, sizeof (id));
13912 id.src_fn = DECL_CONTEXT (omp_decl1);
13913 id.dst_fn = current_function_decl;
13914 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13915 id.decl_map = &decl_map;
13917 id.copy_decl = copy_decl_no_change;
13918 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13919 id.transform_new_cfg = true;
13920 id.transform_return_to_modify = false;
13922 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13926 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13927 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13930 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13932 if (*tp == (tree) data)
13937 /* Similarly, but also walk aggregate fields. */
13939 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13942 c_find_omp_var_r (tree *tp, int *, void *data)
13944 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13946 if (RECORD_OR_UNION_TYPE_P (*tp))
13949 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13951 for (field = TYPE_FIELDS (*tp); field;
13952 field = DECL_CHAIN (field))
13953 if (TREE_CODE (field) == FIELD_DECL)
13955 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13956 c_find_omp_var_r, data, pset);
13959 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13962 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13966 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13971 else if (INTEGRAL_TYPE_P (*tp))
13972 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13973 ((struct c_find_omp_var_s *) data)->pset);
13977 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13978 and clauses containing them should be removed. */
13981 c_omp_finish_iterators (tree iter)
13984 for (tree it = iter; it; it = TREE_CHAIN (it))
13986 tree var = TREE_VEC_ELT (it, 0);
13987 tree begin = TREE_VEC_ELT (it, 1);
13988 tree end = TREE_VEC_ELT (it, 2);
13989 tree step = TREE_VEC_ELT (it, 3);
13991 tree type = TREE_TYPE (var);
13992 location_t loc = DECL_SOURCE_LOCATION (var);
13993 if (type == error_mark_node)
13998 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14000 error_at (loc, "iterator %qD has neither integral nor pointer type",
14005 else if (TYPE_ATOMIC (type))
14007 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14011 else if (TYPE_READONLY (type))
14013 error_at (loc, "iterator %qD has const qualified type", var);
14017 else if (step == error_mark_node
14018 || TREE_TYPE (step) == error_mark_node)
14023 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14025 error_at (EXPR_LOC_OR_LOC (step, loc),
14026 "iterator step with non-integral type");
14030 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14031 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14032 orig_step = save_expr (c_fully_fold (step, false, NULL));
14033 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14034 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14035 if (POINTER_TYPE_P (type))
14037 begin = save_expr (begin);
14038 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14039 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14040 fold_convert (sizetype, step),
14041 fold_convert (sizetype, begin));
14042 step = fold_convert (ssizetype, step);
14044 if (integer_zerop (step))
14046 error_at (loc, "iterator %qD has zero step", var);
14051 if (begin == error_mark_node
14052 || end == error_mark_node
14053 || step == error_mark_node
14054 || orig_step == error_mark_node)
14059 hash_set<tree> pset;
14061 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14063 tree var2 = TREE_VEC_ELT (it2, 0);
14064 tree begin2 = TREE_VEC_ELT (it2, 1);
14065 tree end2 = TREE_VEC_ELT (it2, 2);
14066 tree step2 = TREE_VEC_ELT (it2, 3);
14067 tree type2 = TREE_TYPE (var2);
14068 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14069 struct c_find_omp_var_s data = { var, &pset };
14070 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14073 "type of iterator %qD refers to outer iterator %qD",
14077 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14079 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14080 "begin expression refers to outer iterator %qD", var);
14083 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14085 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14086 "end expression refers to outer iterator %qD", var);
14089 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14091 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14092 "step expression refers to outer iterator %qD", var);
14101 TREE_VEC_ELT (it, 1) = begin;
14102 TREE_VEC_ELT (it, 2) = end;
14103 TREE_VEC_ELT (it, 3) = step;
14104 TREE_VEC_ELT (it, 4) = orig_step;
14109 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14110 Return true if an error has been detected. */
14113 c_oacc_check_attachments (tree c)
14115 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14118 /* OpenACC attach / detach clauses must be pointers. */
14119 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14120 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14122 tree t = OMP_CLAUSE_DECL (c);
14124 while (TREE_CODE (t) == TREE_LIST)
14125 t = TREE_CHAIN (t);
14127 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14129 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14130 user_omp_clause_code_name (c, true));
14138 /* For all elements of CLAUSES, validate them against their constraints.
14139 Remove any elements from the list that are invalid. */
14142 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14144 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14145 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14146 bitmap_head oacc_reduction_head, is_on_device_head;
14147 tree c, t, type, *pc;
14148 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14149 bool branch_seen = false;
14150 bool copyprivate_seen = false;
14151 bool mergeable_seen = false;
14152 tree *detach_seen = NULL;
14153 bool linear_variable_step_check = false;
14154 tree *nowait_clause = NULL;
14155 tree ordered_clause = NULL_TREE;
14156 tree schedule_clause = NULL_TREE;
14157 bool oacc_async = false;
14158 bool indir_component_ref_p = false;
14159 tree last_iterators = NULL_TREE;
14160 bool last_iterators_remove = false;
14161 tree *nogroup_seen = NULL;
14162 tree *order_clause = NULL;
14163 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14164 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14165 int reduction_seen = 0;
14166 bool allocate_seen = false;
14167 bool implicit_moved = false;
14168 bool target_in_reduction_seen = false;
14170 bitmap_obstack_initialize (NULL);
14171 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14172 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14173 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14174 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14175 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14176 bitmap_initialize (&map_head, &bitmap_default_obstack);
14177 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14178 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14179 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14180 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14181 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14182 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14184 if (ort & C_ORT_ACC)
14185 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14186 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14192 for (pc = &clauses, c = clauses; c ; c = *pc)
14194 bool remove = false;
14195 bool need_complete = false;
14196 bool need_implicitly_determined = false;
14198 switch (OMP_CLAUSE_CODE (c))
14200 case OMP_CLAUSE_SHARED:
14201 need_implicitly_determined = true;
14202 goto check_dup_generic;
14204 case OMP_CLAUSE_PRIVATE:
14205 need_complete = true;
14206 need_implicitly_determined = true;
14207 goto check_dup_generic;
14209 case OMP_CLAUSE_REDUCTION:
14210 if (reduction_seen == 0)
14211 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14212 else if (reduction_seen != -2
14213 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14216 error_at (OMP_CLAUSE_LOCATION (c),
14217 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14218 "on the same construct");
14219 reduction_seen = -2;
14222 case OMP_CLAUSE_IN_REDUCTION:
14223 case OMP_CLAUSE_TASK_REDUCTION:
14224 need_implicitly_determined = true;
14225 t = OMP_CLAUSE_DECL (c);
14226 if (TREE_CODE (t) == TREE_LIST)
14228 if (handle_omp_array_sections (c, ort))
14234 t = OMP_CLAUSE_DECL (c);
14235 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14236 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14238 error_at (OMP_CLAUSE_LOCATION (c),
14239 "%<inscan%> %<reduction%> clause with array "
14245 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14246 if (t == error_mark_node)
14252 c_mark_addressable (t);
14253 type = TREE_TYPE (t);
14254 if (TREE_CODE (t) == MEM_REF)
14255 type = TREE_TYPE (type);
14256 if (TREE_CODE (type) == ARRAY_TYPE)
14258 tree oatype = type;
14259 gcc_assert (TREE_CODE (t) != MEM_REF);
14260 while (TREE_CODE (type) == ARRAY_TYPE)
14261 type = TREE_TYPE (type);
14262 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14264 error_at (OMP_CLAUSE_LOCATION (c),
14265 "%qD in %<reduction%> clause is a zero size array",
14270 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14271 TYPE_SIZE_UNIT (type));
14272 if (integer_zerop (size))
14274 error_at (OMP_CLAUSE_LOCATION (c),
14275 "%qD in %<reduction%> clause is a zero size array",
14280 size = size_binop (MINUS_EXPR, size, size_one_node);
14281 size = save_expr (size);
14282 tree index_type = build_index_type (size);
14283 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14285 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14286 tree ptype = build_pointer_type (type);
14287 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14288 t = build_fold_addr_expr (t);
14289 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14290 OMP_CLAUSE_DECL (c) = t;
14292 if (TYPE_ATOMIC (type))
14294 error_at (OMP_CLAUSE_LOCATION (c),
14295 "%<_Atomic%> %qE in %<reduction%> clause", t);
14299 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14300 || OMP_CLAUSE_REDUCTION_TASK (c))
14302 /* Disallow zero sized or potentially zero sized task
14304 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14306 error_at (OMP_CLAUSE_LOCATION (c),
14307 "zero sized type %qT in %qs clause", type,
14308 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14312 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14314 error_at (OMP_CLAUSE_LOCATION (c),
14315 "variable sized type %qT in %qs clause", type,
14316 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14321 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14322 && (FLOAT_TYPE_P (type)
14323 || TREE_CODE (type) == COMPLEX_TYPE))
14325 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14326 const char *r_name = NULL;
14333 case TRUTH_ANDIF_EXPR:
14334 case TRUTH_ORIF_EXPR:
14337 if (TREE_CODE (type) == COMPLEX_TYPE)
14341 if (TREE_CODE (type) == COMPLEX_TYPE)
14354 gcc_unreachable ();
14358 error_at (OMP_CLAUSE_LOCATION (c),
14359 "%qE has invalid type for %<reduction(%s)%>",
14365 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14367 error_at (OMP_CLAUSE_LOCATION (c),
14368 "user defined reduction not found for %qE", t);
14372 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14374 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14375 type = TYPE_MAIN_VARIANT (type);
14376 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14377 VAR_DECL, NULL_TREE, type);
14378 tree decl_placeholder = NULL_TREE;
14379 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14380 DECL_ARTIFICIAL (placeholder) = 1;
14381 DECL_IGNORED_P (placeholder) = 1;
14382 if (TREE_CODE (t) == MEM_REF)
14384 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14385 VAR_DECL, NULL_TREE, type);
14386 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14387 DECL_ARTIFICIAL (decl_placeholder) = 1;
14388 DECL_IGNORED_P (decl_placeholder) = 1;
14390 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14391 c_mark_addressable (placeholder);
14392 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14393 c_mark_addressable (decl_placeholder ? decl_placeholder
14394 : OMP_CLAUSE_DECL (c));
14395 OMP_CLAUSE_REDUCTION_MERGE (c)
14396 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14397 TREE_VEC_ELT (list, 0),
14398 TREE_VEC_ELT (list, 1),
14399 decl_placeholder ? decl_placeholder
14400 : OMP_CLAUSE_DECL (c), placeholder);
14401 OMP_CLAUSE_REDUCTION_MERGE (c)
14402 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14403 void_type_node, NULL_TREE,
14404 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14405 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14406 if (TREE_VEC_LENGTH (list) == 6)
14408 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14409 c_mark_addressable (decl_placeholder ? decl_placeholder
14410 : OMP_CLAUSE_DECL (c));
14411 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14412 c_mark_addressable (placeholder);
14413 tree init = TREE_VEC_ELT (list, 5);
14414 if (init == error_mark_node)
14415 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14416 OMP_CLAUSE_REDUCTION_INIT (c)
14417 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14418 TREE_VEC_ELT (list, 3),
14419 decl_placeholder ? decl_placeholder
14420 : OMP_CLAUSE_DECL (c), placeholder);
14421 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14423 tree v = decl_placeholder ? decl_placeholder : t;
14424 OMP_CLAUSE_REDUCTION_INIT (c)
14425 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14426 OMP_CLAUSE_REDUCTION_INIT (c));
14428 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14429 c_find_omp_placeholder_r,
14430 placeholder, NULL))
14431 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14436 tree v = decl_placeholder ? decl_placeholder : t;
14437 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14438 init = build_constructor (TREE_TYPE (v), NULL);
14440 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14441 OMP_CLAUSE_REDUCTION_INIT (c)
14442 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14444 OMP_CLAUSE_REDUCTION_INIT (c)
14445 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14446 void_type_node, NULL_TREE,
14447 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14448 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14450 if (TREE_CODE (t) == MEM_REF)
14452 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14453 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14456 sorry ("variable length element type in array "
14457 "%<reduction%> clause");
14461 t = TREE_OPERAND (t, 0);
14462 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14463 t = TREE_OPERAND (t, 0);
14464 if (TREE_CODE (t) == ADDR_EXPR)
14465 t = TREE_OPERAND (t, 0);
14467 goto check_dup_generic_t;
14469 case OMP_CLAUSE_COPYPRIVATE:
14470 copyprivate_seen = true;
14473 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14474 "%<nowait%> clause must not be used together "
14475 "with %<copyprivate%>");
14476 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14477 nowait_clause = NULL;
14479 goto check_dup_generic;
14481 case OMP_CLAUSE_COPYIN:
14482 t = OMP_CLAUSE_DECL (c);
14483 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14485 error_at (OMP_CLAUSE_LOCATION (c),
14486 "%qE must be %<threadprivate%> for %<copyin%>", t);
14490 goto check_dup_generic;
14492 case OMP_CLAUSE_LINEAR:
14493 if (ort != C_ORT_OMP_DECLARE_SIMD)
14494 need_implicitly_determined = true;
14495 t = OMP_CLAUSE_DECL (c);
14496 if (ort != C_ORT_OMP_DECLARE_SIMD
14497 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14498 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14500 error_at (OMP_CLAUSE_LOCATION (c),
14501 "modifier should not be specified in %<linear%> "
14502 "clause on %<simd%> or %<for%> constructs when not "
14503 "using OpenMP 5.2 modifiers");
14504 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14506 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14507 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14509 error_at (OMP_CLAUSE_LOCATION (c),
14510 "linear clause applied to non-integral non-pointer "
14511 "variable with type %qT", TREE_TYPE (t));
14515 if (TYPE_ATOMIC (TREE_TYPE (t)))
14517 error_at (OMP_CLAUSE_LOCATION (c),
14518 "%<_Atomic%> %qD in %<linear%> clause", t);
14522 if (ort == C_ORT_OMP_DECLARE_SIMD)
14524 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14525 if (TREE_CODE (s) == PARM_DECL)
14527 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14528 /* map_head bitmap is used as uniform_head if
14530 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14531 linear_variable_step_check = true;
14532 goto check_dup_generic;
14534 if (TREE_CODE (s) != INTEGER_CST)
14536 error_at (OMP_CLAUSE_LOCATION (c),
14537 "%<linear%> clause step %qE is neither constant "
14538 "nor a parameter", s);
14543 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14545 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14546 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14547 OMP_CLAUSE_DECL (c), s);
14548 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14549 sizetype, fold_convert (sizetype, s),
14551 (sizetype, OMP_CLAUSE_DECL (c)));
14552 if (s == error_mark_node)
14554 OMP_CLAUSE_LINEAR_STEP (c) = s;
14557 OMP_CLAUSE_LINEAR_STEP (c)
14558 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14559 goto check_dup_generic;
14562 t = OMP_CLAUSE_DECL (c);
14563 check_dup_generic_t:
14564 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14566 error_at (OMP_CLAUSE_LOCATION (c),
14567 "%qE is not a variable in clause %qs", t,
14568 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14571 else if ((ort == C_ORT_ACC
14572 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14573 || (ort == C_ORT_OMP
14574 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14575 || (OMP_CLAUSE_CODE (c)
14576 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14577 || (ort == C_ORT_OMP_TARGET
14578 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14580 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14581 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14582 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14584 error_at (OMP_CLAUSE_LOCATION (c),
14585 "%qD appears more than once in data-sharing "
14590 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14591 target_in_reduction_seen = true;
14592 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14594 error_at (OMP_CLAUSE_LOCATION (c),
14596 ? "%qD appears more than once in reduction clauses"
14597 : "%qD appears more than once in data clauses",
14602 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14604 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14605 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14606 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14607 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14609 error_at (OMP_CLAUSE_LOCATION (c),
14610 "%qE appears more than once in data clauses", t);
14613 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14614 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
14615 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
14616 && bitmap_bit_p (&map_head, DECL_UID (t)))
14618 if (ort == C_ORT_ACC)
14619 error_at (OMP_CLAUSE_LOCATION (c),
14620 "%qD appears more than once in data clauses", t);
14622 error_at (OMP_CLAUSE_LOCATION (c),
14623 "%qD appears both in data and map clauses", t);
14627 bitmap_set_bit (&generic_head, DECL_UID (t));
14630 case OMP_CLAUSE_FIRSTPRIVATE:
14631 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14634 implicit_moved = true;
14635 /* Move firstprivate and map clauses with
14636 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14638 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14639 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14641 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14642 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14645 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14646 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14648 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14649 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14652 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14653 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14656 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14662 t = OMP_CLAUSE_DECL (c);
14663 need_complete = true;
14664 need_implicitly_determined = true;
14665 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14667 error_at (OMP_CLAUSE_LOCATION (c),
14668 "%qE is not a variable in clause %<firstprivate%>", t);
14671 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14672 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14673 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14675 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14676 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14677 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14679 error_at (OMP_CLAUSE_LOCATION (c),
14680 "%qE appears more than once in data clauses", t);
14683 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14685 if (ort == C_ORT_ACC)
14686 error_at (OMP_CLAUSE_LOCATION (c),
14687 "%qD appears more than once in data clauses", t);
14688 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14689 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14690 /* Silently drop the clause. */;
14692 error_at (OMP_CLAUSE_LOCATION (c),
14693 "%qD appears both in data and map clauses", t);
14697 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14700 case OMP_CLAUSE_LASTPRIVATE:
14701 t = OMP_CLAUSE_DECL (c);
14702 need_complete = true;
14703 need_implicitly_determined = true;
14704 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14706 error_at (OMP_CLAUSE_LOCATION (c),
14707 "%qE is not a variable in clause %<lastprivate%>", t);
14710 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14711 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14713 error_at (OMP_CLAUSE_LOCATION (c),
14714 "%qE appears more than once in data clauses", t);
14718 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14721 case OMP_CLAUSE_ALIGNED:
14722 t = OMP_CLAUSE_DECL (c);
14723 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14725 error_at (OMP_CLAUSE_LOCATION (c),
14726 "%qE is not a variable in %<aligned%> clause", t);
14729 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14730 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14732 error_at (OMP_CLAUSE_LOCATION (c),
14733 "%qE in %<aligned%> clause is neither a pointer nor "
14737 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14739 error_at (OMP_CLAUSE_LOCATION (c),
14740 "%<_Atomic%> %qD in %<aligned%> clause", t);
14744 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14746 error_at (OMP_CLAUSE_LOCATION (c),
14747 "%qE appears more than once in %<aligned%> clauses",
14752 bitmap_set_bit (&aligned_head, DECL_UID (t));
14755 case OMP_CLAUSE_NONTEMPORAL:
14756 t = OMP_CLAUSE_DECL (c);
14757 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14759 error_at (OMP_CLAUSE_LOCATION (c),
14760 "%qE is not a variable in %<nontemporal%> clause", t);
14763 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14765 error_at (OMP_CLAUSE_LOCATION (c),
14766 "%qE appears more than once in %<nontemporal%> "
14771 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14774 case OMP_CLAUSE_ALLOCATE:
14775 t = OMP_CLAUSE_DECL (c);
14776 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14778 error_at (OMP_CLAUSE_LOCATION (c),
14779 "%qE is not a variable in %<allocate%> clause", t);
14782 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14784 warning_at (OMP_CLAUSE_LOCATION (c), 0,
14785 "%qE appears more than once in %<allocate%> clauses",
14791 bitmap_set_bit (&aligned_head, DECL_UID (t));
14792 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14793 allocate_seen = true;
14797 case OMP_CLAUSE_DEPEND:
14798 t = OMP_CLAUSE_DECL (c);
14799 if (t == NULL_TREE)
14801 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14802 == OMP_CLAUSE_DEPEND_SOURCE);
14805 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14807 gcc_assert (TREE_CODE (t) == TREE_LIST);
14808 for (; t; t = TREE_CHAIN (t))
14810 tree decl = TREE_VALUE (t);
14811 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14813 tree offset = TREE_PURPOSE (t);
14814 bool neg = wi::neg_p (wi::to_wide (offset));
14815 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14816 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14817 neg ? MINUS_EXPR : PLUS_EXPR,
14819 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14821 fold_convert (sizetype, t2),
14822 fold_convert (sizetype, decl));
14823 if (t2 == error_mark_node)
14828 TREE_PURPOSE (t) = t2;
14834 case OMP_CLAUSE_AFFINITY:
14835 t = OMP_CLAUSE_DECL (c);
14836 if (TREE_CODE (t) == TREE_LIST
14837 && TREE_PURPOSE (t)
14838 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14840 if (TREE_PURPOSE (t) != last_iterators)
14841 last_iterators_remove
14842 = c_omp_finish_iterators (TREE_PURPOSE (t));
14843 last_iterators = TREE_PURPOSE (t);
14844 t = TREE_VALUE (t);
14845 if (last_iterators_remove)
14846 t = error_mark_node;
14849 last_iterators = NULL_TREE;
14850 if (TREE_CODE (t) == TREE_LIST)
14852 if (handle_omp_array_sections (c, ort))
14854 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14855 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14857 error_at (OMP_CLAUSE_LOCATION (c),
14858 "%<depend%> clause with %<depobj%> dependence "
14859 "type on array section");
14864 if (t == error_mark_node)
14866 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14867 && t == ridpointers[RID_OMP_ALL_MEMORY])
14869 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
14870 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
14872 error_at (OMP_CLAUSE_LOCATION (c),
14873 "%<omp_all_memory%> used with %<depend%> kind "
14874 "other than %<out%> or %<inout%>");
14878 else if (!lvalue_p (t))
14880 error_at (OMP_CLAUSE_LOCATION (c),
14881 "%qE is not lvalue expression nor array section in "
14883 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14886 else if (TREE_CODE (t) == COMPONENT_REF
14887 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14889 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14890 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14891 error_at (OMP_CLAUSE_LOCATION (c),
14892 "bit-field %qE in %qs clause", t,
14893 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14896 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14897 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14899 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14901 error_at (OMP_CLAUSE_LOCATION (c),
14902 "%qE does not have %<omp_depend_t%> type in "
14903 "%<depend%> clause with %<depobj%> dependence "
14908 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14909 && c_omp_depend_t_p (TREE_TYPE (t)))
14911 error_at (OMP_CLAUSE_LOCATION (c),
14912 "%qE should not have %<omp_depend_t%> type in "
14913 "%<depend%> clause with dependence type other than "
14919 if (t == ridpointers[RID_OMP_ALL_MEMORY])
14920 t = null_pointer_node;
14923 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
14924 ADDR_EXPR, t, false);
14925 if (addr == error_mark_node)
14930 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14932 if (t == error_mark_node)
14938 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14939 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14940 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14942 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14944 OMP_CLAUSE_DECL (c) = t;
14948 case OMP_CLAUSE_MAP:
14949 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14950 goto move_implicit;
14952 case OMP_CLAUSE_TO:
14953 case OMP_CLAUSE_FROM:
14954 case OMP_CLAUSE__CACHE_:
14955 t = OMP_CLAUSE_DECL (c);
14956 if (TREE_CODE (t) == TREE_LIST)
14958 if (handle_omp_array_sections (c, ort))
14962 t = OMP_CLAUSE_DECL (c);
14963 if (!omp_mappable_type (TREE_TYPE (t)))
14965 error_at (OMP_CLAUSE_LOCATION (c),
14966 "array section does not have mappable type "
14968 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14971 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14973 error_at (OMP_CLAUSE_LOCATION (c),
14974 "%<_Atomic%> %qE in %qs clause", t,
14975 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14978 while (TREE_CODE (t) == ARRAY_REF)
14979 t = TREE_OPERAND (t, 0);
14980 if (TREE_CODE (t) == COMPONENT_REF
14981 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14985 t = TREE_OPERAND (t, 0);
14986 if (TREE_CODE (t) == MEM_REF
14987 || TREE_CODE (t) == INDIRECT_REF)
14989 t = TREE_OPERAND (t, 0);
14991 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14992 t = TREE_OPERAND (t, 0);
14995 while (TREE_CODE (t) == COMPONENT_REF
14996 || TREE_CODE (t) == ARRAY_REF);
14998 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14999 && OMP_CLAUSE_MAP_IMPLICIT (c)
15000 && (bitmap_bit_p (&map_head, DECL_UID (t))
15001 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15002 || bitmap_bit_p (&map_firstprivate_head,
15008 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15010 if (bitmap_bit_p (&map_head, DECL_UID (t)))
15012 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15013 error_at (OMP_CLAUSE_LOCATION (c),
15014 "%qD appears more than once in motion "
15016 else if (ort == C_ORT_ACC)
15017 error_at (OMP_CLAUSE_LOCATION (c),
15018 "%qD appears more than once in data "
15021 error_at (OMP_CLAUSE_LOCATION (c),
15022 "%qD appears more than once in map "
15028 bitmap_set_bit (&map_head, DECL_UID (t));
15029 bitmap_set_bit (&map_field_head, DECL_UID (t));
15033 if (c_oacc_check_attachments (c))
15035 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15036 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15037 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15038 /* In this case, we have a single array element which is a
15039 pointer, and we already set OMP_CLAUSE_SIZE in
15040 handle_omp_array_sections above. For attach/detach clauses,
15041 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15043 OMP_CLAUSE_SIZE (c) = size_zero_node;
15046 if (t == error_mark_node)
15051 /* OpenACC attach / detach clauses must be pointers. */
15052 if (c_oacc_check_attachments (c))
15057 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15058 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15059 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15060 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15061 bias) to zero here, so it is not set erroneously to the pointer
15062 size later on in gimplify.cc. */
15063 OMP_CLAUSE_SIZE (c) = size_zero_node;
15064 while (TREE_CODE (t) == INDIRECT_REF
15065 || TREE_CODE (t) == ARRAY_REF)
15067 t = TREE_OPERAND (t, 0);
15069 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15070 t = TREE_OPERAND (t, 0);
15072 while (TREE_CODE (t) == COMPOUND_EXPR)
15074 t = TREE_OPERAND (t, 1);
15077 indir_component_ref_p = false;
15078 if (TREE_CODE (t) == COMPONENT_REF
15079 && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15080 || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15081 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15083 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15084 indir_component_ref_p = true;
15086 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15087 t = TREE_OPERAND (t, 0);
15090 if (TREE_CODE (t) == COMPONENT_REF
15091 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15093 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15095 error_at (OMP_CLAUSE_LOCATION (c),
15096 "bit-field %qE in %qs clause",
15097 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15100 else if (!omp_mappable_type (TREE_TYPE (t)))
15102 error_at (OMP_CLAUSE_LOCATION (c),
15103 "%qE does not have a mappable type in %qs clause",
15104 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15107 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15109 error_at (OMP_CLAUSE_LOCATION (c),
15110 "%<_Atomic%> %qE in %qs clause", t,
15111 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15114 while (TREE_CODE (t) == COMPONENT_REF)
15116 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15119 error_at (OMP_CLAUSE_LOCATION (c),
15120 "%qE is a member of a union", t);
15124 t = TREE_OPERAND (t, 0);
15125 if (TREE_CODE (t) == MEM_REF)
15127 if (maybe_ne (mem_ref_offset (t), 0))
15128 error_at (OMP_CLAUSE_LOCATION (c),
15129 "cannot dereference %qE in %qs clause", t,
15130 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15132 t = TREE_OPERAND (t, 0);
15134 while (TREE_CODE (t) == MEM_REF
15135 || TREE_CODE (t) == INDIRECT_REF
15136 || TREE_CODE (t) == ARRAY_REF)
15138 t = TREE_OPERAND (t, 0);
15140 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15141 t = TREE_OPERAND (t, 0);
15146 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15148 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15149 || (ort != C_ORT_ACC
15150 && bitmap_bit_p (&map_head, DECL_UID (t))))
15154 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15156 error_at (OMP_CLAUSE_LOCATION (c),
15157 "%qE is not a variable in %qs clause", t,
15158 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15161 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15163 error_at (OMP_CLAUSE_LOCATION (c),
15164 "%qD is threadprivate variable in %qs clause", t,
15165 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15168 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15169 || (OMP_CLAUSE_MAP_KIND (c)
15170 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15171 && !indir_component_ref_p
15172 && !c_mark_addressable (t))
15174 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15175 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15176 || (OMP_CLAUSE_MAP_KIND (c)
15177 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15178 || (OMP_CLAUSE_MAP_KIND (c)
15179 == GOMP_MAP_FORCE_DEVICEPTR)))
15180 && t == OMP_CLAUSE_DECL (c)
15181 && !omp_mappable_type (TREE_TYPE (t)))
15183 error_at (OMP_CLAUSE_LOCATION (c),
15184 "%qD does not have a mappable type in %qs clause", t,
15185 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15188 else if (TREE_TYPE (t) == error_mark_node)
15190 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15192 error_at (OMP_CLAUSE_LOCATION (c),
15193 "%<_Atomic%> %qE in %qs clause", t,
15194 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15197 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15198 && OMP_CLAUSE_MAP_IMPLICIT (c)
15199 && (bitmap_bit_p (&map_head, DECL_UID (t))
15200 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15201 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15203 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15204 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15206 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15207 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15208 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15210 error_at (OMP_CLAUSE_LOCATION (c),
15211 "%qD appears more than once in data clauses", t);
15214 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15215 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15217 if (ort == C_ORT_ACC)
15218 error_at (OMP_CLAUSE_LOCATION (c),
15219 "%qD appears more than once in data clauses", t);
15221 error_at (OMP_CLAUSE_LOCATION (c),
15222 "%qD appears both in data and map clauses", t);
15226 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15228 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15229 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15231 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15232 error_at (OMP_CLAUSE_LOCATION (c),
15233 "%qD appears more than once in motion clauses", t);
15234 else if (ort == C_ORT_ACC)
15235 error_at (OMP_CLAUSE_LOCATION (c),
15236 "%qD appears more than once in data clauses", t);
15238 error_at (OMP_CLAUSE_LOCATION (c),
15239 "%qD appears more than once in map clauses", t);
15242 else if (ort == C_ORT_ACC
15243 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15245 error_at (OMP_CLAUSE_LOCATION (c),
15246 "%qD appears more than once in data clauses", t);
15249 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15250 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15252 if (ort == C_ORT_ACC)
15253 error_at (OMP_CLAUSE_LOCATION (c),
15254 "%qD appears more than once in data clauses", t);
15256 error_at (OMP_CLAUSE_LOCATION (c),
15257 "%qD appears both in data and map clauses", t);
15262 bitmap_set_bit (&map_head, DECL_UID (t));
15263 if (t != OMP_CLAUSE_DECL (c)
15264 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15265 bitmap_set_bit (&map_field_head, DECL_UID (t));
15269 case OMP_CLAUSE_ENTER:
15270 case OMP_CLAUSE_LINK:
15271 t = OMP_CLAUSE_DECL (c);
15273 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15274 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15275 && OMP_CLAUSE_ENTER_TO (c))
15277 if (TREE_CODE (t) == FUNCTION_DECL
15278 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15280 else if (!VAR_P (t))
15282 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15283 error_at (OMP_CLAUSE_LOCATION (c),
15284 "%qE is neither a variable nor a function name in "
15285 "clause %qs", t, cname);
15287 error_at (OMP_CLAUSE_LOCATION (c),
15288 "%qE is not a variable in clause %qs", t, cname);
15291 else if (DECL_THREAD_LOCAL_P (t))
15293 error_at (OMP_CLAUSE_LOCATION (c),
15294 "%qD is threadprivate variable in %qs clause", t,
15298 else if (!omp_mappable_type (TREE_TYPE (t)))
15300 error_at (OMP_CLAUSE_LOCATION (c),
15301 "%qD does not have a mappable type in %qs clause", t,
15307 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15309 error_at (OMP_CLAUSE_LOCATION (c),
15310 "%qE appears more than once on the same "
15311 "%<declare target%> directive", t);
15315 bitmap_set_bit (&generic_head, DECL_UID (t));
15318 case OMP_CLAUSE_UNIFORM:
15319 t = OMP_CLAUSE_DECL (c);
15320 if (TREE_CODE (t) != PARM_DECL)
15323 error_at (OMP_CLAUSE_LOCATION (c),
15324 "%qD is not an argument in %<uniform%> clause", t);
15326 error_at (OMP_CLAUSE_LOCATION (c),
15327 "%qE is not an argument in %<uniform%> clause", t);
15331 /* map_head bitmap is used as uniform_head if declare_simd. */
15332 bitmap_set_bit (&map_head, DECL_UID (t));
15333 goto check_dup_generic;
15335 case OMP_CLAUSE_IS_DEVICE_PTR:
15336 case OMP_CLAUSE_USE_DEVICE_PTR:
15337 t = OMP_CLAUSE_DECL (c);
15338 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15339 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15340 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15342 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15343 && ort != C_ORT_ACC)
15345 error_at (OMP_CLAUSE_LOCATION (c),
15346 "%qs variable is not a pointer",
15347 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15350 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15352 error_at (OMP_CLAUSE_LOCATION (c),
15353 "%qs variable is neither a pointer nor an array",
15354 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15358 goto check_dup_generic;
15360 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15361 t = OMP_CLAUSE_DECL (c);
15362 if (TREE_CODE (t) == TREE_LIST)
15364 if (handle_omp_array_sections (c, ort))
15368 t = OMP_CLAUSE_DECL (c);
15369 while (TREE_CODE (t) == ARRAY_REF)
15370 t = TREE_OPERAND (t, 0);
15373 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15374 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15375 c_mark_addressable (t);
15376 goto check_dup_generic_t;
15378 case OMP_CLAUSE_USE_DEVICE_ADDR:
15379 t = OMP_CLAUSE_DECL (c);
15380 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15381 c_mark_addressable (t);
15382 goto check_dup_generic;
15384 case OMP_CLAUSE_NOWAIT:
15385 if (copyprivate_seen)
15387 error_at (OMP_CLAUSE_LOCATION (c),
15388 "%<nowait%> clause must not be used together "
15389 "with %<copyprivate%>");
15393 nowait_clause = pc;
15394 pc = &OMP_CLAUSE_CHAIN (c);
15397 case OMP_CLAUSE_ORDER:
15398 if (ordered_clause)
15400 error_at (OMP_CLAUSE_LOCATION (c),
15401 "%<order%> clause must not be used together "
15402 "with %<ordered%>");
15406 else if (order_clause)
15408 /* Silently remove duplicates. */
15413 pc = &OMP_CLAUSE_CHAIN (c);
15416 case OMP_CLAUSE_DETACH:
15417 t = OMP_CLAUSE_DECL (c);
15420 error_at (OMP_CLAUSE_LOCATION (c),
15421 "too many %qs clauses on a task construct",
15427 pc = &OMP_CLAUSE_CHAIN (c);
15428 c_mark_addressable (t);
15431 case OMP_CLAUSE_IF:
15432 case OMP_CLAUSE_NUM_THREADS:
15433 case OMP_CLAUSE_NUM_TEAMS:
15434 case OMP_CLAUSE_THREAD_LIMIT:
15435 case OMP_CLAUSE_DEFAULT:
15436 case OMP_CLAUSE_UNTIED:
15437 case OMP_CLAUSE_COLLAPSE:
15438 case OMP_CLAUSE_FINAL:
15439 case OMP_CLAUSE_DEVICE:
15440 case OMP_CLAUSE_DIST_SCHEDULE:
15441 case OMP_CLAUSE_PARALLEL:
15442 case OMP_CLAUSE_FOR:
15443 case OMP_CLAUSE_SECTIONS:
15444 case OMP_CLAUSE_TASKGROUP:
15445 case OMP_CLAUSE_PROC_BIND:
15446 case OMP_CLAUSE_DEVICE_TYPE:
15447 case OMP_CLAUSE_PRIORITY:
15448 case OMP_CLAUSE_GRAINSIZE:
15449 case OMP_CLAUSE_NUM_TASKS:
15450 case OMP_CLAUSE_THREADS:
15451 case OMP_CLAUSE_SIMD:
15452 case OMP_CLAUSE_HINT:
15453 case OMP_CLAUSE_FILTER:
15454 case OMP_CLAUSE_DEFAULTMAP:
15455 case OMP_CLAUSE_BIND:
15456 case OMP_CLAUSE_NUM_GANGS:
15457 case OMP_CLAUSE_NUM_WORKERS:
15458 case OMP_CLAUSE_VECTOR_LENGTH:
15459 case OMP_CLAUSE_ASYNC:
15460 case OMP_CLAUSE_WAIT:
15461 case OMP_CLAUSE_AUTO:
15462 case OMP_CLAUSE_INDEPENDENT:
15463 case OMP_CLAUSE_SEQ:
15464 case OMP_CLAUSE_GANG:
15465 case OMP_CLAUSE_WORKER:
15466 case OMP_CLAUSE_VECTOR:
15467 case OMP_CLAUSE_TILE:
15468 case OMP_CLAUSE_IF_PRESENT:
15469 case OMP_CLAUSE_FINALIZE:
15470 case OMP_CLAUSE_NOHOST:
15471 pc = &OMP_CLAUSE_CHAIN (c);
15474 case OMP_CLAUSE_MERGEABLE:
15475 mergeable_seen = true;
15476 pc = &OMP_CLAUSE_CHAIN (c);
15479 case OMP_CLAUSE_NOGROUP:
15481 pc = &OMP_CLAUSE_CHAIN (c);
15484 case OMP_CLAUSE_SCHEDULE:
15485 schedule_clause = c;
15486 pc = &OMP_CLAUSE_CHAIN (c);
15489 case OMP_CLAUSE_ORDERED:
15490 ordered_clause = c;
15493 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15494 "%<order%> clause must not be used together "
15495 "with %<ordered%>");
15496 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15497 order_clause = NULL;
15499 pc = &OMP_CLAUSE_CHAIN (c);
15502 case OMP_CLAUSE_SAFELEN:
15504 pc = &OMP_CLAUSE_CHAIN (c);
15506 case OMP_CLAUSE_SIMDLEN:
15508 pc = &OMP_CLAUSE_CHAIN (c);
15511 case OMP_CLAUSE_INBRANCH:
15512 case OMP_CLAUSE_NOTINBRANCH:
15515 error_at (OMP_CLAUSE_LOCATION (c),
15516 "%<inbranch%> clause is incompatible with "
15517 "%<notinbranch%>");
15521 branch_seen = true;
15522 pc = &OMP_CLAUSE_CHAIN (c);
15525 case OMP_CLAUSE_INCLUSIVE:
15526 case OMP_CLAUSE_EXCLUSIVE:
15527 need_complete = true;
15528 need_implicitly_determined = true;
15529 t = OMP_CLAUSE_DECL (c);
15530 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15532 error_at (OMP_CLAUSE_LOCATION (c),
15533 "%qE is not a variable in clause %qs", t,
15534 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15540 gcc_unreachable ();
15545 t = OMP_CLAUSE_DECL (c);
15549 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15550 if (t == error_mark_node)
15554 if (need_implicitly_determined)
15556 const char *share_name = NULL;
15558 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15559 share_name = "threadprivate";
15560 else switch (c_omp_predetermined_sharing (t))
15562 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15564 case OMP_CLAUSE_DEFAULT_SHARED:
15565 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15566 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15567 && c_omp_predefined_variable (t))
15568 /* The __func__ variable and similar function-local
15569 predefined variables may be listed in a shared or
15570 firstprivate clause. */
15572 share_name = "shared";
15574 case OMP_CLAUSE_DEFAULT_PRIVATE:
15575 share_name = "private";
15578 gcc_unreachable ();
15582 error_at (OMP_CLAUSE_LOCATION (c),
15583 "%qE is predetermined %qs for %qs",
15585 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15588 else if (TREE_READONLY (t)
15589 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15590 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15592 error_at (OMP_CLAUSE_LOCATION (c),
15593 "%<const%> qualified %qE may appear only in "
15594 "%<shared%> or %<firstprivate%> clauses", t);
15601 *pc = OMP_CLAUSE_CHAIN (c);
15603 pc = &OMP_CLAUSE_CHAIN (c);
15608 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15609 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15611 error_at (OMP_CLAUSE_LOCATION (simdlen),
15612 "%<simdlen%> clause value is bigger than "
15613 "%<safelen%> clause value");
15614 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15615 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15620 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15621 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15623 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15624 "%<nonmonotonic%> schedule modifier specified together "
15625 "with %<ordered%> clause");
15626 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15627 = (enum omp_clause_schedule_kind)
15628 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15629 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15632 if (reduction_seen < 0 && ordered_clause)
15634 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15635 "%qs clause specified together with %<inscan%> "
15636 "%<reduction%> clause", "ordered");
15637 reduction_seen = -2;
15640 if (reduction_seen < 0 && schedule_clause)
15642 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15643 "%qs clause specified together with %<inscan%> "
15644 "%<reduction%> clause", "schedule");
15645 reduction_seen = -2;
15648 if (linear_variable_step_check
15649 || reduction_seen == -2
15651 || target_in_reduction_seen)
15652 for (pc = &clauses, c = clauses; c ; c = *pc)
15654 bool remove = false;
15656 switch (OMP_CLAUSE_CODE (c))
15658 case OMP_CLAUSE_REDUCTION:
15659 case OMP_CLAUSE_IN_REDUCTION:
15660 case OMP_CLAUSE_TASK_REDUCTION:
15661 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15663 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15664 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15665 t = TREE_OPERAND (t, 0);
15666 if (TREE_CODE (t) == ADDR_EXPR
15667 || TREE_CODE (t) == INDIRECT_REF)
15668 t = TREE_OPERAND (t, 0);
15670 bitmap_clear_bit (&aligned_head, DECL_UID (t));
15674 case OMP_CLAUSE_PRIVATE:
15675 case OMP_CLAUSE_FIRSTPRIVATE:
15676 case OMP_CLAUSE_LASTPRIVATE:
15677 case OMP_CLAUSE_LINEAR:
15678 if (DECL_P (OMP_CLAUSE_DECL (c)))
15679 bitmap_clear_bit (&aligned_head,
15680 DECL_UID (OMP_CLAUSE_DECL (c)));
15685 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15686 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15687 && !bitmap_bit_p (&map_head,
15688 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15690 error_at (OMP_CLAUSE_LOCATION (c),
15691 "%<linear%> clause step is a parameter %qD not "
15692 "specified in %<uniform%> clause",
15693 OMP_CLAUSE_LINEAR_STEP (c));
15696 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15697 && reduction_seen == -2)
15698 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15699 if (target_in_reduction_seen
15700 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15702 tree t = OMP_CLAUSE_DECL (c);
15703 while (handled_component_p (t)
15704 || TREE_CODE (t) == INDIRECT_REF
15705 || TREE_CODE (t) == ADDR_EXPR
15706 || TREE_CODE (t) == MEM_REF
15707 || TREE_CODE (t) == NON_LVALUE_EXPR)
15708 t = TREE_OPERAND (t, 0);
15710 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15711 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15715 *pc = OMP_CLAUSE_CHAIN (c);
15717 pc = &OMP_CLAUSE_CHAIN (c);
15721 for (pc = &clauses, c = clauses; c ; c = *pc)
15723 bool remove = false;
15724 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15725 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15726 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15728 error_at (OMP_CLAUSE_LOCATION (c),
15729 "%qD specified in %<allocate%> clause but not in "
15730 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15734 *pc = OMP_CLAUSE_CHAIN (c);
15736 pc = &OMP_CLAUSE_CHAIN (c);
15739 if (nogroup_seen && reduction_seen)
15741 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15742 "%<nogroup%> clause must not be used together with "
15743 "%<reduction%> clause");
15744 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15749 if (mergeable_seen)
15751 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15752 "%<detach%> clause must not be used together with "
15753 "%<mergeable%> clause");
15754 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15758 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15760 for (pc = &clauses, c = clauses; c ; c = *pc)
15762 bool remove = false;
15763 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15764 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15765 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15766 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15767 && OMP_CLAUSE_DECL (c) == detach_decl)
15769 error_at (OMP_CLAUSE_LOCATION (c),
15770 "the event handle of a %<detach%> clause "
15771 "should not be in a data-sharing clause");
15775 *pc = OMP_CLAUSE_CHAIN (c);
15777 pc = &OMP_CLAUSE_CHAIN (c);
15782 bitmap_obstack_release (NULL);
15786 /* Return code to initialize DST with a copy constructor from SRC.
15787 C doesn't have copy constructors nor assignment operators, only for
15788 _Atomic vars we need to perform __atomic_load from src into a temporary
15789 followed by __atomic_store of the temporary to dst. */
15792 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15794 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15795 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15797 location_t loc = OMP_CLAUSE_LOCATION (clause);
15798 tree type = TREE_TYPE (dst);
15799 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15800 tree tmp = create_tmp_var (nonatomic_type);
15801 tree tmp_addr = build_fold_addr_expr (tmp);
15802 TREE_ADDRESSABLE (tmp) = 1;
15803 suppress_warning (tmp);
15804 tree src_addr = build_fold_addr_expr (src);
15805 tree dst_addr = build_fold_addr_expr (dst);
15806 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15807 vec<tree, va_gc> *params;
15808 /* Expansion of a generic atomic load may require an addition
15809 element, so allocate enough to prevent a resize. */
15810 vec_alloc (params, 4);
15812 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15813 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15814 params->quick_push (src_addr);
15815 params->quick_push (tmp_addr);
15816 params->quick_push (seq_cst);
15817 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15819 vec_alloc (params, 4);
15821 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15822 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15823 params->quick_push (dst_addr);
15824 params->quick_push (tmp_addr);
15825 params->quick_push (seq_cst);
15826 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15827 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15830 /* Create a transaction node. */
15833 c_finish_transaction (location_t loc, tree block, int flags)
15835 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15836 if (flags & TM_STMT_ATTR_OUTER)
15837 TRANSACTION_EXPR_OUTER (stmt) = 1;
15838 if (flags & TM_STMT_ATTR_RELAXED)
15839 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15840 return add_stmt (stmt);
15843 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15844 down to the element type of an array. If ORIG_QUAL_TYPE is not
15845 NULL, then it should be used as the qualified type
15846 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15847 preserve information about the typedef name from which an array
15848 type was derived). */
15851 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15852 size_t orig_qual_indirect)
15854 if (type == error_mark_node)
15857 if (TREE_CODE (type) == ARRAY_TYPE)
15860 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15861 type_quals, orig_qual_type,
15862 orig_qual_indirect - 1);
15864 /* See if we already have an identically qualified type. */
15865 if (orig_qual_type && orig_qual_indirect == 0)
15866 t = orig_qual_type;
15868 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15870 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15871 && TYPE_NAME (t) == TYPE_NAME (type)
15872 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15873 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15874 TYPE_ATTRIBUTES (type)))
15879 tree domain = TYPE_DOMAIN (type);
15881 t = build_variant_type_copy (type);
15882 TREE_TYPE (t) = element_type;
15884 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15885 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15886 SET_TYPE_STRUCTURAL_EQUALITY (t);
15887 else if (TYPE_CANONICAL (element_type) != element_type
15888 || (domain && TYPE_CANONICAL (domain) != domain))
15890 tree unqualified_canon
15891 = build_array_type (TYPE_CANONICAL (element_type),
15892 domain? TYPE_CANONICAL (domain)
15894 if (TYPE_REVERSE_STORAGE_ORDER (type))
15897 = build_distinct_type_copy (unqualified_canon);
15898 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15901 = c_build_qualified_type (unqualified_canon, type_quals);
15904 TYPE_CANONICAL (t) = t;
15909 /* A restrict-qualified pointer type must be a pointer to object or
15910 incomplete type. Note that the use of POINTER_TYPE_P also allows
15911 REFERENCE_TYPEs, which is appropriate for C++. */
15912 if ((type_quals & TYPE_QUAL_RESTRICT)
15913 && (!POINTER_TYPE_P (type)
15914 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15916 error ("invalid use of %<restrict%>");
15917 type_quals &= ~TYPE_QUAL_RESTRICT;
15920 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15922 : build_qualified_type (type, type_quals));
15923 /* A variant type does not inherit the list of incomplete vars from the
15924 type main variant. */
15925 if ((RECORD_OR_UNION_TYPE_P (var_type)
15926 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15927 && TYPE_MAIN_VARIANT (var_type) != var_type)
15928 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15932 /* Build a VA_ARG_EXPR for the C parser. */
15935 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15937 if (error_operand_p (type))
15938 return error_mark_node;
15939 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15940 order because it takes the address of the expression. */
15941 else if (handled_component_p (expr)
15942 && reverse_storage_order_for_component_p (expr))
15944 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15945 return error_mark_node;
15947 else if (!COMPLETE_TYPE_P (type))
15949 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15951 return error_mark_node;
15953 else if (TREE_CODE (type) == FUNCTION_TYPE)
15955 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
15957 return error_mark_node;
15959 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15960 warning_at (loc2, OPT_Wc___compat,
15961 "C++ requires promoted type, not enum type, in %<va_arg%>");
15962 return build_va_arg (loc2, expr, type);
15965 /* Return truthvalue of whether T1 is the same tree structure as T2.
15966 Return 1 if they are the same. Return false if they are different. */
15969 c_tree_equal (tree t1, tree t2)
15971 enum tree_code code1, code2;
15978 for (code1 = TREE_CODE (t1);
15979 CONVERT_EXPR_CODE_P (code1)
15980 || code1 == NON_LVALUE_EXPR;
15981 code1 = TREE_CODE (t1))
15982 t1 = TREE_OPERAND (t1, 0);
15983 for (code2 = TREE_CODE (t2);
15984 CONVERT_EXPR_CODE_P (code2)
15985 || code2 == NON_LVALUE_EXPR;
15986 code2 = TREE_CODE (t2))
15987 t2 = TREE_OPERAND (t2, 0);
15989 /* They might have become equal now. */
15993 if (code1 != code2)
15999 return wi::to_wide (t1) == wi::to_wide (t2);
16002 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16005 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16006 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16007 TREE_STRING_LENGTH (t1));
16010 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16011 TREE_FIXED_CST (t2));
16014 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16015 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16018 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16021 /* We need to do this when determining whether or not two
16022 non-type pointer to member function template arguments
16024 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16025 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16030 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16032 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16033 if (!c_tree_equal (field, elt2->index)
16034 || !c_tree_equal (value, elt2->value))
16041 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16043 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16045 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16048 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16053 call_expr_arg_iterator iter1, iter2;
16054 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16056 for (arg1 = first_call_expr_arg (t1, &iter1),
16057 arg2 = first_call_expr_arg (t2, &iter2);
16059 arg1 = next_call_expr_arg (&iter1),
16060 arg2 = next_call_expr_arg (&iter2))
16061 if (!c_tree_equal (arg1, arg2))
16070 tree o1 = TREE_OPERAND (t1, 0);
16071 tree o2 = TREE_OPERAND (t2, 0);
16073 /* Special case: if either target is an unallocated VAR_DECL,
16074 it means that it's going to be unified with whatever the
16075 TARGET_EXPR is really supposed to initialize, so treat it
16076 as being equivalent to anything. */
16077 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16078 && !DECL_RTL_SET_P (o1))
16080 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16081 && !DECL_RTL_SET_P (o2))
16083 else if (!c_tree_equal (o1, o2))
16086 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16089 case COMPONENT_REF:
16090 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16092 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16098 case FUNCTION_DECL:
16099 case IDENTIFIER_NODE:
16106 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16108 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16109 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16110 TREE_VEC_ELT (t2, ix)))
16119 switch (TREE_CODE_CLASS (code1))
16123 case tcc_comparison:
16124 case tcc_expression:
16126 case tcc_reference:
16127 case tcc_statement:
16129 int i, n = TREE_OPERAND_LENGTH (t1);
16133 case PREINCREMENT_EXPR:
16134 case PREDECREMENT_EXPR:
16135 case POSTINCREMENT_EXPR:
16136 case POSTDECREMENT_EXPR:
16146 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16147 && n != TREE_OPERAND_LENGTH (t2))
16150 for (i = 0; i < n; ++i)
16151 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16158 return comptypes (t1, t2);
16160 gcc_unreachable ();
16164 /* Returns true when the function declaration FNDECL is implicit,
16165 introduced as a result of a call to an otherwise undeclared
16166 function, and false otherwise. */
16169 c_decl_implicit (const_tree fndecl)
16171 return C_DECL_IMPLICIT (fndecl);