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"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "trans-mem.h"
39 #include "langhooks.h"
45 #include "tree-iterator.h"
48 #include "hard-reg-set.h"
51 #include "gimple-expr.h"
53 #include "tree-inline.h"
55 #include "c-family/c-objc.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-ubsan.h"
59 #include "gomp-constants.h"
61 /* Possible cases of implicit bad conversions. Used to select
62 diagnostic messages in convert_for_assignment. */
70 /* The level of nesting inside "__alignof__". */
73 /* The level of nesting inside "sizeof". */
76 /* The level of nesting inside "typeof". */
79 /* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81 tree c_last_sizeof_arg
;
83 /* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85 static int found_missing_braces
;
87 static int require_constant_value
;
88 static int require_constant_elements
;
90 static bool null_pointer_constant_p (const_tree
);
91 static tree
qualify_type (tree
, tree
);
92 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
94 static int comp_target_types (location_t
, tree
, tree
);
95 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
97 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
98 static tree
lookup_field (tree
, tree
);
99 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
100 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
102 static tree
pointer_diff (location_t
, tree
, tree
);
103 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
104 enum impl_conv
, bool, tree
, tree
, int);
105 static tree
valid_compound_expr_initializer (tree
, tree
);
106 static void push_string (const char *);
107 static void push_member_name (tree
);
108 static int spelling_length (void);
109 static char *print_spelling (char *);
110 static void warning_init (location_t
, int, const char *);
111 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
112 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, int,
113 bool, struct obstack
*);
114 static void output_pending_init_elements (int, struct obstack
*);
115 static int set_designator (location_t
, int, struct obstack
*);
116 static void push_range_stack (tree
, struct obstack
*);
117 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
119 static void set_nonincremental_init (struct obstack
*);
120 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
121 static tree
find_init_member (tree
, struct obstack
*);
122 static void readonly_warning (tree
, enum lvalue_use
);
123 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
124 static void record_maybe_used_decl (tree
);
125 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
127 /* Return true if EXP is a null pointer constant, false otherwise. */
130 null_pointer_constant_p (const_tree expr
)
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type
= TREE_TYPE (expr
);
135 return (TREE_CODE (expr
) == INTEGER_CST
136 && !TREE_OVERFLOW (expr
)
137 && integer_zerop (expr
)
138 && (INTEGRAL_TYPE_P (type
)
139 || (TREE_CODE (type
) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type
))
141 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
144 /* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
150 note_integer_operands (tree expr
)
153 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
155 ret
= copy_node (expr
);
156 TREE_OVERFLOW (ret
) = 1;
160 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
166 /* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
172 remove_c_maybe_const_expr (tree expr
)
174 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
175 return C_MAYBE_CONST_EXPR_EXPR (expr
);
180 \f/* This is a cache to hold if two types are compatible or not. */
182 struct tagged_tu_seen_cache
{
183 const struct tagged_tu_seen_cache
* next
;
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
191 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
192 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
194 /* Do `exp = require_complete_type (exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.) */
198 require_complete_type (tree value
)
200 tree type
= TREE_TYPE (value
);
202 if (error_operand_p (value
))
203 return error_mark_node
;
205 /* First, detect a valid value with a complete type. */
206 if (COMPLETE_TYPE_P (type
))
209 c_incomplete_type_error (value
, type
);
210 return error_mark_node
;
213 /* Print an error message for invalid use of an incomplete type.
214 VALUE is the expression that was used (or 0 if that isn't known)
215 and TYPE is the type that was invalid. */
218 c_incomplete_type_error (const_tree value
, const_tree type
)
220 /* Avoid duplicate error message. */
221 if (TREE_CODE (type
) == ERROR_MARK
)
224 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
225 || TREE_CODE (value
) == PARM_DECL
))
226 error ("%qD has an incomplete type %qT", value
, type
);
230 /* We must print an error message. Be clever about what it says. */
232 switch (TREE_CODE (type
))
240 error ("invalid use of void expression");
244 if (TYPE_DOMAIN (type
))
246 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
248 error ("invalid use of flexible array member");
251 type
= TREE_TYPE (type
);
254 error ("invalid use of array with unspecified bounds");
261 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
262 error ("invalid use of undefined type %qT", type
);
264 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
265 error ("invalid use of incomplete typedef %qT", type
);
269 /* Given a type, apply default promotions wrt unnamed function
270 arguments and return the new type. */
273 c_type_promotes_to (tree type
)
275 tree ret
= NULL_TREE
;
277 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
278 ret
= double_type_node
;
279 else if (c_promoting_integer_type_p (type
))
281 /* Preserve unsignedness if not really getting any wider. */
282 if (TYPE_UNSIGNED (type
)
283 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
284 ret
= unsigned_type_node
;
286 ret
= integer_type_node
;
289 if (ret
!= NULL_TREE
)
290 return (TYPE_ATOMIC (type
)
291 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
297 /* Return true if between two named address spaces, whether there is a superset
298 named address space that encompasses both address spaces. If there is a
299 superset, return which address space is the superset. */
302 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
309 else if (targetm
.addr_space
.subset_p (as1
, as2
))
314 else if (targetm
.addr_space
.subset_p (as2
, as1
))
323 /* Return a variant of TYPE which has all the type qualifiers of LIKE
324 as well as those of TYPE. */
327 qualify_type (tree type
, tree like
)
329 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
330 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
331 addr_space_t as_common
;
333 /* If the two named address spaces are different, determine the common
334 superset address space. If there isn't one, raise an error. */
335 if (!addr_space_superset (as_type
, as_like
, &as_common
))
338 error ("%qT and %qT are in disjoint named address spaces",
342 return c_build_qualified_type (type
,
343 TYPE_QUALS_NO_ADDR_SPACE (type
)
344 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
345 | ENCODE_QUAL_ADDR_SPACE (as_common
));
348 /* Return true iff the given tree T is a variable length array. */
351 c_vla_type_p (const_tree t
)
353 if (TREE_CODE (t
) == ARRAY_TYPE
354 && C_TYPE_VARIABLE_SIZE (t
))
359 /* Return the composite type of two compatible types.
361 We assume that comptypes has already been done and returned
362 nonzero; if that isn't so, this may crash. In particular, we
363 assume that qualifiers match. */
366 composite_type (tree t1
, tree t2
)
368 enum tree_code code1
;
369 enum tree_code code2
;
372 /* Save time if the two types are the same. */
374 if (t1
== t2
) return t1
;
376 /* If one type is nonsense, use the other. */
377 if (t1
== error_mark_node
)
379 if (t2
== error_mark_node
)
382 code1
= TREE_CODE (t1
);
383 code2
= TREE_CODE (t2
);
385 /* Merge the attributes. */
386 attributes
= targetm
.merge_type_attributes (t1
, t2
);
388 /* If one is an enumerated type and the other is the compatible
389 integer type, the composite type might be either of the two
390 (DR#013 question 3). For consistency, use the enumerated type as
391 the composite type. */
393 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
395 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
398 gcc_assert (code1
== code2
);
403 /* For two pointers, do this recursively on the target type. */
405 tree pointed_to_1
= TREE_TYPE (t1
);
406 tree pointed_to_2
= TREE_TYPE (t2
);
407 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
408 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
409 t1
= build_type_attribute_variant (t1
, attributes
);
410 return qualify_type (t1
, t2
);
415 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
418 tree d1
= TYPE_DOMAIN (t1
);
419 tree d2
= TYPE_DOMAIN (t2
);
420 bool d1_variable
, d2_variable
;
421 bool d1_zero
, d2_zero
;
422 bool t1_complete
, t2_complete
;
424 /* We should not have any type quals on arrays at all. */
425 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
426 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
428 t1_complete
= COMPLETE_TYPE_P (t1
);
429 t2_complete
= COMPLETE_TYPE_P (t2
);
431 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
432 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
434 d1_variable
= (!d1_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
437 d2_variable
= (!d2_zero
438 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
439 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
440 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
441 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
443 /* Save space: see if the result is identical to one of the args. */
444 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
445 && (d2_variable
|| d2_zero
|| !d1_variable
))
446 return build_type_attribute_variant (t1
, attributes
);
447 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
448 && (d1_variable
|| d1_zero
|| !d2_variable
))
449 return build_type_attribute_variant (t2
, attributes
);
451 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
452 return build_type_attribute_variant (t1
, attributes
);
453 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
454 return build_type_attribute_variant (t2
, attributes
);
456 /* Merge the element types, and have a size if either arg has
457 one. We may have qualifiers on the element types. To set
458 up TYPE_MAIN_VARIANT correctly, we need to form the
459 composite of the unqualified types and add the qualifiers
461 quals
= TYPE_QUALS (strip_array_types (elt
));
462 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
463 t1
= build_array_type (unqual_elt
,
464 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
470 /* Ensure a composite type involving a zero-length array type
471 is a zero-length type not an incomplete type. */
472 if (d1_zero
&& d2_zero
473 && (t1_complete
|| t2_complete
)
474 && !COMPLETE_TYPE_P (t1
))
476 TYPE_SIZE (t1
) = bitsize_zero_node
;
477 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
479 t1
= c_build_qualified_type (t1
, quals
);
480 return build_type_attribute_variant (t1
, attributes
);
486 if (attributes
!= NULL
)
488 /* Try harder not to create a new aggregate type. */
489 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
491 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
494 return build_type_attribute_variant (t1
, attributes
);
497 /* Function types: prefer the one that specified arg types.
498 If both do, merge the arg types. Also merge the return types. */
500 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
501 tree p1
= TYPE_ARG_TYPES (t1
);
502 tree p2
= TYPE_ARG_TYPES (t2
);
507 /* Save space: see if the result is identical to one of the args. */
508 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
509 return build_type_attribute_variant (t1
, attributes
);
510 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
511 return build_type_attribute_variant (t2
, attributes
);
513 /* Simple way if one arg fails to specify argument types. */
514 if (TYPE_ARG_TYPES (t1
) == 0)
516 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
517 t1
= build_type_attribute_variant (t1
, attributes
);
518 return qualify_type (t1
, t2
);
520 if (TYPE_ARG_TYPES (t2
) == 0)
522 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
523 t1
= build_type_attribute_variant (t1
, attributes
);
524 return qualify_type (t1
, t2
);
527 /* If both args specify argument types, we must merge the two
528 lists, argument by argument. */
530 len
= list_length (p1
);
533 for (i
= 0; i
< len
; i
++)
534 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
539 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
541 /* A null type means arg type is not specified.
542 Take whatever the other function type has. */
543 if (TREE_VALUE (p1
) == 0)
545 TREE_VALUE (n
) = TREE_VALUE (p2
);
548 if (TREE_VALUE (p2
) == 0)
550 TREE_VALUE (n
) = TREE_VALUE (p1
);
554 /* Given wait (union {union wait *u; int *i} *)
555 and wait (union wait *),
556 prefer union wait * as type of parm. */
557 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
558 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
561 tree mv2
= TREE_VALUE (p2
);
562 if (mv2
&& mv2
!= error_mark_node
563 && TREE_CODE (mv2
) != ARRAY_TYPE
)
564 mv2
= TYPE_MAIN_VARIANT (mv2
);
565 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
566 memb
; memb
= DECL_CHAIN (memb
))
568 tree mv3
= TREE_TYPE (memb
);
569 if (mv3
&& mv3
!= error_mark_node
570 && TREE_CODE (mv3
) != ARRAY_TYPE
)
571 mv3
= TYPE_MAIN_VARIANT (mv3
);
572 if (comptypes (mv3
, mv2
))
574 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
576 pedwarn (input_location
, OPT_Wpedantic
,
577 "function types not truly compatible in ISO C");
582 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
583 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
586 tree mv1
= TREE_VALUE (p1
);
587 if (mv1
&& mv1
!= error_mark_node
588 && TREE_CODE (mv1
) != ARRAY_TYPE
)
589 mv1
= TYPE_MAIN_VARIANT (mv1
);
590 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
591 memb
; memb
= DECL_CHAIN (memb
))
593 tree mv3
= TREE_TYPE (memb
);
594 if (mv3
&& mv3
!= error_mark_node
595 && TREE_CODE (mv3
) != ARRAY_TYPE
)
596 mv3
= TYPE_MAIN_VARIANT (mv3
);
597 if (comptypes (mv3
, mv1
))
599 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
601 pedwarn (input_location
, OPT_Wpedantic
,
602 "function types not truly compatible in ISO C");
607 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
611 t1
= build_function_type (valtype
, newargs
);
612 t1
= qualify_type (t1
, t2
);
613 /* ... falls through ... */
617 return build_type_attribute_variant (t1
, attributes
);
622 /* Return the type of a conditional expression between pointers to
623 possibly differently qualified versions of compatible types.
625 We assume that comp_target_types has already been done and returned
626 nonzero; if that isn't so, this may crash. */
629 common_pointer_type (tree t1
, tree t2
)
632 tree pointed_to_1
, mv1
;
633 tree pointed_to_2
, mv2
;
635 unsigned target_quals
;
636 addr_space_t as1
, as2
, as_common
;
639 /* Save time if the two types are the same. */
641 if (t1
== t2
) return t1
;
643 /* If one type is nonsense, use the other. */
644 if (t1
== error_mark_node
)
646 if (t2
== error_mark_node
)
649 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
650 && TREE_CODE (t2
) == POINTER_TYPE
);
652 /* Merge the attributes. */
653 attributes
= targetm
.merge_type_attributes (t1
, t2
);
655 /* Find the composite type of the target types, and combine the
656 qualifiers of the two types' targets. Do not lose qualifiers on
657 array element types by taking the TYPE_MAIN_VARIANT. */
658 mv1
= pointed_to_1
= TREE_TYPE (t1
);
659 mv2
= pointed_to_2
= TREE_TYPE (t2
);
660 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
661 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
662 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
663 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
664 target
= composite_type (mv1
, mv2
);
666 /* Strip array types to get correct qualifier for pointers to arrays */
667 quals1
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1
));
668 quals2
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2
));
670 /* For function types do not merge const qualifiers, but drop them
671 if used inconsistently. The middle-end uses these to mark const
672 and noreturn functions. */
673 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
674 target_quals
= (quals1
& quals2
);
676 target_quals
= (quals1
| quals2
);
678 /* If the two named address spaces are different, determine the common
679 superset address space. This is guaranteed to exist due to the
680 assumption that comp_target_type returned non-zero. */
681 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
682 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
683 if (!addr_space_superset (as1
, as2
, &as_common
))
686 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
688 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
689 return build_type_attribute_variant (t1
, attributes
);
692 /* Return the common type for two arithmetic types under the usual
693 arithmetic conversions. The default conversions have already been
694 applied, and enumerated types converted to their compatible integer
695 types. The resulting type is unqualified and has no attributes.
697 This is the type for the result of most arithmetic operations
698 if the operands have the given two types. */
701 c_common_type (tree t1
, tree t2
)
703 enum tree_code code1
;
704 enum tree_code code2
;
706 /* If one type is nonsense, use the other. */
707 if (t1
== error_mark_node
)
709 if (t2
== error_mark_node
)
712 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
713 t1
= TYPE_MAIN_VARIANT (t1
);
715 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
716 t2
= TYPE_MAIN_VARIANT (t2
);
718 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
719 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
721 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
722 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
724 /* Save time if the two types are the same. */
726 if (t1
== t2
) return t1
;
728 code1
= TREE_CODE (t1
);
729 code2
= TREE_CODE (t2
);
731 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
732 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
733 || code1
== INTEGER_TYPE
);
734 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
735 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
736 || code2
== INTEGER_TYPE
);
738 /* When one operand is a decimal float type, the other operand cannot be
739 a generic float type or a complex type. We also disallow vector types
741 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
742 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
744 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
746 error ("can%'t mix operands of decimal float and vector types");
747 return error_mark_node
;
749 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
751 error ("can%'t mix operands of decimal float and complex types");
752 return error_mark_node
;
754 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
756 error ("can%'t mix operands of decimal float and other float types");
757 return error_mark_node
;
761 /* If one type is a vector type, return that type. (How the usual
762 arithmetic conversions apply to the vector types extension is not
763 precisely specified.) */
764 if (code1
== VECTOR_TYPE
)
767 if (code2
== VECTOR_TYPE
)
770 /* If one type is complex, form the common type of the non-complex
771 components, then make that complex. Use T1 or T2 if it is the
773 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
775 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
776 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
777 tree subtype
= c_common_type (subtype1
, subtype2
);
779 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
781 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
784 return build_complex_type (subtype
);
787 /* If only one is real, use it as the result. */
789 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
792 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
795 /* If both are real and either are decimal floating point types, use
796 the decimal floating point type with the greater precision. */
798 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
800 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
801 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
802 return dfloat128_type_node
;
803 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
804 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
805 return dfloat64_type_node
;
806 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
807 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
808 return dfloat32_type_node
;
811 /* Deal with fixed-point types. */
812 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
814 unsigned int unsignedp
= 0, satp
= 0;
816 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
821 /* If one input type is saturating, the result type is saturating. */
822 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
825 /* If both fixed-point types are unsigned, the result type is unsigned.
826 When mixing fixed-point and integer types, follow the sign of the
828 Otherwise, the result type is signed. */
829 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
830 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
831 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t1
))
833 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t2
)))
837 /* The result type is signed. */
840 /* If the input type is unsigned, we need to convert to the
842 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
844 enum mode_class mclass
= (enum mode_class
) 0;
845 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
847 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
851 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
853 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
855 enum mode_class mclass
= (enum mode_class
) 0;
856 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
858 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
862 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
866 if (code1
== FIXED_POINT_TYPE
)
868 fbit1
= GET_MODE_FBIT (m1
);
869 ibit1
= GET_MODE_IBIT (m1
);
874 /* Signed integers need to subtract one sign bit. */
875 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
878 if (code2
== FIXED_POINT_TYPE
)
880 fbit2
= GET_MODE_FBIT (m2
);
881 ibit2
= GET_MODE_IBIT (m2
);
886 /* Signed integers need to subtract one sign bit. */
887 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
890 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
891 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
892 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
896 /* Both real or both integers; use the one with greater precision. */
898 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
900 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
903 /* Same precision. Prefer long longs to longs to ints when the
904 same precision, following the C99 rules on integer type rank
905 (which are equivalent to the C90 rules for C90 types). */
907 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
908 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
909 return long_long_unsigned_type_node
;
911 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
912 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
914 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
915 return long_long_unsigned_type_node
;
917 return long_long_integer_type_node
;
920 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
921 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
922 return long_unsigned_type_node
;
924 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
925 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
927 /* But preserve unsignedness from the other type,
928 since long cannot hold all the values of an unsigned int. */
929 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
930 return long_unsigned_type_node
;
932 return long_integer_type_node
;
935 /* Likewise, prefer long double to double even if same size. */
936 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
937 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
938 return long_double_type_node
;
940 /* Likewise, prefer double to float even if same size.
941 We got a couple of embedded targets with 32 bit doubles, and the
942 pdp11 might have 64 bit floats. */
943 if (TYPE_MAIN_VARIANT (t1
) == double_type_node
944 || TYPE_MAIN_VARIANT (t2
) == double_type_node
)
945 return double_type_node
;
947 /* Otherwise prefer the unsigned one. */
949 if (TYPE_UNSIGNED (t1
))
955 /* Wrapper around c_common_type that is used by c-common.c and other
956 front end optimizations that remove promotions. ENUMERAL_TYPEs
957 are allowed here and are converted to their compatible integer types.
958 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
959 preferably a non-Boolean type as the common type. */
961 common_type (tree t1
, tree t2
)
963 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
964 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
965 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
966 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
968 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
969 if (TREE_CODE (t1
) == BOOLEAN_TYPE
970 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
971 return boolean_type_node
;
973 /* If either type is BOOLEAN_TYPE, then return the other. */
974 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
976 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
979 return c_common_type (t1
, t2
);
982 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
983 or various other operations. Return 2 if they are compatible
984 but a warning may be needed if you use them together. */
987 comptypes (tree type1
, tree type2
)
989 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
992 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
998 /* Like comptypes, but if it returns non-zero because enum and int are
999 compatible, it sets *ENUM_AND_INT_P to true. */
1002 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1004 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1007 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1008 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1013 /* Like comptypes, but if it returns nonzero for different types, it
1014 sets *DIFFERENT_TYPES_P to true. */
1017 comptypes_check_different_types (tree type1
, tree type2
,
1018 bool *different_types_p
)
1020 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1023 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1024 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1029 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1030 or various other operations. Return 2 if they are compatible
1031 but a warning may be needed if you use them together. If
1032 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1033 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1034 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1035 NULL, and the types are compatible but different enough not to be
1036 permitted in C11 typedef redeclarations, then this sets
1037 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1038 false, but may or may not be set if the types are incompatible.
1039 This differs from comptypes, in that we don't free the seen
1043 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1044 bool *different_types_p
)
1046 const_tree t1
= type1
;
1047 const_tree t2
= type2
;
1050 /* Suppress errors caused by previously reported errors. */
1052 if (t1
== t2
|| !t1
|| !t2
1053 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1056 /* Enumerated types are compatible with integer types, but this is
1057 not transitive: two enumerated types in the same translation unit
1058 are compatible with each other only if they are the same type. */
1060 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1062 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1063 if (TREE_CODE (t2
) != VOID_TYPE
)
1065 if (enum_and_int_p
!= NULL
)
1066 *enum_and_int_p
= true;
1067 if (different_types_p
!= NULL
)
1068 *different_types_p
= true;
1071 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1073 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1074 if (TREE_CODE (t1
) != VOID_TYPE
)
1076 if (enum_and_int_p
!= NULL
)
1077 *enum_and_int_p
= true;
1078 if (different_types_p
!= NULL
)
1079 *different_types_p
= true;
1086 /* Different classes of types can't be compatible. */
1088 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1091 /* Qualifiers must match. C99 6.7.3p9 */
1093 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1096 /* Allow for two different type nodes which have essentially the same
1097 definition. Note that we already checked for equality of the type
1098 qualifiers (just above). */
1100 if (TREE_CODE (t1
) != ARRAY_TYPE
1101 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1104 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1105 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1108 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1111 switch (TREE_CODE (t1
))
1114 /* Do not remove mode or aliasing information. */
1115 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1116 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1118 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1119 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1120 enum_and_int_p
, different_types_p
));
1124 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1130 tree d1
= TYPE_DOMAIN (t1
);
1131 tree d2
= TYPE_DOMAIN (t2
);
1132 bool d1_variable
, d2_variable
;
1133 bool d1_zero
, d2_zero
;
1136 /* Target types must match incl. qualifiers. */
1137 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1138 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1140 different_types_p
)))
1143 if (different_types_p
!= NULL
1144 && (d1
== 0) != (d2
== 0))
1145 *different_types_p
= true;
1146 /* Sizes must match unless one is missing or variable. */
1147 if (d1
== 0 || d2
== 0 || d1
== d2
)
1150 d1_zero
= !TYPE_MAX_VALUE (d1
);
1151 d2_zero
= !TYPE_MAX_VALUE (d2
);
1153 d1_variable
= (!d1_zero
1154 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1155 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1156 d2_variable
= (!d2_zero
1157 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1158 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1159 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1160 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1162 if (different_types_p
!= NULL
1163 && d1_variable
!= d2_variable
)
1164 *different_types_p
= true;
1165 if (d1_variable
|| d2_variable
)
1167 if (d1_zero
&& d2_zero
)
1169 if (d1_zero
|| d2_zero
1170 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1171 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1180 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1182 tree a1
= TYPE_ATTRIBUTES (t1
);
1183 tree a2
= TYPE_ATTRIBUTES (t2
);
1185 if (! attribute_list_contained (a1
, a2
)
1186 && ! attribute_list_contained (a2
, a1
))
1190 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1192 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1198 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1199 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1200 enum_and_int_p
, different_types_p
));
1206 return attrval
== 2 && val
== 1 ? 2 : val
;
1209 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1210 their qualifiers, except for named address spaces. If the pointers point to
1211 different named addresses, then we must determine if one address space is a
1212 subset of the other. */
1215 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1219 tree mvl
= TREE_TYPE (ttl
);
1220 tree mvr
= TREE_TYPE (ttr
);
1221 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1222 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1223 addr_space_t as_common
;
1224 bool enum_and_int_p
;
1226 /* Fail if pointers point to incompatible address spaces. */
1227 if (!addr_space_superset (asl
, asr
, &as_common
))
1230 /* For pedantic record result of comptypes on arrays before losing
1231 qualifiers on the element type below. */
1234 if (TREE_CODE (mvl
) == ARRAY_TYPE
1235 && TREE_CODE (mvr
) == ARRAY_TYPE
)
1236 val_ped
= comptypes (mvl
, mvr
);
1238 /* Qualifiers on element types of array types that are
1239 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1241 mvl
= (TYPE_ATOMIC (strip_array_types (mvl
))
1242 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1243 : TYPE_MAIN_VARIANT (mvl
));
1245 mvr
= (TYPE_ATOMIC (strip_array_types (mvr
))
1246 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1247 : TYPE_MAIN_VARIANT (mvr
));
1249 enum_and_int_p
= false;
1250 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1252 if (val
== 1 && val_ped
!= 1)
1253 pedwarn (location
, OPT_Wpedantic
, "pointers to arrays with different qualifiers "
1254 "are incompatible in ISO C");
1257 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1259 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1260 warning_at (location
, OPT_Wc___compat
,
1261 "pointer target types incompatible in C++");
1266 /* Subroutines of `comptypes'. */
1268 /* Determine whether two trees derive from the same translation unit.
1269 If the CONTEXT chain ends in a null, that tree's context is still
1270 being parsed, so if two trees have context chains ending in null,
1271 they're in the same translation unit. */
1273 same_translation_unit_p (const_tree t1
, const_tree t2
)
1275 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1276 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1278 case tcc_declaration
:
1279 t1
= DECL_CONTEXT (t1
); break;
1281 t1
= TYPE_CONTEXT (t1
); break;
1282 case tcc_exceptional
:
1283 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1284 default: gcc_unreachable ();
1287 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1288 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1290 case tcc_declaration
:
1291 t2
= DECL_CONTEXT (t2
); break;
1293 t2
= TYPE_CONTEXT (t2
); break;
1294 case tcc_exceptional
:
1295 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1296 default: gcc_unreachable ();
1302 /* Allocate the seen two types, assuming that they are compatible. */
1304 static struct tagged_tu_seen_cache
*
1305 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1307 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1308 tu
->next
= tagged_tu_seen_base
;
1312 tagged_tu_seen_base
= tu
;
1314 /* The C standard says that two structures in different translation
1315 units are compatible with each other only if the types of their
1316 fields are compatible (among other things). We assume that they
1317 are compatible until proven otherwise when building the cache.
1318 An example where this can occur is:
1323 If we are comparing this against a similar struct in another TU,
1324 and did not assume they were compatible, we end up with an infinite
1330 /* Free the seen types until we get to TU_TIL. */
1333 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1335 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1336 while (tu
!= tu_til
)
1338 const struct tagged_tu_seen_cache
*const tu1
1339 = (const struct tagged_tu_seen_cache
*) tu
;
1341 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1343 tagged_tu_seen_base
= tu_til
;
1346 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1347 compatible. If the two types are not the same (which has been
1348 checked earlier), this can only happen when multiple translation
1349 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1350 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1351 comptypes_internal. */
1354 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1355 bool *enum_and_int_p
, bool *different_types_p
)
1358 bool needs_warning
= false;
1360 /* We have to verify that the tags of the types are the same. This
1361 is harder than it looks because this may be a typedef, so we have
1362 to go look at the original type. It may even be a typedef of a
1364 In the case of compiler-created builtin structs the TYPE_DECL
1365 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1366 while (TYPE_NAME (t1
)
1367 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1368 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1369 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1371 while (TYPE_NAME (t2
)
1372 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1373 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1374 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1376 /* C90 didn't have the requirement that the two tags be the same. */
1377 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1380 /* C90 didn't say what happened if one or both of the types were
1381 incomplete; we choose to follow C99 rules here, which is that they
1383 if (TYPE_SIZE (t1
) == NULL
1384 || TYPE_SIZE (t2
) == NULL
)
1388 const struct tagged_tu_seen_cache
* tts_i
;
1389 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1390 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1394 switch (TREE_CODE (t1
))
1398 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1399 /* Speed up the case where the type values are in the same order. */
1400 tree tv1
= TYPE_VALUES (t1
);
1401 tree tv2
= TYPE_VALUES (t2
);
1408 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1410 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1412 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1419 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1423 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1429 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1435 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1437 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1439 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1450 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1451 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1457 /* Speed up the common case where the fields are in the same order. */
1458 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1459 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1463 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1465 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1466 enum_and_int_p
, different_types_p
);
1468 if (result
!= 1 && !DECL_NAME (s1
))
1476 needs_warning
= true;
1478 if (TREE_CODE (s1
) == FIELD_DECL
1479 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1480 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1488 tu
->val
= needs_warning
? 2 : 1;
1492 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1496 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1497 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1501 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1505 if (result
!= 1 && !DECL_NAME (s1
))
1513 needs_warning
= true;
1515 if (TREE_CODE (s1
) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1517 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1529 tu
->val
= needs_warning
? 2 : 10;
1535 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1537 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1539 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1542 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1543 || DECL_NAME (s1
) != DECL_NAME (s2
))
1545 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1546 enum_and_int_p
, different_types_p
);
1550 needs_warning
= true;
1552 if (TREE_CODE (s1
) == FIELD_DECL
1553 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1554 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1560 tu
->val
= needs_warning
? 2 : 1;
1569 /* Return 1 if two function types F1 and F2 are compatible.
1570 If either type specifies no argument types,
1571 the other must specify a fixed number of self-promoting arg types.
1572 Otherwise, if one type specifies only the number of arguments,
1573 the other must specify that number of self-promoting arg types.
1574 Otherwise, the argument types must match.
1575 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1578 function_types_compatible_p (const_tree f1
, const_tree f2
,
1579 bool *enum_and_int_p
, bool *different_types_p
)
1582 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1587 ret1
= TREE_TYPE (f1
);
1588 ret2
= TREE_TYPE (f2
);
1590 /* 'volatile' qualifiers on a function's return type used to mean
1591 the function is noreturn. */
1592 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1593 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1594 if (TYPE_VOLATILE (ret1
))
1595 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1596 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1597 if (TYPE_VOLATILE (ret2
))
1598 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1599 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1600 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1604 args1
= TYPE_ARG_TYPES (f1
);
1605 args2
= TYPE_ARG_TYPES (f2
);
1607 if (different_types_p
!= NULL
1608 && (args1
== 0) != (args2
== 0))
1609 *different_types_p
= true;
1611 /* An unspecified parmlist matches any specified parmlist
1612 whose argument types don't need default promotions. */
1616 if (!self_promoting_args_p (args2
))
1618 /* If one of these types comes from a non-prototype fn definition,
1619 compare that with the other type's arglist.
1620 If they don't match, ask for a warning (but no error). */
1621 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1622 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1623 enum_and_int_p
, different_types_p
))
1629 if (!self_promoting_args_p (args1
))
1631 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1632 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1633 enum_and_int_p
, different_types_p
))
1638 /* Both types have argument lists: compare them and propagate results. */
1639 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1641 return val1
!= 1 ? val1
: val
;
1644 /* Check two lists of types for compatibility, returning 0 for
1645 incompatible, 1 for compatible, or 2 for compatible with
1646 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1647 comptypes_internal. */
1650 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1651 bool *enum_and_int_p
, bool *different_types_p
)
1653 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1659 tree a1
, mv1
, a2
, mv2
;
1660 if (args1
== 0 && args2
== 0)
1662 /* If one list is shorter than the other,
1663 they fail to match. */
1664 if (args1
== 0 || args2
== 0)
1666 mv1
= a1
= TREE_VALUE (args1
);
1667 mv2
= a2
= TREE_VALUE (args2
);
1668 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1669 mv1
= (TYPE_ATOMIC (mv1
)
1670 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1672 : TYPE_MAIN_VARIANT (mv1
));
1673 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1674 mv2
= (TYPE_ATOMIC (mv2
)
1675 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1677 : TYPE_MAIN_VARIANT (mv2
));
1678 /* A null pointer instead of a type
1679 means there is supposed to be an argument
1680 but nothing is specified about what type it has.
1681 So match anything that self-promotes. */
1682 if (different_types_p
!= NULL
1683 && (a1
== 0) != (a2
== 0))
1684 *different_types_p
= true;
1687 if (c_type_promotes_to (a2
) != a2
)
1692 if (c_type_promotes_to (a1
) != a1
)
1695 /* If one of the lists has an error marker, ignore this arg. */
1696 else if (TREE_CODE (a1
) == ERROR_MARK
1697 || TREE_CODE (a2
) == ERROR_MARK
)
1699 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1700 different_types_p
)))
1702 if (different_types_p
!= NULL
)
1703 *different_types_p
= true;
1704 /* Allow wait (union {union wait *u; int *i} *)
1705 and wait (union wait *) to be compatible. */
1706 if (TREE_CODE (a1
) == UNION_TYPE
1707 && (TYPE_NAME (a1
) == 0
1708 || TYPE_TRANSPARENT_AGGR (a1
))
1709 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1710 && tree_int_cst_equal (TYPE_SIZE (a1
),
1714 for (memb
= TYPE_FIELDS (a1
);
1715 memb
; memb
= DECL_CHAIN (memb
))
1717 tree mv3
= TREE_TYPE (memb
);
1718 if (mv3
&& mv3
!= error_mark_node
1719 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1720 mv3
= (TYPE_ATOMIC (mv3
)
1721 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1723 : TYPE_MAIN_VARIANT (mv3
));
1724 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1731 else if (TREE_CODE (a2
) == UNION_TYPE
1732 && (TYPE_NAME (a2
) == 0
1733 || TYPE_TRANSPARENT_AGGR (a2
))
1734 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1735 && tree_int_cst_equal (TYPE_SIZE (a2
),
1739 for (memb
= TYPE_FIELDS (a2
);
1740 memb
; memb
= DECL_CHAIN (memb
))
1742 tree mv3
= TREE_TYPE (memb
);
1743 if (mv3
&& mv3
!= error_mark_node
1744 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1745 mv3
= (TYPE_ATOMIC (mv3
)
1746 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1748 : TYPE_MAIN_VARIANT (mv3
));
1749 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1760 /* comptypes said ok, but record if it said to warn. */
1764 args1
= TREE_CHAIN (args1
);
1765 args2
= TREE_CHAIN (args2
);
1769 /* Compute the size to increment a pointer by. When a function type or void
1770 type or incomplete type is passed, size_one_node is returned.
1771 This function does not emit any diagnostics; the caller is responsible
1775 c_size_in_bytes (const_tree type
)
1777 enum tree_code code
= TREE_CODE (type
);
1779 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1780 || !COMPLETE_TYPE_P (type
))
1781 return size_one_node
;
1783 /* Convert in case a char is more than one unit. */
1784 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1785 size_int (TYPE_PRECISION (char_type_node
)
1789 /* Return either DECL or its known constant value (if it has one). */
1792 decl_constant_value (tree decl
)
1794 if (/* Don't change a variable array bound or initial value to a constant
1795 in a place where a variable is invalid. Note that DECL_INITIAL
1796 isn't valid for a PARM_DECL. */
1797 current_function_decl
!= 0
1798 && TREE_CODE (decl
) != PARM_DECL
1799 && !TREE_THIS_VOLATILE (decl
)
1800 && TREE_READONLY (decl
)
1801 && DECL_INITIAL (decl
) != 0
1802 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1803 /* This is invalid if initial value is not constant.
1804 If it has either a function call, a memory reference,
1805 or a variable, then re-evaluating it could give different results. */
1806 && TREE_CONSTANT (DECL_INITIAL (decl
))
1807 /* Check for cases where this is sub-optimal, even though valid. */
1808 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1809 return DECL_INITIAL (decl
);
1813 /* Convert the array expression EXP to a pointer. */
1815 array_to_pointer_conversion (location_t loc
, tree exp
)
1817 tree orig_exp
= exp
;
1818 tree type
= TREE_TYPE (exp
);
1820 tree restype
= TREE_TYPE (type
);
1823 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1825 STRIP_TYPE_NOPS (exp
);
1827 if (TREE_NO_WARNING (orig_exp
))
1828 TREE_NO_WARNING (exp
) = 1;
1830 ptrtype
= build_pointer_type (restype
);
1832 if (TREE_CODE (exp
) == INDIRECT_REF
)
1833 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1835 /* In C++ array compound literals are temporary objects unless they are
1836 const or appear in namespace scope, so they are destroyed too soon
1837 to use them for much of anything (c++/53220). */
1838 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1840 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1841 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1842 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1843 "converting an array compound literal to a pointer "
1844 "is ill-formed in C++");
1847 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1848 return convert (ptrtype
, adr
);
1851 /* Convert the function expression EXP to a pointer. */
1853 function_to_pointer_conversion (location_t loc
, tree exp
)
1855 tree orig_exp
= exp
;
1857 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1859 STRIP_TYPE_NOPS (exp
);
1861 if (TREE_NO_WARNING (orig_exp
))
1862 TREE_NO_WARNING (exp
) = 1;
1864 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1867 /* Mark EXP as read, not just set, for set but not used -Wunused
1868 warning purposes. */
1871 mark_exp_read (tree exp
)
1873 switch (TREE_CODE (exp
))
1877 DECL_READ_P (exp
) = 1;
1886 mark_exp_read (TREE_OPERAND (exp
, 0));
1889 case C_MAYBE_CONST_EXPR
:
1890 mark_exp_read (TREE_OPERAND (exp
, 1));
1897 /* Perform the default conversion of arrays and functions to pointers.
1898 Return the result of converting EXP. For any other expression, just
1901 LOC is the location of the expression. */
1904 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1906 tree orig_exp
= exp
.value
;
1907 tree type
= TREE_TYPE (exp
.value
);
1908 enum tree_code code
= TREE_CODE (type
);
1914 bool not_lvalue
= false;
1915 bool lvalue_array_p
;
1917 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1918 || CONVERT_EXPR_P (exp
.value
))
1919 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1921 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1923 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1926 if (TREE_NO_WARNING (orig_exp
))
1927 TREE_NO_WARNING (exp
.value
) = 1;
1929 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1930 if (!flag_isoc99
&& !lvalue_array_p
)
1932 /* Before C99, non-lvalue arrays do not decay to pointers.
1933 Normally, using such an array would be invalid; but it can
1934 be used correctly inside sizeof or as a statement expression.
1935 Thus, do not give an error here; an error will result later. */
1939 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1943 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1953 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1955 mark_exp_read (exp
.value
);
1956 return default_function_array_conversion (loc
, exp
);
1959 /* Return whether EXPR should be treated as an atomic lvalue for the
1960 purposes of load and store handling. */
1963 really_atomic_lvalue (tree expr
)
1965 if (error_operand_p (expr
))
1967 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
1969 if (!lvalue_p (expr
))
1972 /* Ignore _Atomic on register variables, since their addresses can't
1973 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1974 sequences wouldn't work. Ignore _Atomic on structures containing
1975 bit-fields, since accessing elements of atomic structures or
1976 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1977 it's undefined at translation time or execution time, and the
1978 normal atomic sequences again wouldn't work. */
1979 while (handled_component_p (expr
))
1981 if (TREE_CODE (expr
) == COMPONENT_REF
1982 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1984 expr
= TREE_OPERAND (expr
, 0);
1986 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
1991 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1992 including converting functions and arrays to pointers if CONVERT_P.
1993 If READ_P, also mark the expression as having been read. */
1996 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
1997 bool convert_p
, bool read_p
)
2000 mark_exp_read (exp
.value
);
2002 exp
= default_function_array_conversion (loc
, exp
);
2003 if (really_atomic_lvalue (exp
.value
))
2005 vec
<tree
, va_gc
> *params
;
2006 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2007 tree expr_type
= TREE_TYPE (exp
.value
);
2008 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, 0);
2009 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2011 gcc_assert (TYPE_ATOMIC (expr_type
));
2013 /* Expansion of a generic atomic load may require an addition
2014 element, so allocate enough to prevent a resize. */
2015 vec_alloc (params
, 4);
2017 /* Remove the qualifiers for the rest of the expressions and
2018 create the VAL temp variable to hold the RHS. */
2019 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2020 tmp
= create_tmp_var_raw (nonatomic_type
);
2021 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, 0);
2022 TREE_ADDRESSABLE (tmp
) = 1;
2023 TREE_NO_WARNING (tmp
) = 1;
2025 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2026 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2027 params
->quick_push (expr_addr
);
2028 params
->quick_push (tmp_addr
);
2029 params
->quick_push (seq_cst
);
2030 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2032 /* EXPR is always read. */
2033 mark_exp_read (exp
.value
);
2035 /* Return tmp which contains the value loaded. */
2036 exp
.value
= build4 (TARGET_EXPR
, nonatomic_type
, tmp
, func_call
,
2037 NULL_TREE
, NULL_TREE
);
2042 /* EXP is an expression of integer type. Apply the integer promotions
2043 to it and return the promoted value. */
2046 perform_integral_promotions (tree exp
)
2048 tree type
= TREE_TYPE (exp
);
2049 enum tree_code code
= TREE_CODE (type
);
2051 gcc_assert (INTEGRAL_TYPE_P (type
));
2053 /* Normally convert enums to int,
2054 but convert wide enums to something wider. */
2055 if (code
== ENUMERAL_TYPE
)
2057 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2058 TYPE_PRECISION (integer_type_node
)),
2059 ((TYPE_PRECISION (type
)
2060 >= TYPE_PRECISION (integer_type_node
))
2061 && TYPE_UNSIGNED (type
)));
2063 return convert (type
, exp
);
2066 /* ??? This should no longer be needed now bit-fields have their
2068 if (TREE_CODE (exp
) == COMPONENT_REF
2069 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2070 /* If it's thinner than an int, promote it like a
2071 c_promoting_integer_type_p, otherwise leave it alone. */
2072 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2073 TYPE_PRECISION (integer_type_node
)))
2074 return convert (integer_type_node
, exp
);
2076 if (c_promoting_integer_type_p (type
))
2078 /* Preserve unsignedness if not really getting any wider. */
2079 if (TYPE_UNSIGNED (type
)
2080 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2081 return convert (unsigned_type_node
, exp
);
2083 return convert (integer_type_node
, exp
);
2090 /* Perform default promotions for C data used in expressions.
2091 Enumeral types or short or char are converted to int.
2092 In addition, manifest constants symbols are replaced by their values. */
2095 default_conversion (tree exp
)
2098 tree type
= TREE_TYPE (exp
);
2099 enum tree_code code
= TREE_CODE (type
);
2102 mark_exp_read (exp
);
2104 /* Functions and arrays have been converted during parsing. */
2105 gcc_assert (code
!= FUNCTION_TYPE
);
2106 if (code
== ARRAY_TYPE
)
2109 /* Constants can be used directly unless they're not loadable. */
2110 if (TREE_CODE (exp
) == CONST_DECL
)
2111 exp
= DECL_INITIAL (exp
);
2113 /* Strip no-op conversions. */
2115 STRIP_TYPE_NOPS (exp
);
2117 if (TREE_NO_WARNING (orig_exp
))
2118 TREE_NO_WARNING (exp
) = 1;
2120 if (code
== VOID_TYPE
)
2122 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2123 "void value not ignored as it ought to be");
2124 return error_mark_node
;
2127 exp
= require_complete_type (exp
);
2128 if (exp
== error_mark_node
)
2129 return error_mark_node
;
2131 promoted_type
= targetm
.promoted_type (type
);
2133 return convert (promoted_type
, exp
);
2135 if (INTEGRAL_TYPE_P (type
))
2136 return perform_integral_promotions (exp
);
2141 /* Look up COMPONENT in a structure or union TYPE.
2143 If the component name is not found, returns NULL_TREE. Otherwise,
2144 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2145 stepping down the chain to the component, which is in the last
2146 TREE_VALUE of the list. Normally the list is of length one, but if
2147 the component is embedded within (nested) anonymous structures or
2148 unions, the list steps down the chain to the component. */
2151 lookup_field (tree type
, tree component
)
2155 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2156 to the field elements. Use a binary search on this array to quickly
2157 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2158 will always be set for structures which have many elements. */
2160 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2163 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2165 field
= TYPE_FIELDS (type
);
2167 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2168 while (top
- bot
> 1)
2170 half
= (top
- bot
+ 1) >> 1;
2171 field
= field_array
[bot
+half
];
2173 if (DECL_NAME (field
) == NULL_TREE
)
2175 /* Step through all anon unions in linear fashion. */
2176 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2178 field
= field_array
[bot
++];
2179 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2180 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
2182 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2185 return tree_cons (NULL_TREE
, field
, anon
);
2187 /* The Plan 9 compiler permits referring
2188 directly to an anonymous struct/union field
2189 using a typedef name. */
2190 if (flag_plan9_extensions
2191 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2192 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2194 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2200 /* Entire record is only anon unions. */
2204 /* Restart the binary search, with new lower bound. */
2208 if (DECL_NAME (field
) == component
)
2210 if (DECL_NAME (field
) < component
)
2216 if (DECL_NAME (field_array
[bot
]) == component
)
2217 field
= field_array
[bot
];
2218 else if (DECL_NAME (field
) != component
)
2223 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2225 if (DECL_NAME (field
) == NULL_TREE
2226 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2227 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
2229 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2232 return tree_cons (NULL_TREE
, field
, anon
);
2234 /* The Plan 9 compiler permits referring directly to an
2235 anonymous struct/union field using a typedef
2237 if (flag_plan9_extensions
2238 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2239 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2240 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2245 if (DECL_NAME (field
) == component
)
2249 if (field
== NULL_TREE
)
2253 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2256 /* Make an expression to refer to the COMPONENT field of structure or
2257 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2258 location of the COMPONENT_REF. */
2261 build_component_ref (location_t loc
, tree datum
, tree component
)
2263 tree type
= TREE_TYPE (datum
);
2264 enum tree_code code
= TREE_CODE (type
);
2267 bool datum_lvalue
= lvalue_p (datum
);
2269 if (!objc_is_public (datum
, component
))
2270 return error_mark_node
;
2272 /* Detect Objective-C property syntax object.property. */
2273 if (c_dialect_objc ()
2274 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2277 /* See if there is a field or component with name COMPONENT. */
2279 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2281 if (!COMPLETE_TYPE_P (type
))
2283 c_incomplete_type_error (NULL_TREE
, type
);
2284 return error_mark_node
;
2287 field
= lookup_field (type
, component
);
2291 error_at (loc
, "%qT has no member named %qE", type
, component
);
2292 return error_mark_node
;
2295 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2296 This might be better solved in future the way the C++ front
2297 end does it - by giving the anonymous entities each a
2298 separate name and type, and then have build_component_ref
2299 recursively call itself. We can't do that here. */
2302 tree subdatum
= TREE_VALUE (field
);
2305 bool use_datum_quals
;
2307 if (TREE_TYPE (subdatum
) == error_mark_node
)
2308 return error_mark_node
;
2310 /* If this is an rvalue, it does not have qualifiers in C
2311 standard terms and we must avoid propagating such
2312 qualifiers down to a non-lvalue array that is then
2313 converted to a pointer. */
2314 use_datum_quals
= (datum_lvalue
2315 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2317 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2318 if (use_datum_quals
)
2319 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2320 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2322 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2324 SET_EXPR_LOCATION (ref
, loc
);
2325 if (TREE_READONLY (subdatum
)
2326 || (use_datum_quals
&& TREE_READONLY (datum
)))
2327 TREE_READONLY (ref
) = 1;
2328 if (TREE_THIS_VOLATILE (subdatum
)
2329 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2330 TREE_THIS_VOLATILE (ref
) = 1;
2332 if (TREE_DEPRECATED (subdatum
))
2333 warn_deprecated_use (subdatum
, NULL_TREE
);
2337 field
= TREE_CHAIN (field
);
2343 else if (code
!= ERROR_MARK
)
2345 "request for member %qE in something not a structure or union",
2348 return error_mark_node
;
2351 /* Given an expression PTR for a pointer, return an expression
2352 for the value pointed to.
2353 ERRORSTRING is the name of the operator to appear in error messages.
2355 LOC is the location to use for the generated tree. */
2358 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2360 tree pointer
= default_conversion (ptr
);
2361 tree type
= TREE_TYPE (pointer
);
2364 if (TREE_CODE (type
) == POINTER_TYPE
)
2366 if (CONVERT_EXPR_P (pointer
)
2367 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2369 /* If a warning is issued, mark it to avoid duplicates from
2370 the backend. This only needs to be done at
2371 warn_strict_aliasing > 2. */
2372 if (warn_strict_aliasing
> 2)
2373 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2374 type
, TREE_OPERAND (pointer
, 0)))
2375 TREE_NO_WARNING (pointer
) = 1;
2378 if (TREE_CODE (pointer
) == ADDR_EXPR
2379 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2380 == TREE_TYPE (type
)))
2382 ref
= TREE_OPERAND (pointer
, 0);
2383 protected_set_expr_location (ref
, loc
);
2388 tree t
= TREE_TYPE (type
);
2390 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2392 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2394 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)))
2396 error_at (loc
, "dereferencing pointer to incomplete type "
2398 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)) = 1;
2400 return error_mark_node
;
2402 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2403 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2405 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2406 so that we get the proper error message if the result is used
2407 to assign to. Also, &* is supposed to be a no-op.
2408 And ANSI C seems to specify that the type of the result
2409 should be the const type. */
2410 /* A de-reference of a pointer to const is not a const. It is valid
2411 to change it via some other pointer. */
2412 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2413 TREE_SIDE_EFFECTS (ref
)
2414 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2415 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2416 protected_set_expr_location (ref
, loc
);
2420 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2421 invalid_indirection_error (loc
, type
, errstring
);
2423 return error_mark_node
;
2426 /* This handles expressions of the form "a[i]", which denotes
2429 This is logically equivalent in C to *(a+i), but we may do it differently.
2430 If A is a variable or a member, we generate a primitive ARRAY_REF.
2431 This avoids forcing the array out of registers, and can work on
2432 arrays that are not lvalues (for example, members of structures returned
2435 For vector types, allow vector[i] but not i[vector], and create
2436 *(((type*)&vectortype) + i) for the expression.
2438 LOC is the location to use for the returned expression. */
2441 build_array_ref (location_t loc
, tree array
, tree index
)
2444 bool swapped
= false;
2445 if (TREE_TYPE (array
) == error_mark_node
2446 || TREE_TYPE (index
) == error_mark_node
)
2447 return error_mark_node
;
2449 if (flag_cilkplus
&& contains_array_notation_expr (index
))
2452 if (!find_rank (loc
, index
, index
, true, &rank
))
2453 return error_mark_node
;
2456 error_at (loc
, "rank of the array's index is greater than 1");
2457 return error_mark_node
;
2460 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2461 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2462 /* Allow vector[index] but not index[vector]. */
2463 && TREE_CODE (TREE_TYPE (array
)) != VECTOR_TYPE
)
2465 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2466 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2469 "subscripted value is neither array nor pointer nor vector");
2471 return error_mark_node
;
2473 std::swap (array
, index
);
2477 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2479 error_at (loc
, "array subscript is not an integer");
2480 return error_mark_node
;
2483 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2485 error_at (loc
, "subscripted value is pointer to function");
2486 return error_mark_node
;
2489 /* ??? Existing practice has been to warn only when the char
2490 index is syntactically the index, not for char[array]. */
2492 warn_array_subscript_with_type_char (loc
, index
);
2494 /* Apply default promotions *after* noticing character types. */
2495 index
= default_conversion (index
);
2496 if (index
== error_mark_node
)
2497 return error_mark_node
;
2499 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2502 = convert_vector_to_pointer_for_subscript (loc
, &array
, index
);
2504 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2508 /* An array that is indexed by a non-constant
2509 cannot be stored in a register; we must be able to do
2510 address arithmetic on its address.
2511 Likewise an array of elements of variable size. */
2512 if (TREE_CODE (index
) != INTEGER_CST
2513 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2514 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2516 if (!c_mark_addressable (array
))
2517 return error_mark_node
;
2519 /* An array that is indexed by a constant value which is not within
2520 the array bounds cannot be stored in a register either; because we
2521 would get a crash in store_bit_field/extract_bit_field when trying
2522 to access a non-existent part of the register. */
2523 if (TREE_CODE (index
) == INTEGER_CST
2524 && TYPE_DOMAIN (TREE_TYPE (array
))
2525 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2527 if (!c_mark_addressable (array
))
2528 return error_mark_node
;
2531 if (pedantic
|| warn_c90_c99_compat
)
2534 while (TREE_CODE (foo
) == COMPONENT_REF
)
2535 foo
= TREE_OPERAND (foo
, 0);
2536 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2537 pedwarn (loc
, OPT_Wpedantic
,
2538 "ISO C forbids subscripting %<register%> array");
2539 else if (!lvalue_p (foo
))
2540 pedwarn_c90 (loc
, OPT_Wpedantic
,
2541 "ISO C90 forbids subscripting non-lvalue "
2545 type
= TREE_TYPE (TREE_TYPE (array
));
2546 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2547 /* Array ref is const/volatile if the array elements are
2548 or if the array is. */
2549 TREE_READONLY (rval
)
2550 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2551 | TREE_READONLY (array
));
2552 TREE_SIDE_EFFECTS (rval
)
2553 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2554 | TREE_SIDE_EFFECTS (array
));
2555 TREE_THIS_VOLATILE (rval
)
2556 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2557 /* This was added by rms on 16 Nov 91.
2558 It fixes vol struct foo *a; a->elts[1]
2559 in an inline function.
2560 Hope it doesn't break something else. */
2561 | TREE_THIS_VOLATILE (array
));
2562 ret
= require_complete_type (rval
);
2563 protected_set_expr_location (ret
, loc
);
2565 ret
= non_lvalue_loc (loc
, ret
);
2570 tree ar
= default_conversion (array
);
2572 if (ar
== error_mark_node
)
2575 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2576 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2578 ret
= build_indirect_ref (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
,
2582 ret
= non_lvalue_loc (loc
, ret
);
2587 /* Build an external reference to identifier ID. FUN indicates
2588 whether this will be used for a function call. LOC is the source
2589 location of the identifier. This sets *TYPE to the type of the
2590 identifier, which is not the same as the type of the returned value
2591 for CONST_DECLs defined as enum constants. If the type of the
2592 identifier is not available, *TYPE is set to NULL. */
2594 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2597 tree decl
= lookup_name (id
);
2599 /* In Objective-C, an instance variable (ivar) may be preferred to
2600 whatever lookup_name() found. */
2601 decl
= objc_lookup_ivar (decl
, id
);
2604 if (decl
&& decl
!= error_mark_node
)
2607 *type
= TREE_TYPE (ref
);
2610 /* Implicit function declaration. */
2611 ref
= implicitly_declare (loc
, id
);
2612 else if (decl
== error_mark_node
)
2613 /* Don't complain about something that's already been
2614 complained about. */
2615 return error_mark_node
;
2618 undeclared_variable (loc
, id
);
2619 return error_mark_node
;
2622 if (TREE_TYPE (ref
) == error_mark_node
)
2623 return error_mark_node
;
2625 if (TREE_DEPRECATED (ref
))
2626 warn_deprecated_use (ref
, NULL_TREE
);
2628 /* Recursive call does not count as usage. */
2629 if (ref
!= current_function_decl
)
2631 TREE_USED (ref
) = 1;
2634 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2636 if (!in_sizeof
&& !in_typeof
)
2637 C_DECL_USED (ref
) = 1;
2638 else if (DECL_INITIAL (ref
) == 0
2639 && DECL_EXTERNAL (ref
)
2640 && !TREE_PUBLIC (ref
))
2641 record_maybe_used_decl (ref
);
2644 if (TREE_CODE (ref
) == CONST_DECL
)
2646 used_types_insert (TREE_TYPE (ref
));
2649 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2650 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2652 warning_at (loc
, OPT_Wc___compat
,
2653 ("enum constant defined in struct or union "
2654 "is not visible in C++"));
2655 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2658 ref
= DECL_INITIAL (ref
);
2659 TREE_CONSTANT (ref
) = 1;
2661 else if (current_function_decl
!= 0
2662 && !DECL_FILE_SCOPE_P (current_function_decl
)
2663 && (VAR_OR_FUNCTION_DECL_P (ref
)
2664 || TREE_CODE (ref
) == PARM_DECL
))
2666 tree context
= decl_function_context (ref
);
2668 if (context
!= 0 && context
!= current_function_decl
)
2669 DECL_NONLOCAL (ref
) = 1;
2671 /* C99 6.7.4p3: An inline definition of a function with external
2672 linkage ... shall not contain a reference to an identifier with
2673 internal linkage. */
2674 else if (current_function_decl
!= 0
2675 && DECL_DECLARED_INLINE_P (current_function_decl
)
2676 && DECL_EXTERNAL (current_function_decl
)
2677 && VAR_OR_FUNCTION_DECL_P (ref
)
2678 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2679 && ! TREE_PUBLIC (ref
)
2680 && DECL_CONTEXT (ref
) != current_function_decl
)
2681 record_inline_static (loc
, current_function_decl
, ref
,
2687 /* Record details of decls possibly used inside sizeof or typeof. */
2688 struct maybe_used_decl
2692 /* The level seen at (in_sizeof + in_typeof). */
2694 /* The next one at this level or above, or NULL. */
2695 struct maybe_used_decl
*next
;
2698 static struct maybe_used_decl
*maybe_used_decls
;
2700 /* Record that DECL, an undefined static function reference seen
2701 inside sizeof or typeof, might be used if the operand of sizeof is
2702 a VLA type or the operand of typeof is a variably modified
2706 record_maybe_used_decl (tree decl
)
2708 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2710 t
->level
= in_sizeof
+ in_typeof
;
2711 t
->next
= maybe_used_decls
;
2712 maybe_used_decls
= t
;
2715 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2716 USED is false, just discard them. If it is true, mark them used
2717 (if no longer inside sizeof or typeof) or move them to the next
2718 level up (if still inside sizeof or typeof). */
2721 pop_maybe_used (bool used
)
2723 struct maybe_used_decl
*p
= maybe_used_decls
;
2724 int cur_level
= in_sizeof
+ in_typeof
;
2725 while (p
&& p
->level
> cur_level
)
2730 C_DECL_USED (p
->decl
) = 1;
2732 p
->level
= cur_level
;
2736 if (!used
|| cur_level
== 0)
2737 maybe_used_decls
= p
;
2740 /* Return the result of sizeof applied to EXPR. */
2743 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2746 if (expr
.value
== error_mark_node
)
2748 ret
.value
= error_mark_node
;
2749 ret
.original_code
= ERROR_MARK
;
2750 ret
.original_type
= NULL
;
2751 pop_maybe_used (false);
2755 bool expr_const_operands
= true;
2757 if (TREE_CODE (expr
.value
) == PARM_DECL
2758 && C_ARRAY_PARAMETER (expr
.value
))
2760 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2761 "%<sizeof%> on array function parameter %qE will "
2762 "return size of %qT", expr
.value
,
2763 expr
.original_type
))
2764 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2766 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2767 &expr_const_operands
);
2768 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2769 c_last_sizeof_arg
= expr
.value
;
2770 ret
.original_code
= SIZEOF_EXPR
;
2771 ret
.original_type
= NULL
;
2772 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2774 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2775 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2776 folded_expr
, ret
.value
);
2777 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2778 SET_EXPR_LOCATION (ret
.value
, loc
);
2780 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2785 /* Return the result of sizeof applied to T, a structure for the type
2786 name passed to sizeof (rather than the type itself). LOC is the
2787 location of the original expression. */
2790 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2794 tree type_expr
= NULL_TREE
;
2795 bool type_expr_const
= true;
2796 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2797 ret
.value
= c_sizeof (loc
, type
);
2798 c_last_sizeof_arg
= type
;
2799 ret
.original_code
= SIZEOF_EXPR
;
2800 ret
.original_type
= NULL
;
2801 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2802 && c_vla_type_p (type
))
2804 /* If the type is a [*] array, it is a VLA but is represented as
2805 having a size of zero. In such a case we must ensure that
2806 the result of sizeof does not get folded to a constant by
2807 c_fully_fold, because if the size is evaluated the result is
2808 not constant and so constraints on zero or negative size
2809 arrays must not be applied when this sizeof call is inside
2810 another array declarator. */
2812 type_expr
= integer_zero_node
;
2813 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2814 type_expr
, ret
.value
);
2815 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2817 pop_maybe_used (type
!= error_mark_node
2818 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2822 /* Build a function call to function FUNCTION with parameters PARAMS.
2823 The function call is at LOC.
2824 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2825 TREE_VALUE of each node is a parameter-expression.
2826 FUNCTION's data type may be a function type or a pointer-to-function. */
2829 build_function_call (location_t loc
, tree function
, tree params
)
2831 vec
<tree
, va_gc
> *v
;
2834 vec_alloc (v
, list_length (params
));
2835 for (; params
; params
= TREE_CHAIN (params
))
2836 v
->quick_push (TREE_VALUE (params
));
2837 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2842 /* Give a note about the location of the declaration of DECL. */
2845 inform_declaration (tree decl
)
2847 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_IS_BUILTIN (decl
)))
2848 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2851 /* Build a function call to function FUNCTION with parameters PARAMS.
2852 ORIGTYPES, if not NULL, is a vector of types; each element is
2853 either NULL or the original type of the corresponding element in
2854 PARAMS. The original type may differ from TREE_TYPE of the
2855 parameter for enums. FUNCTION's data type may be a function type
2856 or pointer-to-function. This function changes the elements of
2860 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
2861 tree function
, vec
<tree
, va_gc
> *params
,
2862 vec
<tree
, va_gc
> *origtypes
)
2864 tree fntype
, fundecl
= 0;
2865 tree name
= NULL_TREE
, result
;
2871 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2872 STRIP_TYPE_NOPS (function
);
2874 /* Convert anything with function type to a pointer-to-function. */
2875 if (TREE_CODE (function
) == FUNCTION_DECL
)
2877 name
= DECL_NAME (function
);
2880 tm_malloc_replacement (function
);
2882 /* Atomic functions have type checking/casting already done. They are
2883 often rewritten and don't match the original parameter list. */
2884 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
2888 && is_cilkplus_reduce_builtin (function
))
2891 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2892 function
= function_to_pointer_conversion (loc
, function
);
2894 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2895 expressions, like those used for ObjC messenger dispatches. */
2896 if (params
&& !params
->is_empty ())
2897 function
= objc_rewrite_function_call (function
, (*params
)[0]);
2899 function
= c_fully_fold (function
, false, NULL
);
2901 fntype
= TREE_TYPE (function
);
2903 if (TREE_CODE (fntype
) == ERROR_MARK
)
2904 return error_mark_node
;
2906 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2907 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2909 if (!flag_diagnostics_show_caret
)
2911 "called object %qE is not a function or function pointer",
2913 else if (DECL_P (function
))
2916 "called object %qD is not a function or function pointer",
2918 inform_declaration (function
);
2922 "called object is not a function or function pointer");
2923 return error_mark_node
;
2926 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2927 current_function_returns_abnormally
= 1;
2929 /* fntype now gets the type of function pointed to. */
2930 fntype
= TREE_TYPE (fntype
);
2932 /* Convert the parameters to the types declared in the
2933 function prototype, or apply default promotions. */
2935 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
2936 origtypes
, function
, fundecl
);
2938 return error_mark_node
;
2940 /* Check that the function is called through a compatible prototype.
2941 If it is not, warn. */
2942 if (CONVERT_EXPR_P (function
)
2943 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2944 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2945 && !comptypes (fntype
, TREE_TYPE (tem
)))
2947 tree return_type
= TREE_TYPE (fntype
);
2949 /* This situation leads to run-time undefined behavior. We can't,
2950 therefore, simply error unless we can prove that all possible
2951 executions of the program must execute the code. */
2952 warning_at (loc
, 0, "function called through a non-compatible type");
2954 if (VOID_TYPE_P (return_type
)
2955 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2957 "function with qualified void return type called");
2960 argarray
= vec_safe_address (params
);
2962 /* Check that arguments to builtin functions match the expectations. */
2964 && DECL_BUILT_IN (fundecl
)
2965 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2966 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2967 return error_mark_node
;
2969 /* Check that the arguments to the function are valid. */
2970 check_function_arguments (fntype
, nargs
, argarray
);
2972 if (name
!= NULL_TREE
2973 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
2975 if (require_constant_value
)
2977 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
2978 function
, nargs
, argarray
);
2980 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
2981 function
, nargs
, argarray
);
2982 if (TREE_CODE (result
) == NOP_EXPR
2983 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
2984 STRIP_TYPE_NOPS (result
);
2987 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
2988 function
, nargs
, argarray
);
2990 if (VOID_TYPE_P (TREE_TYPE (result
)))
2992 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
2994 "function with qualified void return type called");
2997 return require_complete_type (result
);
3000 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3003 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3004 tree function
, vec
<tree
, va_gc
> *params
,
3005 vec
<tree
, va_gc
> *origtypes
)
3007 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3008 STRIP_TYPE_NOPS (function
);
3010 /* Convert anything with function type to a pointer-to-function. */
3011 if (TREE_CODE (function
) == FUNCTION_DECL
)
3013 /* Implement type-directed function overloading for builtins.
3014 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3015 handle all the type checking. The result is a complete expression
3016 that implements this function call. */
3017 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3021 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3024 /* Convert the argument expressions in the vector VALUES
3025 to the types in the list TYPELIST.
3027 If TYPELIST is exhausted, or when an element has NULL as its type,
3028 perform the default conversions.
3030 ORIGTYPES is the original types of the expressions in VALUES. This
3031 holds the type of enum values which have been converted to integral
3032 types. It may be NULL.
3034 FUNCTION is a tree for the called function. It is used only for
3035 error messages, where it is formatted with %qE.
3037 This is also where warnings about wrong number of args are generated.
3039 ARG_LOC are locations of function arguments (if any).
3041 Returns the actual number of arguments processed (which may be less
3042 than the length of VALUES in some error situations), or -1 on
3046 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3047 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3048 tree function
, tree fundecl
)
3051 unsigned int parmnum
;
3052 bool error_args
= false;
3053 const bool type_generic
= fundecl
3054 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3055 bool type_generic_remove_excess_precision
= false;
3058 /* Change pointer to function to the function itself for
3060 if (TREE_CODE (function
) == ADDR_EXPR
3061 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3062 function
= TREE_OPERAND (function
, 0);
3064 /* Handle an ObjC selector specially for diagnostics. */
3065 selector
= objc_message_selector ();
3067 /* For type-generic built-in functions, determine whether excess
3068 precision should be removed (classification) or not
3071 && DECL_BUILT_IN (fundecl
)
3072 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3074 switch (DECL_FUNCTION_CODE (fundecl
))
3076 case BUILT_IN_ISFINITE
:
3077 case BUILT_IN_ISINF
:
3078 case BUILT_IN_ISINF_SIGN
:
3079 case BUILT_IN_ISNAN
:
3080 case BUILT_IN_ISNORMAL
:
3081 case BUILT_IN_FPCLASSIFY
:
3082 type_generic_remove_excess_precision
= true;
3086 type_generic_remove_excess_precision
= false;
3090 if (flag_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3091 return vec_safe_length (values
);
3093 /* Scan the given expressions and types, producing individual
3094 converted arguments. */
3096 for (typetail
= typelist
, parmnum
= 0;
3097 values
&& values
->iterate (parmnum
, &val
);
3100 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3101 tree valtype
= TREE_TYPE (val
);
3102 tree rname
= function
;
3103 int argnum
= parmnum
+ 1;
3104 const char *invalid_func_diag
;
3105 bool excess_precision
= false;
3108 /* Some __atomic_* builtins have additional hidden argument at
3111 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3112 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3115 if (type
== void_type_node
)
3118 error_at (loc
, "too many arguments to method %qE", selector
);
3120 error_at (loc
, "too many arguments to function %qE", function
);
3121 inform_declaration (fundecl
);
3122 return error_args
? -1 : (int) parmnum
;
3125 if (selector
&& argnum
> 2)
3131 npc
= null_pointer_constant_p (val
);
3133 /* If there is excess precision and a prototype, convert once to
3134 the required type rather than converting via the semantic
3135 type. Likewise without a prototype a float value represented
3136 as long double should be converted once to double. But for
3137 type-generic classification functions excess precision must
3139 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3140 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3142 val
= TREE_OPERAND (val
, 0);
3143 excess_precision
= true;
3145 val
= c_fully_fold (val
, false, NULL
);
3146 STRIP_TYPE_NOPS (val
);
3148 val
= require_complete_type (val
);
3152 /* Formal parm type is specified by a function prototype. */
3154 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3156 error_at (ploc
, "type of formal parameter %d is incomplete",
3164 /* Optionally warn about conversions that
3165 differ from the default conversions. */
3166 if (warn_traditional_conversion
|| warn_traditional
)
3168 unsigned int formal_prec
= TYPE_PRECISION (type
);
3170 if (INTEGRAL_TYPE_P (type
)
3171 && TREE_CODE (valtype
) == REAL_TYPE
)
3172 warning_at (ploc
, OPT_Wtraditional_conversion
,
3173 "passing argument %d of %qE as integer rather "
3174 "than floating due to prototype",
3176 if (INTEGRAL_TYPE_P (type
)
3177 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3178 warning_at (ploc
, OPT_Wtraditional_conversion
,
3179 "passing argument %d of %qE as integer rather "
3180 "than complex due to prototype",
3182 else if (TREE_CODE (type
) == COMPLEX_TYPE
3183 && TREE_CODE (valtype
) == REAL_TYPE
)
3184 warning_at (ploc
, OPT_Wtraditional_conversion
,
3185 "passing argument %d of %qE as complex rather "
3186 "than floating due to prototype",
3188 else if (TREE_CODE (type
) == REAL_TYPE
3189 && INTEGRAL_TYPE_P (valtype
))
3190 warning_at (ploc
, OPT_Wtraditional_conversion
,
3191 "passing argument %d of %qE as floating rather "
3192 "than integer due to prototype",
3194 else if (TREE_CODE (type
) == COMPLEX_TYPE
3195 && INTEGRAL_TYPE_P (valtype
))
3196 warning_at (ploc
, OPT_Wtraditional_conversion
,
3197 "passing argument %d of %qE as complex rather "
3198 "than integer due to prototype",
3200 else if (TREE_CODE (type
) == REAL_TYPE
3201 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3202 warning_at (ploc
, OPT_Wtraditional_conversion
,
3203 "passing argument %d of %qE as floating rather "
3204 "than complex due to prototype",
3206 /* ??? At some point, messages should be written about
3207 conversions between complex types, but that's too messy
3209 else if (TREE_CODE (type
) == REAL_TYPE
3210 && TREE_CODE (valtype
) == REAL_TYPE
)
3212 /* Warn if any argument is passed as `float',
3213 since without a prototype it would be `double'. */
3214 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3215 && type
!= dfloat32_type_node
)
3216 warning_at (ploc
, 0,
3217 "passing argument %d of %qE as %<float%> "
3218 "rather than %<double%> due to prototype",
3221 /* Warn if mismatch between argument and prototype
3222 for decimal float types. Warn of conversions with
3223 binary float types and of precision narrowing due to
3225 else if (type
!= valtype
3226 && (type
== dfloat32_type_node
3227 || type
== dfloat64_type_node
3228 || type
== dfloat128_type_node
3229 || valtype
== dfloat32_type_node
3230 || valtype
== dfloat64_type_node
3231 || valtype
== dfloat128_type_node
)
3233 <= TYPE_PRECISION (valtype
)
3234 || (type
== dfloat128_type_node
3236 != dfloat64_type_node
3238 != dfloat32_type_node
)))
3239 || (type
== dfloat64_type_node
3241 != dfloat32_type_node
))))
3242 warning_at (ploc
, 0,
3243 "passing argument %d of %qE as %qT "
3244 "rather than %qT due to prototype",
3245 argnum
, rname
, type
, valtype
);
3248 /* Detect integer changing in width or signedness.
3249 These warnings are only activated with
3250 -Wtraditional-conversion, not with -Wtraditional. */
3251 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3252 && INTEGRAL_TYPE_P (valtype
))
3254 tree would_have_been
= default_conversion (val
);
3255 tree type1
= TREE_TYPE (would_have_been
);
3257 if (TREE_CODE (type
) == ENUMERAL_TYPE
3258 && (TYPE_MAIN_VARIANT (type
)
3259 == TYPE_MAIN_VARIANT (valtype
)))
3260 /* No warning if function asks for enum
3261 and the actual arg is that enum type. */
3263 else if (formal_prec
!= TYPE_PRECISION (type1
))
3264 warning_at (ploc
, OPT_Wtraditional_conversion
,
3265 "passing argument %d of %qE "
3266 "with different width due to prototype",
3268 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3270 /* Don't complain if the formal parameter type
3271 is an enum, because we can't tell now whether
3272 the value was an enum--even the same enum. */
3273 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3275 else if (TREE_CODE (val
) == INTEGER_CST
3276 && int_fits_type_p (val
, type
))
3277 /* Change in signedness doesn't matter
3278 if a constant value is unaffected. */
3280 /* If the value is extended from a narrower
3281 unsigned type, it doesn't matter whether we
3282 pass it as signed or unsigned; the value
3283 certainly is the same either way. */
3284 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3285 && TYPE_UNSIGNED (valtype
))
3287 else if (TYPE_UNSIGNED (type
))
3288 warning_at (ploc
, OPT_Wtraditional_conversion
,
3289 "passing argument %d of %qE "
3290 "as unsigned due to prototype",
3293 warning_at (ploc
, OPT_Wtraditional_conversion
,
3294 "passing argument %d of %qE "
3295 "as signed due to prototype",
3300 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3301 sake of better warnings from convert_and_check. */
3302 if (excess_precision
)
3303 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3304 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3305 parmval
= convert_for_assignment (loc
, ploc
, type
,
3306 val
, origtype
, ic_argpass
,
3307 npc
, fundecl
, function
,
3310 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3311 && INTEGRAL_TYPE_P (type
)
3312 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3313 parmval
= default_conversion (parmval
);
3316 else if (TREE_CODE (valtype
) == REAL_TYPE
3317 && (TYPE_PRECISION (valtype
)
3318 <= TYPE_PRECISION (double_type_node
))
3319 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3320 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3321 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3327 /* Convert `float' to `double'. */
3328 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3329 warning_at (ploc
, OPT_Wdouble_promotion
,
3330 "implicit conversion from %qT to %qT when passing "
3331 "argument to function",
3332 valtype
, double_type_node
);
3333 parmval
= convert (double_type_node
, val
);
3336 else if (excess_precision
&& !type_generic
)
3337 /* A "double" argument with excess precision being passed
3338 without a prototype or in variable arguments. */
3339 parmval
= convert (valtype
, val
);
3340 else if ((invalid_func_diag
=
3341 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3343 error (invalid_func_diag
);
3347 /* Convert `short' and `char' to full-size `int'. */
3348 parmval
= default_conversion (val
);
3350 (*values
)[parmnum
] = parmval
;
3351 if (parmval
== error_mark_node
)
3355 typetail
= TREE_CHAIN (typetail
);
3358 gcc_assert (parmnum
== vec_safe_length (values
));
3360 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3362 error_at (loc
, "too few arguments to function %qE", function
);
3363 inform_declaration (fundecl
);
3367 return error_args
? -1 : (int) parmnum
;
3370 /* This is the entry point used by the parser to build unary operators
3371 in the input. CODE, a tree_code, specifies the unary operator, and
3372 ARG is the operand. For unary plus, the C parser currently uses
3373 CONVERT_EXPR for code.
3375 LOC is the location to use for the tree generated.
3379 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3381 struct c_expr result
;
3383 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
3384 result
.original_code
= code
;
3385 result
.original_type
= NULL
;
3387 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3388 overflow_warning (loc
, result
.value
);
3393 /* This is the entry point used by the parser to build binary operators
3394 in the input. CODE, a tree_code, specifies the binary operator, and
3395 ARG1 and ARG2 are the operands. In addition to constructing the
3396 expression, we check for operands that were written with other binary
3397 operators in a way that is likely to confuse the user.
3399 LOCATION is the location of the binary operator. */
3402 parser_build_binary_op (location_t location
, enum tree_code code
,
3403 struct c_expr arg1
, struct c_expr arg2
)
3405 struct c_expr result
;
3407 enum tree_code code1
= arg1
.original_code
;
3408 enum tree_code code2
= arg2
.original_code
;
3409 tree type1
= (arg1
.original_type
3410 ? arg1
.original_type
3411 : TREE_TYPE (arg1
.value
));
3412 tree type2
= (arg2
.original_type
3413 ? arg2
.original_type
3414 : TREE_TYPE (arg2
.value
));
3416 result
.value
= build_binary_op (location
, code
,
3417 arg1
.value
, arg2
.value
, 1);
3418 result
.original_code
= code
;
3419 result
.original_type
= NULL
;
3421 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3424 if (location
!= UNKNOWN_LOCATION
)
3425 protected_set_expr_location (result
.value
, location
);
3427 /* Check for cases such as x+y<<z which users are likely
3429 if (warn_parentheses
)
3430 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3433 if (warn_logical_op
)
3434 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3435 code1
, arg1
.value
, code2
, arg2
.value
);
3437 if (warn_logical_not_paren
3438 && TREE_CODE_CLASS (code
) == tcc_comparison
3439 && code1
== TRUTH_NOT_EXPR
3440 && code2
!= TRUTH_NOT_EXPR
3441 /* Avoid warning for !!x == y. */
3442 && (TREE_CODE (arg1
.value
) != NE_EXPR
3443 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
3445 /* Avoid warning for !b == y where b has _Bool type. */
3446 tree t
= integer_zero_node
;
3447 if (TREE_CODE (arg1
.value
) == EQ_EXPR
3448 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
3449 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
3451 t
= TREE_OPERAND (arg1
.value
, 0);
3454 if (TREE_TYPE (t
) != integer_type_node
)
3456 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
3457 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
3458 else if (CONVERT_EXPR_P (t
))
3459 t
= TREE_OPERAND (t
, 0);
3465 if (TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
)
3466 warn_logical_not_parentheses (location
, code
, arg2
.value
);
3469 /* Warn about comparisons against string literals, with the exception
3470 of testing for equality or inequality of a string literal with NULL. */
3471 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3473 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3474 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3475 warning_at (location
, OPT_Waddress
,
3476 "comparison with string literal results in unspecified behavior");
3478 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3479 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3480 warning_at (location
, OPT_Waddress
,
3481 "comparison with string literal results in unspecified behavior");
3483 if (TREE_OVERFLOW_P (result
.value
)
3484 && !TREE_OVERFLOW_P (arg1
.value
)
3485 && !TREE_OVERFLOW_P (arg2
.value
))
3486 overflow_warning (location
, result
.value
);
3488 /* Warn about comparisons of different enum types. */
3489 if (warn_enum_compare
3490 && TREE_CODE_CLASS (code
) == tcc_comparison
3491 && TREE_CODE (type1
) == ENUMERAL_TYPE
3492 && TREE_CODE (type2
) == ENUMERAL_TYPE
3493 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3494 warning_at (location
, OPT_Wenum_compare
,
3495 "comparison between %qT and %qT",
3501 /* Return a tree for the difference of pointers OP0 and OP1.
3502 The resulting tree has type int. */
3505 pointer_diff (location_t loc
, tree op0
, tree op1
)
3507 tree restype
= ptrdiff_type_node
;
3508 tree result
, inttype
;
3510 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3511 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3512 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3513 tree orig_op1
= op1
;
3515 /* If the operands point into different address spaces, we need to
3516 explicitly convert them to pointers into the common address space
3517 before we can subtract the numerical address values. */
3520 addr_space_t as_common
;
3523 /* Determine the common superset address space. This is guaranteed
3524 to exist because the caller verified that comp_target_types
3525 returned non-zero. */
3526 if (!addr_space_superset (as0
, as1
, &as_common
))
3529 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3530 op0
= convert (common_type
, op0
);
3531 op1
= convert (common_type
, op1
);
3534 /* Determine integer type to perform computations in. This will usually
3535 be the same as the result type (ptrdiff_t), but may need to be a wider
3536 type if pointers for the address space are wider than ptrdiff_t. */
3537 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3538 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3542 if (TREE_CODE (target_type
) == VOID_TYPE
)
3543 pedwarn (loc
, OPT_Wpointer_arith
,
3544 "pointer of type %<void *%> used in subtraction");
3545 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3546 pedwarn (loc
, OPT_Wpointer_arith
,
3547 "pointer to a function used in subtraction");
3549 /* First do the subtraction as integers;
3550 then drop through to build the divide operator.
3551 Do not do default conversions on the minus operator
3552 in case restype is a short type. */
3554 op0
= build_binary_op (loc
,
3555 MINUS_EXPR
, convert (inttype
, op0
),
3556 convert (inttype
, op1
), 0);
3557 /* This generates an error if op1 is pointer to incomplete type. */
3558 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3559 error_at (loc
, "arithmetic on pointer to an incomplete type");
3561 op1
= c_size_in_bytes (target_type
);
3563 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3564 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3566 /* Divide by the size, in easiest possible way. */
3567 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3568 op0
, convert (inttype
, op1
));
3570 /* Convert to final result type if necessary. */
3571 return convert (restype
, result
);
3574 /* Expand atomic compound assignments into an approriate sequence as
3575 specified by the C11 standard section 6.5.16.2.
3581 This sequence is used for all types for which these operations are
3584 In addition, built-in versions of the 'fe' prefixed routines may
3585 need to be invoked for floating point (real, complex or vector) when
3586 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3596 __atomic_load (addr, &old, SEQ_CST);
3597 feholdexcept (&fenv);
3599 newval = old op val;
3600 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3603 feclearexcept (FE_ALL_EXCEPT);
3606 feupdateenv (&fenv);
3608 Also note that the compiler is simply issuing the generic form of
3609 the atomic operations. This requires temp(s) and has their address
3610 taken. The atomic processing is smart enough to figure out when the
3611 size of an object can utilize a lock-free version, and convert the
3612 built-in call to the appropriate lock-free routine. The optimizers
3613 will then dispose of any temps that are no longer required, and
3614 lock-free implementations are utilized as long as there is target
3615 support for the required size.
3617 If the operator is NOP_EXPR, then this is a simple assignment, and
3618 an __atomic_store is issued to perform the assignment rather than
3623 /* Build an atomic assignment at LOC, expanding into the proper
3624 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3625 the result of the operation, unless RETURN_OLD_P in which case
3626 return the old value of LHS (this is only for postincrement and
3629 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3630 tree rhs
, bool return_old_p
)
3632 tree fndecl
, func_call
;
3633 vec
<tree
, va_gc
> *params
;
3634 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3637 tree stmt
, goto_stmt
;
3638 tree loop_label
, loop_decl
, done_label
, done_decl
;
3640 tree lhs_type
= TREE_TYPE (lhs
);
3641 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, 0);
3642 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3643 tree rhs_type
= TREE_TYPE (rhs
);
3645 gcc_assert (TYPE_ATOMIC (lhs_type
));
3648 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3650 /* Allocate enough vector items for a compare_exchange. */
3651 vec_alloc (params
, 6);
3653 /* Create a compound statement to hold the sequence of statements
3655 compound_stmt
= c_begin_compound_stmt (false);
3657 /* Fold the RHS if it hasn't already been folded. */
3658 if (modifycode
!= NOP_EXPR
)
3659 rhs
= c_fully_fold (rhs
, false, NULL
);
3661 /* Remove the qualifiers for the rest of the expressions and create
3662 the VAL temp variable to hold the RHS. */
3663 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3664 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3665 val
= create_tmp_var_raw (nonatomic_rhs_type
);
3666 TREE_ADDRESSABLE (val
) = 1;
3667 TREE_NO_WARNING (val
) = 1;
3668 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
3670 SET_EXPR_LOCATION (rhs
, loc
);
3673 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3675 if (modifycode
== NOP_EXPR
)
3677 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3678 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, 0);
3679 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3680 params
->quick_push (lhs_addr
);
3681 params
->quick_push (rhs
);
3682 params
->quick_push (seq_cst
);
3683 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3684 add_stmt (func_call
);
3686 /* Finish the compound statement. */
3687 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3689 /* VAL is the value which was stored, return a COMPOUND_STMT of
3690 the statement and that value. */
3691 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3694 /* Create the variables and labels required for the op= form. */
3695 old
= create_tmp_var_raw (nonatomic_lhs_type
);
3696 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, 0);
3697 TREE_ADDRESSABLE (old
) = 1;
3698 TREE_NO_WARNING (old
) = 1;
3700 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
3701 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, 0);
3702 TREE_ADDRESSABLE (newval
) = 1;
3704 loop_decl
= create_artificial_label (loc
);
3705 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
3707 done_decl
= create_artificial_label (loc
);
3708 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
3710 /* __atomic_load (addr, &old, SEQ_CST). */
3711 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
3712 params
->quick_push (lhs_addr
);
3713 params
->quick_push (old_addr
);
3714 params
->quick_push (seq_cst
);
3715 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3716 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
3719 params
->truncate (0);
3721 /* Create the expressions for floating-point environment
3722 manipulation, if required. */
3723 bool need_fenv
= (flag_trapping_math
3724 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
3725 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
3727 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
3730 add_stmt (hold_call
);
3733 add_stmt (loop_label
);
3735 /* newval = old + val; */
3736 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
3737 rhs
= c_fully_fold (rhs
, false, NULL
);
3738 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
3739 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
3741 if (rhs
!= error_mark_node
)
3743 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
3745 SET_EXPR_LOCATION (rhs
, loc
);
3749 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3751 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
3752 params
->quick_push (lhs_addr
);
3753 params
->quick_push (old_addr
);
3754 params
->quick_push (newval_addr
);
3755 params
->quick_push (integer_zero_node
);
3756 params
->quick_push (seq_cst
);
3757 params
->quick_push (seq_cst
);
3758 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3760 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
3761 SET_EXPR_LOCATION (goto_stmt
, loc
);
3763 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
3764 SET_EXPR_LOCATION (stmt
, loc
);
3768 add_stmt (clear_call
);
3771 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
3772 SET_EXPR_LOCATION (goto_stmt
, loc
);
3773 add_stmt (goto_stmt
);
3776 add_stmt (done_label
);
3779 add_stmt (update_call
);
3781 /* Finish the compound statement. */
3782 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3784 /* NEWVAL is the value that was successfully stored, return a
3785 COMPOUND_EXPR of the statement and the appropriate value. */
3786 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
3787 return_old_p
? old
: newval
);
3790 /* Construct and perhaps optimize a tree representation
3791 for a unary operation. CODE, a tree_code, specifies the operation
3792 and XARG is the operand.
3793 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3794 the default promotions (such as from short to int).
3795 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3796 allows non-lvalues; this is only used to handle conversion of non-lvalue
3797 arrays to pointers in C99.
3799 LOCATION is the location of the operator. */
3802 build_unary_op (location_t location
,
3803 enum tree_code code
, tree xarg
, int flag
)
3805 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3808 enum tree_code typecode
;
3810 tree ret
= error_mark_node
;
3811 tree eptype
= NULL_TREE
;
3812 int noconvert
= flag
;
3813 const char *invalid_op_diag
;
3816 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3818 arg
= remove_c_maybe_const_expr (arg
);
3820 if (code
!= ADDR_EXPR
)
3821 arg
= require_complete_type (arg
);
3823 typecode
= TREE_CODE (TREE_TYPE (arg
));
3824 if (typecode
== ERROR_MARK
)
3825 return error_mark_node
;
3826 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3827 typecode
= INTEGER_TYPE
;
3829 if ((invalid_op_diag
3830 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3832 error_at (location
, invalid_op_diag
);
3833 return error_mark_node
;
3836 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3838 eptype
= TREE_TYPE (arg
);
3839 arg
= TREE_OPERAND (arg
, 0);
3845 /* This is used for unary plus, because a CONVERT_EXPR
3846 is enough to prevent anybody from looking inside for
3847 associativity, but won't generate any code. */
3848 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3849 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3850 || typecode
== VECTOR_TYPE
))
3852 error_at (location
, "wrong type argument to unary plus");
3853 return error_mark_node
;
3855 else if (!noconvert
)
3856 arg
= default_conversion (arg
);
3857 arg
= non_lvalue_loc (location
, arg
);
3861 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3862 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3863 || typecode
== VECTOR_TYPE
))
3865 error_at (location
, "wrong type argument to unary minus");
3866 return error_mark_node
;
3868 else if (!noconvert
)
3869 arg
= default_conversion (arg
);
3873 /* ~ works on integer types and non float vectors. */
3874 if (typecode
== INTEGER_TYPE
3875 || (typecode
== VECTOR_TYPE
3876 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3879 arg
= default_conversion (arg
);
3881 else if (typecode
== COMPLEX_TYPE
)
3884 pedwarn (location
, OPT_Wpedantic
,
3885 "ISO C does not support %<~%> for complex conjugation");
3887 arg
= default_conversion (arg
);
3891 error_at (location
, "wrong type argument to bit-complement");
3892 return error_mark_node
;
3897 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3899 error_at (location
, "wrong type argument to abs");
3900 return error_mark_node
;
3902 else if (!noconvert
)
3903 arg
= default_conversion (arg
);
3907 /* Conjugating a real value is a no-op, but allow it anyway. */
3908 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3909 || typecode
== COMPLEX_TYPE
))
3911 error_at (location
, "wrong type argument to conjugation");
3912 return error_mark_node
;
3914 else if (!noconvert
)
3915 arg
= default_conversion (arg
);
3918 case TRUTH_NOT_EXPR
:
3919 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3920 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3921 && typecode
!= COMPLEX_TYPE
)
3924 "wrong type argument to unary exclamation mark");
3925 return error_mark_node
;
3929 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
3930 arg
= remove_c_maybe_const_expr (arg
);
3933 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3934 ret
= invert_truthvalue_loc (location
, arg
);
3935 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3936 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3937 location
= EXPR_LOCATION (ret
);
3938 goto return_build_unary_op
;
3942 ret
= build_real_imag_expr (location
, code
, arg
);
3943 if (ret
== error_mark_node
)
3944 return error_mark_node
;
3945 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3946 eptype
= TREE_TYPE (eptype
);
3947 goto return_build_unary_op
;
3949 case PREINCREMENT_EXPR
:
3950 case POSTINCREMENT_EXPR
:
3951 case PREDECREMENT_EXPR
:
3952 case POSTDECREMENT_EXPR
:
3954 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3956 tree inner
= build_unary_op (location
, code
,
3957 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3958 if (inner
== error_mark_node
)
3959 return error_mark_node
;
3960 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3961 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3962 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3963 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3964 goto return_build_unary_op
;
3967 /* Complain about anything that is not a true lvalue. In
3968 Objective-C, skip this check for property_refs. */
3969 if (!objc_is_property_ref (arg
)
3970 && !lvalue_or_else (location
,
3971 arg
, ((code
== PREINCREMENT_EXPR
3972 || code
== POSTINCREMENT_EXPR
)
3975 return error_mark_node
;
3977 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
3979 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3980 warning_at (location
, OPT_Wc___compat
,
3981 "increment of enumeration value is invalid in C++");
3983 warning_at (location
, OPT_Wc___compat
,
3984 "decrement of enumeration value is invalid in C++");
3987 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3988 arg
= c_fully_fold (arg
, false, NULL
);
3991 atomic_op
= really_atomic_lvalue (arg
);
3993 /* Increment or decrement the real part of the value,
3994 and don't change the imaginary part. */
3995 if (typecode
== COMPLEX_TYPE
)
3999 pedwarn (location
, OPT_Wpedantic
,
4000 "ISO C does not support %<++%> and %<--%> on complex types");
4004 arg
= stabilize_reference (arg
);
4005 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
4006 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
4007 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
4008 if (real
== error_mark_node
|| imag
== error_mark_node
)
4009 return error_mark_node
;
4010 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4012 goto return_build_unary_op
;
4016 /* Report invalid types. */
4018 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4019 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4020 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
4022 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4023 error_at (location
, "wrong type argument to increment");
4025 error_at (location
, "wrong type argument to decrement");
4027 return error_mark_node
;
4033 argtype
= TREE_TYPE (arg
);
4035 /* Compute the increment. */
4037 if (typecode
== POINTER_TYPE
)
4039 /* If pointer target is an incomplete type,
4040 we just cannot know how to do the arithmetic. */
4041 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4043 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4045 "increment of pointer to an incomplete type %qT",
4046 TREE_TYPE (argtype
));
4049 "decrement of pointer to an incomplete type %qT",
4050 TREE_TYPE (argtype
));
4052 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4053 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4055 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4056 pedwarn (location
, OPT_Wpointer_arith
,
4057 "wrong type argument to increment");
4059 pedwarn (location
, OPT_Wpointer_arith
,
4060 "wrong type argument to decrement");
4063 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4064 inc
= convert_to_ptrofftype_loc (location
, inc
);
4066 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4068 /* For signed fract types, we invert ++ to -- or
4069 -- to ++, and change inc from 1 to -1, because
4070 it is not possible to represent 1 in signed fract constants.
4071 For unsigned fract types, the result always overflows and
4072 we get an undefined (original) or the maximum value. */
4073 if (code
== PREINCREMENT_EXPR
)
4074 code
= PREDECREMENT_EXPR
;
4075 else if (code
== PREDECREMENT_EXPR
)
4076 code
= PREINCREMENT_EXPR
;
4077 else if (code
== POSTINCREMENT_EXPR
)
4078 code
= POSTDECREMENT_EXPR
;
4079 else /* code == POSTDECREMENT_EXPR */
4080 code
= POSTINCREMENT_EXPR
;
4082 inc
= integer_minus_one_node
;
4083 inc
= convert (argtype
, inc
);
4087 inc
= VECTOR_TYPE_P (argtype
)
4088 ? build_one_cst (argtype
)
4090 inc
= convert (argtype
, inc
);
4093 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4094 need to ask Objective-C to build the increment or decrement
4095 expression for it. */
4096 if (objc_is_property_ref (arg
))
4097 return objc_build_incr_expr_for_property_ref (location
, code
,
4100 /* Report a read-only lvalue. */
4101 if (TYPE_READONLY (argtype
))
4103 readonly_error (location
, arg
,
4104 ((code
== PREINCREMENT_EXPR
4105 || code
== POSTINCREMENT_EXPR
)
4106 ? lv_increment
: lv_decrement
));
4107 return error_mark_node
;
4109 else if (TREE_READONLY (arg
))
4110 readonly_warning (arg
,
4111 ((code
== PREINCREMENT_EXPR
4112 || code
== POSTINCREMENT_EXPR
)
4113 ? lv_increment
: lv_decrement
));
4115 /* If the argument is atomic, use the special code sequences for
4116 atomic compound assignment. */
4119 arg
= stabilize_reference (arg
);
4120 ret
= build_atomic_assign (location
, arg
,
4121 ((code
== PREINCREMENT_EXPR
4122 || code
== POSTINCREMENT_EXPR
)
4125 (FRACT_MODE_P (TYPE_MODE (argtype
))
4127 : integer_one_node
),
4128 (code
== POSTINCREMENT_EXPR
4129 || code
== POSTDECREMENT_EXPR
));
4130 goto return_build_unary_op
;
4133 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4134 val
= boolean_increment (code
, arg
);
4136 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4137 TREE_SIDE_EFFECTS (val
) = 1;
4138 if (TREE_CODE (val
) != code
)
4139 TREE_NO_WARNING (val
) = 1;
4141 goto return_build_unary_op
;
4145 /* Note that this operation never does default_conversion. */
4147 /* The operand of unary '&' must be an lvalue (which excludes
4148 expressions of type void), or, in C99, the result of a [] or
4149 unary '*' operator. */
4150 if (VOID_TYPE_P (TREE_TYPE (arg
))
4151 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4152 && (TREE_CODE (arg
) != INDIRECT_REF
4154 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4156 /* Let &* cancel out to simplify resulting code. */
4157 if (TREE_CODE (arg
) == INDIRECT_REF
)
4159 /* Don't let this be an lvalue. */
4160 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4161 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4162 ret
= TREE_OPERAND (arg
, 0);
4163 goto return_build_unary_op
;
4166 /* For &x[y], return x+y */
4167 if (TREE_CODE (arg
) == ARRAY_REF
)
4169 tree op0
= TREE_OPERAND (arg
, 0);
4170 if (!c_mark_addressable (op0
))
4171 return error_mark_node
;
4174 /* Anything not already handled and not a true memory reference
4175 or a non-lvalue array is an error. */
4176 else if (typecode
!= FUNCTION_TYPE
&& !flag
4177 && !lvalue_or_else (location
, arg
, lv_addressof
))
4178 return error_mark_node
;
4180 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4182 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4184 tree inner
= build_unary_op (location
, code
,
4185 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
4186 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4187 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4188 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4189 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4190 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4191 goto return_build_unary_op
;
4194 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4195 argtype
= TREE_TYPE (arg
);
4197 /* If the lvalue is const or volatile, merge that into the type
4198 to which the address will point. This is only needed
4199 for function types. */
4200 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4201 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4202 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4204 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4205 int quals
= orig_quals
;
4207 if (TREE_READONLY (arg
))
4208 quals
|= TYPE_QUAL_CONST
;
4209 if (TREE_THIS_VOLATILE (arg
))
4210 quals
|= TYPE_QUAL_VOLATILE
;
4212 argtype
= c_build_qualified_type (argtype
, quals
);
4215 if (!c_mark_addressable (arg
))
4216 return error_mark_node
;
4218 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4219 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4221 argtype
= build_pointer_type (argtype
);
4223 /* ??? Cope with user tricks that amount to offsetof. Delete this
4224 when we have proper support for integer constant expressions. */
4225 val
= get_base_address (arg
);
4226 if (val
&& TREE_CODE (val
) == INDIRECT_REF
4227 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4229 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4230 goto return_build_unary_op
;
4233 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4236 goto return_build_unary_op
;
4243 argtype
= TREE_TYPE (arg
);
4244 if (TREE_CODE (arg
) == INTEGER_CST
)
4245 ret
= (require_constant_value
4246 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4247 : fold_build1_loc (location
, code
, argtype
, arg
));
4249 ret
= build1 (code
, argtype
, arg
);
4250 return_build_unary_op
:
4251 gcc_assert (ret
!= error_mark_node
);
4252 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4253 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4254 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4255 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4256 ret
= note_integer_operands (ret
);
4258 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4259 protected_set_expr_location (ret
, location
);
4263 /* Return nonzero if REF is an lvalue valid for this language.
4264 Lvalues can be assigned, unless their type has TYPE_READONLY.
4265 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4268 lvalue_p (const_tree ref
)
4270 const enum tree_code code
= TREE_CODE (ref
);
4277 return lvalue_p (TREE_OPERAND (ref
, 0));
4279 case C_MAYBE_CONST_EXPR
:
4280 return lvalue_p (TREE_OPERAND (ref
, 1));
4282 case COMPOUND_LITERAL_EXPR
:
4288 case ARRAY_NOTATION_REF
:
4293 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4294 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4297 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4304 /* Give a warning for storing in something that is read-only in GCC
4305 terms but not const in ISO C terms. */
4308 readonly_warning (tree arg
, enum lvalue_use use
)
4313 warning (0, "assignment of read-only location %qE", arg
);
4316 warning (0, "increment of read-only location %qE", arg
);
4319 warning (0, "decrement of read-only location %qE", arg
);
4328 /* Return nonzero if REF is an lvalue valid for this language;
4329 otherwise, print an error message and return zero. USE says
4330 how the lvalue is being used and so selects the error message.
4331 LOCATION is the location at which any error should be reported. */
4334 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4336 int win
= lvalue_p (ref
);
4339 lvalue_error (loc
, use
);
4344 /* Mark EXP saying that we need to be able to take the
4345 address of it; it should not be allocated in a register.
4346 Returns true if successful. */
4349 c_mark_addressable (tree exp
)
4354 switch (TREE_CODE (x
))
4357 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
4360 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
4364 /* ... fall through ... */
4370 x
= TREE_OPERAND (x
, 0);
4373 case COMPOUND_LITERAL_EXPR
:
4375 TREE_ADDRESSABLE (x
) = 1;
4382 if (C_DECL_REGISTER (x
)
4383 && DECL_NONLOCAL (x
))
4385 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4388 ("global register variable %qD used in nested function", x
);
4391 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4393 else if (C_DECL_REGISTER (x
))
4395 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4396 error ("address of global register variable %qD requested", x
);
4398 error ("address of register variable %qD requested", x
);
4404 TREE_ADDRESSABLE (x
) = 1;
4411 /* Convert EXPR to TYPE, warning about conversion problems with
4412 constants. SEMANTIC_TYPE is the type this conversion would use
4413 without excess precision. If SEMANTIC_TYPE is NULL, this function
4414 is equivalent to convert_and_check. This function is a wrapper that
4415 handles conversions that may be different than
4416 the usual ones because of excess precision. */
4419 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4422 if (TREE_TYPE (expr
) == type
)
4426 return convert_and_check (loc
, type
, expr
);
4428 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4429 && TREE_TYPE (expr
) != semantic_type
)
4431 /* For integers, we need to check the real conversion, not
4432 the conversion to the excess precision type. */
4433 expr
= convert_and_check (loc
, semantic_type
, expr
);
4435 /* Result type is the excess precision type, which should be
4436 large enough, so do not check. */
4437 return convert (type
, expr
);
4440 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4441 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4442 if folded to an integer constant then the unselected half may
4443 contain arbitrary operations not normally permitted in constant
4444 expressions. Set the location of the expression to LOC. */
4447 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4448 tree op1
, tree op1_original_type
, tree op2
,
4449 tree op2_original_type
)
4453 enum tree_code code1
;
4454 enum tree_code code2
;
4455 tree result_type
= NULL
;
4456 tree semantic_result_type
= NULL
;
4457 tree orig_op1
= op1
, orig_op2
= op2
;
4458 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4459 bool ifexp_int_operands
;
4462 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4463 if (op1_int_operands
)
4464 op1
= remove_c_maybe_const_expr (op1
);
4465 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4466 if (op2_int_operands
)
4467 op2
= remove_c_maybe_const_expr (op2
);
4468 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4469 if (ifexp_int_operands
)
4470 ifexp
= remove_c_maybe_const_expr (ifexp
);
4472 /* Promote both alternatives. */
4474 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4475 op1
= default_conversion (op1
);
4476 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4477 op2
= default_conversion (op2
);
4479 if (TREE_CODE (ifexp
) == ERROR_MARK
4480 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4481 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4482 return error_mark_node
;
4484 type1
= TREE_TYPE (op1
);
4485 code1
= TREE_CODE (type1
);
4486 type2
= TREE_TYPE (op2
);
4487 code2
= TREE_CODE (type2
);
4489 /* C90 does not permit non-lvalue arrays in conditional expressions.
4490 In C99 they will be pointers by now. */
4491 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4493 error_at (colon_loc
, "non-lvalue array in conditional expression");
4494 return error_mark_node
;
4497 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4498 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4499 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4500 || code1
== COMPLEX_TYPE
)
4501 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4502 || code2
== COMPLEX_TYPE
))
4504 semantic_result_type
= c_common_type (type1
, type2
);
4505 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4507 op1
= TREE_OPERAND (op1
, 0);
4508 type1
= TREE_TYPE (op1
);
4509 gcc_assert (TREE_CODE (type1
) == code1
);
4511 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4513 op2
= TREE_OPERAND (op2
, 0);
4514 type2
= TREE_TYPE (op2
);
4515 gcc_assert (TREE_CODE (type2
) == code2
);
4519 if (warn_cxx_compat
)
4521 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4522 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4524 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4525 && TREE_CODE (t2
) == ENUMERAL_TYPE
4526 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4527 warning_at (colon_loc
, OPT_Wc___compat
,
4528 ("different enum types in conditional is "
4529 "invalid in C++: %qT vs %qT"),
4533 /* Quickly detect the usual case where op1 and op2 have the same type
4535 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4538 result_type
= type1
;
4540 result_type
= TYPE_MAIN_VARIANT (type1
);
4542 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4543 || code1
== COMPLEX_TYPE
)
4544 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4545 || code2
== COMPLEX_TYPE
))
4547 result_type
= c_common_type (type1
, type2
);
4548 do_warn_double_promotion (result_type
, type1
, type2
,
4549 "implicit conversion from %qT to %qT to "
4550 "match other result of conditional",
4553 /* If -Wsign-compare, warn here if type1 and type2 have
4554 different signedness. We'll promote the signed to unsigned
4555 and later code won't know it used to be different.
4556 Do this check on the original types, so that explicit casts
4557 will be considered, but default promotions won't. */
4558 if (c_inhibit_evaluation_warnings
== 0)
4560 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4561 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4563 if (unsigned_op1
^ unsigned_op2
)
4567 /* Do not warn if the result type is signed, since the
4568 signed type will only be chosen if it can represent
4569 all the values of the unsigned type. */
4570 if (!TYPE_UNSIGNED (result_type
))
4574 bool op1_maybe_const
= true;
4575 bool op2_maybe_const
= true;
4577 /* Do not warn if the signed quantity is an
4578 unsuffixed integer literal (or some static
4579 constant expression involving such literals) and
4580 it is non-negative. This warning requires the
4581 operands to be folded for best results, so do
4582 that folding in this case even without
4583 warn_sign_compare to avoid warning options
4584 possibly affecting code generation. */
4585 c_inhibit_evaluation_warnings
4586 += (ifexp
== truthvalue_false_node
);
4587 op1
= c_fully_fold (op1
, require_constant_value
,
4589 c_inhibit_evaluation_warnings
4590 -= (ifexp
== truthvalue_false_node
);
4592 c_inhibit_evaluation_warnings
4593 += (ifexp
== truthvalue_true_node
);
4594 op2
= c_fully_fold (op2
, require_constant_value
,
4596 c_inhibit_evaluation_warnings
4597 -= (ifexp
== truthvalue_true_node
);
4599 if (warn_sign_compare
)
4602 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4604 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4607 warning_at (colon_loc
, OPT_Wsign_compare
,
4608 ("signed and unsigned type in "
4609 "conditional expression"));
4611 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4612 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4613 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4614 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4619 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4621 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4622 pedwarn (colon_loc
, OPT_Wpedantic
,
4623 "ISO C forbids conditional expr with only one void side");
4624 result_type
= void_type_node
;
4626 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4628 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
4629 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
4630 addr_space_t as_common
;
4632 if (comp_target_types (colon_loc
, type1
, type2
))
4633 result_type
= common_pointer_type (type1
, type2
);
4634 else if (null_pointer_constant_p (orig_op1
))
4635 result_type
= type2
;
4636 else if (null_pointer_constant_p (orig_op2
))
4637 result_type
= type1
;
4638 else if (!addr_space_superset (as1
, as2
, &as_common
))
4640 error_at (colon_loc
, "pointers to disjoint address spaces "
4641 "used in conditional expression");
4642 return error_mark_node
;
4644 else if (VOID_TYPE_P (TREE_TYPE (type1
))
4645 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
4647 if ((TREE_CODE (TREE_TYPE (type2
)) == ARRAY_TYPE
)
4648 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2
)))
4649 & ~TYPE_QUALS (TREE_TYPE (type1
))))
4650 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
4651 "pointer to array loses qualifier "
4652 "in conditional expression");
4654 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
4655 pedwarn (colon_loc
, OPT_Wpedantic
,
4656 "ISO C forbids conditional expr between "
4657 "%<void *%> and function pointer");
4658 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
4659 TREE_TYPE (type2
)));
4661 else if (VOID_TYPE_P (TREE_TYPE (type2
))
4662 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
4664 if ((TREE_CODE (TREE_TYPE (type1
)) == ARRAY_TYPE
)
4665 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1
)))
4666 & ~TYPE_QUALS (TREE_TYPE (type2
))))
4667 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
4668 "pointer to array loses qualifier "
4669 "in conditional expression");
4671 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
4672 pedwarn (colon_loc
, OPT_Wpedantic
,
4673 "ISO C forbids conditional expr between "
4674 "%<void *%> and function pointer");
4675 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
4676 TREE_TYPE (type1
)));
4678 /* Objective-C pointer comparisons are a bit more lenient. */
4679 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
4680 result_type
= objc_common_type (type1
, type2
);
4683 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
4685 pedwarn (colon_loc
, 0,
4686 "pointer type mismatch in conditional expression");
4687 result_type
= build_pointer_type
4688 (build_qualified_type (void_type_node
, qual
));
4691 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4693 if (!null_pointer_constant_p (orig_op2
))
4694 pedwarn (colon_loc
, 0,
4695 "pointer/integer type mismatch in conditional expression");
4698 op2
= null_pointer_node
;
4700 result_type
= type1
;
4702 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4704 if (!null_pointer_constant_p (orig_op1
))
4705 pedwarn (colon_loc
, 0,
4706 "pointer/integer type mismatch in conditional expression");
4709 op1
= null_pointer_node
;
4711 result_type
= type2
;
4716 if (flag_cond_mismatch
)
4717 result_type
= void_type_node
;
4720 error_at (colon_loc
, "type mismatch in conditional expression");
4721 return error_mark_node
;
4725 /* Merge const and volatile flags of the incoming types. */
4727 = build_type_variant (result_type
,
4728 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4729 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4731 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
4732 semantic_result_type
);
4733 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
4734 semantic_result_type
);
4736 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4738 op2_int_operands
= true;
4739 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4741 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4743 op1_int_operands
= true;
4744 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4746 int_const
= int_operands
= (ifexp_int_operands
4748 && op2_int_operands
);
4751 int_const
= ((ifexp
== truthvalue_true_node
4752 && TREE_CODE (orig_op1
) == INTEGER_CST
4753 && !TREE_OVERFLOW (orig_op1
))
4754 || (ifexp
== truthvalue_false_node
4755 && TREE_CODE (orig_op2
) == INTEGER_CST
4756 && !TREE_OVERFLOW (orig_op2
)));
4758 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4759 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4764 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4765 nested inside of the expression. */
4766 op1
= c_fully_fold (op1
, false, NULL
);
4767 op2
= c_fully_fold (op2
, false, NULL
);
4769 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4771 ret
= note_integer_operands (ret
);
4773 if (semantic_result_type
)
4774 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
4776 protected_set_expr_location (ret
, colon_loc
);
4780 /* Return a compound expression that performs two expressions and
4781 returns the value of the second of them.
4783 LOC is the location of the COMPOUND_EXPR. */
4786 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4788 bool expr1_int_operands
, expr2_int_operands
;
4789 tree eptype
= NULL_TREE
;
4793 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
4794 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
4797 "spawned function call cannot be part of a comma expression");
4798 return error_mark_node
;
4800 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4801 if (expr1_int_operands
)
4802 expr1
= remove_c_maybe_const_expr (expr1
);
4803 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4804 if (expr2_int_operands
)
4805 expr2
= remove_c_maybe_const_expr (expr2
);
4807 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4808 expr1
= TREE_OPERAND (expr1
, 0);
4809 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4811 eptype
= TREE_TYPE (expr2
);
4812 expr2
= TREE_OPERAND (expr2
, 0);
4815 if (!TREE_SIDE_EFFECTS (expr1
))
4817 /* The left-hand operand of a comma expression is like an expression
4818 statement: with -Wunused, we should warn if it doesn't have
4819 any side-effects, unless it was explicitly cast to (void). */
4820 if (warn_unused_value
)
4822 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4823 && CONVERT_EXPR_P (expr1
))
4825 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4826 && TREE_CODE (expr1
) == COMPOUND_EXPR
4827 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4828 ; /* (void) a, (void) b, c */
4830 warning_at (loc
, OPT_Wunused_value
,
4831 "left-hand operand of comma expression has no effect");
4834 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
4835 && warn_unused_value
)
4838 location_t cloc
= loc
;
4839 while (TREE_CODE (r
) == COMPOUND_EXPR
)
4841 if (EXPR_HAS_LOCATION (r
))
4842 cloc
= EXPR_LOCATION (r
);
4843 r
= TREE_OPERAND (r
, 1);
4845 if (!TREE_SIDE_EFFECTS (r
)
4846 && !VOID_TYPE_P (TREE_TYPE (r
))
4847 && !CONVERT_EXPR_P (r
))
4848 warning_at (cloc
, OPT_Wunused_value
,
4849 "right-hand operand of comma expression has no effect");
4852 /* With -Wunused, we should also warn if the left-hand operand does have
4853 side-effects, but computes a value which is not used. For example, in
4854 `foo() + bar(), baz()' the result of the `+' operator is not used,
4855 so we should issue a warning. */
4856 else if (warn_unused_value
)
4857 warn_if_unused_value (expr1
, loc
);
4859 if (expr2
== error_mark_node
)
4860 return error_mark_node
;
4862 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4865 && expr1_int_operands
4866 && expr2_int_operands
)
4867 ret
= note_integer_operands (ret
);
4870 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4872 protected_set_expr_location (ret
, loc
);
4876 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4877 which we are casting. OTYPE is the type of the expression being
4878 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4879 of the cast. -Wcast-qual appeared on the command line. Named
4880 address space qualifiers are not handled here, because they result
4881 in different warnings. */
4884 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
4886 tree in_type
= type
;
4887 tree in_otype
= otype
;
4892 /* Check that the qualifiers on IN_TYPE are a superset of the
4893 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4894 nodes is uninteresting and we stop as soon as we hit a
4895 non-POINTER_TYPE node on either type. */
4898 in_otype
= TREE_TYPE (in_otype
);
4899 in_type
= TREE_TYPE (in_type
);
4901 /* GNU C allows cv-qualified function types. 'const' means the
4902 function is very pure, 'volatile' means it can't return. We
4903 need to warn when such qualifiers are added, not when they're
4905 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4906 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4907 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
4908 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
4910 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
4911 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
4913 while (TREE_CODE (in_type
) == POINTER_TYPE
4914 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4917 warning_at (loc
, OPT_Wcast_qual
,
4918 "cast adds %q#v qualifier to function type", added
);
4921 /* There are qualifiers present in IN_OTYPE that are not present
4923 warning_at (loc
, OPT_Wcast_qual
,
4924 "cast discards %qv qualifier from pointer target type",
4927 if (added
|| discarded
)
4930 /* A cast from **T to const **T is unsafe, because it can cause a
4931 const value to be changed with no additional warning. We only
4932 issue this warning if T is the same on both sides, and we only
4933 issue the warning if there are the same number of pointers on
4934 both sides, as otherwise the cast is clearly unsafe anyhow. A
4935 cast is unsafe when a qualifier is added at one level and const
4936 is not present at all outer levels.
4938 To issue this warning, we check at each level whether the cast
4939 adds new qualifiers not already seen. We don't need to special
4940 case function types, as they won't have the same
4941 TYPE_MAIN_VARIANT. */
4943 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4945 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4950 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4953 in_type
= TREE_TYPE (in_type
);
4954 in_otype
= TREE_TYPE (in_otype
);
4955 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4958 warning_at (loc
, OPT_Wcast_qual
,
4959 "to be safe all intermediate pointers in cast from "
4960 "%qT to %qT must be %<const%> qualified",
4965 is_const
= TYPE_READONLY (in_type
);
4967 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4970 /* Build an expression representing a cast to type TYPE of expression EXPR.
4971 LOC is the location of the cast-- typically the open paren of the cast. */
4974 build_c_cast (location_t loc
, tree type
, tree expr
)
4978 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4979 expr
= TREE_OPERAND (expr
, 0);
4983 if (type
== error_mark_node
|| expr
== error_mark_node
)
4984 return error_mark_node
;
4986 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4987 only in <protocol> qualifications. But when constructing cast expressions,
4988 the protocols do matter and must be kept around. */
4989 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
4990 return build1 (NOP_EXPR
, type
, expr
);
4992 type
= TYPE_MAIN_VARIANT (type
);
4994 if (TREE_CODE (type
) == ARRAY_TYPE
)
4996 error_at (loc
, "cast specifies array type");
4997 return error_mark_node
;
5000 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5002 error_at (loc
, "cast specifies function type");
5003 return error_mark_node
;
5006 if (!VOID_TYPE_P (type
))
5008 value
= require_complete_type (value
);
5009 if (value
== error_mark_node
)
5010 return error_mark_node
;
5013 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
5015 if (TREE_CODE (type
) == RECORD_TYPE
5016 || TREE_CODE (type
) == UNION_TYPE
)
5017 pedwarn (loc
, OPT_Wpedantic
,
5018 "ISO C forbids casting nonscalar to the same type");
5020 /* Convert to remove any qualifiers from VALUE's type. */
5021 value
= convert (type
, value
);
5023 else if (TREE_CODE (type
) == UNION_TYPE
)
5027 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5028 if (TREE_TYPE (field
) != error_mark_node
5029 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
5030 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
5036 bool maybe_const
= true;
5038 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
5039 t
= c_fully_fold (value
, false, &maybe_const
);
5040 t
= build_constructor_single (type
, field
, t
);
5042 t
= c_wrap_maybe_const (t
, true);
5043 t
= digest_init (loc
, type
, t
,
5044 NULL_TREE
, false, true, 0);
5045 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
5048 error_at (loc
, "cast to union type from type not present in union");
5049 return error_mark_node
;
5055 if (type
== void_type_node
)
5057 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5058 SET_EXPR_LOCATION (t
, loc
);
5062 otype
= TREE_TYPE (value
);
5064 /* Optionally warn about potentially worrisome casts. */
5066 && TREE_CODE (type
) == POINTER_TYPE
5067 && TREE_CODE (otype
) == POINTER_TYPE
)
5068 handle_warn_cast_qual (loc
, type
, otype
);
5070 /* Warn about conversions between pointers to disjoint
5072 if (TREE_CODE (type
) == POINTER_TYPE
5073 && TREE_CODE (otype
) == POINTER_TYPE
5074 && !null_pointer_constant_p (value
))
5076 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5077 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5078 addr_space_t as_common
;
5080 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5082 if (ADDR_SPACE_GENERIC_P (as_from
))
5083 warning_at (loc
, 0, "cast to %s address space pointer "
5084 "from disjoint generic address space pointer",
5085 c_addr_space_name (as_to
));
5087 else if (ADDR_SPACE_GENERIC_P (as_to
))
5088 warning_at (loc
, 0, "cast to generic address space pointer "
5089 "from disjoint %s address space pointer",
5090 c_addr_space_name (as_from
));
5093 warning_at (loc
, 0, "cast to %s address space pointer "
5094 "from disjoint %s address space pointer",
5095 c_addr_space_name (as_to
),
5096 c_addr_space_name (as_from
));
5100 /* Warn about possible alignment problems. */
5101 if (STRICT_ALIGNMENT
5102 && TREE_CODE (type
) == POINTER_TYPE
5103 && TREE_CODE (otype
) == POINTER_TYPE
5104 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5105 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5106 /* Don't warn about opaque types, where the actual alignment
5107 restriction is unknown. */
5108 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
5109 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
5110 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5111 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5112 warning_at (loc
, OPT_Wcast_align
,
5113 "cast increases required alignment of target type");
5115 if (TREE_CODE (type
) == INTEGER_TYPE
5116 && TREE_CODE (otype
) == POINTER_TYPE
5117 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5118 /* Unlike conversion of integers to pointers, where the
5119 warning is disabled for converting constants because
5120 of cases such as SIG_*, warn about converting constant
5121 pointers to integers. In some cases it may cause unwanted
5122 sign extension, and a warning is appropriate. */
5123 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5124 "cast from pointer to integer of different size");
5126 if (TREE_CODE (value
) == CALL_EXPR
5127 && TREE_CODE (type
) != TREE_CODE (otype
))
5128 warning_at (loc
, OPT_Wbad_function_cast
,
5129 "cast from function call of type %qT "
5130 "to non-matching type %qT", otype
, type
);
5132 if (TREE_CODE (type
) == POINTER_TYPE
5133 && TREE_CODE (otype
) == INTEGER_TYPE
5134 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5135 /* Don't warn about converting any constant. */
5136 && !TREE_CONSTANT (value
))
5138 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5139 "of different size");
5141 if (warn_strict_aliasing
<= 2)
5142 strict_aliasing_warning (otype
, type
, expr
);
5144 /* If pedantic, warn for conversions between function and object
5145 pointer types, except for converting a null pointer constant
5146 to function pointer type. */
5148 && TREE_CODE (type
) == POINTER_TYPE
5149 && TREE_CODE (otype
) == POINTER_TYPE
5150 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5151 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5152 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5153 "conversion of function pointer to object pointer type");
5156 && TREE_CODE (type
) == POINTER_TYPE
5157 && TREE_CODE (otype
) == POINTER_TYPE
5158 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5159 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5160 && !null_pointer_constant_p (value
))
5161 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5162 "conversion of object pointer to function pointer type");
5165 value
= convert (type
, value
);
5167 /* Ignore any integer overflow caused by the cast. */
5168 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5170 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5172 if (!TREE_OVERFLOW (value
))
5174 /* Avoid clobbering a shared constant. */
5175 value
= copy_node (value
);
5176 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5179 else if (TREE_OVERFLOW (value
))
5180 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5181 value
= wide_int_to_tree (TREE_TYPE (value
), value
);
5185 /* Don't let a cast be an lvalue. */
5186 if (lvalue_p (value
))
5187 value
= non_lvalue_loc (loc
, value
);
5189 /* Don't allow the results of casting to floating-point or complex
5190 types be confused with actual constants, or casts involving
5191 integer and pointer types other than direct integer-to-integer
5192 and integer-to-pointer be confused with integer constant
5193 expressions and null pointer constants. */
5194 if (TREE_CODE (value
) == REAL_CST
5195 || TREE_CODE (value
) == COMPLEX_CST
5196 || (TREE_CODE (value
) == INTEGER_CST
5197 && !((TREE_CODE (expr
) == INTEGER_CST
5198 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5199 || TREE_CODE (expr
) == REAL_CST
5200 || TREE_CODE (expr
) == COMPLEX_CST
)))
5201 value
= build1 (NOP_EXPR
, type
, value
);
5203 if (CAN_HAVE_LOCATION_P (value
))
5204 SET_EXPR_LOCATION (value
, loc
);
5208 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5209 location of the open paren of the cast, or the position of the cast
5212 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5215 tree type_expr
= NULL_TREE
;
5216 bool type_expr_const
= true;
5218 int saved_wsp
= warn_strict_prototypes
;
5220 /* This avoids warnings about unprototyped casts on
5221 integers. E.g. "#define SIG_DFL (void(*)())0". */
5222 if (TREE_CODE (expr
) == INTEGER_CST
)
5223 warn_strict_prototypes
= 0;
5224 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5225 warn_strict_prototypes
= saved_wsp
;
5227 ret
= build_c_cast (loc
, type
, expr
);
5230 bool inner_expr_const
= true;
5231 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5232 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5233 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5234 && inner_expr_const
);
5235 SET_EXPR_LOCATION (ret
, loc
);
5238 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
5239 SET_EXPR_LOCATION (ret
, loc
);
5241 /* C++ does not permits types to be defined in a cast, but it
5242 allows references to incomplete types. */
5243 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5244 warning_at (loc
, OPT_Wc___compat
,
5245 "defining a type in a cast is invalid in C++");
5250 /* Build an assignment expression of lvalue LHS from value RHS.
5251 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5252 may differ from TREE_TYPE (LHS) for an enum bitfield.
5253 MODIFYCODE is the code for a binary operator that we use
5254 to combine the old value of LHS with RHS to get the new value.
5255 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5256 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5257 which may differ from TREE_TYPE (RHS) for an enum value.
5259 LOCATION is the location of the MODIFYCODE operator.
5260 RHS_LOC is the location of the RHS. */
5263 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5264 enum tree_code modifycode
,
5265 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5269 tree rhseval
= NULL_TREE
;
5270 tree rhs_semantic_type
= NULL_TREE
;
5271 tree lhstype
= TREE_TYPE (lhs
);
5272 tree olhstype
= lhstype
;
5276 /* Types that aren't fully specified cannot be used in assignments. */
5277 lhs
= require_complete_type (lhs
);
5279 /* Avoid duplicate error messages from operands that had errors. */
5280 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5281 return error_mark_node
;
5283 /* Ensure an error for assigning a non-lvalue array to an array in
5285 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5287 error_at (location
, "assignment to expression with array type");
5288 return error_mark_node
;
5291 /* For ObjC properties, defer this check. */
5292 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5293 return error_mark_node
;
5295 is_atomic_op
= really_atomic_lvalue (lhs
);
5297 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5299 rhs_semantic_type
= TREE_TYPE (rhs
);
5300 rhs
= TREE_OPERAND (rhs
, 0);
5305 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5307 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5308 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5310 if (inner
== error_mark_node
)
5311 return error_mark_node
;
5312 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5313 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5314 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5315 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5316 protected_set_expr_location (result
, location
);
5320 /* If a binary op has been requested, combine the old LHS value with the RHS
5321 producing the value we should actually store into the LHS. */
5323 if (modifycode
!= NOP_EXPR
)
5325 lhs
= c_fully_fold (lhs
, false, NULL
);
5326 lhs
= stabilize_reference (lhs
);
5328 /* Construct the RHS for any non-atomic compound assignemnt. */
5331 /* If in LHS op= RHS the RHS has side-effects, ensure they
5332 are preevaluated before the rest of the assignment expression's
5333 side-effects, because RHS could contain e.g. function calls
5335 if (TREE_SIDE_EFFECTS (rhs
))
5337 newrhs
= in_late_binary_op
? save_expr (rhs
) : c_save_expr (rhs
);
5340 newrhs
= build_binary_op (location
,
5341 modifycode
, lhs
, newrhs
, 1);
5343 /* The original type of the right hand side is no longer
5345 rhs_origtype
= NULL_TREE
;
5349 if (c_dialect_objc ())
5351 /* Check if we are modifying an Objective-C property reference;
5352 if so, we need to generate setter calls. */
5353 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5357 /* Else, do the check that we postponed for Objective-C. */
5358 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5359 return error_mark_node
;
5362 /* Give an error for storing in something that is 'const'. */
5364 if (TYPE_READONLY (lhstype
)
5365 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5366 || TREE_CODE (lhstype
) == UNION_TYPE
)
5367 && C_TYPE_FIELDS_READONLY (lhstype
)))
5369 readonly_error (location
, lhs
, lv_assign
);
5370 return error_mark_node
;
5372 else if (TREE_READONLY (lhs
))
5373 readonly_warning (lhs
, lv_assign
);
5375 /* If storing into a structure or union member,
5376 it has probably been given type `int'.
5377 Compute the type that would go with
5378 the actual amount of storage the member occupies. */
5380 if (TREE_CODE (lhs
) == COMPONENT_REF
5381 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5382 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5383 || TREE_CODE (lhstype
) == REAL_TYPE
5384 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5385 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5387 /* If storing in a field that is in actuality a short or narrower than one,
5388 we must store in the field in its actual type. */
5390 if (lhstype
!= TREE_TYPE (lhs
))
5392 lhs
= copy_node (lhs
);
5393 TREE_TYPE (lhs
) = lhstype
;
5396 /* Issue -Wc++-compat warnings about an assignment to an enum type
5397 when LHS does not have its original type. This happens for,
5398 e.g., an enum bitfield in a struct. */
5400 && lhs_origtype
!= NULL_TREE
5401 && lhs_origtype
!= lhstype
5402 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5404 tree checktype
= (rhs_origtype
!= NULL_TREE
5407 if (checktype
!= error_mark_node
5408 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5409 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5410 warning_at (location
, OPT_Wc___compat
,
5411 "enum conversion in assignment is invalid in C++");
5414 /* If the lhs is atomic, remove that qualifier. */
5417 lhstype
= build_qualified_type (lhstype
,
5418 (TYPE_QUALS (lhstype
)
5419 & ~TYPE_QUAL_ATOMIC
));
5420 olhstype
= build_qualified_type (olhstype
,
5421 (TYPE_QUALS (lhstype
)
5422 & ~TYPE_QUAL_ATOMIC
));
5425 /* Convert new value to destination type. Fold it first, then
5426 restore any excess precision information, for the sake of
5427 conversion warnings. */
5429 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5431 npc
= null_pointer_constant_p (newrhs
);
5432 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5433 if (rhs_semantic_type
)
5434 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5435 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
5436 rhs_origtype
, ic_assign
, npc
,
5437 NULL_TREE
, NULL_TREE
, 0);
5438 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5439 return error_mark_node
;
5442 /* Emit ObjC write barrier, if necessary. */
5443 if (c_dialect_objc () && flag_objc_gc
)
5445 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5448 protected_set_expr_location (result
, location
);
5453 /* Scan operands. */
5456 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5459 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5460 TREE_SIDE_EFFECTS (result
) = 1;
5461 protected_set_expr_location (result
, location
);
5464 /* If we got the LHS in a different type for storing in,
5465 convert the result back to the nominal type of LHS
5466 so that the value we return always has the same type
5467 as the LHS argument. */
5469 if (olhstype
== TREE_TYPE (result
))
5472 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
5473 rhs_origtype
, ic_assign
, false, NULL_TREE
,
5475 protected_set_expr_location (result
, location
);
5479 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
5483 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5484 This is used to implement -fplan9-extensions. */
5487 find_anonymous_field_with_type (tree struct_type
, tree type
)
5492 gcc_assert (TREE_CODE (struct_type
) == RECORD_TYPE
5493 || TREE_CODE (struct_type
) == UNION_TYPE
);
5495 for (field
= TYPE_FIELDS (struct_type
);
5497 field
= TREE_CHAIN (field
))
5499 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5500 ? c_build_qualified_type (TREE_TYPE (field
),
5502 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5503 if (DECL_NAME (field
) == NULL
5504 && comptypes (type
, fieldtype
))
5510 else if (DECL_NAME (field
) == NULL
5511 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
5512 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5513 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5523 /* RHS is an expression whose type is pointer to struct. If there is
5524 an anonymous field in RHS with type TYPE, then return a pointer to
5525 that field in RHS. This is used with -fplan9-extensions. This
5526 returns NULL if no conversion could be found. */
5529 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5531 tree rhs_struct_type
, lhs_main_type
;
5532 tree field
, found_field
;
5533 bool found_sub_field
;
5536 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5537 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5538 gcc_assert (TREE_CODE (rhs_struct_type
) == RECORD_TYPE
5539 || TREE_CODE (rhs_struct_type
) == UNION_TYPE
);
5541 gcc_assert (POINTER_TYPE_P (type
));
5542 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5543 ? c_build_qualified_type (TREE_TYPE (type
),
5545 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5547 found_field
= NULL_TREE
;
5548 found_sub_field
= false;
5549 for (field
= TYPE_FIELDS (rhs_struct_type
);
5551 field
= TREE_CHAIN (field
))
5553 if (DECL_NAME (field
) != NULL_TREE
5554 || (TREE_CODE (TREE_TYPE (field
)) != RECORD_TYPE
5555 && TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
))
5557 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5558 ? c_build_qualified_type (TREE_TYPE (field
),
5560 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5561 if (comptypes (lhs_main_type
, fieldtype
))
5563 if (found_field
!= NULL_TREE
)
5565 found_field
= field
;
5567 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5570 if (found_field
!= NULL_TREE
)
5572 found_field
= field
;
5573 found_sub_field
= true;
5577 if (found_field
== NULL_TREE
)
5580 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5581 build_fold_indirect_ref (rhs
), found_field
,
5583 ret
= build_fold_addr_expr_loc (location
, ret
);
5585 if (found_sub_field
)
5587 ret
= convert_to_anonymous_field (location
, type
, ret
);
5588 gcc_assert (ret
!= NULL_TREE
);
5594 /* Issue an error message for a bad initializer component.
5595 GMSGID identifies the message.
5596 The component name is taken from the spelling stack. */
5599 error_init (location_t loc
, const char *gmsgid
)
5603 /* The gmsgid may be a format string with %< and %>. */
5604 error_at (loc
, gmsgid
);
5605 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5607 inform (loc
, "(near initialization for %qs)", ofwhat
);
5610 /* Issue a pedantic warning for a bad initializer component. OPT is
5611 the option OPT_* (from options.h) controlling this warning or 0 if
5612 it is unconditionally given. GMSGID identifies the message. The
5613 component name is taken from the spelling stack. */
5616 pedwarn_init (location_t location
, int opt
, const char *gmsgid
)
5621 /* The gmsgid may be a format string with %< and %>. */
5622 warned
= pedwarn (location
, opt
, gmsgid
);
5623 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5624 if (*ofwhat
&& warned
)
5625 inform (location
, "(near initialization for %qs)", ofwhat
);
5628 /* Issue a warning for a bad initializer component.
5630 OPT is the OPT_W* value corresponding to the warning option that
5631 controls this warning. GMSGID identifies the message. The
5632 component name is taken from the spelling stack. */
5635 warning_init (location_t loc
, int opt
, const char *gmsgid
)
5640 /* The gmsgid may be a format string with %< and %>. */
5641 warned
= warning_at (loc
, opt
, gmsgid
);
5642 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5643 if (*ofwhat
&& warned
)
5644 inform (loc
, "(near initialization for %qs)", ofwhat
);
5647 /* If TYPE is an array type and EXPR is a parenthesized string
5648 constant, warn if pedantic that EXPR is being used to initialize an
5649 object of type TYPE. */
5652 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
5655 && TREE_CODE (type
) == ARRAY_TYPE
5656 && TREE_CODE (expr
.value
) == STRING_CST
5657 && expr
.original_code
!= STRING_CST
)
5658 pedwarn_init (loc
, OPT_Wpedantic
,
5659 "array initialized from parenthesized string constant");
5662 /* Convert value RHS to type TYPE as preparation for an assignment to
5663 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5664 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5665 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5666 constant before any folding.
5667 The real work of conversion is done by `convert'.
5668 The purpose of this function is to generate error messages
5669 for assignments that are not allowed in C.
5670 ERRTYPE says whether it is argument passing, assignment,
5671 initialization or return.
5673 LOCATION is the location of the assignment, EXPR_LOC is the location of
5674 the RHS or, for a function, location of an argument.
5675 FUNCTION is a tree for the function being called.
5676 PARMNUM is the number of the argument, for printing in error messages. */
5679 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
5680 tree rhs
, tree origtype
, enum impl_conv errtype
,
5681 bool null_pointer_constant
, tree fundecl
,
5682 tree function
, int parmnum
)
5684 enum tree_code codel
= TREE_CODE (type
);
5685 tree orig_rhs
= rhs
;
5687 enum tree_code coder
;
5688 tree rname
= NULL_TREE
;
5689 bool objc_ok
= false;
5691 if (errtype
== ic_argpass
)
5694 /* Change pointer to function to the function itself for
5696 if (TREE_CODE (function
) == ADDR_EXPR
5697 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
5698 function
= TREE_OPERAND (function
, 0);
5700 /* Handle an ObjC selector specially for diagnostics. */
5701 selector
= objc_message_selector ();
5703 if (selector
&& parmnum
> 2)
5710 /* This macro is used to emit diagnostics to ensure that all format
5711 strings are complete sentences, visible to gettext and checked at
5713 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5718 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5719 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5720 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5721 "expected %qT but argument is of type %qT", \
5725 pedwarn (LOCATION, OPT, AS); \
5728 pedwarn_init (LOCATION, OPT, IN); \
5731 pedwarn (LOCATION, OPT, RE); \
5734 gcc_unreachable (); \
5738 /* This macro is used to emit diagnostics to ensure that all format
5739 strings are complete sentences, visible to gettext and checked at
5740 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5741 extra parameter to enumerate qualifiers. */
5742 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5747 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5748 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5749 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5750 "expected %qT but argument is of type %qT", \
5754 pedwarn (LOCATION, OPT, AS, QUALS); \
5757 pedwarn (LOCATION, OPT, IN, QUALS); \
5760 pedwarn (LOCATION, OPT, RE, QUALS); \
5763 gcc_unreachable (); \
5767 /* This macro is used to emit diagnostics to ensure that all format
5768 strings are complete sentences, visible to gettext and checked at
5769 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5770 warning_at instead of pedwarn. */
5771 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5776 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5777 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5778 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5779 "expected %qT but argument is of type %qT", \
5783 warning_at (LOCATION, OPT, AS, QUALS); \
5786 warning_at (LOCATION, OPT, IN, QUALS); \
5789 warning_at (LOCATION, OPT, RE, QUALS); \
5792 gcc_unreachable (); \
5796 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5797 rhs
= TREE_OPERAND (rhs
, 0);
5799 rhstype
= TREE_TYPE (rhs
);
5800 coder
= TREE_CODE (rhstype
);
5802 if (coder
== ERROR_MARK
)
5803 return error_mark_node
;
5805 if (c_dialect_objc ())
5828 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
5831 if (warn_cxx_compat
)
5833 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
5834 if (checktype
!= error_mark_node
5835 && TREE_CODE (type
) == ENUMERAL_TYPE
5836 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
5838 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wc___compat
,
5839 G_("enum conversion when passing argument "
5840 "%d of %qE is invalid in C++"),
5841 G_("enum conversion in assignment is "
5843 G_("enum conversion in initialization is "
5845 G_("enum conversion in return is "
5850 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
5853 if (coder
== VOID_TYPE
)
5855 /* Except for passing an argument to an unprototyped function,
5856 this is a constraint violation. When passing an argument to
5857 an unprototyped function, it is compile-time undefined;
5858 making it a constraint in that case was rejected in
5860 error_at (location
, "void value not ignored as it ought to be");
5861 return error_mark_node
;
5863 rhs
= require_complete_type (rhs
);
5864 if (rhs
== error_mark_node
)
5865 return error_mark_node
;
5866 /* A non-reference type can convert to a reference. This handles
5867 va_start, va_copy and possibly port built-ins. */
5868 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
5870 if (!lvalue_p (rhs
))
5872 error_at (location
, "cannot pass rvalue to reference parameter");
5873 return error_mark_node
;
5875 if (!c_mark_addressable (rhs
))
5876 return error_mark_node
;
5877 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
5878 SET_EXPR_LOCATION (rhs
, location
);
5880 rhs
= convert_for_assignment (location
, expr_loc
,
5881 build_pointer_type (TREE_TYPE (type
)),
5882 rhs
, origtype
, errtype
,
5883 null_pointer_constant
, fundecl
, function
,
5885 if (rhs
== error_mark_node
)
5886 return error_mark_node
;
5888 rhs
= build1 (NOP_EXPR
, type
, rhs
);
5889 SET_EXPR_LOCATION (rhs
, location
);
5892 /* Some types can interconvert without explicit casts. */
5893 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
5894 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
5895 return convert (type
, rhs
);
5896 /* Arithmetic types all interconvert, and enum is treated like int. */
5897 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
5898 || codel
== FIXED_POINT_TYPE
5899 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
5900 || codel
== BOOLEAN_TYPE
)
5901 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
5902 || coder
== FIXED_POINT_TYPE
5903 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
5904 || coder
== BOOLEAN_TYPE
))
5907 bool save
= in_late_binary_op
;
5908 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
5909 || (coder
== REAL_TYPE
5910 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
5911 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
5912 in_late_binary_op
= true;
5913 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5914 ? expr_loc
: location
, type
, orig_rhs
);
5915 in_late_binary_op
= save
;
5919 /* Aggregates in different TUs might need conversion. */
5920 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
5922 && comptypes (type
, rhstype
))
5923 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5924 ? expr_loc
: location
, type
, rhs
);
5926 /* Conversion to a transparent union or record from its member types.
5927 This applies only to function arguments. */
5928 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
5929 && TYPE_TRANSPARENT_AGGR (type
))
5930 && errtype
== ic_argpass
)
5932 tree memb
, marginal_memb
= NULL_TREE
;
5934 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
5936 tree memb_type
= TREE_TYPE (memb
);
5938 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
5939 TYPE_MAIN_VARIANT (rhstype
)))
5942 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
5945 if (coder
== POINTER_TYPE
)
5947 tree ttl
= TREE_TYPE (memb_type
);
5948 tree ttr
= TREE_TYPE (rhstype
);
5950 /* Any non-function converts to a [const][volatile] void *
5951 and vice versa; otherwise, targets must be the same.
5952 Meanwhile, the lhs target must have all the qualifiers of
5954 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5955 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5956 || comp_target_types (location
, memb_type
, rhstype
))
5958 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
5959 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
5960 /* If this type won't generate any warnings, use it. */
5961 if (lquals
== rquals
5962 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
5963 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5964 ? ((lquals
| rquals
) == rquals
)
5965 : ((lquals
| rquals
) == lquals
)))
5968 /* Keep looking for a better type, but remember this one. */
5970 marginal_memb
= memb
;
5974 /* Can convert integer zero to any pointer type. */
5975 if (null_pointer_constant
)
5977 rhs
= null_pointer_node
;
5982 if (memb
|| marginal_memb
)
5986 /* We have only a marginally acceptable member type;
5987 it needs a warning. */
5988 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
5989 tree ttr
= TREE_TYPE (rhstype
);
5991 /* Const and volatile mean something different for function
5992 types, so the usual warnings are not appropriate. */
5993 if (TREE_CODE (ttr
) == FUNCTION_TYPE
5994 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5996 /* Because const and volatile on functions are
5997 restrictions that say the function will not do
5998 certain things, it is okay to use a const or volatile
5999 function where an ordinary one is wanted, but not
6001 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6002 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6003 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6004 OPT_Wdiscarded_qualifiers
,
6005 G_("passing argument %d of %qE "
6006 "makes %q#v qualified function "
6007 "pointer from unqualified"),
6008 G_("assignment makes %q#v qualified "
6009 "function pointer from "
6011 G_("initialization makes %q#v qualified "
6012 "function pointer from "
6014 G_("return makes %q#v qualified function "
6015 "pointer from unqualified"),
6016 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6018 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
6019 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
6020 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6021 OPT_Wdiscarded_qualifiers
,
6022 G_("passing argument %d of %qE discards "
6023 "%qv qualifier from pointer target type"),
6024 G_("assignment discards %qv qualifier "
6025 "from pointer target type"),
6026 G_("initialization discards %qv qualifier "
6027 "from pointer target type"),
6028 G_("return discards %qv qualifier from "
6029 "pointer target type"),
6030 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6032 memb
= marginal_memb
;
6035 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
6036 pedwarn (location
, OPT_Wpedantic
,
6037 "ISO C prohibits argument conversion to union type");
6039 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
6040 return build_constructor_single (type
, memb
, rhs
);
6044 /* Conversions among pointers */
6045 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
6046 && (coder
== codel
))
6048 tree ttl
= TREE_TYPE (type
);
6049 tree ttr
= TREE_TYPE (rhstype
);
6052 bool is_opaque_pointer
;
6053 int target_cmp
= 0; /* Cache comp_target_types () result. */
6057 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
6058 mvl
= (TYPE_ATOMIC (mvl
)
6059 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
6061 : TYPE_MAIN_VARIANT (mvl
));
6062 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
6063 mvr
= (TYPE_ATOMIC (mvr
)
6064 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
6066 : TYPE_MAIN_VARIANT (mvr
));
6067 /* Opaque pointers are treated like void pointers. */
6068 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
6070 /* The Plan 9 compiler permits a pointer to a struct to be
6071 automatically converted into a pointer to an anonymous field
6072 within the struct. */
6073 if (flag_plan9_extensions
6074 && (TREE_CODE (mvl
) == RECORD_TYPE
|| TREE_CODE(mvl
) == UNION_TYPE
)
6075 && (TREE_CODE (mvr
) == RECORD_TYPE
|| TREE_CODE(mvr
) == UNION_TYPE
)
6078 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
6079 if (new_rhs
!= NULL_TREE
)
6082 rhstype
= TREE_TYPE (rhs
);
6083 coder
= TREE_CODE (rhstype
);
6084 ttr
= TREE_TYPE (rhstype
);
6085 mvr
= TYPE_MAIN_VARIANT (ttr
);
6089 /* C++ does not allow the implicit conversion void* -> T*. However,
6090 for the purpose of reducing the number of false positives, we
6091 tolerate the special case of
6095 where NULL is typically defined in C to be '(void *) 0'. */
6096 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
6097 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
6099 "request for implicit conversion "
6100 "from %qT to %qT not permitted in C++", rhstype
, type
);
6102 /* See if the pointers point to incompatible address spaces. */
6103 asl
= TYPE_ADDR_SPACE (ttl
);
6104 asr
= TYPE_ADDR_SPACE (ttr
);
6105 if (!null_pointer_constant_p (rhs
)
6106 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
6111 error_at (expr_loc
, "passing argument %d of %qE from pointer to "
6112 "non-enclosed address space", parmnum
, rname
);
6115 error_at (location
, "assignment from pointer to "
6116 "non-enclosed address space");
6119 error_at (location
, "initialization from pointer to "
6120 "non-enclosed address space");
6123 error_at (location
, "return from pointer to "
6124 "non-enclosed address space");
6129 return error_mark_node
;
6132 /* Check if the right-hand side has a format attribute but the
6133 left-hand side doesn't. */
6134 if (warn_suggest_attribute_format
6135 && check_missing_format_attribute (type
, rhstype
))
6140 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
6141 "argument %d of %qE might be "
6142 "a candidate for a format attribute",
6146 warning_at (location
, OPT_Wsuggest_attribute_format
,
6147 "assignment left-hand side might be "
6148 "a candidate for a format attribute");
6151 warning_at (location
, OPT_Wsuggest_attribute_format
,
6152 "initialization left-hand side might be "
6153 "a candidate for a format attribute");
6156 warning_at (location
, OPT_Wsuggest_attribute_format
,
6157 "return type might be "
6158 "a candidate for a format attribute");
6165 /* Any non-function converts to a [const][volatile] void *
6166 and vice versa; otherwise, targets must be the same.
6167 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6168 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6169 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6170 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6171 || is_opaque_pointer
6172 || ((c_common_unsigned_type (mvl
)
6173 == c_common_unsigned_type (mvr
))
6174 && (c_common_signed_type (mvl
)
6175 == c_common_signed_type (mvr
))
6176 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6178 /* Warn about loss of qualifers from pointers to arrays with
6179 qualifiers on the element type. */
6180 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
6182 ttr
= strip_array_types (ttr
);
6183 ttl
= strip_array_types (ttl
);
6185 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6186 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6187 WARNING_FOR_QUALIFIERS (location
, expr_loc
,
6188 OPT_Wdiscarded_array_qualifiers
,
6189 G_("passing argument %d of %qE discards "
6190 "%qv qualifier from pointer target type"),
6191 G_("assignment discards %qv qualifier "
6192 "from pointer target type"),
6193 G_("initialization discards %qv qualifier "
6194 "from pointer target type"),
6195 G_("return discards %qv qualifier from "
6196 "pointer target type"),
6197 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6200 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6203 && !null_pointer_constant
6204 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6205 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
6206 G_("ISO C forbids passing argument %d of "
6207 "%qE between function pointer "
6209 G_("ISO C forbids assignment between "
6210 "function pointer and %<void *%>"),
6211 G_("ISO C forbids initialization between "
6212 "function pointer and %<void *%>"),
6213 G_("ISO C forbids return between function "
6214 "pointer and %<void *%>"));
6215 /* Const and volatile mean something different for function types,
6216 so the usual warnings are not appropriate. */
6217 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6218 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6220 /* Don't warn about loss of qualifier for conversions from
6221 qualified void* to pointers to arrays with corresponding
6222 qualifier on the element type. */
6224 ttl
= strip_array_types (ttl
);
6226 /* Assignments between atomic and non-atomic objects are OK. */
6227 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6228 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6230 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6231 OPT_Wdiscarded_qualifiers
,
6232 G_("passing argument %d of %qE discards "
6233 "%qv qualifier from pointer target type"),
6234 G_("assignment discards %qv qualifier "
6235 "from pointer target type"),
6236 G_("initialization discards %qv qualifier "
6237 "from pointer target type"),
6238 G_("return discards %qv qualifier from "
6239 "pointer target type"),
6240 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6242 /* If this is not a case of ignoring a mismatch in signedness,
6244 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6247 /* If there is a mismatch, do warn. */
6248 else if (warn_pointer_sign
)
6249 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpointer_sign
,
6250 G_("pointer targets in passing argument "
6251 "%d of %qE differ in signedness"),
6252 G_("pointer targets in assignment "
6253 "differ in signedness"),
6254 G_("pointer targets in initialization "
6255 "differ in signedness"),
6256 G_("pointer targets in return differ "
6259 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6260 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6262 /* Because const and volatile on functions are restrictions
6263 that say the function will not do certain things,
6264 it is okay to use a const or volatile function
6265 where an ordinary one is wanted, but not vice-versa. */
6266 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6267 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6268 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6269 OPT_Wdiscarded_qualifiers
,
6270 G_("passing argument %d of %qE makes "
6271 "%q#v qualified function pointer "
6272 "from unqualified"),
6273 G_("assignment makes %q#v qualified function "
6274 "pointer from unqualified"),
6275 G_("initialization makes %q#v qualified "
6276 "function pointer from unqualified"),
6277 G_("return makes %q#v qualified function "
6278 "pointer from unqualified"),
6279 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6283 /* Avoid warning about the volatile ObjC EH puts on decls. */
6285 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6286 OPT_Wincompatible_pointer_types
,
6287 G_("passing argument %d of %qE from "
6288 "incompatible pointer type"),
6289 G_("assignment from incompatible pointer type"),
6290 G_("initialization from incompatible "
6292 G_("return from incompatible pointer type"));
6294 return convert (type
, rhs
);
6296 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6298 /* ??? This should not be an error when inlining calls to
6299 unprototyped functions. */
6300 error_at (location
, "invalid use of non-lvalue array");
6301 return error_mark_node
;
6303 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6305 /* An explicit constant 0 can convert to a pointer,
6306 or one that results from arithmetic, even including
6307 a cast to integer type. */
6308 if (!null_pointer_constant
)
6309 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6310 OPT_Wint_conversion
,
6311 G_("passing argument %d of %qE makes "
6312 "pointer from integer without a cast"),
6313 G_("assignment makes pointer from integer "
6315 G_("initialization makes pointer from "
6316 "integer without a cast"),
6317 G_("return makes pointer from integer "
6320 return convert (type
, rhs
);
6322 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6324 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6325 OPT_Wint_conversion
,
6326 G_("passing argument %d of %qE makes integer "
6327 "from pointer without a cast"),
6328 G_("assignment makes integer from pointer "
6330 G_("initialization makes integer from pointer "
6332 G_("return makes integer from pointer "
6334 return convert (type
, rhs
);
6336 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6339 bool save
= in_late_binary_op
;
6340 in_late_binary_op
= true;
6341 ret
= convert (type
, rhs
);
6342 in_late_binary_op
= save
;
6349 error_at (expr_loc
, "incompatible type for argument %d of %qE", parmnum
,
6351 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6352 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6353 "expected %qT but argument is of type %qT", type
, rhstype
);
6356 error_at (location
, "incompatible types when assigning to type %qT from "
6357 "type %qT", type
, rhstype
);
6361 "incompatible types when initializing type %qT using type %qT",
6366 "incompatible types when returning type %qT but %qT was "
6367 "expected", rhstype
, type
);
6373 return error_mark_node
;
6376 /* If VALUE is a compound expr all of whose expressions are constant, then
6377 return its value. Otherwise, return error_mark_node.
6379 This is for handling COMPOUND_EXPRs as initializer elements
6380 which is allowed with a warning when -pedantic is specified. */
6383 valid_compound_expr_initializer (tree value
, tree endtype
)
6385 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6387 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6389 return error_mark_node
;
6390 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6393 else if (!initializer_constant_valid_p (value
, endtype
))
6394 return error_mark_node
;
6399 /* Perform appropriate conversions on the initial value of a variable,
6400 store it in the declaration DECL,
6401 and print any error messages that are appropriate.
6402 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6403 If the init is invalid, store an ERROR_MARK.
6405 INIT_LOC is the location of the initial value. */
6408 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6413 /* If variable's type was invalidly declared, just ignore it. */
6415 type
= TREE_TYPE (decl
);
6416 if (TREE_CODE (type
) == ERROR_MARK
)
6419 /* Digest the specified initializer into an expression. */
6422 npc
= null_pointer_constant_p (init
);
6423 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6424 true, TREE_STATIC (decl
));
6426 /* Store the expression if valid; else report error. */
6428 if (!in_system_header_at (input_location
)
6429 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6430 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6431 "aggregate initialization");
6433 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
6434 DECL_INITIAL (decl
) = value
;
6436 /* ANSI wants warnings about out-of-range constant initializers. */
6437 STRIP_TYPE_NOPS (value
);
6438 if (TREE_STATIC (decl
))
6439 constant_expression_warning (value
);
6441 /* Check if we need to set array size from compound literal size. */
6442 if (TREE_CODE (type
) == ARRAY_TYPE
6443 && TYPE_DOMAIN (type
) == 0
6444 && value
!= error_mark_node
)
6446 tree inside_init
= init
;
6448 STRIP_TYPE_NOPS (inside_init
);
6449 inside_init
= fold (inside_init
);
6451 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6453 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6455 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6457 /* For int foo[] = (int [3]){1}; we need to set array size
6458 now since later on array initializer will be just the
6459 brace enclosed list of the compound literal. */
6460 tree etype
= strip_array_types (TREE_TYPE (decl
));
6461 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6462 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6464 layout_decl (cldecl
, 0);
6466 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6472 /* Methods for storing and printing names for error messages. */
6474 /* Implement a spelling stack that allows components of a name to be pushed
6475 and popped. Each element on the stack is this structure. */
6482 unsigned HOST_WIDE_INT i
;
6487 #define SPELLING_STRING 1
6488 #define SPELLING_MEMBER 2
6489 #define SPELLING_BOUNDS 3
6491 static struct spelling
*spelling
; /* Next stack element (unused). */
6492 static struct spelling
*spelling_base
; /* Spelling stack base. */
6493 static int spelling_size
; /* Size of the spelling stack. */
6495 /* Macros to save and restore the spelling stack around push_... functions.
6496 Alternative to SAVE_SPELLING_STACK. */
6498 #define SPELLING_DEPTH() (spelling - spelling_base)
6499 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6501 /* Push an element on the spelling stack with type KIND and assign VALUE
6504 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6506 int depth = SPELLING_DEPTH (); \
6508 if (depth >= spelling_size) \
6510 spelling_size += 10; \
6511 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6513 RESTORE_SPELLING_DEPTH (depth); \
6516 spelling->kind = (KIND); \
6517 spelling->MEMBER = (VALUE); \
6521 /* Push STRING on the stack. Printed literally. */
6524 push_string (const char *string
)
6526 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6529 /* Push a member name on the stack. Printed as '.' STRING. */
6532 push_member_name (tree decl
)
6534 const char *const string
6536 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6537 : _("<anonymous>"));
6538 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6541 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6544 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6546 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6549 /* Compute the maximum size in bytes of the printed spelling. */
6552 spelling_length (void)
6557 for (p
= spelling_base
; p
< spelling
; p
++)
6559 if (p
->kind
== SPELLING_BOUNDS
)
6562 size
+= strlen (p
->u
.s
) + 1;
6568 /* Print the spelling to BUFFER and return it. */
6571 print_spelling (char *buffer
)
6576 for (p
= spelling_base
; p
< spelling
; p
++)
6577 if (p
->kind
== SPELLING_BOUNDS
)
6579 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6585 if (p
->kind
== SPELLING_MEMBER
)
6587 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
6594 /* Digest the parser output INIT as an initializer for type TYPE.
6595 Return a C expression of type TYPE to represent the initial value.
6597 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6599 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6601 If INIT is a string constant, STRICT_STRING is true if it is
6602 unparenthesized or we should not warn here for it being parenthesized.
6603 For other types of INIT, STRICT_STRING is not used.
6605 INIT_LOC is the location of the INIT.
6607 REQUIRE_CONSTANT requests an error if non-constant initializers or
6608 elements are seen. */
6611 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
6612 bool null_pointer_constant
, bool strict_string
,
6613 int require_constant
)
6615 enum tree_code code
= TREE_CODE (type
);
6616 tree inside_init
= init
;
6617 tree semantic_type
= NULL_TREE
;
6618 bool maybe_const
= true;
6620 if (type
== error_mark_node
6622 || error_operand_p (init
))
6623 return error_mark_node
;
6625 STRIP_TYPE_NOPS (inside_init
);
6627 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
6629 semantic_type
= TREE_TYPE (inside_init
);
6630 inside_init
= TREE_OPERAND (inside_init
, 0);
6632 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
6633 inside_init
= decl_constant_value_for_optimization (inside_init
);
6635 /* Initialization of an array of chars from a string constant
6636 optionally enclosed in braces. */
6638 if (code
== ARRAY_TYPE
&& inside_init
6639 && TREE_CODE (inside_init
) == STRING_CST
)
6642 = (TYPE_ATOMIC (TREE_TYPE (type
))
6643 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
6645 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6646 /* Note that an array could be both an array of character type
6647 and an array of wchar_t if wchar_t is signed char or unsigned
6649 bool char_array
= (typ1
== char_type_node
6650 || typ1
== signed_char_type_node
6651 || typ1
== unsigned_char_type_node
);
6652 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
6653 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
6654 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
6656 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
6659 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
6660 expr
.value
= inside_init
;
6661 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
6662 expr
.original_type
= NULL
;
6663 maybe_warn_string_init (init_loc
, type
, expr
);
6665 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
6666 pedwarn_init (init_loc
, OPT_Wpedantic
,
6667 "initialization of a flexible array member");
6669 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6670 TYPE_MAIN_VARIANT (type
)))
6675 if (typ2
!= char_type_node
)
6677 error_init (init_loc
, "char-array initialized from wide "
6679 return error_mark_node
;
6684 if (typ2
== char_type_node
)
6686 error_init (init_loc
, "wide character array initialized "
6687 "from non-wide string");
6688 return error_mark_node
;
6690 else if (!comptypes(typ1
, typ2
))
6692 error_init (init_loc
, "wide character array initialized "
6693 "from incompatible wide string");
6694 return error_mark_node
;
6698 TREE_TYPE (inside_init
) = type
;
6699 if (TYPE_DOMAIN (type
) != 0
6700 && TYPE_SIZE (type
) != 0
6701 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
6703 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
6705 /* Subtract the size of a single (possibly wide) character
6706 because it's ok to ignore the terminating null char
6707 that is counted in the length of the constant. */
6708 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
6710 - (TYPE_PRECISION (typ1
)
6712 pedwarn_init (init_loc
, 0,
6713 ("initializer-string for array of chars "
6715 else if (warn_cxx_compat
6716 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
6717 warning_at (init_loc
, OPT_Wc___compat
,
6718 ("initializer-string for array chars "
6719 "is too long for C++"));
6724 else if (INTEGRAL_TYPE_P (typ1
))
6726 error_init (init_loc
, "array of inappropriate type initialized "
6727 "from string constant");
6728 return error_mark_node
;
6732 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6733 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6734 below and handle as a constructor. */
6735 if (code
== VECTOR_TYPE
6736 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
6737 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
6738 && TREE_CONSTANT (inside_init
))
6740 if (TREE_CODE (inside_init
) == VECTOR_CST
6741 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6742 TYPE_MAIN_VARIANT (type
)))
6745 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
6747 unsigned HOST_WIDE_INT ix
;
6749 bool constant_p
= true;
6751 /* Iterate through elements and check if all constructor
6752 elements are *_CSTs. */
6753 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
6754 if (!CONSTANT_CLASS_P (value
))
6761 return build_vector_from_ctor (type
,
6762 CONSTRUCTOR_ELTS (inside_init
));
6766 if (warn_sequence_point
)
6767 verify_sequence_points (inside_init
);
6769 /* Any type can be initialized
6770 from an expression of the same type, optionally with braces. */
6772 if (inside_init
&& TREE_TYPE (inside_init
) != 0
6773 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6774 TYPE_MAIN_VARIANT (type
))
6775 || (code
== ARRAY_TYPE
6776 && comptypes (TREE_TYPE (inside_init
), type
))
6777 || (code
== VECTOR_TYPE
6778 && comptypes (TREE_TYPE (inside_init
), type
))
6779 || (code
== POINTER_TYPE
6780 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
6781 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
6782 TREE_TYPE (type
)))))
6784 if (code
== POINTER_TYPE
)
6786 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
6788 if (TREE_CODE (inside_init
) == STRING_CST
6789 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6790 inside_init
= array_to_pointer_conversion
6791 (init_loc
, inside_init
);
6794 error_init (init_loc
, "invalid use of non-lvalue array");
6795 return error_mark_node
;
6800 if (code
== VECTOR_TYPE
)
6801 /* Although the types are compatible, we may require a
6803 inside_init
= convert (type
, inside_init
);
6805 if (require_constant
6806 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6808 /* As an extension, allow initializing objects with static storage
6809 duration with compound literals (which are then treated just as
6810 the brace enclosed list they contain). Also allow this for
6811 vectors, as we can only assign them with compound literals. */
6812 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
6813 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
6815 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6816 inside_init
= DECL_INITIAL (decl
);
6819 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
6820 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
6822 error_init (init_loc
, "array initialized from non-constant array "
6824 return error_mark_node
;
6827 /* Compound expressions can only occur here if -Wpedantic or
6828 -pedantic-errors is specified. In the later case, we always want
6829 an error. In the former case, we simply want a warning. */
6830 if (require_constant
&& pedantic
6831 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
6834 = valid_compound_expr_initializer (inside_init
,
6835 TREE_TYPE (inside_init
));
6836 if (inside_init
== error_mark_node
)
6837 error_init (init_loc
, "initializer element is not constant");
6839 pedwarn_init (init_loc
, OPT_Wpedantic
,
6840 "initializer element is not constant");
6841 if (flag_pedantic_errors
)
6842 inside_init
= error_mark_node
;
6844 else if (require_constant
6845 && !initializer_constant_valid_p (inside_init
,
6846 TREE_TYPE (inside_init
)))
6848 error_init (init_loc
, "initializer element is not constant");
6849 inside_init
= error_mark_node
;
6851 else if (require_constant
&& !maybe_const
)
6852 pedwarn_init (init_loc
, OPT_Wpedantic
,
6853 "initializer element is not a constant expression");
6855 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6856 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
6857 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
6858 type
, inside_init
, origtype
,
6859 ic_init
, null_pointer_constant
,
6860 NULL_TREE
, NULL_TREE
, 0);
6864 /* Handle scalar types, including conversions. */
6866 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
6867 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
6868 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
6870 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
6871 && (TREE_CODE (init
) == STRING_CST
6872 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
6873 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
6875 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
6878 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
6879 inside_init
, origtype
, ic_init
,
6880 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
6883 /* Check to see if we have already given an error message. */
6884 if (inside_init
== error_mark_node
)
6886 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
6888 error_init (init_loc
, "initializer element is not constant");
6889 inside_init
= error_mark_node
;
6891 else if (require_constant
6892 && !initializer_constant_valid_p (inside_init
,
6893 TREE_TYPE (inside_init
)))
6895 error_init (init_loc
, "initializer element is not computable at "
6897 inside_init
= error_mark_node
;
6899 else if (require_constant
&& !maybe_const
)
6900 pedwarn_init (init_loc
, 0,
6901 "initializer element is not a constant expression");
6906 /* Come here only for records and arrays. */
6908 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
6910 error_init (init_loc
, "variable-sized object may not be initialized");
6911 return error_mark_node
;
6914 error_init (init_loc
, "invalid initializer");
6915 return error_mark_node
;
6918 /* Handle initializers that use braces. */
6920 /* Type of object we are accumulating a constructor for.
6921 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6922 static tree constructor_type
;
6924 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6926 static tree constructor_fields
;
6928 /* For an ARRAY_TYPE, this is the specified index
6929 at which to store the next element we get. */
6930 static tree constructor_index
;
6932 /* For an ARRAY_TYPE, this is the maximum index. */
6933 static tree constructor_max_index
;
6935 /* For a RECORD_TYPE, this is the first field not yet written out. */
6936 static tree constructor_unfilled_fields
;
6938 /* For an ARRAY_TYPE, this is the index of the first element
6939 not yet written out. */
6940 static tree constructor_unfilled_index
;
6942 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6943 This is so we can generate gaps between fields, when appropriate. */
6944 static tree constructor_bit_index
;
6946 /* If we are saving up the elements rather than allocating them,
6947 this is the list of elements so far (in reverse order,
6948 most recent first). */
6949 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
6951 /* 1 if constructor should be incrementally stored into a constructor chain,
6952 0 if all the elements should be kept in AVL tree. */
6953 static int constructor_incremental
;
6955 /* 1 if so far this constructor's elements are all compile-time constants. */
6956 static int constructor_constant
;
6958 /* 1 if so far this constructor's elements are all valid address constants. */
6959 static int constructor_simple
;
6961 /* 1 if this constructor has an element that cannot be part of a
6962 constant expression. */
6963 static int constructor_nonconst
;
6965 /* 1 if this constructor is erroneous so far. */
6966 static int constructor_erroneous
;
6968 /* 1 if this constructor is the universal zero initializer { 0 }. */
6969 static int constructor_zeroinit
;
6971 /* Structure for managing pending initializer elements, organized as an
6976 struct init_node
*left
, *right
;
6977 struct init_node
*parent
;
6984 /* Tree of pending elements at this constructor level.
6985 These are elements encountered out of order
6986 which belong at places we haven't reached yet in actually
6988 Will never hold tree nodes across GC runs. */
6989 static struct init_node
*constructor_pending_elts
;
6991 /* The SPELLING_DEPTH of this constructor. */
6992 static int constructor_depth
;
6994 /* DECL node for which an initializer is being read.
6995 0 means we are reading a constructor expression
6996 such as (struct foo) {...}. */
6997 static tree constructor_decl
;
6999 /* Nonzero if this is an initializer for a top-level decl. */
7000 static int constructor_top_level
;
7002 /* Nonzero if there were any member designators in this initializer. */
7003 static int constructor_designated
;
7005 /* Nesting depth of designator list. */
7006 static int designator_depth
;
7008 /* Nonzero if there were diagnosed errors in this designator list. */
7009 static int designator_erroneous
;
7012 /* This stack has a level for each implicit or explicit level of
7013 structuring in the initializer, including the outermost one. It
7014 saves the values of most of the variables above. */
7016 struct constructor_range_stack
;
7018 struct constructor_stack
7020 struct constructor_stack
*next
;
7025 tree unfilled_index
;
7026 tree unfilled_fields
;
7028 vec
<constructor_elt
, va_gc
> *elements
;
7029 struct init_node
*pending_elts
;
7032 /* If value nonzero, this value should replace the entire
7033 constructor at this level. */
7034 struct c_expr replacement_value
;
7035 struct constructor_range_stack
*range_stack
;
7044 int designator_depth
;
7047 static struct constructor_stack
*constructor_stack
;
7049 /* This stack represents designators from some range designator up to
7050 the last designator in the list. */
7052 struct constructor_range_stack
7054 struct constructor_range_stack
*next
, *prev
;
7055 struct constructor_stack
*stack
;
7062 static struct constructor_range_stack
*constructor_range_stack
;
7064 /* This stack records separate initializers that are nested.
7065 Nested initializers can't happen in ANSI C, but GNU C allows them
7066 in cases like { ... (struct foo) { ... } ... }. */
7068 struct initializer_stack
7070 struct initializer_stack
*next
;
7072 struct constructor_stack
*constructor_stack
;
7073 struct constructor_range_stack
*constructor_range_stack
;
7074 vec
<constructor_elt
, va_gc
> *elements
;
7075 struct spelling
*spelling
;
7076 struct spelling
*spelling_base
;
7079 char require_constant_value
;
7080 char require_constant_elements
;
7083 static struct initializer_stack
*initializer_stack
;
7085 /* Prepare to parse and output the initializer for variable DECL. */
7088 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
7091 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
7093 p
->decl
= constructor_decl
;
7094 p
->require_constant_value
= require_constant_value
;
7095 p
->require_constant_elements
= require_constant_elements
;
7096 p
->constructor_stack
= constructor_stack
;
7097 p
->constructor_range_stack
= constructor_range_stack
;
7098 p
->elements
= constructor_elements
;
7099 p
->spelling
= spelling
;
7100 p
->spelling_base
= spelling_base
;
7101 p
->spelling_size
= spelling_size
;
7102 p
->top_level
= constructor_top_level
;
7103 p
->next
= initializer_stack
;
7104 initializer_stack
= p
;
7106 constructor_decl
= decl
;
7107 constructor_designated
= 0;
7108 constructor_top_level
= top_level
;
7110 if (decl
!= 0 && decl
!= error_mark_node
)
7112 require_constant_value
= TREE_STATIC (decl
);
7113 require_constant_elements
7114 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
7115 /* For a scalar, you can always use any value to initialize,
7116 even within braces. */
7117 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)));
7118 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
7122 require_constant_value
= 0;
7123 require_constant_elements
= 0;
7124 locus
= _("(anonymous)");
7127 constructor_stack
= 0;
7128 constructor_range_stack
= 0;
7130 found_missing_braces
= 0;
7134 RESTORE_SPELLING_DEPTH (0);
7137 push_string (locus
);
7143 struct initializer_stack
*p
= initializer_stack
;
7145 /* Free the whole constructor stack of this initializer. */
7146 while (constructor_stack
)
7148 struct constructor_stack
*q
= constructor_stack
;
7149 constructor_stack
= q
->next
;
7153 gcc_assert (!constructor_range_stack
);
7155 /* Pop back to the data of the outer initializer (if any). */
7156 free (spelling_base
);
7158 constructor_decl
= p
->decl
;
7159 require_constant_value
= p
->require_constant_value
;
7160 require_constant_elements
= p
->require_constant_elements
;
7161 constructor_stack
= p
->constructor_stack
;
7162 constructor_range_stack
= p
->constructor_range_stack
;
7163 constructor_elements
= p
->elements
;
7164 spelling
= p
->spelling
;
7165 spelling_base
= p
->spelling_base
;
7166 spelling_size
= p
->spelling_size
;
7167 constructor_top_level
= p
->top_level
;
7168 initializer_stack
= p
->next
;
7172 /* Call here when we see the initializer is surrounded by braces.
7173 This is instead of a call to push_init_level;
7174 it is matched by a call to pop_init_level.
7176 TYPE is the type to initialize, for a constructor expression.
7177 For an initializer for a decl, TYPE is zero. */
7180 really_start_incremental_init (tree type
)
7182 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7185 type
= TREE_TYPE (constructor_decl
);
7187 if (TREE_CODE (type
) == VECTOR_TYPE
7188 && TYPE_VECTOR_OPAQUE (type
))
7189 error ("opaque vector types cannot be initialized");
7191 p
->type
= constructor_type
;
7192 p
->fields
= constructor_fields
;
7193 p
->index
= constructor_index
;
7194 p
->max_index
= constructor_max_index
;
7195 p
->unfilled_index
= constructor_unfilled_index
;
7196 p
->unfilled_fields
= constructor_unfilled_fields
;
7197 p
->bit_index
= constructor_bit_index
;
7198 p
->elements
= constructor_elements
;
7199 p
->constant
= constructor_constant
;
7200 p
->simple
= constructor_simple
;
7201 p
->nonconst
= constructor_nonconst
;
7202 p
->erroneous
= constructor_erroneous
;
7203 p
->pending_elts
= constructor_pending_elts
;
7204 p
->depth
= constructor_depth
;
7205 p
->replacement_value
.value
= 0;
7206 p
->replacement_value
.original_code
= ERROR_MARK
;
7207 p
->replacement_value
.original_type
= NULL
;
7211 p
->incremental
= constructor_incremental
;
7212 p
->designated
= constructor_designated
;
7213 p
->designator_depth
= designator_depth
;
7215 constructor_stack
= p
;
7217 constructor_constant
= 1;
7218 constructor_simple
= 1;
7219 constructor_nonconst
= 0;
7220 constructor_depth
= SPELLING_DEPTH ();
7221 constructor_elements
= NULL
;
7222 constructor_pending_elts
= 0;
7223 constructor_type
= type
;
7224 constructor_incremental
= 1;
7225 constructor_designated
= 0;
7226 constructor_zeroinit
= 1;
7227 designator_depth
= 0;
7228 designator_erroneous
= 0;
7230 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7231 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7233 constructor_fields
= TYPE_FIELDS (constructor_type
);
7234 /* Skip any nameless bit fields at the beginning. */
7235 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7236 && DECL_NAME (constructor_fields
) == 0)
7237 constructor_fields
= DECL_CHAIN (constructor_fields
);
7239 constructor_unfilled_fields
= constructor_fields
;
7240 constructor_bit_index
= bitsize_zero_node
;
7242 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7244 if (TYPE_DOMAIN (constructor_type
))
7246 constructor_max_index
7247 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7249 /* Detect non-empty initializations of zero-length arrays. */
7250 if (constructor_max_index
== NULL_TREE
7251 && TYPE_SIZE (constructor_type
))
7252 constructor_max_index
= integer_minus_one_node
;
7254 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7255 to initialize VLAs will cause a proper error; avoid tree
7256 checking errors as well by setting a safe value. */
7257 if (constructor_max_index
7258 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7259 constructor_max_index
= integer_minus_one_node
;
7262 = convert (bitsizetype
,
7263 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7267 constructor_index
= bitsize_zero_node
;
7268 constructor_max_index
= NULL_TREE
;
7271 constructor_unfilled_index
= constructor_index
;
7273 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7275 /* Vectors are like simple fixed-size arrays. */
7276 constructor_max_index
=
7277 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7278 constructor_index
= bitsize_zero_node
;
7279 constructor_unfilled_index
= constructor_index
;
7283 /* Handle the case of int x = {5}; */
7284 constructor_fields
= constructor_type
;
7285 constructor_unfilled_fields
= constructor_type
;
7289 /* Push down into a subobject, for initialization.
7290 If this is for an explicit set of braces, IMPLICIT is 0.
7291 If it is because the next element belongs at a lower level,
7292 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7295 push_init_level (location_t loc
, int implicit
,
7296 struct obstack
*braced_init_obstack
)
7298 struct constructor_stack
*p
;
7299 tree value
= NULL_TREE
;
7301 /* If we've exhausted any levels that didn't have braces,
7302 pop them now. If implicit == 1, this will have been done in
7303 process_init_element; do not repeat it here because in the case
7304 of excess initializers for an empty aggregate this leads to an
7305 infinite cycle of popping a level and immediately recreating
7309 while (constructor_stack
->implicit
)
7311 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7312 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7313 && constructor_fields
== 0)
7314 process_init_element (input_location
,
7315 pop_init_level (loc
, 1, braced_init_obstack
),
7316 true, braced_init_obstack
);
7317 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7318 && constructor_max_index
7319 && tree_int_cst_lt (constructor_max_index
,
7321 process_init_element (input_location
,
7322 pop_init_level (loc
, 1, braced_init_obstack
),
7323 true, braced_init_obstack
);
7329 /* Unless this is an explicit brace, we need to preserve previous
7333 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7334 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7335 && constructor_fields
)
7336 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7337 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7338 value
= find_init_member (constructor_index
, braced_init_obstack
);
7341 p
= XNEW (struct constructor_stack
);
7342 p
->type
= constructor_type
;
7343 p
->fields
= constructor_fields
;
7344 p
->index
= constructor_index
;
7345 p
->max_index
= constructor_max_index
;
7346 p
->unfilled_index
= constructor_unfilled_index
;
7347 p
->unfilled_fields
= constructor_unfilled_fields
;
7348 p
->bit_index
= constructor_bit_index
;
7349 p
->elements
= constructor_elements
;
7350 p
->constant
= constructor_constant
;
7351 p
->simple
= constructor_simple
;
7352 p
->nonconst
= constructor_nonconst
;
7353 p
->erroneous
= constructor_erroneous
;
7354 p
->pending_elts
= constructor_pending_elts
;
7355 p
->depth
= constructor_depth
;
7356 p
->replacement_value
.value
= 0;
7357 p
->replacement_value
.original_code
= ERROR_MARK
;
7358 p
->replacement_value
.original_type
= NULL
;
7359 p
->implicit
= implicit
;
7361 p
->incremental
= constructor_incremental
;
7362 p
->designated
= constructor_designated
;
7363 p
->designator_depth
= designator_depth
;
7364 p
->next
= constructor_stack
;
7366 constructor_stack
= p
;
7368 constructor_constant
= 1;
7369 constructor_simple
= 1;
7370 constructor_nonconst
= 0;
7371 constructor_depth
= SPELLING_DEPTH ();
7372 constructor_elements
= NULL
;
7373 constructor_incremental
= 1;
7374 constructor_designated
= 0;
7375 constructor_pending_elts
= 0;
7378 p
->range_stack
= constructor_range_stack
;
7379 constructor_range_stack
= 0;
7380 designator_depth
= 0;
7381 designator_erroneous
= 0;
7384 /* Don't die if an entire brace-pair level is superfluous
7385 in the containing level. */
7386 if (constructor_type
== 0)
7388 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7389 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7391 /* Don't die if there are extra init elts at the end. */
7392 if (constructor_fields
== 0)
7393 constructor_type
= 0;
7396 constructor_type
= TREE_TYPE (constructor_fields
);
7397 push_member_name (constructor_fields
);
7398 constructor_depth
++;
7400 /* If upper initializer is designated, then mark this as
7401 designated too to prevent bogus warnings. */
7402 constructor_designated
= p
->designated
;
7404 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7406 constructor_type
= TREE_TYPE (constructor_type
);
7407 push_array_bounds (tree_to_uhwi (constructor_index
));
7408 constructor_depth
++;
7411 if (constructor_type
== 0)
7413 error_init (loc
, "extra brace group at end of initializer");
7414 constructor_fields
= 0;
7415 constructor_unfilled_fields
= 0;
7419 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7421 constructor_constant
= TREE_CONSTANT (value
);
7422 constructor_simple
= TREE_STATIC (value
);
7423 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7424 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7425 if (!vec_safe_is_empty (constructor_elements
)
7426 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7427 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7428 set_nonincremental_init (braced_init_obstack
);
7432 found_missing_braces
= 1;
7434 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7435 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7437 constructor_fields
= TYPE_FIELDS (constructor_type
);
7438 /* Skip any nameless bit fields at the beginning. */
7439 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7440 && DECL_NAME (constructor_fields
) == 0)
7441 constructor_fields
= DECL_CHAIN (constructor_fields
);
7443 constructor_unfilled_fields
= constructor_fields
;
7444 constructor_bit_index
= bitsize_zero_node
;
7446 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7448 /* Vectors are like simple fixed-size arrays. */
7449 constructor_max_index
=
7450 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7451 constructor_index
= bitsize_int (0);
7452 constructor_unfilled_index
= constructor_index
;
7454 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7456 if (TYPE_DOMAIN (constructor_type
))
7458 constructor_max_index
7459 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7461 /* Detect non-empty initializations of zero-length arrays. */
7462 if (constructor_max_index
== NULL_TREE
7463 && TYPE_SIZE (constructor_type
))
7464 constructor_max_index
= integer_minus_one_node
;
7466 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7467 to initialize VLAs will cause a proper error; avoid tree
7468 checking errors as well by setting a safe value. */
7469 if (constructor_max_index
7470 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7471 constructor_max_index
= integer_minus_one_node
;
7474 = convert (bitsizetype
,
7475 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7478 constructor_index
= bitsize_zero_node
;
7480 constructor_unfilled_index
= constructor_index
;
7481 if (value
&& TREE_CODE (value
) == STRING_CST
)
7483 /* We need to split the char/wchar array into individual
7484 characters, so that we don't have to special case it
7486 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7491 if (constructor_type
!= error_mark_node
)
7492 warning_init (input_location
, 0, "braces around scalar initializer");
7493 constructor_fields
= constructor_type
;
7494 constructor_unfilled_fields
= constructor_type
;
7498 /* At the end of an implicit or explicit brace level,
7499 finish up that level of constructor. If a single expression
7500 with redundant braces initialized that level, return the
7501 c_expr structure for that expression. Otherwise, the original_code
7502 element is set to ERROR_MARK.
7503 If we were outputting the elements as they are read, return 0 as the value
7504 from inner levels (process_init_element ignores that),
7505 but return error_mark_node as the value from the outermost level
7506 (that's what we want to put in DECL_INITIAL).
7507 Otherwise, return a CONSTRUCTOR expression as the value. */
7510 pop_init_level (location_t loc
, int implicit
,
7511 struct obstack
*braced_init_obstack
)
7513 struct constructor_stack
*p
;
7516 ret
.original_code
= ERROR_MARK
;
7517 ret
.original_type
= NULL
;
7521 /* When we come to an explicit close brace,
7522 pop any inner levels that didn't have explicit braces. */
7523 while (constructor_stack
->implicit
)
7524 process_init_element (input_location
,
7525 pop_init_level (loc
, 1, braced_init_obstack
),
7526 true, braced_init_obstack
);
7527 gcc_assert (!constructor_range_stack
);
7530 /* Now output all pending elements. */
7531 constructor_incremental
= 1;
7532 output_pending_init_elements (1, braced_init_obstack
);
7534 p
= constructor_stack
;
7536 /* Error for initializing a flexible array member, or a zero-length
7537 array member in an inappropriate context. */
7538 if (constructor_type
&& constructor_fields
7539 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7540 && TYPE_DOMAIN (constructor_type
)
7541 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7543 /* Silently discard empty initializations. The parser will
7544 already have pedwarned for empty brackets. */
7545 if (integer_zerop (constructor_unfilled_index
))
7546 constructor_type
= NULL_TREE
;
7549 gcc_assert (!TYPE_SIZE (constructor_type
));
7551 if (constructor_depth
> 2)
7552 error_init (loc
, "initialization of flexible array member in a nested context");
7554 pedwarn_init (loc
, OPT_Wpedantic
,
7555 "initialization of a flexible array member");
7557 /* We have already issued an error message for the existence
7558 of a flexible array member not at the end of the structure.
7559 Discard the initializer so that we do not die later. */
7560 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7561 constructor_type
= NULL_TREE
;
7565 switch (vec_safe_length (constructor_elements
))
7568 /* Initialization with { } counts as zeroinit. */
7569 constructor_zeroinit
= 1;
7572 /* This might be zeroinit as well. */
7573 if (integer_zerop ((*constructor_elements
)[0].value
))
7574 constructor_zeroinit
= 1;
7577 /* If the constructor has more than one element, it can't be { 0 }. */
7578 constructor_zeroinit
= 0;
7582 /* Warn when some structs are initialized with direct aggregation. */
7583 if (!implicit
&& found_missing_braces
&& warn_missing_braces
7584 && !constructor_zeroinit
)
7585 warning_init (loc
, OPT_Wmissing_braces
,
7586 "missing braces around initializer");
7588 /* Warn when some struct elements are implicitly initialized to zero. */
7589 if (warn_missing_field_initializers
7591 && TREE_CODE (constructor_type
) == RECORD_TYPE
7592 && constructor_unfilled_fields
)
7594 /* Do not warn for flexible array members or zero-length arrays. */
7595 while (constructor_unfilled_fields
7596 && (!DECL_SIZE (constructor_unfilled_fields
)
7597 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
7598 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
7600 if (constructor_unfilled_fields
7601 /* Do not warn if this level of the initializer uses member
7602 designators; it is likely to be deliberate. */
7603 && !constructor_designated
7604 /* Do not warn about initializing with { 0 } or with { }. */
7605 && !constructor_zeroinit
)
7607 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
7608 "missing initializer for field %qD of %qT",
7609 constructor_unfilled_fields
,
7611 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
7612 "%qD declared here", constructor_unfilled_fields
);
7616 /* Pad out the end of the structure. */
7617 if (p
->replacement_value
.value
)
7618 /* If this closes a superfluous brace pair,
7619 just pass out the element between them. */
7620 ret
= p
->replacement_value
;
7621 else if (constructor_type
== 0)
7623 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
7624 && TREE_CODE (constructor_type
) != UNION_TYPE
7625 && TREE_CODE (constructor_type
) != ARRAY_TYPE
7626 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
7628 /* A nonincremental scalar initializer--just return
7629 the element, after verifying there is just one. */
7630 if (vec_safe_is_empty (constructor_elements
))
7632 if (!constructor_erroneous
)
7633 error_init (loc
, "empty scalar initializer");
7634 ret
.value
= error_mark_node
;
7636 else if (vec_safe_length (constructor_elements
) != 1)
7638 error_init (loc
, "extra elements in scalar initializer");
7639 ret
.value
= (*constructor_elements
)[0].value
;
7642 ret
.value
= (*constructor_elements
)[0].value
;
7646 if (constructor_erroneous
)
7647 ret
.value
= error_mark_node
;
7650 ret
.value
= build_constructor (constructor_type
,
7651 constructor_elements
);
7652 if (constructor_constant
)
7653 TREE_CONSTANT (ret
.value
) = 1;
7654 if (constructor_constant
&& constructor_simple
)
7655 TREE_STATIC (ret
.value
) = 1;
7656 if (constructor_nonconst
)
7657 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
7661 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
7663 if (constructor_nonconst
)
7664 ret
.original_code
= C_MAYBE_CONST_EXPR
;
7665 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
7666 ret
.original_code
= ERROR_MARK
;
7669 constructor_type
= p
->type
;
7670 constructor_fields
= p
->fields
;
7671 constructor_index
= p
->index
;
7672 constructor_max_index
= p
->max_index
;
7673 constructor_unfilled_index
= p
->unfilled_index
;
7674 constructor_unfilled_fields
= p
->unfilled_fields
;
7675 constructor_bit_index
= p
->bit_index
;
7676 constructor_elements
= p
->elements
;
7677 constructor_constant
= p
->constant
;
7678 constructor_simple
= p
->simple
;
7679 constructor_nonconst
= p
->nonconst
;
7680 constructor_erroneous
= p
->erroneous
;
7681 constructor_incremental
= p
->incremental
;
7682 constructor_designated
= p
->designated
;
7683 designator_depth
= p
->designator_depth
;
7684 constructor_pending_elts
= p
->pending_elts
;
7685 constructor_depth
= p
->depth
;
7687 constructor_range_stack
= p
->range_stack
;
7688 RESTORE_SPELLING_DEPTH (constructor_depth
);
7690 constructor_stack
= p
->next
;
7693 if (ret
.value
== 0 && constructor_stack
== 0)
7694 ret
.value
= error_mark_node
;
7698 /* Common handling for both array range and field name designators.
7699 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7702 set_designator (location_t loc
, int array
,
7703 struct obstack
*braced_init_obstack
)
7706 enum tree_code subcode
;
7708 /* Don't die if an entire brace-pair level is superfluous
7709 in the containing level. */
7710 if (constructor_type
== 0)
7713 /* If there were errors in this designator list already, bail out
7715 if (designator_erroneous
)
7718 if (!designator_depth
)
7720 gcc_assert (!constructor_range_stack
);
7722 /* Designator list starts at the level of closest explicit
7724 while (constructor_stack
->implicit
)
7725 process_init_element (input_location
,
7726 pop_init_level (loc
, 1, braced_init_obstack
),
7727 true, braced_init_obstack
);
7728 constructor_designated
= 1;
7732 switch (TREE_CODE (constructor_type
))
7736 subtype
= TREE_TYPE (constructor_fields
);
7737 if (subtype
!= error_mark_node
)
7738 subtype
= TYPE_MAIN_VARIANT (subtype
);
7741 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7747 subcode
= TREE_CODE (subtype
);
7748 if (array
&& subcode
!= ARRAY_TYPE
)
7750 error_init (loc
, "array index in non-array initializer");
7753 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
7755 error_init (loc
, "field name not in record or union initializer");
7759 constructor_designated
= 1;
7760 push_init_level (loc
, 2, braced_init_obstack
);
7764 /* If there are range designators in designator list, push a new designator
7765 to constructor_range_stack. RANGE_END is end of such stack range or
7766 NULL_TREE if there is no range designator at this level. */
7769 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
7771 struct constructor_range_stack
*p
;
7773 p
= (struct constructor_range_stack
*)
7774 obstack_alloc (braced_init_obstack
,
7775 sizeof (struct constructor_range_stack
));
7776 p
->prev
= constructor_range_stack
;
7778 p
->fields
= constructor_fields
;
7779 p
->range_start
= constructor_index
;
7780 p
->index
= constructor_index
;
7781 p
->stack
= constructor_stack
;
7782 p
->range_end
= range_end
;
7783 if (constructor_range_stack
)
7784 constructor_range_stack
->next
= p
;
7785 constructor_range_stack
= p
;
7788 /* Within an array initializer, specify the next index to be initialized.
7789 FIRST is that index. If LAST is nonzero, then initialize a range
7790 of indices, running from FIRST through LAST. */
7793 set_init_index (location_t loc
, tree first
, tree last
,
7794 struct obstack
*braced_init_obstack
)
7796 if (set_designator (loc
, 1, braced_init_obstack
))
7799 designator_erroneous
= 1;
7801 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
7802 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
7804 error_init (loc
, "array index in initializer not of integer type");
7808 if (TREE_CODE (first
) != INTEGER_CST
)
7810 first
= c_fully_fold (first
, false, NULL
);
7811 if (TREE_CODE (first
) == INTEGER_CST
)
7812 pedwarn_init (loc
, OPT_Wpedantic
,
7813 "array index in initializer is not "
7814 "an integer constant expression");
7817 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
7819 last
= c_fully_fold (last
, false, NULL
);
7820 if (TREE_CODE (last
) == INTEGER_CST
)
7821 pedwarn_init (loc
, OPT_Wpedantic
,
7822 "array index in initializer is not "
7823 "an integer constant expression");
7826 if (TREE_CODE (first
) != INTEGER_CST
)
7827 error_init (loc
, "nonconstant array index in initializer");
7828 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
7829 error_init (loc
, "nonconstant array index in initializer");
7830 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7831 error_init (loc
, "array index in non-array initializer");
7832 else if (tree_int_cst_sgn (first
) == -1)
7833 error_init (loc
, "array index in initializer exceeds array bounds");
7834 else if (constructor_max_index
7835 && tree_int_cst_lt (constructor_max_index
, first
))
7836 error_init (loc
, "array index in initializer exceeds array bounds");
7839 constant_expression_warning (first
);
7841 constant_expression_warning (last
);
7842 constructor_index
= convert (bitsizetype
, first
);
7843 if (tree_int_cst_lt (constructor_index
, first
))
7845 constructor_index
= copy_node (constructor_index
);
7846 TREE_OVERFLOW (constructor_index
) = 1;
7851 if (tree_int_cst_equal (first
, last
))
7853 else if (tree_int_cst_lt (last
, first
))
7855 error_init (loc
, "empty index range in initializer");
7860 last
= convert (bitsizetype
, last
);
7861 if (constructor_max_index
!= 0
7862 && tree_int_cst_lt (constructor_max_index
, last
))
7864 error_init (loc
, "array index range in initializer exceeds "
7872 designator_erroneous
= 0;
7873 if (constructor_range_stack
|| last
)
7874 push_range_stack (last
, braced_init_obstack
);
7878 /* Within a struct initializer, specify the next field to be initialized. */
7881 set_init_label (location_t loc
, tree fieldname
,
7882 struct obstack
*braced_init_obstack
)
7886 if (set_designator (loc
, 0, braced_init_obstack
))
7889 designator_erroneous
= 1;
7891 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7892 && TREE_CODE (constructor_type
) != UNION_TYPE
)
7894 error_init (loc
, "field name not in record or union initializer");
7898 field
= lookup_field (constructor_type
, fieldname
);
7901 error_at (loc
, "unknown field %qE specified in initializer", fieldname
);
7905 constructor_fields
= TREE_VALUE (field
);
7907 designator_erroneous
= 0;
7908 if (constructor_range_stack
)
7909 push_range_stack (NULL_TREE
, braced_init_obstack
);
7910 field
= TREE_CHAIN (field
);
7913 if (set_designator (loc
, 0, braced_init_obstack
))
7917 while (field
!= NULL_TREE
);
7920 /* Add a new initializer to the tree of pending initializers. PURPOSE
7921 identifies the initializer, either array index or field in a structure.
7922 VALUE is the value of that index or field. If ORIGTYPE is not
7923 NULL_TREE, it is the original type of VALUE.
7925 IMPLICIT is true if value comes from pop_init_level (1),
7926 the new initializer has been merged with the existing one
7927 and thus no warnings should be emitted about overriding an
7928 existing initializer. */
7931 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
7932 bool implicit
, struct obstack
*braced_init_obstack
)
7934 struct init_node
*p
, **q
, *r
;
7936 q
= &constructor_pending_elts
;
7939 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7944 if (tree_int_cst_lt (purpose
, p
->purpose
))
7946 else if (tree_int_cst_lt (p
->purpose
, purpose
))
7952 if (TREE_SIDE_EFFECTS (p
->value
))
7953 warning_init (loc
, OPT_Woverride_init_side_effects
,
7954 "initialized field with side-effects "
7956 else if (warn_override_init
)
7957 warning_init (loc
, OPT_Woverride_init
,
7958 "initialized field overwritten");
7961 p
->origtype
= origtype
;
7970 bitpos
= bit_position (purpose
);
7974 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7976 else if (p
->purpose
!= purpose
)
7982 if (TREE_SIDE_EFFECTS (p
->value
))
7983 warning_init (loc
, OPT_Woverride_init_side_effects
,
7984 "initialized field with side-effects "
7986 else if (warn_override_init
)
7987 warning_init (loc
, OPT_Woverride_init
,
7988 "initialized field overwritten");
7991 p
->origtype
= origtype
;
7997 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
7998 sizeof (struct init_node
));
7999 r
->purpose
= purpose
;
8001 r
->origtype
= origtype
;
8011 struct init_node
*s
;
8015 if (p
->balance
== 0)
8017 else if (p
->balance
< 0)
8024 p
->left
->parent
= p
;
8041 constructor_pending_elts
= r
;
8046 struct init_node
*t
= r
->right
;
8050 r
->right
->parent
= r
;
8055 p
->left
->parent
= p
;
8058 p
->balance
= t
->balance
< 0;
8059 r
->balance
= -(t
->balance
> 0);
8074 constructor_pending_elts
= t
;
8080 /* p->balance == +1; growth of left side balances the node. */
8085 else /* r == p->right */
8087 if (p
->balance
== 0)
8088 /* Growth propagation from right side. */
8090 else if (p
->balance
> 0)
8097 p
->right
->parent
= p
;
8114 constructor_pending_elts
= r
;
8116 else /* r->balance == -1 */
8119 struct init_node
*t
= r
->left
;
8123 r
->left
->parent
= r
;
8128 p
->right
->parent
= p
;
8131 r
->balance
= (t
->balance
< 0);
8132 p
->balance
= -(t
->balance
> 0);
8147 constructor_pending_elts
= t
;
8153 /* p->balance == -1; growth of right side balances the node. */
8164 /* Build AVL tree from a sorted chain. */
8167 set_nonincremental_init (struct obstack
* braced_init_obstack
)
8169 unsigned HOST_WIDE_INT ix
;
8172 if (TREE_CODE (constructor_type
) != RECORD_TYPE
8173 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8176 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8177 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
8178 braced_init_obstack
);
8179 constructor_elements
= NULL
;
8180 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8182 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
8183 /* Skip any nameless bit fields at the beginning. */
8184 while (constructor_unfilled_fields
!= 0
8185 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8186 && DECL_NAME (constructor_unfilled_fields
) == 0)
8187 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
8190 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8192 if (TYPE_DOMAIN (constructor_type
))
8193 constructor_unfilled_index
8194 = convert (bitsizetype
,
8195 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8197 constructor_unfilled_index
= bitsize_zero_node
;
8199 constructor_incremental
= 0;
8202 /* Build AVL tree from a string constant. */
8205 set_nonincremental_init_from_string (tree str
,
8206 struct obstack
* braced_init_obstack
)
8208 tree value
, purpose
, type
;
8209 HOST_WIDE_INT val
[2];
8210 const char *p
, *end
;
8211 int byte
, wchar_bytes
, charwidth
, bitpos
;
8213 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8215 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8216 charwidth
= TYPE_PRECISION (char_type_node
);
8217 type
= TREE_TYPE (constructor_type
);
8218 p
= TREE_STRING_POINTER (str
);
8219 end
= p
+ TREE_STRING_LENGTH (str
);
8221 for (purpose
= bitsize_zero_node
;
8223 && !(constructor_max_index
8224 && tree_int_cst_lt (constructor_max_index
, purpose
));
8225 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8227 if (wchar_bytes
== 1)
8229 val
[0] = (unsigned char) *p
++;
8236 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8238 if (BYTES_BIG_ENDIAN
)
8239 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8241 bitpos
= byte
* charwidth
;
8242 val
[bitpos
% HOST_BITS_PER_WIDE_INT
]
8243 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8244 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8248 if (!TYPE_UNSIGNED (type
))
8250 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8251 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8253 if (val
[0] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
8255 val
[0] |= ((HOST_WIDE_INT
) -1) << bitpos
;
8259 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8264 else if (val
[1] & (((HOST_WIDE_INT
) 1)
8265 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8266 val
[1] |= ((HOST_WIDE_INT
) -1)
8267 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
8270 value
= wide_int_to_tree (type
,
8271 wide_int::from_array (val
, 2,
8272 HOST_BITS_PER_WIDE_INT
* 2));
8273 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
8274 braced_init_obstack
);
8277 constructor_incremental
= 0;
8280 /* Return value of FIELD in pending initializer or zero if the field was
8281 not initialized yet. */
8284 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8286 struct init_node
*p
;
8288 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8290 if (constructor_incremental
8291 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8292 set_nonincremental_init (braced_init_obstack
);
8294 p
= constructor_pending_elts
;
8297 if (tree_int_cst_lt (field
, p
->purpose
))
8299 else if (tree_int_cst_lt (p
->purpose
, field
))
8305 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8307 tree bitpos
= bit_position (field
);
8309 if (constructor_incremental
8310 && (!constructor_unfilled_fields
8311 || tree_int_cst_lt (bitpos
,
8312 bit_position (constructor_unfilled_fields
))))
8313 set_nonincremental_init (braced_init_obstack
);
8315 p
= constructor_pending_elts
;
8318 if (field
== p
->purpose
)
8320 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8326 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8328 if (!vec_safe_is_empty (constructor_elements
)
8329 && (constructor_elements
->last ().index
== field
))
8330 return constructor_elements
->last ().value
;
8335 /* "Output" the next constructor element.
8336 At top level, really output it to assembler code now.
8337 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8338 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8339 TYPE is the data type that the containing data type wants here.
8340 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8341 If VALUE is a string constant, STRICT_STRING is true if it is
8342 unparenthesized or we should not warn here for it being parenthesized.
8343 For other types of VALUE, STRICT_STRING is not used.
8345 PENDING if non-nil means output pending elements that belong
8346 right after this element. (PENDING is normally 1;
8347 it is 0 while outputting pending elements, to avoid recursion.)
8349 IMPLICIT is true if value comes from pop_init_level (1),
8350 the new initializer has been merged with the existing one
8351 and thus no warnings should be emitted about overriding an
8352 existing initializer. */
8355 output_init_element (location_t loc
, tree value
, tree origtype
,
8356 bool strict_string
, tree type
, tree field
, int pending
,
8357 bool implicit
, struct obstack
* braced_init_obstack
)
8359 tree semantic_type
= NULL_TREE
;
8360 bool maybe_const
= true;
8363 if (type
== error_mark_node
|| value
== error_mark_node
)
8365 constructor_erroneous
= 1;
8368 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8369 && (TREE_CODE (value
) == STRING_CST
8370 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8371 && !(TREE_CODE (value
) == STRING_CST
8372 && TREE_CODE (type
) == ARRAY_TYPE
8373 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8374 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8375 TYPE_MAIN_VARIANT (type
)))
8376 value
= array_to_pointer_conversion (input_location
, value
);
8378 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8379 && require_constant_value
&& pending
)
8381 /* As an extension, allow initializing objects with static storage
8382 duration with compound literals (which are then treated just as
8383 the brace enclosed list they contain). */
8385 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
8387 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8388 value
= DECL_INITIAL (decl
);
8391 npc
= null_pointer_constant_p (value
);
8392 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8394 semantic_type
= TREE_TYPE (value
);
8395 value
= TREE_OPERAND (value
, 0);
8397 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8399 if (value
== error_mark_node
)
8400 constructor_erroneous
= 1;
8401 else if (!TREE_CONSTANT (value
))
8402 constructor_constant
= 0;
8403 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
8404 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
8405 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8406 && DECL_C_BIT_FIELD (field
)
8407 && TREE_CODE (value
) != INTEGER_CST
))
8408 constructor_simple
= 0;
8410 constructor_nonconst
= 1;
8412 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8414 if (require_constant_value
)
8416 error_init (loc
, "initializer element is not constant");
8417 value
= error_mark_node
;
8419 else if (require_constant_elements
)
8420 pedwarn (loc
, OPT_Wpedantic
,
8421 "initializer element is not computable at load time");
8423 else if (!maybe_const
8424 && (require_constant_value
|| require_constant_elements
))
8425 pedwarn_init (loc
, OPT_Wpedantic
,
8426 "initializer element is not a constant expression");
8428 /* Issue -Wc++-compat warnings about initializing a bitfield with
8431 && field
!= NULL_TREE
8432 && TREE_CODE (field
) == FIELD_DECL
8433 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8434 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8435 != TYPE_MAIN_VARIANT (type
))
8436 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8438 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8439 if (checktype
!= error_mark_node
8440 && (TYPE_MAIN_VARIANT (checktype
)
8441 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8442 warning_init (loc
, OPT_Wc___compat
,
8443 "enum conversion in initialization is invalid in C++");
8446 /* If this field is empty (and not at the end of structure),
8447 don't do anything other than checking the initializer. */
8449 && (TREE_TYPE (field
) == error_mark_node
8450 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8451 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8452 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8453 || DECL_CHAIN (field
)))))
8457 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8458 value
= digest_init (loc
, type
, value
, origtype
, npc
, strict_string
,
8459 require_constant_value
);
8460 if (value
== error_mark_node
)
8462 constructor_erroneous
= 1;
8465 if (require_constant_value
|| require_constant_elements
)
8466 constant_expression_warning (value
);
8468 /* If this element doesn't come next in sequence,
8469 put it on constructor_pending_elts. */
8470 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8471 && (!constructor_incremental
8472 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8474 if (constructor_incremental
8475 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8476 set_nonincremental_init (braced_init_obstack
);
8478 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8479 braced_init_obstack
);
8482 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8483 && (!constructor_incremental
8484 || field
!= constructor_unfilled_fields
))
8486 /* We do this for records but not for unions. In a union,
8487 no matter which field is specified, it can be initialized
8488 right away since it starts at the beginning of the union. */
8489 if (constructor_incremental
)
8491 if (!constructor_unfilled_fields
)
8492 set_nonincremental_init (braced_init_obstack
);
8495 tree bitpos
, unfillpos
;
8497 bitpos
= bit_position (field
);
8498 unfillpos
= bit_position (constructor_unfilled_fields
);
8500 if (tree_int_cst_lt (bitpos
, unfillpos
))
8501 set_nonincremental_init (braced_init_obstack
);
8505 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8506 braced_init_obstack
);
8509 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8510 && !vec_safe_is_empty (constructor_elements
))
8514 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8515 warning_init (loc
, OPT_Woverride_init_side_effects
,
8516 "initialized field with side-effects overwritten");
8517 else if (warn_override_init
)
8518 warning_init (loc
, OPT_Woverride_init
,
8519 "initialized field overwritten");
8522 /* We can have just one union field set. */
8523 constructor_elements
= NULL
;
8526 /* Otherwise, output this element either to
8527 constructor_elements or to the assembler file. */
8529 constructor_elt celt
= {field
, value
};
8530 vec_safe_push (constructor_elements
, celt
);
8532 /* Advance the variable that indicates sequential elements output. */
8533 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8534 constructor_unfilled_index
8535 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8537 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8539 constructor_unfilled_fields
8540 = DECL_CHAIN (constructor_unfilled_fields
);
8542 /* Skip any nameless bit fields. */
8543 while (constructor_unfilled_fields
!= 0
8544 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8545 && DECL_NAME (constructor_unfilled_fields
) == 0)
8546 constructor_unfilled_fields
=
8547 DECL_CHAIN (constructor_unfilled_fields
);
8549 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8550 constructor_unfilled_fields
= 0;
8552 /* Now output any pending elements which have become next. */
8554 output_pending_init_elements (0, braced_init_obstack
);
8557 /* Output any pending elements which have become next.
8558 As we output elements, constructor_unfilled_{fields,index}
8559 advances, which may cause other elements to become next;
8560 if so, they too are output.
8562 If ALL is 0, we return when there are
8563 no more pending elements to output now.
8565 If ALL is 1, we output space as necessary so that
8566 we can output all the pending elements. */
8568 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8570 struct init_node
*elt
= constructor_pending_elts
;
8575 /* Look through the whole pending tree.
8576 If we find an element that should be output now,
8577 output it. Otherwise, set NEXT to the element
8578 that comes first among those still pending. */
8583 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8585 if (tree_int_cst_equal (elt
->purpose
,
8586 constructor_unfilled_index
))
8587 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8588 true, TREE_TYPE (constructor_type
),
8589 constructor_unfilled_index
, 0, false,
8590 braced_init_obstack
);
8591 else if (tree_int_cst_lt (constructor_unfilled_index
,
8594 /* Advance to the next smaller node. */
8599 /* We have reached the smallest node bigger than the
8600 current unfilled index. Fill the space first. */
8601 next
= elt
->purpose
;
8607 /* Advance to the next bigger node. */
8612 /* We have reached the biggest node in a subtree. Find
8613 the parent of it, which is the next bigger node. */
8614 while (elt
->parent
&& elt
->parent
->right
== elt
)
8617 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
8620 next
= elt
->purpose
;
8626 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8627 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8629 tree ctor_unfilled_bitpos
, elt_bitpos
;
8631 /* If the current record is complete we are done. */
8632 if (constructor_unfilled_fields
== 0)
8635 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
8636 elt_bitpos
= bit_position (elt
->purpose
);
8637 /* We can't compare fields here because there might be empty
8638 fields in between. */
8639 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
8641 constructor_unfilled_fields
= elt
->purpose
;
8642 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8643 true, TREE_TYPE (elt
->purpose
),
8644 elt
->purpose
, 0, false,
8645 braced_init_obstack
);
8647 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
8649 /* Advance to the next smaller node. */
8654 /* We have reached the smallest node bigger than the
8655 current unfilled field. Fill the space first. */
8656 next
= elt
->purpose
;
8662 /* Advance to the next bigger node. */
8667 /* We have reached the biggest node in a subtree. Find
8668 the parent of it, which is the next bigger node. */
8669 while (elt
->parent
&& elt
->parent
->right
== elt
)
8673 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
8674 bit_position (elt
->purpose
))))
8676 next
= elt
->purpose
;
8684 /* Ordinarily return, but not if we want to output all
8685 and there are elements left. */
8686 if (!(all
&& next
!= 0))
8689 /* If it's not incremental, just skip over the gap, so that after
8690 jumping to retry we will output the next successive element. */
8691 if (TREE_CODE (constructor_type
) == RECORD_TYPE
8692 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8693 constructor_unfilled_fields
= next
;
8694 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8695 constructor_unfilled_index
= next
;
8697 /* ELT now points to the node in the pending tree with the next
8698 initializer to output. */
8702 /* Add one non-braced element to the current constructor level.
8703 This adjusts the current position within the constructor's type.
8704 This may also start or terminate implicit levels
8705 to handle a partly-braced initializer.
8707 Once this has found the correct level for the new element,
8708 it calls output_init_element.
8710 IMPLICIT is true if value comes from pop_init_level (1),
8711 the new initializer has been merged with the existing one
8712 and thus no warnings should be emitted about overriding an
8713 existing initializer. */
8716 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
8717 struct obstack
* braced_init_obstack
)
8719 tree orig_value
= value
.value
;
8720 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
8721 bool strict_string
= value
.original_code
== STRING_CST
;
8722 bool was_designated
= designator_depth
!= 0;
8724 designator_depth
= 0;
8725 designator_erroneous
= 0;
8727 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
8728 constructor_zeroinit
= 0;
8730 /* Handle superfluous braces around string cst as in
8731 char x[] = {"foo"}; */
8735 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8736 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
8737 && integer_zerop (constructor_unfilled_index
))
8739 if (constructor_stack
->replacement_value
.value
)
8740 error_init (loc
, "excess elements in char array initializer");
8741 constructor_stack
->replacement_value
= value
;
8745 if (constructor_stack
->replacement_value
.value
!= 0)
8747 error_init (loc
, "excess elements in struct initializer");
8751 /* Ignore elements of a brace group if it is entirely superfluous
8752 and has already been diagnosed. */
8753 if (constructor_type
== 0)
8756 if (!implicit
&& warn_designated_init
&& !was_designated
8757 && TREE_CODE (constructor_type
) == RECORD_TYPE
8758 && lookup_attribute ("designated_init",
8759 TYPE_ATTRIBUTES (constructor_type
)))
8761 OPT_Wdesignated_init
,
8762 "positional initialization of field "
8763 "in %<struct%> declared with %<designated_init%> attribute");
8765 /* If we've exhausted any levels that didn't have braces,
8767 while (constructor_stack
->implicit
)
8769 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
8770 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8771 && constructor_fields
== 0)
8772 process_init_element (loc
,
8773 pop_init_level (loc
, 1, braced_init_obstack
),
8774 true, braced_init_obstack
);
8775 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
8776 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8777 && constructor_max_index
8778 && tree_int_cst_lt (constructor_max_index
,
8780 process_init_element (loc
,
8781 pop_init_level (loc
, 1, braced_init_obstack
),
8782 true, braced_init_obstack
);
8787 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8788 if (constructor_range_stack
)
8790 /* If value is a compound literal and we'll be just using its
8791 content, don't put it into a SAVE_EXPR. */
8792 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
8793 || !require_constant_value
)
8795 tree semantic_type
= NULL_TREE
;
8796 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
8798 semantic_type
= TREE_TYPE (value
.value
);
8799 value
.value
= TREE_OPERAND (value
.value
, 0);
8801 value
.value
= c_save_expr (value
.value
);
8803 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
8810 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8813 enum tree_code fieldcode
;
8815 if (constructor_fields
== 0)
8817 pedwarn_init (loc
, 0, "excess elements in struct initializer");
8821 fieldtype
= TREE_TYPE (constructor_fields
);
8822 if (fieldtype
!= error_mark_node
)
8823 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8824 fieldcode
= TREE_CODE (fieldtype
);
8826 /* Error for non-static initialization of a flexible array member. */
8827 if (fieldcode
== ARRAY_TYPE
8828 && !require_constant_value
8829 && TYPE_SIZE (fieldtype
) == NULL_TREE
8830 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8832 error_init (loc
, "non-static initialization of a flexible "
8837 /* Error for initialization of a flexible array member with
8838 a string constant if the structure is in an array. E.g.:
8839 struct S { int x; char y[]; };
8840 struct S s[] = { { 1, "foo" } };
8843 && fieldcode
== ARRAY_TYPE
8844 && constructor_depth
> 1
8845 && TYPE_SIZE (fieldtype
) == NULL_TREE
8846 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8848 bool in_array_p
= false;
8849 for (struct constructor_stack
*p
= constructor_stack
;
8850 p
&& p
->type
; p
= p
->next
)
8851 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
8858 error_init (loc
, "initialization of flexible array "
8859 "member in a nested context");
8864 /* Accept a string constant to initialize a subarray. */
8865 if (value
.value
!= 0
8866 && fieldcode
== ARRAY_TYPE
8867 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8869 value
.value
= orig_value
;
8870 /* Otherwise, if we have come to a subaggregate,
8871 and we don't have an element of its type, push into it. */
8872 else if (value
.value
!= 0
8873 && value
.value
!= error_mark_node
8874 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8875 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8876 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8878 push_init_level (loc
, 1, braced_init_obstack
);
8884 push_member_name (constructor_fields
);
8885 output_init_element (loc
, value
.value
, value
.original_type
,
8886 strict_string
, fieldtype
,
8887 constructor_fields
, 1, implicit
,
8888 braced_init_obstack
);
8889 RESTORE_SPELLING_DEPTH (constructor_depth
);
8892 /* Do the bookkeeping for an element that was
8893 directly output as a constructor. */
8895 /* For a record, keep track of end position of last field. */
8896 if (DECL_SIZE (constructor_fields
))
8897 constructor_bit_index
8898 = size_binop_loc (input_location
, PLUS_EXPR
,
8899 bit_position (constructor_fields
),
8900 DECL_SIZE (constructor_fields
));
8902 /* If the current field was the first one not yet written out,
8903 it isn't now, so update. */
8904 if (constructor_unfilled_fields
== constructor_fields
)
8906 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8907 /* Skip any nameless bit fields. */
8908 while (constructor_unfilled_fields
!= 0
8909 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8910 && DECL_NAME (constructor_unfilled_fields
) == 0)
8911 constructor_unfilled_fields
=
8912 DECL_CHAIN (constructor_unfilled_fields
);
8916 constructor_fields
= DECL_CHAIN (constructor_fields
);
8917 /* Skip any nameless bit fields at the beginning. */
8918 while (constructor_fields
!= 0
8919 && DECL_C_BIT_FIELD (constructor_fields
)
8920 && DECL_NAME (constructor_fields
) == 0)
8921 constructor_fields
= DECL_CHAIN (constructor_fields
);
8923 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8926 enum tree_code fieldcode
;
8928 if (constructor_fields
== 0)
8930 pedwarn_init (loc
, 0,
8931 "excess elements in union initializer");
8935 fieldtype
= TREE_TYPE (constructor_fields
);
8936 if (fieldtype
!= error_mark_node
)
8937 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8938 fieldcode
= TREE_CODE (fieldtype
);
8940 /* Warn that traditional C rejects initialization of unions.
8941 We skip the warning if the value is zero. This is done
8942 under the assumption that the zero initializer in user
8943 code appears conditioned on e.g. __STDC__ to avoid
8944 "missing initializer" warnings and relies on default
8945 initialization to zero in the traditional C case.
8946 We also skip the warning if the initializer is designated,
8947 again on the assumption that this must be conditional on
8948 __STDC__ anyway (and we've already complained about the
8949 member-designator already). */
8950 if (!in_system_header_at (input_location
) && !constructor_designated
8951 && !(value
.value
&& (integer_zerop (value
.value
)
8952 || real_zerop (value
.value
))))
8953 warning (OPT_Wtraditional
, "traditional C rejects initialization "
8956 /* Accept a string constant to initialize a subarray. */
8957 if (value
.value
!= 0
8958 && fieldcode
== ARRAY_TYPE
8959 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8961 value
.value
= orig_value
;
8962 /* Otherwise, if we have come to a subaggregate,
8963 and we don't have an element of its type, push into it. */
8964 else if (value
.value
!= 0
8965 && value
.value
!= error_mark_node
8966 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8967 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8968 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8970 push_init_level (loc
, 1, braced_init_obstack
);
8976 push_member_name (constructor_fields
);
8977 output_init_element (loc
, value
.value
, value
.original_type
,
8978 strict_string
, fieldtype
,
8979 constructor_fields
, 1, implicit
,
8980 braced_init_obstack
);
8981 RESTORE_SPELLING_DEPTH (constructor_depth
);
8984 /* Do the bookkeeping for an element that was
8985 directly output as a constructor. */
8987 constructor_bit_index
= DECL_SIZE (constructor_fields
);
8988 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8991 constructor_fields
= 0;
8993 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8995 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8996 enum tree_code eltcode
= TREE_CODE (elttype
);
8998 /* Accept a string constant to initialize a subarray. */
8999 if (value
.value
!= 0
9000 && eltcode
== ARRAY_TYPE
9001 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
9003 value
.value
= orig_value
;
9004 /* Otherwise, if we have come to a subaggregate,
9005 and we don't have an element of its type, push into it. */
9006 else if (value
.value
!= 0
9007 && value
.value
!= error_mark_node
9008 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
9009 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
9010 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
9012 push_init_level (loc
, 1, braced_init_obstack
);
9016 if (constructor_max_index
!= 0
9017 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
9018 || integer_all_onesp (constructor_max_index
)))
9020 pedwarn_init (loc
, 0,
9021 "excess elements in array initializer");
9025 /* Now output the actual element. */
9028 push_array_bounds (tree_to_uhwi (constructor_index
));
9029 output_init_element (loc
, value
.value
, value
.original_type
,
9030 strict_string
, elttype
,
9031 constructor_index
, 1, implicit
,
9032 braced_init_obstack
);
9033 RESTORE_SPELLING_DEPTH (constructor_depth
);
9037 = size_binop_loc (input_location
, PLUS_EXPR
,
9038 constructor_index
, bitsize_one_node
);
9041 /* If we are doing the bookkeeping for an element that was
9042 directly output as a constructor, we must update
9043 constructor_unfilled_index. */
9044 constructor_unfilled_index
= constructor_index
;
9046 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
9048 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9050 /* Do a basic check of initializer size. Note that vectors
9051 always have a fixed size derived from their type. */
9052 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
9054 pedwarn_init (loc
, 0,
9055 "excess elements in vector initializer");
9059 /* Now output the actual element. */
9062 if (TREE_CODE (value
.value
) == VECTOR_CST
)
9063 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
9064 output_init_element (loc
, value
.value
, value
.original_type
,
9065 strict_string
, elttype
,
9066 constructor_index
, 1, implicit
,
9067 braced_init_obstack
);
9071 = size_binop_loc (input_location
,
9072 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
9075 /* If we are doing the bookkeeping for an element that was
9076 directly output as a constructor, we must update
9077 constructor_unfilled_index. */
9078 constructor_unfilled_index
= constructor_index
;
9081 /* Handle the sole element allowed in a braced initializer
9082 for a scalar variable. */
9083 else if (constructor_type
!= error_mark_node
9084 && constructor_fields
== 0)
9086 pedwarn_init (loc
, 0,
9087 "excess elements in scalar initializer");
9093 output_init_element (loc
, value
.value
, value
.original_type
,
9094 strict_string
, constructor_type
,
9095 NULL_TREE
, 1, implicit
,
9096 braced_init_obstack
);
9097 constructor_fields
= 0;
9100 /* Handle range initializers either at this level or anywhere higher
9101 in the designator stack. */
9102 if (constructor_range_stack
)
9104 struct constructor_range_stack
*p
, *range_stack
;
9107 range_stack
= constructor_range_stack
;
9108 constructor_range_stack
= 0;
9109 while (constructor_stack
!= range_stack
->stack
)
9111 gcc_assert (constructor_stack
->implicit
);
9112 process_init_element (loc
,
9113 pop_init_level (loc
, 1,
9114 braced_init_obstack
),
9115 true, braced_init_obstack
);
9117 for (p
= range_stack
;
9118 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
9121 gcc_assert (constructor_stack
->implicit
);
9122 process_init_element (loc
,
9123 pop_init_level (loc
, 1,
9124 braced_init_obstack
),
9125 true, braced_init_obstack
);
9128 p
->index
= size_binop_loc (input_location
,
9129 PLUS_EXPR
, p
->index
, bitsize_one_node
);
9130 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
9135 constructor_index
= p
->index
;
9136 constructor_fields
= p
->fields
;
9137 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
9145 push_init_level (loc
, 2, braced_init_obstack
);
9146 p
->stack
= constructor_stack
;
9147 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
9148 p
->index
= p
->range_start
;
9152 constructor_range_stack
= range_stack
;
9159 constructor_range_stack
= 0;
9162 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9163 (guaranteed to be 'volatile' or null) and ARGS (represented using
9164 an ASM_EXPR node). */
9166 build_asm_stmt (tree cv_qualifier
, tree args
)
9168 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
9169 ASM_VOLATILE_P (args
) = 1;
9170 return add_stmt (args
);
9173 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9174 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9175 SIMPLE indicates whether there was anything at all after the
9176 string in the asm expression -- asm("blah") and asm("blah" : )
9177 are subtly different. We use a ASM_EXPR node to represent this. */
9179 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
9180 tree clobbers
, tree labels
, bool simple
)
9185 const char *constraint
;
9186 const char **oconstraints
;
9187 bool allows_mem
, allows_reg
, is_inout
;
9188 int ninputs
, noutputs
;
9190 ninputs
= list_length (inputs
);
9191 noutputs
= list_length (outputs
);
9192 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
9194 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
9196 /* Remove output conversions that change the type but not the mode. */
9197 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9199 tree output
= TREE_VALUE (tail
);
9201 output
= c_fully_fold (output
, false, NULL
);
9203 /* ??? Really, this should not be here. Users should be using a
9204 proper lvalue, dammit. But there's a long history of using casts
9205 in the output operands. In cases like longlong.h, this becomes a
9206 primitive form of typechecking -- if the cast can be removed, then
9207 the output operand had a type of the proper width; otherwise we'll
9208 get an error. Gross, but ... */
9209 STRIP_NOPS (output
);
9211 if (!lvalue_or_else (loc
, output
, lv_asm
))
9212 output
= error_mark_node
;
9214 if (output
!= error_mark_node
9215 && (TREE_READONLY (output
)
9216 || TYPE_READONLY (TREE_TYPE (output
))
9217 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
9218 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
9219 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
9220 readonly_error (loc
, output
, lv_asm
);
9222 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9223 oconstraints
[i
] = constraint
;
9225 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
9226 &allows_mem
, &allows_reg
, &is_inout
))
9228 /* If the operand is going to end up in memory,
9229 mark it addressable. */
9230 if (!allows_reg
&& !c_mark_addressable (output
))
9231 output
= error_mark_node
;
9232 if (!(!allows_reg
&& allows_mem
)
9233 && output
!= error_mark_node
9234 && VOID_TYPE_P (TREE_TYPE (output
)))
9236 error_at (loc
, "invalid use of void expression");
9237 output
= error_mark_node
;
9241 output
= error_mark_node
;
9243 TREE_VALUE (tail
) = output
;
9246 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9250 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9251 input
= TREE_VALUE (tail
);
9253 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
9254 oconstraints
, &allows_mem
, &allows_reg
))
9256 /* If the operand is going to end up in memory,
9257 mark it addressable. */
9258 if (!allows_reg
&& allows_mem
)
9260 input
= c_fully_fold (input
, false, NULL
);
9262 /* Strip the nops as we allow this case. FIXME, this really
9263 should be rejected or made deprecated. */
9265 if (!c_mark_addressable (input
))
9266 input
= error_mark_node
;
9271 memset (&expr
, 0, sizeof (expr
));
9273 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9274 input
= c_fully_fold (expr
.value
, false, NULL
);
9276 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9278 error_at (loc
, "invalid use of void expression");
9279 input
= error_mark_node
;
9284 input
= error_mark_node
;
9286 TREE_VALUE (tail
) = input
;
9289 /* ASMs with labels cannot have outputs. This should have been
9290 enforced by the parser. */
9291 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9293 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9295 /* asm statements without outputs, including simple ones, are treated
9297 ASM_INPUT_P (args
) = simple
;
9298 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9303 /* Generate a goto statement to LABEL. LOC is the location of the
9307 c_finish_goto_label (location_t loc
, tree label
)
9309 tree decl
= lookup_label_for_goto (loc
, label
);
9312 TREE_USED (decl
) = 1;
9314 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9315 SET_EXPR_LOCATION (t
, loc
);
9316 return add_stmt (t
);
9320 /* Generate a computed goto statement to EXPR. LOC is the location of
9324 c_finish_goto_ptr (location_t loc
, tree expr
)
9327 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9328 expr
= c_fully_fold (expr
, false, NULL
);
9329 expr
= convert (ptr_type_node
, expr
);
9330 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9331 SET_EXPR_LOCATION (t
, loc
);
9332 return add_stmt (t
);
9335 /* Generate a C `return' statement. RETVAL is the expression for what
9336 to return, or a null pointer for `return;' with no value. LOC is
9337 the location of the return statement, or the location of the expression,
9338 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9339 is the original type of RETVAL. */
9342 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9344 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9345 bool no_warning
= false;
9349 if (TREE_THIS_VOLATILE (current_function_decl
))
9351 "function declared %<noreturn%> has a %<return%> statement");
9353 if (flag_cilkplus
&& contains_array_notation_expr (retval
))
9355 /* Array notations are allowed in a return statement if it is inside a
9356 built-in array notation reduction function. */
9357 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9358 return error_mark_node
;
9361 error_at (loc
, "array notation expression cannot be used as a "
9363 return error_mark_node
;
9366 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
9368 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9370 return error_mark_node
;
9374 tree semantic_type
= NULL_TREE
;
9375 npc
= null_pointer_constant_p (retval
);
9376 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9378 semantic_type
= TREE_TYPE (retval
);
9379 retval
= TREE_OPERAND (retval
, 0);
9381 retval
= c_fully_fold (retval
, false, NULL
);
9383 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9388 current_function_returns_null
= 1;
9389 if ((warn_return_type
|| flag_isoc99
)
9390 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9393 pedwarn (loc
, 0, "%<return%> with no value, in "
9394 "function returning non-void");
9396 warning_at (loc
, OPT_Wreturn_type
, "%<return%> with no value, "
9397 "in function returning non-void");
9401 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9403 current_function_returns_null
= 1;
9404 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9406 "%<return%> with a value, in function returning void");
9408 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
9409 "%<return%> with expression, in function returning void");
9413 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
9414 retval
, origtype
, ic_return
,
9415 npc
, NULL_TREE
, NULL_TREE
, 0);
9416 tree res
= DECL_RESULT (current_function_decl
);
9420 current_function_returns_value
= 1;
9421 if (t
== error_mark_node
)
9424 save
= in_late_binary_op
;
9425 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9426 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
9427 || (TREE_CODE (TREE_TYPE (t
)) == REAL_TYPE
9428 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
9429 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
9430 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
9431 in_late_binary_op
= true;
9432 inner
= t
= convert (TREE_TYPE (res
), t
);
9433 in_late_binary_op
= save
;
9435 /* Strip any conversions, additions, and subtractions, and see if
9436 we are returning the address of a local variable. Warn if so. */
9439 switch (TREE_CODE (inner
))
9442 case NON_LVALUE_EXPR
:
9444 case POINTER_PLUS_EXPR
:
9445 inner
= TREE_OPERAND (inner
, 0);
9449 /* If the second operand of the MINUS_EXPR has a pointer
9450 type (or is converted from it), this may be valid, so
9451 don't give a warning. */
9453 tree op1
= TREE_OPERAND (inner
, 1);
9455 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9456 && (CONVERT_EXPR_P (op1
)
9457 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9458 op1
= TREE_OPERAND (op1
, 0);
9460 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9463 inner
= TREE_OPERAND (inner
, 0);
9468 inner
= TREE_OPERAND (inner
, 0);
9470 while (REFERENCE_CLASS_P (inner
)
9471 && TREE_CODE (inner
) != INDIRECT_REF
)
9472 inner
= TREE_OPERAND (inner
, 0);
9475 && !DECL_EXTERNAL (inner
)
9476 && !TREE_STATIC (inner
)
9477 && DECL_CONTEXT (inner
) == current_function_decl
)
9479 if (TREE_CODE (inner
) == LABEL_DECL
)
9480 warning_at (loc
, OPT_Wreturn_local_addr
,
9481 "function returns address of label");
9484 warning_at (loc
, OPT_Wreturn_local_addr
,
9485 "function returns address of local variable");
9486 tree zero
= build_zero_cst (TREE_TYPE (res
));
9487 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
9499 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9500 SET_EXPR_LOCATION (retval
, loc
);
9502 if (warn_sequence_point
)
9503 verify_sequence_points (retval
);
9506 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9507 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9508 return add_stmt (ret_stmt
);
9512 /* The SWITCH_EXPR being built. */
9515 /* The original type of the testing expression, i.e. before the
9516 default conversion is applied. */
9519 /* A splay-tree mapping the low element of a case range to the high
9520 element, or NULL_TREE if there is no high element. Used to
9521 determine whether or not a new case label duplicates an old case
9522 label. We need a tree, rather than simply a hash table, because
9523 of the GNU case range extension. */
9526 /* The bindings at the point of the switch. This is used for
9527 warnings crossing decls when branching to a case label. */
9528 struct c_spot_bindings
*bindings
;
9530 /* The next node on the stack. */
9531 struct c_switch
*next
;
9534 /* A stack of the currently active switch statements. The innermost
9535 switch statement is on the top of the stack. There is no need to
9536 mark the stack for garbage collection because it is only active
9537 during the processing of the body of a function, and we never
9538 collect at that point. */
9540 struct c_switch
*c_switch_stack
;
9542 /* Start a C switch statement, testing expression EXP. Return the new
9543 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9544 SWITCH_COND_LOC is the location of the switch's condition.
9545 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9548 c_start_case (location_t switch_loc
,
9549 location_t switch_cond_loc
,
9550 tree exp
, bool explicit_cast_p
)
9552 tree orig_type
= error_mark_node
;
9553 struct c_switch
*cs
;
9555 if (exp
!= error_mark_node
)
9557 orig_type
= TREE_TYPE (exp
);
9559 if (!INTEGRAL_TYPE_P (orig_type
))
9561 if (orig_type
!= error_mark_node
)
9563 error_at (switch_cond_loc
, "switch quantity not an integer");
9564 orig_type
= error_mark_node
;
9566 exp
= integer_zero_node
;
9570 tree type
= TYPE_MAIN_VARIANT (orig_type
);
9573 /* Warn if the condition has boolean value. */
9574 while (TREE_CODE (e
) == COMPOUND_EXPR
)
9575 e
= TREE_OPERAND (e
, 1);
9577 if ((TREE_CODE (type
) == BOOLEAN_TYPE
9578 || truth_value_p (TREE_CODE (e
)))
9579 /* Explicit cast to int suppresses this warning. */
9580 && !(TREE_CODE (type
) == INTEGER_TYPE
9581 && explicit_cast_p
))
9582 warning_at (switch_cond_loc
, OPT_Wswitch_bool
,
9583 "switch condition has boolean value");
9585 if (!in_system_header_at (input_location
)
9586 && (type
== long_integer_type_node
9587 || type
== long_unsigned_type_node
))
9588 warning_at (switch_cond_loc
,
9589 OPT_Wtraditional
, "%<long%> switch expression not "
9590 "converted to %<int%> in ISO C");
9592 exp
= c_fully_fold (exp
, false, NULL
);
9593 exp
= default_conversion (exp
);
9595 if (warn_sequence_point
)
9596 verify_sequence_points (exp
);
9600 /* Add this new SWITCH_EXPR to the stack. */
9601 cs
= XNEW (struct c_switch
);
9602 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
9603 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
9604 cs
->orig_type
= orig_type
;
9605 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
9606 cs
->bindings
= c_get_switch_bindings ();
9607 cs
->next
= c_switch_stack
;
9608 c_switch_stack
= cs
;
9610 return add_stmt (cs
->switch_expr
);
9613 /* Process a case label at location LOC. */
9616 do_case (location_t loc
, tree low_value
, tree high_value
)
9618 tree label
= NULL_TREE
;
9620 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
9622 low_value
= c_fully_fold (low_value
, false, NULL
);
9623 if (TREE_CODE (low_value
) == INTEGER_CST
)
9624 pedwarn (loc
, OPT_Wpedantic
,
9625 "case label is not an integer constant expression");
9628 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
9630 high_value
= c_fully_fold (high_value
, false, NULL
);
9631 if (TREE_CODE (high_value
) == INTEGER_CST
)
9632 pedwarn (input_location
, OPT_Wpedantic
,
9633 "case label is not an integer constant expression");
9636 if (c_switch_stack
== NULL
)
9639 error_at (loc
, "case label not within a switch statement");
9641 error_at (loc
, "%<default%> label not within a switch statement");
9645 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
9646 EXPR_LOCATION (c_switch_stack
->switch_expr
),
9650 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
9651 SWITCH_COND (c_switch_stack
->switch_expr
),
9652 c_switch_stack
->orig_type
,
9653 low_value
, high_value
);
9654 if (label
== error_mark_node
)
9659 /* Finish the switch statement. TYPE is the original type of the
9660 controlling expression of the switch, or NULL_TREE. */
9663 c_finish_case (tree body
, tree type
)
9665 struct c_switch
*cs
= c_switch_stack
;
9666 location_t switch_location
;
9668 SWITCH_BODY (cs
->switch_expr
) = body
;
9670 /* Emit warnings as needed. */
9671 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
9672 c_do_switch_warnings (cs
->cases
, switch_location
,
9673 type
? type
: TREE_TYPE (cs
->switch_expr
),
9674 SWITCH_COND (cs
->switch_expr
));
9676 /* Pop the stack. */
9677 c_switch_stack
= cs
->next
;
9678 splay_tree_delete (cs
->cases
);
9679 c_release_switch_bindings (cs
->bindings
);
9683 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9684 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9685 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9686 statement, and was not surrounded with parenthesis. */
9689 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
9690 tree else_block
, bool nested_if
)
9694 /* If the condition has array notations, then the rank of the then_block and
9695 else_block must be either 0 or be equal to the rank of the condition. If
9696 the condition does not have array notations then break them up as it is
9697 broken up in a normal expression. */
9698 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
9700 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
9701 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
9704 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
9707 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
9709 if (cond_rank
!= then_rank
&& then_rank
!= 0)
9711 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9712 " and the then-block");
9715 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
9717 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9718 " and the else-block");
9722 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9723 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
9725 tree inner_if
= then_block
;
9727 /* We know from the grammar productions that there is an IF nested
9728 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9729 it might not be exactly THEN_BLOCK, but should be the last
9730 non-container statement within. */
9732 switch (TREE_CODE (inner_if
))
9737 inner_if
= BIND_EXPR_BODY (inner_if
);
9739 case STATEMENT_LIST
:
9740 inner_if
= expr_last (then_block
);
9742 case TRY_FINALLY_EXPR
:
9743 case TRY_CATCH_EXPR
:
9744 inner_if
= TREE_OPERAND (inner_if
, 0);
9751 if (COND_EXPR_ELSE (inner_if
))
9752 warning_at (if_locus
, OPT_Wparentheses
,
9753 "suggest explicit braces to avoid ambiguous %<else%>");
9756 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
9757 SET_EXPR_LOCATION (stmt
, if_locus
);
9761 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9762 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9763 is false for DO loops. INCR is the FOR increment expression. BODY is
9764 the statement controlled by the loop. BLAB is the break label. CLAB is
9765 the continue label. Everything is allowed to be NULL. */
9768 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
9769 tree blab
, tree clab
, bool cond_is_first
)
9771 tree entry
= NULL
, exit
= NULL
, t
;
9773 /* In theory could forbid cilk spawn for loop increment expression,
9774 but it should work just fine. */
9776 /* If the condition is zero don't generate a loop construct. */
9777 if (cond
&& integer_zerop (cond
))
9781 t
= build_and_jump (&blab
);
9782 SET_EXPR_LOCATION (t
, start_locus
);
9788 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9790 /* If we have an exit condition, then we build an IF with gotos either
9791 out of the loop, or to the top of it. If there's no exit condition,
9792 then we just build a jump back to the top. */
9793 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
9795 if (cond
&& !integer_nonzerop (cond
))
9797 /* Canonicalize the loop condition to the end. This means
9798 generating a branch to the loop condition. Reuse the
9799 continue label, if possible. */
9804 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9805 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
9808 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
9809 SET_EXPR_LOCATION (t
, start_locus
);
9813 t
= build_and_jump (&blab
);
9815 exit
= fold_build3_loc (start_locus
,
9816 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9818 exit
= fold_build3_loc (input_location
,
9819 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9828 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
9836 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
9840 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
9843 tree label
= *label_p
;
9845 /* In switch statements break is sometimes stylistically used after
9846 a return statement. This can lead to spurious warnings about
9847 control reaching the end of a non-void function when it is
9848 inlined. Note that we are calling block_may_fallthru with
9849 language specific tree nodes; this works because
9850 block_may_fallthru returns true when given something it does not
9852 skip
= !block_may_fallthru (cur_stmt_list
);
9857 *label_p
= label
= create_artificial_label (loc
);
9859 else if (TREE_CODE (label
) == LABEL_DECL
)
9861 else switch (TREE_INT_CST_LOW (label
))
9865 error_at (loc
, "break statement not within loop or switch");
9867 error_at (loc
, "continue statement not within a loop");
9871 gcc_assert (is_break
);
9872 error_at (loc
, "break statement used with OpenMP for loop");
9877 error ("break statement within %<#pragma simd%> loop body");
9879 error ("continue statement within %<#pragma simd%> loop body");
9890 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
9892 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
9895 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9898 emit_side_effect_warnings (location_t loc
, tree expr
)
9900 if (expr
== error_mark_node
)
9902 else if (!TREE_SIDE_EFFECTS (expr
))
9904 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
9905 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
9907 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
9910 location_t cloc
= loc
;
9911 while (TREE_CODE (r
) == COMPOUND_EXPR
)
9913 if (EXPR_HAS_LOCATION (r
))
9914 cloc
= EXPR_LOCATION (r
);
9915 r
= TREE_OPERAND (r
, 1);
9917 if (!TREE_SIDE_EFFECTS (r
)
9918 && !VOID_TYPE_P (TREE_TYPE (r
))
9919 && !CONVERT_EXPR_P (r
)
9920 && !TREE_NO_WARNING (r
)
9921 && !TREE_NO_WARNING (expr
))
9922 warning_at (cloc
, OPT_Wunused_value
,
9923 "right-hand operand of comma expression has no effect");
9926 warn_if_unused_value (expr
, loc
);
9929 /* Process an expression as if it were a complete statement. Emit
9930 diagnostics, but do not call ADD_STMT. LOC is the location of the
9934 c_process_expr_stmt (location_t loc
, tree expr
)
9941 expr
= c_fully_fold (expr
, false, NULL
);
9943 if (warn_sequence_point
)
9944 verify_sequence_points (expr
);
9946 if (TREE_TYPE (expr
) != error_mark_node
9947 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
9948 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
9949 error_at (loc
, "expression statement has incomplete type");
9951 /* If we're not processing a statement expression, warn about unused values.
9952 Warnings for statement expressions will be emitted later, once we figure
9953 out which is the result. */
9954 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9955 && warn_unused_value
)
9956 emit_side_effect_warnings (loc
, expr
);
9959 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
9960 exprv
= TREE_OPERAND (exprv
, 1);
9961 while (CONVERT_EXPR_P (exprv
))
9962 exprv
= TREE_OPERAND (exprv
, 0);
9964 || handled_component_p (exprv
)
9965 || TREE_CODE (exprv
) == ADDR_EXPR
)
9966 mark_exp_read (exprv
);
9968 /* If the expression is not of a type to which we cannot assign a line
9969 number, wrap the thing in a no-op NOP_EXPR. */
9970 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
9972 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
9973 SET_EXPR_LOCATION (expr
, loc
);
9979 /* Emit an expression as a statement. LOC is the location of the
9983 c_finish_expr_stmt (location_t loc
, tree expr
)
9986 return add_stmt (c_process_expr_stmt (loc
, expr
));
9991 /* Do the opposite and emit a statement as an expression. To begin,
9992 create a new binding level and return it. */
9995 c_begin_stmt_expr (void)
9999 /* We must force a BLOCK for this level so that, if it is not expanded
10000 later, there is a way to turn off the entire subtree of blocks that
10001 are contained in it. */
10002 keep_next_level ();
10003 ret
= c_begin_compound_stmt (true);
10005 c_bindings_start_stmt_expr (c_switch_stack
== NULL
10007 : c_switch_stack
->bindings
);
10009 /* Mark the current statement list as belonging to a statement list. */
10010 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
10015 /* LOC is the location of the compound statement to which this body
10019 c_finish_stmt_expr (location_t loc
, tree body
)
10021 tree last
, type
, tmp
, val
;
10024 body
= c_end_compound_stmt (loc
, body
, true);
10026 c_bindings_end_stmt_expr (c_switch_stack
== NULL
10028 : c_switch_stack
->bindings
);
10030 /* Locate the last statement in BODY. See c_end_compound_stmt
10031 about always returning a BIND_EXPR. */
10032 last_p
= &BIND_EXPR_BODY (body
);
10033 last
= BIND_EXPR_BODY (body
);
10035 continue_searching
:
10036 if (TREE_CODE (last
) == STATEMENT_LIST
)
10038 tree_stmt_iterator i
;
10040 /* This can happen with degenerate cases like ({ }). No value. */
10041 if (!TREE_SIDE_EFFECTS (last
))
10044 /* If we're supposed to generate side effects warnings, process
10045 all of the statements except the last. */
10046 if (warn_unused_value
)
10048 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
10051 tree t
= tsi_stmt (i
);
10053 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
10054 emit_side_effect_warnings (tloc
, t
);
10058 i
= tsi_last (last
);
10059 last_p
= tsi_stmt_ptr (i
);
10063 /* If the end of the list is exception related, then the list was split
10064 by a call to push_cleanup. Continue searching. */
10065 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
10066 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
10068 last_p
= &TREE_OPERAND (last
, 0);
10070 goto continue_searching
;
10073 if (last
== error_mark_node
)
10076 /* In the case that the BIND_EXPR is not necessary, return the
10077 expression out from inside it. */
10078 if (last
== BIND_EXPR_BODY (body
)
10079 && BIND_EXPR_VARS (body
) == NULL
)
10081 /* Even if this looks constant, do not allow it in a constant
10083 last
= c_wrap_maybe_const (last
, true);
10084 /* Do not warn if the return value of a statement expression is
10086 TREE_NO_WARNING (last
) = 1;
10090 /* Extract the type of said expression. */
10091 type
= TREE_TYPE (last
);
10093 /* If we're not returning a value at all, then the BIND_EXPR that
10094 we already have is a fine expression to return. */
10095 if (!type
|| VOID_TYPE_P (type
))
10098 /* Now that we've located the expression containing the value, it seems
10099 silly to make voidify_wrapper_expr repeat the process. Create a
10100 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10101 tmp
= create_tmp_var_raw (type
);
10103 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10104 tree_expr_nonnegative_p giving up immediately. */
10106 if (TREE_CODE (val
) == NOP_EXPR
10107 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
10108 val
= TREE_OPERAND (val
, 0);
10110 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
10111 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
10114 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
10115 SET_EXPR_LOCATION (t
, loc
);
10120 /* Begin and end compound statements. This is as simple as pushing
10121 and popping new statement lists from the tree. */
10124 c_begin_compound_stmt (bool do_scope
)
10126 tree stmt
= push_stmt_list ();
10132 /* End a compound statement. STMT is the statement. LOC is the
10133 location of the compound statement-- this is usually the location
10134 of the opening brace. */
10137 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
10143 if (c_dialect_objc ())
10144 objc_clear_super_receiver ();
10145 block
= pop_scope ();
10148 stmt
= pop_stmt_list (stmt
);
10149 stmt
= c_build_bind_expr (loc
, block
, stmt
);
10151 /* If this compound statement is nested immediately inside a statement
10152 expression, then force a BIND_EXPR to be created. Otherwise we'll
10153 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10154 STATEMENT_LISTs merge, and thus we can lose track of what statement
10155 was really last. */
10156 if (building_stmt_list_p ()
10157 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10158 && TREE_CODE (stmt
) != BIND_EXPR
)
10160 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
10161 TREE_SIDE_EFFECTS (stmt
) = 1;
10162 SET_EXPR_LOCATION (stmt
, loc
);
10168 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10169 when the current scope is exited. EH_ONLY is true when this is not
10170 meant to apply to normal control flow transfer. */
10173 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
10175 enum tree_code code
;
10179 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
10180 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
10182 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
10183 list
= push_stmt_list ();
10184 TREE_OPERAND (stmt
, 0) = list
;
10185 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
10188 /* Build a binary-operation expression without default conversions.
10189 CODE is the kind of expression to build.
10190 LOCATION is the operator's location.
10191 This function differs from `build' in several ways:
10192 the data type of the result is computed and recorded in it,
10193 warnings are generated if arg data types are invalid,
10194 special handling for addition and subtraction of pointers is known,
10195 and some optimization is done (operations on narrow ints
10196 are done in the narrower type when that gives the same result).
10197 Constant folding is also done before the result is returned.
10199 Note that the operands will never have enumeral types, or function
10200 or array types, because either they will have the default conversions
10201 performed or they have both just been converted to some other type in which
10202 the arithmetic is to be done. */
10205 build_binary_op (location_t location
, enum tree_code code
,
10206 tree orig_op0
, tree orig_op1
, int convert_p
)
10208 tree type0
, type1
, orig_type0
, orig_type1
;
10210 enum tree_code code0
, code1
;
10212 tree ret
= error_mark_node
;
10213 const char *invalid_op_diag
;
10214 bool op0_int_operands
, op1_int_operands
;
10215 bool int_const
, int_const_or_overflow
, int_operands
;
10217 /* Expression code to give to the expression when it is built.
10218 Normally this is CODE, which is what the caller asked for,
10219 but in some special cases we change it. */
10220 enum tree_code resultcode
= code
;
10222 /* Data type in which the computation is to be performed.
10223 In the simplest cases this is the common type of the arguments. */
10224 tree result_type
= NULL
;
10226 /* When the computation is in excess precision, the type of the
10227 final EXCESS_PRECISION_EXPR. */
10228 tree semantic_result_type
= NULL
;
10230 /* Nonzero means operands have already been type-converted
10231 in whatever way is necessary.
10232 Zero means they need to be converted to RESULT_TYPE. */
10235 /* Nonzero means create the expression with this type, rather than
10237 tree build_type
= 0;
10239 /* Nonzero means after finally constructing the expression
10240 convert it to this type. */
10241 tree final_type
= 0;
10243 /* Nonzero if this is an operation like MIN or MAX which can
10244 safely be computed in short if both args are promoted shorts.
10245 Also implies COMMON.
10246 -1 indicates a bitwise operation; this makes a difference
10247 in the exact conditions for when it is safe to do the operation
10248 in a narrower mode. */
10251 /* Nonzero if this is a comparison operation;
10252 if both args are promoted shorts, compare the original shorts.
10253 Also implies COMMON. */
10254 int short_compare
= 0;
10256 /* Nonzero if this is a right-shift operation, which can be computed on the
10257 original short and then promoted if the operand is a promoted short. */
10258 int short_shift
= 0;
10260 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10263 /* True means types are compatible as far as ObjC is concerned. */
10266 /* True means this is an arithmetic operation that may need excess
10268 bool may_need_excess_precision
;
10270 /* True means this is a boolean operation that converts both its
10271 operands to truth-values. */
10272 bool boolean_op
= false;
10274 /* Remember whether we're doing / or %. */
10275 bool doing_div_or_mod
= false;
10277 /* Remember whether we're doing << or >>. */
10278 bool doing_shift
= false;
10280 /* Tree holding instrumentation expression. */
10281 tree instrument_expr
= NULL
;
10283 if (location
== UNKNOWN_LOCATION
)
10284 location
= input_location
;
10289 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
10290 if (op0_int_operands
)
10291 op0
= remove_c_maybe_const_expr (op0
);
10292 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
10293 if (op1_int_operands
)
10294 op1
= remove_c_maybe_const_expr (op1
);
10295 int_operands
= (op0_int_operands
&& op1_int_operands
);
10298 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
10299 && TREE_CODE (orig_op1
) == INTEGER_CST
);
10300 int_const
= (int_const_or_overflow
10301 && !TREE_OVERFLOW (orig_op0
)
10302 && !TREE_OVERFLOW (orig_op1
));
10305 int_const
= int_const_or_overflow
= false;
10307 /* Do not apply default conversion in mixed vector/scalar expression. */
10309 && !((TREE_CODE (TREE_TYPE (op0
)) == VECTOR_TYPE
)
10310 != (TREE_CODE (TREE_TYPE (op1
)) == VECTOR_TYPE
)))
10312 op0
= default_conversion (op0
);
10313 op1
= default_conversion (op1
);
10316 /* When Cilk Plus is enabled and there are array notations inside op0, then
10317 we check to see if there are builtin array notation functions. If
10318 so, then we take on the type of the array notation inside it. */
10319 if (flag_cilkplus
&& contains_array_notation_expr (op0
))
10320 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10322 orig_type0
= type0
= TREE_TYPE (op0
);
10324 if (flag_cilkplus
&& contains_array_notation_expr (op1
))
10325 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10327 orig_type1
= type1
= TREE_TYPE (op1
);
10329 /* The expression codes of the data types of the arguments tell us
10330 whether the arguments are integers, floating, pointers, etc. */
10331 code0
= TREE_CODE (type0
);
10332 code1
= TREE_CODE (type1
);
10334 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10335 STRIP_TYPE_NOPS (op0
);
10336 STRIP_TYPE_NOPS (op1
);
10338 /* If an error was already reported for one of the arguments,
10339 avoid reporting another error. */
10341 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10342 return error_mark_node
;
10344 if ((invalid_op_diag
10345 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10347 error_at (location
, invalid_op_diag
);
10348 return error_mark_node
;
10356 case TRUNC_DIV_EXPR
:
10357 case CEIL_DIV_EXPR
:
10358 case FLOOR_DIV_EXPR
:
10359 case ROUND_DIV_EXPR
:
10360 case EXACT_DIV_EXPR
:
10361 may_need_excess_precision
= true;
10364 may_need_excess_precision
= false;
10367 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10369 op0
= TREE_OPERAND (op0
, 0);
10370 type0
= TREE_TYPE (op0
);
10372 else if (may_need_excess_precision
10373 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10376 op0
= convert (eptype
, op0
);
10378 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10380 op1
= TREE_OPERAND (op1
, 0);
10381 type1
= TREE_TYPE (op1
);
10383 else if (may_need_excess_precision
10384 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10387 op1
= convert (eptype
, op1
);
10390 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10392 /* In case when one of the operands of the binary operation is
10393 a vector and another is a scalar -- convert scalar to vector. */
10394 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10396 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10399 switch (convert_flag
)
10402 return error_mark_node
;
10405 bool maybe_const
= true;
10407 sc
= c_fully_fold (op0
, false, &maybe_const
);
10408 sc
= save_expr (sc
);
10409 sc
= convert (TREE_TYPE (type1
), sc
);
10410 op0
= build_vector_from_val (type1
, sc
);
10412 op0
= c_wrap_maybe_const (op0
, true);
10413 orig_type0
= type0
= TREE_TYPE (op0
);
10414 code0
= TREE_CODE (type0
);
10418 case stv_secondarg
:
10420 bool maybe_const
= true;
10422 sc
= c_fully_fold (op1
, false, &maybe_const
);
10423 sc
= save_expr (sc
);
10424 sc
= convert (TREE_TYPE (type0
), sc
);
10425 op1
= build_vector_from_val (type0
, sc
);
10427 op1
= c_wrap_maybe_const (op1
, true);
10428 orig_type1
= type1
= TREE_TYPE (op1
);
10429 code1
= TREE_CODE (type1
);
10441 /* Handle the pointer + int case. */
10442 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10444 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10445 goto return_build_binary_op
;
10447 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10449 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10450 goto return_build_binary_op
;
10457 /* Subtraction of two similar pointers.
10458 We must subtract them as integers, then divide by object size. */
10459 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10460 && comp_target_types (location
, type0
, type1
))
10462 ret
= pointer_diff (location
, op0
, op1
);
10463 goto return_build_binary_op
;
10465 /* Handle pointer minus int. Just like pointer plus int. */
10466 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10468 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10469 goto return_build_binary_op
;
10479 case TRUNC_DIV_EXPR
:
10480 case CEIL_DIV_EXPR
:
10481 case FLOOR_DIV_EXPR
:
10482 case ROUND_DIV_EXPR
:
10483 case EXACT_DIV_EXPR
:
10484 doing_div_or_mod
= true;
10485 warn_for_div_by_zero (location
, op1
);
10487 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10488 || code0
== FIXED_POINT_TYPE
10489 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10490 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10491 || code1
== FIXED_POINT_TYPE
10492 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10494 enum tree_code tcode0
= code0
, tcode1
= code1
;
10496 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10497 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10498 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10499 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10501 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10502 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10503 resultcode
= RDIV_EXPR
;
10505 /* Although it would be tempting to shorten always here, that
10506 loses on some targets, since the modulo instruction is
10507 undefined if the quotient can't be represented in the
10508 computation mode. We shorten only if unsigned or if
10509 dividing by something we know != -1. */
10510 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10511 || (TREE_CODE (op1
) == INTEGER_CST
10512 && !integer_all_onesp (op1
)));
10520 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10522 /* Allow vector types which are not floating point types. */
10523 else if (code0
== VECTOR_TYPE
10524 && code1
== VECTOR_TYPE
10525 && !VECTOR_FLOAT_TYPE_P (type0
)
10526 && !VECTOR_FLOAT_TYPE_P (type1
))
10530 case TRUNC_MOD_EXPR
:
10531 case FLOOR_MOD_EXPR
:
10532 doing_div_or_mod
= true;
10533 warn_for_div_by_zero (location
, op1
);
10535 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10536 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10537 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10539 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10541 /* Although it would be tempting to shorten always here, that loses
10542 on some targets, since the modulo instruction is undefined if the
10543 quotient can't be represented in the computation mode. We shorten
10544 only if unsigned or if dividing by something we know != -1. */
10545 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10546 || (TREE_CODE (op1
) == INTEGER_CST
10547 && !integer_all_onesp (op1
)));
10552 case TRUTH_ANDIF_EXPR
:
10553 case TRUTH_ORIF_EXPR
:
10554 case TRUTH_AND_EXPR
:
10555 case TRUTH_OR_EXPR
:
10556 case TRUTH_XOR_EXPR
:
10557 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
10558 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10559 || code0
== FIXED_POINT_TYPE
)
10560 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
10561 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10562 || code1
== FIXED_POINT_TYPE
))
10564 /* Result of these operations is always an int,
10565 but that does not mean the operands should be
10566 converted to ints! */
10567 result_type
= integer_type_node
;
10568 if (op0_int_operands
)
10570 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
10571 op0
= remove_c_maybe_const_expr (op0
);
10574 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
10575 if (op1_int_operands
)
10577 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
10578 op1
= remove_c_maybe_const_expr (op1
);
10581 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
10585 if (code
== TRUTH_ANDIF_EXPR
)
10587 int_const_or_overflow
= (int_operands
10588 && TREE_CODE (orig_op0
) == INTEGER_CST
10589 && (op0
== truthvalue_false_node
10590 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10591 int_const
= (int_const_or_overflow
10592 && !TREE_OVERFLOW (orig_op0
)
10593 && (op0
== truthvalue_false_node
10594 || !TREE_OVERFLOW (orig_op1
)));
10596 else if (code
== TRUTH_ORIF_EXPR
)
10598 int_const_or_overflow
= (int_operands
10599 && TREE_CODE (orig_op0
) == INTEGER_CST
10600 && (op0
== truthvalue_true_node
10601 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10602 int_const
= (int_const_or_overflow
10603 && !TREE_OVERFLOW (orig_op0
)
10604 && (op0
== truthvalue_true_node
10605 || !TREE_OVERFLOW (orig_op1
)));
10609 /* Shift operations: result has same type as first operand;
10610 always convert second operand to int.
10611 Also set SHORT_SHIFT if shifting rightward. */
10614 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10615 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10617 result_type
= type0
;
10620 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10621 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10622 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10623 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10625 result_type
= type0
;
10628 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10629 && code1
== INTEGER_TYPE
)
10631 doing_shift
= true;
10632 if (TREE_CODE (op1
) == INTEGER_CST
)
10634 if (tree_int_cst_sgn (op1
) < 0)
10637 if (c_inhibit_evaluation_warnings
== 0)
10638 warning_at (location
, OPT_Wshift_count_negative
,
10639 "right shift count is negative");
10643 if (!integer_zerop (op1
))
10646 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10649 if (c_inhibit_evaluation_warnings
== 0)
10650 warning_at (location
, OPT_Wshift_count_overflow
,
10651 "right shift count >= width of type");
10656 /* Use the type of the value to be shifted. */
10657 result_type
= type0
;
10658 /* Avoid converting op1 to result_type later. */
10664 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10665 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10667 result_type
= type0
;
10670 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10671 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10672 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10673 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10675 result_type
= type0
;
10678 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10679 && code1
== INTEGER_TYPE
)
10681 doing_shift
= true;
10682 if (TREE_CODE (op0
) == INTEGER_CST
10683 && tree_int_cst_sgn (op0
) < 0)
10685 /* Don't reject a left shift of a negative value in a context
10686 where a constant expression is needed in C90. */
10689 if (c_inhibit_evaluation_warnings
== 0)
10690 warning_at (location
, OPT_Wshift_negative_value
,
10691 "left shift of negative value");
10693 if (TREE_CODE (op1
) == INTEGER_CST
)
10695 if (tree_int_cst_sgn (op1
) < 0)
10698 if (c_inhibit_evaluation_warnings
== 0)
10699 warning_at (location
, OPT_Wshift_count_negative
,
10700 "left shift count is negative");
10703 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10706 if (c_inhibit_evaluation_warnings
== 0)
10707 warning_at (location
, OPT_Wshift_count_overflow
,
10708 "left shift count >= width of type");
10712 /* Use the type of the value to be shifted. */
10713 result_type
= type0
;
10714 /* Avoid converting op1 to result_type later. */
10721 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10724 if (!vector_types_compatible_elements_p (type0
, type1
))
10726 error_at (location
, "comparing vectors with different "
10728 return error_mark_node
;
10731 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10733 error_at (location
, "comparing vectors with different "
10734 "number of elements");
10735 return error_mark_node
;
10738 /* Always construct signed integer vector type. */
10739 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10740 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10741 result_type
= build_opaque_vector_type (intt
,
10742 TYPE_VECTOR_SUBPARTS (type0
));
10746 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
10747 warning_at (location
,
10749 "comparing floating point with == or != is unsafe");
10750 /* Result of comparison is always int,
10751 but don't convert the args to int! */
10752 build_type
= integer_type_node
;
10753 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10754 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
10755 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10756 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
10758 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10760 if (TREE_CODE (op0
) == ADDR_EXPR
10761 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
10763 if (code
== EQ_EXPR
)
10764 warning_at (location
,
10766 "the comparison will always evaluate as %<false%> "
10767 "for the address of %qD will never be NULL",
10768 TREE_OPERAND (op0
, 0));
10770 warning_at (location
,
10772 "the comparison will always evaluate as %<true%> "
10773 "for the address of %qD will never be NULL",
10774 TREE_OPERAND (op0
, 0));
10776 result_type
= type0
;
10778 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10780 if (TREE_CODE (op1
) == ADDR_EXPR
10781 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
10783 if (code
== EQ_EXPR
)
10784 warning_at (location
,
10786 "the comparison will always evaluate as %<false%> "
10787 "for the address of %qD will never be NULL",
10788 TREE_OPERAND (op1
, 0));
10790 warning_at (location
,
10792 "the comparison will always evaluate as %<true%> "
10793 "for the address of %qD will never be NULL",
10794 TREE_OPERAND (op1
, 0));
10796 result_type
= type1
;
10798 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10800 tree tt0
= TREE_TYPE (type0
);
10801 tree tt1
= TREE_TYPE (type1
);
10802 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
10803 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
10804 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
10806 /* Anything compares with void *. void * compares with anything.
10807 Otherwise, the targets must be compatible
10808 and both must be object or both incomplete. */
10809 if (comp_target_types (location
, type0
, type1
))
10810 result_type
= common_pointer_type (type0
, type1
);
10811 else if (!addr_space_superset (as0
, as1
, &as_common
))
10813 error_at (location
, "comparison of pointers to "
10814 "disjoint address spaces");
10815 return error_mark_node
;
10817 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
10819 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
10820 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10821 "comparison of %<void *%> with function pointer");
10823 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
10825 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
10826 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10827 "comparison of %<void *%> with function pointer");
10830 /* Avoid warning about the volatile ObjC EH puts on decls. */
10832 pedwarn (location
, 0,
10833 "comparison of distinct pointer types lacks a cast");
10835 if (result_type
== NULL_TREE
)
10837 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10838 result_type
= build_pointer_type
10839 (build_qualified_type (void_type_node
, qual
));
10842 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10844 result_type
= type0
;
10845 pedwarn (location
, 0, "comparison between pointer and integer");
10847 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10849 result_type
= type1
;
10850 pedwarn (location
, 0, "comparison between pointer and integer");
10852 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
10853 || truth_value_p (TREE_CODE (orig_op0
)))
10854 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
10855 || truth_value_p (TREE_CODE (orig_op1
))))
10856 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
10863 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10866 if (!vector_types_compatible_elements_p (type0
, type1
))
10868 error_at (location
, "comparing vectors with different "
10870 return error_mark_node
;
10873 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10875 error_at (location
, "comparing vectors with different "
10876 "number of elements");
10877 return error_mark_node
;
10880 /* Always construct signed integer vector type. */
10881 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10882 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10883 result_type
= build_opaque_vector_type (intt
,
10884 TYPE_VECTOR_SUBPARTS (type0
));
10888 build_type
= integer_type_node
;
10889 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10890 || code0
== FIXED_POINT_TYPE
)
10891 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10892 || code1
== FIXED_POINT_TYPE
))
10894 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10896 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
10897 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
10898 addr_space_t as_common
;
10900 if (comp_target_types (location
, type0
, type1
))
10902 result_type
= common_pointer_type (type0
, type1
);
10903 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
10904 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
10905 pedwarn (location
, 0,
10906 "comparison of complete and incomplete pointers");
10907 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
10908 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10909 "ordered comparisons of pointers to functions");
10910 else if (null_pointer_constant_p (orig_op0
)
10911 || null_pointer_constant_p (orig_op1
))
10912 warning_at (location
, OPT_Wextra
,
10913 "ordered comparison of pointer with null pointer");
10916 else if (!addr_space_superset (as0
, as1
, &as_common
))
10918 error_at (location
, "comparison of pointers to "
10919 "disjoint address spaces");
10920 return error_mark_node
;
10924 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10925 result_type
= build_pointer_type
10926 (build_qualified_type (void_type_node
, qual
));
10927 pedwarn (location
, 0,
10928 "comparison of distinct pointer types lacks a cast");
10931 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10933 result_type
= type0
;
10935 pedwarn (location
, OPT_Wpedantic
,
10936 "ordered comparison of pointer with integer zero");
10937 else if (extra_warnings
)
10938 warning_at (location
, OPT_Wextra
,
10939 "ordered comparison of pointer with integer zero");
10941 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10943 result_type
= type1
;
10945 pedwarn (location
, OPT_Wpedantic
,
10946 "ordered comparison of pointer with integer zero");
10947 else if (extra_warnings
)
10948 warning_at (location
, OPT_Wextra
,
10949 "ordered comparison of pointer with integer zero");
10951 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10953 result_type
= type0
;
10954 pedwarn (location
, 0, "comparison between pointer and integer");
10956 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10958 result_type
= type1
;
10959 pedwarn (location
, 0, "comparison between pointer and integer");
10961 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
10962 || truth_value_p (TREE_CODE (orig_op0
)))
10963 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
10964 || truth_value_p (TREE_CODE (orig_op1
))))
10965 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
10969 gcc_unreachable ();
10972 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10973 return error_mark_node
;
10975 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10976 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
10977 || !vector_types_compatible_elements_p (type0
, type1
)))
10979 binary_op_error (location
, code
, type0
, type1
);
10980 return error_mark_node
;
10983 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10984 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
10986 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10987 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
10989 bool first_complex
= (code0
== COMPLEX_TYPE
);
10990 bool second_complex
= (code1
== COMPLEX_TYPE
);
10991 int none_complex
= (!first_complex
&& !second_complex
);
10993 if (shorten
|| common
|| short_compare
)
10995 result_type
= c_common_type (type0
, type1
);
10996 do_warn_double_promotion (result_type
, type0
, type1
,
10997 "implicit conversion from %qT to %qT "
10998 "to match other operand of binary "
11001 if (result_type
== error_mark_node
)
11002 return error_mark_node
;
11005 if (first_complex
!= second_complex
11006 && (code
== PLUS_EXPR
11007 || code
== MINUS_EXPR
11008 || code
== MULT_EXPR
11009 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
11010 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
11011 && flag_signed_zeros
)
11013 /* An operation on mixed real/complex operands must be
11014 handled specially, but the language-independent code can
11015 more easily optimize the plain complex arithmetic if
11016 -fno-signed-zeros. */
11017 tree real_type
= TREE_TYPE (result_type
);
11019 if (type0
!= orig_type0
|| type1
!= orig_type1
)
11021 gcc_assert (may_need_excess_precision
&& common
);
11022 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11026 if (TREE_TYPE (op0
) != result_type
)
11027 op0
= convert_and_check (location
, result_type
, op0
);
11028 if (TREE_TYPE (op1
) != real_type
)
11029 op1
= convert_and_check (location
, real_type
, op1
);
11033 if (TREE_TYPE (op0
) != real_type
)
11034 op0
= convert_and_check (location
, real_type
, op0
);
11035 if (TREE_TYPE (op1
) != result_type
)
11036 op1
= convert_and_check (location
, result_type
, op1
);
11038 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11039 return error_mark_node
;
11042 op0
= c_save_expr (op0
);
11043 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
11045 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
11050 case TRUNC_DIV_EXPR
:
11051 op1
= c_save_expr (op1
);
11052 imag
= build2 (resultcode
, real_type
, imag
, op1
);
11053 /* Fall through. */
11056 real
= build2 (resultcode
, real_type
, real
, op1
);
11064 op1
= c_save_expr (op1
);
11065 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
11067 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
11072 op0
= c_save_expr (op0
);
11073 imag
= build2 (resultcode
, real_type
, op0
, imag
);
11074 /* Fall through. */
11076 real
= build2 (resultcode
, real_type
, op0
, real
);
11079 real
= build2 (resultcode
, real_type
, op0
, real
);
11080 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
11086 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
11087 goto return_build_binary_op
;
11090 /* For certain operations (which identify themselves by shorten != 0)
11091 if both args were extended from the same smaller type,
11092 do the arithmetic in that type and then extend.
11094 shorten !=0 and !=1 indicates a bitwise operation.
11095 For them, this optimization is safe only if
11096 both args are zero-extended or both are sign-extended.
11097 Otherwise, we might change the result.
11098 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11099 but calculated in (unsigned short) it would be (unsigned short)-1. */
11101 if (shorten
&& none_complex
)
11103 final_type
= result_type
;
11104 result_type
= shorten_binary_op (result_type
, op0
, op1
,
11108 /* Shifts can be shortened if shifting right. */
11113 tree arg0
= get_narrower (op0
, &unsigned_arg
);
11115 final_type
= result_type
;
11117 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
11118 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
11120 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
11121 && tree_int_cst_sgn (op1
) > 0
11122 /* We can shorten only if the shift count is less than the
11123 number of bits in the smaller type size. */
11124 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
11125 /* We cannot drop an unsigned shift after sign-extension. */
11126 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
11128 /* Do an unsigned shift if the operand was zero-extended. */
11130 = c_common_signed_or_unsigned_type (unsigned_arg
,
11132 /* Convert value-to-be-shifted to that type. */
11133 if (TREE_TYPE (op0
) != result_type
)
11134 op0
= convert (result_type
, op0
);
11139 /* Comparison operations are shortened too but differently.
11140 They identify themselves by setting short_compare = 1. */
11144 /* Don't write &op0, etc., because that would prevent op0
11145 from being kept in a register.
11146 Instead, make copies of the our local variables and
11147 pass the copies by reference, then copy them back afterward. */
11148 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
11149 enum tree_code xresultcode
= resultcode
;
11151 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
11157 goto return_build_binary_op
;
11160 op0
= xop0
, op1
= xop1
;
11162 resultcode
= xresultcode
;
11164 if (c_inhibit_evaluation_warnings
== 0)
11166 bool op0_maybe_const
= true;
11167 bool op1_maybe_const
= true;
11168 tree orig_op0_folded
, orig_op1_folded
;
11170 if (in_late_binary_op
)
11172 orig_op0_folded
= orig_op0
;
11173 orig_op1_folded
= orig_op1
;
11177 /* Fold for the sake of possible warnings, as in
11178 build_conditional_expr. This requires the
11179 "original" values to be folded, not just op0 and
11181 c_inhibit_evaluation_warnings
++;
11182 op0
= c_fully_fold (op0
, require_constant_value
,
11184 op1
= c_fully_fold (op1
, require_constant_value
,
11186 c_inhibit_evaluation_warnings
--;
11187 orig_op0_folded
= c_fully_fold (orig_op0
,
11188 require_constant_value
,
11190 orig_op1_folded
= c_fully_fold (orig_op1
,
11191 require_constant_value
,
11195 if (warn_sign_compare
)
11196 warn_for_sign_compare (location
, orig_op0_folded
,
11197 orig_op1_folded
, op0
, op1
,
11198 result_type
, resultcode
);
11199 if (!in_late_binary_op
&& !int_operands
)
11201 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
11202 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
11203 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
11204 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
11210 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11211 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11212 Then the expression will be built.
11213 It will be given type FINAL_TYPE if that is nonzero;
11214 otherwise, it will be given type RESULT_TYPE. */
11218 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
11219 return error_mark_node
;
11222 if (build_type
== NULL_TREE
)
11224 build_type
= result_type
;
11225 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
11228 gcc_assert (may_need_excess_precision
&& common
);
11229 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11235 op0
= ep_convert_and_check (location
, result_type
, op0
,
11236 semantic_result_type
);
11237 op1
= ep_convert_and_check (location
, result_type
, op1
,
11238 semantic_result_type
);
11240 /* This can happen if one operand has a vector type, and the other
11241 has a different type. */
11242 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11243 return error_mark_node
;
11246 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
11247 | SANITIZE_FLOAT_DIVIDE
))
11248 && do_ubsan_in_current_function ()
11249 && (doing_div_or_mod
|| doing_shift
))
11251 /* OP0 and/or OP1 might have side-effects. */
11252 op0
= c_save_expr (op0
);
11253 op1
= c_save_expr (op1
);
11254 op0
= c_fully_fold (op0
, false, NULL
);
11255 op1
= c_fully_fold (op1
, false, NULL
);
11256 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
11257 | SANITIZE_FLOAT_DIVIDE
)))
11258 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
11259 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
11260 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
11263 /* Treat expressions in initializers specially as they can't trap. */
11264 if (int_const_or_overflow
)
11265 ret
= (require_constant_value
11266 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
11268 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
11270 ret
= build2 (resultcode
, build_type
, op0
, op1
);
11271 if (final_type
!= 0)
11272 ret
= convert (final_type
, ret
);
11274 return_build_binary_op
:
11275 gcc_assert (ret
!= error_mark_node
);
11276 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
11277 ret
= (int_operands
11278 ? note_integer_operands (ret
)
11279 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
11280 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
11281 && !in_late_binary_op
)
11282 ret
= note_integer_operands (ret
);
11283 if (semantic_result_type
)
11284 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
11285 protected_set_expr_location (ret
, location
);
11287 if (instrument_expr
!= NULL
)
11288 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
11289 instrument_expr
, ret
);
11295 /* Convert EXPR to be a truth-value, validating its type for this
11296 purpose. LOCATION is the source location for the expression. */
11299 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
11301 bool int_const
, int_operands
;
11303 switch (TREE_CODE (TREE_TYPE (expr
)))
11306 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
11307 return error_mark_node
;
11310 error_at (location
, "used struct type value where scalar is required");
11311 return error_mark_node
;
11314 error_at (location
, "used union type value where scalar is required");
11315 return error_mark_node
;
11318 error_at (location
, "void value not ignored as it ought to be");
11319 return error_mark_node
;
11321 case FUNCTION_TYPE
:
11322 gcc_unreachable ();
11325 error_at (location
, "used vector type where scalar is required");
11326 return error_mark_node
;
11332 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11333 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11334 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11336 expr
= remove_c_maybe_const_expr (expr
);
11337 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11338 convert (TREE_TYPE (expr
), integer_zero_node
));
11339 expr
= note_integer_operands (expr
);
11342 /* ??? Should we also give an error for vectors rather than leaving
11343 those to give errors later? */
11344 expr
= c_common_truthvalue_conversion (location
, expr
);
11346 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11348 if (TREE_OVERFLOW (expr
))
11351 return note_integer_operands (expr
);
11353 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11354 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11359 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11363 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11365 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11367 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11368 /* Executing a compound literal inside a function reinitializes
11370 if (!TREE_STATIC (decl
))
11378 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11379 statement. LOC is the location of the OACC_PARALLEL. */
11382 c_finish_oacc_parallel (location_t loc
, tree clauses
, tree block
)
11386 block
= c_end_compound_stmt (loc
, block
, true);
11388 stmt
= make_node (OACC_PARALLEL
);
11389 TREE_TYPE (stmt
) = void_type_node
;
11390 OACC_PARALLEL_CLAUSES (stmt
) = clauses
;
11391 OACC_PARALLEL_BODY (stmt
) = block
;
11392 SET_EXPR_LOCATION (stmt
, loc
);
11394 return add_stmt (stmt
);
11397 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11398 statement. LOC is the location of the OACC_KERNELS. */
11401 c_finish_oacc_kernels (location_t loc
, tree clauses
, tree block
)
11405 block
= c_end_compound_stmt (loc
, block
, true);
11407 stmt
= make_node (OACC_KERNELS
);
11408 TREE_TYPE (stmt
) = void_type_node
;
11409 OACC_KERNELS_CLAUSES (stmt
) = clauses
;
11410 OACC_KERNELS_BODY (stmt
) = block
;
11411 SET_EXPR_LOCATION (stmt
, loc
);
11413 return add_stmt (stmt
);
11416 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11417 statement. LOC is the location of the OACC_DATA. */
11420 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
11424 block
= c_end_compound_stmt (loc
, block
, true);
11426 stmt
= make_node (OACC_DATA
);
11427 TREE_TYPE (stmt
) = void_type_node
;
11428 OACC_DATA_CLAUSES (stmt
) = clauses
;
11429 OACC_DATA_BODY (stmt
) = block
;
11430 SET_EXPR_LOCATION (stmt
, loc
);
11432 return add_stmt (stmt
);
11435 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11438 c_begin_omp_parallel (void)
11442 keep_next_level ();
11443 block
= c_begin_compound_stmt (true);
11448 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11449 statement. LOC is the location of the OMP_PARALLEL. */
11452 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11456 block
= c_end_compound_stmt (loc
, block
, true);
11458 stmt
= make_node (OMP_PARALLEL
);
11459 TREE_TYPE (stmt
) = void_type_node
;
11460 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11461 OMP_PARALLEL_BODY (stmt
) = block
;
11462 SET_EXPR_LOCATION (stmt
, loc
);
11464 return add_stmt (stmt
);
11467 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11470 c_begin_omp_task (void)
11474 keep_next_level ();
11475 block
= c_begin_compound_stmt (true);
11480 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11481 statement. LOC is the location of the #pragma. */
11484 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11488 block
= c_end_compound_stmt (loc
, block
, true);
11490 stmt
= make_node (OMP_TASK
);
11491 TREE_TYPE (stmt
) = void_type_node
;
11492 OMP_TASK_CLAUSES (stmt
) = clauses
;
11493 OMP_TASK_BODY (stmt
) = block
;
11494 SET_EXPR_LOCATION (stmt
, loc
);
11496 return add_stmt (stmt
);
11499 /* Generate GOMP_cancel call for #pragma omp cancel. */
11502 c_finish_omp_cancel (location_t loc
, tree clauses
)
11504 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
11506 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11508 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11510 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11512 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11516 error_at (loc
, "%<#pragma omp cancel must specify one of "
11517 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11521 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
11522 if (ifc
!= NULL_TREE
)
11524 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
11525 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
11526 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
11527 build_zero_cst (type
));
11530 ifc
= boolean_true_node
;
11531 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
11532 build_int_cst (integer_type_node
, mask
),
11537 /* Generate GOMP_cancellation_point call for
11538 #pragma omp cancellation point. */
11541 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
11543 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
11545 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11547 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11549 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11551 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11555 error_at (loc
, "%<#pragma omp cancellation point must specify one of "
11556 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11560 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
11561 build_int_cst (integer_type_node
, mask
));
11565 /* Helper function for handle_omp_array_sections. Called recursively
11566 to handle multiple array-section-subscripts. C is the clause,
11567 T current expression (initially OMP_CLAUSE_DECL), which is either
11568 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11569 expression if specified, TREE_VALUE length expression if specified,
11570 TREE_CHAIN is what it has been specified after, or some decl.
11571 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11572 set to true if any of the array-section-subscript could have length
11573 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11574 first array-section-subscript which is known not to have length
11576 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11577 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11578 all are or may have length of 1, array-section-subscript [:2] is the
11579 first one knonwn not to have length 1. For array-section-subscript
11580 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11581 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11582 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11583 case though, as some lengths could be zero. */
11586 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
11587 bool &maybe_zero_len
, unsigned int &first_non_one
)
11589 tree ret
, low_bound
, length
, type
;
11590 if (TREE_CODE (t
) != TREE_LIST
)
11592 if (error_operand_p (t
))
11593 return error_mark_node
;
11594 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11597 error_at (OMP_CLAUSE_LOCATION (c
),
11598 "%qD is not a variable in %qs clause", t
,
11599 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11601 error_at (OMP_CLAUSE_LOCATION (c
),
11602 "%qE is not a variable in %qs clause", t
,
11603 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11604 return error_mark_node
;
11606 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11607 && TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11609 error_at (OMP_CLAUSE_LOCATION (c
),
11610 "%qD is threadprivate variable in %qs clause", t
,
11611 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11612 return error_mark_node
;
11617 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
11618 maybe_zero_len
, first_non_one
);
11619 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
11622 type
= TREE_TYPE (ret
);
11623 low_bound
= TREE_PURPOSE (t
);
11624 length
= TREE_VALUE (t
);
11626 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
11627 return error_mark_node
;
11629 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
11631 error_at (OMP_CLAUSE_LOCATION (c
),
11632 "low bound %qE of array section does not have integral type",
11634 return error_mark_node
;
11636 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
11638 error_at (OMP_CLAUSE_LOCATION (c
),
11639 "length %qE of array section does not have integral type",
11641 return error_mark_node
;
11644 && TREE_CODE (low_bound
) == INTEGER_CST
11645 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11646 > TYPE_PRECISION (sizetype
))
11647 low_bound
= fold_convert (sizetype
, low_bound
);
11649 && TREE_CODE (length
) == INTEGER_CST
11650 && TYPE_PRECISION (TREE_TYPE (length
))
11651 > TYPE_PRECISION (sizetype
))
11652 length
= fold_convert (sizetype
, length
);
11653 if (low_bound
== NULL_TREE
)
11654 low_bound
= integer_zero_node
;
11656 if (length
!= NULL_TREE
)
11658 if (!integer_nonzerop (length
))
11659 maybe_zero_len
= true;
11660 if (first_non_one
== types
.length ()
11661 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
11664 if (TREE_CODE (type
) == ARRAY_TYPE
)
11666 if (length
== NULL_TREE
11667 && (TYPE_DOMAIN (type
) == NULL_TREE
11668 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
11670 error_at (OMP_CLAUSE_LOCATION (c
),
11671 "for unknown bound array type length expression must "
11673 return error_mark_node
;
11675 if (TREE_CODE (low_bound
) == INTEGER_CST
11676 && tree_int_cst_sgn (low_bound
) == -1)
11678 error_at (OMP_CLAUSE_LOCATION (c
),
11679 "negative low bound in array section in %qs clause",
11680 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11681 return error_mark_node
;
11683 if (length
!= NULL_TREE
11684 && TREE_CODE (length
) == INTEGER_CST
11685 && tree_int_cst_sgn (length
) == -1)
11687 error_at (OMP_CLAUSE_LOCATION (c
),
11688 "negative length in array section in %qs clause",
11689 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11690 return error_mark_node
;
11692 if (TYPE_DOMAIN (type
)
11693 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
11694 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
11697 tree size
= size_binop (PLUS_EXPR
,
11698 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11700 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11702 if (tree_int_cst_lt (size
, low_bound
))
11704 error_at (OMP_CLAUSE_LOCATION (c
),
11705 "low bound %qE above array section size "
11706 "in %qs clause", low_bound
,
11707 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11708 return error_mark_node
;
11710 if (tree_int_cst_equal (size
, low_bound
))
11711 maybe_zero_len
= true;
11712 else if (length
== NULL_TREE
11713 && first_non_one
== types
.length ()
11714 && tree_int_cst_equal
11715 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11719 else if (length
== NULL_TREE
)
11721 maybe_zero_len
= true;
11722 if (first_non_one
== types
.length ())
11725 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
11727 if (tree_int_cst_lt (size
, length
))
11729 error_at (OMP_CLAUSE_LOCATION (c
),
11730 "length %qE above array section size "
11731 "in %qs clause", length
,
11732 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11733 return error_mark_node
;
11735 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11738 = size_binop (PLUS_EXPR
,
11739 fold_convert (sizetype
, low_bound
),
11740 fold_convert (sizetype
, length
));
11741 if (TREE_CODE (lbpluslen
) == INTEGER_CST
11742 && tree_int_cst_lt (size
, lbpluslen
))
11744 error_at (OMP_CLAUSE_LOCATION (c
),
11745 "high bound %qE above array section size "
11746 "in %qs clause", lbpluslen
,
11747 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11748 return error_mark_node
;
11753 else if (length
== NULL_TREE
)
11755 maybe_zero_len
= true;
11756 if (first_non_one
== types
.length ())
11760 /* For [lb:] we will need to evaluate lb more than once. */
11761 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11763 tree lb
= c_save_expr (low_bound
);
11764 if (lb
!= low_bound
)
11766 TREE_PURPOSE (t
) = lb
;
11771 else if (TREE_CODE (type
) == POINTER_TYPE
)
11773 if (length
== NULL_TREE
)
11775 error_at (OMP_CLAUSE_LOCATION (c
),
11776 "for pointer type length expression must be specified");
11777 return error_mark_node
;
11779 /* If there is a pointer type anywhere but in the very first
11780 array-section-subscript, the array section can't be contiguous. */
11781 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11782 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
11784 error_at (OMP_CLAUSE_LOCATION (c
),
11785 "array section is not contiguous in %qs clause",
11786 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11787 return error_mark_node
;
11792 error_at (OMP_CLAUSE_LOCATION (c
),
11793 "%qE does not have pointer or array type", ret
);
11794 return error_mark_node
;
11796 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11797 types
.safe_push (TREE_TYPE (ret
));
11798 /* We will need to evaluate lb more than once. */
11799 tree lb
= c_save_expr (low_bound
);
11800 if (lb
!= low_bound
)
11802 TREE_PURPOSE (t
) = lb
;
11805 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
11809 /* Handle array sections for clause C. */
11812 handle_omp_array_sections (tree c
)
11814 bool maybe_zero_len
= false;
11815 unsigned int first_non_one
= 0;
11816 vec
<tree
> types
= vNULL
;
11817 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
11818 maybe_zero_len
, first_non_one
);
11819 if (first
== error_mark_node
)
11824 if (first
== NULL_TREE
)
11829 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
11831 tree t
= OMP_CLAUSE_DECL (c
);
11832 tree tem
= NULL_TREE
;
11834 /* Need to evaluate side effects in the length expressions
11836 while (TREE_CODE (t
) == TREE_LIST
)
11838 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
11840 if (tem
== NULL_TREE
)
11841 tem
= TREE_VALUE (t
);
11843 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
11844 TREE_VALUE (t
), tem
);
11846 t
= TREE_CHAIN (t
);
11849 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
11850 first
= c_fully_fold (first
, false, NULL
);
11851 OMP_CLAUSE_DECL (c
) = first
;
11855 unsigned int num
= types
.length (), i
;
11856 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
11857 tree condition
= NULL_TREE
;
11859 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
11860 maybe_zero_len
= true;
11862 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
11863 t
= TREE_CHAIN (t
))
11865 tree low_bound
= TREE_PURPOSE (t
);
11866 tree length
= TREE_VALUE (t
);
11870 && TREE_CODE (low_bound
) == INTEGER_CST
11871 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11872 > TYPE_PRECISION (sizetype
))
11873 low_bound
= fold_convert (sizetype
, low_bound
);
11875 && TREE_CODE (length
) == INTEGER_CST
11876 && TYPE_PRECISION (TREE_TYPE (length
))
11877 > TYPE_PRECISION (sizetype
))
11878 length
= fold_convert (sizetype
, length
);
11879 if (low_bound
== NULL_TREE
)
11880 low_bound
= integer_zero_node
;
11881 if (!maybe_zero_len
&& i
> first_non_one
)
11883 if (integer_nonzerop (low_bound
))
11884 goto do_warn_noncontiguous
;
11885 if (length
!= NULL_TREE
11886 && TREE_CODE (length
) == INTEGER_CST
11887 && TYPE_DOMAIN (types
[i
])
11888 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
11889 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
11893 size
= size_binop (PLUS_EXPR
,
11894 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11896 if (!tree_int_cst_equal (length
, size
))
11898 do_warn_noncontiguous
:
11899 error_at (OMP_CLAUSE_LOCATION (c
),
11900 "array section is not contiguous in %qs "
11902 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11907 if (length
!= NULL_TREE
11908 && TREE_SIDE_EFFECTS (length
))
11910 if (side_effects
== NULL_TREE
)
11911 side_effects
= length
;
11913 side_effects
= build2 (COMPOUND_EXPR
,
11914 TREE_TYPE (side_effects
),
11915 length
, side_effects
);
11922 if (i
> first_non_one
&& length
&& integer_nonzerop (length
))
11925 l
= fold_convert (sizetype
, length
);
11928 l
= size_binop (PLUS_EXPR
,
11929 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11931 l
= size_binop (MINUS_EXPR
, l
,
11932 fold_convert (sizetype
, low_bound
));
11934 if (i
> first_non_one
)
11936 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
11938 if (condition
== NULL_TREE
)
11941 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
11944 else if (size
== NULL_TREE
)
11946 size
= size_in_bytes (TREE_TYPE (types
[i
]));
11947 size
= size_binop (MULT_EXPR
, size
, l
);
11949 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
11950 size
, size_zero_node
);
11953 size
= size_binop (MULT_EXPR
, size
, l
);
11958 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
11959 first
= c_fully_fold (first
, false, NULL
);
11960 OMP_CLAUSE_DECL (c
) = first
;
11962 size
= c_fully_fold (size
, false, NULL
);
11963 OMP_CLAUSE_SIZE (c
) = size
;
11964 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
11966 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
11967 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
11968 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_POINTER
);
11969 if (!c_mark_addressable (t
))
11971 OMP_CLAUSE_DECL (c2
) = t
;
11972 t
= build_fold_addr_expr (first
);
11973 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
11974 tree ptr
= OMP_CLAUSE_DECL (c2
);
11975 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
11976 ptr
= build_fold_addr_expr (ptr
);
11977 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11978 ptrdiff_type_node
, t
,
11979 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
11980 ptrdiff_type_node
, ptr
));
11981 t
= c_fully_fold (t
, false, NULL
);
11982 OMP_CLAUSE_SIZE (c2
) = t
;
11983 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
11984 OMP_CLAUSE_CHAIN (c
) = c2
;
11989 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11990 an inline call. But, remap
11991 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11992 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11995 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
11996 tree decl
, tree placeholder
)
11999 hash_map
<tree
, tree
> decl_map
;
12001 decl_map
.put (omp_decl1
, placeholder
);
12002 decl_map
.put (omp_decl2
, decl
);
12003 memset (&id
, 0, sizeof (id
));
12004 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
12005 id
.dst_fn
= current_function_decl
;
12006 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
12007 id
.decl_map
= &decl_map
;
12009 id
.copy_decl
= copy_decl_no_change
;
12010 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
12011 id
.transform_new_cfg
= true;
12012 id
.transform_return_to_modify
= false;
12013 id
.transform_lang_insert_block
= NULL
;
12015 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
12019 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12020 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12023 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
12025 if (*tp
== (tree
) data
)
12030 /* For all elements of CLAUSES, validate them against their constraints.
12031 Remove any elements from the list that are invalid. */
12034 c_finish_omp_clauses (tree clauses
)
12036 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
12037 bitmap_head aligned_head
;
12039 bool branch_seen
= false;
12040 bool copyprivate_seen
= false;
12041 tree
*nowait_clause
= NULL
;
12043 bitmap_obstack_initialize (NULL
);
12044 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
12045 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
12046 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
12047 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
12049 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
12051 bool remove
= false;
12052 bool need_complete
= false;
12053 bool need_implicitly_determined
= false;
12055 switch (OMP_CLAUSE_CODE (c
))
12057 case OMP_CLAUSE_SHARED
:
12058 need_implicitly_determined
= true;
12059 goto check_dup_generic
;
12061 case OMP_CLAUSE_PRIVATE
:
12062 need_complete
= true;
12063 need_implicitly_determined
= true;
12064 goto check_dup_generic
;
12066 case OMP_CLAUSE_REDUCTION
:
12067 need_implicitly_determined
= true;
12068 t
= OMP_CLAUSE_DECL (c
);
12069 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
12070 && (FLOAT_TYPE_P (TREE_TYPE (t
))
12071 || TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
))
12073 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
12074 const char *r_name
= NULL
;
12083 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
12087 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
12099 case TRUTH_ANDIF_EXPR
:
12100 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
12103 case TRUTH_ORIF_EXPR
:
12104 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
12108 gcc_unreachable ();
12112 error_at (OMP_CLAUSE_LOCATION (c
),
12113 "%qE has invalid type for %<reduction(%s)%>",
12119 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
12121 error_at (OMP_CLAUSE_LOCATION (c
),
12122 "user defined reduction not found for %qD", t
);
12126 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
12128 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
12129 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
12130 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
12131 VAR_DECL
, NULL_TREE
, type
);
12132 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
12133 DECL_ARTIFICIAL (placeholder
) = 1;
12134 DECL_IGNORED_P (placeholder
) = 1;
12135 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
12136 c_mark_addressable (placeholder
);
12137 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
12138 c_mark_addressable (OMP_CLAUSE_DECL (c
));
12139 OMP_CLAUSE_REDUCTION_MERGE (c
)
12140 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
12141 TREE_VEC_ELT (list
, 0),
12142 TREE_VEC_ELT (list
, 1),
12143 OMP_CLAUSE_DECL (c
), placeholder
);
12144 OMP_CLAUSE_REDUCTION_MERGE (c
)
12145 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12146 void_type_node
, NULL_TREE
,
12147 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
12148 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
12149 if (TREE_VEC_LENGTH (list
) == 6)
12151 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
12152 c_mark_addressable (OMP_CLAUSE_DECL (c
));
12153 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
12154 c_mark_addressable (placeholder
);
12155 tree init
= TREE_VEC_ELT (list
, 5);
12156 if (init
== error_mark_node
)
12157 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
12158 OMP_CLAUSE_REDUCTION_INIT (c
)
12159 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
12160 TREE_VEC_ELT (list
, 3),
12161 OMP_CLAUSE_DECL (c
), placeholder
);
12162 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
12163 OMP_CLAUSE_REDUCTION_INIT (c
)
12164 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
,
12165 OMP_CLAUSE_REDUCTION_INIT (c
));
12166 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
12167 c_find_omp_placeholder_r
,
12168 placeholder
, NULL
))
12169 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
12174 if (AGGREGATE_TYPE_P (TREE_TYPE (t
)))
12175 init
= build_constructor (TREE_TYPE (t
), NULL
);
12177 init
= fold_convert (TREE_TYPE (t
), integer_zero_node
);
12178 OMP_CLAUSE_REDUCTION_INIT (c
)
12179 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
, init
);
12181 OMP_CLAUSE_REDUCTION_INIT (c
)
12182 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12183 void_type_node
, NULL_TREE
,
12184 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
12185 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
12187 goto check_dup_generic
;
12189 case OMP_CLAUSE_COPYPRIVATE
:
12190 copyprivate_seen
= true;
12193 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
12194 "%<nowait%> clause must not be used together "
12195 "with %<copyprivate%>");
12196 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
12197 nowait_clause
= NULL
;
12199 goto check_dup_generic
;
12201 case OMP_CLAUSE_COPYIN
:
12202 t
= OMP_CLAUSE_DECL (c
);
12203 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
12205 error_at (OMP_CLAUSE_LOCATION (c
),
12206 "%qE must be %<threadprivate%> for %<copyin%>", t
);
12210 goto check_dup_generic
;
12212 case OMP_CLAUSE_LINEAR
:
12213 t
= OMP_CLAUSE_DECL (c
);
12214 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
12215 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
12217 error_at (OMP_CLAUSE_LOCATION (c
),
12218 "linear clause applied to non-integral non-pointer "
12219 "variable with type %qT", TREE_TYPE (t
));
12223 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
12225 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
12226 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
12227 OMP_CLAUSE_DECL (c
), s
);
12228 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12229 sizetype
, s
, OMP_CLAUSE_DECL (c
));
12230 if (s
== error_mark_node
)
12232 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
12235 OMP_CLAUSE_LINEAR_STEP (c
)
12236 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
12237 goto check_dup_generic
;
12240 t
= OMP_CLAUSE_DECL (c
);
12241 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12243 error_at (OMP_CLAUSE_LOCATION (c
),
12244 "%qE is not a variable in clause %qs", t
,
12245 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12248 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12249 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
12250 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12252 error_at (OMP_CLAUSE_LOCATION (c
),
12253 "%qE appears more than once in data clauses", t
);
12257 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12260 case OMP_CLAUSE_FIRSTPRIVATE
:
12261 t
= OMP_CLAUSE_DECL (c
);
12262 need_complete
= true;
12263 need_implicitly_determined
= true;
12264 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12266 error_at (OMP_CLAUSE_LOCATION (c
),
12267 "%qE is not a variable in clause %<firstprivate%>", t
);
12270 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12271 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
12273 error_at (OMP_CLAUSE_LOCATION (c
),
12274 "%qE appears more than once in data clauses", t
);
12278 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
12281 case OMP_CLAUSE_LASTPRIVATE
:
12282 t
= OMP_CLAUSE_DECL (c
);
12283 need_complete
= true;
12284 need_implicitly_determined
= true;
12285 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12287 error_at (OMP_CLAUSE_LOCATION (c
),
12288 "%qE is not a variable in clause %<lastprivate%>", t
);
12291 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12292 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12294 error_at (OMP_CLAUSE_LOCATION (c
),
12295 "%qE appears more than once in data clauses", t
);
12299 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
12302 case OMP_CLAUSE_ALIGNED
:
12303 t
= OMP_CLAUSE_DECL (c
);
12304 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12306 error_at (OMP_CLAUSE_LOCATION (c
),
12307 "%qE is not a variable in %<aligned%> clause", t
);
12310 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
12311 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
12313 error_at (OMP_CLAUSE_LOCATION (c
),
12314 "%qE in %<aligned%> clause is neither a pointer nor "
12318 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
12320 error_at (OMP_CLAUSE_LOCATION (c
),
12321 "%qE appears more than once in %<aligned%> clauses",
12326 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
12329 case OMP_CLAUSE_DEPEND
:
12330 t
= OMP_CLAUSE_DECL (c
);
12331 if (TREE_CODE (t
) == TREE_LIST
)
12333 if (handle_omp_array_sections (c
))
12337 if (t
== error_mark_node
)
12339 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12341 error_at (OMP_CLAUSE_LOCATION (c
),
12342 "%qE is not a variable in %<depend%> clause", t
);
12345 else if (!c_mark_addressable (t
))
12349 case OMP_CLAUSE_MAP
:
12350 case OMP_CLAUSE_TO
:
12351 case OMP_CLAUSE_FROM
:
12352 case OMP_CLAUSE__CACHE_
:
12353 t
= OMP_CLAUSE_DECL (c
);
12354 if (TREE_CODE (t
) == TREE_LIST
)
12356 if (handle_omp_array_sections (c
))
12360 t
= OMP_CLAUSE_DECL (c
);
12361 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12363 error_at (OMP_CLAUSE_LOCATION (c
),
12364 "array section does not have mappable type "
12366 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12372 if (t
== error_mark_node
)
12374 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12376 error_at (OMP_CLAUSE_LOCATION (c
),
12377 "%qE is not a variable in %qs clause", t
,
12378 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12381 else if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12383 error_at (OMP_CLAUSE_LOCATION (c
),
12384 "%qD is threadprivate variable in %qs clause", t
,
12385 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12388 else if (!c_mark_addressable (t
))
12390 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12391 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
12392 || (OMP_CLAUSE_MAP_KIND (c
)
12393 == GOMP_MAP_FORCE_DEVICEPTR
)))
12394 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12396 error_at (OMP_CLAUSE_LOCATION (c
),
12397 "%qD does not have a mappable type in %qs clause", t
,
12398 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12401 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
12403 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
12404 error ("%qD appears more than once in motion clauses", t
);
12406 error ("%qD appears more than once in map clauses", t
);
12410 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12413 case OMP_CLAUSE_UNIFORM
:
12414 t
= OMP_CLAUSE_DECL (c
);
12415 if (TREE_CODE (t
) != PARM_DECL
)
12418 error_at (OMP_CLAUSE_LOCATION (c
),
12419 "%qD is not an argument in %<uniform%> clause", t
);
12421 error_at (OMP_CLAUSE_LOCATION (c
),
12422 "%qE is not an argument in %<uniform%> clause", t
);
12426 goto check_dup_generic
;
12428 case OMP_CLAUSE_NOWAIT
:
12429 if (copyprivate_seen
)
12431 error_at (OMP_CLAUSE_LOCATION (c
),
12432 "%<nowait%> clause must not be used together "
12433 "with %<copyprivate%>");
12437 nowait_clause
= pc
;
12438 pc
= &OMP_CLAUSE_CHAIN (c
);
12441 case OMP_CLAUSE_IF
:
12442 case OMP_CLAUSE_NUM_THREADS
:
12443 case OMP_CLAUSE_NUM_TEAMS
:
12444 case OMP_CLAUSE_THREAD_LIMIT
:
12445 case OMP_CLAUSE_SCHEDULE
:
12446 case OMP_CLAUSE_ORDERED
:
12447 case OMP_CLAUSE_DEFAULT
:
12448 case OMP_CLAUSE_UNTIED
:
12449 case OMP_CLAUSE_COLLAPSE
:
12450 case OMP_CLAUSE_FINAL
:
12451 case OMP_CLAUSE_MERGEABLE
:
12452 case OMP_CLAUSE_SAFELEN
:
12453 case OMP_CLAUSE_SIMDLEN
:
12454 case OMP_CLAUSE_DEVICE
:
12455 case OMP_CLAUSE_DIST_SCHEDULE
:
12456 case OMP_CLAUSE_PARALLEL
:
12457 case OMP_CLAUSE_FOR
:
12458 case OMP_CLAUSE_SECTIONS
:
12459 case OMP_CLAUSE_TASKGROUP
:
12460 case OMP_CLAUSE_PROC_BIND
:
12461 case OMP_CLAUSE__CILK_FOR_COUNT_
:
12462 case OMP_CLAUSE_NUM_GANGS
:
12463 case OMP_CLAUSE_NUM_WORKERS
:
12464 case OMP_CLAUSE_VECTOR_LENGTH
:
12465 case OMP_CLAUSE_ASYNC
:
12466 case OMP_CLAUSE_WAIT
:
12467 case OMP_CLAUSE_AUTO
:
12468 case OMP_CLAUSE_SEQ
:
12469 case OMP_CLAUSE_GANG
:
12470 case OMP_CLAUSE_WORKER
:
12471 case OMP_CLAUSE_VECTOR
:
12472 pc
= &OMP_CLAUSE_CHAIN (c
);
12475 case OMP_CLAUSE_INBRANCH
:
12476 case OMP_CLAUSE_NOTINBRANCH
:
12479 error_at (OMP_CLAUSE_LOCATION (c
),
12480 "%<inbranch%> clause is incompatible with "
12481 "%<notinbranch%>");
12485 branch_seen
= true;
12486 pc
= &OMP_CLAUSE_CHAIN (c
);
12490 gcc_unreachable ();
12495 t
= OMP_CLAUSE_DECL (c
);
12499 t
= require_complete_type (t
);
12500 if (t
== error_mark_node
)
12504 if (need_implicitly_determined
)
12506 const char *share_name
= NULL
;
12508 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12509 share_name
= "threadprivate";
12510 else switch (c_omp_predetermined_sharing (t
))
12512 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
12514 case OMP_CLAUSE_DEFAULT_SHARED
:
12515 /* const vars may be specified in firstprivate clause. */
12516 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12517 && TREE_READONLY (t
))
12519 share_name
= "shared";
12521 case OMP_CLAUSE_DEFAULT_PRIVATE
:
12522 share_name
= "private";
12525 gcc_unreachable ();
12529 error_at (OMP_CLAUSE_LOCATION (c
),
12530 "%qE is predetermined %qs for %qs",
12532 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12539 *pc
= OMP_CLAUSE_CHAIN (c
);
12541 pc
= &OMP_CLAUSE_CHAIN (c
);
12544 bitmap_obstack_release (NULL
);
12548 /* Create a transaction node. */
12551 c_finish_transaction (location_t loc
, tree block
, int flags
)
12553 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
12554 if (flags
& TM_STMT_ATTR_OUTER
)
12555 TRANSACTION_EXPR_OUTER (stmt
) = 1;
12556 if (flags
& TM_STMT_ATTR_RELAXED
)
12557 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
12558 return add_stmt (stmt
);
12561 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12562 down to the element type of an array. */
12565 c_build_qualified_type (tree type
, int type_quals
)
12567 if (type
== error_mark_node
)
12570 if (TREE_CODE (type
) == ARRAY_TYPE
)
12573 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
12576 /* See if we already have an identically qualified type. */
12577 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12579 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
12580 && TYPE_NAME (t
) == TYPE_NAME (type
)
12581 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
12582 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
12583 TYPE_ATTRIBUTES (type
)))
12588 tree domain
= TYPE_DOMAIN (type
);
12590 t
= build_variant_type_copy (type
);
12591 TREE_TYPE (t
) = element_type
;
12593 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
12594 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
12595 SET_TYPE_STRUCTURAL_EQUALITY (t
);
12596 else if (TYPE_CANONICAL (element_type
) != element_type
12597 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
12599 tree unqualified_canon
12600 = build_array_type (TYPE_CANONICAL (element_type
),
12601 domain
? TYPE_CANONICAL (domain
)
12604 = c_build_qualified_type (unqualified_canon
, type_quals
);
12607 TYPE_CANONICAL (t
) = t
;
12612 /* A restrict-qualified pointer type must be a pointer to object or
12613 incomplete type. Note that the use of POINTER_TYPE_P also allows
12614 REFERENCE_TYPEs, which is appropriate for C++. */
12615 if ((type_quals
& TYPE_QUAL_RESTRICT
)
12616 && (!POINTER_TYPE_P (type
)
12617 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
12619 error ("invalid use of %<restrict%>");
12620 type_quals
&= ~TYPE_QUAL_RESTRICT
;
12623 return build_qualified_type (type
, type_quals
);
12626 /* Build a VA_ARG_EXPR for the C parser. */
12629 c_build_va_arg (location_t loc
, tree expr
, tree type
)
12631 if (error_operand_p (type
))
12632 return error_mark_node
;
12633 else if (!COMPLETE_TYPE_P (type
))
12635 error_at (loc
, "second argument to %<va_arg%> is of incomplete "
12637 return error_mark_node
;
12639 else if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
12640 warning_at (loc
, OPT_Wc___compat
,
12641 "C++ requires promoted type, not enum type, in %<va_arg%>");
12642 return build_va_arg (loc
, expr
, type
);
12645 /* Return truthvalue of whether T1 is the same tree structure as T2.
12646 Return 1 if they are the same. Return 0 if they are different. */
12649 c_tree_equal (tree t1
, tree t2
)
12651 enum tree_code code1
, code2
;
12658 for (code1
= TREE_CODE (t1
);
12659 CONVERT_EXPR_CODE_P (code1
)
12660 || code1
== NON_LVALUE_EXPR
;
12661 code1
= TREE_CODE (t1
))
12662 t1
= TREE_OPERAND (t1
, 0);
12663 for (code2
= TREE_CODE (t2
);
12664 CONVERT_EXPR_CODE_P (code2
)
12665 || code2
== NON_LVALUE_EXPR
;
12666 code2
= TREE_CODE (t2
))
12667 t2
= TREE_OPERAND (t2
, 0);
12669 /* They might have become equal now. */
12673 if (code1
!= code2
)
12679 return wi::eq_p (t1
, t2
);
12682 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1
), TREE_REAL_CST (t2
));
12685 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
12686 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
12687 TREE_STRING_LENGTH (t1
));
12690 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
12691 TREE_FIXED_CST (t2
));
12694 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
12695 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
12698 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
12701 /* We need to do this when determining whether or not two
12702 non-type pointer to member function template arguments
12704 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
12705 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
12710 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
12712 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
12713 if (!c_tree_equal (field
, elt2
->index
)
12714 || !c_tree_equal (value
, elt2
->value
))
12721 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
12723 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
12725 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
12728 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12733 call_expr_arg_iterator iter1
, iter2
;
12734 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
12736 for (arg1
= first_call_expr_arg (t1
, &iter1
),
12737 arg2
= first_call_expr_arg (t2
, &iter2
);
12739 arg1
= next_call_expr_arg (&iter1
),
12740 arg2
= next_call_expr_arg (&iter2
))
12741 if (!c_tree_equal (arg1
, arg2
))
12750 tree o1
= TREE_OPERAND (t1
, 0);
12751 tree o2
= TREE_OPERAND (t2
, 0);
12753 /* Special case: if either target is an unallocated VAR_DECL,
12754 it means that it's going to be unified with whatever the
12755 TARGET_EXPR is really supposed to initialize, so treat it
12756 as being equivalent to anything. */
12757 if (TREE_CODE (o1
) == VAR_DECL
&& DECL_NAME (o1
) == NULL_TREE
12758 && !DECL_RTL_SET_P (o1
))
12760 else if (TREE_CODE (o2
) == VAR_DECL
&& DECL_NAME (o2
) == NULL_TREE
12761 && !DECL_RTL_SET_P (o2
))
12763 else if (!c_tree_equal (o1
, o2
))
12766 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
12769 case COMPONENT_REF
:
12770 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
12772 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12778 case FUNCTION_DECL
:
12779 case IDENTIFIER_NODE
:
12786 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
12788 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
12789 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
12790 TREE_VEC_ELT (t2
, ix
)))
12799 switch (TREE_CODE_CLASS (code1
))
12803 case tcc_comparison
:
12804 case tcc_expression
:
12806 case tcc_reference
:
12807 case tcc_statement
:
12809 int i
, n
= TREE_OPERAND_LENGTH (t1
);
12813 case PREINCREMENT_EXPR
:
12814 case PREDECREMENT_EXPR
:
12815 case POSTINCREMENT_EXPR
:
12816 case POSTDECREMENT_EXPR
:
12826 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
12827 && n
!= TREE_OPERAND_LENGTH (t2
))
12830 for (i
= 0; i
< n
; ++i
)
12831 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
12838 return comptypes (t1
, t2
);
12840 gcc_unreachable ();
12842 /* We can get here with --disable-checking. */
12846 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12847 spawn-helper and BODY is the newly created body for FNDECL. */
12850 cilk_install_body_with_frame_cleanup (tree fndecl
, tree body
, void *w
)
12852 tree list
= alloc_stmt_list ();
12853 tree frame
= make_cilk_frame (fndecl
);
12854 tree dtor
= create_cilk_function_exit (frame
, false, true);
12855 add_local_decl (cfun
, frame
);
12857 DECL_SAVED_TREE (fndecl
) = list
;
12858 tree frame_ptr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (frame
)),
12860 tree body_list
= cilk_install_body_pedigree_operations (frame_ptr
);
12861 gcc_assert (TREE_CODE (body_list
) == STATEMENT_LIST
);
12863 tree detach_expr
= build_call_expr (cilk_detach_fndecl
, 1, frame_ptr
);
12864 append_to_statement_list (detach_expr
, &body_list
);
12866 cilk_outline (fndecl
, &body
, (struct wrapper_data
*) w
);
12867 body
= fold_build_cleanup_point_expr (void_type_node
, body
);
12869 append_to_statement_list (body
, &body_list
);
12870 append_to_statement_list (build_stmt (EXPR_LOCATION (body
), TRY_FINALLY_EXPR
,
12871 body_list
, dtor
), &list
);