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 ORIGTYPES, if not NULL, is a vector of types; each element is
3007 either NULL or the original type of the corresponding element in
3008 PARAMS. The original type may differ from TREE_TYPE of the
3009 parameter for enums. FUNCTION's data type may be a function type
3010 or pointer-to-function. This function changes the elements of
3014 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3015 tree function
, vec
<tree
, va_gc
> *params
,
3016 vec
<tree
, va_gc
> *origtypes
)
3018 tree fntype
, fundecl
= NULL_TREE
;
3019 tree name
= NULL_TREE
, result
;
3025 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3026 STRIP_TYPE_NOPS (function
);
3028 /* Convert anything with function type to a pointer-to-function. */
3029 if (TREE_CODE (function
) == FUNCTION_DECL
)
3031 name
= DECL_NAME (function
);
3034 tm_malloc_replacement (function
);
3036 /* Atomic functions have type checking/casting already done. They are
3037 often rewritten and don't match the original parameter list. */
3038 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
3041 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
3042 function
= function_to_pointer_conversion (loc
, function
);
3044 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3045 expressions, like those used for ObjC messenger dispatches. */
3046 if (params
&& !params
->is_empty ())
3047 function
= objc_rewrite_function_call (function
, (*params
)[0]);
3049 function
= c_fully_fold (function
, false, NULL
);
3051 fntype
= TREE_TYPE (function
);
3053 if (TREE_CODE (fntype
) == ERROR_MARK
)
3054 return error_mark_node
;
3056 if (!(TREE_CODE (fntype
) == POINTER_TYPE
3057 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
3059 if (!flag_diagnostics_show_caret
)
3061 "called object %qE is not a function or function pointer",
3063 else if (DECL_P (function
))
3066 "called object %qD is not a function or function pointer",
3068 inform_declaration (function
);
3072 "called object is not a function or function pointer");
3073 return error_mark_node
;
3076 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
3077 current_function_returns_abnormally
= 1;
3079 /* fntype now gets the type of function pointed to. */
3080 fntype
= TREE_TYPE (fntype
);
3082 /* Convert the parameters to the types declared in the
3083 function prototype, or apply default promotions. */
3085 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
3086 origtypes
, function
, fundecl
);
3088 return error_mark_node
;
3090 /* Check that the function is called through a compatible prototype.
3091 If it is not, warn. */
3092 if (CONVERT_EXPR_P (function
)
3093 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
3094 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
3095 && !comptypes (fntype
, TREE_TYPE (tem
)))
3097 tree return_type
= TREE_TYPE (fntype
);
3099 /* This situation leads to run-time undefined behavior. We can't,
3100 therefore, simply error unless we can prove that all possible
3101 executions of the program must execute the code. */
3102 warning_at (loc
, 0, "function called through a non-compatible type");
3104 if (VOID_TYPE_P (return_type
)
3105 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
3107 "function with qualified void return type called");
3110 argarray
= vec_safe_address (params
);
3112 /* Check that arguments to builtin functions match the expectations. */
3113 if (fundecl
&& fndecl_built_in_p (fundecl
, BUILT_IN_NORMAL
)
3114 && !check_builtin_function_arguments (loc
, arg_loc
, fundecl
, nargs
,
3116 return error_mark_node
;
3118 /* Check that the arguments to the function are valid. */
3119 bool warned_p
= check_function_arguments (loc
, fundecl
, fntype
,
3120 nargs
, argarray
, &arg_loc
);
3122 if (name
!= NULL_TREE
3123 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
3125 if (require_constant_value
)
3127 = fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
3128 function
, nargs
, argarray
);
3130 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
3131 function
, nargs
, argarray
);
3132 if (TREE_CODE (result
) == NOP_EXPR
3133 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
3134 STRIP_TYPE_NOPS (result
);
3137 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
3138 function
, nargs
, argarray
);
3139 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3141 if (warned_p
&& TREE_CODE (result
) == CALL_EXPR
)
3142 TREE_NO_WARNING (result
) = 1;
3144 /* In this improbable scenario, a nested function returns a VM type.
3145 Create a TARGET_EXPR so that the call always has a LHS, much as
3146 what the C++ FE does for functions returning non-PODs. */
3147 if (variably_modified_type_p (TREE_TYPE (fntype
), NULL_TREE
))
3149 tree tmp
= create_tmp_var_raw (TREE_TYPE (fntype
));
3150 result
= build4 (TARGET_EXPR
, TREE_TYPE (fntype
), tmp
, result
,
3151 NULL_TREE
, NULL_TREE
);
3154 if (VOID_TYPE_P (TREE_TYPE (result
)))
3156 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
3158 "function with qualified void return type called");
3161 return require_complete_type (loc
, result
);
3164 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3167 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3168 tree function
, vec
<tree
, va_gc
> *params
,
3169 vec
<tree
, va_gc
> *origtypes
)
3171 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3172 STRIP_TYPE_NOPS (function
);
3174 /* Convert anything with function type to a pointer-to-function. */
3175 if (TREE_CODE (function
) == FUNCTION_DECL
)
3177 /* Implement type-directed function overloading for builtins.
3178 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3179 handle all the type checking. The result is a complete expression
3180 that implements this function call. */
3181 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3185 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3188 /* Helper for convert_arguments called to convert the VALue of argument
3189 number ARGNUM from ORIGTYPE to the corresponding parameter number
3191 PLOC is the location where the conversion is being performed.
3192 FUNCTION and FUNDECL are the same as in convert_arguments.
3193 VALTYPE is the original type of VAL before the conversion and,
3194 for EXCESS_PRECISION_EXPR, the operand of the expression.
3195 NPC is true if VAL represents the null pointer constant (VAL itself
3196 will have been folded to an integer constant).
3197 RNAME is the same as FUNCTION except in Objective C when it's
3198 the function selector.
3199 EXCESS_PRECISION is true when VAL was originally represented
3200 as EXCESS_PRECISION_EXPR.
3201 WARNOPT is the same as in convert_for_assignment. */
3204 convert_argument (location_t ploc
, tree function
, tree fundecl
,
3205 tree type
, tree origtype
, tree val
, tree valtype
,
3206 bool npc
, tree rname
, int parmnum
, int argnum
,
3207 bool excess_precision
, int warnopt
)
3209 /* Formal parm type is specified by a function prototype. */
3211 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3213 error_at (ploc
, "type of formal parameter %d is incomplete",
3218 /* Optionally warn about conversions that differ from the default
3220 if (warn_traditional_conversion
|| warn_traditional
)
3222 unsigned int formal_prec
= TYPE_PRECISION (type
);
3224 if (INTEGRAL_TYPE_P (type
)
3225 && TREE_CODE (valtype
) == REAL_TYPE
)
3226 warning_at (ploc
, OPT_Wtraditional_conversion
,
3227 "passing argument %d of %qE as integer rather "
3228 "than floating due to prototype",
3230 if (INTEGRAL_TYPE_P (type
)
3231 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3232 warning_at (ploc
, OPT_Wtraditional_conversion
,
3233 "passing argument %d of %qE as integer rather "
3234 "than complex due to prototype",
3236 else if (TREE_CODE (type
) == COMPLEX_TYPE
3237 && TREE_CODE (valtype
) == REAL_TYPE
)
3238 warning_at (ploc
, OPT_Wtraditional_conversion
,
3239 "passing argument %d of %qE as complex rather "
3240 "than floating due to prototype",
3242 else if (TREE_CODE (type
) == REAL_TYPE
3243 && INTEGRAL_TYPE_P (valtype
))
3244 warning_at (ploc
, OPT_Wtraditional_conversion
,
3245 "passing argument %d of %qE as floating rather "
3246 "than integer due to prototype",
3248 else if (TREE_CODE (type
) == COMPLEX_TYPE
3249 && INTEGRAL_TYPE_P (valtype
))
3250 warning_at (ploc
, OPT_Wtraditional_conversion
,
3251 "passing argument %d of %qE as complex rather "
3252 "than integer due to prototype",
3254 else if (TREE_CODE (type
) == REAL_TYPE
3255 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3256 warning_at (ploc
, OPT_Wtraditional_conversion
,
3257 "passing argument %d of %qE as floating rather "
3258 "than complex due to prototype",
3260 /* ??? At some point, messages should be written about
3261 conversions between complex types, but that's too messy
3263 else if (TREE_CODE (type
) == REAL_TYPE
3264 && TREE_CODE (valtype
) == REAL_TYPE
)
3266 /* Warn if any argument is passed as `float',
3267 since without a prototype it would be `double'. */
3268 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3269 && type
!= dfloat32_type_node
)
3270 warning_at (ploc
, 0,
3271 "passing argument %d of %qE as %<float%> "
3272 "rather than %<double%> due to prototype",
3275 /* Warn if mismatch between argument and prototype
3276 for decimal float types. Warn of conversions with
3277 binary float types and of precision narrowing due to
3279 else if (type
!= valtype
3280 && (type
== dfloat32_type_node
3281 || type
== dfloat64_type_node
3282 || type
== dfloat128_type_node
3283 || valtype
== dfloat32_type_node
3284 || valtype
== dfloat64_type_node
3285 || valtype
== dfloat128_type_node
)
3287 <= TYPE_PRECISION (valtype
)
3288 || (type
== dfloat128_type_node
3290 != dfloat64_type_node
3292 != dfloat32_type_node
)))
3293 || (type
== dfloat64_type_node
3295 != dfloat32_type_node
))))
3296 warning_at (ploc
, 0,
3297 "passing argument %d of %qE as %qT "
3298 "rather than %qT due to prototype",
3299 argnum
, rname
, type
, valtype
);
3302 /* Detect integer changing in width or signedness.
3303 These warnings are only activated with
3304 -Wtraditional-conversion, not with -Wtraditional. */
3305 else if (warn_traditional_conversion
3306 && INTEGRAL_TYPE_P (type
)
3307 && INTEGRAL_TYPE_P (valtype
))
3309 tree would_have_been
= default_conversion (val
);
3310 tree type1
= TREE_TYPE (would_have_been
);
3312 if (val
== error_mark_node
)
3313 /* VAL could have been of incomplete type. */;
3314 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3315 && (TYPE_MAIN_VARIANT (type
)
3316 == TYPE_MAIN_VARIANT (valtype
)))
3317 /* No warning if function asks for enum
3318 and the actual arg is that enum type. */
3320 else if (formal_prec
!= TYPE_PRECISION (type1
))
3321 warning_at (ploc
, OPT_Wtraditional_conversion
,
3322 "passing argument %d of %qE "
3323 "with different width due to prototype",
3325 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3327 /* Don't complain if the formal parameter type
3328 is an enum, because we can't tell now whether
3329 the value was an enum--even the same enum. */
3330 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3332 else if (TREE_CODE (val
) == INTEGER_CST
3333 && int_fits_type_p (val
, type
))
3334 /* Change in signedness doesn't matter
3335 if a constant value is unaffected. */
3337 /* If the value is extended from a narrower
3338 unsigned type, it doesn't matter whether we
3339 pass it as signed or unsigned; the value
3340 certainly is the same either way. */
3341 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3342 && TYPE_UNSIGNED (valtype
))
3344 else if (TYPE_UNSIGNED (type
))
3345 warning_at (ploc
, OPT_Wtraditional_conversion
,
3346 "passing argument %d of %qE "
3347 "as unsigned due to prototype",
3350 warning_at (ploc
, OPT_Wtraditional_conversion
,
3351 "passing argument %d of %qE "
3352 "as signed due to prototype",
3357 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3358 sake of better warnings from convert_and_check. */
3359 if (excess_precision
)
3360 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3362 tree parmval
= convert_for_assignment (ploc
, ploc
, type
,
3363 val
, origtype
, ic_argpass
,
3364 npc
, fundecl
, function
,
3365 parmnum
+ 1, warnopt
);
3367 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3368 && INTEGRAL_TYPE_P (type
)
3369 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3370 parmval
= default_conversion (parmval
);
3375 /* Convert the argument expressions in the vector VALUES
3376 to the types in the list TYPELIST.
3378 If TYPELIST is exhausted, or when an element has NULL as its type,
3379 perform the default conversions.
3381 ORIGTYPES is the original types of the expressions in VALUES. This
3382 holds the type of enum values which have been converted to integral
3383 types. It may be NULL.
3385 FUNCTION is a tree for the called function. It is used only for
3386 error messages, where it is formatted with %qE.
3388 This is also where warnings about wrong number of args are generated.
3390 ARG_LOC are locations of function arguments (if any).
3392 Returns the actual number of arguments processed (which may be less
3393 than the length of VALUES in some error situations), or -1 on
3397 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3398 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3399 tree function
, tree fundecl
)
3401 unsigned int parmnum
;
3402 bool error_args
= false;
3403 const bool type_generic
= fundecl
3404 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3405 bool type_generic_remove_excess_precision
= false;
3406 bool type_generic_overflow_p
= false;
3409 /* Change pointer to function to the function itself for
3411 if (TREE_CODE (function
) == ADDR_EXPR
3412 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3413 function
= TREE_OPERAND (function
, 0);
3415 /* Handle an ObjC selector specially for diagnostics. */
3416 selector
= objc_message_selector ();
3418 /* For a call to a built-in function declared without a prototype,
3419 set to the built-in function's argument list. */
3420 tree builtin_typelist
= NULL_TREE
;
3422 /* For type-generic built-in functions, determine whether excess
3423 precision should be removed (classification) or not
3426 && fndecl_built_in_p (fundecl
, BUILT_IN_NORMAL
))
3428 built_in_function code
= DECL_FUNCTION_CODE (fundecl
);
3429 if (C_DECL_BUILTIN_PROTOTYPE (fundecl
))
3431 /* For a call to a built-in function declared without a prototype
3432 use the types of the parameters of the internal built-in to
3433 match those of the arguments to. */
3434 if (tree bdecl
= builtin_decl_explicit (code
))
3435 builtin_typelist
= TYPE_ARG_TYPES (TREE_TYPE (bdecl
));
3438 /* For type-generic built-in functions, determine whether excess
3439 precision should be removed (classification) or not
3444 case BUILT_IN_ISFINITE
:
3445 case BUILT_IN_ISINF
:
3446 case BUILT_IN_ISINF_SIGN
:
3447 case BUILT_IN_ISNAN
:
3448 case BUILT_IN_ISNORMAL
:
3449 case BUILT_IN_FPCLASSIFY
:
3450 type_generic_remove_excess_precision
= true;
3453 case BUILT_IN_ADD_OVERFLOW_P
:
3454 case BUILT_IN_SUB_OVERFLOW_P
:
3455 case BUILT_IN_MUL_OVERFLOW_P
:
3456 /* The last argument of these type-generic builtins
3457 should not be promoted. */
3458 type_generic_overflow_p
= true;
3466 /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3467 individual converted arguments. */
3469 tree typetail
, builtin_typetail
, val
;
3470 for (typetail
= typelist
,
3471 builtin_typetail
= builtin_typelist
,
3473 values
&& values
->iterate (parmnum
, &val
);
3476 /* The type of the function parameter (if it was declared with one). */
3477 tree type
= typetail
? TREE_VALUE (typetail
) : NULL_TREE
;
3478 /* The type of the built-in function parameter (if the function
3479 is a built-in). Used to detect type incompatibilities in
3480 calls to built-ins declared without a prototype. */
3481 tree builtin_type
= (builtin_typetail
3482 ? TREE_VALUE (builtin_typetail
) : NULL_TREE
);
3483 /* The original type of the argument being passed to the function. */
3484 tree valtype
= TREE_TYPE (val
);
3485 /* The called function (or function selector in Objective C). */
3486 tree rname
= function
;
3487 int argnum
= parmnum
+ 1;
3488 const char *invalid_func_diag
;
3489 /* Set for EXCESS_PRECISION_EXPR arguments. */
3490 bool excess_precision
= false;
3491 /* The value of the argument after conversion to the type
3492 of the function parameter it is passed to. */
3494 /* Some __atomic_* builtins have additional hidden argument at
3497 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3498 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3501 if (type
== void_type_node
)
3504 error_at (loc
, "too many arguments to method %qE", selector
);
3506 error_at (loc
, "too many arguments to function %qE", function
);
3507 inform_declaration (fundecl
);
3508 return error_args
? -1 : (int) parmnum
;
3511 if (builtin_type
== void_type_node
)
3513 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
3514 "too many arguments to built-in function %qE "
3515 "expecting %d", function
, parmnum
))
3516 inform_declaration (fundecl
);
3517 builtin_typetail
= NULL_TREE
;
3520 if (selector
&& argnum
> 2)
3526 /* Determine if VAL is a null pointer constant before folding it. */
3527 bool npc
= null_pointer_constant_p (val
);
3529 /* If there is excess precision and a prototype, convert once to
3530 the required type rather than converting via the semantic
3531 type. Likewise without a prototype a float value represented
3532 as long double should be converted once to double. But for
3533 type-generic classification functions excess precision must
3535 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3536 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3538 val
= TREE_OPERAND (val
, 0);
3539 excess_precision
= true;
3541 val
= c_fully_fold (val
, false, NULL
);
3542 STRIP_TYPE_NOPS (val
);
3544 val
= require_complete_type (ploc
, val
);
3546 /* Some floating-point arguments must be promoted to double when
3547 no type is specified by a prototype. This applies to
3548 arguments of type float, and to architecture-specific types
3549 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3550 bool promote_float_arg
= false;
3551 if (type
== NULL_TREE
3552 && TREE_CODE (valtype
) == REAL_TYPE
3553 && (TYPE_PRECISION (valtype
)
3554 <= TYPE_PRECISION (double_type_node
))
3555 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3556 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3557 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3559 /* Promote this argument, unless it has a _FloatN or
3561 promote_float_arg
= true;
3562 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
3563 if (TYPE_MAIN_VARIANT (valtype
) == FLOATN_NX_TYPE_NODE (i
))
3565 promote_float_arg
= false;
3570 if (type
!= NULL_TREE
)
3572 tree origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3573 parmval
= convert_argument (ploc
, function
, fundecl
, type
, origtype
,
3574 val
, valtype
, npc
, rname
, parmnum
, argnum
,
3575 excess_precision
, 0);
3577 else if (promote_float_arg
)
3583 /* Convert `float' to `double'. */
3584 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3585 warning_at (ploc
, OPT_Wdouble_promotion
,
3586 "implicit conversion from %qT to %qT when passing "
3587 "argument to function",
3588 valtype
, double_type_node
);
3589 parmval
= convert (double_type_node
, val
);
3592 else if ((excess_precision
&& !type_generic
)
3593 || (type_generic_overflow_p
&& parmnum
== 2))
3594 /* A "double" argument with excess precision being passed
3595 without a prototype or in variable arguments.
3596 The last argument of __builtin_*_overflow_p should not be
3598 parmval
= convert (valtype
, val
);
3599 else if ((invalid_func_diag
=
3600 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3602 error (invalid_func_diag
);
3605 else if (TREE_CODE (val
) == ADDR_EXPR
&& reject_gcc_builtin (val
))
3610 /* Convert `short' and `char' to full-size `int'. */
3611 parmval
= default_conversion (val
);
3613 (*values
)[parmnum
] = parmval
;
3614 if (parmval
== error_mark_node
)
3617 if (!type
&& builtin_type
&& TREE_CODE (builtin_type
) != VOID_TYPE
)
3619 /* For a call to a built-in function declared without a prototype,
3620 perform the conversions from the argument to the expected type
3621 but issue warnings rather than errors for any mismatches.
3622 Ignore the converted argument and use the PARMVAL obtained
3623 above by applying default conversions instead. */
3624 tree origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3625 convert_argument (ploc
, function
, fundecl
, builtin_type
, origtype
,
3626 val
, valtype
, npc
, rname
, parmnum
, argnum
,
3628 OPT_Wbuiltin_declaration_mismatch
);
3632 typetail
= TREE_CHAIN (typetail
);
3634 if (builtin_typetail
)
3635 builtin_typetail
= TREE_CHAIN (builtin_typetail
);
3638 gcc_assert (parmnum
== vec_safe_length (values
));
3640 if (typetail
!= NULL_TREE
&& TREE_VALUE (typetail
) != void_type_node
)
3642 error_at (loc
, "too few arguments to function %qE", function
);
3643 inform_declaration (fundecl
);
3647 if (builtin_typetail
&& TREE_VALUE (builtin_typetail
) != void_type_node
)
3649 unsigned nargs
= parmnum
;
3650 for (tree t
= builtin_typetail
; t
; t
= TREE_CHAIN (t
))
3653 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
3654 "too few arguments to built-in function %qE "
3655 "expecting %u", function
, nargs
- 1))
3656 inform_declaration (fundecl
);
3659 return error_args
? -1 : (int) parmnum
;
3662 /* This is the entry point used by the parser to build unary operators
3663 in the input. CODE, a tree_code, specifies the unary operator, and
3664 ARG is the operand. For unary plus, the C parser currently uses
3665 CONVERT_EXPR for code.
3667 LOC is the location to use for the tree generated.
3671 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3673 struct c_expr result
;
3675 result
.original_code
= code
;
3676 result
.original_type
= NULL
;
3678 if (reject_gcc_builtin (arg
.value
))
3680 result
.value
= error_mark_node
;
3684 result
.value
= build_unary_op (loc
, code
, arg
.value
, false);
3686 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3687 overflow_warning (loc
, result
.value
, arg
.value
);
3690 /* We are typically called when parsing a prefix token at LOC acting on
3691 ARG. Reflect this by updating the source range of the result to
3692 start at LOC and end at the end of ARG. */
3693 set_c_expr_source_range (&result
,
3694 loc
, arg
.get_finish ());
3699 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3702 char_type_p (tree type
)
3704 return (type
== char_type_node
3705 || type
== unsigned_char_type_node
3706 || type
== signed_char_type_node
3707 || type
== char16_type_node
3708 || type
== char32_type_node
);
3711 /* This is the entry point used by the parser to build binary operators
3712 in the input. CODE, a tree_code, specifies the binary operator, and
3713 ARG1 and ARG2 are the operands. In addition to constructing the
3714 expression, we check for operands that were written with other binary
3715 operators in a way that is likely to confuse the user.
3717 LOCATION is the location of the binary operator. */
3720 parser_build_binary_op (location_t location
, enum tree_code code
,
3721 struct c_expr arg1
, struct c_expr arg2
)
3723 struct c_expr result
;
3725 enum tree_code code1
= arg1
.original_code
;
3726 enum tree_code code2
= arg2
.original_code
;
3727 tree type1
= (arg1
.original_type
3728 ? arg1
.original_type
3729 : TREE_TYPE (arg1
.value
));
3730 tree type2
= (arg2
.original_type
3731 ? arg2
.original_type
3732 : TREE_TYPE (arg2
.value
));
3734 result
.value
= build_binary_op (location
, code
,
3735 arg1
.value
, arg2
.value
, true);
3736 result
.original_code
= code
;
3737 result
.original_type
= NULL
;
3739 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3741 set_c_expr_source_range (&result
,
3743 arg2
.get_finish ());
3747 if (location
!= UNKNOWN_LOCATION
)
3748 protected_set_expr_location (result
.value
, location
);
3750 set_c_expr_source_range (&result
,
3752 arg2
.get_finish ());
3754 /* Check for cases such as x+y<<z which users are likely
3756 if (warn_parentheses
)
3757 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3760 if (warn_logical_op
)
3761 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3762 code1
, arg1
.value
, code2
, arg2
.value
);
3764 if (warn_tautological_compare
)
3766 tree lhs
= arg1
.value
;
3767 tree rhs
= arg2
.value
;
3768 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
3770 if (C_MAYBE_CONST_EXPR_PRE (lhs
) != NULL_TREE
3771 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs
)))
3774 lhs
= C_MAYBE_CONST_EXPR_EXPR (lhs
);
3776 if (TREE_CODE (rhs
) == C_MAYBE_CONST_EXPR
)
3778 if (C_MAYBE_CONST_EXPR_PRE (rhs
) != NULL_TREE
3779 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs
)))
3782 rhs
= C_MAYBE_CONST_EXPR_EXPR (rhs
);
3784 if (lhs
!= NULL_TREE
&& rhs
!= NULL_TREE
)
3785 warn_tautological_cmp (location
, code
, lhs
, rhs
);
3788 if (warn_logical_not_paren
3789 && TREE_CODE_CLASS (code
) == tcc_comparison
3790 && code1
== TRUTH_NOT_EXPR
3791 && code2
!= TRUTH_NOT_EXPR
3792 /* Avoid warning for !!x == y. */
3793 && (TREE_CODE (arg1
.value
) != NE_EXPR
3794 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
3796 /* Avoid warning for !b == y where b has _Bool type. */
3797 tree t
= integer_zero_node
;
3798 if (TREE_CODE (arg1
.value
) == EQ_EXPR
3799 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
3800 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
3802 t
= TREE_OPERAND (arg1
.value
, 0);
3805 if (TREE_TYPE (t
) != integer_type_node
)
3807 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
3808 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
3809 else if (CONVERT_EXPR_P (t
))
3810 t
= TREE_OPERAND (t
, 0);
3816 if (TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
)
3817 warn_logical_not_parentheses (location
, code
, arg1
.value
, arg2
.value
);
3820 /* Warn about comparisons against string literals, with the exception
3821 of testing for equality or inequality of a string literal with NULL. */
3822 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3824 if ((code1
== STRING_CST
3825 && !integer_zerop (tree_strip_nop_conversions (arg2
.value
)))
3826 || (code2
== STRING_CST
3827 && !integer_zerop (tree_strip_nop_conversions (arg1
.value
))))
3828 warning_at (location
, OPT_Waddress
,
3829 "comparison with string literal results in unspecified behavior");
3830 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3831 if (POINTER_TYPE_P (type1
)
3832 && null_pointer_constant_p (arg2
.value
)
3833 && char_type_p (type2
))
3835 auto_diagnostic_group d
;
3836 if (warning_at (location
, OPT_Wpointer_compare
,
3837 "comparison between pointer and zero character "
3839 inform (arg1
.get_start (),
3840 "did you mean to dereference the pointer?");
3842 else if (POINTER_TYPE_P (type2
)
3843 && null_pointer_constant_p (arg1
.value
)
3844 && char_type_p (type1
))
3846 auto_diagnostic_group d
;
3847 if (warning_at (location
, OPT_Wpointer_compare
,
3848 "comparison between pointer and zero character "
3850 inform (arg2
.get_start (),
3851 "did you mean to dereference the pointer?");
3854 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3855 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3856 warning_at (location
, OPT_Waddress
,
3857 "comparison with string literal results in unspecified behavior");
3859 if (TREE_OVERFLOW_P (result
.value
)
3860 && !TREE_OVERFLOW_P (arg1
.value
)
3861 && !TREE_OVERFLOW_P (arg2
.value
))
3862 overflow_warning (location
, result
.value
);
3864 /* Warn about comparisons of different enum types. */
3865 if (warn_enum_compare
3866 && TREE_CODE_CLASS (code
) == tcc_comparison
3867 && TREE_CODE (type1
) == ENUMERAL_TYPE
3868 && TREE_CODE (type2
) == ENUMERAL_TYPE
3869 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3870 warning_at (location
, OPT_Wenum_compare
,
3871 "comparison between %qT and %qT",
3877 /* Return a tree for the difference of pointers OP0 and OP1.
3878 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3879 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3882 pointer_diff (location_t loc
, tree op0
, tree op1
, tree
*instrument_expr
)
3884 tree restype
= ptrdiff_type_node
;
3885 tree result
, inttype
;
3887 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3888 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3889 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3890 tree orig_op1
= op1
;
3892 /* If the operands point into different address spaces, we need to
3893 explicitly convert them to pointers into the common address space
3894 before we can subtract the numerical address values. */
3897 addr_space_t as_common
;
3900 /* Determine the common superset address space. This is guaranteed
3901 to exist because the caller verified that comp_target_types
3902 returned non-zero. */
3903 if (!addr_space_superset (as0
, as1
, &as_common
))
3906 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3907 op0
= convert (common_type
, op0
);
3908 op1
= convert (common_type
, op1
);
3911 /* Determine integer type result of the subtraction. This will usually
3912 be the same as the result type (ptrdiff_t), but may need to be a wider
3913 type if pointers for the address space are wider than ptrdiff_t. */
3914 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3915 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3919 if (TREE_CODE (target_type
) == VOID_TYPE
)
3920 pedwarn (loc
, OPT_Wpointer_arith
,
3921 "pointer of type %<void *%> used in subtraction");
3922 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3923 pedwarn (loc
, OPT_Wpointer_arith
,
3924 "pointer to a function used in subtraction");
3926 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
3928 gcc_assert (current_function_decl
!= NULL_TREE
);
3930 op0
= save_expr (op0
);
3931 op1
= save_expr (op1
);
3933 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
3934 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
3937 /* First do the subtraction, then build the divide operator
3938 and only convert at the very end.
3939 Do not do default conversions in case restype is a short type. */
3941 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3942 pointers. If some platform cannot provide that, or has a larger
3943 ptrdiff_type to support differences larger than half the address
3944 space, cast the pointers to some larger integer type and do the
3945 computations in that type. */
3946 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
3947 op0
= build_binary_op (loc
, MINUS_EXPR
, convert (inttype
, op0
),
3948 convert (inttype
, op1
), false);
3951 /* Cast away qualifiers. */
3952 op0
= convert (c_common_type (TREE_TYPE (op0
), TREE_TYPE (op0
)), op0
);
3953 op1
= convert (c_common_type (TREE_TYPE (op1
), TREE_TYPE (op1
)), op1
);
3954 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
3957 /* This generates an error if op1 is pointer to incomplete type. */
3958 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3959 error_at (loc
, "arithmetic on pointer to an incomplete type");
3961 op1
= c_size_in_bytes (target_type
);
3963 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3964 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3966 /* Divide by the size, in easiest possible way. */
3967 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3968 op0
, convert (inttype
, op1
));
3970 /* Convert to final result type if necessary. */
3971 return convert (restype
, result
);
3974 /* Expand atomic compound assignments into an appropriate sequence as
3975 specified by the C11 standard section 6.5.16.2.
3981 This sequence is used for all types for which these operations are
3984 In addition, built-in versions of the 'fe' prefixed routines may
3985 need to be invoked for floating point (real, complex or vector) when
3986 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3996 __atomic_load (addr, &old, SEQ_CST);
3997 feholdexcept (&fenv);
3999 newval = old op val;
4000 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4003 feclearexcept (FE_ALL_EXCEPT);
4006 feupdateenv (&fenv);
4008 The compiler will issue the __atomic_fetch_* built-in when possible,
4009 otherwise it will generate the generic form of the atomic operations.
4010 This requires temp(s) and has their address taken. The atomic processing
4011 is smart enough to figure out when the size of an object can utilize
4012 a lock-free version, and convert the built-in call to the appropriate
4013 lock-free routine. The optimizers will then dispose of any temps that
4014 are no longer required, and lock-free implementations are utilized as
4015 long as there is target support for the required size.
4017 If the operator is NOP_EXPR, then this is a simple assignment, and
4018 an __atomic_store is issued to perform the assignment rather than
4021 /* Build an atomic assignment at LOC, expanding into the proper
4022 sequence to store LHS MODIFYCODE= RHS. Return a value representing
4023 the result of the operation, unless RETURN_OLD_P, in which case
4024 return the old value of LHS (this is only for postincrement and
4028 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
4029 tree rhs
, bool return_old_p
)
4031 tree fndecl
, func_call
;
4032 vec
<tree
, va_gc
> *params
;
4033 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
4036 tree stmt
, goto_stmt
;
4037 tree loop_label
, loop_decl
, done_label
, done_decl
;
4039 tree lhs_type
= TREE_TYPE (lhs
);
4040 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, false);
4041 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
4042 tree rhs_semantic_type
= TREE_TYPE (rhs
);
4043 tree nonatomic_rhs_semantic_type
;
4046 gcc_assert (TYPE_ATOMIC (lhs_type
));
4049 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
4051 /* Allocate enough vector items for a compare_exchange. */
4052 vec_alloc (params
, 6);
4054 /* Create a compound statement to hold the sequence of statements
4056 compound_stmt
= c_begin_compound_stmt (false);
4058 /* Remove any excess precision (which is only present here in the
4059 case of compound assignments). */
4060 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4062 gcc_assert (modifycode
!= NOP_EXPR
);
4063 rhs
= TREE_OPERAND (rhs
, 0);
4065 rhs_type
= TREE_TYPE (rhs
);
4067 /* Fold the RHS if it hasn't already been folded. */
4068 if (modifycode
!= NOP_EXPR
)
4069 rhs
= c_fully_fold (rhs
, false, NULL
);
4071 /* Remove the qualifiers for the rest of the expressions and create
4072 the VAL temp variable to hold the RHS. */
4073 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
4074 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
4075 nonatomic_rhs_semantic_type
= build_qualified_type (rhs_semantic_type
,
4077 val
= create_tmp_var_raw (nonatomic_rhs_type
);
4078 TREE_ADDRESSABLE (val
) = 1;
4079 TREE_NO_WARNING (val
) = 1;
4080 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
4082 SET_EXPR_LOCATION (rhs
, loc
);
4085 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4087 if (modifycode
== NOP_EXPR
)
4089 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
4090 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, false);
4091 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
4092 params
->quick_push (lhs_addr
);
4093 params
->quick_push (rhs
);
4094 params
->quick_push (seq_cst
);
4095 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4096 add_stmt (func_call
);
4098 /* Finish the compound statement. */
4099 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4101 /* VAL is the value which was stored, return a COMPOUND_STMT of
4102 the statement and that value. */
4103 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
4106 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4107 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4108 isn't applicable for such builtins. ??? Do we want to handle enums? */
4109 if ((TREE_CODE (lhs_type
) == INTEGER_TYPE
|| POINTER_TYPE_P (lhs_type
))
4110 && TREE_CODE (rhs_type
) == INTEGER_TYPE
)
4112 built_in_function fncode
;
4116 case POINTER_PLUS_EXPR
:
4117 fncode
= (return_old_p
4118 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4119 : BUILT_IN_ATOMIC_ADD_FETCH_N
);
4122 fncode
= (return_old_p
4123 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4124 : BUILT_IN_ATOMIC_SUB_FETCH_N
);
4127 fncode
= (return_old_p
4128 ? BUILT_IN_ATOMIC_FETCH_AND_N
4129 : BUILT_IN_ATOMIC_AND_FETCH_N
);
4132 fncode
= (return_old_p
4133 ? BUILT_IN_ATOMIC_FETCH_OR_N
4134 : BUILT_IN_ATOMIC_OR_FETCH_N
);
4137 fncode
= (return_old_p
4138 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4139 : BUILT_IN_ATOMIC_XOR_FETCH_N
);
4145 /* We can only use "_1" through "_16" variants of the atomic fetch
4147 unsigned HOST_WIDE_INT size
= tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type
));
4148 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8 && size
!= 16)
4151 /* If this is a pointer type, we need to multiply by the size of
4152 the pointer target type. */
4153 if (POINTER_TYPE_P (lhs_type
))
4155 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type
))
4156 /* ??? This would introduce -Wdiscarded-qualifiers
4157 warning: __atomic_fetch_* expect volatile void *
4158 type as the first argument. (Assignments between
4159 atomic and non-atomic objects are OK.) */
4160 || TYPE_RESTRICT (lhs_type
))
4162 tree sz
= TYPE_SIZE_UNIT (TREE_TYPE (lhs_type
));
4163 rhs
= fold_build2_loc (loc
, MULT_EXPR
, ptrdiff_type_node
,
4164 convert (ptrdiff_type_node
, rhs
),
4165 convert (ptrdiff_type_node
, sz
));
4168 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4169 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4170 fndecl
= builtin_decl_explicit (fncode
);
4171 params
->quick_push (lhs_addr
);
4172 params
->quick_push (rhs
);
4173 params
->quick_push (seq_cst
);
4174 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4176 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4177 TREE_ADDRESSABLE (newval
) = 1;
4178 TREE_NO_WARNING (newval
) = 1;
4179 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, func_call
,
4180 NULL_TREE
, NULL_TREE
);
4181 SET_EXPR_LOCATION (rhs
, loc
);
4184 /* Finish the compound statement. */
4185 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4187 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4188 the statement and that value. */
4189 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, newval
);
4193 /* Create the variables and labels required for the op= form. */
4194 old
= create_tmp_var_raw (nonatomic_lhs_type
);
4195 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, false);
4196 TREE_ADDRESSABLE (old
) = 1;
4197 TREE_NO_WARNING (old
) = 1;
4199 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4200 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, false);
4201 TREE_ADDRESSABLE (newval
) = 1;
4202 TREE_NO_WARNING (newval
) = 1;
4204 loop_decl
= create_artificial_label (loc
);
4205 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
4207 done_decl
= create_artificial_label (loc
);
4208 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
4210 /* __atomic_load (addr, &old, SEQ_CST). */
4211 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
4212 params
->quick_push (lhs_addr
);
4213 params
->quick_push (old_addr
);
4214 params
->quick_push (seq_cst
);
4215 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4216 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
4219 params
->truncate (0);
4221 /* Create the expressions for floating-point environment
4222 manipulation, if required. */
4223 bool need_fenv
= (flag_trapping_math
4224 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
4225 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
4227 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
4230 add_stmt (hold_call
);
4233 add_stmt (loop_label
);
4235 /* newval = old + val; */
4236 if (rhs_type
!= rhs_semantic_type
)
4237 val
= build1 (EXCESS_PRECISION_EXPR
, nonatomic_rhs_semantic_type
, val
);
4238 rhs
= build_binary_op (loc
, modifycode
, old
, val
, true);
4239 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4241 tree eptype
= TREE_TYPE (rhs
);
4242 rhs
= c_fully_fold (TREE_OPERAND (rhs
, 0), false, NULL
);
4243 rhs
= build1 (EXCESS_PRECISION_EXPR
, eptype
, rhs
);
4246 rhs
= c_fully_fold (rhs
, false, NULL
);
4247 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
4248 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
4250 if (rhs
!= error_mark_node
)
4252 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
4254 SET_EXPR_LOCATION (rhs
, loc
);
4258 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4260 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
4261 params
->quick_push (lhs_addr
);
4262 params
->quick_push (old_addr
);
4263 params
->quick_push (newval_addr
);
4264 params
->quick_push (integer_zero_node
);
4265 params
->quick_push (seq_cst
);
4266 params
->quick_push (seq_cst
);
4267 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4269 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
4270 SET_EXPR_LOCATION (goto_stmt
, loc
);
4272 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
4273 SET_EXPR_LOCATION (stmt
, loc
);
4277 add_stmt (clear_call
);
4280 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
4281 SET_EXPR_LOCATION (goto_stmt
, loc
);
4282 add_stmt (goto_stmt
);
4285 add_stmt (done_label
);
4288 add_stmt (update_call
);
4290 /* Finish the compound statement. */
4291 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4293 /* NEWVAL is the value that was successfully stored, return a
4294 COMPOUND_EXPR of the statement and the appropriate value. */
4295 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
4296 return_old_p
? old
: newval
);
4299 /* Construct and perhaps optimize a tree representation
4300 for a unary operation. CODE, a tree_code, specifies the operation
4301 and XARG is the operand.
4302 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4303 promotions (such as from short to int).
4304 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4305 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4308 LOCATION is the location of the operator. */
4311 build_unary_op (location_t location
, enum tree_code code
, tree xarg
,
4314 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4316 tree argtype
= NULL_TREE
;
4317 enum tree_code typecode
;
4319 tree ret
= error_mark_node
;
4320 tree eptype
= NULL_TREE
;
4321 const char *invalid_op_diag
;
4324 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
4326 arg
= remove_c_maybe_const_expr (arg
);
4328 if (code
!= ADDR_EXPR
)
4329 arg
= require_complete_type (location
, arg
);
4331 typecode
= TREE_CODE (TREE_TYPE (arg
));
4332 if (typecode
== ERROR_MARK
)
4333 return error_mark_node
;
4334 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
4335 typecode
= INTEGER_TYPE
;
4337 if ((invalid_op_diag
4338 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
4340 error_at (location
, invalid_op_diag
);
4341 return error_mark_node
;
4344 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
4346 eptype
= TREE_TYPE (arg
);
4347 arg
= TREE_OPERAND (arg
, 0);
4353 /* This is used for unary plus, because a CONVERT_EXPR
4354 is enough to prevent anybody from looking inside for
4355 associativity, but won't generate any code. */
4356 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4357 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4358 || typecode
== VECTOR_TYPE
))
4360 error_at (location
, "wrong type argument to unary plus");
4361 return error_mark_node
;
4363 else if (!noconvert
)
4364 arg
= default_conversion (arg
);
4365 arg
= non_lvalue_loc (location
, arg
);
4369 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4370 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4371 || typecode
== VECTOR_TYPE
))
4373 error_at (location
, "wrong type argument to unary minus");
4374 return error_mark_node
;
4376 else if (!noconvert
)
4377 arg
= default_conversion (arg
);
4381 /* ~ works on integer types and non float vectors. */
4382 if (typecode
== INTEGER_TYPE
4383 || (typecode
== VECTOR_TYPE
4384 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
4388 /* Warn if the expression has boolean value. */
4389 while (TREE_CODE (e
) == COMPOUND_EXPR
)
4390 e
= TREE_OPERAND (e
, 1);
4392 if ((TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
4393 || truth_value_p (TREE_CODE (e
))))
4395 auto_diagnostic_group d
;
4396 if (warning_at (location
, OPT_Wbool_operation
,
4397 "%<~%> on a boolean expression"))
4399 gcc_rich_location
richloc (location
);
4400 richloc
.add_fixit_insert_before (location
, "!");
4401 inform (&richloc
, "did you mean to use logical not?");
4405 arg
= default_conversion (arg
);
4407 else if (typecode
== COMPLEX_TYPE
)
4410 pedwarn (location
, OPT_Wpedantic
,
4411 "ISO C does not support %<~%> for complex conjugation");
4413 arg
= default_conversion (arg
);
4417 error_at (location
, "wrong type argument to bit-complement");
4418 return error_mark_node
;
4423 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
4425 error_at (location
, "wrong type argument to abs");
4426 return error_mark_node
;
4428 else if (!noconvert
)
4429 arg
= default_conversion (arg
);
4433 if (!(typecode
== INTEGER_TYPE
))
4435 error_at (location
, "wrong type argument to absu");
4436 return error_mark_node
;
4438 else if (!noconvert
)
4439 arg
= default_conversion (arg
);
4443 /* Conjugating a real value is a no-op, but allow it anyway. */
4444 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4445 || typecode
== COMPLEX_TYPE
))
4447 error_at (location
, "wrong type argument to conjugation");
4448 return error_mark_node
;
4450 else if (!noconvert
)
4451 arg
= default_conversion (arg
);
4454 case TRUTH_NOT_EXPR
:
4455 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4456 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
4457 && typecode
!= COMPLEX_TYPE
)
4460 "wrong type argument to unary exclamation mark");
4461 return error_mark_node
;
4465 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
4466 arg
= remove_c_maybe_const_expr (arg
);
4469 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
4470 ret
= invert_truthvalue_loc (location
, arg
);
4471 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4472 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
4473 location
= EXPR_LOCATION (ret
);
4474 goto return_build_unary_op
;
4478 ret
= build_real_imag_expr (location
, code
, arg
);
4479 if (ret
== error_mark_node
)
4480 return error_mark_node
;
4481 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
4482 eptype
= TREE_TYPE (eptype
);
4483 goto return_build_unary_op
;
4485 case PREINCREMENT_EXPR
:
4486 case POSTINCREMENT_EXPR
:
4487 case PREDECREMENT_EXPR
:
4488 case POSTDECREMENT_EXPR
:
4490 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4492 tree inner
= build_unary_op (location
, code
,
4493 C_MAYBE_CONST_EXPR_EXPR (arg
),
4495 if (inner
== error_mark_node
)
4496 return error_mark_node
;
4497 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4498 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4499 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4500 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
4501 goto return_build_unary_op
;
4504 /* Complain about anything that is not a true lvalue. In
4505 Objective-C, skip this check for property_refs. */
4506 if (!objc_is_property_ref (arg
)
4507 && !lvalue_or_else (location
,
4508 arg
, ((code
== PREINCREMENT_EXPR
4509 || code
== POSTINCREMENT_EXPR
)
4512 return error_mark_node
;
4514 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
4516 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4517 warning_at (location
, OPT_Wc___compat
,
4518 "increment of enumeration value is invalid in C++");
4520 warning_at (location
, OPT_Wc___compat
,
4521 "decrement of enumeration value is invalid in C++");
4524 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4526 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4527 warning_at (location
, OPT_Wbool_operation
,
4528 "increment of a boolean expression");
4530 warning_at (location
, OPT_Wbool_operation
,
4531 "decrement of a boolean expression");
4534 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4535 arg
= c_fully_fold (arg
, false, NULL
, true);
4538 atomic_op
= really_atomic_lvalue (arg
);
4540 /* Increment or decrement the real part of the value,
4541 and don't change the imaginary part. */
4542 if (typecode
== COMPLEX_TYPE
)
4546 pedwarn (location
, OPT_Wpedantic
,
4547 "ISO C does not support %<++%> and %<--%> on complex types");
4551 arg
= stabilize_reference (arg
);
4552 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
,
4554 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
,
4556 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, true);
4557 if (real
== error_mark_node
|| imag
== error_mark_node
)
4558 return error_mark_node
;
4559 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4561 goto return_build_unary_op
;
4565 /* Report invalid types. */
4567 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4568 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4569 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
4571 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4572 error_at (location
, "wrong type argument to increment");
4574 error_at (location
, "wrong type argument to decrement");
4576 return error_mark_node
;
4582 argtype
= TREE_TYPE (arg
);
4584 /* Compute the increment. */
4586 if (typecode
== POINTER_TYPE
)
4588 /* If pointer target is an incomplete type,
4589 we just cannot know how to do the arithmetic. */
4590 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4592 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4594 "increment of pointer to an incomplete type %qT",
4595 TREE_TYPE (argtype
));
4598 "decrement of pointer to an incomplete type %qT",
4599 TREE_TYPE (argtype
));
4601 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4602 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4604 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4605 pedwarn (location
, OPT_Wpointer_arith
,
4606 "wrong type argument to increment");
4608 pedwarn (location
, OPT_Wpointer_arith
,
4609 "wrong type argument to decrement");
4612 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4613 inc
= convert_to_ptrofftype_loc (location
, inc
);
4615 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4617 /* For signed fract types, we invert ++ to -- or
4618 -- to ++, and change inc from 1 to -1, because
4619 it is not possible to represent 1 in signed fract constants.
4620 For unsigned fract types, the result always overflows and
4621 we get an undefined (original) or the maximum value. */
4622 if (code
== PREINCREMENT_EXPR
)
4623 code
= PREDECREMENT_EXPR
;
4624 else if (code
== PREDECREMENT_EXPR
)
4625 code
= PREINCREMENT_EXPR
;
4626 else if (code
== POSTINCREMENT_EXPR
)
4627 code
= POSTDECREMENT_EXPR
;
4628 else /* code == POSTDECREMENT_EXPR */
4629 code
= POSTINCREMENT_EXPR
;
4631 inc
= integer_minus_one_node
;
4632 inc
= convert (argtype
, inc
);
4636 inc
= VECTOR_TYPE_P (argtype
)
4637 ? build_one_cst (argtype
)
4639 inc
= convert (argtype
, inc
);
4642 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4643 need to ask Objective-C to build the increment or decrement
4644 expression for it. */
4645 if (objc_is_property_ref (arg
))
4646 return objc_build_incr_expr_for_property_ref (location
, code
,
4649 /* Report a read-only lvalue. */
4650 if (TYPE_READONLY (argtype
))
4652 readonly_error (location
, arg
,
4653 ((code
== PREINCREMENT_EXPR
4654 || code
== POSTINCREMENT_EXPR
)
4655 ? lv_increment
: lv_decrement
));
4656 return error_mark_node
;
4658 else if (TREE_READONLY (arg
))
4659 readonly_warning (arg
,
4660 ((code
== PREINCREMENT_EXPR
4661 || code
== POSTINCREMENT_EXPR
)
4662 ? lv_increment
: lv_decrement
));
4664 /* If the argument is atomic, use the special code sequences for
4665 atomic compound assignment. */
4668 arg
= stabilize_reference (arg
);
4669 ret
= build_atomic_assign (location
, arg
,
4670 ((code
== PREINCREMENT_EXPR
4671 || code
== POSTINCREMENT_EXPR
)
4674 (FRACT_MODE_P (TYPE_MODE (argtype
))
4676 : integer_one_node
),
4677 (code
== POSTINCREMENT_EXPR
4678 || code
== POSTDECREMENT_EXPR
));
4679 goto return_build_unary_op
;
4682 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4683 val
= boolean_increment (code
, arg
);
4685 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4686 TREE_SIDE_EFFECTS (val
) = 1;
4687 if (TREE_CODE (val
) != code
)
4688 TREE_NO_WARNING (val
) = 1;
4690 goto return_build_unary_op
;
4694 /* Note that this operation never does default_conversion. */
4696 /* The operand of unary '&' must be an lvalue (which excludes
4697 expressions of type void), or, in C99, the result of a [] or
4698 unary '*' operator. */
4699 if (VOID_TYPE_P (TREE_TYPE (arg
))
4700 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4701 && (!INDIRECT_REF_P (arg
) || !flag_isoc99
))
4702 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4704 /* Let &* cancel out to simplify resulting code. */
4705 if (INDIRECT_REF_P (arg
))
4707 /* Don't let this be an lvalue. */
4708 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4709 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4710 ret
= TREE_OPERAND (arg
, 0);
4711 goto return_build_unary_op
;
4714 /* Anything not already handled and not a true memory reference
4715 or a non-lvalue array is an error. */
4716 if (typecode
!= FUNCTION_TYPE
&& !noconvert
4717 && !lvalue_or_else (location
, arg
, lv_addressof
))
4718 return error_mark_node
;
4720 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4722 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4724 tree inner
= build_unary_op (location
, code
,
4725 C_MAYBE_CONST_EXPR_EXPR (arg
),
4727 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4728 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4729 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4730 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4731 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4732 goto return_build_unary_op
;
4735 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4736 argtype
= TREE_TYPE (arg
);
4738 /* If the lvalue is const or volatile, merge that into the type
4739 to which the address will point. This is only needed
4740 for function types. */
4741 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4742 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4743 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4745 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4746 int quals
= orig_quals
;
4748 if (TREE_READONLY (arg
))
4749 quals
|= TYPE_QUAL_CONST
;
4750 if (TREE_THIS_VOLATILE (arg
))
4751 quals
|= TYPE_QUAL_VOLATILE
;
4753 argtype
= c_build_qualified_type (argtype
, quals
);
4756 switch (TREE_CODE (arg
))
4759 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
4761 error_at (location
, "cannot take address of bit-field %qD",
4762 TREE_OPERAND (arg
, 1));
4763 return error_mark_node
;
4769 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg
, 0))))
4771 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg
))
4772 && !VECTOR_TYPE_P (TREE_TYPE (arg
)))
4774 error_at (location
, "cannot take address of scalar with "
4775 "reverse storage order");
4776 return error_mark_node
;
4779 if (TREE_CODE (TREE_TYPE (arg
)) == ARRAY_TYPE
4780 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg
)))
4781 warning_at (location
, OPT_Wscalar_storage_order
,
4782 "address of array with reverse scalar storage "
4790 if (!c_mark_addressable (arg
))
4791 return error_mark_node
;
4793 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4794 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4796 argtype
= build_pointer_type (argtype
);
4798 /* ??? Cope with user tricks that amount to offsetof. Delete this
4799 when we have proper support for integer constant expressions. */
4800 val
= get_base_address (arg
);
4801 if (val
&& INDIRECT_REF_P (val
)
4802 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4804 ret
= fold_offsetof (arg
, argtype
);
4805 goto return_build_unary_op
;
4808 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4811 goto return_build_unary_op
;
4817 if (argtype
== NULL_TREE
)
4818 argtype
= TREE_TYPE (arg
);
4819 if (TREE_CODE (arg
) == INTEGER_CST
)
4820 ret
= (require_constant_value
4821 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4822 : fold_build1_loc (location
, code
, argtype
, arg
));
4824 ret
= build1 (code
, argtype
, arg
);
4825 return_build_unary_op
:
4826 gcc_assert (ret
!= error_mark_node
);
4827 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4828 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4829 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4830 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4831 ret
= note_integer_operands (ret
);
4833 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4834 protected_set_expr_location (ret
, location
);
4838 /* Return nonzero if REF is an lvalue valid for this language.
4839 Lvalues can be assigned, unless their type has TYPE_READONLY.
4840 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4843 lvalue_p (const_tree ref
)
4845 const enum tree_code code
= TREE_CODE (ref
);
4852 return lvalue_p (TREE_OPERAND (ref
, 0));
4854 case C_MAYBE_CONST_EXPR
:
4855 return lvalue_p (TREE_OPERAND (ref
, 1));
4857 case COMPOUND_LITERAL_EXPR
:
4867 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4868 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4871 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4878 /* Give a warning for storing in something that is read-only in GCC
4879 terms but not const in ISO C terms. */
4882 readonly_warning (tree arg
, enum lvalue_use use
)
4887 warning (0, "assignment of read-only location %qE", arg
);
4890 warning (0, "increment of read-only location %qE", arg
);
4893 warning (0, "decrement of read-only location %qE", arg
);
4902 /* Return nonzero if REF is an lvalue valid for this language;
4903 otherwise, print an error message and return zero. USE says
4904 how the lvalue is being used and so selects the error message.
4905 LOCATION is the location at which any error should be reported. */
4908 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4910 int win
= lvalue_p (ref
);
4913 lvalue_error (loc
, use
);
4918 /* Mark EXP saying that we need to be able to take the
4919 address of it; it should not be allocated in a register.
4920 Returns true if successful. ARRAY_REF_P is true if this
4921 is for ARRAY_REF construction - in that case we don't want
4922 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4923 it is fine to use ARRAY_REFs for vector subscripts on vector
4924 register variables. */
4927 c_mark_addressable (tree exp
, bool array_ref_p
)
4932 switch (TREE_CODE (x
))
4934 case VIEW_CONVERT_EXPR
:
4936 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
4937 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
4945 x
= TREE_OPERAND (x
, 0);
4948 case COMPOUND_LITERAL_EXPR
:
4949 TREE_ADDRESSABLE (x
) = 1;
4950 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x
)) = 1;
4954 TREE_ADDRESSABLE (x
) = 1;
4961 if (C_DECL_REGISTER (x
)
4962 && DECL_NONLOCAL (x
))
4964 if (TREE_PUBLIC (x
) || is_global_var (x
))
4967 ("global register variable %qD used in nested function", x
);
4970 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4972 else if (C_DECL_REGISTER (x
))
4974 if (TREE_PUBLIC (x
) || is_global_var (x
))
4975 error ("address of global register variable %qD requested", x
);
4977 error ("address of register variable %qD requested", x
);
4983 TREE_ADDRESSABLE (x
) = 1;
4990 /* Convert EXPR to TYPE, warning about conversion problems with
4991 constants. SEMANTIC_TYPE is the type this conversion would use
4992 without excess precision. If SEMANTIC_TYPE is NULL, this function
4993 is equivalent to convert_and_check. This function is a wrapper that
4994 handles conversions that may be different than
4995 the usual ones because of excess precision. */
4998 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
5001 if (TREE_TYPE (expr
) == type
)
5004 /* For C11, integer conversions may have results with excess
5006 if (flag_isoc11
|| !semantic_type
)
5007 return convert_and_check (loc
, type
, expr
);
5009 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
5010 && TREE_TYPE (expr
) != semantic_type
)
5012 /* For integers, we need to check the real conversion, not
5013 the conversion to the excess precision type. */
5014 expr
= convert_and_check (loc
, semantic_type
, expr
);
5016 /* Result type is the excess precision type, which should be
5017 large enough, so do not check. */
5018 return convert (type
, expr
);
5021 /* If EXPR refers to a built-in declared without a prototype returns
5022 the actual type of the built-in and, if non-null, set *BLTIN to
5023 a pointer to the built-in. Otherwise return the type of EXPR
5024 and clear *BLTIN if non-null. */
5027 type_or_builtin_type (tree expr
, tree
*bltin
= NULL
)
5035 tree type
= TREE_TYPE (expr
);
5036 if (TREE_CODE (expr
) != ADDR_EXPR
)
5039 tree oper
= TREE_OPERAND (expr
, 0);
5041 || TREE_CODE (oper
) != FUNCTION_DECL
5042 || !fndecl_built_in_p (oper
, BUILT_IN_NORMAL
))
5045 built_in_function code
= DECL_FUNCTION_CODE (oper
);
5046 if (!C_DECL_BUILTIN_PROTOTYPE (oper
))
5049 if ((*bltin
= builtin_decl_implicit (code
)))
5050 type
= build_pointer_type (TREE_TYPE (*bltin
));
5055 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
5056 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5057 if folded to an integer constant then the unselected half may
5058 contain arbitrary operations not normally permitted in constant
5059 expressions. Set the location of the expression to LOC. */
5062 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
5063 tree op1
, tree op1_original_type
, location_t op1_loc
,
5064 tree op2
, tree op2_original_type
, location_t op2_loc
)
5068 enum tree_code code1
;
5069 enum tree_code code2
;
5070 tree result_type
= NULL
;
5071 tree semantic_result_type
= NULL
;
5072 tree orig_op1
= op1
, orig_op2
= op2
;
5073 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
5074 bool ifexp_int_operands
;
5077 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
5078 if (op1_int_operands
)
5079 op1
= remove_c_maybe_const_expr (op1
);
5080 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
5081 if (op2_int_operands
)
5082 op2
= remove_c_maybe_const_expr (op2
);
5083 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
5084 if (ifexp_int_operands
)
5085 ifexp
= remove_c_maybe_const_expr (ifexp
);
5087 /* Promote both alternatives. */
5089 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
5090 op1
= default_conversion (op1
);
5091 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
5092 op2
= default_conversion (op2
);
5094 if (TREE_CODE (ifexp
) == ERROR_MARK
5095 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
5096 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
5097 return error_mark_node
;
5099 tree bltin1
= NULL_TREE
;
5100 tree bltin2
= NULL_TREE
;
5101 type1
= type_or_builtin_type (op1
, &bltin1
);
5102 code1
= TREE_CODE (type1
);
5103 type2
= type_or_builtin_type (op2
, &bltin2
);
5104 code2
= TREE_CODE (type2
);
5106 if (code1
== POINTER_TYPE
&& reject_gcc_builtin (op1
))
5107 return error_mark_node
;
5109 if (code2
== POINTER_TYPE
&& reject_gcc_builtin (op2
))
5110 return error_mark_node
;
5112 /* C90 does not permit non-lvalue arrays in conditional expressions.
5113 In C99 they will be pointers by now. */
5114 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
5116 error_at (colon_loc
, "non-lvalue array in conditional expression");
5117 return error_mark_node
;
5120 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
5121 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
5122 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5123 || code1
== COMPLEX_TYPE
)
5124 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
5125 || code2
== COMPLEX_TYPE
))
5127 semantic_result_type
= c_common_type (type1
, type2
);
5128 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
5130 op1
= TREE_OPERAND (op1
, 0);
5131 type1
= TREE_TYPE (op1
);
5132 gcc_assert (TREE_CODE (type1
) == code1
);
5134 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
5136 op2
= TREE_OPERAND (op2
, 0);
5137 type2
= TREE_TYPE (op2
);
5138 gcc_assert (TREE_CODE (type2
) == code2
);
5142 if (warn_cxx_compat
)
5144 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
5145 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
5147 if (TREE_CODE (t1
) == ENUMERAL_TYPE
5148 && TREE_CODE (t2
) == ENUMERAL_TYPE
5149 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
5150 warning_at (colon_loc
, OPT_Wc___compat
,
5151 ("different enum types in conditional is "
5152 "invalid in C++: %qT vs %qT"),
5156 /* Quickly detect the usual case where op1 and op2 have the same type
5158 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
5161 result_type
= type1
;
5163 result_type
= TYPE_MAIN_VARIANT (type1
);
5165 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5166 || code1
== COMPLEX_TYPE
)
5167 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
5168 || code2
== COMPLEX_TYPE
))
5170 /* In C11, a conditional expression between a floating-point
5171 type and an integer type should convert the integer type to
5172 the evaluation format of the floating-point type, with
5173 possible excess precision. */
5174 tree eptype1
= type1
;
5175 tree eptype2
= type2
;
5179 if (ANY_INTEGRAL_TYPE_P (type1
)
5180 && (eptype
= excess_precision_type (type2
)) != NULL_TREE
)
5183 if (!semantic_result_type
)
5184 semantic_result_type
= c_common_type (type1
, type2
);
5186 else if (ANY_INTEGRAL_TYPE_P (type2
)
5187 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
5190 if (!semantic_result_type
)
5191 semantic_result_type
= c_common_type (type1
, type2
);
5194 result_type
= c_common_type (eptype1
, eptype2
);
5195 if (result_type
== error_mark_node
)
5196 return error_mark_node
;
5197 do_warn_double_promotion (result_type
, type1
, type2
,
5198 "implicit conversion from %qT to %qT to "
5199 "match other result of conditional",
5202 /* If -Wsign-compare, warn here if type1 and type2 have
5203 different signedness. We'll promote the signed to unsigned
5204 and later code won't know it used to be different.
5205 Do this check on the original types, so that explicit casts
5206 will be considered, but default promotions won't. */
5207 if (c_inhibit_evaluation_warnings
== 0)
5209 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
5210 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
5212 if (unsigned_op1
^ unsigned_op2
)
5216 /* Do not warn if the result type is signed, since the
5217 signed type will only be chosen if it can represent
5218 all the values of the unsigned type. */
5219 if (!TYPE_UNSIGNED (result_type
))
5223 bool op1_maybe_const
= true;
5224 bool op2_maybe_const
= true;
5226 /* Do not warn if the signed quantity is an
5227 unsuffixed integer literal (or some static
5228 constant expression involving such literals) and
5229 it is non-negative. This warning requires the
5230 operands to be folded for best results, so do
5231 that folding in this case even without
5232 warn_sign_compare to avoid warning options
5233 possibly affecting code generation. */
5234 c_inhibit_evaluation_warnings
5235 += (ifexp
== truthvalue_false_node
);
5236 op1
= c_fully_fold (op1
, require_constant_value
,
5238 c_inhibit_evaluation_warnings
5239 -= (ifexp
== truthvalue_false_node
);
5241 c_inhibit_evaluation_warnings
5242 += (ifexp
== truthvalue_true_node
);
5243 op2
= c_fully_fold (op2
, require_constant_value
,
5245 c_inhibit_evaluation_warnings
5246 -= (ifexp
== truthvalue_true_node
);
5248 if (warn_sign_compare
)
5251 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
5253 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
5255 else if (unsigned_op2
)
5256 warning_at (op1_loc
, OPT_Wsign_compare
,
5257 "operand of %<?:%> changes signedness from "
5258 "%qT to %qT due to unsignedness of other "
5259 "operand", TREE_TYPE (orig_op1
),
5260 TREE_TYPE (orig_op2
));
5262 warning_at (op2_loc
, OPT_Wsign_compare
,
5263 "operand of %<?:%> changes signedness from "
5264 "%qT to %qT due to unsignedness of other "
5265 "operand", TREE_TYPE (orig_op2
),
5266 TREE_TYPE (orig_op1
));
5268 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
5269 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
5270 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
5271 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
5276 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
5278 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
5279 pedwarn (colon_loc
, OPT_Wpedantic
,
5280 "ISO C forbids conditional expr with only one void side");
5281 result_type
= void_type_node
;
5283 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5285 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
5286 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
5287 addr_space_t as_common
;
5289 if (comp_target_types (colon_loc
, type1
, type2
))
5290 result_type
= common_pointer_type (type1
, type2
);
5291 else if (null_pointer_constant_p (orig_op1
))
5292 result_type
= type2
;
5293 else if (null_pointer_constant_p (orig_op2
))
5294 result_type
= type1
;
5295 else if (!addr_space_superset (as1
, as2
, &as_common
))
5297 error_at (colon_loc
, "pointers to disjoint address spaces "
5298 "used in conditional expression");
5299 return error_mark_node
;
5301 else if (VOID_TYPE_P (TREE_TYPE (type1
))
5302 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
5304 if ((TREE_CODE (TREE_TYPE (type2
)) == ARRAY_TYPE
)
5305 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2
)))
5306 & ~TYPE_QUALS (TREE_TYPE (type1
))))
5307 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5308 "pointer to array loses qualifier "
5309 "in conditional expression");
5311 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
5312 pedwarn (colon_loc
, OPT_Wpedantic
,
5313 "ISO C forbids conditional expr between "
5314 "%<void *%> and function pointer");
5315 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
5316 TREE_TYPE (type2
)));
5318 else if (VOID_TYPE_P (TREE_TYPE (type2
))
5319 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
5321 if ((TREE_CODE (TREE_TYPE (type1
)) == ARRAY_TYPE
)
5322 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1
)))
5323 & ~TYPE_QUALS (TREE_TYPE (type2
))))
5324 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5325 "pointer to array loses qualifier "
5326 "in conditional expression");
5328 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
5329 pedwarn (colon_loc
, OPT_Wpedantic
,
5330 "ISO C forbids conditional expr between "
5331 "%<void *%> and function pointer");
5332 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
5333 TREE_TYPE (type1
)));
5335 /* Objective-C pointer comparisons are a bit more lenient. */
5336 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
5337 result_type
= objc_common_type (type1
, type2
);
5340 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
5341 if (bltin1
&& bltin2
)
5342 warning_at (colon_loc
, OPT_Wincompatible_pointer_types
,
5343 "pointer type mismatch between %qT and %qT "
5344 "of %qD and %qD in conditional expression",
5345 type1
, type2
, bltin1
, bltin2
);
5347 pedwarn (colon_loc
, 0,
5348 "pointer type mismatch in conditional expression");
5349 result_type
= build_pointer_type
5350 (build_qualified_type (void_type_node
, qual
));
5353 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5355 if (!null_pointer_constant_p (orig_op2
))
5356 pedwarn (colon_loc
, 0,
5357 "pointer/integer type mismatch in conditional expression");
5360 op2
= null_pointer_node
;
5362 result_type
= type1
;
5364 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5366 if (!null_pointer_constant_p (orig_op1
))
5367 pedwarn (colon_loc
, 0,
5368 "pointer/integer type mismatch in conditional expression");
5371 op1
= null_pointer_node
;
5373 result_type
= type2
;
5378 if (flag_cond_mismatch
)
5379 result_type
= void_type_node
;
5382 error_at (colon_loc
, "type mismatch in conditional expression");
5383 return error_mark_node
;
5387 /* Merge const and volatile flags of the incoming types. */
5389 = build_type_variant (result_type
,
5390 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
5391 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
5393 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
5394 semantic_result_type
);
5395 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
5396 semantic_result_type
);
5398 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
5400 op2_int_operands
= true;
5401 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
5403 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
5405 op1_int_operands
= true;
5406 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
5408 int_const
= int_operands
= (ifexp_int_operands
5410 && op2_int_operands
);
5413 int_const
= ((ifexp
== truthvalue_true_node
5414 && TREE_CODE (orig_op1
) == INTEGER_CST
5415 && !TREE_OVERFLOW (orig_op1
))
5416 || (ifexp
== truthvalue_false_node
5417 && TREE_CODE (orig_op2
) == INTEGER_CST
5418 && !TREE_OVERFLOW (orig_op2
)));
5421 /* Need to convert condition operand into a vector mask. */
5422 if (VECTOR_TYPE_P (TREE_TYPE (ifexp
)))
5424 tree vectype
= TREE_TYPE (ifexp
);
5425 tree elem_type
= TREE_TYPE (vectype
);
5426 tree zero
= build_int_cst (elem_type
, 0);
5427 tree zero_vec
= build_vector_from_val (vectype
, zero
);
5428 tree cmp_type
= build_same_sized_truth_vector_type (vectype
);
5429 ifexp
= build2 (NE_EXPR
, cmp_type
, ifexp
, zero_vec
);
5432 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
5433 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5438 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5439 nested inside of the expression. */
5440 op1
= c_fully_fold (op1
, false, NULL
);
5441 op2
= c_fully_fold (op2
, false, NULL
);
5443 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5445 ret
= note_integer_operands (ret
);
5447 if (semantic_result_type
)
5448 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
5450 protected_set_expr_location (ret
, colon_loc
);
5452 /* If the OP1 and OP2 are the same and don't have side-effects,
5453 warn here, because the COND_EXPR will be turned into OP1. */
5454 if (warn_duplicated_branches
5455 && TREE_CODE (ret
) == COND_EXPR
5456 && (op1
== op2
|| operand_equal_p (op1
, op2
, 0)))
5457 warning_at (EXPR_LOCATION (ret
), OPT_Wduplicated_branches
,
5458 "this condition has identical branches");
5463 /* Return a compound expression that performs two expressions and
5464 returns the value of the second of them.
5466 LOC is the location of the COMPOUND_EXPR. */
5469 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
5471 bool expr1_int_operands
, expr2_int_operands
;
5472 tree eptype
= NULL_TREE
;
5475 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
5476 if (expr1_int_operands
)
5477 expr1
= remove_c_maybe_const_expr (expr1
);
5478 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
5479 if (expr2_int_operands
)
5480 expr2
= remove_c_maybe_const_expr (expr2
);
5482 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
5483 expr1
= TREE_OPERAND (expr1
, 0);
5484 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
5486 eptype
= TREE_TYPE (expr2
);
5487 expr2
= TREE_OPERAND (expr2
, 0);
5490 if (!TREE_SIDE_EFFECTS (expr1
))
5492 /* The left-hand operand of a comma expression is like an expression
5493 statement: with -Wunused, we should warn if it doesn't have
5494 any side-effects, unless it was explicitly cast to (void). */
5495 if (warn_unused_value
)
5497 if (VOID_TYPE_P (TREE_TYPE (expr1
))
5498 && CONVERT_EXPR_P (expr1
))
5500 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
5501 && TREE_CODE (expr1
) == COMPOUND_EXPR
5502 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
5503 ; /* (void) a, (void) b, c */
5505 warning_at (loc
, OPT_Wunused_value
,
5506 "left-hand operand of comma expression has no effect");
5509 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
5510 && warn_unused_value
)
5513 location_t cloc
= loc
;
5514 while (TREE_CODE (r
) == COMPOUND_EXPR
)
5516 if (EXPR_HAS_LOCATION (r
))
5517 cloc
= EXPR_LOCATION (r
);
5518 r
= TREE_OPERAND (r
, 1);
5520 if (!TREE_SIDE_EFFECTS (r
)
5521 && !VOID_TYPE_P (TREE_TYPE (r
))
5522 && !CONVERT_EXPR_P (r
))
5523 warning_at (cloc
, OPT_Wunused_value
,
5524 "right-hand operand of comma expression has no effect");
5527 /* With -Wunused, we should also warn if the left-hand operand does have
5528 side-effects, but computes a value which is not used. For example, in
5529 `foo() + bar(), baz()' the result of the `+' operator is not used,
5530 so we should issue a warning. */
5531 else if (warn_unused_value
)
5532 warn_if_unused_value (expr1
, loc
);
5534 if (expr2
== error_mark_node
)
5535 return error_mark_node
;
5537 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
5540 && expr1_int_operands
5541 && expr2_int_operands
)
5542 ret
= note_integer_operands (ret
);
5545 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
5547 protected_set_expr_location (ret
, loc
);
5551 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5552 which we are casting. OTYPE is the type of the expression being
5553 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5554 of the cast. -Wcast-qual appeared on the command line. Named
5555 address space qualifiers are not handled here, because they result
5556 in different warnings. */
5559 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
5561 tree in_type
= type
;
5562 tree in_otype
= otype
;
5567 /* Check that the qualifiers on IN_TYPE are a superset of the
5568 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5569 nodes is uninteresting and we stop as soon as we hit a
5570 non-POINTER_TYPE node on either type. */
5573 in_otype
= TREE_TYPE (in_otype
);
5574 in_type
= TREE_TYPE (in_type
);
5576 /* GNU C allows cv-qualified function types. 'const' means the
5577 function is very pure, 'volatile' means it can't return. We
5578 need to warn when such qualifiers are added, not when they're
5580 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
5581 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
5582 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
5583 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
5585 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
5586 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
5588 while (TREE_CODE (in_type
) == POINTER_TYPE
5589 && TREE_CODE (in_otype
) == POINTER_TYPE
);
5592 warning_at (loc
, OPT_Wcast_qual
,
5593 "cast adds %q#v qualifier to function type", added
);
5596 /* There are qualifiers present in IN_OTYPE that are not present
5598 warning_at (loc
, OPT_Wcast_qual
,
5599 "cast discards %qv qualifier from pointer target type",
5602 if (added
|| discarded
)
5605 /* A cast from **T to const **T is unsafe, because it can cause a
5606 const value to be changed with no additional warning. We only
5607 issue this warning if T is the same on both sides, and we only
5608 issue the warning if there are the same number of pointers on
5609 both sides, as otherwise the cast is clearly unsafe anyhow. A
5610 cast is unsafe when a qualifier is added at one level and const
5611 is not present at all outer levels.
5613 To issue this warning, we check at each level whether the cast
5614 adds new qualifiers not already seen. We don't need to special
5615 case function types, as they won't have the same
5616 TYPE_MAIN_VARIANT. */
5618 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
5620 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
5625 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
5628 in_type
= TREE_TYPE (in_type
);
5629 in_otype
= TREE_TYPE (in_otype
);
5630 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
5633 warning_at (loc
, OPT_Wcast_qual
,
5634 "to be safe all intermediate pointers in cast from "
5635 "%qT to %qT must be %<const%> qualified",
5640 is_const
= TYPE_READONLY (in_type
);
5642 while (TREE_CODE (in_type
) == POINTER_TYPE
);
5645 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5648 c_safe_arg_type_equiv_p (tree t1
, tree t2
)
5650 t1
= TYPE_MAIN_VARIANT (t1
);
5651 t2
= TYPE_MAIN_VARIANT (t2
);
5653 if (TREE_CODE (t1
) == POINTER_TYPE
5654 && TREE_CODE (t2
) == POINTER_TYPE
)
5657 /* The signedness of the parameter matters only when an integral
5658 type smaller than int is promoted to int, otherwise only the
5659 precision of the parameter matters.
5660 This check should make sure that the callee does not see
5661 undefined values in argument registers. */
5662 if (INTEGRAL_TYPE_P (t1
)
5663 && INTEGRAL_TYPE_P (t2
)
5664 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
5665 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
5666 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
5667 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
5670 return comptypes (t1
, t2
);
5673 /* Check if a type cast between two function types can be considered safe. */
5676 c_safe_function_type_cast_p (tree t1
, tree t2
)
5678 if (TREE_TYPE (t1
) == void_type_node
&&
5679 TYPE_ARG_TYPES (t1
) == void_list_node
)
5682 if (TREE_TYPE (t2
) == void_type_node
&&
5683 TYPE_ARG_TYPES (t2
) == void_list_node
)
5686 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
5689 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
5691 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
5692 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
5698 /* Build an expression representing a cast to type TYPE of expression EXPR.
5699 LOC is the location of the cast-- typically the open paren of the cast. */
5702 build_c_cast (location_t loc
, tree type
, tree expr
)
5706 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
5707 expr
= TREE_OPERAND (expr
, 0);
5711 if (type
== error_mark_node
|| expr
== error_mark_node
)
5712 return error_mark_node
;
5714 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5715 only in <protocol> qualifications. But when constructing cast expressions,
5716 the protocols do matter and must be kept around. */
5717 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
5718 return build1 (NOP_EXPR
, type
, expr
);
5720 type
= TYPE_MAIN_VARIANT (type
);
5722 if (TREE_CODE (type
) == ARRAY_TYPE
)
5724 error_at (loc
, "cast specifies array type");
5725 return error_mark_node
;
5728 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5730 error_at (loc
, "cast specifies function type");
5731 return error_mark_node
;
5734 if (!VOID_TYPE_P (type
))
5736 value
= require_complete_type (loc
, value
);
5737 if (value
== error_mark_node
)
5738 return error_mark_node
;
5741 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
5743 if (RECORD_OR_UNION_TYPE_P (type
))
5744 pedwarn (loc
, OPT_Wpedantic
,
5745 "ISO C forbids casting nonscalar to the same type");
5747 /* Convert to remove any qualifiers from VALUE's type. */
5748 value
= convert (type
, value
);
5750 else if (TREE_CODE (type
) == UNION_TYPE
)
5754 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5755 if (TREE_TYPE (field
) != error_mark_node
5756 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
5757 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
5763 bool maybe_const
= true;
5765 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
5766 t
= c_fully_fold (value
, false, &maybe_const
);
5767 t
= build_constructor_single (type
, field
, t
);
5769 t
= c_wrap_maybe_const (t
, true);
5770 t
= digest_init (loc
, type
, t
,
5771 NULL_TREE
, false, true, 0);
5772 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
5775 error_at (loc
, "cast to union type from type not present in union");
5776 return error_mark_node
;
5782 if (type
== void_type_node
)
5784 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5785 SET_EXPR_LOCATION (t
, loc
);
5789 otype
= TREE_TYPE (value
);
5791 /* Optionally warn about potentially worrisome casts. */
5793 && TREE_CODE (type
) == POINTER_TYPE
5794 && TREE_CODE (otype
) == POINTER_TYPE
)
5795 handle_warn_cast_qual (loc
, type
, otype
);
5797 /* Warn about conversions between pointers to disjoint
5799 if (TREE_CODE (type
) == POINTER_TYPE
5800 && TREE_CODE (otype
) == POINTER_TYPE
5801 && !null_pointer_constant_p (value
))
5803 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5804 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5805 addr_space_t as_common
;
5807 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5809 if (ADDR_SPACE_GENERIC_P (as_from
))
5810 warning_at (loc
, 0, "cast to %s address space pointer "
5811 "from disjoint generic address space pointer",
5812 c_addr_space_name (as_to
));
5814 else if (ADDR_SPACE_GENERIC_P (as_to
))
5815 warning_at (loc
, 0, "cast to generic address space pointer "
5816 "from disjoint %s address space pointer",
5817 c_addr_space_name (as_from
));
5820 warning_at (loc
, 0, "cast to %s address space pointer "
5821 "from disjoint %s address space pointer",
5822 c_addr_space_name (as_to
),
5823 c_addr_space_name (as_from
));
5827 /* Warn about possible alignment problems. */
5828 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
5829 && TREE_CODE (type
) == POINTER_TYPE
5830 && TREE_CODE (otype
) == POINTER_TYPE
5831 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5832 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5833 /* Don't warn about opaque types, where the actual alignment
5834 restriction is unknown. */
5835 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype
))
5836 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5837 && min_align_of_type (TREE_TYPE (type
))
5838 > min_align_of_type (TREE_TYPE (otype
)))
5839 warning_at (loc
, OPT_Wcast_align
,
5840 "cast increases required alignment of target type");
5842 if (TREE_CODE (type
) == INTEGER_TYPE
5843 && TREE_CODE (otype
) == POINTER_TYPE
5844 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5845 /* Unlike conversion of integers to pointers, where the
5846 warning is disabled for converting constants because
5847 of cases such as SIG_*, warn about converting constant
5848 pointers to integers. In some cases it may cause unwanted
5849 sign extension, and a warning is appropriate. */
5850 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5851 "cast from pointer to integer of different size");
5853 if (TREE_CODE (value
) == CALL_EXPR
5854 && TREE_CODE (type
) != TREE_CODE (otype
))
5855 warning_at (loc
, OPT_Wbad_function_cast
,
5856 "cast from function call of type %qT "
5857 "to non-matching type %qT", otype
, type
);
5859 if (TREE_CODE (type
) == POINTER_TYPE
5860 && TREE_CODE (otype
) == INTEGER_TYPE
5861 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5862 /* Don't warn about converting any constant. */
5863 && !TREE_CONSTANT (value
))
5865 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5866 "of different size");
5868 if (warn_strict_aliasing
<= 2)
5869 strict_aliasing_warning (EXPR_LOCATION (value
), type
, expr
);
5871 /* If pedantic, warn for conversions between function and object
5872 pointer types, except for converting a null pointer constant
5873 to function pointer type. */
5875 && TREE_CODE (type
) == POINTER_TYPE
5876 && TREE_CODE (otype
) == POINTER_TYPE
5877 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5878 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5879 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5880 "conversion of function pointer to object pointer type");
5883 && TREE_CODE (type
) == POINTER_TYPE
5884 && TREE_CODE (otype
) == POINTER_TYPE
5885 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5886 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5887 && !null_pointer_constant_p (value
))
5888 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5889 "conversion of object pointer to function pointer type");
5891 if (TREE_CODE (type
) == POINTER_TYPE
5892 && TREE_CODE (otype
) == POINTER_TYPE
5893 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5894 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5895 && !c_safe_function_type_cast_p (TREE_TYPE (type
),
5897 warning_at (loc
, OPT_Wcast_function_type
,
5898 "cast between incompatible function types"
5899 " from %qT to %qT", otype
, type
);
5902 value
= convert (type
, value
);
5904 /* Ignore any integer overflow caused by the cast. */
5905 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5907 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5909 if (!TREE_OVERFLOW (value
))
5911 /* Avoid clobbering a shared constant. */
5912 value
= copy_node (value
);
5913 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5916 else if (TREE_OVERFLOW (value
))
5917 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5918 value
= wide_int_to_tree (TREE_TYPE (value
), wi::to_wide (value
));
5922 /* Don't let a cast be an lvalue. */
5923 if (lvalue_p (value
))
5924 value
= non_lvalue_loc (loc
, value
);
5926 /* Don't allow the results of casting to floating-point or complex
5927 types be confused with actual constants, or casts involving
5928 integer and pointer types other than direct integer-to-integer
5929 and integer-to-pointer be confused with integer constant
5930 expressions and null pointer constants. */
5931 if (TREE_CODE (value
) == REAL_CST
5932 || TREE_CODE (value
) == COMPLEX_CST
5933 || (TREE_CODE (value
) == INTEGER_CST
5934 && !((TREE_CODE (expr
) == INTEGER_CST
5935 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5936 || TREE_CODE (expr
) == REAL_CST
5937 || TREE_CODE (expr
) == COMPLEX_CST
)))
5938 value
= build1 (NOP_EXPR
, type
, value
);
5940 protected_set_expr_location (value
, loc
);
5944 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5945 location of the open paren of the cast, or the position of the cast
5948 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5951 tree type_expr
= NULL_TREE
;
5952 bool type_expr_const
= true;
5954 int saved_wsp
= warn_strict_prototypes
;
5956 /* This avoids warnings about unprototyped casts on
5957 integers. E.g. "#define SIG_DFL (void(*)())0". */
5958 if (TREE_CODE (expr
) == INTEGER_CST
)
5959 warn_strict_prototypes
= 0;
5960 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5961 warn_strict_prototypes
= saved_wsp
;
5963 if (TREE_CODE (expr
) == ADDR_EXPR
&& !VOID_TYPE_P (type
)
5964 && reject_gcc_builtin (expr
))
5965 return error_mark_node
;
5967 ret
= build_c_cast (loc
, type
, expr
);
5970 bool inner_expr_const
= true;
5971 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5972 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5973 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5974 && inner_expr_const
);
5975 SET_EXPR_LOCATION (ret
, loc
);
5978 if (!EXPR_HAS_LOCATION (ret
))
5979 protected_set_expr_location (ret
, loc
);
5981 /* C++ does not permits types to be defined in a cast, but it
5982 allows references to incomplete types. */
5983 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5984 warning_at (loc
, OPT_Wc___compat
,
5985 "defining a type in a cast is invalid in C++");
5990 /* Build an assignment expression of lvalue LHS from value RHS.
5991 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5992 may differ from TREE_TYPE (LHS) for an enum bitfield.
5993 MODIFYCODE is the code for a binary operator that we use
5994 to combine the old value of LHS with RHS to get the new value.
5995 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5996 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5997 which may differ from TREE_TYPE (RHS) for an enum value.
5999 LOCATION is the location of the MODIFYCODE operator.
6000 RHS_LOC is the location of the RHS. */
6003 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
6004 enum tree_code modifycode
,
6005 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
6009 tree rhseval
= NULL_TREE
;
6010 tree lhstype
= TREE_TYPE (lhs
);
6011 tree olhstype
= lhstype
;
6015 /* Types that aren't fully specified cannot be used in assignments. */
6016 lhs
= require_complete_type (location
, lhs
);
6018 /* Avoid duplicate error messages from operands that had errors. */
6019 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
6020 return error_mark_node
;
6022 /* Ensure an error for assigning a non-lvalue array to an array in
6024 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
6026 error_at (location
, "assignment to expression with array type");
6027 return error_mark_node
;
6030 /* For ObjC properties, defer this check. */
6031 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
6032 return error_mark_node
;
6034 is_atomic_op
= really_atomic_lvalue (lhs
);
6038 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
6040 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
6041 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
6043 if (inner
== error_mark_node
)
6044 return error_mark_node
;
6045 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
6046 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
6047 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
6048 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
6049 protected_set_expr_location (result
, location
);
6053 /* If a binary op has been requested, combine the old LHS value with the RHS
6054 producing the value we should actually store into the LHS. */
6056 if (modifycode
!= NOP_EXPR
)
6058 lhs
= c_fully_fold (lhs
, false, NULL
, true);
6059 lhs
= stabilize_reference (lhs
);
6061 /* Construct the RHS for any non-atomic compound assignemnt. */
6064 /* If in LHS op= RHS the RHS has side-effects, ensure they
6065 are preevaluated before the rest of the assignment expression's
6066 side-effects, because RHS could contain e.g. function calls
6068 if (TREE_SIDE_EFFECTS (rhs
))
6070 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6071 newrhs
= save_expr (TREE_OPERAND (rhs
, 0));
6073 newrhs
= save_expr (rhs
);
6075 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6076 newrhs
= build1 (EXCESS_PRECISION_EXPR
, TREE_TYPE (rhs
),
6079 newrhs
= build_binary_op (location
,
6080 modifycode
, lhs
, newrhs
, true);
6082 /* The original type of the right hand side is no longer
6084 rhs_origtype
= NULL_TREE
;
6088 if (c_dialect_objc ())
6090 /* Check if we are modifying an Objective-C property reference;
6091 if so, we need to generate setter calls. */
6092 if (TREE_CODE (newrhs
) == EXCESS_PRECISION_EXPR
)
6093 result
= objc_maybe_build_modify_expr (lhs
, TREE_OPERAND (newrhs
, 0));
6095 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
6099 /* Else, do the check that we postponed for Objective-C. */
6100 if (!lvalue_or_else (location
, lhs
, lv_assign
))
6101 return error_mark_node
;
6104 /* Give an error for storing in something that is 'const'. */
6106 if (TYPE_READONLY (lhstype
)
6107 || (RECORD_OR_UNION_TYPE_P (lhstype
)
6108 && C_TYPE_FIELDS_READONLY (lhstype
)))
6110 readonly_error (location
, lhs
, lv_assign
);
6111 return error_mark_node
;
6113 else if (TREE_READONLY (lhs
))
6114 readonly_warning (lhs
, lv_assign
);
6116 /* If storing into a structure or union member,
6117 it has probably been given type `int'.
6118 Compute the type that would go with
6119 the actual amount of storage the member occupies. */
6121 if (TREE_CODE (lhs
) == COMPONENT_REF
6122 && (TREE_CODE (lhstype
) == INTEGER_TYPE
6123 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
6124 || TREE_CODE (lhstype
) == REAL_TYPE
6125 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
6126 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
6128 /* If storing in a field that is in actuality a short or narrower than one,
6129 we must store in the field in its actual type. */
6131 if (lhstype
!= TREE_TYPE (lhs
))
6133 lhs
= copy_node (lhs
);
6134 TREE_TYPE (lhs
) = lhstype
;
6137 /* Issue -Wc++-compat warnings about an assignment to an enum type
6138 when LHS does not have its original type. This happens for,
6139 e.g., an enum bitfield in a struct. */
6141 && lhs_origtype
!= NULL_TREE
6142 && lhs_origtype
!= lhstype
6143 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
6145 tree checktype
= (rhs_origtype
!= NULL_TREE
6148 if (checktype
!= error_mark_node
6149 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
6150 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
6151 warning_at (location
, OPT_Wc___compat
,
6152 "enum conversion in assignment is invalid in C++");
6155 /* If the lhs is atomic, remove that qualifier. */
6158 lhstype
= build_qualified_type (lhstype
,
6159 (TYPE_QUALS (lhstype
)
6160 & ~TYPE_QUAL_ATOMIC
));
6161 olhstype
= build_qualified_type (olhstype
,
6162 (TYPE_QUALS (lhstype
)
6163 & ~TYPE_QUAL_ATOMIC
));
6166 /* Convert new value to destination type. Fold it first, then
6167 restore any excess precision information, for the sake of
6168 conversion warnings. */
6170 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
6172 tree rhs_semantic_type
= NULL_TREE
;
6173 if (TREE_CODE (newrhs
) == EXCESS_PRECISION_EXPR
)
6175 rhs_semantic_type
= TREE_TYPE (newrhs
);
6176 newrhs
= TREE_OPERAND (newrhs
, 0);
6178 npc
= null_pointer_constant_p (newrhs
);
6179 newrhs
= c_fully_fold (newrhs
, false, NULL
);
6180 if (rhs_semantic_type
)
6181 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
6182 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
6183 rhs_origtype
, ic_assign
, npc
,
6184 NULL_TREE
, NULL_TREE
, 0);
6185 if (TREE_CODE (newrhs
) == ERROR_MARK
)
6186 return error_mark_node
;
6189 /* Emit ObjC write barrier, if necessary. */
6190 if (c_dialect_objc () && flag_objc_gc
)
6192 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
6195 protected_set_expr_location (result
, location
);
6200 /* Scan operands. */
6203 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
6206 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
6207 TREE_SIDE_EFFECTS (result
) = 1;
6208 protected_set_expr_location (result
, location
);
6211 /* If we got the LHS in a different type for storing in,
6212 convert the result back to the nominal type of LHS
6213 so that the value we return always has the same type
6214 as the LHS argument. */
6216 if (olhstype
== TREE_TYPE (result
))
6219 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
6220 rhs_origtype
, ic_assign
, false, NULL_TREE
,
6222 protected_set_expr_location (result
, location
);
6226 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
6230 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6231 This is used to implement -fplan9-extensions. */
6234 find_anonymous_field_with_type (tree struct_type
, tree type
)
6239 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type
));
6241 for (field
= TYPE_FIELDS (struct_type
);
6243 field
= TREE_CHAIN (field
))
6245 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
6246 ? c_build_qualified_type (TREE_TYPE (field
),
6248 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
6249 if (DECL_NAME (field
) == NULL
6250 && comptypes (type
, fieldtype
))
6256 else if (DECL_NAME (field
) == NULL
6257 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
6258 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
6268 /* RHS is an expression whose type is pointer to struct. If there is
6269 an anonymous field in RHS with type TYPE, then return a pointer to
6270 that field in RHS. This is used with -fplan9-extensions. This
6271 returns NULL if no conversion could be found. */
6274 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
6276 tree rhs_struct_type
, lhs_main_type
;
6277 tree field
, found_field
;
6278 bool found_sub_field
;
6281 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
6282 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
6283 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type
));
6285 gcc_assert (POINTER_TYPE_P (type
));
6286 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
6287 ? c_build_qualified_type (TREE_TYPE (type
),
6289 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6291 found_field
= NULL_TREE
;
6292 found_sub_field
= false;
6293 for (field
= TYPE_FIELDS (rhs_struct_type
);
6295 field
= TREE_CHAIN (field
))
6297 if (DECL_NAME (field
) != NULL_TREE
6298 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
6300 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
6301 ? c_build_qualified_type (TREE_TYPE (field
),
6303 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
6304 if (comptypes (lhs_main_type
, fieldtype
))
6306 if (found_field
!= NULL_TREE
)
6308 found_field
= field
;
6310 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
6313 if (found_field
!= NULL_TREE
)
6315 found_field
= field
;
6316 found_sub_field
= true;
6320 if (found_field
== NULL_TREE
)
6323 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
6324 build_fold_indirect_ref (rhs
), found_field
,
6326 ret
= build_fold_addr_expr_loc (location
, ret
);
6328 if (found_sub_field
)
6330 ret
= convert_to_anonymous_field (location
, type
, ret
);
6331 gcc_assert (ret
!= NULL_TREE
);
6337 /* Issue an error message for a bad initializer component.
6338 GMSGID identifies the message.
6339 The component name is taken from the spelling stack. */
6341 static void ATTRIBUTE_GCC_DIAG (2,0)
6342 error_init (location_t loc
, const char *gmsgid
, ...)
6346 auto_diagnostic_group d
;
6348 /* The gmsgid may be a format string with %< and %>. */
6350 va_start (ap
, gmsgid
);
6351 bool warned
= emit_diagnostic_valist (DK_ERROR
, loc
, -1, gmsgid
, &ap
);
6354 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6355 if (*ofwhat
&& warned
)
6356 inform (loc
, "(near initialization for %qs)", ofwhat
);
6359 /* Issue a pedantic warning for a bad initializer component. OPT is
6360 the option OPT_* (from options.h) controlling this warning or 0 if
6361 it is unconditionally given. GMSGID identifies the message. The
6362 component name is taken from the spelling stack. */
6364 static void ATTRIBUTE_GCC_DIAG (3,0)
6365 pedwarn_init (location_t loc
, int opt
, const char *gmsgid
, ...)
6367 /* Use the location where a macro was expanded rather than where
6368 it was defined to make sure macros defined in system headers
6369 but used incorrectly elsewhere are diagnosed. */
6370 location_t exploc
= expansion_point_location_if_in_system_header (loc
);
6371 auto_diagnostic_group d
;
6373 va_start (ap
, gmsgid
);
6374 bool warned
= emit_diagnostic_valist (DK_PEDWARN
, exploc
, opt
, gmsgid
, &ap
);
6376 char *ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6377 if (*ofwhat
&& warned
)
6378 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6381 /* Issue a warning for a bad initializer component.
6383 OPT is the OPT_W* value corresponding to the warning option that
6384 controls this warning. GMSGID identifies the message. The
6385 component name is taken from the spelling stack. */
6388 warning_init (location_t loc
, int opt
, const char *gmsgid
)
6393 auto_diagnostic_group d
;
6395 /* Use the location where a macro was expanded rather than where
6396 it was defined to make sure macros defined in system headers
6397 but used incorrectly elsewhere are diagnosed. */
6398 location_t exploc
= expansion_point_location_if_in_system_header (loc
);
6400 /* The gmsgid may be a format string with %< and %>. */
6401 warned
= warning_at (exploc
, opt
, gmsgid
);
6402 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6403 if (*ofwhat
&& warned
)
6404 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6407 /* If TYPE is an array type and EXPR is a parenthesized string
6408 constant, warn if pedantic that EXPR is being used to initialize an
6409 object of type TYPE. */
6412 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
6415 && TREE_CODE (type
) == ARRAY_TYPE
6416 && TREE_CODE (expr
.value
) == STRING_CST
6417 && expr
.original_code
!= STRING_CST
)
6418 pedwarn_init (loc
, OPT_Wpedantic
,
6419 "array initialized from parenthesized string constant");
6422 /* Attempt to locate the parameter with the given index within FNDECL,
6423 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6426 get_fndecl_argument_location (tree fndecl
, int argnum
)
6431 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6432 for (i
= 0, param
= DECL_ARGUMENTS (fndecl
);
6433 i
< argnum
&& param
;
6434 i
++, param
= TREE_CHAIN (param
))
6437 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6438 return DECL_SOURCE_LOCATION (FNDECL). */
6440 return DECL_SOURCE_LOCATION (fndecl
);
6442 return DECL_SOURCE_LOCATION (param
);
6445 /* Issue a note about a mismatching argument for parameter PARMNUM
6446 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6447 Attempt to issue the note at the pertinent parameter of the decl;
6448 failing that issue it at the location of FUNDECL; failing that
6449 issue it at PLOC. */
6452 inform_for_arg (tree fundecl
, location_t ploc
, int parmnum
,
6453 tree expected_type
, tree actual_type
)
6456 if (fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6457 loc
= get_fndecl_argument_location (fundecl
, parmnum
- 1);
6462 "expected %qT but argument is of type %qT",
6463 expected_type
, actual_type
);
6466 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6467 function FUNDECL declared without prototype to parameter PARMNUM of
6468 PARMTYPE when ARGTYPE does not promote to PARMTYPE. */
6471 maybe_warn_builtin_no_proto_arg (location_t loc
, tree fundecl
, int parmnum
,
6472 tree parmtype
, tree argtype
)
6474 tree_code parmcode
= TREE_CODE (parmtype
);
6475 tree_code argcode
= TREE_CODE (argtype
);
6476 tree promoted
= c_type_promotes_to (argtype
);
6478 /* Avoid warning for enum arguments that promote to an integer type
6479 of the same size/mode. */
6480 if (parmcode
== INTEGER_TYPE
6481 && argcode
== ENUMERAL_TYPE
6482 && TYPE_MODE (parmtype
) == TYPE_MODE (argtype
))
6485 if ((parmcode
== argcode
6486 || (parmcode
== INTEGER_TYPE
6487 && argcode
== ENUMERAL_TYPE
))
6488 && TYPE_MAIN_VARIANT (parmtype
) == TYPE_MAIN_VARIANT (promoted
))
6491 /* This diagnoses even signed/unsigned mismatches. Those might be
6492 safe in many cases but GCC may emit suboptimal code for them so
6493 warning on those cases drives efficiency improvements. */
6494 if (warning_at (loc
, OPT_Wbuiltin_declaration_mismatch
,
6495 TYPE_MAIN_VARIANT (promoted
) == argtype
6496 ? G_("%qD argument %d type is %qT where %qT is expected "
6497 "in a call to built-in function declared without "
6499 : G_("%qD argument %d promotes to %qT where %qT is expected "
6500 "in a call to built-in function declared without "
6502 fundecl
, parmnum
, promoted
, parmtype
))
6503 inform (DECL_SOURCE_LOCATION (fundecl
),
6504 "built-in %qD declared here",
6508 /* Convert value RHS to type TYPE as preparation for an assignment to
6509 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6510 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6511 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6512 constant before any folding.
6513 The real work of conversion is done by `convert'.
6514 The purpose of this function is to generate error messages
6515 for assignments that are not allowed in C.
6516 ERRTYPE says whether it is argument passing, assignment,
6517 initialization or return.
6519 In the following example, '~' denotes where EXPR_LOC and '^' where
6522 f (var); [ic_argpass]
6524 x = var; [ic_assign]
6526 int x = var; [ic_init]
6528 return x; [ic_return]
6531 FUNCTION is a tree for the function being called.
6532 PARMNUM is the number of the argument, for printing in error messages.
6533 WARNOPT may be set to a warning option to issue the corresponding warning
6534 rather than an error for invalid conversions. Used for calls to built-in
6535 functions declared without a prototype. */
6538 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
6539 tree rhs
, tree origtype
, enum impl_conv errtype
,
6540 bool null_pointer_constant
, tree fundecl
,
6541 tree function
, int parmnum
, int warnopt
/* = 0 */)
6543 enum tree_code codel
= TREE_CODE (type
);
6544 tree orig_rhs
= rhs
;
6546 enum tree_code coder
;
6547 tree rname
= NULL_TREE
;
6548 bool objc_ok
= false;
6550 /* Use the expansion point location to handle cases such as user's
6551 function returning a wrong-type macro defined in a system header. */
6552 location
= expansion_point_location_if_in_system_header (location
);
6554 if (errtype
== ic_argpass
)
6557 /* Change pointer to function to the function itself for
6559 if (TREE_CODE (function
) == ADDR_EXPR
6560 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
6561 function
= TREE_OPERAND (function
, 0);
6563 /* Handle an ObjC selector specially for diagnostics. */
6564 selector
= objc_message_selector ();
6566 if (selector
&& parmnum
> 2)
6573 /* This macro is used to emit diagnostics to ensure that all format
6574 strings are complete sentences, visible to gettext and checked at
6576 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6582 auto_diagnostic_group d; \
6583 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6584 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6588 pedwarn (LOCATION, OPT, AS); \
6591 pedwarn_init (LOCATION, OPT, IN); \
6594 pedwarn (LOCATION, OPT, RE); \
6597 gcc_unreachable (); \
6601 /* This macro is used to emit diagnostics to ensure that all format
6602 strings are complete sentences, visible to gettext and checked at
6603 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6604 extra parameter to enumerate qualifiers. */
6605 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6611 auto_diagnostic_group d; \
6612 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6613 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6617 pedwarn (LOCATION, OPT, AS, QUALS); \
6620 pedwarn (LOCATION, OPT, IN, QUALS); \
6623 pedwarn (LOCATION, OPT, RE, QUALS); \
6626 gcc_unreachable (); \
6630 /* This macro is used to emit diagnostics to ensure that all format
6631 strings are complete sentences, visible to gettext and checked at
6632 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6633 warning_at instead of pedwarn. */
6634 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6640 auto_diagnostic_group d; \
6641 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6642 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6646 warning_at (LOCATION, OPT, AS, QUALS); \
6649 warning_at (LOCATION, OPT, IN, QUALS); \
6652 warning_at (LOCATION, OPT, RE, QUALS); \
6655 gcc_unreachable (); \
6659 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6660 rhs
= TREE_OPERAND (rhs
, 0);
6662 rhstype
= TREE_TYPE (rhs
);
6663 coder
= TREE_CODE (rhstype
);
6665 if (coder
== ERROR_MARK
)
6666 return error_mark_node
;
6668 if (c_dialect_objc ())
6691 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
6694 if (warn_cxx_compat
)
6696 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
6697 if (checktype
!= error_mark_node
6698 && TREE_CODE (type
) == ENUMERAL_TYPE
6699 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
6703 if (pedwarn (expr_loc
, OPT_Wc___compat
, "enum conversion when "
6704 "passing argument %d of %qE is invalid in C++",
6706 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6707 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6708 "expected %qT but argument is of type %qT",
6712 pedwarn (location
, OPT_Wc___compat
, "enum conversion from %qT to "
6713 "%qT in assignment is invalid in C++", rhstype
, type
);
6716 pedwarn_init (location
, OPT_Wc___compat
, "enum conversion from "
6717 "%qT to %qT in initialization is invalid in C++",
6721 pedwarn (location
, OPT_Wc___compat
, "enum conversion from %qT to "
6722 "%qT in return is invalid in C++", rhstype
, type
);
6729 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
6731 warn_for_address_or_pointer_of_packed_member (type
, orig_rhs
);
6735 if (coder
== VOID_TYPE
)
6737 /* Except for passing an argument to an unprototyped function,
6738 this is a constraint violation. When passing an argument to
6739 an unprototyped function, it is compile-time undefined;
6740 making it a constraint in that case was rejected in
6742 const char msg
[] = "void value not ignored as it ought to be";
6744 warning_at (location
, warnopt
, msg
);
6746 error_at (location
, msg
);
6747 return error_mark_node
;
6749 rhs
= require_complete_type (location
, rhs
);
6750 if (rhs
== error_mark_node
)
6751 return error_mark_node
;
6753 if (coder
== POINTER_TYPE
&& reject_gcc_builtin (rhs
))
6754 return error_mark_node
;
6756 /* A non-reference type can convert to a reference. This handles
6757 va_start, va_copy and possibly port built-ins. */
6758 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
6760 if (!lvalue_p (rhs
))
6762 const char msg
[] = "cannot pass rvalue to reference parameter";
6764 warning_at (location
, warnopt
, msg
);
6766 error_at (location
, msg
);
6767 return error_mark_node
;
6769 if (!c_mark_addressable (rhs
))
6770 return error_mark_node
;
6771 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
6772 SET_EXPR_LOCATION (rhs
, location
);
6774 rhs
= convert_for_assignment (location
, expr_loc
,
6775 build_pointer_type (TREE_TYPE (type
)),
6776 rhs
, origtype
, errtype
,
6777 null_pointer_constant
, fundecl
, function
,
6779 if (rhs
== error_mark_node
)
6780 return error_mark_node
;
6782 rhs
= build1 (NOP_EXPR
, type
, rhs
);
6783 SET_EXPR_LOCATION (rhs
, location
);
6786 /* Some types can interconvert without explicit casts. */
6787 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
6788 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
6789 return convert (type
, rhs
);
6790 /* Arithmetic types all interconvert, and enum is treated like int. */
6791 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
6792 || codel
== FIXED_POINT_TYPE
6793 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
6794 || codel
== BOOLEAN_TYPE
)
6795 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
6796 || coder
== FIXED_POINT_TYPE
6797 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
6798 || coder
== BOOLEAN_TYPE
))
6800 if (warnopt
&& errtype
== ic_argpass
)
6801 maybe_warn_builtin_no_proto_arg (expr_loc
, fundecl
, parmnum
, type
,
6804 bool save
= in_late_binary_op
;
6805 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
6806 || (coder
== REAL_TYPE
6807 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
6808 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
6809 in_late_binary_op
= true;
6810 tree ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6811 ? expr_loc
: location
, type
, orig_rhs
);
6812 in_late_binary_op
= save
;
6816 /* Aggregates in different TUs might need conversion. */
6817 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
6819 && comptypes (type
, rhstype
))
6820 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6821 ? expr_loc
: location
, type
, rhs
);
6823 /* Conversion to a transparent union or record from its member types.
6824 This applies only to function arguments. */
6825 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
6826 && TYPE_TRANSPARENT_AGGR (type
))
6827 && errtype
== ic_argpass
)
6829 tree memb
, marginal_memb
= NULL_TREE
;
6831 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
6833 tree memb_type
= TREE_TYPE (memb
);
6835 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
6836 TYPE_MAIN_VARIANT (rhstype
)))
6839 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
6842 if (coder
== POINTER_TYPE
)
6844 tree ttl
= TREE_TYPE (memb_type
);
6845 tree ttr
= TREE_TYPE (rhstype
);
6847 /* Any non-function converts to a [const][volatile] void *
6848 and vice versa; otherwise, targets must be the same.
6849 Meanwhile, the lhs target must have all the qualifiers of
6851 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6852 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6853 || comp_target_types (location
, memb_type
, rhstype
))
6855 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
6856 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
6857 /* If this type won't generate any warnings, use it. */
6858 if (lquals
== rquals
6859 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
6860 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6861 ? ((lquals
| rquals
) == rquals
)
6862 : ((lquals
| rquals
) == lquals
)))
6865 /* Keep looking for a better type, but remember this one. */
6867 marginal_memb
= memb
;
6871 /* Can convert integer zero to any pointer type. */
6872 if (null_pointer_constant
)
6874 rhs
= null_pointer_node
;
6879 if (memb
|| marginal_memb
)
6883 /* We have only a marginally acceptable member type;
6884 it needs a warning. */
6885 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
6886 tree ttr
= TREE_TYPE (rhstype
);
6888 /* Const and volatile mean something different for function
6889 types, so the usual warnings are not appropriate. */
6890 if (TREE_CODE (ttr
) == FUNCTION_TYPE
6891 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6893 /* Because const and volatile on functions are
6894 restrictions that say the function will not do
6895 certain things, it is okay to use a const or volatile
6896 function where an ordinary one is wanted, but not
6898 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6899 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6900 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6901 OPT_Wdiscarded_qualifiers
,
6902 G_("passing argument %d of %qE "
6903 "makes %q#v qualified function "
6904 "pointer from unqualified"),
6905 G_("assignment makes %q#v qualified "
6906 "function pointer from "
6908 G_("initialization makes %q#v qualified "
6909 "function pointer from "
6911 G_("return makes %q#v qualified function "
6912 "pointer from unqualified"),
6913 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6915 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
6916 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
6917 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6918 OPT_Wdiscarded_qualifiers
,
6919 G_("passing argument %d of %qE discards "
6920 "%qv qualifier from pointer target type"),
6921 G_("assignment discards %qv qualifier "
6922 "from pointer target type"),
6923 G_("initialization discards %qv qualifier "
6924 "from pointer target type"),
6925 G_("return discards %qv qualifier from "
6926 "pointer target type"),
6927 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6929 memb
= marginal_memb
;
6932 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
6933 pedwarn (location
, OPT_Wpedantic
,
6934 "ISO C prohibits argument conversion to union type");
6936 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
6937 return build_constructor_single (type
, memb
, rhs
);
6941 /* Conversions among pointers */
6942 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
6943 && (coder
== codel
))
6945 /* If RHS refers to a built-in declared without a prototype
6946 BLTIN is the declaration of the built-in with a prototype
6947 and RHSTYPE is set to the actual type of the built-in. */
6949 rhstype
= type_or_builtin_type (rhs
, &bltin
);
6951 tree ttl
= TREE_TYPE (type
);
6952 tree ttr
= TREE_TYPE (rhstype
);
6955 bool is_opaque_pointer
;
6956 int target_cmp
= 0; /* Cache comp_target_types () result. */
6960 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
6961 mvl
= (TYPE_ATOMIC (mvl
)
6962 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
6964 : TYPE_MAIN_VARIANT (mvl
));
6965 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
6966 mvr
= (TYPE_ATOMIC (mvr
)
6967 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
6969 : TYPE_MAIN_VARIANT (mvr
));
6970 /* Opaque pointers are treated like void pointers. */
6971 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
6973 /* The Plan 9 compiler permits a pointer to a struct to be
6974 automatically converted into a pointer to an anonymous field
6975 within the struct. */
6976 if (flag_plan9_extensions
6977 && RECORD_OR_UNION_TYPE_P (mvl
)
6978 && RECORD_OR_UNION_TYPE_P (mvr
)
6981 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
6982 if (new_rhs
!= NULL_TREE
)
6985 rhstype
= TREE_TYPE (rhs
);
6986 coder
= TREE_CODE (rhstype
);
6987 ttr
= TREE_TYPE (rhstype
);
6988 mvr
= TYPE_MAIN_VARIANT (ttr
);
6992 /* C++ does not allow the implicit conversion void* -> T*. However,
6993 for the purpose of reducing the number of false positives, we
6994 tolerate the special case of
6998 where NULL is typically defined in C to be '(void *) 0'. */
6999 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
7000 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
7002 "request for implicit conversion "
7003 "from %qT to %qT not permitted in C++", rhstype
, type
);
7005 /* See if the pointers point to incompatible address spaces. */
7006 asl
= TYPE_ADDR_SPACE (ttl
);
7007 asr
= TYPE_ADDR_SPACE (ttr
);
7008 if (!null_pointer_constant_p (rhs
)
7009 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
7015 const char msg
[] = G_("passing argument %d of %qE from "
7016 "pointer to non-enclosed address space");
7018 warning_at (expr_loc
, warnopt
, msg
, parmnum
, rname
);
7020 error_at (expr_loc
, msg
, parmnum
, rname
);
7025 const char msg
[] = G_("assignment from pointer to "
7026 "non-enclosed address space");
7028 warning_at (location
, warnopt
, msg
);
7030 error_at (location
, msg
);
7035 const char msg
[] = G_("initialization from pointer to "
7036 "non-enclosed address space");
7038 warning_at (location
, warnopt
, msg
);
7040 error_at (location
, msg
);
7045 const char msg
[] = G_("return from pointer to "
7046 "non-enclosed address space");
7048 warning_at (location
, warnopt
, msg
);
7050 error_at (location
, msg
);
7056 return error_mark_node
;
7059 /* Check if the right-hand side has a format attribute but the
7060 left-hand side doesn't. */
7061 if (warn_suggest_attribute_format
7062 && check_missing_format_attribute (type
, rhstype
))
7067 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
7068 "argument %d of %qE might be "
7069 "a candidate for a format attribute",
7073 warning_at (location
, OPT_Wsuggest_attribute_format
,
7074 "assignment left-hand side might be "
7075 "a candidate for a format attribute");
7078 warning_at (location
, OPT_Wsuggest_attribute_format
,
7079 "initialization left-hand side might be "
7080 "a candidate for a format attribute");
7083 warning_at (location
, OPT_Wsuggest_attribute_format
,
7084 "return type might be "
7085 "a candidate for a format attribute");
7092 /* Any non-function converts to a [const][volatile] void *
7093 and vice versa; otherwise, targets must be the same.
7094 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
7095 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
7096 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
7097 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
7098 || is_opaque_pointer
7099 || ((c_common_unsigned_type (mvl
)
7100 == c_common_unsigned_type (mvr
))
7101 && (c_common_signed_type (mvl
)
7102 == c_common_signed_type (mvr
))
7103 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
7105 /* Warn about loss of qualifers from pointers to arrays with
7106 qualifiers on the element type. */
7107 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
7109 ttr
= strip_array_types (ttr
);
7110 ttl
= strip_array_types (ttl
);
7112 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
7113 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
7114 WARNING_FOR_QUALIFIERS (location
, expr_loc
,
7115 OPT_Wdiscarded_array_qualifiers
,
7116 G_("passing argument %d of %qE discards "
7117 "%qv qualifier from pointer target type"),
7118 G_("assignment discards %qv qualifier "
7119 "from pointer target type"),
7120 G_("initialization discards %qv qualifier "
7121 "from pointer target type"),
7122 G_("return discards %qv qualifier from "
7123 "pointer target type"),
7124 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
7127 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
7130 && !null_pointer_constant
7131 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
7132 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
7133 G_("ISO C forbids passing argument %d of "
7134 "%qE between function pointer "
7136 G_("ISO C forbids assignment between "
7137 "function pointer and %<void *%>"),
7138 G_("ISO C forbids initialization between "
7139 "function pointer and %<void *%>"),
7140 G_("ISO C forbids return between function "
7141 "pointer and %<void *%>"));
7142 /* Const and volatile mean something different for function types,
7143 so the usual warnings are not appropriate. */
7144 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
7145 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
7147 /* Don't warn about loss of qualifier for conversions from
7148 qualified void* to pointers to arrays with corresponding
7149 qualifier on the element type. */
7151 ttl
= strip_array_types (ttl
);
7153 /* Assignments between atomic and non-atomic objects are OK. */
7154 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
7155 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
7157 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
7158 OPT_Wdiscarded_qualifiers
,
7159 G_("passing argument %d of %qE discards "
7160 "%qv qualifier from pointer target type"),
7161 G_("assignment discards %qv qualifier "
7162 "from pointer target type"),
7163 G_("initialization discards %qv qualifier "
7164 "from pointer target type"),
7165 G_("return discards %qv qualifier from "
7166 "pointer target type"),
7167 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
7169 /* If this is not a case of ignoring a mismatch in signedness,
7171 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
7174 /* If there is a mismatch, do warn. */
7175 else if (warn_pointer_sign
)
7180 auto_diagnostic_group d
;
7181 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7182 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7183 if (pedwarn (&richloc
, OPT_Wpointer_sign
,
7184 "pointer targets in passing argument %d of "
7185 "%qE differ in signedness", parmnum
, rname
))
7186 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
,
7191 pedwarn (location
, OPT_Wpointer_sign
,
7192 "pointer targets in assignment from %qT to %qT "
7193 "differ in signedness", rhstype
, type
);
7196 pedwarn_init (location
, OPT_Wpointer_sign
,
7197 "pointer targets in initialization of %qT "
7198 "from %qT differ in signedness", type
,
7202 pedwarn (location
, OPT_Wpointer_sign
, "pointer targets in "
7203 "returning %qT from a function with return type "
7204 "%qT differ in signedness", rhstype
, type
);
7210 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
7211 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
7213 /* Because const and volatile on functions are restrictions
7214 that say the function will not do certain things,
7215 it is okay to use a const or volatile function
7216 where an ordinary one is wanted, but not vice-versa. */
7217 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
7218 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
7219 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
7220 OPT_Wdiscarded_qualifiers
,
7221 G_("passing argument %d of %qE makes "
7222 "%q#v qualified function pointer "
7223 "from unqualified"),
7224 G_("assignment makes %q#v qualified function "
7225 "pointer from unqualified"),
7226 G_("initialization makes %q#v qualified "
7227 "function pointer from unqualified"),
7228 G_("return makes %q#v qualified function "
7229 "pointer from unqualified"),
7230 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
7233 /* Avoid warning about the volatile ObjC EH puts on decls. */
7240 auto_diagnostic_group d
;
7241 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7242 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7243 if (pedwarn (&richloc
, OPT_Wincompatible_pointer_types
,
7244 "passing argument %d of %qE from incompatible "
7245 "pointer type", parmnum
, rname
))
7246 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7251 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7252 "assignment to %qT from pointer to "
7253 "%qD with incompatible type %qT",
7254 type
, bltin
, rhstype
);
7256 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7257 "assignment to %qT from incompatible pointer type %qT",
7262 pedwarn_init (location
, OPT_Wincompatible_pointer_types
,
7263 "initialization of %qT from pointer to "
7264 "%qD with incompatible type %qT",
7265 type
, bltin
, rhstype
);
7267 pedwarn_init (location
, OPT_Wincompatible_pointer_types
,
7268 "initialization of %qT from incompatible "
7274 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7275 "returning pointer to %qD of type %qT from "
7276 "a function with incompatible type %qT",
7277 bltin
, rhstype
, type
);
7279 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7280 "returning %qT from a function with incompatible "
7281 "return type %qT", rhstype
, type
);
7288 /* If RHS isn't an address, check pointer or array of packed
7290 warn_for_address_or_pointer_of_packed_member (type
, orig_rhs
);
7292 return convert (type
, rhs
);
7294 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
7296 /* ??? This should not be an error when inlining calls to
7297 unprototyped functions. */
7298 const char msg
[] = "invalid use of non-lvalue array";
7300 warning_at (location
, warnopt
, msg
);
7302 error_at (location
, msg
);
7303 return error_mark_node
;
7305 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
7307 /* An explicit constant 0 can convert to a pointer,
7308 or one that results from arithmetic, even including
7309 a cast to integer type. */
7310 if (!null_pointer_constant
)
7315 auto_diagnostic_group d
;
7316 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7317 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7318 if (pedwarn (&richloc
, OPT_Wint_conversion
,
7319 "passing argument %d of %qE makes pointer from "
7320 "integer without a cast", parmnum
, rname
))
7321 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7325 pedwarn (location
, OPT_Wint_conversion
,
7326 "assignment to %qT from %qT makes pointer from integer "
7327 "without a cast", type
, rhstype
);
7330 pedwarn_init (location
, OPT_Wint_conversion
,
7331 "initialization of %qT from %qT makes pointer from "
7332 "integer without a cast", type
, rhstype
);
7335 pedwarn (location
, OPT_Wint_conversion
, "returning %qT from a "
7336 "function with return type %qT makes pointer from "
7337 "integer without a cast", rhstype
, type
);
7343 return convert (type
, rhs
);
7345 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
7351 auto_diagnostic_group d
;
7352 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7353 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7354 if (pedwarn (&richloc
, OPT_Wint_conversion
,
7355 "passing argument %d of %qE makes integer from "
7356 "pointer without a cast", parmnum
, rname
))
7357 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7361 pedwarn (location
, OPT_Wint_conversion
,
7362 "assignment to %qT from %qT makes integer from pointer "
7363 "without a cast", type
, rhstype
);
7366 pedwarn_init (location
, OPT_Wint_conversion
,
7367 "initialization of %qT from %qT makes integer from "
7368 "pointer without a cast", type
, rhstype
);
7371 pedwarn (location
, OPT_Wint_conversion
, "returning %qT from a "
7372 "function with return type %qT makes integer from "
7373 "pointer without a cast", rhstype
, type
);
7379 return convert (type
, rhs
);
7381 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
7384 bool save
= in_late_binary_op
;
7385 in_late_binary_op
= true;
7386 ret
= convert (type
, rhs
);
7387 in_late_binary_op
= save
;
7395 auto_diagnostic_group d
;
7396 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7397 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7398 const char msg
[] = G_("incompatible type for argument %d of %qE");
7400 warning_at (expr_loc
, warnopt
, msg
, parmnum
, rname
);
7402 error_at (&richloc
, msg
, parmnum
, rname
);
7403 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7409 = G_("incompatible types when assigning to type %qT from type %qT");
7411 warning_at (expr_loc
, 0, msg
, type
, rhstype
);
7413 error_at (expr_loc
, msg
, type
, rhstype
);
7419 = G_("incompatible types when initializing type %qT using type %qT");
7421 warning_at (location
, 0, msg
, type
, rhstype
);
7423 error_at (location
, msg
, type
, rhstype
);
7429 = G_("incompatible types when returning type %qT but %qT was expected");
7431 warning_at (location
, 0, msg
, rhstype
, type
);
7433 error_at (location
, msg
, rhstype
, type
);
7440 return error_mark_node
;
7443 /* If VALUE is a compound expr all of whose expressions are constant, then
7444 return its value. Otherwise, return error_mark_node.
7446 This is for handling COMPOUND_EXPRs as initializer elements
7447 which is allowed with a warning when -pedantic is specified. */
7450 valid_compound_expr_initializer (tree value
, tree endtype
)
7452 if (TREE_CODE (value
) == COMPOUND_EXPR
)
7454 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
7456 return error_mark_node
;
7457 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
7460 else if (!initializer_constant_valid_p (value
, endtype
))
7461 return error_mark_node
;
7466 /* Perform appropriate conversions on the initial value of a variable,
7467 store it in the declaration DECL,
7468 and print any error messages that are appropriate.
7469 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7470 If the init is invalid, store an ERROR_MARK.
7472 INIT_LOC is the location of the initial value. */
7475 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
7480 /* If variable's type was invalidly declared, just ignore it. */
7482 type
= TREE_TYPE (decl
);
7483 if (TREE_CODE (type
) == ERROR_MARK
)
7486 /* Digest the specified initializer into an expression. */
7489 npc
= null_pointer_constant_p (init
);
7490 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
7491 true, TREE_STATIC (decl
));
7493 /* Store the expression if valid; else report error. */
7495 if (!in_system_header_at (input_location
)
7496 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
7497 warning (OPT_Wtraditional
, "traditional C rejects automatic "
7498 "aggregate initialization");
7500 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
7501 DECL_INITIAL (decl
) = value
;
7503 /* ANSI wants warnings about out-of-range constant initializers. */
7504 STRIP_TYPE_NOPS (value
);
7505 if (TREE_STATIC (decl
))
7506 constant_expression_warning (value
);
7508 /* Check if we need to set array size from compound literal size. */
7509 if (TREE_CODE (type
) == ARRAY_TYPE
7510 && TYPE_DOMAIN (type
) == NULL_TREE
7511 && value
!= error_mark_node
)
7513 tree inside_init
= init
;
7515 STRIP_TYPE_NOPS (inside_init
);
7516 inside_init
= fold (inside_init
);
7518 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7520 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
7522 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
7524 /* For int foo[] = (int [3]){1}; we need to set array size
7525 now since later on array initializer will be just the
7526 brace enclosed list of the compound literal. */
7527 tree etype
= strip_array_types (TREE_TYPE (decl
));
7528 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
7529 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
7531 layout_decl (cldecl
, 0);
7533 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
7539 /* Methods for storing and printing names for error messages. */
7541 /* Implement a spelling stack that allows components of a name to be pushed
7542 and popped. Each element on the stack is this structure. */
7549 unsigned HOST_WIDE_INT i
;
7554 #define SPELLING_STRING 1
7555 #define SPELLING_MEMBER 2
7556 #define SPELLING_BOUNDS 3
7558 static struct spelling
*spelling
; /* Next stack element (unused). */
7559 static struct spelling
*spelling_base
; /* Spelling stack base. */
7560 static int spelling_size
; /* Size of the spelling stack. */
7562 /* Macros to save and restore the spelling stack around push_... functions.
7563 Alternative to SAVE_SPELLING_STACK. */
7565 #define SPELLING_DEPTH() (spelling - spelling_base)
7566 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7568 /* Push an element on the spelling stack with type KIND and assign VALUE
7571 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7573 int depth = SPELLING_DEPTH (); \
7575 if (depth >= spelling_size) \
7577 spelling_size += 10; \
7578 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7580 RESTORE_SPELLING_DEPTH (depth); \
7583 spelling->kind = (KIND); \
7584 spelling->MEMBER = (VALUE); \
7588 /* Push STRING on the stack. Printed literally. */
7591 push_string (const char *string
)
7593 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
7596 /* Push a member name on the stack. Printed as '.' STRING. */
7599 push_member_name (tree decl
)
7601 const char *const string
7603 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
7604 : _("<anonymous>"));
7605 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
7608 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7611 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
7613 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
7616 /* Compute the maximum size in bytes of the printed spelling. */
7619 spelling_length (void)
7624 for (p
= spelling_base
; p
< spelling
; p
++)
7626 if (p
->kind
== SPELLING_BOUNDS
)
7629 size
+= strlen (p
->u
.s
) + 1;
7635 /* Print the spelling to BUFFER and return it. */
7638 print_spelling (char *buffer
)
7643 for (p
= spelling_base
; p
< spelling
; p
++)
7644 if (p
->kind
== SPELLING_BOUNDS
)
7646 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
7652 if (p
->kind
== SPELLING_MEMBER
)
7654 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
7661 /* Digest the parser output INIT as an initializer for type TYPE.
7662 Return a C expression of type TYPE to represent the initial value.
7664 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7666 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7668 If INIT is a string constant, STRICT_STRING is true if it is
7669 unparenthesized or we should not warn here for it being parenthesized.
7670 For other types of INIT, STRICT_STRING is not used.
7672 INIT_LOC is the location of the INIT.
7674 REQUIRE_CONSTANT requests an error if non-constant initializers or
7675 elements are seen. */
7678 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
7679 bool null_pointer_constant
, bool strict_string
,
7680 int require_constant
)
7682 enum tree_code code
= TREE_CODE (type
);
7683 tree inside_init
= init
;
7684 tree semantic_type
= NULL_TREE
;
7685 bool maybe_const
= true;
7687 if (type
== error_mark_node
7689 || error_operand_p (init
))
7690 return error_mark_node
;
7692 STRIP_TYPE_NOPS (inside_init
);
7694 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
7696 semantic_type
= TREE_TYPE (inside_init
);
7697 inside_init
= TREE_OPERAND (inside_init
, 0);
7699 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
7701 /* Initialization of an array of chars from a string constant
7702 optionally enclosed in braces. */
7704 if (code
== ARRAY_TYPE
&& inside_init
7705 && TREE_CODE (inside_init
) == STRING_CST
)
7708 = (TYPE_ATOMIC (TREE_TYPE (type
))
7709 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
7711 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
7712 /* Note that an array could be both an array of character type
7713 and an array of wchar_t if wchar_t is signed char or unsigned
7715 bool char_array
= (typ1
== char_type_node
7716 || typ1
== signed_char_type_node
7717 || typ1
== unsigned_char_type_node
);
7718 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
7719 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
7720 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
7722 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
7725 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
7726 bool incompat_string_cst
= false;
7727 expr
.value
= inside_init
;
7728 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
7729 expr
.original_type
= NULL
;
7730 maybe_warn_string_init (init_loc
, type
, expr
);
7732 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
7733 pedwarn_init (init_loc
, OPT_Wpedantic
,
7734 "initialization of a flexible array member");
7736 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7737 TYPE_MAIN_VARIANT (type
)))
7742 if (typ2
!= char_type_node
)
7743 incompat_string_cst
= true;
7745 else if (!comptypes (typ1
, typ2
))
7746 incompat_string_cst
= true;
7748 if (incompat_string_cst
)
7750 error_init (init_loc
, "cannot initialize array of %qT from "
7751 "a string literal with type array of %qT",
7753 return error_mark_node
;
7756 if (TYPE_DOMAIN (type
) != NULL_TREE
7757 && TYPE_SIZE (type
) != NULL_TREE
7758 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
7760 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
7761 unsigned unit
= TYPE_PRECISION (typ1
) / BITS_PER_UNIT
;
7763 /* Subtract the size of a single (possibly wide) character
7764 because it's ok to ignore the terminating null char
7765 that is counted in the length of the constant. */
7766 if (compare_tree_int (TYPE_SIZE_UNIT (type
), len
- unit
) < 0)
7767 pedwarn_init (init_loc
, 0,
7768 ("initializer-string for array of %qT "
7769 "is too long"), typ1
);
7770 else if (warn_cxx_compat
7771 && compare_tree_int (TYPE_SIZE_UNIT (type
), len
) < 0)
7772 warning_at (init_loc
, OPT_Wc___compat
,
7773 ("initializer-string for array of %qT "
7774 "is too long for C++"), typ1
);
7775 if (compare_tree_int (TYPE_SIZE_UNIT (type
), len
) < 0)
7777 unsigned HOST_WIDE_INT size
7778 = tree_to_uhwi (TYPE_SIZE_UNIT (type
));
7779 const char *p
= TREE_STRING_POINTER (inside_init
);
7781 inside_init
= build_string (size
, p
);
7785 TREE_TYPE (inside_init
) = type
;
7788 else if (INTEGRAL_TYPE_P (typ1
))
7790 error_init (init_loc
, "array of inappropriate type initialized "
7791 "from string constant");
7792 return error_mark_node
;
7796 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7797 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7798 below and handle as a constructor. */
7799 if (code
== VECTOR_TYPE
7800 && VECTOR_TYPE_P (TREE_TYPE (inside_init
))
7801 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
7802 && TREE_CONSTANT (inside_init
))
7804 if (TREE_CODE (inside_init
) == VECTOR_CST
7805 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7806 TYPE_MAIN_VARIANT (type
)))
7809 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
7811 unsigned HOST_WIDE_INT ix
;
7813 bool constant_p
= true;
7815 /* Iterate through elements and check if all constructor
7816 elements are *_CSTs. */
7817 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
7818 if (!CONSTANT_CLASS_P (value
))
7825 return build_vector_from_ctor (type
,
7826 CONSTRUCTOR_ELTS (inside_init
));
7830 if (warn_sequence_point
)
7831 verify_sequence_points (inside_init
);
7833 /* Any type can be initialized
7834 from an expression of the same type, optionally with braces. */
7836 if (inside_init
&& TREE_TYPE (inside_init
) != NULL_TREE
7837 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7838 TYPE_MAIN_VARIANT (type
))
7839 || (code
== ARRAY_TYPE
7840 && comptypes (TREE_TYPE (inside_init
), type
))
7841 || (code
== VECTOR_TYPE
7842 && comptypes (TREE_TYPE (inside_init
), type
))
7843 || (code
== POINTER_TYPE
7844 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
7845 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
7846 TREE_TYPE (type
)))))
7848 if (code
== POINTER_TYPE
)
7850 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
7852 if (TREE_CODE (inside_init
) == STRING_CST
7853 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7854 inside_init
= array_to_pointer_conversion
7855 (init_loc
, inside_init
);
7858 error_init (init_loc
, "invalid use of non-lvalue array");
7859 return error_mark_node
;
7864 if (code
== VECTOR_TYPE
)
7865 /* Although the types are compatible, we may require a
7867 inside_init
= convert (type
, inside_init
);
7869 if (require_constant
7870 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7872 /* As an extension, allow initializing objects with static storage
7873 duration with compound literals (which are then treated just as
7874 the brace enclosed list they contain). Also allow this for
7875 vectors, as we can only assign them with compound literals. */
7876 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
7877 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
7879 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
7880 inside_init
= DECL_INITIAL (decl
);
7883 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
7884 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
7886 error_init (init_loc
, "array initialized from non-constant array "
7888 return error_mark_node
;
7891 /* Compound expressions can only occur here if -Wpedantic or
7892 -pedantic-errors is specified. In the later case, we always want
7893 an error. In the former case, we simply want a warning. */
7894 if (require_constant
&& pedantic
7895 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
7898 = valid_compound_expr_initializer (inside_init
,
7899 TREE_TYPE (inside_init
));
7900 if (inside_init
== error_mark_node
)
7901 error_init (init_loc
, "initializer element is not constant");
7903 pedwarn_init (init_loc
, OPT_Wpedantic
,
7904 "initializer element is not constant");
7905 if (flag_pedantic_errors
)
7906 inside_init
= error_mark_node
;
7908 else if (require_constant
7909 && !initializer_constant_valid_p (inside_init
,
7910 TREE_TYPE (inside_init
)))
7912 error_init (init_loc
, "initializer element is not constant");
7913 inside_init
= error_mark_node
;
7915 else if (require_constant
&& !maybe_const
)
7916 pedwarn_init (init_loc
, OPT_Wpedantic
,
7917 "initializer element is not a constant expression");
7919 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7920 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
7921 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
7922 type
, inside_init
, origtype
,
7923 ic_init
, null_pointer_constant
,
7924 NULL_TREE
, NULL_TREE
, 0);
7928 /* Handle scalar types, including conversions. */
7930 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
7931 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
7932 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
7934 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
7935 && (TREE_CODE (init
) == STRING_CST
7936 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
7937 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
7939 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
7942 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
7943 inside_init
, origtype
, ic_init
,
7944 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
7947 /* Check to see if we have already given an error message. */
7948 if (inside_init
== error_mark_node
)
7950 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
7952 error_init (init_loc
, "initializer element is not constant");
7953 inside_init
= error_mark_node
;
7955 else if (require_constant
7956 && !initializer_constant_valid_p (inside_init
,
7957 TREE_TYPE (inside_init
)))
7959 error_init (init_loc
, "initializer element is not computable at "
7961 inside_init
= error_mark_node
;
7963 else if (require_constant
&& !maybe_const
)
7964 pedwarn_init (init_loc
, OPT_Wpedantic
,
7965 "initializer element is not a constant expression");
7970 /* Come here only for records and arrays. */
7972 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
7974 error_init (init_loc
, "variable-sized object may not be initialized");
7975 return error_mark_node
;
7978 error_init (init_loc
, "invalid initializer");
7979 return error_mark_node
;
7982 /* Handle initializers that use braces. */
7984 /* Type of object we are accumulating a constructor for.
7985 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7986 static tree constructor_type
;
7988 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7990 static tree constructor_fields
;
7992 /* For an ARRAY_TYPE, this is the specified index
7993 at which to store the next element we get. */
7994 static tree constructor_index
;
7996 /* For an ARRAY_TYPE, this is the maximum index. */
7997 static tree constructor_max_index
;
7999 /* For a RECORD_TYPE, this is the first field not yet written out. */
8000 static tree constructor_unfilled_fields
;
8002 /* For an ARRAY_TYPE, this is the index of the first element
8003 not yet written out. */
8004 static tree constructor_unfilled_index
;
8006 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8007 This is so we can generate gaps between fields, when appropriate. */
8008 static tree constructor_bit_index
;
8010 /* If we are saving up the elements rather than allocating them,
8011 this is the list of elements so far (in reverse order,
8012 most recent first). */
8013 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
8015 /* 1 if constructor should be incrementally stored into a constructor chain,
8016 0 if all the elements should be kept in AVL tree. */
8017 static int constructor_incremental
;
8019 /* 1 if so far this constructor's elements are all compile-time constants. */
8020 static int constructor_constant
;
8022 /* 1 if so far this constructor's elements are all valid address constants. */
8023 static int constructor_simple
;
8025 /* 1 if this constructor has an element that cannot be part of a
8026 constant expression. */
8027 static int constructor_nonconst
;
8029 /* 1 if this constructor is erroneous so far. */
8030 static int constructor_erroneous
;
8032 /* 1 if this constructor is the universal zero initializer { 0 }. */
8033 static int constructor_zeroinit
;
8035 /* Structure for managing pending initializer elements, organized as an
8040 struct init_node
*left
, *right
;
8041 struct init_node
*parent
;
8048 /* Tree of pending elements at this constructor level.
8049 These are elements encountered out of order
8050 which belong at places we haven't reached yet in actually
8052 Will never hold tree nodes across GC runs. */
8053 static struct init_node
*constructor_pending_elts
;
8055 /* The SPELLING_DEPTH of this constructor. */
8056 static int constructor_depth
;
8058 /* DECL node for which an initializer is being read.
8059 0 means we are reading a constructor expression
8060 such as (struct foo) {...}. */
8061 static tree constructor_decl
;
8063 /* Nonzero if this is an initializer for a top-level decl. */
8064 static int constructor_top_level
;
8066 /* Nonzero if there were any member designators in this initializer. */
8067 static int constructor_designated
;
8069 /* Nesting depth of designator list. */
8070 static int designator_depth
;
8072 /* Nonzero if there were diagnosed errors in this designator list. */
8073 static int designator_erroneous
;
8076 /* This stack has a level for each implicit or explicit level of
8077 structuring in the initializer, including the outermost one. It
8078 saves the values of most of the variables above. */
8080 struct constructor_range_stack
;
8082 struct constructor_stack
8084 struct constructor_stack
*next
;
8089 tree unfilled_index
;
8090 tree unfilled_fields
;
8092 vec
<constructor_elt
, va_gc
> *elements
;
8093 struct init_node
*pending_elts
;
8096 /* If value nonzero, this value should replace the entire
8097 constructor at this level. */
8098 struct c_expr replacement_value
;
8099 struct constructor_range_stack
*range_stack
;
8108 int designator_depth
;
8111 static struct constructor_stack
*constructor_stack
;
8113 /* This stack represents designators from some range designator up to
8114 the last designator in the list. */
8116 struct constructor_range_stack
8118 struct constructor_range_stack
*next
, *prev
;
8119 struct constructor_stack
*stack
;
8126 static struct constructor_range_stack
*constructor_range_stack
;
8128 /* This stack records separate initializers that are nested.
8129 Nested initializers can't happen in ANSI C, but GNU C allows them
8130 in cases like { ... (struct foo) { ... } ... }. */
8132 struct initializer_stack
8134 struct initializer_stack
*next
;
8136 struct constructor_stack
*constructor_stack
;
8137 struct constructor_range_stack
*constructor_range_stack
;
8138 vec
<constructor_elt
, va_gc
> *elements
;
8139 struct spelling
*spelling
;
8140 struct spelling
*spelling_base
;
8143 char require_constant_value
;
8144 char require_constant_elements
;
8145 rich_location
*missing_brace_richloc
;
8148 static struct initializer_stack
*initializer_stack
;
8150 /* Prepare to parse and output the initializer for variable DECL. */
8153 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
,
8154 rich_location
*richloc
)
8157 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
8159 p
->decl
= constructor_decl
;
8160 p
->require_constant_value
= require_constant_value
;
8161 p
->require_constant_elements
= require_constant_elements
;
8162 p
->constructor_stack
= constructor_stack
;
8163 p
->constructor_range_stack
= constructor_range_stack
;
8164 p
->elements
= constructor_elements
;
8165 p
->spelling
= spelling
;
8166 p
->spelling_base
= spelling_base
;
8167 p
->spelling_size
= spelling_size
;
8168 p
->top_level
= constructor_top_level
;
8169 p
->next
= initializer_stack
;
8170 p
->missing_brace_richloc
= richloc
;
8171 initializer_stack
= p
;
8173 constructor_decl
= decl
;
8174 constructor_designated
= 0;
8175 constructor_top_level
= top_level
;
8177 if (decl
!= NULL_TREE
&& decl
!= error_mark_node
)
8179 require_constant_value
= TREE_STATIC (decl
);
8180 require_constant_elements
8181 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
8182 /* For a scalar, you can always use any value to initialize,
8183 even within braces. */
8184 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)));
8185 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
8189 require_constant_value
= 0;
8190 require_constant_elements
= 0;
8191 locus
= _("(anonymous)");
8194 constructor_stack
= 0;
8195 constructor_range_stack
= 0;
8197 found_missing_braces
= 0;
8201 RESTORE_SPELLING_DEPTH (0);
8204 push_string (locus
);
8210 struct initializer_stack
*p
= initializer_stack
;
8212 /* Free the whole constructor stack of this initializer. */
8213 while (constructor_stack
)
8215 struct constructor_stack
*q
= constructor_stack
;
8216 constructor_stack
= q
->next
;
8220 gcc_assert (!constructor_range_stack
);
8222 /* Pop back to the data of the outer initializer (if any). */
8223 free (spelling_base
);
8225 constructor_decl
= p
->decl
;
8226 require_constant_value
= p
->require_constant_value
;
8227 require_constant_elements
= p
->require_constant_elements
;
8228 constructor_stack
= p
->constructor_stack
;
8229 constructor_range_stack
= p
->constructor_range_stack
;
8230 constructor_elements
= p
->elements
;
8231 spelling
= p
->spelling
;
8232 spelling_base
= p
->spelling_base
;
8233 spelling_size
= p
->spelling_size
;
8234 constructor_top_level
= p
->top_level
;
8235 initializer_stack
= p
->next
;
8239 /* Call here when we see the initializer is surrounded by braces.
8240 This is instead of a call to push_init_level;
8241 it is matched by a call to pop_init_level.
8243 TYPE is the type to initialize, for a constructor expression.
8244 For an initializer for a decl, TYPE is zero. */
8247 really_start_incremental_init (tree type
)
8249 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
8251 if (type
== NULL_TREE
)
8252 type
= TREE_TYPE (constructor_decl
);
8254 if (VECTOR_TYPE_P (type
)
8255 && TYPE_VECTOR_OPAQUE (type
))
8256 error ("opaque vector types cannot be initialized");
8258 p
->type
= constructor_type
;
8259 p
->fields
= constructor_fields
;
8260 p
->index
= constructor_index
;
8261 p
->max_index
= constructor_max_index
;
8262 p
->unfilled_index
= constructor_unfilled_index
;
8263 p
->unfilled_fields
= constructor_unfilled_fields
;
8264 p
->bit_index
= constructor_bit_index
;
8265 p
->elements
= constructor_elements
;
8266 p
->constant
= constructor_constant
;
8267 p
->simple
= constructor_simple
;
8268 p
->nonconst
= constructor_nonconst
;
8269 p
->erroneous
= constructor_erroneous
;
8270 p
->pending_elts
= constructor_pending_elts
;
8271 p
->depth
= constructor_depth
;
8272 p
->replacement_value
.value
= 0;
8273 p
->replacement_value
.original_code
= ERROR_MARK
;
8274 p
->replacement_value
.original_type
= NULL
;
8278 p
->incremental
= constructor_incremental
;
8279 p
->designated
= constructor_designated
;
8280 p
->designator_depth
= designator_depth
;
8282 constructor_stack
= p
;
8284 constructor_constant
= 1;
8285 constructor_simple
= 1;
8286 constructor_nonconst
= 0;
8287 constructor_depth
= SPELLING_DEPTH ();
8288 constructor_elements
= NULL
;
8289 constructor_pending_elts
= 0;
8290 constructor_type
= type
;
8291 constructor_incremental
= 1;
8292 constructor_designated
= 0;
8293 constructor_zeroinit
= 1;
8294 designator_depth
= 0;
8295 designator_erroneous
= 0;
8297 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
8299 constructor_fields
= TYPE_FIELDS (constructor_type
);
8300 /* Skip any nameless bit fields at the beginning. */
8301 while (constructor_fields
!= NULL_TREE
8302 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
8303 constructor_fields
= DECL_CHAIN (constructor_fields
);
8305 constructor_unfilled_fields
= constructor_fields
;
8306 constructor_bit_index
= bitsize_zero_node
;
8308 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8310 if (TYPE_DOMAIN (constructor_type
))
8312 constructor_max_index
8313 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
8315 /* Detect non-empty initializations of zero-length arrays. */
8316 if (constructor_max_index
== NULL_TREE
8317 && TYPE_SIZE (constructor_type
))
8318 constructor_max_index
= integer_minus_one_node
;
8320 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8321 to initialize VLAs will cause a proper error; avoid tree
8322 checking errors as well by setting a safe value. */
8323 if (constructor_max_index
8324 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
8325 constructor_max_index
= integer_minus_one_node
;
8328 = convert (bitsizetype
,
8329 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8333 constructor_index
= bitsize_zero_node
;
8334 constructor_max_index
= NULL_TREE
;
8337 constructor_unfilled_index
= constructor_index
;
8339 else if (VECTOR_TYPE_P (constructor_type
))
8341 /* Vectors are like simple fixed-size arrays. */
8342 constructor_max_index
=
8343 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
8344 constructor_index
= bitsize_zero_node
;
8345 constructor_unfilled_index
= constructor_index
;
8349 /* Handle the case of int x = {5}; */
8350 constructor_fields
= constructor_type
;
8351 constructor_unfilled_fields
= constructor_type
;
8355 extern location_t last_init_list_comma
;
8357 /* Called when we see an open brace for a nested initializer. Finish
8358 off any pending levels with implicit braces. */
8360 finish_implicit_inits (location_t loc
, struct obstack
*braced_init_obstack
)
8362 while (constructor_stack
->implicit
)
8364 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
8365 && constructor_fields
== NULL_TREE
)
8366 process_init_element (input_location
,
8367 pop_init_level (loc
, 1, braced_init_obstack
,
8368 last_init_list_comma
),
8369 true, braced_init_obstack
);
8370 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8371 && constructor_max_index
8372 && tree_int_cst_lt (constructor_max_index
,
8374 process_init_element (input_location
,
8375 pop_init_level (loc
, 1, braced_init_obstack
,
8376 last_init_list_comma
),
8377 true, braced_init_obstack
);
8383 /* Push down into a subobject, for initialization.
8384 If this is for an explicit set of braces, IMPLICIT is 0.
8385 If it is because the next element belongs at a lower level,
8386 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8389 push_init_level (location_t loc
, int implicit
,
8390 struct obstack
*braced_init_obstack
)
8392 struct constructor_stack
*p
;
8393 tree value
= NULL_TREE
;
8395 /* Unless this is an explicit brace, we need to preserve previous
8399 if (RECORD_OR_UNION_TYPE_P (constructor_type
) && constructor_fields
)
8400 value
= find_init_member (constructor_fields
, braced_init_obstack
);
8401 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8402 value
= find_init_member (constructor_index
, braced_init_obstack
);
8405 p
= XNEW (struct constructor_stack
);
8406 p
->type
= constructor_type
;
8407 p
->fields
= constructor_fields
;
8408 p
->index
= constructor_index
;
8409 p
->max_index
= constructor_max_index
;
8410 p
->unfilled_index
= constructor_unfilled_index
;
8411 p
->unfilled_fields
= constructor_unfilled_fields
;
8412 p
->bit_index
= constructor_bit_index
;
8413 p
->elements
= constructor_elements
;
8414 p
->constant
= constructor_constant
;
8415 p
->simple
= constructor_simple
;
8416 p
->nonconst
= constructor_nonconst
;
8417 p
->erroneous
= constructor_erroneous
;
8418 p
->pending_elts
= constructor_pending_elts
;
8419 p
->depth
= constructor_depth
;
8420 p
->replacement_value
.value
= NULL_TREE
;
8421 p
->replacement_value
.original_code
= ERROR_MARK
;
8422 p
->replacement_value
.original_type
= NULL
;
8423 p
->implicit
= implicit
;
8425 p
->incremental
= constructor_incremental
;
8426 p
->designated
= constructor_designated
;
8427 p
->designator_depth
= designator_depth
;
8428 p
->next
= constructor_stack
;
8430 constructor_stack
= p
;
8432 constructor_constant
= 1;
8433 constructor_simple
= 1;
8434 constructor_nonconst
= 0;
8435 constructor_depth
= SPELLING_DEPTH ();
8436 constructor_elements
= NULL
;
8437 constructor_incremental
= 1;
8438 constructor_designated
= 0;
8439 constructor_pending_elts
= 0;
8442 p
->range_stack
= constructor_range_stack
;
8443 constructor_range_stack
= 0;
8444 designator_depth
= 0;
8445 designator_erroneous
= 0;
8448 /* Don't die if an entire brace-pair level is superfluous
8449 in the containing level. */
8450 if (constructor_type
== NULL_TREE
)
8452 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
8454 /* Don't die if there are extra init elts at the end. */
8455 if (constructor_fields
== NULL_TREE
)
8456 constructor_type
= NULL_TREE
;
8459 constructor_type
= TREE_TYPE (constructor_fields
);
8460 push_member_name (constructor_fields
);
8461 constructor_depth
++;
8463 /* If upper initializer is designated, then mark this as
8464 designated too to prevent bogus warnings. */
8465 constructor_designated
= p
->designated
;
8467 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8469 constructor_type
= TREE_TYPE (constructor_type
);
8470 push_array_bounds (tree_to_uhwi (constructor_index
));
8471 constructor_depth
++;
8474 if (constructor_type
== NULL_TREE
)
8476 error_init (loc
, "extra brace group at end of initializer");
8477 constructor_fields
= NULL_TREE
;
8478 constructor_unfilled_fields
= NULL_TREE
;
8482 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
8484 constructor_constant
= TREE_CONSTANT (value
);
8485 constructor_simple
= TREE_STATIC (value
);
8486 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
8487 constructor_elements
= CONSTRUCTOR_ELTS (value
);
8488 if (!vec_safe_is_empty (constructor_elements
)
8489 && (TREE_CODE (constructor_type
) == RECORD_TYPE
8490 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
8491 set_nonincremental_init (braced_init_obstack
);
8496 found_missing_braces
= 1;
8497 if (initializer_stack
->missing_brace_richloc
)
8498 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
8502 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
8504 constructor_fields
= TYPE_FIELDS (constructor_type
);
8505 /* Skip any nameless bit fields at the beginning. */
8506 while (constructor_fields
!= NULL_TREE
8507 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
8508 constructor_fields
= DECL_CHAIN (constructor_fields
);
8510 constructor_unfilled_fields
= constructor_fields
;
8511 constructor_bit_index
= bitsize_zero_node
;
8513 else if (VECTOR_TYPE_P (constructor_type
))
8515 /* Vectors are like simple fixed-size arrays. */
8516 constructor_max_index
=
8517 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
8518 constructor_index
= bitsize_int (0);
8519 constructor_unfilled_index
= constructor_index
;
8521 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8523 if (TYPE_DOMAIN (constructor_type
))
8525 constructor_max_index
8526 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
8528 /* Detect non-empty initializations of zero-length arrays. */
8529 if (constructor_max_index
== NULL_TREE
8530 && TYPE_SIZE (constructor_type
))
8531 constructor_max_index
= integer_minus_one_node
;
8533 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8534 to initialize VLAs will cause a proper error; avoid tree
8535 checking errors as well by setting a safe value. */
8536 if (constructor_max_index
8537 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
8538 constructor_max_index
= integer_minus_one_node
;
8541 = convert (bitsizetype
,
8542 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8545 constructor_index
= bitsize_zero_node
;
8547 constructor_unfilled_index
= constructor_index
;
8548 if (value
&& TREE_CODE (value
) == STRING_CST
)
8550 /* We need to split the char/wchar array into individual
8551 characters, so that we don't have to special case it
8553 set_nonincremental_init_from_string (value
, braced_init_obstack
);
8558 if (constructor_type
!= error_mark_node
)
8559 warning_init (input_location
, 0, "braces around scalar initializer");
8560 constructor_fields
= constructor_type
;
8561 constructor_unfilled_fields
= constructor_type
;
8565 /* At the end of an implicit or explicit brace level,
8566 finish up that level of constructor. If a single expression
8567 with redundant braces initialized that level, return the
8568 c_expr structure for that expression. Otherwise, the original_code
8569 element is set to ERROR_MARK.
8570 If we were outputting the elements as they are read, return 0 as the value
8571 from inner levels (process_init_element ignores that),
8572 but return error_mark_node as the value from the outermost level
8573 (that's what we want to put in DECL_INITIAL).
8574 Otherwise, return a CONSTRUCTOR expression as the value. */
8577 pop_init_level (location_t loc
, int implicit
,
8578 struct obstack
*braced_init_obstack
,
8579 location_t insert_before
)
8581 struct constructor_stack
*p
;
8583 ret
.value
= NULL_TREE
;
8584 ret
.original_code
= ERROR_MARK
;
8585 ret
.original_type
= NULL
;
8589 /* When we come to an explicit close brace,
8590 pop any inner levels that didn't have explicit braces. */
8591 while (constructor_stack
->implicit
)
8592 process_init_element (input_location
,
8593 pop_init_level (loc
, 1, braced_init_obstack
,
8595 true, braced_init_obstack
);
8596 gcc_assert (!constructor_range_stack
);
8599 if (initializer_stack
->missing_brace_richloc
)
8600 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
8601 (insert_before
, "}");
8603 /* Now output all pending elements. */
8604 constructor_incremental
= 1;
8605 output_pending_init_elements (1, braced_init_obstack
);
8607 p
= constructor_stack
;
8609 /* Error for initializing a flexible array member, or a zero-length
8610 array member in an inappropriate context. */
8611 if (constructor_type
&& constructor_fields
8612 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8613 && TYPE_DOMAIN (constructor_type
)
8614 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
8616 /* Silently discard empty initializations. The parser will
8617 already have pedwarned for empty brackets. */
8618 if (integer_zerop (constructor_unfilled_index
))
8619 constructor_type
= NULL_TREE
;
8622 gcc_assert (!TYPE_SIZE (constructor_type
));
8624 if (constructor_depth
> 2)
8625 error_init (loc
, "initialization of flexible array member in a nested context");
8627 pedwarn_init (loc
, OPT_Wpedantic
,
8628 "initialization of a flexible array member");
8630 /* We have already issued an error message for the existence
8631 of a flexible array member not at the end of the structure.
8632 Discard the initializer so that we do not die later. */
8633 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
8634 constructor_type
= NULL_TREE
;
8638 switch (vec_safe_length (constructor_elements
))
8641 /* Initialization with { } counts as zeroinit. */
8642 constructor_zeroinit
= 1;
8645 /* This might be zeroinit as well. */
8646 if (integer_zerop ((*constructor_elements
)[0].value
))
8647 constructor_zeroinit
= 1;
8650 /* If the constructor has more than one element, it can't be { 0 }. */
8651 constructor_zeroinit
= 0;
8655 /* Warn when some structs are initialized with direct aggregation. */
8656 if (!implicit
&& found_missing_braces
&& warn_missing_braces
8657 && !constructor_zeroinit
)
8659 gcc_assert (initializer_stack
->missing_brace_richloc
);
8660 warning_at (initializer_stack
->missing_brace_richloc
,
8661 OPT_Wmissing_braces
,
8662 "missing braces around initializer");
8665 /* Warn when some struct elements are implicitly initialized to zero. */
8666 if (warn_missing_field_initializers
8668 && TREE_CODE (constructor_type
) == RECORD_TYPE
8669 && constructor_unfilled_fields
)
8671 /* Do not warn for flexible array members or zero-length arrays. */
8672 while (constructor_unfilled_fields
8673 && (!DECL_SIZE (constructor_unfilled_fields
)
8674 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
8675 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
8677 if (constructor_unfilled_fields
8678 /* Do not warn if this level of the initializer uses member
8679 designators; it is likely to be deliberate. */
8680 && !constructor_designated
8681 /* Do not warn about initializing with { 0 } or with { }. */
8682 && !constructor_zeroinit
)
8684 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
8685 "missing initializer for field %qD of %qT",
8686 constructor_unfilled_fields
,
8688 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
8689 "%qD declared here", constructor_unfilled_fields
);
8693 /* Pad out the end of the structure. */
8694 if (p
->replacement_value
.value
)
8695 /* If this closes a superfluous brace pair,
8696 just pass out the element between them. */
8697 ret
= p
->replacement_value
;
8698 else if (constructor_type
== NULL_TREE
)
8700 else if (!RECORD_OR_UNION_TYPE_P (constructor_type
)
8701 && TREE_CODE (constructor_type
) != ARRAY_TYPE
8702 && !VECTOR_TYPE_P (constructor_type
))
8704 /* A nonincremental scalar initializer--just return
8705 the element, after verifying there is just one. */
8706 if (vec_safe_is_empty (constructor_elements
))
8708 if (!constructor_erroneous
)
8709 error_init (loc
, "empty scalar initializer");
8710 ret
.value
= error_mark_node
;
8712 else if (vec_safe_length (constructor_elements
) != 1)
8714 error_init (loc
, "extra elements in scalar initializer");
8715 ret
.value
= (*constructor_elements
)[0].value
;
8718 ret
.value
= (*constructor_elements
)[0].value
;
8722 if (constructor_erroneous
)
8723 ret
.value
= error_mark_node
;
8726 ret
.value
= build_constructor (constructor_type
,
8727 constructor_elements
);
8728 if (constructor_constant
)
8729 TREE_CONSTANT (ret
.value
) = 1;
8730 if (constructor_constant
&& constructor_simple
)
8731 TREE_STATIC (ret
.value
) = 1;
8732 if (constructor_nonconst
)
8733 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
8737 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
8739 if (constructor_nonconst
)
8740 ret
.original_code
= C_MAYBE_CONST_EXPR
;
8741 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
8742 ret
.original_code
= ERROR_MARK
;
8745 constructor_type
= p
->type
;
8746 constructor_fields
= p
->fields
;
8747 constructor_index
= p
->index
;
8748 constructor_max_index
= p
->max_index
;
8749 constructor_unfilled_index
= p
->unfilled_index
;
8750 constructor_unfilled_fields
= p
->unfilled_fields
;
8751 constructor_bit_index
= p
->bit_index
;
8752 constructor_elements
= p
->elements
;
8753 constructor_constant
= p
->constant
;
8754 constructor_simple
= p
->simple
;
8755 constructor_nonconst
= p
->nonconst
;
8756 constructor_erroneous
= p
->erroneous
;
8757 constructor_incremental
= p
->incremental
;
8758 constructor_designated
= p
->designated
;
8759 designator_depth
= p
->designator_depth
;
8760 constructor_pending_elts
= p
->pending_elts
;
8761 constructor_depth
= p
->depth
;
8763 constructor_range_stack
= p
->range_stack
;
8764 RESTORE_SPELLING_DEPTH (constructor_depth
);
8766 constructor_stack
= p
->next
;
8769 if (ret
.value
== NULL_TREE
&& constructor_stack
== 0)
8770 ret
.value
= error_mark_node
;
8774 /* Common handling for both array range and field name designators.
8775 ARRAY argument is nonzero for array ranges. Returns false for success. */
8778 set_designator (location_t loc
, bool array
,
8779 struct obstack
*braced_init_obstack
)
8782 enum tree_code subcode
;
8784 /* Don't die if an entire brace-pair level is superfluous
8785 in the containing level. */
8786 if (constructor_type
== NULL_TREE
)
8789 /* If there were errors in this designator list already, bail out
8791 if (designator_erroneous
)
8794 if (!designator_depth
)
8796 gcc_assert (!constructor_range_stack
);
8798 /* Designator list starts at the level of closest explicit
8800 while (constructor_stack
->implicit
)
8801 process_init_element (input_location
,
8802 pop_init_level (loc
, 1, braced_init_obstack
,
8803 last_init_list_comma
),
8804 true, braced_init_obstack
);
8805 constructor_designated
= 1;
8809 switch (TREE_CODE (constructor_type
))
8813 subtype
= TREE_TYPE (constructor_fields
);
8814 if (subtype
!= error_mark_node
)
8815 subtype
= TYPE_MAIN_VARIANT (subtype
);
8818 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8824 subcode
= TREE_CODE (subtype
);
8825 if (array
&& subcode
!= ARRAY_TYPE
)
8827 error_init (loc
, "array index in non-array initializer");
8830 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
8832 error_init (loc
, "field name not in record or union initializer");
8836 constructor_designated
= 1;
8837 finish_implicit_inits (loc
, braced_init_obstack
);
8838 push_init_level (loc
, 2, braced_init_obstack
);
8842 /* If there are range designators in designator list, push a new designator
8843 to constructor_range_stack. RANGE_END is end of such stack range or
8844 NULL_TREE if there is no range designator at this level. */
8847 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
8849 struct constructor_range_stack
*p
;
8851 p
= (struct constructor_range_stack
*)
8852 obstack_alloc (braced_init_obstack
,
8853 sizeof (struct constructor_range_stack
));
8854 p
->prev
= constructor_range_stack
;
8856 p
->fields
= constructor_fields
;
8857 p
->range_start
= constructor_index
;
8858 p
->index
= constructor_index
;
8859 p
->stack
= constructor_stack
;
8860 p
->range_end
= range_end
;
8861 if (constructor_range_stack
)
8862 constructor_range_stack
->next
= p
;
8863 constructor_range_stack
= p
;
8866 /* Within an array initializer, specify the next index to be initialized.
8867 FIRST is that index. If LAST is nonzero, then initialize a range
8868 of indices, running from FIRST through LAST. */
8871 set_init_index (location_t loc
, tree first
, tree last
,
8872 struct obstack
*braced_init_obstack
)
8874 if (set_designator (loc
, true, braced_init_obstack
))
8877 designator_erroneous
= 1;
8879 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
8880 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
8882 error_init (loc
, "array index in initializer not of integer type");
8886 if (TREE_CODE (first
) != INTEGER_CST
)
8888 first
= c_fully_fold (first
, false, NULL
);
8889 if (TREE_CODE (first
) == INTEGER_CST
)
8890 pedwarn_init (loc
, OPT_Wpedantic
,
8891 "array index in initializer is not "
8892 "an integer constant expression");
8895 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
8897 last
= c_fully_fold (last
, false, NULL
);
8898 if (TREE_CODE (last
) == INTEGER_CST
)
8899 pedwarn_init (loc
, OPT_Wpedantic
,
8900 "array index in initializer is not "
8901 "an integer constant expression");
8904 if (TREE_CODE (first
) != INTEGER_CST
)
8905 error_init (loc
, "nonconstant array index in initializer");
8906 else if (last
!= NULL_TREE
&& TREE_CODE (last
) != INTEGER_CST
)
8907 error_init (loc
, "nonconstant array index in initializer");
8908 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8909 error_init (loc
, "array index in non-array initializer");
8910 else if (tree_int_cst_sgn (first
) == -1)
8911 error_init (loc
, "array index in initializer exceeds array bounds");
8912 else if (constructor_max_index
8913 && tree_int_cst_lt (constructor_max_index
, first
))
8914 error_init (loc
, "array index in initializer exceeds array bounds");
8917 constant_expression_warning (first
);
8919 constant_expression_warning (last
);
8920 constructor_index
= convert (bitsizetype
, first
);
8921 if (tree_int_cst_lt (constructor_index
, first
))
8923 constructor_index
= copy_node (constructor_index
);
8924 TREE_OVERFLOW (constructor_index
) = 1;
8929 if (tree_int_cst_equal (first
, last
))
8931 else if (tree_int_cst_lt (last
, first
))
8933 error_init (loc
, "empty index range in initializer");
8938 last
= convert (bitsizetype
, last
);
8939 if (constructor_max_index
!= NULL_TREE
8940 && tree_int_cst_lt (constructor_max_index
, last
))
8942 error_init (loc
, "array index range in initializer exceeds "
8950 designator_erroneous
= 0;
8951 if (constructor_range_stack
|| last
)
8952 push_range_stack (last
, braced_init_obstack
);
8956 /* Within a struct initializer, specify the next field to be initialized. */
8959 set_init_label (location_t loc
, tree fieldname
, location_t fieldname_loc
,
8960 struct obstack
*braced_init_obstack
)
8964 if (set_designator (loc
, false, braced_init_obstack
))
8967 designator_erroneous
= 1;
8969 if (!RECORD_OR_UNION_TYPE_P (constructor_type
))
8971 error_init (loc
, "field name not in record or union initializer");
8975 field
= lookup_field (constructor_type
, fieldname
);
8977 if (field
== NULL_TREE
)
8979 tree guessed_id
= lookup_field_fuzzy (constructor_type
, fieldname
);
8982 gcc_rich_location
rich_loc (fieldname_loc
);
8983 rich_loc
.add_fixit_misspelled_id (fieldname_loc
, guessed_id
);
8984 error_at (&rich_loc
,
8985 "%qT has no member named %qE; did you mean %qE?",
8986 constructor_type
, fieldname
, guessed_id
);
8989 error_at (fieldname_loc
, "%qT has no member named %qE",
8990 constructor_type
, fieldname
);
8995 constructor_fields
= TREE_VALUE (field
);
8997 designator_erroneous
= 0;
8998 if (constructor_range_stack
)
8999 push_range_stack (NULL_TREE
, braced_init_obstack
);
9000 field
= TREE_CHAIN (field
);
9003 if (set_designator (loc
, false, braced_init_obstack
))
9007 while (field
!= NULL_TREE
);
9010 /* Add a new initializer to the tree of pending initializers. PURPOSE
9011 identifies the initializer, either array index or field in a structure.
9012 VALUE is the value of that index or field. If ORIGTYPE is not
9013 NULL_TREE, it is the original type of VALUE.
9015 IMPLICIT is true if value comes from pop_init_level (1),
9016 the new initializer has been merged with the existing one
9017 and thus no warnings should be emitted about overriding an
9018 existing initializer. */
9021 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
9022 bool implicit
, struct obstack
*braced_init_obstack
)
9024 struct init_node
*p
, **q
, *r
;
9026 q
= &constructor_pending_elts
;
9029 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9034 if (tree_int_cst_lt (purpose
, p
->purpose
))
9036 else if (tree_int_cst_lt (p
->purpose
, purpose
))
9042 if (TREE_SIDE_EFFECTS (p
->value
))
9043 warning_init (loc
, OPT_Woverride_init_side_effects
,
9044 "initialized field with side-effects "
9046 else if (warn_override_init
)
9047 warning_init (loc
, OPT_Woverride_init
,
9048 "initialized field overwritten");
9051 p
->origtype
= origtype
;
9060 bitpos
= bit_position (purpose
);
9064 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
9066 else if (p
->purpose
!= purpose
)
9072 if (TREE_SIDE_EFFECTS (p
->value
))
9073 warning_init (loc
, OPT_Woverride_init_side_effects
,
9074 "initialized field with side-effects "
9076 else if (warn_override_init
)
9077 warning_init (loc
, OPT_Woverride_init
,
9078 "initialized field overwritten");
9081 p
->origtype
= origtype
;
9087 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
9088 sizeof (struct init_node
));
9089 r
->purpose
= purpose
;
9091 r
->origtype
= origtype
;
9101 struct init_node
*s
;
9105 if (p
->balance
== 0)
9107 else if (p
->balance
< 0)
9114 p
->left
->parent
= p
;
9131 constructor_pending_elts
= r
;
9136 struct init_node
*t
= r
->right
;
9140 r
->right
->parent
= r
;
9145 p
->left
->parent
= p
;
9148 p
->balance
= t
->balance
< 0;
9149 r
->balance
= -(t
->balance
> 0);
9164 constructor_pending_elts
= t
;
9170 /* p->balance == +1; growth of left side balances the node. */
9175 else /* r == p->right */
9177 if (p
->balance
== 0)
9178 /* Growth propagation from right side. */
9180 else if (p
->balance
> 0)
9187 p
->right
->parent
= p
;
9204 constructor_pending_elts
= r
;
9206 else /* r->balance == -1 */
9209 struct init_node
*t
= r
->left
;
9213 r
->left
->parent
= r
;
9218 p
->right
->parent
= p
;
9221 r
->balance
= (t
->balance
< 0);
9222 p
->balance
= -(t
->balance
> 0);
9237 constructor_pending_elts
= t
;
9243 /* p->balance == -1; growth of right side balances the node. */
9254 /* Build AVL tree from a sorted chain. */
9257 set_nonincremental_init (struct obstack
* braced_init_obstack
)
9259 unsigned HOST_WIDE_INT ix
;
9262 if (TREE_CODE (constructor_type
) != RECORD_TYPE
9263 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
9266 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
9267 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
9268 braced_init_obstack
);
9269 constructor_elements
= NULL
;
9270 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9272 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
9273 /* Skip any nameless bit fields at the beginning. */
9274 while (constructor_unfilled_fields
!= NULL_TREE
9275 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields
))
9276 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
9279 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9281 if (TYPE_DOMAIN (constructor_type
))
9282 constructor_unfilled_index
9283 = convert (bitsizetype
,
9284 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
9286 constructor_unfilled_index
= bitsize_zero_node
;
9288 constructor_incremental
= 0;
9291 /* Build AVL tree from a string constant. */
9294 set_nonincremental_init_from_string (tree str
,
9295 struct obstack
* braced_init_obstack
)
9297 tree value
, purpose
, type
;
9298 HOST_WIDE_INT val
[2];
9299 const char *p
, *end
;
9300 int byte
, wchar_bytes
, charwidth
, bitpos
;
9302 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
9304 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
9305 charwidth
= TYPE_PRECISION (char_type_node
);
9306 gcc_assert ((size_t) wchar_bytes
* charwidth
9307 <= ARRAY_SIZE (val
) * HOST_BITS_PER_WIDE_INT
);
9308 type
= TREE_TYPE (constructor_type
);
9309 p
= TREE_STRING_POINTER (str
);
9310 end
= p
+ TREE_STRING_LENGTH (str
);
9312 for (purpose
= bitsize_zero_node
;
9314 && !(constructor_max_index
9315 && tree_int_cst_lt (constructor_max_index
, purpose
));
9316 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
9318 if (wchar_bytes
== 1)
9320 val
[0] = (unsigned char) *p
++;
9327 for (byte
= 0; byte
< wchar_bytes
; byte
++)
9329 if (BYTES_BIG_ENDIAN
)
9330 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
9332 bitpos
= byte
* charwidth
;
9333 val
[bitpos
/ HOST_BITS_PER_WIDE_INT
]
9334 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
9335 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
9339 if (!TYPE_UNSIGNED (type
))
9341 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
9342 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
9344 if (val
[0] & (HOST_WIDE_INT_1
<< (bitpos
- 1)))
9346 val
[0] |= HOST_WIDE_INT_M1U
<< bitpos
;
9350 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
9355 else if (val
[1] & (HOST_WIDE_INT_1
9356 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
9357 val
[1] |= HOST_WIDE_INT_M1U
<< (bitpos
- HOST_BITS_PER_WIDE_INT
);
9360 value
= wide_int_to_tree (type
,
9361 wide_int::from_array (val
, 2,
9362 HOST_BITS_PER_WIDE_INT
* 2));
9363 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
9364 braced_init_obstack
);
9367 constructor_incremental
= 0;
9370 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9371 not initialized yet. */
9374 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
9376 struct init_node
*p
;
9378 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9380 if (constructor_incremental
9381 && tree_int_cst_lt (field
, constructor_unfilled_index
))
9382 set_nonincremental_init (braced_init_obstack
);
9384 p
= constructor_pending_elts
;
9387 if (tree_int_cst_lt (field
, p
->purpose
))
9389 else if (tree_int_cst_lt (p
->purpose
, field
))
9395 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9397 tree bitpos
= bit_position (field
);
9399 if (constructor_incremental
9400 && (!constructor_unfilled_fields
9401 || tree_int_cst_lt (bitpos
,
9402 bit_position (constructor_unfilled_fields
))))
9403 set_nonincremental_init (braced_init_obstack
);
9405 p
= constructor_pending_elts
;
9408 if (field
== p
->purpose
)
9410 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
9416 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9418 if (!vec_safe_is_empty (constructor_elements
)
9419 && (constructor_elements
->last ().index
== field
))
9420 return constructor_elements
->last ().value
;
9425 /* "Output" the next constructor element.
9426 At top level, really output it to assembler code now.
9427 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9428 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9429 TYPE is the data type that the containing data type wants here.
9430 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9431 If VALUE is a string constant, STRICT_STRING is true if it is
9432 unparenthesized or we should not warn here for it being parenthesized.
9433 For other types of VALUE, STRICT_STRING is not used.
9435 PENDING if true means output pending elements that belong
9436 right after this element. (PENDING is normally true;
9437 it is false while outputting pending elements, to avoid recursion.)
9439 IMPLICIT is true if value comes from pop_init_level (1),
9440 the new initializer has been merged with the existing one
9441 and thus no warnings should be emitted about overriding an
9442 existing initializer. */
9445 output_init_element (location_t loc
, tree value
, tree origtype
,
9446 bool strict_string
, tree type
, tree field
, bool pending
,
9447 bool implicit
, struct obstack
* braced_init_obstack
)
9449 tree semantic_type
= NULL_TREE
;
9450 bool maybe_const
= true;
9453 if (type
== error_mark_node
|| value
== error_mark_node
)
9455 constructor_erroneous
= 1;
9458 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
9459 && (TREE_CODE (value
) == STRING_CST
9460 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
9461 && !(TREE_CODE (value
) == STRING_CST
9462 && TREE_CODE (type
) == ARRAY_TYPE
9463 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
9464 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
9465 TYPE_MAIN_VARIANT (type
)))
9466 value
= array_to_pointer_conversion (input_location
, value
);
9468 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
9469 && require_constant_value
&& pending
)
9471 /* As an extension, allow initializing objects with static storage
9472 duration with compound literals (which are then treated just as
9473 the brace enclosed list they contain). */
9475 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
9477 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
9478 value
= DECL_INITIAL (decl
);
9481 npc
= null_pointer_constant_p (value
);
9482 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
9484 semantic_type
= TREE_TYPE (value
);
9485 value
= TREE_OPERAND (value
, 0);
9487 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
9489 if (value
== error_mark_node
)
9490 constructor_erroneous
= 1;
9491 else if (!TREE_CONSTANT (value
))
9492 constructor_constant
= 0;
9493 else if (!initializer_constant_valid_p (value
,
9495 AGGREGATE_TYPE_P (constructor_type
)
9496 && TYPE_REVERSE_STORAGE_ORDER
9498 || (RECORD_OR_UNION_TYPE_P (constructor_type
)
9499 && DECL_C_BIT_FIELD (field
)
9500 && TREE_CODE (value
) != INTEGER_CST
))
9501 constructor_simple
= 0;
9503 constructor_nonconst
= 1;
9505 /* Digest the initializer and issue any errors about incompatible
9506 types before issuing errors about non-constant initializers. */
9507 tree new_value
= value
;
9509 new_value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
9510 new_value
= digest_init (loc
, type
, new_value
, origtype
, npc
, strict_string
,
9511 require_constant_value
);
9512 if (new_value
== error_mark_node
)
9514 constructor_erroneous
= 1;
9517 if (require_constant_value
|| require_constant_elements
)
9518 constant_expression_warning (new_value
);
9520 /* Proceed to check the constness of the original initializer. */
9521 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
9523 if (require_constant_value
)
9525 error_init (loc
, "initializer element is not constant");
9526 value
= error_mark_node
;
9528 else if (require_constant_elements
)
9529 pedwarn (loc
, OPT_Wpedantic
,
9530 "initializer element is not computable at load time");
9532 else if (!maybe_const
9533 && (require_constant_value
|| require_constant_elements
))
9534 pedwarn_init (loc
, OPT_Wpedantic
,
9535 "initializer element is not a constant expression");
9537 /* Issue -Wc++-compat warnings about initializing a bitfield with
9540 && field
!= NULL_TREE
9541 && TREE_CODE (field
) == FIELD_DECL
9542 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
9543 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
9544 != TYPE_MAIN_VARIANT (type
))
9545 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
9547 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
9548 if (checktype
!= error_mark_node
9549 && (TYPE_MAIN_VARIANT (checktype
)
9550 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
9551 warning_init (loc
, OPT_Wc___compat
,
9552 "enum conversion in initialization is invalid in C++");
9555 /* If this field is empty and does not have side effects (and is not at
9556 the end of structure), don't do anything other than checking the
9559 && (TREE_TYPE (field
) == error_mark_node
9560 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
9561 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
9562 && !TREE_SIDE_EFFECTS (new_value
)
9563 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
9564 || DECL_CHAIN (field
)))))
9567 /* Finally, set VALUE to the initializer value digested above. */
9570 /* If this element doesn't come next in sequence,
9571 put it on constructor_pending_elts. */
9572 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
9573 && (!constructor_incremental
9574 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
9576 if (constructor_incremental
9577 && tree_int_cst_lt (field
, constructor_unfilled_index
))
9578 set_nonincremental_init (braced_init_obstack
);
9580 add_pending_init (loc
, field
, value
, origtype
, implicit
,
9581 braced_init_obstack
);
9584 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
9585 && (!constructor_incremental
9586 || field
!= constructor_unfilled_fields
))
9588 /* We do this for records but not for unions. In a union,
9589 no matter which field is specified, it can be initialized
9590 right away since it starts at the beginning of the union. */
9591 if (constructor_incremental
)
9593 if (!constructor_unfilled_fields
)
9594 set_nonincremental_init (braced_init_obstack
);
9597 tree bitpos
, unfillpos
;
9599 bitpos
= bit_position (field
);
9600 unfillpos
= bit_position (constructor_unfilled_fields
);
9602 if (tree_int_cst_lt (bitpos
, unfillpos
))
9603 set_nonincremental_init (braced_init_obstack
);
9607 add_pending_init (loc
, field
, value
, origtype
, implicit
,
9608 braced_init_obstack
);
9611 else if (TREE_CODE (constructor_type
) == UNION_TYPE
9612 && !vec_safe_is_empty (constructor_elements
))
9616 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
9617 warning_init (loc
, OPT_Woverride_init_side_effects
,
9618 "initialized field with side-effects overwritten");
9619 else if (warn_override_init
)
9620 warning_init (loc
, OPT_Woverride_init
,
9621 "initialized field overwritten");
9624 /* We can have just one union field set. */
9625 constructor_elements
= NULL
;
9628 /* Otherwise, output this element either to
9629 constructor_elements or to the assembler file. */
9631 constructor_elt celt
= {field
, value
};
9632 vec_safe_push (constructor_elements
, celt
);
9634 /* Advance the variable that indicates sequential elements output. */
9635 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9636 constructor_unfilled_index
9637 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
9639 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9641 constructor_unfilled_fields
9642 = DECL_CHAIN (constructor_unfilled_fields
);
9644 /* Skip any nameless bit fields. */
9645 while (constructor_unfilled_fields
!= NULL_TREE
9646 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields
))
9647 constructor_unfilled_fields
=
9648 DECL_CHAIN (constructor_unfilled_fields
);
9650 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9651 constructor_unfilled_fields
= NULL_TREE
;
9653 /* Now output any pending elements which have become next. */
9655 output_pending_init_elements (0, braced_init_obstack
);
9658 /* For two FIELD_DECLs in the same chain, return -1 if field1
9659 comes before field2, 1 if field1 comes after field2 and
9660 0 if field1 == field2. */
9663 init_field_decl_cmp (tree field1
, tree field2
)
9665 if (field1
== field2
)
9668 tree bitpos1
= bit_position (field1
);
9669 tree bitpos2
= bit_position (field2
);
9670 if (tree_int_cst_equal (bitpos1
, bitpos2
))
9672 /* If one of the fields has non-zero bitsize, then that
9673 field must be the last one in a sequence of zero
9674 sized fields, fields after it will have bigger
9676 if (TREE_TYPE (field1
) != error_mark_node
9677 && COMPLETE_TYPE_P (TREE_TYPE (field1
))
9678 && integer_nonzerop (TREE_TYPE (field1
)))
9680 if (TREE_TYPE (field2
) != error_mark_node
9681 && COMPLETE_TYPE_P (TREE_TYPE (field2
))
9682 && integer_nonzerop (TREE_TYPE (field2
)))
9684 /* Otherwise, fallback to DECL_CHAIN walk to find out
9685 which field comes earlier. Walk chains of both
9686 fields, so that if field1 and field2 are close to each
9687 other in either order, it is found soon even for large
9688 sequences of zero sized fields. */
9689 tree f1
= field1
, f2
= field2
;
9692 f1
= DECL_CHAIN (f1
);
9693 f2
= DECL_CHAIN (f2
);
9694 if (f1
== NULL_TREE
)
9699 if (f2
== NULL_TREE
)
9705 if (!tree_int_cst_equal (bit_position (f1
), bitpos1
))
9707 if (!tree_int_cst_equal (bit_position (f2
), bitpos1
))
9711 else if (tree_int_cst_lt (bitpos1
, bitpos2
))
9717 /* Output any pending elements which have become next.
9718 As we output elements, constructor_unfilled_{fields,index}
9719 advances, which may cause other elements to become next;
9720 if so, they too are output.
9722 If ALL is 0, we return when there are
9723 no more pending elements to output now.
9725 If ALL is 1, we output space as necessary so that
9726 we can output all the pending elements. */
9728 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
9730 struct init_node
*elt
= constructor_pending_elts
;
9735 /* Look through the whole pending tree.
9736 If we find an element that should be output now,
9737 output it. Otherwise, set NEXT to the element
9738 that comes first among those still pending. */
9743 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9745 if (tree_int_cst_equal (elt
->purpose
,
9746 constructor_unfilled_index
))
9747 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9748 true, TREE_TYPE (constructor_type
),
9749 constructor_unfilled_index
, false, false,
9750 braced_init_obstack
);
9751 else if (tree_int_cst_lt (constructor_unfilled_index
,
9754 /* Advance to the next smaller node. */
9759 /* We have reached the smallest node bigger than the
9760 current unfilled index. Fill the space first. */
9761 next
= elt
->purpose
;
9767 /* Advance to the next bigger node. */
9772 /* We have reached the biggest node in a subtree. Find
9773 the parent of it, which is the next bigger node. */
9774 while (elt
->parent
&& elt
->parent
->right
== elt
)
9777 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
9780 next
= elt
->purpose
;
9786 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9788 /* If the current record is complete we are done. */
9789 if (constructor_unfilled_fields
== NULL_TREE
)
9792 int cmp
= init_field_decl_cmp (constructor_unfilled_fields
,
9795 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9796 true, TREE_TYPE (elt
->purpose
),
9797 elt
->purpose
, false, false,
9798 braced_init_obstack
);
9801 /* Advance to the next smaller node. */
9806 /* We have reached the smallest node bigger than the
9807 current unfilled field. Fill the space first. */
9808 next
= elt
->purpose
;
9814 /* Advance to the next bigger node. */
9819 /* We have reached the biggest node in a subtree. Find
9820 the parent of it, which is the next bigger node. */
9821 while (elt
->parent
&& elt
->parent
->right
== elt
)
9825 && init_field_decl_cmp (constructor_unfilled_fields
,
9828 next
= elt
->purpose
;
9836 /* Ordinarily return, but not if we want to output all
9837 and there are elements left. */
9838 if (!(all
&& next
!= NULL_TREE
))
9841 /* If it's not incremental, just skip over the gap, so that after
9842 jumping to retry we will output the next successive element. */
9843 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9844 constructor_unfilled_fields
= next
;
9845 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9846 constructor_unfilled_index
= next
;
9848 /* ELT now points to the node in the pending tree with the next
9849 initializer to output. */
9853 /* Add one non-braced element to the current constructor level.
9854 This adjusts the current position within the constructor's type.
9855 This may also start or terminate implicit levels
9856 to handle a partly-braced initializer.
9858 Once this has found the correct level for the new element,
9859 it calls output_init_element.
9861 IMPLICIT is true if value comes from pop_init_level (1),
9862 the new initializer has been merged with the existing one
9863 and thus no warnings should be emitted about overriding an
9864 existing initializer. */
9867 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
9868 struct obstack
* braced_init_obstack
)
9870 tree orig_value
= value
.value
;
9872 = (orig_value
!= NULL_TREE
&& TREE_CODE (orig_value
) == STRING_CST
);
9873 bool strict_string
= value
.original_code
== STRING_CST
;
9874 bool was_designated
= designator_depth
!= 0;
9876 designator_depth
= 0;
9877 designator_erroneous
= 0;
9879 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
9880 constructor_zeroinit
= 0;
9882 /* Handle superfluous braces around string cst as in
9883 char x[] = {"foo"}; */
9887 && TREE_CODE (constructor_type
) == ARRAY_TYPE
9888 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
9889 && integer_zerop (constructor_unfilled_index
))
9891 if (constructor_stack
->replacement_value
.value
)
9892 error_init (loc
, "excess elements in %<char%> array initializer");
9893 constructor_stack
->replacement_value
= value
;
9897 if (constructor_stack
->replacement_value
.value
!= NULL_TREE
)
9899 error_init (loc
, "excess elements in struct initializer");
9903 /* Ignore elements of a brace group if it is entirely superfluous
9904 and has already been diagnosed. */
9905 if (constructor_type
== NULL_TREE
)
9908 if (!implicit
&& warn_designated_init
&& !was_designated
9909 && TREE_CODE (constructor_type
) == RECORD_TYPE
9910 && lookup_attribute ("designated_init",
9911 TYPE_ATTRIBUTES (constructor_type
)))
9913 OPT_Wdesignated_init
,
9914 "positional initialization of field "
9915 "in %<struct%> declared with %<designated_init%> attribute");
9917 /* If we've exhausted any levels that didn't have braces,
9919 while (constructor_stack
->implicit
)
9921 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
9922 && constructor_fields
== NULL_TREE
)
9923 process_init_element (loc
,
9924 pop_init_level (loc
, 1, braced_init_obstack
,
9925 last_init_list_comma
),
9926 true, braced_init_obstack
);
9927 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
9928 || VECTOR_TYPE_P (constructor_type
))
9929 && constructor_max_index
9930 && tree_int_cst_lt (constructor_max_index
,
9932 process_init_element (loc
,
9933 pop_init_level (loc
, 1, braced_init_obstack
,
9934 last_init_list_comma
),
9935 true, braced_init_obstack
);
9940 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9941 if (constructor_range_stack
)
9943 /* If value is a compound literal and we'll be just using its
9944 content, don't put it into a SAVE_EXPR. */
9945 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
9946 || !require_constant_value
)
9948 tree semantic_type
= NULL_TREE
;
9949 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
9951 semantic_type
= TREE_TYPE (value
.value
);
9952 value
.value
= TREE_OPERAND (value
.value
, 0);
9954 value
.value
= save_expr (value
.value
);
9956 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
9963 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9966 enum tree_code fieldcode
;
9968 if (constructor_fields
== NULL_TREE
)
9970 pedwarn_init (loc
, 0, "excess elements in struct initializer");
9974 fieldtype
= TREE_TYPE (constructor_fields
);
9975 if (fieldtype
!= error_mark_node
)
9976 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9977 fieldcode
= TREE_CODE (fieldtype
);
9979 /* Error for non-static initialization of a flexible array member. */
9980 if (fieldcode
== ARRAY_TYPE
9981 && !require_constant_value
9982 && TYPE_SIZE (fieldtype
) == NULL_TREE
9983 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
9985 error_init (loc
, "non-static initialization of a flexible "
9990 /* Error for initialization of a flexible array member with
9991 a string constant if the structure is in an array. E.g.:
9992 struct S { int x; char y[]; };
9993 struct S s[] = { { 1, "foo" } };
9996 && fieldcode
== ARRAY_TYPE
9997 && constructor_depth
> 1
9998 && TYPE_SIZE (fieldtype
) == NULL_TREE
9999 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
10001 bool in_array_p
= false;
10002 for (struct constructor_stack
*p
= constructor_stack
;
10003 p
&& p
->type
; p
= p
->next
)
10004 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
10011 error_init (loc
, "initialization of flexible array "
10012 "member in a nested context");
10017 /* Accept a string constant to initialize a subarray. */
10018 if (value
.value
!= NULL_TREE
10019 && fieldcode
== ARRAY_TYPE
10020 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
10022 value
.value
= orig_value
;
10023 /* Otherwise, if we have come to a subaggregate,
10024 and we don't have an element of its type, push into it. */
10025 else if (value
.value
!= NULL_TREE
10026 && value
.value
!= error_mark_node
10027 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
10028 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
10029 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
10031 push_init_level (loc
, 1, braced_init_obstack
);
10037 push_member_name (constructor_fields
);
10038 output_init_element (loc
, value
.value
, value
.original_type
,
10039 strict_string
, fieldtype
,
10040 constructor_fields
, true, implicit
,
10041 braced_init_obstack
);
10042 RESTORE_SPELLING_DEPTH (constructor_depth
);
10045 /* Do the bookkeeping for an element that was
10046 directly output as a constructor. */
10048 /* For a record, keep track of end position of last field. */
10049 if (DECL_SIZE (constructor_fields
))
10050 constructor_bit_index
10051 = size_binop_loc (input_location
, PLUS_EXPR
,
10052 bit_position (constructor_fields
),
10053 DECL_SIZE (constructor_fields
));
10055 /* If the current field was the first one not yet written out,
10056 it isn't now, so update. */
10057 if (constructor_unfilled_fields
== constructor_fields
)
10059 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
10060 /* Skip any nameless bit fields. */
10061 while (constructor_unfilled_fields
!= 0
10062 && (DECL_UNNAMED_BIT_FIELD
10063 (constructor_unfilled_fields
)))
10064 constructor_unfilled_fields
=
10065 DECL_CHAIN (constructor_unfilled_fields
);
10069 constructor_fields
= DECL_CHAIN (constructor_fields
);
10070 /* Skip any nameless bit fields at the beginning. */
10071 while (constructor_fields
!= NULL_TREE
10072 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
10073 constructor_fields
= DECL_CHAIN (constructor_fields
);
10075 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
10078 enum tree_code fieldcode
;
10080 if (constructor_fields
== NULL_TREE
)
10082 pedwarn_init (loc
, 0,
10083 "excess elements in union initializer");
10087 fieldtype
= TREE_TYPE (constructor_fields
);
10088 if (fieldtype
!= error_mark_node
)
10089 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
10090 fieldcode
= TREE_CODE (fieldtype
);
10092 /* Warn that traditional C rejects initialization of unions.
10093 We skip the warning if the value is zero. This is done
10094 under the assumption that the zero initializer in user
10095 code appears conditioned on e.g. __STDC__ to avoid
10096 "missing initializer" warnings and relies on default
10097 initialization to zero in the traditional C case.
10098 We also skip the warning if the initializer is designated,
10099 again on the assumption that this must be conditional on
10100 __STDC__ anyway (and we've already complained about the
10101 member-designator already). */
10102 if (!in_system_header_at (input_location
) && !constructor_designated
10103 && !(value
.value
&& (integer_zerop (value
.value
)
10104 || real_zerop (value
.value
))))
10105 warning (OPT_Wtraditional
, "traditional C rejects initialization "
10108 /* Accept a string constant to initialize a subarray. */
10109 if (value
.value
!= NULL_TREE
10110 && fieldcode
== ARRAY_TYPE
10111 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
10113 value
.value
= orig_value
;
10114 /* Otherwise, if we have come to a subaggregate,
10115 and we don't have an element of its type, push into it. */
10116 else if (value
.value
!= NULL_TREE
10117 && value
.value
!= error_mark_node
10118 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
10119 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
10120 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
10122 push_init_level (loc
, 1, braced_init_obstack
);
10128 push_member_name (constructor_fields
);
10129 output_init_element (loc
, value
.value
, value
.original_type
,
10130 strict_string
, fieldtype
,
10131 constructor_fields
, true, implicit
,
10132 braced_init_obstack
);
10133 RESTORE_SPELLING_DEPTH (constructor_depth
);
10136 /* Do the bookkeeping for an element that was
10137 directly output as a constructor. */
10139 constructor_bit_index
= DECL_SIZE (constructor_fields
);
10140 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
10143 constructor_fields
= NULL_TREE
;
10145 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
10147 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
10148 enum tree_code eltcode
= TREE_CODE (elttype
);
10150 /* Accept a string constant to initialize a subarray. */
10151 if (value
.value
!= NULL_TREE
10152 && eltcode
== ARRAY_TYPE
10153 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
10155 value
.value
= orig_value
;
10156 /* Otherwise, if we have come to a subaggregate,
10157 and we don't have an element of its type, push into it. */
10158 else if (value
.value
!= NULL_TREE
10159 && value
.value
!= error_mark_node
10160 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
10161 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
10162 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
10164 push_init_level (loc
, 1, braced_init_obstack
);
10168 if (constructor_max_index
!= NULL_TREE
10169 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
10170 || integer_all_onesp (constructor_max_index
)))
10172 pedwarn_init (loc
, 0,
10173 "excess elements in array initializer");
10177 /* Now output the actual element. */
10180 push_array_bounds (tree_to_uhwi (constructor_index
));
10181 output_init_element (loc
, value
.value
, value
.original_type
,
10182 strict_string
, elttype
,
10183 constructor_index
, true, implicit
,
10184 braced_init_obstack
);
10185 RESTORE_SPELLING_DEPTH (constructor_depth
);
10189 = size_binop_loc (input_location
, PLUS_EXPR
,
10190 constructor_index
, bitsize_one_node
);
10193 /* If we are doing the bookkeeping for an element that was
10194 directly output as a constructor, we must update
10195 constructor_unfilled_index. */
10196 constructor_unfilled_index
= constructor_index
;
10198 else if (VECTOR_TYPE_P (constructor_type
))
10200 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
10202 /* Do a basic check of initializer size. Note that vectors
10203 always have a fixed size derived from their type. */
10204 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
10206 pedwarn_init (loc
, 0,
10207 "excess elements in vector initializer");
10211 /* Now output the actual element. */
10214 if (TREE_CODE (value
.value
) == VECTOR_CST
)
10215 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
10216 output_init_element (loc
, value
.value
, value
.original_type
,
10217 strict_string
, elttype
,
10218 constructor_index
, true, implicit
,
10219 braced_init_obstack
);
10223 = size_binop_loc (input_location
,
10224 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
10227 /* If we are doing the bookkeeping for an element that was
10228 directly output as a constructor, we must update
10229 constructor_unfilled_index. */
10230 constructor_unfilled_index
= constructor_index
;
10233 /* Handle the sole element allowed in a braced initializer
10234 for a scalar variable. */
10235 else if (constructor_type
!= error_mark_node
10236 && constructor_fields
== NULL_TREE
)
10238 pedwarn_init (loc
, 0,
10239 "excess elements in scalar initializer");
10245 output_init_element (loc
, value
.value
, value
.original_type
,
10246 strict_string
, constructor_type
,
10247 NULL_TREE
, true, implicit
,
10248 braced_init_obstack
);
10249 constructor_fields
= NULL_TREE
;
10252 /* Handle range initializers either at this level or anywhere higher
10253 in the designator stack. */
10254 if (constructor_range_stack
)
10256 struct constructor_range_stack
*p
, *range_stack
;
10259 range_stack
= constructor_range_stack
;
10260 constructor_range_stack
= 0;
10261 while (constructor_stack
!= range_stack
->stack
)
10263 gcc_assert (constructor_stack
->implicit
);
10264 process_init_element (loc
,
10265 pop_init_level (loc
, 1,
10266 braced_init_obstack
,
10267 last_init_list_comma
),
10268 true, braced_init_obstack
);
10270 for (p
= range_stack
;
10271 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
10274 gcc_assert (constructor_stack
->implicit
);
10275 process_init_element (loc
,
10276 pop_init_level (loc
, 1,
10277 braced_init_obstack
,
10278 last_init_list_comma
),
10279 true, braced_init_obstack
);
10282 p
->index
= size_binop_loc (input_location
,
10283 PLUS_EXPR
, p
->index
, bitsize_one_node
);
10284 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
10289 constructor_index
= p
->index
;
10290 constructor_fields
= p
->fields
;
10291 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
10299 finish_implicit_inits (loc
, braced_init_obstack
);
10300 push_init_level (loc
, 2, braced_init_obstack
);
10301 p
->stack
= constructor_stack
;
10302 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
10303 p
->index
= p
->range_start
;
10307 constructor_range_stack
= range_stack
;
10314 constructor_range_stack
= 0;
10317 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10318 (guaranteed to be 'volatile' or null) and ARGS (represented using
10319 an ASM_EXPR node). */
10321 build_asm_stmt (bool is_volatile
, tree args
)
10324 ASM_VOLATILE_P (args
) = 1;
10325 return add_stmt (args
);
10328 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10329 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10330 SIMPLE indicates whether there was anything at all after the
10331 string in the asm expression -- asm("blah") and asm("blah" : )
10332 are subtly different. We use a ASM_EXPR node to represent this.
10333 LOC is the location of the asm, and IS_INLINE says whether this
10336 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
10337 tree clobbers
, tree labels
, bool simple
, bool is_inline
)
10342 const char *constraint
;
10343 const char **oconstraints
;
10344 bool allows_mem
, allows_reg
, is_inout
;
10345 int ninputs
, noutputs
;
10347 ninputs
= list_length (inputs
);
10348 noutputs
= list_length (outputs
);
10349 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
10351 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
10353 /* Remove output conversions that change the type but not the mode. */
10354 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
10356 tree output
= TREE_VALUE (tail
);
10358 output
= c_fully_fold (output
, false, NULL
, true);
10360 /* ??? Really, this should not be here. Users should be using a
10361 proper lvalue, dammit. But there's a long history of using casts
10362 in the output operands. In cases like longlong.h, this becomes a
10363 primitive form of typechecking -- if the cast can be removed, then
10364 the output operand had a type of the proper width; otherwise we'll
10365 get an error. Gross, but ... */
10366 STRIP_NOPS (output
);
10368 if (!lvalue_or_else (loc
, output
, lv_asm
))
10369 output
= error_mark_node
;
10371 if (output
!= error_mark_node
10372 && (TREE_READONLY (output
)
10373 || TYPE_READONLY (TREE_TYPE (output
))
10374 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output
))
10375 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
10376 readonly_error (loc
, output
, lv_asm
);
10378 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
10379 oconstraints
[i
] = constraint
;
10381 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
10382 &allows_mem
, &allows_reg
, &is_inout
))
10384 /* If the operand is going to end up in memory,
10385 mark it addressable. */
10386 if (!allows_reg
&& !c_mark_addressable (output
))
10387 output
= error_mark_node
;
10388 if (!(!allows_reg
&& allows_mem
)
10389 && output
!= error_mark_node
10390 && VOID_TYPE_P (TREE_TYPE (output
)))
10392 error_at (loc
, "invalid use of void expression");
10393 output
= error_mark_node
;
10397 output
= error_mark_node
;
10399 TREE_VALUE (tail
) = output
;
10402 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
10406 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
10407 input
= TREE_VALUE (tail
);
10409 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
10410 oconstraints
, &allows_mem
, &allows_reg
))
10412 /* If the operand is going to end up in memory,
10413 mark it addressable. */
10414 if (!allows_reg
&& allows_mem
)
10416 input
= c_fully_fold (input
, false, NULL
, true);
10418 /* Strip the nops as we allow this case. FIXME, this really
10419 should be rejected or made deprecated. */
10420 STRIP_NOPS (input
);
10421 if (!c_mark_addressable (input
))
10422 input
= error_mark_node
;
10426 struct c_expr expr
;
10427 memset (&expr
, 0, sizeof (expr
));
10428 expr
.value
= input
;
10429 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
10430 input
= c_fully_fold (expr
.value
, false, NULL
);
10432 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
10434 error_at (loc
, "invalid use of void expression");
10435 input
= error_mark_node
;
10440 input
= error_mark_node
;
10442 TREE_VALUE (tail
) = input
;
10445 /* ASMs with labels cannot have outputs. This should have been
10446 enforced by the parser. */
10447 gcc_assert (outputs
== NULL
|| labels
== NULL
);
10449 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
10451 /* asm statements without outputs, including simple ones, are treated
10453 ASM_INPUT_P (args
) = simple
;
10454 ASM_VOLATILE_P (args
) = (noutputs
== 0);
10455 ASM_INLINE_P (args
) = is_inline
;
10460 /* Generate a goto statement to LABEL. LOC is the location of the
10464 c_finish_goto_label (location_t loc
, tree label
)
10466 tree decl
= lookup_label_for_goto (loc
, label
);
10469 TREE_USED (decl
) = 1;
10471 add_stmt (build_predict_expr (PRED_GOTO
, NOT_TAKEN
));
10472 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
10473 SET_EXPR_LOCATION (t
, loc
);
10474 return add_stmt (t
);
10478 /* Generate a computed goto statement to EXPR. LOC is the location of
10482 c_finish_goto_ptr (location_t loc
, tree expr
)
10485 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
10486 expr
= c_fully_fold (expr
, false, NULL
);
10487 expr
= convert (ptr_type_node
, expr
);
10488 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
10489 SET_EXPR_LOCATION (t
, loc
);
10490 return add_stmt (t
);
10493 /* Generate a C `return' statement. RETVAL is the expression for what
10494 to return, or a null pointer for `return;' with no value. LOC is
10495 the location of the return statement, or the location of the expression,
10496 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10497 is the original type of RETVAL. */
10500 c_finish_return (location_t loc
, tree retval
, tree origtype
)
10502 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
10503 bool no_warning
= false;
10506 /* Use the expansion point to handle cases such as returning NULL
10507 in a function returning void. */
10508 location_t xloc
= expansion_point_location_if_in_system_header (loc
);
10510 if (TREE_THIS_VOLATILE (current_function_decl
))
10511 warning_at (xloc
, 0,
10512 "function declared %<noreturn%> has a %<return%> statement");
10516 tree semantic_type
= NULL_TREE
;
10517 npc
= null_pointer_constant_p (retval
);
10518 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
10520 semantic_type
= TREE_TYPE (retval
);
10521 retval
= TREE_OPERAND (retval
, 0);
10523 retval
= c_fully_fold (retval
, false, NULL
);
10525 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
10530 current_function_returns_null
= 1;
10531 if ((warn_return_type
>= 0 || flag_isoc99
)
10532 && valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
10536 warned_here
= pedwarn
10537 (loc
, warn_return_type
>= 0 ? OPT_Wreturn_type
: 0,
10538 "%<return%> with no value, in function returning non-void");
10540 warned_here
= warning_at
10541 (loc
, OPT_Wreturn_type
,
10542 "%<return%> with no value, in function returning non-void");
10545 inform (DECL_SOURCE_LOCATION (current_function_decl
),
10549 else if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
10551 current_function_returns_null
= 1;
10553 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
10554 warned_here
= pedwarn
10555 (xloc
, warn_return_type
>= 0 ? OPT_Wreturn_type
: 0,
10556 "%<return%> with a value, in function returning void");
10558 warned_here
= pedwarn
10559 (xloc
, OPT_Wpedantic
, "ISO C forbids "
10560 "%<return%> with expression, in function returning void");
10562 inform (DECL_SOURCE_LOCATION (current_function_decl
),
10567 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
10568 retval
, origtype
, ic_return
,
10569 npc
, NULL_TREE
, NULL_TREE
, 0);
10570 tree res
= DECL_RESULT (current_function_decl
);
10574 current_function_returns_value
= 1;
10575 if (t
== error_mark_node
)
10578 save
= in_late_binary_op
;
10579 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
10580 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
10581 || (TREE_CODE (TREE_TYPE (t
)) == REAL_TYPE
10582 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
10583 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
10584 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
10585 in_late_binary_op
= true;
10586 inner
= t
= convert (TREE_TYPE (res
), t
);
10587 in_late_binary_op
= save
;
10589 /* Strip any conversions, additions, and subtractions, and see if
10590 we are returning the address of a local variable. Warn if so. */
10593 switch (TREE_CODE (inner
))
10596 case NON_LVALUE_EXPR
:
10598 case POINTER_PLUS_EXPR
:
10599 inner
= TREE_OPERAND (inner
, 0);
10603 /* If the second operand of the MINUS_EXPR has a pointer
10604 type (or is converted from it), this may be valid, so
10605 don't give a warning. */
10607 tree op1
= TREE_OPERAND (inner
, 1);
10609 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
10610 && (CONVERT_EXPR_P (op1
)
10611 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
10612 op1
= TREE_OPERAND (op1
, 0);
10614 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
10617 inner
= TREE_OPERAND (inner
, 0);
10622 inner
= TREE_OPERAND (inner
, 0);
10624 while (REFERENCE_CLASS_P (inner
)
10625 && !INDIRECT_REF_P (inner
))
10626 inner
= TREE_OPERAND (inner
, 0);
10629 && !DECL_EXTERNAL (inner
)
10630 && !TREE_STATIC (inner
)
10631 && DECL_CONTEXT (inner
) == current_function_decl
10632 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
))))
10634 if (TREE_CODE (inner
) == LABEL_DECL
)
10635 warning_at (loc
, OPT_Wreturn_local_addr
,
10636 "function returns address of label");
10639 warning_at (loc
, OPT_Wreturn_local_addr
,
10640 "function returns address of local variable");
10641 tree zero
= build_zero_cst (TREE_TYPE (res
));
10642 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
10654 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
10655 SET_EXPR_LOCATION (retval
, loc
);
10657 if (warn_sequence_point
)
10658 verify_sequence_points (retval
);
10661 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
10662 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
10663 return add_stmt (ret_stmt
);
10667 /* The SWITCH_EXPR being built. */
10670 /* The original type of the testing expression, i.e. before the
10671 default conversion is applied. */
10674 /* A splay-tree mapping the low element of a case range to the high
10675 element, or NULL_TREE if there is no high element. Used to
10676 determine whether or not a new case label duplicates an old case
10677 label. We need a tree, rather than simply a hash table, because
10678 of the GNU case range extension. */
10681 /* The bindings at the point of the switch. This is used for
10682 warnings crossing decls when branching to a case label. */
10683 struct c_spot_bindings
*bindings
;
10685 /* The next node on the stack. */
10686 struct c_switch
*next
;
10688 /* Remember whether the controlling expression had boolean type
10689 before integer promotions for the sake of -Wswitch-bool. */
10693 /* A stack of the currently active switch statements. The innermost
10694 switch statement is on the top of the stack. There is no need to
10695 mark the stack for garbage collection because it is only active
10696 during the processing of the body of a function, and we never
10697 collect at that point. */
10699 struct c_switch
*c_switch_stack
;
10701 /* Start a C switch statement, testing expression EXP. Return the new
10702 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10703 SWITCH_COND_LOC is the location of the switch's condition.
10704 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10707 c_start_case (location_t switch_loc
,
10708 location_t switch_cond_loc
,
10709 tree exp
, bool explicit_cast_p
)
10711 tree orig_type
= error_mark_node
;
10712 bool bool_cond_p
= false;
10713 struct c_switch
*cs
;
10715 if (exp
!= error_mark_node
)
10717 orig_type
= TREE_TYPE (exp
);
10719 if (!INTEGRAL_TYPE_P (orig_type
))
10721 if (orig_type
!= error_mark_node
)
10723 error_at (switch_cond_loc
, "switch quantity not an integer");
10724 orig_type
= error_mark_node
;
10726 exp
= integer_zero_node
;
10730 tree type
= TYPE_MAIN_VARIANT (orig_type
);
10733 /* Warn if the condition has boolean value. */
10734 while (TREE_CODE (e
) == COMPOUND_EXPR
)
10735 e
= TREE_OPERAND (e
, 1);
10737 if ((TREE_CODE (type
) == BOOLEAN_TYPE
10738 || truth_value_p (TREE_CODE (e
)))
10739 /* Explicit cast to int suppresses this warning. */
10740 && !(TREE_CODE (type
) == INTEGER_TYPE
10741 && explicit_cast_p
))
10742 bool_cond_p
= true;
10744 if (!in_system_header_at (input_location
)
10745 && (type
== long_integer_type_node
10746 || type
== long_unsigned_type_node
))
10747 warning_at (switch_cond_loc
,
10748 OPT_Wtraditional
, "%<long%> switch expression not "
10749 "converted to %<int%> in ISO C");
10751 exp
= c_fully_fold (exp
, false, NULL
);
10752 exp
= default_conversion (exp
);
10754 if (warn_sequence_point
)
10755 verify_sequence_points (exp
);
10759 /* Add this new SWITCH_EXPR to the stack. */
10760 cs
= XNEW (struct c_switch
);
10761 cs
->switch_expr
= build2 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
);
10762 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
10763 cs
->orig_type
= orig_type
;
10764 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
10765 cs
->bindings
= c_get_switch_bindings ();
10766 cs
->bool_cond_p
= bool_cond_p
;
10767 cs
->next
= c_switch_stack
;
10768 c_switch_stack
= cs
;
10770 return add_stmt (cs
->switch_expr
);
10773 /* Process a case label at location LOC. */
10776 do_case (location_t loc
, tree low_value
, tree high_value
)
10778 tree label
= NULL_TREE
;
10780 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
10782 low_value
= c_fully_fold (low_value
, false, NULL
);
10783 if (TREE_CODE (low_value
) == INTEGER_CST
)
10784 pedwarn (loc
, OPT_Wpedantic
,
10785 "case label is not an integer constant expression");
10788 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
10790 high_value
= c_fully_fold (high_value
, false, NULL
);
10791 if (TREE_CODE (high_value
) == INTEGER_CST
)
10792 pedwarn (input_location
, OPT_Wpedantic
,
10793 "case label is not an integer constant expression");
10796 if (c_switch_stack
== NULL
)
10799 error_at (loc
, "case label not within a switch statement");
10801 error_at (loc
, "%<default%> label not within a switch statement");
10805 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
10806 EXPR_LOCATION (c_switch_stack
->switch_expr
),
10810 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
10811 SWITCH_COND (c_switch_stack
->switch_expr
),
10812 low_value
, high_value
);
10813 if (label
== error_mark_node
)
10818 /* Finish the switch statement. TYPE is the original type of the
10819 controlling expression of the switch, or NULL_TREE. */
10822 c_finish_case (tree body
, tree type
)
10824 struct c_switch
*cs
= c_switch_stack
;
10825 location_t switch_location
;
10827 SWITCH_BODY (cs
->switch_expr
) = body
;
10829 /* Emit warnings as needed. */
10830 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
10831 c_do_switch_warnings (cs
->cases
, switch_location
,
10832 type
? type
: TREE_TYPE (cs
->switch_expr
),
10833 SWITCH_COND (cs
->switch_expr
), cs
->bool_cond_p
);
10834 if (c_switch_covers_all_cases_p (cs
->cases
, TREE_TYPE (cs
->switch_expr
)))
10835 SWITCH_ALL_CASES_P (cs
->switch_expr
) = 1;
10837 /* Pop the stack. */
10838 c_switch_stack
= cs
->next
;
10839 splay_tree_delete (cs
->cases
);
10840 c_release_switch_bindings (cs
->bindings
);
10844 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10845 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10849 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
10854 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
10855 SET_EXPR_LOCATION (stmt
, if_locus
);
10859 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10860 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10861 is false for DO loops. INCR is the FOR increment expression. BODY is
10862 the statement controlled by the loop. BLAB is the break label. CLAB is
10863 the continue label. Everything is allowed to be NULL.
10864 COND_LOCUS is the location of the loop condition, INCR_LOCUS is the
10865 location of the FOR increment expression. */
10868 c_finish_loop (location_t start_locus
, location_t cond_locus
, tree cond
,
10869 location_t incr_locus
, tree incr
, tree body
, tree blab
,
10870 tree clab
, bool cond_is_first
)
10872 tree entry
= NULL
, exit
= NULL
, t
;
10874 /* If the condition is zero don't generate a loop construct. */
10875 if (cond
&& integer_zerop (cond
))
10879 t
= build_and_jump (&blab
);
10880 SET_EXPR_LOCATION (t
, start_locus
);
10886 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10888 /* If we have an exit condition, then we build an IF with gotos either
10889 out of the loop, or to the top of it. If there's no exit condition,
10890 then we just build a jump back to the top. */
10891 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
10893 if (cond
&& !integer_nonzerop (cond
))
10895 /* Canonicalize the loop condition to the end. This means
10896 generating a branch to the loop condition. Reuse the
10897 continue label, if possible. */
10902 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10903 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
10906 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
10907 SET_EXPR_LOCATION (t
, start_locus
);
10911 t
= build_and_jump (&blab
);
10912 exit
= fold_build3_loc (cond_is_first
? start_locus
: input_location
,
10913 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10917 /* For the backward-goto's location of an unconditional loop
10918 use the beginning of the body, or, if there is none, the
10919 top of the loop. */
10920 location_t loc
= EXPR_LOCATION (expr_first (body
));
10921 if (loc
== UNKNOWN_LOCATION
)
10923 SET_EXPR_LOCATION (exit
, loc
);
10932 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
10935 if (MAY_HAVE_DEBUG_MARKER_STMTS
&& incr_locus
!= UNKNOWN_LOCATION
)
10937 t
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10938 SET_EXPR_LOCATION (t
, incr_locus
);
10945 if (MAY_HAVE_DEBUG_MARKER_STMTS
&& cond_locus
!= UNKNOWN_LOCATION
)
10947 t
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10948 SET_EXPR_LOCATION (t
, cond_locus
);
10954 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
10958 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
10961 tree label
= *label_p
;
10963 /* In switch statements break is sometimes stylistically used after
10964 a return statement. This can lead to spurious warnings about
10965 control reaching the end of a non-void function when it is
10966 inlined. Note that we are calling block_may_fallthru with
10967 language specific tree nodes; this works because
10968 block_may_fallthru returns true when given something it does not
10970 skip
= !block_may_fallthru (cur_stmt_list
);
10975 *label_p
= label
= create_artificial_label (loc
);
10977 else if (TREE_CODE (label
) == LABEL_DECL
)
10979 else switch (TREE_INT_CST_LOW (label
))
10983 error_at (loc
, "break statement not within loop or switch");
10985 error_at (loc
, "continue statement not within a loop");
10989 gcc_assert (is_break
);
10990 error_at (loc
, "break statement used with OpenMP for loop");
10995 error ("break statement within %<#pragma simd%> loop body");
10997 error ("continue statement within %<#pragma simd%> loop body");
11001 gcc_unreachable ();
11008 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
11010 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
11013 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
11016 emit_side_effect_warnings (location_t loc
, tree expr
)
11018 if (expr
== error_mark_node
)
11020 else if (!TREE_SIDE_EFFECTS (expr
))
11022 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
11023 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
11025 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
11028 location_t cloc
= loc
;
11029 while (TREE_CODE (r
) == COMPOUND_EXPR
)
11031 if (EXPR_HAS_LOCATION (r
))
11032 cloc
= EXPR_LOCATION (r
);
11033 r
= TREE_OPERAND (r
, 1);
11035 if (!TREE_SIDE_EFFECTS (r
)
11036 && !VOID_TYPE_P (TREE_TYPE (r
))
11037 && !CONVERT_EXPR_P (r
)
11038 && !TREE_NO_WARNING (r
)
11039 && !TREE_NO_WARNING (expr
))
11040 warning_at (cloc
, OPT_Wunused_value
,
11041 "right-hand operand of comma expression has no effect");
11044 warn_if_unused_value (expr
, loc
);
11047 /* Process an expression as if it were a complete statement. Emit
11048 diagnostics, but do not call ADD_STMT. LOC is the location of the
11052 c_process_expr_stmt (location_t loc
, tree expr
)
11059 expr
= c_fully_fold (expr
, false, NULL
);
11061 if (warn_sequence_point
)
11062 verify_sequence_points (expr
);
11064 if (TREE_TYPE (expr
) != error_mark_node
11065 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
11066 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
11067 error_at (loc
, "expression statement has incomplete type");
11069 /* If we're not processing a statement expression, warn about unused values.
11070 Warnings for statement expressions will be emitted later, once we figure
11071 out which is the result. */
11072 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
11073 && warn_unused_value
)
11074 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr
, loc
), expr
);
11077 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
11078 exprv
= TREE_OPERAND (exprv
, 1);
11079 while (CONVERT_EXPR_P (exprv
))
11080 exprv
= TREE_OPERAND (exprv
, 0);
11082 || handled_component_p (exprv
)
11083 || TREE_CODE (exprv
) == ADDR_EXPR
)
11084 mark_exp_read (exprv
);
11086 /* If the expression is not of a type to which we cannot assign a line
11087 number, wrap the thing in a no-op NOP_EXPR. */
11088 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
11090 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11091 SET_EXPR_LOCATION (expr
, loc
);
11097 /* Emit an expression as a statement. LOC is the location of the
11101 c_finish_expr_stmt (location_t loc
, tree expr
)
11104 return add_stmt (c_process_expr_stmt (loc
, expr
));
11109 /* Do the opposite and emit a statement as an expression. To begin,
11110 create a new binding level and return it. */
11113 c_begin_stmt_expr (void)
11117 /* We must force a BLOCK for this level so that, if it is not expanded
11118 later, there is a way to turn off the entire subtree of blocks that
11119 are contained in it. */
11120 keep_next_level ();
11121 ret
= c_begin_compound_stmt (true);
11123 c_bindings_start_stmt_expr (c_switch_stack
== NULL
11125 : c_switch_stack
->bindings
);
11127 /* Mark the current statement list as belonging to a statement list. */
11128 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
11133 /* LOC is the location of the compound statement to which this body
11137 c_finish_stmt_expr (location_t loc
, tree body
)
11139 tree last
, type
, tmp
, val
;
11142 body
= c_end_compound_stmt (loc
, body
, true);
11144 c_bindings_end_stmt_expr (c_switch_stack
== NULL
11146 : c_switch_stack
->bindings
);
11148 /* Locate the last statement in BODY. See c_end_compound_stmt
11149 about always returning a BIND_EXPR. */
11150 last_p
= &BIND_EXPR_BODY (body
);
11151 last
= BIND_EXPR_BODY (body
);
11153 continue_searching
:
11154 if (TREE_CODE (last
) == STATEMENT_LIST
)
11156 tree_stmt_iterator l
= tsi_last (last
);
11158 while (!tsi_end_p (l
) && TREE_CODE (tsi_stmt (l
)) == DEBUG_BEGIN_STMT
)
11161 /* This can happen with degenerate cases like ({ }). No value. */
11165 /* If we're supposed to generate side effects warnings, process
11166 all of the statements except the last. */
11167 if (warn_unused_value
)
11169 for (tree_stmt_iterator i
= tsi_start (last
);
11170 tsi_stmt (i
) != tsi_stmt (l
); tsi_next (&i
))
11173 tree t
= tsi_stmt (i
);
11175 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
11176 emit_side_effect_warnings (tloc
, t
);
11179 last_p
= tsi_stmt_ptr (l
);
11183 /* If the end of the list is exception related, then the list was split
11184 by a call to push_cleanup. Continue searching. */
11185 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
11186 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
11188 last_p
= &TREE_OPERAND (last
, 0);
11190 goto continue_searching
;
11193 if (last
== error_mark_node
)
11196 /* In the case that the BIND_EXPR is not necessary, return the
11197 expression out from inside it. */
11198 if ((last
== BIND_EXPR_BODY (body
)
11199 /* Skip nested debug stmts. */
11200 || last
== expr_first (BIND_EXPR_BODY (body
)))
11201 && BIND_EXPR_VARS (body
) == NULL
)
11203 /* Even if this looks constant, do not allow it in a constant
11205 last
= c_wrap_maybe_const (last
, true);
11206 /* Do not warn if the return value of a statement expression is
11208 TREE_NO_WARNING (last
) = 1;
11212 /* Extract the type of said expression. */
11213 type
= TREE_TYPE (last
);
11215 /* If we're not returning a value at all, then the BIND_EXPR that
11216 we already have is a fine expression to return. */
11217 if (!type
|| VOID_TYPE_P (type
))
11220 /* Now that we've located the expression containing the value, it seems
11221 silly to make voidify_wrapper_expr repeat the process. Create a
11222 temporary of the appropriate type and stick it in a TARGET_EXPR. */
11223 tmp
= create_tmp_var_raw (type
);
11225 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
11226 tree_expr_nonnegative_p giving up immediately. */
11228 if (TREE_CODE (val
) == NOP_EXPR
11229 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
11230 val
= TREE_OPERAND (val
, 0);
11232 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
11233 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
11236 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
11237 SET_EXPR_LOCATION (t
, loc
);
11242 /* Begin and end compound statements. This is as simple as pushing
11243 and popping new statement lists from the tree. */
11246 c_begin_compound_stmt (bool do_scope
)
11248 tree stmt
= push_stmt_list ();
11254 /* End a compound statement. STMT is the statement. LOC is the
11255 location of the compound statement-- this is usually the location
11256 of the opening brace. */
11259 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
11265 if (c_dialect_objc ())
11266 objc_clear_super_receiver ();
11267 block
= pop_scope ();
11270 stmt
= pop_stmt_list (stmt
);
11271 stmt
= c_build_bind_expr (loc
, block
, stmt
);
11273 /* If this compound statement is nested immediately inside a statement
11274 expression, then force a BIND_EXPR to be created. Otherwise we'll
11275 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
11276 STATEMENT_LISTs merge, and thus we can lose track of what statement
11277 was really last. */
11278 if (building_stmt_list_p ()
11279 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
11280 && TREE_CODE (stmt
) != BIND_EXPR
)
11282 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
11283 TREE_SIDE_EFFECTS (stmt
) = 1;
11284 SET_EXPR_LOCATION (stmt
, loc
);
11290 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11291 when the current scope is exited. EH_ONLY is true when this is not
11292 meant to apply to normal control flow transfer. */
11295 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
11297 enum tree_code code
;
11301 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
11302 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
11304 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
11305 list
= push_stmt_list ();
11306 TREE_OPERAND (stmt
, 0) = list
;
11307 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
11310 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11311 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11314 build_vec_cmp (tree_code code
, tree type
,
11315 tree arg0
, tree arg1
)
11317 tree zero_vec
= build_zero_cst (type
);
11318 tree minus_one_vec
= build_minus_one_cst (type
);
11319 tree cmp_type
= build_same_sized_truth_vector_type (type
);
11320 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
11321 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
11324 /* Build a binary-operation expression without default conversions.
11325 CODE is the kind of expression to build.
11326 LOCATION is the operator's location.
11327 This function differs from `build' in several ways:
11328 the data type of the result is computed and recorded in it,
11329 warnings are generated if arg data types are invalid,
11330 special handling for addition and subtraction of pointers is known,
11331 and some optimization is done (operations on narrow ints
11332 are done in the narrower type when that gives the same result).
11333 Constant folding is also done before the result is returned.
11335 Note that the operands will never have enumeral types, or function
11336 or array types, because either they will have the default conversions
11337 performed or they have both just been converted to some other type in which
11338 the arithmetic is to be done. */
11341 build_binary_op (location_t location
, enum tree_code code
,
11342 tree orig_op0
, tree orig_op1
, bool convert_p
)
11344 tree type0
, type1
, orig_type0
, orig_type1
;
11346 enum tree_code code0
, code1
;
11348 tree ret
= error_mark_node
;
11349 const char *invalid_op_diag
;
11350 bool op0_int_operands
, op1_int_operands
;
11351 bool int_const
, int_const_or_overflow
, int_operands
;
11353 /* Expression code to give to the expression when it is built.
11354 Normally this is CODE, which is what the caller asked for,
11355 but in some special cases we change it. */
11356 enum tree_code resultcode
= code
;
11358 /* Data type in which the computation is to be performed.
11359 In the simplest cases this is the common type of the arguments. */
11360 tree result_type
= NULL
;
11362 /* When the computation is in excess precision, the type of the
11363 final EXCESS_PRECISION_EXPR. */
11364 tree semantic_result_type
= NULL
;
11366 /* Nonzero means operands have already been type-converted
11367 in whatever way is necessary.
11368 Zero means they need to be converted to RESULT_TYPE. */
11371 /* Nonzero means create the expression with this type, rather than
11373 tree build_type
= NULL_TREE
;
11375 /* Nonzero means after finally constructing the expression
11376 convert it to this type. */
11377 tree final_type
= NULL_TREE
;
11379 /* Nonzero if this is an operation like MIN or MAX which can
11380 safely be computed in short if both args are promoted shorts.
11381 Also implies COMMON.
11382 -1 indicates a bitwise operation; this makes a difference
11383 in the exact conditions for when it is safe to do the operation
11384 in a narrower mode. */
11387 /* Nonzero if this is a comparison operation;
11388 if both args are promoted shorts, compare the original shorts.
11389 Also implies COMMON. */
11390 int short_compare
= 0;
11392 /* Nonzero if this is a right-shift operation, which can be computed on the
11393 original short and then promoted if the operand is a promoted short. */
11394 int short_shift
= 0;
11396 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11399 /* True means types are compatible as far as ObjC is concerned. */
11402 /* True means this is an arithmetic operation that may need excess
11404 bool may_need_excess_precision
;
11406 /* True means this is a boolean operation that converts both its
11407 operands to truth-values. */
11408 bool boolean_op
= false;
11410 /* Remember whether we're doing / or %. */
11411 bool doing_div_or_mod
= false;
11413 /* Remember whether we're doing << or >>. */
11414 bool doing_shift
= false;
11416 /* Tree holding instrumentation expression. */
11417 tree instrument_expr
= NULL
;
11419 if (location
== UNKNOWN_LOCATION
)
11420 location
= input_location
;
11425 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
11426 if (op0_int_operands
)
11427 op0
= remove_c_maybe_const_expr (op0
);
11428 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
11429 if (op1_int_operands
)
11430 op1
= remove_c_maybe_const_expr (op1
);
11431 int_operands
= (op0_int_operands
&& op1_int_operands
);
11434 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
11435 && TREE_CODE (orig_op1
) == INTEGER_CST
);
11436 int_const
= (int_const_or_overflow
11437 && !TREE_OVERFLOW (orig_op0
)
11438 && !TREE_OVERFLOW (orig_op1
));
11441 int_const
= int_const_or_overflow
= false;
11443 /* Do not apply default conversion in mixed vector/scalar expression. */
11445 && VECTOR_TYPE_P (TREE_TYPE (op0
)) == VECTOR_TYPE_P (TREE_TYPE (op1
)))
11447 op0
= default_conversion (op0
);
11448 op1
= default_conversion (op1
);
11451 orig_type0
= type0
= TREE_TYPE (op0
);
11453 orig_type1
= type1
= TREE_TYPE (op1
);
11455 /* The expression codes of the data types of the arguments tell us
11456 whether the arguments are integers, floating, pointers, etc. */
11457 code0
= TREE_CODE (type0
);
11458 code1
= TREE_CODE (type1
);
11460 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11461 STRIP_TYPE_NOPS (op0
);
11462 STRIP_TYPE_NOPS (op1
);
11464 /* If an error was already reported for one of the arguments,
11465 avoid reporting another error. */
11467 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
11468 return error_mark_node
;
11470 if (code0
== POINTER_TYPE
11471 && reject_gcc_builtin (op0
, EXPR_LOCATION (orig_op0
)))
11472 return error_mark_node
;
11474 if (code1
== POINTER_TYPE
11475 && reject_gcc_builtin (op1
, EXPR_LOCATION (orig_op1
)))
11476 return error_mark_node
;
11478 if ((invalid_op_diag
11479 = targetm
.invalid_binary_op (code
, type0
, type1
)))
11481 error_at (location
, invalid_op_diag
);
11482 return error_mark_node
;
11490 case TRUNC_DIV_EXPR
:
11491 case CEIL_DIV_EXPR
:
11492 case FLOOR_DIV_EXPR
:
11493 case ROUND_DIV_EXPR
:
11494 case EXACT_DIV_EXPR
:
11495 may_need_excess_precision
= true;
11504 /* Excess precision for implicit conversions of integers to
11505 floating point in C11 and later. */
11506 may_need_excess_precision
= (flag_isoc11
11507 && (ANY_INTEGRAL_TYPE_P (type0
)
11508 || ANY_INTEGRAL_TYPE_P (type1
)));
11512 may_need_excess_precision
= false;
11515 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
11517 op0
= TREE_OPERAND (op0
, 0);
11518 type0
= TREE_TYPE (op0
);
11520 else if (may_need_excess_precision
11521 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
11524 op0
= convert (eptype
, op0
);
11526 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
11528 op1
= TREE_OPERAND (op1
, 0);
11529 type1
= TREE_TYPE (op1
);
11531 else if (may_need_excess_precision
11532 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
11535 op1
= convert (eptype
, op1
);
11538 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
11540 /* In case when one of the operands of the binary operation is
11541 a vector and another is a scalar -- convert scalar to vector. */
11542 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
11544 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
11547 switch (convert_flag
)
11550 return error_mark_node
;
11553 bool maybe_const
= true;
11555 sc
= c_fully_fold (op0
, false, &maybe_const
);
11556 sc
= save_expr (sc
);
11557 sc
= convert (TREE_TYPE (type1
), sc
);
11558 op0
= build_vector_from_val (type1
, sc
);
11560 op0
= c_wrap_maybe_const (op0
, true);
11561 orig_type0
= type0
= TREE_TYPE (op0
);
11562 code0
= TREE_CODE (type0
);
11566 case stv_secondarg
:
11568 bool maybe_const
= true;
11570 sc
= c_fully_fold (op1
, false, &maybe_const
);
11571 sc
= save_expr (sc
);
11572 sc
= convert (TREE_TYPE (type0
), sc
);
11573 op1
= build_vector_from_val (type0
, sc
);
11575 op1
= c_wrap_maybe_const (op1
, true);
11576 orig_type1
= type1
= TREE_TYPE (op1
);
11577 code1
= TREE_CODE (type1
);
11589 /* Handle the pointer + int case. */
11590 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11592 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
11593 goto return_build_binary_op
;
11595 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
11597 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
11598 goto return_build_binary_op
;
11605 /* Subtraction of two similar pointers.
11606 We must subtract them as integers, then divide by object size. */
11607 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
11608 && comp_target_types (location
, type0
, type1
))
11610 ret
= pointer_diff (location
, op0
, op1
, &instrument_expr
);
11611 goto return_build_binary_op
;
11613 /* Handle pointer minus int. Just like pointer plus int. */
11614 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11616 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
11617 goto return_build_binary_op
;
11627 case TRUNC_DIV_EXPR
:
11628 case CEIL_DIV_EXPR
:
11629 case FLOOR_DIV_EXPR
:
11630 case ROUND_DIV_EXPR
:
11631 case EXACT_DIV_EXPR
:
11632 doing_div_or_mod
= true;
11633 warn_for_div_by_zero (location
, op1
);
11635 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11636 || code0
== FIXED_POINT_TYPE
11637 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
11638 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11639 || code1
== FIXED_POINT_TYPE
11640 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
11642 enum tree_code tcode0
= code0
, tcode1
= code1
;
11644 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
11645 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
11646 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
11647 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
11649 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
11650 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
11651 resultcode
= RDIV_EXPR
;
11653 /* Although it would be tempting to shorten always here, that
11654 loses on some targets, since the modulo instruction is
11655 undefined if the quotient can't be represented in the
11656 computation mode. We shorten only if unsigned or if
11657 dividing by something we know != -1. */
11658 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
11659 || (TREE_CODE (op1
) == INTEGER_CST
11660 && !integer_all_onesp (op1
)));
11668 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
11670 /* Allow vector types which are not floating point types. */
11671 else if (code0
== VECTOR_TYPE
11672 && code1
== VECTOR_TYPE
11673 && !VECTOR_FLOAT_TYPE_P (type0
)
11674 && !VECTOR_FLOAT_TYPE_P (type1
))
11678 case TRUNC_MOD_EXPR
:
11679 case FLOOR_MOD_EXPR
:
11680 doing_div_or_mod
= true;
11681 warn_for_div_by_zero (location
, op1
);
11683 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11684 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11685 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
11687 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
11689 /* Although it would be tempting to shorten always here, that loses
11690 on some targets, since the modulo instruction is undefined if the
11691 quotient can't be represented in the computation mode. We shorten
11692 only if unsigned or if dividing by something we know != -1. */
11693 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
11694 || (TREE_CODE (op1
) == INTEGER_CST
11695 && !integer_all_onesp (op1
)));
11700 case TRUTH_ANDIF_EXPR
:
11701 case TRUTH_ORIF_EXPR
:
11702 case TRUTH_AND_EXPR
:
11703 case TRUTH_OR_EXPR
:
11704 case TRUTH_XOR_EXPR
:
11705 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
11706 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11707 || code0
== FIXED_POINT_TYPE
)
11708 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
11709 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11710 || code1
== FIXED_POINT_TYPE
))
11712 /* Result of these operations is always an int,
11713 but that does not mean the operands should be
11714 converted to ints! */
11715 result_type
= integer_type_node
;
11716 if (op0_int_operands
)
11718 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
11719 op0
= remove_c_maybe_const_expr (op0
);
11722 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
11723 if (op1_int_operands
)
11725 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
11726 op1
= remove_c_maybe_const_expr (op1
);
11729 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
11733 if (code
== TRUTH_ANDIF_EXPR
)
11735 int_const_or_overflow
= (int_operands
11736 && TREE_CODE (orig_op0
) == INTEGER_CST
11737 && (op0
== truthvalue_false_node
11738 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11739 int_const
= (int_const_or_overflow
11740 && !TREE_OVERFLOW (orig_op0
)
11741 && (op0
== truthvalue_false_node
11742 || !TREE_OVERFLOW (orig_op1
)));
11744 else if (code
== TRUTH_ORIF_EXPR
)
11746 int_const_or_overflow
= (int_operands
11747 && TREE_CODE (orig_op0
) == INTEGER_CST
11748 && (op0
== truthvalue_true_node
11749 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11750 int_const
= (int_const_or_overflow
11751 && !TREE_OVERFLOW (orig_op0
)
11752 && (op0
== truthvalue_true_node
11753 || !TREE_OVERFLOW (orig_op1
)));
11757 /* Shift operations: result has same type as first operand;
11758 always convert second operand to int.
11759 Also set SHORT_SHIFT if shifting rightward. */
11762 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11763 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11764 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11765 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
11766 TYPE_VECTOR_SUBPARTS (type1
)))
11768 result_type
= type0
;
11771 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11772 || (code0
== VECTOR_TYPE
11773 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
))
11774 && code1
== INTEGER_TYPE
)
11776 doing_shift
= true;
11777 if (TREE_CODE (op1
) == INTEGER_CST
)
11779 if (tree_int_cst_sgn (op1
) < 0)
11782 if (c_inhibit_evaluation_warnings
== 0)
11783 warning_at (location
, OPT_Wshift_count_negative
,
11784 "right shift count is negative");
11786 else if (code0
== VECTOR_TYPE
)
11788 if (compare_tree_int (op1
,
11789 TYPE_PRECISION (TREE_TYPE (type0
)))
11793 if (c_inhibit_evaluation_warnings
== 0)
11794 warning_at (location
, OPT_Wshift_count_overflow
,
11795 "right shift count >= width of vector element");
11800 if (!integer_zerop (op1
))
11803 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11806 if (c_inhibit_evaluation_warnings
== 0)
11807 warning_at (location
, OPT_Wshift_count_overflow
,
11808 "right shift count >= width of type");
11813 /* Use the type of the value to be shifted. */
11814 result_type
= type0
;
11815 /* Avoid converting op1 to result_type later. */
11821 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11822 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11823 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11824 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
11825 TYPE_VECTOR_SUBPARTS (type1
)))
11827 result_type
= type0
;
11830 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11831 || (code0
== VECTOR_TYPE
11832 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
))
11833 && code1
== INTEGER_TYPE
)
11835 doing_shift
= true;
11836 if (TREE_CODE (op0
) == INTEGER_CST
11837 && tree_int_cst_sgn (op0
) < 0)
11839 /* Don't reject a left shift of a negative value in a context
11840 where a constant expression is needed in C90. */
11843 if (c_inhibit_evaluation_warnings
== 0)
11844 warning_at (location
, OPT_Wshift_negative_value
,
11845 "left shift of negative value");
11847 if (TREE_CODE (op1
) == INTEGER_CST
)
11849 if (tree_int_cst_sgn (op1
) < 0)
11852 if (c_inhibit_evaluation_warnings
== 0)
11853 warning_at (location
, OPT_Wshift_count_negative
,
11854 "left shift count is negative");
11856 else if (code0
== VECTOR_TYPE
)
11858 if (compare_tree_int (op1
,
11859 TYPE_PRECISION (TREE_TYPE (type0
)))
11863 if (c_inhibit_evaluation_warnings
== 0)
11864 warning_at (location
, OPT_Wshift_count_overflow
,
11865 "left shift count >= width of vector element");
11868 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11871 if (c_inhibit_evaluation_warnings
== 0)
11872 warning_at (location
, OPT_Wshift_count_overflow
,
11873 "left shift count >= width of type");
11875 else if (TREE_CODE (op0
) == INTEGER_CST
11876 && maybe_warn_shift_overflow (location
, op0
, op1
)
11881 /* Use the type of the value to be shifted. */
11882 result_type
= type0
;
11883 /* Avoid converting op1 to result_type later. */
11890 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11893 if (!vector_types_compatible_elements_p (type0
, type1
))
11895 error_at (location
, "comparing vectors with different "
11897 return error_mark_node
;
11900 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
11901 TYPE_VECTOR_SUBPARTS (type1
)))
11903 error_at (location
, "comparing vectors with different "
11904 "number of elements");
11905 return error_mark_node
;
11908 /* It's not precisely specified how the usual arithmetic
11909 conversions apply to the vector types. Here, we use
11910 the unsigned type if one of the operands is signed and
11911 the other one is unsigned. */
11912 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11914 if (!TYPE_UNSIGNED (type0
))
11915 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11917 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11918 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11919 "types %qT and %qT", type0
, type1
);
11922 /* Always construct signed integer vector type. */
11923 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11925 (TREE_TYPE (type0
))), 0);
11928 error_at (location
, "could not find an integer type "
11929 "of the same size as %qT",
11930 TREE_TYPE (type0
));
11931 return error_mark_node
;
11933 result_type
= build_opaque_vector_type (intt
,
11934 TYPE_VECTOR_SUBPARTS (type0
));
11936 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11937 goto return_build_binary_op
;
11939 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
11940 warning_at (location
,
11942 "comparing floating point with %<==%> or %<!=%> is unsafe");
11943 /* Result of comparison is always int,
11944 but don't convert the args to int! */
11945 build_type
= integer_type_node
;
11946 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11947 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
11948 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11949 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
11951 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11953 if (TREE_CODE (op0
) == ADDR_EXPR
11954 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0))
11955 && !from_macro_expansion_at (location
))
11957 if (code
== EQ_EXPR
)
11958 warning_at (location
,
11960 "the comparison will always evaluate as %<false%> "
11961 "for the address of %qD will never be NULL",
11962 TREE_OPERAND (op0
, 0));
11964 warning_at (location
,
11966 "the comparison will always evaluate as %<true%> "
11967 "for the address of %qD will never be NULL",
11968 TREE_OPERAND (op0
, 0));
11970 result_type
= type0
;
11972 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11974 if (TREE_CODE (op1
) == ADDR_EXPR
11975 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0))
11976 && !from_macro_expansion_at (location
))
11978 if (code
== EQ_EXPR
)
11979 warning_at (location
,
11981 "the comparison will always evaluate as %<false%> "
11982 "for the address of %qD will never be NULL",
11983 TREE_OPERAND (op1
, 0));
11985 warning_at (location
,
11987 "the comparison will always evaluate as %<true%> "
11988 "for the address of %qD will never be NULL",
11989 TREE_OPERAND (op1
, 0));
11991 result_type
= type1
;
11993 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
11995 tree tt0
= TREE_TYPE (type0
);
11996 tree tt1
= TREE_TYPE (type1
);
11997 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
11998 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
11999 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
12001 /* Anything compares with void *. void * compares with anything.
12002 Otherwise, the targets must be compatible
12003 and both must be object or both incomplete. */
12004 if (comp_target_types (location
, type0
, type1
))
12005 result_type
= common_pointer_type (type0
, type1
);
12006 else if (!addr_space_superset (as0
, as1
, &as_common
))
12008 error_at (location
, "comparison of pointers to "
12009 "disjoint address spaces");
12010 return error_mark_node
;
12012 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
12014 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
12015 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
12016 "comparison of %<void *%> with function pointer");
12018 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
12020 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
12021 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
12022 "comparison of %<void *%> with function pointer");
12025 /* Avoid warning about the volatile ObjC EH puts on decls. */
12027 pedwarn (location
, 0,
12028 "comparison of distinct pointer types lacks a cast");
12030 if (result_type
== NULL_TREE
)
12032 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
12033 result_type
= build_pointer_type
12034 (build_qualified_type (void_type_node
, qual
));
12037 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
12039 result_type
= type0
;
12040 pedwarn (location
, 0, "comparison between pointer and integer");
12042 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
12044 result_type
= type1
;
12045 pedwarn (location
, 0, "comparison between pointer and integer");
12047 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
12048 || truth_value_p (TREE_CODE (orig_op0
)))
12049 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
12050 || truth_value_p (TREE_CODE (orig_op1
))))
12051 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
12058 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
12061 if (!vector_types_compatible_elements_p (type0
, type1
))
12063 error_at (location
, "comparing vectors with different "
12065 return error_mark_node
;
12068 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
12069 TYPE_VECTOR_SUBPARTS (type1
)))
12071 error_at (location
, "comparing vectors with different "
12072 "number of elements");
12073 return error_mark_node
;
12076 /* It's not precisely specified how the usual arithmetic
12077 conversions apply to the vector types. Here, we use
12078 the unsigned type if one of the operands is signed and
12079 the other one is unsigned. */
12080 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
12082 if (!TYPE_UNSIGNED (type0
))
12083 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
12085 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
12086 warning_at (location
, OPT_Wsign_compare
, "comparison between "
12087 "types %qT and %qT", type0
, type1
);
12090 /* Always construct signed integer vector type. */
12091 intt
= c_common_type_for_size (GET_MODE_BITSIZE
12093 (TREE_TYPE (type0
))), 0);
12096 error_at (location
, "could not find an integer type "
12097 "of the same size as %qT",
12098 TREE_TYPE (type0
));
12099 return error_mark_node
;
12101 result_type
= build_opaque_vector_type (intt
,
12102 TYPE_VECTOR_SUBPARTS (type0
));
12104 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
12105 goto return_build_binary_op
;
12107 build_type
= integer_type_node
;
12108 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
12109 || code0
== FIXED_POINT_TYPE
)
12110 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
12111 || code1
== FIXED_POINT_TYPE
))
12113 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
12115 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
12116 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
12117 addr_space_t as_common
;
12119 if (comp_target_types (location
, type0
, type1
))
12121 result_type
= common_pointer_type (type0
, type1
);
12122 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
12123 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
12124 pedwarn (location
, 0,
12125 "comparison of complete and incomplete pointers");
12126 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
12127 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
12128 "ordered comparisons of pointers to functions");
12129 else if (null_pointer_constant_p (orig_op0
)
12130 || null_pointer_constant_p (orig_op1
))
12131 warning_at (location
, OPT_Wextra
,
12132 "ordered comparison of pointer with null pointer");
12135 else if (!addr_space_superset (as0
, as1
, &as_common
))
12137 error_at (location
, "comparison of pointers to "
12138 "disjoint address spaces");
12139 return error_mark_node
;
12143 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
12144 result_type
= build_pointer_type
12145 (build_qualified_type (void_type_node
, qual
));
12146 pedwarn (location
, 0,
12147 "comparison of distinct pointer types lacks a cast");
12150 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
12152 result_type
= type0
;
12154 pedwarn (location
, OPT_Wpedantic
,
12155 "ordered comparison of pointer with integer zero");
12156 else if (extra_warnings
)
12157 warning_at (location
, OPT_Wextra
,
12158 "ordered comparison of pointer with integer zero");
12160 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
12162 result_type
= type1
;
12164 pedwarn (location
, OPT_Wpedantic
,
12165 "ordered comparison of pointer with integer zero");
12166 else if (extra_warnings
)
12167 warning_at (location
, OPT_Wextra
,
12168 "ordered comparison of pointer with integer zero");
12170 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
12172 result_type
= type0
;
12173 pedwarn (location
, 0, "comparison between pointer and integer");
12175 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
12177 result_type
= type1
;
12178 pedwarn (location
, 0, "comparison between pointer and integer");
12181 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
12182 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
12184 op0
= save_expr (op0
);
12185 op1
= save_expr (op1
);
12187 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
12188 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
12191 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
12192 || truth_value_p (TREE_CODE (orig_op0
)))
12193 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
12194 || truth_value_p (TREE_CODE (orig_op1
))))
12195 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
12199 gcc_unreachable ();
12202 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
12203 return error_mark_node
;
12205 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
12206 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
12207 || !vector_types_compatible_elements_p (type0
, type1
)))
12209 gcc_rich_location
richloc (location
);
12210 maybe_range_label_for_tree_type_mismatch
12211 label_for_op0 (orig_op0
, orig_op1
),
12212 label_for_op1 (orig_op1
, orig_op0
);
12213 richloc
.maybe_add_expr (orig_op0
, &label_for_op0
);
12214 richloc
.maybe_add_expr (orig_op1
, &label_for_op1
);
12215 binary_op_error (&richloc
, code
, type0
, type1
);
12216 return error_mark_node
;
12219 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
12220 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
12222 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
12223 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
12225 bool first_complex
= (code0
== COMPLEX_TYPE
);
12226 bool second_complex
= (code1
== COMPLEX_TYPE
);
12227 int none_complex
= (!first_complex
&& !second_complex
);
12229 if (shorten
|| common
|| short_compare
)
12231 result_type
= c_common_type (type0
, type1
);
12232 do_warn_double_promotion (result_type
, type0
, type1
,
12233 "implicit conversion from %qT to %qT "
12234 "to match other operand of binary "
12237 if (result_type
== error_mark_node
)
12238 return error_mark_node
;
12241 if (first_complex
!= second_complex
12242 && (code
== PLUS_EXPR
12243 || code
== MINUS_EXPR
12244 || code
== MULT_EXPR
12245 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
12246 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
12247 && flag_signed_zeros
)
12249 /* An operation on mixed real/complex operands must be
12250 handled specially, but the language-independent code can
12251 more easily optimize the plain complex arithmetic if
12252 -fno-signed-zeros. */
12253 tree real_type
= TREE_TYPE (result_type
);
12255 if (type0
!= orig_type0
|| type1
!= orig_type1
)
12257 gcc_assert (may_need_excess_precision
&& common
);
12258 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
12262 if (TREE_TYPE (op0
) != result_type
)
12263 op0
= convert_and_check (location
, result_type
, op0
);
12264 if (TREE_TYPE (op1
) != real_type
)
12265 op1
= convert_and_check (location
, real_type
, op1
);
12269 if (TREE_TYPE (op0
) != real_type
)
12270 op0
= convert_and_check (location
, real_type
, op0
);
12271 if (TREE_TYPE (op1
) != result_type
)
12272 op1
= convert_and_check (location
, result_type
, op1
);
12274 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
12275 return error_mark_node
;
12278 op0
= save_expr (op0
);
12279 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
12281 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
12286 case TRUNC_DIV_EXPR
:
12287 op1
= save_expr (op1
);
12288 imag
= build2 (resultcode
, real_type
, imag
, op1
);
12289 /* Fall through. */
12292 real
= build2 (resultcode
, real_type
, real
, op1
);
12300 op1
= save_expr (op1
);
12301 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
12303 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
12308 op0
= save_expr (op0
);
12309 imag
= build2 (resultcode
, real_type
, op0
, imag
);
12310 /* Fall through. */
12312 real
= build2 (resultcode
, real_type
, op0
, real
);
12315 real
= build2 (resultcode
, real_type
, op0
, real
);
12316 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
12322 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
12323 goto return_build_binary_op
;
12326 /* For certain operations (which identify themselves by shorten != 0)
12327 if both args were extended from the same smaller type,
12328 do the arithmetic in that type and then extend.
12330 shorten !=0 and !=1 indicates a bitwise operation.
12331 For them, this optimization is safe only if
12332 both args are zero-extended or both are sign-extended.
12333 Otherwise, we might change the result.
12334 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12335 but calculated in (unsigned short) it would be (unsigned short)-1. */
12337 if (shorten
&& none_complex
)
12339 final_type
= result_type
;
12340 result_type
= shorten_binary_op (result_type
, op0
, op1
,
12344 /* Shifts can be shortened if shifting right. */
12349 tree arg0
= get_narrower (op0
, &unsigned_arg
);
12351 final_type
= result_type
;
12353 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
12354 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
12356 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
12357 && tree_int_cst_sgn (op1
) > 0
12358 /* We can shorten only if the shift count is less than the
12359 number of bits in the smaller type size. */
12360 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
12361 /* We cannot drop an unsigned shift after sign-extension. */
12362 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
12364 /* Do an unsigned shift if the operand was zero-extended. */
12366 = c_common_signed_or_unsigned_type (unsigned_arg
,
12368 /* Convert value-to-be-shifted to that type. */
12369 if (TREE_TYPE (op0
) != result_type
)
12370 op0
= convert (result_type
, op0
);
12375 /* Comparison operations are shortened too but differently.
12376 They identify themselves by setting short_compare = 1. */
12380 /* Don't write &op0, etc., because that would prevent op0
12381 from being kept in a register.
12382 Instead, make copies of the our local variables and
12383 pass the copies by reference, then copy them back afterward. */
12384 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
12385 enum tree_code xresultcode
= resultcode
;
12387 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
12390 if (val
!= NULL_TREE
)
12393 goto return_build_binary_op
;
12396 op0
= xop0
, op1
= xop1
;
12398 resultcode
= xresultcode
;
12400 if (c_inhibit_evaluation_warnings
== 0)
12402 bool op0_maybe_const
= true;
12403 bool op1_maybe_const
= true;
12404 tree orig_op0_folded
, orig_op1_folded
;
12406 if (in_late_binary_op
)
12408 orig_op0_folded
= orig_op0
;
12409 orig_op1_folded
= orig_op1
;
12413 /* Fold for the sake of possible warnings, as in
12414 build_conditional_expr. This requires the
12415 "original" values to be folded, not just op0 and
12417 c_inhibit_evaluation_warnings
++;
12418 op0
= c_fully_fold (op0
, require_constant_value
,
12420 op1
= c_fully_fold (op1
, require_constant_value
,
12422 c_inhibit_evaluation_warnings
--;
12423 orig_op0_folded
= c_fully_fold (orig_op0
,
12424 require_constant_value
,
12426 orig_op1_folded
= c_fully_fold (orig_op1
,
12427 require_constant_value
,
12431 if (warn_sign_compare
)
12432 warn_for_sign_compare (location
, orig_op0_folded
,
12433 orig_op1_folded
, op0
, op1
,
12434 result_type
, resultcode
);
12435 if (!in_late_binary_op
&& !int_operands
)
12437 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
12438 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
12439 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
12440 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
12446 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12447 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12448 Then the expression will be built.
12449 It will be given type FINAL_TYPE if that is nonzero;
12450 otherwise, it will be given type RESULT_TYPE. */
12454 /* Favor showing any expression locations that are available. */
12455 op_location_t
oploc (location
, UNKNOWN_LOCATION
);
12456 binary_op_rich_location
richloc (oploc
, orig_op0
, orig_op1
, true);
12457 binary_op_error (&richloc
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
12458 return error_mark_node
;
12461 if (build_type
== NULL_TREE
)
12463 build_type
= result_type
;
12464 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
12467 gcc_assert (may_need_excess_precision
&& common
);
12468 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
12474 op0
= ep_convert_and_check (location
, result_type
, op0
,
12475 semantic_result_type
);
12476 op1
= ep_convert_and_check (location
, result_type
, op1
,
12477 semantic_result_type
);
12479 /* This can happen if one operand has a vector type, and the other
12480 has a different type. */
12481 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
12482 return error_mark_node
;
12485 if (sanitize_flags_p ((SANITIZE_SHIFT
12486 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
12487 && current_function_decl
!= NULL_TREE
12488 && (doing_div_or_mod
|| doing_shift
)
12489 && !require_constant_value
)
12491 /* OP0 and/or OP1 might have side-effects. */
12492 op0
= save_expr (op0
);
12493 op1
= save_expr (op1
);
12494 op0
= c_fully_fold (op0
, false, NULL
);
12495 op1
= c_fully_fold (op1
, false, NULL
);
12496 if (doing_div_or_mod
&& (sanitize_flags_p ((SANITIZE_DIVIDE
12497 | SANITIZE_FLOAT_DIVIDE
))))
12498 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
12499 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
12500 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
12503 /* Treat expressions in initializers specially as they can't trap. */
12504 if (int_const_or_overflow
)
12505 ret
= (require_constant_value
12506 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
12508 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
12510 ret
= build2 (resultcode
, build_type
, op0
, op1
);
12511 if (final_type
!= NULL_TREE
)
12512 ret
= convert (final_type
, ret
);
12514 return_build_binary_op
:
12515 gcc_assert (ret
!= error_mark_node
);
12516 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
12517 ret
= (int_operands
12518 ? note_integer_operands (ret
)
12519 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
12520 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
12521 && !in_late_binary_op
)
12522 ret
= note_integer_operands (ret
);
12523 protected_set_expr_location (ret
, location
);
12525 if (instrument_expr
!= NULL
)
12526 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
12527 instrument_expr
, ret
);
12529 if (semantic_result_type
)
12530 ret
= build1_loc (location
, EXCESS_PRECISION_EXPR
,
12531 semantic_result_type
, ret
);
12537 /* Convert EXPR to be a truth-value, validating its type for this
12538 purpose. LOCATION is the source location for the expression. */
12541 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
12543 bool int_const
, int_operands
;
12545 switch (TREE_CODE (TREE_TYPE (expr
)))
12548 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
12549 return error_mark_node
;
12552 error_at (location
, "used struct type value where scalar is required");
12553 return error_mark_node
;
12556 error_at (location
, "used union type value where scalar is required");
12557 return error_mark_node
;
12560 error_at (location
, "void value not ignored as it ought to be");
12561 return error_mark_node
;
12564 if (reject_gcc_builtin (expr
))
12565 return error_mark_node
;
12568 case FUNCTION_TYPE
:
12569 gcc_unreachable ();
12572 error_at (location
, "used vector type where scalar is required");
12573 return error_mark_node
;
12579 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
12580 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
12581 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
12583 expr
= remove_c_maybe_const_expr (expr
);
12584 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
12585 convert (TREE_TYPE (expr
), integer_zero_node
));
12586 expr
= note_integer_operands (expr
);
12589 /* ??? Should we also give an error for vectors rather than leaving
12590 those to give errors later? */
12591 expr
= c_common_truthvalue_conversion (location
, expr
);
12593 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
12595 if (TREE_OVERFLOW (expr
))
12598 return note_integer_operands (expr
);
12600 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
12601 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
12606 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12610 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
12612 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
12614 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
12615 /* Executing a compound literal inside a function reinitializes
12617 if (!TREE_STATIC (decl
))
12625 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12626 statement. LOC is the location of the construct. */
12629 c_finish_omp_construct (location_t loc
, enum tree_code code
, tree body
,
12632 body
= c_end_compound_stmt (loc
, body
, true);
12634 tree stmt
= make_node (code
);
12635 TREE_TYPE (stmt
) = void_type_node
;
12636 OMP_BODY (stmt
) = body
;
12637 OMP_CLAUSES (stmt
) = clauses
;
12638 SET_EXPR_LOCATION (stmt
, loc
);
12640 return add_stmt (stmt
);
12643 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12644 statement. LOC is the location of the OACC_DATA. */
12647 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
12651 block
= c_end_compound_stmt (loc
, block
, true);
12653 stmt
= make_node (OACC_DATA
);
12654 TREE_TYPE (stmt
) = void_type_node
;
12655 OACC_DATA_CLAUSES (stmt
) = clauses
;
12656 OACC_DATA_BODY (stmt
) = block
;
12657 SET_EXPR_LOCATION (stmt
, loc
);
12659 return add_stmt (stmt
);
12662 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12663 statement. LOC is the location of the OACC_HOST_DATA. */
12666 c_finish_oacc_host_data (location_t loc
, tree clauses
, tree block
)
12670 block
= c_end_compound_stmt (loc
, block
, true);
12672 stmt
= make_node (OACC_HOST_DATA
);
12673 TREE_TYPE (stmt
) = void_type_node
;
12674 OACC_HOST_DATA_CLAUSES (stmt
) = clauses
;
12675 OACC_HOST_DATA_BODY (stmt
) = block
;
12676 SET_EXPR_LOCATION (stmt
, loc
);
12678 return add_stmt (stmt
);
12681 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12684 c_begin_omp_parallel (void)
12688 keep_next_level ();
12689 block
= c_begin_compound_stmt (true);
12694 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12695 statement. LOC is the location of the OMP_PARALLEL. */
12698 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
12702 block
= c_end_compound_stmt (loc
, block
, true);
12704 stmt
= make_node (OMP_PARALLEL
);
12705 TREE_TYPE (stmt
) = void_type_node
;
12706 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
12707 OMP_PARALLEL_BODY (stmt
) = block
;
12708 SET_EXPR_LOCATION (stmt
, loc
);
12710 return add_stmt (stmt
);
12713 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12716 c_begin_omp_task (void)
12720 keep_next_level ();
12721 block
= c_begin_compound_stmt (true);
12726 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12727 statement. LOC is the location of the #pragma. */
12730 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
12734 block
= c_end_compound_stmt (loc
, block
, true);
12736 stmt
= make_node (OMP_TASK
);
12737 TREE_TYPE (stmt
) = void_type_node
;
12738 OMP_TASK_CLAUSES (stmt
) = clauses
;
12739 OMP_TASK_BODY (stmt
) = block
;
12740 SET_EXPR_LOCATION (stmt
, loc
);
12742 return add_stmt (stmt
);
12745 /* Generate GOMP_cancel call for #pragma omp cancel. */
12748 c_finish_omp_cancel (location_t loc
, tree clauses
)
12750 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
12752 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12754 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
12756 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12758 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12762 error_at (loc
, "%<#pragma omp cancel%> must specify one of "
12763 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12767 tree ifc
= omp_find_clause (clauses
, OMP_CLAUSE_IF
);
12768 if (ifc
!= NULL_TREE
)
12770 if (OMP_CLAUSE_IF_MODIFIER (ifc
) != ERROR_MARK
12771 && OMP_CLAUSE_IF_MODIFIER (ifc
) != VOID_CST
)
12772 error_at (OMP_CLAUSE_LOCATION (ifc
),
12773 "expected %<cancel%> %<if%> clause modifier");
12776 tree ifc2
= omp_find_clause (OMP_CLAUSE_CHAIN (ifc
), OMP_CLAUSE_IF
);
12777 if (ifc2
!= NULL_TREE
)
12779 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc
) == VOID_CST
12780 && OMP_CLAUSE_IF_MODIFIER (ifc2
) != ERROR_MARK
12781 && OMP_CLAUSE_IF_MODIFIER (ifc2
) != VOID_CST
);
12782 error_at (OMP_CLAUSE_LOCATION (ifc2
),
12783 "expected %<cancel%> %<if%> clause modifier");
12787 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
12788 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
12789 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
12790 build_zero_cst (type
));
12793 ifc
= boolean_true_node
;
12794 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
12795 build_int_cst (integer_type_node
, mask
),
12800 /* Generate GOMP_cancellation_point call for
12801 #pragma omp cancellation point. */
12804 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
12806 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
12808 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12810 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
12812 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12814 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12818 error_at (loc
, "%<#pragma omp cancellation point%> must specify one of "
12819 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12823 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
12824 build_int_cst (integer_type_node
, mask
));
12828 /* Helper function for handle_omp_array_sections. Called recursively
12829 to handle multiple array-section-subscripts. C is the clause,
12830 T current expression (initially OMP_CLAUSE_DECL), which is either
12831 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12832 expression if specified, TREE_VALUE length expression if specified,
12833 TREE_CHAIN is what it has been specified after, or some decl.
12834 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12835 set to true if any of the array-section-subscript could have length
12836 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12837 first array-section-subscript which is known not to have length
12839 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12840 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12841 all are or may have length of 1, array-section-subscript [:2] is the
12842 first one known not to have length 1. For array-section-subscript
12843 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12844 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12845 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12846 case though, as some lengths could be zero. */
12849 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
12850 bool &maybe_zero_len
, unsigned int &first_non_one
,
12851 enum c_omp_region_type ort
)
12853 tree ret
, low_bound
, length
, type
;
12854 if (TREE_CODE (t
) != TREE_LIST
)
12856 if (error_operand_p (t
))
12857 return error_mark_node
;
12859 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12860 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
12862 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qE in %qs clause",
12863 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12864 return error_mark_node
;
12866 if (TREE_CODE (t
) == COMPONENT_REF
12867 && ort
== C_ORT_OMP
12868 && (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12869 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO
12870 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FROM
))
12872 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
12874 error_at (OMP_CLAUSE_LOCATION (c
),
12875 "bit-field %qE in %qs clause",
12876 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12877 return error_mark_node
;
12879 while (TREE_CODE (t
) == COMPONENT_REF
)
12881 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == UNION_TYPE
)
12883 error_at (OMP_CLAUSE_LOCATION (c
),
12884 "%qE is a member of a union", t
);
12885 return error_mark_node
;
12887 t
= TREE_OPERAND (t
, 0);
12890 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
12893 error_at (OMP_CLAUSE_LOCATION (c
),
12894 "%qD is not a variable in %qs clause", t
,
12895 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12897 error_at (OMP_CLAUSE_LOCATION (c
),
12898 "%qE is not a variable in %qs clause", t
,
12899 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12900 return error_mark_node
;
12902 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12903 && TYPE_ATOMIC (TREE_TYPE (t
)))
12905 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qD in %qs clause",
12906 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12907 return error_mark_node
;
12909 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12911 && DECL_THREAD_LOCAL_P (t
))
12913 error_at (OMP_CLAUSE_LOCATION (c
),
12914 "%qD is threadprivate variable in %qs clause", t
,
12915 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12916 return error_mark_node
;
12918 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12919 && TYPE_ATOMIC (TREE_TYPE (t
))
12920 && POINTER_TYPE_P (TREE_TYPE (t
)))
12922 /* If the array section is pointer based and the pointer
12923 itself is _Atomic qualified, we need to atomically load
12926 memset (&expr
, 0, sizeof (expr
));
12928 expr
= convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c
),
12929 expr
, false, false);
12935 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
12936 maybe_zero_len
, first_non_one
, ort
);
12937 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
12940 type
= TREE_TYPE (ret
);
12941 low_bound
= TREE_PURPOSE (t
);
12942 length
= TREE_VALUE (t
);
12944 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
12945 return error_mark_node
;
12947 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
12949 error_at (OMP_CLAUSE_LOCATION (c
),
12950 "low bound %qE of array section does not have integral type",
12952 return error_mark_node
;
12954 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
12956 error_at (OMP_CLAUSE_LOCATION (c
),
12957 "length %qE of array section does not have integral type",
12959 return error_mark_node
;
12962 && TREE_CODE (low_bound
) == INTEGER_CST
12963 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12964 > TYPE_PRECISION (sizetype
))
12965 low_bound
= fold_convert (sizetype
, low_bound
);
12967 && TREE_CODE (length
) == INTEGER_CST
12968 && TYPE_PRECISION (TREE_TYPE (length
))
12969 > TYPE_PRECISION (sizetype
))
12970 length
= fold_convert (sizetype
, length
);
12971 if (low_bound
== NULL_TREE
)
12972 low_bound
= integer_zero_node
;
12974 if (length
!= NULL_TREE
)
12976 if (!integer_nonzerop (length
))
12978 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12979 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12980 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
12981 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
12983 if (integer_zerop (length
))
12985 error_at (OMP_CLAUSE_LOCATION (c
),
12986 "zero length array section in %qs clause",
12987 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12988 return error_mark_node
;
12992 maybe_zero_len
= true;
12994 if (first_non_one
== types
.length ()
12995 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
12998 if (TREE_CODE (type
) == ARRAY_TYPE
)
13000 if (length
== NULL_TREE
13001 && (TYPE_DOMAIN (type
) == NULL_TREE
13002 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
13004 error_at (OMP_CLAUSE_LOCATION (c
),
13005 "for unknown bound array type length expression must "
13007 return error_mark_node
;
13009 if (TREE_CODE (low_bound
) == INTEGER_CST
13010 && tree_int_cst_sgn (low_bound
) == -1)
13012 error_at (OMP_CLAUSE_LOCATION (c
),
13013 "negative low bound in array section in %qs clause",
13014 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13015 return error_mark_node
;
13017 if (length
!= NULL_TREE
13018 && TREE_CODE (length
) == INTEGER_CST
13019 && tree_int_cst_sgn (length
) == -1)
13021 error_at (OMP_CLAUSE_LOCATION (c
),
13022 "negative length in array section in %qs clause",
13023 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13024 return error_mark_node
;
13026 if (TYPE_DOMAIN (type
)
13027 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
13028 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
13032 = fold_convert (sizetype
, TYPE_MAX_VALUE (TYPE_DOMAIN (type
)));
13033 size
= size_binop (PLUS_EXPR
, size
, size_one_node
);
13034 if (TREE_CODE (low_bound
) == INTEGER_CST
)
13036 if (tree_int_cst_lt (size
, low_bound
))
13038 error_at (OMP_CLAUSE_LOCATION (c
),
13039 "low bound %qE above array section size "
13040 "in %qs clause", low_bound
,
13041 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13042 return error_mark_node
;
13044 if (tree_int_cst_equal (size
, low_bound
))
13046 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
13047 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13048 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13049 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
13051 error_at (OMP_CLAUSE_LOCATION (c
),
13052 "zero length array section in %qs clause",
13053 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13054 return error_mark_node
;
13056 maybe_zero_len
= true;
13058 else if (length
== NULL_TREE
13059 && first_non_one
== types
.length ()
13060 && tree_int_cst_equal
13061 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
13065 else if (length
== NULL_TREE
)
13067 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13068 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
13069 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_IN_REDUCTION
13070 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_TASK_REDUCTION
)
13071 maybe_zero_len
= true;
13072 if (first_non_one
== types
.length ())
13075 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
13077 if (tree_int_cst_lt (size
, length
))
13079 error_at (OMP_CLAUSE_LOCATION (c
),
13080 "length %qE above array section size "
13081 "in %qs clause", length
,
13082 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13083 return error_mark_node
;
13085 if (TREE_CODE (low_bound
) == INTEGER_CST
)
13088 = size_binop (PLUS_EXPR
,
13089 fold_convert (sizetype
, low_bound
),
13090 fold_convert (sizetype
, length
));
13091 if (TREE_CODE (lbpluslen
) == INTEGER_CST
13092 && tree_int_cst_lt (size
, lbpluslen
))
13094 error_at (OMP_CLAUSE_LOCATION (c
),
13095 "high bound %qE above array section size "
13096 "in %qs clause", lbpluslen
,
13097 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13098 return error_mark_node
;
13103 else if (length
== NULL_TREE
)
13105 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13106 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
13107 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_IN_REDUCTION
13108 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_TASK_REDUCTION
)
13109 maybe_zero_len
= true;
13110 if (first_non_one
== types
.length ())
13114 /* For [lb:] we will need to evaluate lb more than once. */
13115 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
13117 tree lb
= save_expr (low_bound
);
13118 if (lb
!= low_bound
)
13120 TREE_PURPOSE (t
) = lb
;
13125 else if (TREE_CODE (type
) == POINTER_TYPE
)
13127 if (length
== NULL_TREE
)
13129 error_at (OMP_CLAUSE_LOCATION (c
),
13130 "for pointer type length expression must be specified");
13131 return error_mark_node
;
13133 if (length
!= NULL_TREE
13134 && TREE_CODE (length
) == INTEGER_CST
13135 && tree_int_cst_sgn (length
) == -1)
13137 error_at (OMP_CLAUSE_LOCATION (c
),
13138 "negative length in array section in %qs clause",
13139 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13140 return error_mark_node
;
13142 /* If there is a pointer type anywhere but in the very first
13143 array-section-subscript, the array section can't be contiguous. */
13144 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
13145 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
13147 error_at (OMP_CLAUSE_LOCATION (c
),
13148 "array section is not contiguous in %qs clause",
13149 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13150 return error_mark_node
;
13155 error_at (OMP_CLAUSE_LOCATION (c
),
13156 "%qE does not have pointer or array type", ret
);
13157 return error_mark_node
;
13159 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
13160 types
.safe_push (TREE_TYPE (ret
));
13161 /* We will need to evaluate lb more than once. */
13162 tree lb
= save_expr (low_bound
);
13163 if (lb
!= low_bound
)
13165 TREE_PURPOSE (t
) = lb
;
13168 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
13172 /* Handle array sections for clause C. */
13175 handle_omp_array_sections (tree c
, enum c_omp_region_type ort
)
13177 bool maybe_zero_len
= false;
13178 unsigned int first_non_one
= 0;
13179 auto_vec
<tree
, 10> types
;
13180 tree
*tp
= &OMP_CLAUSE_DECL (c
);
13181 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
13182 && TREE_CODE (*tp
) == TREE_LIST
13183 && TREE_PURPOSE (*tp
)
13184 && TREE_CODE (TREE_PURPOSE (*tp
)) == TREE_VEC
)
13185 tp
= &TREE_VALUE (*tp
);
13186 tree first
= handle_omp_array_sections_1 (c
, *tp
, types
,
13187 maybe_zero_len
, first_non_one
,
13189 if (first
== error_mark_node
)
13191 if (first
== NULL_TREE
)
13193 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
13196 tree tem
= NULL_TREE
;
13197 /* Need to evaluate side effects in the length expressions
13199 while (TREE_CODE (t
) == TREE_LIST
)
13201 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
13203 if (tem
== NULL_TREE
)
13204 tem
= TREE_VALUE (t
);
13206 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
13207 TREE_VALUE (t
), tem
);
13209 t
= TREE_CHAIN (t
);
13212 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
13213 first
= c_fully_fold (first
, false, NULL
, true);
13218 unsigned int num
= types
.length (), i
;
13219 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
13220 tree condition
= NULL_TREE
;
13222 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
13223 maybe_zero_len
= true;
13225 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
13226 t
= TREE_CHAIN (t
))
13228 tree low_bound
= TREE_PURPOSE (t
);
13229 tree length
= TREE_VALUE (t
);
13233 && TREE_CODE (low_bound
) == INTEGER_CST
13234 && TYPE_PRECISION (TREE_TYPE (low_bound
))
13235 > TYPE_PRECISION (sizetype
))
13236 low_bound
= fold_convert (sizetype
, low_bound
);
13238 && TREE_CODE (length
) == INTEGER_CST
13239 && TYPE_PRECISION (TREE_TYPE (length
))
13240 > TYPE_PRECISION (sizetype
))
13241 length
= fold_convert (sizetype
, length
);
13242 if (low_bound
== NULL_TREE
)
13243 low_bound
= integer_zero_node
;
13244 if (!maybe_zero_len
&& i
> first_non_one
)
13246 if (integer_nonzerop (low_bound
))
13247 goto do_warn_noncontiguous
;
13248 if (length
!= NULL_TREE
13249 && TREE_CODE (length
) == INTEGER_CST
13250 && TYPE_DOMAIN (types
[i
])
13251 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
13252 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
13256 size
= size_binop (PLUS_EXPR
,
13257 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
13259 if (!tree_int_cst_equal (length
, size
))
13261 do_warn_noncontiguous
:
13262 error_at (OMP_CLAUSE_LOCATION (c
),
13263 "array section is not contiguous in %qs "
13265 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13269 if (length
!= NULL_TREE
13270 && TREE_SIDE_EFFECTS (length
))
13272 if (side_effects
== NULL_TREE
)
13273 side_effects
= length
;
13275 side_effects
= build2 (COMPOUND_EXPR
,
13276 TREE_TYPE (side_effects
),
13277 length
, side_effects
);
13284 if (i
> first_non_one
13285 && ((length
&& integer_nonzerop (length
))
13286 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13287 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13288 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
))
13291 l
= fold_convert (sizetype
, length
);
13294 l
= size_binop (PLUS_EXPR
,
13295 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
13297 l
= size_binop (MINUS_EXPR
, l
,
13298 fold_convert (sizetype
, low_bound
));
13300 if (i
> first_non_one
)
13302 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
13304 if (condition
== NULL_TREE
)
13307 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
13310 else if (size
== NULL_TREE
)
13312 size
= size_in_bytes (TREE_TYPE (types
[i
]));
13313 tree eltype
= TREE_TYPE (types
[num
- 1]);
13314 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
13315 eltype
= TREE_TYPE (eltype
);
13316 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13317 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13318 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
13320 if (integer_zerop (size
)
13321 || integer_zerop (size_in_bytes (eltype
)))
13323 error_at (OMP_CLAUSE_LOCATION (c
),
13324 "zero length array section in %qs clause",
13325 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13326 return error_mark_node
;
13328 size
= size_binop (EXACT_DIV_EXPR
, size
,
13329 size_in_bytes (eltype
));
13331 size
= size_binop (MULT_EXPR
, size
, l
);
13333 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
13334 size
, size_zero_node
);
13337 size
= size_binop (MULT_EXPR
, size
, l
);
13341 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
13342 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13343 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
13344 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TASK_REDUCTION
)
13346 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
13347 size
= c_fully_fold (size
, false, NULL
);
13348 size
= save_expr (size
);
13349 tree index_type
= build_index_type (size
);
13350 tree eltype
= TREE_TYPE (first
);
13351 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
13352 eltype
= TREE_TYPE (eltype
);
13353 tree type
= build_array_type (eltype
, index_type
);
13354 tree ptype
= build_pointer_type (eltype
);
13355 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13356 t
= build_fold_addr_expr (t
);
13357 tree t2
= build_fold_addr_expr (first
);
13358 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
13359 ptrdiff_type_node
, t2
);
13360 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13361 ptrdiff_type_node
, t2
,
13362 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
13363 ptrdiff_type_node
, t
));
13364 t2
= c_fully_fold (t2
, false, NULL
);
13365 if (tree_fits_shwi_p (t2
))
13366 t
= build2 (MEM_REF
, type
, t
,
13367 build_int_cst (ptype
, tree_to_shwi (t2
)));
13370 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), sizetype
, t2
);
13371 t
= build2_loc (OMP_CLAUSE_LOCATION (c
), POINTER_PLUS_EXPR
,
13372 TREE_TYPE (t
), t
, t2
);
13373 t
= build2 (MEM_REF
, type
, t
, build_int_cst (ptype
, 0));
13375 OMP_CLAUSE_DECL (c
) = t
;
13378 first
= c_fully_fold (first
, false, NULL
);
13379 OMP_CLAUSE_DECL (c
) = first
;
13381 size
= c_fully_fold (size
, false, NULL
);
13382 OMP_CLAUSE_SIZE (c
) = size
;
13383 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
13384 || (TREE_CODE (t
) == COMPONENT_REF
13385 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
))
13387 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
13388 if (ort
== C_ORT_OMP
|| ort
== C_ORT_ACC
)
13389 switch (OMP_CLAUSE_MAP_KIND (c
))
13391 case GOMP_MAP_ALLOC
:
13393 case GOMP_MAP_FROM
:
13394 case GOMP_MAP_TOFROM
:
13395 case GOMP_MAP_ALWAYS_TO
:
13396 case GOMP_MAP_ALWAYS_FROM
:
13397 case GOMP_MAP_ALWAYS_TOFROM
:
13398 case GOMP_MAP_RELEASE
:
13399 case GOMP_MAP_DELETE
:
13400 case GOMP_MAP_FORCE_TO
:
13401 case GOMP_MAP_FORCE_FROM
:
13402 case GOMP_MAP_FORCE_TOFROM
:
13403 case GOMP_MAP_FORCE_PRESENT
:
13404 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c
) = 1;
13409 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
13410 if (ort
!= C_ORT_OMP
&& ort
!= C_ORT_ACC
)
13411 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_POINTER
);
13412 else if (TREE_CODE (t
) == COMPONENT_REF
)
13413 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_ALWAYS_POINTER
);
13415 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
13416 if (OMP_CLAUSE_MAP_KIND (c2
) != GOMP_MAP_FIRSTPRIVATE_POINTER
13417 && !c_mark_addressable (t
))
13419 OMP_CLAUSE_DECL (c2
) = t
;
13420 t
= build_fold_addr_expr (first
);
13421 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
13422 tree ptr
= OMP_CLAUSE_DECL (c2
);
13423 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
13424 ptr
= build_fold_addr_expr (ptr
);
13425 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13426 ptrdiff_type_node
, t
,
13427 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
13428 ptrdiff_type_node
, ptr
));
13429 t
= c_fully_fold (t
, false, NULL
);
13430 OMP_CLAUSE_SIZE (c2
) = t
;
13431 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
13432 OMP_CLAUSE_CHAIN (c
) = c2
;
13437 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13438 an inline call. But, remap
13439 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13440 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13443 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
13444 tree decl
, tree placeholder
)
13447 hash_map
<tree
, tree
> decl_map
;
13449 decl_map
.put (omp_decl1
, placeholder
);
13450 decl_map
.put (omp_decl2
, decl
);
13451 memset (&id
, 0, sizeof (id
));
13452 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
13453 id
.dst_fn
= current_function_decl
;
13454 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
13455 id
.decl_map
= &decl_map
;
13457 id
.copy_decl
= copy_decl_no_change
;
13458 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
13459 id
.transform_new_cfg
= true;
13460 id
.transform_return_to_modify
= false;
13461 id
.transform_lang_insert_block
= NULL
;
13463 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
13467 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13468 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13471 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
13473 if (*tp
== (tree
) data
)
13478 /* Similarly, but also walk aggregate fields. */
13480 struct c_find_omp_var_s
{ tree var
; hash_set
<tree
> *pset
; };
13483 c_find_omp_var_r (tree
*tp
, int *, void *data
)
13485 if (*tp
== ((struct c_find_omp_var_s
*) data
)->var
)
13487 if (RECORD_OR_UNION_TYPE_P (*tp
))
13490 hash_set
<tree
> *pset
= ((struct c_find_omp_var_s
*) data
)->pset
;
13492 for (field
= TYPE_FIELDS (*tp
); field
;
13493 field
= DECL_CHAIN (field
))
13494 if (TREE_CODE (field
) == FIELD_DECL
)
13496 tree ret
= walk_tree (&DECL_FIELD_OFFSET (field
),
13497 c_find_omp_var_r
, data
, pset
);
13500 ret
= walk_tree (&DECL_SIZE (field
), c_find_omp_var_r
, data
, pset
);
13503 ret
= walk_tree (&DECL_SIZE_UNIT (field
), c_find_omp_var_r
, data
,
13507 ret
= walk_tree (&TREE_TYPE (field
), c_find_omp_var_r
, data
, pset
);
13512 else if (INTEGRAL_TYPE_P (*tp
))
13513 return walk_tree (&TYPE_MAX_VALUE (*tp
), c_find_omp_var_r
, data
,
13514 ((struct c_find_omp_var_s
*) data
)->pset
);
13518 /* Finish OpenMP iterators ITER. Return true if they are errorneous
13519 and clauses containing them should be removed. */
13522 c_omp_finish_iterators (tree iter
)
13525 for (tree it
= iter
; it
; it
= TREE_CHAIN (it
))
13527 tree var
= TREE_VEC_ELT (it
, 0);
13528 tree begin
= TREE_VEC_ELT (it
, 1);
13529 tree end
= TREE_VEC_ELT (it
, 2);
13530 tree step
= TREE_VEC_ELT (it
, 3);
13532 tree type
= TREE_TYPE (var
);
13533 location_t loc
= DECL_SOURCE_LOCATION (var
);
13534 if (type
== error_mark_node
)
13539 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
13541 error_at (loc
, "iterator %qD has neither integral nor pointer type",
13546 else if (TYPE_ATOMIC (type
))
13548 error_at (loc
, "iterator %qD has %<_Atomic%> qualified type", var
);
13552 else if (TYPE_READONLY (type
))
13554 error_at (loc
, "iterator %qD has const qualified type", var
);
13558 else if (step
== error_mark_node
13559 || TREE_TYPE (step
) == error_mark_node
)
13564 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step
)))
13566 error_at (EXPR_LOC_OR_LOC (step
, loc
),
13567 "iterator step with non-integral type");
13571 begin
= c_fully_fold (build_c_cast (loc
, type
, begin
), false, NULL
);
13572 end
= c_fully_fold (build_c_cast (loc
, type
, end
), false, NULL
);
13573 orig_step
= save_expr (c_fully_fold (step
, false, NULL
));
13574 tree stype
= POINTER_TYPE_P (type
) ? sizetype
: type
;
13575 step
= c_fully_fold (build_c_cast (loc
, stype
, orig_step
), false, NULL
);
13576 if (POINTER_TYPE_P (type
))
13578 begin
= save_expr (begin
);
13579 step
= pointer_int_sum (loc
, PLUS_EXPR
, begin
, step
);
13580 step
= fold_build2_loc (loc
, MINUS_EXPR
, sizetype
,
13581 fold_convert (sizetype
, step
),
13582 fold_convert (sizetype
, begin
));
13583 step
= fold_convert (ssizetype
, step
);
13585 if (integer_zerop (step
))
13587 error_at (loc
, "iterator %qD has zero step", var
);
13592 if (begin
== error_mark_node
13593 || end
== error_mark_node
13594 || step
== error_mark_node
13595 || orig_step
== error_mark_node
)
13600 hash_set
<tree
> pset
;
13602 for (it2
= TREE_CHAIN (it
); it2
; it2
= TREE_CHAIN (it2
))
13604 tree var2
= TREE_VEC_ELT (it2
, 0);
13605 tree begin2
= TREE_VEC_ELT (it2
, 1);
13606 tree end2
= TREE_VEC_ELT (it2
, 2);
13607 tree step2
= TREE_VEC_ELT (it2
, 3);
13608 tree type2
= TREE_TYPE (var2
);
13609 location_t loc2
= DECL_SOURCE_LOCATION (var2
);
13610 struct c_find_omp_var_s data
= { var
, &pset
};
13611 if (walk_tree (&type2
, c_find_omp_var_r
, &data
, &pset
))
13614 "type of iterator %qD refers to outer iterator %qD",
13618 else if (walk_tree (&begin2
, c_find_omp_var_r
, &data
, &pset
))
13620 error_at (EXPR_LOC_OR_LOC (begin2
, loc2
),
13621 "begin expression refers to outer iterator %qD", var
);
13624 else if (walk_tree (&end2
, c_find_omp_var_r
, &data
, &pset
))
13626 error_at (EXPR_LOC_OR_LOC (end2
, loc2
),
13627 "end expression refers to outer iterator %qD", var
);
13630 else if (walk_tree (&step2
, c_find_omp_var_r
, &data
, &pset
))
13632 error_at (EXPR_LOC_OR_LOC (step2
, loc2
),
13633 "step expression refers to outer iterator %qD", var
);
13642 TREE_VEC_ELT (it
, 1) = begin
;
13643 TREE_VEC_ELT (it
, 2) = end
;
13644 TREE_VEC_ELT (it
, 3) = step
;
13645 TREE_VEC_ELT (it
, 4) = orig_step
;
13650 /* For all elements of CLAUSES, validate them against their constraints.
13651 Remove any elements from the list that are invalid. */
13654 c_finish_omp_clauses (tree clauses
, enum c_omp_region_type ort
)
13656 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
13657 bitmap_head aligned_head
, map_head
, map_field_head
, oacc_reduction_head
;
13658 tree c
, t
, type
, *pc
;
13659 tree simdlen
= NULL_TREE
, safelen
= NULL_TREE
;
13660 bool branch_seen
= false;
13661 bool copyprivate_seen
= false;
13662 bool linear_variable_step_check
= false;
13663 tree
*nowait_clause
= NULL
;
13664 tree ordered_clause
= NULL_TREE
;
13665 tree schedule_clause
= NULL_TREE
;
13666 bool oacc_async
= false;
13667 tree last_iterators
= NULL_TREE
;
13668 bool last_iterators_remove
= false;
13669 tree
*nogroup_seen
= NULL
;
13670 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13671 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
13672 int reduction_seen
= 0;
13674 bitmap_obstack_initialize (NULL
);
13675 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
13676 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
13677 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
13678 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
13679 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
13680 bitmap_initialize (&map_head
, &bitmap_default_obstack
);
13681 bitmap_initialize (&map_field_head
, &bitmap_default_obstack
);
13682 /* If ort == C_ORT_OMP used as nontemporal_head instead. */
13683 bitmap_initialize (&oacc_reduction_head
, &bitmap_default_obstack
);
13685 if (ort
& C_ORT_ACC
)
13686 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13687 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ASYNC
)
13693 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
13695 bool remove
= false;
13696 bool need_complete
= false;
13697 bool need_implicitly_determined
= false;
13699 switch (OMP_CLAUSE_CODE (c
))
13701 case OMP_CLAUSE_SHARED
:
13702 need_implicitly_determined
= true;
13703 goto check_dup_generic
;
13705 case OMP_CLAUSE_PRIVATE
:
13706 need_complete
= true;
13707 need_implicitly_determined
= true;
13708 goto check_dup_generic
;
13710 case OMP_CLAUSE_REDUCTION
:
13711 if (reduction_seen
== 0)
13712 reduction_seen
= OMP_CLAUSE_REDUCTION_INSCAN (c
) ? -1 : 1;
13713 else if (reduction_seen
!= -2
13714 && reduction_seen
!= (OMP_CLAUSE_REDUCTION_INSCAN (c
)
13717 error_at (OMP_CLAUSE_LOCATION (c
),
13718 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
13719 "on the same construct");
13720 reduction_seen
= -2;
13723 case OMP_CLAUSE_IN_REDUCTION
:
13724 case OMP_CLAUSE_TASK_REDUCTION
:
13725 need_implicitly_determined
= true;
13726 t
= OMP_CLAUSE_DECL (c
);
13727 if (TREE_CODE (t
) == TREE_LIST
)
13729 if (handle_omp_array_sections (c
, ort
))
13735 t
= OMP_CLAUSE_DECL (c
);
13736 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
13737 && OMP_CLAUSE_REDUCTION_INSCAN (c
))
13739 error_at (OMP_CLAUSE_LOCATION (c
),
13740 "%<inscan%> %<reduction%> clause with array "
13746 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
13747 if (t
== error_mark_node
)
13753 c_mark_addressable (t
);
13754 type
= TREE_TYPE (t
);
13755 if (TREE_CODE (t
) == MEM_REF
)
13756 type
= TREE_TYPE (type
);
13757 if (TREE_CODE (type
) == ARRAY_TYPE
)
13759 tree oatype
= type
;
13760 gcc_assert (TREE_CODE (t
) != MEM_REF
);
13761 while (TREE_CODE (type
) == ARRAY_TYPE
)
13762 type
= TREE_TYPE (type
);
13763 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
13765 error_at (OMP_CLAUSE_LOCATION (c
),
13766 "%qD in %<reduction%> clause is a zero size array",
13771 tree size
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (oatype
),
13772 TYPE_SIZE_UNIT (type
));
13773 if (integer_zerop (size
))
13775 error_at (OMP_CLAUSE_LOCATION (c
),
13776 "%qD in %<reduction%> clause is a zero size array",
13781 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
13782 size
= save_expr (size
);
13783 tree index_type
= build_index_type (size
);
13784 tree atype
= build_array_type (type
, index_type
);
13785 tree ptype
= build_pointer_type (type
);
13786 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13787 t
= build_fold_addr_expr (t
);
13788 t
= build2 (MEM_REF
, atype
, t
, build_int_cst (ptype
, 0));
13789 OMP_CLAUSE_DECL (c
) = t
;
13791 if (TYPE_ATOMIC (type
))
13793 error_at (OMP_CLAUSE_LOCATION (c
),
13794 "%<_Atomic%> %qE in %<reduction%> clause", t
);
13798 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
13799 || OMP_CLAUSE_REDUCTION_TASK (c
))
13801 /* Disallow zero sized or potentially zero sized task
13803 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
13805 error_at (OMP_CLAUSE_LOCATION (c
),
13806 "zero sized type %qT in %qs clause", type
,
13807 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13811 else if (TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
)
13813 error_at (OMP_CLAUSE_LOCATION (c
),
13814 "variable sized type %qT in %qs clause", type
,
13815 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13820 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
13821 && (FLOAT_TYPE_P (type
)
13822 || TREE_CODE (type
) == COMPLEX_TYPE
))
13824 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
13825 const char *r_name
= NULL
;
13834 if (TREE_CODE (type
) == COMPLEX_TYPE
)
13838 if (TREE_CODE (type
) == COMPLEX_TYPE
)
13850 case TRUTH_ANDIF_EXPR
:
13851 if (FLOAT_TYPE_P (type
))
13854 case TRUTH_ORIF_EXPR
:
13855 if (FLOAT_TYPE_P (type
))
13859 gcc_unreachable ();
13863 error_at (OMP_CLAUSE_LOCATION (c
),
13864 "%qE has invalid type for %<reduction(%s)%>",
13870 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
13872 error_at (OMP_CLAUSE_LOCATION (c
),
13873 "user defined reduction not found for %qE", t
);
13877 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
13879 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
13880 type
= TYPE_MAIN_VARIANT (type
);
13881 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
13882 VAR_DECL
, NULL_TREE
, type
);
13883 tree decl_placeholder
= NULL_TREE
;
13884 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
13885 DECL_ARTIFICIAL (placeholder
) = 1;
13886 DECL_IGNORED_P (placeholder
) = 1;
13887 if (TREE_CODE (t
) == MEM_REF
)
13889 decl_placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
13890 VAR_DECL
, NULL_TREE
, type
);
13891 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
) = decl_placeholder
;
13892 DECL_ARTIFICIAL (decl_placeholder
) = 1;
13893 DECL_IGNORED_P (decl_placeholder
) = 1;
13895 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
13896 c_mark_addressable (placeholder
);
13897 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
13898 c_mark_addressable (decl_placeholder
? decl_placeholder
13899 : OMP_CLAUSE_DECL (c
));
13900 OMP_CLAUSE_REDUCTION_MERGE (c
)
13901 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
13902 TREE_VEC_ELT (list
, 0),
13903 TREE_VEC_ELT (list
, 1),
13904 decl_placeholder
? decl_placeholder
13905 : OMP_CLAUSE_DECL (c
), placeholder
);
13906 OMP_CLAUSE_REDUCTION_MERGE (c
)
13907 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
13908 void_type_node
, NULL_TREE
,
13909 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
13910 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
13911 if (TREE_VEC_LENGTH (list
) == 6)
13913 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
13914 c_mark_addressable (decl_placeholder
? decl_placeholder
13915 : OMP_CLAUSE_DECL (c
));
13916 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
13917 c_mark_addressable (placeholder
);
13918 tree init
= TREE_VEC_ELT (list
, 5);
13919 if (init
== error_mark_node
)
13920 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
13921 OMP_CLAUSE_REDUCTION_INIT (c
)
13922 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
13923 TREE_VEC_ELT (list
, 3),
13924 decl_placeholder
? decl_placeholder
13925 : OMP_CLAUSE_DECL (c
), placeholder
);
13926 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
13928 tree v
= decl_placeholder
? decl_placeholder
: t
;
13929 OMP_CLAUSE_REDUCTION_INIT (c
)
13930 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
,
13931 OMP_CLAUSE_REDUCTION_INIT (c
));
13933 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
13934 c_find_omp_placeholder_r
,
13935 placeholder
, NULL
))
13936 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
13941 tree v
= decl_placeholder
? decl_placeholder
: t
;
13942 if (AGGREGATE_TYPE_P (TREE_TYPE (v
)))
13943 init
= build_constructor (TREE_TYPE (v
), NULL
);
13945 init
= fold_convert (TREE_TYPE (v
), integer_zero_node
);
13946 OMP_CLAUSE_REDUCTION_INIT (c
)
13947 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
, init
);
13949 OMP_CLAUSE_REDUCTION_INIT (c
)
13950 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
13951 void_type_node
, NULL_TREE
,
13952 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
13953 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
13955 if (TREE_CODE (t
) == MEM_REF
)
13957 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))) == NULL_TREE
13958 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))))
13961 sorry ("variable length element type in array "
13962 "%<reduction%> clause");
13966 t
= TREE_OPERAND (t
, 0);
13967 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
13968 t
= TREE_OPERAND (t
, 0);
13969 if (TREE_CODE (t
) == ADDR_EXPR
)
13970 t
= TREE_OPERAND (t
, 0);
13972 goto check_dup_generic_t
;
13974 case OMP_CLAUSE_COPYPRIVATE
:
13975 copyprivate_seen
= true;
13978 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
13979 "%<nowait%> clause must not be used together "
13980 "with %<copyprivate%>");
13981 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
13982 nowait_clause
= NULL
;
13984 goto check_dup_generic
;
13986 case OMP_CLAUSE_COPYIN
:
13987 t
= OMP_CLAUSE_DECL (c
);
13988 if (!VAR_P (t
) || !DECL_THREAD_LOCAL_P (t
))
13990 error_at (OMP_CLAUSE_LOCATION (c
),
13991 "%qE must be %<threadprivate%> for %<copyin%>", t
);
13995 goto check_dup_generic
;
13997 case OMP_CLAUSE_LINEAR
:
13998 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
13999 need_implicitly_determined
= true;
14000 t
= OMP_CLAUSE_DECL (c
);
14001 if (ort
!= C_ORT_OMP_DECLARE_SIMD
14002 && OMP_CLAUSE_LINEAR_KIND (c
) != OMP_CLAUSE_LINEAR_DEFAULT
)
14004 error_at (OMP_CLAUSE_LOCATION (c
),
14005 "modifier should not be specified in %<linear%> "
14006 "clause on %<simd%> or %<for%> constructs");
14007 OMP_CLAUSE_LINEAR_KIND (c
) = OMP_CLAUSE_LINEAR_DEFAULT
;
14009 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
14010 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
14012 error_at (OMP_CLAUSE_LOCATION (c
),
14013 "linear clause applied to non-integral non-pointer "
14014 "variable with type %qT", TREE_TYPE (t
));
14018 if (TYPE_ATOMIC (TREE_TYPE (t
)))
14020 error_at (OMP_CLAUSE_LOCATION (c
),
14021 "%<_Atomic%> %qD in %<linear%> clause", t
);
14025 if (ort
== C_ORT_OMP_DECLARE_SIMD
)
14027 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
14028 if (TREE_CODE (s
) == PARM_DECL
)
14030 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
) = 1;
14031 /* map_head bitmap is used as uniform_head if
14033 if (!bitmap_bit_p (&map_head
, DECL_UID (s
)))
14034 linear_variable_step_check
= true;
14035 goto check_dup_generic
;
14037 if (TREE_CODE (s
) != INTEGER_CST
)
14039 error_at (OMP_CLAUSE_LOCATION (c
),
14040 "%<linear%> clause step %qE is neither constant "
14041 "nor a parameter", s
);
14046 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
14048 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
14049 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
14050 OMP_CLAUSE_DECL (c
), s
);
14051 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
14052 sizetype
, fold_convert (sizetype
, s
),
14054 (sizetype
, OMP_CLAUSE_DECL (c
)));
14055 if (s
== error_mark_node
)
14057 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
14060 OMP_CLAUSE_LINEAR_STEP (c
)
14061 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
14062 goto check_dup_generic
;
14065 t
= OMP_CLAUSE_DECL (c
);
14066 check_dup_generic_t
:
14067 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14069 error_at (OMP_CLAUSE_LOCATION (c
),
14070 "%qE is not a variable in clause %qs", t
,
14071 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14074 else if (ort
== C_ORT_ACC
14075 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
14077 if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
14079 error_at (OMP_CLAUSE_LOCATION (c
),
14080 "%qD appears more than once in reduction clauses",
14085 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
14087 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14088 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
14089 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
14091 error_at (OMP_CLAUSE_LOCATION (c
),
14092 "%qE appears more than once in data clauses", t
);
14095 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
14096 && bitmap_bit_p (&map_head
, DECL_UID (t
)))
14098 if (ort
== C_ORT_ACC
)
14099 error_at (OMP_CLAUSE_LOCATION (c
),
14100 "%qD appears more than once in data clauses", t
);
14102 error_at (OMP_CLAUSE_LOCATION (c
),
14103 "%qD appears both in data and map clauses", t
);
14107 bitmap_set_bit (&generic_head
, DECL_UID (t
));
14110 case OMP_CLAUSE_FIRSTPRIVATE
:
14111 t
= OMP_CLAUSE_DECL (c
);
14112 need_complete
= true;
14113 need_implicitly_determined
= true;
14114 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14116 error_at (OMP_CLAUSE_LOCATION (c
),
14117 "%qE is not a variable in clause %<firstprivate%>", t
);
14120 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14121 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
14123 error_at (OMP_CLAUSE_LOCATION (c
),
14124 "%qE appears more than once in data clauses", t
);
14127 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
14129 if (ort
== C_ORT_ACC
)
14130 error_at (OMP_CLAUSE_LOCATION (c
),
14131 "%qD appears more than once in data clauses", t
);
14133 error_at (OMP_CLAUSE_LOCATION (c
),
14134 "%qD appears both in data and map clauses", t
);
14138 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
14141 case OMP_CLAUSE_LASTPRIVATE
:
14142 t
= OMP_CLAUSE_DECL (c
);
14143 need_complete
= true;
14144 need_implicitly_determined
= true;
14145 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14147 error_at (OMP_CLAUSE_LOCATION (c
),
14148 "%qE is not a variable in clause %<lastprivate%>", t
);
14151 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14152 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
14154 error_at (OMP_CLAUSE_LOCATION (c
),
14155 "%qE appears more than once in data clauses", t
);
14159 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
14162 case OMP_CLAUSE_ALIGNED
:
14163 t
= OMP_CLAUSE_DECL (c
);
14164 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14166 error_at (OMP_CLAUSE_LOCATION (c
),
14167 "%qE is not a variable in %<aligned%> clause", t
);
14170 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
14171 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
14173 error_at (OMP_CLAUSE_LOCATION (c
),
14174 "%qE in %<aligned%> clause is neither a pointer nor "
14178 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
14180 error_at (OMP_CLAUSE_LOCATION (c
),
14181 "%<_Atomic%> %qD in %<aligned%> clause", t
);
14185 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
14187 error_at (OMP_CLAUSE_LOCATION (c
),
14188 "%qE appears more than once in %<aligned%> clauses",
14193 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
14196 case OMP_CLAUSE_NONTEMPORAL
:
14197 t
= OMP_CLAUSE_DECL (c
);
14198 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14200 error_at (OMP_CLAUSE_LOCATION (c
),
14201 "%qE is not a variable in %<nontemporal%> clause", t
);
14204 else if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
14206 error_at (OMP_CLAUSE_LOCATION (c
),
14207 "%qE appears more than once in %<nontemporal%> "
14212 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
14215 case OMP_CLAUSE_DEPEND
:
14216 t
= OMP_CLAUSE_DECL (c
);
14217 if (t
== NULL_TREE
)
14219 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c
)
14220 == OMP_CLAUSE_DEPEND_SOURCE
);
14223 if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
)
14225 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
14226 for (; t
; t
= TREE_CHAIN (t
))
14228 tree decl
= TREE_VALUE (t
);
14229 if (TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
14231 tree offset
= TREE_PURPOSE (t
);
14232 bool neg
= wi::neg_p (wi::to_wide (offset
));
14233 offset
= fold_unary (ABS_EXPR
, TREE_TYPE (offset
), offset
);
14234 tree t2
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
),
14235 neg
? MINUS_EXPR
: PLUS_EXPR
,
14237 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
14239 fold_convert (sizetype
, t2
),
14240 fold_convert (sizetype
, decl
));
14241 if (t2
== error_mark_node
)
14246 TREE_PURPOSE (t
) = t2
;
14251 if (TREE_CODE (t
) == TREE_LIST
14252 && TREE_PURPOSE (t
)
14253 && TREE_CODE (TREE_PURPOSE (t
)) == TREE_VEC
)
14255 if (TREE_PURPOSE (t
) != last_iterators
)
14256 last_iterators_remove
14257 = c_omp_finish_iterators (TREE_PURPOSE (t
));
14258 last_iterators
= TREE_PURPOSE (t
);
14259 t
= TREE_VALUE (t
);
14260 if (last_iterators_remove
)
14261 t
= error_mark_node
;
14264 last_iterators
= NULL_TREE
;
14265 if (TREE_CODE (t
) == TREE_LIST
)
14267 if (handle_omp_array_sections (c
, ort
))
14269 else if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_DEPOBJ
)
14271 error_at (OMP_CLAUSE_LOCATION (c
),
14272 "%<depend%> clause with %<depobj%> dependence "
14273 "type on array section");
14278 if (t
== error_mark_node
)
14280 else if (!lvalue_p (t
))
14282 error_at (OMP_CLAUSE_LOCATION (c
),
14283 "%qE is not lvalue expression nor array section in "
14284 "%<depend%> clause", t
);
14287 else if (TREE_CODE (t
) == COMPONENT_REF
14288 && DECL_C_BIT_FIELD (TREE_OPERAND (t
, 1)))
14290 error_at (OMP_CLAUSE_LOCATION (c
),
14291 "bit-field %qE in %qs clause", t
, "depend");
14294 else if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_DEPOBJ
)
14296 if (!c_omp_depend_t_p (TREE_TYPE (t
)))
14298 error_at (OMP_CLAUSE_LOCATION (c
),
14299 "%qE does not have %<omp_depend_t%> type in "
14300 "%<depend%> clause with %<depobj%> dependence "
14305 else if (c_omp_depend_t_p (TREE_TYPE (t
)))
14307 error_at (OMP_CLAUSE_LOCATION (c
),
14308 "%qE should not have %<omp_depend_t%> type in "
14309 "%<depend%> clause with dependence type other than "
14315 tree addr
= build_unary_op (OMP_CLAUSE_LOCATION (c
), ADDR_EXPR
,
14317 if (addr
== error_mark_node
)
14321 t
= build_indirect_ref (OMP_CLAUSE_LOCATION (c
), addr
,
14323 if (t
== error_mark_node
)
14325 else if (TREE_CODE (OMP_CLAUSE_DECL (c
)) == TREE_LIST
14326 && TREE_PURPOSE (OMP_CLAUSE_DECL (c
))
14327 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c
)))
14329 TREE_VALUE (OMP_CLAUSE_DECL (c
)) = t
;
14331 OMP_CLAUSE_DECL (c
) = t
;
14336 case OMP_CLAUSE_MAP
:
14337 case OMP_CLAUSE_TO
:
14338 case OMP_CLAUSE_FROM
:
14339 case OMP_CLAUSE__CACHE_
:
14340 t
= OMP_CLAUSE_DECL (c
);
14341 if (TREE_CODE (t
) == TREE_LIST
)
14343 if (handle_omp_array_sections (c
, ort
))
14347 t
= OMP_CLAUSE_DECL (c
);
14348 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
14350 error_at (OMP_CLAUSE_LOCATION (c
),
14351 "array section does not have mappable type "
14353 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14356 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
14358 error_at (OMP_CLAUSE_LOCATION (c
),
14359 "%<_Atomic%> %qE in %qs clause", t
,
14360 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14363 while (TREE_CODE (t
) == ARRAY_REF
)
14364 t
= TREE_OPERAND (t
, 0);
14365 if (TREE_CODE (t
) == COMPONENT_REF
14366 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
14368 while (TREE_CODE (t
) == COMPONENT_REF
)
14369 t
= TREE_OPERAND (t
, 0);
14370 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
14372 if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
14374 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
14375 error_at (OMP_CLAUSE_LOCATION (c
),
14376 "%qD appears more than once in motion "
14378 else if (ort
== C_ORT_ACC
)
14379 error_at (OMP_CLAUSE_LOCATION (c
),
14380 "%qD appears more than once in data "
14383 error_at (OMP_CLAUSE_LOCATION (c
),
14384 "%qD appears more than once in map "
14390 bitmap_set_bit (&map_head
, DECL_UID (t
));
14391 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
14397 if (t
== error_mark_node
)
14402 if (TREE_CODE (t
) == COMPONENT_REF
14403 && (ort
& C_ORT_OMP
)
14404 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE__CACHE_
)
14406 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
14408 error_at (OMP_CLAUSE_LOCATION (c
),
14409 "bit-field %qE in %qs clause",
14410 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14413 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
14415 error_at (OMP_CLAUSE_LOCATION (c
),
14416 "%qE does not have a mappable type in %qs clause",
14417 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14420 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
14422 error_at (OMP_CLAUSE_LOCATION (c
),
14423 "%<_Atomic%> %qE in %qs clause", t
,
14424 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14427 while (TREE_CODE (t
) == COMPONENT_REF
)
14429 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0)))
14432 error_at (OMP_CLAUSE_LOCATION (c
),
14433 "%qE is a member of a union", t
);
14437 t
= TREE_OPERAND (t
, 0);
14441 if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
14443 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
14447 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14449 error_at (OMP_CLAUSE_LOCATION (c
),
14450 "%qE is not a variable in %qs clause", t
,
14451 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14454 else if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
14456 error_at (OMP_CLAUSE_LOCATION (c
),
14457 "%qD is threadprivate variable in %qs clause", t
,
14458 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14461 else if ((OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
14462 || (OMP_CLAUSE_MAP_KIND (c
)
14463 != GOMP_MAP_FIRSTPRIVATE_POINTER
))
14464 && !c_mark_addressable (t
))
14466 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
14467 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
14468 || (OMP_CLAUSE_MAP_KIND (c
)
14469 == GOMP_MAP_FIRSTPRIVATE_POINTER
)
14470 || (OMP_CLAUSE_MAP_KIND (c
)
14471 == GOMP_MAP_FORCE_DEVICEPTR
)))
14472 && t
== OMP_CLAUSE_DECL (c
)
14473 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
14475 error_at (OMP_CLAUSE_LOCATION (c
),
14476 "%qD does not have a mappable type in %qs clause", t
,
14477 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14480 else if (TREE_TYPE (t
) == error_mark_node
)
14482 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
14484 error_at (OMP_CLAUSE_LOCATION (c
),
14485 "%<_Atomic%> %qE 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
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
14492 if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14493 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
14495 error_at (OMP_CLAUSE_LOCATION (c
),
14496 "%qD appears more than once in data clauses", t
);
14499 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
14501 if (ort
== C_ORT_ACC
)
14502 error_at (OMP_CLAUSE_LOCATION (c
),
14503 "%qD appears more than once in data clauses", t
);
14505 error_at (OMP_CLAUSE_LOCATION (c
),
14506 "%qD appears both in data and map clauses", t
);
14510 bitmap_set_bit (&generic_head
, DECL_UID (t
));
14512 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
14514 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
14515 error_at (OMP_CLAUSE_LOCATION (c
),
14516 "%qD appears more than once in motion clauses", t
);
14517 else if (ort
== C_ORT_ACC
)
14518 error_at (OMP_CLAUSE_LOCATION (c
),
14519 "%qD appears more than once in data clauses", t
);
14521 error_at (OMP_CLAUSE_LOCATION (c
),
14522 "%qD appears more than once in map clauses", t
);
14525 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
14526 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
14528 if (ort
== C_ORT_ACC
)
14529 error_at (OMP_CLAUSE_LOCATION (c
),
14530 "%qD appears more than once in data clauses", t
);
14532 error_at (OMP_CLAUSE_LOCATION (c
),
14533 "%qD appears both in data and map clauses", t
);
14538 bitmap_set_bit (&map_head
, DECL_UID (t
));
14539 if (t
!= OMP_CLAUSE_DECL (c
)
14540 && TREE_CODE (OMP_CLAUSE_DECL (c
)) == COMPONENT_REF
)
14541 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
14545 case OMP_CLAUSE_TO_DECLARE
:
14546 case OMP_CLAUSE_LINK
:
14547 t
= OMP_CLAUSE_DECL (c
);
14548 if (TREE_CODE (t
) == FUNCTION_DECL
14549 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
14551 else if (!VAR_P (t
))
14553 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
14554 error_at (OMP_CLAUSE_LOCATION (c
),
14555 "%qE is neither a variable nor a function name in "
14557 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14559 error_at (OMP_CLAUSE_LOCATION (c
),
14560 "%qE is not a variable in clause %qs", t
,
14561 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14564 else if (DECL_THREAD_LOCAL_P (t
))
14566 error_at (OMP_CLAUSE_LOCATION (c
),
14567 "%qD is threadprivate variable in %qs clause", t
,
14568 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14571 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
14573 error_at (OMP_CLAUSE_LOCATION (c
),
14574 "%qD does not have a mappable type in %qs clause", t
,
14575 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14580 if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
14582 error_at (OMP_CLAUSE_LOCATION (c
),
14583 "%qE appears more than once on the same "
14584 "%<declare target%> directive", t
);
14588 bitmap_set_bit (&generic_head
, DECL_UID (t
));
14591 case OMP_CLAUSE_UNIFORM
:
14592 t
= OMP_CLAUSE_DECL (c
);
14593 if (TREE_CODE (t
) != PARM_DECL
)
14596 error_at (OMP_CLAUSE_LOCATION (c
),
14597 "%qD is not an argument in %<uniform%> clause", t
);
14599 error_at (OMP_CLAUSE_LOCATION (c
),
14600 "%qE is not an argument in %<uniform%> clause", t
);
14604 /* map_head bitmap is used as uniform_head if declare_simd. */
14605 bitmap_set_bit (&map_head
, DECL_UID (t
));
14606 goto check_dup_generic
;
14608 case OMP_CLAUSE_IS_DEVICE_PTR
:
14609 case OMP_CLAUSE_USE_DEVICE_PTR
:
14610 t
= OMP_CLAUSE_DECL (c
);
14611 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
14612 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
14614 error_at (OMP_CLAUSE_LOCATION (c
),
14615 "%qs variable is neither a pointer nor an array",
14616 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14619 goto check_dup_generic
;
14621 case OMP_CLAUSE_NOWAIT
:
14622 if (copyprivate_seen
)
14624 error_at (OMP_CLAUSE_LOCATION (c
),
14625 "%<nowait%> clause must not be used together "
14626 "with %<copyprivate%>");
14630 nowait_clause
= pc
;
14631 pc
= &OMP_CLAUSE_CHAIN (c
);
14634 case OMP_CLAUSE_IF
:
14635 case OMP_CLAUSE_NUM_THREADS
:
14636 case OMP_CLAUSE_NUM_TEAMS
:
14637 case OMP_CLAUSE_THREAD_LIMIT
:
14638 case OMP_CLAUSE_DEFAULT
:
14639 case OMP_CLAUSE_UNTIED
:
14640 case OMP_CLAUSE_COLLAPSE
:
14641 case OMP_CLAUSE_FINAL
:
14642 case OMP_CLAUSE_MERGEABLE
:
14643 case OMP_CLAUSE_DEVICE
:
14644 case OMP_CLAUSE_DIST_SCHEDULE
:
14645 case OMP_CLAUSE_PARALLEL
:
14646 case OMP_CLAUSE_FOR
:
14647 case OMP_CLAUSE_SECTIONS
:
14648 case OMP_CLAUSE_TASKGROUP
:
14649 case OMP_CLAUSE_PROC_BIND
:
14650 case OMP_CLAUSE_PRIORITY
:
14651 case OMP_CLAUSE_GRAINSIZE
:
14652 case OMP_CLAUSE_NUM_TASKS
:
14653 case OMP_CLAUSE_THREADS
:
14654 case OMP_CLAUSE_SIMD
:
14655 case OMP_CLAUSE_HINT
:
14656 case OMP_CLAUSE_DEFAULTMAP
:
14657 case OMP_CLAUSE_NUM_GANGS
:
14658 case OMP_CLAUSE_NUM_WORKERS
:
14659 case OMP_CLAUSE_VECTOR_LENGTH
:
14660 case OMP_CLAUSE_ASYNC
:
14661 case OMP_CLAUSE_WAIT
:
14662 case OMP_CLAUSE_AUTO
:
14663 case OMP_CLAUSE_INDEPENDENT
:
14664 case OMP_CLAUSE_SEQ
:
14665 case OMP_CLAUSE_GANG
:
14666 case OMP_CLAUSE_WORKER
:
14667 case OMP_CLAUSE_VECTOR
:
14668 case OMP_CLAUSE_TILE
:
14669 case OMP_CLAUSE_IF_PRESENT
:
14670 case OMP_CLAUSE_FINALIZE
:
14671 pc
= &OMP_CLAUSE_CHAIN (c
);
14674 case OMP_CLAUSE_NOGROUP
:
14676 pc
= &OMP_CLAUSE_CHAIN (c
);
14679 case OMP_CLAUSE_SCHEDULE
:
14680 schedule_clause
= c
;
14681 pc
= &OMP_CLAUSE_CHAIN (c
);
14684 case OMP_CLAUSE_ORDERED
:
14685 ordered_clause
= c
;
14686 pc
= &OMP_CLAUSE_CHAIN (c
);
14689 case OMP_CLAUSE_SAFELEN
:
14691 pc
= &OMP_CLAUSE_CHAIN (c
);
14693 case OMP_CLAUSE_SIMDLEN
:
14695 pc
= &OMP_CLAUSE_CHAIN (c
);
14698 case OMP_CLAUSE_INBRANCH
:
14699 case OMP_CLAUSE_NOTINBRANCH
:
14702 error_at (OMP_CLAUSE_LOCATION (c
),
14703 "%<inbranch%> clause is incompatible with "
14704 "%<notinbranch%>");
14708 branch_seen
= true;
14709 pc
= &OMP_CLAUSE_CHAIN (c
);
14712 case OMP_CLAUSE_INCLUSIVE
:
14713 case OMP_CLAUSE_EXCLUSIVE
:
14714 need_complete
= true;
14715 need_implicitly_determined
= true;
14716 t
= OMP_CLAUSE_DECL (c
);
14717 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
14719 error_at (OMP_CLAUSE_LOCATION (c
),
14720 "%qE is not a variable in clause %qs", t
,
14721 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14727 gcc_unreachable ();
14732 t
= OMP_CLAUSE_DECL (c
);
14736 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
14737 if (t
== error_mark_node
)
14741 if (need_implicitly_determined
)
14743 const char *share_name
= NULL
;
14745 if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
14746 share_name
= "threadprivate";
14747 else switch (c_omp_predetermined_sharing (t
))
14749 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
14751 case OMP_CLAUSE_DEFAULT_SHARED
:
14752 share_name
= "shared";
14754 case OMP_CLAUSE_DEFAULT_PRIVATE
:
14755 share_name
= "private";
14758 gcc_unreachable ();
14762 error_at (OMP_CLAUSE_LOCATION (c
),
14763 "%qE is predetermined %qs for %qs",
14765 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14768 else if (TREE_READONLY (t
)
14769 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_SHARED
14770 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_FIRSTPRIVATE
)
14772 error_at (OMP_CLAUSE_LOCATION (c
),
14773 "%<const%> qualified %qE may appear only in "
14774 "%<shared%> or %<firstprivate%> clauses", t
);
14781 *pc
= OMP_CLAUSE_CHAIN (c
);
14783 pc
= &OMP_CLAUSE_CHAIN (c
);
14788 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen
),
14789 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)))
14791 error_at (OMP_CLAUSE_LOCATION (simdlen
),
14792 "%<simdlen%> clause value is bigger than "
14793 "%<safelen%> clause value");
14794 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)
14795 = OMP_CLAUSE_SAFELEN_EXPR (safelen
);
14800 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
14801 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
14803 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
14804 "%<nonmonotonic%> schedule modifier specified together "
14805 "with %<ordered%> clause");
14806 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
14807 = (enum omp_clause_schedule_kind
)
14808 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
14809 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
14812 if (reduction_seen
< 0 && ordered_clause
)
14814 error_at (OMP_CLAUSE_LOCATION (ordered_clause
),
14815 "%qs clause specified together with %<inscan%> "
14816 "%<reduction%> clause", "ordered");
14817 reduction_seen
= -2;
14820 if (reduction_seen
< 0 && schedule_clause
)
14822 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
14823 "%qs clause specified together with %<inscan%> "
14824 "%<reduction%> clause", "schedule");
14825 reduction_seen
= -2;
14828 if (linear_variable_step_check
|| reduction_seen
== -2)
14829 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
14831 bool remove
= false;
14832 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
14833 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
)
14834 && !bitmap_bit_p (&map_head
,
14835 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c
))))
14837 error_at (OMP_CLAUSE_LOCATION (c
),
14838 "%<linear%> clause step is a parameter %qD not "
14839 "specified in %<uniform%> clause",
14840 OMP_CLAUSE_LINEAR_STEP (c
));
14843 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
14844 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 0;
14847 *pc
= OMP_CLAUSE_CHAIN (c
);
14849 pc
= &OMP_CLAUSE_CHAIN (c
);
14852 if (nogroup_seen
&& reduction_seen
)
14854 error_at (OMP_CLAUSE_LOCATION (*nogroup_seen
),
14855 "%<nogroup%> clause must not be used together with "
14856 "%<reduction%> clause");
14857 *nogroup_seen
= OMP_CLAUSE_CHAIN (*nogroup_seen
);
14860 bitmap_obstack_release (NULL
);
14864 /* Return code to initialize DST with a copy constructor from SRC.
14865 C doesn't have copy constructors nor assignment operators, only for
14866 _Atomic vars we need to perform __atomic_load from src into a temporary
14867 followed by __atomic_store of the temporary to dst. */
14870 c_omp_clause_copy_ctor (tree clause
, tree dst
, tree src
)
14872 if (!really_atomic_lvalue (dst
) && !really_atomic_lvalue (src
))
14873 return build2 (MODIFY_EXPR
, TREE_TYPE (dst
), dst
, src
);
14875 location_t loc
= OMP_CLAUSE_LOCATION (clause
);
14876 tree type
= TREE_TYPE (dst
);
14877 tree nonatomic_type
= build_qualified_type (type
, TYPE_UNQUALIFIED
);
14878 tree tmp
= create_tmp_var (nonatomic_type
);
14879 tree tmp_addr
= build_fold_addr_expr (tmp
);
14880 TREE_ADDRESSABLE (tmp
) = 1;
14881 TREE_NO_WARNING (tmp
) = 1;
14882 tree src_addr
= build_fold_addr_expr (src
);
14883 tree dst_addr
= build_fold_addr_expr (dst
);
14884 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
14885 vec
<tree
, va_gc
> *params
;
14886 /* Expansion of a generic atomic load may require an addition
14887 element, so allocate enough to prevent a resize. */
14888 vec_alloc (params
, 4);
14890 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14891 tree fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
14892 params
->quick_push (src_addr
);
14893 params
->quick_push (tmp_addr
);
14894 params
->quick_push (seq_cst
);
14895 tree load
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
14897 vec_alloc (params
, 4);
14899 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14900 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
14901 params
->quick_push (dst_addr
);
14902 params
->quick_push (tmp_addr
);
14903 params
->quick_push (seq_cst
);
14904 tree store
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
14905 return build2 (COMPOUND_EXPR
, void_type_node
, load
, store
);
14908 /* Create a transaction node. */
14911 c_finish_transaction (location_t loc
, tree block
, int flags
)
14913 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
14914 if (flags
& TM_STMT_ATTR_OUTER
)
14915 TRANSACTION_EXPR_OUTER (stmt
) = 1;
14916 if (flags
& TM_STMT_ATTR_RELAXED
)
14917 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
14918 return add_stmt (stmt
);
14921 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14922 down to the element type of an array. If ORIG_QUAL_TYPE is not
14923 NULL, then it should be used as the qualified type
14924 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14925 preserve information about the typedef name from which an array
14926 type was derived). */
14929 c_build_qualified_type (tree type
, int type_quals
, tree orig_qual_type
,
14930 size_t orig_qual_indirect
)
14932 if (type
== error_mark_node
)
14935 if (TREE_CODE (type
) == ARRAY_TYPE
)
14938 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
14939 type_quals
, orig_qual_type
,
14940 orig_qual_indirect
- 1);
14942 /* See if we already have an identically qualified type. */
14943 if (orig_qual_type
&& orig_qual_indirect
== 0)
14944 t
= orig_qual_type
;
14946 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
14948 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
14949 && TYPE_NAME (t
) == TYPE_NAME (type
)
14950 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
14951 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
14952 TYPE_ATTRIBUTES (type
)))
14957 tree domain
= TYPE_DOMAIN (type
);
14959 t
= build_variant_type_copy (type
);
14960 TREE_TYPE (t
) = element_type
;
14962 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
14963 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
14964 SET_TYPE_STRUCTURAL_EQUALITY (t
);
14965 else if (TYPE_CANONICAL (element_type
) != element_type
14966 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
14968 tree unqualified_canon
14969 = build_array_type (TYPE_CANONICAL (element_type
),
14970 domain
? TYPE_CANONICAL (domain
)
14972 if (TYPE_REVERSE_STORAGE_ORDER (type
))
14975 = build_distinct_type_copy (unqualified_canon
);
14976 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon
) = 1;
14979 = c_build_qualified_type (unqualified_canon
, type_quals
);
14982 TYPE_CANONICAL (t
) = t
;
14987 /* A restrict-qualified pointer type must be a pointer to object or
14988 incomplete type. Note that the use of POINTER_TYPE_P also allows
14989 REFERENCE_TYPEs, which is appropriate for C++. */
14990 if ((type_quals
& TYPE_QUAL_RESTRICT
)
14991 && (!POINTER_TYPE_P (type
)
14992 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
14994 error ("invalid use of %<restrict%>");
14995 type_quals
&= ~TYPE_QUAL_RESTRICT
;
14998 tree var_type
= (orig_qual_type
&& orig_qual_indirect
== 0
15000 : build_qualified_type (type
, type_quals
));
15001 /* A variant type does not inherit the list of incomplete vars from the
15002 type main variant. */
15003 if (RECORD_OR_UNION_TYPE_P (var_type
)
15004 && TYPE_MAIN_VARIANT (var_type
) != var_type
)
15005 C_TYPE_INCOMPLETE_VARS (var_type
) = 0;
15009 /* Build a VA_ARG_EXPR for the C parser. */
15012 c_build_va_arg (location_t loc1
, tree expr
, location_t loc2
, tree type
)
15014 if (error_operand_p (type
))
15015 return error_mark_node
;
15016 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15017 order because it takes the address of the expression. */
15018 else if (handled_component_p (expr
)
15019 && reverse_storage_order_for_component_p (expr
))
15021 error_at (loc1
, "cannot use %<va_arg%> with reverse storage order");
15022 return error_mark_node
;
15024 else if (!COMPLETE_TYPE_P (type
))
15026 error_at (loc2
, "second argument to %<va_arg%> is of incomplete "
15028 return error_mark_node
;
15030 else if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
15031 warning_at (loc2
, OPT_Wc___compat
,
15032 "C++ requires promoted type, not enum type, in %<va_arg%>");
15033 return build_va_arg (loc2
, expr
, type
);
15036 /* Return truthvalue of whether T1 is the same tree structure as T2.
15037 Return 1 if they are the same. Return false if they are different. */
15040 c_tree_equal (tree t1
, tree t2
)
15042 enum tree_code code1
, code2
;
15049 for (code1
= TREE_CODE (t1
);
15050 CONVERT_EXPR_CODE_P (code1
)
15051 || code1
== NON_LVALUE_EXPR
;
15052 code1
= TREE_CODE (t1
))
15053 t1
= TREE_OPERAND (t1
, 0);
15054 for (code2
= TREE_CODE (t2
);
15055 CONVERT_EXPR_CODE_P (code2
)
15056 || code2
== NON_LVALUE_EXPR
;
15057 code2
= TREE_CODE (t2
))
15058 t2
= TREE_OPERAND (t2
, 0);
15060 /* They might have become equal now. */
15064 if (code1
!= code2
)
15070 return wi::to_wide (t1
) == wi::to_wide (t2
);
15073 return real_equal (&TREE_REAL_CST (t1
), &TREE_REAL_CST (t2
));
15076 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
15077 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
15078 TREE_STRING_LENGTH (t1
));
15081 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
15082 TREE_FIXED_CST (t2
));
15085 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
15086 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
15089 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
15092 /* We need to do this when determining whether or not two
15093 non-type pointer to member function template arguments
15095 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
15096 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
15101 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
15103 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
15104 if (!c_tree_equal (field
, elt2
->index
)
15105 || !c_tree_equal (value
, elt2
->value
))
15112 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
15114 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
15116 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
15119 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
15124 call_expr_arg_iterator iter1
, iter2
;
15125 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
15127 for (arg1
= first_call_expr_arg (t1
, &iter1
),
15128 arg2
= first_call_expr_arg (t2
, &iter2
);
15130 arg1
= next_call_expr_arg (&iter1
),
15131 arg2
= next_call_expr_arg (&iter2
))
15132 if (!c_tree_equal (arg1
, arg2
))
15141 tree o1
= TREE_OPERAND (t1
, 0);
15142 tree o2
= TREE_OPERAND (t2
, 0);
15144 /* Special case: if either target is an unallocated VAR_DECL,
15145 it means that it's going to be unified with whatever the
15146 TARGET_EXPR is really supposed to initialize, so treat it
15147 as being equivalent to anything. */
15148 if (VAR_P (o1
) && DECL_NAME (o1
) == NULL_TREE
15149 && !DECL_RTL_SET_P (o1
))
15151 else if (VAR_P (o2
) && DECL_NAME (o2
) == NULL_TREE
15152 && !DECL_RTL_SET_P (o2
))
15154 else if (!c_tree_equal (o1
, o2
))
15157 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
15160 case COMPONENT_REF
:
15161 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
15163 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
15169 case FUNCTION_DECL
:
15170 case IDENTIFIER_NODE
:
15177 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
15179 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
15180 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
15181 TREE_VEC_ELT (t2
, ix
)))
15190 switch (TREE_CODE_CLASS (code1
))
15194 case tcc_comparison
:
15195 case tcc_expression
:
15197 case tcc_reference
:
15198 case tcc_statement
:
15200 int i
, n
= TREE_OPERAND_LENGTH (t1
);
15204 case PREINCREMENT_EXPR
:
15205 case PREDECREMENT_EXPR
:
15206 case POSTINCREMENT_EXPR
:
15207 case POSTDECREMENT_EXPR
:
15217 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
15218 && n
!= TREE_OPERAND_LENGTH (t2
))
15221 for (i
= 0; i
< n
; ++i
)
15222 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
15229 return comptypes (t1
, t2
);
15231 gcc_unreachable ();
15233 /* We can get here with --disable-checking. */
15237 /* Returns true when the function declaration FNDECL is implicit,
15238 introduced as a result of a call to an otherwise undeclared
15239 function, and false otherwise. */
15242 c_decl_implicit (const_tree fndecl
)
15244 return C_DECL_IMPLICIT (fndecl
);