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, "variable-sized object may not be initialized");
8295 return error_mark_node;
8298 error_init (init_loc, "invalid initializer");
8299 return error_mark_node;
8302 /* Handle initializers that use braces. */
8304 /* Type of object we are accumulating a constructor for.
8305 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8306 static tree constructor_type;
8308 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8310 static tree constructor_fields;
8312 /* For an ARRAY_TYPE, this is the specified index
8313 at which to store the next element we get. */
8314 static tree constructor_index;
8316 /* For an ARRAY_TYPE, this is the maximum index. */
8317 static tree constructor_max_index;
8319 /* For a RECORD_TYPE, this is the first field not yet written out. */
8320 static tree constructor_unfilled_fields;
8322 /* For an ARRAY_TYPE, this is the index of the first element
8323 not yet written out. */
8324 static tree constructor_unfilled_index;
8326 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8327 This is so we can generate gaps between fields, when appropriate. */
8328 static tree constructor_bit_index;
8330 /* If we are saving up the elements rather than allocating them,
8331 this is the list of elements so far (in reverse order,
8332 most recent first). */
8333 static vec<constructor_elt, va_gc> *constructor_elements;
8335 /* 1 if constructor should be incrementally stored into a constructor chain,
8336 0 if all the elements should be kept in AVL tree. */
8337 static int constructor_incremental;
8339 /* 1 if so far this constructor's elements are all compile-time constants. */
8340 static int constructor_constant;
8342 /* 1 if so far this constructor's elements are all valid address constants. */
8343 static int constructor_simple;
8345 /* 1 if this constructor has an element that cannot be part of a
8346 constant expression. */
8347 static int constructor_nonconst;
8349 /* 1 if this constructor is erroneous so far. */
8350 static int constructor_erroneous;
8352 /* 1 if this constructor is the universal zero initializer { 0 }. */
8353 static int constructor_zeroinit;
8355 /* Structure for managing pending initializer elements, organized as an
8360 struct init_node *left, *right;
8361 struct init_node *parent;
8368 /* Tree of pending elements at this constructor level.
8369 These are elements encountered out of order
8370 which belong at places we haven't reached yet in actually
8372 Will never hold tree nodes across GC runs. */
8373 static struct init_node *constructor_pending_elts;
8375 /* The SPELLING_DEPTH of this constructor. */
8376 static int constructor_depth;
8378 /* DECL node for which an initializer is being read.
8379 0 means we are reading a constructor expression
8380 such as (struct foo) {...}. */
8381 static tree constructor_decl;
8383 /* Nonzero if this is an initializer for a top-level decl. */
8384 static int constructor_top_level;
8386 /* Nonzero if there were any member designators in this initializer. */
8387 static int constructor_designated;
8389 /* Nesting depth of designator list. */
8390 static int designator_depth;
8392 /* Nonzero if there were diagnosed errors in this designator list. */
8393 static int designator_erroneous;
8396 /* This stack has a level for each implicit or explicit level of
8397 structuring in the initializer, including the outermost one. It
8398 saves the values of most of the variables above. */
8400 struct constructor_range_stack;
8402 struct constructor_stack
8404 struct constructor_stack *next;
8409 tree unfilled_index;
8410 tree unfilled_fields;
8412 vec<constructor_elt, va_gc> *elements;
8413 struct init_node *pending_elts;
8416 /* If value nonzero, this value should replace the entire
8417 constructor at this level. */
8418 struct c_expr replacement_value;
8419 struct constructor_range_stack *range_stack;
8428 int designator_depth;
8431 static struct constructor_stack *constructor_stack;
8433 /* This stack represents designators from some range designator up to
8434 the last designator in the list. */
8436 struct constructor_range_stack
8438 struct constructor_range_stack *next, *prev;
8439 struct constructor_stack *stack;
8446 static struct constructor_range_stack *constructor_range_stack;
8448 /* This stack records separate initializers that are nested.
8449 Nested initializers can't happen in ANSI C, but GNU C allows them
8450 in cases like { ... (struct foo) { ... } ... }. */
8452 struct initializer_stack
8454 struct initializer_stack *next;
8456 struct constructor_stack *constructor_stack;
8457 struct constructor_range_stack *constructor_range_stack;
8458 vec<constructor_elt, va_gc> *elements;
8459 struct spelling *spelling;
8460 struct spelling *spelling_base;
8463 char require_constant_value;
8464 char require_constant_elements;
8466 rich_location *missing_brace_richloc;
8469 static struct initializer_stack *initializer_stack;
8471 /* Prepare to parse and output the initializer for variable DECL. */
8474 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8475 rich_location *richloc)
8478 struct initializer_stack *p = XNEW (struct initializer_stack);
8480 p->decl = constructor_decl;
8481 p->require_constant_value = require_constant_value;
8482 p->require_constant_elements = require_constant_elements;
8483 p->constructor_stack = constructor_stack;
8484 p->constructor_range_stack = constructor_range_stack;
8485 p->elements = constructor_elements;
8486 p->spelling = spelling;
8487 p->spelling_base = spelling_base;
8488 p->spelling_size = spelling_size;
8489 p->top_level = constructor_top_level;
8490 p->next = initializer_stack;
8491 p->missing_brace_richloc = richloc;
8492 p->designated = constructor_designated;
8493 initializer_stack = p;
8495 constructor_decl = decl;
8496 constructor_designated = 0;
8497 constructor_top_level = top_level;
8499 if (decl != NULL_TREE && decl != error_mark_node)
8501 require_constant_value = TREE_STATIC (decl);
8502 require_constant_elements
8503 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8504 /* For a scalar, you can always use any value to initialize,
8505 even within braces. */
8506 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8507 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8511 require_constant_value = 0;
8512 require_constant_elements = 0;
8513 locus = _("(anonymous)");
8516 constructor_stack = 0;
8517 constructor_range_stack = 0;
8519 found_missing_braces = 0;
8523 RESTORE_SPELLING_DEPTH (0);
8526 push_string (locus);
8532 struct initializer_stack *p = initializer_stack;
8534 /* Free the whole constructor stack of this initializer. */
8535 while (constructor_stack)
8537 struct constructor_stack *q = constructor_stack;
8538 constructor_stack = q->next;
8542 gcc_assert (!constructor_range_stack);
8544 /* Pop back to the data of the outer initializer (if any). */
8545 XDELETE (spelling_base);
8547 constructor_decl = p->decl;
8548 require_constant_value = p->require_constant_value;
8549 require_constant_elements = p->require_constant_elements;
8550 constructor_stack = p->constructor_stack;
8551 constructor_designated = p->designated;
8552 constructor_range_stack = p->constructor_range_stack;
8553 constructor_elements = p->elements;
8554 spelling = p->spelling;
8555 spelling_base = p->spelling_base;
8556 spelling_size = p->spelling_size;
8557 constructor_top_level = p->top_level;
8558 initializer_stack = p->next;
8562 /* Call here when we see the initializer is surrounded by braces.
8563 This is instead of a call to push_init_level;
8564 it is matched by a call to pop_init_level.
8566 TYPE is the type to initialize, for a constructor expression.
8567 For an initializer for a decl, TYPE is zero. */
8570 really_start_incremental_init (tree type)
8572 struct constructor_stack *p = XNEW (struct constructor_stack);
8574 if (type == NULL_TREE)
8575 type = TREE_TYPE (constructor_decl);
8577 if (VECTOR_TYPE_P (type)
8578 && TYPE_VECTOR_OPAQUE (type))
8579 error ("opaque vector types cannot be initialized");
8581 p->type = constructor_type;
8582 p->fields = constructor_fields;
8583 p->index = constructor_index;
8584 p->max_index = constructor_max_index;
8585 p->unfilled_index = constructor_unfilled_index;
8586 p->unfilled_fields = constructor_unfilled_fields;
8587 p->bit_index = constructor_bit_index;
8588 p->elements = constructor_elements;
8589 p->constant = constructor_constant;
8590 p->simple = constructor_simple;
8591 p->nonconst = constructor_nonconst;
8592 p->erroneous = constructor_erroneous;
8593 p->pending_elts = constructor_pending_elts;
8594 p->depth = constructor_depth;
8595 p->replacement_value.value = 0;
8596 p->replacement_value.original_code = ERROR_MARK;
8597 p->replacement_value.original_type = NULL;
8601 p->incremental = constructor_incremental;
8602 p->designated = constructor_designated;
8603 p->designator_depth = designator_depth;
8605 constructor_stack = p;
8607 constructor_constant = 1;
8608 constructor_simple = 1;
8609 constructor_nonconst = 0;
8610 constructor_depth = SPELLING_DEPTH ();
8611 constructor_elements = NULL;
8612 constructor_pending_elts = 0;
8613 constructor_type = type;
8614 constructor_incremental = 1;
8615 constructor_designated = 0;
8616 constructor_zeroinit = 1;
8617 designator_depth = 0;
8618 designator_erroneous = 0;
8620 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8622 constructor_fields = TYPE_FIELDS (constructor_type);
8623 /* Skip any nameless bit fields at the beginning. */
8624 while (constructor_fields != NULL_TREE
8625 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8626 constructor_fields = DECL_CHAIN (constructor_fields);
8628 constructor_unfilled_fields = constructor_fields;
8629 constructor_bit_index = bitsize_zero_node;
8631 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8633 if (TYPE_DOMAIN (constructor_type))
8635 constructor_max_index
8636 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8638 /* Detect non-empty initializations of zero-length arrays. */
8639 if (constructor_max_index == NULL_TREE
8640 && TYPE_SIZE (constructor_type))
8641 constructor_max_index = integer_minus_one_node;
8643 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8644 to initialize VLAs will cause a proper error; avoid tree
8645 checking errors as well by setting a safe value. */
8646 if (constructor_max_index
8647 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8648 constructor_max_index = integer_minus_one_node;
8651 = convert (bitsizetype,
8652 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8656 constructor_index = bitsize_zero_node;
8657 constructor_max_index = NULL_TREE;
8660 constructor_unfilled_index = constructor_index;
8662 else if (gnu_vector_type_p (constructor_type))
8664 /* Vectors are like simple fixed-size arrays. */
8665 constructor_max_index =
8666 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8667 constructor_index = bitsize_zero_node;
8668 constructor_unfilled_index = constructor_index;
8672 /* Handle the case of int x = {5}; */
8673 constructor_fields = constructor_type;
8674 constructor_unfilled_fields = constructor_type;
8678 extern location_t last_init_list_comma;
8680 /* Called when we see an open brace for a nested initializer. Finish
8681 off any pending levels with implicit braces. */
8683 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8685 while (constructor_stack->implicit)
8687 if (RECORD_OR_UNION_TYPE_P (constructor_type)
8688 && constructor_fields == NULL_TREE)
8689 process_init_element (input_location,
8690 pop_init_level (loc, 1, braced_init_obstack,
8691 last_init_list_comma),
8692 true, braced_init_obstack);
8693 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8694 && constructor_max_index
8695 && tree_int_cst_lt (constructor_max_index,
8697 process_init_element (input_location,
8698 pop_init_level (loc, 1, braced_init_obstack,
8699 last_init_list_comma),
8700 true, braced_init_obstack);
8706 /* Push down into a subobject, for initialization.
8707 If this is for an explicit set of braces, IMPLICIT is 0.
8708 If it is because the next element belongs at a lower level,
8709 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8712 push_init_level (location_t loc, int implicit,
8713 struct obstack *braced_init_obstack)
8715 struct constructor_stack *p;
8716 tree value = NULL_TREE;
8718 /* Unless this is an explicit brace, we need to preserve previous
8722 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8723 value = find_init_member (constructor_fields, braced_init_obstack);
8724 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8725 value = find_init_member (constructor_index, braced_init_obstack);
8728 p = XNEW (struct constructor_stack);
8729 p->type = constructor_type;
8730 p->fields = constructor_fields;
8731 p->index = constructor_index;
8732 p->max_index = constructor_max_index;
8733 p->unfilled_index = constructor_unfilled_index;
8734 p->unfilled_fields = constructor_unfilled_fields;
8735 p->bit_index = constructor_bit_index;
8736 p->elements = constructor_elements;
8737 p->constant = constructor_constant;
8738 p->simple = constructor_simple;
8739 p->nonconst = constructor_nonconst;
8740 p->erroneous = constructor_erroneous;
8741 p->pending_elts = constructor_pending_elts;
8742 p->depth = constructor_depth;
8743 p->replacement_value.value = NULL_TREE;
8744 p->replacement_value.original_code = ERROR_MARK;
8745 p->replacement_value.original_type = NULL;
8746 p->implicit = implicit;
8748 p->incremental = constructor_incremental;
8749 p->designated = constructor_designated;
8750 p->designator_depth = designator_depth;
8751 p->next = constructor_stack;
8753 constructor_stack = p;
8755 constructor_constant = 1;
8756 constructor_simple = 1;
8757 constructor_nonconst = 0;
8758 constructor_depth = SPELLING_DEPTH ();
8759 constructor_elements = NULL;
8760 constructor_incremental = 1;
8761 /* If the upper initializer is designated, then mark this as
8762 designated too to prevent bogus warnings. */
8763 constructor_designated = p->designated;
8764 constructor_pending_elts = 0;
8767 p->range_stack = constructor_range_stack;
8768 constructor_range_stack = 0;
8769 designator_depth = 0;
8770 designator_erroneous = 0;
8773 /* Don't die if an entire brace-pair level is superfluous
8774 in the containing level. */
8775 if (constructor_type == NULL_TREE)
8777 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8779 /* Don't die if there are extra init elts at the end. */
8780 if (constructor_fields == NULL_TREE)
8781 constructor_type = NULL_TREE;
8784 constructor_type = TREE_TYPE (constructor_fields);
8785 push_member_name (constructor_fields);
8786 constructor_depth++;
8789 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8791 constructor_type = TREE_TYPE (constructor_type);
8792 push_array_bounds (tree_to_uhwi (constructor_index));
8793 constructor_depth++;
8796 if (constructor_type == NULL_TREE)
8798 error_init (loc, "extra brace group at end of initializer");
8799 constructor_fields = NULL_TREE;
8800 constructor_unfilled_fields = NULL_TREE;
8804 if (value && TREE_CODE (value) == CONSTRUCTOR)
8806 constructor_constant = TREE_CONSTANT (value);
8807 constructor_simple = TREE_STATIC (value);
8808 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8809 constructor_elements = CONSTRUCTOR_ELTS (value);
8810 if (!vec_safe_is_empty (constructor_elements)
8811 && (TREE_CODE (constructor_type) == RECORD_TYPE
8812 || TREE_CODE (constructor_type) == ARRAY_TYPE))
8813 set_nonincremental_init (braced_init_obstack);
8818 found_missing_braces = 1;
8819 if (initializer_stack->missing_brace_richloc)
8820 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8824 if (RECORD_OR_UNION_TYPE_P (constructor_type))
8826 constructor_fields = TYPE_FIELDS (constructor_type);
8827 /* Skip any nameless bit fields at the beginning. */
8828 while (constructor_fields != NULL_TREE
8829 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8830 constructor_fields = DECL_CHAIN (constructor_fields);
8832 constructor_unfilled_fields = constructor_fields;
8833 constructor_bit_index = bitsize_zero_node;
8835 else if (gnu_vector_type_p (constructor_type))
8837 /* Vectors are like simple fixed-size arrays. */
8838 constructor_max_index =
8839 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8840 constructor_index = bitsize_int (0);
8841 constructor_unfilled_index = constructor_index;
8843 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8845 if (TYPE_DOMAIN (constructor_type))
8847 constructor_max_index
8848 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8850 /* Detect non-empty initializations of zero-length arrays. */
8851 if (constructor_max_index == NULL_TREE
8852 && TYPE_SIZE (constructor_type))
8853 constructor_max_index = integer_minus_one_node;
8855 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8856 to initialize VLAs will cause a proper error; avoid tree
8857 checking errors as well by setting a safe value. */
8858 if (constructor_max_index
8859 && TREE_CODE (constructor_max_index) != INTEGER_CST)
8860 constructor_max_index = integer_minus_one_node;
8863 = convert (bitsizetype,
8864 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8867 constructor_index = bitsize_zero_node;
8869 constructor_unfilled_index = constructor_index;
8870 if (value && TREE_CODE (value) == STRING_CST)
8872 /* We need to split the char/wchar array into individual
8873 characters, so that we don't have to special case it
8875 set_nonincremental_init_from_string (value, braced_init_obstack);
8880 if (constructor_type != error_mark_node)
8881 warning_init (input_location, 0, "braces around scalar initializer");
8882 constructor_fields = constructor_type;
8883 constructor_unfilled_fields = constructor_type;
8887 /* At the end of an implicit or explicit brace level,
8888 finish up that level of constructor. If a single expression
8889 with redundant braces initialized that level, return the
8890 c_expr structure for that expression. Otherwise, the original_code
8891 element is set to ERROR_MARK.
8892 If we were outputting the elements as they are read, return 0 as the value
8893 from inner levels (process_init_element ignores that),
8894 but return error_mark_node as the value from the outermost level
8895 (that's what we want to put in DECL_INITIAL).
8896 Otherwise, return a CONSTRUCTOR expression as the value. */
8899 pop_init_level (location_t loc, int implicit,
8900 struct obstack *braced_init_obstack,
8901 location_t insert_before)
8903 struct constructor_stack *p;
8905 ret.value = NULL_TREE;
8906 ret.original_code = ERROR_MARK;
8907 ret.original_type = NULL;
8911 /* When we come to an explicit close brace,
8912 pop any inner levels that didn't have explicit braces. */
8913 while (constructor_stack->implicit)
8914 process_init_element (input_location,
8915 pop_init_level (loc, 1, braced_init_obstack,
8917 true, braced_init_obstack);
8918 gcc_assert (!constructor_range_stack);
8921 if (initializer_stack->missing_brace_richloc)
8922 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8923 (insert_before, "}");
8925 /* Now output all pending elements. */
8926 constructor_incremental = 1;
8927 output_pending_init_elements (1, braced_init_obstack);
8929 p = constructor_stack;
8931 /* Error for initializing a flexible array member, or a zero-length
8932 array member in an inappropriate context. */
8933 if (constructor_type && constructor_fields
8934 && TREE_CODE (constructor_type) == ARRAY_TYPE
8935 && TYPE_DOMAIN (constructor_type)
8936 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8938 /* Silently discard empty initializations. The parser will
8939 already have pedwarned for empty brackets. */
8940 if (integer_zerop (constructor_unfilled_index))
8941 constructor_type = NULL_TREE;
8944 gcc_assert (!TYPE_SIZE (constructor_type));
8946 if (constructor_depth > 2)
8947 error_init (loc, "initialization of flexible array member in a nested context");
8949 pedwarn_init (loc, OPT_Wpedantic,
8950 "initialization of a flexible array member");
8952 /* We have already issued an error message for the existence
8953 of a flexible array member not at the end of the structure.
8954 Discard the initializer so that we do not die later. */
8955 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8956 constructor_type = NULL_TREE;
8960 switch (vec_safe_length (constructor_elements))
8963 /* Initialization with { } counts as zeroinit. */
8964 constructor_zeroinit = 1;
8967 /* This might be zeroinit as well. */
8968 if (integer_zerop ((*constructor_elements)[0].value))
8969 constructor_zeroinit = 1;
8972 /* If the constructor has more than one element, it can't be { 0 }. */
8973 constructor_zeroinit = 0;
8977 /* Warn when some structs are initialized with direct aggregation. */
8978 if (!implicit && found_missing_braces && warn_missing_braces
8979 && !constructor_zeroinit)
8981 gcc_assert (initializer_stack->missing_brace_richloc);
8982 warning_at (initializer_stack->missing_brace_richloc,
8983 OPT_Wmissing_braces,
8984 "missing braces around initializer");
8987 /* Warn when some struct elements are implicitly initialized to zero. */
8988 if (warn_missing_field_initializers
8990 && TREE_CODE (constructor_type) == RECORD_TYPE
8991 && constructor_unfilled_fields)
8993 /* Do not warn for flexible array members or zero-length arrays. */
8994 while (constructor_unfilled_fields
8995 && (!DECL_SIZE (constructor_unfilled_fields)
8996 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8997 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8999 if (constructor_unfilled_fields
9000 /* Do not warn if this level of the initializer uses member
9001 designators; it is likely to be deliberate. */
9002 && !constructor_designated
9003 /* Do not warn about initializing with { 0 } or with { }. */
9004 && !constructor_zeroinit)
9006 if (warning_at (input_location, OPT_Wmissing_field_initializers,
9007 "missing initializer for field %qD of %qT",
9008 constructor_unfilled_fields,
9010 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9011 "%qD declared here", constructor_unfilled_fields);
9015 /* Pad out the end of the structure. */
9016 if (p->replacement_value.value)
9017 /* If this closes a superfluous brace pair,
9018 just pass out the element between them. */
9019 ret = p->replacement_value;
9020 else if (constructor_type == NULL_TREE)
9022 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9023 && TREE_CODE (constructor_type) != ARRAY_TYPE
9024 && !gnu_vector_type_p (constructor_type))
9026 /* A nonincremental scalar initializer--just return
9027 the element, after verifying there is just one. */
9028 if (vec_safe_is_empty (constructor_elements))
9030 if (!constructor_erroneous && constructor_type != error_mark_node)
9031 error_init (loc, "empty scalar initializer");
9032 ret.value = error_mark_node;
9034 else if (vec_safe_length (constructor_elements) != 1)
9036 error_init (loc, "extra elements in scalar initializer");
9037 ret.value = (*constructor_elements)[0].value;
9040 ret.value = (*constructor_elements)[0].value;
9044 if (constructor_erroneous)
9045 ret.value = error_mark_node;
9048 ret.value = build_constructor (constructor_type,
9049 constructor_elements);
9050 if (constructor_constant)
9051 TREE_CONSTANT (ret.value) = 1;
9052 if (constructor_constant && constructor_simple)
9053 TREE_STATIC (ret.value) = 1;
9054 if (constructor_nonconst)
9055 CONSTRUCTOR_NON_CONST (ret.value) = 1;
9059 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9061 if (constructor_nonconst)
9062 ret.original_code = C_MAYBE_CONST_EXPR;
9063 else if (ret.original_code == C_MAYBE_CONST_EXPR)
9064 ret.original_code = ERROR_MARK;
9067 constructor_type = p->type;
9068 constructor_fields = p->fields;
9069 constructor_index = p->index;
9070 constructor_max_index = p->max_index;
9071 constructor_unfilled_index = p->unfilled_index;
9072 constructor_unfilled_fields = p->unfilled_fields;
9073 constructor_bit_index = p->bit_index;
9074 constructor_elements = p->elements;
9075 constructor_constant = p->constant;
9076 constructor_simple = p->simple;
9077 constructor_nonconst = p->nonconst;
9078 constructor_erroneous = p->erroneous;
9079 constructor_incremental = p->incremental;
9080 constructor_designated = p->designated;
9081 designator_depth = p->designator_depth;
9082 constructor_pending_elts = p->pending_elts;
9083 constructor_depth = p->depth;
9085 constructor_range_stack = p->range_stack;
9086 RESTORE_SPELLING_DEPTH (constructor_depth);
9088 constructor_stack = p->next;
9091 if (ret.value == NULL_TREE && constructor_stack == 0)
9092 ret.value = error_mark_node;
9096 /* Common handling for both array range and field name designators.
9097 ARRAY argument is nonzero for array ranges. Returns false for success. */
9100 set_designator (location_t loc, bool array,
9101 struct obstack *braced_init_obstack)
9104 enum tree_code subcode;
9106 /* Don't die if an entire brace-pair level is superfluous
9107 in the containing level, or for an erroneous type. */
9108 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9111 /* If there were errors in this designator list already, bail out
9113 if (designator_erroneous)
9116 /* Likewise for an initializer for a variable-size type. Those are
9117 diagnosed in digest_init. */
9118 if (COMPLETE_TYPE_P (constructor_type)
9119 && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9122 if (!designator_depth)
9124 gcc_assert (!constructor_range_stack);
9126 /* Designator list starts at the level of closest explicit
9128 while (constructor_stack->implicit)
9129 process_init_element (input_location,
9130 pop_init_level (loc, 1, braced_init_obstack,
9131 last_init_list_comma),
9132 true, braced_init_obstack);
9133 constructor_designated = 1;
9137 switch (TREE_CODE (constructor_type))
9141 subtype = TREE_TYPE (constructor_fields);
9142 if (subtype != error_mark_node)
9143 subtype = TYPE_MAIN_VARIANT (subtype);
9146 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9152 subcode = TREE_CODE (subtype);
9153 if (array && subcode != ARRAY_TYPE)
9155 error_init (loc, "array index in non-array initializer");
9158 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9160 error_init (loc, "field name not in record or union initializer");
9164 constructor_designated = 1;
9165 finish_implicit_inits (loc, braced_init_obstack);
9166 push_init_level (loc, 2, braced_init_obstack);
9170 /* If there are range designators in designator list, push a new designator
9171 to constructor_range_stack. RANGE_END is end of such stack range or
9172 NULL_TREE if there is no range designator at this level. */
9175 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9177 struct constructor_range_stack *p;
9179 p = (struct constructor_range_stack *)
9180 obstack_alloc (braced_init_obstack,
9181 sizeof (struct constructor_range_stack));
9182 p->prev = constructor_range_stack;
9184 p->fields = constructor_fields;
9185 p->range_start = constructor_index;
9186 p->index = constructor_index;
9187 p->stack = constructor_stack;
9188 p->range_end = range_end;
9189 if (constructor_range_stack)
9190 constructor_range_stack->next = p;
9191 constructor_range_stack = p;
9194 /* Within an array initializer, specify the next index to be initialized.
9195 FIRST is that index. If LAST is nonzero, then initialize a range
9196 of indices, running from FIRST through LAST. */
9199 set_init_index (location_t loc, tree first, tree last,
9200 struct obstack *braced_init_obstack)
9202 if (set_designator (loc, true, braced_init_obstack))
9205 designator_erroneous = 1;
9207 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9208 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9210 error_init (loc, "array index in initializer not of integer type");
9214 if (TREE_CODE (first) != INTEGER_CST)
9216 first = c_fully_fold (first, false, NULL);
9217 if (TREE_CODE (first) == INTEGER_CST)
9218 pedwarn_init (loc, OPT_Wpedantic,
9219 "array index in initializer is not "
9220 "an integer constant expression");
9223 if (last && TREE_CODE (last) != INTEGER_CST)
9225 last = c_fully_fold (last, false, NULL);
9226 if (TREE_CODE (last) == INTEGER_CST)
9227 pedwarn_init (loc, OPT_Wpedantic,
9228 "array index in initializer is not "
9229 "an integer constant expression");
9232 if (TREE_CODE (first) != INTEGER_CST)
9233 error_init (loc, "nonconstant array index in initializer");
9234 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9235 error_init (loc, "nonconstant array index in initializer");
9236 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9237 error_init (loc, "array index in non-array initializer");
9238 else if (tree_int_cst_sgn (first) == -1)
9239 error_init (loc, "array index in initializer exceeds array bounds");
9240 else if (constructor_max_index
9241 && tree_int_cst_lt (constructor_max_index, first))
9242 error_init (loc, "array index in initializer exceeds array bounds");
9245 constant_expression_warning (first);
9247 constant_expression_warning (last);
9248 constructor_index = convert (bitsizetype, first);
9249 if (tree_int_cst_lt (constructor_index, first))
9251 constructor_index = copy_node (constructor_index);
9252 TREE_OVERFLOW (constructor_index) = 1;
9257 if (tree_int_cst_equal (first, last))
9259 else if (tree_int_cst_lt (last, first))
9261 error_init (loc, "empty index range in initializer");
9266 last = convert (bitsizetype, last);
9267 if (constructor_max_index != NULL_TREE
9268 && tree_int_cst_lt (constructor_max_index, last))
9270 error_init (loc, "array index range in initializer exceeds "
9278 designator_erroneous = 0;
9279 if (constructor_range_stack || last)
9280 push_range_stack (last, braced_init_obstack);
9284 /* Within a struct initializer, specify the next field to be initialized. */
9287 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9288 struct obstack *braced_init_obstack)
9292 if (set_designator (loc, false, braced_init_obstack))
9295 designator_erroneous = 1;
9297 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9299 error_init (loc, "field name not in record or union initializer");
9303 field = lookup_field (constructor_type, fieldname);
9305 if (field == NULL_TREE)
9307 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9310 gcc_rich_location rich_loc (fieldname_loc);
9311 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9312 error_at (&rich_loc,
9313 "%qT has no member named %qE; did you mean %qE?",
9314 constructor_type, fieldname, guessed_id);
9317 error_at (fieldname_loc, "%qT has no member named %qE",
9318 constructor_type, fieldname);
9323 constructor_fields = TREE_VALUE (field);
9325 designator_erroneous = 0;
9326 if (constructor_range_stack)
9327 push_range_stack (NULL_TREE, braced_init_obstack);
9328 field = TREE_CHAIN (field);
9331 if (set_designator (loc, false, braced_init_obstack))
9335 while (field != NULL_TREE);
9338 /* Add a new initializer to the tree of pending initializers. PURPOSE
9339 identifies the initializer, either array index or field in a structure.
9340 VALUE is the value of that index or field. If ORIGTYPE is not
9341 NULL_TREE, it is the original type of VALUE.
9343 IMPLICIT is true if value comes from pop_init_level (1),
9344 the new initializer has been merged with the existing one
9345 and thus no warnings should be emitted about overriding an
9346 existing initializer. */
9349 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9350 bool implicit, struct obstack *braced_init_obstack)
9352 struct init_node *p, **q, *r;
9354 q = &constructor_pending_elts;
9357 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9362 if (tree_int_cst_lt (purpose, p->purpose))
9364 else if (tree_int_cst_lt (p->purpose, purpose))
9370 if (TREE_SIDE_EFFECTS (p->value))
9371 warning_init (loc, OPT_Woverride_init_side_effects,
9372 "initialized field with side-effects "
9374 else if (warn_override_init)
9375 warning_init (loc, OPT_Woverride_init,
9376 "initialized field overwritten");
9379 p->origtype = origtype;
9388 bitpos = bit_position (purpose);
9392 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9394 else if (p->purpose != purpose)
9400 if (TREE_SIDE_EFFECTS (p->value))
9401 warning_init (loc, OPT_Woverride_init_side_effects,
9402 "initialized field with side-effects "
9404 else if (warn_override_init)
9405 warning_init (loc, OPT_Woverride_init,
9406 "initialized field overwritten");
9409 p->origtype = origtype;
9415 r = (struct init_node *) obstack_alloc (braced_init_obstack,
9416 sizeof (struct init_node));
9417 r->purpose = purpose;
9419 r->origtype = origtype;
9429 struct init_node *s;
9433 if (p->balance == 0)
9435 else if (p->balance < 0)
9442 p->left->parent = p;
9459 constructor_pending_elts = r;
9464 struct init_node *t = r->right;
9468 r->right->parent = r;
9473 p->left->parent = p;
9476 p->balance = t->balance < 0;
9477 r->balance = -(t->balance > 0);
9492 constructor_pending_elts = t;
9498 /* p->balance == +1; growth of left side balances the node. */
9503 else /* r == p->right */
9505 if (p->balance == 0)
9506 /* Growth propagation from right side. */
9508 else if (p->balance > 0)
9515 p->right->parent = p;
9532 constructor_pending_elts = r;
9534 else /* r->balance == -1 */
9537 struct init_node *t = r->left;
9541 r->left->parent = r;
9546 p->right->parent = p;
9549 r->balance = (t->balance < 0);
9550 p->balance = -(t->balance > 0);
9565 constructor_pending_elts = t;
9571 /* p->balance == -1; growth of right side balances the node. */
9582 /* Build AVL tree from a sorted chain. */
9585 set_nonincremental_init (struct obstack * braced_init_obstack)
9587 unsigned HOST_WIDE_INT ix;
9590 if (TREE_CODE (constructor_type) != RECORD_TYPE
9591 && TREE_CODE (constructor_type) != ARRAY_TYPE)
9594 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9595 add_pending_init (input_location, index, value, NULL_TREE, true,
9596 braced_init_obstack);
9597 constructor_elements = NULL;
9598 if (TREE_CODE (constructor_type) == RECORD_TYPE)
9600 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9601 /* Skip any nameless bit fields at the beginning. */
9602 while (constructor_unfilled_fields != NULL_TREE
9603 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9604 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9607 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9609 if (TYPE_DOMAIN (constructor_type))
9610 constructor_unfilled_index
9611 = convert (bitsizetype,
9612 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9614 constructor_unfilled_index = bitsize_zero_node;
9616 constructor_incremental = 0;
9619 /* Build AVL tree from a string constant. */
9622 set_nonincremental_init_from_string (tree str,
9623 struct obstack * braced_init_obstack)
9625 tree value, purpose, type;
9626 HOST_WIDE_INT val[2];
9627 const char *p, *end;
9628 int byte, wchar_bytes, charwidth, bitpos;
9630 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9632 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9633 charwidth = TYPE_PRECISION (char_type_node);
9634 gcc_assert ((size_t) wchar_bytes * charwidth
9635 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9636 type = TREE_TYPE (constructor_type);
9637 p = TREE_STRING_POINTER (str);
9638 end = p + TREE_STRING_LENGTH (str);
9640 for (purpose = bitsize_zero_node;
9642 && !(constructor_max_index
9643 && tree_int_cst_lt (constructor_max_index, purpose));
9644 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9646 if (wchar_bytes == 1)
9648 val[0] = (unsigned char) *p++;
9655 for (byte = 0; byte < wchar_bytes; byte++)
9657 if (BYTES_BIG_ENDIAN)
9658 bitpos = (wchar_bytes - byte - 1) * charwidth;
9660 bitpos = byte * charwidth;
9661 val[bitpos / HOST_BITS_PER_WIDE_INT]
9662 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9663 << (bitpos % HOST_BITS_PER_WIDE_INT);
9667 if (!TYPE_UNSIGNED (type))
9669 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9670 if (bitpos < HOST_BITS_PER_WIDE_INT)
9672 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9674 val[0] |= HOST_WIDE_INT_M1U << bitpos;
9678 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9683 else if (val[1] & (HOST_WIDE_INT_1
9684 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9685 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9688 value = wide_int_to_tree (type,
9689 wide_int::from_array (val, 2,
9690 HOST_BITS_PER_WIDE_INT * 2));
9691 add_pending_init (input_location, purpose, value, NULL_TREE, true,
9692 braced_init_obstack);
9695 constructor_incremental = 0;
9698 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9699 not initialized yet. */
9702 find_init_member (tree field, struct obstack * braced_init_obstack)
9704 struct init_node *p;
9706 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9708 if (constructor_incremental
9709 && tree_int_cst_lt (field, constructor_unfilled_index))
9710 set_nonincremental_init (braced_init_obstack);
9712 p = constructor_pending_elts;
9715 if (tree_int_cst_lt (field, p->purpose))
9717 else if (tree_int_cst_lt (p->purpose, field))
9723 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9725 tree bitpos = bit_position (field);
9727 if (constructor_incremental
9728 && (!constructor_unfilled_fields
9729 || tree_int_cst_lt (bitpos,
9730 bit_position (constructor_unfilled_fields))))
9731 set_nonincremental_init (braced_init_obstack);
9733 p = constructor_pending_elts;
9736 if (field == p->purpose)
9738 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9744 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9746 if (!vec_safe_is_empty (constructor_elements)
9747 && (constructor_elements->last ().index == field))
9748 return constructor_elements->last ().value;
9753 /* "Output" the next constructor element.
9754 At top level, really output it to assembler code now.
9755 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9756 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9757 TYPE is the data type that the containing data type wants here.
9758 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9759 If VALUE is a string constant, STRICT_STRING is true if it is
9760 unparenthesized or we should not warn here for it being parenthesized.
9761 For other types of VALUE, STRICT_STRING is not used.
9763 PENDING if true means output pending elements that belong
9764 right after this element. (PENDING is normally true;
9765 it is false while outputting pending elements, to avoid recursion.)
9767 IMPLICIT is true if value comes from pop_init_level (1),
9768 the new initializer has been merged with the existing one
9769 and thus no warnings should be emitted about overriding an
9770 existing initializer. */
9773 output_init_element (location_t loc, tree value, tree origtype,
9774 bool strict_string, tree type, tree field, bool pending,
9775 bool implicit, struct obstack * braced_init_obstack)
9777 tree semantic_type = NULL_TREE;
9778 bool maybe_const = true;
9781 if (type == error_mark_node || value == error_mark_node)
9783 constructor_erroneous = 1;
9786 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9787 && (TREE_CODE (value) == STRING_CST
9788 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9789 && !(TREE_CODE (value) == STRING_CST
9790 && TREE_CODE (type) == ARRAY_TYPE
9791 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9792 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9793 TYPE_MAIN_VARIANT (type)))
9794 value = array_to_pointer_conversion (input_location, value);
9796 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9797 && require_constant_value && pending)
9799 /* As an extension, allow initializing objects with static storage
9800 duration with compound literals (which are then treated just as
9801 the brace enclosed list they contain). */
9803 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9805 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9806 value = DECL_INITIAL (decl);
9809 npc = null_pointer_constant_p (value);
9810 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9812 semantic_type = TREE_TYPE (value);
9813 value = TREE_OPERAND (value, 0);
9815 value = c_fully_fold (value, require_constant_value, &maybe_const);
9817 if (value == error_mark_node)
9818 constructor_erroneous = 1;
9819 else if (!TREE_CONSTANT (value))
9820 constructor_constant = 0;
9821 else if (!initializer_constant_valid_p (value,
9823 AGGREGATE_TYPE_P (constructor_type)
9824 && TYPE_REVERSE_STORAGE_ORDER
9826 || (RECORD_OR_UNION_TYPE_P (constructor_type)
9827 && DECL_C_BIT_FIELD (field)
9828 && TREE_CODE (value) != INTEGER_CST))
9829 constructor_simple = 0;
9831 constructor_nonconst = 1;
9833 /* Digest the initializer and issue any errors about incompatible
9834 types before issuing errors about non-constant initializers. */
9835 tree new_value = value;
9837 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9838 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9839 require_constant_value);
9840 if (new_value == error_mark_node)
9842 constructor_erroneous = 1;
9845 if (require_constant_value || require_constant_elements)
9846 constant_expression_warning (new_value);
9848 /* Proceed to check the constness of the original initializer. */
9849 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9851 if (require_constant_value)
9853 error_init (loc, "initializer element is not constant");
9854 value = error_mark_node;
9856 else if (require_constant_elements)
9857 pedwarn (loc, OPT_Wpedantic,
9858 "initializer element is not computable at load time");
9860 else if (!maybe_const
9861 && (require_constant_value || require_constant_elements))
9862 pedwarn_init (loc, OPT_Wpedantic,
9863 "initializer element is not a constant expression");
9865 /* Issue -Wc++-compat warnings about initializing a bitfield with
9868 && field != NULL_TREE
9869 && TREE_CODE (field) == FIELD_DECL
9870 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9871 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9872 != TYPE_MAIN_VARIANT (type))
9873 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9875 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9876 if (checktype != error_mark_node
9877 && (TYPE_MAIN_VARIANT (checktype)
9878 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9879 warning_init (loc, OPT_Wc___compat,
9880 "enum conversion in initialization is invalid in C++");
9883 /* If this field is empty and does not have side effects (and is not at
9884 the end of structure), don't do anything other than checking the
9887 && (TREE_TYPE (field) == error_mark_node
9888 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9889 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9890 && !TREE_SIDE_EFFECTS (new_value)
9891 && (TREE_CODE (constructor_type) == ARRAY_TYPE
9892 || DECL_CHAIN (field)))))
9895 /* Finally, set VALUE to the initializer value digested above. */
9898 /* If this element doesn't come next in sequence,
9899 put it on constructor_pending_elts. */
9900 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9901 && (!constructor_incremental
9902 || !tree_int_cst_equal (field, constructor_unfilled_index)))
9904 if (constructor_incremental
9905 && tree_int_cst_lt (field, constructor_unfilled_index))
9906 set_nonincremental_init (braced_init_obstack);
9908 add_pending_init (loc, field, value, origtype, implicit,
9909 braced_init_obstack);
9912 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9913 && (!constructor_incremental
9914 || field != constructor_unfilled_fields))
9916 /* We do this for records but not for unions. In a union,
9917 no matter which field is specified, it can be initialized
9918 right away since it starts at the beginning of the union. */
9919 if (constructor_incremental)
9921 if (!constructor_unfilled_fields)
9922 set_nonincremental_init (braced_init_obstack);
9925 tree bitpos, unfillpos;
9927 bitpos = bit_position (field);
9928 unfillpos = bit_position (constructor_unfilled_fields);
9930 if (tree_int_cst_lt (bitpos, unfillpos))
9931 set_nonincremental_init (braced_init_obstack);
9935 add_pending_init (loc, field, value, origtype, implicit,
9936 braced_init_obstack);
9939 else if (TREE_CODE (constructor_type) == UNION_TYPE
9940 && !vec_safe_is_empty (constructor_elements))
9944 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9945 warning_init (loc, OPT_Woverride_init_side_effects,
9946 "initialized field with side-effects overwritten");
9947 else if (warn_override_init)
9948 warning_init (loc, OPT_Woverride_init,
9949 "initialized field overwritten");
9952 /* We can have just one union field set. */
9953 constructor_elements = NULL;
9956 /* Otherwise, output this element either to
9957 constructor_elements or to the assembler file. */
9959 constructor_elt celt = {field, value};
9960 vec_safe_push (constructor_elements, celt);
9962 /* Advance the variable that indicates sequential elements output. */
9963 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9964 constructor_unfilled_index
9965 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9967 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9969 constructor_unfilled_fields
9970 = DECL_CHAIN (constructor_unfilled_fields);
9972 /* Skip any nameless bit fields. */
9973 while (constructor_unfilled_fields != NULL_TREE
9974 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9975 constructor_unfilled_fields =
9976 DECL_CHAIN (constructor_unfilled_fields);
9978 else if (TREE_CODE (constructor_type) == UNION_TYPE)
9979 constructor_unfilled_fields = NULL_TREE;
9981 /* Now output any pending elements which have become next. */
9983 output_pending_init_elements (0, braced_init_obstack);
9986 /* For two FIELD_DECLs in the same chain, return -1 if field1
9987 comes before field2, 1 if field1 comes after field2 and
9988 0 if field1 == field2. */
9991 init_field_decl_cmp (tree field1, tree field2)
9993 if (field1 == field2)
9996 tree bitpos1 = bit_position (field1);
9997 tree bitpos2 = bit_position (field2);
9998 if (tree_int_cst_equal (bitpos1, bitpos2))
10000 /* If one of the fields has non-zero bitsize, then that
10001 field must be the last one in a sequence of zero
10002 sized fields, fields after it will have bigger
10004 if (TREE_TYPE (field1) != error_mark_node
10005 && COMPLETE_TYPE_P (TREE_TYPE (field1))
10006 && integer_nonzerop (TREE_TYPE (field1)))
10008 if (TREE_TYPE (field2) != error_mark_node
10009 && COMPLETE_TYPE_P (TREE_TYPE (field2))
10010 && integer_nonzerop (TREE_TYPE (field2)))
10012 /* Otherwise, fallback to DECL_CHAIN walk to find out
10013 which field comes earlier. Walk chains of both
10014 fields, so that if field1 and field2 are close to each
10015 other in either order, it is found soon even for large
10016 sequences of zero sized fields. */
10017 tree f1 = field1, f2 = field2;
10020 f1 = DECL_CHAIN (f1);
10021 f2 = DECL_CHAIN (f2);
10022 if (f1 == NULL_TREE)
10027 if (f2 == NULL_TREE)
10033 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10035 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10039 else if (tree_int_cst_lt (bitpos1, bitpos2))
10045 /* Output any pending elements which have become next.
10046 As we output elements, constructor_unfilled_{fields,index}
10047 advances, which may cause other elements to become next;
10048 if so, they too are output.
10050 If ALL is 0, we return when there are
10051 no more pending elements to output now.
10053 If ALL is 1, we output space as necessary so that
10054 we can output all the pending elements. */
10056 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10058 struct init_node *elt = constructor_pending_elts;
10063 /* Look through the whole pending tree.
10064 If we find an element that should be output now,
10065 output it. Otherwise, set NEXT to the element
10066 that comes first among those still pending. */
10071 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10073 if (tree_int_cst_equal (elt->purpose,
10074 constructor_unfilled_index))
10075 output_init_element (input_location, elt->value, elt->origtype,
10076 true, TREE_TYPE (constructor_type),
10077 constructor_unfilled_index, false, false,
10078 braced_init_obstack);
10079 else if (tree_int_cst_lt (constructor_unfilled_index,
10082 /* Advance to the next smaller node. */
10087 /* We have reached the smallest node bigger than the
10088 current unfilled index. Fill the space first. */
10089 next = elt->purpose;
10095 /* Advance to the next bigger node. */
10100 /* We have reached the biggest node in a subtree. Find
10101 the parent of it, which is the next bigger node. */
10102 while (elt->parent && elt->parent->right == elt)
10105 if (elt && tree_int_cst_lt (constructor_unfilled_index,
10108 next = elt->purpose;
10114 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10116 /* If the current record is complete we are done. */
10117 if (constructor_unfilled_fields == NULL_TREE)
10120 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10123 output_init_element (input_location, elt->value, elt->origtype,
10124 true, TREE_TYPE (elt->purpose),
10125 elt->purpose, false, false,
10126 braced_init_obstack);
10129 /* Advance to the next smaller node. */
10134 /* We have reached the smallest node bigger than the
10135 current unfilled field. Fill the space first. */
10136 next = elt->purpose;
10142 /* Advance to the next bigger node. */
10147 /* We have reached the biggest node in a subtree. Find
10148 the parent of it, which is the next bigger node. */
10149 while (elt->parent && elt->parent->right == elt)
10153 && init_field_decl_cmp (constructor_unfilled_fields,
10156 next = elt->purpose;
10164 /* Ordinarily return, but not if we want to output all
10165 and there are elements left. */
10166 if (!(all && next != NULL_TREE))
10169 /* If it's not incremental, just skip over the gap, so that after
10170 jumping to retry we will output the next successive element. */
10171 if (RECORD_OR_UNION_TYPE_P (constructor_type))
10172 constructor_unfilled_fields = next;
10173 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10174 constructor_unfilled_index = next;
10176 /* ELT now points to the node in the pending tree with the next
10177 initializer to output. */
10181 /* Expression VALUE coincides with the start of type TYPE in a braced
10182 initializer. Return true if we should treat VALUE as initializing
10183 the first element of TYPE, false if we should treat it as initializing
10186 If the initializer is clearly invalid, the question becomes:
10187 which choice gives the best error message? */
10190 initialize_elementwise_p (tree type, tree value)
10192 if (type == error_mark_node || value == error_mark_node)
10195 gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10197 tree value_type = TREE_TYPE (value);
10198 if (value_type == error_mark_node)
10201 /* GNU vectors can be initialized elementwise. However, treat any
10202 kind of vector value as initializing the vector type as a whole,
10203 regardless of whether the value is a GNU vector. Such initializers
10204 are valid if and only if they would have been valid in a non-braced
10209 so recursing into the vector type would be at best confusing or at
10210 worst wrong. For example, when -flax-vector-conversions is in effect,
10211 it's possible to initialize a V8HI from a V4SI, even though the vectors
10212 have different element types and different numbers of elements. */
10213 if (gnu_vector_type_p (type))
10214 return !VECTOR_TYPE_P (value_type);
10216 if (AGGREGATE_TYPE_P (type))
10217 return type != TYPE_MAIN_VARIANT (value_type);
10222 /* Add one non-braced element to the current constructor level.
10223 This adjusts the current position within the constructor's type.
10224 This may also start or terminate implicit levels
10225 to handle a partly-braced initializer.
10227 Once this has found the correct level for the new element,
10228 it calls output_init_element.
10230 IMPLICIT is true if value comes from pop_init_level (1),
10231 the new initializer has been merged with the existing one
10232 and thus no warnings should be emitted about overriding an
10233 existing initializer. */
10236 process_init_element (location_t loc, struct c_expr value, bool implicit,
10237 struct obstack * braced_init_obstack)
10239 tree orig_value = value.value;
10241 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10242 bool strict_string = value.original_code == STRING_CST;
10243 bool was_designated = designator_depth != 0;
10245 designator_depth = 0;
10246 designator_erroneous = 0;
10248 if (!implicit && value.value && !integer_zerop (value.value))
10249 constructor_zeroinit = 0;
10251 /* Handle superfluous braces around string cst as in
10252 char x[] = {"foo"}; */
10254 && constructor_type
10256 && TREE_CODE (constructor_type) == ARRAY_TYPE
10257 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10258 && integer_zerop (constructor_unfilled_index))
10260 if (constructor_stack->replacement_value.value)
10261 error_init (loc, "excess elements in %<char%> array initializer");
10262 constructor_stack->replacement_value = value;
10266 if (constructor_stack->replacement_value.value != NULL_TREE)
10268 error_init (loc, "excess elements in struct initializer");
10272 /* Ignore elements of a brace group if it is entirely superfluous
10273 and has already been diagnosed, or if the type is erroneous. */
10274 if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10277 /* Ignore elements of an initializer for a variable-size type.
10278 Those are diagnosed in digest_init. */
10279 if (COMPLETE_TYPE_P (constructor_type)
10280 && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10283 if (!implicit && warn_designated_init && !was_designated
10284 && TREE_CODE (constructor_type) == RECORD_TYPE
10285 && lookup_attribute ("designated_init",
10286 TYPE_ATTRIBUTES (constructor_type)))
10288 OPT_Wdesignated_init,
10289 "positional initialization of field "
10290 "in %<struct%> declared with %<designated_init%> attribute");
10292 /* If we've exhausted any levels that didn't have braces,
10294 while (constructor_stack->implicit)
10296 if (RECORD_OR_UNION_TYPE_P (constructor_type)
10297 && constructor_fields == NULL_TREE)
10298 process_init_element (loc,
10299 pop_init_level (loc, 1, braced_init_obstack,
10300 last_init_list_comma),
10301 true, braced_init_obstack);
10302 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10303 || gnu_vector_type_p (constructor_type))
10304 && constructor_max_index
10305 && tree_int_cst_lt (constructor_max_index,
10306 constructor_index))
10307 process_init_element (loc,
10308 pop_init_level (loc, 1, braced_init_obstack,
10309 last_init_list_comma),
10310 true, braced_init_obstack);
10315 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10316 if (constructor_range_stack)
10318 /* If value is a compound literal and we'll be just using its
10319 content, don't put it into a SAVE_EXPR. */
10320 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10321 || !require_constant_value)
10323 tree semantic_type = NULL_TREE;
10324 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10326 semantic_type = TREE_TYPE (value.value);
10327 value.value = TREE_OPERAND (value.value, 0);
10329 value.value = save_expr (value.value);
10331 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10338 if (TREE_CODE (constructor_type) == RECORD_TYPE)
10341 enum tree_code fieldcode;
10343 if (constructor_fields == NULL_TREE)
10345 pedwarn_init (loc, 0, "excess elements in struct initializer");
10349 fieldtype = TREE_TYPE (constructor_fields);
10350 if (fieldtype != error_mark_node)
10351 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10352 fieldcode = TREE_CODE (fieldtype);
10354 /* Error for non-static initialization of a flexible array member. */
10355 if (fieldcode == ARRAY_TYPE
10356 && !require_constant_value
10357 && TYPE_SIZE (fieldtype) == NULL_TREE
10358 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10360 error_init (loc, "non-static initialization of a flexible "
10365 /* Error for initialization of a flexible array member with
10366 a string constant if the structure is in an array. E.g.:
10367 struct S { int x; char y[]; };
10368 struct S s[] = { { 1, "foo" } };
10371 && fieldcode == ARRAY_TYPE
10372 && constructor_depth > 1
10373 && TYPE_SIZE (fieldtype) == NULL_TREE
10374 && DECL_CHAIN (constructor_fields) == NULL_TREE)
10376 bool in_array_p = false;
10377 for (struct constructor_stack *p = constructor_stack;
10378 p && p->type; p = p->next)
10379 if (TREE_CODE (p->type) == ARRAY_TYPE)
10386 error_init (loc, "initialization of flexible array "
10387 "member in a nested context");
10392 /* Accept a string constant to initialize a subarray. */
10393 if (value.value != NULL_TREE
10394 && fieldcode == ARRAY_TYPE
10395 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10397 value.value = orig_value;
10398 /* Otherwise, if we have come to a subaggregate,
10399 and we don't have an element of its type, push into it. */
10400 else if (value.value != NULL_TREE
10401 && initialize_elementwise_p (fieldtype, value.value))
10403 push_init_level (loc, 1, braced_init_obstack);
10409 push_member_name (constructor_fields);
10410 output_init_element (loc, value.value, value.original_type,
10411 strict_string, fieldtype,
10412 constructor_fields, true, implicit,
10413 braced_init_obstack);
10414 RESTORE_SPELLING_DEPTH (constructor_depth);
10417 /* Do the bookkeeping for an element that was
10418 directly output as a constructor. */
10420 /* For a record, keep track of end position of last field. */
10421 if (DECL_SIZE (constructor_fields))
10422 constructor_bit_index
10423 = size_binop_loc (input_location, PLUS_EXPR,
10424 bit_position (constructor_fields),
10425 DECL_SIZE (constructor_fields));
10427 /* If the current field was the first one not yet written out,
10428 it isn't now, so update. */
10429 if (constructor_unfilled_fields == constructor_fields)
10431 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10432 /* Skip any nameless bit fields. */
10433 while (constructor_unfilled_fields != 0
10434 && (DECL_UNNAMED_BIT_FIELD
10435 (constructor_unfilled_fields)))
10436 constructor_unfilled_fields =
10437 DECL_CHAIN (constructor_unfilled_fields);
10441 constructor_fields = DECL_CHAIN (constructor_fields);
10442 /* Skip any nameless bit fields at the beginning. */
10443 while (constructor_fields != NULL_TREE
10444 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10445 constructor_fields = DECL_CHAIN (constructor_fields);
10447 else if (TREE_CODE (constructor_type) == UNION_TYPE)
10450 enum tree_code fieldcode;
10452 if (constructor_fields == NULL_TREE)
10454 pedwarn_init (loc, 0,
10455 "excess elements in union initializer");
10459 fieldtype = TREE_TYPE (constructor_fields);
10460 if (fieldtype != error_mark_node)
10461 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10462 fieldcode = TREE_CODE (fieldtype);
10464 /* Warn that traditional C rejects initialization of unions.
10465 We skip the warning if the value is zero. This is done
10466 under the assumption that the zero initializer in user
10467 code appears conditioned on e.g. __STDC__ to avoid
10468 "missing initializer" warnings and relies on default
10469 initialization to zero in the traditional C case.
10470 We also skip the warning if the initializer is designated,
10471 again on the assumption that this must be conditional on
10472 __STDC__ anyway (and we've already complained about the
10473 member-designator already). */
10474 if (!in_system_header_at (input_location) && !constructor_designated
10475 && !(value.value && (integer_zerop (value.value)
10476 || real_zerop (value.value))))
10477 warning (OPT_Wtraditional, "traditional C rejects initialization "
10480 /* Accept a string constant to initialize a subarray. */
10481 if (value.value != NULL_TREE
10482 && fieldcode == ARRAY_TYPE
10483 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10485 value.value = orig_value;
10486 /* Otherwise, if we have come to a subaggregate,
10487 and we don't have an element of its type, push into it. */
10488 else if (value.value != NULL_TREE
10489 && initialize_elementwise_p (fieldtype, value.value))
10491 push_init_level (loc, 1, braced_init_obstack);
10497 push_member_name (constructor_fields);
10498 output_init_element (loc, value.value, value.original_type,
10499 strict_string, fieldtype,
10500 constructor_fields, true, implicit,
10501 braced_init_obstack);
10502 RESTORE_SPELLING_DEPTH (constructor_depth);
10505 /* Do the bookkeeping for an element that was
10506 directly output as a constructor. */
10508 constructor_bit_index = DECL_SIZE (constructor_fields);
10509 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10512 constructor_fields = NULL_TREE;
10514 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10516 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10517 enum tree_code eltcode = TREE_CODE (elttype);
10519 /* Accept a string constant to initialize a subarray. */
10520 if (value.value != NULL_TREE
10521 && eltcode == ARRAY_TYPE
10522 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10524 value.value = orig_value;
10525 /* Otherwise, if we have come to a subaggregate,
10526 and we don't have an element of its type, push into it. */
10527 else if (value.value != NULL_TREE
10528 && initialize_elementwise_p (elttype, value.value))
10530 push_init_level (loc, 1, braced_init_obstack);
10534 if (constructor_max_index != NULL_TREE
10535 && (tree_int_cst_lt (constructor_max_index, constructor_index)
10536 || integer_all_onesp (constructor_max_index)))
10538 pedwarn_init (loc, 0,
10539 "excess elements in array initializer");
10543 /* Now output the actual element. */
10546 push_array_bounds (tree_to_uhwi (constructor_index));
10547 output_init_element (loc, value.value, value.original_type,
10548 strict_string, elttype,
10549 constructor_index, true, implicit,
10550 braced_init_obstack);
10551 RESTORE_SPELLING_DEPTH (constructor_depth);
10555 = size_binop_loc (input_location, PLUS_EXPR,
10556 constructor_index, bitsize_one_node);
10559 /* If we are doing the bookkeeping for an element that was
10560 directly output as a constructor, we must update
10561 constructor_unfilled_index. */
10562 constructor_unfilled_index = constructor_index;
10564 else if (gnu_vector_type_p (constructor_type))
10566 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10568 /* Do a basic check of initializer size. Note that vectors
10569 always have a fixed size derived from their type. */
10570 if (tree_int_cst_lt (constructor_max_index, constructor_index))
10572 pedwarn_init (loc, 0,
10573 "excess elements in vector initializer");
10577 /* Now output the actual element. */
10580 if (TREE_CODE (value.value) == VECTOR_CST)
10581 elttype = TYPE_MAIN_VARIANT (constructor_type);
10582 output_init_element (loc, value.value, value.original_type,
10583 strict_string, elttype,
10584 constructor_index, true, implicit,
10585 braced_init_obstack);
10589 = size_binop_loc (input_location,
10590 PLUS_EXPR, constructor_index, bitsize_one_node);
10593 /* If we are doing the bookkeeping for an element that was
10594 directly output as a constructor, we must update
10595 constructor_unfilled_index. */
10596 constructor_unfilled_index = constructor_index;
10599 /* Handle the sole element allowed in a braced initializer
10600 for a scalar variable. */
10601 else if (constructor_type != error_mark_node
10602 && constructor_fields == NULL_TREE)
10604 pedwarn_init (loc, 0,
10605 "excess elements in scalar initializer");
10611 output_init_element (loc, value.value, value.original_type,
10612 strict_string, constructor_type,
10613 NULL_TREE, true, implicit,
10614 braced_init_obstack);
10615 constructor_fields = NULL_TREE;
10618 /* Handle range initializers either at this level or anywhere higher
10619 in the designator stack. */
10620 if (constructor_range_stack)
10622 struct constructor_range_stack *p, *range_stack;
10625 range_stack = constructor_range_stack;
10626 constructor_range_stack = 0;
10627 while (constructor_stack != range_stack->stack)
10629 gcc_assert (constructor_stack->implicit);
10630 process_init_element (loc,
10631 pop_init_level (loc, 1,
10632 braced_init_obstack,
10633 last_init_list_comma),
10634 true, braced_init_obstack);
10636 for (p = range_stack;
10637 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10640 gcc_assert (constructor_stack->implicit);
10641 process_init_element (loc,
10642 pop_init_level (loc, 1,
10643 braced_init_obstack,
10644 last_init_list_comma),
10645 true, braced_init_obstack);
10648 p->index = size_binop_loc (input_location,
10649 PLUS_EXPR, p->index, bitsize_one_node);
10650 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10655 constructor_index = p->index;
10656 constructor_fields = p->fields;
10657 if (finish && p->range_end && p->index == p->range_start)
10665 finish_implicit_inits (loc, braced_init_obstack);
10666 push_init_level (loc, 2, braced_init_obstack);
10667 p->stack = constructor_stack;
10668 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10669 p->index = p->range_start;
10673 constructor_range_stack = range_stack;
10680 constructor_range_stack = 0;
10683 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10684 (guaranteed to be 'volatile' or null) and ARGS (represented using
10685 an ASM_EXPR node). */
10687 build_asm_stmt (bool is_volatile, tree args)
10690 ASM_VOLATILE_P (args) = 1;
10691 return add_stmt (args);
10694 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10695 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10696 SIMPLE indicates whether there was anything at all after the
10697 string in the asm expression -- asm("blah") and asm("blah" : )
10698 are subtly different. We use a ASM_EXPR node to represent this.
10699 LOC is the location of the asm, and IS_INLINE says whether this
10702 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10703 tree clobbers, tree labels, bool simple, bool is_inline)
10708 const char *constraint;
10709 const char **oconstraints;
10710 bool allows_mem, allows_reg, is_inout;
10711 int ninputs, noutputs;
10713 ninputs = list_length (inputs);
10714 noutputs = list_length (outputs);
10715 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10717 string = resolve_asm_operand_names (string, outputs, inputs, labels);
10719 /* Remove output conversions that change the type but not the mode. */
10720 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10722 tree output = TREE_VALUE (tail);
10724 output = c_fully_fold (output, false, NULL, true);
10726 /* ??? Really, this should not be here. Users should be using a
10727 proper lvalue, dammit. But there's a long history of using casts
10728 in the output operands. In cases like longlong.h, this becomes a
10729 primitive form of typechecking -- if the cast can be removed, then
10730 the output operand had a type of the proper width; otherwise we'll
10731 get an error. Gross, but ... */
10732 STRIP_NOPS (output);
10734 if (!lvalue_or_else (loc, output, lv_asm))
10735 output = error_mark_node;
10737 if (output != error_mark_node
10738 && (TREE_READONLY (output)
10739 || TYPE_READONLY (TREE_TYPE (output))
10740 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10741 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10742 readonly_error (loc, output, lv_asm);
10744 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10745 oconstraints[i] = constraint;
10747 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10748 &allows_mem, &allows_reg, &is_inout))
10750 /* If the operand is going to end up in memory,
10751 mark it addressable. */
10752 if (!allows_reg && !c_mark_addressable (output))
10753 output = error_mark_node;
10754 if (!(!allows_reg && allows_mem)
10755 && output != error_mark_node
10756 && VOID_TYPE_P (TREE_TYPE (output)))
10758 error_at (loc, "invalid use of void expression");
10759 output = error_mark_node;
10763 output = error_mark_node;
10765 TREE_VALUE (tail) = output;
10768 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10772 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10773 input = TREE_VALUE (tail);
10775 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10776 oconstraints, &allows_mem, &allows_reg))
10778 /* If the operand is going to end up in memory,
10779 mark it addressable. */
10780 if (!allows_reg && allows_mem)
10782 input = c_fully_fold (input, false, NULL, true);
10784 /* Strip the nops as we allow this case. FIXME, this really
10785 should be rejected or made deprecated. */
10786 STRIP_NOPS (input);
10787 if (!c_mark_addressable (input))
10788 input = error_mark_node;
10792 struct c_expr expr;
10793 memset (&expr, 0, sizeof (expr));
10794 expr.value = input;
10795 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10796 input = c_fully_fold (expr.value, false, NULL);
10798 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10800 error_at (loc, "invalid use of void expression");
10801 input = error_mark_node;
10806 input = error_mark_node;
10808 TREE_VALUE (tail) = input;
10811 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10813 /* asm statements without outputs, including simple ones, are treated
10815 ASM_INPUT_P (args) = simple;
10816 ASM_VOLATILE_P (args) = (noutputs == 0);
10817 ASM_INLINE_P (args) = is_inline;
10822 /* Generate a goto statement to LABEL. LOC is the location of the
10826 c_finish_goto_label (location_t loc, tree label)
10828 tree decl = lookup_label_for_goto (loc, label);
10831 TREE_USED (decl) = 1;
10833 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10834 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10835 SET_EXPR_LOCATION (t, loc);
10836 return add_stmt (t);
10840 /* Generate a computed goto statement to EXPR. LOC is the location of
10844 c_finish_goto_ptr (location_t loc, c_expr val)
10846 tree expr = val.value;
10848 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10849 if (expr != error_mark_node
10850 && !POINTER_TYPE_P (TREE_TYPE (expr))
10851 && !null_pointer_constant_p (expr))
10853 error_at (val.get_location (),
10854 "computed goto must be pointer type");
10855 expr = build_zero_cst (ptr_type_node);
10857 expr = c_fully_fold (expr, false, NULL);
10858 expr = convert (ptr_type_node, expr);
10859 t = build1 (GOTO_EXPR, void_type_node, expr);
10860 SET_EXPR_LOCATION (t, loc);
10861 return add_stmt (t);
10864 /* Generate a C `return' statement. RETVAL is the expression for what
10865 to return, or a null pointer for `return;' with no value. LOC is
10866 the location of the return statement, or the location of the expression,
10867 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10868 is the original type of RETVAL. */
10871 c_finish_return (location_t loc, tree retval, tree origtype)
10873 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10874 bool no_warning = false;
10877 /* Use the expansion point to handle cases such as returning NULL
10878 in a function returning void. */
10879 location_t xloc = expansion_point_location_if_in_system_header (loc);
10881 if (TREE_THIS_VOLATILE (current_function_decl))
10882 warning_at (xloc, 0,
10883 "function declared %<noreturn%> has a %<return%> statement");
10887 tree semantic_type = NULL_TREE;
10888 npc = null_pointer_constant_p (retval);
10889 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10891 semantic_type = TREE_TYPE (retval);
10892 retval = TREE_OPERAND (retval, 0);
10894 retval = c_fully_fold (retval, false, NULL);
10896 && valtype != NULL_TREE
10897 && TREE_CODE (valtype) != VOID_TYPE)
10898 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10903 current_function_returns_null = 1;
10904 if ((warn_return_type >= 0 || flag_isoc99)
10905 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10909 warned_here = pedwarn
10910 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10911 "%<return%> with no value, in function returning non-void");
10913 warned_here = warning_at
10914 (loc, OPT_Wreturn_type,
10915 "%<return%> with no value, in function returning non-void");
10918 inform (DECL_SOURCE_LOCATION (current_function_decl),
10922 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10924 current_function_returns_null = 1;
10926 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10927 warned_here = pedwarn
10928 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10929 "%<return%> with a value, in function returning void");
10931 warned_here = pedwarn
10932 (xloc, OPT_Wpedantic, "ISO C forbids "
10933 "%<return%> with expression, in function returning void");
10935 inform (DECL_SOURCE_LOCATION (current_function_decl),
10940 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10941 retval, origtype, ic_return,
10942 npc, NULL_TREE, NULL_TREE, 0);
10943 tree res = DECL_RESULT (current_function_decl);
10947 current_function_returns_value = 1;
10948 if (t == error_mark_node)
10951 save = in_late_binary_op;
10952 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10953 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10954 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10955 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10956 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10957 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10958 in_late_binary_op = true;
10959 inner = t = convert (TREE_TYPE (res), t);
10960 in_late_binary_op = save;
10962 /* Strip any conversions, additions, and subtractions, and see if
10963 we are returning the address of a local variable. Warn if so. */
10966 switch (TREE_CODE (inner))
10969 case NON_LVALUE_EXPR:
10971 case POINTER_PLUS_EXPR:
10972 inner = TREE_OPERAND (inner, 0);
10976 /* If the second operand of the MINUS_EXPR has a pointer
10977 type (or is converted from it), this may be valid, so
10978 don't give a warning. */
10980 tree op1 = TREE_OPERAND (inner, 1);
10982 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10983 && (CONVERT_EXPR_P (op1)
10984 || TREE_CODE (op1) == NON_LVALUE_EXPR))
10985 op1 = TREE_OPERAND (op1, 0);
10987 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10990 inner = TREE_OPERAND (inner, 0);
10995 inner = TREE_OPERAND (inner, 0);
10997 while (REFERENCE_CLASS_P (inner)
10998 && !INDIRECT_REF_P (inner))
10999 inner = TREE_OPERAND (inner, 0);
11002 && !DECL_EXTERNAL (inner)
11003 && !TREE_STATIC (inner)
11004 && DECL_CONTEXT (inner) == current_function_decl
11005 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11007 if (TREE_CODE (inner) == LABEL_DECL)
11008 warning_at (loc, OPT_Wreturn_local_addr,
11009 "function returns address of label");
11012 warning_at (loc, OPT_Wreturn_local_addr,
11013 "function returns address of local variable");
11014 tree zero = build_zero_cst (TREE_TYPE (res));
11015 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11027 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11028 SET_EXPR_LOCATION (retval, loc);
11030 if (warn_sequence_point)
11031 verify_sequence_points (retval);
11034 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11036 suppress_warning (ret_stmt, OPT_Wreturn_type);
11037 return add_stmt (ret_stmt);
11041 /* The SWITCH_STMT being built. */
11044 /* The original type of the testing expression, i.e. before the
11045 default conversion is applied. */
11048 /* A splay-tree mapping the low element of a case range to the high
11049 element, or NULL_TREE if there is no high element. Used to
11050 determine whether or not a new case label duplicates an old case
11051 label. We need a tree, rather than simply a hash table, because
11052 of the GNU case range extension. */
11055 /* The bindings at the point of the switch. This is used for
11056 warnings crossing decls when branching to a case label. */
11057 struct c_spot_bindings *bindings;
11059 /* Whether the switch includes any break statements. */
11060 bool break_stmt_seen_p;
11062 /* The next node on the stack. */
11063 struct c_switch *next;
11065 /* Remember whether the controlling expression had boolean type
11066 before integer promotions for the sake of -Wswitch-bool. */
11070 /* A stack of the currently active switch statements. The innermost
11071 switch statement is on the top of the stack. There is no need to
11072 mark the stack for garbage collection because it is only active
11073 during the processing of the body of a function, and we never
11074 collect at that point. */
11076 struct c_switch *c_switch_stack;
11078 /* Start a C switch statement, testing expression EXP. Return the new
11079 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11080 SWITCH_COND_LOC is the location of the switch's condition.
11081 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11084 c_start_switch (location_t switch_loc,
11085 location_t switch_cond_loc,
11086 tree exp, bool explicit_cast_p)
11088 tree orig_type = error_mark_node;
11089 bool bool_cond_p = false;
11090 struct c_switch *cs;
11092 if (exp != error_mark_node)
11094 orig_type = TREE_TYPE (exp);
11096 if (!INTEGRAL_TYPE_P (orig_type))
11098 if (orig_type != error_mark_node)
11100 error_at (switch_cond_loc, "switch quantity not an integer");
11101 orig_type = error_mark_node;
11103 exp = integer_zero_node;
11107 tree type = TYPE_MAIN_VARIANT (orig_type);
11110 /* Warn if the condition has boolean value. */
11111 while (TREE_CODE (e) == COMPOUND_EXPR)
11112 e = TREE_OPERAND (e, 1);
11114 if ((TREE_CODE (type) == BOOLEAN_TYPE
11115 || truth_value_p (TREE_CODE (e)))
11116 /* Explicit cast to int suppresses this warning. */
11117 && !(TREE_CODE (type) == INTEGER_TYPE
11118 && explicit_cast_p))
11119 bool_cond_p = true;
11121 if (!in_system_header_at (input_location)
11122 && (type == long_integer_type_node
11123 || type == long_unsigned_type_node))
11124 warning_at (switch_cond_loc,
11125 OPT_Wtraditional, "%<long%> switch expression not "
11126 "converted to %<int%> in ISO C");
11128 exp = c_fully_fold (exp, false, NULL);
11129 exp = default_conversion (exp);
11131 if (warn_sequence_point)
11132 verify_sequence_points (exp);
11136 /* Add this new SWITCH_STMT to the stack. */
11137 cs = XNEW (struct c_switch);
11138 cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11139 NULL_TREE, orig_type, NULL_TREE);
11140 cs->orig_type = orig_type;
11141 cs->cases = splay_tree_new (case_compare, NULL, NULL);
11142 cs->bindings = c_get_switch_bindings ();
11143 cs->break_stmt_seen_p = false;
11144 cs->bool_cond_p = bool_cond_p;
11145 cs->next = c_switch_stack;
11146 c_switch_stack = cs;
11148 return add_stmt (cs->switch_stmt);
11151 /* Process a case label at location LOC. */
11154 do_case (location_t loc, tree low_value, tree high_value)
11156 tree label = NULL_TREE;
11158 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11160 low_value = c_fully_fold (low_value, false, NULL);
11161 if (TREE_CODE (low_value) == INTEGER_CST)
11162 pedwarn (loc, OPT_Wpedantic,
11163 "case label is not an integer constant expression");
11166 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11168 high_value = c_fully_fold (high_value, false, NULL);
11169 if (TREE_CODE (high_value) == INTEGER_CST)
11170 pedwarn (input_location, OPT_Wpedantic,
11171 "case label is not an integer constant expression");
11174 if (c_switch_stack == NULL)
11177 error_at (loc, "case label not within a switch statement");
11179 error_at (loc, "%<default%> label not within a switch statement");
11183 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11184 EXPR_LOCATION (c_switch_stack->switch_stmt),
11188 label = c_add_case_label (loc, c_switch_stack->cases,
11189 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11190 low_value, high_value);
11191 if (label == error_mark_node)
11196 /* Finish the switch statement. TYPE is the original type of the
11197 controlling expression of the switch, or NULL_TREE. */
11200 c_finish_switch (tree body, tree type)
11202 struct c_switch *cs = c_switch_stack;
11203 location_t switch_location;
11205 SWITCH_STMT_BODY (cs->switch_stmt) = body;
11207 /* Emit warnings as needed. */
11208 switch_location = EXPR_LOCATION (cs->switch_stmt);
11209 c_do_switch_warnings (cs->cases, switch_location,
11210 type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11211 SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11212 if (c_switch_covers_all_cases_p (cs->cases,
11213 SWITCH_STMT_TYPE (cs->switch_stmt)))
11214 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11215 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11217 /* Pop the stack. */
11218 c_switch_stack = cs->next;
11219 splay_tree_delete (cs->cases);
11220 c_release_switch_bindings (cs->bindings);
11224 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11225 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11229 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11234 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11235 SET_EXPR_LOCATION (stmt, if_locus);
11240 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11242 /* In switch statements break is sometimes stylistically used after
11243 a return statement. This can lead to spurious warnings about
11244 control reaching the end of a non-void function when it is
11245 inlined. Note that we are calling block_may_fallthru with
11246 language specific tree nodes; this works because
11247 block_may_fallthru returns true when given something it does not
11249 bool skip = !block_may_fallthru (cur_stmt_list);
11252 switch (in_statement)
11255 error_at (loc, "break statement not within loop or switch");
11258 error_at (loc, "invalid exit from OpenMP structured block");
11261 error_at (loc, "break statement used with OpenMP for loop");
11263 case IN_ITERATION_STMT:
11264 case IN_OBJC_FOREACH:
11267 gcc_assert (in_statement & IN_SWITCH_STMT);
11268 c_switch_stack->break_stmt_seen_p = true;
11272 switch (in_statement & ~IN_SWITCH_STMT)
11275 error_at (loc, "continue statement not within a loop");
11278 error_at (loc, "invalid exit from OpenMP structured block");
11280 case IN_ITERATION_STMT:
11282 case IN_OBJC_FOREACH:
11285 gcc_unreachable ();
11290 else if ((in_statement & IN_OBJC_FOREACH)
11291 && !(is_break && (in_statement & IN_SWITCH_STMT)))
11293 /* The foreach expander produces low-level code using gotos instead
11294 of a structured loop construct. */
11295 gcc_assert (label);
11296 return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11298 return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11301 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11304 emit_side_effect_warnings (location_t loc, tree expr)
11306 maybe_warn_nodiscard (loc, expr);
11307 if (!warn_unused_value)
11309 if (expr == error_mark_node)
11311 else if (!TREE_SIDE_EFFECTS (expr))
11313 if (!VOID_TYPE_P (TREE_TYPE (expr))
11314 && !warning_suppressed_p (expr, OPT_Wunused_value))
11315 warning_at (loc, OPT_Wunused_value, "statement with no effect");
11317 else if (TREE_CODE (expr) == COMPOUND_EXPR)
11320 location_t cloc = loc;
11321 while (TREE_CODE (r) == COMPOUND_EXPR)
11323 if (EXPR_HAS_LOCATION (r))
11324 cloc = EXPR_LOCATION (r);
11325 r = TREE_OPERAND (r, 1);
11327 if (!TREE_SIDE_EFFECTS (r)
11328 && !VOID_TYPE_P (TREE_TYPE (r))
11329 && !CONVERT_EXPR_P (r)
11330 && !warning_suppressed_p (r, OPT_Wunused_value)
11331 && !warning_suppressed_p (expr, OPT_Wunused_value))
11332 warning_at (cloc, OPT_Wunused_value,
11333 "right-hand operand of comma expression has no effect");
11336 warn_if_unused_value (expr, loc);
11339 /* Process an expression as if it were a complete statement. Emit
11340 diagnostics, but do not call ADD_STMT. LOC is the location of the
11344 c_process_expr_stmt (location_t loc, tree expr)
11351 expr = c_fully_fold (expr, false, NULL);
11353 if (warn_sequence_point)
11354 verify_sequence_points (expr);
11356 if (TREE_TYPE (expr) != error_mark_node
11357 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11358 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11359 error_at (loc, "expression statement has incomplete type");
11361 /* If we're not processing a statement expression, warn about unused values.
11362 Warnings for statement expressions will be emitted later, once we figure
11363 out which is the result. */
11364 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11365 && (warn_unused_value || warn_unused_result))
11366 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11369 while (TREE_CODE (exprv) == COMPOUND_EXPR)
11370 exprv = TREE_OPERAND (exprv, 1);
11371 while (CONVERT_EXPR_P (exprv))
11372 exprv = TREE_OPERAND (exprv, 0);
11374 || handled_component_p (exprv)
11375 || TREE_CODE (exprv) == ADDR_EXPR)
11376 mark_exp_read (exprv);
11378 /* If the expression is not of a type to which we cannot assign a line
11379 number, wrap the thing in a no-op NOP_EXPR. */
11380 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11382 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11383 SET_EXPR_LOCATION (expr, loc);
11389 /* Emit an expression as a statement. LOC is the location of the
11393 c_finish_expr_stmt (location_t loc, tree expr)
11396 return add_stmt (c_process_expr_stmt (loc, expr));
11401 /* Do the opposite and emit a statement as an expression. To begin,
11402 create a new binding level and return it. */
11405 c_begin_stmt_expr (void)
11409 /* We must force a BLOCK for this level so that, if it is not expanded
11410 later, there is a way to turn off the entire subtree of blocks that
11411 are contained in it. */
11412 keep_next_level ();
11413 ret = c_begin_compound_stmt (true);
11415 c_bindings_start_stmt_expr (c_switch_stack == NULL
11417 : c_switch_stack->bindings);
11419 /* Mark the current statement list as belonging to a statement list. */
11420 STATEMENT_LIST_STMT_EXPR (ret) = 1;
11425 /* LOC is the location of the compound statement to which this body
11429 c_finish_stmt_expr (location_t loc, tree body)
11431 tree last, type, tmp, val;
11434 body = c_end_compound_stmt (loc, body, true);
11436 c_bindings_end_stmt_expr (c_switch_stack == NULL
11438 : c_switch_stack->bindings);
11440 /* Locate the last statement in BODY. See c_end_compound_stmt
11441 about always returning a BIND_EXPR. */
11442 last_p = &BIND_EXPR_BODY (body);
11443 last = BIND_EXPR_BODY (body);
11445 continue_searching:
11446 if (TREE_CODE (last) == STATEMENT_LIST)
11448 tree_stmt_iterator l = tsi_last (last);
11450 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11453 /* This can happen with degenerate cases like ({ }). No value. */
11457 /* If we're supposed to generate side effects warnings, process
11458 all of the statements except the last. */
11459 if (warn_unused_value || warn_unused_result)
11461 for (tree_stmt_iterator i = tsi_start (last);
11462 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11465 tree t = tsi_stmt (i);
11467 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11468 emit_side_effect_warnings (tloc, t);
11471 last_p = tsi_stmt_ptr (l);
11475 /* If the end of the list is exception related, then the list was split
11476 by a call to push_cleanup. Continue searching. */
11477 if (TREE_CODE (last) == TRY_FINALLY_EXPR
11478 || TREE_CODE (last) == TRY_CATCH_EXPR)
11480 last_p = &TREE_OPERAND (last, 0);
11482 goto continue_searching;
11485 if (last == error_mark_node)
11488 /* In the case that the BIND_EXPR is not necessary, return the
11489 expression out from inside it. */
11490 if ((last == BIND_EXPR_BODY (body)
11491 /* Skip nested debug stmts. */
11492 || last == expr_first (BIND_EXPR_BODY (body)))
11493 && BIND_EXPR_VARS (body) == NULL)
11495 /* Even if this looks constant, do not allow it in a constant
11497 last = c_wrap_maybe_const (last, true);
11498 /* Do not warn if the return value of a statement expression is
11500 suppress_warning (last, OPT_Wunused);
11504 /* Extract the type of said expression. */
11505 type = TREE_TYPE (last);
11507 /* If we're not returning a value at all, then the BIND_EXPR that
11508 we already have is a fine expression to return. */
11509 if (!type || VOID_TYPE_P (type))
11512 /* Now that we've located the expression containing the value, it seems
11513 silly to make voidify_wrapper_expr repeat the process. Create a
11514 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11515 tmp = create_tmp_var_raw (type);
11517 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11518 tree_expr_nonnegative_p giving up immediately. */
11520 if (TREE_CODE (val) == NOP_EXPR
11521 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11522 val = TREE_OPERAND (val, 0);
11524 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11525 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11528 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11529 SET_EXPR_LOCATION (t, loc);
11534 /* Begin and end compound statements. This is as simple as pushing
11535 and popping new statement lists from the tree. */
11538 c_begin_compound_stmt (bool do_scope)
11540 tree stmt = push_stmt_list ();
11546 /* End a compound statement. STMT is the statement. LOC is the
11547 location of the compound statement-- this is usually the location
11548 of the opening brace. */
11551 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11557 if (c_dialect_objc ())
11558 objc_clear_super_receiver ();
11559 block = pop_scope ();
11562 stmt = pop_stmt_list (stmt);
11563 stmt = c_build_bind_expr (loc, block, stmt);
11565 /* If this compound statement is nested immediately inside a statement
11566 expression, then force a BIND_EXPR to be created. Otherwise we'll
11567 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11568 STATEMENT_LISTs merge, and thus we can lose track of what statement
11569 was really last. */
11570 if (building_stmt_list_p ()
11571 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11572 && TREE_CODE (stmt) != BIND_EXPR)
11574 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11575 TREE_SIDE_EFFECTS (stmt) = 1;
11576 SET_EXPR_LOCATION (stmt, loc);
11582 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11583 when the current scope is exited. EH_ONLY is true when this is not
11584 meant to apply to normal control flow transfer. */
11587 push_cleanup (tree decl, tree cleanup, bool eh_only)
11589 enum tree_code code;
11593 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11594 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11596 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11597 list = push_stmt_list ();
11598 TREE_OPERAND (stmt, 0) = list;
11599 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11602 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11603 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11606 build_vec_cmp (tree_code code, tree type,
11607 tree arg0, tree arg1)
11609 tree zero_vec = build_zero_cst (type);
11610 tree minus_one_vec = build_minus_one_cst (type);
11611 tree cmp_type = truth_type_for (type);
11612 tree cmp = build2 (code, cmp_type, arg0, arg1);
11613 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11616 /* Possibly warn about an address of OP never being NULL in a comparison
11617 operation CODE involving null. */
11620 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11622 /* Prevent warnings issued for macro expansion. */
11624 || warning_suppressed_p (op, OPT_Waddress)
11625 || from_macro_expansion_at (loc))
11628 if (TREE_CODE (op) == NOP_EXPR)
11630 /* Allow casts to intptr_t to suppress the warning. */
11631 tree type = TREE_TYPE (op);
11632 if (TREE_CODE (type) == INTEGER_TYPE)
11634 op = TREE_OPERAND (op, 0);
11637 if (TREE_CODE (op) == POINTER_PLUS_EXPR)
11639 /* Allow a cast to void* to suppress the warning. */
11640 tree type = TREE_TYPE (TREE_TYPE (op));
11641 if (VOID_TYPE_P (type))
11644 /* Adding any value to a null pointer, including zero, is undefined
11645 in C. This includes the expression &p[0] where p is the null
11646 pointer, although &p[0] will have been folded to p by this point
11647 and so not diagnosed. */
11648 if (code == EQ_EXPR)
11649 warning_at (loc, OPT_Waddress,
11650 "the comparison will always evaluate as %<false%> "
11651 "for the pointer operand in %qE must not be NULL",
11654 warning_at (loc, OPT_Waddress,
11655 "the comparison will always evaluate as %<true%> "
11656 "for the pointer operand in %qE must not be NULL",
11662 if (TREE_CODE (op) != ADDR_EXPR)
11665 op = TREE_OPERAND (op, 0);
11667 if (TREE_CODE (op) == IMAGPART_EXPR
11668 || TREE_CODE (op) == REALPART_EXPR)
11670 /* The address of either complex part may not be null. */
11671 if (code == EQ_EXPR)
11672 warning_at (loc, OPT_Waddress,
11673 "the comparison will always evaluate as %<false%> "
11674 "for the address of %qE will never be NULL",
11677 warning_at (loc, OPT_Waddress,
11678 "the comparison will always evaluate as %<true%> "
11679 "for the address of %qE will never be NULL",
11684 /* Set to true in the loop below if OP dereferences is operand.
11685 In such a case the ultimate target need not be a decl for
11686 the null [in]equality test to be constant. */
11687 bool deref = false;
11689 /* Get the outermost array or object, or member. */
11690 while (handled_component_p (op))
11692 if (TREE_CODE (op) == COMPONENT_REF)
11694 /* Get the member (its address is never null). */
11695 op = TREE_OPERAND (op, 1);
11699 /* Get the outer array/object to refer to in the warning. */
11700 op = TREE_OPERAND (op, 0);
11704 if ((!deref && !decl_with_nonnull_addr_p (op))
11705 || from_macro_expansion_at (loc))
11708 if (code == EQ_EXPR)
11709 warning_at (loc, OPT_Waddress,
11710 "the comparison will always evaluate as %<false%> "
11711 "for the address of %qE will never be NULL",
11714 warning_at (loc, OPT_Waddress,
11715 "the comparison will always evaluate as %<true%> "
11716 "for the address of %qE will never be NULL",
11720 inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
11723 /* Build a binary-operation expression without default conversions.
11724 CODE is the kind of expression to build.
11725 LOCATION is the operator's location.
11726 This function differs from `build' in several ways:
11727 the data type of the result is computed and recorded in it,
11728 warnings are generated if arg data types are invalid,
11729 special handling for addition and subtraction of pointers is known,
11730 and some optimization is done (operations on narrow ints
11731 are done in the narrower type when that gives the same result).
11732 Constant folding is also done before the result is returned.
11734 Note that the operands will never have enumeral types, or function
11735 or array types, because either they will have the default conversions
11736 performed or they have both just been converted to some other type in which
11737 the arithmetic is to be done. */
11740 build_binary_op (location_t location, enum tree_code code,
11741 tree orig_op0, tree orig_op1, bool convert_p)
11743 tree type0, type1, orig_type0, orig_type1;
11745 enum tree_code code0, code1;
11747 tree ret = error_mark_node;
11748 const char *invalid_op_diag;
11749 bool op0_int_operands, op1_int_operands;
11750 bool int_const, int_const_or_overflow, int_operands;
11752 /* Expression code to give to the expression when it is built.
11753 Normally this is CODE, which is what the caller asked for,
11754 but in some special cases we change it. */
11755 enum tree_code resultcode = code;
11757 /* Data type in which the computation is to be performed.
11758 In the simplest cases this is the common type of the arguments. */
11759 tree result_type = NULL;
11761 /* When the computation is in excess precision, the type of the
11762 final EXCESS_PRECISION_EXPR. */
11763 tree semantic_result_type = NULL;
11765 /* Nonzero means operands have already been type-converted
11766 in whatever way is necessary.
11767 Zero means they need to be converted to RESULT_TYPE. */
11770 /* Nonzero means create the expression with this type, rather than
11772 tree build_type = NULL_TREE;
11774 /* Nonzero means after finally constructing the expression
11775 convert it to this type. */
11776 tree final_type = NULL_TREE;
11778 /* Nonzero if this is an operation like MIN or MAX which can
11779 safely be computed in short if both args are promoted shorts.
11780 Also implies COMMON.
11781 -1 indicates a bitwise operation; this makes a difference
11782 in the exact conditions for when it is safe to do the operation
11783 in a narrower mode. */
11786 /* Nonzero if this is a comparison operation;
11787 if both args are promoted shorts, compare the original shorts.
11788 Also implies COMMON. */
11789 int short_compare = 0;
11791 /* Nonzero if this is a right-shift operation, which can be computed on the
11792 original short and then promoted if the operand is a promoted short. */
11793 int short_shift = 0;
11795 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11798 /* True means types are compatible as far as ObjC is concerned. */
11801 /* True means this is an arithmetic operation that may need excess
11803 bool may_need_excess_precision;
11805 /* True means this is a boolean operation that converts both its
11806 operands to truth-values. */
11807 bool boolean_op = false;
11809 /* Remember whether we're doing / or %. */
11810 bool doing_div_or_mod = false;
11812 /* Remember whether we're doing << or >>. */
11813 bool doing_shift = false;
11815 /* Tree holding instrumentation expression. */
11816 tree instrument_expr = NULL;
11818 if (location == UNKNOWN_LOCATION)
11819 location = input_location;
11824 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11825 if (op0_int_operands)
11826 op0 = remove_c_maybe_const_expr (op0);
11827 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11828 if (op1_int_operands)
11829 op1 = remove_c_maybe_const_expr (op1);
11830 int_operands = (op0_int_operands && op1_int_operands);
11833 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11834 && TREE_CODE (orig_op1) == INTEGER_CST);
11835 int_const = (int_const_or_overflow
11836 && !TREE_OVERFLOW (orig_op0)
11837 && !TREE_OVERFLOW (orig_op1));
11840 int_const = int_const_or_overflow = false;
11842 /* Do not apply default conversion in mixed vector/scalar expression. */
11844 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11846 op0 = default_conversion (op0);
11847 op1 = default_conversion (op1);
11850 orig_type0 = type0 = TREE_TYPE (op0);
11852 orig_type1 = type1 = TREE_TYPE (op1);
11854 /* The expression codes of the data types of the arguments tell us
11855 whether the arguments are integers, floating, pointers, etc. */
11856 code0 = TREE_CODE (type0);
11857 code1 = TREE_CODE (type1);
11859 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11860 STRIP_TYPE_NOPS (op0);
11861 STRIP_TYPE_NOPS (op1);
11863 /* If an error was already reported for one of the arguments,
11864 avoid reporting another error. */
11866 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11867 return error_mark_node;
11869 if (code0 == POINTER_TYPE
11870 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11871 return error_mark_node;
11873 if (code1 == POINTER_TYPE
11874 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11875 return error_mark_node;
11877 if ((invalid_op_diag
11878 = targetm.invalid_binary_op (code, type0, type1)))
11880 error_at (location, invalid_op_diag);
11881 return error_mark_node;
11889 case TRUNC_DIV_EXPR:
11890 case CEIL_DIV_EXPR:
11891 case FLOOR_DIV_EXPR:
11892 case ROUND_DIV_EXPR:
11893 case EXACT_DIV_EXPR:
11894 may_need_excess_precision = true;
11903 /* Excess precision for implicit conversions of integers to
11904 floating point in C11 and later. */
11905 may_need_excess_precision = (flag_isoc11
11906 && (ANY_INTEGRAL_TYPE_P (type0)
11907 || ANY_INTEGRAL_TYPE_P (type1)));
11911 may_need_excess_precision = false;
11914 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11916 op0 = TREE_OPERAND (op0, 0);
11917 type0 = TREE_TYPE (op0);
11919 else if (may_need_excess_precision
11920 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11923 op0 = convert (eptype, op0);
11925 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11927 op1 = TREE_OPERAND (op1, 0);
11928 type1 = TREE_TYPE (op1);
11930 else if (may_need_excess_precision
11931 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11934 op1 = convert (eptype, op1);
11937 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11939 /* In case when one of the operands of the binary operation is
11940 a vector and another is a scalar -- convert scalar to vector. */
11941 if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11942 || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11944 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11947 switch (convert_flag)
11950 return error_mark_node;
11953 bool maybe_const = true;
11955 sc = c_fully_fold (op0, false, &maybe_const);
11956 sc = save_expr (sc);
11957 sc = convert (TREE_TYPE (type1), sc);
11958 op0 = build_vector_from_val (type1, sc);
11960 op0 = c_wrap_maybe_const (op0, true);
11961 orig_type0 = type0 = TREE_TYPE (op0);
11962 code0 = TREE_CODE (type0);
11966 case stv_secondarg:
11968 bool maybe_const = true;
11970 sc = c_fully_fold (op1, false, &maybe_const);
11971 sc = save_expr (sc);
11972 sc = convert (TREE_TYPE (type0), sc);
11973 op1 = build_vector_from_val (type0, sc);
11975 op1 = c_wrap_maybe_const (op1, true);
11976 orig_type1 = type1 = TREE_TYPE (op1);
11977 code1 = TREE_CODE (type1);
11989 /* Handle the pointer + int case. */
11990 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11992 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11993 goto return_build_binary_op;
11995 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11997 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11998 goto return_build_binary_op;
12005 /* Subtraction of two similar pointers.
12006 We must subtract them as integers, then divide by object size. */
12007 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12008 && comp_target_types (location, type0, type1))
12010 ret = pointer_diff (location, op0, op1, &instrument_expr);
12011 goto return_build_binary_op;
12013 /* Handle pointer minus int. Just like pointer plus int. */
12014 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12016 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12017 goto return_build_binary_op;
12027 case TRUNC_DIV_EXPR:
12028 case CEIL_DIV_EXPR:
12029 case FLOOR_DIV_EXPR:
12030 case ROUND_DIV_EXPR:
12031 case EXACT_DIV_EXPR:
12032 doing_div_or_mod = true;
12033 warn_for_div_by_zero (location, op1);
12035 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12036 || code0 == FIXED_POINT_TYPE
12037 || code0 == COMPLEX_TYPE
12038 || gnu_vector_type_p (type0))
12039 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12040 || code1 == FIXED_POINT_TYPE
12041 || code1 == COMPLEX_TYPE
12042 || gnu_vector_type_p (type1)))
12044 enum tree_code tcode0 = code0, tcode1 = code1;
12046 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12047 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12048 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12049 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12051 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12052 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12053 resultcode = RDIV_EXPR;
12055 /* Although it would be tempting to shorten always here, that
12056 loses on some targets, since the modulo instruction is
12057 undefined if the quotient can't be represented in the
12058 computation mode. We shorten only if unsigned or if
12059 dividing by something we know != -1. */
12060 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12061 || (TREE_CODE (op1) == INTEGER_CST
12062 && !integer_all_onesp (op1)));
12070 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12072 /* Allow vector types which are not floating point types. */
12073 else if (gnu_vector_type_p (type0)
12074 && gnu_vector_type_p (type1)
12075 && !VECTOR_FLOAT_TYPE_P (type0)
12076 && !VECTOR_FLOAT_TYPE_P (type1))
12080 case TRUNC_MOD_EXPR:
12081 case FLOOR_MOD_EXPR:
12082 doing_div_or_mod = true;
12083 warn_for_div_by_zero (location, op1);
12085 if (gnu_vector_type_p (type0)
12086 && gnu_vector_type_p (type1)
12087 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12088 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12090 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12092 /* Although it would be tempting to shorten always here, that loses
12093 on some targets, since the modulo instruction is undefined if the
12094 quotient can't be represented in the computation mode. We shorten
12095 only if unsigned or if dividing by something we know != -1. */
12096 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12097 || (TREE_CODE (op1) == INTEGER_CST
12098 && !integer_all_onesp (op1)));
12103 case TRUTH_ANDIF_EXPR:
12104 case TRUTH_ORIF_EXPR:
12105 case TRUTH_AND_EXPR:
12106 case TRUTH_OR_EXPR:
12107 case TRUTH_XOR_EXPR:
12108 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12109 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12110 || code0 == FIXED_POINT_TYPE)
12111 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12112 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12113 || code1 == FIXED_POINT_TYPE))
12115 /* Result of these operations is always an int,
12116 but that does not mean the operands should be
12117 converted to ints! */
12118 result_type = integer_type_node;
12119 if (op0_int_operands)
12121 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12122 op0 = remove_c_maybe_const_expr (op0);
12125 op0 = c_objc_common_truthvalue_conversion (location, op0);
12126 if (op1_int_operands)
12128 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12129 op1 = remove_c_maybe_const_expr (op1);
12132 op1 = c_objc_common_truthvalue_conversion (location, op1);
12136 if (code == TRUTH_ANDIF_EXPR)
12138 int_const_or_overflow = (int_operands
12139 && TREE_CODE (orig_op0) == INTEGER_CST
12140 && (op0 == truthvalue_false_node
12141 || TREE_CODE (orig_op1) == INTEGER_CST));
12142 int_const = (int_const_or_overflow
12143 && !TREE_OVERFLOW (orig_op0)
12144 && (op0 == truthvalue_false_node
12145 || !TREE_OVERFLOW (orig_op1)));
12147 else if (code == TRUTH_ORIF_EXPR)
12149 int_const_or_overflow = (int_operands
12150 && TREE_CODE (orig_op0) == INTEGER_CST
12151 && (op0 == truthvalue_true_node
12152 || TREE_CODE (orig_op1) == INTEGER_CST));
12153 int_const = (int_const_or_overflow
12154 && !TREE_OVERFLOW (orig_op0)
12155 && (op0 == truthvalue_true_node
12156 || !TREE_OVERFLOW (orig_op1)));
12160 /* Shift operations: result has same type as first operand;
12161 always convert second operand to int.
12162 Also set SHORT_SHIFT if shifting rightward. */
12165 if (gnu_vector_type_p (type0)
12166 && gnu_vector_type_p (type1)
12167 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12168 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12169 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12170 TYPE_VECTOR_SUBPARTS (type1)))
12172 result_type = type0;
12175 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12176 || (gnu_vector_type_p (type0)
12177 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12178 && code1 == INTEGER_TYPE)
12180 doing_shift = true;
12181 if (TREE_CODE (op1) == INTEGER_CST)
12183 if (tree_int_cst_sgn (op1) < 0)
12186 if (c_inhibit_evaluation_warnings == 0)
12187 warning_at (location, OPT_Wshift_count_negative,
12188 "right shift count is negative");
12190 else if (code0 == VECTOR_TYPE)
12192 if (compare_tree_int (op1,
12193 TYPE_PRECISION (TREE_TYPE (type0)))
12197 if (c_inhibit_evaluation_warnings == 0)
12198 warning_at (location, OPT_Wshift_count_overflow,
12199 "right shift count >= width of vector element");
12204 if (!integer_zerop (op1))
12207 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12210 if (c_inhibit_evaluation_warnings == 0)
12211 warning_at (location, OPT_Wshift_count_overflow,
12212 "right shift count >= width of type");
12217 /* Use the type of the value to be shifted. */
12218 result_type = type0;
12219 /* Avoid converting op1 to result_type later. */
12225 if (gnu_vector_type_p (type0)
12226 && gnu_vector_type_p (type1)
12227 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12228 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12229 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12230 TYPE_VECTOR_SUBPARTS (type1)))
12232 result_type = type0;
12235 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12236 || (gnu_vector_type_p (type0)
12237 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12238 && code1 == INTEGER_TYPE)
12240 doing_shift = true;
12241 if (TREE_CODE (op0) == INTEGER_CST
12242 && tree_int_cst_sgn (op0) < 0
12243 && !TYPE_OVERFLOW_WRAPS (type0))
12245 /* Don't reject a left shift of a negative value in a context
12246 where a constant expression is needed in C90. */
12249 if (c_inhibit_evaluation_warnings == 0)
12250 warning_at (location, OPT_Wshift_negative_value,
12251 "left shift of negative value");
12253 if (TREE_CODE (op1) == INTEGER_CST)
12255 if (tree_int_cst_sgn (op1) < 0)
12258 if (c_inhibit_evaluation_warnings == 0)
12259 warning_at (location, OPT_Wshift_count_negative,
12260 "left shift count is negative");
12262 else if (code0 == VECTOR_TYPE)
12264 if (compare_tree_int (op1,
12265 TYPE_PRECISION (TREE_TYPE (type0)))
12269 if (c_inhibit_evaluation_warnings == 0)
12270 warning_at (location, OPT_Wshift_count_overflow,
12271 "left shift count >= width of vector element");
12274 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12277 if (c_inhibit_evaluation_warnings == 0)
12278 warning_at (location, OPT_Wshift_count_overflow,
12279 "left shift count >= width of type");
12281 else if (TREE_CODE (op0) == INTEGER_CST
12282 && maybe_warn_shift_overflow (location, op0, op1)
12287 /* Use the type of the value to be shifted. */
12288 result_type = type0;
12289 /* Avoid converting op1 to result_type later. */
12296 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12299 if (!vector_types_compatible_elements_p (type0, type1))
12301 error_at (location, "comparing vectors with different "
12303 return error_mark_node;
12306 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12307 TYPE_VECTOR_SUBPARTS (type1)))
12309 error_at (location, "comparing vectors with different "
12310 "number of elements");
12311 return error_mark_node;
12314 /* It's not precisely specified how the usual arithmetic
12315 conversions apply to the vector types. Here, we use
12316 the unsigned type if one of the operands is signed and
12317 the other one is unsigned. */
12318 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12320 if (!TYPE_UNSIGNED (type0))
12321 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12323 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12324 warning_at (location, OPT_Wsign_compare, "comparison between "
12325 "types %qT and %qT", type0, type1);
12328 /* Always construct signed integer vector type. */
12329 intt = c_common_type_for_size (GET_MODE_BITSIZE
12331 (TREE_TYPE (type0))), 0);
12334 error_at (location, "could not find an integer type "
12335 "of the same size as %qT",
12336 TREE_TYPE (type0));
12337 return error_mark_node;
12339 result_type = build_opaque_vector_type (intt,
12340 TYPE_VECTOR_SUBPARTS (type0));
12342 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12343 goto return_build_binary_op;
12345 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12346 warning_at (location,
12348 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12349 /* Result of comparison is always int,
12350 but don't convert the args to int! */
12351 build_type = integer_type_node;
12352 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12353 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12354 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12355 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12357 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12359 maybe_warn_for_null_address (location, op0, code);
12360 result_type = type0;
12362 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12364 maybe_warn_for_null_address (location, op1, code);
12365 result_type = type1;
12367 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12369 tree tt0 = TREE_TYPE (type0);
12370 tree tt1 = TREE_TYPE (type1);
12371 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12372 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12373 addr_space_t as_common = ADDR_SPACE_GENERIC;
12375 /* Anything compares with void *. void * compares with anything.
12376 Otherwise, the targets must be compatible
12377 and both must be object or both incomplete. */
12378 if (comp_target_types (location, type0, type1))
12379 result_type = common_pointer_type (type0, type1);
12380 else if (!addr_space_superset (as0, as1, &as_common))
12382 error_at (location, "comparison of pointers to "
12383 "disjoint address spaces");
12384 return error_mark_node;
12386 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12388 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12389 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12390 "comparison of %<void *%> with function pointer");
12392 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12394 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12395 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12396 "comparison of %<void *%> with function pointer");
12399 /* Avoid warning about the volatile ObjC EH puts on decls. */
12401 pedwarn (location, 0,
12402 "comparison of distinct pointer types lacks a cast");
12404 if (result_type == NULL_TREE)
12406 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12407 result_type = build_pointer_type
12408 (build_qualified_type (void_type_node, qual));
12411 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12413 result_type = type0;
12414 pedwarn (location, 0, "comparison between pointer and integer");
12416 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12418 result_type = type1;
12419 pedwarn (location, 0, "comparison between pointer and integer");
12421 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12422 || truth_value_p (TREE_CODE (orig_op0)))
12423 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12424 || truth_value_p (TREE_CODE (orig_op1))))
12425 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12432 if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12435 if (!vector_types_compatible_elements_p (type0, type1))
12437 error_at (location, "comparing vectors with different "
12439 return error_mark_node;
12442 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12443 TYPE_VECTOR_SUBPARTS (type1)))
12445 error_at (location, "comparing vectors with different "
12446 "number of elements");
12447 return error_mark_node;
12450 /* It's not precisely specified how the usual arithmetic
12451 conversions apply to the vector types. Here, we use
12452 the unsigned type if one of the operands is signed and
12453 the other one is unsigned. */
12454 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12456 if (!TYPE_UNSIGNED (type0))
12457 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12459 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12460 warning_at (location, OPT_Wsign_compare, "comparison between "
12461 "types %qT and %qT", type0, type1);
12464 /* Always construct signed integer vector type. */
12465 intt = c_common_type_for_size (GET_MODE_BITSIZE
12467 (TREE_TYPE (type0))), 0);
12470 error_at (location, "could not find an integer type "
12471 "of the same size as %qT",
12472 TREE_TYPE (type0));
12473 return error_mark_node;
12475 result_type = build_opaque_vector_type (intt,
12476 TYPE_VECTOR_SUBPARTS (type0));
12478 ret = build_vec_cmp (resultcode, result_type, op0, op1);
12479 goto return_build_binary_op;
12481 build_type = integer_type_node;
12482 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12483 || code0 == FIXED_POINT_TYPE)
12484 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12485 || code1 == FIXED_POINT_TYPE))
12487 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12489 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12490 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12491 addr_space_t as_common;
12493 if (comp_target_types (location, type0, type1))
12495 result_type = common_pointer_type (type0, type1);
12496 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12497 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12498 pedwarn_c99 (location, OPT_Wpedantic,
12499 "comparison of complete and incomplete pointers");
12500 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12501 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12502 "ordered comparisons of pointers to functions");
12503 else if (null_pointer_constant_p (orig_op0)
12504 || null_pointer_constant_p (orig_op1))
12505 warning_at (location, OPT_Wextra,
12506 "ordered comparison of pointer with null pointer");
12509 else if (!addr_space_superset (as0, as1, &as_common))
12511 error_at (location, "comparison of pointers to "
12512 "disjoint address spaces");
12513 return error_mark_node;
12517 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12518 result_type = build_pointer_type
12519 (build_qualified_type (void_type_node, qual));
12520 pedwarn (location, 0,
12521 "comparison of distinct pointer types lacks a cast");
12524 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12526 result_type = type0;
12528 pedwarn (location, OPT_Wpedantic,
12529 "ordered comparison of pointer with integer zero");
12530 else if (extra_warnings)
12531 warning_at (location, OPT_Wextra,
12532 "ordered comparison of pointer with integer zero");
12534 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12536 result_type = type1;
12538 pedwarn (location, OPT_Wpedantic,
12539 "ordered comparison of pointer with integer zero");
12540 else if (extra_warnings)
12541 warning_at (location, OPT_Wextra,
12542 "ordered comparison of pointer with integer zero");
12544 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12546 result_type = type0;
12547 pedwarn (location, 0, "comparison between pointer and integer");
12549 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12551 result_type = type1;
12552 pedwarn (location, 0, "comparison between pointer and integer");
12555 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12556 && current_function_decl != NULL_TREE
12557 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12559 op0 = save_expr (op0);
12560 op1 = save_expr (op1);
12562 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12563 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12566 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12567 || truth_value_p (TREE_CODE (orig_op0)))
12568 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12569 || truth_value_p (TREE_CODE (orig_op1))))
12570 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12575 /* Used for OpenMP atomics. */
12576 gcc_assert (flag_openmp);
12581 gcc_unreachable ();
12584 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12585 return error_mark_node;
12587 if (gnu_vector_type_p (type0)
12588 && gnu_vector_type_p (type1)
12589 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12590 || !vector_types_compatible_elements_p (type0, type1)))
12592 gcc_rich_location richloc (location);
12593 maybe_range_label_for_tree_type_mismatch
12594 label_for_op0 (orig_op0, orig_op1),
12595 label_for_op1 (orig_op1, orig_op0);
12596 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12597 richloc.maybe_add_expr (orig_op1, &label_for_op1);
12598 binary_op_error (&richloc, code, type0, type1);
12599 return error_mark_node;
12602 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12603 || code0 == FIXED_POINT_TYPE
12604 || gnu_vector_type_p (type0))
12606 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12607 || code1 == FIXED_POINT_TYPE
12608 || gnu_vector_type_p (type1)))
12610 bool first_complex = (code0 == COMPLEX_TYPE);
12611 bool second_complex = (code1 == COMPLEX_TYPE);
12612 int none_complex = (!first_complex && !second_complex);
12614 if (shorten || common || short_compare)
12616 result_type = c_common_type (type0, type1);
12617 do_warn_double_promotion (result_type, type0, type1,
12618 "implicit conversion from %qT to %qT "
12619 "to match other operand of binary "
12622 if (result_type == error_mark_node)
12623 return error_mark_node;
12626 if (first_complex != second_complex
12627 && (code == PLUS_EXPR
12628 || code == MINUS_EXPR
12629 || code == MULT_EXPR
12630 || (code == TRUNC_DIV_EXPR && first_complex))
12631 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12632 && flag_signed_zeros)
12634 /* An operation on mixed real/complex operands must be
12635 handled specially, but the language-independent code can
12636 more easily optimize the plain complex arithmetic if
12637 -fno-signed-zeros. */
12638 tree real_type = TREE_TYPE (result_type);
12640 if (type0 != orig_type0 || type1 != orig_type1)
12642 gcc_assert (may_need_excess_precision && common);
12643 semantic_result_type = c_common_type (orig_type0, orig_type1);
12647 if (TREE_TYPE (op0) != result_type)
12648 op0 = convert_and_check (location, result_type, op0);
12649 if (TREE_TYPE (op1) != real_type)
12650 op1 = convert_and_check (location, real_type, op1);
12654 if (TREE_TYPE (op0) != real_type)
12655 op0 = convert_and_check (location, real_type, op0);
12656 if (TREE_TYPE (op1) != result_type)
12657 op1 = convert_and_check (location, result_type, op1);
12659 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12660 return error_mark_node;
12663 op0 = save_expr (op0);
12664 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12666 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12671 case TRUNC_DIV_EXPR:
12672 op1 = save_expr (op1);
12673 imag = build2 (resultcode, real_type, imag, op1);
12674 /* Fall through. */
12677 real = build2 (resultcode, real_type, real, op1);
12685 op1 = save_expr (op1);
12686 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12688 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12693 op0 = save_expr (op0);
12694 imag = build2 (resultcode, real_type, op0, imag);
12695 /* Fall through. */
12697 real = build2 (resultcode, real_type, op0, real);
12700 real = build2 (resultcode, real_type, op0, real);
12701 imag = build1 (NEGATE_EXPR, real_type, imag);
12707 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12708 goto return_build_binary_op;
12711 /* For certain operations (which identify themselves by shorten != 0)
12712 if both args were extended from the same smaller type,
12713 do the arithmetic in that type and then extend.
12715 shorten !=0 and !=1 indicates a bitwise operation.
12716 For them, this optimization is safe only if
12717 both args are zero-extended or both are sign-extended.
12718 Otherwise, we might change the result.
12719 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12720 but calculated in (unsigned short) it would be (unsigned short)-1. */
12722 if (shorten && none_complex)
12724 final_type = result_type;
12725 result_type = shorten_binary_op (result_type, op0, op1,
12729 /* Shifts can be shortened if shifting right. */
12734 tree arg0 = get_narrower (op0, &unsigned_arg);
12736 final_type = result_type;
12738 if (arg0 == op0 && final_type == TREE_TYPE (op0))
12739 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12741 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12742 && tree_int_cst_sgn (op1) > 0
12743 /* We can shorten only if the shift count is less than the
12744 number of bits in the smaller type size. */
12745 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12746 /* We cannot drop an unsigned shift after sign-extension. */
12747 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12749 /* Do an unsigned shift if the operand was zero-extended. */
12751 = c_common_signed_or_unsigned_type (unsigned_arg,
12753 /* Convert value-to-be-shifted to that type. */
12754 if (TREE_TYPE (op0) != result_type)
12755 op0 = convert (result_type, op0);
12760 /* Comparison operations are shortened too but differently.
12761 They identify themselves by setting short_compare = 1. */
12765 /* Don't write &op0, etc., because that would prevent op0
12766 from being kept in a register.
12767 Instead, make copies of the our local variables and
12768 pass the copies by reference, then copy them back afterward. */
12769 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12770 enum tree_code xresultcode = resultcode;
12772 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12775 if (val != NULL_TREE)
12778 goto return_build_binary_op;
12781 op0 = xop0, op1 = xop1;
12783 resultcode = xresultcode;
12785 if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12787 bool op0_maybe_const = true;
12788 bool op1_maybe_const = true;
12789 tree orig_op0_folded, orig_op1_folded;
12791 if (in_late_binary_op)
12793 orig_op0_folded = orig_op0;
12794 orig_op1_folded = orig_op1;
12798 /* Fold for the sake of possible warnings, as in
12799 build_conditional_expr. This requires the
12800 "original" values to be folded, not just op0 and
12802 c_inhibit_evaluation_warnings++;
12803 op0 = c_fully_fold (op0, require_constant_value,
12805 op1 = c_fully_fold (op1, require_constant_value,
12807 c_inhibit_evaluation_warnings--;
12808 orig_op0_folded = c_fully_fold (orig_op0,
12809 require_constant_value,
12811 orig_op1_folded = c_fully_fold (orig_op1,
12812 require_constant_value,
12816 if (warn_sign_compare)
12817 warn_for_sign_compare (location, orig_op0_folded,
12818 orig_op1_folded, op0, op1,
12819 result_type, resultcode);
12820 if (!in_late_binary_op && !int_operands)
12822 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12823 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12824 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12825 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12831 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12832 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12833 Then the expression will be built.
12834 It will be given type FINAL_TYPE if that is nonzero;
12835 otherwise, it will be given type RESULT_TYPE. */
12839 /* Favor showing any expression locations that are available. */
12840 op_location_t oploc (location, UNKNOWN_LOCATION);
12841 binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12842 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12843 return error_mark_node;
12846 if (build_type == NULL_TREE)
12848 build_type = result_type;
12849 if ((type0 != orig_type0 || type1 != orig_type1)
12852 gcc_assert (may_need_excess_precision && common);
12853 semantic_result_type = c_common_type (orig_type0, orig_type1);
12859 op0 = ep_convert_and_check (location, result_type, op0,
12860 semantic_result_type);
12861 op1 = ep_convert_and_check (location, result_type, op1,
12862 semantic_result_type);
12864 /* This can happen if one operand has a vector type, and the other
12865 has a different type. */
12866 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12867 return error_mark_node;
12870 if (sanitize_flags_p ((SANITIZE_SHIFT
12872 | SANITIZE_FLOAT_DIVIDE
12873 | SANITIZE_SI_OVERFLOW))
12874 && current_function_decl != NULL_TREE
12875 && (doing_div_or_mod || doing_shift)
12876 && !require_constant_value)
12878 /* OP0 and/or OP1 might have side-effects. */
12879 op0 = save_expr (op0);
12880 op1 = save_expr (op1);
12881 op0 = c_fully_fold (op0, false, NULL);
12882 op1 = c_fully_fold (op1, false, NULL);
12883 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12884 | SANITIZE_FLOAT_DIVIDE
12885 | SANITIZE_SI_OVERFLOW))))
12886 instrument_expr = ubsan_instrument_division (location, op0, op1);
12887 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12888 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12891 /* Treat expressions in initializers specially as they can't trap. */
12892 if (int_const_or_overflow)
12893 ret = (require_constant_value
12894 ? fold_build2_initializer_loc (location, resultcode, build_type,
12896 : fold_build2_loc (location, resultcode, build_type, op0, op1));
12898 ret = build2 (resultcode, build_type, op0, op1);
12899 if (final_type != NULL_TREE)
12900 ret = convert (final_type, ret);
12902 return_build_binary_op:
12903 gcc_assert (ret != error_mark_node);
12904 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12905 ret = (int_operands
12906 ? note_integer_operands (ret)
12907 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12908 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12909 && !in_late_binary_op)
12910 ret = note_integer_operands (ret);
12911 protected_set_expr_location (ret, location);
12913 if (instrument_expr != NULL)
12914 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12915 instrument_expr, ret);
12917 if (semantic_result_type)
12918 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12919 semantic_result_type, ret);
12925 /* Convert EXPR to be a truth-value, validating its type for this
12926 purpose. LOCATION is the source location for the expression. */
12929 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12931 bool int_const, int_operands;
12933 switch (TREE_CODE (TREE_TYPE (expr)))
12936 error_at (location, "used array that cannot be converted to pointer where scalar is required");
12937 return error_mark_node;
12940 error_at (location, "used struct type value where scalar is required");
12941 return error_mark_node;
12944 error_at (location, "used union type value where scalar is required");
12945 return error_mark_node;
12948 error_at (location, "void value not ignored as it ought to be");
12949 return error_mark_node;
12952 if (reject_gcc_builtin (expr))
12953 return error_mark_node;
12956 case FUNCTION_TYPE:
12957 gcc_unreachable ();
12960 error_at (location, "used vector type where scalar is required");
12961 return error_mark_node;
12967 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12968 int_operands = EXPR_INT_CONST_OPERANDS (expr);
12969 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12971 expr = remove_c_maybe_const_expr (expr);
12972 expr = build2 (NE_EXPR, integer_type_node, expr,
12973 convert (TREE_TYPE (expr), integer_zero_node));
12974 expr = note_integer_operands (expr);
12977 /* ??? Should we also give an error for vectors rather than leaving
12978 those to give errors later? */
12979 expr = c_common_truthvalue_conversion (location, expr);
12981 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12983 if (TREE_OVERFLOW (expr))
12986 return note_integer_operands (expr);
12988 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12989 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12994 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12998 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13000 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13002 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13003 /* Executing a compound literal inside a function reinitializes
13005 if (!TREE_STATIC (decl))
13013 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13014 statement. LOC is the location of the construct. */
13017 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13020 body = c_end_compound_stmt (loc, body, true);
13022 tree stmt = make_node (code);
13023 TREE_TYPE (stmt) = void_type_node;
13024 OMP_BODY (stmt) = body;
13025 OMP_CLAUSES (stmt) = clauses;
13026 SET_EXPR_LOCATION (stmt, loc);
13028 return add_stmt (stmt);
13031 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13032 statement. LOC is the location of the OACC_DATA. */
13035 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13039 block = c_end_compound_stmt (loc, block, true);
13041 stmt = make_node (OACC_DATA);
13042 TREE_TYPE (stmt) = void_type_node;
13043 OACC_DATA_CLAUSES (stmt) = clauses;
13044 OACC_DATA_BODY (stmt) = block;
13045 SET_EXPR_LOCATION (stmt, loc);
13047 return add_stmt (stmt);
13050 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13051 statement. LOC is the location of the OACC_HOST_DATA. */
13054 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13058 block = c_end_compound_stmt (loc, block, true);
13060 stmt = make_node (OACC_HOST_DATA);
13061 TREE_TYPE (stmt) = void_type_node;
13062 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13063 OACC_HOST_DATA_BODY (stmt) = block;
13064 SET_EXPR_LOCATION (stmt, loc);
13066 return add_stmt (stmt);
13069 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13072 c_begin_omp_parallel (void)
13076 keep_next_level ();
13077 block = c_begin_compound_stmt (true);
13082 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13083 statement. LOC is the location of the OMP_PARALLEL. */
13086 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13090 block = c_end_compound_stmt (loc, block, true);
13092 stmt = make_node (OMP_PARALLEL);
13093 TREE_TYPE (stmt) = void_type_node;
13094 OMP_PARALLEL_CLAUSES (stmt) = clauses;
13095 OMP_PARALLEL_BODY (stmt) = block;
13096 SET_EXPR_LOCATION (stmt, loc);
13098 return add_stmt (stmt);
13101 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13104 c_begin_omp_task (void)
13108 keep_next_level ();
13109 block = c_begin_compound_stmt (true);
13114 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13115 statement. LOC is the location of the #pragma. */
13118 c_finish_omp_task (location_t loc, tree clauses, tree block)
13122 block = c_end_compound_stmt (loc, block, true);
13124 stmt = make_node (OMP_TASK);
13125 TREE_TYPE (stmt) = void_type_node;
13126 OMP_TASK_CLAUSES (stmt) = clauses;
13127 OMP_TASK_BODY (stmt) = block;
13128 SET_EXPR_LOCATION (stmt, loc);
13130 return add_stmt (stmt);
13133 /* Generate GOMP_cancel call for #pragma omp cancel. */
13136 c_finish_omp_cancel (location_t loc, tree clauses)
13138 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13140 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13142 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13144 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13146 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13150 error_at (loc, "%<#pragma omp cancel%> must specify one of "
13151 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13155 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13156 if (ifc != NULL_TREE)
13158 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13159 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13160 error_at (OMP_CLAUSE_LOCATION (ifc),
13161 "expected %<cancel%> %<if%> clause modifier");
13164 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13165 if (ifc2 != NULL_TREE)
13167 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13168 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13169 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13170 error_at (OMP_CLAUSE_LOCATION (ifc2),
13171 "expected %<cancel%> %<if%> clause modifier");
13175 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13176 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13177 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13178 build_zero_cst (type));
13181 ifc = boolean_true_node;
13182 tree stmt = build_call_expr_loc (loc, fn, 2,
13183 build_int_cst (integer_type_node, mask),
13188 /* Generate GOMP_cancellation_point call for
13189 #pragma omp cancellation point. */
13192 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13194 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13196 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13198 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13200 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13202 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13206 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13207 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13211 tree stmt = build_call_expr_loc (loc, fn, 1,
13212 build_int_cst (integer_type_node, mask));
13216 /* Helper function for handle_omp_array_sections. Called recursively
13217 to handle multiple array-section-subscripts. C is the clause,
13218 T current expression (initially OMP_CLAUSE_DECL), which is either
13219 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13220 expression if specified, TREE_VALUE length expression if specified,
13221 TREE_CHAIN is what it has been specified after, or some decl.
13222 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13223 set to true if any of the array-section-subscript could have length
13224 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13225 first array-section-subscript which is known not to have length
13227 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13228 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13229 all are or may have length of 1, array-section-subscript [:2] is the
13230 first one known not to have length 1. For array-section-subscript
13231 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13232 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13233 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13234 case though, as some lengths could be zero. */
13237 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13238 bool &maybe_zero_len, unsigned int &first_non_one,
13239 enum c_omp_region_type ort)
13241 tree ret, low_bound, length, type;
13242 if (TREE_CODE (t) != TREE_LIST)
13244 if (error_operand_p (t))
13245 return error_mark_node;
13247 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13248 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13249 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13251 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13252 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13253 return error_mark_node;
13255 while (TREE_CODE (t) == INDIRECT_REF)
13257 t = TREE_OPERAND (t, 0);
13259 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13260 t = TREE_OPERAND (t, 0);
13262 while (TREE_CODE (t) == COMPOUND_EXPR)
13264 t = TREE_OPERAND (t, 1);
13267 if (TREE_CODE (t) == COMPONENT_REF
13268 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13269 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13270 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13272 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13274 error_at (OMP_CLAUSE_LOCATION (c),
13275 "bit-field %qE in %qs clause",
13276 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13277 return error_mark_node;
13279 while (TREE_CODE (t) == COMPONENT_REF)
13281 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13283 error_at (OMP_CLAUSE_LOCATION (c),
13284 "%qE is a member of a union", t);
13285 return error_mark_node;
13287 t = TREE_OPERAND (t, 0);
13288 while (TREE_CODE (t) == MEM_REF
13289 || TREE_CODE (t) == INDIRECT_REF
13290 || TREE_CODE (t) == ARRAY_REF)
13292 t = TREE_OPERAND (t, 0);
13294 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13295 t = TREE_OPERAND (t, 0);
13297 if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13299 if (maybe_ne (mem_ref_offset (t), 0))
13300 error_at (OMP_CLAUSE_LOCATION (c),
13301 "cannot dereference %qE in %qs clause", t,
13302 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13304 t = TREE_OPERAND (t, 0);
13308 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13311 error_at (OMP_CLAUSE_LOCATION (c),
13312 "%qD is not a variable in %qs clause", t,
13313 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13315 error_at (OMP_CLAUSE_LOCATION (c),
13316 "%qE is not a variable in %qs clause", t,
13317 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13318 return error_mark_node;
13320 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13321 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13322 && TYPE_ATOMIC (TREE_TYPE (t)))
13324 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13325 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13326 return error_mark_node;
13328 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13329 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13331 && DECL_THREAD_LOCAL_P (t))
13333 error_at (OMP_CLAUSE_LOCATION (c),
13334 "%qD is threadprivate variable in %qs clause", t,
13335 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13336 return error_mark_node;
13338 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13339 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13340 && TYPE_ATOMIC (TREE_TYPE (t))
13341 && POINTER_TYPE_P (TREE_TYPE (t)))
13343 /* If the array section is pointer based and the pointer
13344 itself is _Atomic qualified, we need to atomically load
13347 memset (&expr, 0, sizeof (expr));
13349 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13350 expr, false, false);
13356 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13357 maybe_zero_len, first_non_one, ort);
13358 if (ret == error_mark_node || ret == NULL_TREE)
13361 type = TREE_TYPE (ret);
13362 low_bound = TREE_PURPOSE (t);
13363 length = TREE_VALUE (t);
13365 if (low_bound == error_mark_node || length == error_mark_node)
13366 return error_mark_node;
13368 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13370 error_at (OMP_CLAUSE_LOCATION (c),
13371 "low bound %qE of array section does not have integral type",
13373 return error_mark_node;
13375 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13377 error_at (OMP_CLAUSE_LOCATION (c),
13378 "length %qE of array section does not have integral type",
13380 return error_mark_node;
13383 && TREE_CODE (low_bound) == INTEGER_CST
13384 && TYPE_PRECISION (TREE_TYPE (low_bound))
13385 > TYPE_PRECISION (sizetype))
13386 low_bound = fold_convert (sizetype, low_bound);
13388 && TREE_CODE (length) == INTEGER_CST
13389 && TYPE_PRECISION (TREE_TYPE (length))
13390 > TYPE_PRECISION (sizetype))
13391 length = fold_convert (sizetype, length);
13392 if (low_bound == NULL_TREE)
13393 low_bound = integer_zero_node;
13394 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13395 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13396 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13398 if (length != integer_one_node)
13400 error_at (OMP_CLAUSE_LOCATION (c),
13401 "expected single pointer in %qs clause",
13402 user_omp_clause_code_name (c, ort == C_ORT_ACC));
13403 return error_mark_node;
13406 if (length != NULL_TREE)
13408 if (!integer_nonzerop (length))
13410 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13411 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13412 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13413 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13414 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13416 if (integer_zerop (length))
13418 error_at (OMP_CLAUSE_LOCATION (c),
13419 "zero length array section in %qs clause",
13420 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13421 return error_mark_node;
13425 maybe_zero_len = true;
13427 if (first_non_one == types.length ()
13428 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13431 if (TREE_CODE (type) == ARRAY_TYPE)
13433 if (length == NULL_TREE
13434 && (TYPE_DOMAIN (type) == NULL_TREE
13435 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13437 error_at (OMP_CLAUSE_LOCATION (c),
13438 "for unknown bound array type length expression must "
13440 return error_mark_node;
13442 if (TREE_CODE (low_bound) == INTEGER_CST
13443 && tree_int_cst_sgn (low_bound) == -1)
13445 error_at (OMP_CLAUSE_LOCATION (c),
13446 "negative low bound in array section in %qs clause",
13447 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13448 return error_mark_node;
13450 if (length != NULL_TREE
13451 && TREE_CODE (length) == INTEGER_CST
13452 && tree_int_cst_sgn (length) == -1)
13454 error_at (OMP_CLAUSE_LOCATION (c),
13455 "negative length in array section in %qs clause",
13456 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13457 return error_mark_node;
13459 if (TYPE_DOMAIN (type)
13460 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13461 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13465 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13466 size = size_binop (PLUS_EXPR, size, size_one_node);
13467 if (TREE_CODE (low_bound) == INTEGER_CST)
13469 if (tree_int_cst_lt (size, low_bound))
13471 error_at (OMP_CLAUSE_LOCATION (c),
13472 "low bound %qE above array section size "
13473 "in %qs clause", low_bound,
13474 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13475 return error_mark_node;
13477 if (tree_int_cst_equal (size, low_bound))
13479 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13480 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13481 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13482 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13483 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13485 error_at (OMP_CLAUSE_LOCATION (c),
13486 "zero length array section in %qs clause",
13487 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13488 return error_mark_node;
13490 maybe_zero_len = true;
13492 else if (length == NULL_TREE
13493 && first_non_one == types.length ()
13494 && tree_int_cst_equal
13495 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13499 else if (length == NULL_TREE)
13501 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13502 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13503 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13504 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13505 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13506 maybe_zero_len = true;
13507 if (first_non_one == types.length ())
13510 if (length && TREE_CODE (length) == INTEGER_CST)
13512 if (tree_int_cst_lt (size, length))
13514 error_at (OMP_CLAUSE_LOCATION (c),
13515 "length %qE above array section size "
13516 "in %qs clause", length,
13517 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13518 return error_mark_node;
13520 if (TREE_CODE (low_bound) == INTEGER_CST)
13523 = size_binop (PLUS_EXPR,
13524 fold_convert (sizetype, low_bound),
13525 fold_convert (sizetype, length));
13526 if (TREE_CODE (lbpluslen) == INTEGER_CST
13527 && tree_int_cst_lt (size, lbpluslen))
13529 error_at (OMP_CLAUSE_LOCATION (c),
13530 "high bound %qE above array section size "
13531 "in %qs clause", lbpluslen,
13532 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13533 return error_mark_node;
13538 else if (length == NULL_TREE)
13540 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13541 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13542 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13543 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13544 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13545 maybe_zero_len = true;
13546 if (first_non_one == types.length ())
13550 /* For [lb:] we will need to evaluate lb more than once. */
13551 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13553 tree lb = save_expr (low_bound);
13554 if (lb != low_bound)
13556 TREE_PURPOSE (t) = lb;
13561 else if (TREE_CODE (type) == POINTER_TYPE)
13563 if (length == NULL_TREE)
13565 if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13566 error_at (OMP_CLAUSE_LOCATION (c),
13567 "for array function parameter length expression "
13568 "must be specified");
13570 error_at (OMP_CLAUSE_LOCATION (c),
13571 "for pointer type length expression must be specified");
13572 return error_mark_node;
13574 if (length != NULL_TREE
13575 && TREE_CODE (length) == INTEGER_CST
13576 && tree_int_cst_sgn (length) == -1)
13578 error_at (OMP_CLAUSE_LOCATION (c),
13579 "negative length in array section in %qs clause",
13580 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13581 return error_mark_node;
13583 /* If there is a pointer type anywhere but in the very first
13584 array-section-subscript, the array section could be non-contiguous. */
13585 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13586 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13587 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13589 /* If any prior dimension has a non-one length, then deem this
13590 array section as non-contiguous. */
13591 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13592 d = TREE_CHAIN (d))
13594 tree d_length = TREE_VALUE (d);
13595 if (d_length == NULL_TREE || !integer_onep (d_length))
13597 error_at (OMP_CLAUSE_LOCATION (c),
13598 "array section is not contiguous in %qs clause",
13599 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13600 return error_mark_node;
13607 error_at (OMP_CLAUSE_LOCATION (c),
13608 "%qE does not have pointer or array type", ret);
13609 return error_mark_node;
13611 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13612 types.safe_push (TREE_TYPE (ret));
13613 /* We will need to evaluate lb more than once. */
13614 tree lb = save_expr (low_bound);
13615 if (lb != low_bound)
13617 TREE_PURPOSE (t) = lb;
13620 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13624 /* Handle array sections for clause C. */
13627 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13629 bool maybe_zero_len = false;
13630 unsigned int first_non_one = 0;
13631 auto_vec<tree, 10> types;
13632 tree *tp = &OMP_CLAUSE_DECL (c);
13633 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13634 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13635 && TREE_CODE (*tp) == TREE_LIST
13636 && TREE_PURPOSE (*tp)
13637 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13638 tp = &TREE_VALUE (*tp);
13639 tree first = handle_omp_array_sections_1 (c, *tp, types,
13640 maybe_zero_len, first_non_one,
13642 if (first == error_mark_node)
13644 if (first == NULL_TREE)
13646 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13647 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13650 tree tem = NULL_TREE;
13651 /* Need to evaluate side effects in the length expressions
13653 while (TREE_CODE (t) == TREE_LIST)
13655 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13657 if (tem == NULL_TREE)
13658 tem = TREE_VALUE (t);
13660 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13661 TREE_VALUE (t), tem);
13663 t = TREE_CHAIN (t);
13666 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13667 first = c_fully_fold (first, false, NULL, true);
13672 unsigned int num = types.length (), i;
13673 tree t, side_effects = NULL_TREE, size = NULL_TREE;
13674 tree condition = NULL_TREE;
13676 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13677 maybe_zero_len = true;
13679 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13680 t = TREE_CHAIN (t))
13682 tree low_bound = TREE_PURPOSE (t);
13683 tree length = TREE_VALUE (t);
13687 && TREE_CODE (low_bound) == INTEGER_CST
13688 && TYPE_PRECISION (TREE_TYPE (low_bound))
13689 > TYPE_PRECISION (sizetype))
13690 low_bound = fold_convert (sizetype, low_bound);
13692 && TREE_CODE (length) == INTEGER_CST
13693 && TYPE_PRECISION (TREE_TYPE (length))
13694 > TYPE_PRECISION (sizetype))
13695 length = fold_convert (sizetype, length);
13696 if (low_bound == NULL_TREE)
13697 low_bound = integer_zero_node;
13698 if (!maybe_zero_len && i > first_non_one)
13700 if (integer_nonzerop (low_bound))
13701 goto do_warn_noncontiguous;
13702 if (length != NULL_TREE
13703 && TREE_CODE (length) == INTEGER_CST
13704 && TYPE_DOMAIN (types[i])
13705 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13706 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13710 size = size_binop (PLUS_EXPR,
13711 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13713 if (!tree_int_cst_equal (length, size))
13715 do_warn_noncontiguous:
13716 error_at (OMP_CLAUSE_LOCATION (c),
13717 "array section is not contiguous in %qs "
13719 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13723 if (length != NULL_TREE
13724 && TREE_SIDE_EFFECTS (length))
13726 if (side_effects == NULL_TREE)
13727 side_effects = length;
13729 side_effects = build2 (COMPOUND_EXPR,
13730 TREE_TYPE (side_effects),
13731 length, side_effects);
13738 if (i > first_non_one
13739 && ((length && integer_nonzerop (length))
13740 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13741 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13742 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13745 l = fold_convert (sizetype, length);
13748 l = size_binop (PLUS_EXPR,
13749 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13751 l = size_binop (MINUS_EXPR, l,
13752 fold_convert (sizetype, low_bound));
13754 if (i > first_non_one)
13756 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13758 if (condition == NULL_TREE)
13761 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13764 else if (size == NULL_TREE)
13766 size = size_in_bytes (TREE_TYPE (types[i]));
13767 tree eltype = TREE_TYPE (types[num - 1]);
13768 while (TREE_CODE (eltype) == ARRAY_TYPE)
13769 eltype = TREE_TYPE (eltype);
13770 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13771 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13772 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13774 if (integer_zerop (size)
13775 || integer_zerop (size_in_bytes (eltype)))
13777 error_at (OMP_CLAUSE_LOCATION (c),
13778 "zero length array section in %qs clause",
13779 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13780 return error_mark_node;
13782 size = size_binop (EXACT_DIV_EXPR, size,
13783 size_in_bytes (eltype));
13785 size = size_binop (MULT_EXPR, size, l);
13787 size = fold_build3 (COND_EXPR, sizetype, condition,
13788 size, size_zero_node);
13791 size = size_binop (MULT_EXPR, size, l);
13795 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13796 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13797 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13798 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13800 size = size_binop (MINUS_EXPR, size, size_one_node);
13801 size = c_fully_fold (size, false, NULL);
13802 size = save_expr (size);
13803 tree index_type = build_index_type (size);
13804 tree eltype = TREE_TYPE (first);
13805 while (TREE_CODE (eltype) == ARRAY_TYPE)
13806 eltype = TREE_TYPE (eltype);
13807 tree type = build_array_type (eltype, index_type);
13808 tree ptype = build_pointer_type (eltype);
13809 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13810 t = build_fold_addr_expr (t);
13811 tree t2 = build_fold_addr_expr (first);
13812 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13813 ptrdiff_type_node, t2);
13814 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13815 ptrdiff_type_node, t2,
13816 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13817 ptrdiff_type_node, t));
13818 t2 = c_fully_fold (t2, false, NULL);
13819 if (tree_fits_shwi_p (t2))
13820 t = build2 (MEM_REF, type, t,
13821 build_int_cst (ptype, tree_to_shwi (t2)));
13824 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13825 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13826 TREE_TYPE (t), t, t2);
13827 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13829 OMP_CLAUSE_DECL (c) = t;
13832 first = c_fully_fold (first, false, NULL);
13833 OMP_CLAUSE_DECL (c) = first;
13834 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13837 size = c_fully_fold (size, false, NULL);
13838 OMP_CLAUSE_SIZE (c) = size;
13839 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13840 || (TREE_CODE (t) == COMPONENT_REF
13841 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13843 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13844 switch (OMP_CLAUSE_MAP_KIND (c))
13846 case GOMP_MAP_ALLOC:
13847 case GOMP_MAP_IF_PRESENT:
13849 case GOMP_MAP_FROM:
13850 case GOMP_MAP_TOFROM:
13851 case GOMP_MAP_ALWAYS_TO:
13852 case GOMP_MAP_ALWAYS_FROM:
13853 case GOMP_MAP_ALWAYS_TOFROM:
13854 case GOMP_MAP_RELEASE:
13855 case GOMP_MAP_DELETE:
13856 case GOMP_MAP_FORCE_TO:
13857 case GOMP_MAP_FORCE_FROM:
13858 case GOMP_MAP_FORCE_TOFROM:
13859 case GOMP_MAP_FORCE_PRESENT:
13860 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13865 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13866 if (TREE_CODE (t) == COMPONENT_REF)
13867 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13869 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13870 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13871 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13872 && !c_mark_addressable (t))
13874 OMP_CLAUSE_DECL (c2) = t;
13875 t = build_fold_addr_expr (first);
13876 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13877 tree ptr = OMP_CLAUSE_DECL (c2);
13878 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13879 ptr = build_fold_addr_expr (ptr);
13880 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13881 ptrdiff_type_node, t,
13882 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13883 ptrdiff_type_node, ptr));
13884 t = c_fully_fold (t, false, NULL);
13885 OMP_CLAUSE_SIZE (c2) = t;
13886 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13887 OMP_CLAUSE_CHAIN (c) = c2;
13892 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13893 an inline call. But, remap
13894 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13895 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13898 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13899 tree decl, tree placeholder)
13902 hash_map<tree, tree> decl_map;
13904 decl_map.put (omp_decl1, placeholder);
13905 decl_map.put (omp_decl2, decl);
13906 memset (&id, 0, sizeof (id));
13907 id.src_fn = DECL_CONTEXT (omp_decl1);
13908 id.dst_fn = current_function_decl;
13909 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13910 id.decl_map = &decl_map;
13912 id.copy_decl = copy_decl_no_change;
13913 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13914 id.transform_new_cfg = true;
13915 id.transform_return_to_modify = false;
13917 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13921 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13922 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13925 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13927 if (*tp == (tree) data)
13932 /* Similarly, but also walk aggregate fields. */
13934 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13937 c_find_omp_var_r (tree *tp, int *, void *data)
13939 if (*tp == ((struct c_find_omp_var_s *) data)->var)
13941 if (RECORD_OR_UNION_TYPE_P (*tp))
13944 hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13946 for (field = TYPE_FIELDS (*tp); field;
13947 field = DECL_CHAIN (field))
13948 if (TREE_CODE (field) == FIELD_DECL)
13950 tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13951 c_find_omp_var_r, data, pset);
13954 ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13957 ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13961 ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13966 else if (INTEGRAL_TYPE_P (*tp))
13967 return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13968 ((struct c_find_omp_var_s *) data)->pset);
13972 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13973 and clauses containing them should be removed. */
13976 c_omp_finish_iterators (tree iter)
13979 for (tree it = iter; it; it = TREE_CHAIN (it))
13981 tree var = TREE_VEC_ELT (it, 0);
13982 tree begin = TREE_VEC_ELT (it, 1);
13983 tree end = TREE_VEC_ELT (it, 2);
13984 tree step = TREE_VEC_ELT (it, 3);
13986 tree type = TREE_TYPE (var);
13987 location_t loc = DECL_SOURCE_LOCATION (var);
13988 if (type == error_mark_node)
13993 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13995 error_at (loc, "iterator %qD has neither integral nor pointer type",
14000 else if (TYPE_ATOMIC (type))
14002 error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14006 else if (TYPE_READONLY (type))
14008 error_at (loc, "iterator %qD has const qualified type", var);
14012 else if (step == error_mark_node
14013 || TREE_TYPE (step) == error_mark_node)
14018 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14020 error_at (EXPR_LOC_OR_LOC (step, loc),
14021 "iterator step with non-integral type");
14025 begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14026 end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14027 orig_step = save_expr (c_fully_fold (step, false, NULL));
14028 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14029 step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14030 if (POINTER_TYPE_P (type))
14032 begin = save_expr (begin);
14033 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14034 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14035 fold_convert (sizetype, step),
14036 fold_convert (sizetype, begin));
14037 step = fold_convert (ssizetype, step);
14039 if (integer_zerop (step))
14041 error_at (loc, "iterator %qD has zero step", var);
14046 if (begin == error_mark_node
14047 || end == error_mark_node
14048 || step == error_mark_node
14049 || orig_step == error_mark_node)
14054 hash_set<tree> pset;
14056 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14058 tree var2 = TREE_VEC_ELT (it2, 0);
14059 tree begin2 = TREE_VEC_ELT (it2, 1);
14060 tree end2 = TREE_VEC_ELT (it2, 2);
14061 tree step2 = TREE_VEC_ELT (it2, 3);
14062 tree type2 = TREE_TYPE (var2);
14063 location_t loc2 = DECL_SOURCE_LOCATION (var2);
14064 struct c_find_omp_var_s data = { var, &pset };
14065 if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14068 "type of iterator %qD refers to outer iterator %qD",
14072 else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14074 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14075 "begin expression refers to outer iterator %qD", var);
14078 else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14080 error_at (EXPR_LOC_OR_LOC (end2, loc2),
14081 "end expression refers to outer iterator %qD", var);
14084 else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14086 error_at (EXPR_LOC_OR_LOC (step2, loc2),
14087 "step expression refers to outer iterator %qD", var);
14096 TREE_VEC_ELT (it, 1) = begin;
14097 TREE_VEC_ELT (it, 2) = end;
14098 TREE_VEC_ELT (it, 3) = step;
14099 TREE_VEC_ELT (it, 4) = orig_step;
14104 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14105 Return true if an error has been detected. */
14108 c_oacc_check_attachments (tree c)
14110 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14113 /* OpenACC attach / detach clauses must be pointers. */
14114 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14115 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14117 tree t = OMP_CLAUSE_DECL (c);
14119 while (TREE_CODE (t) == TREE_LIST)
14120 t = TREE_CHAIN (t);
14122 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14124 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14125 user_omp_clause_code_name (c, true));
14133 /* For all elements of CLAUSES, validate them against their constraints.
14134 Remove any elements from the list that are invalid. */
14137 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14139 bitmap_head generic_head, firstprivate_head, lastprivate_head;
14140 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14141 bitmap_head oacc_reduction_head, is_on_device_head;
14142 tree c, t, type, *pc;
14143 tree simdlen = NULL_TREE, safelen = NULL_TREE;
14144 bool branch_seen = false;
14145 bool copyprivate_seen = false;
14146 bool mergeable_seen = false;
14147 tree *detach_seen = NULL;
14148 bool linear_variable_step_check = false;
14149 tree *nowait_clause = NULL;
14150 tree ordered_clause = NULL_TREE;
14151 tree schedule_clause = NULL_TREE;
14152 bool oacc_async = false;
14153 bool indir_component_ref_p = false;
14154 tree last_iterators = NULL_TREE;
14155 bool last_iterators_remove = false;
14156 tree *nogroup_seen = NULL;
14157 tree *order_clause = NULL;
14158 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14159 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14160 int reduction_seen = 0;
14161 bool allocate_seen = false;
14162 bool implicit_moved = false;
14163 bool target_in_reduction_seen = false;
14165 bitmap_obstack_initialize (NULL);
14166 bitmap_initialize (&generic_head, &bitmap_default_obstack);
14167 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14168 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14169 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14170 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14171 bitmap_initialize (&map_head, &bitmap_default_obstack);
14172 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14173 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14174 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14175 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14176 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14177 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14179 if (ort & C_ORT_ACC)
14180 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14181 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14187 for (pc = &clauses, c = clauses; c ; c = *pc)
14189 bool remove = false;
14190 bool need_complete = false;
14191 bool need_implicitly_determined = false;
14193 switch (OMP_CLAUSE_CODE (c))
14195 case OMP_CLAUSE_SHARED:
14196 need_implicitly_determined = true;
14197 goto check_dup_generic;
14199 case OMP_CLAUSE_PRIVATE:
14200 need_complete = true;
14201 need_implicitly_determined = true;
14202 goto check_dup_generic;
14204 case OMP_CLAUSE_REDUCTION:
14205 if (reduction_seen == 0)
14206 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14207 else if (reduction_seen != -2
14208 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14211 error_at (OMP_CLAUSE_LOCATION (c),
14212 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14213 "on the same construct");
14214 reduction_seen = -2;
14217 case OMP_CLAUSE_IN_REDUCTION:
14218 case OMP_CLAUSE_TASK_REDUCTION:
14219 need_implicitly_determined = true;
14220 t = OMP_CLAUSE_DECL (c);
14221 if (TREE_CODE (t) == TREE_LIST)
14223 if (handle_omp_array_sections (c, ort))
14229 t = OMP_CLAUSE_DECL (c);
14230 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14231 && OMP_CLAUSE_REDUCTION_INSCAN (c))
14233 error_at (OMP_CLAUSE_LOCATION (c),
14234 "%<inscan%> %<reduction%> clause with array "
14240 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14241 if (t == error_mark_node)
14247 c_mark_addressable (t);
14248 type = TREE_TYPE (t);
14249 if (TREE_CODE (t) == MEM_REF)
14250 type = TREE_TYPE (type);
14251 if (TREE_CODE (type) == ARRAY_TYPE)
14253 tree oatype = type;
14254 gcc_assert (TREE_CODE (t) != MEM_REF);
14255 while (TREE_CODE (type) == ARRAY_TYPE)
14256 type = TREE_TYPE (type);
14257 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14259 error_at (OMP_CLAUSE_LOCATION (c),
14260 "%qD in %<reduction%> clause is a zero size array",
14265 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14266 TYPE_SIZE_UNIT (type));
14267 if (integer_zerop (size))
14269 error_at (OMP_CLAUSE_LOCATION (c),
14270 "%qD in %<reduction%> clause is a zero size array",
14275 size = size_binop (MINUS_EXPR, size, size_one_node);
14276 size = save_expr (size);
14277 tree index_type = build_index_type (size);
14278 tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14280 atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14281 tree ptype = build_pointer_type (type);
14282 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14283 t = build_fold_addr_expr (t);
14284 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14285 OMP_CLAUSE_DECL (c) = t;
14287 if (TYPE_ATOMIC (type))
14289 error_at (OMP_CLAUSE_LOCATION (c),
14290 "%<_Atomic%> %qE in %<reduction%> clause", t);
14294 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14295 || OMP_CLAUSE_REDUCTION_TASK (c))
14297 /* Disallow zero sized or potentially zero sized task
14299 if (integer_zerop (TYPE_SIZE_UNIT (type)))
14301 error_at (OMP_CLAUSE_LOCATION (c),
14302 "zero sized type %qT in %qs clause", type,
14303 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14307 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14309 error_at (OMP_CLAUSE_LOCATION (c),
14310 "variable sized type %qT in %qs clause", type,
14311 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14316 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14317 && (FLOAT_TYPE_P (type)
14318 || TREE_CODE (type) == COMPLEX_TYPE))
14320 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14321 const char *r_name = NULL;
14328 case TRUTH_ANDIF_EXPR:
14329 case TRUTH_ORIF_EXPR:
14332 if (TREE_CODE (type) == COMPLEX_TYPE)
14336 if (TREE_CODE (type) == COMPLEX_TYPE)
14349 gcc_unreachable ();
14353 error_at (OMP_CLAUSE_LOCATION (c),
14354 "%qE has invalid type for %<reduction(%s)%>",
14360 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14362 error_at (OMP_CLAUSE_LOCATION (c),
14363 "user defined reduction not found for %qE", t);
14367 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14369 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14370 type = TYPE_MAIN_VARIANT (type);
14371 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14372 VAR_DECL, NULL_TREE, type);
14373 tree decl_placeholder = NULL_TREE;
14374 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14375 DECL_ARTIFICIAL (placeholder) = 1;
14376 DECL_IGNORED_P (placeholder) = 1;
14377 if (TREE_CODE (t) == MEM_REF)
14379 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14380 VAR_DECL, NULL_TREE, type);
14381 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14382 DECL_ARTIFICIAL (decl_placeholder) = 1;
14383 DECL_IGNORED_P (decl_placeholder) = 1;
14385 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14386 c_mark_addressable (placeholder);
14387 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14388 c_mark_addressable (decl_placeholder ? decl_placeholder
14389 : OMP_CLAUSE_DECL (c));
14390 OMP_CLAUSE_REDUCTION_MERGE (c)
14391 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14392 TREE_VEC_ELT (list, 0),
14393 TREE_VEC_ELT (list, 1),
14394 decl_placeholder ? decl_placeholder
14395 : OMP_CLAUSE_DECL (c), placeholder);
14396 OMP_CLAUSE_REDUCTION_MERGE (c)
14397 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14398 void_type_node, NULL_TREE,
14399 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14400 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14401 if (TREE_VEC_LENGTH (list) == 6)
14403 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14404 c_mark_addressable (decl_placeholder ? decl_placeholder
14405 : OMP_CLAUSE_DECL (c));
14406 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14407 c_mark_addressable (placeholder);
14408 tree init = TREE_VEC_ELT (list, 5);
14409 if (init == error_mark_node)
14410 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14411 OMP_CLAUSE_REDUCTION_INIT (c)
14412 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14413 TREE_VEC_ELT (list, 3),
14414 decl_placeholder ? decl_placeholder
14415 : OMP_CLAUSE_DECL (c), placeholder);
14416 if (TREE_VEC_ELT (list, 5) == error_mark_node)
14418 tree v = decl_placeholder ? decl_placeholder : t;
14419 OMP_CLAUSE_REDUCTION_INIT (c)
14420 = build2 (INIT_EXPR, TREE_TYPE (v), v,
14421 OMP_CLAUSE_REDUCTION_INIT (c));
14423 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14424 c_find_omp_placeholder_r,
14425 placeholder, NULL))
14426 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14431 tree v = decl_placeholder ? decl_placeholder : t;
14432 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14433 init = build_constructor (TREE_TYPE (v), NULL);
14435 init = fold_convert (TREE_TYPE (v), integer_zero_node);
14436 OMP_CLAUSE_REDUCTION_INIT (c)
14437 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14439 OMP_CLAUSE_REDUCTION_INIT (c)
14440 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14441 void_type_node, NULL_TREE,
14442 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14443 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14445 if (TREE_CODE (t) == MEM_REF)
14447 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14448 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14451 sorry ("variable length element type in array "
14452 "%<reduction%> clause");
14456 t = TREE_OPERAND (t, 0);
14457 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14458 t = TREE_OPERAND (t, 0);
14459 if (TREE_CODE (t) == ADDR_EXPR)
14460 t = TREE_OPERAND (t, 0);
14462 goto check_dup_generic_t;
14464 case OMP_CLAUSE_COPYPRIVATE:
14465 copyprivate_seen = true;
14468 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14469 "%<nowait%> clause must not be used together "
14470 "with %<copyprivate%>");
14471 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14472 nowait_clause = NULL;
14474 goto check_dup_generic;
14476 case OMP_CLAUSE_COPYIN:
14477 t = OMP_CLAUSE_DECL (c);
14478 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14480 error_at (OMP_CLAUSE_LOCATION (c),
14481 "%qE must be %<threadprivate%> for %<copyin%>", t);
14485 goto check_dup_generic;
14487 case OMP_CLAUSE_LINEAR:
14488 if (ort != C_ORT_OMP_DECLARE_SIMD)
14489 need_implicitly_determined = true;
14490 t = OMP_CLAUSE_DECL (c);
14491 if (ort != C_ORT_OMP_DECLARE_SIMD
14492 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14493 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14495 error_at (OMP_CLAUSE_LOCATION (c),
14496 "modifier should not be specified in %<linear%> "
14497 "clause on %<simd%> or %<for%> constructs when not "
14498 "using OpenMP 5.2 modifiers");
14499 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14501 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14502 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14504 error_at (OMP_CLAUSE_LOCATION (c),
14505 "linear clause applied to non-integral non-pointer "
14506 "variable with type %qT", TREE_TYPE (t));
14510 if (TYPE_ATOMIC (TREE_TYPE (t)))
14512 error_at (OMP_CLAUSE_LOCATION (c),
14513 "%<_Atomic%> %qD in %<linear%> clause", t);
14517 if (ort == C_ORT_OMP_DECLARE_SIMD)
14519 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14520 if (TREE_CODE (s) == PARM_DECL)
14522 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14523 /* map_head bitmap is used as uniform_head if
14525 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14526 linear_variable_step_check = true;
14527 goto check_dup_generic;
14529 if (TREE_CODE (s) != INTEGER_CST)
14531 error_at (OMP_CLAUSE_LOCATION (c),
14532 "%<linear%> clause step %qE is neither constant "
14533 "nor a parameter", s);
14538 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14540 tree s = OMP_CLAUSE_LINEAR_STEP (c);
14541 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14542 OMP_CLAUSE_DECL (c), s);
14543 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14544 sizetype, fold_convert (sizetype, s),
14546 (sizetype, OMP_CLAUSE_DECL (c)));
14547 if (s == error_mark_node)
14549 OMP_CLAUSE_LINEAR_STEP (c) = s;
14552 OMP_CLAUSE_LINEAR_STEP (c)
14553 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14554 goto check_dup_generic;
14557 t = OMP_CLAUSE_DECL (c);
14558 check_dup_generic_t:
14559 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14561 error_at (OMP_CLAUSE_LOCATION (c),
14562 "%qE is not a variable in clause %qs", t,
14563 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14566 else if ((ort == C_ORT_ACC
14567 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14568 || (ort == C_ORT_OMP
14569 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14570 || (OMP_CLAUSE_CODE (c)
14571 == OMP_CLAUSE_USE_DEVICE_ADDR)))
14572 || (ort == C_ORT_OMP_TARGET
14573 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14575 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14576 && (bitmap_bit_p (&generic_head, DECL_UID (t))
14577 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14579 error_at (OMP_CLAUSE_LOCATION (c),
14580 "%qD appears more than once in data-sharing "
14585 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14586 target_in_reduction_seen = true;
14587 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14589 error_at (OMP_CLAUSE_LOCATION (c),
14591 ? "%qD appears more than once in reduction clauses"
14592 : "%qD appears more than once in data clauses",
14597 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14599 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14600 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14601 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14602 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14604 error_at (OMP_CLAUSE_LOCATION (c),
14605 "%qE appears more than once in data clauses", t);
14608 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14609 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
14610 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
14611 && bitmap_bit_p (&map_head, DECL_UID (t)))
14613 if (ort == C_ORT_ACC)
14614 error_at (OMP_CLAUSE_LOCATION (c),
14615 "%qD appears more than once in data clauses", t);
14617 error_at (OMP_CLAUSE_LOCATION (c),
14618 "%qD appears both in data and map clauses", t);
14622 bitmap_set_bit (&generic_head, DECL_UID (t));
14625 case OMP_CLAUSE_FIRSTPRIVATE:
14626 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14629 implicit_moved = true;
14630 /* Move firstprivate and map clauses with
14631 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14633 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14634 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14636 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14637 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14640 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14641 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14643 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14644 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14647 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14648 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14651 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14657 t = OMP_CLAUSE_DECL (c);
14658 need_complete = true;
14659 need_implicitly_determined = true;
14660 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14662 error_at (OMP_CLAUSE_LOCATION (c),
14663 "%qE is not a variable in clause %<firstprivate%>", t);
14666 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14667 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14668 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14670 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14671 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14672 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14674 error_at (OMP_CLAUSE_LOCATION (c),
14675 "%qE appears more than once in data clauses", t);
14678 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14680 if (ort == C_ORT_ACC)
14681 error_at (OMP_CLAUSE_LOCATION (c),
14682 "%qD appears more than once in data clauses", t);
14683 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14684 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14685 /* Silently drop the clause. */;
14687 error_at (OMP_CLAUSE_LOCATION (c),
14688 "%qD appears both in data and map clauses", t);
14692 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14695 case OMP_CLAUSE_LASTPRIVATE:
14696 t = OMP_CLAUSE_DECL (c);
14697 need_complete = true;
14698 need_implicitly_determined = true;
14699 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14701 error_at (OMP_CLAUSE_LOCATION (c),
14702 "%qE is not a variable in clause %<lastprivate%>", t);
14705 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14706 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14708 error_at (OMP_CLAUSE_LOCATION (c),
14709 "%qE appears more than once in data clauses", t);
14713 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14716 case OMP_CLAUSE_ALIGNED:
14717 t = OMP_CLAUSE_DECL (c);
14718 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14720 error_at (OMP_CLAUSE_LOCATION (c),
14721 "%qE is not a variable in %<aligned%> clause", t);
14724 else if (!POINTER_TYPE_P (TREE_TYPE (t))
14725 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14727 error_at (OMP_CLAUSE_LOCATION (c),
14728 "%qE in %<aligned%> clause is neither a pointer nor "
14732 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14734 error_at (OMP_CLAUSE_LOCATION (c),
14735 "%<_Atomic%> %qD in %<aligned%> clause", t);
14739 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14741 error_at (OMP_CLAUSE_LOCATION (c),
14742 "%qE appears more than once in %<aligned%> clauses",
14747 bitmap_set_bit (&aligned_head, DECL_UID (t));
14750 case OMP_CLAUSE_NONTEMPORAL:
14751 t = OMP_CLAUSE_DECL (c);
14752 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14754 error_at (OMP_CLAUSE_LOCATION (c),
14755 "%qE is not a variable in %<nontemporal%> clause", t);
14758 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14760 error_at (OMP_CLAUSE_LOCATION (c),
14761 "%qE appears more than once in %<nontemporal%> "
14766 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14769 case OMP_CLAUSE_ALLOCATE:
14770 t = OMP_CLAUSE_DECL (c);
14771 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14773 error_at (OMP_CLAUSE_LOCATION (c),
14774 "%qE is not a variable in %<allocate%> clause", t);
14777 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14779 warning_at (OMP_CLAUSE_LOCATION (c), 0,
14780 "%qE appears more than once in %<allocate%> clauses",
14786 bitmap_set_bit (&aligned_head, DECL_UID (t));
14787 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14788 allocate_seen = true;
14792 case OMP_CLAUSE_DEPEND:
14793 t = OMP_CLAUSE_DECL (c);
14794 if (t == NULL_TREE)
14796 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14797 == OMP_CLAUSE_DEPEND_SOURCE);
14800 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14802 gcc_assert (TREE_CODE (t) == TREE_LIST);
14803 for (; t; t = TREE_CHAIN (t))
14805 tree decl = TREE_VALUE (t);
14806 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14808 tree offset = TREE_PURPOSE (t);
14809 bool neg = wi::neg_p (wi::to_wide (offset));
14810 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14811 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14812 neg ? MINUS_EXPR : PLUS_EXPR,
14814 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14816 fold_convert (sizetype, t2),
14817 fold_convert (sizetype, decl));
14818 if (t2 == error_mark_node)
14823 TREE_PURPOSE (t) = t2;
14829 case OMP_CLAUSE_AFFINITY:
14830 t = OMP_CLAUSE_DECL (c);
14831 if (TREE_CODE (t) == TREE_LIST
14832 && TREE_PURPOSE (t)
14833 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14835 if (TREE_PURPOSE (t) != last_iterators)
14836 last_iterators_remove
14837 = c_omp_finish_iterators (TREE_PURPOSE (t));
14838 last_iterators = TREE_PURPOSE (t);
14839 t = TREE_VALUE (t);
14840 if (last_iterators_remove)
14841 t = error_mark_node;
14844 last_iterators = NULL_TREE;
14845 if (TREE_CODE (t) == TREE_LIST)
14847 if (handle_omp_array_sections (c, ort))
14849 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14850 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14852 error_at (OMP_CLAUSE_LOCATION (c),
14853 "%<depend%> clause with %<depobj%> dependence "
14854 "type on array section");
14859 if (t == error_mark_node)
14861 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14862 && t == ridpointers[RID_OMP_ALL_MEMORY])
14864 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
14865 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
14867 error_at (OMP_CLAUSE_LOCATION (c),
14868 "%<omp_all_memory%> used with %<depend%> kind "
14869 "other than %<out%> or %<inout%>");
14873 else if (!lvalue_p (t))
14875 error_at (OMP_CLAUSE_LOCATION (c),
14876 "%qE is not lvalue expression nor array section in "
14878 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14881 else if (TREE_CODE (t) == COMPONENT_REF
14882 && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14884 gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14885 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14886 error_at (OMP_CLAUSE_LOCATION (c),
14887 "bit-field %qE in %qs clause", t,
14888 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14891 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14892 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14894 if (!c_omp_depend_t_p (TREE_TYPE (t)))
14896 error_at (OMP_CLAUSE_LOCATION (c),
14897 "%qE does not have %<omp_depend_t%> type in "
14898 "%<depend%> clause with %<depobj%> dependence "
14903 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14904 && c_omp_depend_t_p (TREE_TYPE (t)))
14906 error_at (OMP_CLAUSE_LOCATION (c),
14907 "%qE should not have %<omp_depend_t%> type in "
14908 "%<depend%> clause with dependence type other than "
14914 if (t == ridpointers[RID_OMP_ALL_MEMORY])
14915 t = null_pointer_node;
14918 tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
14919 ADDR_EXPR, t, false);
14920 if (addr == error_mark_node)
14925 t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14927 if (t == error_mark_node)
14933 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14934 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14935 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14937 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14939 OMP_CLAUSE_DECL (c) = t;
14943 case OMP_CLAUSE_MAP:
14944 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14945 goto move_implicit;
14947 case OMP_CLAUSE_TO:
14948 case OMP_CLAUSE_FROM:
14949 case OMP_CLAUSE__CACHE_:
14950 t = OMP_CLAUSE_DECL (c);
14951 if (TREE_CODE (t) == TREE_LIST)
14953 if (handle_omp_array_sections (c, ort))
14957 t = OMP_CLAUSE_DECL (c);
14958 if (!omp_mappable_type (TREE_TYPE (t)))
14960 error_at (OMP_CLAUSE_LOCATION (c),
14961 "array section does not have mappable type "
14963 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14966 else if (TYPE_ATOMIC (TREE_TYPE (t)))
14968 error_at (OMP_CLAUSE_LOCATION (c),
14969 "%<_Atomic%> %qE in %qs clause", t,
14970 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14973 while (TREE_CODE (t) == ARRAY_REF)
14974 t = TREE_OPERAND (t, 0);
14975 if (TREE_CODE (t) == COMPONENT_REF
14976 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14980 t = TREE_OPERAND (t, 0);
14981 if (TREE_CODE (t) == MEM_REF
14982 || TREE_CODE (t) == INDIRECT_REF)
14984 t = TREE_OPERAND (t, 0);
14986 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14987 t = TREE_OPERAND (t, 0);
14990 while (TREE_CODE (t) == COMPONENT_REF
14991 || TREE_CODE (t) == ARRAY_REF);
14993 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14994 && OMP_CLAUSE_MAP_IMPLICIT (c)
14995 && (bitmap_bit_p (&map_head, DECL_UID (t))
14996 || bitmap_bit_p (&map_field_head, DECL_UID (t))
14997 || bitmap_bit_p (&map_firstprivate_head,
15003 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15005 if (bitmap_bit_p (&map_head, DECL_UID (t)))
15007 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15008 error_at (OMP_CLAUSE_LOCATION (c),
15009 "%qD appears more than once in motion "
15011 else if (ort == C_ORT_ACC)
15012 error_at (OMP_CLAUSE_LOCATION (c),
15013 "%qD appears more than once in data "
15016 error_at (OMP_CLAUSE_LOCATION (c),
15017 "%qD appears more than once in map "
15023 bitmap_set_bit (&map_head, DECL_UID (t));
15024 bitmap_set_bit (&map_field_head, DECL_UID (t));
15028 if (c_oacc_check_attachments (c))
15030 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15031 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15032 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15033 /* In this case, we have a single array element which is a
15034 pointer, and we already set OMP_CLAUSE_SIZE in
15035 handle_omp_array_sections above. For attach/detach clauses,
15036 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15038 OMP_CLAUSE_SIZE (c) = size_zero_node;
15041 if (t == error_mark_node)
15046 /* OpenACC attach / detach clauses must be pointers. */
15047 if (c_oacc_check_attachments (c))
15052 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15053 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15054 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15055 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15056 bias) to zero here, so it is not set erroneously to the pointer
15057 size later on in gimplify.cc. */
15058 OMP_CLAUSE_SIZE (c) = size_zero_node;
15059 while (TREE_CODE (t) == INDIRECT_REF
15060 || TREE_CODE (t) == ARRAY_REF)
15062 t = TREE_OPERAND (t, 0);
15064 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15065 t = TREE_OPERAND (t, 0);
15067 while (TREE_CODE (t) == COMPOUND_EXPR)
15069 t = TREE_OPERAND (t, 1);
15072 indir_component_ref_p = false;
15073 if (TREE_CODE (t) == COMPONENT_REF
15074 && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15075 || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15076 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15078 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15079 indir_component_ref_p = true;
15081 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15082 t = TREE_OPERAND (t, 0);
15085 if (TREE_CODE (t) == COMPONENT_REF
15086 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15088 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15090 error_at (OMP_CLAUSE_LOCATION (c),
15091 "bit-field %qE in %qs clause",
15092 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15095 else if (!omp_mappable_type (TREE_TYPE (t)))
15097 error_at (OMP_CLAUSE_LOCATION (c),
15098 "%qE does not have a mappable type in %qs clause",
15099 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15102 else if (TYPE_ATOMIC (TREE_TYPE (t)))
15104 error_at (OMP_CLAUSE_LOCATION (c),
15105 "%<_Atomic%> %qE in %qs clause", t,
15106 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15109 while (TREE_CODE (t) == COMPONENT_REF)
15111 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15114 error_at (OMP_CLAUSE_LOCATION (c),
15115 "%qE is a member of a union", t);
15119 t = TREE_OPERAND (t, 0);
15120 if (TREE_CODE (t) == MEM_REF)
15122 if (maybe_ne (mem_ref_offset (t), 0))
15123 error_at (OMP_CLAUSE_LOCATION (c),
15124 "cannot dereference %qE in %qs clause", t,
15125 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15127 t = TREE_OPERAND (t, 0);
15129 while (TREE_CODE (t) == MEM_REF
15130 || TREE_CODE (t) == INDIRECT_REF
15131 || TREE_CODE (t) == ARRAY_REF)
15133 t = TREE_OPERAND (t, 0);
15135 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15136 t = TREE_OPERAND (t, 0);
15141 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15143 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15144 || (ort != C_ORT_ACC
15145 && bitmap_bit_p (&map_head, DECL_UID (t))))
15149 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15151 error_at (OMP_CLAUSE_LOCATION (c),
15152 "%qE is not a variable in %qs clause", t,
15153 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15156 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15158 error_at (OMP_CLAUSE_LOCATION (c),
15159 "%qD is threadprivate variable in %qs clause", t,
15160 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15163 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15164 || (OMP_CLAUSE_MAP_KIND (c)
15165 != GOMP_MAP_FIRSTPRIVATE_POINTER))
15166 && !indir_component_ref_p
15167 && !c_mark_addressable (t))
15169 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15170 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15171 || (OMP_CLAUSE_MAP_KIND (c)
15172 == GOMP_MAP_FIRSTPRIVATE_POINTER)
15173 || (OMP_CLAUSE_MAP_KIND (c)
15174 == GOMP_MAP_FORCE_DEVICEPTR)))
15175 && t == OMP_CLAUSE_DECL (c)
15176 && !omp_mappable_type (TREE_TYPE (t)))
15178 error_at (OMP_CLAUSE_LOCATION (c),
15179 "%qD does not have a mappable type in %qs clause", t,
15180 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15183 else if (TREE_TYPE (t) == error_mark_node)
15185 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15187 error_at (OMP_CLAUSE_LOCATION (c),
15188 "%<_Atomic%> %qE in %qs clause", t,
15189 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15192 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15193 && OMP_CLAUSE_MAP_IMPLICIT (c)
15194 && (bitmap_bit_p (&map_head, DECL_UID (t))
15195 || bitmap_bit_p (&map_field_head, DECL_UID (t))
15196 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15198 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15199 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15201 if (bitmap_bit_p (&generic_head, DECL_UID (t))
15202 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15203 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15205 error_at (OMP_CLAUSE_LOCATION (c),
15206 "%qD appears more than once in data clauses", t);
15209 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15210 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15212 if (ort == C_ORT_ACC)
15213 error_at (OMP_CLAUSE_LOCATION (c),
15214 "%qD appears more than once in data clauses", t);
15216 error_at (OMP_CLAUSE_LOCATION (c),
15217 "%qD appears both in data and map clauses", t);
15221 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15223 else if (bitmap_bit_p (&map_head, DECL_UID (t))
15224 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15226 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15227 error_at (OMP_CLAUSE_LOCATION (c),
15228 "%qD appears more than once in motion clauses", t);
15229 else if (ort == C_ORT_ACC)
15230 error_at (OMP_CLAUSE_LOCATION (c),
15231 "%qD appears more than once in data clauses", t);
15233 error_at (OMP_CLAUSE_LOCATION (c),
15234 "%qD appears more than once in map clauses", t);
15237 else if (ort == C_ORT_ACC
15238 && bitmap_bit_p (&generic_head, DECL_UID (t)))
15240 error_at (OMP_CLAUSE_LOCATION (c),
15241 "%qD appears more than once in data clauses", t);
15244 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15245 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15247 if (ort == C_ORT_ACC)
15248 error_at (OMP_CLAUSE_LOCATION (c),
15249 "%qD appears more than once in data clauses", t);
15251 error_at (OMP_CLAUSE_LOCATION (c),
15252 "%qD appears both in data and map clauses", t);
15257 bitmap_set_bit (&map_head, DECL_UID (t));
15258 if (t != OMP_CLAUSE_DECL (c)
15259 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15260 bitmap_set_bit (&map_field_head, DECL_UID (t));
15264 case OMP_CLAUSE_ENTER:
15265 case OMP_CLAUSE_LINK:
15266 t = OMP_CLAUSE_DECL (c);
15268 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15269 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15270 && OMP_CLAUSE_ENTER_TO (c))
15272 if (TREE_CODE (t) == FUNCTION_DECL
15273 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15275 else if (!VAR_P (t))
15277 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15278 error_at (OMP_CLAUSE_LOCATION (c),
15279 "%qE is neither a variable nor a function name in "
15280 "clause %qs", t, cname);
15282 error_at (OMP_CLAUSE_LOCATION (c),
15283 "%qE is not a variable in clause %qs", t, cname);
15286 else if (DECL_THREAD_LOCAL_P (t))
15288 error_at (OMP_CLAUSE_LOCATION (c),
15289 "%qD is threadprivate variable in %qs clause", t,
15293 else if (!omp_mappable_type (TREE_TYPE (t)))
15295 error_at (OMP_CLAUSE_LOCATION (c),
15296 "%qD does not have a mappable type in %qs clause", t,
15302 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15304 error_at (OMP_CLAUSE_LOCATION (c),
15305 "%qE appears more than once on the same "
15306 "%<declare target%> directive", t);
15310 bitmap_set_bit (&generic_head, DECL_UID (t));
15313 case OMP_CLAUSE_UNIFORM:
15314 t = OMP_CLAUSE_DECL (c);
15315 if (TREE_CODE (t) != PARM_DECL)
15318 error_at (OMP_CLAUSE_LOCATION (c),
15319 "%qD is not an argument in %<uniform%> clause", t);
15321 error_at (OMP_CLAUSE_LOCATION (c),
15322 "%qE is not an argument in %<uniform%> clause", t);
15326 /* map_head bitmap is used as uniform_head if declare_simd. */
15327 bitmap_set_bit (&map_head, DECL_UID (t));
15328 goto check_dup_generic;
15330 case OMP_CLAUSE_IS_DEVICE_PTR:
15331 case OMP_CLAUSE_USE_DEVICE_PTR:
15332 t = OMP_CLAUSE_DECL (c);
15333 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15334 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15335 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15337 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15338 && ort != C_ORT_ACC)
15340 error_at (OMP_CLAUSE_LOCATION (c),
15341 "%qs variable is not a pointer",
15342 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15345 else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15347 error_at (OMP_CLAUSE_LOCATION (c),
15348 "%qs variable is neither a pointer nor an array",
15349 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15353 goto check_dup_generic;
15355 case OMP_CLAUSE_HAS_DEVICE_ADDR:
15356 t = OMP_CLAUSE_DECL (c);
15357 if (TREE_CODE (t) == TREE_LIST)
15359 if (handle_omp_array_sections (c, ort))
15363 t = OMP_CLAUSE_DECL (c);
15364 while (TREE_CODE (t) == ARRAY_REF)
15365 t = TREE_OPERAND (t, 0);
15368 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15369 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15370 c_mark_addressable (t);
15371 goto check_dup_generic_t;
15373 case OMP_CLAUSE_USE_DEVICE_ADDR:
15374 t = OMP_CLAUSE_DECL (c);
15375 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15376 c_mark_addressable (t);
15377 goto check_dup_generic;
15379 case OMP_CLAUSE_NOWAIT:
15380 if (copyprivate_seen)
15382 error_at (OMP_CLAUSE_LOCATION (c),
15383 "%<nowait%> clause must not be used together "
15384 "with %<copyprivate%>");
15388 nowait_clause = pc;
15389 pc = &OMP_CLAUSE_CHAIN (c);
15392 case OMP_CLAUSE_ORDER:
15393 if (ordered_clause)
15395 error_at (OMP_CLAUSE_LOCATION (c),
15396 "%<order%> clause must not be used together "
15397 "with %<ordered%>");
15401 else if (order_clause)
15403 /* Silently remove duplicates. */
15408 pc = &OMP_CLAUSE_CHAIN (c);
15411 case OMP_CLAUSE_DETACH:
15412 t = OMP_CLAUSE_DECL (c);
15415 error_at (OMP_CLAUSE_LOCATION (c),
15416 "too many %qs clauses on a task construct",
15422 pc = &OMP_CLAUSE_CHAIN (c);
15423 c_mark_addressable (t);
15426 case OMP_CLAUSE_IF:
15427 case OMP_CLAUSE_NUM_THREADS:
15428 case OMP_CLAUSE_NUM_TEAMS:
15429 case OMP_CLAUSE_THREAD_LIMIT:
15430 case OMP_CLAUSE_DEFAULT:
15431 case OMP_CLAUSE_UNTIED:
15432 case OMP_CLAUSE_COLLAPSE:
15433 case OMP_CLAUSE_FINAL:
15434 case OMP_CLAUSE_DEVICE:
15435 case OMP_CLAUSE_DIST_SCHEDULE:
15436 case OMP_CLAUSE_PARALLEL:
15437 case OMP_CLAUSE_FOR:
15438 case OMP_CLAUSE_SECTIONS:
15439 case OMP_CLAUSE_TASKGROUP:
15440 case OMP_CLAUSE_PROC_BIND:
15441 case OMP_CLAUSE_DEVICE_TYPE:
15442 case OMP_CLAUSE_PRIORITY:
15443 case OMP_CLAUSE_GRAINSIZE:
15444 case OMP_CLAUSE_NUM_TASKS:
15445 case OMP_CLAUSE_THREADS:
15446 case OMP_CLAUSE_SIMD:
15447 case OMP_CLAUSE_HINT:
15448 case OMP_CLAUSE_FILTER:
15449 case OMP_CLAUSE_DEFAULTMAP:
15450 case OMP_CLAUSE_BIND:
15451 case OMP_CLAUSE_NUM_GANGS:
15452 case OMP_CLAUSE_NUM_WORKERS:
15453 case OMP_CLAUSE_VECTOR_LENGTH:
15454 case OMP_CLAUSE_ASYNC:
15455 case OMP_CLAUSE_WAIT:
15456 case OMP_CLAUSE_AUTO:
15457 case OMP_CLAUSE_INDEPENDENT:
15458 case OMP_CLAUSE_SEQ:
15459 case OMP_CLAUSE_GANG:
15460 case OMP_CLAUSE_WORKER:
15461 case OMP_CLAUSE_VECTOR:
15462 case OMP_CLAUSE_TILE:
15463 case OMP_CLAUSE_IF_PRESENT:
15464 case OMP_CLAUSE_FINALIZE:
15465 case OMP_CLAUSE_NOHOST:
15466 pc = &OMP_CLAUSE_CHAIN (c);
15469 case OMP_CLAUSE_MERGEABLE:
15470 mergeable_seen = true;
15471 pc = &OMP_CLAUSE_CHAIN (c);
15474 case OMP_CLAUSE_NOGROUP:
15476 pc = &OMP_CLAUSE_CHAIN (c);
15479 case OMP_CLAUSE_SCHEDULE:
15480 schedule_clause = c;
15481 pc = &OMP_CLAUSE_CHAIN (c);
15484 case OMP_CLAUSE_ORDERED:
15485 ordered_clause = c;
15488 error_at (OMP_CLAUSE_LOCATION (*order_clause),
15489 "%<order%> clause must not be used together "
15490 "with %<ordered%>");
15491 *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15492 order_clause = NULL;
15494 pc = &OMP_CLAUSE_CHAIN (c);
15497 case OMP_CLAUSE_SAFELEN:
15499 pc = &OMP_CLAUSE_CHAIN (c);
15501 case OMP_CLAUSE_SIMDLEN:
15503 pc = &OMP_CLAUSE_CHAIN (c);
15506 case OMP_CLAUSE_INBRANCH:
15507 case OMP_CLAUSE_NOTINBRANCH:
15510 error_at (OMP_CLAUSE_LOCATION (c),
15511 "%<inbranch%> clause is incompatible with "
15512 "%<notinbranch%>");
15516 branch_seen = true;
15517 pc = &OMP_CLAUSE_CHAIN (c);
15520 case OMP_CLAUSE_INCLUSIVE:
15521 case OMP_CLAUSE_EXCLUSIVE:
15522 need_complete = true;
15523 need_implicitly_determined = true;
15524 t = OMP_CLAUSE_DECL (c);
15525 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15527 error_at (OMP_CLAUSE_LOCATION (c),
15528 "%qE is not a variable in clause %qs", t,
15529 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15535 gcc_unreachable ();
15540 t = OMP_CLAUSE_DECL (c);
15544 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15545 if (t == error_mark_node)
15549 if (need_implicitly_determined)
15551 const char *share_name = NULL;
15553 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15554 share_name = "threadprivate";
15555 else switch (c_omp_predetermined_sharing (t))
15557 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15559 case OMP_CLAUSE_DEFAULT_SHARED:
15560 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15561 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15562 && c_omp_predefined_variable (t))
15563 /* The __func__ variable and similar function-local
15564 predefined variables may be listed in a shared or
15565 firstprivate clause. */
15567 share_name = "shared";
15569 case OMP_CLAUSE_DEFAULT_PRIVATE:
15570 share_name = "private";
15573 gcc_unreachable ();
15577 error_at (OMP_CLAUSE_LOCATION (c),
15578 "%qE is predetermined %qs for %qs",
15580 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15583 else if (TREE_READONLY (t)
15584 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15585 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15587 error_at (OMP_CLAUSE_LOCATION (c),
15588 "%<const%> qualified %qE may appear only in "
15589 "%<shared%> or %<firstprivate%> clauses", t);
15596 *pc = OMP_CLAUSE_CHAIN (c);
15598 pc = &OMP_CLAUSE_CHAIN (c);
15603 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15604 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15606 error_at (OMP_CLAUSE_LOCATION (simdlen),
15607 "%<simdlen%> clause value is bigger than "
15608 "%<safelen%> clause value");
15609 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15610 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15615 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15616 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15618 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15619 "%<nonmonotonic%> schedule modifier specified together "
15620 "with %<ordered%> clause");
15621 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15622 = (enum omp_clause_schedule_kind)
15623 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15624 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15627 if (reduction_seen < 0 && ordered_clause)
15629 error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15630 "%qs clause specified together with %<inscan%> "
15631 "%<reduction%> clause", "ordered");
15632 reduction_seen = -2;
15635 if (reduction_seen < 0 && schedule_clause)
15637 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15638 "%qs clause specified together with %<inscan%> "
15639 "%<reduction%> clause", "schedule");
15640 reduction_seen = -2;
15643 if (linear_variable_step_check
15644 || reduction_seen == -2
15646 || target_in_reduction_seen)
15647 for (pc = &clauses, c = clauses; c ; c = *pc)
15649 bool remove = false;
15651 switch (OMP_CLAUSE_CODE (c))
15653 case OMP_CLAUSE_REDUCTION:
15654 case OMP_CLAUSE_IN_REDUCTION:
15655 case OMP_CLAUSE_TASK_REDUCTION:
15656 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15658 t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15659 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15660 t = TREE_OPERAND (t, 0);
15661 if (TREE_CODE (t) == ADDR_EXPR
15662 || TREE_CODE (t) == INDIRECT_REF)
15663 t = TREE_OPERAND (t, 0);
15665 bitmap_clear_bit (&aligned_head, DECL_UID (t));
15669 case OMP_CLAUSE_PRIVATE:
15670 case OMP_CLAUSE_FIRSTPRIVATE:
15671 case OMP_CLAUSE_LASTPRIVATE:
15672 case OMP_CLAUSE_LINEAR:
15673 if (DECL_P (OMP_CLAUSE_DECL (c)))
15674 bitmap_clear_bit (&aligned_head,
15675 DECL_UID (OMP_CLAUSE_DECL (c)));
15680 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15681 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15682 && !bitmap_bit_p (&map_head,
15683 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15685 error_at (OMP_CLAUSE_LOCATION (c),
15686 "%<linear%> clause step is a parameter %qD not "
15687 "specified in %<uniform%> clause",
15688 OMP_CLAUSE_LINEAR_STEP (c));
15691 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15692 && reduction_seen == -2)
15693 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15694 if (target_in_reduction_seen
15695 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15697 tree t = OMP_CLAUSE_DECL (c);
15698 while (handled_component_p (t)
15699 || TREE_CODE (t) == INDIRECT_REF
15700 || TREE_CODE (t) == ADDR_EXPR
15701 || TREE_CODE (t) == MEM_REF
15702 || TREE_CODE (t) == NON_LVALUE_EXPR)
15703 t = TREE_OPERAND (t, 0);
15705 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15706 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15710 *pc = OMP_CLAUSE_CHAIN (c);
15712 pc = &OMP_CLAUSE_CHAIN (c);
15716 for (pc = &clauses, c = clauses; c ; c = *pc)
15718 bool remove = false;
15719 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15720 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15721 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15723 error_at (OMP_CLAUSE_LOCATION (c),
15724 "%qD specified in %<allocate%> clause but not in "
15725 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15729 *pc = OMP_CLAUSE_CHAIN (c);
15731 pc = &OMP_CLAUSE_CHAIN (c);
15734 if (nogroup_seen && reduction_seen)
15736 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15737 "%<nogroup%> clause must not be used together with "
15738 "%<reduction%> clause");
15739 *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15744 if (mergeable_seen)
15746 error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15747 "%<detach%> clause must not be used together with "
15748 "%<mergeable%> clause");
15749 *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15753 tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15755 for (pc = &clauses, c = clauses; c ; c = *pc)
15757 bool remove = false;
15758 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15759 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15760 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15761 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15762 && OMP_CLAUSE_DECL (c) == detach_decl)
15764 error_at (OMP_CLAUSE_LOCATION (c),
15765 "the event handle of a %<detach%> clause "
15766 "should not be in a data-sharing clause");
15770 *pc = OMP_CLAUSE_CHAIN (c);
15772 pc = &OMP_CLAUSE_CHAIN (c);
15777 bitmap_obstack_release (NULL);
15781 /* Return code to initialize DST with a copy constructor from SRC.
15782 C doesn't have copy constructors nor assignment operators, only for
15783 _Atomic vars we need to perform __atomic_load from src into a temporary
15784 followed by __atomic_store of the temporary to dst. */
15787 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15789 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15790 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15792 location_t loc = OMP_CLAUSE_LOCATION (clause);
15793 tree type = TREE_TYPE (dst);
15794 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15795 tree tmp = create_tmp_var (nonatomic_type);
15796 tree tmp_addr = build_fold_addr_expr (tmp);
15797 TREE_ADDRESSABLE (tmp) = 1;
15798 suppress_warning (tmp);
15799 tree src_addr = build_fold_addr_expr (src);
15800 tree dst_addr = build_fold_addr_expr (dst);
15801 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15802 vec<tree, va_gc> *params;
15803 /* Expansion of a generic atomic load may require an addition
15804 element, so allocate enough to prevent a resize. */
15805 vec_alloc (params, 4);
15807 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
15808 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15809 params->quick_push (src_addr);
15810 params->quick_push (tmp_addr);
15811 params->quick_push (seq_cst);
15812 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15814 vec_alloc (params, 4);
15816 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
15817 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15818 params->quick_push (dst_addr);
15819 params->quick_push (tmp_addr);
15820 params->quick_push (seq_cst);
15821 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15822 return build2 (COMPOUND_EXPR, void_type_node, load, store);
15825 /* Create a transaction node. */
15828 c_finish_transaction (location_t loc, tree block, int flags)
15830 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15831 if (flags & TM_STMT_ATTR_OUTER)
15832 TRANSACTION_EXPR_OUTER (stmt) = 1;
15833 if (flags & TM_STMT_ATTR_RELAXED)
15834 TRANSACTION_EXPR_RELAXED (stmt) = 1;
15835 return add_stmt (stmt);
15838 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15839 down to the element type of an array. If ORIG_QUAL_TYPE is not
15840 NULL, then it should be used as the qualified type
15841 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15842 preserve information about the typedef name from which an array
15843 type was derived). */
15846 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15847 size_t orig_qual_indirect)
15849 if (type == error_mark_node)
15852 if (TREE_CODE (type) == ARRAY_TYPE)
15855 tree element_type = c_build_qualified_type (TREE_TYPE (type),
15856 type_quals, orig_qual_type,
15857 orig_qual_indirect - 1);
15859 /* See if we already have an identically qualified type. */
15860 if (orig_qual_type && orig_qual_indirect == 0)
15861 t = orig_qual_type;
15863 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15865 if (TYPE_QUALS (strip_array_types (t)) == type_quals
15866 && TYPE_NAME (t) == TYPE_NAME (type)
15867 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15868 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15869 TYPE_ATTRIBUTES (type)))
15874 tree domain = TYPE_DOMAIN (type);
15876 t = build_variant_type_copy (type);
15877 TREE_TYPE (t) = element_type;
15879 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15880 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15881 SET_TYPE_STRUCTURAL_EQUALITY (t);
15882 else if (TYPE_CANONICAL (element_type) != element_type
15883 || (domain && TYPE_CANONICAL (domain) != domain))
15885 tree unqualified_canon
15886 = build_array_type (TYPE_CANONICAL (element_type),
15887 domain? TYPE_CANONICAL (domain)
15889 if (TYPE_REVERSE_STORAGE_ORDER (type))
15892 = build_distinct_type_copy (unqualified_canon);
15893 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15896 = c_build_qualified_type (unqualified_canon, type_quals);
15899 TYPE_CANONICAL (t) = t;
15904 /* A restrict-qualified pointer type must be a pointer to object or
15905 incomplete type. Note that the use of POINTER_TYPE_P also allows
15906 REFERENCE_TYPEs, which is appropriate for C++. */
15907 if ((type_quals & TYPE_QUAL_RESTRICT)
15908 && (!POINTER_TYPE_P (type)
15909 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15911 error ("invalid use of %<restrict%>");
15912 type_quals &= ~TYPE_QUAL_RESTRICT;
15915 tree var_type = (orig_qual_type && orig_qual_indirect == 0
15917 : build_qualified_type (type, type_quals));
15918 /* A variant type does not inherit the list of incomplete vars from the
15919 type main variant. */
15920 if ((RECORD_OR_UNION_TYPE_P (var_type)
15921 || TREE_CODE (var_type) == ENUMERAL_TYPE)
15922 && TYPE_MAIN_VARIANT (var_type) != var_type)
15923 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15927 /* Build a VA_ARG_EXPR for the C parser. */
15930 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15932 if (error_operand_p (type))
15933 return error_mark_node;
15934 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15935 order because it takes the address of the expression. */
15936 else if (handled_component_p (expr)
15937 && reverse_storage_order_for_component_p (expr))
15939 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15940 return error_mark_node;
15942 else if (!COMPLETE_TYPE_P (type))
15944 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15946 return error_mark_node;
15948 else if (TREE_CODE (type) == FUNCTION_TYPE)
15950 error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
15952 return error_mark_node;
15954 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15955 warning_at (loc2, OPT_Wc___compat,
15956 "C++ requires promoted type, not enum type, in %<va_arg%>");
15957 return build_va_arg (loc2, expr, type);
15960 /* Return truthvalue of whether T1 is the same tree structure as T2.
15961 Return 1 if they are the same. Return false if they are different. */
15964 c_tree_equal (tree t1, tree t2)
15966 enum tree_code code1, code2;
15973 for (code1 = TREE_CODE (t1);
15974 CONVERT_EXPR_CODE_P (code1)
15975 || code1 == NON_LVALUE_EXPR;
15976 code1 = TREE_CODE (t1))
15977 t1 = TREE_OPERAND (t1, 0);
15978 for (code2 = TREE_CODE (t2);
15979 CONVERT_EXPR_CODE_P (code2)
15980 || code2 == NON_LVALUE_EXPR;
15981 code2 = TREE_CODE (t2))
15982 t2 = TREE_OPERAND (t2, 0);
15984 /* They might have become equal now. */
15988 if (code1 != code2)
15994 return wi::to_wide (t1) == wi::to_wide (t2);
15997 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16000 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16001 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16002 TREE_STRING_LENGTH (t1));
16005 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16006 TREE_FIXED_CST (t2));
16009 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16010 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16013 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16016 /* We need to do this when determining whether or not two
16017 non-type pointer to member function template arguments
16019 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16020 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16025 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16027 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16028 if (!c_tree_equal (field, elt2->index)
16029 || !c_tree_equal (value, elt2->value))
16036 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16038 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16040 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16043 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16048 call_expr_arg_iterator iter1, iter2;
16049 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16051 for (arg1 = first_call_expr_arg (t1, &iter1),
16052 arg2 = first_call_expr_arg (t2, &iter2);
16054 arg1 = next_call_expr_arg (&iter1),
16055 arg2 = next_call_expr_arg (&iter2))
16056 if (!c_tree_equal (arg1, arg2))
16065 tree o1 = TREE_OPERAND (t1, 0);
16066 tree o2 = TREE_OPERAND (t2, 0);
16068 /* Special case: if either target is an unallocated VAR_DECL,
16069 it means that it's going to be unified with whatever the
16070 TARGET_EXPR is really supposed to initialize, so treat it
16071 as being equivalent to anything. */
16072 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16073 && !DECL_RTL_SET_P (o1))
16075 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16076 && !DECL_RTL_SET_P (o2))
16078 else if (!c_tree_equal (o1, o2))
16081 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16084 case COMPONENT_REF:
16085 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16087 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16093 case FUNCTION_DECL:
16094 case IDENTIFIER_NODE:
16101 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16103 for (ix = TREE_VEC_LENGTH (t1); ix--;)
16104 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16105 TREE_VEC_ELT (t2, ix)))
16114 switch (TREE_CODE_CLASS (code1))
16118 case tcc_comparison:
16119 case tcc_expression:
16121 case tcc_reference:
16122 case tcc_statement:
16124 int i, n = TREE_OPERAND_LENGTH (t1);
16128 case PREINCREMENT_EXPR:
16129 case PREDECREMENT_EXPR:
16130 case POSTINCREMENT_EXPR:
16131 case POSTDECREMENT_EXPR:
16141 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16142 && n != TREE_OPERAND_LENGTH (t2))
16145 for (i = 0; i < n; ++i)
16146 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16153 return comptypes (t1, t2);
16155 gcc_unreachable ();
16159 /* Returns true when the function declaration FNDECL is implicit,
16160 introduced as a result of a call to an otherwise undeclared
16161 function, and false otherwise. */
16164 c_decl_implicit (const_tree fndecl)
16166 return C_DECL_IMPLICIT (fndecl);