1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2016 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"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
50 #include "gomp-constants.h"
51 #include "spellcheck-tree.h"
52 #include "gcc-rich-location.h"
54 /* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
63 /* The level of nesting inside "__alignof__". */
66 /* The level of nesting inside "sizeof". */
69 /* The level of nesting inside "typeof". */
72 /* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74 tree c_last_sizeof_arg
;
76 /* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78 static int found_missing_braces
;
80 static int require_constant_value
;
81 static int require_constant_elements
;
83 static bool null_pointer_constant_p (const_tree
);
84 static tree
qualify_type (tree
, tree
);
85 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
87 static int comp_target_types (location_t
, tree
, tree
);
88 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
90 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
91 static tree
lookup_field (tree
, tree
);
92 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
93 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
95 static tree
pointer_diff (location_t
, tree
, tree
);
96 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
97 enum impl_conv
, bool, tree
, tree
, int);
98 static tree
valid_compound_expr_initializer (tree
, tree
);
99 static void push_string (const char *);
100 static void push_member_name (tree
);
101 static int spelling_length (void);
102 static char *print_spelling (char *);
103 static void warning_init (location_t
, int, const char *);
104 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
105 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, int,
106 bool, struct obstack
*);
107 static void output_pending_init_elements (int, struct obstack
*);
108 static int set_designator (location_t
, int, struct obstack
*);
109 static void push_range_stack (tree
, struct obstack
*);
110 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
112 static void set_nonincremental_init (struct obstack
*);
113 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
114 static tree
find_init_member (tree
, struct obstack
*);
115 static void readonly_warning (tree
, enum lvalue_use
);
116 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
117 static void record_maybe_used_decl (tree
);
118 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
120 /* Return true if EXP is a null pointer constant, false otherwise. */
123 null_pointer_constant_p (const_tree expr
)
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type
= TREE_TYPE (expr
);
128 return (TREE_CODE (expr
) == INTEGER_CST
129 && !TREE_OVERFLOW (expr
)
130 && integer_zerop (expr
)
131 && (INTEGRAL_TYPE_P (type
)
132 || (TREE_CODE (type
) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type
))
134 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
137 /* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
143 note_integer_operands (tree expr
)
146 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
148 ret
= copy_node (expr
);
149 TREE_OVERFLOW (ret
) = 1;
153 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
159 /* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
165 remove_c_maybe_const_expr (tree expr
)
167 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
168 return C_MAYBE_CONST_EXPR_EXPR (expr
);
173 \f/* This is a cache to hold if two types are compatible or not. */
175 struct tagged_tu_seen_cache
{
176 const struct tagged_tu_seen_cache
* next
;
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
184 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
185 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
187 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.)
189 LOC is the location of the use. */
192 require_complete_type (location_t loc
, tree value
)
194 tree type
= TREE_TYPE (value
);
196 if (error_operand_p (value
))
197 return error_mark_node
;
199 /* First, detect a valid value with a complete type. */
200 if (COMPLETE_TYPE_P (type
))
203 c_incomplete_type_error (loc
, value
, type
);
204 return error_mark_node
;
207 /* Print an error message for invalid use of an incomplete type.
208 VALUE is the expression that was used (or 0 if that isn't known)
209 and TYPE is the type that was invalid. LOC is the location for
213 c_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type
) == ERROR_MARK
)
219 if (value
!= 0 && (VAR_P (value
) || TREE_CODE (value
) == PARM_DECL
))
220 error_at (loc
, "%qD has an incomplete type %qT", value
, type
);
224 /* We must print an error message. Be clever about what it says. */
226 switch (TREE_CODE (type
))
234 error_at (loc
, "invalid use of void expression");
238 if (TYPE_DOMAIN (type
))
240 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
242 error_at (loc
, "invalid use of flexible array member");
245 type
= TREE_TYPE (type
);
248 error_at (loc
, "invalid use of array with unspecified bounds");
255 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
256 error_at (loc
, "invalid use of undefined type %qT", type
);
258 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
259 error_at (loc
, "invalid use of incomplete typedef %qT", type
);
263 /* Given a type, apply default promotions wrt unnamed function
264 arguments and return the new type. */
267 c_type_promotes_to (tree type
)
269 tree ret
= NULL_TREE
;
271 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
272 ret
= double_type_node
;
273 else if (c_promoting_integer_type_p (type
))
275 /* Preserve unsignedness if not really getting any wider. */
276 if (TYPE_UNSIGNED (type
)
277 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
278 ret
= unsigned_type_node
;
280 ret
= integer_type_node
;
283 if (ret
!= NULL_TREE
)
284 return (TYPE_ATOMIC (type
)
285 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
291 /* Return true if between two named address spaces, whether there is a superset
292 named address space that encompasses both address spaces. If there is a
293 superset, return which address space is the superset. */
296 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
303 else if (targetm
.addr_space
.subset_p (as1
, as2
))
308 else if (targetm
.addr_space
.subset_p (as2
, as1
))
317 /* Return a variant of TYPE which has all the type qualifiers of LIKE
318 as well as those of TYPE. */
321 qualify_type (tree type
, tree like
)
323 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
324 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
325 addr_space_t as_common
;
327 /* If the two named address spaces are different, determine the common
328 superset address space. If there isn't one, raise an error. */
329 if (!addr_space_superset (as_type
, as_like
, &as_common
))
332 error ("%qT and %qT are in disjoint named address spaces",
336 return c_build_qualified_type (type
,
337 TYPE_QUALS_NO_ADDR_SPACE (type
)
338 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
339 | ENCODE_QUAL_ADDR_SPACE (as_common
));
342 /* Return true iff the given tree T is a variable length array. */
345 c_vla_type_p (const_tree t
)
347 if (TREE_CODE (t
) == ARRAY_TYPE
348 && C_TYPE_VARIABLE_SIZE (t
))
353 /* Return the composite type of two compatible types.
355 We assume that comptypes has already been done and returned
356 nonzero; if that isn't so, this may crash. In particular, we
357 assume that qualifiers match. */
360 composite_type (tree t1
, tree t2
)
362 enum tree_code code1
;
363 enum tree_code code2
;
366 /* Save time if the two types are the same. */
368 if (t1
== t2
) return t1
;
370 /* If one type is nonsense, use the other. */
371 if (t1
== error_mark_node
)
373 if (t2
== error_mark_node
)
376 code1
= TREE_CODE (t1
);
377 code2
= TREE_CODE (t2
);
379 /* Merge the attributes. */
380 attributes
= targetm
.merge_type_attributes (t1
, t2
);
382 /* If one is an enumerated type and the other is the compatible
383 integer type, the composite type might be either of the two
384 (DR#013 question 3). For consistency, use the enumerated type as
385 the composite type. */
387 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
389 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
392 gcc_assert (code1
== code2
);
397 /* For two pointers, do this recursively on the target type. */
399 tree pointed_to_1
= TREE_TYPE (t1
);
400 tree pointed_to_2
= TREE_TYPE (t2
);
401 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
402 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
403 t1
= build_type_attribute_variant (t1
, attributes
);
404 return qualify_type (t1
, t2
);
409 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
412 tree d1
= TYPE_DOMAIN (t1
);
413 tree d2
= TYPE_DOMAIN (t2
);
414 bool d1_variable
, d2_variable
;
415 bool d1_zero
, d2_zero
;
416 bool t1_complete
, t2_complete
;
418 /* We should not have any type quals on arrays at all. */
419 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
420 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
422 t1_complete
= COMPLETE_TYPE_P (t1
);
423 t2_complete
= COMPLETE_TYPE_P (t2
);
425 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
426 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
428 d1_variable
= (!d1_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
431 d2_variable
= (!d2_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
434 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
435 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
437 /* Save space: see if the result is identical to one of the args. */
438 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
439 && (d2_variable
|| d2_zero
|| !d1_variable
))
440 return build_type_attribute_variant (t1
, attributes
);
441 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
442 && (d1_variable
|| d1_zero
|| !d2_variable
))
443 return build_type_attribute_variant (t2
, attributes
);
445 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
446 return build_type_attribute_variant (t1
, attributes
);
447 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
448 return build_type_attribute_variant (t2
, attributes
);
450 /* Merge the element types, and have a size if either arg has
451 one. We may have qualifiers on the element types. To set
452 up TYPE_MAIN_VARIANT correctly, we need to form the
453 composite of the unqualified types and add the qualifiers
455 quals
= TYPE_QUALS (strip_array_types (elt
));
456 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
457 t1
= build_array_type (unqual_elt
,
458 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
464 /* Ensure a composite type involving a zero-length array type
465 is a zero-length type not an incomplete type. */
466 if (d1_zero
&& d2_zero
467 && (t1_complete
|| t2_complete
)
468 && !COMPLETE_TYPE_P (t1
))
470 TYPE_SIZE (t1
) = bitsize_zero_node
;
471 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
473 t1
= c_build_qualified_type (t1
, quals
);
474 return build_type_attribute_variant (t1
, attributes
);
480 if (attributes
!= NULL
)
482 /* Try harder not to create a new aggregate type. */
483 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
488 return build_type_attribute_variant (t1
, attributes
);
491 /* Function types: prefer the one that specified arg types.
492 If both do, merge the arg types. Also merge the return types. */
494 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
495 tree p1
= TYPE_ARG_TYPES (t1
);
496 tree p2
= TYPE_ARG_TYPES (t2
);
501 /* Save space: see if the result is identical to one of the args. */
502 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
503 return build_type_attribute_variant (t1
, attributes
);
504 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
505 return build_type_attribute_variant (t2
, attributes
);
507 /* Simple way if one arg fails to specify argument types. */
508 if (TYPE_ARG_TYPES (t1
) == 0)
510 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
511 t1
= build_type_attribute_variant (t1
, attributes
);
512 return qualify_type (t1
, t2
);
514 if (TYPE_ARG_TYPES (t2
) == 0)
516 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
517 t1
= build_type_attribute_variant (t1
, attributes
);
518 return qualify_type (t1
, t2
);
521 /* If both args specify argument types, we must merge the two
522 lists, argument by argument. */
524 for (len
= 0, newargs
= p1
;
525 newargs
&& newargs
!= void_list_node
;
526 len
++, newargs
= TREE_CHAIN (newargs
))
529 for (i
= 0; i
< len
; i
++)
530 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
534 for (; p1
&& p1
!= void_list_node
;
535 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
537 /* A null type means arg type is not specified.
538 Take whatever the other function type has. */
539 if (TREE_VALUE (p1
) == 0)
541 TREE_VALUE (n
) = TREE_VALUE (p2
);
544 if (TREE_VALUE (p2
) == 0)
546 TREE_VALUE (n
) = TREE_VALUE (p1
);
550 /* Given wait (union {union wait *u; int *i} *)
551 and wait (union wait *),
552 prefer union wait * as type of parm. */
553 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
554 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
557 tree mv2
= TREE_VALUE (p2
);
558 if (mv2
&& mv2
!= error_mark_node
559 && TREE_CODE (mv2
) != ARRAY_TYPE
)
560 mv2
= TYPE_MAIN_VARIANT (mv2
);
561 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
562 memb
; memb
= DECL_CHAIN (memb
))
564 tree mv3
= TREE_TYPE (memb
);
565 if (mv3
&& mv3
!= error_mark_node
566 && TREE_CODE (mv3
) != ARRAY_TYPE
)
567 mv3
= TYPE_MAIN_VARIANT (mv3
);
568 if (comptypes (mv3
, mv2
))
570 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
572 pedwarn (input_location
, OPT_Wpedantic
,
573 "function types not truly compatible in ISO C");
578 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
579 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
582 tree mv1
= TREE_VALUE (p1
);
583 if (mv1
&& mv1
!= error_mark_node
584 && TREE_CODE (mv1
) != ARRAY_TYPE
)
585 mv1
= TYPE_MAIN_VARIANT (mv1
);
586 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
587 memb
; memb
= DECL_CHAIN (memb
))
589 tree mv3
= TREE_TYPE (memb
);
590 if (mv3
&& mv3
!= error_mark_node
591 && TREE_CODE (mv3
) != ARRAY_TYPE
)
592 mv3
= TYPE_MAIN_VARIANT (mv3
);
593 if (comptypes (mv3
, mv1
))
595 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
597 pedwarn (input_location
, OPT_Wpedantic
,
598 "function types not truly compatible in ISO C");
603 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
607 t1
= build_function_type (valtype
, newargs
);
608 t1
= qualify_type (t1
, t2
);
613 return build_type_attribute_variant (t1
, attributes
);
618 /* Return the type of a conditional expression between pointers to
619 possibly differently qualified versions of compatible types.
621 We assume that comp_target_types has already been done and returned
622 nonzero; if that isn't so, this may crash. */
625 common_pointer_type (tree t1
, tree t2
)
628 tree pointed_to_1
, mv1
;
629 tree pointed_to_2
, mv2
;
631 unsigned target_quals
;
632 addr_space_t as1
, as2
, as_common
;
635 /* Save time if the two types are the same. */
637 if (t1
== t2
) return t1
;
639 /* If one type is nonsense, use the other. */
640 if (t1
== error_mark_node
)
642 if (t2
== error_mark_node
)
645 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
646 && TREE_CODE (t2
) == POINTER_TYPE
);
648 /* Merge the attributes. */
649 attributes
= targetm
.merge_type_attributes (t1
, t2
);
651 /* Find the composite type of the target types, and combine the
652 qualifiers of the two types' targets. Do not lose qualifiers on
653 array element types by taking the TYPE_MAIN_VARIANT. */
654 mv1
= pointed_to_1
= TREE_TYPE (t1
);
655 mv2
= pointed_to_2
= TREE_TYPE (t2
);
656 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
657 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
658 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
659 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
660 target
= composite_type (mv1
, mv2
);
662 /* Strip array types to get correct qualifier for pointers to arrays */
663 quals1
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1
));
664 quals2
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2
));
666 /* For function types do not merge const qualifiers, but drop them
667 if used inconsistently. The middle-end uses these to mark const
668 and noreturn functions. */
669 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
670 target_quals
= (quals1
& quals2
);
672 target_quals
= (quals1
| quals2
);
674 /* If the two named address spaces are different, determine the common
675 superset address space. This is guaranteed to exist due to the
676 assumption that comp_target_type returned non-zero. */
677 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
678 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
679 if (!addr_space_superset (as1
, as2
, &as_common
))
682 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
684 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
685 return build_type_attribute_variant (t1
, attributes
);
688 /* Return the common type for two arithmetic types under the usual
689 arithmetic conversions. The default conversions have already been
690 applied, and enumerated types converted to their compatible integer
691 types. The resulting type is unqualified and has no attributes.
693 This is the type for the result of most arithmetic operations
694 if the operands have the given two types. */
697 c_common_type (tree t1
, tree t2
)
699 enum tree_code code1
;
700 enum tree_code code2
;
702 /* If one type is nonsense, use the other. */
703 if (t1
== error_mark_node
)
705 if (t2
== error_mark_node
)
708 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
709 t1
= TYPE_MAIN_VARIANT (t1
);
711 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
712 t2
= TYPE_MAIN_VARIANT (t2
);
714 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
715 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
717 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
718 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
720 /* Save time if the two types are the same. */
722 if (t1
== t2
) return t1
;
724 code1
= TREE_CODE (t1
);
725 code2
= TREE_CODE (t2
);
727 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
728 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
729 || code1
== INTEGER_TYPE
);
730 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
731 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
732 || code2
== INTEGER_TYPE
);
734 /* When one operand is a decimal float type, the other operand cannot be
735 a generic float type or a complex type. We also disallow vector types
737 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
738 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
740 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
742 error ("can%'t mix operands of decimal float and vector types");
743 return error_mark_node
;
745 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
747 error ("can%'t mix operands of decimal float and complex types");
748 return error_mark_node
;
750 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
752 error ("can%'t mix operands of decimal float and other float types");
753 return error_mark_node
;
757 /* If one type is a vector type, return that type. (How the usual
758 arithmetic conversions apply to the vector types extension is not
759 precisely specified.) */
760 if (code1
== VECTOR_TYPE
)
763 if (code2
== VECTOR_TYPE
)
766 /* If one type is complex, form the common type of the non-complex
767 components, then make that complex. Use T1 or T2 if it is the
769 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
771 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
772 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
773 tree subtype
= c_common_type (subtype1
, subtype2
);
775 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
777 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
780 return build_complex_type (subtype
);
783 /* If only one is real, use it as the result. */
785 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
788 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
791 /* If both are real and either are decimal floating point types, use
792 the decimal floating point type with the greater precision. */
794 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
796 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
797 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
798 return dfloat128_type_node
;
799 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
800 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
801 return dfloat64_type_node
;
802 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
803 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
804 return dfloat32_type_node
;
807 /* Deal with fixed-point types. */
808 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
810 unsigned int unsignedp
= 0, satp
= 0;
812 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
817 /* If one input type is saturating, the result type is saturating. */
818 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
821 /* If both fixed-point types are unsigned, the result type is unsigned.
822 When mixing fixed-point and integer types, follow the sign of the
824 Otherwise, the result type is signed. */
825 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
826 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
827 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t1
))
829 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t2
)))
833 /* The result type is signed. */
836 /* If the input type is unsigned, we need to convert to the
838 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
840 enum mode_class mclass
= (enum mode_class
) 0;
841 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
843 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
847 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
849 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
851 enum mode_class mclass
= (enum mode_class
) 0;
852 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
854 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
858 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
862 if (code1
== FIXED_POINT_TYPE
)
864 fbit1
= GET_MODE_FBIT (m1
);
865 ibit1
= GET_MODE_IBIT (m1
);
870 /* Signed integers need to subtract one sign bit. */
871 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
874 if (code2
== FIXED_POINT_TYPE
)
876 fbit2
= GET_MODE_FBIT (m2
);
877 ibit2
= GET_MODE_IBIT (m2
);
882 /* Signed integers need to subtract one sign bit. */
883 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
886 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
887 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
888 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
892 /* Both real or both integers; use the one with greater precision. */
894 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
896 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
899 /* Same precision. Prefer long longs to longs to ints when the
900 same precision, following the C99 rules on integer type rank
901 (which are equivalent to the C90 rules for C90 types). */
903 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
904 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
905 return long_long_unsigned_type_node
;
907 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
908 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
910 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
911 return long_long_unsigned_type_node
;
913 return long_long_integer_type_node
;
916 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
918 return long_unsigned_type_node
;
920 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
923 /* But preserve unsignedness from the other type,
924 since long cannot hold all the values of an unsigned int. */
925 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
926 return long_unsigned_type_node
;
928 return long_integer_type_node
;
931 /* For floating types of the same TYPE_PRECISION (which we here
932 assume means either the same set of values, or sets of values
933 neither a subset of the other, with behavior being undefined in
934 the latter case), follow the rules from TS 18661-3: prefer
935 interchange types _FloatN, then standard types long double,
936 double, float, then extended types _FloatNx. For extended types,
937 check them starting with _Float128x as that seems most consistent
938 in spirit with preferring long double to double; for interchange
939 types, also check in that order for consistency although it's not
940 possible for more than one of them to have the same
942 tree mv1
= TYPE_MAIN_VARIANT (t1
);
943 tree mv2
= TYPE_MAIN_VARIANT (t2
);
945 for (int i
= NUM_FLOATN_TYPES
- 1; i
>= 0; i
--)
946 if (mv1
== FLOATN_TYPE_NODE (i
) || mv2
== FLOATN_TYPE_NODE (i
))
947 return FLOATN_TYPE_NODE (i
);
949 /* Likewise, prefer long double to double even if same size. */
950 if (mv1
== long_double_type_node
|| mv2
== long_double_type_node
)
951 return long_double_type_node
;
953 /* Likewise, prefer double to float even if same size.
954 We got a couple of embedded targets with 32 bit doubles, and the
955 pdp11 might have 64 bit floats. */
956 if (mv1
== double_type_node
|| mv2
== double_type_node
)
957 return double_type_node
;
959 if (mv1
== float_type_node
|| mv2
== float_type_node
)
960 return float_type_node
;
962 for (int i
= NUM_FLOATNX_TYPES
- 1; i
>= 0; i
--)
963 if (mv1
== FLOATNX_TYPE_NODE (i
) || mv2
== FLOATNX_TYPE_NODE (i
))
964 return FLOATNX_TYPE_NODE (i
);
966 /* Otherwise prefer the unsigned one. */
968 if (TYPE_UNSIGNED (t1
))
974 /* Wrapper around c_common_type that is used by c-common.c and other
975 front end optimizations that remove promotions. ENUMERAL_TYPEs
976 are allowed here and are converted to their compatible integer types.
977 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
978 preferably a non-Boolean type as the common type. */
980 common_type (tree t1
, tree t2
)
982 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
983 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
984 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
985 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
987 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
988 if (TREE_CODE (t1
) == BOOLEAN_TYPE
989 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
990 return boolean_type_node
;
992 /* If either type is BOOLEAN_TYPE, then return the other. */
993 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
995 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
998 return c_common_type (t1
, t2
);
1001 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1002 or various other operations. Return 2 if they are compatible
1003 but a warning may be needed if you use them together. */
1006 comptypes (tree type1
, tree type2
)
1008 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1011 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
1012 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1017 /* Like comptypes, but if it returns non-zero because enum and int are
1018 compatible, it sets *ENUM_AND_INT_P to true. */
1021 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1023 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1026 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1027 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1032 /* Like comptypes, but if it returns nonzero for different types, it
1033 sets *DIFFERENT_TYPES_P to true. */
1036 comptypes_check_different_types (tree type1
, tree type2
,
1037 bool *different_types_p
)
1039 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1042 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1043 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1048 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1049 or various other operations. Return 2 if they are compatible
1050 but a warning may be needed if you use them together. If
1051 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1052 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1053 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1054 NULL, and the types are compatible but different enough not to be
1055 permitted in C11 typedef redeclarations, then this sets
1056 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1057 false, but may or may not be set if the types are incompatible.
1058 This differs from comptypes, in that we don't free the seen
1062 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1063 bool *different_types_p
)
1065 const_tree t1
= type1
;
1066 const_tree t2
= type2
;
1069 /* Suppress errors caused by previously reported errors. */
1071 if (t1
== t2
|| !t1
|| !t2
1072 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1075 /* Enumerated types are compatible with integer types, but this is
1076 not transitive: two enumerated types in the same translation unit
1077 are compatible with each other only if they are the same type. */
1079 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1081 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1082 if (TREE_CODE (t2
) != VOID_TYPE
)
1084 if (enum_and_int_p
!= NULL
)
1085 *enum_and_int_p
= true;
1086 if (different_types_p
!= NULL
)
1087 *different_types_p
= true;
1090 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1092 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1093 if (TREE_CODE (t1
) != VOID_TYPE
)
1095 if (enum_and_int_p
!= NULL
)
1096 *enum_and_int_p
= true;
1097 if (different_types_p
!= NULL
)
1098 *different_types_p
= true;
1105 /* Different classes of types can't be compatible. */
1107 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1110 /* Qualifiers must match. C99 6.7.3p9 */
1112 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1115 /* Allow for two different type nodes which have essentially the same
1116 definition. Note that we already checked for equality of the type
1117 qualifiers (just above). */
1119 if (TREE_CODE (t1
) != ARRAY_TYPE
1120 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1123 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1124 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1127 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1130 switch (TREE_CODE (t1
))
1133 case FIXED_POINT_TYPE
:
1135 /* With these nodes, we can't determine type equivalence by
1136 looking at what is stored in the nodes themselves, because
1137 two nodes might have different TYPE_MAIN_VARIANTs but still
1138 represent the same type. For example, wchar_t and int could
1139 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1140 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1141 and are distinct types. On the other hand, int and the
1144 typedef int INT __attribute((may_alias));
1146 have identical properties, different TYPE_MAIN_VARIANTs, but
1147 represent the same type. The canonical type system keeps
1148 track of equivalence in this case, so we fall back on it. */
1149 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1152 /* Do not remove mode information. */
1153 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
1155 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1156 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1157 enum_and_int_p
, different_types_p
));
1161 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1167 tree d1
= TYPE_DOMAIN (t1
);
1168 tree d2
= TYPE_DOMAIN (t2
);
1169 bool d1_variable
, d2_variable
;
1170 bool d1_zero
, d2_zero
;
1173 /* Target types must match incl. qualifiers. */
1174 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1175 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1177 different_types_p
)))
1180 if (different_types_p
!= NULL
1181 && (d1
== 0) != (d2
== 0))
1182 *different_types_p
= true;
1183 /* Sizes must match unless one is missing or variable. */
1184 if (d1
== 0 || d2
== 0 || d1
== d2
)
1187 d1_zero
= !TYPE_MAX_VALUE (d1
);
1188 d2_zero
= !TYPE_MAX_VALUE (d2
);
1190 d1_variable
= (!d1_zero
1191 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1192 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1193 d2_variable
= (!d2_zero
1194 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1195 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1196 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1197 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1199 if (different_types_p
!= NULL
1200 && d1_variable
!= d2_variable
)
1201 *different_types_p
= true;
1202 if (d1_variable
|| d2_variable
)
1204 if (d1_zero
&& d2_zero
)
1206 if (d1_zero
|| d2_zero
1207 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1208 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1217 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1219 tree a1
= TYPE_ATTRIBUTES (t1
);
1220 tree a2
= TYPE_ATTRIBUTES (t2
);
1222 if (! attribute_list_contained (a1
, a2
)
1223 && ! attribute_list_contained (a2
, a1
))
1227 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1229 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1235 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1236 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1237 enum_and_int_p
, different_types_p
));
1243 return attrval
== 2 && val
== 1 ? 2 : val
;
1246 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1247 their qualifiers, except for named address spaces. If the pointers point to
1248 different named addresses, then we must determine if one address space is a
1249 subset of the other. */
1252 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1256 tree mvl
= TREE_TYPE (ttl
);
1257 tree mvr
= TREE_TYPE (ttr
);
1258 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1259 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1260 addr_space_t as_common
;
1261 bool enum_and_int_p
;
1263 /* Fail if pointers point to incompatible address spaces. */
1264 if (!addr_space_superset (asl
, asr
, &as_common
))
1267 /* For pedantic record result of comptypes on arrays before losing
1268 qualifiers on the element type below. */
1271 if (TREE_CODE (mvl
) == ARRAY_TYPE
1272 && TREE_CODE (mvr
) == ARRAY_TYPE
)
1273 val_ped
= comptypes (mvl
, mvr
);
1275 /* Qualifiers on element types of array types that are
1276 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1278 mvl
= (TYPE_ATOMIC (strip_array_types (mvl
))
1279 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1280 : TYPE_MAIN_VARIANT (mvl
));
1282 mvr
= (TYPE_ATOMIC (strip_array_types (mvr
))
1283 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1284 : TYPE_MAIN_VARIANT (mvr
));
1286 enum_and_int_p
= false;
1287 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1289 if (val
== 1 && val_ped
!= 1)
1290 pedwarn (location
, OPT_Wpedantic
, "pointers to arrays with different qualifiers "
1291 "are incompatible in ISO C");
1294 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1296 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1297 warning_at (location
, OPT_Wc___compat
,
1298 "pointer target types incompatible in C++");
1303 /* Subroutines of `comptypes'. */
1305 /* Determine whether two trees derive from the same translation unit.
1306 If the CONTEXT chain ends in a null, that tree's context is still
1307 being parsed, so if two trees have context chains ending in null,
1308 they're in the same translation unit. */
1310 same_translation_unit_p (const_tree t1
, const_tree t2
)
1312 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1313 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1315 case tcc_declaration
:
1316 t1
= DECL_CONTEXT (t1
); break;
1318 t1
= TYPE_CONTEXT (t1
); break;
1319 case tcc_exceptional
:
1320 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1321 default: gcc_unreachable ();
1324 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1325 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1327 case tcc_declaration
:
1328 t2
= DECL_CONTEXT (t2
); break;
1330 t2
= TYPE_CONTEXT (t2
); break;
1331 case tcc_exceptional
:
1332 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1333 default: gcc_unreachable ();
1339 /* Allocate the seen two types, assuming that they are compatible. */
1341 static struct tagged_tu_seen_cache
*
1342 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1344 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1345 tu
->next
= tagged_tu_seen_base
;
1349 tagged_tu_seen_base
= tu
;
1351 /* The C standard says that two structures in different translation
1352 units are compatible with each other only if the types of their
1353 fields are compatible (among other things). We assume that they
1354 are compatible until proven otherwise when building the cache.
1355 An example where this can occur is:
1360 If we are comparing this against a similar struct in another TU,
1361 and did not assume they were compatible, we end up with an infinite
1367 /* Free the seen types until we get to TU_TIL. */
1370 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1372 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1373 while (tu
!= tu_til
)
1375 const struct tagged_tu_seen_cache
*const tu1
1376 = (const struct tagged_tu_seen_cache
*) tu
;
1378 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1380 tagged_tu_seen_base
= tu_til
;
1383 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1384 compatible. If the two types are not the same (which has been
1385 checked earlier), this can only happen when multiple translation
1386 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1387 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1388 comptypes_internal. */
1391 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1392 bool *enum_and_int_p
, bool *different_types_p
)
1395 bool needs_warning
= false;
1397 /* We have to verify that the tags of the types are the same. This
1398 is harder than it looks because this may be a typedef, so we have
1399 to go look at the original type. It may even be a typedef of a
1401 In the case of compiler-created builtin structs the TYPE_DECL
1402 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1403 while (TYPE_NAME (t1
)
1404 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1405 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1406 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1408 while (TYPE_NAME (t2
)
1409 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1410 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1411 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1413 /* C90 didn't have the requirement that the two tags be the same. */
1414 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1417 /* C90 didn't say what happened if one or both of the types were
1418 incomplete; we choose to follow C99 rules here, which is that they
1420 if (TYPE_SIZE (t1
) == NULL
1421 || TYPE_SIZE (t2
) == NULL
)
1425 const struct tagged_tu_seen_cache
* tts_i
;
1426 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1427 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1431 switch (TREE_CODE (t1
))
1435 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1436 /* Speed up the case where the type values are in the same order. */
1437 tree tv1
= TYPE_VALUES (t1
);
1438 tree tv2
= TYPE_VALUES (t2
);
1445 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1447 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1449 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1456 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1460 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1466 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1472 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1474 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1476 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1487 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1488 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1494 /* Speed up the common case where the fields are in the same order. */
1495 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1496 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1500 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1502 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1503 enum_and_int_p
, different_types_p
);
1505 if (result
!= 1 && !DECL_NAME (s1
))
1513 needs_warning
= true;
1515 if (TREE_CODE (s1
) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1517 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1525 tu
->val
= needs_warning
? 2 : 1;
1529 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1533 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1534 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1538 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1542 if (result
!= 1 && !DECL_NAME (s1
))
1550 needs_warning
= true;
1552 if (TREE_CODE (s1
) == FIELD_DECL
1553 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1554 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1566 tu
->val
= needs_warning
? 2 : 10;
1572 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1574 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1576 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1579 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1580 || DECL_NAME (s1
) != DECL_NAME (s2
))
1582 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1583 enum_and_int_p
, different_types_p
);
1587 needs_warning
= true;
1589 if (TREE_CODE (s1
) == FIELD_DECL
1590 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1591 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1597 tu
->val
= needs_warning
? 2 : 1;
1606 /* Return 1 if two function types F1 and F2 are compatible.
1607 If either type specifies no argument types,
1608 the other must specify a fixed number of self-promoting arg types.
1609 Otherwise, if one type specifies only the number of arguments,
1610 the other must specify that number of self-promoting arg types.
1611 Otherwise, the argument types must match.
1612 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1615 function_types_compatible_p (const_tree f1
, const_tree f2
,
1616 bool *enum_and_int_p
, bool *different_types_p
)
1619 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1624 ret1
= TREE_TYPE (f1
);
1625 ret2
= TREE_TYPE (f2
);
1627 /* 'volatile' qualifiers on a function's return type used to mean
1628 the function is noreturn. */
1629 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1630 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1631 if (TYPE_VOLATILE (ret1
))
1632 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1633 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1634 if (TYPE_VOLATILE (ret2
))
1635 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1636 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1637 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1641 args1
= TYPE_ARG_TYPES (f1
);
1642 args2
= TYPE_ARG_TYPES (f2
);
1644 if (different_types_p
!= NULL
1645 && (args1
== 0) != (args2
== 0))
1646 *different_types_p
= true;
1648 /* An unspecified parmlist matches any specified parmlist
1649 whose argument types don't need default promotions. */
1653 if (!self_promoting_args_p (args2
))
1655 /* If one of these types comes from a non-prototype fn definition,
1656 compare that with the other type's arglist.
1657 If they don't match, ask for a warning (but no error). */
1658 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1659 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1660 enum_and_int_p
, different_types_p
))
1666 if (!self_promoting_args_p (args1
))
1668 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1669 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1670 enum_and_int_p
, different_types_p
))
1675 /* Both types have argument lists: compare them and propagate results. */
1676 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1678 return val1
!= 1 ? val1
: val
;
1681 /* Check two lists of types for compatibility, returning 0 for
1682 incompatible, 1 for compatible, or 2 for compatible with
1683 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1684 comptypes_internal. */
1687 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1688 bool *enum_and_int_p
, bool *different_types_p
)
1690 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1696 tree a1
, mv1
, a2
, mv2
;
1697 if (args1
== 0 && args2
== 0)
1699 /* If one list is shorter than the other,
1700 they fail to match. */
1701 if (args1
== 0 || args2
== 0)
1703 mv1
= a1
= TREE_VALUE (args1
);
1704 mv2
= a2
= TREE_VALUE (args2
);
1705 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1706 mv1
= (TYPE_ATOMIC (mv1
)
1707 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1709 : TYPE_MAIN_VARIANT (mv1
));
1710 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1711 mv2
= (TYPE_ATOMIC (mv2
)
1712 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1714 : TYPE_MAIN_VARIANT (mv2
));
1715 /* A null pointer instead of a type
1716 means there is supposed to be an argument
1717 but nothing is specified about what type it has.
1718 So match anything that self-promotes. */
1719 if (different_types_p
!= NULL
1720 && (a1
== 0) != (a2
== 0))
1721 *different_types_p
= true;
1724 if (c_type_promotes_to (a2
) != a2
)
1729 if (c_type_promotes_to (a1
) != a1
)
1732 /* If one of the lists has an error marker, ignore this arg. */
1733 else if (TREE_CODE (a1
) == ERROR_MARK
1734 || TREE_CODE (a2
) == ERROR_MARK
)
1736 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1737 different_types_p
)))
1739 if (different_types_p
!= NULL
)
1740 *different_types_p
= true;
1741 /* Allow wait (union {union wait *u; int *i} *)
1742 and wait (union wait *) to be compatible. */
1743 if (TREE_CODE (a1
) == UNION_TYPE
1744 && (TYPE_NAME (a1
) == 0
1745 || TYPE_TRANSPARENT_AGGR (a1
))
1746 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a1
),
1751 for (memb
= TYPE_FIELDS (a1
);
1752 memb
; memb
= DECL_CHAIN (memb
))
1754 tree mv3
= TREE_TYPE (memb
);
1755 if (mv3
&& mv3
!= error_mark_node
1756 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1757 mv3
= (TYPE_ATOMIC (mv3
)
1758 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1760 : TYPE_MAIN_VARIANT (mv3
));
1761 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1768 else if (TREE_CODE (a2
) == UNION_TYPE
1769 && (TYPE_NAME (a2
) == 0
1770 || TYPE_TRANSPARENT_AGGR (a2
))
1771 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1772 && tree_int_cst_equal (TYPE_SIZE (a2
),
1776 for (memb
= TYPE_FIELDS (a2
);
1777 memb
; memb
= DECL_CHAIN (memb
))
1779 tree mv3
= TREE_TYPE (memb
);
1780 if (mv3
&& mv3
!= error_mark_node
1781 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1782 mv3
= (TYPE_ATOMIC (mv3
)
1783 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1785 : TYPE_MAIN_VARIANT (mv3
));
1786 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1797 /* comptypes said ok, but record if it said to warn. */
1801 args1
= TREE_CHAIN (args1
);
1802 args2
= TREE_CHAIN (args2
);
1806 /* Compute the size to increment a pointer by. When a function type or void
1807 type or incomplete type is passed, size_one_node is returned.
1808 This function does not emit any diagnostics; the caller is responsible
1812 c_size_in_bytes (const_tree type
)
1814 enum tree_code code
= TREE_CODE (type
);
1816 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1817 || !COMPLETE_TYPE_P (type
))
1818 return size_one_node
;
1820 /* Convert in case a char is more than one unit. */
1821 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1822 size_int (TYPE_PRECISION (char_type_node
)
1826 /* Return either DECL or its known constant value (if it has one). */
1829 decl_constant_value (tree decl
)
1831 if (/* Don't change a variable array bound or initial value to a constant
1832 in a place where a variable is invalid. Note that DECL_INITIAL
1833 isn't valid for a PARM_DECL. */
1834 current_function_decl
!= 0
1835 && TREE_CODE (decl
) != PARM_DECL
1836 && !TREE_THIS_VOLATILE (decl
)
1837 && TREE_READONLY (decl
)
1838 && DECL_INITIAL (decl
) != 0
1839 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1840 /* This is invalid if initial value is not constant.
1841 If it has either a function call, a memory reference,
1842 or a variable, then re-evaluating it could give different results. */
1843 && TREE_CONSTANT (DECL_INITIAL (decl
))
1844 /* Check for cases where this is sub-optimal, even though valid. */
1845 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1846 return DECL_INITIAL (decl
);
1850 /* Convert the array expression EXP to a pointer. */
1852 array_to_pointer_conversion (location_t loc
, tree exp
)
1854 tree orig_exp
= exp
;
1855 tree type
= TREE_TYPE (exp
);
1857 tree restype
= TREE_TYPE (type
);
1860 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1862 STRIP_TYPE_NOPS (exp
);
1864 if (TREE_NO_WARNING (orig_exp
))
1865 TREE_NO_WARNING (exp
) = 1;
1867 ptrtype
= build_pointer_type (restype
);
1869 if (INDIRECT_REF_P (exp
))
1870 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1872 /* In C++ array compound literals are temporary objects unless they are
1873 const or appear in namespace scope, so they are destroyed too soon
1874 to use them for much of anything (c++/53220). */
1875 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1877 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1878 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1879 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1880 "converting an array compound literal to a pointer "
1881 "is ill-formed in C++");
1884 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, true);
1885 return convert (ptrtype
, adr
);
1888 /* Convert the function expression EXP to a pointer. */
1890 function_to_pointer_conversion (location_t loc
, tree exp
)
1892 tree orig_exp
= exp
;
1894 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1896 STRIP_TYPE_NOPS (exp
);
1898 if (TREE_NO_WARNING (orig_exp
))
1899 TREE_NO_WARNING (exp
) = 1;
1901 return build_unary_op (loc
, ADDR_EXPR
, exp
, false);
1904 /* Mark EXP as read, not just set, for set but not used -Wunused
1905 warning purposes. */
1908 mark_exp_read (tree exp
)
1910 switch (TREE_CODE (exp
))
1914 DECL_READ_P (exp
) = 1;
1923 case VIEW_CONVERT_EXPR
:
1924 mark_exp_read (TREE_OPERAND (exp
, 0));
1927 case C_MAYBE_CONST_EXPR
:
1928 mark_exp_read (TREE_OPERAND (exp
, 1));
1935 /* Perform the default conversion of arrays and functions to pointers.
1936 Return the result of converting EXP. For any other expression, just
1939 LOC is the location of the expression. */
1942 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1944 tree orig_exp
= exp
.value
;
1945 tree type
= TREE_TYPE (exp
.value
);
1946 enum tree_code code
= TREE_CODE (type
);
1952 bool not_lvalue
= false;
1953 bool lvalue_array_p
;
1955 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1956 || CONVERT_EXPR_P (exp
.value
))
1957 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1959 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1961 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1964 if (TREE_NO_WARNING (orig_exp
))
1965 TREE_NO_WARNING (exp
.value
) = 1;
1967 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1968 if (!flag_isoc99
&& !lvalue_array_p
)
1970 /* Before C99, non-lvalue arrays do not decay to pointers.
1971 Normally, using such an array would be invalid; but it can
1972 be used correctly inside sizeof or as a statement expression.
1973 Thus, do not give an error here; an error will result later. */
1977 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1981 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1991 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1993 mark_exp_read (exp
.value
);
1994 return default_function_array_conversion (loc
, exp
);
1997 /* Return whether EXPR should be treated as an atomic lvalue for the
1998 purposes of load and store handling. */
2001 really_atomic_lvalue (tree expr
)
2003 if (error_operand_p (expr
))
2005 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
2007 if (!lvalue_p (expr
))
2010 /* Ignore _Atomic on register variables, since their addresses can't
2011 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2012 sequences wouldn't work. Ignore _Atomic on structures containing
2013 bit-fields, since accessing elements of atomic structures or
2014 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2015 it's undefined at translation time or execution time, and the
2016 normal atomic sequences again wouldn't work. */
2017 while (handled_component_p (expr
))
2019 if (TREE_CODE (expr
) == COMPONENT_REF
2020 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
2022 expr
= TREE_OPERAND (expr
, 0);
2024 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
2029 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2030 including converting functions and arrays to pointers if CONVERT_P.
2031 If READ_P, also mark the expression as having been read. */
2034 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
2035 bool convert_p
, bool read_p
)
2038 mark_exp_read (exp
.value
);
2040 exp
= default_function_array_conversion (loc
, exp
);
2041 if (really_atomic_lvalue (exp
.value
))
2043 vec
<tree
, va_gc
> *params
;
2044 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2045 tree expr_type
= TREE_TYPE (exp
.value
);
2046 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, false);
2047 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2049 gcc_assert (TYPE_ATOMIC (expr_type
));
2051 /* Expansion of a generic atomic load may require an addition
2052 element, so allocate enough to prevent a resize. */
2053 vec_alloc (params
, 4);
2055 /* Remove the qualifiers for the rest of the expressions and
2056 create the VAL temp variable to hold the RHS. */
2057 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2058 tmp
= create_tmp_var_raw (nonatomic_type
);
2059 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, false);
2060 TREE_ADDRESSABLE (tmp
) = 1;
2061 TREE_NO_WARNING (tmp
) = 1;
2063 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2064 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2065 params
->quick_push (expr_addr
);
2066 params
->quick_push (tmp_addr
);
2067 params
->quick_push (seq_cst
);
2068 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2070 /* EXPR is always read. */
2071 mark_exp_read (exp
.value
);
2073 /* Return tmp which contains the value loaded. */
2074 exp
.value
= build4 (TARGET_EXPR
, nonatomic_type
, tmp
, func_call
,
2075 NULL_TREE
, NULL_TREE
);
2080 /* EXP is an expression of integer type. Apply the integer promotions
2081 to it and return the promoted value. */
2084 perform_integral_promotions (tree exp
)
2086 tree type
= TREE_TYPE (exp
);
2087 enum tree_code code
= TREE_CODE (type
);
2089 gcc_assert (INTEGRAL_TYPE_P (type
));
2091 /* Normally convert enums to int,
2092 but convert wide enums to something wider. */
2093 if (code
== ENUMERAL_TYPE
)
2095 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2096 TYPE_PRECISION (integer_type_node
)),
2097 ((TYPE_PRECISION (type
)
2098 >= TYPE_PRECISION (integer_type_node
))
2099 && TYPE_UNSIGNED (type
)));
2101 return convert (type
, exp
);
2104 /* ??? This should no longer be needed now bit-fields have their
2106 if (TREE_CODE (exp
) == COMPONENT_REF
2107 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2108 /* If it's thinner than an int, promote it like a
2109 c_promoting_integer_type_p, otherwise leave it alone. */
2110 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2111 TYPE_PRECISION (integer_type_node
)))
2112 return convert (integer_type_node
, exp
);
2114 if (c_promoting_integer_type_p (type
))
2116 /* Preserve unsignedness if not really getting any wider. */
2117 if (TYPE_UNSIGNED (type
)
2118 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2119 return convert (unsigned_type_node
, exp
);
2121 return convert (integer_type_node
, exp
);
2128 /* Perform default promotions for C data used in expressions.
2129 Enumeral types or short or char are converted to int.
2130 In addition, manifest constants symbols are replaced by their values. */
2133 default_conversion (tree exp
)
2136 tree type
= TREE_TYPE (exp
);
2137 enum tree_code code
= TREE_CODE (type
);
2140 mark_exp_read (exp
);
2142 /* Functions and arrays have been converted during parsing. */
2143 gcc_assert (code
!= FUNCTION_TYPE
);
2144 if (code
== ARRAY_TYPE
)
2147 /* Constants can be used directly unless they're not loadable. */
2148 if (TREE_CODE (exp
) == CONST_DECL
)
2149 exp
= DECL_INITIAL (exp
);
2151 /* Strip no-op conversions. */
2153 STRIP_TYPE_NOPS (exp
);
2155 if (TREE_NO_WARNING (orig_exp
))
2156 TREE_NO_WARNING (exp
) = 1;
2158 if (code
== VOID_TYPE
)
2160 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2161 "void value not ignored as it ought to be");
2162 return error_mark_node
;
2165 exp
= require_complete_type (EXPR_LOC_OR_LOC (exp
, input_location
), exp
);
2166 if (exp
== error_mark_node
)
2167 return error_mark_node
;
2169 promoted_type
= targetm
.promoted_type (type
);
2171 return convert (promoted_type
, exp
);
2173 if (INTEGRAL_TYPE_P (type
))
2174 return perform_integral_promotions (exp
);
2179 /* Look up COMPONENT in a structure or union TYPE.
2181 If the component name is not found, returns NULL_TREE. Otherwise,
2182 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2183 stepping down the chain to the component, which is in the last
2184 TREE_VALUE of the list. Normally the list is of length one, but if
2185 the component is embedded within (nested) anonymous structures or
2186 unions, the list steps down the chain to the component. */
2189 lookup_field (tree type
, tree component
)
2193 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2194 to the field elements. Use a binary search on this array to quickly
2195 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2196 will always be set for structures which have many elements. */
2198 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2201 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2203 field
= TYPE_FIELDS (type
);
2205 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2206 while (top
- bot
> 1)
2208 half
= (top
- bot
+ 1) >> 1;
2209 field
= field_array
[bot
+half
];
2211 if (DECL_NAME (field
) == NULL_TREE
)
2213 /* Step through all anon unions in linear fashion. */
2214 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2216 field
= field_array
[bot
++];
2217 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2219 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2222 return tree_cons (NULL_TREE
, field
, anon
);
2224 /* The Plan 9 compiler permits referring
2225 directly to an anonymous struct/union field
2226 using a typedef name. */
2227 if (flag_plan9_extensions
2228 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2229 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2231 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2237 /* Entire record is only anon unions. */
2241 /* Restart the binary search, with new lower bound. */
2245 if (DECL_NAME (field
) == component
)
2247 if (DECL_NAME (field
) < component
)
2253 if (DECL_NAME (field_array
[bot
]) == component
)
2254 field
= field_array
[bot
];
2255 else if (DECL_NAME (field
) != component
)
2260 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2262 if (DECL_NAME (field
) == NULL_TREE
2263 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2265 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2268 return tree_cons (NULL_TREE
, field
, anon
);
2270 /* The Plan 9 compiler permits referring directly to an
2271 anonymous struct/union field using a typedef
2273 if (flag_plan9_extensions
2274 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2275 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2276 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2281 if (DECL_NAME (field
) == component
)
2285 if (field
== NULL_TREE
)
2289 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2292 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2295 lookup_field_fuzzy_find_candidates (tree type
, tree component
,
2296 vec
<tree
> *candidates
)
2299 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2301 if (DECL_NAME (field
) == NULL_TREE
2302 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
2303 lookup_field_fuzzy_find_candidates (TREE_TYPE (field
), component
,
2306 if (DECL_NAME (field
))
2307 candidates
->safe_push (DECL_NAME (field
));
2311 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2312 rather than returning a TREE_LIST for an exact match. */
2315 lookup_field_fuzzy (tree type
, tree component
)
2317 gcc_assert (TREE_CODE (component
) == IDENTIFIER_NODE
);
2319 /* First, gather a list of candidates. */
2320 auto_vec
<tree
> candidates
;
2322 lookup_field_fuzzy_find_candidates (type
, component
,
2325 return find_closest_identifier (component
, &candidates
);
2328 /* Support function for build_component_ref's error-handling.
2330 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2331 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2334 should_suggest_deref_p (tree datum_type
)
2336 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2337 allows "." for ptrs; we could be handling a failed attempt
2338 to access a property. */
2339 if (c_dialect_objc ())
2342 /* Only suggest it for pointers... */
2343 if (TREE_CODE (datum_type
) != POINTER_TYPE
)
2346 /* ...to structs/unions. */
2347 tree underlying_type
= TREE_TYPE (datum_type
);
2348 enum tree_code code
= TREE_CODE (underlying_type
);
2349 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2355 /* Make an expression to refer to the COMPONENT field of structure or
2356 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2357 location of the COMPONENT_REF. COMPONENT_LOC is the location
2361 build_component_ref (location_t loc
, tree datum
, tree component
,
2362 location_t component_loc
)
2364 tree type
= TREE_TYPE (datum
);
2365 enum tree_code code
= TREE_CODE (type
);
2368 bool datum_lvalue
= lvalue_p (datum
);
2370 if (!objc_is_public (datum
, component
))
2371 return error_mark_node
;
2373 /* Detect Objective-C property syntax object.property. */
2374 if (c_dialect_objc ()
2375 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2378 /* See if there is a field or component with name COMPONENT. */
2380 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2382 if (!COMPLETE_TYPE_P (type
))
2384 c_incomplete_type_error (loc
, NULL_TREE
, type
);
2385 return error_mark_node
;
2388 field
= lookup_field (type
, component
);
2392 tree guessed_id
= lookup_field_fuzzy (type
, component
);
2395 /* Attempt to provide a fixit replacement hint, if
2396 we have a valid range for the component. */
2397 location_t reported_loc
2398 = (component_loc
!= UNKNOWN_LOCATION
) ? component_loc
: loc
;
2399 gcc_rich_location
rich_loc (reported_loc
);
2400 if (component_loc
!= UNKNOWN_LOCATION
)
2401 rich_loc
.add_fixit_misspelled_id (component_loc
, guessed_id
);
2404 "%qT has no member named %qE; did you mean %qE?",
2405 type
, component
, guessed_id
);
2408 error_at (loc
, "%qT has no member named %qE", type
, component
);
2409 return error_mark_node
;
2412 /* Accessing elements of atomic structures or unions is undefined
2413 behavior (C11 6.5.2.3#5). */
2414 if (TYPE_ATOMIC (type
) && c_inhibit_evaluation_warnings
== 0)
2416 if (code
== RECORD_TYPE
)
2417 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2418 "structure %qE", component
, datum
);
2420 warning_at (loc
, 0, "accessing a member %qE of an atomic "
2421 "union %qE", component
, datum
);
2424 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2425 This might be better solved in future the way the C++ front
2426 end does it - by giving the anonymous entities each a
2427 separate name and type, and then have build_component_ref
2428 recursively call itself. We can't do that here. */
2431 tree subdatum
= TREE_VALUE (field
);
2434 bool use_datum_quals
;
2436 if (TREE_TYPE (subdatum
) == error_mark_node
)
2437 return error_mark_node
;
2439 /* If this is an rvalue, it does not have qualifiers in C
2440 standard terms and we must avoid propagating such
2441 qualifiers down to a non-lvalue array that is then
2442 converted to a pointer. */
2443 use_datum_quals
= (datum_lvalue
2444 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2446 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2447 if (use_datum_quals
)
2448 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2449 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2451 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2453 SET_EXPR_LOCATION (ref
, loc
);
2454 if (TREE_READONLY (subdatum
)
2455 || (use_datum_quals
&& TREE_READONLY (datum
)))
2456 TREE_READONLY (ref
) = 1;
2457 if (TREE_THIS_VOLATILE (subdatum
)
2458 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2459 TREE_THIS_VOLATILE (ref
) = 1;
2461 if (TREE_DEPRECATED (subdatum
))
2462 warn_deprecated_use (subdatum
, NULL_TREE
);
2466 field
= TREE_CHAIN (field
);
2472 else if (should_suggest_deref_p (type
))
2474 /* Special-case the error message for "ptr.field" for the case
2475 where the user has confused "." vs "->". */
2476 rich_location
richloc (line_table
, loc
);
2477 /* "loc" should be the "." token. */
2478 richloc
.add_fixit_replace ("->");
2479 error_at_rich_loc (&richloc
,
2480 "%qE is a pointer; did you mean to use %<->%>?",
2482 return error_mark_node
;
2484 else if (code
!= ERROR_MARK
)
2486 "request for member %qE in something not a structure or union",
2489 return error_mark_node
;
2492 /* Given an expression PTR for a pointer, return an expression
2493 for the value pointed to.
2494 ERRORSTRING is the name of the operator to appear in error messages.
2496 LOC is the location to use for the generated tree. */
2499 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2501 tree pointer
= default_conversion (ptr
);
2502 tree type
= TREE_TYPE (pointer
);
2505 if (TREE_CODE (type
) == POINTER_TYPE
)
2507 if (CONVERT_EXPR_P (pointer
)
2508 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2510 /* If a warning is issued, mark it to avoid duplicates from
2511 the backend. This only needs to be done at
2512 warn_strict_aliasing > 2. */
2513 if (warn_strict_aliasing
> 2)
2514 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2515 type
, TREE_OPERAND (pointer
, 0)))
2516 TREE_NO_WARNING (pointer
) = 1;
2519 if (TREE_CODE (pointer
) == ADDR_EXPR
2520 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2521 == TREE_TYPE (type
)))
2523 ref
= TREE_OPERAND (pointer
, 0);
2524 protected_set_expr_location (ref
, loc
);
2529 tree t
= TREE_TYPE (type
);
2531 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2533 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2535 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)))
2537 error_at (loc
, "dereferencing pointer to incomplete type "
2539 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)) = 1;
2541 return error_mark_node
;
2543 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2544 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2546 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2547 so that we get the proper error message if the result is used
2548 to assign to. Also, &* is supposed to be a no-op.
2549 And ANSI C seems to specify that the type of the result
2550 should be the const type. */
2551 /* A de-reference of a pointer to const is not a const. It is valid
2552 to change it via some other pointer. */
2553 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2554 TREE_SIDE_EFFECTS (ref
)
2555 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2556 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2557 protected_set_expr_location (ref
, loc
);
2561 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2562 invalid_indirection_error (loc
, type
, errstring
);
2564 return error_mark_node
;
2567 /* This handles expressions of the form "a[i]", which denotes
2570 This is logically equivalent in C to *(a+i), but we may do it differently.
2571 If A is a variable or a member, we generate a primitive ARRAY_REF.
2572 This avoids forcing the array out of registers, and can work on
2573 arrays that are not lvalues (for example, members of structures returned
2576 For vector types, allow vector[i] but not i[vector], and create
2577 *(((type*)&vectortype) + i) for the expression.
2579 LOC is the location to use for the returned expression. */
2582 build_array_ref (location_t loc
, tree array
, tree index
)
2585 bool swapped
= false;
2586 if (TREE_TYPE (array
) == error_mark_node
2587 || TREE_TYPE (index
) == error_mark_node
)
2588 return error_mark_node
;
2590 if (flag_cilkplus
&& contains_array_notation_expr (index
))
2593 if (!find_rank (loc
, index
, index
, true, &rank
))
2594 return error_mark_node
;
2597 error_at (loc
, "rank of the array's index is greater than 1");
2598 return error_mark_node
;
2601 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2602 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2603 /* Allow vector[index] but not index[vector]. */
2604 && !VECTOR_TYPE_P (TREE_TYPE (array
)))
2606 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2607 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2610 "subscripted value is neither array nor pointer nor vector");
2612 return error_mark_node
;
2614 std::swap (array
, index
);
2618 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2620 error_at (loc
, "array subscript is not an integer");
2621 return error_mark_node
;
2624 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2626 error_at (loc
, "subscripted value is pointer to function");
2627 return error_mark_node
;
2630 /* ??? Existing practice has been to warn only when the char
2631 index is syntactically the index, not for char[array]. */
2633 warn_array_subscript_with_type_char (loc
, index
);
2635 /* Apply default promotions *after* noticing character types. */
2636 index
= default_conversion (index
);
2637 if (index
== error_mark_node
)
2638 return error_mark_node
;
2640 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2642 bool was_vector
= VECTOR_TYPE_P (TREE_TYPE (array
));
2643 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, index
);
2645 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2649 /* An array that is indexed by a non-constant
2650 cannot be stored in a register; we must be able to do
2651 address arithmetic on its address.
2652 Likewise an array of elements of variable size. */
2653 if (TREE_CODE (index
) != INTEGER_CST
2654 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2655 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2657 if (!c_mark_addressable (array
))
2658 return error_mark_node
;
2660 /* An array that is indexed by a constant value which is not within
2661 the array bounds cannot be stored in a register either; because we
2662 would get a crash in store_bit_field/extract_bit_field when trying
2663 to access a non-existent part of the register. */
2664 if (TREE_CODE (index
) == INTEGER_CST
2665 && TYPE_DOMAIN (TREE_TYPE (array
))
2666 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2668 if (!c_mark_addressable (array
))
2669 return error_mark_node
;
2672 if ((pedantic
|| warn_c90_c99_compat
)
2676 while (TREE_CODE (foo
) == COMPONENT_REF
)
2677 foo
= TREE_OPERAND (foo
, 0);
2678 if (VAR_P (foo
) && C_DECL_REGISTER (foo
))
2679 pedwarn (loc
, OPT_Wpedantic
,
2680 "ISO C forbids subscripting %<register%> array");
2681 else if (!lvalue_p (foo
))
2682 pedwarn_c90 (loc
, OPT_Wpedantic
,
2683 "ISO C90 forbids subscripting non-lvalue "
2687 type
= TREE_TYPE (TREE_TYPE (array
));
2688 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2689 /* Array ref is const/volatile if the array elements are
2690 or if the array is. */
2691 TREE_READONLY (rval
)
2692 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2693 | TREE_READONLY (array
));
2694 TREE_SIDE_EFFECTS (rval
)
2695 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2696 | TREE_SIDE_EFFECTS (array
));
2697 TREE_THIS_VOLATILE (rval
)
2698 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2699 /* This was added by rms on 16 Nov 91.
2700 It fixes vol struct foo *a; a->elts[1]
2701 in an inline function.
2702 Hope it doesn't break something else. */
2703 | TREE_THIS_VOLATILE (array
));
2704 ret
= require_complete_type (loc
, rval
);
2705 protected_set_expr_location (ret
, loc
);
2707 ret
= non_lvalue_loc (loc
, ret
);
2712 tree ar
= default_conversion (array
);
2714 if (ar
== error_mark_node
)
2717 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2718 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2720 ret
= build_indirect_ref (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
,
2724 ret
= non_lvalue_loc (loc
, ret
);
2729 /* Build an external reference to identifier ID. FUN indicates
2730 whether this will be used for a function call. LOC is the source
2731 location of the identifier. This sets *TYPE to the type of the
2732 identifier, which is not the same as the type of the returned value
2733 for CONST_DECLs defined as enum constants. If the type of the
2734 identifier is not available, *TYPE is set to NULL. */
2736 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2739 tree decl
= lookup_name (id
);
2741 /* In Objective-C, an instance variable (ivar) may be preferred to
2742 whatever lookup_name() found. */
2743 decl
= objc_lookup_ivar (decl
, id
);
2746 if (decl
&& decl
!= error_mark_node
)
2749 *type
= TREE_TYPE (ref
);
2752 /* Implicit function declaration. */
2753 ref
= implicitly_declare (loc
, id
);
2754 else if (decl
== error_mark_node
)
2755 /* Don't complain about something that's already been
2756 complained about. */
2757 return error_mark_node
;
2760 undeclared_variable (loc
, id
);
2761 return error_mark_node
;
2764 if (TREE_TYPE (ref
) == error_mark_node
)
2765 return error_mark_node
;
2767 if (TREE_DEPRECATED (ref
))
2768 warn_deprecated_use (ref
, NULL_TREE
);
2770 /* Recursive call does not count as usage. */
2771 if (ref
!= current_function_decl
)
2773 TREE_USED (ref
) = 1;
2776 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2778 if (!in_sizeof
&& !in_typeof
)
2779 C_DECL_USED (ref
) = 1;
2780 else if (DECL_INITIAL (ref
) == 0
2781 && DECL_EXTERNAL (ref
)
2782 && !TREE_PUBLIC (ref
))
2783 record_maybe_used_decl (ref
);
2786 if (TREE_CODE (ref
) == CONST_DECL
)
2788 used_types_insert (TREE_TYPE (ref
));
2791 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2792 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2794 warning_at (loc
, OPT_Wc___compat
,
2795 ("enum constant defined in struct or union "
2796 "is not visible in C++"));
2797 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2800 ref
= DECL_INITIAL (ref
);
2801 TREE_CONSTANT (ref
) = 1;
2803 else if (current_function_decl
!= 0
2804 && !DECL_FILE_SCOPE_P (current_function_decl
)
2805 && (VAR_OR_FUNCTION_DECL_P (ref
)
2806 || TREE_CODE (ref
) == PARM_DECL
))
2808 tree context
= decl_function_context (ref
);
2810 if (context
!= 0 && context
!= current_function_decl
)
2811 DECL_NONLOCAL (ref
) = 1;
2813 /* C99 6.7.4p3: An inline definition of a function with external
2814 linkage ... shall not contain a reference to an identifier with
2815 internal linkage. */
2816 else if (current_function_decl
!= 0
2817 && DECL_DECLARED_INLINE_P (current_function_decl
)
2818 && DECL_EXTERNAL (current_function_decl
)
2819 && VAR_OR_FUNCTION_DECL_P (ref
)
2820 && (!VAR_P (ref
) || TREE_STATIC (ref
))
2821 && ! TREE_PUBLIC (ref
)
2822 && DECL_CONTEXT (ref
) != current_function_decl
)
2823 record_inline_static (loc
, current_function_decl
, ref
,
2829 /* Record details of decls possibly used inside sizeof or typeof. */
2830 struct maybe_used_decl
2834 /* The level seen at (in_sizeof + in_typeof). */
2836 /* The next one at this level or above, or NULL. */
2837 struct maybe_used_decl
*next
;
2840 static struct maybe_used_decl
*maybe_used_decls
;
2842 /* Record that DECL, an undefined static function reference seen
2843 inside sizeof or typeof, might be used if the operand of sizeof is
2844 a VLA type or the operand of typeof is a variably modified
2848 record_maybe_used_decl (tree decl
)
2850 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2852 t
->level
= in_sizeof
+ in_typeof
;
2853 t
->next
= maybe_used_decls
;
2854 maybe_used_decls
= t
;
2857 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2858 USED is false, just discard them. If it is true, mark them used
2859 (if no longer inside sizeof or typeof) or move them to the next
2860 level up (if still inside sizeof or typeof). */
2863 pop_maybe_used (bool used
)
2865 struct maybe_used_decl
*p
= maybe_used_decls
;
2866 int cur_level
= in_sizeof
+ in_typeof
;
2867 while (p
&& p
->level
> cur_level
)
2872 C_DECL_USED (p
->decl
) = 1;
2874 p
->level
= cur_level
;
2878 if (!used
|| cur_level
== 0)
2879 maybe_used_decls
= p
;
2882 /* Return the result of sizeof applied to EXPR. */
2885 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2888 if (expr
.value
== error_mark_node
)
2890 ret
.value
= error_mark_node
;
2891 ret
.original_code
= ERROR_MARK
;
2892 ret
.original_type
= NULL
;
2893 pop_maybe_used (false);
2897 bool expr_const_operands
= true;
2899 if (TREE_CODE (expr
.value
) == PARM_DECL
2900 && C_ARRAY_PARAMETER (expr
.value
))
2902 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2903 "%<sizeof%> on array function parameter %qE will "
2904 "return size of %qT", expr
.value
,
2905 expr
.original_type
))
2906 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2908 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2909 &expr_const_operands
);
2910 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2911 c_last_sizeof_arg
= expr
.value
;
2912 ret
.original_code
= SIZEOF_EXPR
;
2913 ret
.original_type
= NULL
;
2914 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2916 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2917 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2918 folded_expr
, ret
.value
);
2919 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2920 SET_EXPR_LOCATION (ret
.value
, loc
);
2922 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2927 /* Return the result of sizeof applied to T, a structure for the type
2928 name passed to sizeof (rather than the type itself). LOC is the
2929 location of the original expression. */
2932 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2936 tree type_expr
= NULL_TREE
;
2937 bool type_expr_const
= true;
2938 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2939 ret
.value
= c_sizeof (loc
, type
);
2940 c_last_sizeof_arg
= type
;
2941 ret
.original_code
= SIZEOF_EXPR
;
2942 ret
.original_type
= NULL
;
2943 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2944 && c_vla_type_p (type
))
2946 /* If the type is a [*] array, it is a VLA but is represented as
2947 having a size of zero. In such a case we must ensure that
2948 the result of sizeof does not get folded to a constant by
2949 c_fully_fold, because if the size is evaluated the result is
2950 not constant and so constraints on zero or negative size
2951 arrays must not be applied when this sizeof call is inside
2952 another array declarator. */
2954 type_expr
= integer_zero_node
;
2955 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2956 type_expr
, ret
.value
);
2957 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2959 pop_maybe_used (type
!= error_mark_node
2960 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2964 /* Build a function call to function FUNCTION with parameters PARAMS.
2965 The function call is at LOC.
2966 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2967 TREE_VALUE of each node is a parameter-expression.
2968 FUNCTION's data type may be a function type or a pointer-to-function. */
2971 build_function_call (location_t loc
, tree function
, tree params
)
2973 vec
<tree
, va_gc
> *v
;
2976 vec_alloc (v
, list_length (params
));
2977 for (; params
; params
= TREE_CHAIN (params
))
2978 v
->quick_push (TREE_VALUE (params
));
2979 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2984 /* Give a note about the location of the declaration of DECL. */
2987 inform_declaration (tree decl
)
2989 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_IS_BUILTIN (decl
)))
2990 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2993 /* Build a function call to function FUNCTION with parameters PARAMS.
2994 ORIGTYPES, if not NULL, is a vector of types; each element is
2995 either NULL or the original type of the corresponding element in
2996 PARAMS. The original type may differ from TREE_TYPE of the
2997 parameter for enums. FUNCTION's data type may be a function type
2998 or pointer-to-function. This function changes the elements of
3002 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3003 tree function
, vec
<tree
, va_gc
> *params
,
3004 vec
<tree
, va_gc
> *origtypes
)
3006 tree fntype
, fundecl
= 0;
3007 tree name
= NULL_TREE
, result
;
3013 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3014 STRIP_TYPE_NOPS (function
);
3016 /* Convert anything with function type to a pointer-to-function. */
3017 if (TREE_CODE (function
) == FUNCTION_DECL
)
3019 name
= DECL_NAME (function
);
3022 tm_malloc_replacement (function
);
3024 /* Atomic functions have type checking/casting already done. They are
3025 often rewritten and don't match the original parameter list. */
3026 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
3030 && is_cilkplus_reduce_builtin (function
))
3033 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
3034 function
= function_to_pointer_conversion (loc
, function
);
3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
3038 if (params
&& !params
->is_empty ())
3039 function
= objc_rewrite_function_call (function
, (*params
)[0]);
3041 function
= c_fully_fold (function
, false, NULL
);
3043 fntype
= TREE_TYPE (function
);
3045 if (TREE_CODE (fntype
) == ERROR_MARK
)
3046 return error_mark_node
;
3048 if (!(TREE_CODE (fntype
) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
3051 if (!flag_diagnostics_show_caret
)
3053 "called object %qE is not a function or function pointer",
3055 else if (DECL_P (function
))
3058 "called object %qD is not a function or function pointer",
3060 inform_declaration (function
);
3064 "called object is not a function or function pointer");
3065 return error_mark_node
;
3068 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
3069 current_function_returns_abnormally
= 1;
3071 /* fntype now gets the type of function pointed to. */
3072 fntype
= TREE_TYPE (fntype
);
3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3077 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
3078 origtypes
, function
, fundecl
);
3080 return error_mark_node
;
3082 /* Check that the function is called through a compatible prototype.
3083 If it is not, warn. */
3084 if (CONVERT_EXPR_P (function
)
3085 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
3087 && !comptypes (fntype
, TREE_TYPE (tem
)))
3089 tree return_type
= TREE_TYPE (fntype
);
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
3094 warning_at (loc
, 0, "function called through a non-compatible type");
3096 if (VOID_TYPE_P (return_type
)
3097 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
3099 "function with qualified void return type called");
3102 argarray
= vec_safe_address (params
);
3104 /* Check that arguments to builtin functions match the expectations. */
3106 && DECL_BUILT_IN (fundecl
)
3107 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
3108 && !check_builtin_function_arguments (loc
, arg_loc
, fundecl
, nargs
,
3110 return error_mark_node
;
3112 /* Check that the arguments to the function are valid. */
3113 check_function_arguments (loc
, fntype
, nargs
, argarray
);
3115 if (name
!= NULL_TREE
3116 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
3118 if (require_constant_value
)
3120 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
3121 function
, nargs
, argarray
);
3123 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
3124 function
, nargs
, argarray
);
3125 if (TREE_CODE (result
) == NOP_EXPR
3126 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
3127 STRIP_TYPE_NOPS (result
);
3130 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
3131 function
, nargs
, argarray
);
3133 /* In this improbable scenario, a nested function returns a VM type.
3134 Create a TARGET_EXPR so that the call always has a LHS, much as
3135 what the C++ FE does for functions returning non-PODs. */
3136 if (variably_modified_type_p (TREE_TYPE (fntype
), NULL_TREE
))
3138 tree tmp
= create_tmp_var_raw (TREE_TYPE (fntype
));
3139 result
= build4 (TARGET_EXPR
, TREE_TYPE (fntype
), tmp
, result
,
3140 NULL_TREE
, NULL_TREE
);
3143 if (VOID_TYPE_P (TREE_TYPE (result
)))
3145 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
3147 "function with qualified void return type called");
3150 return require_complete_type (loc
, result
);
3153 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3156 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3157 tree function
, vec
<tree
, va_gc
> *params
,
3158 vec
<tree
, va_gc
> *origtypes
)
3160 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3161 STRIP_TYPE_NOPS (function
);
3163 /* Convert anything with function type to a pointer-to-function. */
3164 if (TREE_CODE (function
) == FUNCTION_DECL
)
3166 /* Implement type-directed function overloading for builtins.
3167 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3168 handle all the type checking. The result is a complete expression
3169 that implements this function call. */
3170 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3174 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3177 /* Convert the argument expressions in the vector VALUES
3178 to the types in the list TYPELIST.
3180 If TYPELIST is exhausted, or when an element has NULL as its type,
3181 perform the default conversions.
3183 ORIGTYPES is the original types of the expressions in VALUES. This
3184 holds the type of enum values which have been converted to integral
3185 types. It may be NULL.
3187 FUNCTION is a tree for the called function. It is used only for
3188 error messages, where it is formatted with %qE.
3190 This is also where warnings about wrong number of args are generated.
3192 ARG_LOC are locations of function arguments (if any).
3194 Returns the actual number of arguments processed (which may be less
3195 than the length of VALUES in some error situations), or -1 on
3199 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3200 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3201 tree function
, tree fundecl
)
3204 unsigned int parmnum
;
3205 bool error_args
= false;
3206 const bool type_generic
= fundecl
3207 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3208 bool type_generic_remove_excess_precision
= false;
3209 bool type_generic_overflow_p
= false;
3212 /* Change pointer to function to the function itself for
3214 if (TREE_CODE (function
) == ADDR_EXPR
3215 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3216 function
= TREE_OPERAND (function
, 0);
3218 /* Handle an ObjC selector specially for diagnostics. */
3219 selector
= objc_message_selector ();
3221 /* For type-generic built-in functions, determine whether excess
3222 precision should be removed (classification) or not
3225 && DECL_BUILT_IN (fundecl
)
3226 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3228 switch (DECL_FUNCTION_CODE (fundecl
))
3230 case BUILT_IN_ISFINITE
:
3231 case BUILT_IN_ISINF
:
3232 case BUILT_IN_ISINF_SIGN
:
3233 case BUILT_IN_ISNAN
:
3234 case BUILT_IN_ISNORMAL
:
3235 case BUILT_IN_FPCLASSIFY
:
3236 type_generic_remove_excess_precision
= true;
3239 case BUILT_IN_ADD_OVERFLOW_P
:
3240 case BUILT_IN_SUB_OVERFLOW_P
:
3241 case BUILT_IN_MUL_OVERFLOW_P
:
3242 /* The last argument of these type-generic builtins
3243 should not be promoted. */
3244 type_generic_overflow_p
= true;
3251 if (flag_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3252 return vec_safe_length (values
);
3254 /* Scan the given expressions and types, producing individual
3255 converted arguments. */
3257 for (typetail
= typelist
, parmnum
= 0;
3258 values
&& values
->iterate (parmnum
, &val
);
3261 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3262 tree valtype
= TREE_TYPE (val
);
3263 tree rname
= function
;
3264 int argnum
= parmnum
+ 1;
3265 const char *invalid_func_diag
;
3266 bool excess_precision
= false;
3269 /* Some __atomic_* builtins have additional hidden argument at
3272 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3273 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3276 if (type
== void_type_node
)
3279 error_at (loc
, "too many arguments to method %qE", selector
);
3281 error_at (loc
, "too many arguments to function %qE", function
);
3282 inform_declaration (fundecl
);
3283 return error_args
? -1 : (int) parmnum
;
3286 if (selector
&& argnum
> 2)
3292 npc
= null_pointer_constant_p (val
);
3294 /* If there is excess precision and a prototype, convert once to
3295 the required type rather than converting via the semantic
3296 type. Likewise without a prototype a float value represented
3297 as long double should be converted once to double. But for
3298 type-generic classification functions excess precision must
3300 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3301 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3303 val
= TREE_OPERAND (val
, 0);
3304 excess_precision
= true;
3306 val
= c_fully_fold (val
, false, NULL
);
3307 STRIP_TYPE_NOPS (val
);
3309 val
= require_complete_type (ploc
, val
);
3311 /* Some floating-point arguments must be promoted to double when
3312 no type is specified by a prototype. This applies to
3313 arguments of type float, and to architecture-specific types
3314 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3315 bool promote_float_arg
= false;
3316 if (type
== NULL_TREE
3317 && TREE_CODE (valtype
) == REAL_TYPE
3318 && (TYPE_PRECISION (valtype
)
3319 <= TYPE_PRECISION (double_type_node
))
3320 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3321 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3322 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3324 /* Promote this argument, unless it has a _FloatN or
3326 promote_float_arg
= true;
3327 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; i
++)
3328 if (TYPE_MAIN_VARIANT (valtype
) == FLOATN_NX_TYPE_NODE (i
))
3330 promote_float_arg
= false;
3337 /* Formal parm type is specified by a function prototype. */
3339 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3341 error_at (ploc
, "type of formal parameter %d is incomplete",
3349 /* Optionally warn about conversions that
3350 differ from the default conversions. */
3351 if (warn_traditional_conversion
|| warn_traditional
)
3353 unsigned int formal_prec
= TYPE_PRECISION (type
);
3355 if (INTEGRAL_TYPE_P (type
)
3356 && TREE_CODE (valtype
) == REAL_TYPE
)
3357 warning_at (ploc
, OPT_Wtraditional_conversion
,
3358 "passing argument %d of %qE as integer rather "
3359 "than floating due to prototype",
3361 if (INTEGRAL_TYPE_P (type
)
3362 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3363 warning_at (ploc
, OPT_Wtraditional_conversion
,
3364 "passing argument %d of %qE as integer rather "
3365 "than complex due to prototype",
3367 else if (TREE_CODE (type
) == COMPLEX_TYPE
3368 && TREE_CODE (valtype
) == REAL_TYPE
)
3369 warning_at (ploc
, OPT_Wtraditional_conversion
,
3370 "passing argument %d of %qE as complex rather "
3371 "than floating due to prototype",
3373 else if (TREE_CODE (type
) == REAL_TYPE
3374 && INTEGRAL_TYPE_P (valtype
))
3375 warning_at (ploc
, OPT_Wtraditional_conversion
,
3376 "passing argument %d of %qE as floating rather "
3377 "than integer due to prototype",
3379 else if (TREE_CODE (type
) == COMPLEX_TYPE
3380 && INTEGRAL_TYPE_P (valtype
))
3381 warning_at (ploc
, OPT_Wtraditional_conversion
,
3382 "passing argument %d of %qE as complex rather "
3383 "than integer due to prototype",
3385 else if (TREE_CODE (type
) == REAL_TYPE
3386 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3387 warning_at (ploc
, OPT_Wtraditional_conversion
,
3388 "passing argument %d of %qE as floating rather "
3389 "than complex due to prototype",
3391 /* ??? At some point, messages should be written about
3392 conversions between complex types, but that's too messy
3394 else if (TREE_CODE (type
) == REAL_TYPE
3395 && TREE_CODE (valtype
) == REAL_TYPE
)
3397 /* Warn if any argument is passed as `float',
3398 since without a prototype it would be `double'. */
3399 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3400 && type
!= dfloat32_type_node
)
3401 warning_at (ploc
, 0,
3402 "passing argument %d of %qE as %<float%> "
3403 "rather than %<double%> due to prototype",
3406 /* Warn if mismatch between argument and prototype
3407 for decimal float types. Warn of conversions with
3408 binary float types and of precision narrowing due to
3410 else if (type
!= valtype
3411 && (type
== dfloat32_type_node
3412 || type
== dfloat64_type_node
3413 || type
== dfloat128_type_node
3414 || valtype
== dfloat32_type_node
3415 || valtype
== dfloat64_type_node
3416 || valtype
== dfloat128_type_node
)
3418 <= TYPE_PRECISION (valtype
)
3419 || (type
== dfloat128_type_node
3421 != dfloat64_type_node
3423 != dfloat32_type_node
)))
3424 || (type
== dfloat64_type_node
3426 != dfloat32_type_node
))))
3427 warning_at (ploc
, 0,
3428 "passing argument %d of %qE as %qT "
3429 "rather than %qT due to prototype",
3430 argnum
, rname
, type
, valtype
);
3433 /* Detect integer changing in width or signedness.
3434 These warnings are only activated with
3435 -Wtraditional-conversion, not with -Wtraditional. */
3436 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3437 && INTEGRAL_TYPE_P (valtype
))
3439 tree would_have_been
= default_conversion (val
);
3440 tree type1
= TREE_TYPE (would_have_been
);
3442 if (TREE_CODE (type
) == ENUMERAL_TYPE
3443 && (TYPE_MAIN_VARIANT (type
)
3444 == TYPE_MAIN_VARIANT (valtype
)))
3445 /* No warning if function asks for enum
3446 and the actual arg is that enum type. */
3448 else if (formal_prec
!= TYPE_PRECISION (type1
))
3449 warning_at (ploc
, OPT_Wtraditional_conversion
,
3450 "passing argument %d of %qE "
3451 "with different width due to prototype",
3453 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3455 /* Don't complain if the formal parameter type
3456 is an enum, because we can't tell now whether
3457 the value was an enum--even the same enum. */
3458 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3460 else if (TREE_CODE (val
) == INTEGER_CST
3461 && int_fits_type_p (val
, type
))
3462 /* Change in signedness doesn't matter
3463 if a constant value is unaffected. */
3465 /* If the value is extended from a narrower
3466 unsigned type, it doesn't matter whether we
3467 pass it as signed or unsigned; the value
3468 certainly is the same either way. */
3469 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3470 && TYPE_UNSIGNED (valtype
))
3472 else if (TYPE_UNSIGNED (type
))
3473 warning_at (ploc
, OPT_Wtraditional_conversion
,
3474 "passing argument %d of %qE "
3475 "as unsigned due to prototype",
3478 warning_at (ploc
, OPT_Wtraditional_conversion
,
3479 "passing argument %d of %qE "
3480 "as signed due to prototype",
3485 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3486 sake of better warnings from convert_and_check. */
3487 if (excess_precision
)
3488 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3489 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3490 parmval
= convert_for_assignment (loc
, ploc
, type
,
3491 val
, origtype
, ic_argpass
,
3492 npc
, fundecl
, function
,
3495 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3496 && INTEGRAL_TYPE_P (type
)
3497 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3498 parmval
= default_conversion (parmval
);
3501 else if (promote_float_arg
)
3507 /* Convert `float' to `double'. */
3508 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3509 warning_at (ploc
, OPT_Wdouble_promotion
,
3510 "implicit conversion from %qT to %qT when passing "
3511 "argument to function",
3512 valtype
, double_type_node
);
3513 parmval
= convert (double_type_node
, val
);
3516 else if ((excess_precision
&& !type_generic
)
3517 || (type_generic_overflow_p
&& parmnum
== 2))
3518 /* A "double" argument with excess precision being passed
3519 without a prototype or in variable arguments.
3520 The last argument of __builtin_*_overflow_p should not be
3522 parmval
= convert (valtype
, val
);
3523 else if ((invalid_func_diag
=
3524 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3526 error (invalid_func_diag
);
3529 else if (TREE_CODE (val
) == ADDR_EXPR
&& reject_gcc_builtin (val
))
3534 /* Convert `short' and `char' to full-size `int'. */
3535 parmval
= default_conversion (val
);
3537 (*values
)[parmnum
] = parmval
;
3538 if (parmval
== error_mark_node
)
3542 typetail
= TREE_CHAIN (typetail
);
3545 gcc_assert (parmnum
== vec_safe_length (values
));
3547 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3549 error_at (loc
, "too few arguments to function %qE", function
);
3550 inform_declaration (fundecl
);
3554 return error_args
? -1 : (int) parmnum
;
3557 /* This is the entry point used by the parser to build unary operators
3558 in the input. CODE, a tree_code, specifies the unary operator, and
3559 ARG is the operand. For unary plus, the C parser currently uses
3560 CONVERT_EXPR for code.
3562 LOC is the location to use for the tree generated.
3566 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3568 struct c_expr result
;
3570 result
.original_code
= code
;
3571 result
.original_type
= NULL
;
3573 if (reject_gcc_builtin (arg
.value
))
3575 result
.value
= error_mark_node
;
3579 result
.value
= build_unary_op (loc
, code
, arg
.value
, false);
3581 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3582 overflow_warning (loc
, result
.value
);
3585 /* We are typically called when parsing a prefix token at LOC acting on
3586 ARG. Reflect this by updating the source range of the result to
3587 start at LOC and end at the end of ARG. */
3588 set_c_expr_source_range (&result
,
3589 loc
, arg
.get_finish ());
3594 /* This is the entry point used by the parser to build binary operators
3595 in the input. CODE, a tree_code, specifies the binary operator, and
3596 ARG1 and ARG2 are the operands. In addition to constructing the
3597 expression, we check for operands that were written with other binary
3598 operators in a way that is likely to confuse the user.
3600 LOCATION is the location of the binary operator. */
3603 parser_build_binary_op (location_t location
, enum tree_code code
,
3604 struct c_expr arg1
, struct c_expr arg2
)
3606 struct c_expr result
;
3608 enum tree_code code1
= arg1
.original_code
;
3609 enum tree_code code2
= arg2
.original_code
;
3610 tree type1
= (arg1
.original_type
3611 ? arg1
.original_type
3612 : TREE_TYPE (arg1
.value
));
3613 tree type2
= (arg2
.original_type
3614 ? arg2
.original_type
3615 : TREE_TYPE (arg2
.value
));
3617 result
.value
= build_binary_op (location
, code
,
3618 arg1
.value
, arg2
.value
, 1);
3619 result
.original_code
= code
;
3620 result
.original_type
= NULL
;
3622 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3624 set_c_expr_source_range (&result
,
3626 arg2
.get_finish ());
3630 if (location
!= UNKNOWN_LOCATION
)
3631 protected_set_expr_location (result
.value
, location
);
3633 set_c_expr_source_range (&result
,
3635 arg2
.get_finish ());
3637 /* Check for cases such as x+y<<z which users are likely
3639 if (warn_parentheses
)
3640 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3643 if (warn_logical_op
)
3644 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3645 code1
, arg1
.value
, code2
, arg2
.value
);
3647 if (warn_tautological_compare
)
3649 tree lhs
= arg1
.value
;
3650 tree rhs
= arg2
.value
;
3651 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
3653 if (C_MAYBE_CONST_EXPR_PRE (lhs
) != NULL_TREE
3654 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs
)))
3657 lhs
= C_MAYBE_CONST_EXPR_EXPR (lhs
);
3659 if (TREE_CODE (rhs
) == C_MAYBE_CONST_EXPR
)
3661 if (C_MAYBE_CONST_EXPR_PRE (rhs
) != NULL_TREE
3662 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs
)))
3665 rhs
= C_MAYBE_CONST_EXPR_EXPR (rhs
);
3667 if (lhs
!= NULL_TREE
&& rhs
!= NULL_TREE
)
3668 warn_tautological_cmp (location
, code
, lhs
, rhs
);
3671 if (warn_logical_not_paren
3672 && TREE_CODE_CLASS (code
) == tcc_comparison
3673 && code1
== TRUTH_NOT_EXPR
3674 && code2
!= TRUTH_NOT_EXPR
3675 /* Avoid warning for !!x == y. */
3676 && (TREE_CODE (arg1
.value
) != NE_EXPR
3677 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
3679 /* Avoid warning for !b == y where b has _Bool type. */
3680 tree t
= integer_zero_node
;
3681 if (TREE_CODE (arg1
.value
) == EQ_EXPR
3682 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
3683 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
3685 t
= TREE_OPERAND (arg1
.value
, 0);
3688 if (TREE_TYPE (t
) != integer_type_node
)
3690 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
3691 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
3692 else if (CONVERT_EXPR_P (t
))
3693 t
= TREE_OPERAND (t
, 0);
3699 if (TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
)
3700 warn_logical_not_parentheses (location
, code
, arg1
.value
, arg2
.value
);
3703 /* Warn about comparisons against string literals, with the exception
3704 of testing for equality or inequality of a string literal with NULL. */
3705 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3707 if ((code1
== STRING_CST
3708 && !integer_zerop (tree_strip_nop_conversions (arg2
.value
)))
3709 || (code2
== STRING_CST
3710 && !integer_zerop (tree_strip_nop_conversions (arg1
.value
))))
3711 warning_at (location
, OPT_Waddress
,
3712 "comparison with string literal results in unspecified behavior");
3714 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3715 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3716 warning_at (location
, OPT_Waddress
,
3717 "comparison with string literal results in unspecified behavior");
3719 if (TREE_OVERFLOW_P (result
.value
)
3720 && !TREE_OVERFLOW_P (arg1
.value
)
3721 && !TREE_OVERFLOW_P (arg2
.value
))
3722 overflow_warning (location
, result
.value
);
3724 /* Warn about comparisons of different enum types. */
3725 if (warn_enum_compare
3726 && TREE_CODE_CLASS (code
) == tcc_comparison
3727 && TREE_CODE (type1
) == ENUMERAL_TYPE
3728 && TREE_CODE (type2
) == ENUMERAL_TYPE
3729 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3730 warning_at (location
, OPT_Wenum_compare
,
3731 "comparison between %qT and %qT",
3737 /* Return a tree for the difference of pointers OP0 and OP1.
3738 The resulting tree has type int. */
3741 pointer_diff (location_t loc
, tree op0
, tree op1
)
3743 tree restype
= ptrdiff_type_node
;
3744 tree result
, inttype
;
3746 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3747 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3748 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3749 tree orig_op1
= op1
;
3751 /* If the operands point into different address spaces, we need to
3752 explicitly convert them to pointers into the common address space
3753 before we can subtract the numerical address values. */
3756 addr_space_t as_common
;
3759 /* Determine the common superset address space. This is guaranteed
3760 to exist because the caller verified that comp_target_types
3761 returned non-zero. */
3762 if (!addr_space_superset (as0
, as1
, &as_common
))
3765 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3766 op0
= convert (common_type
, op0
);
3767 op1
= convert (common_type
, op1
);
3770 /* Determine integer type to perform computations in. This will usually
3771 be the same as the result type (ptrdiff_t), but may need to be a wider
3772 type if pointers for the address space are wider than ptrdiff_t. */
3773 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3774 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3778 if (TREE_CODE (target_type
) == VOID_TYPE
)
3779 pedwarn (loc
, OPT_Wpointer_arith
,
3780 "pointer of type %<void *%> used in subtraction");
3781 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3782 pedwarn (loc
, OPT_Wpointer_arith
,
3783 "pointer to a function used in subtraction");
3785 /* First do the subtraction as integers;
3786 then drop through to build the divide operator.
3787 Do not do default conversions on the minus operator
3788 in case restype is a short type. */
3790 op0
= build_binary_op (loc
,
3791 MINUS_EXPR
, convert (inttype
, op0
),
3792 convert (inttype
, op1
), 0);
3793 /* This generates an error if op1 is pointer to incomplete type. */
3794 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3795 error_at (loc
, "arithmetic on pointer to an incomplete type");
3797 op1
= c_size_in_bytes (target_type
);
3799 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3800 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3802 /* Divide by the size, in easiest possible way. */
3803 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3804 op0
, convert (inttype
, op1
));
3806 /* Convert to final result type if necessary. */
3807 return convert (restype
, result
);
3810 /* Expand atomic compound assignments into an appropriate sequence as
3811 specified by the C11 standard section 6.5.16.2.
3817 This sequence is used for all types for which these operations are
3820 In addition, built-in versions of the 'fe' prefixed routines may
3821 need to be invoked for floating point (real, complex or vector) when
3822 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3832 __atomic_load (addr, &old, SEQ_CST);
3833 feholdexcept (&fenv);
3835 newval = old op val;
3836 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3839 feclearexcept (FE_ALL_EXCEPT);
3842 feupdateenv (&fenv);
3844 The compiler will issue the __atomic_fetch_* built-in when possible,
3845 otherwise it will generate the generic form of the atomic operations.
3846 This requires temp(s) and has their address taken. The atomic processing
3847 is smart enough to figure out when the size of an object can utilize
3848 a lock-free version, and convert the built-in call to the appropriate
3849 lock-free routine. The optimizers will then dispose of any temps that
3850 are no longer required, and lock-free implementations are utilized as
3851 long as there is target support for the required size.
3853 If the operator is NOP_EXPR, then this is a simple assignment, and
3854 an __atomic_store is issued to perform the assignment rather than
3857 /* Build an atomic assignment at LOC, expanding into the proper
3858 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3859 the result of the operation, unless RETURN_OLD_P, in which case
3860 return the old value of LHS (this is only for postincrement and
3864 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3865 tree rhs
, bool return_old_p
)
3867 tree fndecl
, func_call
;
3868 vec
<tree
, va_gc
> *params
;
3869 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3872 tree stmt
, goto_stmt
;
3873 tree loop_label
, loop_decl
, done_label
, done_decl
;
3875 tree lhs_type
= TREE_TYPE (lhs
);
3876 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, false);
3877 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3878 tree rhs_type
= TREE_TYPE (rhs
);
3880 gcc_assert (TYPE_ATOMIC (lhs_type
));
3883 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3885 /* Allocate enough vector items for a compare_exchange. */
3886 vec_alloc (params
, 6);
3888 /* Create a compound statement to hold the sequence of statements
3890 compound_stmt
= c_begin_compound_stmt (false);
3892 /* Fold the RHS if it hasn't already been folded. */
3893 if (modifycode
!= NOP_EXPR
)
3894 rhs
= c_fully_fold (rhs
, false, NULL
);
3896 /* Remove the qualifiers for the rest of the expressions and create
3897 the VAL temp variable to hold the RHS. */
3898 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3899 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3900 val
= create_tmp_var_raw (nonatomic_rhs_type
);
3901 TREE_ADDRESSABLE (val
) = 1;
3902 TREE_NO_WARNING (val
) = 1;
3903 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
3905 SET_EXPR_LOCATION (rhs
, loc
);
3908 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3910 if (modifycode
== NOP_EXPR
)
3912 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3913 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, false);
3914 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3915 params
->quick_push (lhs_addr
);
3916 params
->quick_push (rhs
);
3917 params
->quick_push (seq_cst
);
3918 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3919 add_stmt (func_call
);
3921 /* Finish the compound statement. */
3922 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3924 /* VAL is the value which was stored, return a COMPOUND_STMT of
3925 the statement and that value. */
3926 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3929 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3930 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3931 isn't applicable for such builtins. ??? Do we want to handle enums? */
3932 if ((TREE_CODE (lhs_type
) == INTEGER_TYPE
|| POINTER_TYPE_P (lhs_type
))
3933 && TREE_CODE (rhs_type
) == INTEGER_TYPE
)
3935 built_in_function fncode
;
3939 case POINTER_PLUS_EXPR
:
3940 fncode
= (return_old_p
3941 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3942 : BUILT_IN_ATOMIC_ADD_FETCH_N
);
3945 fncode
= (return_old_p
3946 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3947 : BUILT_IN_ATOMIC_SUB_FETCH_N
);
3950 fncode
= (return_old_p
3951 ? BUILT_IN_ATOMIC_FETCH_AND_N
3952 : BUILT_IN_ATOMIC_AND_FETCH_N
);
3955 fncode
= (return_old_p
3956 ? BUILT_IN_ATOMIC_FETCH_OR_N
3957 : BUILT_IN_ATOMIC_OR_FETCH_N
);
3960 fncode
= (return_old_p
3961 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3962 : BUILT_IN_ATOMIC_XOR_FETCH_N
);
3968 /* We can only use "_1" through "_16" variants of the atomic fetch
3970 unsigned HOST_WIDE_INT size
= tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type
));
3971 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8 && size
!= 16)
3974 /* If this is a pointer type, we need to multiply by the size of
3975 the pointer target type. */
3976 if (POINTER_TYPE_P (lhs_type
))
3978 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type
))
3979 /* ??? This would introduce -Wdiscarded-qualifiers
3980 warning: __atomic_fetch_* expect volatile void *
3981 type as the first argument. (Assignments between
3982 atomic and non-atomic objects are OK.) */
3983 || TYPE_RESTRICT (lhs_type
))
3985 tree sz
= TYPE_SIZE_UNIT (TREE_TYPE (lhs_type
));
3986 rhs
= fold_build2_loc (loc
, MULT_EXPR
, ptrdiff_type_node
,
3987 convert (ptrdiff_type_node
, rhs
),
3988 convert (ptrdiff_type_node
, sz
));
3991 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3992 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3993 fndecl
= builtin_decl_explicit (fncode
);
3994 params
->quick_push (lhs_addr
);
3995 params
->quick_push (rhs
);
3996 params
->quick_push (seq_cst
);
3997 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3999 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4000 TREE_ADDRESSABLE (newval
) = 1;
4001 TREE_NO_WARNING (newval
) = 1;
4002 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, func_call
,
4003 NULL_TREE
, NULL_TREE
);
4004 SET_EXPR_LOCATION (rhs
, loc
);
4007 /* Finish the compound statement. */
4008 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4010 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4011 the statement and that value. */
4012 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, newval
);
4016 /* Create the variables and labels required for the op= form. */
4017 old
= create_tmp_var_raw (nonatomic_lhs_type
);
4018 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, false);
4019 TREE_ADDRESSABLE (old
) = 1;
4020 TREE_NO_WARNING (old
) = 1;
4022 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
4023 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, false);
4024 TREE_ADDRESSABLE (newval
) = 1;
4025 TREE_NO_WARNING (newval
) = 1;
4027 loop_decl
= create_artificial_label (loc
);
4028 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
4030 done_decl
= create_artificial_label (loc
);
4031 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
4033 /* __atomic_load (addr, &old, SEQ_CST). */
4034 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
4035 params
->quick_push (lhs_addr
);
4036 params
->quick_push (old_addr
);
4037 params
->quick_push (seq_cst
);
4038 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4039 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
4042 params
->truncate (0);
4044 /* Create the expressions for floating-point environment
4045 manipulation, if required. */
4046 bool need_fenv
= (flag_trapping_math
4047 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
4048 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
4050 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
4053 add_stmt (hold_call
);
4056 add_stmt (loop_label
);
4058 /* newval = old + val; */
4059 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
4060 rhs
= c_fully_fold (rhs
, false, NULL
);
4061 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
4062 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
4064 if (rhs
!= error_mark_node
)
4066 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
4068 SET_EXPR_LOCATION (rhs
, loc
);
4072 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4074 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
4075 params
->quick_push (lhs_addr
);
4076 params
->quick_push (old_addr
);
4077 params
->quick_push (newval_addr
);
4078 params
->quick_push (integer_zero_node
);
4079 params
->quick_push (seq_cst
);
4080 params
->quick_push (seq_cst
);
4081 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
4083 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
4084 SET_EXPR_LOCATION (goto_stmt
, loc
);
4086 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
4087 SET_EXPR_LOCATION (stmt
, loc
);
4091 add_stmt (clear_call
);
4094 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
4095 SET_EXPR_LOCATION (goto_stmt
, loc
);
4096 add_stmt (goto_stmt
);
4099 add_stmt (done_label
);
4102 add_stmt (update_call
);
4104 /* Finish the compound statement. */
4105 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
4107 /* NEWVAL is the value that was successfully stored, return a
4108 COMPOUND_EXPR of the statement and the appropriate value. */
4109 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
4110 return_old_p
? old
: newval
);
4113 /* Construct and perhaps optimize a tree representation
4114 for a unary operation. CODE, a tree_code, specifies the operation
4115 and XARG is the operand.
4116 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4117 promotions (such as from short to int).
4118 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4119 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4122 LOCATION is the location of the operator. */
4125 build_unary_op (location_t location
, enum tree_code code
, tree xarg
,
4128 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4131 enum tree_code typecode
;
4133 tree ret
= error_mark_node
;
4134 tree eptype
= NULL_TREE
;
4135 const char *invalid_op_diag
;
4138 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
4140 arg
= remove_c_maybe_const_expr (arg
);
4142 if (code
!= ADDR_EXPR
)
4143 arg
= require_complete_type (location
, arg
);
4145 typecode
= TREE_CODE (TREE_TYPE (arg
));
4146 if (typecode
== ERROR_MARK
)
4147 return error_mark_node
;
4148 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
4149 typecode
= INTEGER_TYPE
;
4151 if ((invalid_op_diag
4152 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
4154 error_at (location
, invalid_op_diag
);
4155 return error_mark_node
;
4158 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
4160 eptype
= TREE_TYPE (arg
);
4161 arg
= TREE_OPERAND (arg
, 0);
4167 /* This is used for unary plus, because a CONVERT_EXPR
4168 is enough to prevent anybody from looking inside for
4169 associativity, but won't generate any code. */
4170 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4171 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4172 || typecode
== VECTOR_TYPE
))
4174 error_at (location
, "wrong type argument to unary plus");
4175 return error_mark_node
;
4177 else if (!noconvert
)
4178 arg
= default_conversion (arg
);
4179 arg
= non_lvalue_loc (location
, arg
);
4183 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4184 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
4185 || typecode
== VECTOR_TYPE
))
4187 error_at (location
, "wrong type argument to unary minus");
4188 return error_mark_node
;
4190 else if (!noconvert
)
4191 arg
= default_conversion (arg
);
4195 /* ~ works on integer types and non float vectors. */
4196 if (typecode
== INTEGER_TYPE
4197 || (typecode
== VECTOR_TYPE
4198 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
4202 /* Warn if the expression has boolean value. */
4203 while (TREE_CODE (e
) == COMPOUND_EXPR
)
4204 e
= TREE_OPERAND (e
, 1);
4206 if ((TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
4207 || truth_value_p (TREE_CODE (e
)))
4208 && warning_at (location
, OPT_Wbool_operation
,
4209 "%<~%> on a boolean expression"))
4211 gcc_rich_location
richloc (location
);
4212 richloc
.add_fixit_insert_before (location
, "!");
4213 inform_at_rich_loc (&richloc
, "did you mean to use logical "
4217 arg
= default_conversion (arg
);
4219 else if (typecode
== COMPLEX_TYPE
)
4222 pedwarn (location
, OPT_Wpedantic
,
4223 "ISO C does not support %<~%> for complex conjugation");
4225 arg
= default_conversion (arg
);
4229 error_at (location
, "wrong type argument to bit-complement");
4230 return error_mark_node
;
4235 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
4237 error_at (location
, "wrong type argument to abs");
4238 return error_mark_node
;
4240 else if (!noconvert
)
4241 arg
= default_conversion (arg
);
4245 /* Conjugating a real value is a no-op, but allow it anyway. */
4246 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
4247 || typecode
== COMPLEX_TYPE
))
4249 error_at (location
, "wrong type argument to conjugation");
4250 return error_mark_node
;
4252 else if (!noconvert
)
4253 arg
= default_conversion (arg
);
4256 case TRUTH_NOT_EXPR
:
4257 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4258 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
4259 && typecode
!= COMPLEX_TYPE
)
4262 "wrong type argument to unary exclamation mark");
4263 return error_mark_node
;
4267 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
4268 arg
= remove_c_maybe_const_expr (arg
);
4271 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
4272 ret
= invert_truthvalue_loc (location
, arg
);
4273 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4274 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
4275 location
= EXPR_LOCATION (ret
);
4276 goto return_build_unary_op
;
4280 ret
= build_real_imag_expr (location
, code
, arg
);
4281 if (ret
== error_mark_node
)
4282 return error_mark_node
;
4283 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
4284 eptype
= TREE_TYPE (eptype
);
4285 goto return_build_unary_op
;
4287 case PREINCREMENT_EXPR
:
4288 case POSTINCREMENT_EXPR
:
4289 case PREDECREMENT_EXPR
:
4290 case POSTDECREMENT_EXPR
:
4292 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4294 tree inner
= build_unary_op (location
, code
,
4295 C_MAYBE_CONST_EXPR_EXPR (arg
),
4297 if (inner
== error_mark_node
)
4298 return error_mark_node
;
4299 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4300 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4301 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4302 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
4303 goto return_build_unary_op
;
4306 /* Complain about anything that is not a true lvalue. In
4307 Objective-C, skip this check for property_refs. */
4308 if (!objc_is_property_ref (arg
)
4309 && !lvalue_or_else (location
,
4310 arg
, ((code
== PREINCREMENT_EXPR
4311 || code
== POSTINCREMENT_EXPR
)
4314 return error_mark_node
;
4316 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
4318 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4319 warning_at (location
, OPT_Wc___compat
,
4320 "increment of enumeration value is invalid in C++");
4322 warning_at (location
, OPT_Wc___compat
,
4323 "decrement of enumeration value is invalid in C++");
4326 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4328 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4329 warning_at (location
, OPT_Wbool_operation
,
4330 "increment of a boolean expression");
4332 warning_at (location
, OPT_Wbool_operation
,
4333 "decrement of a boolean expression");
4336 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4337 arg
= c_fully_fold (arg
, false, NULL
);
4340 atomic_op
= really_atomic_lvalue (arg
);
4342 /* Increment or decrement the real part of the value,
4343 and don't change the imaginary part. */
4344 if (typecode
== COMPLEX_TYPE
)
4348 pedwarn (location
, OPT_Wpedantic
,
4349 "ISO C does not support %<++%> and %<--%> on complex types");
4353 arg
= stabilize_reference (arg
);
4354 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
,
4356 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
,
4358 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, true);
4359 if (real
== error_mark_node
|| imag
== error_mark_node
)
4360 return error_mark_node
;
4361 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4363 goto return_build_unary_op
;
4367 /* Report invalid types. */
4369 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4370 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4371 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
4373 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4374 error_at (location
, "wrong type argument to increment");
4376 error_at (location
, "wrong type argument to decrement");
4378 return error_mark_node
;
4384 argtype
= TREE_TYPE (arg
);
4386 /* Compute the increment. */
4388 if (typecode
== POINTER_TYPE
)
4390 /* If pointer target is an incomplete type,
4391 we just cannot know how to do the arithmetic. */
4392 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4394 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4396 "increment of pointer to an incomplete type %qT",
4397 TREE_TYPE (argtype
));
4400 "decrement of pointer to an incomplete type %qT",
4401 TREE_TYPE (argtype
));
4403 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4404 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4406 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4407 pedwarn (location
, OPT_Wpointer_arith
,
4408 "wrong type argument to increment");
4410 pedwarn (location
, OPT_Wpointer_arith
,
4411 "wrong type argument to decrement");
4414 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4415 inc
= convert_to_ptrofftype_loc (location
, inc
);
4417 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4419 /* For signed fract types, we invert ++ to -- or
4420 -- to ++, and change inc from 1 to -1, because
4421 it is not possible to represent 1 in signed fract constants.
4422 For unsigned fract types, the result always overflows and
4423 we get an undefined (original) or the maximum value. */
4424 if (code
== PREINCREMENT_EXPR
)
4425 code
= PREDECREMENT_EXPR
;
4426 else if (code
== PREDECREMENT_EXPR
)
4427 code
= PREINCREMENT_EXPR
;
4428 else if (code
== POSTINCREMENT_EXPR
)
4429 code
= POSTDECREMENT_EXPR
;
4430 else /* code == POSTDECREMENT_EXPR */
4431 code
= POSTINCREMENT_EXPR
;
4433 inc
= integer_minus_one_node
;
4434 inc
= convert (argtype
, inc
);
4438 inc
= VECTOR_TYPE_P (argtype
)
4439 ? build_one_cst (argtype
)
4441 inc
= convert (argtype
, inc
);
4444 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4445 need to ask Objective-C to build the increment or decrement
4446 expression for it. */
4447 if (objc_is_property_ref (arg
))
4448 return objc_build_incr_expr_for_property_ref (location
, code
,
4451 /* Report a read-only lvalue. */
4452 if (TYPE_READONLY (argtype
))
4454 readonly_error (location
, arg
,
4455 ((code
== PREINCREMENT_EXPR
4456 || code
== POSTINCREMENT_EXPR
)
4457 ? lv_increment
: lv_decrement
));
4458 return error_mark_node
;
4460 else if (TREE_READONLY (arg
))
4461 readonly_warning (arg
,
4462 ((code
== PREINCREMENT_EXPR
4463 || code
== POSTINCREMENT_EXPR
)
4464 ? lv_increment
: lv_decrement
));
4466 /* If the argument is atomic, use the special code sequences for
4467 atomic compound assignment. */
4470 arg
= stabilize_reference (arg
);
4471 ret
= build_atomic_assign (location
, arg
,
4472 ((code
== PREINCREMENT_EXPR
4473 || code
== POSTINCREMENT_EXPR
)
4476 (FRACT_MODE_P (TYPE_MODE (argtype
))
4478 : integer_one_node
),
4479 (code
== POSTINCREMENT_EXPR
4480 || code
== POSTDECREMENT_EXPR
));
4481 goto return_build_unary_op
;
4484 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4485 val
= boolean_increment (code
, arg
);
4487 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4488 TREE_SIDE_EFFECTS (val
) = 1;
4489 if (TREE_CODE (val
) != code
)
4490 TREE_NO_WARNING (val
) = 1;
4492 goto return_build_unary_op
;
4496 /* Note that this operation never does default_conversion. */
4498 /* The operand of unary '&' must be an lvalue (which excludes
4499 expressions of type void), or, in C99, the result of a [] or
4500 unary '*' operator. */
4501 if (VOID_TYPE_P (TREE_TYPE (arg
))
4502 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4503 && (!INDIRECT_REF_P (arg
) || !flag_isoc99
))
4504 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4506 /* Let &* cancel out to simplify resulting code. */
4507 if (INDIRECT_REF_P (arg
))
4509 /* Don't let this be an lvalue. */
4510 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4511 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4512 ret
= TREE_OPERAND (arg
, 0);
4513 goto return_build_unary_op
;
4516 /* Anything not already handled and not a true memory reference
4517 or a non-lvalue array is an error. */
4518 if (typecode
!= FUNCTION_TYPE
&& !noconvert
4519 && !lvalue_or_else (location
, arg
, lv_addressof
))
4520 return error_mark_node
;
4522 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4524 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4526 tree inner
= build_unary_op (location
, code
,
4527 C_MAYBE_CONST_EXPR_EXPR (arg
),
4529 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4530 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4531 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4532 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4533 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4534 goto return_build_unary_op
;
4537 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4538 argtype
= TREE_TYPE (arg
);
4540 /* If the lvalue is const or volatile, merge that into the type
4541 to which the address will point. This is only needed
4542 for function types. */
4543 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4544 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4545 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4547 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4548 int quals
= orig_quals
;
4550 if (TREE_READONLY (arg
))
4551 quals
|= TYPE_QUAL_CONST
;
4552 if (TREE_THIS_VOLATILE (arg
))
4553 quals
|= TYPE_QUAL_VOLATILE
;
4555 argtype
= c_build_qualified_type (argtype
, quals
);
4558 switch (TREE_CODE (arg
))
4561 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
4563 error_at (location
, "cannot take address of bit-field %qD",
4564 TREE_OPERAND (arg
, 1));
4565 return error_mark_node
;
4571 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg
, 0))))
4573 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg
))
4574 && !VECTOR_TYPE_P (TREE_TYPE (arg
)))
4576 error_at (location
, "cannot take address of scalar with "
4577 "reverse storage order");
4578 return error_mark_node
;
4581 if (TREE_CODE (TREE_TYPE (arg
)) == ARRAY_TYPE
4582 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg
)))
4583 warning_at (location
, OPT_Wscalar_storage_order
,
4584 "address of array with reverse scalar storage "
4592 if (!c_mark_addressable (arg
))
4593 return error_mark_node
;
4595 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4596 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4598 argtype
= build_pointer_type (argtype
);
4600 /* ??? Cope with user tricks that amount to offsetof. Delete this
4601 when we have proper support for integer constant expressions. */
4602 val
= get_base_address (arg
);
4603 if (val
&& INDIRECT_REF_P (val
)
4604 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4606 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4607 goto return_build_unary_op
;
4610 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4613 goto return_build_unary_op
;
4620 argtype
= TREE_TYPE (arg
);
4621 if (TREE_CODE (arg
) == INTEGER_CST
)
4622 ret
= (require_constant_value
4623 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4624 : fold_build1_loc (location
, code
, argtype
, arg
));
4626 ret
= build1 (code
, argtype
, arg
);
4627 return_build_unary_op
:
4628 gcc_assert (ret
!= error_mark_node
);
4629 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4630 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4631 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4632 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4633 ret
= note_integer_operands (ret
);
4635 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4636 protected_set_expr_location (ret
, location
);
4640 /* Return nonzero if REF is an lvalue valid for this language.
4641 Lvalues can be assigned, unless their type has TYPE_READONLY.
4642 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4645 lvalue_p (const_tree ref
)
4647 const enum tree_code code
= TREE_CODE (ref
);
4654 return lvalue_p (TREE_OPERAND (ref
, 0));
4656 case C_MAYBE_CONST_EXPR
:
4657 return lvalue_p (TREE_OPERAND (ref
, 1));
4659 case COMPOUND_LITERAL_EXPR
:
4665 case ARRAY_NOTATION_REF
:
4670 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4671 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4674 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4681 /* Give a warning for storing in something that is read-only in GCC
4682 terms but not const in ISO C terms. */
4685 readonly_warning (tree arg
, enum lvalue_use use
)
4690 warning (0, "assignment of read-only location %qE", arg
);
4693 warning (0, "increment of read-only location %qE", arg
);
4696 warning (0, "decrement of read-only location %qE", arg
);
4705 /* Return nonzero if REF is an lvalue valid for this language;
4706 otherwise, print an error message and return zero. USE says
4707 how the lvalue is being used and so selects the error message.
4708 LOCATION is the location at which any error should be reported. */
4711 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4713 int win
= lvalue_p (ref
);
4716 lvalue_error (loc
, use
);
4721 /* Mark EXP saying that we need to be able to take the
4722 address of it; it should not be allocated in a register.
4723 Returns true if successful. */
4726 c_mark_addressable (tree exp
)
4731 switch (TREE_CODE (x
))
4738 x
= TREE_OPERAND (x
, 0);
4741 case COMPOUND_LITERAL_EXPR
:
4743 TREE_ADDRESSABLE (x
) = 1;
4750 if (C_DECL_REGISTER (x
)
4751 && DECL_NONLOCAL (x
))
4753 if (TREE_PUBLIC (x
) || is_global_var (x
))
4756 ("global register variable %qD used in nested function", x
);
4759 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4761 else if (C_DECL_REGISTER (x
))
4763 if (TREE_PUBLIC (x
) || is_global_var (x
))
4764 error ("address of global register variable %qD requested", x
);
4766 error ("address of register variable %qD requested", x
);
4772 TREE_ADDRESSABLE (x
) = 1;
4779 /* Convert EXPR to TYPE, warning about conversion problems with
4780 constants. SEMANTIC_TYPE is the type this conversion would use
4781 without excess precision. If SEMANTIC_TYPE is NULL, this function
4782 is equivalent to convert_and_check. This function is a wrapper that
4783 handles conversions that may be different than
4784 the usual ones because of excess precision. */
4787 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4790 if (TREE_TYPE (expr
) == type
)
4794 return convert_and_check (loc
, type
, expr
);
4796 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4797 && TREE_TYPE (expr
) != semantic_type
)
4799 /* For integers, we need to check the real conversion, not
4800 the conversion to the excess precision type. */
4801 expr
= convert_and_check (loc
, semantic_type
, expr
);
4803 /* Result type is the excess precision type, which should be
4804 large enough, so do not check. */
4805 return convert (type
, expr
);
4808 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4809 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4810 if folded to an integer constant then the unselected half may
4811 contain arbitrary operations not normally permitted in constant
4812 expressions. Set the location of the expression to LOC. */
4815 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4816 tree op1
, tree op1_original_type
, tree op2
,
4817 tree op2_original_type
)
4821 enum tree_code code1
;
4822 enum tree_code code2
;
4823 tree result_type
= NULL
;
4824 tree semantic_result_type
= NULL
;
4825 tree orig_op1
= op1
, orig_op2
= op2
;
4826 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4827 bool ifexp_int_operands
;
4830 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4831 if (op1_int_operands
)
4832 op1
= remove_c_maybe_const_expr (op1
);
4833 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4834 if (op2_int_operands
)
4835 op2
= remove_c_maybe_const_expr (op2
);
4836 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4837 if (ifexp_int_operands
)
4838 ifexp
= remove_c_maybe_const_expr (ifexp
);
4840 /* Promote both alternatives. */
4842 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4843 op1
= default_conversion (op1
);
4844 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4845 op2
= default_conversion (op2
);
4847 if (TREE_CODE (ifexp
) == ERROR_MARK
4848 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4849 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4850 return error_mark_node
;
4852 type1
= TREE_TYPE (op1
);
4853 code1
= TREE_CODE (type1
);
4854 type2
= TREE_TYPE (op2
);
4855 code2
= TREE_CODE (type2
);
4857 if (code1
== POINTER_TYPE
&& reject_gcc_builtin (op1
))
4858 return error_mark_node
;
4860 if (code2
== POINTER_TYPE
&& reject_gcc_builtin (op2
))
4861 return error_mark_node
;
4863 /* C90 does not permit non-lvalue arrays in conditional expressions.
4864 In C99 they will be pointers by now. */
4865 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4867 error_at (colon_loc
, "non-lvalue array in conditional expression");
4868 return error_mark_node
;
4871 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4872 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4873 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4874 || code1
== COMPLEX_TYPE
)
4875 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4876 || code2
== COMPLEX_TYPE
))
4878 semantic_result_type
= c_common_type (type1
, type2
);
4879 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4881 op1
= TREE_OPERAND (op1
, 0);
4882 type1
= TREE_TYPE (op1
);
4883 gcc_assert (TREE_CODE (type1
) == code1
);
4885 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4887 op2
= TREE_OPERAND (op2
, 0);
4888 type2
= TREE_TYPE (op2
);
4889 gcc_assert (TREE_CODE (type2
) == code2
);
4893 if (warn_cxx_compat
)
4895 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4896 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4898 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4899 && TREE_CODE (t2
) == ENUMERAL_TYPE
4900 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4901 warning_at (colon_loc
, OPT_Wc___compat
,
4902 ("different enum types in conditional is "
4903 "invalid in C++: %qT vs %qT"),
4907 /* Quickly detect the usual case where op1 and op2 have the same type
4909 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4912 result_type
= type1
;
4914 result_type
= TYPE_MAIN_VARIANT (type1
);
4916 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4917 || code1
== COMPLEX_TYPE
)
4918 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4919 || code2
== COMPLEX_TYPE
))
4921 result_type
= c_common_type (type1
, type2
);
4922 if (result_type
== error_mark_node
)
4923 return error_mark_node
;
4924 do_warn_double_promotion (result_type
, type1
, type2
,
4925 "implicit conversion from %qT to %qT to "
4926 "match other result of conditional",
4929 /* If -Wsign-compare, warn here if type1 and type2 have
4930 different signedness. We'll promote the signed to unsigned
4931 and later code won't know it used to be different.
4932 Do this check on the original types, so that explicit casts
4933 will be considered, but default promotions won't. */
4934 if (c_inhibit_evaluation_warnings
== 0)
4936 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4937 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4939 if (unsigned_op1
^ unsigned_op2
)
4943 /* Do not warn if the result type is signed, since the
4944 signed type will only be chosen if it can represent
4945 all the values of the unsigned type. */
4946 if (!TYPE_UNSIGNED (result_type
))
4950 bool op1_maybe_const
= true;
4951 bool op2_maybe_const
= true;
4953 /* Do not warn if the signed quantity is an
4954 unsuffixed integer literal (or some static
4955 constant expression involving such literals) and
4956 it is non-negative. This warning requires the
4957 operands to be folded for best results, so do
4958 that folding in this case even without
4959 warn_sign_compare to avoid warning options
4960 possibly affecting code generation. */
4961 c_inhibit_evaluation_warnings
4962 += (ifexp
== truthvalue_false_node
);
4963 op1
= c_fully_fold (op1
, require_constant_value
,
4965 c_inhibit_evaluation_warnings
4966 -= (ifexp
== truthvalue_false_node
);
4968 c_inhibit_evaluation_warnings
4969 += (ifexp
== truthvalue_true_node
);
4970 op2
= c_fully_fold (op2
, require_constant_value
,
4972 c_inhibit_evaluation_warnings
4973 -= (ifexp
== truthvalue_true_node
);
4975 if (warn_sign_compare
)
4978 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4980 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4983 warning_at (colon_loc
, OPT_Wsign_compare
,
4984 ("signed and unsigned type in "
4985 "conditional expression"));
4987 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4988 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4989 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4990 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4995 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4997 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4998 pedwarn (colon_loc
, OPT_Wpedantic
,
4999 "ISO C forbids conditional expr with only one void side");
5000 result_type
= void_type_node
;
5002 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
5004 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
5005 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
5006 addr_space_t as_common
;
5008 if (comp_target_types (colon_loc
, type1
, type2
))
5009 result_type
= common_pointer_type (type1
, type2
);
5010 else if (null_pointer_constant_p (orig_op1
))
5011 result_type
= type2
;
5012 else if (null_pointer_constant_p (orig_op2
))
5013 result_type
= type1
;
5014 else if (!addr_space_superset (as1
, as2
, &as_common
))
5016 error_at (colon_loc
, "pointers to disjoint address spaces "
5017 "used in conditional expression");
5018 return error_mark_node
;
5020 else if (VOID_TYPE_P (TREE_TYPE (type1
))
5021 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
5023 if ((TREE_CODE (TREE_TYPE (type2
)) == ARRAY_TYPE
)
5024 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2
)))
5025 & ~TYPE_QUALS (TREE_TYPE (type1
))))
5026 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5027 "pointer to array loses qualifier "
5028 "in conditional expression");
5030 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
5031 pedwarn (colon_loc
, OPT_Wpedantic
,
5032 "ISO C forbids conditional expr between "
5033 "%<void *%> and function pointer");
5034 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
5035 TREE_TYPE (type2
)));
5037 else if (VOID_TYPE_P (TREE_TYPE (type2
))
5038 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
5040 if ((TREE_CODE (TREE_TYPE (type1
)) == ARRAY_TYPE
)
5041 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1
)))
5042 & ~TYPE_QUALS (TREE_TYPE (type2
))))
5043 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
5044 "pointer to array loses qualifier "
5045 "in conditional expression");
5047 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
5048 pedwarn (colon_loc
, OPT_Wpedantic
,
5049 "ISO C forbids conditional expr between "
5050 "%<void *%> and function pointer");
5051 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
5052 TREE_TYPE (type1
)));
5054 /* Objective-C pointer comparisons are a bit more lenient. */
5055 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
5056 result_type
= objc_common_type (type1
, type2
);
5059 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
5061 pedwarn (colon_loc
, 0,
5062 "pointer type mismatch in conditional expression");
5063 result_type
= build_pointer_type
5064 (build_qualified_type (void_type_node
, qual
));
5067 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
5069 if (!null_pointer_constant_p (orig_op2
))
5070 pedwarn (colon_loc
, 0,
5071 "pointer/integer type mismatch in conditional expression");
5074 op2
= null_pointer_node
;
5076 result_type
= type1
;
5078 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5080 if (!null_pointer_constant_p (orig_op1
))
5081 pedwarn (colon_loc
, 0,
5082 "pointer/integer type mismatch in conditional expression");
5085 op1
= null_pointer_node
;
5087 result_type
= type2
;
5092 if (flag_cond_mismatch
)
5093 result_type
= void_type_node
;
5096 error_at (colon_loc
, "type mismatch in conditional expression");
5097 return error_mark_node
;
5101 /* Merge const and volatile flags of the incoming types. */
5103 = build_type_variant (result_type
,
5104 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
5105 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
5107 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
5108 semantic_result_type
);
5109 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
5110 semantic_result_type
);
5112 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
5114 op2_int_operands
= true;
5115 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
5117 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
5119 op1_int_operands
= true;
5120 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
5122 int_const
= int_operands
= (ifexp_int_operands
5124 && op2_int_operands
);
5127 int_const
= ((ifexp
== truthvalue_true_node
5128 && TREE_CODE (orig_op1
) == INTEGER_CST
5129 && !TREE_OVERFLOW (orig_op1
))
5130 || (ifexp
== truthvalue_false_node
5131 && TREE_CODE (orig_op2
) == INTEGER_CST
5132 && !TREE_OVERFLOW (orig_op2
)));
5135 /* Need to convert condition operand into a vector mask. */
5136 if (VECTOR_TYPE_P (TREE_TYPE (ifexp
)))
5138 tree vectype
= TREE_TYPE (ifexp
);
5139 tree elem_type
= TREE_TYPE (vectype
);
5140 tree zero
= build_int_cst (elem_type
, 0);
5141 tree zero_vec
= build_vector_from_val (vectype
, zero
);
5142 tree cmp_type
= build_same_sized_truth_vector_type (vectype
);
5143 ifexp
= build2 (NE_EXPR
, cmp_type
, ifexp
, zero_vec
);
5146 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
5147 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5152 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5153 nested inside of the expression. */
5154 op1
= c_fully_fold (op1
, false, NULL
);
5155 op2
= c_fully_fold (op2
, false, NULL
);
5157 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
5159 ret
= note_integer_operands (ret
);
5161 if (semantic_result_type
)
5162 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
5164 protected_set_expr_location (ret
, colon_loc
);
5168 /* Return a compound expression that performs two expressions and
5169 returns the value of the second of them.
5171 LOC is the location of the COMPOUND_EXPR. */
5174 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
5176 bool expr1_int_operands
, expr2_int_operands
;
5177 tree eptype
= NULL_TREE
;
5181 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
5182 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
5185 "spawned function call cannot be part of a comma expression");
5186 return error_mark_node
;
5188 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
5189 if (expr1_int_operands
)
5190 expr1
= remove_c_maybe_const_expr (expr1
);
5191 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
5192 if (expr2_int_operands
)
5193 expr2
= remove_c_maybe_const_expr (expr2
);
5195 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
5196 expr1
= TREE_OPERAND (expr1
, 0);
5197 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
5199 eptype
= TREE_TYPE (expr2
);
5200 expr2
= TREE_OPERAND (expr2
, 0);
5203 if (!TREE_SIDE_EFFECTS (expr1
))
5205 /* The left-hand operand of a comma expression is like an expression
5206 statement: with -Wunused, we should warn if it doesn't have
5207 any side-effects, unless it was explicitly cast to (void). */
5208 if (warn_unused_value
)
5210 if (VOID_TYPE_P (TREE_TYPE (expr1
))
5211 && CONVERT_EXPR_P (expr1
))
5213 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
5214 && TREE_CODE (expr1
) == COMPOUND_EXPR
5215 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
5216 ; /* (void) a, (void) b, c */
5218 warning_at (loc
, OPT_Wunused_value
,
5219 "left-hand operand of comma expression has no effect");
5222 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
5223 && warn_unused_value
)
5226 location_t cloc
= loc
;
5227 while (TREE_CODE (r
) == COMPOUND_EXPR
)
5229 if (EXPR_HAS_LOCATION (r
))
5230 cloc
= EXPR_LOCATION (r
);
5231 r
= TREE_OPERAND (r
, 1);
5233 if (!TREE_SIDE_EFFECTS (r
)
5234 && !VOID_TYPE_P (TREE_TYPE (r
))
5235 && !CONVERT_EXPR_P (r
))
5236 warning_at (cloc
, OPT_Wunused_value
,
5237 "right-hand operand of comma expression has no effect");
5240 /* With -Wunused, we should also warn if the left-hand operand does have
5241 side-effects, but computes a value which is not used. For example, in
5242 `foo() + bar(), baz()' the result of the `+' operator is not used,
5243 so we should issue a warning. */
5244 else if (warn_unused_value
)
5245 warn_if_unused_value (expr1
, loc
);
5247 if (expr2
== error_mark_node
)
5248 return error_mark_node
;
5250 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
5253 && expr1_int_operands
5254 && expr2_int_operands
)
5255 ret
= note_integer_operands (ret
);
5258 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
5260 protected_set_expr_location (ret
, loc
);
5264 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5265 which we are casting. OTYPE is the type of the expression being
5266 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5267 of the cast. -Wcast-qual appeared on the command line. Named
5268 address space qualifiers are not handled here, because they result
5269 in different warnings. */
5272 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
5274 tree in_type
= type
;
5275 tree in_otype
= otype
;
5280 /* Check that the qualifiers on IN_TYPE are a superset of the
5281 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5282 nodes is uninteresting and we stop as soon as we hit a
5283 non-POINTER_TYPE node on either type. */
5286 in_otype
= TREE_TYPE (in_otype
);
5287 in_type
= TREE_TYPE (in_type
);
5289 /* GNU C allows cv-qualified function types. 'const' means the
5290 function is very pure, 'volatile' means it can't return. We
5291 need to warn when such qualifiers are added, not when they're
5293 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
5294 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
5295 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
5296 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
5298 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
5299 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
5301 while (TREE_CODE (in_type
) == POINTER_TYPE
5302 && TREE_CODE (in_otype
) == POINTER_TYPE
);
5305 warning_at (loc
, OPT_Wcast_qual
,
5306 "cast adds %q#v qualifier to function type", added
);
5309 /* There are qualifiers present in IN_OTYPE that are not present
5311 warning_at (loc
, OPT_Wcast_qual
,
5312 "cast discards %qv qualifier from pointer target type",
5315 if (added
|| discarded
)
5318 /* A cast from **T to const **T is unsafe, because it can cause a
5319 const value to be changed with no additional warning. We only
5320 issue this warning if T is the same on both sides, and we only
5321 issue the warning if there are the same number of pointers on
5322 both sides, as otherwise the cast is clearly unsafe anyhow. A
5323 cast is unsafe when a qualifier is added at one level and const
5324 is not present at all outer levels.
5326 To issue this warning, we check at each level whether the cast
5327 adds new qualifiers not already seen. We don't need to special
5328 case function types, as they won't have the same
5329 TYPE_MAIN_VARIANT. */
5331 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
5333 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
5338 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
5341 in_type
= TREE_TYPE (in_type
);
5342 in_otype
= TREE_TYPE (in_otype
);
5343 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
5346 warning_at (loc
, OPT_Wcast_qual
,
5347 "to be safe all intermediate pointers in cast from "
5348 "%qT to %qT must be %<const%> qualified",
5353 is_const
= TYPE_READONLY (in_type
);
5355 while (TREE_CODE (in_type
) == POINTER_TYPE
);
5358 /* Build an expression representing a cast to type TYPE of expression EXPR.
5359 LOC is the location of the cast-- typically the open paren of the cast. */
5362 build_c_cast (location_t loc
, tree type
, tree expr
)
5366 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
5367 expr
= TREE_OPERAND (expr
, 0);
5371 if (type
== error_mark_node
|| expr
== error_mark_node
)
5372 return error_mark_node
;
5374 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5375 only in <protocol> qualifications. But when constructing cast expressions,
5376 the protocols do matter and must be kept around. */
5377 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
5378 return build1 (NOP_EXPR
, type
, expr
);
5380 type
= TYPE_MAIN_VARIANT (type
);
5382 if (TREE_CODE (type
) == ARRAY_TYPE
)
5384 error_at (loc
, "cast specifies array type");
5385 return error_mark_node
;
5388 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5390 error_at (loc
, "cast specifies function type");
5391 return error_mark_node
;
5394 if (!VOID_TYPE_P (type
))
5396 value
= require_complete_type (loc
, value
);
5397 if (value
== error_mark_node
)
5398 return error_mark_node
;
5401 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
5403 if (RECORD_OR_UNION_TYPE_P (type
))
5404 pedwarn (loc
, OPT_Wpedantic
,
5405 "ISO C forbids casting nonscalar to the same type");
5407 /* Convert to remove any qualifiers from VALUE's type. */
5408 value
= convert (type
, value
);
5410 else if (TREE_CODE (type
) == UNION_TYPE
)
5414 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5415 if (TREE_TYPE (field
) != error_mark_node
5416 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
5417 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
5423 bool maybe_const
= true;
5425 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
5426 t
= c_fully_fold (value
, false, &maybe_const
);
5427 t
= build_constructor_single (type
, field
, t
);
5429 t
= c_wrap_maybe_const (t
, true);
5430 t
= digest_init (loc
, type
, t
,
5431 NULL_TREE
, false, true, 0);
5432 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
5435 error_at (loc
, "cast to union type from type not present in union");
5436 return error_mark_node
;
5442 if (type
== void_type_node
)
5444 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5445 SET_EXPR_LOCATION (t
, loc
);
5449 otype
= TREE_TYPE (value
);
5451 /* Optionally warn about potentially worrisome casts. */
5453 && TREE_CODE (type
) == POINTER_TYPE
5454 && TREE_CODE (otype
) == POINTER_TYPE
)
5455 handle_warn_cast_qual (loc
, type
, otype
);
5457 /* Warn about conversions between pointers to disjoint
5459 if (TREE_CODE (type
) == POINTER_TYPE
5460 && TREE_CODE (otype
) == POINTER_TYPE
5461 && !null_pointer_constant_p (value
))
5463 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5464 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5465 addr_space_t as_common
;
5467 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5469 if (ADDR_SPACE_GENERIC_P (as_from
))
5470 warning_at (loc
, 0, "cast to %s address space pointer "
5471 "from disjoint generic address space pointer",
5472 c_addr_space_name (as_to
));
5474 else if (ADDR_SPACE_GENERIC_P (as_to
))
5475 warning_at (loc
, 0, "cast to generic address space pointer "
5476 "from disjoint %s address space pointer",
5477 c_addr_space_name (as_from
));
5480 warning_at (loc
, 0, "cast to %s address space pointer "
5481 "from disjoint %s address space pointer",
5482 c_addr_space_name (as_to
),
5483 c_addr_space_name (as_from
));
5487 /* Warn about possible alignment problems. */
5488 if (STRICT_ALIGNMENT
5489 && TREE_CODE (type
) == POINTER_TYPE
5490 && TREE_CODE (otype
) == POINTER_TYPE
5491 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5492 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5493 /* Don't warn about opaque types, where the actual alignment
5494 restriction is unknown. */
5495 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype
))
5496 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5497 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5498 warning_at (loc
, OPT_Wcast_align
,
5499 "cast increases required alignment of target type");
5501 if (TREE_CODE (type
) == INTEGER_TYPE
5502 && TREE_CODE (otype
) == POINTER_TYPE
5503 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5504 /* Unlike conversion of integers to pointers, where the
5505 warning is disabled for converting constants because
5506 of cases such as SIG_*, warn about converting constant
5507 pointers to integers. In some cases it may cause unwanted
5508 sign extension, and a warning is appropriate. */
5509 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5510 "cast from pointer to integer of different size");
5512 if (TREE_CODE (value
) == CALL_EXPR
5513 && TREE_CODE (type
) != TREE_CODE (otype
))
5514 warning_at (loc
, OPT_Wbad_function_cast
,
5515 "cast from function call of type %qT "
5516 "to non-matching type %qT", otype
, type
);
5518 if (TREE_CODE (type
) == POINTER_TYPE
5519 && TREE_CODE (otype
) == INTEGER_TYPE
5520 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5521 /* Don't warn about converting any constant. */
5522 && !TREE_CONSTANT (value
))
5524 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5525 "of different size");
5527 if (warn_strict_aliasing
<= 2)
5528 strict_aliasing_warning (otype
, type
, expr
);
5530 /* If pedantic, warn for conversions between function and object
5531 pointer types, except for converting a null pointer constant
5532 to function pointer type. */
5534 && TREE_CODE (type
) == POINTER_TYPE
5535 && TREE_CODE (otype
) == POINTER_TYPE
5536 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5537 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5538 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5539 "conversion of function pointer to object pointer type");
5542 && TREE_CODE (type
) == POINTER_TYPE
5543 && TREE_CODE (otype
) == POINTER_TYPE
5544 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5545 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5546 && !null_pointer_constant_p (value
))
5547 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5548 "conversion of object pointer to function pointer type");
5551 value
= convert (type
, value
);
5553 /* Ignore any integer overflow caused by the cast. */
5554 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5556 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5558 if (!TREE_OVERFLOW (value
))
5560 /* Avoid clobbering a shared constant. */
5561 value
= copy_node (value
);
5562 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5565 else if (TREE_OVERFLOW (value
))
5566 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5567 value
= wide_int_to_tree (TREE_TYPE (value
), value
);
5571 /* Don't let a cast be an lvalue. */
5572 if (lvalue_p (value
))
5573 value
= non_lvalue_loc (loc
, value
);
5575 /* Don't allow the results of casting to floating-point or complex
5576 types be confused with actual constants, or casts involving
5577 integer and pointer types other than direct integer-to-integer
5578 and integer-to-pointer be confused with integer constant
5579 expressions and null pointer constants. */
5580 if (TREE_CODE (value
) == REAL_CST
5581 || TREE_CODE (value
) == COMPLEX_CST
5582 || (TREE_CODE (value
) == INTEGER_CST
5583 && !((TREE_CODE (expr
) == INTEGER_CST
5584 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5585 || TREE_CODE (expr
) == REAL_CST
5586 || TREE_CODE (expr
) == COMPLEX_CST
)))
5587 value
= build1 (NOP_EXPR
, type
, value
);
5589 protected_set_expr_location (value
, loc
);
5593 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5594 location of the open paren of the cast, or the position of the cast
5597 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5600 tree type_expr
= NULL_TREE
;
5601 bool type_expr_const
= true;
5603 int saved_wsp
= warn_strict_prototypes
;
5605 /* This avoids warnings about unprototyped casts on
5606 integers. E.g. "#define SIG_DFL (void(*)())0". */
5607 if (TREE_CODE (expr
) == INTEGER_CST
)
5608 warn_strict_prototypes
= 0;
5609 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5610 warn_strict_prototypes
= saved_wsp
;
5612 if (TREE_CODE (expr
) == ADDR_EXPR
&& !VOID_TYPE_P (type
)
5613 && reject_gcc_builtin (expr
))
5614 return error_mark_node
;
5616 ret
= build_c_cast (loc
, type
, expr
);
5619 bool inner_expr_const
= true;
5620 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5621 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5622 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5623 && inner_expr_const
);
5624 SET_EXPR_LOCATION (ret
, loc
);
5627 if (!EXPR_HAS_LOCATION (ret
))
5628 protected_set_expr_location (ret
, loc
);
5630 /* C++ does not permits types to be defined in a cast, but it
5631 allows references to incomplete types. */
5632 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5633 warning_at (loc
, OPT_Wc___compat
,
5634 "defining a type in a cast is invalid in C++");
5639 /* Build an assignment expression of lvalue LHS from value RHS.
5640 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5641 may differ from TREE_TYPE (LHS) for an enum bitfield.
5642 MODIFYCODE is the code for a binary operator that we use
5643 to combine the old value of LHS with RHS to get the new value.
5644 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5645 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5646 which may differ from TREE_TYPE (RHS) for an enum value.
5648 LOCATION is the location of the MODIFYCODE operator.
5649 RHS_LOC is the location of the RHS. */
5652 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5653 enum tree_code modifycode
,
5654 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5658 tree rhseval
= NULL_TREE
;
5659 tree rhs_semantic_type
= NULL_TREE
;
5660 tree lhstype
= TREE_TYPE (lhs
);
5661 tree olhstype
= lhstype
;
5665 /* Types that aren't fully specified cannot be used in assignments. */
5666 lhs
= require_complete_type (location
, lhs
);
5668 /* Avoid duplicate error messages from operands that had errors. */
5669 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5670 return error_mark_node
;
5672 /* Ensure an error for assigning a non-lvalue array to an array in
5674 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5676 error_at (location
, "assignment to expression with array type");
5677 return error_mark_node
;
5680 /* For ObjC properties, defer this check. */
5681 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5682 return error_mark_node
;
5684 is_atomic_op
= really_atomic_lvalue (lhs
);
5686 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5688 rhs_semantic_type
= TREE_TYPE (rhs
);
5689 rhs
= TREE_OPERAND (rhs
, 0);
5694 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5696 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5697 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5699 if (inner
== error_mark_node
)
5700 return error_mark_node
;
5701 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5702 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5703 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5704 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5705 protected_set_expr_location (result
, location
);
5709 /* If a binary op has been requested, combine the old LHS value with the RHS
5710 producing the value we should actually store into the LHS. */
5712 if (modifycode
!= NOP_EXPR
)
5714 lhs
= c_fully_fold (lhs
, false, NULL
);
5715 lhs
= stabilize_reference (lhs
);
5717 /* Construct the RHS for any non-atomic compound assignemnt. */
5720 /* If in LHS op= RHS the RHS has side-effects, ensure they
5721 are preevaluated before the rest of the assignment expression's
5722 side-effects, because RHS could contain e.g. function calls
5724 if (TREE_SIDE_EFFECTS (rhs
))
5726 newrhs
= in_late_binary_op
? save_expr (rhs
) : c_save_expr (rhs
);
5729 newrhs
= build_binary_op (location
,
5730 modifycode
, lhs
, newrhs
, 1);
5732 /* The original type of the right hand side is no longer
5734 rhs_origtype
= NULL_TREE
;
5738 if (c_dialect_objc ())
5740 /* Check if we are modifying an Objective-C property reference;
5741 if so, we need to generate setter calls. */
5742 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5746 /* Else, do the check that we postponed for Objective-C. */
5747 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5748 return error_mark_node
;
5751 /* Give an error for storing in something that is 'const'. */
5753 if (TYPE_READONLY (lhstype
)
5754 || (RECORD_OR_UNION_TYPE_P (lhstype
)
5755 && C_TYPE_FIELDS_READONLY (lhstype
)))
5757 readonly_error (location
, lhs
, lv_assign
);
5758 return error_mark_node
;
5760 else if (TREE_READONLY (lhs
))
5761 readonly_warning (lhs
, lv_assign
);
5763 /* If storing into a structure or union member,
5764 it has probably been given type `int'.
5765 Compute the type that would go with
5766 the actual amount of storage the member occupies. */
5768 if (TREE_CODE (lhs
) == COMPONENT_REF
5769 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5770 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5771 || TREE_CODE (lhstype
) == REAL_TYPE
5772 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5773 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5775 /* If storing in a field that is in actuality a short or narrower than one,
5776 we must store in the field in its actual type. */
5778 if (lhstype
!= TREE_TYPE (lhs
))
5780 lhs
= copy_node (lhs
);
5781 TREE_TYPE (lhs
) = lhstype
;
5784 /* Issue -Wc++-compat warnings about an assignment to an enum type
5785 when LHS does not have its original type. This happens for,
5786 e.g., an enum bitfield in a struct. */
5788 && lhs_origtype
!= NULL_TREE
5789 && lhs_origtype
!= lhstype
5790 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5792 tree checktype
= (rhs_origtype
!= NULL_TREE
5795 if (checktype
!= error_mark_node
5796 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5797 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5798 warning_at (location
, OPT_Wc___compat
,
5799 "enum conversion in assignment is invalid in C++");
5802 /* If the lhs is atomic, remove that qualifier. */
5805 lhstype
= build_qualified_type (lhstype
,
5806 (TYPE_QUALS (lhstype
)
5807 & ~TYPE_QUAL_ATOMIC
));
5808 olhstype
= build_qualified_type (olhstype
,
5809 (TYPE_QUALS (lhstype
)
5810 & ~TYPE_QUAL_ATOMIC
));
5813 /* Convert new value to destination type. Fold it first, then
5814 restore any excess precision information, for the sake of
5815 conversion warnings. */
5817 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5819 npc
= null_pointer_constant_p (newrhs
);
5820 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5821 if (rhs_semantic_type
)
5822 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5823 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
5824 rhs_origtype
, ic_assign
, npc
,
5825 NULL_TREE
, NULL_TREE
, 0);
5826 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5827 return error_mark_node
;
5830 /* Emit ObjC write barrier, if necessary. */
5831 if (c_dialect_objc () && flag_objc_gc
)
5833 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5836 protected_set_expr_location (result
, location
);
5841 /* Scan operands. */
5844 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5847 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5848 TREE_SIDE_EFFECTS (result
) = 1;
5849 protected_set_expr_location (result
, location
);
5852 /* If we got the LHS in a different type for storing in,
5853 convert the result back to the nominal type of LHS
5854 so that the value we return always has the same type
5855 as the LHS argument. */
5857 if (olhstype
== TREE_TYPE (result
))
5860 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
5861 rhs_origtype
, ic_assign
, false, NULL_TREE
,
5863 protected_set_expr_location (result
, location
);
5867 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
5871 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5872 This is used to implement -fplan9-extensions. */
5875 find_anonymous_field_with_type (tree struct_type
, tree type
)
5880 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type
));
5882 for (field
= TYPE_FIELDS (struct_type
);
5884 field
= TREE_CHAIN (field
))
5886 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5887 ? c_build_qualified_type (TREE_TYPE (field
),
5889 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5890 if (DECL_NAME (field
) == NULL
5891 && comptypes (type
, fieldtype
))
5897 else if (DECL_NAME (field
) == NULL
5898 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
))
5899 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5909 /* RHS is an expression whose type is pointer to struct. If there is
5910 an anonymous field in RHS with type TYPE, then return a pointer to
5911 that field in RHS. This is used with -fplan9-extensions. This
5912 returns NULL if no conversion could be found. */
5915 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5917 tree rhs_struct_type
, lhs_main_type
;
5918 tree field
, found_field
;
5919 bool found_sub_field
;
5922 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5923 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5924 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type
));
5926 gcc_assert (POINTER_TYPE_P (type
));
5927 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5928 ? c_build_qualified_type (TREE_TYPE (type
),
5930 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5932 found_field
= NULL_TREE
;
5933 found_sub_field
= false;
5934 for (field
= TYPE_FIELDS (rhs_struct_type
);
5936 field
= TREE_CHAIN (field
))
5938 if (DECL_NAME (field
) != NULL_TREE
5939 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field
)))
5941 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5942 ? c_build_qualified_type (TREE_TYPE (field
),
5944 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5945 if (comptypes (lhs_main_type
, fieldtype
))
5947 if (found_field
!= NULL_TREE
)
5949 found_field
= field
;
5951 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5954 if (found_field
!= NULL_TREE
)
5956 found_field
= field
;
5957 found_sub_field
= true;
5961 if (found_field
== NULL_TREE
)
5964 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5965 build_fold_indirect_ref (rhs
), found_field
,
5967 ret
= build_fold_addr_expr_loc (location
, ret
);
5969 if (found_sub_field
)
5971 ret
= convert_to_anonymous_field (location
, type
, ret
);
5972 gcc_assert (ret
!= NULL_TREE
);
5978 /* Issue an error message for a bad initializer component.
5979 GMSGID identifies the message.
5980 The component name is taken from the spelling stack. */
5983 error_init (location_t loc
, const char *gmsgid
)
5987 /* The gmsgid may be a format string with %< and %>. */
5988 error_at (loc
, gmsgid
);
5989 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5991 inform (loc
, "(near initialization for %qs)", ofwhat
);
5994 /* Issue a pedantic warning for a bad initializer component. OPT is
5995 the option OPT_* (from options.h) controlling this warning or 0 if
5996 it is unconditionally given. GMSGID identifies the message. The
5997 component name is taken from the spelling stack. */
6000 pedwarn_init (location_t loc
, int opt
, const char *gmsgid
)
6005 /* Use the location where a macro was expanded rather than where
6006 it was defined to make sure macros defined in system headers
6007 but used incorrectly elsewhere are diagnosed. */
6008 source_location exploc
= expansion_point_location_if_in_system_header (loc
);
6010 /* The gmsgid may be a format string with %< and %>. */
6011 warned
= pedwarn (exploc
, opt
, gmsgid
);
6012 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6013 if (*ofwhat
&& warned
)
6014 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6017 /* Issue a warning for a bad initializer component.
6019 OPT is the OPT_W* value corresponding to the warning option that
6020 controls this warning. GMSGID identifies the message. The
6021 component name is taken from the spelling stack. */
6024 warning_init (location_t loc
, int opt
, const char *gmsgid
)
6029 /* Use the location where a macro was expanded rather than where
6030 it was defined to make sure macros defined in system headers
6031 but used incorrectly elsewhere are diagnosed. */
6032 source_location exploc
= expansion_point_location_if_in_system_header (loc
);
6034 /* The gmsgid may be a format string with %< and %>. */
6035 warned
= warning_at (exploc
, opt
, gmsgid
);
6036 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6037 if (*ofwhat
&& warned
)
6038 inform (exploc
, "(near initialization for %qs)", ofwhat
);
6041 /* If TYPE is an array type and EXPR is a parenthesized string
6042 constant, warn if pedantic that EXPR is being used to initialize an
6043 object of type TYPE. */
6046 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
6049 && TREE_CODE (type
) == ARRAY_TYPE
6050 && TREE_CODE (expr
.value
) == STRING_CST
6051 && expr
.original_code
!= STRING_CST
)
6052 pedwarn_init (loc
, OPT_Wpedantic
,
6053 "array initialized from parenthesized string constant");
6056 /* Convert value RHS to type TYPE as preparation for an assignment to
6057 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6058 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6059 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6060 constant before any folding.
6061 The real work of conversion is done by `convert'.
6062 The purpose of this function is to generate error messages
6063 for assignments that are not allowed in C.
6064 ERRTYPE says whether it is argument passing, assignment,
6065 initialization or return.
6067 In the following example, '~' denotes where EXPR_LOC and '^' where
6070 f (var); [ic_argpass]
6072 x = var; [ic_assign]
6074 int x = var; [ic_init]
6076 return x; [ic_return]
6079 FUNCTION is a tree for the function being called.
6080 PARMNUM is the number of the argument, for printing in error messages. */
6083 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
6084 tree rhs
, tree origtype
, enum impl_conv errtype
,
6085 bool null_pointer_constant
, tree fundecl
,
6086 tree function
, int parmnum
)
6088 enum tree_code codel
= TREE_CODE (type
);
6089 tree orig_rhs
= rhs
;
6091 enum tree_code coder
;
6092 tree rname
= NULL_TREE
;
6093 bool objc_ok
= false;
6095 /* Use the expansion point location to handle cases such as user's
6096 function returning a wrong-type macro defined in a system header. */
6097 location
= expansion_point_location_if_in_system_header (location
);
6099 if (errtype
== ic_argpass
)
6102 /* Change pointer to function to the function itself for
6104 if (TREE_CODE (function
) == ADDR_EXPR
6105 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
6106 function
= TREE_OPERAND (function
, 0);
6108 /* Handle an ObjC selector specially for diagnostics. */
6109 selector
= objc_message_selector ();
6111 if (selector
&& parmnum
> 2)
6118 /* This macro is used to emit diagnostics to ensure that all format
6119 strings are complete sentences, visible to gettext and checked at
6121 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
6126 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6127 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6128 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6129 "expected %qT but argument is of type %qT", \
6133 pedwarn (LOCATION, OPT, AS); \
6136 pedwarn_init (LOCATION, OPT, IN); \
6139 pedwarn (LOCATION, OPT, RE); \
6142 gcc_unreachable (); \
6146 /* This macro is used to emit diagnostics to ensure that all format
6147 strings are complete sentences, visible to gettext and checked at
6148 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6149 extra parameter to enumerate qualifiers. */
6150 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6155 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6156 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6157 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6158 "expected %qT but argument is of type %qT", \
6162 pedwarn (LOCATION, OPT, AS, QUALS); \
6165 pedwarn (LOCATION, OPT, IN, QUALS); \
6168 pedwarn (LOCATION, OPT, RE, QUALS); \
6171 gcc_unreachable (); \
6175 /* This macro is used to emit diagnostics to ensure that all format
6176 strings are complete sentences, visible to gettext and checked at
6177 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6178 warning_at instead of pedwarn. */
6179 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6184 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6185 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6186 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6187 "expected %qT but argument is of type %qT", \
6191 warning_at (LOCATION, OPT, AS, QUALS); \
6194 warning_at (LOCATION, OPT, IN, QUALS); \
6197 warning_at (LOCATION, OPT, RE, QUALS); \
6200 gcc_unreachable (); \
6204 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
6205 rhs
= TREE_OPERAND (rhs
, 0);
6207 rhstype
= TREE_TYPE (rhs
);
6208 coder
= TREE_CODE (rhstype
);
6210 if (coder
== ERROR_MARK
)
6211 return error_mark_node
;
6213 if (c_dialect_objc ())
6236 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
6239 if (warn_cxx_compat
)
6241 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
6242 if (checktype
!= error_mark_node
6243 && TREE_CODE (type
) == ENUMERAL_TYPE
6244 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
6246 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wc___compat
,
6247 G_("enum conversion when passing argument "
6248 "%d of %qE is invalid in C++"),
6249 G_("enum conversion in assignment is "
6251 G_("enum conversion in initialization is "
6253 G_("enum conversion in return is "
6258 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
6261 if (coder
== VOID_TYPE
)
6263 /* Except for passing an argument to an unprototyped function,
6264 this is a constraint violation. When passing an argument to
6265 an unprototyped function, it is compile-time undefined;
6266 making it a constraint in that case was rejected in
6268 error_at (location
, "void value not ignored as it ought to be");
6269 return error_mark_node
;
6271 rhs
= require_complete_type (location
, rhs
);
6272 if (rhs
== error_mark_node
)
6273 return error_mark_node
;
6275 if (coder
== POINTER_TYPE
&& reject_gcc_builtin (rhs
))
6276 return error_mark_node
;
6278 /* A non-reference type can convert to a reference. This handles
6279 va_start, va_copy and possibly port built-ins. */
6280 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
6282 if (!lvalue_p (rhs
))
6284 error_at (location
, "cannot pass rvalue to reference parameter");
6285 return error_mark_node
;
6287 if (!c_mark_addressable (rhs
))
6288 return error_mark_node
;
6289 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
6290 SET_EXPR_LOCATION (rhs
, location
);
6292 rhs
= convert_for_assignment (location
, expr_loc
,
6293 build_pointer_type (TREE_TYPE (type
)),
6294 rhs
, origtype
, errtype
,
6295 null_pointer_constant
, fundecl
, function
,
6297 if (rhs
== error_mark_node
)
6298 return error_mark_node
;
6300 rhs
= build1 (NOP_EXPR
, type
, rhs
);
6301 SET_EXPR_LOCATION (rhs
, location
);
6304 /* Some types can interconvert without explicit casts. */
6305 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
6306 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
6307 return convert (type
, rhs
);
6308 /* Arithmetic types all interconvert, and enum is treated like int. */
6309 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
6310 || codel
== FIXED_POINT_TYPE
6311 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
6312 || codel
== BOOLEAN_TYPE
)
6313 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
6314 || coder
== FIXED_POINT_TYPE
6315 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
6316 || coder
== BOOLEAN_TYPE
))
6319 bool save
= in_late_binary_op
;
6320 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
6321 || (coder
== REAL_TYPE
6322 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
6323 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
6324 in_late_binary_op
= true;
6325 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6326 ? expr_loc
: location
, type
, orig_rhs
);
6327 in_late_binary_op
= save
;
6331 /* Aggregates in different TUs might need conversion. */
6332 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
6334 && comptypes (type
, rhstype
))
6335 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
6336 ? expr_loc
: location
, type
, rhs
);
6338 /* Conversion to a transparent union or record from its member types.
6339 This applies only to function arguments. */
6340 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
6341 && TYPE_TRANSPARENT_AGGR (type
))
6342 && errtype
== ic_argpass
)
6344 tree memb
, marginal_memb
= NULL_TREE
;
6346 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
6348 tree memb_type
= TREE_TYPE (memb
);
6350 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
6351 TYPE_MAIN_VARIANT (rhstype
)))
6354 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
6357 if (coder
== POINTER_TYPE
)
6359 tree ttl
= TREE_TYPE (memb_type
);
6360 tree ttr
= TREE_TYPE (rhstype
);
6362 /* Any non-function converts to a [const][volatile] void *
6363 and vice versa; otherwise, targets must be the same.
6364 Meanwhile, the lhs target must have all the qualifiers of
6366 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6367 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6368 || comp_target_types (location
, memb_type
, rhstype
))
6370 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
6371 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
6372 /* If this type won't generate any warnings, use it. */
6373 if (lquals
== rquals
6374 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
6375 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6376 ? ((lquals
| rquals
) == rquals
)
6377 : ((lquals
| rquals
) == lquals
)))
6380 /* Keep looking for a better type, but remember this one. */
6382 marginal_memb
= memb
;
6386 /* Can convert integer zero to any pointer type. */
6387 if (null_pointer_constant
)
6389 rhs
= null_pointer_node
;
6394 if (memb
|| marginal_memb
)
6398 /* We have only a marginally acceptable member type;
6399 it needs a warning. */
6400 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
6401 tree ttr
= TREE_TYPE (rhstype
);
6403 /* Const and volatile mean something different for function
6404 types, so the usual warnings are not appropriate. */
6405 if (TREE_CODE (ttr
) == FUNCTION_TYPE
6406 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6408 /* Because const and volatile on functions are
6409 restrictions that say the function will not do
6410 certain things, it is okay to use a const or volatile
6411 function where an ordinary one is wanted, but not
6413 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6414 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6415 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6416 OPT_Wdiscarded_qualifiers
,
6417 G_("passing argument %d of %qE "
6418 "makes %q#v qualified function "
6419 "pointer from unqualified"),
6420 G_("assignment makes %q#v qualified "
6421 "function pointer from "
6423 G_("initialization makes %q#v qualified "
6424 "function pointer from "
6426 G_("return makes %q#v qualified function "
6427 "pointer from unqualified"),
6428 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6430 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
6431 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
6432 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6433 OPT_Wdiscarded_qualifiers
,
6434 G_("passing argument %d of %qE discards "
6435 "%qv qualifier from pointer target type"),
6436 G_("assignment discards %qv qualifier "
6437 "from pointer target type"),
6438 G_("initialization discards %qv qualifier "
6439 "from pointer target type"),
6440 G_("return discards %qv qualifier from "
6441 "pointer target type"),
6442 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6444 memb
= marginal_memb
;
6447 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
6448 pedwarn (location
, OPT_Wpedantic
,
6449 "ISO C prohibits argument conversion to union type");
6451 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
6452 return build_constructor_single (type
, memb
, rhs
);
6456 /* Conversions among pointers */
6457 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
6458 && (coder
== codel
))
6460 tree ttl
= TREE_TYPE (type
);
6461 tree ttr
= TREE_TYPE (rhstype
);
6464 bool is_opaque_pointer
;
6465 int target_cmp
= 0; /* Cache comp_target_types () result. */
6469 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
6470 mvl
= (TYPE_ATOMIC (mvl
)
6471 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
6473 : TYPE_MAIN_VARIANT (mvl
));
6474 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
6475 mvr
= (TYPE_ATOMIC (mvr
)
6476 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
6478 : TYPE_MAIN_VARIANT (mvr
));
6479 /* Opaque pointers are treated like void pointers. */
6480 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
6482 /* The Plan 9 compiler permits a pointer to a struct to be
6483 automatically converted into a pointer to an anonymous field
6484 within the struct. */
6485 if (flag_plan9_extensions
6486 && RECORD_OR_UNION_TYPE_P (mvl
)
6487 && RECORD_OR_UNION_TYPE_P (mvr
)
6490 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
6491 if (new_rhs
!= NULL_TREE
)
6494 rhstype
= TREE_TYPE (rhs
);
6495 coder
= TREE_CODE (rhstype
);
6496 ttr
= TREE_TYPE (rhstype
);
6497 mvr
= TYPE_MAIN_VARIANT (ttr
);
6501 /* C++ does not allow the implicit conversion void* -> T*. However,
6502 for the purpose of reducing the number of false positives, we
6503 tolerate the special case of
6507 where NULL is typically defined in C to be '(void *) 0'. */
6508 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
6509 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
6511 "request for implicit conversion "
6512 "from %qT to %qT not permitted in C++", rhstype
, type
);
6514 /* See if the pointers point to incompatible address spaces. */
6515 asl
= TYPE_ADDR_SPACE (ttl
);
6516 asr
= TYPE_ADDR_SPACE (ttr
);
6517 if (!null_pointer_constant_p (rhs
)
6518 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
6523 error_at (expr_loc
, "passing argument %d of %qE from pointer to "
6524 "non-enclosed address space", parmnum
, rname
);
6527 error_at (location
, "assignment from pointer to "
6528 "non-enclosed address space");
6531 error_at (location
, "initialization from pointer to "
6532 "non-enclosed address space");
6535 error_at (location
, "return from pointer to "
6536 "non-enclosed address space");
6541 return error_mark_node
;
6544 /* Check if the right-hand side has a format attribute but the
6545 left-hand side doesn't. */
6546 if (warn_suggest_attribute_format
6547 && check_missing_format_attribute (type
, rhstype
))
6552 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
6553 "argument %d of %qE might be "
6554 "a candidate for a format attribute",
6558 warning_at (location
, OPT_Wsuggest_attribute_format
,
6559 "assignment left-hand side might be "
6560 "a candidate for a format attribute");
6563 warning_at (location
, OPT_Wsuggest_attribute_format
,
6564 "initialization left-hand side might be "
6565 "a candidate for a format attribute");
6568 warning_at (location
, OPT_Wsuggest_attribute_format
,
6569 "return type might be "
6570 "a candidate for a format attribute");
6577 /* Any non-function converts to a [const][volatile] void *
6578 and vice versa; otherwise, targets must be the same.
6579 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6580 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6581 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6582 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6583 || is_opaque_pointer
6584 || ((c_common_unsigned_type (mvl
)
6585 == c_common_unsigned_type (mvr
))
6586 && (c_common_signed_type (mvl
)
6587 == c_common_signed_type (mvr
))
6588 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6590 /* Warn about loss of qualifers from pointers to arrays with
6591 qualifiers on the element type. */
6592 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
6594 ttr
= strip_array_types (ttr
);
6595 ttl
= strip_array_types (ttl
);
6597 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6598 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6599 WARNING_FOR_QUALIFIERS (location
, expr_loc
,
6600 OPT_Wdiscarded_array_qualifiers
,
6601 G_("passing argument %d of %qE discards "
6602 "%qv qualifier from pointer target type"),
6603 G_("assignment discards %qv qualifier "
6604 "from pointer target type"),
6605 G_("initialization discards %qv qualifier "
6606 "from pointer target type"),
6607 G_("return discards %qv qualifier from "
6608 "pointer target type"),
6609 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6612 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6615 && !null_pointer_constant
6616 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6617 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
6618 G_("ISO C forbids passing argument %d of "
6619 "%qE between function pointer "
6621 G_("ISO C forbids assignment between "
6622 "function pointer and %<void *%>"),
6623 G_("ISO C forbids initialization between "
6624 "function pointer and %<void *%>"),
6625 G_("ISO C forbids return between function "
6626 "pointer and %<void *%>"));
6627 /* Const and volatile mean something different for function types,
6628 so the usual warnings are not appropriate. */
6629 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6630 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6632 /* Don't warn about loss of qualifier for conversions from
6633 qualified void* to pointers to arrays with corresponding
6634 qualifier on the element type. */
6636 ttl
= strip_array_types (ttl
);
6638 /* Assignments between atomic and non-atomic objects are OK. */
6639 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6640 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6642 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6643 OPT_Wdiscarded_qualifiers
,
6644 G_("passing argument %d of %qE discards "
6645 "%qv qualifier from pointer target type"),
6646 G_("assignment discards %qv qualifier "
6647 "from pointer target type"),
6648 G_("initialization discards %qv qualifier "
6649 "from pointer target type"),
6650 G_("return discards %qv qualifier from "
6651 "pointer target type"),
6652 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6654 /* If this is not a case of ignoring a mismatch in signedness,
6656 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6659 /* If there is a mismatch, do warn. */
6660 else if (warn_pointer_sign
)
6661 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpointer_sign
,
6662 G_("pointer targets in passing argument "
6663 "%d of %qE differ in signedness"),
6664 G_("pointer targets in assignment "
6665 "differ in signedness"),
6666 G_("pointer targets in initialization "
6667 "differ in signedness"),
6668 G_("pointer targets in return differ "
6671 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6672 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6674 /* Because const and volatile on functions are restrictions
6675 that say the function will not do certain things,
6676 it is okay to use a const or volatile function
6677 where an ordinary one is wanted, but not vice-versa. */
6678 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6679 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6680 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6681 OPT_Wdiscarded_qualifiers
,
6682 G_("passing argument %d of %qE makes "
6683 "%q#v qualified function pointer "
6684 "from unqualified"),
6685 G_("assignment makes %q#v qualified function "
6686 "pointer from unqualified"),
6687 G_("initialization makes %q#v qualified "
6688 "function pointer from unqualified"),
6689 G_("return makes %q#v qualified function "
6690 "pointer from unqualified"),
6691 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6695 /* Avoid warning about the volatile ObjC EH puts on decls. */
6697 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6698 OPT_Wincompatible_pointer_types
,
6699 G_("passing argument %d of %qE from "
6700 "incompatible pointer type"),
6701 G_("assignment from incompatible pointer type"),
6702 G_("initialization from incompatible "
6704 G_("return from incompatible pointer type"));
6706 return convert (type
, rhs
);
6708 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6710 /* ??? This should not be an error when inlining calls to
6711 unprototyped functions. */
6712 error_at (location
, "invalid use of non-lvalue array");
6713 return error_mark_node
;
6715 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6717 /* An explicit constant 0 can convert to a pointer,
6718 or one that results from arithmetic, even including
6719 a cast to integer type. */
6720 if (!null_pointer_constant
)
6721 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6722 OPT_Wint_conversion
,
6723 G_("passing argument %d of %qE makes "
6724 "pointer from integer without a cast"),
6725 G_("assignment makes pointer from integer "
6727 G_("initialization makes pointer from "
6728 "integer without a cast"),
6729 G_("return makes pointer from integer "
6732 return convert (type
, rhs
);
6734 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6736 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6737 OPT_Wint_conversion
,
6738 G_("passing argument %d of %qE makes integer "
6739 "from pointer without a cast"),
6740 G_("assignment makes integer from pointer "
6742 G_("initialization makes integer from pointer "
6744 G_("return makes integer from pointer "
6746 return convert (type
, rhs
);
6748 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6751 bool save
= in_late_binary_op
;
6752 in_late_binary_op
= true;
6753 ret
= convert (type
, rhs
);
6754 in_late_binary_op
= save
;
6761 error_at (expr_loc
, "incompatible type for argument %d of %qE", parmnum
,
6763 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6764 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6765 "expected %qT but argument is of type %qT", type
, rhstype
);
6768 error_at (location
, "incompatible types when assigning to type %qT from "
6769 "type %qT", type
, rhstype
);
6773 "incompatible types when initializing type %qT using type %qT",
6778 "incompatible types when returning type %qT but %qT was "
6779 "expected", rhstype
, type
);
6785 return error_mark_node
;
6788 /* If VALUE is a compound expr all of whose expressions are constant, then
6789 return its value. Otherwise, return error_mark_node.
6791 This is for handling COMPOUND_EXPRs as initializer elements
6792 which is allowed with a warning when -pedantic is specified. */
6795 valid_compound_expr_initializer (tree value
, tree endtype
)
6797 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6799 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6801 return error_mark_node
;
6802 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6805 else if (!initializer_constant_valid_p (value
, endtype
))
6806 return error_mark_node
;
6811 /* Perform appropriate conversions on the initial value of a variable,
6812 store it in the declaration DECL,
6813 and print any error messages that are appropriate.
6814 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6815 If the init is invalid, store an ERROR_MARK.
6817 INIT_LOC is the location of the initial value. */
6820 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6825 /* If variable's type was invalidly declared, just ignore it. */
6827 type
= TREE_TYPE (decl
);
6828 if (TREE_CODE (type
) == ERROR_MARK
)
6831 /* Digest the specified initializer into an expression. */
6834 npc
= null_pointer_constant_p (init
);
6835 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6836 true, TREE_STATIC (decl
));
6838 /* Store the expression if valid; else report error. */
6840 if (!in_system_header_at (input_location
)
6841 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6842 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6843 "aggregate initialization");
6845 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
6846 DECL_INITIAL (decl
) = value
;
6848 /* ANSI wants warnings about out-of-range constant initializers. */
6849 STRIP_TYPE_NOPS (value
);
6850 if (TREE_STATIC (decl
))
6851 constant_expression_warning (value
);
6853 /* Check if we need to set array size from compound literal size. */
6854 if (TREE_CODE (type
) == ARRAY_TYPE
6855 && TYPE_DOMAIN (type
) == 0
6856 && value
!= error_mark_node
)
6858 tree inside_init
= init
;
6860 STRIP_TYPE_NOPS (inside_init
);
6861 inside_init
= fold (inside_init
);
6863 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6865 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6867 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6869 /* For int foo[] = (int [3]){1}; we need to set array size
6870 now since later on array initializer will be just the
6871 brace enclosed list of the compound literal. */
6872 tree etype
= strip_array_types (TREE_TYPE (decl
));
6873 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6874 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6876 layout_decl (cldecl
, 0);
6878 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6884 /* Methods for storing and printing names for error messages. */
6886 /* Implement a spelling stack that allows components of a name to be pushed
6887 and popped. Each element on the stack is this structure. */
6894 unsigned HOST_WIDE_INT i
;
6899 #define SPELLING_STRING 1
6900 #define SPELLING_MEMBER 2
6901 #define SPELLING_BOUNDS 3
6903 static struct spelling
*spelling
; /* Next stack element (unused). */
6904 static struct spelling
*spelling_base
; /* Spelling stack base. */
6905 static int spelling_size
; /* Size of the spelling stack. */
6907 /* Macros to save and restore the spelling stack around push_... functions.
6908 Alternative to SAVE_SPELLING_STACK. */
6910 #define SPELLING_DEPTH() (spelling - spelling_base)
6911 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6913 /* Push an element on the spelling stack with type KIND and assign VALUE
6916 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6918 int depth = SPELLING_DEPTH (); \
6920 if (depth >= spelling_size) \
6922 spelling_size += 10; \
6923 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6925 RESTORE_SPELLING_DEPTH (depth); \
6928 spelling->kind = (KIND); \
6929 spelling->MEMBER = (VALUE); \
6933 /* Push STRING on the stack. Printed literally. */
6936 push_string (const char *string
)
6938 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6941 /* Push a member name on the stack. Printed as '.' STRING. */
6944 push_member_name (tree decl
)
6946 const char *const string
6948 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6949 : _("<anonymous>"));
6950 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6953 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6956 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6958 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6961 /* Compute the maximum size in bytes of the printed spelling. */
6964 spelling_length (void)
6969 for (p
= spelling_base
; p
< spelling
; p
++)
6971 if (p
->kind
== SPELLING_BOUNDS
)
6974 size
+= strlen (p
->u
.s
) + 1;
6980 /* Print the spelling to BUFFER and return it. */
6983 print_spelling (char *buffer
)
6988 for (p
= spelling_base
; p
< spelling
; p
++)
6989 if (p
->kind
== SPELLING_BOUNDS
)
6991 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6997 if (p
->kind
== SPELLING_MEMBER
)
6999 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
7006 /* Digest the parser output INIT as an initializer for type TYPE.
7007 Return a C expression of type TYPE to represent the initial value.
7009 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7011 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7013 If INIT is a string constant, STRICT_STRING is true if it is
7014 unparenthesized or we should not warn here for it being parenthesized.
7015 For other types of INIT, STRICT_STRING is not used.
7017 INIT_LOC is the location of the INIT.
7019 REQUIRE_CONSTANT requests an error if non-constant initializers or
7020 elements are seen. */
7023 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
7024 bool null_pointer_constant
, bool strict_string
,
7025 int require_constant
)
7027 enum tree_code code
= TREE_CODE (type
);
7028 tree inside_init
= init
;
7029 tree semantic_type
= NULL_TREE
;
7030 bool maybe_const
= true;
7032 if (type
== error_mark_node
7034 || error_operand_p (init
))
7035 return error_mark_node
;
7037 STRIP_TYPE_NOPS (inside_init
);
7039 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
7041 semantic_type
= TREE_TYPE (inside_init
);
7042 inside_init
= TREE_OPERAND (inside_init
, 0);
7044 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
7045 inside_init
= decl_constant_value_for_optimization (inside_init
);
7047 /* Initialization of an array of chars from a string constant
7048 optionally enclosed in braces. */
7050 if (code
== ARRAY_TYPE
&& inside_init
7051 && TREE_CODE (inside_init
) == STRING_CST
)
7054 = (TYPE_ATOMIC (TREE_TYPE (type
))
7055 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
7057 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
7058 /* Note that an array could be both an array of character type
7059 and an array of wchar_t if wchar_t is signed char or unsigned
7061 bool char_array
= (typ1
== char_type_node
7062 || typ1
== signed_char_type_node
7063 || typ1
== unsigned_char_type_node
);
7064 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
7065 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
7066 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
7068 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
7071 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
7072 expr
.value
= inside_init
;
7073 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
7074 expr
.original_type
= NULL
;
7075 maybe_warn_string_init (init_loc
, type
, expr
);
7077 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
7078 pedwarn_init (init_loc
, OPT_Wpedantic
,
7079 "initialization of a flexible array member");
7081 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7082 TYPE_MAIN_VARIANT (type
)))
7087 if (typ2
!= char_type_node
)
7089 error_init (init_loc
, "char-array initialized from wide "
7091 return error_mark_node
;
7096 if (typ2
== char_type_node
)
7098 error_init (init_loc
, "wide character array initialized "
7099 "from non-wide string");
7100 return error_mark_node
;
7102 else if (!comptypes(typ1
, typ2
))
7104 error_init (init_loc
, "wide character array initialized "
7105 "from incompatible wide string");
7106 return error_mark_node
;
7110 TREE_TYPE (inside_init
) = type
;
7111 if (TYPE_DOMAIN (type
) != 0
7112 && TYPE_SIZE (type
) != 0
7113 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
7115 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
7117 /* Subtract the size of a single (possibly wide) character
7118 because it's ok to ignore the terminating null char
7119 that is counted in the length of the constant. */
7120 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
7122 - (TYPE_PRECISION (typ1
)
7124 pedwarn_init (init_loc
, 0,
7125 ("initializer-string for array of chars "
7127 else if (warn_cxx_compat
7128 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
7129 warning_at (init_loc
, OPT_Wc___compat
,
7130 ("initializer-string for array chars "
7131 "is too long for C++"));
7136 else if (INTEGRAL_TYPE_P (typ1
))
7138 error_init (init_loc
, "array of inappropriate type initialized "
7139 "from string constant");
7140 return error_mark_node
;
7144 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7145 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7146 below and handle as a constructor. */
7147 if (code
== VECTOR_TYPE
7148 && VECTOR_TYPE_P (TREE_TYPE (inside_init
))
7149 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
7150 && TREE_CONSTANT (inside_init
))
7152 if (TREE_CODE (inside_init
) == VECTOR_CST
7153 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7154 TYPE_MAIN_VARIANT (type
)))
7157 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
7159 unsigned HOST_WIDE_INT ix
;
7161 bool constant_p
= true;
7163 /* Iterate through elements and check if all constructor
7164 elements are *_CSTs. */
7165 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
7166 if (!CONSTANT_CLASS_P (value
))
7173 return build_vector_from_ctor (type
,
7174 CONSTRUCTOR_ELTS (inside_init
));
7178 if (warn_sequence_point
)
7179 verify_sequence_points (inside_init
);
7181 /* Any type can be initialized
7182 from an expression of the same type, optionally with braces. */
7184 if (inside_init
&& TREE_TYPE (inside_init
) != 0
7185 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
7186 TYPE_MAIN_VARIANT (type
))
7187 || (code
== ARRAY_TYPE
7188 && comptypes (TREE_TYPE (inside_init
), type
))
7189 || (code
== VECTOR_TYPE
7190 && comptypes (TREE_TYPE (inside_init
), type
))
7191 || (code
== POINTER_TYPE
7192 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
7193 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
7194 TREE_TYPE (type
)))))
7196 if (code
== POINTER_TYPE
)
7198 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
7200 if (TREE_CODE (inside_init
) == STRING_CST
7201 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7202 inside_init
= array_to_pointer_conversion
7203 (init_loc
, inside_init
);
7206 error_init (init_loc
, "invalid use of non-lvalue array");
7207 return error_mark_node
;
7212 if (code
== VECTOR_TYPE
)
7213 /* Although the types are compatible, we may require a
7215 inside_init
= convert (type
, inside_init
);
7217 if (require_constant
7218 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
7220 /* As an extension, allow initializing objects with static storage
7221 duration with compound literals (which are then treated just as
7222 the brace enclosed list they contain). Also allow this for
7223 vectors, as we can only assign them with compound literals. */
7224 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
7225 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
7227 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
7228 inside_init
= DECL_INITIAL (decl
);
7231 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
7232 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
7234 error_init (init_loc
, "array initialized from non-constant array "
7236 return error_mark_node
;
7239 /* Compound expressions can only occur here if -Wpedantic or
7240 -pedantic-errors is specified. In the later case, we always want
7241 an error. In the former case, we simply want a warning. */
7242 if (require_constant
&& pedantic
7243 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
7246 = valid_compound_expr_initializer (inside_init
,
7247 TREE_TYPE (inside_init
));
7248 if (inside_init
== error_mark_node
)
7249 error_init (init_loc
, "initializer element is not constant");
7251 pedwarn_init (init_loc
, OPT_Wpedantic
,
7252 "initializer element is not constant");
7253 if (flag_pedantic_errors
)
7254 inside_init
= error_mark_node
;
7256 else if (require_constant
7257 && !initializer_constant_valid_p (inside_init
,
7258 TREE_TYPE (inside_init
)))
7260 error_init (init_loc
, "initializer element is not constant");
7261 inside_init
= error_mark_node
;
7263 else if (require_constant
&& !maybe_const
)
7264 pedwarn_init (init_loc
, OPT_Wpedantic
,
7265 "initializer element is not a constant expression");
7267 /* Added to enable additional -Wsuggest-attribute=format warnings. */
7268 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
7269 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
7270 type
, inside_init
, origtype
,
7271 ic_init
, null_pointer_constant
,
7272 NULL_TREE
, NULL_TREE
, 0);
7276 /* Handle scalar types, including conversions. */
7278 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
7279 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
7280 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
7282 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
7283 && (TREE_CODE (init
) == STRING_CST
7284 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
7285 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
7287 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
7290 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
7291 inside_init
, origtype
, ic_init
,
7292 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
7295 /* Check to see if we have already given an error message. */
7296 if (inside_init
== error_mark_node
)
7298 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
7300 error_init (init_loc
, "initializer element is not constant");
7301 inside_init
= error_mark_node
;
7303 else if (require_constant
7304 && !initializer_constant_valid_p (inside_init
,
7305 TREE_TYPE (inside_init
)))
7307 error_init (init_loc
, "initializer element is not computable at "
7309 inside_init
= error_mark_node
;
7311 else if (require_constant
&& !maybe_const
)
7312 pedwarn_init (init_loc
, OPT_Wpedantic
,
7313 "initializer element is not a constant expression");
7318 /* Come here only for records and arrays. */
7320 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
7322 error_init (init_loc
, "variable-sized object may not be initialized");
7323 return error_mark_node
;
7326 error_init (init_loc
, "invalid initializer");
7327 return error_mark_node
;
7330 /* Handle initializers that use braces. */
7332 /* Type of object we are accumulating a constructor for.
7333 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7334 static tree constructor_type
;
7336 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7338 static tree constructor_fields
;
7340 /* For an ARRAY_TYPE, this is the specified index
7341 at which to store the next element we get. */
7342 static tree constructor_index
;
7344 /* For an ARRAY_TYPE, this is the maximum index. */
7345 static tree constructor_max_index
;
7347 /* For a RECORD_TYPE, this is the first field not yet written out. */
7348 static tree constructor_unfilled_fields
;
7350 /* For an ARRAY_TYPE, this is the index of the first element
7351 not yet written out. */
7352 static tree constructor_unfilled_index
;
7354 /* In a RECORD_TYPE, the byte index of the next consecutive field.
7355 This is so we can generate gaps between fields, when appropriate. */
7356 static tree constructor_bit_index
;
7358 /* If we are saving up the elements rather than allocating them,
7359 this is the list of elements so far (in reverse order,
7360 most recent first). */
7361 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
7363 /* 1 if constructor should be incrementally stored into a constructor chain,
7364 0 if all the elements should be kept in AVL tree. */
7365 static int constructor_incremental
;
7367 /* 1 if so far this constructor's elements are all compile-time constants. */
7368 static int constructor_constant
;
7370 /* 1 if so far this constructor's elements are all valid address constants. */
7371 static int constructor_simple
;
7373 /* 1 if this constructor has an element that cannot be part of a
7374 constant expression. */
7375 static int constructor_nonconst
;
7377 /* 1 if this constructor is erroneous so far. */
7378 static int constructor_erroneous
;
7380 /* 1 if this constructor is the universal zero initializer { 0 }. */
7381 static int constructor_zeroinit
;
7383 /* Structure for managing pending initializer elements, organized as an
7388 struct init_node
*left
, *right
;
7389 struct init_node
*parent
;
7396 /* Tree of pending elements at this constructor level.
7397 These are elements encountered out of order
7398 which belong at places we haven't reached yet in actually
7400 Will never hold tree nodes across GC runs. */
7401 static struct init_node
*constructor_pending_elts
;
7403 /* The SPELLING_DEPTH of this constructor. */
7404 static int constructor_depth
;
7406 /* DECL node for which an initializer is being read.
7407 0 means we are reading a constructor expression
7408 such as (struct foo) {...}. */
7409 static tree constructor_decl
;
7411 /* Nonzero if this is an initializer for a top-level decl. */
7412 static int constructor_top_level
;
7414 /* Nonzero if there were any member designators in this initializer. */
7415 static int constructor_designated
;
7417 /* Nesting depth of designator list. */
7418 static int designator_depth
;
7420 /* Nonzero if there were diagnosed errors in this designator list. */
7421 static int designator_erroneous
;
7424 /* This stack has a level for each implicit or explicit level of
7425 structuring in the initializer, including the outermost one. It
7426 saves the values of most of the variables above. */
7428 struct constructor_range_stack
;
7430 struct constructor_stack
7432 struct constructor_stack
*next
;
7437 tree unfilled_index
;
7438 tree unfilled_fields
;
7440 vec
<constructor_elt
, va_gc
> *elements
;
7441 struct init_node
*pending_elts
;
7444 /* If value nonzero, this value should replace the entire
7445 constructor at this level. */
7446 struct c_expr replacement_value
;
7447 struct constructor_range_stack
*range_stack
;
7456 int designator_depth
;
7459 static struct constructor_stack
*constructor_stack
;
7461 /* This stack represents designators from some range designator up to
7462 the last designator in the list. */
7464 struct constructor_range_stack
7466 struct constructor_range_stack
*next
, *prev
;
7467 struct constructor_stack
*stack
;
7474 static struct constructor_range_stack
*constructor_range_stack
;
7476 /* This stack records separate initializers that are nested.
7477 Nested initializers can't happen in ANSI C, but GNU C allows them
7478 in cases like { ... (struct foo) { ... } ... }. */
7480 struct initializer_stack
7482 struct initializer_stack
*next
;
7484 struct constructor_stack
*constructor_stack
;
7485 struct constructor_range_stack
*constructor_range_stack
;
7486 vec
<constructor_elt
, va_gc
> *elements
;
7487 struct spelling
*spelling
;
7488 struct spelling
*spelling_base
;
7491 char require_constant_value
;
7492 char require_constant_elements
;
7495 static struct initializer_stack
*initializer_stack
;
7497 /* Prepare to parse and output the initializer for variable DECL. */
7500 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
7503 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
7505 p
->decl
= constructor_decl
;
7506 p
->require_constant_value
= require_constant_value
;
7507 p
->require_constant_elements
= require_constant_elements
;
7508 p
->constructor_stack
= constructor_stack
;
7509 p
->constructor_range_stack
= constructor_range_stack
;
7510 p
->elements
= constructor_elements
;
7511 p
->spelling
= spelling
;
7512 p
->spelling_base
= spelling_base
;
7513 p
->spelling_size
= spelling_size
;
7514 p
->top_level
= constructor_top_level
;
7515 p
->next
= initializer_stack
;
7516 initializer_stack
= p
;
7518 constructor_decl
= decl
;
7519 constructor_designated
= 0;
7520 constructor_top_level
= top_level
;
7522 if (decl
!= 0 && decl
!= error_mark_node
)
7524 require_constant_value
= TREE_STATIC (decl
);
7525 require_constant_elements
7526 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
7527 /* For a scalar, you can always use any value to initialize,
7528 even within braces. */
7529 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)));
7530 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
7534 require_constant_value
= 0;
7535 require_constant_elements
= 0;
7536 locus
= _("(anonymous)");
7539 constructor_stack
= 0;
7540 constructor_range_stack
= 0;
7542 found_missing_braces
= 0;
7546 RESTORE_SPELLING_DEPTH (0);
7549 push_string (locus
);
7555 struct initializer_stack
*p
= initializer_stack
;
7557 /* Free the whole constructor stack of this initializer. */
7558 while (constructor_stack
)
7560 struct constructor_stack
*q
= constructor_stack
;
7561 constructor_stack
= q
->next
;
7565 gcc_assert (!constructor_range_stack
);
7567 /* Pop back to the data of the outer initializer (if any). */
7568 free (spelling_base
);
7570 constructor_decl
= p
->decl
;
7571 require_constant_value
= p
->require_constant_value
;
7572 require_constant_elements
= p
->require_constant_elements
;
7573 constructor_stack
= p
->constructor_stack
;
7574 constructor_range_stack
= p
->constructor_range_stack
;
7575 constructor_elements
= p
->elements
;
7576 spelling
= p
->spelling
;
7577 spelling_base
= p
->spelling_base
;
7578 spelling_size
= p
->spelling_size
;
7579 constructor_top_level
= p
->top_level
;
7580 initializer_stack
= p
->next
;
7584 /* Call here when we see the initializer is surrounded by braces.
7585 This is instead of a call to push_init_level;
7586 it is matched by a call to pop_init_level.
7588 TYPE is the type to initialize, for a constructor expression.
7589 For an initializer for a decl, TYPE is zero. */
7592 really_start_incremental_init (tree type
)
7594 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7597 type
= TREE_TYPE (constructor_decl
);
7599 if (VECTOR_TYPE_P (type
)
7600 && TYPE_VECTOR_OPAQUE (type
))
7601 error ("opaque vector types cannot be initialized");
7603 p
->type
= constructor_type
;
7604 p
->fields
= constructor_fields
;
7605 p
->index
= constructor_index
;
7606 p
->max_index
= constructor_max_index
;
7607 p
->unfilled_index
= constructor_unfilled_index
;
7608 p
->unfilled_fields
= constructor_unfilled_fields
;
7609 p
->bit_index
= constructor_bit_index
;
7610 p
->elements
= constructor_elements
;
7611 p
->constant
= constructor_constant
;
7612 p
->simple
= constructor_simple
;
7613 p
->nonconst
= constructor_nonconst
;
7614 p
->erroneous
= constructor_erroneous
;
7615 p
->pending_elts
= constructor_pending_elts
;
7616 p
->depth
= constructor_depth
;
7617 p
->replacement_value
.value
= 0;
7618 p
->replacement_value
.original_code
= ERROR_MARK
;
7619 p
->replacement_value
.original_type
= NULL
;
7623 p
->incremental
= constructor_incremental
;
7624 p
->designated
= constructor_designated
;
7625 p
->designator_depth
= designator_depth
;
7627 constructor_stack
= p
;
7629 constructor_constant
= 1;
7630 constructor_simple
= 1;
7631 constructor_nonconst
= 0;
7632 constructor_depth
= SPELLING_DEPTH ();
7633 constructor_elements
= NULL
;
7634 constructor_pending_elts
= 0;
7635 constructor_type
= type
;
7636 constructor_incremental
= 1;
7637 constructor_designated
= 0;
7638 constructor_zeroinit
= 1;
7639 designator_depth
= 0;
7640 designator_erroneous
= 0;
7642 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
7644 constructor_fields
= TYPE_FIELDS (constructor_type
);
7645 /* Skip any nameless bit fields at the beginning. */
7646 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7647 && DECL_NAME (constructor_fields
) == 0)
7648 constructor_fields
= DECL_CHAIN (constructor_fields
);
7650 constructor_unfilled_fields
= constructor_fields
;
7651 constructor_bit_index
= bitsize_zero_node
;
7653 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7655 if (TYPE_DOMAIN (constructor_type
))
7657 constructor_max_index
7658 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7660 /* Detect non-empty initializations of zero-length arrays. */
7661 if (constructor_max_index
== NULL_TREE
7662 && TYPE_SIZE (constructor_type
))
7663 constructor_max_index
= integer_minus_one_node
;
7665 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7666 to initialize VLAs will cause a proper error; avoid tree
7667 checking errors as well by setting a safe value. */
7668 if (constructor_max_index
7669 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7670 constructor_max_index
= integer_minus_one_node
;
7673 = convert (bitsizetype
,
7674 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7678 constructor_index
= bitsize_zero_node
;
7679 constructor_max_index
= NULL_TREE
;
7682 constructor_unfilled_index
= constructor_index
;
7684 else if (VECTOR_TYPE_P (constructor_type
))
7686 /* Vectors are like simple fixed-size arrays. */
7687 constructor_max_index
=
7688 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7689 constructor_index
= bitsize_zero_node
;
7690 constructor_unfilled_index
= constructor_index
;
7694 /* Handle the case of int x = {5}; */
7695 constructor_fields
= constructor_type
;
7696 constructor_unfilled_fields
= constructor_type
;
7700 /* Called when we see an open brace for a nested initializer. Finish
7701 off any pending levels with implicit braces. */
7703 finish_implicit_inits (location_t loc
, struct obstack
*braced_init_obstack
)
7705 while (constructor_stack
->implicit
)
7707 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
7708 && constructor_fields
== 0)
7709 process_init_element (input_location
,
7710 pop_init_level (loc
, 1, braced_init_obstack
),
7711 true, braced_init_obstack
);
7712 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7713 && constructor_max_index
7714 && tree_int_cst_lt (constructor_max_index
,
7716 process_init_element (input_location
,
7717 pop_init_level (loc
, 1, braced_init_obstack
),
7718 true, braced_init_obstack
);
7724 /* Push down into a subobject, for initialization.
7725 If this is for an explicit set of braces, IMPLICIT is 0.
7726 If it is because the next element belongs at a lower level,
7727 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7730 push_init_level (location_t loc
, int implicit
,
7731 struct obstack
*braced_init_obstack
)
7733 struct constructor_stack
*p
;
7734 tree value
= NULL_TREE
;
7736 /* Unless this is an explicit brace, we need to preserve previous
7740 if (RECORD_OR_UNION_TYPE_P (constructor_type
) && constructor_fields
)
7741 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7742 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7743 value
= find_init_member (constructor_index
, braced_init_obstack
);
7746 p
= XNEW (struct constructor_stack
);
7747 p
->type
= constructor_type
;
7748 p
->fields
= constructor_fields
;
7749 p
->index
= constructor_index
;
7750 p
->max_index
= constructor_max_index
;
7751 p
->unfilled_index
= constructor_unfilled_index
;
7752 p
->unfilled_fields
= constructor_unfilled_fields
;
7753 p
->bit_index
= constructor_bit_index
;
7754 p
->elements
= constructor_elements
;
7755 p
->constant
= constructor_constant
;
7756 p
->simple
= constructor_simple
;
7757 p
->nonconst
= constructor_nonconst
;
7758 p
->erroneous
= constructor_erroneous
;
7759 p
->pending_elts
= constructor_pending_elts
;
7760 p
->depth
= constructor_depth
;
7761 p
->replacement_value
.value
= 0;
7762 p
->replacement_value
.original_code
= ERROR_MARK
;
7763 p
->replacement_value
.original_type
= NULL
;
7764 p
->implicit
= implicit
;
7766 p
->incremental
= constructor_incremental
;
7767 p
->designated
= constructor_designated
;
7768 p
->designator_depth
= designator_depth
;
7769 p
->next
= constructor_stack
;
7771 constructor_stack
= p
;
7773 constructor_constant
= 1;
7774 constructor_simple
= 1;
7775 constructor_nonconst
= 0;
7776 constructor_depth
= SPELLING_DEPTH ();
7777 constructor_elements
= NULL
;
7778 constructor_incremental
= 1;
7779 constructor_designated
= 0;
7780 constructor_pending_elts
= 0;
7783 p
->range_stack
= constructor_range_stack
;
7784 constructor_range_stack
= 0;
7785 designator_depth
= 0;
7786 designator_erroneous
= 0;
7789 /* Don't die if an entire brace-pair level is superfluous
7790 in the containing level. */
7791 if (constructor_type
== 0)
7793 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
7795 /* Don't die if there are extra init elts at the end. */
7796 if (constructor_fields
== 0)
7797 constructor_type
= 0;
7800 constructor_type
= TREE_TYPE (constructor_fields
);
7801 push_member_name (constructor_fields
);
7802 constructor_depth
++;
7804 /* If upper initializer is designated, then mark this as
7805 designated too to prevent bogus warnings. */
7806 constructor_designated
= p
->designated
;
7808 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7810 constructor_type
= TREE_TYPE (constructor_type
);
7811 push_array_bounds (tree_to_uhwi (constructor_index
));
7812 constructor_depth
++;
7815 if (constructor_type
== 0)
7817 error_init (loc
, "extra brace group at end of initializer");
7818 constructor_fields
= 0;
7819 constructor_unfilled_fields
= 0;
7823 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7825 constructor_constant
= TREE_CONSTANT (value
);
7826 constructor_simple
= TREE_STATIC (value
);
7827 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7828 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7829 if (!vec_safe_is_empty (constructor_elements
)
7830 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7831 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7832 set_nonincremental_init (braced_init_obstack
);
7836 found_missing_braces
= 1;
7838 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
7840 constructor_fields
= TYPE_FIELDS (constructor_type
);
7841 /* Skip any nameless bit fields at the beginning. */
7842 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7843 && DECL_NAME (constructor_fields
) == 0)
7844 constructor_fields
= DECL_CHAIN (constructor_fields
);
7846 constructor_unfilled_fields
= constructor_fields
;
7847 constructor_bit_index
= bitsize_zero_node
;
7849 else if (VECTOR_TYPE_P (constructor_type
))
7851 /* Vectors are like simple fixed-size arrays. */
7852 constructor_max_index
=
7853 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7854 constructor_index
= bitsize_int (0);
7855 constructor_unfilled_index
= constructor_index
;
7857 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7859 if (TYPE_DOMAIN (constructor_type
))
7861 constructor_max_index
7862 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7864 /* Detect non-empty initializations of zero-length arrays. */
7865 if (constructor_max_index
== NULL_TREE
7866 && TYPE_SIZE (constructor_type
))
7867 constructor_max_index
= integer_minus_one_node
;
7869 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7870 to initialize VLAs will cause a proper error; avoid tree
7871 checking errors as well by setting a safe value. */
7872 if (constructor_max_index
7873 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7874 constructor_max_index
= integer_minus_one_node
;
7877 = convert (bitsizetype
,
7878 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7881 constructor_index
= bitsize_zero_node
;
7883 constructor_unfilled_index
= constructor_index
;
7884 if (value
&& TREE_CODE (value
) == STRING_CST
)
7886 /* We need to split the char/wchar array into individual
7887 characters, so that we don't have to special case it
7889 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7894 if (constructor_type
!= error_mark_node
)
7895 warning_init (input_location
, 0, "braces around scalar initializer");
7896 constructor_fields
= constructor_type
;
7897 constructor_unfilled_fields
= constructor_type
;
7901 /* At the end of an implicit or explicit brace level,
7902 finish up that level of constructor. If a single expression
7903 with redundant braces initialized that level, return the
7904 c_expr structure for that expression. Otherwise, the original_code
7905 element is set to ERROR_MARK.
7906 If we were outputting the elements as they are read, return 0 as the value
7907 from inner levels (process_init_element ignores that),
7908 but return error_mark_node as the value from the outermost level
7909 (that's what we want to put in DECL_INITIAL).
7910 Otherwise, return a CONSTRUCTOR expression as the value. */
7913 pop_init_level (location_t loc
, int implicit
,
7914 struct obstack
*braced_init_obstack
)
7916 struct constructor_stack
*p
;
7919 ret
.original_code
= ERROR_MARK
;
7920 ret
.original_type
= NULL
;
7924 /* When we come to an explicit close brace,
7925 pop any inner levels that didn't have explicit braces. */
7926 while (constructor_stack
->implicit
)
7927 process_init_element (input_location
,
7928 pop_init_level (loc
, 1, braced_init_obstack
),
7929 true, braced_init_obstack
);
7930 gcc_assert (!constructor_range_stack
);
7933 /* Now output all pending elements. */
7934 constructor_incremental
= 1;
7935 output_pending_init_elements (1, braced_init_obstack
);
7937 p
= constructor_stack
;
7939 /* Error for initializing a flexible array member, or a zero-length
7940 array member in an inappropriate context. */
7941 if (constructor_type
&& constructor_fields
7942 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7943 && TYPE_DOMAIN (constructor_type
)
7944 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7946 /* Silently discard empty initializations. The parser will
7947 already have pedwarned for empty brackets. */
7948 if (integer_zerop (constructor_unfilled_index
))
7949 constructor_type
= NULL_TREE
;
7952 gcc_assert (!TYPE_SIZE (constructor_type
));
7954 if (constructor_depth
> 2)
7955 error_init (loc
, "initialization of flexible array member in a nested context");
7957 pedwarn_init (loc
, OPT_Wpedantic
,
7958 "initialization of a flexible array member");
7960 /* We have already issued an error message for the existence
7961 of a flexible array member not at the end of the structure.
7962 Discard the initializer so that we do not die later. */
7963 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7964 constructor_type
= NULL_TREE
;
7968 switch (vec_safe_length (constructor_elements
))
7971 /* Initialization with { } counts as zeroinit. */
7972 constructor_zeroinit
= 1;
7975 /* This might be zeroinit as well. */
7976 if (integer_zerop ((*constructor_elements
)[0].value
))
7977 constructor_zeroinit
= 1;
7980 /* If the constructor has more than one element, it can't be { 0 }. */
7981 constructor_zeroinit
= 0;
7985 /* Warn when some structs are initialized with direct aggregation. */
7986 if (!implicit
&& found_missing_braces
&& warn_missing_braces
7987 && !constructor_zeroinit
)
7988 warning_init (loc
, OPT_Wmissing_braces
,
7989 "missing braces around initializer");
7991 /* Warn when some struct elements are implicitly initialized to zero. */
7992 if (warn_missing_field_initializers
7994 && TREE_CODE (constructor_type
) == RECORD_TYPE
7995 && constructor_unfilled_fields
)
7997 /* Do not warn for flexible array members or zero-length arrays. */
7998 while (constructor_unfilled_fields
7999 && (!DECL_SIZE (constructor_unfilled_fields
)
8000 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
8001 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
8003 if (constructor_unfilled_fields
8004 /* Do not warn if this level of the initializer uses member
8005 designators; it is likely to be deliberate. */
8006 && !constructor_designated
8007 /* Do not warn about initializing with { 0 } or with { }. */
8008 && !constructor_zeroinit
)
8010 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
8011 "missing initializer for field %qD of %qT",
8012 constructor_unfilled_fields
,
8014 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
8015 "%qD declared here", constructor_unfilled_fields
);
8019 /* Pad out the end of the structure. */
8020 if (p
->replacement_value
.value
)
8021 /* If this closes a superfluous brace pair,
8022 just pass out the element between them. */
8023 ret
= p
->replacement_value
;
8024 else if (constructor_type
== 0)
8026 else if (!RECORD_OR_UNION_TYPE_P (constructor_type
)
8027 && TREE_CODE (constructor_type
) != ARRAY_TYPE
8028 && !VECTOR_TYPE_P (constructor_type
))
8030 /* A nonincremental scalar initializer--just return
8031 the element, after verifying there is just one. */
8032 if (vec_safe_is_empty (constructor_elements
))
8034 if (!constructor_erroneous
)
8035 error_init (loc
, "empty scalar initializer");
8036 ret
.value
= error_mark_node
;
8038 else if (vec_safe_length (constructor_elements
) != 1)
8040 error_init (loc
, "extra elements in scalar initializer");
8041 ret
.value
= (*constructor_elements
)[0].value
;
8044 ret
.value
= (*constructor_elements
)[0].value
;
8048 if (constructor_erroneous
)
8049 ret
.value
= error_mark_node
;
8052 ret
.value
= build_constructor (constructor_type
,
8053 constructor_elements
);
8054 if (constructor_constant
)
8055 TREE_CONSTANT (ret
.value
) = 1;
8056 if (constructor_constant
&& constructor_simple
)
8057 TREE_STATIC (ret
.value
) = 1;
8058 if (constructor_nonconst
)
8059 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
8063 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
8065 if (constructor_nonconst
)
8066 ret
.original_code
= C_MAYBE_CONST_EXPR
;
8067 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
8068 ret
.original_code
= ERROR_MARK
;
8071 constructor_type
= p
->type
;
8072 constructor_fields
= p
->fields
;
8073 constructor_index
= p
->index
;
8074 constructor_max_index
= p
->max_index
;
8075 constructor_unfilled_index
= p
->unfilled_index
;
8076 constructor_unfilled_fields
= p
->unfilled_fields
;
8077 constructor_bit_index
= p
->bit_index
;
8078 constructor_elements
= p
->elements
;
8079 constructor_constant
= p
->constant
;
8080 constructor_simple
= p
->simple
;
8081 constructor_nonconst
= p
->nonconst
;
8082 constructor_erroneous
= p
->erroneous
;
8083 constructor_incremental
= p
->incremental
;
8084 constructor_designated
= p
->designated
;
8085 designator_depth
= p
->designator_depth
;
8086 constructor_pending_elts
= p
->pending_elts
;
8087 constructor_depth
= p
->depth
;
8089 constructor_range_stack
= p
->range_stack
;
8090 RESTORE_SPELLING_DEPTH (constructor_depth
);
8092 constructor_stack
= p
->next
;
8095 if (ret
.value
== 0 && constructor_stack
== 0)
8096 ret
.value
= error_mark_node
;
8100 /* Common handling for both array range and field name designators.
8101 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8104 set_designator (location_t loc
, int array
,
8105 struct obstack
*braced_init_obstack
)
8108 enum tree_code subcode
;
8110 /* Don't die if an entire brace-pair level is superfluous
8111 in the containing level. */
8112 if (constructor_type
== 0)
8115 /* If there were errors in this designator list already, bail out
8117 if (designator_erroneous
)
8120 if (!designator_depth
)
8122 gcc_assert (!constructor_range_stack
);
8124 /* Designator list starts at the level of closest explicit
8126 while (constructor_stack
->implicit
)
8127 process_init_element (input_location
,
8128 pop_init_level (loc
, 1, braced_init_obstack
),
8129 true, braced_init_obstack
);
8130 constructor_designated
= 1;
8134 switch (TREE_CODE (constructor_type
))
8138 subtype
= TREE_TYPE (constructor_fields
);
8139 if (subtype
!= error_mark_node
)
8140 subtype
= TYPE_MAIN_VARIANT (subtype
);
8143 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8149 subcode
= TREE_CODE (subtype
);
8150 if (array
&& subcode
!= ARRAY_TYPE
)
8152 error_init (loc
, "array index in non-array initializer");
8155 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
8157 error_init (loc
, "field name not in record or union initializer");
8161 constructor_designated
= 1;
8162 finish_implicit_inits (loc
, braced_init_obstack
);
8163 push_init_level (loc
, 2, braced_init_obstack
);
8167 /* If there are range designators in designator list, push a new designator
8168 to constructor_range_stack. RANGE_END is end of such stack range or
8169 NULL_TREE if there is no range designator at this level. */
8172 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
8174 struct constructor_range_stack
*p
;
8176 p
= (struct constructor_range_stack
*)
8177 obstack_alloc (braced_init_obstack
,
8178 sizeof (struct constructor_range_stack
));
8179 p
->prev
= constructor_range_stack
;
8181 p
->fields
= constructor_fields
;
8182 p
->range_start
= constructor_index
;
8183 p
->index
= constructor_index
;
8184 p
->stack
= constructor_stack
;
8185 p
->range_end
= range_end
;
8186 if (constructor_range_stack
)
8187 constructor_range_stack
->next
= p
;
8188 constructor_range_stack
= p
;
8191 /* Within an array initializer, specify the next index to be initialized.
8192 FIRST is that index. If LAST is nonzero, then initialize a range
8193 of indices, running from FIRST through LAST. */
8196 set_init_index (location_t loc
, tree first
, tree last
,
8197 struct obstack
*braced_init_obstack
)
8199 if (set_designator (loc
, 1, braced_init_obstack
))
8202 designator_erroneous
= 1;
8204 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
8205 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
8207 error_init (loc
, "array index in initializer not of integer type");
8211 if (TREE_CODE (first
) != INTEGER_CST
)
8213 first
= c_fully_fold (first
, false, NULL
);
8214 if (TREE_CODE (first
) == INTEGER_CST
)
8215 pedwarn_init (loc
, OPT_Wpedantic
,
8216 "array index in initializer is not "
8217 "an integer constant expression");
8220 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
8222 last
= c_fully_fold (last
, false, NULL
);
8223 if (TREE_CODE (last
) == INTEGER_CST
)
8224 pedwarn_init (loc
, OPT_Wpedantic
,
8225 "array index in initializer is not "
8226 "an integer constant expression");
8229 if (TREE_CODE (first
) != INTEGER_CST
)
8230 error_init (loc
, "nonconstant array index in initializer");
8231 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
8232 error_init (loc
, "nonconstant array index in initializer");
8233 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8234 error_init (loc
, "array index in non-array initializer");
8235 else if (tree_int_cst_sgn (first
) == -1)
8236 error_init (loc
, "array index in initializer exceeds array bounds");
8237 else if (constructor_max_index
8238 && tree_int_cst_lt (constructor_max_index
, first
))
8239 error_init (loc
, "array index in initializer exceeds array bounds");
8242 constant_expression_warning (first
);
8244 constant_expression_warning (last
);
8245 constructor_index
= convert (bitsizetype
, first
);
8246 if (tree_int_cst_lt (constructor_index
, first
))
8248 constructor_index
= copy_node (constructor_index
);
8249 TREE_OVERFLOW (constructor_index
) = 1;
8254 if (tree_int_cst_equal (first
, last
))
8256 else if (tree_int_cst_lt (last
, first
))
8258 error_init (loc
, "empty index range in initializer");
8263 last
= convert (bitsizetype
, last
);
8264 if (constructor_max_index
!= 0
8265 && tree_int_cst_lt (constructor_max_index
, last
))
8267 error_init (loc
, "array index range in initializer exceeds "
8275 designator_erroneous
= 0;
8276 if (constructor_range_stack
|| last
)
8277 push_range_stack (last
, braced_init_obstack
);
8281 /* Within a struct initializer, specify the next field to be initialized. */
8284 set_init_label (location_t loc
, tree fieldname
, location_t fieldname_loc
,
8285 struct obstack
*braced_init_obstack
)
8289 if (set_designator (loc
, 0, braced_init_obstack
))
8292 designator_erroneous
= 1;
8294 if (!RECORD_OR_UNION_TYPE_P (constructor_type
))
8296 error_init (loc
, "field name not in record or union initializer");
8300 field
= lookup_field (constructor_type
, fieldname
);
8304 tree guessed_id
= lookup_field_fuzzy (constructor_type
, fieldname
);
8307 gcc_rich_location
rich_loc (fieldname_loc
);
8308 rich_loc
.add_fixit_misspelled_id (fieldname_loc
, guessed_id
);
8311 "%qT has no member named %qE; did you mean %qE?",
8312 constructor_type
, fieldname
, guessed_id
);
8315 error_at (fieldname_loc
, "%qT has no member named %qE",
8316 constructor_type
, fieldname
);
8321 constructor_fields
= TREE_VALUE (field
);
8323 designator_erroneous
= 0;
8324 if (constructor_range_stack
)
8325 push_range_stack (NULL_TREE
, braced_init_obstack
);
8326 field
= TREE_CHAIN (field
);
8329 if (set_designator (loc
, 0, braced_init_obstack
))
8333 while (field
!= NULL_TREE
);
8336 /* Add a new initializer to the tree of pending initializers. PURPOSE
8337 identifies the initializer, either array index or field in a structure.
8338 VALUE is the value of that index or field. If ORIGTYPE is not
8339 NULL_TREE, it is the original type of VALUE.
8341 IMPLICIT is true if value comes from pop_init_level (1),
8342 the new initializer has been merged with the existing one
8343 and thus no warnings should be emitted about overriding an
8344 existing initializer. */
8347 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
8348 bool implicit
, struct obstack
*braced_init_obstack
)
8350 struct init_node
*p
, **q
, *r
;
8352 q
= &constructor_pending_elts
;
8355 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8360 if (tree_int_cst_lt (purpose
, p
->purpose
))
8362 else if (tree_int_cst_lt (p
->purpose
, purpose
))
8368 if (TREE_SIDE_EFFECTS (p
->value
))
8369 warning_init (loc
, OPT_Woverride_init_side_effects
,
8370 "initialized field with side-effects "
8372 else if (warn_override_init
)
8373 warning_init (loc
, OPT_Woverride_init
,
8374 "initialized field overwritten");
8377 p
->origtype
= origtype
;
8386 bitpos
= bit_position (purpose
);
8390 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8392 else if (p
->purpose
!= purpose
)
8398 if (TREE_SIDE_EFFECTS (p
->value
))
8399 warning_init (loc
, OPT_Woverride_init_side_effects
,
8400 "initialized field with side-effects "
8402 else if (warn_override_init
)
8403 warning_init (loc
, OPT_Woverride_init
,
8404 "initialized field overwritten");
8407 p
->origtype
= origtype
;
8413 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
8414 sizeof (struct init_node
));
8415 r
->purpose
= purpose
;
8417 r
->origtype
= origtype
;
8427 struct init_node
*s
;
8431 if (p
->balance
== 0)
8433 else if (p
->balance
< 0)
8440 p
->left
->parent
= p
;
8457 constructor_pending_elts
= r
;
8462 struct init_node
*t
= r
->right
;
8466 r
->right
->parent
= r
;
8471 p
->left
->parent
= p
;
8474 p
->balance
= t
->balance
< 0;
8475 r
->balance
= -(t
->balance
> 0);
8490 constructor_pending_elts
= t
;
8496 /* p->balance == +1; growth of left side balances the node. */
8501 else /* r == p->right */
8503 if (p
->balance
== 0)
8504 /* Growth propagation from right side. */
8506 else if (p
->balance
> 0)
8513 p
->right
->parent
= p
;
8530 constructor_pending_elts
= r
;
8532 else /* r->balance == -1 */
8535 struct init_node
*t
= r
->left
;
8539 r
->left
->parent
= r
;
8544 p
->right
->parent
= p
;
8547 r
->balance
= (t
->balance
< 0);
8548 p
->balance
= -(t
->balance
> 0);
8563 constructor_pending_elts
= t
;
8569 /* p->balance == -1; growth of right side balances the node. */
8580 /* Build AVL tree from a sorted chain. */
8583 set_nonincremental_init (struct obstack
* braced_init_obstack
)
8585 unsigned HOST_WIDE_INT ix
;
8588 if (TREE_CODE (constructor_type
) != RECORD_TYPE
8589 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8592 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8593 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
8594 braced_init_obstack
);
8595 constructor_elements
= NULL
;
8596 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8598 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
8599 /* Skip any nameless bit fields at the beginning. */
8600 while (constructor_unfilled_fields
!= 0
8601 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8602 && DECL_NAME (constructor_unfilled_fields
) == 0)
8603 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
8606 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8608 if (TYPE_DOMAIN (constructor_type
))
8609 constructor_unfilled_index
8610 = convert (bitsizetype
,
8611 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8613 constructor_unfilled_index
= bitsize_zero_node
;
8615 constructor_incremental
= 0;
8618 /* Build AVL tree from a string constant. */
8621 set_nonincremental_init_from_string (tree str
,
8622 struct obstack
* braced_init_obstack
)
8624 tree value
, purpose
, type
;
8625 HOST_WIDE_INT val
[2];
8626 const char *p
, *end
;
8627 int byte
, wchar_bytes
, charwidth
, bitpos
;
8629 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8631 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8632 charwidth
= TYPE_PRECISION (char_type_node
);
8633 gcc_assert ((size_t) wchar_bytes
* charwidth
8634 <= ARRAY_SIZE (val
) * HOST_BITS_PER_WIDE_INT
);
8635 type
= TREE_TYPE (constructor_type
);
8636 p
= TREE_STRING_POINTER (str
);
8637 end
= p
+ TREE_STRING_LENGTH (str
);
8639 for (purpose
= bitsize_zero_node
;
8641 && !(constructor_max_index
8642 && tree_int_cst_lt (constructor_max_index
, purpose
));
8643 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8645 if (wchar_bytes
== 1)
8647 val
[0] = (unsigned char) *p
++;
8654 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8656 if (BYTES_BIG_ENDIAN
)
8657 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8659 bitpos
= byte
* charwidth
;
8660 val
[bitpos
/ HOST_BITS_PER_WIDE_INT
]
8661 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8662 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8666 if (!TYPE_UNSIGNED (type
))
8668 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8669 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8671 if (val
[0] & (HOST_WIDE_INT_1
<< (bitpos
- 1)))
8673 val
[0] |= HOST_WIDE_INT_M1U
<< bitpos
;
8677 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8682 else if (val
[1] & (HOST_WIDE_INT_1
8683 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8684 val
[1] |= HOST_WIDE_INT_M1U
<< (bitpos
- HOST_BITS_PER_WIDE_INT
);
8687 value
= wide_int_to_tree (type
,
8688 wide_int::from_array (val
, 2,
8689 HOST_BITS_PER_WIDE_INT
* 2));
8690 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
8691 braced_init_obstack
);
8694 constructor_incremental
= 0;
8697 /* Return value of FIELD in pending initializer or zero if the field was
8698 not initialized yet. */
8701 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8703 struct init_node
*p
;
8705 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8707 if (constructor_incremental
8708 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8709 set_nonincremental_init (braced_init_obstack
);
8711 p
= constructor_pending_elts
;
8714 if (tree_int_cst_lt (field
, p
->purpose
))
8716 else if (tree_int_cst_lt (p
->purpose
, field
))
8722 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8724 tree bitpos
= bit_position (field
);
8726 if (constructor_incremental
8727 && (!constructor_unfilled_fields
8728 || tree_int_cst_lt (bitpos
,
8729 bit_position (constructor_unfilled_fields
))))
8730 set_nonincremental_init (braced_init_obstack
);
8732 p
= constructor_pending_elts
;
8735 if (field
== p
->purpose
)
8737 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8743 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8745 if (!vec_safe_is_empty (constructor_elements
)
8746 && (constructor_elements
->last ().index
== field
))
8747 return constructor_elements
->last ().value
;
8752 /* "Output" the next constructor element.
8753 At top level, really output it to assembler code now.
8754 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8755 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8756 TYPE is the data type that the containing data type wants here.
8757 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8758 If VALUE is a string constant, STRICT_STRING is true if it is
8759 unparenthesized or we should not warn here for it being parenthesized.
8760 For other types of VALUE, STRICT_STRING is not used.
8762 PENDING if non-nil means output pending elements that belong
8763 right after this element. (PENDING is normally 1;
8764 it is 0 while outputting pending elements, to avoid recursion.)
8766 IMPLICIT is true if value comes from pop_init_level (1),
8767 the new initializer has been merged with the existing one
8768 and thus no warnings should be emitted about overriding an
8769 existing initializer. */
8772 output_init_element (location_t loc
, tree value
, tree origtype
,
8773 bool strict_string
, tree type
, tree field
, int pending
,
8774 bool implicit
, struct obstack
* braced_init_obstack
)
8776 tree semantic_type
= NULL_TREE
;
8777 bool maybe_const
= true;
8780 if (type
== error_mark_node
|| value
== error_mark_node
)
8782 constructor_erroneous
= 1;
8785 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8786 && (TREE_CODE (value
) == STRING_CST
8787 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8788 && !(TREE_CODE (value
) == STRING_CST
8789 && TREE_CODE (type
) == ARRAY_TYPE
8790 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8791 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8792 TYPE_MAIN_VARIANT (type
)))
8793 value
= array_to_pointer_conversion (input_location
, value
);
8795 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8796 && require_constant_value
&& pending
)
8798 /* As an extension, allow initializing objects with static storage
8799 duration with compound literals (which are then treated just as
8800 the brace enclosed list they contain). */
8802 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
8804 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8805 value
= DECL_INITIAL (decl
);
8808 npc
= null_pointer_constant_p (value
);
8809 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8811 semantic_type
= TREE_TYPE (value
);
8812 value
= TREE_OPERAND (value
, 0);
8814 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8816 if (value
== error_mark_node
)
8817 constructor_erroneous
= 1;
8818 else if (!TREE_CONSTANT (value
))
8819 constructor_constant
= 0;
8820 else if (!initializer_constant_valid_p (value
,
8822 AGGREGATE_TYPE_P (constructor_type
)
8823 && TYPE_REVERSE_STORAGE_ORDER
8825 || (RECORD_OR_UNION_TYPE_P (constructor_type
)
8826 && DECL_C_BIT_FIELD (field
)
8827 && TREE_CODE (value
) != INTEGER_CST
))
8828 constructor_simple
= 0;
8830 constructor_nonconst
= 1;
8832 /* Digest the initializer and issue any errors about incompatible
8833 types before issuing errors about non-constant initializers. */
8834 tree new_value
= value
;
8836 new_value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8837 new_value
= digest_init (loc
, type
, new_value
, origtype
, npc
, strict_string
,
8838 require_constant_value
);
8839 if (new_value
== error_mark_node
)
8841 constructor_erroneous
= 1;
8844 if (require_constant_value
|| require_constant_elements
)
8845 constant_expression_warning (new_value
);
8847 /* Proceed to check the constness of the original initializer. */
8848 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8850 if (require_constant_value
)
8852 error_init (loc
, "initializer element is not constant");
8853 value
= error_mark_node
;
8855 else if (require_constant_elements
)
8856 pedwarn (loc
, OPT_Wpedantic
,
8857 "initializer element is not computable at load time");
8859 else if (!maybe_const
8860 && (require_constant_value
|| require_constant_elements
))
8861 pedwarn_init (loc
, OPT_Wpedantic
,
8862 "initializer element is not a constant expression");
8864 /* Issue -Wc++-compat warnings about initializing a bitfield with
8867 && field
!= NULL_TREE
8868 && TREE_CODE (field
) == FIELD_DECL
8869 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8870 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8871 != TYPE_MAIN_VARIANT (type
))
8872 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8874 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8875 if (checktype
!= error_mark_node
8876 && (TYPE_MAIN_VARIANT (checktype
)
8877 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8878 warning_init (loc
, OPT_Wc___compat
,
8879 "enum conversion in initialization is invalid in C++");
8882 /* If this field is empty (and not at the end of structure),
8883 don't do anything other than checking the initializer. */
8885 && (TREE_TYPE (field
) == error_mark_node
8886 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8887 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8888 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8889 || DECL_CHAIN (field
)))))
8892 /* Finally, set VALUE to the initializer value digested above. */
8895 /* If this element doesn't come next in sequence,
8896 put it on constructor_pending_elts. */
8897 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8898 && (!constructor_incremental
8899 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8901 if (constructor_incremental
8902 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8903 set_nonincremental_init (braced_init_obstack
);
8905 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8906 braced_init_obstack
);
8909 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8910 && (!constructor_incremental
8911 || field
!= constructor_unfilled_fields
))
8913 /* We do this for records but not for unions. In a union,
8914 no matter which field is specified, it can be initialized
8915 right away since it starts at the beginning of the union. */
8916 if (constructor_incremental
)
8918 if (!constructor_unfilled_fields
)
8919 set_nonincremental_init (braced_init_obstack
);
8922 tree bitpos
, unfillpos
;
8924 bitpos
= bit_position (field
);
8925 unfillpos
= bit_position (constructor_unfilled_fields
);
8927 if (tree_int_cst_lt (bitpos
, unfillpos
))
8928 set_nonincremental_init (braced_init_obstack
);
8932 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8933 braced_init_obstack
);
8936 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8937 && !vec_safe_is_empty (constructor_elements
))
8941 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8942 warning_init (loc
, OPT_Woverride_init_side_effects
,
8943 "initialized field with side-effects overwritten");
8944 else if (warn_override_init
)
8945 warning_init (loc
, OPT_Woverride_init
,
8946 "initialized field overwritten");
8949 /* We can have just one union field set. */
8950 constructor_elements
= NULL
;
8953 /* Otherwise, output this element either to
8954 constructor_elements or to the assembler file. */
8956 constructor_elt celt
= {field
, value
};
8957 vec_safe_push (constructor_elements
, celt
);
8959 /* Advance the variable that indicates sequential elements output. */
8960 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8961 constructor_unfilled_index
8962 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8964 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8966 constructor_unfilled_fields
8967 = DECL_CHAIN (constructor_unfilled_fields
);
8969 /* Skip any nameless bit fields. */
8970 while (constructor_unfilled_fields
!= 0
8971 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8972 && DECL_NAME (constructor_unfilled_fields
) == 0)
8973 constructor_unfilled_fields
=
8974 DECL_CHAIN (constructor_unfilled_fields
);
8976 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8977 constructor_unfilled_fields
= 0;
8979 /* Now output any pending elements which have become next. */
8981 output_pending_init_elements (0, braced_init_obstack
);
8984 /* Output any pending elements which have become next.
8985 As we output elements, constructor_unfilled_{fields,index}
8986 advances, which may cause other elements to become next;
8987 if so, they too are output.
8989 If ALL is 0, we return when there are
8990 no more pending elements to output now.
8992 If ALL is 1, we output space as necessary so that
8993 we can output all the pending elements. */
8995 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8997 struct init_node
*elt
= constructor_pending_elts
;
9002 /* Look through the whole pending tree.
9003 If we find an element that should be output now,
9004 output it. Otherwise, set NEXT to the element
9005 that comes first among those still pending. */
9010 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9012 if (tree_int_cst_equal (elt
->purpose
,
9013 constructor_unfilled_index
))
9014 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9015 true, TREE_TYPE (constructor_type
),
9016 constructor_unfilled_index
, 0, false,
9017 braced_init_obstack
);
9018 else if (tree_int_cst_lt (constructor_unfilled_index
,
9021 /* Advance to the next smaller node. */
9026 /* We have reached the smallest node bigger than the
9027 current unfilled index. Fill the space first. */
9028 next
= elt
->purpose
;
9034 /* Advance to the next bigger node. */
9039 /* We have reached the biggest node in a subtree. Find
9040 the parent of it, which is the next bigger node. */
9041 while (elt
->parent
&& elt
->parent
->right
== elt
)
9044 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
9047 next
= elt
->purpose
;
9053 else if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9055 tree ctor_unfilled_bitpos
, elt_bitpos
;
9057 /* If the current record is complete we are done. */
9058 if (constructor_unfilled_fields
== 0)
9061 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
9062 elt_bitpos
= bit_position (elt
->purpose
);
9063 /* We can't compare fields here because there might be empty
9064 fields in between. */
9065 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
9067 constructor_unfilled_fields
= elt
->purpose
;
9068 output_init_element (input_location
, elt
->value
, elt
->origtype
,
9069 true, TREE_TYPE (elt
->purpose
),
9070 elt
->purpose
, 0, false,
9071 braced_init_obstack
);
9073 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
9075 /* Advance to the next smaller node. */
9080 /* We have reached the smallest node bigger than the
9081 current unfilled field. Fill the space first. */
9082 next
= elt
->purpose
;
9088 /* Advance to the next bigger node. */
9093 /* We have reached the biggest node in a subtree. Find
9094 the parent of it, which is the next bigger node. */
9095 while (elt
->parent
&& elt
->parent
->right
== elt
)
9099 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
9100 bit_position (elt
->purpose
))))
9102 next
= elt
->purpose
;
9110 /* Ordinarily return, but not if we want to output all
9111 and there are elements left. */
9112 if (!(all
&& next
!= 0))
9115 /* If it's not incremental, just skip over the gap, so that after
9116 jumping to retry we will output the next successive element. */
9117 if (RECORD_OR_UNION_TYPE_P (constructor_type
))
9118 constructor_unfilled_fields
= next
;
9119 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9120 constructor_unfilled_index
= next
;
9122 /* ELT now points to the node in the pending tree with the next
9123 initializer to output. */
9127 /* Add one non-braced element to the current constructor level.
9128 This adjusts the current position within the constructor's type.
9129 This may also start or terminate implicit levels
9130 to handle a partly-braced initializer.
9132 Once this has found the correct level for the new element,
9133 it calls output_init_element.
9135 IMPLICIT is true if value comes from pop_init_level (1),
9136 the new initializer has been merged with the existing one
9137 and thus no warnings should be emitted about overriding an
9138 existing initializer. */
9141 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
9142 struct obstack
* braced_init_obstack
)
9144 tree orig_value
= value
.value
;
9145 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
9146 bool strict_string
= value
.original_code
== STRING_CST
;
9147 bool was_designated
= designator_depth
!= 0;
9149 designator_depth
= 0;
9150 designator_erroneous
= 0;
9152 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
9153 constructor_zeroinit
= 0;
9155 /* Handle superfluous braces around string cst as in
9156 char x[] = {"foo"}; */
9160 && TREE_CODE (constructor_type
) == ARRAY_TYPE
9161 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
9162 && integer_zerop (constructor_unfilled_index
))
9164 if (constructor_stack
->replacement_value
.value
)
9165 error_init (loc
, "excess elements in char array initializer");
9166 constructor_stack
->replacement_value
= value
;
9170 if (constructor_stack
->replacement_value
.value
!= 0)
9172 error_init (loc
, "excess elements in struct initializer");
9176 /* Ignore elements of a brace group if it is entirely superfluous
9177 and has already been diagnosed. */
9178 if (constructor_type
== 0)
9181 if (!implicit
&& warn_designated_init
&& !was_designated
9182 && TREE_CODE (constructor_type
) == RECORD_TYPE
9183 && lookup_attribute ("designated_init",
9184 TYPE_ATTRIBUTES (constructor_type
)))
9186 OPT_Wdesignated_init
,
9187 "positional initialization of field "
9188 "in %<struct%> declared with %<designated_init%> attribute");
9190 /* If we've exhausted any levels that didn't have braces,
9192 while (constructor_stack
->implicit
)
9194 if (RECORD_OR_UNION_TYPE_P (constructor_type
)
9195 && constructor_fields
== 0)
9196 process_init_element (loc
,
9197 pop_init_level (loc
, 1, braced_init_obstack
),
9198 true, braced_init_obstack
);
9199 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
9200 || VECTOR_TYPE_P (constructor_type
))
9201 && constructor_max_index
9202 && tree_int_cst_lt (constructor_max_index
,
9204 process_init_element (loc
,
9205 pop_init_level (loc
, 1, braced_init_obstack
),
9206 true, braced_init_obstack
);
9211 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9212 if (constructor_range_stack
)
9214 /* If value is a compound literal and we'll be just using its
9215 content, don't put it into a SAVE_EXPR. */
9216 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
9217 || !require_constant_value
)
9219 tree semantic_type
= NULL_TREE
;
9220 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
9222 semantic_type
= TREE_TYPE (value
.value
);
9223 value
.value
= TREE_OPERAND (value
.value
, 0);
9225 value
.value
= c_save_expr (value
.value
);
9227 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
9234 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
9237 enum tree_code fieldcode
;
9239 if (constructor_fields
== 0)
9241 pedwarn_init (loc
, 0, "excess elements in struct initializer");
9245 fieldtype
= TREE_TYPE (constructor_fields
);
9246 if (fieldtype
!= error_mark_node
)
9247 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9248 fieldcode
= TREE_CODE (fieldtype
);
9250 /* Error for non-static initialization of a flexible array member. */
9251 if (fieldcode
== ARRAY_TYPE
9252 && !require_constant_value
9253 && TYPE_SIZE (fieldtype
) == NULL_TREE
9254 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
9256 error_init (loc
, "non-static initialization of a flexible "
9261 /* Error for initialization of a flexible array member with
9262 a string constant if the structure is in an array. E.g.:
9263 struct S { int x; char y[]; };
9264 struct S s[] = { { 1, "foo" } };
9267 && fieldcode
== ARRAY_TYPE
9268 && constructor_depth
> 1
9269 && TYPE_SIZE (fieldtype
) == NULL_TREE
9270 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
9272 bool in_array_p
= false;
9273 for (struct constructor_stack
*p
= constructor_stack
;
9274 p
&& p
->type
; p
= p
->next
)
9275 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
9282 error_init (loc
, "initialization of flexible array "
9283 "member in a nested context");
9288 /* Accept a string constant to initialize a subarray. */
9289 if (value
.value
!= 0
9290 && fieldcode
== ARRAY_TYPE
9291 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
9293 value
.value
= orig_value
;
9294 /* Otherwise, if we have come to a subaggregate,
9295 and we don't have an element of its type, push into it. */
9296 else if (value
.value
!= 0
9297 && value
.value
!= error_mark_node
9298 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
9299 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
9300 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
9302 push_init_level (loc
, 1, braced_init_obstack
);
9308 push_member_name (constructor_fields
);
9309 output_init_element (loc
, value
.value
, value
.original_type
,
9310 strict_string
, fieldtype
,
9311 constructor_fields
, 1, implicit
,
9312 braced_init_obstack
);
9313 RESTORE_SPELLING_DEPTH (constructor_depth
);
9316 /* Do the bookkeeping for an element that was
9317 directly output as a constructor. */
9319 /* For a record, keep track of end position of last field. */
9320 if (DECL_SIZE (constructor_fields
))
9321 constructor_bit_index
9322 = size_binop_loc (input_location
, PLUS_EXPR
,
9323 bit_position (constructor_fields
),
9324 DECL_SIZE (constructor_fields
));
9326 /* If the current field was the first one not yet written out,
9327 it isn't now, so update. */
9328 if (constructor_unfilled_fields
== constructor_fields
)
9330 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9331 /* Skip any nameless bit fields. */
9332 while (constructor_unfilled_fields
!= 0
9333 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
9334 && DECL_NAME (constructor_unfilled_fields
) == 0)
9335 constructor_unfilled_fields
=
9336 DECL_CHAIN (constructor_unfilled_fields
);
9340 constructor_fields
= DECL_CHAIN (constructor_fields
);
9341 /* Skip any nameless bit fields at the beginning. */
9342 while (constructor_fields
!= 0
9343 && DECL_C_BIT_FIELD (constructor_fields
)
9344 && DECL_NAME (constructor_fields
) == 0)
9345 constructor_fields
= DECL_CHAIN (constructor_fields
);
9347 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
9350 enum tree_code fieldcode
;
9352 if (constructor_fields
== 0)
9354 pedwarn_init (loc
, 0,
9355 "excess elements in union initializer");
9359 fieldtype
= TREE_TYPE (constructor_fields
);
9360 if (fieldtype
!= error_mark_node
)
9361 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
9362 fieldcode
= TREE_CODE (fieldtype
);
9364 /* Warn that traditional C rejects initialization of unions.
9365 We skip the warning if the value is zero. This is done
9366 under the assumption that the zero initializer in user
9367 code appears conditioned on e.g. __STDC__ to avoid
9368 "missing initializer" warnings and relies on default
9369 initialization to zero in the traditional C case.
9370 We also skip the warning if the initializer is designated,
9371 again on the assumption that this must be conditional on
9372 __STDC__ anyway (and we've already complained about the
9373 member-designator already). */
9374 if (!in_system_header_at (input_location
) && !constructor_designated
9375 && !(value
.value
&& (integer_zerop (value
.value
)
9376 || real_zerop (value
.value
))))
9377 warning (OPT_Wtraditional
, "traditional C rejects initialization "
9380 /* Accept a string constant to initialize a subarray. */
9381 if (value
.value
!= 0
9382 && fieldcode
== ARRAY_TYPE
9383 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
9385 value
.value
= orig_value
;
9386 /* Otherwise, if we have come to a subaggregate,
9387 and we don't have an element of its type, push into it. */
9388 else if (value
.value
!= 0
9389 && value
.value
!= error_mark_node
9390 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
9391 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
9392 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
9394 push_init_level (loc
, 1, braced_init_obstack
);
9400 push_member_name (constructor_fields
);
9401 output_init_element (loc
, value
.value
, value
.original_type
,
9402 strict_string
, fieldtype
,
9403 constructor_fields
, 1, implicit
,
9404 braced_init_obstack
);
9405 RESTORE_SPELLING_DEPTH (constructor_depth
);
9408 /* Do the bookkeeping for an element that was
9409 directly output as a constructor. */
9411 constructor_bit_index
= DECL_SIZE (constructor_fields
);
9412 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9415 constructor_fields
= 0;
9417 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9419 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9420 enum tree_code eltcode
= TREE_CODE (elttype
);
9422 /* Accept a string constant to initialize a subarray. */
9423 if (value
.value
!= 0
9424 && eltcode
== ARRAY_TYPE
9425 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
9427 value
.value
= orig_value
;
9428 /* Otherwise, if we have come to a subaggregate,
9429 and we don't have an element of its type, push into it. */
9430 else if (value
.value
!= 0
9431 && value
.value
!= error_mark_node
9432 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
9433 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
9434 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
9436 push_init_level (loc
, 1, braced_init_obstack
);
9440 if (constructor_max_index
!= 0
9441 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
9442 || integer_all_onesp (constructor_max_index
)))
9444 pedwarn_init (loc
, 0,
9445 "excess elements in array initializer");
9449 /* Now output the actual element. */
9452 push_array_bounds (tree_to_uhwi (constructor_index
));
9453 output_init_element (loc
, value
.value
, value
.original_type
,
9454 strict_string
, elttype
,
9455 constructor_index
, 1, implicit
,
9456 braced_init_obstack
);
9457 RESTORE_SPELLING_DEPTH (constructor_depth
);
9461 = size_binop_loc (input_location
, PLUS_EXPR
,
9462 constructor_index
, bitsize_one_node
);
9465 /* If we are doing the bookkeeping for an element that was
9466 directly output as a constructor, we must update
9467 constructor_unfilled_index. */
9468 constructor_unfilled_index
= constructor_index
;
9470 else if (VECTOR_TYPE_P (constructor_type
))
9472 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9474 /* Do a basic check of initializer size. Note that vectors
9475 always have a fixed size derived from their type. */
9476 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
9478 pedwarn_init (loc
, 0,
9479 "excess elements in vector initializer");
9483 /* Now output the actual element. */
9486 if (TREE_CODE (value
.value
) == VECTOR_CST
)
9487 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
9488 output_init_element (loc
, value
.value
, value
.original_type
,
9489 strict_string
, elttype
,
9490 constructor_index
, 1, implicit
,
9491 braced_init_obstack
);
9495 = size_binop_loc (input_location
,
9496 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
9499 /* If we are doing the bookkeeping for an element that was
9500 directly output as a constructor, we must update
9501 constructor_unfilled_index. */
9502 constructor_unfilled_index
= constructor_index
;
9505 /* Handle the sole element allowed in a braced initializer
9506 for a scalar variable. */
9507 else if (constructor_type
!= error_mark_node
9508 && constructor_fields
== 0)
9510 pedwarn_init (loc
, 0,
9511 "excess elements in scalar initializer");
9517 output_init_element (loc
, value
.value
, value
.original_type
,
9518 strict_string
, constructor_type
,
9519 NULL_TREE
, 1, implicit
,
9520 braced_init_obstack
);
9521 constructor_fields
= 0;
9524 /* Handle range initializers either at this level or anywhere higher
9525 in the designator stack. */
9526 if (constructor_range_stack
)
9528 struct constructor_range_stack
*p
, *range_stack
;
9531 range_stack
= constructor_range_stack
;
9532 constructor_range_stack
= 0;
9533 while (constructor_stack
!= range_stack
->stack
)
9535 gcc_assert (constructor_stack
->implicit
);
9536 process_init_element (loc
,
9537 pop_init_level (loc
, 1,
9538 braced_init_obstack
),
9539 true, braced_init_obstack
);
9541 for (p
= range_stack
;
9542 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
9545 gcc_assert (constructor_stack
->implicit
);
9546 process_init_element (loc
,
9547 pop_init_level (loc
, 1,
9548 braced_init_obstack
),
9549 true, braced_init_obstack
);
9552 p
->index
= size_binop_loc (input_location
,
9553 PLUS_EXPR
, p
->index
, bitsize_one_node
);
9554 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
9559 constructor_index
= p
->index
;
9560 constructor_fields
= p
->fields
;
9561 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
9569 finish_implicit_inits (loc
, braced_init_obstack
);
9570 push_init_level (loc
, 2, braced_init_obstack
);
9571 p
->stack
= constructor_stack
;
9572 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
9573 p
->index
= p
->range_start
;
9577 constructor_range_stack
= range_stack
;
9584 constructor_range_stack
= 0;
9587 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9588 (guaranteed to be 'volatile' or null) and ARGS (represented using
9589 an ASM_EXPR node). */
9591 build_asm_stmt (tree cv_qualifier
, tree args
)
9593 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
9594 ASM_VOLATILE_P (args
) = 1;
9595 return add_stmt (args
);
9598 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9599 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9600 SIMPLE indicates whether there was anything at all after the
9601 string in the asm expression -- asm("blah") and asm("blah" : )
9602 are subtly different. We use a ASM_EXPR node to represent this. */
9604 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
9605 tree clobbers
, tree labels
, bool simple
)
9610 const char *constraint
;
9611 const char **oconstraints
;
9612 bool allows_mem
, allows_reg
, is_inout
;
9613 int ninputs
, noutputs
;
9615 ninputs
= list_length (inputs
);
9616 noutputs
= list_length (outputs
);
9617 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
9619 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
9621 /* Remove output conversions that change the type but not the mode. */
9622 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9624 tree output
= TREE_VALUE (tail
);
9626 output
= c_fully_fold (output
, false, NULL
);
9628 /* ??? Really, this should not be here. Users should be using a
9629 proper lvalue, dammit. But there's a long history of using casts
9630 in the output operands. In cases like longlong.h, this becomes a
9631 primitive form of typechecking -- if the cast can be removed, then
9632 the output operand had a type of the proper width; otherwise we'll
9633 get an error. Gross, but ... */
9634 STRIP_NOPS (output
);
9636 if (!lvalue_or_else (loc
, output
, lv_asm
))
9637 output
= error_mark_node
;
9639 if (output
!= error_mark_node
9640 && (TREE_READONLY (output
)
9641 || TYPE_READONLY (TREE_TYPE (output
))
9642 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output
))
9643 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
9644 readonly_error (loc
, output
, lv_asm
);
9646 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9647 oconstraints
[i
] = constraint
;
9649 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
9650 &allows_mem
, &allows_reg
, &is_inout
))
9652 /* If the operand is going to end up in memory,
9653 mark it addressable. */
9654 if (!allows_reg
&& !c_mark_addressable (output
))
9655 output
= error_mark_node
;
9656 if (!(!allows_reg
&& allows_mem
)
9657 && output
!= error_mark_node
9658 && VOID_TYPE_P (TREE_TYPE (output
)))
9660 error_at (loc
, "invalid use of void expression");
9661 output
= error_mark_node
;
9665 output
= error_mark_node
;
9667 TREE_VALUE (tail
) = output
;
9670 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9674 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9675 input
= TREE_VALUE (tail
);
9677 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
9678 oconstraints
, &allows_mem
, &allows_reg
))
9680 /* If the operand is going to end up in memory,
9681 mark it addressable. */
9682 if (!allows_reg
&& allows_mem
)
9684 input
= c_fully_fold (input
, false, NULL
);
9686 /* Strip the nops as we allow this case. FIXME, this really
9687 should be rejected or made deprecated. */
9689 if (!c_mark_addressable (input
))
9690 input
= error_mark_node
;
9695 memset (&expr
, 0, sizeof (expr
));
9697 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9698 input
= c_fully_fold (expr
.value
, false, NULL
);
9700 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9702 error_at (loc
, "invalid use of void expression");
9703 input
= error_mark_node
;
9708 input
= error_mark_node
;
9710 TREE_VALUE (tail
) = input
;
9713 /* ASMs with labels cannot have outputs. This should have been
9714 enforced by the parser. */
9715 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9717 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9719 /* asm statements without outputs, including simple ones, are treated
9721 ASM_INPUT_P (args
) = simple
;
9722 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9727 /* Generate a goto statement to LABEL. LOC is the location of the
9731 c_finish_goto_label (location_t loc
, tree label
)
9733 tree decl
= lookup_label_for_goto (loc
, label
);
9736 TREE_USED (decl
) = 1;
9738 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9739 SET_EXPR_LOCATION (t
, loc
);
9740 return add_stmt (t
);
9744 /* Generate a computed goto statement to EXPR. LOC is the location of
9748 c_finish_goto_ptr (location_t loc
, tree expr
)
9751 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9752 expr
= c_fully_fold (expr
, false, NULL
);
9753 expr
= convert (ptr_type_node
, expr
);
9754 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9755 SET_EXPR_LOCATION (t
, loc
);
9756 return add_stmt (t
);
9759 /* Generate a C `return' statement. RETVAL is the expression for what
9760 to return, or a null pointer for `return;' with no value. LOC is
9761 the location of the return statement, or the location of the expression,
9762 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9763 is the original type of RETVAL. */
9766 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9768 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9769 bool no_warning
= false;
9773 /* Use the expansion point to handle cases such as returning NULL
9774 in a function returning void. */
9775 source_location xloc
= expansion_point_location_if_in_system_header (loc
);
9777 if (TREE_THIS_VOLATILE (current_function_decl
))
9778 warning_at (xloc
, 0,
9779 "function declared %<noreturn%> has a %<return%> statement");
9781 if (flag_cilkplus
&& contains_array_notation_expr (retval
))
9783 /* Array notations are allowed in a return statement if it is inside a
9784 built-in array notation reduction function. */
9785 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9786 return error_mark_node
;
9789 error_at (loc
, "array notation expression cannot be used as a "
9791 return error_mark_node
;
9794 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
9796 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9798 return error_mark_node
;
9802 tree semantic_type
= NULL_TREE
;
9803 npc
= null_pointer_constant_p (retval
);
9804 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9806 semantic_type
= TREE_TYPE (retval
);
9807 retval
= TREE_OPERAND (retval
, 0);
9809 retval
= c_fully_fold (retval
, false, NULL
);
9811 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9816 current_function_returns_null
= 1;
9817 if ((warn_return_type
|| flag_isoc99
)
9818 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9822 warned_here
= pedwarn
9824 "%<return%> with no value, in function returning non-void");
9826 warned_here
= warning_at
9827 (loc
, OPT_Wreturn_type
,
9828 "%<return%> with no value, in function returning non-void");
9831 inform (DECL_SOURCE_LOCATION (current_function_decl
),
9835 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9837 current_function_returns_null
= 1;
9839 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9840 warned_here
= pedwarn
9842 "%<return%> with a value, in function returning void");
9844 warned_here
= pedwarn
9845 (xloc
, OPT_Wpedantic
, "ISO C forbids "
9846 "%<return%> with expression, in function returning void");
9848 inform (DECL_SOURCE_LOCATION (current_function_decl
),
9853 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
9854 retval
, origtype
, ic_return
,
9855 npc
, NULL_TREE
, NULL_TREE
, 0);
9856 tree res
= DECL_RESULT (current_function_decl
);
9860 current_function_returns_value
= 1;
9861 if (t
== error_mark_node
)
9864 save
= in_late_binary_op
;
9865 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9866 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
9867 || (TREE_CODE (TREE_TYPE (t
)) == REAL_TYPE
9868 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
9869 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
9870 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
9871 in_late_binary_op
= true;
9872 inner
= t
= convert (TREE_TYPE (res
), t
);
9873 in_late_binary_op
= save
;
9875 /* Strip any conversions, additions, and subtractions, and see if
9876 we are returning the address of a local variable. Warn if so. */
9879 switch (TREE_CODE (inner
))
9882 case NON_LVALUE_EXPR
:
9884 case POINTER_PLUS_EXPR
:
9885 inner
= TREE_OPERAND (inner
, 0);
9889 /* If the second operand of the MINUS_EXPR has a pointer
9890 type (or is converted from it), this may be valid, so
9891 don't give a warning. */
9893 tree op1
= TREE_OPERAND (inner
, 1);
9895 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9896 && (CONVERT_EXPR_P (op1
)
9897 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9898 op1
= TREE_OPERAND (op1
, 0);
9900 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9903 inner
= TREE_OPERAND (inner
, 0);
9908 inner
= TREE_OPERAND (inner
, 0);
9910 while (REFERENCE_CLASS_P (inner
)
9911 && !INDIRECT_REF_P (inner
))
9912 inner
= TREE_OPERAND (inner
, 0);
9915 && !DECL_EXTERNAL (inner
)
9916 && !TREE_STATIC (inner
)
9917 && DECL_CONTEXT (inner
) == current_function_decl
)
9919 if (TREE_CODE (inner
) == LABEL_DECL
)
9920 warning_at (loc
, OPT_Wreturn_local_addr
,
9921 "function returns address of label");
9924 warning_at (loc
, OPT_Wreturn_local_addr
,
9925 "function returns address of local variable");
9926 tree zero
= build_zero_cst (TREE_TYPE (res
));
9927 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
9939 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9940 SET_EXPR_LOCATION (retval
, loc
);
9942 if (warn_sequence_point
)
9943 verify_sequence_points (retval
);
9946 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9947 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9948 return add_stmt (ret_stmt
);
9952 /* The SWITCH_EXPR being built. */
9955 /* The original type of the testing expression, i.e. before the
9956 default conversion is applied. */
9959 /* A splay-tree mapping the low element of a case range to the high
9960 element, or NULL_TREE if there is no high element. Used to
9961 determine whether or not a new case label duplicates an old case
9962 label. We need a tree, rather than simply a hash table, because
9963 of the GNU case range extension. */
9966 /* The bindings at the point of the switch. This is used for
9967 warnings crossing decls when branching to a case label. */
9968 struct c_spot_bindings
*bindings
;
9970 /* The next node on the stack. */
9971 struct c_switch
*next
;
9973 /* Remember whether the controlling expression had boolean type
9974 before integer promotions for the sake of -Wswitch-bool. */
9977 /* Remember whether there was a case value that is outside the
9978 range of the ORIG_TYPE. */
9979 bool outside_range_p
;
9982 /* A stack of the currently active switch statements. The innermost
9983 switch statement is on the top of the stack. There is no need to
9984 mark the stack for garbage collection because it is only active
9985 during the processing of the body of a function, and we never
9986 collect at that point. */
9988 struct c_switch
*c_switch_stack
;
9990 /* Start a C switch statement, testing expression EXP. Return the new
9991 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9992 SWITCH_COND_LOC is the location of the switch's condition.
9993 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
9996 c_start_case (location_t switch_loc
,
9997 location_t switch_cond_loc
,
9998 tree exp
, bool explicit_cast_p
)
10000 tree orig_type
= error_mark_node
;
10001 bool bool_cond_p
= false;
10002 struct c_switch
*cs
;
10004 if (exp
!= error_mark_node
)
10006 orig_type
= TREE_TYPE (exp
);
10008 if (!INTEGRAL_TYPE_P (orig_type
))
10010 if (orig_type
!= error_mark_node
)
10012 error_at (switch_cond_loc
, "switch quantity not an integer");
10013 orig_type
= error_mark_node
;
10015 exp
= integer_zero_node
;
10019 tree type
= TYPE_MAIN_VARIANT (orig_type
);
10022 /* Warn if the condition has boolean value. */
10023 while (TREE_CODE (e
) == COMPOUND_EXPR
)
10024 e
= TREE_OPERAND (e
, 1);
10026 if ((TREE_CODE (type
) == BOOLEAN_TYPE
10027 || truth_value_p (TREE_CODE (e
)))
10028 /* Explicit cast to int suppresses this warning. */
10029 && !(TREE_CODE (type
) == INTEGER_TYPE
10030 && explicit_cast_p
))
10031 bool_cond_p
= true;
10033 if (!in_system_header_at (input_location
)
10034 && (type
== long_integer_type_node
10035 || type
== long_unsigned_type_node
))
10036 warning_at (switch_cond_loc
,
10037 OPT_Wtraditional
, "%<long%> switch expression not "
10038 "converted to %<int%> in ISO C");
10040 exp
= c_fully_fold (exp
, false, NULL
);
10041 exp
= default_conversion (exp
);
10043 if (warn_sequence_point
)
10044 verify_sequence_points (exp
);
10048 /* Add this new SWITCH_EXPR to the stack. */
10049 cs
= XNEW (struct c_switch
);
10050 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
10051 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
10052 cs
->orig_type
= orig_type
;
10053 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
10054 cs
->bindings
= c_get_switch_bindings ();
10055 cs
->bool_cond_p
= bool_cond_p
;
10056 cs
->outside_range_p
= false;
10057 cs
->next
= c_switch_stack
;
10058 c_switch_stack
= cs
;
10060 return add_stmt (cs
->switch_expr
);
10063 /* Process a case label at location LOC. */
10066 do_case (location_t loc
, tree low_value
, tree high_value
)
10068 tree label
= NULL_TREE
;
10070 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
10072 low_value
= c_fully_fold (low_value
, false, NULL
);
10073 if (TREE_CODE (low_value
) == INTEGER_CST
)
10074 pedwarn (loc
, OPT_Wpedantic
,
10075 "case label is not an integer constant expression");
10078 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
10080 high_value
= c_fully_fold (high_value
, false, NULL
);
10081 if (TREE_CODE (high_value
) == INTEGER_CST
)
10082 pedwarn (input_location
, OPT_Wpedantic
,
10083 "case label is not an integer constant expression");
10086 if (c_switch_stack
== NULL
)
10089 error_at (loc
, "case label not within a switch statement");
10091 error_at (loc
, "%<default%> label not within a switch statement");
10095 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
10096 EXPR_LOCATION (c_switch_stack
->switch_expr
),
10100 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
10101 SWITCH_COND (c_switch_stack
->switch_expr
),
10102 c_switch_stack
->orig_type
,
10103 low_value
, high_value
,
10104 &c_switch_stack
->outside_range_p
);
10105 if (label
== error_mark_node
)
10110 /* Finish the switch statement. TYPE is the original type of the
10111 controlling expression of the switch, or NULL_TREE. */
10114 c_finish_case (tree body
, tree type
)
10116 struct c_switch
*cs
= c_switch_stack
;
10117 location_t switch_location
;
10119 SWITCH_BODY (cs
->switch_expr
) = body
;
10121 /* Emit warnings as needed. */
10122 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
10123 c_do_switch_warnings (cs
->cases
, switch_location
,
10124 type
? type
: TREE_TYPE (cs
->switch_expr
),
10125 SWITCH_COND (cs
->switch_expr
),
10126 cs
->bool_cond_p
, cs
->outside_range_p
);
10128 /* Pop the stack. */
10129 c_switch_stack
= cs
->next
;
10130 splay_tree_delete (cs
->cases
);
10131 c_release_switch_bindings (cs
->bindings
);
10135 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10136 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10140 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
10145 /* If the condition has array notations, then the rank of the then_block and
10146 else_block must be either 0 or be equal to the rank of the condition. If
10147 the condition does not have array notations then break them up as it is
10148 broken up in a normal expression. */
10149 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
10151 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
10152 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
10155 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
10158 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
10160 if (cond_rank
!= then_rank
&& then_rank
!= 0)
10162 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
10163 " and the then-block");
10166 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
10168 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
10169 " and the else-block");
10174 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
10175 SET_EXPR_LOCATION (stmt
, if_locus
);
10179 /* Emit a general-purpose loop construct. START_LOCUS is the location of
10180 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10181 is false for DO loops. INCR is the FOR increment expression. BODY is
10182 the statement controlled by the loop. BLAB is the break label. CLAB is
10183 the continue label. Everything is allowed to be NULL. */
10186 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
10187 tree blab
, tree clab
, bool cond_is_first
)
10189 tree entry
= NULL
, exit
= NULL
, t
;
10191 /* In theory could forbid cilk spawn for loop increment expression,
10192 but it should work just fine. */
10194 /* If the condition is zero don't generate a loop construct. */
10195 if (cond
&& integer_zerop (cond
))
10199 t
= build_and_jump (&blab
);
10200 SET_EXPR_LOCATION (t
, start_locus
);
10206 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10208 /* If we have an exit condition, then we build an IF with gotos either
10209 out of the loop, or to the top of it. If there's no exit condition,
10210 then we just build a jump back to the top. */
10211 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
10213 if (cond
&& !integer_nonzerop (cond
))
10215 /* Canonicalize the loop condition to the end. This means
10216 generating a branch to the loop condition. Reuse the
10217 continue label, if possible. */
10222 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
10223 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
10226 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
10227 SET_EXPR_LOCATION (t
, start_locus
);
10231 t
= build_and_jump (&blab
);
10233 exit
= fold_build3_loc (start_locus
,
10234 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10236 exit
= fold_build3_loc (input_location
,
10237 COND_EXPR
, void_type_node
, cond
, exit
, t
);
10241 /* For the backward-goto's location of an unconditional loop
10242 use the beginning of the body, or, if there is none, the
10243 top of the loop. */
10244 location_t loc
= EXPR_LOCATION (expr_first (body
));
10245 if (loc
== UNKNOWN_LOCATION
)
10247 SET_EXPR_LOCATION (exit
, loc
);
10256 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
10264 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
10268 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
10271 tree label
= *label_p
;
10273 /* In switch statements break is sometimes stylistically used after
10274 a return statement. This can lead to spurious warnings about
10275 control reaching the end of a non-void function when it is
10276 inlined. Note that we are calling block_may_fallthru with
10277 language specific tree nodes; this works because
10278 block_may_fallthru returns true when given something it does not
10280 skip
= !block_may_fallthru (cur_stmt_list
);
10285 *label_p
= label
= create_artificial_label (loc
);
10287 else if (TREE_CODE (label
) == LABEL_DECL
)
10289 else switch (TREE_INT_CST_LOW (label
))
10293 error_at (loc
, "break statement not within loop or switch");
10295 error_at (loc
, "continue statement not within a loop");
10299 gcc_assert (is_break
);
10300 error_at (loc
, "break statement used with OpenMP for loop");
10305 error ("break statement within %<#pragma simd%> loop body");
10307 error ("continue statement within %<#pragma simd%> loop body");
10311 gcc_unreachable ();
10318 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
10320 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
10323 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
10326 emit_side_effect_warnings (location_t loc
, tree expr
)
10328 if (expr
== error_mark_node
)
10330 else if (!TREE_SIDE_EFFECTS (expr
))
10332 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
10333 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
10335 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
10338 location_t cloc
= loc
;
10339 while (TREE_CODE (r
) == COMPOUND_EXPR
)
10341 if (EXPR_HAS_LOCATION (r
))
10342 cloc
= EXPR_LOCATION (r
);
10343 r
= TREE_OPERAND (r
, 1);
10345 if (!TREE_SIDE_EFFECTS (r
)
10346 && !VOID_TYPE_P (TREE_TYPE (r
))
10347 && !CONVERT_EXPR_P (r
)
10348 && !TREE_NO_WARNING (r
)
10349 && !TREE_NO_WARNING (expr
))
10350 warning_at (cloc
, OPT_Wunused_value
,
10351 "right-hand operand of comma expression has no effect");
10354 warn_if_unused_value (expr
, loc
);
10357 /* Process an expression as if it were a complete statement. Emit
10358 diagnostics, but do not call ADD_STMT. LOC is the location of the
10362 c_process_expr_stmt (location_t loc
, tree expr
)
10369 expr
= c_fully_fold (expr
, false, NULL
);
10371 if (warn_sequence_point
)
10372 verify_sequence_points (expr
);
10374 if (TREE_TYPE (expr
) != error_mark_node
10375 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
10376 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
10377 error_at (loc
, "expression statement has incomplete type");
10379 /* If we're not processing a statement expression, warn about unused values.
10380 Warnings for statement expressions will be emitted later, once we figure
10381 out which is the result. */
10382 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10383 && warn_unused_value
)
10384 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr
, loc
), expr
);
10387 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
10388 exprv
= TREE_OPERAND (exprv
, 1);
10389 while (CONVERT_EXPR_P (exprv
))
10390 exprv
= TREE_OPERAND (exprv
, 0);
10392 || handled_component_p (exprv
)
10393 || TREE_CODE (exprv
) == ADDR_EXPR
)
10394 mark_exp_read (exprv
);
10396 /* If the expression is not of a type to which we cannot assign a line
10397 number, wrap the thing in a no-op NOP_EXPR. */
10398 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
10400 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
10401 SET_EXPR_LOCATION (expr
, loc
);
10407 /* Emit an expression as a statement. LOC is the location of the
10411 c_finish_expr_stmt (location_t loc
, tree expr
)
10414 return add_stmt (c_process_expr_stmt (loc
, expr
));
10419 /* Do the opposite and emit a statement as an expression. To begin,
10420 create a new binding level and return it. */
10423 c_begin_stmt_expr (void)
10427 /* We must force a BLOCK for this level so that, if it is not expanded
10428 later, there is a way to turn off the entire subtree of blocks that
10429 are contained in it. */
10430 keep_next_level ();
10431 ret
= c_begin_compound_stmt (true);
10433 c_bindings_start_stmt_expr (c_switch_stack
== NULL
10435 : c_switch_stack
->bindings
);
10437 /* Mark the current statement list as belonging to a statement list. */
10438 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
10443 /* LOC is the location of the compound statement to which this body
10447 c_finish_stmt_expr (location_t loc
, tree body
)
10449 tree last
, type
, tmp
, val
;
10452 body
= c_end_compound_stmt (loc
, body
, true);
10454 c_bindings_end_stmt_expr (c_switch_stack
== NULL
10456 : c_switch_stack
->bindings
);
10458 /* Locate the last statement in BODY. See c_end_compound_stmt
10459 about always returning a BIND_EXPR. */
10460 last_p
= &BIND_EXPR_BODY (body
);
10461 last
= BIND_EXPR_BODY (body
);
10463 continue_searching
:
10464 if (TREE_CODE (last
) == STATEMENT_LIST
)
10466 tree_stmt_iterator i
;
10468 /* This can happen with degenerate cases like ({ }). No value. */
10469 if (!TREE_SIDE_EFFECTS (last
))
10472 /* If we're supposed to generate side effects warnings, process
10473 all of the statements except the last. */
10474 if (warn_unused_value
)
10476 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
10479 tree t
= tsi_stmt (i
);
10481 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
10482 emit_side_effect_warnings (tloc
, t
);
10486 i
= tsi_last (last
);
10487 last_p
= tsi_stmt_ptr (i
);
10491 /* If the end of the list is exception related, then the list was split
10492 by a call to push_cleanup. Continue searching. */
10493 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
10494 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
10496 last_p
= &TREE_OPERAND (last
, 0);
10498 goto continue_searching
;
10501 if (last
== error_mark_node
)
10504 /* In the case that the BIND_EXPR is not necessary, return the
10505 expression out from inside it. */
10506 if (last
== BIND_EXPR_BODY (body
)
10507 && BIND_EXPR_VARS (body
) == NULL
)
10509 /* Even if this looks constant, do not allow it in a constant
10511 last
= c_wrap_maybe_const (last
, true);
10512 /* Do not warn if the return value of a statement expression is
10514 TREE_NO_WARNING (last
) = 1;
10518 /* Extract the type of said expression. */
10519 type
= TREE_TYPE (last
);
10521 /* If we're not returning a value at all, then the BIND_EXPR that
10522 we already have is a fine expression to return. */
10523 if (!type
|| VOID_TYPE_P (type
))
10526 /* Now that we've located the expression containing the value, it seems
10527 silly to make voidify_wrapper_expr repeat the process. Create a
10528 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10529 tmp
= create_tmp_var_raw (type
);
10531 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10532 tree_expr_nonnegative_p giving up immediately. */
10534 if (TREE_CODE (val
) == NOP_EXPR
10535 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
10536 val
= TREE_OPERAND (val
, 0);
10538 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
10539 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
10542 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
10543 SET_EXPR_LOCATION (t
, loc
);
10548 /* Begin and end compound statements. This is as simple as pushing
10549 and popping new statement lists from the tree. */
10552 c_begin_compound_stmt (bool do_scope
)
10554 tree stmt
= push_stmt_list ();
10560 /* End a compound statement. STMT is the statement. LOC is the
10561 location of the compound statement-- this is usually the location
10562 of the opening brace. */
10565 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
10571 if (c_dialect_objc ())
10572 objc_clear_super_receiver ();
10573 block
= pop_scope ();
10576 stmt
= pop_stmt_list (stmt
);
10577 stmt
= c_build_bind_expr (loc
, block
, stmt
);
10579 /* If this compound statement is nested immediately inside a statement
10580 expression, then force a BIND_EXPR to be created. Otherwise we'll
10581 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10582 STATEMENT_LISTs merge, and thus we can lose track of what statement
10583 was really last. */
10584 if (building_stmt_list_p ()
10585 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10586 && TREE_CODE (stmt
) != BIND_EXPR
)
10588 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
10589 TREE_SIDE_EFFECTS (stmt
) = 1;
10590 SET_EXPR_LOCATION (stmt
, loc
);
10596 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10597 when the current scope is exited. EH_ONLY is true when this is not
10598 meant to apply to normal control flow transfer. */
10601 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
10603 enum tree_code code
;
10607 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
10608 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
10610 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
10611 list
= push_stmt_list ();
10612 TREE_OPERAND (stmt
, 0) = list
;
10613 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
10616 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10617 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10620 build_vec_cmp (tree_code code
, tree type
,
10621 tree arg0
, tree arg1
)
10623 tree zero_vec
= build_zero_cst (type
);
10624 tree minus_one_vec
= build_minus_one_cst (type
);
10625 tree cmp_type
= build_same_sized_truth_vector_type (type
);
10626 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
10627 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
10630 /* Build a binary-operation expression without default conversions.
10631 CODE is the kind of expression to build.
10632 LOCATION is the operator's location.
10633 This function differs from `build' in several ways:
10634 the data type of the result is computed and recorded in it,
10635 warnings are generated if arg data types are invalid,
10636 special handling for addition and subtraction of pointers is known,
10637 and some optimization is done (operations on narrow ints
10638 are done in the narrower type when that gives the same result).
10639 Constant folding is also done before the result is returned.
10641 Note that the operands will never have enumeral types, or function
10642 or array types, because either they will have the default conversions
10643 performed or they have both just been converted to some other type in which
10644 the arithmetic is to be done. */
10647 build_binary_op (location_t location
, enum tree_code code
,
10648 tree orig_op0
, tree orig_op1
, int convert_p
)
10650 tree type0
, type1
, orig_type0
, orig_type1
;
10652 enum tree_code code0
, code1
;
10654 tree ret
= error_mark_node
;
10655 const char *invalid_op_diag
;
10656 bool op0_int_operands
, op1_int_operands
;
10657 bool int_const
, int_const_or_overflow
, int_operands
;
10659 /* Expression code to give to the expression when it is built.
10660 Normally this is CODE, which is what the caller asked for,
10661 but in some special cases we change it. */
10662 enum tree_code resultcode
= code
;
10664 /* Data type in which the computation is to be performed.
10665 In the simplest cases this is the common type of the arguments. */
10666 tree result_type
= NULL
;
10668 /* When the computation is in excess precision, the type of the
10669 final EXCESS_PRECISION_EXPR. */
10670 tree semantic_result_type
= NULL
;
10672 /* Nonzero means operands have already been type-converted
10673 in whatever way is necessary.
10674 Zero means they need to be converted to RESULT_TYPE. */
10677 /* Nonzero means create the expression with this type, rather than
10679 tree build_type
= 0;
10681 /* Nonzero means after finally constructing the expression
10682 convert it to this type. */
10683 tree final_type
= 0;
10685 /* Nonzero if this is an operation like MIN or MAX which can
10686 safely be computed in short if both args are promoted shorts.
10687 Also implies COMMON.
10688 -1 indicates a bitwise operation; this makes a difference
10689 in the exact conditions for when it is safe to do the operation
10690 in a narrower mode. */
10693 /* Nonzero if this is a comparison operation;
10694 if both args are promoted shorts, compare the original shorts.
10695 Also implies COMMON. */
10696 int short_compare
= 0;
10698 /* Nonzero if this is a right-shift operation, which can be computed on the
10699 original short and then promoted if the operand is a promoted short. */
10700 int short_shift
= 0;
10702 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10705 /* True means types are compatible as far as ObjC is concerned. */
10708 /* True means this is an arithmetic operation that may need excess
10710 bool may_need_excess_precision
;
10712 /* True means this is a boolean operation that converts both its
10713 operands to truth-values. */
10714 bool boolean_op
= false;
10716 /* Remember whether we're doing / or %. */
10717 bool doing_div_or_mod
= false;
10719 /* Remember whether we're doing << or >>. */
10720 bool doing_shift
= false;
10722 /* Tree holding instrumentation expression. */
10723 tree instrument_expr
= NULL
;
10725 if (location
== UNKNOWN_LOCATION
)
10726 location
= input_location
;
10731 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
10732 if (op0_int_operands
)
10733 op0
= remove_c_maybe_const_expr (op0
);
10734 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
10735 if (op1_int_operands
)
10736 op1
= remove_c_maybe_const_expr (op1
);
10737 int_operands
= (op0_int_operands
&& op1_int_operands
);
10740 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
10741 && TREE_CODE (orig_op1
) == INTEGER_CST
);
10742 int_const
= (int_const_or_overflow
10743 && !TREE_OVERFLOW (orig_op0
)
10744 && !TREE_OVERFLOW (orig_op1
));
10747 int_const
= int_const_or_overflow
= false;
10749 /* Do not apply default conversion in mixed vector/scalar expression. */
10751 && VECTOR_TYPE_P (TREE_TYPE (op0
)) == VECTOR_TYPE_P (TREE_TYPE (op1
)))
10753 op0
= default_conversion (op0
);
10754 op1
= default_conversion (op1
);
10757 /* When Cilk Plus is enabled and there are array notations inside op0, then
10758 we check to see if there are builtin array notation functions. If
10759 so, then we take on the type of the array notation inside it. */
10760 if (flag_cilkplus
&& contains_array_notation_expr (op0
))
10761 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10763 orig_type0
= type0
= TREE_TYPE (op0
);
10765 if (flag_cilkplus
&& contains_array_notation_expr (op1
))
10766 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10768 orig_type1
= type1
= TREE_TYPE (op1
);
10770 /* The expression codes of the data types of the arguments tell us
10771 whether the arguments are integers, floating, pointers, etc. */
10772 code0
= TREE_CODE (type0
);
10773 code1
= TREE_CODE (type1
);
10775 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10776 STRIP_TYPE_NOPS (op0
);
10777 STRIP_TYPE_NOPS (op1
);
10779 /* If an error was already reported for one of the arguments,
10780 avoid reporting another error. */
10782 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10783 return error_mark_node
;
10785 if (code0
== POINTER_TYPE
10786 && reject_gcc_builtin (op0
, EXPR_LOCATION (orig_op0
)))
10787 return error_mark_node
;
10789 if (code1
== POINTER_TYPE
10790 && reject_gcc_builtin (op1
, EXPR_LOCATION (orig_op1
)))
10791 return error_mark_node
;
10793 if ((invalid_op_diag
10794 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10796 error_at (location
, invalid_op_diag
);
10797 return error_mark_node
;
10805 case TRUNC_DIV_EXPR
:
10806 case CEIL_DIV_EXPR
:
10807 case FLOOR_DIV_EXPR
:
10808 case ROUND_DIV_EXPR
:
10809 case EXACT_DIV_EXPR
:
10810 may_need_excess_precision
= true;
10813 may_need_excess_precision
= false;
10816 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10818 op0
= TREE_OPERAND (op0
, 0);
10819 type0
= TREE_TYPE (op0
);
10821 else if (may_need_excess_precision
10822 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10825 op0
= convert (eptype
, op0
);
10827 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10829 op1
= TREE_OPERAND (op1
, 0);
10830 type1
= TREE_TYPE (op1
);
10832 else if (may_need_excess_precision
10833 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10836 op1
= convert (eptype
, op1
);
10839 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10841 /* In case when one of the operands of the binary operation is
10842 a vector and another is a scalar -- convert scalar to vector. */
10843 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10845 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10848 switch (convert_flag
)
10851 return error_mark_node
;
10854 bool maybe_const
= true;
10856 sc
= c_fully_fold (op0
, false, &maybe_const
);
10857 sc
= save_expr (sc
);
10858 sc
= convert (TREE_TYPE (type1
), sc
);
10859 op0
= build_vector_from_val (type1
, sc
);
10861 op0
= c_wrap_maybe_const (op0
, true);
10862 orig_type0
= type0
= TREE_TYPE (op0
);
10863 code0
= TREE_CODE (type0
);
10867 case stv_secondarg
:
10869 bool maybe_const
= true;
10871 sc
= c_fully_fold (op1
, false, &maybe_const
);
10872 sc
= save_expr (sc
);
10873 sc
= convert (TREE_TYPE (type0
), sc
);
10874 op1
= build_vector_from_val (type0
, sc
);
10876 op1
= c_wrap_maybe_const (op1
, true);
10877 orig_type1
= type1
= TREE_TYPE (op1
);
10878 code1
= TREE_CODE (type1
);
10890 /* Handle the pointer + int case. */
10891 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10893 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10894 goto return_build_binary_op
;
10896 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10898 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10899 goto return_build_binary_op
;
10906 /* Subtraction of two similar pointers.
10907 We must subtract them as integers, then divide by object size. */
10908 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10909 && comp_target_types (location
, type0
, type1
))
10911 ret
= pointer_diff (location
, op0
, op1
);
10912 goto return_build_binary_op
;
10914 /* Handle pointer minus int. Just like pointer plus int. */
10915 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10917 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10918 goto return_build_binary_op
;
10928 case TRUNC_DIV_EXPR
:
10929 case CEIL_DIV_EXPR
:
10930 case FLOOR_DIV_EXPR
:
10931 case ROUND_DIV_EXPR
:
10932 case EXACT_DIV_EXPR
:
10933 doing_div_or_mod
= true;
10934 warn_for_div_by_zero (location
, op1
);
10936 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10937 || code0
== FIXED_POINT_TYPE
10938 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10939 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10940 || code1
== FIXED_POINT_TYPE
10941 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10943 enum tree_code tcode0
= code0
, tcode1
= code1
;
10945 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10946 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10947 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10948 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10950 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10951 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10952 resultcode
= RDIV_EXPR
;
10954 /* Although it would be tempting to shorten always here, that
10955 loses on some targets, since the modulo instruction is
10956 undefined if the quotient can't be represented in the
10957 computation mode. We shorten only if unsigned or if
10958 dividing by something we know != -1. */
10959 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10960 || (TREE_CODE (op1
) == INTEGER_CST
10961 && !integer_all_onesp (op1
)));
10969 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10971 /* Allow vector types which are not floating point types. */
10972 else if (code0
== VECTOR_TYPE
10973 && code1
== VECTOR_TYPE
10974 && !VECTOR_FLOAT_TYPE_P (type0
)
10975 && !VECTOR_FLOAT_TYPE_P (type1
))
10979 case TRUNC_MOD_EXPR
:
10980 case FLOOR_MOD_EXPR
:
10981 doing_div_or_mod
= true;
10982 warn_for_div_by_zero (location
, op1
);
10984 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10985 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10986 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10988 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10990 /* Although it would be tempting to shorten always here, that loses
10991 on some targets, since the modulo instruction is undefined if the
10992 quotient can't be represented in the computation mode. We shorten
10993 only if unsigned or if dividing by something we know != -1. */
10994 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10995 || (TREE_CODE (op1
) == INTEGER_CST
10996 && !integer_all_onesp (op1
)));
11001 case TRUTH_ANDIF_EXPR
:
11002 case TRUTH_ORIF_EXPR
:
11003 case TRUTH_AND_EXPR
:
11004 case TRUTH_OR_EXPR
:
11005 case TRUTH_XOR_EXPR
:
11006 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
11007 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11008 || code0
== FIXED_POINT_TYPE
)
11009 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
11010 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11011 || code1
== FIXED_POINT_TYPE
))
11013 /* Result of these operations is always an int,
11014 but that does not mean the operands should be
11015 converted to ints! */
11016 result_type
= integer_type_node
;
11017 if (op0_int_operands
)
11019 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
11020 op0
= remove_c_maybe_const_expr (op0
);
11023 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
11024 if (op1_int_operands
)
11026 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
11027 op1
= remove_c_maybe_const_expr (op1
);
11030 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
11034 if (code
== TRUTH_ANDIF_EXPR
)
11036 int_const_or_overflow
= (int_operands
11037 && TREE_CODE (orig_op0
) == INTEGER_CST
11038 && (op0
== truthvalue_false_node
11039 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11040 int_const
= (int_const_or_overflow
11041 && !TREE_OVERFLOW (orig_op0
)
11042 && (op0
== truthvalue_false_node
11043 || !TREE_OVERFLOW (orig_op1
)));
11045 else if (code
== TRUTH_ORIF_EXPR
)
11047 int_const_or_overflow
= (int_operands
11048 && TREE_CODE (orig_op0
) == INTEGER_CST
11049 && (op0
== truthvalue_true_node
11050 || TREE_CODE (orig_op1
) == INTEGER_CST
));
11051 int_const
= (int_const_or_overflow
11052 && !TREE_OVERFLOW (orig_op0
)
11053 && (op0
== truthvalue_true_node
11054 || !TREE_OVERFLOW (orig_op1
)));
11058 /* Shift operations: result has same type as first operand;
11059 always convert second operand to int.
11060 Also set SHORT_SHIFT if shifting rightward. */
11063 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11064 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11065 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11066 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
11068 result_type
= type0
;
11071 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11072 || code0
== VECTOR_TYPE
)
11073 && code1
== INTEGER_TYPE
)
11075 doing_shift
= true;
11076 if (TREE_CODE (op1
) == INTEGER_CST
)
11078 if (tree_int_cst_sgn (op1
) < 0)
11081 if (c_inhibit_evaluation_warnings
== 0)
11082 warning_at (location
, OPT_Wshift_count_negative
,
11083 "right shift count is negative");
11085 else if (code0
== VECTOR_TYPE
)
11087 if (compare_tree_int (op1
,
11088 TYPE_PRECISION (TREE_TYPE (type0
)))
11092 if (c_inhibit_evaluation_warnings
== 0)
11093 warning_at (location
, OPT_Wshift_count_overflow
,
11094 "right shift count >= width of vector element");
11099 if (!integer_zerop (op1
))
11102 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11105 if (c_inhibit_evaluation_warnings
== 0)
11106 warning_at (location
, OPT_Wshift_count_overflow
,
11107 "right shift count >= width of type");
11112 /* Use the type of the value to be shifted. */
11113 result_type
= type0
;
11114 /* Avoid converting op1 to result_type later. */
11120 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11121 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
11122 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
11123 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
11125 result_type
= type0
;
11128 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
11129 || code0
== VECTOR_TYPE
)
11130 && code1
== INTEGER_TYPE
)
11132 doing_shift
= true;
11133 if (TREE_CODE (op0
) == INTEGER_CST
11134 && tree_int_cst_sgn (op0
) < 0)
11136 /* Don't reject a left shift of a negative value in a context
11137 where a constant expression is needed in C90. */
11140 if (c_inhibit_evaluation_warnings
== 0)
11141 warning_at (location
, OPT_Wshift_negative_value
,
11142 "left shift of negative value");
11144 if (TREE_CODE (op1
) == INTEGER_CST
)
11146 if (tree_int_cst_sgn (op1
) < 0)
11149 if (c_inhibit_evaluation_warnings
== 0)
11150 warning_at (location
, OPT_Wshift_count_negative
,
11151 "left shift count is negative");
11153 else if (code0
== VECTOR_TYPE
)
11155 if (compare_tree_int (op1
,
11156 TYPE_PRECISION (TREE_TYPE (type0
)))
11160 if (c_inhibit_evaluation_warnings
== 0)
11161 warning_at (location
, OPT_Wshift_count_overflow
,
11162 "left shift count >= width of vector element");
11165 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
11168 if (c_inhibit_evaluation_warnings
== 0)
11169 warning_at (location
, OPT_Wshift_count_overflow
,
11170 "left shift count >= width of type");
11172 else if (TREE_CODE (op0
) == INTEGER_CST
11173 && maybe_warn_shift_overflow (location
, op0
, op1
)
11178 /* Use the type of the value to be shifted. */
11179 result_type
= type0
;
11180 /* Avoid converting op1 to result_type later. */
11187 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11190 if (!vector_types_compatible_elements_p (type0
, type1
))
11192 error_at (location
, "comparing vectors with different "
11194 return error_mark_node
;
11197 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
11199 error_at (location
, "comparing vectors with different "
11200 "number of elements");
11201 return error_mark_node
;
11204 /* It's not precisely specified how the usual arithmetic
11205 conversions apply to the vector types. Here, we use
11206 the unsigned type if one of the operands is signed and
11207 the other one is unsigned. */
11208 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11210 if (!TYPE_UNSIGNED (type0
))
11211 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11213 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11214 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11215 "types %qT and %qT", type0
, type1
);
11218 /* Always construct signed integer vector type. */
11219 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11220 (TYPE_MODE (TREE_TYPE (type0
))), 0);
11221 result_type
= build_opaque_vector_type (intt
,
11222 TYPE_VECTOR_SUBPARTS (type0
));
11224 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11225 goto return_build_binary_op
;
11227 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
11228 warning_at (location
,
11230 "comparing floating point with == or != is unsafe");
11231 /* Result of comparison is always int,
11232 but don't convert the args to int! */
11233 build_type
= integer_type_node
;
11234 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11235 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
11236 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11237 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
11239 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11241 if (TREE_CODE (op0
) == ADDR_EXPR
11242 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0))
11243 && !from_macro_expansion_at (location
))
11245 if (code
== EQ_EXPR
)
11246 warning_at (location
,
11248 "the comparison will always evaluate as %<false%> "
11249 "for the address of %qD will never be NULL",
11250 TREE_OPERAND (op0
, 0));
11252 warning_at (location
,
11254 "the comparison will always evaluate as %<true%> "
11255 "for the address of %qD will never be NULL",
11256 TREE_OPERAND (op0
, 0));
11258 result_type
= type0
;
11260 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11262 if (TREE_CODE (op1
) == ADDR_EXPR
11263 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0))
11264 && !from_macro_expansion_at (location
))
11266 if (code
== EQ_EXPR
)
11267 warning_at (location
,
11269 "the comparison will always evaluate as %<false%> "
11270 "for the address of %qD will never be NULL",
11271 TREE_OPERAND (op1
, 0));
11273 warning_at (location
,
11275 "the comparison will always evaluate as %<true%> "
11276 "for the address of %qD will never be NULL",
11277 TREE_OPERAND (op1
, 0));
11279 result_type
= type1
;
11281 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
11283 tree tt0
= TREE_TYPE (type0
);
11284 tree tt1
= TREE_TYPE (type1
);
11285 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
11286 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
11287 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
11289 /* Anything compares with void *. void * compares with anything.
11290 Otherwise, the targets must be compatible
11291 and both must be object or both incomplete. */
11292 if (comp_target_types (location
, type0
, type1
))
11293 result_type
= common_pointer_type (type0
, type1
);
11294 else if (!addr_space_superset (as0
, as1
, &as_common
))
11296 error_at (location
, "comparison of pointers to "
11297 "disjoint address spaces");
11298 return error_mark_node
;
11300 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
11302 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
11303 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11304 "comparison of %<void *%> with function pointer");
11306 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
11308 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
11309 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11310 "comparison of %<void *%> with function pointer");
11313 /* Avoid warning about the volatile ObjC EH puts on decls. */
11315 pedwarn (location
, 0,
11316 "comparison of distinct pointer types lacks a cast");
11318 if (result_type
== NULL_TREE
)
11320 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
11321 result_type
= build_pointer_type
11322 (build_qualified_type (void_type_node
, qual
));
11325 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11327 result_type
= type0
;
11328 pedwarn (location
, 0, "comparison between pointer and integer");
11330 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
11332 result_type
= type1
;
11333 pedwarn (location
, 0, "comparison between pointer and integer");
11335 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
11336 || truth_value_p (TREE_CODE (orig_op0
)))
11337 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
11338 || truth_value_p (TREE_CODE (orig_op1
))))
11339 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
11346 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
11349 if (!vector_types_compatible_elements_p (type0
, type1
))
11351 error_at (location
, "comparing vectors with different "
11353 return error_mark_node
;
11356 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
11358 error_at (location
, "comparing vectors with different "
11359 "number of elements");
11360 return error_mark_node
;
11363 /* It's not precisely specified how the usual arithmetic
11364 conversions apply to the vector types. Here, we use
11365 the unsigned type if one of the operands is signed and
11366 the other one is unsigned. */
11367 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
11369 if (!TYPE_UNSIGNED (type0
))
11370 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
11372 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
11373 warning_at (location
, OPT_Wsign_compare
, "comparison between "
11374 "types %qT and %qT", type0
, type1
);
11377 /* Always construct signed integer vector type. */
11378 intt
= c_common_type_for_size (GET_MODE_BITSIZE
11379 (TYPE_MODE (TREE_TYPE (type0
))), 0);
11380 result_type
= build_opaque_vector_type (intt
,
11381 TYPE_VECTOR_SUBPARTS (type0
));
11383 ret
= build_vec_cmp (resultcode
, result_type
, op0
, op1
);
11384 goto return_build_binary_op
;
11386 build_type
= integer_type_node
;
11387 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
11388 || code0
== FIXED_POINT_TYPE
)
11389 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
11390 || code1
== FIXED_POINT_TYPE
))
11392 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
11394 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
11395 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
11396 addr_space_t as_common
;
11398 if (comp_target_types (location
, type0
, type1
))
11400 result_type
= common_pointer_type (type0
, type1
);
11401 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
11402 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
11403 pedwarn (location
, 0,
11404 "comparison of complete and incomplete pointers");
11405 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
11406 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
11407 "ordered comparisons of pointers to functions");
11408 else if (null_pointer_constant_p (orig_op0
)
11409 || null_pointer_constant_p (orig_op1
))
11410 warning_at (location
, OPT_Wextra
,
11411 "ordered comparison of pointer with null pointer");
11414 else if (!addr_space_superset (as0
, as1
, &as_common
))
11416 error_at (location
, "comparison of pointers to "
11417 "disjoint address spaces");
11418 return error_mark_node
;
11422 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
11423 result_type
= build_pointer_type
11424 (build_qualified_type (void_type_node
, qual
));
11425 pedwarn (location
, 0,
11426 "comparison of distinct pointer types lacks a cast");
11429 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
11431 result_type
= type0
;
11433 pedwarn (location
, OPT_Wpedantic
,
11434 "ordered comparison of pointer with integer zero");
11435 else if (extra_warnings
)
11436 warning_at (location
, OPT_Wextra
,
11437 "ordered comparison of pointer with integer zero");
11439 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
11441 result_type
= type1
;
11443 pedwarn (location
, OPT_Wpedantic
,
11444 "ordered comparison of pointer with integer zero");
11445 else if (extra_warnings
)
11446 warning_at (location
, OPT_Wextra
,
11447 "ordered comparison of pointer with integer zero");
11449 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
11451 result_type
= type0
;
11452 pedwarn (location
, 0, "comparison between pointer and integer");
11454 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
11456 result_type
= type1
;
11457 pedwarn (location
, 0, "comparison between pointer and integer");
11459 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
11460 || truth_value_p (TREE_CODE (orig_op0
)))
11461 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
11462 || truth_value_p (TREE_CODE (orig_op1
))))
11463 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
11467 gcc_unreachable ();
11470 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
11471 return error_mark_node
;
11473 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
11474 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
11475 || !vector_types_compatible_elements_p (type0
, type1
)))
11477 gcc_rich_location
richloc (location
);
11478 richloc
.maybe_add_expr (orig_op0
);
11479 richloc
.maybe_add_expr (orig_op1
);
11480 binary_op_error (&richloc
, code
, type0
, type1
);
11481 return error_mark_node
;
11484 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11485 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
11487 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11488 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
11490 bool first_complex
= (code0
== COMPLEX_TYPE
);
11491 bool second_complex
= (code1
== COMPLEX_TYPE
);
11492 int none_complex
= (!first_complex
&& !second_complex
);
11494 if (shorten
|| common
|| short_compare
)
11496 result_type
= c_common_type (type0
, type1
);
11497 do_warn_double_promotion (result_type
, type0
, type1
,
11498 "implicit conversion from %qT to %qT "
11499 "to match other operand of binary "
11502 if (result_type
== error_mark_node
)
11503 return error_mark_node
;
11506 if (first_complex
!= second_complex
11507 && (code
== PLUS_EXPR
11508 || code
== MINUS_EXPR
11509 || code
== MULT_EXPR
11510 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
11511 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
11512 && flag_signed_zeros
)
11514 /* An operation on mixed real/complex operands must be
11515 handled specially, but the language-independent code can
11516 more easily optimize the plain complex arithmetic if
11517 -fno-signed-zeros. */
11518 tree real_type
= TREE_TYPE (result_type
);
11520 if (type0
!= orig_type0
|| type1
!= orig_type1
)
11522 gcc_assert (may_need_excess_precision
&& common
);
11523 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11527 if (TREE_TYPE (op0
) != result_type
)
11528 op0
= convert_and_check (location
, result_type
, op0
);
11529 if (TREE_TYPE (op1
) != real_type
)
11530 op1
= convert_and_check (location
, real_type
, op1
);
11534 if (TREE_TYPE (op0
) != real_type
)
11535 op0
= convert_and_check (location
, real_type
, op0
);
11536 if (TREE_TYPE (op1
) != result_type
)
11537 op1
= convert_and_check (location
, result_type
, op1
);
11539 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11540 return error_mark_node
;
11543 op0
= c_save_expr (op0
);
11544 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
11546 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
11551 case TRUNC_DIV_EXPR
:
11552 op1
= c_save_expr (op1
);
11553 imag
= build2 (resultcode
, real_type
, imag
, op1
);
11554 /* Fall through. */
11557 real
= build2 (resultcode
, real_type
, real
, op1
);
11565 op1
= c_save_expr (op1
);
11566 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
11568 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
11573 op0
= c_save_expr (op0
);
11574 imag
= build2 (resultcode
, real_type
, op0
, imag
);
11575 /* Fall through. */
11577 real
= build2 (resultcode
, real_type
, op0
, real
);
11580 real
= build2 (resultcode
, real_type
, op0
, real
);
11581 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
11587 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
11588 goto return_build_binary_op
;
11591 /* For certain operations (which identify themselves by shorten != 0)
11592 if both args were extended from the same smaller type,
11593 do the arithmetic in that type and then extend.
11595 shorten !=0 and !=1 indicates a bitwise operation.
11596 For them, this optimization is safe only if
11597 both args are zero-extended or both are sign-extended.
11598 Otherwise, we might change the result.
11599 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11600 but calculated in (unsigned short) it would be (unsigned short)-1. */
11602 if (shorten
&& none_complex
)
11604 final_type
= result_type
;
11605 result_type
= shorten_binary_op (result_type
, op0
, op1
,
11609 /* Shifts can be shortened if shifting right. */
11614 tree arg0
= get_narrower (op0
, &unsigned_arg
);
11616 final_type
= result_type
;
11618 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
11619 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
11621 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
11622 && tree_int_cst_sgn (op1
) > 0
11623 /* We can shorten only if the shift count is less than the
11624 number of bits in the smaller type size. */
11625 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
11626 /* We cannot drop an unsigned shift after sign-extension. */
11627 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
11629 /* Do an unsigned shift if the operand was zero-extended. */
11631 = c_common_signed_or_unsigned_type (unsigned_arg
,
11633 /* Convert value-to-be-shifted to that type. */
11634 if (TREE_TYPE (op0
) != result_type
)
11635 op0
= convert (result_type
, op0
);
11640 /* Comparison operations are shortened too but differently.
11641 They identify themselves by setting short_compare = 1. */
11645 /* Don't write &op0, etc., because that would prevent op0
11646 from being kept in a register.
11647 Instead, make copies of the our local variables and
11648 pass the copies by reference, then copy them back afterward. */
11649 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
11650 enum tree_code xresultcode
= resultcode
;
11652 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
11658 goto return_build_binary_op
;
11661 op0
= xop0
, op1
= xop1
;
11663 resultcode
= xresultcode
;
11665 if (c_inhibit_evaluation_warnings
== 0)
11667 bool op0_maybe_const
= true;
11668 bool op1_maybe_const
= true;
11669 tree orig_op0_folded
, orig_op1_folded
;
11671 if (in_late_binary_op
)
11673 orig_op0_folded
= orig_op0
;
11674 orig_op1_folded
= orig_op1
;
11678 /* Fold for the sake of possible warnings, as in
11679 build_conditional_expr. This requires the
11680 "original" values to be folded, not just op0 and
11682 c_inhibit_evaluation_warnings
++;
11683 op0
= c_fully_fold (op0
, require_constant_value
,
11685 op1
= c_fully_fold (op1
, require_constant_value
,
11687 c_inhibit_evaluation_warnings
--;
11688 orig_op0_folded
= c_fully_fold (orig_op0
,
11689 require_constant_value
,
11691 orig_op1_folded
= c_fully_fold (orig_op1
,
11692 require_constant_value
,
11696 if (warn_sign_compare
)
11697 warn_for_sign_compare (location
, orig_op0_folded
,
11698 orig_op1_folded
, op0
, op1
,
11699 result_type
, resultcode
);
11700 if (!in_late_binary_op
&& !int_operands
)
11702 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
11703 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
11704 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
11705 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
11711 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11712 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11713 Then the expression will be built.
11714 It will be given type FINAL_TYPE if that is nonzero;
11715 otherwise, it will be given type RESULT_TYPE. */
11719 gcc_rich_location
richloc (location
);
11720 richloc
.maybe_add_expr (orig_op0
);
11721 richloc
.maybe_add_expr (orig_op1
);
11722 binary_op_error (&richloc
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
11723 return error_mark_node
;
11726 if (build_type
== NULL_TREE
)
11728 build_type
= result_type
;
11729 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
11732 gcc_assert (may_need_excess_precision
&& common
);
11733 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11739 op0
= ep_convert_and_check (location
, result_type
, op0
,
11740 semantic_result_type
);
11741 op1
= ep_convert_and_check (location
, result_type
, op1
,
11742 semantic_result_type
);
11744 /* This can happen if one operand has a vector type, and the other
11745 has a different type. */
11746 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11747 return error_mark_node
;
11750 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
11751 | SANITIZE_FLOAT_DIVIDE
))
11752 && do_ubsan_in_current_function ()
11753 && (doing_div_or_mod
|| doing_shift
)
11754 && !require_constant_value
)
11756 /* OP0 and/or OP1 might have side-effects. */
11757 op0
= c_save_expr (op0
);
11758 op1
= c_save_expr (op1
);
11759 op0
= c_fully_fold (op0
, false, NULL
);
11760 op1
= c_fully_fold (op1
, false, NULL
);
11761 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
11762 | SANITIZE_FLOAT_DIVIDE
)))
11763 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
11764 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
11765 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
11768 /* Treat expressions in initializers specially as they can't trap. */
11769 if (int_const_or_overflow
)
11770 ret
= (require_constant_value
11771 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
11773 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
11775 ret
= build2 (resultcode
, build_type
, op0
, op1
);
11776 if (final_type
!= 0)
11777 ret
= convert (final_type
, ret
);
11779 return_build_binary_op
:
11780 gcc_assert (ret
!= error_mark_node
);
11781 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
11782 ret
= (int_operands
11783 ? note_integer_operands (ret
)
11784 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
11785 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
11786 && !in_late_binary_op
)
11787 ret
= note_integer_operands (ret
);
11788 if (semantic_result_type
)
11789 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
11790 protected_set_expr_location (ret
, location
);
11792 if (instrument_expr
!= NULL
)
11793 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
11794 instrument_expr
, ret
);
11800 /* Convert EXPR to be a truth-value, validating its type for this
11801 purpose. LOCATION is the source location for the expression. */
11804 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
11806 bool int_const
, int_operands
;
11808 switch (TREE_CODE (TREE_TYPE (expr
)))
11811 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
11812 return error_mark_node
;
11815 error_at (location
, "used struct type value where scalar is required");
11816 return error_mark_node
;
11819 error_at (location
, "used union type value where scalar is required");
11820 return error_mark_node
;
11823 error_at (location
, "void value not ignored as it ought to be");
11824 return error_mark_node
;
11827 if (reject_gcc_builtin (expr
))
11828 return error_mark_node
;
11831 case FUNCTION_TYPE
:
11832 gcc_unreachable ();
11835 error_at (location
, "used vector type where scalar is required");
11836 return error_mark_node
;
11842 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11843 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11844 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11846 expr
= remove_c_maybe_const_expr (expr
);
11847 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11848 convert (TREE_TYPE (expr
), integer_zero_node
));
11849 expr
= note_integer_operands (expr
);
11852 /* ??? Should we also give an error for vectors rather than leaving
11853 those to give errors later? */
11854 expr
= c_common_truthvalue_conversion (location
, expr
);
11856 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11858 if (TREE_OVERFLOW (expr
))
11861 return note_integer_operands (expr
);
11863 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11864 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11869 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11873 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11875 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11877 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11878 /* Executing a compound literal inside a function reinitializes
11880 if (!TREE_STATIC (decl
))
11888 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11889 statement. LOC is the location of the construct. */
11892 c_finish_omp_construct (location_t loc
, enum tree_code code
, tree body
,
11895 body
= c_end_compound_stmt (loc
, body
, true);
11897 tree stmt
= make_node (code
);
11898 TREE_TYPE (stmt
) = void_type_node
;
11899 OMP_BODY (stmt
) = body
;
11900 OMP_CLAUSES (stmt
) = clauses
;
11901 SET_EXPR_LOCATION (stmt
, loc
);
11903 return add_stmt (stmt
);
11906 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11907 statement. LOC is the location of the OACC_DATA. */
11910 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
11914 block
= c_end_compound_stmt (loc
, block
, true);
11916 stmt
= make_node (OACC_DATA
);
11917 TREE_TYPE (stmt
) = void_type_node
;
11918 OACC_DATA_CLAUSES (stmt
) = clauses
;
11919 OACC_DATA_BODY (stmt
) = block
;
11920 SET_EXPR_LOCATION (stmt
, loc
);
11922 return add_stmt (stmt
);
11925 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11926 statement. LOC is the location of the OACC_HOST_DATA. */
11929 c_finish_oacc_host_data (location_t loc
, tree clauses
, tree block
)
11933 block
= c_end_compound_stmt (loc
, block
, true);
11935 stmt
= make_node (OACC_HOST_DATA
);
11936 TREE_TYPE (stmt
) = void_type_node
;
11937 OACC_HOST_DATA_CLAUSES (stmt
) = clauses
;
11938 OACC_HOST_DATA_BODY (stmt
) = block
;
11939 SET_EXPR_LOCATION (stmt
, loc
);
11941 return add_stmt (stmt
);
11944 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11947 c_begin_omp_parallel (void)
11951 keep_next_level ();
11952 block
= c_begin_compound_stmt (true);
11957 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11958 statement. LOC is the location of the OMP_PARALLEL. */
11961 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11965 block
= c_end_compound_stmt (loc
, block
, true);
11967 stmt
= make_node (OMP_PARALLEL
);
11968 TREE_TYPE (stmt
) = void_type_node
;
11969 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11970 OMP_PARALLEL_BODY (stmt
) = block
;
11971 SET_EXPR_LOCATION (stmt
, loc
);
11973 return add_stmt (stmt
);
11976 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11979 c_begin_omp_task (void)
11983 keep_next_level ();
11984 block
= c_begin_compound_stmt (true);
11989 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11990 statement. LOC is the location of the #pragma. */
11993 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11997 block
= c_end_compound_stmt (loc
, block
, true);
11999 stmt
= make_node (OMP_TASK
);
12000 TREE_TYPE (stmt
) = void_type_node
;
12001 OMP_TASK_CLAUSES (stmt
) = clauses
;
12002 OMP_TASK_BODY (stmt
) = block
;
12003 SET_EXPR_LOCATION (stmt
, loc
);
12005 return add_stmt (stmt
);
12008 /* Generate GOMP_cancel call for #pragma omp cancel. */
12011 c_finish_omp_cancel (location_t loc
, tree clauses
)
12013 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
12015 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12017 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
12019 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12021 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12025 error_at (loc
, "%<#pragma omp cancel%> must specify one of "
12026 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12030 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
12031 if (ifc
!= NULL_TREE
)
12033 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
12034 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
12035 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
12036 build_zero_cst (type
));
12039 ifc
= boolean_true_node
;
12040 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
12041 build_int_cst (integer_type_node
, mask
),
12046 /* Generate GOMP_cancellation_point call for
12047 #pragma omp cancellation point. */
12050 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
12052 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
12054 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
12056 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
12058 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
12060 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
12064 error_at (loc
, "%<#pragma omp cancellation point%> must specify one of "
12065 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12069 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
12070 build_int_cst (integer_type_node
, mask
));
12074 /* Helper function for handle_omp_array_sections. Called recursively
12075 to handle multiple array-section-subscripts. C is the clause,
12076 T current expression (initially OMP_CLAUSE_DECL), which is either
12077 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12078 expression if specified, TREE_VALUE length expression if specified,
12079 TREE_CHAIN is what it has been specified after, or some decl.
12080 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12081 set to true if any of the array-section-subscript could have length
12082 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12083 first array-section-subscript which is known not to have length
12085 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12086 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12087 all are or may have length of 1, array-section-subscript [:2] is the
12088 first one known not to have length 1. For array-section-subscript
12089 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12090 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12091 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12092 case though, as some lengths could be zero. */
12095 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
12096 bool &maybe_zero_len
, unsigned int &first_non_one
,
12097 enum c_omp_region_type ort
)
12099 tree ret
, low_bound
, length
, type
;
12100 if (TREE_CODE (t
) != TREE_LIST
)
12102 if (error_operand_p (t
))
12103 return error_mark_node
;
12105 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12106 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
12108 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qE in %qs clause",
12109 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12110 return error_mark_node
;
12112 if (TREE_CODE (t
) == COMPONENT_REF
12113 && ort
== C_ORT_OMP
12114 && (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12115 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO
12116 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FROM
))
12118 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
12120 error_at (OMP_CLAUSE_LOCATION (c
),
12121 "bit-field %qE in %qs clause",
12122 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12123 return error_mark_node
;
12125 while (TREE_CODE (t
) == COMPONENT_REF
)
12127 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == UNION_TYPE
)
12129 error_at (OMP_CLAUSE_LOCATION (c
),
12130 "%qE is a member of a union", t
);
12131 return error_mark_node
;
12133 t
= TREE_OPERAND (t
, 0);
12136 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
12139 error_at (OMP_CLAUSE_LOCATION (c
),
12140 "%qD is not a variable in %qs clause", t
,
12141 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12143 error_at (OMP_CLAUSE_LOCATION (c
),
12144 "%qE is not a variable in %qs clause", t
,
12145 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12146 return error_mark_node
;
12148 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12149 && TYPE_ATOMIC (TREE_TYPE (t
)))
12151 error_at (OMP_CLAUSE_LOCATION (c
), "%<_Atomic%> %qD in %qs clause",
12152 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12153 return error_mark_node
;
12155 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12157 && DECL_THREAD_LOCAL_P (t
))
12159 error_at (OMP_CLAUSE_LOCATION (c
),
12160 "%qD is threadprivate variable in %qs clause", t
,
12161 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12162 return error_mark_node
;
12164 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12165 && TYPE_ATOMIC (TREE_TYPE (t
))
12166 && POINTER_TYPE_P (TREE_TYPE (t
)))
12168 /* If the array section is pointer based and the pointer
12169 itself is _Atomic qualified, we need to atomically load
12172 memset (&expr
, 0, sizeof (expr
));
12174 expr
= convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c
),
12175 expr
, false, false);
12181 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
12182 maybe_zero_len
, first_non_one
, ort
);
12183 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
12186 type
= TREE_TYPE (ret
);
12187 low_bound
= TREE_PURPOSE (t
);
12188 length
= TREE_VALUE (t
);
12190 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
12191 return error_mark_node
;
12193 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
12195 error_at (OMP_CLAUSE_LOCATION (c
),
12196 "low bound %qE of array section does not have integral type",
12198 return error_mark_node
;
12200 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
12202 error_at (OMP_CLAUSE_LOCATION (c
),
12203 "length %qE of array section does not have integral type",
12205 return error_mark_node
;
12208 && TREE_CODE (low_bound
) == INTEGER_CST
12209 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12210 > TYPE_PRECISION (sizetype
))
12211 low_bound
= fold_convert (sizetype
, low_bound
);
12213 && TREE_CODE (length
) == INTEGER_CST
12214 && TYPE_PRECISION (TREE_TYPE (length
))
12215 > TYPE_PRECISION (sizetype
))
12216 length
= fold_convert (sizetype
, length
);
12217 if (low_bound
== NULL_TREE
)
12218 low_bound
= integer_zero_node
;
12220 if (length
!= NULL_TREE
)
12222 if (!integer_nonzerop (length
))
12224 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12225 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12227 if (integer_zerop (length
))
12229 error_at (OMP_CLAUSE_LOCATION (c
),
12230 "zero length array section in %qs clause",
12231 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12232 return error_mark_node
;
12236 maybe_zero_len
= true;
12238 if (first_non_one
== types
.length ()
12239 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
12242 if (TREE_CODE (type
) == ARRAY_TYPE
)
12244 if (length
== NULL_TREE
12245 && (TYPE_DOMAIN (type
) == NULL_TREE
12246 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
12248 error_at (OMP_CLAUSE_LOCATION (c
),
12249 "for unknown bound array type length expression must "
12251 return error_mark_node
;
12253 if (TREE_CODE (low_bound
) == INTEGER_CST
12254 && tree_int_cst_sgn (low_bound
) == -1)
12256 error_at (OMP_CLAUSE_LOCATION (c
),
12257 "negative low bound in array section in %qs clause",
12258 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12259 return error_mark_node
;
12261 if (length
!= NULL_TREE
12262 && TREE_CODE (length
) == INTEGER_CST
12263 && tree_int_cst_sgn (length
) == -1)
12265 error_at (OMP_CLAUSE_LOCATION (c
),
12266 "negative length in array section in %qs clause",
12267 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12268 return error_mark_node
;
12270 if (TYPE_DOMAIN (type
)
12271 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
12272 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
12275 tree size
= size_binop (PLUS_EXPR
,
12276 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
12278 if (TREE_CODE (low_bound
) == INTEGER_CST
)
12280 if (tree_int_cst_lt (size
, low_bound
))
12282 error_at (OMP_CLAUSE_LOCATION (c
),
12283 "low bound %qE above array section size "
12284 "in %qs clause", low_bound
,
12285 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12286 return error_mark_node
;
12288 if (tree_int_cst_equal (size
, low_bound
))
12290 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
12291 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12293 error_at (OMP_CLAUSE_LOCATION (c
),
12294 "zero length array section in %qs clause",
12295 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12296 return error_mark_node
;
12298 maybe_zero_len
= true;
12300 else if (length
== NULL_TREE
12301 && first_non_one
== types
.length ()
12302 && tree_int_cst_equal
12303 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
12307 else if (length
== NULL_TREE
)
12309 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12310 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
)
12311 maybe_zero_len
= true;
12312 if (first_non_one
== types
.length ())
12315 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
12317 if (tree_int_cst_lt (size
, length
))
12319 error_at (OMP_CLAUSE_LOCATION (c
),
12320 "length %qE above array section size "
12321 "in %qs clause", length
,
12322 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12323 return error_mark_node
;
12325 if (TREE_CODE (low_bound
) == INTEGER_CST
)
12328 = size_binop (PLUS_EXPR
,
12329 fold_convert (sizetype
, low_bound
),
12330 fold_convert (sizetype
, length
));
12331 if (TREE_CODE (lbpluslen
) == INTEGER_CST
12332 && tree_int_cst_lt (size
, lbpluslen
))
12334 error_at (OMP_CLAUSE_LOCATION (c
),
12335 "high bound %qE above array section size "
12336 "in %qs clause", lbpluslen
,
12337 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12338 return error_mark_node
;
12343 else if (length
== NULL_TREE
)
12345 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12346 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_REDUCTION
)
12347 maybe_zero_len
= true;
12348 if (first_non_one
== types
.length ())
12352 /* For [lb:] we will need to evaluate lb more than once. */
12353 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
12355 tree lb
= c_save_expr (low_bound
);
12356 if (lb
!= low_bound
)
12358 TREE_PURPOSE (t
) = lb
;
12363 else if (TREE_CODE (type
) == POINTER_TYPE
)
12365 if (length
== NULL_TREE
)
12367 error_at (OMP_CLAUSE_LOCATION (c
),
12368 "for pointer type length expression must be specified");
12369 return error_mark_node
;
12371 if (length
!= NULL_TREE
12372 && TREE_CODE (length
) == INTEGER_CST
12373 && tree_int_cst_sgn (length
) == -1)
12375 error_at (OMP_CLAUSE_LOCATION (c
),
12376 "negative length in array section in %qs clause",
12377 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12378 return error_mark_node
;
12380 /* If there is a pointer type anywhere but in the very first
12381 array-section-subscript, the array section can't be contiguous. */
12382 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
12383 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
12385 error_at (OMP_CLAUSE_LOCATION (c
),
12386 "array section is not contiguous in %qs clause",
12387 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12388 return error_mark_node
;
12393 error_at (OMP_CLAUSE_LOCATION (c
),
12394 "%qE does not have pointer or array type", ret
);
12395 return error_mark_node
;
12397 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
12398 types
.safe_push (TREE_TYPE (ret
));
12399 /* We will need to evaluate lb more than once. */
12400 tree lb
= c_save_expr (low_bound
);
12401 if (lb
!= low_bound
)
12403 TREE_PURPOSE (t
) = lb
;
12406 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
12410 /* Handle array sections for clause C. */
12413 handle_omp_array_sections (tree c
, enum c_omp_region_type ort
)
12415 bool maybe_zero_len
= false;
12416 unsigned int first_non_one
= 0;
12417 auto_vec
<tree
, 10> types
;
12418 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
12419 maybe_zero_len
, first_non_one
,
12421 if (first
== error_mark_node
)
12423 if (first
== NULL_TREE
)
12425 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
12427 tree t
= OMP_CLAUSE_DECL (c
);
12428 tree tem
= NULL_TREE
;
12429 /* Need to evaluate side effects in the length expressions
12431 while (TREE_CODE (t
) == TREE_LIST
)
12433 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
12435 if (tem
== NULL_TREE
)
12436 tem
= TREE_VALUE (t
);
12438 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
12439 TREE_VALUE (t
), tem
);
12441 t
= TREE_CHAIN (t
);
12444 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
12445 first
= c_fully_fold (first
, false, NULL
);
12446 OMP_CLAUSE_DECL (c
) = first
;
12450 unsigned int num
= types
.length (), i
;
12451 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
12452 tree condition
= NULL_TREE
;
12454 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
12455 maybe_zero_len
= true;
12457 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
12458 t
= TREE_CHAIN (t
))
12460 tree low_bound
= TREE_PURPOSE (t
);
12461 tree length
= TREE_VALUE (t
);
12465 && TREE_CODE (low_bound
) == INTEGER_CST
12466 && TYPE_PRECISION (TREE_TYPE (low_bound
))
12467 > TYPE_PRECISION (sizetype
))
12468 low_bound
= fold_convert (sizetype
, low_bound
);
12470 && TREE_CODE (length
) == INTEGER_CST
12471 && TYPE_PRECISION (TREE_TYPE (length
))
12472 > TYPE_PRECISION (sizetype
))
12473 length
= fold_convert (sizetype
, length
);
12474 if (low_bound
== NULL_TREE
)
12475 low_bound
= integer_zero_node
;
12476 if (!maybe_zero_len
&& i
> first_non_one
)
12478 if (integer_nonzerop (low_bound
))
12479 goto do_warn_noncontiguous
;
12480 if (length
!= NULL_TREE
12481 && TREE_CODE (length
) == INTEGER_CST
12482 && TYPE_DOMAIN (types
[i
])
12483 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
12484 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
12488 size
= size_binop (PLUS_EXPR
,
12489 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
12491 if (!tree_int_cst_equal (length
, size
))
12493 do_warn_noncontiguous
:
12494 error_at (OMP_CLAUSE_LOCATION (c
),
12495 "array section is not contiguous in %qs "
12497 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12501 if (length
!= NULL_TREE
12502 && TREE_SIDE_EFFECTS (length
))
12504 if (side_effects
== NULL_TREE
)
12505 side_effects
= length
;
12507 side_effects
= build2 (COMPOUND_EXPR
,
12508 TREE_TYPE (side_effects
),
12509 length
, side_effects
);
12516 if (i
> first_non_one
12517 && ((length
&& integer_nonzerop (length
))
12518 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
))
12521 l
= fold_convert (sizetype
, length
);
12524 l
= size_binop (PLUS_EXPR
,
12525 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
12527 l
= size_binop (MINUS_EXPR
, l
,
12528 fold_convert (sizetype
, low_bound
));
12530 if (i
> first_non_one
)
12532 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
12534 if (condition
== NULL_TREE
)
12537 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
12540 else if (size
== NULL_TREE
)
12542 size
= size_in_bytes (TREE_TYPE (types
[i
]));
12543 tree eltype
= TREE_TYPE (types
[num
- 1]);
12544 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
12545 eltype
= TREE_TYPE (eltype
);
12546 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12548 if (integer_zerop (size
)
12549 || integer_zerop (size_in_bytes (eltype
)))
12551 error_at (OMP_CLAUSE_LOCATION (c
),
12552 "zero length array section in %qs clause",
12553 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12554 return error_mark_node
;
12556 size
= size_binop (EXACT_DIV_EXPR
, size
,
12557 size_in_bytes (eltype
));
12559 size
= size_binop (MULT_EXPR
, size
, l
);
12561 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
12562 size
, size_zero_node
);
12565 size
= size_binop (MULT_EXPR
, size
, l
);
12569 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
12570 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
12572 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
12573 size
= c_fully_fold (size
, false, NULL
);
12574 tree index_type
= build_index_type (size
);
12575 tree eltype
= TREE_TYPE (first
);
12576 while (TREE_CODE (eltype
) == ARRAY_TYPE
)
12577 eltype
= TREE_TYPE (eltype
);
12578 tree type
= build_array_type (eltype
, index_type
);
12579 tree ptype
= build_pointer_type (eltype
);
12580 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
12581 t
= build_fold_addr_expr (t
);
12582 tree t2
= build_fold_addr_expr (first
);
12583 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
12584 ptrdiff_type_node
, t2
);
12585 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12586 ptrdiff_type_node
, t2
,
12587 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
12588 ptrdiff_type_node
, t
));
12589 t2
= c_fully_fold (t2
, false, NULL
);
12590 if (tree_fits_shwi_p (t2
))
12591 t
= build2 (MEM_REF
, type
, t
,
12592 build_int_cst (ptype
, tree_to_shwi (t2
)));
12595 t2
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), sizetype
, t2
);
12596 t
= build2_loc (OMP_CLAUSE_LOCATION (c
), POINTER_PLUS_EXPR
,
12597 TREE_TYPE (t
), t
, t2
);
12598 t
= build2 (MEM_REF
, type
, t
, build_int_cst (ptype
, 0));
12600 OMP_CLAUSE_DECL (c
) = t
;
12603 first
= c_fully_fold (first
, false, NULL
);
12604 OMP_CLAUSE_DECL (c
) = first
;
12606 size
= c_fully_fold (size
, false, NULL
);
12607 OMP_CLAUSE_SIZE (c
) = size
;
12608 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
12609 || (TREE_CODE (t
) == COMPONENT_REF
12610 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
))
12612 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
12613 if (ort
== C_ORT_OMP
|| ort
== C_ORT_ACC
)
12614 switch (OMP_CLAUSE_MAP_KIND (c
))
12616 case GOMP_MAP_ALLOC
:
12618 case GOMP_MAP_FROM
:
12619 case GOMP_MAP_TOFROM
:
12620 case GOMP_MAP_ALWAYS_TO
:
12621 case GOMP_MAP_ALWAYS_FROM
:
12622 case GOMP_MAP_ALWAYS_TOFROM
:
12623 case GOMP_MAP_RELEASE
:
12624 case GOMP_MAP_DELETE
:
12625 case GOMP_MAP_FORCE_TO
:
12626 case GOMP_MAP_FORCE_FROM
:
12627 case GOMP_MAP_FORCE_TOFROM
:
12628 case GOMP_MAP_FORCE_PRESENT
:
12629 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c
) = 1;
12634 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
12635 if (ort
!= C_ORT_OMP
&& ort
!= C_ORT_ACC
)
12636 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_POINTER
);
12637 else if (TREE_CODE (t
) == COMPONENT_REF
)
12638 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_ALWAYS_POINTER
);
12640 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_FIRSTPRIVATE_POINTER
);
12641 if (OMP_CLAUSE_MAP_KIND (c2
) != GOMP_MAP_FIRSTPRIVATE_POINTER
12642 && !c_mark_addressable (t
))
12644 OMP_CLAUSE_DECL (c2
) = t
;
12645 t
= build_fold_addr_expr (first
);
12646 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
12647 tree ptr
= OMP_CLAUSE_DECL (c2
);
12648 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
12649 ptr
= build_fold_addr_expr (ptr
);
12650 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12651 ptrdiff_type_node
, t
,
12652 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
12653 ptrdiff_type_node
, ptr
));
12654 t
= c_fully_fold (t
, false, NULL
);
12655 OMP_CLAUSE_SIZE (c2
) = t
;
12656 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
12657 OMP_CLAUSE_CHAIN (c
) = c2
;
12662 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12663 an inline call. But, remap
12664 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12665 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12668 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
12669 tree decl
, tree placeholder
)
12672 hash_map
<tree
, tree
> decl_map
;
12674 decl_map
.put (omp_decl1
, placeholder
);
12675 decl_map
.put (omp_decl2
, decl
);
12676 memset (&id
, 0, sizeof (id
));
12677 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
12678 id
.dst_fn
= current_function_decl
;
12679 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
12680 id
.decl_map
= &decl_map
;
12682 id
.copy_decl
= copy_decl_no_change
;
12683 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
12684 id
.transform_new_cfg
= true;
12685 id
.transform_return_to_modify
= false;
12686 id
.transform_lang_insert_block
= NULL
;
12688 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
12692 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12693 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12696 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
12698 if (*tp
== (tree
) data
)
12703 /* For all elements of CLAUSES, validate them against their constraints.
12704 Remove any elements from the list that are invalid. */
12707 c_finish_omp_clauses (tree clauses
, enum c_omp_region_type ort
)
12709 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
12710 bitmap_head aligned_head
, map_head
, map_field_head
, oacc_reduction_head
;
12711 tree c
, t
, type
, *pc
;
12712 tree simdlen
= NULL_TREE
, safelen
= NULL_TREE
;
12713 bool branch_seen
= false;
12714 bool copyprivate_seen
= false;
12715 bool linear_variable_step_check
= false;
12716 tree
*nowait_clause
= NULL
;
12717 bool ordered_seen
= false;
12718 tree schedule_clause
= NULL_TREE
;
12719 bool oacc_async
= false;
12721 bitmap_obstack_initialize (NULL
);
12722 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
12723 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
12724 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
12725 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
12726 bitmap_initialize (&map_head
, &bitmap_default_obstack
);
12727 bitmap_initialize (&map_field_head
, &bitmap_default_obstack
);
12728 bitmap_initialize (&oacc_reduction_head
, &bitmap_default_obstack
);
12730 if (ort
& C_ORT_ACC
)
12731 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12732 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_ASYNC
)
12738 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
12740 bool remove
= false;
12741 bool need_complete
= false;
12742 bool need_implicitly_determined
= false;
12744 switch (OMP_CLAUSE_CODE (c
))
12746 case OMP_CLAUSE_SHARED
:
12747 need_implicitly_determined
= true;
12748 goto check_dup_generic
;
12750 case OMP_CLAUSE_PRIVATE
:
12751 need_complete
= true;
12752 need_implicitly_determined
= true;
12753 goto check_dup_generic
;
12755 case OMP_CLAUSE_REDUCTION
:
12756 need_implicitly_determined
= true;
12757 t
= OMP_CLAUSE_DECL (c
);
12758 if (TREE_CODE (t
) == TREE_LIST
)
12760 if (handle_omp_array_sections (c
, ort
))
12766 t
= OMP_CLAUSE_DECL (c
);
12768 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
12769 if (t
== error_mark_node
)
12775 c_mark_addressable (t
);
12776 type
= TREE_TYPE (t
);
12777 if (TREE_CODE (t
) == MEM_REF
)
12778 type
= TREE_TYPE (type
);
12779 if (TREE_CODE (type
) == ARRAY_TYPE
)
12781 tree oatype
= type
;
12782 gcc_assert (TREE_CODE (t
) != MEM_REF
);
12783 while (TREE_CODE (type
) == ARRAY_TYPE
)
12784 type
= TREE_TYPE (type
);
12785 if (integer_zerop (TYPE_SIZE_UNIT (type
)))
12787 error_at (OMP_CLAUSE_LOCATION (c
),
12788 "%qD in %<reduction%> clause is a zero size array",
12793 tree size
= size_binop (EXACT_DIV_EXPR
, TYPE_SIZE_UNIT (oatype
),
12794 TYPE_SIZE_UNIT (type
));
12795 if (integer_zerop (size
))
12797 error_at (OMP_CLAUSE_LOCATION (c
),
12798 "%qD in %<reduction%> clause is a zero size array",
12803 size
= size_binop (MINUS_EXPR
, size
, size_one_node
);
12804 tree index_type
= build_index_type (size
);
12805 tree atype
= build_array_type (type
, index_type
);
12806 tree ptype
= build_pointer_type (type
);
12807 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
12808 t
= build_fold_addr_expr (t
);
12809 t
= build2 (MEM_REF
, atype
, t
, build_int_cst (ptype
, 0));
12810 OMP_CLAUSE_DECL (c
) = t
;
12812 if (TYPE_ATOMIC (type
))
12814 error_at (OMP_CLAUSE_LOCATION (c
),
12815 "%<_Atomic%> %qE in %<reduction%> clause", t
);
12819 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
12820 && (FLOAT_TYPE_P (type
)
12821 || TREE_CODE (type
) == COMPLEX_TYPE
))
12823 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
12824 const char *r_name
= NULL
;
12833 if (TREE_CODE (type
) == COMPLEX_TYPE
)
12837 if (TREE_CODE (type
) == COMPLEX_TYPE
)
12849 case TRUTH_ANDIF_EXPR
:
12850 if (FLOAT_TYPE_P (type
))
12853 case TRUTH_ORIF_EXPR
:
12854 if (FLOAT_TYPE_P (type
))
12858 gcc_unreachable ();
12862 error_at (OMP_CLAUSE_LOCATION (c
),
12863 "%qE has invalid type for %<reduction(%s)%>",
12869 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
12871 error_at (OMP_CLAUSE_LOCATION (c
),
12872 "user defined reduction not found for %qE", t
);
12876 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
12878 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
12879 type
= TYPE_MAIN_VARIANT (type
);
12880 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
12881 VAR_DECL
, NULL_TREE
, type
);
12882 tree decl_placeholder
= NULL_TREE
;
12883 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
12884 DECL_ARTIFICIAL (placeholder
) = 1;
12885 DECL_IGNORED_P (placeholder
) = 1;
12886 if (TREE_CODE (t
) == MEM_REF
)
12888 decl_placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
12889 VAR_DECL
, NULL_TREE
, type
);
12890 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c
) = decl_placeholder
;
12891 DECL_ARTIFICIAL (decl_placeholder
) = 1;
12892 DECL_IGNORED_P (decl_placeholder
) = 1;
12894 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
12895 c_mark_addressable (placeholder
);
12896 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
12897 c_mark_addressable (decl_placeholder
? decl_placeholder
12898 : OMP_CLAUSE_DECL (c
));
12899 OMP_CLAUSE_REDUCTION_MERGE (c
)
12900 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
12901 TREE_VEC_ELT (list
, 0),
12902 TREE_VEC_ELT (list
, 1),
12903 decl_placeholder
? decl_placeholder
12904 : OMP_CLAUSE_DECL (c
), placeholder
);
12905 OMP_CLAUSE_REDUCTION_MERGE (c
)
12906 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12907 void_type_node
, NULL_TREE
,
12908 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
12909 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
12910 if (TREE_VEC_LENGTH (list
) == 6)
12912 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
12913 c_mark_addressable (decl_placeholder
? decl_placeholder
12914 : OMP_CLAUSE_DECL (c
));
12915 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
12916 c_mark_addressable (placeholder
);
12917 tree init
= TREE_VEC_ELT (list
, 5);
12918 if (init
== error_mark_node
)
12919 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
12920 OMP_CLAUSE_REDUCTION_INIT (c
)
12921 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
12922 TREE_VEC_ELT (list
, 3),
12923 decl_placeholder
? decl_placeholder
12924 : OMP_CLAUSE_DECL (c
), placeholder
);
12925 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
12927 tree v
= decl_placeholder
? decl_placeholder
: t
;
12928 OMP_CLAUSE_REDUCTION_INIT (c
)
12929 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
,
12930 OMP_CLAUSE_REDUCTION_INIT (c
));
12932 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
12933 c_find_omp_placeholder_r
,
12934 placeholder
, NULL
))
12935 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
12940 tree v
= decl_placeholder
? decl_placeholder
: t
;
12941 if (AGGREGATE_TYPE_P (TREE_TYPE (v
)))
12942 init
= build_constructor (TREE_TYPE (v
), NULL
);
12944 init
= fold_convert (TREE_TYPE (v
), integer_zero_node
);
12945 OMP_CLAUSE_REDUCTION_INIT (c
)
12946 = build2 (INIT_EXPR
, TREE_TYPE (v
), v
, init
);
12948 OMP_CLAUSE_REDUCTION_INIT (c
)
12949 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12950 void_type_node
, NULL_TREE
,
12951 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
12952 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
12954 if (TREE_CODE (t
) == MEM_REF
)
12956 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))) == NULL_TREE
12957 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))))
12960 sorry ("variable length element type in array "
12961 "%<reduction%> clause");
12965 t
= TREE_OPERAND (t
, 0);
12966 if (TREE_CODE (t
) == POINTER_PLUS_EXPR
)
12967 t
= TREE_OPERAND (t
, 0);
12968 if (TREE_CODE (t
) == ADDR_EXPR
)
12969 t
= TREE_OPERAND (t
, 0);
12971 goto check_dup_generic_t
;
12973 case OMP_CLAUSE_COPYPRIVATE
:
12974 copyprivate_seen
= true;
12977 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
12978 "%<nowait%> clause must not be used together "
12979 "with %<copyprivate%>");
12980 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
12981 nowait_clause
= NULL
;
12983 goto check_dup_generic
;
12985 case OMP_CLAUSE_COPYIN
:
12986 t
= OMP_CLAUSE_DECL (c
);
12987 if (!VAR_P (t
) || !DECL_THREAD_LOCAL_P (t
))
12989 error_at (OMP_CLAUSE_LOCATION (c
),
12990 "%qE must be %<threadprivate%> for %<copyin%>", t
);
12994 goto check_dup_generic
;
12996 case OMP_CLAUSE_LINEAR
:
12997 if (ort
!= C_ORT_OMP_DECLARE_SIMD
)
12998 need_implicitly_determined
= true;
12999 t
= OMP_CLAUSE_DECL (c
);
13000 if (ort
!= C_ORT_OMP_DECLARE_SIMD
13001 && OMP_CLAUSE_LINEAR_KIND (c
) != OMP_CLAUSE_LINEAR_DEFAULT
)
13003 error_at (OMP_CLAUSE_LOCATION (c
),
13004 "modifier should not be specified in %<linear%> "
13005 "clause on %<simd%> or %<for%> constructs");
13006 OMP_CLAUSE_LINEAR_KIND (c
) = OMP_CLAUSE_LINEAR_DEFAULT
;
13008 if (ort
& C_ORT_CILK
)
13010 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
13011 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t
))
13012 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
13014 error_at (OMP_CLAUSE_LOCATION (c
),
13015 "linear clause applied to non-integral, "
13016 "non-floating, non-pointer variable with type %qT",
13024 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
13025 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
13027 error_at (OMP_CLAUSE_LOCATION (c
),
13028 "linear clause applied to non-integral non-pointer "
13029 "variable with type %qT", TREE_TYPE (t
));
13033 if (TYPE_ATOMIC (TREE_TYPE (t
)))
13035 error_at (OMP_CLAUSE_LOCATION (c
),
13036 "%<_Atomic%> %qD in %<linear%> clause", t
);
13041 if (ort
== C_ORT_OMP_DECLARE_SIMD
)
13043 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
13044 if (TREE_CODE (s
) == PARM_DECL
)
13046 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
) = 1;
13047 /* map_head bitmap is used as uniform_head if
13049 if (!bitmap_bit_p (&map_head
, DECL_UID (s
)))
13050 linear_variable_step_check
= true;
13051 goto check_dup_generic
;
13053 if (TREE_CODE (s
) != INTEGER_CST
)
13055 error_at (OMP_CLAUSE_LOCATION (c
),
13056 "%<linear%> clause step %qE is neither constant "
13057 "nor a parameter", s
);
13062 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
13064 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
13065 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
13066 OMP_CLAUSE_DECL (c
), s
);
13067 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13068 sizetype
, fold_convert (sizetype
, s
),
13070 (sizetype
, OMP_CLAUSE_DECL (c
)));
13071 if (s
== error_mark_node
)
13073 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
13076 OMP_CLAUSE_LINEAR_STEP (c
)
13077 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
13078 goto check_dup_generic
;
13081 t
= OMP_CLAUSE_DECL (c
);
13082 check_dup_generic_t
:
13083 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13085 error_at (OMP_CLAUSE_LOCATION (c
),
13086 "%qE is not a variable in clause %qs", t
,
13087 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13090 else if (ort
== C_ORT_ACC
13091 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
)
13093 if (bitmap_bit_p (&oacc_reduction_head
, DECL_UID (t
)))
13095 error ("%qD appears more than once in reduction clauses", t
);
13099 bitmap_set_bit (&oacc_reduction_head
, DECL_UID (t
));
13101 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13102 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
13103 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
13105 error_at (OMP_CLAUSE_LOCATION (c
),
13106 "%qE appears more than once in data clauses", t
);
13109 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
13110 && bitmap_bit_p (&map_head
, DECL_UID (t
)))
13112 if (ort
== C_ORT_ACC
)
13113 error ("%qD appears more than once in data clauses", t
);
13115 error ("%qD appears both in data and map clauses", t
);
13119 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13122 case OMP_CLAUSE_FIRSTPRIVATE
:
13123 t
= OMP_CLAUSE_DECL (c
);
13124 need_complete
= true;
13125 need_implicitly_determined
= true;
13126 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13128 error_at (OMP_CLAUSE_LOCATION (c
),
13129 "%qE is not a variable in clause %<firstprivate%>", t
);
13132 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13133 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13135 error_at (OMP_CLAUSE_LOCATION (c
),
13136 "%qE appears more than once in data clauses", t
);
13139 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13141 if (ort
== C_ORT_ACC
)
13142 error ("%qD appears more than once in data clauses", t
);
13144 error ("%qD appears both in data and map clauses", t
);
13148 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
13151 case OMP_CLAUSE_LASTPRIVATE
:
13152 t
= OMP_CLAUSE_DECL (c
);
13153 need_complete
= true;
13154 need_implicitly_determined
= true;
13155 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13157 error_at (OMP_CLAUSE_LOCATION (c
),
13158 "%qE is not a variable in clause %<lastprivate%>", t
);
13161 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13162 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
13164 error_at (OMP_CLAUSE_LOCATION (c
),
13165 "%qE appears more than once in data clauses", t
);
13169 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
13172 case OMP_CLAUSE_ALIGNED
:
13173 t
= OMP_CLAUSE_DECL (c
);
13174 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13176 error_at (OMP_CLAUSE_LOCATION (c
),
13177 "%qE is not a variable in %<aligned%> clause", t
);
13180 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
13181 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
13183 error_at (OMP_CLAUSE_LOCATION (c
),
13184 "%qE in %<aligned%> clause is neither a pointer nor "
13188 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13190 error_at (OMP_CLAUSE_LOCATION (c
),
13191 "%<_Atomic%> %qD in %<aligned%> clause", t
);
13195 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
13197 error_at (OMP_CLAUSE_LOCATION (c
),
13198 "%qE appears more than once in %<aligned%> clauses",
13203 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
13206 case OMP_CLAUSE_DEPEND
:
13207 t
= OMP_CLAUSE_DECL (c
);
13208 if (t
== NULL_TREE
)
13210 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c
)
13211 == OMP_CLAUSE_DEPEND_SOURCE
);
13214 if (OMP_CLAUSE_DEPEND_KIND (c
) == OMP_CLAUSE_DEPEND_SINK
)
13216 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
13217 for (; t
; t
= TREE_CHAIN (t
))
13219 tree decl
= TREE_VALUE (t
);
13220 if (TREE_CODE (TREE_TYPE (decl
)) == POINTER_TYPE
)
13222 tree offset
= TREE_PURPOSE (t
);
13223 bool neg
= wi::neg_p ((wide_int
) offset
);
13224 offset
= fold_unary (ABS_EXPR
, TREE_TYPE (offset
), offset
);
13225 tree t2
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
),
13226 neg
? MINUS_EXPR
: PLUS_EXPR
,
13228 t2
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
13230 fold_convert (sizetype
, t2
),
13231 fold_convert (sizetype
, decl
));
13232 if (t2
== error_mark_node
)
13237 TREE_PURPOSE (t
) = t2
;
13242 if (TREE_CODE (t
) == TREE_LIST
)
13244 if (handle_omp_array_sections (c
, ort
))
13248 if (t
== error_mark_node
)
13250 else if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13252 error_at (OMP_CLAUSE_LOCATION (c
),
13253 "%qE is not a variable in %<depend%> clause", t
);
13256 else if (!c_mark_addressable (t
))
13260 case OMP_CLAUSE_MAP
:
13261 case OMP_CLAUSE_TO
:
13262 case OMP_CLAUSE_FROM
:
13263 case OMP_CLAUSE__CACHE_
:
13264 t
= OMP_CLAUSE_DECL (c
);
13265 if (TREE_CODE (t
) == TREE_LIST
)
13267 if (handle_omp_array_sections (c
, ort
))
13271 t
= OMP_CLAUSE_DECL (c
);
13272 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13274 error_at (OMP_CLAUSE_LOCATION (c
),
13275 "array section does not have mappable type "
13277 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13280 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13282 error_at (OMP_CLAUSE_LOCATION (c
),
13283 "%<_Atomic%> %qE in %qs clause", t
,
13284 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13287 while (TREE_CODE (t
) == ARRAY_REF
)
13288 t
= TREE_OPERAND (t
, 0);
13289 if (TREE_CODE (t
) == COMPONENT_REF
13290 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
13292 while (TREE_CODE (t
) == COMPONENT_REF
)
13293 t
= TREE_OPERAND (t
, 0);
13294 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
13296 if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13298 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
13299 error ("%qD appears more than once in motion"
13301 else if (ort
== C_ORT_ACC
)
13302 error ("%qD appears more than once in data"
13305 error ("%qD appears more than once in map"
13311 bitmap_set_bit (&map_head
, DECL_UID (t
));
13312 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
13318 if (t
== error_mark_node
)
13323 if (TREE_CODE (t
) == COMPONENT_REF
13324 && (ort
& C_ORT_OMP
)
13325 && OMP_CLAUSE_CODE (c
) != OMP_CLAUSE__CACHE_
)
13327 if (DECL_BIT_FIELD (TREE_OPERAND (t
, 1)))
13329 error_at (OMP_CLAUSE_LOCATION (c
),
13330 "bit-field %qE in %qs clause",
13331 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13334 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13336 error_at (OMP_CLAUSE_LOCATION (c
),
13337 "%qE does not have a mappable type in %qs clause",
13338 t
, omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13341 else if (TYPE_ATOMIC (TREE_TYPE (t
)))
13343 error_at (OMP_CLAUSE_LOCATION (c
),
13344 "%<_Atomic%> %qE in %qs clause", t
,
13345 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13348 while (TREE_CODE (t
) == COMPONENT_REF
)
13350 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0)))
13353 error_at (OMP_CLAUSE_LOCATION (c
),
13354 "%qE is a member of a union", t
);
13358 t
= TREE_OPERAND (t
, 0);
13362 if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
13364 if (bitmap_bit_p (&map_field_head
, DECL_UID (t
)))
13368 if (!VAR_P (t
) && TREE_CODE (t
) != PARM_DECL
)
13370 error_at (OMP_CLAUSE_LOCATION (c
),
13371 "%qE is not a variable in %qs clause", t
,
13372 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13375 else if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
13377 error_at (OMP_CLAUSE_LOCATION (c
),
13378 "%qD is threadprivate variable in %qs clause", t
,
13379 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13382 else if ((OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
13383 || (OMP_CLAUSE_MAP_KIND (c
)
13384 != GOMP_MAP_FIRSTPRIVATE_POINTER
))
13385 && !c_mark_addressable (t
))
13387 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13388 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
13389 || (OMP_CLAUSE_MAP_KIND (c
)
13390 == GOMP_MAP_FIRSTPRIVATE_POINTER
)
13391 || (OMP_CLAUSE_MAP_KIND (c
)
13392 == GOMP_MAP_FORCE_DEVICEPTR
)))
13393 && t
== OMP_CLAUSE_DECL (c
)
13394 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13396 error_at (OMP_CLAUSE_LOCATION (c
),
13397 "%qD does not have a mappable type in %qs clause", t
,
13398 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13401 else if (TREE_TYPE (t
) == error_mark_node
)
13403 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t
))))
13405 error_at (OMP_CLAUSE_LOCATION (c
),
13406 "%<_Atomic%> %qE in %qs clause", t
,
13407 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13410 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
13411 && OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_FIRSTPRIVATE_POINTER
)
13413 if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13414 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13416 error ("%qD appears more than once in data clauses", t
);
13419 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13421 if (ort
== C_ORT_ACC
)
13422 error ("%qD appears more than once in data clauses", t
);
13424 error ("%qD appears both in data and map clauses", t
);
13428 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13430 else if (bitmap_bit_p (&map_head
, DECL_UID (t
)))
13432 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
13433 error ("%qD appears more than once in motion clauses", t
);
13434 else if (ort
== C_ORT_ACC
)
13435 error ("%qD appears more than once in data clauses", t
);
13437 error ("%qD appears more than once in map clauses", t
);
13440 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
13441 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
13443 if (ort
== C_ORT_ACC
)
13444 error ("%qD appears more than once in data clauses", t
);
13446 error ("%qD appears both in data and map clauses", t
);
13451 bitmap_set_bit (&map_head
, DECL_UID (t
));
13452 if (t
!= OMP_CLAUSE_DECL (c
)
13453 && TREE_CODE (OMP_CLAUSE_DECL (c
)) == COMPONENT_REF
)
13454 bitmap_set_bit (&map_field_head
, DECL_UID (t
));
13458 case OMP_CLAUSE_TO_DECLARE
:
13459 case OMP_CLAUSE_LINK
:
13460 t
= OMP_CLAUSE_DECL (c
);
13461 if (TREE_CODE (t
) == FUNCTION_DECL
13462 && OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
13464 else if (!VAR_P (t
))
13466 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_TO_DECLARE
)
13467 error_at (OMP_CLAUSE_LOCATION (c
),
13468 "%qE is neither a variable nor a function name in "
13470 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13472 error_at (OMP_CLAUSE_LOCATION (c
),
13473 "%qE is not a variable in clause %qs", t
,
13474 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13477 else if (DECL_THREAD_LOCAL_P (t
))
13479 error_at (OMP_CLAUSE_LOCATION (c
),
13480 "%qD is threadprivate variable in %qs clause", t
,
13481 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13484 else if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
13486 error_at (OMP_CLAUSE_LOCATION (c
),
13487 "%qD does not have a mappable type in %qs clause", t
,
13488 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13493 if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
13495 error_at (OMP_CLAUSE_LOCATION (c
),
13496 "%qE appears more than once on the same "
13497 "%<declare target%> directive", t
);
13501 bitmap_set_bit (&generic_head
, DECL_UID (t
));
13504 case OMP_CLAUSE_UNIFORM
:
13505 t
= OMP_CLAUSE_DECL (c
);
13506 if (TREE_CODE (t
) != PARM_DECL
)
13509 error_at (OMP_CLAUSE_LOCATION (c
),
13510 "%qD is not an argument in %<uniform%> clause", t
);
13512 error_at (OMP_CLAUSE_LOCATION (c
),
13513 "%qE is not an argument in %<uniform%> clause", t
);
13517 /* map_head bitmap is used as uniform_head if declare_simd. */
13518 bitmap_set_bit (&map_head
, DECL_UID (t
));
13519 goto check_dup_generic
;
13521 case OMP_CLAUSE_IS_DEVICE_PTR
:
13522 case OMP_CLAUSE_USE_DEVICE_PTR
:
13523 t
= OMP_CLAUSE_DECL (c
);
13524 if (TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
13525 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
13527 error_at (OMP_CLAUSE_LOCATION (c
),
13528 "%qs variable is neither a pointer nor an array",
13529 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13532 goto check_dup_generic
;
13534 case OMP_CLAUSE_NOWAIT
:
13535 if (copyprivate_seen
)
13537 error_at (OMP_CLAUSE_LOCATION (c
),
13538 "%<nowait%> clause must not be used together "
13539 "with %<copyprivate%>");
13543 nowait_clause
= pc
;
13544 pc
= &OMP_CLAUSE_CHAIN (c
);
13547 case OMP_CLAUSE_IF
:
13548 case OMP_CLAUSE_NUM_THREADS
:
13549 case OMP_CLAUSE_NUM_TEAMS
:
13550 case OMP_CLAUSE_THREAD_LIMIT
:
13551 case OMP_CLAUSE_DEFAULT
:
13552 case OMP_CLAUSE_UNTIED
:
13553 case OMP_CLAUSE_COLLAPSE
:
13554 case OMP_CLAUSE_FINAL
:
13555 case OMP_CLAUSE_MERGEABLE
:
13556 case OMP_CLAUSE_DEVICE
:
13557 case OMP_CLAUSE_DIST_SCHEDULE
:
13558 case OMP_CLAUSE_PARALLEL
:
13559 case OMP_CLAUSE_FOR
:
13560 case OMP_CLAUSE_SECTIONS
:
13561 case OMP_CLAUSE_TASKGROUP
:
13562 case OMP_CLAUSE_PROC_BIND
:
13563 case OMP_CLAUSE_PRIORITY
:
13564 case OMP_CLAUSE_GRAINSIZE
:
13565 case OMP_CLAUSE_NUM_TASKS
:
13566 case OMP_CLAUSE_NOGROUP
:
13567 case OMP_CLAUSE_THREADS
:
13568 case OMP_CLAUSE_SIMD
:
13569 case OMP_CLAUSE_HINT
:
13570 case OMP_CLAUSE_DEFAULTMAP
:
13571 case OMP_CLAUSE__CILK_FOR_COUNT_
:
13572 case OMP_CLAUSE_NUM_GANGS
:
13573 case OMP_CLAUSE_NUM_WORKERS
:
13574 case OMP_CLAUSE_VECTOR_LENGTH
:
13575 case OMP_CLAUSE_ASYNC
:
13576 case OMP_CLAUSE_WAIT
:
13577 case OMP_CLAUSE_AUTO
:
13578 case OMP_CLAUSE_INDEPENDENT
:
13579 case OMP_CLAUSE_SEQ
:
13580 case OMP_CLAUSE_GANG
:
13581 case OMP_CLAUSE_WORKER
:
13582 case OMP_CLAUSE_VECTOR
:
13583 case OMP_CLAUSE_TILE
:
13584 pc
= &OMP_CLAUSE_CHAIN (c
);
13587 case OMP_CLAUSE_SCHEDULE
:
13588 if (OMP_CLAUSE_SCHEDULE_KIND (c
) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
)
13590 const char *p
= NULL
;
13591 switch (OMP_CLAUSE_SCHEDULE_KIND (c
) & OMP_CLAUSE_SCHEDULE_MASK
)
13593 case OMP_CLAUSE_SCHEDULE_STATIC
: p
= "static"; break;
13594 case OMP_CLAUSE_SCHEDULE_DYNAMIC
: break;
13595 case OMP_CLAUSE_SCHEDULE_GUIDED
: break;
13596 case OMP_CLAUSE_SCHEDULE_AUTO
: p
= "auto"; break;
13597 case OMP_CLAUSE_SCHEDULE_RUNTIME
: p
= "runtime"; break;
13598 default: gcc_unreachable ();
13602 error_at (OMP_CLAUSE_LOCATION (c
),
13603 "%<nonmonotonic%> modifier specified for %qs "
13604 "schedule kind", p
);
13605 OMP_CLAUSE_SCHEDULE_KIND (c
)
13606 = (enum omp_clause_schedule_kind
)
13607 (OMP_CLAUSE_SCHEDULE_KIND (c
)
13608 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
13611 schedule_clause
= c
;
13612 pc
= &OMP_CLAUSE_CHAIN (c
);
13615 case OMP_CLAUSE_ORDERED
:
13616 ordered_seen
= true;
13617 pc
= &OMP_CLAUSE_CHAIN (c
);
13620 case OMP_CLAUSE_SAFELEN
:
13622 pc
= &OMP_CLAUSE_CHAIN (c
);
13624 case OMP_CLAUSE_SIMDLEN
:
13626 pc
= &OMP_CLAUSE_CHAIN (c
);
13629 case OMP_CLAUSE_INBRANCH
:
13630 case OMP_CLAUSE_NOTINBRANCH
:
13633 error_at (OMP_CLAUSE_LOCATION (c
),
13634 "%<inbranch%> clause is incompatible with "
13635 "%<notinbranch%>");
13639 branch_seen
= true;
13640 pc
= &OMP_CLAUSE_CHAIN (c
);
13644 gcc_unreachable ();
13649 t
= OMP_CLAUSE_DECL (c
);
13653 t
= require_complete_type (OMP_CLAUSE_LOCATION (c
), t
);
13654 if (t
== error_mark_node
)
13658 if (need_implicitly_determined
)
13660 const char *share_name
= NULL
;
13662 if (VAR_P (t
) && DECL_THREAD_LOCAL_P (t
))
13663 share_name
= "threadprivate";
13664 else switch (c_omp_predetermined_sharing (t
))
13666 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
13668 case OMP_CLAUSE_DEFAULT_SHARED
:
13669 /* const vars may be specified in firstprivate clause. */
13670 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
13671 && TREE_READONLY (t
))
13673 share_name
= "shared";
13675 case OMP_CLAUSE_DEFAULT_PRIVATE
:
13676 share_name
= "private";
13679 gcc_unreachable ();
13683 error_at (OMP_CLAUSE_LOCATION (c
),
13684 "%qE is predetermined %qs for %qs",
13686 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
13693 *pc
= OMP_CLAUSE_CHAIN (c
);
13695 pc
= &OMP_CLAUSE_CHAIN (c
);
13700 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen
),
13701 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)))
13703 error_at (OMP_CLAUSE_LOCATION (simdlen
),
13704 "%<simdlen%> clause value is bigger than "
13705 "%<safelen%> clause value");
13706 OMP_CLAUSE_SIMDLEN_EXPR (simdlen
)
13707 = OMP_CLAUSE_SAFELEN_EXPR (safelen
);
13712 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
13713 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
13715 error_at (OMP_CLAUSE_LOCATION (schedule_clause
),
13716 "%<nonmonotonic%> schedule modifier specified together "
13717 "with %<ordered%> clause");
13718 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
13719 = (enum omp_clause_schedule_kind
)
13720 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause
)
13721 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC
);
13724 if (linear_variable_step_check
)
13725 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
13727 bool remove
= false;
13728 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
13729 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c
)
13730 && !bitmap_bit_p (&map_head
,
13731 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c
))))
13733 error_at (OMP_CLAUSE_LOCATION (c
),
13734 "%<linear%> clause step is a parameter %qD not "
13735 "specified in %<uniform%> clause",
13736 OMP_CLAUSE_LINEAR_STEP (c
));
13741 *pc
= OMP_CLAUSE_CHAIN (c
);
13743 pc
= &OMP_CLAUSE_CHAIN (c
);
13746 bitmap_obstack_release (NULL
);
13750 /* Return code to initialize DST with a copy constructor from SRC.
13751 C doesn't have copy constructors nor assignment operators, only for
13752 _Atomic vars we need to perform __atomic_load from src into a temporary
13753 followed by __atomic_store of the temporary to dst. */
13756 c_omp_clause_copy_ctor (tree clause
, tree dst
, tree src
)
13758 if (!really_atomic_lvalue (dst
) && !really_atomic_lvalue (src
))
13759 return build2 (MODIFY_EXPR
, TREE_TYPE (dst
), dst
, src
);
13761 location_t loc
= OMP_CLAUSE_LOCATION (clause
);
13762 tree type
= TREE_TYPE (dst
);
13763 tree nonatomic_type
= build_qualified_type (type
, TYPE_UNQUALIFIED
);
13764 tree tmp
= create_tmp_var (nonatomic_type
);
13765 tree tmp_addr
= build_fold_addr_expr (tmp
);
13766 TREE_ADDRESSABLE (tmp
) = 1;
13767 TREE_NO_WARNING (tmp
) = 1;
13768 tree src_addr
= build_fold_addr_expr (src
);
13769 tree dst_addr
= build_fold_addr_expr (dst
);
13770 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
13771 vec
<tree
, va_gc
> *params
;
13772 /* Expansion of a generic atomic load may require an addition
13773 element, so allocate enough to prevent a resize. */
13774 vec_alloc (params
, 4);
13776 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13777 tree fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
13778 params
->quick_push (src_addr
);
13779 params
->quick_push (tmp_addr
);
13780 params
->quick_push (seq_cst
);
13781 tree load
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
13783 vec_alloc (params
, 4);
13785 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13786 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
13787 params
->quick_push (dst_addr
);
13788 params
->quick_push (tmp_addr
);
13789 params
->quick_push (seq_cst
);
13790 tree store
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
13791 return build2 (COMPOUND_EXPR
, void_type_node
, load
, store
);
13794 /* Create a transaction node. */
13797 c_finish_transaction (location_t loc
, tree block
, int flags
)
13799 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
13800 if (flags
& TM_STMT_ATTR_OUTER
)
13801 TRANSACTION_EXPR_OUTER (stmt
) = 1;
13802 if (flags
& TM_STMT_ATTR_RELAXED
)
13803 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
13804 return add_stmt (stmt
);
13807 /* Make a variant type in the proper way for C/C++, propagating qualifiers
13808 down to the element type of an array. If ORIG_QUAL_TYPE is not
13809 NULL, then it should be used as the qualified type
13810 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13811 preserve information about the typedef name from which an array
13812 type was derived). */
13815 c_build_qualified_type (tree type
, int type_quals
, tree orig_qual_type
,
13816 size_t orig_qual_indirect
)
13818 if (type
== error_mark_node
)
13821 if (TREE_CODE (type
) == ARRAY_TYPE
)
13824 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
13825 type_quals
, orig_qual_type
,
13826 orig_qual_indirect
- 1);
13828 /* See if we already have an identically qualified type. */
13829 if (orig_qual_type
&& orig_qual_indirect
== 0)
13830 t
= orig_qual_type
;
13832 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
13834 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
13835 && TYPE_NAME (t
) == TYPE_NAME (type
)
13836 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
13837 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
13838 TYPE_ATTRIBUTES (type
)))
13843 tree domain
= TYPE_DOMAIN (type
);
13845 t
= build_variant_type_copy (type
);
13846 TREE_TYPE (t
) = element_type
;
13848 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
13849 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
13850 SET_TYPE_STRUCTURAL_EQUALITY (t
);
13851 else if (TYPE_CANONICAL (element_type
) != element_type
13852 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
13854 tree unqualified_canon
13855 = build_array_type (TYPE_CANONICAL (element_type
),
13856 domain
? TYPE_CANONICAL (domain
)
13858 if (TYPE_REVERSE_STORAGE_ORDER (type
))
13861 = build_distinct_type_copy (unqualified_canon
);
13862 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon
) = 1;
13865 = c_build_qualified_type (unqualified_canon
, type_quals
);
13868 TYPE_CANONICAL (t
) = t
;
13873 /* A restrict-qualified pointer type must be a pointer to object or
13874 incomplete type. Note that the use of POINTER_TYPE_P also allows
13875 REFERENCE_TYPEs, which is appropriate for C++. */
13876 if ((type_quals
& TYPE_QUAL_RESTRICT
)
13877 && (!POINTER_TYPE_P (type
)
13878 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
13880 error ("invalid use of %<restrict%>");
13881 type_quals
&= ~TYPE_QUAL_RESTRICT
;
13884 tree var_type
= (orig_qual_type
&& orig_qual_indirect
== 0
13886 : build_qualified_type (type
, type_quals
));
13887 /* A variant type does not inherit the list of incomplete vars from the
13888 type main variant. */
13889 if (RECORD_OR_UNION_TYPE_P (var_type
)
13890 && TYPE_MAIN_VARIANT (var_type
) != var_type
)
13891 C_TYPE_INCOMPLETE_VARS (var_type
) = 0;
13895 /* Build a VA_ARG_EXPR for the C parser. */
13898 c_build_va_arg (location_t loc1
, tree expr
, location_t loc2
, tree type
)
13900 if (error_operand_p (type
))
13901 return error_mark_node
;
13902 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13903 order because it takes the address of the expression. */
13904 else if (handled_component_p (expr
)
13905 && reverse_storage_order_for_component_p (expr
))
13907 error_at (loc1
, "cannot use %<va_arg%> with reverse storage order");
13908 return error_mark_node
;
13910 else if (!COMPLETE_TYPE_P (type
))
13912 error_at (loc2
, "second argument to %<va_arg%> is of incomplete "
13914 return error_mark_node
;
13916 else if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
13917 warning_at (loc2
, OPT_Wc___compat
,
13918 "C++ requires promoted type, not enum type, in %<va_arg%>");
13919 return build_va_arg (loc2
, expr
, type
);
13922 /* Return truthvalue of whether T1 is the same tree structure as T2.
13923 Return 1 if they are the same. Return 0 if they are different. */
13926 c_tree_equal (tree t1
, tree t2
)
13928 enum tree_code code1
, code2
;
13935 for (code1
= TREE_CODE (t1
);
13936 CONVERT_EXPR_CODE_P (code1
)
13937 || code1
== NON_LVALUE_EXPR
;
13938 code1
= TREE_CODE (t1
))
13939 t1
= TREE_OPERAND (t1
, 0);
13940 for (code2
= TREE_CODE (t2
);
13941 CONVERT_EXPR_CODE_P (code2
)
13942 || code2
== NON_LVALUE_EXPR
;
13943 code2
= TREE_CODE (t2
))
13944 t2
= TREE_OPERAND (t2
, 0);
13946 /* They might have become equal now. */
13950 if (code1
!= code2
)
13956 return wi::eq_p (t1
, t2
);
13959 return real_equal (&TREE_REAL_CST (t1
), &TREE_REAL_CST (t2
));
13962 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
13963 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
13964 TREE_STRING_LENGTH (t1
));
13967 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
13968 TREE_FIXED_CST (t2
));
13971 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
13972 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
13975 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
13978 /* We need to do this when determining whether or not two
13979 non-type pointer to member function template arguments
13981 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
13982 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
13987 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
13989 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
13990 if (!c_tree_equal (field
, elt2
->index
)
13991 || !c_tree_equal (value
, elt2
->value
))
13998 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
14000 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
14002 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
14005 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
14010 call_expr_arg_iterator iter1
, iter2
;
14011 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
14013 for (arg1
= first_call_expr_arg (t1
, &iter1
),
14014 arg2
= first_call_expr_arg (t2
, &iter2
);
14016 arg1
= next_call_expr_arg (&iter1
),
14017 arg2
= next_call_expr_arg (&iter2
))
14018 if (!c_tree_equal (arg1
, arg2
))
14027 tree o1
= TREE_OPERAND (t1
, 0);
14028 tree o2
= TREE_OPERAND (t2
, 0);
14030 /* Special case: if either target is an unallocated VAR_DECL,
14031 it means that it's going to be unified with whatever the
14032 TARGET_EXPR is really supposed to initialize, so treat it
14033 as being equivalent to anything. */
14034 if (VAR_P (o1
) && DECL_NAME (o1
) == NULL_TREE
14035 && !DECL_RTL_SET_P (o1
))
14037 else if (VAR_P (o2
) && DECL_NAME (o2
) == NULL_TREE
14038 && !DECL_RTL_SET_P (o2
))
14040 else if (!c_tree_equal (o1
, o2
))
14043 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
14046 case COMPONENT_REF
:
14047 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
14049 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
14055 case FUNCTION_DECL
:
14056 case IDENTIFIER_NODE
:
14063 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
14065 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
14066 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
14067 TREE_VEC_ELT (t2
, ix
)))
14076 switch (TREE_CODE_CLASS (code1
))
14080 case tcc_comparison
:
14081 case tcc_expression
:
14083 case tcc_reference
:
14084 case tcc_statement
:
14086 int i
, n
= TREE_OPERAND_LENGTH (t1
);
14090 case PREINCREMENT_EXPR
:
14091 case PREDECREMENT_EXPR
:
14092 case POSTINCREMENT_EXPR
:
14093 case POSTDECREMENT_EXPR
:
14103 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
14104 && n
!= TREE_OPERAND_LENGTH (t2
))
14107 for (i
= 0; i
< n
; ++i
)
14108 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
14115 return comptypes (t1
, t2
);
14117 gcc_unreachable ();
14119 /* We can get here with --disable-checking. */
14123 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14124 spawn-helper and BODY is the newly created body for FNDECL. */
14127 cilk_install_body_with_frame_cleanup (tree fndecl
, tree body
, void *w
)
14129 tree list
= alloc_stmt_list ();
14130 tree frame
= make_cilk_frame (fndecl
);
14131 tree dtor
= create_cilk_function_exit (frame
, false, true);
14132 add_local_decl (cfun
, frame
);
14134 DECL_SAVED_TREE (fndecl
) = list
;
14135 tree frame_ptr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (frame
)),
14137 tree body_list
= cilk_install_body_pedigree_operations (frame_ptr
);
14138 gcc_assert (TREE_CODE (body_list
) == STATEMENT_LIST
);
14140 tree detach_expr
= build_call_expr (cilk_detach_fndecl
, 1, frame_ptr
);
14141 append_to_statement_list (detach_expr
, &body_list
);
14143 cilk_outline (fndecl
, &body
, (struct wrapper_data
*) w
);
14144 body
= fold_build_cleanup_point_expr (void_type_node
, body
);
14146 append_to_statement_list (body
, &body_list
);
14147 append_to_statement_list (build_stmt (EXPR_LOCATION (body
), TRY_FINALLY_EXPR
,
14148 body_list
, dtor
), &list
);
14151 /* Returns true when the function declaration FNDECL is implicit,
14152 introduced as a result of a call to an otherwise undeclared
14153 function, and false otherwise. */
14156 c_decl_implicit (const_tree fndecl
)
14158 return C_DECL_IMPLICIT (fndecl
);