1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
28 #include "coretypes.h"
34 #include "gimple-expr.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
40 #include "langhooks.h"
43 #include "tree-iterator.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
56 /* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
65 /* The level of nesting inside "__alignof__". */
68 /* The level of nesting inside "sizeof". */
71 /* The level of nesting inside "typeof". */
74 /* The argument of last parsed sizeof expression, only to be tested
75 if expr.original_code == SIZEOF_EXPR. */
76 tree c_last_sizeof_arg
;
77 location_t c_last_sizeof_loc
;
79 /* Nonzero if we might need to print a "missing braces around
80 initializer" message within this initializer. */
81 static int found_missing_braces
;
83 static int require_constant_value
;
84 static int require_constant_elements
;
86 static bool null_pointer_constant_p (const_tree
);
87 static tree
qualify_type (tree
, tree
);
88 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
90 static int comp_target_types (location_t
, tree
, tree
);
91 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
93 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
94 static tree
lookup_field (tree
, tree
);
95 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
96 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
98 static tree
pointer_diff (location_t
, tree
, tree
, tree
*);
99 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
100 enum impl_conv
, bool, tree
, tree
, int,
102 static tree
valid_compound_expr_initializer (tree
, tree
);
103 static void push_string (const char *);
104 static void push_member_name (tree
);
105 static int spelling_length (void);
106 static char *print_spelling (char *);
107 static void warning_init (location_t
, int, const char *);
108 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
109 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, bool,
110 bool, struct obstack
*);
111 static void output_pending_init_elements (int, struct obstack
*);
112 static bool set_designator (location_t
, bool, struct obstack
*);
113 static void push_range_stack (tree
, struct obstack
*);
114 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
116 static void set_nonincremental_init (struct obstack
*);
117 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
118 static tree
find_init_member (tree
, struct obstack
*);
119 static void readonly_warning (tree
, enum lvalue_use
);
120 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
121 static void record_maybe_used_decl (tree
);
122 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
124 /* Return true if EXP is a null pointer constant, false otherwise. */
127 null_pointer_constant_p (const_tree expr
)
129 /* This should really operate on c_expr structures, but they aren't
130 yet available everywhere required. */
131 tree type
= TREE_TYPE (expr
);
132 return (TREE_CODE (expr
) == INTEGER_CST
133 && !TREE_OVERFLOW (expr
)
134 && integer_zerop (expr
)
135 && (INTEGRAL_TYPE_P (type
)
136 || (TREE_CODE (type
) == POINTER_TYPE
137 && VOID_TYPE_P (TREE_TYPE (type
))
138 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
141 /* EXPR may appear in an unevaluated part of an integer constant
142 expression, but not in an evaluated part. Wrap it in a
143 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
147 note_integer_operands (tree expr
)
150 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
152 ret
= copy_node (expr
);
153 TREE_OVERFLOW (ret
) = 1;
157 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
158 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
163 /* Having checked whether EXPR may appear in an unevaluated part of an
164 integer constant expression and found that it may, remove any
165 C_MAYBE_CONST_EXPR noting this fact and return the resulting
169 remove_c_maybe_const_expr (tree expr
)
171 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
172 return C_MAYBE_CONST_EXPR_EXPR (expr
);
177 \f/* This is a cache to hold if two types are compatible or not. */
179 struct tagged_tu_seen_cache
{
180 const struct tagged_tu_seen_cache
* next
;
183 /* The return value of tagged_types_tu_compatible_p if we had seen
184 these two types already. */
188 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
189 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
191 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
192 does not have an incomplete type. (That includes void types.)
193 LOC is the location of the use. */
196 require_complete_type (location_t loc
, tree value
)
198 tree type
= TREE_TYPE (value
);
200 if (error_operand_p (value
))
201 return error_mark_node
;
203 /* First, detect a valid value with a complete type. */
204 if (COMPLETE_TYPE_P (type
))
207 c_incomplete_type_error (loc
, value
, type
);
208 return error_mark_node
;
211 /* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
213 and TYPE is the type that was invalid. LOC is the location for
217 c_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
219 /* Avoid duplicate error message. */
220 if (TREE_CODE (type
) == ERROR_MARK
)
223 if (value
!= NULL_TREE
&& (VAR_P (value
) || TREE_CODE (value
) == PARM_DECL
))
224 error_at (loc
, "%qD has an incomplete type %qT", value
, type
);
228 /* We must print an error message. Be clever about what it says. */
230 switch (TREE_CODE (type
))
238 error_at (loc
, "invalid use of void expression");
242 if (TYPE_DOMAIN (type
))
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
246 error_at (loc
, "invalid use of flexible array member");
249 type
= TREE_TYPE (type
);
252 error_at (loc
, "invalid use of array with unspecified bounds");
259 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
260 error_at (loc
, "invalid use of undefined type %qT", type
);
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error_at (loc
, "invalid use of incomplete typedef %qT", type
);
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
271 c_type_promotes_to (tree type
)
273 tree ret
= NULL_TREE
;
275 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
276 ret
= double_type_node
;
277 else if (c_promoting_integer_type_p (type
))
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type
)
281 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
282 ret
= unsigned_type_node
;
284 ret
= integer_type_node
;
287 if (ret
!= NULL_TREE
)
288 return (TYPE_ATOMIC (type
)
289 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
295 /* Return true if between two named address spaces, whether there is a superset
296 named address space that encompasses both address spaces. If there is a
297 superset, return which address space is the superset. */
300 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
307 else if (targetm
.addr_space
.subset_p (as1
, as2
))
312 else if (targetm
.addr_space
.subset_p (as2
, as1
))
321 /* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
325 qualify_type (tree type
, tree like
)
327 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
328 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
329 addr_space_t as_common
;
331 /* If the two named address spaces are different, determine the common
332 superset address space. If there isn't one, raise an error. */
333 if (!addr_space_superset (as_type
, as_like
, &as_common
))
336 error ("%qT and %qT are in disjoint named address spaces",
340 return c_build_qualified_type (type
,
341 TYPE_QUALS_NO_ADDR_SPACE (type
)
342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
343 | ENCODE_QUAL_ADDR_SPACE (as_common
));
346 /* Return true iff the given tree T is a variable length array. */
349 c_vla_type_p (const_tree t
)
351 if (TREE_CODE (t
) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t
))
357 /* Return the composite type of two compatible types.
359 We assume that comptypes has already been done and returned
360 nonzero; if that isn't so, this may crash. In particular, we
361 assume that qualifiers match. */
364 composite_type (tree t1
, tree t2
)
366 enum tree_code code1
;
367 enum tree_code code2
;
370 /* Save time if the two types are the same. */
372 if (t1
== t2
) return t1
;
374 /* If one type is nonsense, use the other. */
375 if (t1
== error_mark_node
)
377 if (t2
== error_mark_node
)
380 code1
= TREE_CODE (t1
);
381 code2
= TREE_CODE (t2
);
383 /* Merge the attributes. */
384 attributes
= targetm
.merge_type_attributes (t1
, t2
);
386 /* If one is an enumerated type and the other is the compatible
387 integer type, the composite type might be either of the two
388 (DR#013 question 3). For consistency, use the enumerated type as
389 the composite type. */
391 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
393 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
396 gcc_assert (code1
== code2
);
401 /* For two pointers, do this recursively on the target type. */
403 tree pointed_to_1
= TREE_TYPE (t1
);
404 tree pointed_to_2
= TREE_TYPE (t2
);
405 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
406 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
407 t1
= build_type_attribute_variant (t1
, attributes
);
408 return qualify_type (t1
, t2
);
413 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
416 tree d1
= TYPE_DOMAIN (t1
);
417 tree d2
= TYPE_DOMAIN (t2
);
418 bool d1_variable
, d2_variable
;
419 bool d1_zero
, d2_zero
;
420 bool t1_complete
, t2_complete
;
422 /* We should not have any type quals on arrays at all. */
423 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
424 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
426 t1_complete
= COMPLETE_TYPE_P (t1
);
427 t2_complete
= COMPLETE_TYPE_P (t2
);
429 d1_zero
= d1
== NULL_TREE
|| !TYPE_MAX_VALUE (d1
);
430 d2_zero
= d2
== NULL_TREE
|| !TYPE_MAX_VALUE (d2
);
432 d1_variable
= (!d1_zero
433 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
434 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
435 d2_variable
= (!d2_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
438 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
439 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
441 /* Save space: see if the result is identical to one of the args. */
442 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
443 && (d2_variable
|| d2_zero
|| !d1_variable
))
444 return build_type_attribute_variant (t1
, attributes
);
445 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
446 && (d1_variable
|| d1_zero
|| !d2_variable
))
447 return build_type_attribute_variant (t2
, attributes
);
449 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
450 return build_type_attribute_variant (t1
, attributes
);
451 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
452 return build_type_attribute_variant (t2
, attributes
);
454 /* Merge the element types, and have a size if either arg has
455 one. We may have qualifiers on the element types. To set
456 up TYPE_MAIN_VARIANT correctly, we need to form the
457 composite of the unqualified types and add the qualifiers
459 quals
= TYPE_QUALS (strip_array_types (elt
));
460 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
461 t1
= build_array_type (unqual_elt
,
462 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
468 /* Ensure a composite type involving a zero-length array type
469 is a zero-length type not an incomplete type. */
470 if (d1_zero
&& d2_zero
471 && (t1_complete
|| t2_complete
)
472 && !COMPLETE_TYPE_P (t1
))
474 TYPE_SIZE (t1
) = bitsize_zero_node
;
475 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
477 t1
= c_build_qualified_type (t1
, quals
);
478 return build_type_attribute_variant (t1
, attributes
);
484 if (attributes
!= NULL
)
486 /* Try harder not to create a new aggregate type. */
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
489 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
492 return build_type_attribute_variant (t1
, attributes
);
495 /* Function types: prefer the one that specified arg types.
496 If both do, merge the arg types. Also merge the return types. */
498 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
499 tree p1
= TYPE_ARG_TYPES (t1
);
500 tree p2
= TYPE_ARG_TYPES (t2
);
505 /* Save space: see if the result is identical to one of the args. */
506 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
507 return build_type_attribute_variant (t1
, attributes
);
508 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
509 return build_type_attribute_variant (t2
, attributes
);
511 /* Simple way if one arg fails to specify argument types. */
512 if (TYPE_ARG_TYPES (t1
) == NULL_TREE
)
514 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
515 t1
= build_type_attribute_variant (t1
, attributes
);
516 return qualify_type (t1
, t2
);
518 if (TYPE_ARG_TYPES (t2
) == NULL_TREE
)
520 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
521 t1
= build_type_attribute_variant (t1
, attributes
);
522 return qualify_type (t1
, t2
);
525 /* If both args specify argument types, we must merge the two
526 lists, argument by argument. */
528 for (len
= 0, newargs
= p1
;
529 newargs
&& newargs
!= void_list_node
;
530 len
++, newargs
= TREE_CHAIN (newargs
))
533 for (i
= 0; i
< len
; i
++)
534 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
538 for (; p1
&& p1
!= void_list_node
;
539 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
541 /* A null type means arg type is not specified.
542 Take whatever the other function type has. */
543 if (TREE_VALUE (p1
) == NULL_TREE
)
545 TREE_VALUE (n
) = TREE_VALUE (p2
);
548 if (TREE_VALUE (p2
) == NULL_TREE
)
550 TREE_VALUE (n
) = TREE_VALUE (p1
);
554 /* Given wait (union {union wait *u; int *i} *)
555 and wait (union wait *),
556 prefer union wait * as type of parm. */
557 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
558 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
561 tree mv2
= TREE_VALUE (p2
);
562 if (mv2
&& mv2
!= error_mark_node
563 && TREE_CODE (mv2
) != ARRAY_TYPE
)
564 mv2
= TYPE_MAIN_VARIANT (mv2
);
565 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
566 memb
; memb
= DECL_CHAIN (memb
))
568 tree mv3
= TREE_TYPE (memb
);
569 if (mv3
&& mv3
!= error_mark_node
570 && TREE_CODE (mv3
) != ARRAY_TYPE
)
571 mv3
= TYPE_MAIN_VARIANT (mv3
);
572 if (comptypes (mv3
, mv2
))
574 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
576 pedwarn (input_location
, OPT_Wpedantic
,
577 "function types not truly compatible in ISO C");
582 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
583 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
586 tree mv1
= TREE_VALUE (p1
);
587 if (mv1
&& mv1
!= error_mark_node
588 && TREE_CODE (mv1
) != ARRAY_TYPE
)
589 mv1
= TYPE_MAIN_VARIANT (mv1
);
590 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
591 memb
; memb
= DECL_CHAIN (memb
))
593 tree mv3
= TREE_TYPE (memb
);
594 if (mv3
&& mv3
!= error_mark_node
595 && TREE_CODE (mv3
) != ARRAY_TYPE
)
596 mv3
= TYPE_MAIN_VARIANT (mv3
);
597 if (comptypes (mv3
, mv1
))
599 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
601 pedwarn (input_location
, OPT_Wpedantic
,
602 "function types not truly compatible in ISO C");
607 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
611 t1
= build_function_type (valtype
, newargs
);
612 t1
= qualify_type (t1
, t2
);
617 return build_type_attribute_variant (t1
, attributes
);
622 /* Return the type of a conditional expression between pointers to
623 possibly differently qualified versions of compatible types.
625 We assume that comp_target_types has already been done and returned
626 nonzero; if that isn't so, this may crash. */
629 common_pointer_type (tree t1
, tree t2
)
632 tree pointed_to_1
, mv1
;
633 tree pointed_to_2
, mv2
;
635 unsigned target_quals
;
636 addr_space_t as1
, as2
, as_common
;
639 /* Save time if the two types are the same. */
641 if (t1
== t2
) return t1
;
643 /* If one type is nonsense, use the other. */
644 if (t1
== error_mark_node
)
646 if (t2
== error_mark_node
)
649 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
650 && TREE_CODE (t2
) == POINTER_TYPE
);
652 /* Merge the attributes. */
653 attributes
= targetm
.merge_type_attributes (t1
, t2
);
655 /* Find the composite type of the target types, and combine the
656 qualifiers of the two types' targets. Do not lose qualifiers on
657 array element types by taking the TYPE_MAIN_VARIANT. */
658 mv1
= pointed_to_1
= TREE_TYPE (t1
);
659 mv2
= pointed_to_2
= TREE_TYPE (t2
);
660 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
661 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
662 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
663 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
664 target
= composite_type (mv1
, mv2
);
666 /* Strip array types to get correct qualifier for pointers to arrays */
667 quals1
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1
));
668 quals2
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2
));
670 /* For function types do not merge const qualifiers, but drop them
671 if used inconsistently. The middle-end uses these to mark const
672 and noreturn functions. */
673 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
674 target_quals
= (quals1
& quals2
);
676 target_quals
= (quals1
| quals2
);
678 /* If the two named address spaces are different, determine the common
679 superset address space. This is guaranteed to exist due to the
680 assumption that comp_target_type returned non-zero. */
681 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
682 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
683 if (!addr_space_superset (as1
, as2
, &as_common
))
686 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
688 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
689 return build_type_attribute_variant (t1
, attributes
);
692 /* Return the common type for two arithmetic types under the usual
693 arithmetic conversions. The default conversions have already been
694 applied, and enumerated types converted to their compatible integer
695 types. The resulting type is unqualified and has no attributes.
697 This is the type for the result of most arithmetic operations
698 if the operands have the given two types. */
701 c_common_type (tree t1
, tree t2
)
703 enum tree_code code1
;
704 enum tree_code code2
;
706 /* If one type is nonsense, use the other. */
707 if (t1
== error_mark_node
)
709 if (t2
== error_mark_node
)
712 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
713 t1
= TYPE_MAIN_VARIANT (t1
);
715 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
716 t2
= TYPE_MAIN_VARIANT (t2
);
718 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
719 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
721 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
722 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
724 /* Save time if the two types are the same. */
726 if (t1
== t2
) return t1
;
728 code1
= TREE_CODE (t1
);
729 code2
= TREE_CODE (t2
);
731 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
732 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
733 || code1
== INTEGER_TYPE
);
734 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
735 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
736 || code2
== INTEGER_TYPE
);
738 /* When one operand is a decimal float type, the other operand cannot be
739 a generic float type or a complex type. We also disallow vector types
741 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
742 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
744 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
746 error ("cannot mix operands of decimal floating and vector types");
747 return error_mark_node
;
749 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
751 error ("cannot mix operands of decimal floating and complex types");
752 return error_mark_node
;
754 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
756 error ("cannot mix operands of decimal floating "
757 "and other floating types");
758 return error_mark_node
;
762 /* If one type is a vector type, return that type. (How the usual
763 arithmetic conversions apply to the vector types extension is not
764 precisely specified.) */
765 if (code1
== VECTOR_TYPE
)
768 if (code2
== VECTOR_TYPE
)
771 /* If one type is complex, form the common type of the non-complex
772 components, then make that complex. Use T1 or T2 if it is the
774 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
776 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
777 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
778 tree subtype
= c_common_type (subtype1
, subtype2
);
780 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
782 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
785 return build_complex_type (subtype
);
788 /* If only one is real, use it as the result. */
790 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
793 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
796 /* If both are real and either are decimal floating point types, use
797 the decimal floating point type with the greater precision. */
799 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
801 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
802 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
803 return dfloat128_type_node
;
804 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
805 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
806 return dfloat64_type_node
;
807 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
808 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
809 return dfloat32_type_node
;
812 /* Deal with fixed-point types. */
813 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
815 unsigned int unsignedp
= 0, satp
= 0;
817 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
819 m1
= SCALAR_TYPE_MODE (t1
);
820 m2
= SCALAR_TYPE_MODE (t2
);
822 /* If one input type is saturating, the result type is saturating. */
823 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
826 /* If both fixed-point types are unsigned, the result type is unsigned.
827 When mixing fixed-point and integer types, follow the sign of the
829 Otherwise, the result type is signed. */
830 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
831 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
832 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t1
))
834 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
835 && TYPE_UNSIGNED (t2
)))
838 /* The result type is signed. */
841 /* If the input type is unsigned, we need to convert to the
843 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
845 enum mode_class mclass
= (enum mode_class
) 0;
846 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
848 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
852 m1
= as_a
<scalar_mode
>
853 (mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0));
855 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
857 enum mode_class mclass
= (enum mode_class
) 0;
858 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
860 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
864 m2
= as_a
<scalar_mode
>
865 (mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0));
869 if (code1
== FIXED_POINT_TYPE
)
871 fbit1
= GET_MODE_FBIT (m1
);
872 ibit1
= GET_MODE_IBIT (m1
);
877 /* Signed integers need to subtract one sign bit. */
878 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
881 if (code2
== FIXED_POINT_TYPE
)
883 fbit2
= GET_MODE_FBIT (m2
);
884 ibit2
= GET_MODE_IBIT (m2
);
889 /* Signed integers need to subtract one sign bit. */
890 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
893 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
894 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
895 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
899 /* Both real or both integers; use the one with greater precision. */
901 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
903 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
906 /* Same precision. Prefer long longs to longs to ints when the
907 same precision, following the C99 rules on integer type rank
908 (which are equivalent to the C90 rules for C90 types). */
910 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
911 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
912 return long_long_unsigned_type_node
;
914 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
915 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
917 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
918 return long_long_unsigned_type_node
;
920 return long_long_integer_type_node
;
923 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
924 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
925 return long_unsigned_type_node
;
927 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
928 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
930 /* But preserve unsignedness from the other type,
931 since long cannot hold all the values of an unsigned int. */
932 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
933 return long_unsigned_type_node
;
935 return long_integer_type_node
;
938 /* For floating types of the same TYPE_PRECISION (which we here
939 assume means either the same set of values, or sets of values
940 neither a subset of the other, with behavior being undefined in
941 the latter case), follow the rules from TS 18661-3: prefer
942 interchange types _FloatN, then standard types long double,
943 double, float, then extended types _FloatNx. For extended types,
944 check them starting with _Float128x as that seems most consistent
945 in spirit with preferring long double to double; for interchange
946 types, also check in that order for consistency although it's not
947 possible for more than one of them to have the same
949 tree mv1
= TYPE_MAIN_VARIANT (t1
);
950 tree mv2
= TYPE_MAIN_VARIANT (t2
);
952 for (int i
= NUM_FLOATN_TYPES
- 1; i
>= 0; i
--)
953 if (mv1
== FLOATN_TYPE_NODE (i
) || mv2
== FLOATN_TYPE_NODE (i
))
954 return FLOATN_TYPE_NODE (i
);
956 /* Likewise, prefer long double to double even if same size. */
957 if (mv1
== long_double_type_node
|| mv2
== long_double_type_node
)
958 return long_double_type_node
;
960 /* Likewise, prefer double to float even if same size.
961 We got a couple of embedded targets with 32 bit doubles, and the
962 pdp11 might have 64 bit floats. */
963 if (mv1
== double_type_node
|| mv2
== double_type_node
)
964 return double_type_node
;
966 if (mv1
== float_type_node
|| mv2
== float_type_node
)
967 return float_type_node
;
969 for (int i
= NUM_FLOATNX_TYPES
- 1; i
>= 0; i
--)
970 if (mv1
== FLOATNX_TYPE_NODE (i
) || mv2
== FLOATNX_TYPE_NODE (i
))
971 return FLOATNX_TYPE_NODE (i
);
973 /* Otherwise prefer the unsigned one. */
975 if (TYPE_UNSIGNED (t1
))
981 /* Wrapper around c_common_type that is used by c-common.c and other
982 front end optimizations that remove promotions. ENUMERAL_TYPEs
983 are allowed here and are converted to their compatible integer types.
984 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
985 preferably a non-Boolean type as the common type. */
987 common_type (tree t1
, tree t2
)
989 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
990 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
991 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
992 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
994 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
995 if (TREE_CODE (t1
) == BOOLEAN_TYPE
996 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
997 return boolean_type_node
;
999 /* If either type is BOOLEAN_TYPE, then return the other. */
1000 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
1002 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
1005 return c_common_type (t1
, t2
);
1008 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1009 or various other operations. Return 2 if they are compatible
1010 but a warning may be needed if you use them together. */
1013 comptypes (tree type1
, tree type2
)
1015 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1018 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
1019 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1024 /* Like comptypes, but if it returns non-zero because enum and int are
1025 compatible, it sets *ENUM_AND_INT_P to true. */
1028 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1030 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1033 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1034 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1039 /* Like comptypes, but if it returns nonzero for different types, it
1040 sets *DIFFERENT_TYPES_P to true. */
1043 comptypes_check_different_types (tree type1
, tree type2
,
1044 bool *different_types_p
)
1046 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1049 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1050 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1055 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1056 or various other operations. Return 2 if they are compatible
1057 but a warning may be needed if you use them together. If
1058 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1059 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1060 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1061 NULL, and the types are compatible but different enough not to be
1062 permitted in C11 typedef redeclarations, then this sets
1063 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1064 false, but may or may not be set if the types are incompatible.
1065 This differs from comptypes, in that we don't free the seen
1069 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1070 bool *different_types_p
)
1072 const_tree t1
= type1
;
1073 const_tree t2
= type2
;
1076 /* Suppress errors caused by previously reported errors. */
1078 if (t1
== t2
|| !t1
|| !t2
1079 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1082 /* Enumerated types are compatible with integer types, but this is
1083 not transitive: two enumerated types in the same translation unit
1084 are compatible with each other only if they are the same type. */
1086 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1088 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1089 if (TREE_CODE (t2
) != VOID_TYPE
)
1091 if (enum_and_int_p
!= NULL
)
1092 *enum_and_int_p
= true;
1093 if (different_types_p
!= NULL
)
1094 *different_types_p
= true;
1097 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1099 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1100 if (TREE_CODE (t1
) != VOID_TYPE
)
1102 if (enum_and_int_p
!= NULL
)
1103 *enum_and_int_p
= true;
1104 if (different_types_p
!= NULL
)
1105 *different_types_p
= true;
1112 /* Different classes of types can't be compatible. */
1114 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1117 /* Qualifiers must match. C99 6.7.3p9 */
1119 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1122 /* Allow for two different type nodes which have essentially the same
1123 definition. Note that we already checked for equality of the type
1124 qualifiers (just above). */
1126 if (TREE_CODE (t1
) != ARRAY_TYPE
1127 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1130 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1131 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1134 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1137 switch (TREE_CODE (t1
))
1140 case FIXED_POINT_TYPE
:
1142 /* With these nodes, we can't determine type equivalence by
1143 looking at what is stored in the nodes themselves, because
1144 two nodes might have different TYPE_MAIN_VARIANTs but still
1145 represent the same type. For example, wchar_t and int could
1146 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1147 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1148 and are distinct types. On the other hand, int and the
1151 typedef int INT __attribute((may_alias));
1153 have identical properties, different TYPE_MAIN_VARIANTs, but
1154 represent the same type. The canonical type system keeps
1155 track of equivalence in this case, so we fall back on it. */
1156 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1159 /* Do not remove mode information. */
1160 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
1162 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1163 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1164 enum_and_int_p
, different_types_p
));
1168 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1174 tree d1
= TYPE_DOMAIN (t1
);
1175 tree d2
= TYPE_DOMAIN (t2
);
1176 bool d1_variable
, d2_variable
;
1177 bool d1_zero
, d2_zero
;
1180 /* Target types must match incl. qualifiers. */
1181 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1182 && (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1184 different_types_p
)) == 0)
1187 if (different_types_p
!= NULL
1188 && (d1
== NULL_TREE
) != (d2
== NULL_TREE
))
1189 *different_types_p
= true;
1190 /* Sizes must match unless one is missing or variable. */
1191 if (d1
== NULL_TREE
|| d2
== NULL_TREE
|| d1
== d2
)
1194 d1_zero
= !TYPE_MAX_VALUE (d1
);
1195 d2_zero
= !TYPE_MAX_VALUE (d2
);
1197 d1_variable
= (!d1_zero
1198 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1199 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1200 d2_variable
= (!d2_zero
1201 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1202 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1203 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1204 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1206 if (different_types_p
!= NULL
1207 && d1_variable
!= d2_variable
)
1208 *different_types_p
= true;
1209 if (d1_variable
|| d2_variable
)
1211 if (d1_zero
&& d2_zero
)
1213 if (d1_zero
|| d2_zero
1214 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1215 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1224 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1226 tree a1
= TYPE_ATTRIBUTES (t1
);
1227 tree a2
= TYPE_ATTRIBUTES (t2
);
1229 if (! attribute_list_contained (a1
, a2
)
1230 && ! attribute_list_contained (a2
, a1
))
1234 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1236 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1242 val
= (known_eq (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1243 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1244 enum_and_int_p
, different_types_p
));
1250 return attrval
== 2 && val
== 1 ? 2 : val
;
1253 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1254 their qualifiers, except for named address spaces. If the pointers point to
1255 different named addresses, then we must determine if one address space is a
1256 subset of the other. */
1259 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1263 tree mvl
= TREE_TYPE (ttl
);
1264 tree mvr
= TREE_TYPE (ttr
);
1265 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1266 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1267 addr_space_t as_common
;
1268 bool enum_and_int_p
;
1270 /* Fail if pointers point to incompatible address spaces. */
1271 if (!addr_space_superset (asl
, asr
, &as_common
))
1274 /* For pedantic record result of comptypes on arrays before losing
1275 qualifiers on the element type below. */
1278 if (TREE_CODE (mvl
) == ARRAY_TYPE
1279 && TREE_CODE (mvr
) == ARRAY_TYPE
)
1280 val_ped
= comptypes (mvl
, mvr
);
1282 /* Qualifiers on element types of array types that are
1283 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1285 mvl
= (TYPE_ATOMIC (strip_array_types (mvl
))
1286 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1287 : TYPE_MAIN_VARIANT (mvl
));
1289 mvr
= (TYPE_ATOMIC (strip_array_types (mvr
))
1290 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1291 : TYPE_MAIN_VARIANT (mvr
));
1293 enum_and_int_p
= false;
1294 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1296 if (val
== 1 && val_ped
!= 1)
1297 pedwarn (location
, OPT_Wpedantic
, "pointers to arrays with different qualifiers "
1298 "are incompatible in ISO C");
1301 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1303 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1304 warning_at (location
, OPT_Wc___compat
,
1305 "pointer target types incompatible in C++");
1310 /* Subroutines of `comptypes'. */
1312 /* Determine whether two trees derive from the same translation unit.
1313 If the CONTEXT chain ends in a null, that tree's context is still
1314 being parsed, so if two trees have context chains ending in null,
1315 they're in the same translation unit. */
1318 same_translation_unit_p (const_tree t1
, const_tree t2
)
1320 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1321 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1323 case tcc_declaration
:
1324 t1
= DECL_CONTEXT (t1
); break;
1326 t1
= TYPE_CONTEXT (t1
); break;
1327 case tcc_exceptional
:
1328 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1329 default: gcc_unreachable ();
1332 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1333 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1335 case tcc_declaration
:
1336 t2
= DECL_CONTEXT (t2
); break;
1338 t2
= TYPE_CONTEXT (t2
); break;
1339 case tcc_exceptional
:
1340 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1341 default: gcc_unreachable ();
1347 /* Allocate the seen two types, assuming that they are compatible. */
1349 static struct tagged_tu_seen_cache
*
1350 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1352 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1353 tu
->next
= tagged_tu_seen_base
;
1357 tagged_tu_seen_base
= tu
;
1359 /* The C standard says that two structures in different translation
1360 units are compatible with each other only if the types of their
1361 fields are compatible (among other things). We assume that they
1362 are compatible until proven otherwise when building the cache.
1363 An example where this can occur is:
1368 If we are comparing this against a similar struct in another TU,
1369 and did not assume they were compatible, we end up with an infinite
1375 /* Free the seen types until we get to TU_TIL. */
1378 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1380 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1381 while (tu
!= tu_til
)
1383 const struct tagged_tu_seen_cache
*const tu1
1384 = (const struct tagged_tu_seen_cache
*) tu
;
1386 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1388 tagged_tu_seen_base
= tu_til
;
1391 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1392 compatible. If the two types are not the same (which has been
1393 checked earlier), this can only happen when multiple translation
1394 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1395 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1396 comptypes_internal. */
1399 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1400 bool *enum_and_int_p
, bool *different_types_p
)
1403 bool needs_warning
= false;
1405 /* We have to verify that the tags of the types are the same. This
1406 is harder than it looks because this may be a typedef, so we have
1407 to go look at the original type. It may even be a typedef of a
1409 In the case of compiler-created builtin structs the TYPE_DECL
1410 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1411 while (TYPE_NAME (t1
)
1412 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1413 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1414 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1416 while (TYPE_NAME (t2
)
1417 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1418 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1419 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1421 /* C90 didn't have the requirement that the two tags be the same. */
1422 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1425 /* C90 didn't say what happened if one or both of the types were
1426 incomplete; we choose to follow C99 rules here, which is that they
1428 if (TYPE_SIZE (t1
) == NULL
1429 || TYPE_SIZE (t2
) == NULL
)
1433 const struct tagged_tu_seen_cache
* tts_i
;
1434 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1435 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1439 switch (TREE_CODE (t1
))
1443 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1444 /* Speed up the case where the type values are in the same order. */
1445 tree tv1
= TYPE_VALUES (t1
);
1446 tree tv2
= TYPE_VALUES (t2
);
1453 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1455 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1457 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1464 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1468 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1474 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1480 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1482 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1484 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1495 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1496 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1502 /* Speed up the common case where the fields are in the same order. */
1503 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1504 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1508 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1510 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1511 enum_and_int_p
, different_types_p
);
1513 if (result
!= 1 && !DECL_NAME (s1
))
1521 needs_warning
= true;
1523 if (TREE_CODE (s1
) == FIELD_DECL
1524 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1525 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1533 tu
->val
= needs_warning
? 2 : 1;
1537 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1541 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1542 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1546 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1550 if (result
!= 1 && !DECL_NAME (s1
))
1558 needs_warning
= true;
1560 if (TREE_CODE (s1
) == FIELD_DECL
1561 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1562 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1574 tu
->val
= needs_warning
? 2 : 10;
1580 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1582 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1584 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1587 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1588 || DECL_NAME (s1
) != DECL_NAME (s2
))
1590 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1591 enum_and_int_p
, different_types_p
);
1595 needs_warning
= true;
1597 if (TREE_CODE (s1
) == FIELD_DECL
1598 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1599 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1605 tu
->val
= needs_warning
? 2 : 1;
1614 /* Return 1 if two function types F1 and F2 are compatible.
1615 If either type specifies no argument types,
1616 the other must specify a fixed number of self-promoting arg types.
1617 Otherwise, if one type specifies only the number of arguments,
1618 the other must specify that number of self-promoting arg types.
1619 Otherwise, the argument types must match.
1620 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1623 function_types_compatible_p (const_tree f1
, const_tree f2
,
1624 bool *enum_and_int_p
, bool *different_types_p
)
1627 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1632 ret1
= TREE_TYPE (f1
);
1633 ret2
= TREE_TYPE (f2
);
1635 /* 'volatile' qualifiers on a function's return type used to mean
1636 the function is noreturn. */
1637 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1638 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1639 if (TYPE_VOLATILE (ret1
))
1640 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1641 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1642 if (TYPE_VOLATILE (ret2
))
1643 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1644 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1645 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1649 args1
= TYPE_ARG_TYPES (f1
);
1650 args2
= TYPE_ARG_TYPES (f2
);
1652 if (different_types_p
!= NULL
1653 && (args1
== NULL_TREE
) != (args2
== NULL_TREE
))
1654 *different_types_p
= true;
1656 /* An unspecified parmlist matches any specified parmlist
1657 whose argument types don't need default promotions. */
1659 if (args1
== NULL_TREE
)
1661 if (!self_promoting_args_p (args2
))
1663 /* If one of these types comes from a non-prototype fn definition,
1664 compare that with the other type's arglist.
1665 If they don't match, ask for a warning (but no error). */
1666 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1667 && type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1668 enum_and_int_p
, different_types_p
) != 1)
1672 if (args2
== NULL_TREE
)
1674 if (!self_promoting_args_p (args1
))
1676 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1677 && type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1678 enum_and_int_p
, different_types_p
) != 1)
1683 /* Both types have argument lists: compare them and propagate results. */
1684 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1686 return val1
!= 1 ? val1
: val
;
1689 /* Check two lists of types for compatibility, returning 0 for
1690 incompatible, 1 for compatible, or 2 for compatible with
1691 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1692 comptypes_internal. */
1695 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1696 bool *enum_and_int_p
, bool *different_types_p
)
1698 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1704 tree a1
, mv1
, a2
, mv2
;
1705 if (args1
== NULL_TREE
&& args2
== NULL_TREE
)
1707 /* If one list is shorter than the other,
1708 they fail to match. */
1709 if (args1
== NULL_TREE
|| args2
== NULL_TREE
)
1711 mv1
= a1
= TREE_VALUE (args1
);
1712 mv2
= a2
= TREE_VALUE (args2
);
1713 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1714 mv1
= (TYPE_ATOMIC (mv1
)
1715 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1717 : TYPE_MAIN_VARIANT (mv1
));
1718 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1719 mv2
= (TYPE_ATOMIC (mv2
)
1720 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1722 : TYPE_MAIN_VARIANT (mv2
));
1723 /* A null pointer instead of a type
1724 means there is supposed to be an argument
1725 but nothing is specified about what type it has.
1726 So match anything that self-promotes. */
1727 if (different_types_p
!= NULL
1728 && (a1
== NULL_TREE
) != (a2
== NULL_TREE
))
1729 *different_types_p
= true;
1730 if (a1
== NULL_TREE
)
1732 if (c_type_promotes_to (a2
) != a2
)
1735 else if (a2
== NULL_TREE
)
1737 if (c_type_promotes_to (a1
) != a1
)
1740 /* If one of the lists has an error marker, ignore this arg. */
1741 else if (TREE_CODE (a1
) == ERROR_MARK
1742 || TREE_CODE (a2
) == ERROR_MARK
)
1744 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1745 different_types_p
)))
1747 if (different_types_p
!= NULL
)
1748 *different_types_p
= true;
1749 /* Allow wait (union {union wait *u; int *i} *)
1750 and wait (union wait *) to be compatible. */
1751 if (TREE_CODE (a1
) == UNION_TYPE
1752 && (TYPE_NAME (a1
) == NULL_TREE
1753 || TYPE_TRANSPARENT_AGGR (a1
))
1754 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1755 && tree_int_cst_equal (TYPE_SIZE (a1
),
1759 for (memb
= TYPE_FIELDS (a1
);
1760 memb
; memb
= DECL_CHAIN (memb
))
1762 tree mv3
= TREE_TYPE (memb
);
1763 if (mv3
&& mv3
!= error_mark_node
1764 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1765 mv3
= (TYPE_ATOMIC (mv3
)
1766 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1768 : TYPE_MAIN_VARIANT (mv3
));
1769 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1773 if (memb
== NULL_TREE
)
1776 else if (TREE_CODE (a2
) == UNION_TYPE
1777 && (TYPE_NAME (a2
) == NULL_TREE
1778 || TYPE_TRANSPARENT_AGGR (a2
))
1779 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1780 && tree_int_cst_equal (TYPE_SIZE (a2
),
1784 for (memb
= TYPE_FIELDS (a2
);
1785 memb
; memb
= DECL_CHAIN (memb
))
1787 tree mv3
= TREE_TYPE (memb
);
1788 if (mv3
&& mv3
!= error_mark_node
1789 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1790 mv3
= (TYPE_ATOMIC (mv3
)
1791 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1793 : TYPE_MAIN_VARIANT (mv3
));
1794 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1798 if (memb
== NULL_TREE
)
1805 /* comptypes said ok, but record if it said to warn. */
1809 args1
= TREE_CHAIN (args1
);
1810 args2
= TREE_CHAIN (args2
);
1814 /* Compute the size to increment a pointer by. When a function type or void
1815 type or incomplete type is passed, size_one_node is returned.
1816 This function does not emit any diagnostics; the caller is responsible
1820 c_size_in_bytes (const_tree type
)
1822 enum tree_code code
= TREE_CODE (type
);
1824 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1825 || !COMPLETE_TYPE_P (type
))
1826 return size_one_node
;
1828 /* Convert in case a char is more than one unit. */
1829 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1830 size_int (TYPE_PRECISION (char_type_node
)
1834 /* Return either DECL or its known constant value (if it has one). */
1837 decl_constant_value_1 (tree decl
, bool in_init
)
1839 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1840 TREE_CODE (decl
) != PARM_DECL
1841 && !TREE_THIS_VOLATILE (decl
)
1842 && TREE_READONLY (decl
)
1843 && DECL_INITIAL (decl
) != NULL_TREE
1844 && !error_operand_p (DECL_INITIAL (decl
))
1845 /* This is invalid if initial value is not constant.
1846 If it has either a function call, a memory reference,
1847 or a variable, then re-evaluating it could give different results. */
1848 && TREE_CONSTANT (DECL_INITIAL (decl
))
1849 /* Check for cases where this is sub-optimal, even though valid. */
1850 && (in_init
|| TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
))
1851 return DECL_INITIAL (decl
);
1855 /* Return either DECL or its known constant value (if it has one).
1856 Like the above, but always return decl outside of functions. */
1859 decl_constant_value (tree decl
)
1861 /* Don't change a variable array bound or initial value to a constant
1862 in a place where a variable is invalid. */
1863 return current_function_decl
? decl_constant_value_1 (decl
, false) : decl
;
1866 /* Convert the array expression EXP to a pointer. */
1868 array_to_pointer_conversion (location_t loc
, tree exp
)
1870 tree orig_exp
= exp
;
1871 tree type
= TREE_TYPE (exp
);
1873 tree restype
= TREE_TYPE (type
);
1876 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1878 STRIP_TYPE_NOPS (exp
);
1880 if (TREE_NO_WARNING (orig_exp
))
1881 TREE_NO_WARNING (exp
) = 1;
1883 ptrtype
= build_pointer_type (restype
);
1885 if (INDIRECT_REF_P (exp
))
1886 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1888 /* In C++ array compound literals are temporary objects unless they are
1889 const or appear in namespace scope, so they are destroyed too soon
1890 to use them for much of anything (c++/53220). */
1891 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1893 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1894 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1895 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1896 "converting an array compound literal to a pointer "
1897 "is ill-formed in C++");
1900 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, true);
1901 return convert (ptrtype
, adr
);
1904 /* Convert the function expression EXP to a pointer. */
1906 function_to_pointer_conversion (location_t loc
, tree exp
)
1908 tree orig_exp
= exp
;
1910 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1912 STRIP_TYPE_NOPS (exp
);
1914 if (TREE_NO_WARNING (orig_exp
))
1915 TREE_NO_WARNING (exp
) = 1;
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 case C_MAYBE_CONST_EXPR
:
1944 mark_exp_read (TREE_OPERAND (exp
, 1));
1951 /* Perform the default conversion of arrays and functions to pointers.
1952 Return the result of converting EXP. For any other expression, just
1955 LOC is the location of the expression. */
1958 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1960 tree orig_exp
= exp
.value
;
1961 tree type
= TREE_TYPE (exp
.value
);
1962 enum tree_code code
= TREE_CODE (type
);
1968 bool not_lvalue
= false;
1969 bool lvalue_array_p
;
1971 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1972 || CONVERT_EXPR_P (exp
.value
))
1973 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1975 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1977 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1980 if (TREE_NO_WARNING (orig_exp
))
1981 TREE_NO_WARNING (exp
.value
) = 1;
1983 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1984 if (!flag_isoc99
&& !lvalue_array_p
)
1986 /* Before C99, non-lvalue arrays do not decay to pointers.
1987 Normally, using such an array would be invalid; but it can
1988 be used correctly inside sizeof or as a statement expression.
1989 Thus, do not give an error here; an error will result later. */
1993 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1997 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
2007 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
2009 mark_exp_read (exp
.value
);
2010 return default_function_array_conversion (loc
, exp
);
2013 /* Return whether EXPR should be treated as an atomic lvalue for the
2014 purposes of load and store handling. */
2017 really_atomic_lvalue (tree expr
)
2019 if (error_operand_p (expr
))
2021 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
2023 if (!lvalue_p (expr
))
2026 /* Ignore _Atomic on register variables, since their addresses can't
2027 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2028 sequences wouldn't work. Ignore _Atomic on structures containing
2029 bit-fields, since accessing elements of atomic structures or
2030 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2031 it's undefined at translation time or execution time, and the
2032 normal atomic sequences again wouldn't work. */
2033 while (handled_component_p (expr
))
2035 if (TREE_CODE (expr
) == COMPONENT_REF
2036 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
2038 expr
= TREE_OPERAND (expr
, 0);
2040 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
2045 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2046 including converting functions and arrays to pointers if CONVERT_P.
2047 If READ_P, also mark the expression as having been read. */
2050 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
2051 bool convert_p
, bool read_p
)
2054 mark_exp_read (exp
.value
);
2056 exp
= default_function_array_conversion (loc
, exp
);
2057 if (really_atomic_lvalue (exp
.value
))
2059 vec
<tree
, va_gc
> *params
;
2060 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2061 tree expr_type
= TREE_TYPE (exp
.value
);
2062 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, false);
2063 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2065 gcc_assert (TYPE_ATOMIC (expr_type
));
2067 /* Expansion of a generic atomic load may require an addition
2068 element, so allocate enough to prevent a resize. */
2069 vec_alloc (params
, 4);
2071 /* Remove the qualifiers for the rest of the expressions and
2072 create the VAL temp variable to hold the RHS. */
2073 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2074 tmp
= create_tmp_var_raw (nonatomic_type
);
2075 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, false);
2076 TREE_ADDRESSABLE (tmp
) = 1;
2077 TREE_NO_WARNING (tmp
) = 1;
2079 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2080 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2081 params
->quick_push (expr_addr
);
2082 params
->quick_push (tmp_addr
);
2083 params
->quick_push (seq_cst
);
2084 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2086 /* EXPR is always read. */
2087 mark_exp_read (exp
.value
);
2089 /* Return tmp which contains the value loaded. */
2090 exp
.value
= build4 (TARGET_EXPR
, nonatomic_type
, tmp
, func_call
,
2091 NULL_TREE
, NULL_TREE
);
2096 /* EXP is an expression of integer type. Apply the integer promotions
2097 to it and return the promoted value. */
2100 perform_integral_promotions (tree exp
)
2102 tree type
= TREE_TYPE (exp
);
2103 enum tree_code code
= TREE_CODE (type
);
2105 gcc_assert (INTEGRAL_TYPE_P (type
));
2107 /* Normally convert enums to int,
2108 but convert wide enums to something wider. */
2109 if (code
== ENUMERAL_TYPE
)
2111 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2112 TYPE_PRECISION (integer_type_node
)),
2113 ((TYPE_PRECISION (type
)
2114 >= TYPE_PRECISION (integer_type_node
))
2115 && TYPE_UNSIGNED (type
)));
2117 return convert (type
, exp
);
2120 /* ??? This should no longer be needed now bit-fields have their
2122 if (TREE_CODE (exp
) == COMPONENT_REF
2123 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2124 /* If it's thinner than an int, promote it like a
2125 c_promoting_integer_type_p, otherwise leave it alone. */
2126 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2127 TYPE_PRECISION (integer_type_node
)) < 0)
2128 return convert (integer_type_node
, exp
);
2130 if (c_promoting_integer_type_p (type
))
2132 /* Preserve unsignedness if not really getting any wider. */
2133 if (TYPE_UNSIGNED (type
)
2134 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2135 return convert (unsigned_type_node
, exp
);
2137 return convert (integer_type_node
, exp
);
2144 /* Perform default promotions for C data used in expressions.
2145 Enumeral types or short or char are converted to int.
2146 In addition, manifest constants symbols are replaced by their values. */
2149 default_conversion (tree exp
)
2152 tree type
= TREE_TYPE (exp
);
2153 enum tree_code code
= TREE_CODE (type
);
2156 mark_exp_read (exp
);
2158 /* Functions and arrays have been converted during parsing. */
2159 gcc_assert (code
!= FUNCTION_TYPE
);
2160 if (code
== ARRAY_TYPE
)
2163 /* Constants can be used directly unless they're not loadable. */
2164 if (TREE_CODE (exp
) == CONST_DECL
)
2165 exp
= DECL_INITIAL (exp
);
2167 /* Strip no-op conversions. */
2169 STRIP_TYPE_NOPS (exp
);
2171 if (TREE_NO_WARNING (orig_exp
))
2172 TREE_NO_WARNING (exp
) = 1;
2174 if (code
== VOID_TYPE
)
2176 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2177 "void value not ignored as it ought to be");
2178 return error_mark_node
;
2181 exp
= require_complete_type (EXPR_LOC_OR_LOC (exp
, input_location
), exp
);
2182 if (exp
== error_mark_node
)
2183 return error_mark_node
;
2185 promoted_type
= targetm
.promoted_type (type
);
2187 return convert (promoted_type
, exp
);
2189 if (INTEGRAL_TYPE_P (type
))
2190 return perform_integral_promotions (exp
);
2195 /* Look up COMPONENT in a structure or union TYPE.
2197 If the component name is not found, returns NULL_TREE. Otherwise,
2198 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2199 stepping down the chain to the component, which is in the last
2200 TREE_VALUE of the list. Normally the list is of length one, but if
2201 the component is embedded within (nested) anonymous structures or
2202 unions, the list steps down the chain to the component. */
2205 lookup_field (tree type
, tree component
)
2209 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2210 to the field elements. Use a binary search on this array to quickly
2211 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2212 will always be set for structures which have many elements.
2214 Duplicate field checking replaces duplicates with NULL_TREE so
2215 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2216 case just iterate using DECL_CHAIN. */
2218 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
2222 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2224 field
= TYPE_FIELDS (type
);
2226 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2227 while (top
- bot
> 1)
2229 half
= (top
- bot
+ 1) >> 1;
2230 field
= field_array
[bot
+half
];
2232 if (DECL_NAME (field
) == NULL_TREE
)
2234 /* Step through all anon unions in linear fashion. */
2235 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2237 field
= field_array
[bot
++];
2238 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2240 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2243 return tree_cons (NULL_TREE
, field
, anon
);
2245 /* The Plan 9 compiler permits referring
2246 directly to an anonymous struct/union field
2247 using a typedef name. */
2248 if (flag_plan9_extensions
2249 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2250 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2252 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2258 /* Entire record is only anon unions. */
2262 /* Restart the binary search, with new lower bound. */
2266 if (DECL_NAME (field
) == component
)
2268 if (DECL_NAME (field
) < component
)
2274 if (DECL_NAME (field_array
[bot
]) == component
)
2275 field
= field_array
[bot
];
2276 else if (DECL_NAME (field
) != component
)
2281 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2283 if (DECL_NAME (field
) == NULL_TREE
2284 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2286 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2289 return tree_cons (NULL_TREE
, field
, anon
);
2291 /* The Plan 9 compiler permits referring directly to an
2292 anonymous struct/union field using a typedef
2294 if (flag_plan9_extensions
2295 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2296 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2297 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2302 if (DECL_NAME (field
) == component
)
2306 if (field
== NULL_TREE
)
2310 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2313 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2316 lookup_field_fuzzy_find_candidates (tree type
, tree component
,
2317 vec
<tree
> *candidates
)
2320 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2322 if (DECL_NAME (field
) == NULL_TREE
2323 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2324 lookup_field_fuzzy_find_candidates (TREE_TYPE (field
), component
,
2327 if (DECL_NAME (field
))
2328 candidates
->safe_push (DECL_NAME (field
));
2332 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2333 rather than returning a TREE_LIST for an exact match. */
2336 lookup_field_fuzzy (tree type
, tree component
)
2338 gcc_assert (TREE_CODE (component
) == IDENTIFIER_NODE
);
2340 /* First, gather a list of candidates. */
2341 auto_vec
<tree
> candidates
;
2343 lookup_field_fuzzy_find_candidates (type
, component
,
2346 return find_closest_identifier (component
, &candidates
);
2349 /* Support function for build_component_ref's error-handling.
2351 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2352 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2355 should_suggest_deref_p (tree datum_type
)
2357 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2358 allows "." for ptrs; we could be handling a failed attempt
2359 to access a property. */
2360 if (c_dialect_objc ())
2363 /* Only suggest it for pointers... */
2364 if (TREE_CODE (datum_type
) != POINTER_TYPE
)
2367 /* ...to structs/unions. */
2368 tree underlying_type
= TREE_TYPE (datum_type
);
2369 enum tree_code code
= TREE_CODE (underlying_type
);
2370 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2376 /* Make an expression to refer to the COMPONENT field of structure or
2377 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2378 location of the COMPONENT_REF. COMPONENT_LOC is the location
2382 build_component_ref (location_t loc
, tree datum
, tree component
,
2383 location_t component_loc
)
2385 tree type
= TREE_TYPE (datum
);
2386 enum tree_code code
= TREE_CODE (type
);
2389 bool datum_lvalue
= lvalue_p (datum
);
2391 if (!objc_is_public (datum
, component
))
2392 return error_mark_node
;
2394 /* Detect Objective-C property syntax object.property. */
2395 if (c_dialect_objc ()
2396 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2399 /* See if there is a field or component with name COMPONENT. */
2401 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2403 if (!COMPLETE_TYPE_P (type
))
2405 c_incomplete_type_error (loc
, NULL_TREE
, type
);
2406 return error_mark_node
;
2409 field
= lookup_field (type
, component
);
2413 tree guessed_id
= lookup_field_fuzzy (type
, component
);
2416 /* Attempt to provide a fixit replacement hint, if
2417 we have a valid range for the component. */
2418 location_t reported_loc
2419 = (component_loc
!= UNKNOWN_LOCATION
) ? component_loc
: loc
;
2420 gcc_rich_location
rich_loc (reported_loc
);
2421 if (component_loc
!= UNKNOWN_LOCATION
)
2422 rich_loc
.add_fixit_misspelled_id (component_loc
, guessed_id
);
2423 error_at (&rich_loc
,
2424 "%qT has no member named %qE; did you mean %qE?",
2425 type
, component
, guessed_id
);
2428 error_at (loc
, "%qT has no member named %qE", type
, component
);
2429 return error_mark_node
;
2432 /* Accessing elements of atomic structures or unions is undefined
2433 behavior (C11 6.5.2.3#5). */
2434 if (TYPE_ATOMIC (type
) && c_inhibit_evaluation_warnings
== 0)
2436 if (code
== RECORD_TYPE
)
2437 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2438 "structure %qE", component
, datum
);
2440 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2441 "union %qE", component
, datum
);
2444 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2445 This might be better solved in future the way the C++ front
2446 end does it - by giving the anonymous entities each a
2447 separate name and type, and then have build_component_ref
2448 recursively call itself. We can't do that here. */
2451 tree subdatum
= TREE_VALUE (field
);
2454 bool use_datum_quals
;
2456 if (TREE_TYPE (subdatum
) == error_mark_node
)
2457 return error_mark_node
;
2459 /* If this is an rvalue, it does not have qualifiers in C
2460 standard terms and we must avoid propagating such
2461 qualifiers down to a non-lvalue array that is then
2462 converted to a pointer. */
2463 use_datum_quals
= (datum_lvalue
2464 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2466 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2467 if (use_datum_quals
)
2468 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2469 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2471 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2473 SET_EXPR_LOCATION (ref
, loc
);
2474 if (TREE_READONLY (subdatum
)
2475 || (use_datum_quals
&& TREE_READONLY (datum
)))
2476 TREE_READONLY (ref
) = 1;
2477 if (TREE_THIS_VOLATILE (subdatum
)
2478 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2479 TREE_THIS_VOLATILE (ref
) = 1;
2481 if (TREE_DEPRECATED (subdatum
))
2482 warn_deprecated_use (subdatum
, NULL_TREE
);
2486 field
= TREE_CHAIN (field
);
2492 else if (should_suggest_deref_p (type
))
2494 /* Special-case the error message for "ptr.field" for the case
2495 where the user has confused "." vs "->". */
2496 rich_location
richloc (line_table
, loc
);
2497 /* "loc" should be the "." token. */
2498 richloc
.add_fixit_replace ("->");
2500 "%qE is a pointer; did you mean to use %<->%>?",
2502 return error_mark_node
;
2504 else if (code
!= ERROR_MARK
)
2506 "request for member %qE in something not a structure or union",
2509 return error_mark_node
;
2512 /* Given an expression PTR for a pointer, return an expression
2513 for the value pointed to.
2514 ERRORSTRING is the name of the operator to appear in error messages.
2516 LOC is the location to use for the generated tree. */
2519 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2521 tree pointer
= default_conversion (ptr
);
2522 tree type
= TREE_TYPE (pointer
);
2525 if (TREE_CODE (type
) == POINTER_TYPE
)
2527 if (CONVERT_EXPR_P (pointer
)
2528 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2530 /* If a warning is issued, mark it to avoid duplicates from
2531 the backend. This only needs to be done at
2532 warn_strict_aliasing > 2. */
2533 if (warn_strict_aliasing
> 2)
2534 if (strict_aliasing_warning (EXPR_LOCATION (pointer
),
2535 type
, TREE_OPERAND (pointer
, 0)))
2536 TREE_NO_WARNING (pointer
) = 1;
2539 if (TREE_CODE (pointer
) == ADDR_EXPR
2540 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2541 == TREE_TYPE (type
)))
2543 ref
= TREE_OPERAND (pointer
, 0);
2544 protected_set_expr_location (ref
, loc
);
2549 tree t
= TREE_TYPE (type
);
2551 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2553 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2555 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)))
2557 error_at (loc
, "dereferencing pointer to incomplete type "
2559 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)) = 1;
2561 return error_mark_node
;
2563 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2564 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2566 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2567 so that we get the proper error message if the result is used
2568 to assign to. Also, &* is supposed to be a no-op.
2569 And ANSI C seems to specify that the type of the result
2570 should be the const type. */
2571 /* A de-reference of a pointer to const is not a const. It is valid
2572 to change it via some other pointer. */
2573 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2574 TREE_SIDE_EFFECTS (ref
)
2575 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2576 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2577 protected_set_expr_location (ref
, loc
);
2581 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2582 invalid_indirection_error (loc
, type
, errstring
);
2584 return error_mark_node
;
2587 /* This handles expressions of the form "a[i]", which denotes
2590 This is logically equivalent in C to *(a+i), but we may do it differently.
2591 If A is a variable or a member, we generate a primitive ARRAY_REF.
2592 This avoids forcing the array out of registers, and can work on
2593 arrays that are not lvalues (for example, members of structures returned
2596 For vector types, allow vector[i] but not i[vector], and create
2597 *(((type*)&vectortype) + i) for the expression.
2599 LOC is the location to use for the returned expression. */
2602 build_array_ref (location_t loc
, tree array
, tree index
)
2605 bool swapped
= false;
2606 if (TREE_TYPE (array
) == error_mark_node
2607 || TREE_TYPE (index
) == error_mark_node
)
2608 return error_mark_node
;
2610 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2611 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2612 /* Allow vector[index] but not index[vector]. */
2613 && !VECTOR_TYPE_P (TREE_TYPE (array
)))
2615 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2616 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2619 "subscripted value is neither array nor pointer nor vector");
2621 return error_mark_node
;
2623 std::swap (array
, index
);
2627 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2629 error_at (loc
, "array subscript is not an integer");
2630 return error_mark_node
;
2633 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2635 error_at (loc
, "subscripted value is pointer to function");
2636 return error_mark_node
;
2639 /* ??? Existing practice has been to warn only when the char
2640 index is syntactically the index, not for char[array]. */
2642 warn_array_subscript_with_type_char (loc
, index
);
2644 /* Apply default promotions *after* noticing character types. */
2645 index
= default_conversion (index
);
2646 if (index
== error_mark_node
)
2647 return error_mark_node
;
2649 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2651 bool was_vector
= VECTOR_TYPE_P (TREE_TYPE (array
));
2652 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, index
);
2654 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2658 /* An array that is indexed by a non-constant
2659 cannot be stored in a register; we must be able to do
2660 address arithmetic on its address.
2661 Likewise an array of elements of variable size. */
2662 if (TREE_CODE (index
) != INTEGER_CST
2663 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2664 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2666 if (!c_mark_addressable (array
, true))
2667 return error_mark_node
;
2669 /* An array that is indexed by a constant value which is not within
2670 the array bounds cannot be stored in a register either; because we
2671 would get a crash in store_bit_field/extract_bit_field when trying
2672 to access a non-existent part of the register. */
2673 if (TREE_CODE (index
) == INTEGER_CST
2674 && TYPE_DOMAIN (TREE_TYPE (array
))
2675 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2677 if (!c_mark_addressable (array
))
2678 return error_mark_node
;
2681 if ((pedantic
|| warn_c90_c99_compat
)
2685 while (TREE_CODE (foo
) == COMPONENT_REF
)
2686 foo
= TREE_OPERAND (foo
, 0);
2687 if (VAR_P (foo
) && C_DECL_REGISTER (foo
))
2688 pedwarn (loc
, OPT_Wpedantic
,
2689 "ISO C forbids subscripting %<register%> array");
2690 else if (!lvalue_p (foo
))
2691 pedwarn_c90 (loc
, OPT_Wpedantic
,
2692 "ISO C90 forbids subscripting non-lvalue "
2696 type
= TREE_TYPE (TREE_TYPE (array
));
2697 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2698 /* Array ref is const/volatile if the array elements are
2699 or if the array is. */
2700 TREE_READONLY (rval
)
2701 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2702 | TREE_READONLY (array
));
2703 TREE_SIDE_EFFECTS (rval
)
2704 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2705 | TREE_SIDE_EFFECTS (array
));
2706 TREE_THIS_VOLATILE (rval
)
2707 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2708 /* This was added by rms on 16 Nov 91.
2709 It fixes vol struct foo *a; a->elts[1]
2710 in an inline function.
2711 Hope it doesn't break something else. */
2712 | TREE_THIS_VOLATILE (array
));
2713 ret
= require_complete_type (loc
, rval
);
2714 protected_set_expr_location (ret
, loc
);
2716 ret
= non_lvalue_loc (loc
, ret
);
2721 tree ar
= default_conversion (array
);
2723 if (ar
== error_mark_node
)
2726 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2727 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2729 ret
= build_indirect_ref (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
,
2733 ret
= non_lvalue_loc (loc
, ret
);
2738 /* Build an external reference to identifier ID. FUN indicates
2739 whether this will be used for a function call. LOC is the source
2740 location of the identifier. This sets *TYPE to the type of the
2741 identifier, which is not the same as the type of the returned value
2742 for CONST_DECLs defined as enum constants. If the type of the
2743 identifier is not available, *TYPE is set to NULL. */
2745 build_external_ref (location_t loc
, tree id
, bool fun
, tree
*type
)
2748 tree decl
= lookup_name (id
);
2750 /* In Objective-C, an instance variable (ivar) may be preferred to
2751 whatever lookup_name() found. */
2752 decl
= objc_lookup_ivar (decl
, id
);
2755 if (decl
&& decl
!= error_mark_node
)
2758 *type
= TREE_TYPE (ref
);
2761 /* Implicit function declaration. */
2762 ref
= implicitly_declare (loc
, id
);
2763 else if (decl
== error_mark_node
)
2764 /* Don't complain about something that's already been
2765 complained about. */
2766 return error_mark_node
;
2769 undeclared_variable (loc
, id
);
2770 return error_mark_node
;
2773 if (TREE_TYPE (ref
) == error_mark_node
)
2774 return error_mark_node
;
2776 if (TREE_DEPRECATED (ref
))
2777 warn_deprecated_use (ref
, NULL_TREE
);
2779 /* Recursive call does not count as usage. */
2780 if (ref
!= current_function_decl
)
2782 TREE_USED (ref
) = 1;
2785 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2787 if (!in_sizeof
&& !in_typeof
)
2788 C_DECL_USED (ref
) = 1;
2789 else if (DECL_INITIAL (ref
) == NULL_TREE
2790 && DECL_EXTERNAL (ref
)
2791 && !TREE_PUBLIC (ref
))
2792 record_maybe_used_decl (ref
);
2795 if (TREE_CODE (ref
) == CONST_DECL
)
2797 used_types_insert (TREE_TYPE (ref
));
2800 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2801 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2803 warning_at (loc
, OPT_Wc___compat
,
2804 ("enum constant defined in struct or union "
2805 "is not visible in C++"));
2806 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2809 ref
= DECL_INITIAL (ref
);
2810 TREE_CONSTANT (ref
) = 1;
2812 else if (current_function_decl
!= NULL_TREE
2813 && !DECL_FILE_SCOPE_P (current_function_decl
)
2814 && (VAR_OR_FUNCTION_DECL_P (ref
)
2815 || TREE_CODE (ref
) == PARM_DECL
))
2817 tree context
= decl_function_context (ref
);
2819 if (context
!= NULL_TREE
&& context
!= current_function_decl
)
2820 DECL_NONLOCAL (ref
) = 1;
2822 /* C99 6.7.4p3: An inline definition of a function with external
2823 linkage ... shall not contain a reference to an identifier with
2824 internal linkage. */
2825 else if (current_function_decl
!= NULL_TREE
2826 && DECL_DECLARED_INLINE_P (current_function_decl
)
2827 && DECL_EXTERNAL (current_function_decl
)
2828 && VAR_OR_FUNCTION_DECL_P (ref
)
2829 && (!VAR_P (ref
) || TREE_STATIC (ref
))
2830 && ! TREE_PUBLIC (ref
)
2831 && DECL_CONTEXT (ref
) != current_function_decl
)
2832 record_inline_static (loc
, current_function_decl
, ref
,
2838 /* Record details of decls possibly used inside sizeof or typeof. */
2839 struct maybe_used_decl
2843 /* The level seen at (in_sizeof + in_typeof). */
2845 /* The next one at this level or above, or NULL. */
2846 struct maybe_used_decl
*next
;
2849 static struct maybe_used_decl
*maybe_used_decls
;
2851 /* Record that DECL, an undefined static function reference seen
2852 inside sizeof or typeof, might be used if the operand of sizeof is
2853 a VLA type or the operand of typeof is a variably modified
2857 record_maybe_used_decl (tree decl
)
2859 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2861 t
->level
= in_sizeof
+ in_typeof
;
2862 t
->next
= maybe_used_decls
;
2863 maybe_used_decls
= t
;
2866 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2867 USED is false, just discard them. If it is true, mark them used
2868 (if no longer inside sizeof or typeof) or move them to the next
2869 level up (if still inside sizeof or typeof). */
2872 pop_maybe_used (bool used
)
2874 struct maybe_used_decl
*p
= maybe_used_decls
;
2875 int cur_level
= in_sizeof
+ in_typeof
;
2876 while (p
&& p
->level
> cur_level
)
2881 C_DECL_USED (p
->decl
) = 1;
2883 p
->level
= cur_level
;
2887 if (!used
|| cur_level
== 0)
2888 maybe_used_decls
= p
;
2891 /* Return the result of sizeof applied to EXPR. */
2894 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2897 if (expr
.value
== error_mark_node
)
2899 ret
.value
= error_mark_node
;
2900 ret
.original_code
= ERROR_MARK
;
2901 ret
.original_type
= NULL
;
2902 pop_maybe_used (false);
2906 bool expr_const_operands
= true;
2908 if (TREE_CODE (expr
.value
) == PARM_DECL
2909 && C_ARRAY_PARAMETER (expr
.value
))
2911 auto_diagnostic_group d
;
2912 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2913 "%<sizeof%> on array function parameter %qE will "
2914 "return size of %qT", expr
.value
,
2915 TREE_TYPE (expr
.value
)))
2916 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2918 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2919 &expr_const_operands
);
2920 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2921 c_last_sizeof_arg
= expr
.value
;
2922 c_last_sizeof_loc
= loc
;
2923 ret
.original_code
= SIZEOF_EXPR
;
2924 ret
.original_type
= NULL
;
2925 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2927 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2928 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2929 folded_expr
, ret
.value
);
2930 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2931 SET_EXPR_LOCATION (ret
.value
, loc
);
2933 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2938 /* Return the result of sizeof applied to T, a structure for the type
2939 name passed to sizeof (rather than the type itself). LOC is the
2940 location of the original expression. */
2943 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2947 tree type_expr
= NULL_TREE
;
2948 bool type_expr_const
= true;
2949 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2950 ret
.value
= c_sizeof (loc
, type
);
2951 c_last_sizeof_arg
= type
;
2952 c_last_sizeof_loc
= loc
;
2953 ret
.original_code
= SIZEOF_EXPR
;
2954 ret
.original_type
= NULL
;
2955 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2956 && c_vla_type_p (type
))
2958 /* If the type is a [*] array, it is a VLA but is represented as
2959 having a size of zero. In such a case we must ensure that
2960 the result of sizeof does not get folded to a constant by
2961 c_fully_fold, because if the size is evaluated the result is
2962 not constant and so constraints on zero or negative size
2963 arrays must not be applied when this sizeof call is inside
2964 another array declarator. */
2966 type_expr
= integer_zero_node
;
2967 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2968 type_expr
, ret
.value
);
2969 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2971 pop_maybe_used (type
!= error_mark_node
2972 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2976 /* Build a function call to function FUNCTION with parameters PARAMS.
2977 The function call is at LOC.
2978 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2979 TREE_VALUE of each node is a parameter-expression.
2980 FUNCTION's data type may be a function type or a pointer-to-function. */
2983 build_function_call (location_t loc
, tree function
, tree params
)
2985 vec
<tree
, va_gc
> *v
;
2988 vec_alloc (v
, list_length (params
));
2989 for (; params
; params
= TREE_CHAIN (params
))
2990 v
->quick_push (TREE_VALUE (params
));
2991 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2996 /* Give a note about the location of the declaration of DECL. */
2999 inform_declaration (tree decl
)
3001 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_IS_BUILTIN (decl
)))
3002 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
3005 /* Build a function call to function FUNCTION with parameters PARAMS.
3006 If FUNCTION is the result of resolving an overloaded target built-in,
3007 ORIG_FUNDECL is the original function decl, otherwise it is null.
3008 ORIGTYPES, if not NULL, is a vector of types; each element is
3009 either NULL or the original type of the corresponding element in
3010 PARAMS. The original type may differ from TREE_TYPE of the
3011 parameter for enums. FUNCTION's data type may be a function type
3012 or pointer-to-function. This function changes the elements of
3016 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3017 tree function
, vec
<tree
, va_gc
> *params
,
3018 vec
<tree
, va_gc
> *origtypes
, tree orig_fundecl
)
3020 tree fntype
, fundecl
= NULL_TREE
;
3021 tree name
= NULL_TREE
, result
;
3027 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3028 STRIP_TYPE_NOPS (function
);
3030 /* Convert anything with function type to a pointer-to-function. */
3031 if (TREE_CODE (function
) == FUNCTION_DECL
)
3033 name
= DECL_NAME (function
);
3036 tm_malloc_replacement (function
);
3039 orig_fundecl
= fundecl
;
3040 /* Atomic functions have type checking/casting already done. They are
3041 often rewritten and don't match the original parameter list. */
3042 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
3045 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
3046 function
= function_to_pointer_conversion (loc
, function
);
3048 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3049 expressions, like those used for ObjC messenger dispatches. */
3050 if (params
&& !params
->is_empty ())
3051 function
= objc_rewrite_function_call (function
, (*params
)[0]);
3053 function
= c_fully_fold (function
, false, NULL
);
3055 fntype
= TREE_TYPE (function
);
3057 if (TREE_CODE (fntype
) == ERROR_MARK
)
3058 return error_mark_node
;
3060 if (!(TREE_CODE (fntype
) == POINTER_TYPE
3061 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
3063 if (!flag_diagnostics_show_caret
)
3065 "called object %qE is not a function or function pointer",
3067 else if (DECL_P (function
))
3070 "called object %qD is not a function or function pointer",
3072 inform_declaration (function
);
3076 "called object is not a function or function pointer");
3077 return error_mark_node
;
3080 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
3081 current_function_returns_abnormally
= 1;
3083 /* fntype now gets the type of function pointed to. */
3084 fntype
= TREE_TYPE (fntype
);
3086 /* Convert the parameters to the types declared in the
3087 function prototype, or apply default promotions. */
3089 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
3090 origtypes
, function
, fundecl
);
3092 return error_mark_node
;
3094 /* Check that the function is called through a compatible prototype.
3095 If it is not, warn. */
3096 if (CONVERT_EXPR_P (function
)
3097 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
3098 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
3099 && !comptypes (fntype
, TREE_TYPE (tem
)))
3101 tree return_type
= TREE_TYPE (fntype
);
3103 /* This situation leads to run-time undefined behavior. We can't,
3104 therefore, simply error unless we can prove that all possible
3105 executions of the program must execute the code. */
3106 warning_at (loc
, 0, "function called through a non-compatible type");
3108 if (VOID_TYPE_P (return_type
)
3109 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
3111 "function with qualified void return type called");
3114 argarray
= vec_safe_address (params
);
3116 /* Check that arguments to builtin functions match the expectations. */
3118 && fndecl_built_in_p (fundecl
)
3119 && !check_builtin_function_arguments (loc
, arg_loc
, fundecl
,
3120 orig_fundecl
, nargs
, argarray
))
3121 return error_mark_node
;
3123 /* Check that the arguments to the function are valid. */
3124 bool warned_p
= check_function_arguments (loc
, fundecl
, fntype
,
3125 nargs
, argarray
, &arg_loc
);
3127 if (name
!= NULL_TREE
3128 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
3130 if (require_constant_value
)
3132 = fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
3133 function
, nargs
, argarray
);
3135 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
3136 function
, nargs
, argarray
);
3137 if (TREE_CODE (result
) == NOP_EXPR
3138 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
3139 STRIP_TYPE_NOPS (result
);
3142 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
3143 function
, nargs
, argarray
);
3144 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3146 if (warned_p
&& TREE_CODE (result
) == CALL_EXPR
)
3147 TREE_NO_WARNING (result
) = 1;
3149 /* In this improbable scenario, a nested function returns a VM type.
3150 Create a TARGET_EXPR so that the call always has a LHS, much as
3151 what the C++ FE does for functions returning non-PODs. */
3152 if (variably_modified_type_p (TREE_TYPE (fntype
), NULL_TREE
))
3154 tree tmp
= create_tmp_var_raw (TREE_TYPE (fntype
));
3155 result
= build4 (TARGET_EXPR
, TREE_TYPE (fntype
), tmp
, result
,
3156 NULL_TREE
, NULL_TREE
);
3159 if (VOID_TYPE_P (TREE_TYPE (result
)))
3161 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
3163 "function with qualified void return type called");
3166 return require_complete_type (loc
, result
);
3169 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3172 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3173 tree function
, vec
<tree
, va_gc
> *params
,
3174 vec
<tree
, va_gc
> *origtypes
)
3176 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3177 STRIP_TYPE_NOPS (function
);
3179 /* Convert anything with function type to a pointer-to-function. */
3180 if (TREE_CODE (function
) == FUNCTION_DECL
)
3182 /* Implement type-directed function overloading for builtins.
3183 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3184 handle all the type checking. The result is a complete expression
3185 that implements this function call. */
3186 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3190 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3193 /* Helper for convert_arguments called to convert the VALue of argument
3194 number ARGNUM from ORIGTYPE to the corresponding parameter number
3196 PLOC is the location where the conversion is being performed.
3197 FUNCTION and FUNDECL are the same as in convert_arguments.
3198 VALTYPE is the original type of VAL before the conversion and,
3199 for EXCESS_PRECISION_EXPR, the operand of the expression.
3200 NPC is true if VAL represents the null pointer constant (VAL itself
3201 will have been folded to an integer constant).
3202 RNAME is the same as FUNCTION except in Objective C when it's
3203 the function selector.
3204 EXCESS_PRECISION is true when VAL was originally represented
3205 as EXCESS_PRECISION_EXPR.
3206 WARNOPT is the same as in convert_for_assignment. */
3209 convert_argument (location_t ploc
, tree function
, tree fundecl
,
3210 tree type
, tree origtype
, tree val
, tree valtype
,
3211 bool npc
, tree rname
, int parmnum
, int argnum
,
3212 bool excess_precision
, int warnopt
)
3214 /* Formal parm type is specified by a function prototype. */
3216 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3218 error_at (ploc
, "type of formal parameter %d is incomplete",
3223 /* Optionally warn about conversions that differ from the default
3225 if (warn_traditional_conversion
|| warn_traditional
)
3227 unsigned int formal_prec
= TYPE_PRECISION (type
);
3229 if (INTEGRAL_TYPE_P (type
)
3230 && TREE_CODE (valtype
) == REAL_TYPE
)
3231 warning_at (ploc
, OPT_Wtraditional_conversion
,
3232 "passing argument %d of %qE as integer rather "
3233 "than floating due to prototype",
3235 if (INTEGRAL_TYPE_P (type
)
3236 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3237 warning_at (ploc
, OPT_Wtraditional_conversion
,
3238 "passing argument %d of %qE as integer rather "
3239 "than complex due to prototype",
3241 else if (TREE_CODE (type
) == COMPLEX_TYPE
3242 && TREE_CODE (valtype
) == REAL_TYPE
)
3243 warning_at (ploc
, OPT_Wtraditional_conversion
,
3244 "passing argument %d of %qE as complex rather "
3245 "than floating due to prototype",
3247 else if (TREE_CODE (type
) == REAL_TYPE
3248 && INTEGRAL_TYPE_P (valtype
))
3249 warning_at (ploc
, OPT_Wtraditional_conversion
,
3250 "passing argument %d of %qE as floating rather "
3251 "than integer due to prototype",
3253 else if (TREE_CODE (type
) == COMPLEX_TYPE
3254 && INTEGRAL_TYPE_P (valtype
))
3255 warning_at (ploc
, OPT_Wtraditional_conversion
,
3256 "passing argument %d of %qE as complex rather "
3257 "than integer due to prototype",
3259 else if (TREE_CODE (type
) == REAL_TYPE
3260 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3261 warning_at (ploc
, OPT_Wtraditional_conversion
,
3262 "passing argument %d of %qE as floating rather "
3263 "than complex due to prototype",
3265 /* ??? At some point, messages should be written about
3266 conversions between complex types, but that's too messy
3268 else if (TREE_CODE (type
) == REAL_TYPE
3269 && TREE_CODE (valtype
) == REAL_TYPE
)
3271 /* Warn if any argument is passed as `float',
3272 since without a prototype it would be `double'. */
3273 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3274 && type
!= dfloat32_type_node
)
3275 warning_at (ploc
, 0,
3276 "passing argument %d of %qE as %<float%> "
3277 "rather than %<double%> due to prototype",
3280 /* Warn if mismatch between argument and prototype
3281 for decimal float types. Warn of conversions with
3282 binary float types and of precision narrowing due to
3284 else if (type
!= valtype
3285 && (type
== dfloat32_type_node
3286 || type
== dfloat64_type_node
3287 || type
== dfloat128_type_node
3288 || valtype
== dfloat32_type_node
3289 || valtype
== dfloat64_type_node
3290 || valtype
== dfloat128_type_node
)
3292 <= TYPE_PRECISION (valtype
)
3293 || (type
== dfloat128_type_node
3295 != dfloat64_type_node
3297 != dfloat32_type_node
)))
3298 || (type
== dfloat64_type_node
3300 != dfloat32_type_node
))))
3301 warning_at (ploc
, 0,
3302 "passing argument %d of %qE as %qT "
3303 "rather than %qT due to prototype",
3304 argnum
, rname
, type
, valtype
);
3307 /* Detect integer changing in width or signedness.
3308 These warnings are only activated with
3309 -Wtraditional-conversion, not with -Wtraditional. */
3310 else if (warn_traditional_conversion
3311 && INTEGRAL_TYPE_P (type
)
3312 && INTEGRAL_TYPE_P (valtype
))
3314 tree would_have_been
= default_conversion (val
);
3315 tree type1
= TREE_TYPE (would_have_been
);
3317 if (val
== error_mark_node
)
3318 /* VAL could have been of incomplete type. */;
3319 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3320 && (TYPE_MAIN_VARIANT (type
)
3321 == TYPE_MAIN_VARIANT (valtype
)))
3322 /* No warning if function asks for enum
3323 and the actual arg is that enum type. */
3325 else if (formal_prec
!= TYPE_PRECISION (type1
))
3326 warning_at (ploc
, OPT_Wtraditional_conversion
,
3327 "passing argument %d of %qE "
3328 "with different width due to prototype",
3330 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3332 /* Don't complain if the formal parameter type
3333 is an enum, because we can't tell now whether
3334 the value was an enum--even the same enum. */
3335 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3337 else if (TREE_CODE (val
) == INTEGER_CST
3338 && int_fits_type_p (val
, type
))
3339 /* Change in signedness doesn't matter
3340 if a constant value is unaffected. */
3342 /* If the value is extended from a narrower
3343 unsigned type, it doesn't matter whether we
3344 pass it as signed or unsigned; the value
3345 certainly is the same either way. */
3346 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3347 && TYPE_UNSIGNED (valtype
))
3349 else if (TYPE_UNSIGNED (type
))
3350 warning_at (ploc
, OPT_Wtraditional_conversion
,
3351 "passing argument %d of %qE "
3352 "as unsigned due to prototype",
3355 warning_at (ploc
, OPT_Wtraditional_conversion
,
3356 "passing argument %d of %qE "
3357 "as signed due to prototype",
3362 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3363 sake of better warnings from convert_and_check. */
3364 if (excess_precision
)
3365 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3367 tree parmval
= convert_for_assignment (ploc
, ploc
, type
,
3368 val
, origtype
, ic_argpass
,
3369 npc
, fundecl
, function
,
3370 parmnum
+ 1, warnopt
);
3372 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3373 && INTEGRAL_TYPE_P (type
)
3374 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3375 parmval
= default_conversion (parmval
);
3380 /* Convert the argument expressions in the vector VALUES
3381 to the types in the list TYPELIST.
3383 If TYPELIST is exhausted, or when an element has NULL as its type,
3384 perform the default conversions.
3386 ORIGTYPES is the original types of the expressions in VALUES. This
3387 holds the type of enum values which have been converted to integral
3388 types. It may be NULL.
3390 FUNCTION is a tree for the called function. It is used only for
3391 error messages, where it is formatted with %qE.
3393 This is also where warnings about wrong number of args are generated.
3395 ARG_LOC are locations of function arguments (if any).
3397 Returns the actual number of arguments processed (which may be less
3398 than the length of VALUES in some error situations), or -1 on
3402 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3403 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3404 tree function
, tree fundecl
)
3406 unsigned int parmnum
;
3407 bool error_args
= false;
3408 const bool type_generic
= fundecl
3409 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3410 bool type_generic_remove_excess_precision
= false;
3411 bool type_generic_overflow_p
= false;
3414 /* Change pointer to function to the function itself for
3416 if (TREE_CODE (function
) == ADDR_EXPR
3417 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3418 function
= TREE_OPERAND (function
, 0);
3420 /* Handle an ObjC selector specially for diagnostics. */
3421 selector
= objc_message_selector ();
3423 /* For a call to a built-in function declared without a prototype,
3424 set to the built-in function's argument list. */
3425 tree builtin_typelist
= NULL_TREE
;
3427 /* For type-generic built-in functions, determine whether excess
3428 precision should be removed (classification) or not
3431 && fndecl_built_in_p (fundecl
, BUILT_IN_NORMAL
))
3433 built_in_function code
= DECL_FUNCTION_CODE (fundecl
);
3434 if (C_DECL_BUILTIN_PROTOTYPE (fundecl
))
3436 /* For a call to a built-in function declared without a prototype
3437 use the types of the parameters of the internal built-in to
3438 match those of the arguments to. */
3439 if (tree bdecl
= builtin_decl_explicit (code
))
3440 builtin_typelist
= TYPE_ARG_TYPES (TREE_TYPE (bdecl
));
3443 /* For type-generic built-in functions, determine whether excess
3444 precision should be removed (classification) or not
3449 case BUILT_IN_ISFINITE
:
3450 case BUILT_IN_ISINF
:
3451 case BUILT_IN_ISINF_SIGN
:
3452 case BUILT_IN_ISNAN
:
3453 case BUILT_IN_ISNORMAL
:
3454 case BUILT_IN_FPCLASSIFY
:
3455 type_generic_remove_excess_precision
= true;
3458 case BUILT_IN_ADD_OVERFLOW_P
:
3459 case BUILT_IN_SUB_OVERFLOW_P
:
3460 case BUILT_IN_MUL_OVERFLOW_P
:
3461 /* The last argument of these type-generic builtins
3462 should not be promoted. */
3463 type_generic_overflow_p
= true;
3471 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3472 individual converted arguments. */
3474 tree typetail
, builtin_typetail
, val
;
3475 for (typetail
= typelist
,
3476 builtin_typetail
= builtin_typelist
,
3478 values
&& values
->iterate (parmnum
, &val
);
3481 /* The type of the function parameter (if it was declared with one). */
3482 tree type
= typetail
? TREE_VALUE (typetail
) : NULL_TREE
;
3483 /* The type of the built-in function parameter (if the function
3484 is a built-in). Used to detect type incompatibilities in
3485 calls to built-ins declared without a prototype. */
3486 tree builtin_type
= (builtin_typetail
3487 ? TREE_VALUE (builtin_typetail
) : NULL_TREE
);
3488 /* The original type of the argument being passed to the function. */
3489 tree valtype
= TREE_TYPE (val
);
3490 /* The called function (or function selector in Objective C). */
3491 tree rname
= function
;
3492 int argnum
= parmnum
+ 1;
3493 const char *invalid_func_diag
;
3494 /* Set for EXCESS_PRECISION_EXPR arguments. */
3495 bool excess_precision
= false;
3496 /* The value of the argument after conversion to the type
3497 of the function parameter it is passed to. */
3499 /* Some __atomic_* builtins have additional hidden argument at
3502 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3503 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3506 if (type
== void_type_node
)
3509 error_at (loc
, "too many arguments to method %qE", selector
);
3511 error_at (loc
, "too many arguments to function %qE", function
);
3512 inform_declaration (fundecl
);
3513 return error_args
? -1 : (int) parmnum
;
3516 if (builtin_type
== void_type_node
)
3518 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
3519 "too many arguments to built-in function %qE "
3520 "expecting %d", function
, parmnum
))
3521 inform_declaration (fundecl
);
3522 builtin_typetail
= NULL_TREE
;
3525 if (selector
&& argnum
> 2)
3531 /* Determine if VAL is a null pointer constant before folding it. */
3532 bool npc
= null_pointer_constant_p (val
);
3534 /* If there is excess precision and a prototype, convert once to
3535 the required type rather than converting via the semantic
3536 type. Likewise without a prototype a float value represented
3537 as long double should be converted once to double. But for
3538 type-generic classification functions excess precision must
3540 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3541 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3543 val
= TREE_OPERAND (val
, 0);
3544 excess_precision
= true;
3546 val
= c_fully_fold (val
, false, NULL
);
3547 STRIP_TYPE_NOPS (val
);
3549 val
= require_complete_type (ploc
, val
);
3551 /* Some floating-point arguments must be promoted to double when
3552 no type is specified by a prototype. This applies to
3553 arguments of type float, and to architecture-specific types
3554 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3555 bool promote_float_arg
= false;
3556 if (type
== NULL_TREE
3557 && TREE_CODE (valtype
) == REAL_TYPE
3558 && (TYPE_PRECISION (valtype
)
3559 <= TYPE_PRECISION (double_type_node
))
3560 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3561 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3562 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3564 /* Promote this argument, unless it has a _FloatN or
3566 promote_float_arg
= true;
3567 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
3568 if (TYPE_MAIN_VARIANT (valtype
) == FLOATN_NX_TYPE_NODE (i
))
3570 promote_float_arg
= false;
3575 if (type
!= NULL_TREE
)
3577 tree origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3578 parmval
= convert_argument (ploc
, function
, fundecl
, type
, origtype
,
3579 val
, valtype
, npc
, rname
, parmnum
, argnum
,
3580 excess_precision
, 0);
3582 else if (promote_float_arg
)
3588 /* Convert `float' to `double'. */
3589 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3590 warning_at (ploc
, OPT_Wdouble_promotion
,
3591 "implicit conversion from %qT to %qT when passing "
3592 "argument to function",
3593 valtype
, double_type_node
);
3594 parmval
= convert (double_type_node
, val
);
3597 else if ((excess_precision
&& !type_generic
)
3598 || (type_generic_overflow_p
&& parmnum
== 2))
3599 /* A "double" argument with excess precision being passed
3600 without a prototype or in variable arguments.
3601 The last argument of __builtin_*_overflow_p should not be
3603 parmval
= convert (valtype
, val
);
3604 else if ((invalid_func_diag
=
3605 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3607 error (invalid_func_diag
);
3610 else if (TREE_CODE (val
) == ADDR_EXPR
&& reject_gcc_builtin (val
))
3615 /* Convert `short' and `char' to full-size `int'. */
3616 parmval
= default_conversion (val
);
3618 (*values
)[parmnum
] = parmval
;
3619 if (parmval
== error_mark_node
)
3622 if (!type
&& builtin_type
&& TREE_CODE (builtin_type
) != VOID_TYPE
)
3624 /* For a call to a built-in function declared without a prototype,
3625 perform the conversions from the argument to the expected type
3626 but issue warnings rather than errors for any mismatches.
3627 Ignore the converted argument and use the PARMVAL obtained
3628 above by applying default conversions instead. */
3629 tree origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3630 convert_argument (ploc
, function
, fundecl
, builtin_type
, origtype
,
3631 val
, valtype
, npc
, rname
, parmnum
, argnum
,
3633 OPT_Wbuiltin_declaration_mismatch
);
3637 typetail
= TREE_CHAIN (typetail
);
3639 if (builtin_typetail
)
3640 builtin_typetail
= TREE_CHAIN (builtin_typetail
);
3643 gcc_assert (parmnum
== vec_safe_length (values
));
3645 if (typetail
!= NULL_TREE
&& TREE_VALUE (typetail
) != void_type_node
)
3647 error_at (loc
, "too few arguments to function %qE", function
);
3648 inform_declaration (fundecl
);
3652 if (builtin_typetail
&& TREE_VALUE (builtin_typetail
) != void_type_node
)
3654 unsigned nargs
= parmnum
;
3655 for (tree t
= builtin_typetail
; t
; t
= TREE_CHAIN (t
))
3658 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
3659 "too few arguments to built-in function %qE "
3660 "expecting %u", function
, nargs
- 1))
3661 inform_declaration (fundecl
);
3664 return error_args
? -1 : (int) parmnum
;
3667 /* This is the entry point used by the parser to build unary operators
3668 in the input. CODE, a tree_code, specifies the unary operator, and
3669 ARG is the operand. For unary plus, the C parser currently uses
3670 CONVERT_EXPR for code.
3672 LOC is the location to use for the tree generated.
3676 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3678 struct c_expr result
;
3680 result
.original_code
= code
;
3681 result
.original_type
= NULL
;
3683 if (reject_gcc_builtin (arg
.value
))
3685 result
.value
= error_mark_node
;
3689 result
.value
= build_unary_op (loc
, code
, arg
.value
, false);
3691 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3692 overflow_warning (loc
, result
.value
, arg
.value
);
3695 /* We are typically called when parsing a prefix token at LOC acting on
3696 ARG. Reflect this by updating the source range of the result to
3697 start at LOC and end at the end of ARG. */
3698 set_c_expr_source_range (&result
,
3699 loc
, arg
.get_finish ());
3704 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3707 char_type_p (tree type
)
3709 return (type
== char_type_node
3710 || type
== unsigned_char_type_node
3711 || type
== signed_char_type_node
3712 || type
== char16_type_node
3713 || type
== char32_type_node
);
3716 /* This is the entry point used by the parser to build binary operators
3717 in the input. CODE, a tree_code, specifies the binary operator, and
3718 ARG1 and ARG2 are the operands. In addition to constructing the
3719 expression, we check for operands that were written with other binary
3720 operators in a way that is likely to confuse the user.
3722 LOCATION is the location of the binary operator. */
3725 parser_build_binary_op (location_t location
, enum tree_code code
,
3726 struct c_expr arg1
, struct c_expr arg2
)
3728 struct c_expr result
;
3730 enum tree_code code1
= arg1
.original_code
;
3731 enum tree_code code2
= arg2
.original_code
;
3732 tree type1
= (arg1
.original_type
3733 ? arg1
.original_type
3734 : TREE_TYPE (arg1
.value
));
3735 tree type2
= (arg2
.original_type
3736 ? arg2
.original_type
3737 : TREE_TYPE (arg2
.value
));
3739 result
.value
= build_binary_op (location
, code
,
3740 arg1
.value
, arg2
.value
, true);
3741 result
.original_code
= code
;
3742 result
.original_type
= NULL
;
3744 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3746 set_c_expr_source_range (&result
,
3748 arg2
.get_finish ());
3752 if (location
!= UNKNOWN_LOCATION
)
3753 protected_set_expr_location (result
.value
, location
);
3755 set_c_expr_source_range (&result
,
3757 arg2
.get_finish ());
3759 /* Check for cases such as x+y<<z which users are likely
3761 if (warn_parentheses
)
3762 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3765 if (warn_logical_op
)
3766 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3767 code1
, arg1
.value
, code2
, arg2
.value
);
3769 if (warn_tautological_compare
)
3771 tree lhs
= arg1
.value
;
3772 tree rhs
= arg2
.value
;
3773 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
3775 if (C_MAYBE_CONST_EXPR_PRE (lhs
) != NULL_TREE
3776 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs
)))
3779 lhs
= C_MAYBE_CONST_EXPR_EXPR (lhs
);
3781 if (TREE_CODE (rhs
) == C_MAYBE_CONST_EXPR
)
3783 if (C_MAYBE_CONST_EXPR_PRE (rhs
) != NULL_TREE
3784 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs
)))
3787 rhs
= C_MAYBE_CONST_EXPR_EXPR (rhs
);
3789 if (lhs
!= NULL_TREE
&& rhs
!= NULL_TREE
)
3790 warn_tautological_cmp (location
, code
, lhs
, rhs
);
3793 if (warn_logical_not_paren
3794 && TREE_CODE_CLASS (code
) == tcc_comparison
3795 && code1
== TRUTH_NOT_EXPR
3796 && code2
!= TRUTH_NOT_EXPR
3797 /* Avoid warning for !!x == y. */
3798 && (TREE_CODE (arg1
.value
) != NE_EXPR
3799 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
3801 /* Avoid warning for !b == y where b has _Bool type. */
3802 tree t
= integer_zero_node
;
3803 if (TREE_CODE (arg1
.value
) == EQ_EXPR
3804 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
3805 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
3807 t
= TREE_OPERAND (arg1
.value
, 0);
3810 if (TREE_TYPE (t
) != integer_type_node
)
3812 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
3813 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
3814 else if (CONVERT_EXPR_P (t
))
3815 t
= TREE_OPERAND (t
, 0);
3821 if (TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
)
3822 warn_logical_not_parentheses (location
, code
, arg1
.value
, arg2
.value
);
3825 /* Warn about comparisons against string literals, with the exception
3826 of testing for equality or inequality of a string literal with NULL. */
3827 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3829 if ((code1
== STRING_CST
3830 && !integer_zerop (tree_strip_nop_conversions (arg2
.value
)))
3831 || (code2
== STRING_CST
3832 && !integer_zerop (tree_strip_nop_conversions (arg1
.value
))))
3833 warning_at (location
, OPT_Waddress
,
3834 "comparison with string literal results in unspecified behavior");
3835 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3836 if (POINTER_TYPE_P (type1
)
3837 && null_pointer_constant_p (arg2
.value
)
3838 && char_type_p (type2
))
3840 auto_diagnostic_group d
;
3841 if (warning_at (location
, OPT_Wpointer_compare
,
3842 "comparison between pointer and zero character "
3844 inform (arg1
.get_start (),
3845 "did you mean to dereference the pointer?");
3847 else if (POINTER_TYPE_P (type2
)
3848 && null_pointer_constant_p (arg1
.value
)
3849 && char_type_p (type1
))
3851 auto_diagnostic_group d
;
3852 if (warning_at (location
, OPT_Wpointer_compare
,
3853 "comparison between pointer and zero character "
3855 inform (arg2
.get_start (),
3856 "did you mean to dereference the pointer?");
3859 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3860 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3861 warning_at (location
, OPT_Waddress
,
3862 "comparison with string literal results in unspecified behavior");
3864 if (TREE_OVERFLOW_P (result
.value
)
3865 && !TREE_OVERFLOW_P (arg1
.value
)
3866 && !TREE_OVERFLOW_P (arg2
.value
))
3867 overflow_warning (location
, result
.value
);
3869 /* Warn about comparisons of different enum types. */
3870 if (warn_enum_compare
3871 && TREE_CODE_CLASS (code
) == tcc_comparison
3872 && TREE_CODE (type1
) == ENUMERAL_TYPE
3873 && TREE_CODE (type2
) == ENUMERAL_TYPE
3874 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3875 warning_at (location
, OPT_Wenum_compare
,
3876 "comparison between %qT and %qT",
3882 /* Return a tree for the difference of pointers OP0 and OP1.
3883 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3884 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3887 pointer_diff (location_t loc
, tree op0
, tree op1
, tree
*instrument_expr
)
3889 tree restype
= ptrdiff_type_node
;
3890 tree result
, inttype
;
3892 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3893 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3894 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3895 tree orig_op1
= op1
;
3897 /* If the operands point into different address spaces, we need to
3898 explicitly convert them to pointers into the common address space
3899 before we can subtract the numerical address values. */
3902 addr_space_t as_common
;
3905 /* Determine the common superset address space. This is guaranteed
3906 to exist because the caller verified that comp_target_types
3907 returned non-zero. */
3908 if (!addr_space_superset (as0
, as1
, &as_common
))
3911 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3912 op0
= convert (common_type
, op0
);
3913 op1
= convert (common_type
, op1
);
3916 /* Determine integer type result of the subtraction. This will usually
3917 be the same as the result type (ptrdiff_t), but may need to be a wider
3918 type if pointers for the address space are wider than ptrdiff_t. */
3919 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3920 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3924 if (TREE_CODE (target_type
) == VOID_TYPE
)
3925 pedwarn (loc
, OPT_Wpointer_arith
,
3926 "pointer of type %<void *%> used in subtraction");
3927 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3928 pedwarn (loc
, OPT_Wpointer_arith
,
3929 "pointer to a function used in subtraction");
3931 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
3933 gcc_assert (current_function_decl
!= NULL_TREE
);
3935 op0
= save_expr (op0
);
3936 op1
= save_expr (op1
);
3938 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
3939 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
3942 /* First do the subtraction, then build the divide operator
3943 and only convert at the very end.
3944 Do not do default conversions in case restype is a short type. */
3946 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3947 pointers. If some platform cannot provide that, or has a larger
3948 ptrdiff_type to support differences larger than half the address
3949 space, cast the pointers to some larger integer type and do the
3950 computations in that type. */
3951 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
3952 op0
= build_binary_op (loc
, MINUS_EXPR
, convert (inttype
, op0
),
3953 convert (inttype
, op1
), false);
3956 /* Cast away qualifiers. */
3957 op0
= convert (c_common_type (TREE_TYPE (op0
), TREE_TYPE (op0
)), op0
);
3958 op1
= convert (c_common_type (TREE_TYPE (op1
), TREE_TYPE (op1
)), op1
);
3959 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
3962 /* This generates an error if op1 is pointer to incomplete type. */
3963 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3964 error_at (loc
, "arithmetic on pointer to an incomplete type");
3966 op1
= c_size_in_bytes (target_type
);
3968 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3969 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3971 /* Divide by the size, in easiest possible way. */
3972 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3973 op0
, convert (inttype
, op1
));
3975 /* Convert to final result type if necessary. */
3976 return convert (restype
, result
);
3979 /* Expand atomic compound assignments into an appropriate sequence as
3980 specified by the C11 standard section 6.5.16.2.
3986 This sequence is used for all types for which these operations are
3989 In addition, built-in versions of the 'fe' prefixed routines may
3990 need to be invoked for floating point (real, complex or vector) when
3991 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
4001 __atomic_load (addr, &old, SEQ_CST);
4002 feholdexcept (&fenv);
4004 newval = old op val;
4005 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4008 feclearexcept (FE_ALL_EXCEPT);
4011 feupdateenv (&fenv);
4013 The compiler will issue the __atomic_fetch_* built-in when possible,
4014 otherwise it will generate the generic form of the atomic operations.
4015 This requires temp(s) and has their address taken. The atomic processing
4016 is smart enough to figure out when the size of an object can utilize
4017 a lock-free version, and convert the built-in call to the appropriate
4018 lock-free routine. The optimizers will then dispose of any temps that
4019 are no longer required, and lock-free implementations are utilized as
4020 long as there is target support for the required size.
4022 If the operator is NOP_EXPR, then this is a simple assignment, and
4023 an __atomic_store is issued to perform the assignment rather than
4026 /* Build an atomic assignment at LOC, expanding into the proper
4027 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4028 the result of the operation, unless RETURN_OLD_P, in which case
4029 return the old value of LHS (this is only for postincrement and
4033 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
4034 tree rhs
, bool return_old_p
)
4036 tree fndecl
, func_call
;
4037 vec
<tree
, va_gc
> *params
;
4038 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
4041 tree stmt
, goto_stmt
;
4042 tree loop_label
, loop_decl
, done_label
, done_decl
;
4044 tree lhs_type
= TREE_TYPE (lhs
);
4045 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, false);
4046 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
4047 tree rhs_semantic_type
= TREE_TYPE (rhs
);
4048 tree nonatomic_rhs_semantic_type
;
4051 gcc_assert (TYPE_ATOMIC (lhs_type
));
4054 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
4056 /* Allocate enough vector items for a compare_exchange. */
4057 vec_alloc (params
, 6);
4059 /* Create a compound statement to hold the sequence of statements
4061 compound_stmt
= c_begin_compound_stmt (false);
4063 /* Remove any excess precision (which is only present here in the
4064 case of compound assignments). */
4065 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4067 gcc_assert (modifycode
!= NOP_EXPR
);
4068 rhs
= TREE_OPERAND (rhs
, 0);
4070 rhs_type
= TREE_TYPE (rhs
);
4072 /* Fold the RHS if it hasn't already been folded. */
4073 if (modifycode
!= NOP_EXPR
)
4074 rhs
= c_fully_fold (rhs
, false, NULL
);
4076 /* Remove the qualifiers for the rest of the expressions and create
4077 the VAL temp variable to hold the RHS. */
4078 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
4079 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
4080 nonatomic_rhs_semantic_type
= build_qualified_type (rhs_semantic_type
,
4082 val
= create_tmp_var_raw (nonatomic_rhs_type
);
4083 TREE_ADDRESSABLE (val
) = 1;
4084 TREE_NO_WARNING (val
) = 1;
4085 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
4087 SET_EXPR_LOCATION (rhs
, loc
);
4090 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4092 if (modifycode
== NOP_EXPR
)
4094 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4095 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, false);
4096 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
4097 params
->quick_push (lhs_addr
);
4098 params
->quick_push (rhs
);
4099 params
->quick_push (seq_cst
);
4100 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4101 add_stmt (func_call
);
4103 /* Finish the compound statement. */
4104 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4106 /* VAL is the value which was stored, return a COMPOUND_STMT of
4107 the statement and that value. */
4108 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
4111 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4112 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4113 isn't applicable for such builtins. ??? Do we want to handle enums? */
4114 if ((TREE_CODE (lhs_type
) == INTEGER_TYPE
|| POINTER_TYPE_P (lhs_type
))
4115 && TREE_CODE (rhs_type
) == INTEGER_TYPE
)
4117 built_in_function fncode
;
4121 case POINTER_PLUS_EXPR
:
4122 fncode
= (return_old_p
4123 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4124 : BUILT_IN_ATOMIC_ADD_FETCH_N
);
4127 fncode
= (return_old_p
4128 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4129 : BUILT_IN_ATOMIC_SUB_FETCH_N
);
4132 fncode
= (return_old_p
4133 ? BUILT_IN_ATOMIC_FETCH_AND_N
4134 : BUILT_IN_ATOMIC_AND_FETCH_N
);
4137 fncode
= (return_old_p
4138 ? BUILT_IN_ATOMIC_FETCH_OR_N
4139 : BUILT_IN_ATOMIC_OR_FETCH_N
);
4142 fncode
= (return_old_p
4143 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4144 : BUILT_IN_ATOMIC_XOR_FETCH_N
);
4150 /* We can only use "_1" through "_16" variants of the atomic fetch
4152 unsigned HOST_WIDE_INT size
= tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type
));
4153 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8 && size
!= 16)
4156 /* If this is a pointer type, we need to multiply by the size of
4157 the pointer target type. */
4158 if (POINTER_TYPE_P (lhs_type
))
4160 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type
))
4161 /* ??? This would introduce -Wdiscarded-qualifiers
4162 warning: __atomic_fetch_* expect volatile void *
4163 type as the first argument. (Assignments between
4164 atomic and non-atomic objects are OK.) */
4165 || TYPE_RESTRICT (lhs_type
))
4167 tree sz
= TYPE_SIZE_UNIT (TREE_TYPE (lhs_type
));
4168 rhs
= fold_build2_loc (loc
, MULT_EXPR
, ptrdiff_type_node
,
4169 convert (ptrdiff_type_node
, rhs
),
4170 convert (ptrdiff_type_node
, sz
));
4173 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4174 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4175 fndecl
= builtin_decl_explicit (fncode
);
4176 params
->quick_push (lhs_addr
);
4177 params
->quick_push (rhs
);
4178 params
->quick_push (seq_cst
);
4179 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4181 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4182 TREE_ADDRESSABLE (newval
) = 1;
4183 TREE_NO_WARNING (newval
) = 1;
4184 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, func_call
,
4185 NULL_TREE
, NULL_TREE
);
4186 SET_EXPR_LOCATION (rhs
, loc
);
4189 /* Finish the compound statement. */
4190 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4192 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4193 the statement and that value. */
4194 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, newval
);
4198 /* Create the variables and labels required for the op= form. */
4199 old
= create_tmp_var_raw (nonatomic_lhs_type
);
4200 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, false);
4201 TREE_ADDRESSABLE (old
) = 1;
4202 TREE_NO_WARNING (old
) = 1;
4204 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4205 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, false);
4206 TREE_ADDRESSABLE (newval
) = 1;
4207 TREE_NO_WARNING (newval
) = 1;
4209 loop_decl
= create_artificial_label (loc
);
4210 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
4212 done_decl
= create_artificial_label (loc
);
4213 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
4215 /* __atomic_load (addr, &old, SEQ_CST). */
4216 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
4217 params
->quick_push (lhs_addr
);
4218 params
->quick_push (old_addr
);
4219 params
->quick_push (seq_cst
);
4220 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4221 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
4224 params
->truncate (0);
4226 /* Create the expressions for floating-point environment
4227 manipulation, if required. */
4228 bool need_fenv
= (flag_trapping_math
4229 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
4230 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
4232 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
4235 add_stmt (hold_call
);
4238 add_stmt (loop_label
);
4240 /* newval = old + val; */
4241 if (rhs_type
!= rhs_semantic_type
)
4242 val
= build1 (EXCESS_PRECISION_EXPR
, nonatomic_rhs_semantic_type
, val
);
4243 rhs
= build_binary_op (loc
, modifycode
, old
, val
, true);
4244 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4246 tree eptype
= TREE_TYPE (rhs
);
4247 rhs
= c_fully_fold (TREE_OPERAND (rhs
, 0), false, NULL
);
4248 rhs
= build1 (EXCESS_PRECISION_EXPR
, eptype
, rhs
);
4251 rhs
= c_fully_fold (rhs
, false, NULL
);
4252 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
4253 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
4255 if (rhs
!= error_mark_node
)
4257 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
4259 SET_EXPR_LOCATION (rhs
, loc
);
4263 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4265 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
4266 params
->quick_push (lhs_addr
);
4267 params
->quick_push (old_addr
);
4268 params
->quick_push (newval_addr
);
4269 params
->quick_push (integer_zero_node
);
4270 params
->quick_push (seq_cst
);
4271 params
->quick_push (seq_cst
);
4272 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4274 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
4275 SET_EXPR_LOCATION (goto_stmt
, loc
);
4277 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
4278 SET_EXPR_LOCATION (stmt
, loc
);
4282 add_stmt (clear_call
);
4285 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
4286 SET_EXPR_LOCATION (goto_stmt
, loc
);
4287 add_stmt (goto_stmt
);
4290 add_stmt (done_label
);
4293 add_stmt (update_call
);
4295 /* Finish the compound statement. */
4296 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4298 /* NEWVAL is the value that was successfully stored, return a
4299 COMPOUND_EXPR of the statement and the appropriate value. */
4300 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
4301 return_old_p
? old
: newval
);
4304 /* Construct and perhaps optimize a tree representation
4305 for a unary operation. CODE, a tree_code, specifies the operation
4306 and XARG is the operand.
4307 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4308 promotions (such as from short to int).
4309 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4310 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4313 LOCATION is the location of the operator. */
4316 build_unary_op (location_t location
, enum tree_code code
, tree xarg
,
4319 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4321 tree argtype
= NULL_TREE
;
4322 enum tree_code typecode
;
4324 tree ret
= error_mark_node
;
4325 tree eptype
= NULL_TREE
;
4326 const char *invalid_op_diag
;
4329 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
4331 arg
= remove_c_maybe_const_expr (arg
);
4333 if (code
!= ADDR_EXPR
)
4334 arg
= require_complete_type (location
, arg
);
4336 typecode
= TREE_CODE (TREE_TYPE (arg
));
4337 if (typecode
== ERROR_MARK
)
4338 return error_mark_node
;
4339 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
4340 typecode
= INTEGER_TYPE
;
4342 if ((invalid_op_diag
4343 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
4345 error_at (location
, invalid_op_diag
);
4346 return error_mark_node
;
4349 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
4351 eptype
= TREE_TYPE (arg
);
4352 arg
= TREE_OPERAND (arg
, 0);
4358 /* This is used for unary plus, because a CONVERT_EXPR
4359 is enough to prevent anybody from looking inside for
4360 associativity, but won't generate any code. */
4361 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4362 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4363 || typecode
== VECTOR_TYPE
))
4365 error_at (location
, "wrong type argument to unary plus");
4366 return error_mark_node
;
4368 else if (!noconvert
)
4369 arg
= default_conversion (arg
);
4370 arg
= non_lvalue_loc (location
, arg
);
4374 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4375 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4376 || typecode
== VECTOR_TYPE
))
4378 error_at (location
, "wrong type argument to unary minus");
4379 return error_mark_node
;
4381 else if (!noconvert
)
4382 arg
= default_conversion (arg
);
4386 /* ~ works on integer types and non float vectors. */
4387 if (typecode
== INTEGER_TYPE
4388 || (typecode
== VECTOR_TYPE
4389 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
4393 /* Warn if the expression has boolean value. */
4394 while (TREE_CODE (e
) == COMPOUND_EXPR
)
4395 e
= TREE_OPERAND (e
, 1);
4397 if ((TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
4398 || truth_value_p (TREE_CODE (e
))))
4400 auto_diagnostic_group d
;
4401 if (warning_at (location
, OPT_Wbool_operation
,
4402 "%<~%> on a boolean expression"))
4404 gcc_rich_location
richloc (location
);
4405 richloc
.add_fixit_insert_before (location
, "!");
4406 inform (&richloc
, "did you mean to use logical not?");
4410 arg
= default_conversion (arg
);
4412 else if (typecode
== COMPLEX_TYPE
)
4415 pedwarn (location
, OPT_Wpedantic
,
4416 "ISO C does not support %<~%> for complex conjugation");
4418 arg
= default_conversion (arg
);
4422 error_at (location
, "wrong type argument to bit-complement");
4423 return error_mark_node
;
4428 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
4430 error_at (location
, "wrong type argument to abs");
4431 return error_mark_node
;
4433 else if (!noconvert
)
4434 arg
= default_conversion (arg
);
4438 if (!(typecode
== INTEGER_TYPE
))
4440 error_at (location
, "wrong type argument to absu");
4441 return error_mark_node
;
4443 else if (!noconvert
)
4444 arg
= default_conversion (arg
);
4448 /* Conjugating a real value is a no-op, but allow it anyway. */
4449 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4450 || typecode
== COMPLEX_TYPE
))
4452 error_at (location
, "wrong type argument to conjugation");
4453 return error_mark_node
;
4455 else if (!noconvert
)
4456 arg
= default_conversion (arg
);
4459 case TRUTH_NOT_EXPR
:
4460 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4461 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
4462 && typecode
!= COMPLEX_TYPE
)
4465 "wrong type argument to unary exclamation mark");
4466 return error_mark_node
;
4470 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
4471 arg
= remove_c_maybe_const_expr (arg
);
4474 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
4475 ret
= invert_truthvalue_loc (location
, arg
);
4476 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4477 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
4478 location
= EXPR_LOCATION (ret
);
4479 goto return_build_unary_op
;
4483 ret
= build_real_imag_expr (location
, code
, arg
);
4484 if (ret
== error_mark_node
)
4485 return error_mark_node
;
4486 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
4487 eptype
= TREE_TYPE (eptype
);
4488 goto return_build_unary_op
;
4490 case PREINCREMENT_EXPR
:
4491 case POSTINCREMENT_EXPR
:
4492 case PREDECREMENT_EXPR
:
4493 case POSTDECREMENT_EXPR
:
4495 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4497 tree inner
= build_unary_op (location
, code
,
4498 C_MAYBE_CONST_EXPR_EXPR (arg
),
4500 if (inner
== error_mark_node
)
4501 return error_mark_node
;
4502 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4503 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4504 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4505 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
4506 goto return_build_unary_op
;
4509 /* Complain about anything that is not a true lvalue. In
4510 Objective-C, skip this check for property_refs. */
4511 if (!objc_is_property_ref (arg
)
4512 && !lvalue_or_else (location
,
4513 arg
, ((code
== PREINCREMENT_EXPR
4514 || code
== POSTINCREMENT_EXPR
)
4517 return error_mark_node
;
4519 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
4521 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4522 warning_at (location
, OPT_Wc___compat
,
4523 "increment of enumeration value is invalid in C++");
4525 warning_at (location
, OPT_Wc___compat
,
4526 "decrement of enumeration value is invalid in C++");
4529 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4531 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4532 warning_at (location
, OPT_Wbool_operation
,
4533 "increment of a boolean expression");
4535 warning_at (location
, OPT_Wbool_operation
,
4536 "decrement of a boolean expression");
4539 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4540 arg
= c_fully_fold (arg
, false, NULL
, true);
4543 atomic_op
= really_atomic_lvalue (arg
);
4545 /* Increment or decrement the real part of the value,
4546 and don't change the imaginary part. */
4547 if (typecode
== COMPLEX_TYPE
)
4551 pedwarn (location
, OPT_Wpedantic
,
4552 "ISO C does not support %<++%> and %<--%> on complex types");
4556 arg
= stabilize_reference (arg
);
4557 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
,
4559 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
,
4561 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, true);
4562 if (real
== error_mark_node
|| imag
== error_mark_node
)
4563 return error_mark_node
;
4564 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4566 goto return_build_unary_op
;
4570 /* Report invalid types. */
4572 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4573 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4574 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
4576 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4577 error_at (location
, "wrong type argument to increment");
4579 error_at (location
, "wrong type argument to decrement");
4581 return error_mark_node
;
4587 argtype
= TREE_TYPE (arg
);
4589 /* Compute the increment. */
4591 if (typecode
== POINTER_TYPE
)
4593 /* If pointer target is an incomplete type,
4594 we just cannot know how to do the arithmetic. */
4595 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4597 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4599 "increment of pointer to an incomplete type %qT",
4600 TREE_TYPE (argtype
));
4603 "decrement of pointer to an incomplete type %qT",
4604 TREE_TYPE (argtype
));
4606 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4607 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4609 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4610 pedwarn (location
, OPT_Wpointer_arith
,
4611 "wrong type argument to increment");
4613 pedwarn (location
, OPT_Wpointer_arith
,
4614 "wrong type argument to decrement");
4617 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4618 inc
= convert_to_ptrofftype_loc (location
, inc
);
4620 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4622 /* For signed fract types, we invert ++ to -- or
4623 -- to ++, and change inc from 1 to -1, because
4624 it is not possible to represent 1 in signed fract constants.
4625 For unsigned fract types, the result always overflows and
4626 we get an undefined (original) or the maximum value. */
4627 if (code
== PREINCREMENT_EXPR
)
4628 code
= PREDECREMENT_EXPR
;
4629 else if (code
== PREDECREMENT_EXPR
)
4630 code
= PREINCREMENT_EXPR
;
4631 else if (code
== POSTINCREMENT_EXPR
)
4632 code
= POSTDECREMENT_EXPR
;
4633 else /* code == POSTDECREMENT_EXPR */
4634 code
= POSTINCREMENT_EXPR
;
4636 inc
= integer_minus_one_node
;
4637 inc
= convert (argtype
, inc
);
4641 inc
= VECTOR_TYPE_P (argtype
)
4642 ? build_one_cst (argtype
)
4644 inc
= convert (argtype
, inc
);
4647 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4648 need to ask Objective-C to build the increment or decrement
4649 expression for it. */
4650 if (objc_is_property_ref (arg
))
4651 return objc_build_incr_expr_for_property_ref (location
, code
,
4654 /* Report a read-only lvalue. */
4655 if (TYPE_READONLY (argtype
))
4657 readonly_error (location
, arg
,
4658 ((code
== PREINCREMENT_EXPR
4659 || code
== POSTINCREMENT_EXPR
)
4660 ? lv_increment
: lv_decrement
));
4661 return error_mark_node
;
4663 else if (TREE_READONLY (arg
))
4664 readonly_warning (arg
,
4665 ((code
== PREINCREMENT_EXPR
4666 || code
== POSTINCREMENT_EXPR
)
4667 ? lv_increment
: lv_decrement
));
4669 /* If the argument is atomic, use the special code sequences for
4670 atomic compound assignment. */
4673 arg
= stabilize_reference (arg
);
4674 ret
= build_atomic_assign (location
, arg
,
4675 ((code
== PREINCREMENT_EXPR
4676 || code
== POSTINCREMENT_EXPR
)
4679 (FRACT_MODE_P (TYPE_MODE (argtype
))
4681 : integer_one_node
),
4682 (code
== POSTINCREMENT_EXPR
4683 || code
== POSTDECREMENT_EXPR
));
4684 goto return_build_unary_op
;
4687 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4688 val
= boolean_increment (code
, arg
);
4690 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4691 TREE_SIDE_EFFECTS (val
) = 1;
4692 if (TREE_CODE (val
) != code
)
4693 TREE_NO_WARNING (val
) = 1;
4695 goto return_build_unary_op
;
4699 /* Note that this operation never does default_conversion. */
4701 /* The operand of unary '&' must be an lvalue (which excludes
4702 expressions of type void), or, in C99, the result of a [] or
4703 unary '*' operator. */
4704 if (VOID_TYPE_P (TREE_TYPE (arg
))
4705 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4706 && (!INDIRECT_REF_P (arg
) || !flag_isoc99
))
4707 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4709 /* Let &* cancel out to simplify resulting code. */
4710 if (INDIRECT_REF_P (arg
))
4712 /* Don't let this be an lvalue. */
4713 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4714 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4715 ret
= TREE_OPERAND (arg
, 0);
4716 goto return_build_unary_op
;
4719 /* Anything not already handled and not a true memory reference
4720 or a non-lvalue array is an error. */
4721 if (typecode
!= FUNCTION_TYPE
&& !noconvert
4722 && !lvalue_or_else (location
, arg
, lv_addressof
))
4723 return error_mark_node
;
4725 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4727 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4729 tree inner
= build_unary_op (location
, code
,
4730 C_MAYBE_CONST_EXPR_EXPR (arg
),
4732 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4733 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4734 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4735 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4736 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4737 goto return_build_unary_op
;
4740 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4741 argtype
= TREE_TYPE (arg
);
4743 /* If the lvalue is const or volatile, merge that into the type
4744 to which the address will point. This is only needed
4745 for function types. */
4746 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4747 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4748 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4750 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4751 int quals
= orig_quals
;
4753 if (TREE_READONLY (arg
))
4754 quals
|= TYPE_QUAL_CONST
;
4755 if (TREE_THIS_VOLATILE (arg
))
4756 quals
|= TYPE_QUAL_VOLATILE
;
4758 argtype
= c_build_qualified_type (argtype
, quals
);
4761 switch (TREE_CODE (arg
))
4764 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
4766 error_at (location
, "cannot take address of bit-field %qD",
4767 TREE_OPERAND (arg
, 1));
4768 return error_mark_node
;
4774 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg
, 0))))
4776 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg
))
4777 && !VECTOR_TYPE_P (TREE_TYPE (arg
)))
4779 error_at (location
, "cannot take address of scalar with "
4780 "reverse storage order");
4781 return error_mark_node
;
4784 if (TREE_CODE (TREE_TYPE (arg
)) == ARRAY_TYPE
4785 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg
)))
4786 warning_at (location
, OPT_Wscalar_storage_order
,
4787 "address of array with reverse scalar storage "
4795 if (!c_mark_addressable (arg
))
4796 return error_mark_node
;
4798 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4799 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4801 argtype
= build_pointer_type (argtype
);
4803 /* ??? Cope with user tricks that amount to offsetof. Delete this
4804 when we have proper support for integer constant expressions. */
4805 val
= get_base_address (arg
);
4806 if (val
&& INDIRECT_REF_P (val
)
4807 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4809 ret
= fold_offsetof (arg
, argtype
);
4810 goto return_build_unary_op
;
4813 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4816 goto return_build_unary_op
;
4822 if (argtype
== NULL_TREE
)
4823 argtype
= TREE_TYPE (arg
);
4824 if (TREE_CODE (arg
) == INTEGER_CST
)
4825 ret
= (require_constant_value
4826 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4827 : fold_build1_loc (location
, code
, argtype
, arg
));
4829 ret
= build1 (code
, argtype
, arg
);
4830 return_build_unary_op
:
4831 gcc_assert (ret
!= error_mark_node
);
4832 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4833 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4834 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4835 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4836 ret
= note_integer_operands (ret
);
4838 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4839 protected_set_expr_location (ret
, location
);
4843 /* Return nonzero if REF is an lvalue valid for this language.
4844 Lvalues can be assigned, unless their type has TYPE_READONLY.
4845 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4848 lvalue_p (const_tree ref
)
4850 const enum tree_code code
= TREE_CODE (ref
);
4857 return lvalue_p (TREE_OPERAND (ref
, 0));
4859 case C_MAYBE_CONST_EXPR
:
4860 return lvalue_p (TREE_OPERAND (ref
, 1));
4862 case COMPOUND_LITERAL_EXPR
:
4872 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4873 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4876 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4883 /* Give a warning for storing in something that is read-only in GCC
4884 terms but not const in ISO C terms. */
4887 readonly_warning (tree arg
, enum lvalue_use use
)
4892 warning (0, "assignment of read-only location %qE", arg
);
4895 warning (0, "increment of read-only location %qE", arg
);
4898 warning (0, "decrement of read-only location %qE", arg
);
4907 /* Return nonzero if REF is an lvalue valid for this language;
4908 otherwise, print an error message and return zero. USE says
4909 how the lvalue is being used and so selects the error message.
4910 LOCATION is the location at which any error should be reported. */
4913 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4915 int win
= lvalue_p (ref
);
4918 lvalue_error (loc
, use
);
4923 /* Mark EXP saying that we need to be able to take the
4924 address of it; it should not be allocated in a register.
4925 Returns true if successful. ARRAY_REF_P is true if this
4926 is for ARRAY_REF construction - in that case we don't want
4927 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4928 it is fine to use ARRAY_REFs for vector subscripts on vector
4929 register variables. */
4932 c_mark_addressable (tree exp
, bool array_ref_p
)
4937 switch (TREE_CODE (x
))
4939 case VIEW_CONVERT_EXPR
:
4941 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
4942 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
4950 x
= TREE_OPERAND (x
, 0);
4953 case COMPOUND_LITERAL_EXPR
:
4954 TREE_ADDRESSABLE (x
) = 1;
4955 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x
)) = 1;
4959 TREE_ADDRESSABLE (x
) = 1;
4966 if (C_DECL_REGISTER (x
)
4967 && DECL_NONLOCAL (x
))
4969 if (TREE_PUBLIC (x
) || is_global_var (x
))
4972 ("global register variable %qD used in nested function", x
);
4975 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4977 else if (C_DECL_REGISTER (x
))
4979 if (TREE_PUBLIC (x
) || is_global_var (x
))
4980 error ("address of global register variable %qD requested", x
);
4982 error ("address of register variable %qD requested", x
);
4988 TREE_ADDRESSABLE (x
) = 1;
4995 /* Convert EXPR to TYPE, warning about conversion problems with
4996 constants. SEMANTIC_TYPE is the type this conversion would use
4997 without excess precision. If SEMANTIC_TYPE is NULL, this function
4998 is equivalent to convert_and_check. This function is a wrapper that
4999 handles conversions that may be different than
5000 the usual ones because of excess precision. */
5003 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
5006 if (TREE_TYPE (expr
) == type
)
5009 /* For C11, integer conversions may have results with excess
5011 if (flag_isoc11
|| !semantic_type
)
5012 return convert_and_check (loc
, type
, expr
);
5014 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
5015 && TREE_TYPE (expr
) != semantic_type
)
5017 /* For integers, we need to check the real conversion, not
5018 the conversion to the excess precision type. */
5019 expr
= convert_and_check (loc
, semantic_type
, expr
);
5021 /* Result type is the excess precision type, which should be
5022 large enough, so do not check. */
5023 return convert (type
, expr
);
5026 /* If EXPR refers to a built-in declared without a prototype returns
5027 the actual type of the built-in and, if non-null, set *BLTIN to
5028 a pointer to the built-in. Otherwise return the type of EXPR
5029 and clear *BLTIN if non-null. */
5032 type_or_builtin_type (tree expr
, tree
*bltin
= NULL
)
5040 tree type
= TREE_TYPE (expr
);
5041 if (TREE_CODE (expr
) != ADDR_EXPR
)
5044 tree oper
= TREE_OPERAND (expr
, 0);
5046 || TREE_CODE (oper
) != FUNCTION_DECL
5047 || !fndecl_built_in_p (oper
, BUILT_IN_NORMAL
))
5050 built_in_function code
= DECL_FUNCTION_CODE (oper
);
5051 if (!C_DECL_BUILTIN_PROTOTYPE (oper
))
5054 if ((*bltin
= builtin_decl_implicit (code
)))
5055 type
= build_pointer_type (TREE_TYPE (*bltin
));
5060 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5061 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5062 if folded to an integer constant then the unselected half may
5063 contain arbitrary operations not normally permitted in constant
5064 expressions. Set the location of the expression to LOC. */
5067 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
5068 tree op1
, tree op1_original_type
, location_t op1_loc
,
5069 tree op2
, tree op2_original_type
, location_t op2_loc
)
5073 enum tree_code code1
;
5074 enum tree_code code2
;
5075 tree result_type
= NULL
;
5076 tree semantic_result_type
= NULL
;
5077 tree orig_op1
= op1
, orig_op2
= op2
;
5078 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
5079 bool ifexp_int_operands
;
5082 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
5083 if (op1_int_operands
)
5084 op1
= remove_c_maybe_const_expr (op1
);
5085 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
5086 if (op2_int_operands
)
5087 op2
= remove_c_maybe_const_expr (op2
);
5088 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
5089 if (ifexp_int_operands
)
5090 ifexp
= remove_c_maybe_const_expr (ifexp
);
5092 /* Promote both alternatives. */
5094 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
5095 op1
= default_conversion (op1
);
5096 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
5097 op2
= default_conversion (op2
);
5099 if (TREE_CODE (ifexp
) == ERROR_MARK
5100 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
5101 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
5102 return error_mark_node
;
5104 tree bltin1
= NULL_TREE
;
5105 tree bltin2
= NULL_TREE
;
5106 type1
= type_or_builtin_type (op1
, &bltin1
);
5107 code1
= TREE_CODE (type1
);
5108 type2
= type_or_builtin_type (op2
, &bltin2
);
5109 code2
= TREE_CODE (type2
);
5111 if (code1
== POINTER_TYPE
&& reject_gcc_builtin (op1
))
5112 return error_mark_node
;
5114 if (code2
== POINTER_TYPE
&& reject_gcc_builtin (op2
))
5115 return error_mark_node
;
5117 /* C90 does not permit non-lvalue arrays in conditional expressions.
5118 In C99 they will be pointers by now. */
5119 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
5121 error_at (colon_loc
, "non-lvalue array in conditional expression");
5122 return error_mark_node
;
5125 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
5126 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
5127 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5128 || code1
== COMPLEX_TYPE
)
5129 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
5130 || code2
== COMPLEX_TYPE
))
5132 semantic_result_type
= c_common_type (type1
, type2
);
5133 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
5135 op1
= TREE_OPERAND (op1
, 0);
5136 type1
= TREE_TYPE (op1
);
5137 gcc_assert (TREE_CODE (type1
) == code1
);
5139 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
5141 op2
= TREE_OPERAND (op2
, 0);
5142 type2
= TREE_TYPE (op2
);
5143 gcc_assert (TREE_CODE (type2
) == code2
);
5147 if (warn_cxx_compat
)
5149 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
5150 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
5152 if (TREE_CODE (t1
) == ENUMERAL_TYPE
5153 && TREE_CODE (t2
) == ENUMERAL_TYPE
5154 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
5155 warning_at (colon_loc
, OPT_Wc___compat
,
5156 ("different enum types in conditional is "
5157 "invalid in C++: %qT vs %qT"),
5161 /* Quickly detect the usual case where op1 and op2 have the same type
5163 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
5166 result_type
= type1
;
5168 result_type
= TYPE_MAIN_VARIANT (type1
);
5170 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5171 || code1
== COMPLEX_TYPE
)
5172 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
5173 || code2
== COMPLEX_TYPE
))
5175 /* In C11, a conditional expression between a floating-point
5176 type and an integer type should convert the integer type to
5177 the evaluation format of the floating-point type, with
5178 possible excess precision. */
5179 tree eptype1
= type1
;
5180 tree eptype2
= type2
;
5184 if (ANY_INTEGRAL_TYPE_P (type1
)
5185 && (eptype
= excess_precision_type (type2
)) != NULL_TREE
)
5188 if (!semantic_result_type
)
5189 semantic_result_type
= c_common_type (type1
, type2
);
5191 else if (ANY_INTEGRAL_TYPE_P (type2
)
5192 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
5195 if (!semantic_result_type
)
5196 semantic_result_type
= c_common_type (type1
, type2
);
5199 result_type
= c_common_type (eptype1
, eptype2
);
5200 if (result_type
== error_mark_node
)
5201 return error_mark_node
;
5202 do_warn_double_promotion (result_type
, type1
, type2
,
5203 "implicit conversion from %qT to %qT to "
5204 "match other result of conditional",
5207 /* If -Wsign-compare, warn here if type1 and type2 have
5208 different signedness. We'll promote the signed to unsigned
5209 and later code won't know it used to be different.
5210 Do this check on the original types, so that explicit casts
5211 will be considered, but default promotions won't. */
5212 if (c_inhibit_evaluation_warnings
== 0)
5214 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
5215 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
5217 if (unsigned_op1
^ unsigned_op2
)
5221 /* Do not warn if the result type is signed, since the
5222 signed type will only be chosen if it can represent
5223 all the values of the unsigned type. */
5224 if (!TYPE_UNSIGNED (result_type
))
5228 bool op1_maybe_const
= true;
5229 bool op2_maybe_const
= true;
5231 /* Do not warn if the signed quantity is an
5232 unsuffixed integer literal (or some static
5233 constant expression involving such literals) and
5234 it is non-negative. This warning requires the
5235 operands to be folded for best results, so do
5236 that folding in this case even without
5237 warn_sign_compare to avoid warning options
5238 possibly affecting code generation. */
5239 c_inhibit_evaluation_warnings
5240 += (ifexp
== truthvalue_false_node
);
5241 op1
= c_fully_fold (op1
, require_constant_value
,
5243 c_inhibit_evaluation_warnings
5244 -= (ifexp
== truthvalue_false_node
);
5246 c_inhibit_evaluation_warnings
5247 += (ifexp
== truthvalue_true_node
);
5248 op2
= c_fully_fold (op2
, require_constant_value
,
5250 c_inhibit_evaluation_warnings
5251 -= (ifexp
== truthvalue_true_node
);
5253 if (warn_sign_compare
)
5256 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
5258 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
5260 else if (unsigned_op2
)
5261 warning_at (op1_loc
, OPT_Wsign_compare
,
5262 "operand of %<?:%> changes signedness from "
5263 "%qT to %qT due to unsignedness of other "
5264 "operand", TREE_TYPE (orig_op1
),
5265 TREE_TYPE (orig_op2
));
5267 warning_at (op2_loc
, OPT_Wsign_compare
,
5268 "operand of %<?:%> changes signedness from "
5269 "%qT to %qT due to unsignedness of other "
5270 "operand", TREE_TYPE (orig_op2
),
5271 TREE_TYPE (orig_op1
));
5273 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
5274 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
5275 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
5276 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
5281 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
5283 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
5284 pedwarn (colon_loc
, OPT_Wpedantic
,
5285 "ISO C forbids conditional expr with only one void side");
5286 result_type
= void_type_node
;
5288 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5290 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
5291 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
5292 addr_space_t as_common
;
5294 if (comp_target_types (colon_loc
, type1
, type2
))
5295 result_type
= common_pointer_type (type1
, type2
);
5296 else if (null_pointer_constant_p (orig_op1
))
5297 result_type
= type2
;
5298 else if (null_pointer_constant_p (orig_op2
))
5299 result_type
= type1
;
5300 else if (!addr_space_superset (as1
, as2
, &as_common
))
5302 error_at (colon_loc
, "pointers to disjoint address spaces "
5303 "used in conditional expression");
5304 return error_mark_node
;
5306 else if (VOID_TYPE_P (TREE_TYPE (type1
))
5307 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
5309 if ((TREE_CODE (TREE_TYPE (type2
)) == ARRAY_TYPE
)
5310 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2
)))
5311 & ~TYPE_QUALS (TREE_TYPE (type1
))))
5312 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5313 "pointer to array loses qualifier "
5314 "in conditional expression");
5316 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
5317 pedwarn (colon_loc
, OPT_Wpedantic
,
5318 "ISO C forbids conditional expr between "
5319 "%<void *%> and function pointer");
5320 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
5321 TREE_TYPE (type2
)));
5323 else if (VOID_TYPE_P (TREE_TYPE (type2
))
5324 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
5326 if ((TREE_CODE (TREE_TYPE (type1
)) == ARRAY_TYPE
)
5327 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1
)))
5328 & ~TYPE_QUALS (TREE_TYPE (type2
))))
5329 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5330 "pointer to array loses qualifier "
5331 "in conditional expression");
5333 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
5334 pedwarn (colon_loc
, OPT_Wpedantic
,
5335 "ISO C forbids conditional expr between "
5336 "%<void *%> and function pointer");
5337 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
5338 TREE_TYPE (type1
)));
5340 /* Objective-C pointer comparisons are a bit more lenient. */
5341 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
5342 result_type
= objc_common_type (type1
, type2
);
5345 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
5346 if (bltin1
&& bltin2
)
5347 warning_at (colon_loc
, OPT_Wincompatible_pointer_types
,
5348 "pointer type mismatch between %qT and %qT "
5349 "of %qD and %qD in conditional expression",
5350 type1
, type2
, bltin1
, bltin2
);
5352 pedwarn (colon_loc
, 0,
5353 "pointer type mismatch in conditional expression");
5354 result_type
= build_pointer_type
5355 (build_qualified_type (void_type_node
, qual
));
5358 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5360 if (!null_pointer_constant_p (orig_op2
))
5361 pedwarn (colon_loc
, 0,
5362 "pointer/integer type mismatch in conditional expression");
5365 op2
= null_pointer_node
;
5367 result_type
= type1
;
5369 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5371 if (!null_pointer_constant_p (orig_op1
))
5372 pedwarn (colon_loc
, 0,
5373 "pointer/integer type mismatch in conditional expression");
5376 op1
= null_pointer_node
;
5378 result_type
= type2
;
5383 if (flag_cond_mismatch
)
5384 result_type
= void_type_node
;
5387 error_at (colon_loc
, "type mismatch in conditional expression");
5388 return error_mark_node
;
5392 /* Merge const and volatile flags of the incoming types. */
5394 = build_type_variant (result_type
,
5395 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
5396 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
5398 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
5399 semantic_result_type
);
5400 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
5401 semantic_result_type
);
5403 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
5405 op2_int_operands
= true;
5406 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
5408 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
5410 op1_int_operands
= true;
5411 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
5413 int_const
= int_operands
= (ifexp_int_operands
5415 && op2_int_operands
);
5418 int_const
= ((ifexp
== truthvalue_true_node
5419 && TREE_CODE (orig_op1
) == INTEGER_CST
5420 && !TREE_OVERFLOW (orig_op1
))
5421 || (ifexp
== truthvalue_false_node
5422 && TREE_CODE (orig_op2
) == INTEGER_CST
5423 && !TREE_OVERFLOW (orig_op2
)));
5426 /* Need to convert condition operand into a vector mask. */
5427 if (VECTOR_TYPE_P (TREE_TYPE (ifexp
)))
5429 tree vectype
= TREE_TYPE (ifexp
);
5430 tree elem_type
= TREE_TYPE (vectype
);
5431 tree zero
= build_int_cst (elem_type
, 0);
5432 tree zero_vec
= build_vector_from_val (vectype
, zero
);
5433 tree cmp_type
= build_same_sized_truth_vector_type (vectype
);
5434 ifexp
= build2 (NE_EXPR
, cmp_type
, ifexp
, zero_vec
);
5437 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
5438 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5443 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5444 nested inside of the expression. */
5445 op1
= c_fully_fold (op1
, false, NULL
);
5446 op2
= c_fully_fold (op2
, false, NULL
);
5448 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5450 ret
= note_integer_operands (ret
);
5452 if (semantic_result_type
)
5453 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
5455 protected_set_expr_location (ret
, colon_loc
);
5457 /* If the OP1 and OP2 are the same and don't have side-effects,
5458 warn here, because the COND_EXPR will be turned into OP1. */
5459 if (warn_duplicated_branches
5460 && TREE_CODE (ret
) == COND_EXPR
5461 && (op1
== op2
|| operand_equal_p (op1
, op2
, 0)))
5462 warning_at (EXPR_LOCATION (ret
), OPT_Wduplicated_branches
,
5463 "this condition has identical branches");
5468 /* Return a compound expression that performs two expressions and
5469 returns the value of the second of them.
5471 LOC is the location of the COMPOUND_EXPR. */
5474 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
5476 bool expr1_int_operands
, expr2_int_operands
;
5477 tree eptype
= NULL_TREE
;
5480 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
5481 if (expr1_int_operands
)
5482 expr1
= remove_c_maybe_const_expr (expr1
);
5483 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
5484 if (expr2_int_operands
)
5485 expr2
= remove_c_maybe_const_expr (expr2
);
5487 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
5488 expr1
= TREE_OPERAND (expr1
, 0);
5489 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
5491 eptype
= TREE_TYPE (expr2
);
5492 expr2
= TREE_OPERAND (expr2
, 0);
5495 if (!TREE_SIDE_EFFECTS (expr1
))
5497 /* The left-hand operand of a comma expression is like an expression
5498 statement: with -Wunused, we should warn if it doesn't have
5499 any side-effects, unless it was explicitly cast to (void). */
5500 if (warn_unused_value
)
5502 if (VOID_TYPE_P (TREE_TYPE (expr1
))
5503 && CONVERT_EXPR_P (expr1
))
5505 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
5506 && TREE_CODE (expr1
) == COMPOUND_EXPR
5507 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
5508 ; /* (void) a, (void) b, c */
5510 warning_at (loc
, OPT_Wunused_value
,
5511 "left-hand operand of comma expression has no effect");
5514 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
5515 && warn_unused_value
)
5518 location_t cloc
= loc
;
5519 while (TREE_CODE (r
) == COMPOUND_EXPR
)
5521 if (EXPR_HAS_LOCATION (r
))
5522 cloc
= EXPR_LOCATION (r
);
5523 r
= TREE_OPERAND (r
, 1);
5525 if (!TREE_SIDE_EFFECTS (r
)
5526 && !VOID_TYPE_P (TREE_TYPE (r
))
5527 && !CONVERT_EXPR_P (r
))
5528 warning_at (cloc
, OPT_Wunused_value
,
5529 "right-hand operand of comma expression has no effect");
5532 /* With -Wunused, we should also warn if the left-hand operand does have
5533 side-effects, but computes a value which is not used. For example, in
5534 `foo() + bar(), baz()' the result of the `+' operator is not used,
5535 so we should issue a warning. */
5536 else if (warn_unused_value
)
5537 warn_if_unused_value (expr1
, loc
);
5539 if (expr2
== error_mark_node
)
5540 return error_mark_node
;
5542 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
5545 && expr1_int_operands
5546 && expr2_int_operands
)
5547 ret
= note_integer_operands (ret
);
5550 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
5552 protected_set_expr_location (ret
, loc
);
5556 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5557 which we are casting. OTYPE is the type of the expression being
5558 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5559 of the cast. -Wcast-qual appeared on the command line. Named
5560 address space qualifiers are not handled here, because they result
5561 in different warnings. */
5564 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
5566 tree in_type
= type
;
5567 tree in_otype
= otype
;
5572 /* Check that the qualifiers on IN_TYPE are a superset of the
5573 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5574 nodes is uninteresting and we stop as soon as we hit a
5575 non-POINTER_TYPE node on either type. */
5578 in_otype
= TREE_TYPE (in_otype
);
5579 in_type
= TREE_TYPE (in_type
);
5581 /* GNU C allows cv-qualified function types. 'const' means the
5582 function is very pure, 'volatile' means it can't return. We
5583 need to warn when such qualifiers are added, not when they're
5585 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
5586 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
5587 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
5588 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
5590 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
5591 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
5593 while (TREE_CODE (in_type
) == POINTER_TYPE
5594 && TREE_CODE (in_otype
) == POINTER_TYPE
);
5597 warning_at (loc
, OPT_Wcast_qual
,
5598 "cast adds %q#v qualifier to function type", added
);
5601 /* There are qualifiers present in IN_OTYPE that are not present
5603 warning_at (loc
, OPT_Wcast_qual
,
5604 "cast discards %qv qualifier from pointer target type",
5607 if (added
|| discarded
)
5610 /* A cast from **T to const **T is unsafe, because it can cause a
5611 const value to be changed with no additional warning. We only
5612 issue this warning if T is the same on both sides, and we only
5613 issue the warning if there are the same number of pointers on
5614 both sides, as otherwise the cast is clearly unsafe anyhow. A
5615 cast is unsafe when a qualifier is added at one level and const
5616 is not present at all outer levels.
5618 To issue this warning, we check at each level whether the cast
5619 adds new qualifiers not already seen. We don't need to special
5620 case function types, as they won't have the same
5621 TYPE_MAIN_VARIANT. */
5623 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
5625 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
5630 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
5633 in_type
= TREE_TYPE (in_type
);
5634 in_otype
= TREE_TYPE (in_otype
);
5635 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
5638 warning_at (loc
, OPT_Wcast_qual
,
5639 "to be safe all intermediate pointers in cast from "
5640 "%qT to %qT must be %<const%> qualified",
5645 is_const
= TYPE_READONLY (in_type
);
5647 while (TREE_CODE (in_type
) == POINTER_TYPE
);
5650 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5653 c_safe_arg_type_equiv_p (tree t1
, tree t2
)
5655 t1
= TYPE_MAIN_VARIANT (t1
);
5656 t2
= TYPE_MAIN_VARIANT (t2
);
5658 if (TREE_CODE (t1
) == POINTER_TYPE
5659 && TREE_CODE (t2
) == POINTER_TYPE
)
5662 /* The signedness of the parameter matters only when an integral
5663 type smaller than int is promoted to int, otherwise only the
5664 precision of the parameter matters.
5665 This check should make sure that the callee does not see
5666 undefined values in argument registers. */
5667 if (INTEGRAL_TYPE_P (t1
)
5668 && INTEGRAL_TYPE_P (t2
)
5669 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
5670 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
5671 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
5672 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
5675 return comptypes (t1
, t2
);
5678 /* Check if a type cast between two function types can be considered safe. */
5681 c_safe_function_type_cast_p (tree t1
, tree t2
)
5683 if (TREE_TYPE (t1
) == void_type_node
&&
5684 TYPE_ARG_TYPES (t1
) == void_list_node
)
5687 if (TREE_TYPE (t2
) == void_type_node
&&
5688 TYPE_ARG_TYPES (t2
) == void_list_node
)
5691 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
5694 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
5696 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
5697 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
5703 /* Build an expression representing a cast to type TYPE of expression EXPR.
5704 LOC is the location of the cast-- typically the open paren of the cast. */
5707 build_c_cast (location_t loc
, tree type
, tree expr
)
5711 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
5712 expr
= TREE_OPERAND (expr
, 0);
5716 if (type
== error_mark_node
|| expr
== error_mark_node
)
5717 return error_mark_node
;
5719 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5720 only in <protocol> qualifications. But when constructing cast expressions,
5721 the protocols do matter and must be kept around. */
5722 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
5723 return build1 (NOP_EXPR
, type
, expr
);
5725 type
= TYPE_MAIN_VARIANT (type
);
5727 if (TREE_CODE (type
) == ARRAY_TYPE
)
5729 error_at (loc
, "cast specifies array type");
5730 return error_mark_node
;
5733 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5735 error_at (loc
, "cast specifies function type");
5736 return error_mark_node
;
5739 if (!VOID_TYPE_P (type
))
5741 value
= require_complete_type (loc
, value
);
5742 if (value
== error_mark_node
)
5743 return error_mark_node
;
5746 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
5748 if (RECORD_OR_UNION_TYPE_P (type
))
5749 pedwarn (loc
, OPT_Wpedantic
,
5750 "ISO C forbids casting nonscalar to the same type");
5752 /* Convert to remove any qualifiers from VALUE's type. */
5753 value
= convert (type
, value
);
5755 else if (TREE_CODE (type
) == UNION_TYPE
)
5759 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5760 if (TREE_TYPE (field
) != error_mark_node
5761 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
5762 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
5768 bool maybe_const
= true;
5770 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
5771 t
= c_fully_fold (value
, false, &maybe_const
);
5772 t
= build_constructor_single (type
, field
, t
);
5774 t
= c_wrap_maybe_const (t
, true);
5775 t
= digest_init (loc
, type
, t
,
5776 NULL_TREE
, false, true, 0);
5777 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
5780 error_at (loc
, "cast to union type from type not present in union");
5781 return error_mark_node
;
5787 if (type
== void_type_node
)
5789 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5790 SET_EXPR_LOCATION (t
, loc
);
5794 otype
= TREE_TYPE (value
);
5796 /* Optionally warn about potentially worrisome casts. */
5798 && TREE_CODE (type
) == POINTER_TYPE
5799 && TREE_CODE (otype
) == POINTER_TYPE
)
5800 handle_warn_cast_qual (loc
, type
, otype
);
5802 /* Warn about conversions between pointers to disjoint
5804 if (TREE_CODE (type
) == POINTER_TYPE
5805 && TREE_CODE (otype
) == POINTER_TYPE
5806 && !null_pointer_constant_p (value
))
5808 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5809 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5810 addr_space_t as_common
;
5812 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5814 if (ADDR_SPACE_GENERIC_P (as_from
))
5815 warning_at (loc
, 0, "cast to %s address space pointer "
5816 "from disjoint generic address space pointer",
5817 c_addr_space_name (as_to
));
5819 else if (ADDR_SPACE_GENERIC_P (as_to
))
5820 warning_at (loc
, 0, "cast to generic address space pointer "
5821 "from disjoint %s address space pointer",
5822 c_addr_space_name (as_from
));
5825 warning_at (loc
, 0, "cast to %s address space pointer "
5826 "from disjoint %s address space pointer",
5827 c_addr_space_name (as_to
),
5828 c_addr_space_name (as_from
));
5832 /* Warn about possible alignment problems. */
5833 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
5834 && TREE_CODE (type
) == POINTER_TYPE
5835 && TREE_CODE (otype
) == POINTER_TYPE
5836 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5837 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5838 /* Don't warn about opaque types, where the actual alignment
5839 restriction is unknown. */
5840 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype
))
5841 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5842 && min_align_of_type (TREE_TYPE (type
))
5843 > min_align_of_type (TREE_TYPE (otype
)))
5844 warning_at (loc
, OPT_Wcast_align
,
5845 "cast increases required alignment of target type");
5847 if (TREE_CODE (type
) == INTEGER_TYPE
5848 && TREE_CODE (otype
) == POINTER_TYPE
5849 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5850 /* Unlike conversion of integers to pointers, where the
5851 warning is disabled for converting constants because
5852 of cases such as SIG_*, warn about converting constant
5853 pointers to integers. In some cases it may cause unwanted
5854 sign extension, and a warning is appropriate. */
5855 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5856 "cast from pointer to integer of different size");
5858 if (TREE_CODE (value
) == CALL_EXPR
5859 && TREE_CODE (type
) != TREE_CODE (otype
))
5860 warning_at (loc
, OPT_Wbad_function_cast
,
5861 "cast from function call of type %qT "
5862 "to non-matching type %qT", otype
, type
);
5864 if (TREE_CODE (type
) == POINTER_TYPE
5865 && TREE_CODE (otype
) == INTEGER_TYPE
5866 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5867 /* Don't warn about converting any constant. */
5868 && !TREE_CONSTANT (value
))
5870 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5871 "of different size");
5873 if (warn_strict_aliasing
<= 2)
5874 strict_aliasing_warning (EXPR_LOCATION (value
), type
, expr
);
5876 /* If pedantic, warn for conversions between function and object
5877 pointer types, except for converting a null pointer constant
5878 to function pointer type. */
5880 && TREE_CODE (type
) == POINTER_TYPE
5881 && TREE_CODE (otype
) == POINTER_TYPE
5882 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5883 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5884 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5885 "conversion of function pointer to object pointer type");
5888 && TREE_CODE (type
) == POINTER_TYPE
5889 && TREE_CODE (otype
) == POINTER_TYPE
5890 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5891 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5892 && !null_pointer_constant_p (value
))
5893 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5894 "conversion of object pointer to function pointer type");
5896 if (TREE_CODE (type
) == POINTER_TYPE
5897 && TREE_CODE (otype
) == POINTER_TYPE
5898 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5899 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5900 && !c_safe_function_type_cast_p (TREE_TYPE (type
),
5902 warning_at (loc
, OPT_Wcast_function_type
,
5903 "cast between incompatible function types"
5904 " from %qT to %qT", otype
, type
);
5907 value
= convert (type
, value
);
5909 /* Ignore any integer overflow caused by the cast. */
5910 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5912 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5914 if (!TREE_OVERFLOW (value
))
5916 /* Avoid clobbering a shared constant. */
5917 value
= copy_node (value
);
5918 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5921 else if (TREE_OVERFLOW (value
))
5922 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5923 value
= wide_int_to_tree (TREE_TYPE (value
), wi::to_wide (value
));
5927 /* Don't let a cast be an lvalue. */
5928 if (lvalue_p (value
))
5929 value
= non_lvalue_loc (loc
, value
);
5931 /* Don't allow the results of casting to floating-point or complex
5932 types be confused with actual constants, or casts involving
5933 integer and pointer types other than direct integer-to-integer
5934 and integer-to-pointer be confused with integer constant
5935 expressions and null pointer constants. */
5936 if (TREE_CODE (value
) == REAL_CST
5937 || TREE_CODE (value
) == COMPLEX_CST
5938 || (TREE_CODE (value
) == INTEGER_CST
5939 && !((TREE_CODE (expr
) == INTEGER_CST
5940 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5941 || TREE_CODE (expr
) == REAL_CST
5942 || TREE_CODE (expr
) == COMPLEX_CST
)))
5943 value
= build1 (NOP_EXPR
, type
, value
);
5945 protected_set_expr_location (value
, loc
);
5949 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5950 location of the open paren of the cast, or the position of the cast
5953 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5956 tree type_expr
= NULL_TREE
;
5957 bool type_expr_const
= true;
5959 int saved_wsp
= warn_strict_prototypes
;
5961 /* This avoids warnings about unprototyped casts on
5962 integers. E.g. "#define SIG_DFL (void(*)())0". */
5963 if (TREE_CODE (expr
) == INTEGER_CST
)
5964 warn_strict_prototypes
= 0;
5965 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5966 warn_strict_prototypes
= saved_wsp
;
5968 if (TREE_CODE (expr
) == ADDR_EXPR
&& !VOID_TYPE_P (type
)
5969 && reject_gcc_builtin (expr
))
5970 return error_mark_node
;
5972 ret
= build_c_cast (loc
, type
, expr
);
5975 bool inner_expr_const
= true;
5976 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5977 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5978 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5979 && inner_expr_const
);
5980 SET_EXPR_LOCATION (ret
, loc
);
5983 if (!EXPR_HAS_LOCATION (ret
))
5984 protected_set_expr_location (ret
, loc
);
5986 /* C++ does not permits types to be defined in a cast, but it
5987 allows references to incomplete types. */
5988 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5989 warning_at (loc
, OPT_Wc___compat
,
5990 "defining a type in a cast is invalid in C++");
5995 /* Build an assignment expression of lvalue LHS from value RHS.
5996 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5997 may differ from TREE_TYPE (LHS) for an enum bitfield.
5998 MODIFYCODE is the code for a binary operator that we use
5999 to combine the old value of LHS with RHS to get the new value.
6000 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6001 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6002 which may differ from TREE_TYPE (RHS) for an enum value.
6004 LOCATION is the location of the MODIFYCODE operator.
6005 RHS_LOC is the location of the RHS. */
6008 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
6009 enum tree_code modifycode
,
6010 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
6014 tree rhseval
= NULL_TREE
;
6015 tree lhstype
= TREE_TYPE (lhs
);
6016 tree olhstype
= lhstype
;
6020 /* Types that aren't fully specified cannot be used in assignments. */
6021 lhs
= require_complete_type (location
, lhs
);
6023 /* Avoid duplicate error messages from operands that had errors. */
6024 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
6025 return error_mark_node
;
6027 /* Ensure an error for assigning a non-lvalue array to an array in
6029 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
6031 error_at (location
, "assignment to expression with array type");
6032 return error_mark_node
;
6035 /* For ObjC properties, defer this check. */
6036 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
6037 return error_mark_node
;
6039 is_atomic_op
= really_atomic_lvalue (lhs
);
6043 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
6045 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
6046 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
6048 if (inner
== error_mark_node
)
6049 return error_mark_node
;
6050 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
6051 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
6052 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
6053 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
6054 protected_set_expr_location (result
, location
);
6058 /* If a binary op has been requested, combine the old LHS value with the RHS
6059 producing the value we should actually store into the LHS. */
6061 if (modifycode
!= NOP_EXPR
)
6063 lhs
= c_fully_fold (lhs
, false, NULL
, true);
6064 lhs
= stabilize_reference (lhs
);
6066 /* Construct the RHS for any non-atomic compound assignemnt. */
6069 /* If in LHS op= RHS the RHS has side-effects, ensure they
6070 are preevaluated before the rest of the assignment expression's
6071 side-effects, because RHS could contain e.g. function calls
6073 if (TREE_SIDE_EFFECTS (rhs
))
6075 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6076 newrhs
= save_expr (TREE_OPERAND (rhs
, 0));
6078 newrhs
= save_expr (rhs
);
6080 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6081 newrhs
= build1 (EXCESS_PRECISION_EXPR
, TREE_TYPE (rhs
),
6084 newrhs
= build_binary_op (location
,
6085 modifycode
, lhs
, newrhs
, true);
6087 /* The original type of the right hand side is no longer
6089 rhs_origtype
= NULL_TREE
;
6093 if (c_dialect_objc ())
6095 /* Check if we are modifying an Objective-C property reference;
6096 if so, we need to generate setter calls. */
6097 if (TREE_CODE (newrhs
) == EXCESS_PRECISION_EXPR
)
6098 result
= objc_maybe_build_modify_expr (lhs
, TREE_OPERAND (newrhs
, 0));
6100 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
6104 /* Else, do the check that we postponed for Objective-C. */
6105 if (!lvalue_or_else (location
, lhs
, lv_assign
))
6106 return error_mark_node
;
6109 /* Give an error for storing in something that is 'const'. */
6111 if (TYPE_READONLY (lhstype
)
6112 || (RECORD_OR_UNION_TYPE_P (lhstype
)
6113 && C_TYPE_FIELDS_READONLY (lhstype
)))
6115 readonly_error (location
, lhs
, lv_assign
);
6116 return error_mark_node
;
6118 else if (TREE_READONLY (lhs
))
6119 readonly_warning (lhs
, lv_assign
);
6121 /* If storing into a structure or union member,
6122 it has probably been given type `int'.
6123 Compute the type that would go with
6124 the actual amount of storage the member occupies. */
6126 if (TREE_CODE (lhs
) == COMPONENT_REF
6127 && (TREE_CODE (lhstype
) == INTEGER_TYPE
6128 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
6129 || TREE_CODE (lhstype
) == REAL_TYPE
6130 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
6131 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
6133 /* If storing in a field that is in actuality a short or narrower than one,
6134 we must store in the field in its actual type. */
6136 if (lhstype
!= TREE_TYPE (lhs
))
6138 lhs
= copy_node (lhs
);
6139 TREE_TYPE (lhs
) = lhstype
;
6142 /* Issue -Wc++-compat warnings about an assignment to an enum type
6143 when LHS does not have its original type. This happens for,
6144 e.g., an enum bitfield in a struct. */
6146 && lhs_origtype
!= NULL_TREE
6147 && lhs_origtype
!= lhstype
6148 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
6150 tree checktype
= (rhs_origtype
!= NULL_TREE
6153 if (checktype
!= error_mark_node
6154 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
6155 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
6156 warning_at (location
, OPT_Wc___compat
,
6157 "enum conversion in assignment is invalid in C++");
6160 /* If the lhs is atomic, remove that qualifier. */
6163 lhstype
= build_qualified_type (lhstype
,
6164 (TYPE_QUALS (lhstype
)
6165 & ~TYPE_QUAL_ATOMIC
));
6166 olhstype
= build_qualified_type (olhstype
,
6167 (TYPE_QUALS (lhstype
)
6168 & ~TYPE_QUAL_ATOMIC
));
6171 /* Convert new value to destination type. Fold it first, then
6172 restore any excess precision information, for the sake of
6173 conversion warnings. */
6175 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
6177 tree rhs_semantic_type
= NULL_TREE
;
6178 if (TREE_CODE (newrhs
) == EXCESS_PRECISION_EXPR
)
6180 rhs_semantic_type
= TREE_TYPE (newrhs
);
6181 newrhs
= TREE_OPERAND (newrhs
, 0);
6183 npc
= null_pointer_constant_p (newrhs
);
6184 newrhs
= c_fully_fold (newrhs
, false, NULL
);
6185 if (rhs_semantic_type
)
6186 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
6187 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
6188 rhs_origtype
, ic_assign
, npc
,
6189 NULL_TREE
, NULL_TREE
, 0);
6190 if (TREE_CODE (newrhs
) == ERROR_MARK
)
6191 return error_mark_node
;
6194 /* Emit ObjC write barrier, if necessary. */
6195 if (c_dialect_objc () && flag_objc_gc
)
6197 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
6200 protected_set_expr_location (result
, location
);
6205 /* Scan operands. */
6208 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
6211 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
6212 TREE_SIDE_EFFECTS (result
) = 1;
6213 protected_set_expr_location (result
, location
);
6216 /* If we got the LHS in a different type for storing in,
6217 convert the result back to the nominal type of LHS
6218 so that the value we return always has the same type
6219 as the LHS argument. */
6221 if (olhstype
== TREE_TYPE (result
))
6224 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
6225 rhs_origtype
, ic_assign
, false, NULL_TREE
,
6227 protected_set_expr_location (result
, location
);
6231 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
6235 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6236 This is used to implement -fplan9-extensions. */
6239 find_anonymous_field_with_type (tree struct_type
, tree type
)
6244 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type
));
6246 for (field
= TYPE_FIELDS (struct_type
);
6248 field
= TREE_CHAIN (field
))
6250 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
6251 ? c_build_qualified_type (TREE_TYPE (field
),
6253 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
6254 if (DECL_NAME (field
) == NULL
6255 && comptypes (type
, fieldtype
))
6261 else if (DECL_NAME (field
) == NULL
6262 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
6263 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
6273 /* RHS is an expression whose type is pointer to struct. If there is
6274 an anonymous field in RHS with type TYPE, then return a pointer to
6275 that field in RHS. This is used with -fplan9-extensions. This
6276 returns NULL if no conversion could be found. */
6279 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
6281 tree rhs_struct_type
, lhs_main_type
;
6282 tree field
, found_field
;
6283 bool found_sub_field
;
6286 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
6287 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
6288 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type
));
6290 gcc_assert (POINTER_TYPE_P (type
));
6291 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
6292 ? c_build_qualified_type (TREE_TYPE (type
),
6294 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6296 found_field
= NULL_TREE
;
6297 found_sub_field
= false;
6298 for (field
= TYPE_FIELDS (rhs_struct_type
);
6300 field
= TREE_CHAIN (field
))
6302 if (DECL_NAME (field
) != NULL_TREE
6303 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
6305 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
6306 ? c_build_qualified_type (TREE_TYPE (field
),
6308 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
6309 if (comptypes (lhs_main_type
, fieldtype
))
6311 if (found_field
!= NULL_TREE
)
6313 found_field
= field
;
6315 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
6318 if (found_field
!= NULL_TREE
)
6320 found_field
= field
;
6321 found_sub_field
= true;
6325 if (found_field
== NULL_TREE
)
6328 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
6329 build_fold_indirect_ref (rhs
), found_field
,
6331 ret
= build_fold_addr_expr_loc (location
, ret
);
6333 if (found_sub_field
)
6335 ret
= convert_to_anonymous_field (location
, type
, ret
);
6336 gcc_assert (ret
!= NULL_TREE
);
6342 /* Issue an error message for a bad initializer component.
6343 GMSGID identifies the message.
6344 The component name is taken from the spelling stack. */
6346 static void ATTRIBUTE_GCC_DIAG (2,0)
6347 error_init (location_t loc
, const char *gmsgid
, ...)
6351 auto_diagnostic_group d
;
6353 /* The gmsgid may be a format string with %< and %>. */
6355 va_start (ap
, gmsgid
);
6356 bool warned
= emit_diagnostic_valist (DK_ERROR
, loc
, -1, gmsgid
, &ap
);
6359 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6360 if (*ofwhat
&& warned
)
6361 inform (loc
, "(near initialization for %qs)", ofwhat
);
6364 /* Issue a pedantic warning for a bad initializer component. OPT is
6365 the option OPT_* (from options.h) controlling this warning or 0 if
6366 it is unconditionally given. GMSGID identifies the message. The
6367 component name is taken from the spelling stack. */
6369 static void ATTRIBUTE_GCC_DIAG (3,0)
6370 pedwarn_init (location_t loc
, int opt
, const char *gmsgid
, ...)
6372 /* Use the location where a macro was expanded rather than where
6373 it was defined to make sure macros defined in system headers
6374 but used incorrectly elsewhere are diagnosed. */
6375 location_t exploc
= expansion_point_location_if_in_system_header (loc
);
6376 auto_diagnostic_group d
;
6378 va_start (ap
, gmsgid
);
6379 bool warned
= emit_diagnostic_valist (DK_PEDWARN
, exploc
, opt
, gmsgid
, &ap
);
6381 char *ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6382 if (*ofwhat
&& warned
)
6383 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6386 /* Issue a warning for a bad initializer component.
6388 OPT is the OPT_W* value corresponding to the warning option that
6389 controls this warning. GMSGID identifies the message. The
6390 component name is taken from the spelling stack. */
6393 warning_init (location_t loc
, int opt
, const char *gmsgid
)
6398 auto_diagnostic_group d
;
6400 /* Use the location where a macro was expanded rather than where
6401 it was defined to make sure macros defined in system headers
6402 but used incorrectly elsewhere are diagnosed. */
6403 location_t exploc
= expansion_point_location_if_in_system_header (loc
);
6405 /* The gmsgid may be a format string with %< and %>. */
6406 warned
= warning_at (exploc
, opt
, gmsgid
);
6407 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6408 if (*ofwhat
&& warned
)
6409 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6412 /* If TYPE is an array type and EXPR is a parenthesized string
6413 constant, warn if pedantic that EXPR is being used to initialize an
6414 object of type TYPE. */
6417 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
6420 && TREE_CODE (type
) == ARRAY_TYPE
6421 && TREE_CODE (expr
.value
) == STRING_CST
6422 && expr
.original_code
!= STRING_CST
)
6423 pedwarn_init (loc
, OPT_Wpedantic
,
6424 "array initialized from parenthesized string constant");
6427 /* Attempt to locate the parameter with the given index within FNDECL,
6428 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6431 get_fndecl_argument_location (tree fndecl
, int argnum
)
6436 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6437 for (i
= 0, param
= DECL_ARGUMENTS (fndecl
);
6438 i
< argnum
&& param
;
6439 i
++, param
= TREE_CHAIN (param
))
6442 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6443 return DECL_SOURCE_LOCATION (FNDECL). */
6445 return DECL_SOURCE_LOCATION (fndecl
);
6447 return DECL_SOURCE_LOCATION (param
);
6450 /* Issue a note about a mismatching argument for parameter PARMNUM
6451 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6452 Attempt to issue the note at the pertinent parameter of the decl;
6453 failing that issue it at the location of FUNDECL; failing that
6454 issue it at PLOC. */
6457 inform_for_arg (tree fundecl
, location_t ploc
, int parmnum
,
6458 tree expected_type
, tree actual_type
)
6461 if (fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6462 loc
= get_fndecl_argument_location (fundecl
, parmnum
- 1);
6467 "expected %qT but argument is of type %qT",
6468 expected_type
, actual_type
);
6471 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6472 function FUNDECL declared without prototype to parameter PARMNUM of
6473 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6476 maybe_warn_builtin_no_proto_arg (location_t loc
, tree fundecl
, int parmnum
,
6477 tree parmtype
, tree argtype
)
6479 tree_code parmcode
= TREE_CODE (parmtype
);
6480 tree_code argcode
= TREE_CODE (argtype
);
6481 tree promoted
= c_type_promotes_to (argtype
);
6483 /* Avoid warning for enum arguments that promote to an integer type
6484 of the same size/mode. */
6485 if (parmcode
== INTEGER_TYPE
6486 && argcode
== ENUMERAL_TYPE
6487 && TYPE_MODE (parmtype
) == TYPE_MODE (argtype
))
6490 if ((parmcode
== argcode
6491 || (parmcode
== INTEGER_TYPE
6492 && argcode
== ENUMERAL_TYPE
))
6493 && TYPE_MAIN_VARIANT (parmtype
) == TYPE_MAIN_VARIANT (promoted
))
6496 /* This diagnoses even signed/unsigned mismatches. Those might be
6497 safe in many cases but GCC may emit suboptimal code for them so
6498 warning on those cases drives efficiency improvements. */
6499 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
6500 TYPE_MAIN_VARIANT (promoted
) == argtype
6501 ? G_("%qD argument %d type is %qT where %qT is expected "
6502 "in a call to built-in function declared without "
6504 : G_("%qD argument %d promotes to %qT where %qT is expected "
6505 "in a call to built-in function declared without "
6507 fundecl
, parmnum
, promoted
, parmtype
))
6508 inform (DECL_SOURCE_LOCATION (fundecl
),
6509 "built-in %qD declared here",
6513 /* Convert value RHS to type TYPE as preparation for an assignment to
6514 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6515 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6516 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6517 constant before any folding.
6518 The real work of conversion is done by `convert'.
6519 The purpose of this function is to generate error messages
6520 for assignments that are not allowed in C.
6521 ERRTYPE says whether it is argument passing, assignment,
6522 initialization or return.
6524 In the following example, '~' denotes where EXPR_LOC and '^' where
6527 f (var); [ic_argpass]
6529 x = var; [ic_assign]
6531 int x = var; [ic_init]
6533 return x; [ic_return]
6536 FUNCTION is a tree for the function being called.
6537 PARMNUM is the number of the argument, for printing in error messages.
6538 WARNOPT may be set to a warning option to issue the corresponding warning
6539 rather than an error for invalid conversions. Used for calls to built-in
6540 functions declared without a prototype. */
6543 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
6544 tree rhs
, tree origtype
, enum impl_conv errtype
,
6545 bool null_pointer_constant
, tree fundecl
,
6546 tree function
, int parmnum
, int warnopt
/* = 0 */)
6548 enum tree_code codel
= TREE_CODE (type
);
6549 tree orig_rhs
= rhs
;
6551 enum tree_code coder
;
6552 tree rname
= NULL_TREE
;
6553 bool objc_ok
= false;
6555 /* Use the expansion point location to handle cases such as user's
6556 function returning a wrong-type macro defined in a system header. */
6557 location
= expansion_point_location_if_in_system_header (location
);
6559 if (errtype
== ic_argpass
)
6562 /* Change pointer to function to the function itself for
6564 if (TREE_CODE (function
) == ADDR_EXPR
6565 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
6566 function
= TREE_OPERAND (function
, 0);
6568 /* Handle an ObjC selector specially for diagnostics. */
6569 selector
= objc_message_selector ();
6571 if (selector
&& parmnum
> 2)
6578 /* This macro is used to emit diagnostics to ensure that all format
6579 strings are complete sentences, visible to gettext and checked at
6581 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6587 auto_diagnostic_group d; \
6588 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6589 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6593 pedwarn (LOCATION, OPT, AS); \
6596 pedwarn_init (LOCATION, OPT, IN); \
6599 pedwarn (LOCATION, OPT, RE); \
6602 gcc_unreachable (); \
6606 /* This macro is used to emit diagnostics to ensure that all format
6607 strings are complete sentences, visible to gettext and checked at
6608 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6609 extra parameter to enumerate qualifiers. */
6610 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6616 auto_diagnostic_group d; \
6617 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6618 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6622 pedwarn (LOCATION, OPT, AS, QUALS); \
6625 pedwarn (LOCATION, OPT, IN, QUALS); \
6628 pedwarn (LOCATION, OPT, RE, QUALS); \
6631 gcc_unreachable (); \
6635 /* This macro is used to emit diagnostics to ensure that all format
6636 strings are complete sentences, visible to gettext and checked at
6637 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6638 warning_at instead of pedwarn. */
6639 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6645 auto_diagnostic_group d; \
6646 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6647 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6651 warning_at (LOCATION, OPT, AS, QUALS); \
6654 warning_at (LOCATION, OPT, IN, QUALS); \
6657 warning_at (LOCATION, OPT, RE, QUALS); \
6660 gcc_unreachable (); \
6664 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6665 rhs
= TREE_OPERAND (rhs
, 0);
6667 rhstype
= TREE_TYPE (rhs
);
6668 coder
= TREE_CODE (rhstype
);
6670 if (coder
== ERROR_MARK
)
6671 return error_mark_node
;
6673 if (c_dialect_objc ())
6696 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
6699 if (warn_cxx_compat
)
6701 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
6702 if (checktype
!= error_mark_node
6703 && TREE_CODE (type
) == ENUMERAL_TYPE
6704 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
6708 if (pedwarn (expr_loc
, OPT_Wc___compat
, "enum conversion when "
6709 "passing argument %d of %qE is invalid in C++",
6711 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6712 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6713 "expected %qT but argument is of type %qT",
6717 pedwarn (location
, OPT_Wc___compat
, "enum conversion from %qT to "
6718 "%qT in assignment is invalid in C++", rhstype
, type
);
6721 pedwarn_init (location
, OPT_Wc___compat
, "enum conversion from "
6722 "%qT to %qT in initialization is invalid in C++",
6726 pedwarn (location
, OPT_Wc___compat
, "enum conversion from %qT to "
6727 "%qT in return is invalid in C++", rhstype
, type
);
6734 if (warn_enum_conversion
)
6736 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
6737 if (checktype
!= error_mark_node
6738 && TREE_CODE (checktype
) == ENUMERAL_TYPE
6739 && TREE_CODE (type
) == ENUMERAL_TYPE
6740 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
6742 gcc_rich_location
loc (location
);
6743 warning_at (&loc
, OPT_Wenum_conversion
,
6744 "implicit conversion from %qT to %qT",
6749 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
6751 warn_for_address_or_pointer_of_packed_member (type
, orig_rhs
);
6755 if (coder
== VOID_TYPE
)
6757 /* Except for passing an argument to an unprototyped function,
6758 this is a constraint violation. When passing an argument to
6759 an unprototyped function, it is compile-time undefined;
6760 making it a constraint in that case was rejected in
6762 const char msg
[] = "void value not ignored as it ought to be";
6764 warning_at (location
, warnopt
, msg
);
6766 error_at (location
, msg
);
6767 return error_mark_node
;
6769 rhs
= require_complete_type (location
, rhs
);
6770 if (rhs
== error_mark_node
)
6771 return error_mark_node
;
6773 if (coder
== POINTER_TYPE
&& reject_gcc_builtin (rhs
))
6774 return error_mark_node
;
6776 /* A non-reference type can convert to a reference. This handles
6777 va_start, va_copy and possibly port built-ins. */
6778 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
6780 if (!lvalue_p (rhs
))
6782 const char msg
[] = "cannot pass rvalue to reference parameter";
6784 warning_at (location
, warnopt
, msg
);
6786 error_at (location
, msg
);
6787 return error_mark_node
;
6789 if (!c_mark_addressable (rhs
))
6790 return error_mark_node
;
6791 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
6792 SET_EXPR_LOCATION (rhs
, location
);
6794 rhs
= convert_for_assignment (location
, expr_loc
,
6795 build_pointer_type (TREE_TYPE (type
)),
6796 rhs
, origtype
, errtype
,
6797 null_pointer_constant
, fundecl
, function
,
6799 if (rhs
== error_mark_node
)
6800 return error_mark_node
;
6802 rhs
= build1 (NOP_EXPR
, type
, rhs
);
6803 SET_EXPR_LOCATION (rhs
, location
);
6806 /* Some types can interconvert without explicit casts. */
6807 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
6808 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
6809 return convert (type
, rhs
);
6810 /* Arithmetic types all interconvert, and enum is treated like int. */
6811 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
6812 || codel
== FIXED_POINT_TYPE
6813 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
6814 || codel
== BOOLEAN_TYPE
)
6815 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
6816 || coder
== FIXED_POINT_TYPE
6817 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
6818 || coder
== BOOLEAN_TYPE
))
6820 if (warnopt
&& errtype
== ic_argpass
)
6821 maybe_warn_builtin_no_proto_arg (expr_loc
, fundecl
, parmnum
, type
,
6824 bool save
= in_late_binary_op
;
6825 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
6826 || (coder
== REAL_TYPE
6827 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
6828 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
6829 in_late_binary_op
= true;
6830 tree ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6831 ? expr_loc
: location
, type
, orig_rhs
);
6832 in_late_binary_op
= save
;
6836 /* Aggregates in different TUs might need conversion. */
6837 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
6839 && comptypes (type
, rhstype
))
6840 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6841 ? expr_loc
: location
, type
, rhs
);
6843 /* Conversion to a transparent union or record from its member types.
6844 This applies only to function arguments. */
6845 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
6846 && TYPE_TRANSPARENT_AGGR (type
))
6847 && errtype
== ic_argpass
)
6849 tree memb
, marginal_memb
= NULL_TREE
;
6851 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
6853 tree memb_type
= TREE_TYPE (memb
);
6855 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
6856 TYPE_MAIN_VARIANT (rhstype
)))
6859 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
6862 if (coder
== POINTER_TYPE
)
6864 tree ttl
= TREE_TYPE (memb_type
);
6865 tree ttr
= TREE_TYPE (rhstype
);
6867 /* Any non-function converts to a [const][volatile] void *
6868 and vice versa; otherwise, targets must be the same.
6869 Meanwhile, the lhs target must have all the qualifiers of
6871 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6872 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6873 || comp_target_types (location
, memb_type
, rhstype
))
6875 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
6876 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
6877 /* If this type won't generate any warnings, use it. */
6878 if (lquals
== rquals
6879 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
6880 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6881 ? ((lquals
| rquals
) == rquals
)
6882 : ((lquals
| rquals
) == lquals
)))
6885 /* Keep looking for a better type, but remember this one. */
6887 marginal_memb
= memb
;
6891 /* Can convert integer zero to any pointer type. */
6892 if (null_pointer_constant
)
6894 rhs
= null_pointer_node
;
6899 if (memb
|| marginal_memb
)
6903 /* We have only a marginally acceptable member type;
6904 it needs a warning. */
6905 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
6906 tree ttr
= TREE_TYPE (rhstype
);
6908 /* Const and volatile mean something different for function
6909 types, so the usual warnings are not appropriate. */
6910 if (TREE_CODE (ttr
) == FUNCTION_TYPE
6911 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6913 /* Because const and volatile on functions are
6914 restrictions that say the function will not do
6915 certain things, it is okay to use a const or volatile
6916 function where an ordinary one is wanted, but not
6918 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6919 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6920 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6921 OPT_Wdiscarded_qualifiers
,
6922 G_("passing argument %d of %qE "
6923 "makes %q#v qualified function "
6924 "pointer from unqualified"),
6925 G_("assignment makes %q#v qualified "
6926 "function pointer from "
6928 G_("initialization makes %q#v qualified "
6929 "function pointer from "
6931 G_("return makes %q#v qualified function "
6932 "pointer from unqualified"),
6933 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6935 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
6936 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
6937 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6938 OPT_Wdiscarded_qualifiers
,
6939 G_("passing argument %d of %qE discards "
6940 "%qv qualifier from pointer target type"),
6941 G_("assignment discards %qv qualifier "
6942 "from pointer target type"),
6943 G_("initialization discards %qv qualifier "
6944 "from pointer target type"),
6945 G_("return discards %qv qualifier from "
6946 "pointer target type"),
6947 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6949 memb
= marginal_memb
;
6952 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
6953 pedwarn (location
, OPT_Wpedantic
,
6954 "ISO C prohibits argument conversion to union type");
6956 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
6957 return build_constructor_single (type
, memb
, rhs
);
6961 /* Conversions among pointers */
6962 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
6963 && (coder
== codel
))
6965 /* If RHS refers to a built-in declared without a prototype
6966 BLTIN is the declaration of the built-in with a prototype
6967 and RHSTYPE is set to the actual type of the built-in. */
6969 rhstype
= type_or_builtin_type (rhs
, &bltin
);
6971 tree ttl
= TREE_TYPE (type
);
6972 tree ttr
= TREE_TYPE (rhstype
);
6975 bool is_opaque_pointer
;
6976 int target_cmp
= 0; /* Cache comp_target_types () result. */
6980 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
6981 mvl
= (TYPE_ATOMIC (mvl
)
6982 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
6984 : TYPE_MAIN_VARIANT (mvl
));
6985 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
6986 mvr
= (TYPE_ATOMIC (mvr
)
6987 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
6989 : TYPE_MAIN_VARIANT (mvr
));
6990 /* Opaque pointers are treated like void pointers. */
6991 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
6993 /* The Plan 9 compiler permits a pointer to a struct to be
6994 automatically converted into a pointer to an anonymous field
6995 within the struct. */
6996 if (flag_plan9_extensions
6997 && RECORD_OR_UNION_TYPE_P (mvl
)
6998 && RECORD_OR_UNION_TYPE_P (mvr
)
7001 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
7002 if (new_rhs
!= NULL_TREE
)
7005 rhstype
= TREE_TYPE (rhs
);
7006 coder
= TREE_CODE (rhstype
);
7007 ttr
= TREE_TYPE (rhstype
);
7008 mvr
= TYPE_MAIN_VARIANT (ttr
);
7012 /* C++ does not allow the implicit conversion void* -> T*. However,
7013 for the purpose of reducing the number of false positives, we
7014 tolerate the special case of
7018 where NULL is typically defined in C to be '(void *) 0'. */
7019 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
7020 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
7022 "request for implicit conversion "
7023 "from %qT to %qT not permitted in C++", rhstype
, type
);
7025 /* See if the pointers point to incompatible address spaces. */
7026 asl
= TYPE_ADDR_SPACE (ttl
);
7027 asr
= TYPE_ADDR_SPACE (ttr
);
7028 if (!null_pointer_constant_p (rhs
)
7029 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
7035 const char msg
[] = G_("passing argument %d of %qE from "
7036 "pointer to non-enclosed address space");
7038 warning_at (expr_loc
, warnopt
, msg
, parmnum
, rname
);
7040 error_at (expr_loc
, msg
, parmnum
, rname
);
7045 const char msg
[] = G_("assignment from pointer to "
7046 "non-enclosed address space");
7048 warning_at (location
, warnopt
, msg
);
7050 error_at (location
, msg
);
7055 const char msg
[] = G_("initialization from pointer to "
7056 "non-enclosed address space");
7058 warning_at (location
, warnopt
, msg
);
7060 error_at (location
, msg
);
7065 const char msg
[] = G_("return from pointer to "
7066 "non-enclosed address space");
7068 warning_at (location
, warnopt
, msg
);
7070 error_at (location
, msg
);
7076 return error_mark_node
;
7079 /* Check if the right-hand side has a format attribute but the
7080 left-hand side doesn't. */
7081 if (warn_suggest_attribute_format
7082 && check_missing_format_attribute (type
, rhstype
))
7087 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
7088 "argument %d of %qE might be "
7089 "a candidate for a format attribute",
7093 warning_at (location
, OPT_Wsuggest_attribute_format
,
7094 "assignment left-hand side might be "
7095 "a candidate for a format attribute");
7098 warning_at (location
, OPT_Wsuggest_attribute_format
,
7099 "initialization left-hand side might be "
7100 "a candidate for a format attribute");
7103 warning_at (location
, OPT_Wsuggest_attribute_format
,
7104 "return type might be "
7105 "a candidate for a format attribute");
7112 /* Any non-function converts to a [const][volatile] void *
7113 and vice versa; otherwise, targets must be the same.
7114 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7115 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
7116 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
7117 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
7118 || is_opaque_pointer
7119 || ((c_common_unsigned_type (mvl
)
7120 == c_common_unsigned_type (mvr
))
7121 && (c_common_signed_type (mvl
)
7122 == c_common_signed_type (mvr
))
7123 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
7125 /* Warn about loss of qualifers from pointers to arrays with
7126 qualifiers on the element type. */
7127 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
7129 ttr
= strip_array_types (ttr
);
7130 ttl
= strip_array_types (ttl
);
7132 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
7133 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
7134 WARNING_FOR_QUALIFIERS (location
, expr_loc
,
7135 OPT_Wdiscarded_array_qualifiers
,
7136 G_("passing argument %d of %qE discards "
7137 "%qv qualifier from pointer target type"),
7138 G_("assignment discards %qv qualifier "
7139 "from pointer target type"),
7140 G_("initialization discards %qv qualifier "
7141 "from pointer target type"),
7142 G_("return discards %qv qualifier from "
7143 "pointer target type"),
7144 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
7147 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
7150 && !null_pointer_constant
7151 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
7152 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
7153 G_("ISO C forbids passing argument %d of "
7154 "%qE between function pointer "
7156 G_("ISO C forbids assignment between "
7157 "function pointer and %<void *%>"),
7158 G_("ISO C forbids initialization between "
7159 "function pointer and %<void *%>"),
7160 G_("ISO C forbids return between function "
7161 "pointer and %<void *%>"));
7162 /* Const and volatile mean something different for function types,
7163 so the usual warnings are not appropriate. */
7164 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
7165 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
7167 /* Don't warn about loss of qualifier for conversions from
7168 qualified void* to pointers to arrays with corresponding
7169 qualifier on the element type. */
7171 ttl
= strip_array_types (ttl
);
7173 /* Assignments between atomic and non-atomic objects are OK. */
7174 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
7175 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
7177 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
7178 OPT_Wdiscarded_qualifiers
,
7179 G_("passing argument %d of %qE discards "
7180 "%qv qualifier from pointer target type"),
7181 G_("assignment discards %qv qualifier "
7182 "from pointer target type"),
7183 G_("initialization discards %qv qualifier "
7184 "from pointer target type"),
7185 G_("return discards %qv qualifier from "
7186 "pointer target type"),
7187 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
7189 /* If this is not a case of ignoring a mismatch in signedness,
7191 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
7194 /* If there is a mismatch, do warn. */
7195 else if (warn_pointer_sign
)
7200 auto_diagnostic_group d
;
7201 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7202 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7203 if (pedwarn (&richloc
, OPT_Wpointer_sign
,
7204 "pointer targets in passing argument %d of "
7205 "%qE differ in signedness", parmnum
, rname
))
7206 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
,
7211 pedwarn (location
, OPT_Wpointer_sign
,
7212 "pointer targets in assignment from %qT to %qT "
7213 "differ in signedness", rhstype
, type
);
7216 pedwarn_init (location
, OPT_Wpointer_sign
,
7217 "pointer targets in initialization of %qT "
7218 "from %qT differ in signedness", type
,
7222 pedwarn (location
, OPT_Wpointer_sign
, "pointer targets in "
7223 "returning %qT from a function with return type "
7224 "%qT differ in signedness", rhstype
, type
);
7230 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
7231 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
7233 /* Because const and volatile on functions are restrictions
7234 that say the function will not do certain things,
7235 it is okay to use a const or volatile function
7236 where an ordinary one is wanted, but not vice-versa. */
7237 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
7238 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
7239 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
7240 OPT_Wdiscarded_qualifiers
,
7241 G_("passing argument %d of %qE makes "
7242 "%q#v qualified function pointer "
7243 "from unqualified"),
7244 G_("assignment makes %q#v qualified function "
7245 "pointer from unqualified"),
7246 G_("initialization makes %q#v qualified "
7247 "function pointer from unqualified"),
7248 G_("return makes %q#v qualified function "
7249 "pointer from unqualified"),
7250 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
7253 /* Avoid warning about the volatile ObjC EH puts on decls. */
7260 auto_diagnostic_group d
;
7261 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7262 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7263 if (pedwarn (&richloc
, OPT_Wincompatible_pointer_types
,
7264 "passing argument %d of %qE from incompatible "
7265 "pointer type", parmnum
, rname
))
7266 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7271 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7272 "assignment to %qT from pointer to "
7273 "%qD with incompatible type %qT",
7274 type
, bltin
, rhstype
);
7276 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7277 "assignment to %qT from incompatible pointer type %qT",
7282 pedwarn_init (location
, OPT_Wincompatible_pointer_types
,
7283 "initialization of %qT from pointer to "
7284 "%qD with incompatible type %qT",
7285 type
, bltin
, rhstype
);
7287 pedwarn_init (location
, OPT_Wincompatible_pointer_types
,
7288 "initialization of %qT from incompatible "
7294 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7295 "returning pointer to %qD of type %qT from "
7296 "a function with incompatible type %qT",
7297 bltin
, rhstype
, type
);
7299 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7300 "returning %qT from a function with incompatible "
7301 "return type %qT", rhstype
, type
);
7308 /* If RHS isn't an address, check pointer or array of packed
7310 warn_for_address_or_pointer_of_packed_member (type
, orig_rhs
);
7312 return convert (type
, rhs
);
7314 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
7316 /* ??? This should not be an error when inlining calls to
7317 unprototyped functions. */
7318 const char msg
[] = "invalid use of non-lvalue array";
7320 warning_at (location
, warnopt
, msg
);
7322 error_at (location
, msg
);
7323 return error_mark_node
;
7325 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
7327 /* An explicit constant 0 can convert to a pointer,
7328 or one that results from arithmetic, even including
7329 a cast to integer type. */
7330 if (!null_pointer_constant
)
7335 auto_diagnostic_group d
;
7336 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7337 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7338 if (pedwarn (&richloc
, OPT_Wint_conversion
,
7339 "passing argument %d of %qE makes pointer from "
7340 "integer without a cast", parmnum
, rname
))
7341 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7345 pedwarn (location
, OPT_Wint_conversion
,
7346 "assignment to %qT from %qT makes pointer from integer "
7347 "without a cast", type
, rhstype
);
7350 pedwarn_init (location
, OPT_Wint_conversion
,
7351 "initialization of %qT from %qT makes pointer from "
7352 "integer without a cast", type
, rhstype
);
7355 pedwarn (location
, OPT_Wint_conversion
, "returning %qT from a "
7356 "function with return type %qT makes pointer from "
7357 "integer without a cast", rhstype
, type
);
7363 return convert (type
, rhs
);
7365 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
7371 auto_diagnostic_group d
;
7372 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7373 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7374 if (pedwarn (&richloc
, OPT_Wint_conversion
,
7375 "passing argument %d of %qE makes integer from "
7376 "pointer without a cast", parmnum
, rname
))
7377 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7381 pedwarn (location
, OPT_Wint_conversion
,
7382 "assignment to %qT from %qT makes integer from pointer "
7383 "without a cast", type
, rhstype
);
7386 pedwarn_init (location
, OPT_Wint_conversion
,
7387 "initialization of %qT from %qT makes integer from "
7388 "pointer without a cast", type
, rhstype
);
7391 pedwarn (location
, OPT_Wint_conversion
, "returning %qT from a "
7392 "function with return type %qT makes integer from "
7393 "pointer without a cast", rhstype
, type
);
7399 return convert (type
, rhs
);
7401 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
7404 bool save
= in_late_binary_op
;
7405 in_late_binary_op
= true;
7406 ret
= convert (type
, rhs
);
7407 in_late_binary_op
= save
;
7415 auto_diagnostic_group d
;
7416 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7417 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7418 const char msg
[] = G_("incompatible type for argument %d of %qE");
7420 warning_at (expr_loc
, warnopt
, msg
, parmnum
, rname
);
7422 error_at (&richloc
, msg
, parmnum
, rname
);
7423 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7429 = G_("incompatible types when assigning to type %qT from type %qT");
7431 warning_at (expr_loc
, 0, msg
, type
, rhstype
);
7433 error_at (expr_loc
, msg
, type
, rhstype
);
7439 = G_("incompatible types when initializing type %qT using type %qT");
7441 warning_at (location
, 0, msg
, type
, rhstype
);
7443 error_at (location
, msg
, type
, rhstype
);
7449 = G_("incompatible types when returning type %qT but %qT was expected");
7451 warning_at (location
, 0, msg
, rhstype
, type
);
7453 error_at (location
, msg
, rhstype
, type
);
7460 return error_mark_node
;
7463 /* If VALUE is a compound expr all of whose expressions are constant, then
7464 return its value. Otherwise, return error_mark_node.
7466 This is for handling COMPOUND_EXPRs as initializer elements
7467 which is allowed with a warning when -pedantic is specified. */
7470 valid_compound_expr_initializer (tree value
, tree endtype
)
7472 if (TREE_CODE (value
) == COMPOUND_EXPR
)
7474 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
7476 return error_mark_node
;
7477 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
7480 else if (!initializer_constant_valid_p (value
, endtype
))
7481 return error_mark_node
;
7486 /* Perform appropriate conversions on the initial value of a variable,
7487 store it in the declaration DECL,
7488 and print any error messages that are appropriate.
7489 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7490 If the init is invalid, store an ERROR_MARK.
7492 INIT_LOC is the location of the initial value. */
7495 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
7500 /* If variable's type was invalidly declared, just ignore it. */
7502 type
= TREE_TYPE (decl
);
7503 if (TREE_CODE (type
) == ERROR_MARK
)
7506 /* Digest the specified initializer into an expression. */
7509 npc
= null_pointer_constant_p (init
);
7510 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
7511 true, TREE_STATIC (decl
));
7513 /* Store the expression if valid; else report error. */
7515 if (!in_system_header_at (input_location
)
7516 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
7517 warning (OPT_Wtraditional
, "traditional C rejects automatic "
7518 "aggregate initialization");
7520 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
7521 DECL_INITIAL (decl
) = value
;
7523 /* ANSI wants warnings about out-of-range constant initializers. */
7524 STRIP_TYPE_NOPS (value
);
7525 if (TREE_STATIC (decl
))
7526 constant_expression_warning (value
);
7528 /* Check if we need to set array size from compound literal size. */
7529 if (TREE_CODE (type
) == ARRAY_TYPE
7530 && TYPE_DOMAIN (type
) == NULL_TREE
7531 && value
!= error_mark_node
)
7533 tree inside_init
= init
;
7535 STRIP_TYPE_NOPS (inside_init
);
7536 inside_init
= fold (inside_init
);
7538 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7540 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
7542 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
7544 /* For int foo[] = (int [3]){1}; we need to set array size
7545 now since later on array initializer will be just the
7546 brace enclosed list of the compound literal. */
7547 tree etype
= strip_array_types (TREE_TYPE (decl
));
7548 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
7549 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
7551 layout_decl (cldecl
, 0);
7553 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
7559 /* Methods for storing and printing names for error messages. */
7561 /* Implement a spelling stack that allows components of a name to be pushed
7562 and popped. Each element on the stack is this structure. */
7569 unsigned HOST_WIDE_INT i
;
7574 #define SPELLING_STRING 1
7575 #define SPELLING_MEMBER 2
7576 #define SPELLING_BOUNDS 3
7578 static struct spelling
*spelling
; /* Next stack element (unused). */
7579 static struct spelling
*spelling_base
; /* Spelling stack base. */
7580 static int spelling_size
; /* Size of the spelling stack. */
7582 /* Macros to save and restore the spelling stack around push_... functions.
7583 Alternative to SAVE_SPELLING_STACK. */
7585 #define SPELLING_DEPTH() (spelling - spelling_base)
7586 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7588 /* Push an element on the spelling stack with type KIND and assign VALUE
7591 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7593 int depth = SPELLING_DEPTH (); \
7595 if (depth >= spelling_size) \
7597 spelling_size += 10; \
7598 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7600 RESTORE_SPELLING_DEPTH (depth); \
7603 spelling->kind = (KIND); \
7604 spelling->MEMBER = (VALUE); \
7608 /* Push STRING on the stack. Printed literally. */
7611 push_string (const char *string
)
7613 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
7616 /* Push a member name on the stack. Printed as '.' STRING. */
7619 push_member_name (tree decl
)
7621 const char *const string
7623 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
7624 : _("<anonymous>"));
7625 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
7628 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7631 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
7633 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
7636 /* Compute the maximum size in bytes of the printed spelling. */
7639 spelling_length (void)
7644 for (p
= spelling_base
; p
< spelling
; p
++)
7646 if (p
->kind
== SPELLING_BOUNDS
)
7649 size
+= strlen (p
->u
.s
) + 1;
7655 /* Print the spelling to BUFFER and return it. */
7658 print_spelling (char *buffer
)
7663 for (p
= spelling_base
; p
< spelling
; p
++)
7664 if (p
->kind
== SPELLING_BOUNDS
)
7666 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
7672 if (p
->kind
== SPELLING_MEMBER
)
7674 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
7681 /* Digest the parser output INIT as an initializer for type TYPE.
7682 Return a C expression of type TYPE to represent the initial value.
7684 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7686 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7688 If INIT is a string constant, STRICT_STRING is true if it is
7689 unparenthesized or we should not warn here for it being parenthesized.
7690 For other types of INIT, STRICT_STRING is not used.
7692 INIT_LOC is the location of the INIT.
7694 REQUIRE_CONSTANT requests an error if non-constant initializers or
7695 elements are seen. */
7698 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
7699 bool null_pointer_constant
, bool strict_string
,
7700 int require_constant
)
7702 enum tree_code code
= TREE_CODE (type
);
7703 tree inside_init
= init
;
7704 tree semantic_type
= NULL_TREE
;
7705 bool maybe_const
= true;
7707 if (type
== error_mark_node
7709 || error_operand_p (init
))
7710 return error_mark_node
;
7712 STRIP_TYPE_NOPS (inside_init
);
7714 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
7716 semantic_type
= TREE_TYPE (inside_init
);
7717 inside_init
= TREE_OPERAND (inside_init
, 0);
7719 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
7721 /* Initialization of an array of chars from a string constant
7722 optionally enclosed in braces. */
7724 if (code
== ARRAY_TYPE
&& inside_init
7725 && TREE_CODE (inside_init
) == STRING_CST
)
7728 = (TYPE_ATOMIC (TREE_TYPE (type
))
7729 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
7731 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
7732 /* Note that an array could be both an array of character type
7733 and an array of wchar_t if wchar_t is signed char or unsigned
7735 bool char_array
= (typ1
== char_type_node
7736 || typ1
== signed_char_type_node
7737 || typ1
== unsigned_char_type_node
);
7738 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
7739 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
7740 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
7742 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
7745 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
7746 bool incompat_string_cst
= false;
7747 expr
.value
= inside_init
;
7748 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
7749 expr
.original_type
= NULL
;
7750 maybe_warn_string_init (init_loc
, type
, expr
);
7752 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
7753 pedwarn_init (init_loc
, OPT_Wpedantic
,
7754 "initialization of a flexible array member");
7756 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7757 TYPE_MAIN_VARIANT (type
)))
7762 if (typ2
!= char_type_node
)
7763 incompat_string_cst
= true;
7765 else if (!comptypes (typ1
, typ2
))
7766 incompat_string_cst
= true;
7768 if (incompat_string_cst
)
7770 error_init (init_loc
, "cannot initialize array of %qT from "
7771 "a string literal with type array of %qT",
7773 return error_mark_node
;
7776 if (TYPE_DOMAIN (type
) != NULL_TREE
7777 && TYPE_SIZE (type
) != NULL_TREE
7778 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
7780 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
7781 unsigned unit
= TYPE_PRECISION (typ1
) / BITS_PER_UNIT
;
7783 /* Subtract the size of a single (possibly wide) character
7784 because it's ok to ignore the terminating null char
7785 that is counted in the length of the constant. */
7786 if (compare_tree_int (TYPE_SIZE_UNIT (type
), len
- unit
) < 0)
7787 pedwarn_init (init_loc
, 0,
7788 ("initializer-string for array of %qT "
7789 "is too long"), typ1
);
7790 else if (warn_cxx_compat
7791 && compare_tree_int (TYPE_SIZE_UNIT (type
), len
) < 0)
7792 warning_at (init_loc
, OPT_Wc___compat
,
7793 ("initializer-string for array of %qT "
7794 "is too long for C++"), typ1
);
7795 if (compare_tree_int (TYPE_SIZE_UNIT (type
), len
) < 0)
7797 unsigned HOST_WIDE_INT size
7798 = tree_to_uhwi (TYPE_SIZE_UNIT (type
));
7799 const char *p
= TREE_STRING_POINTER (inside_init
);
7801 inside_init
= build_string (size
, p
);
7805 TREE_TYPE (inside_init
) = type
;
7808 else if (INTEGRAL_TYPE_P (typ1
))
7810 error_init (init_loc
, "array of inappropriate type initialized "
7811 "from string constant");
7812 return error_mark_node
;
7816 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7817 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7818 below and handle as a constructor. */
7819 if (code
== VECTOR_TYPE
7820 && VECTOR_TYPE_P (TREE_TYPE (inside_init
))
7821 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
7822 && TREE_CONSTANT (inside_init
))
7824 if (TREE_CODE (inside_init
) == VECTOR_CST
7825 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7826 TYPE_MAIN_VARIANT (type
)))
7829 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
7831 unsigned HOST_WIDE_INT ix
;
7833 bool constant_p
= true;
7835 /* Iterate through elements and check if all constructor
7836 elements are *_CSTs. */
7837 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
7838 if (!CONSTANT_CLASS_P (value
))
7845 return build_vector_from_ctor (type
,
7846 CONSTRUCTOR_ELTS (inside_init
));
7850 if (warn_sequence_point
)
7851 verify_sequence_points (inside_init
);
7853 /* Any type can be initialized
7854 from an expression of the same type, optionally with braces. */
7856 if (inside_init
&& TREE_TYPE (inside_init
) != NULL_TREE
7857 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7858 TYPE_MAIN_VARIANT (type
))
7859 || (code
== ARRAY_TYPE
7860 && comptypes (TREE_TYPE (inside_init
), type
))
7861 || (code
== VECTOR_TYPE
7862 && comptypes (TREE_TYPE (inside_init
), type
))
7863 || (code
== POINTER_TYPE
7864 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
7865 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
7866 TREE_TYPE (type
)))))
7868 if (code
== POINTER_TYPE
)
7870 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
7872 if (TREE_CODE (inside_init
) == STRING_CST
7873 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7874 inside_init
= array_to_pointer_conversion
7875 (init_loc
, inside_init
);
7878 error_init (init_loc
, "invalid use of non-lvalue array");
7879 return error_mark_node
;
7884 if (code
== VECTOR_TYPE
)
7885 /* Although the types are compatible, we may require a
7887 inside_init
= convert (type
, inside_init
);
7889 if (require_constant
7890 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7892 /* As an extension, allow initializing objects with static storage
7893 duration with compound literals (which are then treated just as
7894 the brace enclosed list they contain). Also allow this for
7895 vectors, as we can only assign them with compound literals. */
7896 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
7897 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
7899 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
7900 inside_init
= DECL_INITIAL (decl
);
7903 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
7904 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
7906 error_init (init_loc
, "array initialized from non-constant array "
7908 return error_mark_node
;
7911 /* Compound expressions can only occur here if -Wpedantic or
7912 -pedantic-errors is specified. In the later case, we always want
7913 an error. In the former case, we simply want a warning. */
7914 if (require_constant
&& pedantic
7915 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
7918 = valid_compound_expr_initializer (inside_init
,
7919 TREE_TYPE (inside_init
));
7920 if (inside_init
== error_mark_node
)
7921 error_init (init_loc
, "initializer element is not constant");
7923 pedwarn_init (init_loc
, OPT_Wpedantic
,
7924 "initializer element is not constant");
7925 if (flag_pedantic_errors
)
7926 inside_init
= error_mark_node
;
7928 else if (require_constant
7929 && !initializer_constant_valid_p (inside_init
,
7930 TREE_TYPE (inside_init
)))
7932 error_init (init_loc
, "initializer element is not constant");
7933 inside_init
= error_mark_node
;
7935 else if (require_constant
&& !maybe_const
)
7936 pedwarn_init (init_loc
, OPT_Wpedantic
,
7937 "initializer element is not a constant expression");
7939 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7940 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
7941 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
7942 type
, inside_init
, origtype
,
7943 ic_init
, null_pointer_constant
,
7944 NULL_TREE
, NULL_TREE
, 0);
7948 /* Handle scalar types, including conversions. */
7950 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
7951 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
7952 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
7954 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
7955 && (TREE_CODE (init
) == STRING_CST
7956 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
7957 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
7959 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
7962 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
7963 inside_init
, origtype
, ic_init
,
7964 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
7967 /* Check to see if we have already given an error message. */
7968 if (inside_init
== error_mark_node
)
7970 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
7972 error_init (init_loc
, "initializer element is not constant");
7973 inside_init
= error_mark_node
;
7975 else if (require_constant
7976 && !initializer_constant_valid_p (inside_init
,
7977 TREE_TYPE (inside_init
)))
7979 error_init (init_loc
, "initializer element is not computable at "
7981 inside_init
= error_mark_node
;
7983 else if (require_constant
&& !maybe_const
)
7984 pedwarn_init (init_loc
, OPT_Wpedantic
,
7985 "initializer element is not a constant expression");
7990 /* Come here only for records and arrays. */
7992 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
7994 error_init (init_loc
, "variable-sized object may not be initialized");
7995 return error_mark_node
;
7998 error_init (init_loc
, "invalid initializer");
7999 return error_mark_node
;
8002 /* Handle initializers that use braces. */
8004 /* Type of object we are accumulating a constructor for.
8005 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
8006 static tree constructor_type
;
8008 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8010 static tree constructor_fields
;
8012 /* For an ARRAY_TYPE, this is the specified index
8013 at which to store the next element we get. */
8014 static tree constructor_index
;
8016 /* For an ARRAY_TYPE, this is the maximum index. */
8017 static tree constructor_max_index
;
8019 /* For a RECORD_TYPE, this is the first field not yet written out. */
8020 static tree constructor_unfilled_fields
;
8022 /* For an ARRAY_TYPE, this is the index of the first element
8023 not yet written out. */
8024 static tree constructor_unfilled_index
;
8026 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8027 This is so we can generate gaps between fields, when appropriate. */
8028 static tree constructor_bit_index
;
8030 /* If we are saving up the elements rather than allocating them,
8031 this is the list of elements so far (in reverse order,
8032 most recent first). */
8033 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
8035 /* 1 if constructor should be incrementally stored into a constructor chain,
8036 0 if all the elements should be kept in AVL tree. */
8037 static int constructor_incremental
;
8039 /* 1 if so far this constructor's elements are all compile-time constants. */
8040 static int constructor_constant
;
8042 /* 1 if so far this constructor's elements are all valid address constants. */
8043 static int constructor_simple
;
8045 /* 1 if this constructor has an element that cannot be part of a
8046 constant expression. */
8047 static int constructor_nonconst
;
8049 /* 1 if this constructor is erroneous so far. */
8050 static int constructor_erroneous
;
8052 /* 1 if this constructor is the universal zero initializer { 0 }. */
8053 static int constructor_zeroinit
;
8055 /* Structure for managing pending initializer elements, organized as an
8060 struct init_node
*left
, *right
;
8061 struct init_node
*parent
;
8068 /* Tree of pending elements at this constructor level.
8069 These are elements encountered out of order
8070 which belong at places we haven't reached yet in actually
8072 Will never hold tree nodes across GC runs. */
8073 static struct init_node
*constructor_pending_elts
;
8075 /* The SPELLING_DEPTH of this constructor. */
8076 static int constructor_depth
;
8078 /* DECL node for which an initializer is being read.
8079 0 means we are reading a constructor expression
8080 such as (struct foo) {...}. */
8081 static tree constructor_decl
;
8083 /* Nonzero if this is an initializer for a top-level decl. */
8084 static int constructor_top_level
;
8086 /* Nonzero if there were any member designators in this initializer. */
8087 static int constructor_designated
;
8089 /* Nesting depth of designator list. */
8090 static int designator_depth
;
8092 /* Nonzero if there were diagnosed errors in this designator list. */
8093 static int designator_erroneous
;
8096 /* This stack has a level for each implicit or explicit level of
8097 structuring in the initializer, including the outermost one. It
8098 saves the values of most of the variables above. */
8100 struct constructor_range_stack
;
8102 struct constructor_stack
8104 struct constructor_stack
*next
;
8109 tree unfilled_index
;
8110 tree unfilled_fields
;
8112 vec
<constructor_elt
, va_gc
> *elements
;
8113 struct init_node
*pending_elts
;
8116 /* If value nonzero, this value should replace the entire
8117 constructor at this level. */
8118 struct c_expr replacement_value
;
8119 struct constructor_range_stack
*range_stack
;
8128 int designator_depth
;
8131 static struct constructor_stack
*constructor_stack
;
8133 /* This stack represents designators from some range designator up to
8134 the last designator in the list. */
8136 struct constructor_range_stack
8138 struct constructor_range_stack
*next
, *prev
;
8139 struct constructor_stack
*stack
;
8146 static struct constructor_range_stack
*constructor_range_stack
;
8148 /* This stack records separate initializers that are nested.
8149 Nested initializers can't happen in ANSI C, but GNU C allows them
8150 in cases like { ... (struct foo) { ... } ... }. */
8152 struct initializer_stack
8154 struct initializer_stack
*next
;
8156 struct constructor_stack
*constructor_stack
;
8157 struct constructor_range_stack
*constructor_range_stack
;
8158 vec
<constructor_elt
, va_gc
> *elements
;
8159 struct spelling
*spelling
;
8160 struct spelling
*spelling_base
;
8163 char require_constant_value
;
8164 char require_constant_elements
;
8165 rich_location
*missing_brace_richloc
;
8168 static struct initializer_stack
*initializer_stack
;
8170 /* Prepare to parse and output the initializer for variable DECL. */
8173 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
,
8174 rich_location
*richloc
)
8177 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
8179 p
->decl
= constructor_decl
;
8180 p
->require_constant_value
= require_constant_value
;
8181 p
->require_constant_elements
= require_constant_elements
;
8182 p
->constructor_stack
= constructor_stack
;
8183 p
->constructor_range_stack
= constructor_range_stack
;
8184 p
->elements
= constructor_elements
;
8185 p
->spelling
= spelling
;
8186 p
->spelling_base
= spelling_base
;
8187 p
->spelling_size
= spelling_size
;
8188 p
->top_level
= constructor_top_level
;
8189 p
->next
= initializer_stack
;
8190 p
->missing_brace_richloc
= richloc
;
8191 initializer_stack
= p
;
8193 constructor_decl
= decl
;
8194 constructor_designated
= 0;
8195 constructor_top_level
= top_level
;
8197 if (decl
!= NULL_TREE
&& decl
!= error_mark_node
)
8199 require_constant_value
= TREE_STATIC (decl
);
8200 require_constant_elements
8201 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
8202 /* For a scalar, you can always use any value to initialize,
8203 even within braces. */
8204 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)));
8205 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
8209 require_constant_value
= 0;
8210 require_constant_elements
= 0;
8211 locus
= _("(anonymous)");
8214 constructor_stack
= 0;
8215 constructor_range_stack
= 0;
8217 found_missing_braces
= 0;
8221 RESTORE_SPELLING_DEPTH (0);
8224 push_string (locus
);
8230 struct initializer_stack
*p
= initializer_stack
;
8232 /* Free the whole constructor stack of this initializer. */
8233 while (constructor_stack
)
8235 struct constructor_stack
*q
= constructor_stack
;
8236 constructor_stack
= q
->next
;
8240 gcc_assert (!constructor_range_stack
);
8242 /* Pop back to the data of the outer initializer (if any). */
8243 free (spelling_base
);
8245 constructor_decl
= p
->decl
;
8246 require_constant_value
= p
->require_constant_value
;
8247 require_constant_elements
= p
->require_constant_elements
;
8248 constructor_stack
= p
->constructor_stack
;
8249 constructor_range_stack
= p
->constructor_range_stack
;
8250 constructor_elements
= p
->elements
;
8251 spelling
= p
->spelling
;
8252 spelling_base
= p
->spelling_base
;
8253 spelling_size
= p
->spelling_size
;
8254 constructor_top_level
= p
->top_level
;
8255 initializer_stack
= p
->next
;
8259 /* Call here when we see the initializer is surrounded by braces.
8260 This is instead of a call to push_init_level;
8261 it is matched by a call to pop_init_level.
8263 TYPE is the type to initialize, for a constructor expression.
8264 For an initializer for a decl, TYPE is zero. */
8267 really_start_incremental_init (tree type
)
8269 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
8271 if (type
== NULL_TREE
)
8272 type
= TREE_TYPE (constructor_decl
);
8274 if (VECTOR_TYPE_P (type
)
8275 && TYPE_VECTOR_OPAQUE (type
))
8276 error ("opaque vector types cannot be initialized");
8278 p
->type
= constructor_type
;
8279 p
->fields
= constructor_fields
;
8280 p
->index
= constructor_index
;
8281 p
->max_index
= constructor_max_index
;
8282 p
->unfilled_index
= constructor_unfilled_index
;
8283 p
->unfilled_fields
= constructor_unfilled_fields
;
8284 p
->bit_index
= constructor_bit_index
;
8285 p
->elements
= constructor_elements
;
8286 p
->constant
= constructor_constant
;
8287 p
->simple
= constructor_simple
;
8288 p
->nonconst
= constructor_nonconst
;
8289 p
->erroneous
= constructor_erroneous
;
8290 p
->pending_elts
= constructor_pending_elts
;
8291 p
->depth
= constructor_depth
;
8292 p
->replacement_value
.value
= 0;
8293 p
->replacement_value
.original_code
= ERROR_MARK
;
8294 p
->replacement_value
.original_type
= NULL
;
8298 p
->incremental
= constructor_incremental
;
8299 p
->designated
= constructor_designated
;
8300 p
->designator_depth
= designator_depth
;
8302 constructor_stack
= p
;
8304 constructor_constant
= 1;
8305 constructor_simple
= 1;
8306 constructor_nonconst
= 0;
8307 constructor_depth
= SPELLING_DEPTH ();
8308 constructor_elements
= NULL
;
8309 constructor_pending_elts
= 0;
8310 constructor_type
= type
;
8311 constructor_incremental
= 1;
8312 constructor_designated
= 0;
8313 constructor_zeroinit
= 1;
8314 designator_depth
= 0;
8315 designator_erroneous
= 0;
8317 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
8319 constructor_fields
= TYPE_FIELDS (constructor_type
);
8320 /* Skip any nameless bit fields at the beginning. */
8321 while (constructor_fields
!= NULL_TREE
8322 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
8323 constructor_fields
= DECL_CHAIN (constructor_fields
);
8325 constructor_unfilled_fields
= constructor_fields
;
8326 constructor_bit_index
= bitsize_zero_node
;
8328 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8330 if (TYPE_DOMAIN (constructor_type
))
8332 constructor_max_index
8333 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
8335 /* Detect non-empty initializations of zero-length arrays. */
8336 if (constructor_max_index
== NULL_TREE
8337 && TYPE_SIZE (constructor_type
))
8338 constructor_max_index
= integer_minus_one_node
;
8340 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8341 to initialize VLAs will cause a proper error; avoid tree
8342 checking errors as well by setting a safe value. */
8343 if (constructor_max_index
8344 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
8345 constructor_max_index
= integer_minus_one_node
;
8348 = convert (bitsizetype
,
8349 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8353 constructor_index
= bitsize_zero_node
;
8354 constructor_max_index
= NULL_TREE
;
8357 constructor_unfilled_index
= constructor_index
;
8359 else if (VECTOR_TYPE_P (constructor_type
))
8361 /* Vectors are like simple fixed-size arrays. */
8362 constructor_max_index
=
8363 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
8364 constructor_index
= bitsize_zero_node
;
8365 constructor_unfilled_index
= constructor_index
;
8369 /* Handle the case of int x = {5}; */
8370 constructor_fields
= constructor_type
;
8371 constructor_unfilled_fields
= constructor_type
;
8375 extern location_t last_init_list_comma
;
8377 /* Called when we see an open brace for a nested initializer. Finish
8378 off any pending levels with implicit braces. */
8380 finish_implicit_inits (location_t loc
, struct obstack
*braced_init_obstack
)
8382 while (constructor_stack
->implicit
)
8384 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
8385 && constructor_fields
== NULL_TREE
)
8386 process_init_element (input_location
,
8387 pop_init_level (loc
, 1, braced_init_obstack
,
8388 last_init_list_comma
),
8389 true, braced_init_obstack
);
8390 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8391 && constructor_max_index
8392 && tree_int_cst_lt (constructor_max_index
,
8394 process_init_element (input_location
,
8395 pop_init_level (loc
, 1, braced_init_obstack
,
8396 last_init_list_comma
),
8397 true, braced_init_obstack
);
8403 /* Push down into a subobject, for initialization.
8404 If this is for an explicit set of braces, IMPLICIT is 0.
8405 If it is because the next element belongs at a lower level,
8406 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8409 push_init_level (location_t loc
, int implicit
,
8410 struct obstack
*braced_init_obstack
)
8412 struct constructor_stack
*p
;
8413 tree value
= NULL_TREE
;
8415 /* Unless this is an explicit brace, we need to preserve previous
8419 if (RECORD_OR_UNION_TYPE_P (constructor_type
) && constructor_fields
)
8420 value
= find_init_member (constructor_fields
, braced_init_obstack
);
8421 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8422 value
= find_init_member (constructor_index
, braced_init_obstack
);
8425 p
= XNEW (struct constructor_stack
);
8426 p
->type
= constructor_type
;
8427 p
->fields
= constructor_fields
;
8428 p
->index
= constructor_index
;
8429 p
->max_index
= constructor_max_index
;
8430 p
->unfilled_index
= constructor_unfilled_index
;
8431 p
->unfilled_fields
= constructor_unfilled_fields
;
8432 p
->bit_index
= constructor_bit_index
;
8433 p
->elements
= constructor_elements
;
8434 p
->constant
= constructor_constant
;
8435 p
->simple
= constructor_simple
;
8436 p
->nonconst
= constructor_nonconst
;
8437 p
->erroneous
= constructor_erroneous
;
8438 p
->pending_elts
= constructor_pending_elts
;
8439 p
->depth
= constructor_depth
;
8440 p
->replacement_value
.value
= NULL_TREE
;
8441 p
->replacement_value
.original_code
= ERROR_MARK
;
8442 p
->replacement_value
.original_type
= NULL
;
8443 p
->implicit
= implicit
;
8445 p
->incremental
= constructor_incremental
;
8446 p
->designated
= constructor_designated
;
8447 p
->designator_depth
= designator_depth
;
8448 p
->next
= constructor_stack
;
8450 constructor_stack
= p
;
8452 constructor_constant
= 1;
8453 constructor_simple
= 1;
8454 constructor_nonconst
= 0;
8455 constructor_depth
= SPELLING_DEPTH ();
8456 constructor_elements
= NULL
;
8457 constructor_incremental
= 1;
8458 constructor_designated
= 0;
8459 constructor_pending_elts
= 0;
8462 p
->range_stack
= constructor_range_stack
;
8463 constructor_range_stack
= 0;
8464 designator_depth
= 0;
8465 designator_erroneous
= 0;
8468 /* Don't die if an entire brace-pair level is superfluous
8469 in the containing level. */
8470 if (constructor_type
== NULL_TREE
)
8472 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
8474 /* Don't die if there are extra init elts at the end. */
8475 if (constructor_fields
== NULL_TREE
)
8476 constructor_type
= NULL_TREE
;
8479 constructor_type
= TREE_TYPE (constructor_fields
);
8480 push_member_name (constructor_fields
);
8481 constructor_depth
++;
8483 /* If upper initializer is designated, then mark this as
8484 designated too to prevent bogus warnings. */
8485 constructor_designated
= p
->designated
;
8487 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8489 constructor_type
= TREE_TYPE (constructor_type
);
8490 push_array_bounds (tree_to_uhwi (constructor_index
));
8491 constructor_depth
++;
8494 if (constructor_type
== NULL_TREE
)
8496 error_init (loc
, "extra brace group at end of initializer");
8497 constructor_fields
= NULL_TREE
;
8498 constructor_unfilled_fields
= NULL_TREE
;
8502 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
8504 constructor_constant
= TREE_CONSTANT (value
);
8505 constructor_simple
= TREE_STATIC (value
);
8506 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
8507 constructor_elements
= CONSTRUCTOR_ELTS (value
);
8508 if (!vec_safe_is_empty (constructor_elements
)
8509 && (TREE_CODE (constructor_type
) == RECORD_TYPE
8510 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
8511 set_nonincremental_init (braced_init_obstack
);
8516 found_missing_braces
= 1;
8517 if (initializer_stack
->missing_brace_richloc
)
8518 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
8522 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
8524 constructor_fields
= TYPE_FIELDS (constructor_type
);
8525 /* Skip any nameless bit fields at the beginning. */
8526 while (constructor_fields
!= NULL_TREE
8527 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
8528 constructor_fields
= DECL_CHAIN (constructor_fields
);
8530 constructor_unfilled_fields
= constructor_fields
;
8531 constructor_bit_index
= bitsize_zero_node
;
8533 else if (VECTOR_TYPE_P (constructor_type
))
8535 /* Vectors are like simple fixed-size arrays. */
8536 constructor_max_index
=
8537 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
8538 constructor_index
= bitsize_int (0);
8539 constructor_unfilled_index
= constructor_index
;
8541 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8543 if (TYPE_DOMAIN (constructor_type
))
8545 constructor_max_index
8546 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
8548 /* Detect non-empty initializations of zero-length arrays. */
8549 if (constructor_max_index
== NULL_TREE
8550 && TYPE_SIZE (constructor_type
))
8551 constructor_max_index
= integer_minus_one_node
;
8553 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8554 to initialize VLAs will cause a proper error; avoid tree
8555 checking errors as well by setting a safe value. */
8556 if (constructor_max_index
8557 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
8558 constructor_max_index
= integer_minus_one_node
;
8561 = convert (bitsizetype
,
8562 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8565 constructor_index
= bitsize_zero_node
;
8567 constructor_unfilled_index
= constructor_index
;
8568 if (value
&& TREE_CODE (value
) == STRING_CST
)
8570 /* We need to split the char/wchar array into individual
8571 characters, so that we don't have to special case it
8573 set_nonincremental_init_from_string (value
, braced_init_obstack
);
8578 if (constructor_type
!= error_mark_node
)
8579 warning_init (input_location
, 0, "braces around scalar initializer");
8580 constructor_fields
= constructor_type
;
8581 constructor_unfilled_fields
= constructor_type
;
8585 /* At the end of an implicit or explicit brace level,
8586 finish up that level of constructor. If a single expression
8587 with redundant braces initialized that level, return the
8588 c_expr structure for that expression. Otherwise, the original_code
8589 element is set to ERROR_MARK.
8590 If we were outputting the elements as they are read, return 0 as the value
8591 from inner levels (process_init_element ignores that),
8592 but return error_mark_node as the value from the outermost level
8593 (that's what we want to put in DECL_INITIAL).
8594 Otherwise, return a CONSTRUCTOR expression as the value. */
8597 pop_init_level (location_t loc
, int implicit
,
8598 struct obstack
*braced_init_obstack
,
8599 location_t insert_before
)
8601 struct constructor_stack
*p
;
8603 ret
.value
= NULL_TREE
;
8604 ret
.original_code
= ERROR_MARK
;
8605 ret
.original_type
= NULL
;
8609 /* When we come to an explicit close brace,
8610 pop any inner levels that didn't have explicit braces. */
8611 while (constructor_stack
->implicit
)
8612 process_init_element (input_location
,
8613 pop_init_level (loc
, 1, braced_init_obstack
,
8615 true, braced_init_obstack
);
8616 gcc_assert (!constructor_range_stack
);
8619 if (initializer_stack
->missing_brace_richloc
)
8620 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
8621 (insert_before
, "}");
8623 /* Now output all pending elements. */
8624 constructor_incremental
= 1;
8625 output_pending_init_elements (1, braced_init_obstack
);
8627 p
= constructor_stack
;
8629 /* Error for initializing a flexible array member, or a zero-length
8630 array member in an inappropriate context. */
8631 if (constructor_type
&& constructor_fields
8632 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8633 && TYPE_DOMAIN (constructor_type
)
8634 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
8636 /* Silently discard empty initializations. The parser will
8637 already have pedwarned for empty brackets. */
8638 if (integer_zerop (constructor_unfilled_index
))
8639 constructor_type
= NULL_TREE
;
8642 gcc_assert (!TYPE_SIZE (constructor_type
));
8644 if (constructor_depth
> 2)
8645 error_init (loc
, "initialization of flexible array member in a nested context");
8647 pedwarn_init (loc
, OPT_Wpedantic
,
8648 "initialization of a flexible array member");
8650 /* We have already issued an error message for the existence
8651 of a flexible array member not at the end of the structure.
8652 Discard the initializer so that we do not die later. */
8653 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
8654 constructor_type
= NULL_TREE
;
8658 switch (vec_safe_length (constructor_elements
))
8661 /* Initialization with { } counts as zeroinit. */
8662 constructor_zeroinit
= 1;
8665 /* This might be zeroinit as well. */
8666 if (integer_zerop ((*constructor_elements
)[0].value
))
8667 constructor_zeroinit
= 1;
8670 /* If the constructor has more than one element, it can't be { 0 }. */
8671 constructor_zeroinit
= 0;
8675 /* Warn when some structs are initialized with direct aggregation. */
8676 if (!implicit
&& found_missing_braces
&& warn_missing_braces
8677 && !constructor_zeroinit
)
8679 gcc_assert (initializer_stack
->missing_brace_richloc
);
8680 warning_at (initializer_stack
->missing_brace_richloc
,
8681 OPT_Wmissing_braces
,
8682 "missing braces around initializer");
8685 /* Warn when some struct elements are implicitly initialized to zero. */
8686 if (warn_missing_field_initializers
8688 && TREE_CODE (constructor_type
) == RECORD_TYPE
8689 && constructor_unfilled_fields
)
8691 /* Do not warn for flexible array members or zero-length arrays. */
8692 while (constructor_unfilled_fields
8693 && (!DECL_SIZE (constructor_unfilled_fields
)
8694 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
8695 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
8697 if (constructor_unfilled_fields
8698 /* Do not warn if this level of the initializer uses member
8699 designators; it is likely to be deliberate. */
8700 && !constructor_designated
8701 /* Do not warn about initializing with { 0 } or with { }. */
8702 && !constructor_zeroinit
)
8704 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
8705 "missing initializer for field %qD of %qT",
8706 constructor_unfilled_fields
,
8708 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
8709 "%qD declared here", constructor_unfilled_fields
);
8713 /* Pad out the end of the structure. */
8714 if (p
->replacement_value
.value
)
8715 /* If this closes a superfluous brace pair,
8716 just pass out the element between them. */
8717 ret
= p
->replacement_value
;
8718 else if (constructor_type
== NULL_TREE
)
8720 else if (!RECORD_OR_UNION_TYPE_P (constructor_type
)
8721 && TREE_CODE (constructor_type
) != ARRAY_TYPE
8722 && !VECTOR_TYPE_P (constructor_type
))
8724 /* A nonincremental scalar initializer--just return
8725 the element, after verifying there is just one. */
8726 if (vec_safe_is_empty (constructor_elements
))
8728 if (!constructor_erroneous
)
8729 error_init (loc
, "empty scalar initializer");
8730 ret
.value
= error_mark_node
;
8732 else if (vec_safe_length (constructor_elements
) != 1)
8734 error_init (loc
, "extra elements in scalar initializer");
8735 ret
.value
= (*constructor_elements
)[0].value
;
8738 ret
.value
= (*constructor_elements
)[0].value
;
8742 if (constructor_erroneous
)
8743 ret
.value
= error_mark_node
;
8746 ret
.value
= build_constructor (constructor_type
,
8747 constructor_elements
);
8748 if (constructor_constant
)
8749 TREE_CONSTANT (ret
.value
) = 1;
8750 if (constructor_constant
&& constructor_simple
)
8751 TREE_STATIC (ret
.value
) = 1;
8752 if (constructor_nonconst
)
8753 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
8757 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
8759 if (constructor_nonconst
)
8760 ret
.original_code
= C_MAYBE_CONST_EXPR
;
8761 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
8762 ret
.original_code
= ERROR_MARK
;
8765 constructor_type
= p
->type
;
8766 constructor_fields
= p
->fields
;
8767 constructor_index
= p
->index
;
8768 constructor_max_index
= p
->max_index
;
8769 constructor_unfilled_index
= p
->unfilled_index
;
8770 constructor_unfilled_fields
= p
->unfilled_fields
;
8771 constructor_bit_index
= p
->bit_index
;
8772 constructor_elements
= p
->elements
;
8773 constructor_constant
= p
->constant
;
8774 constructor_simple
= p
->simple
;
8775 constructor_nonconst
= p
->nonconst
;
8776 constructor_erroneous
= p
->erroneous
;
8777 constructor_incremental
= p
->incremental
;
8778 constructor_designated
= p
->designated
;
8779 designator_depth
= p
->designator_depth
;
8780 constructor_pending_elts
= p
->pending_elts
;
8781 constructor_depth
= p
->depth
;
8783 constructor_range_stack
= p
->range_stack
;
8784 RESTORE_SPELLING_DEPTH (constructor_depth
);
8786 constructor_stack
= p
->next
;
8789 if (ret
.value
== NULL_TREE
&& constructor_stack
== 0)
8790 ret
.value
= error_mark_node
;
8794 /* Common handling for both array range and field name designators.
8795 ARRAY argument is nonzero for array ranges. Returns false for success. */
8798 set_designator (location_t loc
, bool array
,
8799 struct obstack
*braced_init_obstack
)
8802 enum tree_code subcode
;
8804 /* Don't die if an entire brace-pair level is superfluous
8805 in the containing level. */
8806 if (constructor_type
== NULL_TREE
)
8809 /* If there were errors in this designator list already, bail out
8811 if (designator_erroneous
)
8814 if (!designator_depth
)
8816 gcc_assert (!constructor_range_stack
);
8818 /* Designator list starts at the level of closest explicit
8820 while (constructor_stack
->implicit
)
8821 process_init_element (input_location
,
8822 pop_init_level (loc
, 1, braced_init_obstack
,
8823 last_init_list_comma
),
8824 true, braced_init_obstack
);
8825 constructor_designated
= 1;
8829 switch (TREE_CODE (constructor_type
))
8833 subtype
= TREE_TYPE (constructor_fields
);
8834 if (subtype
!= error_mark_node
)
8835 subtype
= TYPE_MAIN_VARIANT (subtype
);
8838 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8844 subcode
= TREE_CODE (subtype
);
8845 if (array
&& subcode
!= ARRAY_TYPE
)
8847 error_init (loc
, "array index in non-array initializer");
8850 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
8852 error_init (loc
, "field name not in record or union initializer");
8856 constructor_designated
= 1;
8857 finish_implicit_inits (loc
, braced_init_obstack
);
8858 push_init_level (loc
, 2, braced_init_obstack
);
8862 /* If there are range designators in designator list, push a new designator
8863 to constructor_range_stack. RANGE_END is end of such stack range or
8864 NULL_TREE if there is no range designator at this level. */
8867 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
8869 struct constructor_range_stack
*p
;
8871 p
= (struct constructor_range_stack
*)
8872 obstack_alloc (braced_init_obstack
,
8873 sizeof (struct constructor_range_stack
));
8874 p
->prev
= constructor_range_stack
;
8876 p
->fields
= constructor_fields
;
8877 p
->range_start
= constructor_index
;
8878 p
->index
= constructor_index
;
8879 p
->stack
= constructor_stack
;
8880 p
->range_end
= range_end
;
8881 if (constructor_range_stack
)
8882 constructor_range_stack
->next
= p
;
8883 constructor_range_stack
= p
;
8886 /* Within an array initializer, specify the next index to be initialized.
8887 FIRST is that index. If LAST is nonzero, then initialize a range
8888 of indices, running from FIRST through LAST. */
8891 set_init_index (location_t loc
, tree first
, tree last
,
8892 struct obstack
*braced_init_obstack
)
8894 if (set_designator (loc
, true, braced_init_obstack
))
8897 designator_erroneous
= 1;
8899 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
8900 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
8902 error_init (loc
, "array index in initializer not of integer type");
8906 if (TREE_CODE (first
) != INTEGER_CST
)
8908 first
= c_fully_fold (first
, false, NULL
);
8909 if (TREE_CODE (first
) == INTEGER_CST
)
8910 pedwarn_init (loc
, OPT_Wpedantic
,
8911 "array index in initializer is not "
8912 "an integer constant expression");
8915 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
8917 last
= c_fully_fold (last
, false, NULL
);
8918 if (TREE_CODE (last
) == INTEGER_CST
)
8919 pedwarn_init (loc
, OPT_Wpedantic
,
8920 "array index in initializer is not "
8921 "an integer constant expression");
8924 if (TREE_CODE (first
) != INTEGER_CST
)
8925 error_init (loc
, "nonconstant array index in initializer");
8926 else if (last
!= NULL_TREE
&& TREE_CODE (last
) != INTEGER_CST
)
8927 error_init (loc
, "nonconstant array index in initializer");
8928 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8929 error_init (loc
, "array index in non-array initializer");
8930 else if (tree_int_cst_sgn (first
) == -1)
8931 error_init (loc
, "array index in initializer exceeds array bounds");
8932 else if (constructor_max_index
8933 && tree_int_cst_lt (constructor_max_index
, first
))
8934 error_init (loc
, "array index in initializer exceeds array bounds");
8937 constant_expression_warning (first
);
8939 constant_expression_warning (last
);
8940 constructor_index
= convert (bitsizetype
, first
);
8941 if (tree_int_cst_lt (constructor_index
, first
))
8943 constructor_index
= copy_node (constructor_index
);
8944 TREE_OVERFLOW (constructor_index
) = 1;
8949 if (tree_int_cst_equal (first
, last
))
8951 else if (tree_int_cst_lt (last
, first
))
8953 error_init (loc
, "empty index range in initializer");
8958 last
= convert (bitsizetype
, last
);
8959 if (constructor_max_index
!= NULL_TREE
8960 && tree_int_cst_lt (constructor_max_index
, last
))
8962 error_init (loc
, "array index range in initializer exceeds "
8970 designator_erroneous
= 0;
8971 if (constructor_range_stack
|| last
)
8972 push_range_stack (last
, braced_init_obstack
);
8976 /* Within a struct initializer, specify the next field to be initialized. */
8979 set_init_label (location_t loc
, tree fieldname
, location_t fieldname_loc
,
8980 struct obstack
*braced_init_obstack
)
8984 if (set_designator (loc
, false, braced_init_obstack
))
8987 designator_erroneous
= 1;
8989 if (!RECORD_OR_UNION_TYPE_P (constructor_type
))
8991 error_init (loc
, "field name not in record or union initializer");
8995 field
= lookup_field (constructor_type
, fieldname
);
8997 if (field
== NULL_TREE
)
8999 tree guessed_id
= lookup_field_fuzzy (constructor_type
, fieldname
);
9002 gcc_rich_location
rich_loc (fieldname_loc
);
9003 rich_loc
.add_fixit_misspelled_id (fieldname_loc
, guessed_id
);
9004 error_at (&rich_loc
,
9005 "%qT has no member named %qE; did you mean %qE?",
9006 constructor_type
, fieldname
, guessed_id
);
9009 error_at (fieldname_loc
, "%qT has no member named %qE",
9010 constructor_type
, fieldname
);
9015 constructor_fields
= TREE_VALUE (field
);
9017 designator_erroneous
= 0;
9018 if (constructor_range_stack
)
9019 push_range_stack (NULL_TREE
, braced_init_obstack
);
9020 field
= TREE_CHAIN (field
);
9023 if (set_designator (loc
, false, braced_init_obstack
))
9027 while (field
!= NULL_TREE
);
9030 /* Add a new initializer to the tree of pending initializers. PURPOSE
9031 identifies the initializer, either array index or field in a structure.
9032 VALUE is the value of that index or field. If ORIGTYPE is not
9033 NULL_TREE, it is the original type of VALUE.
9035 IMPLICIT is true if value comes from pop_init_level (1),
9036 the new initializer has been merged with the existing one
9037 and thus no warnings should be emitted about overriding an
9038 existing initializer. */
9041 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
9042 bool implicit
, struct obstack
*braced_init_obstack
)
9044 struct init_node
*p
, **q
, *r
;
9046 q
= &constructor_pending_elts
;
9049 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9054 if (tree_int_cst_lt (purpose
, p
->purpose
))
9056 else if (tree_int_cst_lt (p
->purpose
, purpose
))
9062 if (TREE_SIDE_EFFECTS (p
->value
))
9063 warning_init (loc
, OPT_Woverride_init_side_effects
,
9064 "initialized field with side-effects "
9066 else if (warn_override_init
)
9067 warning_init (loc
, OPT_Woverride_init
,
9068 "initialized field overwritten");
9071 p
->origtype
= origtype
;
9080 bitpos
= bit_position (purpose
);
9084 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
9086 else if (p
->purpose
!= purpose
)
9092 if (TREE_SIDE_EFFECTS (p
->value
))
9093 warning_init (loc
, OPT_Woverride_init_side_effects
,
9094 "initialized field with side-effects "
9096 else if (warn_override_init
)
9097 warning_init (loc
, OPT_Woverride_init
,
9098 "initialized field overwritten");
9101 p
->origtype
= origtype
;
9107 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
9108 sizeof (struct init_node
));
9109 r
->purpose
= purpose
;
9111 r
->origtype
= origtype
;
9121 struct init_node
*s
;
9125 if (p
->balance
== 0)
9127 else if (p
->balance
< 0)
9134 p
->left
->parent
= p
;
9151 constructor_pending_elts
= r
;
9156 struct init_node
*t
= r
->right
;
9160 r
->right
->parent
= r
;
9165 p
->left
->parent
= p
;
9168 p
->balance
= t
->balance
< 0;
9169 r
->balance
= -(t
->balance
> 0);
9184 constructor_pending_elts
= t
;
9190 /* p->balance == +1; growth of left side balances the node. */
9195 else /* r == p->right */
9197 if (p
->balance
== 0)
9198 /* Growth propagation from right side. */
9200 else if (p
->balance
> 0)
9207 p
->right
->parent
= p
;
9224 constructor_pending_elts
= r
;
9226 else /* r->balance == -1 */
9229 struct init_node
*t
= r
->left
;
9233 r
->left
->parent
= r
;
9238 p
->right
->parent
= p
;
9241 r
->balance
= (t
->balance
< 0);
9242 p
->balance
= -(t
->balance
> 0);
9257 constructor_pending_elts
= t
;
9263 /* p->balance == -1; growth of right side balances the node. */
9274 /* Build AVL tree from a sorted chain. */
9277 set_nonincremental_init (struct obstack
* braced_init_obstack
)
9279 unsigned HOST_WIDE_INT ix
;
9282 if (TREE_CODE (constructor_type
) != RECORD_TYPE
9283 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
9286 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
9287 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
9288 braced_init_obstack
);
9289 constructor_elements
= NULL
;
9290 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9292 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
9293 /* Skip any nameless bit fields at the beginning. */
9294 while (constructor_unfilled_fields
!= NULL_TREE
9295 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields
))
9296 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
9299 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9301 if (TYPE_DOMAIN (constructor_type
))
9302 constructor_unfilled_index
9303 = convert (bitsizetype
,
9304 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
9306 constructor_unfilled_index
= bitsize_zero_node
;
9308 constructor_incremental
= 0;
9311 /* Build AVL tree from a string constant. */
9314 set_nonincremental_init_from_string (tree str
,
9315 struct obstack
* braced_init_obstack
)
9317 tree value
, purpose
, type
;
9318 HOST_WIDE_INT val
[2];
9319 const char *p
, *end
;
9320 int byte
, wchar_bytes
, charwidth
, bitpos
;
9322 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
9324 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
9325 charwidth
= TYPE_PRECISION (char_type_node
);
9326 gcc_assert ((size_t) wchar_bytes
* charwidth
9327 <= ARRAY_SIZE (val
) * HOST_BITS_PER_WIDE_INT
);
9328 type
= TREE_TYPE (constructor_type
);
9329 p
= TREE_STRING_POINTER (str
);
9330 end
= p
+ TREE_STRING_LENGTH (str
);
9332 for (purpose
= bitsize_zero_node
;
9334 && !(constructor_max_index
9335 && tree_int_cst_lt (constructor_max_index
, purpose
));
9336 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
9338 if (wchar_bytes
== 1)
9340 val
[0] = (unsigned char) *p
++;
9347 for (byte
= 0; byte
< wchar_bytes
; byte
++)
9349 if (BYTES_BIG_ENDIAN
)
9350 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
9352 bitpos
= byte
* charwidth
;
9353 val
[bitpos
/ HOST_BITS_PER_WIDE_INT
]
9354 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
9355 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
9359 if (!TYPE_UNSIGNED (type
))
9361 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
9362 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
9364 if (val
[0] & (HOST_WIDE_INT_1
<< (bitpos
- 1)))
9366 val
[0] |= HOST_WIDE_INT_M1U
<< bitpos
;
9370 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
9375 else if (val
[1] & (HOST_WIDE_INT_1
9376 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
9377 val
[1] |= HOST_WIDE_INT_M1U
<< (bitpos
- HOST_BITS_PER_WIDE_INT
);
9380 value
= wide_int_to_tree (type
,
9381 wide_int::from_array (val
, 2,
9382 HOST_BITS_PER_WIDE_INT
* 2));
9383 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
9384 braced_init_obstack
);
9387 constructor_incremental
= 0;
9390 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9391 not initialized yet. */
9394 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
9396 struct init_node
*p
;
9398 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9400 if (constructor_incremental
9401 && tree_int_cst_lt (field
, constructor_unfilled_index
))
9402 set_nonincremental_init (braced_init_obstack
);
9404 p
= constructor_pending_elts
;
9407 if (tree_int_cst_lt (field
, p
->purpose
))
9409 else if (tree_int_cst_lt (p
->purpose
, field
))
9415 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9417 tree bitpos
= bit_position (field
);
9419 if (constructor_incremental
9420 && (!constructor_unfilled_fields
9421 || tree_int_cst_lt (bitpos
,
9422 bit_position (constructor_unfilled_fields
))))
9423 set_nonincremental_init (braced_init_obstack
);
9425 p
= constructor_pending_elts
;
9428 if (field
== p
->purpose
)
9430 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
9436 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9438 if (!vec_safe_is_empty (constructor_elements
)
9439 && (constructor_elements
->last ().index
== field
))
9440 return constructor_elements
->last ().value
;
9445 /* "Output" the next constructor element.
9446 At top level, really output it to assembler code now.
9447 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9448 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9449 TYPE is the data type that the containing data type wants here.
9450 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9451 If VALUE is a string constant, STRICT_STRING is true if it is
9452 unparenthesized or we should not warn here for it being parenthesized.
9453 For other types of VALUE, STRICT_STRING is not used.
9455 PENDING if true means output pending elements that belong
9456 right after this element. (PENDING is normally true;
9457 it is false while outputting pending elements, to avoid recursion.)
9459 IMPLICIT is true if value comes from pop_init_level (1),
9460 the new initializer has been merged with the existing one
9461 and thus no warnings should be emitted about overriding an
9462 existing initializer. */
9465 output_init_element (location_t loc
, tree value
, tree origtype
,
9466 bool strict_string
, tree type
, tree field
, bool pending
,
9467 bool implicit
, struct obstack
* braced_init_obstack
)
9469 tree semantic_type
= NULL_TREE
;
9470 bool maybe_const
= true;
9473 if (type
== error_mark_node
|| value
== error_mark_node
)
9475 constructor_erroneous
= 1;
9478 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
9479 && (TREE_CODE (value
) == STRING_CST
9480 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
9481 && !(TREE_CODE (value
) == STRING_CST
9482 && TREE_CODE (type
) == ARRAY_TYPE
9483 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
9484 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
9485 TYPE_MAIN_VARIANT (type
)))
9486 value
= array_to_pointer_conversion (input_location
, value
);
9488 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
9489 && require_constant_value
&& pending
)
9491 /* As an extension, allow initializing objects with static storage
9492 duration with compound literals (which are then treated just as
9493 the brace enclosed list they contain). */
9495 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
9497 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
9498 value
= DECL_INITIAL (decl
);
9501 npc
= null_pointer_constant_p (value
);
9502 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
9504 semantic_type
= TREE_TYPE (value
);
9505 value
= TREE_OPERAND (value
, 0);
9507 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
9509 if (value
== error_mark_node
)
9510 constructor_erroneous
= 1;
9511 else if (!TREE_CONSTANT (value
))
9512 constructor_constant
= 0;
9513 else if (!initializer_constant_valid_p (value
,
9515 AGGREGATE_TYPE_P (constructor_type
)
9516 && TYPE_REVERSE_STORAGE_ORDER
9518 || (RECORD_OR_UNION_TYPE_P (constructor_type
)
9519 && DECL_C_BIT_FIELD (field
)
9520 && TREE_CODE (value
) != INTEGER_CST
))
9521 constructor_simple
= 0;
9523 constructor_nonconst
= 1;
9525 /* Digest the initializer and issue any errors about incompatible
9526 types before issuing errors about non-constant initializers. */
9527 tree new_value
= value
;
9529 new_value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
9530 new_value
= digest_init (loc
, type
, new_value
, origtype
, npc
, strict_string
,
9531 require_constant_value
);
9532 if (new_value
== error_mark_node
)
9534 constructor_erroneous
= 1;
9537 if (require_constant_value
|| require_constant_elements
)
9538 constant_expression_warning (new_value
);
9540 /* Proceed to check the constness of the original initializer. */
9541 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
9543 if (require_constant_value
)
9545 error_init (loc
, "initializer element is not constant");
9546 value
= error_mark_node
;
9548 else if (require_constant_elements
)
9549 pedwarn (loc
, OPT_Wpedantic
,
9550 "initializer element is not computable at load time");
9552 else if (!maybe_const
9553 && (require_constant_value
|| require_constant_elements
))
9554 pedwarn_init (loc
, OPT_Wpedantic
,
9555 "initializer element is not a constant expression");
9557 /* Issue -Wc++-compat warnings about initializing a bitfield with
9560 && field
!= NULL_TREE
9561 && TREE_CODE (field
) == FIELD_DECL
9562 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
9563 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
9564 != TYPE_MAIN_VARIANT (type
))
9565 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
9567 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
9568 if (checktype
!= error_mark_node
9569 && (TYPE_MAIN_VARIANT (checktype
)
9570 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
9571 warning_init (loc
, OPT_Wc___compat
,
9572 "enum conversion in initialization is invalid in C++");
9575 /* If this field is empty and does not have side effects (and is not at
9576 the end of structure), don't do anything other than checking the
9579 && (TREE_TYPE (field
) == error_mark_node
9580 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
9581 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
9582 && !TREE_SIDE_EFFECTS (new_value
)
9583 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
9584 || DECL_CHAIN (field
)))))
9587 /* Finally, set VALUE to the initializer value digested above. */
9590 /* If this element doesn't come next in sequence,
9591 put it on constructor_pending_elts. */
9592 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
9593 && (!constructor_incremental
9594 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
9596 if (constructor_incremental
9597 && tree_int_cst_lt (field
, constructor_unfilled_index
))
9598 set_nonincremental_init (braced_init_obstack
);
9600 add_pending_init (loc
, field
, value
, origtype
, implicit
,
9601 braced_init_obstack
);
9604 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
9605 && (!constructor_incremental
9606 || field
!= constructor_unfilled_fields
))
9608 /* We do this for records but not for unions. In a union,
9609 no matter which field is specified, it can be initialized
9610 right away since it starts at the beginning of the union. */
9611 if (constructor_incremental
)
9613 if (!constructor_unfilled_fields
)
9614 set_nonincremental_init (braced_init_obstack
);
9617 tree bitpos
, unfillpos
;
9619 bitpos
= bit_position (field
);
9620 unfillpos
= bit_position (constructor_unfilled_fields
);
9622 if (tree_int_cst_lt (bitpos
, unfillpos
))
9623 set_nonincremental_init (braced_init_obstack
);
9627 add_pending_init (loc
, field
, value
, origtype
, implicit
,
9628 braced_init_obstack
);
9631 else if (TREE_CODE (constructor_type
) == UNION_TYPE
9632 && !vec_safe_is_empty (constructor_elements
))
9636 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
9637 warning_init (loc
, OPT_Woverride_init_side_effects
,
9638 "initialized field with side-effects overwritten");
9639 else if (warn_override_init
)
9640 warning_init (loc
, OPT_Woverride_init
,
9641 "initialized field overwritten");
9644 /* We can have just one union field set. */
9645 constructor_elements
= NULL
;
9648 /* Otherwise, output this element either to
9649 constructor_elements or to the assembler file. */
9651 constructor_elt celt
= {field
, value
};
9652 vec_safe_push (constructor_elements
, celt
);
9654 /* Advance the variable that indicates sequential elements output. */
9655 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9656 constructor_unfilled_index
9657 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
9659 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9661 constructor_unfilled_fields
9662 = DECL_CHAIN (constructor_unfilled_fields
);
9664 /* Skip any nameless bit fields. */
9665 while (constructor_unfilled_fields
!= NULL_TREE
9666 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields
))
9667 constructor_unfilled_fields
=
9668 DECL_CHAIN (constructor_unfilled_fields
);
9670 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9671 constructor_unfilled_fields
= NULL_TREE
;
9673 /* Now output any pending elements which have become next. */
9675 output_pending_init_elements (0, braced_init_obstack
);
9678 /* For two FIELD_DECLs in the same chain, return -1 if field1
9679 comes before field2, 1 if field1 comes after field2 and
9680 0 if field1 == field2. */
9683 init_field_decl_cmp (tree field1
, tree field2
)
9685 if (field1
== field2
)
9688 tree bitpos1
= bit_position (field1
);
9689 tree bitpos2
= bit_position (field2
);
9690 if (tree_int_cst_equal (bitpos1
, bitpos2
))
9692 /* If one of the fields has non-zero bitsize, then that
9693 field must be the last one in a sequence of zero
9694 sized fields, fields after it will have bigger
9696 if (TREE_TYPE (field1
) != error_mark_node
9697 && COMPLETE_TYPE_P (TREE_TYPE (field1
))
9698 && integer_nonzerop (TREE_TYPE (field1
)))
9700 if (TREE_TYPE (field2
) != error_mark_node
9701 && COMPLETE_TYPE_P (TREE_TYPE (field2
))
9702 && integer_nonzerop (TREE_TYPE (field2
)))
9704 /* Otherwise, fallback to DECL_CHAIN walk to find out
9705 which field comes earlier. Walk chains of both
9706 fields, so that if field1 and field2 are close to each
9707 other in either order, it is found soon even for large
9708 sequences of zero sized fields. */
9709 tree f1
= field1
, f2
= field2
;
9712 f1
= DECL_CHAIN (f1
);
9713 f2
= DECL_CHAIN (f2
);
9714 if (f1
== NULL_TREE
)
9719 if (f2
== NULL_TREE
)
9725 if (!tree_int_cst_equal (bit_position (f1
), bitpos1
))
9727 if (!tree_int_cst_equal (bit_position (f2
), bitpos1
))
9731 else if (tree_int_cst_lt (bitpos1
, bitpos2
))
9737 /* Output any pending elements which have become next.
9738 As we output elements, constructor_unfilled_{fields,index}
9739 advances, which may cause other elements to become next;
9740 if so, they too are output.
9742 If ALL is 0, we return when there are
9743 no more pending elements to output now.
9745 If ALL is 1, we output space as necessary so that
9746 we can output all the pending elements. */
9748 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
9750 struct init_node
*elt
= constructor_pending_elts
;
9755 /* Look through the whole pending tree.
9756 If we find an element that should be output now,
9757 output it. Otherwise, set NEXT to the element
9758 that comes first among those still pending. */
9763 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9765 if (tree_int_cst_equal (elt
->purpose
,
9766 constructor_unfilled_index
))
9767 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9768 true, TREE_TYPE (constructor_type
),
9769 constructor_unfilled_index
, false, false,
9770 braced_init_obstack
);
9771 else if (tree_int_cst_lt (constructor_unfilled_index
,
9774 /* Advance to the next smaller node. */
9779 /* We have reached the smallest node bigger than the
9780 current unfilled index. Fill the space first. */
9781 next
= elt
->purpose
;
9787 /* Advance to the next bigger node. */
9792 /* We have reached the biggest node in a subtree. Find
9793 the parent of it, which is the next bigger node. */
9794 while (elt
->parent
&& elt
->parent
->right
== elt
)
9797 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
9800 next
= elt
->purpose
;
9806 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9808 /* If the current record is complete we are done. */
9809 if (constructor_unfilled_fields
== NULL_TREE
)
9812 int cmp
= init_field_decl_cmp (constructor_unfilled_fields
,
9815 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9816 true, TREE_TYPE (elt
->purpose
),
9817 elt
->purpose
, false, false,
9818 braced_init_obstack
);
9821 /* Advance to the next smaller node. */
9826 /* We have reached the smallest node bigger than the
9827 current unfilled field. Fill the space first. */
9828 next
= elt
->purpose
;
9834 /* Advance to the next bigger node. */
9839 /* We have reached the biggest node in a subtree. Find
9840 the parent of it, which is the next bigger node. */
9841 while (elt
->parent
&& elt
->parent
->right
== elt
)
9845 && init_field_decl_cmp (constructor_unfilled_fields
,
9848 next
= elt
->purpose
;
9856 /* Ordinarily return, but not if we want to output all
9857 and there are elements left. */
9858 if (!(all
&& next
!= NULL_TREE
))
9861 /* If it's not incremental, just skip over the gap, so that after
9862 jumping to retry we will output the next successive element. */
9863 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9864 constructor_unfilled_fields
= next
;
9865 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9866 constructor_unfilled_index
= next
;
9868 /* ELT now points to the node in the pending tree with the next
9869 initializer to output. */
9873 /* Add one non-braced element to the current constructor level.
9874 This adjusts the current position within the constructor's type.
9875 This may also start or terminate implicit levels
9876 to handle a partly-braced initializer.
9878 Once this has found the correct level for the new element,
9879 it calls output_init_element.
9881 IMPLICIT is true if value comes from pop_init_level (1),
9882 the new initializer has been merged with the existing one
9883 and thus no warnings should be emitted about overriding an
9884 existing initializer. */
9887 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
9888 struct obstack
* braced_init_obstack
)
9890 tree orig_value
= value
.value
;
9892 = (orig_value
!= NULL_TREE
&& TREE_CODE (orig_value
) == STRING_CST
);
9893 bool strict_string
= value
.original_code
== STRING_CST
;
9894 bool was_designated
= designator_depth
!= 0;
9896 designator_depth
= 0;
9897 designator_erroneous
= 0;
9899 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
9900 constructor_zeroinit
= 0;
9902 /* Handle superfluous braces around string cst as in
9903 char x[] = {"foo"}; */
9907 && TREE_CODE (constructor_type
) == ARRAY_TYPE
9908 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
9909 && integer_zerop (constructor_unfilled_index
))
9911 if (constructor_stack
->replacement_value
.value
)
9912 error_init (loc
, "excess elements in %<char%> array initializer");
9913 constructor_stack
->replacement_value
= value
;
9917 if (constructor_stack
->replacement_value
.value
!= NULL_TREE
)
9919 error_init (loc
, "excess elements in struct initializer");
9923 /* Ignore elements of a brace group if it is entirely superfluous
9924 and has already been diagnosed. */
9925 if (constructor_type
== NULL_TREE
)
9928 if (!implicit
&& warn_designated_init
&& !was_designated
9929 && TREE_CODE (constructor_type
) == RECORD_TYPE
9930 && lookup_attribute ("designated_init",
9931 TYPE_ATTRIBUTES (constructor_type
)))
9933 OPT_Wdesignated_init
,
9934 "positional initialization of field "
9935 "in %<struct%> declared with %<designated_init%> attribute");
9937 /* If we've exhausted any levels that didn't have braces,
9939 while (constructor_stack
->implicit
)
9941 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
9942 && constructor_fields
== NULL_TREE
)
9943 process_init_element (loc
,
9944 pop_init_level (loc
, 1, braced_init_obstack
,
9945 last_init_list_comma
),
9946 true, braced_init_obstack
);
9947 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
9948 || VECTOR_TYPE_P (constructor_type
))
9949 && constructor_max_index
9950 && tree_int_cst_lt (constructor_max_index
,
9952 process_init_element (loc
,
9953 pop_init_level (loc
, 1, braced_init_obstack
,
9954 last_init_list_comma
),
9955 true, braced_init_obstack
);
9960 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9961 if (constructor_range_stack
)
9963 /* If value is a compound literal and we'll be just using its
9964 content, don't put it into a SAVE_EXPR. */
9965 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
9966 || !require_constant_value
)
9968 tree semantic_type
= NULL_TREE
;
9969 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
9971 semantic_type
= TREE_TYPE (value
.value
);
9972 value
.value
= TREE_OPERAND (value
.value
, 0);
9974 value
.value
= save_expr (value
.value
);
9976 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
9983 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9986 enum tree_code fieldcode
;
9988 if (constructor_fields
== NULL_TREE
)
9990 pedwarn_init (loc
, 0, "excess elements in struct initializer");
9994 fieldtype
= TREE_TYPE (constructor_fields
);
9995 if (fieldtype
!= error_mark_node
)
9996 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9997 fieldcode
= TREE_CODE (fieldtype
);
9999 /* Error for non-static initialization of a flexible array member. */
10000 if (fieldcode
== ARRAY_TYPE
10001 && !require_constant_value
10002 && TYPE_SIZE (fieldtype
) == NULL_TREE
10003 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
10005 error_init (loc
, "non-static initialization of a flexible "
10010 /* Error for initialization of a flexible array member with
10011 a string constant if the structure is in an array. E.g.:
10012 struct S { int x; char y[]; };
10013 struct S s[] = { { 1, "foo" } };
10016 && fieldcode
== ARRAY_TYPE
10017 && constructor_depth
> 1
10018 && TYPE_SIZE (fieldtype
) == NULL_TREE
10019 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
10021 bool in_array_p
= false;
10022 for (struct constructor_stack
*p
= constructor_stack
;
10023 p
&& p
->type
; p
= p
->next
)
10024 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
10031 error_init (loc
, "initialization of flexible array "
10032 "member in a nested context");
10037 /* Accept a string constant to initialize a subarray. */
10038 if (value
.value
!= NULL_TREE
10039 && fieldcode
== ARRAY_TYPE
10040 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
10042 value
.value
= orig_value
;
10043 /* Otherwise, if we have come to a subaggregate,
10044 and we don't have an element of its type, push into it. */
10045 else if (value
.value
!= NULL_TREE
10046 && value
.value
!= error_mark_node
10047 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
10048 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
10049 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
10051 push_init_level (loc
, 1, braced_init_obstack
);
10057 push_member_name (constructor_fields
);
10058 output_init_element (loc
, value
.value
, value
.original_type
,
10059 strict_string
, fieldtype
,
10060 constructor_fields
, true, implicit
,
10061 braced_init_obstack
);
10062 RESTORE_SPELLING_DEPTH (constructor_depth
);
10065 /* Do the bookkeeping for an element that was
10066 directly output as a constructor. */
10068 /* For a record, keep track of end position of last field. */
10069 if (DECL_SIZE (constructor_fields
))
10070 constructor_bit_index
10071 = size_binop_loc (input_location
, PLUS_EXPR
,
10072 bit_position (constructor_fields
),
10073 DECL_SIZE (constructor_fields
));
10075 /* If the current field was the first one not yet written out,
10076 it isn't now, so update. */
10077 if (constructor_unfilled_fields
== constructor_fields
)
10079 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
10080 /* Skip any nameless bit fields. */
10081 while (constructor_unfilled_fields
!= 0
10082 && (DECL_UNNAMED_BIT_FIELD
10083 (constructor_unfilled_fields
)))
10084 constructor_unfilled_fields
=
10085 DECL_CHAIN (constructor_unfilled_fields
);
10089 constructor_fields
= DECL_CHAIN (constructor_fields
);
10090 /* Skip any nameless bit fields at the beginning. */
10091 while (constructor_fields
!= NULL_TREE
10092 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
10093 constructor_fields
= DECL_CHAIN (constructor_fields
);
10095 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
10098 enum tree_code fieldcode
;
10100 if (constructor_fields
== NULL_TREE
)
10102 pedwarn_init (loc
, 0,
10103 "excess elements in union initializer");
10107 fieldtype
= TREE_TYPE (constructor_fields
);
10108 if (fieldtype
!= error_mark_node
)
10109 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
10110 fieldcode
= TREE_CODE (fieldtype
);
10112 /* Warn that traditional C rejects initialization of unions.
10113 We skip the warning if the value is zero. This is done
10114 under the assumption that the zero initializer in user
10115 code appears conditioned on e.g. __STDC__ to avoid
10116 "missing initializer" warnings and relies on default
10117 initialization to zero in the traditional C case.
10118 We also skip the warning if the initializer is designated,
10119 again on the assumption that this must be conditional on
10120 __STDC__ anyway (and we've already complained about the
10121 member-designator already). */
10122 if (!in_system_header_at (input_location
) && !constructor_designated
10123 && !(value
.value
&& (integer_zerop (value
.value
)
10124 || real_zerop (value
.value
))))
10125 warning (OPT_Wtraditional
, "traditional C rejects initialization "
10128 /* Accept a string constant to initialize a subarray. */
10129 if (value
.value
!= NULL_TREE
10130 && fieldcode
== ARRAY_TYPE
10131 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
10133 value
.value
= orig_value
;
10134 /* Otherwise, if we have come to a subaggregate,
10135 and we don't have an element of its type, push into it. */
10136 else if (value
.value
!= NULL_TREE
10137 && value
.value
!= error_mark_node
10138 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
10139 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
10140 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
10142 push_init_level (loc
, 1, braced_init_obstack
);
10148 push_member_name (constructor_fields
);
10149 output_init_element (loc
, value
.value
, value
.original_type
,
10150 strict_string
, fieldtype
,
10151 constructor_fields
, true, implicit
,
10152 braced_init_obstack
);
10153 RESTORE_SPELLING_DEPTH (constructor_depth
);
10156 /* Do the bookkeeping for an element that was
10157 directly output as a constructor. */
10159 constructor_bit_index
= DECL_SIZE (constructor_fields
);
10160 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
10163 constructor_fields
= NULL_TREE
;
10165 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
10167 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
10168 enum tree_code eltcode
= TREE_CODE (elttype
);
10170 /* Accept a string constant to initialize a subarray. */
10171 if (value
.value
!= NULL_TREE
10172 && eltcode
== ARRAY_TYPE
10173 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
10175 value
.value
= orig_value
;
10176 /* Otherwise, if we have come to a subaggregate,
10177 and we don't have an element of its type, push into it. */
10178 else if (value
.value
!= NULL_TREE
10179 && value
.value
!= error_mark_node
10180 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
10181 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
10182 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
10184 push_init_level (loc
, 1, braced_init_obstack
);
10188 if (constructor_max_index
!= NULL_TREE
10189 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
10190 || integer_all_onesp (constructor_max_index
)))
10192 pedwarn_init (loc
, 0,
10193 "excess elements in array initializer");
10197 /* Now output the actual element. */
10200 push_array_bounds (tree_to_uhwi (constructor_index
));
10201 output_init_element (loc
, value
.value
, value
.original_type
,
10202 strict_string
, elttype
,
10203 constructor_index
, true, implicit
,
10204 braced_init_obstack
);
10205 RESTORE_SPELLING_DEPTH (constructor_depth
);
10209 = size_binop_loc (input_location
, PLUS_EXPR
,
10210 constructor_index
, bitsize_one_node
);
10213 /* If we are doing the bookkeeping for an element that was
10214 directly output as a constructor, we must update
10215 constructor_unfilled_index. */
10216 constructor_unfilled_index
= constructor_index
;
10218 else if (VECTOR_TYPE_P (constructor_type
))
10220 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
10222 /* Do a basic check of initializer size. Note that vectors
10223 always have a fixed size derived from their type. */
10224 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
10226 pedwarn_init (loc
, 0,
10227 "excess elements in vector initializer");
10231 /* Now output the actual element. */
10234 if (TREE_CODE (value
.value
) == VECTOR_CST
)
10235 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
10236 output_init_element (loc
, value
.value
, value
.original_type
,
10237 strict_string
, elttype
,
10238 constructor_index
, true, implicit
,
10239 braced_init_obstack
);
10243 = size_binop_loc (input_location
,
10244 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
10247 /* If we are doing the bookkeeping for an element that was
10248 directly output as a constructor, we must update
10249 constructor_unfilled_index. */
10250 constructor_unfilled_index
= constructor_index
;
10253 /* Handle the sole element allowed in a braced initializer
10254 for a scalar variable. */
10255 else if (constructor_type
!= error_mark_node
10256 && constructor_fields
== NULL_TREE
)
10258 pedwarn_init (loc
, 0,
10259 "excess elements in scalar initializer");
10265 output_init_element (loc
, value
.value
, value
.original_type
,
10266 strict_string
, constructor_type
,
10267 NULL_TREE
, true, implicit
,
10268 braced_init_obstack
);
10269 constructor_fields
= NULL_TREE
;
10272 /* Handle range initializers either at this level or anywhere higher
10273 in the designator stack. */
10274 if (constructor_range_stack
)
10276 struct constructor_range_stack
*p
, *range_stack
;
10279 range_stack
= constructor_range_stack
;
10280 constructor_range_stack
= 0;
10281 while (constructor_stack
!= range_stack
->stack
)
10283 gcc_assert (constructor_stack
->implicit
);
10284 process_init_element (loc
,
10285 pop_init_level (loc
, 1,
10286 braced_init_obstack
,
10287 last_init_list_comma
),
10288 true, braced_init_obstack
);
10290 for (p
= range_stack
;
10291 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
10294 gcc_assert (constructor_stack
->implicit
);
10295 process_init_element (loc
,
10296 pop_init_level (loc
, 1,
10297 braced_init_obstack
,
10298 last_init_list_comma
),
10299 true, braced_init_obstack
);
10302 p
->index
= size_binop_loc (input_location
,
10303 PLUS_EXPR
, p
->index
, bitsize_one_node
);
10304 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
10309 constructor_index
= p
->index
;
10310 constructor_fields
= p
->fields
;
10311 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
10319 finish_implicit_inits (loc
, braced_init_obstack
);
10320 push_init_level (loc
, 2, braced_init_obstack
);
10321 p
->stack
= constructor_stack
;
10322 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
10323 p
->index
= p
->range_start
;
10327 constructor_range_stack
= range_stack
;
10334 constructor_range_stack
= 0;
10337 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10338 (guaranteed to be 'volatile' or null) and ARGS (represented using
10339 an ASM_EXPR node). */
10341 build_asm_stmt (bool is_volatile
, tree args
)
10344 ASM_VOLATILE_P (args
) = 1;
10345 return add_stmt (args
);
10348 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10349 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10350 SIMPLE indicates whether there was anything at all after the
10351 string in the asm expression -- asm("blah") and asm("blah" : )
10352 are subtly different. We use a ASM_EXPR node to represent this.
10353 LOC is the location of the asm, and IS_INLINE says whether this
10356 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
10357 tree clobbers
, tree labels
, bool simple
, bool is_inline
)
10362 const char *constraint
;
10363 const char **oconstraints
;
10364 bool allows_mem
, allows_reg
, is_inout
;
10365 int ninputs
, noutputs
;
10367 ninputs
= list_length (inputs
);
10368 noutputs
= list_length (outputs
);
10369 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
10371 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
10373 /* Remove output conversions that change the type but not the mode. */
10374 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
10376 tree output
= TREE_VALUE (tail
);
10378 output
= c_fully_fold (output
, false, NULL
, true);
10380 /* ??? Really, this should not be here. Users should be using a
10381 proper lvalue, dammit. But there's a long history of using casts
10382 in the output operands. In cases like longlong.h, this becomes a
10383 primitive form of typechecking -- if the cast can be removed, then
10384 the output operand had a type of the proper width; otherwise we'll
10385 get an error. Gross, but ... */
10386 STRIP_NOPS (output
);
10388 if (!lvalue_or_else (loc
, output
, lv_asm
))
10389 output
= error_mark_node
;
10391 if (output
!= error_mark_node
10392 && (TREE_READONLY (output
)
10393 || TYPE_READONLY (TREE_TYPE (output
))
10394 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output
))
10395 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
10396 readonly_error (loc
, output
, lv_asm
);
10398 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
10399 oconstraints
[i
] = constraint
;
10401 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
10402 &allows_mem
, &allows_reg
, &is_inout
))
10404 /* If the operand is going to end up in memory,
10405 mark it addressable. */
10406 if (!allows_reg
&& !c_mark_addressable (output
))
10407 output
= error_mark_node
;
10408 if (!(!allows_reg
&& allows_mem
)
10409 && output
!= error_mark_node
10410 && VOID_TYPE_P (TREE_TYPE (output
)))
10412 error_at (loc
, "invalid use of void expression");
10413 output
= error_mark_node
;
10417 output
= error_mark_node
;
10419 TREE_VALUE (tail
) = output
;
10422 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
10426 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
10427 input
= TREE_VALUE (tail
);
10429 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
10430 oconstraints
, &allows_mem
, &allows_reg
))
10432 /* If the operand is going to end up in memory,
10433 mark it addressable. */
10434 if (!allows_reg
&& allows_mem
)
10436 input
= c_fully_fold (input
, false, NULL
, true);
10438 /* Strip the nops as we allow this case. FIXME, this really
10439 should be rejected or made deprecated. */
10440 STRIP_NOPS (input
);
10441 if (!c_mark_addressable (input
))
10442 input
= error_mark_node
;
10446 struct c_expr expr
;
10447 memset (&expr
, 0, sizeof (expr
));
10448 expr
.value
= input
;
10449 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
10450 input
= c_fully_fold (expr
.value
, false, NULL
);
10452 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
10454 error_at (loc
, "invalid use of void expression");
10455 input
= error_mark_node
;
10460 input
= error_mark_node
;
10462 TREE_VALUE (tail
) = input
;
10465 /* ASMs with labels cannot have outputs. This should have been
10466 enforced by the parser. */
10467 gcc_assert (outputs
== NULL
|| labels
== NULL
);
10469 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
10471 /* asm statements without outputs, including simple ones, are treated
10473 ASM_INPUT_P (args
) = simple
;
10474 ASM_VOLATILE_P (args
) = (noutputs
== 0);
10475 ASM_INLINE_P (args
) = is_inline
;
10480 /* Generate a goto statement to LABEL. LOC is the location of the
10484 c_finish_goto_label (location_t loc
, tree label
)
10486 tree decl
= lookup_label_for_goto (loc
, label
);
10489 TREE_USED (decl
) = 1;
10491 add_stmt (build_predict_expr (PRED_GOTO
, NOT_TAKEN
));
10492 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
10493 SET_EXPR_LOCATION (t
, loc
);
10494 return add_stmt (t
);
10498 /* Generate a computed goto statement to EXPR. LOC is the location of
10502 c_finish_goto_ptr (location_t loc
, tree expr
)
10505 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
10506 expr
= c_fully_fold (expr
, false, NULL
);
10507 expr
= convert (ptr_type_node
, expr
);
10508 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
10509 SET_EXPR_LOCATION (t
, loc
);
10510 return add_stmt (t
);
10513 /* Generate a C `return' statement. RETVAL is the expression for what
10514 to return, or a null pointer for `return;' with no value. LOC is
10515 the location of the return statement, or the location of the expression,
10516 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10517 is the original type of RETVAL. */
10520 c_finish_return (location_t loc
, tree retval
, tree origtype
)
10522 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
10523 bool no_warning
= false;
10526 /* Use the expansion point to handle cases such as returning NULL
10527 in a function returning void. */
10528 location_t xloc
= expansion_point_location_if_in_system_header (loc
);
10530 if (TREE_THIS_VOLATILE (current_function_decl
))
10531 warning_at (xloc
, 0,
10532 "function declared %<noreturn%> has a %<return%> statement");
10536 tree semantic_type
= NULL_TREE
;
10537 npc
= null_pointer_constant_p (retval
);
10538 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
10540 semantic_type
= TREE_TYPE (retval
);
10541 retval
= TREE_OPERAND (retval
, 0);
10543 retval
= c_fully_fold (retval
, false, NULL
);
10545 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
10550 current_function_returns_null
= 1;
10551 if ((warn_return_type
>= 0 || flag_isoc99
)
10552 && valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
10556 warned_here
= pedwarn
10557 (loc
, warn_return_type
>= 0 ? OPT_Wreturn_type
: 0,
10558 "%<return%> with no value, in function returning non-void");
10560 warned_here
= warning_at
10561 (loc
, OPT_Wreturn_type
,
10562 "%<return%> with no value, in function returning non-void");
10565 inform (DECL_SOURCE_LOCATION (current_function_decl
),
10569 else if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
10571 current_function_returns_null
= 1;
10573 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
10574 warned_here
= pedwarn
10575 (xloc
, warn_return_type
>= 0 ? OPT_Wreturn_type
: 0,
10576 "%<return%> with a value, in function returning void");
10578 warned_here
= pedwarn
10579 (xloc
, OPT_Wpedantic
, "ISO C forbids "
10580 "%<return%> with expression, in function returning void");
10582 inform (DECL_SOURCE_LOCATION (current_function_decl
),
10587 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
10588 retval
, origtype
, ic_return
,
10589 npc
, NULL_TREE
, NULL_TREE
, 0);
10590 tree res
= DECL_RESULT (current_function_decl
);
10594 current_function_returns_value
= 1;
10595 if (t
== error_mark_node
)
10598 save
= in_late_binary_op
;
10599 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
10600 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
10601 || (TREE_CODE (TREE_TYPE (t
)) == REAL_TYPE
10602 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
10603 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
10604 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
10605 in_late_binary_op
= true;
10606 inner
= t
= convert (TREE_TYPE (res
), t
);
10607 in_late_binary_op
= save
;
10609 /* Strip any conversions, additions, and subtractions, and see if
10610 we are returning the address of a local variable. Warn if so. */
10613 switch (TREE_CODE (inner
))
10616 case NON_LVALUE_EXPR
:
10618 case POINTER_PLUS_EXPR
:
10619 inner
= TREE_OPERAND (inner
, 0);
10623 /* If the second operand of the MINUS_EXPR has a pointer
10624 type (or is converted from it), this may be valid, so
10625 don't give a warning. */
10627 tree op1
= TREE_OPERAND (inner
, 1);
10629 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
10630 && (CONVERT_EXPR_P (op1
)
10631 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
10632 op1
= TREE_OPERAND (op1
, 0);
10634 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
10637 inner
= TREE_OPERAND (inner
, 0);
10642 inner
= TREE_OPERAND (inner
, 0);
10644 while (REFERENCE_CLASS_P (inner
)
10645 && !INDIRECT_REF_P (inner
))
10646 inner
= TREE_OPERAND (inner
, 0);
10649 && !DECL_EXTERNAL (inner
)
10650 && !TREE_STATIC (inner
)
10651 && DECL_CONTEXT (inner
) == current_function_decl
10652 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
10654 if (TREE_CODE (inner
) == LABEL_DECL
)
10655 warning_at (loc
, OPT_Wreturn_local_addr
,
10656 "function returns address of label");
10659 warning_at (loc
, OPT_Wreturn_local_addr
,
10660 "function returns address of local variable");
10661 tree zero
= build_zero_cst (TREE_TYPE (res
));
10662 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
10674 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
10675 SET_EXPR_LOCATION (retval
, loc
);
10677 if (warn_sequence_point
)
10678 verify_sequence_points (retval
);
10681 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
10682 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
10683 return add_stmt (ret_stmt
);
10687 /* The SWITCH_EXPR being built. */
10690 /* The original type of the testing expression, i.e. before the
10691 default conversion is applied. */
10694 /* A splay-tree mapping the low element of a case range to the high
10695 element, or NULL_TREE if there is no high element. Used to
10696 determine whether or not a new case label duplicates an old case
10697 label. We need a tree, rather than simply a hash table, because
10698 of the GNU case range extension. */
10701 /* The bindings at the point of the switch. This is used for
10702 warnings crossing decls when branching to a case label. */
10703 struct c_spot_bindings
*bindings
;
10705 /* The next node on the stack. */
10706 struct c_switch
*next
;
10708 /* Remember whether the controlling expression had boolean type
10709 before integer promotions for the sake of -Wswitch-bool. */
10713 /* A stack of the currently active switch statements. The innermost
10714 switch statement is on the top of the stack. There is no need to
10715 mark the stack for garbage collection because it is only active
10716 during the processing of the body of a function, and we never
10717 collect at that point. */
10719 struct c_switch
*c_switch_stack
;
10721 /* Start a C switch statement, testing expression EXP. Return the new
10722 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10723 SWITCH_COND_LOC is the location of the switch's condition.
10724 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10727 c_start_case (location_t switch_loc
,
10728 location_t switch_cond_loc
,
10729 tree exp
, bool explicit_cast_p
)
10731 tree orig_type
= error_mark_node
;
10732 bool bool_cond_p
= false;
10733 struct c_switch
*cs
;
10735 if (exp
!= error_mark_node
)
10737 orig_type
= TREE_TYPE (exp
);
10739 if (!INTEGRAL_TYPE_P (orig_type
))
10741 if (orig_type
!= error_mark_node
)
10743 error_at (switch_cond_loc
, "switch quantity not an integer");
10744 orig_type
= error_mark_node
;
10746 exp
= integer_zero_node
;
10750 tree type
= TYPE_MAIN_VARIANT (orig_type
);
10753 /* Warn if the condition has boolean value. */
10754 while (TREE_CODE (e
) == COMPOUND_EXPR
)
10755 e
= TREE_OPERAND (e
, 1);
10757 if ((TREE_CODE (type
) == BOOLEAN_TYPE
10758 || truth_value_p (TREE_CODE (e
)))
10759 /* Explicit cast to int suppresses this warning. */
10760 && !(TREE_CODE (type
) == INTEGER_TYPE
10761 && explicit_cast_p
))
10762 bool_cond_p
= true;
10764 if (!in_system_header_at (input_location
)
10765 && (type
== long_integer_type_node
10766 || type
== long_unsigned_type_node
))
10767 warning_at (switch_cond_loc
,
10768 OPT_Wtraditional
, "%<long%> switch expression not "
10769 "converted to %<int%> in ISO C");
10771 exp
= c_fully_fold (exp
, false, NULL
);
10772 exp
= default_conversion (exp
);
10774 if (warn_sequence_point
)
10775 verify_sequence_points (exp
);
10779 /* Add this new SWITCH_EXPR to the stack. */
10780 cs
= XNEW (struct c_switch
);
10781 cs
->switch_expr
= build2 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
);
10782 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
10783 cs
->orig_type
= orig_type
;
10784 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
10785 cs
->bindings
= c_get_switch_bindings ();
10786 cs
->bool_cond_p
= bool_cond_p
;
10787 cs
->next
= c_switch_stack
;
10788 c_switch_stack
= cs
;
10790 return add_stmt (cs
->switch_expr
);
10793 /* Process a case label at location LOC. */
10796 do_case (location_t loc
, tree low_value
, tree high_value
)
10798 tree label
= NULL_TREE
;
10800 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
10802 low_value
= c_fully_fold (low_value
, false, NULL
);
10803 if (TREE_CODE (low_value
) == INTEGER_CST
)
10804 pedwarn (loc
, OPT_Wpedantic
,
10805 "case label is not an integer constant expression");
10808 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
10810 high_value
= c_fully_fold (high_value
, false, NULL
);
10811 if (TREE_CODE (high_value
) == INTEGER_CST
)
10812 pedwarn (input_location
, OPT_Wpedantic
,
10813 "case label is not an integer constant expression");
10816 if (c_switch_stack
== NULL
)
10819 error_at (loc
, "case label not within a switch statement");
10821 error_at (loc
, "%<default%> label not within a switch statement");
10825 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
10826 EXPR_LOCATION (c_switch_stack
->switch_expr
),
10830 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
10831 SWITCH_COND (c_switch_stack
->switch_expr
),
10832 low_value
, high_value
);
10833 if (label
== error_mark_node
)
10838 /* Finish the switch statement. TYPE is the original type of the
10839 controlling expression of the switch, or NULL_TREE. */
10842 c_finish_case (tree body
, tree type
)
10844 struct c_switch
*cs
= c_switch_stack
;
10845 location_t switch_location
;
10847 SWITCH_BODY (cs
->switch_expr
) = body
;
10849 /* Emit warnings as needed. */
10850 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
10851 c_do_switch_warnings (cs
->cases
, switch_location
,
10852 type
? type
: TREE_TYPE (cs
->switch_expr
),
10853 SWITCH_COND (cs
->switch_expr
), cs
->bool_cond_p
);
10854 if (c_switch_covers_all_cases_p (cs
->cases
, TREE_TYPE (cs
->switch_expr
)))
10855 SWITCH_ALL_CASES_P (cs
->switch_expr
) = 1;
10857 /* Pop the stack. */
10858 c_switch_stack
= cs
->next
;
10859 splay_tree_delete (cs
->cases
);
10860 c_release_switch_bindings (cs
->bindings
);
10864 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10865 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10869 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
10874 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
10875 SET_EXPR_LOCATION (stmt
, if_locus
);
10879 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10880 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10881 is false for DO loops. INCR is the FOR increment expression. BODY is
10882 the statement controlled by the loop. BLAB is the break label. CLAB is
10883 the continue label. Everything is allowed to be NULL.
10884 COND_LOCUS is the location of the loop condition, INCR_LOCUS is the
10885 location of the FOR increment expression. */
10888 c_finish_loop (location_t start_locus
, location_t cond_locus
, tree cond
,
10889 location_t incr_locus
, tree incr
, tree body
, tree blab
,
10890 tree clab
, bool cond_is_first
)
10892 tree entry
= NULL
, exit
= NULL
, t
;
10894 /* If the condition is zero don't generate a loop construct. */
10895 if (cond
&& integer_zerop (cond
))
10899 t
= build_and_jump (&blab
);
10900 SET_EXPR_LOCATION (t
, start_locus
);
10906 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10908 /* If we have an exit condition, then we build an IF with gotos either
10909 out of the loop, or to the top of it. If there's no exit condition,
10910 then we just build a jump back to the top. */
10911 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
10913 if (cond
&& !integer_nonzerop (cond
))
10915 /* Canonicalize the loop condition to the end. This means
10916 generating a branch to the loop condition. Reuse the
10917 continue label, if possible. */
10922 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10923 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
10926 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
10927 SET_EXPR_LOCATION (t
, start_locus
);
10931 t
= build_and_jump (&blab
);
10932 exit
= fold_build3_loc (cond_is_first
? start_locus
: input_location
,
10933 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10937 /* For the backward-goto's location of an unconditional loop
10938 use the beginning of the body, or, if there is none, the
10939 top of the loop. */
10940 location_t loc
= EXPR_LOCATION (expr_first (body
));
10941 if (loc
== UNKNOWN_LOCATION
)
10943 SET_EXPR_LOCATION (exit
, loc
);
10952 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
10955 if (MAY_HAVE_DEBUG_MARKER_STMTS
&& incr_locus
!= UNKNOWN_LOCATION
)
10957 t
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10958 SET_EXPR_LOCATION (t
, incr_locus
);
10965 if (MAY_HAVE_DEBUG_MARKER_STMTS
&& cond_locus
!= UNKNOWN_LOCATION
)
10967 t
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10968 SET_EXPR_LOCATION (t
, cond_locus
);
10974 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
10978 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
10981 tree label
= *label_p
;
10983 /* In switch statements break is sometimes stylistically used after
10984 a return statement. This can lead to spurious warnings about
10985 control reaching the end of a non-void function when it is
10986 inlined. Note that we are calling block_may_fallthru with
10987 language specific tree nodes; this works because
10988 block_may_fallthru returns true when given something it does not
10990 skip
= !block_may_fallthru (cur_stmt_list
);
10995 *label_p
= label
= create_artificial_label (loc
);
10997 else if (TREE_CODE (label
) == LABEL_DECL
)
10999 else switch (TREE_INT_CST_LOW (label
))
11003 error_at (loc
, "break statement not within loop or switch");
11005 error_at (loc
, "continue statement not within a loop");
11009 gcc_assert (is_break
);
11010 error_at (loc
, "break statement used with OpenMP for loop");
11015 error ("break statement within %<#pragma simd%> loop body");
11017 error ("continue statement within %<#pragma simd%> loop body");
11021 gcc_unreachable ();
11028 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
11030 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
11033 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11036 emit_side_effect_warnings (location_t loc
, tree expr
)
11038 if (expr
== error_mark_node
)
11040 else if (!TREE_SIDE_EFFECTS (expr
))
11042 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
11043 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
11045 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
11048 location_t cloc
= loc
;
11049 while (TREE_CODE (r
) == COMPOUND_EXPR
)
11051 if (EXPR_HAS_LOCATION (r
))
11052 cloc
= EXPR_LOCATION (r
);
11053 r
= TREE_OPERAND (r
, 1);
11055 if (!TREE_SIDE_EFFECTS (r
)
11056 && !VOID_TYPE_P (TREE_TYPE (r
))
11057 && !CONVERT_EXPR_P (r
)
11058 && !TREE_NO_WARNING (r
)
11059 && !TREE_NO_WARNING (expr
))
11060 warning_at (cloc
, OPT_Wunused_value
,
11061 "right-hand operand of comma expression has no effect");
11064 warn_if_unused_value (expr
, loc
);
11067 /* Process an expression as if it were a complete statement. Emit
11068 diagnostics, but do not call ADD_STMT. LOC is the location of the
11072 c_process_expr_stmt (location_t loc
, tree expr
)
11079 expr
= c_fully_fold (expr
, false, NULL
);
11081 if (warn_sequence_point
)
11082 verify_sequence_points (expr
);
11084 if (TREE_TYPE (expr
) != error_mark_node
11085 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
11086 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
11087 error_at (loc
, "expression statement has incomplete type");
11089 /* If we're not processing a statement expression, warn about unused values.
11090 Warnings for statement expressions will be emitted later, once we figure
11091 out which is the result. */
11092 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
11093 && warn_unused_value
)
11094 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr
, loc
), expr
);
11097 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
11098 exprv
= TREE_OPERAND (exprv
, 1);
11099 while (CONVERT_EXPR_P (exprv
))
11100 exprv
= TREE_OPERAND (exprv
, 0);
11102 || handled_component_p (exprv
)
11103 || TREE_CODE (exprv
) == ADDR_EXPR
)
11104 mark_exp_read (exprv
);
11106 /* If the expression is not of a type to which we cannot assign a line
11107 number, wrap the thing in a no-op NOP_EXPR. */
11108 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
11110 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11111 SET_EXPR_LOCATION (expr
, loc
);
11117 /* Emit an expression as a statement. LOC is the location of the
11121 c_finish_expr_stmt (location_t loc
, tree expr
)
11124 return add_stmt (c_process_expr_stmt (loc
, expr
));
11129 /* Do the opposite and emit a statement as an expression. To begin,
11130 create a new binding level and return it. */
11133 c_begin_stmt_expr (void)
11137 /* We must force a BLOCK for this level so that, if it is not expanded
11138 later, there is a way to turn off the entire subtree of blocks that
11139 are contained in it. */
11140 keep_next_level ();
11141 ret
= c_begin_compound_stmt (true);
11143 c_bindings_start_stmt_expr (c_switch_stack
== NULL
11145 : c_switch_stack
->bindings
);
11147 /* Mark the current statement list as belonging to a statement list. */
11148 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
11153 /* LOC is the location of the compound statement to which this body
11157 c_finish_stmt_expr (location_t loc
, tree body
)
11159 tree last
, type
, tmp
, val
;
11162 body
= c_end_compound_stmt (loc
, body
, true);
11164 c_bindings_end_stmt_expr (c_switch_stack
== NULL
11166 : c_switch_stack
->bindings
);
11168 /* Locate the last statement in BODY. See c_end_compound_stmt
11169 about always returning a BIND_EXPR. */
11170 last_p
= &BIND_EXPR_BODY (body
);
11171 last
= BIND_EXPR_BODY (body
);
11173 continue_searching
:
11174 if (TREE_CODE (last
) == STATEMENT_LIST
)
11176 tree_stmt_iterator l
= tsi_last (last
);
11178 while (!tsi_end_p (l
) && TREE_CODE (tsi_stmt (l
)) == DEBUG_BEGIN_STMT
)
11181 /* This can happen with degenerate cases like ({ }). No value. */
11185 /* If we're supposed to generate side effects warnings, process
11186 all of the statements except the last. */
11187 if (warn_unused_value
)
11189 for (tree_stmt_iterator i
= tsi_start (last
);
11190 tsi_stmt (i
) != tsi_stmt (l
); tsi_next (&i
))
11193 tree t
= tsi_stmt (i
);
11195 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
11196 emit_side_effect_warnings (tloc
, t
);
11199 last_p
= tsi_stmt_ptr (l
);
11203 /* If the end of the list is exception related, then the list was split
11204 by a call to push_cleanup. Continue searching. */
11205 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
11206 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
11208 last_p
= &TREE_OPERAND (last
, 0);
11210 goto continue_searching
;
11213 if (last
== error_mark_node
)
11216 /* In the case that the BIND_EXPR is not necessary, return the
11217 expression out from inside it. */
11218 if ((last
== BIND_EXPR_BODY (body
)
11219 /* Skip nested debug stmts. */
11220 || last
== expr_first (BIND_EXPR_BODY (body
)))
11221 && BIND_EXPR_VARS (body
) == NULL
)
11223 /* Even if this looks constant, do not allow it in a constant
11225 last
= c_wrap_maybe_const (last
, true);
11226 /* Do not warn if the return value of a statement expression is
11228 TREE_NO_WARNING (last
) = 1;
11232 /* Extract the type of said expression. */
11233 type
= TREE_TYPE (last
);
11235 /* If we're not returning a value at all, then the BIND_EXPR that
11236 we already have is a fine expression to return. */
11237 if (!type
|| VOID_TYPE_P (type
))
11240 /* Now that we've located the expression containing the value, it seems
11241 silly to make voidify_wrapper_expr repeat the process. Create a
11242 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11243 tmp
= create_tmp_var_raw (type
);
11245 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11246 tree_expr_nonnegative_p giving up immediately. */
11248 if (TREE_CODE (val
) == NOP_EXPR
11249 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
11250 val
= TREE_OPERAND (val
, 0);
11252 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
11253 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
11256 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
11257 SET_EXPR_LOCATION (t
, loc
);
11262 /* Begin and end compound statements. This is as simple as pushing
11263 and popping new statement lists from the tree. */
11266 c_begin_compound_stmt (bool do_scope
)
11268 tree stmt
= push_stmt_list ();
11274 /* End a compound statement. STMT is the statement. LOC is the
11275 location of the compound statement-- this is usually the location
11276 of the opening brace. */
11279 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
11285 if (c_dialect_objc ())
11286 objc_clear_super_receiver ();
11287 block
= pop_scope ();
11290 stmt
= pop_stmt_list (stmt
);
11291 stmt
= c_build_bind_expr (loc
, block
, stmt
);
11293 /* If this compound statement is nested immediately inside a statement
11294 expression, then force a BIND_EXPR to be created. Otherwise we'll
11295 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11296 STATEMENT_LISTs merge, and thus we can lose track of what statement
11297 was really last. */
11298 if (building_stmt_list_p ()
11299 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
11300 && TREE_CODE (stmt
) != BIND_EXPR
)
11302 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
11303 TREE_SIDE_EFFECTS (stmt
) = 1;
11304 SET_EXPR_LOCATION (stmt
, loc
);
11310 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11311 when the current scope is exited. EH_ONLY is true when this is not
11312 meant to apply to normal control flow transfer. */
11315 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
11317 enum tree_code code
;
11321 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
11322 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
11324 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
11325 list
= push_stmt_list ();
11326 TREE_OPERAND (stmt
, 0) = list
;
11327 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
11330 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11331 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11334 build_vec_cmp (tree_code code
, tree type
,
11335 tree arg0
, tree arg1
)
11337 tree zero_vec
= build_zero_cst (type
);
11338 tree minus_one_vec
= build_minus_one_cst (type
);
11339 tree cmp_type
= build_same_sized_truth_vector_type (type
);
11340 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
11341 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
11344 /* Build a binary-operation expression without default conversions.
11345 CODE is the kind of expression to build.
11346 LOCATION is the operator's location.
11347 This function differs from `build' in several ways:
11348 the data type of the result is computed and recorded in it,
11349 warnings are generated if arg data types are invalid,
11350 special handling for addition and subtraction of pointers is known,
11351 and some optimization is done (operations on narrow ints
11352 are done in the narrower type when that gives the same result).
11353 Constant folding is also done before the result is returned.
11355 Note that the operands will never have enumeral types, or function
11356 or array types, because either they will have the default conversions
11357 performed or they have both just been converted to some other type in which
11358 the arithmetic is to be done. */
11361 build_binary_op (location_t location
, enum tree_code code
,
11362 tree orig_op0
, tree orig_op1
, bool convert_p
)
11364 tree type0
, type1
, orig_type0
, orig_type1
;
11366 enum tree_code code0
, code1
;
11368 tree ret
= error_mark_node
;
11369 const char *invalid_op_diag
;
11370 bool op0_int_operands
, op1_int_operands
;
11371 bool int_const
, int_const_or_overflow
, int_operands
;
11373 /* Expression code to give to the expression when it is built.
11374 Normally this is CODE, which is what the caller asked for,
11375 but in some special cases we change it. */
11376 enum tree_code resultcode
= code
;
11378 /* Data type in which the computation is to be performed.
11379 In the simplest cases this is the common type of the arguments. */
11380 tree result_type
= NULL
;
11382 /* When the computation is in excess precision, the type of the
11383 final EXCESS_PRECISION_EXPR. */
11384 tree semantic_result_type
= NULL
;
11386 /* Nonzero means operands have already been type-converted
11387 in whatever way is necessary.
11388 Zero means they need to be converted to RESULT_TYPE. */
11391 /* Nonzero means create the expression with this type, rather than
11393 tree build_type
= NULL_TREE
;
11395 /* Nonzero means after finally constructing the expression
11396 convert it to this type. */
11397 tree final_type
= NULL_TREE
;
11399 /* Nonzero if this is an operation like MIN or MAX which can
11400 safely be computed in short if both args are promoted shorts.
11401 Also implies COMMON.
11402 -1 indicates a bitwise operation; this makes a difference
11403 in the exact conditions for when it is safe to do the operation
11404 in a narrower mode. */
11407 /* Nonzero if this is a comparison operation;
11408 if both args are promoted shorts, compare the original shorts.
11409 Also implies COMMON. */
11410 int short_compare
= 0;
11412 /* Nonzero if this is a right-shift operation, which can be computed on the
11413 original short and then promoted if the operand is a promoted short. */
11414 int short_shift
= 0;
11416 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11419 /* True means types are compatible as far as ObjC is concerned. */
11422 /* True means this is an arithmetic operation that may need excess
11424 bool may_need_excess_precision
;
11426 /* True means this is a boolean operation that converts both its
11427 operands to truth-values. */
11428 bool boolean_op
= false;
11430 /* Remember whether we're doing / or %. */
11431 bool doing_div_or_mod
= false;
11433 /* Remember whether we're doing << or >>. */
11434 bool doing_shift
= false;
11436 /* Tree holding instrumentation expression. */
11437 tree instrument_expr
= NULL
;
11439 if (location
== UNKNOWN_LOCATION
)
11440 location
= input_location
;
11445 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
11446 if (op0_int_operands
)
11447 op0
= remove_c_maybe_const_expr (op0
);
11448 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
11449 if (op1_int_operands
)
11450 op1
= remove_c_maybe_const_expr (op1
);
11451 int_operands
= (op0_int_operands
&& op1_int_operands
);
11454 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
11455 && TREE_CODE (orig_op1
) == INTEGER_CST
);
11456 int_const
= (int_const_or_overflow
11457 && !TREE_OVERFLOW (orig_op0
)
11458 && !TREE_OVERFLOW (orig_op1
));
11461 int_const
= int_const_or_overflow
= false;
11463 /* Do not apply default conversion in mixed vector/scalar expression. */
11465 && VECTOR_TYPE_P (TREE_TYPE (op0
)) == VECTOR_TYPE_P (TREE_TYPE (op1
)))
11467 op0
= default_conversion (op0
);
11468 op1
= default_conversion (op1
);
11471 orig_type0
= type0
= TREE_TYPE (op0
);
11473 orig_type1
= type1
= TREE_TYPE (op1
);
11475 /* The expression codes of the data types of the arguments tell us
11476 whether the arguments are integers, floating, pointers, etc. */
11477 code0
= TREE_CODE (type0
);
11478 code1
= TREE_CODE (type1
);
11480 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11481 STRIP_TYPE_NOPS (op0
);
11482 STRIP_TYPE_NOPS (op1
);
11484 /* If an error was already reported for one of the arguments,
11485 avoid reporting another error. */
11487 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
11488 return error_mark_node
;
11490 if (code0
== POINTER_TYPE
11491 && reject_gcc_builtin (op0
, EXPR_LOCATION (orig_op0
)))
11492 return error_mark_node
;
11494 if (code1
== POINTER_TYPE
11495 && reject_gcc_builtin (op1
, EXPR_LOCATION (orig_op1
)))
11496 return error_mark_node
;
11498 if ((invalid_op_diag
11499 = targetm
.invalid_binary_op (code
, type0
, type1
)))
11501 error_at (location
, invalid_op_diag
);
11502 return error_mark_node
;
11510 case TRUNC_DIV_EXPR
:
11511 case CEIL_DIV_EXPR
:
11512 case FLOOR_DIV_EXPR
:
11513 case ROUND_DIV_EXPR
:
11514 case EXACT_DIV_EXPR
:
11515 may_need_excess_precision
= true;
11524 /* Excess precision for implicit conversions of integers to
11525 floating point in C11 and later. */
11526 may_need_excess_precision
= (flag_isoc11
11527 && (ANY_INTEGRAL_TYPE_P (type0
)
11528 || ANY_INTEGRAL_TYPE_P (type1
)));
11532 may_need_excess_precision
= false;
11535 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
11537 op0
= TREE_OPERAND (op0
, 0);
11538 type0
= TREE_TYPE (op0
);
11540 else if (may_need_excess_precision
11541 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
11544 op0
= convert (eptype
, op0
);
11546 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
11548 op1
= TREE_OPERAND (op1
, 0);
11549 type1
= TREE_TYPE (op1
);
11551 else if (may_need_excess_precision
11552 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
11555 op1
= convert (eptype
, op1
);
11558 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
11560 /* In case when one of the operands of the binary operation is
11561 a vector and another is a scalar -- convert scalar to vector. */
11562 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
11564 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
11567 switch (convert_flag
)
11570 return error_mark_node
;
11573 bool maybe_const
= true;
11575 sc
= c_fully_fold (op0
, false, &maybe_const
);
11576 sc
= save_expr (sc
);
11577 sc
= convert (TREE_TYPE (type1
), sc
);
11578 op0
= build_vector_from_val (type1
, sc
);
11580 op0
= c_wrap_maybe_const (op0
, true);
11581 orig_type0
= type0
= TREE_TYPE (op0
);
11582 code0
= TREE_CODE (type0
);
11586 case stv_secondarg
:
11588 bool maybe_const
= true;
11590 sc
= c_fully_fold (op1
, false, &maybe_const
);
11591 sc
= save_expr (sc
);
11592 sc
= convert (TREE_TYPE (type0
), sc
);
11593 op1
= build_vector_from_val (type0
, sc
);
11595 op1
= c_wrap_maybe_const (op1
, true);
11596 orig_type1
= type1
= TREE_TYPE (op1
);
11597 code1
= TREE_CODE (type1
);
11609 /* Handle the pointer + int case. */
11610 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11612 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
11613 goto return_build_binary_op
;
11615 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
11617 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
11618 goto return_build_binary_op
;
11625 /* Subtraction of two similar pointers.
11626 We must subtract them as integers, then divide by object size. */
11627 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
11628 && comp_target_types (location
, type0
, type1
))
11630 ret
= pointer_diff (location
, op0
, op1
, &instrument_expr
);
11631 goto return_build_binary_op
;
11633 /* Handle pointer minus int. Just like pointer plus int. */
11634 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11636 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
11637 goto return_build_binary_op
;
11647 case TRUNC_DIV_EXPR
:
11648 case CEIL_DIV_EXPR
:
11649 case FLOOR_DIV_EXPR
:
11650 case ROUND_DIV_EXPR
:
11651 case EXACT_DIV_EXPR
:
11652 doing_div_or_mod
= true;
11653 warn_for_div_by_zero (location
, op1
);
11655 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11656 || code0
== FIXED_POINT_TYPE
11657 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
11658 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11659 || code1
== FIXED_POINT_TYPE
11660 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
11662 enum tree_code tcode0
= code0
, tcode1
= code1
;
11664 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
11665 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
11666 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
11667 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
11669 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
11670 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
11671 resultcode
= RDIV_EXPR
;
11673 /* Although it would be tempting to shorten always here, that
11674 loses on some targets, since the modulo instruction is
11675 undefined if the quotient can't be represented in the
11676 computation mode. We shorten only if unsigned or if
11677 dividing by something we know != -1. */
11678 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
11679 || (TREE_CODE (op1
) == INTEGER_CST
11680 && !integer_all_onesp (op1
)));
11688 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
11690 /* Allow vector types which are not floating point types. */
11691 else if (code0
== VECTOR_TYPE
11692 && code1
== VECTOR_TYPE
11693 && !VECTOR_FLOAT_TYPE_P (type0
)
11694 && !VECTOR_FLOAT_TYPE_P (type1
))
11698 case TRUNC_MOD_EXPR
:
11699 case FLOOR_MOD_EXPR
:
11700 doing_div_or_mod
= true;
11701 warn_for_div_by_zero (location
, op1
);
11703 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11704 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11705 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
11707 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
11709 /* Although it would be tempting to shorten always here, that loses
11710 on some targets, since the modulo instruction is undefined if the
11711 quotient can't be represented in the computation mode. We shorten
11712 only if unsigned or if dividing by something we know != -1. */
11713 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
11714 || (TREE_CODE (op1
) == INTEGER_CST
11715 && !integer_all_onesp (op1
)));
11720 case TRUTH_ANDIF_EXPR
:
11721 case TRUTH_ORIF_EXPR
:
11722 case TRUTH_AND_EXPR
:
11723 case TRUTH_OR_EXPR
:
11724 case TRUTH_XOR_EXPR
:
11725 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
11726 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11727 || code0
== FIXED_POINT_TYPE
)
11728 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
11729 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11730 || code1
== FIXED_POINT_TYPE
))
11732 /* Result of these operations is always an int,
11733 but that does not mean the operands should be
11734 converted to ints! */
11735 result_type
= integer_type_node
;
11736 if (op0_int_operands
)
11738 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
11739 op0
= remove_c_maybe_const_expr (op0
);
11742 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
11743 if (op1_int_operands
)
11745 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
11746 op1
= remove_c_maybe_const_expr (op1
);
11749 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
11753 if (code
== TRUTH_ANDIF_EXPR
)
11755 int_const_or_overflow
= (int_operands
11756 && TREE_CODE (orig_op0
) == INTEGER_CST
11757 && (op0
== truthvalue_false_node
11758 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11759 int_const
= (int_const_or_overflow
11760 && !TREE_OVERFLOW (orig_op0
)
11761 && (op0
== truthvalue_false_node
11762 || !TREE_OVERFLOW (orig_op1
)));
11764 else if (code
== TRUTH_ORIF_EXPR
)
11766 int_const_or_overflow
= (int_operands
11767 && TREE_CODE (orig_op0
) == INTEGER_CST
11768 && (op0
== truthvalue_true_node
11769 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11770 int_const
= (int_const_or_overflow
11771 && !TREE_OVERFLOW (orig_op0
)
11772 && (op0
== truthvalue_true_node
11773 || !TREE_OVERFLOW (orig_op1
)));
11777 /* Shift operations: result has same type as first operand;
11778 always convert second operand to int.
11779 Also set SHORT_SHIFT if shifting rightward. */
11782 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11783 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11784 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11785 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
11786 TYPE_VECTOR_SUBPARTS (type1
)))
11788 result_type
= type0
;
11791 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11792 || (code0
== VECTOR_TYPE
11793 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
))
11794 && code1
== INTEGER_TYPE
)
11796 doing_shift
= true;
11797 if (TREE_CODE (op1
) == INTEGER_CST
)
11799 if (tree_int_cst_sgn (op1
) < 0)
11802 if (c_inhibit_evaluation_warnings
== 0)
11803 warning_at (location
, OPT_Wshift_count_negative
,
11804 "right shift count is negative");
11806 else if (code0
== VECTOR_TYPE
)
11808 if (compare_tree_int (op1
,
11809 TYPE_PRECISION (TREE_TYPE (type0
)))
11813 if (c_inhibit_evaluation_warnings
== 0)
11814 warning_at (location
, OPT_Wshift_count_overflow
,
11815 "right shift count >= width of vector element");
11820 if (!integer_zerop (op1
))
11823 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11826 if (c_inhibit_evaluation_warnings
== 0)
11827 warning_at (location
, OPT_Wshift_count_overflow
,
11828 "right shift count >= width of type");
11833 /* Use the type of the value to be shifted. */
11834 result_type
= type0
;
11835 /* Avoid converting op1 to result_type later. */
11841 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11842 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11843 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11844 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
11845 TYPE_VECTOR_SUBPARTS (type1
)))
11847 result_type
= type0
;
11850 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11851 || (code0
== VECTOR_TYPE
11852 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
))
11853 && code1
== INTEGER_TYPE
)
11855 doing_shift
= true;
11856 if (TREE_CODE (op0
) == INTEGER_CST
11857 && tree_int_cst_sgn (op0
) < 0)
11859 /* Don't reject a left shift of a negative value in a context
11860 where a constant expression is needed in C90. */
11863 if (c_inhibit_evaluation_warnings
== 0)
11864 warning_at (location
, OPT_Wshift_negative_value
,
11865 "left shift of negative value");
11867 if (TREE_CODE (op1
) == INTEGER_CST
)
11869 if (tree_int_cst_sgn (op1
) < 0)
11872 if (c_inhibit_evaluation_warnings
== 0)
11873 warning_at (location
, OPT_Wshift_count_negative
,
11874 "left shift count is negative");
11876 else if (code0
== VECTOR_TYPE
)
11878 if (compare_tree_int (op1
,
11879 TYPE_PRECISION (TREE_TYPE (type0
)))
11883 if (c_inhibit_evaluation_warnings
== 0)
11884 warning_at (location
, OPT_Wshift_count_overflow
,
11885 "left shift count >= width of vector element");
11888 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11891 if (c_inhibit_evaluation_warnings
== 0)
11892 warning_at (location
, OPT_Wshift_count_overflow
,
11893 "left shift count >= width of type");
11895 else if (TREE_CODE (op0
) == INTEGER_CST
11896 && maybe_warn_shift_overflow (location
, op0
, op1
)
11901 /* Use the type of the value to be shifted. */
11902 result_type
= type0
;
11903 /* Avoid converting op1 to result_type later. */
11910 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11913 if (!vector_types_compatible_elements_p (type0
, type1
))
11915 error_at (location
, "comparing vectors with different "
11917 return error_mark_node
;
11920 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
11921 TYPE_VECTOR_SUBPARTS (type1
)))
11923 error_at (location
, "comparing vectors with different "
11924 "number of elements");
11925 return error_mark_node
;
11928 /* It's not precisely specified how the usual arithmetic
11929 conversions apply to the vector types. Here, we use
11930 the unsigned type if one of the operands is signed and
11931 the other one is unsigned. */
11932 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11934 if (!TYPE_UNSIGNED (type0
))
11935 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11937 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11938 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11939 "types %qT and %qT", type0
, type1
);
11942 /* Always construct signed integer vector type. */
11943 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11945 (TREE_TYPE (type0
))), 0);
11948 error_at (location
, "could not find an integer type "
11949 "of the same size as %qT",
11950 TREE_TYPE (type0
));
11951 return error_mark_node
;
11953 result_type
= build_opaque_vector_type (intt
,
11954 TYPE_VECTOR_SUBPARTS (type0
));
11956 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11957 goto return_build_binary_op
;
11959 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
11960 warning_at (location
,
11962 "comparing floating-point with %<==%> or %<!=%> is unsafe");
11963 /* Result of comparison is always int,
11964 but don't convert the args to int! */
11965 build_type
= integer_type_node
;
11966 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11967 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
11968 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11969 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
11971 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11973 if (TREE_CODE (op0
) == ADDR_EXPR
11974 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0))
11975 && !from_macro_expansion_at (location
))
11977 if (code
== EQ_EXPR
)
11978 warning_at (location
,
11980 "the comparison will always evaluate as %<false%> "
11981 "for the address of %qD will never be NULL",
11982 TREE_OPERAND (op0
, 0));
11984 warning_at (location
,
11986 "the comparison will always evaluate as %<true%> "
11987 "for the address of %qD will never be NULL",
11988 TREE_OPERAND (op0
, 0));
11990 result_type
= type0
;
11992 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11994 if (TREE_CODE (op1
) == ADDR_EXPR
11995 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0))
11996 && !from_macro_expansion_at (location
))
11998 if (code
== EQ_EXPR
)
11999 warning_at (location
,
12001 "the comparison will always evaluate as %<false%> "
12002 "for the address of %qD will never be NULL",
12003 TREE_OPERAND (op1
, 0));
12005 warning_at (location
,
12007 "the comparison will always evaluate as %<true%> "
12008 "for the address of %qD will never be NULL",
12009 TREE_OPERAND (op1
, 0));
12011 result_type
= type1
;
12013 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
12015 tree tt0
= TREE_TYPE (type0
);
12016 tree tt1
= TREE_TYPE (type1
);
12017 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
12018 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
12019 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
12021 /* Anything compares with void *. void * compares with anything.
12022 Otherwise, the targets must be compatible
12023 and both must be object or both incomplete. */
12024 if (comp_target_types (location
, type0
, type1
))
12025 result_type
= common_pointer_type (type0
, type1
);
12026 else if (!addr_space_superset (as0
, as1
, &as_common
))
12028 error_at (location
, "comparison of pointers to "
12029 "disjoint address spaces");
12030 return error_mark_node
;
12032 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
12034 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
12035 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
12036 "comparison of %<void *%> with function pointer");
12038 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
12040 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
12041 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
12042 "comparison of %<void *%> with function pointer");
12045 /* Avoid warning about the volatile ObjC EH puts on decls. */
12047 pedwarn (location
, 0,
12048 "comparison of distinct pointer types lacks a cast");
12050 if (result_type
== NULL_TREE
)
12052 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
12053 result_type
= build_pointer_type
12054 (build_qualified_type (void_type_node
, qual
));
12057 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
12059 result_type
= type0
;
12060 pedwarn (location
, 0, "comparison between pointer and integer");
12062 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
12064 result_type
= type1
;
12065 pedwarn (location
, 0, "comparison between pointer and integer");
12067 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
12068 || truth_value_p (TREE_CODE (orig_op0
)))
12069 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
12070 || truth_value_p (TREE_CODE (orig_op1
))))
12071 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
12078 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
12081 if (!vector_types_compatible_elements_p (type0
, type1
))
12083 error_at (location
, "comparing vectors with different "
12085 return error_mark_node
;
12088 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
12089 TYPE_VECTOR_SUBPARTS (type1
)))
12091 error_at (location
, "comparing vectors with different "
12092 "number of elements");
12093 return error_mark_node
;
12096 /* It's not precisely specified how the usual arithmetic
12097 conversions apply to the vector types. Here, we use
12098 the unsigned type if one of the operands is signed and
12099 the other one is unsigned. */
12100 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
12102 if (!TYPE_UNSIGNED (type0
))
12103 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
12105 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
12106 warning_at (location
, OPT_Wsign_compare
, "comparison between "
12107 "types %qT and %qT", type0
, type1
);
12110 /* Always construct signed integer vector type. */
12111 intt
= c_common_type_for_size (GET_MODE_BITSIZE
12113 (TREE_TYPE (type0
))), 0);
12116 error_at (location
, "could not find an integer type "
12117 "of the same size as %qT",
12118 TREE_TYPE (type0
));
12119 return error_mark_node
;
12121 result_type
= build_opaque_vector_type (intt
,
12122 TYPE_VECTOR_SUBPARTS (type0
));
12124 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
12125 goto return_build_binary_op
;
12127 build_type
= integer_type_node
;
12128 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
12129 || code0
== FIXED_POINT_TYPE
)
12130 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
12131 || code1
== FIXED_POINT_TYPE
))
12133 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
12135 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
12136 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
12137 addr_space_t as_common
;
12139 if (comp_target_types (location
, type0
, type1
))
12141 result_type
= common_pointer_type (type0
, type1
);
12142 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
12143 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
12144 pedwarn (location
, 0,
12145 "comparison of complete and incomplete pointers");
12146 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
12147 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
12148 "ordered comparisons of pointers to functions");
12149 else if (null_pointer_constant_p (orig_op0
)
12150 || null_pointer_constant_p (orig_op1
))
12151 warning_at (location
, OPT_Wextra
,
12152 "ordered comparison of pointer with null pointer");
12155 else if (!addr_space_superset (as0
, as1
, &as_common
))
12157 error_at (location
, "comparison of pointers to "
12158 "disjoint address spaces");
12159 return error_mark_node
;
12163 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
12164 result_type
= build_pointer_type
12165 (build_qualified_type (void_type_node
, qual
));
12166 pedwarn (location
, 0,
12167 "comparison of distinct pointer types lacks a cast");
12170 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
12172 result_type
= type0
;
12174 pedwarn (location
, OPT_Wpedantic
,
12175 "ordered comparison of pointer with integer zero");
12176 else if (extra_warnings
)
12177 warning_at (location
, OPT_Wextra
,
12178 "ordered comparison of pointer with integer zero");
12180 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
12182 result_type
= type1
;
12184 pedwarn (location
, OPT_Wpedantic
,
12185 "ordered comparison of pointer with integer zero");
12186 else if (extra_warnings
)
12187 warning_at (location
, OPT_Wextra
,
12188 "ordered comparison of pointer with integer zero");
12190 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
12192 result_type
= type0
;
12193 pedwarn (location
, 0, "comparison between pointer and integer");
12195 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
12197 result_type
= type1
;
12198 pedwarn (location
, 0, "comparison between pointer and integer");
12201 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
12202 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
12204 op0
= save_expr (op0
);
12205 op1
= save_expr (op1
);
12207 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
12208 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
12211 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
12212 || truth_value_p (TREE_CODE (orig_op0
)))
12213 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
12214 || truth_value_p (TREE_CODE (orig_op1
))))
12215 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
12219 gcc_unreachable ();
12222 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
12223 return error_mark_node
;
12225 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
12226 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
12227 || !vector_types_compatible_elements_p (type0
, type1
)))
12229 gcc_rich_location
richloc (location
);
12230 maybe_range_label_for_tree_type_mismatch
12231 label_for_op0 (orig_op0
, orig_op1
),
12232 label_for_op1 (orig_op1
, orig_op0
);
12233 richloc
.maybe_add_expr (orig_op0
, &label_for_op0
);
12234 richloc
.maybe_add_expr (orig_op1
, &label_for_op1
);
12235 binary_op_error (&richloc
, code
, type0
, type1
);
12236 return error_mark_node
;
12239 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
12240 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
12242 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
12243 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
12245 bool first_complex
= (code0
== COMPLEX_TYPE
);
12246 bool second_complex
= (code1
== COMPLEX_TYPE
);
12247 int none_complex
= (!first_complex
&& !second_complex
);
12249 if (shorten
|| common
|| short_compare
)
12251 result_type
= c_common_type (type0
, type1
);
12252 do_warn_double_promotion (result_type
, type0
, type1
,
12253 "implicit conversion from %qT to %qT "
12254 "to match other operand of binary "
12257 if (result_type
== error_mark_node
)
12258 return error_mark_node
;
12261 if (first_complex
!= second_complex
12262 && (code
== PLUS_EXPR
12263 || code
== MINUS_EXPR
12264 || code
== MULT_EXPR
12265 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
12266 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
12267 && flag_signed_zeros
)
12269 /* An operation on mixed real/complex operands must be
12270 handled specially, but the language-independent code can
12271 more easily optimize the plain complex arithmetic if
12272 -fno-signed-zeros. */
12273 tree real_type
= TREE_TYPE (result_type
);
12275 if (type0
!= orig_type0
|| type1
!= orig_type1
)
12277 gcc_assert (may_need_excess_precision
&& common
);
12278 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
12282 if (TREE_TYPE (op0
) != result_type
)
12283 op0
= convert_and_check (location
, result_type
, op0
);
12284 if (TREE_TYPE (op1
) != real_type
)
12285 op1
= convert_and_check (location
, real_type
, op1
);
12289 if (TREE_TYPE (op0
) != real_type
)
12290 op0
= convert_and_check (location
, real_type
, op0
);
12291 if (TREE_TYPE (op1
) != result_type
)
12292 op1
= convert_and_check (location
, result_type
, op1
);
12294 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
12295 return error_mark_node
;
12298 op0
= save_expr (op0
);
12299 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
12301 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
12306 case TRUNC_DIV_EXPR
:
12307 op1
= save_expr (op1
);
12308 imag
= build2 (resultcode
, real_type
, imag
, op1
);
12309 /* Fall through. */
12312 real
= build2 (resultcode
, real_type
, real
, op1
);
12320 op1
= save_expr (op1
);
12321 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
12323 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
12328 op0
= save_expr (op0
);
12329 imag
= build2 (resultcode
, real_type
, op0
, imag
);
12330 /* Fall through. */
12332 real
= build2 (resultcode
, real_type
, op0
, real
);
12335 real
= build2 (resultcode
, real_type
, op0
, real
);
12336 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
12342 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
12343 goto return_build_binary_op
;
12346 /* For certain operations (which identify themselves by shorten != 0)
12347 if both args were extended from the same smaller type,
12348 do the arithmetic in that type and then extend.
12350 shorten !=0 and !=1 indicates a bitwise operation.
12351 For them, this optimization is safe only if
12352 both args are zero-extended or both are sign-extended.
12353 Otherwise, we might change the result.
12354 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12355 but calculated in (unsigned short) it would be (unsigned short)-1. */
12357 if (shorten
&& none_complex
)
12359 final_type
= result_type
;
12360 result_type
= shorten_binary_op (result_type
, op0
, op1
,
12364 /* Shifts can be shortened if shifting right. */
12369 tree arg0
= get_narrower (op0
, &unsigned_arg
);
12371 final_type
= result_type
;
12373 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
12374 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
12376 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
12377 && tree_int_cst_sgn (op1
) > 0
12378 /* We can shorten only if the shift count is less than the
12379 number of bits in the smaller type size. */
12380 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
12381 /* We cannot drop an unsigned shift after sign-extension. */
12382 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
12384 /* Do an unsigned shift if the operand was zero-extended. */
12386 = c_common_signed_or_unsigned_type (unsigned_arg
,
12388 /* Convert value-to-be-shifted to that type. */
12389 if (TREE_TYPE (op0
) != result_type
)
12390 op0
= convert (result_type
, op0
);
12395 /* Comparison operations are shortened too but differently.
12396 They identify themselves by setting short_compare = 1. */
12400 /* Don't write &op0, etc., because that would prevent op0
12401 from being kept in a register.
12402 Instead, make copies of the our local variables and
12403 pass the copies by reference, then copy them back afterward. */
12404 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
12405 enum tree_code xresultcode
= resultcode
;
12407 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
12410 if (val
!= NULL_TREE
)
12413 goto return_build_binary_op
;
12416 op0
= xop0
, op1
= xop1
;
12418 resultcode
= xresultcode
;
12420 if (c_inhibit_evaluation_warnings
== 0)
12422 bool op0_maybe_const
= true;
12423 bool op1_maybe_const
= true;
12424 tree orig_op0_folded
, orig_op1_folded
;
12426 if (in_late_binary_op
)
12428 orig_op0_folded
= orig_op0
;
12429 orig_op1_folded
= orig_op1
;
12433 /* Fold for the sake of possible warnings, as in
12434 build_conditional_expr. This requires the
12435 "original" values to be folded, not just op0 and
12437 c_inhibit_evaluation_warnings
++;
12438 op0
= c_fully_fold (op0
, require_constant_value
,
12440 op1
= c_fully_fold (op1
, require_constant_value
,
12442 c_inhibit_evaluation_warnings
--;
12443 orig_op0_folded
= c_fully_fold (orig_op0
,
12444 require_constant_value
,
12446 orig_op1_folded
= c_fully_fold (orig_op1
,
12447 require_constant_value
,
12451 if (warn_sign_compare
)
12452 warn_for_sign_compare (location
, orig_op0_folded
,
12453 orig_op1_folded
, op0
, op1
,
12454 result_type
, resultcode
);
12455 if (!in_late_binary_op
&& !int_operands
)
12457 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
12458 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
12459 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
12460 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
12466 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12467 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12468 Then the expression will be built.
12469 It will be given type FINAL_TYPE if that is nonzero;
12470 otherwise, it will be given type RESULT_TYPE. */
12474 /* Favor showing any expression locations that are available. */
12475 op_location_t
oploc (location
, UNKNOWN_LOCATION
);
12476 binary_op_rich_location
richloc (oploc
, orig_op0
, orig_op1
, true);
12477 binary_op_error (&richloc
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
12478 return error_mark_node
;
12481 if (build_type
== NULL_TREE
)
12483 build_type
= result_type
;
12484 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
12487 gcc_assert (may_need_excess_precision
&& common
);
12488 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
12494 op0
= ep_convert_and_check (location
, result_type
, op0
,
12495 semantic_result_type
);
12496 op1
= ep_convert_and_check (location
, result_type
, op1
,
12497 semantic_result_type
);
12499 /* This can happen if one operand has a vector type, and the other
12500 has a different type. */
12501 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
12502 return error_mark_node
;
12505 if (sanitize_flags_p ((SANITIZE_SHIFT
12506 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
12507 && current_function_decl
!= NULL_TREE
12508 && (doing_div_or_mod
|| doing_shift
)
12509 && !require_constant_value
)
12511 /* OP0 and/or OP1 might have side-effects. */
12512 op0
= save_expr (op0
);
12513 op1
= save_expr (op1
);
12514 op0
= c_fully_fold (op0
, false, NULL
);
12515 op1
= c_fully_fold (op1
, false, NULL
);
12516 if (doing_div_or_mod
&& (sanitize_flags_p ((SANITIZE_DIVIDE
12517 | SANITIZE_FLOAT_DIVIDE
))))
12518 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
12519 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
12520 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
12523 /* Treat expressions in initializers specially as they can't trap. */
12524 if (int_const_or_overflow
)
12525 ret
= (require_constant_value
12526 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
12528 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
12530 ret
= build2 (resultcode
, build_type
, op0
, op1
);
12531 if (final_type
!= NULL_TREE
)
12532 ret
= convert (final_type
, ret
);
12534 return_build_binary_op
:
12535 gcc_assert (ret
!= error_mark_node
);
12536 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
12537 ret
= (int_operands
12538 ? note_integer_operands (ret
)
12539 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
12540 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
12541 && !in_late_binary_op
)
12542 ret
= note_integer_operands (ret
);
12543 protected_set_expr_location (ret
, location
);
12545 if (instrument_expr
!= NULL
)
12546 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
12547 instrument_expr
, ret
);
12549 if (semantic_result_type
)
12550 ret
= build1_loc (location
, EXCESS_PRECISION_EXPR
,
12551 semantic_result_type
, ret
);
12557 /* Convert EXPR to be a truth-value, validating its type for this
12558 purpose. LOCATION is the source location for the expression. */
12561 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
12563 bool int_const
, int_operands
;
12565 switch (TREE_CODE (TREE_TYPE (expr
)))
12568 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
12569 return error_mark_node
;
12572 error_at (location
, "used struct type value where scalar is required");
12573 return error_mark_node
;
12576 error_at (location
, "used union type value where scalar is required");
12577 return error_mark_node
;
12580 error_at (location
, "void value not ignored as it ought to be");
12581 return error_mark_node
;
12584 if (reject_gcc_builtin (expr
))
12585 return error_mark_node
;
12588 case FUNCTION_TYPE
:
12589 gcc_unreachable ();
12592 error_at (location
, "used vector type where scalar is required");
12593 return error_mark_node
;
12599 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
12600 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
12601 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
12603 expr
= remove_c_maybe_const_expr (expr
);
12604 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
12605 convert (TREE_TYPE (expr
), integer_zero_node
));
12606 expr
= note_integer_operands (expr
);
12609 /* ??? Should we also give an error for vectors rather than leaving
12610 those to give errors later? */
12611 expr
= c_common_truthvalue_conversion (location
, expr
);
12613 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
12615 if (TREE_OVERFLOW (expr
))
12618 return note_integer_operands (expr
);
12620 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
12621 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
12626 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12630 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
12632 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
12634 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
12635 /* Executing a compound literal inside a function reinitializes
12637 if (!TREE_STATIC (decl
))
12645 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12646 statement. LOC is the location of the construct. */
12649 c_finish_omp_construct (location_t loc
, enum tree_code code
, tree body
,
12652 body
= c_end_compound_stmt (loc
, body
, true);
12654 tree stmt
= make_node (code
);
12655 TREE_TYPE (stmt
) = void_type_node
;
12656 OMP_BODY (stmt
) = body
;
12657 OMP_CLAUSES (stmt
) = clauses
;
12658 SET_EXPR_LOCATION (stmt
, loc
);
12660 return add_stmt (stmt
);
12663 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12664 statement. LOC is the location of the OACC_DATA. */
12667 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
12671 block
= c_end_compound_stmt (loc
, block
, true);
12673 stmt
= make_node (OACC_DATA
);
12674 TREE_TYPE (stmt
) = void_type_node
;
12675 OACC_DATA_CLAUSES (stmt
) = clauses
;
12676 OACC_DATA_BODY (stmt
) = block
;
12677 SET_EXPR_LOCATION (stmt
, loc
);
12679 return add_stmt (stmt
);
12682 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12683 statement. LOC is the location of the OACC_HOST_DATA. */
12686 c_finish_oacc_host_data (location_t loc
, tree clauses
, tree block
)
12690 block
= c_end_compound_stmt (loc
, block
, true);
12692 stmt
= make_node (OACC_HOST_DATA
);
12693 TREE_TYPE (stmt
) = void_type_node
;
12694 OACC_HOST_DATA_CLAUSES (stmt
) = clauses
;
12695 OACC_HOST_DATA_BODY (stmt
) = block
;
12696 SET_EXPR_LOCATION (stmt
, loc
);
12698 return add_stmt (stmt
);
12701 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12704 c_begin_omp_parallel (void)
12708 keep_next_level ();
12709 block
= c_begin_compound_stmt (true);
12714 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12715 statement. LOC is the location of the OMP_PARALLEL. */
12718 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
12722 block
= c_end_compound_stmt (loc
, block
, true);
12724 stmt
= make_node (OMP_PARALLEL
);
12725 TREE_TYPE (stmt
) = void_type_node
;
12726 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
12727 OMP_PARALLEL_BODY (stmt
) = block
;
12728 SET_EXPR_LOCATION (stmt
, loc
);
12730 return add_stmt (stmt
);
12733 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12736 c_begin_omp_task (void)
12740 keep_next_level ();
12741 block
= c_begin_compound_stmt (true);
12746 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12747 statement. LOC is the location of the #pragma. */
12750 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
12754 block
= c_end_compound_stmt (loc
, block
, true);
12756 stmt
= make_node (OMP_TASK
);
12757 TREE_TYPE (stmt
) = void_type_node
;
12758 OMP_TASK_CLAUSES (stmt
) = clauses
;
12759 OMP_TASK_BODY (stmt
) = block
;
12760 SET_EXPR_LOCATION (stmt
, loc
);
12762 return add_stmt (stmt
);
12765 /* Generate GOMP_cancel call for #pragma omp cancel. */
12768 c_finish_omp_cancel (location_t loc
, tree clauses
)
12770 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
12772 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12774 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
12776 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12778 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12782 error_at (loc
, "%<#pragma omp cancel%> must specify one of "
12783 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12787 tree ifc
= omp_find_clause (clauses
, OMP_CLAUSE_IF
);
12788 if (ifc
!= NULL_TREE
)
12790 if (OMP_CLAUSE_IF_MODIFIER (ifc
) != ERROR_MARK
12791 && OMP_CLAUSE_IF_MODIFIER (ifc
) != VOID_CST
)
12792 error_at (OMP_CLAUSE_LOCATION (ifc
),
12793 "expected %<cancel%> %<if%> clause modifier");
12796 tree ifc2
= omp_find_clause (OMP_CLAUSE_CHAIN (ifc
), OMP_CLAUSE_IF
);
12797 if (ifc2
!= NULL_TREE
)
12799 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc
) == VOID_CST
12800 && OMP_CLAUSE_IF_MODIFIER (ifc2
) != ERROR_MARK
12801 && OMP_CLAUSE_IF_MODIFIER (ifc2
) != VOID_CST
);
12802 error_at (OMP_CLAUSE_LOCATION (ifc2
),
12803 "expected %<cancel%> %<if%> clause modifier");
12807 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
12808 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
12809 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
12810 build_zero_cst (type
));
12813 ifc
= boolean_true_node
;
12814 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
12815 build_int_cst (integer_type_node
, mask
),
12820 /* Generate GOMP_cancellation_point call for
12821 #pragma omp cancellation point. */
12824 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
12826 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
12828 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12830 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
12832 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12834 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12838 error_at (loc
, "%<#pragma omp cancellation point%> must specify one of "
12839 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12843 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
12844 build_int_cst (integer_type_node
, mask
));
12848 /* Helper function for handle_omp_array_sections. Called recursively
12849 to handle multiple array-section-subscripts. C is the clause,
12850 T current expression (initially OMP_CLAUSE_DECL), which is either
12851 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12852 expression if specified, TREE_VALUE length expression if specified,
12853 TREE_CHAIN is what it has been specified after, or some decl.
12854 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12855 set to true if any of the array-section-subscript could have length
12856 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12857 first array-section-subscript which is known not to have length
12859 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12860 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12861 all are or may have length of 1, array-section-subscript [:2] is the
12862 first one known not to have length 1. For array-section-subscript
12863 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12864 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12865 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12866 case though, as some lengths could be zero. */
12869 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
12870 bool &maybe_zero_len
, unsigned int &first_non_one
,
12871 enum c_omp_region_type ort
)
12873 tree ret
, low_bound
, length
, type
;
12874 if (TREE_CODE (t
) != TREE_LIST
)
12876 if (error_operand_p (t
))
12877 return error_mark_node
;
12879 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12880 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
12882 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qE in %qs clause",
12883 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12884 return error_mark_node
;
12886 if (TREE_CODE (t
) == COMPONENT_REF
12887 && ort
== C_ORT_OMP
12888 && (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12889 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO
12890 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FROM
))
12892 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
12894 error_at (OMP_CLAUSE_LOCATION (c
),
12895 "bit-field %qE in %qs clause",
12896 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12897 return error_mark_node
;
12899 while (TREE_CODE (t
) == COMPONENT_REF
)
12901 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == UNION_TYPE
)
12903 error_at (OMP_CLAUSE_LOCATION (c
),
12904 "%qE is a member of a union", t
);
12905 return error_mark_node
;
12907 t
= TREE_OPERAND (t
, 0);
12910 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
12913 error_at (OMP_CLAUSE_LOCATION (c
),
12914 "%qD is not a variable in %qs clause", t
,
12915 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12917 error_at (OMP_CLAUSE_LOCATION (c
),
12918 "%qE is not a variable in %qs clause", t
,
12919 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12920 return error_mark_node
;
12922 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12923 && TYPE_ATOMIC (TREE_TYPE (t
)))
12925 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qD in %qs clause",
12926 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12927 return error_mark_node
;
12929 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12931 && DECL_THREAD_LOCAL_P (t
))
12933 error_at (OMP_CLAUSE_LOCATION (c
),
12934 "%qD is threadprivate variable in %qs clause", t
,
12935 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12936 return error_mark_node
;
12938 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12939 && TYPE_ATOMIC (TREE_TYPE (t
))
12940 && POINTER_TYPE_P (TREE_TYPE (t
)))
12942 /* If the array section is pointer based and the pointer
12943 itself is _Atomic qualified, we need to atomically load
12946 memset (&expr
, 0, sizeof (expr
));
12948 expr
= convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c
),
12949 expr
, false, false);
12955 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
12956 maybe_zero_len
, first_non_one
, ort
);
12957 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
12960 type
= TREE_TYPE (ret
);
12961 low_bound
= TREE_PURPOSE (t
);
12962 length
= TREE_VALUE (t
);
12964 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
12965 return error_mark_node
;
12967 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
12969 error_at (OMP_CLAUSE_LOCATION (c
),
12970 "low bound %qE of array section does not have integral type",
12972 return error_mark_node
;
12974 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
12976 error_at (OMP_CLAUSE_LOCATION (c
),
12977 "length %qE of array section does not have integral type",
12979 return error_mark_node
;
12982 && TREE_CODE (low_bound
) == INTEGER_CST
12983 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12984 > TYPE_PRECISION (sizetype
))
12985 low_bound
= fold_convert (sizetype
, low_bound
);
12987 && TREE_CODE (length
) == INTEGER_CST
12988 && TYPE_PRECISION (TREE_TYPE (length
))
12989 > TYPE_PRECISION (sizetype
))
12990 length
= fold_convert (sizetype
, length
);
12991 if (low_bound
== NULL_TREE
)
12992 low_bound
= integer_zero_node
;
12994 if (length
!= NULL_TREE
)
12996 if (!integer_nonzerop (length
))
12998 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12999 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13000 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13001 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
13003 if (integer_zerop (length
))
13005 error_at (OMP_CLAUSE_LOCATION (c
),
13006 "zero length array section in %qs clause",
13007 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13008 return error_mark_node
;
13012 maybe_zero_len
= true;
13014 if (first_non_one
== types
.length ()
13015 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
13018 if (TREE_CODE (type
) == ARRAY_TYPE
)
13020 if (length
== NULL_TREE
13021 && (TYPE_DOMAIN (type
) == NULL_TREE
13022 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
13024 error_at (OMP_CLAUSE_LOCATION (c
),
13025 "for unknown bound array type length expression must "
13027 return error_mark_node
;
13029 if (TREE_CODE (low_bound
) == INTEGER_CST
13030 && tree_int_cst_sgn (low_bound
) == -1)
13032 error_at (OMP_CLAUSE_LOCATION (c
),
13033 "negative low bound in array section in %qs clause",
13034 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13035 return error_mark_node
;
13037 if (length
!= NULL_TREE
13038 && TREE_CODE (length
) == INTEGER_CST
13039 && tree_int_cst_sgn (length
) == -1)
13041 error_at (OMP_CLAUSE_LOCATION (c
),
13042 "negative length in array section in %qs clause",
13043 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13044 return error_mark_node
;
13046 if (TYPE_DOMAIN (type
)
13047 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
13048 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
13052 = fold_convert (sizetype
, TYPE_MAX_VALUE (TYPE_DOMAIN (type
)));
13053 size
= size_binop (PLUS_EXPR
, size
, size_one_node
);
13054 if (TREE_CODE (low_bound
) == INTEGER_CST
)
13056 if (tree_int_cst_lt (size
, low_bound
))
13058 error_at (OMP_CLAUSE_LOCATION (c
),
13059 "low bound %qE above array section size "
13060 "in %qs clause", low_bound
,
13061 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13062 return error_mark_node
;
13064 if (tree_int_cst_equal (size
, low_bound
))
13066 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
13067 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13068 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13069 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
13071 error_at (OMP_CLAUSE_LOCATION (c
),
13072 "zero length array section in %qs clause",
13073 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13074 return error_mark_node
;
13076 maybe_zero_len
= true;
13078 else if (length
== NULL_TREE
13079 && first_non_one
== types
.length ()
13080 && tree_int_cst_equal
13081 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
13085 else if (length
== NULL_TREE
)
13087 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13088 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
13089 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_IN_REDUCTION
13090 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_TASK_REDUCTION
)
13091 maybe_zero_len
= true;
13092 if (first_non_one
== types
.length ())
13095 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
13097 if (tree_int_cst_lt (size
, length
))
13099 error_at (OMP_CLAUSE_LOCATION (c
),
13100 "length %qE above array section size "
13101 "in %qs clause", length
,
13102 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13103 return error_mark_node
;
13105 if (TREE_CODE (low_bound
) == INTEGER_CST
)
13108 = size_binop (PLUS_EXPR
,
13109 fold_convert (sizetype
, low_bound
),
13110 fold_convert (sizetype
, length
));
13111 if (TREE_CODE (lbpluslen
) == INTEGER_CST
13112 && tree_int_cst_lt (size
, lbpluslen
))
13114 error_at (OMP_CLAUSE_LOCATION (c
),
13115 "high bound %qE above array section size "
13116 "in %qs clause", lbpluslen
,
13117 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13118 return error_mark_node
;
13123 else if (length
== NULL_TREE
)
13125 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13126 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
13127 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_IN_REDUCTION
13128 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_TASK_REDUCTION
)
13129 maybe_zero_len
= true;
13130 if (first_non_one
== types
.length ())
13134 /* For [lb:] we will need to evaluate lb more than once. */
13135 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
13137 tree lb
= save_expr (low_bound
);
13138 if (lb
!= low_bound
)
13140 TREE_PURPOSE (t
) = lb
;
13145 else if (TREE_CODE (type
) == POINTER_TYPE
)
13147 if (length
== NULL_TREE
)
13149 error_at (OMP_CLAUSE_LOCATION (c
),
13150 "for pointer type length expression must be specified");
13151 return error_mark_node
;
13153 if (length
!= NULL_TREE
13154 && TREE_CODE (length
) == INTEGER_CST
13155 && tree_int_cst_sgn (length
) == -1)
13157 error_at (OMP_CLAUSE_LOCATION (c
),
13158 "negative length in array section in %qs clause",
13159 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13160 return error_mark_node
;
13162 /* If there is a pointer type anywhere but in the very first
13163 array-section-subscript, the array section can't be contiguous. */
13164 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13165 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
13167 error_at (OMP_CLAUSE_LOCATION (c
),
13168 "array section is not contiguous in %qs clause",
13169 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13170 return error_mark_node
;
13175 error_at (OMP_CLAUSE_LOCATION (c
),
13176 "%qE does not have pointer or array type", ret
);
13177 return error_mark_node
;
13179 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
13180 types
.safe_push (TREE_TYPE (ret
));
13181 /* We will need to evaluate lb more than once. */
13182 tree lb
= save_expr (low_bound
);
13183 if (lb
!= low_bound
)
13185 TREE_PURPOSE (t
) = lb
;
13188 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
13192 /* Handle array sections for clause C. */
13195 handle_omp_array_sections (tree c
, enum c_omp_region_type ort
)
13197 bool maybe_zero_len
= false;
13198 unsigned int first_non_one
= 0;
13199 auto_vec
<tree
, 10> types
;
13200 tree
*tp
= &OMP_CLAUSE_DECL (c
);
13201 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
13202 && TREE_CODE (*tp
) == TREE_LIST
13203 && TREE_PURPOSE (*tp
)
13204 && TREE_CODE (TREE_PURPOSE (*tp
)) == TREE_VEC
)
13205 tp
= &TREE_VALUE (*tp
);
13206 tree first
= handle_omp_array_sections_1 (c
, *tp
, types
,
13207 maybe_zero_len
, first_non_one
,
13209 if (first
== error_mark_node
)
13211 if (first
== NULL_TREE
)
13213 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
13216 tree tem
= NULL_TREE
;
13217 /* Need to evaluate side effects in the length expressions
13219 while (TREE_CODE (t
) == TREE_LIST
)
13221 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
13223 if (tem
== NULL_TREE
)
13224 tem
= TREE_VALUE (t
);
13226 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
13227 TREE_VALUE (t
), tem
);
13229 t
= TREE_CHAIN (t
);
13232 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
13233 first
= c_fully_fold (first
, false, NULL
, true);
13238 unsigned int num
= types
.length (), i
;
13239 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
13240 tree condition
= NULL_TREE
;
13242 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
13243 maybe_zero_len
= true;
13245 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
13246 t
= TREE_CHAIN (t
))
13248 tree low_bound
= TREE_PURPOSE (t
);
13249 tree length
= TREE_VALUE (t
);
13253 && TREE_CODE (low_bound
) == INTEGER_CST
13254 && TYPE_PRECISION (TREE_TYPE (low_bound
))
13255 > TYPE_PRECISION (sizetype
))
13256 low_bound
= fold_convert (sizetype
, low_bound
);
13258 && TREE_CODE (length
) == INTEGER_CST
13259 && TYPE_PRECISION (TREE_TYPE (length
))
13260 > TYPE_PRECISION (sizetype
))
13261 length
= fold_convert (sizetype
, length
);
13262 if (low_bound
== NULL_TREE
)
13263 low_bound
= integer_zero_node
;
13264 if (!maybe_zero_len
&& i
> first_non_one
)
13266 if (integer_nonzerop (low_bound
))
13267 goto do_warn_noncontiguous
;
13268 if (length
!= NULL_TREE
13269 && TREE_CODE (length
) == INTEGER_CST
13270 && TYPE_DOMAIN (types
[i
])
13271 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
13272 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
13276 size
= size_binop (PLUS_EXPR
,
13277 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
13279 if (!tree_int_cst_equal (length
, size
))
13281 do_warn_noncontiguous
:
13282 error_at (OMP_CLAUSE_LOCATION (c
),
13283 "array section is not contiguous in %qs "
13285 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13289 if (length
!= NULL_TREE
13290 && TREE_SIDE_EFFECTS (length
))
13292 if (side_effects
== NULL_TREE
)
13293 side_effects
= length
;
13295 side_effects
= build2 (COMPOUND_EXPR
,
13296 TREE_TYPE (side_effects
),
13297 length
, side_effects
);
13304 if (i
> first_non_one
13305 && ((length
&& integer_nonzerop (length
))
13306 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13307 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13308 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
))
13311 l
= fold_convert (sizetype
, length
);
13314 l
= size_binop (PLUS_EXPR
,
13315 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
13317 l
= size_binop (MINUS_EXPR
, l
,
13318 fold_convert (sizetype
, low_bound
));
13320 if (i
> first_non_one
)
13322 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
13324 if (condition
== NULL_TREE
)
13327 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
13330 else if (size
== NULL_TREE
)
13332 size
= size_in_bytes (TREE_TYPE (types
[i
]));
13333 tree eltype
= TREE_TYPE (types
[num
- 1]);
13334 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
13335 eltype
= TREE_TYPE (eltype
);
13336 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13337 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13338 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
13340 if (integer_zerop (size
)
13341 || integer_zerop (size_in_bytes (eltype
)))
13343 error_at (OMP_CLAUSE_LOCATION (c
),
13344 "zero length array section in %qs clause",
13345 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13346 return error_mark_node
;
13348 size
= size_binop (EXACT_DIV_EXPR
, size
,
13349 size_in_bytes (eltype
));
13351 size
= size_binop (MULT_EXPR
, size
, l
);
13353 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
13354 size
, size_zero_node
);
13357 size
= size_binop (MULT_EXPR
, size
, l
);
13361 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
13362 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13363 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13364 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
13366 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
13367 size
= c_fully_fold (size
, false, NULL
);
13368 size
= save_expr (size
);
13369 tree index_type
= build_index_type (size
);
13370 tree eltype
= TREE_TYPE (first
);
13371 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
13372 eltype
= TREE_TYPE (eltype
);
13373 tree type
= build_array_type (eltype
, index_type
);
13374 tree ptype
= build_pointer_type (eltype
);
13375 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13376 t
= build_fold_addr_expr (t
);
13377 tree t2
= build_fold_addr_expr (first
);
13378 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
13379 ptrdiff_type_node
, t2
);
13380 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13381 ptrdiff_type_node
, t2
,
13382 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
13383 ptrdiff_type_node
, t
));
13384 t2
= c_fully_fold (t2
, false, NULL
);
13385 if (tree_fits_shwi_p (t2
))
13386 t
= build2 (MEM_REF
, type
, t
,
13387 build_int_cst (ptype
, tree_to_shwi (t2
)));
13390 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), sizetype
, t2
);
13391 t
= build2_loc (OMP_CLAUSE_LOCATION (c
), POINTER_PLUS_EXPR
,
13392 TREE_TYPE (t
), t
, t2
);
13393 t
= build2 (MEM_REF
, type
, t
, build_int_cst (ptype
, 0));
13395 OMP_CLAUSE_DECL (c
) = t
;
13398 first
= c_fully_fold (first
, false, NULL
);
13399 OMP_CLAUSE_DECL (c
) = first
;
13401 size
= c_fully_fold (size
, false, NULL
);
13402 OMP_CLAUSE_SIZE (c
) = size
;
13403 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
13404 || (TREE_CODE (t
) == COMPONENT_REF
13405 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
))
13407 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
13408 if (ort
== C_ORT_OMP
|| ort
== C_ORT_ACC
)
13409 switch (OMP_CLAUSE_MAP_KIND (c
))
13411 case GOMP_MAP_ALLOC
:
13413 case GOMP_MAP_FROM
:
13414 case GOMP_MAP_TOFROM
:
13415 case GOMP_MAP_ALWAYS_TO
:
13416 case GOMP_MAP_ALWAYS_FROM
:
13417 case GOMP_MAP_ALWAYS_TOFROM
:
13418 case GOMP_MAP_RELEASE
:
13419 case GOMP_MAP_DELETE
:
13420 case GOMP_MAP_FORCE_TO
:
13421 case GOMP_MAP_FORCE_FROM
:
13422 case GOMP_MAP_FORCE_TOFROM
:
13423 case GOMP_MAP_FORCE_PRESENT
:
13424 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c
) = 1;
13429 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
13430 if (ort
!= C_ORT_OMP
&& ort
!= C_ORT_ACC
)
13431 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_POINTER
);
13432 else if (TREE_CODE (t
) == COMPONENT_REF
)
13433 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_ALWAYS_POINTER
);
13435 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
13436 if (OMP_CLAUSE_MAP_KIND (c2
) != GOMP_MAP_FIRSTPRIVATE_POINTER
13437 && !c_mark_addressable (t
))
13439 OMP_CLAUSE_DECL (c2
) = t
;
13440 t
= build_fold_addr_expr (first
);
13441 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
13442 tree ptr
= OMP_CLAUSE_DECL (c2
);
13443 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
13444 ptr
= build_fold_addr_expr (ptr
);
13445 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13446 ptrdiff_type_node
, t
,
13447 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
13448 ptrdiff_type_node
, ptr
));
13449 t
= c_fully_fold (t
, false, NULL
);
13450 OMP_CLAUSE_SIZE (c2
) = t
;
13451 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
13452 OMP_CLAUSE_CHAIN (c
) = c2
;
13457 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13458 an inline call. But, remap
13459 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13460 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13463 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
13464 tree decl
, tree placeholder
)
13467 hash_map
<tree
, tree
> decl_map
;
13469 decl_map
.put (omp_decl1
, placeholder
);
13470 decl_map
.put (omp_decl2
, decl
);
13471 memset (&id
, 0, sizeof (id
));
13472 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
13473 id
.dst_fn
= current_function_decl
;
13474 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
13475 id
.decl_map
= &decl_map
;
13477 id
.copy_decl
= copy_decl_no_change
;
13478 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
13479 id
.transform_new_cfg
= true;
13480 id
.transform_return_to_modify
= false;
13481 id
.transform_lang_insert_block
= NULL
;
13483 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
13487 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13488 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13491 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
13493 if (*tp
== (tree
) data
)
13498 /* Similarly, but also walk aggregate fields. */
13500 struct c_find_omp_var_s
{ tree var
; hash_set
<tree
> *pset
; };
13503 c_find_omp_var_r (tree
*tp
, int *, void *data
)
13505 if (*tp
== ((struct c_find_omp_var_s
*) data
)->var
)
13507 if (RECORD_OR_UNION_TYPE_P (*tp
))
13510 hash_set
<tree
> *pset
= ((struct c_find_omp_var_s
*) data
)->pset
;
13512 for (field
= TYPE_FIELDS (*tp
); field
;
13513 field
= DECL_CHAIN (field
))
13514 if (TREE_CODE (field
) == FIELD_DECL
)
13516 tree ret
= walk_tree (&DECL_FIELD_OFFSET (field
),
13517 c_find_omp_var_r
, data
, pset
);
13520 ret
= walk_tree (&DECL_SIZE (field
), c_find_omp_var_r
, data
, pset
);
13523 ret
= walk_tree (&DECL_SIZE_UNIT (field
), c_find_omp_var_r
, data
,
13527 ret
= walk_tree (&TREE_TYPE (field
), c_find_omp_var_r
, data
, pset
);
13532 else if (INTEGRAL_TYPE_P (*tp
))
13533 return walk_tree (&TYPE_MAX_VALUE (*tp
), c_find_omp_var_r
, data
,
13534 ((struct c_find_omp_var_s
*) data
)->pset
);
13538 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13539 and clauses containing them should be removed. */
13542 c_omp_finish_iterators (tree iter
)
13545 for (tree it
= iter
; it
; it
= TREE_CHAIN (it
))
13547 tree var
= TREE_VEC_ELT (it
, 0);
13548 tree begin
= TREE_VEC_ELT (it
, 1);
13549 tree end
= TREE_VEC_ELT (it
, 2);
13550 tree step
= TREE_VEC_ELT (it
, 3);
13552 tree type
= TREE_TYPE (var
);
13553 location_t loc
= DECL_SOURCE_LOCATION (var
);
13554 if (type
== error_mark_node
)
13559 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
13561 error_at (loc
, "iterator %qD has neither integral nor pointer type",
13566 else if (TYPE_ATOMIC (type
))
13568 error_at (loc
, "iterator %qD has %<_Atomic%> qualified type", var
);
13572 else if (TYPE_READONLY (type
))
13574 error_at (loc
, "iterator %qD has const qualified type", var
);
13578 else if (step
== error_mark_node
13579 || TREE_TYPE (step
) == error_mark_node
)
13584 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step
)))
13586 error_at (EXPR_LOC_OR_LOC (step
, loc
),
13587 "iterator step with non-integral type");
13591 begin
= c_fully_fold (build_c_cast (loc
, type
, begin
), false, NULL
);
13592 end
= c_fully_fold (build_c_cast (loc
, type
, end
), false, NULL
);
13593 orig_step
= save_expr (c_fully_fold (step
, false, NULL
));
13594 tree stype
= POINTER_TYPE_P (type
) ? sizetype
: type
;
13595 step
= c_fully_fold (build_c_cast (loc
, stype
, orig_step
), false, NULL
);
13596 if (POINTER_TYPE_P (type
))
13598 begin
= save_expr (begin
);
13599 step
= pointer_int_sum (loc
, PLUS_EXPR
, begin
, step
);
13600 step
= fold_build2_loc (loc
, MINUS_EXPR
, sizetype
,
13601 fold_convert (sizetype
, step
),
13602 fold_convert (sizetype
, begin
));
13603 step
= fold_convert (ssizetype
, step
);
13605 if (integer_zerop (step
))
13607 error_at (loc
, "iterator %qD has zero step", var
);
13612 if (begin
== error_mark_node
13613 || end
== error_mark_node
13614 || step
== error_mark_node
13615 || orig_step
== error_mark_node
)
13620 hash_set
<tree
> pset
;
13622 for (it2
= TREE_CHAIN (it
); it2
; it2
= TREE_CHAIN (it2
))
13624 tree var2
= TREE_VEC_ELT (it2
, 0);
13625 tree begin2
= TREE_VEC_ELT (it2
, 1);
13626 tree end2
= TREE_VEC_ELT (it2
, 2);
13627 tree step2
= TREE_VEC_ELT (it2
, 3);
13628 tree type2
= TREE_TYPE (var2
);
13629 location_t loc2
= DECL_SOURCE_LOCATION (var2
);
13630 struct c_find_omp_var_s data
= { var
, &pset
};
13631 if (walk_tree (&type2
, c_find_omp_var_r
, &data
, &pset
))
13634 "type of iterator %qD refers to outer iterator %qD",
13638 else if (walk_tree (&begin2
, c_find_omp_var_r
, &data
, &pset
))
13640 error_at (EXPR_LOC_OR_LOC (begin2
, loc2
),
13641 "begin expression refers to outer iterator %qD", var
);
13644 else if (walk_tree (&end2
, c_find_omp_var_r
, &data
, &pset
))
13646 error_at (EXPR_LOC_OR_LOC (end2
, loc2
),
13647 "end expression refers to outer iterator %qD", var
);
13650 else if (walk_tree (&step2
, c_find_omp_var_r
, &data
, &pset
))
13652 error_at (EXPR_LOC_OR_LOC (step2
, loc2
),
13653 "step expression refers to outer iterator %qD", var
);
13662 TREE_VEC_ELT (it
, 1) = begin
;
13663 TREE_VEC_ELT (it
, 2) = end
;
13664 TREE_VEC_ELT (it
, 3) = step
;
13665 TREE_VEC_ELT (it
, 4) = orig_step
;
13670 /* For all elements of CLAUSES, validate them against their constraints.
13671 Remove any elements from the list that are invalid. */
13674 c_finish_omp_clauses (tree clauses
, enum c_omp_region_type ort
)
13676 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
13677 bitmap_head aligned_head
, map_head
, map_field_head
, oacc_reduction_head
;
13678 tree c
, t
, type
, *pc
;
13679 tree simdlen
= NULL_TREE
, safelen
= NULL_TREE
;
13680 bool branch_seen
= false;
13681 bool copyprivate_seen
= false;
13682 bool linear_variable_step_check
= false;
13683 tree
*nowait_clause
= NULL
;
13684 tree ordered_clause
= NULL_TREE
;
13685 tree schedule_clause
= NULL_TREE
;
13686 bool oacc_async
= false;
13687 tree last_iterators
= NULL_TREE
;
13688 bool last_iterators_remove
= false;
13689 tree
*nogroup_seen
= NULL
;
13690 tree
*order_clause
= NULL
;
13691 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13692 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13693 int reduction_seen
= 0;
13695 bitmap_obstack_initialize (NULL
);
13696 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
13697 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
13698 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
13699 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
13700 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13701 bitmap_initialize (&map_head
, &bitmap_default_obstack
);
13702 bitmap_initialize (&map_field_head
, &bitmap_default_obstack
);
13703 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
13705 bitmap_initialize (&oacc_reduction_head
, &bitmap_default_obstack
);
13707 if (ort
& C_ORT_ACC
)
13708 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13709 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ASYNC
)
13715 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
13717 bool remove
= false;
13718 bool need_complete
= false;
13719 bool need_implicitly_determined
= false;
13721 switch (OMP_CLAUSE_CODE (c
))
13723 case OMP_CLAUSE_SHARED
:
13724 need_implicitly_determined
= true;
13725 goto check_dup_generic
;
13727 case OMP_CLAUSE_PRIVATE
:
13728 need_complete
= true;
13729 need_implicitly_determined
= true;
13730 goto check_dup_generic
;
13732 case OMP_CLAUSE_REDUCTION
:
13733 if (reduction_seen
== 0)
13734 reduction_seen
= OMP_CLAUSE_REDUCTION_INSCAN (c
) ? -1 : 1;
13735 else if (reduction_seen
!= -2
13736 && reduction_seen
!= (OMP_CLAUSE_REDUCTION_INSCAN (c
)
13739 error_at (OMP_CLAUSE_LOCATION (c
),
13740 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
13741 "on the same construct");
13742 reduction_seen
= -2;
13745 case OMP_CLAUSE_IN_REDUCTION
:
13746 case OMP_CLAUSE_TASK_REDUCTION
:
13747 need_implicitly_determined
= true;
13748 t
= OMP_CLAUSE_DECL (c
);
13749 if (TREE_CODE (t
) == TREE_LIST
)
13751 if (handle_omp_array_sections (c
, ort
))
13757 t
= OMP_CLAUSE_DECL (c
);
13758 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13759 && OMP_CLAUSE_REDUCTION_INSCAN (c
))
13761 error_at (OMP_CLAUSE_LOCATION (c
),
13762 "%<inscan%> %<reduction%> clause with array "
13768 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
13769 if (t
== error_mark_node
)
13775 c_mark_addressable (t
);
13776 type
= TREE_TYPE (t
);
13777 if (TREE_CODE (t
) == MEM_REF
)
13778 type
= TREE_TYPE (type
);
13779 if (TREE_CODE (type
) == ARRAY_TYPE
)
13781 tree oatype
= type
;
13782 gcc_assert (TREE_CODE (t
) != MEM_REF
);
13783 while (TREE_CODE (type
) == ARRAY_TYPE
)
13784 type
= TREE_TYPE (type
);
13785 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
13787 error_at (OMP_CLAUSE_LOCATION (c
),
13788 "%qD in %<reduction%> clause is a zero size array",
13793 tree size
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (oatype
),
13794 TYPE_SIZE_UNIT (type
));
13795 if (integer_zerop (size
))
13797 error_at (OMP_CLAUSE_LOCATION (c
),
13798 "%qD in %<reduction%> clause is a zero size array",
13803 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
13804 size
= save_expr (size
);
13805 tree index_type
= build_index_type (size
);
13806 tree atype
= build_array_type (type
, index_type
);
13807 tree ptype
= build_pointer_type (type
);
13808 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13809 t
= build_fold_addr_expr (t
);
13810 t
= build2 (MEM_REF
, atype
, t
, build_int_cst (ptype
, 0));
13811 OMP_CLAUSE_DECL (c
) = t
;
13813 if (TYPE_ATOMIC (type
))
13815 error_at (OMP_CLAUSE_LOCATION (c
),
13816 "%<_Atomic%> %qE in %<reduction%> clause", t
);
13820 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
13821 || OMP_CLAUSE_REDUCTION_TASK (c
))
13823 /* Disallow zero sized or potentially zero sized task
13825 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
13827 error_at (OMP_CLAUSE_LOCATION (c
),
13828 "zero sized type %qT in %qs clause", type
,
13829 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13833 else if (TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
)
13835 error_at (OMP_CLAUSE_LOCATION (c
),
13836 "variable sized type %qT in %qs clause", type
,
13837 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13842 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
13843 && (FLOAT_TYPE_P (type
)
13844 || TREE_CODE (type
) == COMPLEX_TYPE
))
13846 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
13847 const char *r_name
= NULL
;
13856 if (TREE_CODE (type
) == COMPLEX_TYPE
)
13860 if (TREE_CODE (type
) == COMPLEX_TYPE
)
13872 case TRUTH_ANDIF_EXPR
:
13873 if (FLOAT_TYPE_P (type
))
13876 case TRUTH_ORIF_EXPR
:
13877 if (FLOAT_TYPE_P (type
))
13881 gcc_unreachable ();
13885 error_at (OMP_CLAUSE_LOCATION (c
),
13886 "%qE has invalid type for %<reduction(%s)%>",
13892 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
13894 error_at (OMP_CLAUSE_LOCATION (c
),
13895 "user defined reduction not found for %qE", t
);
13899 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
13901 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
13902 type
= TYPE_MAIN_VARIANT (type
);
13903 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
13904 VAR_DECL
, NULL_TREE
, type
);
13905 tree decl_placeholder
= NULL_TREE
;
13906 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
13907 DECL_ARTIFICIAL (placeholder
) = 1;
13908 DECL_IGNORED_P (placeholder
) = 1;
13909 if (TREE_CODE (t
) == MEM_REF
)
13911 decl_placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
13912 VAR_DECL
, NULL_TREE
, type
);
13913 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
) = decl_placeholder
;
13914 DECL_ARTIFICIAL (decl_placeholder
) = 1;
13915 DECL_IGNORED_P (decl_placeholder
) = 1;
13917 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
13918 c_mark_addressable (placeholder
);
13919 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
13920 c_mark_addressable (decl_placeholder
? decl_placeholder
13921 : OMP_CLAUSE_DECL (c
));
13922 OMP_CLAUSE_REDUCTION_MERGE (c
)
13923 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
13924 TREE_VEC_ELT (list
, 0),
13925 TREE_VEC_ELT (list
, 1),
13926 decl_placeholder
? decl_placeholder
13927 : OMP_CLAUSE_DECL (c
), placeholder
);
13928 OMP_CLAUSE_REDUCTION_MERGE (c
)
13929 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
13930 void_type_node
, NULL_TREE
,
13931 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
13932 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
13933 if (TREE_VEC_LENGTH (list
) == 6)
13935 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
13936 c_mark_addressable (decl_placeholder
? decl_placeholder
13937 : OMP_CLAUSE_DECL (c
));
13938 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
13939 c_mark_addressable (placeholder
);
13940 tree init
= TREE_VEC_ELT (list
, 5);
13941 if (init
== error_mark_node
)
13942 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
13943 OMP_CLAUSE_REDUCTION_INIT (c
)
13944 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
13945 TREE_VEC_ELT (list
, 3),
13946 decl_placeholder
? decl_placeholder
13947 : OMP_CLAUSE_DECL (c
), placeholder
);
13948 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
13950 tree v
= decl_placeholder
? decl_placeholder
: t
;
13951 OMP_CLAUSE_REDUCTION_INIT (c
)
13952 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
,
13953 OMP_CLAUSE_REDUCTION_INIT (c
));
13955 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
13956 c_find_omp_placeholder_r
,
13957 placeholder
, NULL
))
13958 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
13963 tree v
= decl_placeholder
? decl_placeholder
: t
;
13964 if (AGGREGATE_TYPE_P (TREE_TYPE (v
)))
13965 init
= build_constructor (TREE_TYPE (v
), NULL
);
13967 init
= fold_convert (TREE_TYPE (v
), integer_zero_node
);
13968 OMP_CLAUSE_REDUCTION_INIT (c
)
13969 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
, init
);
13971 OMP_CLAUSE_REDUCTION_INIT (c
)
13972 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
13973 void_type_node
, NULL_TREE
,
13974 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
13975 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
13977 if (TREE_CODE (t
) == MEM_REF
)
13979 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))) == NULL_TREE
13980 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))))
13983 sorry ("variable length element type in array "
13984 "%<reduction%> clause");
13988 t
= TREE_OPERAND (t
, 0);
13989 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
13990 t
= TREE_OPERAND (t
, 0);
13991 if (TREE_CODE (t
) == ADDR_EXPR
)
13992 t
= TREE_OPERAND (t
, 0);
13994 goto check_dup_generic_t
;
13996 case OMP_CLAUSE_COPYPRIVATE
:
13997 copyprivate_seen
= true;
14000 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
14001 "%<nowait%> clause must not be used together "
14002 "with %<copyprivate%>");
14003 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
14004 nowait_clause
= NULL
;
14006 goto check_dup_generic
;
14008 case OMP_CLAUSE_COPYIN
:
14009 t
= OMP_CLAUSE_DECL (c
);
14010 if (!VAR_P (t
) || !DECL_THREAD_LOCAL_P (t
))
14012 error_at (OMP_CLAUSE_LOCATION (c
),
14013 "%qE must be %<threadprivate%> for %<copyin%>", t
);
14017 goto check_dup_generic
;
14019 case OMP_CLAUSE_LINEAR
:
14020 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
14021 need_implicitly_determined
= true;
14022 t
= OMP_CLAUSE_DECL (c
);
14023 if (ort
!= C_ORT_OMP_DECLARE_SIMD
14024 && OMP_CLAUSE_LINEAR_KIND (c
) != OMP_CLAUSE_LINEAR_DEFAULT
)
14026 error_at (OMP_CLAUSE_LOCATION (c
),
14027 "modifier should not be specified in %<linear%> "
14028 "clause on %<simd%> or %<for%> constructs");
14029 OMP_CLAUSE_LINEAR_KIND (c
) = OMP_CLAUSE_LINEAR_DEFAULT
;
14031 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
14032 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
14034 error_at (OMP_CLAUSE_LOCATION (c
),
14035 "linear clause applied to non-integral non-pointer "
14036 "variable with type %qT", TREE_TYPE (t
));
14040 if (TYPE_ATOMIC (TREE_TYPE (t
)))
14042 error_at (OMP_CLAUSE_LOCATION (c
),
14043 "%<_Atomic%> %qD in %<linear%> clause", t
);
14047 if (ort
== C_ORT_OMP_DECLARE_SIMD
)
14049 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
14050 if (TREE_CODE (s
) == PARM_DECL
)
14052 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
) = 1;
14053 /* map_head bitmap is used as uniform_head if
14055 if (!bitmap_bit_p (&map_head
, DECL_UID (s
)))
14056 linear_variable_step_check
= true;
14057 goto check_dup_generic
;
14059 if (TREE_CODE (s
) != INTEGER_CST
)
14061 error_at (OMP_CLAUSE_LOCATION (c
),
14062 "%<linear%> clause step %qE is neither constant "
14063 "nor a parameter", s
);
14068 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
14070 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
14071 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
14072 OMP_CLAUSE_DECL (c
), s
);
14073 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
14074 sizetype
, fold_convert (sizetype
, s
),
14076 (sizetype
, OMP_CLAUSE_DECL (c
)));
14077 if (s
== error_mark_node
)
14079 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
14082 OMP_CLAUSE_LINEAR_STEP (c
)
14083 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
14084 goto check_dup_generic
;
14087 t
= OMP_CLAUSE_DECL (c
);
14088 check_dup_generic_t
:
14089 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14091 error_at (OMP_CLAUSE_LOCATION (c
),
14092 "%qE is not a variable in clause %qs", t
,
14093 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14096 else if ((ort
== C_ORT_ACC
14097 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
14098 || (ort
== C_ORT_OMP
14099 && (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_USE_DEVICE_PTR
14100 || (OMP_CLAUSE_CODE (c
)
14101 == OMP_CLAUSE_USE_DEVICE_ADDR
))))
14103 if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
14105 error_at (OMP_CLAUSE_LOCATION (c
),
14107 ? "%qD appears more than once in reduction clauses"
14108 : "%qD appears more than once in data clauses",
14113 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
14115 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14116 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
14117 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
14119 error_at (OMP_CLAUSE_LOCATION (c
),
14120 "%qE appears more than once in data clauses", t
);
14123 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
14124 && bitmap_bit_p (&map_head
, DECL_UID (t
)))
14126 if (ort
== C_ORT_ACC
)
14127 error_at (OMP_CLAUSE_LOCATION (c
),
14128 "%qD appears more than once in data clauses", t
);
14130 error_at (OMP_CLAUSE_LOCATION (c
),
14131 "%qD appears both in data and map clauses", t
);
14135 bitmap_set_bit (&generic_head
, DECL_UID (t
));
14138 case OMP_CLAUSE_FIRSTPRIVATE
:
14139 t
= OMP_CLAUSE_DECL (c
);
14140 need_complete
= true;
14141 need_implicitly_determined
= true;
14142 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14144 error_at (OMP_CLAUSE_LOCATION (c
),
14145 "%qE is not a variable in clause %<firstprivate%>", t
);
14148 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14149 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
14151 error_at (OMP_CLAUSE_LOCATION (c
),
14152 "%qE appears more than once in data clauses", t
);
14155 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
14157 if (ort
== C_ORT_ACC
)
14158 error_at (OMP_CLAUSE_LOCATION (c
),
14159 "%qD appears more than once in data clauses", t
);
14161 error_at (OMP_CLAUSE_LOCATION (c
),
14162 "%qD appears both in data and map clauses", t
);
14166 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
14169 case OMP_CLAUSE_LASTPRIVATE
:
14170 t
= OMP_CLAUSE_DECL (c
);
14171 need_complete
= true;
14172 need_implicitly_determined
= true;
14173 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14175 error_at (OMP_CLAUSE_LOCATION (c
),
14176 "%qE is not a variable in clause %<lastprivate%>", t
);
14179 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14180 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
14182 error_at (OMP_CLAUSE_LOCATION (c
),
14183 "%qE appears more than once in data clauses", t
);
14187 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
14190 case OMP_CLAUSE_ALIGNED
:
14191 t
= OMP_CLAUSE_DECL (c
);
14192 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14194 error_at (OMP_CLAUSE_LOCATION (c
),
14195 "%qE is not a variable in %<aligned%> clause", t
);
14198 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
14199 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
14201 error_at (OMP_CLAUSE_LOCATION (c
),
14202 "%qE in %<aligned%> clause is neither a pointer nor "
14206 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
14208 error_at (OMP_CLAUSE_LOCATION (c
),
14209 "%<_Atomic%> %qD in %<aligned%> clause", t
);
14213 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
14215 error_at (OMP_CLAUSE_LOCATION (c
),
14216 "%qE appears more than once in %<aligned%> clauses",
14221 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
14224 case OMP_CLAUSE_NONTEMPORAL
:
14225 t
= OMP_CLAUSE_DECL (c
);
14226 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14228 error_at (OMP_CLAUSE_LOCATION (c
),
14229 "%qE is not a variable in %<nontemporal%> clause", t
);
14232 else if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
14234 error_at (OMP_CLAUSE_LOCATION (c
),
14235 "%qE appears more than once in %<nontemporal%> "
14240 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
14243 case OMP_CLAUSE_DEPEND
:
14244 t
= OMP_CLAUSE_DECL (c
);
14245 if (t
== NULL_TREE
)
14247 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c
)
14248 == OMP_CLAUSE_DEPEND_SOURCE
);
14251 if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
)
14253 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
14254 for (; t
; t
= TREE_CHAIN (t
))
14256 tree decl
= TREE_VALUE (t
);
14257 if (TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
14259 tree offset
= TREE_PURPOSE (t
);
14260 bool neg
= wi::neg_p (wi::to_wide (offset
));
14261 offset
= fold_unary (ABS_EXPR
, TREE_TYPE (offset
), offset
);
14262 tree t2
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
),
14263 neg
? MINUS_EXPR
: PLUS_EXPR
,
14265 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
14267 fold_convert (sizetype
, t2
),
14268 fold_convert (sizetype
, decl
));
14269 if (t2
== error_mark_node
)
14274 TREE_PURPOSE (t
) = t2
;
14279 if (TREE_CODE (t
) == TREE_LIST
14280 && TREE_PURPOSE (t
)
14281 && TREE_CODE (TREE_PURPOSE (t
)) == TREE_VEC
)
14283 if (TREE_PURPOSE (t
) != last_iterators
)
14284 last_iterators_remove
14285 = c_omp_finish_iterators (TREE_PURPOSE (t
));
14286 last_iterators
= TREE_PURPOSE (t
);
14287 t
= TREE_VALUE (t
);
14288 if (last_iterators_remove
)
14289 t
= error_mark_node
;
14292 last_iterators
= NULL_TREE
;
14293 if (TREE_CODE (t
) == TREE_LIST
)
14295 if (handle_omp_array_sections (c
, ort
))
14297 else if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_DEPOBJ
)
14299 error_at (OMP_CLAUSE_LOCATION (c
),
14300 "%<depend%> clause with %<depobj%> dependence "
14301 "type on array section");
14306 if (t
== error_mark_node
)
14308 else if (!lvalue_p (t
))
14310 error_at (OMP_CLAUSE_LOCATION (c
),
14311 "%qE is not lvalue expression nor array section in "
14312 "%<depend%> clause", t
);
14315 else if (TREE_CODE (t
) == COMPONENT_REF
14316 && DECL_C_BIT_FIELD (TREE_OPERAND (t
, 1)))
14318 error_at (OMP_CLAUSE_LOCATION (c
),
14319 "bit-field %qE in %qs clause", t
, "depend");
14322 else if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_DEPOBJ
)
14324 if (!c_omp_depend_t_p (TREE_TYPE (t
)))
14326 error_at (OMP_CLAUSE_LOCATION (c
),
14327 "%qE does not have %<omp_depend_t%> type in "
14328 "%<depend%> clause with %<depobj%> dependence "
14333 else if (c_omp_depend_t_p (TREE_TYPE (t
)))
14335 error_at (OMP_CLAUSE_LOCATION (c
),
14336 "%qE should not have %<omp_depend_t%> type in "
14337 "%<depend%> clause with dependence type other than "
14343 tree addr
= build_unary_op (OMP_CLAUSE_LOCATION (c
), ADDR_EXPR
,
14345 if (addr
== error_mark_node
)
14349 t
= build_indirect_ref (OMP_CLAUSE_LOCATION (c
), addr
,
14351 if (t
== error_mark_node
)
14353 else if (TREE_CODE (OMP_CLAUSE_DECL (c
)) == TREE_LIST
14354 && TREE_PURPOSE (OMP_CLAUSE_DECL (c
))
14355 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c
)))
14357 TREE_VALUE (OMP_CLAUSE_DECL (c
)) = t
;
14359 OMP_CLAUSE_DECL (c
) = t
;
14364 case OMP_CLAUSE_MAP
:
14365 case OMP_CLAUSE_TO
:
14366 case OMP_CLAUSE_FROM
:
14367 case OMP_CLAUSE__CACHE_
:
14368 t
= OMP_CLAUSE_DECL (c
);
14369 if (TREE_CODE (t
) == TREE_LIST
)
14371 if (handle_omp_array_sections (c
, ort
))
14375 t
= OMP_CLAUSE_DECL (c
);
14376 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
14378 error_at (OMP_CLAUSE_LOCATION (c
),
14379 "array section does not have mappable type "
14381 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14384 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
14386 error_at (OMP_CLAUSE_LOCATION (c
),
14387 "%<_Atomic%> %qE in %qs clause", t
,
14388 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14391 while (TREE_CODE (t
) == ARRAY_REF
)
14392 t
= TREE_OPERAND (t
, 0);
14393 if (TREE_CODE (t
) == COMPONENT_REF
14394 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
14396 while (TREE_CODE (t
) == COMPONENT_REF
)
14397 t
= TREE_OPERAND (t
, 0);
14398 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
14400 if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
14402 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
14403 error_at (OMP_CLAUSE_LOCATION (c
),
14404 "%qD appears more than once in motion "
14406 else if (ort
== C_ORT_ACC
)
14407 error_at (OMP_CLAUSE_LOCATION (c
),
14408 "%qD appears more than once in data "
14411 error_at (OMP_CLAUSE_LOCATION (c
),
14412 "%qD appears more than once in map "
14418 bitmap_set_bit (&map_head
, DECL_UID (t
));
14419 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
14425 if (t
== error_mark_node
)
14430 if (TREE_CODE (t
) == COMPONENT_REF
14431 && (ort
& C_ORT_OMP
)
14432 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE__CACHE_
)
14434 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
14436 error_at (OMP_CLAUSE_LOCATION (c
),
14437 "bit-field %qE in %qs clause",
14438 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14441 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
14443 error_at (OMP_CLAUSE_LOCATION (c
),
14444 "%qE does not have a mappable type in %qs clause",
14445 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14448 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
14450 error_at (OMP_CLAUSE_LOCATION (c
),
14451 "%<_Atomic%> %qE in %qs clause", t
,
14452 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14455 while (TREE_CODE (t
) == COMPONENT_REF
)
14457 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0)))
14460 error_at (OMP_CLAUSE_LOCATION (c
),
14461 "%qE is a member of a union", t
);
14465 t
= TREE_OPERAND (t
, 0);
14469 if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
14471 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
14475 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14477 error_at (OMP_CLAUSE_LOCATION (c
),
14478 "%qE is not a variable in %qs clause", t
,
14479 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14482 else if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
14484 error_at (OMP_CLAUSE_LOCATION (c
),
14485 "%qD is threadprivate variable in %qs clause", t
,
14486 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14489 else if ((OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
14490 || (OMP_CLAUSE_MAP_KIND (c
)
14491 != GOMP_MAP_FIRSTPRIVATE_POINTER
))
14492 && !c_mark_addressable (t
))
14494 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
14495 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
14496 || (OMP_CLAUSE_MAP_KIND (c
)
14497 == GOMP_MAP_FIRSTPRIVATE_POINTER
)
14498 || (OMP_CLAUSE_MAP_KIND (c
)
14499 == GOMP_MAP_FORCE_DEVICEPTR
)))
14500 && t
== OMP_CLAUSE_DECL (c
)
14501 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
14503 error_at (OMP_CLAUSE_LOCATION (c
),
14504 "%qD does not have a mappable type in %qs clause", t
,
14505 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14508 else if (TREE_TYPE (t
) == error_mark_node
)
14510 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
14512 error_at (OMP_CLAUSE_LOCATION (c
),
14513 "%<_Atomic%> %qE in %qs clause", t
,
14514 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14517 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
14518 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
14520 if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14521 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
14523 error_at (OMP_CLAUSE_LOCATION (c
),
14524 "%qD appears more than once in data clauses", t
);
14527 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
14529 if (ort
== C_ORT_ACC
)
14530 error_at (OMP_CLAUSE_LOCATION (c
),
14531 "%qD appears more than once in data clauses", t
);
14533 error_at (OMP_CLAUSE_LOCATION (c
),
14534 "%qD appears both in data and map clauses", t
);
14538 bitmap_set_bit (&generic_head
, DECL_UID (t
));
14540 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
14542 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
14543 error_at (OMP_CLAUSE_LOCATION (c
),
14544 "%qD appears more than once in motion clauses", t
);
14545 else if (ort
== C_ORT_ACC
)
14546 error_at (OMP_CLAUSE_LOCATION (c
),
14547 "%qD appears more than once in data clauses", t
);
14549 error_at (OMP_CLAUSE_LOCATION (c
),
14550 "%qD appears more than once in map clauses", t
);
14553 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14554 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
14556 if (ort
== C_ORT_ACC
)
14557 error_at (OMP_CLAUSE_LOCATION (c
),
14558 "%qD appears more than once in data clauses", t
);
14560 error_at (OMP_CLAUSE_LOCATION (c
),
14561 "%qD appears both in data and map clauses", t
);
14566 bitmap_set_bit (&map_head
, DECL_UID (t
));
14567 if (t
!= OMP_CLAUSE_DECL (c
)
14568 && TREE_CODE (OMP_CLAUSE_DECL (c
)) == COMPONENT_REF
)
14569 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
14573 case OMP_CLAUSE_TO_DECLARE
:
14574 case OMP_CLAUSE_LINK
:
14575 t
= OMP_CLAUSE_DECL (c
);
14576 if (TREE_CODE (t
) == FUNCTION_DECL
14577 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
14579 else if (!VAR_P (t
))
14581 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
14582 error_at (OMP_CLAUSE_LOCATION (c
),
14583 "%qE is neither a variable nor a function name in "
14585 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14587 error_at (OMP_CLAUSE_LOCATION (c
),
14588 "%qE is not a variable in clause %qs", t
,
14589 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14592 else if (DECL_THREAD_LOCAL_P (t
))
14594 error_at (OMP_CLAUSE_LOCATION (c
),
14595 "%qD is threadprivate variable in %qs clause", t
,
14596 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14599 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
14601 error_at (OMP_CLAUSE_LOCATION (c
),
14602 "%qD does not have a mappable type in %qs clause", t
,
14603 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14608 if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
14610 error_at (OMP_CLAUSE_LOCATION (c
),
14611 "%qE appears more than once on the same "
14612 "%<declare target%> directive", t
);
14616 bitmap_set_bit (&generic_head
, DECL_UID (t
));
14619 case OMP_CLAUSE_UNIFORM
:
14620 t
= OMP_CLAUSE_DECL (c
);
14621 if (TREE_CODE (t
) != PARM_DECL
)
14624 error_at (OMP_CLAUSE_LOCATION (c
),
14625 "%qD is not an argument in %<uniform%> clause", t
);
14627 error_at (OMP_CLAUSE_LOCATION (c
),
14628 "%qE is not an argument in %<uniform%> clause", t
);
14632 /* map_head bitmap is used as uniform_head if declare_simd. */
14633 bitmap_set_bit (&map_head
, DECL_UID (t
));
14634 goto check_dup_generic
;
14636 case OMP_CLAUSE_IS_DEVICE_PTR
:
14637 case OMP_CLAUSE_USE_DEVICE_PTR
:
14638 t
= OMP_CLAUSE_DECL (c
);
14639 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
14641 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_USE_DEVICE_PTR
14642 && ort
== C_ORT_OMP
)
14644 error_at (OMP_CLAUSE_LOCATION (c
),
14645 "%qs variable is not a pointer",
14646 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14649 else if (TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
14651 error_at (OMP_CLAUSE_LOCATION (c
),
14652 "%qs variable is neither a pointer nor an array",
14653 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14657 goto check_dup_generic
;
14659 case OMP_CLAUSE_USE_DEVICE_ADDR
:
14660 t
= OMP_CLAUSE_DECL (c
);
14661 if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
14662 c_mark_addressable (t
);
14663 goto check_dup_generic
;
14665 case OMP_CLAUSE_NOWAIT
:
14666 if (copyprivate_seen
)
14668 error_at (OMP_CLAUSE_LOCATION (c
),
14669 "%<nowait%> clause must not be used together "
14670 "with %<copyprivate%>");
14674 nowait_clause
= pc
;
14675 pc
= &OMP_CLAUSE_CHAIN (c
);
14678 case OMP_CLAUSE_ORDER
:
14679 if (ordered_clause
)
14681 error_at (OMP_CLAUSE_LOCATION (c
),
14682 "%<order%> clause must not be used together "
14683 "with %<ordered%>");
14687 else if (order_clause
)
14689 /* Silently remove duplicates. */
14694 pc
= &OMP_CLAUSE_CHAIN (c
);
14697 case OMP_CLAUSE_IF
:
14698 case OMP_CLAUSE_NUM_THREADS
:
14699 case OMP_CLAUSE_NUM_TEAMS
:
14700 case OMP_CLAUSE_THREAD_LIMIT
:
14701 case OMP_CLAUSE_DEFAULT
:
14702 case OMP_CLAUSE_UNTIED
:
14703 case OMP_CLAUSE_COLLAPSE
:
14704 case OMP_CLAUSE_FINAL
:
14705 case OMP_CLAUSE_MERGEABLE
:
14706 case OMP_CLAUSE_DEVICE
:
14707 case OMP_CLAUSE_DIST_SCHEDULE
:
14708 case OMP_CLAUSE_PARALLEL
:
14709 case OMP_CLAUSE_FOR
:
14710 case OMP_CLAUSE_SECTIONS
:
14711 case OMP_CLAUSE_TASKGROUP
:
14712 case OMP_CLAUSE_PROC_BIND
:
14713 case OMP_CLAUSE_DEVICE_TYPE
:
14714 case OMP_CLAUSE_PRIORITY
:
14715 case OMP_CLAUSE_GRAINSIZE
:
14716 case OMP_CLAUSE_NUM_TASKS
:
14717 case OMP_CLAUSE_THREADS
:
14718 case OMP_CLAUSE_SIMD
:
14719 case OMP_CLAUSE_HINT
:
14720 case OMP_CLAUSE_DEFAULTMAP
:
14721 case OMP_CLAUSE_BIND
:
14722 case OMP_CLAUSE_NUM_GANGS
:
14723 case OMP_CLAUSE_NUM_WORKERS
:
14724 case OMP_CLAUSE_VECTOR_LENGTH
:
14725 case OMP_CLAUSE_ASYNC
:
14726 case OMP_CLAUSE_WAIT
:
14727 case OMP_CLAUSE_AUTO
:
14728 case OMP_CLAUSE_INDEPENDENT
:
14729 case OMP_CLAUSE_SEQ
:
14730 case OMP_CLAUSE_GANG
:
14731 case OMP_CLAUSE_WORKER
:
14732 case OMP_CLAUSE_VECTOR
:
14733 case OMP_CLAUSE_TILE
:
14734 case OMP_CLAUSE_IF_PRESENT
:
14735 case OMP_CLAUSE_FINALIZE
:
14736 pc
= &OMP_CLAUSE_CHAIN (c
);
14739 case OMP_CLAUSE_NOGROUP
:
14741 pc
= &OMP_CLAUSE_CHAIN (c
);
14744 case OMP_CLAUSE_SCHEDULE
:
14745 schedule_clause
= c
;
14746 pc
= &OMP_CLAUSE_CHAIN (c
);
14749 case OMP_CLAUSE_ORDERED
:
14750 ordered_clause
= c
;
14753 error_at (OMP_CLAUSE_LOCATION (*order_clause
),
14754 "%<order%> clause must not be used together "
14755 "with %<ordered%>");
14756 *order_clause
= OMP_CLAUSE_CHAIN (*order_clause
);
14757 order_clause
= NULL
;
14759 pc
= &OMP_CLAUSE_CHAIN (c
);
14762 case OMP_CLAUSE_SAFELEN
:
14764 pc
= &OMP_CLAUSE_CHAIN (c
);
14766 case OMP_CLAUSE_SIMDLEN
:
14768 pc
= &OMP_CLAUSE_CHAIN (c
);
14771 case OMP_CLAUSE_INBRANCH
:
14772 case OMP_CLAUSE_NOTINBRANCH
:
14775 error_at (OMP_CLAUSE_LOCATION (c
),
14776 "%<inbranch%> clause is incompatible with "
14777 "%<notinbranch%>");
14781 branch_seen
= true;
14782 pc
= &OMP_CLAUSE_CHAIN (c
);
14785 case OMP_CLAUSE_INCLUSIVE
:
14786 case OMP_CLAUSE_EXCLUSIVE
:
14787 need_complete
= true;
14788 need_implicitly_determined
= true;
14789 t
= OMP_CLAUSE_DECL (c
);
14790 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14792 error_at (OMP_CLAUSE_LOCATION (c
),
14793 "%qE is not a variable in clause %qs", t
,
14794 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14800 gcc_unreachable ();
14805 t
= OMP_CLAUSE_DECL (c
);
14809 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
14810 if (t
== error_mark_node
)
14814 if (need_implicitly_determined
)
14816 const char *share_name
= NULL
;
14818 if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
14819 share_name
= "threadprivate";
14820 else switch (c_omp_predetermined_sharing (t
))
14822 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
14824 case OMP_CLAUSE_DEFAULT_SHARED
:
14825 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_SHARED
14826 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
)
14827 && c_omp_predefined_variable (t
))
14828 /* The __func__ variable and similar function-local
14829 predefined variables may be listed in a shared or
14830 firstprivate clause. */
14832 share_name
= "shared";
14834 case OMP_CLAUSE_DEFAULT_PRIVATE
:
14835 share_name
= "private";
14838 gcc_unreachable ();
14842 error_at (OMP_CLAUSE_LOCATION (c
),
14843 "%qE is predetermined %qs for %qs",
14845 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14848 else if (TREE_READONLY (t
)
14849 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_SHARED
14850 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_FIRSTPRIVATE
)
14852 error_at (OMP_CLAUSE_LOCATION (c
),
14853 "%<const%> qualified %qE may appear only in "
14854 "%<shared%> or %<firstprivate%> clauses", t
);
14861 *pc
= OMP_CLAUSE_CHAIN (c
);
14863 pc
= &OMP_CLAUSE_CHAIN (c
);
14868 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen
),
14869 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)))
14871 error_at (OMP_CLAUSE_LOCATION (simdlen
),
14872 "%<simdlen%> clause value is bigger than "
14873 "%<safelen%> clause value");
14874 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)
14875 = OMP_CLAUSE_SAFELEN_EXPR (safelen
);
14880 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
14881 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
14883 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
14884 "%<nonmonotonic%> schedule modifier specified together "
14885 "with %<ordered%> clause");
14886 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
14887 = (enum omp_clause_schedule_kind
)
14888 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
14889 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
14892 if (reduction_seen
< 0 && ordered_clause
)
14894 error_at (OMP_CLAUSE_LOCATION (ordered_clause
),
14895 "%qs clause specified together with %<inscan%> "
14896 "%<reduction%> clause", "ordered");
14897 reduction_seen
= -2;
14900 if (reduction_seen
< 0 && schedule_clause
)
14902 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
14903 "%qs clause specified together with %<inscan%> "
14904 "%<reduction%> clause", "schedule");
14905 reduction_seen
= -2;
14908 if (linear_variable_step_check
|| reduction_seen
== -2)
14909 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
14911 bool remove
= false;
14912 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
14913 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
)
14914 && !bitmap_bit_p (&map_head
,
14915 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c
))))
14917 error_at (OMP_CLAUSE_LOCATION (c
),
14918 "%<linear%> clause step is a parameter %qD not "
14919 "specified in %<uniform%> clause",
14920 OMP_CLAUSE_LINEAR_STEP (c
));
14923 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
14924 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 0;
14927 *pc
= OMP_CLAUSE_CHAIN (c
);
14929 pc
= &OMP_CLAUSE_CHAIN (c
);
14932 if (nogroup_seen
&& reduction_seen
)
14934 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen
),
14935 "%<nogroup%> clause must not be used together with "
14936 "%<reduction%> clause");
14937 *nogroup_seen
= OMP_CLAUSE_CHAIN (*nogroup_seen
);
14940 bitmap_obstack_release (NULL
);
14944 /* Return code to initialize DST with a copy constructor from SRC.
14945 C doesn't have copy constructors nor assignment operators, only for
14946 _Atomic vars we need to perform __atomic_load from src into a temporary
14947 followed by __atomic_store of the temporary to dst. */
14950 c_omp_clause_copy_ctor (tree clause
, tree dst
, tree src
)
14952 if (!really_atomic_lvalue (dst
) && !really_atomic_lvalue (src
))
14953 return build2 (MODIFY_EXPR
, TREE_TYPE (dst
), dst
, src
);
14955 location_t loc
= OMP_CLAUSE_LOCATION (clause
);
14956 tree type
= TREE_TYPE (dst
);
14957 tree nonatomic_type
= build_qualified_type (type
, TYPE_UNQUALIFIED
);
14958 tree tmp
= create_tmp_var (nonatomic_type
);
14959 tree tmp_addr
= build_fold_addr_expr (tmp
);
14960 TREE_ADDRESSABLE (tmp
) = 1;
14961 TREE_NO_WARNING (tmp
) = 1;
14962 tree src_addr
= build_fold_addr_expr (src
);
14963 tree dst_addr
= build_fold_addr_expr (dst
);
14964 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
14965 vec
<tree
, va_gc
> *params
;
14966 /* Expansion of a generic atomic load may require an addition
14967 element, so allocate enough to prevent a resize. */
14968 vec_alloc (params
, 4);
14970 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14971 tree fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
14972 params
->quick_push (src_addr
);
14973 params
->quick_push (tmp_addr
);
14974 params
->quick_push (seq_cst
);
14975 tree load
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
14977 vec_alloc (params
, 4);
14979 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14980 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
14981 params
->quick_push (dst_addr
);
14982 params
->quick_push (tmp_addr
);
14983 params
->quick_push (seq_cst
);
14984 tree store
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
14985 return build2 (COMPOUND_EXPR
, void_type_node
, load
, store
);
14988 /* Create a transaction node. */
14991 c_finish_transaction (location_t loc
, tree block
, int flags
)
14993 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
14994 if (flags
& TM_STMT_ATTR_OUTER
)
14995 TRANSACTION_EXPR_OUTER (stmt
) = 1;
14996 if (flags
& TM_STMT_ATTR_RELAXED
)
14997 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
14998 return add_stmt (stmt
);
15001 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15002 down to the element type of an array. If ORIG_QUAL_TYPE is not
15003 NULL, then it should be used as the qualified type
15004 ORIG_QUAL_INDIRECT levels down in array type derivation (to
15005 preserve information about the typedef name from which an array
15006 type was derived). */
15009 c_build_qualified_type (tree type
, int type_quals
, tree orig_qual_type
,
15010 size_t orig_qual_indirect
)
15012 if (type
== error_mark_node
)
15015 if (TREE_CODE (type
) == ARRAY_TYPE
)
15018 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
15019 type_quals
, orig_qual_type
,
15020 orig_qual_indirect
- 1);
15022 /* See if we already have an identically qualified type. */
15023 if (orig_qual_type
&& orig_qual_indirect
== 0)
15024 t
= orig_qual_type
;
15026 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
15028 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
15029 && TYPE_NAME (t
) == TYPE_NAME (type
)
15030 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
15031 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
15032 TYPE_ATTRIBUTES (type
)))
15037 tree domain
= TYPE_DOMAIN (type
);
15039 t
= build_variant_type_copy (type
);
15040 TREE_TYPE (t
) = element_type
;
15042 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
15043 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
15044 SET_TYPE_STRUCTURAL_EQUALITY (t
);
15045 else if (TYPE_CANONICAL (element_type
) != element_type
15046 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
15048 tree unqualified_canon
15049 = build_array_type (TYPE_CANONICAL (element_type
),
15050 domain
? TYPE_CANONICAL (domain
)
15052 if (TYPE_REVERSE_STORAGE_ORDER (type
))
15055 = build_distinct_type_copy (unqualified_canon
);
15056 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon
) = 1;
15059 = c_build_qualified_type (unqualified_canon
, type_quals
);
15062 TYPE_CANONICAL (t
) = t
;
15067 /* A restrict-qualified pointer type must be a pointer to object or
15068 incomplete type. Note that the use of POINTER_TYPE_P also allows
15069 REFERENCE_TYPEs, which is appropriate for C++. */
15070 if ((type_quals
& TYPE_QUAL_RESTRICT
)
15071 && (!POINTER_TYPE_P (type
)
15072 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
15074 error ("invalid use of %<restrict%>");
15075 type_quals
&= ~TYPE_QUAL_RESTRICT
;
15078 tree var_type
= (orig_qual_type
&& orig_qual_indirect
== 0
15080 : build_qualified_type (type
, type_quals
));
15081 /* A variant type does not inherit the list of incomplete vars from the
15082 type main variant. */
15083 if (RECORD_OR_UNION_TYPE_P (var_type
)
15084 && TYPE_MAIN_VARIANT (var_type
) != var_type
)
15085 C_TYPE_INCOMPLETE_VARS (var_type
) = 0;
15089 /* Build a VA_ARG_EXPR for the C parser. */
15092 c_build_va_arg (location_t loc1
, tree expr
, location_t loc2
, tree type
)
15094 if (error_operand_p (type
))
15095 return error_mark_node
;
15096 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15097 order because it takes the address of the expression. */
15098 else if (handled_component_p (expr
)
15099 && reverse_storage_order_for_component_p (expr
))
15101 error_at (loc1
, "cannot use %<va_arg%> with reverse storage order");
15102 return error_mark_node
;
15104 else if (!COMPLETE_TYPE_P (type
))
15106 error_at (loc2
, "second argument to %<va_arg%> is of incomplete "
15108 return error_mark_node
;
15110 else if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
15111 warning_at (loc2
, OPT_Wc___compat
,
15112 "C++ requires promoted type, not enum type, in %<va_arg%>");
15113 return build_va_arg (loc2
, expr
, type
);
15116 /* Return truthvalue of whether T1 is the same tree structure as T2.
15117 Return 1 if they are the same. Return false if they are different. */
15120 c_tree_equal (tree t1
, tree t2
)
15122 enum tree_code code1
, code2
;
15129 for (code1
= TREE_CODE (t1
);
15130 CONVERT_EXPR_CODE_P (code1
)
15131 || code1
== NON_LVALUE_EXPR
;
15132 code1
= TREE_CODE (t1
))
15133 t1
= TREE_OPERAND (t1
, 0);
15134 for (code2
= TREE_CODE (t2
);
15135 CONVERT_EXPR_CODE_P (code2
)
15136 || code2
== NON_LVALUE_EXPR
;
15137 code2
= TREE_CODE (t2
))
15138 t2
= TREE_OPERAND (t2
, 0);
15140 /* They might have become equal now. */
15144 if (code1
!= code2
)
15150 return wi::to_wide (t1
) == wi::to_wide (t2
);
15153 return real_equal (&TREE_REAL_CST (t1
), &TREE_REAL_CST (t2
));
15156 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
15157 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
15158 TREE_STRING_LENGTH (t1
));
15161 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
15162 TREE_FIXED_CST (t2
));
15165 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
15166 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
15169 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
15172 /* We need to do this when determining whether or not two
15173 non-type pointer to member function template arguments
15175 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
15176 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
15181 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
15183 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
15184 if (!c_tree_equal (field
, elt2
->index
)
15185 || !c_tree_equal (value
, elt2
->value
))
15192 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
15194 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
15196 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
15199 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
15204 call_expr_arg_iterator iter1
, iter2
;
15205 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
15207 for (arg1
= first_call_expr_arg (t1
, &iter1
),
15208 arg2
= first_call_expr_arg (t2
, &iter2
);
15210 arg1
= next_call_expr_arg (&iter1
),
15211 arg2
= next_call_expr_arg (&iter2
))
15212 if (!c_tree_equal (arg1
, arg2
))
15221 tree o1
= TREE_OPERAND (t1
, 0);
15222 tree o2
= TREE_OPERAND (t2
, 0);
15224 /* Special case: if either target is an unallocated VAR_DECL,
15225 it means that it's going to be unified with whatever the
15226 TARGET_EXPR is really supposed to initialize, so treat it
15227 as being equivalent to anything. */
15228 if (VAR_P (o1
) && DECL_NAME (o1
) == NULL_TREE
15229 && !DECL_RTL_SET_P (o1
))
15231 else if (VAR_P (o2
) && DECL_NAME (o2
) == NULL_TREE
15232 && !DECL_RTL_SET_P (o2
))
15234 else if (!c_tree_equal (o1
, o2
))
15237 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
15240 case COMPONENT_REF
:
15241 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
15243 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
15249 case FUNCTION_DECL
:
15250 case IDENTIFIER_NODE
:
15257 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
15259 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
15260 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
15261 TREE_VEC_ELT (t2
, ix
)))
15270 switch (TREE_CODE_CLASS (code1
))
15274 case tcc_comparison
:
15275 case tcc_expression
:
15277 case tcc_reference
:
15278 case tcc_statement
:
15280 int i
, n
= TREE_OPERAND_LENGTH (t1
);
15284 case PREINCREMENT_EXPR
:
15285 case PREDECREMENT_EXPR
:
15286 case POSTINCREMENT_EXPR
:
15287 case POSTDECREMENT_EXPR
:
15297 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
15298 && n
!= TREE_OPERAND_LENGTH (t2
))
15301 for (i
= 0; i
< n
; ++i
)
15302 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
15309 return comptypes (t1
, t2
);
15311 gcc_unreachable ();
15313 /* We can get here with --disable-checking. */
15317 /* Returns true when the function declaration FNDECL is implicit,
15318 introduced as a result of a call to an otherwise undeclared
15319 function, and false otherwise. */
15322 c_decl_implicit (const_tree fndecl
)
15324 return C_DECL_IMPLICIT (fndecl
);