1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 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"
35 #include "double-int.h"
39 #include "fixed-value.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
43 #include "trans-mem.h"
46 #include "langhooks.h"
52 #include "tree-iterator.h"
59 #include "hard-reg-set.h"
62 #include "gimple-expr.h"
64 #include "tree-inline.h"
66 #include "c-family/c-objc.h"
67 #include "c-family/c-common.h"
68 #include "c-family/c-ubsan.h"
71 #include "gomp-constants.h"
73 /* Possible cases of implicit bad conversions. Used to select
74 diagnostic messages in convert_for_assignment. */
82 /* The level of nesting inside "__alignof__". */
85 /* The level of nesting inside "sizeof". */
88 /* The level of nesting inside "typeof". */
91 /* The argument of last parsed sizeof expression, only to be tested
92 if expr.original_code == SIZEOF_EXPR. */
93 tree c_last_sizeof_arg
;
95 /* Nonzero if we might need to print a "missing braces around
96 initializer" message within this initializer. */
97 static int found_missing_braces
;
99 static int require_constant_value
;
100 static int require_constant_elements
;
102 static bool null_pointer_constant_p (const_tree
);
103 static tree
qualify_type (tree
, tree
);
104 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
106 static int comp_target_types (location_t
, tree
, tree
);
107 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
109 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
110 static tree
lookup_field (tree
, tree
);
111 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
112 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
114 static tree
pointer_diff (location_t
, tree
, tree
);
115 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
116 enum impl_conv
, bool, tree
, tree
, int);
117 static tree
valid_compound_expr_initializer (tree
, tree
);
118 static void push_string (const char *);
119 static void push_member_name (tree
);
120 static int spelling_length (void);
121 static char *print_spelling (char *);
122 static void warning_init (location_t
, int, const char *);
123 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
124 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, int,
125 bool, struct obstack
*);
126 static void output_pending_init_elements (int, struct obstack
*);
127 static int set_designator (location_t
, int, struct obstack
*);
128 static void push_range_stack (tree
, struct obstack
*);
129 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
131 static void set_nonincremental_init (struct obstack
*);
132 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
133 static tree
find_init_member (tree
, struct obstack
*);
134 static void readonly_warning (tree
, enum lvalue_use
);
135 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
136 static void record_maybe_used_decl (tree
);
137 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
139 /* Return true if EXP is a null pointer constant, false otherwise. */
142 null_pointer_constant_p (const_tree expr
)
144 /* This should really operate on c_expr structures, but they aren't
145 yet available everywhere required. */
146 tree type
= TREE_TYPE (expr
);
147 return (TREE_CODE (expr
) == INTEGER_CST
148 && !TREE_OVERFLOW (expr
)
149 && integer_zerop (expr
)
150 && (INTEGRAL_TYPE_P (type
)
151 || (TREE_CODE (type
) == POINTER_TYPE
152 && VOID_TYPE_P (TREE_TYPE (type
))
153 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
156 /* EXPR may appear in an unevaluated part of an integer constant
157 expression, but not in an evaluated part. Wrap it in a
158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
162 note_integer_operands (tree expr
)
165 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
167 ret
= copy_node (expr
);
168 TREE_OVERFLOW (ret
) = 1;
172 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
178 /* Having checked whether EXPR may appear in an unevaluated part of an
179 integer constant expression and found that it may, remove any
180 C_MAYBE_CONST_EXPR noting this fact and return the resulting
184 remove_c_maybe_const_expr (tree expr
)
186 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
187 return C_MAYBE_CONST_EXPR_EXPR (expr
);
192 \f/* This is a cache to hold if two types are compatible or not. */
194 struct tagged_tu_seen_cache
{
195 const struct tagged_tu_seen_cache
* next
;
198 /* The return value of tagged_types_tu_compatible_p if we had seen
199 these two types already. */
203 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
204 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
206 /* Do `exp = require_complete_type (exp);' to make sure exp
207 does not have an incomplete type. (That includes void types.) */
210 require_complete_type (tree value
)
212 tree type
= TREE_TYPE (value
);
214 if (error_operand_p (value
))
215 return error_mark_node
;
217 /* First, detect a valid value with a complete type. */
218 if (COMPLETE_TYPE_P (type
))
221 c_incomplete_type_error (value
, type
);
222 return error_mark_node
;
225 /* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. */
230 c_incomplete_type_error (const_tree value
, const_tree type
)
232 /* Avoid duplicate error message. */
233 if (TREE_CODE (type
) == ERROR_MARK
)
236 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
237 || TREE_CODE (value
) == PARM_DECL
))
238 error ("%qD has an incomplete type %qT", value
, type
);
242 /* We must print an error message. Be clever about what it says. */
244 switch (TREE_CODE (type
))
252 error ("invalid use of void expression");
256 if (TYPE_DOMAIN (type
))
258 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
260 error ("invalid use of flexible array member");
263 type
= TREE_TYPE (type
);
266 error ("invalid use of array with unspecified bounds");
273 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
274 error ("invalid use of undefined type %qT", type
);
276 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
277 error ("invalid use of incomplete typedef %qT", type
);
281 /* Given a type, apply default promotions wrt unnamed function
282 arguments and return the new type. */
285 c_type_promotes_to (tree type
)
287 tree ret
= NULL_TREE
;
289 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
290 ret
= double_type_node
;
291 else if (c_promoting_integer_type_p (type
))
293 /* Preserve unsignedness if not really getting any wider. */
294 if (TYPE_UNSIGNED (type
)
295 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
296 ret
= unsigned_type_node
;
298 ret
= integer_type_node
;
301 if (ret
!= NULL_TREE
)
302 return (TYPE_ATOMIC (type
)
303 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
309 /* Return true if between two named address spaces, whether there is a superset
310 named address space that encompasses both address spaces. If there is a
311 superset, return which address space is the superset. */
314 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
321 else if (targetm
.addr_space
.subset_p (as1
, as2
))
326 else if (targetm
.addr_space
.subset_p (as2
, as1
))
335 /* Return a variant of TYPE which has all the type qualifiers of LIKE
336 as well as those of TYPE. */
339 qualify_type (tree type
, tree like
)
341 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
342 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
343 addr_space_t as_common
;
345 /* If the two named address spaces are different, determine the common
346 superset address space. If there isn't one, raise an error. */
347 if (!addr_space_superset (as_type
, as_like
, &as_common
))
350 error ("%qT and %qT are in disjoint named address spaces",
354 return c_build_qualified_type (type
,
355 TYPE_QUALS_NO_ADDR_SPACE (type
)
356 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
357 | ENCODE_QUAL_ADDR_SPACE (as_common
));
360 /* Return true iff the given tree T is a variable length array. */
363 c_vla_type_p (const_tree t
)
365 if (TREE_CODE (t
) == ARRAY_TYPE
366 && C_TYPE_VARIABLE_SIZE (t
))
371 /* Return the composite type of two compatible types.
373 We assume that comptypes has already been done and returned
374 nonzero; if that isn't so, this may crash. In particular, we
375 assume that qualifiers match. */
378 composite_type (tree t1
, tree t2
)
380 enum tree_code code1
;
381 enum tree_code code2
;
384 /* Save time if the two types are the same. */
386 if (t1
== t2
) return t1
;
388 /* If one type is nonsense, use the other. */
389 if (t1
== error_mark_node
)
391 if (t2
== error_mark_node
)
394 code1
= TREE_CODE (t1
);
395 code2
= TREE_CODE (t2
);
397 /* Merge the attributes. */
398 attributes
= targetm
.merge_type_attributes (t1
, t2
);
400 /* If one is an enumerated type and the other is the compatible
401 integer type, the composite type might be either of the two
402 (DR#013 question 3). For consistency, use the enumerated type as
403 the composite type. */
405 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
407 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
410 gcc_assert (code1
== code2
);
415 /* For two pointers, do this recursively on the target type. */
417 tree pointed_to_1
= TREE_TYPE (t1
);
418 tree pointed_to_2
= TREE_TYPE (t2
);
419 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
420 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
421 t1
= build_type_attribute_variant (t1
, attributes
);
422 return qualify_type (t1
, t2
);
427 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
430 tree d1
= TYPE_DOMAIN (t1
);
431 tree d2
= TYPE_DOMAIN (t2
);
432 bool d1_variable
, d2_variable
;
433 bool d1_zero
, d2_zero
;
434 bool t1_complete
, t2_complete
;
436 /* We should not have any type quals on arrays at all. */
437 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
438 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
440 t1_complete
= COMPLETE_TYPE_P (t1
);
441 t2_complete
= COMPLETE_TYPE_P (t2
);
443 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
444 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
446 d1_variable
= (!d1_zero
447 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
448 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
449 d2_variable
= (!d2_zero
450 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
451 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
452 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
453 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
455 /* Save space: see if the result is identical to one of the args. */
456 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
457 && (d2_variable
|| d2_zero
|| !d1_variable
))
458 return build_type_attribute_variant (t1
, attributes
);
459 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
460 && (d1_variable
|| d1_zero
|| !d2_variable
))
461 return build_type_attribute_variant (t2
, attributes
);
463 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
464 return build_type_attribute_variant (t1
, attributes
);
465 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
466 return build_type_attribute_variant (t2
, attributes
);
468 /* Merge the element types, and have a size if either arg has
469 one. We may have qualifiers on the element types. To set
470 up TYPE_MAIN_VARIANT correctly, we need to form the
471 composite of the unqualified types and add the qualifiers
473 quals
= TYPE_QUALS (strip_array_types (elt
));
474 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
475 t1
= build_array_type (unqual_elt
,
476 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
482 /* Ensure a composite type involving a zero-length array type
483 is a zero-length type not an incomplete type. */
484 if (d1_zero
&& d2_zero
485 && (t1_complete
|| t2_complete
)
486 && !COMPLETE_TYPE_P (t1
))
488 TYPE_SIZE (t1
) = bitsize_zero_node
;
489 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
491 t1
= c_build_qualified_type (t1
, quals
);
492 return build_type_attribute_variant (t1
, attributes
);
498 if (attributes
!= NULL
)
500 /* Try harder not to create a new aggregate type. */
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
503 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
506 return build_type_attribute_variant (t1
, attributes
);
509 /* Function types: prefer the one that specified arg types.
510 If both do, merge the arg types. Also merge the return types. */
512 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
513 tree p1
= TYPE_ARG_TYPES (t1
);
514 tree p2
= TYPE_ARG_TYPES (t2
);
519 /* Save space: see if the result is identical to one of the args. */
520 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
521 return build_type_attribute_variant (t1
, attributes
);
522 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
523 return build_type_attribute_variant (t2
, attributes
);
525 /* Simple way if one arg fails to specify argument types. */
526 if (TYPE_ARG_TYPES (t1
) == 0)
528 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
529 t1
= build_type_attribute_variant (t1
, attributes
);
530 return qualify_type (t1
, t2
);
532 if (TYPE_ARG_TYPES (t2
) == 0)
534 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
535 t1
= build_type_attribute_variant (t1
, attributes
);
536 return qualify_type (t1
, t2
);
539 /* If both args specify argument types, we must merge the two
540 lists, argument by argument. */
542 len
= list_length (p1
);
545 for (i
= 0; i
< len
; i
++)
546 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
551 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
553 /* A null type means arg type is not specified.
554 Take whatever the other function type has. */
555 if (TREE_VALUE (p1
) == 0)
557 TREE_VALUE (n
) = TREE_VALUE (p2
);
560 if (TREE_VALUE (p2
) == 0)
562 TREE_VALUE (n
) = TREE_VALUE (p1
);
566 /* Given wait (union {union wait *u; int *i} *)
567 and wait (union wait *),
568 prefer union wait * as type of parm. */
569 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
570 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
573 tree mv2
= TREE_VALUE (p2
);
574 if (mv2
&& mv2
!= error_mark_node
575 && TREE_CODE (mv2
) != ARRAY_TYPE
)
576 mv2
= TYPE_MAIN_VARIANT (mv2
);
577 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
578 memb
; memb
= DECL_CHAIN (memb
))
580 tree mv3
= TREE_TYPE (memb
);
581 if (mv3
&& mv3
!= error_mark_node
582 && TREE_CODE (mv3
) != ARRAY_TYPE
)
583 mv3
= TYPE_MAIN_VARIANT (mv3
);
584 if (comptypes (mv3
, mv2
))
586 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
588 pedwarn (input_location
, OPT_Wpedantic
,
589 "function types not truly compatible in ISO C");
594 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
595 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
598 tree mv1
= TREE_VALUE (p1
);
599 if (mv1
&& mv1
!= error_mark_node
600 && TREE_CODE (mv1
) != ARRAY_TYPE
)
601 mv1
= TYPE_MAIN_VARIANT (mv1
);
602 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
603 memb
; memb
= DECL_CHAIN (memb
))
605 tree mv3
= TREE_TYPE (memb
);
606 if (mv3
&& mv3
!= error_mark_node
607 && TREE_CODE (mv3
) != ARRAY_TYPE
)
608 mv3
= TYPE_MAIN_VARIANT (mv3
);
609 if (comptypes (mv3
, mv1
))
611 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
613 pedwarn (input_location
, OPT_Wpedantic
,
614 "function types not truly compatible in ISO C");
619 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
623 t1
= build_function_type (valtype
, newargs
);
624 t1
= qualify_type (t1
, t2
);
625 /* ... falls through ... */
629 return build_type_attribute_variant (t1
, attributes
);
634 /* Return the type of a conditional expression between pointers to
635 possibly differently qualified versions of compatible types.
637 We assume that comp_target_types has already been done and returned
638 nonzero; if that isn't so, this may crash. */
641 common_pointer_type (tree t1
, tree t2
)
644 tree pointed_to_1
, mv1
;
645 tree pointed_to_2
, mv2
;
647 unsigned target_quals
;
648 addr_space_t as1
, as2
, as_common
;
651 /* Save time if the two types are the same. */
653 if (t1
== t2
) return t1
;
655 /* If one type is nonsense, use the other. */
656 if (t1
== error_mark_node
)
658 if (t2
== error_mark_node
)
661 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
662 && TREE_CODE (t2
) == POINTER_TYPE
);
664 /* Merge the attributes. */
665 attributes
= targetm
.merge_type_attributes (t1
, t2
);
667 /* Find the composite type of the target types, and combine the
668 qualifiers of the two types' targets. Do not lose qualifiers on
669 array element types by taking the TYPE_MAIN_VARIANT. */
670 mv1
= pointed_to_1
= TREE_TYPE (t1
);
671 mv2
= pointed_to_2
= TREE_TYPE (t2
);
672 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
673 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
674 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
675 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
676 target
= composite_type (mv1
, mv2
);
678 /* Strip array types to get correct qualifier for pointers to arrays */
679 quals1
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1
));
680 quals2
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2
));
682 /* For function types do not merge const qualifiers, but drop them
683 if used inconsistently. The middle-end uses these to mark const
684 and noreturn functions. */
685 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
686 target_quals
= (quals1
& quals2
);
688 target_quals
= (quals1
| quals2
);
690 /* If the two named address spaces are different, determine the common
691 superset address space. This is guaranteed to exist due to the
692 assumption that comp_target_type returned non-zero. */
693 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
694 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
695 if (!addr_space_superset (as1
, as2
, &as_common
))
698 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
700 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
701 return build_type_attribute_variant (t1
, attributes
);
704 /* Return the common type for two arithmetic types under the usual
705 arithmetic conversions. The default conversions have already been
706 applied, and enumerated types converted to their compatible integer
707 types. The resulting type is unqualified and has no attributes.
709 This is the type for the result of most arithmetic operations
710 if the operands have the given two types. */
713 c_common_type (tree t1
, tree t2
)
715 enum tree_code code1
;
716 enum tree_code code2
;
718 /* If one type is nonsense, use the other. */
719 if (t1
== error_mark_node
)
721 if (t2
== error_mark_node
)
724 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
725 t1
= TYPE_MAIN_VARIANT (t1
);
727 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
728 t2
= TYPE_MAIN_VARIANT (t2
);
730 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
731 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
733 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
734 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
736 /* Save time if the two types are the same. */
738 if (t1
== t2
) return t1
;
740 code1
= TREE_CODE (t1
);
741 code2
= TREE_CODE (t2
);
743 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
744 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
745 || code1
== INTEGER_TYPE
);
746 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
747 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
748 || code2
== INTEGER_TYPE
);
750 /* When one operand is a decimal float type, the other operand cannot be
751 a generic float type or a complex type. We also disallow vector types
753 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
754 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
756 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
758 error ("can%'t mix operands of decimal float and vector types");
759 return error_mark_node
;
761 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
763 error ("can%'t mix operands of decimal float and complex types");
764 return error_mark_node
;
766 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
768 error ("can%'t mix operands of decimal float and other float types");
769 return error_mark_node
;
773 /* If one type is a vector type, return that type. (How the usual
774 arithmetic conversions apply to the vector types extension is not
775 precisely specified.) */
776 if (code1
== VECTOR_TYPE
)
779 if (code2
== VECTOR_TYPE
)
782 /* If one type is complex, form the common type of the non-complex
783 components, then make that complex. Use T1 or T2 if it is the
785 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
787 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
788 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
789 tree subtype
= c_common_type (subtype1
, subtype2
);
791 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
793 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
796 return build_complex_type (subtype
);
799 /* If only one is real, use it as the result. */
801 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
804 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
807 /* If both are real and either are decimal floating point types, use
808 the decimal floating point type with the greater precision. */
810 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
812 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
813 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
814 return dfloat128_type_node
;
815 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
816 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
817 return dfloat64_type_node
;
818 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
819 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
820 return dfloat32_type_node
;
823 /* Deal with fixed-point types. */
824 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
826 unsigned int unsignedp
= 0, satp
= 0;
828 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
833 /* If one input type is saturating, the result type is saturating. */
834 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
837 /* If both fixed-point types are unsigned, the result type is unsigned.
838 When mixing fixed-point and integer types, follow the sign of the
840 Otherwise, the result type is signed. */
841 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
842 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
843 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
844 && TYPE_UNSIGNED (t1
))
845 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
846 && TYPE_UNSIGNED (t2
)))
849 /* The result type is signed. */
852 /* If the input type is unsigned, we need to convert to the
854 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
856 enum mode_class mclass
= (enum mode_class
) 0;
857 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
859 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
863 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
865 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
867 enum mode_class mclass
= (enum mode_class
) 0;
868 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
870 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
874 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
878 if (code1
== FIXED_POINT_TYPE
)
880 fbit1
= GET_MODE_FBIT (m1
);
881 ibit1
= GET_MODE_IBIT (m1
);
886 /* Signed integers need to subtract one sign bit. */
887 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
890 if (code2
== FIXED_POINT_TYPE
)
892 fbit2
= GET_MODE_FBIT (m2
);
893 ibit2
= GET_MODE_IBIT (m2
);
898 /* Signed integers need to subtract one sign bit. */
899 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
902 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
903 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
904 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
908 /* Both real or both integers; use the one with greater precision. */
910 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
912 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
915 /* Same precision. Prefer long longs to longs to ints when the
916 same precision, following the C99 rules on integer type rank
917 (which are equivalent to the C90 rules for C90 types). */
919 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
920 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
921 return long_long_unsigned_type_node
;
923 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
924 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
926 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
927 return long_long_unsigned_type_node
;
929 return long_long_integer_type_node
;
932 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
933 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
934 return long_unsigned_type_node
;
936 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
937 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
939 /* But preserve unsignedness from the other type,
940 since long cannot hold all the values of an unsigned int. */
941 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
942 return long_unsigned_type_node
;
944 return long_integer_type_node
;
947 /* Likewise, prefer long double to double even if same size. */
948 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
949 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
950 return long_double_type_node
;
952 /* Likewise, prefer double to float even if same size.
953 We got a couple of embedded targets with 32 bit doubles, and the
954 pdp11 might have 64 bit floats. */
955 if (TYPE_MAIN_VARIANT (t1
) == double_type_node
956 || TYPE_MAIN_VARIANT (t2
) == double_type_node
)
957 return double_type_node
;
959 /* Otherwise prefer the unsigned one. */
961 if (TYPE_UNSIGNED (t1
))
967 /* Wrapper around c_common_type that is used by c-common.c and other
968 front end optimizations that remove promotions. ENUMERAL_TYPEs
969 are allowed here and are converted to their compatible integer types.
970 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
971 preferably a non-Boolean type as the common type. */
973 common_type (tree t1
, tree t2
)
975 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
976 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
977 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
978 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
980 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
981 if (TREE_CODE (t1
) == BOOLEAN_TYPE
982 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
983 return boolean_type_node
;
985 /* If either type is BOOLEAN_TYPE, then return the other. */
986 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
988 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
991 return c_common_type (t1
, t2
);
994 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
995 or various other operations. Return 2 if they are compatible
996 but a warning may be needed if you use them together. */
999 comptypes (tree type1
, tree type2
)
1001 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1004 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1010 /* Like comptypes, but if it returns non-zero because enum and int are
1011 compatible, it sets *ENUM_AND_INT_P to true. */
1014 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1016 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1019 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1020 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1025 /* Like comptypes, but if it returns nonzero for different types, it
1026 sets *DIFFERENT_TYPES_P to true. */
1029 comptypes_check_different_types (tree type1
, tree type2
,
1030 bool *different_types_p
)
1032 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1035 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1036 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1041 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1042 or various other operations. Return 2 if they are compatible
1043 but a warning may be needed if you use them together. If
1044 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1045 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1046 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1047 NULL, and the types are compatible but different enough not to be
1048 permitted in C11 typedef redeclarations, then this sets
1049 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1050 false, but may or may not be set if the types are incompatible.
1051 This differs from comptypes, in that we don't free the seen
1055 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1056 bool *different_types_p
)
1058 const_tree t1
= type1
;
1059 const_tree t2
= type2
;
1062 /* Suppress errors caused by previously reported errors. */
1064 if (t1
== t2
|| !t1
|| !t2
1065 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1068 /* Enumerated types are compatible with integer types, but this is
1069 not transitive: two enumerated types in the same translation unit
1070 are compatible with each other only if they are the same type. */
1072 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1074 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1075 if (TREE_CODE (t2
) != VOID_TYPE
)
1077 if (enum_and_int_p
!= NULL
)
1078 *enum_and_int_p
= true;
1079 if (different_types_p
!= NULL
)
1080 *different_types_p
= true;
1083 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1085 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1086 if (TREE_CODE (t1
) != VOID_TYPE
)
1088 if (enum_and_int_p
!= NULL
)
1089 *enum_and_int_p
= true;
1090 if (different_types_p
!= NULL
)
1091 *different_types_p
= true;
1098 /* Different classes of types can't be compatible. */
1100 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1103 /* Qualifiers must match. C99 6.7.3p9 */
1105 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1108 /* Allow for two different type nodes which have essentially the same
1109 definition. Note that we already checked for equality of the type
1110 qualifiers (just above). */
1112 if (TREE_CODE (t1
) != ARRAY_TYPE
1113 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1116 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1117 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1120 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1123 switch (TREE_CODE (t1
))
1126 /* Do not remove mode or aliasing information. */
1127 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1128 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1130 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1131 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1132 enum_and_int_p
, different_types_p
));
1136 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1142 tree d1
= TYPE_DOMAIN (t1
);
1143 tree d2
= TYPE_DOMAIN (t2
);
1144 bool d1_variable
, d2_variable
;
1145 bool d1_zero
, d2_zero
;
1148 /* Target types must match incl. qualifiers. */
1149 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1150 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1152 different_types_p
)))
1155 if (different_types_p
!= NULL
1156 && (d1
== 0) != (d2
== 0))
1157 *different_types_p
= true;
1158 /* Sizes must match unless one is missing or variable. */
1159 if (d1
== 0 || d2
== 0 || d1
== d2
)
1162 d1_zero
= !TYPE_MAX_VALUE (d1
);
1163 d2_zero
= !TYPE_MAX_VALUE (d2
);
1165 d1_variable
= (!d1_zero
1166 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1167 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1168 d2_variable
= (!d2_zero
1169 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1170 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1171 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1172 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1174 if (different_types_p
!= NULL
1175 && d1_variable
!= d2_variable
)
1176 *different_types_p
= true;
1177 if (d1_variable
|| d2_variable
)
1179 if (d1_zero
&& d2_zero
)
1181 if (d1_zero
|| d2_zero
1182 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1183 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1192 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1194 tree a1
= TYPE_ATTRIBUTES (t1
);
1195 tree a2
= TYPE_ATTRIBUTES (t2
);
1197 if (! attribute_list_contained (a1
, a2
)
1198 && ! attribute_list_contained (a2
, a1
))
1202 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1204 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1210 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1211 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1212 enum_and_int_p
, different_types_p
));
1218 return attrval
== 2 && val
== 1 ? 2 : val
;
1221 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1222 their qualifiers, except for named address spaces. If the pointers point to
1223 different named addresses, then we must determine if one address space is a
1224 subset of the other. */
1227 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1231 tree mvl
= TREE_TYPE (ttl
);
1232 tree mvr
= TREE_TYPE (ttr
);
1233 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1234 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1235 addr_space_t as_common
;
1236 bool enum_and_int_p
;
1238 /* Fail if pointers point to incompatible address spaces. */
1239 if (!addr_space_superset (asl
, asr
, &as_common
))
1242 /* For pedantic record result of comptypes on arrays before losing
1243 qualifiers on the element type below. */
1246 if (TREE_CODE (mvl
) == ARRAY_TYPE
1247 && TREE_CODE (mvr
) == ARRAY_TYPE
)
1248 val_ped
= comptypes (mvl
, mvr
);
1250 /* Qualifiers on element types of array types that are
1251 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1253 mvl
= (TYPE_ATOMIC (strip_array_types (mvl
))
1254 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1255 : TYPE_MAIN_VARIANT (mvl
));
1257 mvr
= (TYPE_ATOMIC (strip_array_types (mvr
))
1258 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1259 : TYPE_MAIN_VARIANT (mvr
));
1261 enum_and_int_p
= false;
1262 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1264 if (val
== 1 && val_ped
!= 1)
1265 pedwarn (location
, OPT_Wpedantic
, "pointers to arrays with different qualifiers "
1266 "are incompatible in ISO C");
1269 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1271 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1272 warning_at (location
, OPT_Wc___compat
,
1273 "pointer target types incompatible in C++");
1278 /* Subroutines of `comptypes'. */
1280 /* Determine whether two trees derive from the same translation unit.
1281 If the CONTEXT chain ends in a null, that tree's context is still
1282 being parsed, so if two trees have context chains ending in null,
1283 they're in the same translation unit. */
1285 same_translation_unit_p (const_tree t1
, const_tree t2
)
1287 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1288 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1290 case tcc_declaration
:
1291 t1
= DECL_CONTEXT (t1
); break;
1293 t1
= TYPE_CONTEXT (t1
); break;
1294 case tcc_exceptional
:
1295 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1296 default: gcc_unreachable ();
1299 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1300 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1302 case tcc_declaration
:
1303 t2
= DECL_CONTEXT (t2
); break;
1305 t2
= TYPE_CONTEXT (t2
); break;
1306 case tcc_exceptional
:
1307 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1308 default: gcc_unreachable ();
1314 /* Allocate the seen two types, assuming that they are compatible. */
1316 static struct tagged_tu_seen_cache
*
1317 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1319 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1320 tu
->next
= tagged_tu_seen_base
;
1324 tagged_tu_seen_base
= tu
;
1326 /* The C standard says that two structures in different translation
1327 units are compatible with each other only if the types of their
1328 fields are compatible (among other things). We assume that they
1329 are compatible until proven otherwise when building the cache.
1330 An example where this can occur is:
1335 If we are comparing this against a similar struct in another TU,
1336 and did not assume they were compatible, we end up with an infinite
1342 /* Free the seen types until we get to TU_TIL. */
1345 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1347 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1348 while (tu
!= tu_til
)
1350 const struct tagged_tu_seen_cache
*const tu1
1351 = (const struct tagged_tu_seen_cache
*) tu
;
1353 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1355 tagged_tu_seen_base
= tu_til
;
1358 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1359 compatible. If the two types are not the same (which has been
1360 checked earlier), this can only happen when multiple translation
1361 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1362 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1363 comptypes_internal. */
1366 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1367 bool *enum_and_int_p
, bool *different_types_p
)
1370 bool needs_warning
= false;
1372 /* We have to verify that the tags of the types are the same. This
1373 is harder than it looks because this may be a typedef, so we have
1374 to go look at the original type. It may even be a typedef of a
1376 In the case of compiler-created builtin structs the TYPE_DECL
1377 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1378 while (TYPE_NAME (t1
)
1379 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1380 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1381 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1383 while (TYPE_NAME (t2
)
1384 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1385 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1386 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1388 /* C90 didn't have the requirement that the two tags be the same. */
1389 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1392 /* C90 didn't say what happened if one or both of the types were
1393 incomplete; we choose to follow C99 rules here, which is that they
1395 if (TYPE_SIZE (t1
) == NULL
1396 || TYPE_SIZE (t2
) == NULL
)
1400 const struct tagged_tu_seen_cache
* tts_i
;
1401 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1402 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1406 switch (TREE_CODE (t1
))
1410 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1411 /* Speed up the case where the type values are in the same order. */
1412 tree tv1
= TYPE_VALUES (t1
);
1413 tree tv2
= TYPE_VALUES (t2
);
1420 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1422 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1424 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1431 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1435 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1441 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1447 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1449 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1451 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1462 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1463 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1469 /* Speed up the common case where the fields are in the same order. */
1470 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1471 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1475 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1477 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1478 enum_and_int_p
, different_types_p
);
1480 if (result
!= 1 && !DECL_NAME (s1
))
1488 needs_warning
= true;
1490 if (TREE_CODE (s1
) == FIELD_DECL
1491 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1492 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1500 tu
->val
= needs_warning
? 2 : 1;
1504 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1508 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1509 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1513 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1517 if (result
!= 1 && !DECL_NAME (s1
))
1525 needs_warning
= true;
1527 if (TREE_CODE (s1
) == FIELD_DECL
1528 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1529 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1541 tu
->val
= needs_warning
? 2 : 10;
1547 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1549 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1551 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1554 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1555 || DECL_NAME (s1
) != DECL_NAME (s2
))
1557 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1558 enum_and_int_p
, different_types_p
);
1562 needs_warning
= true;
1564 if (TREE_CODE (s1
) == FIELD_DECL
1565 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1566 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1572 tu
->val
= needs_warning
? 2 : 1;
1581 /* Return 1 if two function types F1 and F2 are compatible.
1582 If either type specifies no argument types,
1583 the other must specify a fixed number of self-promoting arg types.
1584 Otherwise, if one type specifies only the number of arguments,
1585 the other must specify that number of self-promoting arg types.
1586 Otherwise, the argument types must match.
1587 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1590 function_types_compatible_p (const_tree f1
, const_tree f2
,
1591 bool *enum_and_int_p
, bool *different_types_p
)
1594 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1599 ret1
= TREE_TYPE (f1
);
1600 ret2
= TREE_TYPE (f2
);
1602 /* 'volatile' qualifiers on a function's return type used to mean
1603 the function is noreturn. */
1604 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1605 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1606 if (TYPE_VOLATILE (ret1
))
1607 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1608 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1609 if (TYPE_VOLATILE (ret2
))
1610 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1611 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1612 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1616 args1
= TYPE_ARG_TYPES (f1
);
1617 args2
= TYPE_ARG_TYPES (f2
);
1619 if (different_types_p
!= NULL
1620 && (args1
== 0) != (args2
== 0))
1621 *different_types_p
= true;
1623 /* An unspecified parmlist matches any specified parmlist
1624 whose argument types don't need default promotions. */
1628 if (!self_promoting_args_p (args2
))
1630 /* If one of these types comes from a non-prototype fn definition,
1631 compare that with the other type's arglist.
1632 If they don't match, ask for a warning (but no error). */
1633 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1634 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1635 enum_and_int_p
, different_types_p
))
1641 if (!self_promoting_args_p (args1
))
1643 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1644 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1645 enum_and_int_p
, different_types_p
))
1650 /* Both types have argument lists: compare them and propagate results. */
1651 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1653 return val1
!= 1 ? val1
: val
;
1656 /* Check two lists of types for compatibility, returning 0 for
1657 incompatible, 1 for compatible, or 2 for compatible with
1658 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1659 comptypes_internal. */
1662 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1663 bool *enum_and_int_p
, bool *different_types_p
)
1665 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1671 tree a1
, mv1
, a2
, mv2
;
1672 if (args1
== 0 && args2
== 0)
1674 /* If one list is shorter than the other,
1675 they fail to match. */
1676 if (args1
== 0 || args2
== 0)
1678 mv1
= a1
= TREE_VALUE (args1
);
1679 mv2
= a2
= TREE_VALUE (args2
);
1680 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1681 mv1
= (TYPE_ATOMIC (mv1
)
1682 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1684 : TYPE_MAIN_VARIANT (mv1
));
1685 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1686 mv2
= (TYPE_ATOMIC (mv2
)
1687 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1689 : TYPE_MAIN_VARIANT (mv2
));
1690 /* A null pointer instead of a type
1691 means there is supposed to be an argument
1692 but nothing is specified about what type it has.
1693 So match anything that self-promotes. */
1694 if (different_types_p
!= NULL
1695 && (a1
== 0) != (a2
== 0))
1696 *different_types_p
= true;
1699 if (c_type_promotes_to (a2
) != a2
)
1704 if (c_type_promotes_to (a1
) != a1
)
1707 /* If one of the lists has an error marker, ignore this arg. */
1708 else if (TREE_CODE (a1
) == ERROR_MARK
1709 || TREE_CODE (a2
) == ERROR_MARK
)
1711 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1712 different_types_p
)))
1714 if (different_types_p
!= NULL
)
1715 *different_types_p
= true;
1716 /* Allow wait (union {union wait *u; int *i} *)
1717 and wait (union wait *) to be compatible. */
1718 if (TREE_CODE (a1
) == UNION_TYPE
1719 && (TYPE_NAME (a1
) == 0
1720 || TYPE_TRANSPARENT_AGGR (a1
))
1721 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1722 && tree_int_cst_equal (TYPE_SIZE (a1
),
1726 for (memb
= TYPE_FIELDS (a1
);
1727 memb
; memb
= DECL_CHAIN (memb
))
1729 tree mv3
= TREE_TYPE (memb
);
1730 if (mv3
&& mv3
!= error_mark_node
1731 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1732 mv3
= (TYPE_ATOMIC (mv3
)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1735 : TYPE_MAIN_VARIANT (mv3
));
1736 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1743 else if (TREE_CODE (a2
) == UNION_TYPE
1744 && (TYPE_NAME (a2
) == 0
1745 || TYPE_TRANSPARENT_AGGR (a2
))
1746 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a2
),
1751 for (memb
= TYPE_FIELDS (a2
);
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
, mv1
, enum_and_int_p
,
1772 /* comptypes said ok, but record if it said to warn. */
1776 args1
= TREE_CHAIN (args1
);
1777 args2
= TREE_CHAIN (args2
);
1781 /* Compute the size to increment a pointer by. When a function type or void
1782 type or incomplete type is passed, size_one_node is returned.
1783 This function does not emit any diagnostics; the caller is responsible
1787 c_size_in_bytes (const_tree type
)
1789 enum tree_code code
= TREE_CODE (type
);
1791 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1792 || !COMPLETE_TYPE_P (type
))
1793 return size_one_node
;
1795 /* Convert in case a char is more than one unit. */
1796 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1797 size_int (TYPE_PRECISION (char_type_node
)
1801 /* Return either DECL or its known constant value (if it has one). */
1804 decl_constant_value (tree decl
)
1806 if (/* Don't change a variable array bound or initial value to a constant
1807 in a place where a variable is invalid. Note that DECL_INITIAL
1808 isn't valid for a PARM_DECL. */
1809 current_function_decl
!= 0
1810 && TREE_CODE (decl
) != PARM_DECL
1811 && !TREE_THIS_VOLATILE (decl
)
1812 && TREE_READONLY (decl
)
1813 && DECL_INITIAL (decl
) != 0
1814 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1815 /* This is invalid if initial value is not constant.
1816 If it has either a function call, a memory reference,
1817 or a variable, then re-evaluating it could give different results. */
1818 && TREE_CONSTANT (DECL_INITIAL (decl
))
1819 /* Check for cases where this is sub-optimal, even though valid. */
1820 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1821 return DECL_INITIAL (decl
);
1825 /* Convert the array expression EXP to a pointer. */
1827 array_to_pointer_conversion (location_t loc
, tree exp
)
1829 tree orig_exp
= exp
;
1830 tree type
= TREE_TYPE (exp
);
1832 tree restype
= TREE_TYPE (type
);
1835 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1837 STRIP_TYPE_NOPS (exp
);
1839 if (TREE_NO_WARNING (orig_exp
))
1840 TREE_NO_WARNING (exp
) = 1;
1842 ptrtype
= build_pointer_type (restype
);
1844 if (TREE_CODE (exp
) == INDIRECT_REF
)
1845 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1847 /* In C++ array compound literals are temporary objects unless they are
1848 const or appear in namespace scope, so they are destroyed too soon
1849 to use them for much of anything (c++/53220). */
1850 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1852 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1853 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1854 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1855 "converting an array compound literal to a pointer "
1856 "is ill-formed in C++");
1859 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1860 return convert (ptrtype
, adr
);
1863 /* Convert the function expression EXP to a pointer. */
1865 function_to_pointer_conversion (location_t loc
, tree exp
)
1867 tree orig_exp
= exp
;
1869 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1871 STRIP_TYPE_NOPS (exp
);
1873 if (TREE_NO_WARNING (orig_exp
))
1874 TREE_NO_WARNING (exp
) = 1;
1876 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1879 /* Mark EXP as read, not just set, for set but not used -Wunused
1880 warning purposes. */
1883 mark_exp_read (tree exp
)
1885 switch (TREE_CODE (exp
))
1889 DECL_READ_P (exp
) = 1;
1898 mark_exp_read (TREE_OPERAND (exp
, 0));
1901 case C_MAYBE_CONST_EXPR
:
1902 mark_exp_read (TREE_OPERAND (exp
, 1));
1909 /* Perform the default conversion of arrays and functions to pointers.
1910 Return the result of converting EXP. For any other expression, just
1913 LOC is the location of the expression. */
1916 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1918 tree orig_exp
= exp
.value
;
1919 tree type
= TREE_TYPE (exp
.value
);
1920 enum tree_code code
= TREE_CODE (type
);
1926 bool not_lvalue
= false;
1927 bool lvalue_array_p
;
1929 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1930 || CONVERT_EXPR_P (exp
.value
))
1931 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1933 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1935 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1938 if (TREE_NO_WARNING (orig_exp
))
1939 TREE_NO_WARNING (exp
.value
) = 1;
1941 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1942 if (!flag_isoc99
&& !lvalue_array_p
)
1944 /* Before C99, non-lvalue arrays do not decay to pointers.
1945 Normally, using such an array would be invalid; but it can
1946 be used correctly inside sizeof or as a statement expression.
1947 Thus, do not give an error here; an error will result later. */
1951 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1955 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1965 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1967 mark_exp_read (exp
.value
);
1968 return default_function_array_conversion (loc
, exp
);
1971 /* Return whether EXPR should be treated as an atomic lvalue for the
1972 purposes of load and store handling. */
1975 really_atomic_lvalue (tree expr
)
1977 if (error_operand_p (expr
))
1979 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
1981 if (!lvalue_p (expr
))
1984 /* Ignore _Atomic on register variables, since their addresses can't
1985 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1986 sequences wouldn't work. Ignore _Atomic on structures containing
1987 bit-fields, since accessing elements of atomic structures or
1988 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1989 it's undefined at translation time or execution time, and the
1990 normal atomic sequences again wouldn't work. */
1991 while (handled_component_p (expr
))
1993 if (TREE_CODE (expr
) == COMPONENT_REF
1994 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1996 expr
= TREE_OPERAND (expr
, 0);
1998 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
2003 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2004 including converting functions and arrays to pointers if CONVERT_P.
2005 If READ_P, also mark the expression as having been read. */
2008 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
2009 bool convert_p
, bool read_p
)
2012 mark_exp_read (exp
.value
);
2014 exp
= default_function_array_conversion (loc
, exp
);
2015 if (really_atomic_lvalue (exp
.value
))
2017 vec
<tree
, va_gc
> *params
;
2018 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2019 tree expr_type
= TREE_TYPE (exp
.value
);
2020 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, 0);
2021 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2023 gcc_assert (TYPE_ATOMIC (expr_type
));
2025 /* Expansion of a generic atomic load may require an addition
2026 element, so allocate enough to prevent a resize. */
2027 vec_alloc (params
, 4);
2029 /* Remove the qualifiers for the rest of the expressions and
2030 create the VAL temp variable to hold the RHS. */
2031 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2032 tmp
= create_tmp_var_raw (nonatomic_type
);
2033 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, 0);
2034 TREE_ADDRESSABLE (tmp
) = 1;
2035 TREE_NO_WARNING (tmp
) = 1;
2037 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2038 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2039 params
->quick_push (expr_addr
);
2040 params
->quick_push (tmp_addr
);
2041 params
->quick_push (seq_cst
);
2042 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2044 /* EXPR is always read. */
2045 mark_exp_read (exp
.value
);
2047 /* Return tmp which contains the value loaded. */
2048 exp
.value
= build4 (TARGET_EXPR
, nonatomic_type
, tmp
, func_call
,
2049 NULL_TREE
, NULL_TREE
);
2054 /* EXP is an expression of integer type. Apply the integer promotions
2055 to it and return the promoted value. */
2058 perform_integral_promotions (tree exp
)
2060 tree type
= TREE_TYPE (exp
);
2061 enum tree_code code
= TREE_CODE (type
);
2063 gcc_assert (INTEGRAL_TYPE_P (type
));
2065 /* Normally convert enums to int,
2066 but convert wide enums to something wider. */
2067 if (code
== ENUMERAL_TYPE
)
2069 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2070 TYPE_PRECISION (integer_type_node
)),
2071 ((TYPE_PRECISION (type
)
2072 >= TYPE_PRECISION (integer_type_node
))
2073 && TYPE_UNSIGNED (type
)));
2075 return convert (type
, exp
);
2078 /* ??? This should no longer be needed now bit-fields have their
2080 if (TREE_CODE (exp
) == COMPONENT_REF
2081 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2082 /* If it's thinner than an int, promote it like a
2083 c_promoting_integer_type_p, otherwise leave it alone. */
2084 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2085 TYPE_PRECISION (integer_type_node
)))
2086 return convert (integer_type_node
, exp
);
2088 if (c_promoting_integer_type_p (type
))
2090 /* Preserve unsignedness if not really getting any wider. */
2091 if (TYPE_UNSIGNED (type
)
2092 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2093 return convert (unsigned_type_node
, exp
);
2095 return convert (integer_type_node
, exp
);
2102 /* Perform default promotions for C data used in expressions.
2103 Enumeral types or short or char are converted to int.
2104 In addition, manifest constants symbols are replaced by their values. */
2107 default_conversion (tree exp
)
2110 tree type
= TREE_TYPE (exp
);
2111 enum tree_code code
= TREE_CODE (type
);
2114 mark_exp_read (exp
);
2116 /* Functions and arrays have been converted during parsing. */
2117 gcc_assert (code
!= FUNCTION_TYPE
);
2118 if (code
== ARRAY_TYPE
)
2121 /* Constants can be used directly unless they're not loadable. */
2122 if (TREE_CODE (exp
) == CONST_DECL
)
2123 exp
= DECL_INITIAL (exp
);
2125 /* Strip no-op conversions. */
2127 STRIP_TYPE_NOPS (exp
);
2129 if (TREE_NO_WARNING (orig_exp
))
2130 TREE_NO_WARNING (exp
) = 1;
2132 if (code
== VOID_TYPE
)
2134 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2135 "void value not ignored as it ought to be");
2136 return error_mark_node
;
2139 exp
= require_complete_type (exp
);
2140 if (exp
== error_mark_node
)
2141 return error_mark_node
;
2143 promoted_type
= targetm
.promoted_type (type
);
2145 return convert (promoted_type
, exp
);
2147 if (INTEGRAL_TYPE_P (type
))
2148 return perform_integral_promotions (exp
);
2153 /* Look up COMPONENT in a structure or union TYPE.
2155 If the component name is not found, returns NULL_TREE. Otherwise,
2156 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2157 stepping down the chain to the component, which is in the last
2158 TREE_VALUE of the list. Normally the list is of length one, but if
2159 the component is embedded within (nested) anonymous structures or
2160 unions, the list steps down the chain to the component. */
2163 lookup_field (tree type
, tree component
)
2167 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2168 to the field elements. Use a binary search on this array to quickly
2169 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2170 will always be set for structures which have many elements. */
2172 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2175 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2177 field
= TYPE_FIELDS (type
);
2179 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2180 while (top
- bot
> 1)
2182 half
= (top
- bot
+ 1) >> 1;
2183 field
= field_array
[bot
+half
];
2185 if (DECL_NAME (field
) == NULL_TREE
)
2187 /* Step through all anon unions in linear fashion. */
2188 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2190 field
= field_array
[bot
++];
2191 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2192 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
2194 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2197 return tree_cons (NULL_TREE
, field
, anon
);
2199 /* The Plan 9 compiler permits referring
2200 directly to an anonymous struct/union field
2201 using a typedef name. */
2202 if (flag_plan9_extensions
2203 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2204 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2206 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2212 /* Entire record is only anon unions. */
2216 /* Restart the binary search, with new lower bound. */
2220 if (DECL_NAME (field
) == component
)
2222 if (DECL_NAME (field
) < component
)
2228 if (DECL_NAME (field_array
[bot
]) == component
)
2229 field
= field_array
[bot
];
2230 else if (DECL_NAME (field
) != component
)
2235 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2237 if (DECL_NAME (field
) == NULL_TREE
2238 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2239 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
2241 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2244 return tree_cons (NULL_TREE
, field
, anon
);
2246 /* The Plan 9 compiler permits referring directly to an
2247 anonymous struct/union field using a typedef
2249 if (flag_plan9_extensions
2250 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2251 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2252 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2257 if (DECL_NAME (field
) == component
)
2261 if (field
== NULL_TREE
)
2265 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2268 /* Make an expression to refer to the COMPONENT field of structure or
2269 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2270 location of the COMPONENT_REF. */
2273 build_component_ref (location_t loc
, tree datum
, tree component
)
2275 tree type
= TREE_TYPE (datum
);
2276 enum tree_code code
= TREE_CODE (type
);
2279 bool datum_lvalue
= lvalue_p (datum
);
2281 if (!objc_is_public (datum
, component
))
2282 return error_mark_node
;
2284 /* Detect Objective-C property syntax object.property. */
2285 if (c_dialect_objc ()
2286 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2289 /* See if there is a field or component with name COMPONENT. */
2291 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2293 if (!COMPLETE_TYPE_P (type
))
2295 c_incomplete_type_error (NULL_TREE
, type
);
2296 return error_mark_node
;
2299 field
= lookup_field (type
, component
);
2303 error_at (loc
, "%qT has no member named %qE", type
, component
);
2304 return error_mark_node
;
2307 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2308 This might be better solved in future the way the C++ front
2309 end does it - by giving the anonymous entities each a
2310 separate name and type, and then have build_component_ref
2311 recursively call itself. We can't do that here. */
2314 tree subdatum
= TREE_VALUE (field
);
2317 bool use_datum_quals
;
2319 if (TREE_TYPE (subdatum
) == error_mark_node
)
2320 return error_mark_node
;
2322 /* If this is an rvalue, it does not have qualifiers in C
2323 standard terms and we must avoid propagating such
2324 qualifiers down to a non-lvalue array that is then
2325 converted to a pointer. */
2326 use_datum_quals
= (datum_lvalue
2327 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2329 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2330 if (use_datum_quals
)
2331 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2332 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2334 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2336 SET_EXPR_LOCATION (ref
, loc
);
2337 if (TREE_READONLY (subdatum
)
2338 || (use_datum_quals
&& TREE_READONLY (datum
)))
2339 TREE_READONLY (ref
) = 1;
2340 if (TREE_THIS_VOLATILE (subdatum
)
2341 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2342 TREE_THIS_VOLATILE (ref
) = 1;
2344 if (TREE_DEPRECATED (subdatum
))
2345 warn_deprecated_use (subdatum
, NULL_TREE
);
2349 field
= TREE_CHAIN (field
);
2355 else if (code
!= ERROR_MARK
)
2357 "request for member %qE in something not a structure or union",
2360 return error_mark_node
;
2363 /* Given an expression PTR for a pointer, return an expression
2364 for the value pointed to.
2365 ERRORSTRING is the name of the operator to appear in error messages.
2367 LOC is the location to use for the generated tree. */
2370 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2372 tree pointer
= default_conversion (ptr
);
2373 tree type
= TREE_TYPE (pointer
);
2376 if (TREE_CODE (type
) == POINTER_TYPE
)
2378 if (CONVERT_EXPR_P (pointer
)
2379 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2381 /* If a warning is issued, mark it to avoid duplicates from
2382 the backend. This only needs to be done at
2383 warn_strict_aliasing > 2. */
2384 if (warn_strict_aliasing
> 2)
2385 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2386 type
, TREE_OPERAND (pointer
, 0)))
2387 TREE_NO_WARNING (pointer
) = 1;
2390 if (TREE_CODE (pointer
) == ADDR_EXPR
2391 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2392 == TREE_TYPE (type
)))
2394 ref
= TREE_OPERAND (pointer
, 0);
2395 protected_set_expr_location (ref
, loc
);
2400 tree t
= TREE_TYPE (type
);
2402 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2404 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2406 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)))
2408 error_at (loc
, "dereferencing pointer to incomplete type "
2410 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)) = 1;
2412 return error_mark_node
;
2414 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2415 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2417 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2418 so that we get the proper error message if the result is used
2419 to assign to. Also, &* is supposed to be a no-op.
2420 And ANSI C seems to specify that the type of the result
2421 should be the const type. */
2422 /* A de-reference of a pointer to const is not a const. It is valid
2423 to change it via some other pointer. */
2424 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2425 TREE_SIDE_EFFECTS (ref
)
2426 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2427 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2428 protected_set_expr_location (ref
, loc
);
2432 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2433 invalid_indirection_error (loc
, type
, errstring
);
2435 return error_mark_node
;
2438 /* This handles expressions of the form "a[i]", which denotes
2441 This is logically equivalent in C to *(a+i), but we may do it differently.
2442 If A is a variable or a member, we generate a primitive ARRAY_REF.
2443 This avoids forcing the array out of registers, and can work on
2444 arrays that are not lvalues (for example, members of structures returned
2447 For vector types, allow vector[i] but not i[vector], and create
2448 *(((type*)&vectortype) + i) for the expression.
2450 LOC is the location to use for the returned expression. */
2453 build_array_ref (location_t loc
, tree array
, tree index
)
2456 bool swapped
= false;
2457 if (TREE_TYPE (array
) == error_mark_node
2458 || TREE_TYPE (index
) == error_mark_node
)
2459 return error_mark_node
;
2461 if (flag_cilkplus
&& contains_array_notation_expr (index
))
2464 if (!find_rank (loc
, index
, index
, true, &rank
))
2465 return error_mark_node
;
2468 error_at (loc
, "rank of the array's index is greater than 1");
2469 return error_mark_node
;
2472 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2473 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2474 /* Allow vector[index] but not index[vector]. */
2475 && TREE_CODE (TREE_TYPE (array
)) != VECTOR_TYPE
)
2478 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2479 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2482 "subscripted value is neither array nor pointer nor vector");
2484 return error_mark_node
;
2492 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2494 error_at (loc
, "array subscript is not an integer");
2495 return error_mark_node
;
2498 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2500 error_at (loc
, "subscripted value is pointer to function");
2501 return error_mark_node
;
2504 /* ??? Existing practice has been to warn only when the char
2505 index is syntactically the index, not for char[array]. */
2507 warn_array_subscript_with_type_char (loc
, index
);
2509 /* Apply default promotions *after* noticing character types. */
2510 index
= default_conversion (index
);
2511 if (index
== error_mark_node
)
2512 return error_mark_node
;
2514 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2517 = convert_vector_to_pointer_for_subscript (loc
, &array
, index
);
2519 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2523 /* An array that is indexed by a non-constant
2524 cannot be stored in a register; we must be able to do
2525 address arithmetic on its address.
2526 Likewise an array of elements of variable size. */
2527 if (TREE_CODE (index
) != INTEGER_CST
2528 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2529 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2531 if (!c_mark_addressable (array
))
2532 return error_mark_node
;
2534 /* An array that is indexed by a constant value which is not within
2535 the array bounds cannot be stored in a register either; because we
2536 would get a crash in store_bit_field/extract_bit_field when trying
2537 to access a non-existent part of the register. */
2538 if (TREE_CODE (index
) == INTEGER_CST
2539 && TYPE_DOMAIN (TREE_TYPE (array
))
2540 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2542 if (!c_mark_addressable (array
))
2543 return error_mark_node
;
2546 if (pedantic
|| warn_c90_c99_compat
)
2549 while (TREE_CODE (foo
) == COMPONENT_REF
)
2550 foo
= TREE_OPERAND (foo
, 0);
2551 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2552 pedwarn (loc
, OPT_Wpedantic
,
2553 "ISO C forbids subscripting %<register%> array");
2554 else if (!lvalue_p (foo
))
2555 pedwarn_c90 (loc
, OPT_Wpedantic
,
2556 "ISO C90 forbids subscripting non-lvalue "
2560 type
= TREE_TYPE (TREE_TYPE (array
));
2561 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2562 /* Array ref is const/volatile if the array elements are
2563 or if the array is. */
2564 TREE_READONLY (rval
)
2565 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2566 | TREE_READONLY (array
));
2567 TREE_SIDE_EFFECTS (rval
)
2568 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2569 | TREE_SIDE_EFFECTS (array
));
2570 TREE_THIS_VOLATILE (rval
)
2571 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2572 /* This was added by rms on 16 Nov 91.
2573 It fixes vol struct foo *a; a->elts[1]
2574 in an inline function.
2575 Hope it doesn't break something else. */
2576 | TREE_THIS_VOLATILE (array
));
2577 ret
= require_complete_type (rval
);
2578 protected_set_expr_location (ret
, loc
);
2580 ret
= non_lvalue_loc (loc
, ret
);
2585 tree ar
= default_conversion (array
);
2587 if (ar
== error_mark_node
)
2590 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2591 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2593 ret
= build_indirect_ref (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
,
2597 ret
= non_lvalue_loc (loc
, ret
);
2602 /* Build an external reference to identifier ID. FUN indicates
2603 whether this will be used for a function call. LOC is the source
2604 location of the identifier. This sets *TYPE to the type of the
2605 identifier, which is not the same as the type of the returned value
2606 for CONST_DECLs defined as enum constants. If the type of the
2607 identifier is not available, *TYPE is set to NULL. */
2609 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2612 tree decl
= lookup_name (id
);
2614 /* In Objective-C, an instance variable (ivar) may be preferred to
2615 whatever lookup_name() found. */
2616 decl
= objc_lookup_ivar (decl
, id
);
2619 if (decl
&& decl
!= error_mark_node
)
2622 *type
= TREE_TYPE (ref
);
2625 /* Implicit function declaration. */
2626 ref
= implicitly_declare (loc
, id
);
2627 else if (decl
== error_mark_node
)
2628 /* Don't complain about something that's already been
2629 complained about. */
2630 return error_mark_node
;
2633 undeclared_variable (loc
, id
);
2634 return error_mark_node
;
2637 if (TREE_TYPE (ref
) == error_mark_node
)
2638 return error_mark_node
;
2640 if (TREE_DEPRECATED (ref
))
2641 warn_deprecated_use (ref
, NULL_TREE
);
2643 /* Recursive call does not count as usage. */
2644 if (ref
!= current_function_decl
)
2646 TREE_USED (ref
) = 1;
2649 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2651 if (!in_sizeof
&& !in_typeof
)
2652 C_DECL_USED (ref
) = 1;
2653 else if (DECL_INITIAL (ref
) == 0
2654 && DECL_EXTERNAL (ref
)
2655 && !TREE_PUBLIC (ref
))
2656 record_maybe_used_decl (ref
);
2659 if (TREE_CODE (ref
) == CONST_DECL
)
2661 used_types_insert (TREE_TYPE (ref
));
2664 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2665 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2667 warning_at (loc
, OPT_Wc___compat
,
2668 ("enum constant defined in struct or union "
2669 "is not visible in C++"));
2670 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2673 ref
= DECL_INITIAL (ref
);
2674 TREE_CONSTANT (ref
) = 1;
2676 else if (current_function_decl
!= 0
2677 && !DECL_FILE_SCOPE_P (current_function_decl
)
2678 && (TREE_CODE (ref
) == VAR_DECL
2679 || TREE_CODE (ref
) == PARM_DECL
2680 || TREE_CODE (ref
) == FUNCTION_DECL
))
2682 tree context
= decl_function_context (ref
);
2684 if (context
!= 0 && context
!= current_function_decl
)
2685 DECL_NONLOCAL (ref
) = 1;
2687 /* C99 6.7.4p3: An inline definition of a function with external
2688 linkage ... shall not contain a reference to an identifier with
2689 internal linkage. */
2690 else if (current_function_decl
!= 0
2691 && DECL_DECLARED_INLINE_P (current_function_decl
)
2692 && DECL_EXTERNAL (current_function_decl
)
2693 && VAR_OR_FUNCTION_DECL_P (ref
)
2694 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2695 && ! TREE_PUBLIC (ref
)
2696 && DECL_CONTEXT (ref
) != current_function_decl
)
2697 record_inline_static (loc
, current_function_decl
, ref
,
2703 /* Record details of decls possibly used inside sizeof or typeof. */
2704 struct maybe_used_decl
2708 /* The level seen at (in_sizeof + in_typeof). */
2710 /* The next one at this level or above, or NULL. */
2711 struct maybe_used_decl
*next
;
2714 static struct maybe_used_decl
*maybe_used_decls
;
2716 /* Record that DECL, an undefined static function reference seen
2717 inside sizeof or typeof, might be used if the operand of sizeof is
2718 a VLA type or the operand of typeof is a variably modified
2722 record_maybe_used_decl (tree decl
)
2724 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2726 t
->level
= in_sizeof
+ in_typeof
;
2727 t
->next
= maybe_used_decls
;
2728 maybe_used_decls
= t
;
2731 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2732 USED is false, just discard them. If it is true, mark them used
2733 (if no longer inside sizeof or typeof) or move them to the next
2734 level up (if still inside sizeof or typeof). */
2737 pop_maybe_used (bool used
)
2739 struct maybe_used_decl
*p
= maybe_used_decls
;
2740 int cur_level
= in_sizeof
+ in_typeof
;
2741 while (p
&& p
->level
> cur_level
)
2746 C_DECL_USED (p
->decl
) = 1;
2748 p
->level
= cur_level
;
2752 if (!used
|| cur_level
== 0)
2753 maybe_used_decls
= p
;
2756 /* Return the result of sizeof applied to EXPR. */
2759 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2762 if (expr
.value
== error_mark_node
)
2764 ret
.value
= error_mark_node
;
2765 ret
.original_code
= ERROR_MARK
;
2766 ret
.original_type
= NULL
;
2767 pop_maybe_used (false);
2771 bool expr_const_operands
= true;
2773 if (TREE_CODE (expr
.value
) == PARM_DECL
2774 && C_ARRAY_PARAMETER (expr
.value
))
2776 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2777 "%<sizeof%> on array function parameter %qE will "
2778 "return size of %qT", expr
.value
,
2779 expr
.original_type
))
2780 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2782 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2783 &expr_const_operands
);
2784 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2785 c_last_sizeof_arg
= expr
.value
;
2786 ret
.original_code
= SIZEOF_EXPR
;
2787 ret
.original_type
= NULL
;
2788 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2790 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2791 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2792 folded_expr
, ret
.value
);
2793 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2794 SET_EXPR_LOCATION (ret
.value
, loc
);
2796 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2801 /* Return the result of sizeof applied to T, a structure for the type
2802 name passed to sizeof (rather than the type itself). LOC is the
2803 location of the original expression. */
2806 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2810 tree type_expr
= NULL_TREE
;
2811 bool type_expr_const
= true;
2812 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2813 ret
.value
= c_sizeof (loc
, type
);
2814 c_last_sizeof_arg
= type
;
2815 ret
.original_code
= SIZEOF_EXPR
;
2816 ret
.original_type
= NULL
;
2817 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2818 && c_vla_type_p (type
))
2820 /* If the type is a [*] array, it is a VLA but is represented as
2821 having a size of zero. In such a case we must ensure that
2822 the result of sizeof does not get folded to a constant by
2823 c_fully_fold, because if the size is evaluated the result is
2824 not constant and so constraints on zero or negative size
2825 arrays must not be applied when this sizeof call is inside
2826 another array declarator. */
2828 type_expr
= integer_zero_node
;
2829 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2830 type_expr
, ret
.value
);
2831 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2833 pop_maybe_used (type
!= error_mark_node
2834 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2838 /* Build a function call to function FUNCTION with parameters PARAMS.
2839 The function call is at LOC.
2840 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2841 TREE_VALUE of each node is a parameter-expression.
2842 FUNCTION's data type may be a function type or a pointer-to-function. */
2845 build_function_call (location_t loc
, tree function
, tree params
)
2847 vec
<tree
, va_gc
> *v
;
2850 vec_alloc (v
, list_length (params
));
2851 for (; params
; params
= TREE_CHAIN (params
))
2852 v
->quick_push (TREE_VALUE (params
));
2853 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2858 /* Give a note about the location of the declaration of DECL. */
2860 static void inform_declaration (tree decl
)
2862 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_BUILT_IN (decl
)))
2863 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2866 /* Build a function call to function FUNCTION with parameters PARAMS.
2867 ORIGTYPES, if not NULL, is a vector of types; each element is
2868 either NULL or the original type of the corresponding element in
2869 PARAMS. The original type may differ from TREE_TYPE of the
2870 parameter for enums. FUNCTION's data type may be a function type
2871 or pointer-to-function. This function changes the elements of
2875 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
2876 tree function
, vec
<tree
, va_gc
> *params
,
2877 vec
<tree
, va_gc
> *origtypes
)
2879 tree fntype
, fundecl
= 0;
2880 tree name
= NULL_TREE
, result
;
2886 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2887 STRIP_TYPE_NOPS (function
);
2889 /* Convert anything with function type to a pointer-to-function. */
2890 if (TREE_CODE (function
) == FUNCTION_DECL
)
2892 name
= DECL_NAME (function
);
2895 tm_malloc_replacement (function
);
2897 /* Atomic functions have type checking/casting already done. They are
2898 often rewritten and don't match the original parameter list. */
2899 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
2903 && is_cilkplus_reduce_builtin (function
))
2906 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2907 function
= function_to_pointer_conversion (loc
, function
);
2909 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2910 expressions, like those used for ObjC messenger dispatches. */
2911 if (params
&& !params
->is_empty ())
2912 function
= objc_rewrite_function_call (function
, (*params
)[0]);
2914 function
= c_fully_fold (function
, false, NULL
);
2916 fntype
= TREE_TYPE (function
);
2918 if (TREE_CODE (fntype
) == ERROR_MARK
)
2919 return error_mark_node
;
2921 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2922 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2924 if (!flag_diagnostics_show_caret
)
2926 "called object %qE is not a function or function pointer",
2928 else if (DECL_P (function
))
2931 "called object %qD is not a function or function pointer",
2933 inform_declaration (function
);
2937 "called object is not a function or function pointer");
2938 return error_mark_node
;
2941 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2942 current_function_returns_abnormally
= 1;
2944 /* fntype now gets the type of function pointed to. */
2945 fntype
= TREE_TYPE (fntype
);
2947 /* Convert the parameters to the types declared in the
2948 function prototype, or apply default promotions. */
2950 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
2951 origtypes
, function
, fundecl
);
2953 return error_mark_node
;
2955 /* Check that the function is called through a compatible prototype.
2956 If it is not, warn. */
2957 if (CONVERT_EXPR_P (function
)
2958 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2959 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2960 && !comptypes (fntype
, TREE_TYPE (tem
)))
2962 tree return_type
= TREE_TYPE (fntype
);
2964 /* This situation leads to run-time undefined behavior. We can't,
2965 therefore, simply error unless we can prove that all possible
2966 executions of the program must execute the code. */
2967 warning_at (loc
, 0, "function called through a non-compatible type");
2969 if (VOID_TYPE_P (return_type
)
2970 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2972 "function with qualified void return type called");
2975 argarray
= vec_safe_address (params
);
2977 /* Check that arguments to builtin functions match the expectations. */
2979 && DECL_BUILT_IN (fundecl
)
2980 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2981 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2982 return error_mark_node
;
2984 /* Check that the arguments to the function are valid. */
2985 check_function_arguments (fntype
, nargs
, argarray
);
2987 if (name
!= NULL_TREE
2988 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
2990 if (require_constant_value
)
2992 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
2993 function
, nargs
, argarray
);
2995 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
2996 function
, nargs
, argarray
);
2997 if (TREE_CODE (result
) == NOP_EXPR
2998 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
2999 STRIP_TYPE_NOPS (result
);
3002 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
3003 function
, nargs
, argarray
);
3005 if (VOID_TYPE_P (TREE_TYPE (result
)))
3007 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
3009 "function with qualified void return type called");
3012 return require_complete_type (result
);
3015 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3018 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3019 tree function
, vec
<tree
, va_gc
> *params
,
3020 vec
<tree
, va_gc
> *origtypes
)
3022 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3023 STRIP_TYPE_NOPS (function
);
3025 /* Convert anything with function type to a pointer-to-function. */
3026 if (TREE_CODE (function
) == FUNCTION_DECL
)
3028 /* Implement type-directed function overloading for builtins.
3029 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3030 handle all the type checking. The result is a complete expression
3031 that implements this function call. */
3032 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3036 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3039 /* Convert the argument expressions in the vector VALUES
3040 to the types in the list TYPELIST.
3042 If TYPELIST is exhausted, or when an element has NULL as its type,
3043 perform the default conversions.
3045 ORIGTYPES is the original types of the expressions in VALUES. This
3046 holds the type of enum values which have been converted to integral
3047 types. It may be NULL.
3049 FUNCTION is a tree for the called function. It is used only for
3050 error messages, where it is formatted with %qE.
3052 This is also where warnings about wrong number of args are generated.
3054 ARG_LOC are locations of function arguments (if any).
3056 Returns the actual number of arguments processed (which may be less
3057 than the length of VALUES in some error situations), or -1 on
3061 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3062 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3063 tree function
, tree fundecl
)
3066 unsigned int parmnum
;
3067 bool error_args
= false;
3068 const bool type_generic
= fundecl
3069 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3070 bool type_generic_remove_excess_precision
= false;
3073 /* Change pointer to function to the function itself for
3075 if (TREE_CODE (function
) == ADDR_EXPR
3076 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3077 function
= TREE_OPERAND (function
, 0);
3079 /* Handle an ObjC selector specially for diagnostics. */
3080 selector
= objc_message_selector ();
3082 /* For type-generic built-in functions, determine whether excess
3083 precision should be removed (classification) or not
3086 && DECL_BUILT_IN (fundecl
)
3087 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3089 switch (DECL_FUNCTION_CODE (fundecl
))
3091 case BUILT_IN_ISFINITE
:
3092 case BUILT_IN_ISINF
:
3093 case BUILT_IN_ISINF_SIGN
:
3094 case BUILT_IN_ISNAN
:
3095 case BUILT_IN_ISNORMAL
:
3096 case BUILT_IN_FPCLASSIFY
:
3097 type_generic_remove_excess_precision
= true;
3101 type_generic_remove_excess_precision
= false;
3105 if (flag_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3106 return vec_safe_length (values
);
3108 /* Scan the given expressions and types, producing individual
3109 converted arguments. */
3111 for (typetail
= typelist
, parmnum
= 0;
3112 values
&& values
->iterate (parmnum
, &val
);
3115 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3116 tree valtype
= TREE_TYPE (val
);
3117 tree rname
= function
;
3118 int argnum
= parmnum
+ 1;
3119 const char *invalid_func_diag
;
3120 bool excess_precision
= false;
3123 /* Some __atomic_* builtins have additional hidden argument at
3126 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3127 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3130 if (type
== void_type_node
)
3133 error_at (loc
, "too many arguments to method %qE", selector
);
3135 error_at (loc
, "too many arguments to function %qE", function
);
3136 inform_declaration (fundecl
);
3137 return error_args
? -1 : (int) parmnum
;
3140 if (selector
&& argnum
> 2)
3146 npc
= null_pointer_constant_p (val
);
3148 /* If there is excess precision and a prototype, convert once to
3149 the required type rather than converting via the semantic
3150 type. Likewise without a prototype a float value represented
3151 as long double should be converted once to double. But for
3152 type-generic classification functions excess precision must
3154 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3155 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3157 val
= TREE_OPERAND (val
, 0);
3158 excess_precision
= true;
3160 val
= c_fully_fold (val
, false, NULL
);
3161 STRIP_TYPE_NOPS (val
);
3163 val
= require_complete_type (val
);
3167 /* Formal parm type is specified by a function prototype. */
3169 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3171 error_at (ploc
, "type of formal parameter %d is incomplete",
3179 /* Optionally warn about conversions that
3180 differ from the default conversions. */
3181 if (warn_traditional_conversion
|| warn_traditional
)
3183 unsigned int formal_prec
= TYPE_PRECISION (type
);
3185 if (INTEGRAL_TYPE_P (type
)
3186 && TREE_CODE (valtype
) == REAL_TYPE
)
3187 warning_at (ploc
, OPT_Wtraditional_conversion
,
3188 "passing argument %d of %qE as integer rather "
3189 "than floating due to prototype",
3191 if (INTEGRAL_TYPE_P (type
)
3192 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3193 warning_at (ploc
, OPT_Wtraditional_conversion
,
3194 "passing argument %d of %qE as integer rather "
3195 "than complex due to prototype",
3197 else if (TREE_CODE (type
) == COMPLEX_TYPE
3198 && TREE_CODE (valtype
) == REAL_TYPE
)
3199 warning_at (ploc
, OPT_Wtraditional_conversion
,
3200 "passing argument %d of %qE as complex rather "
3201 "than floating due to prototype",
3203 else if (TREE_CODE (type
) == REAL_TYPE
3204 && INTEGRAL_TYPE_P (valtype
))
3205 warning_at (ploc
, OPT_Wtraditional_conversion
,
3206 "passing argument %d of %qE as floating rather "
3207 "than integer due to prototype",
3209 else if (TREE_CODE (type
) == COMPLEX_TYPE
3210 && INTEGRAL_TYPE_P (valtype
))
3211 warning_at (ploc
, OPT_Wtraditional_conversion
,
3212 "passing argument %d of %qE as complex rather "
3213 "than integer due to prototype",
3215 else if (TREE_CODE (type
) == REAL_TYPE
3216 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3217 warning_at (ploc
, OPT_Wtraditional_conversion
,
3218 "passing argument %d of %qE as floating rather "
3219 "than complex due to prototype",
3221 /* ??? At some point, messages should be written about
3222 conversions between complex types, but that's too messy
3224 else if (TREE_CODE (type
) == REAL_TYPE
3225 && TREE_CODE (valtype
) == REAL_TYPE
)
3227 /* Warn if any argument is passed as `float',
3228 since without a prototype it would be `double'. */
3229 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3230 && type
!= dfloat32_type_node
)
3231 warning_at (ploc
, 0,
3232 "passing argument %d of %qE as %<float%> "
3233 "rather than %<double%> due to prototype",
3236 /* Warn if mismatch between argument and prototype
3237 for decimal float types. Warn of conversions with
3238 binary float types and of precision narrowing due to
3240 else if (type
!= valtype
3241 && (type
== dfloat32_type_node
3242 || type
== dfloat64_type_node
3243 || type
== dfloat128_type_node
3244 || valtype
== dfloat32_type_node
3245 || valtype
== dfloat64_type_node
3246 || valtype
== dfloat128_type_node
)
3248 <= TYPE_PRECISION (valtype
)
3249 || (type
== dfloat128_type_node
3251 != dfloat64_type_node
3253 != dfloat32_type_node
)))
3254 || (type
== dfloat64_type_node
3256 != dfloat32_type_node
))))
3257 warning_at (ploc
, 0,
3258 "passing argument %d of %qE as %qT "
3259 "rather than %qT due to prototype",
3260 argnum
, rname
, type
, valtype
);
3263 /* Detect integer changing in width or signedness.
3264 These warnings are only activated with
3265 -Wtraditional-conversion, not with -Wtraditional. */
3266 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3267 && INTEGRAL_TYPE_P (valtype
))
3269 tree would_have_been
= default_conversion (val
);
3270 tree type1
= TREE_TYPE (would_have_been
);
3272 if (TREE_CODE (type
) == ENUMERAL_TYPE
3273 && (TYPE_MAIN_VARIANT (type
)
3274 == TYPE_MAIN_VARIANT (valtype
)))
3275 /* No warning if function asks for enum
3276 and the actual arg is that enum type. */
3278 else if (formal_prec
!= TYPE_PRECISION (type1
))
3279 warning_at (ploc
, OPT_Wtraditional_conversion
,
3280 "passing argument %d of %qE "
3281 "with different width due to prototype",
3283 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3285 /* Don't complain if the formal parameter type
3286 is an enum, because we can't tell now whether
3287 the value was an enum--even the same enum. */
3288 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3290 else if (TREE_CODE (val
) == INTEGER_CST
3291 && int_fits_type_p (val
, type
))
3292 /* Change in signedness doesn't matter
3293 if a constant value is unaffected. */
3295 /* If the value is extended from a narrower
3296 unsigned type, it doesn't matter whether we
3297 pass it as signed or unsigned; the value
3298 certainly is the same either way. */
3299 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3300 && TYPE_UNSIGNED (valtype
))
3302 else if (TYPE_UNSIGNED (type
))
3303 warning_at (ploc
, OPT_Wtraditional_conversion
,
3304 "passing argument %d of %qE "
3305 "as unsigned due to prototype",
3308 warning_at (ploc
, OPT_Wtraditional_conversion
,
3309 "passing argument %d of %qE "
3310 "as signed due to prototype",
3315 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3316 sake of better warnings from convert_and_check. */
3317 if (excess_precision
)
3318 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3319 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3320 parmval
= convert_for_assignment (loc
, ploc
, type
,
3321 val
, origtype
, ic_argpass
,
3322 npc
, fundecl
, function
,
3325 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3326 && INTEGRAL_TYPE_P (type
)
3327 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3328 parmval
= default_conversion (parmval
);
3331 else if (TREE_CODE (valtype
) == REAL_TYPE
3332 && (TYPE_PRECISION (valtype
)
3333 <= TYPE_PRECISION (double_type_node
))
3334 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3335 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3336 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3342 /* Convert `float' to `double'. */
3343 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3344 warning_at (ploc
, OPT_Wdouble_promotion
,
3345 "implicit conversion from %qT to %qT when passing "
3346 "argument to function",
3347 valtype
, double_type_node
);
3348 parmval
= convert (double_type_node
, val
);
3351 else if (excess_precision
&& !type_generic
)
3352 /* A "double" argument with excess precision being passed
3353 without a prototype or in variable arguments. */
3354 parmval
= convert (valtype
, val
);
3355 else if ((invalid_func_diag
=
3356 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3358 error (invalid_func_diag
);
3362 /* Convert `short' and `char' to full-size `int'. */
3363 parmval
= default_conversion (val
);
3365 (*values
)[parmnum
] = parmval
;
3366 if (parmval
== error_mark_node
)
3370 typetail
= TREE_CHAIN (typetail
);
3373 gcc_assert (parmnum
== vec_safe_length (values
));
3375 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3377 error_at (loc
, "too few arguments to function %qE", function
);
3378 inform_declaration (fundecl
);
3382 return error_args
? -1 : (int) parmnum
;
3385 /* This is the entry point used by the parser to build unary operators
3386 in the input. CODE, a tree_code, specifies the unary operator, and
3387 ARG is the operand. For unary plus, the C parser currently uses
3388 CONVERT_EXPR for code.
3390 LOC is the location to use for the tree generated.
3394 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3396 struct c_expr result
;
3398 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
3399 result
.original_code
= code
;
3400 result
.original_type
= NULL
;
3402 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3403 overflow_warning (loc
, result
.value
);
3408 /* This is the entry point used by the parser to build binary operators
3409 in the input. CODE, a tree_code, specifies the binary operator, and
3410 ARG1 and ARG2 are the operands. In addition to constructing the
3411 expression, we check for operands that were written with other binary
3412 operators in a way that is likely to confuse the user.
3414 LOCATION is the location of the binary operator. */
3417 parser_build_binary_op (location_t location
, enum tree_code code
,
3418 struct c_expr arg1
, struct c_expr arg2
)
3420 struct c_expr result
;
3422 enum tree_code code1
= arg1
.original_code
;
3423 enum tree_code code2
= arg2
.original_code
;
3424 tree type1
= (arg1
.original_type
3425 ? arg1
.original_type
3426 : TREE_TYPE (arg1
.value
));
3427 tree type2
= (arg2
.original_type
3428 ? arg2
.original_type
3429 : TREE_TYPE (arg2
.value
));
3431 result
.value
= build_binary_op (location
, code
,
3432 arg1
.value
, arg2
.value
, 1);
3433 result
.original_code
= code
;
3434 result
.original_type
= NULL
;
3436 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3439 if (location
!= UNKNOWN_LOCATION
)
3440 protected_set_expr_location (result
.value
, location
);
3442 /* Check for cases such as x+y<<z which users are likely
3444 if (warn_parentheses
)
3445 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3448 if (warn_logical_op
)
3449 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3450 code1
, arg1
.value
, code2
, arg2
.value
);
3452 if (warn_logical_not_paren
3453 && TREE_CODE_CLASS (code
) == tcc_comparison
3454 && code1
== TRUTH_NOT_EXPR
3455 && code2
!= TRUTH_NOT_EXPR
3456 /* Avoid warning for !!x == y. */
3457 && (TREE_CODE (arg1
.value
) != NE_EXPR
3458 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
3460 /* Avoid warning for !b == y where b has _Bool type. */
3461 tree t
= integer_zero_node
;
3462 if (TREE_CODE (arg1
.value
) == EQ_EXPR
3463 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
3464 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
3466 t
= TREE_OPERAND (arg1
.value
, 0);
3469 if (TREE_TYPE (t
) != integer_type_node
)
3471 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
3472 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
3473 else if (CONVERT_EXPR_P (t
))
3474 t
= TREE_OPERAND (t
, 0);
3480 if (TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
)
3481 warn_logical_not_parentheses (location
, code
, arg2
.value
);
3484 /* Warn about comparisons against string literals, with the exception
3485 of testing for equality or inequality of a string literal with NULL. */
3486 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3488 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3489 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3490 warning_at (location
, OPT_Waddress
,
3491 "comparison with string literal results in unspecified behavior");
3493 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3494 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3495 warning_at (location
, OPT_Waddress
,
3496 "comparison with string literal results in unspecified behavior");
3498 if (TREE_OVERFLOW_P (result
.value
)
3499 && !TREE_OVERFLOW_P (arg1
.value
)
3500 && !TREE_OVERFLOW_P (arg2
.value
))
3501 overflow_warning (location
, result
.value
);
3503 /* Warn about comparisons of different enum types. */
3504 if (warn_enum_compare
3505 && TREE_CODE_CLASS (code
) == tcc_comparison
3506 && TREE_CODE (type1
) == ENUMERAL_TYPE
3507 && TREE_CODE (type2
) == ENUMERAL_TYPE
3508 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3509 warning_at (location
, OPT_Wenum_compare
,
3510 "comparison between %qT and %qT",
3516 /* Return a tree for the difference of pointers OP0 and OP1.
3517 The resulting tree has type int. */
3520 pointer_diff (location_t loc
, tree op0
, tree op1
)
3522 tree restype
= ptrdiff_type_node
;
3523 tree result
, inttype
;
3525 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3526 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3527 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3528 tree orig_op1
= op1
;
3530 /* If the operands point into different address spaces, we need to
3531 explicitly convert them to pointers into the common address space
3532 before we can subtract the numerical address values. */
3535 addr_space_t as_common
;
3538 /* Determine the common superset address space. This is guaranteed
3539 to exist because the caller verified that comp_target_types
3540 returned non-zero. */
3541 if (!addr_space_superset (as0
, as1
, &as_common
))
3544 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3545 op0
= convert (common_type
, op0
);
3546 op1
= convert (common_type
, op1
);
3549 /* Determine integer type to perform computations in. This will usually
3550 be the same as the result type (ptrdiff_t), but may need to be a wider
3551 type if pointers for the address space are wider than ptrdiff_t. */
3552 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3553 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3557 if (TREE_CODE (target_type
) == VOID_TYPE
)
3558 pedwarn (loc
, OPT_Wpointer_arith
,
3559 "pointer of type %<void *%> used in subtraction");
3560 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3561 pedwarn (loc
, OPT_Wpointer_arith
,
3562 "pointer to a function used in subtraction");
3564 /* First do the subtraction as integers;
3565 then drop through to build the divide operator.
3566 Do not do default conversions on the minus operator
3567 in case restype is a short type. */
3569 op0
= build_binary_op (loc
,
3570 MINUS_EXPR
, convert (inttype
, op0
),
3571 convert (inttype
, op1
), 0);
3572 /* This generates an error if op1 is pointer to incomplete type. */
3573 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3574 error_at (loc
, "arithmetic on pointer to an incomplete type");
3576 op1
= c_size_in_bytes (target_type
);
3578 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3579 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3581 /* Divide by the size, in easiest possible way. */
3582 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3583 op0
, convert (inttype
, op1
));
3585 /* Convert to final result type if necessary. */
3586 return convert (restype
, result
);
3589 /* Expand atomic compound assignments into an approriate sequence as
3590 specified by the C11 standard section 6.5.16.2.
3596 This sequence is used for all types for which these operations are
3599 In addition, built-in versions of the 'fe' prefixed routines may
3600 need to be invoked for floating point (real, complex or vector) when
3601 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3611 __atomic_load (addr, &old, SEQ_CST);
3612 feholdexcept (&fenv);
3614 newval = old op val;
3615 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3618 feclearexcept (FE_ALL_EXCEPT);
3621 feupdateenv (&fenv);
3623 Also note that the compiler is simply issuing the generic form of
3624 the atomic operations. This requires temp(s) and has their address
3625 taken. The atomic processing is smart enough to figure out when the
3626 size of an object can utilize a lock-free version, and convert the
3627 built-in call to the appropriate lock-free routine. The optimizers
3628 will then dispose of any temps that are no longer required, and
3629 lock-free implementations are utilized as long as there is target
3630 support for the required size.
3632 If the operator is NOP_EXPR, then this is a simple assignment, and
3633 an __atomic_store is issued to perform the assignment rather than
3638 /* Build an atomic assignment at LOC, expanding into the proper
3639 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3640 the result of the operation, unless RETURN_OLD_P in which case
3641 return the old value of LHS (this is only for postincrement and
3644 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3645 tree rhs
, bool return_old_p
)
3647 tree fndecl
, func_call
;
3648 vec
<tree
, va_gc
> *params
;
3649 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3652 tree stmt
, goto_stmt
;
3653 tree loop_label
, loop_decl
, done_label
, done_decl
;
3655 tree lhs_type
= TREE_TYPE (lhs
);
3656 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, 0);
3657 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3658 tree rhs_type
= TREE_TYPE (rhs
);
3660 gcc_assert (TYPE_ATOMIC (lhs_type
));
3663 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3665 /* Allocate enough vector items for a compare_exchange. */
3666 vec_alloc (params
, 6);
3668 /* Create a compound statement to hold the sequence of statements
3670 compound_stmt
= c_begin_compound_stmt (false);
3672 /* Fold the RHS if it hasn't already been folded. */
3673 if (modifycode
!= NOP_EXPR
)
3674 rhs
= c_fully_fold (rhs
, false, NULL
);
3676 /* Remove the qualifiers for the rest of the expressions and create
3677 the VAL temp variable to hold the RHS. */
3678 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3679 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3680 val
= create_tmp_var_raw (nonatomic_rhs_type
);
3681 TREE_ADDRESSABLE (val
) = 1;
3682 TREE_NO_WARNING (val
) = 1;
3683 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
3685 SET_EXPR_LOCATION (rhs
, loc
);
3688 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3690 if (modifycode
== NOP_EXPR
)
3692 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3693 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, 0);
3694 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3695 params
->quick_push (lhs_addr
);
3696 params
->quick_push (rhs
);
3697 params
->quick_push (seq_cst
);
3698 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3699 add_stmt (func_call
);
3701 /* Finish the compound statement. */
3702 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3704 /* VAL is the value which was stored, return a COMPOUND_STMT of
3705 the statement and that value. */
3706 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3709 /* Create the variables and labels required for the op= form. */
3710 old
= create_tmp_var_raw (nonatomic_lhs_type
);
3711 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, 0);
3712 TREE_ADDRESSABLE (old
) = 1;
3713 TREE_NO_WARNING (old
) = 1;
3715 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
3716 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, 0);
3717 TREE_ADDRESSABLE (newval
) = 1;
3719 loop_decl
= create_artificial_label (loc
);
3720 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
3722 done_decl
= create_artificial_label (loc
);
3723 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
3725 /* __atomic_load (addr, &old, SEQ_CST). */
3726 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
3727 params
->quick_push (lhs_addr
);
3728 params
->quick_push (old_addr
);
3729 params
->quick_push (seq_cst
);
3730 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3731 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
3734 params
->truncate (0);
3736 /* Create the expressions for floating-point environment
3737 manipulation, if required. */
3738 bool need_fenv
= (flag_trapping_math
3739 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
3740 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
3742 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
3745 add_stmt (hold_call
);
3748 add_stmt (loop_label
);
3750 /* newval = old + val; */
3751 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
3752 rhs
= c_fully_fold (rhs
, false, NULL
);
3753 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
3754 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
3756 if (rhs
!= error_mark_node
)
3758 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
3760 SET_EXPR_LOCATION (rhs
, loc
);
3764 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3766 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
3767 params
->quick_push (lhs_addr
);
3768 params
->quick_push (old_addr
);
3769 params
->quick_push (newval_addr
);
3770 params
->quick_push (integer_zero_node
);
3771 params
->quick_push (seq_cst
);
3772 params
->quick_push (seq_cst
);
3773 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3775 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
3776 SET_EXPR_LOCATION (goto_stmt
, loc
);
3778 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
3779 SET_EXPR_LOCATION (stmt
, loc
);
3783 add_stmt (clear_call
);
3786 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
3787 SET_EXPR_LOCATION (goto_stmt
, loc
);
3788 add_stmt (goto_stmt
);
3791 add_stmt (done_label
);
3794 add_stmt (update_call
);
3796 /* Finish the compound statement. */
3797 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3799 /* NEWVAL is the value that was successfully stored, return a
3800 COMPOUND_EXPR of the statement and the appropriate value. */
3801 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
3802 return_old_p
? old
: newval
);
3805 /* Construct and perhaps optimize a tree representation
3806 for a unary operation. CODE, a tree_code, specifies the operation
3807 and XARG is the operand.
3808 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3809 the default promotions (such as from short to int).
3810 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3811 allows non-lvalues; this is only used to handle conversion of non-lvalue
3812 arrays to pointers in C99.
3814 LOCATION is the location of the operator. */
3817 build_unary_op (location_t location
,
3818 enum tree_code code
, tree xarg
, int flag
)
3820 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3823 enum tree_code typecode
;
3825 tree ret
= error_mark_node
;
3826 tree eptype
= NULL_TREE
;
3827 int noconvert
= flag
;
3828 const char *invalid_op_diag
;
3831 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3833 arg
= remove_c_maybe_const_expr (arg
);
3835 if (code
!= ADDR_EXPR
)
3836 arg
= require_complete_type (arg
);
3838 typecode
= TREE_CODE (TREE_TYPE (arg
));
3839 if (typecode
== ERROR_MARK
)
3840 return error_mark_node
;
3841 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3842 typecode
= INTEGER_TYPE
;
3844 if ((invalid_op_diag
3845 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3847 error_at (location
, invalid_op_diag
);
3848 return error_mark_node
;
3851 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3853 eptype
= TREE_TYPE (arg
);
3854 arg
= TREE_OPERAND (arg
, 0);
3860 /* This is used for unary plus, because a CONVERT_EXPR
3861 is enough to prevent anybody from looking inside for
3862 associativity, but won't generate any code. */
3863 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3864 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3865 || typecode
== VECTOR_TYPE
))
3867 error_at (location
, "wrong type argument to unary plus");
3868 return error_mark_node
;
3870 else if (!noconvert
)
3871 arg
= default_conversion (arg
);
3872 arg
= non_lvalue_loc (location
, arg
);
3876 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3877 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3878 || typecode
== VECTOR_TYPE
))
3880 error_at (location
, "wrong type argument to unary minus");
3881 return error_mark_node
;
3883 else if (!noconvert
)
3884 arg
= default_conversion (arg
);
3888 /* ~ works on integer types and non float vectors. */
3889 if (typecode
== INTEGER_TYPE
3890 || (typecode
== VECTOR_TYPE
3891 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3894 arg
= default_conversion (arg
);
3896 else if (typecode
== COMPLEX_TYPE
)
3899 pedwarn (location
, OPT_Wpedantic
,
3900 "ISO C does not support %<~%> for complex conjugation");
3902 arg
= default_conversion (arg
);
3906 error_at (location
, "wrong type argument to bit-complement");
3907 return error_mark_node
;
3912 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3914 error_at (location
, "wrong type argument to abs");
3915 return error_mark_node
;
3917 else if (!noconvert
)
3918 arg
= default_conversion (arg
);
3922 /* Conjugating a real value is a no-op, but allow it anyway. */
3923 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3924 || typecode
== COMPLEX_TYPE
))
3926 error_at (location
, "wrong type argument to conjugation");
3927 return error_mark_node
;
3929 else if (!noconvert
)
3930 arg
= default_conversion (arg
);
3933 case TRUTH_NOT_EXPR
:
3934 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3935 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3936 && typecode
!= COMPLEX_TYPE
)
3939 "wrong type argument to unary exclamation mark");
3940 return error_mark_node
;
3944 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
3945 arg
= remove_c_maybe_const_expr (arg
);
3948 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3949 ret
= invert_truthvalue_loc (location
, arg
);
3950 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3951 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3952 location
= EXPR_LOCATION (ret
);
3953 goto return_build_unary_op
;
3957 ret
= build_real_imag_expr (location
, code
, arg
);
3958 if (ret
== error_mark_node
)
3959 return error_mark_node
;
3960 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3961 eptype
= TREE_TYPE (eptype
);
3962 goto return_build_unary_op
;
3964 case PREINCREMENT_EXPR
:
3965 case POSTINCREMENT_EXPR
:
3966 case PREDECREMENT_EXPR
:
3967 case POSTDECREMENT_EXPR
:
3969 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3971 tree inner
= build_unary_op (location
, code
,
3972 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3973 if (inner
== error_mark_node
)
3974 return error_mark_node
;
3975 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3976 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3977 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3978 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3979 goto return_build_unary_op
;
3982 /* Complain about anything that is not a true lvalue. In
3983 Objective-C, skip this check for property_refs. */
3984 if (!objc_is_property_ref (arg
)
3985 && !lvalue_or_else (location
,
3986 arg
, ((code
== PREINCREMENT_EXPR
3987 || code
== POSTINCREMENT_EXPR
)
3990 return error_mark_node
;
3992 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
3994 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3995 warning_at (location
, OPT_Wc___compat
,
3996 "increment of enumeration value is invalid in C++");
3998 warning_at (location
, OPT_Wc___compat
,
3999 "decrement of enumeration value is invalid in C++");
4002 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4003 arg
= c_fully_fold (arg
, false, NULL
);
4006 atomic_op
= really_atomic_lvalue (arg
);
4008 /* Increment or decrement the real part of the value,
4009 and don't change the imaginary part. */
4010 if (typecode
== COMPLEX_TYPE
)
4014 pedwarn (location
, OPT_Wpedantic
,
4015 "ISO C does not support %<++%> and %<--%> on complex types");
4019 arg
= stabilize_reference (arg
);
4020 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
4021 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
4022 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
4023 if (real
== error_mark_node
|| imag
== error_mark_node
)
4024 return error_mark_node
;
4025 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4027 goto return_build_unary_op
;
4031 /* Report invalid types. */
4033 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4034 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4035 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
4037 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4038 error_at (location
, "wrong type argument to increment");
4040 error_at (location
, "wrong type argument to decrement");
4042 return error_mark_node
;
4048 argtype
= TREE_TYPE (arg
);
4050 /* Compute the increment. */
4052 if (typecode
== POINTER_TYPE
)
4054 /* If pointer target is an incomplete type,
4055 we just cannot know how to do the arithmetic. */
4056 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4058 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4060 "increment of pointer to an incomplete type %qT",
4061 TREE_TYPE (argtype
));
4064 "decrement of pointer to an incomplete type %qT",
4065 TREE_TYPE (argtype
));
4067 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4068 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4070 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4071 pedwarn (location
, OPT_Wpointer_arith
,
4072 "wrong type argument to increment");
4074 pedwarn (location
, OPT_Wpointer_arith
,
4075 "wrong type argument to decrement");
4078 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4079 inc
= convert_to_ptrofftype_loc (location
, inc
);
4081 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4083 /* For signed fract types, we invert ++ to -- or
4084 -- to ++, and change inc from 1 to -1, because
4085 it is not possible to represent 1 in signed fract constants.
4086 For unsigned fract types, the result always overflows and
4087 we get an undefined (original) or the maximum value. */
4088 if (code
== PREINCREMENT_EXPR
)
4089 code
= PREDECREMENT_EXPR
;
4090 else if (code
== PREDECREMENT_EXPR
)
4091 code
= PREINCREMENT_EXPR
;
4092 else if (code
== POSTINCREMENT_EXPR
)
4093 code
= POSTDECREMENT_EXPR
;
4094 else /* code == POSTDECREMENT_EXPR */
4095 code
= POSTINCREMENT_EXPR
;
4097 inc
= integer_minus_one_node
;
4098 inc
= convert (argtype
, inc
);
4102 inc
= VECTOR_TYPE_P (argtype
)
4103 ? build_one_cst (argtype
)
4105 inc
= convert (argtype
, inc
);
4108 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4109 need to ask Objective-C to build the increment or decrement
4110 expression for it. */
4111 if (objc_is_property_ref (arg
))
4112 return objc_build_incr_expr_for_property_ref (location
, code
,
4115 /* Report a read-only lvalue. */
4116 if (TYPE_READONLY (argtype
))
4118 readonly_error (location
, arg
,
4119 ((code
== PREINCREMENT_EXPR
4120 || code
== POSTINCREMENT_EXPR
)
4121 ? lv_increment
: lv_decrement
));
4122 return error_mark_node
;
4124 else if (TREE_READONLY (arg
))
4125 readonly_warning (arg
,
4126 ((code
== PREINCREMENT_EXPR
4127 || code
== POSTINCREMENT_EXPR
)
4128 ? lv_increment
: lv_decrement
));
4130 /* If the argument is atomic, use the special code sequences for
4131 atomic compound assignment. */
4134 arg
= stabilize_reference (arg
);
4135 ret
= build_atomic_assign (location
, arg
,
4136 ((code
== PREINCREMENT_EXPR
4137 || code
== POSTINCREMENT_EXPR
)
4140 (FRACT_MODE_P (TYPE_MODE (argtype
))
4142 : integer_one_node
),
4143 (code
== POSTINCREMENT_EXPR
4144 || code
== POSTDECREMENT_EXPR
));
4145 goto return_build_unary_op
;
4148 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4149 val
= boolean_increment (code
, arg
);
4151 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4152 TREE_SIDE_EFFECTS (val
) = 1;
4153 if (TREE_CODE (val
) != code
)
4154 TREE_NO_WARNING (val
) = 1;
4156 goto return_build_unary_op
;
4160 /* Note that this operation never does default_conversion. */
4162 /* The operand of unary '&' must be an lvalue (which excludes
4163 expressions of type void), or, in C99, the result of a [] or
4164 unary '*' operator. */
4165 if (VOID_TYPE_P (TREE_TYPE (arg
))
4166 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4167 && (TREE_CODE (arg
) != INDIRECT_REF
4169 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4171 /* Let &* cancel out to simplify resulting code. */
4172 if (TREE_CODE (arg
) == INDIRECT_REF
)
4174 /* Don't let this be an lvalue. */
4175 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4176 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4177 ret
= TREE_OPERAND (arg
, 0);
4178 goto return_build_unary_op
;
4181 /* For &x[y], return x+y */
4182 if (TREE_CODE (arg
) == ARRAY_REF
)
4184 tree op0
= TREE_OPERAND (arg
, 0);
4185 if (!c_mark_addressable (op0
))
4186 return error_mark_node
;
4189 /* Anything not already handled and not a true memory reference
4190 or a non-lvalue array is an error. */
4191 else if (typecode
!= FUNCTION_TYPE
&& !flag
4192 && !lvalue_or_else (location
, arg
, lv_addressof
))
4193 return error_mark_node
;
4195 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4197 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4199 tree inner
= build_unary_op (location
, code
,
4200 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
4201 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4202 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4203 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4204 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4205 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4206 goto return_build_unary_op
;
4209 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4210 argtype
= TREE_TYPE (arg
);
4212 /* If the lvalue is const or volatile, merge that into the type
4213 to which the address will point. This is only needed
4214 for function types. */
4215 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4216 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4217 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4219 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4220 int quals
= orig_quals
;
4222 if (TREE_READONLY (arg
))
4223 quals
|= TYPE_QUAL_CONST
;
4224 if (TREE_THIS_VOLATILE (arg
))
4225 quals
|= TYPE_QUAL_VOLATILE
;
4227 argtype
= c_build_qualified_type (argtype
, quals
);
4230 if (!c_mark_addressable (arg
))
4231 return error_mark_node
;
4233 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4234 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4236 argtype
= build_pointer_type (argtype
);
4238 /* ??? Cope with user tricks that amount to offsetof. Delete this
4239 when we have proper support for integer constant expressions. */
4240 val
= get_base_address (arg
);
4241 if (val
&& TREE_CODE (val
) == INDIRECT_REF
4242 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4244 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4245 goto return_build_unary_op
;
4248 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4251 goto return_build_unary_op
;
4258 argtype
= TREE_TYPE (arg
);
4259 if (TREE_CODE (arg
) == INTEGER_CST
)
4260 ret
= (require_constant_value
4261 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4262 : fold_build1_loc (location
, code
, argtype
, arg
));
4264 ret
= build1 (code
, argtype
, arg
);
4265 return_build_unary_op
:
4266 gcc_assert (ret
!= error_mark_node
);
4267 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4268 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4269 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4270 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4271 ret
= note_integer_operands (ret
);
4273 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4274 protected_set_expr_location (ret
, location
);
4278 /* Return nonzero if REF is an lvalue valid for this language.
4279 Lvalues can be assigned, unless their type has TYPE_READONLY.
4280 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4283 lvalue_p (const_tree ref
)
4285 const enum tree_code code
= TREE_CODE (ref
);
4292 return lvalue_p (TREE_OPERAND (ref
, 0));
4294 case C_MAYBE_CONST_EXPR
:
4295 return lvalue_p (TREE_OPERAND (ref
, 1));
4297 case COMPOUND_LITERAL_EXPR
:
4303 case ARRAY_NOTATION_REF
:
4308 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4309 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4312 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4319 /* Give a warning for storing in something that is read-only in GCC
4320 terms but not const in ISO C terms. */
4323 readonly_warning (tree arg
, enum lvalue_use use
)
4328 warning (0, "assignment of read-only location %qE", arg
);
4331 warning (0, "increment of read-only location %qE", arg
);
4334 warning (0, "decrement of read-only location %qE", arg
);
4343 /* Return nonzero if REF is an lvalue valid for this language;
4344 otherwise, print an error message and return zero. USE says
4345 how the lvalue is being used and so selects the error message.
4346 LOCATION is the location at which any error should be reported. */
4349 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4351 int win
= lvalue_p (ref
);
4354 lvalue_error (loc
, use
);
4359 /* Mark EXP saying that we need to be able to take the
4360 address of it; it should not be allocated in a register.
4361 Returns true if successful. */
4364 c_mark_addressable (tree exp
)
4369 switch (TREE_CODE (x
))
4372 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
4375 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
4379 /* ... fall through ... */
4385 x
= TREE_OPERAND (x
, 0);
4388 case COMPOUND_LITERAL_EXPR
:
4390 TREE_ADDRESSABLE (x
) = 1;
4397 if (C_DECL_REGISTER (x
)
4398 && DECL_NONLOCAL (x
))
4400 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4403 ("global register variable %qD used in nested function", x
);
4406 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4408 else if (C_DECL_REGISTER (x
))
4410 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4411 error ("address of global register variable %qD requested", x
);
4413 error ("address of register variable %qD requested", x
);
4419 TREE_ADDRESSABLE (x
) = 1;
4426 /* Convert EXPR to TYPE, warning about conversion problems with
4427 constants. SEMANTIC_TYPE is the type this conversion would use
4428 without excess precision. If SEMANTIC_TYPE is NULL, this function
4429 is equivalent to convert_and_check. This function is a wrapper that
4430 handles conversions that may be different than
4431 the usual ones because of excess precision. */
4434 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4437 if (TREE_TYPE (expr
) == type
)
4441 return convert_and_check (loc
, type
, expr
);
4443 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4444 && TREE_TYPE (expr
) != semantic_type
)
4446 /* For integers, we need to check the real conversion, not
4447 the conversion to the excess precision type. */
4448 expr
= convert_and_check (loc
, semantic_type
, expr
);
4450 /* Result type is the excess precision type, which should be
4451 large enough, so do not check. */
4452 return convert (type
, expr
);
4455 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4456 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4457 if folded to an integer constant then the unselected half may
4458 contain arbitrary operations not normally permitted in constant
4459 expressions. Set the location of the expression to LOC. */
4462 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4463 tree op1
, tree op1_original_type
, tree op2
,
4464 tree op2_original_type
)
4468 enum tree_code code1
;
4469 enum tree_code code2
;
4470 tree result_type
= NULL
;
4471 tree semantic_result_type
= NULL
;
4472 tree orig_op1
= op1
, orig_op2
= op2
;
4473 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4474 bool ifexp_int_operands
;
4477 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4478 if (op1_int_operands
)
4479 op1
= remove_c_maybe_const_expr (op1
);
4480 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4481 if (op2_int_operands
)
4482 op2
= remove_c_maybe_const_expr (op2
);
4483 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4484 if (ifexp_int_operands
)
4485 ifexp
= remove_c_maybe_const_expr (ifexp
);
4487 /* Promote both alternatives. */
4489 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4490 op1
= default_conversion (op1
);
4491 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4492 op2
= default_conversion (op2
);
4494 if (TREE_CODE (ifexp
) == ERROR_MARK
4495 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4496 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4497 return error_mark_node
;
4499 type1
= TREE_TYPE (op1
);
4500 code1
= TREE_CODE (type1
);
4501 type2
= TREE_TYPE (op2
);
4502 code2
= TREE_CODE (type2
);
4504 /* C90 does not permit non-lvalue arrays in conditional expressions.
4505 In C99 they will be pointers by now. */
4506 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4508 error_at (colon_loc
, "non-lvalue array in conditional expression");
4509 return error_mark_node
;
4512 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4513 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4514 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4515 || code1
== COMPLEX_TYPE
)
4516 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4517 || code2
== COMPLEX_TYPE
))
4519 semantic_result_type
= c_common_type (type1
, type2
);
4520 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4522 op1
= TREE_OPERAND (op1
, 0);
4523 type1
= TREE_TYPE (op1
);
4524 gcc_assert (TREE_CODE (type1
) == code1
);
4526 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4528 op2
= TREE_OPERAND (op2
, 0);
4529 type2
= TREE_TYPE (op2
);
4530 gcc_assert (TREE_CODE (type2
) == code2
);
4534 if (warn_cxx_compat
)
4536 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4537 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4539 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4540 && TREE_CODE (t2
) == ENUMERAL_TYPE
4541 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4542 warning_at (colon_loc
, OPT_Wc___compat
,
4543 ("different enum types in conditional is "
4544 "invalid in C++: %qT vs %qT"),
4548 /* Quickly detect the usual case where op1 and op2 have the same type
4550 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4553 result_type
= type1
;
4555 result_type
= TYPE_MAIN_VARIANT (type1
);
4557 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4558 || code1
== COMPLEX_TYPE
)
4559 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4560 || code2
== COMPLEX_TYPE
))
4562 result_type
= c_common_type (type1
, type2
);
4563 do_warn_double_promotion (result_type
, type1
, type2
,
4564 "implicit conversion from %qT to %qT to "
4565 "match other result of conditional",
4568 /* If -Wsign-compare, warn here if type1 and type2 have
4569 different signedness. We'll promote the signed to unsigned
4570 and later code won't know it used to be different.
4571 Do this check on the original types, so that explicit casts
4572 will be considered, but default promotions won't. */
4573 if (c_inhibit_evaluation_warnings
== 0)
4575 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4576 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4578 if (unsigned_op1
^ unsigned_op2
)
4582 /* Do not warn if the result type is signed, since the
4583 signed type will only be chosen if it can represent
4584 all the values of the unsigned type. */
4585 if (!TYPE_UNSIGNED (result_type
))
4589 bool op1_maybe_const
= true;
4590 bool op2_maybe_const
= true;
4592 /* Do not warn if the signed quantity is an
4593 unsuffixed integer literal (or some static
4594 constant expression involving such literals) and
4595 it is non-negative. This warning requires the
4596 operands to be folded for best results, so do
4597 that folding in this case even without
4598 warn_sign_compare to avoid warning options
4599 possibly affecting code generation. */
4600 c_inhibit_evaluation_warnings
4601 += (ifexp
== truthvalue_false_node
);
4602 op1
= c_fully_fold (op1
, require_constant_value
,
4604 c_inhibit_evaluation_warnings
4605 -= (ifexp
== truthvalue_false_node
);
4607 c_inhibit_evaluation_warnings
4608 += (ifexp
== truthvalue_true_node
);
4609 op2
= c_fully_fold (op2
, require_constant_value
,
4611 c_inhibit_evaluation_warnings
4612 -= (ifexp
== truthvalue_true_node
);
4614 if (warn_sign_compare
)
4617 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4619 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4622 warning_at (colon_loc
, OPT_Wsign_compare
,
4623 ("signed and unsigned type in "
4624 "conditional expression"));
4626 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4627 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4628 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4629 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4634 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4636 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4637 pedwarn (colon_loc
, OPT_Wpedantic
,
4638 "ISO C forbids conditional expr with only one void side");
4639 result_type
= void_type_node
;
4641 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4643 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
4644 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
4645 addr_space_t as_common
;
4647 if (comp_target_types (colon_loc
, type1
, type2
))
4648 result_type
= common_pointer_type (type1
, type2
);
4649 else if (null_pointer_constant_p (orig_op1
))
4650 result_type
= type2
;
4651 else if (null_pointer_constant_p (orig_op2
))
4652 result_type
= type1
;
4653 else if (!addr_space_superset (as1
, as2
, &as_common
))
4655 error_at (colon_loc
, "pointers to disjoint address spaces "
4656 "used in conditional expression");
4657 return error_mark_node
;
4659 else if (VOID_TYPE_P (TREE_TYPE (type1
))
4660 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
4662 if ((TREE_CODE (TREE_TYPE (type2
)) == ARRAY_TYPE
)
4663 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2
)))
4664 & ~TYPE_QUALS (TREE_TYPE (type1
))))
4665 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
4666 "pointer to array loses qualifier "
4667 "in conditional expression");
4669 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
4670 pedwarn (colon_loc
, OPT_Wpedantic
,
4671 "ISO C forbids conditional expr between "
4672 "%<void *%> and function pointer");
4673 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
4674 TREE_TYPE (type2
)));
4676 else if (VOID_TYPE_P (TREE_TYPE (type2
))
4677 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
4679 if ((TREE_CODE (TREE_TYPE (type1
)) == ARRAY_TYPE
)
4680 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1
)))
4681 & ~TYPE_QUALS (TREE_TYPE (type2
))))
4682 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
4683 "pointer to array loses qualifier "
4684 "in conditional expression");
4686 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
4687 pedwarn (colon_loc
, OPT_Wpedantic
,
4688 "ISO C forbids conditional expr between "
4689 "%<void *%> and function pointer");
4690 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
4691 TREE_TYPE (type1
)));
4693 /* Objective-C pointer comparisons are a bit more lenient. */
4694 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
4695 result_type
= objc_common_type (type1
, type2
);
4698 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
4700 pedwarn (colon_loc
, 0,
4701 "pointer type mismatch in conditional expression");
4702 result_type
= build_pointer_type
4703 (build_qualified_type (void_type_node
, qual
));
4706 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4708 if (!null_pointer_constant_p (orig_op2
))
4709 pedwarn (colon_loc
, 0,
4710 "pointer/integer type mismatch in conditional expression");
4713 op2
= null_pointer_node
;
4715 result_type
= type1
;
4717 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4719 if (!null_pointer_constant_p (orig_op1
))
4720 pedwarn (colon_loc
, 0,
4721 "pointer/integer type mismatch in conditional expression");
4724 op1
= null_pointer_node
;
4726 result_type
= type2
;
4731 if (flag_cond_mismatch
)
4732 result_type
= void_type_node
;
4735 error_at (colon_loc
, "type mismatch in conditional expression");
4736 return error_mark_node
;
4740 /* Merge const and volatile flags of the incoming types. */
4742 = build_type_variant (result_type
,
4743 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4744 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4746 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
4747 semantic_result_type
);
4748 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
4749 semantic_result_type
);
4751 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4753 op2_int_operands
= true;
4754 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4756 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4758 op1_int_operands
= true;
4759 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4761 int_const
= int_operands
= (ifexp_int_operands
4763 && op2_int_operands
);
4766 int_const
= ((ifexp
== truthvalue_true_node
4767 && TREE_CODE (orig_op1
) == INTEGER_CST
4768 && !TREE_OVERFLOW (orig_op1
))
4769 || (ifexp
== truthvalue_false_node
4770 && TREE_CODE (orig_op2
) == INTEGER_CST
4771 && !TREE_OVERFLOW (orig_op2
)));
4773 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4774 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4779 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4780 nested inside of the expression. */
4781 op1
= c_fully_fold (op1
, false, NULL
);
4782 op2
= c_fully_fold (op2
, false, NULL
);
4784 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4786 ret
= note_integer_operands (ret
);
4788 if (semantic_result_type
)
4789 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
4791 protected_set_expr_location (ret
, colon_loc
);
4795 /* Return a compound expression that performs two expressions and
4796 returns the value of the second of them.
4798 LOC is the location of the COMPOUND_EXPR. */
4801 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4803 bool expr1_int_operands
, expr2_int_operands
;
4804 tree eptype
= NULL_TREE
;
4808 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
4809 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
4812 "spawned function call cannot be part of a comma expression");
4813 return error_mark_node
;
4815 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4816 if (expr1_int_operands
)
4817 expr1
= remove_c_maybe_const_expr (expr1
);
4818 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4819 if (expr2_int_operands
)
4820 expr2
= remove_c_maybe_const_expr (expr2
);
4822 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4823 expr1
= TREE_OPERAND (expr1
, 0);
4824 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4826 eptype
= TREE_TYPE (expr2
);
4827 expr2
= TREE_OPERAND (expr2
, 0);
4830 if (!TREE_SIDE_EFFECTS (expr1
))
4832 /* The left-hand operand of a comma expression is like an expression
4833 statement: with -Wunused, we should warn if it doesn't have
4834 any side-effects, unless it was explicitly cast to (void). */
4835 if (warn_unused_value
)
4837 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4838 && CONVERT_EXPR_P (expr1
))
4840 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4841 && TREE_CODE (expr1
) == COMPOUND_EXPR
4842 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4843 ; /* (void) a, (void) b, c */
4845 warning_at (loc
, OPT_Wunused_value
,
4846 "left-hand operand of comma expression has no effect");
4849 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
4850 && warn_unused_value
)
4853 location_t cloc
= loc
;
4854 while (TREE_CODE (r
) == COMPOUND_EXPR
)
4856 if (EXPR_HAS_LOCATION (r
))
4857 cloc
= EXPR_LOCATION (r
);
4858 r
= TREE_OPERAND (r
, 1);
4860 if (!TREE_SIDE_EFFECTS (r
)
4861 && !VOID_TYPE_P (TREE_TYPE (r
))
4862 && !CONVERT_EXPR_P (r
))
4863 warning_at (cloc
, OPT_Wunused_value
,
4864 "right-hand operand of comma expression has no effect");
4867 /* With -Wunused, we should also warn if the left-hand operand does have
4868 side-effects, but computes a value which is not used. For example, in
4869 `foo() + bar(), baz()' the result of the `+' operator is not used,
4870 so we should issue a warning. */
4871 else if (warn_unused_value
)
4872 warn_if_unused_value (expr1
, loc
);
4874 if (expr2
== error_mark_node
)
4875 return error_mark_node
;
4877 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4880 && expr1_int_operands
4881 && expr2_int_operands
)
4882 ret
= note_integer_operands (ret
);
4885 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4887 protected_set_expr_location (ret
, loc
);
4891 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4892 which we are casting. OTYPE is the type of the expression being
4893 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4894 of the cast. -Wcast-qual appeared on the command line. Named
4895 address space qualifiers are not handled here, because they result
4896 in different warnings. */
4899 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
4901 tree in_type
= type
;
4902 tree in_otype
= otype
;
4907 /* Check that the qualifiers on IN_TYPE are a superset of the
4908 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4909 nodes is uninteresting and we stop as soon as we hit a
4910 non-POINTER_TYPE node on either type. */
4913 in_otype
= TREE_TYPE (in_otype
);
4914 in_type
= TREE_TYPE (in_type
);
4916 /* GNU C allows cv-qualified function types. 'const' means the
4917 function is very pure, 'volatile' means it can't return. We
4918 need to warn when such qualifiers are added, not when they're
4920 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4921 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4922 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
4923 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
4925 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
4926 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
4928 while (TREE_CODE (in_type
) == POINTER_TYPE
4929 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4932 warning_at (loc
, OPT_Wcast_qual
,
4933 "cast adds %q#v qualifier to function type", added
);
4936 /* There are qualifiers present in IN_OTYPE that are not present
4938 warning_at (loc
, OPT_Wcast_qual
,
4939 "cast discards %qv qualifier from pointer target type",
4942 if (added
|| discarded
)
4945 /* A cast from **T to const **T is unsafe, because it can cause a
4946 const value to be changed with no additional warning. We only
4947 issue this warning if T is the same on both sides, and we only
4948 issue the warning if there are the same number of pointers on
4949 both sides, as otherwise the cast is clearly unsafe anyhow. A
4950 cast is unsafe when a qualifier is added at one level and const
4951 is not present at all outer levels.
4953 To issue this warning, we check at each level whether the cast
4954 adds new qualifiers not already seen. We don't need to special
4955 case function types, as they won't have the same
4956 TYPE_MAIN_VARIANT. */
4958 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4960 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4965 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4968 in_type
= TREE_TYPE (in_type
);
4969 in_otype
= TREE_TYPE (in_otype
);
4970 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4973 warning_at (loc
, OPT_Wcast_qual
,
4974 "to be safe all intermediate pointers in cast from "
4975 "%qT to %qT must be %<const%> qualified",
4980 is_const
= TYPE_READONLY (in_type
);
4982 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4985 /* Build an expression representing a cast to type TYPE of expression EXPR.
4986 LOC is the location of the cast-- typically the open paren of the cast. */
4989 build_c_cast (location_t loc
, tree type
, tree expr
)
4993 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4994 expr
= TREE_OPERAND (expr
, 0);
4998 if (type
== error_mark_node
|| expr
== error_mark_node
)
4999 return error_mark_node
;
5001 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5002 only in <protocol> qualifications. But when constructing cast expressions,
5003 the protocols do matter and must be kept around. */
5004 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
5005 return build1 (NOP_EXPR
, type
, expr
);
5007 type
= TYPE_MAIN_VARIANT (type
);
5009 if (TREE_CODE (type
) == ARRAY_TYPE
)
5011 error_at (loc
, "cast specifies array type");
5012 return error_mark_node
;
5015 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5017 error_at (loc
, "cast specifies function type");
5018 return error_mark_node
;
5021 if (!VOID_TYPE_P (type
))
5023 value
= require_complete_type (value
);
5024 if (value
== error_mark_node
)
5025 return error_mark_node
;
5028 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
5030 if (TREE_CODE (type
) == RECORD_TYPE
5031 || TREE_CODE (type
) == UNION_TYPE
)
5032 pedwarn (loc
, OPT_Wpedantic
,
5033 "ISO C forbids casting nonscalar to the same type");
5035 /* Convert to remove any qualifiers from VALUE's type. */
5036 value
= convert (type
, value
);
5038 else if (TREE_CODE (type
) == UNION_TYPE
)
5042 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5043 if (TREE_TYPE (field
) != error_mark_node
5044 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
5045 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
5051 bool maybe_const
= true;
5053 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
5054 t
= c_fully_fold (value
, false, &maybe_const
);
5055 t
= build_constructor_single (type
, field
, t
);
5057 t
= c_wrap_maybe_const (t
, true);
5058 t
= digest_init (loc
, type
, t
,
5059 NULL_TREE
, false, true, 0);
5060 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
5063 error_at (loc
, "cast to union type from type not present in union");
5064 return error_mark_node
;
5070 if (type
== void_type_node
)
5072 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5073 SET_EXPR_LOCATION (t
, loc
);
5077 otype
= TREE_TYPE (value
);
5079 /* Optionally warn about potentially worrisome casts. */
5081 && TREE_CODE (type
) == POINTER_TYPE
5082 && TREE_CODE (otype
) == POINTER_TYPE
)
5083 handle_warn_cast_qual (loc
, type
, otype
);
5085 /* Warn about conversions between pointers to disjoint
5087 if (TREE_CODE (type
) == POINTER_TYPE
5088 && TREE_CODE (otype
) == POINTER_TYPE
5089 && !null_pointer_constant_p (value
))
5091 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5092 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5093 addr_space_t as_common
;
5095 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5097 if (ADDR_SPACE_GENERIC_P (as_from
))
5098 warning_at (loc
, 0, "cast to %s address space pointer "
5099 "from disjoint generic address space pointer",
5100 c_addr_space_name (as_to
));
5102 else if (ADDR_SPACE_GENERIC_P (as_to
))
5103 warning_at (loc
, 0, "cast to generic address space pointer "
5104 "from disjoint %s address space pointer",
5105 c_addr_space_name (as_from
));
5108 warning_at (loc
, 0, "cast to %s address space pointer "
5109 "from disjoint %s address space pointer",
5110 c_addr_space_name (as_to
),
5111 c_addr_space_name (as_from
));
5115 /* Warn about possible alignment problems. */
5116 if (STRICT_ALIGNMENT
5117 && TREE_CODE (type
) == POINTER_TYPE
5118 && TREE_CODE (otype
) == POINTER_TYPE
5119 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5120 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5121 /* Don't warn about opaque types, where the actual alignment
5122 restriction is unknown. */
5123 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
5124 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
5125 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5126 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5127 warning_at (loc
, OPT_Wcast_align
,
5128 "cast increases required alignment of target type");
5130 if (TREE_CODE (type
) == INTEGER_TYPE
5131 && TREE_CODE (otype
) == POINTER_TYPE
5132 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5133 /* Unlike conversion of integers to pointers, where the
5134 warning is disabled for converting constants because
5135 of cases such as SIG_*, warn about converting constant
5136 pointers to integers. In some cases it may cause unwanted
5137 sign extension, and a warning is appropriate. */
5138 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5139 "cast from pointer to integer of different size");
5141 if (TREE_CODE (value
) == CALL_EXPR
5142 && TREE_CODE (type
) != TREE_CODE (otype
))
5143 warning_at (loc
, OPT_Wbad_function_cast
,
5144 "cast from function call of type %qT "
5145 "to non-matching type %qT", otype
, type
);
5147 if (TREE_CODE (type
) == POINTER_TYPE
5148 && TREE_CODE (otype
) == INTEGER_TYPE
5149 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5150 /* Don't warn about converting any constant. */
5151 && !TREE_CONSTANT (value
))
5153 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5154 "of different size");
5156 if (warn_strict_aliasing
<= 2)
5157 strict_aliasing_warning (otype
, type
, expr
);
5159 /* If pedantic, warn for conversions between function and object
5160 pointer types, except for converting a null pointer constant
5161 to function pointer type. */
5163 && TREE_CODE (type
) == POINTER_TYPE
5164 && TREE_CODE (otype
) == POINTER_TYPE
5165 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5166 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5167 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5168 "conversion of function pointer to object pointer type");
5171 && TREE_CODE (type
) == POINTER_TYPE
5172 && TREE_CODE (otype
) == POINTER_TYPE
5173 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5174 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5175 && !null_pointer_constant_p (value
))
5176 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5177 "conversion of object pointer to function pointer type");
5180 value
= convert (type
, value
);
5182 /* Ignore any integer overflow caused by the cast. */
5183 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5185 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5187 if (!TREE_OVERFLOW (value
))
5189 /* Avoid clobbering a shared constant. */
5190 value
= copy_node (value
);
5191 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5194 else if (TREE_OVERFLOW (value
))
5195 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5196 value
= wide_int_to_tree (TREE_TYPE (value
), value
);
5200 /* Don't let a cast be an lvalue. */
5202 value
= non_lvalue_loc (loc
, value
);
5204 /* Don't allow the results of casting to floating-point or complex
5205 types be confused with actual constants, or casts involving
5206 integer and pointer types other than direct integer-to-integer
5207 and integer-to-pointer be confused with integer constant
5208 expressions and null pointer constants. */
5209 if (TREE_CODE (value
) == REAL_CST
5210 || TREE_CODE (value
) == COMPLEX_CST
5211 || (TREE_CODE (value
) == INTEGER_CST
5212 && !((TREE_CODE (expr
) == INTEGER_CST
5213 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5214 || TREE_CODE (expr
) == REAL_CST
5215 || TREE_CODE (expr
) == COMPLEX_CST
)))
5216 value
= build1 (NOP_EXPR
, type
, value
);
5218 if (CAN_HAVE_LOCATION_P (value
))
5219 SET_EXPR_LOCATION (value
, loc
);
5223 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5224 location of the open paren of the cast, or the position of the cast
5227 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5230 tree type_expr
= NULL_TREE
;
5231 bool type_expr_const
= true;
5233 int saved_wsp
= warn_strict_prototypes
;
5235 /* This avoids warnings about unprototyped casts on
5236 integers. E.g. "#define SIG_DFL (void(*)())0". */
5237 if (TREE_CODE (expr
) == INTEGER_CST
)
5238 warn_strict_prototypes
= 0;
5239 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5240 warn_strict_prototypes
= saved_wsp
;
5242 ret
= build_c_cast (loc
, type
, expr
);
5245 bool inner_expr_const
= true;
5246 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5247 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5248 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5249 && inner_expr_const
);
5250 SET_EXPR_LOCATION (ret
, loc
);
5253 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
5254 SET_EXPR_LOCATION (ret
, loc
);
5256 /* C++ does not permits types to be defined in a cast, but it
5257 allows references to incomplete types. */
5258 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5259 warning_at (loc
, OPT_Wc___compat
,
5260 "defining a type in a cast is invalid in C++");
5265 /* Build an assignment expression of lvalue LHS from value RHS.
5266 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5267 may differ from TREE_TYPE (LHS) for an enum bitfield.
5268 MODIFYCODE is the code for a binary operator that we use
5269 to combine the old value of LHS with RHS to get the new value.
5270 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5271 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5272 which may differ from TREE_TYPE (RHS) for an enum value.
5274 LOCATION is the location of the MODIFYCODE operator.
5275 RHS_LOC is the location of the RHS. */
5278 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5279 enum tree_code modifycode
,
5280 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5284 tree rhseval
= NULL_TREE
;
5285 tree rhs_semantic_type
= NULL_TREE
;
5286 tree lhstype
= TREE_TYPE (lhs
);
5287 tree olhstype
= lhstype
;
5291 /* Types that aren't fully specified cannot be used in assignments. */
5292 lhs
= require_complete_type (lhs
);
5294 /* Avoid duplicate error messages from operands that had errors. */
5295 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5296 return error_mark_node
;
5298 /* Ensure an error for assigning a non-lvalue array to an array in
5300 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5302 error_at (location
, "assignment to expression with array type");
5303 return error_mark_node
;
5306 /* For ObjC properties, defer this check. */
5307 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5308 return error_mark_node
;
5310 is_atomic_op
= really_atomic_lvalue (lhs
);
5312 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5314 rhs_semantic_type
= TREE_TYPE (rhs
);
5315 rhs
= TREE_OPERAND (rhs
, 0);
5320 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5322 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5323 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5325 if (inner
== error_mark_node
)
5326 return error_mark_node
;
5327 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5328 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5329 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5330 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5331 protected_set_expr_location (result
, location
);
5335 /* If a binary op has been requested, combine the old LHS value with the RHS
5336 producing the value we should actually store into the LHS. */
5338 if (modifycode
!= NOP_EXPR
)
5340 lhs
= c_fully_fold (lhs
, false, NULL
);
5341 lhs
= stabilize_reference (lhs
);
5343 /* Construct the RHS for any non-atomic compound assignemnt. */
5346 /* If in LHS op= RHS the RHS has side-effects, ensure they
5347 are preevaluated before the rest of the assignment expression's
5348 side-effects, because RHS could contain e.g. function calls
5350 if (TREE_SIDE_EFFECTS (rhs
))
5352 newrhs
= in_late_binary_op
? save_expr (rhs
) : c_save_expr (rhs
);
5355 newrhs
= build_binary_op (location
,
5356 modifycode
, lhs
, newrhs
, 1);
5358 /* The original type of the right hand side is no longer
5360 rhs_origtype
= NULL_TREE
;
5364 if (c_dialect_objc ())
5366 /* Check if we are modifying an Objective-C property reference;
5367 if so, we need to generate setter calls. */
5368 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5372 /* Else, do the check that we postponed for Objective-C. */
5373 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5374 return error_mark_node
;
5377 /* Give an error for storing in something that is 'const'. */
5379 if (TYPE_READONLY (lhstype
)
5380 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5381 || TREE_CODE (lhstype
) == UNION_TYPE
)
5382 && C_TYPE_FIELDS_READONLY (lhstype
)))
5384 readonly_error (location
, lhs
, lv_assign
);
5385 return error_mark_node
;
5387 else if (TREE_READONLY (lhs
))
5388 readonly_warning (lhs
, lv_assign
);
5390 /* If storing into a structure or union member,
5391 it has probably been given type `int'.
5392 Compute the type that would go with
5393 the actual amount of storage the member occupies. */
5395 if (TREE_CODE (lhs
) == COMPONENT_REF
5396 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5397 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5398 || TREE_CODE (lhstype
) == REAL_TYPE
5399 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5400 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5402 /* If storing in a field that is in actuality a short or narrower than one,
5403 we must store in the field in its actual type. */
5405 if (lhstype
!= TREE_TYPE (lhs
))
5407 lhs
= copy_node (lhs
);
5408 TREE_TYPE (lhs
) = lhstype
;
5411 /* Issue -Wc++-compat warnings about an assignment to an enum type
5412 when LHS does not have its original type. This happens for,
5413 e.g., an enum bitfield in a struct. */
5415 && lhs_origtype
!= NULL_TREE
5416 && lhs_origtype
!= lhstype
5417 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5419 tree checktype
= (rhs_origtype
!= NULL_TREE
5422 if (checktype
!= error_mark_node
5423 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5424 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5425 warning_at (location
, OPT_Wc___compat
,
5426 "enum conversion in assignment is invalid in C++");
5429 /* If the lhs is atomic, remove that qualifier. */
5432 lhstype
= build_qualified_type (lhstype
,
5433 (TYPE_QUALS (lhstype
)
5434 & ~TYPE_QUAL_ATOMIC
));
5435 olhstype
= build_qualified_type (olhstype
,
5436 (TYPE_QUALS (lhstype
)
5437 & ~TYPE_QUAL_ATOMIC
));
5440 /* Convert new value to destination type. Fold it first, then
5441 restore any excess precision information, for the sake of
5442 conversion warnings. */
5444 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5446 npc
= null_pointer_constant_p (newrhs
);
5447 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5448 if (rhs_semantic_type
)
5449 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5450 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
5451 rhs_origtype
, ic_assign
, npc
,
5452 NULL_TREE
, NULL_TREE
, 0);
5453 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5454 return error_mark_node
;
5457 /* Emit ObjC write barrier, if necessary. */
5458 if (c_dialect_objc () && flag_objc_gc
)
5460 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5463 protected_set_expr_location (result
, location
);
5468 /* Scan operands. */
5471 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5474 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5475 TREE_SIDE_EFFECTS (result
) = 1;
5476 protected_set_expr_location (result
, location
);
5479 /* If we got the LHS in a different type for storing in,
5480 convert the result back to the nominal type of LHS
5481 so that the value we return always has the same type
5482 as the LHS argument. */
5484 if (olhstype
== TREE_TYPE (result
))
5487 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
5488 rhs_origtype
, ic_assign
, false, NULL_TREE
,
5490 protected_set_expr_location (result
, location
);
5494 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
5498 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5499 This is used to implement -fplan9-extensions. */
5502 find_anonymous_field_with_type (tree struct_type
, tree type
)
5507 gcc_assert (TREE_CODE (struct_type
) == RECORD_TYPE
5508 || TREE_CODE (struct_type
) == UNION_TYPE
);
5510 for (field
= TYPE_FIELDS (struct_type
);
5512 field
= TREE_CHAIN (field
))
5514 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5515 ? c_build_qualified_type (TREE_TYPE (field
),
5517 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5518 if (DECL_NAME (field
) == NULL
5519 && comptypes (type
, fieldtype
))
5525 else if (DECL_NAME (field
) == NULL
5526 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
5527 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5528 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5538 /* RHS is an expression whose type is pointer to struct. If there is
5539 an anonymous field in RHS with type TYPE, then return a pointer to
5540 that field in RHS. This is used with -fplan9-extensions. This
5541 returns NULL if no conversion could be found. */
5544 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5546 tree rhs_struct_type
, lhs_main_type
;
5547 tree field
, found_field
;
5548 bool found_sub_field
;
5551 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5552 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5553 gcc_assert (TREE_CODE (rhs_struct_type
) == RECORD_TYPE
5554 || TREE_CODE (rhs_struct_type
) == UNION_TYPE
);
5556 gcc_assert (POINTER_TYPE_P (type
));
5557 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5558 ? c_build_qualified_type (TREE_TYPE (type
),
5560 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5562 found_field
= NULL_TREE
;
5563 found_sub_field
= false;
5564 for (field
= TYPE_FIELDS (rhs_struct_type
);
5566 field
= TREE_CHAIN (field
))
5568 if (DECL_NAME (field
) != NULL_TREE
5569 || (TREE_CODE (TREE_TYPE (field
)) != RECORD_TYPE
5570 && TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
))
5572 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5573 ? c_build_qualified_type (TREE_TYPE (field
),
5575 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5576 if (comptypes (lhs_main_type
, fieldtype
))
5578 if (found_field
!= NULL_TREE
)
5580 found_field
= field
;
5582 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5585 if (found_field
!= NULL_TREE
)
5587 found_field
= field
;
5588 found_sub_field
= true;
5592 if (found_field
== NULL_TREE
)
5595 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5596 build_fold_indirect_ref (rhs
), found_field
,
5598 ret
= build_fold_addr_expr_loc (location
, ret
);
5600 if (found_sub_field
)
5602 ret
= convert_to_anonymous_field (location
, type
, ret
);
5603 gcc_assert (ret
!= NULL_TREE
);
5609 /* Issue an error message for a bad initializer component.
5610 GMSGID identifies the message.
5611 The component name is taken from the spelling stack. */
5614 error_init (location_t loc
, const char *gmsgid
)
5618 /* The gmsgid may be a format string with %< and %>. */
5619 error_at (loc
, gmsgid
);
5620 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5622 inform (loc
, "(near initialization for %qs)", ofwhat
);
5625 /* Issue a pedantic warning for a bad initializer component. OPT is
5626 the option OPT_* (from options.h) controlling this warning or 0 if
5627 it is unconditionally given. GMSGID identifies the message. The
5628 component name is taken from the spelling stack. */
5631 pedwarn_init (location_t location
, int opt
, const char *gmsgid
)
5636 /* The gmsgid may be a format string with %< and %>. */
5637 warned
= pedwarn (location
, opt
, gmsgid
);
5638 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5639 if (*ofwhat
&& warned
)
5640 inform (location
, "(near initialization for %qs)", ofwhat
);
5643 /* Issue a warning for a bad initializer component.
5645 OPT is the OPT_W* value corresponding to the warning option that
5646 controls this warning. GMSGID identifies the message. The
5647 component name is taken from the spelling stack. */
5650 warning_init (location_t loc
, int opt
, const char *gmsgid
)
5655 /* The gmsgid may be a format string with %< and %>. */
5656 warned
= warning_at (loc
, opt
, gmsgid
);
5657 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5658 if (*ofwhat
&& warned
)
5659 inform (loc
, "(near initialization for %qs)", ofwhat
);
5662 /* If TYPE is an array type and EXPR is a parenthesized string
5663 constant, warn if pedantic that EXPR is being used to initialize an
5664 object of type TYPE. */
5667 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
5670 && TREE_CODE (type
) == ARRAY_TYPE
5671 && TREE_CODE (expr
.value
) == STRING_CST
5672 && expr
.original_code
!= STRING_CST
)
5673 pedwarn_init (loc
, OPT_Wpedantic
,
5674 "array initialized from parenthesized string constant");
5677 /* Convert value RHS to type TYPE as preparation for an assignment to
5678 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5679 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5680 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5681 constant before any folding.
5682 The real work of conversion is done by `convert'.
5683 The purpose of this function is to generate error messages
5684 for assignments that are not allowed in C.
5685 ERRTYPE says whether it is argument passing, assignment,
5686 initialization or return.
5688 LOCATION is the location of the assignment, EXPR_LOC is the location of
5689 the RHS or, for a function, location of an argument.
5690 FUNCTION is a tree for the function being called.
5691 PARMNUM is the number of the argument, for printing in error messages. */
5694 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
5695 tree rhs
, tree origtype
, enum impl_conv errtype
,
5696 bool null_pointer_constant
, tree fundecl
,
5697 tree function
, int parmnum
)
5699 enum tree_code codel
= TREE_CODE (type
);
5700 tree orig_rhs
= rhs
;
5702 enum tree_code coder
;
5703 tree rname
= NULL_TREE
;
5704 bool objc_ok
= false;
5706 if (errtype
== ic_argpass
)
5709 /* Change pointer to function to the function itself for
5711 if (TREE_CODE (function
) == ADDR_EXPR
5712 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
5713 function
= TREE_OPERAND (function
, 0);
5715 /* Handle an ObjC selector specially for diagnostics. */
5716 selector
= objc_message_selector ();
5718 if (selector
&& parmnum
> 2)
5725 /* This macro is used to emit diagnostics to ensure that all format
5726 strings are complete sentences, visible to gettext and checked at
5728 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5733 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5734 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5735 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5736 "expected %qT but argument is of type %qT", \
5740 pedwarn (LOCATION, OPT, AS); \
5743 pedwarn_init (LOCATION, OPT, IN); \
5746 pedwarn (LOCATION, OPT, RE); \
5749 gcc_unreachable (); \
5753 /* This macro is used to emit diagnostics to ensure that all format
5754 strings are complete sentences, visible to gettext and checked at
5755 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5756 extra parameter to enumerate qualifiers. */
5757 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5762 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5763 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5764 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5765 "expected %qT but argument is of type %qT", \
5769 pedwarn (LOCATION, OPT, AS, QUALS); \
5772 pedwarn (LOCATION, OPT, IN, QUALS); \
5775 pedwarn (LOCATION, OPT, RE, QUALS); \
5778 gcc_unreachable (); \
5782 /* This macro is used to emit diagnostics to ensure that all format
5783 strings are complete sentences, visible to gettext and checked at
5784 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5785 warning_at instead of pedwarn. */
5786 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5791 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5792 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5793 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5794 "expected %qT but argument is of type %qT", \
5798 warning_at (LOCATION, OPT, AS, QUALS); \
5801 warning_at (LOCATION, OPT, IN, QUALS); \
5804 warning_at (LOCATION, OPT, RE, QUALS); \
5807 gcc_unreachable (); \
5811 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5812 rhs
= TREE_OPERAND (rhs
, 0);
5814 rhstype
= TREE_TYPE (rhs
);
5815 coder
= TREE_CODE (rhstype
);
5817 if (coder
== ERROR_MARK
)
5818 return error_mark_node
;
5820 if (c_dialect_objc ())
5843 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
5846 if (warn_cxx_compat
)
5848 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
5849 if (checktype
!= error_mark_node
5850 && TREE_CODE (type
) == ENUMERAL_TYPE
5851 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
5853 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wc___compat
,
5854 G_("enum conversion when passing argument "
5855 "%d of %qE is invalid in C++"),
5856 G_("enum conversion in assignment is "
5858 G_("enum conversion in initialization is "
5860 G_("enum conversion in return is "
5865 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
5868 if (coder
== VOID_TYPE
)
5870 /* Except for passing an argument to an unprototyped function,
5871 this is a constraint violation. When passing an argument to
5872 an unprototyped function, it is compile-time undefined;
5873 making it a constraint in that case was rejected in
5875 error_at (location
, "void value not ignored as it ought to be");
5876 return error_mark_node
;
5878 rhs
= require_complete_type (rhs
);
5879 if (rhs
== error_mark_node
)
5880 return error_mark_node
;
5881 /* A non-reference type can convert to a reference. This handles
5882 va_start, va_copy and possibly port built-ins. */
5883 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
5885 if (!lvalue_p (rhs
))
5887 error_at (location
, "cannot pass rvalue to reference parameter");
5888 return error_mark_node
;
5890 if (!c_mark_addressable (rhs
))
5891 return error_mark_node
;
5892 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
5893 SET_EXPR_LOCATION (rhs
, location
);
5895 rhs
= convert_for_assignment (location
, expr_loc
,
5896 build_pointer_type (TREE_TYPE (type
)),
5897 rhs
, origtype
, errtype
,
5898 null_pointer_constant
, fundecl
, function
,
5900 if (rhs
== error_mark_node
)
5901 return error_mark_node
;
5903 rhs
= build1 (NOP_EXPR
, type
, rhs
);
5904 SET_EXPR_LOCATION (rhs
, location
);
5907 /* Some types can interconvert without explicit casts. */
5908 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
5909 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
5910 return convert (type
, rhs
);
5911 /* Arithmetic types all interconvert, and enum is treated like int. */
5912 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
5913 || codel
== FIXED_POINT_TYPE
5914 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
5915 || codel
== BOOLEAN_TYPE
)
5916 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
5917 || coder
== FIXED_POINT_TYPE
5918 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
5919 || coder
== BOOLEAN_TYPE
))
5922 bool save
= in_late_binary_op
;
5923 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
5924 || (coder
== REAL_TYPE
5925 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
5926 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
5927 in_late_binary_op
= true;
5928 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5929 ? expr_loc
: location
, type
, orig_rhs
);
5930 in_late_binary_op
= save
;
5934 /* Aggregates in different TUs might need conversion. */
5935 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
5937 && comptypes (type
, rhstype
))
5938 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5939 ? expr_loc
: location
, type
, rhs
);
5941 /* Conversion to a transparent union or record from its member types.
5942 This applies only to function arguments. */
5943 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
5944 && TYPE_TRANSPARENT_AGGR (type
))
5945 && errtype
== ic_argpass
)
5947 tree memb
, marginal_memb
= NULL_TREE
;
5949 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
5951 tree memb_type
= TREE_TYPE (memb
);
5953 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
5954 TYPE_MAIN_VARIANT (rhstype
)))
5957 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
5960 if (coder
== POINTER_TYPE
)
5962 tree ttl
= TREE_TYPE (memb_type
);
5963 tree ttr
= TREE_TYPE (rhstype
);
5965 /* Any non-function converts to a [const][volatile] void *
5966 and vice versa; otherwise, targets must be the same.
5967 Meanwhile, the lhs target must have all the qualifiers of
5969 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5970 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5971 || comp_target_types (location
, memb_type
, rhstype
))
5973 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
5974 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
5975 /* If this type won't generate any warnings, use it. */
5976 if (lquals
== rquals
5977 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
5978 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5979 ? ((lquals
| rquals
) == rquals
)
5980 : ((lquals
| rquals
) == lquals
)))
5983 /* Keep looking for a better type, but remember this one. */
5985 marginal_memb
= memb
;
5989 /* Can convert integer zero to any pointer type. */
5990 if (null_pointer_constant
)
5992 rhs
= null_pointer_node
;
5997 if (memb
|| marginal_memb
)
6001 /* We have only a marginally acceptable member type;
6002 it needs a warning. */
6003 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
6004 tree ttr
= TREE_TYPE (rhstype
);
6006 /* Const and volatile mean something different for function
6007 types, so the usual warnings are not appropriate. */
6008 if (TREE_CODE (ttr
) == FUNCTION_TYPE
6009 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6011 /* Because const and volatile on functions are
6012 restrictions that say the function will not do
6013 certain things, it is okay to use a const or volatile
6014 function where an ordinary one is wanted, but not
6016 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6017 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6018 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6019 OPT_Wdiscarded_qualifiers
,
6020 G_("passing argument %d of %qE "
6021 "makes %q#v qualified function "
6022 "pointer from unqualified"),
6023 G_("assignment makes %q#v qualified "
6024 "function pointer from "
6026 G_("initialization makes %q#v qualified "
6027 "function pointer from "
6029 G_("return makes %q#v qualified function "
6030 "pointer from unqualified"),
6031 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6033 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
6034 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
6035 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6036 OPT_Wdiscarded_qualifiers
,
6037 G_("passing argument %d of %qE discards "
6038 "%qv qualifier from pointer target type"),
6039 G_("assignment discards %qv qualifier "
6040 "from pointer target type"),
6041 G_("initialization discards %qv qualifier "
6042 "from pointer target type"),
6043 G_("return discards %qv qualifier from "
6044 "pointer target type"),
6045 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6047 memb
= marginal_memb
;
6050 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
6051 pedwarn (location
, OPT_Wpedantic
,
6052 "ISO C prohibits argument conversion to union type");
6054 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
6055 return build_constructor_single (type
, memb
, rhs
);
6059 /* Conversions among pointers */
6060 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
6061 && (coder
== codel
))
6063 tree ttl
= TREE_TYPE (type
);
6064 tree ttr
= TREE_TYPE (rhstype
);
6067 bool is_opaque_pointer
;
6068 int target_cmp
= 0; /* Cache comp_target_types () result. */
6072 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
6073 mvl
= (TYPE_ATOMIC (mvl
)
6074 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
6076 : TYPE_MAIN_VARIANT (mvl
));
6077 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
6078 mvr
= (TYPE_ATOMIC (mvr
)
6079 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
6081 : TYPE_MAIN_VARIANT (mvr
));
6082 /* Opaque pointers are treated like void pointers. */
6083 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
6085 /* The Plan 9 compiler permits a pointer to a struct to be
6086 automatically converted into a pointer to an anonymous field
6087 within the struct. */
6088 if (flag_plan9_extensions
6089 && (TREE_CODE (mvl
) == RECORD_TYPE
|| TREE_CODE(mvl
) == UNION_TYPE
)
6090 && (TREE_CODE (mvr
) == RECORD_TYPE
|| TREE_CODE(mvr
) == UNION_TYPE
)
6093 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
6094 if (new_rhs
!= NULL_TREE
)
6097 rhstype
= TREE_TYPE (rhs
);
6098 coder
= TREE_CODE (rhstype
);
6099 ttr
= TREE_TYPE (rhstype
);
6100 mvr
= TYPE_MAIN_VARIANT (ttr
);
6104 /* C++ does not allow the implicit conversion void* -> T*. However,
6105 for the purpose of reducing the number of false positives, we
6106 tolerate the special case of
6110 where NULL is typically defined in C to be '(void *) 0'. */
6111 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
6112 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
6114 "request for implicit conversion "
6115 "from %qT to %qT not permitted in C++", rhstype
, type
);
6117 /* See if the pointers point to incompatible address spaces. */
6118 asl
= TYPE_ADDR_SPACE (ttl
);
6119 asr
= TYPE_ADDR_SPACE (ttr
);
6120 if (!null_pointer_constant_p (rhs
)
6121 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
6126 error_at (expr_loc
, "passing argument %d of %qE from pointer to "
6127 "non-enclosed address space", parmnum
, rname
);
6130 error_at (location
, "assignment from pointer to "
6131 "non-enclosed address space");
6134 error_at (location
, "initialization from pointer to "
6135 "non-enclosed address space");
6138 error_at (location
, "return from pointer to "
6139 "non-enclosed address space");
6144 return error_mark_node
;
6147 /* Check if the right-hand side has a format attribute but the
6148 left-hand side doesn't. */
6149 if (warn_suggest_attribute_format
6150 && check_missing_format_attribute (type
, rhstype
))
6155 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
6156 "argument %d of %qE might be "
6157 "a candidate for a format attribute",
6161 warning_at (location
, OPT_Wsuggest_attribute_format
,
6162 "assignment left-hand side might be "
6163 "a candidate for a format attribute");
6166 warning_at (location
, OPT_Wsuggest_attribute_format
,
6167 "initialization left-hand side might be "
6168 "a candidate for a format attribute");
6171 warning_at (location
, OPT_Wsuggest_attribute_format
,
6172 "return type might be "
6173 "a candidate for a format attribute");
6180 /* Any non-function converts to a [const][volatile] void *
6181 and vice versa; otherwise, targets must be the same.
6182 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6183 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6184 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6185 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6186 || is_opaque_pointer
6187 || ((c_common_unsigned_type (mvl
)
6188 == c_common_unsigned_type (mvr
))
6189 && (c_common_signed_type (mvl
)
6190 == c_common_signed_type (mvr
))
6191 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6193 /* Warn about loss of qualifers from pointers to arrays with
6194 qualifiers on the element type. */
6195 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
6197 ttr
= strip_array_types (ttr
);
6198 ttl
= strip_array_types (ttl
);
6200 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6201 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6202 WARNING_FOR_QUALIFIERS (location
, expr_loc
,
6203 OPT_Wdiscarded_array_qualifiers
,
6204 G_("passing argument %d of %qE discards "
6205 "%qv qualifier from pointer target type"),
6206 G_("assignment discards %qv qualifier "
6207 "from pointer target type"),
6208 G_("initialization discards %qv qualifier "
6209 "from pointer target type"),
6210 G_("return discards %qv qualifier from "
6211 "pointer target type"),
6212 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6215 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6218 && !null_pointer_constant
6219 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6220 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
6221 G_("ISO C forbids passing argument %d of "
6222 "%qE between function pointer "
6224 G_("ISO C forbids assignment between "
6225 "function pointer and %<void *%>"),
6226 G_("ISO C forbids initialization between "
6227 "function pointer and %<void *%>"),
6228 G_("ISO C forbids return between function "
6229 "pointer and %<void *%>"));
6230 /* Const and volatile mean something different for function types,
6231 so the usual warnings are not appropriate. */
6232 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6233 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6235 /* Don't warn about loss of qualifier for conversions from
6236 qualified void* to pointers to arrays with corresponding
6237 qualifier on the element type. */
6239 ttl
= strip_array_types (ttl
);
6241 /* Assignments between atomic and non-atomic objects are OK. */
6242 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6243 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6245 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6246 OPT_Wdiscarded_qualifiers
,
6247 G_("passing argument %d of %qE discards "
6248 "%qv qualifier from pointer target type"),
6249 G_("assignment discards %qv qualifier "
6250 "from pointer target type"),
6251 G_("initialization discards %qv qualifier "
6252 "from pointer target type"),
6253 G_("return discards %qv qualifier from "
6254 "pointer target type"),
6255 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6257 /* If this is not a case of ignoring a mismatch in signedness,
6259 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6262 /* If there is a mismatch, do warn. */
6263 else if (warn_pointer_sign
)
6264 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpointer_sign
,
6265 G_("pointer targets in passing argument "
6266 "%d of %qE differ in signedness"),
6267 G_("pointer targets in assignment "
6268 "differ in signedness"),
6269 G_("pointer targets in initialization "
6270 "differ in signedness"),
6271 G_("pointer targets in return differ "
6274 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6275 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6277 /* Because const and volatile on functions are restrictions
6278 that say the function will not do certain things,
6279 it is okay to use a const or volatile function
6280 where an ordinary one is wanted, but not vice-versa. */
6281 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6282 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6283 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6284 OPT_Wdiscarded_qualifiers
,
6285 G_("passing argument %d of %qE makes "
6286 "%q#v qualified function pointer "
6287 "from unqualified"),
6288 G_("assignment makes %q#v qualified function "
6289 "pointer from unqualified"),
6290 G_("initialization makes %q#v qualified "
6291 "function pointer from unqualified"),
6292 G_("return makes %q#v qualified function "
6293 "pointer from unqualified"),
6294 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6298 /* Avoid warning about the volatile ObjC EH puts on decls. */
6300 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6301 OPT_Wincompatible_pointer_types
,
6302 G_("passing argument %d of %qE from "
6303 "incompatible pointer type"),
6304 G_("assignment from incompatible pointer type"),
6305 G_("initialization from incompatible "
6307 G_("return from incompatible pointer type"));
6309 return convert (type
, rhs
);
6311 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6313 /* ??? This should not be an error when inlining calls to
6314 unprototyped functions. */
6315 error_at (location
, "invalid use of non-lvalue array");
6316 return error_mark_node
;
6318 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6320 /* An explicit constant 0 can convert to a pointer,
6321 or one that results from arithmetic, even including
6322 a cast to integer type. */
6323 if (!null_pointer_constant
)
6324 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6325 OPT_Wint_conversion
,
6326 G_("passing argument %d of %qE makes "
6327 "pointer from integer without a cast"),
6328 G_("assignment makes pointer from integer "
6330 G_("initialization makes pointer from "
6331 "integer without a cast"),
6332 G_("return makes pointer from integer "
6335 return convert (type
, rhs
);
6337 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6339 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6340 OPT_Wint_conversion
,
6341 G_("passing argument %d of %qE makes integer "
6342 "from pointer without a cast"),
6343 G_("assignment makes integer from pointer "
6345 G_("initialization makes integer from pointer "
6347 G_("return makes integer from pointer "
6349 return convert (type
, rhs
);
6351 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6354 bool save
= in_late_binary_op
;
6355 in_late_binary_op
= true;
6356 ret
= convert (type
, rhs
);
6357 in_late_binary_op
= save
;
6364 error_at (expr_loc
, "incompatible type for argument %d of %qE", parmnum
,
6366 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6367 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6368 "expected %qT but argument is of type %qT", type
, rhstype
);
6371 error_at (location
, "incompatible types when assigning to type %qT from "
6372 "type %qT", type
, rhstype
);
6376 "incompatible types when initializing type %qT using type %qT",
6381 "incompatible types when returning type %qT but %qT was "
6382 "expected", rhstype
, type
);
6388 return error_mark_node
;
6391 /* If VALUE is a compound expr all of whose expressions are constant, then
6392 return its value. Otherwise, return error_mark_node.
6394 This is for handling COMPOUND_EXPRs as initializer elements
6395 which is allowed with a warning when -pedantic is specified. */
6398 valid_compound_expr_initializer (tree value
, tree endtype
)
6400 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6402 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6404 return error_mark_node
;
6405 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6408 else if (!initializer_constant_valid_p (value
, endtype
))
6409 return error_mark_node
;
6414 /* Perform appropriate conversions on the initial value of a variable,
6415 store it in the declaration DECL,
6416 and print any error messages that are appropriate.
6417 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6418 If the init is invalid, store an ERROR_MARK.
6420 INIT_LOC is the location of the initial value. */
6423 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6428 /* If variable's type was invalidly declared, just ignore it. */
6430 type
= TREE_TYPE (decl
);
6431 if (TREE_CODE (type
) == ERROR_MARK
)
6434 /* Digest the specified initializer into an expression. */
6437 npc
= null_pointer_constant_p (init
);
6438 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6439 true, TREE_STATIC (decl
));
6441 /* Store the expression if valid; else report error. */
6443 if (!in_system_header_at (input_location
)
6444 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6445 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6446 "aggregate initialization");
6448 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
6449 DECL_INITIAL (decl
) = value
;
6451 /* ANSI wants warnings about out-of-range constant initializers. */
6452 STRIP_TYPE_NOPS (value
);
6453 if (TREE_STATIC (decl
))
6454 constant_expression_warning (value
);
6456 /* Check if we need to set array size from compound literal size. */
6457 if (TREE_CODE (type
) == ARRAY_TYPE
6458 && TYPE_DOMAIN (type
) == 0
6459 && value
!= error_mark_node
)
6461 tree inside_init
= init
;
6463 STRIP_TYPE_NOPS (inside_init
);
6464 inside_init
= fold (inside_init
);
6466 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6468 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6470 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6472 /* For int foo[] = (int [3]){1}; we need to set array size
6473 now since later on array initializer will be just the
6474 brace enclosed list of the compound literal. */
6475 tree etype
= strip_array_types (TREE_TYPE (decl
));
6476 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6477 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6479 layout_decl (cldecl
, 0);
6481 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6487 /* Methods for storing and printing names for error messages. */
6489 /* Implement a spelling stack that allows components of a name to be pushed
6490 and popped. Each element on the stack is this structure. */
6497 unsigned HOST_WIDE_INT i
;
6502 #define SPELLING_STRING 1
6503 #define SPELLING_MEMBER 2
6504 #define SPELLING_BOUNDS 3
6506 static struct spelling
*spelling
; /* Next stack element (unused). */
6507 static struct spelling
*spelling_base
; /* Spelling stack base. */
6508 static int spelling_size
; /* Size of the spelling stack. */
6510 /* Macros to save and restore the spelling stack around push_... functions.
6511 Alternative to SAVE_SPELLING_STACK. */
6513 #define SPELLING_DEPTH() (spelling - spelling_base)
6514 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6516 /* Push an element on the spelling stack with type KIND and assign VALUE
6519 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6521 int depth = SPELLING_DEPTH (); \
6523 if (depth >= spelling_size) \
6525 spelling_size += 10; \
6526 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6528 RESTORE_SPELLING_DEPTH (depth); \
6531 spelling->kind = (KIND); \
6532 spelling->MEMBER = (VALUE); \
6536 /* Push STRING on the stack. Printed literally. */
6539 push_string (const char *string
)
6541 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6544 /* Push a member name on the stack. Printed as '.' STRING. */
6547 push_member_name (tree decl
)
6549 const char *const string
6551 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6552 : _("<anonymous>"));
6553 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6556 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6559 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6561 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6564 /* Compute the maximum size in bytes of the printed spelling. */
6567 spelling_length (void)
6572 for (p
= spelling_base
; p
< spelling
; p
++)
6574 if (p
->kind
== SPELLING_BOUNDS
)
6577 size
+= strlen (p
->u
.s
) + 1;
6583 /* Print the spelling to BUFFER and return it. */
6586 print_spelling (char *buffer
)
6591 for (p
= spelling_base
; p
< spelling
; p
++)
6592 if (p
->kind
== SPELLING_BOUNDS
)
6594 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6600 if (p
->kind
== SPELLING_MEMBER
)
6602 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
6609 /* Digest the parser output INIT as an initializer for type TYPE.
6610 Return a C expression of type TYPE to represent the initial value.
6612 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6614 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6616 If INIT is a string constant, STRICT_STRING is true if it is
6617 unparenthesized or we should not warn here for it being parenthesized.
6618 For other types of INIT, STRICT_STRING is not used.
6620 INIT_LOC is the location of the INIT.
6622 REQUIRE_CONSTANT requests an error if non-constant initializers or
6623 elements are seen. */
6626 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
6627 bool null_pointer_constant
, bool strict_string
,
6628 int require_constant
)
6630 enum tree_code code
= TREE_CODE (type
);
6631 tree inside_init
= init
;
6632 tree semantic_type
= NULL_TREE
;
6633 bool maybe_const
= true;
6635 if (type
== error_mark_node
6637 || error_operand_p (init
))
6638 return error_mark_node
;
6640 STRIP_TYPE_NOPS (inside_init
);
6642 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
6644 semantic_type
= TREE_TYPE (inside_init
);
6645 inside_init
= TREE_OPERAND (inside_init
, 0);
6647 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
6648 inside_init
= decl_constant_value_for_optimization (inside_init
);
6650 /* Initialization of an array of chars from a string constant
6651 optionally enclosed in braces. */
6653 if (code
== ARRAY_TYPE
&& inside_init
6654 && TREE_CODE (inside_init
) == STRING_CST
)
6657 = (TYPE_ATOMIC (TREE_TYPE (type
))
6658 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
6660 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6661 /* Note that an array could be both an array of character type
6662 and an array of wchar_t if wchar_t is signed char or unsigned
6664 bool char_array
= (typ1
== char_type_node
6665 || typ1
== signed_char_type_node
6666 || typ1
== unsigned_char_type_node
);
6667 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
6668 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
6669 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
6671 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
6674 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
6675 expr
.value
= inside_init
;
6676 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
6677 expr
.original_type
= NULL
;
6678 maybe_warn_string_init (init_loc
, type
, expr
);
6680 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
6681 pedwarn_init (init_loc
, OPT_Wpedantic
,
6682 "initialization of a flexible array member");
6684 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6685 TYPE_MAIN_VARIANT (type
)))
6690 if (typ2
!= char_type_node
)
6692 error_init (init_loc
, "char-array initialized from wide "
6694 return error_mark_node
;
6699 if (typ2
== char_type_node
)
6701 error_init (init_loc
, "wide character array initialized "
6702 "from non-wide string");
6703 return error_mark_node
;
6705 else if (!comptypes(typ1
, typ2
))
6707 error_init (init_loc
, "wide character array initialized "
6708 "from incompatible wide string");
6709 return error_mark_node
;
6713 TREE_TYPE (inside_init
) = type
;
6714 if (TYPE_DOMAIN (type
) != 0
6715 && TYPE_SIZE (type
) != 0
6716 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
6718 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
6720 /* Subtract the size of a single (possibly wide) character
6721 because it's ok to ignore the terminating null char
6722 that is counted in the length of the constant. */
6723 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
6725 - (TYPE_PRECISION (typ1
)
6727 pedwarn_init (init_loc
, 0,
6728 ("initializer-string for array of chars "
6730 else if (warn_cxx_compat
6731 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
6732 warning_at (init_loc
, OPT_Wc___compat
,
6733 ("initializer-string for array chars "
6734 "is too long for C++"));
6739 else if (INTEGRAL_TYPE_P (typ1
))
6741 error_init (init_loc
, "array of inappropriate type initialized "
6742 "from string constant");
6743 return error_mark_node
;
6747 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6748 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6749 below and handle as a constructor. */
6750 if (code
== VECTOR_TYPE
6751 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
6752 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
6753 && TREE_CONSTANT (inside_init
))
6755 if (TREE_CODE (inside_init
) == VECTOR_CST
6756 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6757 TYPE_MAIN_VARIANT (type
)))
6760 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
6762 unsigned HOST_WIDE_INT ix
;
6764 bool constant_p
= true;
6766 /* Iterate through elements and check if all constructor
6767 elements are *_CSTs. */
6768 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
6769 if (!CONSTANT_CLASS_P (value
))
6776 return build_vector_from_ctor (type
,
6777 CONSTRUCTOR_ELTS (inside_init
));
6781 if (warn_sequence_point
)
6782 verify_sequence_points (inside_init
);
6784 /* Any type can be initialized
6785 from an expression of the same type, optionally with braces. */
6787 if (inside_init
&& TREE_TYPE (inside_init
) != 0
6788 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6789 TYPE_MAIN_VARIANT (type
))
6790 || (code
== ARRAY_TYPE
6791 && comptypes (TREE_TYPE (inside_init
), type
))
6792 || (code
== VECTOR_TYPE
6793 && comptypes (TREE_TYPE (inside_init
), type
))
6794 || (code
== POINTER_TYPE
6795 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
6796 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
6797 TREE_TYPE (type
)))))
6799 if (code
== POINTER_TYPE
)
6801 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
6803 if (TREE_CODE (inside_init
) == STRING_CST
6804 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6805 inside_init
= array_to_pointer_conversion
6806 (init_loc
, inside_init
);
6809 error_init (init_loc
, "invalid use of non-lvalue array");
6810 return error_mark_node
;
6815 if (code
== VECTOR_TYPE
)
6816 /* Although the types are compatible, we may require a
6818 inside_init
= convert (type
, inside_init
);
6820 if (require_constant
6821 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6823 /* As an extension, allow initializing objects with static storage
6824 duration with compound literals (which are then treated just as
6825 the brace enclosed list they contain). Also allow this for
6826 vectors, as we can only assign them with compound literals. */
6827 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
6828 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
6830 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6831 inside_init
= DECL_INITIAL (decl
);
6834 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
6835 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
6837 error_init (init_loc
, "array initialized from non-constant array "
6839 return error_mark_node
;
6842 /* Compound expressions can only occur here if -Wpedantic or
6843 -pedantic-errors is specified. In the later case, we always want
6844 an error. In the former case, we simply want a warning. */
6845 if (require_constant
&& pedantic
6846 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
6849 = valid_compound_expr_initializer (inside_init
,
6850 TREE_TYPE (inside_init
));
6851 if (inside_init
== error_mark_node
)
6852 error_init (init_loc
, "initializer element is not constant");
6854 pedwarn_init (init_loc
, OPT_Wpedantic
,
6855 "initializer element is not constant");
6856 if (flag_pedantic_errors
)
6857 inside_init
= error_mark_node
;
6859 else if (require_constant
6860 && !initializer_constant_valid_p (inside_init
,
6861 TREE_TYPE (inside_init
)))
6863 error_init (init_loc
, "initializer element is not constant");
6864 inside_init
= error_mark_node
;
6866 else if (require_constant
&& !maybe_const
)
6867 pedwarn_init (init_loc
, OPT_Wpedantic
,
6868 "initializer element is not a constant expression");
6870 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6871 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
6872 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
6873 type
, inside_init
, origtype
,
6874 ic_init
, null_pointer_constant
,
6875 NULL_TREE
, NULL_TREE
, 0);
6879 /* Handle scalar types, including conversions. */
6881 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
6882 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
6883 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
6885 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
6886 && (TREE_CODE (init
) == STRING_CST
6887 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
6888 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
6890 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
6893 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
6894 inside_init
, origtype
, ic_init
,
6895 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
6898 /* Check to see if we have already given an error message. */
6899 if (inside_init
== error_mark_node
)
6901 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
6903 error_init (init_loc
, "initializer element is not constant");
6904 inside_init
= error_mark_node
;
6906 else if (require_constant
6907 && !initializer_constant_valid_p (inside_init
,
6908 TREE_TYPE (inside_init
)))
6910 error_init (init_loc
, "initializer element is not computable at "
6912 inside_init
= error_mark_node
;
6914 else if (require_constant
&& !maybe_const
)
6915 pedwarn_init (init_loc
, 0,
6916 "initializer element is not a constant expression");
6921 /* Come here only for records and arrays. */
6923 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
6925 error_init (init_loc
, "variable-sized object may not be initialized");
6926 return error_mark_node
;
6929 error_init (init_loc
, "invalid initializer");
6930 return error_mark_node
;
6933 /* Handle initializers that use braces. */
6935 /* Type of object we are accumulating a constructor for.
6936 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6937 static tree constructor_type
;
6939 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6941 static tree constructor_fields
;
6943 /* For an ARRAY_TYPE, this is the specified index
6944 at which to store the next element we get. */
6945 static tree constructor_index
;
6947 /* For an ARRAY_TYPE, this is the maximum index. */
6948 static tree constructor_max_index
;
6950 /* For a RECORD_TYPE, this is the first field not yet written out. */
6951 static tree constructor_unfilled_fields
;
6953 /* For an ARRAY_TYPE, this is the index of the first element
6954 not yet written out. */
6955 static tree constructor_unfilled_index
;
6957 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6958 This is so we can generate gaps between fields, when appropriate. */
6959 static tree constructor_bit_index
;
6961 /* If we are saving up the elements rather than allocating them,
6962 this is the list of elements so far (in reverse order,
6963 most recent first). */
6964 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
6966 /* 1 if constructor should be incrementally stored into a constructor chain,
6967 0 if all the elements should be kept in AVL tree. */
6968 static int constructor_incremental
;
6970 /* 1 if so far this constructor's elements are all compile-time constants. */
6971 static int constructor_constant
;
6973 /* 1 if so far this constructor's elements are all valid address constants. */
6974 static int constructor_simple
;
6976 /* 1 if this constructor has an element that cannot be part of a
6977 constant expression. */
6978 static int constructor_nonconst
;
6980 /* 1 if this constructor is erroneous so far. */
6981 static int constructor_erroneous
;
6983 /* 1 if this constructor is the universal zero initializer { 0 }. */
6984 static int constructor_zeroinit
;
6986 /* Structure for managing pending initializer elements, organized as an
6991 struct init_node
*left
, *right
;
6992 struct init_node
*parent
;
6999 /* Tree of pending elements at this constructor level.
7000 These are elements encountered out of order
7001 which belong at places we haven't reached yet in actually
7003 Will never hold tree nodes across GC runs. */
7004 static struct init_node
*constructor_pending_elts
;
7006 /* The SPELLING_DEPTH of this constructor. */
7007 static int constructor_depth
;
7009 /* DECL node for which an initializer is being read.
7010 0 means we are reading a constructor expression
7011 such as (struct foo) {...}. */
7012 static tree constructor_decl
;
7014 /* Nonzero if this is an initializer for a top-level decl. */
7015 static int constructor_top_level
;
7017 /* Nonzero if there were any member designators in this initializer. */
7018 static int constructor_designated
;
7020 /* Nesting depth of designator list. */
7021 static int designator_depth
;
7023 /* Nonzero if there were diagnosed errors in this designator list. */
7024 static int designator_erroneous
;
7027 /* This stack has a level for each implicit or explicit level of
7028 structuring in the initializer, including the outermost one. It
7029 saves the values of most of the variables above. */
7031 struct constructor_range_stack
;
7033 struct constructor_stack
7035 struct constructor_stack
*next
;
7040 tree unfilled_index
;
7041 tree unfilled_fields
;
7043 vec
<constructor_elt
, va_gc
> *elements
;
7044 struct init_node
*pending_elts
;
7047 /* If value nonzero, this value should replace the entire
7048 constructor at this level. */
7049 struct c_expr replacement_value
;
7050 struct constructor_range_stack
*range_stack
;
7059 int designator_depth
;
7062 static struct constructor_stack
*constructor_stack
;
7064 /* This stack represents designators from some range designator up to
7065 the last designator in the list. */
7067 struct constructor_range_stack
7069 struct constructor_range_stack
*next
, *prev
;
7070 struct constructor_stack
*stack
;
7077 static struct constructor_range_stack
*constructor_range_stack
;
7079 /* This stack records separate initializers that are nested.
7080 Nested initializers can't happen in ANSI C, but GNU C allows them
7081 in cases like { ... (struct foo) { ... } ... }. */
7083 struct initializer_stack
7085 struct initializer_stack
*next
;
7087 struct constructor_stack
*constructor_stack
;
7088 struct constructor_range_stack
*constructor_range_stack
;
7089 vec
<constructor_elt
, va_gc
> *elements
;
7090 struct spelling
*spelling
;
7091 struct spelling
*spelling_base
;
7094 char require_constant_value
;
7095 char require_constant_elements
;
7098 static struct initializer_stack
*initializer_stack
;
7100 /* Prepare to parse and output the initializer for variable DECL. */
7103 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
7106 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
7108 p
->decl
= constructor_decl
;
7109 p
->require_constant_value
= require_constant_value
;
7110 p
->require_constant_elements
= require_constant_elements
;
7111 p
->constructor_stack
= constructor_stack
;
7112 p
->constructor_range_stack
= constructor_range_stack
;
7113 p
->elements
= constructor_elements
;
7114 p
->spelling
= spelling
;
7115 p
->spelling_base
= spelling_base
;
7116 p
->spelling_size
= spelling_size
;
7117 p
->top_level
= constructor_top_level
;
7118 p
->next
= initializer_stack
;
7119 initializer_stack
= p
;
7121 constructor_decl
= decl
;
7122 constructor_designated
= 0;
7123 constructor_top_level
= top_level
;
7125 if (decl
!= 0 && decl
!= error_mark_node
)
7127 require_constant_value
= TREE_STATIC (decl
);
7128 require_constant_elements
7129 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
7130 /* For a scalar, you can always use any value to initialize,
7131 even within braces. */
7132 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
7133 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
7134 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
7135 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
7136 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
7140 require_constant_value
= 0;
7141 require_constant_elements
= 0;
7142 locus
= _("(anonymous)");
7145 constructor_stack
= 0;
7146 constructor_range_stack
= 0;
7148 found_missing_braces
= 0;
7152 RESTORE_SPELLING_DEPTH (0);
7155 push_string (locus
);
7161 struct initializer_stack
*p
= initializer_stack
;
7163 /* Free the whole constructor stack of this initializer. */
7164 while (constructor_stack
)
7166 struct constructor_stack
*q
= constructor_stack
;
7167 constructor_stack
= q
->next
;
7171 gcc_assert (!constructor_range_stack
);
7173 /* Pop back to the data of the outer initializer (if any). */
7174 free (spelling_base
);
7176 constructor_decl
= p
->decl
;
7177 require_constant_value
= p
->require_constant_value
;
7178 require_constant_elements
= p
->require_constant_elements
;
7179 constructor_stack
= p
->constructor_stack
;
7180 constructor_range_stack
= p
->constructor_range_stack
;
7181 constructor_elements
= p
->elements
;
7182 spelling
= p
->spelling
;
7183 spelling_base
= p
->spelling_base
;
7184 spelling_size
= p
->spelling_size
;
7185 constructor_top_level
= p
->top_level
;
7186 initializer_stack
= p
->next
;
7190 /* Call here when we see the initializer is surrounded by braces.
7191 This is instead of a call to push_init_level;
7192 it is matched by a call to pop_init_level.
7194 TYPE is the type to initialize, for a constructor expression.
7195 For an initializer for a decl, TYPE is zero. */
7198 really_start_incremental_init (tree type
)
7200 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7203 type
= TREE_TYPE (constructor_decl
);
7205 if (TREE_CODE (type
) == VECTOR_TYPE
7206 && TYPE_VECTOR_OPAQUE (type
))
7207 error ("opaque vector types cannot be initialized");
7209 p
->type
= constructor_type
;
7210 p
->fields
= constructor_fields
;
7211 p
->index
= constructor_index
;
7212 p
->max_index
= constructor_max_index
;
7213 p
->unfilled_index
= constructor_unfilled_index
;
7214 p
->unfilled_fields
= constructor_unfilled_fields
;
7215 p
->bit_index
= constructor_bit_index
;
7216 p
->elements
= constructor_elements
;
7217 p
->constant
= constructor_constant
;
7218 p
->simple
= constructor_simple
;
7219 p
->nonconst
= constructor_nonconst
;
7220 p
->erroneous
= constructor_erroneous
;
7221 p
->pending_elts
= constructor_pending_elts
;
7222 p
->depth
= constructor_depth
;
7223 p
->replacement_value
.value
= 0;
7224 p
->replacement_value
.original_code
= ERROR_MARK
;
7225 p
->replacement_value
.original_type
= NULL
;
7229 p
->incremental
= constructor_incremental
;
7230 p
->designated
= constructor_designated
;
7231 p
->designator_depth
= designator_depth
;
7233 constructor_stack
= p
;
7235 constructor_constant
= 1;
7236 constructor_simple
= 1;
7237 constructor_nonconst
= 0;
7238 constructor_depth
= SPELLING_DEPTH ();
7239 constructor_elements
= NULL
;
7240 constructor_pending_elts
= 0;
7241 constructor_type
= type
;
7242 constructor_incremental
= 1;
7243 constructor_designated
= 0;
7244 constructor_zeroinit
= 1;
7245 designator_depth
= 0;
7246 designator_erroneous
= 0;
7248 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7249 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7251 constructor_fields
= TYPE_FIELDS (constructor_type
);
7252 /* Skip any nameless bit fields at the beginning. */
7253 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7254 && DECL_NAME (constructor_fields
) == 0)
7255 constructor_fields
= DECL_CHAIN (constructor_fields
);
7257 constructor_unfilled_fields
= constructor_fields
;
7258 constructor_bit_index
= bitsize_zero_node
;
7260 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7262 if (TYPE_DOMAIN (constructor_type
))
7264 constructor_max_index
7265 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7267 /* Detect non-empty initializations of zero-length arrays. */
7268 if (constructor_max_index
== NULL_TREE
7269 && TYPE_SIZE (constructor_type
))
7270 constructor_max_index
= integer_minus_one_node
;
7272 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7273 to initialize VLAs will cause a proper error; avoid tree
7274 checking errors as well by setting a safe value. */
7275 if (constructor_max_index
7276 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7277 constructor_max_index
= integer_minus_one_node
;
7280 = convert (bitsizetype
,
7281 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7285 constructor_index
= bitsize_zero_node
;
7286 constructor_max_index
= NULL_TREE
;
7289 constructor_unfilled_index
= constructor_index
;
7291 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7293 /* Vectors are like simple fixed-size arrays. */
7294 constructor_max_index
=
7295 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7296 constructor_index
= bitsize_zero_node
;
7297 constructor_unfilled_index
= constructor_index
;
7301 /* Handle the case of int x = {5}; */
7302 constructor_fields
= constructor_type
;
7303 constructor_unfilled_fields
= constructor_type
;
7307 /* Push down into a subobject, for initialization.
7308 If this is for an explicit set of braces, IMPLICIT is 0.
7309 If it is because the next element belongs at a lower level,
7310 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7313 push_init_level (location_t loc
, int implicit
,
7314 struct obstack
*braced_init_obstack
)
7316 struct constructor_stack
*p
;
7317 tree value
= NULL_TREE
;
7319 /* If we've exhausted any levels that didn't have braces,
7320 pop them now. If implicit == 1, this will have been done in
7321 process_init_element; do not repeat it here because in the case
7322 of excess initializers for an empty aggregate this leads to an
7323 infinite cycle of popping a level and immediately recreating
7327 while (constructor_stack
->implicit
)
7329 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7330 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7331 && constructor_fields
== 0)
7332 process_init_element (input_location
,
7333 pop_init_level (loc
, 1, braced_init_obstack
),
7334 true, braced_init_obstack
);
7335 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7336 && constructor_max_index
7337 && tree_int_cst_lt (constructor_max_index
,
7339 process_init_element (input_location
,
7340 pop_init_level (loc
, 1, braced_init_obstack
),
7341 true, braced_init_obstack
);
7347 /* Unless this is an explicit brace, we need to preserve previous
7351 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7352 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7353 && constructor_fields
)
7354 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7355 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7356 value
= find_init_member (constructor_index
, braced_init_obstack
);
7359 p
= XNEW (struct constructor_stack
);
7360 p
->type
= constructor_type
;
7361 p
->fields
= constructor_fields
;
7362 p
->index
= constructor_index
;
7363 p
->max_index
= constructor_max_index
;
7364 p
->unfilled_index
= constructor_unfilled_index
;
7365 p
->unfilled_fields
= constructor_unfilled_fields
;
7366 p
->bit_index
= constructor_bit_index
;
7367 p
->elements
= constructor_elements
;
7368 p
->constant
= constructor_constant
;
7369 p
->simple
= constructor_simple
;
7370 p
->nonconst
= constructor_nonconst
;
7371 p
->erroneous
= constructor_erroneous
;
7372 p
->pending_elts
= constructor_pending_elts
;
7373 p
->depth
= constructor_depth
;
7374 p
->replacement_value
.value
= 0;
7375 p
->replacement_value
.original_code
= ERROR_MARK
;
7376 p
->replacement_value
.original_type
= NULL
;
7377 p
->implicit
= implicit
;
7379 p
->incremental
= constructor_incremental
;
7380 p
->designated
= constructor_designated
;
7381 p
->designator_depth
= designator_depth
;
7382 p
->next
= constructor_stack
;
7384 constructor_stack
= p
;
7386 constructor_constant
= 1;
7387 constructor_simple
= 1;
7388 constructor_nonconst
= 0;
7389 constructor_depth
= SPELLING_DEPTH ();
7390 constructor_elements
= NULL
;
7391 constructor_incremental
= 1;
7392 constructor_designated
= 0;
7393 constructor_pending_elts
= 0;
7396 p
->range_stack
= constructor_range_stack
;
7397 constructor_range_stack
= 0;
7398 designator_depth
= 0;
7399 designator_erroneous
= 0;
7402 /* Don't die if an entire brace-pair level is superfluous
7403 in the containing level. */
7404 if (constructor_type
== 0)
7406 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7407 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7409 /* Don't die if there are extra init elts at the end. */
7410 if (constructor_fields
== 0)
7411 constructor_type
= 0;
7414 constructor_type
= TREE_TYPE (constructor_fields
);
7415 push_member_name (constructor_fields
);
7416 constructor_depth
++;
7418 /* If upper initializer is designated, then mark this as
7419 designated too to prevent bogus warnings. */
7420 constructor_designated
= p
->designated
;
7422 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7424 constructor_type
= TREE_TYPE (constructor_type
);
7425 push_array_bounds (tree_to_uhwi (constructor_index
));
7426 constructor_depth
++;
7429 if (constructor_type
== 0)
7431 error_init (loc
, "extra brace group at end of initializer");
7432 constructor_fields
= 0;
7433 constructor_unfilled_fields
= 0;
7437 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7439 constructor_constant
= TREE_CONSTANT (value
);
7440 constructor_simple
= TREE_STATIC (value
);
7441 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7442 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7443 if (!vec_safe_is_empty (constructor_elements
)
7444 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7445 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7446 set_nonincremental_init (braced_init_obstack
);
7450 found_missing_braces
= 1;
7452 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7453 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7455 constructor_fields
= TYPE_FIELDS (constructor_type
);
7456 /* Skip any nameless bit fields at the beginning. */
7457 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7458 && DECL_NAME (constructor_fields
) == 0)
7459 constructor_fields
= DECL_CHAIN (constructor_fields
);
7461 constructor_unfilled_fields
= constructor_fields
;
7462 constructor_bit_index
= bitsize_zero_node
;
7464 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7466 /* Vectors are like simple fixed-size arrays. */
7467 constructor_max_index
=
7468 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7469 constructor_index
= bitsize_int (0);
7470 constructor_unfilled_index
= constructor_index
;
7472 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7474 if (TYPE_DOMAIN (constructor_type
))
7476 constructor_max_index
7477 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7479 /* Detect non-empty initializations of zero-length arrays. */
7480 if (constructor_max_index
== NULL_TREE
7481 && TYPE_SIZE (constructor_type
))
7482 constructor_max_index
= integer_minus_one_node
;
7484 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7485 to initialize VLAs will cause a proper error; avoid tree
7486 checking errors as well by setting a safe value. */
7487 if (constructor_max_index
7488 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7489 constructor_max_index
= integer_minus_one_node
;
7492 = convert (bitsizetype
,
7493 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7496 constructor_index
= bitsize_zero_node
;
7498 constructor_unfilled_index
= constructor_index
;
7499 if (value
&& TREE_CODE (value
) == STRING_CST
)
7501 /* We need to split the char/wchar array into individual
7502 characters, so that we don't have to special case it
7504 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7509 if (constructor_type
!= error_mark_node
)
7510 warning_init (input_location
, 0, "braces around scalar initializer");
7511 constructor_fields
= constructor_type
;
7512 constructor_unfilled_fields
= constructor_type
;
7516 /* At the end of an implicit or explicit brace level,
7517 finish up that level of constructor. If a single expression
7518 with redundant braces initialized that level, return the
7519 c_expr structure for that expression. Otherwise, the original_code
7520 element is set to ERROR_MARK.
7521 If we were outputting the elements as they are read, return 0 as the value
7522 from inner levels (process_init_element ignores that),
7523 but return error_mark_node as the value from the outermost level
7524 (that's what we want to put in DECL_INITIAL).
7525 Otherwise, return a CONSTRUCTOR expression as the value. */
7528 pop_init_level (location_t loc
, int implicit
,
7529 struct obstack
*braced_init_obstack
)
7531 struct constructor_stack
*p
;
7534 ret
.original_code
= ERROR_MARK
;
7535 ret
.original_type
= NULL
;
7539 /* When we come to an explicit close brace,
7540 pop any inner levels that didn't have explicit braces. */
7541 while (constructor_stack
->implicit
)
7542 process_init_element (input_location
,
7543 pop_init_level (loc
, 1, braced_init_obstack
),
7544 true, braced_init_obstack
);
7545 gcc_assert (!constructor_range_stack
);
7548 /* Now output all pending elements. */
7549 constructor_incremental
= 1;
7550 output_pending_init_elements (1, braced_init_obstack
);
7552 p
= constructor_stack
;
7554 /* Error for initializing a flexible array member, or a zero-length
7555 array member in an inappropriate context. */
7556 if (constructor_type
&& constructor_fields
7557 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7558 && TYPE_DOMAIN (constructor_type
)
7559 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7561 /* Silently discard empty initializations. The parser will
7562 already have pedwarned for empty brackets. */
7563 if (integer_zerop (constructor_unfilled_index
))
7564 constructor_type
= NULL_TREE
;
7567 gcc_assert (!TYPE_SIZE (constructor_type
));
7569 if (constructor_depth
> 2)
7570 error_init (loc
, "initialization of flexible array member in a nested context");
7572 pedwarn_init (loc
, OPT_Wpedantic
,
7573 "initialization of a flexible array member");
7575 /* We have already issued an error message for the existence
7576 of a flexible array member not at the end of the structure.
7577 Discard the initializer so that we do not die later. */
7578 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7579 constructor_type
= NULL_TREE
;
7583 switch (vec_safe_length (constructor_elements
))
7586 /* Initialization with { } counts as zeroinit. */
7587 constructor_zeroinit
= 1;
7590 /* This might be zeroinit as well. */
7591 if (integer_zerop ((*constructor_elements
)[0].value
))
7592 constructor_zeroinit
= 1;
7595 /* If the constructor has more than one element, it can't be { 0 }. */
7596 constructor_zeroinit
= 0;
7600 /* Warn when some structs are initialized with direct aggregation. */
7601 if (!implicit
&& found_missing_braces
&& warn_missing_braces
7602 && !constructor_zeroinit
)
7603 warning_init (loc
, OPT_Wmissing_braces
,
7604 "missing braces around initializer");
7606 /* Warn when some struct elements are implicitly initialized to zero. */
7607 if (warn_missing_field_initializers
7609 && TREE_CODE (constructor_type
) == RECORD_TYPE
7610 && constructor_unfilled_fields
)
7612 /* Do not warn for flexible array members or zero-length arrays. */
7613 while (constructor_unfilled_fields
7614 && (!DECL_SIZE (constructor_unfilled_fields
)
7615 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
7616 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
7618 if (constructor_unfilled_fields
7619 /* Do not warn if this level of the initializer uses member
7620 designators; it is likely to be deliberate. */
7621 && !constructor_designated
7622 /* Do not warn about initializing with { 0 } or with { }. */
7623 && !constructor_zeroinit
)
7625 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
7626 "missing initializer for field %qD of %qT",
7627 constructor_unfilled_fields
,
7629 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
7630 "%qD declared here", constructor_unfilled_fields
);
7634 /* Pad out the end of the structure. */
7635 if (p
->replacement_value
.value
)
7636 /* If this closes a superfluous brace pair,
7637 just pass out the element between them. */
7638 ret
= p
->replacement_value
;
7639 else if (constructor_type
== 0)
7641 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
7642 && TREE_CODE (constructor_type
) != UNION_TYPE
7643 && TREE_CODE (constructor_type
) != ARRAY_TYPE
7644 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
7646 /* A nonincremental scalar initializer--just return
7647 the element, after verifying there is just one. */
7648 if (vec_safe_is_empty (constructor_elements
))
7650 if (!constructor_erroneous
)
7651 error_init (loc
, "empty scalar initializer");
7652 ret
.value
= error_mark_node
;
7654 else if (vec_safe_length (constructor_elements
) != 1)
7656 error_init (loc
, "extra elements in scalar initializer");
7657 ret
.value
= (*constructor_elements
)[0].value
;
7660 ret
.value
= (*constructor_elements
)[0].value
;
7664 if (constructor_erroneous
)
7665 ret
.value
= error_mark_node
;
7668 ret
.value
= build_constructor (constructor_type
,
7669 constructor_elements
);
7670 if (constructor_constant
)
7671 TREE_CONSTANT (ret
.value
) = 1;
7672 if (constructor_constant
&& constructor_simple
)
7673 TREE_STATIC (ret
.value
) = 1;
7674 if (constructor_nonconst
)
7675 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
7679 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
7681 if (constructor_nonconst
)
7682 ret
.original_code
= C_MAYBE_CONST_EXPR
;
7683 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
7684 ret
.original_code
= ERROR_MARK
;
7687 constructor_type
= p
->type
;
7688 constructor_fields
= p
->fields
;
7689 constructor_index
= p
->index
;
7690 constructor_max_index
= p
->max_index
;
7691 constructor_unfilled_index
= p
->unfilled_index
;
7692 constructor_unfilled_fields
= p
->unfilled_fields
;
7693 constructor_bit_index
= p
->bit_index
;
7694 constructor_elements
= p
->elements
;
7695 constructor_constant
= p
->constant
;
7696 constructor_simple
= p
->simple
;
7697 constructor_nonconst
= p
->nonconst
;
7698 constructor_erroneous
= p
->erroneous
;
7699 constructor_incremental
= p
->incremental
;
7700 constructor_designated
= p
->designated
;
7701 designator_depth
= p
->designator_depth
;
7702 constructor_pending_elts
= p
->pending_elts
;
7703 constructor_depth
= p
->depth
;
7705 constructor_range_stack
= p
->range_stack
;
7706 RESTORE_SPELLING_DEPTH (constructor_depth
);
7708 constructor_stack
= p
->next
;
7711 if (ret
.value
== 0 && constructor_stack
== 0)
7712 ret
.value
= error_mark_node
;
7716 /* Common handling for both array range and field name designators.
7717 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7720 set_designator (location_t loc
, int array
,
7721 struct obstack
*braced_init_obstack
)
7724 enum tree_code subcode
;
7726 /* Don't die if an entire brace-pair level is superfluous
7727 in the containing level. */
7728 if (constructor_type
== 0)
7731 /* If there were errors in this designator list already, bail out
7733 if (designator_erroneous
)
7736 if (!designator_depth
)
7738 gcc_assert (!constructor_range_stack
);
7740 /* Designator list starts at the level of closest explicit
7742 while (constructor_stack
->implicit
)
7743 process_init_element (input_location
,
7744 pop_init_level (loc
, 1, braced_init_obstack
),
7745 true, braced_init_obstack
);
7746 constructor_designated
= 1;
7750 switch (TREE_CODE (constructor_type
))
7754 subtype
= TREE_TYPE (constructor_fields
);
7755 if (subtype
!= error_mark_node
)
7756 subtype
= TYPE_MAIN_VARIANT (subtype
);
7759 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7765 subcode
= TREE_CODE (subtype
);
7766 if (array
&& subcode
!= ARRAY_TYPE
)
7768 error_init (loc
, "array index in non-array initializer");
7771 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
7773 error_init (loc
, "field name not in record or union initializer");
7777 constructor_designated
= 1;
7778 push_init_level (loc
, 2, braced_init_obstack
);
7782 /* If there are range designators in designator list, push a new designator
7783 to constructor_range_stack. RANGE_END is end of such stack range or
7784 NULL_TREE if there is no range designator at this level. */
7787 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
7789 struct constructor_range_stack
*p
;
7791 p
= (struct constructor_range_stack
*)
7792 obstack_alloc (braced_init_obstack
,
7793 sizeof (struct constructor_range_stack
));
7794 p
->prev
= constructor_range_stack
;
7796 p
->fields
= constructor_fields
;
7797 p
->range_start
= constructor_index
;
7798 p
->index
= constructor_index
;
7799 p
->stack
= constructor_stack
;
7800 p
->range_end
= range_end
;
7801 if (constructor_range_stack
)
7802 constructor_range_stack
->next
= p
;
7803 constructor_range_stack
= p
;
7806 /* Within an array initializer, specify the next index to be initialized.
7807 FIRST is that index. If LAST is nonzero, then initialize a range
7808 of indices, running from FIRST through LAST. */
7811 set_init_index (location_t loc
, tree first
, tree last
,
7812 struct obstack
*braced_init_obstack
)
7814 if (set_designator (loc
, 1, braced_init_obstack
))
7817 designator_erroneous
= 1;
7819 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
7820 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
7822 error_init (loc
, "array index in initializer not of integer type");
7826 if (TREE_CODE (first
) != INTEGER_CST
)
7828 first
= c_fully_fold (first
, false, NULL
);
7829 if (TREE_CODE (first
) == INTEGER_CST
)
7830 pedwarn_init (loc
, OPT_Wpedantic
,
7831 "array index in initializer is not "
7832 "an integer constant expression");
7835 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
7837 last
= c_fully_fold (last
, false, NULL
);
7838 if (TREE_CODE (last
) == INTEGER_CST
)
7839 pedwarn_init (loc
, OPT_Wpedantic
,
7840 "array index in initializer is not "
7841 "an integer constant expression");
7844 if (TREE_CODE (first
) != INTEGER_CST
)
7845 error_init (loc
, "nonconstant array index in initializer");
7846 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
7847 error_init (loc
, "nonconstant array index in initializer");
7848 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7849 error_init (loc
, "array index in non-array initializer");
7850 else if (tree_int_cst_sgn (first
) == -1)
7851 error_init (loc
, "array index in initializer exceeds array bounds");
7852 else if (constructor_max_index
7853 && tree_int_cst_lt (constructor_max_index
, first
))
7854 error_init (loc
, "array index in initializer exceeds array bounds");
7857 constant_expression_warning (first
);
7859 constant_expression_warning (last
);
7860 constructor_index
= convert (bitsizetype
, first
);
7861 if (tree_int_cst_lt (constructor_index
, first
))
7863 constructor_index
= copy_node (constructor_index
);
7864 TREE_OVERFLOW (constructor_index
) = 1;
7869 if (tree_int_cst_equal (first
, last
))
7871 else if (tree_int_cst_lt (last
, first
))
7873 error_init (loc
, "empty index range in initializer");
7878 last
= convert (bitsizetype
, last
);
7879 if (constructor_max_index
!= 0
7880 && tree_int_cst_lt (constructor_max_index
, last
))
7882 error_init (loc
, "array index range in initializer exceeds "
7890 designator_erroneous
= 0;
7891 if (constructor_range_stack
|| last
)
7892 push_range_stack (last
, braced_init_obstack
);
7896 /* Within a struct initializer, specify the next field to be initialized. */
7899 set_init_label (location_t loc
, tree fieldname
,
7900 struct obstack
*braced_init_obstack
)
7904 if (set_designator (loc
, 0, braced_init_obstack
))
7907 designator_erroneous
= 1;
7909 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7910 && TREE_CODE (constructor_type
) != UNION_TYPE
)
7912 error_init (loc
, "field name not in record or union initializer");
7916 field
= lookup_field (constructor_type
, fieldname
);
7919 error_at (loc
, "unknown field %qE specified in initializer", fieldname
);
7923 constructor_fields
= TREE_VALUE (field
);
7925 designator_erroneous
= 0;
7926 if (constructor_range_stack
)
7927 push_range_stack (NULL_TREE
, braced_init_obstack
);
7928 field
= TREE_CHAIN (field
);
7931 if (set_designator (loc
, 0, braced_init_obstack
))
7935 while (field
!= NULL_TREE
);
7938 /* Add a new initializer to the tree of pending initializers. PURPOSE
7939 identifies the initializer, either array index or field in a structure.
7940 VALUE is the value of that index or field. If ORIGTYPE is not
7941 NULL_TREE, it is the original type of VALUE.
7943 IMPLICIT is true if value comes from pop_init_level (1),
7944 the new initializer has been merged with the existing one
7945 and thus no warnings should be emitted about overriding an
7946 existing initializer. */
7949 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
7950 bool implicit
, struct obstack
*braced_init_obstack
)
7952 struct init_node
*p
, **q
, *r
;
7954 q
= &constructor_pending_elts
;
7957 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7962 if (tree_int_cst_lt (purpose
, p
->purpose
))
7964 else if (tree_int_cst_lt (p
->purpose
, purpose
))
7970 if (TREE_SIDE_EFFECTS (p
->value
))
7971 warning_init (loc
, OPT_Woverride_init_side_effects
,
7972 "initialized field with side-effects "
7974 else if (warn_override_init
)
7975 warning_init (loc
, OPT_Woverride_init
,
7976 "initialized field overwritten");
7979 p
->origtype
= origtype
;
7988 bitpos
= bit_position (purpose
);
7992 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7994 else if (p
->purpose
!= purpose
)
8000 if (TREE_SIDE_EFFECTS (p
->value
))
8001 warning_init (loc
, OPT_Woverride_init_side_effects
,
8002 "initialized field with side-effects "
8004 else if (warn_override_init
)
8005 warning_init (loc
, OPT_Woverride_init
,
8006 "initialized field overwritten");
8009 p
->origtype
= origtype
;
8015 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
8016 sizeof (struct init_node
));
8017 r
->purpose
= purpose
;
8019 r
->origtype
= origtype
;
8029 struct init_node
*s
;
8033 if (p
->balance
== 0)
8035 else if (p
->balance
< 0)
8042 p
->left
->parent
= p
;
8059 constructor_pending_elts
= r
;
8064 struct init_node
*t
= r
->right
;
8068 r
->right
->parent
= r
;
8073 p
->left
->parent
= p
;
8076 p
->balance
= t
->balance
< 0;
8077 r
->balance
= -(t
->balance
> 0);
8092 constructor_pending_elts
= t
;
8098 /* p->balance == +1; growth of left side balances the node. */
8103 else /* r == p->right */
8105 if (p
->balance
== 0)
8106 /* Growth propagation from right side. */
8108 else if (p
->balance
> 0)
8115 p
->right
->parent
= p
;
8132 constructor_pending_elts
= r
;
8134 else /* r->balance == -1 */
8137 struct init_node
*t
= r
->left
;
8141 r
->left
->parent
= r
;
8146 p
->right
->parent
= p
;
8149 r
->balance
= (t
->balance
< 0);
8150 p
->balance
= -(t
->balance
> 0);
8165 constructor_pending_elts
= t
;
8171 /* p->balance == -1; growth of right side balances the node. */
8182 /* Build AVL tree from a sorted chain. */
8185 set_nonincremental_init (struct obstack
* braced_init_obstack
)
8187 unsigned HOST_WIDE_INT ix
;
8190 if (TREE_CODE (constructor_type
) != RECORD_TYPE
8191 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8194 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8195 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
8196 braced_init_obstack
);
8197 constructor_elements
= NULL
;
8198 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8200 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
8201 /* Skip any nameless bit fields at the beginning. */
8202 while (constructor_unfilled_fields
!= 0
8203 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8204 && DECL_NAME (constructor_unfilled_fields
) == 0)
8205 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
8208 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8210 if (TYPE_DOMAIN (constructor_type
))
8211 constructor_unfilled_index
8212 = convert (bitsizetype
,
8213 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8215 constructor_unfilled_index
= bitsize_zero_node
;
8217 constructor_incremental
= 0;
8220 /* Build AVL tree from a string constant. */
8223 set_nonincremental_init_from_string (tree str
,
8224 struct obstack
* braced_init_obstack
)
8226 tree value
, purpose
, type
;
8227 HOST_WIDE_INT val
[2];
8228 const char *p
, *end
;
8229 int byte
, wchar_bytes
, charwidth
, bitpos
;
8231 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8233 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8234 charwidth
= TYPE_PRECISION (char_type_node
);
8235 type
= TREE_TYPE (constructor_type
);
8236 p
= TREE_STRING_POINTER (str
);
8237 end
= p
+ TREE_STRING_LENGTH (str
);
8239 for (purpose
= bitsize_zero_node
;
8241 && !(constructor_max_index
8242 && tree_int_cst_lt (constructor_max_index
, purpose
));
8243 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8245 if (wchar_bytes
== 1)
8247 val
[0] = (unsigned char) *p
++;
8254 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8256 if (BYTES_BIG_ENDIAN
)
8257 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8259 bitpos
= byte
* charwidth
;
8260 val
[bitpos
% HOST_BITS_PER_WIDE_INT
]
8261 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8262 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8266 if (!TYPE_UNSIGNED (type
))
8268 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8269 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8271 if (val
[0] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
8273 val
[0] |= ((HOST_WIDE_INT
) -1) << bitpos
;
8277 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8282 else if (val
[1] & (((HOST_WIDE_INT
) 1)
8283 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8284 val
[1] |= ((HOST_WIDE_INT
) -1)
8285 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
8288 value
= wide_int_to_tree (type
,
8289 wide_int::from_array (val
, 2,
8290 HOST_BITS_PER_WIDE_INT
* 2));
8291 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
8292 braced_init_obstack
);
8295 constructor_incremental
= 0;
8298 /* Return value of FIELD in pending initializer or zero if the field was
8299 not initialized yet. */
8302 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8304 struct init_node
*p
;
8306 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8308 if (constructor_incremental
8309 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8310 set_nonincremental_init (braced_init_obstack
);
8312 p
= constructor_pending_elts
;
8315 if (tree_int_cst_lt (field
, p
->purpose
))
8317 else if (tree_int_cst_lt (p
->purpose
, field
))
8323 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8325 tree bitpos
= bit_position (field
);
8327 if (constructor_incremental
8328 && (!constructor_unfilled_fields
8329 || tree_int_cst_lt (bitpos
,
8330 bit_position (constructor_unfilled_fields
))))
8331 set_nonincremental_init (braced_init_obstack
);
8333 p
= constructor_pending_elts
;
8336 if (field
== p
->purpose
)
8338 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8344 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8346 if (!vec_safe_is_empty (constructor_elements
)
8347 && (constructor_elements
->last ().index
== field
))
8348 return constructor_elements
->last ().value
;
8353 /* "Output" the next constructor element.
8354 At top level, really output it to assembler code now.
8355 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8356 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8357 TYPE is the data type that the containing data type wants here.
8358 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8359 If VALUE is a string constant, STRICT_STRING is true if it is
8360 unparenthesized or we should not warn here for it being parenthesized.
8361 For other types of VALUE, STRICT_STRING is not used.
8363 PENDING if non-nil means output pending elements that belong
8364 right after this element. (PENDING is normally 1;
8365 it is 0 while outputting pending elements, to avoid recursion.)
8367 IMPLICIT is true if value comes from pop_init_level (1),
8368 the new initializer has been merged with the existing one
8369 and thus no warnings should be emitted about overriding an
8370 existing initializer. */
8373 output_init_element (location_t loc
, tree value
, tree origtype
,
8374 bool strict_string
, tree type
, tree field
, int pending
,
8375 bool implicit
, struct obstack
* braced_init_obstack
)
8377 tree semantic_type
= NULL_TREE
;
8378 bool maybe_const
= true;
8381 if (type
== error_mark_node
|| value
== error_mark_node
)
8383 constructor_erroneous
= 1;
8386 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8387 && (TREE_CODE (value
) == STRING_CST
8388 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8389 && !(TREE_CODE (value
) == STRING_CST
8390 && TREE_CODE (type
) == ARRAY_TYPE
8391 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8392 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8393 TYPE_MAIN_VARIANT (type
)))
8394 value
= array_to_pointer_conversion (input_location
, value
);
8396 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8397 && require_constant_value
&& pending
)
8399 /* As an extension, allow initializing objects with static storage
8400 duration with compound literals (which are then treated just as
8401 the brace enclosed list they contain). */
8403 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
8405 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8406 value
= DECL_INITIAL (decl
);
8409 npc
= null_pointer_constant_p (value
);
8410 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8412 semantic_type
= TREE_TYPE (value
);
8413 value
= TREE_OPERAND (value
, 0);
8415 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8417 if (value
== error_mark_node
)
8418 constructor_erroneous
= 1;
8419 else if (!TREE_CONSTANT (value
))
8420 constructor_constant
= 0;
8421 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
8422 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
8423 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8424 && DECL_C_BIT_FIELD (field
)
8425 && TREE_CODE (value
) != INTEGER_CST
))
8426 constructor_simple
= 0;
8428 constructor_nonconst
= 1;
8430 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8432 if (require_constant_value
)
8434 error_init (loc
, "initializer element is not constant");
8435 value
= error_mark_node
;
8437 else if (require_constant_elements
)
8438 pedwarn (loc
, OPT_Wpedantic
,
8439 "initializer element is not computable at load time");
8441 else if (!maybe_const
8442 && (require_constant_value
|| require_constant_elements
))
8443 pedwarn_init (loc
, OPT_Wpedantic
,
8444 "initializer element is not a constant expression");
8446 /* Issue -Wc++-compat warnings about initializing a bitfield with
8449 && field
!= NULL_TREE
8450 && TREE_CODE (field
) == FIELD_DECL
8451 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8452 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8453 != TYPE_MAIN_VARIANT (type
))
8454 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8456 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8457 if (checktype
!= error_mark_node
8458 && (TYPE_MAIN_VARIANT (checktype
)
8459 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8460 warning_init (loc
, OPT_Wc___compat
,
8461 "enum conversion in initialization is invalid in C++");
8464 /* If this field is empty (and not at the end of structure),
8465 don't do anything other than checking the initializer. */
8467 && (TREE_TYPE (field
) == error_mark_node
8468 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8469 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8470 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8471 || DECL_CHAIN (field
)))))
8475 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8476 value
= digest_init (loc
, type
, value
, origtype
, npc
, strict_string
,
8477 require_constant_value
);
8478 if (value
== error_mark_node
)
8480 constructor_erroneous
= 1;
8483 if (require_constant_value
|| require_constant_elements
)
8484 constant_expression_warning (value
);
8486 /* If this element doesn't come next in sequence,
8487 put it on constructor_pending_elts. */
8488 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8489 && (!constructor_incremental
8490 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8492 if (constructor_incremental
8493 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8494 set_nonincremental_init (braced_init_obstack
);
8496 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8497 braced_init_obstack
);
8500 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8501 && (!constructor_incremental
8502 || field
!= constructor_unfilled_fields
))
8504 /* We do this for records but not for unions. In a union,
8505 no matter which field is specified, it can be initialized
8506 right away since it starts at the beginning of the union. */
8507 if (constructor_incremental
)
8509 if (!constructor_unfilled_fields
)
8510 set_nonincremental_init (braced_init_obstack
);
8513 tree bitpos
, unfillpos
;
8515 bitpos
= bit_position (field
);
8516 unfillpos
= bit_position (constructor_unfilled_fields
);
8518 if (tree_int_cst_lt (bitpos
, unfillpos
))
8519 set_nonincremental_init (braced_init_obstack
);
8523 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8524 braced_init_obstack
);
8527 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8528 && !vec_safe_is_empty (constructor_elements
))
8532 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8533 warning_init (loc
, OPT_Woverride_init_side_effects
,
8534 "initialized field with side-effects overwritten");
8535 else if (warn_override_init
)
8536 warning_init (loc
, OPT_Woverride_init
,
8537 "initialized field overwritten");
8540 /* We can have just one union field set. */
8541 constructor_elements
= NULL
;
8544 /* Otherwise, output this element either to
8545 constructor_elements or to the assembler file. */
8547 constructor_elt celt
= {field
, value
};
8548 vec_safe_push (constructor_elements
, celt
);
8550 /* Advance the variable that indicates sequential elements output. */
8551 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8552 constructor_unfilled_index
8553 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8555 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8557 constructor_unfilled_fields
8558 = DECL_CHAIN (constructor_unfilled_fields
);
8560 /* Skip any nameless bit fields. */
8561 while (constructor_unfilled_fields
!= 0
8562 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8563 && DECL_NAME (constructor_unfilled_fields
) == 0)
8564 constructor_unfilled_fields
=
8565 DECL_CHAIN (constructor_unfilled_fields
);
8567 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8568 constructor_unfilled_fields
= 0;
8570 /* Now output any pending elements which have become next. */
8572 output_pending_init_elements (0, braced_init_obstack
);
8575 /* Output any pending elements which have become next.
8576 As we output elements, constructor_unfilled_{fields,index}
8577 advances, which may cause other elements to become next;
8578 if so, they too are output.
8580 If ALL is 0, we return when there are
8581 no more pending elements to output now.
8583 If ALL is 1, we output space as necessary so that
8584 we can output all the pending elements. */
8586 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8588 struct init_node
*elt
= constructor_pending_elts
;
8593 /* Look through the whole pending tree.
8594 If we find an element that should be output now,
8595 output it. Otherwise, set NEXT to the element
8596 that comes first among those still pending. */
8601 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8603 if (tree_int_cst_equal (elt
->purpose
,
8604 constructor_unfilled_index
))
8605 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8606 true, TREE_TYPE (constructor_type
),
8607 constructor_unfilled_index
, 0, false,
8608 braced_init_obstack
);
8609 else if (tree_int_cst_lt (constructor_unfilled_index
,
8612 /* Advance to the next smaller node. */
8617 /* We have reached the smallest node bigger than the
8618 current unfilled index. Fill the space first. */
8619 next
= elt
->purpose
;
8625 /* Advance to the next bigger node. */
8630 /* We have reached the biggest node in a subtree. Find
8631 the parent of it, which is the next bigger node. */
8632 while (elt
->parent
&& elt
->parent
->right
== elt
)
8635 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
8638 next
= elt
->purpose
;
8644 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8645 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8647 tree ctor_unfilled_bitpos
, elt_bitpos
;
8649 /* If the current record is complete we are done. */
8650 if (constructor_unfilled_fields
== 0)
8653 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
8654 elt_bitpos
= bit_position (elt
->purpose
);
8655 /* We can't compare fields here because there might be empty
8656 fields in between. */
8657 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
8659 constructor_unfilled_fields
= elt
->purpose
;
8660 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8661 true, TREE_TYPE (elt
->purpose
),
8662 elt
->purpose
, 0, false,
8663 braced_init_obstack
);
8665 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
8667 /* Advance to the next smaller node. */
8672 /* We have reached the smallest node bigger than the
8673 current unfilled field. Fill the space first. */
8674 next
= elt
->purpose
;
8680 /* Advance to the next bigger node. */
8685 /* We have reached the biggest node in a subtree. Find
8686 the parent of it, which is the next bigger node. */
8687 while (elt
->parent
&& elt
->parent
->right
== elt
)
8691 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
8692 bit_position (elt
->purpose
))))
8694 next
= elt
->purpose
;
8702 /* Ordinarily return, but not if we want to output all
8703 and there are elements left. */
8704 if (!(all
&& next
!= 0))
8707 /* If it's not incremental, just skip over the gap, so that after
8708 jumping to retry we will output the next successive element. */
8709 if (TREE_CODE (constructor_type
) == RECORD_TYPE
8710 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8711 constructor_unfilled_fields
= next
;
8712 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8713 constructor_unfilled_index
= next
;
8715 /* ELT now points to the node in the pending tree with the next
8716 initializer to output. */
8720 /* Add one non-braced element to the current constructor level.
8721 This adjusts the current position within the constructor's type.
8722 This may also start or terminate implicit levels
8723 to handle a partly-braced initializer.
8725 Once this has found the correct level for the new element,
8726 it calls output_init_element.
8728 IMPLICIT is true if value comes from pop_init_level (1),
8729 the new initializer has been merged with the existing one
8730 and thus no warnings should be emitted about overriding an
8731 existing initializer. */
8734 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
8735 struct obstack
* braced_init_obstack
)
8737 tree orig_value
= value
.value
;
8738 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
8739 bool strict_string
= value
.original_code
== STRING_CST
;
8740 bool was_designated
= designator_depth
!= 0;
8742 designator_depth
= 0;
8743 designator_erroneous
= 0;
8745 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
8746 constructor_zeroinit
= 0;
8748 /* Handle superfluous braces around string cst as in
8749 char x[] = {"foo"}; */
8753 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8754 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
8755 && integer_zerop (constructor_unfilled_index
))
8757 if (constructor_stack
->replacement_value
.value
)
8758 error_init (loc
, "excess elements in char array initializer");
8759 constructor_stack
->replacement_value
= value
;
8763 if (constructor_stack
->replacement_value
.value
!= 0)
8765 error_init (loc
, "excess elements in struct initializer");
8769 /* Ignore elements of a brace group if it is entirely superfluous
8770 and has already been diagnosed. */
8771 if (constructor_type
== 0)
8774 if (!implicit
&& warn_designated_init
&& !was_designated
8775 && TREE_CODE (constructor_type
) == RECORD_TYPE
8776 && lookup_attribute ("designated_init",
8777 TYPE_ATTRIBUTES (constructor_type
)))
8779 OPT_Wdesignated_init
,
8780 "positional initialization of field "
8781 "in %<struct%> declared with %<designated_init%> attribute");
8783 /* If we've exhausted any levels that didn't have braces,
8785 while (constructor_stack
->implicit
)
8787 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
8788 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8789 && constructor_fields
== 0)
8790 process_init_element (loc
,
8791 pop_init_level (loc
, 1, braced_init_obstack
),
8792 true, braced_init_obstack
);
8793 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
8794 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8795 && constructor_max_index
8796 && tree_int_cst_lt (constructor_max_index
,
8798 process_init_element (loc
,
8799 pop_init_level (loc
, 1, braced_init_obstack
),
8800 true, braced_init_obstack
);
8805 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8806 if (constructor_range_stack
)
8808 /* If value is a compound literal and we'll be just using its
8809 content, don't put it into a SAVE_EXPR. */
8810 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
8811 || !require_constant_value
)
8813 tree semantic_type
= NULL_TREE
;
8814 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
8816 semantic_type
= TREE_TYPE (value
.value
);
8817 value
.value
= TREE_OPERAND (value
.value
, 0);
8819 value
.value
= c_save_expr (value
.value
);
8821 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
8828 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8831 enum tree_code fieldcode
;
8833 if (constructor_fields
== 0)
8835 pedwarn_init (loc
, 0, "excess elements in struct initializer");
8839 fieldtype
= TREE_TYPE (constructor_fields
);
8840 if (fieldtype
!= error_mark_node
)
8841 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8842 fieldcode
= TREE_CODE (fieldtype
);
8844 /* Error for non-static initialization of a flexible array member. */
8845 if (fieldcode
== ARRAY_TYPE
8846 && !require_constant_value
8847 && TYPE_SIZE (fieldtype
) == NULL_TREE
8848 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8850 error_init (loc
, "non-static initialization of a flexible "
8855 /* Error for initialization of a flexible array member with
8856 a string constant if the structure is in an array. E.g.:
8857 struct S { int x; char y[]; };
8858 struct S s[] = { { 1, "foo" } };
8861 && fieldcode
== ARRAY_TYPE
8862 && constructor_depth
> 1
8863 && TYPE_SIZE (fieldtype
) == NULL_TREE
8864 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8866 bool in_array_p
= false;
8867 for (struct constructor_stack
*p
= constructor_stack
;
8868 p
&& p
->type
; p
= p
->next
)
8869 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
8876 error_init (loc
, "initialization of flexible array "
8877 "member in a nested context");
8882 /* Accept a string constant to initialize a subarray. */
8883 if (value
.value
!= 0
8884 && fieldcode
== ARRAY_TYPE
8885 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8887 value
.value
= orig_value
;
8888 /* Otherwise, if we have come to a subaggregate,
8889 and we don't have an element of its type, push into it. */
8890 else if (value
.value
!= 0
8891 && value
.value
!= error_mark_node
8892 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8893 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8894 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8896 push_init_level (loc
, 1, braced_init_obstack
);
8902 push_member_name (constructor_fields
);
8903 output_init_element (loc
, value
.value
, value
.original_type
,
8904 strict_string
, fieldtype
,
8905 constructor_fields
, 1, implicit
,
8906 braced_init_obstack
);
8907 RESTORE_SPELLING_DEPTH (constructor_depth
);
8910 /* Do the bookkeeping for an element that was
8911 directly output as a constructor. */
8913 /* For a record, keep track of end position of last field. */
8914 if (DECL_SIZE (constructor_fields
))
8915 constructor_bit_index
8916 = size_binop_loc (input_location
, PLUS_EXPR
,
8917 bit_position (constructor_fields
),
8918 DECL_SIZE (constructor_fields
));
8920 /* If the current field was the first one not yet written out,
8921 it isn't now, so update. */
8922 if (constructor_unfilled_fields
== constructor_fields
)
8924 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8925 /* Skip any nameless bit fields. */
8926 while (constructor_unfilled_fields
!= 0
8927 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8928 && DECL_NAME (constructor_unfilled_fields
) == 0)
8929 constructor_unfilled_fields
=
8930 DECL_CHAIN (constructor_unfilled_fields
);
8934 constructor_fields
= DECL_CHAIN (constructor_fields
);
8935 /* Skip any nameless bit fields at the beginning. */
8936 while (constructor_fields
!= 0
8937 && DECL_C_BIT_FIELD (constructor_fields
)
8938 && DECL_NAME (constructor_fields
) == 0)
8939 constructor_fields
= DECL_CHAIN (constructor_fields
);
8941 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8944 enum tree_code fieldcode
;
8946 if (constructor_fields
== 0)
8948 pedwarn_init (loc
, 0,
8949 "excess elements in union initializer");
8953 fieldtype
= TREE_TYPE (constructor_fields
);
8954 if (fieldtype
!= error_mark_node
)
8955 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8956 fieldcode
= TREE_CODE (fieldtype
);
8958 /* Warn that traditional C rejects initialization of unions.
8959 We skip the warning if the value is zero. This is done
8960 under the assumption that the zero initializer in user
8961 code appears conditioned on e.g. __STDC__ to avoid
8962 "missing initializer" warnings and relies on default
8963 initialization to zero in the traditional C case.
8964 We also skip the warning if the initializer is designated,
8965 again on the assumption that this must be conditional on
8966 __STDC__ anyway (and we've already complained about the
8967 member-designator already). */
8968 if (!in_system_header_at (input_location
) && !constructor_designated
8969 && !(value
.value
&& (integer_zerop (value
.value
)
8970 || real_zerop (value
.value
))))
8971 warning (OPT_Wtraditional
, "traditional C rejects initialization "
8974 /* Accept a string constant to initialize a subarray. */
8975 if (value
.value
!= 0
8976 && fieldcode
== ARRAY_TYPE
8977 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8979 value
.value
= orig_value
;
8980 /* Otherwise, if we have come to a subaggregate,
8981 and we don't have an element of its type, push into it. */
8982 else if (value
.value
!= 0
8983 && value
.value
!= error_mark_node
8984 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8985 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8986 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8988 push_init_level (loc
, 1, braced_init_obstack
);
8994 push_member_name (constructor_fields
);
8995 output_init_element (loc
, value
.value
, value
.original_type
,
8996 strict_string
, fieldtype
,
8997 constructor_fields
, 1, implicit
,
8998 braced_init_obstack
);
8999 RESTORE_SPELLING_DEPTH (constructor_depth
);
9002 /* Do the bookkeeping for an element that was
9003 directly output as a constructor. */
9005 constructor_bit_index
= DECL_SIZE (constructor_fields
);
9006 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9009 constructor_fields
= 0;
9011 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9013 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9014 enum tree_code eltcode
= TREE_CODE (elttype
);
9016 /* Accept a string constant to initialize a subarray. */
9017 if (value
.value
!= 0
9018 && eltcode
== ARRAY_TYPE
9019 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
9021 value
.value
= orig_value
;
9022 /* Otherwise, if we have come to a subaggregate,
9023 and we don't have an element of its type, push into it. */
9024 else if (value
.value
!= 0
9025 && value
.value
!= error_mark_node
9026 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
9027 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
9028 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
9030 push_init_level (loc
, 1, braced_init_obstack
);
9034 if (constructor_max_index
!= 0
9035 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
9036 || integer_all_onesp (constructor_max_index
)))
9038 pedwarn_init (loc
, 0,
9039 "excess elements in array initializer");
9043 /* Now output the actual element. */
9046 push_array_bounds (tree_to_uhwi (constructor_index
));
9047 output_init_element (loc
, value
.value
, value
.original_type
,
9048 strict_string
, elttype
,
9049 constructor_index
, 1, implicit
,
9050 braced_init_obstack
);
9051 RESTORE_SPELLING_DEPTH (constructor_depth
);
9055 = size_binop_loc (input_location
, PLUS_EXPR
,
9056 constructor_index
, bitsize_one_node
);
9059 /* If we are doing the bookkeeping for an element that was
9060 directly output as a constructor, we must update
9061 constructor_unfilled_index. */
9062 constructor_unfilled_index
= constructor_index
;
9064 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
9066 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9068 /* Do a basic check of initializer size. Note that vectors
9069 always have a fixed size derived from their type. */
9070 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
9072 pedwarn_init (loc
, 0,
9073 "excess elements in vector initializer");
9077 /* Now output the actual element. */
9080 if (TREE_CODE (value
.value
) == VECTOR_CST
)
9081 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
9082 output_init_element (loc
, value
.value
, value
.original_type
,
9083 strict_string
, elttype
,
9084 constructor_index
, 1, implicit
,
9085 braced_init_obstack
);
9089 = size_binop_loc (input_location
,
9090 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
9093 /* If we are doing the bookkeeping for an element that was
9094 directly output as a constructor, we must update
9095 constructor_unfilled_index. */
9096 constructor_unfilled_index
= constructor_index
;
9099 /* Handle the sole element allowed in a braced initializer
9100 for a scalar variable. */
9101 else if (constructor_type
!= error_mark_node
9102 && constructor_fields
== 0)
9104 pedwarn_init (loc
, 0,
9105 "excess elements in scalar initializer");
9111 output_init_element (loc
, value
.value
, value
.original_type
,
9112 strict_string
, constructor_type
,
9113 NULL_TREE
, 1, implicit
,
9114 braced_init_obstack
);
9115 constructor_fields
= 0;
9118 /* Handle range initializers either at this level or anywhere higher
9119 in the designator stack. */
9120 if (constructor_range_stack
)
9122 struct constructor_range_stack
*p
, *range_stack
;
9125 range_stack
= constructor_range_stack
;
9126 constructor_range_stack
= 0;
9127 while (constructor_stack
!= range_stack
->stack
)
9129 gcc_assert (constructor_stack
->implicit
);
9130 process_init_element (loc
,
9131 pop_init_level (loc
, 1,
9132 braced_init_obstack
),
9133 true, braced_init_obstack
);
9135 for (p
= range_stack
;
9136 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
9139 gcc_assert (constructor_stack
->implicit
);
9140 process_init_element (loc
,
9141 pop_init_level (loc
, 1,
9142 braced_init_obstack
),
9143 true, braced_init_obstack
);
9146 p
->index
= size_binop_loc (input_location
,
9147 PLUS_EXPR
, p
->index
, bitsize_one_node
);
9148 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
9153 constructor_index
= p
->index
;
9154 constructor_fields
= p
->fields
;
9155 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
9163 push_init_level (loc
, 2, braced_init_obstack
);
9164 p
->stack
= constructor_stack
;
9165 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
9166 p
->index
= p
->range_start
;
9170 constructor_range_stack
= range_stack
;
9177 constructor_range_stack
= 0;
9180 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9181 (guaranteed to be 'volatile' or null) and ARGS (represented using
9182 an ASM_EXPR node). */
9184 build_asm_stmt (tree cv_qualifier
, tree args
)
9186 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
9187 ASM_VOLATILE_P (args
) = 1;
9188 return add_stmt (args
);
9191 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9192 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9193 SIMPLE indicates whether there was anything at all after the
9194 string in the asm expression -- asm("blah") and asm("blah" : )
9195 are subtly different. We use a ASM_EXPR node to represent this. */
9197 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
9198 tree clobbers
, tree labels
, bool simple
)
9203 const char *constraint
;
9204 const char **oconstraints
;
9205 bool allows_mem
, allows_reg
, is_inout
;
9206 int ninputs
, noutputs
;
9208 ninputs
= list_length (inputs
);
9209 noutputs
= list_length (outputs
);
9210 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
9212 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
9214 /* Remove output conversions that change the type but not the mode. */
9215 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9217 tree output
= TREE_VALUE (tail
);
9219 output
= c_fully_fold (output
, false, NULL
);
9221 /* ??? Really, this should not be here. Users should be using a
9222 proper lvalue, dammit. But there's a long history of using casts
9223 in the output operands. In cases like longlong.h, this becomes a
9224 primitive form of typechecking -- if the cast can be removed, then
9225 the output operand had a type of the proper width; otherwise we'll
9226 get an error. Gross, but ... */
9227 STRIP_NOPS (output
);
9229 if (!lvalue_or_else (loc
, output
, lv_asm
))
9230 output
= error_mark_node
;
9232 if (output
!= error_mark_node
9233 && (TREE_READONLY (output
)
9234 || TYPE_READONLY (TREE_TYPE (output
))
9235 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
9236 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
9237 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
9238 readonly_error (loc
, output
, lv_asm
);
9240 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9241 oconstraints
[i
] = constraint
;
9243 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
9244 &allows_mem
, &allows_reg
, &is_inout
))
9246 /* If the operand is going to end up in memory,
9247 mark it addressable. */
9248 if (!allows_reg
&& !c_mark_addressable (output
))
9249 output
= error_mark_node
;
9250 if (!(!allows_reg
&& allows_mem
)
9251 && output
!= error_mark_node
9252 && VOID_TYPE_P (TREE_TYPE (output
)))
9254 error_at (loc
, "invalid use of void expression");
9255 output
= error_mark_node
;
9259 output
= error_mark_node
;
9261 TREE_VALUE (tail
) = output
;
9264 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9268 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9269 input
= TREE_VALUE (tail
);
9271 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
9272 oconstraints
, &allows_mem
, &allows_reg
))
9274 /* If the operand is going to end up in memory,
9275 mark it addressable. */
9276 if (!allows_reg
&& allows_mem
)
9278 input
= c_fully_fold (input
, false, NULL
);
9280 /* Strip the nops as we allow this case. FIXME, this really
9281 should be rejected or made deprecated. */
9283 if (!c_mark_addressable (input
))
9284 input
= error_mark_node
;
9289 memset (&expr
, 0, sizeof (expr
));
9291 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9292 input
= c_fully_fold (expr
.value
, false, NULL
);
9294 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9296 error_at (loc
, "invalid use of void expression");
9297 input
= error_mark_node
;
9302 input
= error_mark_node
;
9304 TREE_VALUE (tail
) = input
;
9307 /* ASMs with labels cannot have outputs. This should have been
9308 enforced by the parser. */
9309 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9311 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9313 /* asm statements without outputs, including simple ones, are treated
9315 ASM_INPUT_P (args
) = simple
;
9316 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9321 /* Generate a goto statement to LABEL. LOC is the location of the
9325 c_finish_goto_label (location_t loc
, tree label
)
9327 tree decl
= lookup_label_for_goto (loc
, label
);
9330 TREE_USED (decl
) = 1;
9332 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9333 SET_EXPR_LOCATION (t
, loc
);
9334 return add_stmt (t
);
9338 /* Generate a computed goto statement to EXPR. LOC is the location of
9342 c_finish_goto_ptr (location_t loc
, tree expr
)
9345 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9346 expr
= c_fully_fold (expr
, false, NULL
);
9347 expr
= convert (ptr_type_node
, expr
);
9348 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9349 SET_EXPR_LOCATION (t
, loc
);
9350 return add_stmt (t
);
9353 /* Generate a C `return' statement. RETVAL is the expression for what
9354 to return, or a null pointer for `return;' with no value. LOC is
9355 the location of the return statement, or the location of the expression,
9356 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9357 is the original type of RETVAL. */
9360 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9362 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9363 bool no_warning
= false;
9367 if (TREE_THIS_VOLATILE (current_function_decl
))
9369 "function declared %<noreturn%> has a %<return%> statement");
9371 if (flag_cilkplus
&& contains_array_notation_expr (retval
))
9373 /* Array notations are allowed in a return statement if it is inside a
9374 built-in array notation reduction function. */
9375 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9376 return error_mark_node
;
9379 error_at (loc
, "array notation expression cannot be used as a "
9381 return error_mark_node
;
9384 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
9386 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9388 return error_mark_node
;
9392 tree semantic_type
= NULL_TREE
;
9393 npc
= null_pointer_constant_p (retval
);
9394 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9396 semantic_type
= TREE_TYPE (retval
);
9397 retval
= TREE_OPERAND (retval
, 0);
9399 retval
= c_fully_fold (retval
, false, NULL
);
9401 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9406 current_function_returns_null
= 1;
9407 if ((warn_return_type
|| flag_isoc99
)
9408 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9411 pedwarn (loc
, 0, "%<return%> with no value, in "
9412 "function returning non-void");
9414 warning_at (loc
, OPT_Wreturn_type
, "%<return%> with no value, "
9415 "in function returning non-void");
9419 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9421 current_function_returns_null
= 1;
9422 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9424 "%<return%> with a value, in function returning void");
9426 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
9427 "%<return%> with expression, in function returning void");
9431 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
9432 retval
, origtype
, ic_return
,
9433 npc
, NULL_TREE
, NULL_TREE
, 0);
9434 tree res
= DECL_RESULT (current_function_decl
);
9438 current_function_returns_value
= 1;
9439 if (t
== error_mark_node
)
9442 save
= in_late_binary_op
;
9443 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9444 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
9445 || (TREE_CODE (TREE_TYPE (t
)) == REAL_TYPE
9446 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
9447 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
9448 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
9449 in_late_binary_op
= true;
9450 inner
= t
= convert (TREE_TYPE (res
), t
);
9451 in_late_binary_op
= save
;
9453 /* Strip any conversions, additions, and subtractions, and see if
9454 we are returning the address of a local variable. Warn if so. */
9457 switch (TREE_CODE (inner
))
9460 case NON_LVALUE_EXPR
:
9462 case POINTER_PLUS_EXPR
:
9463 inner
= TREE_OPERAND (inner
, 0);
9467 /* If the second operand of the MINUS_EXPR has a pointer
9468 type (or is converted from it), this may be valid, so
9469 don't give a warning. */
9471 tree op1
= TREE_OPERAND (inner
, 1);
9473 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9474 && (CONVERT_EXPR_P (op1
)
9475 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9476 op1
= TREE_OPERAND (op1
, 0);
9478 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9481 inner
= TREE_OPERAND (inner
, 0);
9486 inner
= TREE_OPERAND (inner
, 0);
9488 while (REFERENCE_CLASS_P (inner
)
9489 && TREE_CODE (inner
) != INDIRECT_REF
)
9490 inner
= TREE_OPERAND (inner
, 0);
9493 && !DECL_EXTERNAL (inner
)
9494 && !TREE_STATIC (inner
)
9495 && DECL_CONTEXT (inner
) == current_function_decl
)
9497 if (TREE_CODE (inner
) == LABEL_DECL
)
9498 warning_at (loc
, OPT_Wreturn_local_addr
,
9499 "function returns address of label");
9502 warning_at (loc
, OPT_Wreturn_local_addr
,
9503 "function returns address of local variable");
9504 tree zero
= build_zero_cst (TREE_TYPE (res
));
9505 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
9517 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9518 SET_EXPR_LOCATION (retval
, loc
);
9520 if (warn_sequence_point
)
9521 verify_sequence_points (retval
);
9524 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9525 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9526 return add_stmt (ret_stmt
);
9530 /* The SWITCH_EXPR being built. */
9533 /* The original type of the testing expression, i.e. before the
9534 default conversion is applied. */
9537 /* A splay-tree mapping the low element of a case range to the high
9538 element, or NULL_TREE if there is no high element. Used to
9539 determine whether or not a new case label duplicates an old case
9540 label. We need a tree, rather than simply a hash table, because
9541 of the GNU case range extension. */
9544 /* The bindings at the point of the switch. This is used for
9545 warnings crossing decls when branching to a case label. */
9546 struct c_spot_bindings
*bindings
;
9548 /* The next node on the stack. */
9549 struct c_switch
*next
;
9552 /* A stack of the currently active switch statements. The innermost
9553 switch statement is on the top of the stack. There is no need to
9554 mark the stack for garbage collection because it is only active
9555 during the processing of the body of a function, and we never
9556 collect at that point. */
9558 struct c_switch
*c_switch_stack
;
9560 /* Start a C switch statement, testing expression EXP. Return the new
9561 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9562 SWITCH_COND_LOC is the location of the switch's condition.
9563 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9566 c_start_case (location_t switch_loc
,
9567 location_t switch_cond_loc
,
9568 tree exp
, bool explicit_cast_p
)
9570 tree orig_type
= error_mark_node
;
9571 struct c_switch
*cs
;
9573 if (exp
!= error_mark_node
)
9575 orig_type
= TREE_TYPE (exp
);
9577 if (!INTEGRAL_TYPE_P (orig_type
))
9579 if (orig_type
!= error_mark_node
)
9581 error_at (switch_cond_loc
, "switch quantity not an integer");
9582 orig_type
= error_mark_node
;
9584 exp
= integer_zero_node
;
9588 tree type
= TYPE_MAIN_VARIANT (orig_type
);
9591 /* Warn if the condition has boolean value. */
9592 while (TREE_CODE (e
) == COMPOUND_EXPR
)
9593 e
= TREE_OPERAND (e
, 1);
9595 if ((TREE_CODE (type
) == BOOLEAN_TYPE
9596 || truth_value_p (TREE_CODE (e
)))
9597 /* Explicit cast to int suppresses this warning. */
9598 && !(TREE_CODE (type
) == INTEGER_TYPE
9599 && explicit_cast_p
))
9600 warning_at (switch_cond_loc
, OPT_Wswitch_bool
,
9601 "switch condition has boolean value");
9603 if (!in_system_header_at (input_location
)
9604 && (type
== long_integer_type_node
9605 || type
== long_unsigned_type_node
))
9606 warning_at (switch_cond_loc
,
9607 OPT_Wtraditional
, "%<long%> switch expression not "
9608 "converted to %<int%> in ISO C");
9610 exp
= c_fully_fold (exp
, false, NULL
);
9611 exp
= default_conversion (exp
);
9613 if (warn_sequence_point
)
9614 verify_sequence_points (exp
);
9618 /* Add this new SWITCH_EXPR to the stack. */
9619 cs
= XNEW (struct c_switch
);
9620 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
9621 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
9622 cs
->orig_type
= orig_type
;
9623 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
9624 cs
->bindings
= c_get_switch_bindings ();
9625 cs
->next
= c_switch_stack
;
9626 c_switch_stack
= cs
;
9628 return add_stmt (cs
->switch_expr
);
9631 /* Process a case label at location LOC. */
9634 do_case (location_t loc
, tree low_value
, tree high_value
)
9636 tree label
= NULL_TREE
;
9638 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
9640 low_value
= c_fully_fold (low_value
, false, NULL
);
9641 if (TREE_CODE (low_value
) == INTEGER_CST
)
9642 pedwarn (loc
, OPT_Wpedantic
,
9643 "case label is not an integer constant expression");
9646 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
9648 high_value
= c_fully_fold (high_value
, false, NULL
);
9649 if (TREE_CODE (high_value
) == INTEGER_CST
)
9650 pedwarn (input_location
, OPT_Wpedantic
,
9651 "case label is not an integer constant expression");
9654 if (c_switch_stack
== NULL
)
9657 error_at (loc
, "case label not within a switch statement");
9659 error_at (loc
, "%<default%> label not within a switch statement");
9663 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
9664 EXPR_LOCATION (c_switch_stack
->switch_expr
),
9668 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
9669 SWITCH_COND (c_switch_stack
->switch_expr
),
9670 c_switch_stack
->orig_type
,
9671 low_value
, high_value
);
9672 if (label
== error_mark_node
)
9677 /* Finish the switch statement. TYPE is the original type of the
9678 controlling expression of the switch, or NULL_TREE. */
9681 c_finish_case (tree body
, tree type
)
9683 struct c_switch
*cs
= c_switch_stack
;
9684 location_t switch_location
;
9686 SWITCH_BODY (cs
->switch_expr
) = body
;
9688 /* Emit warnings as needed. */
9689 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
9690 c_do_switch_warnings (cs
->cases
, switch_location
,
9691 type
? type
: TREE_TYPE (cs
->switch_expr
),
9692 SWITCH_COND (cs
->switch_expr
));
9694 /* Pop the stack. */
9695 c_switch_stack
= cs
->next
;
9696 splay_tree_delete (cs
->cases
);
9697 c_release_switch_bindings (cs
->bindings
);
9701 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9702 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9703 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9704 statement, and was not surrounded with parenthesis. */
9707 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
9708 tree else_block
, bool nested_if
)
9712 /* If the condition has array notations, then the rank of the then_block and
9713 else_block must be either 0 or be equal to the rank of the condition. If
9714 the condition does not have array notations then break them up as it is
9715 broken up in a normal expression. */
9716 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
9718 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
9719 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
9722 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
9725 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
9727 if (cond_rank
!= then_rank
&& then_rank
!= 0)
9729 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9730 " and the then-block");
9733 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
9735 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9736 " and the else-block");
9740 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9741 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
9743 tree inner_if
= then_block
;
9745 /* We know from the grammar productions that there is an IF nested
9746 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9747 it might not be exactly THEN_BLOCK, but should be the last
9748 non-container statement within. */
9750 switch (TREE_CODE (inner_if
))
9755 inner_if
= BIND_EXPR_BODY (inner_if
);
9757 case STATEMENT_LIST
:
9758 inner_if
= expr_last (then_block
);
9760 case TRY_FINALLY_EXPR
:
9761 case TRY_CATCH_EXPR
:
9762 inner_if
= TREE_OPERAND (inner_if
, 0);
9769 if (COND_EXPR_ELSE (inner_if
))
9770 warning_at (if_locus
, OPT_Wparentheses
,
9771 "suggest explicit braces to avoid ambiguous %<else%>");
9774 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
9775 SET_EXPR_LOCATION (stmt
, if_locus
);
9779 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9780 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9781 is false for DO loops. INCR is the FOR increment expression. BODY is
9782 the statement controlled by the loop. BLAB is the break label. CLAB is
9783 the continue label. Everything is allowed to be NULL. */
9786 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
9787 tree blab
, tree clab
, bool cond_is_first
)
9789 tree entry
= NULL
, exit
= NULL
, t
;
9791 /* In theory could forbid cilk spawn for loop increment expression,
9792 but it should work just fine. */
9794 /* If the condition is zero don't generate a loop construct. */
9795 if (cond
&& integer_zerop (cond
))
9799 t
= build_and_jump (&blab
);
9800 SET_EXPR_LOCATION (t
, start_locus
);
9806 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9808 /* If we have an exit condition, then we build an IF with gotos either
9809 out of the loop, or to the top of it. If there's no exit condition,
9810 then we just build a jump back to the top. */
9811 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
9813 if (cond
&& !integer_nonzerop (cond
))
9815 /* Canonicalize the loop condition to the end. This means
9816 generating a branch to the loop condition. Reuse the
9817 continue label, if possible. */
9822 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9823 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
9826 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
9827 SET_EXPR_LOCATION (t
, start_locus
);
9831 t
= build_and_jump (&blab
);
9833 exit
= fold_build3_loc (start_locus
,
9834 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9836 exit
= fold_build3_loc (input_location
,
9837 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9846 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
9854 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
9858 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
9861 tree label
= *label_p
;
9863 /* In switch statements break is sometimes stylistically used after
9864 a return statement. This can lead to spurious warnings about
9865 control reaching the end of a non-void function when it is
9866 inlined. Note that we are calling block_may_fallthru with
9867 language specific tree nodes; this works because
9868 block_may_fallthru returns true when given something it does not
9870 skip
= !block_may_fallthru (cur_stmt_list
);
9875 *label_p
= label
= create_artificial_label (loc
);
9877 else if (TREE_CODE (label
) == LABEL_DECL
)
9879 else switch (TREE_INT_CST_LOW (label
))
9883 error_at (loc
, "break statement not within loop or switch");
9885 error_at (loc
, "continue statement not within a loop");
9889 gcc_assert (is_break
);
9890 error_at (loc
, "break statement used with OpenMP for loop");
9895 error ("break statement within %<#pragma simd%> loop body");
9897 error ("continue statement within %<#pragma simd%> loop body");
9908 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
9910 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
9913 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9916 emit_side_effect_warnings (location_t loc
, tree expr
)
9918 if (expr
== error_mark_node
)
9920 else if (!TREE_SIDE_EFFECTS (expr
))
9922 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
9923 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
9925 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
9928 location_t cloc
= loc
;
9929 while (TREE_CODE (r
) == COMPOUND_EXPR
)
9931 if (EXPR_HAS_LOCATION (r
))
9932 cloc
= EXPR_LOCATION (r
);
9933 r
= TREE_OPERAND (r
, 1);
9935 if (!TREE_SIDE_EFFECTS (r
)
9936 && !VOID_TYPE_P (TREE_TYPE (r
))
9937 && !CONVERT_EXPR_P (r
)
9938 && !TREE_NO_WARNING (r
)
9939 && !TREE_NO_WARNING (expr
))
9940 warning_at (cloc
, OPT_Wunused_value
,
9941 "right-hand operand of comma expression has no effect");
9944 warn_if_unused_value (expr
, loc
);
9947 /* Process an expression as if it were a complete statement. Emit
9948 diagnostics, but do not call ADD_STMT. LOC is the location of the
9952 c_process_expr_stmt (location_t loc
, tree expr
)
9959 expr
= c_fully_fold (expr
, false, NULL
);
9961 if (warn_sequence_point
)
9962 verify_sequence_points (expr
);
9964 if (TREE_TYPE (expr
) != error_mark_node
9965 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
9966 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
9967 error_at (loc
, "expression statement has incomplete type");
9969 /* If we're not processing a statement expression, warn about unused values.
9970 Warnings for statement expressions will be emitted later, once we figure
9971 out which is the result. */
9972 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9973 && warn_unused_value
)
9974 emit_side_effect_warnings (loc
, expr
);
9977 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
9978 exprv
= TREE_OPERAND (exprv
, 1);
9979 while (CONVERT_EXPR_P (exprv
))
9980 exprv
= TREE_OPERAND (exprv
, 0);
9982 || handled_component_p (exprv
)
9983 || TREE_CODE (exprv
) == ADDR_EXPR
)
9984 mark_exp_read (exprv
);
9986 /* If the expression is not of a type to which we cannot assign a line
9987 number, wrap the thing in a no-op NOP_EXPR. */
9988 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
9990 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
9991 SET_EXPR_LOCATION (expr
, loc
);
9997 /* Emit an expression as a statement. LOC is the location of the
10001 c_finish_expr_stmt (location_t loc
, tree expr
)
10004 return add_stmt (c_process_expr_stmt (loc
, expr
));
10009 /* Do the opposite and emit a statement as an expression. To begin,
10010 create a new binding level and return it. */
10013 c_begin_stmt_expr (void)
10017 /* We must force a BLOCK for this level so that, if it is not expanded
10018 later, there is a way to turn off the entire subtree of blocks that
10019 are contained in it. */
10020 keep_next_level ();
10021 ret
= c_begin_compound_stmt (true);
10023 c_bindings_start_stmt_expr (c_switch_stack
== NULL
10025 : c_switch_stack
->bindings
);
10027 /* Mark the current statement list as belonging to a statement list. */
10028 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
10033 /* LOC is the location of the compound statement to which this body
10037 c_finish_stmt_expr (location_t loc
, tree body
)
10039 tree last
, type
, tmp
, val
;
10042 body
= c_end_compound_stmt (loc
, body
, true);
10044 c_bindings_end_stmt_expr (c_switch_stack
== NULL
10046 : c_switch_stack
->bindings
);
10048 /* Locate the last statement in BODY. See c_end_compound_stmt
10049 about always returning a BIND_EXPR. */
10050 last_p
= &BIND_EXPR_BODY (body
);
10051 last
= BIND_EXPR_BODY (body
);
10053 continue_searching
:
10054 if (TREE_CODE (last
) == STATEMENT_LIST
)
10056 tree_stmt_iterator i
;
10058 /* This can happen with degenerate cases like ({ }). No value. */
10059 if (!TREE_SIDE_EFFECTS (last
))
10062 /* If we're supposed to generate side effects warnings, process
10063 all of the statements except the last. */
10064 if (warn_unused_value
)
10066 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
10069 tree t
= tsi_stmt (i
);
10071 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
10072 emit_side_effect_warnings (tloc
, t
);
10076 i
= tsi_last (last
);
10077 last_p
= tsi_stmt_ptr (i
);
10081 /* If the end of the list is exception related, then the list was split
10082 by a call to push_cleanup. Continue searching. */
10083 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
10084 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
10086 last_p
= &TREE_OPERAND (last
, 0);
10088 goto continue_searching
;
10091 if (last
== error_mark_node
)
10094 /* In the case that the BIND_EXPR is not necessary, return the
10095 expression out from inside it. */
10096 if (last
== BIND_EXPR_BODY (body
)
10097 && BIND_EXPR_VARS (body
) == NULL
)
10099 /* Even if this looks constant, do not allow it in a constant
10101 last
= c_wrap_maybe_const (last
, true);
10102 /* Do not warn if the return value of a statement expression is
10104 TREE_NO_WARNING (last
) = 1;
10108 /* Extract the type of said expression. */
10109 type
= TREE_TYPE (last
);
10111 /* If we're not returning a value at all, then the BIND_EXPR that
10112 we already have is a fine expression to return. */
10113 if (!type
|| VOID_TYPE_P (type
))
10116 /* Now that we've located the expression containing the value, it seems
10117 silly to make voidify_wrapper_expr repeat the process. Create a
10118 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10119 tmp
= create_tmp_var_raw (type
);
10121 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10122 tree_expr_nonnegative_p giving up immediately. */
10124 if (TREE_CODE (val
) == NOP_EXPR
10125 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
10126 val
= TREE_OPERAND (val
, 0);
10128 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
10129 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
10132 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
10133 SET_EXPR_LOCATION (t
, loc
);
10138 /* Begin and end compound statements. This is as simple as pushing
10139 and popping new statement lists from the tree. */
10142 c_begin_compound_stmt (bool do_scope
)
10144 tree stmt
= push_stmt_list ();
10150 /* End a compound statement. STMT is the statement. LOC is the
10151 location of the compound statement-- this is usually the location
10152 of the opening brace. */
10155 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
10161 if (c_dialect_objc ())
10162 objc_clear_super_receiver ();
10163 block
= pop_scope ();
10166 stmt
= pop_stmt_list (stmt
);
10167 stmt
= c_build_bind_expr (loc
, block
, stmt
);
10169 /* If this compound statement is nested immediately inside a statement
10170 expression, then force a BIND_EXPR to be created. Otherwise we'll
10171 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10172 STATEMENT_LISTs merge, and thus we can lose track of what statement
10173 was really last. */
10174 if (building_stmt_list_p ()
10175 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10176 && TREE_CODE (stmt
) != BIND_EXPR
)
10178 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
10179 TREE_SIDE_EFFECTS (stmt
) = 1;
10180 SET_EXPR_LOCATION (stmt
, loc
);
10186 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10187 when the current scope is exited. EH_ONLY is true when this is not
10188 meant to apply to normal control flow transfer. */
10191 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
10193 enum tree_code code
;
10197 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
10198 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
10200 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
10201 list
= push_stmt_list ();
10202 TREE_OPERAND (stmt
, 0) = list
;
10203 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
10206 /* Build a binary-operation expression without default conversions.
10207 CODE is the kind of expression to build.
10208 LOCATION is the operator's location.
10209 This function differs from `build' in several ways:
10210 the data type of the result is computed and recorded in it,
10211 warnings are generated if arg data types are invalid,
10212 special handling for addition and subtraction of pointers is known,
10213 and some optimization is done (operations on narrow ints
10214 are done in the narrower type when that gives the same result).
10215 Constant folding is also done before the result is returned.
10217 Note that the operands will never have enumeral types, or function
10218 or array types, because either they will have the default conversions
10219 performed or they have both just been converted to some other type in which
10220 the arithmetic is to be done. */
10223 build_binary_op (location_t location
, enum tree_code code
,
10224 tree orig_op0
, tree orig_op1
, int convert_p
)
10226 tree type0
, type1
, orig_type0
, orig_type1
;
10228 enum tree_code code0
, code1
;
10230 tree ret
= error_mark_node
;
10231 const char *invalid_op_diag
;
10232 bool op0_int_operands
, op1_int_operands
;
10233 bool int_const
, int_const_or_overflow
, int_operands
;
10235 /* Expression code to give to the expression when it is built.
10236 Normally this is CODE, which is what the caller asked for,
10237 but in some special cases we change it. */
10238 enum tree_code resultcode
= code
;
10240 /* Data type in which the computation is to be performed.
10241 In the simplest cases this is the common type of the arguments. */
10242 tree result_type
= NULL
;
10244 /* When the computation is in excess precision, the type of the
10245 final EXCESS_PRECISION_EXPR. */
10246 tree semantic_result_type
= NULL
;
10248 /* Nonzero means operands have already been type-converted
10249 in whatever way is necessary.
10250 Zero means they need to be converted to RESULT_TYPE. */
10253 /* Nonzero means create the expression with this type, rather than
10255 tree build_type
= 0;
10257 /* Nonzero means after finally constructing the expression
10258 convert it to this type. */
10259 tree final_type
= 0;
10261 /* Nonzero if this is an operation like MIN or MAX which can
10262 safely be computed in short if both args are promoted shorts.
10263 Also implies COMMON.
10264 -1 indicates a bitwise operation; this makes a difference
10265 in the exact conditions for when it is safe to do the operation
10266 in a narrower mode. */
10269 /* Nonzero if this is a comparison operation;
10270 if both args are promoted shorts, compare the original shorts.
10271 Also implies COMMON. */
10272 int short_compare
= 0;
10274 /* Nonzero if this is a right-shift operation, which can be computed on the
10275 original short and then promoted if the operand is a promoted short. */
10276 int short_shift
= 0;
10278 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10281 /* True means types are compatible as far as ObjC is concerned. */
10284 /* True means this is an arithmetic operation that may need excess
10286 bool may_need_excess_precision
;
10288 /* True means this is a boolean operation that converts both its
10289 operands to truth-values. */
10290 bool boolean_op
= false;
10292 /* Remember whether we're doing / or %. */
10293 bool doing_div_or_mod
= false;
10295 /* Remember whether we're doing << or >>. */
10296 bool doing_shift
= false;
10298 /* Tree holding instrumentation expression. */
10299 tree instrument_expr
= NULL
;
10301 if (location
== UNKNOWN_LOCATION
)
10302 location
= input_location
;
10307 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
10308 if (op0_int_operands
)
10309 op0
= remove_c_maybe_const_expr (op0
);
10310 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
10311 if (op1_int_operands
)
10312 op1
= remove_c_maybe_const_expr (op1
);
10313 int_operands
= (op0_int_operands
&& op1_int_operands
);
10316 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
10317 && TREE_CODE (orig_op1
) == INTEGER_CST
);
10318 int_const
= (int_const_or_overflow
10319 && !TREE_OVERFLOW (orig_op0
)
10320 && !TREE_OVERFLOW (orig_op1
));
10323 int_const
= int_const_or_overflow
= false;
10325 /* Do not apply default conversion in mixed vector/scalar expression. */
10327 && !((TREE_CODE (TREE_TYPE (op0
)) == VECTOR_TYPE
)
10328 != (TREE_CODE (TREE_TYPE (op1
)) == VECTOR_TYPE
)))
10330 op0
= default_conversion (op0
);
10331 op1
= default_conversion (op1
);
10334 /* When Cilk Plus is enabled and there are array notations inside op0, then
10335 we check to see if there are builtin array notation functions. If
10336 so, then we take on the type of the array notation inside it. */
10337 if (flag_cilkplus
&& contains_array_notation_expr (op0
))
10338 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10340 orig_type0
= type0
= TREE_TYPE (op0
);
10342 if (flag_cilkplus
&& contains_array_notation_expr (op1
))
10343 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10345 orig_type1
= type1
= TREE_TYPE (op1
);
10347 /* The expression codes of the data types of the arguments tell us
10348 whether the arguments are integers, floating, pointers, etc. */
10349 code0
= TREE_CODE (type0
);
10350 code1
= TREE_CODE (type1
);
10352 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10353 STRIP_TYPE_NOPS (op0
);
10354 STRIP_TYPE_NOPS (op1
);
10356 /* If an error was already reported for one of the arguments,
10357 avoid reporting another error. */
10359 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10360 return error_mark_node
;
10362 if ((invalid_op_diag
10363 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10365 error_at (location
, invalid_op_diag
);
10366 return error_mark_node
;
10374 case TRUNC_DIV_EXPR
:
10375 case CEIL_DIV_EXPR
:
10376 case FLOOR_DIV_EXPR
:
10377 case ROUND_DIV_EXPR
:
10378 case EXACT_DIV_EXPR
:
10379 may_need_excess_precision
= true;
10382 may_need_excess_precision
= false;
10385 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10387 op0
= TREE_OPERAND (op0
, 0);
10388 type0
= TREE_TYPE (op0
);
10390 else if (may_need_excess_precision
10391 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10394 op0
= convert (eptype
, op0
);
10396 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10398 op1
= TREE_OPERAND (op1
, 0);
10399 type1
= TREE_TYPE (op1
);
10401 else if (may_need_excess_precision
10402 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10405 op1
= convert (eptype
, op1
);
10408 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10410 /* In case when one of the operands of the binary operation is
10411 a vector and another is a scalar -- convert scalar to vector. */
10412 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10414 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10417 switch (convert_flag
)
10420 return error_mark_node
;
10423 bool maybe_const
= true;
10425 sc
= c_fully_fold (op0
, false, &maybe_const
);
10426 sc
= save_expr (sc
);
10427 sc
= convert (TREE_TYPE (type1
), sc
);
10428 op0
= build_vector_from_val (type1
, sc
);
10430 op0
= c_wrap_maybe_const (op0
, true);
10431 orig_type0
= type0
= TREE_TYPE (op0
);
10432 code0
= TREE_CODE (type0
);
10436 case stv_secondarg
:
10438 bool maybe_const
= true;
10440 sc
= c_fully_fold (op1
, false, &maybe_const
);
10441 sc
= save_expr (sc
);
10442 sc
= convert (TREE_TYPE (type0
), sc
);
10443 op1
= build_vector_from_val (type0
, sc
);
10445 op1
= c_wrap_maybe_const (op1
, true);
10446 orig_type1
= type1
= TREE_TYPE (op1
);
10447 code1
= TREE_CODE (type1
);
10459 /* Handle the pointer + int case. */
10460 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10462 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10463 goto return_build_binary_op
;
10465 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10467 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10468 goto return_build_binary_op
;
10475 /* Subtraction of two similar pointers.
10476 We must subtract them as integers, then divide by object size. */
10477 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10478 && comp_target_types (location
, type0
, type1
))
10480 ret
= pointer_diff (location
, op0
, op1
);
10481 goto return_build_binary_op
;
10483 /* Handle pointer minus int. Just like pointer plus int. */
10484 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10486 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10487 goto return_build_binary_op
;
10497 case TRUNC_DIV_EXPR
:
10498 case CEIL_DIV_EXPR
:
10499 case FLOOR_DIV_EXPR
:
10500 case ROUND_DIV_EXPR
:
10501 case EXACT_DIV_EXPR
:
10502 doing_div_or_mod
= true;
10503 warn_for_div_by_zero (location
, op1
);
10505 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10506 || code0
== FIXED_POINT_TYPE
10507 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10508 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10509 || code1
== FIXED_POINT_TYPE
10510 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10512 enum tree_code tcode0
= code0
, tcode1
= code1
;
10514 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10515 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10516 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10517 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10519 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10520 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10521 resultcode
= RDIV_EXPR
;
10523 /* Although it would be tempting to shorten always here, that
10524 loses on some targets, since the modulo instruction is
10525 undefined if the quotient can't be represented in the
10526 computation mode. We shorten only if unsigned or if
10527 dividing by something we know != -1. */
10528 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10529 || (TREE_CODE (op1
) == INTEGER_CST
10530 && !integer_all_onesp (op1
)));
10538 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10540 /* Allow vector types which are not floating point types. */
10541 else if (code0
== VECTOR_TYPE
10542 && code1
== VECTOR_TYPE
10543 && !VECTOR_FLOAT_TYPE_P (type0
)
10544 && !VECTOR_FLOAT_TYPE_P (type1
))
10548 case TRUNC_MOD_EXPR
:
10549 case FLOOR_MOD_EXPR
:
10550 doing_div_or_mod
= true;
10551 warn_for_div_by_zero (location
, op1
);
10553 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10554 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10555 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10557 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10559 /* Although it would be tempting to shorten always here, that loses
10560 on some targets, since the modulo instruction is undefined if the
10561 quotient can't be represented in the computation mode. We shorten
10562 only if unsigned or if dividing by something we know != -1. */
10563 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10564 || (TREE_CODE (op1
) == INTEGER_CST
10565 && !integer_all_onesp (op1
)));
10570 case TRUTH_ANDIF_EXPR
:
10571 case TRUTH_ORIF_EXPR
:
10572 case TRUTH_AND_EXPR
:
10573 case TRUTH_OR_EXPR
:
10574 case TRUTH_XOR_EXPR
:
10575 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
10576 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10577 || code0
== FIXED_POINT_TYPE
)
10578 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
10579 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10580 || code1
== FIXED_POINT_TYPE
))
10582 /* Result of these operations is always an int,
10583 but that does not mean the operands should be
10584 converted to ints! */
10585 result_type
= integer_type_node
;
10586 if (op0_int_operands
)
10588 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
10589 op0
= remove_c_maybe_const_expr (op0
);
10592 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
10593 if (op1_int_operands
)
10595 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
10596 op1
= remove_c_maybe_const_expr (op1
);
10599 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
10603 if (code
== TRUTH_ANDIF_EXPR
)
10605 int_const_or_overflow
= (int_operands
10606 && TREE_CODE (orig_op0
) == INTEGER_CST
10607 && (op0
== truthvalue_false_node
10608 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10609 int_const
= (int_const_or_overflow
10610 && !TREE_OVERFLOW (orig_op0
)
10611 && (op0
== truthvalue_false_node
10612 || !TREE_OVERFLOW (orig_op1
)));
10614 else if (code
== TRUTH_ORIF_EXPR
)
10616 int_const_or_overflow
= (int_operands
10617 && TREE_CODE (orig_op0
) == INTEGER_CST
10618 && (op0
== truthvalue_true_node
10619 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10620 int_const
= (int_const_or_overflow
10621 && !TREE_OVERFLOW (orig_op0
)
10622 && (op0
== truthvalue_true_node
10623 || !TREE_OVERFLOW (orig_op1
)));
10627 /* Shift operations: result has same type as first operand;
10628 always convert second operand to int.
10629 Also set SHORT_SHIFT if shifting rightward. */
10632 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10633 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10635 result_type
= type0
;
10638 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10639 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10640 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10641 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10643 result_type
= type0
;
10646 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10647 && code1
== INTEGER_TYPE
)
10649 doing_shift
= true;
10650 if (TREE_CODE (op1
) == INTEGER_CST
)
10652 if (tree_int_cst_sgn (op1
) < 0)
10655 if (c_inhibit_evaluation_warnings
== 0)
10656 warning_at (location
, OPT_Wshift_count_negative
,
10657 "right shift count is negative");
10661 if (!integer_zerop (op1
))
10664 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10667 if (c_inhibit_evaluation_warnings
== 0)
10668 warning_at (location
, OPT_Wshift_count_overflow
,
10669 "right shift count >= width of type");
10674 /* Use the type of the value to be shifted. */
10675 result_type
= type0
;
10676 /* Avoid converting op1 to result_type later. */
10682 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10683 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10685 result_type
= type0
;
10688 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10689 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10690 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10691 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10693 result_type
= type0
;
10696 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10697 && code1
== INTEGER_TYPE
)
10699 doing_shift
= true;
10700 if (TREE_CODE (op0
) == INTEGER_CST
10701 && tree_int_cst_sgn (op0
) < 0)
10703 /* Don't reject a left shift of a negative value in a context
10704 where a constant expression is needed in C90. */
10707 if (c_inhibit_evaluation_warnings
== 0)
10708 warning_at (location
, OPT_Wshift_negative_value
,
10709 "left shift of negative value");
10711 if (TREE_CODE (op1
) == INTEGER_CST
)
10713 if (tree_int_cst_sgn (op1
) < 0)
10716 if (c_inhibit_evaluation_warnings
== 0)
10717 warning_at (location
, OPT_Wshift_count_negative
,
10718 "left shift count is negative");
10721 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10724 if (c_inhibit_evaluation_warnings
== 0)
10725 warning_at (location
, OPT_Wshift_count_overflow
,
10726 "left shift count >= width of type");
10730 /* Use the type of the value to be shifted. */
10731 result_type
= type0
;
10732 /* Avoid converting op1 to result_type later. */
10739 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10742 if (!vector_types_compatible_elements_p (type0
, type1
))
10744 error_at (location
, "comparing vectors with different "
10746 return error_mark_node
;
10749 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10751 error_at (location
, "comparing vectors with different "
10752 "number of elements");
10753 return error_mark_node
;
10756 /* Always construct signed integer vector type. */
10757 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10758 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10759 result_type
= build_opaque_vector_type (intt
,
10760 TYPE_VECTOR_SUBPARTS (type0
));
10764 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
10765 warning_at (location
,
10767 "comparing floating point with == or != is unsafe");
10768 /* Result of comparison is always int,
10769 but don't convert the args to int! */
10770 build_type
= integer_type_node
;
10771 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10772 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
10773 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10774 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
10776 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10778 if (TREE_CODE (op0
) == ADDR_EXPR
10779 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
10781 if (code
== EQ_EXPR
)
10782 warning_at (location
,
10784 "the comparison will always evaluate as %<false%> "
10785 "for the address of %qD will never be NULL",
10786 TREE_OPERAND (op0
, 0));
10788 warning_at (location
,
10790 "the comparison will always evaluate as %<true%> "
10791 "for the address of %qD will never be NULL",
10792 TREE_OPERAND (op0
, 0));
10794 result_type
= type0
;
10796 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10798 if (TREE_CODE (op1
) == ADDR_EXPR
10799 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
10801 if (code
== EQ_EXPR
)
10802 warning_at (location
,
10804 "the comparison will always evaluate as %<false%> "
10805 "for the address of %qD will never be NULL",
10806 TREE_OPERAND (op1
, 0));
10808 warning_at (location
,
10810 "the comparison will always evaluate as %<true%> "
10811 "for the address of %qD will never be NULL",
10812 TREE_OPERAND (op1
, 0));
10814 result_type
= type1
;
10816 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10818 tree tt0
= TREE_TYPE (type0
);
10819 tree tt1
= TREE_TYPE (type1
);
10820 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
10821 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
10822 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
10824 /* Anything compares with void *. void * compares with anything.
10825 Otherwise, the targets must be compatible
10826 and both must be object or both incomplete. */
10827 if (comp_target_types (location
, type0
, type1
))
10828 result_type
= common_pointer_type (type0
, type1
);
10829 else if (!addr_space_superset (as0
, as1
, &as_common
))
10831 error_at (location
, "comparison of pointers to "
10832 "disjoint address spaces");
10833 return error_mark_node
;
10835 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
10837 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
10838 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10839 "comparison of %<void *%> with function pointer");
10841 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
10843 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
10844 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10845 "comparison of %<void *%> with function pointer");
10848 /* Avoid warning about the volatile ObjC EH puts on decls. */
10850 pedwarn (location
, 0,
10851 "comparison of distinct pointer types lacks a cast");
10853 if (result_type
== NULL_TREE
)
10855 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10856 result_type
= build_pointer_type
10857 (build_qualified_type (void_type_node
, qual
));
10860 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10862 result_type
= type0
;
10863 pedwarn (location
, 0, "comparison between pointer and integer");
10865 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10867 result_type
= type1
;
10868 pedwarn (location
, 0, "comparison between pointer and integer");
10870 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
10871 || truth_value_p (TREE_CODE (orig_op0
)))
10872 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
10873 || truth_value_p (TREE_CODE (orig_op1
))))
10874 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
10881 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10884 if (!vector_types_compatible_elements_p (type0
, type1
))
10886 error_at (location
, "comparing vectors with different "
10888 return error_mark_node
;
10891 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10893 error_at (location
, "comparing vectors with different "
10894 "number of elements");
10895 return error_mark_node
;
10898 /* Always construct signed integer vector type. */
10899 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10900 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10901 result_type
= build_opaque_vector_type (intt
,
10902 TYPE_VECTOR_SUBPARTS (type0
));
10906 build_type
= integer_type_node
;
10907 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10908 || code0
== FIXED_POINT_TYPE
)
10909 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10910 || code1
== FIXED_POINT_TYPE
))
10912 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10914 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
10915 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
10916 addr_space_t as_common
;
10918 if (comp_target_types (location
, type0
, type1
))
10920 result_type
= common_pointer_type (type0
, type1
);
10921 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
10922 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
10923 pedwarn (location
, 0,
10924 "comparison of complete and incomplete pointers");
10925 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
10926 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10927 "ordered comparisons of pointers to functions");
10928 else if (null_pointer_constant_p (orig_op0
)
10929 || null_pointer_constant_p (orig_op1
))
10930 warning_at (location
, OPT_Wextra
,
10931 "ordered comparison of pointer with null pointer");
10934 else if (!addr_space_superset (as0
, as1
, &as_common
))
10936 error_at (location
, "comparison of pointers to "
10937 "disjoint address spaces");
10938 return error_mark_node
;
10942 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10943 result_type
= build_pointer_type
10944 (build_qualified_type (void_type_node
, qual
));
10945 pedwarn (location
, 0,
10946 "comparison of distinct pointer types lacks a cast");
10949 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10951 result_type
= type0
;
10953 pedwarn (location
, OPT_Wpedantic
,
10954 "ordered comparison of pointer with integer zero");
10955 else if (extra_warnings
)
10956 warning_at (location
, OPT_Wextra
,
10957 "ordered comparison of pointer with integer zero");
10959 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10961 result_type
= type1
;
10963 pedwarn (location
, OPT_Wpedantic
,
10964 "ordered comparison of pointer with integer zero");
10965 else if (extra_warnings
)
10966 warning_at (location
, OPT_Wextra
,
10967 "ordered comparison of pointer with integer zero");
10969 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10971 result_type
= type0
;
10972 pedwarn (location
, 0, "comparison between pointer and integer");
10974 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10976 result_type
= type1
;
10977 pedwarn (location
, 0, "comparison between pointer and integer");
10979 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
10980 || truth_value_p (TREE_CODE (orig_op0
)))
10981 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
10982 || truth_value_p (TREE_CODE (orig_op1
))))
10983 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
10987 gcc_unreachable ();
10990 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10991 return error_mark_node
;
10993 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10994 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
10995 || !vector_types_compatible_elements_p (type0
, type1
)))
10997 binary_op_error (location
, code
, type0
, type1
);
10998 return error_mark_node
;
11001 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11002 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
11004 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11005 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
11007 bool first_complex
= (code0
== COMPLEX_TYPE
);
11008 bool second_complex
= (code1
== COMPLEX_TYPE
);
11009 int none_complex
= (!first_complex
&& !second_complex
);
11011 if (shorten
|| common
|| short_compare
)
11013 result_type
= c_common_type (type0
, type1
);
11014 do_warn_double_promotion (result_type
, type0
, type1
,
11015 "implicit conversion from %qT to %qT "
11016 "to match other operand of binary "
11019 if (result_type
== error_mark_node
)
11020 return error_mark_node
;
11023 if (first_complex
!= second_complex
11024 && (code
== PLUS_EXPR
11025 || code
== MINUS_EXPR
11026 || code
== MULT_EXPR
11027 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
11028 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
11029 && flag_signed_zeros
)
11031 /* An operation on mixed real/complex operands must be
11032 handled specially, but the language-independent code can
11033 more easily optimize the plain complex arithmetic if
11034 -fno-signed-zeros. */
11035 tree real_type
= TREE_TYPE (result_type
);
11037 if (type0
!= orig_type0
|| type1
!= orig_type1
)
11039 gcc_assert (may_need_excess_precision
&& common
);
11040 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11044 if (TREE_TYPE (op0
) != result_type
)
11045 op0
= convert_and_check (location
, result_type
, op0
);
11046 if (TREE_TYPE (op1
) != real_type
)
11047 op1
= convert_and_check (location
, real_type
, op1
);
11051 if (TREE_TYPE (op0
) != real_type
)
11052 op0
= convert_and_check (location
, real_type
, op0
);
11053 if (TREE_TYPE (op1
) != result_type
)
11054 op1
= convert_and_check (location
, result_type
, op1
);
11056 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11057 return error_mark_node
;
11060 op0
= c_save_expr (op0
);
11061 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
11063 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
11068 case TRUNC_DIV_EXPR
:
11069 op1
= c_save_expr (op1
);
11070 imag
= build2 (resultcode
, real_type
, imag
, op1
);
11071 /* Fall through. */
11074 real
= build2 (resultcode
, real_type
, real
, op1
);
11082 op1
= c_save_expr (op1
);
11083 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
11085 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
11090 op0
= c_save_expr (op0
);
11091 imag
= build2 (resultcode
, real_type
, op0
, imag
);
11092 /* Fall through. */
11094 real
= build2 (resultcode
, real_type
, op0
, real
);
11097 real
= build2 (resultcode
, real_type
, op0
, real
);
11098 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
11104 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
11105 goto return_build_binary_op
;
11108 /* For certain operations (which identify themselves by shorten != 0)
11109 if both args were extended from the same smaller type,
11110 do the arithmetic in that type and then extend.
11112 shorten !=0 and !=1 indicates a bitwise operation.
11113 For them, this optimization is safe only if
11114 both args are zero-extended or both are sign-extended.
11115 Otherwise, we might change the result.
11116 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11117 but calculated in (unsigned short) it would be (unsigned short)-1. */
11119 if (shorten
&& none_complex
)
11121 final_type
= result_type
;
11122 result_type
= shorten_binary_op (result_type
, op0
, op1
,
11126 /* Shifts can be shortened if shifting right. */
11131 tree arg0
= get_narrower (op0
, &unsigned_arg
);
11133 final_type
= result_type
;
11135 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
11136 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
11138 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
11139 && tree_int_cst_sgn (op1
) > 0
11140 /* We can shorten only if the shift count is less than the
11141 number of bits in the smaller type size. */
11142 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
11143 /* We cannot drop an unsigned shift after sign-extension. */
11144 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
11146 /* Do an unsigned shift if the operand was zero-extended. */
11148 = c_common_signed_or_unsigned_type (unsigned_arg
,
11150 /* Convert value-to-be-shifted to that type. */
11151 if (TREE_TYPE (op0
) != result_type
)
11152 op0
= convert (result_type
, op0
);
11157 /* Comparison operations are shortened too but differently.
11158 They identify themselves by setting short_compare = 1. */
11162 /* Don't write &op0, etc., because that would prevent op0
11163 from being kept in a register.
11164 Instead, make copies of the our local variables and
11165 pass the copies by reference, then copy them back afterward. */
11166 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
11167 enum tree_code xresultcode
= resultcode
;
11169 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
11175 goto return_build_binary_op
;
11178 op0
= xop0
, op1
= xop1
;
11180 resultcode
= xresultcode
;
11182 if (c_inhibit_evaluation_warnings
== 0)
11184 bool op0_maybe_const
= true;
11185 bool op1_maybe_const
= true;
11186 tree orig_op0_folded
, orig_op1_folded
;
11188 if (in_late_binary_op
)
11190 orig_op0_folded
= orig_op0
;
11191 orig_op1_folded
= orig_op1
;
11195 /* Fold for the sake of possible warnings, as in
11196 build_conditional_expr. This requires the
11197 "original" values to be folded, not just op0 and
11199 c_inhibit_evaluation_warnings
++;
11200 op0
= c_fully_fold (op0
, require_constant_value
,
11202 op1
= c_fully_fold (op1
, require_constant_value
,
11204 c_inhibit_evaluation_warnings
--;
11205 orig_op0_folded
= c_fully_fold (orig_op0
,
11206 require_constant_value
,
11208 orig_op1_folded
= c_fully_fold (orig_op1
,
11209 require_constant_value
,
11213 if (warn_sign_compare
)
11214 warn_for_sign_compare (location
, orig_op0_folded
,
11215 orig_op1_folded
, op0
, op1
,
11216 result_type
, resultcode
);
11217 if (!in_late_binary_op
&& !int_operands
)
11219 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
11220 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
11221 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
11222 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
11228 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11229 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11230 Then the expression will be built.
11231 It will be given type FINAL_TYPE if that is nonzero;
11232 otherwise, it will be given type RESULT_TYPE. */
11236 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
11237 return error_mark_node
;
11240 if (build_type
== NULL_TREE
)
11242 build_type
= result_type
;
11243 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
11246 gcc_assert (may_need_excess_precision
&& common
);
11247 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11253 op0
= ep_convert_and_check (location
, result_type
, op0
,
11254 semantic_result_type
);
11255 op1
= ep_convert_and_check (location
, result_type
, op1
,
11256 semantic_result_type
);
11258 /* This can happen if one operand has a vector type, and the other
11259 has a different type. */
11260 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11261 return error_mark_node
;
11264 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
11265 | SANITIZE_FLOAT_DIVIDE
))
11266 && do_ubsan_in_current_function ()
11267 && (doing_div_or_mod
|| doing_shift
))
11269 /* OP0 and/or OP1 might have side-effects. */
11270 op0
= c_save_expr (op0
);
11271 op1
= c_save_expr (op1
);
11272 op0
= c_fully_fold (op0
, false, NULL
);
11273 op1
= c_fully_fold (op1
, false, NULL
);
11274 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
11275 | SANITIZE_FLOAT_DIVIDE
)))
11276 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
11277 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
11278 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
11281 /* Treat expressions in initializers specially as they can't trap. */
11282 if (int_const_or_overflow
)
11283 ret
= (require_constant_value
11284 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
11286 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
11288 ret
= build2 (resultcode
, build_type
, op0
, op1
);
11289 if (final_type
!= 0)
11290 ret
= convert (final_type
, ret
);
11292 return_build_binary_op
:
11293 gcc_assert (ret
!= error_mark_node
);
11294 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
11295 ret
= (int_operands
11296 ? note_integer_operands (ret
)
11297 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
11298 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
11299 && !in_late_binary_op
)
11300 ret
= note_integer_operands (ret
);
11301 if (semantic_result_type
)
11302 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
11303 protected_set_expr_location (ret
, location
);
11305 if (instrument_expr
!= NULL
)
11306 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
11307 instrument_expr
, ret
);
11313 /* Convert EXPR to be a truth-value, validating its type for this
11314 purpose. LOCATION is the source location for the expression. */
11317 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
11319 bool int_const
, int_operands
;
11321 switch (TREE_CODE (TREE_TYPE (expr
)))
11324 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
11325 return error_mark_node
;
11328 error_at (location
, "used struct type value where scalar is required");
11329 return error_mark_node
;
11332 error_at (location
, "used union type value where scalar is required");
11333 return error_mark_node
;
11336 error_at (location
, "void value not ignored as it ought to be");
11337 return error_mark_node
;
11339 case FUNCTION_TYPE
:
11340 gcc_unreachable ();
11343 error_at (location
, "used vector type where scalar is required");
11344 return error_mark_node
;
11350 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11351 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11352 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11354 expr
= remove_c_maybe_const_expr (expr
);
11355 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11356 convert (TREE_TYPE (expr
), integer_zero_node
));
11357 expr
= note_integer_operands (expr
);
11360 /* ??? Should we also give an error for vectors rather than leaving
11361 those to give errors later? */
11362 expr
= c_common_truthvalue_conversion (location
, expr
);
11364 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11366 if (TREE_OVERFLOW (expr
))
11369 return note_integer_operands (expr
);
11371 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11372 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11377 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11381 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11383 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11385 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11386 /* Executing a compound literal inside a function reinitializes
11388 if (!TREE_STATIC (decl
))
11396 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11397 statement. LOC is the location of the OACC_PARALLEL. */
11400 c_finish_oacc_parallel (location_t loc
, tree clauses
, tree block
)
11404 block
= c_end_compound_stmt (loc
, block
, true);
11406 stmt
= make_node (OACC_PARALLEL
);
11407 TREE_TYPE (stmt
) = void_type_node
;
11408 OACC_PARALLEL_CLAUSES (stmt
) = clauses
;
11409 OACC_PARALLEL_BODY (stmt
) = block
;
11410 SET_EXPR_LOCATION (stmt
, loc
);
11412 return add_stmt (stmt
);
11415 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11416 statement. LOC is the location of the OACC_KERNELS. */
11419 c_finish_oacc_kernels (location_t loc
, tree clauses
, tree block
)
11423 block
= c_end_compound_stmt (loc
, block
, true);
11425 stmt
= make_node (OACC_KERNELS
);
11426 TREE_TYPE (stmt
) = void_type_node
;
11427 OACC_KERNELS_CLAUSES (stmt
) = clauses
;
11428 OACC_KERNELS_BODY (stmt
) = block
;
11429 SET_EXPR_LOCATION (stmt
, loc
);
11431 return add_stmt (stmt
);
11434 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11435 statement. LOC is the location of the OACC_DATA. */
11438 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
11442 block
= c_end_compound_stmt (loc
, block
, true);
11444 stmt
= make_node (OACC_DATA
);
11445 TREE_TYPE (stmt
) = void_type_node
;
11446 OACC_DATA_CLAUSES (stmt
) = clauses
;
11447 OACC_DATA_BODY (stmt
) = block
;
11448 SET_EXPR_LOCATION (stmt
, loc
);
11450 return add_stmt (stmt
);
11453 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11456 c_begin_omp_parallel (void)
11460 keep_next_level ();
11461 block
= c_begin_compound_stmt (true);
11466 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11467 statement. LOC is the location of the OMP_PARALLEL. */
11470 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11474 block
= c_end_compound_stmt (loc
, block
, true);
11476 stmt
= make_node (OMP_PARALLEL
);
11477 TREE_TYPE (stmt
) = void_type_node
;
11478 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11479 OMP_PARALLEL_BODY (stmt
) = block
;
11480 SET_EXPR_LOCATION (stmt
, loc
);
11482 return add_stmt (stmt
);
11485 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11488 c_begin_omp_task (void)
11492 keep_next_level ();
11493 block
= c_begin_compound_stmt (true);
11498 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11499 statement. LOC is the location of the #pragma. */
11502 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11506 block
= c_end_compound_stmt (loc
, block
, true);
11508 stmt
= make_node (OMP_TASK
);
11509 TREE_TYPE (stmt
) = void_type_node
;
11510 OMP_TASK_CLAUSES (stmt
) = clauses
;
11511 OMP_TASK_BODY (stmt
) = block
;
11512 SET_EXPR_LOCATION (stmt
, loc
);
11514 return add_stmt (stmt
);
11517 /* Generate GOMP_cancel call for #pragma omp cancel. */
11520 c_finish_omp_cancel (location_t loc
, tree clauses
)
11522 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
11524 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11526 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11528 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11530 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11534 error_at (loc
, "%<#pragma omp cancel must specify one of "
11535 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11539 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
11540 if (ifc
!= NULL_TREE
)
11542 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
11543 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
11544 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
11545 build_zero_cst (type
));
11548 ifc
= boolean_true_node
;
11549 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
11550 build_int_cst (integer_type_node
, mask
),
11555 /* Generate GOMP_cancellation_point call for
11556 #pragma omp cancellation point. */
11559 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
11561 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
11563 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11565 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11567 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11569 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11573 error_at (loc
, "%<#pragma omp cancellation point must specify one of "
11574 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11578 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
11579 build_int_cst (integer_type_node
, mask
));
11583 /* Helper function for handle_omp_array_sections. Called recursively
11584 to handle multiple array-section-subscripts. C is the clause,
11585 T current expression (initially OMP_CLAUSE_DECL), which is either
11586 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11587 expression if specified, TREE_VALUE length expression if specified,
11588 TREE_CHAIN is what it has been specified after, or some decl.
11589 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11590 set to true if any of the array-section-subscript could have length
11591 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11592 first array-section-subscript which is known not to have length
11594 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11595 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11596 all are or may have length of 1, array-section-subscript [:2] is the
11597 first one knonwn not to have length 1. For array-section-subscript
11598 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11599 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11600 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11601 case though, as some lengths could be zero. */
11604 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
11605 bool &maybe_zero_len
, unsigned int &first_non_one
)
11607 tree ret
, low_bound
, length
, type
;
11608 if (TREE_CODE (t
) != TREE_LIST
)
11610 if (error_operand_p (t
))
11611 return error_mark_node
;
11612 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11615 error_at (OMP_CLAUSE_LOCATION (c
),
11616 "%qD is not a variable in %qs clause", t
,
11617 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11619 error_at (OMP_CLAUSE_LOCATION (c
),
11620 "%qE is not a variable in %qs clause", t
,
11621 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11622 return error_mark_node
;
11624 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11625 && TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11627 error_at (OMP_CLAUSE_LOCATION (c
),
11628 "%qD is threadprivate variable in %qs clause", t
,
11629 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11630 return error_mark_node
;
11635 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
11636 maybe_zero_len
, first_non_one
);
11637 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
11640 type
= TREE_TYPE (ret
);
11641 low_bound
= TREE_PURPOSE (t
);
11642 length
= TREE_VALUE (t
);
11644 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
11645 return error_mark_node
;
11647 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
11649 error_at (OMP_CLAUSE_LOCATION (c
),
11650 "low bound %qE of array section does not have integral type",
11652 return error_mark_node
;
11654 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
11656 error_at (OMP_CLAUSE_LOCATION (c
),
11657 "length %qE of array section does not have integral type",
11659 return error_mark_node
;
11662 && TREE_CODE (low_bound
) == INTEGER_CST
11663 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11664 > TYPE_PRECISION (sizetype
))
11665 low_bound
= fold_convert (sizetype
, low_bound
);
11667 && TREE_CODE (length
) == INTEGER_CST
11668 && TYPE_PRECISION (TREE_TYPE (length
))
11669 > TYPE_PRECISION (sizetype
))
11670 length
= fold_convert (sizetype
, length
);
11671 if (low_bound
== NULL_TREE
)
11672 low_bound
= integer_zero_node
;
11674 if (length
!= NULL_TREE
)
11676 if (!integer_nonzerop (length
))
11677 maybe_zero_len
= true;
11678 if (first_non_one
== types
.length ()
11679 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
11682 if (TREE_CODE (type
) == ARRAY_TYPE
)
11684 if (length
== NULL_TREE
11685 && (TYPE_DOMAIN (type
) == NULL_TREE
11686 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
11688 error_at (OMP_CLAUSE_LOCATION (c
),
11689 "for unknown bound array type length expression must "
11691 return error_mark_node
;
11693 if (TREE_CODE (low_bound
) == INTEGER_CST
11694 && tree_int_cst_sgn (low_bound
) == -1)
11696 error_at (OMP_CLAUSE_LOCATION (c
),
11697 "negative low bound in array section in %qs clause",
11698 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11699 return error_mark_node
;
11701 if (length
!= NULL_TREE
11702 && TREE_CODE (length
) == INTEGER_CST
11703 && tree_int_cst_sgn (length
) == -1)
11705 error_at (OMP_CLAUSE_LOCATION (c
),
11706 "negative length in array section in %qs clause",
11707 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11708 return error_mark_node
;
11710 if (TYPE_DOMAIN (type
)
11711 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
11712 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
11715 tree size
= size_binop (PLUS_EXPR
,
11716 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11718 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11720 if (tree_int_cst_lt (size
, low_bound
))
11722 error_at (OMP_CLAUSE_LOCATION (c
),
11723 "low bound %qE above array section size "
11724 "in %qs clause", low_bound
,
11725 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11726 return error_mark_node
;
11728 if (tree_int_cst_equal (size
, low_bound
))
11729 maybe_zero_len
= true;
11730 else if (length
== NULL_TREE
11731 && first_non_one
== types
.length ()
11732 && tree_int_cst_equal
11733 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11737 else if (length
== NULL_TREE
)
11739 maybe_zero_len
= true;
11740 if (first_non_one
== types
.length ())
11743 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
11745 if (tree_int_cst_lt (size
, length
))
11747 error_at (OMP_CLAUSE_LOCATION (c
),
11748 "length %qE above array section size "
11749 "in %qs clause", length
,
11750 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11751 return error_mark_node
;
11753 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11756 = size_binop (PLUS_EXPR
,
11757 fold_convert (sizetype
, low_bound
),
11758 fold_convert (sizetype
, length
));
11759 if (TREE_CODE (lbpluslen
) == INTEGER_CST
11760 && tree_int_cst_lt (size
, lbpluslen
))
11762 error_at (OMP_CLAUSE_LOCATION (c
),
11763 "high bound %qE above array section size "
11764 "in %qs clause", lbpluslen
,
11765 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11766 return error_mark_node
;
11771 else if (length
== NULL_TREE
)
11773 maybe_zero_len
= true;
11774 if (first_non_one
== types
.length ())
11778 /* For [lb:] we will need to evaluate lb more than once. */
11779 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11781 tree lb
= c_save_expr (low_bound
);
11782 if (lb
!= low_bound
)
11784 TREE_PURPOSE (t
) = lb
;
11789 else if (TREE_CODE (type
) == POINTER_TYPE
)
11791 if (length
== NULL_TREE
)
11793 error_at (OMP_CLAUSE_LOCATION (c
),
11794 "for pointer type length expression must be specified");
11795 return error_mark_node
;
11797 /* If there is a pointer type anywhere but in the very first
11798 array-section-subscript, the array section can't be contiguous. */
11799 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11800 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
11802 error_at (OMP_CLAUSE_LOCATION (c
),
11803 "array section is not contiguous in %qs clause",
11804 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11805 return error_mark_node
;
11810 error_at (OMP_CLAUSE_LOCATION (c
),
11811 "%qE does not have pointer or array type", ret
);
11812 return error_mark_node
;
11814 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11815 types
.safe_push (TREE_TYPE (ret
));
11816 /* We will need to evaluate lb more than once. */
11817 tree lb
= c_save_expr (low_bound
);
11818 if (lb
!= low_bound
)
11820 TREE_PURPOSE (t
) = lb
;
11823 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
11827 /* Handle array sections for clause C. */
11830 handle_omp_array_sections (tree c
)
11832 bool maybe_zero_len
= false;
11833 unsigned int first_non_one
= 0;
11834 vec
<tree
> types
= vNULL
;
11835 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
11836 maybe_zero_len
, first_non_one
);
11837 if (first
== error_mark_node
)
11842 if (first
== NULL_TREE
)
11847 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
11849 tree t
= OMP_CLAUSE_DECL (c
);
11850 tree tem
= NULL_TREE
;
11852 /* Need to evaluate side effects in the length expressions
11854 while (TREE_CODE (t
) == TREE_LIST
)
11856 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
11858 if (tem
== NULL_TREE
)
11859 tem
= TREE_VALUE (t
);
11861 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
11862 TREE_VALUE (t
), tem
);
11864 t
= TREE_CHAIN (t
);
11867 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
11868 first
= c_fully_fold (first
, false, NULL
);
11869 OMP_CLAUSE_DECL (c
) = first
;
11873 unsigned int num
= types
.length (), i
;
11874 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
11875 tree condition
= NULL_TREE
;
11877 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
11878 maybe_zero_len
= true;
11880 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
11881 t
= TREE_CHAIN (t
))
11883 tree low_bound
= TREE_PURPOSE (t
);
11884 tree length
= TREE_VALUE (t
);
11888 && TREE_CODE (low_bound
) == INTEGER_CST
11889 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11890 > TYPE_PRECISION (sizetype
))
11891 low_bound
= fold_convert (sizetype
, low_bound
);
11893 && TREE_CODE (length
) == INTEGER_CST
11894 && TYPE_PRECISION (TREE_TYPE (length
))
11895 > TYPE_PRECISION (sizetype
))
11896 length
= fold_convert (sizetype
, length
);
11897 if (low_bound
== NULL_TREE
)
11898 low_bound
= integer_zero_node
;
11899 if (!maybe_zero_len
&& i
> first_non_one
)
11901 if (integer_nonzerop (low_bound
))
11902 goto do_warn_noncontiguous
;
11903 if (length
!= NULL_TREE
11904 && TREE_CODE (length
) == INTEGER_CST
11905 && TYPE_DOMAIN (types
[i
])
11906 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
11907 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
11911 size
= size_binop (PLUS_EXPR
,
11912 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11914 if (!tree_int_cst_equal (length
, size
))
11916 do_warn_noncontiguous
:
11917 error_at (OMP_CLAUSE_LOCATION (c
),
11918 "array section is not contiguous in %qs "
11920 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11925 if (length
!= NULL_TREE
11926 && TREE_SIDE_EFFECTS (length
))
11928 if (side_effects
== NULL_TREE
)
11929 side_effects
= length
;
11931 side_effects
= build2 (COMPOUND_EXPR
,
11932 TREE_TYPE (side_effects
),
11933 length
, side_effects
);
11940 if (i
> first_non_one
&& length
&& integer_nonzerop (length
))
11943 l
= fold_convert (sizetype
, length
);
11946 l
= size_binop (PLUS_EXPR
,
11947 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11949 l
= size_binop (MINUS_EXPR
, l
,
11950 fold_convert (sizetype
, low_bound
));
11952 if (i
> first_non_one
)
11954 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
11956 if (condition
== NULL_TREE
)
11959 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
11962 else if (size
== NULL_TREE
)
11964 size
= size_in_bytes (TREE_TYPE (types
[i
]));
11965 size
= size_binop (MULT_EXPR
, size
, l
);
11967 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
11968 size
, size_zero_node
);
11971 size
= size_binop (MULT_EXPR
, size
, l
);
11976 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
11977 first
= c_fully_fold (first
, false, NULL
);
11978 OMP_CLAUSE_DECL (c
) = first
;
11980 size
= c_fully_fold (size
, false, NULL
);
11981 OMP_CLAUSE_SIZE (c
) = size
;
11982 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
11984 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
11985 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
11986 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_POINTER
);
11987 if (!c_mark_addressable (t
))
11989 OMP_CLAUSE_DECL (c2
) = t
;
11990 t
= build_fold_addr_expr (first
);
11991 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
11992 tree ptr
= OMP_CLAUSE_DECL (c2
);
11993 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
11994 ptr
= build_fold_addr_expr (ptr
);
11995 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11996 ptrdiff_type_node
, t
,
11997 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
11998 ptrdiff_type_node
, ptr
));
11999 t
= c_fully_fold (t
, false, NULL
);
12000 OMP_CLAUSE_SIZE (c2
) = t
;
12001 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
12002 OMP_CLAUSE_CHAIN (c
) = c2
;
12007 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12008 an inline call. But, remap
12009 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12010 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12013 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
12014 tree decl
, tree placeholder
)
12017 hash_map
<tree
, tree
> decl_map
;
12019 decl_map
.put (omp_decl1
, placeholder
);
12020 decl_map
.put (omp_decl2
, decl
);
12021 memset (&id
, 0, sizeof (id
));
12022 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
12023 id
.dst_fn
= current_function_decl
;
12024 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
12025 id
.decl_map
= &decl_map
;
12027 id
.copy_decl
= copy_decl_no_change
;
12028 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
12029 id
.transform_new_cfg
= true;
12030 id
.transform_return_to_modify
= false;
12031 id
.transform_lang_insert_block
= NULL
;
12033 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
12037 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12038 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12041 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
12043 if (*tp
== (tree
) data
)
12048 /* For all elements of CLAUSES, validate them against their constraints.
12049 Remove any elements from the list that are invalid. */
12052 c_finish_omp_clauses (tree clauses
)
12054 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
12055 bitmap_head aligned_head
;
12057 bool branch_seen
= false;
12058 bool copyprivate_seen
= false;
12059 tree
*nowait_clause
= NULL
;
12061 bitmap_obstack_initialize (NULL
);
12062 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
12063 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
12064 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
12065 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
12067 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
12069 bool remove
= false;
12070 bool need_complete
= false;
12071 bool need_implicitly_determined
= false;
12073 switch (OMP_CLAUSE_CODE (c
))
12075 case OMP_CLAUSE_SHARED
:
12076 need_implicitly_determined
= true;
12077 goto check_dup_generic
;
12079 case OMP_CLAUSE_PRIVATE
:
12080 need_complete
= true;
12081 need_implicitly_determined
= true;
12082 goto check_dup_generic
;
12084 case OMP_CLAUSE_REDUCTION
:
12085 need_implicitly_determined
= true;
12086 t
= OMP_CLAUSE_DECL (c
);
12087 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
12088 && (FLOAT_TYPE_P (TREE_TYPE (t
))
12089 || TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
))
12091 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
12092 const char *r_name
= NULL
;
12101 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
12105 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
12117 case TRUTH_ANDIF_EXPR
:
12118 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
12121 case TRUTH_ORIF_EXPR
:
12122 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
12126 gcc_unreachable ();
12130 error_at (OMP_CLAUSE_LOCATION (c
),
12131 "%qE has invalid type for %<reduction(%s)%>",
12137 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
12139 error_at (OMP_CLAUSE_LOCATION (c
),
12140 "user defined reduction not found for %qD", t
);
12144 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
12146 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
12147 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
12148 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
12149 VAR_DECL
, NULL_TREE
, type
);
12150 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
12151 DECL_ARTIFICIAL (placeholder
) = 1;
12152 DECL_IGNORED_P (placeholder
) = 1;
12153 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
12154 c_mark_addressable (placeholder
);
12155 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
12156 c_mark_addressable (OMP_CLAUSE_DECL (c
));
12157 OMP_CLAUSE_REDUCTION_MERGE (c
)
12158 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
12159 TREE_VEC_ELT (list
, 0),
12160 TREE_VEC_ELT (list
, 1),
12161 OMP_CLAUSE_DECL (c
), placeholder
);
12162 OMP_CLAUSE_REDUCTION_MERGE (c
)
12163 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12164 void_type_node
, NULL_TREE
,
12165 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
12166 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
12167 if (TREE_VEC_LENGTH (list
) == 6)
12169 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
12170 c_mark_addressable (OMP_CLAUSE_DECL (c
));
12171 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
12172 c_mark_addressable (placeholder
);
12173 tree init
= TREE_VEC_ELT (list
, 5);
12174 if (init
== error_mark_node
)
12175 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
12176 OMP_CLAUSE_REDUCTION_INIT (c
)
12177 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
12178 TREE_VEC_ELT (list
, 3),
12179 OMP_CLAUSE_DECL (c
), placeholder
);
12180 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
12181 OMP_CLAUSE_REDUCTION_INIT (c
)
12182 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
,
12183 OMP_CLAUSE_REDUCTION_INIT (c
));
12184 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
12185 c_find_omp_placeholder_r
,
12186 placeholder
, NULL
))
12187 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
12192 if (AGGREGATE_TYPE_P (TREE_TYPE (t
)))
12193 init
= build_constructor (TREE_TYPE (t
), NULL
);
12195 init
= fold_convert (TREE_TYPE (t
), integer_zero_node
);
12196 OMP_CLAUSE_REDUCTION_INIT (c
)
12197 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
, init
);
12199 OMP_CLAUSE_REDUCTION_INIT (c
)
12200 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12201 void_type_node
, NULL_TREE
,
12202 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
12203 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
12205 goto check_dup_generic
;
12207 case OMP_CLAUSE_COPYPRIVATE
:
12208 copyprivate_seen
= true;
12211 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
12212 "%<nowait%> clause must not be used together "
12213 "with %<copyprivate%>");
12214 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
12215 nowait_clause
= NULL
;
12217 goto check_dup_generic
;
12219 case OMP_CLAUSE_COPYIN
:
12220 t
= OMP_CLAUSE_DECL (c
);
12221 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
12223 error_at (OMP_CLAUSE_LOCATION (c
),
12224 "%qE must be %<threadprivate%> for %<copyin%>", t
);
12228 goto check_dup_generic
;
12230 case OMP_CLAUSE_LINEAR
:
12231 t
= OMP_CLAUSE_DECL (c
);
12232 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
12233 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
12235 error_at (OMP_CLAUSE_LOCATION (c
),
12236 "linear clause applied to non-integral non-pointer "
12237 "variable with type %qT", TREE_TYPE (t
));
12241 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
12243 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
12244 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
12245 OMP_CLAUSE_DECL (c
), s
);
12246 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12247 sizetype
, s
, OMP_CLAUSE_DECL (c
));
12248 if (s
== error_mark_node
)
12250 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
12253 OMP_CLAUSE_LINEAR_STEP (c
)
12254 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
12255 goto check_dup_generic
;
12258 t
= OMP_CLAUSE_DECL (c
);
12259 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12261 error_at (OMP_CLAUSE_LOCATION (c
),
12262 "%qE is not a variable in clause %qs", t
,
12263 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12266 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12267 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
12268 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12270 error_at (OMP_CLAUSE_LOCATION (c
),
12271 "%qE appears more than once in data clauses", t
);
12275 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12278 case OMP_CLAUSE_FIRSTPRIVATE
:
12279 t
= OMP_CLAUSE_DECL (c
);
12280 need_complete
= true;
12281 need_implicitly_determined
= true;
12282 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12284 error_at (OMP_CLAUSE_LOCATION (c
),
12285 "%qE is not a variable in clause %<firstprivate%>", t
);
12288 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12289 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
12291 error_at (OMP_CLAUSE_LOCATION (c
),
12292 "%qE appears more than once in data clauses", t
);
12296 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
12299 case OMP_CLAUSE_LASTPRIVATE
:
12300 t
= OMP_CLAUSE_DECL (c
);
12301 need_complete
= true;
12302 need_implicitly_determined
= true;
12303 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12305 error_at (OMP_CLAUSE_LOCATION (c
),
12306 "%qE is not a variable in clause %<lastprivate%>", t
);
12309 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12310 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12312 error_at (OMP_CLAUSE_LOCATION (c
),
12313 "%qE appears more than once in data clauses", t
);
12317 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
12320 case OMP_CLAUSE_ALIGNED
:
12321 t
= OMP_CLAUSE_DECL (c
);
12322 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12324 error_at (OMP_CLAUSE_LOCATION (c
),
12325 "%qE is not a variable in %<aligned%> clause", t
);
12328 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
12329 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
12331 error_at (OMP_CLAUSE_LOCATION (c
),
12332 "%qE in %<aligned%> clause is neither a pointer nor "
12336 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
12338 error_at (OMP_CLAUSE_LOCATION (c
),
12339 "%qE appears more than once in %<aligned%> clauses",
12344 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
12347 case OMP_CLAUSE_DEPEND
:
12348 t
= OMP_CLAUSE_DECL (c
);
12349 if (TREE_CODE (t
) == TREE_LIST
)
12351 if (handle_omp_array_sections (c
))
12355 if (t
== error_mark_node
)
12357 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12359 error_at (OMP_CLAUSE_LOCATION (c
),
12360 "%qE is not a variable in %<depend%> clause", t
);
12363 else if (!c_mark_addressable (t
))
12367 case OMP_CLAUSE_MAP
:
12368 case OMP_CLAUSE_TO
:
12369 case OMP_CLAUSE_FROM
:
12370 case OMP_CLAUSE__CACHE_
:
12371 t
= OMP_CLAUSE_DECL (c
);
12372 if (TREE_CODE (t
) == TREE_LIST
)
12374 if (handle_omp_array_sections (c
))
12378 t
= OMP_CLAUSE_DECL (c
);
12379 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12381 error_at (OMP_CLAUSE_LOCATION (c
),
12382 "array section does not have mappable type "
12384 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12390 if (t
== error_mark_node
)
12392 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12394 error_at (OMP_CLAUSE_LOCATION (c
),
12395 "%qE is not a variable in %qs clause", t
,
12396 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12399 else if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12401 error_at (OMP_CLAUSE_LOCATION (c
),
12402 "%qD is threadprivate variable in %qs clause", t
,
12403 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12406 else if (!c_mark_addressable (t
))
12408 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12409 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
12410 || (OMP_CLAUSE_MAP_KIND (c
)
12411 == GOMP_MAP_FORCE_DEVICEPTR
)))
12412 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12414 error_at (OMP_CLAUSE_LOCATION (c
),
12415 "%qD does not have a mappable type in %qs clause", t
,
12416 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12419 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
12421 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
12422 error ("%qD appears more than once in motion clauses", t
);
12424 error ("%qD appears more than once in map clauses", t
);
12428 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12431 case OMP_CLAUSE_UNIFORM
:
12432 t
= OMP_CLAUSE_DECL (c
);
12433 if (TREE_CODE (t
) != PARM_DECL
)
12436 error_at (OMP_CLAUSE_LOCATION (c
),
12437 "%qD is not an argument in %<uniform%> clause", t
);
12439 error_at (OMP_CLAUSE_LOCATION (c
),
12440 "%qE is not an argument in %<uniform%> clause", t
);
12444 goto check_dup_generic
;
12446 case OMP_CLAUSE_NOWAIT
:
12447 if (copyprivate_seen
)
12449 error_at (OMP_CLAUSE_LOCATION (c
),
12450 "%<nowait%> clause must not be used together "
12451 "with %<copyprivate%>");
12455 nowait_clause
= pc
;
12456 pc
= &OMP_CLAUSE_CHAIN (c
);
12459 case OMP_CLAUSE_IF
:
12460 case OMP_CLAUSE_NUM_THREADS
:
12461 case OMP_CLAUSE_NUM_TEAMS
:
12462 case OMP_CLAUSE_THREAD_LIMIT
:
12463 case OMP_CLAUSE_SCHEDULE
:
12464 case OMP_CLAUSE_ORDERED
:
12465 case OMP_CLAUSE_DEFAULT
:
12466 case OMP_CLAUSE_UNTIED
:
12467 case OMP_CLAUSE_COLLAPSE
:
12468 case OMP_CLAUSE_FINAL
:
12469 case OMP_CLAUSE_MERGEABLE
:
12470 case OMP_CLAUSE_SAFELEN
:
12471 case OMP_CLAUSE_SIMDLEN
:
12472 case OMP_CLAUSE_DEVICE
:
12473 case OMP_CLAUSE_DIST_SCHEDULE
:
12474 case OMP_CLAUSE_PARALLEL
:
12475 case OMP_CLAUSE_FOR
:
12476 case OMP_CLAUSE_SECTIONS
:
12477 case OMP_CLAUSE_TASKGROUP
:
12478 case OMP_CLAUSE_PROC_BIND
:
12479 case OMP_CLAUSE__CILK_FOR_COUNT_
:
12480 case OMP_CLAUSE_NUM_GANGS
:
12481 case OMP_CLAUSE_NUM_WORKERS
:
12482 case OMP_CLAUSE_VECTOR_LENGTH
:
12483 case OMP_CLAUSE_ASYNC
:
12484 case OMP_CLAUSE_WAIT
:
12485 case OMP_CLAUSE_AUTO
:
12486 case OMP_CLAUSE_SEQ
:
12487 case OMP_CLAUSE_GANG
:
12488 case OMP_CLAUSE_WORKER
:
12489 case OMP_CLAUSE_VECTOR
:
12490 pc
= &OMP_CLAUSE_CHAIN (c
);
12493 case OMP_CLAUSE_INBRANCH
:
12494 case OMP_CLAUSE_NOTINBRANCH
:
12497 error_at (OMP_CLAUSE_LOCATION (c
),
12498 "%<inbranch%> clause is incompatible with "
12499 "%<notinbranch%>");
12503 branch_seen
= true;
12504 pc
= &OMP_CLAUSE_CHAIN (c
);
12508 gcc_unreachable ();
12513 t
= OMP_CLAUSE_DECL (c
);
12517 t
= require_complete_type (t
);
12518 if (t
== error_mark_node
)
12522 if (need_implicitly_determined
)
12524 const char *share_name
= NULL
;
12526 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12527 share_name
= "threadprivate";
12528 else switch (c_omp_predetermined_sharing (t
))
12530 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
12532 case OMP_CLAUSE_DEFAULT_SHARED
:
12533 /* const vars may be specified in firstprivate clause. */
12534 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12535 && TREE_READONLY (t
))
12537 share_name
= "shared";
12539 case OMP_CLAUSE_DEFAULT_PRIVATE
:
12540 share_name
= "private";
12543 gcc_unreachable ();
12547 error_at (OMP_CLAUSE_LOCATION (c
),
12548 "%qE is predetermined %qs for %qs",
12550 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12557 *pc
= OMP_CLAUSE_CHAIN (c
);
12559 pc
= &OMP_CLAUSE_CHAIN (c
);
12562 bitmap_obstack_release (NULL
);
12566 /* Create a transaction node. */
12569 c_finish_transaction (location_t loc
, tree block
, int flags
)
12571 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
12572 if (flags
& TM_STMT_ATTR_OUTER
)
12573 TRANSACTION_EXPR_OUTER (stmt
) = 1;
12574 if (flags
& TM_STMT_ATTR_RELAXED
)
12575 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
12576 return add_stmt (stmt
);
12579 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12580 down to the element type of an array. */
12583 c_build_qualified_type (tree type
, int type_quals
)
12585 if (type
== error_mark_node
)
12588 if (TREE_CODE (type
) == ARRAY_TYPE
)
12591 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
12594 /* See if we already have an identically qualified type. */
12595 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12597 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
12598 && TYPE_NAME (t
) == TYPE_NAME (type
)
12599 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
12600 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
12601 TYPE_ATTRIBUTES (type
)))
12606 tree domain
= TYPE_DOMAIN (type
);
12608 t
= build_variant_type_copy (type
);
12609 TREE_TYPE (t
) = element_type
;
12611 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
12612 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
12613 SET_TYPE_STRUCTURAL_EQUALITY (t
);
12614 else if (TYPE_CANONICAL (element_type
) != element_type
12615 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
12617 tree unqualified_canon
12618 = build_array_type (TYPE_CANONICAL (element_type
),
12619 domain
? TYPE_CANONICAL (domain
)
12622 = c_build_qualified_type (unqualified_canon
, type_quals
);
12625 TYPE_CANONICAL (t
) = t
;
12630 /* A restrict-qualified pointer type must be a pointer to object or
12631 incomplete type. Note that the use of POINTER_TYPE_P also allows
12632 REFERENCE_TYPEs, which is appropriate for C++. */
12633 if ((type_quals
& TYPE_QUAL_RESTRICT
)
12634 && (!POINTER_TYPE_P (type
)
12635 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
12637 error ("invalid use of %<restrict%>");
12638 type_quals
&= ~TYPE_QUAL_RESTRICT
;
12641 return build_qualified_type (type
, type_quals
);
12644 /* Build a VA_ARG_EXPR for the C parser. */
12647 c_build_va_arg (location_t loc
, tree expr
, tree type
)
12649 if (error_operand_p (type
))
12650 return error_mark_node
;
12651 else if (!COMPLETE_TYPE_P (type
))
12653 error_at (loc
, "second argument to %<va_arg%> is of incomplete "
12655 return error_mark_node
;
12657 else if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
12658 warning_at (loc
, OPT_Wc___compat
,
12659 "C++ requires promoted type, not enum type, in %<va_arg%>");
12660 return build_va_arg (loc
, expr
, type
);
12663 /* Return truthvalue of whether T1 is the same tree structure as T2.
12664 Return 1 if they are the same. Return 0 if they are different. */
12667 c_tree_equal (tree t1
, tree t2
)
12669 enum tree_code code1
, code2
;
12676 for (code1
= TREE_CODE (t1
);
12677 CONVERT_EXPR_CODE_P (code1
)
12678 || code1
== NON_LVALUE_EXPR
;
12679 code1
= TREE_CODE (t1
))
12680 t1
= TREE_OPERAND (t1
, 0);
12681 for (code2
= TREE_CODE (t2
);
12682 CONVERT_EXPR_CODE_P (code2
)
12683 || code2
== NON_LVALUE_EXPR
;
12684 code2
= TREE_CODE (t2
))
12685 t2
= TREE_OPERAND (t2
, 0);
12687 /* They might have become equal now. */
12691 if (code1
!= code2
)
12697 return wi::eq_p (t1
, t2
);
12700 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1
), TREE_REAL_CST (t2
));
12703 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
12704 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
12705 TREE_STRING_LENGTH (t1
));
12708 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
12709 TREE_FIXED_CST (t2
));
12712 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
12713 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
12716 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
12719 /* We need to do this when determining whether or not two
12720 non-type pointer to member function template arguments
12722 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
12723 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
12728 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
12730 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
12731 if (!c_tree_equal (field
, elt2
->index
)
12732 || !c_tree_equal (value
, elt2
->value
))
12739 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
12741 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
12743 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
12746 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12751 call_expr_arg_iterator iter1
, iter2
;
12752 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
12754 for (arg1
= first_call_expr_arg (t1
, &iter1
),
12755 arg2
= first_call_expr_arg (t2
, &iter2
);
12757 arg1
= next_call_expr_arg (&iter1
),
12758 arg2
= next_call_expr_arg (&iter2
))
12759 if (!c_tree_equal (arg1
, arg2
))
12768 tree o1
= TREE_OPERAND (t1
, 0);
12769 tree o2
= TREE_OPERAND (t2
, 0);
12771 /* Special case: if either target is an unallocated VAR_DECL,
12772 it means that it's going to be unified with whatever the
12773 TARGET_EXPR is really supposed to initialize, so treat it
12774 as being equivalent to anything. */
12775 if (TREE_CODE (o1
) == VAR_DECL
&& DECL_NAME (o1
) == NULL_TREE
12776 && !DECL_RTL_SET_P (o1
))
12778 else if (TREE_CODE (o2
) == VAR_DECL
&& DECL_NAME (o2
) == NULL_TREE
12779 && !DECL_RTL_SET_P (o2
))
12781 else if (!c_tree_equal (o1
, o2
))
12784 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
12787 case COMPONENT_REF
:
12788 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
12790 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12796 case FUNCTION_DECL
:
12797 case IDENTIFIER_NODE
:
12804 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
12806 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
12807 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
12808 TREE_VEC_ELT (t2
, ix
)))
12817 switch (TREE_CODE_CLASS (code1
))
12821 case tcc_comparison
:
12822 case tcc_expression
:
12824 case tcc_reference
:
12825 case tcc_statement
:
12827 int i
, n
= TREE_OPERAND_LENGTH (t1
);
12831 case PREINCREMENT_EXPR
:
12832 case PREDECREMENT_EXPR
:
12833 case POSTINCREMENT_EXPR
:
12834 case POSTDECREMENT_EXPR
:
12844 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
12845 && n
!= TREE_OPERAND_LENGTH (t2
))
12848 for (i
= 0; i
< n
; ++i
)
12849 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
12856 return comptypes (t1
, t2
);
12858 gcc_unreachable ();
12860 /* We can get here with --disable-checking. */
12864 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12865 spawn-helper and BODY is the newly created body for FNDECL. */
12868 cilk_install_body_with_frame_cleanup (tree fndecl
, tree body
, void *w
)
12870 tree list
= alloc_stmt_list ();
12871 tree frame
= make_cilk_frame (fndecl
);
12872 tree dtor
= create_cilk_function_exit (frame
, false, true);
12873 add_local_decl (cfun
, frame
);
12875 DECL_SAVED_TREE (fndecl
) = list
;
12876 tree frame_ptr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (frame
)),
12878 tree body_list
= cilk_install_body_pedigree_operations (frame_ptr
);
12879 gcc_assert (TREE_CODE (body_list
) == STATEMENT_LIST
);
12881 tree detach_expr
= build_call_expr (cilk_detach_fndecl
, 1, frame_ptr
);
12882 append_to_statement_list (detach_expr
, &body_list
);
12884 cilk_outline (fndecl
, &body
, (struct wrapper_data
*) w
);
12885 body
= fold_build_cleanup_point_expr (void_type_node
, body
);
12887 append_to_statement_list (body
, &body_list
);
12888 append_to_statement_list (build_stmt (EXPR_LOCATION (body
), TRY_FINALLY_EXPR
,
12889 body_list
, dtor
), &list
);