1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2013 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"
31 #include "stor-layout.h"
32 #include "trans-mem.h"
35 #include "langhooks.h"
41 #include "tree-iterator.h"
45 #include "tree-inline.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-common.h"
49 #include "c-family/c-ubsan.h"
51 /* Possible cases of implicit bad conversions. Used to select
52 diagnostic messages in convert_for_assignment. */
60 /* The level of nesting inside "__alignof__". */
63 /* The level of nesting inside "sizeof". */
66 /* The level of nesting inside "typeof". */
69 /* The argument of last parsed sizeof expression, only to be tested
70 if expr.original_code == SIZEOF_EXPR. */
71 tree c_last_sizeof_arg
;
73 /* Nonzero if we've already printed a "missing braces around initializer"
74 message within this initializer. */
75 static int missing_braces_mentioned
;
77 static int require_constant_value
;
78 static int require_constant_elements
;
80 static bool null_pointer_constant_p (const_tree
);
81 static tree
qualify_type (tree
, tree
);
82 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
84 static int comp_target_types (location_t
, tree
, tree
);
85 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
87 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
88 static tree
lookup_field (tree
, tree
);
89 static int convert_arguments (tree
, vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *,
91 static tree
pointer_diff (location_t
, tree
, tree
);
92 static tree
convert_for_assignment (location_t
, tree
, tree
, tree
,
93 enum impl_conv
, bool, tree
, tree
, int);
94 static tree
valid_compound_expr_initializer (tree
, tree
);
95 static void push_string (const char *);
96 static void push_member_name (tree
);
97 static int spelling_length (void);
98 static char *print_spelling (char *);
99 static void warning_init (int, const char *);
100 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
101 static void output_init_element (tree
, tree
, bool, tree
, tree
, int, bool,
103 static void output_pending_init_elements (int, struct obstack
*);
104 static int set_designator (int, struct obstack
*);
105 static void push_range_stack (tree
, struct obstack
*);
106 static void add_pending_init (tree
, tree
, tree
, bool, struct obstack
*);
107 static void set_nonincremental_init (struct obstack
*);
108 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
109 static tree
find_init_member (tree
, struct obstack
*);
110 static void readonly_warning (tree
, enum lvalue_use
);
111 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
112 static void record_maybe_used_decl (tree
);
113 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
115 /* Return true if EXP is a null pointer constant, false otherwise. */
118 null_pointer_constant_p (const_tree expr
)
120 /* This should really operate on c_expr structures, but they aren't
121 yet available everywhere required. */
122 tree type
= TREE_TYPE (expr
);
123 return (TREE_CODE (expr
) == INTEGER_CST
124 && !TREE_OVERFLOW (expr
)
125 && integer_zerop (expr
)
126 && (INTEGRAL_TYPE_P (type
)
127 || (TREE_CODE (type
) == POINTER_TYPE
128 && VOID_TYPE_P (TREE_TYPE (type
))
129 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
132 /* EXPR may appear in an unevaluated part of an integer constant
133 expression, but not in an evaluated part. Wrap it in a
134 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
135 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
138 note_integer_operands (tree expr
)
141 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
143 ret
= copy_node (expr
);
144 TREE_OVERFLOW (ret
) = 1;
148 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
149 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
154 /* Having checked whether EXPR may appear in an unevaluated part of an
155 integer constant expression and found that it may, remove any
156 C_MAYBE_CONST_EXPR noting this fact and return the resulting
160 remove_c_maybe_const_expr (tree expr
)
162 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
163 return C_MAYBE_CONST_EXPR_EXPR (expr
);
168 \f/* This is a cache to hold if two types are compatible or not. */
170 struct tagged_tu_seen_cache
{
171 const struct tagged_tu_seen_cache
* next
;
174 /* The return value of tagged_types_tu_compatible_p if we had seen
175 these two types already. */
179 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
180 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
182 /* Do `exp = require_complete_type (exp);' to make sure exp
183 does not have an incomplete type. (That includes void types.) */
186 require_complete_type (tree value
)
188 tree type
= TREE_TYPE (value
);
190 if (value
== error_mark_node
|| type
== error_mark_node
)
191 return error_mark_node
;
193 /* First, detect a valid value with a complete type. */
194 if (COMPLETE_TYPE_P (type
))
197 c_incomplete_type_error (value
, type
);
198 return error_mark_node
;
201 /* Print an error message for invalid use of an incomplete type.
202 VALUE is the expression that was used (or 0 if that isn't known)
203 and TYPE is the type that was invalid. */
206 c_incomplete_type_error (const_tree value
, const_tree type
)
208 const char *type_code_string
;
210 /* Avoid duplicate error message. */
211 if (TREE_CODE (type
) == ERROR_MARK
)
214 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
215 || TREE_CODE (value
) == PARM_DECL
))
216 error ("%qD has an incomplete type", value
);
220 /* We must print an error message. Be clever about what it says. */
222 switch (TREE_CODE (type
))
225 type_code_string
= "struct";
229 type_code_string
= "union";
233 type_code_string
= "enum";
237 error ("invalid use of void expression");
241 if (TYPE_DOMAIN (type
))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
245 error ("invalid use of flexible array member");
248 type
= TREE_TYPE (type
);
251 error ("invalid use of array with unspecified bounds");
258 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
259 error ("invalid use of undefined type %<%s %E%>",
260 type_code_string
, TYPE_NAME (type
));
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type
));
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
271 c_type_promotes_to (tree type
)
273 tree ret
= NULL_TREE
;
275 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
276 ret
= double_type_node
;
277 else if (c_promoting_integer_type_p (type
))
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type
)
281 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
282 ret
= unsigned_type_node
;
284 ret
= integer_type_node
;
287 if (ret
!= NULL_TREE
)
288 return (TYPE_ATOMIC (type
)
289 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
295 /* Return true if between two named address spaces, whether there is a superset
296 named address space that encompasses both address spaces. If there is a
297 superset, return which address space is the superset. */
300 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
307 else if (targetm
.addr_space
.subset_p (as1
, as2
))
312 else if (targetm
.addr_space
.subset_p (as2
, as1
))
321 /* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
325 qualify_type (tree type
, tree like
)
327 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
328 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
329 addr_space_t as_common
;
331 /* If the two named address spaces are different, determine the common
332 superset address space. If there isn't one, raise an error. */
333 if (!addr_space_superset (as_type
, as_like
, &as_common
))
336 error ("%qT and %qT are in disjoint named address spaces",
340 return c_build_qualified_type (type
,
341 TYPE_QUALS_NO_ADDR_SPACE (type
)
342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
343 | ENCODE_QUAL_ADDR_SPACE (as_common
));
346 /* Return true iff the given tree T is a variable length array. */
349 c_vla_type_p (const_tree t
)
351 if (TREE_CODE (t
) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t
))
357 /* Return the composite type of two compatible types.
359 We assume that comptypes has already been done and returned
360 nonzero; if that isn't so, this may crash. In particular, we
361 assume that qualifiers match. */
364 composite_type (tree t1
, tree t2
)
366 enum tree_code code1
;
367 enum tree_code code2
;
370 /* Save time if the two types are the same. */
372 if (t1
== t2
) return t1
;
374 /* If one type is nonsense, use the other. */
375 if (t1
== error_mark_node
)
377 if (t2
== error_mark_node
)
380 code1
= TREE_CODE (t1
);
381 code2
= TREE_CODE (t2
);
383 /* Merge the attributes. */
384 attributes
= targetm
.merge_type_attributes (t1
, t2
);
386 /* If one is an enumerated type and the other is the compatible
387 integer type, the composite type might be either of the two
388 (DR#013 question 3). For consistency, use the enumerated type as
389 the composite type. */
391 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
393 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
396 gcc_assert (code1
== code2
);
401 /* For two pointers, do this recursively on the target type. */
403 tree pointed_to_1
= TREE_TYPE (t1
);
404 tree pointed_to_2
= TREE_TYPE (t2
);
405 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
406 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
407 t1
= build_type_attribute_variant (t1
, attributes
);
408 return qualify_type (t1
, t2
);
413 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
416 tree d1
= TYPE_DOMAIN (t1
);
417 tree d2
= TYPE_DOMAIN (t2
);
418 bool d1_variable
, d2_variable
;
419 bool d1_zero
, d2_zero
;
420 bool t1_complete
, t2_complete
;
422 /* We should not have any type quals on arrays at all. */
423 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
424 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
426 t1_complete
= COMPLETE_TYPE_P (t1
);
427 t2_complete
= COMPLETE_TYPE_P (t2
);
429 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
430 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
432 d1_variable
= (!d1_zero
433 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
434 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
435 d2_variable
= (!d2_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
438 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
439 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
441 /* Save space: see if the result is identical to one of the args. */
442 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
443 && (d2_variable
|| d2_zero
|| !d1_variable
))
444 return build_type_attribute_variant (t1
, attributes
);
445 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
446 && (d1_variable
|| d1_zero
|| !d2_variable
))
447 return build_type_attribute_variant (t2
, attributes
);
449 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
450 return build_type_attribute_variant (t1
, attributes
);
451 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
452 return build_type_attribute_variant (t2
, attributes
);
454 /* Merge the element types, and have a size if either arg has
455 one. We may have qualifiers on the element types. To set
456 up TYPE_MAIN_VARIANT correctly, we need to form the
457 composite of the unqualified types and add the qualifiers
459 quals
= TYPE_QUALS (strip_array_types (elt
));
460 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
461 t1
= build_array_type (unqual_elt
,
462 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
468 /* Ensure a composite type involving a zero-length array type
469 is a zero-length type not an incomplete type. */
470 if (d1_zero
&& d2_zero
471 && (t1_complete
|| t2_complete
)
472 && !COMPLETE_TYPE_P (t1
))
474 TYPE_SIZE (t1
) = bitsize_zero_node
;
475 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
477 t1
= c_build_qualified_type (t1
, quals
);
478 return build_type_attribute_variant (t1
, attributes
);
484 if (attributes
!= NULL
)
486 /* Try harder not to create a new aggregate type. */
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
489 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
492 return build_type_attribute_variant (t1
, attributes
);
495 /* Function types: prefer the one that specified arg types.
496 If both do, merge the arg types. Also merge the return types. */
498 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
499 tree p1
= TYPE_ARG_TYPES (t1
);
500 tree p2
= TYPE_ARG_TYPES (t2
);
505 /* Save space: see if the result is identical to one of the args. */
506 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
507 return build_type_attribute_variant (t1
, attributes
);
508 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
509 return build_type_attribute_variant (t2
, attributes
);
511 /* Simple way if one arg fails to specify argument types. */
512 if (TYPE_ARG_TYPES (t1
) == 0)
514 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
515 t1
= build_type_attribute_variant (t1
, attributes
);
516 return qualify_type (t1
, t2
);
518 if (TYPE_ARG_TYPES (t2
) == 0)
520 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
521 t1
= build_type_attribute_variant (t1
, attributes
);
522 return qualify_type (t1
, t2
);
525 /* If both args specify argument types, we must merge the two
526 lists, argument by argument. */
528 len
= list_length (p1
);
531 for (i
= 0; i
< len
; i
++)
532 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
537 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
539 /* A null type means arg type is not specified.
540 Take whatever the other function type has. */
541 if (TREE_VALUE (p1
) == 0)
543 TREE_VALUE (n
) = TREE_VALUE (p2
);
546 if (TREE_VALUE (p2
) == 0)
548 TREE_VALUE (n
) = TREE_VALUE (p1
);
552 /* Given wait (union {union wait *u; int *i} *)
553 and wait (union wait *),
554 prefer union wait * as type of parm. */
555 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
556 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
559 tree mv2
= TREE_VALUE (p2
);
560 if (mv2
&& mv2
!= error_mark_node
561 && TREE_CODE (mv2
) != ARRAY_TYPE
)
562 mv2
= TYPE_MAIN_VARIANT (mv2
);
563 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
564 memb
; memb
= DECL_CHAIN (memb
))
566 tree mv3
= TREE_TYPE (memb
);
567 if (mv3
&& mv3
!= error_mark_node
568 && TREE_CODE (mv3
) != ARRAY_TYPE
)
569 mv3
= TYPE_MAIN_VARIANT (mv3
);
570 if (comptypes (mv3
, mv2
))
572 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
574 pedwarn (input_location
, OPT_Wpedantic
,
575 "function types not truly compatible in ISO C");
580 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
581 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
584 tree mv1
= TREE_VALUE (p1
);
585 if (mv1
&& mv1
!= error_mark_node
586 && TREE_CODE (mv1
) != ARRAY_TYPE
)
587 mv1
= TYPE_MAIN_VARIANT (mv1
);
588 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
589 memb
; memb
= DECL_CHAIN (memb
))
591 tree mv3
= TREE_TYPE (memb
);
592 if (mv3
&& mv3
!= error_mark_node
593 && TREE_CODE (mv3
) != ARRAY_TYPE
)
594 mv3
= TYPE_MAIN_VARIANT (mv3
);
595 if (comptypes (mv3
, mv1
))
597 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
599 pedwarn (input_location
, OPT_Wpedantic
,
600 "function types not truly compatible in ISO C");
605 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
609 t1
= build_function_type (valtype
, newargs
);
610 t1
= qualify_type (t1
, t2
);
611 /* ... falls through ... */
615 return build_type_attribute_variant (t1
, attributes
);
620 /* Return the type of a conditional expression between pointers to
621 possibly differently qualified versions of compatible types.
623 We assume that comp_target_types has already been done and returned
624 nonzero; if that isn't so, this may crash. */
627 common_pointer_type (tree t1
, tree t2
)
630 tree pointed_to_1
, mv1
;
631 tree pointed_to_2
, mv2
;
633 unsigned target_quals
;
634 addr_space_t as1
, as2
, as_common
;
637 /* Save time if the two types are the same. */
639 if (t1
== t2
) return t1
;
641 /* If one type is nonsense, use the other. */
642 if (t1
== error_mark_node
)
644 if (t2
== error_mark_node
)
647 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
648 && TREE_CODE (t2
) == POINTER_TYPE
);
650 /* Merge the attributes. */
651 attributes
= targetm
.merge_type_attributes (t1
, t2
);
653 /* Find the composite type of the target types, and combine the
654 qualifiers of the two types' targets. Do not lose qualifiers on
655 array element types by taking the TYPE_MAIN_VARIANT. */
656 mv1
= pointed_to_1
= TREE_TYPE (t1
);
657 mv2
= pointed_to_2
= TREE_TYPE (t2
);
658 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
659 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
660 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
661 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
662 target
= composite_type (mv1
, mv2
);
664 /* For function types do not merge const qualifiers, but drop them
665 if used inconsistently. The middle-end uses these to mark const
666 and noreturn functions. */
667 quals1
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1
);
668 quals2
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2
);
670 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
671 target_quals
= (quals1
& quals2
);
673 target_quals
= (quals1
| quals2
);
675 /* If the two named address spaces are different, determine the common
676 superset address space. This is guaranteed to exist due to the
677 assumption that comp_target_type returned non-zero. */
678 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
679 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
680 if (!addr_space_superset (as1
, as2
, &as_common
))
683 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
685 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
686 return build_type_attribute_variant (t1
, attributes
);
689 /* Return the common type for two arithmetic types under the usual
690 arithmetic conversions. The default conversions have already been
691 applied, and enumerated types converted to their compatible integer
692 types. The resulting type is unqualified and has no attributes.
694 This is the type for the result of most arithmetic operations
695 if the operands have the given two types. */
698 c_common_type (tree t1
, tree t2
)
700 enum tree_code code1
;
701 enum tree_code code2
;
703 /* If one type is nonsense, use the other. */
704 if (t1
== error_mark_node
)
706 if (t2
== error_mark_node
)
709 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
710 t1
= TYPE_MAIN_VARIANT (t1
);
712 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
713 t2
= TYPE_MAIN_VARIANT (t2
);
715 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
716 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
718 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
719 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
721 /* Save time if the two types are the same. */
723 if (t1
== t2
) return t1
;
725 code1
= TREE_CODE (t1
);
726 code2
= TREE_CODE (t2
);
728 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
729 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
730 || code1
== INTEGER_TYPE
);
731 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
732 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
733 || code2
== INTEGER_TYPE
);
735 /* When one operand is a decimal float type, the other operand cannot be
736 a generic float type or a complex type. We also disallow vector types
738 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
739 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
741 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
743 error ("can%'t mix operands of decimal float and vector types");
744 return error_mark_node
;
746 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
748 error ("can%'t mix operands of decimal float and complex types");
749 return error_mark_node
;
751 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
753 error ("can%'t mix operands of decimal float and other float types");
754 return error_mark_node
;
758 /* If one type is a vector type, return that type. (How the usual
759 arithmetic conversions apply to the vector types extension is not
760 precisely specified.) */
761 if (code1
== VECTOR_TYPE
)
764 if (code2
== VECTOR_TYPE
)
767 /* If one type is complex, form the common type of the non-complex
768 components, then make that complex. Use T1 or T2 if it is the
770 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
772 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
773 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
774 tree subtype
= c_common_type (subtype1
, subtype2
);
776 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
778 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
781 return build_complex_type (subtype
);
784 /* If only one is real, use it as the result. */
786 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
789 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
792 /* If both are real and either are decimal floating point types, use
793 the decimal floating point type with the greater precision. */
795 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
797 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
798 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
799 return dfloat128_type_node
;
800 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
801 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
802 return dfloat64_type_node
;
803 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
804 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
805 return dfloat32_type_node
;
808 /* Deal with fixed-point types. */
809 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
811 unsigned int unsignedp
= 0, satp
= 0;
812 enum machine_mode m1
, m2
;
813 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
818 /* If one input type is saturating, the result type is saturating. */
819 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
822 /* If both fixed-point types are unsigned, the result type is unsigned.
823 When mixing fixed-point and integer types, follow the sign of the
825 Otherwise, the result type is signed. */
826 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
827 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
828 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
829 && TYPE_UNSIGNED (t1
))
830 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t2
)))
834 /* The result type is signed. */
837 /* If the input type is unsigned, we need to convert to the
839 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
841 enum mode_class mclass
= (enum mode_class
) 0;
842 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
844 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
848 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
850 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
852 enum mode_class mclass
= (enum mode_class
) 0;
853 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
855 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
859 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
863 if (code1
== FIXED_POINT_TYPE
)
865 fbit1
= GET_MODE_FBIT (m1
);
866 ibit1
= GET_MODE_IBIT (m1
);
871 /* Signed integers need to subtract one sign bit. */
872 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
875 if (code2
== FIXED_POINT_TYPE
)
877 fbit2
= GET_MODE_FBIT (m2
);
878 ibit2
= GET_MODE_IBIT (m2
);
883 /* Signed integers need to subtract one sign bit. */
884 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
887 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
888 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
889 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
893 /* Both real or both integers; use the one with greater precision. */
895 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
897 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
900 /* Same precision. Prefer long longs to longs to ints when the
901 same precision, following the C99 rules on integer type rank
902 (which are equivalent to the C90 rules for C90 types). */
904 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
905 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
906 return long_long_unsigned_type_node
;
908 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
909 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
911 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
912 return long_long_unsigned_type_node
;
914 return long_long_integer_type_node
;
917 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
918 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
919 return long_unsigned_type_node
;
921 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
922 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
924 /* But preserve unsignedness from the other type,
925 since long cannot hold all the values of an unsigned int. */
926 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
927 return long_unsigned_type_node
;
929 return long_integer_type_node
;
932 /* Likewise, prefer long double to double even if same size. */
933 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
934 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
935 return long_double_type_node
;
937 /* Likewise, prefer double to float even if same size.
938 We got a couple of embedded targets with 32 bit doubles, and the
939 pdp11 might have 64 bit floats. */
940 if (TYPE_MAIN_VARIANT (t1
) == double_type_node
941 || TYPE_MAIN_VARIANT (t2
) == double_type_node
)
942 return double_type_node
;
944 /* Otherwise prefer the unsigned one. */
946 if (TYPE_UNSIGNED (t1
))
952 /* Wrapper around c_common_type that is used by c-common.c and other
953 front end optimizations that remove promotions. ENUMERAL_TYPEs
954 are allowed here and are converted to their compatible integer types.
955 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
956 preferably a non-Boolean type as the common type. */
958 common_type (tree t1
, tree t2
)
960 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
961 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
962 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
963 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
965 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
966 if (TREE_CODE (t1
) == BOOLEAN_TYPE
967 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
968 return boolean_type_node
;
970 /* If either type is BOOLEAN_TYPE, then return the other. */
971 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
973 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
976 return c_common_type (t1
, t2
);
979 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
980 or various other operations. Return 2 if they are compatible
981 but a warning may be needed if you use them together. */
984 comptypes (tree type1
, tree type2
)
986 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
989 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
990 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
995 /* Like comptypes, but if it returns non-zero because enum and int are
996 compatible, it sets *ENUM_AND_INT_P to true. */
999 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1001 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1004 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1010 /* Like comptypes, but if it returns nonzero for different types, it
1011 sets *DIFFERENT_TYPES_P to true. */
1014 comptypes_check_different_types (tree type1
, tree type2
,
1015 bool *different_types_p
)
1017 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1020 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1021 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1026 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1027 or various other operations. Return 2 if they are compatible
1028 but a warning may be needed if you use them together. If
1029 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1030 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1031 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1032 NULL, and the types are compatible but different enough not to be
1033 permitted in C11 typedef redeclarations, then this sets
1034 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1035 false, but may or may not be set if the types are incompatible.
1036 This differs from comptypes, in that we don't free the seen
1040 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1041 bool *different_types_p
)
1043 const_tree t1
= type1
;
1044 const_tree t2
= type2
;
1047 /* Suppress errors caused by previously reported errors. */
1049 if (t1
== t2
|| !t1
|| !t2
1050 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1053 /* Enumerated types are compatible with integer types, but this is
1054 not transitive: two enumerated types in the same translation unit
1055 are compatible with each other only if they are the same type. */
1057 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1059 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1060 if (TREE_CODE (t2
) != VOID_TYPE
)
1062 if (enum_and_int_p
!= NULL
)
1063 *enum_and_int_p
= true;
1064 if (different_types_p
!= NULL
)
1065 *different_types_p
= true;
1068 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1070 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1071 if (TREE_CODE (t1
) != VOID_TYPE
)
1073 if (enum_and_int_p
!= NULL
)
1074 *enum_and_int_p
= true;
1075 if (different_types_p
!= NULL
)
1076 *different_types_p
= true;
1083 /* Different classes of types can't be compatible. */
1085 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1088 /* Qualifiers must match. C99 6.7.3p9 */
1090 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1093 /* Allow for two different type nodes which have essentially the same
1094 definition. Note that we already checked for equality of the type
1095 qualifiers (just above). */
1097 if (TREE_CODE (t1
) != ARRAY_TYPE
1098 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1101 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1102 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1105 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1108 switch (TREE_CODE (t1
))
1111 /* Do not remove mode or aliasing information. */
1112 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1113 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1115 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1116 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1117 enum_and_int_p
, different_types_p
));
1121 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1127 tree d1
= TYPE_DOMAIN (t1
);
1128 tree d2
= TYPE_DOMAIN (t2
);
1129 bool d1_variable
, d2_variable
;
1130 bool d1_zero
, d2_zero
;
1133 /* Target types must match incl. qualifiers. */
1134 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1135 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1137 different_types_p
)))
1140 if (different_types_p
!= NULL
1141 && (d1
== 0) != (d2
== 0))
1142 *different_types_p
= true;
1143 /* Sizes must match unless one is missing or variable. */
1144 if (d1
== 0 || d2
== 0 || d1
== d2
)
1147 d1_zero
= !TYPE_MAX_VALUE (d1
);
1148 d2_zero
= !TYPE_MAX_VALUE (d2
);
1150 d1_variable
= (!d1_zero
1151 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1152 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1153 d2_variable
= (!d2_zero
1154 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1155 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1156 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1157 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1159 if (different_types_p
!= NULL
1160 && d1_variable
!= d2_variable
)
1161 *different_types_p
= true;
1162 if (d1_variable
|| d2_variable
)
1164 if (d1_zero
&& d2_zero
)
1166 if (d1_zero
|| d2_zero
1167 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1168 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1177 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1179 tree a1
= TYPE_ATTRIBUTES (t1
);
1180 tree a2
= TYPE_ATTRIBUTES (t2
);
1182 if (! attribute_list_contained (a1
, a2
)
1183 && ! attribute_list_contained (a2
, a1
))
1187 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1189 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1195 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1196 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1197 enum_and_int_p
, different_types_p
));
1203 return attrval
== 2 && val
== 1 ? 2 : val
;
1206 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1207 their qualifiers, except for named address spaces. If the pointers point to
1208 different named addresses, then we must determine if one address space is a
1209 subset of the other. */
1212 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1215 tree mvl
= TREE_TYPE (ttl
);
1216 tree mvr
= TREE_TYPE (ttr
);
1217 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1218 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1219 addr_space_t as_common
;
1220 bool enum_and_int_p
;
1222 /* Fail if pointers point to incompatible address spaces. */
1223 if (!addr_space_superset (asl
, asr
, &as_common
))
1226 /* Do not lose qualifiers on element types of array types that are
1227 pointer targets by taking their TYPE_MAIN_VARIANT. */
1228 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
1229 mvl
= (TYPE_ATOMIC (mvl
)
1230 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1231 : TYPE_MAIN_VARIANT (mvl
));
1232 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
1233 mvr
= (TYPE_ATOMIC (mvr
)
1234 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1235 : TYPE_MAIN_VARIANT (mvr
));
1236 enum_and_int_p
= false;
1237 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1240 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1242 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1243 warning_at (location
, OPT_Wc___compat
,
1244 "pointer target types incompatible in C++");
1249 /* Subroutines of `comptypes'. */
1251 /* Determine whether two trees derive from the same translation unit.
1252 If the CONTEXT chain ends in a null, that tree's context is still
1253 being parsed, so if two trees have context chains ending in null,
1254 they're in the same translation unit. */
1256 same_translation_unit_p (const_tree t1
, const_tree t2
)
1258 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1259 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1261 case tcc_declaration
:
1262 t1
= DECL_CONTEXT (t1
); break;
1264 t1
= TYPE_CONTEXT (t1
); break;
1265 case tcc_exceptional
:
1266 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1267 default: gcc_unreachable ();
1270 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1271 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1273 case tcc_declaration
:
1274 t2
= DECL_CONTEXT (t2
); break;
1276 t2
= TYPE_CONTEXT (t2
); break;
1277 case tcc_exceptional
:
1278 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1279 default: gcc_unreachable ();
1285 /* Allocate the seen two types, assuming that they are compatible. */
1287 static struct tagged_tu_seen_cache
*
1288 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1290 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1291 tu
->next
= tagged_tu_seen_base
;
1295 tagged_tu_seen_base
= tu
;
1297 /* The C standard says that two structures in different translation
1298 units are compatible with each other only if the types of their
1299 fields are compatible (among other things). We assume that they
1300 are compatible until proven otherwise when building the cache.
1301 An example where this can occur is:
1306 If we are comparing this against a similar struct in another TU,
1307 and did not assume they were compatible, we end up with an infinite
1313 /* Free the seen types until we get to TU_TIL. */
1316 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1318 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1319 while (tu
!= tu_til
)
1321 const struct tagged_tu_seen_cache
*const tu1
1322 = (const struct tagged_tu_seen_cache
*) tu
;
1324 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1326 tagged_tu_seen_base
= tu_til
;
1329 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1330 compatible. If the two types are not the same (which has been
1331 checked earlier), this can only happen when multiple translation
1332 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1333 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1334 comptypes_internal. */
1337 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1338 bool *enum_and_int_p
, bool *different_types_p
)
1341 bool needs_warning
= false;
1343 /* We have to verify that the tags of the types are the same. This
1344 is harder than it looks because this may be a typedef, so we have
1345 to go look at the original type. It may even be a typedef of a
1347 In the case of compiler-created builtin structs the TYPE_DECL
1348 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1349 while (TYPE_NAME (t1
)
1350 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1351 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1352 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1354 while (TYPE_NAME (t2
)
1355 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1356 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1357 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1359 /* C90 didn't have the requirement that the two tags be the same. */
1360 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1363 /* C90 didn't say what happened if one or both of the types were
1364 incomplete; we choose to follow C99 rules here, which is that they
1366 if (TYPE_SIZE (t1
) == NULL
1367 || TYPE_SIZE (t2
) == NULL
)
1371 const struct tagged_tu_seen_cache
* tts_i
;
1372 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1373 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1377 switch (TREE_CODE (t1
))
1381 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1382 /* Speed up the case where the type values are in the same order. */
1383 tree tv1
= TYPE_VALUES (t1
);
1384 tree tv2
= TYPE_VALUES (t2
);
1391 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1393 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1395 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1402 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1406 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1412 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1418 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1420 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1422 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1433 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1434 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1440 /* Speed up the common case where the fields are in the same order. */
1441 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1442 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1446 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1448 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1449 enum_and_int_p
, different_types_p
);
1451 if (result
!= 1 && !DECL_NAME (s1
))
1459 needs_warning
= true;
1461 if (TREE_CODE (s1
) == FIELD_DECL
1462 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1463 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1471 tu
->val
= needs_warning
? 2 : 1;
1475 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1479 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1480 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1484 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1488 if (result
!= 1 && !DECL_NAME (s1
))
1496 needs_warning
= true;
1498 if (TREE_CODE (s1
) == FIELD_DECL
1499 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1500 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1512 tu
->val
= needs_warning
? 2 : 10;
1518 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1520 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1522 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1525 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1526 || DECL_NAME (s1
) != DECL_NAME (s2
))
1528 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1529 enum_and_int_p
, different_types_p
);
1533 needs_warning
= true;
1535 if (TREE_CODE (s1
) == FIELD_DECL
1536 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1537 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1543 tu
->val
= needs_warning
? 2 : 1;
1552 /* Return 1 if two function types F1 and F2 are compatible.
1553 If either type specifies no argument types,
1554 the other must specify a fixed number of self-promoting arg types.
1555 Otherwise, if one type specifies only the number of arguments,
1556 the other must specify that number of self-promoting arg types.
1557 Otherwise, the argument types must match.
1558 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1561 function_types_compatible_p (const_tree f1
, const_tree f2
,
1562 bool *enum_and_int_p
, bool *different_types_p
)
1565 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1570 ret1
= TREE_TYPE (f1
);
1571 ret2
= TREE_TYPE (f2
);
1573 /* 'volatile' qualifiers on a function's return type used to mean
1574 the function is noreturn. */
1575 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1576 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1577 if (TYPE_VOLATILE (ret1
))
1578 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1579 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1580 if (TYPE_VOLATILE (ret2
))
1581 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1582 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1583 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1587 args1
= TYPE_ARG_TYPES (f1
);
1588 args2
= TYPE_ARG_TYPES (f2
);
1590 if (different_types_p
!= NULL
1591 && (args1
== 0) != (args2
== 0))
1592 *different_types_p
= true;
1594 /* An unspecified parmlist matches any specified parmlist
1595 whose argument types don't need default promotions. */
1599 if (!self_promoting_args_p (args2
))
1601 /* If one of these types comes from a non-prototype fn definition,
1602 compare that with the other type's arglist.
1603 If they don't match, ask for a warning (but no error). */
1604 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1605 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1606 enum_and_int_p
, different_types_p
))
1612 if (!self_promoting_args_p (args1
))
1614 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1615 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1616 enum_and_int_p
, different_types_p
))
1621 /* Both types have argument lists: compare them and propagate results. */
1622 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1624 return val1
!= 1 ? val1
: val
;
1627 /* Check two lists of types for compatibility, returning 0 for
1628 incompatible, 1 for compatible, or 2 for compatible with
1629 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1630 comptypes_internal. */
1633 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1634 bool *enum_and_int_p
, bool *different_types_p
)
1636 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1642 tree a1
, mv1
, a2
, mv2
;
1643 if (args1
== 0 && args2
== 0)
1645 /* If one list is shorter than the other,
1646 they fail to match. */
1647 if (args1
== 0 || args2
== 0)
1649 mv1
= a1
= TREE_VALUE (args1
);
1650 mv2
= a2
= TREE_VALUE (args2
);
1651 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1652 mv1
= (TYPE_ATOMIC (mv1
)
1653 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1655 : TYPE_MAIN_VARIANT (mv1
));
1656 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1657 mv2
= (TYPE_ATOMIC (mv2
)
1658 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1660 : TYPE_MAIN_VARIANT (mv2
));
1661 /* A null pointer instead of a type
1662 means there is supposed to be an argument
1663 but nothing is specified about what type it has.
1664 So match anything that self-promotes. */
1665 if (different_types_p
!= NULL
1666 && (a1
== 0) != (a2
== 0))
1667 *different_types_p
= true;
1670 if (c_type_promotes_to (a2
) != a2
)
1675 if (c_type_promotes_to (a1
) != a1
)
1678 /* If one of the lists has an error marker, ignore this arg. */
1679 else if (TREE_CODE (a1
) == ERROR_MARK
1680 || TREE_CODE (a2
) == ERROR_MARK
)
1682 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1683 different_types_p
)))
1685 if (different_types_p
!= NULL
)
1686 *different_types_p
= true;
1687 /* Allow wait (union {union wait *u; int *i} *)
1688 and wait (union wait *) to be compatible. */
1689 if (TREE_CODE (a1
) == UNION_TYPE
1690 && (TYPE_NAME (a1
) == 0
1691 || TYPE_TRANSPARENT_AGGR (a1
))
1692 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1693 && tree_int_cst_equal (TYPE_SIZE (a1
),
1697 for (memb
= TYPE_FIELDS (a1
);
1698 memb
; memb
= DECL_CHAIN (memb
))
1700 tree mv3
= TREE_TYPE (memb
);
1701 if (mv3
&& mv3
!= error_mark_node
1702 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1703 mv3
= (TYPE_ATOMIC (mv3
)
1704 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1706 : TYPE_MAIN_VARIANT (mv3
));
1707 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1714 else if (TREE_CODE (a2
) == UNION_TYPE
1715 && (TYPE_NAME (a2
) == 0
1716 || TYPE_TRANSPARENT_AGGR (a2
))
1717 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1718 && tree_int_cst_equal (TYPE_SIZE (a2
),
1722 for (memb
= TYPE_FIELDS (a2
);
1723 memb
; memb
= DECL_CHAIN (memb
))
1725 tree mv3
= TREE_TYPE (memb
);
1726 if (mv3
&& mv3
!= error_mark_node
1727 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1728 mv3
= (TYPE_ATOMIC (mv3
)
1729 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1731 : TYPE_MAIN_VARIANT (mv3
));
1732 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1743 /* comptypes said ok, but record if it said to warn. */
1747 args1
= TREE_CHAIN (args1
);
1748 args2
= TREE_CHAIN (args2
);
1752 /* Compute the size to increment a pointer by. */
1755 c_size_in_bytes (const_tree type
)
1757 enum tree_code code
= TREE_CODE (type
);
1759 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
)
1760 return size_one_node
;
1762 if (!COMPLETE_OR_VOID_TYPE_P (type
))
1764 error ("arithmetic on pointer to an incomplete type");
1765 return size_one_node
;
1768 /* Convert in case a char is more than one unit. */
1769 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1770 size_int (TYPE_PRECISION (char_type_node
)
1774 /* Return either DECL or its known constant value (if it has one). */
1777 decl_constant_value (tree decl
)
1779 if (/* Don't change a variable array bound or initial value to a constant
1780 in a place where a variable is invalid. Note that DECL_INITIAL
1781 isn't valid for a PARM_DECL. */
1782 current_function_decl
!= 0
1783 && TREE_CODE (decl
) != PARM_DECL
1784 && !TREE_THIS_VOLATILE (decl
)
1785 && TREE_READONLY (decl
)
1786 && DECL_INITIAL (decl
) != 0
1787 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1788 /* This is invalid if initial value is not constant.
1789 If it has either a function call, a memory reference,
1790 or a variable, then re-evaluating it could give different results. */
1791 && TREE_CONSTANT (DECL_INITIAL (decl
))
1792 /* Check for cases where this is sub-optimal, even though valid. */
1793 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1794 return DECL_INITIAL (decl
);
1798 /* Convert the array expression EXP to a pointer. */
1800 array_to_pointer_conversion (location_t loc
, tree exp
)
1802 tree orig_exp
= exp
;
1803 tree type
= TREE_TYPE (exp
);
1805 tree restype
= TREE_TYPE (type
);
1808 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1810 STRIP_TYPE_NOPS (exp
);
1812 if (TREE_NO_WARNING (orig_exp
))
1813 TREE_NO_WARNING (exp
) = 1;
1815 ptrtype
= build_pointer_type (restype
);
1817 if (TREE_CODE (exp
) == INDIRECT_REF
)
1818 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1820 /* In C++ array compound literals are temporary objects unless they are
1821 const or appear in namespace scope, so they are destroyed too soon
1822 to use them for much of anything (c++/53220). */
1823 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1825 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1826 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1827 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1828 "converting an array compound literal to a pointer "
1829 "is ill-formed in C++");
1832 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1833 return convert (ptrtype
, adr
);
1836 /* Convert the function expression EXP to a pointer. */
1838 function_to_pointer_conversion (location_t loc
, tree exp
)
1840 tree orig_exp
= exp
;
1842 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1844 STRIP_TYPE_NOPS (exp
);
1846 if (TREE_NO_WARNING (orig_exp
))
1847 TREE_NO_WARNING (exp
) = 1;
1849 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1852 /* Mark EXP as read, not just set, for set but not used -Wunused
1853 warning purposes. */
1856 mark_exp_read (tree exp
)
1858 switch (TREE_CODE (exp
))
1862 DECL_READ_P (exp
) = 1;
1871 mark_exp_read (TREE_OPERAND (exp
, 0));
1874 case C_MAYBE_CONST_EXPR
:
1875 mark_exp_read (TREE_OPERAND (exp
, 1));
1882 /* Perform the default conversion of arrays and functions to pointers.
1883 Return the result of converting EXP. For any other expression, just
1886 LOC is the location of the expression. */
1889 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1891 tree orig_exp
= exp
.value
;
1892 tree type
= TREE_TYPE (exp
.value
);
1893 enum tree_code code
= TREE_CODE (type
);
1899 bool not_lvalue
= false;
1900 bool lvalue_array_p
;
1902 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1903 || CONVERT_EXPR_P (exp
.value
))
1904 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1906 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1908 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1911 if (TREE_NO_WARNING (orig_exp
))
1912 TREE_NO_WARNING (exp
.value
) = 1;
1914 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1915 if (!flag_isoc99
&& !lvalue_array_p
)
1917 /* Before C99, non-lvalue arrays do not decay to pointers.
1918 Normally, using such an array would be invalid; but it can
1919 be used correctly inside sizeof or as a statement expression.
1920 Thus, do not give an error here; an error will result later. */
1924 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1928 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1938 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1940 mark_exp_read (exp
.value
);
1941 return default_function_array_conversion (loc
, exp
);
1944 /* Return whether EXPR should be treated as an atomic lvalue for the
1945 purposes of load and store handling. */
1948 really_atomic_lvalue (tree expr
)
1950 if (expr
== error_mark_node
|| TREE_TYPE (expr
) == error_mark_node
)
1952 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
1954 if (!lvalue_p (expr
))
1957 /* Ignore _Atomic on register variables, since their addresses can't
1958 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1959 sequences wouldn't work. Ignore _Atomic on structures containing
1960 bit-fields, since accessing elements of atomic structures or
1961 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1962 it's undefined at translation time or execution time, and the
1963 normal atomic sequences again wouldn't work. */
1964 while (handled_component_p (expr
))
1966 if (TREE_CODE (expr
) == COMPONENT_REF
1967 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1969 expr
= TREE_OPERAND (expr
, 0);
1971 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
1976 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1977 including converting functions and arrays to pointers if CONVERT_P.
1978 If READ_P, also mark the expression as having been read. */
1981 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
1982 bool convert_p
, bool read_p
)
1985 mark_exp_read (exp
.value
);
1987 exp
= default_function_array_conversion (loc
, exp
);
1988 if (really_atomic_lvalue (exp
.value
))
1990 vec
<tree
, va_gc
> *params
;
1991 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
1992 tree expr_type
= TREE_TYPE (exp
.value
);
1993 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, 0);
1994 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
1996 gcc_assert (TYPE_ATOMIC (expr_type
));
1998 /* Expansion of a generic atomic load may require an addition
1999 element, so allocate enough to prevent a resize. */
2000 vec_alloc (params
, 4);
2002 /* Remove the qualifiers for the rest of the expressions and
2003 create the VAL temp variable to hold the RHS. */
2004 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2005 tmp
= create_tmp_var (nonatomic_type
, NULL
);
2006 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, 0);
2007 TREE_ADDRESSABLE (tmp
) = 1;
2009 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2010 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2011 params
->quick_push (expr_addr
);
2012 params
->quick_push (tmp_addr
);
2013 params
->quick_push (seq_cst
);
2014 func_call
= build_function_call_vec (loc
, fndecl
, params
, NULL
);
2016 /* Return tmp which contains the value loaded. */
2017 exp
.value
= build2 (COMPOUND_EXPR
, nonatomic_type
, func_call
, tmp
);
2022 /* EXP is an expression of integer type. Apply the integer promotions
2023 to it and return the promoted value. */
2026 perform_integral_promotions (tree exp
)
2028 tree type
= TREE_TYPE (exp
);
2029 enum tree_code code
= TREE_CODE (type
);
2031 gcc_assert (INTEGRAL_TYPE_P (type
));
2033 /* Normally convert enums to int,
2034 but convert wide enums to something wider. */
2035 if (code
== ENUMERAL_TYPE
)
2037 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2038 TYPE_PRECISION (integer_type_node
)),
2039 ((TYPE_PRECISION (type
)
2040 >= TYPE_PRECISION (integer_type_node
))
2041 && TYPE_UNSIGNED (type
)));
2043 return convert (type
, exp
);
2046 /* ??? This should no longer be needed now bit-fields have their
2048 if (TREE_CODE (exp
) == COMPONENT_REF
2049 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2050 /* If it's thinner than an int, promote it like a
2051 c_promoting_integer_type_p, otherwise leave it alone. */
2052 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2053 TYPE_PRECISION (integer_type_node
)))
2054 return convert (integer_type_node
, exp
);
2056 if (c_promoting_integer_type_p (type
))
2058 /* Preserve unsignedness if not really getting any wider. */
2059 if (TYPE_UNSIGNED (type
)
2060 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2061 return convert (unsigned_type_node
, exp
);
2063 return convert (integer_type_node
, exp
);
2070 /* Perform default promotions for C data used in expressions.
2071 Enumeral types or short or char are converted to int.
2072 In addition, manifest constants symbols are replaced by their values. */
2075 default_conversion (tree exp
)
2078 tree type
= TREE_TYPE (exp
);
2079 enum tree_code code
= TREE_CODE (type
);
2082 mark_exp_read (exp
);
2084 /* Functions and arrays have been converted during parsing. */
2085 gcc_assert (code
!= FUNCTION_TYPE
);
2086 if (code
== ARRAY_TYPE
)
2089 /* Constants can be used directly unless they're not loadable. */
2090 if (TREE_CODE (exp
) == CONST_DECL
)
2091 exp
= DECL_INITIAL (exp
);
2093 /* Strip no-op conversions. */
2095 STRIP_TYPE_NOPS (exp
);
2097 if (TREE_NO_WARNING (orig_exp
))
2098 TREE_NO_WARNING (exp
) = 1;
2100 if (code
== VOID_TYPE
)
2102 error ("void value not ignored as it ought to be");
2103 return error_mark_node
;
2106 exp
= require_complete_type (exp
);
2107 if (exp
== error_mark_node
)
2108 return error_mark_node
;
2110 promoted_type
= targetm
.promoted_type (type
);
2112 return convert (promoted_type
, exp
);
2114 if (INTEGRAL_TYPE_P (type
))
2115 return perform_integral_promotions (exp
);
2120 /* Look up COMPONENT in a structure or union TYPE.
2122 If the component name is not found, returns NULL_TREE. Otherwise,
2123 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2124 stepping down the chain to the component, which is in the last
2125 TREE_VALUE of the list. Normally the list is of length one, but if
2126 the component is embedded within (nested) anonymous structures or
2127 unions, the list steps down the chain to the component. */
2130 lookup_field (tree type
, tree component
)
2134 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2135 to the field elements. Use a binary search on this array to quickly
2136 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2137 will always be set for structures which have many elements. */
2139 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2142 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2144 field
= TYPE_FIELDS (type
);
2146 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2147 while (top
- bot
> 1)
2149 half
= (top
- bot
+ 1) >> 1;
2150 field
= field_array
[bot
+half
];
2152 if (DECL_NAME (field
) == NULL_TREE
)
2154 /* Step through all anon unions in linear fashion. */
2155 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2157 field
= field_array
[bot
++];
2158 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2159 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
2161 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2164 return tree_cons (NULL_TREE
, field
, anon
);
2166 /* The Plan 9 compiler permits referring
2167 directly to an anonymous struct/union field
2168 using a typedef name. */
2169 if (flag_plan9_extensions
2170 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2171 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2173 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2179 /* Entire record is only anon unions. */
2183 /* Restart the binary search, with new lower bound. */
2187 if (DECL_NAME (field
) == component
)
2189 if (DECL_NAME (field
) < component
)
2195 if (DECL_NAME (field_array
[bot
]) == component
)
2196 field
= field_array
[bot
];
2197 else if (DECL_NAME (field
) != component
)
2202 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2204 if (DECL_NAME (field
) == NULL_TREE
2205 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2206 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
2208 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2211 return tree_cons (NULL_TREE
, field
, anon
);
2213 /* The Plan 9 compiler permits referring directly to an
2214 anonymous struct/union field using a typedef
2216 if (flag_plan9_extensions
2217 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2218 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2219 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2224 if (DECL_NAME (field
) == component
)
2228 if (field
== NULL_TREE
)
2232 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2235 /* Make an expression to refer to the COMPONENT field of structure or
2236 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2237 location of the COMPONENT_REF. */
2240 build_component_ref (location_t loc
, tree datum
, tree component
)
2242 tree type
= TREE_TYPE (datum
);
2243 enum tree_code code
= TREE_CODE (type
);
2246 bool datum_lvalue
= lvalue_p (datum
);
2248 if (!objc_is_public (datum
, component
))
2249 return error_mark_node
;
2251 /* Detect Objective-C property syntax object.property. */
2252 if (c_dialect_objc ()
2253 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2256 /* See if there is a field or component with name COMPONENT. */
2258 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2260 if (!COMPLETE_TYPE_P (type
))
2262 c_incomplete_type_error (NULL_TREE
, type
);
2263 return error_mark_node
;
2266 field
= lookup_field (type
, component
);
2270 error_at (loc
, "%qT has no member named %qE", type
, component
);
2271 return error_mark_node
;
2274 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2275 This might be better solved in future the way the C++ front
2276 end does it - by giving the anonymous entities each a
2277 separate name and type, and then have build_component_ref
2278 recursively call itself. We can't do that here. */
2281 tree subdatum
= TREE_VALUE (field
);
2284 bool use_datum_quals
;
2286 if (TREE_TYPE (subdatum
) == error_mark_node
)
2287 return error_mark_node
;
2289 /* If this is an rvalue, it does not have qualifiers in C
2290 standard terms and we must avoid propagating such
2291 qualifiers down to a non-lvalue array that is then
2292 converted to a pointer. */
2293 use_datum_quals
= (datum_lvalue
2294 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2296 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2297 if (use_datum_quals
)
2298 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2299 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2301 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2303 SET_EXPR_LOCATION (ref
, loc
);
2304 if (TREE_READONLY (subdatum
)
2305 || (use_datum_quals
&& TREE_READONLY (datum
)))
2306 TREE_READONLY (ref
) = 1;
2307 if (TREE_THIS_VOLATILE (subdatum
)
2308 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2309 TREE_THIS_VOLATILE (ref
) = 1;
2311 if (TREE_DEPRECATED (subdatum
))
2312 warn_deprecated_use (subdatum
, NULL_TREE
);
2316 field
= TREE_CHAIN (field
);
2322 else if (code
!= ERROR_MARK
)
2324 "request for member %qE in something not a structure or union",
2327 return error_mark_node
;
2330 /* Given an expression PTR for a pointer, return an expression
2331 for the value pointed to.
2332 ERRORSTRING is the name of the operator to appear in error messages.
2334 LOC is the location to use for the generated tree. */
2337 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2339 tree pointer
= default_conversion (ptr
);
2340 tree type
= TREE_TYPE (pointer
);
2343 if (TREE_CODE (type
) == POINTER_TYPE
)
2345 if (CONVERT_EXPR_P (pointer
)
2346 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2348 /* If a warning is issued, mark it to avoid duplicates from
2349 the backend. This only needs to be done at
2350 warn_strict_aliasing > 2. */
2351 if (warn_strict_aliasing
> 2)
2352 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2353 type
, TREE_OPERAND (pointer
, 0)))
2354 TREE_NO_WARNING (pointer
) = 1;
2357 if (TREE_CODE (pointer
) == ADDR_EXPR
2358 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2359 == TREE_TYPE (type
)))
2361 ref
= TREE_OPERAND (pointer
, 0);
2362 protected_set_expr_location (ref
, loc
);
2367 tree t
= TREE_TYPE (type
);
2369 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2371 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2373 error_at (loc
, "dereferencing pointer to incomplete type");
2374 return error_mark_node
;
2376 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2377 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2379 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2380 so that we get the proper error message if the result is used
2381 to assign to. Also, &* is supposed to be a no-op.
2382 And ANSI C seems to specify that the type of the result
2383 should be the const type. */
2384 /* A de-reference of a pointer to const is not a const. It is valid
2385 to change it via some other pointer. */
2386 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2387 TREE_SIDE_EFFECTS (ref
)
2388 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2389 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2390 protected_set_expr_location (ref
, loc
);
2394 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2395 invalid_indirection_error (loc
, type
, errstring
);
2397 return error_mark_node
;
2400 /* This handles expressions of the form "a[i]", which denotes
2403 This is logically equivalent in C to *(a+i), but we may do it differently.
2404 If A is a variable or a member, we generate a primitive ARRAY_REF.
2405 This avoids forcing the array out of registers, and can work on
2406 arrays that are not lvalues (for example, members of structures returned
2409 For vector types, allow vector[i] but not i[vector], and create
2410 *(((type*)&vectortype) + i) for the expression.
2412 LOC is the location to use for the returned expression. */
2415 build_array_ref (location_t loc
, tree array
, tree index
)
2418 bool swapped
= false;
2419 if (TREE_TYPE (array
) == error_mark_node
2420 || TREE_TYPE (index
) == error_mark_node
)
2421 return error_mark_node
;
2423 if (flag_enable_cilkplus
&& contains_array_notation_expr (index
))
2426 if (!find_rank (loc
, index
, index
, true, &rank
))
2427 return error_mark_node
;
2430 error_at (loc
, "rank of the array's index is greater than 1");
2431 return error_mark_node
;
2434 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2435 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2436 /* Allow vector[index] but not index[vector]. */
2437 && TREE_CODE (TREE_TYPE (array
)) != VECTOR_TYPE
)
2440 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2441 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2444 "subscripted value is neither array nor pointer nor vector");
2446 return error_mark_node
;
2454 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2456 error_at (loc
, "array subscript is not an integer");
2457 return error_mark_node
;
2460 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2462 error_at (loc
, "subscripted value is pointer to function");
2463 return error_mark_node
;
2466 /* ??? Existing practice has been to warn only when the char
2467 index is syntactically the index, not for char[array]. */
2469 warn_array_subscript_with_type_char (index
);
2471 /* Apply default promotions *after* noticing character types. */
2472 index
= default_conversion (index
);
2474 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2476 convert_vector_to_pointer_for_subscript (loc
, &array
, index
);
2478 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2482 /* An array that is indexed by a non-constant
2483 cannot be stored in a register; we must be able to do
2484 address arithmetic on its address.
2485 Likewise an array of elements of variable size. */
2486 if (TREE_CODE (index
) != INTEGER_CST
2487 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2488 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2490 if (!c_mark_addressable (array
))
2491 return error_mark_node
;
2493 /* An array that is indexed by a constant value which is not within
2494 the array bounds cannot be stored in a register either; because we
2495 would get a crash in store_bit_field/extract_bit_field when trying
2496 to access a non-existent part of the register. */
2497 if (TREE_CODE (index
) == INTEGER_CST
2498 && TYPE_DOMAIN (TREE_TYPE (array
))
2499 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2501 if (!c_mark_addressable (array
))
2502 return error_mark_node
;
2508 while (TREE_CODE (foo
) == COMPONENT_REF
)
2509 foo
= TREE_OPERAND (foo
, 0);
2510 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2511 pedwarn (loc
, OPT_Wpedantic
,
2512 "ISO C forbids subscripting %<register%> array");
2513 else if (!flag_isoc99
&& !lvalue_p (foo
))
2514 pedwarn (loc
, OPT_Wpedantic
,
2515 "ISO C90 forbids subscripting non-lvalue array");
2518 type
= TREE_TYPE (TREE_TYPE (array
));
2519 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2520 /* Array ref is const/volatile if the array elements are
2521 or if the array is. */
2522 TREE_READONLY (rval
)
2523 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2524 | TREE_READONLY (array
));
2525 TREE_SIDE_EFFECTS (rval
)
2526 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2527 | TREE_SIDE_EFFECTS (array
));
2528 TREE_THIS_VOLATILE (rval
)
2529 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2530 /* This was added by rms on 16 Nov 91.
2531 It fixes vol struct foo *a; a->elts[1]
2532 in an inline function.
2533 Hope it doesn't break something else. */
2534 | TREE_THIS_VOLATILE (array
));
2535 ret
= require_complete_type (rval
);
2536 protected_set_expr_location (ret
, loc
);
2541 tree ar
= default_conversion (array
);
2543 if (ar
== error_mark_node
)
2546 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2547 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2549 return build_indirect_ref
2550 (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
, index
, 0),
2555 /* Build an external reference to identifier ID. FUN indicates
2556 whether this will be used for a function call. LOC is the source
2557 location of the identifier. This sets *TYPE to the type of the
2558 identifier, which is not the same as the type of the returned value
2559 for CONST_DECLs defined as enum constants. If the type of the
2560 identifier is not available, *TYPE is set to NULL. */
2562 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2565 tree decl
= lookup_name (id
);
2567 /* In Objective-C, an instance variable (ivar) may be preferred to
2568 whatever lookup_name() found. */
2569 decl
= objc_lookup_ivar (decl
, id
);
2572 if (decl
&& decl
!= error_mark_node
)
2575 *type
= TREE_TYPE (ref
);
2578 /* Implicit function declaration. */
2579 ref
= implicitly_declare (loc
, id
);
2580 else if (decl
== error_mark_node
)
2581 /* Don't complain about something that's already been
2582 complained about. */
2583 return error_mark_node
;
2586 undeclared_variable (loc
, id
);
2587 return error_mark_node
;
2590 if (TREE_TYPE (ref
) == error_mark_node
)
2591 return error_mark_node
;
2593 if (TREE_DEPRECATED (ref
))
2594 warn_deprecated_use (ref
, NULL_TREE
);
2596 /* Recursive call does not count as usage. */
2597 if (ref
!= current_function_decl
)
2599 TREE_USED (ref
) = 1;
2602 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2604 if (!in_sizeof
&& !in_typeof
)
2605 C_DECL_USED (ref
) = 1;
2606 else if (DECL_INITIAL (ref
) == 0
2607 && DECL_EXTERNAL (ref
)
2608 && !TREE_PUBLIC (ref
))
2609 record_maybe_used_decl (ref
);
2612 if (TREE_CODE (ref
) == CONST_DECL
)
2614 used_types_insert (TREE_TYPE (ref
));
2617 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2618 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2620 warning_at (loc
, OPT_Wc___compat
,
2621 ("enum constant defined in struct or union "
2622 "is not visible in C++"));
2623 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2626 ref
= DECL_INITIAL (ref
);
2627 TREE_CONSTANT (ref
) = 1;
2629 else if (current_function_decl
!= 0
2630 && !DECL_FILE_SCOPE_P (current_function_decl
)
2631 && (TREE_CODE (ref
) == VAR_DECL
2632 || TREE_CODE (ref
) == PARM_DECL
2633 || TREE_CODE (ref
) == FUNCTION_DECL
))
2635 tree context
= decl_function_context (ref
);
2637 if (context
!= 0 && context
!= current_function_decl
)
2638 DECL_NONLOCAL (ref
) = 1;
2640 /* C99 6.7.4p3: An inline definition of a function with external
2641 linkage ... shall not contain a reference to an identifier with
2642 internal linkage. */
2643 else if (current_function_decl
!= 0
2644 && DECL_DECLARED_INLINE_P (current_function_decl
)
2645 && DECL_EXTERNAL (current_function_decl
)
2646 && VAR_OR_FUNCTION_DECL_P (ref
)
2647 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2648 && ! TREE_PUBLIC (ref
)
2649 && DECL_CONTEXT (ref
) != current_function_decl
)
2650 record_inline_static (loc
, current_function_decl
, ref
,
2656 /* Record details of decls possibly used inside sizeof or typeof. */
2657 struct maybe_used_decl
2661 /* The level seen at (in_sizeof + in_typeof). */
2663 /* The next one at this level or above, or NULL. */
2664 struct maybe_used_decl
*next
;
2667 static struct maybe_used_decl
*maybe_used_decls
;
2669 /* Record that DECL, an undefined static function reference seen
2670 inside sizeof or typeof, might be used if the operand of sizeof is
2671 a VLA type or the operand of typeof is a variably modified
2675 record_maybe_used_decl (tree decl
)
2677 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2679 t
->level
= in_sizeof
+ in_typeof
;
2680 t
->next
= maybe_used_decls
;
2681 maybe_used_decls
= t
;
2684 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2685 USED is false, just discard them. If it is true, mark them used
2686 (if no longer inside sizeof or typeof) or move them to the next
2687 level up (if still inside sizeof or typeof). */
2690 pop_maybe_used (bool used
)
2692 struct maybe_used_decl
*p
= maybe_used_decls
;
2693 int cur_level
= in_sizeof
+ in_typeof
;
2694 while (p
&& p
->level
> cur_level
)
2699 C_DECL_USED (p
->decl
) = 1;
2701 p
->level
= cur_level
;
2705 if (!used
|| cur_level
== 0)
2706 maybe_used_decls
= p
;
2709 /* Return the result of sizeof applied to EXPR. */
2712 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2715 if (expr
.value
== error_mark_node
)
2717 ret
.value
= error_mark_node
;
2718 ret
.original_code
= ERROR_MARK
;
2719 ret
.original_type
= NULL
;
2720 pop_maybe_used (false);
2724 bool expr_const_operands
= true;
2725 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2726 &expr_const_operands
);
2727 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2728 c_last_sizeof_arg
= expr
.value
;
2729 ret
.original_code
= SIZEOF_EXPR
;
2730 ret
.original_type
= NULL
;
2731 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2733 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2734 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2735 folded_expr
, ret
.value
);
2736 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2737 SET_EXPR_LOCATION (ret
.value
, loc
);
2739 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2744 /* Return the result of sizeof applied to T, a structure for the type
2745 name passed to sizeof (rather than the type itself). LOC is the
2746 location of the original expression. */
2749 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2753 tree type_expr
= NULL_TREE
;
2754 bool type_expr_const
= true;
2755 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2756 ret
.value
= c_sizeof (loc
, type
);
2757 c_last_sizeof_arg
= type
;
2758 ret
.original_code
= SIZEOF_EXPR
;
2759 ret
.original_type
= NULL
;
2760 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2761 && c_vla_type_p (type
))
2763 /* If the type is a [*] array, it is a VLA but is represented as
2764 having a size of zero. In such a case we must ensure that
2765 the result of sizeof does not get folded to a constant by
2766 c_fully_fold, because if the size is evaluated the result is
2767 not constant and so constraints on zero or negative size
2768 arrays must not be applied when this sizeof call is inside
2769 another array declarator. */
2771 type_expr
= integer_zero_node
;
2772 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2773 type_expr
, ret
.value
);
2774 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2776 pop_maybe_used (type
!= error_mark_node
2777 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2781 /* Build a function call to function FUNCTION with parameters PARAMS.
2782 The function call is at LOC.
2783 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2784 TREE_VALUE of each node is a parameter-expression.
2785 FUNCTION's data type may be a function type or a pointer-to-function. */
2788 build_function_call (location_t loc
, tree function
, tree params
)
2790 vec
<tree
, va_gc
> *v
;
2793 vec_alloc (v
, list_length (params
));
2794 for (; params
; params
= TREE_CHAIN (params
))
2795 v
->quick_push (TREE_VALUE (params
));
2796 ret
= build_function_call_vec (loc
, function
, v
, NULL
);
2801 /* Give a note about the location of the declaration of DECL. */
2803 static void inform_declaration (tree decl
)
2805 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_BUILT_IN (decl
)))
2806 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2809 /* Build a function call to function FUNCTION with parameters PARAMS.
2810 ORIGTYPES, if not NULL, is a vector of types; each element is
2811 either NULL or the original type of the corresponding element in
2812 PARAMS. The original type may differ from TREE_TYPE of the
2813 parameter for enums. FUNCTION's data type may be a function type
2814 or pointer-to-function. This function changes the elements of
2818 build_function_call_vec (location_t loc
, tree function
,
2819 vec
<tree
, va_gc
> *params
,
2820 vec
<tree
, va_gc
> *origtypes
)
2822 tree fntype
, fundecl
= 0;
2823 tree name
= NULL_TREE
, result
;
2829 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2830 STRIP_TYPE_NOPS (function
);
2832 /* Convert anything with function type to a pointer-to-function. */
2833 if (TREE_CODE (function
) == FUNCTION_DECL
)
2835 /* Implement type-directed function overloading for builtins.
2836 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2837 handle all the type checking. The result is a complete expression
2838 that implements this function call. */
2839 tem
= resolve_overloaded_builtin (loc
, function
, params
);
2843 name
= DECL_NAME (function
);
2846 tm_malloc_replacement (function
);
2848 /* Atomic functions have type checking/casting already done. They are
2849 often rewritten and don't match the original parameter list. */
2850 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
2853 if (flag_enable_cilkplus
2854 && is_cilkplus_reduce_builtin (function
))
2857 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2858 function
= function_to_pointer_conversion (loc
, function
);
2860 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2861 expressions, like those used for ObjC messenger dispatches. */
2862 if (params
&& !params
->is_empty ())
2863 function
= objc_rewrite_function_call (function
, (*params
)[0]);
2865 function
= c_fully_fold (function
, false, NULL
);
2867 fntype
= TREE_TYPE (function
);
2869 if (TREE_CODE (fntype
) == ERROR_MARK
)
2870 return error_mark_node
;
2872 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2873 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2875 if (!flag_diagnostics_show_caret
)
2877 "called object %qE is not a function or function pointer",
2879 else if (DECL_P (function
))
2882 "called object %qD is not a function or function pointer",
2884 inform_declaration (function
);
2888 "called object is not a function or function pointer");
2889 return error_mark_node
;
2892 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2893 current_function_returns_abnormally
= 1;
2895 /* fntype now gets the type of function pointed to. */
2896 fntype
= TREE_TYPE (fntype
);
2898 /* Convert the parameters to the types declared in the
2899 function prototype, or apply default promotions. */
2901 nargs
= convert_arguments (TYPE_ARG_TYPES (fntype
), params
, origtypes
,
2904 return error_mark_node
;
2906 /* Check that the function is called through a compatible prototype.
2907 If it is not, replace the call by a trap, wrapped up in a compound
2908 expression if necessary. This has the nice side-effect to prevent
2909 the tree-inliner from generating invalid assignment trees which may
2910 blow up in the RTL expander later. */
2911 if (CONVERT_EXPR_P (function
)
2912 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2913 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2914 && !comptypes (fntype
, TREE_TYPE (tem
)))
2916 tree return_type
= TREE_TYPE (fntype
);
2917 tree trap
= build_function_call (loc
,
2918 builtin_decl_explicit (BUILT_IN_TRAP
),
2922 /* This situation leads to run-time undefined behavior. We can't,
2923 therefore, simply error unless we can prove that all possible
2924 executions of the program must execute the code. */
2925 if (warning_at (loc
, 0, "function called through a non-compatible type"))
2926 /* We can, however, treat "undefined" any way we please.
2927 Call abort to encourage the user to fix the program. */
2928 inform (loc
, "if this code is reached, the program will abort");
2929 /* Before the abort, allow the function arguments to exit or
2931 for (i
= 0; i
< nargs
; i
++)
2932 trap
= build2 (COMPOUND_EXPR
, void_type_node
, (*params
)[i
], trap
);
2934 if (VOID_TYPE_P (return_type
))
2936 if (TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2938 "function with qualified void return type called");
2945 if (AGGREGATE_TYPE_P (return_type
))
2946 rhs
= build_compound_literal (loc
, return_type
,
2947 build_constructor (return_type
,
2951 rhs
= build_zero_cst (return_type
);
2953 return require_complete_type (build2 (COMPOUND_EXPR
, return_type
,
2958 argarray
= vec_safe_address (params
);
2960 /* Check that arguments to builtin functions match the expectations. */
2962 && DECL_BUILT_IN (fundecl
)
2963 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2964 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2965 return error_mark_node
;
2967 /* Check that the arguments to the function are valid. */
2968 check_function_arguments (fntype
, nargs
, argarray
);
2970 if (name
!= NULL_TREE
2971 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
2973 if (require_constant_value
)
2975 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
2976 function
, nargs
, argarray
);
2978 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
2979 function
, nargs
, argarray
);
2980 if (TREE_CODE (result
) == NOP_EXPR
2981 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
2982 STRIP_TYPE_NOPS (result
);
2985 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
2986 function
, nargs
, argarray
);
2988 if (VOID_TYPE_P (TREE_TYPE (result
)))
2990 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
2992 "function with qualified void return type called");
2995 return require_complete_type (result
);
2998 /* Convert the argument expressions in the vector VALUES
2999 to the types in the list TYPELIST.
3001 If TYPELIST is exhausted, or when an element has NULL as its type,
3002 perform the default conversions.
3004 ORIGTYPES is the original types of the expressions in VALUES. This
3005 holds the type of enum values which have been converted to integral
3006 types. It may be NULL.
3008 FUNCTION is a tree for the called function. It is used only for
3009 error messages, where it is formatted with %qE.
3011 This is also where warnings about wrong number of args are generated.
3013 Returns the actual number of arguments processed (which may be less
3014 than the length of VALUES in some error situations), or -1 on
3018 convert_arguments (tree typelist
, vec
<tree
, va_gc
> *values
,
3019 vec
<tree
, va_gc
> *origtypes
, tree function
, tree fundecl
)
3022 unsigned int parmnum
;
3023 bool error_args
= false;
3024 const bool type_generic
= fundecl
3025 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl
)));
3026 bool type_generic_remove_excess_precision
= false;
3029 /* Change pointer to function to the function itself for
3031 if (TREE_CODE (function
) == ADDR_EXPR
3032 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3033 function
= TREE_OPERAND (function
, 0);
3035 /* Handle an ObjC selector specially for diagnostics. */
3036 selector
= objc_message_selector ();
3038 /* For type-generic built-in functions, determine whether excess
3039 precision should be removed (classification) or not
3042 && DECL_BUILT_IN (fundecl
)
3043 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3045 switch (DECL_FUNCTION_CODE (fundecl
))
3047 case BUILT_IN_ISFINITE
:
3048 case BUILT_IN_ISINF
:
3049 case BUILT_IN_ISINF_SIGN
:
3050 case BUILT_IN_ISNAN
:
3051 case BUILT_IN_ISNORMAL
:
3052 case BUILT_IN_FPCLASSIFY
:
3053 type_generic_remove_excess_precision
= true;
3057 type_generic_remove_excess_precision
= false;
3061 if (flag_enable_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3062 return vec_safe_length (values
);
3064 /* Scan the given expressions and types, producing individual
3065 converted arguments. */
3067 for (typetail
= typelist
, parmnum
= 0;
3068 values
&& values
->iterate (parmnum
, &val
);
3071 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3072 tree valtype
= TREE_TYPE (val
);
3073 tree rname
= function
;
3074 int argnum
= parmnum
+ 1;
3075 const char *invalid_func_diag
;
3076 bool excess_precision
= false;
3080 if (type
== void_type_node
)
3083 error_at (input_location
,
3084 "too many arguments to method %qE", selector
);
3086 error_at (input_location
,
3087 "too many arguments to function %qE", function
);
3088 inform_declaration (fundecl
);
3092 if (selector
&& argnum
> 2)
3098 npc
= null_pointer_constant_p (val
);
3100 /* If there is excess precision and a prototype, convert once to
3101 the required type rather than converting via the semantic
3102 type. Likewise without a prototype a float value represented
3103 as long double should be converted once to double. But for
3104 type-generic classification functions excess precision must
3106 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3107 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3109 val
= TREE_OPERAND (val
, 0);
3110 excess_precision
= true;
3112 val
= c_fully_fold (val
, false, NULL
);
3113 STRIP_TYPE_NOPS (val
);
3115 val
= require_complete_type (val
);
3119 /* Formal parm type is specified by a function prototype. */
3121 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3123 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
3130 /* Optionally warn about conversions that
3131 differ from the default conversions. */
3132 if (warn_traditional_conversion
|| warn_traditional
)
3134 unsigned int formal_prec
= TYPE_PRECISION (type
);
3136 if (INTEGRAL_TYPE_P (type
)
3137 && TREE_CODE (valtype
) == REAL_TYPE
)
3138 warning (0, "passing argument %d of %qE as integer "
3139 "rather than floating due to prototype",
3141 if (INTEGRAL_TYPE_P (type
)
3142 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3143 warning (0, "passing argument %d of %qE as integer "
3144 "rather than complex due to prototype",
3146 else if (TREE_CODE (type
) == COMPLEX_TYPE
3147 && TREE_CODE (valtype
) == REAL_TYPE
)
3148 warning (0, "passing argument %d of %qE as complex "
3149 "rather than floating due to prototype",
3151 else if (TREE_CODE (type
) == REAL_TYPE
3152 && INTEGRAL_TYPE_P (valtype
))
3153 warning (0, "passing argument %d of %qE as floating "
3154 "rather than integer due to prototype",
3156 else if (TREE_CODE (type
) == COMPLEX_TYPE
3157 && INTEGRAL_TYPE_P (valtype
))
3158 warning (0, "passing argument %d of %qE as complex "
3159 "rather than integer due to prototype",
3161 else if (TREE_CODE (type
) == REAL_TYPE
3162 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3163 warning (0, "passing argument %d of %qE as floating "
3164 "rather than complex due to prototype",
3166 /* ??? At some point, messages should be written about
3167 conversions between complex types, but that's too messy
3169 else if (TREE_CODE (type
) == REAL_TYPE
3170 && TREE_CODE (valtype
) == REAL_TYPE
)
3172 /* Warn if any argument is passed as `float',
3173 since without a prototype it would be `double'. */
3174 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3175 && type
!= dfloat32_type_node
)
3176 warning (0, "passing argument %d of %qE as %<float%> "
3177 "rather than %<double%> due to prototype",
3180 /* Warn if mismatch between argument and prototype
3181 for decimal float types. Warn of conversions with
3182 binary float types and of precision narrowing due to
3184 else if (type
!= valtype
3185 && (type
== dfloat32_type_node
3186 || type
== dfloat64_type_node
3187 || type
== dfloat128_type_node
3188 || valtype
== dfloat32_type_node
3189 || valtype
== dfloat64_type_node
3190 || valtype
== dfloat128_type_node
)
3192 <= TYPE_PRECISION (valtype
)
3193 || (type
== dfloat128_type_node
3195 != dfloat64_type_node
3197 != dfloat32_type_node
)))
3198 || (type
== dfloat64_type_node
3200 != dfloat32_type_node
))))
3201 warning (0, "passing argument %d of %qE as %qT "
3202 "rather than %qT due to prototype",
3203 argnum
, rname
, type
, valtype
);
3206 /* Detect integer changing in width or signedness.
3207 These warnings are only activated with
3208 -Wtraditional-conversion, not with -Wtraditional. */
3209 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3210 && INTEGRAL_TYPE_P (valtype
))
3212 tree would_have_been
= default_conversion (val
);
3213 tree type1
= TREE_TYPE (would_have_been
);
3215 if (TREE_CODE (type
) == ENUMERAL_TYPE
3216 && (TYPE_MAIN_VARIANT (type
)
3217 == TYPE_MAIN_VARIANT (valtype
)))
3218 /* No warning if function asks for enum
3219 and the actual arg is that enum type. */
3221 else if (formal_prec
!= TYPE_PRECISION (type1
))
3222 warning (OPT_Wtraditional_conversion
,
3223 "passing argument %d of %qE "
3224 "with different width due to prototype",
3226 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3228 /* Don't complain if the formal parameter type
3229 is an enum, because we can't tell now whether
3230 the value was an enum--even the same enum. */
3231 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3233 else if (TREE_CODE (val
) == INTEGER_CST
3234 && int_fits_type_p (val
, type
))
3235 /* Change in signedness doesn't matter
3236 if a constant value is unaffected. */
3238 /* If the value is extended from a narrower
3239 unsigned type, it doesn't matter whether we
3240 pass it as signed or unsigned; the value
3241 certainly is the same either way. */
3242 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3243 && TYPE_UNSIGNED (valtype
))
3245 else if (TYPE_UNSIGNED (type
))
3246 warning (OPT_Wtraditional_conversion
,
3247 "passing argument %d of %qE "
3248 "as unsigned due to prototype",
3251 warning (OPT_Wtraditional_conversion
,
3252 "passing argument %d of %qE "
3253 "as signed due to prototype", argnum
, rname
);
3257 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3258 sake of better warnings from convert_and_check. */
3259 if (excess_precision
)
3260 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3261 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3262 parmval
= convert_for_assignment (input_location
, type
, val
,
3263 origtype
, ic_argpass
, npc
,
3267 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3268 && INTEGRAL_TYPE_P (type
)
3269 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3270 parmval
= default_conversion (parmval
);
3273 else if (TREE_CODE (valtype
) == REAL_TYPE
3274 && (TYPE_PRECISION (valtype
)
3275 <= TYPE_PRECISION (double_type_node
))
3276 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3277 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3278 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3284 /* Convert `float' to `double'. */
3285 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3286 warning (OPT_Wdouble_promotion
,
3287 "implicit conversion from %qT to %qT when passing "
3288 "argument to function",
3289 valtype
, double_type_node
);
3290 parmval
= convert (double_type_node
, val
);
3293 else if (excess_precision
&& !type_generic
)
3294 /* A "double" argument with excess precision being passed
3295 without a prototype or in variable arguments. */
3296 parmval
= convert (valtype
, val
);
3297 else if ((invalid_func_diag
=
3298 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3300 error (invalid_func_diag
);
3304 /* Convert `short' and `char' to full-size `int'. */
3305 parmval
= default_conversion (val
);
3307 (*values
)[parmnum
] = parmval
;
3308 if (parmval
== error_mark_node
)
3312 typetail
= TREE_CHAIN (typetail
);
3315 gcc_assert (parmnum
== vec_safe_length (values
));
3317 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3319 error_at (input_location
,
3320 "too few arguments to function %qE", function
);
3321 inform_declaration (fundecl
);
3325 return error_args
? -1 : (int) parmnum
;
3328 /* This is the entry point used by the parser to build unary operators
3329 in the input. CODE, a tree_code, specifies the unary operator, and
3330 ARG is the operand. For unary plus, the C parser currently uses
3331 CONVERT_EXPR for code.
3333 LOC is the location to use for the tree generated.
3337 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3339 struct c_expr result
;
3341 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
3342 result
.original_code
= code
;
3343 result
.original_type
= NULL
;
3345 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3346 overflow_warning (loc
, result
.value
);
3351 /* This is the entry point used by the parser to build binary operators
3352 in the input. CODE, a tree_code, specifies the binary operator, and
3353 ARG1 and ARG2 are the operands. In addition to constructing the
3354 expression, we check for operands that were written with other binary
3355 operators in a way that is likely to confuse the user.
3357 LOCATION is the location of the binary operator. */
3360 parser_build_binary_op (location_t location
, enum tree_code code
,
3361 struct c_expr arg1
, struct c_expr arg2
)
3363 struct c_expr result
;
3365 enum tree_code code1
= arg1
.original_code
;
3366 enum tree_code code2
= arg2
.original_code
;
3367 tree type1
= (arg1
.original_type
3368 ? arg1
.original_type
3369 : TREE_TYPE (arg1
.value
));
3370 tree type2
= (arg2
.original_type
3371 ? arg2
.original_type
3372 : TREE_TYPE (arg2
.value
));
3374 result
.value
= build_binary_op (location
, code
,
3375 arg1
.value
, arg2
.value
, 1);
3376 result
.original_code
= code
;
3377 result
.original_type
= NULL
;
3379 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3382 if (location
!= UNKNOWN_LOCATION
)
3383 protected_set_expr_location (result
.value
, location
);
3385 /* Check for cases such as x+y<<z which users are likely
3387 if (warn_parentheses
)
3388 warn_about_parentheses (input_location
, code
,
3389 code1
, arg1
.value
, code2
, arg2
.value
);
3391 if (warn_logical_op
)
3392 warn_logical_operator (input_location
, code
, TREE_TYPE (result
.value
),
3393 code1
, arg1
.value
, code2
, arg2
.value
);
3395 /* Warn about comparisons against string literals, with the exception
3396 of testing for equality or inequality of a string literal with NULL. */
3397 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3399 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3400 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3401 warning_at (location
, OPT_Waddress
,
3402 "comparison with string literal results in unspecified behavior");
3404 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3405 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3406 warning_at (location
, OPT_Waddress
,
3407 "comparison with string literal results in unspecified behavior");
3409 if (TREE_OVERFLOW_P (result
.value
)
3410 && !TREE_OVERFLOW_P (arg1
.value
)
3411 && !TREE_OVERFLOW_P (arg2
.value
))
3412 overflow_warning (location
, result
.value
);
3414 /* Warn about comparisons of different enum types. */
3415 if (warn_enum_compare
3416 && TREE_CODE_CLASS (code
) == tcc_comparison
3417 && TREE_CODE (type1
) == ENUMERAL_TYPE
3418 && TREE_CODE (type2
) == ENUMERAL_TYPE
3419 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3420 warning_at (location
, OPT_Wenum_compare
,
3421 "comparison between %qT and %qT",
3427 /* Return a tree for the difference of pointers OP0 and OP1.
3428 The resulting tree has type int. */
3431 pointer_diff (location_t loc
, tree op0
, tree op1
)
3433 tree restype
= ptrdiff_type_node
;
3434 tree result
, inttype
;
3436 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3437 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3438 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3439 tree con0
, con1
, lit0
, lit1
;
3440 tree orig_op1
= op1
;
3442 /* If the operands point into different address spaces, we need to
3443 explicitly convert them to pointers into the common address space
3444 before we can subtract the numerical address values. */
3447 addr_space_t as_common
;
3450 /* Determine the common superset address space. This is guaranteed
3451 to exist because the caller verified that comp_target_types
3452 returned non-zero. */
3453 if (!addr_space_superset (as0
, as1
, &as_common
))
3456 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3457 op0
= convert (common_type
, op0
);
3458 op1
= convert (common_type
, op1
);
3461 /* Determine integer type to perform computations in. This will usually
3462 be the same as the result type (ptrdiff_t), but may need to be a wider
3463 type if pointers for the address space are wider than ptrdiff_t. */
3464 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3465 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3470 if (TREE_CODE (target_type
) == VOID_TYPE
)
3471 pedwarn (loc
, OPT_Wpointer_arith
,
3472 "pointer of type %<void *%> used in subtraction");
3473 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3474 pedwarn (loc
, OPT_Wpointer_arith
,
3475 "pointer to a function used in subtraction");
3477 /* If the conversion to ptrdiff_type does anything like widening or
3478 converting a partial to an integral mode, we get a convert_expression
3479 that is in the way to do any simplifications.
3480 (fold-const.c doesn't know that the extra bits won't be needed.
3481 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3482 different mode in place.)
3483 So first try to find a common term here 'by hand'; we want to cover
3484 at least the cases that occur in legal static initializers. */
3485 if (CONVERT_EXPR_P (op0
)
3486 && (TYPE_PRECISION (TREE_TYPE (op0
))
3487 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))))
3488 con0
= TREE_OPERAND (op0
, 0);
3491 if (CONVERT_EXPR_P (op1
)
3492 && (TYPE_PRECISION (TREE_TYPE (op1
))
3493 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))))
3494 con1
= TREE_OPERAND (op1
, 0);
3498 if (TREE_CODE (con0
) == POINTER_PLUS_EXPR
)
3500 lit0
= TREE_OPERAND (con0
, 1);
3501 con0
= TREE_OPERAND (con0
, 0);
3504 lit0
= integer_zero_node
;
3506 if (TREE_CODE (con1
) == POINTER_PLUS_EXPR
)
3508 lit1
= TREE_OPERAND (con1
, 1);
3509 con1
= TREE_OPERAND (con1
, 0);
3512 lit1
= integer_zero_node
;
3514 if (operand_equal_p (con0
, con1
, 0))
3521 /* First do the subtraction as integers;
3522 then drop through to build the divide operator.
3523 Do not do default conversions on the minus operator
3524 in case restype is a short type. */
3526 op0
= build_binary_op (loc
,
3527 MINUS_EXPR
, convert (inttype
, op0
),
3528 convert (inttype
, op1
), 0);
3529 /* This generates an error if op1 is pointer to incomplete type. */
3530 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3531 error_at (loc
, "arithmetic on pointer to an incomplete type");
3533 /* This generates an error if op0 is pointer to incomplete type. */
3534 op1
= c_size_in_bytes (target_type
);
3536 /* Divide by the size, in easiest possible way. */
3537 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3538 op0
, convert (inttype
, op1
));
3540 /* Convert to final result type if necessary. */
3541 return convert (restype
, result
);
3544 /* Expand atomic compound assignments into an approriate sequence as
3545 specified by the C11 standard section 6.5.16.2.
3551 This sequence is used for all types for which these operations are
3554 In addition, built-in versions of the 'fe' prefixed routines may
3555 need to be invoked for floating point (real, complex or vector) when
3556 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3566 __atomic_load (addr, &old, SEQ_CST);
3567 feholdexcept (&fenv);
3569 newval = old op val;
3570 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3573 feclearexcept (FE_ALL_EXCEPT);
3576 feupdateenv (&fenv);
3578 Also note that the compiler is simply issuing the generic form of
3579 the atomic operations. This requires temp(s) and has their address
3580 taken. The atomic processing is smart enough to figure out when the
3581 size of an object can utilize a lock-free version, and convert the
3582 built-in call to the appropriate lock-free routine. The optimizers
3583 will then dispose of any temps that are no longer required, and
3584 lock-free implementations are utilized as long as there is target
3585 support for the required size.
3587 If the operator is NOP_EXPR, then this is a simple assignment, and
3588 an __atomic_store is issued to perform the assignment rather than
3593 /* Build an atomic assignment at LOC, expanding into the proper
3594 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3595 the result of the operation, unless RETURN_OLD_P in which case
3596 return the old value of LHS (this is only for postincrement and
3599 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3600 tree rhs
, bool return_old_p
)
3602 tree fndecl
, func_call
;
3603 vec
<tree
, va_gc
> *params
;
3604 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3607 tree stmt
, goto_stmt
;
3608 tree loop_label
, loop_decl
, done_label
, done_decl
;
3610 tree lhs_type
= TREE_TYPE (lhs
);
3611 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, 0);
3612 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3613 tree rhs_type
= TREE_TYPE (rhs
);
3615 gcc_assert (TYPE_ATOMIC (lhs_type
));
3618 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3620 /* Allocate enough vector items for a compare_exchange. */
3621 vec_alloc (params
, 6);
3623 /* Create a compound statement to hold the sequence of statements
3625 compound_stmt
= c_begin_compound_stmt (false);
3627 /* Fold the RHS if it hasn't already been folded. */
3628 if (modifycode
!= NOP_EXPR
)
3629 rhs
= c_fully_fold (rhs
, false, NULL
);
3631 /* Remove the qualifiers for the rest of the expressions and create
3632 the VAL temp variable to hold the RHS. */
3633 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3634 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3635 val
= create_tmp_var (nonatomic_rhs_type
, NULL
);
3636 TREE_ADDRESSABLE (val
) = 1;
3637 rhs
= build2 (MODIFY_EXPR
, nonatomic_rhs_type
, val
, rhs
);
3638 SET_EXPR_LOCATION (rhs
, loc
);
3641 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3643 if (modifycode
== NOP_EXPR
)
3645 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3646 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, 0);
3647 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3648 params
->quick_push (lhs_addr
);
3649 params
->quick_push (rhs
);
3650 params
->quick_push (seq_cst
);
3651 func_call
= build_function_call_vec (loc
, fndecl
, params
, NULL
);
3652 add_stmt (func_call
);
3654 /* Finish the compound statement. */
3655 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3657 /* VAL is the value which was stored, return a COMPOUND_STMT of
3658 the statement and that value. */
3659 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3662 /* Create the variables and labels required for the op= form. */
3663 old
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3664 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, 0);
3665 TREE_ADDRESSABLE (val
) = 1;
3667 newval
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3668 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, 0);
3669 TREE_ADDRESSABLE (newval
) = 1;
3671 loop_decl
= create_artificial_label (loc
);
3672 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
3674 done_decl
= create_artificial_label (loc
);
3675 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
3677 /* __atomic_load (addr, &old, SEQ_CST). */
3678 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
3679 params
->quick_push (lhs_addr
);
3680 params
->quick_push (old_addr
);
3681 params
->quick_push (seq_cst
);
3682 func_call
= build_function_call_vec (loc
, fndecl
, params
, NULL
);
3683 add_stmt (func_call
);
3684 params
->truncate (0);
3686 /* Create the expressions for floating-point environment
3687 manipulation, if required. */
3688 bool need_fenv
= (flag_trapping_math
3689 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
3690 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
3692 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
3695 add_stmt (hold_call
);
3698 add_stmt (loop_label
);
3700 /* newval = old + val; */
3701 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
3702 rhs
= convert_for_assignment (loc
, nonatomic_lhs_type
, rhs
, NULL_TREE
,
3703 ic_assign
, false, NULL_TREE
,
3705 if (rhs
!= error_mark_node
)
3707 rhs
= build2 (MODIFY_EXPR
, nonatomic_lhs_type
, newval
, rhs
);
3708 SET_EXPR_LOCATION (rhs
, loc
);
3712 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3714 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
3715 params
->quick_push (lhs_addr
);
3716 params
->quick_push (old_addr
);
3717 params
->quick_push (newval_addr
);
3718 params
->quick_push (integer_zero_node
);
3719 params
->quick_push (seq_cst
);
3720 params
->quick_push (seq_cst
);
3721 func_call
= build_function_call_vec (loc
, fndecl
, params
, NULL
);
3723 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
3724 SET_EXPR_LOCATION (goto_stmt
, loc
);
3726 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
3727 SET_EXPR_LOCATION (stmt
, loc
);
3731 add_stmt (clear_call
);
3734 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
3735 SET_EXPR_LOCATION (goto_stmt
, loc
);
3736 add_stmt (goto_stmt
);
3739 add_stmt (done_label
);
3742 add_stmt (update_call
);
3744 /* Finish the compound statement. */
3745 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3747 /* NEWVAL is the value that was successfully stored, return a
3748 COMPOUND_EXPR of the statement and the appropriate value. */
3749 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
3750 return_old_p
? old
: newval
);
3753 /* Construct and perhaps optimize a tree representation
3754 for a unary operation. CODE, a tree_code, specifies the operation
3755 and XARG is the operand.
3756 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3757 the default promotions (such as from short to int).
3758 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3759 allows non-lvalues; this is only used to handle conversion of non-lvalue
3760 arrays to pointers in C99.
3762 LOCATION is the location of the operator. */
3765 build_unary_op (location_t location
,
3766 enum tree_code code
, tree xarg
, int flag
)
3768 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3771 enum tree_code typecode
;
3773 tree ret
= error_mark_node
;
3774 tree eptype
= NULL_TREE
;
3775 int noconvert
= flag
;
3776 const char *invalid_op_diag
;
3779 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3781 arg
= remove_c_maybe_const_expr (arg
);
3783 if (code
!= ADDR_EXPR
)
3784 arg
= require_complete_type (arg
);
3786 typecode
= TREE_CODE (TREE_TYPE (arg
));
3787 if (typecode
== ERROR_MARK
)
3788 return error_mark_node
;
3789 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3790 typecode
= INTEGER_TYPE
;
3792 if ((invalid_op_diag
3793 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3795 error_at (location
, invalid_op_diag
);
3796 return error_mark_node
;
3799 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3801 eptype
= TREE_TYPE (arg
);
3802 arg
= TREE_OPERAND (arg
, 0);
3808 /* This is used for unary plus, because a CONVERT_EXPR
3809 is enough to prevent anybody from looking inside for
3810 associativity, but won't generate any code. */
3811 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3812 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3813 || typecode
== VECTOR_TYPE
))
3815 error_at (location
, "wrong type argument to unary plus");
3816 return error_mark_node
;
3818 else if (!noconvert
)
3819 arg
= default_conversion (arg
);
3820 arg
= non_lvalue_loc (location
, arg
);
3824 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3825 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3826 || typecode
== VECTOR_TYPE
))
3828 error_at (location
, "wrong type argument to unary minus");
3829 return error_mark_node
;
3831 else if (!noconvert
)
3832 arg
= default_conversion (arg
);
3836 /* ~ works on integer types and non float vectors. */
3837 if (typecode
== INTEGER_TYPE
3838 || (typecode
== VECTOR_TYPE
3839 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3842 arg
= default_conversion (arg
);
3844 else if (typecode
== COMPLEX_TYPE
)
3847 pedwarn (location
, OPT_Wpedantic
,
3848 "ISO C does not support %<~%> for complex conjugation");
3850 arg
= default_conversion (arg
);
3854 error_at (location
, "wrong type argument to bit-complement");
3855 return error_mark_node
;
3860 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3862 error_at (location
, "wrong type argument to abs");
3863 return error_mark_node
;
3865 else if (!noconvert
)
3866 arg
= default_conversion (arg
);
3870 /* Conjugating a real value is a no-op, but allow it anyway. */
3871 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3872 || typecode
== COMPLEX_TYPE
))
3874 error_at (location
, "wrong type argument to conjugation");
3875 return error_mark_node
;
3877 else if (!noconvert
)
3878 arg
= default_conversion (arg
);
3881 case TRUTH_NOT_EXPR
:
3882 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3883 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3884 && typecode
!= COMPLEX_TYPE
)
3887 "wrong type argument to unary exclamation mark");
3888 return error_mark_node
;
3892 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
3893 arg
= remove_c_maybe_const_expr (arg
);
3896 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3897 ret
= invert_truthvalue_loc (location
, arg
);
3898 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3899 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3900 location
= EXPR_LOCATION (ret
);
3901 goto return_build_unary_op
;
3905 ret
= build_real_imag_expr (location
, code
, arg
);
3906 if (ret
== error_mark_node
)
3907 return error_mark_node
;
3908 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3909 eptype
= TREE_TYPE (eptype
);
3910 goto return_build_unary_op
;
3912 case PREINCREMENT_EXPR
:
3913 case POSTINCREMENT_EXPR
:
3914 case PREDECREMENT_EXPR
:
3915 case POSTDECREMENT_EXPR
:
3917 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3919 tree inner
= build_unary_op (location
, code
,
3920 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3921 if (inner
== error_mark_node
)
3922 return error_mark_node
;
3923 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3924 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3925 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3926 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3927 goto return_build_unary_op
;
3930 /* Complain about anything that is not a true lvalue. In
3931 Objective-C, skip this check for property_refs. */
3932 if (!objc_is_property_ref (arg
)
3933 && !lvalue_or_else (location
,
3934 arg
, ((code
== PREINCREMENT_EXPR
3935 || code
== POSTINCREMENT_EXPR
)
3938 return error_mark_node
;
3940 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
3942 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3943 warning_at (location
, OPT_Wc___compat
,
3944 "increment of enumeration value is invalid in C++");
3946 warning_at (location
, OPT_Wc___compat
,
3947 "decrement of enumeration value is invalid in C++");
3950 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3951 arg
= c_fully_fold (arg
, false, NULL
);
3954 atomic_op
= really_atomic_lvalue (arg
);
3956 /* Increment or decrement the real part of the value,
3957 and don't change the imaginary part. */
3958 if (typecode
== COMPLEX_TYPE
)
3962 pedwarn (location
, OPT_Wpedantic
,
3963 "ISO C does not support %<++%> and %<--%> on complex types");
3967 arg
= stabilize_reference (arg
);
3968 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
3969 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
3970 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
3971 if (real
== error_mark_node
|| imag
== error_mark_node
)
3972 return error_mark_node
;
3973 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
3975 goto return_build_unary_op
;
3979 /* Report invalid types. */
3981 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3982 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
3983 && typecode
!= COMPLEX_TYPE
)
3985 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3986 error_at (location
, "wrong type argument to increment");
3988 error_at (location
, "wrong type argument to decrement");
3990 return error_mark_node
;
3996 argtype
= TREE_TYPE (arg
);
3998 /* Compute the increment. */
4000 if (typecode
== POINTER_TYPE
)
4002 /* If pointer target is an undefined struct,
4003 we just cannot know how to do the arithmetic. */
4004 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4006 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4008 "increment of pointer to unknown structure");
4011 "decrement of pointer to unknown structure");
4013 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4014 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4016 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4017 pedwarn (location
, OPT_Wpointer_arith
,
4018 "wrong type argument to increment");
4020 pedwarn (location
, OPT_Wpointer_arith
,
4021 "wrong type argument to decrement");
4024 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4025 inc
= convert_to_ptrofftype_loc (location
, inc
);
4027 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4029 /* For signed fract types, we invert ++ to -- or
4030 -- to ++, and change inc from 1 to -1, because
4031 it is not possible to represent 1 in signed fract constants.
4032 For unsigned fract types, the result always overflows and
4033 we get an undefined (original) or the maximum value. */
4034 if (code
== PREINCREMENT_EXPR
)
4035 code
= PREDECREMENT_EXPR
;
4036 else if (code
== PREDECREMENT_EXPR
)
4037 code
= PREINCREMENT_EXPR
;
4038 else if (code
== POSTINCREMENT_EXPR
)
4039 code
= POSTDECREMENT_EXPR
;
4040 else /* code == POSTDECREMENT_EXPR */
4041 code
= POSTINCREMENT_EXPR
;
4043 inc
= integer_minus_one_node
;
4044 inc
= convert (argtype
, inc
);
4048 inc
= integer_one_node
;
4049 inc
= convert (argtype
, inc
);
4052 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4053 need to ask Objective-C to build the increment or decrement
4054 expression for it. */
4055 if (objc_is_property_ref (arg
))
4056 return objc_build_incr_expr_for_property_ref (location
, code
,
4059 /* Report a read-only lvalue. */
4060 if (TYPE_READONLY (argtype
))
4062 readonly_error (location
, arg
,
4063 ((code
== PREINCREMENT_EXPR
4064 || code
== POSTINCREMENT_EXPR
)
4065 ? lv_increment
: lv_decrement
));
4066 return error_mark_node
;
4068 else if (TREE_READONLY (arg
))
4069 readonly_warning (arg
,
4070 ((code
== PREINCREMENT_EXPR
4071 || code
== POSTINCREMENT_EXPR
)
4072 ? lv_increment
: lv_decrement
));
4074 /* If the argument is atomic, use the special code sequences for
4075 atomic compound assignment. */
4078 arg
= stabilize_reference (arg
);
4079 ret
= build_atomic_assign (location
, arg
,
4080 ((code
== PREINCREMENT_EXPR
4081 || code
== POSTINCREMENT_EXPR
)
4084 (FRACT_MODE_P (TYPE_MODE (argtype
))
4086 : integer_one_node
),
4087 (code
== POSTINCREMENT_EXPR
4088 || code
== POSTDECREMENT_EXPR
));
4089 goto return_build_unary_op
;
4092 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4093 val
= boolean_increment (code
, arg
);
4095 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4096 TREE_SIDE_EFFECTS (val
) = 1;
4097 if (TREE_CODE (val
) != code
)
4098 TREE_NO_WARNING (val
) = 1;
4100 goto return_build_unary_op
;
4104 /* Note that this operation never does default_conversion. */
4106 /* The operand of unary '&' must be an lvalue (which excludes
4107 expressions of type void), or, in C99, the result of a [] or
4108 unary '*' operator. */
4109 if (VOID_TYPE_P (TREE_TYPE (arg
))
4110 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4111 && (TREE_CODE (arg
) != INDIRECT_REF
4113 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4115 /* Let &* cancel out to simplify resulting code. */
4116 if (TREE_CODE (arg
) == INDIRECT_REF
)
4118 /* Don't let this be an lvalue. */
4119 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4120 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4121 ret
= TREE_OPERAND (arg
, 0);
4122 goto return_build_unary_op
;
4125 /* For &x[y], return x+y */
4126 if (TREE_CODE (arg
) == ARRAY_REF
)
4128 tree op0
= TREE_OPERAND (arg
, 0);
4129 if (!c_mark_addressable (op0
))
4130 return error_mark_node
;
4133 /* Anything not already handled and not a true memory reference
4134 or a non-lvalue array is an error. */
4135 else if (typecode
!= FUNCTION_TYPE
&& !flag
4136 && !lvalue_or_else (location
, arg
, lv_addressof
))
4137 return error_mark_node
;
4139 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4141 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4143 tree inner
= build_unary_op (location
, code
,
4144 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
4145 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4146 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4147 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4148 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4149 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4150 goto return_build_unary_op
;
4153 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4154 argtype
= TREE_TYPE (arg
);
4156 /* If the lvalue is const or volatile, merge that into the type
4157 to which the address will point. This is only needed
4158 for function types. */
4159 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4160 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4161 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4163 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4164 int quals
= orig_quals
;
4166 if (TREE_READONLY (arg
))
4167 quals
|= TYPE_QUAL_CONST
;
4168 if (TREE_THIS_VOLATILE (arg
))
4169 quals
|= TYPE_QUAL_VOLATILE
;
4171 argtype
= c_build_qualified_type (argtype
, quals
);
4174 if (!c_mark_addressable (arg
))
4175 return error_mark_node
;
4177 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4178 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4180 argtype
= build_pointer_type (argtype
);
4182 /* ??? Cope with user tricks that amount to offsetof. Delete this
4183 when we have proper support for integer constant expressions. */
4184 val
= get_base_address (arg
);
4185 if (val
&& TREE_CODE (val
) == INDIRECT_REF
4186 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4188 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4189 goto return_build_unary_op
;
4192 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4195 goto return_build_unary_op
;
4202 argtype
= TREE_TYPE (arg
);
4203 if (TREE_CODE (arg
) == INTEGER_CST
)
4204 ret
= (require_constant_value
4205 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4206 : fold_build1_loc (location
, code
, argtype
, arg
));
4208 ret
= build1 (code
, argtype
, arg
);
4209 return_build_unary_op
:
4210 gcc_assert (ret
!= error_mark_node
);
4211 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4212 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4213 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4214 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4215 ret
= note_integer_operands (ret
);
4217 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4218 protected_set_expr_location (ret
, location
);
4222 /* Return nonzero if REF is an lvalue valid for this language.
4223 Lvalues can be assigned, unless their type has TYPE_READONLY.
4224 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4227 lvalue_p (const_tree ref
)
4229 const enum tree_code code
= TREE_CODE (ref
);
4236 return lvalue_p (TREE_OPERAND (ref
, 0));
4238 case C_MAYBE_CONST_EXPR
:
4239 return lvalue_p (TREE_OPERAND (ref
, 1));
4241 case COMPOUND_LITERAL_EXPR
:
4247 case ARRAY_NOTATION_REF
:
4252 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4253 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4256 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4263 /* Give a warning for storing in something that is read-only in GCC
4264 terms but not const in ISO C terms. */
4267 readonly_warning (tree arg
, enum lvalue_use use
)
4272 warning (0, "assignment of read-only location %qE", arg
);
4275 warning (0, "increment of read-only location %qE", arg
);
4278 warning (0, "decrement of read-only location %qE", arg
);
4287 /* Return nonzero if REF is an lvalue valid for this language;
4288 otherwise, print an error message and return zero. USE says
4289 how the lvalue is being used and so selects the error message.
4290 LOCATION is the location at which any error should be reported. */
4293 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4295 int win
= lvalue_p (ref
);
4298 lvalue_error (loc
, use
);
4303 /* Mark EXP saying that we need to be able to take the
4304 address of it; it should not be allocated in a register.
4305 Returns true if successful. */
4308 c_mark_addressable (tree exp
)
4313 switch (TREE_CODE (x
))
4316 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
4319 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
4323 /* ... fall through ... */
4329 x
= TREE_OPERAND (x
, 0);
4332 case COMPOUND_LITERAL_EXPR
:
4334 TREE_ADDRESSABLE (x
) = 1;
4341 if (C_DECL_REGISTER (x
)
4342 && DECL_NONLOCAL (x
))
4344 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4347 ("global register variable %qD used in nested function", x
);
4350 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4352 else if (C_DECL_REGISTER (x
))
4354 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4355 error ("address of global register variable %qD requested", x
);
4357 error ("address of register variable %qD requested", x
);
4363 TREE_ADDRESSABLE (x
) = 1;
4370 /* Convert EXPR to TYPE, warning about conversion problems with
4371 constants. SEMANTIC_TYPE is the type this conversion would use
4372 without excess precision. If SEMANTIC_TYPE is NULL, this function
4373 is equivalent to convert_and_check. This function is a wrapper that
4374 handles conversions that may be different than
4375 the usual ones because of excess precision. */
4378 ep_convert_and_check (tree type
, tree expr
, tree semantic_type
)
4380 if (TREE_TYPE (expr
) == type
)
4384 return convert_and_check (type
, expr
);
4386 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4387 && TREE_TYPE (expr
) != semantic_type
)
4389 /* For integers, we need to check the real conversion, not
4390 the conversion to the excess precision type. */
4391 expr
= convert_and_check (semantic_type
, expr
);
4393 /* Result type is the excess precision type, which should be
4394 large enough, so do not check. */
4395 return convert (type
, expr
);
4398 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4399 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4400 if folded to an integer constant then the unselected half may
4401 contain arbitrary operations not normally permitted in constant
4402 expressions. Set the location of the expression to LOC. */
4405 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4406 tree op1
, tree op1_original_type
, tree op2
,
4407 tree op2_original_type
)
4411 enum tree_code code1
;
4412 enum tree_code code2
;
4413 tree result_type
= NULL
;
4414 tree semantic_result_type
= NULL
;
4415 tree orig_op1
= op1
, orig_op2
= op2
;
4416 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4417 bool ifexp_int_operands
;
4420 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4421 if (op1_int_operands
)
4422 op1
= remove_c_maybe_const_expr (op1
);
4423 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4424 if (op2_int_operands
)
4425 op2
= remove_c_maybe_const_expr (op2
);
4426 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4427 if (ifexp_int_operands
)
4428 ifexp
= remove_c_maybe_const_expr (ifexp
);
4430 /* Promote both alternatives. */
4432 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4433 op1
= default_conversion (op1
);
4434 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4435 op2
= default_conversion (op2
);
4437 if (TREE_CODE (ifexp
) == ERROR_MARK
4438 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4439 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4440 return error_mark_node
;
4442 type1
= TREE_TYPE (op1
);
4443 code1
= TREE_CODE (type1
);
4444 type2
= TREE_TYPE (op2
);
4445 code2
= TREE_CODE (type2
);
4447 /* C90 does not permit non-lvalue arrays in conditional expressions.
4448 In C99 they will be pointers by now. */
4449 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4451 error_at (colon_loc
, "non-lvalue array in conditional expression");
4452 return error_mark_node
;
4455 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4456 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4457 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4458 || code1
== COMPLEX_TYPE
)
4459 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4460 || code2
== COMPLEX_TYPE
))
4462 semantic_result_type
= c_common_type (type1
, type2
);
4463 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4465 op1
= TREE_OPERAND (op1
, 0);
4466 type1
= TREE_TYPE (op1
);
4467 gcc_assert (TREE_CODE (type1
) == code1
);
4469 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4471 op2
= TREE_OPERAND (op2
, 0);
4472 type2
= TREE_TYPE (op2
);
4473 gcc_assert (TREE_CODE (type2
) == code2
);
4477 if (warn_cxx_compat
)
4479 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4480 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4482 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4483 && TREE_CODE (t2
) == ENUMERAL_TYPE
4484 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4485 warning_at (colon_loc
, OPT_Wc___compat
,
4486 ("different enum types in conditional is "
4487 "invalid in C++: %qT vs %qT"),
4491 /* Quickly detect the usual case where op1 and op2 have the same type
4493 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4496 result_type
= type1
;
4498 result_type
= TYPE_MAIN_VARIANT (type1
);
4500 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4501 || code1
== COMPLEX_TYPE
)
4502 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4503 || code2
== COMPLEX_TYPE
))
4505 result_type
= c_common_type (type1
, type2
);
4506 do_warn_double_promotion (result_type
, type1
, type2
,
4507 "implicit conversion from %qT to %qT to "
4508 "match other result of conditional",
4511 /* If -Wsign-compare, warn here if type1 and type2 have
4512 different signedness. We'll promote the signed to unsigned
4513 and later code won't know it used to be different.
4514 Do this check on the original types, so that explicit casts
4515 will be considered, but default promotions won't. */
4516 if (c_inhibit_evaluation_warnings
== 0)
4518 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4519 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4521 if (unsigned_op1
^ unsigned_op2
)
4525 /* Do not warn if the result type is signed, since the
4526 signed type will only be chosen if it can represent
4527 all the values of the unsigned type. */
4528 if (!TYPE_UNSIGNED (result_type
))
4532 bool op1_maybe_const
= true;
4533 bool op2_maybe_const
= true;
4535 /* Do not warn if the signed quantity is an
4536 unsuffixed integer literal (or some static
4537 constant expression involving such literals) and
4538 it is non-negative. This warning requires the
4539 operands to be folded for best results, so do
4540 that folding in this case even without
4541 warn_sign_compare to avoid warning options
4542 possibly affecting code generation. */
4543 c_inhibit_evaluation_warnings
4544 += (ifexp
== truthvalue_false_node
);
4545 op1
= c_fully_fold (op1
, require_constant_value
,
4547 c_inhibit_evaluation_warnings
4548 -= (ifexp
== truthvalue_false_node
);
4550 c_inhibit_evaluation_warnings
4551 += (ifexp
== truthvalue_true_node
);
4552 op2
= c_fully_fold (op2
, require_constant_value
,
4554 c_inhibit_evaluation_warnings
4555 -= (ifexp
== truthvalue_true_node
);
4557 if (warn_sign_compare
)
4560 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4562 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4565 warning_at (colon_loc
, OPT_Wsign_compare
,
4566 ("signed and unsigned type in "
4567 "conditional expression"));
4569 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4570 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4571 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4572 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4577 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4579 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4580 pedwarn (colon_loc
, OPT_Wpedantic
,
4581 "ISO C forbids conditional expr with only one void side");
4582 result_type
= void_type_node
;
4584 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4586 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
4587 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
4588 addr_space_t as_common
;
4590 if (comp_target_types (colon_loc
, type1
, type2
))
4591 result_type
= common_pointer_type (type1
, type2
);
4592 else if (null_pointer_constant_p (orig_op1
))
4593 result_type
= type2
;
4594 else if (null_pointer_constant_p (orig_op2
))
4595 result_type
= type1
;
4596 else if (!addr_space_superset (as1
, as2
, &as_common
))
4598 error_at (colon_loc
, "pointers to disjoint address spaces "
4599 "used in conditional expression");
4600 return error_mark_node
;
4602 else if (VOID_TYPE_P (TREE_TYPE (type1
))
4603 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
4605 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
4606 pedwarn (colon_loc
, OPT_Wpedantic
,
4607 "ISO C forbids conditional expr between "
4608 "%<void *%> and function pointer");
4609 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
4610 TREE_TYPE (type2
)));
4612 else if (VOID_TYPE_P (TREE_TYPE (type2
))
4613 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
4615 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
4616 pedwarn (colon_loc
, OPT_Wpedantic
,
4617 "ISO C forbids conditional expr between "
4618 "%<void *%> and function pointer");
4619 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
4620 TREE_TYPE (type1
)));
4622 /* Objective-C pointer comparisons are a bit more lenient. */
4623 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
4624 result_type
= objc_common_type (type1
, type2
);
4627 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
4629 pedwarn (colon_loc
, 0,
4630 "pointer type mismatch in conditional expression");
4631 result_type
= build_pointer_type
4632 (build_qualified_type (void_type_node
, qual
));
4635 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4637 if (!null_pointer_constant_p (orig_op2
))
4638 pedwarn (colon_loc
, 0,
4639 "pointer/integer type mismatch in conditional expression");
4642 op2
= null_pointer_node
;
4644 result_type
= type1
;
4646 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4648 if (!null_pointer_constant_p (orig_op1
))
4649 pedwarn (colon_loc
, 0,
4650 "pointer/integer type mismatch in conditional expression");
4653 op1
= null_pointer_node
;
4655 result_type
= type2
;
4660 if (flag_cond_mismatch
)
4661 result_type
= void_type_node
;
4664 error_at (colon_loc
, "type mismatch in conditional expression");
4665 return error_mark_node
;
4669 /* Merge const and volatile flags of the incoming types. */
4671 = build_type_variant (result_type
,
4672 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4673 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4675 op1
= ep_convert_and_check (result_type
, op1
, semantic_result_type
);
4676 op2
= ep_convert_and_check (result_type
, op2
, semantic_result_type
);
4678 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4680 op2_int_operands
= true;
4681 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4683 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4685 op1_int_operands
= true;
4686 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4688 int_const
= int_operands
= (ifexp_int_operands
4690 && op2_int_operands
);
4693 int_const
= ((ifexp
== truthvalue_true_node
4694 && TREE_CODE (orig_op1
) == INTEGER_CST
4695 && !TREE_OVERFLOW (orig_op1
))
4696 || (ifexp
== truthvalue_false_node
4697 && TREE_CODE (orig_op2
) == INTEGER_CST
4698 && !TREE_OVERFLOW (orig_op2
)));
4700 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4701 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4706 op1
= remove_c_maybe_const_expr (op1
);
4707 op2
= remove_c_maybe_const_expr (op2
);
4709 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4711 ret
= note_integer_operands (ret
);
4713 if (semantic_result_type
)
4714 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
4716 protected_set_expr_location (ret
, colon_loc
);
4720 /* Return a compound expression that performs two expressions and
4721 returns the value of the second of them.
4723 LOC is the location of the COMPOUND_EXPR. */
4726 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4728 bool expr1_int_operands
, expr2_int_operands
;
4729 tree eptype
= NULL_TREE
;
4732 if (flag_enable_cilkplus
4733 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
4734 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
4737 "spawned function call cannot be part of a comma expression");
4738 return error_mark_node
;
4740 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4741 if (expr1_int_operands
)
4742 expr1
= remove_c_maybe_const_expr (expr1
);
4743 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4744 if (expr2_int_operands
)
4745 expr2
= remove_c_maybe_const_expr (expr2
);
4747 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4748 expr1
= TREE_OPERAND (expr1
, 0);
4749 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4751 eptype
= TREE_TYPE (expr2
);
4752 expr2
= TREE_OPERAND (expr2
, 0);
4755 if (!TREE_SIDE_EFFECTS (expr1
))
4757 /* The left-hand operand of a comma expression is like an expression
4758 statement: with -Wunused, we should warn if it doesn't have
4759 any side-effects, unless it was explicitly cast to (void). */
4760 if (warn_unused_value
)
4762 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4763 && CONVERT_EXPR_P (expr1
))
4765 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4766 && TREE_CODE (expr1
) == COMPOUND_EXPR
4767 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4768 ; /* (void) a, (void) b, c */
4770 warning_at (loc
, OPT_Wunused_value
,
4771 "left-hand operand of comma expression has no effect");
4775 /* With -Wunused, we should also warn if the left-hand operand does have
4776 side-effects, but computes a value which is not used. For example, in
4777 `foo() + bar(), baz()' the result of the `+' operator is not used,
4778 so we should issue a warning. */
4779 else if (warn_unused_value
)
4780 warn_if_unused_value (expr1
, loc
);
4782 if (expr2
== error_mark_node
)
4783 return error_mark_node
;
4785 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4788 && expr1_int_operands
4789 && expr2_int_operands
)
4790 ret
= note_integer_operands (ret
);
4793 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4795 protected_set_expr_location (ret
, loc
);
4799 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4800 which we are casting. OTYPE is the type of the expression being
4801 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4802 of the cast. -Wcast-qual appeared on the command line. Named
4803 address space qualifiers are not handled here, because they result
4804 in different warnings. */
4807 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
4809 tree in_type
= type
;
4810 tree in_otype
= otype
;
4815 /* Check that the qualifiers on IN_TYPE are a superset of the
4816 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4817 nodes is uninteresting and we stop as soon as we hit a
4818 non-POINTER_TYPE node on either type. */
4821 in_otype
= TREE_TYPE (in_otype
);
4822 in_type
= TREE_TYPE (in_type
);
4824 /* GNU C allows cv-qualified function types. 'const' means the
4825 function is very pure, 'volatile' means it can't return. We
4826 need to warn when such qualifiers are added, not when they're
4828 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4829 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4830 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
4831 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
4833 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
4834 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
4836 while (TREE_CODE (in_type
) == POINTER_TYPE
4837 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4840 warning_at (loc
, OPT_Wcast_qual
,
4841 "cast adds %q#v qualifier to function type", added
);
4844 /* There are qualifiers present in IN_OTYPE that are not present
4846 warning_at (loc
, OPT_Wcast_qual
,
4847 "cast discards %q#v qualifier from pointer target type",
4850 if (added
|| discarded
)
4853 /* A cast from **T to const **T is unsafe, because it can cause a
4854 const value to be changed with no additional warning. We only
4855 issue this warning if T is the same on both sides, and we only
4856 issue the warning if there are the same number of pointers on
4857 both sides, as otherwise the cast is clearly unsafe anyhow. A
4858 cast is unsafe when a qualifier is added at one level and const
4859 is not present at all outer levels.
4861 To issue this warning, we check at each level whether the cast
4862 adds new qualifiers not already seen. We don't need to special
4863 case function types, as they won't have the same
4864 TYPE_MAIN_VARIANT. */
4866 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4868 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4873 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4876 in_type
= TREE_TYPE (in_type
);
4877 in_otype
= TREE_TYPE (in_otype
);
4878 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4881 warning_at (loc
, OPT_Wcast_qual
,
4882 "to be safe all intermediate pointers in cast from "
4883 "%qT to %qT must be %<const%> qualified",
4888 is_const
= TYPE_READONLY (in_type
);
4890 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4893 /* Build an expression representing a cast to type TYPE of expression EXPR.
4894 LOC is the location of the cast-- typically the open paren of the cast. */
4897 build_c_cast (location_t loc
, tree type
, tree expr
)
4901 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4902 expr
= TREE_OPERAND (expr
, 0);
4906 if (type
== error_mark_node
|| expr
== error_mark_node
)
4907 return error_mark_node
;
4909 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4910 only in <protocol> qualifications. But when constructing cast expressions,
4911 the protocols do matter and must be kept around. */
4912 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
4913 return build1 (NOP_EXPR
, type
, expr
);
4915 type
= TYPE_MAIN_VARIANT (type
);
4917 if (TREE_CODE (type
) == ARRAY_TYPE
)
4919 error_at (loc
, "cast specifies array type");
4920 return error_mark_node
;
4923 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4925 error_at (loc
, "cast specifies function type");
4926 return error_mark_node
;
4929 if (!VOID_TYPE_P (type
))
4931 value
= require_complete_type (value
);
4932 if (value
== error_mark_node
)
4933 return error_mark_node
;
4936 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
4938 if (TREE_CODE (type
) == RECORD_TYPE
4939 || TREE_CODE (type
) == UNION_TYPE
)
4940 pedwarn (loc
, OPT_Wpedantic
,
4941 "ISO C forbids casting nonscalar to the same type");
4943 else if (TREE_CODE (type
) == UNION_TYPE
)
4947 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4948 if (TREE_TYPE (field
) != error_mark_node
4949 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
4950 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
4956 bool maybe_const
= true;
4958 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
4959 t
= c_fully_fold (value
, false, &maybe_const
);
4960 t
= build_constructor_single (type
, field
, t
);
4962 t
= c_wrap_maybe_const (t
, true);
4963 t
= digest_init (loc
, type
, t
,
4964 NULL_TREE
, false, true, 0);
4965 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
4968 error_at (loc
, "cast to union type from type not present in union");
4969 return error_mark_node
;
4975 if (type
== void_type_node
)
4977 tree t
= build1 (CONVERT_EXPR
, type
, value
);
4978 SET_EXPR_LOCATION (t
, loc
);
4982 otype
= TREE_TYPE (value
);
4984 /* Optionally warn about potentially worrisome casts. */
4986 && TREE_CODE (type
) == POINTER_TYPE
4987 && TREE_CODE (otype
) == POINTER_TYPE
)
4988 handle_warn_cast_qual (loc
, type
, otype
);
4990 /* Warn about conversions between pointers to disjoint
4992 if (TREE_CODE (type
) == POINTER_TYPE
4993 && TREE_CODE (otype
) == POINTER_TYPE
4994 && !null_pointer_constant_p (value
))
4996 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
4997 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
4998 addr_space_t as_common
;
5000 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5002 if (ADDR_SPACE_GENERIC_P (as_from
))
5003 warning_at (loc
, 0, "cast to %s address space pointer "
5004 "from disjoint generic address space pointer",
5005 c_addr_space_name (as_to
));
5007 else if (ADDR_SPACE_GENERIC_P (as_to
))
5008 warning_at (loc
, 0, "cast to generic address space pointer "
5009 "from disjoint %s address space pointer",
5010 c_addr_space_name (as_from
));
5013 warning_at (loc
, 0, "cast to %s address space pointer "
5014 "from disjoint %s address space pointer",
5015 c_addr_space_name (as_to
),
5016 c_addr_space_name (as_from
));
5020 /* Warn about possible alignment problems. */
5021 if (STRICT_ALIGNMENT
5022 && TREE_CODE (type
) == POINTER_TYPE
5023 && TREE_CODE (otype
) == POINTER_TYPE
5024 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5025 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5026 /* Don't warn about opaque types, where the actual alignment
5027 restriction is unknown. */
5028 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
5029 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
5030 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5031 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5032 warning_at (loc
, OPT_Wcast_align
,
5033 "cast increases required alignment of target type");
5035 if (TREE_CODE (type
) == INTEGER_TYPE
5036 && TREE_CODE (otype
) == POINTER_TYPE
5037 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5038 /* Unlike conversion of integers to pointers, where the
5039 warning is disabled for converting constants because
5040 of cases such as SIG_*, warn about converting constant
5041 pointers to integers. In some cases it may cause unwanted
5042 sign extension, and a warning is appropriate. */
5043 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5044 "cast from pointer to integer of different size");
5046 if (TREE_CODE (value
) == CALL_EXPR
5047 && TREE_CODE (type
) != TREE_CODE (otype
))
5048 warning_at (loc
, OPT_Wbad_function_cast
,
5049 "cast from function call of type %qT "
5050 "to non-matching type %qT", otype
, type
);
5052 if (TREE_CODE (type
) == POINTER_TYPE
5053 && TREE_CODE (otype
) == INTEGER_TYPE
5054 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5055 /* Don't warn about converting any constant. */
5056 && !TREE_CONSTANT (value
))
5058 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5059 "of different size");
5061 if (warn_strict_aliasing
<= 2)
5062 strict_aliasing_warning (otype
, type
, expr
);
5064 /* If pedantic, warn for conversions between function and object
5065 pointer types, except for converting a null pointer constant
5066 to function pointer type. */
5068 && TREE_CODE (type
) == POINTER_TYPE
5069 && TREE_CODE (otype
) == POINTER_TYPE
5070 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5071 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5072 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5073 "conversion of function pointer to object pointer type");
5076 && TREE_CODE (type
) == POINTER_TYPE
5077 && TREE_CODE (otype
) == POINTER_TYPE
5078 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5079 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5080 && !null_pointer_constant_p (value
))
5081 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5082 "conversion of object pointer to function pointer type");
5085 value
= convert (type
, value
);
5087 /* Ignore any integer overflow caused by the cast. */
5088 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5090 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5092 if (!TREE_OVERFLOW (value
))
5094 /* Avoid clobbering a shared constant. */
5095 value
= copy_node (value
);
5096 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5099 else if (TREE_OVERFLOW (value
))
5100 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5101 value
= build_int_cst_wide (TREE_TYPE (value
),
5102 TREE_INT_CST_LOW (value
),
5103 TREE_INT_CST_HIGH (value
));
5107 /* Don't let a cast be an lvalue. */
5109 value
= non_lvalue_loc (loc
, value
);
5111 /* Don't allow the results of casting to floating-point or complex
5112 types be confused with actual constants, or casts involving
5113 integer and pointer types other than direct integer-to-integer
5114 and integer-to-pointer be confused with integer constant
5115 expressions and null pointer constants. */
5116 if (TREE_CODE (value
) == REAL_CST
5117 || TREE_CODE (value
) == COMPLEX_CST
5118 || (TREE_CODE (value
) == INTEGER_CST
5119 && !((TREE_CODE (expr
) == INTEGER_CST
5120 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5121 || TREE_CODE (expr
) == REAL_CST
5122 || TREE_CODE (expr
) == COMPLEX_CST
)))
5123 value
= build1 (NOP_EXPR
, type
, value
);
5125 if (CAN_HAVE_LOCATION_P (value
))
5126 SET_EXPR_LOCATION (value
, loc
);
5130 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5131 location of the open paren of the cast, or the position of the cast
5134 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5137 tree type_expr
= NULL_TREE
;
5138 bool type_expr_const
= true;
5140 int saved_wsp
= warn_strict_prototypes
;
5142 /* This avoids warnings about unprototyped casts on
5143 integers. E.g. "#define SIG_DFL (void(*)())0". */
5144 if (TREE_CODE (expr
) == INTEGER_CST
)
5145 warn_strict_prototypes
= 0;
5146 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5147 warn_strict_prototypes
= saved_wsp
;
5149 ret
= build_c_cast (loc
, type
, expr
);
5152 bool inner_expr_const
= true;
5153 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5154 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5155 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5156 && inner_expr_const
);
5157 SET_EXPR_LOCATION (ret
, loc
);
5160 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
5161 SET_EXPR_LOCATION (ret
, loc
);
5163 /* C++ does not permits types to be defined in a cast, but it
5164 allows references to incomplete types. */
5165 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5166 warning_at (loc
, OPT_Wc___compat
,
5167 "defining a type in a cast is invalid in C++");
5172 /* Build an assignment expression of lvalue LHS from value RHS.
5173 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5174 may differ from TREE_TYPE (LHS) for an enum bitfield.
5175 MODIFYCODE is the code for a binary operator that we use
5176 to combine the old value of LHS with RHS to get the new value.
5177 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5178 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5179 which may differ from TREE_TYPE (RHS) for an enum value.
5181 LOCATION is the location of the MODIFYCODE operator.
5182 RHS_LOC is the location of the RHS. */
5185 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5186 enum tree_code modifycode
,
5187 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5191 tree rhs_semantic_type
= NULL_TREE
;
5192 tree lhstype
= TREE_TYPE (lhs
);
5193 tree olhstype
= lhstype
;
5197 /* Types that aren't fully specified cannot be used in assignments. */
5198 lhs
= require_complete_type (lhs
);
5200 /* Avoid duplicate error messages from operands that had errors. */
5201 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5202 return error_mark_node
;
5204 /* For ObjC properties, defer this check. */
5205 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5206 return error_mark_node
;
5208 is_atomic_op
= really_atomic_lvalue (lhs
);
5210 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5212 rhs_semantic_type
= TREE_TYPE (rhs
);
5213 rhs
= TREE_OPERAND (rhs
, 0);
5218 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5220 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5221 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5223 if (inner
== error_mark_node
)
5224 return error_mark_node
;
5225 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5226 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5227 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5228 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5229 protected_set_expr_location (result
, location
);
5233 /* If a binary op has been requested, combine the old LHS value with the RHS
5234 producing the value we should actually store into the LHS. */
5236 if (modifycode
!= NOP_EXPR
)
5238 lhs
= c_fully_fold (lhs
, false, NULL
);
5239 lhs
= stabilize_reference (lhs
);
5241 /* Construct the RHS for any non-atomic compound assignemnt. */
5244 newrhs
= build_binary_op (location
,
5245 modifycode
, lhs
, rhs
, 1);
5247 /* The original type of the right hand side is no longer
5249 rhs_origtype
= NULL_TREE
;
5253 if (c_dialect_objc ())
5255 /* Check if we are modifying an Objective-C property reference;
5256 if so, we need to generate setter calls. */
5257 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5261 /* Else, do the check that we postponed for Objective-C. */
5262 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5263 return error_mark_node
;
5266 /* Give an error for storing in something that is 'const'. */
5268 if (TYPE_READONLY (lhstype
)
5269 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5270 || TREE_CODE (lhstype
) == UNION_TYPE
)
5271 && C_TYPE_FIELDS_READONLY (lhstype
)))
5273 readonly_error (location
, lhs
, lv_assign
);
5274 return error_mark_node
;
5276 else if (TREE_READONLY (lhs
))
5277 readonly_warning (lhs
, lv_assign
);
5279 /* If storing into a structure or union member,
5280 it has probably been given type `int'.
5281 Compute the type that would go with
5282 the actual amount of storage the member occupies. */
5284 if (TREE_CODE (lhs
) == COMPONENT_REF
5285 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5286 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5287 || TREE_CODE (lhstype
) == REAL_TYPE
5288 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5289 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5291 /* If storing in a field that is in actuality a short or narrower than one,
5292 we must store in the field in its actual type. */
5294 if (lhstype
!= TREE_TYPE (lhs
))
5296 lhs
= copy_node (lhs
);
5297 TREE_TYPE (lhs
) = lhstype
;
5300 /* Issue -Wc++-compat warnings about an assignment to an enum type
5301 when LHS does not have its original type. This happens for,
5302 e.g., an enum bitfield in a struct. */
5304 && lhs_origtype
!= NULL_TREE
5305 && lhs_origtype
!= lhstype
5306 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5308 tree checktype
= (rhs_origtype
!= NULL_TREE
5311 if (checktype
!= error_mark_node
5312 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5313 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5314 warning_at (location
, OPT_Wc___compat
,
5315 "enum conversion in assignment is invalid in C++");
5318 /* If the lhs is atomic, remove that qualifier. */
5321 lhstype
= build_qualified_type (lhstype
,
5322 (TYPE_QUALS (lhstype
)
5323 & ~TYPE_QUAL_ATOMIC
));
5324 olhstype
= build_qualified_type (olhstype
,
5325 (TYPE_QUALS (lhstype
)
5326 & ~TYPE_QUAL_ATOMIC
));
5329 /* Convert new value to destination type. Fold it first, then
5330 restore any excess precision information, for the sake of
5331 conversion warnings. */
5333 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5335 npc
= null_pointer_constant_p (newrhs
);
5336 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5337 if (rhs_semantic_type
)
5338 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5339 newrhs
= convert_for_assignment (location
, lhstype
, newrhs
, rhs_origtype
,
5340 ic_assign
, npc
, NULL_TREE
,
5342 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5343 return error_mark_node
;
5346 /* Emit ObjC write barrier, if necessary. */
5347 if (c_dialect_objc () && flag_objc_gc
)
5349 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5352 protected_set_expr_location (result
, location
);
5357 /* Scan operands. */
5360 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5363 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5364 TREE_SIDE_EFFECTS (result
) = 1;
5365 protected_set_expr_location (result
, location
);
5368 /* If we got the LHS in a different type for storing in,
5369 convert the result back to the nominal type of LHS
5370 so that the value we return always has the same type
5371 as the LHS argument. */
5373 if (olhstype
== TREE_TYPE (result
))
5376 result
= convert_for_assignment (location
, olhstype
, result
, rhs_origtype
,
5377 ic_assign
, false, NULL_TREE
, NULL_TREE
, 0);
5378 protected_set_expr_location (result
, location
);
5382 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5383 This is used to implement -fplan9-extensions. */
5386 find_anonymous_field_with_type (tree struct_type
, tree type
)
5391 gcc_assert (TREE_CODE (struct_type
) == RECORD_TYPE
5392 || TREE_CODE (struct_type
) == UNION_TYPE
);
5394 for (field
= TYPE_FIELDS (struct_type
);
5396 field
= TREE_CHAIN (field
))
5398 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5399 ? c_build_qualified_type (TREE_TYPE (field
),
5401 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5402 if (DECL_NAME (field
) == NULL
5403 && comptypes (type
, fieldtype
))
5409 else if (DECL_NAME (field
) == NULL
5410 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
5411 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5412 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5422 /* RHS is an expression whose type is pointer to struct. If there is
5423 an anonymous field in RHS with type TYPE, then return a pointer to
5424 that field in RHS. This is used with -fplan9-extensions. This
5425 returns NULL if no conversion could be found. */
5428 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5430 tree rhs_struct_type
, lhs_main_type
;
5431 tree field
, found_field
;
5432 bool found_sub_field
;
5435 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5436 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5437 gcc_assert (TREE_CODE (rhs_struct_type
) == RECORD_TYPE
5438 || TREE_CODE (rhs_struct_type
) == UNION_TYPE
);
5440 gcc_assert (POINTER_TYPE_P (type
));
5441 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5442 ? c_build_qualified_type (TREE_TYPE (type
),
5444 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5446 found_field
= NULL_TREE
;
5447 found_sub_field
= false;
5448 for (field
= TYPE_FIELDS (rhs_struct_type
);
5450 field
= TREE_CHAIN (field
))
5452 if (DECL_NAME (field
) != NULL_TREE
5453 || (TREE_CODE (TREE_TYPE (field
)) != RECORD_TYPE
5454 && TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
))
5456 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5457 ? c_build_qualified_type (TREE_TYPE (field
),
5459 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5460 if (comptypes (lhs_main_type
, fieldtype
))
5462 if (found_field
!= NULL_TREE
)
5464 found_field
= field
;
5466 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5469 if (found_field
!= NULL_TREE
)
5471 found_field
= field
;
5472 found_sub_field
= true;
5476 if (found_field
== NULL_TREE
)
5479 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5480 build_fold_indirect_ref (rhs
), found_field
,
5482 ret
= build_fold_addr_expr_loc (location
, ret
);
5484 if (found_sub_field
)
5486 ret
= convert_to_anonymous_field (location
, type
, ret
);
5487 gcc_assert (ret
!= NULL_TREE
);
5493 /* Convert value RHS to type TYPE as preparation for an assignment to
5494 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5495 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5496 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5497 constant before any folding.
5498 The real work of conversion is done by `convert'.
5499 The purpose of this function is to generate error messages
5500 for assignments that are not allowed in C.
5501 ERRTYPE says whether it is argument passing, assignment,
5502 initialization or return.
5504 LOCATION is the location of the RHS.
5505 FUNCTION is a tree for the function being called.
5506 PARMNUM is the number of the argument, for printing in error messages. */
5509 convert_for_assignment (location_t location
, tree type
, tree rhs
,
5510 tree origtype
, enum impl_conv errtype
,
5511 bool null_pointer_constant
, tree fundecl
,
5512 tree function
, int parmnum
)
5514 enum tree_code codel
= TREE_CODE (type
);
5515 tree orig_rhs
= rhs
;
5517 enum tree_code coder
;
5518 tree rname
= NULL_TREE
;
5519 bool objc_ok
= false;
5521 if (errtype
== ic_argpass
)
5524 /* Change pointer to function to the function itself for
5526 if (TREE_CODE (function
) == ADDR_EXPR
5527 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
5528 function
= TREE_OPERAND (function
, 0);
5530 /* Handle an ObjC selector specially for diagnostics. */
5531 selector
= objc_message_selector ();
5533 if (selector
&& parmnum
> 2)
5540 /* This macro is used to emit diagnostics to ensure that all format
5541 strings are complete sentences, visible to gettext and checked at
5543 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5548 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5549 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5550 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5551 "expected %qT but argument is of type %qT", \
5555 pedwarn (LOCATION, OPT, AS); \
5558 pedwarn_init (LOCATION, OPT, IN); \
5561 pedwarn (LOCATION, OPT, RE); \
5564 gcc_unreachable (); \
5568 /* This macro is used to emit diagnostics to ensure that all format
5569 strings are complete sentences, visible to gettext and checked at
5570 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5571 extra parameter to enumerate qualifiers. */
5573 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5578 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5579 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5580 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5581 "expected %qT but argument is of type %qT", \
5585 pedwarn (LOCATION, OPT, AS, QUALS); \
5588 pedwarn (LOCATION, OPT, IN, QUALS); \
5591 pedwarn (LOCATION, OPT, RE, QUALS); \
5594 gcc_unreachable (); \
5598 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5599 rhs
= TREE_OPERAND (rhs
, 0);
5601 rhstype
= TREE_TYPE (rhs
);
5602 coder
= TREE_CODE (rhstype
);
5604 if (coder
== ERROR_MARK
)
5605 return error_mark_node
;
5607 if (c_dialect_objc ())
5630 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
5633 if (warn_cxx_compat
)
5635 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
5636 if (checktype
!= error_mark_node
5637 && TREE_CODE (type
) == ENUMERAL_TYPE
5638 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
5640 WARN_FOR_ASSIGNMENT (input_location
, OPT_Wc___compat
,
5641 G_("enum conversion when passing argument "
5642 "%d of %qE is invalid in C++"),
5643 G_("enum conversion in assignment is "
5645 G_("enum conversion in initialization is "
5647 G_("enum conversion in return is "
5652 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
5655 if (coder
== VOID_TYPE
)
5657 /* Except for passing an argument to an unprototyped function,
5658 this is a constraint violation. When passing an argument to
5659 an unprototyped function, it is compile-time undefined;
5660 making it a constraint in that case was rejected in
5662 error_at (location
, "void value not ignored as it ought to be");
5663 return error_mark_node
;
5665 rhs
= require_complete_type (rhs
);
5666 if (rhs
== error_mark_node
)
5667 return error_mark_node
;
5668 /* A non-reference type can convert to a reference. This handles
5669 va_start, va_copy and possibly port built-ins. */
5670 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
5672 if (!lvalue_p (rhs
))
5674 error_at (location
, "cannot pass rvalue to reference parameter");
5675 return error_mark_node
;
5677 if (!c_mark_addressable (rhs
))
5678 return error_mark_node
;
5679 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
5680 SET_EXPR_LOCATION (rhs
, location
);
5682 rhs
= convert_for_assignment (location
, build_pointer_type (TREE_TYPE (type
)),
5683 rhs
, origtype
, errtype
, null_pointer_constant
,
5684 fundecl
, function
, parmnum
);
5685 if (rhs
== error_mark_node
)
5686 return error_mark_node
;
5688 rhs
= build1 (NOP_EXPR
, type
, rhs
);
5689 SET_EXPR_LOCATION (rhs
, location
);
5692 /* Some types can interconvert without explicit casts. */
5693 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
5694 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
5695 return convert (type
, rhs
);
5696 /* Arithmetic types all interconvert, and enum is treated like int. */
5697 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
5698 || codel
== FIXED_POINT_TYPE
5699 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
5700 || codel
== BOOLEAN_TYPE
)
5701 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
5702 || coder
== FIXED_POINT_TYPE
5703 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
5704 || coder
== BOOLEAN_TYPE
))
5707 bool save
= in_late_binary_op
;
5708 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5709 in_late_binary_op
= true;
5710 ret
= convert_and_check (type
, orig_rhs
);
5711 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5712 in_late_binary_op
= save
;
5716 /* Aggregates in different TUs might need conversion. */
5717 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
5719 && comptypes (type
, rhstype
))
5720 return convert_and_check (type
, rhs
);
5722 /* Conversion to a transparent union or record from its member types.
5723 This applies only to function arguments. */
5724 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
5725 && TYPE_TRANSPARENT_AGGR (type
))
5726 && errtype
== ic_argpass
)
5728 tree memb
, marginal_memb
= NULL_TREE
;
5730 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
5732 tree memb_type
= TREE_TYPE (memb
);
5734 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
5735 TYPE_MAIN_VARIANT (rhstype
)))
5738 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
5741 if (coder
== POINTER_TYPE
)
5743 tree ttl
= TREE_TYPE (memb_type
);
5744 tree ttr
= TREE_TYPE (rhstype
);
5746 /* Any non-function converts to a [const][volatile] void *
5747 and vice versa; otherwise, targets must be the same.
5748 Meanwhile, the lhs target must have all the qualifiers of
5750 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5751 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5752 || comp_target_types (location
, memb_type
, rhstype
))
5754 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
5755 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
5756 /* If this type won't generate any warnings, use it. */
5757 if (lquals
== rquals
5758 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
5759 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5760 ? ((lquals
| rquals
) == rquals
)
5761 : ((lquals
| rquals
) == lquals
)))
5764 /* Keep looking for a better type, but remember this one. */
5766 marginal_memb
= memb
;
5770 /* Can convert integer zero to any pointer type. */
5771 if (null_pointer_constant
)
5773 rhs
= null_pointer_node
;
5778 if (memb
|| marginal_memb
)
5782 /* We have only a marginally acceptable member type;
5783 it needs a warning. */
5784 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
5785 tree ttr
= TREE_TYPE (rhstype
);
5787 /* Const and volatile mean something different for function
5788 types, so the usual warnings are not appropriate. */
5789 if (TREE_CODE (ttr
) == FUNCTION_TYPE
5790 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5792 /* Because const and volatile on functions are
5793 restrictions that say the function will not do
5794 certain things, it is okay to use a const or volatile
5795 function where an ordinary one is wanted, but not
5797 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
5798 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
5799 WARN_FOR_QUALIFIERS (location
, 0,
5800 G_("passing argument %d of %qE "
5801 "makes %q#v qualified function "
5802 "pointer from unqualified"),
5803 G_("assignment makes %q#v qualified "
5804 "function pointer from "
5806 G_("initialization makes %q#v qualified "
5807 "function pointer from "
5809 G_("return makes %q#v qualified function "
5810 "pointer from unqualified"),
5811 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
5813 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
5814 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
5815 WARN_FOR_QUALIFIERS (location
, 0,
5816 G_("passing argument %d of %qE discards "
5817 "%qv qualifier from pointer target type"),
5818 G_("assignment discards %qv qualifier "
5819 "from pointer target type"),
5820 G_("initialization discards %qv qualifier "
5821 "from pointer target type"),
5822 G_("return discards %qv qualifier from "
5823 "pointer target type"),
5824 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
5826 memb
= marginal_memb
;
5829 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
5830 pedwarn (location
, OPT_Wpedantic
,
5831 "ISO C prohibits argument conversion to union type");
5833 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
5834 return build_constructor_single (type
, memb
, rhs
);
5838 /* Conversions among pointers */
5839 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
5840 && (coder
== codel
))
5842 tree ttl
= TREE_TYPE (type
);
5843 tree ttr
= TREE_TYPE (rhstype
);
5846 bool is_opaque_pointer
;
5847 int target_cmp
= 0; /* Cache comp_target_types () result. */
5851 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
5852 mvl
= (TYPE_ATOMIC (mvl
)
5853 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
5855 : TYPE_MAIN_VARIANT (mvl
));
5856 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
5857 mvr
= (TYPE_ATOMIC (mvr
)
5858 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
5860 : TYPE_MAIN_VARIANT (mvr
));
5861 /* Opaque pointers are treated like void pointers. */
5862 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
5864 /* The Plan 9 compiler permits a pointer to a struct to be
5865 automatically converted into a pointer to an anonymous field
5866 within the struct. */
5867 if (flag_plan9_extensions
5868 && (TREE_CODE (mvl
) == RECORD_TYPE
|| TREE_CODE(mvl
) == UNION_TYPE
)
5869 && (TREE_CODE (mvr
) == RECORD_TYPE
|| TREE_CODE(mvr
) == UNION_TYPE
)
5872 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
5873 if (new_rhs
!= NULL_TREE
)
5876 rhstype
= TREE_TYPE (rhs
);
5877 coder
= TREE_CODE (rhstype
);
5878 ttr
= TREE_TYPE (rhstype
);
5879 mvr
= TYPE_MAIN_VARIANT (ttr
);
5883 /* C++ does not allow the implicit conversion void* -> T*. However,
5884 for the purpose of reducing the number of false positives, we
5885 tolerate the special case of
5889 where NULL is typically defined in C to be '(void *) 0'. */
5890 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
5891 warning_at (location
, OPT_Wc___compat
,
5892 "request for implicit conversion "
5893 "from %qT to %qT not permitted in C++", rhstype
, type
);
5895 /* See if the pointers point to incompatible address spaces. */
5896 asl
= TYPE_ADDR_SPACE (ttl
);
5897 asr
= TYPE_ADDR_SPACE (ttr
);
5898 if (!null_pointer_constant_p (rhs
)
5899 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
5904 error_at (location
, "passing argument %d of %qE from pointer to "
5905 "non-enclosed address space", parmnum
, rname
);
5908 error_at (location
, "assignment from pointer to "
5909 "non-enclosed address space");
5912 error_at (location
, "initialization from pointer to "
5913 "non-enclosed address space");
5916 error_at (location
, "return from pointer to "
5917 "non-enclosed address space");
5922 return error_mark_node
;
5925 /* Check if the right-hand side has a format attribute but the
5926 left-hand side doesn't. */
5927 if (warn_suggest_attribute_format
5928 && check_missing_format_attribute (type
, rhstype
))
5933 warning_at (location
, OPT_Wsuggest_attribute_format
,
5934 "argument %d of %qE might be "
5935 "a candidate for a format attribute",
5939 warning_at (location
, OPT_Wsuggest_attribute_format
,
5940 "assignment left-hand side might be "
5941 "a candidate for a format attribute");
5944 warning_at (location
, OPT_Wsuggest_attribute_format
,
5945 "initialization left-hand side might be "
5946 "a candidate for a format attribute");
5949 warning_at (location
, OPT_Wsuggest_attribute_format
,
5950 "return type might be "
5951 "a candidate for a format attribute");
5958 /* Any non-function converts to a [const][volatile] void *
5959 and vice versa; otherwise, targets must be the same.
5960 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5961 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5962 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5963 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
5964 || is_opaque_pointer
5965 || ((c_common_unsigned_type (mvl
)
5966 == c_common_unsigned_type (mvr
))
5967 && (c_common_signed_type (mvl
)
5968 == c_common_signed_type (mvr
))
5969 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
5972 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
5975 && !null_pointer_constant
5976 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
5977 WARN_FOR_ASSIGNMENT (location
, OPT_Wpedantic
,
5978 G_("ISO C forbids passing argument %d of "
5979 "%qE between function pointer "
5981 G_("ISO C forbids assignment between "
5982 "function pointer and %<void *%>"),
5983 G_("ISO C forbids initialization between "
5984 "function pointer and %<void *%>"),
5985 G_("ISO C forbids return between function "
5986 "pointer and %<void *%>"));
5987 /* Const and volatile mean something different for function types,
5988 so the usual warnings are not appropriate. */
5989 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
5990 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
5992 /* Assignments between atomic and non-atomic objects are OK. */
5993 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
5994 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
5996 WARN_FOR_QUALIFIERS (location
, 0,
5997 G_("passing argument %d of %qE discards "
5998 "%qv qualifier from pointer target type"),
5999 G_("assignment discards %qv qualifier "
6000 "from pointer target type"),
6001 G_("initialization discards %qv qualifier "
6002 "from pointer target type"),
6003 G_("return discards %qv qualifier from "
6004 "pointer target type"),
6005 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6007 /* If this is not a case of ignoring a mismatch in signedness,
6009 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6012 /* If there is a mismatch, do warn. */
6013 else if (warn_pointer_sign
)
6014 WARN_FOR_ASSIGNMENT (location
, OPT_Wpointer_sign
,
6015 G_("pointer targets in passing argument "
6016 "%d of %qE differ in signedness"),
6017 G_("pointer targets in assignment "
6018 "differ in signedness"),
6019 G_("pointer targets in initialization "
6020 "differ in signedness"),
6021 G_("pointer targets in return differ "
6024 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6025 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6027 /* Because const and volatile on functions are restrictions
6028 that say the function will not do certain things,
6029 it is okay to use a const or volatile function
6030 where an ordinary one is wanted, but not vice-versa. */
6031 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6032 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6033 WARN_FOR_QUALIFIERS (location
, 0,
6034 G_("passing argument %d of %qE makes "
6035 "%q#v qualified function pointer "
6036 "from unqualified"),
6037 G_("assignment makes %q#v qualified function "
6038 "pointer from unqualified"),
6039 G_("initialization makes %q#v qualified "
6040 "function pointer from unqualified"),
6041 G_("return makes %q#v qualified function "
6042 "pointer from unqualified"),
6043 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6047 /* Avoid warning about the volatile ObjC EH puts on decls. */
6049 WARN_FOR_ASSIGNMENT (location
, 0,
6050 G_("passing argument %d of %qE from "
6051 "incompatible pointer type"),
6052 G_("assignment from incompatible pointer type"),
6053 G_("initialization from incompatible "
6055 G_("return from incompatible pointer type"));
6057 return convert (type
, rhs
);
6059 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6061 /* ??? This should not be an error when inlining calls to
6062 unprototyped functions. */
6063 error_at (location
, "invalid use of non-lvalue array");
6064 return error_mark_node
;
6066 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6068 /* An explicit constant 0 can convert to a pointer,
6069 or one that results from arithmetic, even including
6070 a cast to integer type. */
6071 if (!null_pointer_constant
)
6072 WARN_FOR_ASSIGNMENT (location
, 0,
6073 G_("passing argument %d of %qE makes "
6074 "pointer from integer without a cast"),
6075 G_("assignment makes pointer from integer "
6077 G_("initialization makes pointer from "
6078 "integer without a cast"),
6079 G_("return makes pointer from integer "
6082 return convert (type
, rhs
);
6084 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6086 WARN_FOR_ASSIGNMENT (location
, 0,
6087 G_("passing argument %d of %qE makes integer "
6088 "from pointer without a cast"),
6089 G_("assignment makes integer from pointer "
6091 G_("initialization makes integer from pointer "
6093 G_("return makes integer from pointer "
6095 return convert (type
, rhs
);
6097 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6100 bool save
= in_late_binary_op
;
6101 in_late_binary_op
= true;
6102 ret
= convert (type
, rhs
);
6103 in_late_binary_op
= save
;
6110 error_at (location
, "incompatible type for argument %d of %qE", parmnum
, rname
);
6111 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6112 ? DECL_SOURCE_LOCATION (fundecl
) : input_location
,
6113 "expected %qT but argument is of type %qT", type
, rhstype
);
6116 error_at (location
, "incompatible types when assigning to type %qT from "
6117 "type %qT", type
, rhstype
);
6121 "incompatible types when initializing type %qT using type %qT",
6126 "incompatible types when returning type %qT but %qT was "
6127 "expected", rhstype
, type
);
6133 return error_mark_node
;
6136 /* If VALUE is a compound expr all of whose expressions are constant, then
6137 return its value. Otherwise, return error_mark_node.
6139 This is for handling COMPOUND_EXPRs as initializer elements
6140 which is allowed with a warning when -pedantic is specified. */
6143 valid_compound_expr_initializer (tree value
, tree endtype
)
6145 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6147 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6149 return error_mark_node
;
6150 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6153 else if (!initializer_constant_valid_p (value
, endtype
))
6154 return error_mark_node
;
6159 /* Perform appropriate conversions on the initial value of a variable,
6160 store it in the declaration DECL,
6161 and print any error messages that are appropriate.
6162 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6163 If the init is invalid, store an ERROR_MARK.
6165 INIT_LOC is the location of the initial value. */
6168 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6173 /* If variable's type was invalidly declared, just ignore it. */
6175 type
= TREE_TYPE (decl
);
6176 if (TREE_CODE (type
) == ERROR_MARK
)
6179 /* Digest the specified initializer into an expression. */
6182 npc
= null_pointer_constant_p (init
);
6183 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6184 true, TREE_STATIC (decl
));
6186 /* Store the expression if valid; else report error. */
6188 if (!in_system_header
6189 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6190 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6191 "aggregate initialization");
6193 DECL_INITIAL (decl
) = value
;
6195 /* ANSI wants warnings about out-of-range constant initializers. */
6196 STRIP_TYPE_NOPS (value
);
6197 if (TREE_STATIC (decl
))
6198 constant_expression_warning (value
);
6200 /* Check if we need to set array size from compound literal size. */
6201 if (TREE_CODE (type
) == ARRAY_TYPE
6202 && TYPE_DOMAIN (type
) == 0
6203 && value
!= error_mark_node
)
6205 tree inside_init
= init
;
6207 STRIP_TYPE_NOPS (inside_init
);
6208 inside_init
= fold (inside_init
);
6210 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6212 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6214 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6216 /* For int foo[] = (int [3]){1}; we need to set array size
6217 now since later on array initializer will be just the
6218 brace enclosed list of the compound literal. */
6219 tree etype
= strip_array_types (TREE_TYPE (decl
));
6220 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6221 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6223 layout_decl (cldecl
, 0);
6225 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6231 /* Methods for storing and printing names for error messages. */
6233 /* Implement a spelling stack that allows components of a name to be pushed
6234 and popped. Each element on the stack is this structure. */
6241 unsigned HOST_WIDE_INT i
;
6246 #define SPELLING_STRING 1
6247 #define SPELLING_MEMBER 2
6248 #define SPELLING_BOUNDS 3
6250 static struct spelling
*spelling
; /* Next stack element (unused). */
6251 static struct spelling
*spelling_base
; /* Spelling stack base. */
6252 static int spelling_size
; /* Size of the spelling stack. */
6254 /* Macros to save and restore the spelling stack around push_... functions.
6255 Alternative to SAVE_SPELLING_STACK. */
6257 #define SPELLING_DEPTH() (spelling - spelling_base)
6258 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6260 /* Push an element on the spelling stack with type KIND and assign VALUE
6263 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6265 int depth = SPELLING_DEPTH (); \
6267 if (depth >= spelling_size) \
6269 spelling_size += 10; \
6270 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6272 RESTORE_SPELLING_DEPTH (depth); \
6275 spelling->kind = (KIND); \
6276 spelling->MEMBER = (VALUE); \
6280 /* Push STRING on the stack. Printed literally. */
6283 push_string (const char *string
)
6285 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6288 /* Push a member name on the stack. Printed as '.' STRING. */
6291 push_member_name (tree decl
)
6293 const char *const string
6295 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6296 : _("<anonymous>"));
6297 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6300 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6303 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6305 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6308 /* Compute the maximum size in bytes of the printed spelling. */
6311 spelling_length (void)
6316 for (p
= spelling_base
; p
< spelling
; p
++)
6318 if (p
->kind
== SPELLING_BOUNDS
)
6321 size
+= strlen (p
->u
.s
) + 1;
6327 /* Print the spelling to BUFFER and return it. */
6330 print_spelling (char *buffer
)
6335 for (p
= spelling_base
; p
< spelling
; p
++)
6336 if (p
->kind
== SPELLING_BOUNDS
)
6338 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6344 if (p
->kind
== SPELLING_MEMBER
)
6346 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
6353 /* Issue an error message for a bad initializer component.
6354 GMSGID identifies the message.
6355 The component name is taken from the spelling stack. */
6358 error_init (const char *gmsgid
)
6362 /* The gmsgid may be a format string with %< and %>. */
6364 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6366 error ("(near initialization for %qs)", ofwhat
);
6369 /* Issue a pedantic warning for a bad initializer component. OPT is
6370 the option OPT_* (from options.h) controlling this warning or 0 if
6371 it is unconditionally given. GMSGID identifies the message. The
6372 component name is taken from the spelling stack. */
6375 pedwarn_init (location_t location
, int opt
, const char *gmsgid
)
6379 /* The gmsgid may be a format string with %< and %>. */
6380 pedwarn (location
, opt
, gmsgid
);
6381 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6383 pedwarn (location
, opt
, "(near initialization for %qs)", ofwhat
);
6386 /* Issue a warning for a bad initializer component.
6388 OPT is the OPT_W* value corresponding to the warning option that
6389 controls this warning. GMSGID identifies the message. The
6390 component name is taken from the spelling stack. */
6393 warning_init (int opt
, const char *gmsgid
)
6397 /* The gmsgid may be a format string with %< and %>. */
6398 warning (opt
, gmsgid
);
6399 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6401 warning (opt
, "(near initialization for %qs)", ofwhat
);
6404 /* If TYPE is an array type and EXPR is a parenthesized string
6405 constant, warn if pedantic that EXPR is being used to initialize an
6406 object of type TYPE. */
6409 maybe_warn_string_init (tree type
, struct c_expr expr
)
6412 && TREE_CODE (type
) == ARRAY_TYPE
6413 && TREE_CODE (expr
.value
) == STRING_CST
6414 && expr
.original_code
!= STRING_CST
)
6415 pedwarn_init (input_location
, OPT_Wpedantic
,
6416 "array initialized from parenthesized string constant");
6419 /* Digest the parser output INIT as an initializer for type TYPE.
6420 Return a C expression of type TYPE to represent the initial value.
6422 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6424 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6426 If INIT is a string constant, STRICT_STRING is true if it is
6427 unparenthesized or we should not warn here for it being parenthesized.
6428 For other types of INIT, STRICT_STRING is not used.
6430 INIT_LOC is the location of the INIT.
6432 REQUIRE_CONSTANT requests an error if non-constant initializers or
6433 elements are seen. */
6436 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
6437 bool null_pointer_constant
, bool strict_string
,
6438 int require_constant
)
6440 enum tree_code code
= TREE_CODE (type
);
6441 tree inside_init
= init
;
6442 tree semantic_type
= NULL_TREE
;
6443 bool maybe_const
= true;
6445 if (type
== error_mark_node
6447 || init
== error_mark_node
6448 || TREE_TYPE (init
) == error_mark_node
)
6449 return error_mark_node
;
6451 STRIP_TYPE_NOPS (inside_init
);
6453 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
6455 semantic_type
= TREE_TYPE (inside_init
);
6456 inside_init
= TREE_OPERAND (inside_init
, 0);
6458 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
6459 inside_init
= decl_constant_value_for_optimization (inside_init
);
6461 /* Initialization of an array of chars from a string constant
6462 optionally enclosed in braces. */
6464 if (code
== ARRAY_TYPE
&& inside_init
6465 && TREE_CODE (inside_init
) == STRING_CST
)
6468 = (TYPE_ATOMIC (TREE_TYPE (type
))
6469 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
6471 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6472 /* Note that an array could be both an array of character type
6473 and an array of wchar_t if wchar_t is signed char or unsigned
6475 bool char_array
= (typ1
== char_type_node
6476 || typ1
== signed_char_type_node
6477 || typ1
== unsigned_char_type_node
);
6478 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
6479 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
6480 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
6482 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
6485 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
6486 expr
.value
= inside_init
;
6487 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
6488 expr
.original_type
= NULL
;
6489 maybe_warn_string_init (type
, expr
);
6491 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
6492 pedwarn_init (init_loc
, OPT_Wpedantic
,
6493 "initialization of a flexible array member");
6495 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6496 TYPE_MAIN_VARIANT (type
)))
6501 if (typ2
!= char_type_node
)
6503 error_init ("char-array initialized from wide string");
6504 return error_mark_node
;
6509 if (typ2
== char_type_node
)
6511 error_init ("wide character array initialized from non-wide "
6513 return error_mark_node
;
6515 else if (!comptypes(typ1
, typ2
))
6517 error_init ("wide character array initialized from "
6518 "incompatible wide string");
6519 return error_mark_node
;
6523 TREE_TYPE (inside_init
) = type
;
6524 if (TYPE_DOMAIN (type
) != 0
6525 && TYPE_SIZE (type
) != 0
6526 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
6528 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
6530 /* Subtract the size of a single (possibly wide) character
6531 because it's ok to ignore the terminating null char
6532 that is counted in the length of the constant. */
6533 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
6535 - (TYPE_PRECISION (typ1
)
6537 pedwarn_init (init_loc
, 0,
6538 ("initializer-string for array of chars "
6540 else if (warn_cxx_compat
6541 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
6542 warning_at (init_loc
, OPT_Wc___compat
,
6543 ("initializer-string for array chars "
6544 "is too long for C++"));
6549 else if (INTEGRAL_TYPE_P (typ1
))
6551 error_init ("array of inappropriate type initialized "
6552 "from string constant");
6553 return error_mark_node
;
6557 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6558 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6559 below and handle as a constructor. */
6560 if (code
== VECTOR_TYPE
6561 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
6562 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
6563 && TREE_CONSTANT (inside_init
))
6565 if (TREE_CODE (inside_init
) == VECTOR_CST
6566 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6567 TYPE_MAIN_VARIANT (type
)))
6570 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
6572 unsigned HOST_WIDE_INT ix
;
6574 bool constant_p
= true;
6576 /* Iterate through elements and check if all constructor
6577 elements are *_CSTs. */
6578 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
6579 if (!CONSTANT_CLASS_P (value
))
6586 return build_vector_from_ctor (type
,
6587 CONSTRUCTOR_ELTS (inside_init
));
6591 if (warn_sequence_point
)
6592 verify_sequence_points (inside_init
);
6594 /* Any type can be initialized
6595 from an expression of the same type, optionally with braces. */
6597 if (inside_init
&& TREE_TYPE (inside_init
) != 0
6598 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6599 TYPE_MAIN_VARIANT (type
))
6600 || (code
== ARRAY_TYPE
6601 && comptypes (TREE_TYPE (inside_init
), type
))
6602 || (code
== VECTOR_TYPE
6603 && comptypes (TREE_TYPE (inside_init
), type
))
6604 || (code
== POINTER_TYPE
6605 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
6606 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
6607 TREE_TYPE (type
)))))
6609 if (code
== POINTER_TYPE
)
6611 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
6613 if (TREE_CODE (inside_init
) == STRING_CST
6614 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6615 inside_init
= array_to_pointer_conversion
6616 (init_loc
, inside_init
);
6619 error_init ("invalid use of non-lvalue array");
6620 return error_mark_node
;
6625 if (code
== VECTOR_TYPE
)
6626 /* Although the types are compatible, we may require a
6628 inside_init
= convert (type
, inside_init
);
6630 if (require_constant
6631 && (code
== VECTOR_TYPE
|| !flag_isoc99
)
6632 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6634 /* As an extension, allow initializing objects with static storage
6635 duration with compound literals (which are then treated just as
6636 the brace enclosed list they contain). Also allow this for
6637 vectors, as we can only assign them with compound literals. */
6638 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6639 inside_init
= DECL_INITIAL (decl
);
6642 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
6643 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
6645 error_init ("array initialized from non-constant array expression");
6646 return error_mark_node
;
6649 /* Compound expressions can only occur here if -Wpedantic or
6650 -pedantic-errors is specified. In the later case, we always want
6651 an error. In the former case, we simply want a warning. */
6652 if (require_constant
&& pedantic
6653 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
6656 = valid_compound_expr_initializer (inside_init
,
6657 TREE_TYPE (inside_init
));
6658 if (inside_init
== error_mark_node
)
6659 error_init ("initializer element is not constant");
6661 pedwarn_init (init_loc
, OPT_Wpedantic
,
6662 "initializer element is not constant");
6663 if (flag_pedantic_errors
)
6664 inside_init
= error_mark_node
;
6666 else if (require_constant
6667 && !initializer_constant_valid_p (inside_init
,
6668 TREE_TYPE (inside_init
)))
6670 error_init ("initializer element is not constant");
6671 inside_init
= error_mark_node
;
6673 else if (require_constant
&& !maybe_const
)
6674 pedwarn_init (init_loc
, 0,
6675 "initializer element is not a constant expression");
6677 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6678 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
6679 inside_init
= convert_for_assignment (init_loc
, type
, inside_init
,
6681 ic_init
, null_pointer_constant
,
6682 NULL_TREE
, NULL_TREE
, 0);
6686 /* Handle scalar types, including conversions. */
6688 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
6689 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
6690 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
6692 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
6693 && (TREE_CODE (init
) == STRING_CST
6694 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
6695 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
6697 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
6700 = convert_for_assignment (init_loc
, type
, inside_init
, origtype
,
6701 ic_init
, null_pointer_constant
,
6702 NULL_TREE
, NULL_TREE
, 0);
6704 /* Check to see if we have already given an error message. */
6705 if (inside_init
== error_mark_node
)
6707 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
6709 error_init ("initializer element is not constant");
6710 inside_init
= error_mark_node
;
6712 else if (require_constant
6713 && !initializer_constant_valid_p (inside_init
,
6714 TREE_TYPE (inside_init
)))
6716 error_init ("initializer element is not computable at load time");
6717 inside_init
= error_mark_node
;
6719 else if (require_constant
&& !maybe_const
)
6720 pedwarn_init (init_loc
, 0,
6721 "initializer element is not a constant expression");
6726 /* Come here only for records and arrays. */
6728 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
6730 error_init ("variable-sized object may not be initialized");
6731 return error_mark_node
;
6734 error_init ("invalid initializer");
6735 return error_mark_node
;
6738 /* Handle initializers that use braces. */
6740 /* Type of object we are accumulating a constructor for.
6741 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6742 static tree constructor_type
;
6744 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6746 static tree constructor_fields
;
6748 /* For an ARRAY_TYPE, this is the specified index
6749 at which to store the next element we get. */
6750 static tree constructor_index
;
6752 /* For an ARRAY_TYPE, this is the maximum index. */
6753 static tree constructor_max_index
;
6755 /* For a RECORD_TYPE, this is the first field not yet written out. */
6756 static tree constructor_unfilled_fields
;
6758 /* For an ARRAY_TYPE, this is the index of the first element
6759 not yet written out. */
6760 static tree constructor_unfilled_index
;
6762 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6763 This is so we can generate gaps between fields, when appropriate. */
6764 static tree constructor_bit_index
;
6766 /* If we are saving up the elements rather than allocating them,
6767 this is the list of elements so far (in reverse order,
6768 most recent first). */
6769 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
6771 /* 1 if constructor should be incrementally stored into a constructor chain,
6772 0 if all the elements should be kept in AVL tree. */
6773 static int constructor_incremental
;
6775 /* 1 if so far this constructor's elements are all compile-time constants. */
6776 static int constructor_constant
;
6778 /* 1 if so far this constructor's elements are all valid address constants. */
6779 static int constructor_simple
;
6781 /* 1 if this constructor has an element that cannot be part of a
6782 constant expression. */
6783 static int constructor_nonconst
;
6785 /* 1 if this constructor is erroneous so far. */
6786 static int constructor_erroneous
;
6788 /* Structure for managing pending initializer elements, organized as an
6793 struct init_node
*left
, *right
;
6794 struct init_node
*parent
;
6801 /* Tree of pending elements at this constructor level.
6802 These are elements encountered out of order
6803 which belong at places we haven't reached yet in actually
6805 Will never hold tree nodes across GC runs. */
6806 static struct init_node
*constructor_pending_elts
;
6808 /* The SPELLING_DEPTH of this constructor. */
6809 static int constructor_depth
;
6811 /* DECL node for which an initializer is being read.
6812 0 means we are reading a constructor expression
6813 such as (struct foo) {...}. */
6814 static tree constructor_decl
;
6816 /* Nonzero if this is an initializer for a top-level decl. */
6817 static int constructor_top_level
;
6819 /* Nonzero if there were any member designators in this initializer. */
6820 static int constructor_designated
;
6822 /* Nesting depth of designator list. */
6823 static int designator_depth
;
6825 /* Nonzero if there were diagnosed errors in this designator list. */
6826 static int designator_erroneous
;
6829 /* This stack has a level for each implicit or explicit level of
6830 structuring in the initializer, including the outermost one. It
6831 saves the values of most of the variables above. */
6833 struct constructor_range_stack
;
6835 struct constructor_stack
6837 struct constructor_stack
*next
;
6842 tree unfilled_index
;
6843 tree unfilled_fields
;
6845 vec
<constructor_elt
, va_gc
> *elements
;
6846 struct init_node
*pending_elts
;
6849 /* If value nonzero, this value should replace the entire
6850 constructor at this level. */
6851 struct c_expr replacement_value
;
6852 struct constructor_range_stack
*range_stack
;
6863 static struct constructor_stack
*constructor_stack
;
6865 /* This stack represents designators from some range designator up to
6866 the last designator in the list. */
6868 struct constructor_range_stack
6870 struct constructor_range_stack
*next
, *prev
;
6871 struct constructor_stack
*stack
;
6878 static struct constructor_range_stack
*constructor_range_stack
;
6880 /* This stack records separate initializers that are nested.
6881 Nested initializers can't happen in ANSI C, but GNU C allows them
6882 in cases like { ... (struct foo) { ... } ... }. */
6884 struct initializer_stack
6886 struct initializer_stack
*next
;
6888 struct constructor_stack
*constructor_stack
;
6889 struct constructor_range_stack
*constructor_range_stack
;
6890 vec
<constructor_elt
, va_gc
> *elements
;
6891 struct spelling
*spelling
;
6892 struct spelling
*spelling_base
;
6895 char require_constant_value
;
6896 char require_constant_elements
;
6899 static struct initializer_stack
*initializer_stack
;
6901 /* Prepare to parse and output the initializer for variable DECL. */
6904 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
6907 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
6909 p
->decl
= constructor_decl
;
6910 p
->require_constant_value
= require_constant_value
;
6911 p
->require_constant_elements
= require_constant_elements
;
6912 p
->constructor_stack
= constructor_stack
;
6913 p
->constructor_range_stack
= constructor_range_stack
;
6914 p
->elements
= constructor_elements
;
6915 p
->spelling
= spelling
;
6916 p
->spelling_base
= spelling_base
;
6917 p
->spelling_size
= spelling_size
;
6918 p
->top_level
= constructor_top_level
;
6919 p
->next
= initializer_stack
;
6920 initializer_stack
= p
;
6922 constructor_decl
= decl
;
6923 constructor_designated
= 0;
6924 constructor_top_level
= top_level
;
6926 if (decl
!= 0 && decl
!= error_mark_node
)
6928 require_constant_value
= TREE_STATIC (decl
);
6929 require_constant_elements
6930 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
6931 /* For a scalar, you can always use any value to initialize,
6932 even within braces. */
6933 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
6934 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
6935 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
6936 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
6937 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
6941 require_constant_value
= 0;
6942 require_constant_elements
= 0;
6943 locus
= _("(anonymous)");
6946 constructor_stack
= 0;
6947 constructor_range_stack
= 0;
6949 missing_braces_mentioned
= 0;
6953 RESTORE_SPELLING_DEPTH (0);
6956 push_string (locus
);
6962 struct initializer_stack
*p
= initializer_stack
;
6964 /* Free the whole constructor stack of this initializer. */
6965 while (constructor_stack
)
6967 struct constructor_stack
*q
= constructor_stack
;
6968 constructor_stack
= q
->next
;
6972 gcc_assert (!constructor_range_stack
);
6974 /* Pop back to the data of the outer initializer (if any). */
6975 free (spelling_base
);
6977 constructor_decl
= p
->decl
;
6978 require_constant_value
= p
->require_constant_value
;
6979 require_constant_elements
= p
->require_constant_elements
;
6980 constructor_stack
= p
->constructor_stack
;
6981 constructor_range_stack
= p
->constructor_range_stack
;
6982 constructor_elements
= p
->elements
;
6983 spelling
= p
->spelling
;
6984 spelling_base
= p
->spelling_base
;
6985 spelling_size
= p
->spelling_size
;
6986 constructor_top_level
= p
->top_level
;
6987 initializer_stack
= p
->next
;
6991 /* Call here when we see the initializer is surrounded by braces.
6992 This is instead of a call to push_init_level;
6993 it is matched by a call to pop_init_level.
6995 TYPE is the type to initialize, for a constructor expression.
6996 For an initializer for a decl, TYPE is zero. */
6999 really_start_incremental_init (tree type
)
7001 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7004 type
= TREE_TYPE (constructor_decl
);
7006 if (TREE_CODE (type
) == VECTOR_TYPE
7007 && TYPE_VECTOR_OPAQUE (type
))
7008 error ("opaque vector types cannot be initialized");
7010 p
->type
= constructor_type
;
7011 p
->fields
= constructor_fields
;
7012 p
->index
= constructor_index
;
7013 p
->max_index
= constructor_max_index
;
7014 p
->unfilled_index
= constructor_unfilled_index
;
7015 p
->unfilled_fields
= constructor_unfilled_fields
;
7016 p
->bit_index
= constructor_bit_index
;
7017 p
->elements
= constructor_elements
;
7018 p
->constant
= constructor_constant
;
7019 p
->simple
= constructor_simple
;
7020 p
->nonconst
= constructor_nonconst
;
7021 p
->erroneous
= constructor_erroneous
;
7022 p
->pending_elts
= constructor_pending_elts
;
7023 p
->depth
= constructor_depth
;
7024 p
->replacement_value
.value
= 0;
7025 p
->replacement_value
.original_code
= ERROR_MARK
;
7026 p
->replacement_value
.original_type
= NULL
;
7030 p
->incremental
= constructor_incremental
;
7031 p
->designated
= constructor_designated
;
7033 constructor_stack
= p
;
7035 constructor_constant
= 1;
7036 constructor_simple
= 1;
7037 constructor_nonconst
= 0;
7038 constructor_depth
= SPELLING_DEPTH ();
7039 constructor_elements
= NULL
;
7040 constructor_pending_elts
= 0;
7041 constructor_type
= type
;
7042 constructor_incremental
= 1;
7043 constructor_designated
= 0;
7044 designator_depth
= 0;
7045 designator_erroneous
= 0;
7047 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7048 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7050 constructor_fields
= TYPE_FIELDS (constructor_type
);
7051 /* Skip any nameless bit fields at the beginning. */
7052 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7053 && DECL_NAME (constructor_fields
) == 0)
7054 constructor_fields
= DECL_CHAIN (constructor_fields
);
7056 constructor_unfilled_fields
= constructor_fields
;
7057 constructor_bit_index
= bitsize_zero_node
;
7059 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7061 if (TYPE_DOMAIN (constructor_type
))
7063 constructor_max_index
7064 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7066 /* Detect non-empty initializations of zero-length arrays. */
7067 if (constructor_max_index
== NULL_TREE
7068 && TYPE_SIZE (constructor_type
))
7069 constructor_max_index
= integer_minus_one_node
;
7071 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7072 to initialize VLAs will cause a proper error; avoid tree
7073 checking errors as well by setting a safe value. */
7074 if (constructor_max_index
7075 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7076 constructor_max_index
= integer_minus_one_node
;
7079 = convert (bitsizetype
,
7080 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7084 constructor_index
= bitsize_zero_node
;
7085 constructor_max_index
= NULL_TREE
;
7088 constructor_unfilled_index
= constructor_index
;
7090 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7092 /* Vectors are like simple fixed-size arrays. */
7093 constructor_max_index
=
7094 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7095 constructor_index
= bitsize_zero_node
;
7096 constructor_unfilled_index
= constructor_index
;
7100 /* Handle the case of int x = {5}; */
7101 constructor_fields
= constructor_type
;
7102 constructor_unfilled_fields
= constructor_type
;
7106 /* Push down into a subobject, for initialization.
7107 If this is for an explicit set of braces, IMPLICIT is 0.
7108 If it is because the next element belongs at a lower level,
7109 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7112 push_init_level (int implicit
, struct obstack
* braced_init_obstack
)
7114 struct constructor_stack
*p
;
7115 tree value
= NULL_TREE
;
7117 /* If we've exhausted any levels that didn't have braces,
7118 pop them now. If implicit == 1, this will have been done in
7119 process_init_element; do not repeat it here because in the case
7120 of excess initializers for an empty aggregate this leads to an
7121 infinite cycle of popping a level and immediately recreating
7125 while (constructor_stack
->implicit
)
7127 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7128 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7129 && constructor_fields
== 0)
7130 process_init_element (pop_init_level (1, braced_init_obstack
),
7131 true, braced_init_obstack
);
7132 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7133 && constructor_max_index
7134 && tree_int_cst_lt (constructor_max_index
,
7136 process_init_element (pop_init_level (1, braced_init_obstack
),
7137 true, braced_init_obstack
);
7143 /* Unless this is an explicit brace, we need to preserve previous
7147 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7148 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7149 && constructor_fields
)
7150 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7151 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7152 value
= find_init_member (constructor_index
, braced_init_obstack
);
7155 p
= XNEW (struct constructor_stack
);
7156 p
->type
= constructor_type
;
7157 p
->fields
= constructor_fields
;
7158 p
->index
= constructor_index
;
7159 p
->max_index
= constructor_max_index
;
7160 p
->unfilled_index
= constructor_unfilled_index
;
7161 p
->unfilled_fields
= constructor_unfilled_fields
;
7162 p
->bit_index
= constructor_bit_index
;
7163 p
->elements
= constructor_elements
;
7164 p
->constant
= constructor_constant
;
7165 p
->simple
= constructor_simple
;
7166 p
->nonconst
= constructor_nonconst
;
7167 p
->erroneous
= constructor_erroneous
;
7168 p
->pending_elts
= constructor_pending_elts
;
7169 p
->depth
= constructor_depth
;
7170 p
->replacement_value
.value
= 0;
7171 p
->replacement_value
.original_code
= ERROR_MARK
;
7172 p
->replacement_value
.original_type
= NULL
;
7173 p
->implicit
= implicit
;
7175 p
->incremental
= constructor_incremental
;
7176 p
->designated
= constructor_designated
;
7177 p
->next
= constructor_stack
;
7179 constructor_stack
= p
;
7181 constructor_constant
= 1;
7182 constructor_simple
= 1;
7183 constructor_nonconst
= 0;
7184 constructor_depth
= SPELLING_DEPTH ();
7185 constructor_elements
= NULL
;
7186 constructor_incremental
= 1;
7187 constructor_designated
= 0;
7188 constructor_pending_elts
= 0;
7191 p
->range_stack
= constructor_range_stack
;
7192 constructor_range_stack
= 0;
7193 designator_depth
= 0;
7194 designator_erroneous
= 0;
7197 /* Don't die if an entire brace-pair level is superfluous
7198 in the containing level. */
7199 if (constructor_type
== 0)
7201 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7202 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7204 /* Don't die if there are extra init elts at the end. */
7205 if (constructor_fields
== 0)
7206 constructor_type
= 0;
7209 constructor_type
= TREE_TYPE (constructor_fields
);
7210 push_member_name (constructor_fields
);
7211 constructor_depth
++;
7214 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7216 constructor_type
= TREE_TYPE (constructor_type
);
7217 push_array_bounds (tree_to_uhwi (constructor_index
));
7218 constructor_depth
++;
7221 if (constructor_type
== 0)
7223 error_init ("extra brace group at end of initializer");
7224 constructor_fields
= 0;
7225 constructor_unfilled_fields
= 0;
7229 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7231 constructor_constant
= TREE_CONSTANT (value
);
7232 constructor_simple
= TREE_STATIC (value
);
7233 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7234 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7235 if (!vec_safe_is_empty (constructor_elements
)
7236 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7237 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7238 set_nonincremental_init (braced_init_obstack
);
7241 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
7243 missing_braces_mentioned
= 1;
7244 warning_init (OPT_Wmissing_braces
, "missing braces around initializer");
7247 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7248 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7250 constructor_fields
= TYPE_FIELDS (constructor_type
);
7251 /* Skip any nameless bit fields at the beginning. */
7252 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7253 && DECL_NAME (constructor_fields
) == 0)
7254 constructor_fields
= DECL_CHAIN (constructor_fields
);
7256 constructor_unfilled_fields
= constructor_fields
;
7257 constructor_bit_index
= bitsize_zero_node
;
7259 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7261 /* Vectors are like simple fixed-size arrays. */
7262 constructor_max_index
=
7263 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7264 constructor_index
= bitsize_int (0);
7265 constructor_unfilled_index
= constructor_index
;
7267 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7269 if (TYPE_DOMAIN (constructor_type
))
7271 constructor_max_index
7272 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7274 /* Detect non-empty initializations of zero-length arrays. */
7275 if (constructor_max_index
== NULL_TREE
7276 && TYPE_SIZE (constructor_type
))
7277 constructor_max_index
= integer_minus_one_node
;
7279 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7280 to initialize VLAs will cause a proper error; avoid tree
7281 checking errors as well by setting a safe value. */
7282 if (constructor_max_index
7283 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7284 constructor_max_index
= integer_minus_one_node
;
7287 = convert (bitsizetype
,
7288 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7291 constructor_index
= bitsize_zero_node
;
7293 constructor_unfilled_index
= constructor_index
;
7294 if (value
&& TREE_CODE (value
) == STRING_CST
)
7296 /* We need to split the char/wchar array into individual
7297 characters, so that we don't have to special case it
7299 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7304 if (constructor_type
!= error_mark_node
)
7305 warning_init (0, "braces around scalar initializer");
7306 constructor_fields
= constructor_type
;
7307 constructor_unfilled_fields
= constructor_type
;
7311 /* At the end of an implicit or explicit brace level,
7312 finish up that level of constructor. If a single expression
7313 with redundant braces initialized that level, return the
7314 c_expr structure for that expression. Otherwise, the original_code
7315 element is set to ERROR_MARK.
7316 If we were outputting the elements as they are read, return 0 as the value
7317 from inner levels (process_init_element ignores that),
7318 but return error_mark_node as the value from the outermost level
7319 (that's what we want to put in DECL_INITIAL).
7320 Otherwise, return a CONSTRUCTOR expression as the value. */
7323 pop_init_level (int implicit
, struct obstack
* braced_init_obstack
)
7325 struct constructor_stack
*p
;
7328 ret
.original_code
= ERROR_MARK
;
7329 ret
.original_type
= NULL
;
7333 /* When we come to an explicit close brace,
7334 pop any inner levels that didn't have explicit braces. */
7335 while (constructor_stack
->implicit
)
7337 process_init_element (pop_init_level (1, braced_init_obstack
),
7338 true, braced_init_obstack
);
7340 gcc_assert (!constructor_range_stack
);
7343 /* Now output all pending elements. */
7344 constructor_incremental
= 1;
7345 output_pending_init_elements (1, braced_init_obstack
);
7347 p
= constructor_stack
;
7349 /* Error for initializing a flexible array member, or a zero-length
7350 array member in an inappropriate context. */
7351 if (constructor_type
&& constructor_fields
7352 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7353 && TYPE_DOMAIN (constructor_type
)
7354 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7356 /* Silently discard empty initializations. The parser will
7357 already have pedwarned for empty brackets. */
7358 if (integer_zerop (constructor_unfilled_index
))
7359 constructor_type
= NULL_TREE
;
7362 gcc_assert (!TYPE_SIZE (constructor_type
));
7364 if (constructor_depth
> 2)
7365 error_init ("initialization of flexible array member in a nested context");
7367 pedwarn_init (input_location
, OPT_Wpedantic
,
7368 "initialization of a flexible array member");
7370 /* We have already issued an error message for the existence
7371 of a flexible array member not at the end of the structure.
7372 Discard the initializer so that we do not die later. */
7373 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7374 constructor_type
= NULL_TREE
;
7378 /* Warn when some struct elements are implicitly initialized to zero. */
7379 if (warn_missing_field_initializers
7381 && TREE_CODE (constructor_type
) == RECORD_TYPE
7382 && constructor_unfilled_fields
)
7384 bool constructor_zeroinit
=
7385 (vec_safe_length (constructor_elements
) == 1
7386 && integer_zerop ((*constructor_elements
)[0].value
));
7388 /* Do not warn for flexible array members or zero-length arrays. */
7389 while (constructor_unfilled_fields
7390 && (!DECL_SIZE (constructor_unfilled_fields
)
7391 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
7392 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
7394 if (constructor_unfilled_fields
7395 /* Do not warn if this level of the initializer uses member
7396 designators; it is likely to be deliberate. */
7397 && !constructor_designated
7398 /* Do not warn about initializing with ` = {0}'. */
7399 && !constructor_zeroinit
)
7401 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
7402 "missing initializer for field %qD of %qT",
7403 constructor_unfilled_fields
,
7405 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
7406 "%qD declared here", constructor_unfilled_fields
);
7410 /* Pad out the end of the structure. */
7411 if (p
->replacement_value
.value
)
7412 /* If this closes a superfluous brace pair,
7413 just pass out the element between them. */
7414 ret
= p
->replacement_value
;
7415 else if (constructor_type
== 0)
7417 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
7418 && TREE_CODE (constructor_type
) != UNION_TYPE
7419 && TREE_CODE (constructor_type
) != ARRAY_TYPE
7420 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
7422 /* A nonincremental scalar initializer--just return
7423 the element, after verifying there is just one. */
7424 if (vec_safe_is_empty (constructor_elements
))
7426 if (!constructor_erroneous
)
7427 error_init ("empty scalar initializer");
7428 ret
.value
= error_mark_node
;
7430 else if (vec_safe_length (constructor_elements
) != 1)
7432 error_init ("extra elements in scalar initializer");
7433 ret
.value
= (*constructor_elements
)[0].value
;
7436 ret
.value
= (*constructor_elements
)[0].value
;
7440 if (constructor_erroneous
)
7441 ret
.value
= error_mark_node
;
7444 ret
.value
= build_constructor (constructor_type
,
7445 constructor_elements
);
7446 if (constructor_constant
)
7447 TREE_CONSTANT (ret
.value
) = 1;
7448 if (constructor_constant
&& constructor_simple
)
7449 TREE_STATIC (ret
.value
) = 1;
7450 if (constructor_nonconst
)
7451 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
7455 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
7457 if (constructor_nonconst
)
7458 ret
.original_code
= C_MAYBE_CONST_EXPR
;
7459 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
7460 ret
.original_code
= ERROR_MARK
;
7463 constructor_type
= p
->type
;
7464 constructor_fields
= p
->fields
;
7465 constructor_index
= p
->index
;
7466 constructor_max_index
= p
->max_index
;
7467 constructor_unfilled_index
= p
->unfilled_index
;
7468 constructor_unfilled_fields
= p
->unfilled_fields
;
7469 constructor_bit_index
= p
->bit_index
;
7470 constructor_elements
= p
->elements
;
7471 constructor_constant
= p
->constant
;
7472 constructor_simple
= p
->simple
;
7473 constructor_nonconst
= p
->nonconst
;
7474 constructor_erroneous
= p
->erroneous
;
7475 constructor_incremental
= p
->incremental
;
7476 constructor_designated
= p
->designated
;
7477 constructor_pending_elts
= p
->pending_elts
;
7478 constructor_depth
= p
->depth
;
7480 constructor_range_stack
= p
->range_stack
;
7481 RESTORE_SPELLING_DEPTH (constructor_depth
);
7483 constructor_stack
= p
->next
;
7486 if (ret
.value
== 0 && constructor_stack
== 0)
7487 ret
.value
= error_mark_node
;
7491 /* Common handling for both array range and field name designators.
7492 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7495 set_designator (int array
, struct obstack
* braced_init_obstack
)
7498 enum tree_code subcode
;
7500 /* Don't die if an entire brace-pair level is superfluous
7501 in the containing level. */
7502 if (constructor_type
== 0)
7505 /* If there were errors in this designator list already, bail out
7507 if (designator_erroneous
)
7510 if (!designator_depth
)
7512 gcc_assert (!constructor_range_stack
);
7514 /* Designator list starts at the level of closest explicit
7516 while (constructor_stack
->implicit
)
7518 process_init_element (pop_init_level (1, braced_init_obstack
),
7519 true, braced_init_obstack
);
7521 constructor_designated
= 1;
7525 switch (TREE_CODE (constructor_type
))
7529 subtype
= TREE_TYPE (constructor_fields
);
7530 if (subtype
!= error_mark_node
)
7531 subtype
= TYPE_MAIN_VARIANT (subtype
);
7534 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7540 subcode
= TREE_CODE (subtype
);
7541 if (array
&& subcode
!= ARRAY_TYPE
)
7543 error_init ("array index in non-array initializer");
7546 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
7548 error_init ("field name not in record or union initializer");
7552 constructor_designated
= 1;
7553 push_init_level (2, braced_init_obstack
);
7557 /* If there are range designators in designator list, push a new designator
7558 to constructor_range_stack. RANGE_END is end of such stack range or
7559 NULL_TREE if there is no range designator at this level. */
7562 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
7564 struct constructor_range_stack
*p
;
7566 p
= (struct constructor_range_stack
*)
7567 obstack_alloc (braced_init_obstack
,
7568 sizeof (struct constructor_range_stack
));
7569 p
->prev
= constructor_range_stack
;
7571 p
->fields
= constructor_fields
;
7572 p
->range_start
= constructor_index
;
7573 p
->index
= constructor_index
;
7574 p
->stack
= constructor_stack
;
7575 p
->range_end
= range_end
;
7576 if (constructor_range_stack
)
7577 constructor_range_stack
->next
= p
;
7578 constructor_range_stack
= p
;
7581 /* Within an array initializer, specify the next index to be initialized.
7582 FIRST is that index. If LAST is nonzero, then initialize a range
7583 of indices, running from FIRST through LAST. */
7586 set_init_index (tree first
, tree last
,
7587 struct obstack
* braced_init_obstack
)
7589 if (set_designator (1, braced_init_obstack
))
7592 designator_erroneous
= 1;
7594 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
7595 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
7597 error_init ("array index in initializer not of integer type");
7601 if (TREE_CODE (first
) != INTEGER_CST
)
7603 first
= c_fully_fold (first
, false, NULL
);
7604 if (TREE_CODE (first
) == INTEGER_CST
)
7605 pedwarn_init (input_location
, OPT_Wpedantic
,
7606 "array index in initializer is not "
7607 "an integer constant expression");
7610 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
7612 last
= c_fully_fold (last
, false, NULL
);
7613 if (TREE_CODE (last
) == INTEGER_CST
)
7614 pedwarn_init (input_location
, OPT_Wpedantic
,
7615 "array index in initializer is not "
7616 "an integer constant expression");
7619 if (TREE_CODE (first
) != INTEGER_CST
)
7620 error_init ("nonconstant array index in initializer");
7621 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
7622 error_init ("nonconstant array index in initializer");
7623 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7624 error_init ("array index in non-array initializer");
7625 else if (tree_int_cst_sgn (first
) == -1)
7626 error_init ("array index in initializer exceeds array bounds");
7627 else if (constructor_max_index
7628 && tree_int_cst_lt (constructor_max_index
, first
))
7629 error_init ("array index in initializer exceeds array bounds");
7632 constant_expression_warning (first
);
7634 constant_expression_warning (last
);
7635 constructor_index
= convert (bitsizetype
, first
);
7636 if (tree_int_cst_lt (constructor_index
, first
))
7638 constructor_index
= copy_node (constructor_index
);
7639 TREE_OVERFLOW (constructor_index
) = 1;
7644 if (tree_int_cst_equal (first
, last
))
7646 else if (tree_int_cst_lt (last
, first
))
7648 error_init ("empty index range in initializer");
7653 last
= convert (bitsizetype
, last
);
7654 if (constructor_max_index
!= 0
7655 && tree_int_cst_lt (constructor_max_index
, last
))
7657 error_init ("array index range in initializer exceeds array bounds");
7664 designator_erroneous
= 0;
7665 if (constructor_range_stack
|| last
)
7666 push_range_stack (last
, braced_init_obstack
);
7670 /* Within a struct initializer, specify the next field to be initialized. */
7673 set_init_label (tree fieldname
, struct obstack
* braced_init_obstack
)
7677 if (set_designator (0, braced_init_obstack
))
7680 designator_erroneous
= 1;
7682 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7683 && TREE_CODE (constructor_type
) != UNION_TYPE
)
7685 error_init ("field name not in record or union initializer");
7689 field
= lookup_field (constructor_type
, fieldname
);
7692 error ("unknown field %qE specified in initializer", fieldname
);
7696 constructor_fields
= TREE_VALUE (field
);
7698 designator_erroneous
= 0;
7699 if (constructor_range_stack
)
7700 push_range_stack (NULL_TREE
, braced_init_obstack
);
7701 field
= TREE_CHAIN (field
);
7704 if (set_designator (0, braced_init_obstack
))
7708 while (field
!= NULL_TREE
);
7711 /* Add a new initializer to the tree of pending initializers. PURPOSE
7712 identifies the initializer, either array index or field in a structure.
7713 VALUE is the value of that index or field. If ORIGTYPE is not
7714 NULL_TREE, it is the original type of VALUE.
7716 IMPLICIT is true if value comes from pop_init_level (1),
7717 the new initializer has been merged with the existing one
7718 and thus no warnings should be emitted about overriding an
7719 existing initializer. */
7722 add_pending_init (tree purpose
, tree value
, tree origtype
, bool implicit
,
7723 struct obstack
* braced_init_obstack
)
7725 struct init_node
*p
, **q
, *r
;
7727 q
= &constructor_pending_elts
;
7730 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7735 if (tree_int_cst_lt (purpose
, p
->purpose
))
7737 else if (tree_int_cst_lt (p
->purpose
, purpose
))
7743 if (TREE_SIDE_EFFECTS (p
->value
))
7744 warning_init (0, "initialized field with side-effects overwritten");
7745 else if (warn_override_init
)
7746 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7749 p
->origtype
= origtype
;
7758 bitpos
= bit_position (purpose
);
7762 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7764 else if (p
->purpose
!= purpose
)
7770 if (TREE_SIDE_EFFECTS (p
->value
))
7771 warning_init (0, "initialized field with side-effects overwritten");
7772 else if (warn_override_init
)
7773 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7776 p
->origtype
= origtype
;
7782 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
7783 sizeof (struct init_node
));
7784 r
->purpose
= purpose
;
7786 r
->origtype
= origtype
;
7796 struct init_node
*s
;
7800 if (p
->balance
== 0)
7802 else if (p
->balance
< 0)
7809 p
->left
->parent
= p
;
7826 constructor_pending_elts
= r
;
7831 struct init_node
*t
= r
->right
;
7835 r
->right
->parent
= r
;
7840 p
->left
->parent
= p
;
7843 p
->balance
= t
->balance
< 0;
7844 r
->balance
= -(t
->balance
> 0);
7859 constructor_pending_elts
= t
;
7865 /* p->balance == +1; growth of left side balances the node. */
7870 else /* r == p->right */
7872 if (p
->balance
== 0)
7873 /* Growth propagation from right side. */
7875 else if (p
->balance
> 0)
7882 p
->right
->parent
= p
;
7899 constructor_pending_elts
= r
;
7901 else /* r->balance == -1 */
7904 struct init_node
*t
= r
->left
;
7908 r
->left
->parent
= r
;
7913 p
->right
->parent
= p
;
7916 r
->balance
= (t
->balance
< 0);
7917 p
->balance
= -(t
->balance
> 0);
7932 constructor_pending_elts
= t
;
7938 /* p->balance == -1; growth of right side balances the node. */
7949 /* Build AVL tree from a sorted chain. */
7952 set_nonincremental_init (struct obstack
* braced_init_obstack
)
7954 unsigned HOST_WIDE_INT ix
;
7957 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7958 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7961 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
7963 add_pending_init (index
, value
, NULL_TREE
, true,
7964 braced_init_obstack
);
7966 constructor_elements
= NULL
;
7967 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7969 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
7970 /* Skip any nameless bit fields at the beginning. */
7971 while (constructor_unfilled_fields
!= 0
7972 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
7973 && DECL_NAME (constructor_unfilled_fields
) == 0)
7974 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
7977 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7979 if (TYPE_DOMAIN (constructor_type
))
7980 constructor_unfilled_index
7981 = convert (bitsizetype
,
7982 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7984 constructor_unfilled_index
= bitsize_zero_node
;
7986 constructor_incremental
= 0;
7989 /* Build AVL tree from a string constant. */
7992 set_nonincremental_init_from_string (tree str
,
7993 struct obstack
* braced_init_obstack
)
7995 tree value
, purpose
, type
;
7996 HOST_WIDE_INT val
[2];
7997 const char *p
, *end
;
7998 int byte
, wchar_bytes
, charwidth
, bitpos
;
8000 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8002 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8003 charwidth
= TYPE_PRECISION (char_type_node
);
8004 type
= TREE_TYPE (constructor_type
);
8005 p
= TREE_STRING_POINTER (str
);
8006 end
= p
+ TREE_STRING_LENGTH (str
);
8008 for (purpose
= bitsize_zero_node
;
8010 && !(constructor_max_index
8011 && tree_int_cst_lt (constructor_max_index
, purpose
));
8012 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8014 if (wchar_bytes
== 1)
8016 val
[1] = (unsigned char) *p
++;
8023 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8025 if (BYTES_BIG_ENDIAN
)
8026 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8028 bitpos
= byte
* charwidth
;
8029 val
[bitpos
< HOST_BITS_PER_WIDE_INT
]
8030 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8031 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8035 if (!TYPE_UNSIGNED (type
))
8037 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8038 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8040 if (val
[1] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
8042 val
[1] |= ((HOST_WIDE_INT
) -1) << bitpos
;
8046 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8051 else if (val
[0] & (((HOST_WIDE_INT
) 1)
8052 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8053 val
[0] |= ((HOST_WIDE_INT
) -1)
8054 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
8057 value
= build_int_cst_wide (type
, val
[1], val
[0]);
8058 add_pending_init (purpose
, value
, NULL_TREE
, true,
8059 braced_init_obstack
);
8062 constructor_incremental
= 0;
8065 /* Return value of FIELD in pending initializer or zero if the field was
8066 not initialized yet. */
8069 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8071 struct init_node
*p
;
8073 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8075 if (constructor_incremental
8076 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8077 set_nonincremental_init (braced_init_obstack
);
8079 p
= constructor_pending_elts
;
8082 if (tree_int_cst_lt (field
, p
->purpose
))
8084 else if (tree_int_cst_lt (p
->purpose
, field
))
8090 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8092 tree bitpos
= bit_position (field
);
8094 if (constructor_incremental
8095 && (!constructor_unfilled_fields
8096 || tree_int_cst_lt (bitpos
,
8097 bit_position (constructor_unfilled_fields
))))
8098 set_nonincremental_init (braced_init_obstack
);
8100 p
= constructor_pending_elts
;
8103 if (field
== p
->purpose
)
8105 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8111 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8113 if (!vec_safe_is_empty (constructor_elements
)
8114 && (constructor_elements
->last ().index
== field
))
8115 return constructor_elements
->last ().value
;
8120 /* "Output" the next constructor element.
8121 At top level, really output it to assembler code now.
8122 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8123 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8124 TYPE is the data type that the containing data type wants here.
8125 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8126 If VALUE is a string constant, STRICT_STRING is true if it is
8127 unparenthesized or we should not warn here for it being parenthesized.
8128 For other types of VALUE, STRICT_STRING is not used.
8130 PENDING if non-nil means output pending elements that belong
8131 right after this element. (PENDING is normally 1;
8132 it is 0 while outputting pending elements, to avoid recursion.)
8134 IMPLICIT is true if value comes from pop_init_level (1),
8135 the new initializer has been merged with the existing one
8136 and thus no warnings should be emitted about overriding an
8137 existing initializer. */
8140 output_init_element (tree value
, tree origtype
, bool strict_string
, tree type
,
8141 tree field
, int pending
, bool implicit
,
8142 struct obstack
* braced_init_obstack
)
8144 tree semantic_type
= NULL_TREE
;
8145 bool maybe_const
= true;
8148 if (type
== error_mark_node
|| value
== error_mark_node
)
8150 constructor_erroneous
= 1;
8153 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8154 && (TREE_CODE (value
) == STRING_CST
8155 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8156 && !(TREE_CODE (value
) == STRING_CST
8157 && TREE_CODE (type
) == ARRAY_TYPE
8158 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8159 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8160 TYPE_MAIN_VARIANT (type
)))
8161 value
= array_to_pointer_conversion (input_location
, value
);
8163 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8164 && require_constant_value
&& !flag_isoc99
&& pending
)
8166 /* As an extension, allow initializing objects with static storage
8167 duration with compound literals (which are then treated just as
8168 the brace enclosed list they contain). */
8169 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8170 value
= DECL_INITIAL (decl
);
8173 npc
= null_pointer_constant_p (value
);
8174 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8176 semantic_type
= TREE_TYPE (value
);
8177 value
= TREE_OPERAND (value
, 0);
8179 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8181 if (value
== error_mark_node
)
8182 constructor_erroneous
= 1;
8183 else if (!TREE_CONSTANT (value
))
8184 constructor_constant
= 0;
8185 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
8186 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
8187 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8188 && DECL_C_BIT_FIELD (field
)
8189 && TREE_CODE (value
) != INTEGER_CST
))
8190 constructor_simple
= 0;
8192 constructor_nonconst
= 1;
8194 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8196 if (require_constant_value
)
8198 error_init ("initializer element is not constant");
8199 value
= error_mark_node
;
8201 else if (require_constant_elements
)
8202 pedwarn (input_location
, 0,
8203 "initializer element is not computable at load time");
8205 else if (!maybe_const
8206 && (require_constant_value
|| require_constant_elements
))
8207 pedwarn_init (input_location
, 0,
8208 "initializer element is not a constant expression");
8210 /* Issue -Wc++-compat warnings about initializing a bitfield with
8213 && field
!= NULL_TREE
8214 && TREE_CODE (field
) == FIELD_DECL
8215 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8216 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8217 != TYPE_MAIN_VARIANT (type
))
8218 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8220 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8221 if (checktype
!= error_mark_node
8222 && (TYPE_MAIN_VARIANT (checktype
)
8223 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8224 warning_init (OPT_Wc___compat
,
8225 "enum conversion in initialization is invalid in C++");
8228 /* If this field is empty (and not at the end of structure),
8229 don't do anything other than checking the initializer. */
8231 && (TREE_TYPE (field
) == error_mark_node
8232 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8233 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8234 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8235 || DECL_CHAIN (field
)))))
8239 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8240 value
= digest_init (input_location
, type
, value
, origtype
, npc
,
8241 strict_string
, require_constant_value
);
8242 if (value
== error_mark_node
)
8244 constructor_erroneous
= 1;
8247 if (require_constant_value
|| require_constant_elements
)
8248 constant_expression_warning (value
);
8250 /* If this element doesn't come next in sequence,
8251 put it on constructor_pending_elts. */
8252 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8253 && (!constructor_incremental
8254 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8256 if (constructor_incremental
8257 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8258 set_nonincremental_init (braced_init_obstack
);
8260 add_pending_init (field
, value
, origtype
, implicit
,
8261 braced_init_obstack
);
8264 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8265 && (!constructor_incremental
8266 || field
!= constructor_unfilled_fields
))
8268 /* We do this for records but not for unions. In a union,
8269 no matter which field is specified, it can be initialized
8270 right away since it starts at the beginning of the union. */
8271 if (constructor_incremental
)
8273 if (!constructor_unfilled_fields
)
8274 set_nonincremental_init (braced_init_obstack
);
8277 tree bitpos
, unfillpos
;
8279 bitpos
= bit_position (field
);
8280 unfillpos
= bit_position (constructor_unfilled_fields
);
8282 if (tree_int_cst_lt (bitpos
, unfillpos
))
8283 set_nonincremental_init (braced_init_obstack
);
8287 add_pending_init (field
, value
, origtype
, implicit
,
8288 braced_init_obstack
);
8291 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8292 && !vec_safe_is_empty (constructor_elements
))
8296 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8298 "initialized field with side-effects overwritten");
8299 else if (warn_override_init
)
8300 warning_init (OPT_Woverride_init
, "initialized field overwritten");
8303 /* We can have just one union field set. */
8304 constructor_elements
= NULL
;
8307 /* Otherwise, output this element either to
8308 constructor_elements or to the assembler file. */
8310 constructor_elt celt
= {field
, value
};
8311 vec_safe_push (constructor_elements
, celt
);
8313 /* Advance the variable that indicates sequential elements output. */
8314 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8315 constructor_unfilled_index
8316 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8318 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8320 constructor_unfilled_fields
8321 = DECL_CHAIN (constructor_unfilled_fields
);
8323 /* Skip any nameless bit fields. */
8324 while (constructor_unfilled_fields
!= 0
8325 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8326 && DECL_NAME (constructor_unfilled_fields
) == 0)
8327 constructor_unfilled_fields
=
8328 DECL_CHAIN (constructor_unfilled_fields
);
8330 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8331 constructor_unfilled_fields
= 0;
8333 /* Now output any pending elements which have become next. */
8335 output_pending_init_elements (0, braced_init_obstack
);
8338 /* Output any pending elements which have become next.
8339 As we output elements, constructor_unfilled_{fields,index}
8340 advances, which may cause other elements to become next;
8341 if so, they too are output.
8343 If ALL is 0, we return when there are
8344 no more pending elements to output now.
8346 If ALL is 1, we output space as necessary so that
8347 we can output all the pending elements. */
8349 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8351 struct init_node
*elt
= constructor_pending_elts
;
8356 /* Look through the whole pending tree.
8357 If we find an element that should be output now,
8358 output it. Otherwise, set NEXT to the element
8359 that comes first among those still pending. */
8364 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8366 if (tree_int_cst_equal (elt
->purpose
,
8367 constructor_unfilled_index
))
8368 output_init_element (elt
->value
, elt
->origtype
, true,
8369 TREE_TYPE (constructor_type
),
8370 constructor_unfilled_index
, 0, false,
8371 braced_init_obstack
);
8372 else if (tree_int_cst_lt (constructor_unfilled_index
,
8375 /* Advance to the next smaller node. */
8380 /* We have reached the smallest node bigger than the
8381 current unfilled index. Fill the space first. */
8382 next
= elt
->purpose
;
8388 /* Advance to the next bigger node. */
8393 /* We have reached the biggest node in a subtree. Find
8394 the parent of it, which is the next bigger node. */
8395 while (elt
->parent
&& elt
->parent
->right
== elt
)
8398 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
8401 next
= elt
->purpose
;
8407 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8408 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8410 tree ctor_unfilled_bitpos
, elt_bitpos
;
8412 /* If the current record is complete we are done. */
8413 if (constructor_unfilled_fields
== 0)
8416 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
8417 elt_bitpos
= bit_position (elt
->purpose
);
8418 /* We can't compare fields here because there might be empty
8419 fields in between. */
8420 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
8422 constructor_unfilled_fields
= elt
->purpose
;
8423 output_init_element (elt
->value
, elt
->origtype
, true,
8424 TREE_TYPE (elt
->purpose
),
8425 elt
->purpose
, 0, false,
8426 braced_init_obstack
);
8428 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
8430 /* Advance to the next smaller node. */
8435 /* We have reached the smallest node bigger than the
8436 current unfilled field. Fill the space first. */
8437 next
= elt
->purpose
;
8443 /* Advance to the next bigger node. */
8448 /* We have reached the biggest node in a subtree. Find
8449 the parent of it, which is the next bigger node. */
8450 while (elt
->parent
&& elt
->parent
->right
== elt
)
8454 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
8455 bit_position (elt
->purpose
))))
8457 next
= elt
->purpose
;
8465 /* Ordinarily return, but not if we want to output all
8466 and there are elements left. */
8467 if (!(all
&& next
!= 0))
8470 /* If it's not incremental, just skip over the gap, so that after
8471 jumping to retry we will output the next successive element. */
8472 if (TREE_CODE (constructor_type
) == RECORD_TYPE
8473 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8474 constructor_unfilled_fields
= next
;
8475 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8476 constructor_unfilled_index
= next
;
8478 /* ELT now points to the node in the pending tree with the next
8479 initializer to output. */
8483 /* Add one non-braced element to the current constructor level.
8484 This adjusts the current position within the constructor's type.
8485 This may also start or terminate implicit levels
8486 to handle a partly-braced initializer.
8488 Once this has found the correct level for the new element,
8489 it calls output_init_element.
8491 IMPLICIT is true if value comes from pop_init_level (1),
8492 the new initializer has been merged with the existing one
8493 and thus no warnings should be emitted about overriding an
8494 existing initializer. */
8497 process_init_element (struct c_expr value
, bool implicit
,
8498 struct obstack
* braced_init_obstack
)
8500 tree orig_value
= value
.value
;
8501 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
8502 bool strict_string
= value
.original_code
== STRING_CST
;
8504 designator_depth
= 0;
8505 designator_erroneous
= 0;
8507 /* Handle superfluous braces around string cst as in
8508 char x[] = {"foo"}; */
8511 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8512 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
8513 && integer_zerop (constructor_unfilled_index
))
8515 if (constructor_stack
->replacement_value
.value
)
8516 error_init ("excess elements in char array initializer");
8517 constructor_stack
->replacement_value
= value
;
8521 if (constructor_stack
->replacement_value
.value
!= 0)
8523 error_init ("excess elements in struct initializer");
8527 /* Ignore elements of a brace group if it is entirely superfluous
8528 and has already been diagnosed. */
8529 if (constructor_type
== 0)
8532 /* If we've exhausted any levels that didn't have braces,
8534 while (constructor_stack
->implicit
)
8536 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
8537 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8538 && constructor_fields
== 0)
8539 process_init_element (pop_init_level (1, braced_init_obstack
),
8540 true, braced_init_obstack
);
8541 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
8542 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8543 && constructor_max_index
8544 && tree_int_cst_lt (constructor_max_index
,
8546 process_init_element (pop_init_level (1, braced_init_obstack
),
8547 true, braced_init_obstack
);
8552 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8553 if (constructor_range_stack
)
8555 /* If value is a compound literal and we'll be just using its
8556 content, don't put it into a SAVE_EXPR. */
8557 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
8558 || !require_constant_value
8561 tree semantic_type
= NULL_TREE
;
8562 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
8564 semantic_type
= TREE_TYPE (value
.value
);
8565 value
.value
= TREE_OPERAND (value
.value
, 0);
8567 value
.value
= c_save_expr (value
.value
);
8569 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
8576 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8579 enum tree_code fieldcode
;
8581 if (constructor_fields
== 0)
8583 pedwarn_init (input_location
, 0,
8584 "excess elements in struct initializer");
8588 fieldtype
= TREE_TYPE (constructor_fields
);
8589 if (fieldtype
!= error_mark_node
)
8590 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8591 fieldcode
= TREE_CODE (fieldtype
);
8593 /* Error for non-static initialization of a flexible array member. */
8594 if (fieldcode
== ARRAY_TYPE
8595 && !require_constant_value
8596 && TYPE_SIZE (fieldtype
) == NULL_TREE
8597 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8599 error_init ("non-static initialization of a flexible array member");
8603 /* Accept a string constant to initialize a subarray. */
8604 if (value
.value
!= 0
8605 && fieldcode
== ARRAY_TYPE
8606 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8608 value
.value
= orig_value
;
8609 /* Otherwise, if we have come to a subaggregate,
8610 and we don't have an element of its type, push into it. */
8611 else if (value
.value
!= 0
8612 && value
.value
!= error_mark_node
8613 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8614 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8615 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8617 push_init_level (1, braced_init_obstack
);
8623 push_member_name (constructor_fields
);
8624 output_init_element (value
.value
, value
.original_type
,
8625 strict_string
, fieldtype
,
8626 constructor_fields
, 1, implicit
,
8627 braced_init_obstack
);
8628 RESTORE_SPELLING_DEPTH (constructor_depth
);
8631 /* Do the bookkeeping for an element that was
8632 directly output as a constructor. */
8634 /* For a record, keep track of end position of last field. */
8635 if (DECL_SIZE (constructor_fields
))
8636 constructor_bit_index
8637 = size_binop_loc (input_location
, PLUS_EXPR
,
8638 bit_position (constructor_fields
),
8639 DECL_SIZE (constructor_fields
));
8641 /* If the current field was the first one not yet written out,
8642 it isn't now, so update. */
8643 if (constructor_unfilled_fields
== constructor_fields
)
8645 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8646 /* Skip any nameless bit fields. */
8647 while (constructor_unfilled_fields
!= 0
8648 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8649 && DECL_NAME (constructor_unfilled_fields
) == 0)
8650 constructor_unfilled_fields
=
8651 DECL_CHAIN (constructor_unfilled_fields
);
8655 constructor_fields
= DECL_CHAIN (constructor_fields
);
8656 /* Skip any nameless bit fields at the beginning. */
8657 while (constructor_fields
!= 0
8658 && DECL_C_BIT_FIELD (constructor_fields
)
8659 && DECL_NAME (constructor_fields
) == 0)
8660 constructor_fields
= DECL_CHAIN (constructor_fields
);
8662 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8665 enum tree_code fieldcode
;
8667 if (constructor_fields
== 0)
8669 pedwarn_init (input_location
, 0,
8670 "excess elements in union initializer");
8674 fieldtype
= TREE_TYPE (constructor_fields
);
8675 if (fieldtype
!= error_mark_node
)
8676 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8677 fieldcode
= TREE_CODE (fieldtype
);
8679 /* Warn that traditional C rejects initialization of unions.
8680 We skip the warning if the value is zero. This is done
8681 under the assumption that the zero initializer in user
8682 code appears conditioned on e.g. __STDC__ to avoid
8683 "missing initializer" warnings and relies on default
8684 initialization to zero in the traditional C case.
8685 We also skip the warning if the initializer is designated,
8686 again on the assumption that this must be conditional on
8687 __STDC__ anyway (and we've already complained about the
8688 member-designator already). */
8689 if (!in_system_header
&& !constructor_designated
8690 && !(value
.value
&& (integer_zerop (value
.value
)
8691 || real_zerop (value
.value
))))
8692 warning (OPT_Wtraditional
, "traditional C rejects initialization "
8695 /* Accept a string constant to initialize a subarray. */
8696 if (value
.value
!= 0
8697 && fieldcode
== ARRAY_TYPE
8698 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8700 value
.value
= orig_value
;
8701 /* Otherwise, if we have come to a subaggregate,
8702 and we don't have an element of its type, push into it. */
8703 else if (value
.value
!= 0
8704 && value
.value
!= error_mark_node
8705 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8706 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8707 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8709 push_init_level (1, braced_init_obstack
);
8715 push_member_name (constructor_fields
);
8716 output_init_element (value
.value
, value
.original_type
,
8717 strict_string
, fieldtype
,
8718 constructor_fields
, 1, implicit
,
8719 braced_init_obstack
);
8720 RESTORE_SPELLING_DEPTH (constructor_depth
);
8723 /* Do the bookkeeping for an element that was
8724 directly output as a constructor. */
8726 constructor_bit_index
= DECL_SIZE (constructor_fields
);
8727 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8730 constructor_fields
= 0;
8732 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8734 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8735 enum tree_code eltcode
= TREE_CODE (elttype
);
8737 /* Accept a string constant to initialize a subarray. */
8738 if (value
.value
!= 0
8739 && eltcode
== ARRAY_TYPE
8740 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
8742 value
.value
= orig_value
;
8743 /* Otherwise, if we have come to a subaggregate,
8744 and we don't have an element of its type, push into it. */
8745 else if (value
.value
!= 0
8746 && value
.value
!= error_mark_node
8747 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
8748 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
8749 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
8751 push_init_level (1, braced_init_obstack
);
8755 if (constructor_max_index
!= 0
8756 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
8757 || integer_all_onesp (constructor_max_index
)))
8759 pedwarn_init (input_location
, 0,
8760 "excess elements in array initializer");
8764 /* Now output the actual element. */
8767 push_array_bounds (tree_to_uhwi (constructor_index
));
8768 output_init_element (value
.value
, value
.original_type
,
8769 strict_string
, elttype
,
8770 constructor_index
, 1, implicit
,
8771 braced_init_obstack
);
8772 RESTORE_SPELLING_DEPTH (constructor_depth
);
8776 = size_binop_loc (input_location
, PLUS_EXPR
,
8777 constructor_index
, bitsize_one_node
);
8780 /* If we are doing the bookkeeping for an element that was
8781 directly output as a constructor, we must update
8782 constructor_unfilled_index. */
8783 constructor_unfilled_index
= constructor_index
;
8785 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8787 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8789 /* Do a basic check of initializer size. Note that vectors
8790 always have a fixed size derived from their type. */
8791 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
8793 pedwarn_init (input_location
, 0,
8794 "excess elements in vector initializer");
8798 /* Now output the actual element. */
8801 if (TREE_CODE (value
.value
) == VECTOR_CST
)
8802 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
8803 output_init_element (value
.value
, value
.original_type
,
8804 strict_string
, elttype
,
8805 constructor_index
, 1, implicit
,
8806 braced_init_obstack
);
8810 = size_binop_loc (input_location
,
8811 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
8814 /* If we are doing the bookkeeping for an element that was
8815 directly output as a constructor, we must update
8816 constructor_unfilled_index. */
8817 constructor_unfilled_index
= constructor_index
;
8820 /* Handle the sole element allowed in a braced initializer
8821 for a scalar variable. */
8822 else if (constructor_type
!= error_mark_node
8823 && constructor_fields
== 0)
8825 pedwarn_init (input_location
, 0,
8826 "excess elements in scalar initializer");
8832 output_init_element (value
.value
, value
.original_type
,
8833 strict_string
, constructor_type
,
8834 NULL_TREE
, 1, implicit
,
8835 braced_init_obstack
);
8836 constructor_fields
= 0;
8839 /* Handle range initializers either at this level or anywhere higher
8840 in the designator stack. */
8841 if (constructor_range_stack
)
8843 struct constructor_range_stack
*p
, *range_stack
;
8846 range_stack
= constructor_range_stack
;
8847 constructor_range_stack
= 0;
8848 while (constructor_stack
!= range_stack
->stack
)
8850 gcc_assert (constructor_stack
->implicit
);
8851 process_init_element (pop_init_level (1,
8852 braced_init_obstack
),
8853 true, braced_init_obstack
);
8855 for (p
= range_stack
;
8856 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
8859 gcc_assert (constructor_stack
->implicit
);
8860 process_init_element (pop_init_level (1, braced_init_obstack
),
8861 true, braced_init_obstack
);
8864 p
->index
= size_binop_loc (input_location
,
8865 PLUS_EXPR
, p
->index
, bitsize_one_node
);
8866 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
8871 constructor_index
= p
->index
;
8872 constructor_fields
= p
->fields
;
8873 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
8881 push_init_level (2, braced_init_obstack
);
8882 p
->stack
= constructor_stack
;
8883 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
8884 p
->index
= p
->range_start
;
8888 constructor_range_stack
= range_stack
;
8895 constructor_range_stack
= 0;
8898 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8899 (guaranteed to be 'volatile' or null) and ARGS (represented using
8900 an ASM_EXPR node). */
8902 build_asm_stmt (tree cv_qualifier
, tree args
)
8904 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
8905 ASM_VOLATILE_P (args
) = 1;
8906 return add_stmt (args
);
8909 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8910 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8911 SIMPLE indicates whether there was anything at all after the
8912 string in the asm expression -- asm("blah") and asm("blah" : )
8913 are subtly different. We use a ASM_EXPR node to represent this. */
8915 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
8916 tree clobbers
, tree labels
, bool simple
)
8921 const char *constraint
;
8922 const char **oconstraints
;
8923 bool allows_mem
, allows_reg
, is_inout
;
8924 int ninputs
, noutputs
;
8926 ninputs
= list_length (inputs
);
8927 noutputs
= list_length (outputs
);
8928 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
8930 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
8932 /* Remove output conversions that change the type but not the mode. */
8933 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
8935 tree output
= TREE_VALUE (tail
);
8937 output
= c_fully_fold (output
, false, NULL
);
8939 /* ??? Really, this should not be here. Users should be using a
8940 proper lvalue, dammit. But there's a long history of using casts
8941 in the output operands. In cases like longlong.h, this becomes a
8942 primitive form of typechecking -- if the cast can be removed, then
8943 the output operand had a type of the proper width; otherwise we'll
8944 get an error. Gross, but ... */
8945 STRIP_NOPS (output
);
8947 if (!lvalue_or_else (loc
, output
, lv_asm
))
8948 output
= error_mark_node
;
8950 if (output
!= error_mark_node
8951 && (TREE_READONLY (output
)
8952 || TYPE_READONLY (TREE_TYPE (output
))
8953 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
8954 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
8955 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
8956 readonly_error (loc
, output
, lv_asm
);
8958 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
8959 oconstraints
[i
] = constraint
;
8961 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
8962 &allows_mem
, &allows_reg
, &is_inout
))
8964 /* If the operand is going to end up in memory,
8965 mark it addressable. */
8966 if (!allows_reg
&& !c_mark_addressable (output
))
8967 output
= error_mark_node
;
8968 if (!(!allows_reg
&& allows_mem
)
8969 && output
!= error_mark_node
8970 && VOID_TYPE_P (TREE_TYPE (output
)))
8972 error_at (loc
, "invalid use of void expression");
8973 output
= error_mark_node
;
8977 output
= error_mark_node
;
8979 TREE_VALUE (tail
) = output
;
8982 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
8986 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
8987 input
= TREE_VALUE (tail
);
8989 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
8990 oconstraints
, &allows_mem
, &allows_reg
))
8992 /* If the operand is going to end up in memory,
8993 mark it addressable. */
8994 if (!allows_reg
&& allows_mem
)
8996 input
= c_fully_fold (input
, false, NULL
);
8998 /* Strip the nops as we allow this case. FIXME, this really
8999 should be rejected or made deprecated. */
9001 if (!c_mark_addressable (input
))
9002 input
= error_mark_node
;
9007 memset (&expr
, 0, sizeof (expr
));
9009 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9010 input
= c_fully_fold (expr
.value
, false, NULL
);
9012 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9014 error_at (loc
, "invalid use of void expression");
9015 input
= error_mark_node
;
9020 input
= error_mark_node
;
9022 TREE_VALUE (tail
) = input
;
9025 /* ASMs with labels cannot have outputs. This should have been
9026 enforced by the parser. */
9027 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9029 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9031 /* asm statements without outputs, including simple ones, are treated
9033 ASM_INPUT_P (args
) = simple
;
9034 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9039 /* Generate a goto statement to LABEL. LOC is the location of the
9043 c_finish_goto_label (location_t loc
, tree label
)
9045 tree decl
= lookup_label_for_goto (loc
, label
);
9048 TREE_USED (decl
) = 1;
9050 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9051 SET_EXPR_LOCATION (t
, loc
);
9052 return add_stmt (t
);
9056 /* Generate a computed goto statement to EXPR. LOC is the location of
9060 c_finish_goto_ptr (location_t loc
, tree expr
)
9063 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9064 expr
= c_fully_fold (expr
, false, NULL
);
9065 expr
= convert (ptr_type_node
, expr
);
9066 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9067 SET_EXPR_LOCATION (t
, loc
);
9068 return add_stmt (t
);
9071 /* Generate a C `return' statement. RETVAL is the expression for what
9072 to return, or a null pointer for `return;' with no value. LOC is
9073 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9074 is the original type of RETVAL. */
9077 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9079 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9080 bool no_warning
= false;
9084 if (TREE_THIS_VOLATILE (current_function_decl
))
9086 "function declared %<noreturn%> has a %<return%> statement");
9088 if (flag_enable_cilkplus
&& contains_array_notation_expr (retval
))
9090 /* Array notations are allowed in a return statement if it is inside a
9091 built-in array notation reduction function. */
9092 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9093 return error_mark_node
;
9096 error_at (loc
, "array notation expression cannot be used as a "
9098 return error_mark_node
;
9101 if (flag_enable_cilkplus
&& retval
&& TREE_CODE (retval
) == CILK_SPAWN_STMT
)
9103 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9105 return error_mark_node
;
9109 tree semantic_type
= NULL_TREE
;
9110 npc
= null_pointer_constant_p (retval
);
9111 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9113 semantic_type
= TREE_TYPE (retval
);
9114 retval
= TREE_OPERAND (retval
, 0);
9116 retval
= c_fully_fold (retval
, false, NULL
);
9118 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9123 current_function_returns_null
= 1;
9124 if ((warn_return_type
|| flag_isoc99
)
9125 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9127 pedwarn_c99 (loc
, flag_isoc99
? 0 : OPT_Wreturn_type
,
9128 "%<return%> with no value, in "
9129 "function returning non-void");
9133 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9135 current_function_returns_null
= 1;
9136 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9138 "%<return%> with a value, in function returning void");
9140 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
9141 "%<return%> with expression, in function returning void");
9145 tree t
= convert_for_assignment (loc
, valtype
, retval
, origtype
,
9147 npc
, NULL_TREE
, NULL_TREE
, 0);
9148 tree res
= DECL_RESULT (current_function_decl
);
9152 current_function_returns_value
= 1;
9153 if (t
== error_mark_node
)
9156 save
= in_late_binary_op
;
9157 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9158 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
)
9159 in_late_binary_op
= true;
9160 inner
= t
= convert (TREE_TYPE (res
), t
);
9161 in_late_binary_op
= save
;
9163 /* Strip any conversions, additions, and subtractions, and see if
9164 we are returning the address of a local variable. Warn if so. */
9167 switch (TREE_CODE (inner
))
9170 case NON_LVALUE_EXPR
:
9172 case POINTER_PLUS_EXPR
:
9173 inner
= TREE_OPERAND (inner
, 0);
9177 /* If the second operand of the MINUS_EXPR has a pointer
9178 type (or is converted from it), this may be valid, so
9179 don't give a warning. */
9181 tree op1
= TREE_OPERAND (inner
, 1);
9183 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9184 && (CONVERT_EXPR_P (op1
)
9185 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9186 op1
= TREE_OPERAND (op1
, 0);
9188 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9191 inner
= TREE_OPERAND (inner
, 0);
9196 inner
= TREE_OPERAND (inner
, 0);
9198 while (REFERENCE_CLASS_P (inner
)
9199 && TREE_CODE (inner
) != INDIRECT_REF
)
9200 inner
= TREE_OPERAND (inner
, 0);
9203 && !DECL_EXTERNAL (inner
)
9204 && !TREE_STATIC (inner
)
9205 && DECL_CONTEXT (inner
) == current_function_decl
)
9207 OPT_Wreturn_local_addr
, "function returns address "
9208 "of local variable");
9218 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9219 SET_EXPR_LOCATION (retval
, loc
);
9221 if (warn_sequence_point
)
9222 verify_sequence_points (retval
);
9225 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9226 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9227 return add_stmt (ret_stmt
);
9231 /* The SWITCH_EXPR being built. */
9234 /* The original type of the testing expression, i.e. before the
9235 default conversion is applied. */
9238 /* A splay-tree mapping the low element of a case range to the high
9239 element, or NULL_TREE if there is no high element. Used to
9240 determine whether or not a new case label duplicates an old case
9241 label. We need a tree, rather than simply a hash table, because
9242 of the GNU case range extension. */
9245 /* The bindings at the point of the switch. This is used for
9246 warnings crossing decls when branching to a case label. */
9247 struct c_spot_bindings
*bindings
;
9249 /* The next node on the stack. */
9250 struct c_switch
*next
;
9253 /* A stack of the currently active switch statements. The innermost
9254 switch statement is on the top of the stack. There is no need to
9255 mark the stack for garbage collection because it is only active
9256 during the processing of the body of a function, and we never
9257 collect at that point. */
9259 struct c_switch
*c_switch_stack
;
9261 /* Start a C switch statement, testing expression EXP. Return the new
9262 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9263 SWITCH_COND_LOC is the location of the switch's condition. */
9266 c_start_case (location_t switch_loc
,
9267 location_t switch_cond_loc
,
9270 tree orig_type
= error_mark_node
;
9271 struct c_switch
*cs
;
9273 if (exp
!= error_mark_node
)
9275 orig_type
= TREE_TYPE (exp
);
9277 if (!INTEGRAL_TYPE_P (orig_type
))
9279 if (orig_type
!= error_mark_node
)
9281 error_at (switch_cond_loc
, "switch quantity not an integer");
9282 orig_type
= error_mark_node
;
9284 exp
= integer_zero_node
;
9288 tree type
= TYPE_MAIN_VARIANT (orig_type
);
9290 if (!in_system_header
9291 && (type
== long_integer_type_node
9292 || type
== long_unsigned_type_node
))
9293 warning_at (switch_cond_loc
,
9294 OPT_Wtraditional
, "%<long%> switch expression not "
9295 "converted to %<int%> in ISO C");
9297 exp
= c_fully_fold (exp
, false, NULL
);
9298 exp
= default_conversion (exp
);
9300 if (warn_sequence_point
)
9301 verify_sequence_points (exp
);
9305 /* Add this new SWITCH_EXPR to the stack. */
9306 cs
= XNEW (struct c_switch
);
9307 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
9308 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
9309 cs
->orig_type
= orig_type
;
9310 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
9311 cs
->bindings
= c_get_switch_bindings ();
9312 cs
->next
= c_switch_stack
;
9313 c_switch_stack
= cs
;
9315 return add_stmt (cs
->switch_expr
);
9318 /* Process a case label at location LOC. */
9321 do_case (location_t loc
, tree low_value
, tree high_value
)
9323 tree label
= NULL_TREE
;
9325 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
9327 low_value
= c_fully_fold (low_value
, false, NULL
);
9328 if (TREE_CODE (low_value
) == INTEGER_CST
)
9329 pedwarn (input_location
, OPT_Wpedantic
,
9330 "case label is not an integer constant expression");
9333 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
9335 high_value
= c_fully_fold (high_value
, false, NULL
);
9336 if (TREE_CODE (high_value
) == INTEGER_CST
)
9337 pedwarn (input_location
, OPT_Wpedantic
,
9338 "case label is not an integer constant expression");
9341 if (c_switch_stack
== NULL
)
9344 error_at (loc
, "case label not within a switch statement");
9346 error_at (loc
, "%<default%> label not within a switch statement");
9350 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
9351 EXPR_LOCATION (c_switch_stack
->switch_expr
),
9355 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
9356 SWITCH_COND (c_switch_stack
->switch_expr
),
9357 c_switch_stack
->orig_type
,
9358 low_value
, high_value
);
9359 if (label
== error_mark_node
)
9364 /* Finish the switch statement. */
9367 c_finish_case (tree body
)
9369 struct c_switch
*cs
= c_switch_stack
;
9370 location_t switch_location
;
9372 SWITCH_BODY (cs
->switch_expr
) = body
;
9374 /* Emit warnings as needed. */
9375 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
9376 c_do_switch_warnings (cs
->cases
, switch_location
,
9377 TREE_TYPE (cs
->switch_expr
),
9378 SWITCH_COND (cs
->switch_expr
));
9380 /* Pop the stack. */
9381 c_switch_stack
= cs
->next
;
9382 splay_tree_delete (cs
->cases
);
9383 c_release_switch_bindings (cs
->bindings
);
9387 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9388 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9389 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9390 statement, and was not surrounded with parenthesis. */
9393 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
9394 tree else_block
, bool nested_if
)
9398 /* If the condition has array notations, then the rank of the then_block and
9399 else_block must be either 0 or be equal to the rank of the condition. If
9400 the condition does not have array notations then break them up as it is
9401 broken up in a normal expression. */
9402 if (flag_enable_cilkplus
&& contains_array_notation_expr (cond
))
9404 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
9405 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
9408 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
9411 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
9413 if (cond_rank
!= then_rank
&& then_rank
!= 0)
9415 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9416 " and the then-block");
9419 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
9421 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9422 " and the else-block");
9426 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9427 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
9429 tree inner_if
= then_block
;
9431 /* We know from the grammar productions that there is an IF nested
9432 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9433 it might not be exactly THEN_BLOCK, but should be the last
9434 non-container statement within. */
9436 switch (TREE_CODE (inner_if
))
9441 inner_if
= BIND_EXPR_BODY (inner_if
);
9443 case STATEMENT_LIST
:
9444 inner_if
= expr_last (then_block
);
9446 case TRY_FINALLY_EXPR
:
9447 case TRY_CATCH_EXPR
:
9448 inner_if
= TREE_OPERAND (inner_if
, 0);
9455 if (COND_EXPR_ELSE (inner_if
))
9456 warning_at (if_locus
, OPT_Wparentheses
,
9457 "suggest explicit braces to avoid ambiguous %<else%>");
9460 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
9461 SET_EXPR_LOCATION (stmt
, if_locus
);
9465 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9466 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9467 is false for DO loops. INCR is the FOR increment expression. BODY is
9468 the statement controlled by the loop. BLAB is the break label. CLAB is
9469 the continue label. Everything is allowed to be NULL. */
9472 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
9473 tree blab
, tree clab
, bool cond_is_first
)
9475 tree entry
= NULL
, exit
= NULL
, t
;
9477 if (flag_enable_cilkplus
&& contains_array_notation_expr (cond
))
9479 error_at (start_locus
, "array notation expression cannot be used in a "
9480 "loop%'s condition");
9484 /* If the condition is zero don't generate a loop construct. */
9485 if (cond
&& integer_zerop (cond
))
9489 t
= build_and_jump (&blab
);
9490 SET_EXPR_LOCATION (t
, start_locus
);
9496 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9498 /* If we have an exit condition, then we build an IF with gotos either
9499 out of the loop, or to the top of it. If there's no exit condition,
9500 then we just build a jump back to the top. */
9501 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
9503 if (cond
&& !integer_nonzerop (cond
))
9505 /* Canonicalize the loop condition to the end. This means
9506 generating a branch to the loop condition. Reuse the
9507 continue label, if possible. */
9512 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9513 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
9516 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
9517 SET_EXPR_LOCATION (t
, start_locus
);
9521 t
= build_and_jump (&blab
);
9523 exit
= fold_build3_loc (start_locus
,
9524 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9526 exit
= fold_build3_loc (input_location
,
9527 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9536 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
9544 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
9548 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
9551 tree label
= *label_p
;
9553 /* In switch statements break is sometimes stylistically used after
9554 a return statement. This can lead to spurious warnings about
9555 control reaching the end of a non-void function when it is
9556 inlined. Note that we are calling block_may_fallthru with
9557 language specific tree nodes; this works because
9558 block_may_fallthru returns true when given something it does not
9560 skip
= !block_may_fallthru (cur_stmt_list
);
9565 *label_p
= label
= create_artificial_label (loc
);
9567 else if (TREE_CODE (label
) == LABEL_DECL
)
9569 else switch (TREE_INT_CST_LOW (label
))
9573 error_at (loc
, "break statement not within loop or switch");
9575 error_at (loc
, "continue statement not within a loop");
9579 gcc_assert (is_break
);
9580 error_at (loc
, "break statement used with OpenMP for loop");
9585 error ("break statement within %<#pragma simd%> loop body");
9587 error ("continue statement within %<#pragma simd%> loop body");
9598 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
9600 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
9603 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9606 emit_side_effect_warnings (location_t loc
, tree expr
)
9608 if (expr
== error_mark_node
)
9610 else if (!TREE_SIDE_EFFECTS (expr
))
9612 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
9613 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
9616 warn_if_unused_value (expr
, loc
);
9619 /* Process an expression as if it were a complete statement. Emit
9620 diagnostics, but do not call ADD_STMT. LOC is the location of the
9624 c_process_expr_stmt (location_t loc
, tree expr
)
9631 expr
= c_fully_fold (expr
, false, NULL
);
9633 if (warn_sequence_point
)
9634 verify_sequence_points (expr
);
9636 if (TREE_TYPE (expr
) != error_mark_node
9637 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
9638 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
9639 error_at (loc
, "expression statement has incomplete type");
9641 /* If we're not processing a statement expression, warn about unused values.
9642 Warnings for statement expressions will be emitted later, once we figure
9643 out which is the result. */
9644 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9645 && warn_unused_value
)
9646 emit_side_effect_warnings (loc
, expr
);
9649 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
9650 exprv
= TREE_OPERAND (exprv
, 1);
9651 while (CONVERT_EXPR_P (exprv
))
9652 exprv
= TREE_OPERAND (exprv
, 0);
9654 || handled_component_p (exprv
)
9655 || TREE_CODE (exprv
) == ADDR_EXPR
)
9656 mark_exp_read (exprv
);
9658 /* If the expression is not of a type to which we cannot assign a line
9659 number, wrap the thing in a no-op NOP_EXPR. */
9660 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
9662 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
9663 SET_EXPR_LOCATION (expr
, loc
);
9669 /* Emit an expression as a statement. LOC is the location of the
9673 c_finish_expr_stmt (location_t loc
, tree expr
)
9676 return add_stmt (c_process_expr_stmt (loc
, expr
));
9681 /* Do the opposite and emit a statement as an expression. To begin,
9682 create a new binding level and return it. */
9685 c_begin_stmt_expr (void)
9689 /* We must force a BLOCK for this level so that, if it is not expanded
9690 later, there is a way to turn off the entire subtree of blocks that
9691 are contained in it. */
9693 ret
= c_begin_compound_stmt (true);
9695 c_bindings_start_stmt_expr (c_switch_stack
== NULL
9697 : c_switch_stack
->bindings
);
9699 /* Mark the current statement list as belonging to a statement list. */
9700 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
9705 /* LOC is the location of the compound statement to which this body
9709 c_finish_stmt_expr (location_t loc
, tree body
)
9711 tree last
, type
, tmp
, val
;
9714 body
= c_end_compound_stmt (loc
, body
, true);
9716 c_bindings_end_stmt_expr (c_switch_stack
== NULL
9718 : c_switch_stack
->bindings
);
9720 /* Locate the last statement in BODY. See c_end_compound_stmt
9721 about always returning a BIND_EXPR. */
9722 last_p
= &BIND_EXPR_BODY (body
);
9723 last
= BIND_EXPR_BODY (body
);
9726 if (TREE_CODE (last
) == STATEMENT_LIST
)
9728 tree_stmt_iterator i
;
9730 /* This can happen with degenerate cases like ({ }). No value. */
9731 if (!TREE_SIDE_EFFECTS (last
))
9734 /* If we're supposed to generate side effects warnings, process
9735 all of the statements except the last. */
9736 if (warn_unused_value
)
9738 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
9741 tree t
= tsi_stmt (i
);
9743 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
9744 emit_side_effect_warnings (tloc
, t
);
9748 i
= tsi_last (last
);
9749 last_p
= tsi_stmt_ptr (i
);
9753 /* If the end of the list is exception related, then the list was split
9754 by a call to push_cleanup. Continue searching. */
9755 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
9756 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
9758 last_p
= &TREE_OPERAND (last
, 0);
9760 goto continue_searching
;
9763 if (last
== error_mark_node
)
9766 /* In the case that the BIND_EXPR is not necessary, return the
9767 expression out from inside it. */
9768 if (last
== BIND_EXPR_BODY (body
)
9769 && BIND_EXPR_VARS (body
) == NULL
)
9771 /* Even if this looks constant, do not allow it in a constant
9773 last
= c_wrap_maybe_const (last
, true);
9774 /* Do not warn if the return value of a statement expression is
9776 TREE_NO_WARNING (last
) = 1;
9780 /* Extract the type of said expression. */
9781 type
= TREE_TYPE (last
);
9783 /* If we're not returning a value at all, then the BIND_EXPR that
9784 we already have is a fine expression to return. */
9785 if (!type
|| VOID_TYPE_P (type
))
9788 /* Now that we've located the expression containing the value, it seems
9789 silly to make voidify_wrapper_expr repeat the process. Create a
9790 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9791 tmp
= create_tmp_var_raw (type
, NULL
);
9793 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9794 tree_expr_nonnegative_p giving up immediately. */
9796 if (TREE_CODE (val
) == NOP_EXPR
9797 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
9798 val
= TREE_OPERAND (val
, 0);
9800 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
9801 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
9804 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
9805 SET_EXPR_LOCATION (t
, loc
);
9810 /* Begin and end compound statements. This is as simple as pushing
9811 and popping new statement lists from the tree. */
9814 c_begin_compound_stmt (bool do_scope
)
9816 tree stmt
= push_stmt_list ();
9822 /* End a compound statement. STMT is the statement. LOC is the
9823 location of the compound statement-- this is usually the location
9824 of the opening brace. */
9827 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
9833 if (c_dialect_objc ())
9834 objc_clear_super_receiver ();
9835 block
= pop_scope ();
9838 stmt
= pop_stmt_list (stmt
);
9839 stmt
= c_build_bind_expr (loc
, block
, stmt
);
9841 /* If this compound statement is nested immediately inside a statement
9842 expression, then force a BIND_EXPR to be created. Otherwise we'll
9843 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9844 STATEMENT_LISTs merge, and thus we can lose track of what statement
9846 if (building_stmt_list_p ()
9847 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9848 && TREE_CODE (stmt
) != BIND_EXPR
)
9850 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
9851 TREE_SIDE_EFFECTS (stmt
) = 1;
9852 SET_EXPR_LOCATION (stmt
, loc
);
9858 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9859 when the current scope is exited. EH_ONLY is true when this is not
9860 meant to apply to normal control flow transfer. */
9863 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
9865 enum tree_code code
;
9869 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
9870 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
9872 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
9873 list
= push_stmt_list ();
9874 TREE_OPERAND (stmt
, 0) = list
;
9875 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
9878 /* Build a binary-operation expression without default conversions.
9879 CODE is the kind of expression to build.
9880 LOCATION is the operator's location.
9881 This function differs from `build' in several ways:
9882 the data type of the result is computed and recorded in it,
9883 warnings are generated if arg data types are invalid,
9884 special handling for addition and subtraction of pointers is known,
9885 and some optimization is done (operations on narrow ints
9886 are done in the narrower type when that gives the same result).
9887 Constant folding is also done before the result is returned.
9889 Note that the operands will never have enumeral types, or function
9890 or array types, because either they will have the default conversions
9891 performed or they have both just been converted to some other type in which
9892 the arithmetic is to be done. */
9895 build_binary_op (location_t location
, enum tree_code code
,
9896 tree orig_op0
, tree orig_op1
, int convert_p
)
9898 tree type0
, type1
, orig_type0
, orig_type1
;
9900 enum tree_code code0
, code1
;
9902 tree ret
= error_mark_node
;
9903 const char *invalid_op_diag
;
9904 bool op0_int_operands
, op1_int_operands
;
9905 bool int_const
, int_const_or_overflow
, int_operands
;
9907 /* Expression code to give to the expression when it is built.
9908 Normally this is CODE, which is what the caller asked for,
9909 but in some special cases we change it. */
9910 enum tree_code resultcode
= code
;
9912 /* Data type in which the computation is to be performed.
9913 In the simplest cases this is the common type of the arguments. */
9914 tree result_type
= NULL
;
9916 /* When the computation is in excess precision, the type of the
9917 final EXCESS_PRECISION_EXPR. */
9918 tree semantic_result_type
= NULL
;
9920 /* Nonzero means operands have already been type-converted
9921 in whatever way is necessary.
9922 Zero means they need to be converted to RESULT_TYPE. */
9925 /* Nonzero means create the expression with this type, rather than
9927 tree build_type
= 0;
9929 /* Nonzero means after finally constructing the expression
9930 convert it to this type. */
9931 tree final_type
= 0;
9933 /* Nonzero if this is an operation like MIN or MAX which can
9934 safely be computed in short if both args are promoted shorts.
9935 Also implies COMMON.
9936 -1 indicates a bitwise operation; this makes a difference
9937 in the exact conditions for when it is safe to do the operation
9938 in a narrower mode. */
9941 /* Nonzero if this is a comparison operation;
9942 if both args are promoted shorts, compare the original shorts.
9943 Also implies COMMON. */
9944 int short_compare
= 0;
9946 /* Nonzero if this is a right-shift operation, which can be computed on the
9947 original short and then promoted if the operand is a promoted short. */
9948 int short_shift
= 0;
9950 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9953 /* True means types are compatible as far as ObjC is concerned. */
9956 /* True means this is an arithmetic operation that may need excess
9958 bool may_need_excess_precision
;
9960 /* True means this is a boolean operation that converts both its
9961 operands to truth-values. */
9962 bool boolean_op
= false;
9964 /* Remember whether we're doing / or %. */
9965 bool doing_div_or_mod
= false;
9967 /* Remember whether we're doing << or >>. */
9968 bool doing_shift
= false;
9970 /* Tree holding instrumentation expression. */
9971 tree instrument_expr
= NULL
;
9973 if (location
== UNKNOWN_LOCATION
)
9974 location
= input_location
;
9979 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
9980 if (op0_int_operands
)
9981 op0
= remove_c_maybe_const_expr (op0
);
9982 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
9983 if (op1_int_operands
)
9984 op1
= remove_c_maybe_const_expr (op1
);
9985 int_operands
= (op0_int_operands
&& op1_int_operands
);
9988 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
9989 && TREE_CODE (orig_op1
) == INTEGER_CST
);
9990 int_const
= (int_const_or_overflow
9991 && !TREE_OVERFLOW (orig_op0
)
9992 && !TREE_OVERFLOW (orig_op1
));
9995 int_const
= int_const_or_overflow
= false;
9997 /* Do not apply default conversion in mixed vector/scalar expression. */
9999 && !((TREE_CODE (TREE_TYPE (op0
)) == VECTOR_TYPE
)
10000 != (TREE_CODE (TREE_TYPE (op1
)) == VECTOR_TYPE
)))
10002 op0
= default_conversion (op0
);
10003 op1
= default_conversion (op1
);
10006 /* When Cilk Plus is enabled and there are array notations inside op0, then
10007 we check to see if there are builtin array notation functions. If
10008 so, then we take on the type of the array notation inside it. */
10009 if (flag_enable_cilkplus
&& contains_array_notation_expr (op0
))
10010 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10012 orig_type0
= type0
= TREE_TYPE (op0
);
10014 if (flag_enable_cilkplus
&& contains_array_notation_expr (op1
))
10015 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10017 orig_type1
= type1
= TREE_TYPE (op1
);
10019 /* The expression codes of the data types of the arguments tell us
10020 whether the arguments are integers, floating, pointers, etc. */
10021 code0
= TREE_CODE (type0
);
10022 code1
= TREE_CODE (type1
);
10024 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10025 STRIP_TYPE_NOPS (op0
);
10026 STRIP_TYPE_NOPS (op1
);
10028 /* If an error was already reported for one of the arguments,
10029 avoid reporting another error. */
10031 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10032 return error_mark_node
;
10034 if ((invalid_op_diag
10035 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10037 error_at (location
, invalid_op_diag
);
10038 return error_mark_node
;
10046 case TRUNC_DIV_EXPR
:
10047 case CEIL_DIV_EXPR
:
10048 case FLOOR_DIV_EXPR
:
10049 case ROUND_DIV_EXPR
:
10050 case EXACT_DIV_EXPR
:
10051 may_need_excess_precision
= true;
10054 may_need_excess_precision
= false;
10057 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10059 op0
= TREE_OPERAND (op0
, 0);
10060 type0
= TREE_TYPE (op0
);
10062 else if (may_need_excess_precision
10063 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10066 op0
= convert (eptype
, op0
);
10068 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10070 op1
= TREE_OPERAND (op1
, 0);
10071 type1
= TREE_TYPE (op1
);
10073 else if (may_need_excess_precision
10074 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10077 op1
= convert (eptype
, op1
);
10080 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10082 /* In case when one of the operands of the binary operation is
10083 a vector and another is a scalar -- convert scalar to vector. */
10084 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10086 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10089 switch (convert_flag
)
10092 return error_mark_node
;
10095 bool maybe_const
= true;
10097 sc
= c_fully_fold (op0
, false, &maybe_const
);
10098 sc
= save_expr (sc
);
10099 sc
= convert (TREE_TYPE (type1
), sc
);
10100 op0
= build_vector_from_val (type1
, sc
);
10102 op0
= c_wrap_maybe_const (op0
, true);
10103 orig_type0
= type0
= TREE_TYPE (op0
);
10104 code0
= TREE_CODE (type0
);
10108 case stv_secondarg
:
10110 bool maybe_const
= true;
10112 sc
= c_fully_fold (op1
, false, &maybe_const
);
10113 sc
= save_expr (sc
);
10114 sc
= convert (TREE_TYPE (type0
), sc
);
10115 op1
= build_vector_from_val (type0
, sc
);
10117 op1
= c_wrap_maybe_const (op1
, true);
10118 orig_type1
= type1
= TREE_TYPE (op1
);
10119 code1
= TREE_CODE (type1
);
10131 /* Handle the pointer + int case. */
10132 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10134 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10135 goto return_build_binary_op
;
10137 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10139 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10140 goto return_build_binary_op
;
10147 /* Subtraction of two similar pointers.
10148 We must subtract them as integers, then divide by object size. */
10149 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10150 && comp_target_types (location
, type0
, type1
))
10152 ret
= pointer_diff (location
, op0
, op1
);
10153 goto return_build_binary_op
;
10155 /* Handle pointer minus int. Just like pointer plus int. */
10156 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10158 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10159 goto return_build_binary_op
;
10169 case TRUNC_DIV_EXPR
:
10170 case CEIL_DIV_EXPR
:
10171 case FLOOR_DIV_EXPR
:
10172 case ROUND_DIV_EXPR
:
10173 case EXACT_DIV_EXPR
:
10174 doing_div_or_mod
= true;
10175 warn_for_div_by_zero (location
, op1
);
10177 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10178 || code0
== FIXED_POINT_TYPE
10179 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10180 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10181 || code1
== FIXED_POINT_TYPE
10182 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10184 enum tree_code tcode0
= code0
, tcode1
= code1
;
10186 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10187 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10188 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10189 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10191 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10192 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10193 resultcode
= RDIV_EXPR
;
10195 /* Although it would be tempting to shorten always here, that
10196 loses on some targets, since the modulo instruction is
10197 undefined if the quotient can't be represented in the
10198 computation mode. We shorten only if unsigned or if
10199 dividing by something we know != -1. */
10200 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10201 || (TREE_CODE (op1
) == INTEGER_CST
10202 && !integer_all_onesp (op1
)));
10210 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10212 /* Allow vector types which are not floating point types. */
10213 else if (code0
== VECTOR_TYPE
10214 && code1
== VECTOR_TYPE
10215 && !VECTOR_FLOAT_TYPE_P (type0
)
10216 && !VECTOR_FLOAT_TYPE_P (type1
))
10220 case TRUNC_MOD_EXPR
:
10221 case FLOOR_MOD_EXPR
:
10222 doing_div_or_mod
= true;
10223 warn_for_div_by_zero (location
, op1
);
10225 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10226 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10227 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10229 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10231 /* Although it would be tempting to shorten always here, that loses
10232 on some targets, since the modulo instruction is undefined if the
10233 quotient can't be represented in the computation mode. We shorten
10234 only if unsigned or if dividing by something we know != -1. */
10235 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10236 || (TREE_CODE (op1
) == INTEGER_CST
10237 && !integer_all_onesp (op1
)));
10242 case TRUTH_ANDIF_EXPR
:
10243 case TRUTH_ORIF_EXPR
:
10244 case TRUTH_AND_EXPR
:
10245 case TRUTH_OR_EXPR
:
10246 case TRUTH_XOR_EXPR
:
10247 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
10248 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10249 || code0
== FIXED_POINT_TYPE
)
10250 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
10251 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10252 || code1
== FIXED_POINT_TYPE
))
10254 /* Result of these operations is always an int,
10255 but that does not mean the operands should be
10256 converted to ints! */
10257 result_type
= integer_type_node
;
10258 if (op0_int_operands
)
10260 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
10261 op0
= remove_c_maybe_const_expr (op0
);
10264 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
10265 if (op1_int_operands
)
10267 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
10268 op1
= remove_c_maybe_const_expr (op1
);
10271 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
10275 if (code
== TRUTH_ANDIF_EXPR
)
10277 int_const_or_overflow
= (int_operands
10278 && TREE_CODE (orig_op0
) == INTEGER_CST
10279 && (op0
== truthvalue_false_node
10280 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10281 int_const
= (int_const_or_overflow
10282 && !TREE_OVERFLOW (orig_op0
)
10283 && (op0
== truthvalue_false_node
10284 || !TREE_OVERFLOW (orig_op1
)));
10286 else if (code
== TRUTH_ORIF_EXPR
)
10288 int_const_or_overflow
= (int_operands
10289 && TREE_CODE (orig_op0
) == INTEGER_CST
10290 && (op0
== truthvalue_true_node
10291 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10292 int_const
= (int_const_or_overflow
10293 && !TREE_OVERFLOW (orig_op0
)
10294 && (op0
== truthvalue_true_node
10295 || !TREE_OVERFLOW (orig_op1
)));
10299 /* Shift operations: result has same type as first operand;
10300 always convert second operand to int.
10301 Also set SHORT_SHIFT if shifting rightward. */
10304 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10305 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10307 result_type
= type0
;
10310 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10311 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10312 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10313 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10315 result_type
= type0
;
10318 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10319 && code1
== INTEGER_TYPE
)
10321 doing_shift
= true;
10322 if (TREE_CODE (op1
) == INTEGER_CST
)
10324 if (tree_int_cst_sgn (op1
) < 0)
10327 if (c_inhibit_evaluation_warnings
== 0)
10328 warning (0, "right shift count is negative");
10332 if (!integer_zerop (op1
))
10335 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10338 if (c_inhibit_evaluation_warnings
== 0)
10339 warning (0, "right shift count >= width of type");
10344 /* Use the type of the value to be shifted. */
10345 result_type
= type0
;
10346 /* Convert the non vector shift-count to an integer, regardless
10347 of size of value being shifted. */
10348 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10349 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10350 op1
= convert (integer_type_node
, op1
);
10351 /* Avoid converting op1 to result_type later. */
10357 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10358 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10360 result_type
= type0
;
10363 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10364 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10365 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10366 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10368 result_type
= type0
;
10371 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10372 && code1
== INTEGER_TYPE
)
10374 doing_shift
= true;
10375 if (TREE_CODE (op1
) == INTEGER_CST
)
10377 if (tree_int_cst_sgn (op1
) < 0)
10380 if (c_inhibit_evaluation_warnings
== 0)
10381 warning (0, "left shift count is negative");
10384 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10387 if (c_inhibit_evaluation_warnings
== 0)
10388 warning (0, "left shift count >= width of type");
10392 /* Use the type of the value to be shifted. */
10393 result_type
= type0
;
10394 /* Convert the non vector shift-count to an integer, regardless
10395 of size of value being shifted. */
10396 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10397 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10398 op1
= convert (integer_type_node
, op1
);
10399 /* Avoid converting op1 to result_type later. */
10406 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10409 if (!vector_types_compatible_elements_p (type0
, type1
))
10411 error_at (location
, "comparing vectors with different "
10413 return error_mark_node
;
10416 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10418 error_at (location
, "comparing vectors with different "
10419 "number of elements");
10420 return error_mark_node
;
10423 /* Always construct signed integer vector type. */
10424 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10425 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10426 result_type
= build_opaque_vector_type (intt
,
10427 TYPE_VECTOR_SUBPARTS (type0
));
10431 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
10432 warning_at (location
,
10434 "comparing floating point with == or != is unsafe");
10435 /* Result of comparison is always int,
10436 but don't convert the args to int! */
10437 build_type
= integer_type_node
;
10438 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10439 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
10440 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10441 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
10443 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10445 if (TREE_CODE (op0
) == ADDR_EXPR
10446 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
10448 if (code
== EQ_EXPR
)
10449 warning_at (location
,
10451 "the comparison will always evaluate as %<false%> "
10452 "for the address of %qD will never be NULL",
10453 TREE_OPERAND (op0
, 0));
10455 warning_at (location
,
10457 "the comparison will always evaluate as %<true%> "
10458 "for the address of %qD will never be NULL",
10459 TREE_OPERAND (op0
, 0));
10461 result_type
= type0
;
10463 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10465 if (TREE_CODE (op1
) == ADDR_EXPR
10466 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
10468 if (code
== EQ_EXPR
)
10469 warning_at (location
,
10471 "the comparison will always evaluate as %<false%> "
10472 "for the address of %qD will never be NULL",
10473 TREE_OPERAND (op1
, 0));
10475 warning_at (location
,
10477 "the comparison will always evaluate as %<true%> "
10478 "for the address of %qD will never be NULL",
10479 TREE_OPERAND (op1
, 0));
10481 result_type
= type1
;
10483 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10485 tree tt0
= TREE_TYPE (type0
);
10486 tree tt1
= TREE_TYPE (type1
);
10487 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
10488 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
10489 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
10491 /* Anything compares with void *. void * compares with anything.
10492 Otherwise, the targets must be compatible
10493 and both must be object or both incomplete. */
10494 if (comp_target_types (location
, type0
, type1
))
10495 result_type
= common_pointer_type (type0
, type1
);
10496 else if (!addr_space_superset (as0
, as1
, &as_common
))
10498 error_at (location
, "comparison of pointers to "
10499 "disjoint address spaces");
10500 return error_mark_node
;
10502 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
10504 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
10505 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10506 "comparison of %<void *%> with function pointer");
10508 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
10510 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
10511 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10512 "comparison of %<void *%> with function pointer");
10515 /* Avoid warning about the volatile ObjC EH puts on decls. */
10517 pedwarn (location
, 0,
10518 "comparison of distinct pointer types lacks a cast");
10520 if (result_type
== NULL_TREE
)
10522 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10523 result_type
= build_pointer_type
10524 (build_qualified_type (void_type_node
, qual
));
10527 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10529 result_type
= type0
;
10530 pedwarn (location
, 0, "comparison between pointer and integer");
10532 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10534 result_type
= type1
;
10535 pedwarn (location
, 0, "comparison between pointer and integer");
10543 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10546 if (!vector_types_compatible_elements_p (type0
, type1
))
10548 error_at (location
, "comparing vectors with different "
10550 return error_mark_node
;
10553 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10555 error_at (location
, "comparing vectors with different "
10556 "number of elements");
10557 return error_mark_node
;
10560 /* Always construct signed integer vector type. */
10561 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10562 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10563 result_type
= build_opaque_vector_type (intt
,
10564 TYPE_VECTOR_SUBPARTS (type0
));
10568 build_type
= integer_type_node
;
10569 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10570 || code0
== FIXED_POINT_TYPE
)
10571 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10572 || code1
== FIXED_POINT_TYPE
))
10574 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10576 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
10577 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
10578 addr_space_t as_common
;
10580 if (comp_target_types (location
, type0
, type1
))
10582 result_type
= common_pointer_type (type0
, type1
);
10583 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
10584 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
10585 pedwarn (location
, 0,
10586 "comparison of complete and incomplete pointers");
10587 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
10588 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10589 "ordered comparisons of pointers to functions");
10590 else if (null_pointer_constant_p (orig_op0
)
10591 || null_pointer_constant_p (orig_op1
))
10592 warning_at (location
, OPT_Wextra
,
10593 "ordered comparison of pointer with null pointer");
10596 else if (!addr_space_superset (as0
, as1
, &as_common
))
10598 error_at (location
, "comparison of pointers to "
10599 "disjoint address spaces");
10600 return error_mark_node
;
10604 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10605 result_type
= build_pointer_type
10606 (build_qualified_type (void_type_node
, qual
));
10607 pedwarn (location
, 0,
10608 "comparison of distinct pointer types lacks a cast");
10611 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10613 result_type
= type0
;
10615 pedwarn (location
, OPT_Wpedantic
,
10616 "ordered comparison of pointer with integer zero");
10617 else if (extra_warnings
)
10618 warning_at (location
, OPT_Wextra
,
10619 "ordered comparison of pointer with integer zero");
10621 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10623 result_type
= type1
;
10625 pedwarn (location
, OPT_Wpedantic
,
10626 "ordered comparison of pointer with integer zero");
10627 else if (extra_warnings
)
10628 warning_at (location
, OPT_Wextra
,
10629 "ordered comparison of pointer with integer zero");
10631 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10633 result_type
= type0
;
10634 pedwarn (location
, 0, "comparison between pointer and integer");
10636 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10638 result_type
= type1
;
10639 pedwarn (location
, 0, "comparison between pointer and integer");
10644 gcc_unreachable ();
10647 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10648 return error_mark_node
;
10650 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10651 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
10652 || !vector_types_compatible_elements_p (type0
, type1
)))
10654 binary_op_error (location
, code
, type0
, type1
);
10655 return error_mark_node
;
10658 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10659 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
10661 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10662 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
10664 bool first_complex
= (code0
== COMPLEX_TYPE
);
10665 bool second_complex
= (code1
== COMPLEX_TYPE
);
10666 int none_complex
= (!first_complex
&& !second_complex
);
10668 if (shorten
|| common
|| short_compare
)
10670 result_type
= c_common_type (type0
, type1
);
10671 do_warn_double_promotion (result_type
, type0
, type1
,
10672 "implicit conversion from %qT to %qT "
10673 "to match other operand of binary "
10676 if (result_type
== error_mark_node
)
10677 return error_mark_node
;
10680 if (first_complex
!= second_complex
10681 && (code
== PLUS_EXPR
10682 || code
== MINUS_EXPR
10683 || code
== MULT_EXPR
10684 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
10685 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
10686 && flag_signed_zeros
)
10688 /* An operation on mixed real/complex operands must be
10689 handled specially, but the language-independent code can
10690 more easily optimize the plain complex arithmetic if
10691 -fno-signed-zeros. */
10692 tree real_type
= TREE_TYPE (result_type
);
10694 if (type0
!= orig_type0
|| type1
!= orig_type1
)
10696 gcc_assert (may_need_excess_precision
&& common
);
10697 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
10701 if (TREE_TYPE (op0
) != result_type
)
10702 op0
= convert_and_check (result_type
, op0
);
10703 if (TREE_TYPE (op1
) != real_type
)
10704 op1
= convert_and_check (real_type
, op1
);
10708 if (TREE_TYPE (op0
) != real_type
)
10709 op0
= convert_and_check (real_type
, op0
);
10710 if (TREE_TYPE (op1
) != result_type
)
10711 op1
= convert_and_check (result_type
, op1
);
10713 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
10714 return error_mark_node
;
10717 op0
= c_save_expr (op0
);
10718 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
10720 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
10725 case TRUNC_DIV_EXPR
:
10726 op1
= c_save_expr (op1
);
10727 imag
= build2 (resultcode
, real_type
, imag
, op1
);
10728 /* Fall through. */
10731 real
= build2 (resultcode
, real_type
, real
, op1
);
10739 op1
= c_save_expr (op1
);
10740 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
10742 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
10747 op0
= c_save_expr (op0
);
10748 imag
= build2 (resultcode
, real_type
, op0
, imag
);
10749 /* Fall through. */
10751 real
= build2 (resultcode
, real_type
, op0
, real
);
10754 real
= build2 (resultcode
, real_type
, op0
, real
);
10755 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
10761 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
10762 goto return_build_binary_op
;
10765 /* For certain operations (which identify themselves by shorten != 0)
10766 if both args were extended from the same smaller type,
10767 do the arithmetic in that type and then extend.
10769 shorten !=0 and !=1 indicates a bitwise operation.
10770 For them, this optimization is safe only if
10771 both args are zero-extended or both are sign-extended.
10772 Otherwise, we might change the result.
10773 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10774 but calculated in (unsigned short) it would be (unsigned short)-1. */
10776 if (shorten
&& none_complex
)
10778 final_type
= result_type
;
10779 result_type
= shorten_binary_op (result_type
, op0
, op1
,
10783 /* Shifts can be shortened if shifting right. */
10788 tree arg0
= get_narrower (op0
, &unsigned_arg
);
10790 final_type
= result_type
;
10792 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
10793 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
10795 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
10796 && tree_int_cst_sgn (op1
) > 0
10797 /* We can shorten only if the shift count is less than the
10798 number of bits in the smaller type size. */
10799 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
10800 /* We cannot drop an unsigned shift after sign-extension. */
10801 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
10803 /* Do an unsigned shift if the operand was zero-extended. */
10805 = c_common_signed_or_unsigned_type (unsigned_arg
,
10807 /* Convert value-to-be-shifted to that type. */
10808 if (TREE_TYPE (op0
) != result_type
)
10809 op0
= convert (result_type
, op0
);
10814 /* Comparison operations are shortened too but differently.
10815 They identify themselves by setting short_compare = 1. */
10819 /* Don't write &op0, etc., because that would prevent op0
10820 from being kept in a register.
10821 Instead, make copies of the our local variables and
10822 pass the copies by reference, then copy them back afterward. */
10823 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
10824 enum tree_code xresultcode
= resultcode
;
10826 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
10831 goto return_build_binary_op
;
10834 op0
= xop0
, op1
= xop1
;
10836 resultcode
= xresultcode
;
10838 if (c_inhibit_evaluation_warnings
== 0)
10840 bool op0_maybe_const
= true;
10841 bool op1_maybe_const
= true;
10842 tree orig_op0_folded
, orig_op1_folded
;
10844 if (in_late_binary_op
)
10846 orig_op0_folded
= orig_op0
;
10847 orig_op1_folded
= orig_op1
;
10851 /* Fold for the sake of possible warnings, as in
10852 build_conditional_expr. This requires the
10853 "original" values to be folded, not just op0 and
10855 c_inhibit_evaluation_warnings
++;
10856 op0
= c_fully_fold (op0
, require_constant_value
,
10858 op1
= c_fully_fold (op1
, require_constant_value
,
10860 c_inhibit_evaluation_warnings
--;
10861 orig_op0_folded
= c_fully_fold (orig_op0
,
10862 require_constant_value
,
10864 orig_op1_folded
= c_fully_fold (orig_op1
,
10865 require_constant_value
,
10869 if (warn_sign_compare
)
10870 warn_for_sign_compare (location
, orig_op0_folded
,
10871 orig_op1_folded
, op0
, op1
,
10872 result_type
, resultcode
);
10873 if (!in_late_binary_op
&& !int_operands
)
10875 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
10876 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
10877 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
10878 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
10884 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10885 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10886 Then the expression will be built.
10887 It will be given type FINAL_TYPE if that is nonzero;
10888 otherwise, it will be given type RESULT_TYPE. */
10892 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
10893 return error_mark_node
;
10896 if (build_type
== NULL_TREE
)
10898 build_type
= result_type
;
10899 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
10902 gcc_assert (may_need_excess_precision
&& common
);
10903 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
10909 op0
= ep_convert_and_check (result_type
, op0
, semantic_result_type
);
10910 op1
= ep_convert_and_check (result_type
, op1
, semantic_result_type
);
10912 /* This can happen if one operand has a vector type, and the other
10913 has a different type. */
10914 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
10915 return error_mark_node
;
10918 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
))
10919 && current_function_decl
!= 0
10920 && !lookup_attribute ("no_sanitize_undefined",
10921 DECL_ATTRIBUTES (current_function_decl
))
10922 && (doing_div_or_mod
|| doing_shift
))
10924 /* OP0 and/or OP1 might have side-effects. */
10925 op0
= c_save_expr (op0
);
10926 op1
= c_save_expr (op1
);
10927 op0
= c_fully_fold (op0
, false, NULL
);
10928 op1
= c_fully_fold (op1
, false, NULL
);
10929 if (doing_div_or_mod
&& (flag_sanitize
& SANITIZE_DIVIDE
))
10930 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
10931 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
10932 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
10935 /* Treat expressions in initializers specially as they can't trap. */
10936 if (int_const_or_overflow
)
10937 ret
= (require_constant_value
10938 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
10940 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
10942 ret
= build2 (resultcode
, build_type
, op0
, op1
);
10943 if (final_type
!= 0)
10944 ret
= convert (final_type
, ret
);
10946 return_build_binary_op
:
10947 gcc_assert (ret
!= error_mark_node
);
10948 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
10949 ret
= (int_operands
10950 ? note_integer_operands (ret
)
10951 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
10952 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
10953 && !in_late_binary_op
)
10954 ret
= note_integer_operands (ret
);
10955 if (semantic_result_type
)
10956 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
10957 protected_set_expr_location (ret
, location
);
10959 if (instrument_expr
!= NULL
)
10960 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
10961 instrument_expr
, ret
);
10967 /* Convert EXPR to be a truth-value, validating its type for this
10968 purpose. LOCATION is the source location for the expression. */
10971 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
10973 bool int_const
, int_operands
;
10975 switch (TREE_CODE (TREE_TYPE (expr
)))
10978 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
10979 return error_mark_node
;
10982 error_at (location
, "used struct type value where scalar is required");
10983 return error_mark_node
;
10986 error_at (location
, "used union type value where scalar is required");
10987 return error_mark_node
;
10990 error_at (location
, "void value not ignored as it ought to be");
10991 return error_mark_node
;
10993 case FUNCTION_TYPE
:
10994 gcc_unreachable ();
10997 error_at (location
, "used vector type where scalar is required");
10998 return error_mark_node
;
11004 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11005 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11006 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11008 expr
= remove_c_maybe_const_expr (expr
);
11009 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11010 convert (TREE_TYPE (expr
), integer_zero_node
));
11011 expr
= note_integer_operands (expr
);
11014 /* ??? Should we also give an error for vectors rather than leaving
11015 those to give errors later? */
11016 expr
= c_common_truthvalue_conversion (location
, expr
);
11018 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11020 if (TREE_OVERFLOW (expr
))
11023 return note_integer_operands (expr
);
11025 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11026 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11031 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11035 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11037 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11039 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11040 /* Executing a compound literal inside a function reinitializes
11042 if (!TREE_STATIC (decl
))
11050 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11053 c_begin_omp_parallel (void)
11057 keep_next_level ();
11058 block
= c_begin_compound_stmt (true);
11063 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11064 statement. LOC is the location of the OMP_PARALLEL. */
11067 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11071 block
= c_end_compound_stmt (loc
, block
, true);
11073 stmt
= make_node (OMP_PARALLEL
);
11074 TREE_TYPE (stmt
) = void_type_node
;
11075 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11076 OMP_PARALLEL_BODY (stmt
) = block
;
11077 SET_EXPR_LOCATION (stmt
, loc
);
11079 return add_stmt (stmt
);
11082 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11085 c_begin_omp_task (void)
11089 keep_next_level ();
11090 block
= c_begin_compound_stmt (true);
11095 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11096 statement. LOC is the location of the #pragma. */
11099 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11103 block
= c_end_compound_stmt (loc
, block
, true);
11105 stmt
= make_node (OMP_TASK
);
11106 TREE_TYPE (stmt
) = void_type_node
;
11107 OMP_TASK_CLAUSES (stmt
) = clauses
;
11108 OMP_TASK_BODY (stmt
) = block
;
11109 SET_EXPR_LOCATION (stmt
, loc
);
11111 return add_stmt (stmt
);
11114 /* Generate GOMP_cancel call for #pragma omp cancel. */
11117 c_finish_omp_cancel (location_t loc
, tree clauses
)
11119 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
11121 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11123 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11125 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11127 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11131 error_at (loc
, "%<#pragma omp cancel must specify one of "
11132 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11136 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
11137 if (ifc
!= NULL_TREE
)
11139 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
11140 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
11141 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
11142 build_zero_cst (type
));
11145 ifc
= boolean_true_node
;
11146 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
11147 build_int_cst (integer_type_node
, mask
),
11152 /* Generate GOMP_cancellation_point call for
11153 #pragma omp cancellation point. */
11156 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
11158 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
11160 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11162 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11164 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11166 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11170 error_at (loc
, "%<#pragma omp cancellation point must specify one of "
11171 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11175 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
11176 build_int_cst (integer_type_node
, mask
));
11180 /* Helper function for handle_omp_array_sections. Called recursively
11181 to handle multiple array-section-subscripts. C is the clause,
11182 T current expression (initially OMP_CLAUSE_DECL), which is either
11183 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11184 expression if specified, TREE_VALUE length expression if specified,
11185 TREE_CHAIN is what it has been specified after, or some decl.
11186 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11187 set to true if any of the array-section-subscript could have length
11188 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11189 first array-section-subscript which is known not to have length
11191 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11192 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11193 all are or may have length of 1, array-section-subscript [:2] is the
11194 first one knonwn not to have length 1. For array-section-subscript
11195 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11196 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11197 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11198 case though, as some lengths could be zero. */
11201 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
11202 bool &maybe_zero_len
, unsigned int &first_non_one
)
11204 tree ret
, low_bound
, length
, type
;
11205 if (TREE_CODE (t
) != TREE_LIST
)
11207 if (t
== error_mark_node
|| TREE_TYPE (t
) == error_mark_node
)
11208 return error_mark_node
;
11209 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11212 error_at (OMP_CLAUSE_LOCATION (c
),
11213 "%qD is not a variable in %qs clause", t
,
11214 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11216 error_at (OMP_CLAUSE_LOCATION (c
),
11217 "%qE is not a variable in %qs clause", t
,
11218 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11219 return error_mark_node
;
11221 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11222 && TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11224 error_at (OMP_CLAUSE_LOCATION (c
),
11225 "%qD is threadprivate variable in %qs clause", t
,
11226 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11227 return error_mark_node
;
11232 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
11233 maybe_zero_len
, first_non_one
);
11234 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
11237 type
= TREE_TYPE (ret
);
11238 low_bound
= TREE_PURPOSE (t
);
11239 length
= TREE_VALUE (t
);
11241 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
11242 return error_mark_node
;
11244 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
11246 error_at (OMP_CLAUSE_LOCATION (c
),
11247 "low bound %qE of array section does not have integral type",
11249 return error_mark_node
;
11251 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
11253 error_at (OMP_CLAUSE_LOCATION (c
),
11254 "length %qE of array section does not have integral type",
11256 return error_mark_node
;
11259 && TREE_CODE (low_bound
) == INTEGER_CST
11260 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11261 > TYPE_PRECISION (sizetype
))
11262 low_bound
= fold_convert (sizetype
, low_bound
);
11264 && TREE_CODE (length
) == INTEGER_CST
11265 && TYPE_PRECISION (TREE_TYPE (length
))
11266 > TYPE_PRECISION (sizetype
))
11267 length
= fold_convert (sizetype
, length
);
11268 if (low_bound
== NULL_TREE
)
11269 low_bound
= integer_zero_node
;
11271 if (length
!= NULL_TREE
)
11273 if (!integer_nonzerop (length
))
11274 maybe_zero_len
= true;
11275 if (first_non_one
== types
.length ()
11276 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
11279 if (TREE_CODE (type
) == ARRAY_TYPE
)
11281 if (length
== NULL_TREE
11282 && (TYPE_DOMAIN (type
) == NULL_TREE
11283 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
11285 error_at (OMP_CLAUSE_LOCATION (c
),
11286 "for unknown bound array type length expression must "
11288 return error_mark_node
;
11290 if (TREE_CODE (low_bound
) == INTEGER_CST
11291 && tree_int_cst_sgn (low_bound
) == -1)
11293 error_at (OMP_CLAUSE_LOCATION (c
),
11294 "negative low bound in array section in %qs clause",
11295 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11296 return error_mark_node
;
11298 if (length
!= NULL_TREE
11299 && TREE_CODE (length
) == INTEGER_CST
11300 && tree_int_cst_sgn (length
) == -1)
11302 error_at (OMP_CLAUSE_LOCATION (c
),
11303 "negative length in array section in %qs clause",
11304 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11305 return error_mark_node
;
11307 if (TYPE_DOMAIN (type
)
11308 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
11309 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
11312 tree size
= size_binop (PLUS_EXPR
,
11313 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11315 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11317 if (tree_int_cst_lt (size
, low_bound
))
11319 error_at (OMP_CLAUSE_LOCATION (c
),
11320 "low bound %qE above array section size "
11321 "in %qs clause", low_bound
,
11322 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11323 return error_mark_node
;
11325 if (tree_int_cst_equal (size
, low_bound
))
11326 maybe_zero_len
= true;
11327 else if (length
== NULL_TREE
11328 && first_non_one
== types
.length ()
11329 && tree_int_cst_equal
11330 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11334 else if (length
== NULL_TREE
)
11336 maybe_zero_len
= true;
11337 if (first_non_one
== types
.length ())
11340 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
11342 if (tree_int_cst_lt (size
, length
))
11344 error_at (OMP_CLAUSE_LOCATION (c
),
11345 "length %qE above array section size "
11346 "in %qs clause", length
,
11347 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11348 return error_mark_node
;
11350 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11353 = size_binop (PLUS_EXPR
,
11354 fold_convert (sizetype
, low_bound
),
11355 fold_convert (sizetype
, length
));
11356 if (TREE_CODE (lbpluslen
) == INTEGER_CST
11357 && tree_int_cst_lt (size
, lbpluslen
))
11359 error_at (OMP_CLAUSE_LOCATION (c
),
11360 "high bound %qE above array section size "
11361 "in %qs clause", lbpluslen
,
11362 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11363 return error_mark_node
;
11368 else if (length
== NULL_TREE
)
11370 maybe_zero_len
= true;
11371 if (first_non_one
== types
.length ())
11375 /* For [lb:] we will need to evaluate lb more than once. */
11376 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11378 tree lb
= c_save_expr (low_bound
);
11379 if (lb
!= low_bound
)
11381 TREE_PURPOSE (t
) = lb
;
11386 else if (TREE_CODE (type
) == POINTER_TYPE
)
11388 if (length
== NULL_TREE
)
11390 error_at (OMP_CLAUSE_LOCATION (c
),
11391 "for pointer type length expression must be specified");
11392 return error_mark_node
;
11394 /* If there is a pointer type anywhere but in the very first
11395 array-section-subscript, the array section can't be contiguous. */
11396 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11397 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
11399 error_at (OMP_CLAUSE_LOCATION (c
),
11400 "array section is not contiguous in %qs clause",
11401 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11402 return error_mark_node
;
11407 error_at (OMP_CLAUSE_LOCATION (c
),
11408 "%qE does not have pointer or array type", ret
);
11409 return error_mark_node
;
11411 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11412 types
.safe_push (TREE_TYPE (ret
));
11413 /* We will need to evaluate lb more than once. */
11414 tree lb
= c_save_expr (low_bound
);
11415 if (lb
!= low_bound
)
11417 TREE_PURPOSE (t
) = lb
;
11420 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
11424 /* Handle array sections for clause C. */
11427 handle_omp_array_sections (tree c
)
11429 bool maybe_zero_len
= false;
11430 unsigned int first_non_one
= 0;
11431 vec
<tree
> types
= vNULL
;
11432 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
11433 maybe_zero_len
, first_non_one
);
11434 if (first
== error_mark_node
)
11439 if (first
== NULL_TREE
)
11444 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
11446 tree t
= OMP_CLAUSE_DECL (c
);
11447 tree tem
= NULL_TREE
;
11449 /* Need to evaluate side effects in the length expressions
11451 while (TREE_CODE (t
) == TREE_LIST
)
11453 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
11455 if (tem
== NULL_TREE
)
11456 tem
= TREE_VALUE (t
);
11458 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
11459 TREE_VALUE (t
), tem
);
11461 t
= TREE_CHAIN (t
);
11464 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
11465 first
= c_fully_fold (first
, false, NULL
);
11466 OMP_CLAUSE_DECL (c
) = first
;
11470 unsigned int num
= types
.length (), i
;
11471 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
11472 tree condition
= NULL_TREE
;
11474 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
11475 maybe_zero_len
= true;
11477 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
11478 t
= TREE_CHAIN (t
))
11480 tree low_bound
= TREE_PURPOSE (t
);
11481 tree length
= TREE_VALUE (t
);
11485 && TREE_CODE (low_bound
) == INTEGER_CST
11486 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11487 > TYPE_PRECISION (sizetype
))
11488 low_bound
= fold_convert (sizetype
, low_bound
);
11490 && TREE_CODE (length
) == INTEGER_CST
11491 && TYPE_PRECISION (TREE_TYPE (length
))
11492 > TYPE_PRECISION (sizetype
))
11493 length
= fold_convert (sizetype
, length
);
11494 if (low_bound
== NULL_TREE
)
11495 low_bound
= integer_zero_node
;
11496 if (!maybe_zero_len
&& i
> first_non_one
)
11498 if (integer_nonzerop (low_bound
))
11499 goto do_warn_noncontiguous
;
11500 if (length
!= NULL_TREE
11501 && TREE_CODE (length
) == INTEGER_CST
11502 && TYPE_DOMAIN (types
[i
])
11503 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
11504 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
11508 size
= size_binop (PLUS_EXPR
,
11509 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11511 if (!tree_int_cst_equal (length
, size
))
11513 do_warn_noncontiguous
:
11514 error_at (OMP_CLAUSE_LOCATION (c
),
11515 "array section is not contiguous in %qs "
11517 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11522 if (length
!= NULL_TREE
11523 && TREE_SIDE_EFFECTS (length
))
11525 if (side_effects
== NULL_TREE
)
11526 side_effects
= length
;
11528 side_effects
= build2 (COMPOUND_EXPR
,
11529 TREE_TYPE (side_effects
),
11530 length
, side_effects
);
11537 if (i
> first_non_one
&& length
&& integer_nonzerop (length
))
11540 l
= fold_convert (sizetype
, length
);
11543 l
= size_binop (PLUS_EXPR
,
11544 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11546 l
= size_binop (MINUS_EXPR
, l
,
11547 fold_convert (sizetype
, low_bound
));
11549 if (i
> first_non_one
)
11551 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
11553 if (condition
== NULL_TREE
)
11556 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
11559 else if (size
== NULL_TREE
)
11561 size
= size_in_bytes (TREE_TYPE (types
[i
]));
11562 size
= size_binop (MULT_EXPR
, size
, l
);
11564 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
11565 size
, size_zero_node
);
11568 size
= size_binop (MULT_EXPR
, size
, l
);
11573 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
11574 first
= c_fully_fold (first
, false, NULL
);
11575 OMP_CLAUSE_DECL (c
) = first
;
11577 size
= c_fully_fold (size
, false, NULL
);
11578 OMP_CLAUSE_SIZE (c
) = size
;
11579 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
11581 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
11582 OMP_CLAUSE_MAP_KIND (c2
) = OMP_CLAUSE_MAP_POINTER
;
11583 if (!c_mark_addressable (t
))
11585 OMP_CLAUSE_DECL (c2
) = t
;
11586 t
= build_fold_addr_expr (first
);
11587 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
11588 tree ptr
= OMP_CLAUSE_DECL (c2
);
11589 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
11590 ptr
= build_fold_addr_expr (ptr
);
11591 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11592 ptrdiff_type_node
, t
,
11593 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
11594 ptrdiff_type_node
, ptr
));
11595 t
= c_fully_fold (t
, false, NULL
);
11596 OMP_CLAUSE_SIZE (c2
) = t
;
11597 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
11598 OMP_CLAUSE_CHAIN (c
) = c2
;
11603 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11604 an inline call. But, remap
11605 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11606 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11609 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
11610 tree decl
, tree placeholder
)
11613 struct pointer_map_t
*decl_map
= pointer_map_create ();
11615 *pointer_map_insert (decl_map
, omp_decl1
) = placeholder
;
11616 *pointer_map_insert (decl_map
, omp_decl2
) = decl
;
11617 memset (&id
, 0, sizeof (id
));
11618 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
11619 id
.dst_fn
= current_function_decl
;
11620 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
11621 id
.decl_map
= decl_map
;
11623 id
.copy_decl
= copy_decl_no_change
;
11624 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
11625 id
.transform_new_cfg
= true;
11626 id
.transform_return_to_modify
= false;
11627 id
.transform_lang_insert_block
= NULL
;
11629 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
11630 pointer_map_destroy (decl_map
);
11634 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11635 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11638 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
11640 if (*tp
== (tree
) data
)
11645 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11646 Remove any elements from the list that are invalid. */
11649 c_finish_omp_clauses (tree clauses
)
11651 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
11652 bitmap_head aligned_head
;
11653 tree c
, t
, *pc
= &clauses
;
11654 bool branch_seen
= false;
11655 bool copyprivate_seen
= false;
11656 tree
*nowait_clause
= NULL
;
11658 bitmap_obstack_initialize (NULL
);
11659 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
11660 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
11661 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
11662 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
11664 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
11666 bool remove
= false;
11667 bool need_complete
= false;
11668 bool need_implicitly_determined
= false;
11670 switch (OMP_CLAUSE_CODE (c
))
11672 case OMP_CLAUSE_SHARED
:
11673 need_implicitly_determined
= true;
11674 goto check_dup_generic
;
11676 case OMP_CLAUSE_PRIVATE
:
11677 need_complete
= true;
11678 need_implicitly_determined
= true;
11679 goto check_dup_generic
;
11681 case OMP_CLAUSE_REDUCTION
:
11682 need_implicitly_determined
= true;
11683 t
= OMP_CLAUSE_DECL (c
);
11684 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
11685 && FLOAT_TYPE_P (TREE_TYPE (t
)))
11687 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
11688 const char *r_name
= NULL
;
11707 case TRUTH_ANDIF_EXPR
:
11710 case TRUTH_ORIF_EXPR
:
11714 gcc_unreachable ();
11718 error_at (OMP_CLAUSE_LOCATION (c
),
11719 "%qE has invalid type for %<reduction(%s)%>",
11725 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
11727 error_at (OMP_CLAUSE_LOCATION (c
),
11728 "user defined reduction not found for %qD", t
);
11732 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
11734 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
11735 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
11736 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
11737 VAR_DECL
, NULL_TREE
, type
);
11738 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
11739 DECL_ARTIFICIAL (placeholder
) = 1;
11740 DECL_IGNORED_P (placeholder
) = 1;
11741 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
11742 c_mark_addressable (placeholder
);
11743 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
11744 c_mark_addressable (OMP_CLAUSE_DECL (c
));
11745 OMP_CLAUSE_REDUCTION_MERGE (c
)
11746 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
11747 TREE_VEC_ELT (list
, 0),
11748 TREE_VEC_ELT (list
, 1),
11749 OMP_CLAUSE_DECL (c
), placeholder
);
11750 OMP_CLAUSE_REDUCTION_MERGE (c
)
11751 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
11752 void_type_node
, NULL_TREE
,
11753 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
11754 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
11755 if (TREE_VEC_LENGTH (list
) == 6)
11757 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
11758 c_mark_addressable (OMP_CLAUSE_DECL (c
));
11759 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
11760 c_mark_addressable (placeholder
);
11761 tree init
= TREE_VEC_ELT (list
, 5);
11762 if (init
== error_mark_node
)
11763 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
11764 OMP_CLAUSE_REDUCTION_INIT (c
)
11765 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
11766 TREE_VEC_ELT (list
, 3),
11767 OMP_CLAUSE_DECL (c
), placeholder
);
11768 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
11769 OMP_CLAUSE_REDUCTION_INIT (c
)
11770 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
,
11771 OMP_CLAUSE_REDUCTION_INIT (c
));
11772 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
11773 c_find_omp_placeholder_r
,
11774 placeholder
, NULL
))
11775 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
11780 if (AGGREGATE_TYPE_P (TREE_TYPE (t
)))
11781 init
= build_constructor (TREE_TYPE (t
), NULL
);
11783 init
= fold_convert (TREE_TYPE (t
), integer_zero_node
);
11784 OMP_CLAUSE_REDUCTION_INIT (c
)
11785 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
, init
);
11787 OMP_CLAUSE_REDUCTION_INIT (c
)
11788 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
11789 void_type_node
, NULL_TREE
,
11790 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
11791 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
11793 goto check_dup_generic
;
11795 case OMP_CLAUSE_COPYPRIVATE
:
11796 copyprivate_seen
= true;
11799 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
11800 "%<nowait%> clause must not be used together "
11801 "with %<copyprivate%>");
11802 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
11803 nowait_clause
= NULL
;
11805 goto check_dup_generic
;
11807 case OMP_CLAUSE_COPYIN
:
11808 t
= OMP_CLAUSE_DECL (c
);
11809 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
11811 error_at (OMP_CLAUSE_LOCATION (c
),
11812 "%qE must be %<threadprivate%> for %<copyin%>", t
);
11816 goto check_dup_generic
;
11818 case OMP_CLAUSE_LINEAR
:
11819 t
= OMP_CLAUSE_DECL (c
);
11820 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
11821 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
11823 error_at (OMP_CLAUSE_LOCATION (c
),
11824 "linear clause applied to non-integral non-pointer "
11825 "variable with type %qT", TREE_TYPE (t
));
11829 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
11831 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
11832 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
11833 OMP_CLAUSE_DECL (c
), s
);
11834 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11835 sizetype
, s
, OMP_CLAUSE_DECL (c
));
11836 if (s
== error_mark_node
)
11838 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
11840 goto check_dup_generic
;
11843 t
= OMP_CLAUSE_DECL (c
);
11844 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11846 error_at (OMP_CLAUSE_LOCATION (c
),
11847 "%qE is not a variable in clause %qs", t
,
11848 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11851 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
11852 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
11853 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
11855 error_at (OMP_CLAUSE_LOCATION (c
),
11856 "%qE appears more than once in data clauses", t
);
11860 bitmap_set_bit (&generic_head
, DECL_UID (t
));
11863 case OMP_CLAUSE_FIRSTPRIVATE
:
11864 t
= OMP_CLAUSE_DECL (c
);
11865 need_complete
= true;
11866 need_implicitly_determined
= true;
11867 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11869 error_at (OMP_CLAUSE_LOCATION (c
),
11870 "%qE is not a variable in clause %<firstprivate%>", t
);
11873 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
11874 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
11876 error_at (OMP_CLAUSE_LOCATION (c
),
11877 "%qE appears more than once in data clauses", t
);
11881 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
11884 case OMP_CLAUSE_LASTPRIVATE
:
11885 t
= OMP_CLAUSE_DECL (c
);
11886 need_complete
= true;
11887 need_implicitly_determined
= true;
11888 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11890 error_at (OMP_CLAUSE_LOCATION (c
),
11891 "%qE is not a variable in clause %<lastprivate%>", t
);
11894 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
11895 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
11897 error_at (OMP_CLAUSE_LOCATION (c
),
11898 "%qE appears more than once in data clauses", t
);
11902 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
11905 case OMP_CLAUSE_ALIGNED
:
11906 t
= OMP_CLAUSE_DECL (c
);
11907 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11909 error_at (OMP_CLAUSE_LOCATION (c
),
11910 "%qE is not a variable in %<aligned%> clause", t
);
11913 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
11914 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
11916 error_at (OMP_CLAUSE_LOCATION (c
),
11917 "%qE in %<aligned%> clause is neither a pointer nor "
11921 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
11923 error_at (OMP_CLAUSE_LOCATION (c
),
11924 "%qE appears more than once in %<aligned%> clauses",
11929 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
11932 case OMP_CLAUSE_DEPEND
:
11933 t
= OMP_CLAUSE_DECL (c
);
11934 if (TREE_CODE (t
) == TREE_LIST
)
11936 if (handle_omp_array_sections (c
))
11940 if (t
== error_mark_node
)
11942 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11944 error_at (OMP_CLAUSE_LOCATION (c
),
11945 "%qE is not a variable in %<depend%> clause", t
);
11948 else if (!c_mark_addressable (t
))
11952 case OMP_CLAUSE_MAP
:
11953 case OMP_CLAUSE_TO
:
11954 case OMP_CLAUSE_FROM
:
11955 t
= OMP_CLAUSE_DECL (c
);
11956 if (TREE_CODE (t
) == TREE_LIST
)
11958 if (handle_omp_array_sections (c
))
11962 t
= OMP_CLAUSE_DECL (c
);
11963 if (!COMPLETE_TYPE_P (TREE_TYPE (t
)))
11965 error_at (OMP_CLAUSE_LOCATION (c
),
11966 "array section does not have mappable type "
11968 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11974 if (t
== error_mark_node
)
11976 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11978 error_at (OMP_CLAUSE_LOCATION (c
),
11979 "%qE is not a variable in %qs clause", t
,
11980 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11983 else if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11985 error_at (OMP_CLAUSE_LOCATION (c
),
11986 "%qD is threadprivate variable in %qs clause", t
,
11987 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11990 else if (!c_mark_addressable (t
))
11992 else if (!COMPLETE_TYPE_P (TREE_TYPE (t
))
11993 && !(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
11994 && OMP_CLAUSE_MAP_KIND (c
) == OMP_CLAUSE_MAP_POINTER
))
11996 error_at (OMP_CLAUSE_LOCATION (c
),
11997 "%qD does not have a mappable type in %qs clause", t
,
11998 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12001 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
12003 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
12004 error ("%qD appears more than once in motion clauses", t
);
12006 error ("%qD appears more than once in map clauses", t
);
12010 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12013 case OMP_CLAUSE_UNIFORM
:
12014 t
= OMP_CLAUSE_DECL (c
);
12015 if (TREE_CODE (t
) != PARM_DECL
)
12018 error_at (OMP_CLAUSE_LOCATION (c
),
12019 "%qD is not an argument in %<uniform%> clause", t
);
12021 error_at (OMP_CLAUSE_LOCATION (c
),
12022 "%qE is not an argument in %<uniform%> clause", t
);
12026 goto check_dup_generic
;
12028 case OMP_CLAUSE_NOWAIT
:
12029 if (copyprivate_seen
)
12031 error_at (OMP_CLAUSE_LOCATION (c
),
12032 "%<nowait%> clause must not be used together "
12033 "with %<copyprivate%>");
12037 nowait_clause
= pc
;
12038 pc
= &OMP_CLAUSE_CHAIN (c
);
12041 case OMP_CLAUSE_IF
:
12042 case OMP_CLAUSE_NUM_THREADS
:
12043 case OMP_CLAUSE_NUM_TEAMS
:
12044 case OMP_CLAUSE_THREAD_LIMIT
:
12045 case OMP_CLAUSE_SCHEDULE
:
12046 case OMP_CLAUSE_ORDERED
:
12047 case OMP_CLAUSE_DEFAULT
:
12048 case OMP_CLAUSE_UNTIED
:
12049 case OMP_CLAUSE_COLLAPSE
:
12050 case OMP_CLAUSE_FINAL
:
12051 case OMP_CLAUSE_MERGEABLE
:
12052 case OMP_CLAUSE_SAFELEN
:
12053 case OMP_CLAUSE_SIMDLEN
:
12054 case OMP_CLAUSE_DEVICE
:
12055 case OMP_CLAUSE_DIST_SCHEDULE
:
12056 case OMP_CLAUSE_PARALLEL
:
12057 case OMP_CLAUSE_FOR
:
12058 case OMP_CLAUSE_SECTIONS
:
12059 case OMP_CLAUSE_TASKGROUP
:
12060 case OMP_CLAUSE_PROC_BIND
:
12061 pc
= &OMP_CLAUSE_CHAIN (c
);
12064 case OMP_CLAUSE_INBRANCH
:
12065 case OMP_CLAUSE_NOTINBRANCH
:
12068 error_at (OMP_CLAUSE_LOCATION (c
),
12069 "%<inbranch%> clause is incompatible with "
12070 "%<notinbranch%>");
12074 branch_seen
= true;
12075 pc
= &OMP_CLAUSE_CHAIN (c
);
12079 gcc_unreachable ();
12084 t
= OMP_CLAUSE_DECL (c
);
12088 t
= require_complete_type (t
);
12089 if (t
== error_mark_node
)
12093 if (need_implicitly_determined
)
12095 const char *share_name
= NULL
;
12097 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12098 share_name
= "threadprivate";
12099 else switch (c_omp_predetermined_sharing (t
))
12101 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
12103 case OMP_CLAUSE_DEFAULT_SHARED
:
12104 /* const vars may be specified in firstprivate clause. */
12105 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12106 && TREE_READONLY (t
))
12108 share_name
= "shared";
12110 case OMP_CLAUSE_DEFAULT_PRIVATE
:
12111 share_name
= "private";
12114 gcc_unreachable ();
12118 error_at (OMP_CLAUSE_LOCATION (c
),
12119 "%qE is predetermined %qs for %qs",
12121 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12128 *pc
= OMP_CLAUSE_CHAIN (c
);
12130 pc
= &OMP_CLAUSE_CHAIN (c
);
12133 bitmap_obstack_release (NULL
);
12137 /* Create a transaction node. */
12140 c_finish_transaction (location_t loc
, tree block
, int flags
)
12142 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
12143 if (flags
& TM_STMT_ATTR_OUTER
)
12144 TRANSACTION_EXPR_OUTER (stmt
) = 1;
12145 if (flags
& TM_STMT_ATTR_RELAXED
)
12146 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
12147 return add_stmt (stmt
);
12150 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12151 down to the element type of an array. */
12154 c_build_qualified_type (tree type
, int type_quals
)
12156 if (type
== error_mark_node
)
12159 if (TREE_CODE (type
) == ARRAY_TYPE
)
12162 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
12165 /* See if we already have an identically qualified type. */
12166 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12168 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
12169 && TYPE_NAME (t
) == TYPE_NAME (type
)
12170 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
12171 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
12172 TYPE_ATTRIBUTES (type
)))
12177 tree domain
= TYPE_DOMAIN (type
);
12179 t
= build_variant_type_copy (type
);
12180 TREE_TYPE (t
) = element_type
;
12182 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
12183 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
12184 SET_TYPE_STRUCTURAL_EQUALITY (t
);
12185 else if (TYPE_CANONICAL (element_type
) != element_type
12186 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
12188 tree unqualified_canon
12189 = build_array_type (TYPE_CANONICAL (element_type
),
12190 domain
? TYPE_CANONICAL (domain
)
12193 = c_build_qualified_type (unqualified_canon
, type_quals
);
12196 TYPE_CANONICAL (t
) = t
;
12201 /* A restrict-qualified pointer type must be a pointer to object or
12202 incomplete type. Note that the use of POINTER_TYPE_P also allows
12203 REFERENCE_TYPEs, which is appropriate for C++. */
12204 if ((type_quals
& TYPE_QUAL_RESTRICT
)
12205 && (!POINTER_TYPE_P (type
)
12206 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
12208 error ("invalid use of %<restrict%>");
12209 type_quals
&= ~TYPE_QUAL_RESTRICT
;
12212 return build_qualified_type (type
, type_quals
);
12215 /* Build a VA_ARG_EXPR for the C parser. */
12218 c_build_va_arg (location_t loc
, tree expr
, tree type
)
12220 if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
12221 warning_at (loc
, OPT_Wc___compat
,
12222 "C++ requires promoted type, not enum type, in %<va_arg%>");
12223 return build_va_arg (loc
, expr
, type
);
12226 /* Return truthvalue of whether T1 is the same tree structure as T2.
12227 Return 1 if they are the same. Return 0 if they are different. */
12230 c_tree_equal (tree t1
, tree t2
)
12232 enum tree_code code1
, code2
;
12239 for (code1
= TREE_CODE (t1
);
12240 CONVERT_EXPR_CODE_P (code1
)
12241 || code1
== NON_LVALUE_EXPR
;
12242 code1
= TREE_CODE (t1
))
12243 t1
= TREE_OPERAND (t1
, 0);
12244 for (code2
= TREE_CODE (t2
);
12245 CONVERT_EXPR_CODE_P (code2
)
12246 || code2
== NON_LVALUE_EXPR
;
12247 code2
= TREE_CODE (t2
))
12248 t2
= TREE_OPERAND (t2
, 0);
12250 /* They might have become equal now. */
12254 if (code1
!= code2
)
12260 return TREE_INT_CST_LOW (t1
) == TREE_INT_CST_LOW (t2
)
12261 && TREE_INT_CST_HIGH (t1
) == TREE_INT_CST_HIGH (t2
);
12264 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1
), TREE_REAL_CST (t2
));
12267 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
12268 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
12269 TREE_STRING_LENGTH (t1
));
12272 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
12273 TREE_FIXED_CST (t2
));
12276 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
12277 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
12280 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
12283 /* We need to do this when determining whether or not two
12284 non-type pointer to member function template arguments
12286 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
12287 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
12292 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
12294 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
12295 if (!c_tree_equal (field
, elt2
->index
)
12296 || !c_tree_equal (value
, elt2
->value
))
12303 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
12305 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
12307 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
12310 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12315 call_expr_arg_iterator iter1
, iter2
;
12316 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
12318 for (arg1
= first_call_expr_arg (t1
, &iter1
),
12319 arg2
= first_call_expr_arg (t2
, &iter2
);
12321 arg1
= next_call_expr_arg (&iter1
),
12322 arg2
= next_call_expr_arg (&iter2
))
12323 if (!c_tree_equal (arg1
, arg2
))
12332 tree o1
= TREE_OPERAND (t1
, 0);
12333 tree o2
= TREE_OPERAND (t2
, 0);
12335 /* Special case: if either target is an unallocated VAR_DECL,
12336 it means that it's going to be unified with whatever the
12337 TARGET_EXPR is really supposed to initialize, so treat it
12338 as being equivalent to anything. */
12339 if (TREE_CODE (o1
) == VAR_DECL
&& DECL_NAME (o1
) == NULL_TREE
12340 && !DECL_RTL_SET_P (o1
))
12342 else if (TREE_CODE (o2
) == VAR_DECL
&& DECL_NAME (o2
) == NULL_TREE
12343 && !DECL_RTL_SET_P (o2
))
12345 else if (!c_tree_equal (o1
, o2
))
12348 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
12351 case COMPONENT_REF
:
12352 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
12354 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12360 case FUNCTION_DECL
:
12361 case IDENTIFIER_NODE
:
12368 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
12370 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
12371 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
12372 TREE_VEC_ELT (t2
, ix
)))
12381 switch (TREE_CODE_CLASS (code1
))
12385 case tcc_comparison
:
12386 case tcc_expression
:
12388 case tcc_reference
:
12389 case tcc_statement
:
12391 int i
, n
= TREE_OPERAND_LENGTH (t1
);
12395 case PREINCREMENT_EXPR
:
12396 case PREDECREMENT_EXPR
:
12397 case POSTINCREMENT_EXPR
:
12398 case POSTDECREMENT_EXPR
:
12408 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
12409 && n
!= TREE_OPERAND_LENGTH (t2
))
12412 for (i
= 0; i
< n
; ++i
)
12413 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
12420 return comptypes (t1
, t2
);
12422 gcc_unreachable ();
12424 /* We can get here with --disable-checking. */