1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
28 #include "coretypes.h"
34 #include "gimple-expr.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
40 #include "langhooks.h"
43 #include "tree-iterator.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
57 /* Possible cases of implicit conversions. Used to select diagnostic messages
58 and control folding initializers in convert_for_assignment. */
67 /* The level of nesting inside "__alignof__". */
70 /* The level of nesting inside "sizeof". */
73 /* The level of nesting inside "typeof". */
76 /* True when parsing OpenMP loop expressions. */
79 /* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81 tree c_last_sizeof_arg
;
82 location_t c_last_sizeof_loc
;
84 /* Nonzero if we might need to print a "missing braces around
85 initializer" message within this initializer. */
86 static int found_missing_braces
;
88 static bool require_constant_value
;
89 static bool require_constant_elements
;
90 static bool require_constexpr_value
;
92 static tree
qualify_type (tree
, tree
);
93 struct comptypes_data
;
94 static bool tagged_types_tu_compatible_p (const_tree
, const_tree
,
95 struct comptypes_data
*);
96 static bool comp_target_types (location_t
, tree
, tree
);
97 static bool function_types_compatible_p (const_tree
, const_tree
,
98 struct comptypes_data
*);
99 static bool type_lists_compatible_p (const_tree
, const_tree
,
100 struct comptypes_data
*);
101 static tree
lookup_field (tree
, tree
);
102 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
103 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
105 static tree
pointer_diff (location_t
, tree
, tree
, tree
*);
106 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
107 enum impl_conv
, bool, tree
, tree
, int,
109 static tree
valid_compound_expr_initializer (tree
, tree
);
110 static void push_string (const char *);
111 static void push_member_name (tree
);
112 static int spelling_length (void);
113 static char *print_spelling (char *);
114 static void warning_init (location_t
, int, const char *);
115 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, bool, bool,
117 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, bool,
118 bool, struct obstack
*);
119 static void output_pending_init_elements (int, struct obstack
*);
120 static bool set_designator (location_t
, bool, struct obstack
*);
121 static void push_range_stack (tree
, struct obstack
*);
122 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
124 static void set_nonincremental_init (struct obstack
*);
125 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
126 static tree
find_init_member (tree
, struct obstack
*);
127 static void readonly_warning (tree
, enum lvalue_use
);
128 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
129 static void record_maybe_used_decl (tree
);
130 static bool comptypes_internal (const_tree
, const_tree
,
131 struct comptypes_data
*data
);
133 /* Return true if EXP is a null pointer constant, false otherwise. */
136 null_pointer_constant_p (const_tree expr
)
138 /* This should really operate on c_expr structures, but they aren't
139 yet available everywhere required. */
140 tree type
= TREE_TYPE (expr
);
142 /* An integer constant expression with the value 0, such an expression
143 cast to type void*, or the predefined constant nullptr, are a null
145 if (expr
== nullptr_node
)
148 return (TREE_CODE (expr
) == INTEGER_CST
149 && !TREE_OVERFLOW (expr
)
150 && integer_zerop (expr
)
151 && (INTEGRAL_TYPE_P (type
)
152 || (TREE_CODE (type
) == POINTER_TYPE
153 && VOID_TYPE_P (TREE_TYPE (type
))
154 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
157 /* EXPR may appear in an unevaluated part of an integer constant
158 expression, but not in an evaluated part. Wrap it in a
159 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
160 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
163 note_integer_operands (tree expr
)
166 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
168 ret
= copy_node (expr
);
169 TREE_OVERFLOW (ret
) = 1;
173 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
174 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
179 /* Having checked whether EXPR may appear in an unevaluated part of an
180 integer constant expression and found that it may, remove any
181 C_MAYBE_CONST_EXPR noting this fact and return the resulting
185 remove_c_maybe_const_expr (tree expr
)
187 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
188 return C_MAYBE_CONST_EXPR_EXPR (expr
);
193 \f/* This is a cache to hold if two types are seen. */
195 struct tagged_tu_seen_cache
{
196 const struct tagged_tu_seen_cache
* next
;
201 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
202 does not have an incomplete type. (That includes void types.)
203 LOC is the location of the use. */
206 require_complete_type (location_t loc
, tree value
)
208 tree type
= TREE_TYPE (value
);
210 if (error_operand_p (value
))
211 return error_mark_node
;
213 /* First, detect a valid value with a complete type. */
214 if (COMPLETE_TYPE_P (type
))
217 c_incomplete_type_error (loc
, value
, type
);
218 return error_mark_node
;
221 /* Print an error message for invalid use of an incomplete type.
222 VALUE is the expression that was used (or 0 if that isn't known)
223 and TYPE is the type that was invalid. LOC is the location for
227 c_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
229 /* Avoid duplicate error message. */
230 if (TREE_CODE (type
) == ERROR_MARK
)
233 if (value
!= NULL_TREE
&& (VAR_P (value
) || TREE_CODE (value
) == PARM_DECL
))
234 error_at (loc
, "%qD has an incomplete type %qT", value
, type
);
238 /* We must print an error message. Be clever about what it says. */
240 switch (TREE_CODE (type
))
248 error_at (loc
, "invalid use of void expression");
252 if (TYPE_DOMAIN (type
))
254 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
256 error_at (loc
, "invalid use of flexible array member");
259 type
= TREE_TYPE (type
);
262 error_at (loc
, "invalid use of array with unspecified bounds");
269 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
270 error_at (loc
, "invalid use of undefined type %qT", type
);
272 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
273 error_at (loc
, "invalid use of incomplete typedef %qT", type
);
277 /* Given a type, apply default promotions wrt unnamed function
278 arguments and return the new type. */
281 c_type_promotes_to (tree type
)
283 tree ret
= NULL_TREE
;
285 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
286 ret
= double_type_node
;
287 else if (c_promoting_integer_type_p (type
))
289 /* Preserve unsignedness if not really getting any wider. */
290 if (TYPE_UNSIGNED (type
)
291 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
292 ret
= unsigned_type_node
;
294 ret
= integer_type_node
;
297 if (ret
!= NULL_TREE
)
298 return (TYPE_ATOMIC (type
)
299 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
305 /* Return true if between two named address spaces, whether there is a superset
306 named address space that encompasses both address spaces. If there is a
307 superset, return which address space is the superset. */
310 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
317 else if (targetm
.addr_space
.subset_p (as1
, as2
))
322 else if (targetm
.addr_space
.subset_p (as2
, as1
))
331 /* Return a variant of TYPE which has all the type qualifiers of LIKE
332 as well as those of TYPE. */
335 qualify_type (tree type
, tree like
)
337 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
338 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
339 addr_space_t as_common
;
341 /* If the two named address spaces are different, determine the common
342 superset address space. If there isn't one, raise an error. */
343 if (!addr_space_superset (as_type
, as_like
, &as_common
))
346 error ("%qT and %qT are in disjoint named address spaces",
350 return c_build_qualified_type (type
,
351 TYPE_QUALS_NO_ADDR_SPACE (type
)
352 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
353 | ENCODE_QUAL_ADDR_SPACE (as_common
));
357 /* If NTYPE is a type of a non-variadic function with a prototype
358 and OTYPE is a type of a function without a prototype and ATTRS
359 contains attribute format, diagnosess and removes it from ATTRS.
360 Returns the result of build_type_attribute_variant of NTYPE and
361 the (possibly) modified ATTRS. */
364 build_functype_attribute_variant (tree ntype
, tree otype
, tree attrs
)
366 if (!prototype_p (otype
)
367 && prototype_p (ntype
)
368 && lookup_attribute ("format", attrs
))
370 warning_at (input_location
, OPT_Wattributes
,
371 "%qs attribute cannot be applied to a function that "
372 "does not take variable arguments", "format");
373 attrs
= remove_attribute ("format", attrs
);
375 return build_type_attribute_variant (ntype
, attrs
);
378 /* Return the composite type of two compatible types.
380 We assume that comptypes has already been done and returned
381 nonzero; if that isn't so, this may crash. In particular, we
382 assume that qualifiers match. */
384 struct composite_cache
{
388 struct composite_cache
* next
;
392 composite_type_internal (tree t1
, tree t2
, struct composite_cache
* cache
)
394 enum tree_code code1
;
395 enum tree_code code2
;
398 /* Save time if the two types are the same. */
400 if (t1
== t2
) return t1
;
402 /* If one type is nonsense, use the other. */
403 if (t1
== error_mark_node
)
405 if (t2
== error_mark_node
)
408 code1
= TREE_CODE (t1
);
409 code2
= TREE_CODE (t2
);
411 /* Merge the attributes. */
412 attributes
= targetm
.merge_type_attributes (t1
, t2
);
414 /* If one is an enumerated type and the other is the compatible
415 integer type, the composite type might be either of the two
416 (DR#013 question 3). For consistency, use the enumerated type as
417 the composite type. */
419 if (code1
== ENUMERAL_TYPE
420 && (code2
== INTEGER_TYPE
421 || code2
== BOOLEAN_TYPE
))
423 if (code2
== ENUMERAL_TYPE
424 && (code1
== INTEGER_TYPE
425 || code1
== BOOLEAN_TYPE
))
428 gcc_assert (code1
== code2
);
433 /* For two pointers, do this recursively on the target type. */
435 tree pointed_to_1
= TREE_TYPE (t1
);
436 tree pointed_to_2
= TREE_TYPE (t2
);
437 tree target
= composite_type_internal (pointed_to_1
,
438 pointed_to_2
, cache
);
439 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
440 t1
= build_type_attribute_variant (t1
, attributes
);
441 return qualify_type (t1
, t2
);
446 tree elt
= composite_type_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
450 tree d1
= TYPE_DOMAIN (t1
);
451 tree d2
= TYPE_DOMAIN (t2
);
452 bool d1_variable
, d2_variable
;
453 bool d1_zero
, d2_zero
;
454 bool t1_complete
, t2_complete
;
456 /* We should not have any type quals on arrays at all. */
457 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
458 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
460 t1_complete
= COMPLETE_TYPE_P (t1
);
461 t2_complete
= COMPLETE_TYPE_P (t2
);
463 d1_zero
= d1
== NULL_TREE
|| !TYPE_MAX_VALUE (d1
);
464 d2_zero
= d2
== NULL_TREE
|| !TYPE_MAX_VALUE (d2
);
466 d1_variable
= (!d1_zero
467 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
468 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
469 d2_variable
= (!d2_zero
470 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
471 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
472 d1_variable
= d1_variable
|| (d1_zero
&& C_TYPE_VARIABLE_SIZE (t1
));
473 d2_variable
= d2_variable
|| (d2_zero
&& C_TYPE_VARIABLE_SIZE (t2
));
475 /* Save space: see if the result is identical to one of the args. */
476 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
477 && (d2_variable
|| d2_zero
|| !d1_variable
))
478 return build_type_attribute_variant (t1
, attributes
);
479 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
480 && (d1_variable
|| d1_zero
|| !d2_variable
))
481 return build_type_attribute_variant (t2
, attributes
);
483 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
484 return build_type_attribute_variant (t1
, attributes
);
485 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
486 return build_type_attribute_variant (t2
, attributes
);
488 /* Merge the element types, and have a size if either arg has
489 one. We may have qualifiers on the element types. To set
490 up TYPE_MAIN_VARIANT correctly, we need to form the
491 composite of the unqualified types and add the qualifiers
493 quals
= TYPE_QUALS (strip_array_types (elt
));
494 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
495 t1
= build_array_type (unqual_elt
,
496 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
502 /* Ensure a composite type involving a zero-length array type
503 is a zero-length type not an incomplete type. */
504 if (d1_zero
&& d2_zero
505 && (t1_complete
|| t2_complete
)
506 && !COMPLETE_TYPE_P (t1
))
508 TYPE_SIZE (t1
) = bitsize_zero_node
;
509 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
511 t1
= c_build_qualified_type (t1
, quals
);
512 return build_type_attribute_variant (t1
, attributes
);
517 if (flag_isoc23
&& !comptypes_same_p (t1
, t2
))
519 gcc_checking_assert (COMPLETE_TYPE_P (t1
) && COMPLETE_TYPE_P (t2
));
520 gcc_checking_assert (!TYPE_NAME (t1
) || comptypes (t1
, t2
));
522 /* If a composite type for these two types is already under
523 construction, return it. */
525 for (struct composite_cache
*c
= cache
; c
!= NULL
; c
= c
->next
)
526 if (c
->t1
== t1
&& c
->t2
== t2
)
529 /* Otherwise, create a new type node and link it into the cache. */
531 tree n
= make_node (code1
);
532 TYPE_NAME (n
) = TYPE_NAME (t1
);
534 struct composite_cache cache2
= { t1
, t2
, n
, cache
};
537 tree f1
= TYPE_FIELDS (t1
);
538 tree f2
= TYPE_FIELDS (t2
);
539 tree fields
= NULL_TREE
;
541 for (tree a
= f1
, b
= f2
; a
&& b
;
542 a
= DECL_CHAIN (a
), b
= DECL_CHAIN (b
))
544 tree ta
= TREE_TYPE (a
);
545 tree tb
= TREE_TYPE (b
);
547 if (DECL_C_BIT_FIELD (a
))
549 ta
= DECL_BIT_FIELD_TYPE (a
);
550 tb
= DECL_BIT_FIELD_TYPE (b
);
553 gcc_assert (DECL_NAME (a
) == DECL_NAME (b
));
554 gcc_checking_assert (!DECL_NAME (a
) || comptypes (ta
, tb
));
556 tree t
= composite_type_internal (ta
, tb
, cache
);
557 tree f
= build_decl (input_location
, FIELD_DECL
, DECL_NAME (a
), t
);
559 DECL_PACKED (f
) = DECL_PACKED (a
);
560 SET_DECL_ALIGN (f
, DECL_ALIGN (a
));
561 DECL_ATTRIBUTES (f
) = DECL_ATTRIBUTES (a
);
562 C_DECL_VARIABLE_SIZE (f
) = C_TYPE_VARIABLE_SIZE (t
);
564 finish_decl (f
, input_location
, NULL
, NULL
, NULL
);
566 if (DECL_C_BIT_FIELD (a
))
568 /* This will be processed by finish_struct. */
569 SET_DECL_C_BIT_FIELD (f
);
570 DECL_INITIAL (f
) = build_int_cst (integer_type_node
,
571 tree_to_uhwi (DECL_SIZE (a
)));
572 DECL_NONADDRESSABLE_P (f
) = true;
573 DECL_PADDING_P (f
) = !DECL_NAME (a
);
576 DECL_CHAIN (f
) = fields
;
580 fields
= nreverse (fields
);
582 /* Setup the struct/union type. Because we inherit all variably
583 modified components, we can ignore the size expression. */
584 tree expr
= NULL_TREE
;
585 n
= finish_struct(input_location
, n
, fields
, attributes
, NULL
, &expr
);
587 n
= qualify_type (n
, t1
);
589 gcc_checking_assert (!TYPE_NAME (n
) || comptypes (n
, t1
));
590 gcc_checking_assert (!TYPE_NAME (n
) || comptypes (n
, t2
));
596 if (attributes
!= NULL
)
598 /* Try harder not to create a new aggregate type. */
599 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
601 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
604 return build_type_attribute_variant (t1
, attributes
);
607 /* Function types: prefer the one that specified arg types.
608 If both do, merge the arg types. Also merge the return types. */
610 tree valtype
= composite_type_internal (TREE_TYPE (t1
),
611 TREE_TYPE (t2
), cache
);
612 tree p1
= TYPE_ARG_TYPES (t1
);
613 tree p2
= TYPE_ARG_TYPES (t2
);
618 /* Save space: see if the result is identical to one of the args. */
619 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
620 return build_functype_attribute_variant (t1
, t2
, attributes
);
621 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
622 return build_functype_attribute_variant (t2
, t1
, attributes
);
624 /* Simple way if one arg fails to specify argument types. */
625 if (TYPE_ARG_TYPES (t1
) == NULL_TREE
)
627 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
),
628 TYPE_NO_NAMED_ARGS_STDARG_P (t2
));
629 t1
= build_type_attribute_variant (t1
, attributes
);
630 return qualify_type (t1
, t2
);
632 if (TYPE_ARG_TYPES (t2
) == NULL_TREE
)
634 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
),
635 TYPE_NO_NAMED_ARGS_STDARG_P (t1
));
636 t1
= build_type_attribute_variant (t1
, attributes
);
637 return qualify_type (t1
, t2
);
640 /* If both args specify argument types, we must merge the two
641 lists, argument by argument. */
643 for (len
= 0, newargs
= p1
;
644 newargs
&& newargs
!= void_list_node
;
645 len
++, newargs
= TREE_CHAIN (newargs
))
648 for (i
= 0; i
< len
; i
++)
649 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
653 for (; p1
&& p1
!= void_list_node
;
654 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
656 tree mv1
= TREE_VALUE (p1
);
657 if (mv1
&& mv1
!= error_mark_node
658 && TREE_CODE (mv1
) != ARRAY_TYPE
)
659 mv1
= TYPE_MAIN_VARIANT (mv1
);
661 tree mv2
= TREE_VALUE (p2
);
662 if (mv2
&& mv2
!= error_mark_node
663 && TREE_CODE (mv2
) != ARRAY_TYPE
)
664 mv2
= TYPE_MAIN_VARIANT (mv2
);
666 /* A null type means arg type is not specified.
667 Take whatever the other function type has. */
668 if (TREE_VALUE (p1
) == NULL_TREE
)
670 TREE_VALUE (n
) = TREE_VALUE (p2
);
673 if (TREE_VALUE (p2
) == NULL_TREE
)
675 TREE_VALUE (n
) = TREE_VALUE (p1
);
679 /* Given wait (union {union wait *u; int *i} *)
680 and wait (union wait *),
681 prefer union wait * as type of parm. */
682 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
683 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
686 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
687 memb
; memb
= DECL_CHAIN (memb
))
689 tree mv3
= TREE_TYPE (memb
);
690 if (mv3
&& mv3
!= error_mark_node
691 && TREE_CODE (mv3
) != ARRAY_TYPE
)
692 mv3
= TYPE_MAIN_VARIANT (mv3
);
693 if (comptypes (mv3
, mv2
))
695 TREE_VALUE (n
) = composite_type_internal (TREE_TYPE (memb
),
698 pedwarn (input_location
, OPT_Wpedantic
,
699 "function types not truly compatible in ISO C");
704 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
705 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
708 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
709 memb
; memb
= DECL_CHAIN (memb
))
711 tree mv3
= TREE_TYPE (memb
);
712 if (mv3
&& mv3
!= error_mark_node
713 && TREE_CODE (mv3
) != ARRAY_TYPE
)
714 mv3
= TYPE_MAIN_VARIANT (mv3
);
715 if (comptypes (mv3
, mv1
))
718 = composite_type_internal (TREE_TYPE (memb
),
721 pedwarn (input_location
, OPT_Wpedantic
,
722 "function types not truly compatible in ISO C");
727 TREE_VALUE (n
) = composite_type_internal (mv1
, mv2
, cache
);
731 t1
= build_function_type (valtype
, newargs
);
732 t1
= qualify_type (t1
, t2
);
737 return build_type_attribute_variant (t1
, attributes
);
742 composite_type (tree t1
, tree t2
)
744 struct composite_cache cache
= { };
745 return composite_type_internal (t1
, t2
, &cache
);
748 /* Return the type of a conditional expression between pointers to
749 possibly differently qualified versions of compatible types.
751 We assume that comp_target_types has already been done and returned
752 true; if that isn't so, this may crash. */
755 common_pointer_type (tree t1
, tree t2
)
758 tree pointed_to_1
, mv1
;
759 tree pointed_to_2
, mv2
;
761 unsigned target_quals
;
762 addr_space_t as1
, as2
, as_common
;
765 /* Save time if the two types are the same. */
767 if (t1
== t2
) return t1
;
769 /* If one type is nonsense, use the other. */
770 if (t1
== error_mark_node
)
772 if (t2
== error_mark_node
)
775 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
776 && TREE_CODE (t2
) == POINTER_TYPE
);
778 /* Merge the attributes. */
779 attributes
= targetm
.merge_type_attributes (t1
, t2
);
781 /* Find the composite type of the target types, and combine the
782 qualifiers of the two types' targets. Do not lose qualifiers on
783 array element types by taking the TYPE_MAIN_VARIANT. */
784 mv1
= pointed_to_1
= TREE_TYPE (t1
);
785 mv2
= pointed_to_2
= TREE_TYPE (t2
);
786 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
787 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
788 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
789 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
790 target
= composite_type (mv1
, mv2
);
792 /* Strip array types to get correct qualifier for pointers to arrays */
793 quals1
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1
));
794 quals2
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2
));
796 /* For function types do not merge const qualifiers, but drop them
797 if used inconsistently. The middle-end uses these to mark const
798 and noreturn functions. */
799 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
800 target_quals
= (quals1
& quals2
);
802 target_quals
= (quals1
| quals2
);
804 /* If the two named address spaces are different, determine the common
805 superset address space. This is guaranteed to exist due to the
806 assumption that comp_target_type returned true. */
807 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
808 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
809 if (!addr_space_superset (as1
, as2
, &as_common
))
812 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
814 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
815 return build_type_attribute_variant (t1
, attributes
);
818 /* Return the common type for two arithmetic types under the usual
819 arithmetic conversions. The default conversions have already been
820 applied, and enumerated types converted to their compatible integer
821 types. The resulting type is unqualified and has no attributes.
823 This is the type for the result of most arithmetic operations
824 if the operands have the given two types. */
827 c_common_type (tree t1
, tree t2
)
829 enum tree_code code1
;
830 enum tree_code code2
;
832 /* If one type is nonsense, use the other. */
833 if (t1
== error_mark_node
)
835 if (t2
== error_mark_node
)
838 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
839 t1
= TYPE_MAIN_VARIANT (t1
);
841 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
842 t2
= TYPE_MAIN_VARIANT (t2
);
844 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
846 tree attrs
= affects_type_identity_attributes (TYPE_ATTRIBUTES (t1
));
847 t1
= build_type_attribute_variant (t1
, attrs
);
850 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
852 tree attrs
= affects_type_identity_attributes (TYPE_ATTRIBUTES (t2
));
853 t2
= build_type_attribute_variant (t2
, attrs
);
856 /* Save time if the two types are the same. */
858 if (t1
== t2
) return t1
;
860 code1
= TREE_CODE (t1
);
861 code2
= TREE_CODE (t2
);
863 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
864 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
865 || code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
);
866 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
867 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
868 || code2
== INTEGER_TYPE
|| code2
== BITINT_TYPE
);
870 /* When one operand is a decimal float type, the other operand cannot be
871 a generic float type or a complex type. We also disallow vector types
873 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
874 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
876 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
878 error ("cannot mix operands of decimal floating and vector types");
879 return error_mark_node
;
881 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
883 error ("cannot mix operands of decimal floating and complex types");
884 return error_mark_node
;
886 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
888 error ("cannot mix operands of decimal floating "
889 "and other floating types");
890 return error_mark_node
;
894 /* If one type is a vector type, return that type. (How the usual
895 arithmetic conversions apply to the vector types extension is not
896 precisely specified.) */
897 if (code1
== VECTOR_TYPE
)
900 if (code2
== VECTOR_TYPE
)
903 /* If one type is complex, form the common type of the non-complex
904 components, then make that complex. Use T1 or T2 if it is the
906 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
908 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
909 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
910 tree subtype
= c_common_type (subtype1
, subtype2
);
912 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
914 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
916 else if (TREE_CODE (subtype
) == BITINT_TYPE
)
918 sorry ("%<_Complex _BitInt(%d)%> unsupported",
919 TYPE_PRECISION (subtype
));
920 return code1
== COMPLEX_TYPE
? t1
: t2
;
923 return build_complex_type (subtype
);
926 /* If only one is real, use it as the result. */
928 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
931 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
934 /* If both are real and either are decimal floating point types, use
935 the decimal floating point type with the greater precision. */
937 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
939 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
940 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
941 return dfloat128_type_node
;
942 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
943 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
944 return dfloat64_type_node
;
945 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
946 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
947 return dfloat32_type_node
;
950 /* Deal with fixed-point types. */
951 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
953 unsigned int unsignedp
= 0, satp
= 0;
955 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
957 m1
= SCALAR_TYPE_MODE (t1
);
958 m2
= SCALAR_TYPE_MODE (t2
);
960 /* If one input type is saturating, the result type is saturating. */
961 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
964 /* If both fixed-point types are unsigned, the result type is unsigned.
965 When mixing fixed-point and integer types, follow the sign of the
967 Otherwise, the result type is signed. */
968 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
969 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
970 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
971 && TYPE_UNSIGNED (t1
))
972 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
973 && TYPE_UNSIGNED (t2
)))
976 /* The result type is signed. */
979 /* If the input type is unsigned, we need to convert to the
981 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
983 enum mode_class mclass
= (enum mode_class
) 0;
984 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
986 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
990 m1
= as_a
<scalar_mode
>
991 (mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0));
993 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
995 enum mode_class mclass
= (enum mode_class
) 0;
996 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
998 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
1002 m2
= as_a
<scalar_mode
>
1003 (mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0));
1007 if (code1
== FIXED_POINT_TYPE
)
1009 fbit1
= GET_MODE_FBIT (m1
);
1010 ibit1
= GET_MODE_IBIT (m1
);
1015 /* Signed integers need to subtract one sign bit. */
1016 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
1019 if (code2
== FIXED_POINT_TYPE
)
1021 fbit2
= GET_MODE_FBIT (m2
);
1022 ibit2
= GET_MODE_IBIT (m2
);
1027 /* Signed integers need to subtract one sign bit. */
1028 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
1031 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
1032 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
1033 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
1037 /* Both real or both integers; use the one with greater precision. */
1039 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
1041 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
1044 /* Same precision. Prefer long longs to longs to ints when the
1045 same precision, following the C99 rules on integer type rank
1046 (which are equivalent to the C90 rules for C90 types). */
1048 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
1049 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
1050 return long_long_unsigned_type_node
;
1052 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
1053 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
1055 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
1056 return long_long_unsigned_type_node
;
1058 return long_long_integer_type_node
;
1061 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
1062 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
1063 return long_unsigned_type_node
;
1065 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
1066 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
1068 /* But preserve unsignedness from the other type,
1069 since long cannot hold all the values of an unsigned int. */
1070 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
1071 return long_unsigned_type_node
;
1073 return long_integer_type_node
;
1076 /* For floating types of the same TYPE_PRECISION (which we here
1077 assume means either the same set of values, or sets of values
1078 neither a subset of the other, with behavior being undefined in
1079 the latter case), follow the rules from TS 18661-3: prefer
1080 interchange types _FloatN, then standard types long double,
1081 double, float, then extended types _FloatNx. For extended types,
1082 check them starting with _Float128x as that seems most consistent
1083 in spirit with preferring long double to double; for interchange
1084 types, also check in that order for consistency although it's not
1085 possible for more than one of them to have the same
1087 tree mv1
= TYPE_MAIN_VARIANT (t1
);
1088 tree mv2
= TYPE_MAIN_VARIANT (t2
);
1090 for (int i
= NUM_FLOATN_TYPES
- 1; i
>= 0; i
--)
1091 if (mv1
== FLOATN_TYPE_NODE (i
) || mv2
== FLOATN_TYPE_NODE (i
))
1092 return FLOATN_TYPE_NODE (i
);
1094 /* Likewise, prefer long double to double even if same size. */
1095 if (mv1
== long_double_type_node
|| mv2
== long_double_type_node
)
1096 return long_double_type_node
;
1098 /* Likewise, prefer double to float even if same size.
1099 We got a couple of embedded targets with 32 bit doubles, and the
1100 pdp11 might have 64 bit floats. */
1101 if (mv1
== double_type_node
|| mv2
== double_type_node
)
1102 return double_type_node
;
1104 if (mv1
== float_type_node
|| mv2
== float_type_node
)
1105 return float_type_node
;
1107 for (int i
= NUM_FLOATNX_TYPES
- 1; i
>= 0; i
--)
1108 if (mv1
== FLOATNX_TYPE_NODE (i
) || mv2
== FLOATNX_TYPE_NODE (i
))
1109 return FLOATNX_TYPE_NODE (i
);
1111 if ((code1
== BITINT_TYPE
|| code2
== BITINT_TYPE
) && code1
!= code2
)
1113 /* Prefer any other integral types over bit-precise integer types. */
1114 if (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
))
1115 return code1
== BITINT_TYPE
? t2
: t1
;
1116 /* If BITINT_TYPE is unsigned and the other type is signed
1117 non-BITINT_TYPE with the same precision, the latter has higher rank.
1119 Otherwise, both operands are converted to the unsigned integer type
1120 corresponding to the type of the operand with signed integer type. */
1121 if (TYPE_UNSIGNED (code1
== BITINT_TYPE
? t1
: t2
))
1122 return c_common_unsigned_type (code1
== BITINT_TYPE
? t2
: t1
);
1125 /* Otherwise prefer the unsigned one. */
1127 if (TYPE_UNSIGNED (t1
))
1133 /* Wrapper around c_common_type that is used by c-common.cc and other
1134 front end optimizations that remove promotions. ENUMERAL_TYPEs
1135 are allowed here and are converted to their compatible integer types.
1136 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1137 preferably a non-Boolean type as the common type. */
1139 common_type (tree t1
, tree t2
)
1141 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
1142 t1
= ENUM_UNDERLYING_TYPE (t1
);
1143 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
1144 t2
= ENUM_UNDERLYING_TYPE (t2
);
1146 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
1147 if (TREE_CODE (t1
) == BOOLEAN_TYPE
1148 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
1149 return boolean_type_node
;
1151 /* If either type is BOOLEAN_TYPE, then return the other. */
1152 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
1154 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
1157 return c_common_type (t1
, t2
);
1160 struct comptypes_data
{
1161 bool enum_and_int_p
;
1162 bool different_types_p
;
1163 bool warning_needed
;
1167 const struct tagged_tu_seen_cache
* cache
;
1170 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1171 or various other operations. Return 2 if they are compatible
1172 but a warning may be needed if you use them together. */
1175 comptypes (tree type1
, tree type2
)
1177 struct comptypes_data data
= { };
1178 bool ret
= comptypes_internal (type1
, type2
, &data
);
1180 return ret
? (data
.warning_needed
? 2 : 1) : 0;
1184 /* Like comptypes, but it returns non-zero only for identical
1188 comptypes_same_p (tree type1
, tree type2
)
1190 struct comptypes_data data
= { };
1191 bool ret
= comptypes_internal (type1
, type2
, &data
);
1193 if (data
.different_types_p
)
1200 /* Like comptypes, but if it returns non-zero because enum and int are
1201 compatible, it sets *ENUM_AND_INT_P to true. */
1204 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1206 struct comptypes_data data
= { };
1207 bool ret
= comptypes_internal (type1
, type2
, &data
);
1208 *enum_and_int_p
= data
.enum_and_int_p
;
1210 return ret
? (data
.warning_needed
? 2 : 1) : 0;
1213 /* Like comptypes, but if it returns nonzero for different types, it
1214 sets *DIFFERENT_TYPES_P to true. */
1217 comptypes_check_different_types (tree type1
, tree type2
,
1218 bool *different_types_p
)
1220 struct comptypes_data data
= { };
1221 bool ret
= comptypes_internal (type1
, type2
, &data
);
1222 *different_types_p
= data
.different_types_p
;
1224 return ret
? (data
.warning_needed
? 2 : 1) : 0;
1228 /* Like comptypes, but if it returns nonzero for struct and union
1229 types considered equivalent for aliasing purposes. */
1232 comptypes_equiv_p (tree type1
, tree type2
)
1234 struct comptypes_data data
= { };
1236 bool ret
= comptypes_internal (type1
, type2
, &data
);
1242 /* Return true if TYPE1 and TYPE2 are compatible types for assignment
1243 or various other operations. If they are compatible but a warning may
1244 be needed if you use them together, 'warning_needed' in DATA is set.
1245 If one type is an enum and the other a compatible integer type, then
1246 this sets 'enum_and_int_p' in DATA to true (it is never set to
1247 false). If the types are compatible but different enough not to be
1248 permitted in C11 typedef redeclarations, then this sets
1249 'different_types_p' in DATA to true; it is never set to
1250 false, but may or may not be set if the types are incompatible.
1251 This differs from comptypes, in that we don't free the seen
1255 comptypes_internal (const_tree type1
, const_tree type2
,
1256 struct comptypes_data
*data
)
1258 const_tree t1
= type1
;
1259 const_tree t2
= type2
;
1261 /* Suppress errors caused by previously reported errors. */
1263 if (t1
== t2
|| !t1
|| !t2
1264 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1267 /* Enumerated types are compatible with integer types, but this is
1268 not transitive: two enumerated types in the same translation unit
1269 are compatible with each other only if they are the same type. */
1271 if (TREE_CODE (t1
) == ENUMERAL_TYPE
1272 && COMPLETE_TYPE_P (t1
)
1273 && TREE_CODE (t2
) != ENUMERAL_TYPE
)
1275 t1
= ENUM_UNDERLYING_TYPE (t1
);
1276 if (TREE_CODE (t2
) != VOID_TYPE
)
1278 data
->enum_and_int_p
= true;
1279 data
->different_types_p
= true;
1282 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
1283 && COMPLETE_TYPE_P (t2
)
1284 && TREE_CODE (t1
) != ENUMERAL_TYPE
)
1286 t2
= ENUM_UNDERLYING_TYPE (t2
);
1287 if (TREE_CODE (t1
) != VOID_TYPE
)
1289 data
->enum_and_int_p
= true;
1290 data
->different_types_p
= true;
1297 /* Different classes of types can't be compatible. */
1299 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1302 /* Qualifiers must match. C99 6.7.3p9 */
1304 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1307 /* Allow for two different type nodes which have essentially the same
1308 definition. Note that we already checked for equality of the type
1309 qualifiers (just above). */
1311 if (TREE_CODE (t1
) != ARRAY_TYPE
1312 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1317 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1318 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1322 data
->warning_needed
= true;
1324 switch (TREE_CODE (t1
))
1327 case FIXED_POINT_TYPE
:
1330 /* With these nodes, we can't determine type equivalence by
1331 looking at what is stored in the nodes themselves, because
1332 two nodes might have different TYPE_MAIN_VARIANTs but still
1333 represent the same type. For example, wchar_t and int could
1334 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1335 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1336 and are distinct types. On the other hand, int and the
1339 typedef int INT __attribute((may_alias));
1341 have identical properties, different TYPE_MAIN_VARIANTs, but
1342 represent the same type. The canonical type system keeps
1343 track of equivalence in this case, so we fall back on it. */
1344 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1347 /* Do not remove mode information. */
1348 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
1350 return comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
), data
);
1353 return function_types_compatible_p (t1
, t2
, data
);
1357 tree d1
= TYPE_DOMAIN (t1
);
1358 tree d2
= TYPE_DOMAIN (t2
);
1359 bool d1_variable
, d2_variable
;
1360 bool d1_zero
, d2_zero
;
1362 /* Target types must match incl. qualifiers. */
1363 if (!comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
), data
))
1366 if ((d1
== NULL_TREE
) != (d2
== NULL_TREE
))
1367 data
->different_types_p
= true;
1368 /* Ignore size mismatches. */
1371 /* Sizes must match unless one is missing or variable. */
1372 if (d1
== NULL_TREE
|| d2
== NULL_TREE
|| d1
== d2
)
1375 d1_zero
= !TYPE_MAX_VALUE (d1
);
1376 d2_zero
= !TYPE_MAX_VALUE (d2
);
1378 d1_variable
= (!d1_zero
1379 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1380 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1381 d2_variable
= (!d2_zero
1382 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1383 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1384 d1_variable
= d1_variable
|| (d1_zero
&& C_TYPE_VARIABLE_SIZE (t1
));
1385 d2_variable
= d2_variable
|| (d2_zero
&& C_TYPE_VARIABLE_SIZE (t2
));
1387 if (d1_variable
!= d2_variable
)
1388 data
->different_types_p
= true;
1389 if (d1_variable
|| d2_variable
)
1391 if (d1_zero
&& d2_zero
)
1393 if (d1_zero
|| d2_zero
1394 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1395 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1408 return tagged_types_tu_compatible_p (t1
, t2
, data
);
1411 return known_eq (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1412 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
), data
);
1420 /* Return true if TTL and TTR are pointers to types that are equivalent, ignoring
1421 their qualifiers, except for named address spaces. If the pointers point to
1422 different named addresses, then we must determine if one address space is a
1423 subset of the other. */
1426 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1430 tree mvl
= TREE_TYPE (ttl
);
1431 tree mvr
= TREE_TYPE (ttr
);
1432 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1433 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1434 addr_space_t as_common
;
1435 bool enum_and_int_p
;
1437 /* Fail if pointers point to incompatible address spaces. */
1438 if (!addr_space_superset (asl
, asr
, &as_common
))
1441 /* For pedantic record result of comptypes on arrays before losing
1442 qualifiers on the element type below. */
1445 if (TREE_CODE (mvl
) == ARRAY_TYPE
1446 && TREE_CODE (mvr
) == ARRAY_TYPE
)
1447 val_ped
= comptypes (mvl
, mvr
);
1449 /* Qualifiers on element types of array types that are
1450 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1452 mvl
= (TYPE_ATOMIC (strip_array_types (mvl
))
1453 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1454 : TYPE_MAIN_VARIANT (mvl
));
1456 mvr
= (TYPE_ATOMIC (strip_array_types (mvr
))
1457 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1458 : TYPE_MAIN_VARIANT (mvr
));
1460 enum_and_int_p
= false;
1461 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1463 if (val
== 1 && val_ped
!= 1)
1464 pedwarn_c11 (location
, OPT_Wpedantic
, "invalid use of pointers to arrays with different qualifiers "
1465 "in ISO C before C23");
1468 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1470 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1471 warning_at (location
, OPT_Wc___compat
,
1472 "pointer target types incompatible in C++");
1477 /* Subroutines of `comptypes'. */
1479 /* Return true if two 'struct', 'union', or 'enum' types T1 and T2 are
1480 compatible. The two types are not the same (which has been
1481 checked earlier in comptypes_internal). */
1484 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1485 struct comptypes_data
*data
)
1489 /* We have to verify that the tags of the types are the same. This
1490 is harder than it looks because this may be a typedef, so we have
1491 to go look at the original type. It may even be a typedef of a
1493 In the case of compiler-created builtin structs the TYPE_DECL
1494 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1495 while (TYPE_NAME (t1
)
1496 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1497 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1498 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1500 while (TYPE_NAME (t2
)
1501 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1502 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1503 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1505 if (TYPE_NAME (t1
) != TYPE_NAME (t2
))
1508 if (!data
->anon_field
&& NULL_TREE
== TYPE_NAME (t1
))
1511 if (!data
->anon_field
&& TYPE_STUB_DECL (t1
) != TYPE_STUB_DECL (t2
))
1512 data
->different_types_p
= true;
1514 /* Incomplete types are incompatible inside a TU. */
1515 if (TYPE_SIZE (t1
) == NULL
|| TYPE_SIZE (t2
) == NULL
)
1518 if (ENUMERAL_TYPE
!= TREE_CODE (t1
)
1519 && (TYPE_REVERSE_STORAGE_ORDER (t1
)
1520 != TYPE_REVERSE_STORAGE_ORDER (t2
)))
1523 /* For types already being looked at in some active
1524 invocation of this function, assume compatibility.
1525 The cache is built as a linked list on the stack
1526 with the head of the list passed downwards. */
1527 for (const struct tagged_tu_seen_cache
*t
= data
->cache
;
1528 t
!= NULL
; t
= t
->next
)
1529 if (t
->t1
== t1
&& t
->t2
== t2
)
1532 const struct tagged_tu_seen_cache entry
= { data
->cache
, t1
, t2
};
1534 switch (TREE_CODE (t1
))
1538 if (!comptypes (ENUM_UNDERLYING_TYPE (t1
), ENUM_UNDERLYING_TYPE (t2
)))
1541 /* Speed up the case where the type values are in the same order. */
1542 tree tv1
= TYPE_VALUES (t1
);
1543 tree tv2
= TYPE_VALUES (t2
);
1548 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1550 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1553 if (simple_cst_equal (DECL_INITIAL (TREE_VALUE (tv1
)),
1554 DECL_INITIAL (TREE_VALUE (tv2
))) != 1)
1558 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1561 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1564 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1567 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1569 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1572 || simple_cst_equal (DECL_INITIAL (TREE_VALUE (s1
)),
1573 DECL_INITIAL (TREE_VALUE (s2
))) != 1)
1583 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1586 if (data
->equiv
&& (C_TYPE_VARIABLE_SIZE (t1
) || C_TYPE_VARIABLE_SIZE (t2
)))
1589 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1591 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1593 gcc_assert (TREE_CODE (s1
) == FIELD_DECL
);
1594 gcc_assert (TREE_CODE (s2
) == FIELD_DECL
);
1596 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1599 if (DECL_ALIGN (s1
) != DECL_ALIGN (s2
))
1602 data
->anon_field
= !DECL_NAME (s1
);
1604 data
->cache
= &entry
;
1605 if (!comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
), data
))
1608 tree st1
= TYPE_SIZE (TREE_TYPE (s1
));
1609 tree st2
= TYPE_SIZE (TREE_TYPE (s2
));
1612 && st1
&& TREE_CODE (st1
) == INTEGER_CST
1613 && st2
&& TREE_CODE (st2
) == INTEGER_CST
1614 && !tree_int_cst_equal (st1
, st2
))
1624 /* Return true if two function types F1 and F2 are compatible.
1625 If either type specifies no argument types,
1626 the other must specify a fixed number of self-promoting arg types.
1627 Otherwise, if one type specifies only the number of arguments,
1628 the other must specify that number of self-promoting arg types.
1629 Otherwise, the argument types must match. */
1632 function_types_compatible_p (const_tree f1
, const_tree f2
,
1633 struct comptypes_data
*data
)
1636 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1641 ret1
= TREE_TYPE (f1
);
1642 ret2
= TREE_TYPE (f2
);
1644 /* 'volatile' qualifiers on a function's return type used to mean
1645 the function is noreturn. */
1646 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1647 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1648 if (TYPE_VOLATILE (ret1
))
1649 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1650 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1651 if (TYPE_VOLATILE (ret2
))
1652 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1653 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1654 val
= comptypes_internal (ret1
, ret2
, data
);
1658 args1
= TYPE_ARG_TYPES (f1
);
1659 args2
= TYPE_ARG_TYPES (f2
);
1661 if ((args1
== NULL_TREE
) != (args2
== NULL_TREE
))
1662 data
->different_types_p
= true;
1664 /* An unspecified parmlist matches any specified parmlist
1665 whose argument types don't need default promotions. */
1667 if (args1
== NULL_TREE
)
1669 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1
) != TYPE_NO_NAMED_ARGS_STDARG_P (f2
))
1671 if (!self_promoting_args_p (args2
))
1673 /* If one of these types comes from a non-prototype fn definition,
1674 compare that with the other type's arglist.
1675 If they don't match, ask for a warning (but no error). */
1676 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1677 && type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1681 data
->warning_needed
= true;
1685 if (args2
== NULL_TREE
)
1687 if (TYPE_NO_NAMED_ARGS_STDARG_P (f1
) != TYPE_NO_NAMED_ARGS_STDARG_P (f2
))
1689 if (!self_promoting_args_p (args1
))
1691 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1692 && type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1696 data
->warning_needed
= true;
1701 /* Both types have argument lists: compare them and propagate results. */
1702 val1
= type_lists_compatible_p (args1
, args2
, data
);
1706 /* Check two lists of types for compatibility, returning false for
1707 incompatible, true for compatible. */
1710 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1711 struct comptypes_data
*data
)
1715 tree a1
, mv1
, a2
, mv2
;
1716 if (args1
== NULL_TREE
&& args2
== NULL_TREE
)
1718 /* If one list is shorter than the other,
1719 they fail to match. */
1720 if (args1
== NULL_TREE
|| args2
== NULL_TREE
)
1722 mv1
= a1
= TREE_VALUE (args1
);
1723 mv2
= a2
= TREE_VALUE (args2
);
1724 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1725 mv1
= (TYPE_ATOMIC (mv1
)
1726 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1728 : TYPE_MAIN_VARIANT (mv1
));
1729 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1730 mv2
= (TYPE_ATOMIC (mv2
)
1731 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1733 : TYPE_MAIN_VARIANT (mv2
));
1734 /* A null pointer instead of a type
1735 means there is supposed to be an argument
1736 but nothing is specified about what type it has.
1737 So match anything that self-promotes. */
1738 if ((a1
== NULL_TREE
) != (a2
== NULL_TREE
))
1739 data
->different_types_p
= true;
1740 if (a1
== NULL_TREE
)
1742 if (c_type_promotes_to (a2
) != a2
)
1745 else if (a2
== NULL_TREE
)
1747 if (c_type_promotes_to (a1
) != a1
)
1750 /* If one of the lists has an error marker, ignore this arg. */
1751 else if (TREE_CODE (a1
) == ERROR_MARK
1752 || TREE_CODE (a2
) == ERROR_MARK
)
1754 else if (!comptypes_internal (mv1
, mv2
, data
))
1756 data
->different_types_p
= true;
1757 /* Allow wait (union {union wait *u; int *i} *)
1758 and wait (union wait *) to be compatible. */
1759 if (TREE_CODE (a1
) == UNION_TYPE
1760 && (TYPE_NAME (a1
) == NULL_TREE
1761 || TYPE_TRANSPARENT_AGGR (a1
))
1762 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1763 && tree_int_cst_equal (TYPE_SIZE (a1
),
1767 for (memb
= TYPE_FIELDS (a1
);
1768 memb
; memb
= DECL_CHAIN (memb
))
1770 tree mv3
= TREE_TYPE (memb
);
1771 if (mv3
&& mv3
!= error_mark_node
1772 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1773 mv3
= (TYPE_ATOMIC (mv3
)
1774 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1776 : TYPE_MAIN_VARIANT (mv3
));
1777 if (comptypes_internal (mv3
, mv2
, data
))
1780 if (memb
== NULL_TREE
)
1783 else if (TREE_CODE (a2
) == UNION_TYPE
1784 && (TYPE_NAME (a2
) == NULL_TREE
1785 || TYPE_TRANSPARENT_AGGR (a2
))
1786 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1787 && tree_int_cst_equal (TYPE_SIZE (a2
),
1791 for (memb
= TYPE_FIELDS (a2
);
1792 memb
; memb
= DECL_CHAIN (memb
))
1794 tree mv3
= TREE_TYPE (memb
);
1795 if (mv3
&& mv3
!= error_mark_node
1796 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1797 mv3
= (TYPE_ATOMIC (mv3
)
1798 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1800 : TYPE_MAIN_VARIANT (mv3
));
1801 if (comptypes_internal (mv3
, mv1
, data
))
1804 if (memb
== NULL_TREE
)
1811 args1
= TREE_CHAIN (args1
);
1812 args2
= TREE_CHAIN (args2
);
1816 /* Compute the size to increment a pointer by. When a function type or void
1817 type or incomplete type is passed, size_one_node is returned.
1818 This function does not emit any diagnostics; the caller is responsible
1822 c_size_in_bytes (const_tree type
)
1824 enum tree_code code
= TREE_CODE (type
);
1826 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1827 || !COMPLETE_TYPE_P (type
))
1828 return size_one_node
;
1830 /* Convert in case a char is more than one unit. */
1831 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1832 size_int (TYPE_PRECISION (char_type_node
)
1836 /* Return either DECL or its known constant value (if it has one). */
1839 decl_constant_value_1 (tree decl
, bool in_init
)
1841 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1842 TREE_CODE (decl
) != PARM_DECL
1843 && !TREE_THIS_VOLATILE (decl
)
1844 && TREE_READONLY (decl
)
1845 && DECL_INITIAL (decl
) != NULL_TREE
1846 && !error_operand_p (DECL_INITIAL (decl
))
1847 /* This is invalid if initial value is not constant.
1848 If it has either a function call, a memory reference,
1849 or a variable, then re-evaluating it could give different results. */
1850 && TREE_CONSTANT (DECL_INITIAL (decl
))
1851 /* Check for cases where this is sub-optimal, even though valid. */
1852 && (in_init
|| TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
))
1853 return DECL_INITIAL (decl
);
1857 /* Return either DECL or its known constant value (if it has one).
1858 Like the above, but always return decl outside of functions. */
1861 decl_constant_value (tree decl
)
1863 /* Don't change a variable array bound or initial value to a constant
1864 in a place where a variable is invalid. */
1865 return current_function_decl
? decl_constant_value_1 (decl
, false) : decl
;
1868 /* Convert the array expression EXP to a pointer. */
1870 array_to_pointer_conversion (location_t loc
, tree exp
)
1872 tree orig_exp
= exp
;
1873 tree type
= TREE_TYPE (exp
);
1875 tree restype
= TREE_TYPE (type
);
1878 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1880 STRIP_TYPE_NOPS (exp
);
1882 copy_warning (exp
, orig_exp
);
1884 ptrtype
= build_pointer_type (restype
);
1886 if (INDIRECT_REF_P (exp
))
1887 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1889 /* In C++ array compound literals are temporary objects unless they are
1890 const or appear in namespace scope, so they are destroyed too soon
1891 to use them for much of anything (c++/53220). */
1892 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1894 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1895 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1896 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1897 "converting an array compound literal to a pointer "
1898 "leads to a dangling pointer in C++");
1901 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, true);
1902 return convert (ptrtype
, adr
);
1905 /* Convert the function expression EXP to a pointer. */
1907 function_to_pointer_conversion (location_t loc
, tree exp
)
1909 tree orig_exp
= exp
;
1911 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1913 STRIP_TYPE_NOPS (exp
);
1915 copy_warning (exp
, orig_exp
);
1917 return build_unary_op (loc
, ADDR_EXPR
, exp
, false);
1920 /* Mark EXP as read, not just set, for set but not used -Wunused
1921 warning purposes. */
1924 mark_exp_read (tree exp
)
1926 switch (TREE_CODE (exp
))
1930 DECL_READ_P (exp
) = 1;
1939 case VIEW_CONVERT_EXPR
:
1940 mark_exp_read (TREE_OPERAND (exp
, 0));
1943 /* Pattern match what build_atomic_assign produces with modifycode
1945 if (VAR_P (TREE_OPERAND (exp
, 1))
1946 && DECL_ARTIFICIAL (TREE_OPERAND (exp
, 1))
1947 && TREE_CODE (TREE_OPERAND (exp
, 0)) == COMPOUND_EXPR
)
1949 tree t1
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1950 tree t2
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 1);
1951 if (TREE_CODE (t1
) == TARGET_EXPR
1952 && TARGET_EXPR_SLOT (t1
) == TREE_OPERAND (exp
, 1)
1953 && TREE_CODE (t2
) == CALL_EXPR
)
1955 tree fndecl
= get_callee_fndecl (t2
);
1956 tree arg
= NULL_TREE
;
1958 && TREE_CODE (fndecl
) == FUNCTION_DECL
1959 && fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
1960 && call_expr_nargs (t2
) >= 2)
1961 switch (DECL_FUNCTION_CODE (fndecl
))
1963 case BUILT_IN_ATOMIC_STORE
:
1964 arg
= CALL_EXPR_ARG (t2
, 1);
1966 case BUILT_IN_ATOMIC_STORE_1
:
1967 case BUILT_IN_ATOMIC_STORE_2
:
1968 case BUILT_IN_ATOMIC_STORE_4
:
1969 case BUILT_IN_ATOMIC_STORE_8
:
1970 case BUILT_IN_ATOMIC_STORE_16
:
1971 arg
= CALL_EXPR_ARG (t2
, 0);
1979 if (TREE_CODE (arg
) == ADDR_EXPR
1980 && DECL_P (TREE_OPERAND (arg
, 0))
1981 && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg
, 0))))
1982 mark_exp_read (TREE_OPERAND (arg
, 0));
1987 case C_MAYBE_CONST_EXPR
:
1988 mark_exp_read (TREE_OPERAND (exp
, 1));
1995 /* Perform the default conversion of arrays and functions to pointers.
1996 Return the result of converting EXP. For any other expression, just
1999 LOC is the location of the expression. */
2002 default_function_array_conversion (location_t loc
, struct c_expr exp
)
2004 tree orig_exp
= exp
.value
;
2005 tree type
= TREE_TYPE (exp
.value
);
2006 enum tree_code code
= TREE_CODE (type
);
2012 bool not_lvalue
= false;
2013 bool lvalue_array_p
;
2015 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
2016 || CONVERT_EXPR_P (exp
.value
))
2017 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
2019 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
2021 exp
.value
= TREE_OPERAND (exp
.value
, 0);
2024 copy_warning (exp
.value
, orig_exp
);
2026 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
2027 if (!flag_isoc99
&& !lvalue_array_p
)
2029 /* Before C99, non-lvalue arrays do not decay to pointers.
2030 Normally, using such an array would be invalid; but it can
2031 be used correctly inside sizeof or as a statement expression.
2032 Thus, do not give an error here; an error will result later. */
2036 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
2040 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
2050 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
2052 mark_exp_read (exp
.value
);
2053 return default_function_array_conversion (loc
, exp
);
2056 /* Return whether EXPR should be treated as an atomic lvalue for the
2057 purposes of load and store handling. */
2060 really_atomic_lvalue (tree expr
)
2062 if (error_operand_p (expr
))
2064 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
2066 if (!lvalue_p (expr
))
2069 /* Ignore _Atomic on register variables, since their addresses can't
2070 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2071 sequences wouldn't work. Ignore _Atomic on structures containing
2072 bit-fields, since accessing elements of atomic structures or
2073 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2074 it's undefined at translation time or execution time, and the
2075 normal atomic sequences again wouldn't work. */
2076 while (handled_component_p (expr
))
2078 if (TREE_CODE (expr
) == COMPONENT_REF
2079 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
2081 expr
= TREE_OPERAND (expr
, 0);
2083 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
2088 /* If EXPR is a named constant (C23) derived from a constexpr variable
2089 - that is, a reference to such a variable, or a member extracted by
2090 a sequence of structure and union (but not array) member accesses
2091 (where union member accesses must access the same member as
2092 initialized) - then return the corresponding initializer;
2093 otherwise, return NULL_TREE. */
2096 maybe_get_constexpr_init (tree expr
)
2098 tree decl
= NULL_TREE
;
2099 if (TREE_CODE (expr
) == VAR_DECL
)
2101 else if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
2102 decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
2104 && C_DECL_DECLARED_CONSTEXPR (decl
)
2105 && DECL_INITIAL (decl
) != NULL_TREE
2106 && !error_operand_p (DECL_INITIAL (decl
)))
2107 return DECL_INITIAL (decl
);
2108 if (TREE_CODE (expr
) != COMPONENT_REF
)
2110 tree inner
= maybe_get_constexpr_init (TREE_OPERAND (expr
, 0));
2111 if (inner
== NULL_TREE
)
2113 while ((CONVERT_EXPR_P (inner
) || TREE_CODE (inner
) == NON_LVALUE_EXPR
)
2114 && !error_operand_p (inner
)
2115 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner
))
2116 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner
, 0)))))
2117 inner
= TREE_OPERAND (inner
, 0);
2118 if (TREE_CODE (inner
) != CONSTRUCTOR
)
2120 tree field
= TREE_OPERAND (expr
, 1);
2121 unsigned HOST_WIDE_INT cidx
;
2122 tree cfield
, cvalue
;
2123 bool have_other_init
= false;
2124 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner
), cidx
, cfield
, cvalue
)
2126 if (cfield
== field
)
2128 have_other_init
= true;
2130 if (TREE_CODE (TREE_TYPE (inner
)) == UNION_TYPE
2131 && (have_other_init
|| field
!= TYPE_FIELDS (TREE_TYPE (inner
))))
2133 /* Return a default initializer. */
2134 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr
)))
2135 return build_constructor (TREE_TYPE (expr
), NULL
);
2136 return build_zero_cst (TREE_TYPE (expr
));
2139 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2140 including converting functions and arrays to pointers if CONVERT_P.
2141 If READ_P, also mark the expression as having been read. If
2142 FOR_INIT, constexpr expressions of structure and union type should
2143 be replaced by the corresponding CONSTRUCTOR; otherwise, only
2144 constexpr scalars (including elements of structures and unions) are
2145 replaced by their initializers. */
2148 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
2149 bool convert_p
, bool read_p
, bool for_init
)
2151 bool force_non_npc
= false;
2153 mark_exp_read (exp
.value
);
2155 exp
= default_function_array_conversion (loc
, exp
);
2156 if (!VOID_TYPE_P (TREE_TYPE (exp
.value
)))
2157 exp
.value
= require_complete_type (loc
, exp
.value
);
2158 if (for_init
|| !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp
.value
)))
2160 tree init
= maybe_get_constexpr_init (exp
.value
);
2161 if (init
!= NULL_TREE
)
2163 /* A named constant of pointer type or type nullptr_t is not
2164 a null pointer constant even if the initializer is
2166 if (TREE_CODE (init
) == INTEGER_CST
2167 && !INTEGRAL_TYPE_P (TREE_TYPE (init
))
2168 && integer_zerop (init
))
2169 force_non_npc
= true;
2173 if (really_atomic_lvalue (exp
.value
))
2175 vec
<tree
, va_gc
> *params
;
2176 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2177 tree expr_type
= TREE_TYPE (exp
.value
);
2178 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, false);
2179 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2181 gcc_assert (TYPE_ATOMIC (expr_type
));
2183 /* Expansion of a generic atomic load may require an addition
2184 element, so allocate enough to prevent a resize. */
2185 vec_alloc (params
, 4);
2187 /* Remove the qualifiers for the rest of the expressions and
2188 create the VAL temp variable to hold the RHS. */
2189 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2190 tmp
= create_tmp_var_raw (nonatomic_type
);
2191 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, false);
2192 TREE_ADDRESSABLE (tmp
) = 1;
2193 /* Do not disable warnings for TMP even though it's artificial.
2194 -Winvalid-memory-model depends on it. */
2196 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2197 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2198 params
->quick_push (expr_addr
);
2199 params
->quick_push (tmp_addr
);
2200 params
->quick_push (seq_cst
);
2201 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2203 /* EXPR is always read. */
2204 mark_exp_read (exp
.value
);
2206 /* Return tmp which contains the value loaded. */
2207 exp
.value
= build4 (TARGET_EXPR
, nonatomic_type
, tmp
, func_call
,
2208 NULL_TREE
, NULL_TREE
);
2210 if (convert_p
&& !error_operand_p (exp
.value
)
2211 && (TREE_CODE (TREE_TYPE (exp
.value
)) != ARRAY_TYPE
))
2212 exp
.value
= convert (build_qualified_type (TREE_TYPE (exp
.value
), TYPE_UNQUALIFIED
), exp
.value
);
2214 exp
.value
= build1 (NOP_EXPR
, TREE_TYPE (exp
.value
), exp
.value
);
2217 tree false_value
, true_value
;
2218 if (convert_p
&& !error_operand_p (exp
.value
)
2219 && c_hardbool_type_attr (TREE_TYPE (exp
.value
),
2220 &false_value
, &true_value
))
2222 tree t
= save_expr (exp
.value
);
2224 mark_exp_read (exp
.value
);
2226 tree trapfn
= builtin_decl_explicit (BUILT_IN_TRAP
);
2227 tree expr
= build_call_expr_loc (loc
, trapfn
, 0);
2228 expr
= build_compound_expr (loc
, expr
, boolean_true_node
);
2229 expr
= fold_build3_loc (loc
, COND_EXPR
, boolean_type_node
,
2230 fold_build2_loc (loc
, NE_EXPR
,
2233 expr
, boolean_true_node
);
2234 expr
= fold_build3_loc (loc
, COND_EXPR
, boolean_type_node
,
2235 fold_build2_loc (loc
, NE_EXPR
,
2238 expr
, boolean_false_node
);
2247 /* EXP is an expression of integer type. Apply the integer promotions
2248 to it and return the promoted value. */
2251 perform_integral_promotions (tree exp
)
2253 tree type
= TREE_TYPE (exp
);
2254 enum tree_code code
= TREE_CODE (type
);
2256 gcc_assert (INTEGRAL_TYPE_P (type
));
2258 /* Convert enums to the result of applying the integer promotions to
2259 their underlying type. */
2260 if (code
== ENUMERAL_TYPE
)
2262 type
= ENUM_UNDERLYING_TYPE (type
);
2263 if (c_promoting_integer_type_p (type
))
2265 if (TYPE_UNSIGNED (type
)
2266 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2267 type
= unsigned_type_node
;
2269 type
= integer_type_node
;
2272 return convert (type
, exp
);
2275 /* ??? This should no longer be needed now bit-fields have their
2277 if (TREE_CODE (exp
) == COMPONENT_REF
2278 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1)))
2280 if (TREE_CODE (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp
, 1)))
2282 return convert (DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp
, 1)), exp
);
2283 /* If it's thinner than an int, promote it like a
2284 c_promoting_integer_type_p, otherwise leave it alone. */
2285 if (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2286 TYPE_PRECISION (integer_type_node
)) < 0)
2287 return convert (integer_type_node
, exp
);
2290 if (c_promoting_integer_type_p (type
))
2292 /* Preserve unsignedness if not really getting any wider. */
2293 if (TYPE_UNSIGNED (type
)
2294 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2295 return convert (unsigned_type_node
, exp
);
2297 return convert (integer_type_node
, exp
);
2304 /* Perform default promotions for C data used in expressions.
2305 Enumeral types or short or char are converted to int.
2306 In addition, manifest constants symbols are replaced by their values. */
2309 default_conversion (tree exp
)
2312 tree type
= TREE_TYPE (exp
);
2313 enum tree_code code
= TREE_CODE (type
);
2316 mark_exp_read (exp
);
2318 /* Functions and arrays have been converted during parsing. */
2319 gcc_assert (code
!= FUNCTION_TYPE
);
2320 if (code
== ARRAY_TYPE
)
2323 /* Constants can be used directly unless they're not loadable. */
2324 if (TREE_CODE (exp
) == CONST_DECL
)
2325 exp
= DECL_INITIAL (exp
);
2327 /* Strip no-op conversions. */
2329 STRIP_TYPE_NOPS (exp
);
2331 copy_warning (exp
, orig_exp
);
2333 if (code
== VOID_TYPE
)
2335 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2336 "void value not ignored as it ought to be");
2337 return error_mark_node
;
2340 exp
= require_complete_type (EXPR_LOC_OR_LOC (exp
, input_location
), exp
);
2341 if (exp
== error_mark_node
)
2342 return error_mark_node
;
2344 promoted_type
= targetm
.promoted_type (type
);
2346 return convert (promoted_type
, exp
);
2348 if (INTEGRAL_TYPE_P (type
))
2349 return perform_integral_promotions (exp
);
2354 /* Look up COMPONENT in a structure or union TYPE.
2356 If the component name is not found, returns NULL_TREE. Otherwise,
2357 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2358 stepping down the chain to the component, which is in the last
2359 TREE_VALUE of the list. Normally the list is of length one, but if
2360 the component is embedded within (nested) anonymous structures or
2361 unions, the list steps down the chain to the component. */
2364 lookup_field (tree type
, tree component
)
2368 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2369 to the field elements. Use a binary search on this array to quickly
2370 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2371 will always be set for structures which have many elements.
2373 Duplicate field checking replaces duplicates with NULL_TREE so
2374 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2375 case just iterate using DECL_CHAIN. */
2377 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
2381 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2383 field
= TYPE_FIELDS (type
);
2385 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2386 while (top
- bot
> 1)
2388 half
= (top
- bot
+ 1) >> 1;
2389 field
= field_array
[bot
+half
];
2391 if (DECL_NAME (field
) == NULL_TREE
)
2393 /* Step through all anon unions in linear fashion. */
2394 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2396 field
= field_array
[bot
++];
2397 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2399 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2402 return tree_cons (NULL_TREE
, field
, anon
);
2404 /* The Plan 9 compiler permits referring
2405 directly to an anonymous struct/union field
2406 using a typedef name. */
2407 if (flag_plan9_extensions
2408 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2409 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2411 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2417 /* Entire record is only anon unions. */
2421 /* Restart the binary search, with new lower bound. */
2425 if (DECL_NAME (field
) == component
)
2427 if (DECL_NAME (field
) < component
)
2433 if (DECL_NAME (field_array
[bot
]) == component
)
2434 field
= field_array
[bot
];
2435 else if (DECL_NAME (field
) != component
)
2440 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2442 if (DECL_NAME (field
) == NULL_TREE
2443 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2445 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2448 return tree_cons (NULL_TREE
, field
, anon
);
2450 /* The Plan 9 compiler permits referring directly to an
2451 anonymous struct/union field using a typedef
2453 if (flag_plan9_extensions
2454 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2455 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2456 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2461 if (DECL_NAME (field
) == component
)
2465 if (field
== NULL_TREE
)
2469 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2472 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2475 lookup_field_fuzzy_find_candidates (tree type
, tree component
,
2476 vec
<tree
> *candidates
)
2479 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2481 if (DECL_NAME (field
) == NULL_TREE
2482 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2483 lookup_field_fuzzy_find_candidates (TREE_TYPE (field
), component
,
2486 if (DECL_NAME (field
))
2487 candidates
->safe_push (DECL_NAME (field
));
2491 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2492 rather than returning a TREE_LIST for an exact match. */
2495 lookup_field_fuzzy (tree type
, tree component
)
2497 gcc_assert (TREE_CODE (component
) == IDENTIFIER_NODE
);
2499 /* First, gather a list of candidates. */
2500 auto_vec
<tree
> candidates
;
2502 lookup_field_fuzzy_find_candidates (type
, component
,
2505 return find_closest_identifier (component
, &candidates
);
2508 /* Support function for build_component_ref's error-handling.
2510 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2511 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2514 should_suggest_deref_p (tree datum_type
)
2516 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2517 allows "." for ptrs; we could be handling a failed attempt
2518 to access a property. */
2519 if (c_dialect_objc ())
2522 /* Only suggest it for pointers... */
2523 if (TREE_CODE (datum_type
) != POINTER_TYPE
)
2526 /* ...to structs/unions. */
2527 tree underlying_type
= TREE_TYPE (datum_type
);
2528 enum tree_code code
= TREE_CODE (underlying_type
);
2529 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2535 /* Make an expression to refer to the COMPONENT field of structure or
2536 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2537 location of the COMPONENT_REF. COMPONENT_LOC is the location
2538 of COMPONENT. ARROW_LOC is the location of the first -> operand if
2539 it is from -> operator. */
2542 build_component_ref (location_t loc
, tree datum
, tree component
,
2543 location_t component_loc
, location_t arrow_loc
)
2545 tree type
= TREE_TYPE (datum
);
2546 enum tree_code code
= TREE_CODE (type
);
2549 bool datum_lvalue
= lvalue_p (datum
);
2551 if (!objc_is_public (datum
, component
))
2552 return error_mark_node
;
2554 /* Detect Objective-C property syntax object.property. */
2555 if (c_dialect_objc ()
2556 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2559 /* See if there is a field or component with name COMPONENT. */
2561 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2563 if (!COMPLETE_TYPE_P (type
))
2565 c_incomplete_type_error (loc
, NULL_TREE
, type
);
2566 return error_mark_node
;
2569 field
= lookup_field (type
, component
);
2573 tree guessed_id
= lookup_field_fuzzy (type
, component
);
2576 /* Attempt to provide a fixit replacement hint, if
2577 we have a valid range for the component. */
2578 location_t reported_loc
2579 = (component_loc
!= UNKNOWN_LOCATION
) ? component_loc
: loc
;
2580 gcc_rich_location
rich_loc (reported_loc
);
2581 if (component_loc
!= UNKNOWN_LOCATION
)
2582 rich_loc
.add_fixit_misspelled_id (component_loc
, guessed_id
);
2583 error_at (&rich_loc
,
2584 "%qT has no member named %qE; did you mean %qE?",
2585 type
, component
, guessed_id
);
2588 error_at (loc
, "%qT has no member named %qE", type
, component
);
2589 return error_mark_node
;
2592 /* Accessing elements of atomic structures or unions is undefined
2593 behavior (C11 6.5.2.3#5). */
2594 if (TYPE_ATOMIC (type
) && c_inhibit_evaluation_warnings
== 0)
2596 if (code
== RECORD_TYPE
)
2597 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2598 "structure %qE", component
, datum
);
2600 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2601 "union %qE", component
, datum
);
2604 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2605 This might be better solved in future the way the C++ front
2606 end does it - by giving the anonymous entities each a
2607 separate name and type, and then have build_component_ref
2608 recursively call itself. We can't do that here. */
2611 tree subdatum
= TREE_VALUE (field
);
2614 bool use_datum_quals
;
2616 if (TREE_TYPE (subdatum
) == error_mark_node
)
2617 return error_mark_node
;
2619 /* If this is an rvalue, it does not have qualifiers in C
2620 standard terms and we must avoid propagating such
2621 qualifiers down to a non-lvalue array that is then
2622 converted to a pointer. */
2623 use_datum_quals
= (datum_lvalue
2624 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2626 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2627 if (use_datum_quals
)
2628 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2629 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2631 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2633 SET_EXPR_LOCATION (ref
, loc
);
2634 if (TREE_READONLY (subdatum
)
2635 || (use_datum_quals
&& TREE_READONLY (datum
)))
2636 TREE_READONLY (ref
) = 1;
2637 if (TREE_THIS_VOLATILE (subdatum
)
2638 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2639 TREE_THIS_VOLATILE (ref
) = 1;
2641 if (TREE_UNAVAILABLE (subdatum
))
2642 error_unavailable_use (subdatum
, NULL_TREE
);
2643 else if (TREE_DEPRECATED (subdatum
))
2644 warn_deprecated_use (subdatum
, NULL_TREE
);
2648 field
= TREE_CHAIN (field
);
2654 else if (should_suggest_deref_p (type
))
2656 /* Special-case the error message for "ptr.field" for the case
2657 where the user has confused "." vs "->". */
2658 rich_location
richloc (line_table
, loc
);
2659 if (INDIRECT_REF_P (datum
) && arrow_loc
!= UNKNOWN_LOCATION
)
2661 richloc
.add_fixit_insert_before (arrow_loc
, "(*");
2662 richloc
.add_fixit_insert_after (arrow_loc
, ")");
2664 "%qE is a pointer to pointer; did you mean to dereference "
2665 "it before applying %<->%> to it?",
2666 TREE_OPERAND (datum
, 0));
2670 /* "loc" should be the "." token. */
2671 richloc
.add_fixit_replace ("->");
2673 "%qE is a pointer; did you mean to use %<->%>?",
2676 return error_mark_node
;
2678 else if (code
!= ERROR_MARK
)
2680 "request for member %qE in something not a structure or union",
2683 return error_mark_node
;
2686 /* Given an expression PTR for a pointer, return an expression
2687 for the value pointed to.
2688 ERRORSTRING is the name of the operator to appear in error messages.
2690 LOC is the location to use for the generated tree. */
2693 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2695 tree pointer
= default_conversion (ptr
);
2696 tree type
= TREE_TYPE (pointer
);
2699 if (TREE_CODE (type
) == POINTER_TYPE
)
2701 if (CONVERT_EXPR_P (pointer
)
2702 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2704 /* If a warning is issued, mark it to avoid duplicates from
2705 the backend. This only needs to be done at
2706 warn_strict_aliasing > 2. */
2707 if (warn_strict_aliasing
> 2)
2708 if (strict_aliasing_warning (EXPR_LOCATION (pointer
),
2709 type
, TREE_OPERAND (pointer
, 0)))
2710 suppress_warning (pointer
, OPT_Wstrict_aliasing_
);
2713 if (TREE_CODE (pointer
) == ADDR_EXPR
2714 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2715 == TREE_TYPE (type
)))
2717 ref
= TREE_OPERAND (pointer
, 0);
2718 protected_set_expr_location (ref
, loc
);
2723 tree t
= TREE_TYPE (type
);
2725 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2727 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2728 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2730 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2731 so that we get the proper error message if the result is used
2732 to assign to. Also, &* is supposed to be a no-op.
2733 And ANSI C seems to specify that the type of the result
2734 should be the const type. */
2735 /* A de-reference of a pointer to const is not a const. It is valid
2736 to change it via some other pointer. */
2737 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2738 TREE_SIDE_EFFECTS (ref
)
2739 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2740 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2741 protected_set_expr_location (ref
, loc
);
2745 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2746 invalid_indirection_error (loc
, type
, errstring
);
2748 return error_mark_node
;
2751 /* This handles expressions of the form "a[i]", which denotes
2754 This is logically equivalent in C to *(a+i), but we may do it differently.
2755 If A is a variable or a member, we generate a primitive ARRAY_REF.
2756 This avoids forcing the array out of registers, and can work on
2757 arrays that are not lvalues (for example, members of structures returned
2760 For vector types, allow vector[i] but not i[vector], and create
2761 *(((type*)&vectortype) + i) for the expression.
2763 LOC is the location to use for the returned expression. */
2766 build_array_ref (location_t loc
, tree array
, tree index
)
2769 bool swapped
= false;
2770 if (TREE_TYPE (array
) == error_mark_node
2771 || TREE_TYPE (index
) == error_mark_node
)
2772 return error_mark_node
;
2774 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2775 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2776 /* Allow vector[index] but not index[vector]. */
2777 && !gnu_vector_type_p (TREE_TYPE (array
)))
2779 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2780 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2783 "subscripted value is neither array nor pointer nor vector");
2785 return error_mark_node
;
2787 std::swap (array
, index
);
2791 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2793 error_at (loc
, "array subscript is not an integer");
2794 return error_mark_node
;
2797 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2799 error_at (loc
, "subscripted value is pointer to function");
2800 return error_mark_node
;
2803 /* ??? Existing practice has been to warn only when the char
2804 index is syntactically the index, not for char[array]. */
2806 warn_array_subscript_with_type_char (loc
, index
);
2808 /* Apply default promotions *after* noticing character types. */
2809 index
= default_conversion (index
);
2810 if (index
== error_mark_node
)
2811 return error_mark_node
;
2813 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
2814 || TREE_CODE (TREE_TYPE (index
)) == BITINT_TYPE
);
2816 bool was_vector
= VECTOR_TYPE_P (TREE_TYPE (array
));
2817 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, index
);
2819 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2823 /* An array that is indexed by a non-constant
2824 cannot be stored in a register; we must be able to do
2825 address arithmetic on its address.
2826 Likewise an array of elements of variable size. */
2827 if (TREE_CODE (index
) != INTEGER_CST
2828 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2829 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2831 if (!c_mark_addressable (array
, true))
2832 return error_mark_node
;
2834 /* An array that is indexed by a constant value which is not within
2835 the array bounds cannot be stored in a register either; because we
2836 would get a crash in store_bit_field/extract_bit_field when trying
2837 to access a non-existent part of the register. */
2838 if (TREE_CODE (index
) == INTEGER_CST
2839 && TYPE_DOMAIN (TREE_TYPE (array
))
2840 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2842 if (!c_mark_addressable (array
))
2843 return error_mark_node
;
2846 if ((pedantic
|| warn_c90_c99_compat
)
2850 while (TREE_CODE (foo
) == COMPONENT_REF
)
2851 foo
= TREE_OPERAND (foo
, 0);
2852 if (VAR_P (foo
) && C_DECL_REGISTER (foo
))
2853 pedwarn (loc
, OPT_Wpedantic
,
2854 "ISO C forbids subscripting %<register%> array");
2855 else if (!lvalue_p (foo
))
2856 pedwarn_c90 (loc
, OPT_Wpedantic
,
2857 "ISO C90 forbids subscripting non-lvalue "
2861 type
= TREE_TYPE (TREE_TYPE (array
));
2862 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2863 /* Array ref is const/volatile if the array elements are
2864 or if the array is. */
2865 TREE_READONLY (rval
)
2866 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2867 | TREE_READONLY (array
));
2868 TREE_SIDE_EFFECTS (rval
)
2869 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2870 | TREE_SIDE_EFFECTS (array
));
2871 TREE_THIS_VOLATILE (rval
)
2872 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2873 /* This was added by rms on 16 Nov 91.
2874 It fixes vol struct foo *a; a->elts[1]
2875 in an inline function.
2876 Hope it doesn't break something else. */
2877 | TREE_THIS_VOLATILE (array
));
2878 ret
= require_complete_type (loc
, rval
);
2879 protected_set_expr_location (ret
, loc
);
2881 ret
= non_lvalue_loc (loc
, ret
);
2886 tree ar
= default_conversion (array
);
2888 if (ar
== error_mark_node
)
2891 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2892 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2894 ret
= build_indirect_ref (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
,
2898 ret
= non_lvalue_loc (loc
, ret
);
2903 /* Build an external reference to identifier ID. FUN indicates
2904 whether this will be used for a function call. LOC is the source
2905 location of the identifier. This sets *TYPE to the type of the
2906 identifier, which is not the same as the type of the returned value
2907 for CONST_DECLs defined as enum constants. If the type of the
2908 identifier is not available, *TYPE is set to NULL. */
2910 build_external_ref (location_t loc
, tree id
, bool fun
, tree
*type
)
2913 tree decl
= lookup_name (id
);
2915 /* In Objective-C, an instance variable (ivar) may be preferred to
2916 whatever lookup_name() found. */
2917 decl
= objc_lookup_ivar (decl
, id
);
2920 if (decl
&& decl
!= error_mark_node
)
2923 *type
= TREE_TYPE (ref
);
2924 if (DECL_P (decl
) && C_DECL_UNDERSPECIFIED (decl
))
2925 error_at (loc
, "underspecified %qD referenced in its initializer",
2929 /* Implicit function declaration. */
2930 ref
= implicitly_declare (loc
, id
);
2931 else if (decl
== error_mark_node
)
2932 /* Don't complain about something that's already been
2933 complained about. */
2934 return error_mark_node
;
2937 undeclared_variable (loc
, id
);
2938 return error_mark_node
;
2941 if (TREE_TYPE (ref
) == error_mark_node
)
2942 return error_mark_node
;
2944 if (TREE_UNAVAILABLE (ref
))
2945 error_unavailable_use (ref
, NULL_TREE
);
2946 else if (TREE_DEPRECATED (ref
))
2947 warn_deprecated_use (ref
, NULL_TREE
);
2949 /* Recursive call does not count as usage. */
2950 if (ref
!= current_function_decl
)
2952 TREE_USED (ref
) = 1;
2955 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2957 if (!in_sizeof
&& !in_typeof
)
2958 C_DECL_USED (ref
) = 1;
2959 else if (DECL_INITIAL (ref
) == NULL_TREE
2960 && DECL_EXTERNAL (ref
)
2961 && !TREE_PUBLIC (ref
))
2962 record_maybe_used_decl (ref
);
2965 if (TREE_CODE (ref
) == CONST_DECL
)
2967 used_types_insert (TREE_TYPE (ref
));
2970 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2971 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2973 warning_at (loc
, OPT_Wc___compat
,
2974 ("enum constant defined in struct or union "
2975 "is not visible in C++"));
2976 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2979 ref
= DECL_INITIAL (ref
);
2980 TREE_CONSTANT (ref
) = 1;
2982 else if (current_function_decl
!= NULL_TREE
2983 && !DECL_FILE_SCOPE_P (current_function_decl
)
2984 && (VAR_OR_FUNCTION_DECL_P (ref
)
2985 || TREE_CODE (ref
) == PARM_DECL
))
2987 tree context
= decl_function_context (ref
);
2989 if (context
!= NULL_TREE
&& context
!= current_function_decl
)
2990 DECL_NONLOCAL (ref
) = 1;
2992 /* C99 6.7.4p3: An inline definition of a function with external
2993 linkage ... shall not contain a reference to an identifier with
2994 internal linkage. */
2995 else if (current_function_decl
!= NULL_TREE
2996 && DECL_DECLARED_INLINE_P (current_function_decl
)
2997 && DECL_EXTERNAL (current_function_decl
)
2998 && VAR_OR_FUNCTION_DECL_P (ref
)
2999 && (!VAR_P (ref
) || TREE_STATIC (ref
))
3000 && ! TREE_PUBLIC (ref
)
3001 && DECL_CONTEXT (ref
) != current_function_decl
)
3002 record_inline_static (loc
, current_function_decl
, ref
,
3008 /* Record details of decls possibly used inside sizeof or typeof. */
3009 struct maybe_used_decl
3013 /* The level seen at (in_sizeof + in_typeof). */
3015 /* The next one at this level or above, or NULL. */
3016 struct maybe_used_decl
*next
;
3019 static struct maybe_used_decl
*maybe_used_decls
;
3021 /* Record that DECL, an undefined static function reference seen
3022 inside sizeof or typeof, might be used if the operand of sizeof is
3023 a VLA type or the operand of typeof is a variably modified
3027 record_maybe_used_decl (tree decl
)
3029 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
3031 t
->level
= in_sizeof
+ in_typeof
;
3032 t
->next
= maybe_used_decls
;
3033 maybe_used_decls
= t
;
3036 /* Pop the stack of decls possibly used inside sizeof or typeof. If
3037 USED is false, just discard them. If it is true, mark them used
3038 (if no longer inside sizeof or typeof) or move them to the next
3039 level up (if still inside sizeof or typeof). */
3042 pop_maybe_used (bool used
)
3044 struct maybe_used_decl
*p
= maybe_used_decls
;
3045 int cur_level
= in_sizeof
+ in_typeof
;
3046 while (p
&& p
->level
> cur_level
)
3051 C_DECL_USED (p
->decl
) = 1;
3053 p
->level
= cur_level
;
3057 if (!used
|| cur_level
== 0)
3058 maybe_used_decls
= p
;
3061 /* Return the result of sizeof applied to EXPR. */
3064 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
3067 if (expr
.value
== error_mark_node
)
3069 ret
.value
= error_mark_node
;
3070 ret
.original_code
= ERROR_MARK
;
3071 ret
.original_type
= NULL
;
3073 pop_maybe_used (false);
3077 bool expr_const_operands
= true;
3079 if (TREE_CODE (expr
.value
) == PARM_DECL
3080 && C_ARRAY_PARAMETER (expr
.value
))
3082 auto_diagnostic_group d
;
3083 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
3084 "%<sizeof%> on array function parameter %qE will "
3085 "return size of %qT", expr
.value
,
3086 TREE_TYPE (expr
.value
)))
3087 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
3089 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
3090 &expr_const_operands
);
3091 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
3092 c_last_sizeof_arg
= expr
.value
;
3093 c_last_sizeof_loc
= loc
;
3094 ret
.original_code
= SIZEOF_EXPR
;
3095 ret
.original_type
= NULL
;
3097 if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)))
3099 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
3100 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
3101 folded_expr
, ret
.value
);
3102 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
3103 SET_EXPR_LOCATION (ret
.value
, loc
);
3105 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
3110 /* Return the result of sizeof applied to T, a structure for the type
3111 name passed to sizeof (rather than the type itself). LOC is the
3112 location of the original expression. */
3115 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
3119 tree type_expr
= NULL_TREE
;
3120 bool type_expr_const
= true;
3121 type
= groktypename (t
, &type_expr
, &type_expr_const
);
3122 ret
.value
= c_sizeof (loc
, type
);
3123 c_last_sizeof_arg
= type
;
3124 c_last_sizeof_loc
= loc
;
3125 ret
.original_code
= SIZEOF_EXPR
;
3126 ret
.original_type
= NULL
;
3128 if (type
== error_mark_node
)
3130 ret
.value
= error_mark_node
;
3131 ret
.original_code
= ERROR_MARK
;
3134 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
3135 && C_TYPE_VARIABLE_SIZE (type
))
3137 /* If the type is a [*] array, it is a VLA but is represented as
3138 having a size of zero. In such a case we must ensure that
3139 the result of sizeof does not get folded to a constant by
3140 c_fully_fold, because if the size is evaluated the result is
3141 not constant and so constraints on zero or negative size
3142 arrays must not be applied when this sizeof call is inside
3143 another array declarator. */
3145 type_expr
= integer_zero_node
;
3146 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
3147 type_expr
, ret
.value
);
3148 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
3150 pop_maybe_used (type
!= error_mark_node
3151 ? C_TYPE_VARIABLE_SIZE (type
) : false);
3155 /* Build a function call to function FUNCTION with parameters PARAMS.
3156 The function call is at LOC.
3157 PARAMS is a list--a chain of TREE_LIST nodes--in which the
3158 TREE_VALUE of each node is a parameter-expression.
3159 FUNCTION's data type may be a function type or a pointer-to-function. */
3162 build_function_call (location_t loc
, tree function
, tree params
)
3164 vec
<tree
, va_gc
> *v
;
3167 vec_alloc (v
, list_length (params
));
3168 for (; params
; params
= TREE_CHAIN (params
))
3169 v
->quick_push (TREE_VALUE (params
));
3170 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
3175 /* Give a note about the location of the declaration of DECL. */
3178 inform_declaration (tree decl
)
3180 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
3181 || !DECL_IS_UNDECLARED_BUILTIN (decl
)))
3182 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
3185 /* Build a function call to function FUNCTION with parameters PARAMS.
3186 If FUNCTION is the result of resolving an overloaded target built-in,
3187 ORIG_FUNDECL is the original function decl, otherwise it is null.
3188 ORIGTYPES, if not NULL, is a vector of types; each element is
3189 either NULL or the original type of the corresponding element in
3190 PARAMS. The original type may differ from TREE_TYPE of the
3191 parameter for enums. FUNCTION's data type may be a function type
3192 or pointer-to-function. This function changes the elements of
3196 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3197 tree function
, vec
<tree
, va_gc
> *params
,
3198 vec
<tree
, va_gc
> *origtypes
, tree orig_fundecl
)
3200 tree fntype
, fundecl
= NULL_TREE
;
3201 tree name
= NULL_TREE
, result
;
3207 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3208 STRIP_TYPE_NOPS (function
);
3210 /* Convert anything with function type to a pointer-to-function. */
3211 if (TREE_CODE (function
) == FUNCTION_DECL
)
3213 name
= DECL_NAME (function
);
3216 tm_malloc_replacement (function
);
3219 orig_fundecl
= fundecl
;
3220 /* Atomic functions have type checking/casting already done. They are
3221 often rewritten and don't match the original parameter list. */
3222 if (name
&& startswith (IDENTIFIER_POINTER (name
), "__atomic_"))
3225 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
3226 function
= function_to_pointer_conversion (loc
, function
);
3228 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3229 expressions, like those used for ObjC messenger dispatches. */
3230 if (params
&& !params
->is_empty ())
3231 function
= objc_rewrite_function_call (function
, (*params
)[0]);
3233 function
= c_fully_fold (function
, false, NULL
);
3235 fntype
= TREE_TYPE (function
);
3237 if (TREE_CODE (fntype
) == ERROR_MARK
)
3238 return error_mark_node
;
3240 if (!(TREE_CODE (fntype
) == POINTER_TYPE
3241 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
3243 if (!flag_diagnostics_show_caret
&& !STATEMENT_CLASS_P (function
))
3245 "called object %qE is not a function or function pointer",
3247 else if (DECL_P (function
))
3250 "called object %qD is not a function or function pointer",
3252 inform_declaration (function
);
3256 "called object is not a function or function pointer");
3257 return error_mark_node
;
3260 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
3261 current_function_returns_abnormally
= 1;
3263 /* fntype now gets the type of function pointed to. */
3264 fntype
= TREE_TYPE (fntype
);
3265 tree return_type
= TREE_TYPE (fntype
);
3267 /* Convert the parameters to the types declared in the
3268 function prototype, or apply default promotions. */
3270 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
3271 origtypes
, function
, fundecl
);
3273 return error_mark_node
;
3275 /* Check that the function is called through a compatible prototype.
3276 If it is not, warn. */
3277 if (CONVERT_EXPR_P (function
)
3278 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
3279 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
3280 && !comptypes (fntype
, TREE_TYPE (tem
)))
3282 /* This situation leads to run-time undefined behavior. We can't,
3283 therefore, simply error unless we can prove that all possible
3284 executions of the program must execute the code. */
3285 warning_at (loc
, 0, "function called through a non-compatible type");
3287 if (VOID_TYPE_P (return_type
)
3288 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
3290 "function with qualified void return type called");
3293 argarray
= vec_safe_address (params
);
3295 /* Check that arguments to builtin functions match the expectations. */
3297 && fndecl_built_in_p (fundecl
)
3298 && !check_builtin_function_arguments (loc
, arg_loc
, fundecl
,
3299 orig_fundecl
, nargs
, argarray
))
3300 return error_mark_node
;
3302 /* Check that the arguments to the function are valid. */
3303 bool warned_p
= check_function_arguments (loc
, fundecl
, fntype
,
3304 nargs
, argarray
, &arg_loc
);
3306 if (TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
3307 && !VOID_TYPE_P (return_type
))
3308 return_type
= c_build_qualified_type (return_type
, TYPE_UNQUALIFIED
);
3309 if (name
!= NULL_TREE
3310 && startswith (IDENTIFIER_POINTER (name
), "__builtin_"))
3312 if (require_constant_value
)
3314 = fold_build_call_array_initializer_loc (loc
, return_type
,
3315 function
, nargs
, argarray
);
3317 result
= fold_build_call_array_loc (loc
, return_type
,
3318 function
, nargs
, argarray
);
3319 if (TREE_CODE (result
) == NOP_EXPR
3320 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
3321 STRIP_TYPE_NOPS (result
);
3324 result
= build_call_array_loc (loc
, return_type
,
3325 function
, nargs
, argarray
);
3326 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3328 if (warned_p
&& TREE_CODE (result
) == CALL_EXPR
)
3329 suppress_warning (result
, OPT_Wnonnull
);
3331 /* In this improbable scenario, a nested function returns a VM type.
3332 Create a TARGET_EXPR so that the call always has a LHS, much as
3333 what the C++ FE does for functions returning non-PODs. */
3334 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (fntype
)))
3336 tree tmp
= create_tmp_var_raw (TREE_TYPE (fntype
));
3337 result
= build4 (TARGET_EXPR
, TREE_TYPE (fntype
), tmp
, result
,
3338 NULL_TREE
, NULL_TREE
);
3341 if (VOID_TYPE_P (TREE_TYPE (result
)))
3343 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
3345 "function with qualified void return type called");
3348 return require_complete_type (loc
, result
);
3351 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3354 c_build_function_call_vec (location_t loc
, const vec
<location_t
> &arg_loc
,
3355 tree function
, vec
<tree
, va_gc
> *params
,
3356 vec
<tree
, va_gc
> *origtypes
)
3358 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3359 STRIP_TYPE_NOPS (function
);
3361 /* Convert anything with function type to a pointer-to-function. */
3362 if (TREE_CODE (function
) == FUNCTION_DECL
)
3364 /* Implement type-directed function overloading for builtins.
3365 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3366 handle all the type checking. The result is a complete expression
3367 that implements this function call. */
3368 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3372 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3375 /* Helper for convert_arguments called to convert the VALue of argument
3376 number ARGNUM from ORIGTYPE to the corresponding parameter number
3378 PLOC is the location where the conversion is being performed.
3379 FUNCTION and FUNDECL are the same as in convert_arguments.
3380 VALTYPE is the original type of VAL before the conversion and,
3381 for EXCESS_PRECISION_EXPR, the operand of the expression.
3382 NPC is true if VAL represents the null pointer constant (VAL itself
3383 will have been folded to an integer constant).
3384 RNAME is the same as FUNCTION except in Objective C when it's
3385 the function selector.
3386 EXCESS_PRECISION is true when VAL was originally represented
3387 as EXCESS_PRECISION_EXPR.
3388 WARNOPT is the same as in convert_for_assignment. */
3391 convert_argument (location_t ploc
, tree function
, tree fundecl
,
3392 tree type
, tree origtype
, tree val
, tree valtype
,
3393 bool npc
, tree rname
, int parmnum
, int argnum
,
3394 bool excess_precision
, int warnopt
)
3396 /* Formal parm type is specified by a function prototype. */
3398 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3400 error_at (ploc
, "type of formal parameter %d is incomplete",
3402 return error_mark_node
;
3405 /* Optionally warn about conversions that differ from the default
3407 if (warn_traditional_conversion
|| warn_traditional
)
3409 if (INTEGRAL_TYPE_P (type
)
3410 && SCALAR_FLOAT_TYPE_P (valtype
))
3411 warning_at (ploc
, OPT_Wtraditional_conversion
,
3412 "passing argument %d of %qE as integer rather "
3413 "than floating due to prototype",
3415 if (INTEGRAL_TYPE_P (type
)
3416 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3417 warning_at (ploc
, OPT_Wtraditional_conversion
,
3418 "passing argument %d of %qE as integer rather "
3419 "than complex due to prototype",
3421 else if (TREE_CODE (type
) == COMPLEX_TYPE
3422 && SCALAR_FLOAT_TYPE_P (valtype
))
3423 warning_at (ploc
, OPT_Wtraditional_conversion
,
3424 "passing argument %d of %qE as complex rather "
3425 "than floating due to prototype",
3427 else if (SCALAR_FLOAT_TYPE_P (type
)
3428 && INTEGRAL_TYPE_P (valtype
))
3429 warning_at (ploc
, OPT_Wtraditional_conversion
,
3430 "passing argument %d of %qE as floating rather "
3431 "than integer due to prototype",
3433 else if (TREE_CODE (type
) == COMPLEX_TYPE
3434 && INTEGRAL_TYPE_P (valtype
))
3435 warning_at (ploc
, OPT_Wtraditional_conversion
,
3436 "passing argument %d of %qE as complex rather "
3437 "than integer due to prototype",
3439 else if (SCALAR_FLOAT_TYPE_P (type
)
3440 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3441 warning_at (ploc
, OPT_Wtraditional_conversion
,
3442 "passing argument %d of %qE as floating rather "
3443 "than complex due to prototype",
3445 /* ??? At some point, messages should be written about
3446 conversions between complex types, but that's too messy
3448 else if (SCALAR_FLOAT_TYPE_P (type
)
3449 && SCALAR_FLOAT_TYPE_P (valtype
))
3451 unsigned int formal_prec
= TYPE_PRECISION (type
);
3453 /* Warn if any argument is passed as `float',
3454 since without a prototype it would be `double'. */
3455 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3456 && type
!= dfloat32_type_node
)
3457 warning_at (ploc
, 0,
3458 "passing argument %d of %qE as %<float%> "
3459 "rather than %<double%> due to prototype",
3462 /* Warn if mismatch between argument and prototype
3463 for decimal float types. Warn of conversions with
3464 binary float types and of precision narrowing due to
3466 else if (type
!= valtype
3467 && (type
== dfloat32_type_node
3468 || type
== dfloat64_type_node
3469 || type
== dfloat128_type_node
3470 || valtype
== dfloat32_type_node
3471 || valtype
== dfloat64_type_node
3472 || valtype
== dfloat128_type_node
)
3474 <= TYPE_PRECISION (valtype
)
3475 || (type
== dfloat128_type_node
3477 != dfloat64_type_node
3479 != dfloat32_type_node
)))
3480 || (type
== dfloat64_type_node
3482 != dfloat32_type_node
))))
3483 warning_at (ploc
, 0,
3484 "passing argument %d of %qE as %qT "
3485 "rather than %qT due to prototype",
3486 argnum
, rname
, type
, valtype
);
3489 /* Detect integer changing in width or signedness.
3490 These warnings are only activated with
3491 -Wtraditional-conversion, not with -Wtraditional. */
3492 else if (warn_traditional_conversion
3493 && INTEGRAL_TYPE_P (type
)
3494 && INTEGRAL_TYPE_P (valtype
))
3496 unsigned int formal_prec
= TYPE_PRECISION (type
);
3497 tree would_have_been
= default_conversion (val
);
3498 tree type1
= TREE_TYPE (would_have_been
);
3500 if (val
== error_mark_node
)
3501 /* VAL could have been of incomplete type. */;
3502 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3503 && (TYPE_MAIN_VARIANT (type
)
3504 == TYPE_MAIN_VARIANT (valtype
)))
3505 /* No warning if function asks for enum
3506 and the actual arg is that enum type. */
3508 else if (formal_prec
!= TYPE_PRECISION (type1
))
3509 warning_at (ploc
, OPT_Wtraditional_conversion
,
3510 "passing argument %d of %qE "
3511 "with different width due to prototype",
3513 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3515 /* Don't complain if the formal parameter type
3516 is an enum, because we can't tell now whether
3517 the value was an enum--even the same enum. */
3518 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3520 else if (TREE_CODE (val
) == INTEGER_CST
3521 && int_fits_type_p (val
, type
))
3522 /* Change in signedness doesn't matter
3523 if a constant value is unaffected. */
3525 /* If the value is extended from a narrower
3526 unsigned type, it doesn't matter whether we
3527 pass it as signed or unsigned; the value
3528 certainly is the same either way. */
3529 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3530 && TYPE_UNSIGNED (valtype
))
3532 else if (TYPE_UNSIGNED (type
))
3533 warning_at (ploc
, OPT_Wtraditional_conversion
,
3534 "passing argument %d of %qE "
3535 "as unsigned due to prototype",
3538 warning_at (ploc
, OPT_Wtraditional_conversion
,
3539 "passing argument %d of %qE "
3540 "as signed due to prototype",
3545 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3546 sake of better warnings from convert_and_check. */
3547 if (excess_precision
)
3548 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3550 tree parmval
= convert_for_assignment (ploc
, ploc
, type
,
3551 val
, origtype
, ic_argpass
,
3552 npc
, fundecl
, function
,
3553 parmnum
+ 1, warnopt
);
3555 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3556 && INTEGRAL_TYPE_P (type
)
3557 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3558 parmval
= default_conversion (parmval
);
3563 /* Convert the argument expressions in the vector VALUES
3564 to the types in the list TYPELIST.
3566 If TYPELIST is exhausted, or when an element has NULL as its type,
3567 perform the default conversions.
3569 ORIGTYPES is the original types of the expressions in VALUES. This
3570 holds the type of enum values which have been converted to integral
3571 types. It may be NULL.
3573 FUNCTION is a tree for the called function. It is used only for
3574 error messages, where it is formatted with %qE.
3576 This is also where warnings about wrong number of args are generated.
3578 ARG_LOC are locations of function arguments (if any).
3580 Returns the actual number of arguments processed (which may be less
3581 than the length of VALUES in some error situations), or -1 on
3585 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3586 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3587 tree function
, tree fundecl
)
3589 unsigned int parmnum
;
3590 bool error_args
= false;
3591 const bool type_generic
= fundecl
3592 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3593 bool type_generic_remove_excess_precision
= false;
3594 bool type_generic_overflow_p
= false;
3595 bool type_generic_bit_query
= false;
3598 /* Change pointer to function to the function itself for
3600 if (TREE_CODE (function
) == ADDR_EXPR
3601 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3602 function
= TREE_OPERAND (function
, 0);
3604 /* Handle an ObjC selector specially for diagnostics. */
3605 selector
= objc_message_selector ();
3607 /* For a call to a built-in function declared without a prototype,
3608 set to the built-in function's argument list. */
3609 tree builtin_typelist
= NULL_TREE
;
3611 /* For type-generic built-in functions, determine whether excess
3612 precision should be removed (classification) or not
3615 && fndecl_built_in_p (fundecl
, BUILT_IN_NORMAL
))
3617 built_in_function code
= DECL_FUNCTION_CODE (fundecl
);
3618 if (C_DECL_BUILTIN_PROTOTYPE (fundecl
))
3620 /* For a call to a built-in function declared without a prototype
3621 use the types of the parameters of the internal built-in to
3622 match those of the arguments to. */
3623 if (tree bdecl
= builtin_decl_explicit (code
))
3624 builtin_typelist
= TYPE_ARG_TYPES (TREE_TYPE (bdecl
));
3627 /* For type-generic built-in functions, determine whether excess
3628 precision should be removed (classification) or not
3633 case BUILT_IN_ISFINITE
:
3634 case BUILT_IN_ISINF
:
3635 case BUILT_IN_ISINF_SIGN
:
3636 case BUILT_IN_ISNAN
:
3637 case BUILT_IN_ISNORMAL
:
3638 case BUILT_IN_ISSIGNALING
:
3639 case BUILT_IN_FPCLASSIFY
:
3640 type_generic_remove_excess_precision
= true;
3643 case BUILT_IN_ADD_OVERFLOW_P
:
3644 case BUILT_IN_SUB_OVERFLOW_P
:
3645 case BUILT_IN_MUL_OVERFLOW_P
:
3646 /* The last argument of these type-generic builtins
3647 should not be promoted. */
3648 type_generic_overflow_p
= true;
3653 case BUILT_IN_CLRSBG
:
3655 case BUILT_IN_PARITYG
:
3656 case BUILT_IN_POPCOUNTG
:
3657 /* The first argument of these type-generic builtins
3658 should not be promoted. */
3659 type_generic_bit_query
= true;
3667 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3668 individual converted arguments. */
3670 tree typetail
, builtin_typetail
, val
;
3671 for (typetail
= typelist
,
3672 builtin_typetail
= builtin_typelist
,
3674 values
&& values
->iterate (parmnum
, &val
);
3677 /* The type of the function parameter (if it was declared with one). */
3678 tree type
= typetail
? TREE_VALUE (typetail
) : NULL_TREE
;
3679 /* The type of the built-in function parameter (if the function
3680 is a built-in). Used to detect type incompatibilities in
3681 calls to built-ins declared without a prototype. */
3682 tree builtin_type
= (builtin_typetail
3683 ? TREE_VALUE (builtin_typetail
) : NULL_TREE
);
3684 /* The original type of the argument being passed to the function. */
3685 tree valtype
= TREE_TYPE (val
);
3686 /* The called function (or function selector in Objective C). */
3687 tree rname
= function
;
3688 int argnum
= parmnum
+ 1;
3689 const char *invalid_func_diag
;
3690 /* Set for EXCESS_PRECISION_EXPR arguments. */
3691 bool excess_precision
= false;
3692 /* The value of the argument after conversion to the type
3693 of the function parameter it is passed to. */
3695 /* Some __atomic_* builtins have additional hidden argument at
3698 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3699 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3702 if (type
== void_type_node
)
3705 error_at (loc
, "too many arguments to method %qE", selector
);
3707 error_at (loc
, "too many arguments to function %qE", function
);
3708 inform_declaration (fundecl
);
3709 return error_args
? -1 : (int) parmnum
;
3712 if (builtin_type
== void_type_node
)
3714 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
3715 "too many arguments to built-in function %qE "
3716 "expecting %d", function
, parmnum
))
3717 inform_declaration (fundecl
);
3718 builtin_typetail
= NULL_TREE
;
3721 if (selector
&& argnum
> 2)
3727 /* Determine if VAL is a null pointer constant before folding it. */
3728 bool npc
= null_pointer_constant_p (val
);
3730 /* If there is excess precision and a prototype, convert once to
3731 the required type rather than converting via the semantic
3732 type. Likewise without a prototype a float value represented
3733 as long double should be converted once to double. But for
3734 type-generic classification functions excess precision must
3736 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3737 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3739 val
= TREE_OPERAND (val
, 0);
3740 excess_precision
= true;
3742 val
= c_fully_fold (val
, false, NULL
);
3743 STRIP_TYPE_NOPS (val
);
3745 val
= require_complete_type (ploc
, val
);
3747 /* Some floating-point arguments must be promoted to double when
3748 no type is specified by a prototype. This applies to
3749 arguments of type float, and to architecture-specific types
3750 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3751 bool promote_float_arg
= false;
3752 if (type
== NULL_TREE
3753 && TREE_CODE (valtype
) == REAL_TYPE
3754 && (TYPE_PRECISION (valtype
)
3755 <= TYPE_PRECISION (double_type_node
))
3756 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3757 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3758 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3760 /* Promote this argument, unless it has a _FloatN or
3762 promote_float_arg
= true;
3763 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
3764 if (TYPE_MAIN_VARIANT (valtype
) == FLOATN_NX_TYPE_NODE (i
))
3766 promote_float_arg
= false;
3769 /* Don't promote __bf16 either. */
3770 if (TYPE_MAIN_VARIANT (valtype
) == bfloat16_type_node
)
3771 promote_float_arg
= false;
3774 if (type
!= NULL_TREE
)
3776 tree origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3777 parmval
= convert_argument (ploc
, function
, fundecl
, type
, origtype
,
3778 val
, valtype
, npc
, rname
, parmnum
, argnum
,
3779 excess_precision
, 0);
3781 else if (promote_float_arg
)
3787 /* Convert `float' to `double'. */
3788 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3789 warning_at (ploc
, OPT_Wdouble_promotion
,
3790 "implicit conversion from %qT to %qT when passing "
3791 "argument to function",
3792 valtype
, double_type_node
);
3793 parmval
= convert (double_type_node
, val
);
3796 else if ((excess_precision
&& !type_generic
)
3797 || (type_generic_overflow_p
&& parmnum
== 2)
3798 || (type_generic_bit_query
&& parmnum
== 0))
3799 /* A "double" argument with excess precision being passed
3800 without a prototype or in variable arguments.
3801 The last argument of __builtin_*_overflow_p should not be
3802 promoted, similarly the first argument of
3803 __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */
3804 parmval
= convert (valtype
, val
);
3805 else if ((invalid_func_diag
=
3806 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3808 error (invalid_func_diag
);
3811 else if (TREE_CODE (val
) == ADDR_EXPR
&& reject_gcc_builtin (val
))
3816 /* Convert `short' and `char' to full-size `int'. */
3817 parmval
= default_conversion (val
);
3819 (*values
)[parmnum
] = parmval
;
3820 if (parmval
== error_mark_node
)
3823 if (!type
&& builtin_type
&& TREE_CODE (builtin_type
) != VOID_TYPE
)
3825 /* For a call to a built-in function declared without a prototype,
3826 perform the conversions from the argument to the expected type
3827 but issue warnings rather than errors for any mismatches.
3828 Ignore the converted argument and use the PARMVAL obtained
3829 above by applying default conversions instead. */
3830 tree origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3831 convert_argument (ploc
, function
, fundecl
, builtin_type
, origtype
,
3832 val
, valtype
, npc
, rname
, parmnum
, argnum
,
3834 OPT_Wbuiltin_declaration_mismatch
);
3838 typetail
= TREE_CHAIN (typetail
);
3840 if (builtin_typetail
)
3841 builtin_typetail
= TREE_CHAIN (builtin_typetail
);
3844 gcc_assert (parmnum
== vec_safe_length (values
));
3846 if (typetail
!= NULL_TREE
&& TREE_VALUE (typetail
) != void_type_node
)
3848 error_at (loc
, "too few arguments to function %qE", function
);
3849 inform_declaration (fundecl
);
3853 if (builtin_typetail
&& TREE_VALUE (builtin_typetail
) != void_type_node
)
3855 unsigned nargs
= parmnum
;
3856 for (tree t
= builtin_typetail
; t
; t
= TREE_CHAIN (t
))
3859 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
3860 "too few arguments to built-in function %qE "
3861 "expecting %u", function
, nargs
- 1))
3862 inform_declaration (fundecl
);
3865 return error_args
? -1 : (int) parmnum
;
3868 /* This is the entry point used by the parser to build unary operators
3869 in the input. CODE, a tree_code, specifies the unary operator, and
3870 ARG is the operand. For unary plus, the C parser currently uses
3871 CONVERT_EXPR for code.
3873 LOC is the location to use for the tree generated.
3877 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3879 struct c_expr result
;
3881 result
.original_code
= code
;
3882 result
.original_type
= NULL
;
3883 result
.m_decimal
= 0;
3885 if (reject_gcc_builtin (arg
.value
))
3887 result
.value
= error_mark_node
;
3891 result
.value
= build_unary_op (loc
, code
, arg
.value
, false);
3893 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3894 overflow_warning (loc
, result
.value
, arg
.value
);
3897 /* We are typically called when parsing a prefix token at LOC acting on
3898 ARG. Reflect this by updating the source range of the result to
3899 start at LOC and end at the end of ARG. */
3900 set_c_expr_source_range (&result
,
3901 loc
, arg
.get_finish ());
3906 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3909 char_type_p (tree type
)
3911 return (type
== char_type_node
3912 || type
== unsigned_char_type_node
3913 || type
== signed_char_type_node
3914 || type
== char16_type_node
3915 || type
== char32_type_node
);
3918 /* This is the entry point used by the parser to build binary operators
3919 in the input. CODE, a tree_code, specifies the binary operator, and
3920 ARG1 and ARG2 are the operands. In addition to constructing the
3921 expression, we check for operands that were written with other binary
3922 operators in a way that is likely to confuse the user.
3924 LOCATION is the location of the binary operator. */
3927 parser_build_binary_op (location_t location
, enum tree_code code
,
3928 struct c_expr arg1
, struct c_expr arg2
)
3930 struct c_expr result
;
3931 result
.m_decimal
= 0;
3933 enum tree_code code1
= arg1
.original_code
;
3934 enum tree_code code2
= arg2
.original_code
;
3935 tree type1
= (arg1
.original_type
3936 ? arg1
.original_type
3937 : TREE_TYPE (arg1
.value
));
3938 tree type2
= (arg2
.original_type
3939 ? arg2
.original_type
3940 : TREE_TYPE (arg2
.value
));
3942 result
.value
= build_binary_op (location
, code
,
3943 arg1
.value
, arg2
.value
, true);
3944 result
.original_code
= code
;
3945 result
.original_type
= NULL
;
3946 result
.m_decimal
= 0;
3948 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3950 set_c_expr_source_range (&result
,
3952 arg2
.get_finish ());
3956 if (location
!= UNKNOWN_LOCATION
)
3957 protected_set_expr_location (result
.value
, location
);
3959 set_c_expr_source_range (&result
,
3961 arg2
.get_finish ());
3963 /* Check for cases such as x+y<<z which users are likely
3965 if (warn_parentheses
)
3966 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3969 if (warn_logical_op
)
3970 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3971 code1
, arg1
.value
, code2
, arg2
.value
);
3973 if (warn_tautological_compare
)
3975 tree lhs
= arg1
.value
;
3976 tree rhs
= arg2
.value
;
3977 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
3979 if (C_MAYBE_CONST_EXPR_PRE (lhs
) != NULL_TREE
3980 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs
)))
3983 lhs
= C_MAYBE_CONST_EXPR_EXPR (lhs
);
3985 if (TREE_CODE (rhs
) == C_MAYBE_CONST_EXPR
)
3987 if (C_MAYBE_CONST_EXPR_PRE (rhs
) != NULL_TREE
3988 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs
)))
3991 rhs
= C_MAYBE_CONST_EXPR_EXPR (rhs
);
3993 if (lhs
!= NULL_TREE
&& rhs
!= NULL_TREE
)
3994 warn_tautological_cmp (location
, code
, lhs
, rhs
);
3997 if (warn_logical_not_paren
3998 && TREE_CODE_CLASS (code
) == tcc_comparison
3999 && code1
== TRUTH_NOT_EXPR
4000 && code2
!= TRUTH_NOT_EXPR
4001 /* Avoid warning for !!x == y. */
4002 && (TREE_CODE (arg1
.value
) != NE_EXPR
4003 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
4005 /* Avoid warning for !b == y where b has _Bool type. */
4006 tree t
= integer_zero_node
;
4007 if (TREE_CODE (arg1
.value
) == EQ_EXPR
4008 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
4009 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
4011 t
= TREE_OPERAND (arg1
.value
, 0);
4014 if (TREE_TYPE (t
) != integer_type_node
)
4016 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
4017 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
4018 else if (CONVERT_EXPR_P (t
))
4019 t
= TREE_OPERAND (t
, 0);
4025 if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t
)))
4026 warn_logical_not_parentheses (location
, code
, arg1
.value
, arg2
.value
);
4029 /* Warn about comparisons against string literals, with the exception
4030 of testing for equality or inequality of a string literal with NULL. */
4031 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
4033 if ((code1
== STRING_CST
4034 && !integer_zerop (tree_strip_nop_conversions (arg2
.value
)))
4035 || (code2
== STRING_CST
4036 && !integer_zerop (tree_strip_nop_conversions (arg1
.value
))))
4037 warning_at (location
, OPT_Waddress
,
4038 "comparison with string literal results in unspecified behavior");
4039 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
4040 if (POINTER_TYPE_P (type1
)
4041 && null_pointer_constant_p (arg2
.value
)
4042 && char_type_p (type2
))
4044 auto_diagnostic_group d
;
4045 if (warning_at (location
, OPT_Wpointer_compare
,
4046 "comparison between pointer and zero character "
4048 inform (arg1
.get_start (),
4049 "did you mean to dereference the pointer?");
4051 else if (POINTER_TYPE_P (type2
)
4052 && null_pointer_constant_p (arg1
.value
)
4053 && char_type_p (type1
))
4055 auto_diagnostic_group d
;
4056 if (warning_at (location
, OPT_Wpointer_compare
,
4057 "comparison between pointer and zero character "
4059 inform (arg2
.get_start (),
4060 "did you mean to dereference the pointer?");
4063 else if (TREE_CODE_CLASS (code
) == tcc_comparison
4064 && (code1
== STRING_CST
|| code2
== STRING_CST
))
4065 warning_at (location
, OPT_Waddress
,
4066 "comparison with string literal results in unspecified "
4069 if (warn_array_compare
4070 && TREE_CODE_CLASS (code
) == tcc_comparison
4071 && TREE_CODE (type1
) == ARRAY_TYPE
4072 && TREE_CODE (type2
) == ARRAY_TYPE
)
4073 do_warn_array_compare (location
, code
, arg1
.value
, arg2
.value
);
4075 if (TREE_OVERFLOW_P (result
.value
)
4076 && !TREE_OVERFLOW_P (arg1
.value
)
4077 && !TREE_OVERFLOW_P (arg2
.value
))
4078 overflow_warning (location
, result
.value
);
4080 /* Warn about comparisons of different enum types. */
4081 if (warn_enum_compare
4082 && TREE_CODE_CLASS (code
) == tcc_comparison
4083 && TREE_CODE (type1
) == ENUMERAL_TYPE
4084 && TREE_CODE (type2
) == ENUMERAL_TYPE
4085 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
4086 warning_at (location
, OPT_Wenum_compare
,
4087 "comparison between %qT and %qT",
4090 if (warn_xor_used_as_pow
4091 && code
== BIT_XOR_EXPR
4094 check_for_xor_used_as_pow (arg1
.get_location (), arg1
.value
,
4096 arg2
.get_location (), arg2
.value
);
4101 /* Return a tree for the difference of pointers OP0 and OP1.
4102 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
4103 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
4106 pointer_diff (location_t loc
, tree op0
, tree op1
, tree
*instrument_expr
)
4108 tree restype
= ptrdiff_type_node
;
4109 tree result
, inttype
;
4111 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
4112 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
4113 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
4114 tree orig_op0
= op0
;
4115 tree orig_op1
= op1
;
4117 /* If the operands point into different address spaces, we need to
4118 explicitly convert them to pointers into the common address space
4119 before we can subtract the numerical address values. */
4122 addr_space_t as_common
;
4125 /* Determine the common superset address space. This is guaranteed
4126 to exist because the caller verified that comp_target_types
4127 returned non-zero. */
4128 if (!addr_space_superset (as0
, as1
, &as_common
))
4131 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
4132 op0
= convert (common_type
, op0
);
4133 op1
= convert (common_type
, op1
);
4136 /* Determine integer type result of the subtraction. This will usually
4137 be the same as the result type (ptrdiff_t), but may need to be a wider
4138 type if pointers for the address space are wider than ptrdiff_t. */
4139 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
4140 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
4144 if (VOID_TYPE_P (target_type
))
4145 pedwarn (loc
, OPT_Wpointer_arith
,
4146 "pointer of type %<void *%> used in subtraction");
4147 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4148 pedwarn (loc
, OPT_Wpointer_arith
,
4149 "pointer to a function used in subtraction");
4151 if (current_function_decl
!= NULL_TREE
4152 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
4154 op0
= save_expr (op0
);
4155 op1
= save_expr (op1
);
4157 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
4158 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
4161 /* First do the subtraction, then build the divide operator
4162 and only convert at the very end.
4163 Do not do default conversions in case restype is a short type. */
4165 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4166 pointers. If some platform cannot provide that, or has a larger
4167 ptrdiff_type to support differences larger than half the address
4168 space, cast the pointers to some larger integer type and do the
4169 computations in that type. */
4170 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
4171 op0
= build_binary_op (loc
, MINUS_EXPR
, convert (inttype
, op0
),
4172 convert (inttype
, op1
), false);
4175 /* Cast away qualifiers. */
4176 op0
= convert (c_common_type (TREE_TYPE (op0
), TREE_TYPE (op0
)), op0
);
4177 op1
= convert (c_common_type (TREE_TYPE (op1
), TREE_TYPE (op1
)), op1
);
4178 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
4181 /* This generates an error if op1 is pointer to incomplete type. */
4182 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
4183 error_at (loc
, "arithmetic on pointer to an incomplete type");
4184 else if (verify_type_context (loc
, TCTX_POINTER_ARITH
,
4185 TREE_TYPE (TREE_TYPE (orig_op0
))))
4186 verify_type_context (loc
, TCTX_POINTER_ARITH
,
4187 TREE_TYPE (TREE_TYPE (orig_op1
)));
4189 op1
= c_size_in_bytes (target_type
);
4191 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
4192 error_at (loc
, "arithmetic on pointer to an empty aggregate");
4194 /* Divide by the size, in easiest possible way. */
4195 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
4196 op0
, convert (inttype
, op1
));
4198 /* Convert to final result type if necessary. */
4199 return convert (restype
, result
);
4202 /* Expand atomic compound assignments into an appropriate sequence as
4203 specified by the C11 standard section 6.5.16.2.
4209 This sequence is used for all types for which these operations are
4212 In addition, built-in versions of the 'fe' prefixed routines may
4213 need to be invoked for floating point (real, complex or vector) when
4214 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4224 __atomic_load (addr, &old, SEQ_CST);
4225 feholdexcept (&fenv);
4227 newval = old op val;
4228 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4231 feclearexcept (FE_ALL_EXCEPT);
4234 feupdateenv (&fenv);
4236 The compiler will issue the __atomic_fetch_* built-in when possible,
4237 otherwise it will generate the generic form of the atomic operations.
4238 This requires temp(s) and has their address taken. The atomic processing
4239 is smart enough to figure out when the size of an object can utilize
4240 a lock-free version, and convert the built-in call to the appropriate
4241 lock-free routine. The optimizers will then dispose of any temps that
4242 are no longer required, and lock-free implementations are utilized as
4243 long as there is target support for the required size.
4245 If the operator is NOP_EXPR, then this is a simple assignment, and
4246 an __atomic_store is issued to perform the assignment rather than
4249 /* Build an atomic assignment at LOC, expanding into the proper
4250 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4251 the result of the operation, unless RETURN_OLD_P, in which case
4252 return the old value of LHS (this is only for postincrement and
4256 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
4257 tree rhs
, bool return_old_p
)
4259 tree fndecl
, func_call
;
4260 vec
<tree
, va_gc
> *params
;
4261 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
4263 tree compound_stmt
= NULL_TREE
;
4264 tree stmt
, goto_stmt
;
4265 tree loop_label
, loop_decl
, done_label
, done_decl
;
4267 tree lhs_type
= TREE_TYPE (lhs
);
4268 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, false);
4269 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
4270 tree rhs_semantic_type
= TREE_TYPE (rhs
);
4271 tree nonatomic_rhs_semantic_type
;
4274 gcc_assert (TYPE_ATOMIC (lhs_type
));
4277 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
4279 /* Allocate enough vector items for a compare_exchange. */
4280 vec_alloc (params
, 6);
4282 /* Create a compound statement to hold the sequence of statements
4284 if (modifycode
!= NOP_EXPR
)
4286 compound_stmt
= c_begin_compound_stmt (false);
4288 /* For consistency with build_modify_expr on non-_Atomic,
4289 mark the lhs as read. Also, it would be very hard to match
4290 such expressions in mark_exp_read. */
4291 mark_exp_read (lhs
);
4294 /* Remove any excess precision (which is only present here in the
4295 case of compound assignments). */
4296 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4298 gcc_assert (modifycode
!= NOP_EXPR
);
4299 rhs
= TREE_OPERAND (rhs
, 0);
4301 rhs_type
= TREE_TYPE (rhs
);
4303 /* Fold the RHS if it hasn't already been folded. */
4304 if (modifycode
!= NOP_EXPR
)
4305 rhs
= c_fully_fold (rhs
, false, NULL
);
4307 /* Remove the qualifiers for the rest of the expressions and create
4308 the VAL temp variable to hold the RHS. */
4309 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
4310 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
4311 nonatomic_rhs_semantic_type
= build_qualified_type (rhs_semantic_type
,
4313 val
= create_tmp_var_raw (nonatomic_rhs_type
);
4314 TREE_ADDRESSABLE (val
) = 1;
4315 suppress_warning (val
);
4316 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
4318 TREE_SIDE_EFFECTS (rhs
) = 1;
4319 SET_EXPR_LOCATION (rhs
, loc
);
4320 if (modifycode
!= NOP_EXPR
)
4323 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4325 if (modifycode
== NOP_EXPR
)
4327 compound_stmt
= rhs
;
4328 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4329 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, false);
4330 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
4331 params
->quick_push (lhs_addr
);
4332 params
->quick_push (rhs
);
4333 params
->quick_push (seq_cst
);
4334 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4336 compound_stmt
= build2 (COMPOUND_EXPR
, void_type_node
,
4337 compound_stmt
, func_call
);
4339 /* VAL is the value which was stored, return a COMPOUND_STMT of
4340 the statement and that value. */
4341 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
4344 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4345 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4346 isn't applicable for such builtins. ??? Do we want to handle enums? */
4347 if ((TREE_CODE (lhs_type
) == INTEGER_TYPE
|| POINTER_TYPE_P (lhs_type
))
4348 && TREE_CODE (rhs_type
) == INTEGER_TYPE
)
4350 built_in_function fncode
;
4354 case POINTER_PLUS_EXPR
:
4355 fncode
= (return_old_p
4356 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4357 : BUILT_IN_ATOMIC_ADD_FETCH_N
);
4360 fncode
= (return_old_p
4361 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4362 : BUILT_IN_ATOMIC_SUB_FETCH_N
);
4365 fncode
= (return_old_p
4366 ? BUILT_IN_ATOMIC_FETCH_AND_N
4367 : BUILT_IN_ATOMIC_AND_FETCH_N
);
4370 fncode
= (return_old_p
4371 ? BUILT_IN_ATOMIC_FETCH_OR_N
4372 : BUILT_IN_ATOMIC_OR_FETCH_N
);
4375 fncode
= (return_old_p
4376 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4377 : BUILT_IN_ATOMIC_XOR_FETCH_N
);
4383 /* We can only use "_1" through "_16" variants of the atomic fetch
4385 unsigned HOST_WIDE_INT size
= tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type
));
4386 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8 && size
!= 16)
4389 /* If this is a pointer type, we need to multiply by the size of
4390 the pointer target type. */
4391 if (POINTER_TYPE_P (lhs_type
))
4393 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type
))
4394 /* ??? This would introduce -Wdiscarded-qualifiers
4395 warning: __atomic_fetch_* expect volatile void *
4396 type as the first argument. (Assignments between
4397 atomic and non-atomic objects are OK.) */
4398 || TYPE_RESTRICT (lhs_type
))
4400 tree sz
= TYPE_SIZE_UNIT (TREE_TYPE (lhs_type
));
4401 rhs
= fold_build2_loc (loc
, MULT_EXPR
, ptrdiff_type_node
,
4402 convert (ptrdiff_type_node
, rhs
),
4403 convert (ptrdiff_type_node
, sz
));
4406 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4407 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4408 fndecl
= builtin_decl_explicit (fncode
);
4409 params
->quick_push (lhs_addr
);
4410 params
->quick_push (rhs
);
4411 params
->quick_push (seq_cst
);
4412 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4414 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4415 TREE_ADDRESSABLE (newval
) = 1;
4416 suppress_warning (newval
);
4417 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, func_call
,
4418 NULL_TREE
, NULL_TREE
);
4419 SET_EXPR_LOCATION (rhs
, loc
);
4422 /* Finish the compound statement. */
4423 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4425 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4426 the statement and that value. */
4427 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, newval
);
4431 /* Create the variables and labels required for the op= form. */
4432 old
= create_tmp_var_raw (nonatomic_lhs_type
);
4433 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, false);
4434 TREE_ADDRESSABLE (old
) = 1;
4435 suppress_warning (old
);
4437 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4438 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, false);
4439 TREE_ADDRESSABLE (newval
) = 1;
4440 suppress_warning (newval
);
4442 loop_decl
= create_artificial_label (loc
);
4443 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
4445 done_decl
= create_artificial_label (loc
);
4446 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
4448 /* __atomic_load (addr, &old, SEQ_CST). */
4449 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
4450 params
->quick_push (lhs_addr
);
4451 params
->quick_push (old_addr
);
4452 params
->quick_push (seq_cst
);
4453 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4454 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
4457 params
->truncate (0);
4459 /* Create the expressions for floating-point environment
4460 manipulation, if required. */
4461 bool need_fenv
= (flag_trapping_math
4462 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
4463 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
4465 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
4468 add_stmt (hold_call
);
4471 add_stmt (loop_label
);
4473 /* newval = old + val; */
4474 if (rhs_type
!= rhs_semantic_type
)
4475 val
= build1 (EXCESS_PRECISION_EXPR
, nonatomic_rhs_semantic_type
, val
);
4476 rhs
= build_binary_op (loc
, modifycode
, old
, val
, true);
4477 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4479 tree eptype
= TREE_TYPE (rhs
);
4480 rhs
= c_fully_fold (TREE_OPERAND (rhs
, 0), false, NULL
);
4481 rhs
= build1 (EXCESS_PRECISION_EXPR
, eptype
, rhs
);
4484 rhs
= c_fully_fold (rhs
, false, NULL
);
4485 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
4486 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
4488 if (rhs
!= error_mark_node
)
4490 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
4492 SET_EXPR_LOCATION (rhs
, loc
);
4496 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4498 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
4499 params
->quick_push (lhs_addr
);
4500 params
->quick_push (old_addr
);
4501 params
->quick_push (newval_addr
);
4502 params
->quick_push (integer_zero_node
);
4503 params
->quick_push (seq_cst
);
4504 params
->quick_push (seq_cst
);
4505 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4507 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
4508 SET_EXPR_LOCATION (goto_stmt
, loc
);
4510 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
4511 SET_EXPR_LOCATION (stmt
, loc
);
4515 add_stmt (clear_call
);
4518 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
4519 SET_EXPR_LOCATION (goto_stmt
, loc
);
4520 add_stmt (goto_stmt
);
4523 add_stmt (done_label
);
4526 add_stmt (update_call
);
4528 /* Finish the compound statement. */
4529 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4531 /* NEWVAL is the value that was successfully stored, return a
4532 COMPOUND_EXPR of the statement and the appropriate value. */
4533 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
4534 return_old_p
? old
: newval
);
4537 /* Construct and perhaps optimize a tree representation
4538 for a unary operation. CODE, a tree_code, specifies the operation
4539 and XARG is the operand.
4540 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4541 promotions (such as from short to int).
4542 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4543 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4546 LOCATION is the location of the operator. */
4549 build_unary_op (location_t location
, enum tree_code code
, tree xarg
,
4552 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4554 tree argtype
= NULL_TREE
;
4555 enum tree_code typecode
;
4557 tree ret
= error_mark_node
;
4558 tree eptype
= NULL_TREE
;
4559 const char *invalid_op_diag
;
4562 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
4564 arg
= remove_c_maybe_const_expr (arg
);
4566 if (code
!= ADDR_EXPR
)
4567 arg
= require_complete_type (location
, arg
);
4569 typecode
= TREE_CODE (TREE_TYPE (arg
));
4570 if (typecode
== ERROR_MARK
)
4571 return error_mark_node
;
4572 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
4573 typecode
= INTEGER_TYPE
;
4575 if ((invalid_op_diag
4576 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
4578 error_at (location
, invalid_op_diag
);
4579 return error_mark_node
;
4582 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
4584 eptype
= TREE_TYPE (arg
);
4585 arg
= TREE_OPERAND (arg
, 0);
4591 /* This is used for unary plus, because a CONVERT_EXPR
4592 is enough to prevent anybody from looking inside for
4593 associativity, but won't generate any code. */
4594 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4595 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4596 || typecode
== BITINT_TYPE
4597 || gnu_vector_type_p (TREE_TYPE (arg
))))
4599 error_at (location
, "wrong type argument to unary plus");
4600 return error_mark_node
;
4602 else if (!noconvert
)
4603 arg
= default_conversion (arg
);
4604 arg
= non_lvalue_loc (location
, arg
);
4608 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4609 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4610 || typecode
== BITINT_TYPE
4611 || gnu_vector_type_p (TREE_TYPE (arg
))))
4613 error_at (location
, "wrong type argument to unary minus");
4614 return error_mark_node
;
4616 else if (!noconvert
)
4617 arg
= default_conversion (arg
);
4621 /* ~ works on integer types and non float vectors. */
4622 if (typecode
== INTEGER_TYPE
4623 || typecode
== BITINT_TYPE
4624 || (gnu_vector_type_p (TREE_TYPE (arg
))
4625 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
4629 /* Warn if the expression has boolean value. */
4630 while (TREE_CODE (e
) == COMPOUND_EXPR
)
4631 e
= TREE_OPERAND (e
, 1);
4633 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg
))
4634 || truth_value_p (TREE_CODE (e
))))
4636 auto_diagnostic_group d
;
4637 if (warning_at (location
, OPT_Wbool_operation
,
4638 "%<~%> on a boolean expression"))
4640 gcc_rich_location
richloc (location
);
4641 richloc
.add_fixit_insert_before (location
, "!");
4642 inform (&richloc
, "did you mean to use logical not?");
4646 arg
= default_conversion (arg
);
4648 else if (typecode
== COMPLEX_TYPE
)
4651 pedwarn (location
, OPT_Wpedantic
,
4652 "ISO C does not support %<~%> for complex conjugation");
4654 arg
= default_conversion (arg
);
4658 error_at (location
, "wrong type argument to bit-complement");
4659 return error_mark_node
;
4664 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
4666 error_at (location
, "wrong type argument to abs");
4667 return error_mark_node
;
4669 else if (!noconvert
)
4670 arg
= default_conversion (arg
);
4674 if (!(typecode
== INTEGER_TYPE
))
4676 error_at (location
, "wrong type argument to absu");
4677 return error_mark_node
;
4679 else if (!noconvert
)
4680 arg
= default_conversion (arg
);
4684 /* Conjugating a real value is a no-op, but allow it anyway. */
4685 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4686 || typecode
== COMPLEX_TYPE
))
4688 error_at (location
, "wrong type argument to conjugation");
4689 return error_mark_node
;
4691 else if (!noconvert
)
4692 arg
= default_conversion (arg
);
4695 case TRUTH_NOT_EXPR
:
4696 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4697 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
4698 && typecode
!= COMPLEX_TYPE
&& typecode
!= NULLPTR_TYPE
4699 && typecode
!= BITINT_TYPE
)
4702 "wrong type argument to unary exclamation mark");
4703 return error_mark_node
;
4707 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
4708 arg
= remove_c_maybe_const_expr (arg
);
4711 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
4712 ret
= invert_truthvalue_loc (location
, arg
);
4713 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4714 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
4715 location
= EXPR_LOCATION (ret
);
4716 goto return_build_unary_op
;
4720 ret
= build_real_imag_expr (location
, code
, arg
);
4721 if (ret
== error_mark_node
)
4722 return error_mark_node
;
4723 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
4724 eptype
= TREE_TYPE (eptype
);
4725 goto return_build_unary_op
;
4727 case PREINCREMENT_EXPR
:
4728 case POSTINCREMENT_EXPR
:
4729 case PREDECREMENT_EXPR
:
4730 case POSTDECREMENT_EXPR
:
4732 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4734 tree inner
= build_unary_op (location
, code
,
4735 C_MAYBE_CONST_EXPR_EXPR (arg
),
4737 if (inner
== error_mark_node
)
4738 return error_mark_node
;
4739 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4740 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4741 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4742 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
4743 goto return_build_unary_op
;
4746 /* Complain about anything that is not a true lvalue. In
4747 Objective-C, skip this check for property_refs. */
4748 if (!objc_is_property_ref (arg
)
4749 && !lvalue_or_else (location
,
4750 arg
, ((code
== PREINCREMENT_EXPR
4751 || code
== POSTINCREMENT_EXPR
)
4754 return error_mark_node
;
4756 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
4758 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4759 warning_at (location
, OPT_Wc___compat
,
4760 "increment of enumeration value is invalid in C++");
4762 warning_at (location
, OPT_Wc___compat
,
4763 "decrement of enumeration value is invalid in C++");
4766 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg
)))
4768 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4769 warning_at (location
, OPT_Wbool_operation
,
4770 "increment of a boolean expression");
4772 warning_at (location
, OPT_Wbool_operation
,
4773 "decrement of a boolean expression");
4776 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4777 arg
= c_fully_fold (arg
, false, NULL
, true);
4780 atomic_op
= really_atomic_lvalue (arg
);
4782 /* Increment or decrement the real part of the value,
4783 and don't change the imaginary part. */
4784 if (typecode
== COMPLEX_TYPE
)
4788 pedwarn (location
, OPT_Wpedantic
,
4789 "ISO C does not support %<++%> and %<--%> on complex types");
4793 arg
= stabilize_reference (arg
);
4794 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
,
4796 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
,
4798 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, true);
4799 if (real
== error_mark_node
|| imag
== error_mark_node
)
4800 return error_mark_node
;
4801 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4803 goto return_build_unary_op
;
4807 /* Report invalid types. */
4809 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4810 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4811 && typecode
!= COMPLEX_TYPE
&& typecode
!= BITINT_TYPE
4812 && !gnu_vector_type_p (TREE_TYPE (arg
)))
4814 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4815 error_at (location
, "wrong type argument to increment");
4817 error_at (location
, "wrong type argument to decrement");
4819 return error_mark_node
;
4825 argtype
= TREE_TYPE (arg
);
4827 /* Compute the increment. */
4829 if (typecode
== POINTER_TYPE
)
4831 /* If pointer target is an incomplete type,
4832 we just cannot know how to do the arithmetic. */
4833 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4835 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4837 "increment of pointer to an incomplete type %qT",
4838 TREE_TYPE (argtype
));
4841 "decrement of pointer to an incomplete type %qT",
4842 TREE_TYPE (argtype
));
4844 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4845 || VOID_TYPE_P (TREE_TYPE (argtype
)))
4847 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4848 pedwarn (location
, OPT_Wpointer_arith
,
4849 "wrong type argument to increment");
4851 pedwarn (location
, OPT_Wpointer_arith
,
4852 "wrong type argument to decrement");
4855 verify_type_context (location
, TCTX_POINTER_ARITH
,
4856 TREE_TYPE (argtype
));
4858 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4859 inc
= convert_to_ptrofftype_loc (location
, inc
);
4861 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4863 /* For signed fract types, we invert ++ to -- or
4864 -- to ++, and change inc from 1 to -1, because
4865 it is not possible to represent 1 in signed fract constants.
4866 For unsigned fract types, the result always overflows and
4867 we get an undefined (original) or the maximum value. */
4868 if (code
== PREINCREMENT_EXPR
)
4869 code
= PREDECREMENT_EXPR
;
4870 else if (code
== PREDECREMENT_EXPR
)
4871 code
= PREINCREMENT_EXPR
;
4872 else if (code
== POSTINCREMENT_EXPR
)
4873 code
= POSTDECREMENT_EXPR
;
4874 else /* code == POSTDECREMENT_EXPR */
4875 code
= POSTINCREMENT_EXPR
;
4877 inc
= integer_minus_one_node
;
4878 inc
= convert (argtype
, inc
);
4882 inc
= VECTOR_TYPE_P (argtype
)
4883 ? build_one_cst (argtype
)
4885 inc
= convert (argtype
, inc
);
4888 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4889 need to ask Objective-C to build the increment or decrement
4890 expression for it. */
4891 if (objc_is_property_ref (arg
))
4892 return objc_build_incr_expr_for_property_ref (location
, code
,
4895 /* Report a read-only lvalue. */
4896 if (TYPE_READONLY (argtype
))
4898 readonly_error (location
, arg
,
4899 ((code
== PREINCREMENT_EXPR
4900 || code
== POSTINCREMENT_EXPR
)
4901 ? lv_increment
: lv_decrement
));
4902 return error_mark_node
;
4904 else if (TREE_READONLY (arg
))
4905 readonly_warning (arg
,
4906 ((code
== PREINCREMENT_EXPR
4907 || code
== POSTINCREMENT_EXPR
)
4908 ? lv_increment
: lv_decrement
));
4910 /* If the argument is atomic, use the special code sequences for
4911 atomic compound assignment. */
4914 arg
= stabilize_reference (arg
);
4915 ret
= build_atomic_assign (location
, arg
,
4916 ((code
== PREINCREMENT_EXPR
4917 || code
== POSTINCREMENT_EXPR
)
4920 (FRACT_MODE_P (TYPE_MODE (argtype
))
4922 : integer_one_node
),
4923 (code
== POSTINCREMENT_EXPR
4924 || code
== POSTDECREMENT_EXPR
));
4925 goto return_build_unary_op
;
4928 if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg
)))
4929 val
= boolean_increment (code
, arg
);
4931 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4932 TREE_SIDE_EFFECTS (val
) = 1;
4933 if (TYPE_QUALS (TREE_TYPE (val
)) != TYPE_UNQUALIFIED
)
4934 TREE_TYPE (val
) = c_build_qualified_type (TREE_TYPE (val
),
4937 goto return_build_unary_op
;
4941 /* Note that this operation never does default_conversion. */
4943 /* The operand of unary '&' must be an lvalue (which excludes
4944 expressions of type void), or, in C99, the result of a [] or
4945 unary '*' operator. */
4946 if (VOID_TYPE_P (TREE_TYPE (arg
))
4947 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4948 && (!INDIRECT_REF_P (arg
) || !flag_isoc99
))
4949 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4951 /* Let &* cancel out to simplify resulting code. */
4952 if (INDIRECT_REF_P (arg
))
4954 /* Don't let this be an lvalue. */
4955 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4956 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4957 ret
= TREE_OPERAND (arg
, 0);
4958 goto return_build_unary_op
;
4961 /* Anything not already handled and not a true memory reference
4962 or a non-lvalue array is an error. */
4963 if (typecode
!= FUNCTION_TYPE
&& !noconvert
4964 && !lvalue_or_else (location
, arg
, lv_addressof
))
4965 return error_mark_node
;
4967 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4969 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4971 tree inner
= build_unary_op (location
, code
,
4972 C_MAYBE_CONST_EXPR_EXPR (arg
),
4974 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4975 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4976 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4977 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4978 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4979 goto return_build_unary_op
;
4982 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4983 argtype
= TREE_TYPE (arg
);
4985 /* If the lvalue is const or volatile, merge that into the type
4986 to which the address will point. This is only needed
4987 for function types. */
4988 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4989 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4990 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4992 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4993 int quals
= orig_quals
;
4995 if (TREE_READONLY (arg
))
4996 quals
|= TYPE_QUAL_CONST
;
4997 if (TREE_THIS_VOLATILE (arg
))
4998 quals
|= TYPE_QUAL_VOLATILE
;
5000 argtype
= c_build_qualified_type (argtype
, quals
);
5003 switch (TREE_CODE (arg
))
5006 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
5008 error_at (location
, "cannot take address of bit-field %qD",
5009 TREE_OPERAND (arg
, 1));
5010 return error_mark_node
;
5016 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg
, 0))))
5018 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg
))
5019 && !POINTER_TYPE_P (TREE_TYPE (arg
))
5020 && !VECTOR_TYPE_P (TREE_TYPE (arg
)))
5022 error_at (location
, "cannot take address of scalar with "
5023 "reverse storage order");
5024 return error_mark_node
;
5027 if (TREE_CODE (TREE_TYPE (arg
)) == ARRAY_TYPE
5028 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg
)))
5029 warning_at (location
, OPT_Wscalar_storage_order
,
5030 "address of array with reverse scalar storage "
5038 if (!c_mark_addressable (arg
))
5039 return error_mark_node
;
5041 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
5042 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
5044 argtype
= build_pointer_type (argtype
);
5046 /* ??? Cope with user tricks that amount to offsetof. Delete this
5047 when we have proper support for integer constant expressions. */
5048 val
= get_base_address (arg
);
5049 if (val
&& INDIRECT_REF_P (val
)
5050 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
5052 ret
= fold_offsetof (arg
, argtype
);
5053 goto return_build_unary_op
;
5056 val
= build1 (ADDR_EXPR
, argtype
, arg
);
5059 goto return_build_unary_op
;
5062 ret
= build1 (code
, TREE_TYPE (arg
), arg
);
5063 goto return_build_unary_op
;
5069 if (argtype
== NULL_TREE
)
5070 argtype
= TREE_TYPE (arg
);
5071 if (TREE_CODE (arg
) == INTEGER_CST
)
5072 ret
= (require_constant_value
5073 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
5074 : fold_build1_loc (location
, code
, argtype
, arg
));
5076 ret
= build1 (code
, argtype
, arg
);
5077 return_build_unary_op
:
5078 gcc_assert (ret
!= error_mark_node
);
5079 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
5080 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
5081 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
5082 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
5083 ret
= note_integer_operands (ret
);
5085 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
5086 protected_set_expr_location (ret
, location
);
5090 /* Return nonzero if REF is an lvalue valid for this language.
5091 Lvalues can be assigned, unless their type has TYPE_READONLY.
5092 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
5095 lvalue_p (const_tree ref
)
5097 const enum tree_code code
= TREE_CODE (ref
);
5104 return lvalue_p (TREE_OPERAND (ref
, 0));
5106 case C_MAYBE_CONST_EXPR
:
5107 return lvalue_p (TREE_OPERAND (ref
, 1));
5109 case COMPOUND_LITERAL_EXPR
:
5114 case TARGET_MEM_REF
:
5115 /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5123 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
5124 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
5127 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
5134 /* Give a warning for storing in something that is read-only in GCC
5135 terms but not const in ISO C terms. */
5138 readonly_warning (tree arg
, enum lvalue_use use
)
5143 warning (0, "assignment of read-only location %qE", arg
);
5146 warning (0, "increment of read-only location %qE", arg
);
5149 warning (0, "decrement of read-only location %qE", arg
);
5158 /* Return nonzero if REF is an lvalue valid for this language;
5159 otherwise, print an error message and return zero. USE says
5160 how the lvalue is being used and so selects the error message.
5161 LOCATION is the location at which any error should be reported. */
5164 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
5166 int win
= lvalue_p (ref
);
5169 lvalue_error (loc
, use
);
5174 /* Mark EXP saying that we need to be able to take the
5175 address of it; it should not be allocated in a register.
5176 Returns true if successful. ARRAY_REF_P is true if this
5177 is for ARRAY_REF construction - in that case we don't want
5178 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5179 it is fine to use ARRAY_REFs for vector subscripts on vector
5180 register variables. */
5183 c_mark_addressable (tree exp
, bool array_ref_p
)
5188 switch (TREE_CODE (x
))
5190 case VIEW_CONVERT_EXPR
:
5192 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
5193 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
5195 x
= TREE_OPERAND (x
, 0);
5199 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
5201 error ("cannot take address of bit-field %qD",
5202 TREE_OPERAND (x
, 1));
5210 x
= TREE_OPERAND (x
, 0);
5213 case COMPOUND_LITERAL_EXPR
:
5214 if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x
)))
5216 error ("address of register compound literal requested");
5219 TREE_ADDRESSABLE (x
) = 1;
5220 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x
)) = 1;
5224 TREE_ADDRESSABLE (x
) = 1;
5231 if (C_DECL_REGISTER (x
)
5232 && DECL_NONLOCAL (x
))
5234 if (TREE_PUBLIC (x
) || is_global_var (x
))
5237 ("global register variable %qD used in nested function", x
);
5240 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
5242 else if (C_DECL_REGISTER (x
))
5244 if (TREE_PUBLIC (x
) || is_global_var (x
))
5245 error ("address of global register variable %qD requested", x
);
5247 error ("address of register variable %qD requested", x
);
5253 TREE_ADDRESSABLE (x
) = 1;
5260 /* Convert EXPR to TYPE, warning about conversion problems with
5261 constants. SEMANTIC_TYPE is the type this conversion would use
5262 without excess precision. If SEMANTIC_TYPE is NULL, this function
5263 is equivalent to convert_and_check. This function is a wrapper that
5264 handles conversions that may be different than
5265 the usual ones because of excess precision. */
5268 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
5271 if (TREE_TYPE (expr
) == type
)
5274 /* For C11, integer conversions may have results with excess
5276 if (flag_isoc11
|| !semantic_type
)
5277 return convert_and_check (loc
, type
, expr
);
5279 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
5280 && TREE_TYPE (expr
) != semantic_type
)
5282 /* For integers, we need to check the real conversion, not
5283 the conversion to the excess precision type. */
5284 expr
= convert_and_check (loc
, semantic_type
, expr
);
5286 /* Result type is the excess precision type, which should be
5287 large enough, so do not check. */
5288 return convert (type
, expr
);
5291 /* If EXPR refers to a built-in declared without a prototype returns
5292 the actual type of the built-in and, if non-null, set *BLTIN to
5293 a pointer to the built-in. Otherwise return the type of EXPR
5294 and clear *BLTIN if non-null. */
5297 type_or_builtin_type (tree expr
, tree
*bltin
= NULL
)
5305 tree type
= TREE_TYPE (expr
);
5306 if (TREE_CODE (expr
) != ADDR_EXPR
)
5309 tree oper
= TREE_OPERAND (expr
, 0);
5311 || TREE_CODE (oper
) != FUNCTION_DECL
5312 || !fndecl_built_in_p (oper
, BUILT_IN_NORMAL
))
5315 built_in_function code
= DECL_FUNCTION_CODE (oper
);
5316 if (!C_DECL_BUILTIN_PROTOTYPE (oper
))
5319 if ((*bltin
= builtin_decl_implicit (code
)))
5320 type
= build_pointer_type (TREE_TYPE (*bltin
));
5325 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5326 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5327 if folded to an integer constant then the unselected half may
5328 contain arbitrary operations not normally permitted in constant
5329 expressions. Set the location of the expression to LOC. */
5332 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
5333 tree op1
, tree op1_original_type
, location_t op1_loc
,
5334 tree op2
, tree op2_original_type
, location_t op2_loc
)
5338 enum tree_code code1
;
5339 enum tree_code code2
;
5340 tree result_type
= NULL
;
5341 tree semantic_result_type
= NULL
;
5342 tree orig_op1
= op1
, orig_op2
= op2
;
5343 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
5344 bool ifexp_int_operands
;
5347 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
5348 if (op1_int_operands
)
5349 op1
= remove_c_maybe_const_expr (op1
);
5350 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
5351 if (op2_int_operands
)
5352 op2
= remove_c_maybe_const_expr (op2
);
5353 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
5354 if (ifexp_int_operands
)
5355 ifexp
= remove_c_maybe_const_expr (ifexp
);
5357 /* Promote both alternatives. */
5359 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
5360 op1
= default_conversion (op1
);
5361 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
5362 op2
= default_conversion (op2
);
5364 if (TREE_CODE (ifexp
) == ERROR_MARK
5365 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
5366 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
5367 return error_mark_node
;
5369 tree bltin1
= NULL_TREE
;
5370 tree bltin2
= NULL_TREE
;
5371 type1
= type_or_builtin_type (op1
, &bltin1
);
5372 code1
= TREE_CODE (type1
);
5373 type2
= type_or_builtin_type (op2
, &bltin2
);
5374 code2
= TREE_CODE (type2
);
5376 if (code1
== POINTER_TYPE
&& reject_gcc_builtin (op1
))
5377 return error_mark_node
;
5379 if (code2
== POINTER_TYPE
&& reject_gcc_builtin (op2
))
5380 return error_mark_node
;
5382 /* C90 does not permit non-lvalue arrays in conditional expressions.
5383 In C99 they will be pointers by now. */
5384 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
5386 error_at (colon_loc
, "non-lvalue array in conditional expression");
5387 return error_mark_node
;
5390 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
5391 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
5392 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5393 || code1
== COMPLEX_TYPE
|| code1
== BITINT_TYPE
)
5394 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
5395 || code2
== COMPLEX_TYPE
|| code2
== BITINT_TYPE
))
5397 semantic_result_type
= c_common_type (type1
, type2
);
5398 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
5400 op1
= TREE_OPERAND (op1
, 0);
5401 type1
= TREE_TYPE (op1
);
5402 gcc_assert (TREE_CODE (type1
) == code1
);
5404 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
5406 op2
= TREE_OPERAND (op2
, 0);
5407 type2
= TREE_TYPE (op2
);
5408 gcc_assert (TREE_CODE (type2
) == code2
);
5412 if (warn_cxx_compat
)
5414 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
5415 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
5417 if (TREE_CODE (t1
) == ENUMERAL_TYPE
5418 && TREE_CODE (t2
) == ENUMERAL_TYPE
5419 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
5420 warning_at (colon_loc
, OPT_Wc___compat
,
5421 ("different enum types in conditional is "
5422 "invalid in C++: %qT vs %qT"),
5426 /* Quickly detect the usual case where op1 and op2 have the same type
5428 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
5431 result_type
= type1
;
5433 result_type
= TYPE_MAIN_VARIANT (type1
);
5435 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5436 || code1
== COMPLEX_TYPE
|| code1
== BITINT_TYPE
)
5437 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
5438 || code2
== COMPLEX_TYPE
|| code2
== BITINT_TYPE
))
5440 /* In C11, a conditional expression between a floating-point
5441 type and an integer type should convert the integer type to
5442 the evaluation format of the floating-point type, with
5443 possible excess precision. */
5444 tree eptype1
= type1
;
5445 tree eptype2
= type2
;
5449 if (ANY_INTEGRAL_TYPE_P (type1
)
5450 && (eptype
= excess_precision_type (type2
)) != NULL_TREE
)
5453 if (!semantic_result_type
)
5454 semantic_result_type
= c_common_type (type1
, type2
);
5456 else if (ANY_INTEGRAL_TYPE_P (type2
)
5457 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
5460 if (!semantic_result_type
)
5461 semantic_result_type
= c_common_type (type1
, type2
);
5464 result_type
= c_common_type (eptype1
, eptype2
);
5465 if (result_type
== error_mark_node
)
5466 return error_mark_node
;
5467 do_warn_double_promotion (result_type
, type1
, type2
,
5468 "implicit conversion from %qT to %qT to "
5469 "match other result of conditional",
5472 /* If -Wsign-compare, warn here if type1 and type2 have
5473 different signedness. We'll promote the signed to unsigned
5474 and later code won't know it used to be different.
5475 Do this check on the original types, so that explicit casts
5476 will be considered, but default promotions won't. */
5477 if (c_inhibit_evaluation_warnings
== 0)
5479 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
5480 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
5482 if (unsigned_op1
^ unsigned_op2
)
5486 /* Do not warn if the result type is signed, since the
5487 signed type will only be chosen if it can represent
5488 all the values of the unsigned type. */
5489 if (!TYPE_UNSIGNED (result_type
))
5493 bool op1_maybe_const
= true;
5494 bool op2_maybe_const
= true;
5496 /* Do not warn if the signed quantity is an
5497 unsuffixed integer literal (or some static
5498 constant expression involving such literals) and
5499 it is non-negative. This warning requires the
5500 operands to be folded for best results, so do
5501 that folding in this case even without
5502 warn_sign_compare to avoid warning options
5503 possibly affecting code generation. */
5504 c_inhibit_evaluation_warnings
5505 += (ifexp
== truthvalue_false_node
);
5506 op1
= c_fully_fold (op1
, require_constant_value
,
5508 c_inhibit_evaluation_warnings
5509 -= (ifexp
== truthvalue_false_node
);
5511 c_inhibit_evaluation_warnings
5512 += (ifexp
== truthvalue_true_node
);
5513 op2
= c_fully_fold (op2
, require_constant_value
,
5515 c_inhibit_evaluation_warnings
5516 -= (ifexp
== truthvalue_true_node
);
5518 if (warn_sign_compare
)
5521 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
5523 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
5525 else if (unsigned_op2
)
5526 warning_at (op1_loc
, OPT_Wsign_compare
,
5527 "operand of %<?:%> changes signedness from "
5528 "%qT to %qT due to unsignedness of other "
5529 "operand", TREE_TYPE (orig_op1
),
5530 TREE_TYPE (orig_op2
));
5532 warning_at (op2_loc
, OPT_Wsign_compare
,
5533 "operand of %<?:%> changes signedness from "
5534 "%qT to %qT due to unsignedness of other "
5535 "operand", TREE_TYPE (orig_op2
),
5536 TREE_TYPE (orig_op1
));
5538 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
5539 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
5540 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
5541 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
5546 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
5548 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
5549 pedwarn (colon_loc
, OPT_Wpedantic
,
5550 "ISO C forbids conditional expr with only one void side");
5551 result_type
= void_type_node
;
5553 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5555 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
5556 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
5557 addr_space_t as_common
;
5559 if (comp_target_types (colon_loc
, type1
, type2
))
5560 result_type
= common_pointer_type (type1
, type2
);
5561 else if (null_pointer_constant_p (orig_op1
))
5562 result_type
= type2
;
5563 else if (null_pointer_constant_p (orig_op2
))
5564 result_type
= type1
;
5565 else if (!addr_space_superset (as1
, as2
, &as_common
))
5567 error_at (colon_loc
, "pointers to disjoint address spaces "
5568 "used in conditional expression");
5569 return error_mark_node
;
5571 else if ((VOID_TYPE_P (TREE_TYPE (type1
))
5572 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
5573 || (VOID_TYPE_P (TREE_TYPE (type2
))
5574 && !TYPE_ATOMIC (TREE_TYPE (type2
))))
5576 tree t1
= TREE_TYPE (type1
);
5577 tree t2
= TREE_TYPE (type2
);
5578 if (!(VOID_TYPE_P (t1
)
5579 && !TYPE_ATOMIC (t1
)))
5581 /* roles are swapped */
5583 t2
= TREE_TYPE (type1
);
5585 tree t2_stripped
= strip_array_types (t2
);
5586 if ((TREE_CODE (t2
) == ARRAY_TYPE
)
5587 && (TYPE_QUALS (t2_stripped
) & ~TYPE_QUALS (t1
)))
5590 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5591 "pointer to array loses qualifier "
5592 "in conditional expression");
5593 else if (warn_c11_c23_compat
> 0)
5594 warning_at (colon_loc
, OPT_Wc11_c23_compat
,
5595 "pointer to array loses qualifier "
5596 "in conditional expression in ISO C before C23");
5598 if (TREE_CODE (t2
) == FUNCTION_TYPE
)
5599 pedwarn (colon_loc
, OPT_Wpedantic
,
5600 "ISO C forbids conditional expr between "
5601 "%<void *%> and function pointer");
5602 /* for array, use qualifiers of element type */
5605 result_type
= build_pointer_type (qualify_type (t1
, t2
));
5607 /* Objective-C pointer comparisons are a bit more lenient. */
5608 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
5609 result_type
= objc_common_type (type1
, type2
);
5612 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
5613 diagnostic_t kind
= DK_PERMERROR
;
5615 /* This downgrade to a warning ensures that -std=gnu89
5616 -pedantic-errors does not flag these mismatches between
5617 builtins as errors (as DK_PERMERROR would). ISO C99
5618 and later do not have implicit function declarations,
5619 so the mismatch cannot occur naturally there. */
5620 kind
= bltin1
&& bltin2
? DK_WARNING
: DK_PEDWARN
;
5621 if (emit_diagnostic (kind
, colon_loc
, OPT_Wincompatible_pointer_types
,
5622 "pointer type mismatch "
5623 "in conditional expression"))
5625 inform (op1_loc
, "first expression has type %qT", type1
);
5626 inform (op2_loc
, "second expression has type %qT", type2
);
5628 result_type
= build_pointer_type
5629 (build_qualified_type (void_type_node
, qual
));
5632 else if (code1
== POINTER_TYPE
5633 && (code2
== INTEGER_TYPE
|| code2
== BITINT_TYPE
))
5635 if (!null_pointer_constant_p (orig_op2
))
5636 permerror_opt (colon_loc
, OPT_Wint_conversion
,
5637 "pointer/integer type mismatch "
5638 "in conditional expression");
5641 op2
= null_pointer_node
;
5643 result_type
= type1
;
5645 else if (code2
== POINTER_TYPE
5646 && (code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
))
5648 if (!null_pointer_constant_p (orig_op1
))
5649 permerror_opt (colon_loc
, OPT_Wint_conversion
,
5650 "pointer/integer type mismatch "
5651 "in conditional expression");
5654 op1
= null_pointer_node
;
5656 result_type
= type2
;
5658 /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
5659 type nullptr_t and the other is a pointer, the result type is the pointer
5661 else if (code1
== NULLPTR_TYPE
&& code2
== POINTER_TYPE
)
5662 result_type
= type2
;
5663 else if (code1
== POINTER_TYPE
&& code2
== NULLPTR_TYPE
)
5664 result_type
= type1
;
5665 else if (RECORD_OR_UNION_TYPE_P (type1
) && RECORD_OR_UNION_TYPE_P (type2
)
5666 && comptypes (TYPE_MAIN_VARIANT (type1
),
5667 TYPE_MAIN_VARIANT (type2
)))
5668 result_type
= composite_type (TYPE_MAIN_VARIANT (type1
),
5669 TYPE_MAIN_VARIANT (type2
));
5673 if (flag_cond_mismatch
)
5674 result_type
= void_type_node
;
5677 error_at (colon_loc
, "type mismatch in conditional expression");
5678 return error_mark_node
;
5682 /* Merge const and volatile flags of the incoming types. */
5684 = build_type_variant (result_type
,
5685 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
5686 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
5688 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
5689 semantic_result_type
);
5690 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
5691 semantic_result_type
);
5693 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
5695 op2_int_operands
= true;
5696 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
5698 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
5700 op1_int_operands
= true;
5701 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
5703 int_const
= int_operands
= (ifexp_int_operands
5705 && op2_int_operands
);
5708 int_const
= ((ifexp
== truthvalue_true_node
5709 && TREE_CODE (orig_op1
) == INTEGER_CST
5710 && !TREE_OVERFLOW (orig_op1
))
5711 || (ifexp
== truthvalue_false_node
5712 && TREE_CODE (orig_op2
) == INTEGER_CST
5713 && !TREE_OVERFLOW (orig_op2
)));
5716 /* Need to convert condition operand into a vector mask. */
5717 if (VECTOR_TYPE_P (TREE_TYPE (ifexp
)))
5719 tree vectype
= TREE_TYPE (ifexp
);
5720 tree elem_type
= TREE_TYPE (vectype
);
5721 tree zero
= build_int_cst (elem_type
, 0);
5722 tree zero_vec
= build_vector_from_val (vectype
, zero
);
5723 tree cmp_type
= truth_type_for (vectype
);
5724 ifexp
= build2 (NE_EXPR
, cmp_type
, ifexp
, zero_vec
);
5727 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
5728 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5733 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5734 nested inside of the expression. */
5735 op1
= c_fully_fold (op1
, false, NULL
);
5736 op2
= c_fully_fold (op2
, false, NULL
);
5738 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5740 ret
= note_integer_operands (ret
);
5742 if (semantic_result_type
)
5743 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
5745 protected_set_expr_location (ret
, colon_loc
);
5747 /* If the OP1 and OP2 are the same and don't have side-effects,
5748 warn here, because the COND_EXPR will be turned into OP1. */
5749 if (warn_duplicated_branches
5750 && TREE_CODE (ret
) == COND_EXPR
5751 && (op1
== op2
|| operand_equal_p (op1
, op2
, OEP_ADDRESS_OF_SAME_FIELD
)))
5752 warning_at (EXPR_LOCATION (ret
), OPT_Wduplicated_branches
,
5753 "this condition has identical branches");
5758 /* EXPR is an expression, location LOC, whose result is discarded.
5759 Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5760 whose right-hand operand is such a call, possibly recursively). */
5763 maybe_warn_nodiscard (location_t loc
, tree expr
)
5765 if (VOID_TYPE_P (TREE_TYPE (expr
)))
5767 while (TREE_CODE (expr
) == COMPOUND_EXPR
)
5769 expr
= TREE_OPERAND (expr
, 1);
5770 if (EXPR_HAS_LOCATION (expr
))
5771 loc
= EXPR_LOCATION (expr
);
5773 if (TREE_CODE (expr
) != CALL_EXPR
)
5775 tree fn
= CALL_EXPR_FN (expr
);
5779 if (TREE_CODE (fn
) == ADDR_EXPR
5780 && TREE_CODE (TREE_OPERAND (fn
, 0)) == FUNCTION_DECL
5781 && (attr
= lookup_attribute ("nodiscard",
5782 DECL_ATTRIBUTES (TREE_OPERAND (fn
, 0)))))
5784 fn
= TREE_OPERAND (fn
, 0);
5785 tree args
= TREE_VALUE (attr
);
5787 args
= TREE_VALUE (args
);
5788 auto_diagnostic_group d
;
5791 warned
= warning_at (loc
, OPT_Wunused_result
,
5792 "ignoring return value of %qD, declared with "
5793 "attribute %<nodiscard%>: %E", fn
, args
);
5795 warned
= warning_at (loc
, OPT_Wunused_result
,
5796 "ignoring return value of %qD, declared with "
5797 "attribute %<nodiscard%>", fn
);
5799 inform (DECL_SOURCE_LOCATION (fn
), "declared here");
5803 tree rettype
= TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
)));
5804 attr
= lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype
));
5807 tree args
= TREE_VALUE (attr
);
5809 args
= TREE_VALUE (args
);
5810 auto_diagnostic_group d
;
5813 warned
= warning_at (loc
, OPT_Wunused_result
,
5814 "ignoring return value of type %qT, declared "
5815 "with attribute %<nodiscard%>: %E",
5818 warned
= warning_at (loc
, OPT_Wunused_result
,
5819 "ignoring return value of type %qT, declared "
5820 "with attribute %<nodiscard%>", rettype
);
5823 if (TREE_CODE (fn
) == ADDR_EXPR
)
5825 fn
= TREE_OPERAND (fn
, 0);
5826 if (TREE_CODE (fn
) == FUNCTION_DECL
)
5827 inform (DECL_SOURCE_LOCATION (fn
),
5828 "in call to %qD, declared here", fn
);
5834 /* Return a compound expression that performs two expressions and
5835 returns the value of the second of them.
5837 LOC is the location of the COMPOUND_EXPR. */
5840 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
5842 bool expr1_int_operands
, expr2_int_operands
;
5843 tree eptype
= NULL_TREE
;
5846 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
5847 if (expr1_int_operands
)
5848 expr1
= remove_c_maybe_const_expr (expr1
);
5849 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
5850 if (expr2_int_operands
)
5851 expr2
= remove_c_maybe_const_expr (expr2
);
5853 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
5854 expr1
= TREE_OPERAND (expr1
, 0);
5855 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
5857 eptype
= TREE_TYPE (expr2
);
5858 expr2
= TREE_OPERAND (expr2
, 0);
5861 if (!TREE_SIDE_EFFECTS (expr1
))
5863 /* The left-hand operand of a comma expression is like an expression
5864 statement: with -Wunused, we should warn if it doesn't have
5865 any side-effects, unless it was explicitly cast to (void). */
5866 if (warn_unused_value
)
5868 if (VOID_TYPE_P (TREE_TYPE (expr1
))
5869 && CONVERT_EXPR_P (expr1
))
5871 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
5872 && TREE_CODE (expr1
) == COMPOUND_EXPR
5873 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
5874 ; /* (void) a, (void) b, c */
5876 warning_at (loc
, OPT_Wunused_value
,
5877 "left-hand operand of comma expression has no effect");
5880 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
5881 && warn_unused_value
)
5884 location_t cloc
= loc
;
5885 while (TREE_CODE (r
) == COMPOUND_EXPR
)
5887 if (EXPR_HAS_LOCATION (r
))
5888 cloc
= EXPR_LOCATION (r
);
5889 r
= TREE_OPERAND (r
, 1);
5891 if (!TREE_SIDE_EFFECTS (r
)
5892 && !VOID_TYPE_P (TREE_TYPE (r
))
5893 && !CONVERT_EXPR_P (r
))
5894 warning_at (cloc
, OPT_Wunused_value
,
5895 "right-hand operand of comma expression has no effect");
5898 /* With -Wunused, we should also warn if the left-hand operand does have
5899 side-effects, but computes a value which is not used. For example, in
5900 `foo() + bar(), baz()' the result of the `+' operator is not used,
5901 so we should issue a warning. */
5902 else if (warn_unused_value
)
5903 warn_if_unused_value (expr1
, loc
);
5905 maybe_warn_nodiscard (loc
, expr1
);
5907 if (expr2
== error_mark_node
)
5908 return error_mark_node
;
5910 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
5913 && expr1_int_operands
5914 && expr2_int_operands
)
5915 ret
= note_integer_operands (ret
);
5918 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
5920 protected_set_expr_location (ret
, loc
);
5924 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5925 which we are casting. OTYPE is the type of the expression being
5926 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5927 of the cast. -Wcast-qual appeared on the command line. Named
5928 address space qualifiers are not handled here, because they result
5929 in different warnings. */
5932 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
5934 tree in_type
= type
;
5935 tree in_otype
= otype
;
5940 /* Check that the qualifiers on IN_TYPE are a superset of the
5941 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5942 nodes is uninteresting and we stop as soon as we hit a
5943 non-POINTER_TYPE node on either type. */
5946 in_otype
= TREE_TYPE (in_otype
);
5947 in_type
= TREE_TYPE (in_type
);
5949 /* GNU C allows cv-qualified function types. 'const' means the
5950 function is very pure, 'volatile' means it can't return. We
5951 need to warn when such qualifiers are added, not when they're
5953 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
5954 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
5955 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
5956 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
5958 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
5959 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
5961 while (TREE_CODE (in_type
) == POINTER_TYPE
5962 && TREE_CODE (in_otype
) == POINTER_TYPE
);
5965 warning_at (loc
, OPT_Wcast_qual
,
5966 "cast adds %q#v qualifier to function type", added
);
5969 /* There are qualifiers present in IN_OTYPE that are not present
5971 warning_at (loc
, OPT_Wcast_qual
,
5972 "cast discards %qv qualifier from pointer target type",
5975 if (added
|| discarded
)
5978 /* A cast from **T to const **T is unsafe, because it can cause a
5979 const value to be changed with no additional warning. We only
5980 issue this warning if T is the same on both sides, and we only
5981 issue the warning if there are the same number of pointers on
5982 both sides, as otherwise the cast is clearly unsafe anyhow. A
5983 cast is unsafe when a qualifier is added at one level and const
5984 is not present at all outer levels.
5986 To issue this warning, we check at each level whether the cast
5987 adds new qualifiers not already seen. We don't need to special
5988 case function types, as they won't have the same
5989 TYPE_MAIN_VARIANT. */
5991 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
5993 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
5998 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
6001 in_type
= TREE_TYPE (in_type
);
6002 in_otype
= TREE_TYPE (in_otype
);
6003 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
6006 warning_at (loc
, OPT_Wcast_qual
,
6007 "to be safe all intermediate pointers in cast from "
6008 "%qT to %qT must be %<const%> qualified",
6013 is_const
= TYPE_READONLY (in_type
);
6015 while (TREE_CODE (in_type
) == POINTER_TYPE
);
6018 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
6021 c_safe_arg_type_equiv_p (tree t1
, tree t2
)
6023 if (error_operand_p (t1
) || error_operand_p (t2
))
6026 t1
= TYPE_MAIN_VARIANT (t1
);
6027 t2
= TYPE_MAIN_VARIANT (t2
);
6029 if (TREE_CODE (t1
) == POINTER_TYPE
6030 && TREE_CODE (t2
) == POINTER_TYPE
)
6033 /* The signedness of the parameter matters only when an integral
6034 type smaller than int is promoted to int, otherwise only the
6035 precision of the parameter matters.
6036 This check should make sure that the callee does not see
6037 undefined values in argument registers. */
6038 if (INTEGRAL_TYPE_P (t1
)
6039 && INTEGRAL_TYPE_P (t2
)
6040 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
6041 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
6042 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
6043 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
6046 return comptypes (t1
, t2
);
6049 /* Check if a type cast between two function types can be considered safe. */
6052 c_safe_function_type_cast_p (tree t1
, tree t2
)
6054 if (TREE_TYPE (t1
) == void_type_node
&&
6055 TYPE_ARG_TYPES (t1
) == void_list_node
)
6058 if (TREE_TYPE (t2
) == void_type_node
&&
6059 TYPE_ARG_TYPES (t2
) == void_list_node
)
6062 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
6065 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
6067 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
6068 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
6074 /* Build an expression representing a cast to type TYPE of expression EXPR.
6075 LOC is the location of the cast-- typically the open paren of the cast. */
6078 build_c_cast (location_t loc
, tree type
, tree expr
)
6082 bool int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
6084 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
6085 expr
= TREE_OPERAND (expr
, 0);
6089 value
= remove_c_maybe_const_expr (value
);
6091 if (type
== error_mark_node
|| expr
== error_mark_node
)
6092 return error_mark_node
;
6094 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
6095 only in <protocol> qualifications. But when constructing cast expressions,
6096 the protocols do matter and must be kept around. */
6097 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
6098 return build1 (NOP_EXPR
, type
, expr
);
6100 type
= TYPE_MAIN_VARIANT (type
);
6102 if (TREE_CODE (type
) == ARRAY_TYPE
)
6104 error_at (loc
, "cast specifies array type");
6105 return error_mark_node
;
6108 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6110 error_at (loc
, "cast specifies function type");
6111 return error_mark_node
;
6114 if (!VOID_TYPE_P (type
))
6116 value
= require_complete_type (loc
, value
);
6117 if (value
== error_mark_node
)
6118 return error_mark_node
;
6121 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
6123 if (RECORD_OR_UNION_TYPE_P (type
)
6124 && pedwarn (loc
, OPT_Wpedantic
,
6125 "ISO C forbids casting nonscalar to the same type"))
6127 else if (warn_useless_cast
)
6128 warning_at (loc
, OPT_Wuseless_cast
,
6129 "useless cast to type %qT", type
);
6131 /* Convert to remove any qualifiers from VALUE's type. */
6132 value
= convert (type
, value
);
6134 else if (TREE_CODE (type
) == UNION_TYPE
)
6138 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
6139 if (TREE_TYPE (field
) != error_mark_node
6140 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
6141 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
6147 bool maybe_const
= true;
6149 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
6150 t
= c_fully_fold (value
, false, &maybe_const
);
6151 t
= build_constructor_single (type
, field
, t
);
6153 t
= c_wrap_maybe_const (t
, true);
6154 t
= digest_init (loc
, type
, t
,
6155 NULL_TREE
, false, false, false, true, false, false);
6156 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
6159 error_at (loc
, "cast to union type from type not present in union");
6160 return error_mark_node
;
6166 if (type
== void_type_node
)
6168 tree t
= build1 (CONVERT_EXPR
, type
, value
);
6169 SET_EXPR_LOCATION (t
, loc
);
6173 otype
= TREE_TYPE (value
);
6175 /* Optionally warn about potentially worrisome casts. */
6177 && TREE_CODE (type
) == POINTER_TYPE
6178 && TREE_CODE (otype
) == POINTER_TYPE
)
6179 handle_warn_cast_qual (loc
, type
, otype
);
6181 /* Warn about conversions between pointers to disjoint
6183 if (TREE_CODE (type
) == POINTER_TYPE
6184 && TREE_CODE (otype
) == POINTER_TYPE
6185 && !null_pointer_constant_p (value
))
6187 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
6188 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
6189 addr_space_t as_common
;
6191 if (!addr_space_superset (as_to
, as_from
, &as_common
))
6193 if (ADDR_SPACE_GENERIC_P (as_from
))
6194 warning_at (loc
, 0, "cast to %qs address space pointer "
6195 "from disjoint generic address space pointer",
6196 c_addr_space_name (as_to
));
6198 else if (ADDR_SPACE_GENERIC_P (as_to
))
6199 warning_at (loc
, 0, "cast to generic address space pointer "
6200 "from disjoint %qs address space pointer",
6201 c_addr_space_name (as_from
));
6204 warning_at (loc
, 0, "cast to %qs address space pointer "
6205 "from disjoint %qs address space pointer",
6206 c_addr_space_name (as_to
),
6207 c_addr_space_name (as_from
));
6210 /* Warn of new allocations that are not big enough for the target
6212 if (warn_alloc_size
&& TREE_CODE (value
) == CALL_EXPR
)
6213 if (tree fndecl
= get_callee_fndecl (value
))
6214 if (DECL_IS_MALLOC (fndecl
))
6216 tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (fndecl
));
6217 tree alloc_size
= lookup_attribute ("alloc_size", attrs
);
6219 warn_for_alloc_size (loc
, TREE_TYPE (type
), value
,
6224 /* Warn about possible alignment problems. */
6225 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
6226 && TREE_CODE (type
) == POINTER_TYPE
6227 && TREE_CODE (otype
) == POINTER_TYPE
6228 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
6229 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
6230 /* Don't warn about opaque types, where the actual alignment
6231 restriction is unknown. */
6232 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype
))
6233 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
6234 && min_align_of_type (TREE_TYPE (type
))
6235 > min_align_of_type (TREE_TYPE (otype
)))
6236 warning_at (loc
, OPT_Wcast_align
,
6237 "cast increases required alignment of target type");
6239 if ((TREE_CODE (type
) == INTEGER_TYPE
6240 || TREE_CODE (type
) == BITINT_TYPE
)
6241 && TREE_CODE (otype
) == POINTER_TYPE
6242 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
6243 /* Unlike conversion of integers to pointers, where the
6244 warning is disabled for converting constants because
6245 of cases such as SIG_*, warn about converting constant
6246 pointers to integers. In some cases it may cause unwanted
6247 sign extension, and a warning is appropriate. */
6248 warning_at (loc
, OPT_Wpointer_to_int_cast
,
6249 "cast from pointer to integer of different size");
6251 if (TREE_CODE (value
) == CALL_EXPR
6252 && TREE_CODE (type
) != TREE_CODE (otype
))
6253 warning_at (loc
, OPT_Wbad_function_cast
,
6254 "cast from function call of type %qT "
6255 "to non-matching type %qT", otype
, type
);
6257 if (TREE_CODE (type
) == POINTER_TYPE
6258 && (TREE_CODE (otype
) == INTEGER_TYPE
6259 || TREE_CODE (otype
) == BITINT_TYPE
)
6260 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
6261 /* Don't warn about converting any constant. */
6262 && !TREE_CONSTANT (value
))
6264 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
6265 "of different size");
6267 if (warn_strict_aliasing
<= 2)
6268 strict_aliasing_warning (EXPR_LOCATION (value
), type
, expr
);
6270 /* If pedantic, warn for conversions between function and object
6271 pointer types, except for converting a null pointer constant
6272 to function pointer type. */
6274 && TREE_CODE (type
) == POINTER_TYPE
6275 && TREE_CODE (otype
) == POINTER_TYPE
6276 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
6277 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
6278 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
6279 "conversion of function pointer to object pointer type");
6282 && TREE_CODE (type
) == POINTER_TYPE
6283 && TREE_CODE (otype
) == POINTER_TYPE
6284 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6285 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
6286 && !null_pointer_constant_p (value
))
6287 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
6288 "conversion of object pointer to function pointer type");
6290 if (TREE_CODE (type
) == POINTER_TYPE
6291 && TREE_CODE (otype
) == POINTER_TYPE
6292 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6293 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
6294 && !c_safe_function_type_cast_p (TREE_TYPE (type
),
6296 warning_at (loc
, OPT_Wcast_function_type
,
6297 "cast between incompatible function types"
6298 " from %qT to %qT", otype
, type
);
6301 value
= convert (type
, value
);
6303 /* Ignore any integer overflow caused by the cast. */
6304 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
6306 if (TREE_OVERFLOW_P (ovalue
))
6308 if (!TREE_OVERFLOW (value
))
6310 /* Avoid clobbering a shared constant. */
6311 value
= copy_node (value
);
6312 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
6315 else if (TREE_OVERFLOW (value
))
6316 /* Reset VALUE's overflow flags, ensuring constant sharing. */
6317 value
= wide_int_to_tree (TREE_TYPE (value
), wi::to_wide (value
));
6321 /* Don't let a cast be an lvalue. */
6322 if (lvalue_p (value
))
6323 value
= non_lvalue_loc (loc
, value
);
6325 /* Don't allow the results of casting to floating-point or complex
6326 types be confused with actual constants, or casts involving
6327 integer and pointer types other than direct integer-to-integer
6328 and integer-to-pointer be confused with integer constant
6329 expressions and null pointer constants. */
6330 if (TREE_CODE (value
) == REAL_CST
6331 || TREE_CODE (value
) == COMPLEX_CST
6332 || (TREE_CODE (value
) == INTEGER_CST
6333 && !((TREE_CODE (expr
) == INTEGER_CST
6334 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
6335 || TREE_CODE (expr
) == REAL_CST
6336 || TREE_CODE (expr
) == COMPLEX_CST
)))
6337 value
= build1 (NOP_EXPR
, type
, value
);
6339 /* If the expression has integer operands and so can occur in an
6340 unevaluated part of an integer constant expression, ensure the
6341 return value reflects this. */
6343 && INTEGRAL_TYPE_P (type
)
6344 && value
!= error_mark_node
6345 && !EXPR_INT_CONST_OPERANDS (value
))
6346 value
= note_integer_operands (value
);
6348 protected_set_expr_location (value
, loc
);
6352 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
6353 location of the open paren of the cast, or the position of the cast
6356 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
6359 tree type_expr
= NULL_TREE
;
6360 bool type_expr_const
= true;
6362 int saved_wsp
= warn_strict_prototypes
;
6364 /* This avoids warnings about unprototyped casts on
6365 integers. E.g. "#define SIG_DFL (void(*)())0". */
6366 if (TREE_CODE (expr
) == INTEGER_CST
)
6367 warn_strict_prototypes
= 0;
6368 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
6369 warn_strict_prototypes
= saved_wsp
;
6371 if (TREE_CODE (expr
) == ADDR_EXPR
&& !VOID_TYPE_P (type
)
6372 && reject_gcc_builtin (expr
))
6373 return error_mark_node
;
6375 ret
= build_c_cast (loc
, type
, expr
);
6378 bool inner_expr_const
= true;
6379 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
6380 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
6381 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
6382 && inner_expr_const
);
6383 SET_EXPR_LOCATION (ret
, loc
);
6386 if (!EXPR_HAS_LOCATION (ret
))
6387 protected_set_expr_location (ret
, loc
);
6389 /* C++ does not permits types to be defined in a cast, but it
6390 allows references to incomplete types. */
6391 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
6392 warning_at (loc
, OPT_Wc___compat
,
6393 "defining a type in a cast is invalid in C++");
6398 /* Build an assignment expression of lvalue LHS from value RHS.
6399 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6400 may differ from TREE_TYPE (LHS) for an enum bitfield.
6401 MODIFYCODE is the code for a binary operator that we use
6402 to combine the old value of LHS with RHS to get the new value.
6403 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6404 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6405 which may differ from TREE_TYPE (RHS) for an enum value.
6407 LOCATION is the location of the MODIFYCODE operator.
6408 RHS_LOC is the location of the RHS. */
6411 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
6412 enum tree_code modifycode
,
6413 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
6417 tree rhseval
= NULL_TREE
;
6418 tree lhstype
= TREE_TYPE (lhs
);
6419 tree olhstype
= lhstype
;
6423 /* Types that aren't fully specified cannot be used in assignments. */
6424 lhs
= require_complete_type (location
, lhs
);
6426 /* Avoid duplicate error messages from operands that had errors. */
6427 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
6428 return error_mark_node
;
6430 /* Ensure an error for assigning a non-lvalue array to an array in
6432 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
6434 error_at (location
, "assignment to expression with array type");
6435 return error_mark_node
;
6438 /* For ObjC properties, defer this check. */
6439 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
6440 return error_mark_node
;
6442 is_atomic_op
= really_atomic_lvalue (lhs
);
6446 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
6448 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
6449 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
6451 if (inner
== error_mark_node
)
6452 return error_mark_node
;
6453 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
6454 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
6455 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
6456 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
6457 protected_set_expr_location (result
, location
);
6461 /* If a binary op has been requested, combine the old LHS value with the RHS
6462 producing the value we should actually store into the LHS. */
6464 if (modifycode
!= NOP_EXPR
)
6466 lhs
= c_fully_fold (lhs
, false, NULL
, true);
6467 lhs
= stabilize_reference (lhs
);
6469 /* Construct the RHS for any non-atomic compound assignemnt. */
6472 /* If in LHS op= RHS the RHS has side-effects, ensure they
6473 are preevaluated before the rest of the assignment expression's
6474 side-effects, because RHS could contain e.g. function calls
6476 if (TREE_SIDE_EFFECTS (rhs
))
6478 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6479 newrhs
= save_expr (TREE_OPERAND (rhs
, 0));
6481 newrhs
= save_expr (rhs
);
6483 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6484 newrhs
= build1 (EXCESS_PRECISION_EXPR
, TREE_TYPE (rhs
),
6487 newrhs
= build_binary_op (location
,
6488 modifycode
, lhs
, newrhs
, true);
6490 /* The original type of the right hand side is no longer
6492 rhs_origtype
= NULL_TREE
;
6496 if (c_dialect_objc ())
6498 /* Check if we are modifying an Objective-C property reference;
6499 if so, we need to generate setter calls. */
6500 if (TREE_CODE (newrhs
) == EXCESS_PRECISION_EXPR
)
6501 result
= objc_maybe_build_modify_expr (lhs
, TREE_OPERAND (newrhs
, 0));
6503 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
6507 /* Else, do the check that we postponed for Objective-C. */
6508 if (!lvalue_or_else (location
, lhs
, lv_assign
))
6509 return error_mark_node
;
6512 /* Give an error for storing in something that is 'const'. */
6514 if (TYPE_READONLY (lhstype
)
6515 || (RECORD_OR_UNION_TYPE_P (lhstype
)
6516 && C_TYPE_FIELDS_READONLY (lhstype
)))
6518 readonly_error (location
, lhs
, lv_assign
);
6519 return error_mark_node
;
6521 else if (TREE_READONLY (lhs
))
6522 readonly_warning (lhs
, lv_assign
);
6524 /* If storing into a structure or union member,
6525 it has probably been given type `int'.
6526 Compute the type that would go with
6527 the actual amount of storage the member occupies. */
6529 if (TREE_CODE (lhs
) == COMPONENT_REF
6530 && (TREE_CODE (lhstype
) == INTEGER_TYPE
6531 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
6532 || SCALAR_FLOAT_TYPE_P (lhstype
)
6533 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
6534 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
6536 /* If storing in a field that is in actuality a short or narrower than one,
6537 we must store in the field in its actual type. */
6539 if (lhstype
!= TREE_TYPE (lhs
))
6541 lhs
= copy_node (lhs
);
6542 TREE_TYPE (lhs
) = lhstype
;
6545 /* Issue -Wc++-compat warnings about an assignment to an enum type
6546 when LHS does not have its original type. This happens for,
6547 e.g., an enum bitfield in a struct. */
6549 && lhs_origtype
!= NULL_TREE
6550 && lhs_origtype
!= lhstype
6551 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
6553 tree checktype
= (rhs_origtype
!= NULL_TREE
6556 if (checktype
!= error_mark_node
6557 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
6558 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
6559 warning_at (location
, OPT_Wc___compat
,
6560 "enum conversion in assignment is invalid in C++");
6563 /* Remove qualifiers. */
6564 lhstype
= build_qualified_type (lhstype
, TYPE_UNQUALIFIED
);
6565 olhstype
= build_qualified_type (olhstype
, TYPE_UNQUALIFIED
);
6567 /* Convert new value to destination type. Fold it first, then
6568 restore any excess precision information, for the sake of
6569 conversion warnings. */
6571 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
6573 tree rhs_semantic_type
= NULL_TREE
;
6576 if (TREE_CODE (newrhs
) == EXCESS_PRECISION_EXPR
)
6578 rhs_semantic_type
= TREE_TYPE (newrhs
);
6579 newrhs
= TREE_OPERAND (newrhs
, 0);
6581 npc
= null_pointer_constant_p (newrhs
);
6582 newrhs
= c_fully_fold (newrhs
, false, NULL
);
6583 if (rhs_semantic_type
)
6584 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
6587 npc
= null_pointer_constant_p (newrhs
);
6588 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
6589 rhs_origtype
, ic_assign
, npc
,
6590 NULL_TREE
, NULL_TREE
, 0);
6591 if (TREE_CODE (newrhs
) == ERROR_MARK
)
6592 return error_mark_node
;
6595 /* Emit ObjC write barrier, if necessary. */
6596 if (c_dialect_objc () && flag_objc_gc
)
6598 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
6601 protected_set_expr_location (result
, location
);
6606 /* Scan operands. */
6609 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
6612 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
6613 TREE_SIDE_EFFECTS (result
) = 1;
6614 protected_set_expr_location (result
, location
);
6617 /* If we got the LHS in a different type for storing in,
6618 convert the result back to the nominal type of LHS
6619 so that the value we return always has the same type
6620 as the LHS argument. */
6622 if (olhstype
== TREE_TYPE (result
))
6625 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
6626 rhs_origtype
, ic_assign
, false, NULL_TREE
,
6628 protected_set_expr_location (result
, location
);
6632 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
6636 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6637 This is used to implement -fplan9-extensions. */
6640 find_anonymous_field_with_type (tree struct_type
, tree type
)
6645 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type
));
6647 for (field
= TYPE_FIELDS (struct_type
);
6649 field
= TREE_CHAIN (field
))
6651 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
6652 ? c_build_qualified_type (TREE_TYPE (field
),
6654 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
6655 if (DECL_NAME (field
) == NULL
6656 && comptypes (type
, fieldtype
))
6662 else if (DECL_NAME (field
) == NULL
6663 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
6664 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
6674 /* RHS is an expression whose type is pointer to struct. If there is
6675 an anonymous field in RHS with type TYPE, then return a pointer to
6676 that field in RHS. This is used with -fplan9-extensions. This
6677 returns NULL if no conversion could be found. */
6680 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
6682 tree rhs_struct_type
, lhs_main_type
;
6683 tree field
, found_field
;
6684 bool found_sub_field
;
6687 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
6688 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
6689 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type
));
6691 gcc_assert (POINTER_TYPE_P (type
));
6692 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
6693 ? c_build_qualified_type (TREE_TYPE (type
),
6695 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6697 found_field
= NULL_TREE
;
6698 found_sub_field
= false;
6699 for (field
= TYPE_FIELDS (rhs_struct_type
);
6701 field
= TREE_CHAIN (field
))
6703 if (DECL_NAME (field
) != NULL_TREE
6704 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
6706 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
6707 ? c_build_qualified_type (TREE_TYPE (field
),
6709 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
6710 if (comptypes (lhs_main_type
, fieldtype
))
6712 if (found_field
!= NULL_TREE
)
6714 found_field
= field
;
6716 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
6719 if (found_field
!= NULL_TREE
)
6721 found_field
= field
;
6722 found_sub_field
= true;
6726 if (found_field
== NULL_TREE
)
6729 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
6730 build_fold_indirect_ref (rhs
), found_field
,
6732 ret
= build_fold_addr_expr_loc (location
, ret
);
6734 if (found_sub_field
)
6736 ret
= convert_to_anonymous_field (location
, type
, ret
);
6737 gcc_assert (ret
!= NULL_TREE
);
6743 /* Issue an error message for a bad initializer component.
6744 GMSGID identifies the message.
6745 The component name is taken from the spelling stack. */
6747 static void ATTRIBUTE_GCC_DIAG (2,0)
6748 error_init (location_t loc
, const char *gmsgid
, ...)
6752 auto_diagnostic_group d
;
6754 /* The gmsgid may be a format string with %< and %>. */
6756 va_start (ap
, gmsgid
);
6757 bool warned
= emit_diagnostic_valist (DK_ERROR
, loc
, -1, gmsgid
, &ap
);
6760 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6761 if (*ofwhat
&& warned
)
6762 inform (loc
, "(near initialization for %qs)", ofwhat
);
6765 /* Used to implement pedwarn_init and permerror_init. */
6767 static void ATTRIBUTE_GCC_DIAG (3,0)
6768 pedwarn_permerror_init (location_t loc
, int opt
, const char *gmsgid
,
6769 va_list *ap
, diagnostic_t kind
)
6771 /* Use the location where a macro was expanded rather than where
6772 it was defined to make sure macros defined in system headers
6773 but used incorrectly elsewhere are diagnosed. */
6774 location_t exploc
= expansion_point_location_if_in_system_header (loc
);
6775 auto_diagnostic_group d
;
6776 bool warned
= emit_diagnostic_valist (kind
, exploc
, opt
, gmsgid
, ap
);
6777 char *ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6778 if (*ofwhat
&& warned
)
6779 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6782 /* Issue a pedantic warning for a bad initializer component. OPT is
6783 the option OPT_* (from options.h) controlling this warning or 0 if
6784 it is unconditionally given. GMSGID identifies the message. The
6785 component name is taken from the spelling stack. */
6787 static void ATTRIBUTE_GCC_DIAG (3,0)
6788 pedwarn_init (location_t loc
, int opt
, const char *gmsgid
, ...)
6791 va_start (ap
, gmsgid
);
6792 pedwarn_permerror_init (loc
, opt
, gmsgid
, &ap
, DK_PEDWARN
);
6796 /* Like pedwarn_init, but issue a permerror. */
6798 static void ATTRIBUTE_GCC_DIAG (3,0)
6799 permerror_init (location_t loc
, int opt
, const char *gmsgid
, ...)
6802 va_start (ap
, gmsgid
);
6803 pedwarn_permerror_init (loc
, opt
, gmsgid
, &ap
, DK_PERMERROR
);
6807 /* Issue a warning for a bad initializer component.
6809 OPT is the OPT_W* value corresponding to the warning option that
6810 controls this warning. GMSGID identifies the message. The
6811 component name is taken from the spelling stack. */
6814 warning_init (location_t loc
, int opt
, const char *gmsgid
)
6819 auto_diagnostic_group d
;
6821 /* Use the location where a macro was expanded rather than where
6822 it was defined to make sure macros defined in system headers
6823 but used incorrectly elsewhere are diagnosed. */
6824 location_t exploc
= expansion_point_location_if_in_system_header (loc
);
6826 /* The gmsgid may be a format string with %< and %>. */
6827 warned
= warning_at (exploc
, opt
, gmsgid
);
6828 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6829 if (*ofwhat
&& warned
)
6830 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6833 /* If TYPE is an array type and EXPR is a parenthesized string
6834 constant, warn if pedantic that EXPR is being used to initialize an
6835 object of type TYPE. */
6838 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
6841 && TREE_CODE (type
) == ARRAY_TYPE
6842 && TREE_CODE (expr
.value
) == STRING_CST
6843 && expr
.original_code
!= STRING_CST
)
6844 pedwarn_init (loc
, OPT_Wpedantic
,
6845 "array initialized from parenthesized string constant");
6848 /* Attempt to locate the parameter with the given index within FNDECL,
6849 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6852 get_fndecl_argument_location (tree fndecl
, int argnum
)
6857 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6858 for (i
= 0, param
= DECL_ARGUMENTS (fndecl
);
6859 i
< argnum
&& param
;
6860 i
++, param
= TREE_CHAIN (param
))
6863 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6864 return DECL_SOURCE_LOCATION (FNDECL). */
6866 return DECL_SOURCE_LOCATION (fndecl
);
6868 return DECL_SOURCE_LOCATION (param
);
6871 /* Issue a note about a mismatching argument for parameter PARMNUM
6872 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6873 Attempt to issue the note at the pertinent parameter of the decl;
6874 failing that issue it at the location of FUNDECL; failing that
6875 issue it at PLOC. */
6878 inform_for_arg (tree fundecl
, location_t ploc
, int parmnum
,
6879 tree expected_type
, tree actual_type
)
6882 if (fundecl
&& !DECL_IS_UNDECLARED_BUILTIN (fundecl
))
6883 loc
= get_fndecl_argument_location (fundecl
, parmnum
- 1);
6888 "expected %qT but argument is of type %qT",
6889 expected_type
, actual_type
);
6892 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6893 function FUNDECL declared without prototype to parameter PARMNUM of
6894 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6897 maybe_warn_builtin_no_proto_arg (location_t loc
, tree fundecl
, int parmnum
,
6898 tree parmtype
, tree argtype
)
6900 tree_code parmcode
= TREE_CODE (parmtype
);
6901 tree_code argcode
= TREE_CODE (argtype
);
6902 tree promoted
= c_type_promotes_to (argtype
);
6904 /* Avoid warning for enum arguments that promote to an integer type
6905 of the same size/mode. */
6906 if (parmcode
== INTEGER_TYPE
6907 && argcode
== ENUMERAL_TYPE
6908 && TYPE_MODE (parmtype
) == TYPE_MODE (argtype
))
6911 if ((parmcode
== argcode
6912 || (parmcode
== INTEGER_TYPE
6913 && argcode
== ENUMERAL_TYPE
))
6914 && TYPE_MAIN_VARIANT (parmtype
) == TYPE_MAIN_VARIANT (promoted
))
6917 /* This diagnoses even signed/unsigned mismatches. Those might be
6918 safe in many cases but GCC may emit suboptimal code for them so
6919 warning on those cases drives efficiency improvements. */
6920 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
6921 TYPE_MAIN_VARIANT (promoted
) == argtype
6922 ? G_("%qD argument %d type is %qT where %qT is expected "
6923 "in a call to built-in function declared without "
6925 : G_("%qD argument %d promotes to %qT where %qT is expected "
6926 "in a call to built-in function declared without "
6928 fundecl
, parmnum
, promoted
, parmtype
))
6929 inform (DECL_SOURCE_LOCATION (fundecl
),
6930 "built-in %qD declared here",
6934 /* Convert value RHS to type TYPE as preparation for an assignment to
6935 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6936 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6937 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6938 constant before any folding.
6939 The real work of conversion is done by `convert'.
6940 The purpose of this function is to generate error messages
6941 for assignments that are not allowed in C.
6942 ERRTYPE says whether it is argument passing, assignment,
6943 initialization or return.
6945 In the following example, '~' denotes where EXPR_LOC and '^' where
6948 f (var); [ic_argpass]
6950 x = var; [ic_assign]
6952 int x = var; [ic_init]
6954 return x; [ic_return]
6957 FUNCTION is a tree for the function being called.
6958 PARMNUM is the number of the argument, for printing in error messages.
6959 WARNOPT may be set to a warning option to issue the corresponding warning
6960 rather than an error for invalid conversions. Used for calls to built-in
6961 functions declared without a prototype. */
6964 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
6965 tree rhs
, tree origtype
, enum impl_conv errtype
,
6966 bool null_pointer_constant
, tree fundecl
,
6967 tree function
, int parmnum
, int warnopt
/* = 0 */)
6969 enum tree_code codel
= TREE_CODE (type
);
6970 tree orig_rhs
= rhs
;
6972 enum tree_code coder
;
6973 tree rname
= NULL_TREE
;
6974 bool objc_ok
= false;
6976 /* Use the expansion point location to handle cases such as user's
6977 function returning a wrong-type macro defined in a system header. */
6978 location
= expansion_point_location_if_in_system_header (location
);
6980 if (errtype
== ic_argpass
)
6983 /* Change pointer to function to the function itself for
6985 if (TREE_CODE (function
) == ADDR_EXPR
6986 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
6987 function
= TREE_OPERAND (function
, 0);
6989 /* Handle an ObjC selector specially for diagnostics. */
6990 selector
= objc_message_selector ();
6992 if (selector
&& parmnum
> 2)
6999 /* This macro is used to emit diagnostics to ensure that all format
7000 strings are complete sentences, visible to gettext and checked at
7002 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
7008 auto_diagnostic_group d; \
7009 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
7010 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7014 pedwarn (LOCATION, OPT, AS); \
7017 case ic_init_const: \
7018 pedwarn_init (LOCATION, OPT, IN); \
7021 pedwarn (LOCATION, OPT, RE); \
7024 gcc_unreachable (); \
7028 /* This macro is used to emit diagnostics to ensure that all format
7029 strings are complete sentences, visible to gettext and checked at
7030 compile time. It can be called with 'pedwarn' or 'warning_at'. */
7031 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7037 auto_diagnostic_group d; \
7039 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
7040 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7042 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
7043 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
7049 pedwarn (LOCATION, OPT, AS, QUALS); \
7051 warning_at (LOCATION, OPT, AS, QUALS); \
7054 case ic_init_const: \
7056 pedwarn (LOCATION, OPT, IN, QUALS); \
7058 warning_at (LOCATION, OPT, IN, QUALS); \
7062 pedwarn (LOCATION, OPT, RE, QUALS); \
7064 warning_at (LOCATION, OPT, RE, QUALS); \
7067 gcc_unreachable (); \
7071 /* This macro is used to emit diagnostics to ensure that all format
7072 strings are complete sentences, visible to gettext and checked at
7073 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
7074 extra parameter to enumerate qualifiers. */
7075 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7076 WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
7079 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
7080 rhs
= TREE_OPERAND (rhs
, 0);
7082 rhstype
= TREE_TYPE (rhs
);
7083 coder
= TREE_CODE (rhstype
);
7085 if (coder
== ERROR_MARK
)
7086 return error_mark_node
;
7088 if (c_dialect_objc ())
7112 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
7115 if (warn_cxx_compat
)
7117 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
7118 if (checktype
!= error_mark_node
7119 && TREE_CODE (type
) == ENUMERAL_TYPE
7120 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
7124 if (pedwarn (expr_loc
, OPT_Wc___compat
, "enum conversion when "
7125 "passing argument %d of %qE is invalid in C++",
7127 inform ((fundecl
&& !DECL_IS_UNDECLARED_BUILTIN (fundecl
))
7128 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
7129 "expected %qT but argument is of type %qT",
7133 pedwarn (location
, OPT_Wc___compat
, "enum conversion from %qT to "
7134 "%qT in assignment is invalid in C++", rhstype
, type
);
7138 pedwarn_init (location
, OPT_Wc___compat
, "enum conversion from "
7139 "%qT to %qT in initialization is invalid in C++",
7143 pedwarn (location
, OPT_Wc___compat
, "enum conversion from %qT to "
7144 "%qT in return is invalid in C++", rhstype
, type
);
7151 if (warn_enum_conversion
)
7153 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
7154 if (checktype
!= error_mark_node
7155 && TREE_CODE (checktype
) == ENUMERAL_TYPE
7156 && TREE_CODE (type
) == ENUMERAL_TYPE
7157 && !comptypes (TYPE_MAIN_VARIANT (checktype
), TYPE_MAIN_VARIANT (type
)))
7159 gcc_rich_location
loc (location
);
7160 warning_at (&loc
, OPT_Wenum_conversion
,
7161 "implicit conversion from %qT to %qT",
7166 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
7168 warn_for_address_of_packed_member (type
, orig_rhs
);
7172 if (coder
== VOID_TYPE
)
7174 /* Except for passing an argument to an unprototyped function,
7175 this is a constraint violation. When passing an argument to
7176 an unprototyped function, it is compile-time undefined;
7177 making it a constraint in that case was rejected in
7179 const char msg
[] = "void value not ignored as it ought to be";
7181 warning_at (location
, warnopt
, msg
);
7183 error_at (location
, msg
);
7184 return error_mark_node
;
7186 rhs
= require_complete_type (location
, rhs
);
7187 if (rhs
== error_mark_node
)
7188 return error_mark_node
;
7190 if (coder
== POINTER_TYPE
&& reject_gcc_builtin (rhs
))
7191 return error_mark_node
;
7193 /* A non-reference type can convert to a reference. This handles
7194 va_start, va_copy and possibly port built-ins. */
7195 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
7197 if (!lvalue_p (rhs
))
7199 const char msg
[] = "cannot pass rvalue to reference parameter";
7201 warning_at (location
, warnopt
, msg
);
7203 error_at (location
, msg
);
7204 return error_mark_node
;
7206 if (!c_mark_addressable (rhs
))
7207 return error_mark_node
;
7208 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
7209 SET_EXPR_LOCATION (rhs
, location
);
7211 rhs
= convert_for_assignment (location
, expr_loc
,
7212 build_pointer_type (TREE_TYPE (type
)),
7213 rhs
, origtype
, errtype
,
7214 null_pointer_constant
, fundecl
, function
,
7216 if (rhs
== error_mark_node
)
7217 return error_mark_node
;
7219 rhs
= build1 (NOP_EXPR
, type
, rhs
);
7220 SET_EXPR_LOCATION (rhs
, location
);
7223 /* Some types can interconvert without explicit casts. */
7224 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
7225 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
7226 return convert (type
, rhs
);
7227 /* Arithmetic types all interconvert, and enum is treated like int. */
7228 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
7229 || codel
== FIXED_POINT_TYPE
7230 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
7231 || codel
== BOOLEAN_TYPE
|| codel
== BITINT_TYPE
)
7232 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
7233 || coder
== FIXED_POINT_TYPE
7234 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
7235 || coder
== BOOLEAN_TYPE
|| coder
== BITINT_TYPE
))
7237 if (warnopt
&& errtype
== ic_argpass
)
7238 maybe_warn_builtin_no_proto_arg (expr_loc
, fundecl
, parmnum
, type
,
7241 bool save
= in_late_binary_op
;
7242 if (C_BOOLEAN_TYPE_P (type
) || codel
== COMPLEX_TYPE
7243 || (coder
== REAL_TYPE
7244 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
7245 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
7246 in_late_binary_op
= true;
7247 tree ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
7248 ? expr_loc
: location
, type
, orig_rhs
,
7249 errtype
== ic_init_const
);
7250 in_late_binary_op
= save
;
7254 /* Aggregates in different TUs might need conversion. */
7255 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
7257 && comptypes (TYPE_MAIN_VARIANT (type
), TYPE_MAIN_VARIANT (rhstype
)))
7258 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
7259 ? expr_loc
: location
, type
, rhs
);
7261 /* Conversion to a transparent union or record from its member types.
7262 This applies only to function arguments. */
7263 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
7264 && TYPE_TRANSPARENT_AGGR (type
))
7265 && errtype
== ic_argpass
)
7267 tree memb
, marginal_memb
= NULL_TREE
;
7269 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
7271 tree memb_type
= TREE_TYPE (memb
);
7273 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
7274 TYPE_MAIN_VARIANT (rhstype
)))
7277 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
7280 if (coder
== POINTER_TYPE
)
7282 tree ttl
= TREE_TYPE (memb_type
);
7283 tree ttr
= TREE_TYPE (rhstype
);
7285 /* Any non-function converts to a [const][volatile] void *
7286 and vice versa; otherwise, targets must be the same.
7287 Meanwhile, the lhs target must have all the qualifiers of
7289 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
7290 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
7291 || comp_target_types (location
, memb_type
, rhstype
))
7293 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
7294 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
7295 /* If this type won't generate any warnings, use it. */
7296 if (lquals
== rquals
7297 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
7298 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
7299 ? ((lquals
| rquals
) == rquals
)
7300 : ((lquals
| rquals
) == lquals
)))
7303 /* Keep looking for a better type, but remember this one. */
7305 marginal_memb
= memb
;
7309 /* Can convert integer zero to any pointer type. */
7310 if (null_pointer_constant
)
7312 rhs
= null_pointer_node
;
7317 if (memb
|| marginal_memb
)
7321 /* We have only a marginally acceptable member type;
7322 it needs a warning. */
7323 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
7324 tree ttr
= TREE_TYPE (rhstype
);
7326 /* Const and volatile mean something different for function
7327 types, so the usual warnings are not appropriate. */
7328 if (TREE_CODE (ttr
) == FUNCTION_TYPE
7329 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
7331 /* Because const and volatile on functions are
7332 restrictions that say the function will not do
7333 certain things, it is okay to use a const or volatile
7334 function where an ordinary one is wanted, but not
7336 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
7337 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
7338 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
7339 OPT_Wdiscarded_qualifiers
,
7340 G_("passing argument %d of %qE "
7341 "makes %q#v qualified function "
7342 "pointer from unqualified"),
7343 G_("assignment makes %q#v qualified "
7344 "function pointer from "
7346 G_("initialization makes %q#v qualified "
7347 "function pointer from "
7349 G_("return makes %q#v qualified function "
7350 "pointer from unqualified"),
7351 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
7353 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
7354 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
7355 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
7356 OPT_Wdiscarded_qualifiers
,
7357 G_("passing argument %d of %qE discards "
7358 "%qv qualifier from pointer target type"),
7359 G_("assignment discards %qv qualifier "
7360 "from pointer target type"),
7361 G_("initialization discards %qv qualifier "
7362 "from pointer target type"),
7363 G_("return discards %qv qualifier from "
7364 "pointer target type"),
7365 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
7367 memb
= marginal_memb
;
7370 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
7371 pedwarn (location
, OPT_Wpedantic
,
7372 "ISO C prohibits argument conversion to union type");
7374 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
7375 return build_constructor_single (type
, memb
, rhs
);
7379 /* Conversions among pointers */
7380 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
7381 && (coder
== codel
))
7383 /* If RHS refers to a built-in declared without a prototype
7384 BLTIN is the declaration of the built-in with a prototype
7385 and RHSTYPE is set to the actual type of the built-in. */
7387 rhstype
= type_or_builtin_type (rhs
, &bltin
);
7389 tree ttl
= TREE_TYPE (type
);
7390 tree ttr
= TREE_TYPE (rhstype
);
7393 bool is_opaque_pointer
;
7394 bool target_cmp
= false; /* Cache comp_target_types () result. */
7398 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
7399 mvl
= (TYPE_ATOMIC (mvl
)
7400 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
7402 : TYPE_MAIN_VARIANT (mvl
));
7403 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
7404 mvr
= (TYPE_ATOMIC (mvr
)
7405 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
7407 : TYPE_MAIN_VARIANT (mvr
));
7408 /* Opaque pointers are treated like void pointers. */
7409 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
7411 /* The Plan 9 compiler permits a pointer to a struct to be
7412 automatically converted into a pointer to an anonymous field
7413 within the struct. */
7414 if (flag_plan9_extensions
7415 && RECORD_OR_UNION_TYPE_P (mvl
)
7416 && RECORD_OR_UNION_TYPE_P (mvr
)
7419 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
7420 if (new_rhs
!= NULL_TREE
)
7423 rhstype
= TREE_TYPE (rhs
);
7424 coder
= TREE_CODE (rhstype
);
7425 ttr
= TREE_TYPE (rhstype
);
7426 mvr
= TYPE_MAIN_VARIANT (ttr
);
7430 /* C++ does not allow the implicit conversion void* -> T*. However,
7431 for the purpose of reducing the number of false positives, we
7432 tolerate the special case of
7436 where NULL is typically defined in C to be '(void *) 0'. */
7437 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
7438 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
7440 "request for implicit conversion "
7441 "from %qT to %qT not permitted in C++", rhstype
, type
);
7443 /* Warn of new allocations that are not big enough for the target
7445 if (warn_alloc_size
&& TREE_CODE (rhs
) == CALL_EXPR
)
7446 if (tree fndecl
= get_callee_fndecl (rhs
))
7447 if (DECL_IS_MALLOC (fndecl
))
7449 tree attrs
= TYPE_ATTRIBUTES (TREE_TYPE (fndecl
));
7450 tree alloc_size
= lookup_attribute ("alloc_size", attrs
);
7452 warn_for_alloc_size (location
, ttl
, rhs
, alloc_size
);
7455 /* See if the pointers point to incompatible address spaces. */
7456 asl
= TYPE_ADDR_SPACE (ttl
);
7457 asr
= TYPE_ADDR_SPACE (ttr
);
7458 if (!null_pointer_constant_p (rhs
)
7459 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
7461 auto_diagnostic_group d
;
7462 bool diagnosed
= true;
7467 const char msg
[] = G_("passing argument %d of %qE from "
7468 "pointer to non-enclosed address space");
7471 = warning_at (expr_loc
, warnopt
, msg
, parmnum
, rname
);
7473 error_at (expr_loc
, msg
, parmnum
, rname
);
7478 const char msg
[] = G_("assignment from pointer to "
7479 "non-enclosed address space");
7481 diagnosed
= warning_at (location
, warnopt
, msg
);
7483 error_at (location
, msg
);
7489 const char msg
[] = G_("initialization from pointer to "
7490 "non-enclosed address space");
7492 diagnosed
= warning_at (location
, warnopt
, msg
);
7494 error_at (location
, msg
);
7499 const char msg
[] = G_("return from pointer to "
7500 "non-enclosed address space");
7502 diagnosed
= warning_at (location
, warnopt
, msg
);
7504 error_at (location
, msg
);
7512 if (errtype
== ic_argpass
)
7513 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7515 inform (location
, "expected %qT but pointer is of type %qT",
7518 return error_mark_node
;
7521 /* Check if the right-hand side has a format attribute but the
7522 left-hand side doesn't. */
7523 if (warn_suggest_attribute_format
7524 && check_missing_format_attribute (type
, rhstype
))
7529 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
7530 "argument %d of %qE might be "
7531 "a candidate for a format attribute",
7535 warning_at (location
, OPT_Wsuggest_attribute_format
,
7536 "assignment left-hand side might be "
7537 "a candidate for a format attribute");
7541 warning_at (location
, OPT_Wsuggest_attribute_format
,
7542 "initialization left-hand side might be "
7543 "a candidate for a format attribute");
7546 warning_at (location
, OPT_Wsuggest_attribute_format
,
7547 "return type might be "
7548 "a candidate for a format attribute");
7555 /* See if the pointers point to incompatible scalar storage orders. */
7556 if (warn_scalar_storage_order
7557 && !null_pointer_constant_p (rhs
)
7558 && (AGGREGATE_TYPE_P (ttl
) && TYPE_REVERSE_STORAGE_ORDER (ttl
))
7559 != (AGGREGATE_TYPE_P (ttr
) && TYPE_REVERSE_STORAGE_ORDER (ttr
)))
7566 /* Do not warn for built-in functions, for example memcpy, since we
7567 control how they behave and they can be useful in this area. */
7568 if (TREE_CODE (rname
) != FUNCTION_DECL
7569 || !fndecl_built_in_p (rname
))
7570 warning_at (location
, OPT_Wscalar_storage_order
,
7571 "passing argument %d of %qE from incompatible "
7572 "scalar storage order", parmnum
, rname
);
7575 /* Do not warn if the RHS is a call to a function that returns a
7576 pointer that is not an alias. */
7577 if (TREE_CODE (rhs
) != CALL_EXPR
7578 || (t
= get_callee_fndecl (rhs
)) == NULL_TREE
7579 || !DECL_IS_MALLOC (t
))
7580 warning_at (location
, OPT_Wscalar_storage_order
,
7581 "assignment to %qT from pointer type %qT with "
7582 "incompatible scalar storage order", type
, rhstype
);
7587 if (TREE_CODE (rhs
) != CALL_EXPR
7588 || (t
= get_callee_fndecl (rhs
)) == NULL_TREE
7589 || !DECL_IS_MALLOC (t
))
7590 warning_at (location
, OPT_Wscalar_storage_order
,
7591 "initialization of %qT from pointer type %qT with "
7592 "incompatible scalar storage order", type
, rhstype
);
7595 warning_at (location
, OPT_Wscalar_storage_order
,
7596 "returning %qT from pointer type with incompatible "
7597 "scalar storage order %qT", rhstype
, type
);
7604 /* Any non-function converts to a [const][volatile] void *
7605 and vice versa; otherwise, targets must be the same.
7606 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7607 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
7608 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
7609 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
7610 || is_opaque_pointer
7611 || ((c_common_unsigned_type (mvl
)
7612 == c_common_unsigned_type (mvr
))
7613 && (c_common_signed_type (mvl
)
7614 == c_common_signed_type (mvr
))
7615 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
7617 /* Warn about loss of qualifers from pointers to arrays with
7618 qualifiers on the element type. */
7619 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
7621 ttr
= strip_array_types (ttr
);
7622 ttl
= strip_array_types (ttl
);
7624 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
7625 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
7626 WARNING_FOR_QUALIFIERS (flag_isoc23
,
7628 OPT_Wdiscarded_array_qualifiers
,
7629 G_("passing argument %d of %qE discards "
7630 "%qv qualifier from pointer target type"),
7631 G_("assignment discards %qv qualifier "
7632 "from pointer target type"),
7633 G_("initialization discards %qv qualifier "
7634 "from pointer target type"),
7635 G_("return discards %qv qualifier from "
7636 "pointer target type"),
7637 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
7640 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
7643 && !null_pointer_constant
7644 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
7645 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
7646 G_("ISO C forbids passing argument %d of "
7647 "%qE between function pointer "
7649 G_("ISO C forbids assignment between "
7650 "function pointer and %<void *%>"),
7651 G_("ISO C forbids initialization between "
7652 "function pointer and %<void *%>"),
7653 G_("ISO C forbids return between function "
7654 "pointer and %<void *%>"));
7655 /* Const and volatile mean something different for function types,
7656 so the usual warnings are not appropriate. */
7657 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
7658 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
7660 /* Assignments between atomic and non-atomic objects are OK. */
7661 bool warn_quals_ped
= TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
7662 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
);
7663 bool warn_quals
= TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
7664 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl
));
7666 /* Don't warn about loss of qualifier for conversions from
7667 qualified void* to pointers to arrays with corresponding
7668 qualifier on the element type (except for pedantic before C23). */
7669 if (warn_quals
|| (warn_quals_ped
&& pedantic
&& !flag_isoc23
))
7670 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
7671 OPT_Wdiscarded_qualifiers
,
7672 G_("passing argument %d of %qE discards "
7673 "%qv qualifier from pointer target type"),
7674 G_("assignment discards %qv qualifier "
7675 "from pointer target type"),
7676 G_("initialization discards %qv qualifier "
7677 "from pointer target type"),
7678 G_("return discards %qv qualifier from "
7679 "pointer target type"),
7680 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
7681 else if (warn_quals_ped
)
7682 pedwarn_c11 (location
, OPT_Wc11_c23_compat
,
7683 "array with qualifier on the element is not qualified before C23");
7685 /* If this is not a case of ignoring a mismatch in signedness,
7687 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
7690 /* If there is a mismatch, do warn. */
7691 else if (warn_pointer_sign
)
7696 auto_diagnostic_group d
;
7697 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7698 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7699 if (pedwarn (&richloc
, OPT_Wpointer_sign
,
7700 "pointer targets in passing argument %d of "
7701 "%qE differ in signedness", parmnum
, rname
))
7702 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
,
7707 pedwarn (location
, OPT_Wpointer_sign
,
7708 "pointer targets in assignment from %qT to %qT "
7709 "differ in signedness", rhstype
, type
);
7713 pedwarn_init (location
, OPT_Wpointer_sign
,
7714 "pointer targets in initialization of %qT "
7715 "from %qT differ in signedness", type
,
7719 pedwarn (location
, OPT_Wpointer_sign
, "pointer targets in "
7720 "returning %qT from a function with return type "
7721 "%qT differ in signedness", rhstype
, type
);
7727 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
7728 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
7730 /* Because const and volatile on functions are restrictions
7731 that say the function will not do certain things,
7732 it is okay to use a const or volatile function
7733 where an ordinary one is wanted, but not vice-versa. */
7734 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
7735 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
7736 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
7737 OPT_Wdiscarded_qualifiers
,
7738 G_("passing argument %d of %qE makes "
7739 "%q#v qualified function pointer "
7740 "from unqualified"),
7741 G_("assignment makes %q#v qualified function "
7742 "pointer from unqualified"),
7743 G_("initialization makes %q#v qualified "
7744 "function pointer from unqualified"),
7745 G_("return makes %q#v qualified function "
7746 "pointer from unqualified"),
7747 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
7750 /* Avoid warning about the volatile ObjC EH puts on decls. */
7757 auto_diagnostic_group d
;
7758 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7759 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7760 if (permerror_opt (&richloc
, OPT_Wincompatible_pointer_types
,
7761 "passing argument %d of %qE from "
7762 "incompatible pointer type",
7764 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7769 permerror_opt (location
, OPT_Wincompatible_pointer_types
,
7770 "assignment to %qT from pointer to "
7771 "%qD with incompatible type %qT",
7772 type
, bltin
, rhstype
);
7774 permerror_opt (location
, OPT_Wincompatible_pointer_types
,
7775 "assignment to %qT from incompatible pointer "
7776 "type %qT", type
, rhstype
);
7781 permerror_init (location
, OPT_Wincompatible_pointer_types
,
7782 "initialization of %qT from pointer to "
7783 "%qD with incompatible type %qT",
7784 type
, bltin
, rhstype
);
7786 permerror_init (location
, OPT_Wincompatible_pointer_types
,
7787 "initialization of %qT from incompatible "
7793 permerror_opt (location
, OPT_Wincompatible_pointer_types
,
7794 "returning pointer to %qD of type %qT from "
7795 "a function with incompatible type %qT",
7796 bltin
, rhstype
, type
);
7798 permerror_opt (location
, OPT_Wincompatible_pointer_types
,
7799 "returning %qT from a function with "
7800 "incompatible return type %qT", rhstype
, type
);
7807 /* If RHS isn't an address, check pointer or array of packed
7809 warn_for_address_of_packed_member (type
, orig_rhs
);
7811 return convert (type
, rhs
);
7813 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
7815 /* ??? This should not be an error when inlining calls to
7816 unprototyped functions. */
7817 const char msg
[] = "invalid use of non-lvalue array";
7819 warning_at (location
, warnopt
, msg
);
7821 error_at (location
, msg
);
7822 return error_mark_node
;
7824 else if (codel
== POINTER_TYPE
7825 && (coder
== INTEGER_TYPE
7826 || coder
== NULLPTR_TYPE
7827 || coder
== BITINT_TYPE
))
7829 /* An explicit constant 0 or type nullptr_t can convert to a pointer,
7830 or one that results from arithmetic, even including a cast to
7832 if (!null_pointer_constant
&& coder
!= NULLPTR_TYPE
)
7837 auto_diagnostic_group d
;
7838 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7839 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7840 if (permerror_opt (&richloc
, OPT_Wint_conversion
,
7841 "passing argument %d of %qE makes pointer "
7842 "from integer without a cast", parmnum
, rname
))
7843 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7847 permerror_opt (location
, OPT_Wint_conversion
,
7848 "assignment to %qT from %qT makes pointer from "
7849 "integer without a cast", type
, rhstype
);
7853 permerror_init (location
, OPT_Wint_conversion
,
7854 "initialization of %qT from %qT makes pointer "
7855 "from integer without a cast", type
, rhstype
);
7858 permerror_init (location
, OPT_Wint_conversion
,
7859 "returning %qT from a function with return type "
7860 "%qT makes pointer from integer without a cast",
7867 return convert (type
, rhs
);
7869 else if ((codel
== INTEGER_TYPE
|| codel
== BITINT_TYPE
)
7870 && coder
== POINTER_TYPE
)
7876 auto_diagnostic_group d
;
7877 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7878 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7879 if (permerror_opt (&richloc
, OPT_Wint_conversion
,
7880 "passing argument %d of %qE makes integer from "
7881 "pointer without a cast", parmnum
, rname
))
7882 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7886 permerror_opt (location
, OPT_Wint_conversion
,
7887 "assignment to %qT from %qT makes integer from "
7888 "pointer without a cast", type
, rhstype
);
7892 permerror_init (location
, OPT_Wint_conversion
,
7893 "initialization of %qT from %qT makes integer "
7894 "from pointer without a cast", type
, rhstype
);
7897 permerror_opt (location
, OPT_Wint_conversion
, "returning %qT from a "
7898 "function with return type %qT makes integer from "
7899 "pointer without a cast", rhstype
, type
);
7905 return convert (type
, rhs
);
7907 else if (C_BOOLEAN_TYPE_P (type
)
7908 /* The type nullptr_t may be converted to bool. The
7910 && (coder
== POINTER_TYPE
|| coder
== NULLPTR_TYPE
))
7913 bool save
= in_late_binary_op
;
7914 in_late_binary_op
= true;
7915 ret
= convert (type
, rhs
);
7916 in_late_binary_op
= save
;
7919 else if (codel
== NULLPTR_TYPE
&& null_pointer_constant
)
7920 return convert (type
, rhs
);
7926 auto_diagnostic_group d
;
7927 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7928 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7929 const char msg
[] = G_("incompatible type for argument %d of %qE");
7931 warning_at (expr_loc
, warnopt
, msg
, parmnum
, rname
);
7933 error_at (&richloc
, msg
, parmnum
, rname
);
7934 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7940 = G_("incompatible types when assigning to type %qT from type %qT");
7942 warning_at (expr_loc
, 0, msg
, type
, rhstype
);
7944 error_at (expr_loc
, msg
, type
, rhstype
);
7951 = G_("incompatible types when initializing type %qT using type %qT");
7953 warning_at (location
, 0, msg
, type
, rhstype
);
7955 error_at (location
, msg
, type
, rhstype
);
7961 = G_("incompatible types when returning type %qT but %qT was expected");
7963 warning_at (location
, 0, msg
, rhstype
, type
);
7965 error_at (location
, msg
, rhstype
, type
);
7972 return error_mark_node
;
7975 /* If VALUE is a compound expr all of whose expressions are constant, then
7976 return its value. Otherwise, return error_mark_node.
7978 This is for handling COMPOUND_EXPRs as initializer elements
7979 which is allowed with a warning when -pedantic is specified. */
7982 valid_compound_expr_initializer (tree value
, tree endtype
)
7984 if (TREE_CODE (value
) == COMPOUND_EXPR
)
7986 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
7988 return error_mark_node
;
7989 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
7992 else if (!initializer_constant_valid_p (value
, endtype
))
7993 return error_mark_node
;
7998 /* Perform appropriate conversions on the initial value of a variable,
7999 store it in the declaration DECL,
8000 and print any error messages that are appropriate.
8001 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8002 If the init is invalid, store an ERROR_MARK.
8004 INIT_LOC is the location of the initial value. */
8007 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
8011 bool int_const_expr
= false;
8012 bool arith_const_expr
= false;
8014 /* If variable's type was invalidly declared, just ignore it. */
8016 type
= TREE_TYPE (decl
);
8017 if (TREE_CODE (type
) == ERROR_MARK
)
8020 /* Digest the specified initializer into an expression. */
8024 npc
= null_pointer_constant_p (init
);
8025 int_const_expr
= (TREE_CODE (init
) == INTEGER_CST
8026 && !TREE_OVERFLOW (init
)
8027 && INTEGRAL_TYPE_P (TREE_TYPE (init
)));
8028 /* Not fully determined before folding. */
8029 arith_const_expr
= true;
8031 bool constexpr_p
= (VAR_P (decl
)
8032 && C_DECL_DECLARED_CONSTEXPR (decl
));
8033 value
= digest_init (init_loc
, type
, init
, origtype
, npc
, int_const_expr
,
8034 arith_const_expr
, true,
8035 TREE_STATIC (decl
) || constexpr_p
, constexpr_p
);
8037 /* Store the expression if valid; else report error. */
8039 if (!in_system_header_at (input_location
)
8040 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
8041 warning (OPT_Wtraditional
, "traditional C rejects automatic "
8042 "aggregate initialization");
8044 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
8045 DECL_INITIAL (decl
) = value
;
8047 /* ANSI wants warnings about out-of-range constant initializers. */
8048 STRIP_TYPE_NOPS (value
);
8049 if (TREE_STATIC (decl
))
8050 constant_expression_warning (value
);
8052 /* Check if we need to set array size from compound literal size. */
8053 if (TREE_CODE (type
) == ARRAY_TYPE
8054 && TYPE_DOMAIN (type
) == NULL_TREE
8055 && value
!= error_mark_node
)
8057 tree inside_init
= init
;
8059 STRIP_TYPE_NOPS (inside_init
);
8060 inside_init
= fold (inside_init
);
8062 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
8064 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
8066 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
8068 /* For int foo[] = (int [3]){1}; we need to set array size
8069 now since later on array initializer will be just the
8070 brace enclosed list of the compound literal. */
8071 tree etype
= strip_array_types (TREE_TYPE (decl
));
8072 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
8073 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
8075 layout_decl (cldecl
, 0);
8077 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
8083 /* Methods for storing and printing names for error messages. */
8085 /* Implement a spelling stack that allows components of a name to be pushed
8086 and popped. Each element on the stack is this structure. */
8093 unsigned HOST_WIDE_INT i
;
8098 #define SPELLING_STRING 1
8099 #define SPELLING_MEMBER 2
8100 #define SPELLING_BOUNDS 3
8102 static struct spelling
*spelling
; /* Next stack element (unused). */
8103 static struct spelling
*spelling_base
; /* Spelling stack base. */
8104 static int spelling_size
; /* Size of the spelling stack. */
8106 /* Macros to save and restore the spelling stack around push_... functions.
8107 Alternative to SAVE_SPELLING_STACK. */
8109 #define SPELLING_DEPTH() (spelling - spelling_base)
8110 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
8112 /* Push an element on the spelling stack with type KIND and assign VALUE
8115 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
8117 int depth = SPELLING_DEPTH (); \
8119 if (depth >= spelling_size) \
8121 spelling_size += 10; \
8122 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
8124 RESTORE_SPELLING_DEPTH (depth); \
8127 spelling->kind = (KIND); \
8128 spelling->MEMBER = (VALUE); \
8132 /* Push STRING on the stack. Printed literally. */
8135 push_string (const char *string
)
8137 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
8140 /* Push a member name on the stack. Printed as '.' STRING. */
8143 push_member_name (tree decl
)
8145 const char *const string
8147 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
8148 : _("<anonymous>"));
8149 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
8152 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
8155 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
8157 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
8160 /* Compute the maximum size in bytes of the printed spelling. */
8163 spelling_length (void)
8168 for (p
= spelling_base
; p
< spelling
; p
++)
8170 if (p
->kind
== SPELLING_BOUNDS
)
8173 size
+= strlen (p
->u
.s
) + 1;
8179 /* Print the spelling to BUFFER and return it. */
8182 print_spelling (char *buffer
)
8187 for (p
= spelling_base
; p
< spelling
; p
++)
8188 if (p
->kind
== SPELLING_BOUNDS
)
8190 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
8196 if (p
->kind
== SPELLING_MEMBER
)
8198 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
8205 /* Check whether INIT, a floating or integer constant, is
8206 representable in TYPE, a real floating type with the same radix or
8207 a decimal floating type initialized with a binary floating
8208 constant. Return true if OK, false if not. */
8210 constexpr_init_fits_real_type (tree type
, tree init
)
8212 gcc_assert (SCALAR_FLOAT_TYPE_P (type
));
8213 gcc_assert (TREE_CODE (init
) == INTEGER_CST
|| TREE_CODE (init
) == REAL_CST
);
8214 if (TREE_CODE (init
) == REAL_CST
8215 && TYPE_MODE (TREE_TYPE (init
)) == TYPE_MODE (type
))
8217 /* Same mode, no conversion required except for the case of
8218 signaling NaNs if the types are incompatible (e.g. double and
8219 long double with the same mode). */
8220 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init
))
8221 && !comptypes (TYPE_MAIN_VARIANT (type
),
8222 TYPE_MAIN_VARIANT (TREE_TYPE (init
))))
8226 if (TREE_CODE (init
) == INTEGER_CST
)
8228 tree converted
= build_real_from_int_cst (type
, init
);
8230 wide_int w
= real_to_integer (&TREE_REAL_CST (converted
), &fail
,
8231 TYPE_PRECISION (TREE_TYPE (init
)));
8232 return !fail
&& wi::eq_p (w
, wi::to_wide (init
));
8234 if (REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (init
)))
8236 if ((REAL_VALUE_ISINF (TREE_REAL_CST (init
))
8237 && MODE_HAS_INFINITIES (TYPE_MODE (type
)))
8238 || (REAL_VALUE_ISNAN (TREE_REAL_CST (init
))
8239 && MODE_HAS_NANS (TYPE_MODE (type
))))
8241 if (DECIMAL_FLOAT_TYPE_P (type
)
8242 && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init
)))
8244 /* This is valid if the real number represented by the
8245 initializer can be exactly represented in the decimal
8246 type. Compare the values using MPFR. */
8248 real_convert (&t
, TYPE_MODE (type
), &TREE_REAL_CST (init
));
8249 mpfr_t bin_val
, dec_val
;
8250 mpfr_init2 (bin_val
, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init
)))->p
);
8251 mpfr_init2 (dec_val
, REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (init
)))->p
);
8252 mpfr_from_real (bin_val
, &TREE_REAL_CST (init
), MPFR_RNDN
);
8254 real_to_decimal (string
, &t
, sizeof string
, 0, 1);
8255 bool res
= (mpfr_strtofr (dec_val
, string
, NULL
, 10, MPFR_RNDN
) == 0
8256 && mpfr_equal_p (bin_val
, dec_val
));
8257 mpfr_clear (bin_val
);
8258 mpfr_clear (dec_val
);
8261 /* exact_real_truncate is not quite right here, since it doesn't
8262 allow even an exact conversion to subnormal values. */
8264 real_convert (&t
, TYPE_MODE (type
), &TREE_REAL_CST (init
));
8265 return real_identical (&t
, &TREE_REAL_CST (init
));
8268 /* Check whether INIT (location LOC) is valid as a 'constexpr'
8269 initializer for type TYPE, and give an error if not. INIT has
8270 already been folded and verified to be constant. INT_CONST_EXPR
8271 and ARITH_CONST_EXPR say whether it is an integer constant
8272 expression or arithmetic constant expression, respectively. If
8273 TYPE is not a scalar type, this function does nothing. */
8276 check_constexpr_init (location_t loc
, tree type
, tree init
,
8277 bool int_const_expr
, bool arith_const_expr
)
8279 if (POINTER_TYPE_P (type
))
8281 /* The initializer must be null. */
8282 if (TREE_CODE (init
) != INTEGER_CST
|| !integer_zerop (init
))
8283 error_at (loc
, "%<constexpr%> pointer initializer is not null");
8286 if (INTEGRAL_TYPE_P (type
))
8288 /* The initializer must be an integer constant expression,
8289 representable in the target type. */
8290 if (!int_const_expr
)
8291 error_at (loc
, "%<constexpr%> integer initializer is not an "
8292 "integer constant expression");
8293 if (!int_fits_type_p (init
, type
))
8294 error_at (loc
, "%<constexpr%> initializer not representable in "
8298 /* We don't apply any extra checks to extension types such as vector
8299 or fixed-point types. */
8300 if (TREE_CODE (type
) != REAL_TYPE
&& TREE_CODE (type
) != COMPLEX_TYPE
)
8302 if (!arith_const_expr
)
8304 error_at (loc
, "%<constexpr%> initializer is not an arithmetic "
8305 "constant expression");
8308 /* We don't apply any extra checks to complex integers. */
8309 if (TREE_CODE (type
) == COMPLEX_TYPE
8310 && TREE_CODE (TREE_TYPE (type
)) != REAL_TYPE
)
8312 /* Following N3082, a real type cannot be initialized from a complex
8313 type and a binary type cannot be initialized from a decimal type
8314 (but initializing a decimal type from a binary type is OK).
8315 Signaling NaN initializers are OK only if the types are
8316 compatible (not just the same mode); all quiet NaN and infinity
8317 initializations are considered to preserve the value. */
8318 if (TREE_CODE (TREE_TYPE (init
)) == COMPLEX_TYPE
8319 && SCALAR_FLOAT_TYPE_P (type
))
8321 error_at (loc
, "%<constexpr%> initializer for a real type is of "
8325 if (SCALAR_FLOAT_TYPE_P (type
)
8326 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (init
))
8327 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init
))
8328 && !DECIMAL_FLOAT_TYPE_P (type
))
8330 error_at (loc
, "%<constexpr%> initializer for a binary "
8331 "floating-point type is of decimal type");
8335 if (TREE_CODE (type
) == COMPLEX_TYPE
)
8337 switch (TREE_CODE (init
))
8341 fits
= constexpr_init_fits_real_type (TREE_TYPE (type
), init
);
8344 fits
= (constexpr_init_fits_real_type (TREE_TYPE (type
),
8345 TREE_REALPART (init
))
8346 && constexpr_init_fits_real_type (TREE_TYPE (type
),
8347 TREE_IMAGPART (init
)));
8354 fits
= constexpr_init_fits_real_type (type
, init
);
8356 error_at (loc
, "%<constexpr%> initializer not representable in "
8360 /* Digest the parser output INIT as an initializer for type TYPE.
8361 Return a C expression of type TYPE to represent the initial value.
8363 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8365 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
8366 INT_CONST_EXPR is true if INIT is an integer constant expression,
8367 and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
8368 constant expression, false if it has already been determined in the
8369 caller that it is not (but folding may have made the value passed here
8370 indistinguishable from an arithmetic constant expression).
8372 If INIT is a string constant, STRICT_STRING is true if it is
8373 unparenthesized or we should not warn here for it being parenthesized.
8374 For other types of INIT, STRICT_STRING is not used.
8376 INIT_LOC is the location of the INIT.
8378 REQUIRE_CONSTANT requests an error if non-constant initializers or
8379 elements are seen. REQUIRE_CONSTEXPR means the stricter requirements
8380 on initializers for 'constexpr' objects apply. */
8383 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
8384 bool null_pointer_constant
, bool int_const_expr
,
8385 bool arith_const_expr
, bool strict_string
,
8386 bool require_constant
, bool require_constexpr
)
8388 enum tree_code code
= TREE_CODE (type
);
8389 tree inside_init
= init
;
8390 tree semantic_type
= NULL_TREE
;
8391 bool maybe_const
= true;
8393 if (type
== error_mark_node
8395 || error_operand_p (init
))
8396 return error_mark_node
;
8398 STRIP_TYPE_NOPS (inside_init
);
8402 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
8404 semantic_type
= TREE_TYPE (inside_init
);
8405 inside_init
= TREE_OPERAND (inside_init
, 0);
8407 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
8409 /* TODO: this may not detect all cases of expressions folding to
8410 constants that are not arithmetic constant expressions. */
8412 arith_const_expr
= false;
8413 else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init
))
8414 && TREE_CODE (TREE_TYPE (inside_init
)) != REAL_TYPE
8415 && TREE_CODE (TREE_TYPE (inside_init
)) != COMPLEX_TYPE
)
8416 arith_const_expr
= false;
8417 else if (TREE_CODE (inside_init
) != INTEGER_CST
8418 && TREE_CODE (inside_init
) != REAL_CST
8419 && TREE_CODE (inside_init
) != COMPLEX_CST
)
8420 arith_const_expr
= false;
8421 else if (TREE_OVERFLOW (inside_init
))
8422 arith_const_expr
= false;
8424 /* Initialization of an array of chars from a string constant
8425 optionally enclosed in braces. */
8427 if (code
== ARRAY_TYPE
&& inside_init
8428 && TREE_CODE (inside_init
) == STRING_CST
)
8431 = (TYPE_ATOMIC (TREE_TYPE (type
))
8432 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
8434 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
8435 /* Note that an array could be both an array of character type
8436 and an array of wchar_t if wchar_t is signed char or unsigned
8438 bool char_array
= (typ1
== char_type_node
8439 || typ1
== signed_char_type_node
8440 || typ1
== unsigned_char_type_node
);
8441 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
8442 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
8443 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
8445 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
8448 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
8449 bool incompat_string_cst
= false;
8450 expr
.value
= inside_init
;
8451 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
8452 expr
.original_type
= NULL
;
8454 maybe_warn_string_init (init_loc
, type
, expr
);
8456 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
8457 pedwarn_init (init_loc
, OPT_Wpedantic
,
8458 "initialization of a flexible array member");
8460 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
8461 TYPE_MAIN_VARIANT (type
)))
8466 if (typ2
!= char_type_node
&& typ2
!= char8_type_node
)
8467 incompat_string_cst
= true;
8469 else if (!comptypes (typ1
, typ2
))
8470 incompat_string_cst
= true;
8472 if (incompat_string_cst
)
8474 error_init (init_loc
, "cannot initialize array of %qT from "
8475 "a string literal with type array of %qT",
8477 return error_mark_node
;
8480 if (require_constexpr
8481 && TYPE_UNSIGNED (typ1
) != TYPE_UNSIGNED (typ2
))
8483 /* Check if all characters of the string can be
8484 represented in the type of the constexpr object being
8486 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
8487 const unsigned char *p
=
8488 (const unsigned char *) TREE_STRING_POINTER (inside_init
);
8489 gcc_assert (CHAR_TYPE_SIZE
== 8 && CHAR_BIT
== 8);
8490 for (unsigned i
= 0; i
< len
; i
++)
8493 error_init (init_loc
, "%<constexpr%> initializer not "
8494 "representable in type of object");
8499 if (TYPE_DOMAIN (type
) != NULL_TREE
8500 && TYPE_SIZE (type
) != NULL_TREE
8501 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
8503 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
8504 unsigned unit
= TYPE_PRECISION (typ1
) / BITS_PER_UNIT
;
8506 /* Subtract the size of a single (possibly wide) character
8507 because it's ok to ignore the terminating null char
8508 that is counted in the length of the constant. */
8509 if (compare_tree_int (TYPE_SIZE_UNIT (type
), len
- unit
) < 0)
8510 pedwarn_init (init_loc
, 0,
8511 ("initializer-string for array of %qT "
8512 "is too long"), typ1
);
8513 else if (warn_cxx_compat
8514 && compare_tree_int (TYPE_SIZE_UNIT (type
), len
) < 0)
8515 warning_at (init_loc
, OPT_Wc___compat
,
8516 ("initializer-string for array of %qT "
8517 "is too long for C++"), typ1
);
8518 if (compare_tree_int (TYPE_SIZE_UNIT (type
), len
) < 0)
8520 unsigned HOST_WIDE_INT size
8521 = tree_to_uhwi (TYPE_SIZE_UNIT (type
));
8522 const char *p
= TREE_STRING_POINTER (inside_init
);
8524 inside_init
= build_string (size
, p
);
8528 TREE_TYPE (inside_init
) = type
;
8531 else if (INTEGRAL_TYPE_P (typ1
))
8533 error_init (init_loc
, "array of inappropriate type initialized "
8534 "from string constant");
8535 return error_mark_node
;
8539 /* Build a VECTOR_CST from a *constant* vector constructor. If the
8540 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8541 below and handle as a constructor. */
8542 if (code
== VECTOR_TYPE
8543 && VECTOR_TYPE_P (TREE_TYPE (inside_init
))
8544 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
8545 && TREE_CONSTANT (inside_init
))
8547 if (TREE_CODE (inside_init
) == VECTOR_CST
8548 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
8549 TYPE_MAIN_VARIANT (type
)))
8552 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
8554 unsigned HOST_WIDE_INT ix
;
8556 bool constant_p
= true;
8558 /* Iterate through elements and check if all constructor
8559 elements are *_CSTs. */
8560 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
8561 if (!CONSTANT_CLASS_P (value
))
8568 return build_vector_from_ctor (type
,
8569 CONSTRUCTOR_ELTS (inside_init
));
8573 if (warn_sequence_point
)
8574 verify_sequence_points (inside_init
);
8576 /* Any type can be initialized
8577 from an expression of the same type, optionally with braces. */
8579 if (inside_init
&& TREE_TYPE (inside_init
) != NULL_TREE
8580 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
8581 TYPE_MAIN_VARIANT (type
))
8582 || (code
== ARRAY_TYPE
8583 && comptypes (TREE_TYPE (inside_init
), type
))
8584 || (gnu_vector_type_p (type
)
8585 && comptypes (TREE_TYPE (inside_init
), type
))
8586 || (code
== POINTER_TYPE
8587 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
8588 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
8589 TREE_TYPE (type
)))))
8591 if (code
== POINTER_TYPE
)
8593 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
8595 if (TREE_CODE (inside_init
) == STRING_CST
8596 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
8597 inside_init
= array_to_pointer_conversion
8598 (init_loc
, inside_init
);
8601 error_init (init_loc
, "invalid use of non-lvalue array");
8602 return error_mark_node
;
8607 if (code
== VECTOR_TYPE
|| c_hardbool_type_attr (type
))
8608 /* Although the types are compatible, we may require a
8610 inside_init
= convert (type
, inside_init
);
8612 if ((code
== RECORD_TYPE
|| code
== UNION_TYPE
)
8613 && !comptypes (TYPE_MAIN_VARIANT (type
), TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
))))
8615 error_init (init_loc
, "invalid initializer");
8616 return error_mark_node
;
8619 if (require_constant
8620 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
8622 /* As an extension, allow initializing objects with static storage
8623 duration with compound literals (which are then treated just as
8624 the brace enclosed list they contain). Also allow this for
8625 vectors, as we can only assign them with compound literals. */
8626 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
8627 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
8629 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
8630 inside_init
= DECL_INITIAL (decl
);
8633 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
8634 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
8636 error_init (init_loc
, "array initialized from non-constant array "
8638 return error_mark_node
;
8641 /* Compound expressions can only occur here if -Wpedantic or
8642 -pedantic-errors is specified. In the later case, we always want
8643 an error. In the former case, we simply want a warning. */
8644 if (require_constant
&& pedantic
8645 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
8648 = valid_compound_expr_initializer (inside_init
,
8649 TREE_TYPE (inside_init
));
8650 if (inside_init
== error_mark_node
)
8651 error_init (init_loc
, "initializer element is not constant");
8653 pedwarn_init (init_loc
, OPT_Wpedantic
,
8654 "initializer element is not constant");
8655 if (flag_pedantic_errors
)
8656 inside_init
= error_mark_node
;
8658 else if (require_constant
8659 && !initializer_constant_valid_p (inside_init
,
8660 TREE_TYPE (inside_init
)))
8662 error_init (init_loc
, "initializer element is not constant");
8663 inside_init
= error_mark_node
;
8665 else if (require_constant
&& !maybe_const
)
8666 pedwarn_init (init_loc
, OPT_Wpedantic
,
8667 "initializer element is not a constant expression");
8668 else if (require_constexpr
)
8669 check_constexpr_init (init_loc
, type
, inside_init
,
8670 int_const_expr
, arith_const_expr
);
8672 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8673 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
8674 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
8675 type
, inside_init
, origtype
,
8678 : ic_init
), null_pointer_constant
,
8679 NULL_TREE
, NULL_TREE
, 0);
8683 /* Handle scalar types, including conversions. */
8685 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
8686 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
8687 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
|| code
== NULLPTR_TYPE
8688 || code
== BITINT_TYPE
)
8690 tree unconverted_init
= inside_init
;
8691 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
8692 && (TREE_CODE (init
) == STRING_CST
8693 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
8694 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
8696 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
8699 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
8700 inside_init
, origtype
,
8701 require_constant
? ic_init_const
: ic_init
,
8702 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
8705 /* Check to see if we have already given an error message. */
8706 if (inside_init
== error_mark_node
)
8708 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
8710 error_init (init_loc
, "initializer element is not constant");
8711 inside_init
= error_mark_node
;
8713 else if (require_constant
8714 && !initializer_constant_valid_p (inside_init
,
8715 TREE_TYPE (inside_init
)))
8717 error_init (init_loc
, "initializer element is not computable at "
8719 inside_init
= error_mark_node
;
8721 else if (require_constant
&& !maybe_const
)
8722 pedwarn_init (init_loc
, OPT_Wpedantic
,
8723 "initializer element is not a constant expression");
8724 else if (require_constexpr
)
8725 check_constexpr_init (init_loc
, type
, unconverted_init
,
8726 int_const_expr
, arith_const_expr
);
8731 /* Come here only for records and arrays. */
8733 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
8735 error_init (init_loc
,
8736 "variable-sized object may not be initialized except "
8737 "with an empty initializer");
8738 return error_mark_node
;
8741 error_init (init_loc
, "invalid initializer");
8742 return error_mark_node
;
8745 /* Handle initializers that use braces. */
8747 /* Type of object we are accumulating a constructor for.
8748 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8749 static tree constructor_type
;
8751 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8753 static tree constructor_fields
;
8755 /* For an ARRAY_TYPE, this is the specified index
8756 at which to store the next element we get. */
8757 static tree constructor_index
;
8759 /* For an ARRAY_TYPE, this is the maximum index. */
8760 static tree constructor_max_index
;
8762 /* For a RECORD_TYPE, this is the first field not yet written out. */
8763 static tree constructor_unfilled_fields
;
8765 /* For an ARRAY_TYPE, this is the index of the first element
8766 not yet written out. */
8767 static tree constructor_unfilled_index
;
8769 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8770 This is so we can generate gaps between fields, when appropriate. */
8771 static tree constructor_bit_index
;
8773 /* If we are saving up the elements rather than allocating them,
8774 this is the list of elements so far (in reverse order,
8775 most recent first). */
8776 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
8778 /* 1 if constructor should be incrementally stored into a constructor chain,
8779 0 if all the elements should be kept in AVL tree. */
8780 static int constructor_incremental
;
8782 /* 1 if so far this constructor's elements are all compile-time constants. */
8783 static int constructor_constant
;
8785 /* 1 if so far this constructor's elements are all valid address constants. */
8786 static int constructor_simple
;
8788 /* 1 if this constructor has an element that cannot be part of a
8789 constant expression. */
8790 static int constructor_nonconst
;
8792 /* 1 if this constructor is erroneous so far. */
8793 static int constructor_erroneous
;
8795 /* 1 if this constructor is the universal zero initializer { 0 }. */
8796 static int constructor_zeroinit
;
8798 /* Structure for managing pending initializer elements, organized as an
8803 struct init_node
*left
, *right
;
8804 struct init_node
*parent
;
8811 /* Tree of pending elements at this constructor level.
8812 These are elements encountered out of order
8813 which belong at places we haven't reached yet in actually
8815 Will never hold tree nodes across GC runs. */
8816 static struct init_node
*constructor_pending_elts
;
8818 /* The SPELLING_DEPTH of this constructor. */
8819 static int constructor_depth
;
8821 /* DECL node for which an initializer is being read.
8822 0 means we are reading a constructor expression
8823 such as (struct foo) {...}. */
8824 static tree constructor_decl
;
8826 /* Nonzero if there were any member designators in this initializer. */
8827 static int constructor_designated
;
8829 /* Nesting depth of designator list. */
8830 static int designator_depth
;
8832 /* Nonzero if there were diagnosed errors in this designator list. */
8833 static int designator_erroneous
;
8836 /* This stack has a level for each implicit or explicit level of
8837 structuring in the initializer, including the outermost one. It
8838 saves the values of most of the variables above. */
8840 struct constructor_range_stack
;
8842 struct constructor_stack
8844 struct constructor_stack
*next
;
8849 tree unfilled_index
;
8850 tree unfilled_fields
;
8852 vec
<constructor_elt
, va_gc
> *elements
;
8853 struct init_node
*pending_elts
;
8856 /* If value nonzero, this value should replace the entire
8857 constructor at this level. */
8858 struct c_expr replacement_value
;
8859 struct constructor_range_stack
*range_stack
;
8868 int designator_depth
;
8871 static struct constructor_stack
*constructor_stack
;
8873 /* This stack represents designators from some range designator up to
8874 the last designator in the list. */
8876 struct constructor_range_stack
8878 struct constructor_range_stack
*next
, *prev
;
8879 struct constructor_stack
*stack
;
8886 static struct constructor_range_stack
*constructor_range_stack
;
8888 /* This stack records separate initializers that are nested.
8889 Nested initializers can't happen in ANSI C, but GNU C allows them
8890 in cases like { ... (struct foo) { ... } ... }. */
8892 struct initializer_stack
8894 struct initializer_stack
*next
;
8896 struct constructor_stack
*constructor_stack
;
8897 struct constructor_range_stack
*constructor_range_stack
;
8898 vec
<constructor_elt
, va_gc
> *elements
;
8899 struct spelling
*spelling
;
8900 struct spelling
*spelling_base
;
8902 char require_constant_value
;
8903 char require_constant_elements
;
8904 char require_constexpr_value
;
8906 rich_location
*missing_brace_richloc
;
8909 static struct initializer_stack
*initializer_stack
;
8911 /* Prepare to parse and output the initializer for variable DECL. */
8914 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
,
8915 bool init_require_constant
, bool init_require_constexpr
,
8916 rich_location
*richloc
)
8919 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
8921 p
->decl
= constructor_decl
;
8922 p
->require_constant_value
= require_constant_value
;
8923 p
->require_constant_elements
= require_constant_elements
;
8924 p
->require_constexpr_value
= require_constexpr_value
;
8925 p
->constructor_stack
= constructor_stack
;
8926 p
->constructor_range_stack
= constructor_range_stack
;
8927 p
->elements
= constructor_elements
;
8928 p
->spelling
= spelling
;
8929 p
->spelling_base
= spelling_base
;
8930 p
->spelling_size
= spelling_size
;
8931 p
->next
= initializer_stack
;
8932 p
->missing_brace_richloc
= richloc
;
8933 p
->designated
= constructor_designated
;
8934 initializer_stack
= p
;
8936 constructor_decl
= decl
;
8937 constructor_designated
= 0;
8939 require_constant_value
= init_require_constant
;
8940 require_constexpr_value
= init_require_constexpr
;
8941 if (decl
!= NULL_TREE
&& decl
!= error_mark_node
)
8943 require_constant_elements
8944 = ((init_require_constant
|| (pedantic
&& !flag_isoc99
))
8945 /* For a scalar, you can always use any value to initialize,
8946 even within braces. */
8947 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)));
8948 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
8952 require_constant_elements
= false;
8953 locus
= _("(anonymous)");
8956 constructor_stack
= 0;
8957 constructor_range_stack
= 0;
8959 found_missing_braces
= 0;
8963 RESTORE_SPELLING_DEPTH (0);
8966 push_string (locus
);
8972 struct initializer_stack
*p
= initializer_stack
;
8974 /* Free the whole constructor stack of this initializer. */
8975 while (constructor_stack
)
8977 struct constructor_stack
*q
= constructor_stack
;
8978 constructor_stack
= q
->next
;
8982 gcc_assert (!constructor_range_stack
);
8984 /* Pop back to the data of the outer initializer (if any). */
8985 XDELETE (spelling_base
);
8987 constructor_decl
= p
->decl
;
8988 require_constant_value
= p
->require_constant_value
;
8989 require_constant_elements
= p
->require_constant_elements
;
8990 require_constexpr_value
= p
->require_constexpr_value
;
8991 constructor_stack
= p
->constructor_stack
;
8992 constructor_designated
= p
->designated
;
8993 constructor_range_stack
= p
->constructor_range_stack
;
8994 constructor_elements
= p
->elements
;
8995 spelling
= p
->spelling
;
8996 spelling_base
= p
->spelling_base
;
8997 spelling_size
= p
->spelling_size
;
8998 initializer_stack
= p
->next
;
9002 /* Call here when we see the initializer is surrounded by braces.
9003 This is instead of a call to push_init_level;
9004 it is matched by a call to pop_init_level.
9006 TYPE is the type to initialize, for a constructor expression.
9007 For an initializer for a decl, TYPE is zero. */
9010 really_start_incremental_init (tree type
)
9012 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
9014 if (type
== NULL_TREE
)
9015 type
= TREE_TYPE (constructor_decl
);
9017 if (VECTOR_TYPE_P (type
)
9018 && TYPE_VECTOR_OPAQUE (type
))
9019 error ("opaque vector types cannot be initialized");
9021 p
->type
= constructor_type
;
9022 p
->fields
= constructor_fields
;
9023 p
->index
= constructor_index
;
9024 p
->max_index
= constructor_max_index
;
9025 p
->unfilled_index
= constructor_unfilled_index
;
9026 p
->unfilled_fields
= constructor_unfilled_fields
;
9027 p
->bit_index
= constructor_bit_index
;
9028 p
->elements
= constructor_elements
;
9029 p
->constant
= constructor_constant
;
9030 p
->simple
= constructor_simple
;
9031 p
->nonconst
= constructor_nonconst
;
9032 p
->erroneous
= constructor_erroneous
;
9033 p
->pending_elts
= constructor_pending_elts
;
9034 p
->depth
= constructor_depth
;
9035 p
->replacement_value
.value
= 0;
9036 p
->replacement_value
.original_code
= ERROR_MARK
;
9037 p
->replacement_value
.original_type
= NULL
;
9041 p
->incremental
= constructor_incremental
;
9042 p
->designated
= constructor_designated
;
9043 p
->designator_depth
= designator_depth
;
9045 constructor_stack
= p
;
9047 constructor_constant
= 1;
9048 constructor_simple
= 1;
9049 constructor_nonconst
= 0;
9050 constructor_depth
= SPELLING_DEPTH ();
9051 constructor_elements
= NULL
;
9052 constructor_pending_elts
= 0;
9053 constructor_type
= type
;
9054 constructor_incremental
= 1;
9055 constructor_designated
= 0;
9056 constructor_zeroinit
= 1;
9057 designator_depth
= 0;
9058 designator_erroneous
= 0;
9060 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9062 constructor_fields
= TYPE_FIELDS (constructor_type
);
9063 /* Skip any nameless bit fields at the beginning. */
9064 while (constructor_fields
!= NULL_TREE
9065 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
9066 constructor_fields
= DECL_CHAIN (constructor_fields
);
9068 constructor_unfilled_fields
= constructor_fields
;
9069 constructor_bit_index
= bitsize_zero_node
;
9071 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9073 if (TYPE_DOMAIN (constructor_type
))
9075 constructor_max_index
9076 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
9078 /* Detect non-empty initializations of zero-length arrays. */
9079 if (constructor_max_index
== NULL_TREE
9080 && TYPE_SIZE (constructor_type
))
9081 constructor_max_index
= integer_minus_one_node
;
9083 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9084 to initialize VLAs with a nonempty initializer will cause a
9085 proper error; avoid tree checking errors as well by setting a
9087 if (constructor_max_index
9088 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
9089 constructor_max_index
= integer_minus_one_node
;
9092 = convert (bitsizetype
,
9093 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
9097 constructor_index
= bitsize_zero_node
;
9098 constructor_max_index
= NULL_TREE
;
9101 constructor_unfilled_index
= constructor_index
;
9103 else if (gnu_vector_type_p (constructor_type
))
9105 /* Vectors are like simple fixed-size arrays. */
9106 constructor_max_index
=
9107 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
9108 constructor_index
= bitsize_zero_node
;
9109 constructor_unfilled_index
= constructor_index
;
9113 /* Handle the case of int x = {5}; */
9114 constructor_fields
= constructor_type
;
9115 constructor_unfilled_fields
= constructor_type
;
9119 extern location_t last_init_list_comma
;
9121 /* Called when we see an open brace for a nested initializer. Finish
9122 off any pending levels with implicit braces. */
9124 finish_implicit_inits (location_t loc
, struct obstack
*braced_init_obstack
)
9126 while (constructor_stack
->implicit
)
9128 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
9129 && constructor_fields
== NULL_TREE
)
9130 process_init_element (input_location
,
9131 pop_init_level (loc
, 1, braced_init_obstack
,
9132 last_init_list_comma
),
9133 true, braced_init_obstack
);
9134 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
9135 && constructor_max_index
9136 && tree_int_cst_lt (constructor_max_index
,
9138 process_init_element (input_location
,
9139 pop_init_level (loc
, 1, braced_init_obstack
,
9140 last_init_list_comma
),
9141 true, braced_init_obstack
);
9147 /* Push down into a subobject, for initialization.
9148 If this is for an explicit set of braces, IMPLICIT is 0.
9149 If it is because the next element belongs at a lower level,
9150 IMPLICIT is 1 (or 2 if the push is because of designator list). */
9153 push_init_level (location_t loc
, int implicit
,
9154 struct obstack
*braced_init_obstack
)
9156 struct constructor_stack
*p
;
9157 tree value
= NULL_TREE
;
9159 /* Unless this is an explicit brace, we need to preserve previous
9163 if (RECORD_OR_UNION_TYPE_P (constructor_type
) && constructor_fields
)
9164 value
= find_init_member (constructor_fields
, braced_init_obstack
);
9165 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9166 value
= find_init_member (constructor_index
, braced_init_obstack
);
9169 p
= XNEW (struct constructor_stack
);
9170 p
->type
= constructor_type
;
9171 p
->fields
= constructor_fields
;
9172 p
->index
= constructor_index
;
9173 p
->max_index
= constructor_max_index
;
9174 p
->unfilled_index
= constructor_unfilled_index
;
9175 p
->unfilled_fields
= constructor_unfilled_fields
;
9176 p
->bit_index
= constructor_bit_index
;
9177 p
->elements
= constructor_elements
;
9178 p
->constant
= constructor_constant
;
9179 p
->simple
= constructor_simple
;
9180 p
->nonconst
= constructor_nonconst
;
9181 p
->erroneous
= constructor_erroneous
;
9182 p
->pending_elts
= constructor_pending_elts
;
9183 p
->depth
= constructor_depth
;
9184 p
->replacement_value
.value
= NULL_TREE
;
9185 p
->replacement_value
.original_code
= ERROR_MARK
;
9186 p
->replacement_value
.original_type
= NULL
;
9187 p
->implicit
= implicit
;
9189 p
->incremental
= constructor_incremental
;
9190 p
->designated
= constructor_designated
;
9191 p
->designator_depth
= designator_depth
;
9192 p
->next
= constructor_stack
;
9194 constructor_stack
= p
;
9196 constructor_constant
= 1;
9197 constructor_simple
= 1;
9198 constructor_nonconst
= 0;
9199 constructor_depth
= SPELLING_DEPTH ();
9200 constructor_elements
= NULL
;
9201 constructor_incremental
= 1;
9202 /* If the upper initializer is designated, then mark this as
9203 designated too to prevent bogus warnings. */
9204 constructor_designated
= p
->designated
;
9205 constructor_pending_elts
= 0;
9208 p
->range_stack
= constructor_range_stack
;
9209 constructor_range_stack
= 0;
9210 designator_depth
= 0;
9211 designator_erroneous
= 0;
9214 /* Don't die if an entire brace-pair level is superfluous
9215 in the containing level. */
9216 if (constructor_type
== NULL_TREE
)
9218 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9220 /* Don't die if there are extra init elts at the end. */
9221 if (constructor_fields
== NULL_TREE
)
9222 constructor_type
= NULL_TREE
;
9225 constructor_type
= TREE_TYPE (constructor_fields
);
9226 push_member_name (constructor_fields
);
9227 constructor_depth
++;
9230 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9232 constructor_type
= TREE_TYPE (constructor_type
);
9233 push_array_bounds (tree_to_uhwi (constructor_index
));
9234 constructor_depth
++;
9237 if (constructor_type
== NULL_TREE
)
9239 error_init (loc
, "extra brace group at end of initializer");
9240 constructor_fields
= NULL_TREE
;
9241 constructor_unfilled_fields
= NULL_TREE
;
9245 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
9247 constructor_constant
= TREE_CONSTANT (value
);
9248 constructor_simple
= TREE_STATIC (value
);
9249 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
9250 constructor_elements
= CONSTRUCTOR_ELTS (value
);
9251 if (!vec_safe_is_empty (constructor_elements
)
9252 && (TREE_CODE (constructor_type
) == RECORD_TYPE
9253 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
9254 set_nonincremental_init (braced_init_obstack
);
9259 found_missing_braces
= 1;
9260 if (initializer_stack
->missing_brace_richloc
)
9261 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
9265 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9267 constructor_fields
= TYPE_FIELDS (constructor_type
);
9268 /* Skip any nameless bit fields at the beginning. */
9269 while (constructor_fields
!= NULL_TREE
9270 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
9271 constructor_fields
= DECL_CHAIN (constructor_fields
);
9273 constructor_unfilled_fields
= constructor_fields
;
9274 constructor_bit_index
= bitsize_zero_node
;
9276 else if (gnu_vector_type_p (constructor_type
))
9278 /* Vectors are like simple fixed-size arrays. */
9279 constructor_max_index
=
9280 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
9281 constructor_index
= bitsize_int (0);
9282 constructor_unfilled_index
= constructor_index
;
9284 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9286 if (TYPE_DOMAIN (constructor_type
))
9288 constructor_max_index
9289 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
9291 /* Detect non-empty initializations of zero-length arrays. */
9292 if (constructor_max_index
== NULL_TREE
9293 && TYPE_SIZE (constructor_type
))
9294 constructor_max_index
= integer_minus_one_node
;
9296 /* constructor_max_index needs to be an INTEGER_CST. Attempts
9297 to initialize VLAs will cause a proper error; avoid tree
9298 checking errors as well by setting a safe value. */
9299 if (constructor_max_index
9300 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
9301 constructor_max_index
= integer_minus_one_node
;
9304 = convert (bitsizetype
,
9305 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
9308 constructor_index
= bitsize_zero_node
;
9310 constructor_unfilled_index
= constructor_index
;
9311 if (value
&& TREE_CODE (value
) == STRING_CST
)
9313 /* We need to split the char/wchar array into individual
9314 characters, so that we don't have to special case it
9316 set_nonincremental_init_from_string (value
, braced_init_obstack
);
9321 if (constructor_type
!= error_mark_node
)
9322 warning_init (input_location
, 0, "braces around scalar initializer");
9323 constructor_fields
= constructor_type
;
9324 constructor_unfilled_fields
= constructor_type
;
9328 /* At the end of an implicit or explicit brace level,
9329 finish up that level of constructor. If a single expression
9330 with redundant braces initialized that level, return the
9331 c_expr structure for that expression. Otherwise, the original_code
9332 element is set to ERROR_MARK.
9333 If we were outputting the elements as they are read, return 0 as the value
9334 from inner levels (process_init_element ignores that),
9335 but return error_mark_node as the value from the outermost level
9336 (that's what we want to put in DECL_INITIAL).
9337 Otherwise, return a CONSTRUCTOR expression as the value. */
9340 pop_init_level (location_t loc
, int implicit
,
9341 struct obstack
*braced_init_obstack
,
9342 location_t insert_before
)
9344 struct constructor_stack
*p
;
9346 ret
.value
= NULL_TREE
;
9347 ret
.original_code
= ERROR_MARK
;
9348 ret
.original_type
= NULL
;
9353 /* When we come to an explicit close brace,
9354 pop any inner levels that didn't have explicit braces. */
9355 while (constructor_stack
->implicit
)
9356 process_init_element (input_location
,
9357 pop_init_level (loc
, 1, braced_init_obstack
,
9359 true, braced_init_obstack
);
9360 gcc_assert (!constructor_range_stack
);
9363 if (initializer_stack
->missing_brace_richloc
)
9364 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
9365 (insert_before
, "}");
9367 /* Now output all pending elements. */
9368 constructor_incremental
= 1;
9369 output_pending_init_elements (1, braced_init_obstack
);
9371 p
= constructor_stack
;
9373 /* Error for initializing a flexible array member, or a zero-length
9374 array member in an inappropriate context. */
9375 if (constructor_type
&& constructor_fields
9376 && TREE_CODE (constructor_type
) == ARRAY_TYPE
9377 && TYPE_DOMAIN (constructor_type
)
9378 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
9380 /* Silently discard empty initializations. The parser will
9381 already have pedwarned for empty brackets. */
9382 if (integer_zerop (constructor_unfilled_index
))
9383 constructor_type
= NULL_TREE
;
9386 gcc_assert (!TYPE_SIZE (constructor_type
));
9388 if (constructor_depth
> 2)
9389 error_init (loc
, "initialization of flexible array member in a nested context");
9391 pedwarn_init (loc
, OPT_Wpedantic
,
9392 "initialization of a flexible array member");
9394 /* We have already issued an error message for the existence
9395 of a flexible array member not at the end of the structure.
9396 Discard the initializer so that we do not die later. */
9397 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
9398 constructor_type
= NULL_TREE
;
9402 switch (vec_safe_length (constructor_elements
))
9405 /* Initialization with { } counts as zeroinit. */
9406 constructor_zeroinit
= 1;
9409 /* This might be zeroinit as well. */
9410 if (integer_zerop ((*constructor_elements
)[0].value
))
9411 constructor_zeroinit
= 1;
9414 /* If the constructor has more than one element, it can't be { 0 }. */
9415 constructor_zeroinit
= 0;
9419 /* Warn when some structs are initialized with direct aggregation. */
9420 if (!implicit
&& found_missing_braces
&& warn_missing_braces
9421 && !constructor_zeroinit
)
9423 gcc_assert (initializer_stack
->missing_brace_richloc
);
9424 warning_at (initializer_stack
->missing_brace_richloc
,
9425 OPT_Wmissing_braces
,
9426 "missing braces around initializer");
9429 /* Warn when some struct elements are implicitly initialized to zero. */
9430 if (warn_missing_field_initializers
9432 && TREE_CODE (constructor_type
) == RECORD_TYPE
9433 && constructor_unfilled_fields
)
9435 /* Do not warn for flexible array members or zero-length arrays. */
9436 while (constructor_unfilled_fields
9437 && (!DECL_SIZE (constructor_unfilled_fields
)
9438 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
9439 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
9441 if (constructor_unfilled_fields
9442 /* Do not warn if this level of the initializer uses member
9443 designators; it is likely to be deliberate. */
9444 && !constructor_designated
9445 /* Do not warn about initializing with { 0 } or with { }. */
9446 && !constructor_zeroinit
)
9448 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
9449 "missing initializer for field %qD of %qT",
9450 constructor_unfilled_fields
,
9452 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
9453 "%qD declared here", constructor_unfilled_fields
);
9457 /* Pad out the end of the structure. */
9458 if (p
->replacement_value
.value
)
9459 /* If this closes a superfluous brace pair,
9460 just pass out the element between them. */
9461 ret
= p
->replacement_value
;
9462 else if (constructor_type
== NULL_TREE
)
9464 else if (!RECORD_OR_UNION_TYPE_P (constructor_type
)
9465 && TREE_CODE (constructor_type
) != ARRAY_TYPE
9466 && !gnu_vector_type_p (constructor_type
))
9468 /* A nonincremental scalar initializer--just return
9469 the element, after verifying there is just one.
9470 Empty scalar initializers are supported in C23. */
9471 if (vec_safe_is_empty (constructor_elements
))
9473 if (constructor_erroneous
|| constructor_type
== error_mark_node
)
9474 ret
.value
= error_mark_node
;
9475 else if (TREE_CODE (constructor_type
) == FUNCTION_TYPE
)
9477 error_init (loc
, "invalid initializer");
9478 ret
.value
= error_mark_node
;
9480 else if (TREE_CODE (constructor_type
) == POINTER_TYPE
)
9481 /* Ensure this is a null pointer constant in the case of a
9482 'constexpr' object initialized with {}. */
9483 ret
.value
= build_zero_cst (ptr_type_node
);
9485 ret
.value
= build_zero_cst (constructor_type
);
9487 else if (vec_safe_length (constructor_elements
) != 1)
9489 error_init (loc
, "extra elements in scalar initializer");
9490 ret
.value
= (*constructor_elements
)[0].value
;
9493 ret
.value
= (*constructor_elements
)[0].value
;
9497 if (constructor_erroneous
)
9498 ret
.value
= error_mark_node
;
9501 ret
.value
= build_constructor (constructor_type
,
9502 constructor_elements
);
9503 if (constructor_constant
)
9504 TREE_CONSTANT (ret
.value
) = 1;
9505 if (constructor_constant
&& constructor_simple
)
9506 TREE_STATIC (ret
.value
) = 1;
9507 if (constructor_nonconst
)
9508 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
9512 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
9514 if (constructor_nonconst
)
9515 ret
.original_code
= C_MAYBE_CONST_EXPR
;
9516 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
9517 ret
.original_code
= ERROR_MARK
;
9520 constructor_type
= p
->type
;
9521 constructor_fields
= p
->fields
;
9522 constructor_index
= p
->index
;
9523 constructor_max_index
= p
->max_index
;
9524 constructor_unfilled_index
= p
->unfilled_index
;
9525 constructor_unfilled_fields
= p
->unfilled_fields
;
9526 constructor_bit_index
= p
->bit_index
;
9527 constructor_elements
= p
->elements
;
9528 constructor_constant
= p
->constant
;
9529 constructor_simple
= p
->simple
;
9530 constructor_nonconst
= p
->nonconst
;
9531 constructor_erroneous
= p
->erroneous
;
9532 constructor_incremental
= p
->incremental
;
9533 constructor_designated
= p
->designated
;
9534 designator_depth
= p
->designator_depth
;
9535 constructor_pending_elts
= p
->pending_elts
;
9536 constructor_depth
= p
->depth
;
9538 constructor_range_stack
= p
->range_stack
;
9539 RESTORE_SPELLING_DEPTH (constructor_depth
);
9541 constructor_stack
= p
->next
;
9544 if (ret
.value
== NULL_TREE
&& constructor_stack
== 0)
9545 ret
.value
= error_mark_node
;
9549 /* Common handling for both array range and field name designators.
9550 ARRAY argument is nonzero for array ranges. Returns false for success. */
9553 set_designator (location_t loc
, bool array
,
9554 struct obstack
*braced_init_obstack
)
9557 enum tree_code subcode
;
9559 /* Don't die if an entire brace-pair level is superfluous
9560 in the containing level, or for an erroneous type. */
9561 if (constructor_type
== NULL_TREE
|| constructor_type
== error_mark_node
)
9564 /* If there were errors in this designator list already, bail out
9566 if (designator_erroneous
)
9569 /* Likewise for an initializer for a variable-size type. Those are
9570 diagnosed in the parser, except for empty initializer braces. */
9571 if (COMPLETE_TYPE_P (constructor_type
)
9572 && TREE_CODE (TYPE_SIZE (constructor_type
)) != INTEGER_CST
)
9575 if (!designator_depth
)
9577 gcc_assert (!constructor_range_stack
);
9579 /* Designator list starts at the level of closest explicit
9581 while (constructor_stack
->implicit
)
9582 process_init_element (input_location
,
9583 pop_init_level (loc
, 1, braced_init_obstack
,
9584 last_init_list_comma
),
9585 true, braced_init_obstack
);
9586 constructor_designated
= 1;
9590 switch (TREE_CODE (constructor_type
))
9594 subtype
= TREE_TYPE (constructor_fields
);
9595 if (subtype
!= error_mark_node
)
9596 subtype
= TYPE_MAIN_VARIANT (subtype
);
9599 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9605 subcode
= TREE_CODE (subtype
);
9606 if (array
&& subcode
!= ARRAY_TYPE
)
9608 error_init (loc
, "array index in non-array initializer");
9611 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
9613 error_init (loc
, "field name not in record or union initializer");
9617 constructor_designated
= 1;
9618 finish_implicit_inits (loc
, braced_init_obstack
);
9619 push_init_level (loc
, 2, braced_init_obstack
);
9623 /* If there are range designators in designator list, push a new designator
9624 to constructor_range_stack. RANGE_END is end of such stack range or
9625 NULL_TREE if there is no range designator at this level. */
9628 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
9630 struct constructor_range_stack
*p
;
9632 p
= (struct constructor_range_stack
*)
9633 obstack_alloc (braced_init_obstack
,
9634 sizeof (struct constructor_range_stack
));
9635 p
->prev
= constructor_range_stack
;
9637 p
->fields
= constructor_fields
;
9638 p
->range_start
= constructor_index
;
9639 p
->index
= constructor_index
;
9640 p
->stack
= constructor_stack
;
9641 p
->range_end
= range_end
;
9642 if (constructor_range_stack
)
9643 constructor_range_stack
->next
= p
;
9644 constructor_range_stack
= p
;
9647 /* Within an array initializer, specify the next index to be initialized.
9648 FIRST is that index. If LAST is nonzero, then initialize a range
9649 of indices, running from FIRST through LAST. */
9652 set_init_index (location_t loc
, tree first
, tree last
,
9653 struct obstack
*braced_init_obstack
)
9655 if (set_designator (loc
, true, braced_init_obstack
))
9658 designator_erroneous
= 1;
9660 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
9661 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
9663 error_init (loc
, "array index in initializer not of integer type");
9667 if (TREE_CODE (first
) != INTEGER_CST
)
9669 first
= c_fully_fold (first
, false, NULL
);
9670 if (TREE_CODE (first
) == INTEGER_CST
)
9671 pedwarn_init (loc
, OPT_Wpedantic
,
9672 "array index in initializer is not "
9673 "an integer constant expression");
9676 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
9678 last
= c_fully_fold (last
, false, NULL
);
9679 if (TREE_CODE (last
) == INTEGER_CST
)
9680 pedwarn_init (loc
, OPT_Wpedantic
,
9681 "array index in initializer is not "
9682 "an integer constant expression");
9685 if (TREE_CODE (first
) != INTEGER_CST
)
9686 error_init (loc
, "nonconstant array index in initializer");
9687 else if (last
!= NULL_TREE
&& TREE_CODE (last
) != INTEGER_CST
)
9688 error_init (loc
, "nonconstant array index in initializer");
9689 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
9690 error_init (loc
, "array index in non-array initializer");
9691 else if (tree_int_cst_sgn (first
) == -1)
9692 error_init (loc
, "array index in initializer exceeds array bounds");
9693 else if (constructor_max_index
9694 && tree_int_cst_lt (constructor_max_index
, first
))
9695 error_init (loc
, "array index in initializer exceeds array bounds");
9698 constant_expression_warning (first
);
9700 constant_expression_warning (last
);
9701 constructor_index
= convert (bitsizetype
, first
);
9702 if (tree_int_cst_lt (constructor_index
, first
))
9704 constructor_index
= copy_node (constructor_index
);
9705 TREE_OVERFLOW (constructor_index
) = 1;
9710 if (tree_int_cst_equal (first
, last
))
9712 else if (tree_int_cst_lt (last
, first
))
9714 error_init (loc
, "empty index range in initializer");
9719 last
= convert (bitsizetype
, last
);
9720 if (constructor_max_index
!= NULL_TREE
9721 && tree_int_cst_lt (constructor_max_index
, last
))
9723 error_init (loc
, "array index range in initializer exceeds "
9731 designator_erroneous
= 0;
9732 if (constructor_range_stack
|| last
)
9733 push_range_stack (last
, braced_init_obstack
);
9737 /* Within a struct initializer, specify the next field to be initialized. */
9740 set_init_label (location_t loc
, tree fieldname
, location_t fieldname_loc
,
9741 struct obstack
*braced_init_obstack
)
9745 if (set_designator (loc
, false, braced_init_obstack
))
9748 designator_erroneous
= 1;
9750 if (!RECORD_OR_UNION_TYPE_P (constructor_type
))
9752 error_init (loc
, "field name not in record or union initializer");
9756 field
= lookup_field (constructor_type
, fieldname
);
9758 if (field
== NULL_TREE
)
9760 tree guessed_id
= lookup_field_fuzzy (constructor_type
, fieldname
);
9763 gcc_rich_location
rich_loc (fieldname_loc
);
9764 rich_loc
.add_fixit_misspelled_id (fieldname_loc
, guessed_id
);
9765 error_at (&rich_loc
,
9766 "%qT has no member named %qE; did you mean %qE?",
9767 constructor_type
, fieldname
, guessed_id
);
9770 error_at (fieldname_loc
, "%qT has no member named %qE",
9771 constructor_type
, fieldname
);
9776 constructor_fields
= TREE_VALUE (field
);
9778 designator_erroneous
= 0;
9779 if (constructor_range_stack
)
9780 push_range_stack (NULL_TREE
, braced_init_obstack
);
9781 field
= TREE_CHAIN (field
);
9784 if (set_designator (loc
, false, braced_init_obstack
))
9788 while (field
!= NULL_TREE
);
9791 /* Add a new initializer to the tree of pending initializers. PURPOSE
9792 identifies the initializer, either array index or field in a structure.
9793 VALUE is the value of that index or field. If ORIGTYPE is not
9794 NULL_TREE, it is the original type of VALUE.
9796 IMPLICIT is true if value comes from pop_init_level (1),
9797 the new initializer has been merged with the existing one
9798 and thus no warnings should be emitted about overriding an
9799 existing initializer. */
9802 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
9803 bool implicit
, struct obstack
*braced_init_obstack
)
9805 struct init_node
*p
, **q
, *r
;
9807 q
= &constructor_pending_elts
;
9810 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9815 if (tree_int_cst_lt (purpose
, p
->purpose
))
9817 else if (tree_int_cst_lt (p
->purpose
, purpose
))
9823 if (TREE_SIDE_EFFECTS (p
->value
))
9824 warning_init (loc
, OPT_Woverride_init_side_effects
,
9825 "initialized field with side-effects "
9827 else if (warn_override_init
)
9828 warning_init (loc
, OPT_Woverride_init
,
9829 "initialized field overwritten");
9832 p
->origtype
= origtype
;
9841 bitpos
= bit_position (purpose
);
9845 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
9847 else if (p
->purpose
!= purpose
)
9853 if (TREE_SIDE_EFFECTS (p
->value
))
9854 warning_init (loc
, OPT_Woverride_init_side_effects
,
9855 "initialized field with side-effects "
9857 else if (warn_override_init
)
9858 warning_init (loc
, OPT_Woverride_init
,
9859 "initialized field overwritten");
9862 p
->origtype
= origtype
;
9868 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
9869 sizeof (struct init_node
));
9870 r
->purpose
= purpose
;
9872 r
->origtype
= origtype
;
9882 struct init_node
*s
;
9886 if (p
->balance
== 0)
9888 else if (p
->balance
< 0)
9895 p
->left
->parent
= p
;
9912 constructor_pending_elts
= r
;
9917 struct init_node
*t
= r
->right
;
9921 r
->right
->parent
= r
;
9926 p
->left
->parent
= p
;
9929 p
->balance
= t
->balance
< 0;
9930 r
->balance
= -(t
->balance
> 0);
9945 constructor_pending_elts
= t
;
9951 /* p->balance == +1; growth of left side balances the node. */
9956 else /* r == p->right */
9958 if (p
->balance
== 0)
9959 /* Growth propagation from right side. */
9961 else if (p
->balance
> 0)
9968 p
->right
->parent
= p
;
9985 constructor_pending_elts
= r
;
9987 else /* r->balance == -1 */
9990 struct init_node
*t
= r
->left
;
9994 r
->left
->parent
= r
;
9999 p
->right
->parent
= p
;
10002 r
->balance
= (t
->balance
< 0);
10003 p
->balance
= -(t
->balance
> 0);
10018 constructor_pending_elts
= t
;
10024 /* p->balance == -1; growth of right side balances the node. */
10035 /* Build AVL tree from a sorted chain. */
10038 set_nonincremental_init (struct obstack
* braced_init_obstack
)
10040 unsigned HOST_WIDE_INT ix
;
10043 if (TREE_CODE (constructor_type
) != RECORD_TYPE
10044 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
10047 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
10048 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
10049 braced_init_obstack
);
10050 constructor_elements
= NULL
;
10051 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
10053 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
10054 /* Skip any nameless bit fields at the beginning. */
10055 while (constructor_unfilled_fields
!= NULL_TREE
10056 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields
))
10057 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
10060 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
10062 if (TYPE_DOMAIN (constructor_type
))
10063 constructor_unfilled_index
10064 = convert (bitsizetype
,
10065 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
10067 constructor_unfilled_index
= bitsize_zero_node
;
10069 constructor_incremental
= 0;
10072 /* Build AVL tree from a string constant. */
10075 set_nonincremental_init_from_string (tree str
,
10076 struct obstack
* braced_init_obstack
)
10078 tree value
, purpose
, type
;
10079 HOST_WIDE_INT val
[2];
10080 const char *p
, *end
;
10081 int byte
, wchar_bytes
, charwidth
, bitpos
;
10083 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
10085 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
10086 charwidth
= TYPE_PRECISION (char_type_node
);
10087 gcc_assert ((size_t) wchar_bytes
* charwidth
10088 <= ARRAY_SIZE (val
) * HOST_BITS_PER_WIDE_INT
);
10089 type
= TREE_TYPE (constructor_type
);
10090 p
= TREE_STRING_POINTER (str
);
10091 end
= p
+ TREE_STRING_LENGTH (str
);
10093 for (purpose
= bitsize_zero_node
;
10095 && !(constructor_max_index
10096 && tree_int_cst_lt (constructor_max_index
, purpose
));
10097 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
10099 if (wchar_bytes
== 1)
10101 val
[0] = (unsigned char) *p
++;
10108 for (byte
= 0; byte
< wchar_bytes
; byte
++)
10110 if (BYTES_BIG_ENDIAN
)
10111 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
10113 bitpos
= byte
* charwidth
;
10114 val
[bitpos
/ HOST_BITS_PER_WIDE_INT
]
10115 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
10116 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
10120 if (!TYPE_UNSIGNED (type
))
10122 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
10123 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
10125 if (val
[0] & (HOST_WIDE_INT_1
<< (bitpos
- 1)))
10127 val
[0] |= HOST_WIDE_INT_M1U
<< bitpos
;
10131 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
10136 else if (val
[1] & (HOST_WIDE_INT_1
10137 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
10138 val
[1] |= HOST_WIDE_INT_M1U
<< (bitpos
- HOST_BITS_PER_WIDE_INT
);
10141 value
= wide_int_to_tree (type
,
10142 wide_int::from_array (val
, 2,
10143 HOST_BITS_PER_WIDE_INT
* 2));
10144 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
10145 braced_init_obstack
);
10148 constructor_incremental
= 0;
10151 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
10152 not initialized yet. */
10155 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
10157 struct init_node
*p
;
10159 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
10161 if (constructor_incremental
10162 && tree_int_cst_lt (field
, constructor_unfilled_index
))
10163 set_nonincremental_init (braced_init_obstack
);
10165 p
= constructor_pending_elts
;
10168 if (tree_int_cst_lt (field
, p
->purpose
))
10170 else if (tree_int_cst_lt (p
->purpose
, field
))
10176 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
10178 tree bitpos
= bit_position (field
);
10180 if (constructor_incremental
10181 && (!constructor_unfilled_fields
10182 || tree_int_cst_lt (bitpos
,
10183 bit_position (constructor_unfilled_fields
))))
10184 set_nonincremental_init (braced_init_obstack
);
10186 p
= constructor_pending_elts
;
10189 if (field
== p
->purpose
)
10191 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
10197 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
10199 if (!vec_safe_is_empty (constructor_elements
)
10200 && (constructor_elements
->last ().index
== field
))
10201 return constructor_elements
->last ().value
;
10206 /* "Output" the next constructor element.
10207 At top level, really output it to assembler code now.
10208 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
10209 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
10210 TYPE is the data type that the containing data type wants here.
10211 FIELD is the field (a FIELD_DECL) or the index that this element fills.
10212 If VALUE is a string constant, STRICT_STRING is true if it is
10213 unparenthesized or we should not warn here for it being parenthesized.
10214 For other types of VALUE, STRICT_STRING is not used.
10216 PENDING if true means output pending elements that belong
10217 right after this element. (PENDING is normally true;
10218 it is false while outputting pending elements, to avoid recursion.)
10220 IMPLICIT is true if value comes from pop_init_level (1),
10221 the new initializer has been merged with the existing one
10222 and thus no warnings should be emitted about overriding an
10223 existing initializer. */
10226 output_init_element (location_t loc
, tree value
, tree origtype
,
10227 bool strict_string
, tree type
, tree field
, bool pending
,
10228 bool implicit
, struct obstack
* braced_init_obstack
)
10230 tree semantic_type
= NULL_TREE
;
10231 bool maybe_const
= true;
10232 bool npc
, int_const_expr
, arith_const_expr
;
10234 if (type
== error_mark_node
|| value
== error_mark_node
)
10236 constructor_erroneous
= 1;
10239 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
10240 && (TREE_CODE (value
) == STRING_CST
10241 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
10242 && !(TREE_CODE (value
) == STRING_CST
10243 && TREE_CODE (type
) == ARRAY_TYPE
10244 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
10245 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
10246 TYPE_MAIN_VARIANT (type
)))
10247 value
= array_to_pointer_conversion (input_location
, value
);
10249 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
10250 && require_constant_value
&& pending
)
10252 /* As an extension, allow initializing objects with static storage
10253 duration with compound literals (which are then treated just as
10254 the brace enclosed list they contain). */
10256 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
10258 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
10259 value
= DECL_INITIAL (decl
);
10262 npc
= null_pointer_constant_p (value
);
10263 int_const_expr
= (TREE_CODE (value
) == INTEGER_CST
10264 && !TREE_OVERFLOW (value
)
10265 && INTEGRAL_TYPE_P (TREE_TYPE (value
)));
10266 /* Not fully determined before folding. */
10267 arith_const_expr
= true;
10268 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
10270 semantic_type
= TREE_TYPE (value
);
10271 value
= TREE_OPERAND (value
, 0);
10273 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
10274 /* TODO: this may not detect all cases of expressions folding to
10275 constants that are not arithmetic constant expressions. */
10277 arith_const_expr
= false;
10278 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value
))
10279 && TREE_CODE (TREE_TYPE (value
)) != REAL_TYPE
10280 && TREE_CODE (TREE_TYPE (value
)) != COMPLEX_TYPE
)
10281 arith_const_expr
= false;
10282 else if (TREE_CODE (value
) != INTEGER_CST
10283 && TREE_CODE (value
) != REAL_CST
10284 && TREE_CODE (value
) != COMPLEX_CST
)
10285 arith_const_expr
= false;
10286 else if (TREE_OVERFLOW (value
))
10287 arith_const_expr
= false;
10289 if (value
== error_mark_node
)
10290 constructor_erroneous
= 1;
10291 else if (!TREE_CONSTANT (value
))
10292 constructor_constant
= 0;
10293 else if (!initializer_constant_valid_p (value
,
10295 AGGREGATE_TYPE_P (constructor_type
)
10296 && TYPE_REVERSE_STORAGE_ORDER
10297 (constructor_type
))
10298 || (RECORD_OR_UNION_TYPE_P (constructor_type
)
10299 && DECL_C_BIT_FIELD (field
)
10300 && TREE_CODE (value
) != INTEGER_CST
))
10301 constructor_simple
= 0;
10303 constructor_nonconst
= 1;
10305 /* Digest the initializer and issue any errors about incompatible
10306 types before issuing errors about non-constant initializers. */
10307 tree new_value
= value
;
10309 new_value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
10310 /* In the case of braces around a scalar initializer, the result of
10311 this initializer processing goes through digest_init again at the
10312 outer level. In the case of a constexpr initializer for a
10313 pointer, avoid converting a null pointer constant to something
10314 that is not a null pointer constant to avoid a spurious error
10315 from that second processing. */
10316 if (!require_constexpr_value
10318 || TREE_CODE (constructor_type
) != POINTER_TYPE
)
10319 new_value
= digest_init (loc
, type
, new_value
, origtype
, npc
,
10320 int_const_expr
, arith_const_expr
, strict_string
,
10321 require_constant_value
, require_constexpr_value
);
10322 if (new_value
== error_mark_node
)
10324 constructor_erroneous
= 1;
10327 if (require_constant_value
|| require_constant_elements
)
10328 constant_expression_warning (new_value
);
10330 /* Proceed to check the constness of the original initializer. */
10331 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
10333 if (require_constant_value
)
10335 error_init (loc
, "initializer element is not constant");
10336 value
= error_mark_node
;
10338 else if (require_constant_elements
)
10339 pedwarn (loc
, OPT_Wpedantic
,
10340 "initializer element is not computable at load time");
10342 else if (!maybe_const
10343 && (require_constant_value
|| require_constant_elements
))
10344 pedwarn_init (loc
, OPT_Wpedantic
,
10345 "initializer element is not a constant expression");
10346 /* digest_init has already carried out the additional checks
10347 required for 'constexpr' initializers (using the information
10348 passed to it about whether the original initializer was certain
10349 kinds of constant expression), so that check does not need to be
10352 /* Issue -Wc++-compat warnings about initializing a bitfield with
10354 if (warn_cxx_compat
10355 && field
!= NULL_TREE
10356 && TREE_CODE (field
) == FIELD_DECL
10357 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
10358 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
10359 != TYPE_MAIN_VARIANT (type
))
10360 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
10362 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
10363 if (checktype
!= error_mark_node
10364 && (TYPE_MAIN_VARIANT (checktype
)
10365 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
10366 warning_init (loc
, OPT_Wc___compat
,
10367 "enum conversion in initialization is invalid in C++");
10370 /* If this field is empty and does not have side effects (and is not at
10371 the end of structure), don't do anything other than checking the
10374 && (TREE_TYPE (field
) == error_mark_node
10375 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
10376 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
10377 && !TREE_SIDE_EFFECTS (new_value
)
10378 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
10379 || DECL_CHAIN (field
)))))
10382 /* Finally, set VALUE to the initializer value digested above. */
10385 /* If this element doesn't come next in sequence,
10386 put it on constructor_pending_elts. */
10387 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
10388 && (!constructor_incremental
10389 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
10391 if (constructor_incremental
10392 && tree_int_cst_lt (field
, constructor_unfilled_index
))
10393 set_nonincremental_init (braced_init_obstack
);
10395 add_pending_init (loc
, field
, value
, origtype
, implicit
,
10396 braced_init_obstack
);
10399 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
10400 && (!constructor_incremental
10401 || field
!= constructor_unfilled_fields
))
10403 /* We do this for records but not for unions. In a union,
10404 no matter which field is specified, it can be initialized
10405 right away since it starts at the beginning of the union. */
10406 if (constructor_incremental
)
10408 if (!constructor_unfilled_fields
)
10409 set_nonincremental_init (braced_init_obstack
);
10412 tree bitpos
, unfillpos
;
10414 bitpos
= bit_position (field
);
10415 unfillpos
= bit_position (constructor_unfilled_fields
);
10417 if (tree_int_cst_lt (bitpos
, unfillpos
))
10418 set_nonincremental_init (braced_init_obstack
);
10422 add_pending_init (loc
, field
, value
, origtype
, implicit
,
10423 braced_init_obstack
);
10426 else if (TREE_CODE (constructor_type
) == UNION_TYPE
10427 && !vec_safe_is_empty (constructor_elements
))
10431 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
10432 warning_init (loc
, OPT_Woverride_init_side_effects
,
10433 "initialized field with side-effects overwritten");
10434 else if (warn_override_init
)
10435 warning_init (loc
, OPT_Woverride_init
,
10436 "initialized field overwritten");
10439 /* We can have just one union field set. */
10440 constructor_elements
= NULL
;
10443 /* Otherwise, output this element either to
10444 constructor_elements or to the assembler file. */
10446 constructor_elt celt
= {field
, value
};
10447 vec_safe_push (constructor_elements
, celt
);
10449 /* Advance the variable that indicates sequential elements output. */
10450 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
10451 constructor_unfilled_index
10452 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
10454 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
10456 constructor_unfilled_fields
10457 = DECL_CHAIN (constructor_unfilled_fields
);
10459 /* Skip any nameless bit fields. */
10460 while (constructor_unfilled_fields
!= NULL_TREE
10461 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields
))
10462 constructor_unfilled_fields
=
10463 DECL_CHAIN (constructor_unfilled_fields
);
10465 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
10466 constructor_unfilled_fields
= NULL_TREE
;
10468 /* Now output any pending elements which have become next. */
10470 output_pending_init_elements (0, braced_init_obstack
);
10473 /* For two FIELD_DECLs in the same chain, return -1 if field1
10474 comes before field2, 1 if field1 comes after field2 and
10475 0 if field1 == field2. */
10478 init_field_decl_cmp (tree field1
, tree field2
)
10480 if (field1
== field2
)
10483 tree bitpos1
= bit_position (field1
);
10484 tree bitpos2
= bit_position (field2
);
10485 if (tree_int_cst_equal (bitpos1
, bitpos2
))
10487 /* If one of the fields has non-zero bitsize, then that
10488 field must be the last one in a sequence of zero
10489 sized fields, fields after it will have bigger
10491 if (TREE_TYPE (field1
) != error_mark_node
10492 && COMPLETE_TYPE_P (TREE_TYPE (field1
))
10493 && integer_nonzerop (TREE_TYPE (field1
)))
10495 if (TREE_TYPE (field2
) != error_mark_node
10496 && COMPLETE_TYPE_P (TREE_TYPE (field2
))
10497 && integer_nonzerop (TREE_TYPE (field2
)))
10499 /* Otherwise, fallback to DECL_CHAIN walk to find out
10500 which field comes earlier. Walk chains of both
10501 fields, so that if field1 and field2 are close to each
10502 other in either order, it is found soon even for large
10503 sequences of zero sized fields. */
10504 tree f1
= field1
, f2
= field2
;
10507 f1
= DECL_CHAIN (f1
);
10508 f2
= DECL_CHAIN (f2
);
10509 if (f1
== NULL_TREE
)
10514 if (f2
== NULL_TREE
)
10520 if (!tree_int_cst_equal (bit_position (f1
), bitpos1
))
10522 if (!tree_int_cst_equal (bit_position (f2
), bitpos1
))
10526 else if (tree_int_cst_lt (bitpos1
, bitpos2
))
10532 /* Output any pending elements which have become next.
10533 As we output elements, constructor_unfilled_{fields,index}
10534 advances, which may cause other elements to become next;
10535 if so, they too are output.
10537 If ALL is 0, we return when there are
10538 no more pending elements to output now.
10540 If ALL is 1, we output space as necessary so that
10541 we can output all the pending elements. */
10543 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
10545 struct init_node
*elt
= constructor_pending_elts
;
10550 /* Look through the whole pending tree.
10551 If we find an element that should be output now,
10552 output it. Otherwise, set NEXT to the element
10553 that comes first among those still pending. */
10558 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
10560 if (tree_int_cst_equal (elt
->purpose
,
10561 constructor_unfilled_index
))
10562 output_init_element (input_location
, elt
->value
, elt
->origtype
,
10563 true, TREE_TYPE (constructor_type
),
10564 constructor_unfilled_index
, false, false,
10565 braced_init_obstack
);
10566 else if (tree_int_cst_lt (constructor_unfilled_index
,
10569 /* Advance to the next smaller node. */
10574 /* We have reached the smallest node bigger than the
10575 current unfilled index. Fill the space first. */
10576 next
= elt
->purpose
;
10582 /* Advance to the next bigger node. */
10587 /* We have reached the biggest node in a subtree. Find
10588 the parent of it, which is the next bigger node. */
10589 while (elt
->parent
&& elt
->parent
->right
== elt
)
10592 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
10595 next
= elt
->purpose
;
10601 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
10603 /* If the current record is complete we are done. */
10604 if (constructor_unfilled_fields
== NULL_TREE
)
10607 int cmp
= init_field_decl_cmp (constructor_unfilled_fields
,
10610 output_init_element (input_location
, elt
->value
, elt
->origtype
,
10611 true, TREE_TYPE (elt
->purpose
),
10612 elt
->purpose
, false, false,
10613 braced_init_obstack
);
10616 /* Advance to the next smaller node. */
10621 /* We have reached the smallest node bigger than the
10622 current unfilled field. Fill the space first. */
10623 next
= elt
->purpose
;
10629 /* Advance to the next bigger node. */
10634 /* We have reached the biggest node in a subtree. Find
10635 the parent of it, which is the next bigger node. */
10636 while (elt
->parent
&& elt
->parent
->right
== elt
)
10640 && init_field_decl_cmp (constructor_unfilled_fields
,
10643 next
= elt
->purpose
;
10651 /* Ordinarily return, but not if we want to output all
10652 and there are elements left. */
10653 if (!(all
&& next
!= NULL_TREE
))
10656 /* If it's not incremental, just skip over the gap, so that after
10657 jumping to retry we will output the next successive element. */
10658 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
10659 constructor_unfilled_fields
= next
;
10660 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
10661 constructor_unfilled_index
= next
;
10663 /* ELT now points to the node in the pending tree with the next
10664 initializer to output. */
10668 /* Expression VALUE coincides with the start of type TYPE in a braced
10669 initializer. Return true if we should treat VALUE as initializing
10670 the first element of TYPE, false if we should treat it as initializing
10673 If the initializer is clearly invalid, the question becomes:
10674 which choice gives the best error message? */
10677 initialize_elementwise_p (tree type
, tree value
)
10679 if (type
== error_mark_node
|| value
== error_mark_node
)
10682 gcc_checking_assert (TYPE_MAIN_VARIANT (type
) == type
);
10684 tree value_type
= TREE_TYPE (value
);
10685 if (value_type
== error_mark_node
)
10688 /* GNU vectors can be initialized elementwise. However, treat any
10689 kind of vector value as initializing the vector type as a whole,
10690 regardless of whether the value is a GNU vector. Such initializers
10691 are valid if and only if they would have been valid in a non-braced
10696 so recursing into the vector type would be at best confusing or at
10697 worst wrong. For example, when -flax-vector-conversions is in effect,
10698 it's possible to initialize a V8HI from a V4SI, even though the vectors
10699 have different element types and different numbers of elements. */
10700 if (gnu_vector_type_p (type
))
10701 return !VECTOR_TYPE_P (value_type
);
10703 if (AGGREGATE_TYPE_P (type
))
10704 return !comptypes (type
, TYPE_MAIN_VARIANT (value_type
));
10709 /* Add one non-braced element to the current constructor level.
10710 This adjusts the current position within the constructor's type.
10711 This may also start or terminate implicit levels
10712 to handle a partly-braced initializer.
10714 Once this has found the correct level for the new element,
10715 it calls output_init_element.
10717 IMPLICIT is true if value comes from pop_init_level (1),
10718 the new initializer has been merged with the existing one
10719 and thus no warnings should be emitted about overriding an
10720 existing initializer. */
10723 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
10724 struct obstack
* braced_init_obstack
)
10726 tree orig_value
= value
.value
;
10728 = (orig_value
!= NULL_TREE
&& TREE_CODE (orig_value
) == STRING_CST
);
10729 bool strict_string
= value
.original_code
== STRING_CST
;
10730 bool was_designated
= designator_depth
!= 0;
10732 designator_depth
= 0;
10733 designator_erroneous
= 0;
10735 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
10736 constructor_zeroinit
= 0;
10738 /* Handle superfluous braces around string cst as in
10739 char x[] = {"foo"}; */
10740 if (constructor_type
10742 && TREE_CODE (constructor_type
) == ARRAY_TYPE
10743 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
10744 && integer_zerop (constructor_unfilled_index
))
10746 if (constructor_stack
->replacement_value
.value
)
10748 error_init (loc
, "excess elements in %qT initializer", constructor_type
);
10751 else if (string_flag
)
10753 constructor_stack
->replacement_value
= value
;
10758 if (constructor_stack
->replacement_value
.value
!= NULL_TREE
)
10760 error_init (loc
, "excess elements in struct initializer");
10764 /* Ignore elements of a brace group if it is entirely superfluous
10765 and has already been diagnosed, or if the type is erroneous. */
10766 if (constructor_type
== NULL_TREE
|| constructor_type
== error_mark_node
)
10769 /* Ignore elements of an initializer for a variable-size type.
10770 Those are diagnosed in the parser (empty initializer braces are OK). */
10771 if (COMPLETE_TYPE_P (constructor_type
)
10772 && !poly_int_tree_p (TYPE_SIZE (constructor_type
)))
10775 if (!implicit
&& warn_designated_init
&& !was_designated
10776 && TREE_CODE (constructor_type
) == RECORD_TYPE
10777 && lookup_attribute ("designated_init",
10778 TYPE_ATTRIBUTES (constructor_type
)))
10780 OPT_Wdesignated_init
,
10781 "positional initialization of field "
10782 "in %<struct%> declared with %<designated_init%> attribute");
10784 /* If we've exhausted any levels that didn't have braces,
10786 while (constructor_stack
->implicit
)
10788 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
10789 && constructor_fields
== NULL_TREE
)
10790 process_init_element (loc
,
10791 pop_init_level (loc
, 1, braced_init_obstack
,
10792 last_init_list_comma
),
10793 true, braced_init_obstack
);
10794 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
10795 || gnu_vector_type_p (constructor_type
))
10796 && constructor_max_index
10797 && tree_int_cst_lt (constructor_max_index
,
10798 constructor_index
))
10799 process_init_element (loc
,
10800 pop_init_level (loc
, 1, braced_init_obstack
,
10801 last_init_list_comma
),
10802 true, braced_init_obstack
);
10807 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
10808 if (constructor_range_stack
)
10810 /* If value is a compound literal and we'll be just using its
10811 content, don't put it into a SAVE_EXPR. */
10812 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
10813 || !require_constant_value
)
10815 tree semantic_type
= NULL_TREE
;
10816 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
10818 semantic_type
= TREE_TYPE (value
.value
);
10819 value
.value
= TREE_OPERAND (value
.value
, 0);
10821 value
.value
= save_expr (value
.value
);
10823 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
10830 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
10833 enum tree_code fieldcode
;
10835 if (constructor_fields
== NULL_TREE
)
10837 pedwarn_init (loc
, 0, "excess elements in struct initializer");
10841 fieldtype
= TREE_TYPE (constructor_fields
);
10842 if (fieldtype
!= error_mark_node
)
10843 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
10844 fieldcode
= TREE_CODE (fieldtype
);
10846 /* Error for non-static initialization of a flexible array member. */
10847 if (fieldcode
== ARRAY_TYPE
10848 && !require_constant_value
10849 && TYPE_SIZE (fieldtype
) == NULL_TREE
10850 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
10852 error_init (loc
, "non-static initialization of a flexible "
10857 /* Error for initialization of a flexible array member with
10858 a string constant if the structure is in an array. E.g.:
10859 struct S { int x; char y[]; };
10860 struct S s[] = { { 1, "foo" } };
10863 && fieldcode
== ARRAY_TYPE
10864 && constructor_depth
> 1
10865 && TYPE_SIZE (fieldtype
) == NULL_TREE
10866 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
10868 bool in_array_p
= false;
10869 for (struct constructor_stack
*p
= constructor_stack
;
10870 p
&& p
->type
; p
= p
->next
)
10871 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
10878 error_init (loc
, "initialization of flexible array "
10879 "member in a nested context");
10884 /* Accept a string constant to initialize a subarray. */
10885 if (value
.value
!= NULL_TREE
10886 && fieldcode
== ARRAY_TYPE
10887 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
10889 value
.value
= orig_value
;
10890 /* Otherwise, if we have come to a subaggregate,
10891 and we don't have an element of its type, push into it. */
10892 else if (value
.value
!= NULL_TREE
10893 && initialize_elementwise_p (fieldtype
, value
.value
))
10895 push_init_level (loc
, 1, braced_init_obstack
);
10901 push_member_name (constructor_fields
);
10902 output_init_element (loc
, value
.value
, value
.original_type
,
10903 strict_string
, fieldtype
,
10904 constructor_fields
, true, implicit
,
10905 braced_init_obstack
);
10906 RESTORE_SPELLING_DEPTH (constructor_depth
);
10909 /* Do the bookkeeping for an element that was
10910 directly output as a constructor. */
10912 /* For a record, keep track of end position of last field. */
10913 if (DECL_SIZE (constructor_fields
))
10914 constructor_bit_index
10915 = size_binop_loc (input_location
, PLUS_EXPR
,
10916 bit_position (constructor_fields
),
10917 DECL_SIZE (constructor_fields
));
10919 /* If the current field was the first one not yet written out,
10920 it isn't now, so update. */
10921 if (constructor_unfilled_fields
== constructor_fields
)
10923 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
10924 /* Skip any nameless bit fields. */
10925 while (constructor_unfilled_fields
!= 0
10926 && (DECL_UNNAMED_BIT_FIELD
10927 (constructor_unfilled_fields
)))
10928 constructor_unfilled_fields
=
10929 DECL_CHAIN (constructor_unfilled_fields
);
10933 constructor_fields
= DECL_CHAIN (constructor_fields
);
10934 /* Skip any nameless bit fields at the beginning. */
10935 while (constructor_fields
!= NULL_TREE
10936 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
10937 constructor_fields
= DECL_CHAIN (constructor_fields
);
10939 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
10942 enum tree_code fieldcode
;
10944 if (constructor_fields
== NULL_TREE
)
10946 pedwarn_init (loc
, 0,
10947 "excess elements in union initializer");
10951 fieldtype
= TREE_TYPE (constructor_fields
);
10952 if (fieldtype
!= error_mark_node
)
10953 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
10954 fieldcode
= TREE_CODE (fieldtype
);
10956 /* Warn that traditional C rejects initialization of unions.
10957 We skip the warning if the value is zero. This is done
10958 under the assumption that the zero initializer in user
10959 code appears conditioned on e.g. __STDC__ to avoid
10960 "missing initializer" warnings and relies on default
10961 initialization to zero in the traditional C case.
10962 We also skip the warning if the initializer is designated,
10963 again on the assumption that this must be conditional on
10964 __STDC__ anyway (and we've already complained about the
10965 member-designator already). */
10966 if (!in_system_header_at (input_location
) && !constructor_designated
10967 && !(value
.value
&& (integer_zerop (value
.value
)
10968 || real_zerop (value
.value
))))
10969 warning (OPT_Wtraditional
, "traditional C rejects initialization "
10972 /* Accept a string constant to initialize a subarray. */
10973 if (value
.value
!= NULL_TREE
10974 && fieldcode
== ARRAY_TYPE
10975 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
10977 value
.value
= orig_value
;
10978 /* Otherwise, if we have come to a subaggregate,
10979 and we don't have an element of its type, push into it. */
10980 else if (value
.value
!= NULL_TREE
10981 && initialize_elementwise_p (fieldtype
, value
.value
))
10983 push_init_level (loc
, 1, braced_init_obstack
);
10989 push_member_name (constructor_fields
);
10990 output_init_element (loc
, value
.value
, value
.original_type
,
10991 strict_string
, fieldtype
,
10992 constructor_fields
, true, implicit
,
10993 braced_init_obstack
);
10994 RESTORE_SPELLING_DEPTH (constructor_depth
);
10997 /* Do the bookkeeping for an element that was
10998 directly output as a constructor. */
11000 constructor_bit_index
= DECL_SIZE (constructor_fields
);
11001 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
11004 constructor_fields
= NULL_TREE
;
11006 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
11008 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
11009 enum tree_code eltcode
= TREE_CODE (elttype
);
11011 /* Accept a string constant to initialize a subarray. */
11012 if (value
.value
!= NULL_TREE
11013 && eltcode
== ARRAY_TYPE
11014 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
11016 value
.value
= orig_value
;
11017 /* Otherwise, if we have come to a subaggregate,
11018 and we don't have an element of its type, push into it. */
11019 else if (value
.value
!= NULL_TREE
11020 && initialize_elementwise_p (elttype
, value
.value
))
11022 push_init_level (loc
, 1, braced_init_obstack
);
11026 if (constructor_max_index
!= NULL_TREE
11027 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
11028 || integer_all_onesp (constructor_max_index
)))
11030 pedwarn_init (loc
, 0,
11031 "excess elements in array initializer");
11035 /* Now output the actual element. */
11038 push_array_bounds (tree_to_uhwi (constructor_index
));
11039 output_init_element (loc
, value
.value
, value
.original_type
,
11040 strict_string
, elttype
,
11041 constructor_index
, true, implicit
,
11042 braced_init_obstack
);
11043 RESTORE_SPELLING_DEPTH (constructor_depth
);
11047 = size_binop_loc (input_location
, PLUS_EXPR
,
11048 constructor_index
, bitsize_one_node
);
11051 /* If we are doing the bookkeeping for an element that was
11052 directly output as a constructor, we must update
11053 constructor_unfilled_index. */
11054 constructor_unfilled_index
= constructor_index
;
11056 else if (gnu_vector_type_p (constructor_type
))
11058 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
11060 /* Do a basic check of initializer size. Note that vectors
11061 always have a fixed size derived from their type. */
11062 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
11064 pedwarn_init (loc
, 0,
11065 "excess elements in vector initializer");
11069 /* Now output the actual element. */
11072 if (TREE_CODE (value
.value
) == VECTOR_CST
)
11073 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
11074 output_init_element (loc
, value
.value
, value
.original_type
,
11075 strict_string
, elttype
,
11076 constructor_index
, true, implicit
,
11077 braced_init_obstack
);
11081 = size_binop_loc (input_location
,
11082 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
11085 /* If we are doing the bookkeeping for an element that was
11086 directly output as a constructor, we must update
11087 constructor_unfilled_index. */
11088 constructor_unfilled_index
= constructor_index
;
11091 /* Handle the sole element allowed in a braced initializer
11092 for a scalar variable. */
11093 else if (constructor_type
!= error_mark_node
11094 && constructor_fields
== NULL_TREE
)
11096 pedwarn_init (loc
, 0,
11097 "excess elements in scalar initializer");
11103 output_init_element (loc
, value
.value
, value
.original_type
,
11104 strict_string
, constructor_type
,
11105 NULL_TREE
, true, implicit
,
11106 braced_init_obstack
);
11107 constructor_fields
= NULL_TREE
;
11110 /* Handle range initializers either at this level or anywhere higher
11111 in the designator stack. */
11112 if (constructor_range_stack
)
11114 struct constructor_range_stack
*p
, *range_stack
;
11117 range_stack
= constructor_range_stack
;
11118 constructor_range_stack
= 0;
11119 while (constructor_stack
!= range_stack
->stack
)
11121 gcc_assert (constructor_stack
->implicit
);
11122 process_init_element (loc
,
11123 pop_init_level (loc
, 1,
11124 braced_init_obstack
,
11125 last_init_list_comma
),
11126 true, braced_init_obstack
);
11128 for (p
= range_stack
;
11129 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
11132 gcc_assert (constructor_stack
->implicit
);
11133 process_init_element (loc
,
11134 pop_init_level (loc
, 1,
11135 braced_init_obstack
,
11136 last_init_list_comma
),
11137 true, braced_init_obstack
);
11140 p
->index
= size_binop_loc (input_location
,
11141 PLUS_EXPR
, p
->index
, bitsize_one_node
);
11142 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
11147 constructor_index
= p
->index
;
11148 constructor_fields
= p
->fields
;
11149 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
11157 finish_implicit_inits (loc
, braced_init_obstack
);
11158 push_init_level (loc
, 2, braced_init_obstack
);
11159 p
->stack
= constructor_stack
;
11160 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
11161 p
->index
= p
->range_start
;
11165 constructor_range_stack
= range_stack
;
11172 constructor_range_stack
= 0;
11175 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
11176 (guaranteed to be 'volatile' or null) and ARGS (represented using
11177 an ASM_EXPR node). */
11179 build_asm_stmt (bool is_volatile
, tree args
)
11182 ASM_VOLATILE_P (args
) = 1;
11183 return add_stmt (args
);
11186 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
11187 some INPUTS, and some CLOBBERS. The latter three may be NULL.
11188 SIMPLE indicates whether there was anything at all after the
11189 string in the asm expression -- asm("blah") and asm("blah" : )
11190 are subtly different. We use a ASM_EXPR node to represent this.
11191 LOC is the location of the asm, and IS_INLINE says whether this
11194 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
11195 tree clobbers
, tree labels
, bool simple
, bool is_inline
)
11200 const char *constraint
;
11201 const char **oconstraints
;
11202 bool allows_mem
, allows_reg
, is_inout
;
11203 int ninputs
, noutputs
;
11205 ninputs
= list_length (inputs
);
11206 noutputs
= list_length (outputs
);
11207 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
11209 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
11211 /* Remove output conversions that change the type but not the mode. */
11212 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
11214 tree output
= TREE_VALUE (tail
);
11216 output
= c_fully_fold (output
, false, NULL
, true);
11218 /* ??? Really, this should not be here. Users should be using a
11219 proper lvalue, dammit. But there's a long history of using casts
11220 in the output operands. In cases like longlong.h, this becomes a
11221 primitive form of typechecking -- if the cast can be removed, then
11222 the output operand had a type of the proper width; otherwise we'll
11223 get an error. Gross, but ... */
11224 STRIP_NOPS (output
);
11226 if (!lvalue_or_else (loc
, output
, lv_asm
))
11227 output
= error_mark_node
;
11229 if (output
!= error_mark_node
11230 && (TREE_READONLY (output
)
11231 || TYPE_READONLY (TREE_TYPE (output
))
11232 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output
))
11233 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
11234 readonly_error (loc
, output
, lv_asm
);
11236 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
11237 oconstraints
[i
] = constraint
;
11239 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
11240 &allows_mem
, &allows_reg
, &is_inout
))
11242 /* If the operand is going to end up in memory,
11243 mark it addressable. */
11244 if (!allows_reg
&& !c_mark_addressable (output
))
11245 output
= error_mark_node
;
11246 if (!(!allows_reg
&& allows_mem
)
11247 && output
!= error_mark_node
11248 && VOID_TYPE_P (TREE_TYPE (output
)))
11250 error_at (loc
, "invalid use of void expression");
11251 output
= error_mark_node
;
11255 output
= error_mark_node
;
11257 TREE_VALUE (tail
) = output
;
11260 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
11264 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
11265 input
= TREE_VALUE (tail
);
11267 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
11268 oconstraints
, &allows_mem
, &allows_reg
))
11270 /* If the operand is going to end up in memory,
11271 mark it addressable. */
11272 if (!allows_reg
&& allows_mem
)
11274 input
= c_fully_fold (input
, false, NULL
, true);
11276 /* Strip the nops as we allow this case. FIXME, this really
11277 should be rejected or made deprecated. */
11278 STRIP_NOPS (input
);
11279 if (!c_mark_addressable (input
))
11280 input
= error_mark_node
;
11284 struct c_expr expr
;
11285 memset (&expr
, 0, sizeof (expr
));
11286 expr
.value
= input
;
11287 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
11288 input
= c_fully_fold (expr
.value
, false, NULL
);
11290 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
11292 error_at (loc
, "invalid use of void expression");
11293 input
= error_mark_node
;
11298 input
= error_mark_node
;
11300 TREE_VALUE (tail
) = input
;
11303 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
11305 /* asm statements without outputs, including simple ones, are treated
11307 ASM_INPUT_P (args
) = simple
;
11308 ASM_VOLATILE_P (args
) = (noutputs
== 0);
11309 ASM_INLINE_P (args
) = is_inline
;
11314 /* Generate a goto statement to LABEL. LOC is the location of the
11318 c_finish_goto_label (location_t loc
, tree label
)
11320 tree decl
= lookup_label_for_goto (loc
, label
);
11323 TREE_USED (decl
) = 1;
11325 add_stmt (build_predict_expr (PRED_GOTO
, NOT_TAKEN
));
11326 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
11327 SET_EXPR_LOCATION (t
, loc
);
11328 return add_stmt (t
);
11332 /* Generate a computed goto statement to EXPR. LOC is the location of
11336 c_finish_goto_ptr (location_t loc
, c_expr val
)
11338 tree expr
= val
.value
;
11340 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
11341 if (expr
!= error_mark_node
11342 && !POINTER_TYPE_P (TREE_TYPE (expr
))
11343 && !null_pointer_constant_p (expr
))
11345 error_at (val
.get_location (),
11346 "computed goto must be pointer type");
11347 expr
= build_zero_cst (ptr_type_node
);
11349 expr
= c_fully_fold (expr
, false, NULL
);
11350 expr
= convert (ptr_type_node
, expr
);
11351 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
11352 SET_EXPR_LOCATION (t
, loc
);
11353 return add_stmt (t
);
11356 /* Generate a C `return' statement. RETVAL is the expression for what
11357 to return, or a null pointer for `return;' with no value. LOC is
11358 the location of the return statement, or the location of the expression,
11359 if the statement has any. If ORIGTYPE is not NULL_TREE, it
11360 is the original type of RETVAL. */
11363 c_finish_return (location_t loc
, tree retval
, tree origtype
)
11365 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
11366 bool no_warning
= false;
11369 /* Use the expansion point to handle cases such as returning NULL
11370 in a function returning void. */
11371 location_t xloc
= expansion_point_location_if_in_system_header (loc
);
11373 if (TREE_THIS_VOLATILE (current_function_decl
))
11374 warning_at (xloc
, 0,
11375 "function declared %<noreturn%> has a %<return%> statement");
11379 tree semantic_type
= NULL_TREE
;
11380 npc
= null_pointer_constant_p (retval
);
11381 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
11383 semantic_type
= TREE_TYPE (retval
);
11384 retval
= TREE_OPERAND (retval
, 0);
11386 retval
= c_fully_fold (retval
, false, NULL
);
11388 && valtype
!= NULL_TREE
11389 && TREE_CODE (valtype
) != VOID_TYPE
)
11390 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
11395 current_function_returns_null
= 1;
11396 if ((warn_return_type
>= 0 || flag_isoc99
)
11397 && valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
11400 if (emit_diagnostic (flag_isoc99
? DK_PERMERROR
: DK_WARNING
,
11401 loc
, OPT_Wreturn_mismatch
,
11402 "%<return%> with no value,"
11403 " in function returning non-void"))
11404 inform (DECL_SOURCE_LOCATION (current_function_decl
),
11408 else if (valtype
== NULL_TREE
|| VOID_TYPE_P (valtype
))
11410 current_function_returns_null
= 1;
11412 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
11413 warned_here
= permerror_opt
11414 (xloc
, OPT_Wreturn_mismatch
,
11415 "%<return%> with a value, in function returning void");
11417 warned_here
= pedwarn
11418 (xloc
, OPT_Wpedantic
, "ISO C forbids "
11419 "%<return%> with expression, in function returning void");
11421 inform (DECL_SOURCE_LOCATION (current_function_decl
),
11426 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
11427 retval
, origtype
, ic_return
,
11428 npc
, NULL_TREE
, NULL_TREE
, 0);
11429 tree res
= DECL_RESULT (current_function_decl
);
11433 current_function_returns_value
= 1;
11434 if (t
== error_mark_node
)
11437 save
= in_late_binary_op
;
11438 if (C_BOOLEAN_TYPE_P (TREE_TYPE (res
))
11439 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
11440 || (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t
))
11441 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
11442 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
11443 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
11444 in_late_binary_op
= true;
11445 inner
= t
= convert (TREE_TYPE (res
), t
);
11446 in_late_binary_op
= save
;
11448 /* Strip any conversions, additions, and subtractions, and see if
11449 we are returning the address of a local variable. Warn if so. */
11452 switch (TREE_CODE (inner
))
11455 case NON_LVALUE_EXPR
:
11457 case POINTER_PLUS_EXPR
:
11458 inner
= TREE_OPERAND (inner
, 0);
11462 /* If the second operand of the MINUS_EXPR has a pointer
11463 type (or is converted from it), this may be valid, so
11464 don't give a warning. */
11466 tree op1
= TREE_OPERAND (inner
, 1);
11468 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
11469 && (CONVERT_EXPR_P (op1
)
11470 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
11471 op1
= TREE_OPERAND (op1
, 0);
11473 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
11476 inner
= TREE_OPERAND (inner
, 0);
11481 inner
= TREE_OPERAND (inner
, 0);
11483 while (REFERENCE_CLASS_P (inner
)
11484 && !INDIRECT_REF_P (inner
))
11485 inner
= TREE_OPERAND (inner
, 0);
11488 && !DECL_EXTERNAL (inner
)
11489 && !TREE_STATIC (inner
)
11490 && DECL_CONTEXT (inner
) == current_function_decl
11491 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
11493 if (TREE_CODE (inner
) == LABEL_DECL
)
11494 warning_at (loc
, OPT_Wreturn_local_addr
,
11495 "function returns address of label");
11498 warning_at (loc
, OPT_Wreturn_local_addr
,
11499 "function returns address of local variable");
11500 tree zero
= build_zero_cst (TREE_TYPE (res
));
11501 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
11513 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
11514 SET_EXPR_LOCATION (retval
, loc
);
11516 if (warn_sequence_point
)
11517 verify_sequence_points (retval
);
11520 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
11522 suppress_warning (ret_stmt
, OPT_Wreturn_type
);
11523 return add_stmt (ret_stmt
);
11527 /* The SWITCH_STMT being built. */
11530 /* The original type of the testing expression, i.e. before the
11531 default conversion is applied. */
11534 /* A splay-tree mapping the low element of a case range to the high
11535 element, or NULL_TREE if there is no high element. Used to
11536 determine whether or not a new case label duplicates an old case
11537 label. We need a tree, rather than simply a hash table, because
11538 of the GNU case range extension. */
11541 /* The bindings at the point of the switch. This is used for
11542 warnings crossing decls when branching to a case label. */
11543 struct c_spot_bindings
*bindings
;
11545 /* Whether the switch includes any break statements. */
11546 bool break_stmt_seen_p
;
11548 /* The next node on the stack. */
11549 struct c_switch
*next
;
11551 /* Remember whether the controlling expression had boolean type
11552 before integer promotions for the sake of -Wswitch-bool. */
11556 /* A stack of the currently active switch statements. The innermost
11557 switch statement is on the top of the stack. There is no need to
11558 mark the stack for garbage collection because it is only active
11559 during the processing of the body of a function, and we never
11560 collect at that point. */
11562 struct c_switch
*c_switch_stack
;
11564 /* Start a C switch statement, testing expression EXP. Return the new
11565 SWITCH_STMT. SWITCH_LOC is the location of the `switch'.
11566 SWITCH_COND_LOC is the location of the switch's condition.
11567 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
11570 c_start_switch (location_t switch_loc
,
11571 location_t switch_cond_loc
,
11572 tree exp
, bool explicit_cast_p
)
11574 tree orig_type
= error_mark_node
;
11575 bool bool_cond_p
= false;
11576 struct c_switch
*cs
;
11578 if (exp
!= error_mark_node
)
11580 orig_type
= TREE_TYPE (exp
);
11582 if (!INTEGRAL_TYPE_P (orig_type
))
11584 if (orig_type
!= error_mark_node
)
11586 error_at (switch_cond_loc
, "switch quantity not an integer");
11587 orig_type
= error_mark_node
;
11589 exp
= integer_zero_node
;
11593 tree type
= TYPE_MAIN_VARIANT (orig_type
);
11596 /* Warn if the condition has boolean value. */
11597 while (TREE_CODE (e
) == COMPOUND_EXPR
)
11598 e
= TREE_OPERAND (e
, 1);
11600 if ((C_BOOLEAN_TYPE_P (type
)
11601 || truth_value_p (TREE_CODE (e
)))
11602 /* Explicit cast to int suppresses this warning. */
11603 && !(TREE_CODE (type
) == INTEGER_TYPE
11604 && explicit_cast_p
))
11605 bool_cond_p
= true;
11607 if (!in_system_header_at (input_location
)
11608 && (type
== long_integer_type_node
11609 || type
== long_unsigned_type_node
))
11610 warning_at (switch_cond_loc
,
11611 OPT_Wtraditional
, "%<long%> switch expression not "
11612 "converted to %<int%> in ISO C");
11614 exp
= c_fully_fold (exp
, false, NULL
);
11615 exp
= default_conversion (exp
);
11617 if (warn_sequence_point
)
11618 verify_sequence_points (exp
);
11622 /* Add this new SWITCH_STMT to the stack. */
11623 cs
= XNEW (struct c_switch
);
11624 cs
->switch_stmt
= build_stmt (switch_loc
, SWITCH_STMT
, exp
,
11625 NULL_TREE
, orig_type
, NULL_TREE
);
11626 cs
->orig_type
= orig_type
;
11627 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
11628 cs
->bindings
= c_get_switch_bindings ();
11629 cs
->break_stmt_seen_p
= false;
11630 cs
->bool_cond_p
= bool_cond_p
;
11631 cs
->next
= c_switch_stack
;
11632 c_switch_stack
= cs
;
11634 return add_stmt (cs
->switch_stmt
);
11637 /* Process a case label at location LOC, with attributes ATTRS. */
11640 do_case (location_t loc
, tree low_value
, tree high_value
, tree attrs
)
11642 tree label
= NULL_TREE
;
11644 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
11646 low_value
= c_fully_fold (low_value
, false, NULL
);
11647 if (TREE_CODE (low_value
) == INTEGER_CST
)
11648 pedwarn (loc
, OPT_Wpedantic
,
11649 "case label is not an integer constant expression");
11652 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
11654 high_value
= c_fully_fold (high_value
, false, NULL
);
11655 if (TREE_CODE (high_value
) == INTEGER_CST
)
11656 pedwarn (input_location
, OPT_Wpedantic
,
11657 "case label is not an integer constant expression");
11660 if (c_switch_stack
== NULL
)
11663 error_at (loc
, "case label not within a switch statement");
11665 error_at (loc
, "%<default%> label not within a switch statement");
11669 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
11670 EXPR_LOCATION (c_switch_stack
->switch_stmt
),
11674 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
11675 SWITCH_STMT_COND (c_switch_stack
->switch_stmt
),
11676 low_value
, high_value
, attrs
);
11677 if (label
== error_mark_node
)
11682 /* Finish the switch statement. TYPE is the original type of the
11683 controlling expression of the switch, or NULL_TREE. */
11686 c_finish_switch (tree body
, tree type
)
11688 struct c_switch
*cs
= c_switch_stack
;
11689 location_t switch_location
;
11691 SWITCH_STMT_BODY (cs
->switch_stmt
) = body
;
11693 /* Emit warnings as needed. */
11694 switch_location
= EXPR_LOCATION (cs
->switch_stmt
);
11695 c_do_switch_warnings (cs
->cases
, switch_location
,
11696 type
? type
: SWITCH_STMT_TYPE (cs
->switch_stmt
),
11697 SWITCH_STMT_COND (cs
->switch_stmt
), cs
->bool_cond_p
);
11698 if (c_switch_covers_all_cases_p (cs
->cases
,
11699 SWITCH_STMT_TYPE (cs
->switch_stmt
)))
11700 SWITCH_STMT_ALL_CASES_P (cs
->switch_stmt
) = 1;
11701 SWITCH_STMT_NO_BREAK_P (cs
->switch_stmt
) = !cs
->break_stmt_seen_p
;
11703 /* Pop the stack. */
11704 c_switch_stack
= cs
->next
;
11705 splay_tree_delete (cs
->cases
);
11706 c_release_switch_bindings (cs
->bindings
);
11710 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
11711 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11715 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
11720 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
11721 SET_EXPR_LOCATION (stmt
, if_locus
);
11726 c_finish_bc_stmt (location_t loc
, tree label
, bool is_break
)
11728 /* In switch statements break is sometimes stylistically used after
11729 a return statement. This can lead to spurious warnings about
11730 control reaching the end of a non-void function when it is
11731 inlined. Note that we are calling block_may_fallthru with
11732 language specific tree nodes; this works because
11733 block_may_fallthru returns true when given something it does not
11735 bool skip
= !block_may_fallthru (cur_stmt_list
);
11738 switch (in_statement
)
11741 error_at (loc
, "break statement not within loop or switch");
11744 error_at (loc
, "invalid exit from OpenMP structured block");
11747 error_at (loc
, "break statement used with OpenMP for loop");
11749 case IN_ITERATION_STMT
:
11750 case IN_OBJC_FOREACH
:
11753 gcc_assert (in_statement
& IN_SWITCH_STMT
);
11754 c_switch_stack
->break_stmt_seen_p
= true;
11758 switch (in_statement
& ~IN_SWITCH_STMT
)
11761 error_at (loc
, "continue statement not within a loop");
11764 error_at (loc
, "invalid exit from OpenMP structured block");
11766 case IN_ITERATION_STMT
:
11768 case IN_OBJC_FOREACH
:
11771 gcc_unreachable ();
11776 else if ((in_statement
& IN_OBJC_FOREACH
)
11777 && !(is_break
&& (in_statement
& IN_SWITCH_STMT
)))
11779 /* The foreach expander produces low-level code using gotos instead
11780 of a structured loop construct. */
11781 gcc_assert (label
);
11782 return add_stmt (build_stmt (loc
, GOTO_EXPR
, label
));
11784 return add_stmt (build_stmt (loc
, (is_break
? BREAK_STMT
: CONTINUE_STMT
)));
11787 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11790 emit_side_effect_warnings (location_t loc
, tree expr
)
11792 maybe_warn_nodiscard (loc
, expr
);
11793 if (!warn_unused_value
)
11795 if (expr
== error_mark_node
)
11797 else if (!TREE_SIDE_EFFECTS (expr
))
11799 if (!VOID_TYPE_P (TREE_TYPE (expr
))
11800 && !warning_suppressed_p (expr
, OPT_Wunused_value
))
11801 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
11803 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
11806 location_t cloc
= loc
;
11807 while (TREE_CODE (r
) == COMPOUND_EXPR
)
11809 if (EXPR_HAS_LOCATION (r
))
11810 cloc
= EXPR_LOCATION (r
);
11811 r
= TREE_OPERAND (r
, 1);
11813 if (!TREE_SIDE_EFFECTS (r
)
11814 && !VOID_TYPE_P (TREE_TYPE (r
))
11815 && !CONVERT_EXPR_P (r
)
11816 && !warning_suppressed_p (r
, OPT_Wunused_value
)
11817 && !warning_suppressed_p (expr
, OPT_Wunused_value
))
11818 warning_at (cloc
, OPT_Wunused_value
,
11819 "right-hand operand of comma expression has no effect");
11822 warn_if_unused_value (expr
, loc
);
11825 /* Process an expression as if it were a complete statement. Emit
11826 diagnostics, but do not call ADD_STMT. LOC is the location of the
11830 c_process_expr_stmt (location_t loc
, tree expr
)
11837 expr
= c_fully_fold (expr
, false, NULL
);
11839 if (warn_sequence_point
)
11840 verify_sequence_points (expr
);
11842 if (TREE_TYPE (expr
) != error_mark_node
11843 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
11844 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
11845 error_at (loc
, "expression statement has incomplete type");
11847 /* If we're not processing a statement expression, warn about unused values.
11848 Warnings for statement expressions will be emitted later, once we figure
11849 out which is the result. */
11850 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
11851 && (warn_unused_value
|| warn_unused_result
))
11852 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr
, loc
), expr
);
11855 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
11856 exprv
= TREE_OPERAND (exprv
, 1);
11857 while (CONVERT_EXPR_P (exprv
))
11858 exprv
= TREE_OPERAND (exprv
, 0);
11860 || handled_component_p (exprv
)
11861 || TREE_CODE (exprv
) == ADDR_EXPR
)
11862 mark_exp_read (exprv
);
11864 /* If the expression is not of a type to which we cannot assign a line
11865 number, wrap the thing in a no-op NOP_EXPR. */
11866 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
11868 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11869 SET_EXPR_LOCATION (expr
, loc
);
11875 /* Emit an expression as a statement. LOC is the location of the
11879 c_finish_expr_stmt (location_t loc
, tree expr
)
11882 return add_stmt (c_process_expr_stmt (loc
, expr
));
11887 /* Do the opposite and emit a statement as an expression. To begin,
11888 create a new binding level and return it. */
11891 c_begin_stmt_expr (void)
11895 /* We must force a BLOCK for this level so that, if it is not expanded
11896 later, there is a way to turn off the entire subtree of blocks that
11897 are contained in it. */
11898 keep_next_level ();
11899 ret
= c_begin_compound_stmt (true);
11901 c_bindings_start_stmt_expr (c_switch_stack
== NULL
11903 : c_switch_stack
->bindings
);
11905 /* Mark the current statement list as belonging to a statement list. */
11906 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
11911 /* LOC is the location of the compound statement to which this body
11915 c_finish_stmt_expr (location_t loc
, tree body
)
11917 tree last
, type
, tmp
, val
;
11920 body
= c_end_compound_stmt (loc
, body
, true);
11922 c_bindings_end_stmt_expr (c_switch_stack
== NULL
11924 : c_switch_stack
->bindings
);
11926 /* Locate the last statement in BODY. See c_end_compound_stmt
11927 about always returning a BIND_EXPR. */
11928 last_p
= &BIND_EXPR_BODY (body
);
11929 last
= BIND_EXPR_BODY (body
);
11931 continue_searching
:
11932 if (TREE_CODE (last
) == STATEMENT_LIST
)
11934 tree_stmt_iterator l
= tsi_last (last
);
11936 while (!tsi_end_p (l
) && TREE_CODE (tsi_stmt (l
)) == DEBUG_BEGIN_STMT
)
11939 /* This can happen with degenerate cases like ({ }). No value. */
11943 /* If we're supposed to generate side effects warnings, process
11944 all of the statements except the last. */
11945 if (warn_unused_value
|| warn_unused_result
)
11947 for (tree_stmt_iterator i
= tsi_start (last
);
11948 tsi_stmt (i
) != tsi_stmt (l
); tsi_next (&i
))
11951 tree t
= tsi_stmt (i
);
11953 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
11954 emit_side_effect_warnings (tloc
, t
);
11957 last_p
= tsi_stmt_ptr (l
);
11961 /* If the end of the list is exception related, then the list was split
11962 by a call to push_cleanup. Continue searching. */
11963 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
11964 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
11966 last_p
= &TREE_OPERAND (last
, 0);
11968 goto continue_searching
;
11971 if (last
== error_mark_node
)
11974 /* In the case that the BIND_EXPR is not necessary, return the
11975 expression out from inside it. */
11976 if ((last
== BIND_EXPR_BODY (body
)
11977 /* Skip nested debug stmts. */
11978 || last
== expr_first (BIND_EXPR_BODY (body
)))
11979 && BIND_EXPR_VARS (body
) == NULL
)
11981 /* Even if this looks constant, do not allow it in a constant
11983 last
= c_wrap_maybe_const (last
, true);
11984 /* Do not warn if the return value of a statement expression is
11986 suppress_warning (last
, OPT_Wunused
);
11990 /* Extract the type of said expression. */
11991 type
= TREE_TYPE (last
);
11993 /* If we're not returning a value at all, then the BIND_EXPR that
11994 we already have is a fine expression to return. */
11995 if (!type
|| VOID_TYPE_P (type
))
11998 /* Now that we've located the expression containing the value, it seems
11999 silly to make voidify_wrapper_expr repeat the process. Create a
12000 temporary of the appropriate type and stick it in a TARGET_EXPR. */
12001 tmp
= create_tmp_var_raw (type
);
12003 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
12004 tree_expr_nonnegative_p giving up immediately. */
12006 if (TREE_CODE (val
) == NOP_EXPR
12007 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
12008 val
= TREE_OPERAND (val
, 0);
12010 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
12011 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
12014 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
12015 SET_EXPR_LOCATION (t
, loc
);
12020 /* Begin and end compound statements. This is as simple as pushing
12021 and popping new statement lists from the tree. */
12024 c_begin_compound_stmt (bool do_scope
)
12026 tree stmt
= push_stmt_list ();
12032 /* End a compound statement. STMT is the statement. LOC is the
12033 location of the compound statement-- this is usually the location
12034 of the opening brace. */
12037 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
12043 if (c_dialect_objc ())
12044 objc_clear_super_receiver ();
12045 block
= pop_scope ();
12048 stmt
= pop_stmt_list (stmt
);
12049 stmt
= c_build_bind_expr (loc
, block
, stmt
);
12051 /* If this compound statement is nested immediately inside a statement
12052 expression, then force a BIND_EXPR to be created. Otherwise we'll
12053 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
12054 STATEMENT_LISTs merge, and thus we can lose track of what statement
12055 was really last. */
12056 if (building_stmt_list_p ()
12057 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
12058 && TREE_CODE (stmt
) != BIND_EXPR
)
12060 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
12061 TREE_SIDE_EFFECTS (stmt
) = 1;
12062 SET_EXPR_LOCATION (stmt
, loc
);
12068 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
12069 when the current scope is exited. EH_ONLY is true when this is not
12070 meant to apply to normal control flow transfer. */
12073 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
12075 enum tree_code code
;
12079 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
12080 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
12082 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
12083 list
= push_stmt_list ();
12084 TREE_OPERAND (stmt
, 0) = list
;
12085 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
12088 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
12089 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
12092 build_vec_cmp (tree_code code
, tree type
,
12093 tree arg0
, tree arg1
)
12095 tree zero_vec
= build_zero_cst (type
);
12096 tree minus_one_vec
= build_minus_one_cst (type
);
12097 tree cmp_type
= truth_type_for (TREE_TYPE (arg0
));
12098 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
12099 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
12102 /* Possibly warn about an address of OP never being NULL in a comparison
12103 operation CODE involving null. */
12106 maybe_warn_for_null_address (location_t loc
, tree op
, tree_code code
)
12108 /* Prevent warnings issued for macro expansion. */
12110 || warning_suppressed_p (op
, OPT_Waddress
)
12111 || from_macro_expansion_at (loc
))
12114 if (TREE_CODE (op
) == NOP_EXPR
)
12116 /* Allow casts to intptr_t to suppress the warning. */
12117 tree type
= TREE_TYPE (op
);
12118 if (TREE_CODE (type
) == INTEGER_TYPE
)
12120 op
= TREE_OPERAND (op
, 0);
12123 if (TREE_CODE (op
) == POINTER_PLUS_EXPR
)
12125 /* Allow a cast to void* to suppress the warning. */
12126 tree type
= TREE_TYPE (TREE_TYPE (op
));
12127 if (VOID_TYPE_P (type
))
12130 /* Adding any value to a null pointer, including zero, is undefined
12131 in C. This includes the expression &p[0] where p is the null
12132 pointer, although &p[0] will have been folded to p by this point
12133 and so not diagnosed. */
12134 if (code
== EQ_EXPR
)
12135 warning_at (loc
, OPT_Waddress
,
12136 "the comparison will always evaluate as %<false%> "
12137 "for the pointer operand in %qE must not be NULL",
12140 warning_at (loc
, OPT_Waddress
,
12141 "the comparison will always evaluate as %<true%> "
12142 "for the pointer operand in %qE must not be NULL",
12148 if (TREE_CODE (op
) != ADDR_EXPR
)
12151 op
= TREE_OPERAND (op
, 0);
12153 if (TREE_CODE (op
) == IMAGPART_EXPR
12154 || TREE_CODE (op
) == REALPART_EXPR
)
12156 /* The address of either complex part may not be null. */
12157 if (code
== EQ_EXPR
)
12158 warning_at (loc
, OPT_Waddress
,
12159 "the comparison will always evaluate as %<false%> "
12160 "for the address of %qE will never be NULL",
12163 warning_at (loc
, OPT_Waddress
,
12164 "the comparison will always evaluate as %<true%> "
12165 "for the address of %qE will never be NULL",
12170 /* Set to true in the loop below if OP dereferences is operand.
12171 In such a case the ultimate target need not be a decl for
12172 the null [in]equality test to be constant. */
12173 bool deref
= false;
12175 /* Get the outermost array or object, or member. */
12176 while (handled_component_p (op
))
12178 if (TREE_CODE (op
) == COMPONENT_REF
)
12180 /* Get the member (its address is never null). */
12181 op
= TREE_OPERAND (op
, 1);
12185 /* Get the outer array/object to refer to in the warning. */
12186 op
= TREE_OPERAND (op
, 0);
12190 if ((!deref
&& !decl_with_nonnull_addr_p (op
))
12191 || from_macro_expansion_at (loc
))
12195 if (code
== EQ_EXPR
)
12196 w
= warning_at (loc
, OPT_Waddress
,
12197 "the comparison will always evaluate as %<false%> "
12198 "for the address of %qE will never be NULL",
12201 w
= warning_at (loc
, OPT_Waddress
,
12202 "the comparison will always evaluate as %<true%> "
12203 "for the address of %qE will never be NULL",
12206 if (w
&& DECL_P (op
))
12207 inform (DECL_SOURCE_LOCATION (op
), "%qD declared here", op
);
12210 /* Build a binary-operation expression without default conversions.
12211 CODE is the kind of expression to build.
12212 LOCATION is the operator's location.
12213 This function differs from `build' in several ways:
12214 the data type of the result is computed and recorded in it,
12215 warnings are generated if arg data types are invalid,
12216 special handling for addition and subtraction of pointers is known,
12217 and some optimization is done (operations on narrow ints
12218 are done in the narrower type when that gives the same result).
12219 Constant folding is also done before the result is returned.
12221 Note that the operands will never have enumeral types, or function
12222 or array types, because either they will have the default conversions
12223 performed or they have both just been converted to some other type in which
12224 the arithmetic is to be done. */
12227 build_binary_op (location_t location
, enum tree_code code
,
12228 tree orig_op0
, tree orig_op1
, bool convert_p
)
12230 tree type0
, type1
, orig_type0
, orig_type1
;
12232 enum tree_code code0
, code1
;
12234 tree ret
= error_mark_node
;
12235 const char *invalid_op_diag
;
12236 bool op0_int_operands
, op1_int_operands
;
12237 bool int_const
, int_const_or_overflow
, int_operands
;
12239 /* Expression code to give to the expression when it is built.
12240 Normally this is CODE, which is what the caller asked for,
12241 but in some special cases we change it. */
12242 enum tree_code resultcode
= code
;
12244 /* Data type in which the computation is to be performed.
12245 In the simplest cases this is the common type of the arguments. */
12246 tree result_type
= NULL
;
12248 /* When the computation is in excess precision, the type of the
12249 final EXCESS_PRECISION_EXPR. */
12250 tree semantic_result_type
= NULL
;
12252 /* Nonzero means operands have already been type-converted
12253 in whatever way is necessary.
12254 Zero means they need to be converted to RESULT_TYPE. */
12257 /* Nonzero means create the expression with this type, rather than
12259 tree build_type
= NULL_TREE
;
12261 /* Nonzero means after finally constructing the expression
12262 convert it to this type. */
12263 tree final_type
= NULL_TREE
;
12265 /* Nonzero if this is an operation like MIN or MAX which can
12266 safely be computed in short if both args are promoted shorts.
12267 Also implies COMMON.
12268 -1 indicates a bitwise operation; this makes a difference
12269 in the exact conditions for when it is safe to do the operation
12270 in a narrower mode. */
12273 /* Nonzero if this is a comparison operation;
12274 if both args are promoted shorts, compare the original shorts.
12275 Also implies COMMON. */
12276 int short_compare
= 0;
12278 /* Nonzero if this is a right-shift operation, which can be computed on the
12279 original short and then promoted if the operand is a promoted short. */
12280 int short_shift
= 0;
12282 /* Nonzero means set RESULT_TYPE to the common type of the args. */
12285 /* True means types are compatible as far as ObjC is concerned. */
12288 /* True means this is an arithmetic operation that may need excess
12290 bool may_need_excess_precision
;
12292 /* True means this is a boolean operation that converts both its
12293 operands to truth-values. */
12294 bool boolean_op
= false;
12296 /* Remember whether we're doing / or %. */
12297 bool doing_div_or_mod
= false;
12299 /* Remember whether we're doing << or >>. */
12300 bool doing_shift
= false;
12302 /* Tree holding instrumentation expression. */
12303 tree instrument_expr
= NULL
;
12305 if (location
== UNKNOWN_LOCATION
)
12306 location
= input_location
;
12311 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
12312 if (op0_int_operands
)
12313 op0
= remove_c_maybe_const_expr (op0
);
12314 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
12315 if (op1_int_operands
)
12316 op1
= remove_c_maybe_const_expr (op1
);
12317 int_operands
= (op0_int_operands
&& op1_int_operands
);
12320 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
12321 && TREE_CODE (orig_op1
) == INTEGER_CST
);
12322 int_const
= (int_const_or_overflow
12323 && !TREE_OVERFLOW (orig_op0
)
12324 && !TREE_OVERFLOW (orig_op1
));
12327 int_const
= int_const_or_overflow
= false;
12329 /* Do not apply default conversion in mixed vector/scalar expression. */
12331 && VECTOR_TYPE_P (TREE_TYPE (op0
)) == VECTOR_TYPE_P (TREE_TYPE (op1
)))
12333 op0
= default_conversion (op0
);
12334 op1
= default_conversion (op1
);
12337 orig_type0
= type0
= TREE_TYPE (op0
);
12339 orig_type1
= type1
= TREE_TYPE (op1
);
12341 /* The expression codes of the data types of the arguments tell us
12342 whether the arguments are integers, floating, pointers, etc. */
12343 code0
= TREE_CODE (type0
);
12344 code1
= TREE_CODE (type1
);
12346 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
12347 STRIP_TYPE_NOPS (op0
);
12348 STRIP_TYPE_NOPS (op1
);
12350 /* If an error was already reported for one of the arguments,
12351 avoid reporting another error. */
12353 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
12354 return error_mark_node
;
12356 if (code0
== POINTER_TYPE
12357 && reject_gcc_builtin (op0
, EXPR_LOCATION (orig_op0
)))
12358 return error_mark_node
;
12360 if (code1
== POINTER_TYPE
12361 && reject_gcc_builtin (op1
, EXPR_LOCATION (orig_op1
)))
12362 return error_mark_node
;
12364 if ((invalid_op_diag
12365 = targetm
.invalid_binary_op (code
, type0
, type1
)))
12367 error_at (location
, invalid_op_diag
);
12368 return error_mark_node
;
12376 case TRUNC_DIV_EXPR
:
12377 case CEIL_DIV_EXPR
:
12378 case FLOOR_DIV_EXPR
:
12379 case ROUND_DIV_EXPR
:
12380 case EXACT_DIV_EXPR
:
12381 may_need_excess_precision
= true;
12390 /* Excess precision for implicit conversions of integers to
12391 floating point in C11 and later. */
12392 may_need_excess_precision
= (flag_isoc11
12393 && (ANY_INTEGRAL_TYPE_P (type0
)
12394 || ANY_INTEGRAL_TYPE_P (type1
)));
12398 may_need_excess_precision
= false;
12401 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
12403 op0
= TREE_OPERAND (op0
, 0);
12404 type0
= TREE_TYPE (op0
);
12406 else if (may_need_excess_precision
12407 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
12410 op0
= convert (eptype
, op0
);
12412 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
12414 op1
= TREE_OPERAND (op1
, 0);
12415 type1
= TREE_TYPE (op1
);
12417 else if (may_need_excess_precision
12418 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
12421 op1
= convert (eptype
, op1
);
12424 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
12426 /* In case when one of the operands of the binary operation is
12427 a vector and another is a scalar -- convert scalar to vector. */
12428 if ((gnu_vector_type_p (type0
) && code1
!= VECTOR_TYPE
)
12429 || (gnu_vector_type_p (type1
) && code0
!= VECTOR_TYPE
))
12431 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, orig_op0
,
12434 switch (convert_flag
)
12437 return error_mark_node
;
12440 bool maybe_const
= true;
12442 sc
= c_fully_fold (op0
, false, &maybe_const
);
12443 sc
= save_expr (sc
);
12444 sc
= convert (TREE_TYPE (type1
), sc
);
12445 op0
= build_vector_from_val (type1
, sc
);
12447 op0
= c_wrap_maybe_const (op0
, true);
12448 orig_type0
= type0
= TREE_TYPE (op0
);
12449 code0
= TREE_CODE (type0
);
12453 case stv_secondarg
:
12455 bool maybe_const
= true;
12457 sc
= c_fully_fold (op1
, false, &maybe_const
);
12458 sc
= save_expr (sc
);
12459 sc
= convert (TREE_TYPE (type0
), sc
);
12460 op1
= build_vector_from_val (type0
, sc
);
12462 op1
= c_wrap_maybe_const (op1
, true);
12463 orig_type1
= type1
= TREE_TYPE (op1
);
12464 code1
= TREE_CODE (type1
);
12476 /* Handle the pointer + int case. */
12477 if (code0
== POINTER_TYPE
12478 && (code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
))
12480 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
12481 goto return_build_binary_op
;
12483 else if (code1
== POINTER_TYPE
12484 && (code0
== INTEGER_TYPE
|| code0
== BITINT_TYPE
))
12486 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
12487 goto return_build_binary_op
;
12494 /* Subtraction of two similar pointers.
12495 We must subtract them as integers, then divide by object size. */
12496 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
12497 && comp_target_types (location
, type0
, type1
))
12499 ret
= pointer_diff (location
, op0
, op1
, &instrument_expr
);
12500 goto return_build_binary_op
;
12502 /* Handle pointer minus int. Just like pointer plus int. */
12503 else if (code0
== POINTER_TYPE
12504 && (code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
))
12506 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
12507 goto return_build_binary_op
;
12517 case TRUNC_DIV_EXPR
:
12518 case CEIL_DIV_EXPR
:
12519 case FLOOR_DIV_EXPR
:
12520 case ROUND_DIV_EXPR
:
12521 case EXACT_DIV_EXPR
:
12522 doing_div_or_mod
= true;
12523 warn_for_div_by_zero (location
, op1
);
12525 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
12526 || code0
== FIXED_POINT_TYPE
|| code0
== BITINT_TYPE
12527 || code0
== COMPLEX_TYPE
12528 || gnu_vector_type_p (type0
))
12529 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
12530 || code1
== FIXED_POINT_TYPE
|| code1
== BITINT_TYPE
12531 || code1
== COMPLEX_TYPE
12532 || gnu_vector_type_p (type1
)))
12534 enum tree_code tcode0
= code0
, tcode1
= code1
;
12536 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
12537 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
12538 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
12539 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
12541 if (!(((tcode0
== INTEGER_TYPE
|| tcode0
== BITINT_TYPE
)
12542 && (tcode1
== INTEGER_TYPE
|| tcode1
== BITINT_TYPE
))
12543 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
12544 resultcode
= RDIV_EXPR
;
12546 /* Although it would be tempting to shorten always here, that
12547 loses on some targets, since the modulo instruction is
12548 undefined if the quotient can't be represented in the
12549 computation mode. We shorten only if unsigned or if
12550 dividing by something we know != -1. */
12551 shorten
= may_shorten_divmod (op0
, op1
);
12559 if ((code0
== INTEGER_TYPE
|| code0
== BITINT_TYPE
)
12560 && (code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
))
12562 /* Allow vector types which are not floating point types. */
12563 else if (gnu_vector_type_p (type0
)
12564 && gnu_vector_type_p (type1
)
12565 && !VECTOR_FLOAT_TYPE_P (type0
)
12566 && !VECTOR_FLOAT_TYPE_P (type1
))
12570 case TRUNC_MOD_EXPR
:
12571 case FLOOR_MOD_EXPR
:
12572 doing_div_or_mod
= true;
12573 warn_for_div_by_zero (location
, op1
);
12575 if (gnu_vector_type_p (type0
)
12576 && gnu_vector_type_p (type1
)
12577 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
12578 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
12580 else if ((code0
== INTEGER_TYPE
|| code0
== BITINT_TYPE
)
12581 && (code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
))
12583 /* Although it would be tempting to shorten always here, that loses
12584 on some targets, since the modulo instruction is undefined if the
12585 quotient can't be represented in the computation mode. We shorten
12586 only if unsigned or if dividing by something we know != -1. */
12587 shorten
= may_shorten_divmod (op0
, op1
);
12592 case TRUTH_ANDIF_EXPR
:
12593 case TRUTH_ORIF_EXPR
:
12594 case TRUTH_AND_EXPR
:
12595 case TRUTH_OR_EXPR
:
12596 case TRUTH_XOR_EXPR
:
12597 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
12598 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
12599 || code0
== FIXED_POINT_TYPE
|| code0
== NULLPTR_TYPE
12600 || code0
== BITINT_TYPE
)
12601 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
12602 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
12603 || code1
== FIXED_POINT_TYPE
|| code1
== NULLPTR_TYPE
12604 || code1
== BITINT_TYPE
))
12606 /* Result of these operations is always an int,
12607 but that does not mean the operands should be
12608 converted to ints! */
12609 result_type
= integer_type_node
;
12610 if (op0_int_operands
)
12612 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
12613 op0
= remove_c_maybe_const_expr (op0
);
12616 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
12617 if (op1_int_operands
)
12619 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
12620 op1
= remove_c_maybe_const_expr (op1
);
12623 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
12627 if (code
== TRUTH_ANDIF_EXPR
)
12629 int_const_or_overflow
= (int_operands
12630 && TREE_CODE (orig_op0
) == INTEGER_CST
12631 && (op0
== truthvalue_false_node
12632 || TREE_CODE (orig_op1
) == INTEGER_CST
));
12633 int_const
= (int_const_or_overflow
12634 && !TREE_OVERFLOW (orig_op0
)
12635 && (op0
== truthvalue_false_node
12636 || !TREE_OVERFLOW (orig_op1
)));
12638 else if (code
== TRUTH_ORIF_EXPR
)
12640 int_const_or_overflow
= (int_operands
12641 && TREE_CODE (orig_op0
) == INTEGER_CST
12642 && (op0
== truthvalue_true_node
12643 || TREE_CODE (orig_op1
) == INTEGER_CST
));
12644 int_const
= (int_const_or_overflow
12645 && !TREE_OVERFLOW (orig_op0
)
12646 && (op0
== truthvalue_true_node
12647 || !TREE_OVERFLOW (orig_op1
)));
12651 /* Shift operations: result has same type as first operand;
12652 always convert second operand to int.
12653 Also set SHORT_SHIFT if shifting rightward. */
12656 if (gnu_vector_type_p (type0
)
12657 && gnu_vector_type_p (type1
)
12658 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
12659 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
12660 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
12661 TYPE_VECTOR_SUBPARTS (type1
)))
12663 result_type
= type0
;
12666 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
12667 || code0
== BITINT_TYPE
12668 || (gnu_vector_type_p (type0
)
12669 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
))
12670 && (code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
))
12672 doing_shift
= true;
12673 if (TREE_CODE (op1
) == INTEGER_CST
)
12675 if (tree_int_cst_sgn (op1
) < 0)
12678 if (c_inhibit_evaluation_warnings
== 0)
12679 warning_at (location
, OPT_Wshift_count_negative
,
12680 "right shift count is negative");
12682 else if (code0
== VECTOR_TYPE
)
12684 if (compare_tree_int (op1
,
12685 TYPE_PRECISION (TREE_TYPE (type0
)))
12689 if (c_inhibit_evaluation_warnings
== 0)
12690 warning_at (location
, OPT_Wshift_count_overflow
,
12691 "right shift count >= width of vector element");
12696 if (!integer_zerop (op1
))
12699 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
12702 if (c_inhibit_evaluation_warnings
== 0)
12703 warning_at (location
, OPT_Wshift_count_overflow
,
12704 "right shift count >= width of type");
12709 /* Use the type of the value to be shifted. */
12710 result_type
= type0
;
12711 /* Avoid converting op1 to result_type later. */
12717 if (gnu_vector_type_p (type0
)
12718 && gnu_vector_type_p (type1
)
12719 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
12720 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
12721 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
12722 TYPE_VECTOR_SUBPARTS (type1
)))
12724 result_type
= type0
;
12727 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
12728 || code0
== BITINT_TYPE
12729 || (gnu_vector_type_p (type0
)
12730 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
))
12731 && (code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
))
12733 doing_shift
= true;
12734 if (TREE_CODE (op0
) == INTEGER_CST
12735 && tree_int_cst_sgn (op0
) < 0
12736 && !TYPE_OVERFLOW_WRAPS (type0
))
12738 /* Don't reject a left shift of a negative value in a context
12739 where a constant expression is needed in C90. */
12742 if (c_inhibit_evaluation_warnings
== 0)
12743 warning_at (location
, OPT_Wshift_negative_value
,
12744 "left shift of negative value");
12746 if (TREE_CODE (op1
) == INTEGER_CST
)
12748 if (tree_int_cst_sgn (op1
) < 0)
12751 if (c_inhibit_evaluation_warnings
== 0)
12752 warning_at (location
, OPT_Wshift_count_negative
,
12753 "left shift count is negative");
12755 else if (code0
== VECTOR_TYPE
)
12757 if (compare_tree_int (op1
,
12758 TYPE_PRECISION (TREE_TYPE (type0
)))
12762 if (c_inhibit_evaluation_warnings
== 0)
12763 warning_at (location
, OPT_Wshift_count_overflow
,
12764 "left shift count >= width of vector element");
12767 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
12770 if (c_inhibit_evaluation_warnings
== 0)
12771 warning_at (location
, OPT_Wshift_count_overflow
,
12772 "left shift count >= width of type");
12774 else if (TREE_CODE (op0
) == INTEGER_CST
12775 && maybe_warn_shift_overflow (location
, op0
, op1
)
12780 /* Use the type of the value to be shifted. */
12781 result_type
= type0
;
12782 /* Avoid converting op1 to result_type later. */
12789 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
12792 if (!vector_types_compatible_elements_p (type0
, type1
))
12794 error_at (location
, "comparing vectors with different "
12796 return error_mark_node
;
12799 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
12800 TYPE_VECTOR_SUBPARTS (type1
)))
12802 error_at (location
, "comparing vectors with different "
12803 "number of elements");
12804 return error_mark_node
;
12807 /* It's not precisely specified how the usual arithmetic
12808 conversions apply to the vector types. Here, we use
12809 the unsigned type if one of the operands is signed and
12810 the other one is unsigned. */
12811 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
12813 if (!TYPE_UNSIGNED (type0
))
12814 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
12816 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
12817 warning_at (location
, OPT_Wsign_compare
, "comparison between "
12818 "types %qT and %qT", type0
, type1
);
12821 /* Always construct signed integer vector type. */
12822 intt
= c_common_type_for_size (GET_MODE_BITSIZE
12824 (TREE_TYPE (type0
))), 0);
12827 error_at (location
, "could not find an integer type "
12828 "of the same size as %qT",
12829 TREE_TYPE (type0
));
12830 return error_mark_node
;
12832 result_type
= build_opaque_vector_type (intt
,
12833 TYPE_VECTOR_SUBPARTS (type0
));
12835 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
12836 goto return_build_binary_op
;
12838 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
12839 warning_at (location
,
12841 "comparing floating-point with %<==%> or %<!=%> is unsafe");
12842 /* Result of comparison is always int,
12843 but don't convert the args to int! */
12844 build_type
= integer_type_node
;
12845 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== BITINT_TYPE
12846 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
12847 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
12848 || code1
== BITINT_TYPE
12849 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
12851 else if (code0
== POINTER_TYPE
12852 && (code1
== NULLPTR_TYPE
12853 || null_pointer_constant_p (orig_op1
)))
12855 maybe_warn_for_null_address (location
, op0
, code
);
12856 result_type
= type0
;
12858 else if (code1
== POINTER_TYPE
12859 && (code0
== NULLPTR_TYPE
12860 || null_pointer_constant_p (orig_op0
)))
12862 maybe_warn_for_null_address (location
, op1
, code
);
12863 result_type
= type1
;
12865 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
12867 tree tt0
= TREE_TYPE (type0
);
12868 tree tt1
= TREE_TYPE (type1
);
12869 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
12870 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
12871 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
12873 /* Anything compares with void *. void * compares with anything.
12874 Otherwise, the targets must be compatible
12875 and both must be object or both incomplete. */
12876 if (comp_target_types (location
, type0
, type1
))
12877 result_type
= common_pointer_type (type0
, type1
);
12878 else if (!addr_space_superset (as0
, as1
, &as_common
))
12880 error_at (location
, "comparison of pointers to "
12881 "disjoint address spaces");
12882 return error_mark_node
;
12884 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
12886 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
12887 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
12888 "comparison of %<void *%> with function pointer");
12890 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
12892 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
12893 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
12894 "comparison of %<void *%> with function pointer");
12897 /* Avoid warning about the volatile ObjC EH puts on decls. */
12899 pedwarn (location
, OPT_Wcompare_distinct_pointer_types
,
12900 "comparison of distinct pointer types lacks a cast");
12902 if (result_type
== NULL_TREE
)
12904 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
12905 result_type
= build_pointer_type
12906 (build_qualified_type (void_type_node
, qual
));
12909 else if (code0
== POINTER_TYPE
12910 && (code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
))
12912 result_type
= type0
;
12913 pedwarn (location
, 0, "comparison between pointer and integer");
12915 else if ((code0
== INTEGER_TYPE
|| code0
== BITINT_TYPE
)
12916 && code1
== POINTER_TYPE
)
12918 result_type
= type1
;
12919 pedwarn (location
, 0, "comparison between pointer and integer");
12921 /* 6.5.9: One of the following shall hold:
12922 -- both operands have type nullptr_t; */
12923 else if (code0
== NULLPTR_TYPE
&& code1
== NULLPTR_TYPE
)
12925 result_type
= nullptr_type_node
;
12926 /* No need to convert the operands to result_type later. */
12929 /* -- one operand has type nullptr_t and the other is a null pointer
12930 constant. We will have to convert the former to the type of the
12931 latter, because during gimplification we can't have mismatching
12932 comparison operand type. We convert from nullptr_t to the other
12933 type, since only nullptr_t can be converted to nullptr_t. Also,
12934 even a constant 0 is a null pointer constant, so we may have to
12935 create a pointer type from its type. */
12936 else if (code0
== NULLPTR_TYPE
&& null_pointer_constant_p (orig_op1
))
12937 result_type
= (INTEGRAL_TYPE_P (type1
)
12938 ? build_pointer_type (type1
) : type1
);
12939 else if (code1
== NULLPTR_TYPE
&& null_pointer_constant_p (orig_op0
))
12940 result_type
= (INTEGRAL_TYPE_P (type0
)
12941 ? build_pointer_type (type0
) : type0
);
12942 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0
))
12943 || truth_value_p (TREE_CODE (orig_op0
)))
12944 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1
))
12945 || truth_value_p (TREE_CODE (orig_op1
))))
12946 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
12953 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
12956 if (!vector_types_compatible_elements_p (type0
, type1
))
12958 error_at (location
, "comparing vectors with different "
12960 return error_mark_node
;
12963 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
12964 TYPE_VECTOR_SUBPARTS (type1
)))
12966 error_at (location
, "comparing vectors with different "
12967 "number of elements");
12968 return error_mark_node
;
12971 /* It's not precisely specified how the usual arithmetic
12972 conversions apply to the vector types. Here, we use
12973 the unsigned type if one of the operands is signed and
12974 the other one is unsigned. */
12975 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
12977 if (!TYPE_UNSIGNED (type0
))
12978 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
12980 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
12981 warning_at (location
, OPT_Wsign_compare
, "comparison between "
12982 "types %qT and %qT", type0
, type1
);
12985 /* Always construct signed integer vector type. */
12986 intt
= c_common_type_for_size (GET_MODE_BITSIZE
12988 (TREE_TYPE (type0
))), 0);
12991 error_at (location
, "could not find an integer type "
12992 "of the same size as %qT",
12993 TREE_TYPE (type0
));
12994 return error_mark_node
;
12996 result_type
= build_opaque_vector_type (intt
,
12997 TYPE_VECTOR_SUBPARTS (type0
));
12999 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
13000 goto return_build_binary_op
;
13002 build_type
= integer_type_node
;
13003 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
13004 || code0
== BITINT_TYPE
|| code0
== FIXED_POINT_TYPE
)
13005 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
13006 || code1
== BITINT_TYPE
|| code1
== FIXED_POINT_TYPE
))
13008 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
13010 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
13011 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
13012 addr_space_t as_common
;
13014 if (comp_target_types (location
, type0
, type1
))
13016 result_type
= common_pointer_type (type0
, type1
);
13017 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
13018 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
13019 pedwarn_c99 (location
, OPT_Wpedantic
,
13020 "comparison of complete and incomplete pointers");
13021 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
13022 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
13023 "ordered comparisons of pointers to functions");
13024 else if (null_pointer_constant_p (orig_op0
)
13025 || null_pointer_constant_p (orig_op1
))
13026 warning_at (location
, OPT_Wextra
,
13027 "ordered comparison of pointer with null pointer");
13030 else if (!addr_space_superset (as0
, as1
, &as_common
))
13032 error_at (location
, "comparison of pointers to "
13033 "disjoint address spaces");
13034 return error_mark_node
;
13038 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
13039 result_type
= build_pointer_type
13040 (build_qualified_type (void_type_node
, qual
));
13041 pedwarn (location
, OPT_Wcompare_distinct_pointer_types
,
13042 "comparison of distinct pointer types lacks a cast");
13045 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
13047 result_type
= type0
;
13049 pedwarn (location
, OPT_Wpedantic
,
13050 "ordered comparison of pointer with integer zero");
13051 else if (extra_warnings
)
13052 warning_at (location
, OPT_Wextra
,
13053 "ordered comparison of pointer with integer zero");
13055 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
13057 result_type
= type1
;
13059 pedwarn (location
, OPT_Wpedantic
,
13060 "ordered comparison of pointer with integer zero");
13061 else if (extra_warnings
)
13062 warning_at (location
, OPT_Wextra
,
13063 "ordered comparison of pointer with integer zero");
13065 else if (code0
== POINTER_TYPE
13066 && (code1
== INTEGER_TYPE
|| code1
== BITINT_TYPE
))
13068 result_type
= type0
;
13069 pedwarn (location
, 0, "comparison between pointer and integer");
13071 else if ((code0
== INTEGER_TYPE
|| code0
== BITINT_TYPE
)
13072 && code1
== POINTER_TYPE
)
13074 result_type
= type1
;
13075 pedwarn (location
, 0, "comparison between pointer and integer");
13078 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
13079 && current_function_decl
!= NULL_TREE
13080 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
13082 op0
= save_expr (op0
);
13083 op1
= save_expr (op1
);
13085 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
13086 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
13089 if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0
))
13090 || truth_value_p (TREE_CODE (orig_op0
)))
13091 ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1
))
13092 || truth_value_p (TREE_CODE (orig_op1
))))
13093 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
13098 /* Used for OpenMP atomics. */
13099 gcc_assert (flag_openmp
);
13104 gcc_unreachable ();
13107 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
13108 return error_mark_node
;
13110 if (gnu_vector_type_p (type0
)
13111 && gnu_vector_type_p (type1
)
13112 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
13113 || !vector_types_compatible_elements_p (type0
, type1
)))
13115 gcc_rich_location
richloc (location
);
13116 maybe_range_label_for_tree_type_mismatch
13117 label_for_op0 (orig_op0
, orig_op1
),
13118 label_for_op1 (orig_op1
, orig_op0
);
13119 richloc
.maybe_add_expr (orig_op0
, &label_for_op0
);
13120 richloc
.maybe_add_expr (orig_op1
, &label_for_op1
);
13121 binary_op_error (&richloc
, code
, type0
, type1
);
13122 return error_mark_node
;
13125 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
13126 || code0
== FIXED_POINT_TYPE
|| code0
== BITINT_TYPE
13127 || gnu_vector_type_p (type0
))
13128 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
13129 || code1
== FIXED_POINT_TYPE
|| code1
== BITINT_TYPE
13130 || gnu_vector_type_p (type1
)))
13132 bool first_complex
= (code0
== COMPLEX_TYPE
);
13133 bool second_complex
= (code1
== COMPLEX_TYPE
);
13134 int none_complex
= (!first_complex
&& !second_complex
);
13136 if (shorten
|| common
|| short_compare
)
13138 result_type
= c_common_type (type0
, type1
);
13139 do_warn_double_promotion (result_type
, type0
, type1
,
13140 "implicit conversion from %qT to %qT "
13141 "to match other operand of binary "
13144 if (result_type
== error_mark_node
)
13145 return error_mark_node
;
13148 if (first_complex
!= second_complex
13149 && (code
== PLUS_EXPR
13150 || code
== MINUS_EXPR
13151 || code
== MULT_EXPR
13152 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
13153 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
13154 && flag_signed_zeros
)
13156 /* An operation on mixed real/complex operands must be
13157 handled specially, but the language-independent code can
13158 more easily optimize the plain complex arithmetic if
13159 -fno-signed-zeros. */
13160 tree real_type
= TREE_TYPE (result_type
);
13162 if (type0
!= orig_type0
|| type1
!= orig_type1
)
13164 gcc_assert (may_need_excess_precision
&& common
);
13165 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
13169 if (TREE_TYPE (op0
) != result_type
)
13170 op0
= convert_and_check (location
, result_type
, op0
);
13171 if (TREE_TYPE (op1
) != real_type
)
13172 op1
= convert_and_check (location
, real_type
, op1
);
13176 if (TREE_TYPE (op0
) != real_type
)
13177 op0
= convert_and_check (location
, real_type
, op0
);
13178 if (TREE_TYPE (op1
) != result_type
)
13179 op1
= convert_and_check (location
, result_type
, op1
);
13181 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
13182 return error_mark_node
;
13185 op0
= save_expr (op0
);
13186 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
13188 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
13193 case TRUNC_DIV_EXPR
:
13194 op1
= save_expr (op1
);
13195 imag
= build2 (resultcode
, real_type
, imag
, op1
);
13196 /* Fall through. */
13199 real
= build2 (resultcode
, real_type
, real
, op1
);
13207 op1
= save_expr (op1
);
13208 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
13210 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
13215 op0
= save_expr (op0
);
13216 imag
= build2 (resultcode
, real_type
, op0
, imag
);
13217 /* Fall through. */
13219 real
= build2 (resultcode
, real_type
, op0
, real
);
13222 real
= build2 (resultcode
, real_type
, op0
, real
);
13223 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
13229 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
13230 goto return_build_binary_op
;
13233 /* For certain operations (which identify themselves by shorten != 0)
13234 if both args were extended from the same smaller type,
13235 do the arithmetic in that type and then extend.
13237 shorten !=0 and !=1 indicates a bitwise operation.
13238 For them, this optimization is safe only if
13239 both args are zero-extended or both are sign-extended.
13240 Otherwise, we might change the result.
13241 Eg, (short)-1 | (unsigned short)-1 is (int)-1
13242 but calculated in (unsigned short) it would be (unsigned short)-1. */
13244 if (shorten
&& none_complex
)
13246 final_type
= result_type
;
13247 result_type
= shorten_binary_op (result_type
, op0
, op1
,
13251 /* Shifts can be shortened if shifting right. */
13256 tree arg0
= get_narrower (op0
, &unsigned_arg
);
13258 final_type
= result_type
;
13260 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
13261 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
13263 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
13264 && tree_int_cst_sgn (op1
) > 0
13265 /* We can shorten only if the shift count is less than the
13266 number of bits in the smaller type size. */
13267 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
13268 /* We cannot drop an unsigned shift after sign-extension. */
13269 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
13271 /* Do an unsigned shift if the operand was zero-extended. */
13273 = c_common_signed_or_unsigned_type (unsigned_arg
,
13275 /* Convert value-to-be-shifted to that type. */
13276 if (TREE_TYPE (op0
) != result_type
)
13277 op0
= convert (result_type
, op0
);
13282 /* Comparison operations are shortened too but differently.
13283 They identify themselves by setting short_compare = 1. */
13287 /* Don't write &op0, etc., because that would prevent op0
13288 from being kept in a register.
13289 Instead, make copies of the our local variables and
13290 pass the copies by reference, then copy them back afterward. */
13291 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
13292 enum tree_code xresultcode
= resultcode
;
13294 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
13297 if (val
!= NULL_TREE
)
13300 goto return_build_binary_op
;
13303 op0
= xop0
, op1
= xop1
;
13305 resultcode
= xresultcode
;
13307 if (c_inhibit_evaluation_warnings
== 0 && !c_in_omp_for
)
13309 bool op0_maybe_const
= true;
13310 bool op1_maybe_const
= true;
13311 tree orig_op0_folded
, orig_op1_folded
;
13313 if (in_late_binary_op
)
13315 orig_op0_folded
= orig_op0
;
13316 orig_op1_folded
= orig_op1
;
13320 /* Fold for the sake of possible warnings, as in
13321 build_conditional_expr. This requires the
13322 "original" values to be folded, not just op0 and
13324 c_inhibit_evaluation_warnings
++;
13325 op0
= c_fully_fold (op0
, require_constant_value
,
13327 op1
= c_fully_fold (op1
, require_constant_value
,
13329 c_inhibit_evaluation_warnings
--;
13330 orig_op0_folded
= c_fully_fold (orig_op0
,
13331 require_constant_value
,
13333 orig_op1_folded
= c_fully_fold (orig_op1
,
13334 require_constant_value
,
13338 if (warn_sign_compare
)
13339 warn_for_sign_compare (location
, orig_op0_folded
,
13340 orig_op1_folded
, op0
, op1
,
13341 result_type
, resultcode
);
13342 if (!in_late_binary_op
&& !int_operands
)
13344 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
13345 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
13346 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
13347 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
13353 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
13354 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
13355 Then the expression will be built.
13356 It will be given type FINAL_TYPE if that is nonzero;
13357 otherwise, it will be given type RESULT_TYPE. */
13361 /* Favor showing any expression locations that are available. */
13362 op_location_t
oploc (location
, UNKNOWN_LOCATION
);
13363 binary_op_rich_location
richloc (oploc
, orig_op0
, orig_op1
, true);
13364 binary_op_error (&richloc
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
13365 return error_mark_node
;
13368 if (build_type
== NULL_TREE
)
13370 build_type
= result_type
;
13371 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
13374 gcc_assert (may_need_excess_precision
&& common
);
13375 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
13381 op0
= ep_convert_and_check (location
, result_type
, op0
,
13382 semantic_result_type
);
13383 op1
= ep_convert_and_check (location
, result_type
, op1
,
13384 semantic_result_type
);
13386 /* This can happen if one operand has a vector type, and the other
13387 has a different type. */
13388 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
13389 return error_mark_node
;
13392 if (sanitize_flags_p ((SANITIZE_SHIFT
13394 | SANITIZE_FLOAT_DIVIDE
13395 | SANITIZE_SI_OVERFLOW
))
13396 && current_function_decl
!= NULL_TREE
13397 && (doing_div_or_mod
|| doing_shift
)
13398 && !require_constant_value
)
13400 /* OP0 and/or OP1 might have side-effects. */
13401 op0
= save_expr (op0
);
13402 op1
= save_expr (op1
);
13403 op0
= c_fully_fold (op0
, false, NULL
);
13404 op1
= c_fully_fold (op1
, false, NULL
);
13405 if (doing_div_or_mod
&& (sanitize_flags_p ((SANITIZE_DIVIDE
13406 | SANITIZE_FLOAT_DIVIDE
13407 | SANITIZE_SI_OVERFLOW
))))
13408 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
13409 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
13410 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
13413 /* Treat expressions in initializers specially as they can't trap. */
13414 if (int_const_or_overflow
)
13415 ret
= (require_constant_value
13416 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
13418 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
13420 ret
= build2 (resultcode
, build_type
, op0
, op1
);
13421 if (final_type
!= NULL_TREE
)
13422 ret
= convert (final_type
, ret
);
13424 return_build_binary_op
:
13425 gcc_assert (ret
!= error_mark_node
);
13426 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
13427 ret
= (int_operands
13428 ? note_integer_operands (ret
)
13429 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
13430 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
13431 && !in_late_binary_op
)
13432 ret
= note_integer_operands (ret
);
13433 protected_set_expr_location (ret
, location
);
13435 if (instrument_expr
!= NULL
)
13436 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
13437 instrument_expr
, ret
);
13439 if (semantic_result_type
)
13440 ret
= build1_loc (location
, EXCESS_PRECISION_EXPR
,
13441 semantic_result_type
, ret
);
13447 /* Convert EXPR to be a truth-value, validating its type for this
13448 purpose. LOCATION is the source location for the expression. */
13451 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
13453 bool int_const
, int_operands
;
13455 switch (TREE_CODE (TREE_TYPE (expr
)))
13458 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
13459 return error_mark_node
;
13462 error_at (location
, "used struct type value where scalar is required");
13463 return error_mark_node
;
13466 error_at (location
, "used union type value where scalar is required");
13467 return error_mark_node
;
13470 error_at (location
, "void value not ignored as it ought to be");
13471 return error_mark_node
;
13474 if (reject_gcc_builtin (expr
))
13475 return error_mark_node
;
13478 case FUNCTION_TYPE
:
13479 gcc_unreachable ();
13482 error_at (location
, "used vector type where scalar is required");
13483 return error_mark_node
;
13489 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
13490 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
13491 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
13493 expr
= remove_c_maybe_const_expr (expr
);
13494 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
13495 convert (TREE_TYPE (expr
), integer_zero_node
));
13496 expr
= note_integer_operands (expr
);
13499 /* ??? Should we also give an error for vectors rather than leaving
13500 those to give errors later? */
13501 expr
= c_common_truthvalue_conversion (location
, expr
);
13503 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
13505 if (TREE_OVERFLOW (expr
))
13508 return note_integer_operands (expr
);
13510 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
13511 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
13516 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13520 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
13522 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
13524 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
13525 /* Executing a compound literal inside a function reinitializes
13527 if (!TREE_STATIC (decl
))
13535 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13536 statement. LOC is the location of the construct. */
13539 c_finish_omp_construct (location_t loc
, enum tree_code code
, tree body
,
13542 body
= c_end_compound_stmt (loc
, body
, true);
13544 tree stmt
= make_node (code
);
13545 TREE_TYPE (stmt
) = void_type_node
;
13546 OMP_BODY (stmt
) = body
;
13547 OMP_CLAUSES (stmt
) = clauses
;
13548 SET_EXPR_LOCATION (stmt
, loc
);
13550 return add_stmt (stmt
);
13553 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13554 statement. LOC is the location of the OACC_DATA. */
13557 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
13561 block
= c_end_compound_stmt (loc
, block
, true);
13563 stmt
= make_node (OACC_DATA
);
13564 TREE_TYPE (stmt
) = void_type_node
;
13565 OACC_DATA_CLAUSES (stmt
) = clauses
;
13566 OACC_DATA_BODY (stmt
) = block
;
13567 SET_EXPR_LOCATION (stmt
, loc
);
13569 return add_stmt (stmt
);
13572 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13573 statement. LOC is the location of the OACC_HOST_DATA. */
13576 c_finish_oacc_host_data (location_t loc
, tree clauses
, tree block
)
13580 block
= c_end_compound_stmt (loc
, block
, true);
13582 stmt
= make_node (OACC_HOST_DATA
);
13583 TREE_TYPE (stmt
) = void_type_node
;
13584 OACC_HOST_DATA_CLAUSES (stmt
) = clauses
;
13585 OACC_HOST_DATA_BODY (stmt
) = block
;
13586 SET_EXPR_LOCATION (stmt
, loc
);
13588 return add_stmt (stmt
);
13591 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13594 c_begin_omp_parallel (void)
13598 keep_next_level ();
13599 block
= c_begin_compound_stmt (true);
13604 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13605 statement. LOC is the location of the OMP_PARALLEL. */
13608 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
13612 block
= c_end_compound_stmt (loc
, block
, true);
13614 stmt
= make_node (OMP_PARALLEL
);
13615 TREE_TYPE (stmt
) = void_type_node
;
13616 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
13617 OMP_PARALLEL_BODY (stmt
) = block
;
13618 SET_EXPR_LOCATION (stmt
, loc
);
13620 return add_stmt (stmt
);
13623 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
13626 c_begin_omp_task (void)
13630 keep_next_level ();
13631 block
= c_begin_compound_stmt (true);
13636 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13637 statement. LOC is the location of the #pragma. */
13640 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
13644 block
= c_end_compound_stmt (loc
, block
, true);
13646 stmt
= make_node (OMP_TASK
);
13647 TREE_TYPE (stmt
) = void_type_node
;
13648 OMP_TASK_CLAUSES (stmt
) = clauses
;
13649 OMP_TASK_BODY (stmt
) = block
;
13650 SET_EXPR_LOCATION (stmt
, loc
);
13652 return add_stmt (stmt
);
13655 /* Generate GOMP_cancel call for #pragma omp cancel. */
13658 c_finish_omp_cancel (location_t loc
, tree clauses
)
13660 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
13662 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
13664 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
13666 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
13668 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
13672 error_at (loc
, "%<#pragma omp cancel%> must specify one of "
13673 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13677 tree ifc
= omp_find_clause (clauses
, OMP_CLAUSE_IF
);
13678 if (ifc
!= NULL_TREE
)
13680 if (OMP_CLAUSE_IF_MODIFIER (ifc
) != ERROR_MARK
13681 && OMP_CLAUSE_IF_MODIFIER (ifc
) != VOID_CST
)
13682 error_at (OMP_CLAUSE_LOCATION (ifc
),
13683 "expected %<cancel%> %<if%> clause modifier");
13686 tree ifc2
= omp_find_clause (OMP_CLAUSE_CHAIN (ifc
), OMP_CLAUSE_IF
);
13687 if (ifc2
!= NULL_TREE
)
13689 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc
) == VOID_CST
13690 && OMP_CLAUSE_IF_MODIFIER (ifc2
) != ERROR_MARK
13691 && OMP_CLAUSE_IF_MODIFIER (ifc2
) != VOID_CST
);
13692 error_at (OMP_CLAUSE_LOCATION (ifc2
),
13693 "expected %<cancel%> %<if%> clause modifier");
13697 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
13698 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
13699 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
13700 build_zero_cst (type
));
13703 ifc
= boolean_true_node
;
13704 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
13705 build_int_cst (integer_type_node
, mask
),
13710 /* Generate GOMP_cancellation_point call for
13711 #pragma omp cancellation point. */
13714 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
13716 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
13718 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
13720 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
13722 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
13724 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
13728 error_at (loc
, "%<#pragma omp cancellation point%> must specify one of "
13729 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13733 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
13734 build_int_cst (integer_type_node
, mask
));
13738 /* Helper function for handle_omp_array_sections. Called recursively
13739 to handle multiple array-section-subscripts. C is the clause,
13740 T current expression (initially OMP_CLAUSE_DECL), which is either
13741 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13742 expression if specified, TREE_VALUE length expression if specified,
13743 TREE_CHAIN is what it has been specified after, or some decl.
13744 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13745 set to true if any of the array-section-subscript could have length
13746 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13747 first array-section-subscript which is known not to have length
13749 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13750 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13751 all are or may have length of 1, array-section-subscript [:2] is the
13752 first one known not to have length 1. For array-section-subscript
13753 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13754 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13755 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
13756 case though, as some lengths could be zero. */
13759 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
13760 bool &maybe_zero_len
, unsigned int &first_non_one
,
13761 enum c_omp_region_type ort
)
13763 tree ret
, low_bound
, length
, type
;
13764 bool openacc
= (ort
& C_ORT_ACC
) != 0;
13765 if (TREE_CODE (t
) != TREE_LIST
)
13767 if (error_operand_p (t
))
13768 return error_mark_node
;
13769 c_omp_address_inspector
ai (OMP_CLAUSE_LOCATION (c
), t
);
13771 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_AFFINITY
13772 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13773 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
13775 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qE in %qs clause",
13776 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13777 return error_mark_node
;
13779 if (!ai
.check_clause (c
))
13780 return error_mark_node
;
13781 else if (ai
.component_access_p ()
13782 && (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13783 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO
13784 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FROM
))
13785 t
= ai
.get_root_term (true);
13787 t
= ai
.unconverted_ref_origin ();
13788 if (t
== error_mark_node
)
13789 return error_mark_node
;
13790 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13793 error_at (OMP_CLAUSE_LOCATION (c
),
13794 "%qD is not a variable in %qs clause", t
,
13795 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13797 error_at (OMP_CLAUSE_LOCATION (c
),
13798 "%qE is not a variable in %qs clause", t
,
13799 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13800 return error_mark_node
;
13802 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_AFFINITY
13803 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13804 && TYPE_ATOMIC (TREE_TYPE (t
)))
13806 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qD in %qs clause",
13807 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13808 return error_mark_node
;
13810 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_AFFINITY
13811 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13813 && DECL_THREAD_LOCAL_P (t
))
13815 error_at (OMP_CLAUSE_LOCATION (c
),
13816 "%qD is threadprivate variable in %qs clause", t
,
13817 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13818 return error_mark_node
;
13820 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_AFFINITY
13821 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
13822 && TYPE_ATOMIC (TREE_TYPE (t
))
13823 && POINTER_TYPE_P (TREE_TYPE (t
)))
13825 /* If the array section is pointer based and the pointer
13826 itself is _Atomic qualified, we need to atomically load
13829 memset (&expr
, 0, sizeof (expr
));
13831 expr
= convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c
),
13832 expr
, false, false);
13838 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
13839 maybe_zero_len
, first_non_one
, ort
);
13840 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
13843 type
= TREE_TYPE (ret
);
13844 low_bound
= TREE_PURPOSE (t
);
13845 length
= TREE_VALUE (t
);
13847 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
13848 return error_mark_node
;
13850 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
13852 error_at (OMP_CLAUSE_LOCATION (c
),
13853 "low bound %qE of array section does not have integral type",
13855 return error_mark_node
;
13857 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
13859 error_at (OMP_CLAUSE_LOCATION (c
),
13860 "length %qE of array section does not have integral type",
13862 return error_mark_node
;
13865 && TREE_CODE (low_bound
) == INTEGER_CST
13866 && TYPE_PRECISION (TREE_TYPE (low_bound
))
13867 > TYPE_PRECISION (sizetype
))
13868 low_bound
= fold_convert (sizetype
, low_bound
);
13870 && TREE_CODE (length
) == INTEGER_CST
13871 && TYPE_PRECISION (TREE_TYPE (length
))
13872 > TYPE_PRECISION (sizetype
))
13873 length
= fold_convert (sizetype
, length
);
13874 if (low_bound
== NULL_TREE
)
13875 low_bound
= integer_zero_node
;
13876 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13877 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ATTACH
13878 || OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_DETACH
))
13880 if (length
!= integer_one_node
)
13882 error_at (OMP_CLAUSE_LOCATION (c
),
13883 "expected single pointer in %qs clause",
13884 user_omp_clause_code_name (c
, openacc
));
13885 return error_mark_node
;
13888 if (length
!= NULL_TREE
)
13890 if (!integer_nonzerop (length
))
13892 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_AFFINITY
13893 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
13894 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13895 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13896 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
13898 if (integer_zerop (length
))
13900 error_at (OMP_CLAUSE_LOCATION (c
),
13901 "zero length array section in %qs clause",
13902 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13903 return error_mark_node
;
13907 maybe_zero_len
= true;
13909 if (first_non_one
== types
.length ()
13910 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
13913 if (TREE_CODE (type
) == ARRAY_TYPE
)
13915 if (length
== NULL_TREE
13916 && (TYPE_DOMAIN (type
) == NULL_TREE
13917 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
13919 error_at (OMP_CLAUSE_LOCATION (c
),
13920 "for unknown bound array type length expression must "
13922 return error_mark_node
;
13924 if (TREE_CODE (low_bound
) == INTEGER_CST
13925 && tree_int_cst_sgn (low_bound
) == -1)
13927 error_at (OMP_CLAUSE_LOCATION (c
),
13928 "negative low bound in array section in %qs clause",
13929 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13930 return error_mark_node
;
13932 if (length
!= NULL_TREE
13933 && TREE_CODE (length
) == INTEGER_CST
13934 && tree_int_cst_sgn (length
) == -1)
13936 error_at (OMP_CLAUSE_LOCATION (c
),
13937 "negative length in array section in %qs clause",
13938 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13939 return error_mark_node
;
13941 if (TYPE_DOMAIN (type
)
13942 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
13943 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
13947 = fold_convert (sizetype
, TYPE_MAX_VALUE (TYPE_DOMAIN (type
)));
13948 size
= size_binop (PLUS_EXPR
, size
, size_one_node
);
13949 if (TREE_CODE (low_bound
) == INTEGER_CST
)
13951 if (tree_int_cst_lt (size
, low_bound
))
13953 error_at (OMP_CLAUSE_LOCATION (c
),
13954 "low bound %qE above array section size "
13955 "in %qs clause", low_bound
,
13956 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13957 return error_mark_node
;
13959 if (tree_int_cst_equal (size
, low_bound
))
13961 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_AFFINITY
13962 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
13963 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13964 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13965 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
13967 error_at (OMP_CLAUSE_LOCATION (c
),
13968 "zero length array section in %qs clause",
13969 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13970 return error_mark_node
;
13972 maybe_zero_len
= true;
13974 else if (length
== NULL_TREE
13975 && first_non_one
== types
.length ()
13976 && tree_int_cst_equal
13977 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
13981 else if (length
== NULL_TREE
)
13983 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_AFFINITY
13984 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13985 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
13986 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_IN_REDUCTION
13987 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_TASK_REDUCTION
)
13988 maybe_zero_len
= true;
13989 if (first_non_one
== types
.length ())
13992 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
13994 if (tree_int_cst_lt (size
, length
))
13996 error_at (OMP_CLAUSE_LOCATION (c
),
13997 "length %qE above array section size "
13998 "in %qs clause", length
,
13999 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14000 return error_mark_node
;
14002 if (TREE_CODE (low_bound
) == INTEGER_CST
)
14005 = size_binop (PLUS_EXPR
,
14006 fold_convert (sizetype
, low_bound
),
14007 fold_convert (sizetype
, length
));
14008 if (TREE_CODE (lbpluslen
) == INTEGER_CST
14009 && tree_int_cst_lt (size
, lbpluslen
))
14011 error_at (OMP_CLAUSE_LOCATION (c
),
14012 "high bound %qE above array section size "
14013 "in %qs clause", lbpluslen
,
14014 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14015 return error_mark_node
;
14020 else if (length
== NULL_TREE
)
14022 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_AFFINITY
14023 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
14024 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
14025 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_IN_REDUCTION
14026 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_TASK_REDUCTION
)
14027 maybe_zero_len
= true;
14028 if (first_non_one
== types
.length ())
14032 /* For [lb:] we will need to evaluate lb more than once. */
14033 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
14035 tree lb
= save_expr (low_bound
);
14036 if (lb
!= low_bound
)
14038 TREE_PURPOSE (t
) = lb
;
14043 else if (TREE_CODE (type
) == POINTER_TYPE
)
14045 if (length
== NULL_TREE
)
14047 if (TREE_CODE (ret
) == PARM_DECL
&& C_ARRAY_PARAMETER (ret
))
14048 error_at (OMP_CLAUSE_LOCATION (c
),
14049 "for array function parameter length expression "
14050 "must be specified");
14052 error_at (OMP_CLAUSE_LOCATION (c
),
14053 "for pointer type length expression must be specified");
14054 return error_mark_node
;
14056 if (length
!= NULL_TREE
14057 && TREE_CODE (length
) == INTEGER_CST
14058 && tree_int_cst_sgn (length
) == -1)
14060 error_at (OMP_CLAUSE_LOCATION (c
),
14061 "negative length in array section in %qs clause",
14062 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14063 return error_mark_node
;
14065 /* If there is a pointer type anywhere but in the very first
14066 array-section-subscript, the array section could be non-contiguous. */
14067 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
14068 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_AFFINITY
14069 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
14071 /* If any prior dimension has a non-one length, then deem this
14072 array section as non-contiguous. */
14073 for (tree d
= TREE_CHAIN (t
); TREE_CODE (d
) == TREE_LIST
;
14074 d
= TREE_CHAIN (d
))
14076 tree d_length
= TREE_VALUE (d
);
14077 if (d_length
== NULL_TREE
|| !integer_onep (d_length
))
14079 error_at (OMP_CLAUSE_LOCATION (c
),
14080 "array section is not contiguous in %qs clause",
14081 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14082 return error_mark_node
;
14089 error_at (OMP_CLAUSE_LOCATION (c
),
14090 "%qE does not have pointer or array type", ret
);
14091 return error_mark_node
;
14093 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
14094 types
.safe_push (TREE_TYPE (ret
));
14095 /* We will need to evaluate lb more than once. */
14096 tree lb
= save_expr (low_bound
);
14097 if (lb
!= low_bound
)
14099 TREE_PURPOSE (t
) = lb
;
14102 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
14106 /* Handle array sections for clause C. */
14109 handle_omp_array_sections (tree
&c
, enum c_omp_region_type ort
)
14111 bool maybe_zero_len
= false;
14112 unsigned int first_non_one
= 0;
14113 auto_vec
<tree
, 10> types
;
14114 tree
*tp
= &OMP_CLAUSE_DECL (c
);
14115 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
14116 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_AFFINITY
)
14117 && TREE_CODE (*tp
) == TREE_LIST
14118 && TREE_PURPOSE (*tp
)
14119 && TREE_CODE (TREE_PURPOSE (*tp
)) == TREE_VEC
)
14120 tp
= &TREE_VALUE (*tp
);
14121 tree first
= handle_omp_array_sections_1 (c
, *tp
, types
,
14122 maybe_zero_len
, first_non_one
,
14124 if (first
== error_mark_node
)
14126 if (first
== NULL_TREE
)
14128 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
14129 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_AFFINITY
)
14132 tree tem
= NULL_TREE
;
14133 /* Need to evaluate side effects in the length expressions
14135 while (TREE_CODE (t
) == TREE_LIST
)
14137 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
14139 if (tem
== NULL_TREE
)
14140 tem
= TREE_VALUE (t
);
14142 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
14143 TREE_VALUE (t
), tem
);
14145 t
= TREE_CHAIN (t
);
14148 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
14149 first
= c_fully_fold (first
, false, NULL
, true);
14154 unsigned int num
= types
.length (), i
;
14155 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
14156 tree condition
= NULL_TREE
;
14158 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
14159 maybe_zero_len
= true;
14161 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
14162 t
= TREE_CHAIN (t
))
14164 tree low_bound
= TREE_PURPOSE (t
);
14165 tree length
= TREE_VALUE (t
);
14169 && TREE_CODE (low_bound
) == INTEGER_CST
14170 && TYPE_PRECISION (TREE_TYPE (low_bound
))
14171 > TYPE_PRECISION (sizetype
))
14172 low_bound
= fold_convert (sizetype
, low_bound
);
14174 && TREE_CODE (length
) == INTEGER_CST
14175 && TYPE_PRECISION (TREE_TYPE (length
))
14176 > TYPE_PRECISION (sizetype
))
14177 length
= fold_convert (sizetype
, length
);
14178 if (low_bound
== NULL_TREE
)
14179 low_bound
= integer_zero_node
;
14180 if (!maybe_zero_len
&& i
> first_non_one
)
14182 if (integer_nonzerop (low_bound
))
14183 goto do_warn_noncontiguous
;
14184 if (length
!= NULL_TREE
14185 && TREE_CODE (length
) == INTEGER_CST
14186 && TYPE_DOMAIN (types
[i
])
14187 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
14188 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
14192 size
= size_binop (PLUS_EXPR
,
14193 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
14195 if (!tree_int_cst_equal (length
, size
))
14197 do_warn_noncontiguous
:
14198 error_at (OMP_CLAUSE_LOCATION (c
),
14199 "array section is not contiguous in %qs "
14201 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14205 if (length
!= NULL_TREE
14206 && TREE_SIDE_EFFECTS (length
))
14208 if (side_effects
== NULL_TREE
)
14209 side_effects
= length
;
14211 side_effects
= build2 (COMPOUND_EXPR
,
14212 TREE_TYPE (side_effects
),
14213 length
, side_effects
);
14220 if (i
> first_non_one
14221 && ((length
&& integer_nonzerop (length
))
14222 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
14223 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
14224 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
))
14227 l
= fold_convert (sizetype
, length
);
14230 l
= size_binop (PLUS_EXPR
,
14231 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
14233 l
= size_binop (MINUS_EXPR
, l
,
14234 fold_convert (sizetype
, low_bound
));
14236 if (i
> first_non_one
)
14238 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
14240 if (condition
== NULL_TREE
)
14243 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
14246 else if (size
== NULL_TREE
)
14248 size
= size_in_bytes (TREE_TYPE (types
[i
]));
14249 tree eltype
= TREE_TYPE (types
[num
- 1]);
14250 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
14251 eltype
= TREE_TYPE (eltype
);
14252 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
14253 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
14254 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
14256 if (integer_zerop (size
)
14257 || integer_zerop (size_in_bytes (eltype
)))
14259 error_at (OMP_CLAUSE_LOCATION (c
),
14260 "zero length array section in %qs clause",
14261 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14262 return error_mark_node
;
14264 size
= size_binop (EXACT_DIV_EXPR
, size
,
14265 size_in_bytes (eltype
));
14267 size
= size_binop (MULT_EXPR
, size
, l
);
14269 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
14270 size
, size_zero_node
);
14273 size
= size_binop (MULT_EXPR
, size
, l
);
14277 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
14278 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
14279 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
14280 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
14282 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
14283 size
= c_fully_fold (size
, false, NULL
);
14284 size
= save_expr (size
);
14285 tree index_type
= build_index_type (size
);
14286 tree eltype
= TREE_TYPE (first
);
14287 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
14288 eltype
= TREE_TYPE (eltype
);
14289 tree type
= build_array_type (eltype
, index_type
);
14290 tree ptype
= build_pointer_type (eltype
);
14291 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
14292 t
= build_fold_addr_expr (t
);
14293 tree t2
= build_fold_addr_expr (first
);
14294 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
14295 ptrdiff_type_node
, t2
);
14296 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
14297 ptrdiff_type_node
, t2
,
14298 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
14299 ptrdiff_type_node
, t
));
14300 t2
= c_fully_fold (t2
, false, NULL
);
14301 if (tree_fits_shwi_p (t2
))
14302 t
= build2 (MEM_REF
, type
, t
,
14303 build_int_cst (ptype
, tree_to_shwi (t2
)));
14306 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), sizetype
, t2
);
14307 t
= build2_loc (OMP_CLAUSE_LOCATION (c
), POINTER_PLUS_EXPR
,
14308 TREE_TYPE (t
), t
, t2
);
14309 t
= build2 (MEM_REF
, type
, t
, build_int_cst (ptype
, 0));
14311 OMP_CLAUSE_DECL (c
) = t
;
14314 first
= c_fully_fold (first
, false, NULL
);
14315 OMP_CLAUSE_DECL (c
) = first
;
14316 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_HAS_DEVICE_ADDR
)
14318 /* Don't set OMP_CLAUSE_SIZE for bare attach/detach clauses. */
14319 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
14320 || (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_ATTACH
14321 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_DETACH
14322 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DETACH
))
14325 size
= c_fully_fold (size
, false, NULL
);
14326 OMP_CLAUSE_SIZE (c
) = size
;
14329 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
14332 auto_vec
<omp_addr_token
*, 10> addr_tokens
;
14334 if (!omp_parse_expr (addr_tokens
, first
))
14337 c_omp_address_inspector
ai (OMP_CLAUSE_LOCATION (c
), t
);
14339 tree nc
= ai
.expand_map_clause (c
, first
, addr_tokens
, ort
);
14340 if (nc
!= error_mark_node
)
14342 using namespace omp_addr_tokenizer
;
14344 if (ai
.maybe_zero_length_array_section (c
))
14345 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c
) = 1;
14347 /* !!! If we're accessing a base decl via chained access
14348 methods (e.g. multiple indirections), duplicate clause
14349 detection won't work properly. Skip it in that case. */
14350 if ((addr_tokens
[0]->type
== STRUCTURE_BASE
14351 || addr_tokens
[0]->type
== ARRAY_BASE
)
14352 && addr_tokens
[0]->u
.structure_base_kind
== BASE_DECL
14353 && addr_tokens
[1]->type
== ACCESS_METHOD
14354 && omp_access_chain_p (addr_tokens
, 1))
14363 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
14364 an inline call. But, remap
14365 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
14366 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
14369 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
14370 tree decl
, tree placeholder
)
14373 hash_map
<tree
, tree
> decl_map
;
14375 decl_map
.put (omp_decl1
, placeholder
);
14376 decl_map
.put (omp_decl2
, decl
);
14377 memset (&id
, 0, sizeof (id
));
14378 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
14379 id
.dst_fn
= current_function_decl
;
14380 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
14381 id
.decl_map
= &decl_map
;
14383 id
.copy_decl
= copy_decl_no_change
;
14384 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
14385 id
.transform_new_cfg
= true;
14386 id
.transform_return_to_modify
= false;
14388 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
14392 /* Helper function of c_finish_omp_clauses, called via walk_tree.
14393 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
14396 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
14398 if (*tp
== (tree
) data
)
14403 /* Similarly, but also walk aggregate fields. */
14405 struct c_find_omp_var_s
{ tree var
; hash_set
<tree
> *pset
; };
14408 c_find_omp_var_r (tree
*tp
, int *, void *data
)
14410 if (*tp
== ((struct c_find_omp_var_s
*) data
)->var
)
14412 if (RECORD_OR_UNION_TYPE_P (*tp
))
14415 hash_set
<tree
> *pset
= ((struct c_find_omp_var_s
*) data
)->pset
;
14417 for (field
= TYPE_FIELDS (*tp
); field
;
14418 field
= DECL_CHAIN (field
))
14419 if (TREE_CODE (field
) == FIELD_DECL
)
14421 tree ret
= walk_tree (&DECL_FIELD_OFFSET (field
),
14422 c_find_omp_var_r
, data
, pset
);
14425 ret
= walk_tree (&DECL_SIZE (field
), c_find_omp_var_r
, data
, pset
);
14428 ret
= walk_tree (&DECL_SIZE_UNIT (field
), c_find_omp_var_r
, data
,
14432 ret
= walk_tree (&TREE_TYPE (field
), c_find_omp_var_r
, data
, pset
);
14437 else if (INTEGRAL_TYPE_P (*tp
))
14438 return walk_tree (&TYPE_MAX_VALUE (*tp
), c_find_omp_var_r
, data
,
14439 ((struct c_find_omp_var_s
*) data
)->pset
);
14443 /* Finish OpenMP iterators ITER. Return true if they are errorneous
14444 and clauses containing them should be removed. */
14447 c_omp_finish_iterators (tree iter
)
14450 for (tree it
= iter
; it
; it
= TREE_CHAIN (it
))
14452 tree var
= TREE_VEC_ELT (it
, 0);
14453 tree begin
= TREE_VEC_ELT (it
, 1);
14454 tree end
= TREE_VEC_ELT (it
, 2);
14455 tree step
= TREE_VEC_ELT (it
, 3);
14457 tree type
= TREE_TYPE (var
);
14458 location_t loc
= DECL_SOURCE_LOCATION (var
);
14459 if (type
== error_mark_node
)
14464 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
14466 error_at (loc
, "iterator %qD has neither integral nor pointer type",
14471 else if (TYPE_ATOMIC (type
))
14473 error_at (loc
, "iterator %qD has %<_Atomic%> qualified type", var
);
14477 else if (TYPE_READONLY (type
))
14479 error_at (loc
, "iterator %qD has const qualified type", var
);
14483 else if (step
== error_mark_node
14484 || TREE_TYPE (step
) == error_mark_node
)
14489 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step
)))
14491 error_at (EXPR_LOC_OR_LOC (step
, loc
),
14492 "iterator step with non-integral type");
14496 begin
= c_fully_fold (build_c_cast (loc
, type
, begin
), false, NULL
);
14497 end
= c_fully_fold (build_c_cast (loc
, type
, end
), false, NULL
);
14498 orig_step
= save_expr (c_fully_fold (step
, false, NULL
));
14499 tree stype
= POINTER_TYPE_P (type
) ? sizetype
: type
;
14500 step
= c_fully_fold (build_c_cast (loc
, stype
, orig_step
), false, NULL
);
14501 if (POINTER_TYPE_P (type
))
14503 begin
= save_expr (begin
);
14504 step
= pointer_int_sum (loc
, PLUS_EXPR
, begin
, step
);
14505 step
= fold_build2_loc (loc
, MINUS_EXPR
, sizetype
,
14506 fold_convert (sizetype
, step
),
14507 fold_convert (sizetype
, begin
));
14508 step
= fold_convert (ssizetype
, step
);
14510 if (integer_zerop (step
))
14512 error_at (loc
, "iterator %qD has zero step", var
);
14517 if (begin
== error_mark_node
14518 || end
== error_mark_node
14519 || step
== error_mark_node
14520 || orig_step
== error_mark_node
)
14525 hash_set
<tree
> pset
;
14527 for (it2
= TREE_CHAIN (it
); it2
; it2
= TREE_CHAIN (it2
))
14529 tree var2
= TREE_VEC_ELT (it2
, 0);
14530 tree begin2
= TREE_VEC_ELT (it2
, 1);
14531 tree end2
= TREE_VEC_ELT (it2
, 2);
14532 tree step2
= TREE_VEC_ELT (it2
, 3);
14533 tree type2
= TREE_TYPE (var2
);
14534 location_t loc2
= DECL_SOURCE_LOCATION (var2
);
14535 struct c_find_omp_var_s data
= { var
, &pset
};
14536 if (walk_tree (&type2
, c_find_omp_var_r
, &data
, &pset
))
14539 "type of iterator %qD refers to outer iterator %qD",
14543 else if (walk_tree (&begin2
, c_find_omp_var_r
, &data
, &pset
))
14545 error_at (EXPR_LOC_OR_LOC (begin2
, loc2
),
14546 "begin expression refers to outer iterator %qD", var
);
14549 else if (walk_tree (&end2
, c_find_omp_var_r
, &data
, &pset
))
14551 error_at (EXPR_LOC_OR_LOC (end2
, loc2
),
14552 "end expression refers to outer iterator %qD", var
);
14555 else if (walk_tree (&step2
, c_find_omp_var_r
, &data
, &pset
))
14557 error_at (EXPR_LOC_OR_LOC (step2
, loc2
),
14558 "step expression refers to outer iterator %qD", var
);
14567 TREE_VEC_ELT (it
, 1) = begin
;
14568 TREE_VEC_ELT (it
, 2) = end
;
14569 TREE_VEC_ELT (it
, 3) = step
;
14570 TREE_VEC_ELT (it
, 4) = orig_step
;
14575 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14576 Return true if an error has been detected. */
14579 c_oacc_check_attachments (tree c
)
14581 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
14584 /* OpenACC attach / detach clauses must be pointers. */
14585 if (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ATTACH
14586 || OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_DETACH
)
14588 tree t
= OMP_CLAUSE_DECL (c
);
14590 while (TREE_CODE (t
) == TREE_LIST
)
14591 t
= TREE_CHAIN (t
);
14593 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
14595 error_at (OMP_CLAUSE_LOCATION (c
), "expected pointer in %qs clause",
14596 user_omp_clause_code_name (c
, true));
14604 /* For all elements of CLAUSES, validate them against their constraints.
14605 Remove any elements from the list that are invalid. */
14608 c_finish_omp_clauses (tree clauses
, enum c_omp_region_type ort
)
14610 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
14611 bitmap_head aligned_head
, map_head
, map_field_head
, map_firstprivate_head
;
14612 bitmap_head oacc_reduction_head
, is_on_device_head
;
14613 tree c
, t
, type
, *pc
;
14614 tree simdlen
= NULL_TREE
, safelen
= NULL_TREE
;
14615 bool branch_seen
= false;
14616 bool copyprivate_seen
= false;
14617 bool mergeable_seen
= false;
14618 tree
*detach_seen
= NULL
;
14619 bool linear_variable_step_check
= false;
14620 tree
*nowait_clause
= NULL
;
14621 tree ordered_clause
= NULL_TREE
;
14622 tree schedule_clause
= NULL_TREE
;
14623 bool oacc_async
= false;
14624 tree last_iterators
= NULL_TREE
;
14625 bool last_iterators_remove
= false;
14626 tree
*nogroup_seen
= NULL
;
14627 tree
*order_clause
= NULL
;
14628 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14629 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
14630 int reduction_seen
= 0;
14631 bool allocate_seen
= false;
14632 bool implicit_moved
= false;
14633 bool target_in_reduction_seen
= false;
14634 bool openacc
= (ort
& C_ORT_ACC
) != 0;
14636 bitmap_obstack_initialize (NULL
);
14637 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
14638 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
14639 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
14640 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
14641 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
14642 bitmap_initialize (&map_head
, &bitmap_default_obstack
);
14643 bitmap_initialize (&map_field_head
, &bitmap_default_obstack
);
14644 bitmap_initialize (&map_firstprivate_head
, &bitmap_default_obstack
);
14645 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14646 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
14647 bitmap_initialize (&oacc_reduction_head
, &bitmap_default_obstack
);
14648 bitmap_initialize (&is_on_device_head
, &bitmap_default_obstack
);
14651 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
14652 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ASYNC
)
14658 tree
*grp_start_p
= NULL
, grp_sentinel
= NULL_TREE
;
14660 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
14662 bool remove
= false;
14663 bool need_complete
= false;
14664 bool need_implicitly_determined
= false;
14666 /* We've reached the end of a list of expanded nodes. Reset the group
14668 if (c
== grp_sentinel
)
14669 grp_start_p
= NULL
;
14671 switch (OMP_CLAUSE_CODE (c
))
14673 case OMP_CLAUSE_SHARED
:
14674 need_implicitly_determined
= true;
14675 goto check_dup_generic
;
14677 case OMP_CLAUSE_PRIVATE
:
14678 need_complete
= true;
14679 need_implicitly_determined
= true;
14680 goto check_dup_generic
;
14682 case OMP_CLAUSE_REDUCTION
:
14683 if (reduction_seen
== 0)
14684 reduction_seen
= OMP_CLAUSE_REDUCTION_INSCAN (c
) ? -1 : 1;
14685 else if (reduction_seen
!= -2
14686 && reduction_seen
!= (OMP_CLAUSE_REDUCTION_INSCAN (c
)
14689 error_at (OMP_CLAUSE_LOCATION (c
),
14690 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14691 "on the same construct");
14692 reduction_seen
= -2;
14695 case OMP_CLAUSE_IN_REDUCTION
:
14696 case OMP_CLAUSE_TASK_REDUCTION
:
14697 need_implicitly_determined
= true;
14698 t
= OMP_CLAUSE_DECL (c
);
14699 if (TREE_CODE (t
) == TREE_LIST
)
14701 if (handle_omp_array_sections (c
, ort
))
14707 t
= OMP_CLAUSE_DECL (c
);
14708 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
14709 && OMP_CLAUSE_REDUCTION_INSCAN (c
))
14711 error_at (OMP_CLAUSE_LOCATION (c
),
14712 "%<inscan%> %<reduction%> clause with array "
14718 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
14719 if (t
== error_mark_node
)
14725 c_mark_addressable (t
);
14726 type
= TREE_TYPE (t
);
14727 if (TREE_CODE (t
) == MEM_REF
)
14728 type
= TREE_TYPE (type
);
14729 if (TREE_CODE (type
) == ARRAY_TYPE
)
14731 tree oatype
= type
;
14732 gcc_assert (TREE_CODE (t
) != MEM_REF
);
14733 while (TREE_CODE (type
) == ARRAY_TYPE
)
14734 type
= TREE_TYPE (type
);
14735 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
14737 error_at (OMP_CLAUSE_LOCATION (c
),
14738 "%qD in %<reduction%> clause is a zero size array",
14743 tree size
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (oatype
),
14744 TYPE_SIZE_UNIT (type
));
14745 if (integer_zerop (size
))
14747 error_at (OMP_CLAUSE_LOCATION (c
),
14748 "%qD in %<reduction%> clause is a zero size array",
14753 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
14754 size
= save_expr (size
);
14755 tree index_type
= build_index_type (size
);
14756 tree atype
= build_array_type (TYPE_MAIN_VARIANT (type
),
14758 atype
= c_build_qualified_type (atype
, TYPE_QUALS (type
));
14759 tree ptype
= build_pointer_type (type
);
14760 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
14761 t
= build_fold_addr_expr (t
);
14762 t
= build2 (MEM_REF
, atype
, t
, build_int_cst (ptype
, 0));
14763 OMP_CLAUSE_DECL (c
) = t
;
14765 if (TYPE_ATOMIC (type
))
14767 error_at (OMP_CLAUSE_LOCATION (c
),
14768 "%<_Atomic%> %qE in %<reduction%> clause", t
);
14772 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
14773 || OMP_CLAUSE_REDUCTION_TASK (c
))
14775 /* Disallow zero sized or potentially zero sized task
14777 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
14779 error_at (OMP_CLAUSE_LOCATION (c
),
14780 "zero sized type %qT in %qs clause", type
,
14781 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14785 else if (TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
)
14787 error_at (OMP_CLAUSE_LOCATION (c
),
14788 "variable sized type %qT in %qs clause", type
,
14789 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14794 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
14795 && (FLOAT_TYPE_P (type
)
14796 || TREE_CODE (type
) == COMPLEX_TYPE
))
14798 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
14799 const char *r_name
= NULL
;
14806 case TRUTH_ANDIF_EXPR
:
14807 case TRUTH_ORIF_EXPR
:
14810 if (TREE_CODE (type
) == COMPLEX_TYPE
)
14814 if (TREE_CODE (type
) == COMPLEX_TYPE
)
14827 gcc_unreachable ();
14831 error_at (OMP_CLAUSE_LOCATION (c
),
14832 "%qE has invalid type for %<reduction(%s)%>",
14838 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
14840 error_at (OMP_CLAUSE_LOCATION (c
),
14841 "user defined reduction not found for %qE", t
);
14845 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
14847 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
14848 type
= TYPE_MAIN_VARIANT (type
);
14849 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
14850 VAR_DECL
, NULL_TREE
, type
);
14851 tree decl_placeholder
= NULL_TREE
;
14852 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
14853 DECL_ARTIFICIAL (placeholder
) = 1;
14854 DECL_IGNORED_P (placeholder
) = 1;
14855 if (TREE_CODE (t
) == MEM_REF
)
14857 decl_placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
14858 VAR_DECL
, NULL_TREE
, type
);
14859 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
) = decl_placeholder
;
14860 DECL_ARTIFICIAL (decl_placeholder
) = 1;
14861 DECL_IGNORED_P (decl_placeholder
) = 1;
14863 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
14864 c_mark_addressable (placeholder
);
14865 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
14866 c_mark_addressable (decl_placeholder
? decl_placeholder
14867 : OMP_CLAUSE_DECL (c
));
14868 OMP_CLAUSE_REDUCTION_MERGE (c
)
14869 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
14870 TREE_VEC_ELT (list
, 0),
14871 TREE_VEC_ELT (list
, 1),
14872 decl_placeholder
? decl_placeholder
14873 : OMP_CLAUSE_DECL (c
), placeholder
);
14874 OMP_CLAUSE_REDUCTION_MERGE (c
)
14875 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
14876 void_type_node
, NULL_TREE
,
14877 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
14878 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
14879 if (TREE_VEC_LENGTH (list
) == 6)
14881 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
14882 c_mark_addressable (decl_placeholder
? decl_placeholder
14883 : OMP_CLAUSE_DECL (c
));
14884 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
14885 c_mark_addressable (placeholder
);
14886 tree init
= TREE_VEC_ELT (list
, 5);
14887 if (init
== error_mark_node
)
14888 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
14889 OMP_CLAUSE_REDUCTION_INIT (c
)
14890 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
14891 TREE_VEC_ELT (list
, 3),
14892 decl_placeholder
? decl_placeholder
14893 : OMP_CLAUSE_DECL (c
), placeholder
);
14894 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
14896 tree v
= decl_placeholder
? decl_placeholder
: t
;
14897 OMP_CLAUSE_REDUCTION_INIT (c
)
14898 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
,
14899 OMP_CLAUSE_REDUCTION_INIT (c
));
14901 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
14902 c_find_omp_placeholder_r
,
14903 placeholder
, NULL
))
14904 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
14909 tree v
= decl_placeholder
? decl_placeholder
: t
;
14910 if (AGGREGATE_TYPE_P (TREE_TYPE (v
)))
14911 init
= build_constructor (TREE_TYPE (v
), NULL
);
14913 init
= fold_convert (TREE_TYPE (v
), integer_zero_node
);
14914 OMP_CLAUSE_REDUCTION_INIT (c
)
14915 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
, init
);
14917 OMP_CLAUSE_REDUCTION_INIT (c
)
14918 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
14919 void_type_node
, NULL_TREE
,
14920 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
14921 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
14923 if (TREE_CODE (t
) == MEM_REF
)
14925 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))) == NULL_TREE
14926 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))))
14929 sorry ("variable length element type in array "
14930 "%<reduction%> clause");
14934 t
= TREE_OPERAND (t
, 0);
14935 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
14936 t
= TREE_OPERAND (t
, 0);
14937 if (TREE_CODE (t
) == ADDR_EXPR
)
14938 t
= TREE_OPERAND (t
, 0);
14940 goto check_dup_generic_t
;
14942 case OMP_CLAUSE_COPYPRIVATE
:
14943 copyprivate_seen
= true;
14946 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
14947 "%<nowait%> clause must not be used together "
14948 "with %<copyprivate%>");
14949 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
14950 nowait_clause
= NULL
;
14952 goto check_dup_generic
;
14954 case OMP_CLAUSE_COPYIN
:
14955 t
= OMP_CLAUSE_DECL (c
);
14956 if (!VAR_P (t
) || !DECL_THREAD_LOCAL_P (t
))
14958 error_at (OMP_CLAUSE_LOCATION (c
),
14959 "%qE must be %<threadprivate%> for %<copyin%>", t
);
14963 goto check_dup_generic
;
14965 case OMP_CLAUSE_LINEAR
:
14966 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
14967 need_implicitly_determined
= true;
14968 t
= OMP_CLAUSE_DECL (c
);
14969 if (ort
!= C_ORT_OMP_DECLARE_SIMD
14970 && OMP_CLAUSE_LINEAR_KIND (c
) != OMP_CLAUSE_LINEAR_DEFAULT
14971 && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c
))
14973 error_at (OMP_CLAUSE_LOCATION (c
),
14974 "modifier should not be specified in %<linear%> "
14975 "clause on %<simd%> or %<for%> constructs when not "
14976 "using OpenMP 5.2 modifiers");
14977 OMP_CLAUSE_LINEAR_KIND (c
) = OMP_CLAUSE_LINEAR_DEFAULT
;
14979 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
14980 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
14982 error_at (OMP_CLAUSE_LOCATION (c
),
14983 "linear clause applied to non-integral non-pointer "
14984 "variable with type %qT", TREE_TYPE (t
));
14988 if (TYPE_ATOMIC (TREE_TYPE (t
)))
14990 error_at (OMP_CLAUSE_LOCATION (c
),
14991 "%<_Atomic%> %qD in %<linear%> clause", t
);
14995 if (ort
== C_ORT_OMP_DECLARE_SIMD
)
14997 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
14998 if (TREE_CODE (s
) == PARM_DECL
)
15000 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
) = 1;
15001 /* map_head bitmap is used as uniform_head if
15003 if (!bitmap_bit_p (&map_head
, DECL_UID (s
)))
15004 linear_variable_step_check
= true;
15005 goto check_dup_generic
;
15007 if (TREE_CODE (s
) != INTEGER_CST
)
15009 error_at (OMP_CLAUSE_LOCATION (c
),
15010 "%<linear%> clause step %qE is neither constant "
15011 "nor a parameter", s
);
15016 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
15018 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
15019 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
15020 OMP_CLAUSE_DECL (c
), s
);
15021 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
15022 sizetype
, fold_convert (sizetype
, s
),
15024 (sizetype
, OMP_CLAUSE_DECL (c
)));
15025 if (s
== error_mark_node
)
15027 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
15030 OMP_CLAUSE_LINEAR_STEP (c
)
15031 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
15032 goto check_dup_generic
;
15035 t
= OMP_CLAUSE_DECL (c
);
15036 check_dup_generic_t
:
15037 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
15039 error_at (OMP_CLAUSE_LOCATION (c
),
15040 "%qE is not a variable in clause %qs", t
,
15041 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15044 else if ((openacc
&& OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
15045 || (ort
== C_ORT_OMP
15046 && (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_USE_DEVICE_PTR
15047 || (OMP_CLAUSE_CODE (c
)
15048 == OMP_CLAUSE_USE_DEVICE_ADDR
)))
15049 || (ort
== C_ORT_OMP_TARGET
15050 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
))
15052 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
15053 && (bitmap_bit_p (&generic_head
, DECL_UID (t
))
15054 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))))
15056 error_at (OMP_CLAUSE_LOCATION (c
),
15057 "%qD appears more than once in data-sharing "
15062 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
15063 target_in_reduction_seen
= true;
15064 if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
15066 error_at (OMP_CLAUSE_LOCATION (c
),
15068 ? "%qD appears more than once in reduction clauses"
15069 : "%qD appears more than once in data clauses",
15074 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
15076 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
15077 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
15078 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
))
15079 || bitmap_bit_p (&map_firstprivate_head
, DECL_UID (t
)))
15081 error_at (OMP_CLAUSE_LOCATION (c
),
15082 "%qE appears more than once in data clauses", t
);
15085 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
15086 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_HAS_DEVICE_ADDR
15087 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IS_DEVICE_PTR
)
15088 && bitmap_bit_p (&map_head
, DECL_UID (t
)))
15091 error_at (OMP_CLAUSE_LOCATION (c
),
15092 "%qD appears more than once in data clauses", t
);
15094 error_at (OMP_CLAUSE_LOCATION (c
),
15095 "%qD appears both in data and map clauses", t
);
15099 bitmap_set_bit (&generic_head
, DECL_UID (t
));
15102 case OMP_CLAUSE_FIRSTPRIVATE
:
15103 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c
) && !implicit_moved
)
15106 implicit_moved
= true;
15107 /* Move firstprivate and map clauses with
15108 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
15110 tree cl1
= NULL_TREE
, cl2
= NULL_TREE
;
15111 tree
*pc1
= pc
, *pc2
= &cl1
, *pc3
= &cl2
;
15113 if (OMP_CLAUSE_CODE (*pc1
) == OMP_CLAUSE_FIRSTPRIVATE
15114 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1
))
15117 pc3
= &OMP_CLAUSE_CHAIN (*pc3
);
15118 *pc1
= OMP_CLAUSE_CHAIN (*pc1
);
15120 else if (OMP_CLAUSE_CODE (*pc1
) == OMP_CLAUSE_MAP
15121 && OMP_CLAUSE_MAP_IMPLICIT (*pc1
))
15124 pc2
= &OMP_CLAUSE_CHAIN (*pc2
);
15125 *pc1
= OMP_CLAUSE_CHAIN (*pc1
);
15128 pc1
= &OMP_CLAUSE_CHAIN (*pc1
);
15134 t
= OMP_CLAUSE_DECL (c
);
15135 need_complete
= true;
15136 need_implicitly_determined
= true;
15137 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
15139 error_at (OMP_CLAUSE_LOCATION (c
),
15140 "%qE is not a variable in clause %<firstprivate%>", t
);
15143 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c
)
15144 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c
)
15145 && bitmap_bit_p (&map_firstprivate_head
, DECL_UID (t
)))
15147 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
15148 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
15149 || bitmap_bit_p (&map_firstprivate_head
, DECL_UID (t
)))
15151 error_at (OMP_CLAUSE_LOCATION (c
),
15152 "%qE appears more than once in data clauses", t
);
15155 else if (bitmap_bit_p (&map_head
, DECL_UID (t
))
15156 || bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
15159 error_at (OMP_CLAUSE_LOCATION (c
),
15160 "%qD appears more than once in data clauses", t
);
15161 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c
)
15162 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c
))
15163 /* Silently drop the clause. */;
15165 error_at (OMP_CLAUSE_LOCATION (c
),
15166 "%qD appears both in data and map clauses", t
);
15170 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
15173 case OMP_CLAUSE_LASTPRIVATE
:
15174 t
= OMP_CLAUSE_DECL (c
);
15175 need_complete
= true;
15176 need_implicitly_determined
= true;
15177 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
15179 error_at (OMP_CLAUSE_LOCATION (c
),
15180 "%qE is not a variable in clause %<lastprivate%>", t
);
15183 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
15184 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
15186 error_at (OMP_CLAUSE_LOCATION (c
),
15187 "%qE appears more than once in data clauses", t
);
15191 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
15194 case OMP_CLAUSE_ALIGNED
:
15195 t
= OMP_CLAUSE_DECL (c
);
15196 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
15198 error_at (OMP_CLAUSE_LOCATION (c
),
15199 "%qE is not a variable in %<aligned%> clause", t
);
15202 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
15203 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
15205 error_at (OMP_CLAUSE_LOCATION (c
),
15206 "%qE in %<aligned%> clause is neither a pointer nor "
15210 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
15212 error_at (OMP_CLAUSE_LOCATION (c
),
15213 "%<_Atomic%> %qD in %<aligned%> clause", t
);
15217 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
15219 error_at (OMP_CLAUSE_LOCATION (c
),
15220 "%qE appears more than once in %<aligned%> clauses",
15225 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
15228 case OMP_CLAUSE_NONTEMPORAL
:
15229 t
= OMP_CLAUSE_DECL (c
);
15230 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
15232 error_at (OMP_CLAUSE_LOCATION (c
),
15233 "%qE is not a variable in %<nontemporal%> clause", t
);
15236 else if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
15238 error_at (OMP_CLAUSE_LOCATION (c
),
15239 "%qE appears more than once in %<nontemporal%> "
15244 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
15247 case OMP_CLAUSE_ALLOCATE
:
15248 t
= OMP_CLAUSE_DECL (c
);
15249 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
15251 error_at (OMP_CLAUSE_LOCATION (c
),
15252 "%qE is not a variable in %<allocate%> clause", t
);
15255 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
15257 warning_at (OMP_CLAUSE_LOCATION (c
), 0,
15258 "%qE appears more than once in %<allocate%> clauses",
15264 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
15265 if (!OMP_CLAUSE_ALLOCATE_COMBINED (c
))
15266 allocate_seen
= true;
15270 case OMP_CLAUSE_DOACROSS
:
15271 t
= OMP_CLAUSE_DECL (c
);
15272 if (t
== NULL_TREE
)
15274 if (OMP_CLAUSE_DOACROSS_KIND (c
) == OMP_CLAUSE_DOACROSS_SINK
)
15276 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
15277 for (; t
; t
= TREE_CHAIN (t
))
15279 tree decl
= TREE_VALUE (t
);
15280 if (TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
15282 tree offset
= TREE_PURPOSE (t
);
15283 bool neg
= wi::neg_p (wi::to_wide (offset
));
15284 offset
= fold_unary (ABS_EXPR
, TREE_TYPE (offset
), offset
);
15285 tree t2
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
),
15286 neg
? MINUS_EXPR
: PLUS_EXPR
,
15288 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
15290 fold_convert (sizetype
, t2
),
15291 fold_convert (sizetype
, decl
));
15292 if (t2
== error_mark_node
)
15297 TREE_PURPOSE (t
) = t2
;
15302 gcc_unreachable ();
15303 case OMP_CLAUSE_DEPEND
:
15304 case OMP_CLAUSE_AFFINITY
:
15305 t
= OMP_CLAUSE_DECL (c
);
15306 if (TREE_CODE (t
) == TREE_LIST
15307 && TREE_PURPOSE (t
)
15308 && TREE_CODE (TREE_PURPOSE (t
)) == TREE_VEC
)
15310 if (TREE_PURPOSE (t
) != last_iterators
)
15311 last_iterators_remove
15312 = c_omp_finish_iterators (TREE_PURPOSE (t
));
15313 last_iterators
= TREE_PURPOSE (t
);
15314 t
= TREE_VALUE (t
);
15315 if (last_iterators_remove
)
15316 t
= error_mark_node
;
15319 last_iterators
= NULL_TREE
;
15320 if (TREE_CODE (t
) == TREE_LIST
)
15322 if (handle_omp_array_sections (c
, ort
))
15324 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
15325 && OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_DEPOBJ
)
15327 error_at (OMP_CLAUSE_LOCATION (c
),
15328 "%<depend%> clause with %<depobj%> dependence "
15329 "type on array section");
15334 if (t
== error_mark_node
)
15336 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
15337 && t
== ridpointers
[RID_OMP_ALL_MEMORY
])
15339 if (OMP_CLAUSE_DEPEND_KIND (c
) != OMP_CLAUSE_DEPEND_OUT
15340 && OMP_CLAUSE_DEPEND_KIND (c
) != OMP_CLAUSE_DEPEND_INOUT
)
15342 error_at (OMP_CLAUSE_LOCATION (c
),
15343 "%<omp_all_memory%> used with %<depend%> kind "
15344 "other than %<out%> or %<inout%>");
15348 else if (!lvalue_p (t
))
15350 error_at (OMP_CLAUSE_LOCATION (c
),
15351 "%qE is not lvalue expression nor array section in "
15353 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15356 else if (TREE_CODE (t
) == COMPONENT_REF
15357 && DECL_C_BIT_FIELD (TREE_OPERAND (t
, 1)))
15359 gcc_assert (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
15360 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_AFFINITY
);
15361 error_at (OMP_CLAUSE_LOCATION (c
),
15362 "bit-field %qE in %qs clause", t
,
15363 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15366 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
15367 && OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_DEPOBJ
)
15369 if (!c_omp_depend_t_p (TREE_TYPE (t
)))
15371 error_at (OMP_CLAUSE_LOCATION (c
),
15372 "%qE does not have %<omp_depend_t%> type in "
15373 "%<depend%> clause with %<depobj%> dependence "
15378 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
15379 && c_omp_depend_t_p (TREE_TYPE (t
)))
15381 error_at (OMP_CLAUSE_LOCATION (c
),
15382 "%qE should not have %<omp_depend_t%> type in "
15383 "%<depend%> clause with dependence type other than "
15389 if (t
== ridpointers
[RID_OMP_ALL_MEMORY
])
15390 t
= null_pointer_node
;
15393 tree addr
= build_unary_op (OMP_CLAUSE_LOCATION (c
),
15394 ADDR_EXPR
, t
, false);
15395 if (addr
== error_mark_node
)
15400 t
= build_indirect_ref (OMP_CLAUSE_LOCATION (c
), addr
,
15402 if (t
== error_mark_node
)
15408 if (TREE_CODE (OMP_CLAUSE_DECL (c
)) == TREE_LIST
15409 && TREE_PURPOSE (OMP_CLAUSE_DECL (c
))
15410 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c
)))
15412 TREE_VALUE (OMP_CLAUSE_DECL (c
)) = t
;
15414 OMP_CLAUSE_DECL (c
) = t
;
15418 case OMP_CLAUSE_MAP
:
15419 if (OMP_CLAUSE_MAP_IMPLICIT (c
) && !implicit_moved
)
15420 goto move_implicit
;
15422 case OMP_CLAUSE_TO
:
15423 case OMP_CLAUSE_FROM
:
15424 case OMP_CLAUSE__CACHE_
:
15426 using namespace omp_addr_tokenizer
;
15427 auto_vec
<omp_addr_token
*, 10> addr_tokens
;
15429 t
= OMP_CLAUSE_DECL (c
);
15430 if (TREE_CODE (t
) == TREE_LIST
)
15433 grp_sentinel
= OMP_CLAUSE_CHAIN (c
);
15435 if (handle_omp_array_sections (c
, ort
))
15439 t
= OMP_CLAUSE_DECL (c
);
15440 if (!omp_mappable_type (TREE_TYPE (t
)))
15442 error_at (OMP_CLAUSE_LOCATION (c
),
15443 "array section does not have mappable type "
15445 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15448 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
15450 error_at (OMP_CLAUSE_LOCATION (c
),
15451 "%<_Atomic%> %qE in %qs clause", t
,
15452 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15455 while (TREE_CODE (t
) == ARRAY_REF
)
15456 t
= TREE_OPERAND (t
, 0);
15458 c_omp_address_inspector
ai (OMP_CLAUSE_LOCATION (c
), t
);
15460 if (!omp_parse_expr (addr_tokens
, t
))
15462 sorry_at (OMP_CLAUSE_LOCATION (c
),
15463 "unsupported map expression %qE",
15464 OMP_CLAUSE_DECL (c
));
15469 /* This check is to determine if this will be the only map
15470 node created for this clause. Otherwise, we'll check
15471 the following FIRSTPRIVATE_POINTER or ATTACH_DETACH
15472 node on the next iteration(s) of the loop. */
15473 if (addr_tokens
.length () >= 4
15474 && addr_tokens
[0]->type
== STRUCTURE_BASE
15475 && addr_tokens
[0]->u
.structure_base_kind
== BASE_DECL
15476 && addr_tokens
[1]->type
== ACCESS_METHOD
15477 && addr_tokens
[2]->type
== COMPONENT_SELECTOR
15478 && addr_tokens
[3]->type
== ACCESS_METHOD
15479 && (addr_tokens
[3]->u
.access_kind
== ACCESS_DIRECT
15480 || (addr_tokens
[3]->u
.access_kind
15481 == ACCESS_INDEXED_ARRAY
)))
15483 tree rt
= addr_tokens
[1]->expr
;
15485 gcc_assert (DECL_P (rt
));
15487 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
15488 && OMP_CLAUSE_MAP_IMPLICIT (c
)
15489 && (bitmap_bit_p (&map_head
, DECL_UID (rt
))
15490 || bitmap_bit_p (&map_field_head
, DECL_UID (rt
))
15491 || bitmap_bit_p (&map_firstprivate_head
,
15497 if (bitmap_bit_p (&map_field_head
, DECL_UID (rt
)))
15499 if (bitmap_bit_p (&map_head
, DECL_UID (rt
)))
15501 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
15502 error_at (OMP_CLAUSE_LOCATION (c
),
15503 "%qD appears more than once in motion "
15506 error_at (OMP_CLAUSE_LOCATION (c
),
15507 "%qD appears more than once in data "
15510 error_at (OMP_CLAUSE_LOCATION (c
),
15511 "%qD appears more than once in map "
15517 bitmap_set_bit (&map_head
, DECL_UID (rt
));
15518 bitmap_set_bit (&map_field_head
, DECL_UID (rt
));
15522 if (c_oacc_check_attachments (c
))
15524 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
15525 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ATTACH
15526 || OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_DETACH
)
15527 && !OMP_CLAUSE_SIZE (c
))
15528 /* In this case, we have a single array element which is a
15529 pointer, and we already set OMP_CLAUSE_SIZE in
15530 handle_omp_array_sections above. For attach/detach
15531 clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
15533 OMP_CLAUSE_SIZE (c
) = size_zero_node
;
15536 else if (!omp_parse_expr (addr_tokens
, t
))
15538 sorry_at (OMP_CLAUSE_LOCATION (c
),
15539 "unsupported map expression %qE",
15540 OMP_CLAUSE_DECL (c
));
15544 if (t
== error_mark_node
)
15549 /* OpenACC attach / detach clauses must be pointers. */
15550 if (c_oacc_check_attachments (c
))
15555 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
15556 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_ATTACH
15557 || OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_DETACH
)
15558 && !OMP_CLAUSE_SIZE (c
))
15559 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15560 bias) to zero here, so it is not set erroneously to the pointer
15561 size later on in gimplify.cc. */
15562 OMP_CLAUSE_SIZE (c
) = size_zero_node
;
15564 c_omp_address_inspector
ai (OMP_CLAUSE_LOCATION (c
), t
);
15566 if (!ai
.check_clause (c
))
15572 if (!ai
.map_supported_p ())
15574 sorry_at (OMP_CLAUSE_LOCATION (c
),
15575 "unsupported map expression %qE",
15576 OMP_CLAUSE_DECL (c
));
15581 gcc_assert ((addr_tokens
[0]->type
== ARRAY_BASE
15582 || addr_tokens
[0]->type
== STRUCTURE_BASE
)
15583 && addr_tokens
[1]->type
== ACCESS_METHOD
);
15585 t
= addr_tokens
[1]->expr
;
15587 if (addr_tokens
[0]->u
.structure_base_kind
!= BASE_DECL
)
15588 goto skip_decl_checks
;
15590 /* For OpenMP, we can access a struct "t" and "t.d" on the same
15591 mapping. OpenACC allows multiple fields of the same structure
15593 if (addr_tokens
[0]->type
== STRUCTURE_BASE
15594 && (bitmap_bit_p (&map_field_head
, DECL_UID (t
))
15595 || (!openacc
&& bitmap_bit_p (&map_head
, DECL_UID (t
)))))
15596 goto skip_decl_checks
;
15598 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
15600 error_at (OMP_CLAUSE_LOCATION (c
),
15601 "%qE is not a variable in %qs clause", t
,
15602 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15605 else if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
15607 error_at (OMP_CLAUSE_LOCATION (c
),
15608 "%qD is threadprivate variable in %qs clause", t
,
15609 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15612 else if ((OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
15613 || (OMP_CLAUSE_MAP_KIND (c
)
15614 != GOMP_MAP_FIRSTPRIVATE_POINTER
))
15615 && !c_mark_addressable (t
))
15617 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
15618 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
15619 || (OMP_CLAUSE_MAP_KIND (c
)
15620 == GOMP_MAP_FIRSTPRIVATE_POINTER
)
15621 || (OMP_CLAUSE_MAP_KIND (c
)
15622 == GOMP_MAP_FORCE_DEVICEPTR
)))
15623 && t
== OMP_CLAUSE_DECL (c
)
15624 && !omp_mappable_type (TREE_TYPE (t
)))
15626 error_at (OMP_CLAUSE_LOCATION (c
),
15627 "%qD does not have a mappable type in %qs clause", t
,
15628 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15631 else if (TREE_TYPE (t
) == error_mark_node
)
15633 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
15635 error_at (OMP_CLAUSE_LOCATION (c
),
15636 "%<_Atomic%> %qE in %qs clause", t
,
15637 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15640 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
15641 && OMP_CLAUSE_MAP_IMPLICIT (c
)
15642 && (bitmap_bit_p (&map_head
, DECL_UID (t
))
15643 || bitmap_bit_p (&map_field_head
, DECL_UID (t
))
15644 || bitmap_bit_p (&map_firstprivate_head
,
15647 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
15648 && (OMP_CLAUSE_MAP_KIND (c
)
15649 == GOMP_MAP_FIRSTPRIVATE_POINTER
))
15651 if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
15652 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
15653 || bitmap_bit_p (&map_firstprivate_head
, DECL_UID (t
)))
15655 error_at (OMP_CLAUSE_LOCATION (c
),
15656 "%qD appears more than once in data clauses", t
);
15659 else if (bitmap_bit_p (&map_head
, DECL_UID (t
))
15660 && !bitmap_bit_p (&map_field_head
, DECL_UID (t
))
15663 error_at (OMP_CLAUSE_LOCATION (c
),
15664 "%qD appears more than once in data clauses", t
);
15668 bitmap_set_bit (&map_firstprivate_head
, DECL_UID (t
));
15670 else if (bitmap_bit_p (&map_head
, DECL_UID (t
))
15671 && !bitmap_bit_p (&map_field_head
, DECL_UID (t
))
15672 && ort
!= C_ORT_OMP
15673 && ort
!= C_ORT_OMP_EXIT_DATA
)
15675 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
15676 error_at (OMP_CLAUSE_LOCATION (c
),
15677 "%qD appears more than once in motion clauses", t
);
15679 error_at (OMP_CLAUSE_LOCATION (c
),
15680 "%qD appears more than once in data clauses", t
);
15682 error_at (OMP_CLAUSE_LOCATION (c
),
15683 "%qD appears more than once in map clauses", t
);
15686 else if (openacc
&& bitmap_bit_p (&generic_head
, DECL_UID (t
)))
15688 error_at (OMP_CLAUSE_LOCATION (c
),
15689 "%qD appears more than once in data clauses", t
);
15692 else if (bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
15693 || bitmap_bit_p (&is_on_device_head
, DECL_UID (t
)))
15696 error_at (OMP_CLAUSE_LOCATION (c
),
15697 "%qD appears more than once in data clauses", t
);
15699 error_at (OMP_CLAUSE_LOCATION (c
),
15700 "%qD appears both in data and map clauses", t
);
15703 else if (!omp_access_chain_p (addr_tokens
, 1))
15705 bitmap_set_bit (&map_head
, DECL_UID (t
));
15706 if (t
!= OMP_CLAUSE_DECL (c
)
15707 && TREE_CODE (OMP_CLAUSE_DECL (c
)) == COMPONENT_REF
)
15708 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
15712 /* If we call omp_expand_map_clause in handle_omp_array_sections,
15713 the containing loop (here) iterates through the new nodes
15714 created by that expansion. Avoid expanding those again (just
15715 by checking the node type). */
15717 && ort
!= C_ORT_DECLARE_SIMD
15718 && (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
15719 || (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FIRSTPRIVATE_POINTER
15720 && (OMP_CLAUSE_MAP_KIND (c
)
15721 != GOMP_MAP_FIRSTPRIVATE_REFERENCE
)
15722 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_ALWAYS_POINTER
15723 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_ATTACH_DETACH
15724 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_ATTACH
15725 && OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_DETACH
)))
15728 grp_sentinel
= OMP_CLAUSE_CHAIN (c
);
15729 tree nc
= ai
.expand_map_clause (c
, OMP_CLAUSE_DECL (c
),
15731 if (nc
!= error_mark_node
)
15737 case OMP_CLAUSE_ENTER
:
15738 case OMP_CLAUSE_LINK
:
15739 t
= OMP_CLAUSE_DECL (c
);
15741 cname
= omp_clause_code_name
[OMP_CLAUSE_CODE (c
)];
15742 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ENTER
15743 && OMP_CLAUSE_ENTER_TO (c
))
15745 if (TREE_CODE (t
) == FUNCTION_DECL
15746 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ENTER
)
15748 else if (!VAR_P (t
))
15750 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ENTER
)
15751 error_at (OMP_CLAUSE_LOCATION (c
),
15752 "%qE is neither a variable nor a function name in "
15753 "clause %qs", t
, cname
);
15755 error_at (OMP_CLAUSE_LOCATION (c
),
15756 "%qE is not a variable in clause %qs", t
, cname
);
15759 else if (DECL_THREAD_LOCAL_P (t
))
15761 error_at (OMP_CLAUSE_LOCATION (c
),
15762 "%qD is threadprivate variable in %qs clause", t
,
15766 else if (!omp_mappable_type (TREE_TYPE (t
)))
15768 error_at (OMP_CLAUSE_LOCATION (c
),
15769 "%qD does not have a mappable type in %qs clause", t
,
15775 if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
15777 error_at (OMP_CLAUSE_LOCATION (c
),
15778 "%qE appears more than once on the same "
15779 "%<declare target%> directive", t
);
15783 bitmap_set_bit (&generic_head
, DECL_UID (t
));
15786 case OMP_CLAUSE_UNIFORM
:
15787 t
= OMP_CLAUSE_DECL (c
);
15788 if (TREE_CODE (t
) != PARM_DECL
)
15791 error_at (OMP_CLAUSE_LOCATION (c
),
15792 "%qD is not an argument in %<uniform%> clause", t
);
15794 error_at (OMP_CLAUSE_LOCATION (c
),
15795 "%qE is not an argument in %<uniform%> clause", t
);
15799 /* map_head bitmap is used as uniform_head if declare_simd. */
15800 bitmap_set_bit (&map_head
, DECL_UID (t
));
15801 goto check_dup_generic
;
15803 case OMP_CLAUSE_IS_DEVICE_PTR
:
15804 case OMP_CLAUSE_USE_DEVICE_PTR
:
15805 t
= OMP_CLAUSE_DECL (c
);
15806 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IS_DEVICE_PTR
)
15807 bitmap_set_bit (&is_on_device_head
, DECL_UID (t
));
15808 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
15810 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_USE_DEVICE_PTR
15813 error_at (OMP_CLAUSE_LOCATION (c
),
15814 "%qs variable is not a pointer",
15815 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15818 else if (TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
15820 error_at (OMP_CLAUSE_LOCATION (c
),
15821 "%qs variable is neither a pointer nor an array",
15822 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
15826 goto check_dup_generic
;
15828 case OMP_CLAUSE_HAS_DEVICE_ADDR
:
15829 t
= OMP_CLAUSE_DECL (c
);
15830 if (TREE_CODE (t
) == TREE_LIST
)
15832 if (handle_omp_array_sections (c
, ort
))
15836 t
= OMP_CLAUSE_DECL (c
);
15837 while (TREE_CODE (t
) == ARRAY_REF
)
15838 t
= TREE_OPERAND (t
, 0);
15841 bitmap_set_bit (&is_on_device_head
, DECL_UID (t
));
15842 if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
15843 c_mark_addressable (t
);
15844 goto check_dup_generic_t
;
15846 case OMP_CLAUSE_USE_DEVICE_ADDR
:
15847 t
= OMP_CLAUSE_DECL (c
);
15848 if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
15849 c_mark_addressable (t
);
15850 goto check_dup_generic
;
15852 case OMP_CLAUSE_NOWAIT
:
15853 if (copyprivate_seen
)
15855 error_at (OMP_CLAUSE_LOCATION (c
),
15856 "%<nowait%> clause must not be used together "
15857 "with %<copyprivate%>");
15861 nowait_clause
= pc
;
15862 pc
= &OMP_CLAUSE_CHAIN (c
);
15865 case OMP_CLAUSE_ORDER
:
15866 if (ordered_clause
)
15868 error_at (OMP_CLAUSE_LOCATION (c
),
15869 "%<order%> clause must not be used together "
15870 "with %<ordered%>");
15874 else if (order_clause
)
15876 /* Silently remove duplicates. */
15881 pc
= &OMP_CLAUSE_CHAIN (c
);
15884 case OMP_CLAUSE_DETACH
:
15885 t
= OMP_CLAUSE_DECL (c
);
15888 error_at (OMP_CLAUSE_LOCATION (c
),
15889 "too many %qs clauses on a task construct",
15895 pc
= &OMP_CLAUSE_CHAIN (c
);
15896 c_mark_addressable (t
);
15899 case OMP_CLAUSE_IF
:
15900 case OMP_CLAUSE_SELF
:
15901 case OMP_CLAUSE_NUM_THREADS
:
15902 case OMP_CLAUSE_NUM_TEAMS
:
15903 case OMP_CLAUSE_THREAD_LIMIT
:
15904 case OMP_CLAUSE_DEFAULT
:
15905 case OMP_CLAUSE_UNTIED
:
15906 case OMP_CLAUSE_COLLAPSE
:
15907 case OMP_CLAUSE_FINAL
:
15908 case OMP_CLAUSE_DEVICE
:
15909 case OMP_CLAUSE_DIST_SCHEDULE
:
15910 case OMP_CLAUSE_PARALLEL
:
15911 case OMP_CLAUSE_FOR
:
15912 case OMP_CLAUSE_SECTIONS
:
15913 case OMP_CLAUSE_TASKGROUP
:
15914 case OMP_CLAUSE_PROC_BIND
:
15915 case OMP_CLAUSE_DEVICE_TYPE
:
15916 case OMP_CLAUSE_PRIORITY
:
15917 case OMP_CLAUSE_GRAINSIZE
:
15918 case OMP_CLAUSE_NUM_TASKS
:
15919 case OMP_CLAUSE_THREADS
:
15920 case OMP_CLAUSE_SIMD
:
15921 case OMP_CLAUSE_HINT
:
15922 case OMP_CLAUSE_FILTER
:
15923 case OMP_CLAUSE_DEFAULTMAP
:
15924 case OMP_CLAUSE_BIND
:
15925 case OMP_CLAUSE_NUM_GANGS
:
15926 case OMP_CLAUSE_NUM_WORKERS
:
15927 case OMP_CLAUSE_VECTOR_LENGTH
:
15928 case OMP_CLAUSE_ASYNC
:
15929 case OMP_CLAUSE_WAIT
:
15930 case OMP_CLAUSE_AUTO
:
15931 case OMP_CLAUSE_INDEPENDENT
:
15932 case OMP_CLAUSE_SEQ
:
15933 case OMP_CLAUSE_GANG
:
15934 case OMP_CLAUSE_WORKER
:
15935 case OMP_CLAUSE_VECTOR
:
15936 case OMP_CLAUSE_TILE
:
15937 case OMP_CLAUSE_IF_PRESENT
:
15938 case OMP_CLAUSE_FINALIZE
:
15939 case OMP_CLAUSE_NOHOST
:
15940 case OMP_CLAUSE_INDIRECT
:
15941 pc
= &OMP_CLAUSE_CHAIN (c
);
15944 case OMP_CLAUSE_MERGEABLE
:
15945 mergeable_seen
= true;
15946 pc
= &OMP_CLAUSE_CHAIN (c
);
15949 case OMP_CLAUSE_NOGROUP
:
15951 pc
= &OMP_CLAUSE_CHAIN (c
);
15954 case OMP_CLAUSE_SCHEDULE
:
15955 schedule_clause
= c
;
15956 pc
= &OMP_CLAUSE_CHAIN (c
);
15959 case OMP_CLAUSE_ORDERED
:
15960 ordered_clause
= c
;
15963 error_at (OMP_CLAUSE_LOCATION (*order_clause
),
15964 "%<order%> clause must not be used together "
15965 "with %<ordered%>");
15966 *order_clause
= OMP_CLAUSE_CHAIN (*order_clause
);
15967 order_clause
= NULL
;
15969 pc
= &OMP_CLAUSE_CHAIN (c
);
15972 case OMP_CLAUSE_SAFELEN
:
15974 pc
= &OMP_CLAUSE_CHAIN (c
);
15976 case OMP_CLAUSE_SIMDLEN
:
15978 pc
= &OMP_CLAUSE_CHAIN (c
);
15981 case OMP_CLAUSE_INBRANCH
:
15982 case OMP_CLAUSE_NOTINBRANCH
:
15985 error_at (OMP_CLAUSE_LOCATION (c
),
15986 "%<inbranch%> clause is incompatible with "
15987 "%<notinbranch%>");
15991 branch_seen
= true;
15992 pc
= &OMP_CLAUSE_CHAIN (c
);
15995 case OMP_CLAUSE_INCLUSIVE
:
15996 case OMP_CLAUSE_EXCLUSIVE
:
15997 need_complete
= true;
15998 need_implicitly_determined
= true;
15999 t
= OMP_CLAUSE_DECL (c
);
16000 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
16002 error_at (OMP_CLAUSE_LOCATION (c
),
16003 "%qE is not a variable in clause %qs", t
,
16004 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
16010 gcc_unreachable ();
16015 t
= OMP_CLAUSE_DECL (c
);
16019 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
16020 if (t
== error_mark_node
)
16024 if (need_implicitly_determined
)
16026 const char *share_name
= NULL
;
16028 if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
16029 share_name
= "threadprivate";
16030 else switch (c_omp_predetermined_sharing (t
))
16032 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
16034 case OMP_CLAUSE_DEFAULT_SHARED
:
16035 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
16036 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
)
16037 && c_omp_predefined_variable (t
))
16038 /* The __func__ variable and similar function-local
16039 predefined variables may be listed in a shared or
16040 firstprivate clause. */
16042 share_name
= "shared";
16044 case OMP_CLAUSE_DEFAULT_PRIVATE
:
16045 share_name
= "private";
16048 gcc_unreachable ();
16052 error_at (OMP_CLAUSE_LOCATION (c
),
16053 "%qE is predetermined %qs for %qs",
16055 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
16058 else if (TREE_READONLY (t
)
16059 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_SHARED
16060 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_FIRSTPRIVATE
)
16062 error_at (OMP_CLAUSE_LOCATION (c
),
16063 "%<const%> qualified %qE may appear only in "
16064 "%<shared%> or %<firstprivate%> clauses", t
);
16074 /* If we found a clause to remove, we want to remove the whole
16075 expanded group, otherwise gimplify
16076 (omp_resolve_clause_dependencies) can get confused. */
16077 *grp_start_p
= grp_sentinel
;
16079 grp_start_p
= NULL
;
16082 *pc
= OMP_CLAUSE_CHAIN (c
);
16085 pc
= &OMP_CLAUSE_CHAIN (c
);
16090 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen
),
16091 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)))
16093 error_at (OMP_CLAUSE_LOCATION (simdlen
),
16094 "%<simdlen%> clause value is bigger than "
16095 "%<safelen%> clause value");
16096 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)
16097 = OMP_CLAUSE_SAFELEN_EXPR (safelen
);
16102 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
16103 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
16105 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
16106 "%<nonmonotonic%> schedule modifier specified together "
16107 "with %<ordered%> clause");
16108 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
16109 = (enum omp_clause_schedule_kind
)
16110 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
16111 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
16114 if (reduction_seen
< 0 && ordered_clause
)
16116 error_at (OMP_CLAUSE_LOCATION (ordered_clause
),
16117 "%qs clause specified together with %<inscan%> "
16118 "%<reduction%> clause", "ordered");
16119 reduction_seen
= -2;
16122 if (reduction_seen
< 0 && schedule_clause
)
16124 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
16125 "%qs clause specified together with %<inscan%> "
16126 "%<reduction%> clause", "schedule");
16127 reduction_seen
= -2;
16130 if (linear_variable_step_check
16131 || reduction_seen
== -2
16133 || target_in_reduction_seen
)
16134 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
16136 bool remove
= false;
16138 switch (OMP_CLAUSE_CODE (c
))
16140 case OMP_CLAUSE_REDUCTION
:
16141 case OMP_CLAUSE_IN_REDUCTION
:
16142 case OMP_CLAUSE_TASK_REDUCTION
:
16143 if (TREE_CODE (OMP_CLAUSE_DECL (c
)) == MEM_REF
)
16145 t
= TREE_OPERAND (OMP_CLAUSE_DECL (c
), 0);
16146 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
16147 t
= TREE_OPERAND (t
, 0);
16148 if (TREE_CODE (t
) == ADDR_EXPR
16149 || INDIRECT_REF_P (t
))
16150 t
= TREE_OPERAND (t
, 0);
16152 bitmap_clear_bit (&aligned_head
, DECL_UID (t
));
16156 case OMP_CLAUSE_PRIVATE
:
16157 case OMP_CLAUSE_FIRSTPRIVATE
:
16158 case OMP_CLAUSE_LASTPRIVATE
:
16159 case OMP_CLAUSE_LINEAR
:
16160 if (DECL_P (OMP_CLAUSE_DECL (c
)))
16161 bitmap_clear_bit (&aligned_head
,
16162 DECL_UID (OMP_CLAUSE_DECL (c
)));
16167 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
16168 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
)
16169 && !bitmap_bit_p (&map_head
,
16170 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c
))))
16172 error_at (OMP_CLAUSE_LOCATION (c
),
16173 "%<linear%> clause step is a parameter %qD not "
16174 "specified in %<uniform%> clause",
16175 OMP_CLAUSE_LINEAR_STEP (c
));
16178 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
16179 && reduction_seen
== -2)
16180 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 0;
16181 if (target_in_reduction_seen
16182 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
)
16184 tree t
= OMP_CLAUSE_DECL (c
);
16185 while (handled_component_p (t
)
16186 || INDIRECT_REF_P (t
)
16187 || TREE_CODE (t
) == ADDR_EXPR
16188 || TREE_CODE (t
) == MEM_REF
16189 || TREE_CODE (t
) == NON_LVALUE_EXPR
)
16190 t
= TREE_OPERAND (t
, 0);
16192 && bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
16193 OMP_CLAUSE_MAP_IN_REDUCTION (c
) = 1;
16197 *pc
= OMP_CLAUSE_CHAIN (c
);
16199 pc
= &OMP_CLAUSE_CHAIN (c
);
16203 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
16205 bool remove
= false;
16206 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ALLOCATE
16207 && !OMP_CLAUSE_ALLOCATE_COMBINED (c
)
16208 && bitmap_bit_p (&aligned_head
, DECL_UID (OMP_CLAUSE_DECL (c
))))
16210 error_at (OMP_CLAUSE_LOCATION (c
),
16211 "%qD specified in %<allocate%> clause but not in "
16212 "an explicit privatization clause", OMP_CLAUSE_DECL (c
));
16216 *pc
= OMP_CLAUSE_CHAIN (c
);
16218 pc
= &OMP_CLAUSE_CHAIN (c
);
16221 if (nogroup_seen
&& reduction_seen
)
16223 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen
),
16224 "%<nogroup%> clause must not be used together with "
16225 "%<reduction%> clause");
16226 *nogroup_seen
= OMP_CLAUSE_CHAIN (*nogroup_seen
);
16231 if (mergeable_seen
)
16233 error_at (OMP_CLAUSE_LOCATION (*detach_seen
),
16234 "%<detach%> clause must not be used together with "
16235 "%<mergeable%> clause");
16236 *detach_seen
= OMP_CLAUSE_CHAIN (*detach_seen
);
16240 tree detach_decl
= OMP_CLAUSE_DECL (*detach_seen
);
16242 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
16244 bool remove
= false;
16245 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
16246 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
16247 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
16248 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
16249 && OMP_CLAUSE_DECL (c
) == detach_decl
)
16251 error_at (OMP_CLAUSE_LOCATION (c
),
16252 "the event handle of a %<detach%> clause "
16253 "should not be in a data-sharing clause");
16257 *pc
= OMP_CLAUSE_CHAIN (c
);
16259 pc
= &OMP_CLAUSE_CHAIN (c
);
16264 bitmap_obstack_release (NULL
);
16268 /* Return code to initialize DST with a copy constructor from SRC.
16269 C doesn't have copy constructors nor assignment operators, only for
16270 _Atomic vars we need to perform __atomic_load from src into a temporary
16271 followed by __atomic_store of the temporary to dst. */
16274 c_omp_clause_copy_ctor (tree clause
, tree dst
, tree src
)
16276 if (!really_atomic_lvalue (dst
) && !really_atomic_lvalue (src
))
16277 return build2 (MODIFY_EXPR
, TREE_TYPE (dst
), dst
, src
);
16279 location_t loc
= OMP_CLAUSE_LOCATION (clause
);
16280 tree type
= TREE_TYPE (dst
);
16281 tree nonatomic_type
= build_qualified_type (type
, TYPE_UNQUALIFIED
);
16282 tree tmp
= create_tmp_var (nonatomic_type
);
16283 tree tmp_addr
= build_fold_addr_expr (tmp
);
16284 TREE_ADDRESSABLE (tmp
) = 1;
16285 suppress_warning (tmp
);
16286 tree src_addr
= build_fold_addr_expr (src
);
16287 tree dst_addr
= build_fold_addr_expr (dst
);
16288 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
16289 vec
<tree
, va_gc
> *params
;
16290 /* Expansion of a generic atomic load may require an addition
16291 element, so allocate enough to prevent a resize. */
16292 vec_alloc (params
, 4);
16294 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
16295 tree fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
16296 params
->quick_push (src_addr
);
16297 params
->quick_push (tmp_addr
);
16298 params
->quick_push (seq_cst
);
16299 tree load
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
16301 vec_alloc (params
, 4);
16303 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
16304 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
16305 params
->quick_push (dst_addr
);
16306 params
->quick_push (tmp_addr
);
16307 params
->quick_push (seq_cst
);
16308 tree store
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
16309 return build2 (COMPOUND_EXPR
, void_type_node
, load
, store
);
16312 /* Create a transaction node. */
16315 c_finish_transaction (location_t loc
, tree block
, int flags
)
16317 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
16318 if (flags
& TM_STMT_ATTR_OUTER
)
16319 TRANSACTION_EXPR_OUTER (stmt
) = 1;
16320 if (flags
& TM_STMT_ATTR_RELAXED
)
16321 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
16322 return add_stmt (stmt
);
16325 /* Make a variant type in the proper way for C/C++, propagating qualifiers
16326 down to the element type of an array. If ORIG_QUAL_TYPE is not
16327 NULL, then it should be used as the qualified type
16328 ORIG_QUAL_INDIRECT levels down in array type derivation (to
16329 preserve information about the typedef name from which an array
16330 type was derived). */
16333 c_build_qualified_type (tree type
, int type_quals
, tree orig_qual_type
,
16334 size_t orig_qual_indirect
)
16336 if (type
== error_mark_node
)
16339 if (TREE_CODE (type
) == ARRAY_TYPE
)
16342 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
16343 type_quals
, orig_qual_type
,
16344 orig_qual_indirect
- 1);
16346 /* See if we already have an identically qualified type. */
16347 if (orig_qual_type
&& orig_qual_indirect
== 0)
16348 t
= orig_qual_type
;
16350 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
16352 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
16353 && TYPE_NAME (t
) == TYPE_NAME (type
)
16354 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
16355 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
16356 TYPE_ATTRIBUTES (type
)))
16361 tree domain
= TYPE_DOMAIN (type
);
16363 t
= build_variant_type_copy (type
);
16364 TREE_TYPE (t
) = element_type
;
16365 TYPE_ADDR_SPACE (t
) = TYPE_ADDR_SPACE (element_type
);
16367 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
16368 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
16369 SET_TYPE_STRUCTURAL_EQUALITY (t
);
16370 else if (TYPE_CANONICAL (element_type
) != element_type
16371 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
16373 tree unqualified_canon
16374 = build_array_type (TYPE_CANONICAL (element_type
),
16375 domain
? TYPE_CANONICAL (domain
)
16377 if (TYPE_REVERSE_STORAGE_ORDER (type
))
16380 = build_distinct_type_copy (unqualified_canon
);
16381 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon
) = 1;
16384 = c_build_qualified_type (unqualified_canon
, type_quals
);
16387 TYPE_CANONICAL (t
) = t
;
16392 /* A restrict-qualified pointer type must be a pointer to object or
16393 incomplete type. Note that the use of POINTER_TYPE_P also allows
16394 REFERENCE_TYPEs, which is appropriate for C++. */
16395 if ((type_quals
& TYPE_QUAL_RESTRICT
)
16396 && (!POINTER_TYPE_P (type
)
16397 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
16399 error ("invalid use of %<restrict%>");
16400 type_quals
&= ~TYPE_QUAL_RESTRICT
;
16403 tree var_type
= (orig_qual_type
&& orig_qual_indirect
== 0
16405 : build_qualified_type (type
, type_quals
));
16406 /* A variant type does not inherit the list of incomplete vars from the
16407 type main variant. */
16408 if ((RECORD_OR_UNION_TYPE_P (var_type
)
16409 || TREE_CODE (var_type
) == ENUMERAL_TYPE
)
16410 && TYPE_MAIN_VARIANT (var_type
) != var_type
)
16411 C_TYPE_INCOMPLETE_VARS (var_type
) = 0;
16415 /* Build a VA_ARG_EXPR for the C parser. */
16418 c_build_va_arg (location_t loc1
, tree expr
, location_t loc2
, tree type
)
16420 if (error_operand_p (type
))
16421 return error_mark_node
;
16422 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
16423 order because it takes the address of the expression. */
16424 else if (handled_component_p (expr
)
16425 && reverse_storage_order_for_component_p (expr
))
16427 error_at (loc1
, "cannot use %<va_arg%> with reverse storage order");
16428 return error_mark_node
;
16430 else if (!COMPLETE_TYPE_P (type
))
16432 error_at (loc2
, "second argument to %<va_arg%> is of incomplete "
16434 return error_mark_node
;
16436 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
16438 error_at (loc2
, "second argument to %<va_arg%> is a function type %qT",
16440 return error_mark_node
;
16442 else if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
16443 warning_at (loc2
, OPT_Wc___compat
,
16444 "C++ requires promoted type, not enum type, in %<va_arg%>");
16445 return build_va_arg (loc2
, expr
, type
);
16448 /* Return truthvalue of whether T1 is the same tree structure as T2.
16449 Return 1 if they are the same. Return false if they are different. */
16452 c_tree_equal (tree t1
, tree t2
)
16454 enum tree_code code1
, code2
;
16461 for (code1
= TREE_CODE (t1
); code1
== NON_LVALUE_EXPR
;
16462 code1
= TREE_CODE (t1
))
16463 t1
= TREE_OPERAND (t1
, 0);
16464 for (code2
= TREE_CODE (t2
); code2
== NON_LVALUE_EXPR
;
16465 code2
= TREE_CODE (t2
))
16466 t2
= TREE_OPERAND (t2
, 0);
16468 /* They might have become equal now. */
16472 if (code1
!= code2
)
16475 if (CONSTANT_CLASS_P (t1
) && !comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
)))
16481 return wi::to_wide (t1
) == wi::to_wide (t2
);
16484 return real_equal (&TREE_REAL_CST (t1
), &TREE_REAL_CST (t2
));
16487 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
16488 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
16489 TREE_STRING_LENGTH (t1
));
16492 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
16493 TREE_FIXED_CST (t2
));
16496 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
16497 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
16500 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
16503 /* We need to do this when determining whether or not two
16504 non-type pointer to member function template arguments
16506 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
16507 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
16512 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
16514 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
16515 if (!c_tree_equal (field
, elt2
->index
)
16516 || !c_tree_equal (value
, elt2
->value
))
16523 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
16525 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
16527 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
16530 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
16535 call_expr_arg_iterator iter1
, iter2
;
16536 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
16538 for (arg1
= first_call_expr_arg (t1
, &iter1
),
16539 arg2
= first_call_expr_arg (t2
, &iter2
);
16541 arg1
= next_call_expr_arg (&iter1
),
16542 arg2
= next_call_expr_arg (&iter2
))
16543 if (!c_tree_equal (arg1
, arg2
))
16552 tree o1
= TREE_OPERAND (t1
, 0);
16553 tree o2
= TREE_OPERAND (t2
, 0);
16555 /* Special case: if either target is an unallocated VAR_DECL,
16556 it means that it's going to be unified with whatever the
16557 TARGET_EXPR is really supposed to initialize, so treat it
16558 as being equivalent to anything. */
16559 if (VAR_P (o1
) && DECL_NAME (o1
) == NULL_TREE
16560 && !DECL_RTL_SET_P (o1
))
16562 else if (VAR_P (o2
) && DECL_NAME (o2
) == NULL_TREE
16563 && !DECL_RTL_SET_P (o2
))
16565 else if (!c_tree_equal (o1
, o2
))
16568 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
16571 case COMPONENT_REF
:
16572 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
16574 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
16580 case FUNCTION_DECL
:
16581 case IDENTIFIER_NODE
:
16588 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
16590 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
16591 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
16592 TREE_VEC_ELT (t2
, ix
)))
16598 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
)))
16606 switch (TREE_CODE_CLASS (code1
))
16610 case tcc_comparison
:
16611 case tcc_expression
:
16613 case tcc_reference
:
16614 case tcc_statement
:
16616 int i
, n
= TREE_OPERAND_LENGTH (t1
);
16620 case PREINCREMENT_EXPR
:
16621 case PREDECREMENT_EXPR
:
16622 case POSTINCREMENT_EXPR
:
16623 case POSTDECREMENT_EXPR
:
16633 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
16634 && n
!= TREE_OPERAND_LENGTH (t2
))
16637 for (i
= 0; i
< n
; ++i
)
16638 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
16645 return comptypes (t1
, t2
);
16647 gcc_unreachable ();
16651 /* Returns true when the function declaration FNDECL is implicit,
16652 introduced as a result of a call to an otherwise undeclared
16653 function, and false otherwise. */
16656 c_decl_implicit (const_tree fndecl
)
16658 return C_DECL_IMPLICIT (fndecl
);