1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2018 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);
101 static tree
valid_compound_expr_initializer (tree
, tree
);
102 static void push_string (const char *);
103 static void push_member_name (tree
);
104 static int spelling_length (void);
105 static char *print_spelling (char *);
106 static void warning_init (location_t
, int, const char *);
107 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
108 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, bool,
109 bool, struct obstack
*);
110 static void output_pending_init_elements (int, struct obstack
*);
111 static bool set_designator (location_t
, bool, struct obstack
*);
112 static void push_range_stack (tree
, struct obstack
*);
113 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
115 static void set_nonincremental_init (struct obstack
*);
116 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
117 static tree
find_init_member (tree
, struct obstack
*);
118 static void readonly_warning (tree
, enum lvalue_use
);
119 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
120 static void record_maybe_used_decl (tree
);
121 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
123 /* Return true if EXP is a null pointer constant, false otherwise. */
126 null_pointer_constant_p (const_tree expr
)
128 /* This should really operate on c_expr structures, but they aren't
129 yet available everywhere required. */
130 tree type
= TREE_TYPE (expr
);
131 return (TREE_CODE (expr
) == INTEGER_CST
132 && !TREE_OVERFLOW (expr
)
133 && integer_zerop (expr
)
134 && (INTEGRAL_TYPE_P (type
)
135 || (TREE_CODE (type
) == POINTER_TYPE
136 && VOID_TYPE_P (TREE_TYPE (type
))
137 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
140 /* EXPR may appear in an unevaluated part of an integer constant
141 expression, but not in an evaluated part. Wrap it in a
142 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
143 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
146 note_integer_operands (tree expr
)
149 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
151 ret
= copy_node (expr
);
152 TREE_OVERFLOW (ret
) = 1;
156 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
157 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
162 /* Having checked whether EXPR may appear in an unevaluated part of an
163 integer constant expression and found that it may, remove any
164 C_MAYBE_CONST_EXPR noting this fact and return the resulting
168 remove_c_maybe_const_expr (tree expr
)
170 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
171 return C_MAYBE_CONST_EXPR_EXPR (expr
);
176 \f/* This is a cache to hold if two types are compatible or not. */
178 struct tagged_tu_seen_cache
{
179 const struct tagged_tu_seen_cache
* next
;
182 /* The return value of tagged_types_tu_compatible_p if we had seen
183 these two types already. */
187 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
188 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
190 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
191 does not have an incomplete type. (That includes void types.)
192 LOC is the location of the use. */
195 require_complete_type (location_t loc
, tree value
)
197 tree type
= TREE_TYPE (value
);
199 if (error_operand_p (value
))
200 return error_mark_node
;
202 /* First, detect a valid value with a complete type. */
203 if (COMPLETE_TYPE_P (type
))
206 c_incomplete_type_error (loc
, value
, type
);
207 return error_mark_node
;
210 /* Print an error message for invalid use of an incomplete type.
211 VALUE is the expression that was used (or 0 if that isn't known)
212 and TYPE is the type that was invalid. LOC is the location for
216 c_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type
) == ERROR_MARK
)
222 if (value
!= NULL_TREE
&& (VAR_P (value
) || TREE_CODE (value
) == PARM_DECL
))
223 error_at (loc
, "%qD has an incomplete type %qT", value
, type
);
227 /* We must print an error message. Be clever about what it says. */
229 switch (TREE_CODE (type
))
237 error_at (loc
, "invalid use of void expression");
241 if (TYPE_DOMAIN (type
))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
245 error_at (loc
, "invalid use of flexible array member");
248 type
= TREE_TYPE (type
);
251 error_at (loc
, "invalid use of array with unspecified bounds");
258 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
259 error_at (loc
, "invalid use of undefined type %qT", type
);
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error_at (loc
, "invalid use of incomplete typedef %qT", type
);
266 /* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
270 c_type_promotes_to (tree type
)
272 tree ret
= NULL_TREE
;
274 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
275 ret
= double_type_node
;
276 else if (c_promoting_integer_type_p (type
))
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type
)
280 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
281 ret
= unsigned_type_node
;
283 ret
= integer_type_node
;
286 if (ret
!= NULL_TREE
)
287 return (TYPE_ATOMIC (type
)
288 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
294 /* Return true if between two named address spaces, whether there is a superset
295 named address space that encompasses both address spaces. If there is a
296 superset, return which address space is the superset. */
299 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
306 else if (targetm
.addr_space
.subset_p (as1
, as2
))
311 else if (targetm
.addr_space
.subset_p (as2
, as1
))
320 /* Return a variant of TYPE which has all the type qualifiers of LIKE
321 as well as those of TYPE. */
324 qualify_type (tree type
, tree like
)
326 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
327 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
328 addr_space_t as_common
;
330 /* If the two named address spaces are different, determine the common
331 superset address space. If there isn't one, raise an error. */
332 if (!addr_space_superset (as_type
, as_like
, &as_common
))
335 error ("%qT and %qT are in disjoint named address spaces",
339 return c_build_qualified_type (type
,
340 TYPE_QUALS_NO_ADDR_SPACE (type
)
341 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
342 | ENCODE_QUAL_ADDR_SPACE (as_common
));
345 /* Return true iff the given tree T is a variable length array. */
348 c_vla_type_p (const_tree t
)
350 if (TREE_CODE (t
) == ARRAY_TYPE
351 && C_TYPE_VARIABLE_SIZE (t
))
356 /* Return the composite type of two compatible types.
358 We assume that comptypes has already been done and returned
359 nonzero; if that isn't so, this may crash. In particular, we
360 assume that qualifiers match. */
363 composite_type (tree t1
, tree t2
)
365 enum tree_code code1
;
366 enum tree_code code2
;
369 /* Save time if the two types are the same. */
371 if (t1
== t2
) return t1
;
373 /* If one type is nonsense, use the other. */
374 if (t1
== error_mark_node
)
376 if (t2
== error_mark_node
)
379 code1
= TREE_CODE (t1
);
380 code2
= TREE_CODE (t2
);
382 /* Merge the attributes. */
383 attributes
= targetm
.merge_type_attributes (t1
, t2
);
385 /* If one is an enumerated type and the other is the compatible
386 integer type, the composite type might be either of the two
387 (DR#013 question 3). For consistency, use the enumerated type as
388 the composite type. */
390 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
392 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
395 gcc_assert (code1
== code2
);
400 /* For two pointers, do this recursively on the target type. */
402 tree pointed_to_1
= TREE_TYPE (t1
);
403 tree pointed_to_2
= TREE_TYPE (t2
);
404 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
405 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
406 t1
= build_type_attribute_variant (t1
, attributes
);
407 return qualify_type (t1
, t2
);
412 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
415 tree d1
= TYPE_DOMAIN (t1
);
416 tree d2
= TYPE_DOMAIN (t2
);
417 bool d1_variable
, d2_variable
;
418 bool d1_zero
, d2_zero
;
419 bool t1_complete
, t2_complete
;
421 /* We should not have any type quals on arrays at all. */
422 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
423 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
425 t1_complete
= COMPLETE_TYPE_P (t1
);
426 t2_complete
= COMPLETE_TYPE_P (t2
);
428 d1_zero
= d1
== NULL_TREE
|| !TYPE_MAX_VALUE (d1
);
429 d2_zero
= d2
== NULL_TREE
|| !TYPE_MAX_VALUE (d2
);
431 d1_variable
= (!d1_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
434 d2_variable
= (!d2_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
437 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
438 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
440 /* Save space: see if the result is identical to one of the args. */
441 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
442 && (d2_variable
|| d2_zero
|| !d1_variable
))
443 return build_type_attribute_variant (t1
, attributes
);
444 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
445 && (d1_variable
|| d1_zero
|| !d2_variable
))
446 return build_type_attribute_variant (t2
, attributes
);
448 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
449 return build_type_attribute_variant (t1
, attributes
);
450 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
451 return build_type_attribute_variant (t2
, attributes
);
453 /* Merge the element types, and have a size if either arg has
454 one. We may have qualifiers on the element types. To set
455 up TYPE_MAIN_VARIANT correctly, we need to form the
456 composite of the unqualified types and add the qualifiers
458 quals
= TYPE_QUALS (strip_array_types (elt
));
459 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
460 t1
= build_array_type (unqual_elt
,
461 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
467 /* Ensure a composite type involving a zero-length array type
468 is a zero-length type not an incomplete type. */
469 if (d1_zero
&& d2_zero
470 && (t1_complete
|| t2_complete
)
471 && !COMPLETE_TYPE_P (t1
))
473 TYPE_SIZE (t1
) = bitsize_zero_node
;
474 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
476 t1
= c_build_qualified_type (t1
, quals
);
477 return build_type_attribute_variant (t1
, attributes
);
483 if (attributes
!= NULL
)
485 /* Try harder not to create a new aggregate type. */
486 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
491 return build_type_attribute_variant (t1
, attributes
);
494 /* Function types: prefer the one that specified arg types.
495 If both do, merge the arg types. Also merge the return types. */
497 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
498 tree p1
= TYPE_ARG_TYPES (t1
);
499 tree p2
= TYPE_ARG_TYPES (t2
);
504 /* Save space: see if the result is identical to one of the args. */
505 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
506 return build_type_attribute_variant (t1
, attributes
);
507 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
508 return build_type_attribute_variant (t2
, attributes
);
510 /* Simple way if one arg fails to specify argument types. */
511 if (TYPE_ARG_TYPES (t1
) == NULL_TREE
)
513 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
514 t1
= build_type_attribute_variant (t1
, attributes
);
515 return qualify_type (t1
, t2
);
517 if (TYPE_ARG_TYPES (t2
) == NULL_TREE
)
519 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
520 t1
= build_type_attribute_variant (t1
, attributes
);
521 return qualify_type (t1
, t2
);
524 /* If both args specify argument types, we must merge the two
525 lists, argument by argument. */
527 for (len
= 0, newargs
= p1
;
528 newargs
&& newargs
!= void_list_node
;
529 len
++, newargs
= TREE_CHAIN (newargs
))
532 for (i
= 0; i
< len
; i
++)
533 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
537 for (; p1
&& p1
!= void_list_node
;
538 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
540 /* A null type means arg type is not specified.
541 Take whatever the other function type has. */
542 if (TREE_VALUE (p1
) == NULL_TREE
)
544 TREE_VALUE (n
) = TREE_VALUE (p2
);
547 if (TREE_VALUE (p2
) == NULL_TREE
)
549 TREE_VALUE (n
) = TREE_VALUE (p1
);
553 /* Given wait (union {union wait *u; int *i} *)
554 and wait (union wait *),
555 prefer union wait * as type of parm. */
556 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
557 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
560 tree mv2
= TREE_VALUE (p2
);
561 if (mv2
&& mv2
!= error_mark_node
562 && TREE_CODE (mv2
) != ARRAY_TYPE
)
563 mv2
= TYPE_MAIN_VARIANT (mv2
);
564 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
565 memb
; memb
= DECL_CHAIN (memb
))
567 tree mv3
= TREE_TYPE (memb
);
568 if (mv3
&& mv3
!= error_mark_node
569 && TREE_CODE (mv3
) != ARRAY_TYPE
)
570 mv3
= TYPE_MAIN_VARIANT (mv3
);
571 if (comptypes (mv3
, mv2
))
573 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
575 pedwarn (input_location
, OPT_Wpedantic
,
576 "function types not truly compatible in ISO C");
581 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
582 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
585 tree mv1
= TREE_VALUE (p1
);
586 if (mv1
&& mv1
!= error_mark_node
587 && TREE_CODE (mv1
) != ARRAY_TYPE
)
588 mv1
= TYPE_MAIN_VARIANT (mv1
);
589 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
590 memb
; memb
= DECL_CHAIN (memb
))
592 tree mv3
= TREE_TYPE (memb
);
593 if (mv3
&& mv3
!= error_mark_node
594 && TREE_CODE (mv3
) != ARRAY_TYPE
)
595 mv3
= TYPE_MAIN_VARIANT (mv3
);
596 if (comptypes (mv3
, mv1
))
598 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
600 pedwarn (input_location
, OPT_Wpedantic
,
601 "function types not truly compatible in ISO C");
606 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
610 t1
= build_function_type (valtype
, newargs
);
611 t1
= qualify_type (t1
, t2
);
616 return build_type_attribute_variant (t1
, attributes
);
621 /* Return the type of a conditional expression between pointers to
622 possibly differently qualified versions of compatible types.
624 We assume that comp_target_types has already been done and returned
625 nonzero; if that isn't so, this may crash. */
628 common_pointer_type (tree t1
, tree t2
)
631 tree pointed_to_1
, mv1
;
632 tree pointed_to_2
, mv2
;
634 unsigned target_quals
;
635 addr_space_t as1
, as2
, as_common
;
638 /* Save time if the two types are the same. */
640 if (t1
== t2
) return t1
;
642 /* If one type is nonsense, use the other. */
643 if (t1
== error_mark_node
)
645 if (t2
== error_mark_node
)
648 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
649 && TREE_CODE (t2
) == POINTER_TYPE
);
651 /* Merge the attributes. */
652 attributes
= targetm
.merge_type_attributes (t1
, t2
);
654 /* Find the composite type of the target types, and combine the
655 qualifiers of the two types' targets. Do not lose qualifiers on
656 array element types by taking the TYPE_MAIN_VARIANT. */
657 mv1
= pointed_to_1
= TREE_TYPE (t1
);
658 mv2
= pointed_to_2
= TREE_TYPE (t2
);
659 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
660 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
661 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
662 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
663 target
= composite_type (mv1
, mv2
);
665 /* Strip array types to get correct qualifier for pointers to arrays */
666 quals1
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1
));
667 quals2
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2
));
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
673 target_quals
= (quals1
& quals2
);
675 target_quals
= (quals1
| quals2
);
677 /* If the two named address spaces are different, determine the common
678 superset address space. This is guaranteed to exist due to the
679 assumption that comp_target_type returned non-zero. */
680 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
681 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
682 if (!addr_space_superset (as1
, as2
, &as_common
))
685 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
687 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
688 return build_type_attribute_variant (t1
, attributes
);
691 /* Return the common type for two arithmetic types under the usual
692 arithmetic conversions. The default conversions have already been
693 applied, and enumerated types converted to their compatible integer
694 types. The resulting type is unqualified and has no attributes.
696 This is the type for the result of most arithmetic operations
697 if the operands have the given two types. */
700 c_common_type (tree t1
, tree t2
)
702 enum tree_code code1
;
703 enum tree_code code2
;
705 /* If one type is nonsense, use the other. */
706 if (t1
== error_mark_node
)
708 if (t2
== error_mark_node
)
711 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
712 t1
= TYPE_MAIN_VARIANT (t1
);
714 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
715 t2
= TYPE_MAIN_VARIANT (t2
);
717 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
718 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
720 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
721 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
723 /* Save time if the two types are the same. */
725 if (t1
== t2
) return t1
;
727 code1
= TREE_CODE (t1
);
728 code2
= TREE_CODE (t2
);
730 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
731 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
732 || code1
== INTEGER_TYPE
);
733 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
734 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
735 || code2
== INTEGER_TYPE
);
737 /* When one operand is a decimal float type, the other operand cannot be
738 a generic float type or a complex type. We also disallow vector types
740 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
741 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
743 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
745 error ("can%'t mix operands of decimal float and vector types");
746 return error_mark_node
;
748 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
750 error ("can%'t mix operands of decimal float and complex types");
751 return error_mark_node
;
753 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
755 error ("can%'t mix operands of decimal float and other float types");
756 return error_mark_node
;
760 /* If one type is a vector type, return that type. (How the usual
761 arithmetic conversions apply to the vector types extension is not
762 precisely specified.) */
763 if (code1
== VECTOR_TYPE
)
766 if (code2
== VECTOR_TYPE
)
769 /* If one type is complex, form the common type of the non-complex
770 components, then make that complex. Use T1 or T2 if it is the
772 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
774 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
775 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
776 tree subtype
= c_common_type (subtype1
, subtype2
);
778 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
780 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
783 return build_complex_type (subtype
);
786 /* If only one is real, use it as the result. */
788 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
791 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
794 /* If both are real and either are decimal floating point types, use
795 the decimal floating point type with the greater precision. */
797 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
799 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
800 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
801 return dfloat128_type_node
;
802 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
803 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
804 return dfloat64_type_node
;
805 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
806 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
807 return dfloat32_type_node
;
810 /* Deal with fixed-point types. */
811 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
813 unsigned int unsignedp
= 0, satp
= 0;
815 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
817 m1
= SCALAR_TYPE_MODE (t1
);
818 m2
= SCALAR_TYPE_MODE (t2
);
820 /* If one input type is saturating, the result type is saturating. */
821 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
824 /* If both fixed-point types are unsigned, the result type is unsigned.
825 When mixing fixed-point and integer types, follow the sign of the
827 Otherwise, the result type is signed. */
828 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
829 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
830 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t1
))
832 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t2
)))
836 /* The result type is signed. */
839 /* If the input type is unsigned, we need to convert to the
841 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
843 enum mode_class mclass
= (enum mode_class
) 0;
844 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
846 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
850 m1
= as_a
<scalar_mode
>
851 (mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0));
853 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
855 enum mode_class mclass
= (enum mode_class
) 0;
856 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
858 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
862 m2
= as_a
<scalar_mode
>
863 (mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0));
867 if (code1
== FIXED_POINT_TYPE
)
869 fbit1
= GET_MODE_FBIT (m1
);
870 ibit1
= GET_MODE_IBIT (m1
);
875 /* Signed integers need to subtract one sign bit. */
876 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
879 if (code2
== FIXED_POINT_TYPE
)
881 fbit2
= GET_MODE_FBIT (m2
);
882 ibit2
= GET_MODE_IBIT (m2
);
887 /* Signed integers need to subtract one sign bit. */
888 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
891 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
892 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
893 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
897 /* Both real or both integers; use the one with greater precision. */
899 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
901 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
908 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
910 return long_long_unsigned_type_node
;
912 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
915 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
916 return long_long_unsigned_type_node
;
918 return long_long_integer_type_node
;
921 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
923 return long_unsigned_type_node
;
925 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
931 return long_unsigned_type_node
;
933 return long_integer_type_node
;
936 /* For floating types of the same TYPE_PRECISION (which we here
937 assume means either the same set of values, or sets of values
938 neither a subset of the other, with behavior being undefined in
939 the latter case), follow the rules from TS 18661-3: prefer
940 interchange types _FloatN, then standard types long double,
941 double, float, then extended types _FloatNx. For extended types,
942 check them starting with _Float128x as that seems most consistent
943 in spirit with preferring long double to double; for interchange
944 types, also check in that order for consistency although it's not
945 possible for more than one of them to have the same
947 tree mv1
= TYPE_MAIN_VARIANT (t1
);
948 tree mv2
= TYPE_MAIN_VARIANT (t2
);
950 for (int i
= NUM_FLOATN_TYPES
- 1; i
>= 0; i
--)
951 if (mv1
== FLOATN_TYPE_NODE (i
) || mv2
== FLOATN_TYPE_NODE (i
))
952 return FLOATN_TYPE_NODE (i
);
954 /* Likewise, prefer long double to double even if same size. */
955 if (mv1
== long_double_type_node
|| mv2
== long_double_type_node
)
956 return long_double_type_node
;
958 /* Likewise, prefer double to float even if same size.
959 We got a couple of embedded targets with 32 bit doubles, and the
960 pdp11 might have 64 bit floats. */
961 if (mv1
== double_type_node
|| mv2
== double_type_node
)
962 return double_type_node
;
964 if (mv1
== float_type_node
|| mv2
== float_type_node
)
965 return float_type_node
;
967 for (int i
= NUM_FLOATNX_TYPES
- 1; i
>= 0; i
--)
968 if (mv1
== FLOATNX_TYPE_NODE (i
) || mv2
== FLOATNX_TYPE_NODE (i
))
969 return FLOATNX_TYPE_NODE (i
);
971 /* Otherwise prefer the unsigned one. */
973 if (TYPE_UNSIGNED (t1
))
979 /* Wrapper around c_common_type that is used by c-common.c and other
980 front end optimizations that remove promotions. ENUMERAL_TYPEs
981 are allowed here and are converted to their compatible integer types.
982 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
983 preferably a non-Boolean type as the common type. */
985 common_type (tree t1
, tree t2
)
987 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
988 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
989 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
990 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
992 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
993 if (TREE_CODE (t1
) == BOOLEAN_TYPE
994 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
995 return boolean_type_node
;
997 /* If either type is BOOLEAN_TYPE, then return the other. */
998 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
1000 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
1003 return c_common_type (t1
, t2
);
1006 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1007 or various other operations. Return 2 if they are compatible
1008 but a warning may be needed if you use them together. */
1011 comptypes (tree type1
, tree type2
)
1013 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1016 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1022 /* Like comptypes, but if it returns non-zero because enum and int are
1023 compatible, it sets *ENUM_AND_INT_P to true. */
1026 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1028 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1031 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1032 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1037 /* Like comptypes, but if it returns nonzero for different types, it
1038 sets *DIFFERENT_TYPES_P to true. */
1041 comptypes_check_different_types (tree type1
, tree type2
,
1042 bool *different_types_p
)
1044 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1047 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1048 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1053 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1054 or various other operations. Return 2 if they are compatible
1055 but a warning may be needed if you use them together. If
1056 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1057 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1058 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1059 NULL, and the types are compatible but different enough not to be
1060 permitted in C11 typedef redeclarations, then this sets
1061 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1062 false, but may or may not be set if the types are incompatible.
1063 This differs from comptypes, in that we don't free the seen
1067 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1068 bool *different_types_p
)
1070 const_tree t1
= type1
;
1071 const_tree t2
= type2
;
1074 /* Suppress errors caused by previously reported errors. */
1076 if (t1
== t2
|| !t1
|| !t2
1077 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1080 /* Enumerated types are compatible with integer types, but this is
1081 not transitive: two enumerated types in the same translation unit
1082 are compatible with each other only if they are the same type. */
1084 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1086 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1087 if (TREE_CODE (t2
) != VOID_TYPE
)
1089 if (enum_and_int_p
!= NULL
)
1090 *enum_and_int_p
= true;
1091 if (different_types_p
!= NULL
)
1092 *different_types_p
= true;
1095 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1097 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1098 if (TREE_CODE (t1
) != VOID_TYPE
)
1100 if (enum_and_int_p
!= NULL
)
1101 *enum_and_int_p
= true;
1102 if (different_types_p
!= NULL
)
1103 *different_types_p
= true;
1110 /* Different classes of types can't be compatible. */
1112 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1115 /* Qualifiers must match. C99 6.7.3p9 */
1117 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1120 /* Allow for two different type nodes which have essentially the same
1121 definition. Note that we already checked for equality of the type
1122 qualifiers (just above). */
1124 if (TREE_CODE (t1
) != ARRAY_TYPE
1125 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1128 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1129 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1132 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1135 switch (TREE_CODE (t1
))
1138 case FIXED_POINT_TYPE
:
1140 /* With these nodes, we can't determine type equivalence by
1141 looking at what is stored in the nodes themselves, because
1142 two nodes might have different TYPE_MAIN_VARIANTs but still
1143 represent the same type. For example, wchar_t and int could
1144 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1145 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1146 and are distinct types. On the other hand, int and the
1149 typedef int INT __attribute((may_alias));
1151 have identical properties, different TYPE_MAIN_VARIANTs, but
1152 represent the same type. The canonical type system keeps
1153 track of equivalence in this case, so we fall back on it. */
1154 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1157 /* Do not remove mode information. */
1158 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
1160 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1161 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1162 enum_and_int_p
, different_types_p
));
1166 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1172 tree d1
= TYPE_DOMAIN (t1
);
1173 tree d2
= TYPE_DOMAIN (t2
);
1174 bool d1_variable
, d2_variable
;
1175 bool d1_zero
, d2_zero
;
1178 /* Target types must match incl. qualifiers. */
1179 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1180 && (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1182 different_types_p
)) == 0)
1185 if (different_types_p
!= NULL
1186 && (d1
== NULL_TREE
) != (d2
== NULL_TREE
))
1187 *different_types_p
= true;
1188 /* Sizes must match unless one is missing or variable. */
1189 if (d1
== NULL_TREE
|| d2
== NULL_TREE
|| d1
== d2
)
1192 d1_zero
= !TYPE_MAX_VALUE (d1
);
1193 d2_zero
= !TYPE_MAX_VALUE (d2
);
1195 d1_variable
= (!d1_zero
1196 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1197 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1198 d2_variable
= (!d2_zero
1199 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1200 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1201 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1202 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1204 if (different_types_p
!= NULL
1205 && d1_variable
!= d2_variable
)
1206 *different_types_p
= true;
1207 if (d1_variable
|| d2_variable
)
1209 if (d1_zero
&& d2_zero
)
1211 if (d1_zero
|| d2_zero
1212 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1213 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1222 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1224 tree a1
= TYPE_ATTRIBUTES (t1
);
1225 tree a2
= TYPE_ATTRIBUTES (t2
);
1227 if (! attribute_list_contained (a1
, a2
)
1228 && ! attribute_list_contained (a2
, a1
))
1232 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1234 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1240 val
= (known_eq (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1241 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1242 enum_and_int_p
, different_types_p
));
1248 return attrval
== 2 && val
== 1 ? 2 : val
;
1251 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1252 their qualifiers, except for named address spaces. If the pointers point to
1253 different named addresses, then we must determine if one address space is a
1254 subset of the other. */
1257 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1261 tree mvl
= TREE_TYPE (ttl
);
1262 tree mvr
= TREE_TYPE (ttr
);
1263 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1264 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1265 addr_space_t as_common
;
1266 bool enum_and_int_p
;
1268 /* Fail if pointers point to incompatible address spaces. */
1269 if (!addr_space_superset (asl
, asr
, &as_common
))
1272 /* For pedantic record result of comptypes on arrays before losing
1273 qualifiers on the element type below. */
1276 if (TREE_CODE (mvl
) == ARRAY_TYPE
1277 && TREE_CODE (mvr
) == ARRAY_TYPE
)
1278 val_ped
= comptypes (mvl
, mvr
);
1280 /* Qualifiers on element types of array types that are
1281 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1283 mvl
= (TYPE_ATOMIC (strip_array_types (mvl
))
1284 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1285 : TYPE_MAIN_VARIANT (mvl
));
1287 mvr
= (TYPE_ATOMIC (strip_array_types (mvr
))
1288 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1289 : TYPE_MAIN_VARIANT (mvr
));
1291 enum_and_int_p
= false;
1292 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1294 if (val
== 1 && val_ped
!= 1)
1295 pedwarn (location
, OPT_Wpedantic
, "pointers to arrays with different qualifiers "
1296 "are incompatible in ISO C");
1299 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1301 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1302 warning_at (location
, OPT_Wc___compat
,
1303 "pointer target types incompatible in C++");
1308 /* Subroutines of `comptypes'. */
1310 /* Determine whether two trees derive from the same translation unit.
1311 If the CONTEXT chain ends in a null, that tree's context is still
1312 being parsed, so if two trees have context chains ending in null,
1313 they're in the same translation unit. */
1316 same_translation_unit_p (const_tree t1
, const_tree t2
)
1318 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1319 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1321 case tcc_declaration
:
1322 t1
= DECL_CONTEXT (t1
); break;
1324 t1
= TYPE_CONTEXT (t1
); break;
1325 case tcc_exceptional
:
1326 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1327 default: gcc_unreachable ();
1330 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1331 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1333 case tcc_declaration
:
1334 t2
= DECL_CONTEXT (t2
); break;
1336 t2
= TYPE_CONTEXT (t2
); break;
1337 case tcc_exceptional
:
1338 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1339 default: gcc_unreachable ();
1345 /* Allocate the seen two types, assuming that they are compatible. */
1347 static struct tagged_tu_seen_cache
*
1348 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1350 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1351 tu
->next
= tagged_tu_seen_base
;
1355 tagged_tu_seen_base
= tu
;
1357 /* The C standard says that two structures in different translation
1358 units are compatible with each other only if the types of their
1359 fields are compatible (among other things). We assume that they
1360 are compatible until proven otherwise when building the cache.
1361 An example where this can occur is:
1366 If we are comparing this against a similar struct in another TU,
1367 and did not assume they were compatible, we end up with an infinite
1373 /* Free the seen types until we get to TU_TIL. */
1376 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1378 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1379 while (tu
!= tu_til
)
1381 const struct tagged_tu_seen_cache
*const tu1
1382 = (const struct tagged_tu_seen_cache
*) tu
;
1384 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1386 tagged_tu_seen_base
= tu_til
;
1389 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1390 compatible. If the two types are not the same (which has been
1391 checked earlier), this can only happen when multiple translation
1392 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1393 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1394 comptypes_internal. */
1397 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1398 bool *enum_and_int_p
, bool *different_types_p
)
1401 bool needs_warning
= false;
1403 /* We have to verify that the tags of the types are the same. This
1404 is harder than it looks because this may be a typedef, so we have
1405 to go look at the original type. It may even be a typedef of a
1407 In the case of compiler-created builtin structs the TYPE_DECL
1408 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1409 while (TYPE_NAME (t1
)
1410 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1411 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1412 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1414 while (TYPE_NAME (t2
)
1415 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1416 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1417 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1419 /* C90 didn't have the requirement that the two tags be the same. */
1420 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1423 /* C90 didn't say what happened if one or both of the types were
1424 incomplete; we choose to follow C99 rules here, which is that they
1426 if (TYPE_SIZE (t1
) == NULL
1427 || TYPE_SIZE (t2
) == NULL
)
1431 const struct tagged_tu_seen_cache
* tts_i
;
1432 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1433 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1437 switch (TREE_CODE (t1
))
1441 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1442 /* Speed up the case where the type values are in the same order. */
1443 tree tv1
= TYPE_VALUES (t1
);
1444 tree tv2
= TYPE_VALUES (t2
);
1451 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1453 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1455 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1462 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1466 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1472 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1478 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1480 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1482 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1493 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1494 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1500 /* Speed up the common case where the fields are in the same order. */
1501 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1502 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1506 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1508 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1509 enum_and_int_p
, different_types_p
);
1511 if (result
!= 1 && !DECL_NAME (s1
))
1519 needs_warning
= true;
1521 if (TREE_CODE (s1
) == FIELD_DECL
1522 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1523 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1531 tu
->val
= needs_warning
? 2 : 1;
1535 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1539 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1540 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1544 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1548 if (result
!= 1 && !DECL_NAME (s1
))
1556 needs_warning
= true;
1558 if (TREE_CODE (s1
) == FIELD_DECL
1559 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1560 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1572 tu
->val
= needs_warning
? 2 : 10;
1578 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1580 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1582 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1585 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1586 || DECL_NAME (s1
) != DECL_NAME (s2
))
1588 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1589 enum_and_int_p
, different_types_p
);
1593 needs_warning
= true;
1595 if (TREE_CODE (s1
) == FIELD_DECL
1596 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1597 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1603 tu
->val
= needs_warning
? 2 : 1;
1612 /* Return 1 if two function types F1 and F2 are compatible.
1613 If either type specifies no argument types,
1614 the other must specify a fixed number of self-promoting arg types.
1615 Otherwise, if one type specifies only the number of arguments,
1616 the other must specify that number of self-promoting arg types.
1617 Otherwise, the argument types must match.
1618 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1621 function_types_compatible_p (const_tree f1
, const_tree f2
,
1622 bool *enum_and_int_p
, bool *different_types_p
)
1625 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1630 ret1
= TREE_TYPE (f1
);
1631 ret2
= TREE_TYPE (f2
);
1633 /* 'volatile' qualifiers on a function's return type used to mean
1634 the function is noreturn. */
1635 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1636 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1637 if (TYPE_VOLATILE (ret1
))
1638 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1639 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1640 if (TYPE_VOLATILE (ret2
))
1641 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1642 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1643 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1647 args1
= TYPE_ARG_TYPES (f1
);
1648 args2
= TYPE_ARG_TYPES (f2
);
1650 if (different_types_p
!= NULL
1651 && (args1
== NULL_TREE
) != (args2
== NULL_TREE
))
1652 *different_types_p
= true;
1654 /* An unspecified parmlist matches any specified parmlist
1655 whose argument types don't need default promotions. */
1657 if (args1
== NULL_TREE
)
1659 if (!self_promoting_args_p (args2
))
1661 /* If one of these types comes from a non-prototype fn definition,
1662 compare that with the other type's arglist.
1663 If they don't match, ask for a warning (but no error). */
1664 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1665 && type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1666 enum_and_int_p
, different_types_p
) != 1)
1670 if (args2
== NULL_TREE
)
1672 if (!self_promoting_args_p (args1
))
1674 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1675 && type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1676 enum_and_int_p
, different_types_p
) != 1)
1681 /* Both types have argument lists: compare them and propagate results. */
1682 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1684 return val1
!= 1 ? val1
: val
;
1687 /* Check two lists of types for compatibility, returning 0 for
1688 incompatible, 1 for compatible, or 2 for compatible with
1689 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1690 comptypes_internal. */
1693 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1694 bool *enum_and_int_p
, bool *different_types_p
)
1696 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1702 tree a1
, mv1
, a2
, mv2
;
1703 if (args1
== NULL_TREE
&& args2
== NULL_TREE
)
1705 /* If one list is shorter than the other,
1706 they fail to match. */
1707 if (args1
== NULL_TREE
|| args2
== NULL_TREE
)
1709 mv1
= a1
= TREE_VALUE (args1
);
1710 mv2
= a2
= TREE_VALUE (args2
);
1711 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1712 mv1
= (TYPE_ATOMIC (mv1
)
1713 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1715 : TYPE_MAIN_VARIANT (mv1
));
1716 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1717 mv2
= (TYPE_ATOMIC (mv2
)
1718 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1720 : TYPE_MAIN_VARIANT (mv2
));
1721 /* A null pointer instead of a type
1722 means there is supposed to be an argument
1723 but nothing is specified about what type it has.
1724 So match anything that self-promotes. */
1725 if (different_types_p
!= NULL
1726 && (a1
== NULL_TREE
) != (a2
== NULL_TREE
))
1727 *different_types_p
= true;
1728 if (a1
== NULL_TREE
)
1730 if (c_type_promotes_to (a2
) != a2
)
1733 else if (a2
== NULL_TREE
)
1735 if (c_type_promotes_to (a1
) != a1
)
1738 /* If one of the lists has an error marker, ignore this arg. */
1739 else if (TREE_CODE (a1
) == ERROR_MARK
1740 || TREE_CODE (a2
) == ERROR_MARK
)
1742 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1743 different_types_p
)))
1745 if (different_types_p
!= NULL
)
1746 *different_types_p
= true;
1747 /* Allow wait (union {union wait *u; int *i} *)
1748 and wait (union wait *) to be compatible. */
1749 if (TREE_CODE (a1
) == UNION_TYPE
1750 && (TYPE_NAME (a1
) == NULL_TREE
1751 || TYPE_TRANSPARENT_AGGR (a1
))
1752 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1753 && tree_int_cst_equal (TYPE_SIZE (a1
),
1757 for (memb
= TYPE_FIELDS (a1
);
1758 memb
; memb
= DECL_CHAIN (memb
))
1760 tree mv3
= TREE_TYPE (memb
);
1761 if (mv3
&& mv3
!= error_mark_node
1762 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1763 mv3
= (TYPE_ATOMIC (mv3
)
1764 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1766 : TYPE_MAIN_VARIANT (mv3
));
1767 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1771 if (memb
== NULL_TREE
)
1774 else if (TREE_CODE (a2
) == UNION_TYPE
1775 && (TYPE_NAME (a2
) == NULL_TREE
1776 || TYPE_TRANSPARENT_AGGR (a2
))
1777 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1778 && tree_int_cst_equal (TYPE_SIZE (a2
),
1782 for (memb
= TYPE_FIELDS (a2
);
1783 memb
; memb
= DECL_CHAIN (memb
))
1785 tree mv3
= TREE_TYPE (memb
);
1786 if (mv3
&& mv3
!= error_mark_node
1787 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1788 mv3
= (TYPE_ATOMIC (mv3
)
1789 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1791 : TYPE_MAIN_VARIANT (mv3
));
1792 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1796 if (memb
== NULL_TREE
)
1803 /* comptypes said ok, but record if it said to warn. */
1807 args1
= TREE_CHAIN (args1
);
1808 args2
= TREE_CHAIN (args2
);
1812 /* Compute the size to increment a pointer by. When a function type or void
1813 type or incomplete type is passed, size_one_node is returned.
1814 This function does not emit any diagnostics; the caller is responsible
1818 c_size_in_bytes (const_tree type
)
1820 enum tree_code code
= TREE_CODE (type
);
1822 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1823 || !COMPLETE_TYPE_P (type
))
1824 return size_one_node
;
1826 /* Convert in case a char is more than one unit. */
1827 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1828 size_int (TYPE_PRECISION (char_type_node
)
1832 /* Return either DECL or its known constant value (if it has one). */
1835 decl_constant_value_1 (tree decl
, bool in_init
)
1837 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1838 TREE_CODE (decl
) != PARM_DECL
1839 && !TREE_THIS_VOLATILE (decl
)
1840 && TREE_READONLY (decl
)
1841 && DECL_INITIAL (decl
) != NULL_TREE
1842 && !error_operand_p (DECL_INITIAL (decl
))
1843 /* This is invalid if initial value is not constant.
1844 If it has either a function call, a memory reference,
1845 or a variable, then re-evaluating it could give different results. */
1846 && TREE_CONSTANT (DECL_INITIAL (decl
))
1847 /* Check for cases where this is sub-optimal, even though valid. */
1848 && (in_init
|| TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
))
1849 return DECL_INITIAL (decl
);
1853 /* Return either DECL or its known constant value (if it has one).
1854 Like the above, but always return decl outside of functions. */
1857 decl_constant_value (tree decl
)
1859 /* Don't change a variable array bound or initial value to a constant
1860 in a place where a variable is invalid. */
1861 return current_function_decl
? decl_constant_value_1 (decl
, false) : decl
;
1864 /* Convert the array expression EXP to a pointer. */
1866 array_to_pointer_conversion (location_t loc
, tree exp
)
1868 tree orig_exp
= exp
;
1869 tree type
= TREE_TYPE (exp
);
1871 tree restype
= TREE_TYPE (type
);
1874 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1876 STRIP_TYPE_NOPS (exp
);
1878 if (TREE_NO_WARNING (orig_exp
))
1879 TREE_NO_WARNING (exp
) = 1;
1881 ptrtype
= build_pointer_type (restype
);
1883 if (INDIRECT_REF_P (exp
))
1884 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1886 /* In C++ array compound literals are temporary objects unless they are
1887 const or appear in namespace scope, so they are destroyed too soon
1888 to use them for much of anything (c++/53220). */
1889 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1891 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1892 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1893 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1894 "converting an array compound literal to a pointer "
1895 "is ill-formed in C++");
1898 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, true);
1899 return convert (ptrtype
, adr
);
1902 /* Convert the function expression EXP to a pointer. */
1904 function_to_pointer_conversion (location_t loc
, tree exp
)
1906 tree orig_exp
= exp
;
1908 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1910 STRIP_TYPE_NOPS (exp
);
1912 if (TREE_NO_WARNING (orig_exp
))
1913 TREE_NO_WARNING (exp
) = 1;
1915 return build_unary_op (loc
, ADDR_EXPR
, exp
, false);
1918 /* Mark EXP as read, not just set, for set but not used -Wunused
1919 warning purposes. */
1922 mark_exp_read (tree exp
)
1924 switch (TREE_CODE (exp
))
1928 DECL_READ_P (exp
) = 1;
1937 case VIEW_CONVERT_EXPR
:
1938 mark_exp_read (TREE_OPERAND (exp
, 0));
1941 case C_MAYBE_CONST_EXPR
:
1942 mark_exp_read (TREE_OPERAND (exp
, 1));
1949 /* Perform the default conversion of arrays and functions to pointers.
1950 Return the result of converting EXP. For any other expression, just
1953 LOC is the location of the expression. */
1956 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1958 tree orig_exp
= exp
.value
;
1959 tree type
= TREE_TYPE (exp
.value
);
1960 enum tree_code code
= TREE_CODE (type
);
1966 bool not_lvalue
= false;
1967 bool lvalue_array_p
;
1969 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1970 || CONVERT_EXPR_P (exp
.value
))
1971 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1973 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1975 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1978 if (TREE_NO_WARNING (orig_exp
))
1979 TREE_NO_WARNING (exp
.value
) = 1;
1981 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1982 if (!flag_isoc99
&& !lvalue_array_p
)
1984 /* Before C99, non-lvalue arrays do not decay to pointers.
1985 Normally, using such an array would be invalid; but it can
1986 be used correctly inside sizeof or as a statement expression.
1987 Thus, do not give an error here; an error will result later. */
1991 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1995 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
2005 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
2007 mark_exp_read (exp
.value
);
2008 return default_function_array_conversion (loc
, exp
);
2011 /* Return whether EXPR should be treated as an atomic lvalue for the
2012 purposes of load and store handling. */
2015 really_atomic_lvalue (tree expr
)
2017 if (error_operand_p (expr
))
2019 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
2021 if (!lvalue_p (expr
))
2024 /* Ignore _Atomic on register variables, since their addresses can't
2025 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2026 sequences wouldn't work. Ignore _Atomic on structures containing
2027 bit-fields, since accessing elements of atomic structures or
2028 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2029 it's undefined at translation time or execution time, and the
2030 normal atomic sequences again wouldn't work. */
2031 while (handled_component_p (expr
))
2033 if (TREE_CODE (expr
) == COMPONENT_REF
2034 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
2036 expr
= TREE_OPERAND (expr
, 0);
2038 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
2043 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2044 including converting functions and arrays to pointers if CONVERT_P.
2045 If READ_P, also mark the expression as having been read. */
2048 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
2049 bool convert_p
, bool read_p
)
2052 mark_exp_read (exp
.value
);
2054 exp
= default_function_array_conversion (loc
, exp
);
2055 if (really_atomic_lvalue (exp
.value
))
2057 vec
<tree
, va_gc
> *params
;
2058 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2059 tree expr_type
= TREE_TYPE (exp
.value
);
2060 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, false);
2061 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2063 gcc_assert (TYPE_ATOMIC (expr_type
));
2065 /* Expansion of a generic atomic load may require an addition
2066 element, so allocate enough to prevent a resize. */
2067 vec_alloc (params
, 4);
2069 /* Remove the qualifiers for the rest of the expressions and
2070 create the VAL temp variable to hold the RHS. */
2071 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2072 tmp
= create_tmp_var_raw (nonatomic_type
);
2073 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, false);
2074 TREE_ADDRESSABLE (tmp
) = 1;
2075 TREE_NO_WARNING (tmp
) = 1;
2077 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2078 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2079 params
->quick_push (expr_addr
);
2080 params
->quick_push (tmp_addr
);
2081 params
->quick_push (seq_cst
);
2082 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2084 /* EXPR is always read. */
2085 mark_exp_read (exp
.value
);
2087 /* Return tmp which contains the value loaded. */
2088 exp
.value
= build4 (TARGET_EXPR
, nonatomic_type
, tmp
, func_call
,
2089 NULL_TREE
, NULL_TREE
);
2094 /* EXP is an expression of integer type. Apply the integer promotions
2095 to it and return the promoted value. */
2098 perform_integral_promotions (tree exp
)
2100 tree type
= TREE_TYPE (exp
);
2101 enum tree_code code
= TREE_CODE (type
);
2103 gcc_assert (INTEGRAL_TYPE_P (type
));
2105 /* Normally convert enums to int,
2106 but convert wide enums to something wider. */
2107 if (code
== ENUMERAL_TYPE
)
2109 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2110 TYPE_PRECISION (integer_type_node
)),
2111 ((TYPE_PRECISION (type
)
2112 >= TYPE_PRECISION (integer_type_node
))
2113 && TYPE_UNSIGNED (type
)));
2115 return convert (type
, exp
);
2118 /* ??? This should no longer be needed now bit-fields have their
2120 if (TREE_CODE (exp
) == COMPONENT_REF
2121 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2122 /* If it's thinner than an int, promote it like a
2123 c_promoting_integer_type_p, otherwise leave it alone. */
2124 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2125 TYPE_PRECISION (integer_type_node
)) < 0)
2126 return convert (integer_type_node
, exp
);
2128 if (c_promoting_integer_type_p (type
))
2130 /* Preserve unsignedness if not really getting any wider. */
2131 if (TYPE_UNSIGNED (type
)
2132 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2133 return convert (unsigned_type_node
, exp
);
2135 return convert (integer_type_node
, exp
);
2142 /* Perform default promotions for C data used in expressions.
2143 Enumeral types or short or char are converted to int.
2144 In addition, manifest constants symbols are replaced by their values. */
2147 default_conversion (tree exp
)
2150 tree type
= TREE_TYPE (exp
);
2151 enum tree_code code
= TREE_CODE (type
);
2154 mark_exp_read (exp
);
2156 /* Functions and arrays have been converted during parsing. */
2157 gcc_assert (code
!= FUNCTION_TYPE
);
2158 if (code
== ARRAY_TYPE
)
2161 /* Constants can be used directly unless they're not loadable. */
2162 if (TREE_CODE (exp
) == CONST_DECL
)
2163 exp
= DECL_INITIAL (exp
);
2165 /* Strip no-op conversions. */
2167 STRIP_TYPE_NOPS (exp
);
2169 if (TREE_NO_WARNING (orig_exp
))
2170 TREE_NO_WARNING (exp
) = 1;
2172 if (code
== VOID_TYPE
)
2174 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2175 "void value not ignored as it ought to be");
2176 return error_mark_node
;
2179 exp
= require_complete_type (EXPR_LOC_OR_LOC (exp
, input_location
), exp
);
2180 if (exp
== error_mark_node
)
2181 return error_mark_node
;
2183 promoted_type
= targetm
.promoted_type (type
);
2185 return convert (promoted_type
, exp
);
2187 if (INTEGRAL_TYPE_P (type
))
2188 return perform_integral_promotions (exp
);
2193 /* Look up COMPONENT in a structure or union TYPE.
2195 If the component name is not found, returns NULL_TREE. Otherwise,
2196 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2197 stepping down the chain to the component, which is in the last
2198 TREE_VALUE of the list. Normally the list is of length one, but if
2199 the component is embedded within (nested) anonymous structures or
2200 unions, the list steps down the chain to the component. */
2203 lookup_field (tree type
, tree component
)
2207 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2208 to the field elements. Use a binary search on this array to quickly
2209 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2210 will always be set for structures which have many elements.
2212 Duplicate field checking replaces duplicates with NULL_TREE so
2213 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2214 case just iterate using DECL_CHAIN. */
2216 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
2220 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2222 field
= TYPE_FIELDS (type
);
2224 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2225 while (top
- bot
> 1)
2227 half
= (top
- bot
+ 1) >> 1;
2228 field
= field_array
[bot
+half
];
2230 if (DECL_NAME (field
) == NULL_TREE
)
2232 /* Step through all anon unions in linear fashion. */
2233 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2235 field
= field_array
[bot
++];
2236 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2238 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2241 return tree_cons (NULL_TREE
, field
, anon
);
2243 /* The Plan 9 compiler permits referring
2244 directly to an anonymous struct/union field
2245 using a typedef name. */
2246 if (flag_plan9_extensions
2247 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2248 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2250 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2256 /* Entire record is only anon unions. */
2260 /* Restart the binary search, with new lower bound. */
2264 if (DECL_NAME (field
) == component
)
2266 if (DECL_NAME (field
) < component
)
2272 if (DECL_NAME (field_array
[bot
]) == component
)
2273 field
= field_array
[bot
];
2274 else if (DECL_NAME (field
) != component
)
2279 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2281 if (DECL_NAME (field
) == NULL_TREE
2282 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2284 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2287 return tree_cons (NULL_TREE
, field
, anon
);
2289 /* The Plan 9 compiler permits referring directly to an
2290 anonymous struct/union field using a typedef
2292 if (flag_plan9_extensions
2293 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2294 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2295 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2300 if (DECL_NAME (field
) == component
)
2304 if (field
== NULL_TREE
)
2308 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2311 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2314 lookup_field_fuzzy_find_candidates (tree type
, tree component
,
2315 vec
<tree
> *candidates
)
2318 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2320 if (DECL_NAME (field
) == NULL_TREE
2321 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2322 lookup_field_fuzzy_find_candidates (TREE_TYPE (field
), component
,
2325 if (DECL_NAME (field
))
2326 candidates
->safe_push (DECL_NAME (field
));
2330 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2331 rather than returning a TREE_LIST for an exact match. */
2334 lookup_field_fuzzy (tree type
, tree component
)
2336 gcc_assert (TREE_CODE (component
) == IDENTIFIER_NODE
);
2338 /* First, gather a list of candidates. */
2339 auto_vec
<tree
> candidates
;
2341 lookup_field_fuzzy_find_candidates (type
, component
,
2344 return find_closest_identifier (component
, &candidates
);
2347 /* Support function for build_component_ref's error-handling.
2349 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2350 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2353 should_suggest_deref_p (tree datum_type
)
2355 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2356 allows "." for ptrs; we could be handling a failed attempt
2357 to access a property. */
2358 if (c_dialect_objc ())
2361 /* Only suggest it for pointers... */
2362 if (TREE_CODE (datum_type
) != POINTER_TYPE
)
2365 /* ...to structs/unions. */
2366 tree underlying_type
= TREE_TYPE (datum_type
);
2367 enum tree_code code
= TREE_CODE (underlying_type
);
2368 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2374 /* Make an expression to refer to the COMPONENT field of structure or
2375 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2376 location of the COMPONENT_REF. COMPONENT_LOC is the location
2380 build_component_ref (location_t loc
, tree datum
, tree component
,
2381 location_t component_loc
)
2383 tree type
= TREE_TYPE (datum
);
2384 enum tree_code code
= TREE_CODE (type
);
2387 bool datum_lvalue
= lvalue_p (datum
);
2389 if (!objc_is_public (datum
, component
))
2390 return error_mark_node
;
2392 /* Detect Objective-C property syntax object.property. */
2393 if (c_dialect_objc ()
2394 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2397 /* See if there is a field or component with name COMPONENT. */
2399 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2401 if (!COMPLETE_TYPE_P (type
))
2403 c_incomplete_type_error (loc
, NULL_TREE
, type
);
2404 return error_mark_node
;
2407 field
= lookup_field (type
, component
);
2411 tree guessed_id
= lookup_field_fuzzy (type
, component
);
2414 /* Attempt to provide a fixit replacement hint, if
2415 we have a valid range for the component. */
2416 location_t reported_loc
2417 = (component_loc
!= UNKNOWN_LOCATION
) ? component_loc
: loc
;
2418 gcc_rich_location
rich_loc (reported_loc
);
2419 if (component_loc
!= UNKNOWN_LOCATION
)
2420 rich_loc
.add_fixit_misspelled_id (component_loc
, guessed_id
);
2421 error_at (&rich_loc
,
2422 "%qT has no member named %qE; did you mean %qE?",
2423 type
, component
, guessed_id
);
2426 error_at (loc
, "%qT has no member named %qE", type
, component
);
2427 return error_mark_node
;
2430 /* Accessing elements of atomic structures or unions is undefined
2431 behavior (C11 6.5.2.3#5). */
2432 if (TYPE_ATOMIC (type
) && c_inhibit_evaluation_warnings
== 0)
2434 if (code
== RECORD_TYPE
)
2435 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2436 "structure %qE", component
, datum
);
2438 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2439 "union %qE", component
, datum
);
2442 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2443 This might be better solved in future the way the C++ front
2444 end does it - by giving the anonymous entities each a
2445 separate name and type, and then have build_component_ref
2446 recursively call itself. We can't do that here. */
2449 tree subdatum
= TREE_VALUE (field
);
2452 bool use_datum_quals
;
2454 if (TREE_TYPE (subdatum
) == error_mark_node
)
2455 return error_mark_node
;
2457 /* If this is an rvalue, it does not have qualifiers in C
2458 standard terms and we must avoid propagating such
2459 qualifiers down to a non-lvalue array that is then
2460 converted to a pointer. */
2461 use_datum_quals
= (datum_lvalue
2462 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2464 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2465 if (use_datum_quals
)
2466 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2467 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2469 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2471 SET_EXPR_LOCATION (ref
, loc
);
2472 if (TREE_READONLY (subdatum
)
2473 || (use_datum_quals
&& TREE_READONLY (datum
)))
2474 TREE_READONLY (ref
) = 1;
2475 if (TREE_THIS_VOLATILE (subdatum
)
2476 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2477 TREE_THIS_VOLATILE (ref
) = 1;
2479 if (TREE_DEPRECATED (subdatum
))
2480 warn_deprecated_use (subdatum
, NULL_TREE
);
2484 field
= TREE_CHAIN (field
);
2490 else if (should_suggest_deref_p (type
))
2492 /* Special-case the error message for "ptr.field" for the case
2493 where the user has confused "." vs "->". */
2494 rich_location
richloc (line_table
, loc
);
2495 /* "loc" should be the "." token. */
2496 richloc
.add_fixit_replace ("->");
2498 "%qE is a pointer; did you mean to use %<->%>?",
2500 return error_mark_node
;
2502 else if (code
!= ERROR_MARK
)
2504 "request for member %qE in something not a structure or union",
2507 return error_mark_node
;
2510 /* Given an expression PTR for a pointer, return an expression
2511 for the value pointed to.
2512 ERRORSTRING is the name of the operator to appear in error messages.
2514 LOC is the location to use for the generated tree. */
2517 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2519 tree pointer
= default_conversion (ptr
);
2520 tree type
= TREE_TYPE (pointer
);
2523 if (TREE_CODE (type
) == POINTER_TYPE
)
2525 if (CONVERT_EXPR_P (pointer
)
2526 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2528 /* If a warning is issued, mark it to avoid duplicates from
2529 the backend. This only needs to be done at
2530 warn_strict_aliasing > 2. */
2531 if (warn_strict_aliasing
> 2)
2532 if (strict_aliasing_warning (EXPR_LOCATION (pointer
),
2533 type
, TREE_OPERAND (pointer
, 0)))
2534 TREE_NO_WARNING (pointer
) = 1;
2537 if (TREE_CODE (pointer
) == ADDR_EXPR
2538 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2539 == TREE_TYPE (type
)))
2541 ref
= TREE_OPERAND (pointer
, 0);
2542 protected_set_expr_location (ref
, loc
);
2547 tree t
= TREE_TYPE (type
);
2549 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2551 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2553 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)))
2555 error_at (loc
, "dereferencing pointer to incomplete type "
2557 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)) = 1;
2559 return error_mark_node
;
2561 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2562 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2564 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2565 so that we get the proper error message if the result is used
2566 to assign to. Also, &* is supposed to be a no-op.
2567 And ANSI C seems to specify that the type of the result
2568 should be the const type. */
2569 /* A de-reference of a pointer to const is not a const. It is valid
2570 to change it via some other pointer. */
2571 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2572 TREE_SIDE_EFFECTS (ref
)
2573 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2574 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2575 protected_set_expr_location (ref
, loc
);
2579 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2580 invalid_indirection_error (loc
, type
, errstring
);
2582 return error_mark_node
;
2585 /* This handles expressions of the form "a[i]", which denotes
2588 This is logically equivalent in C to *(a+i), but we may do it differently.
2589 If A is a variable or a member, we generate a primitive ARRAY_REF.
2590 This avoids forcing the array out of registers, and can work on
2591 arrays that are not lvalues (for example, members of structures returned
2594 For vector types, allow vector[i] but not i[vector], and create
2595 *(((type*)&vectortype) + i) for the expression.
2597 LOC is the location to use for the returned expression. */
2600 build_array_ref (location_t loc
, tree array
, tree index
)
2603 bool swapped
= false;
2604 if (TREE_TYPE (array
) == error_mark_node
2605 || TREE_TYPE (index
) == error_mark_node
)
2606 return error_mark_node
;
2608 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2609 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2610 /* Allow vector[index] but not index[vector]. */
2611 && !VECTOR_TYPE_P (TREE_TYPE (array
)))
2613 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2614 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2617 "subscripted value is neither array nor pointer nor vector");
2619 return error_mark_node
;
2621 std::swap (array
, index
);
2625 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2627 error_at (loc
, "array subscript is not an integer");
2628 return error_mark_node
;
2631 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2633 error_at (loc
, "subscripted value is pointer to function");
2634 return error_mark_node
;
2637 /* ??? Existing practice has been to warn only when the char
2638 index is syntactically the index, not for char[array]. */
2640 warn_array_subscript_with_type_char (loc
, index
);
2642 /* Apply default promotions *after* noticing character types. */
2643 index
= default_conversion (index
);
2644 if (index
== error_mark_node
)
2645 return error_mark_node
;
2647 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2649 bool was_vector
= VECTOR_TYPE_P (TREE_TYPE (array
));
2650 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, index
);
2652 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2656 /* An array that is indexed by a non-constant
2657 cannot be stored in a register; we must be able to do
2658 address arithmetic on its address.
2659 Likewise an array of elements of variable size. */
2660 if (TREE_CODE (index
) != INTEGER_CST
2661 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2662 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2664 if (!c_mark_addressable (array
, true))
2665 return error_mark_node
;
2667 /* An array that is indexed by a constant value which is not within
2668 the array bounds cannot be stored in a register either; because we
2669 would get a crash in store_bit_field/extract_bit_field when trying
2670 to access a non-existent part of the register. */
2671 if (TREE_CODE (index
) == INTEGER_CST
2672 && TYPE_DOMAIN (TREE_TYPE (array
))
2673 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2675 if (!c_mark_addressable (array
))
2676 return error_mark_node
;
2679 if ((pedantic
|| warn_c90_c99_compat
)
2683 while (TREE_CODE (foo
) == COMPONENT_REF
)
2684 foo
= TREE_OPERAND (foo
, 0);
2685 if (VAR_P (foo
) && C_DECL_REGISTER (foo
))
2686 pedwarn (loc
, OPT_Wpedantic
,
2687 "ISO C forbids subscripting %<register%> array");
2688 else if (!lvalue_p (foo
))
2689 pedwarn_c90 (loc
, OPT_Wpedantic
,
2690 "ISO C90 forbids subscripting non-lvalue "
2694 type
= TREE_TYPE (TREE_TYPE (array
));
2695 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2696 /* Array ref is const/volatile if the array elements are
2697 or if the array is. */
2698 TREE_READONLY (rval
)
2699 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2700 | TREE_READONLY (array
));
2701 TREE_SIDE_EFFECTS (rval
)
2702 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2703 | TREE_SIDE_EFFECTS (array
));
2704 TREE_THIS_VOLATILE (rval
)
2705 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2706 /* This was added by rms on 16 Nov 91.
2707 It fixes vol struct foo *a; a->elts[1]
2708 in an inline function.
2709 Hope it doesn't break something else. */
2710 | TREE_THIS_VOLATILE (array
));
2711 ret
= require_complete_type (loc
, rval
);
2712 protected_set_expr_location (ret
, loc
);
2714 ret
= non_lvalue_loc (loc
, ret
);
2719 tree ar
= default_conversion (array
);
2721 if (ar
== error_mark_node
)
2724 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2725 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2727 ret
= build_indirect_ref (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
,
2731 ret
= non_lvalue_loc (loc
, ret
);
2736 /* Build an external reference to identifier ID. FUN indicates
2737 whether this will be used for a function call. LOC is the source
2738 location of the identifier. This sets *TYPE to the type of the
2739 identifier, which is not the same as the type of the returned value
2740 for CONST_DECLs defined as enum constants. If the type of the
2741 identifier is not available, *TYPE is set to NULL. */
2743 build_external_ref (location_t loc
, tree id
, bool fun
, tree
*type
)
2746 tree decl
= lookup_name (id
);
2748 /* In Objective-C, an instance variable (ivar) may be preferred to
2749 whatever lookup_name() found. */
2750 decl
= objc_lookup_ivar (decl
, id
);
2753 if (decl
&& decl
!= error_mark_node
)
2756 *type
= TREE_TYPE (ref
);
2759 /* Implicit function declaration. */
2760 ref
= implicitly_declare (loc
, id
);
2761 else if (decl
== error_mark_node
)
2762 /* Don't complain about something that's already been
2763 complained about. */
2764 return error_mark_node
;
2767 undeclared_variable (loc
, id
);
2768 return error_mark_node
;
2771 if (TREE_TYPE (ref
) == error_mark_node
)
2772 return error_mark_node
;
2774 if (TREE_DEPRECATED (ref
))
2775 warn_deprecated_use (ref
, NULL_TREE
);
2777 /* Recursive call does not count as usage. */
2778 if (ref
!= current_function_decl
)
2780 TREE_USED (ref
) = 1;
2783 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2785 if (!in_sizeof
&& !in_typeof
)
2786 C_DECL_USED (ref
) = 1;
2787 else if (DECL_INITIAL (ref
) == NULL_TREE
2788 && DECL_EXTERNAL (ref
)
2789 && !TREE_PUBLIC (ref
))
2790 record_maybe_used_decl (ref
);
2793 if (TREE_CODE (ref
) == CONST_DECL
)
2795 used_types_insert (TREE_TYPE (ref
));
2798 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2799 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2801 warning_at (loc
, OPT_Wc___compat
,
2802 ("enum constant defined in struct or union "
2803 "is not visible in C++"));
2804 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2807 ref
= DECL_INITIAL (ref
);
2808 TREE_CONSTANT (ref
) = 1;
2810 else if (current_function_decl
!= NULL_TREE
2811 && !DECL_FILE_SCOPE_P (current_function_decl
)
2812 && (VAR_OR_FUNCTION_DECL_P (ref
)
2813 || TREE_CODE (ref
) == PARM_DECL
))
2815 tree context
= decl_function_context (ref
);
2817 if (context
!= NULL_TREE
&& context
!= current_function_decl
)
2818 DECL_NONLOCAL (ref
) = 1;
2820 /* C99 6.7.4p3: An inline definition of a function with external
2821 linkage ... shall not contain a reference to an identifier with
2822 internal linkage. */
2823 else if (current_function_decl
!= NULL_TREE
2824 && DECL_DECLARED_INLINE_P (current_function_decl
)
2825 && DECL_EXTERNAL (current_function_decl
)
2826 && VAR_OR_FUNCTION_DECL_P (ref
)
2827 && (!VAR_P (ref
) || TREE_STATIC (ref
))
2828 && ! TREE_PUBLIC (ref
)
2829 && DECL_CONTEXT (ref
) != current_function_decl
)
2830 record_inline_static (loc
, current_function_decl
, ref
,
2836 /* Record details of decls possibly used inside sizeof or typeof. */
2837 struct maybe_used_decl
2841 /* The level seen at (in_sizeof + in_typeof). */
2843 /* The next one at this level or above, or NULL. */
2844 struct maybe_used_decl
*next
;
2847 static struct maybe_used_decl
*maybe_used_decls
;
2849 /* Record that DECL, an undefined static function reference seen
2850 inside sizeof or typeof, might be used if the operand of sizeof is
2851 a VLA type or the operand of typeof is a variably modified
2855 record_maybe_used_decl (tree decl
)
2857 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2859 t
->level
= in_sizeof
+ in_typeof
;
2860 t
->next
= maybe_used_decls
;
2861 maybe_used_decls
= t
;
2864 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2865 USED is false, just discard them. If it is true, mark them used
2866 (if no longer inside sizeof or typeof) or move them to the next
2867 level up (if still inside sizeof or typeof). */
2870 pop_maybe_used (bool used
)
2872 struct maybe_used_decl
*p
= maybe_used_decls
;
2873 int cur_level
= in_sizeof
+ in_typeof
;
2874 while (p
&& p
->level
> cur_level
)
2879 C_DECL_USED (p
->decl
) = 1;
2881 p
->level
= cur_level
;
2885 if (!used
|| cur_level
== 0)
2886 maybe_used_decls
= p
;
2889 /* Return the result of sizeof applied to EXPR. */
2892 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2895 if (expr
.value
== error_mark_node
)
2897 ret
.value
= error_mark_node
;
2898 ret
.original_code
= ERROR_MARK
;
2899 ret
.original_type
= NULL
;
2900 pop_maybe_used (false);
2904 bool expr_const_operands
= true;
2906 if (TREE_CODE (expr
.value
) == PARM_DECL
2907 && C_ARRAY_PARAMETER (expr
.value
))
2909 auto_diagnostic_group d
;
2910 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2911 "%<sizeof%> on array function parameter %qE will "
2912 "return size of %qT", expr
.value
,
2913 TREE_TYPE (expr
.value
)))
2914 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2916 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2917 &expr_const_operands
);
2918 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2919 c_last_sizeof_arg
= expr
.value
;
2920 c_last_sizeof_loc
= loc
;
2921 ret
.original_code
= SIZEOF_EXPR
;
2922 ret
.original_type
= NULL
;
2923 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2925 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2926 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2927 folded_expr
, ret
.value
);
2928 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2929 SET_EXPR_LOCATION (ret
.value
, loc
);
2931 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2936 /* Return the result of sizeof applied to T, a structure for the type
2937 name passed to sizeof (rather than the type itself). LOC is the
2938 location of the original expression. */
2941 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2945 tree type_expr
= NULL_TREE
;
2946 bool type_expr_const
= true;
2947 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2948 ret
.value
= c_sizeof (loc
, type
);
2949 c_last_sizeof_arg
= type
;
2950 c_last_sizeof_loc
= loc
;
2951 ret
.original_code
= SIZEOF_EXPR
;
2952 ret
.original_type
= NULL
;
2953 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2954 && c_vla_type_p (type
))
2956 /* If the type is a [*] array, it is a VLA but is represented as
2957 having a size of zero. In such a case we must ensure that
2958 the result of sizeof does not get folded to a constant by
2959 c_fully_fold, because if the size is evaluated the result is
2960 not constant and so constraints on zero or negative size
2961 arrays must not be applied when this sizeof call is inside
2962 another array declarator. */
2964 type_expr
= integer_zero_node
;
2965 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2966 type_expr
, ret
.value
);
2967 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2969 pop_maybe_used (type
!= error_mark_node
2970 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2974 /* Build a function call to function FUNCTION with parameters PARAMS.
2975 The function call is at LOC.
2976 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2977 TREE_VALUE of each node is a parameter-expression.
2978 FUNCTION's data type may be a function type or a pointer-to-function. */
2981 build_function_call (location_t loc
, tree function
, tree params
)
2983 vec
<tree
, va_gc
> *v
;
2986 vec_alloc (v
, list_length (params
));
2987 for (; params
; params
= TREE_CHAIN (params
))
2988 v
->quick_push (TREE_VALUE (params
));
2989 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2994 /* Give a note about the location of the declaration of DECL. */
2997 inform_declaration (tree decl
)
2999 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_IS_BUILTIN (decl
)))
3000 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
3003 /* Build a function call to function FUNCTION with parameters PARAMS.
3004 ORIGTYPES, if not NULL, is a vector of types; each element is
3005 either NULL or the original type of the corresponding element in
3006 PARAMS. The original type may differ from TREE_TYPE of the
3007 parameter for enums. FUNCTION's data type may be a function type
3008 or pointer-to-function. This function changes the elements of
3012 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3013 tree function
, vec
<tree
, va_gc
> *params
,
3014 vec
<tree
, va_gc
> *origtypes
)
3016 tree fntype
, fundecl
= NULL_TREE
;
3017 tree name
= NULL_TREE
, result
;
3023 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3024 STRIP_TYPE_NOPS (function
);
3026 /* Convert anything with function type to a pointer-to-function. */
3027 if (TREE_CODE (function
) == FUNCTION_DECL
)
3029 name
= DECL_NAME (function
);
3032 tm_malloc_replacement (function
);
3034 /* Atomic functions have type checking/casting already done. They are
3035 often rewritten and don't match the original parameter list. */
3036 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
3039 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
3040 function
= function_to_pointer_conversion (loc
, function
);
3042 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3043 expressions, like those used for ObjC messenger dispatches. */
3044 if (params
&& !params
->is_empty ())
3045 function
= objc_rewrite_function_call (function
, (*params
)[0]);
3047 function
= c_fully_fold (function
, false, NULL
);
3049 fntype
= TREE_TYPE (function
);
3051 if (TREE_CODE (fntype
) == ERROR_MARK
)
3052 return error_mark_node
;
3054 if (!(TREE_CODE (fntype
) == POINTER_TYPE
3055 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
3057 if (!flag_diagnostics_show_caret
)
3059 "called object %qE is not a function or function pointer",
3061 else if (DECL_P (function
))
3064 "called object %qD is not a function or function pointer",
3066 inform_declaration (function
);
3070 "called object is not a function or function pointer");
3071 return error_mark_node
;
3074 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
3075 current_function_returns_abnormally
= 1;
3077 /* fntype now gets the type of function pointed to. */
3078 fntype
= TREE_TYPE (fntype
);
3080 /* Convert the parameters to the types declared in the
3081 function prototype, or apply default promotions. */
3083 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
3084 origtypes
, function
, fundecl
);
3086 return error_mark_node
;
3088 /* Check that the function is called through a compatible prototype.
3089 If it is not, warn. */
3090 if (CONVERT_EXPR_P (function
)
3091 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
3092 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
3093 && !comptypes (fntype
, TREE_TYPE (tem
)))
3095 tree return_type
= TREE_TYPE (fntype
);
3097 /* This situation leads to run-time undefined behavior. We can't,
3098 therefore, simply error unless we can prove that all possible
3099 executions of the program must execute the code. */
3100 warning_at (loc
, 0, "function called through a non-compatible type");
3102 if (VOID_TYPE_P (return_type
)
3103 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
3105 "function with qualified void return type called");
3108 argarray
= vec_safe_address (params
);
3110 /* Check that arguments to builtin functions match the expectations. */
3111 if (fundecl
&& fndecl_built_in_p (fundecl
, BUILT_IN_NORMAL
)
3112 && !check_builtin_function_arguments (loc
, arg_loc
, fundecl
, nargs
,
3114 return error_mark_node
;
3116 /* Check that the arguments to the function are valid. */
3117 bool warned_p
= check_function_arguments (loc
, fundecl
, fntype
,
3118 nargs
, argarray
, &arg_loc
);
3120 if (name
!= NULL_TREE
3121 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
3123 if (require_constant_value
)
3125 = fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
3126 function
, nargs
, argarray
);
3128 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
3129 function
, nargs
, argarray
);
3130 if (TREE_CODE (result
) == NOP_EXPR
3131 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
3132 STRIP_TYPE_NOPS (result
);
3135 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
3136 function
, nargs
, argarray
);
3137 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3139 if (warned_p
&& TREE_CODE (result
) == CALL_EXPR
)
3140 TREE_NO_WARNING (result
) = 1;
3142 /* In this improbable scenario, a nested function returns a VM type.
3143 Create a TARGET_EXPR so that the call always has a LHS, much as
3144 what the C++ FE does for functions returning non-PODs. */
3145 if (variably_modified_type_p (TREE_TYPE (fntype
), NULL_TREE
))
3147 tree tmp
= create_tmp_var_raw (TREE_TYPE (fntype
));
3148 result
= build4 (TARGET_EXPR
, TREE_TYPE (fntype
), tmp
, result
,
3149 NULL_TREE
, NULL_TREE
);
3152 if (VOID_TYPE_P (TREE_TYPE (result
)))
3154 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
3156 "function with qualified void return type called");
3159 return require_complete_type (loc
, result
);
3162 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3165 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3166 tree function
, vec
<tree
, va_gc
> *params
,
3167 vec
<tree
, va_gc
> *origtypes
)
3169 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3170 STRIP_TYPE_NOPS (function
);
3172 /* Convert anything with function type to a pointer-to-function. */
3173 if (TREE_CODE (function
) == FUNCTION_DECL
)
3175 /* Implement type-directed function overloading for builtins.
3176 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3177 handle all the type checking. The result is a complete expression
3178 that implements this function call. */
3179 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3183 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3186 /* Convert the argument expressions in the vector VALUES
3187 to the types in the list TYPELIST.
3189 If TYPELIST is exhausted, or when an element has NULL as its type,
3190 perform the default conversions.
3192 ORIGTYPES is the original types of the expressions in VALUES. This
3193 holds the type of enum values which have been converted to integral
3194 types. It may be NULL.
3196 FUNCTION is a tree for the called function. It is used only for
3197 error messages, where it is formatted with %qE.
3199 This is also where warnings about wrong number of args are generated.
3201 ARG_LOC are locations of function arguments (if any).
3203 Returns the actual number of arguments processed (which may be less
3204 than the length of VALUES in some error situations), or -1 on
3208 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3209 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3210 tree function
, tree fundecl
)
3213 unsigned int parmnum
;
3214 bool error_args
= false;
3215 const bool type_generic
= fundecl
3216 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3217 bool type_generic_remove_excess_precision
= false;
3218 bool type_generic_overflow_p
= false;
3221 /* Change pointer to function to the function itself for
3223 if (TREE_CODE (function
) == ADDR_EXPR
3224 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3225 function
= TREE_OPERAND (function
, 0);
3227 /* Handle an ObjC selector specially for diagnostics. */
3228 selector
= objc_message_selector ();
3230 /* For type-generic built-in functions, determine whether excess
3231 precision should be removed (classification) or not
3234 && fndecl_built_in_p (fundecl
, BUILT_IN_NORMAL
))
3236 switch (DECL_FUNCTION_CODE (fundecl
))
3238 case BUILT_IN_ISFINITE
:
3239 case BUILT_IN_ISINF
:
3240 case BUILT_IN_ISINF_SIGN
:
3241 case BUILT_IN_ISNAN
:
3242 case BUILT_IN_ISNORMAL
:
3243 case BUILT_IN_FPCLASSIFY
:
3244 type_generic_remove_excess_precision
= true;
3247 case BUILT_IN_ADD_OVERFLOW_P
:
3248 case BUILT_IN_SUB_OVERFLOW_P
:
3249 case BUILT_IN_MUL_OVERFLOW_P
:
3250 /* The last argument of these type-generic builtins
3251 should not be promoted. */
3252 type_generic_overflow_p
= true;
3260 /* Scan the given expressions and types, producing individual
3261 converted arguments. */
3263 for (typetail
= typelist
, parmnum
= 0;
3264 values
&& values
->iterate (parmnum
, &val
);
3267 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3268 tree valtype
= TREE_TYPE (val
);
3269 tree rname
= function
;
3270 int argnum
= parmnum
+ 1;
3271 const char *invalid_func_diag
;
3272 bool excess_precision
= false;
3275 /* Some __atomic_* builtins have additional hidden argument at
3278 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3279 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3282 if (type
== void_type_node
)
3285 error_at (loc
, "too many arguments to method %qE", selector
);
3287 error_at (loc
, "too many arguments to function %qE", function
);
3288 inform_declaration (fundecl
);
3289 return error_args
? -1 : (int) parmnum
;
3292 if (selector
&& argnum
> 2)
3298 npc
= null_pointer_constant_p (val
);
3300 /* If there is excess precision and a prototype, convert once to
3301 the required type rather than converting via the semantic
3302 type. Likewise without a prototype a float value represented
3303 as long double should be converted once to double. But for
3304 type-generic classification functions excess precision must
3306 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3307 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3309 val
= TREE_OPERAND (val
, 0);
3310 excess_precision
= true;
3312 val
= c_fully_fold (val
, false, NULL
);
3313 STRIP_TYPE_NOPS (val
);
3315 val
= require_complete_type (ploc
, val
);
3317 /* Some floating-point arguments must be promoted to double when
3318 no type is specified by a prototype. This applies to
3319 arguments of type float, and to architecture-specific types
3320 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3321 bool promote_float_arg
= false;
3322 if (type
== NULL_TREE
3323 && TREE_CODE (valtype
) == REAL_TYPE
3324 && (TYPE_PRECISION (valtype
)
3325 <= TYPE_PRECISION (double_type_node
))
3326 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3327 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3328 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3330 /* Promote this argument, unless it has a _FloatN or
3332 promote_float_arg
= true;
3333 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
3334 if (TYPE_MAIN_VARIANT (valtype
) == FLOATN_NX_TYPE_NODE (i
))
3336 promote_float_arg
= false;
3341 if (type
!= NULL_TREE
)
3343 /* Formal parm type is specified by a function prototype. */
3345 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3347 error_at (ploc
, "type of formal parameter %d is incomplete",
3355 /* Optionally warn about conversions that
3356 differ from the default conversions. */
3357 if (warn_traditional_conversion
|| warn_traditional
)
3359 unsigned int formal_prec
= TYPE_PRECISION (type
);
3361 if (INTEGRAL_TYPE_P (type
)
3362 && TREE_CODE (valtype
) == REAL_TYPE
)
3363 warning_at (ploc
, OPT_Wtraditional_conversion
,
3364 "passing argument %d of %qE as integer rather "
3365 "than floating due to prototype",
3367 if (INTEGRAL_TYPE_P (type
)
3368 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3369 warning_at (ploc
, OPT_Wtraditional_conversion
,
3370 "passing argument %d of %qE as integer rather "
3371 "than complex due to prototype",
3373 else if (TREE_CODE (type
) == COMPLEX_TYPE
3374 && TREE_CODE (valtype
) == REAL_TYPE
)
3375 warning_at (ploc
, OPT_Wtraditional_conversion
,
3376 "passing argument %d of %qE as complex rather "
3377 "than floating due to prototype",
3379 else if (TREE_CODE (type
) == REAL_TYPE
3380 && INTEGRAL_TYPE_P (valtype
))
3381 warning_at (ploc
, OPT_Wtraditional_conversion
,
3382 "passing argument %d of %qE as floating rather "
3383 "than integer due to prototype",
3385 else if (TREE_CODE (type
) == COMPLEX_TYPE
3386 && INTEGRAL_TYPE_P (valtype
))
3387 warning_at (ploc
, OPT_Wtraditional_conversion
,
3388 "passing argument %d of %qE as complex rather "
3389 "than integer due to prototype",
3391 else if (TREE_CODE (type
) == REAL_TYPE
3392 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3393 warning_at (ploc
, OPT_Wtraditional_conversion
,
3394 "passing argument %d of %qE as floating rather "
3395 "than complex due to prototype",
3397 /* ??? At some point, messages should be written about
3398 conversions between complex types, but that's too messy
3400 else if (TREE_CODE (type
) == REAL_TYPE
3401 && TREE_CODE (valtype
) == REAL_TYPE
)
3403 /* Warn if any argument is passed as `float',
3404 since without a prototype it would be `double'. */
3405 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3406 && type
!= dfloat32_type_node
)
3407 warning_at (ploc
, 0,
3408 "passing argument %d of %qE as %<float%> "
3409 "rather than %<double%> due to prototype",
3412 /* Warn if mismatch between argument and prototype
3413 for decimal float types. Warn of conversions with
3414 binary float types and of precision narrowing due to
3416 else if (type
!= valtype
3417 && (type
== dfloat32_type_node
3418 || type
== dfloat64_type_node
3419 || type
== dfloat128_type_node
3420 || valtype
== dfloat32_type_node
3421 || valtype
== dfloat64_type_node
3422 || valtype
== dfloat128_type_node
)
3424 <= TYPE_PRECISION (valtype
)
3425 || (type
== dfloat128_type_node
3427 != dfloat64_type_node
3429 != dfloat32_type_node
)))
3430 || (type
== dfloat64_type_node
3432 != dfloat32_type_node
))))
3433 warning_at (ploc
, 0,
3434 "passing argument %d of %qE as %qT "
3435 "rather than %qT due to prototype",
3436 argnum
, rname
, type
, valtype
);
3439 /* Detect integer changing in width or signedness.
3440 These warnings are only activated with
3441 -Wtraditional-conversion, not with -Wtraditional. */
3442 else if (warn_traditional_conversion
3443 && INTEGRAL_TYPE_P (type
)
3444 && INTEGRAL_TYPE_P (valtype
))
3446 tree would_have_been
= default_conversion (val
);
3447 tree type1
= TREE_TYPE (would_have_been
);
3449 if (val
== error_mark_node
)
3450 /* VAL could have been of incomplete type. */;
3451 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3452 && (TYPE_MAIN_VARIANT (type
)
3453 == TYPE_MAIN_VARIANT (valtype
)))
3454 /* No warning if function asks for enum
3455 and the actual arg is that enum type. */
3457 else if (formal_prec
!= TYPE_PRECISION (type1
))
3458 warning_at (ploc
, OPT_Wtraditional_conversion
,
3459 "passing argument %d of %qE "
3460 "with different width due to prototype",
3462 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3464 /* Don't complain if the formal parameter type
3465 is an enum, because we can't tell now whether
3466 the value was an enum--even the same enum. */
3467 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3469 else if (TREE_CODE (val
) == INTEGER_CST
3470 && int_fits_type_p (val
, type
))
3471 /* Change in signedness doesn't matter
3472 if a constant value is unaffected. */
3474 /* If the value is extended from a narrower
3475 unsigned type, it doesn't matter whether we
3476 pass it as signed or unsigned; the value
3477 certainly is the same either way. */
3478 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3479 && TYPE_UNSIGNED (valtype
))
3481 else if (TYPE_UNSIGNED (type
))
3482 warning_at (ploc
, OPT_Wtraditional_conversion
,
3483 "passing argument %d of %qE "
3484 "as unsigned due to prototype",
3487 warning_at (ploc
, OPT_Wtraditional_conversion
,
3488 "passing argument %d of %qE "
3489 "as signed due to prototype",
3494 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3495 sake of better warnings from convert_and_check. */
3496 if (excess_precision
)
3497 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3498 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3499 parmval
= convert_for_assignment (loc
, ploc
, type
,
3500 val
, origtype
, ic_argpass
,
3501 npc
, fundecl
, function
,
3504 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3505 && INTEGRAL_TYPE_P (type
)
3506 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3507 parmval
= default_conversion (parmval
);
3510 else if (promote_float_arg
)
3516 /* Convert `float' to `double'. */
3517 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3518 warning_at (ploc
, OPT_Wdouble_promotion
,
3519 "implicit conversion from %qT to %qT when passing "
3520 "argument to function",
3521 valtype
, double_type_node
);
3522 parmval
= convert (double_type_node
, val
);
3525 else if ((excess_precision
&& !type_generic
)
3526 || (type_generic_overflow_p
&& parmnum
== 2))
3527 /* A "double" argument with excess precision being passed
3528 without a prototype or in variable arguments.
3529 The last argument of __builtin_*_overflow_p should not be
3531 parmval
= convert (valtype
, val
);
3532 else if ((invalid_func_diag
=
3533 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3535 error (invalid_func_diag
);
3538 else if (TREE_CODE (val
) == ADDR_EXPR
&& reject_gcc_builtin (val
))
3543 /* Convert `short' and `char' to full-size `int'. */
3544 parmval
= default_conversion (val
);
3546 (*values
)[parmnum
] = parmval
;
3547 if (parmval
== error_mark_node
)
3551 typetail
= TREE_CHAIN (typetail
);
3554 gcc_assert (parmnum
== vec_safe_length (values
));
3556 if (typetail
!= NULL_TREE
&& TREE_VALUE (typetail
) != void_type_node
)
3558 error_at (loc
, "too few arguments to function %qE", function
);
3559 inform_declaration (fundecl
);
3563 return error_args
? -1 : (int) parmnum
;
3566 /* This is the entry point used by the parser to build unary operators
3567 in the input. CODE, a tree_code, specifies the unary operator, and
3568 ARG is the operand. For unary plus, the C parser currently uses
3569 CONVERT_EXPR for code.
3571 LOC is the location to use for the tree generated.
3575 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3577 struct c_expr result
;
3579 result
.original_code
= code
;
3580 result
.original_type
= NULL
;
3582 if (reject_gcc_builtin (arg
.value
))
3584 result
.value
= error_mark_node
;
3588 result
.value
= build_unary_op (loc
, code
, arg
.value
, false);
3590 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3591 overflow_warning (loc
, result
.value
, arg
.value
);
3594 /* We are typically called when parsing a prefix token at LOC acting on
3595 ARG. Reflect this by updating the source range of the result to
3596 start at LOC and end at the end of ARG. */
3597 set_c_expr_source_range (&result
,
3598 loc
, arg
.get_finish ());
3603 /* Returns true if TYPE is a character type, *not* including wchar_t. */
3606 char_type_p (tree type
)
3608 return (type
== char_type_node
3609 || type
== unsigned_char_type_node
3610 || type
== signed_char_type_node
3611 || type
== char16_type_node
3612 || type
== char32_type_node
);
3615 /* This is the entry point used by the parser to build binary operators
3616 in the input. CODE, a tree_code, specifies the binary operator, and
3617 ARG1 and ARG2 are the operands. In addition to constructing the
3618 expression, we check for operands that were written with other binary
3619 operators in a way that is likely to confuse the user.
3621 LOCATION is the location of the binary operator. */
3624 parser_build_binary_op (location_t location
, enum tree_code code
,
3625 struct c_expr arg1
, struct c_expr arg2
)
3627 struct c_expr result
;
3629 enum tree_code code1
= arg1
.original_code
;
3630 enum tree_code code2
= arg2
.original_code
;
3631 tree type1
= (arg1
.original_type
3632 ? arg1
.original_type
3633 : TREE_TYPE (arg1
.value
));
3634 tree type2
= (arg2
.original_type
3635 ? arg2
.original_type
3636 : TREE_TYPE (arg2
.value
));
3638 result
.value
= build_binary_op (location
, code
,
3639 arg1
.value
, arg2
.value
, true);
3640 result
.original_code
= code
;
3641 result
.original_type
= NULL
;
3643 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3645 set_c_expr_source_range (&result
,
3647 arg2
.get_finish ());
3651 if (location
!= UNKNOWN_LOCATION
)
3652 protected_set_expr_location (result
.value
, location
);
3654 set_c_expr_source_range (&result
,
3656 arg2
.get_finish ());
3658 /* Check for cases such as x+y<<z which users are likely
3660 if (warn_parentheses
)
3661 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3664 if (warn_logical_op
)
3665 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3666 code1
, arg1
.value
, code2
, arg2
.value
);
3668 if (warn_tautological_compare
)
3670 tree lhs
= arg1
.value
;
3671 tree rhs
= arg2
.value
;
3672 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
3674 if (C_MAYBE_CONST_EXPR_PRE (lhs
) != NULL_TREE
3675 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs
)))
3678 lhs
= C_MAYBE_CONST_EXPR_EXPR (lhs
);
3680 if (TREE_CODE (rhs
) == C_MAYBE_CONST_EXPR
)
3682 if (C_MAYBE_CONST_EXPR_PRE (rhs
) != NULL_TREE
3683 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs
)))
3686 rhs
= C_MAYBE_CONST_EXPR_EXPR (rhs
);
3688 if (lhs
!= NULL_TREE
&& rhs
!= NULL_TREE
)
3689 warn_tautological_cmp (location
, code
, lhs
, rhs
);
3692 if (warn_logical_not_paren
3693 && TREE_CODE_CLASS (code
) == tcc_comparison
3694 && code1
== TRUTH_NOT_EXPR
3695 && code2
!= TRUTH_NOT_EXPR
3696 /* Avoid warning for !!x == y. */
3697 && (TREE_CODE (arg1
.value
) != NE_EXPR
3698 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
3700 /* Avoid warning for !b == y where b has _Bool type. */
3701 tree t
= integer_zero_node
;
3702 if (TREE_CODE (arg1
.value
) == EQ_EXPR
3703 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
3704 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
3706 t
= TREE_OPERAND (arg1
.value
, 0);
3709 if (TREE_TYPE (t
) != integer_type_node
)
3711 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
3712 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
3713 else if (CONVERT_EXPR_P (t
))
3714 t
= TREE_OPERAND (t
, 0);
3720 if (TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
)
3721 warn_logical_not_parentheses (location
, code
, arg1
.value
, arg2
.value
);
3724 /* Warn about comparisons against string literals, with the exception
3725 of testing for equality or inequality of a string literal with NULL. */
3726 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3728 if ((code1
== STRING_CST
3729 && !integer_zerop (tree_strip_nop_conversions (arg2
.value
)))
3730 || (code2
== STRING_CST
3731 && !integer_zerop (tree_strip_nop_conversions (arg1
.value
))))
3732 warning_at (location
, OPT_Waddress
,
3733 "comparison with string literal results in unspecified behavior");
3734 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3735 if (POINTER_TYPE_P (type1
)
3736 && null_pointer_constant_p (arg2
.value
)
3737 && char_type_p (type2
))
3739 auto_diagnostic_group d
;
3740 if (warning_at (location
, OPT_Wpointer_compare
,
3741 "comparison between pointer and zero character "
3743 inform (arg1
.get_start (),
3744 "did you mean to dereference the pointer?");
3746 else if (POINTER_TYPE_P (type2
)
3747 && null_pointer_constant_p (arg1
.value
)
3748 && char_type_p (type1
))
3750 auto_diagnostic_group d
;
3751 if (warning_at (location
, OPT_Wpointer_compare
,
3752 "comparison between pointer and zero character "
3754 inform (arg2
.get_start (),
3755 "did you mean to dereference the pointer?");
3758 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3759 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3760 warning_at (location
, OPT_Waddress
,
3761 "comparison with string literal results in unspecified behavior");
3763 if (TREE_OVERFLOW_P (result
.value
)
3764 && !TREE_OVERFLOW_P (arg1
.value
)
3765 && !TREE_OVERFLOW_P (arg2
.value
))
3766 overflow_warning (location
, result
.value
);
3768 /* Warn about comparisons of different enum types. */
3769 if (warn_enum_compare
3770 && TREE_CODE_CLASS (code
) == tcc_comparison
3771 && TREE_CODE (type1
) == ENUMERAL_TYPE
3772 && TREE_CODE (type2
) == ENUMERAL_TYPE
3773 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3774 warning_at (location
, OPT_Wenum_compare
,
3775 "comparison between %qT and %qT",
3781 /* Return a tree for the difference of pointers OP0 and OP1.
3782 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3783 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
3786 pointer_diff (location_t loc
, tree op0
, tree op1
, tree
*instrument_expr
)
3788 tree restype
= ptrdiff_type_node
;
3789 tree result
, inttype
;
3791 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3792 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3793 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3794 tree orig_op1
= op1
;
3796 /* If the operands point into different address spaces, we need to
3797 explicitly convert them to pointers into the common address space
3798 before we can subtract the numerical address values. */
3801 addr_space_t as_common
;
3804 /* Determine the common superset address space. This is guaranteed
3805 to exist because the caller verified that comp_target_types
3806 returned non-zero. */
3807 if (!addr_space_superset (as0
, as1
, &as_common
))
3810 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3811 op0
= convert (common_type
, op0
);
3812 op1
= convert (common_type
, op1
);
3815 /* Determine integer type result of the subtraction. This will usually
3816 be the same as the result type (ptrdiff_t), but may need to be a wider
3817 type if pointers for the address space are wider than ptrdiff_t. */
3818 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3819 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3823 if (TREE_CODE (target_type
) == VOID_TYPE
)
3824 pedwarn (loc
, OPT_Wpointer_arith
,
3825 "pointer of type %<void *%> used in subtraction");
3826 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3827 pedwarn (loc
, OPT_Wpointer_arith
,
3828 "pointer to a function used in subtraction");
3830 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
3832 gcc_assert (current_function_decl
!= NULL_TREE
);
3834 op0
= save_expr (op0
);
3835 op1
= save_expr (op1
);
3837 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
3838 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
3841 /* First do the subtraction, then build the divide operator
3842 and only convert at the very end.
3843 Do not do default conversions in case restype is a short type. */
3845 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3846 pointers. If some platform cannot provide that, or has a larger
3847 ptrdiff_type to support differences larger than half the address
3848 space, cast the pointers to some larger integer type and do the
3849 computations in that type. */
3850 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
3851 op0
= build_binary_op (loc
, MINUS_EXPR
, convert (inttype
, op0
),
3852 convert (inttype
, op1
), false);
3855 /* Cast away qualifiers. */
3856 op0
= convert (c_common_type (TREE_TYPE (op0
), TREE_TYPE (op0
)), op0
);
3857 op1
= convert (c_common_type (TREE_TYPE (op1
), TREE_TYPE (op1
)), op1
);
3858 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
3861 /* This generates an error if op1 is pointer to incomplete type. */
3862 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3863 error_at (loc
, "arithmetic on pointer to an incomplete type");
3865 op1
= c_size_in_bytes (target_type
);
3867 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3868 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3870 /* Divide by the size, in easiest possible way. */
3871 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3872 op0
, convert (inttype
, op1
));
3874 /* Convert to final result type if necessary. */
3875 return convert (restype
, result
);
3878 /* Expand atomic compound assignments into an appropriate sequence as
3879 specified by the C11 standard section 6.5.16.2.
3885 This sequence is used for all types for which these operations are
3888 In addition, built-in versions of the 'fe' prefixed routines may
3889 need to be invoked for floating point (real, complex or vector) when
3890 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3900 __atomic_load (addr, &old, SEQ_CST);
3901 feholdexcept (&fenv);
3903 newval = old op val;
3904 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3907 feclearexcept (FE_ALL_EXCEPT);
3910 feupdateenv (&fenv);
3912 The compiler will issue the __atomic_fetch_* built-in when possible,
3913 otherwise it will generate the generic form of the atomic operations.
3914 This requires temp(s) and has their address taken. The atomic processing
3915 is smart enough to figure out when the size of an object can utilize
3916 a lock-free version, and convert the built-in call to the appropriate
3917 lock-free routine. The optimizers will then dispose of any temps that
3918 are no longer required, and lock-free implementations are utilized as
3919 long as there is target support for the required size.
3921 If the operator is NOP_EXPR, then this is a simple assignment, and
3922 an __atomic_store is issued to perform the assignment rather than
3925 /* Build an atomic assignment at LOC, expanding into the proper
3926 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3927 the result of the operation, unless RETURN_OLD_P, in which case
3928 return the old value of LHS (this is only for postincrement and
3932 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3933 tree rhs
, bool return_old_p
)
3935 tree fndecl
, func_call
;
3936 vec
<tree
, va_gc
> *params
;
3937 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3940 tree stmt
, goto_stmt
;
3941 tree loop_label
, loop_decl
, done_label
, done_decl
;
3943 tree lhs_type
= TREE_TYPE (lhs
);
3944 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, false);
3945 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3946 tree rhs_semantic_type
= TREE_TYPE (rhs
);
3947 tree nonatomic_rhs_semantic_type
;
3950 gcc_assert (TYPE_ATOMIC (lhs_type
));
3953 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3955 /* Allocate enough vector items for a compare_exchange. */
3956 vec_alloc (params
, 6);
3958 /* Create a compound statement to hold the sequence of statements
3960 compound_stmt
= c_begin_compound_stmt (false);
3962 /* Remove any excess precision (which is only present here in the
3963 case of compound assignments). */
3964 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
3966 gcc_assert (modifycode
!= NOP_EXPR
);
3967 rhs
= TREE_OPERAND (rhs
, 0);
3969 rhs_type
= TREE_TYPE (rhs
);
3971 /* Fold the RHS if it hasn't already been folded. */
3972 if (modifycode
!= NOP_EXPR
)
3973 rhs
= c_fully_fold (rhs
, false, NULL
);
3975 /* Remove the qualifiers for the rest of the expressions and create
3976 the VAL temp variable to hold the RHS. */
3977 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3978 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3979 nonatomic_rhs_semantic_type
= build_qualified_type (rhs_semantic_type
,
3981 val
= create_tmp_var_raw (nonatomic_rhs_type
);
3982 TREE_ADDRESSABLE (val
) = 1;
3983 TREE_NO_WARNING (val
) = 1;
3984 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
3986 SET_EXPR_LOCATION (rhs
, loc
);
3989 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3991 if (modifycode
== NOP_EXPR
)
3993 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3994 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, false);
3995 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3996 params
->quick_push (lhs_addr
);
3997 params
->quick_push (rhs
);
3998 params
->quick_push (seq_cst
);
3999 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4000 add_stmt (func_call
);
4002 /* Finish the compound statement. */
4003 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4005 /* VAL is the value which was stored, return a COMPOUND_STMT of
4006 the statement and that value. */
4007 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
4010 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4011 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4012 isn't applicable for such builtins. ??? Do we want to handle enums? */
4013 if ((TREE_CODE (lhs_type
) == INTEGER_TYPE
|| POINTER_TYPE_P (lhs_type
))
4014 && TREE_CODE (rhs_type
) == INTEGER_TYPE
)
4016 built_in_function fncode
;
4020 case POINTER_PLUS_EXPR
:
4021 fncode
= (return_old_p
4022 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4023 : BUILT_IN_ATOMIC_ADD_FETCH_N
);
4026 fncode
= (return_old_p
4027 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4028 : BUILT_IN_ATOMIC_SUB_FETCH_N
);
4031 fncode
= (return_old_p
4032 ? BUILT_IN_ATOMIC_FETCH_AND_N
4033 : BUILT_IN_ATOMIC_AND_FETCH_N
);
4036 fncode
= (return_old_p
4037 ? BUILT_IN_ATOMIC_FETCH_OR_N
4038 : BUILT_IN_ATOMIC_OR_FETCH_N
);
4041 fncode
= (return_old_p
4042 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4043 : BUILT_IN_ATOMIC_XOR_FETCH_N
);
4049 /* We can only use "_1" through "_16" variants of the atomic fetch
4051 unsigned HOST_WIDE_INT size
= tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type
));
4052 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8 && size
!= 16)
4055 /* If this is a pointer type, we need to multiply by the size of
4056 the pointer target type. */
4057 if (POINTER_TYPE_P (lhs_type
))
4059 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type
))
4060 /* ??? This would introduce -Wdiscarded-qualifiers
4061 warning: __atomic_fetch_* expect volatile void *
4062 type as the first argument. (Assignments between
4063 atomic and non-atomic objects are OK.) */
4064 || TYPE_RESTRICT (lhs_type
))
4066 tree sz
= TYPE_SIZE_UNIT (TREE_TYPE (lhs_type
));
4067 rhs
= fold_build2_loc (loc
, MULT_EXPR
, ptrdiff_type_node
,
4068 convert (ptrdiff_type_node
, rhs
),
4069 convert (ptrdiff_type_node
, sz
));
4072 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4073 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4074 fndecl
= builtin_decl_explicit (fncode
);
4075 params
->quick_push (lhs_addr
);
4076 params
->quick_push (rhs
);
4077 params
->quick_push (seq_cst
);
4078 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4080 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4081 TREE_ADDRESSABLE (newval
) = 1;
4082 TREE_NO_WARNING (newval
) = 1;
4083 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, func_call
,
4084 NULL_TREE
, NULL_TREE
);
4085 SET_EXPR_LOCATION (rhs
, loc
);
4088 /* Finish the compound statement. */
4089 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4091 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4092 the statement and that value. */
4093 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, newval
);
4097 /* Create the variables and labels required for the op= form. */
4098 old
= create_tmp_var_raw (nonatomic_lhs_type
);
4099 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, false);
4100 TREE_ADDRESSABLE (old
) = 1;
4101 TREE_NO_WARNING (old
) = 1;
4103 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4104 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, false);
4105 TREE_ADDRESSABLE (newval
) = 1;
4106 TREE_NO_WARNING (newval
) = 1;
4108 loop_decl
= create_artificial_label (loc
);
4109 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
4111 done_decl
= create_artificial_label (loc
);
4112 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
4114 /* __atomic_load (addr, &old, SEQ_CST). */
4115 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
4116 params
->quick_push (lhs_addr
);
4117 params
->quick_push (old_addr
);
4118 params
->quick_push (seq_cst
);
4119 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4120 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
4123 params
->truncate (0);
4125 /* Create the expressions for floating-point environment
4126 manipulation, if required. */
4127 bool need_fenv
= (flag_trapping_math
4128 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
4129 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
4131 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
4134 add_stmt (hold_call
);
4137 add_stmt (loop_label
);
4139 /* newval = old + val; */
4140 if (rhs_type
!= rhs_semantic_type
)
4141 val
= build1 (EXCESS_PRECISION_EXPR
, nonatomic_rhs_semantic_type
, val
);
4142 rhs
= build_binary_op (loc
, modifycode
, old
, val
, true);
4143 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
4145 tree eptype
= TREE_TYPE (rhs
);
4146 rhs
= c_fully_fold (TREE_OPERAND (rhs
, 0), false, NULL
);
4147 rhs
= build1 (EXCESS_PRECISION_EXPR
, eptype
, rhs
);
4150 rhs
= c_fully_fold (rhs
, false, NULL
);
4151 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
4152 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
4154 if (rhs
!= error_mark_node
)
4156 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
4158 SET_EXPR_LOCATION (rhs
, loc
);
4162 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4164 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
4165 params
->quick_push (lhs_addr
);
4166 params
->quick_push (old_addr
);
4167 params
->quick_push (newval_addr
);
4168 params
->quick_push (integer_zero_node
);
4169 params
->quick_push (seq_cst
);
4170 params
->quick_push (seq_cst
);
4171 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4173 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
4174 SET_EXPR_LOCATION (goto_stmt
, loc
);
4176 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
4177 SET_EXPR_LOCATION (stmt
, loc
);
4181 add_stmt (clear_call
);
4184 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
4185 SET_EXPR_LOCATION (goto_stmt
, loc
);
4186 add_stmt (goto_stmt
);
4189 add_stmt (done_label
);
4192 add_stmt (update_call
);
4194 /* Finish the compound statement. */
4195 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4197 /* NEWVAL is the value that was successfully stored, return a
4198 COMPOUND_EXPR of the statement and the appropriate value. */
4199 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
4200 return_old_p
? old
: newval
);
4203 /* Construct and perhaps optimize a tree representation
4204 for a unary operation. CODE, a tree_code, specifies the operation
4205 and XARG is the operand.
4206 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4207 promotions (such as from short to int).
4208 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4209 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4212 LOCATION is the location of the operator. */
4215 build_unary_op (location_t location
, enum tree_code code
, tree xarg
,
4218 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4220 tree argtype
= NULL_TREE
;
4221 enum tree_code typecode
;
4223 tree ret
= error_mark_node
;
4224 tree eptype
= NULL_TREE
;
4225 const char *invalid_op_diag
;
4228 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
4230 arg
= remove_c_maybe_const_expr (arg
);
4232 if (code
!= ADDR_EXPR
)
4233 arg
= require_complete_type (location
, arg
);
4235 typecode
= TREE_CODE (TREE_TYPE (arg
));
4236 if (typecode
== ERROR_MARK
)
4237 return error_mark_node
;
4238 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
4239 typecode
= INTEGER_TYPE
;
4241 if ((invalid_op_diag
4242 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
4244 error_at (location
, invalid_op_diag
);
4245 return error_mark_node
;
4248 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
4250 eptype
= TREE_TYPE (arg
);
4251 arg
= TREE_OPERAND (arg
, 0);
4257 /* This is used for unary plus, because a CONVERT_EXPR
4258 is enough to prevent anybody from looking inside for
4259 associativity, but won't generate any code. */
4260 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4261 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4262 || typecode
== VECTOR_TYPE
))
4264 error_at (location
, "wrong type argument to unary plus");
4265 return error_mark_node
;
4267 else if (!noconvert
)
4268 arg
= default_conversion (arg
);
4269 arg
= non_lvalue_loc (location
, arg
);
4273 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4274 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4275 || typecode
== VECTOR_TYPE
))
4277 error_at (location
, "wrong type argument to unary minus");
4278 return error_mark_node
;
4280 else if (!noconvert
)
4281 arg
= default_conversion (arg
);
4285 /* ~ works on integer types and non float vectors. */
4286 if (typecode
== INTEGER_TYPE
4287 || (typecode
== VECTOR_TYPE
4288 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
4292 /* Warn if the expression has boolean value. */
4293 while (TREE_CODE (e
) == COMPOUND_EXPR
)
4294 e
= TREE_OPERAND (e
, 1);
4296 if ((TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
4297 || truth_value_p (TREE_CODE (e
))))
4299 auto_diagnostic_group d
;
4300 if (warning_at (location
, OPT_Wbool_operation
,
4301 "%<~%> on a boolean expression"))
4303 gcc_rich_location
richloc (location
);
4304 richloc
.add_fixit_insert_before (location
, "!");
4305 inform (&richloc
, "did you mean to use logical not?");
4309 arg
= default_conversion (arg
);
4311 else if (typecode
== COMPLEX_TYPE
)
4314 pedwarn (location
, OPT_Wpedantic
,
4315 "ISO C does not support %<~%> for complex conjugation");
4317 arg
= default_conversion (arg
);
4321 error_at (location
, "wrong type argument to bit-complement");
4322 return error_mark_node
;
4327 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
4329 error_at (location
, "wrong type argument to abs");
4330 return error_mark_node
;
4332 else if (!noconvert
)
4333 arg
= default_conversion (arg
);
4337 if (!(typecode
== INTEGER_TYPE
))
4339 error_at (location
, "wrong type argument to absu");
4340 return error_mark_node
;
4342 else if (!noconvert
)
4343 arg
= default_conversion (arg
);
4347 /* Conjugating a real value is a no-op, but allow it anyway. */
4348 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4349 || typecode
== COMPLEX_TYPE
))
4351 error_at (location
, "wrong type argument to conjugation");
4352 return error_mark_node
;
4354 else if (!noconvert
)
4355 arg
= default_conversion (arg
);
4358 case TRUTH_NOT_EXPR
:
4359 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4360 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
4361 && typecode
!= COMPLEX_TYPE
)
4364 "wrong type argument to unary exclamation mark");
4365 return error_mark_node
;
4369 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
4370 arg
= remove_c_maybe_const_expr (arg
);
4373 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
4374 ret
= invert_truthvalue_loc (location
, arg
);
4375 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4376 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
4377 location
= EXPR_LOCATION (ret
);
4378 goto return_build_unary_op
;
4382 ret
= build_real_imag_expr (location
, code
, arg
);
4383 if (ret
== error_mark_node
)
4384 return error_mark_node
;
4385 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
4386 eptype
= TREE_TYPE (eptype
);
4387 goto return_build_unary_op
;
4389 case PREINCREMENT_EXPR
:
4390 case POSTINCREMENT_EXPR
:
4391 case PREDECREMENT_EXPR
:
4392 case POSTDECREMENT_EXPR
:
4394 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4396 tree inner
= build_unary_op (location
, code
,
4397 C_MAYBE_CONST_EXPR_EXPR (arg
),
4399 if (inner
== error_mark_node
)
4400 return error_mark_node
;
4401 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4402 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4403 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4404 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
4405 goto return_build_unary_op
;
4408 /* Complain about anything that is not a true lvalue. In
4409 Objective-C, skip this check for property_refs. */
4410 if (!objc_is_property_ref (arg
)
4411 && !lvalue_or_else (location
,
4412 arg
, ((code
== PREINCREMENT_EXPR
4413 || code
== POSTINCREMENT_EXPR
)
4416 return error_mark_node
;
4418 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
4420 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4421 warning_at (location
, OPT_Wc___compat
,
4422 "increment of enumeration value is invalid in C++");
4424 warning_at (location
, OPT_Wc___compat
,
4425 "decrement of enumeration value is invalid in C++");
4428 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4430 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4431 warning_at (location
, OPT_Wbool_operation
,
4432 "increment of a boolean expression");
4434 warning_at (location
, OPT_Wbool_operation
,
4435 "decrement of a boolean expression");
4438 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4439 arg
= c_fully_fold (arg
, false, NULL
, true);
4442 atomic_op
= really_atomic_lvalue (arg
);
4444 /* Increment or decrement the real part of the value,
4445 and don't change the imaginary part. */
4446 if (typecode
== COMPLEX_TYPE
)
4450 pedwarn (location
, OPT_Wpedantic
,
4451 "ISO C does not support %<++%> and %<--%> on complex types");
4455 arg
= stabilize_reference (arg
);
4456 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
,
4458 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
,
4460 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, true);
4461 if (real
== error_mark_node
|| imag
== error_mark_node
)
4462 return error_mark_node
;
4463 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4465 goto return_build_unary_op
;
4469 /* Report invalid types. */
4471 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4472 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4473 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
4475 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4476 error_at (location
, "wrong type argument to increment");
4478 error_at (location
, "wrong type argument to decrement");
4480 return error_mark_node
;
4486 argtype
= TREE_TYPE (arg
);
4488 /* Compute the increment. */
4490 if (typecode
== POINTER_TYPE
)
4492 /* If pointer target is an incomplete type,
4493 we just cannot know how to do the arithmetic. */
4494 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4496 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4498 "increment of pointer to an incomplete type %qT",
4499 TREE_TYPE (argtype
));
4502 "decrement of pointer to an incomplete type %qT",
4503 TREE_TYPE (argtype
));
4505 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4506 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4508 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4509 pedwarn (location
, OPT_Wpointer_arith
,
4510 "wrong type argument to increment");
4512 pedwarn (location
, OPT_Wpointer_arith
,
4513 "wrong type argument to decrement");
4516 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4517 inc
= convert_to_ptrofftype_loc (location
, inc
);
4519 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4521 /* For signed fract types, we invert ++ to -- or
4522 -- to ++, and change inc from 1 to -1, because
4523 it is not possible to represent 1 in signed fract constants.
4524 For unsigned fract types, the result always overflows and
4525 we get an undefined (original) or the maximum value. */
4526 if (code
== PREINCREMENT_EXPR
)
4527 code
= PREDECREMENT_EXPR
;
4528 else if (code
== PREDECREMENT_EXPR
)
4529 code
= PREINCREMENT_EXPR
;
4530 else if (code
== POSTINCREMENT_EXPR
)
4531 code
= POSTDECREMENT_EXPR
;
4532 else /* code == POSTDECREMENT_EXPR */
4533 code
= POSTINCREMENT_EXPR
;
4535 inc
= integer_minus_one_node
;
4536 inc
= convert (argtype
, inc
);
4540 inc
= VECTOR_TYPE_P (argtype
)
4541 ? build_one_cst (argtype
)
4543 inc
= convert (argtype
, inc
);
4546 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4547 need to ask Objective-C to build the increment or decrement
4548 expression for it. */
4549 if (objc_is_property_ref (arg
))
4550 return objc_build_incr_expr_for_property_ref (location
, code
,
4553 /* Report a read-only lvalue. */
4554 if (TYPE_READONLY (argtype
))
4556 readonly_error (location
, arg
,
4557 ((code
== PREINCREMENT_EXPR
4558 || code
== POSTINCREMENT_EXPR
)
4559 ? lv_increment
: lv_decrement
));
4560 return error_mark_node
;
4562 else if (TREE_READONLY (arg
))
4563 readonly_warning (arg
,
4564 ((code
== PREINCREMENT_EXPR
4565 || code
== POSTINCREMENT_EXPR
)
4566 ? lv_increment
: lv_decrement
));
4568 /* If the argument is atomic, use the special code sequences for
4569 atomic compound assignment. */
4572 arg
= stabilize_reference (arg
);
4573 ret
= build_atomic_assign (location
, arg
,
4574 ((code
== PREINCREMENT_EXPR
4575 || code
== POSTINCREMENT_EXPR
)
4578 (FRACT_MODE_P (TYPE_MODE (argtype
))
4580 : integer_one_node
),
4581 (code
== POSTINCREMENT_EXPR
4582 || code
== POSTDECREMENT_EXPR
));
4583 goto return_build_unary_op
;
4586 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4587 val
= boolean_increment (code
, arg
);
4589 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4590 TREE_SIDE_EFFECTS (val
) = 1;
4591 if (TREE_CODE (val
) != code
)
4592 TREE_NO_WARNING (val
) = 1;
4594 goto return_build_unary_op
;
4598 /* Note that this operation never does default_conversion. */
4600 /* The operand of unary '&' must be an lvalue (which excludes
4601 expressions of type void), or, in C99, the result of a [] or
4602 unary '*' operator. */
4603 if (VOID_TYPE_P (TREE_TYPE (arg
))
4604 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4605 && (!INDIRECT_REF_P (arg
) || !flag_isoc99
))
4606 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4608 /* Let &* cancel out to simplify resulting code. */
4609 if (INDIRECT_REF_P (arg
))
4611 /* Don't let this be an lvalue. */
4612 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4613 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4614 ret
= TREE_OPERAND (arg
, 0);
4615 goto return_build_unary_op
;
4618 /* Anything not already handled and not a true memory reference
4619 or a non-lvalue array is an error. */
4620 if (typecode
!= FUNCTION_TYPE
&& !noconvert
4621 && !lvalue_or_else (location
, arg
, lv_addressof
))
4622 return error_mark_node
;
4624 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4626 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4628 tree inner
= build_unary_op (location
, code
,
4629 C_MAYBE_CONST_EXPR_EXPR (arg
),
4631 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4632 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4633 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4634 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4635 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4636 goto return_build_unary_op
;
4639 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4640 argtype
= TREE_TYPE (arg
);
4642 /* If the lvalue is const or volatile, merge that into the type
4643 to which the address will point. This is only needed
4644 for function types. */
4645 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4646 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4647 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4649 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4650 int quals
= orig_quals
;
4652 if (TREE_READONLY (arg
))
4653 quals
|= TYPE_QUAL_CONST
;
4654 if (TREE_THIS_VOLATILE (arg
))
4655 quals
|= TYPE_QUAL_VOLATILE
;
4657 argtype
= c_build_qualified_type (argtype
, quals
);
4660 switch (TREE_CODE (arg
))
4663 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
4665 error_at (location
, "cannot take address of bit-field %qD",
4666 TREE_OPERAND (arg
, 1));
4667 return error_mark_node
;
4673 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg
, 0))))
4675 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg
))
4676 && !VECTOR_TYPE_P (TREE_TYPE (arg
)))
4678 error_at (location
, "cannot take address of scalar with "
4679 "reverse storage order");
4680 return error_mark_node
;
4683 if (TREE_CODE (TREE_TYPE (arg
)) == ARRAY_TYPE
4684 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg
)))
4685 warning_at (location
, OPT_Wscalar_storage_order
,
4686 "address of array with reverse scalar storage "
4694 if (!c_mark_addressable (arg
))
4695 return error_mark_node
;
4697 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4698 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4700 argtype
= build_pointer_type (argtype
);
4702 /* ??? Cope with user tricks that amount to offsetof. Delete this
4703 when we have proper support for integer constant expressions. */
4704 val
= get_base_address (arg
);
4705 if (val
&& INDIRECT_REF_P (val
)
4706 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4708 ret
= fold_offsetof (arg
, argtype
);
4709 goto return_build_unary_op
;
4712 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4715 goto return_build_unary_op
;
4721 if (argtype
== NULL_TREE
)
4722 argtype
= TREE_TYPE (arg
);
4723 if (TREE_CODE (arg
) == INTEGER_CST
)
4724 ret
= (require_constant_value
4725 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4726 : fold_build1_loc (location
, code
, argtype
, arg
));
4728 ret
= build1 (code
, argtype
, arg
);
4729 return_build_unary_op
:
4730 gcc_assert (ret
!= error_mark_node
);
4731 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4732 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4733 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4734 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4735 ret
= note_integer_operands (ret
);
4737 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4738 protected_set_expr_location (ret
, location
);
4742 /* Return nonzero if REF is an lvalue valid for this language.
4743 Lvalues can be assigned, unless their type has TYPE_READONLY.
4744 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4747 lvalue_p (const_tree ref
)
4749 const enum tree_code code
= TREE_CODE (ref
);
4756 return lvalue_p (TREE_OPERAND (ref
, 0));
4758 case C_MAYBE_CONST_EXPR
:
4759 return lvalue_p (TREE_OPERAND (ref
, 1));
4761 case COMPOUND_LITERAL_EXPR
:
4771 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4772 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4775 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4782 /* Give a warning for storing in something that is read-only in GCC
4783 terms but not const in ISO C terms. */
4786 readonly_warning (tree arg
, enum lvalue_use use
)
4791 warning (0, "assignment of read-only location %qE", arg
);
4794 warning (0, "increment of read-only location %qE", arg
);
4797 warning (0, "decrement of read-only location %qE", arg
);
4806 /* Return nonzero if REF is an lvalue valid for this language;
4807 otherwise, print an error message and return zero. USE says
4808 how the lvalue is being used and so selects the error message.
4809 LOCATION is the location at which any error should be reported. */
4812 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4814 int win
= lvalue_p (ref
);
4817 lvalue_error (loc
, use
);
4822 /* Mark EXP saying that we need to be able to take the
4823 address of it; it should not be allocated in a register.
4824 Returns true if successful. ARRAY_REF_P is true if this
4825 is for ARRAY_REF construction - in that case we don't want
4826 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4827 it is fine to use ARRAY_REFs for vector subscripts on vector
4828 register variables. */
4831 c_mark_addressable (tree exp
, bool array_ref_p
)
4836 switch (TREE_CODE (x
))
4838 case VIEW_CONVERT_EXPR
:
4840 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
4841 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
4849 x
= TREE_OPERAND (x
, 0);
4852 case COMPOUND_LITERAL_EXPR
:
4853 TREE_ADDRESSABLE (x
) = 1;
4854 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x
)) = 1;
4858 TREE_ADDRESSABLE (x
) = 1;
4865 if (C_DECL_REGISTER (x
)
4866 && DECL_NONLOCAL (x
))
4868 if (TREE_PUBLIC (x
) || is_global_var (x
))
4871 ("global register variable %qD used in nested function", x
);
4874 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4876 else if (C_DECL_REGISTER (x
))
4878 if (TREE_PUBLIC (x
) || is_global_var (x
))
4879 error ("address of global register variable %qD requested", x
);
4881 error ("address of register variable %qD requested", x
);
4887 TREE_ADDRESSABLE (x
) = 1;
4894 /* Convert EXPR to TYPE, warning about conversion problems with
4895 constants. SEMANTIC_TYPE is the type this conversion would use
4896 without excess precision. If SEMANTIC_TYPE is NULL, this function
4897 is equivalent to convert_and_check. This function is a wrapper that
4898 handles conversions that may be different than
4899 the usual ones because of excess precision. */
4902 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4905 if (TREE_TYPE (expr
) == type
)
4908 /* For C11, integer conversions may have results with excess
4910 if (flag_isoc11
|| !semantic_type
)
4911 return convert_and_check (loc
, type
, expr
);
4913 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4914 && TREE_TYPE (expr
) != semantic_type
)
4916 /* For integers, we need to check the real conversion, not
4917 the conversion to the excess precision type. */
4918 expr
= convert_and_check (loc
, semantic_type
, expr
);
4920 /* Result type is the excess precision type, which should be
4921 large enough, so do not check. */
4922 return convert (type
, expr
);
4925 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4926 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4927 if folded to an integer constant then the unselected half may
4928 contain arbitrary operations not normally permitted in constant
4929 expressions. Set the location of the expression to LOC. */
4932 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4933 tree op1
, tree op1_original_type
, location_t op1_loc
,
4934 tree op2
, tree op2_original_type
, location_t op2_loc
)
4938 enum tree_code code1
;
4939 enum tree_code code2
;
4940 tree result_type
= NULL
;
4941 tree semantic_result_type
= NULL
;
4942 tree orig_op1
= op1
, orig_op2
= op2
;
4943 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4944 bool ifexp_int_operands
;
4947 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4948 if (op1_int_operands
)
4949 op1
= remove_c_maybe_const_expr (op1
);
4950 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4951 if (op2_int_operands
)
4952 op2
= remove_c_maybe_const_expr (op2
);
4953 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4954 if (ifexp_int_operands
)
4955 ifexp
= remove_c_maybe_const_expr (ifexp
);
4957 /* Promote both alternatives. */
4959 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4960 op1
= default_conversion (op1
);
4961 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4962 op2
= default_conversion (op2
);
4964 if (TREE_CODE (ifexp
) == ERROR_MARK
4965 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4966 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4967 return error_mark_node
;
4969 type1
= TREE_TYPE (op1
);
4970 code1
= TREE_CODE (type1
);
4971 type2
= TREE_TYPE (op2
);
4972 code2
= TREE_CODE (type2
);
4974 if (code1
== POINTER_TYPE
&& reject_gcc_builtin (op1
))
4975 return error_mark_node
;
4977 if (code2
== POINTER_TYPE
&& reject_gcc_builtin (op2
))
4978 return error_mark_node
;
4980 /* C90 does not permit non-lvalue arrays in conditional expressions.
4981 In C99 they will be pointers by now. */
4982 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4984 error_at (colon_loc
, "non-lvalue array in conditional expression");
4985 return error_mark_node
;
4988 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4989 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4990 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4991 || code1
== COMPLEX_TYPE
)
4992 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4993 || code2
== COMPLEX_TYPE
))
4995 semantic_result_type
= c_common_type (type1
, type2
);
4996 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4998 op1
= TREE_OPERAND (op1
, 0);
4999 type1
= TREE_TYPE (op1
);
5000 gcc_assert (TREE_CODE (type1
) == code1
);
5002 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
5004 op2
= TREE_OPERAND (op2
, 0);
5005 type2
= TREE_TYPE (op2
);
5006 gcc_assert (TREE_CODE (type2
) == code2
);
5010 if (warn_cxx_compat
)
5012 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
5013 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
5015 if (TREE_CODE (t1
) == ENUMERAL_TYPE
5016 && TREE_CODE (t2
) == ENUMERAL_TYPE
5017 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
5018 warning_at (colon_loc
, OPT_Wc___compat
,
5019 ("different enum types in conditional is "
5020 "invalid in C++: %qT vs %qT"),
5024 /* Quickly detect the usual case where op1 and op2 have the same type
5026 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
5029 result_type
= type1
;
5031 result_type
= TYPE_MAIN_VARIANT (type1
);
5033 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5034 || code1
== COMPLEX_TYPE
)
5035 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
5036 || code2
== COMPLEX_TYPE
))
5038 /* In C11, a conditional expression between a floating-point
5039 type and an integer type should convert the integer type to
5040 the evaluation format of the floating-point type, with
5041 possible excess precision. */
5042 tree eptype1
= type1
;
5043 tree eptype2
= type2
;
5047 if (ANY_INTEGRAL_TYPE_P (type1
)
5048 && (eptype
= excess_precision_type (type2
)) != NULL_TREE
)
5051 if (!semantic_result_type
)
5052 semantic_result_type
= c_common_type (type1
, type2
);
5054 else if (ANY_INTEGRAL_TYPE_P (type2
)
5055 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
5058 if (!semantic_result_type
)
5059 semantic_result_type
= c_common_type (type1
, type2
);
5062 result_type
= c_common_type (eptype1
, eptype2
);
5063 if (result_type
== error_mark_node
)
5064 return error_mark_node
;
5065 do_warn_double_promotion (result_type
, type1
, type2
,
5066 "implicit conversion from %qT to %qT to "
5067 "match other result of conditional",
5070 /* If -Wsign-compare, warn here if type1 and type2 have
5071 different signedness. We'll promote the signed to unsigned
5072 and later code won't know it used to be different.
5073 Do this check on the original types, so that explicit casts
5074 will be considered, but default promotions won't. */
5075 if (c_inhibit_evaluation_warnings
== 0)
5077 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
5078 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
5080 if (unsigned_op1
^ unsigned_op2
)
5084 /* Do not warn if the result type is signed, since the
5085 signed type will only be chosen if it can represent
5086 all the values of the unsigned type. */
5087 if (!TYPE_UNSIGNED (result_type
))
5091 bool op1_maybe_const
= true;
5092 bool op2_maybe_const
= true;
5094 /* Do not warn if the signed quantity is an
5095 unsuffixed integer literal (or some static
5096 constant expression involving such literals) and
5097 it is non-negative. This warning requires the
5098 operands to be folded for best results, so do
5099 that folding in this case even without
5100 warn_sign_compare to avoid warning options
5101 possibly affecting code generation. */
5102 c_inhibit_evaluation_warnings
5103 += (ifexp
== truthvalue_false_node
);
5104 op1
= c_fully_fold (op1
, require_constant_value
,
5106 c_inhibit_evaluation_warnings
5107 -= (ifexp
== truthvalue_false_node
);
5109 c_inhibit_evaluation_warnings
5110 += (ifexp
== truthvalue_true_node
);
5111 op2
= c_fully_fold (op2
, require_constant_value
,
5113 c_inhibit_evaluation_warnings
5114 -= (ifexp
== truthvalue_true_node
);
5116 if (warn_sign_compare
)
5119 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
5121 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
5123 else if (unsigned_op2
)
5124 warning_at (op1_loc
, OPT_Wsign_compare
,
5125 "operand of ?: changes signedness from "
5126 "%qT to %qT due to unsignedness of other "
5127 "operand", TREE_TYPE (orig_op1
),
5128 TREE_TYPE (orig_op2
));
5130 warning_at (op2_loc
, OPT_Wsign_compare
,
5131 "operand of ?: changes signedness from "
5132 "%qT to %qT due to unsignedness of other "
5133 "operand", TREE_TYPE (orig_op2
),
5134 TREE_TYPE (orig_op1
));
5136 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
5137 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
5138 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
5139 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
5144 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
5146 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
5147 pedwarn (colon_loc
, OPT_Wpedantic
,
5148 "ISO C forbids conditional expr with only one void side");
5149 result_type
= void_type_node
;
5151 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5153 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
5154 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
5155 addr_space_t as_common
;
5157 if (comp_target_types (colon_loc
, type1
, type2
))
5158 result_type
= common_pointer_type (type1
, type2
);
5159 else if (null_pointer_constant_p (orig_op1
))
5160 result_type
= type2
;
5161 else if (null_pointer_constant_p (orig_op2
))
5162 result_type
= type1
;
5163 else if (!addr_space_superset (as1
, as2
, &as_common
))
5165 error_at (colon_loc
, "pointers to disjoint address spaces "
5166 "used in conditional expression");
5167 return error_mark_node
;
5169 else if (VOID_TYPE_P (TREE_TYPE (type1
))
5170 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
5172 if ((TREE_CODE (TREE_TYPE (type2
)) == ARRAY_TYPE
)
5173 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2
)))
5174 & ~TYPE_QUALS (TREE_TYPE (type1
))))
5175 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5176 "pointer to array loses qualifier "
5177 "in conditional expression");
5179 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
5180 pedwarn (colon_loc
, OPT_Wpedantic
,
5181 "ISO C forbids conditional expr between "
5182 "%<void *%> and function pointer");
5183 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
5184 TREE_TYPE (type2
)));
5186 else if (VOID_TYPE_P (TREE_TYPE (type2
))
5187 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
5189 if ((TREE_CODE (TREE_TYPE (type1
)) == ARRAY_TYPE
)
5190 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1
)))
5191 & ~TYPE_QUALS (TREE_TYPE (type2
))))
5192 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5193 "pointer to array loses qualifier "
5194 "in conditional expression");
5196 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
5197 pedwarn (colon_loc
, OPT_Wpedantic
,
5198 "ISO C forbids conditional expr between "
5199 "%<void *%> and function pointer");
5200 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
5201 TREE_TYPE (type1
)));
5203 /* Objective-C pointer comparisons are a bit more lenient. */
5204 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
5205 result_type
= objc_common_type (type1
, type2
);
5208 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
5210 pedwarn (colon_loc
, 0,
5211 "pointer type mismatch in conditional expression");
5212 result_type
= build_pointer_type
5213 (build_qualified_type (void_type_node
, qual
));
5216 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5218 if (!null_pointer_constant_p (orig_op2
))
5219 pedwarn (colon_loc
, 0,
5220 "pointer/integer type mismatch in conditional expression");
5223 op2
= null_pointer_node
;
5225 result_type
= type1
;
5227 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5229 if (!null_pointer_constant_p (orig_op1
))
5230 pedwarn (colon_loc
, 0,
5231 "pointer/integer type mismatch in conditional expression");
5234 op1
= null_pointer_node
;
5236 result_type
= type2
;
5241 if (flag_cond_mismatch
)
5242 result_type
= void_type_node
;
5245 error_at (colon_loc
, "type mismatch in conditional expression");
5246 return error_mark_node
;
5250 /* Merge const and volatile flags of the incoming types. */
5252 = build_type_variant (result_type
,
5253 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
5254 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
5256 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
5257 semantic_result_type
);
5258 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
5259 semantic_result_type
);
5261 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
5263 op2_int_operands
= true;
5264 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
5266 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
5268 op1_int_operands
= true;
5269 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
5271 int_const
= int_operands
= (ifexp_int_operands
5273 && op2_int_operands
);
5276 int_const
= ((ifexp
== truthvalue_true_node
5277 && TREE_CODE (orig_op1
) == INTEGER_CST
5278 && !TREE_OVERFLOW (orig_op1
))
5279 || (ifexp
== truthvalue_false_node
5280 && TREE_CODE (orig_op2
) == INTEGER_CST
5281 && !TREE_OVERFLOW (orig_op2
)));
5284 /* Need to convert condition operand into a vector mask. */
5285 if (VECTOR_TYPE_P (TREE_TYPE (ifexp
)))
5287 tree vectype
= TREE_TYPE (ifexp
);
5288 tree elem_type
= TREE_TYPE (vectype
);
5289 tree zero
= build_int_cst (elem_type
, 0);
5290 tree zero_vec
= build_vector_from_val (vectype
, zero
);
5291 tree cmp_type
= build_same_sized_truth_vector_type (vectype
);
5292 ifexp
= build2 (NE_EXPR
, cmp_type
, ifexp
, zero_vec
);
5295 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
5296 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5301 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5302 nested inside of the expression. */
5303 op1
= c_fully_fold (op1
, false, NULL
);
5304 op2
= c_fully_fold (op2
, false, NULL
);
5306 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5308 ret
= note_integer_operands (ret
);
5310 if (semantic_result_type
)
5311 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
5313 protected_set_expr_location (ret
, colon_loc
);
5315 /* If the OP1 and OP2 are the same and don't have side-effects,
5316 warn here, because the COND_EXPR will be turned into OP1. */
5317 if (warn_duplicated_branches
5318 && TREE_CODE (ret
) == COND_EXPR
5319 && (op1
== op2
|| operand_equal_p (op1
, op2
, 0)))
5320 warning_at (EXPR_LOCATION (ret
), OPT_Wduplicated_branches
,
5321 "this condition has identical branches");
5326 /* Return a compound expression that performs two expressions and
5327 returns the value of the second of them.
5329 LOC is the location of the COMPOUND_EXPR. */
5332 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
5334 bool expr1_int_operands
, expr2_int_operands
;
5335 tree eptype
= NULL_TREE
;
5338 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
5339 if (expr1_int_operands
)
5340 expr1
= remove_c_maybe_const_expr (expr1
);
5341 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
5342 if (expr2_int_operands
)
5343 expr2
= remove_c_maybe_const_expr (expr2
);
5345 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
5346 expr1
= TREE_OPERAND (expr1
, 0);
5347 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
5349 eptype
= TREE_TYPE (expr2
);
5350 expr2
= TREE_OPERAND (expr2
, 0);
5353 if (!TREE_SIDE_EFFECTS (expr1
))
5355 /* The left-hand operand of a comma expression is like an expression
5356 statement: with -Wunused, we should warn if it doesn't have
5357 any side-effects, unless it was explicitly cast to (void). */
5358 if (warn_unused_value
)
5360 if (VOID_TYPE_P (TREE_TYPE (expr1
))
5361 && CONVERT_EXPR_P (expr1
))
5363 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
5364 && TREE_CODE (expr1
) == COMPOUND_EXPR
5365 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
5366 ; /* (void) a, (void) b, c */
5368 warning_at (loc
, OPT_Wunused_value
,
5369 "left-hand operand of comma expression has no effect");
5372 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
5373 && warn_unused_value
)
5376 location_t cloc
= loc
;
5377 while (TREE_CODE (r
) == COMPOUND_EXPR
)
5379 if (EXPR_HAS_LOCATION (r
))
5380 cloc
= EXPR_LOCATION (r
);
5381 r
= TREE_OPERAND (r
, 1);
5383 if (!TREE_SIDE_EFFECTS (r
)
5384 && !VOID_TYPE_P (TREE_TYPE (r
))
5385 && !CONVERT_EXPR_P (r
))
5386 warning_at (cloc
, OPT_Wunused_value
,
5387 "right-hand operand of comma expression has no effect");
5390 /* With -Wunused, we should also warn if the left-hand operand does have
5391 side-effects, but computes a value which is not used. For example, in
5392 `foo() + bar(), baz()' the result of the `+' operator is not used,
5393 so we should issue a warning. */
5394 else if (warn_unused_value
)
5395 warn_if_unused_value (expr1
, loc
);
5397 if (expr2
== error_mark_node
)
5398 return error_mark_node
;
5400 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
5403 && expr1_int_operands
5404 && expr2_int_operands
)
5405 ret
= note_integer_operands (ret
);
5408 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
5410 protected_set_expr_location (ret
, loc
);
5414 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5415 which we are casting. OTYPE is the type of the expression being
5416 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5417 of the cast. -Wcast-qual appeared on the command line. Named
5418 address space qualifiers are not handled here, because they result
5419 in different warnings. */
5422 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
5424 tree in_type
= type
;
5425 tree in_otype
= otype
;
5430 /* Check that the qualifiers on IN_TYPE are a superset of the
5431 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5432 nodes is uninteresting and we stop as soon as we hit a
5433 non-POINTER_TYPE node on either type. */
5436 in_otype
= TREE_TYPE (in_otype
);
5437 in_type
= TREE_TYPE (in_type
);
5439 /* GNU C allows cv-qualified function types. 'const' means the
5440 function is very pure, 'volatile' means it can't return. We
5441 need to warn when such qualifiers are added, not when they're
5443 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
5444 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
5445 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
5446 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
5448 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
5449 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
5451 while (TREE_CODE (in_type
) == POINTER_TYPE
5452 && TREE_CODE (in_otype
) == POINTER_TYPE
);
5455 warning_at (loc
, OPT_Wcast_qual
,
5456 "cast adds %q#v qualifier to function type", added
);
5459 /* There are qualifiers present in IN_OTYPE that are not present
5461 warning_at (loc
, OPT_Wcast_qual
,
5462 "cast discards %qv qualifier from pointer target type",
5465 if (added
|| discarded
)
5468 /* A cast from **T to const **T is unsafe, because it can cause a
5469 const value to be changed with no additional warning. We only
5470 issue this warning if T is the same on both sides, and we only
5471 issue the warning if there are the same number of pointers on
5472 both sides, as otherwise the cast is clearly unsafe anyhow. A
5473 cast is unsafe when a qualifier is added at one level and const
5474 is not present at all outer levels.
5476 To issue this warning, we check at each level whether the cast
5477 adds new qualifiers not already seen. We don't need to special
5478 case function types, as they won't have the same
5479 TYPE_MAIN_VARIANT. */
5481 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
5483 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
5488 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
5491 in_type
= TREE_TYPE (in_type
);
5492 in_otype
= TREE_TYPE (in_otype
);
5493 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
5496 warning_at (loc
, OPT_Wcast_qual
,
5497 "to be safe all intermediate pointers in cast from "
5498 "%qT to %qT must be %<const%> qualified",
5503 is_const
= TYPE_READONLY (in_type
);
5505 while (TREE_CODE (in_type
) == POINTER_TYPE
);
5508 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
5511 c_safe_arg_type_equiv_p (tree t1
, tree t2
)
5513 t1
= TYPE_MAIN_VARIANT (t1
);
5514 t2
= TYPE_MAIN_VARIANT (t2
);
5516 if (TREE_CODE (t1
) == POINTER_TYPE
5517 && TREE_CODE (t2
) == POINTER_TYPE
)
5520 /* The signedness of the parameter matters only when an integral
5521 type smaller than int is promoted to int, otherwise only the
5522 precision of the parameter matters.
5523 This check should make sure that the callee does not see
5524 undefined values in argument registers. */
5525 if (INTEGRAL_TYPE_P (t1
)
5526 && INTEGRAL_TYPE_P (t2
)
5527 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
5528 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
5529 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
5530 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
5533 return comptypes (t1
, t2
);
5536 /* Check if a type cast between two function types can be considered safe. */
5539 c_safe_function_type_cast_p (tree t1
, tree t2
)
5541 if (TREE_TYPE (t1
) == void_type_node
&&
5542 TYPE_ARG_TYPES (t1
) == void_list_node
)
5545 if (TREE_TYPE (t2
) == void_type_node
&&
5546 TYPE_ARG_TYPES (t2
) == void_list_node
)
5549 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
5552 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
5554 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
5555 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
5561 /* Build an expression representing a cast to type TYPE of expression EXPR.
5562 LOC is the location of the cast-- typically the open paren of the cast. */
5565 build_c_cast (location_t loc
, tree type
, tree expr
)
5569 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
5570 expr
= TREE_OPERAND (expr
, 0);
5574 if (type
== error_mark_node
|| expr
== error_mark_node
)
5575 return error_mark_node
;
5577 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5578 only in <protocol> qualifications. But when constructing cast expressions,
5579 the protocols do matter and must be kept around. */
5580 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
5581 return build1 (NOP_EXPR
, type
, expr
);
5583 type
= TYPE_MAIN_VARIANT (type
);
5585 if (TREE_CODE (type
) == ARRAY_TYPE
)
5587 error_at (loc
, "cast specifies array type");
5588 return error_mark_node
;
5591 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5593 error_at (loc
, "cast specifies function type");
5594 return error_mark_node
;
5597 if (!VOID_TYPE_P (type
))
5599 value
= require_complete_type (loc
, value
);
5600 if (value
== error_mark_node
)
5601 return error_mark_node
;
5604 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
5606 if (RECORD_OR_UNION_TYPE_P (type
))
5607 pedwarn (loc
, OPT_Wpedantic
,
5608 "ISO C forbids casting nonscalar to the same type");
5610 /* Convert to remove any qualifiers from VALUE's type. */
5611 value
= convert (type
, value
);
5613 else if (TREE_CODE (type
) == UNION_TYPE
)
5617 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5618 if (TREE_TYPE (field
) != error_mark_node
5619 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
5620 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
5626 bool maybe_const
= true;
5628 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
5629 t
= c_fully_fold (value
, false, &maybe_const
);
5630 t
= build_constructor_single (type
, field
, t
);
5632 t
= c_wrap_maybe_const (t
, true);
5633 t
= digest_init (loc
, type
, t
,
5634 NULL_TREE
, false, true, 0);
5635 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
5638 error_at (loc
, "cast to union type from type not present in union");
5639 return error_mark_node
;
5645 if (type
== void_type_node
)
5647 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5648 SET_EXPR_LOCATION (t
, loc
);
5652 otype
= TREE_TYPE (value
);
5654 /* Optionally warn about potentially worrisome casts. */
5656 && TREE_CODE (type
) == POINTER_TYPE
5657 && TREE_CODE (otype
) == POINTER_TYPE
)
5658 handle_warn_cast_qual (loc
, type
, otype
);
5660 /* Warn about conversions between pointers to disjoint
5662 if (TREE_CODE (type
) == POINTER_TYPE
5663 && TREE_CODE (otype
) == POINTER_TYPE
5664 && !null_pointer_constant_p (value
))
5666 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5667 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5668 addr_space_t as_common
;
5670 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5672 if (ADDR_SPACE_GENERIC_P (as_from
))
5673 warning_at (loc
, 0, "cast to %s address space pointer "
5674 "from disjoint generic address space pointer",
5675 c_addr_space_name (as_to
));
5677 else if (ADDR_SPACE_GENERIC_P (as_to
))
5678 warning_at (loc
, 0, "cast to generic address space pointer "
5679 "from disjoint %s address space pointer",
5680 c_addr_space_name (as_from
));
5683 warning_at (loc
, 0, "cast to %s address space pointer "
5684 "from disjoint %s address space pointer",
5685 c_addr_space_name (as_to
),
5686 c_addr_space_name (as_from
));
5690 /* Warn about possible alignment problems. */
5691 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
5692 && TREE_CODE (type
) == POINTER_TYPE
5693 && TREE_CODE (otype
) == POINTER_TYPE
5694 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5695 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5696 /* Don't warn about opaque types, where the actual alignment
5697 restriction is unknown. */
5698 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype
))
5699 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5700 && min_align_of_type (TREE_TYPE (type
))
5701 > min_align_of_type (TREE_TYPE (otype
)))
5702 warning_at (loc
, OPT_Wcast_align
,
5703 "cast increases required alignment of target type");
5705 if (TREE_CODE (type
) == INTEGER_TYPE
5706 && TREE_CODE (otype
) == POINTER_TYPE
5707 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5708 /* Unlike conversion of integers to pointers, where the
5709 warning is disabled for converting constants because
5710 of cases such as SIG_*, warn about converting constant
5711 pointers to integers. In some cases it may cause unwanted
5712 sign extension, and a warning is appropriate. */
5713 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5714 "cast from pointer to integer of different size");
5716 if (TREE_CODE (value
) == CALL_EXPR
5717 && TREE_CODE (type
) != TREE_CODE (otype
))
5718 warning_at (loc
, OPT_Wbad_function_cast
,
5719 "cast from function call of type %qT "
5720 "to non-matching type %qT", otype
, type
);
5722 if (TREE_CODE (type
) == POINTER_TYPE
5723 && TREE_CODE (otype
) == INTEGER_TYPE
5724 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5725 /* Don't warn about converting any constant. */
5726 && !TREE_CONSTANT (value
))
5728 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5729 "of different size");
5731 if (warn_strict_aliasing
<= 2)
5732 strict_aliasing_warning (EXPR_LOCATION (value
), type
, expr
);
5734 /* If pedantic, warn for conversions between function and object
5735 pointer types, except for converting a null pointer constant
5736 to function pointer type. */
5738 && TREE_CODE (type
) == POINTER_TYPE
5739 && TREE_CODE (otype
) == POINTER_TYPE
5740 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5741 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5742 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5743 "conversion of function pointer to object pointer type");
5746 && TREE_CODE (type
) == POINTER_TYPE
5747 && TREE_CODE (otype
) == POINTER_TYPE
5748 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5749 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5750 && !null_pointer_constant_p (value
))
5751 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5752 "conversion of object pointer to function pointer type");
5754 if (TREE_CODE (type
) == POINTER_TYPE
5755 && TREE_CODE (otype
) == POINTER_TYPE
5756 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5757 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5758 && !c_safe_function_type_cast_p (TREE_TYPE (type
),
5760 warning_at (loc
, OPT_Wcast_function_type
,
5761 "cast between incompatible function types"
5762 " from %qT to %qT", otype
, type
);
5765 value
= convert (type
, value
);
5767 /* Ignore any integer overflow caused by the cast. */
5768 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5770 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5772 if (!TREE_OVERFLOW (value
))
5774 /* Avoid clobbering a shared constant. */
5775 value
= copy_node (value
);
5776 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5779 else if (TREE_OVERFLOW (value
))
5780 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5781 value
= wide_int_to_tree (TREE_TYPE (value
), wi::to_wide (value
));
5785 /* Don't let a cast be an lvalue. */
5786 if (lvalue_p (value
))
5787 value
= non_lvalue_loc (loc
, value
);
5789 /* Don't allow the results of casting to floating-point or complex
5790 types be confused with actual constants, or casts involving
5791 integer and pointer types other than direct integer-to-integer
5792 and integer-to-pointer be confused with integer constant
5793 expressions and null pointer constants. */
5794 if (TREE_CODE (value
) == REAL_CST
5795 || TREE_CODE (value
) == COMPLEX_CST
5796 || (TREE_CODE (value
) == INTEGER_CST
5797 && !((TREE_CODE (expr
) == INTEGER_CST
5798 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5799 || TREE_CODE (expr
) == REAL_CST
5800 || TREE_CODE (expr
) == COMPLEX_CST
)))
5801 value
= build1 (NOP_EXPR
, type
, value
);
5803 protected_set_expr_location (value
, loc
);
5807 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5808 location of the open paren of the cast, or the position of the cast
5811 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5814 tree type_expr
= NULL_TREE
;
5815 bool type_expr_const
= true;
5817 int saved_wsp
= warn_strict_prototypes
;
5819 /* This avoids warnings about unprototyped casts on
5820 integers. E.g. "#define SIG_DFL (void(*)())0". */
5821 if (TREE_CODE (expr
) == INTEGER_CST
)
5822 warn_strict_prototypes
= 0;
5823 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5824 warn_strict_prototypes
= saved_wsp
;
5826 if (TREE_CODE (expr
) == ADDR_EXPR
&& !VOID_TYPE_P (type
)
5827 && reject_gcc_builtin (expr
))
5828 return error_mark_node
;
5830 ret
= build_c_cast (loc
, type
, expr
);
5833 bool inner_expr_const
= true;
5834 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5835 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5836 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5837 && inner_expr_const
);
5838 SET_EXPR_LOCATION (ret
, loc
);
5841 if (!EXPR_HAS_LOCATION (ret
))
5842 protected_set_expr_location (ret
, loc
);
5844 /* C++ does not permits types to be defined in a cast, but it
5845 allows references to incomplete types. */
5846 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5847 warning_at (loc
, OPT_Wc___compat
,
5848 "defining a type in a cast is invalid in C++");
5853 /* Build an assignment expression of lvalue LHS from value RHS.
5854 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5855 may differ from TREE_TYPE (LHS) for an enum bitfield.
5856 MODIFYCODE is the code for a binary operator that we use
5857 to combine the old value of LHS with RHS to get the new value.
5858 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5859 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5860 which may differ from TREE_TYPE (RHS) for an enum value.
5862 LOCATION is the location of the MODIFYCODE operator.
5863 RHS_LOC is the location of the RHS. */
5866 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5867 enum tree_code modifycode
,
5868 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5872 tree rhseval
= NULL_TREE
;
5873 tree lhstype
= TREE_TYPE (lhs
);
5874 tree olhstype
= lhstype
;
5878 /* Types that aren't fully specified cannot be used in assignments. */
5879 lhs
= require_complete_type (location
, lhs
);
5881 /* Avoid duplicate error messages from operands that had errors. */
5882 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5883 return error_mark_node
;
5885 /* Ensure an error for assigning a non-lvalue array to an array in
5887 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5889 error_at (location
, "assignment to expression with array type");
5890 return error_mark_node
;
5893 /* For ObjC properties, defer this check. */
5894 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5895 return error_mark_node
;
5897 is_atomic_op
= really_atomic_lvalue (lhs
);
5901 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5903 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5904 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5906 if (inner
== error_mark_node
)
5907 return error_mark_node
;
5908 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5909 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5910 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5911 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5912 protected_set_expr_location (result
, location
);
5916 /* If a binary op has been requested, combine the old LHS value with the RHS
5917 producing the value we should actually store into the LHS. */
5919 if (modifycode
!= NOP_EXPR
)
5921 lhs
= c_fully_fold (lhs
, false, NULL
, true);
5922 lhs
= stabilize_reference (lhs
);
5924 /* Construct the RHS for any non-atomic compound assignemnt. */
5927 /* If in LHS op= RHS the RHS has side-effects, ensure they
5928 are preevaluated before the rest of the assignment expression's
5929 side-effects, because RHS could contain e.g. function calls
5931 if (TREE_SIDE_EFFECTS (rhs
))
5933 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5934 newrhs
= save_expr (TREE_OPERAND (rhs
, 0));
5936 newrhs
= save_expr (rhs
);
5938 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5939 newrhs
= build1 (EXCESS_PRECISION_EXPR
, TREE_TYPE (rhs
),
5942 newrhs
= build_binary_op (location
,
5943 modifycode
, lhs
, newrhs
, true);
5945 /* The original type of the right hand side is no longer
5947 rhs_origtype
= NULL_TREE
;
5951 if (c_dialect_objc ())
5953 /* Check if we are modifying an Objective-C property reference;
5954 if so, we need to generate setter calls. */
5955 if (TREE_CODE (newrhs
) == EXCESS_PRECISION_EXPR
)
5956 result
= objc_maybe_build_modify_expr (lhs
, TREE_OPERAND (newrhs
, 0));
5958 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5962 /* Else, do the check that we postponed for Objective-C. */
5963 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5964 return error_mark_node
;
5967 /* Give an error for storing in something that is 'const'. */
5969 if (TYPE_READONLY (lhstype
)
5970 || (RECORD_OR_UNION_TYPE_P (lhstype
)
5971 && C_TYPE_FIELDS_READONLY (lhstype
)))
5973 readonly_error (location
, lhs
, lv_assign
);
5974 return error_mark_node
;
5976 else if (TREE_READONLY (lhs
))
5977 readonly_warning (lhs
, lv_assign
);
5979 /* If storing into a structure or union member,
5980 it has probably been given type `int'.
5981 Compute the type that would go with
5982 the actual amount of storage the member occupies. */
5984 if (TREE_CODE (lhs
) == COMPONENT_REF
5985 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5986 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5987 || TREE_CODE (lhstype
) == REAL_TYPE
5988 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5989 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5991 /* If storing in a field that is in actuality a short or narrower than one,
5992 we must store in the field in its actual type. */
5994 if (lhstype
!= TREE_TYPE (lhs
))
5996 lhs
= copy_node (lhs
);
5997 TREE_TYPE (lhs
) = lhstype
;
6000 /* Issue -Wc++-compat warnings about an assignment to an enum type
6001 when LHS does not have its original type. This happens for,
6002 e.g., an enum bitfield in a struct. */
6004 && lhs_origtype
!= NULL_TREE
6005 && lhs_origtype
!= lhstype
6006 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
6008 tree checktype
= (rhs_origtype
!= NULL_TREE
6011 if (checktype
!= error_mark_node
6012 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
6013 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
6014 warning_at (location
, OPT_Wc___compat
,
6015 "enum conversion in assignment is invalid in C++");
6018 /* If the lhs is atomic, remove that qualifier. */
6021 lhstype
= build_qualified_type (lhstype
,
6022 (TYPE_QUALS (lhstype
)
6023 & ~TYPE_QUAL_ATOMIC
));
6024 olhstype
= build_qualified_type (olhstype
,
6025 (TYPE_QUALS (lhstype
)
6026 & ~TYPE_QUAL_ATOMIC
));
6029 /* Convert new value to destination type. Fold it first, then
6030 restore any excess precision information, for the sake of
6031 conversion warnings. */
6033 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
6035 tree rhs_semantic_type
= NULL_TREE
;
6036 if (TREE_CODE (newrhs
) == EXCESS_PRECISION_EXPR
)
6038 rhs_semantic_type
= TREE_TYPE (newrhs
);
6039 newrhs
= TREE_OPERAND (newrhs
, 0);
6041 npc
= null_pointer_constant_p (newrhs
);
6042 newrhs
= c_fully_fold (newrhs
, false, NULL
);
6043 if (rhs_semantic_type
)
6044 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
6045 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
6046 rhs_origtype
, ic_assign
, npc
,
6047 NULL_TREE
, NULL_TREE
, 0);
6048 if (TREE_CODE (newrhs
) == ERROR_MARK
)
6049 return error_mark_node
;
6052 /* Emit ObjC write barrier, if necessary. */
6053 if (c_dialect_objc () && flag_objc_gc
)
6055 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
6058 protected_set_expr_location (result
, location
);
6063 /* Scan operands. */
6066 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
6069 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
6070 TREE_SIDE_EFFECTS (result
) = 1;
6071 protected_set_expr_location (result
, location
);
6074 /* If we got the LHS in a different type for storing in,
6075 convert the result back to the nominal type of LHS
6076 so that the value we return always has the same type
6077 as the LHS argument. */
6079 if (olhstype
== TREE_TYPE (result
))
6082 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
6083 rhs_origtype
, ic_assign
, false, NULL_TREE
,
6085 protected_set_expr_location (result
, location
);
6089 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
6093 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6094 This is used to implement -fplan9-extensions. */
6097 find_anonymous_field_with_type (tree struct_type
, tree type
)
6102 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type
));
6104 for (field
= TYPE_FIELDS (struct_type
);
6106 field
= TREE_CHAIN (field
))
6108 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
6109 ? c_build_qualified_type (TREE_TYPE (field
),
6111 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
6112 if (DECL_NAME (field
) == NULL
6113 && comptypes (type
, fieldtype
))
6119 else if (DECL_NAME (field
) == NULL
6120 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
6121 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
6131 /* RHS is an expression whose type is pointer to struct. If there is
6132 an anonymous field in RHS with type TYPE, then return a pointer to
6133 that field in RHS. This is used with -fplan9-extensions. This
6134 returns NULL if no conversion could be found. */
6137 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
6139 tree rhs_struct_type
, lhs_main_type
;
6140 tree field
, found_field
;
6141 bool found_sub_field
;
6144 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
6145 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
6146 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type
));
6148 gcc_assert (POINTER_TYPE_P (type
));
6149 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
6150 ? c_build_qualified_type (TREE_TYPE (type
),
6152 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6154 found_field
= NULL_TREE
;
6155 found_sub_field
= false;
6156 for (field
= TYPE_FIELDS (rhs_struct_type
);
6158 field
= TREE_CHAIN (field
))
6160 if (DECL_NAME (field
) != NULL_TREE
6161 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
6163 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
6164 ? c_build_qualified_type (TREE_TYPE (field
),
6166 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
6167 if (comptypes (lhs_main_type
, fieldtype
))
6169 if (found_field
!= NULL_TREE
)
6171 found_field
= field
;
6173 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
6176 if (found_field
!= NULL_TREE
)
6178 found_field
= field
;
6179 found_sub_field
= true;
6183 if (found_field
== NULL_TREE
)
6186 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
6187 build_fold_indirect_ref (rhs
), found_field
,
6189 ret
= build_fold_addr_expr_loc (location
, ret
);
6191 if (found_sub_field
)
6193 ret
= convert_to_anonymous_field (location
, type
, ret
);
6194 gcc_assert (ret
!= NULL_TREE
);
6200 /* Issue an error message for a bad initializer component.
6201 GMSGID identifies the message.
6202 The component name is taken from the spelling stack. */
6205 error_init (location_t loc
, const char *gmsgid
)
6209 auto_diagnostic_group d
;
6211 /* The gmsgid may be a format string with %< and %>. */
6212 error_at (loc
, gmsgid
);
6213 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6215 inform (loc
, "(near initialization for %qs)", ofwhat
);
6218 /* Issue a pedantic warning for a bad initializer component. OPT is
6219 the option OPT_* (from options.h) controlling this warning or 0 if
6220 it is unconditionally given. GMSGID identifies the message. The
6221 component name is taken from the spelling stack. */
6223 static void ATTRIBUTE_GCC_DIAG (3,0)
6224 pedwarn_init (location_t loc
, int opt
, const char *gmsgid
, ...)
6226 /* Use the location where a macro was expanded rather than where
6227 it was defined to make sure macros defined in system headers
6228 but used incorrectly elsewhere are diagnosed. */
6229 source_location exploc
= expansion_point_location_if_in_system_header (loc
);
6230 auto_diagnostic_group d
;
6232 va_start (ap
, gmsgid
);
6233 bool warned
= emit_diagnostic_valist (DK_PEDWARN
, exploc
, opt
, gmsgid
, &ap
);
6235 char *ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6236 if (*ofwhat
&& warned
)
6237 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6240 /* Issue a warning for a bad initializer component.
6242 OPT is the OPT_W* value corresponding to the warning option that
6243 controls this warning. GMSGID identifies the message. The
6244 component name is taken from the spelling stack. */
6247 warning_init (location_t loc
, int opt
, const char *gmsgid
)
6252 auto_diagnostic_group d
;
6254 /* Use the location where a macro was expanded rather than where
6255 it was defined to make sure macros defined in system headers
6256 but used incorrectly elsewhere are diagnosed. */
6257 source_location exploc
= expansion_point_location_if_in_system_header (loc
);
6259 /* The gmsgid may be a format string with %< and %>. */
6260 warned
= warning_at (exploc
, opt
, gmsgid
);
6261 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6262 if (*ofwhat
&& warned
)
6263 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6266 /* If TYPE is an array type and EXPR is a parenthesized string
6267 constant, warn if pedantic that EXPR is being used to initialize an
6268 object of type TYPE. */
6271 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
6274 && TREE_CODE (type
) == ARRAY_TYPE
6275 && TREE_CODE (expr
.value
) == STRING_CST
6276 && expr
.original_code
!= STRING_CST
)
6277 pedwarn_init (loc
, OPT_Wpedantic
,
6278 "array initialized from parenthesized string constant");
6281 /* Attempt to locate the parameter with the given index within FNDECL,
6282 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6285 get_fndecl_argument_location (tree fndecl
, int argnum
)
6290 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6291 for (i
= 0, param
= DECL_ARGUMENTS (fndecl
);
6292 i
< argnum
&& param
;
6293 i
++, param
= TREE_CHAIN (param
))
6296 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6297 return DECL_SOURCE_LOCATION (FNDECL). */
6299 return DECL_SOURCE_LOCATION (fndecl
);
6301 return DECL_SOURCE_LOCATION (param
);
6304 /* Issue a note about a mismatching argument for parameter PARMNUM
6305 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6306 Attempt to issue the note at the pertinent parameter of the decl;
6307 failing that issue it at the location of FUNDECL; failing that
6308 issue it at PLOC. */
6311 inform_for_arg (tree fundecl
, location_t ploc
, int parmnum
,
6312 tree expected_type
, tree actual_type
)
6315 if (fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6316 loc
= get_fndecl_argument_location (fundecl
, parmnum
- 1);
6321 "expected %qT but argument is of type %qT",
6322 expected_type
, actual_type
);
6325 /* Convert value RHS to type TYPE as preparation for an assignment to
6326 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6327 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6328 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6329 constant before any folding.
6330 The real work of conversion is done by `convert'.
6331 The purpose of this function is to generate error messages
6332 for assignments that are not allowed in C.
6333 ERRTYPE says whether it is argument passing, assignment,
6334 initialization or return.
6336 In the following example, '~' denotes where EXPR_LOC and '^' where
6339 f (var); [ic_argpass]
6341 x = var; [ic_assign]
6343 int x = var; [ic_init]
6345 return x; [ic_return]
6348 FUNCTION is a tree for the function being called.
6349 PARMNUM is the number of the argument, for printing in error messages. */
6352 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
6353 tree rhs
, tree origtype
, enum impl_conv errtype
,
6354 bool null_pointer_constant
, tree fundecl
,
6355 tree function
, int parmnum
)
6357 enum tree_code codel
= TREE_CODE (type
);
6358 tree orig_rhs
= rhs
;
6360 enum tree_code coder
;
6361 tree rname
= NULL_TREE
;
6362 bool objc_ok
= false;
6364 /* Use the expansion point location to handle cases such as user's
6365 function returning a wrong-type macro defined in a system header. */
6366 location
= expansion_point_location_if_in_system_header (location
);
6368 if (errtype
== ic_argpass
)
6371 /* Change pointer to function to the function itself for
6373 if (TREE_CODE (function
) == ADDR_EXPR
6374 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
6375 function
= TREE_OPERAND (function
, 0);
6377 /* Handle an ObjC selector specially for diagnostics. */
6378 selector
= objc_message_selector ();
6380 if (selector
&& parmnum
> 2)
6387 /* This macro is used to emit diagnostics to ensure that all format
6388 strings are complete sentences, visible to gettext and checked at
6390 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6396 auto_diagnostic_group d; \
6397 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6398 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6402 pedwarn (LOCATION, OPT, AS); \
6405 pedwarn_init (LOCATION, OPT, IN); \
6408 pedwarn (LOCATION, OPT, RE); \
6411 gcc_unreachable (); \
6415 /* This macro is used to emit diagnostics to ensure that all format
6416 strings are complete sentences, visible to gettext and checked at
6417 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6418 extra parameter to enumerate qualifiers. */
6419 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6425 auto_diagnostic_group d; \
6426 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6427 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6431 pedwarn (LOCATION, OPT, AS, QUALS); \
6434 pedwarn (LOCATION, OPT, IN, QUALS); \
6437 pedwarn (LOCATION, OPT, RE, QUALS); \
6440 gcc_unreachable (); \
6444 /* This macro is used to emit diagnostics to ensure that all format
6445 strings are complete sentences, visible to gettext and checked at
6446 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6447 warning_at instead of pedwarn. */
6448 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6454 auto_diagnostic_group d; \
6455 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6456 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6460 warning_at (LOCATION, OPT, AS, QUALS); \
6463 warning_at (LOCATION, OPT, IN, QUALS); \
6466 warning_at (LOCATION, OPT, RE, QUALS); \
6469 gcc_unreachable (); \
6473 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6474 rhs
= TREE_OPERAND (rhs
, 0);
6476 rhstype
= TREE_TYPE (rhs
);
6477 coder
= TREE_CODE (rhstype
);
6479 if (coder
== ERROR_MARK
)
6480 return error_mark_node
;
6482 if (c_dialect_objc ())
6505 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
6508 if (warn_cxx_compat
)
6510 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
6511 if (checktype
!= error_mark_node
6512 && TREE_CODE (type
) == ENUMERAL_TYPE
6513 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
6517 if (pedwarn (expr_loc
, OPT_Wc___compat
, "enum conversion when "
6518 "passing argument %d of %qE is invalid in C++",
6520 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6521 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6522 "expected %qT but argument is of type %qT",
6526 pedwarn (location
, OPT_Wc___compat
, "enum conversion from %qT to "
6527 "%qT in assignment is invalid in C++", rhstype
, type
);
6530 pedwarn_init (location
, OPT_Wc___compat
, "enum conversion from "
6531 "%qT to %qT in initialization is invalid in C++",
6535 pedwarn (location
, OPT_Wc___compat
, "enum conversion from %qT to "
6536 "%qT in return is invalid in C++", rhstype
, type
);
6543 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
6546 if (coder
== VOID_TYPE
)
6548 /* Except for passing an argument to an unprototyped function,
6549 this is a constraint violation. When passing an argument to
6550 an unprototyped function, it is compile-time undefined;
6551 making it a constraint in that case was rejected in
6553 error_at (location
, "void value not ignored as it ought to be");
6554 return error_mark_node
;
6556 rhs
= require_complete_type (location
, rhs
);
6557 if (rhs
== error_mark_node
)
6558 return error_mark_node
;
6560 if (coder
== POINTER_TYPE
&& reject_gcc_builtin (rhs
))
6561 return error_mark_node
;
6563 /* A non-reference type can convert to a reference. This handles
6564 va_start, va_copy and possibly port built-ins. */
6565 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
6567 if (!lvalue_p (rhs
))
6569 error_at (location
, "cannot pass rvalue to reference parameter");
6570 return error_mark_node
;
6572 if (!c_mark_addressable (rhs
))
6573 return error_mark_node
;
6574 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
6575 SET_EXPR_LOCATION (rhs
, location
);
6577 rhs
= convert_for_assignment (location
, expr_loc
,
6578 build_pointer_type (TREE_TYPE (type
)),
6579 rhs
, origtype
, errtype
,
6580 null_pointer_constant
, fundecl
, function
,
6582 if (rhs
== error_mark_node
)
6583 return error_mark_node
;
6585 rhs
= build1 (NOP_EXPR
, type
, rhs
);
6586 SET_EXPR_LOCATION (rhs
, location
);
6589 /* Some types can interconvert without explicit casts. */
6590 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
6591 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
6592 return convert (type
, rhs
);
6593 /* Arithmetic types all interconvert, and enum is treated like int. */
6594 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
6595 || codel
== FIXED_POINT_TYPE
6596 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
6597 || codel
== BOOLEAN_TYPE
)
6598 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
6599 || coder
== FIXED_POINT_TYPE
6600 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
6601 || coder
== BOOLEAN_TYPE
))
6604 bool save
= in_late_binary_op
;
6605 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
6606 || (coder
== REAL_TYPE
6607 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
6608 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
6609 in_late_binary_op
= true;
6610 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6611 ? expr_loc
: location
, type
, orig_rhs
);
6612 in_late_binary_op
= save
;
6616 /* Aggregates in different TUs might need conversion. */
6617 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
6619 && comptypes (type
, rhstype
))
6620 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6621 ? expr_loc
: location
, type
, rhs
);
6623 /* Conversion to a transparent union or record from its member types.
6624 This applies only to function arguments. */
6625 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
6626 && TYPE_TRANSPARENT_AGGR (type
))
6627 && errtype
== ic_argpass
)
6629 tree memb
, marginal_memb
= NULL_TREE
;
6631 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
6633 tree memb_type
= TREE_TYPE (memb
);
6635 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
6636 TYPE_MAIN_VARIANT (rhstype
)))
6639 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
6642 if (coder
== POINTER_TYPE
)
6644 tree ttl
= TREE_TYPE (memb_type
);
6645 tree ttr
= TREE_TYPE (rhstype
);
6647 /* Any non-function converts to a [const][volatile] void *
6648 and vice versa; otherwise, targets must be the same.
6649 Meanwhile, the lhs target must have all the qualifiers of
6651 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6652 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6653 || comp_target_types (location
, memb_type
, rhstype
))
6655 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
6656 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
6657 /* If this type won't generate any warnings, use it. */
6658 if (lquals
== rquals
6659 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
6660 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6661 ? ((lquals
| rquals
) == rquals
)
6662 : ((lquals
| rquals
) == lquals
)))
6665 /* Keep looking for a better type, but remember this one. */
6667 marginal_memb
= memb
;
6671 /* Can convert integer zero to any pointer type. */
6672 if (null_pointer_constant
)
6674 rhs
= null_pointer_node
;
6679 if (memb
|| marginal_memb
)
6683 /* We have only a marginally acceptable member type;
6684 it needs a warning. */
6685 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
6686 tree ttr
= TREE_TYPE (rhstype
);
6688 /* Const and volatile mean something different for function
6689 types, so the usual warnings are not appropriate. */
6690 if (TREE_CODE (ttr
) == FUNCTION_TYPE
6691 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6693 /* Because const and volatile on functions are
6694 restrictions that say the function will not do
6695 certain things, it is okay to use a const or volatile
6696 function where an ordinary one is wanted, but not
6698 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6699 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6700 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6701 OPT_Wdiscarded_qualifiers
,
6702 G_("passing argument %d of %qE "
6703 "makes %q#v qualified function "
6704 "pointer from unqualified"),
6705 G_("assignment makes %q#v qualified "
6706 "function pointer from "
6708 G_("initialization makes %q#v qualified "
6709 "function pointer from "
6711 G_("return makes %q#v qualified function "
6712 "pointer from unqualified"),
6713 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6715 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
6716 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
6717 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6718 OPT_Wdiscarded_qualifiers
,
6719 G_("passing argument %d of %qE discards "
6720 "%qv qualifier from pointer target type"),
6721 G_("assignment discards %qv qualifier "
6722 "from pointer target type"),
6723 G_("initialization discards %qv qualifier "
6724 "from pointer target type"),
6725 G_("return discards %qv qualifier from "
6726 "pointer target type"),
6727 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6729 memb
= marginal_memb
;
6732 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
6733 pedwarn (location
, OPT_Wpedantic
,
6734 "ISO C prohibits argument conversion to union type");
6736 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
6737 return build_constructor_single (type
, memb
, rhs
);
6741 /* Conversions among pointers */
6742 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
6743 && (coder
== codel
))
6745 tree ttl
= TREE_TYPE (type
);
6746 tree ttr
= TREE_TYPE (rhstype
);
6749 bool is_opaque_pointer
;
6750 int target_cmp
= 0; /* Cache comp_target_types () result. */
6754 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
6755 mvl
= (TYPE_ATOMIC (mvl
)
6756 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
6758 : TYPE_MAIN_VARIANT (mvl
));
6759 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
6760 mvr
= (TYPE_ATOMIC (mvr
)
6761 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
6763 : TYPE_MAIN_VARIANT (mvr
));
6764 /* Opaque pointers are treated like void pointers. */
6765 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
6767 /* The Plan 9 compiler permits a pointer to a struct to be
6768 automatically converted into a pointer to an anonymous field
6769 within the struct. */
6770 if (flag_plan9_extensions
6771 && RECORD_OR_UNION_TYPE_P (mvl
)
6772 && RECORD_OR_UNION_TYPE_P (mvr
)
6775 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
6776 if (new_rhs
!= NULL_TREE
)
6779 rhstype
= TREE_TYPE (rhs
);
6780 coder
= TREE_CODE (rhstype
);
6781 ttr
= TREE_TYPE (rhstype
);
6782 mvr
= TYPE_MAIN_VARIANT (ttr
);
6786 /* C++ does not allow the implicit conversion void* -> T*. However,
6787 for the purpose of reducing the number of false positives, we
6788 tolerate the special case of
6792 where NULL is typically defined in C to be '(void *) 0'. */
6793 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
6794 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
6796 "request for implicit conversion "
6797 "from %qT to %qT not permitted in C++", rhstype
, type
);
6799 /* See if the pointers point to incompatible address spaces. */
6800 asl
= TYPE_ADDR_SPACE (ttl
);
6801 asr
= TYPE_ADDR_SPACE (ttr
);
6802 if (!null_pointer_constant_p (rhs
)
6803 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
6808 error_at (expr_loc
, "passing argument %d of %qE from pointer to "
6809 "non-enclosed address space", parmnum
, rname
);
6812 error_at (location
, "assignment from pointer to "
6813 "non-enclosed address space");
6816 error_at (location
, "initialization from pointer to "
6817 "non-enclosed address space");
6820 error_at (location
, "return from pointer to "
6821 "non-enclosed address space");
6826 return error_mark_node
;
6829 /* Check if the right-hand side has a format attribute but the
6830 left-hand side doesn't. */
6831 if (warn_suggest_attribute_format
6832 && check_missing_format_attribute (type
, rhstype
))
6837 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
6838 "argument %d of %qE might be "
6839 "a candidate for a format attribute",
6843 warning_at (location
, OPT_Wsuggest_attribute_format
,
6844 "assignment left-hand side might be "
6845 "a candidate for a format attribute");
6848 warning_at (location
, OPT_Wsuggest_attribute_format
,
6849 "initialization left-hand side might be "
6850 "a candidate for a format attribute");
6853 warning_at (location
, OPT_Wsuggest_attribute_format
,
6854 "return type might be "
6855 "a candidate for a format attribute");
6862 /* Any non-function converts to a [const][volatile] void *
6863 and vice versa; otherwise, targets must be the same.
6864 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6865 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6866 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6867 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6868 || is_opaque_pointer
6869 || ((c_common_unsigned_type (mvl
)
6870 == c_common_unsigned_type (mvr
))
6871 && (c_common_signed_type (mvl
)
6872 == c_common_signed_type (mvr
))
6873 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6875 /* Warn about loss of qualifers from pointers to arrays with
6876 qualifiers on the element type. */
6877 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
6879 ttr
= strip_array_types (ttr
);
6880 ttl
= strip_array_types (ttl
);
6882 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6883 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6884 WARNING_FOR_QUALIFIERS (location
, expr_loc
,
6885 OPT_Wdiscarded_array_qualifiers
,
6886 G_("passing argument %d of %qE discards "
6887 "%qv qualifier from pointer target type"),
6888 G_("assignment discards %qv qualifier "
6889 "from pointer target type"),
6890 G_("initialization discards %qv qualifier "
6891 "from pointer target type"),
6892 G_("return discards %qv qualifier from "
6893 "pointer target type"),
6894 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6897 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6900 && !null_pointer_constant
6901 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6902 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
6903 G_("ISO C forbids passing argument %d of "
6904 "%qE between function pointer "
6906 G_("ISO C forbids assignment between "
6907 "function pointer and %<void *%>"),
6908 G_("ISO C forbids initialization between "
6909 "function pointer and %<void *%>"),
6910 G_("ISO C forbids return between function "
6911 "pointer and %<void *%>"));
6912 /* Const and volatile mean something different for function types,
6913 so the usual warnings are not appropriate. */
6914 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6915 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6917 /* Don't warn about loss of qualifier for conversions from
6918 qualified void* to pointers to arrays with corresponding
6919 qualifier on the element type. */
6921 ttl
= strip_array_types (ttl
);
6923 /* Assignments between atomic and non-atomic objects are OK. */
6924 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6925 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6927 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6928 OPT_Wdiscarded_qualifiers
,
6929 G_("passing argument %d of %qE discards "
6930 "%qv qualifier from pointer target type"),
6931 G_("assignment discards %qv qualifier "
6932 "from pointer target type"),
6933 G_("initialization discards %qv qualifier "
6934 "from pointer target type"),
6935 G_("return discards %qv qualifier from "
6936 "pointer target type"),
6937 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6939 /* If this is not a case of ignoring a mismatch in signedness,
6941 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6944 /* If there is a mismatch, do warn. */
6945 else if (warn_pointer_sign
)
6950 auto_diagnostic_group d
;
6951 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
6952 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
6953 if (pedwarn (&richloc
, OPT_Wpointer_sign
,
6954 "pointer targets in passing argument %d of "
6955 "%qE differ in signedness", parmnum
, rname
))
6956 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
,
6961 pedwarn (location
, OPT_Wpointer_sign
,
6962 "pointer targets in assignment from %qT to %qT "
6963 "differ in signedness", rhstype
, type
);
6966 pedwarn_init (location
, OPT_Wpointer_sign
,
6967 "pointer targets in initialization of %qT "
6968 "from %qT differ in signedness", type
,
6972 pedwarn (location
, OPT_Wpointer_sign
, "pointer targets in "
6973 "returning %qT from a function with return type "
6974 "%qT differ in signedness", rhstype
, type
);
6980 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6981 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6983 /* Because const and volatile on functions are restrictions
6984 that say the function will not do certain things,
6985 it is okay to use a const or volatile function
6986 where an ordinary one is wanted, but not vice-versa. */
6987 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6988 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6989 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6990 OPT_Wdiscarded_qualifiers
,
6991 G_("passing argument %d of %qE makes "
6992 "%q#v qualified function pointer "
6993 "from unqualified"),
6994 G_("assignment makes %q#v qualified function "
6995 "pointer from unqualified"),
6996 G_("initialization makes %q#v qualified "
6997 "function pointer from unqualified"),
6998 G_("return makes %q#v qualified function "
6999 "pointer from unqualified"),
7000 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
7003 /* Avoid warning about the volatile ObjC EH puts on decls. */
7010 auto_diagnostic_group d
;
7011 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7012 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7013 if (pedwarn (&richloc
, OPT_Wincompatible_pointer_types
,
7014 "passing argument %d of %qE from incompatible "
7015 "pointer type", parmnum
, rname
))
7016 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7020 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7021 "assignment to %qT from incompatible pointer type %qT",
7025 pedwarn_init (location
, OPT_Wincompatible_pointer_types
,
7026 "initialization of %qT from incompatible pointer "
7027 "type %qT", type
, rhstype
);
7030 pedwarn (location
, OPT_Wincompatible_pointer_types
,
7031 "returning %qT from a function with incompatible "
7032 "return type %qT", rhstype
, type
);
7039 return convert (type
, rhs
);
7041 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
7043 /* ??? This should not be an error when inlining calls to
7044 unprototyped functions. */
7045 error_at (location
, "invalid use of non-lvalue array");
7046 return error_mark_node
;
7048 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
7050 /* An explicit constant 0 can convert to a pointer,
7051 or one that results from arithmetic, even including
7052 a cast to integer type. */
7053 if (!null_pointer_constant
)
7058 auto_diagnostic_group d
;
7059 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7060 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7061 if (pedwarn (&richloc
, OPT_Wint_conversion
,
7062 "passing argument %d of %qE makes pointer from "
7063 "integer without a cast", parmnum
, rname
))
7064 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7068 pedwarn (location
, OPT_Wint_conversion
,
7069 "assignment to %qT from %qT makes pointer from integer "
7070 "without a cast", type
, rhstype
);
7073 pedwarn_init (location
, OPT_Wint_conversion
,
7074 "initialization of %qT from %qT makes pointer from "
7075 "integer without a cast", type
, rhstype
);
7078 pedwarn (location
, OPT_Wint_conversion
, "returning %qT from a "
7079 "function with return type %qT makes pointer from "
7080 "integer without a cast", rhstype
, type
);
7086 return convert (type
, rhs
);
7088 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
7094 auto_diagnostic_group d
;
7095 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7096 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7097 if (pedwarn (&richloc
, OPT_Wint_conversion
,
7098 "passing argument %d of %qE makes integer from "
7099 "pointer without a cast", parmnum
, rname
))
7100 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7104 pedwarn (location
, OPT_Wint_conversion
,
7105 "assignment to %qT from %qT makes integer from pointer "
7106 "without a cast", type
, rhstype
);
7109 pedwarn_init (location
, OPT_Wint_conversion
,
7110 "initialization of %qT from %qT makes integer from "
7111 "pointer without a cast", type
, rhstype
);
7114 pedwarn (location
, OPT_Wint_conversion
, "returning %qT from a "
7115 "function with return type %qT makes integer from "
7116 "pointer without a cast", rhstype
, type
);
7122 return convert (type
, rhs
);
7124 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
7127 bool save
= in_late_binary_op
;
7128 in_late_binary_op
= true;
7129 ret
= convert (type
, rhs
);
7130 in_late_binary_op
= save
;
7138 auto_diagnostic_group d
;
7139 range_label_for_type_mismatch
rhs_label (rhstype
, type
);
7140 gcc_rich_location
richloc (expr_loc
, &rhs_label
);
7141 error_at (&richloc
, "incompatible type for argument %d of %qE", parmnum
,
7143 inform_for_arg (fundecl
, expr_loc
, parmnum
, type
, rhstype
);
7147 error_at (location
, "incompatible types when assigning to type %qT from "
7148 "type %qT", type
, rhstype
);
7152 "incompatible types when initializing type %qT using type %qT",
7157 "incompatible types when returning type %qT but %qT was "
7158 "expected", rhstype
, type
);
7164 return error_mark_node
;
7167 /* If VALUE is a compound expr all of whose expressions are constant, then
7168 return its value. Otherwise, return error_mark_node.
7170 This is for handling COMPOUND_EXPRs as initializer elements
7171 which is allowed with a warning when -pedantic is specified. */
7174 valid_compound_expr_initializer (tree value
, tree endtype
)
7176 if (TREE_CODE (value
) == COMPOUND_EXPR
)
7178 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
7180 return error_mark_node
;
7181 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
7184 else if (!initializer_constant_valid_p (value
, endtype
))
7185 return error_mark_node
;
7190 /* Perform appropriate conversions on the initial value of a variable,
7191 store it in the declaration DECL,
7192 and print any error messages that are appropriate.
7193 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7194 If the init is invalid, store an ERROR_MARK.
7196 INIT_LOC is the location of the initial value. */
7199 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
7204 /* If variable's type was invalidly declared, just ignore it. */
7206 type
= TREE_TYPE (decl
);
7207 if (TREE_CODE (type
) == ERROR_MARK
)
7210 /* Digest the specified initializer into an expression. */
7213 npc
= null_pointer_constant_p (init
);
7214 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
7215 true, TREE_STATIC (decl
));
7217 /* Store the expression if valid; else report error. */
7219 if (!in_system_header_at (input_location
)
7220 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
7221 warning (OPT_Wtraditional
, "traditional C rejects automatic "
7222 "aggregate initialization");
7224 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
7225 DECL_INITIAL (decl
) = value
;
7227 /* ANSI wants warnings about out-of-range constant initializers. */
7228 STRIP_TYPE_NOPS (value
);
7229 if (TREE_STATIC (decl
))
7230 constant_expression_warning (value
);
7232 /* Check if we need to set array size from compound literal size. */
7233 if (TREE_CODE (type
) == ARRAY_TYPE
7234 && TYPE_DOMAIN (type
) == NULL_TREE
7235 && value
!= error_mark_node
)
7237 tree inside_init
= init
;
7239 STRIP_TYPE_NOPS (inside_init
);
7240 inside_init
= fold (inside_init
);
7242 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7244 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
7246 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
7248 /* For int foo[] = (int [3]){1}; we need to set array size
7249 now since later on array initializer will be just the
7250 brace enclosed list of the compound literal. */
7251 tree etype
= strip_array_types (TREE_TYPE (decl
));
7252 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
7253 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
7255 layout_decl (cldecl
, 0);
7257 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
7263 /* Methods for storing and printing names for error messages. */
7265 /* Implement a spelling stack that allows components of a name to be pushed
7266 and popped. Each element on the stack is this structure. */
7273 unsigned HOST_WIDE_INT i
;
7278 #define SPELLING_STRING 1
7279 #define SPELLING_MEMBER 2
7280 #define SPELLING_BOUNDS 3
7282 static struct spelling
*spelling
; /* Next stack element (unused). */
7283 static struct spelling
*spelling_base
; /* Spelling stack base. */
7284 static int spelling_size
; /* Size of the spelling stack. */
7286 /* Macros to save and restore the spelling stack around push_... functions.
7287 Alternative to SAVE_SPELLING_STACK. */
7289 #define SPELLING_DEPTH() (spelling - spelling_base)
7290 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7292 /* Push an element on the spelling stack with type KIND and assign VALUE
7295 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7297 int depth = SPELLING_DEPTH (); \
7299 if (depth >= spelling_size) \
7301 spelling_size += 10; \
7302 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7304 RESTORE_SPELLING_DEPTH (depth); \
7307 spelling->kind = (KIND); \
7308 spelling->MEMBER = (VALUE); \
7312 /* Push STRING on the stack. Printed literally. */
7315 push_string (const char *string
)
7317 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
7320 /* Push a member name on the stack. Printed as '.' STRING. */
7323 push_member_name (tree decl
)
7325 const char *const string
7327 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
7328 : _("<anonymous>"));
7329 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
7332 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
7335 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
7337 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
7340 /* Compute the maximum size in bytes of the printed spelling. */
7343 spelling_length (void)
7348 for (p
= spelling_base
; p
< spelling
; p
++)
7350 if (p
->kind
== SPELLING_BOUNDS
)
7353 size
+= strlen (p
->u
.s
) + 1;
7359 /* Print the spelling to BUFFER and return it. */
7362 print_spelling (char *buffer
)
7367 for (p
= spelling_base
; p
< spelling
; p
++)
7368 if (p
->kind
== SPELLING_BOUNDS
)
7370 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
7376 if (p
->kind
== SPELLING_MEMBER
)
7378 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
7385 /* Digest the parser output INIT as an initializer for type TYPE.
7386 Return a C expression of type TYPE to represent the initial value.
7388 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7390 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7392 If INIT is a string constant, STRICT_STRING is true if it is
7393 unparenthesized or we should not warn here for it being parenthesized.
7394 For other types of INIT, STRICT_STRING is not used.
7396 INIT_LOC is the location of the INIT.
7398 REQUIRE_CONSTANT requests an error if non-constant initializers or
7399 elements are seen. */
7402 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
7403 bool null_pointer_constant
, bool strict_string
,
7404 int require_constant
)
7406 enum tree_code code
= TREE_CODE (type
);
7407 tree inside_init
= init
;
7408 tree semantic_type
= NULL_TREE
;
7409 bool maybe_const
= true;
7411 if (type
== error_mark_node
7413 || error_operand_p (init
))
7414 return error_mark_node
;
7416 STRIP_TYPE_NOPS (inside_init
);
7418 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
7420 semantic_type
= TREE_TYPE (inside_init
);
7421 inside_init
= TREE_OPERAND (inside_init
, 0);
7423 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
7425 /* Initialization of an array of chars from a string constant
7426 optionally enclosed in braces. */
7428 if (code
== ARRAY_TYPE
&& inside_init
7429 && TREE_CODE (inside_init
) == STRING_CST
)
7432 = (TYPE_ATOMIC (TREE_TYPE (type
))
7433 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
7435 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
7436 /* Note that an array could be both an array of character type
7437 and an array of wchar_t if wchar_t is signed char or unsigned
7439 bool char_array
= (typ1
== char_type_node
7440 || typ1
== signed_char_type_node
7441 || typ1
== unsigned_char_type_node
);
7442 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
7443 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
7444 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
7446 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
7449 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
7450 expr
.value
= inside_init
;
7451 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
7452 expr
.original_type
= NULL
;
7453 maybe_warn_string_init (init_loc
, type
, expr
);
7455 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
7456 pedwarn_init (init_loc
, OPT_Wpedantic
,
7457 "initialization of a flexible array member");
7459 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7460 TYPE_MAIN_VARIANT (type
)))
7465 if (typ2
!= char_type_node
)
7467 error_init (init_loc
, "char-array initialized from wide "
7469 return error_mark_node
;
7474 if (typ2
== char_type_node
)
7476 error_init (init_loc
, "wide character array initialized "
7477 "from non-wide string");
7478 return error_mark_node
;
7480 else if (!comptypes(typ1
, typ2
))
7482 error_init (init_loc
, "wide character array initialized "
7483 "from incompatible wide string");
7484 return error_mark_node
;
7488 if (TYPE_DOMAIN (type
) != NULL_TREE
7489 && TYPE_SIZE (type
) != NULL_TREE
7490 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
7492 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
7493 unsigned unit
= TYPE_PRECISION (typ1
) / BITS_PER_UNIT
;
7495 /* Subtract the size of a single (possibly wide) character
7496 because it's ok to ignore the terminating null char
7497 that is counted in the length of the constant. */
7498 if (compare_tree_int (TYPE_SIZE_UNIT (type
), len
- unit
) < 0)
7499 pedwarn_init (init_loc
, 0,
7500 ("initializer-string for array of chars "
7502 else if (warn_cxx_compat
7503 && compare_tree_int (TYPE_SIZE_UNIT (type
), len
) < 0)
7504 warning_at (init_loc
, OPT_Wc___compat
,
7505 ("initializer-string for array chars "
7506 "is too long for C++"));
7507 if (compare_tree_int (TYPE_SIZE_UNIT (type
), len
) < 0)
7509 unsigned HOST_WIDE_INT size
7510 = tree_to_uhwi (TYPE_SIZE_UNIT (type
));
7511 const char *p
= TREE_STRING_POINTER (inside_init
);
7513 inside_init
= build_string (size
, p
);
7517 TREE_TYPE (inside_init
) = type
;
7520 else if (INTEGRAL_TYPE_P (typ1
))
7522 error_init (init_loc
, "array of inappropriate type initialized "
7523 "from string constant");
7524 return error_mark_node
;
7528 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7529 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7530 below and handle as a constructor. */
7531 if (code
== VECTOR_TYPE
7532 && VECTOR_TYPE_P (TREE_TYPE (inside_init
))
7533 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
7534 && TREE_CONSTANT (inside_init
))
7536 if (TREE_CODE (inside_init
) == VECTOR_CST
7537 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7538 TYPE_MAIN_VARIANT (type
)))
7541 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
7543 unsigned HOST_WIDE_INT ix
;
7545 bool constant_p
= true;
7547 /* Iterate through elements and check if all constructor
7548 elements are *_CSTs. */
7549 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
7550 if (!CONSTANT_CLASS_P (value
))
7557 return build_vector_from_ctor (type
,
7558 CONSTRUCTOR_ELTS (inside_init
));
7562 if (warn_sequence_point
)
7563 verify_sequence_points (inside_init
);
7565 /* Any type can be initialized
7566 from an expression of the same type, optionally with braces. */
7568 if (inside_init
&& TREE_TYPE (inside_init
) != NULL_TREE
7569 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7570 TYPE_MAIN_VARIANT (type
))
7571 || (code
== ARRAY_TYPE
7572 && comptypes (TREE_TYPE (inside_init
), type
))
7573 || (code
== VECTOR_TYPE
7574 && comptypes (TREE_TYPE (inside_init
), type
))
7575 || (code
== POINTER_TYPE
7576 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
7577 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
7578 TREE_TYPE (type
)))))
7580 if (code
== POINTER_TYPE
)
7582 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
7584 if (TREE_CODE (inside_init
) == STRING_CST
7585 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7586 inside_init
= array_to_pointer_conversion
7587 (init_loc
, inside_init
);
7590 error_init (init_loc
, "invalid use of non-lvalue array");
7591 return error_mark_node
;
7596 if (code
== VECTOR_TYPE
)
7597 /* Although the types are compatible, we may require a
7599 inside_init
= convert (type
, inside_init
);
7601 if (require_constant
7602 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7604 /* As an extension, allow initializing objects with static storage
7605 duration with compound literals (which are then treated just as
7606 the brace enclosed list they contain). Also allow this for
7607 vectors, as we can only assign them with compound literals. */
7608 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
7609 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
7611 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
7612 inside_init
= DECL_INITIAL (decl
);
7615 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
7616 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
7618 error_init (init_loc
, "array initialized from non-constant array "
7620 return error_mark_node
;
7623 /* Compound expressions can only occur here if -Wpedantic or
7624 -pedantic-errors is specified. In the later case, we always want
7625 an error. In the former case, we simply want a warning. */
7626 if (require_constant
&& pedantic
7627 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
7630 = valid_compound_expr_initializer (inside_init
,
7631 TREE_TYPE (inside_init
));
7632 if (inside_init
== error_mark_node
)
7633 error_init (init_loc
, "initializer element is not constant");
7635 pedwarn_init (init_loc
, OPT_Wpedantic
,
7636 "initializer element is not constant");
7637 if (flag_pedantic_errors
)
7638 inside_init
= error_mark_node
;
7640 else if (require_constant
7641 && !initializer_constant_valid_p (inside_init
,
7642 TREE_TYPE (inside_init
)))
7644 error_init (init_loc
, "initializer element is not constant");
7645 inside_init
= error_mark_node
;
7647 else if (require_constant
&& !maybe_const
)
7648 pedwarn_init (init_loc
, OPT_Wpedantic
,
7649 "initializer element is not a constant expression");
7651 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7652 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
7653 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
7654 type
, inside_init
, origtype
,
7655 ic_init
, null_pointer_constant
,
7656 NULL_TREE
, NULL_TREE
, 0);
7660 /* Handle scalar types, including conversions. */
7662 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
7663 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
7664 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
7666 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
7667 && (TREE_CODE (init
) == STRING_CST
7668 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
7669 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
7671 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
7674 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
7675 inside_init
, origtype
, ic_init
,
7676 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
7679 /* Check to see if we have already given an error message. */
7680 if (inside_init
== error_mark_node
)
7682 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
7684 error_init (init_loc
, "initializer element is not constant");
7685 inside_init
= error_mark_node
;
7687 else if (require_constant
7688 && !initializer_constant_valid_p (inside_init
,
7689 TREE_TYPE (inside_init
)))
7691 error_init (init_loc
, "initializer element is not computable at "
7693 inside_init
= error_mark_node
;
7695 else if (require_constant
&& !maybe_const
)
7696 pedwarn_init (init_loc
, OPT_Wpedantic
,
7697 "initializer element is not a constant expression");
7702 /* Come here only for records and arrays. */
7704 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
7706 error_init (init_loc
, "variable-sized object may not be initialized");
7707 return error_mark_node
;
7710 error_init (init_loc
, "invalid initializer");
7711 return error_mark_node
;
7714 /* Handle initializers that use braces. */
7716 /* Type of object we are accumulating a constructor for.
7717 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7718 static tree constructor_type
;
7720 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7722 static tree constructor_fields
;
7724 /* For an ARRAY_TYPE, this is the specified index
7725 at which to store the next element we get. */
7726 static tree constructor_index
;
7728 /* For an ARRAY_TYPE, this is the maximum index. */
7729 static tree constructor_max_index
;
7731 /* For a RECORD_TYPE, this is the first field not yet written out. */
7732 static tree constructor_unfilled_fields
;
7734 /* For an ARRAY_TYPE, this is the index of the first element
7735 not yet written out. */
7736 static tree constructor_unfilled_index
;
7738 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7739 This is so we can generate gaps between fields, when appropriate. */
7740 static tree constructor_bit_index
;
7742 /* If we are saving up the elements rather than allocating them,
7743 this is the list of elements so far (in reverse order,
7744 most recent first). */
7745 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
7747 /* 1 if constructor should be incrementally stored into a constructor chain,
7748 0 if all the elements should be kept in AVL tree. */
7749 static int constructor_incremental
;
7751 /* 1 if so far this constructor's elements are all compile-time constants. */
7752 static int constructor_constant
;
7754 /* 1 if so far this constructor's elements are all valid address constants. */
7755 static int constructor_simple
;
7757 /* 1 if this constructor has an element that cannot be part of a
7758 constant expression. */
7759 static int constructor_nonconst
;
7761 /* 1 if this constructor is erroneous so far. */
7762 static int constructor_erroneous
;
7764 /* 1 if this constructor is the universal zero initializer { 0 }. */
7765 static int constructor_zeroinit
;
7767 /* Structure for managing pending initializer elements, organized as an
7772 struct init_node
*left
, *right
;
7773 struct init_node
*parent
;
7780 /* Tree of pending elements at this constructor level.
7781 These are elements encountered out of order
7782 which belong at places we haven't reached yet in actually
7784 Will never hold tree nodes across GC runs. */
7785 static struct init_node
*constructor_pending_elts
;
7787 /* The SPELLING_DEPTH of this constructor. */
7788 static int constructor_depth
;
7790 /* DECL node for which an initializer is being read.
7791 0 means we are reading a constructor expression
7792 such as (struct foo) {...}. */
7793 static tree constructor_decl
;
7795 /* Nonzero if this is an initializer for a top-level decl. */
7796 static int constructor_top_level
;
7798 /* Nonzero if there were any member designators in this initializer. */
7799 static int constructor_designated
;
7801 /* Nesting depth of designator list. */
7802 static int designator_depth
;
7804 /* Nonzero if there were diagnosed errors in this designator list. */
7805 static int designator_erroneous
;
7808 /* This stack has a level for each implicit or explicit level of
7809 structuring in the initializer, including the outermost one. It
7810 saves the values of most of the variables above. */
7812 struct constructor_range_stack
;
7814 struct constructor_stack
7816 struct constructor_stack
*next
;
7821 tree unfilled_index
;
7822 tree unfilled_fields
;
7824 vec
<constructor_elt
, va_gc
> *elements
;
7825 struct init_node
*pending_elts
;
7828 /* If value nonzero, this value should replace the entire
7829 constructor at this level. */
7830 struct c_expr replacement_value
;
7831 struct constructor_range_stack
*range_stack
;
7840 int designator_depth
;
7843 static struct constructor_stack
*constructor_stack
;
7845 /* This stack represents designators from some range designator up to
7846 the last designator in the list. */
7848 struct constructor_range_stack
7850 struct constructor_range_stack
*next
, *prev
;
7851 struct constructor_stack
*stack
;
7858 static struct constructor_range_stack
*constructor_range_stack
;
7860 /* This stack records separate initializers that are nested.
7861 Nested initializers can't happen in ANSI C, but GNU C allows them
7862 in cases like { ... (struct foo) { ... } ... }. */
7864 struct initializer_stack
7866 struct initializer_stack
*next
;
7868 struct constructor_stack
*constructor_stack
;
7869 struct constructor_range_stack
*constructor_range_stack
;
7870 vec
<constructor_elt
, va_gc
> *elements
;
7871 struct spelling
*spelling
;
7872 struct spelling
*spelling_base
;
7875 char require_constant_value
;
7876 char require_constant_elements
;
7877 rich_location
*missing_brace_richloc
;
7880 static struct initializer_stack
*initializer_stack
;
7882 /* Prepare to parse and output the initializer for variable DECL. */
7885 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
,
7886 rich_location
*richloc
)
7889 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
7891 p
->decl
= constructor_decl
;
7892 p
->require_constant_value
= require_constant_value
;
7893 p
->require_constant_elements
= require_constant_elements
;
7894 p
->constructor_stack
= constructor_stack
;
7895 p
->constructor_range_stack
= constructor_range_stack
;
7896 p
->elements
= constructor_elements
;
7897 p
->spelling
= spelling
;
7898 p
->spelling_base
= spelling_base
;
7899 p
->spelling_size
= spelling_size
;
7900 p
->top_level
= constructor_top_level
;
7901 p
->next
= initializer_stack
;
7902 p
->missing_brace_richloc
= richloc
;
7903 initializer_stack
= p
;
7905 constructor_decl
= decl
;
7906 constructor_designated
= 0;
7907 constructor_top_level
= top_level
;
7909 if (decl
!= NULL_TREE
&& decl
!= error_mark_node
)
7911 require_constant_value
= TREE_STATIC (decl
);
7912 require_constant_elements
7913 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
7914 /* For a scalar, you can always use any value to initialize,
7915 even within braces. */
7916 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)));
7917 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
7921 require_constant_value
= 0;
7922 require_constant_elements
= 0;
7923 locus
= _("(anonymous)");
7926 constructor_stack
= 0;
7927 constructor_range_stack
= 0;
7929 found_missing_braces
= 0;
7933 RESTORE_SPELLING_DEPTH (0);
7936 push_string (locus
);
7942 struct initializer_stack
*p
= initializer_stack
;
7944 /* Free the whole constructor stack of this initializer. */
7945 while (constructor_stack
)
7947 struct constructor_stack
*q
= constructor_stack
;
7948 constructor_stack
= q
->next
;
7952 gcc_assert (!constructor_range_stack
);
7954 /* Pop back to the data of the outer initializer (if any). */
7955 free (spelling_base
);
7957 constructor_decl
= p
->decl
;
7958 require_constant_value
= p
->require_constant_value
;
7959 require_constant_elements
= p
->require_constant_elements
;
7960 constructor_stack
= p
->constructor_stack
;
7961 constructor_range_stack
= p
->constructor_range_stack
;
7962 constructor_elements
= p
->elements
;
7963 spelling
= p
->spelling
;
7964 spelling_base
= p
->spelling_base
;
7965 spelling_size
= p
->spelling_size
;
7966 constructor_top_level
= p
->top_level
;
7967 initializer_stack
= p
->next
;
7971 /* Call here when we see the initializer is surrounded by braces.
7972 This is instead of a call to push_init_level;
7973 it is matched by a call to pop_init_level.
7975 TYPE is the type to initialize, for a constructor expression.
7976 For an initializer for a decl, TYPE is zero. */
7979 really_start_incremental_init (tree type
)
7981 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7983 if (type
== NULL_TREE
)
7984 type
= TREE_TYPE (constructor_decl
);
7986 if (VECTOR_TYPE_P (type
)
7987 && TYPE_VECTOR_OPAQUE (type
))
7988 error ("opaque vector types cannot be initialized");
7990 p
->type
= constructor_type
;
7991 p
->fields
= constructor_fields
;
7992 p
->index
= constructor_index
;
7993 p
->max_index
= constructor_max_index
;
7994 p
->unfilled_index
= constructor_unfilled_index
;
7995 p
->unfilled_fields
= constructor_unfilled_fields
;
7996 p
->bit_index
= constructor_bit_index
;
7997 p
->elements
= constructor_elements
;
7998 p
->constant
= constructor_constant
;
7999 p
->simple
= constructor_simple
;
8000 p
->nonconst
= constructor_nonconst
;
8001 p
->erroneous
= constructor_erroneous
;
8002 p
->pending_elts
= constructor_pending_elts
;
8003 p
->depth
= constructor_depth
;
8004 p
->replacement_value
.value
= 0;
8005 p
->replacement_value
.original_code
= ERROR_MARK
;
8006 p
->replacement_value
.original_type
= NULL
;
8010 p
->incremental
= constructor_incremental
;
8011 p
->designated
= constructor_designated
;
8012 p
->designator_depth
= designator_depth
;
8014 constructor_stack
= p
;
8016 constructor_constant
= 1;
8017 constructor_simple
= 1;
8018 constructor_nonconst
= 0;
8019 constructor_depth
= SPELLING_DEPTH ();
8020 constructor_elements
= NULL
;
8021 constructor_pending_elts
= 0;
8022 constructor_type
= type
;
8023 constructor_incremental
= 1;
8024 constructor_designated
= 0;
8025 constructor_zeroinit
= 1;
8026 designator_depth
= 0;
8027 designator_erroneous
= 0;
8029 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
8031 constructor_fields
= TYPE_FIELDS (constructor_type
);
8032 /* Skip any nameless bit fields at the beginning. */
8033 while (constructor_fields
!= NULL_TREE
8034 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
8035 constructor_fields
= DECL_CHAIN (constructor_fields
);
8037 constructor_unfilled_fields
= constructor_fields
;
8038 constructor_bit_index
= bitsize_zero_node
;
8040 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8042 if (TYPE_DOMAIN (constructor_type
))
8044 constructor_max_index
8045 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
8047 /* Detect non-empty initializations of zero-length arrays. */
8048 if (constructor_max_index
== NULL_TREE
8049 && TYPE_SIZE (constructor_type
))
8050 constructor_max_index
= integer_minus_one_node
;
8052 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8053 to initialize VLAs will cause a proper error; avoid tree
8054 checking errors as well by setting a safe value. */
8055 if (constructor_max_index
8056 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
8057 constructor_max_index
= integer_minus_one_node
;
8060 = convert (bitsizetype
,
8061 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8065 constructor_index
= bitsize_zero_node
;
8066 constructor_max_index
= NULL_TREE
;
8069 constructor_unfilled_index
= constructor_index
;
8071 else if (VECTOR_TYPE_P (constructor_type
))
8073 /* Vectors are like simple fixed-size arrays. */
8074 constructor_max_index
=
8075 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
8076 constructor_index
= bitsize_zero_node
;
8077 constructor_unfilled_index
= constructor_index
;
8081 /* Handle the case of int x = {5}; */
8082 constructor_fields
= constructor_type
;
8083 constructor_unfilled_fields
= constructor_type
;
8087 extern location_t last_init_list_comma
;
8089 /* Called when we see an open brace for a nested initializer. Finish
8090 off any pending levels with implicit braces. */
8092 finish_implicit_inits (location_t loc
, struct obstack
*braced_init_obstack
)
8094 while (constructor_stack
->implicit
)
8096 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
8097 && constructor_fields
== NULL_TREE
)
8098 process_init_element (input_location
,
8099 pop_init_level (loc
, 1, braced_init_obstack
,
8100 last_init_list_comma
),
8101 true, braced_init_obstack
);
8102 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8103 && constructor_max_index
8104 && tree_int_cst_lt (constructor_max_index
,
8106 process_init_element (input_location
,
8107 pop_init_level (loc
, 1, braced_init_obstack
,
8108 last_init_list_comma
),
8109 true, braced_init_obstack
);
8115 /* Push down into a subobject, for initialization.
8116 If this is for an explicit set of braces, IMPLICIT is 0.
8117 If it is because the next element belongs at a lower level,
8118 IMPLICIT is 1 (or 2 if the push is because of designator list). */
8121 push_init_level (location_t loc
, int implicit
,
8122 struct obstack
*braced_init_obstack
)
8124 struct constructor_stack
*p
;
8125 tree value
= NULL_TREE
;
8127 /* Unless this is an explicit brace, we need to preserve previous
8131 if (RECORD_OR_UNION_TYPE_P (constructor_type
) && constructor_fields
)
8132 value
= find_init_member (constructor_fields
, braced_init_obstack
);
8133 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8134 value
= find_init_member (constructor_index
, braced_init_obstack
);
8137 p
= XNEW (struct constructor_stack
);
8138 p
->type
= constructor_type
;
8139 p
->fields
= constructor_fields
;
8140 p
->index
= constructor_index
;
8141 p
->max_index
= constructor_max_index
;
8142 p
->unfilled_index
= constructor_unfilled_index
;
8143 p
->unfilled_fields
= constructor_unfilled_fields
;
8144 p
->bit_index
= constructor_bit_index
;
8145 p
->elements
= constructor_elements
;
8146 p
->constant
= constructor_constant
;
8147 p
->simple
= constructor_simple
;
8148 p
->nonconst
= constructor_nonconst
;
8149 p
->erroneous
= constructor_erroneous
;
8150 p
->pending_elts
= constructor_pending_elts
;
8151 p
->depth
= constructor_depth
;
8152 p
->replacement_value
.value
= NULL_TREE
;
8153 p
->replacement_value
.original_code
= ERROR_MARK
;
8154 p
->replacement_value
.original_type
= NULL
;
8155 p
->implicit
= implicit
;
8157 p
->incremental
= constructor_incremental
;
8158 p
->designated
= constructor_designated
;
8159 p
->designator_depth
= designator_depth
;
8160 p
->next
= constructor_stack
;
8162 constructor_stack
= p
;
8164 constructor_constant
= 1;
8165 constructor_simple
= 1;
8166 constructor_nonconst
= 0;
8167 constructor_depth
= SPELLING_DEPTH ();
8168 constructor_elements
= NULL
;
8169 constructor_incremental
= 1;
8170 constructor_designated
= 0;
8171 constructor_pending_elts
= 0;
8174 p
->range_stack
= constructor_range_stack
;
8175 constructor_range_stack
= 0;
8176 designator_depth
= 0;
8177 designator_erroneous
= 0;
8180 /* Don't die if an entire brace-pair level is superfluous
8181 in the containing level. */
8182 if (constructor_type
== NULL_TREE
)
8184 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
8186 /* Don't die if there are extra init elts at the end. */
8187 if (constructor_fields
== NULL_TREE
)
8188 constructor_type
= NULL_TREE
;
8191 constructor_type
= TREE_TYPE (constructor_fields
);
8192 push_member_name (constructor_fields
);
8193 constructor_depth
++;
8195 /* If upper initializer is designated, then mark this as
8196 designated too to prevent bogus warnings. */
8197 constructor_designated
= p
->designated
;
8199 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8201 constructor_type
= TREE_TYPE (constructor_type
);
8202 push_array_bounds (tree_to_uhwi (constructor_index
));
8203 constructor_depth
++;
8206 if (constructor_type
== NULL_TREE
)
8208 error_init (loc
, "extra brace group at end of initializer");
8209 constructor_fields
= NULL_TREE
;
8210 constructor_unfilled_fields
= NULL_TREE
;
8214 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
8216 constructor_constant
= TREE_CONSTANT (value
);
8217 constructor_simple
= TREE_STATIC (value
);
8218 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
8219 constructor_elements
= CONSTRUCTOR_ELTS (value
);
8220 if (!vec_safe_is_empty (constructor_elements
)
8221 && (TREE_CODE (constructor_type
) == RECORD_TYPE
8222 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
8223 set_nonincremental_init (braced_init_obstack
);
8228 found_missing_braces
= 1;
8229 if (initializer_stack
->missing_brace_richloc
)
8230 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
8234 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
8236 constructor_fields
= TYPE_FIELDS (constructor_type
);
8237 /* Skip any nameless bit fields at the beginning. */
8238 while (constructor_fields
!= NULL_TREE
8239 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
8240 constructor_fields
= DECL_CHAIN (constructor_fields
);
8242 constructor_unfilled_fields
= constructor_fields
;
8243 constructor_bit_index
= bitsize_zero_node
;
8245 else if (VECTOR_TYPE_P (constructor_type
))
8247 /* Vectors are like simple fixed-size arrays. */
8248 constructor_max_index
=
8249 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
8250 constructor_index
= bitsize_int (0);
8251 constructor_unfilled_index
= constructor_index
;
8253 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8255 if (TYPE_DOMAIN (constructor_type
))
8257 constructor_max_index
8258 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
8260 /* Detect non-empty initializations of zero-length arrays. */
8261 if (constructor_max_index
== NULL_TREE
8262 && TYPE_SIZE (constructor_type
))
8263 constructor_max_index
= integer_minus_one_node
;
8265 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8266 to initialize VLAs will cause a proper error; avoid tree
8267 checking errors as well by setting a safe value. */
8268 if (constructor_max_index
8269 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
8270 constructor_max_index
= integer_minus_one_node
;
8273 = convert (bitsizetype
,
8274 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8277 constructor_index
= bitsize_zero_node
;
8279 constructor_unfilled_index
= constructor_index
;
8280 if (value
&& TREE_CODE (value
) == STRING_CST
)
8282 /* We need to split the char/wchar array into individual
8283 characters, so that we don't have to special case it
8285 set_nonincremental_init_from_string (value
, braced_init_obstack
);
8290 if (constructor_type
!= error_mark_node
)
8291 warning_init (input_location
, 0, "braces around scalar initializer");
8292 constructor_fields
= constructor_type
;
8293 constructor_unfilled_fields
= constructor_type
;
8297 /* At the end of an implicit or explicit brace level,
8298 finish up that level of constructor. If a single expression
8299 with redundant braces initialized that level, return the
8300 c_expr structure for that expression. Otherwise, the original_code
8301 element is set to ERROR_MARK.
8302 If we were outputting the elements as they are read, return 0 as the value
8303 from inner levels (process_init_element ignores that),
8304 but return error_mark_node as the value from the outermost level
8305 (that's what we want to put in DECL_INITIAL).
8306 Otherwise, return a CONSTRUCTOR expression as the value. */
8309 pop_init_level (location_t loc
, int implicit
,
8310 struct obstack
*braced_init_obstack
,
8311 location_t insert_before
)
8313 struct constructor_stack
*p
;
8315 ret
.value
= NULL_TREE
;
8316 ret
.original_code
= ERROR_MARK
;
8317 ret
.original_type
= NULL
;
8321 /* When we come to an explicit close brace,
8322 pop any inner levels that didn't have explicit braces. */
8323 while (constructor_stack
->implicit
)
8324 process_init_element (input_location
,
8325 pop_init_level (loc
, 1, braced_init_obstack
,
8327 true, braced_init_obstack
);
8328 gcc_assert (!constructor_range_stack
);
8331 if (initializer_stack
->missing_brace_richloc
)
8332 initializer_stack
->missing_brace_richloc
->add_fixit_insert_before
8333 (insert_before
, "}");
8335 /* Now output all pending elements. */
8336 constructor_incremental
= 1;
8337 output_pending_init_elements (1, braced_init_obstack
);
8339 p
= constructor_stack
;
8341 /* Error for initializing a flexible array member, or a zero-length
8342 array member in an inappropriate context. */
8343 if (constructor_type
&& constructor_fields
8344 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8345 && TYPE_DOMAIN (constructor_type
)
8346 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
8348 /* Silently discard empty initializations. The parser will
8349 already have pedwarned for empty brackets. */
8350 if (integer_zerop (constructor_unfilled_index
))
8351 constructor_type
= NULL_TREE
;
8354 gcc_assert (!TYPE_SIZE (constructor_type
));
8356 if (constructor_depth
> 2)
8357 error_init (loc
, "initialization of flexible array member in a nested context");
8359 pedwarn_init (loc
, OPT_Wpedantic
,
8360 "initialization of a flexible array member");
8362 /* We have already issued an error message for the existence
8363 of a flexible array member not at the end of the structure.
8364 Discard the initializer so that we do not die later. */
8365 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
8366 constructor_type
= NULL_TREE
;
8370 switch (vec_safe_length (constructor_elements
))
8373 /* Initialization with { } counts as zeroinit. */
8374 constructor_zeroinit
= 1;
8377 /* This might be zeroinit as well. */
8378 if (integer_zerop ((*constructor_elements
)[0].value
))
8379 constructor_zeroinit
= 1;
8382 /* If the constructor has more than one element, it can't be { 0 }. */
8383 constructor_zeroinit
= 0;
8387 /* Warn when some structs are initialized with direct aggregation. */
8388 if (!implicit
&& found_missing_braces
&& warn_missing_braces
8389 && !constructor_zeroinit
)
8391 gcc_assert (initializer_stack
->missing_brace_richloc
);
8392 warning_at (initializer_stack
->missing_brace_richloc
,
8393 OPT_Wmissing_braces
,
8394 "missing braces around initializer");
8397 /* Warn when some struct elements are implicitly initialized to zero. */
8398 if (warn_missing_field_initializers
8400 && TREE_CODE (constructor_type
) == RECORD_TYPE
8401 && constructor_unfilled_fields
)
8403 /* Do not warn for flexible array members or zero-length arrays. */
8404 while (constructor_unfilled_fields
8405 && (!DECL_SIZE (constructor_unfilled_fields
)
8406 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
8407 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
8409 if (constructor_unfilled_fields
8410 /* Do not warn if this level of the initializer uses member
8411 designators; it is likely to be deliberate. */
8412 && !constructor_designated
8413 /* Do not warn about initializing with { 0 } or with { }. */
8414 && !constructor_zeroinit
)
8416 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
8417 "missing initializer for field %qD of %qT",
8418 constructor_unfilled_fields
,
8420 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
8421 "%qD declared here", constructor_unfilled_fields
);
8425 /* Pad out the end of the structure. */
8426 if (p
->replacement_value
.value
)
8427 /* If this closes a superfluous brace pair,
8428 just pass out the element between them. */
8429 ret
= p
->replacement_value
;
8430 else if (constructor_type
== NULL_TREE
)
8432 else if (!RECORD_OR_UNION_TYPE_P (constructor_type
)
8433 && TREE_CODE (constructor_type
) != ARRAY_TYPE
8434 && !VECTOR_TYPE_P (constructor_type
))
8436 /* A nonincremental scalar initializer--just return
8437 the element, after verifying there is just one. */
8438 if (vec_safe_is_empty (constructor_elements
))
8440 if (!constructor_erroneous
)
8441 error_init (loc
, "empty scalar initializer");
8442 ret
.value
= error_mark_node
;
8444 else if (vec_safe_length (constructor_elements
) != 1)
8446 error_init (loc
, "extra elements in scalar initializer");
8447 ret
.value
= (*constructor_elements
)[0].value
;
8450 ret
.value
= (*constructor_elements
)[0].value
;
8454 if (constructor_erroneous
)
8455 ret
.value
= error_mark_node
;
8458 ret
.value
= build_constructor (constructor_type
,
8459 constructor_elements
);
8460 if (constructor_constant
)
8461 TREE_CONSTANT (ret
.value
) = 1;
8462 if (constructor_constant
&& constructor_simple
)
8463 TREE_STATIC (ret
.value
) = 1;
8464 if (constructor_nonconst
)
8465 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
8469 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
8471 if (constructor_nonconst
)
8472 ret
.original_code
= C_MAYBE_CONST_EXPR
;
8473 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
8474 ret
.original_code
= ERROR_MARK
;
8477 constructor_type
= p
->type
;
8478 constructor_fields
= p
->fields
;
8479 constructor_index
= p
->index
;
8480 constructor_max_index
= p
->max_index
;
8481 constructor_unfilled_index
= p
->unfilled_index
;
8482 constructor_unfilled_fields
= p
->unfilled_fields
;
8483 constructor_bit_index
= p
->bit_index
;
8484 constructor_elements
= p
->elements
;
8485 constructor_constant
= p
->constant
;
8486 constructor_simple
= p
->simple
;
8487 constructor_nonconst
= p
->nonconst
;
8488 constructor_erroneous
= p
->erroneous
;
8489 constructor_incremental
= p
->incremental
;
8490 constructor_designated
= p
->designated
;
8491 designator_depth
= p
->designator_depth
;
8492 constructor_pending_elts
= p
->pending_elts
;
8493 constructor_depth
= p
->depth
;
8495 constructor_range_stack
= p
->range_stack
;
8496 RESTORE_SPELLING_DEPTH (constructor_depth
);
8498 constructor_stack
= p
->next
;
8501 if (ret
.value
== NULL_TREE
&& constructor_stack
== 0)
8502 ret
.value
= error_mark_node
;
8506 /* Common handling for both array range and field name designators.
8507 ARRAY argument is nonzero for array ranges. Returns false for success. */
8510 set_designator (location_t loc
, bool array
,
8511 struct obstack
*braced_init_obstack
)
8514 enum tree_code subcode
;
8516 /* Don't die if an entire brace-pair level is superfluous
8517 in the containing level. */
8518 if (constructor_type
== NULL_TREE
)
8521 /* If there were errors in this designator list already, bail out
8523 if (designator_erroneous
)
8526 if (!designator_depth
)
8528 gcc_assert (!constructor_range_stack
);
8530 /* Designator list starts at the level of closest explicit
8532 while (constructor_stack
->implicit
)
8533 process_init_element (input_location
,
8534 pop_init_level (loc
, 1, braced_init_obstack
,
8535 last_init_list_comma
),
8536 true, braced_init_obstack
);
8537 constructor_designated
= 1;
8541 switch (TREE_CODE (constructor_type
))
8545 subtype
= TREE_TYPE (constructor_fields
);
8546 if (subtype
!= error_mark_node
)
8547 subtype
= TYPE_MAIN_VARIANT (subtype
);
8550 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8556 subcode
= TREE_CODE (subtype
);
8557 if (array
&& subcode
!= ARRAY_TYPE
)
8559 error_init (loc
, "array index in non-array initializer");
8562 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
8564 error_init (loc
, "field name not in record or union initializer");
8568 constructor_designated
= 1;
8569 finish_implicit_inits (loc
, braced_init_obstack
);
8570 push_init_level (loc
, 2, braced_init_obstack
);
8574 /* If there are range designators in designator list, push a new designator
8575 to constructor_range_stack. RANGE_END is end of such stack range or
8576 NULL_TREE if there is no range designator at this level. */
8579 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
8581 struct constructor_range_stack
*p
;
8583 p
= (struct constructor_range_stack
*)
8584 obstack_alloc (braced_init_obstack
,
8585 sizeof (struct constructor_range_stack
));
8586 p
->prev
= constructor_range_stack
;
8588 p
->fields
= constructor_fields
;
8589 p
->range_start
= constructor_index
;
8590 p
->index
= constructor_index
;
8591 p
->stack
= constructor_stack
;
8592 p
->range_end
= range_end
;
8593 if (constructor_range_stack
)
8594 constructor_range_stack
->next
= p
;
8595 constructor_range_stack
= p
;
8598 /* Within an array initializer, specify the next index to be initialized.
8599 FIRST is that index. If LAST is nonzero, then initialize a range
8600 of indices, running from FIRST through LAST. */
8603 set_init_index (location_t loc
, tree first
, tree last
,
8604 struct obstack
*braced_init_obstack
)
8606 if (set_designator (loc
, true, braced_init_obstack
))
8609 designator_erroneous
= 1;
8611 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
8612 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
8614 error_init (loc
, "array index in initializer not of integer type");
8618 if (TREE_CODE (first
) != INTEGER_CST
)
8620 first
= c_fully_fold (first
, false, NULL
);
8621 if (TREE_CODE (first
) == INTEGER_CST
)
8622 pedwarn_init (loc
, OPT_Wpedantic
,
8623 "array index in initializer is not "
8624 "an integer constant expression");
8627 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
8629 last
= c_fully_fold (last
, false, NULL
);
8630 if (TREE_CODE (last
) == INTEGER_CST
)
8631 pedwarn_init (loc
, OPT_Wpedantic
,
8632 "array index in initializer is not "
8633 "an integer constant expression");
8636 if (TREE_CODE (first
) != INTEGER_CST
)
8637 error_init (loc
, "nonconstant array index in initializer");
8638 else if (last
!= NULL_TREE
&& TREE_CODE (last
) != INTEGER_CST
)
8639 error_init (loc
, "nonconstant array index in initializer");
8640 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8641 error_init (loc
, "array index in non-array initializer");
8642 else if (tree_int_cst_sgn (first
) == -1)
8643 error_init (loc
, "array index in initializer exceeds array bounds");
8644 else if (constructor_max_index
8645 && tree_int_cst_lt (constructor_max_index
, first
))
8646 error_init (loc
, "array index in initializer exceeds array bounds");
8649 constant_expression_warning (first
);
8651 constant_expression_warning (last
);
8652 constructor_index
= convert (bitsizetype
, first
);
8653 if (tree_int_cst_lt (constructor_index
, first
))
8655 constructor_index
= copy_node (constructor_index
);
8656 TREE_OVERFLOW (constructor_index
) = 1;
8661 if (tree_int_cst_equal (first
, last
))
8663 else if (tree_int_cst_lt (last
, first
))
8665 error_init (loc
, "empty index range in initializer");
8670 last
= convert (bitsizetype
, last
);
8671 if (constructor_max_index
!= NULL_TREE
8672 && tree_int_cst_lt (constructor_max_index
, last
))
8674 error_init (loc
, "array index range in initializer exceeds "
8682 designator_erroneous
= 0;
8683 if (constructor_range_stack
|| last
)
8684 push_range_stack (last
, braced_init_obstack
);
8688 /* Within a struct initializer, specify the next field to be initialized. */
8691 set_init_label (location_t loc
, tree fieldname
, location_t fieldname_loc
,
8692 struct obstack
*braced_init_obstack
)
8696 if (set_designator (loc
, false, braced_init_obstack
))
8699 designator_erroneous
= 1;
8701 if (!RECORD_OR_UNION_TYPE_P (constructor_type
))
8703 error_init (loc
, "field name not in record or union initializer");
8707 field
= lookup_field (constructor_type
, fieldname
);
8709 if (field
== NULL_TREE
)
8711 tree guessed_id
= lookup_field_fuzzy (constructor_type
, fieldname
);
8714 gcc_rich_location
rich_loc (fieldname_loc
);
8715 rich_loc
.add_fixit_misspelled_id (fieldname_loc
, guessed_id
);
8716 error_at (&rich_loc
,
8717 "%qT has no member named %qE; did you mean %qE?",
8718 constructor_type
, fieldname
, guessed_id
);
8721 error_at (fieldname_loc
, "%qT has no member named %qE",
8722 constructor_type
, fieldname
);
8727 constructor_fields
= TREE_VALUE (field
);
8729 designator_erroneous
= 0;
8730 if (constructor_range_stack
)
8731 push_range_stack (NULL_TREE
, braced_init_obstack
);
8732 field
= TREE_CHAIN (field
);
8735 if (set_designator (loc
, false, braced_init_obstack
))
8739 while (field
!= NULL_TREE
);
8742 /* Add a new initializer to the tree of pending initializers. PURPOSE
8743 identifies the initializer, either array index or field in a structure.
8744 VALUE is the value of that index or field. If ORIGTYPE is not
8745 NULL_TREE, it is the original type of VALUE.
8747 IMPLICIT is true if value comes from pop_init_level (1),
8748 the new initializer has been merged with the existing one
8749 and thus no warnings should be emitted about overriding an
8750 existing initializer. */
8753 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
8754 bool implicit
, struct obstack
*braced_init_obstack
)
8756 struct init_node
*p
, **q
, *r
;
8758 q
= &constructor_pending_elts
;
8761 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8766 if (tree_int_cst_lt (purpose
, p
->purpose
))
8768 else if (tree_int_cst_lt (p
->purpose
, purpose
))
8774 if (TREE_SIDE_EFFECTS (p
->value
))
8775 warning_init (loc
, OPT_Woverride_init_side_effects
,
8776 "initialized field with side-effects "
8778 else if (warn_override_init
)
8779 warning_init (loc
, OPT_Woverride_init
,
8780 "initialized field overwritten");
8783 p
->origtype
= origtype
;
8792 bitpos
= bit_position (purpose
);
8796 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8798 else if (p
->purpose
!= purpose
)
8804 if (TREE_SIDE_EFFECTS (p
->value
))
8805 warning_init (loc
, OPT_Woverride_init_side_effects
,
8806 "initialized field with side-effects "
8808 else if (warn_override_init
)
8809 warning_init (loc
, OPT_Woverride_init
,
8810 "initialized field overwritten");
8813 p
->origtype
= origtype
;
8819 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
8820 sizeof (struct init_node
));
8821 r
->purpose
= purpose
;
8823 r
->origtype
= origtype
;
8833 struct init_node
*s
;
8837 if (p
->balance
== 0)
8839 else if (p
->balance
< 0)
8846 p
->left
->parent
= p
;
8863 constructor_pending_elts
= r
;
8868 struct init_node
*t
= r
->right
;
8872 r
->right
->parent
= r
;
8877 p
->left
->parent
= p
;
8880 p
->balance
= t
->balance
< 0;
8881 r
->balance
= -(t
->balance
> 0);
8896 constructor_pending_elts
= t
;
8902 /* p->balance == +1; growth of left side balances the node. */
8907 else /* r == p->right */
8909 if (p
->balance
== 0)
8910 /* Growth propagation from right side. */
8912 else if (p
->balance
> 0)
8919 p
->right
->parent
= p
;
8936 constructor_pending_elts
= r
;
8938 else /* r->balance == -1 */
8941 struct init_node
*t
= r
->left
;
8945 r
->left
->parent
= r
;
8950 p
->right
->parent
= p
;
8953 r
->balance
= (t
->balance
< 0);
8954 p
->balance
= -(t
->balance
> 0);
8969 constructor_pending_elts
= t
;
8975 /* p->balance == -1; growth of right side balances the node. */
8986 /* Build AVL tree from a sorted chain. */
8989 set_nonincremental_init (struct obstack
* braced_init_obstack
)
8991 unsigned HOST_WIDE_INT ix
;
8994 if (TREE_CODE (constructor_type
) != RECORD_TYPE
8995 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8998 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8999 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
9000 braced_init_obstack
);
9001 constructor_elements
= NULL
;
9002 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9004 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
9005 /* Skip any nameless bit fields at the beginning. */
9006 while (constructor_unfilled_fields
!= NULL_TREE
9007 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields
))
9008 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
9011 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9013 if (TYPE_DOMAIN (constructor_type
))
9014 constructor_unfilled_index
9015 = convert (bitsizetype
,
9016 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
9018 constructor_unfilled_index
= bitsize_zero_node
;
9020 constructor_incremental
= 0;
9023 /* Build AVL tree from a string constant. */
9026 set_nonincremental_init_from_string (tree str
,
9027 struct obstack
* braced_init_obstack
)
9029 tree value
, purpose
, type
;
9030 HOST_WIDE_INT val
[2];
9031 const char *p
, *end
;
9032 int byte
, wchar_bytes
, charwidth
, bitpos
;
9034 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
9036 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
9037 charwidth
= TYPE_PRECISION (char_type_node
);
9038 gcc_assert ((size_t) wchar_bytes
* charwidth
9039 <= ARRAY_SIZE (val
) * HOST_BITS_PER_WIDE_INT
);
9040 type
= TREE_TYPE (constructor_type
);
9041 p
= TREE_STRING_POINTER (str
);
9042 end
= p
+ TREE_STRING_LENGTH (str
);
9044 for (purpose
= bitsize_zero_node
;
9046 && !(constructor_max_index
9047 && tree_int_cst_lt (constructor_max_index
, purpose
));
9048 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
9050 if (wchar_bytes
== 1)
9052 val
[0] = (unsigned char) *p
++;
9059 for (byte
= 0; byte
< wchar_bytes
; byte
++)
9061 if (BYTES_BIG_ENDIAN
)
9062 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
9064 bitpos
= byte
* charwidth
;
9065 val
[bitpos
/ HOST_BITS_PER_WIDE_INT
]
9066 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
9067 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
9071 if (!TYPE_UNSIGNED (type
))
9073 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
9074 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
9076 if (val
[0] & (HOST_WIDE_INT_1
<< (bitpos
- 1)))
9078 val
[0] |= HOST_WIDE_INT_M1U
<< bitpos
;
9082 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
9087 else if (val
[1] & (HOST_WIDE_INT_1
9088 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
9089 val
[1] |= HOST_WIDE_INT_M1U
<< (bitpos
- HOST_BITS_PER_WIDE_INT
);
9092 value
= wide_int_to_tree (type
,
9093 wide_int::from_array (val
, 2,
9094 HOST_BITS_PER_WIDE_INT
* 2));
9095 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
9096 braced_init_obstack
);
9099 constructor_incremental
= 0;
9102 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9103 not initialized yet. */
9106 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
9108 struct init_node
*p
;
9110 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9112 if (constructor_incremental
9113 && tree_int_cst_lt (field
, constructor_unfilled_index
))
9114 set_nonincremental_init (braced_init_obstack
);
9116 p
= constructor_pending_elts
;
9119 if (tree_int_cst_lt (field
, p
->purpose
))
9121 else if (tree_int_cst_lt (p
->purpose
, field
))
9127 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9129 tree bitpos
= bit_position (field
);
9131 if (constructor_incremental
9132 && (!constructor_unfilled_fields
9133 || tree_int_cst_lt (bitpos
,
9134 bit_position (constructor_unfilled_fields
))))
9135 set_nonincremental_init (braced_init_obstack
);
9137 p
= constructor_pending_elts
;
9140 if (field
== p
->purpose
)
9142 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
9148 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9150 if (!vec_safe_is_empty (constructor_elements
)
9151 && (constructor_elements
->last ().index
== field
))
9152 return constructor_elements
->last ().value
;
9157 /* "Output" the next constructor element.
9158 At top level, really output it to assembler code now.
9159 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9160 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9161 TYPE is the data type that the containing data type wants here.
9162 FIELD is the field (a FIELD_DECL) or the index that this element fills.
9163 If VALUE is a string constant, STRICT_STRING is true if it is
9164 unparenthesized or we should not warn here for it being parenthesized.
9165 For other types of VALUE, STRICT_STRING is not used.
9167 PENDING if true means output pending elements that belong
9168 right after this element. (PENDING is normally true;
9169 it is false while outputting pending elements, to avoid recursion.)
9171 IMPLICIT is true if value comes from pop_init_level (1),
9172 the new initializer has been merged with the existing one
9173 and thus no warnings should be emitted about overriding an
9174 existing initializer. */
9177 output_init_element (location_t loc
, tree value
, tree origtype
,
9178 bool strict_string
, tree type
, tree field
, bool pending
,
9179 bool implicit
, struct obstack
* braced_init_obstack
)
9181 tree semantic_type
= NULL_TREE
;
9182 bool maybe_const
= true;
9185 if (type
== error_mark_node
|| value
== error_mark_node
)
9187 constructor_erroneous
= 1;
9190 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
9191 && (TREE_CODE (value
) == STRING_CST
9192 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
9193 && !(TREE_CODE (value
) == STRING_CST
9194 && TREE_CODE (type
) == ARRAY_TYPE
9195 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
9196 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
9197 TYPE_MAIN_VARIANT (type
)))
9198 value
= array_to_pointer_conversion (input_location
, value
);
9200 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
9201 && require_constant_value
&& pending
)
9203 /* As an extension, allow initializing objects with static storage
9204 duration with compound literals (which are then treated just as
9205 the brace enclosed list they contain). */
9207 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
9209 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
9210 value
= DECL_INITIAL (decl
);
9213 npc
= null_pointer_constant_p (value
);
9214 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
9216 semantic_type
= TREE_TYPE (value
);
9217 value
= TREE_OPERAND (value
, 0);
9219 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
9221 if (value
== error_mark_node
)
9222 constructor_erroneous
= 1;
9223 else if (!TREE_CONSTANT (value
))
9224 constructor_constant
= 0;
9225 else if (!initializer_constant_valid_p (value
,
9227 AGGREGATE_TYPE_P (constructor_type
)
9228 && TYPE_REVERSE_STORAGE_ORDER
9230 || (RECORD_OR_UNION_TYPE_P (constructor_type
)
9231 && DECL_C_BIT_FIELD (field
)
9232 && TREE_CODE (value
) != INTEGER_CST
))
9233 constructor_simple
= 0;
9235 constructor_nonconst
= 1;
9237 /* Digest the initializer and issue any errors about incompatible
9238 types before issuing errors about non-constant initializers. */
9239 tree new_value
= value
;
9241 new_value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
9242 new_value
= digest_init (loc
, type
, new_value
, origtype
, npc
, strict_string
,
9243 require_constant_value
);
9244 if (new_value
== error_mark_node
)
9246 constructor_erroneous
= 1;
9249 if (require_constant_value
|| require_constant_elements
)
9250 constant_expression_warning (new_value
);
9252 /* Proceed to check the constness of the original initializer. */
9253 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
9255 if (require_constant_value
)
9257 error_init (loc
, "initializer element is not constant");
9258 value
= error_mark_node
;
9260 else if (require_constant_elements
)
9261 pedwarn (loc
, OPT_Wpedantic
,
9262 "initializer element is not computable at load time");
9264 else if (!maybe_const
9265 && (require_constant_value
|| require_constant_elements
))
9266 pedwarn_init (loc
, OPT_Wpedantic
,
9267 "initializer element is not a constant expression");
9269 /* Issue -Wc++-compat warnings about initializing a bitfield with
9272 && field
!= NULL_TREE
9273 && TREE_CODE (field
) == FIELD_DECL
9274 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
9275 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
9276 != TYPE_MAIN_VARIANT (type
))
9277 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
9279 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
9280 if (checktype
!= error_mark_node
9281 && (TYPE_MAIN_VARIANT (checktype
)
9282 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
9283 warning_init (loc
, OPT_Wc___compat
,
9284 "enum conversion in initialization is invalid in C++");
9287 /* If this field is empty and does not have side effects (and is not at
9288 the end of structure), don't do anything other than checking the
9291 && (TREE_TYPE (field
) == error_mark_node
9292 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
9293 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
9294 && !TREE_SIDE_EFFECTS (new_value
)
9295 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
9296 || DECL_CHAIN (field
)))))
9299 /* Finally, set VALUE to the initializer value digested above. */
9302 /* If this element doesn't come next in sequence,
9303 put it on constructor_pending_elts. */
9304 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
9305 && (!constructor_incremental
9306 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
9308 if (constructor_incremental
9309 && tree_int_cst_lt (field
, constructor_unfilled_index
))
9310 set_nonincremental_init (braced_init_obstack
);
9312 add_pending_init (loc
, field
, value
, origtype
, implicit
,
9313 braced_init_obstack
);
9316 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
9317 && (!constructor_incremental
9318 || field
!= constructor_unfilled_fields
))
9320 /* We do this for records but not for unions. In a union,
9321 no matter which field is specified, it can be initialized
9322 right away since it starts at the beginning of the union. */
9323 if (constructor_incremental
)
9325 if (!constructor_unfilled_fields
)
9326 set_nonincremental_init (braced_init_obstack
);
9329 tree bitpos
, unfillpos
;
9331 bitpos
= bit_position (field
);
9332 unfillpos
= bit_position (constructor_unfilled_fields
);
9334 if (tree_int_cst_lt (bitpos
, unfillpos
))
9335 set_nonincremental_init (braced_init_obstack
);
9339 add_pending_init (loc
, field
, value
, origtype
, implicit
,
9340 braced_init_obstack
);
9343 else if (TREE_CODE (constructor_type
) == UNION_TYPE
9344 && !vec_safe_is_empty (constructor_elements
))
9348 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
9349 warning_init (loc
, OPT_Woverride_init_side_effects
,
9350 "initialized field with side-effects overwritten");
9351 else if (warn_override_init
)
9352 warning_init (loc
, OPT_Woverride_init
,
9353 "initialized field overwritten");
9356 /* We can have just one union field set. */
9357 constructor_elements
= NULL
;
9360 /* Otherwise, output this element either to
9361 constructor_elements or to the assembler file. */
9363 constructor_elt celt
= {field
, value
};
9364 vec_safe_push (constructor_elements
, celt
);
9366 /* Advance the variable that indicates sequential elements output. */
9367 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9368 constructor_unfilled_index
9369 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
9371 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9373 constructor_unfilled_fields
9374 = DECL_CHAIN (constructor_unfilled_fields
);
9376 /* Skip any nameless bit fields. */
9377 while (constructor_unfilled_fields
!= NULL_TREE
9378 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields
))
9379 constructor_unfilled_fields
=
9380 DECL_CHAIN (constructor_unfilled_fields
);
9382 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9383 constructor_unfilled_fields
= NULL_TREE
;
9385 /* Now output any pending elements which have become next. */
9387 output_pending_init_elements (0, braced_init_obstack
);
9390 /* For two FIELD_DECLs in the same chain, return -1 if field1
9391 comes before field2, 1 if field1 comes after field2 and
9392 0 if field1 == field2. */
9395 init_field_decl_cmp (tree field1
, tree field2
)
9397 if (field1
== field2
)
9400 tree bitpos1
= bit_position (field1
);
9401 tree bitpos2
= bit_position (field2
);
9402 if (tree_int_cst_equal (bitpos1
, bitpos2
))
9404 /* If one of the fields has non-zero bitsize, then that
9405 field must be the last one in a sequence of zero
9406 sized fields, fields after it will have bigger
9408 if (TREE_TYPE (field1
) != error_mark_node
9409 && COMPLETE_TYPE_P (TREE_TYPE (field1
))
9410 && integer_nonzerop (TREE_TYPE (field1
)))
9412 if (TREE_TYPE (field2
) != error_mark_node
9413 && COMPLETE_TYPE_P (TREE_TYPE (field2
))
9414 && integer_nonzerop (TREE_TYPE (field2
)))
9416 /* Otherwise, fallback to DECL_CHAIN walk to find out
9417 which field comes earlier. Walk chains of both
9418 fields, so that if field1 and field2 are close to each
9419 other in either order, it is found soon even for large
9420 sequences of zero sized fields. */
9421 tree f1
= field1
, f2
= field2
;
9424 f1
= DECL_CHAIN (f1
);
9425 f2
= DECL_CHAIN (f2
);
9426 if (f1
== NULL_TREE
)
9431 if (f2
== NULL_TREE
)
9437 if (!tree_int_cst_equal (bit_position (f1
), bitpos1
))
9439 if (!tree_int_cst_equal (bit_position (f2
), bitpos1
))
9443 else if (tree_int_cst_lt (bitpos1
, bitpos2
))
9449 /* Output any pending elements which have become next.
9450 As we output elements, constructor_unfilled_{fields,index}
9451 advances, which may cause other elements to become next;
9452 if so, they too are output.
9454 If ALL is 0, we return when there are
9455 no more pending elements to output now.
9457 If ALL is 1, we output space as necessary so that
9458 we can output all the pending elements. */
9460 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
9462 struct init_node
*elt
= constructor_pending_elts
;
9467 /* Look through the whole pending tree.
9468 If we find an element that should be output now,
9469 output it. Otherwise, set NEXT to the element
9470 that comes first among those still pending. */
9475 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9477 if (tree_int_cst_equal (elt
->purpose
,
9478 constructor_unfilled_index
))
9479 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9480 true, TREE_TYPE (constructor_type
),
9481 constructor_unfilled_index
, false, false,
9482 braced_init_obstack
);
9483 else if (tree_int_cst_lt (constructor_unfilled_index
,
9486 /* Advance to the next smaller node. */
9491 /* We have reached the smallest node bigger than the
9492 current unfilled index. Fill the space first. */
9493 next
= elt
->purpose
;
9499 /* Advance to the next bigger node. */
9504 /* We have reached the biggest node in a subtree. Find
9505 the parent of it, which is the next bigger node. */
9506 while (elt
->parent
&& elt
->parent
->right
== elt
)
9509 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
9512 next
= elt
->purpose
;
9518 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9520 /* If the current record is complete we are done. */
9521 if (constructor_unfilled_fields
== NULL_TREE
)
9524 int cmp
= init_field_decl_cmp (constructor_unfilled_fields
,
9527 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9528 true, TREE_TYPE (elt
->purpose
),
9529 elt
->purpose
, false, false,
9530 braced_init_obstack
);
9533 /* Advance to the next smaller node. */
9538 /* We have reached the smallest node bigger than the
9539 current unfilled field. Fill the space first. */
9540 next
= elt
->purpose
;
9546 /* Advance to the next bigger node. */
9551 /* We have reached the biggest node in a subtree. Find
9552 the parent of it, which is the next bigger node. */
9553 while (elt
->parent
&& elt
->parent
->right
== elt
)
9557 && init_field_decl_cmp (constructor_unfilled_fields
,
9560 next
= elt
->purpose
;
9568 /* Ordinarily return, but not if we want to output all
9569 and there are elements left. */
9570 if (!(all
&& next
!= NULL_TREE
))
9573 /* If it's not incremental, just skip over the gap, so that after
9574 jumping to retry we will output the next successive element. */
9575 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9576 constructor_unfilled_fields
= next
;
9577 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9578 constructor_unfilled_index
= next
;
9580 /* ELT now points to the node in the pending tree with the next
9581 initializer to output. */
9585 /* Add one non-braced element to the current constructor level.
9586 This adjusts the current position within the constructor's type.
9587 This may also start or terminate implicit levels
9588 to handle a partly-braced initializer.
9590 Once this has found the correct level for the new element,
9591 it calls output_init_element.
9593 IMPLICIT is true if value comes from pop_init_level (1),
9594 the new initializer has been merged with the existing one
9595 and thus no warnings should be emitted about overriding an
9596 existing initializer. */
9599 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
9600 struct obstack
* braced_init_obstack
)
9602 tree orig_value
= value
.value
;
9604 = (orig_value
!= NULL_TREE
&& TREE_CODE (orig_value
) == STRING_CST
);
9605 bool strict_string
= value
.original_code
== STRING_CST
;
9606 bool was_designated
= designator_depth
!= 0;
9608 designator_depth
= 0;
9609 designator_erroneous
= 0;
9611 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
9612 constructor_zeroinit
= 0;
9614 /* Handle superfluous braces around string cst as in
9615 char x[] = {"foo"}; */
9619 && TREE_CODE (constructor_type
) == ARRAY_TYPE
9620 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
9621 && integer_zerop (constructor_unfilled_index
))
9623 if (constructor_stack
->replacement_value
.value
)
9624 error_init (loc
, "excess elements in char array initializer");
9625 constructor_stack
->replacement_value
= value
;
9629 if (constructor_stack
->replacement_value
.value
!= NULL_TREE
)
9631 error_init (loc
, "excess elements in struct initializer");
9635 /* Ignore elements of a brace group if it is entirely superfluous
9636 and has already been diagnosed. */
9637 if (constructor_type
== NULL_TREE
)
9640 if (!implicit
&& warn_designated_init
&& !was_designated
9641 && TREE_CODE (constructor_type
) == RECORD_TYPE
9642 && lookup_attribute ("designated_init",
9643 TYPE_ATTRIBUTES (constructor_type
)))
9645 OPT_Wdesignated_init
,
9646 "positional initialization of field "
9647 "in %<struct%> declared with %<designated_init%> attribute");
9649 /* If we've exhausted any levels that didn't have braces,
9651 while (constructor_stack
->implicit
)
9653 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
9654 && constructor_fields
== NULL_TREE
)
9655 process_init_element (loc
,
9656 pop_init_level (loc
, 1, braced_init_obstack
,
9657 last_init_list_comma
),
9658 true, braced_init_obstack
);
9659 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
9660 || VECTOR_TYPE_P (constructor_type
))
9661 && constructor_max_index
9662 && tree_int_cst_lt (constructor_max_index
,
9664 process_init_element (loc
,
9665 pop_init_level (loc
, 1, braced_init_obstack
,
9666 last_init_list_comma
),
9667 true, braced_init_obstack
);
9672 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9673 if (constructor_range_stack
)
9675 /* If value is a compound literal and we'll be just using its
9676 content, don't put it into a SAVE_EXPR. */
9677 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
9678 || !require_constant_value
)
9680 tree semantic_type
= NULL_TREE
;
9681 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
9683 semantic_type
= TREE_TYPE (value
.value
);
9684 value
.value
= TREE_OPERAND (value
.value
, 0);
9686 value
.value
= save_expr (value
.value
);
9688 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
9695 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9698 enum tree_code fieldcode
;
9700 if (constructor_fields
== NULL_TREE
)
9702 pedwarn_init (loc
, 0, "excess elements in struct initializer");
9706 fieldtype
= TREE_TYPE (constructor_fields
);
9707 if (fieldtype
!= error_mark_node
)
9708 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9709 fieldcode
= TREE_CODE (fieldtype
);
9711 /* Error for non-static initialization of a flexible array member. */
9712 if (fieldcode
== ARRAY_TYPE
9713 && !require_constant_value
9714 && TYPE_SIZE (fieldtype
) == NULL_TREE
9715 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
9717 error_init (loc
, "non-static initialization of a flexible "
9722 /* Error for initialization of a flexible array member with
9723 a string constant if the structure is in an array. E.g.:
9724 struct S { int x; char y[]; };
9725 struct S s[] = { { 1, "foo" } };
9728 && fieldcode
== ARRAY_TYPE
9729 && constructor_depth
> 1
9730 && TYPE_SIZE (fieldtype
) == NULL_TREE
9731 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
9733 bool in_array_p
= false;
9734 for (struct constructor_stack
*p
= constructor_stack
;
9735 p
&& p
->type
; p
= p
->next
)
9736 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
9743 error_init (loc
, "initialization of flexible array "
9744 "member in a nested context");
9749 /* Accept a string constant to initialize a subarray. */
9750 if (value
.value
!= NULL_TREE
9751 && fieldcode
== ARRAY_TYPE
9752 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
9754 value
.value
= orig_value
;
9755 /* Otherwise, if we have come to a subaggregate,
9756 and we don't have an element of its type, push into it. */
9757 else if (value
.value
!= NULL_TREE
9758 && value
.value
!= error_mark_node
9759 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
9760 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
9761 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
9763 push_init_level (loc
, 1, braced_init_obstack
);
9769 push_member_name (constructor_fields
);
9770 output_init_element (loc
, value
.value
, value
.original_type
,
9771 strict_string
, fieldtype
,
9772 constructor_fields
, true, implicit
,
9773 braced_init_obstack
);
9774 RESTORE_SPELLING_DEPTH (constructor_depth
);
9777 /* Do the bookkeeping for an element that was
9778 directly output as a constructor. */
9780 /* For a record, keep track of end position of last field. */
9781 if (DECL_SIZE (constructor_fields
))
9782 constructor_bit_index
9783 = size_binop_loc (input_location
, PLUS_EXPR
,
9784 bit_position (constructor_fields
),
9785 DECL_SIZE (constructor_fields
));
9787 /* If the current field was the first one not yet written out,
9788 it isn't now, so update. */
9789 if (constructor_unfilled_fields
== constructor_fields
)
9791 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9792 /* Skip any nameless bit fields. */
9793 while (constructor_unfilled_fields
!= 0
9794 && (DECL_UNNAMED_BIT_FIELD
9795 (constructor_unfilled_fields
)))
9796 constructor_unfilled_fields
=
9797 DECL_CHAIN (constructor_unfilled_fields
);
9801 constructor_fields
= DECL_CHAIN (constructor_fields
);
9802 /* Skip any nameless bit fields at the beginning. */
9803 while (constructor_fields
!= NULL_TREE
9804 && DECL_UNNAMED_BIT_FIELD (constructor_fields
))
9805 constructor_fields
= DECL_CHAIN (constructor_fields
);
9807 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9810 enum tree_code fieldcode
;
9812 if (constructor_fields
== NULL_TREE
)
9814 pedwarn_init (loc
, 0,
9815 "excess elements in union initializer");
9819 fieldtype
= TREE_TYPE (constructor_fields
);
9820 if (fieldtype
!= error_mark_node
)
9821 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9822 fieldcode
= TREE_CODE (fieldtype
);
9824 /* Warn that traditional C rejects initialization of unions.
9825 We skip the warning if the value is zero. This is done
9826 under the assumption that the zero initializer in user
9827 code appears conditioned on e.g. __STDC__ to avoid
9828 "missing initializer" warnings and relies on default
9829 initialization to zero in the traditional C case.
9830 We also skip the warning if the initializer is designated,
9831 again on the assumption that this must be conditional on
9832 __STDC__ anyway (and we've already complained about the
9833 member-designator already). */
9834 if (!in_system_header_at (input_location
) && !constructor_designated
9835 && !(value
.value
&& (integer_zerop (value
.value
)
9836 || real_zerop (value
.value
))))
9837 warning (OPT_Wtraditional
, "traditional C rejects initialization "
9840 /* Accept a string constant to initialize a subarray. */
9841 if (value
.value
!= NULL_TREE
9842 && fieldcode
== ARRAY_TYPE
9843 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
9845 value
.value
= orig_value
;
9846 /* Otherwise, if we have come to a subaggregate,
9847 and we don't have an element of its type, push into it. */
9848 else if (value
.value
!= NULL_TREE
9849 && value
.value
!= error_mark_node
9850 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
9851 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
9852 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
9854 push_init_level (loc
, 1, braced_init_obstack
);
9860 push_member_name (constructor_fields
);
9861 output_init_element (loc
, value
.value
, value
.original_type
,
9862 strict_string
, fieldtype
,
9863 constructor_fields
, true, implicit
,
9864 braced_init_obstack
);
9865 RESTORE_SPELLING_DEPTH (constructor_depth
);
9868 /* Do the bookkeeping for an element that was
9869 directly output as a constructor. */
9871 constructor_bit_index
= DECL_SIZE (constructor_fields
);
9872 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9875 constructor_fields
= NULL_TREE
;
9877 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9879 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9880 enum tree_code eltcode
= TREE_CODE (elttype
);
9882 /* Accept a string constant to initialize a subarray. */
9883 if (value
.value
!= NULL_TREE
9884 && eltcode
== ARRAY_TYPE
9885 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
9887 value
.value
= orig_value
;
9888 /* Otherwise, if we have come to a subaggregate,
9889 and we don't have an element of its type, push into it. */
9890 else if (value
.value
!= NULL_TREE
9891 && value
.value
!= error_mark_node
9892 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
9893 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
9894 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
9896 push_init_level (loc
, 1, braced_init_obstack
);
9900 if (constructor_max_index
!= NULL_TREE
9901 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
9902 || integer_all_onesp (constructor_max_index
)))
9904 pedwarn_init (loc
, 0,
9905 "excess elements in array initializer");
9909 /* Now output the actual element. */
9912 push_array_bounds (tree_to_uhwi (constructor_index
));
9913 output_init_element (loc
, value
.value
, value
.original_type
,
9914 strict_string
, elttype
,
9915 constructor_index
, true, implicit
,
9916 braced_init_obstack
);
9917 RESTORE_SPELLING_DEPTH (constructor_depth
);
9921 = size_binop_loc (input_location
, PLUS_EXPR
,
9922 constructor_index
, bitsize_one_node
);
9925 /* If we are doing the bookkeeping for an element that was
9926 directly output as a constructor, we must update
9927 constructor_unfilled_index. */
9928 constructor_unfilled_index
= constructor_index
;
9930 else if (VECTOR_TYPE_P (constructor_type
))
9932 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9934 /* Do a basic check of initializer size. Note that vectors
9935 always have a fixed size derived from their type. */
9936 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
9938 pedwarn_init (loc
, 0,
9939 "excess elements in vector initializer");
9943 /* Now output the actual element. */
9946 if (TREE_CODE (value
.value
) == VECTOR_CST
)
9947 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
9948 output_init_element (loc
, value
.value
, value
.original_type
,
9949 strict_string
, elttype
,
9950 constructor_index
, true, implicit
,
9951 braced_init_obstack
);
9955 = size_binop_loc (input_location
,
9956 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
9959 /* If we are doing the bookkeeping for an element that was
9960 directly output as a constructor, we must update
9961 constructor_unfilled_index. */
9962 constructor_unfilled_index
= constructor_index
;
9965 /* Handle the sole element allowed in a braced initializer
9966 for a scalar variable. */
9967 else if (constructor_type
!= error_mark_node
9968 && constructor_fields
== NULL_TREE
)
9970 pedwarn_init (loc
, 0,
9971 "excess elements in scalar initializer");
9977 output_init_element (loc
, value
.value
, value
.original_type
,
9978 strict_string
, constructor_type
,
9979 NULL_TREE
, true, implicit
,
9980 braced_init_obstack
);
9981 constructor_fields
= NULL_TREE
;
9984 /* Handle range initializers either at this level or anywhere higher
9985 in the designator stack. */
9986 if (constructor_range_stack
)
9988 struct constructor_range_stack
*p
, *range_stack
;
9991 range_stack
= constructor_range_stack
;
9992 constructor_range_stack
= 0;
9993 while (constructor_stack
!= range_stack
->stack
)
9995 gcc_assert (constructor_stack
->implicit
);
9996 process_init_element (loc
,
9997 pop_init_level (loc
, 1,
9998 braced_init_obstack
,
9999 last_init_list_comma
),
10000 true, braced_init_obstack
);
10002 for (p
= range_stack
;
10003 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
10006 gcc_assert (constructor_stack
->implicit
);
10007 process_init_element (loc
,
10008 pop_init_level (loc
, 1,
10009 braced_init_obstack
,
10010 last_init_list_comma
),
10011 true, braced_init_obstack
);
10014 p
->index
= size_binop_loc (input_location
,
10015 PLUS_EXPR
, p
->index
, bitsize_one_node
);
10016 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
10021 constructor_index
= p
->index
;
10022 constructor_fields
= p
->fields
;
10023 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
10031 finish_implicit_inits (loc
, braced_init_obstack
);
10032 push_init_level (loc
, 2, braced_init_obstack
);
10033 p
->stack
= constructor_stack
;
10034 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
10035 p
->index
= p
->range_start
;
10039 constructor_range_stack
= range_stack
;
10046 constructor_range_stack
= 0;
10049 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10050 (guaranteed to be 'volatile' or null) and ARGS (represented using
10051 an ASM_EXPR node). */
10053 build_asm_stmt (tree cv_qualifier
, tree args
)
10055 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
10056 ASM_VOLATILE_P (args
) = 1;
10057 return add_stmt (args
);
10060 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10061 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10062 SIMPLE indicates whether there was anything at all after the
10063 string in the asm expression -- asm("blah") and asm("blah" : )
10064 are subtly different. We use a ASM_EXPR node to represent this. */
10066 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
10067 tree clobbers
, tree labels
, bool simple
)
10072 const char *constraint
;
10073 const char **oconstraints
;
10074 bool allows_mem
, allows_reg
, is_inout
;
10075 int ninputs
, noutputs
;
10077 ninputs
= list_length (inputs
);
10078 noutputs
= list_length (outputs
);
10079 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
10081 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
10083 /* Remove output conversions that change the type but not the mode. */
10084 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
10086 tree output
= TREE_VALUE (tail
);
10088 output
= c_fully_fold (output
, false, NULL
, true);
10090 /* ??? Really, this should not be here. Users should be using a
10091 proper lvalue, dammit. But there's a long history of using casts
10092 in the output operands. In cases like longlong.h, this becomes a
10093 primitive form of typechecking -- if the cast can be removed, then
10094 the output operand had a type of the proper width; otherwise we'll
10095 get an error. Gross, but ... */
10096 STRIP_NOPS (output
);
10098 if (!lvalue_or_else (loc
, output
, lv_asm
))
10099 output
= error_mark_node
;
10101 if (output
!= error_mark_node
10102 && (TREE_READONLY (output
)
10103 || TYPE_READONLY (TREE_TYPE (output
))
10104 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output
))
10105 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
10106 readonly_error (loc
, output
, lv_asm
);
10108 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
10109 oconstraints
[i
] = constraint
;
10111 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
10112 &allows_mem
, &allows_reg
, &is_inout
))
10114 /* If the operand is going to end up in memory,
10115 mark it addressable. */
10116 if (!allows_reg
&& !c_mark_addressable (output
))
10117 output
= error_mark_node
;
10118 if (!(!allows_reg
&& allows_mem
)
10119 && output
!= error_mark_node
10120 && VOID_TYPE_P (TREE_TYPE (output
)))
10122 error_at (loc
, "invalid use of void expression");
10123 output
= error_mark_node
;
10127 output
= error_mark_node
;
10129 TREE_VALUE (tail
) = output
;
10132 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
10136 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
10137 input
= TREE_VALUE (tail
);
10139 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
10140 oconstraints
, &allows_mem
, &allows_reg
))
10142 /* If the operand is going to end up in memory,
10143 mark it addressable. */
10144 if (!allows_reg
&& allows_mem
)
10146 input
= c_fully_fold (input
, false, NULL
, true);
10148 /* Strip the nops as we allow this case. FIXME, this really
10149 should be rejected or made deprecated. */
10150 STRIP_NOPS (input
);
10151 if (!c_mark_addressable (input
))
10152 input
= error_mark_node
;
10156 struct c_expr expr
;
10157 memset (&expr
, 0, sizeof (expr
));
10158 expr
.value
= input
;
10159 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
10160 input
= c_fully_fold (expr
.value
, false, NULL
);
10162 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
10164 error_at (loc
, "invalid use of void expression");
10165 input
= error_mark_node
;
10170 input
= error_mark_node
;
10172 TREE_VALUE (tail
) = input
;
10175 /* ASMs with labels cannot have outputs. This should have been
10176 enforced by the parser. */
10177 gcc_assert (outputs
== NULL
|| labels
== NULL
);
10179 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
10181 /* asm statements without outputs, including simple ones, are treated
10183 ASM_INPUT_P (args
) = simple
;
10184 ASM_VOLATILE_P (args
) = (noutputs
== 0);
10189 /* Generate a goto statement to LABEL. LOC is the location of the
10193 c_finish_goto_label (location_t loc
, tree label
)
10195 tree decl
= lookup_label_for_goto (loc
, label
);
10198 TREE_USED (decl
) = 1;
10200 add_stmt (build_predict_expr (PRED_GOTO
, NOT_TAKEN
));
10201 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
10202 SET_EXPR_LOCATION (t
, loc
);
10203 return add_stmt (t
);
10207 /* Generate a computed goto statement to EXPR. LOC is the location of
10211 c_finish_goto_ptr (location_t loc
, tree expr
)
10214 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
10215 expr
= c_fully_fold (expr
, false, NULL
);
10216 expr
= convert (ptr_type_node
, expr
);
10217 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
10218 SET_EXPR_LOCATION (t
, loc
);
10219 return add_stmt (t
);
10222 /* Generate a C `return' statement. RETVAL is the expression for what
10223 to return, or a null pointer for `return;' with no value. LOC is
10224 the location of the return statement, or the location of the expression,
10225 if the statement has any. If ORIGTYPE is not NULL_TREE, it
10226 is the original type of RETVAL. */
10229 c_finish_return (location_t loc
, tree retval
, tree origtype
)
10231 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
10232 bool no_warning
= false;
10235 /* Use the expansion point to handle cases such as returning NULL
10236 in a function returning void. */
10237 source_location xloc
= expansion_point_location_if_in_system_header (loc
);
10239 if (TREE_THIS_VOLATILE (current_function_decl
))
10240 warning_at (xloc
, 0,
10241 "function declared %<noreturn%> has a %<return%> statement");
10245 tree semantic_type
= NULL_TREE
;
10246 npc
= null_pointer_constant_p (retval
);
10247 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
10249 semantic_type
= TREE_TYPE (retval
);
10250 retval
= TREE_OPERAND (retval
, 0);
10252 retval
= c_fully_fold (retval
, false, NULL
);
10254 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
10259 current_function_returns_null
= 1;
10260 if ((warn_return_type
>= 0 || flag_isoc99
)
10261 && valtype
!= NULL_TREE
&& TREE_CODE (valtype
) != VOID_TYPE
)
10265 warned_here
= pedwarn
10266 (loc
, warn_return_type
>= 0 ? OPT_Wreturn_type
: 0,
10267 "%<return%> with no value, in function returning non-void");
10269 warned_here
= warning_at
10270 (loc
, OPT_Wreturn_type
,
10271 "%<return%> with no value, in function returning non-void");
10274 inform (DECL_SOURCE_LOCATION (current_function_decl
),
10278 else if (valtype
== NULL_TREE
|| TREE_CODE (valtype
) == VOID_TYPE
)
10280 current_function_returns_null
= 1;
10282 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
10283 warned_here
= pedwarn
10284 (xloc
, warn_return_type
>= 0 ? OPT_Wreturn_type
: 0,
10285 "%<return%> with a value, in function returning void");
10287 warned_here
= pedwarn
10288 (xloc
, OPT_Wpedantic
, "ISO C forbids "
10289 "%<return%> with expression, in function returning void");
10291 inform (DECL_SOURCE_LOCATION (current_function_decl
),
10296 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
10297 retval
, origtype
, ic_return
,
10298 npc
, NULL_TREE
, NULL_TREE
, 0);
10299 tree res
= DECL_RESULT (current_function_decl
);
10303 current_function_returns_value
= 1;
10304 if (t
== error_mark_node
)
10307 save
= in_late_binary_op
;
10308 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
10309 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
10310 || (TREE_CODE (TREE_TYPE (t
)) == REAL_TYPE
10311 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
10312 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
10313 && sanitize_flags_p (SANITIZE_FLOAT_CAST
)))
10314 in_late_binary_op
= true;
10315 inner
= t
= convert (TREE_TYPE (res
), t
);
10316 in_late_binary_op
= save
;
10318 /* Strip any conversions, additions, and subtractions, and see if
10319 we are returning the address of a local variable. Warn if so. */
10322 switch (TREE_CODE (inner
))
10325 case NON_LVALUE_EXPR
:
10327 case POINTER_PLUS_EXPR
:
10328 inner
= TREE_OPERAND (inner
, 0);
10332 /* If the second operand of the MINUS_EXPR has a pointer
10333 type (or is converted from it), this may be valid, so
10334 don't give a warning. */
10336 tree op1
= TREE_OPERAND (inner
, 1);
10338 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
10339 && (CONVERT_EXPR_P (op1
)
10340 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
10341 op1
= TREE_OPERAND (op1
, 0);
10343 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
10346 inner
= TREE_OPERAND (inner
, 0);
10351 inner
= TREE_OPERAND (inner
, 0);
10353 while (REFERENCE_CLASS_P (inner
)
10354 && !INDIRECT_REF_P (inner
))
10355 inner
= TREE_OPERAND (inner
, 0);
10358 && !DECL_EXTERNAL (inner
)
10359 && !TREE_STATIC (inner
)
10360 && DECL_CONTEXT (inner
) == current_function_decl
)
10362 if (TREE_CODE (inner
) == LABEL_DECL
)
10363 warning_at (loc
, OPT_Wreturn_local_addr
,
10364 "function returns address of label");
10367 warning_at (loc
, OPT_Wreturn_local_addr
,
10368 "function returns address of local variable");
10369 tree zero
= build_zero_cst (TREE_TYPE (res
));
10370 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
10382 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
10383 SET_EXPR_LOCATION (retval
, loc
);
10385 if (warn_sequence_point
)
10386 verify_sequence_points (retval
);
10389 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
10390 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
10391 return add_stmt (ret_stmt
);
10395 /* The SWITCH_EXPR being built. */
10398 /* The original type of the testing expression, i.e. before the
10399 default conversion is applied. */
10402 /* A splay-tree mapping the low element of a case range to the high
10403 element, or NULL_TREE if there is no high element. Used to
10404 determine whether or not a new case label duplicates an old case
10405 label. We need a tree, rather than simply a hash table, because
10406 of the GNU case range extension. */
10409 /* The bindings at the point of the switch. This is used for
10410 warnings crossing decls when branching to a case label. */
10411 struct c_spot_bindings
*bindings
;
10413 /* The next node on the stack. */
10414 struct c_switch
*next
;
10416 /* Remember whether the controlling expression had boolean type
10417 before integer promotions for the sake of -Wswitch-bool. */
10420 /* Remember whether there was a case value that is outside the
10421 range of the ORIG_TYPE. */
10422 bool outside_range_p
;
10425 /* A stack of the currently active switch statements. The innermost
10426 switch statement is on the top of the stack. There is no need to
10427 mark the stack for garbage collection because it is only active
10428 during the processing of the body of a function, and we never
10429 collect at that point. */
10431 struct c_switch
*c_switch_stack
;
10433 /* Start a C switch statement, testing expression EXP. Return the new
10434 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
10435 SWITCH_COND_LOC is the location of the switch's condition.
10436 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
10439 c_start_case (location_t switch_loc
,
10440 location_t switch_cond_loc
,
10441 tree exp
, bool explicit_cast_p
)
10443 tree orig_type
= error_mark_node
;
10444 bool bool_cond_p
= false;
10445 struct c_switch
*cs
;
10447 if (exp
!= error_mark_node
)
10449 orig_type
= TREE_TYPE (exp
);
10451 if (!INTEGRAL_TYPE_P (orig_type
))
10453 if (orig_type
!= error_mark_node
)
10455 error_at (switch_cond_loc
, "switch quantity not an integer");
10456 orig_type
= error_mark_node
;
10458 exp
= integer_zero_node
;
10462 tree type
= TYPE_MAIN_VARIANT (orig_type
);
10465 /* Warn if the condition has boolean value. */
10466 while (TREE_CODE (e
) == COMPOUND_EXPR
)
10467 e
= TREE_OPERAND (e
, 1);
10469 if ((TREE_CODE (type
) == BOOLEAN_TYPE
10470 || truth_value_p (TREE_CODE (e
)))
10471 /* Explicit cast to int suppresses this warning. */
10472 && !(TREE_CODE (type
) == INTEGER_TYPE
10473 && explicit_cast_p
))
10474 bool_cond_p
= true;
10476 if (!in_system_header_at (input_location
)
10477 && (type
== long_integer_type_node
10478 || type
== long_unsigned_type_node
))
10479 warning_at (switch_cond_loc
,
10480 OPT_Wtraditional
, "%<long%> switch expression not "
10481 "converted to %<int%> in ISO C");
10483 exp
= c_fully_fold (exp
, false, NULL
);
10484 exp
= default_conversion (exp
);
10486 if (warn_sequence_point
)
10487 verify_sequence_points (exp
);
10491 /* Add this new SWITCH_EXPR to the stack. */
10492 cs
= XNEW (struct c_switch
);
10493 cs
->switch_expr
= build2 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
);
10494 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
10495 cs
->orig_type
= orig_type
;
10496 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
10497 cs
->bindings
= c_get_switch_bindings ();
10498 cs
->bool_cond_p
= bool_cond_p
;
10499 cs
->outside_range_p
= false;
10500 cs
->next
= c_switch_stack
;
10501 c_switch_stack
= cs
;
10503 return add_stmt (cs
->switch_expr
);
10506 /* Process a case label at location LOC. */
10509 do_case (location_t loc
, tree low_value
, tree high_value
)
10511 tree label
= NULL_TREE
;
10513 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
10515 low_value
= c_fully_fold (low_value
, false, NULL
);
10516 if (TREE_CODE (low_value
) == INTEGER_CST
)
10517 pedwarn (loc
, OPT_Wpedantic
,
10518 "case label is not an integer constant expression");
10521 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
10523 high_value
= c_fully_fold (high_value
, false, NULL
);
10524 if (TREE_CODE (high_value
) == INTEGER_CST
)
10525 pedwarn (input_location
, OPT_Wpedantic
,
10526 "case label is not an integer constant expression");
10529 if (c_switch_stack
== NULL
)
10532 error_at (loc
, "case label not within a switch statement");
10534 error_at (loc
, "%<default%> label not within a switch statement");
10538 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
10539 EXPR_LOCATION (c_switch_stack
->switch_expr
),
10543 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
10544 SWITCH_COND (c_switch_stack
->switch_expr
),
10545 c_switch_stack
->orig_type
,
10546 low_value
, high_value
,
10547 &c_switch_stack
->outside_range_p
);
10548 if (label
== error_mark_node
)
10553 /* Finish the switch statement. TYPE is the original type of the
10554 controlling expression of the switch, or NULL_TREE. */
10557 c_finish_case (tree body
, tree type
)
10559 struct c_switch
*cs
= c_switch_stack
;
10560 location_t switch_location
;
10562 SWITCH_BODY (cs
->switch_expr
) = body
;
10564 /* Emit warnings as needed. */
10565 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
10566 c_do_switch_warnings (cs
->cases
, switch_location
,
10567 type
? type
: TREE_TYPE (cs
->switch_expr
),
10568 SWITCH_COND (cs
->switch_expr
),
10569 cs
->bool_cond_p
, cs
->outside_range_p
);
10570 if (c_switch_covers_all_cases_p (cs
->cases
, TREE_TYPE (cs
->switch_expr
)))
10571 SWITCH_ALL_CASES_P (cs
->switch_expr
) = 1;
10573 /* Pop the stack. */
10574 c_switch_stack
= cs
->next
;
10575 splay_tree_delete (cs
->cases
);
10576 c_release_switch_bindings (cs
->bindings
);
10580 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10581 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10585 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
10590 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
10591 SET_EXPR_LOCATION (stmt
, if_locus
);
10595 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10596 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10597 is false for DO loops. INCR is the FOR increment expression. BODY is
10598 the statement controlled by the loop. BLAB is the break label. CLAB is
10599 the continue label. Everything is allowed to be NULL. */
10602 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
10603 tree blab
, tree clab
, bool cond_is_first
)
10605 tree entry
= NULL
, exit
= NULL
, t
;
10607 /* If the condition is zero don't generate a loop construct. */
10608 if (cond
&& integer_zerop (cond
))
10612 t
= build_and_jump (&blab
);
10613 SET_EXPR_LOCATION (t
, start_locus
);
10619 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10621 /* If we have an exit condition, then we build an IF with gotos either
10622 out of the loop, or to the top of it. If there's no exit condition,
10623 then we just build a jump back to the top. */
10624 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
10626 if (cond
&& !integer_nonzerop (cond
))
10628 /* Canonicalize the loop condition to the end. This means
10629 generating a branch to the loop condition. Reuse the
10630 continue label, if possible. */
10635 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10636 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
10639 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
10640 SET_EXPR_LOCATION (t
, start_locus
);
10644 t
= build_and_jump (&blab
);
10646 exit
= fold_build3_loc (start_locus
,
10647 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10649 exit
= fold_build3_loc (input_location
,
10650 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10654 /* For the backward-goto's location of an unconditional loop
10655 use the beginning of the body, or, if there is none, the
10656 top of the loop. */
10657 location_t loc
= EXPR_LOCATION (expr_first (body
));
10658 if (loc
== UNKNOWN_LOCATION
)
10660 SET_EXPR_LOCATION (exit
, loc
);
10669 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
10677 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
10681 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
10684 tree label
= *label_p
;
10686 /* In switch statements break is sometimes stylistically used after
10687 a return statement. This can lead to spurious warnings about
10688 control reaching the end of a non-void function when it is
10689 inlined. Note that we are calling block_may_fallthru with
10690 language specific tree nodes; this works because
10691 block_may_fallthru returns true when given something it does not
10693 skip
= !block_may_fallthru (cur_stmt_list
);
10698 *label_p
= label
= create_artificial_label (loc
);
10700 else if (TREE_CODE (label
) == LABEL_DECL
)
10702 else switch (TREE_INT_CST_LOW (label
))
10706 error_at (loc
, "break statement not within loop or switch");
10708 error_at (loc
, "continue statement not within a loop");
10712 gcc_assert (is_break
);
10713 error_at (loc
, "break statement used with OpenMP for loop");
10718 error ("break statement within %<#pragma simd%> loop body");
10720 error ("continue statement within %<#pragma simd%> loop body");
10724 gcc_unreachable ();
10731 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
10733 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
10736 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10739 emit_side_effect_warnings (location_t loc
, tree expr
)
10741 if (expr
== error_mark_node
)
10743 else if (!TREE_SIDE_EFFECTS (expr
))
10745 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
10746 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
10748 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
10751 location_t cloc
= loc
;
10752 while (TREE_CODE (r
) == COMPOUND_EXPR
)
10754 if (EXPR_HAS_LOCATION (r
))
10755 cloc
= EXPR_LOCATION (r
);
10756 r
= TREE_OPERAND (r
, 1);
10758 if (!TREE_SIDE_EFFECTS (r
)
10759 && !VOID_TYPE_P (TREE_TYPE (r
))
10760 && !CONVERT_EXPR_P (r
)
10761 && !TREE_NO_WARNING (r
)
10762 && !TREE_NO_WARNING (expr
))
10763 warning_at (cloc
, OPT_Wunused_value
,
10764 "right-hand operand of comma expression has no effect");
10767 warn_if_unused_value (expr
, loc
);
10770 /* Process an expression as if it were a complete statement. Emit
10771 diagnostics, but do not call ADD_STMT. LOC is the location of the
10775 c_process_expr_stmt (location_t loc
, tree expr
)
10782 expr
= c_fully_fold (expr
, false, NULL
);
10784 if (warn_sequence_point
)
10785 verify_sequence_points (expr
);
10787 if (TREE_TYPE (expr
) != error_mark_node
10788 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
10789 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
10790 error_at (loc
, "expression statement has incomplete type");
10792 /* If we're not processing a statement expression, warn about unused values.
10793 Warnings for statement expressions will be emitted later, once we figure
10794 out which is the result. */
10795 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10796 && warn_unused_value
)
10797 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr
, loc
), expr
);
10800 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
10801 exprv
= TREE_OPERAND (exprv
, 1);
10802 while (CONVERT_EXPR_P (exprv
))
10803 exprv
= TREE_OPERAND (exprv
, 0);
10805 || handled_component_p (exprv
)
10806 || TREE_CODE (exprv
) == ADDR_EXPR
)
10807 mark_exp_read (exprv
);
10809 /* If the expression is not of a type to which we cannot assign a line
10810 number, wrap the thing in a no-op NOP_EXPR. */
10811 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
10813 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
10814 SET_EXPR_LOCATION (expr
, loc
);
10820 /* Emit an expression as a statement. LOC is the location of the
10824 c_finish_expr_stmt (location_t loc
, tree expr
)
10827 return add_stmt (c_process_expr_stmt (loc
, expr
));
10832 /* Do the opposite and emit a statement as an expression. To begin,
10833 create a new binding level and return it. */
10836 c_begin_stmt_expr (void)
10840 /* We must force a BLOCK for this level so that, if it is not expanded
10841 later, there is a way to turn off the entire subtree of blocks that
10842 are contained in it. */
10843 keep_next_level ();
10844 ret
= c_begin_compound_stmt (true);
10846 c_bindings_start_stmt_expr (c_switch_stack
== NULL
10848 : c_switch_stack
->bindings
);
10850 /* Mark the current statement list as belonging to a statement list. */
10851 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
10856 /* LOC is the location of the compound statement to which this body
10860 c_finish_stmt_expr (location_t loc
, tree body
)
10862 tree last
, type
, tmp
, val
;
10865 body
= c_end_compound_stmt (loc
, body
, true);
10867 c_bindings_end_stmt_expr (c_switch_stack
== NULL
10869 : c_switch_stack
->bindings
);
10871 /* Locate the last statement in BODY. See c_end_compound_stmt
10872 about always returning a BIND_EXPR. */
10873 last_p
= &BIND_EXPR_BODY (body
);
10874 last
= BIND_EXPR_BODY (body
);
10876 continue_searching
:
10877 if (TREE_CODE (last
) == STATEMENT_LIST
)
10879 tree_stmt_iterator l
= tsi_last (last
);
10881 while (!tsi_end_p (l
) && TREE_CODE (tsi_stmt (l
)) == DEBUG_BEGIN_STMT
)
10884 /* This can happen with degenerate cases like ({ }). No value. */
10888 /* If we're supposed to generate side effects warnings, process
10889 all of the statements except the last. */
10890 if (warn_unused_value
)
10892 for (tree_stmt_iterator i
= tsi_start (last
);
10893 tsi_stmt (i
) != tsi_stmt (l
); tsi_next (&i
))
10896 tree t
= tsi_stmt (i
);
10898 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
10899 emit_side_effect_warnings (tloc
, t
);
10902 last_p
= tsi_stmt_ptr (l
);
10906 /* If the end of the list is exception related, then the list was split
10907 by a call to push_cleanup. Continue searching. */
10908 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
10909 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
10911 last_p
= &TREE_OPERAND (last
, 0);
10913 goto continue_searching
;
10916 if (last
== error_mark_node
)
10919 /* In the case that the BIND_EXPR is not necessary, return the
10920 expression out from inside it. */
10921 if ((last
== BIND_EXPR_BODY (body
)
10922 /* Skip nested debug stmts. */
10923 || last
== expr_first (BIND_EXPR_BODY (body
)))
10924 && BIND_EXPR_VARS (body
) == NULL
)
10926 /* Even if this looks constant, do not allow it in a constant
10928 last
= c_wrap_maybe_const (last
, true);
10929 /* Do not warn if the return value of a statement expression is
10931 TREE_NO_WARNING (last
) = 1;
10935 /* Extract the type of said expression. */
10936 type
= TREE_TYPE (last
);
10938 /* If we're not returning a value at all, then the BIND_EXPR that
10939 we already have is a fine expression to return. */
10940 if (!type
|| VOID_TYPE_P (type
))
10943 /* Now that we've located the expression containing the value, it seems
10944 silly to make voidify_wrapper_expr repeat the process. Create a
10945 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10946 tmp
= create_tmp_var_raw (type
);
10948 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10949 tree_expr_nonnegative_p giving up immediately. */
10951 if (TREE_CODE (val
) == NOP_EXPR
10952 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
10953 val
= TREE_OPERAND (val
, 0);
10955 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
10956 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
10959 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
10960 SET_EXPR_LOCATION (t
, loc
);
10965 /* Begin and end compound statements. This is as simple as pushing
10966 and popping new statement lists from the tree. */
10969 c_begin_compound_stmt (bool do_scope
)
10971 tree stmt
= push_stmt_list ();
10977 /* End a compound statement. STMT is the statement. LOC is the
10978 location of the compound statement-- this is usually the location
10979 of the opening brace. */
10982 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
10988 if (c_dialect_objc ())
10989 objc_clear_super_receiver ();
10990 block
= pop_scope ();
10993 stmt
= pop_stmt_list (stmt
);
10994 stmt
= c_build_bind_expr (loc
, block
, stmt
);
10996 /* If this compound statement is nested immediately inside a statement
10997 expression, then force a BIND_EXPR to be created. Otherwise we'll
10998 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10999 STATEMENT_LISTs merge, and thus we can lose track of what statement
11000 was really last. */
11001 if (building_stmt_list_p ()
11002 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
11003 && TREE_CODE (stmt
) != BIND_EXPR
)
11005 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
11006 TREE_SIDE_EFFECTS (stmt
) = 1;
11007 SET_EXPR_LOCATION (stmt
, loc
);
11013 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
11014 when the current scope is exited. EH_ONLY is true when this is not
11015 meant to apply to normal control flow transfer. */
11018 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
11020 enum tree_code code
;
11024 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
11025 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
11027 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
11028 list
= push_stmt_list ();
11029 TREE_OPERAND (stmt
, 0) = list
;
11030 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
11033 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11034 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11037 build_vec_cmp (tree_code code
, tree type
,
11038 tree arg0
, tree arg1
)
11040 tree zero_vec
= build_zero_cst (type
);
11041 tree minus_one_vec
= build_minus_one_cst (type
);
11042 tree cmp_type
= build_same_sized_truth_vector_type (type
);
11043 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
11044 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
11047 /* Subclass of range_label for labelling the type of EXPR when reporting
11048 a type mismatch between EXPR and OTHER_EXPR.
11049 Either or both of EXPR and OTHER_EXPR could be NULL. */
11051 class maybe_range_label_for_tree_type_mismatch
: public range_label
11054 maybe_range_label_for_tree_type_mismatch (tree expr
, tree other_expr
)
11055 : m_expr (expr
), m_other_expr (other_expr
)
11059 label_text
get_text (unsigned range_idx
) const FINAL OVERRIDE
11061 if (m_expr
== NULL_TREE
11062 || !EXPR_P (m_expr
))
11063 return label_text (NULL
, false);
11064 tree expr_type
= TREE_TYPE (m_expr
);
11066 tree other_type
= NULL_TREE
;
11067 if (m_other_expr
&& EXPR_P (m_other_expr
))
11068 other_type
= TREE_TYPE (m_other_expr
);
11070 range_label_for_type_mismatch
inner (expr_type
, other_type
);
11071 return inner
.get_text (range_idx
);
11079 /* Build a binary-operation expression without default conversions.
11080 CODE is the kind of expression to build.
11081 LOCATION is the operator's location.
11082 This function differs from `build' in several ways:
11083 the data type of the result is computed and recorded in it,
11084 warnings are generated if arg data types are invalid,
11085 special handling for addition and subtraction of pointers is known,
11086 and some optimization is done (operations on narrow ints
11087 are done in the narrower type when that gives the same result).
11088 Constant folding is also done before the result is returned.
11090 Note that the operands will never have enumeral types, or function
11091 or array types, because either they will have the default conversions
11092 performed or they have both just been converted to some other type in which
11093 the arithmetic is to be done. */
11096 build_binary_op (location_t location
, enum tree_code code
,
11097 tree orig_op0
, tree orig_op1
, bool convert_p
)
11099 tree type0
, type1
, orig_type0
, orig_type1
;
11101 enum tree_code code0
, code1
;
11103 tree ret
= error_mark_node
;
11104 const char *invalid_op_diag
;
11105 bool op0_int_operands
, op1_int_operands
;
11106 bool int_const
, int_const_or_overflow
, int_operands
;
11108 /* Expression code to give to the expression when it is built.
11109 Normally this is CODE, which is what the caller asked for,
11110 but in some special cases we change it. */
11111 enum tree_code resultcode
= code
;
11113 /* Data type in which the computation is to be performed.
11114 In the simplest cases this is the common type of the arguments. */
11115 tree result_type
= NULL
;
11117 /* When the computation is in excess precision, the type of the
11118 final EXCESS_PRECISION_EXPR. */
11119 tree semantic_result_type
= NULL
;
11121 /* Nonzero means operands have already been type-converted
11122 in whatever way is necessary.
11123 Zero means they need to be converted to RESULT_TYPE. */
11126 /* Nonzero means create the expression with this type, rather than
11128 tree build_type
= NULL_TREE
;
11130 /* Nonzero means after finally constructing the expression
11131 convert it to this type. */
11132 tree final_type
= NULL_TREE
;
11134 /* Nonzero if this is an operation like MIN or MAX which can
11135 safely be computed in short if both args are promoted shorts.
11136 Also implies COMMON.
11137 -1 indicates a bitwise operation; this makes a difference
11138 in the exact conditions for when it is safe to do the operation
11139 in a narrower mode. */
11142 /* Nonzero if this is a comparison operation;
11143 if both args are promoted shorts, compare the original shorts.
11144 Also implies COMMON. */
11145 int short_compare
= 0;
11147 /* Nonzero if this is a right-shift operation, which can be computed on the
11148 original short and then promoted if the operand is a promoted short. */
11149 int short_shift
= 0;
11151 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11154 /* True means types are compatible as far as ObjC is concerned. */
11157 /* True means this is an arithmetic operation that may need excess
11159 bool may_need_excess_precision
;
11161 /* True means this is a boolean operation that converts both its
11162 operands to truth-values. */
11163 bool boolean_op
= false;
11165 /* Remember whether we're doing / or %. */
11166 bool doing_div_or_mod
= false;
11168 /* Remember whether we're doing << or >>. */
11169 bool doing_shift
= false;
11171 /* Tree holding instrumentation expression. */
11172 tree instrument_expr
= NULL
;
11174 if (location
== UNKNOWN_LOCATION
)
11175 location
= input_location
;
11180 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
11181 if (op0_int_operands
)
11182 op0
= remove_c_maybe_const_expr (op0
);
11183 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
11184 if (op1_int_operands
)
11185 op1
= remove_c_maybe_const_expr (op1
);
11186 int_operands
= (op0_int_operands
&& op1_int_operands
);
11189 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
11190 && TREE_CODE (orig_op1
) == INTEGER_CST
);
11191 int_const
= (int_const_or_overflow
11192 && !TREE_OVERFLOW (orig_op0
)
11193 && !TREE_OVERFLOW (orig_op1
));
11196 int_const
= int_const_or_overflow
= false;
11198 /* Do not apply default conversion in mixed vector/scalar expression. */
11200 && VECTOR_TYPE_P (TREE_TYPE (op0
)) == VECTOR_TYPE_P (TREE_TYPE (op1
)))
11202 op0
= default_conversion (op0
);
11203 op1
= default_conversion (op1
);
11206 orig_type0
= type0
= TREE_TYPE (op0
);
11208 orig_type1
= type1
= TREE_TYPE (op1
);
11210 /* The expression codes of the data types of the arguments tell us
11211 whether the arguments are integers, floating, pointers, etc. */
11212 code0
= TREE_CODE (type0
);
11213 code1
= TREE_CODE (type1
);
11215 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11216 STRIP_TYPE_NOPS (op0
);
11217 STRIP_TYPE_NOPS (op1
);
11219 /* If an error was already reported for one of the arguments,
11220 avoid reporting another error. */
11222 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
11223 return error_mark_node
;
11225 if (code0
== POINTER_TYPE
11226 && reject_gcc_builtin (op0
, EXPR_LOCATION (orig_op0
)))
11227 return error_mark_node
;
11229 if (code1
== POINTER_TYPE
11230 && reject_gcc_builtin (op1
, EXPR_LOCATION (orig_op1
)))
11231 return error_mark_node
;
11233 if ((invalid_op_diag
11234 = targetm
.invalid_binary_op (code
, type0
, type1
)))
11236 error_at (location
, invalid_op_diag
);
11237 return error_mark_node
;
11245 case TRUNC_DIV_EXPR
:
11246 case CEIL_DIV_EXPR
:
11247 case FLOOR_DIV_EXPR
:
11248 case ROUND_DIV_EXPR
:
11249 case EXACT_DIV_EXPR
:
11250 may_need_excess_precision
= true;
11253 may_need_excess_precision
= false;
11256 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
11258 op0
= TREE_OPERAND (op0
, 0);
11259 type0
= TREE_TYPE (op0
);
11261 else if (may_need_excess_precision
11262 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
11265 op0
= convert (eptype
, op0
);
11267 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
11269 op1
= TREE_OPERAND (op1
, 0);
11270 type1
= TREE_TYPE (op1
);
11272 else if (may_need_excess_precision
11273 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
11276 op1
= convert (eptype
, op1
);
11279 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
11281 /* In case when one of the operands of the binary operation is
11282 a vector and another is a scalar -- convert scalar to vector. */
11283 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
11285 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
11288 switch (convert_flag
)
11291 return error_mark_node
;
11294 bool maybe_const
= true;
11296 sc
= c_fully_fold (op0
, false, &maybe_const
);
11297 sc
= save_expr (sc
);
11298 sc
= convert (TREE_TYPE (type1
), sc
);
11299 op0
= build_vector_from_val (type1
, sc
);
11301 op0
= c_wrap_maybe_const (op0
, true);
11302 orig_type0
= type0
= TREE_TYPE (op0
);
11303 code0
= TREE_CODE (type0
);
11307 case stv_secondarg
:
11309 bool maybe_const
= true;
11311 sc
= c_fully_fold (op1
, false, &maybe_const
);
11312 sc
= save_expr (sc
);
11313 sc
= convert (TREE_TYPE (type0
), sc
);
11314 op1
= build_vector_from_val (type0
, sc
);
11316 op1
= c_wrap_maybe_const (op1
, true);
11317 orig_type1
= type1
= TREE_TYPE (op1
);
11318 code1
= TREE_CODE (type1
);
11330 /* Handle the pointer + int case. */
11331 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11333 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
11334 goto return_build_binary_op
;
11336 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
11338 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
11339 goto return_build_binary_op
;
11346 /* Subtraction of two similar pointers.
11347 We must subtract them as integers, then divide by object size. */
11348 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
11349 && comp_target_types (location
, type0
, type1
))
11351 ret
= pointer_diff (location
, op0
, op1
, &instrument_expr
);
11352 goto return_build_binary_op
;
11354 /* Handle pointer minus int. Just like pointer plus int. */
11355 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11357 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
11358 goto return_build_binary_op
;
11368 case TRUNC_DIV_EXPR
:
11369 case CEIL_DIV_EXPR
:
11370 case FLOOR_DIV_EXPR
:
11371 case ROUND_DIV_EXPR
:
11372 case EXACT_DIV_EXPR
:
11373 doing_div_or_mod
= true;
11374 warn_for_div_by_zero (location
, op1
);
11376 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11377 || code0
== FIXED_POINT_TYPE
11378 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
11379 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11380 || code1
== FIXED_POINT_TYPE
11381 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
11383 enum tree_code tcode0
= code0
, tcode1
= code1
;
11385 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
11386 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
11387 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
11388 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
11390 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
11391 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
11392 resultcode
= RDIV_EXPR
;
11394 /* Although it would be tempting to shorten always here, that
11395 loses on some targets, since the modulo instruction is
11396 undefined if the quotient can't be represented in the
11397 computation mode. We shorten only if unsigned or if
11398 dividing by something we know != -1. */
11399 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
11400 || (TREE_CODE (op1
) == INTEGER_CST
11401 && !integer_all_onesp (op1
)));
11409 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
11411 /* Allow vector types which are not floating point types. */
11412 else if (code0
== VECTOR_TYPE
11413 && code1
== VECTOR_TYPE
11414 && !VECTOR_FLOAT_TYPE_P (type0
)
11415 && !VECTOR_FLOAT_TYPE_P (type1
))
11419 case TRUNC_MOD_EXPR
:
11420 case FLOOR_MOD_EXPR
:
11421 doing_div_or_mod
= true;
11422 warn_for_div_by_zero (location
, op1
);
11424 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11425 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11426 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
11428 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
11430 /* Although it would be tempting to shorten always here, that loses
11431 on some targets, since the modulo instruction is undefined if the
11432 quotient can't be represented in the computation mode. We shorten
11433 only if unsigned or if dividing by something we know != -1. */
11434 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
11435 || (TREE_CODE (op1
) == INTEGER_CST
11436 && !integer_all_onesp (op1
)));
11441 case TRUTH_ANDIF_EXPR
:
11442 case TRUTH_ORIF_EXPR
:
11443 case TRUTH_AND_EXPR
:
11444 case TRUTH_OR_EXPR
:
11445 case TRUTH_XOR_EXPR
:
11446 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
11447 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11448 || code0
== FIXED_POINT_TYPE
)
11449 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
11450 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11451 || code1
== FIXED_POINT_TYPE
))
11453 /* Result of these operations is always an int,
11454 but that does not mean the operands should be
11455 converted to ints! */
11456 result_type
= integer_type_node
;
11457 if (op0_int_operands
)
11459 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
11460 op0
= remove_c_maybe_const_expr (op0
);
11463 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
11464 if (op1_int_operands
)
11466 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
11467 op1
= remove_c_maybe_const_expr (op1
);
11470 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
11474 if (code
== TRUTH_ANDIF_EXPR
)
11476 int_const_or_overflow
= (int_operands
11477 && TREE_CODE (orig_op0
) == INTEGER_CST
11478 && (op0
== truthvalue_false_node
11479 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11480 int_const
= (int_const_or_overflow
11481 && !TREE_OVERFLOW (orig_op0
)
11482 && (op0
== truthvalue_false_node
11483 || !TREE_OVERFLOW (orig_op1
)));
11485 else if (code
== TRUTH_ORIF_EXPR
)
11487 int_const_or_overflow
= (int_operands
11488 && TREE_CODE (orig_op0
) == INTEGER_CST
11489 && (op0
== truthvalue_true_node
11490 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11491 int_const
= (int_const_or_overflow
11492 && !TREE_OVERFLOW (orig_op0
)
11493 && (op0
== truthvalue_true_node
11494 || !TREE_OVERFLOW (orig_op1
)));
11498 /* Shift operations: result has same type as first operand;
11499 always convert second operand to int.
11500 Also set SHORT_SHIFT if shifting rightward. */
11503 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11504 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11505 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11506 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
11507 TYPE_VECTOR_SUBPARTS (type1
)))
11509 result_type
= type0
;
11512 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11513 || (code0
== VECTOR_TYPE
11514 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
))
11515 && code1
== INTEGER_TYPE
)
11517 doing_shift
= true;
11518 if (TREE_CODE (op1
) == INTEGER_CST
)
11520 if (tree_int_cst_sgn (op1
) < 0)
11523 if (c_inhibit_evaluation_warnings
== 0)
11524 warning_at (location
, OPT_Wshift_count_negative
,
11525 "right shift count is negative");
11527 else if (code0
== VECTOR_TYPE
)
11529 if (compare_tree_int (op1
,
11530 TYPE_PRECISION (TREE_TYPE (type0
)))
11534 if (c_inhibit_evaluation_warnings
== 0)
11535 warning_at (location
, OPT_Wshift_count_overflow
,
11536 "right shift count >= width of vector element");
11541 if (!integer_zerop (op1
))
11544 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11547 if (c_inhibit_evaluation_warnings
== 0)
11548 warning_at (location
, OPT_Wshift_count_overflow
,
11549 "right shift count >= width of type");
11554 /* Use the type of the value to be shifted. */
11555 result_type
= type0
;
11556 /* Avoid converting op1 to result_type later. */
11562 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11563 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11564 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11565 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
11566 TYPE_VECTOR_SUBPARTS (type1
)))
11568 result_type
= type0
;
11571 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11572 || (code0
== VECTOR_TYPE
11573 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
))
11574 && code1
== INTEGER_TYPE
)
11576 doing_shift
= true;
11577 if (TREE_CODE (op0
) == INTEGER_CST
11578 && tree_int_cst_sgn (op0
) < 0)
11580 /* Don't reject a left shift of a negative value in a context
11581 where a constant expression is needed in C90. */
11584 if (c_inhibit_evaluation_warnings
== 0)
11585 warning_at (location
, OPT_Wshift_negative_value
,
11586 "left shift of negative value");
11588 if (TREE_CODE (op1
) == INTEGER_CST
)
11590 if (tree_int_cst_sgn (op1
) < 0)
11593 if (c_inhibit_evaluation_warnings
== 0)
11594 warning_at (location
, OPT_Wshift_count_negative
,
11595 "left shift count is negative");
11597 else if (code0
== VECTOR_TYPE
)
11599 if (compare_tree_int (op1
,
11600 TYPE_PRECISION (TREE_TYPE (type0
)))
11604 if (c_inhibit_evaluation_warnings
== 0)
11605 warning_at (location
, OPT_Wshift_count_overflow
,
11606 "left shift count >= width of vector element");
11609 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11612 if (c_inhibit_evaluation_warnings
== 0)
11613 warning_at (location
, OPT_Wshift_count_overflow
,
11614 "left shift count >= width of type");
11616 else if (TREE_CODE (op0
) == INTEGER_CST
11617 && maybe_warn_shift_overflow (location
, op0
, op1
)
11622 /* Use the type of the value to be shifted. */
11623 result_type
= type0
;
11624 /* Avoid converting op1 to result_type later. */
11631 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11634 if (!vector_types_compatible_elements_p (type0
, type1
))
11636 error_at (location
, "comparing vectors with different "
11638 return error_mark_node
;
11641 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
11642 TYPE_VECTOR_SUBPARTS (type1
)))
11644 error_at (location
, "comparing vectors with different "
11645 "number of elements");
11646 return error_mark_node
;
11649 /* It's not precisely specified how the usual arithmetic
11650 conversions apply to the vector types. Here, we use
11651 the unsigned type if one of the operands is signed and
11652 the other one is unsigned. */
11653 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11655 if (!TYPE_UNSIGNED (type0
))
11656 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11658 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11659 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11660 "types %qT and %qT", type0
, type1
);
11663 /* Always construct signed integer vector type. */
11664 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11666 (TREE_TYPE (type0
))), 0);
11669 error_at (location
, "could not find an integer type "
11670 "of the same size as %qT",
11671 TREE_TYPE (type0
));
11672 return error_mark_node
;
11674 result_type
= build_opaque_vector_type (intt
,
11675 TYPE_VECTOR_SUBPARTS (type0
));
11677 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11678 goto return_build_binary_op
;
11680 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
11681 warning_at (location
,
11683 "comparing floating point with == or != is unsafe");
11684 /* Result of comparison is always int,
11685 but don't convert the args to int! */
11686 build_type
= integer_type_node
;
11687 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11688 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
11689 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11690 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
11692 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11694 if (TREE_CODE (op0
) == ADDR_EXPR
11695 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0))
11696 && !from_macro_expansion_at (location
))
11698 if (code
== EQ_EXPR
)
11699 warning_at (location
,
11701 "the comparison will always evaluate as %<false%> "
11702 "for the address of %qD will never be NULL",
11703 TREE_OPERAND (op0
, 0));
11705 warning_at (location
,
11707 "the comparison will always evaluate as %<true%> "
11708 "for the address of %qD will never be NULL",
11709 TREE_OPERAND (op0
, 0));
11711 result_type
= type0
;
11713 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11715 if (TREE_CODE (op1
) == ADDR_EXPR
11716 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0))
11717 && !from_macro_expansion_at (location
))
11719 if (code
== EQ_EXPR
)
11720 warning_at (location
,
11722 "the comparison will always evaluate as %<false%> "
11723 "for the address of %qD will never be NULL",
11724 TREE_OPERAND (op1
, 0));
11726 warning_at (location
,
11728 "the comparison will always evaluate as %<true%> "
11729 "for the address of %qD will never be NULL",
11730 TREE_OPERAND (op1
, 0));
11732 result_type
= type1
;
11734 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
11736 tree tt0
= TREE_TYPE (type0
);
11737 tree tt1
= TREE_TYPE (type1
);
11738 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
11739 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
11740 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
11742 /* Anything compares with void *. void * compares with anything.
11743 Otherwise, the targets must be compatible
11744 and both must be object or both incomplete. */
11745 if (comp_target_types (location
, type0
, type1
))
11746 result_type
= common_pointer_type (type0
, type1
);
11747 else if (!addr_space_superset (as0
, as1
, &as_common
))
11749 error_at (location
, "comparison of pointers to "
11750 "disjoint address spaces");
11751 return error_mark_node
;
11753 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
11755 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
11756 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11757 "comparison of %<void *%> with function pointer");
11759 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
11761 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
11762 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11763 "comparison of %<void *%> with function pointer");
11766 /* Avoid warning about the volatile ObjC EH puts on decls. */
11768 pedwarn (location
, 0,
11769 "comparison of distinct pointer types lacks a cast");
11771 if (result_type
== NULL_TREE
)
11773 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
11774 result_type
= build_pointer_type
11775 (build_qualified_type (void_type_node
, qual
));
11778 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11780 result_type
= type0
;
11781 pedwarn (location
, 0, "comparison between pointer and integer");
11783 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
11785 result_type
= type1
;
11786 pedwarn (location
, 0, "comparison between pointer and integer");
11788 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
11789 || truth_value_p (TREE_CODE (orig_op0
)))
11790 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
11791 || truth_value_p (TREE_CODE (orig_op1
))))
11792 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
11799 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11802 if (!vector_types_compatible_elements_p (type0
, type1
))
11804 error_at (location
, "comparing vectors with different "
11806 return error_mark_node
;
11809 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
11810 TYPE_VECTOR_SUBPARTS (type1
)))
11812 error_at (location
, "comparing vectors with different "
11813 "number of elements");
11814 return error_mark_node
;
11817 /* It's not precisely specified how the usual arithmetic
11818 conversions apply to the vector types. Here, we use
11819 the unsigned type if one of the operands is signed and
11820 the other one is unsigned. */
11821 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11823 if (!TYPE_UNSIGNED (type0
))
11824 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11826 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11827 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11828 "types %qT and %qT", type0
, type1
);
11831 /* Always construct signed integer vector type. */
11832 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11834 (TREE_TYPE (type0
))), 0);
11837 error_at (location
, "could not find an integer type "
11838 "of the same size as %qT",
11839 TREE_TYPE (type0
));
11840 return error_mark_node
;
11842 result_type
= build_opaque_vector_type (intt
,
11843 TYPE_VECTOR_SUBPARTS (type0
));
11845 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11846 goto return_build_binary_op
;
11848 build_type
= integer_type_node
;
11849 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11850 || code0
== FIXED_POINT_TYPE
)
11851 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11852 || code1
== FIXED_POINT_TYPE
))
11854 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
11856 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
11857 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
11858 addr_space_t as_common
;
11860 if (comp_target_types (location
, type0
, type1
))
11862 result_type
= common_pointer_type (type0
, type1
);
11863 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
11864 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
11865 pedwarn (location
, 0,
11866 "comparison of complete and incomplete pointers");
11867 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
11868 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11869 "ordered comparisons of pointers to functions");
11870 else if (null_pointer_constant_p (orig_op0
)
11871 || null_pointer_constant_p (orig_op1
))
11872 warning_at (location
, OPT_Wextra
,
11873 "ordered comparison of pointer with null pointer");
11876 else if (!addr_space_superset (as0
, as1
, &as_common
))
11878 error_at (location
, "comparison of pointers to "
11879 "disjoint address spaces");
11880 return error_mark_node
;
11884 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
11885 result_type
= build_pointer_type
11886 (build_qualified_type (void_type_node
, qual
));
11887 pedwarn (location
, 0,
11888 "comparison of distinct pointer types lacks a cast");
11891 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11893 result_type
= type0
;
11895 pedwarn (location
, OPT_Wpedantic
,
11896 "ordered comparison of pointer with integer zero");
11897 else if (extra_warnings
)
11898 warning_at (location
, OPT_Wextra
,
11899 "ordered comparison of pointer with integer zero");
11901 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11903 result_type
= type1
;
11905 pedwarn (location
, OPT_Wpedantic
,
11906 "ordered comparison of pointer with integer zero");
11907 else if (extra_warnings
)
11908 warning_at (location
, OPT_Wextra
,
11909 "ordered comparison of pointer with integer zero");
11911 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11913 result_type
= type0
;
11914 pedwarn (location
, 0, "comparison between pointer and integer");
11916 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
11918 result_type
= type1
;
11919 pedwarn (location
, 0, "comparison between pointer and integer");
11922 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
11923 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
11925 op0
= save_expr (op0
);
11926 op1
= save_expr (op1
);
11928 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
11929 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
11932 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
11933 || truth_value_p (TREE_CODE (orig_op0
)))
11934 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
11935 || truth_value_p (TREE_CODE (orig_op1
))))
11936 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
11940 gcc_unreachable ();
11943 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
11944 return error_mark_node
;
11946 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11947 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
11948 || !vector_types_compatible_elements_p (type0
, type1
)))
11950 gcc_rich_location
richloc (location
);
11951 maybe_range_label_for_tree_type_mismatch
11952 label_for_op0 (orig_op0
, orig_op1
),
11953 label_for_op1 (orig_op1
, orig_op0
);
11954 richloc
.maybe_add_expr (orig_op0
, &label_for_op0
);
11955 richloc
.maybe_add_expr (orig_op1
, &label_for_op1
);
11956 binary_op_error (&richloc
, code
, type0
, type1
);
11957 return error_mark_node
;
11960 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11961 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
11963 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11964 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
11966 bool first_complex
= (code0
== COMPLEX_TYPE
);
11967 bool second_complex
= (code1
== COMPLEX_TYPE
);
11968 int none_complex
= (!first_complex
&& !second_complex
);
11970 if (shorten
|| common
|| short_compare
)
11972 result_type
= c_common_type (type0
, type1
);
11973 do_warn_double_promotion (result_type
, type0
, type1
,
11974 "implicit conversion from %qT to %qT "
11975 "to match other operand of binary "
11978 if (result_type
== error_mark_node
)
11979 return error_mark_node
;
11982 if (first_complex
!= second_complex
11983 && (code
== PLUS_EXPR
11984 || code
== MINUS_EXPR
11985 || code
== MULT_EXPR
11986 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
11987 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
11988 && flag_signed_zeros
)
11990 /* An operation on mixed real/complex operands must be
11991 handled specially, but the language-independent code can
11992 more easily optimize the plain complex arithmetic if
11993 -fno-signed-zeros. */
11994 tree real_type
= TREE_TYPE (result_type
);
11996 if (type0
!= orig_type0
|| type1
!= orig_type1
)
11998 gcc_assert (may_need_excess_precision
&& common
);
11999 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
12003 if (TREE_TYPE (op0
) != result_type
)
12004 op0
= convert_and_check (location
, result_type
, op0
);
12005 if (TREE_TYPE (op1
) != real_type
)
12006 op1
= convert_and_check (location
, real_type
, op1
);
12010 if (TREE_TYPE (op0
) != real_type
)
12011 op0
= convert_and_check (location
, real_type
, op0
);
12012 if (TREE_TYPE (op1
) != result_type
)
12013 op1
= convert_and_check (location
, result_type
, op1
);
12015 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
12016 return error_mark_node
;
12019 op0
= save_expr (op0
);
12020 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
12022 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
12027 case TRUNC_DIV_EXPR
:
12028 op1
= save_expr (op1
);
12029 imag
= build2 (resultcode
, real_type
, imag
, op1
);
12030 /* Fall through. */
12033 real
= build2 (resultcode
, real_type
, real
, op1
);
12041 op1
= save_expr (op1
);
12042 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
12044 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
12049 op0
= save_expr (op0
);
12050 imag
= build2 (resultcode
, real_type
, op0
, imag
);
12051 /* Fall through. */
12053 real
= build2 (resultcode
, real_type
, op0
, real
);
12056 real
= build2 (resultcode
, real_type
, op0
, real
);
12057 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
12063 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
12064 goto return_build_binary_op
;
12067 /* For certain operations (which identify themselves by shorten != 0)
12068 if both args were extended from the same smaller type,
12069 do the arithmetic in that type and then extend.
12071 shorten !=0 and !=1 indicates a bitwise operation.
12072 For them, this optimization is safe only if
12073 both args are zero-extended or both are sign-extended.
12074 Otherwise, we might change the result.
12075 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12076 but calculated in (unsigned short) it would be (unsigned short)-1. */
12078 if (shorten
&& none_complex
)
12080 final_type
= result_type
;
12081 result_type
= shorten_binary_op (result_type
, op0
, op1
,
12085 /* Shifts can be shortened if shifting right. */
12090 tree arg0
= get_narrower (op0
, &unsigned_arg
);
12092 final_type
= result_type
;
12094 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
12095 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
12097 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
12098 && tree_int_cst_sgn (op1
) > 0
12099 /* We can shorten only if the shift count is less than the
12100 number of bits in the smaller type size. */
12101 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
12102 /* We cannot drop an unsigned shift after sign-extension. */
12103 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
12105 /* Do an unsigned shift if the operand was zero-extended. */
12107 = c_common_signed_or_unsigned_type (unsigned_arg
,
12109 /* Convert value-to-be-shifted to that type. */
12110 if (TREE_TYPE (op0
) != result_type
)
12111 op0
= convert (result_type
, op0
);
12116 /* Comparison operations are shortened too but differently.
12117 They identify themselves by setting short_compare = 1. */
12121 /* Don't write &op0, etc., because that would prevent op0
12122 from being kept in a register.
12123 Instead, make copies of the our local variables and
12124 pass the copies by reference, then copy them back afterward. */
12125 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
12126 enum tree_code xresultcode
= resultcode
;
12128 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
12131 if (val
!= NULL_TREE
)
12134 goto return_build_binary_op
;
12137 op0
= xop0
, op1
= xop1
;
12139 resultcode
= xresultcode
;
12141 if (c_inhibit_evaluation_warnings
== 0)
12143 bool op0_maybe_const
= true;
12144 bool op1_maybe_const
= true;
12145 tree orig_op0_folded
, orig_op1_folded
;
12147 if (in_late_binary_op
)
12149 orig_op0_folded
= orig_op0
;
12150 orig_op1_folded
= orig_op1
;
12154 /* Fold for the sake of possible warnings, as in
12155 build_conditional_expr. This requires the
12156 "original" values to be folded, not just op0 and
12158 c_inhibit_evaluation_warnings
++;
12159 op0
= c_fully_fold (op0
, require_constant_value
,
12161 op1
= c_fully_fold (op1
, require_constant_value
,
12163 c_inhibit_evaluation_warnings
--;
12164 orig_op0_folded
= c_fully_fold (orig_op0
,
12165 require_constant_value
,
12167 orig_op1_folded
= c_fully_fold (orig_op1
,
12168 require_constant_value
,
12172 if (warn_sign_compare
)
12173 warn_for_sign_compare (location
, orig_op0_folded
,
12174 orig_op1_folded
, op0
, op1
,
12175 result_type
, resultcode
);
12176 if (!in_late_binary_op
&& !int_operands
)
12178 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
12179 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
12180 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
12181 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
12187 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12188 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12189 Then the expression will be built.
12190 It will be given type FINAL_TYPE if that is nonzero;
12191 otherwise, it will be given type RESULT_TYPE. */
12195 gcc_rich_location
richloc (location
);
12196 maybe_range_label_for_tree_type_mismatch
12197 label_for_op0 (orig_op0
, orig_op1
),
12198 label_for_op1 (orig_op1
, orig_op0
);
12199 richloc
.maybe_add_expr (orig_op0
, &label_for_op0
);
12200 richloc
.maybe_add_expr (orig_op1
, &label_for_op1
);
12201 binary_op_error (&richloc
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
12202 return error_mark_node
;
12205 if (build_type
== NULL_TREE
)
12207 build_type
= result_type
;
12208 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
12211 gcc_assert (may_need_excess_precision
&& common
);
12212 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
12218 op0
= ep_convert_and_check (location
, result_type
, op0
,
12219 semantic_result_type
);
12220 op1
= ep_convert_and_check (location
, result_type
, op1
,
12221 semantic_result_type
);
12223 /* This can happen if one operand has a vector type, and the other
12224 has a different type. */
12225 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
12226 return error_mark_node
;
12229 if (sanitize_flags_p ((SANITIZE_SHIFT
12230 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
12231 && current_function_decl
!= NULL_TREE
12232 && (doing_div_or_mod
|| doing_shift
)
12233 && !require_constant_value
)
12235 /* OP0 and/or OP1 might have side-effects. */
12236 op0
= save_expr (op0
);
12237 op1
= save_expr (op1
);
12238 op0
= c_fully_fold (op0
, false, NULL
);
12239 op1
= c_fully_fold (op1
, false, NULL
);
12240 if (doing_div_or_mod
&& (sanitize_flags_p ((SANITIZE_DIVIDE
12241 | SANITIZE_FLOAT_DIVIDE
))))
12242 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
12243 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
12244 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
12247 /* Treat expressions in initializers specially as they can't trap. */
12248 if (int_const_or_overflow
)
12249 ret
= (require_constant_value
12250 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
12252 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
12254 ret
= build2 (resultcode
, build_type
, op0
, op1
);
12255 if (final_type
!= NULL_TREE
)
12256 ret
= convert (final_type
, ret
);
12258 return_build_binary_op
:
12259 gcc_assert (ret
!= error_mark_node
);
12260 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
12261 ret
= (int_operands
12262 ? note_integer_operands (ret
)
12263 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
12264 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
12265 && !in_late_binary_op
)
12266 ret
= note_integer_operands (ret
);
12267 protected_set_expr_location (ret
, location
);
12269 if (instrument_expr
!= NULL
)
12270 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
12271 instrument_expr
, ret
);
12273 if (semantic_result_type
)
12274 ret
= build1_loc (location
, EXCESS_PRECISION_EXPR
,
12275 semantic_result_type
, ret
);
12281 /* Convert EXPR to be a truth-value, validating its type for this
12282 purpose. LOCATION is the source location for the expression. */
12285 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
12287 bool int_const
, int_operands
;
12289 switch (TREE_CODE (TREE_TYPE (expr
)))
12292 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
12293 return error_mark_node
;
12296 error_at (location
, "used struct type value where scalar is required");
12297 return error_mark_node
;
12300 error_at (location
, "used union type value where scalar is required");
12301 return error_mark_node
;
12304 error_at (location
, "void value not ignored as it ought to be");
12305 return error_mark_node
;
12308 if (reject_gcc_builtin (expr
))
12309 return error_mark_node
;
12312 case FUNCTION_TYPE
:
12313 gcc_unreachable ();
12316 error_at (location
, "used vector type where scalar is required");
12317 return error_mark_node
;
12323 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
12324 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
12325 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
12327 expr
= remove_c_maybe_const_expr (expr
);
12328 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
12329 convert (TREE_TYPE (expr
), integer_zero_node
));
12330 expr
= note_integer_operands (expr
);
12333 /* ??? Should we also give an error for vectors rather than leaving
12334 those to give errors later? */
12335 expr
= c_common_truthvalue_conversion (location
, expr
);
12337 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
12339 if (TREE_OVERFLOW (expr
))
12342 return note_integer_operands (expr
);
12344 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
12345 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
12350 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12354 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
12356 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
12358 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
12359 /* Executing a compound literal inside a function reinitializes
12361 if (!TREE_STATIC (decl
))
12369 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12370 statement. LOC is the location of the construct. */
12373 c_finish_omp_construct (location_t loc
, enum tree_code code
, tree body
,
12376 body
= c_end_compound_stmt (loc
, body
, true);
12378 tree stmt
= make_node (code
);
12379 TREE_TYPE (stmt
) = void_type_node
;
12380 OMP_BODY (stmt
) = body
;
12381 OMP_CLAUSES (stmt
) = clauses
;
12382 SET_EXPR_LOCATION (stmt
, loc
);
12384 return add_stmt (stmt
);
12387 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12388 statement. LOC is the location of the OACC_DATA. */
12391 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
12395 block
= c_end_compound_stmt (loc
, block
, true);
12397 stmt
= make_node (OACC_DATA
);
12398 TREE_TYPE (stmt
) = void_type_node
;
12399 OACC_DATA_CLAUSES (stmt
) = clauses
;
12400 OACC_DATA_BODY (stmt
) = block
;
12401 SET_EXPR_LOCATION (stmt
, loc
);
12403 return add_stmt (stmt
);
12406 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12407 statement. LOC is the location of the OACC_HOST_DATA. */
12410 c_finish_oacc_host_data (location_t loc
, tree clauses
, tree block
)
12414 block
= c_end_compound_stmt (loc
, block
, true);
12416 stmt
= make_node (OACC_HOST_DATA
);
12417 TREE_TYPE (stmt
) = void_type_node
;
12418 OACC_HOST_DATA_CLAUSES (stmt
) = clauses
;
12419 OACC_HOST_DATA_BODY (stmt
) = block
;
12420 SET_EXPR_LOCATION (stmt
, loc
);
12422 return add_stmt (stmt
);
12425 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12428 c_begin_omp_parallel (void)
12432 keep_next_level ();
12433 block
= c_begin_compound_stmt (true);
12438 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12439 statement. LOC is the location of the OMP_PARALLEL. */
12442 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
12446 block
= c_end_compound_stmt (loc
, block
, true);
12448 stmt
= make_node (OMP_PARALLEL
);
12449 TREE_TYPE (stmt
) = void_type_node
;
12450 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
12451 OMP_PARALLEL_BODY (stmt
) = block
;
12452 SET_EXPR_LOCATION (stmt
, loc
);
12454 return add_stmt (stmt
);
12457 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12460 c_begin_omp_task (void)
12464 keep_next_level ();
12465 block
= c_begin_compound_stmt (true);
12470 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12471 statement. LOC is the location of the #pragma. */
12474 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
12478 block
= c_end_compound_stmt (loc
, block
, true);
12480 stmt
= make_node (OMP_TASK
);
12481 TREE_TYPE (stmt
) = void_type_node
;
12482 OMP_TASK_CLAUSES (stmt
) = clauses
;
12483 OMP_TASK_BODY (stmt
) = block
;
12484 SET_EXPR_LOCATION (stmt
, loc
);
12486 return add_stmt (stmt
);
12489 /* Generate GOMP_cancel call for #pragma omp cancel. */
12492 c_finish_omp_cancel (location_t loc
, tree clauses
)
12494 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
12496 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12498 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
12500 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12502 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12506 error_at (loc
, "%<#pragma omp cancel%> must specify one of "
12507 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12511 tree ifc
= omp_find_clause (clauses
, OMP_CLAUSE_IF
);
12512 if (ifc
!= NULL_TREE
)
12514 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
12515 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
12516 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
12517 build_zero_cst (type
));
12520 ifc
= boolean_true_node
;
12521 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
12522 build_int_cst (integer_type_node
, mask
),
12527 /* Generate GOMP_cancellation_point call for
12528 #pragma omp cancellation point. */
12531 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
12533 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
12535 if (omp_find_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12537 else if (omp_find_clause (clauses
, OMP_CLAUSE_FOR
))
12539 else if (omp_find_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12541 else if (omp_find_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12545 error_at (loc
, "%<#pragma omp cancellation point%> must specify one of "
12546 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12550 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
12551 build_int_cst (integer_type_node
, mask
));
12555 /* Helper function for handle_omp_array_sections. Called recursively
12556 to handle multiple array-section-subscripts. C is the clause,
12557 T current expression (initially OMP_CLAUSE_DECL), which is either
12558 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12559 expression if specified, TREE_VALUE length expression if specified,
12560 TREE_CHAIN is what it has been specified after, or some decl.
12561 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12562 set to true if any of the array-section-subscript could have length
12563 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12564 first array-section-subscript which is known not to have length
12566 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12567 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12568 all are or may have length of 1, array-section-subscript [:2] is the
12569 first one known not to have length 1. For array-section-subscript
12570 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12571 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12572 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12573 case though, as some lengths could be zero. */
12576 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
12577 bool &maybe_zero_len
, unsigned int &first_non_one
,
12578 enum c_omp_region_type ort
)
12580 tree ret
, low_bound
, length
, type
;
12581 if (TREE_CODE (t
) != TREE_LIST
)
12583 if (error_operand_p (t
))
12584 return error_mark_node
;
12586 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12587 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
12589 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qE in %qs clause",
12590 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12591 return error_mark_node
;
12593 if (TREE_CODE (t
) == COMPONENT_REF
12594 && ort
== C_ORT_OMP
12595 && (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12596 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO
12597 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FROM
))
12599 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
12601 error_at (OMP_CLAUSE_LOCATION (c
),
12602 "bit-field %qE in %qs clause",
12603 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12604 return error_mark_node
;
12606 while (TREE_CODE (t
) == COMPONENT_REF
)
12608 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == UNION_TYPE
)
12610 error_at (OMP_CLAUSE_LOCATION (c
),
12611 "%qE is a member of a union", t
);
12612 return error_mark_node
;
12614 t
= TREE_OPERAND (t
, 0);
12617 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
12620 error_at (OMP_CLAUSE_LOCATION (c
),
12621 "%qD is not a variable in %qs clause", t
,
12622 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12624 error_at (OMP_CLAUSE_LOCATION (c
),
12625 "%qE is not a variable in %qs clause", t
,
12626 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12627 return error_mark_node
;
12629 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12630 && TYPE_ATOMIC (TREE_TYPE (t
)))
12632 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qD in %qs clause",
12633 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12634 return error_mark_node
;
12636 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12638 && DECL_THREAD_LOCAL_P (t
))
12640 error_at (OMP_CLAUSE_LOCATION (c
),
12641 "%qD is threadprivate variable in %qs clause", t
,
12642 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12643 return error_mark_node
;
12645 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12646 && TYPE_ATOMIC (TREE_TYPE (t
))
12647 && POINTER_TYPE_P (TREE_TYPE (t
)))
12649 /* If the array section is pointer based and the pointer
12650 itself is _Atomic qualified, we need to atomically load
12653 memset (&expr
, 0, sizeof (expr
));
12655 expr
= convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c
),
12656 expr
, false, false);
12662 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
12663 maybe_zero_len
, first_non_one
, ort
);
12664 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
12667 type
= TREE_TYPE (ret
);
12668 low_bound
= TREE_PURPOSE (t
);
12669 length
= TREE_VALUE (t
);
12671 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
12672 return error_mark_node
;
12674 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
12676 error_at (OMP_CLAUSE_LOCATION (c
),
12677 "low bound %qE of array section does not have integral type",
12679 return error_mark_node
;
12681 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
12683 error_at (OMP_CLAUSE_LOCATION (c
),
12684 "length %qE of array section does not have integral type",
12686 return error_mark_node
;
12689 && TREE_CODE (low_bound
) == INTEGER_CST
12690 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12691 > TYPE_PRECISION (sizetype
))
12692 low_bound
= fold_convert (sizetype
, low_bound
);
12694 && TREE_CODE (length
) == INTEGER_CST
12695 && TYPE_PRECISION (TREE_TYPE (length
))
12696 > TYPE_PRECISION (sizetype
))
12697 length
= fold_convert (sizetype
, length
);
12698 if (low_bound
== NULL_TREE
)
12699 low_bound
= integer_zero_node
;
12701 if (length
!= NULL_TREE
)
12703 if (!integer_nonzerop (length
))
12705 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12706 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12708 if (integer_zerop (length
))
12710 error_at (OMP_CLAUSE_LOCATION (c
),
12711 "zero length array section in %qs clause",
12712 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12713 return error_mark_node
;
12717 maybe_zero_len
= true;
12719 if (first_non_one
== types
.length ()
12720 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
12723 if (TREE_CODE (type
) == ARRAY_TYPE
)
12725 if (length
== NULL_TREE
12726 && (TYPE_DOMAIN (type
) == NULL_TREE
12727 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
12729 error_at (OMP_CLAUSE_LOCATION (c
),
12730 "for unknown bound array type length expression must "
12732 return error_mark_node
;
12734 if (TREE_CODE (low_bound
) == INTEGER_CST
12735 && tree_int_cst_sgn (low_bound
) == -1)
12737 error_at (OMP_CLAUSE_LOCATION (c
),
12738 "negative low bound in array section in %qs clause",
12739 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12740 return error_mark_node
;
12742 if (length
!= NULL_TREE
12743 && TREE_CODE (length
) == INTEGER_CST
12744 && tree_int_cst_sgn (length
) == -1)
12746 error_at (OMP_CLAUSE_LOCATION (c
),
12747 "negative length in array section in %qs clause",
12748 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12749 return error_mark_node
;
12751 if (TYPE_DOMAIN (type
)
12752 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
12753 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
12757 = fold_convert (sizetype
, TYPE_MAX_VALUE (TYPE_DOMAIN (type
)));
12758 size
= size_binop (PLUS_EXPR
, size
, size_one_node
);
12759 if (TREE_CODE (low_bound
) == INTEGER_CST
)
12761 if (tree_int_cst_lt (size
, low_bound
))
12763 error_at (OMP_CLAUSE_LOCATION (c
),
12764 "low bound %qE above array section size "
12765 "in %qs clause", low_bound
,
12766 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12767 return error_mark_node
;
12769 if (tree_int_cst_equal (size
, low_bound
))
12771 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12772 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12774 error_at (OMP_CLAUSE_LOCATION (c
),
12775 "zero length array section in %qs clause",
12776 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12777 return error_mark_node
;
12779 maybe_zero_len
= true;
12781 else if (length
== NULL_TREE
12782 && first_non_one
== types
.length ()
12783 && tree_int_cst_equal
12784 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
12788 else if (length
== NULL_TREE
)
12790 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12791 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
)
12792 maybe_zero_len
= true;
12793 if (first_non_one
== types
.length ())
12796 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
12798 if (tree_int_cst_lt (size
, length
))
12800 error_at (OMP_CLAUSE_LOCATION (c
),
12801 "length %qE above array section size "
12802 "in %qs clause", length
,
12803 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12804 return error_mark_node
;
12806 if (TREE_CODE (low_bound
) == INTEGER_CST
)
12809 = size_binop (PLUS_EXPR
,
12810 fold_convert (sizetype
, low_bound
),
12811 fold_convert (sizetype
, length
));
12812 if (TREE_CODE (lbpluslen
) == INTEGER_CST
12813 && tree_int_cst_lt (size
, lbpluslen
))
12815 error_at (OMP_CLAUSE_LOCATION (c
),
12816 "high bound %qE above array section size "
12817 "in %qs clause", lbpluslen
,
12818 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12819 return error_mark_node
;
12824 else if (length
== NULL_TREE
)
12826 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12827 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
)
12828 maybe_zero_len
= true;
12829 if (first_non_one
== types
.length ())
12833 /* For [lb:] we will need to evaluate lb more than once. */
12834 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
12836 tree lb
= save_expr (low_bound
);
12837 if (lb
!= low_bound
)
12839 TREE_PURPOSE (t
) = lb
;
12844 else if (TREE_CODE (type
) == POINTER_TYPE
)
12846 if (length
== NULL_TREE
)
12848 error_at (OMP_CLAUSE_LOCATION (c
),
12849 "for pointer type length expression must be specified");
12850 return error_mark_node
;
12852 if (length
!= NULL_TREE
12853 && TREE_CODE (length
) == INTEGER_CST
12854 && tree_int_cst_sgn (length
) == -1)
12856 error_at (OMP_CLAUSE_LOCATION (c
),
12857 "negative length in array section in %qs clause",
12858 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12859 return error_mark_node
;
12861 /* If there is a pointer type anywhere but in the very first
12862 array-section-subscript, the array section can't be contiguous. */
12863 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12864 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
12866 error_at (OMP_CLAUSE_LOCATION (c
),
12867 "array section is not contiguous in %qs clause",
12868 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12869 return error_mark_node
;
12874 error_at (OMP_CLAUSE_LOCATION (c
),
12875 "%qE does not have pointer or array type", ret
);
12876 return error_mark_node
;
12878 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
12879 types
.safe_push (TREE_TYPE (ret
));
12880 /* We will need to evaluate lb more than once. */
12881 tree lb
= save_expr (low_bound
);
12882 if (lb
!= low_bound
)
12884 TREE_PURPOSE (t
) = lb
;
12887 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
12891 /* Handle array sections for clause C. */
12894 handle_omp_array_sections (tree c
, enum c_omp_region_type ort
)
12896 bool maybe_zero_len
= false;
12897 unsigned int first_non_one
= 0;
12898 auto_vec
<tree
, 10> types
;
12899 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
12900 maybe_zero_len
, first_non_one
,
12902 if (first
== error_mark_node
)
12904 if (first
== NULL_TREE
)
12906 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
12908 tree t
= OMP_CLAUSE_DECL (c
);
12909 tree tem
= NULL_TREE
;
12910 /* Need to evaluate side effects in the length expressions
12912 while (TREE_CODE (t
) == TREE_LIST
)
12914 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
12916 if (tem
== NULL_TREE
)
12917 tem
= TREE_VALUE (t
);
12919 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
12920 TREE_VALUE (t
), tem
);
12922 t
= TREE_CHAIN (t
);
12925 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
12926 first
= c_fully_fold (first
, false, NULL
, true);
12927 OMP_CLAUSE_DECL (c
) = first
;
12931 unsigned int num
= types
.length (), i
;
12932 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
12933 tree condition
= NULL_TREE
;
12935 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
12936 maybe_zero_len
= true;
12938 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
12939 t
= TREE_CHAIN (t
))
12941 tree low_bound
= TREE_PURPOSE (t
);
12942 tree length
= TREE_VALUE (t
);
12946 && TREE_CODE (low_bound
) == INTEGER_CST
12947 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12948 > TYPE_PRECISION (sizetype
))
12949 low_bound
= fold_convert (sizetype
, low_bound
);
12951 && TREE_CODE (length
) == INTEGER_CST
12952 && TYPE_PRECISION (TREE_TYPE (length
))
12953 > TYPE_PRECISION (sizetype
))
12954 length
= fold_convert (sizetype
, length
);
12955 if (low_bound
== NULL_TREE
)
12956 low_bound
= integer_zero_node
;
12957 if (!maybe_zero_len
&& i
> first_non_one
)
12959 if (integer_nonzerop (low_bound
))
12960 goto do_warn_noncontiguous
;
12961 if (length
!= NULL_TREE
12962 && TREE_CODE (length
) == INTEGER_CST
12963 && TYPE_DOMAIN (types
[i
])
12964 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
12965 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
12969 size
= size_binop (PLUS_EXPR
,
12970 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
12972 if (!tree_int_cst_equal (length
, size
))
12974 do_warn_noncontiguous
:
12975 error_at (OMP_CLAUSE_LOCATION (c
),
12976 "array section is not contiguous in %qs "
12978 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12982 if (length
!= NULL_TREE
12983 && TREE_SIDE_EFFECTS (length
))
12985 if (side_effects
== NULL_TREE
)
12986 side_effects
= length
;
12988 side_effects
= build2 (COMPOUND_EXPR
,
12989 TREE_TYPE (side_effects
),
12990 length
, side_effects
);
12997 if (i
> first_non_one
12998 && ((length
&& integer_nonzerop (length
))
12999 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
))
13002 l
= fold_convert (sizetype
, length
);
13005 l
= size_binop (PLUS_EXPR
,
13006 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
13008 l
= size_binop (MINUS_EXPR
, l
,
13009 fold_convert (sizetype
, low_bound
));
13011 if (i
> first_non_one
)
13013 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
13015 if (condition
== NULL_TREE
)
13018 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
13021 else if (size
== NULL_TREE
)
13023 size
= size_in_bytes (TREE_TYPE (types
[i
]));
13024 tree eltype
= TREE_TYPE (types
[num
- 1]);
13025 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
13026 eltype
= TREE_TYPE (eltype
);
13027 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
13029 if (integer_zerop (size
)
13030 || integer_zerop (size_in_bytes (eltype
)))
13032 error_at (OMP_CLAUSE_LOCATION (c
),
13033 "zero length array section in %qs clause",
13034 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13035 return error_mark_node
;
13037 size
= size_binop (EXACT_DIV_EXPR
, size
,
13038 size_in_bytes (eltype
));
13040 size
= size_binop (MULT_EXPR
, size
, l
);
13042 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
13043 size
, size_zero_node
);
13046 size
= size_binop (MULT_EXPR
, size
, l
);
13050 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
13051 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
13053 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
13054 size
= c_fully_fold (size
, false, NULL
);
13055 tree index_type
= build_index_type (size
);
13056 tree eltype
= TREE_TYPE (first
);
13057 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
13058 eltype
= TREE_TYPE (eltype
);
13059 tree type
= build_array_type (eltype
, index_type
);
13060 tree ptype
= build_pointer_type (eltype
);
13061 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13062 t
= build_fold_addr_expr (t
);
13063 tree t2
= build_fold_addr_expr (first
);
13064 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
13065 ptrdiff_type_node
, t2
);
13066 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13067 ptrdiff_type_node
, t2
,
13068 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
13069 ptrdiff_type_node
, t
));
13070 t2
= c_fully_fold (t2
, false, NULL
);
13071 if (tree_fits_shwi_p (t2
))
13072 t
= build2 (MEM_REF
, type
, t
,
13073 build_int_cst (ptype
, tree_to_shwi (t2
)));
13076 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), sizetype
, t2
);
13077 t
= build2_loc (OMP_CLAUSE_LOCATION (c
), POINTER_PLUS_EXPR
,
13078 TREE_TYPE (t
), t
, t2
);
13079 t
= build2 (MEM_REF
, type
, t
, build_int_cst (ptype
, 0));
13081 OMP_CLAUSE_DECL (c
) = t
;
13084 first
= c_fully_fold (first
, false, NULL
);
13085 OMP_CLAUSE_DECL (c
) = first
;
13087 size
= c_fully_fold (size
, false, NULL
);
13088 OMP_CLAUSE_SIZE (c
) = size
;
13089 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
13090 || (TREE_CODE (t
) == COMPONENT_REF
13091 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
))
13093 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
13094 if (ort
== C_ORT_OMP
|| ort
== C_ORT_ACC
)
13095 switch (OMP_CLAUSE_MAP_KIND (c
))
13097 case GOMP_MAP_ALLOC
:
13099 case GOMP_MAP_FROM
:
13100 case GOMP_MAP_TOFROM
:
13101 case GOMP_MAP_ALWAYS_TO
:
13102 case GOMP_MAP_ALWAYS_FROM
:
13103 case GOMP_MAP_ALWAYS_TOFROM
:
13104 case GOMP_MAP_RELEASE
:
13105 case GOMP_MAP_DELETE
:
13106 case GOMP_MAP_FORCE_TO
:
13107 case GOMP_MAP_FORCE_FROM
:
13108 case GOMP_MAP_FORCE_TOFROM
:
13109 case GOMP_MAP_FORCE_PRESENT
:
13110 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c
) = 1;
13115 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
13116 if (ort
!= C_ORT_OMP
&& ort
!= C_ORT_ACC
)
13117 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_POINTER
);
13118 else if (TREE_CODE (t
) == COMPONENT_REF
)
13119 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_ALWAYS_POINTER
);
13121 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
13122 if (OMP_CLAUSE_MAP_KIND (c2
) != GOMP_MAP_FIRSTPRIVATE_POINTER
13123 && !c_mark_addressable (t
))
13125 OMP_CLAUSE_DECL (c2
) = t
;
13126 t
= build_fold_addr_expr (first
);
13127 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
13128 tree ptr
= OMP_CLAUSE_DECL (c2
);
13129 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
13130 ptr
= build_fold_addr_expr (ptr
);
13131 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13132 ptrdiff_type_node
, t
,
13133 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
13134 ptrdiff_type_node
, ptr
));
13135 t
= c_fully_fold (t
, false, NULL
);
13136 OMP_CLAUSE_SIZE (c2
) = t
;
13137 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
13138 OMP_CLAUSE_CHAIN (c
) = c2
;
13143 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
13144 an inline call. But, remap
13145 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13146 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13149 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
13150 tree decl
, tree placeholder
)
13153 hash_map
<tree
, tree
> decl_map
;
13155 decl_map
.put (omp_decl1
, placeholder
);
13156 decl_map
.put (omp_decl2
, decl
);
13157 memset (&id
, 0, sizeof (id
));
13158 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
13159 id
.dst_fn
= current_function_decl
;
13160 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
13161 id
.decl_map
= &decl_map
;
13163 id
.copy_decl
= copy_decl_no_change
;
13164 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
13165 id
.transform_new_cfg
= true;
13166 id
.transform_return_to_modify
= false;
13167 id
.transform_lang_insert_block
= NULL
;
13169 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
13173 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13174 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13177 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
13179 if (*tp
== (tree
) data
)
13184 /* For all elements of CLAUSES, validate them against their constraints.
13185 Remove any elements from the list that are invalid. */
13188 c_finish_omp_clauses (tree clauses
, enum c_omp_region_type ort
)
13190 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
13191 bitmap_head aligned_head
, map_head
, map_field_head
, oacc_reduction_head
;
13192 tree c
, t
, type
, *pc
;
13193 tree simdlen
= NULL_TREE
, safelen
= NULL_TREE
;
13194 bool branch_seen
= false;
13195 bool copyprivate_seen
= false;
13196 bool linear_variable_step_check
= false;
13197 tree
*nowait_clause
= NULL
;
13198 bool ordered_seen
= false;
13199 tree schedule_clause
= NULL_TREE
;
13200 bool oacc_async
= false;
13202 bitmap_obstack_initialize (NULL
);
13203 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
13204 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
13205 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
13206 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
13207 bitmap_initialize (&map_head
, &bitmap_default_obstack
);
13208 bitmap_initialize (&map_field_head
, &bitmap_default_obstack
);
13209 bitmap_initialize (&oacc_reduction_head
, &bitmap_default_obstack
);
13211 if (ort
& C_ORT_ACC
)
13212 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
13213 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ASYNC
)
13219 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
13221 bool remove
= false;
13222 bool need_complete
= false;
13223 bool need_implicitly_determined
= false;
13225 switch (OMP_CLAUSE_CODE (c
))
13227 case OMP_CLAUSE_SHARED
:
13228 need_implicitly_determined
= true;
13229 goto check_dup_generic
;
13231 case OMP_CLAUSE_PRIVATE
:
13232 need_complete
= true;
13233 need_implicitly_determined
= true;
13234 goto check_dup_generic
;
13236 case OMP_CLAUSE_REDUCTION
:
13237 need_implicitly_determined
= true;
13238 t
= OMP_CLAUSE_DECL (c
);
13239 if (TREE_CODE (t
) == TREE_LIST
)
13241 if (handle_omp_array_sections (c
, ort
))
13247 t
= OMP_CLAUSE_DECL (c
);
13249 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
13250 if (t
== error_mark_node
)
13256 c_mark_addressable (t
);
13257 type
= TREE_TYPE (t
);
13258 if (TREE_CODE (t
) == MEM_REF
)
13259 type
= TREE_TYPE (type
);
13260 if (TREE_CODE (type
) == ARRAY_TYPE
)
13262 tree oatype
= type
;
13263 gcc_assert (TREE_CODE (t
) != MEM_REF
);
13264 while (TREE_CODE (type
) == ARRAY_TYPE
)
13265 type
= TREE_TYPE (type
);
13266 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
13268 error_at (OMP_CLAUSE_LOCATION (c
),
13269 "%qD in %<reduction%> clause is a zero size array",
13274 tree size
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (oatype
),
13275 TYPE_SIZE_UNIT (type
));
13276 if (integer_zerop (size
))
13278 error_at (OMP_CLAUSE_LOCATION (c
),
13279 "%qD in %<reduction%> clause is a zero size array",
13284 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
13285 tree index_type
= build_index_type (size
);
13286 tree atype
= build_array_type (type
, index_type
);
13287 tree ptype
= build_pointer_type (type
);
13288 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13289 t
= build_fold_addr_expr (t
);
13290 t
= build2 (MEM_REF
, atype
, t
, build_int_cst (ptype
, 0));
13291 OMP_CLAUSE_DECL (c
) = t
;
13293 if (TYPE_ATOMIC (type
))
13295 error_at (OMP_CLAUSE_LOCATION (c
),
13296 "%<_Atomic%> %qE in %<reduction%> clause", t
);
13300 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
13301 && (FLOAT_TYPE_P (type
)
13302 || TREE_CODE (type
) == COMPLEX_TYPE
))
13304 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
13305 const char *r_name
= NULL
;
13314 if (TREE_CODE (type
) == COMPLEX_TYPE
)
13318 if (TREE_CODE (type
) == COMPLEX_TYPE
)
13330 case TRUTH_ANDIF_EXPR
:
13331 if (FLOAT_TYPE_P (type
))
13334 case TRUTH_ORIF_EXPR
:
13335 if (FLOAT_TYPE_P (type
))
13339 gcc_unreachable ();
13343 error_at (OMP_CLAUSE_LOCATION (c
),
13344 "%qE has invalid type for %<reduction(%s)%>",
13350 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
13352 error_at (OMP_CLAUSE_LOCATION (c
),
13353 "user defined reduction not found for %qE", t
);
13357 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
13359 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
13360 type
= TYPE_MAIN_VARIANT (type
);
13361 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
13362 VAR_DECL
, NULL_TREE
, type
);
13363 tree decl_placeholder
= NULL_TREE
;
13364 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
13365 DECL_ARTIFICIAL (placeholder
) = 1;
13366 DECL_IGNORED_P (placeholder
) = 1;
13367 if (TREE_CODE (t
) == MEM_REF
)
13369 decl_placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
13370 VAR_DECL
, NULL_TREE
, type
);
13371 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
) = decl_placeholder
;
13372 DECL_ARTIFICIAL (decl_placeholder
) = 1;
13373 DECL_IGNORED_P (decl_placeholder
) = 1;
13375 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
13376 c_mark_addressable (placeholder
);
13377 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
13378 c_mark_addressable (decl_placeholder
? decl_placeholder
13379 : OMP_CLAUSE_DECL (c
));
13380 OMP_CLAUSE_REDUCTION_MERGE (c
)
13381 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
13382 TREE_VEC_ELT (list
, 0),
13383 TREE_VEC_ELT (list
, 1),
13384 decl_placeholder
? decl_placeholder
13385 : OMP_CLAUSE_DECL (c
), placeholder
);
13386 OMP_CLAUSE_REDUCTION_MERGE (c
)
13387 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
13388 void_type_node
, NULL_TREE
,
13389 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
13390 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
13391 if (TREE_VEC_LENGTH (list
) == 6)
13393 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
13394 c_mark_addressable (decl_placeholder
? decl_placeholder
13395 : OMP_CLAUSE_DECL (c
));
13396 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
13397 c_mark_addressable (placeholder
);
13398 tree init
= TREE_VEC_ELT (list
, 5);
13399 if (init
== error_mark_node
)
13400 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
13401 OMP_CLAUSE_REDUCTION_INIT (c
)
13402 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
13403 TREE_VEC_ELT (list
, 3),
13404 decl_placeholder
? decl_placeholder
13405 : OMP_CLAUSE_DECL (c
), placeholder
);
13406 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
13408 tree v
= decl_placeholder
? decl_placeholder
: t
;
13409 OMP_CLAUSE_REDUCTION_INIT (c
)
13410 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
,
13411 OMP_CLAUSE_REDUCTION_INIT (c
));
13413 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
13414 c_find_omp_placeholder_r
,
13415 placeholder
, NULL
))
13416 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
13421 tree v
= decl_placeholder
? decl_placeholder
: t
;
13422 if (AGGREGATE_TYPE_P (TREE_TYPE (v
)))
13423 init
= build_constructor (TREE_TYPE (v
), NULL
);
13425 init
= fold_convert (TREE_TYPE (v
), integer_zero_node
);
13426 OMP_CLAUSE_REDUCTION_INIT (c
)
13427 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
, init
);
13429 OMP_CLAUSE_REDUCTION_INIT (c
)
13430 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
13431 void_type_node
, NULL_TREE
,
13432 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
13433 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
13435 if (TREE_CODE (t
) == MEM_REF
)
13437 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))) == NULL_TREE
13438 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))))
13441 sorry ("variable length element type in array "
13442 "%<reduction%> clause");
13446 t
= TREE_OPERAND (t
, 0);
13447 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
13448 t
= TREE_OPERAND (t
, 0);
13449 if (TREE_CODE (t
) == ADDR_EXPR
)
13450 t
= TREE_OPERAND (t
, 0);
13452 goto check_dup_generic_t
;
13454 case OMP_CLAUSE_COPYPRIVATE
:
13455 copyprivate_seen
= true;
13458 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
13459 "%<nowait%> clause must not be used together "
13460 "with %<copyprivate%>");
13461 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
13462 nowait_clause
= NULL
;
13464 goto check_dup_generic
;
13466 case OMP_CLAUSE_COPYIN
:
13467 t
= OMP_CLAUSE_DECL (c
);
13468 if (!VAR_P (t
) || !DECL_THREAD_LOCAL_P (t
))
13470 error_at (OMP_CLAUSE_LOCATION (c
),
13471 "%qE must be %<threadprivate%> for %<copyin%>", t
);
13475 goto check_dup_generic
;
13477 case OMP_CLAUSE_LINEAR
:
13478 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
13479 need_implicitly_determined
= true;
13480 t
= OMP_CLAUSE_DECL (c
);
13481 if (ort
!= C_ORT_OMP_DECLARE_SIMD
13482 && OMP_CLAUSE_LINEAR_KIND (c
) != OMP_CLAUSE_LINEAR_DEFAULT
)
13484 error_at (OMP_CLAUSE_LOCATION (c
),
13485 "modifier should not be specified in %<linear%> "
13486 "clause on %<simd%> or %<for%> constructs");
13487 OMP_CLAUSE_LINEAR_KIND (c
) = OMP_CLAUSE_LINEAR_DEFAULT
;
13489 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
13490 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
13492 error_at (OMP_CLAUSE_LOCATION (c
),
13493 "linear clause applied to non-integral non-pointer "
13494 "variable with type %qT", TREE_TYPE (t
));
13498 if (TYPE_ATOMIC (TREE_TYPE (t
)))
13500 error_at (OMP_CLAUSE_LOCATION (c
),
13501 "%<_Atomic%> %qD in %<linear%> clause", t
);
13505 if (ort
== C_ORT_OMP_DECLARE_SIMD
)
13507 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
13508 if (TREE_CODE (s
) == PARM_DECL
)
13510 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
) = 1;
13511 /* map_head bitmap is used as uniform_head if
13513 if (!bitmap_bit_p (&map_head
, DECL_UID (s
)))
13514 linear_variable_step_check
= true;
13515 goto check_dup_generic
;
13517 if (TREE_CODE (s
) != INTEGER_CST
)
13519 error_at (OMP_CLAUSE_LOCATION (c
),
13520 "%<linear%> clause step %qE is neither constant "
13521 "nor a parameter", s
);
13526 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
13528 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
13529 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
13530 OMP_CLAUSE_DECL (c
), s
);
13531 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13532 sizetype
, fold_convert (sizetype
, s
),
13534 (sizetype
, OMP_CLAUSE_DECL (c
)));
13535 if (s
== error_mark_node
)
13537 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
13540 OMP_CLAUSE_LINEAR_STEP (c
)
13541 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
13542 goto check_dup_generic
;
13545 t
= OMP_CLAUSE_DECL (c
);
13546 check_dup_generic_t
:
13547 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13549 error_at (OMP_CLAUSE_LOCATION (c
),
13550 "%qE is not a variable in clause %qs", t
,
13551 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13554 else if (ort
== C_ORT_ACC
13555 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
13557 if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
13559 error ("%qD appears more than once in reduction clauses", t
);
13563 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
13565 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13566 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
13567 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
13569 error_at (OMP_CLAUSE_LOCATION (c
),
13570 "%qE appears more than once in data clauses", t
);
13573 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13574 && bitmap_bit_p (&map_head
, DECL_UID (t
)))
13576 if (ort
== C_ORT_ACC
)
13577 error ("%qD appears more than once in data clauses", t
);
13579 error ("%qD appears both in data and map clauses", t
);
13583 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13586 case OMP_CLAUSE_FIRSTPRIVATE
:
13587 t
= OMP_CLAUSE_DECL (c
);
13588 need_complete
= true;
13589 need_implicitly_determined
= true;
13590 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13592 error_at (OMP_CLAUSE_LOCATION (c
),
13593 "%qE is not a variable in clause %<firstprivate%>", t
);
13596 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13597 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13599 error_at (OMP_CLAUSE_LOCATION (c
),
13600 "%qE appears more than once in data clauses", t
);
13603 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13605 if (ort
== C_ORT_ACC
)
13606 error ("%qD appears more than once in data clauses", t
);
13608 error ("%qD appears both in data and map clauses", t
);
13612 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
13615 case OMP_CLAUSE_LASTPRIVATE
:
13616 t
= OMP_CLAUSE_DECL (c
);
13617 need_complete
= true;
13618 need_implicitly_determined
= true;
13619 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13621 error_at (OMP_CLAUSE_LOCATION (c
),
13622 "%qE is not a variable in clause %<lastprivate%>", t
);
13625 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13626 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
13628 error_at (OMP_CLAUSE_LOCATION (c
),
13629 "%qE appears more than once in data clauses", t
);
13633 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
13636 case OMP_CLAUSE_ALIGNED
:
13637 t
= OMP_CLAUSE_DECL (c
);
13638 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13640 error_at (OMP_CLAUSE_LOCATION (c
),
13641 "%qE is not a variable in %<aligned%> clause", t
);
13644 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
13645 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
13647 error_at (OMP_CLAUSE_LOCATION (c
),
13648 "%qE in %<aligned%> clause is neither a pointer nor "
13652 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13654 error_at (OMP_CLAUSE_LOCATION (c
),
13655 "%<_Atomic%> %qD in %<aligned%> clause", t
);
13659 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
13661 error_at (OMP_CLAUSE_LOCATION (c
),
13662 "%qE appears more than once in %<aligned%> clauses",
13667 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
13670 case OMP_CLAUSE_DEPEND
:
13671 t
= OMP_CLAUSE_DECL (c
);
13672 if (t
== NULL_TREE
)
13674 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c
)
13675 == OMP_CLAUSE_DEPEND_SOURCE
);
13678 if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
)
13680 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13681 for (; t
; t
= TREE_CHAIN (t
))
13683 tree decl
= TREE_VALUE (t
);
13684 if (TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
13686 tree offset
= TREE_PURPOSE (t
);
13687 bool neg
= wi::neg_p (wi::to_wide (offset
));
13688 offset
= fold_unary (ABS_EXPR
, TREE_TYPE (offset
), offset
);
13689 tree t2
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
),
13690 neg
? MINUS_EXPR
: PLUS_EXPR
,
13692 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13694 fold_convert (sizetype
, t2
),
13695 fold_convert (sizetype
, decl
));
13696 if (t2
== error_mark_node
)
13701 TREE_PURPOSE (t
) = t2
;
13706 if (TREE_CODE (t
) == TREE_LIST
)
13708 if (handle_omp_array_sections (c
, ort
))
13712 if (t
== error_mark_node
)
13714 else if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13716 error_at (OMP_CLAUSE_LOCATION (c
),
13717 "%qE is not a variable in %<depend%> clause", t
);
13720 else if (!c_mark_addressable (t
))
13724 case OMP_CLAUSE_MAP
:
13725 case OMP_CLAUSE_TO
:
13726 case OMP_CLAUSE_FROM
:
13727 case OMP_CLAUSE__CACHE_
:
13728 t
= OMP_CLAUSE_DECL (c
);
13729 if (TREE_CODE (t
) == TREE_LIST
)
13731 if (handle_omp_array_sections (c
, ort
))
13735 t
= OMP_CLAUSE_DECL (c
);
13736 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13738 error_at (OMP_CLAUSE_LOCATION (c
),
13739 "array section does not have mappable type "
13741 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13744 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13746 error_at (OMP_CLAUSE_LOCATION (c
),
13747 "%<_Atomic%> %qE in %qs clause", t
,
13748 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13751 while (TREE_CODE (t
) == ARRAY_REF
)
13752 t
= TREE_OPERAND (t
, 0);
13753 if (TREE_CODE (t
) == COMPONENT_REF
13754 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13756 while (TREE_CODE (t
) == COMPONENT_REF
)
13757 t
= TREE_OPERAND (t
, 0);
13758 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
13760 if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13762 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
13763 error ("%qD appears more than once in motion"
13765 else if (ort
== C_ORT_ACC
)
13766 error ("%qD appears more than once in data"
13769 error ("%qD appears more than once in map"
13775 bitmap_set_bit (&map_head
, DECL_UID (t
));
13776 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
13782 if (t
== error_mark_node
)
13787 if (TREE_CODE (t
) == COMPONENT_REF
13788 && (ort
& C_ORT_OMP
)
13789 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE__CACHE_
)
13791 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
13793 error_at (OMP_CLAUSE_LOCATION (c
),
13794 "bit-field %qE in %qs clause",
13795 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13798 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13800 error_at (OMP_CLAUSE_LOCATION (c
),
13801 "%qE does not have a mappable type in %qs clause",
13802 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13805 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13807 error_at (OMP_CLAUSE_LOCATION (c
),
13808 "%<_Atomic%> %qE in %qs clause", t
,
13809 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13812 while (TREE_CODE (t
) == COMPONENT_REF
)
13814 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0)))
13817 error_at (OMP_CLAUSE_LOCATION (c
),
13818 "%qE is a member of a union", t
);
13822 t
= TREE_OPERAND (t
, 0);
13826 if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
13828 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
13832 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13834 error_at (OMP_CLAUSE_LOCATION (c
),
13835 "%qE is not a variable in %qs clause", t
,
13836 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13839 else if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
13841 error_at (OMP_CLAUSE_LOCATION (c
),
13842 "%qD is threadprivate variable in %qs clause", t
,
13843 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13846 else if ((OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
13847 || (OMP_CLAUSE_MAP_KIND (c
)
13848 != GOMP_MAP_FIRSTPRIVATE_POINTER
))
13849 && !c_mark_addressable (t
))
13851 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13852 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
13853 || (OMP_CLAUSE_MAP_KIND (c
)
13854 == GOMP_MAP_FIRSTPRIVATE_POINTER
)
13855 || (OMP_CLAUSE_MAP_KIND (c
)
13856 == GOMP_MAP_FORCE_DEVICEPTR
)))
13857 && t
== OMP_CLAUSE_DECL (c
)
13858 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13860 error_at (OMP_CLAUSE_LOCATION (c
),
13861 "%qD does not have a mappable type in %qs clause", t
,
13862 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13865 else if (TREE_TYPE (t
) == error_mark_node
)
13867 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
13869 error_at (OMP_CLAUSE_LOCATION (c
),
13870 "%<_Atomic%> %qE in %qs clause", t
,
13871 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13874 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13875 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
13877 if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13878 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13880 error ("%qD appears more than once in data clauses", t
);
13883 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13885 if (ort
== C_ORT_ACC
)
13886 error ("%qD appears more than once in data clauses", t
);
13888 error ("%qD appears both in data and map clauses", t
);
13892 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13894 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13896 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
13897 error ("%qD appears more than once in motion clauses", t
);
13898 else if (ort
== C_ORT_ACC
)
13899 error ("%qD appears more than once in data clauses", t
);
13901 error ("%qD appears more than once in map clauses", t
);
13904 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13905 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13907 if (ort
== C_ORT_ACC
)
13908 error ("%qD appears more than once in data clauses", t
);
13910 error ("%qD appears both in data and map clauses", t
);
13915 bitmap_set_bit (&map_head
, DECL_UID (t
));
13916 if (t
!= OMP_CLAUSE_DECL (c
)
13917 && TREE_CODE (OMP_CLAUSE_DECL (c
)) == COMPONENT_REF
)
13918 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
13922 case OMP_CLAUSE_TO_DECLARE
:
13923 case OMP_CLAUSE_LINK
:
13924 t
= OMP_CLAUSE_DECL (c
);
13925 if (TREE_CODE (t
) == FUNCTION_DECL
13926 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
13928 else if (!VAR_P (t
))
13930 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
13931 error_at (OMP_CLAUSE_LOCATION (c
),
13932 "%qE is neither a variable nor a function name in "
13934 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13936 error_at (OMP_CLAUSE_LOCATION (c
),
13937 "%qE is not a variable in clause %qs", t
,
13938 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13941 else if (DECL_THREAD_LOCAL_P (t
))
13943 error_at (OMP_CLAUSE_LOCATION (c
),
13944 "%qD is threadprivate variable in %qs clause", t
,
13945 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13948 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13950 error_at (OMP_CLAUSE_LOCATION (c
),
13951 "%qD does not have a mappable type in %qs clause", t
,
13952 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13957 if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
13959 error_at (OMP_CLAUSE_LOCATION (c
),
13960 "%qE appears more than once on the same "
13961 "%<declare target%> directive", t
);
13965 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13968 case OMP_CLAUSE_UNIFORM
:
13969 t
= OMP_CLAUSE_DECL (c
);
13970 if (TREE_CODE (t
) != PARM_DECL
)
13973 error_at (OMP_CLAUSE_LOCATION (c
),
13974 "%qD is not an argument in %<uniform%> clause", t
);
13976 error_at (OMP_CLAUSE_LOCATION (c
),
13977 "%qE is not an argument in %<uniform%> clause", t
);
13981 /* map_head bitmap is used as uniform_head if declare_simd. */
13982 bitmap_set_bit (&map_head
, DECL_UID (t
));
13983 goto check_dup_generic
;
13985 case OMP_CLAUSE_IS_DEVICE_PTR
:
13986 case OMP_CLAUSE_USE_DEVICE_PTR
:
13987 t
= OMP_CLAUSE_DECL (c
);
13988 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
13989 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
13991 error_at (OMP_CLAUSE_LOCATION (c
),
13992 "%qs variable is neither a pointer nor an array",
13993 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13996 goto check_dup_generic
;
13998 case OMP_CLAUSE_NOWAIT
:
13999 if (copyprivate_seen
)
14001 error_at (OMP_CLAUSE_LOCATION (c
),
14002 "%<nowait%> clause must not be used together "
14003 "with %<copyprivate%>");
14007 nowait_clause
= pc
;
14008 pc
= &OMP_CLAUSE_CHAIN (c
);
14011 case OMP_CLAUSE_IF
:
14012 case OMP_CLAUSE_NUM_THREADS
:
14013 case OMP_CLAUSE_NUM_TEAMS
:
14014 case OMP_CLAUSE_THREAD_LIMIT
:
14015 case OMP_CLAUSE_DEFAULT
:
14016 case OMP_CLAUSE_UNTIED
:
14017 case OMP_CLAUSE_COLLAPSE
:
14018 case OMP_CLAUSE_FINAL
:
14019 case OMP_CLAUSE_MERGEABLE
:
14020 case OMP_CLAUSE_DEVICE
:
14021 case OMP_CLAUSE_DIST_SCHEDULE
:
14022 case OMP_CLAUSE_PARALLEL
:
14023 case OMP_CLAUSE_FOR
:
14024 case OMP_CLAUSE_SECTIONS
:
14025 case OMP_CLAUSE_TASKGROUP
:
14026 case OMP_CLAUSE_PROC_BIND
:
14027 case OMP_CLAUSE_PRIORITY
:
14028 case OMP_CLAUSE_GRAINSIZE
:
14029 case OMP_CLAUSE_NUM_TASKS
:
14030 case OMP_CLAUSE_NOGROUP
:
14031 case OMP_CLAUSE_THREADS
:
14032 case OMP_CLAUSE_SIMD
:
14033 case OMP_CLAUSE_HINT
:
14034 case OMP_CLAUSE_DEFAULTMAP
:
14035 case OMP_CLAUSE_NUM_GANGS
:
14036 case OMP_CLAUSE_NUM_WORKERS
:
14037 case OMP_CLAUSE_VECTOR_LENGTH
:
14038 case OMP_CLAUSE_ASYNC
:
14039 case OMP_CLAUSE_WAIT
:
14040 case OMP_CLAUSE_AUTO
:
14041 case OMP_CLAUSE_INDEPENDENT
:
14042 case OMP_CLAUSE_SEQ
:
14043 case OMP_CLAUSE_GANG
:
14044 case OMP_CLAUSE_WORKER
:
14045 case OMP_CLAUSE_VECTOR
:
14046 case OMP_CLAUSE_TILE
:
14047 case OMP_CLAUSE_IF_PRESENT
:
14048 case OMP_CLAUSE_FINALIZE
:
14049 pc
= &OMP_CLAUSE_CHAIN (c
);
14052 case OMP_CLAUSE_SCHEDULE
:
14053 if (OMP_CLAUSE_SCHEDULE_KIND (c
) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
)
14055 const char *p
= NULL
;
14056 switch (OMP_CLAUSE_SCHEDULE_KIND (c
) & OMP_CLAUSE_SCHEDULE_MASK
)
14058 case OMP_CLAUSE_SCHEDULE_STATIC
: p
= "static"; break;
14059 case OMP_CLAUSE_SCHEDULE_DYNAMIC
: break;
14060 case OMP_CLAUSE_SCHEDULE_GUIDED
: break;
14061 case OMP_CLAUSE_SCHEDULE_AUTO
: p
= "auto"; break;
14062 case OMP_CLAUSE_SCHEDULE_RUNTIME
: p
= "runtime"; break;
14063 default: gcc_unreachable ();
14067 error_at (OMP_CLAUSE_LOCATION (c
),
14068 "%<nonmonotonic%> modifier specified for %qs "
14069 "schedule kind", p
);
14070 OMP_CLAUSE_SCHEDULE_KIND (c
)
14071 = (enum omp_clause_schedule_kind
)
14072 (OMP_CLAUSE_SCHEDULE_KIND (c
)
14073 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
14076 schedule_clause
= c
;
14077 pc
= &OMP_CLAUSE_CHAIN (c
);
14080 case OMP_CLAUSE_ORDERED
:
14081 ordered_seen
= true;
14082 pc
= &OMP_CLAUSE_CHAIN (c
);
14085 case OMP_CLAUSE_SAFELEN
:
14087 pc
= &OMP_CLAUSE_CHAIN (c
);
14089 case OMP_CLAUSE_SIMDLEN
:
14091 pc
= &OMP_CLAUSE_CHAIN (c
);
14094 case OMP_CLAUSE_INBRANCH
:
14095 case OMP_CLAUSE_NOTINBRANCH
:
14098 error_at (OMP_CLAUSE_LOCATION (c
),
14099 "%<inbranch%> clause is incompatible with "
14100 "%<notinbranch%>");
14104 branch_seen
= true;
14105 pc
= &OMP_CLAUSE_CHAIN (c
);
14109 gcc_unreachable ();
14114 t
= OMP_CLAUSE_DECL (c
);
14118 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
14119 if (t
== error_mark_node
)
14123 if (need_implicitly_determined
)
14125 const char *share_name
= NULL
;
14127 if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
14128 share_name
= "threadprivate";
14129 else switch (c_omp_predetermined_sharing (t
))
14131 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
14133 case OMP_CLAUSE_DEFAULT_SHARED
:
14134 /* const vars may be specified in firstprivate clause. */
14135 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
14136 && TREE_READONLY (t
))
14138 share_name
= "shared";
14140 case OMP_CLAUSE_DEFAULT_PRIVATE
:
14141 share_name
= "private";
14144 gcc_unreachable ();
14148 error_at (OMP_CLAUSE_LOCATION (c
),
14149 "%qE is predetermined %qs for %qs",
14151 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
14158 *pc
= OMP_CLAUSE_CHAIN (c
);
14160 pc
= &OMP_CLAUSE_CHAIN (c
);
14165 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen
),
14166 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)))
14168 error_at (OMP_CLAUSE_LOCATION (simdlen
),
14169 "%<simdlen%> clause value is bigger than "
14170 "%<safelen%> clause value");
14171 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)
14172 = OMP_CLAUSE_SAFELEN_EXPR (safelen
);
14177 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
14178 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
14180 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
14181 "%<nonmonotonic%> schedule modifier specified together "
14182 "with %<ordered%> clause");
14183 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
14184 = (enum omp_clause_schedule_kind
)
14185 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
14186 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
14189 if (linear_variable_step_check
)
14190 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
14192 bool remove
= false;
14193 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
14194 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
)
14195 && !bitmap_bit_p (&map_head
,
14196 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c
))))
14198 error_at (OMP_CLAUSE_LOCATION (c
),
14199 "%<linear%> clause step is a parameter %qD not "
14200 "specified in %<uniform%> clause",
14201 OMP_CLAUSE_LINEAR_STEP (c
));
14206 *pc
= OMP_CLAUSE_CHAIN (c
);
14208 pc
= &OMP_CLAUSE_CHAIN (c
);
14211 bitmap_obstack_release (NULL
);
14215 /* Return code to initialize DST with a copy constructor from SRC.
14216 C doesn't have copy constructors nor assignment operators, only for
14217 _Atomic vars we need to perform __atomic_load from src into a temporary
14218 followed by __atomic_store of the temporary to dst. */
14221 c_omp_clause_copy_ctor (tree clause
, tree dst
, tree src
)
14223 if (!really_atomic_lvalue (dst
) && !really_atomic_lvalue (src
))
14224 return build2 (MODIFY_EXPR
, TREE_TYPE (dst
), dst
, src
);
14226 location_t loc
= OMP_CLAUSE_LOCATION (clause
);
14227 tree type
= TREE_TYPE (dst
);
14228 tree nonatomic_type
= build_qualified_type (type
, TYPE_UNQUALIFIED
);
14229 tree tmp
= create_tmp_var (nonatomic_type
);
14230 tree tmp_addr
= build_fold_addr_expr (tmp
);
14231 TREE_ADDRESSABLE (tmp
) = 1;
14232 TREE_NO_WARNING (tmp
) = 1;
14233 tree src_addr
= build_fold_addr_expr (src
);
14234 tree dst_addr
= build_fold_addr_expr (dst
);
14235 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
14236 vec
<tree
, va_gc
> *params
;
14237 /* Expansion of a generic atomic load may require an addition
14238 element, so allocate enough to prevent a resize. */
14239 vec_alloc (params
, 4);
14241 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14242 tree fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
14243 params
->quick_push (src_addr
);
14244 params
->quick_push (tmp_addr
);
14245 params
->quick_push (seq_cst
);
14246 tree load
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
14248 vec_alloc (params
, 4);
14250 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14251 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
14252 params
->quick_push (dst_addr
);
14253 params
->quick_push (tmp_addr
);
14254 params
->quick_push (seq_cst
);
14255 tree store
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
14256 return build2 (COMPOUND_EXPR
, void_type_node
, load
, store
);
14259 /* Create a transaction node. */
14262 c_finish_transaction (location_t loc
, tree block
, int flags
)
14264 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
14265 if (flags
& TM_STMT_ATTR_OUTER
)
14266 TRANSACTION_EXPR_OUTER (stmt
) = 1;
14267 if (flags
& TM_STMT_ATTR_RELAXED
)
14268 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
14269 return add_stmt (stmt
);
14272 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14273 down to the element type of an array. If ORIG_QUAL_TYPE is not
14274 NULL, then it should be used as the qualified type
14275 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14276 preserve information about the typedef name from which an array
14277 type was derived). */
14280 c_build_qualified_type (tree type
, int type_quals
, tree orig_qual_type
,
14281 size_t orig_qual_indirect
)
14283 if (type
== error_mark_node
)
14286 if (TREE_CODE (type
) == ARRAY_TYPE
)
14289 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
14290 type_quals
, orig_qual_type
,
14291 orig_qual_indirect
- 1);
14293 /* See if we already have an identically qualified type. */
14294 if (orig_qual_type
&& orig_qual_indirect
== 0)
14295 t
= orig_qual_type
;
14297 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
14299 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
14300 && TYPE_NAME (t
) == TYPE_NAME (type
)
14301 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
14302 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
14303 TYPE_ATTRIBUTES (type
)))
14308 tree domain
= TYPE_DOMAIN (type
);
14310 t
= build_variant_type_copy (type
);
14311 TREE_TYPE (t
) = element_type
;
14313 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
14314 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
14315 SET_TYPE_STRUCTURAL_EQUALITY (t
);
14316 else if (TYPE_CANONICAL (element_type
) != element_type
14317 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
14319 tree unqualified_canon
14320 = build_array_type (TYPE_CANONICAL (element_type
),
14321 domain
? TYPE_CANONICAL (domain
)
14323 if (TYPE_REVERSE_STORAGE_ORDER (type
))
14326 = build_distinct_type_copy (unqualified_canon
);
14327 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon
) = 1;
14330 = c_build_qualified_type (unqualified_canon
, type_quals
);
14333 TYPE_CANONICAL (t
) = t
;
14338 /* A restrict-qualified pointer type must be a pointer to object or
14339 incomplete type. Note that the use of POINTER_TYPE_P also allows
14340 REFERENCE_TYPEs, which is appropriate for C++. */
14341 if ((type_quals
& TYPE_QUAL_RESTRICT
)
14342 && (!POINTER_TYPE_P (type
)
14343 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
14345 error ("invalid use of %<restrict%>");
14346 type_quals
&= ~TYPE_QUAL_RESTRICT
;
14349 tree var_type
= (orig_qual_type
&& orig_qual_indirect
== 0
14351 : build_qualified_type (type
, type_quals
));
14352 /* A variant type does not inherit the list of incomplete vars from the
14353 type main variant. */
14354 if (RECORD_OR_UNION_TYPE_P (var_type
)
14355 && TYPE_MAIN_VARIANT (var_type
) != var_type
)
14356 C_TYPE_INCOMPLETE_VARS (var_type
) = 0;
14360 /* Build a VA_ARG_EXPR for the C parser. */
14363 c_build_va_arg (location_t loc1
, tree expr
, location_t loc2
, tree type
)
14365 if (error_operand_p (type
))
14366 return error_mark_node
;
14367 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14368 order because it takes the address of the expression. */
14369 else if (handled_component_p (expr
)
14370 && reverse_storage_order_for_component_p (expr
))
14372 error_at (loc1
, "cannot use %<va_arg%> with reverse storage order");
14373 return error_mark_node
;
14375 else if (!COMPLETE_TYPE_P (type
))
14377 error_at (loc2
, "second argument to %<va_arg%> is of incomplete "
14379 return error_mark_node
;
14381 else if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
14382 warning_at (loc2
, OPT_Wc___compat
,
14383 "C++ requires promoted type, not enum type, in %<va_arg%>");
14384 return build_va_arg (loc2
, expr
, type
);
14387 /* Return truthvalue of whether T1 is the same tree structure as T2.
14388 Return 1 if they are the same. Return false if they are different. */
14391 c_tree_equal (tree t1
, tree t2
)
14393 enum tree_code code1
, code2
;
14400 for (code1
= TREE_CODE (t1
);
14401 CONVERT_EXPR_CODE_P (code1
)
14402 || code1
== NON_LVALUE_EXPR
;
14403 code1
= TREE_CODE (t1
))
14404 t1
= TREE_OPERAND (t1
, 0);
14405 for (code2
= TREE_CODE (t2
);
14406 CONVERT_EXPR_CODE_P (code2
)
14407 || code2
== NON_LVALUE_EXPR
;
14408 code2
= TREE_CODE (t2
))
14409 t2
= TREE_OPERAND (t2
, 0);
14411 /* They might have become equal now. */
14415 if (code1
!= code2
)
14421 return wi::to_wide (t1
) == wi::to_wide (t2
);
14424 return real_equal (&TREE_REAL_CST (t1
), &TREE_REAL_CST (t2
));
14427 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
14428 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
14429 TREE_STRING_LENGTH (t1
));
14432 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
14433 TREE_FIXED_CST (t2
));
14436 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
14437 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
14440 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
14443 /* We need to do this when determining whether or not two
14444 non-type pointer to member function template arguments
14446 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
14447 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
14452 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
14454 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
14455 if (!c_tree_equal (field
, elt2
->index
)
14456 || !c_tree_equal (value
, elt2
->value
))
14463 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
14465 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
14467 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
14470 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
14475 call_expr_arg_iterator iter1
, iter2
;
14476 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
14478 for (arg1
= first_call_expr_arg (t1
, &iter1
),
14479 arg2
= first_call_expr_arg (t2
, &iter2
);
14481 arg1
= next_call_expr_arg (&iter1
),
14482 arg2
= next_call_expr_arg (&iter2
))
14483 if (!c_tree_equal (arg1
, arg2
))
14492 tree o1
= TREE_OPERAND (t1
, 0);
14493 tree o2
= TREE_OPERAND (t2
, 0);
14495 /* Special case: if either target is an unallocated VAR_DECL,
14496 it means that it's going to be unified with whatever the
14497 TARGET_EXPR is really supposed to initialize, so treat it
14498 as being equivalent to anything. */
14499 if (VAR_P (o1
) && DECL_NAME (o1
) == NULL_TREE
14500 && !DECL_RTL_SET_P (o1
))
14502 else if (VAR_P (o2
) && DECL_NAME (o2
) == NULL_TREE
14503 && !DECL_RTL_SET_P (o2
))
14505 else if (!c_tree_equal (o1
, o2
))
14508 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
14511 case COMPONENT_REF
:
14512 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
14514 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
14520 case FUNCTION_DECL
:
14521 case IDENTIFIER_NODE
:
14528 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
14530 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
14531 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
14532 TREE_VEC_ELT (t2
, ix
)))
14541 switch (TREE_CODE_CLASS (code1
))
14545 case tcc_comparison
:
14546 case tcc_expression
:
14548 case tcc_reference
:
14549 case tcc_statement
:
14551 int i
, n
= TREE_OPERAND_LENGTH (t1
);
14555 case PREINCREMENT_EXPR
:
14556 case PREDECREMENT_EXPR
:
14557 case POSTINCREMENT_EXPR
:
14558 case POSTDECREMENT_EXPR
:
14568 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
14569 && n
!= TREE_OPERAND_LENGTH (t2
))
14572 for (i
= 0; i
< n
; ++i
)
14573 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
14580 return comptypes (t1
, t2
);
14582 gcc_unreachable ();
14584 /* We can get here with --disable-checking. */
14588 /* Returns true when the function declaration FNDECL is implicit,
14589 introduced as a result of a call to an otherwise undeclared
14590 function, and false otherwise. */
14593 c_decl_implicit (const_tree fndecl
)
14595 return C_DECL_IMPLICIT (fndecl
);