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"
52 /* Possible cases of implicit bad conversions. Used to select
53 diagnostic messages in convert_for_assignment. */
61 /* The level of nesting inside "__alignof__". */
64 /* The level of nesting inside "sizeof". */
67 /* The level of nesting inside "typeof". */
70 /* The argument of last parsed sizeof expression, only to be tested
71 if expr.original_code == SIZEOF_EXPR. */
72 tree c_last_sizeof_arg
;
74 /* Nonzero if we've already printed a "missing braces around initializer"
75 message within this initializer. */
76 static int missing_braces_mentioned
;
78 static int require_constant_value
;
79 static int require_constant_elements
;
81 static bool null_pointer_constant_p (const_tree
);
82 static tree
qualify_type (tree
, tree
);
83 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
85 static int comp_target_types (location_t
, tree
, tree
);
86 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
88 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
89 static tree
lookup_field (tree
, tree
);
90 static int convert_arguments (tree
, vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *,
92 static tree
pointer_diff (location_t
, tree
, tree
);
93 static tree
convert_for_assignment (location_t
, tree
, tree
, tree
,
94 enum impl_conv
, bool, tree
, tree
, int);
95 static tree
valid_compound_expr_initializer (tree
, tree
);
96 static void push_string (const char *);
97 static void push_member_name (tree
);
98 static int spelling_length (void);
99 static char *print_spelling (char *);
100 static void warning_init (int, const char *);
101 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
102 static void output_init_element (tree
, tree
, bool, tree
, tree
, int, bool,
104 static void output_pending_init_elements (int, struct obstack
*);
105 static int set_designator (int, struct obstack
*);
106 static void push_range_stack (tree
, struct obstack
*);
107 static void add_pending_init (tree
, tree
, tree
, bool, struct obstack
*);
108 static void set_nonincremental_init (struct obstack
*);
109 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
110 static tree
find_init_member (tree
, struct obstack
*);
111 static void readonly_warning (tree
, enum lvalue_use
);
112 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
113 static void record_maybe_used_decl (tree
);
114 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
116 /* Return true if EXP is a null pointer constant, false otherwise. */
119 null_pointer_constant_p (const_tree expr
)
121 /* This should really operate on c_expr structures, but they aren't
122 yet available everywhere required. */
123 tree type
= TREE_TYPE (expr
);
124 return (TREE_CODE (expr
) == INTEGER_CST
125 && !TREE_OVERFLOW (expr
)
126 && integer_zerop (expr
)
127 && (INTEGRAL_TYPE_P (type
)
128 || (TREE_CODE (type
) == POINTER_TYPE
129 && VOID_TYPE_P (TREE_TYPE (type
))
130 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
133 /* EXPR may appear in an unevaluated part of an integer constant
134 expression, but not in an evaluated part. Wrap it in a
135 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
136 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
139 note_integer_operands (tree expr
)
142 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
144 ret
= copy_node (expr
);
145 TREE_OVERFLOW (ret
) = 1;
149 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
150 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
155 /* Having checked whether EXPR may appear in an unevaluated part of an
156 integer constant expression and found that it may, remove any
157 C_MAYBE_CONST_EXPR noting this fact and return the resulting
161 remove_c_maybe_const_expr (tree expr
)
163 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
164 return C_MAYBE_CONST_EXPR_EXPR (expr
);
169 \f/* This is a cache to hold if two types are compatible or not. */
171 struct tagged_tu_seen_cache
{
172 const struct tagged_tu_seen_cache
* next
;
175 /* The return value of tagged_types_tu_compatible_p if we had seen
176 these two types already. */
180 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
181 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
183 /* Do `exp = require_complete_type (exp);' to make sure exp
184 does not have an incomplete type. (That includes void types.) */
187 require_complete_type (tree value
)
189 tree type
= TREE_TYPE (value
);
191 if (value
== error_mark_node
|| type
== error_mark_node
)
192 return error_mark_node
;
194 /* First, detect a valid value with a complete type. */
195 if (COMPLETE_TYPE_P (type
))
198 c_incomplete_type_error (value
, type
);
199 return error_mark_node
;
202 /* Print an error message for invalid use of an incomplete type.
203 VALUE is the expression that was used (or 0 if that isn't known)
204 and TYPE is the type that was invalid. */
207 c_incomplete_type_error (const_tree value
, const_tree type
)
209 const char *type_code_string
;
211 /* Avoid duplicate error message. */
212 if (TREE_CODE (type
) == ERROR_MARK
)
215 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
216 || TREE_CODE (value
) == PARM_DECL
))
217 error ("%qD has an incomplete type", value
);
221 /* We must print an error message. Be clever about what it says. */
223 switch (TREE_CODE (type
))
226 type_code_string
= "struct";
230 type_code_string
= "union";
234 type_code_string
= "enum";
238 error ("invalid use of void expression");
242 if (TYPE_DOMAIN (type
))
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
246 error ("invalid use of flexible array member");
249 type
= TREE_TYPE (type
);
252 error ("invalid use of array with unspecified bounds");
259 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
260 error ("invalid use of undefined type %<%s %E%>",
261 type_code_string
, TYPE_NAME (type
));
263 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
264 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type
));
268 /* Given a type, apply default promotions wrt unnamed function
269 arguments and return the new type. */
272 c_type_promotes_to (tree type
)
274 tree ret
= NULL_TREE
;
276 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
277 ret
= double_type_node
;
278 else if (c_promoting_integer_type_p (type
))
280 /* Preserve unsignedness if not really getting any wider. */
281 if (TYPE_UNSIGNED (type
)
282 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
283 ret
= unsigned_type_node
;
285 ret
= integer_type_node
;
288 if (ret
!= NULL_TREE
)
289 return (TYPE_ATOMIC (type
)
290 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
296 /* Return true if between two named address spaces, whether there is a superset
297 named address space that encompasses both address spaces. If there is a
298 superset, return which address space is the superset. */
301 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
308 else if (targetm
.addr_space
.subset_p (as1
, as2
))
313 else if (targetm
.addr_space
.subset_p (as2
, as1
))
322 /* Return a variant of TYPE which has all the type qualifiers of LIKE
323 as well as those of TYPE. */
326 qualify_type (tree type
, tree like
)
328 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
329 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
330 addr_space_t as_common
;
332 /* If the two named address spaces are different, determine the common
333 superset address space. If there isn't one, raise an error. */
334 if (!addr_space_superset (as_type
, as_like
, &as_common
))
337 error ("%qT and %qT are in disjoint named address spaces",
341 return c_build_qualified_type (type
,
342 TYPE_QUALS_NO_ADDR_SPACE (type
)
343 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
344 | ENCODE_QUAL_ADDR_SPACE (as_common
));
347 /* Return true iff the given tree T is a variable length array. */
350 c_vla_type_p (const_tree t
)
352 if (TREE_CODE (t
) == ARRAY_TYPE
353 && C_TYPE_VARIABLE_SIZE (t
))
358 /* Return the composite type of two compatible types.
360 We assume that comptypes has already been done and returned
361 nonzero; if that isn't so, this may crash. In particular, we
362 assume that qualifiers match. */
365 composite_type (tree t1
, tree t2
)
367 enum tree_code code1
;
368 enum tree_code code2
;
371 /* Save time if the two types are the same. */
373 if (t1
== t2
) return t1
;
375 /* If one type is nonsense, use the other. */
376 if (t1
== error_mark_node
)
378 if (t2
== error_mark_node
)
381 code1
= TREE_CODE (t1
);
382 code2
= TREE_CODE (t2
);
384 /* Merge the attributes. */
385 attributes
= targetm
.merge_type_attributes (t1
, t2
);
387 /* If one is an enumerated type and the other is the compatible
388 integer type, the composite type might be either of the two
389 (DR#013 question 3). For consistency, use the enumerated type as
390 the composite type. */
392 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
394 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
397 gcc_assert (code1
== code2
);
402 /* For two pointers, do this recursively on the target type. */
404 tree pointed_to_1
= TREE_TYPE (t1
);
405 tree pointed_to_2
= TREE_TYPE (t2
);
406 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
407 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
408 t1
= build_type_attribute_variant (t1
, attributes
);
409 return qualify_type (t1
, t2
);
414 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
417 tree d1
= TYPE_DOMAIN (t1
);
418 tree d2
= TYPE_DOMAIN (t2
);
419 bool d1_variable
, d2_variable
;
420 bool d1_zero
, d2_zero
;
421 bool t1_complete
, t2_complete
;
423 /* We should not have any type quals on arrays at all. */
424 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
425 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
427 t1_complete
= COMPLETE_TYPE_P (t1
);
428 t2_complete
= COMPLETE_TYPE_P (t2
);
430 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
431 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
433 d1_variable
= (!d1_zero
434 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
435 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
436 d2_variable
= (!d2_zero
437 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
438 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
439 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
440 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
442 /* Save space: see if the result is identical to one of the args. */
443 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
444 && (d2_variable
|| d2_zero
|| !d1_variable
))
445 return build_type_attribute_variant (t1
, attributes
);
446 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
447 && (d1_variable
|| d1_zero
|| !d2_variable
))
448 return build_type_attribute_variant (t2
, attributes
);
450 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
451 return build_type_attribute_variant (t1
, attributes
);
452 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
453 return build_type_attribute_variant (t2
, attributes
);
455 /* Merge the element types, and have a size if either arg has
456 one. We may have qualifiers on the element types. To set
457 up TYPE_MAIN_VARIANT correctly, we need to form the
458 composite of the unqualified types and add the qualifiers
460 quals
= TYPE_QUALS (strip_array_types (elt
));
461 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
462 t1
= build_array_type (unqual_elt
,
463 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
469 /* Ensure a composite type involving a zero-length array type
470 is a zero-length type not an incomplete type. */
471 if (d1_zero
&& d2_zero
472 && (t1_complete
|| t2_complete
)
473 && !COMPLETE_TYPE_P (t1
))
475 TYPE_SIZE (t1
) = bitsize_zero_node
;
476 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
478 t1
= c_build_qualified_type (t1
, quals
);
479 return build_type_attribute_variant (t1
, attributes
);
485 if (attributes
!= NULL
)
487 /* Try harder not to create a new aggregate type. */
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
490 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
493 return build_type_attribute_variant (t1
, attributes
);
496 /* Function types: prefer the one that specified arg types.
497 If both do, merge the arg types. Also merge the return types. */
499 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
500 tree p1
= TYPE_ARG_TYPES (t1
);
501 tree p2
= TYPE_ARG_TYPES (t2
);
506 /* Save space: see if the result is identical to one of the args. */
507 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
508 return build_type_attribute_variant (t1
, attributes
);
509 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
510 return build_type_attribute_variant (t2
, attributes
);
512 /* Simple way if one arg fails to specify argument types. */
513 if (TYPE_ARG_TYPES (t1
) == 0)
515 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
516 t1
= build_type_attribute_variant (t1
, attributes
);
517 return qualify_type (t1
, t2
);
519 if (TYPE_ARG_TYPES (t2
) == 0)
521 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
522 t1
= build_type_attribute_variant (t1
, attributes
);
523 return qualify_type (t1
, t2
);
526 /* If both args specify argument types, we must merge the two
527 lists, argument by argument. */
529 len
= list_length (p1
);
532 for (i
= 0; i
< len
; i
++)
533 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
538 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
540 /* A null type means arg type is not specified.
541 Take whatever the other function type has. */
542 if (TREE_VALUE (p1
) == 0)
544 TREE_VALUE (n
) = TREE_VALUE (p2
);
547 if (TREE_VALUE (p2
) == 0)
549 TREE_VALUE (n
) = TREE_VALUE (p1
);
553 /* Given wait (union {union wait *u; int *i} *)
554 and wait (union wait *),
555 prefer union wait * as type of parm. */
556 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
557 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
560 tree mv2
= TREE_VALUE (p2
);
561 if (mv2
&& mv2
!= error_mark_node
562 && TREE_CODE (mv2
) != ARRAY_TYPE
)
563 mv2
= TYPE_MAIN_VARIANT (mv2
);
564 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
565 memb
; memb
= DECL_CHAIN (memb
))
567 tree mv3
= TREE_TYPE (memb
);
568 if (mv3
&& mv3
!= error_mark_node
569 && TREE_CODE (mv3
) != ARRAY_TYPE
)
570 mv3
= TYPE_MAIN_VARIANT (mv3
);
571 if (comptypes (mv3
, mv2
))
573 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
575 pedwarn (input_location
, OPT_Wpedantic
,
576 "function types not truly compatible in ISO C");
581 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
582 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
585 tree mv1
= TREE_VALUE (p1
);
586 if (mv1
&& mv1
!= error_mark_node
587 && TREE_CODE (mv1
) != ARRAY_TYPE
)
588 mv1
= TYPE_MAIN_VARIANT (mv1
);
589 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
590 memb
; memb
= DECL_CHAIN (memb
))
592 tree mv3
= TREE_TYPE (memb
);
593 if (mv3
&& mv3
!= error_mark_node
594 && TREE_CODE (mv3
) != ARRAY_TYPE
)
595 mv3
= TYPE_MAIN_VARIANT (mv3
);
596 if (comptypes (mv3
, mv1
))
598 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
600 pedwarn (input_location
, OPT_Wpedantic
,
601 "function types not truly compatible in ISO C");
606 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
610 t1
= build_function_type (valtype
, newargs
);
611 t1
= qualify_type (t1
, t2
);
612 /* ... falls through ... */
616 return build_type_attribute_variant (t1
, attributes
);
621 /* Return the type of a conditional expression between pointers to
622 possibly differently qualified versions of compatible types.
624 We assume that comp_target_types has already been done and returned
625 nonzero; if that isn't so, this may crash. */
628 common_pointer_type (tree t1
, tree t2
)
631 tree pointed_to_1
, mv1
;
632 tree pointed_to_2
, mv2
;
634 unsigned target_quals
;
635 addr_space_t as1
, as2
, as_common
;
638 /* Save time if the two types are the same. */
640 if (t1
== t2
) return t1
;
642 /* If one type is nonsense, use the other. */
643 if (t1
== error_mark_node
)
645 if (t2
== error_mark_node
)
648 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
649 && TREE_CODE (t2
) == POINTER_TYPE
);
651 /* Merge the attributes. */
652 attributes
= targetm
.merge_type_attributes (t1
, t2
);
654 /* Find the composite type of the target types, and combine the
655 qualifiers of the two types' targets. Do not lose qualifiers on
656 array element types by taking the TYPE_MAIN_VARIANT. */
657 mv1
= pointed_to_1
= TREE_TYPE (t1
);
658 mv2
= pointed_to_2
= TREE_TYPE (t2
);
659 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
660 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
661 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
662 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
663 target
= composite_type (mv1
, mv2
);
665 /* For function types do not merge const qualifiers, but drop them
666 if used inconsistently. The middle-end uses these to mark const
667 and noreturn functions. */
668 quals1
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1
);
669 quals2
= TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2
);
671 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
672 target_quals
= (quals1
& quals2
);
674 target_quals
= (quals1
| quals2
);
676 /* If the two named address spaces are different, determine the common
677 superset address space. This is guaranteed to exist due to the
678 assumption that comp_target_type returned non-zero. */
679 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
680 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
681 if (!addr_space_superset (as1
, as2
, &as_common
))
684 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
686 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
687 return build_type_attribute_variant (t1
, attributes
);
690 /* Return the common type for two arithmetic types under the usual
691 arithmetic conversions. The default conversions have already been
692 applied, and enumerated types converted to their compatible integer
693 types. The resulting type is unqualified and has no attributes.
695 This is the type for the result of most arithmetic operations
696 if the operands have the given two types. */
699 c_common_type (tree t1
, tree t2
)
701 enum tree_code code1
;
702 enum tree_code code2
;
704 /* If one type is nonsense, use the other. */
705 if (t1
== error_mark_node
)
707 if (t2
== error_mark_node
)
710 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
711 t1
= TYPE_MAIN_VARIANT (t1
);
713 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
714 t2
= TYPE_MAIN_VARIANT (t2
);
716 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
717 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
719 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
720 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
722 /* Save time if the two types are the same. */
724 if (t1
== t2
) return t1
;
726 code1
= TREE_CODE (t1
);
727 code2
= TREE_CODE (t2
);
729 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
730 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
731 || code1
== INTEGER_TYPE
);
732 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
733 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
734 || code2
== INTEGER_TYPE
);
736 /* When one operand is a decimal float type, the other operand cannot be
737 a generic float type or a complex type. We also disallow vector types
739 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
740 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
742 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
744 error ("can%'t mix operands of decimal float and vector types");
745 return error_mark_node
;
747 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
749 error ("can%'t mix operands of decimal float and complex types");
750 return error_mark_node
;
752 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
754 error ("can%'t mix operands of decimal float and other float types");
755 return error_mark_node
;
759 /* If one type is a vector type, return that type. (How the usual
760 arithmetic conversions apply to the vector types extension is not
761 precisely specified.) */
762 if (code1
== VECTOR_TYPE
)
765 if (code2
== VECTOR_TYPE
)
768 /* If one type is complex, form the common type of the non-complex
769 components, then make that complex. Use T1 or T2 if it is the
771 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
773 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
774 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
775 tree subtype
= c_common_type (subtype1
, subtype2
);
777 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
779 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
782 return build_complex_type (subtype
);
785 /* If only one is real, use it as the result. */
787 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
790 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
793 /* If both are real and either are decimal floating point types, use
794 the decimal floating point type with the greater precision. */
796 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
798 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
799 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
800 return dfloat128_type_node
;
801 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
802 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
803 return dfloat64_type_node
;
804 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
805 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
806 return dfloat32_type_node
;
809 /* Deal with fixed-point types. */
810 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
812 unsigned int unsignedp
= 0, satp
= 0;
813 enum machine_mode m1
, m2
;
814 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
819 /* If one input type is saturating, the result type is saturating. */
820 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
823 /* If both fixed-point types are unsigned, the result type is unsigned.
824 When mixing fixed-point and integer types, follow the sign of the
826 Otherwise, the result type is signed. */
827 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
828 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
829 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t1
))
831 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t2
)))
835 /* The result type is signed. */
838 /* If the input type is unsigned, we need to convert to the
840 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
842 enum mode_class mclass
= (enum mode_class
) 0;
843 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
845 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
849 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
851 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
853 enum mode_class mclass
= (enum mode_class
) 0;
854 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
856 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
860 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
864 if (code1
== FIXED_POINT_TYPE
)
866 fbit1
= GET_MODE_FBIT (m1
);
867 ibit1
= GET_MODE_IBIT (m1
);
872 /* Signed integers need to subtract one sign bit. */
873 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
876 if (code2
== FIXED_POINT_TYPE
)
878 fbit2
= GET_MODE_FBIT (m2
);
879 ibit2
= GET_MODE_IBIT (m2
);
884 /* Signed integers need to subtract one sign bit. */
885 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
888 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
889 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
890 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
894 /* Both real or both integers; use the one with greater precision. */
896 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
898 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
901 /* Same precision. Prefer long longs to longs to ints when the
902 same precision, following the C99 rules on integer type rank
903 (which are equivalent to the C90 rules for C90 types). */
905 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
906 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
907 return long_long_unsigned_type_node
;
909 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
910 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
912 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
913 return long_long_unsigned_type_node
;
915 return long_long_integer_type_node
;
918 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
919 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
920 return long_unsigned_type_node
;
922 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
923 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
925 /* But preserve unsignedness from the other type,
926 since long cannot hold all the values of an unsigned int. */
927 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
928 return long_unsigned_type_node
;
930 return long_integer_type_node
;
933 /* Likewise, prefer long double to double even if same size. */
934 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
935 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
936 return long_double_type_node
;
938 /* Likewise, prefer double to float even if same size.
939 We got a couple of embedded targets with 32 bit doubles, and the
940 pdp11 might have 64 bit floats. */
941 if (TYPE_MAIN_VARIANT (t1
) == double_type_node
942 || TYPE_MAIN_VARIANT (t2
) == double_type_node
)
943 return double_type_node
;
945 /* Otherwise prefer the unsigned one. */
947 if (TYPE_UNSIGNED (t1
))
953 /* Wrapper around c_common_type that is used by c-common.c and other
954 front end optimizations that remove promotions. ENUMERAL_TYPEs
955 are allowed here and are converted to their compatible integer types.
956 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
957 preferably a non-Boolean type as the common type. */
959 common_type (tree t1
, tree t2
)
961 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
962 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
963 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
964 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
966 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
967 if (TREE_CODE (t1
) == BOOLEAN_TYPE
968 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
969 return boolean_type_node
;
971 /* If either type is BOOLEAN_TYPE, then return the other. */
972 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
974 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
977 return c_common_type (t1
, t2
);
980 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
981 or various other operations. Return 2 if they are compatible
982 but a warning may be needed if you use them together. */
985 comptypes (tree type1
, tree type2
)
987 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
990 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
991 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
996 /* Like comptypes, but if it returns non-zero because enum and int are
997 compatible, it sets *ENUM_AND_INT_P to true. */
1000 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1002 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1005 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1006 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1011 /* Like comptypes, but if it returns nonzero for different types, it
1012 sets *DIFFERENT_TYPES_P to true. */
1015 comptypes_check_different_types (tree type1
, tree type2
,
1016 bool *different_types_p
)
1018 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1021 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1022 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1027 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1028 or various other operations. Return 2 if they are compatible
1029 but a warning may be needed if you use them together. If
1030 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1031 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1032 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1033 NULL, and the types are compatible but different enough not to be
1034 permitted in C11 typedef redeclarations, then this sets
1035 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1036 false, but may or may not be set if the types are incompatible.
1037 This differs from comptypes, in that we don't free the seen
1041 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1042 bool *different_types_p
)
1044 const_tree t1
= type1
;
1045 const_tree t2
= type2
;
1048 /* Suppress errors caused by previously reported errors. */
1050 if (t1
== t2
|| !t1
|| !t2
1051 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1054 /* Enumerated types are compatible with integer types, but this is
1055 not transitive: two enumerated types in the same translation unit
1056 are compatible with each other only if they are the same type. */
1058 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1060 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1061 if (TREE_CODE (t2
) != VOID_TYPE
)
1063 if (enum_and_int_p
!= NULL
)
1064 *enum_and_int_p
= true;
1065 if (different_types_p
!= NULL
)
1066 *different_types_p
= true;
1069 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1071 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1072 if (TREE_CODE (t1
) != VOID_TYPE
)
1074 if (enum_and_int_p
!= NULL
)
1075 *enum_and_int_p
= true;
1076 if (different_types_p
!= NULL
)
1077 *different_types_p
= true;
1084 /* Different classes of types can't be compatible. */
1086 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1089 /* Qualifiers must match. C99 6.7.3p9 */
1091 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1094 /* Allow for two different type nodes which have essentially the same
1095 definition. Note that we already checked for equality of the type
1096 qualifiers (just above). */
1098 if (TREE_CODE (t1
) != ARRAY_TYPE
1099 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1102 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1103 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1106 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1109 switch (TREE_CODE (t1
))
1112 /* Do not remove mode or aliasing information. */
1113 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1114 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1116 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1117 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1118 enum_and_int_p
, different_types_p
));
1122 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1128 tree d1
= TYPE_DOMAIN (t1
);
1129 tree d2
= TYPE_DOMAIN (t2
);
1130 bool d1_variable
, d2_variable
;
1131 bool d1_zero
, d2_zero
;
1134 /* Target types must match incl. qualifiers. */
1135 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1136 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1138 different_types_p
)))
1141 if (different_types_p
!= NULL
1142 && (d1
== 0) != (d2
== 0))
1143 *different_types_p
= true;
1144 /* Sizes must match unless one is missing or variable. */
1145 if (d1
== 0 || d2
== 0 || d1
== d2
)
1148 d1_zero
= !TYPE_MAX_VALUE (d1
);
1149 d2_zero
= !TYPE_MAX_VALUE (d2
);
1151 d1_variable
= (!d1_zero
1152 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1153 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1154 d2_variable
= (!d2_zero
1155 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1156 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1157 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1158 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1160 if (different_types_p
!= NULL
1161 && d1_variable
!= d2_variable
)
1162 *different_types_p
= true;
1163 if (d1_variable
|| d2_variable
)
1165 if (d1_zero
&& d2_zero
)
1167 if (d1_zero
|| d2_zero
1168 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1169 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1178 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1180 tree a1
= TYPE_ATTRIBUTES (t1
);
1181 tree a2
= TYPE_ATTRIBUTES (t2
);
1183 if (! attribute_list_contained (a1
, a2
)
1184 && ! attribute_list_contained (a2
, a1
))
1188 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1190 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1196 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1197 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1198 enum_and_int_p
, different_types_p
));
1204 return attrval
== 2 && val
== 1 ? 2 : val
;
1207 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1208 their qualifiers, except for named address spaces. If the pointers point to
1209 different named addresses, then we must determine if one address space is a
1210 subset of the other. */
1213 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1216 tree mvl
= TREE_TYPE (ttl
);
1217 tree mvr
= TREE_TYPE (ttr
);
1218 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1219 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1220 addr_space_t as_common
;
1221 bool enum_and_int_p
;
1223 /* Fail if pointers point to incompatible address spaces. */
1224 if (!addr_space_superset (asl
, asr
, &as_common
))
1227 /* Do not lose qualifiers on element types of array types that are
1228 pointer targets by taking their TYPE_MAIN_VARIANT. */
1229 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
1230 mvl
= (TYPE_ATOMIC (mvl
)
1231 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1232 : TYPE_MAIN_VARIANT (mvl
));
1233 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
1234 mvr
= (TYPE_ATOMIC (mvr
)
1235 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1236 : TYPE_MAIN_VARIANT (mvr
));
1237 enum_and_int_p
= false;
1238 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1241 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1243 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1244 warning_at (location
, OPT_Wc___compat
,
1245 "pointer target types incompatible in C++");
1250 /* Subroutines of `comptypes'. */
1252 /* Determine whether two trees derive from the same translation unit.
1253 If the CONTEXT chain ends in a null, that tree's context is still
1254 being parsed, so if two trees have context chains ending in null,
1255 they're in the same translation unit. */
1257 same_translation_unit_p (const_tree t1
, const_tree t2
)
1259 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1260 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1262 case tcc_declaration
:
1263 t1
= DECL_CONTEXT (t1
); break;
1265 t1
= TYPE_CONTEXT (t1
); break;
1266 case tcc_exceptional
:
1267 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1268 default: gcc_unreachable ();
1271 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1272 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1274 case tcc_declaration
:
1275 t2
= DECL_CONTEXT (t2
); break;
1277 t2
= TYPE_CONTEXT (t2
); break;
1278 case tcc_exceptional
:
1279 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1280 default: gcc_unreachable ();
1286 /* Allocate the seen two types, assuming that they are compatible. */
1288 static struct tagged_tu_seen_cache
*
1289 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1291 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1292 tu
->next
= tagged_tu_seen_base
;
1296 tagged_tu_seen_base
= tu
;
1298 /* The C standard says that two structures in different translation
1299 units are compatible with each other only if the types of their
1300 fields are compatible (among other things). We assume that they
1301 are compatible until proven otherwise when building the cache.
1302 An example where this can occur is:
1307 If we are comparing this against a similar struct in another TU,
1308 and did not assume they were compatible, we end up with an infinite
1314 /* Free the seen types until we get to TU_TIL. */
1317 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1319 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1320 while (tu
!= tu_til
)
1322 const struct tagged_tu_seen_cache
*const tu1
1323 = (const struct tagged_tu_seen_cache
*) tu
;
1325 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1327 tagged_tu_seen_base
= tu_til
;
1330 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1331 compatible. If the two types are not the same (which has been
1332 checked earlier), this can only happen when multiple translation
1333 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1334 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1335 comptypes_internal. */
1338 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1339 bool *enum_and_int_p
, bool *different_types_p
)
1342 bool needs_warning
= false;
1344 /* We have to verify that the tags of the types are the same. This
1345 is harder than it looks because this may be a typedef, so we have
1346 to go look at the original type. It may even be a typedef of a
1348 In the case of compiler-created builtin structs the TYPE_DECL
1349 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1350 while (TYPE_NAME (t1
)
1351 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1352 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1353 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1355 while (TYPE_NAME (t2
)
1356 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1357 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1358 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1360 /* C90 didn't have the requirement that the two tags be the same. */
1361 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1364 /* C90 didn't say what happened if one or both of the types were
1365 incomplete; we choose to follow C99 rules here, which is that they
1367 if (TYPE_SIZE (t1
) == NULL
1368 || TYPE_SIZE (t2
) == NULL
)
1372 const struct tagged_tu_seen_cache
* tts_i
;
1373 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1374 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1378 switch (TREE_CODE (t1
))
1382 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1383 /* Speed up the case where the type values are in the same order. */
1384 tree tv1
= TYPE_VALUES (t1
);
1385 tree tv2
= TYPE_VALUES (t2
);
1392 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1394 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1396 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1403 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1407 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1413 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1419 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1421 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1423 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1434 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1435 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1441 /* Speed up the common case where the fields are in the same order. */
1442 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1443 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1447 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1449 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1450 enum_and_int_p
, different_types_p
);
1452 if (result
!= 1 && !DECL_NAME (s1
))
1460 needs_warning
= true;
1462 if (TREE_CODE (s1
) == FIELD_DECL
1463 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1464 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1472 tu
->val
= needs_warning
? 2 : 1;
1476 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1480 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1481 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1485 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1489 if (result
!= 1 && !DECL_NAME (s1
))
1497 needs_warning
= true;
1499 if (TREE_CODE (s1
) == FIELD_DECL
1500 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1501 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1513 tu
->val
= needs_warning
? 2 : 10;
1519 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1521 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1523 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1526 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1527 || DECL_NAME (s1
) != DECL_NAME (s2
))
1529 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1530 enum_and_int_p
, different_types_p
);
1534 needs_warning
= true;
1536 if (TREE_CODE (s1
) == FIELD_DECL
1537 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1538 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1544 tu
->val
= needs_warning
? 2 : 1;
1553 /* Return 1 if two function types F1 and F2 are compatible.
1554 If either type specifies no argument types,
1555 the other must specify a fixed number of self-promoting arg types.
1556 Otherwise, if one type specifies only the number of arguments,
1557 the other must specify that number of self-promoting arg types.
1558 Otherwise, the argument types must match.
1559 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1562 function_types_compatible_p (const_tree f1
, const_tree f2
,
1563 bool *enum_and_int_p
, bool *different_types_p
)
1566 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1571 ret1
= TREE_TYPE (f1
);
1572 ret2
= TREE_TYPE (f2
);
1574 /* 'volatile' qualifiers on a function's return type used to mean
1575 the function is noreturn. */
1576 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1577 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1578 if (TYPE_VOLATILE (ret1
))
1579 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1580 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1581 if (TYPE_VOLATILE (ret2
))
1582 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1583 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1584 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1588 args1
= TYPE_ARG_TYPES (f1
);
1589 args2
= TYPE_ARG_TYPES (f2
);
1591 if (different_types_p
!= NULL
1592 && (args1
== 0) != (args2
== 0))
1593 *different_types_p
= true;
1595 /* An unspecified parmlist matches any specified parmlist
1596 whose argument types don't need default promotions. */
1600 if (!self_promoting_args_p (args2
))
1602 /* If one of these types comes from a non-prototype fn definition,
1603 compare that with the other type's arglist.
1604 If they don't match, ask for a warning (but no error). */
1605 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1606 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1607 enum_and_int_p
, different_types_p
))
1613 if (!self_promoting_args_p (args1
))
1615 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1616 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1617 enum_and_int_p
, different_types_p
))
1622 /* Both types have argument lists: compare them and propagate results. */
1623 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1625 return val1
!= 1 ? val1
: val
;
1628 /* Check two lists of types for compatibility, returning 0 for
1629 incompatible, 1 for compatible, or 2 for compatible with
1630 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1631 comptypes_internal. */
1634 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1635 bool *enum_and_int_p
, bool *different_types_p
)
1637 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1643 tree a1
, mv1
, a2
, mv2
;
1644 if (args1
== 0 && args2
== 0)
1646 /* If one list is shorter than the other,
1647 they fail to match. */
1648 if (args1
== 0 || args2
== 0)
1650 mv1
= a1
= TREE_VALUE (args1
);
1651 mv2
= a2
= TREE_VALUE (args2
);
1652 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1653 mv1
= (TYPE_ATOMIC (mv1
)
1654 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1656 : TYPE_MAIN_VARIANT (mv1
));
1657 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1658 mv2
= (TYPE_ATOMIC (mv2
)
1659 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1661 : TYPE_MAIN_VARIANT (mv2
));
1662 /* A null pointer instead of a type
1663 means there is supposed to be an argument
1664 but nothing is specified about what type it has.
1665 So match anything that self-promotes. */
1666 if (different_types_p
!= NULL
1667 && (a1
== 0) != (a2
== 0))
1668 *different_types_p
= true;
1671 if (c_type_promotes_to (a2
) != a2
)
1676 if (c_type_promotes_to (a1
) != a1
)
1679 /* If one of the lists has an error marker, ignore this arg. */
1680 else if (TREE_CODE (a1
) == ERROR_MARK
1681 || TREE_CODE (a2
) == ERROR_MARK
)
1683 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1684 different_types_p
)))
1686 if (different_types_p
!= NULL
)
1687 *different_types_p
= true;
1688 /* Allow wait (union {union wait *u; int *i} *)
1689 and wait (union wait *) to be compatible. */
1690 if (TREE_CODE (a1
) == UNION_TYPE
1691 && (TYPE_NAME (a1
) == 0
1692 || TYPE_TRANSPARENT_AGGR (a1
))
1693 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1694 && tree_int_cst_equal (TYPE_SIZE (a1
),
1698 for (memb
= TYPE_FIELDS (a1
);
1699 memb
; memb
= DECL_CHAIN (memb
))
1701 tree mv3
= TREE_TYPE (memb
);
1702 if (mv3
&& mv3
!= error_mark_node
1703 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1704 mv3
= (TYPE_ATOMIC (mv3
)
1705 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1707 : TYPE_MAIN_VARIANT (mv3
));
1708 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1715 else if (TREE_CODE (a2
) == UNION_TYPE
1716 && (TYPE_NAME (a2
) == 0
1717 || TYPE_TRANSPARENT_AGGR (a2
))
1718 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1719 && tree_int_cst_equal (TYPE_SIZE (a2
),
1723 for (memb
= TYPE_FIELDS (a2
);
1724 memb
; memb
= DECL_CHAIN (memb
))
1726 tree mv3
= TREE_TYPE (memb
);
1727 if (mv3
&& mv3
!= error_mark_node
1728 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1729 mv3
= (TYPE_ATOMIC (mv3
)
1730 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1732 : TYPE_MAIN_VARIANT (mv3
));
1733 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1744 /* comptypes said ok, but record if it said to warn. */
1748 args1
= TREE_CHAIN (args1
);
1749 args2
= TREE_CHAIN (args2
);
1753 /* Compute the size to increment a pointer by. */
1756 c_size_in_bytes (const_tree type
)
1758 enum tree_code code
= TREE_CODE (type
);
1760 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
)
1761 return size_one_node
;
1763 if (!COMPLETE_OR_VOID_TYPE_P (type
))
1765 error ("arithmetic on pointer to an incomplete type");
1766 return size_one_node
;
1769 /* Convert in case a char is more than one unit. */
1770 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1771 size_int (TYPE_PRECISION (char_type_node
)
1775 /* Return either DECL or its known constant value (if it has one). */
1778 decl_constant_value (tree decl
)
1780 if (/* Don't change a variable array bound or initial value to a constant
1781 in a place where a variable is invalid. Note that DECL_INITIAL
1782 isn't valid for a PARM_DECL. */
1783 current_function_decl
!= 0
1784 && TREE_CODE (decl
) != PARM_DECL
1785 && !TREE_THIS_VOLATILE (decl
)
1786 && TREE_READONLY (decl
)
1787 && DECL_INITIAL (decl
) != 0
1788 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1789 /* This is invalid if initial value is not constant.
1790 If it has either a function call, a memory reference,
1791 or a variable, then re-evaluating it could give different results. */
1792 && TREE_CONSTANT (DECL_INITIAL (decl
))
1793 /* Check for cases where this is sub-optimal, even though valid. */
1794 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1795 return DECL_INITIAL (decl
);
1799 /* Convert the array expression EXP to a pointer. */
1801 array_to_pointer_conversion (location_t loc
, tree exp
)
1803 tree orig_exp
= exp
;
1804 tree type
= TREE_TYPE (exp
);
1806 tree restype
= TREE_TYPE (type
);
1809 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1811 STRIP_TYPE_NOPS (exp
);
1813 if (TREE_NO_WARNING (orig_exp
))
1814 TREE_NO_WARNING (exp
) = 1;
1816 ptrtype
= build_pointer_type (restype
);
1818 if (TREE_CODE (exp
) == INDIRECT_REF
)
1819 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1821 /* In C++ array compound literals are temporary objects unless they are
1822 const or appear in namespace scope, so they are destroyed too soon
1823 to use them for much of anything (c++/53220). */
1824 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1826 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1827 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1828 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1829 "converting an array compound literal to a pointer "
1830 "is ill-formed in C++");
1833 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1834 return convert (ptrtype
, adr
);
1837 /* Convert the function expression EXP to a pointer. */
1839 function_to_pointer_conversion (location_t loc
, tree exp
)
1841 tree orig_exp
= exp
;
1843 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1845 STRIP_TYPE_NOPS (exp
);
1847 if (TREE_NO_WARNING (orig_exp
))
1848 TREE_NO_WARNING (exp
) = 1;
1850 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1853 /* Mark EXP as read, not just set, for set but not used -Wunused
1854 warning purposes. */
1857 mark_exp_read (tree exp
)
1859 switch (TREE_CODE (exp
))
1863 DECL_READ_P (exp
) = 1;
1872 mark_exp_read (TREE_OPERAND (exp
, 0));
1875 case C_MAYBE_CONST_EXPR
:
1876 mark_exp_read (TREE_OPERAND (exp
, 1));
1883 /* Perform the default conversion of arrays and functions to pointers.
1884 Return the result of converting EXP. For any other expression, just
1887 LOC is the location of the expression. */
1890 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1892 tree orig_exp
= exp
.value
;
1893 tree type
= TREE_TYPE (exp
.value
);
1894 enum tree_code code
= TREE_CODE (type
);
1900 bool not_lvalue
= false;
1901 bool lvalue_array_p
;
1903 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1904 || CONVERT_EXPR_P (exp
.value
))
1905 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1907 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1909 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1912 if (TREE_NO_WARNING (orig_exp
))
1913 TREE_NO_WARNING (exp
.value
) = 1;
1915 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1916 if (!flag_isoc99
&& !lvalue_array_p
)
1918 /* Before C99, non-lvalue arrays do not decay to pointers.
1919 Normally, using such an array would be invalid; but it can
1920 be used correctly inside sizeof or as a statement expression.
1921 Thus, do not give an error here; an error will result later. */
1925 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1929 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1939 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1941 mark_exp_read (exp
.value
);
1942 return default_function_array_conversion (loc
, exp
);
1945 /* Return whether EXPR should be treated as an atomic lvalue for the
1946 purposes of load and store handling. */
1949 really_atomic_lvalue (tree expr
)
1951 if (expr
== error_mark_node
|| TREE_TYPE (expr
) == error_mark_node
)
1953 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
1955 if (!lvalue_p (expr
))
1958 /* Ignore _Atomic on register variables, since their addresses can't
1959 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1960 sequences wouldn't work. Ignore _Atomic on structures containing
1961 bit-fields, since accessing elements of atomic structures or
1962 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1963 it's undefined at translation time or execution time, and the
1964 normal atomic sequences again wouldn't work. */
1965 while (handled_component_p (expr
))
1967 if (TREE_CODE (expr
) == COMPONENT_REF
1968 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1970 expr
= TREE_OPERAND (expr
, 0);
1972 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
1977 /* Convert expression EXP (location LOC) from lvalue to rvalue,
1978 including converting functions and arrays to pointers if CONVERT_P.
1979 If READ_P, also mark the expression as having been read. */
1982 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
1983 bool convert_p
, bool read_p
)
1986 mark_exp_read (exp
.value
);
1988 exp
= default_function_array_conversion (loc
, exp
);
1989 if (really_atomic_lvalue (exp
.value
))
1991 vec
<tree
, va_gc
> *params
;
1992 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
1993 tree expr_type
= TREE_TYPE (exp
.value
);
1994 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, 0);
1995 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
1997 gcc_assert (TYPE_ATOMIC (expr_type
));
1999 /* Expansion of a generic atomic load may require an addition
2000 element, so allocate enough to prevent a resize. */
2001 vec_alloc (params
, 4);
2003 /* Remove the qualifiers for the rest of the expressions and
2004 create the VAL temp variable to hold the RHS. */
2005 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2006 tmp
= create_tmp_var (nonatomic_type
, NULL
);
2007 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, 0);
2008 TREE_ADDRESSABLE (tmp
) = 1;
2010 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2011 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2012 params
->quick_push (expr_addr
);
2013 params
->quick_push (tmp_addr
);
2014 params
->quick_push (seq_cst
);
2015 func_call
= build_function_call_vec (loc
, fndecl
, params
, NULL
);
2017 /* Return tmp which contains the value loaded. */
2018 exp
.value
= build2 (COMPOUND_EXPR
, nonatomic_type
, func_call
, tmp
);
2023 /* EXP is an expression of integer type. Apply the integer promotions
2024 to it and return the promoted value. */
2027 perform_integral_promotions (tree exp
)
2029 tree type
= TREE_TYPE (exp
);
2030 enum tree_code code
= TREE_CODE (type
);
2032 gcc_assert (INTEGRAL_TYPE_P (type
));
2034 /* Normally convert enums to int,
2035 but convert wide enums to something wider. */
2036 if (code
== ENUMERAL_TYPE
)
2038 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2039 TYPE_PRECISION (integer_type_node
)),
2040 ((TYPE_PRECISION (type
)
2041 >= TYPE_PRECISION (integer_type_node
))
2042 && TYPE_UNSIGNED (type
)));
2044 return convert (type
, exp
);
2047 /* ??? This should no longer be needed now bit-fields have their
2049 if (TREE_CODE (exp
) == COMPONENT_REF
2050 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2051 /* If it's thinner than an int, promote it like a
2052 c_promoting_integer_type_p, otherwise leave it alone. */
2053 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2054 TYPE_PRECISION (integer_type_node
)))
2055 return convert (integer_type_node
, exp
);
2057 if (c_promoting_integer_type_p (type
))
2059 /* Preserve unsignedness if not really getting any wider. */
2060 if (TYPE_UNSIGNED (type
)
2061 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2062 return convert (unsigned_type_node
, exp
);
2064 return convert (integer_type_node
, exp
);
2071 /* Perform default promotions for C data used in expressions.
2072 Enumeral types or short or char are converted to int.
2073 In addition, manifest constants symbols are replaced by their values. */
2076 default_conversion (tree exp
)
2079 tree type
= TREE_TYPE (exp
);
2080 enum tree_code code
= TREE_CODE (type
);
2083 mark_exp_read (exp
);
2085 /* Functions and arrays have been converted during parsing. */
2086 gcc_assert (code
!= FUNCTION_TYPE
);
2087 if (code
== ARRAY_TYPE
)
2090 /* Constants can be used directly unless they're not loadable. */
2091 if (TREE_CODE (exp
) == CONST_DECL
)
2092 exp
= DECL_INITIAL (exp
);
2094 /* Strip no-op conversions. */
2096 STRIP_TYPE_NOPS (exp
);
2098 if (TREE_NO_WARNING (orig_exp
))
2099 TREE_NO_WARNING (exp
) = 1;
2101 if (code
== VOID_TYPE
)
2103 error ("void value not ignored as it ought to be");
2104 return error_mark_node
;
2107 exp
= require_complete_type (exp
);
2108 if (exp
== error_mark_node
)
2109 return error_mark_node
;
2111 promoted_type
= targetm
.promoted_type (type
);
2113 return convert (promoted_type
, exp
);
2115 if (INTEGRAL_TYPE_P (type
))
2116 return perform_integral_promotions (exp
);
2121 /* Look up COMPONENT in a structure or union TYPE.
2123 If the component name is not found, returns NULL_TREE. Otherwise,
2124 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2125 stepping down the chain to the component, which is in the last
2126 TREE_VALUE of the list. Normally the list is of length one, but if
2127 the component is embedded within (nested) anonymous structures or
2128 unions, the list steps down the chain to the component. */
2131 lookup_field (tree type
, tree component
)
2135 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2136 to the field elements. Use a binary search on this array to quickly
2137 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2138 will always be set for structures which have many elements. */
2140 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2143 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2145 field
= TYPE_FIELDS (type
);
2147 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2148 while (top
- bot
> 1)
2150 half
= (top
- bot
+ 1) >> 1;
2151 field
= field_array
[bot
+half
];
2153 if (DECL_NAME (field
) == NULL_TREE
)
2155 /* Step through all anon unions in linear fashion. */
2156 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2158 field
= field_array
[bot
++];
2159 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2160 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
2162 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2165 return tree_cons (NULL_TREE
, field
, anon
);
2167 /* The Plan 9 compiler permits referring
2168 directly to an anonymous struct/union field
2169 using a typedef name. */
2170 if (flag_plan9_extensions
2171 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2172 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2174 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2180 /* Entire record is only anon unions. */
2184 /* Restart the binary search, with new lower bound. */
2188 if (DECL_NAME (field
) == component
)
2190 if (DECL_NAME (field
) < component
)
2196 if (DECL_NAME (field_array
[bot
]) == component
)
2197 field
= field_array
[bot
];
2198 else if (DECL_NAME (field
) != component
)
2203 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2205 if (DECL_NAME (field
) == NULL_TREE
2206 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2207 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
2209 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2212 return tree_cons (NULL_TREE
, field
, anon
);
2214 /* The Plan 9 compiler permits referring directly to an
2215 anonymous struct/union field using a typedef
2217 if (flag_plan9_extensions
2218 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2219 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2220 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2225 if (DECL_NAME (field
) == component
)
2229 if (field
== NULL_TREE
)
2233 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2236 /* Make an expression to refer to the COMPONENT field of structure or
2237 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2238 location of the COMPONENT_REF. */
2241 build_component_ref (location_t loc
, tree datum
, tree component
)
2243 tree type
= TREE_TYPE (datum
);
2244 enum tree_code code
= TREE_CODE (type
);
2247 bool datum_lvalue
= lvalue_p (datum
);
2249 if (!objc_is_public (datum
, component
))
2250 return error_mark_node
;
2252 /* Detect Objective-C property syntax object.property. */
2253 if (c_dialect_objc ()
2254 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2257 /* See if there is a field or component with name COMPONENT. */
2259 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2261 if (!COMPLETE_TYPE_P (type
))
2263 c_incomplete_type_error (NULL_TREE
, type
);
2264 return error_mark_node
;
2267 field
= lookup_field (type
, component
);
2271 error_at (loc
, "%qT has no member named %qE", type
, component
);
2272 return error_mark_node
;
2275 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2276 This might be better solved in future the way the C++ front
2277 end does it - by giving the anonymous entities each a
2278 separate name and type, and then have build_component_ref
2279 recursively call itself. We can't do that here. */
2282 tree subdatum
= TREE_VALUE (field
);
2285 bool use_datum_quals
;
2287 if (TREE_TYPE (subdatum
) == error_mark_node
)
2288 return error_mark_node
;
2290 /* If this is an rvalue, it does not have qualifiers in C
2291 standard terms and we must avoid propagating such
2292 qualifiers down to a non-lvalue array that is then
2293 converted to a pointer. */
2294 use_datum_quals
= (datum_lvalue
2295 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2297 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2298 if (use_datum_quals
)
2299 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2300 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2302 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2304 SET_EXPR_LOCATION (ref
, loc
);
2305 if (TREE_READONLY (subdatum
)
2306 || (use_datum_quals
&& TREE_READONLY (datum
)))
2307 TREE_READONLY (ref
) = 1;
2308 if (TREE_THIS_VOLATILE (subdatum
)
2309 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2310 TREE_THIS_VOLATILE (ref
) = 1;
2312 if (TREE_DEPRECATED (subdatum
))
2313 warn_deprecated_use (subdatum
, NULL_TREE
);
2317 field
= TREE_CHAIN (field
);
2323 else if (code
!= ERROR_MARK
)
2325 "request for member %qE in something not a structure or union",
2328 return error_mark_node
;
2331 /* Given an expression PTR for a pointer, return an expression
2332 for the value pointed to.
2333 ERRORSTRING is the name of the operator to appear in error messages.
2335 LOC is the location to use for the generated tree. */
2338 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2340 tree pointer
= default_conversion (ptr
);
2341 tree type
= TREE_TYPE (pointer
);
2344 if (TREE_CODE (type
) == POINTER_TYPE
)
2346 if (CONVERT_EXPR_P (pointer
)
2347 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2349 /* If a warning is issued, mark it to avoid duplicates from
2350 the backend. This only needs to be done at
2351 warn_strict_aliasing > 2. */
2352 if (warn_strict_aliasing
> 2)
2353 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2354 type
, TREE_OPERAND (pointer
, 0)))
2355 TREE_NO_WARNING (pointer
) = 1;
2358 if (TREE_CODE (pointer
) == ADDR_EXPR
2359 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2360 == TREE_TYPE (type
)))
2362 ref
= TREE_OPERAND (pointer
, 0);
2363 protected_set_expr_location (ref
, loc
);
2368 tree t
= TREE_TYPE (type
);
2370 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2372 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2374 error_at (loc
, "dereferencing pointer to incomplete type");
2375 return error_mark_node
;
2377 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2378 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2380 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2381 so that we get the proper error message if the result is used
2382 to assign to. Also, &* is supposed to be a no-op.
2383 And ANSI C seems to specify that the type of the result
2384 should be the const type. */
2385 /* A de-reference of a pointer to const is not a const. It is valid
2386 to change it via some other pointer. */
2387 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2388 TREE_SIDE_EFFECTS (ref
)
2389 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2390 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2391 protected_set_expr_location (ref
, loc
);
2395 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2396 invalid_indirection_error (loc
, type
, errstring
);
2398 return error_mark_node
;
2401 /* This handles expressions of the form "a[i]", which denotes
2404 This is logically equivalent in C to *(a+i), but we may do it differently.
2405 If A is a variable or a member, we generate a primitive ARRAY_REF.
2406 This avoids forcing the array out of registers, and can work on
2407 arrays that are not lvalues (for example, members of structures returned
2410 For vector types, allow vector[i] but not i[vector], and create
2411 *(((type*)&vectortype) + i) for the expression.
2413 LOC is the location to use for the returned expression. */
2416 build_array_ref (location_t loc
, tree array
, tree index
)
2419 bool swapped
= false;
2420 if (TREE_TYPE (array
) == error_mark_node
2421 || TREE_TYPE (index
) == error_mark_node
)
2422 return error_mark_node
;
2424 if (flag_enable_cilkplus
&& contains_array_notation_expr (index
))
2427 if (!find_rank (loc
, index
, index
, true, &rank
))
2428 return error_mark_node
;
2431 error_at (loc
, "rank of the array's index is greater than 1");
2432 return error_mark_node
;
2435 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2436 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2437 /* Allow vector[index] but not index[vector]. */
2438 && TREE_CODE (TREE_TYPE (array
)) != VECTOR_TYPE
)
2441 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2442 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2445 "subscripted value is neither array nor pointer nor vector");
2447 return error_mark_node
;
2455 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2457 error_at (loc
, "array subscript is not an integer");
2458 return error_mark_node
;
2461 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2463 error_at (loc
, "subscripted value is pointer to function");
2464 return error_mark_node
;
2467 /* ??? Existing practice has been to warn only when the char
2468 index is syntactically the index, not for char[array]. */
2470 warn_array_subscript_with_type_char (index
);
2472 /* Apply default promotions *after* noticing character types. */
2473 index
= default_conversion (index
);
2475 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2477 convert_vector_to_pointer_for_subscript (loc
, &array
, index
);
2479 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2483 /* An array that is indexed by a non-constant
2484 cannot be stored in a register; we must be able to do
2485 address arithmetic on its address.
2486 Likewise an array of elements of variable size. */
2487 if (TREE_CODE (index
) != INTEGER_CST
2488 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2489 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2491 if (!c_mark_addressable (array
))
2492 return error_mark_node
;
2494 /* An array that is indexed by a constant value which is not within
2495 the array bounds cannot be stored in a register either; because we
2496 would get a crash in store_bit_field/extract_bit_field when trying
2497 to access a non-existent part of the register. */
2498 if (TREE_CODE (index
) == INTEGER_CST
2499 && TYPE_DOMAIN (TREE_TYPE (array
))
2500 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2502 if (!c_mark_addressable (array
))
2503 return error_mark_node
;
2509 while (TREE_CODE (foo
) == COMPONENT_REF
)
2510 foo
= TREE_OPERAND (foo
, 0);
2511 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2512 pedwarn (loc
, OPT_Wpedantic
,
2513 "ISO C forbids subscripting %<register%> array");
2514 else if (!flag_isoc99
&& !lvalue_p (foo
))
2515 pedwarn (loc
, OPT_Wpedantic
,
2516 "ISO C90 forbids subscripting non-lvalue array");
2519 type
= TREE_TYPE (TREE_TYPE (array
));
2520 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2521 /* Array ref is const/volatile if the array elements are
2522 or if the array is. */
2523 TREE_READONLY (rval
)
2524 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2525 | TREE_READONLY (array
));
2526 TREE_SIDE_EFFECTS (rval
)
2527 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2528 | TREE_SIDE_EFFECTS (array
));
2529 TREE_THIS_VOLATILE (rval
)
2530 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2531 /* This was added by rms on 16 Nov 91.
2532 It fixes vol struct foo *a; a->elts[1]
2533 in an inline function.
2534 Hope it doesn't break something else. */
2535 | TREE_THIS_VOLATILE (array
));
2536 ret
= require_complete_type (rval
);
2537 protected_set_expr_location (ret
, loc
);
2542 tree ar
= default_conversion (array
);
2544 if (ar
== error_mark_node
)
2547 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2548 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2550 return build_indirect_ref
2551 (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
, index
, 0),
2556 /* Build an external reference to identifier ID. FUN indicates
2557 whether this will be used for a function call. LOC is the source
2558 location of the identifier. This sets *TYPE to the type of the
2559 identifier, which is not the same as the type of the returned value
2560 for CONST_DECLs defined as enum constants. If the type of the
2561 identifier is not available, *TYPE is set to NULL. */
2563 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2566 tree decl
= lookup_name (id
);
2568 /* In Objective-C, an instance variable (ivar) may be preferred to
2569 whatever lookup_name() found. */
2570 decl
= objc_lookup_ivar (decl
, id
);
2573 if (decl
&& decl
!= error_mark_node
)
2576 *type
= TREE_TYPE (ref
);
2579 /* Implicit function declaration. */
2580 ref
= implicitly_declare (loc
, id
);
2581 else if (decl
== error_mark_node
)
2582 /* Don't complain about something that's already been
2583 complained about. */
2584 return error_mark_node
;
2587 undeclared_variable (loc
, id
);
2588 return error_mark_node
;
2591 if (TREE_TYPE (ref
) == error_mark_node
)
2592 return error_mark_node
;
2594 if (TREE_DEPRECATED (ref
))
2595 warn_deprecated_use (ref
, NULL_TREE
);
2597 /* Recursive call does not count as usage. */
2598 if (ref
!= current_function_decl
)
2600 TREE_USED (ref
) = 1;
2603 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2605 if (!in_sizeof
&& !in_typeof
)
2606 C_DECL_USED (ref
) = 1;
2607 else if (DECL_INITIAL (ref
) == 0
2608 && DECL_EXTERNAL (ref
)
2609 && !TREE_PUBLIC (ref
))
2610 record_maybe_used_decl (ref
);
2613 if (TREE_CODE (ref
) == CONST_DECL
)
2615 used_types_insert (TREE_TYPE (ref
));
2618 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2619 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2621 warning_at (loc
, OPT_Wc___compat
,
2622 ("enum constant defined in struct or union "
2623 "is not visible in C++"));
2624 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2627 ref
= DECL_INITIAL (ref
);
2628 TREE_CONSTANT (ref
) = 1;
2630 else if (current_function_decl
!= 0
2631 && !DECL_FILE_SCOPE_P (current_function_decl
)
2632 && (TREE_CODE (ref
) == VAR_DECL
2633 || TREE_CODE (ref
) == PARM_DECL
2634 || TREE_CODE (ref
) == FUNCTION_DECL
))
2636 tree context
= decl_function_context (ref
);
2638 if (context
!= 0 && context
!= current_function_decl
)
2639 DECL_NONLOCAL (ref
) = 1;
2641 /* C99 6.7.4p3: An inline definition of a function with external
2642 linkage ... shall not contain a reference to an identifier with
2643 internal linkage. */
2644 else if (current_function_decl
!= 0
2645 && DECL_DECLARED_INLINE_P (current_function_decl
)
2646 && DECL_EXTERNAL (current_function_decl
)
2647 && VAR_OR_FUNCTION_DECL_P (ref
)
2648 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2649 && ! TREE_PUBLIC (ref
)
2650 && DECL_CONTEXT (ref
) != current_function_decl
)
2651 record_inline_static (loc
, current_function_decl
, ref
,
2657 /* Record details of decls possibly used inside sizeof or typeof. */
2658 struct maybe_used_decl
2662 /* The level seen at (in_sizeof + in_typeof). */
2664 /* The next one at this level or above, or NULL. */
2665 struct maybe_used_decl
*next
;
2668 static struct maybe_used_decl
*maybe_used_decls
;
2670 /* Record that DECL, an undefined static function reference seen
2671 inside sizeof or typeof, might be used if the operand of sizeof is
2672 a VLA type or the operand of typeof is a variably modified
2676 record_maybe_used_decl (tree decl
)
2678 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2680 t
->level
= in_sizeof
+ in_typeof
;
2681 t
->next
= maybe_used_decls
;
2682 maybe_used_decls
= t
;
2685 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2686 USED is false, just discard them. If it is true, mark them used
2687 (if no longer inside sizeof or typeof) or move them to the next
2688 level up (if still inside sizeof or typeof). */
2691 pop_maybe_used (bool used
)
2693 struct maybe_used_decl
*p
= maybe_used_decls
;
2694 int cur_level
= in_sizeof
+ in_typeof
;
2695 while (p
&& p
->level
> cur_level
)
2700 C_DECL_USED (p
->decl
) = 1;
2702 p
->level
= cur_level
;
2706 if (!used
|| cur_level
== 0)
2707 maybe_used_decls
= p
;
2710 /* Return the result of sizeof applied to EXPR. */
2713 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2716 if (expr
.value
== error_mark_node
)
2718 ret
.value
= error_mark_node
;
2719 ret
.original_code
= ERROR_MARK
;
2720 ret
.original_type
= NULL
;
2721 pop_maybe_used (false);
2725 bool expr_const_operands
= true;
2726 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2727 &expr_const_operands
);
2728 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2729 c_last_sizeof_arg
= expr
.value
;
2730 ret
.original_code
= SIZEOF_EXPR
;
2731 ret
.original_type
= NULL
;
2732 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2734 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2735 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2736 folded_expr
, ret
.value
);
2737 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2738 SET_EXPR_LOCATION (ret
.value
, loc
);
2740 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2745 /* Return the result of sizeof applied to T, a structure for the type
2746 name passed to sizeof (rather than the type itself). LOC is the
2747 location of the original expression. */
2750 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2754 tree type_expr
= NULL_TREE
;
2755 bool type_expr_const
= true;
2756 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2757 ret
.value
= c_sizeof (loc
, type
);
2758 c_last_sizeof_arg
= type
;
2759 ret
.original_code
= SIZEOF_EXPR
;
2760 ret
.original_type
= NULL
;
2761 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2762 && c_vla_type_p (type
))
2764 /* If the type is a [*] array, it is a VLA but is represented as
2765 having a size of zero. In such a case we must ensure that
2766 the result of sizeof does not get folded to a constant by
2767 c_fully_fold, because if the size is evaluated the result is
2768 not constant and so constraints on zero or negative size
2769 arrays must not be applied when this sizeof call is inside
2770 another array declarator. */
2772 type_expr
= integer_zero_node
;
2773 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2774 type_expr
, ret
.value
);
2775 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2777 pop_maybe_used (type
!= error_mark_node
2778 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2782 /* Build a function call to function FUNCTION with parameters PARAMS.
2783 The function call is at LOC.
2784 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2785 TREE_VALUE of each node is a parameter-expression.
2786 FUNCTION's data type may be a function type or a pointer-to-function. */
2789 build_function_call (location_t loc
, tree function
, tree params
)
2791 vec
<tree
, va_gc
> *v
;
2794 vec_alloc (v
, list_length (params
));
2795 for (; params
; params
= TREE_CHAIN (params
))
2796 v
->quick_push (TREE_VALUE (params
));
2797 ret
= build_function_call_vec (loc
, function
, v
, NULL
);
2802 /* Give a note about the location of the declaration of DECL. */
2804 static void inform_declaration (tree decl
)
2806 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_BUILT_IN (decl
)))
2807 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2810 /* Build a function call to function FUNCTION with parameters PARAMS.
2811 ORIGTYPES, if not NULL, is a vector of types; each element is
2812 either NULL or the original type of the corresponding element in
2813 PARAMS. The original type may differ from TREE_TYPE of the
2814 parameter for enums. FUNCTION's data type may be a function type
2815 or pointer-to-function. This function changes the elements of
2819 build_function_call_vec (location_t loc
, tree function
,
2820 vec
<tree
, va_gc
> *params
,
2821 vec
<tree
, va_gc
> *origtypes
)
2823 tree fntype
, fundecl
= 0;
2824 tree name
= NULL_TREE
, result
;
2830 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2831 STRIP_TYPE_NOPS (function
);
2833 /* Convert anything with function type to a pointer-to-function. */
2834 if (TREE_CODE (function
) == FUNCTION_DECL
)
2836 /* Implement type-directed function overloading for builtins.
2837 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2838 handle all the type checking. The result is a complete expression
2839 that implements this function call. */
2840 tem
= resolve_overloaded_builtin (loc
, function
, params
);
2844 name
= DECL_NAME (function
);
2847 tm_malloc_replacement (function
);
2849 /* Atomic functions have type checking/casting already done. They are
2850 often rewritten and don't match the original parameter list. */
2851 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
2854 if (flag_enable_cilkplus
2855 && is_cilkplus_reduce_builtin (function
))
2858 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2859 function
= function_to_pointer_conversion (loc
, function
);
2861 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2862 expressions, like those used for ObjC messenger dispatches. */
2863 if (params
&& !params
->is_empty ())
2864 function
= objc_rewrite_function_call (function
, (*params
)[0]);
2866 function
= c_fully_fold (function
, false, NULL
);
2868 fntype
= TREE_TYPE (function
);
2870 if (TREE_CODE (fntype
) == ERROR_MARK
)
2871 return error_mark_node
;
2873 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2874 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2876 if (!flag_diagnostics_show_caret
)
2878 "called object %qE is not a function or function pointer",
2880 else if (DECL_P (function
))
2883 "called object %qD is not a function or function pointer",
2885 inform_declaration (function
);
2889 "called object is not a function or function pointer");
2890 return error_mark_node
;
2893 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2894 current_function_returns_abnormally
= 1;
2896 /* fntype now gets the type of function pointed to. */
2897 fntype
= TREE_TYPE (fntype
);
2899 /* Convert the parameters to the types declared in the
2900 function prototype, or apply default promotions. */
2902 nargs
= convert_arguments (TYPE_ARG_TYPES (fntype
), params
, origtypes
,
2905 return error_mark_node
;
2907 /* Check that the function is called through a compatible prototype.
2908 If it is not, replace the call by a trap, wrapped up in a compound
2909 expression if necessary. This has the nice side-effect to prevent
2910 the tree-inliner from generating invalid assignment trees which may
2911 blow up in the RTL expander later. */
2912 if (CONVERT_EXPR_P (function
)
2913 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2914 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2915 && !comptypes (fntype
, TREE_TYPE (tem
)))
2917 tree return_type
= TREE_TYPE (fntype
);
2918 tree trap
= build_function_call (loc
,
2919 builtin_decl_explicit (BUILT_IN_TRAP
),
2923 /* This situation leads to run-time undefined behavior. We can't,
2924 therefore, simply error unless we can prove that all possible
2925 executions of the program must execute the code. */
2926 if (warning_at (loc
, 0, "function called through a non-compatible type"))
2927 /* We can, however, treat "undefined" any way we please.
2928 Call abort to encourage the user to fix the program. */
2929 inform (loc
, "if this code is reached, the program will abort");
2930 /* Before the abort, allow the function arguments to exit or
2932 for (i
= 0; i
< nargs
; i
++)
2933 trap
= build2 (COMPOUND_EXPR
, void_type_node
, (*params
)[i
], trap
);
2935 if (VOID_TYPE_P (return_type
))
2937 if (TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2939 "function with qualified void return type called");
2946 if (AGGREGATE_TYPE_P (return_type
))
2947 rhs
= build_compound_literal (loc
, return_type
,
2948 build_constructor (return_type
,
2952 rhs
= build_zero_cst (return_type
);
2954 return require_complete_type (build2 (COMPOUND_EXPR
, return_type
,
2959 argarray
= vec_safe_address (params
);
2961 /* Check that arguments to builtin functions match the expectations. */
2963 && DECL_BUILT_IN (fundecl
)
2964 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2965 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2966 return error_mark_node
;
2968 /* Check that the arguments to the function are valid. */
2969 check_function_arguments (fntype
, nargs
, argarray
);
2971 if (name
!= NULL_TREE
2972 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
2974 if (require_constant_value
)
2976 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
2977 function
, nargs
, argarray
);
2979 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
2980 function
, nargs
, argarray
);
2981 if (TREE_CODE (result
) == NOP_EXPR
2982 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
2983 STRIP_TYPE_NOPS (result
);
2986 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
2987 function
, nargs
, argarray
);
2989 if (VOID_TYPE_P (TREE_TYPE (result
)))
2991 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
2993 "function with qualified void return type called");
2996 return require_complete_type (result
);
2999 /* Convert the argument expressions in the vector VALUES
3000 to the types in the list TYPELIST.
3002 If TYPELIST is exhausted, or when an element has NULL as its type,
3003 perform the default conversions.
3005 ORIGTYPES is the original types of the expressions in VALUES. This
3006 holds the type of enum values which have been converted to integral
3007 types. It may be NULL.
3009 FUNCTION is a tree for the called function. It is used only for
3010 error messages, where it is formatted with %qE.
3012 This is also where warnings about wrong number of args are generated.
3014 Returns the actual number of arguments processed (which may be less
3015 than the length of VALUES in some error situations), or -1 on
3019 convert_arguments (tree typelist
, vec
<tree
, va_gc
> *values
,
3020 vec
<tree
, va_gc
> *origtypes
, tree function
, tree fundecl
)
3023 unsigned int parmnum
;
3024 bool error_args
= false;
3025 const bool type_generic
= fundecl
3026 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl
)));
3027 bool type_generic_remove_excess_precision
= false;
3030 /* Change pointer to function to the function itself for
3032 if (TREE_CODE (function
) == ADDR_EXPR
3033 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3034 function
= TREE_OPERAND (function
, 0);
3036 /* Handle an ObjC selector specially for diagnostics. */
3037 selector
= objc_message_selector ();
3039 /* For type-generic built-in functions, determine whether excess
3040 precision should be removed (classification) or not
3043 && DECL_BUILT_IN (fundecl
)
3044 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3046 switch (DECL_FUNCTION_CODE (fundecl
))
3048 case BUILT_IN_ISFINITE
:
3049 case BUILT_IN_ISINF
:
3050 case BUILT_IN_ISINF_SIGN
:
3051 case BUILT_IN_ISNAN
:
3052 case BUILT_IN_ISNORMAL
:
3053 case BUILT_IN_FPCLASSIFY
:
3054 type_generic_remove_excess_precision
= true;
3058 type_generic_remove_excess_precision
= false;
3062 if (flag_enable_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3063 return vec_safe_length (values
);
3065 /* Scan the given expressions and types, producing individual
3066 converted arguments. */
3068 for (typetail
= typelist
, parmnum
= 0;
3069 values
&& values
->iterate (parmnum
, &val
);
3072 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3073 tree valtype
= TREE_TYPE (val
);
3074 tree rname
= function
;
3075 int argnum
= parmnum
+ 1;
3076 const char *invalid_func_diag
;
3077 bool excess_precision
= false;
3081 if (type
== void_type_node
)
3084 error_at (input_location
,
3085 "too many arguments to method %qE", selector
);
3087 error_at (input_location
,
3088 "too many arguments to function %qE", function
);
3089 inform_declaration (fundecl
);
3093 if (selector
&& argnum
> 2)
3099 npc
= null_pointer_constant_p (val
);
3101 /* If there is excess precision and a prototype, convert once to
3102 the required type rather than converting via the semantic
3103 type. Likewise without a prototype a float value represented
3104 as long double should be converted once to double. But for
3105 type-generic classification functions excess precision must
3107 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3108 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3110 val
= TREE_OPERAND (val
, 0);
3111 excess_precision
= true;
3113 val
= c_fully_fold (val
, false, NULL
);
3114 STRIP_TYPE_NOPS (val
);
3116 val
= require_complete_type (val
);
3120 /* Formal parm type is specified by a function prototype. */
3122 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3124 error ("type of formal parameter %d is incomplete", parmnum
+ 1);
3131 /* Optionally warn about conversions that
3132 differ from the default conversions. */
3133 if (warn_traditional_conversion
|| warn_traditional
)
3135 unsigned int formal_prec
= TYPE_PRECISION (type
);
3137 if (INTEGRAL_TYPE_P (type
)
3138 && TREE_CODE (valtype
) == REAL_TYPE
)
3139 warning (0, "passing argument %d of %qE as integer "
3140 "rather than floating due to prototype",
3142 if (INTEGRAL_TYPE_P (type
)
3143 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3144 warning (0, "passing argument %d of %qE as integer "
3145 "rather than complex due to prototype",
3147 else if (TREE_CODE (type
) == COMPLEX_TYPE
3148 && TREE_CODE (valtype
) == REAL_TYPE
)
3149 warning (0, "passing argument %d of %qE as complex "
3150 "rather than floating due to prototype",
3152 else if (TREE_CODE (type
) == REAL_TYPE
3153 && INTEGRAL_TYPE_P (valtype
))
3154 warning (0, "passing argument %d of %qE as floating "
3155 "rather than integer due to prototype",
3157 else if (TREE_CODE (type
) == COMPLEX_TYPE
3158 && INTEGRAL_TYPE_P (valtype
))
3159 warning (0, "passing argument %d of %qE as complex "
3160 "rather than integer due to prototype",
3162 else if (TREE_CODE (type
) == REAL_TYPE
3163 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3164 warning (0, "passing argument %d of %qE as floating "
3165 "rather than complex due to prototype",
3167 /* ??? At some point, messages should be written about
3168 conversions between complex types, but that's too messy
3170 else if (TREE_CODE (type
) == REAL_TYPE
3171 && TREE_CODE (valtype
) == REAL_TYPE
)
3173 /* Warn if any argument is passed as `float',
3174 since without a prototype it would be `double'. */
3175 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3176 && type
!= dfloat32_type_node
)
3177 warning (0, "passing argument %d of %qE as %<float%> "
3178 "rather than %<double%> due to prototype",
3181 /* Warn if mismatch between argument and prototype
3182 for decimal float types. Warn of conversions with
3183 binary float types and of precision narrowing due to
3185 else if (type
!= valtype
3186 && (type
== dfloat32_type_node
3187 || type
== dfloat64_type_node
3188 || type
== dfloat128_type_node
3189 || valtype
== dfloat32_type_node
3190 || valtype
== dfloat64_type_node
3191 || valtype
== dfloat128_type_node
)
3193 <= TYPE_PRECISION (valtype
)
3194 || (type
== dfloat128_type_node
3196 != dfloat64_type_node
3198 != dfloat32_type_node
)))
3199 || (type
== dfloat64_type_node
3201 != dfloat32_type_node
))))
3202 warning (0, "passing argument %d of %qE as %qT "
3203 "rather than %qT due to prototype",
3204 argnum
, rname
, type
, valtype
);
3207 /* Detect integer changing in width or signedness.
3208 These warnings are only activated with
3209 -Wtraditional-conversion, not with -Wtraditional. */
3210 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3211 && INTEGRAL_TYPE_P (valtype
))
3213 tree would_have_been
= default_conversion (val
);
3214 tree type1
= TREE_TYPE (would_have_been
);
3216 if (TREE_CODE (type
) == ENUMERAL_TYPE
3217 && (TYPE_MAIN_VARIANT (type
)
3218 == TYPE_MAIN_VARIANT (valtype
)))
3219 /* No warning if function asks for enum
3220 and the actual arg is that enum type. */
3222 else if (formal_prec
!= TYPE_PRECISION (type1
))
3223 warning (OPT_Wtraditional_conversion
,
3224 "passing argument %d of %qE "
3225 "with different width due to prototype",
3227 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3229 /* Don't complain if the formal parameter type
3230 is an enum, because we can't tell now whether
3231 the value was an enum--even the same enum. */
3232 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3234 else if (TREE_CODE (val
) == INTEGER_CST
3235 && int_fits_type_p (val
, type
))
3236 /* Change in signedness doesn't matter
3237 if a constant value is unaffected. */
3239 /* If the value is extended from a narrower
3240 unsigned type, it doesn't matter whether we
3241 pass it as signed or unsigned; the value
3242 certainly is the same either way. */
3243 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3244 && TYPE_UNSIGNED (valtype
))
3246 else if (TYPE_UNSIGNED (type
))
3247 warning (OPT_Wtraditional_conversion
,
3248 "passing argument %d of %qE "
3249 "as unsigned due to prototype",
3252 warning (OPT_Wtraditional_conversion
,
3253 "passing argument %d of %qE "
3254 "as signed due to prototype", argnum
, rname
);
3258 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3259 sake of better warnings from convert_and_check. */
3260 if (excess_precision
)
3261 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3262 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3263 parmval
= convert_for_assignment (input_location
, type
, val
,
3264 origtype
, ic_argpass
, npc
,
3268 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3269 && INTEGRAL_TYPE_P (type
)
3270 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3271 parmval
= default_conversion (parmval
);
3274 else if (TREE_CODE (valtype
) == REAL_TYPE
3275 && (TYPE_PRECISION (valtype
)
3276 <= TYPE_PRECISION (double_type_node
))
3277 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3278 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3279 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3285 /* Convert `float' to `double'. */
3286 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3287 warning (OPT_Wdouble_promotion
,
3288 "implicit conversion from %qT to %qT when passing "
3289 "argument to function",
3290 valtype
, double_type_node
);
3291 parmval
= convert (double_type_node
, val
);
3294 else if (excess_precision
&& !type_generic
)
3295 /* A "double" argument with excess precision being passed
3296 without a prototype or in variable arguments. */
3297 parmval
= convert (valtype
, val
);
3298 else if ((invalid_func_diag
=
3299 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3301 error (invalid_func_diag
);
3305 /* Convert `short' and `char' to full-size `int'. */
3306 parmval
= default_conversion (val
);
3308 (*values
)[parmnum
] = parmval
;
3309 if (parmval
== error_mark_node
)
3313 typetail
= TREE_CHAIN (typetail
);
3316 gcc_assert (parmnum
== vec_safe_length (values
));
3318 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3320 error_at (input_location
,
3321 "too few arguments to function %qE", function
);
3322 inform_declaration (fundecl
);
3326 return error_args
? -1 : (int) parmnum
;
3329 /* This is the entry point used by the parser to build unary operators
3330 in the input. CODE, a tree_code, specifies the unary operator, and
3331 ARG is the operand. For unary plus, the C parser currently uses
3332 CONVERT_EXPR for code.
3334 LOC is the location to use for the tree generated.
3338 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3340 struct c_expr result
;
3342 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
3343 result
.original_code
= code
;
3344 result
.original_type
= NULL
;
3346 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3347 overflow_warning (loc
, result
.value
);
3352 /* This is the entry point used by the parser to build binary operators
3353 in the input. CODE, a tree_code, specifies the binary operator, and
3354 ARG1 and ARG2 are the operands. In addition to constructing the
3355 expression, we check for operands that were written with other binary
3356 operators in a way that is likely to confuse the user.
3358 LOCATION is the location of the binary operator. */
3361 parser_build_binary_op (location_t location
, enum tree_code code
,
3362 struct c_expr arg1
, struct c_expr arg2
)
3364 struct c_expr result
;
3366 enum tree_code code1
= arg1
.original_code
;
3367 enum tree_code code2
= arg2
.original_code
;
3368 tree type1
= (arg1
.original_type
3369 ? arg1
.original_type
3370 : TREE_TYPE (arg1
.value
));
3371 tree type2
= (arg2
.original_type
3372 ? arg2
.original_type
3373 : TREE_TYPE (arg2
.value
));
3375 result
.value
= build_binary_op (location
, code
,
3376 arg1
.value
, arg2
.value
, 1);
3377 result
.original_code
= code
;
3378 result
.original_type
= NULL
;
3380 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3383 if (location
!= UNKNOWN_LOCATION
)
3384 protected_set_expr_location (result
.value
, location
);
3386 /* Check for cases such as x+y<<z which users are likely
3388 if (warn_parentheses
)
3389 warn_about_parentheses (input_location
, code
,
3390 code1
, arg1
.value
, code2
, arg2
.value
);
3392 if (warn_logical_op
)
3393 warn_logical_operator (input_location
, code
, TREE_TYPE (result
.value
),
3394 code1
, arg1
.value
, code2
, arg2
.value
);
3396 /* Warn about comparisons against string literals, with the exception
3397 of testing for equality or inequality of a string literal with NULL. */
3398 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3400 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3401 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3402 warning_at (location
, OPT_Waddress
,
3403 "comparison with string literal results in unspecified behavior");
3405 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3406 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3407 warning_at (location
, OPT_Waddress
,
3408 "comparison with string literal results in unspecified behavior");
3410 if (TREE_OVERFLOW_P (result
.value
)
3411 && !TREE_OVERFLOW_P (arg1
.value
)
3412 && !TREE_OVERFLOW_P (arg2
.value
))
3413 overflow_warning (location
, result
.value
);
3415 /* Warn about comparisons of different enum types. */
3416 if (warn_enum_compare
3417 && TREE_CODE_CLASS (code
) == tcc_comparison
3418 && TREE_CODE (type1
) == ENUMERAL_TYPE
3419 && TREE_CODE (type2
) == ENUMERAL_TYPE
3420 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3421 warning_at (location
, OPT_Wenum_compare
,
3422 "comparison between %qT and %qT",
3428 /* Return a tree for the difference of pointers OP0 and OP1.
3429 The resulting tree has type int. */
3432 pointer_diff (location_t loc
, tree op0
, tree op1
)
3434 tree restype
= ptrdiff_type_node
;
3435 tree result
, inttype
;
3437 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3438 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3439 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3440 tree con0
, con1
, lit0
, lit1
;
3441 tree orig_op1
= op1
;
3443 /* If the operands point into different address spaces, we need to
3444 explicitly convert them to pointers into the common address space
3445 before we can subtract the numerical address values. */
3448 addr_space_t as_common
;
3451 /* Determine the common superset address space. This is guaranteed
3452 to exist because the caller verified that comp_target_types
3453 returned non-zero. */
3454 if (!addr_space_superset (as0
, as1
, &as_common
))
3457 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3458 op0
= convert (common_type
, op0
);
3459 op1
= convert (common_type
, op1
);
3462 /* Determine integer type to perform computations in. This will usually
3463 be the same as the result type (ptrdiff_t), but may need to be a wider
3464 type if pointers for the address space are wider than ptrdiff_t. */
3465 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3466 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3471 if (TREE_CODE (target_type
) == VOID_TYPE
)
3472 pedwarn (loc
, OPT_Wpointer_arith
,
3473 "pointer of type %<void *%> used in subtraction");
3474 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3475 pedwarn (loc
, OPT_Wpointer_arith
,
3476 "pointer to a function used in subtraction");
3478 /* If the conversion to ptrdiff_type does anything like widening or
3479 converting a partial to an integral mode, we get a convert_expression
3480 that is in the way to do any simplifications.
3481 (fold-const.c doesn't know that the extra bits won't be needed.
3482 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3483 different mode in place.)
3484 So first try to find a common term here 'by hand'; we want to cover
3485 at least the cases that occur in legal static initializers. */
3486 if (CONVERT_EXPR_P (op0
)
3487 && (TYPE_PRECISION (TREE_TYPE (op0
))
3488 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0
, 0)))))
3489 con0
= TREE_OPERAND (op0
, 0);
3492 if (CONVERT_EXPR_P (op1
)
3493 && (TYPE_PRECISION (TREE_TYPE (op1
))
3494 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1
, 0)))))
3495 con1
= TREE_OPERAND (op1
, 0);
3499 if (TREE_CODE (con0
) == POINTER_PLUS_EXPR
)
3501 lit0
= TREE_OPERAND (con0
, 1);
3502 con0
= TREE_OPERAND (con0
, 0);
3505 lit0
= integer_zero_node
;
3507 if (TREE_CODE (con1
) == POINTER_PLUS_EXPR
)
3509 lit1
= TREE_OPERAND (con1
, 1);
3510 con1
= TREE_OPERAND (con1
, 0);
3513 lit1
= integer_zero_node
;
3515 if (operand_equal_p (con0
, con1
, 0))
3522 /* First do the subtraction as integers;
3523 then drop through to build the divide operator.
3524 Do not do default conversions on the minus operator
3525 in case restype is a short type. */
3527 op0
= build_binary_op (loc
,
3528 MINUS_EXPR
, convert (inttype
, op0
),
3529 convert (inttype
, op1
), 0);
3530 /* This generates an error if op1 is pointer to incomplete type. */
3531 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3532 error_at (loc
, "arithmetic on pointer to an incomplete type");
3534 /* This generates an error if op0 is pointer to incomplete type. */
3535 op1
= c_size_in_bytes (target_type
);
3537 /* Divide by the size, in easiest possible way. */
3538 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3539 op0
, convert (inttype
, op1
));
3541 /* Convert to final result type if necessary. */
3542 return convert (restype
, result
);
3545 /* Expand atomic compound assignments into an approriate sequence as
3546 specified by the C11 standard section 6.5.16.2.
3552 This sequence is used for all types for which these operations are
3555 In addition, built-in versions of the 'fe' prefixed routines may
3556 need to be invoked for floating point (real, complex or vector) when
3557 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3567 __atomic_load (addr, &old, SEQ_CST);
3568 feholdexcept (&fenv);
3570 newval = old op val;
3571 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3574 feclearexcept (FE_ALL_EXCEPT);
3577 feupdateenv (&fenv);
3579 Also note that the compiler is simply issuing the generic form of
3580 the atomic operations. This requires temp(s) and has their address
3581 taken. The atomic processing is smart enough to figure out when the
3582 size of an object can utilize a lock-free version, and convert the
3583 built-in call to the appropriate lock-free routine. The optimizers
3584 will then dispose of any temps that are no longer required, and
3585 lock-free implementations are utilized as long as there is target
3586 support for the required size.
3588 If the operator is NOP_EXPR, then this is a simple assignment, and
3589 an __atomic_store is issued to perform the assignment rather than
3594 /* Build an atomic assignment at LOC, expanding into the proper
3595 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3596 the result of the operation, unless RETURN_OLD_P in which case
3597 return the old value of LHS (this is only for postincrement and
3600 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3601 tree rhs
, bool return_old_p
)
3603 tree fndecl
, func_call
;
3604 vec
<tree
, va_gc
> *params
;
3605 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3608 tree stmt
, goto_stmt
;
3609 tree loop_label
, loop_decl
, done_label
, done_decl
;
3611 tree lhs_type
= TREE_TYPE (lhs
);
3612 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, 0);
3613 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3614 tree rhs_type
= TREE_TYPE (rhs
);
3616 gcc_assert (TYPE_ATOMIC (lhs_type
));
3619 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3621 /* Allocate enough vector items for a compare_exchange. */
3622 vec_alloc (params
, 6);
3624 /* Create a compound statement to hold the sequence of statements
3626 compound_stmt
= c_begin_compound_stmt (false);
3628 /* Fold the RHS if it hasn't already been folded. */
3629 if (modifycode
!= NOP_EXPR
)
3630 rhs
= c_fully_fold (rhs
, false, NULL
);
3632 /* Remove the qualifiers for the rest of the expressions and create
3633 the VAL temp variable to hold the RHS. */
3634 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3635 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3636 val
= create_tmp_var (nonatomic_rhs_type
, NULL
);
3637 TREE_ADDRESSABLE (val
) = 1;
3638 rhs
= build2 (MODIFY_EXPR
, nonatomic_rhs_type
, val
, rhs
);
3639 SET_EXPR_LOCATION (rhs
, loc
);
3642 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3644 if (modifycode
== NOP_EXPR
)
3646 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3647 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, 0);
3648 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3649 params
->quick_push (lhs_addr
);
3650 params
->quick_push (rhs
);
3651 params
->quick_push (seq_cst
);
3652 func_call
= build_function_call_vec (loc
, fndecl
, params
, NULL
);
3653 add_stmt (func_call
);
3655 /* Finish the compound statement. */
3656 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3658 /* VAL is the value which was stored, return a COMPOUND_STMT of
3659 the statement and that value. */
3660 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3663 /* Create the variables and labels required for the op= form. */
3664 old
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3665 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, 0);
3666 TREE_ADDRESSABLE (val
) = 1;
3668 newval
= create_tmp_var (nonatomic_lhs_type
, NULL
);
3669 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, 0);
3670 TREE_ADDRESSABLE (newval
) = 1;
3672 loop_decl
= create_artificial_label (loc
);
3673 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
3675 done_decl
= create_artificial_label (loc
);
3676 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
3678 /* __atomic_load (addr, &old, SEQ_CST). */
3679 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
3680 params
->quick_push (lhs_addr
);
3681 params
->quick_push (old_addr
);
3682 params
->quick_push (seq_cst
);
3683 func_call
= build_function_call_vec (loc
, fndecl
, params
, NULL
);
3684 add_stmt (func_call
);
3685 params
->truncate (0);
3687 /* Create the expressions for floating-point environment
3688 manipulation, if required. */
3689 bool need_fenv
= (flag_trapping_math
3690 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
3691 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
3693 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
3696 add_stmt (hold_call
);
3699 add_stmt (loop_label
);
3701 /* newval = old + val; */
3702 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
3703 rhs
= convert_for_assignment (loc
, nonatomic_lhs_type
, rhs
, NULL_TREE
,
3704 ic_assign
, false, NULL_TREE
,
3706 if (rhs
!= error_mark_node
)
3708 rhs
= build2 (MODIFY_EXPR
, nonatomic_lhs_type
, newval
, rhs
);
3709 SET_EXPR_LOCATION (rhs
, loc
);
3713 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3715 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
3716 params
->quick_push (lhs_addr
);
3717 params
->quick_push (old_addr
);
3718 params
->quick_push (newval_addr
);
3719 params
->quick_push (integer_zero_node
);
3720 params
->quick_push (seq_cst
);
3721 params
->quick_push (seq_cst
);
3722 func_call
= build_function_call_vec (loc
, fndecl
, params
, NULL
);
3724 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
3725 SET_EXPR_LOCATION (goto_stmt
, loc
);
3727 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
3728 SET_EXPR_LOCATION (stmt
, loc
);
3732 add_stmt (clear_call
);
3735 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
3736 SET_EXPR_LOCATION (goto_stmt
, loc
);
3737 add_stmt (goto_stmt
);
3740 add_stmt (done_label
);
3743 add_stmt (update_call
);
3745 /* Finish the compound statement. */
3746 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3748 /* NEWVAL is the value that was successfully stored, return a
3749 COMPOUND_EXPR of the statement and the appropriate value. */
3750 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
3751 return_old_p
? old
: newval
);
3754 /* Construct and perhaps optimize a tree representation
3755 for a unary operation. CODE, a tree_code, specifies the operation
3756 and XARG is the operand.
3757 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3758 the default promotions (such as from short to int).
3759 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3760 allows non-lvalues; this is only used to handle conversion of non-lvalue
3761 arrays to pointers in C99.
3763 LOCATION is the location of the operator. */
3766 build_unary_op (location_t location
,
3767 enum tree_code code
, tree xarg
, int flag
)
3769 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3772 enum tree_code typecode
;
3774 tree ret
= error_mark_node
;
3775 tree eptype
= NULL_TREE
;
3776 int noconvert
= flag
;
3777 const char *invalid_op_diag
;
3780 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3782 arg
= remove_c_maybe_const_expr (arg
);
3784 if (code
!= ADDR_EXPR
)
3785 arg
= require_complete_type (arg
);
3787 typecode
= TREE_CODE (TREE_TYPE (arg
));
3788 if (typecode
== ERROR_MARK
)
3789 return error_mark_node
;
3790 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3791 typecode
= INTEGER_TYPE
;
3793 if ((invalid_op_diag
3794 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3796 error_at (location
, invalid_op_diag
);
3797 return error_mark_node
;
3800 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3802 eptype
= TREE_TYPE (arg
);
3803 arg
= TREE_OPERAND (arg
, 0);
3809 /* This is used for unary plus, because a CONVERT_EXPR
3810 is enough to prevent anybody from looking inside for
3811 associativity, but won't generate any code. */
3812 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3813 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3814 || typecode
== VECTOR_TYPE
))
3816 error_at (location
, "wrong type argument to unary plus");
3817 return error_mark_node
;
3819 else if (!noconvert
)
3820 arg
= default_conversion (arg
);
3821 arg
= non_lvalue_loc (location
, arg
);
3825 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3826 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3827 || typecode
== VECTOR_TYPE
))
3829 error_at (location
, "wrong type argument to unary minus");
3830 return error_mark_node
;
3832 else if (!noconvert
)
3833 arg
= default_conversion (arg
);
3837 /* ~ works on integer types and non float vectors. */
3838 if (typecode
== INTEGER_TYPE
3839 || (typecode
== VECTOR_TYPE
3840 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3843 arg
= default_conversion (arg
);
3845 else if (typecode
== COMPLEX_TYPE
)
3848 pedwarn (location
, OPT_Wpedantic
,
3849 "ISO C does not support %<~%> for complex conjugation");
3851 arg
= default_conversion (arg
);
3855 error_at (location
, "wrong type argument to bit-complement");
3856 return error_mark_node
;
3861 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3863 error_at (location
, "wrong type argument to abs");
3864 return error_mark_node
;
3866 else if (!noconvert
)
3867 arg
= default_conversion (arg
);
3871 /* Conjugating a real value is a no-op, but allow it anyway. */
3872 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3873 || typecode
== COMPLEX_TYPE
))
3875 error_at (location
, "wrong type argument to conjugation");
3876 return error_mark_node
;
3878 else if (!noconvert
)
3879 arg
= default_conversion (arg
);
3882 case TRUTH_NOT_EXPR
:
3883 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3884 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3885 && typecode
!= COMPLEX_TYPE
)
3888 "wrong type argument to unary exclamation mark");
3889 return error_mark_node
;
3893 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
3894 arg
= remove_c_maybe_const_expr (arg
);
3897 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3898 ret
= invert_truthvalue_loc (location
, arg
);
3899 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3900 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3901 location
= EXPR_LOCATION (ret
);
3902 goto return_build_unary_op
;
3906 ret
= build_real_imag_expr (location
, code
, arg
);
3907 if (ret
== error_mark_node
)
3908 return error_mark_node
;
3909 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3910 eptype
= TREE_TYPE (eptype
);
3911 goto return_build_unary_op
;
3913 case PREINCREMENT_EXPR
:
3914 case POSTINCREMENT_EXPR
:
3915 case PREDECREMENT_EXPR
:
3916 case POSTDECREMENT_EXPR
:
3918 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3920 tree inner
= build_unary_op (location
, code
,
3921 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3922 if (inner
== error_mark_node
)
3923 return error_mark_node
;
3924 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3925 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3926 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3927 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3928 goto return_build_unary_op
;
3931 /* Complain about anything that is not a true lvalue. In
3932 Objective-C, skip this check for property_refs. */
3933 if (!objc_is_property_ref (arg
)
3934 && !lvalue_or_else (location
,
3935 arg
, ((code
== PREINCREMENT_EXPR
3936 || code
== POSTINCREMENT_EXPR
)
3939 return error_mark_node
;
3941 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
3943 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3944 warning_at (location
, OPT_Wc___compat
,
3945 "increment of enumeration value is invalid in C++");
3947 warning_at (location
, OPT_Wc___compat
,
3948 "decrement of enumeration value is invalid in C++");
3951 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3952 arg
= c_fully_fold (arg
, false, NULL
);
3955 atomic_op
= really_atomic_lvalue (arg
);
3957 /* Increment or decrement the real part of the value,
3958 and don't change the imaginary part. */
3959 if (typecode
== COMPLEX_TYPE
)
3963 pedwarn (location
, OPT_Wpedantic
,
3964 "ISO C does not support %<++%> and %<--%> on complex types");
3968 arg
= stabilize_reference (arg
);
3969 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
3970 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
3971 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
3972 if (real
== error_mark_node
|| imag
== error_mark_node
)
3973 return error_mark_node
;
3974 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
3976 goto return_build_unary_op
;
3980 /* Report invalid types. */
3982 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3983 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
3984 && typecode
!= COMPLEX_TYPE
)
3986 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
3987 error_at (location
, "wrong type argument to increment");
3989 error_at (location
, "wrong type argument to decrement");
3991 return error_mark_node
;
3997 argtype
= TREE_TYPE (arg
);
3999 /* Compute the increment. */
4001 if (typecode
== POINTER_TYPE
)
4003 /* If pointer target is an undefined struct,
4004 we just cannot know how to do the arithmetic. */
4005 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4007 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4009 "increment of pointer to unknown structure");
4012 "decrement of pointer to unknown structure");
4014 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4015 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4017 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4018 pedwarn (location
, OPT_Wpointer_arith
,
4019 "wrong type argument to increment");
4021 pedwarn (location
, OPT_Wpointer_arith
,
4022 "wrong type argument to decrement");
4025 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4026 inc
= convert_to_ptrofftype_loc (location
, inc
);
4028 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4030 /* For signed fract types, we invert ++ to -- or
4031 -- to ++, and change inc from 1 to -1, because
4032 it is not possible to represent 1 in signed fract constants.
4033 For unsigned fract types, the result always overflows and
4034 we get an undefined (original) or the maximum value. */
4035 if (code
== PREINCREMENT_EXPR
)
4036 code
= PREDECREMENT_EXPR
;
4037 else if (code
== PREDECREMENT_EXPR
)
4038 code
= PREINCREMENT_EXPR
;
4039 else if (code
== POSTINCREMENT_EXPR
)
4040 code
= POSTDECREMENT_EXPR
;
4041 else /* code == POSTDECREMENT_EXPR */
4042 code
= POSTINCREMENT_EXPR
;
4044 inc
= integer_minus_one_node
;
4045 inc
= convert (argtype
, inc
);
4049 inc
= integer_one_node
;
4050 inc
= convert (argtype
, inc
);
4053 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4054 need to ask Objective-C to build the increment or decrement
4055 expression for it. */
4056 if (objc_is_property_ref (arg
))
4057 return objc_build_incr_expr_for_property_ref (location
, code
,
4060 /* Report a read-only lvalue. */
4061 if (TYPE_READONLY (argtype
))
4063 readonly_error (location
, arg
,
4064 ((code
== PREINCREMENT_EXPR
4065 || code
== POSTINCREMENT_EXPR
)
4066 ? lv_increment
: lv_decrement
));
4067 return error_mark_node
;
4069 else if (TREE_READONLY (arg
))
4070 readonly_warning (arg
,
4071 ((code
== PREINCREMENT_EXPR
4072 || code
== POSTINCREMENT_EXPR
)
4073 ? lv_increment
: lv_decrement
));
4075 /* If the argument is atomic, use the special code sequences for
4076 atomic compound assignment. */
4079 arg
= stabilize_reference (arg
);
4080 ret
= build_atomic_assign (location
, arg
,
4081 ((code
== PREINCREMENT_EXPR
4082 || code
== POSTINCREMENT_EXPR
)
4085 (FRACT_MODE_P (TYPE_MODE (argtype
))
4087 : integer_one_node
),
4088 (code
== POSTINCREMENT_EXPR
4089 || code
== POSTDECREMENT_EXPR
));
4090 goto return_build_unary_op
;
4093 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4094 val
= boolean_increment (code
, arg
);
4096 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4097 TREE_SIDE_EFFECTS (val
) = 1;
4098 if (TREE_CODE (val
) != code
)
4099 TREE_NO_WARNING (val
) = 1;
4101 goto return_build_unary_op
;
4105 /* Note that this operation never does default_conversion. */
4107 /* The operand of unary '&' must be an lvalue (which excludes
4108 expressions of type void), or, in C99, the result of a [] or
4109 unary '*' operator. */
4110 if (VOID_TYPE_P (TREE_TYPE (arg
))
4111 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4112 && (TREE_CODE (arg
) != INDIRECT_REF
4114 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4116 /* Let &* cancel out to simplify resulting code. */
4117 if (TREE_CODE (arg
) == INDIRECT_REF
)
4119 /* Don't let this be an lvalue. */
4120 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4121 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4122 ret
= TREE_OPERAND (arg
, 0);
4123 goto return_build_unary_op
;
4126 /* For &x[y], return x+y */
4127 if (TREE_CODE (arg
) == ARRAY_REF
)
4129 tree op0
= TREE_OPERAND (arg
, 0);
4130 if (!c_mark_addressable (op0
))
4131 return error_mark_node
;
4134 /* Anything not already handled and not a true memory reference
4135 or a non-lvalue array is an error. */
4136 else if (typecode
!= FUNCTION_TYPE
&& !flag
4137 && !lvalue_or_else (location
, arg
, lv_addressof
))
4138 return error_mark_node
;
4140 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4142 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4144 tree inner
= build_unary_op (location
, code
,
4145 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
4146 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4147 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4148 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4149 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4150 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4151 goto return_build_unary_op
;
4154 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4155 argtype
= TREE_TYPE (arg
);
4157 /* If the lvalue is const or volatile, merge that into the type
4158 to which the address will point. This is only needed
4159 for function types. */
4160 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4161 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4162 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4164 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4165 int quals
= orig_quals
;
4167 if (TREE_READONLY (arg
))
4168 quals
|= TYPE_QUAL_CONST
;
4169 if (TREE_THIS_VOLATILE (arg
))
4170 quals
|= TYPE_QUAL_VOLATILE
;
4172 argtype
= c_build_qualified_type (argtype
, quals
);
4175 if (!c_mark_addressable (arg
))
4176 return error_mark_node
;
4178 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4179 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4181 argtype
= build_pointer_type (argtype
);
4183 /* ??? Cope with user tricks that amount to offsetof. Delete this
4184 when we have proper support for integer constant expressions. */
4185 val
= get_base_address (arg
);
4186 if (val
&& TREE_CODE (val
) == INDIRECT_REF
4187 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4189 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4190 goto return_build_unary_op
;
4193 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4196 goto return_build_unary_op
;
4203 argtype
= TREE_TYPE (arg
);
4204 if (TREE_CODE (arg
) == INTEGER_CST
)
4205 ret
= (require_constant_value
4206 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4207 : fold_build1_loc (location
, code
, argtype
, arg
));
4209 ret
= build1 (code
, argtype
, arg
);
4210 return_build_unary_op
:
4211 gcc_assert (ret
!= error_mark_node
);
4212 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4213 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4214 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4215 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4216 ret
= note_integer_operands (ret
);
4218 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4219 protected_set_expr_location (ret
, location
);
4223 /* Return nonzero if REF is an lvalue valid for this language.
4224 Lvalues can be assigned, unless their type has TYPE_READONLY.
4225 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4228 lvalue_p (const_tree ref
)
4230 const enum tree_code code
= TREE_CODE (ref
);
4237 return lvalue_p (TREE_OPERAND (ref
, 0));
4239 case C_MAYBE_CONST_EXPR
:
4240 return lvalue_p (TREE_OPERAND (ref
, 1));
4242 case COMPOUND_LITERAL_EXPR
:
4248 case ARRAY_NOTATION_REF
:
4253 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4254 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4257 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4264 /* Give a warning for storing in something that is read-only in GCC
4265 terms but not const in ISO C terms. */
4268 readonly_warning (tree arg
, enum lvalue_use use
)
4273 warning (0, "assignment of read-only location %qE", arg
);
4276 warning (0, "increment of read-only location %qE", arg
);
4279 warning (0, "decrement of read-only location %qE", arg
);
4288 /* Return nonzero if REF is an lvalue valid for this language;
4289 otherwise, print an error message and return zero. USE says
4290 how the lvalue is being used and so selects the error message.
4291 LOCATION is the location at which any error should be reported. */
4294 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4296 int win
= lvalue_p (ref
);
4299 lvalue_error (loc
, use
);
4304 /* Mark EXP saying that we need to be able to take the
4305 address of it; it should not be allocated in a register.
4306 Returns true if successful. */
4309 c_mark_addressable (tree exp
)
4314 switch (TREE_CODE (x
))
4317 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
4320 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
4324 /* ... fall through ... */
4330 x
= TREE_OPERAND (x
, 0);
4333 case COMPOUND_LITERAL_EXPR
:
4335 TREE_ADDRESSABLE (x
) = 1;
4342 if (C_DECL_REGISTER (x
)
4343 && DECL_NONLOCAL (x
))
4345 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4348 ("global register variable %qD used in nested function", x
);
4351 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4353 else if (C_DECL_REGISTER (x
))
4355 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4356 error ("address of global register variable %qD requested", x
);
4358 error ("address of register variable %qD requested", x
);
4364 TREE_ADDRESSABLE (x
) = 1;
4371 /* Convert EXPR to TYPE, warning about conversion problems with
4372 constants. SEMANTIC_TYPE is the type this conversion would use
4373 without excess precision. If SEMANTIC_TYPE is NULL, this function
4374 is equivalent to convert_and_check. This function is a wrapper that
4375 handles conversions that may be different than
4376 the usual ones because of excess precision. */
4379 ep_convert_and_check (tree type
, tree expr
, tree semantic_type
)
4381 if (TREE_TYPE (expr
) == type
)
4385 return convert_and_check (type
, expr
);
4387 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4388 && TREE_TYPE (expr
) != semantic_type
)
4390 /* For integers, we need to check the real conversion, not
4391 the conversion to the excess precision type. */
4392 expr
= convert_and_check (semantic_type
, expr
);
4394 /* Result type is the excess precision type, which should be
4395 large enough, so do not check. */
4396 return convert (type
, expr
);
4399 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4400 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4401 if folded to an integer constant then the unselected half may
4402 contain arbitrary operations not normally permitted in constant
4403 expressions. Set the location of the expression to LOC. */
4406 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4407 tree op1
, tree op1_original_type
, tree op2
,
4408 tree op2_original_type
)
4412 enum tree_code code1
;
4413 enum tree_code code2
;
4414 tree result_type
= NULL
;
4415 tree semantic_result_type
= NULL
;
4416 tree orig_op1
= op1
, orig_op2
= op2
;
4417 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4418 bool ifexp_int_operands
;
4421 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4422 if (op1_int_operands
)
4423 op1
= remove_c_maybe_const_expr (op1
);
4424 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4425 if (op2_int_operands
)
4426 op2
= remove_c_maybe_const_expr (op2
);
4427 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4428 if (ifexp_int_operands
)
4429 ifexp
= remove_c_maybe_const_expr (ifexp
);
4431 /* Promote both alternatives. */
4433 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4434 op1
= default_conversion (op1
);
4435 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4436 op2
= default_conversion (op2
);
4438 if (TREE_CODE (ifexp
) == ERROR_MARK
4439 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4440 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4441 return error_mark_node
;
4443 type1
= TREE_TYPE (op1
);
4444 code1
= TREE_CODE (type1
);
4445 type2
= TREE_TYPE (op2
);
4446 code2
= TREE_CODE (type2
);
4448 /* C90 does not permit non-lvalue arrays in conditional expressions.
4449 In C99 they will be pointers by now. */
4450 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4452 error_at (colon_loc
, "non-lvalue array in conditional expression");
4453 return error_mark_node
;
4456 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4457 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4458 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4459 || code1
== COMPLEX_TYPE
)
4460 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4461 || code2
== COMPLEX_TYPE
))
4463 semantic_result_type
= c_common_type (type1
, type2
);
4464 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4466 op1
= TREE_OPERAND (op1
, 0);
4467 type1
= TREE_TYPE (op1
);
4468 gcc_assert (TREE_CODE (type1
) == code1
);
4470 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4472 op2
= TREE_OPERAND (op2
, 0);
4473 type2
= TREE_TYPE (op2
);
4474 gcc_assert (TREE_CODE (type2
) == code2
);
4478 if (warn_cxx_compat
)
4480 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4481 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4483 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4484 && TREE_CODE (t2
) == ENUMERAL_TYPE
4485 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4486 warning_at (colon_loc
, OPT_Wc___compat
,
4487 ("different enum types in conditional is "
4488 "invalid in C++: %qT vs %qT"),
4492 /* Quickly detect the usual case where op1 and op2 have the same type
4494 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4497 result_type
= type1
;
4499 result_type
= TYPE_MAIN_VARIANT (type1
);
4501 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4502 || code1
== COMPLEX_TYPE
)
4503 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4504 || code2
== COMPLEX_TYPE
))
4506 result_type
= c_common_type (type1
, type2
);
4507 do_warn_double_promotion (result_type
, type1
, type2
,
4508 "implicit conversion from %qT to %qT to "
4509 "match other result of conditional",
4512 /* If -Wsign-compare, warn here if type1 and type2 have
4513 different signedness. We'll promote the signed to unsigned
4514 and later code won't know it used to be different.
4515 Do this check on the original types, so that explicit casts
4516 will be considered, but default promotions won't. */
4517 if (c_inhibit_evaluation_warnings
== 0)
4519 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4520 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4522 if (unsigned_op1
^ unsigned_op2
)
4526 /* Do not warn if the result type is signed, since the
4527 signed type will only be chosen if it can represent
4528 all the values of the unsigned type. */
4529 if (!TYPE_UNSIGNED (result_type
))
4533 bool op1_maybe_const
= true;
4534 bool op2_maybe_const
= true;
4536 /* Do not warn if the signed quantity is an
4537 unsuffixed integer literal (or some static
4538 constant expression involving such literals) and
4539 it is non-negative. This warning requires the
4540 operands to be folded for best results, so do
4541 that folding in this case even without
4542 warn_sign_compare to avoid warning options
4543 possibly affecting code generation. */
4544 c_inhibit_evaluation_warnings
4545 += (ifexp
== truthvalue_false_node
);
4546 op1
= c_fully_fold (op1
, require_constant_value
,
4548 c_inhibit_evaluation_warnings
4549 -= (ifexp
== truthvalue_false_node
);
4551 c_inhibit_evaluation_warnings
4552 += (ifexp
== truthvalue_true_node
);
4553 op2
= c_fully_fold (op2
, require_constant_value
,
4555 c_inhibit_evaluation_warnings
4556 -= (ifexp
== truthvalue_true_node
);
4558 if (warn_sign_compare
)
4561 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4563 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4566 warning_at (colon_loc
, OPT_Wsign_compare
,
4567 ("signed and unsigned type in "
4568 "conditional expression"));
4570 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4571 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4572 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4573 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4578 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4580 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4581 pedwarn (colon_loc
, OPT_Wpedantic
,
4582 "ISO C forbids conditional expr with only one void side");
4583 result_type
= void_type_node
;
4585 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4587 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
4588 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
4589 addr_space_t as_common
;
4591 if (comp_target_types (colon_loc
, type1
, type2
))
4592 result_type
= common_pointer_type (type1
, type2
);
4593 else if (null_pointer_constant_p (orig_op1
))
4594 result_type
= type2
;
4595 else if (null_pointer_constant_p (orig_op2
))
4596 result_type
= type1
;
4597 else if (!addr_space_superset (as1
, as2
, &as_common
))
4599 error_at (colon_loc
, "pointers to disjoint address spaces "
4600 "used in conditional expression");
4601 return error_mark_node
;
4603 else if (VOID_TYPE_P (TREE_TYPE (type1
))
4604 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
4606 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
4607 pedwarn (colon_loc
, OPT_Wpedantic
,
4608 "ISO C forbids conditional expr between "
4609 "%<void *%> and function pointer");
4610 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
4611 TREE_TYPE (type2
)));
4613 else if (VOID_TYPE_P (TREE_TYPE (type2
))
4614 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
4616 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
4617 pedwarn (colon_loc
, OPT_Wpedantic
,
4618 "ISO C forbids conditional expr between "
4619 "%<void *%> and function pointer");
4620 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
4621 TREE_TYPE (type1
)));
4623 /* Objective-C pointer comparisons are a bit more lenient. */
4624 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
4625 result_type
= objc_common_type (type1
, type2
);
4628 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
4630 pedwarn (colon_loc
, 0,
4631 "pointer type mismatch in conditional expression");
4632 result_type
= build_pointer_type
4633 (build_qualified_type (void_type_node
, qual
));
4636 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4638 if (!null_pointer_constant_p (orig_op2
))
4639 pedwarn (colon_loc
, 0,
4640 "pointer/integer type mismatch in conditional expression");
4643 op2
= null_pointer_node
;
4645 result_type
= type1
;
4647 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4649 if (!null_pointer_constant_p (orig_op1
))
4650 pedwarn (colon_loc
, 0,
4651 "pointer/integer type mismatch in conditional expression");
4654 op1
= null_pointer_node
;
4656 result_type
= type2
;
4661 if (flag_cond_mismatch
)
4662 result_type
= void_type_node
;
4665 error_at (colon_loc
, "type mismatch in conditional expression");
4666 return error_mark_node
;
4670 /* Merge const and volatile flags of the incoming types. */
4672 = build_type_variant (result_type
,
4673 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4674 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4676 op1
= ep_convert_and_check (result_type
, op1
, semantic_result_type
);
4677 op2
= ep_convert_and_check (result_type
, op2
, semantic_result_type
);
4679 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4681 op2_int_operands
= true;
4682 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4684 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4686 op1_int_operands
= true;
4687 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4689 int_const
= int_operands
= (ifexp_int_operands
4691 && op2_int_operands
);
4694 int_const
= ((ifexp
== truthvalue_true_node
4695 && TREE_CODE (orig_op1
) == INTEGER_CST
4696 && !TREE_OVERFLOW (orig_op1
))
4697 || (ifexp
== truthvalue_false_node
4698 && TREE_CODE (orig_op2
) == INTEGER_CST
4699 && !TREE_OVERFLOW (orig_op2
)));
4701 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4702 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4707 op1
= remove_c_maybe_const_expr (op1
);
4708 op2
= remove_c_maybe_const_expr (op2
);
4710 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4712 ret
= note_integer_operands (ret
);
4714 if (semantic_result_type
)
4715 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
4717 protected_set_expr_location (ret
, colon_loc
);
4721 /* Return a compound expression that performs two expressions and
4722 returns the value of the second of them.
4724 LOC is the location of the COMPOUND_EXPR. */
4727 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4729 bool expr1_int_operands
, expr2_int_operands
;
4730 tree eptype
= NULL_TREE
;
4733 if (flag_enable_cilkplus
4734 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
4735 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
4738 "spawned function call cannot be part of a comma expression");
4739 return error_mark_node
;
4741 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4742 if (expr1_int_operands
)
4743 expr1
= remove_c_maybe_const_expr (expr1
);
4744 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4745 if (expr2_int_operands
)
4746 expr2
= remove_c_maybe_const_expr (expr2
);
4748 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4749 expr1
= TREE_OPERAND (expr1
, 0);
4750 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4752 eptype
= TREE_TYPE (expr2
);
4753 expr2
= TREE_OPERAND (expr2
, 0);
4756 if (!TREE_SIDE_EFFECTS (expr1
))
4758 /* The left-hand operand of a comma expression is like an expression
4759 statement: with -Wunused, we should warn if it doesn't have
4760 any side-effects, unless it was explicitly cast to (void). */
4761 if (warn_unused_value
)
4763 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4764 && CONVERT_EXPR_P (expr1
))
4766 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4767 && TREE_CODE (expr1
) == COMPOUND_EXPR
4768 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4769 ; /* (void) a, (void) b, c */
4771 warning_at (loc
, OPT_Wunused_value
,
4772 "left-hand operand of comma expression has no effect");
4776 /* With -Wunused, we should also warn if the left-hand operand does have
4777 side-effects, but computes a value which is not used. For example, in
4778 `foo() + bar(), baz()' the result of the `+' operator is not used,
4779 so we should issue a warning. */
4780 else if (warn_unused_value
)
4781 warn_if_unused_value (expr1
, loc
);
4783 if (expr2
== error_mark_node
)
4784 return error_mark_node
;
4786 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4789 && expr1_int_operands
4790 && expr2_int_operands
)
4791 ret
= note_integer_operands (ret
);
4794 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4796 protected_set_expr_location (ret
, loc
);
4800 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4801 which we are casting. OTYPE is the type of the expression being
4802 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4803 of the cast. -Wcast-qual appeared on the command line. Named
4804 address space qualifiers are not handled here, because they result
4805 in different warnings. */
4808 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
4810 tree in_type
= type
;
4811 tree in_otype
= otype
;
4816 /* Check that the qualifiers on IN_TYPE are a superset of the
4817 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4818 nodes is uninteresting and we stop as soon as we hit a
4819 non-POINTER_TYPE node on either type. */
4822 in_otype
= TREE_TYPE (in_otype
);
4823 in_type
= TREE_TYPE (in_type
);
4825 /* GNU C allows cv-qualified function types. 'const' means the
4826 function is very pure, 'volatile' means it can't return. We
4827 need to warn when such qualifiers are added, not when they're
4829 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4830 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4831 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
4832 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
4834 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
4835 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
4837 while (TREE_CODE (in_type
) == POINTER_TYPE
4838 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4841 warning_at (loc
, OPT_Wcast_qual
,
4842 "cast adds %q#v qualifier to function type", added
);
4845 /* There are qualifiers present in IN_OTYPE that are not present
4847 warning_at (loc
, OPT_Wcast_qual
,
4848 "cast discards %q#v qualifier from pointer target type",
4851 if (added
|| discarded
)
4854 /* A cast from **T to const **T is unsafe, because it can cause a
4855 const value to be changed with no additional warning. We only
4856 issue this warning if T is the same on both sides, and we only
4857 issue the warning if there are the same number of pointers on
4858 both sides, as otherwise the cast is clearly unsafe anyhow. A
4859 cast is unsafe when a qualifier is added at one level and const
4860 is not present at all outer levels.
4862 To issue this warning, we check at each level whether the cast
4863 adds new qualifiers not already seen. We don't need to special
4864 case function types, as they won't have the same
4865 TYPE_MAIN_VARIANT. */
4867 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4869 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4874 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4877 in_type
= TREE_TYPE (in_type
);
4878 in_otype
= TREE_TYPE (in_otype
);
4879 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4882 warning_at (loc
, OPT_Wcast_qual
,
4883 "to be safe all intermediate pointers in cast from "
4884 "%qT to %qT must be %<const%> qualified",
4889 is_const
= TYPE_READONLY (in_type
);
4891 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4894 /* Build an expression representing a cast to type TYPE of expression EXPR.
4895 LOC is the location of the cast-- typically the open paren of the cast. */
4898 build_c_cast (location_t loc
, tree type
, tree expr
)
4902 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
4903 expr
= TREE_OPERAND (expr
, 0);
4907 if (type
== error_mark_node
|| expr
== error_mark_node
)
4908 return error_mark_node
;
4910 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4911 only in <protocol> qualifications. But when constructing cast expressions,
4912 the protocols do matter and must be kept around. */
4913 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
4914 return build1 (NOP_EXPR
, type
, expr
);
4916 type
= TYPE_MAIN_VARIANT (type
);
4918 if (TREE_CODE (type
) == ARRAY_TYPE
)
4920 error_at (loc
, "cast specifies array type");
4921 return error_mark_node
;
4924 if (TREE_CODE (type
) == FUNCTION_TYPE
)
4926 error_at (loc
, "cast specifies function type");
4927 return error_mark_node
;
4930 if (!VOID_TYPE_P (type
))
4932 value
= require_complete_type (value
);
4933 if (value
== error_mark_node
)
4934 return error_mark_node
;
4937 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
4939 if (TREE_CODE (type
) == RECORD_TYPE
4940 || TREE_CODE (type
) == UNION_TYPE
)
4941 pedwarn (loc
, OPT_Wpedantic
,
4942 "ISO C forbids casting nonscalar to the same type");
4944 else if (TREE_CODE (type
) == UNION_TYPE
)
4948 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4949 if (TREE_TYPE (field
) != error_mark_node
4950 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
4951 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
4957 bool maybe_const
= true;
4959 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
4960 t
= c_fully_fold (value
, false, &maybe_const
);
4961 t
= build_constructor_single (type
, field
, t
);
4963 t
= c_wrap_maybe_const (t
, true);
4964 t
= digest_init (loc
, type
, t
,
4965 NULL_TREE
, false, true, 0);
4966 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
4969 error_at (loc
, "cast to union type from type not present in union");
4970 return error_mark_node
;
4976 if (type
== void_type_node
)
4978 tree t
= build1 (CONVERT_EXPR
, type
, value
);
4979 SET_EXPR_LOCATION (t
, loc
);
4983 otype
= TREE_TYPE (value
);
4985 /* Optionally warn about potentially worrisome casts. */
4987 && TREE_CODE (type
) == POINTER_TYPE
4988 && TREE_CODE (otype
) == POINTER_TYPE
)
4989 handle_warn_cast_qual (loc
, type
, otype
);
4991 /* Warn about conversions between pointers to disjoint
4993 if (TREE_CODE (type
) == POINTER_TYPE
4994 && TREE_CODE (otype
) == POINTER_TYPE
4995 && !null_pointer_constant_p (value
))
4997 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
4998 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
4999 addr_space_t as_common
;
5001 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5003 if (ADDR_SPACE_GENERIC_P (as_from
))
5004 warning_at (loc
, 0, "cast to %s address space pointer "
5005 "from disjoint generic address space pointer",
5006 c_addr_space_name (as_to
));
5008 else if (ADDR_SPACE_GENERIC_P (as_to
))
5009 warning_at (loc
, 0, "cast to generic address space pointer "
5010 "from disjoint %s address space pointer",
5011 c_addr_space_name (as_from
));
5014 warning_at (loc
, 0, "cast to %s address space pointer "
5015 "from disjoint %s address space pointer",
5016 c_addr_space_name (as_to
),
5017 c_addr_space_name (as_from
));
5021 /* Warn about possible alignment problems. */
5022 if (STRICT_ALIGNMENT
5023 && TREE_CODE (type
) == POINTER_TYPE
5024 && TREE_CODE (otype
) == POINTER_TYPE
5025 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5026 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5027 /* Don't warn about opaque types, where the actual alignment
5028 restriction is unknown. */
5029 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
5030 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
5031 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5032 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5033 warning_at (loc
, OPT_Wcast_align
,
5034 "cast increases required alignment of target type");
5036 if (TREE_CODE (type
) == INTEGER_TYPE
5037 && TREE_CODE (otype
) == POINTER_TYPE
5038 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5039 /* Unlike conversion of integers to pointers, where the
5040 warning is disabled for converting constants because
5041 of cases such as SIG_*, warn about converting constant
5042 pointers to integers. In some cases it may cause unwanted
5043 sign extension, and a warning is appropriate. */
5044 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5045 "cast from pointer to integer of different size");
5047 if (TREE_CODE (value
) == CALL_EXPR
5048 && TREE_CODE (type
) != TREE_CODE (otype
))
5049 warning_at (loc
, OPT_Wbad_function_cast
,
5050 "cast from function call of type %qT "
5051 "to non-matching type %qT", otype
, type
);
5053 if (TREE_CODE (type
) == POINTER_TYPE
5054 && TREE_CODE (otype
) == INTEGER_TYPE
5055 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5056 /* Don't warn about converting any constant. */
5057 && !TREE_CONSTANT (value
))
5059 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5060 "of different size");
5062 if (warn_strict_aliasing
<= 2)
5063 strict_aliasing_warning (otype
, type
, expr
);
5065 /* If pedantic, warn for conversions between function and object
5066 pointer types, except for converting a null pointer constant
5067 to function pointer type. */
5069 && TREE_CODE (type
) == POINTER_TYPE
5070 && TREE_CODE (otype
) == POINTER_TYPE
5071 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5072 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5073 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5074 "conversion of function pointer to object pointer type");
5077 && TREE_CODE (type
) == POINTER_TYPE
5078 && TREE_CODE (otype
) == POINTER_TYPE
5079 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5080 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5081 && !null_pointer_constant_p (value
))
5082 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5083 "conversion of object pointer to function pointer type");
5086 value
= convert (type
, value
);
5088 /* Ignore any integer overflow caused by the cast. */
5089 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5091 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5093 if (!TREE_OVERFLOW (value
))
5095 /* Avoid clobbering a shared constant. */
5096 value
= copy_node (value
);
5097 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5100 else if (TREE_OVERFLOW (value
))
5101 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5102 value
= wide_int_to_tree (TREE_TYPE (value
), value
);
5106 /* Don't let a cast be an lvalue. */
5108 value
= non_lvalue_loc (loc
, value
);
5110 /* Don't allow the results of casting to floating-point or complex
5111 types be confused with actual constants, or casts involving
5112 integer and pointer types other than direct integer-to-integer
5113 and integer-to-pointer be confused with integer constant
5114 expressions and null pointer constants. */
5115 if (TREE_CODE (value
) == REAL_CST
5116 || TREE_CODE (value
) == COMPLEX_CST
5117 || (TREE_CODE (value
) == INTEGER_CST
5118 && !((TREE_CODE (expr
) == INTEGER_CST
5119 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5120 || TREE_CODE (expr
) == REAL_CST
5121 || TREE_CODE (expr
) == COMPLEX_CST
)))
5122 value
= build1 (NOP_EXPR
, type
, value
);
5124 if (CAN_HAVE_LOCATION_P (value
))
5125 SET_EXPR_LOCATION (value
, loc
);
5129 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5130 location of the open paren of the cast, or the position of the cast
5133 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5136 tree type_expr
= NULL_TREE
;
5137 bool type_expr_const
= true;
5139 int saved_wsp
= warn_strict_prototypes
;
5141 /* This avoids warnings about unprototyped casts on
5142 integers. E.g. "#define SIG_DFL (void(*)())0". */
5143 if (TREE_CODE (expr
) == INTEGER_CST
)
5144 warn_strict_prototypes
= 0;
5145 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5146 warn_strict_prototypes
= saved_wsp
;
5148 ret
= build_c_cast (loc
, type
, expr
);
5151 bool inner_expr_const
= true;
5152 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5153 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5154 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5155 && inner_expr_const
);
5156 SET_EXPR_LOCATION (ret
, loc
);
5159 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
5160 SET_EXPR_LOCATION (ret
, loc
);
5162 /* C++ does not permits types to be defined in a cast, but it
5163 allows references to incomplete types. */
5164 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5165 warning_at (loc
, OPT_Wc___compat
,
5166 "defining a type in a cast is invalid in C++");
5171 /* Build an assignment expression of lvalue LHS from value RHS.
5172 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5173 may differ from TREE_TYPE (LHS) for an enum bitfield.
5174 MODIFYCODE is the code for a binary operator that we use
5175 to combine the old value of LHS with RHS to get the new value.
5176 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5177 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5178 which may differ from TREE_TYPE (RHS) for an enum value.
5180 LOCATION is the location of the MODIFYCODE operator.
5181 RHS_LOC is the location of the RHS. */
5184 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5185 enum tree_code modifycode
,
5186 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5190 tree rhs_semantic_type
= NULL_TREE
;
5191 tree lhstype
= TREE_TYPE (lhs
);
5192 tree olhstype
= lhstype
;
5196 /* Types that aren't fully specified cannot be used in assignments. */
5197 lhs
= require_complete_type (lhs
);
5199 /* Avoid duplicate error messages from operands that had errors. */
5200 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5201 return error_mark_node
;
5203 /* For ObjC properties, defer this check. */
5204 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5205 return error_mark_node
;
5207 is_atomic_op
= really_atomic_lvalue (lhs
);
5209 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5211 rhs_semantic_type
= TREE_TYPE (rhs
);
5212 rhs
= TREE_OPERAND (rhs
, 0);
5217 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5219 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5220 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5222 if (inner
== error_mark_node
)
5223 return error_mark_node
;
5224 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5225 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5226 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5227 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5228 protected_set_expr_location (result
, location
);
5232 /* If a binary op has been requested, combine the old LHS value with the RHS
5233 producing the value we should actually store into the LHS. */
5235 if (modifycode
!= NOP_EXPR
)
5237 lhs
= c_fully_fold (lhs
, false, NULL
);
5238 lhs
= stabilize_reference (lhs
);
5240 /* Construct the RHS for any non-atomic compound assignemnt. */
5243 newrhs
= build_binary_op (location
,
5244 modifycode
, lhs
, rhs
, 1);
5246 /* The original type of the right hand side is no longer
5248 rhs_origtype
= NULL_TREE
;
5252 if (c_dialect_objc ())
5254 /* Check if we are modifying an Objective-C property reference;
5255 if so, we need to generate setter calls. */
5256 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5260 /* Else, do the check that we postponed for Objective-C. */
5261 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5262 return error_mark_node
;
5265 /* Give an error for storing in something that is 'const'. */
5267 if (TYPE_READONLY (lhstype
)
5268 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5269 || TREE_CODE (lhstype
) == UNION_TYPE
)
5270 && C_TYPE_FIELDS_READONLY (lhstype
)))
5272 readonly_error (location
, lhs
, lv_assign
);
5273 return error_mark_node
;
5275 else if (TREE_READONLY (lhs
))
5276 readonly_warning (lhs
, lv_assign
);
5278 /* If storing into a structure or union member,
5279 it has probably been given type `int'.
5280 Compute the type that would go with
5281 the actual amount of storage the member occupies. */
5283 if (TREE_CODE (lhs
) == COMPONENT_REF
5284 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5285 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5286 || TREE_CODE (lhstype
) == REAL_TYPE
5287 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5288 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5290 /* If storing in a field that is in actuality a short or narrower than one,
5291 we must store in the field in its actual type. */
5293 if (lhstype
!= TREE_TYPE (lhs
))
5295 lhs
= copy_node (lhs
);
5296 TREE_TYPE (lhs
) = lhstype
;
5299 /* Issue -Wc++-compat warnings about an assignment to an enum type
5300 when LHS does not have its original type. This happens for,
5301 e.g., an enum bitfield in a struct. */
5303 && lhs_origtype
!= NULL_TREE
5304 && lhs_origtype
!= lhstype
5305 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5307 tree checktype
= (rhs_origtype
!= NULL_TREE
5310 if (checktype
!= error_mark_node
5311 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5312 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5313 warning_at (location
, OPT_Wc___compat
,
5314 "enum conversion in assignment is invalid in C++");
5317 /* If the lhs is atomic, remove that qualifier. */
5320 lhstype
= build_qualified_type (lhstype
,
5321 (TYPE_QUALS (lhstype
)
5322 & ~TYPE_QUAL_ATOMIC
));
5323 olhstype
= build_qualified_type (olhstype
,
5324 (TYPE_QUALS (lhstype
)
5325 & ~TYPE_QUAL_ATOMIC
));
5328 /* Convert new value to destination type. Fold it first, then
5329 restore any excess precision information, for the sake of
5330 conversion warnings. */
5332 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5334 npc
= null_pointer_constant_p (newrhs
);
5335 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5336 if (rhs_semantic_type
)
5337 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5338 newrhs
= convert_for_assignment (location
, lhstype
, newrhs
, rhs_origtype
,
5339 ic_assign
, npc
, NULL_TREE
,
5341 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5342 return error_mark_node
;
5345 /* Emit ObjC write barrier, if necessary. */
5346 if (c_dialect_objc () && flag_objc_gc
)
5348 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5351 protected_set_expr_location (result
, location
);
5356 /* Scan operands. */
5359 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5362 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5363 TREE_SIDE_EFFECTS (result
) = 1;
5364 protected_set_expr_location (result
, location
);
5367 /* If we got the LHS in a different type for storing in,
5368 convert the result back to the nominal type of LHS
5369 so that the value we return always has the same type
5370 as the LHS argument. */
5372 if (olhstype
== TREE_TYPE (result
))
5375 result
= convert_for_assignment (location
, olhstype
, result
, rhs_origtype
,
5376 ic_assign
, false, NULL_TREE
, NULL_TREE
, 0);
5377 protected_set_expr_location (result
, location
);
5381 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5382 This is used to implement -fplan9-extensions. */
5385 find_anonymous_field_with_type (tree struct_type
, tree type
)
5390 gcc_assert (TREE_CODE (struct_type
) == RECORD_TYPE
5391 || TREE_CODE (struct_type
) == UNION_TYPE
);
5393 for (field
= TYPE_FIELDS (struct_type
);
5395 field
= TREE_CHAIN (field
))
5397 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5398 ? c_build_qualified_type (TREE_TYPE (field
),
5400 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5401 if (DECL_NAME (field
) == NULL
5402 && comptypes (type
, fieldtype
))
5408 else if (DECL_NAME (field
) == NULL
5409 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
5410 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5411 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5421 /* RHS is an expression whose type is pointer to struct. If there is
5422 an anonymous field in RHS with type TYPE, then return a pointer to
5423 that field in RHS. This is used with -fplan9-extensions. This
5424 returns NULL if no conversion could be found. */
5427 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5429 tree rhs_struct_type
, lhs_main_type
;
5430 tree field
, found_field
;
5431 bool found_sub_field
;
5434 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5435 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5436 gcc_assert (TREE_CODE (rhs_struct_type
) == RECORD_TYPE
5437 || TREE_CODE (rhs_struct_type
) == UNION_TYPE
);
5439 gcc_assert (POINTER_TYPE_P (type
));
5440 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5441 ? c_build_qualified_type (TREE_TYPE (type
),
5443 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5445 found_field
= NULL_TREE
;
5446 found_sub_field
= false;
5447 for (field
= TYPE_FIELDS (rhs_struct_type
);
5449 field
= TREE_CHAIN (field
))
5451 if (DECL_NAME (field
) != NULL_TREE
5452 || (TREE_CODE (TREE_TYPE (field
)) != RECORD_TYPE
5453 && TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
))
5455 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5456 ? c_build_qualified_type (TREE_TYPE (field
),
5458 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5459 if (comptypes (lhs_main_type
, fieldtype
))
5461 if (found_field
!= NULL_TREE
)
5463 found_field
= field
;
5465 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5468 if (found_field
!= NULL_TREE
)
5470 found_field
= field
;
5471 found_sub_field
= true;
5475 if (found_field
== NULL_TREE
)
5478 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5479 build_fold_indirect_ref (rhs
), found_field
,
5481 ret
= build_fold_addr_expr_loc (location
, ret
);
5483 if (found_sub_field
)
5485 ret
= convert_to_anonymous_field (location
, type
, ret
);
5486 gcc_assert (ret
!= NULL_TREE
);
5492 /* Convert value RHS to type TYPE as preparation for an assignment to
5493 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5494 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5495 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5496 constant before any folding.
5497 The real work of conversion is done by `convert'.
5498 The purpose of this function is to generate error messages
5499 for assignments that are not allowed in C.
5500 ERRTYPE says whether it is argument passing, assignment,
5501 initialization or return.
5503 LOCATION is the location of the RHS.
5504 FUNCTION is a tree for the function being called.
5505 PARMNUM is the number of the argument, for printing in error messages. */
5508 convert_for_assignment (location_t location
, tree type
, tree rhs
,
5509 tree origtype
, enum impl_conv errtype
,
5510 bool null_pointer_constant
, tree fundecl
,
5511 tree function
, int parmnum
)
5513 enum tree_code codel
= TREE_CODE (type
);
5514 tree orig_rhs
= rhs
;
5516 enum tree_code coder
;
5517 tree rname
= NULL_TREE
;
5518 bool objc_ok
= false;
5520 if (errtype
== ic_argpass
)
5523 /* Change pointer to function to the function itself for
5525 if (TREE_CODE (function
) == ADDR_EXPR
5526 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
5527 function
= TREE_OPERAND (function
, 0);
5529 /* Handle an ObjC selector specially for diagnostics. */
5530 selector
= objc_message_selector ();
5532 if (selector
&& parmnum
> 2)
5539 /* This macro is used to emit diagnostics to ensure that all format
5540 strings are complete sentences, visible to gettext and checked at
5542 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5547 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5548 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5549 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5550 "expected %qT but argument is of type %qT", \
5554 pedwarn (LOCATION, OPT, AS); \
5557 pedwarn_init (LOCATION, OPT, IN); \
5560 pedwarn (LOCATION, OPT, RE); \
5563 gcc_unreachable (); \
5567 /* This macro is used to emit diagnostics to ensure that all format
5568 strings are complete sentences, visible to gettext and checked at
5569 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5570 extra parameter to enumerate qualifiers. */
5572 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5577 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5578 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5579 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5580 "expected %qT but argument is of type %qT", \
5584 pedwarn (LOCATION, OPT, AS, QUALS); \
5587 pedwarn (LOCATION, OPT, IN, QUALS); \
5590 pedwarn (LOCATION, OPT, RE, QUALS); \
5593 gcc_unreachable (); \
5597 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5598 rhs
= TREE_OPERAND (rhs
, 0);
5600 rhstype
= TREE_TYPE (rhs
);
5601 coder
= TREE_CODE (rhstype
);
5603 if (coder
== ERROR_MARK
)
5604 return error_mark_node
;
5606 if (c_dialect_objc ())
5629 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
5632 if (warn_cxx_compat
)
5634 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
5635 if (checktype
!= error_mark_node
5636 && TREE_CODE (type
) == ENUMERAL_TYPE
5637 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
5639 WARN_FOR_ASSIGNMENT (input_location
, OPT_Wc___compat
,
5640 G_("enum conversion when passing argument "
5641 "%d of %qE is invalid in C++"),
5642 G_("enum conversion in assignment is "
5644 G_("enum conversion in initialization is "
5646 G_("enum conversion in return is "
5651 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
5654 if (coder
== VOID_TYPE
)
5656 /* Except for passing an argument to an unprototyped function,
5657 this is a constraint violation. When passing an argument to
5658 an unprototyped function, it is compile-time undefined;
5659 making it a constraint in that case was rejected in
5661 error_at (location
, "void value not ignored as it ought to be");
5662 return error_mark_node
;
5664 rhs
= require_complete_type (rhs
);
5665 if (rhs
== error_mark_node
)
5666 return error_mark_node
;
5667 /* A non-reference type can convert to a reference. This handles
5668 va_start, va_copy and possibly port built-ins. */
5669 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
5671 if (!lvalue_p (rhs
))
5673 error_at (location
, "cannot pass rvalue to reference parameter");
5674 return error_mark_node
;
5676 if (!c_mark_addressable (rhs
))
5677 return error_mark_node
;
5678 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
5679 SET_EXPR_LOCATION (rhs
, location
);
5681 rhs
= convert_for_assignment (location
, build_pointer_type (TREE_TYPE (type
)),
5682 rhs
, origtype
, errtype
, null_pointer_constant
,
5683 fundecl
, function
, parmnum
);
5684 if (rhs
== error_mark_node
)
5685 return error_mark_node
;
5687 rhs
= build1 (NOP_EXPR
, type
, rhs
);
5688 SET_EXPR_LOCATION (rhs
, location
);
5691 /* Some types can interconvert without explicit casts. */
5692 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
5693 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
5694 return convert (type
, rhs
);
5695 /* Arithmetic types all interconvert, and enum is treated like int. */
5696 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
5697 || codel
== FIXED_POINT_TYPE
5698 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
5699 || codel
== BOOLEAN_TYPE
)
5700 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
5701 || coder
== FIXED_POINT_TYPE
5702 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
5703 || coder
== BOOLEAN_TYPE
))
5706 bool save
= in_late_binary_op
;
5707 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5708 in_late_binary_op
= true;
5709 ret
= convert_and_check (type
, orig_rhs
);
5710 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
)
5711 in_late_binary_op
= save
;
5715 /* Aggregates in different TUs might need conversion. */
5716 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
5718 && comptypes (type
, rhstype
))
5719 return convert_and_check (type
, rhs
);
5721 /* Conversion to a transparent union or record from its member types.
5722 This applies only to function arguments. */
5723 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
5724 && TYPE_TRANSPARENT_AGGR (type
))
5725 && errtype
== ic_argpass
)
5727 tree memb
, marginal_memb
= NULL_TREE
;
5729 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
5731 tree memb_type
= TREE_TYPE (memb
);
5733 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
5734 TYPE_MAIN_VARIANT (rhstype
)))
5737 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
5740 if (coder
== POINTER_TYPE
)
5742 tree ttl
= TREE_TYPE (memb_type
);
5743 tree ttr
= TREE_TYPE (rhstype
);
5745 /* Any non-function converts to a [const][volatile] void *
5746 and vice versa; otherwise, targets must be the same.
5747 Meanwhile, the lhs target must have all the qualifiers of
5749 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5750 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5751 || comp_target_types (location
, memb_type
, rhstype
))
5753 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
5754 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
5755 /* If this type won't generate any warnings, use it. */
5756 if (lquals
== rquals
5757 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
5758 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5759 ? ((lquals
| rquals
) == rquals
)
5760 : ((lquals
| rquals
) == lquals
)))
5763 /* Keep looking for a better type, but remember this one. */
5765 marginal_memb
= memb
;
5769 /* Can convert integer zero to any pointer type. */
5770 if (null_pointer_constant
)
5772 rhs
= null_pointer_node
;
5777 if (memb
|| marginal_memb
)
5781 /* We have only a marginally acceptable member type;
5782 it needs a warning. */
5783 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
5784 tree ttr
= TREE_TYPE (rhstype
);
5786 /* Const and volatile mean something different for function
5787 types, so the usual warnings are not appropriate. */
5788 if (TREE_CODE (ttr
) == FUNCTION_TYPE
5789 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5791 /* Because const and volatile on functions are
5792 restrictions that say the function will not do
5793 certain things, it is okay to use a const or volatile
5794 function where an ordinary one is wanted, but not
5796 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
5797 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
5798 WARN_FOR_QUALIFIERS (location
, 0,
5799 G_("passing argument %d of %qE "
5800 "makes %q#v qualified function "
5801 "pointer from unqualified"),
5802 G_("assignment makes %q#v qualified "
5803 "function pointer from "
5805 G_("initialization makes %q#v qualified "
5806 "function pointer from "
5808 G_("return makes %q#v qualified function "
5809 "pointer from unqualified"),
5810 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
5812 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
5813 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
5814 WARN_FOR_QUALIFIERS (location
, 0,
5815 G_("passing argument %d of %qE discards "
5816 "%qv qualifier from pointer target type"),
5817 G_("assignment discards %qv qualifier "
5818 "from pointer target type"),
5819 G_("initialization discards %qv qualifier "
5820 "from pointer target type"),
5821 G_("return discards %qv qualifier from "
5822 "pointer target type"),
5823 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
5825 memb
= marginal_memb
;
5828 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
5829 pedwarn (location
, OPT_Wpedantic
,
5830 "ISO C prohibits argument conversion to union type");
5832 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
5833 return build_constructor_single (type
, memb
, rhs
);
5837 /* Conversions among pointers */
5838 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
5839 && (coder
== codel
))
5841 tree ttl
= TREE_TYPE (type
);
5842 tree ttr
= TREE_TYPE (rhstype
);
5845 bool is_opaque_pointer
;
5846 int target_cmp
= 0; /* Cache comp_target_types () result. */
5850 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
5851 mvl
= (TYPE_ATOMIC (mvl
)
5852 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
5854 : TYPE_MAIN_VARIANT (mvl
));
5855 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
5856 mvr
= (TYPE_ATOMIC (mvr
)
5857 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
5859 : TYPE_MAIN_VARIANT (mvr
));
5860 /* Opaque pointers are treated like void pointers. */
5861 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
5863 /* The Plan 9 compiler permits a pointer to a struct to be
5864 automatically converted into a pointer to an anonymous field
5865 within the struct. */
5866 if (flag_plan9_extensions
5867 && (TREE_CODE (mvl
) == RECORD_TYPE
|| TREE_CODE(mvl
) == UNION_TYPE
)
5868 && (TREE_CODE (mvr
) == RECORD_TYPE
|| TREE_CODE(mvr
) == UNION_TYPE
)
5871 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
5872 if (new_rhs
!= NULL_TREE
)
5875 rhstype
= TREE_TYPE (rhs
);
5876 coder
= TREE_CODE (rhstype
);
5877 ttr
= TREE_TYPE (rhstype
);
5878 mvr
= TYPE_MAIN_VARIANT (ttr
);
5882 /* C++ does not allow the implicit conversion void* -> T*. However,
5883 for the purpose of reducing the number of false positives, we
5884 tolerate the special case of
5888 where NULL is typically defined in C to be '(void *) 0'. */
5889 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
5890 warning_at (location
, OPT_Wc___compat
,
5891 "request for implicit conversion "
5892 "from %qT to %qT not permitted in C++", rhstype
, type
);
5894 /* See if the pointers point to incompatible address spaces. */
5895 asl
= TYPE_ADDR_SPACE (ttl
);
5896 asr
= TYPE_ADDR_SPACE (ttr
);
5897 if (!null_pointer_constant_p (rhs
)
5898 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
5903 error_at (location
, "passing argument %d of %qE from pointer to "
5904 "non-enclosed address space", parmnum
, rname
);
5907 error_at (location
, "assignment from pointer to "
5908 "non-enclosed address space");
5911 error_at (location
, "initialization from pointer to "
5912 "non-enclosed address space");
5915 error_at (location
, "return from pointer to "
5916 "non-enclosed address space");
5921 return error_mark_node
;
5924 /* Check if the right-hand side has a format attribute but the
5925 left-hand side doesn't. */
5926 if (warn_suggest_attribute_format
5927 && check_missing_format_attribute (type
, rhstype
))
5932 warning_at (location
, OPT_Wsuggest_attribute_format
,
5933 "argument %d of %qE might be "
5934 "a candidate for a format attribute",
5938 warning_at (location
, OPT_Wsuggest_attribute_format
,
5939 "assignment left-hand side might be "
5940 "a candidate for a format attribute");
5943 warning_at (location
, OPT_Wsuggest_attribute_format
,
5944 "initialization left-hand side might be "
5945 "a candidate for a format attribute");
5948 warning_at (location
, OPT_Wsuggest_attribute_format
,
5949 "return type might be "
5950 "a candidate for a format attribute");
5957 /* Any non-function converts to a [const][volatile] void *
5958 and vice versa; otherwise, targets must be the same.
5959 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5960 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5961 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5962 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
5963 || is_opaque_pointer
5964 || ((c_common_unsigned_type (mvl
)
5965 == c_common_unsigned_type (mvr
))
5966 && (c_common_signed_type (mvl
)
5967 == c_common_signed_type (mvr
))
5968 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
5971 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
5974 && !null_pointer_constant
5975 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
5976 WARN_FOR_ASSIGNMENT (location
, OPT_Wpedantic
,
5977 G_("ISO C forbids passing argument %d of "
5978 "%qE between function pointer "
5980 G_("ISO C forbids assignment between "
5981 "function pointer and %<void *%>"),
5982 G_("ISO C forbids initialization between "
5983 "function pointer and %<void *%>"),
5984 G_("ISO C forbids return between function "
5985 "pointer and %<void *%>"));
5986 /* Const and volatile mean something different for function types,
5987 so the usual warnings are not appropriate. */
5988 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
5989 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
5991 /* Assignments between atomic and non-atomic objects are OK. */
5992 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
5993 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
5995 WARN_FOR_QUALIFIERS (location
, 0,
5996 G_("passing argument %d of %qE discards "
5997 "%qv qualifier from pointer target type"),
5998 G_("assignment discards %qv qualifier "
5999 "from pointer target type"),
6000 G_("initialization discards %qv qualifier "
6001 "from pointer target type"),
6002 G_("return discards %qv qualifier from "
6003 "pointer target type"),
6004 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6006 /* If this is not a case of ignoring a mismatch in signedness,
6008 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6011 /* If there is a mismatch, do warn. */
6012 else if (warn_pointer_sign
)
6013 WARN_FOR_ASSIGNMENT (location
, OPT_Wpointer_sign
,
6014 G_("pointer targets in passing argument "
6015 "%d of %qE differ in signedness"),
6016 G_("pointer targets in assignment "
6017 "differ in signedness"),
6018 G_("pointer targets in initialization "
6019 "differ in signedness"),
6020 G_("pointer targets in return differ "
6023 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6024 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6026 /* Because const and volatile on functions are restrictions
6027 that say the function will not do certain things,
6028 it is okay to use a const or volatile function
6029 where an ordinary one is wanted, but not vice-versa. */
6030 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6031 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6032 WARN_FOR_QUALIFIERS (location
, 0,
6033 G_("passing argument %d of %qE makes "
6034 "%q#v qualified function pointer "
6035 "from unqualified"),
6036 G_("assignment makes %q#v qualified function "
6037 "pointer from unqualified"),
6038 G_("initialization makes %q#v qualified "
6039 "function pointer from unqualified"),
6040 G_("return makes %q#v qualified function "
6041 "pointer from unqualified"),
6042 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6046 /* Avoid warning about the volatile ObjC EH puts on decls. */
6048 WARN_FOR_ASSIGNMENT (location
, 0,
6049 G_("passing argument %d of %qE from "
6050 "incompatible pointer type"),
6051 G_("assignment from incompatible pointer type"),
6052 G_("initialization from incompatible "
6054 G_("return from incompatible pointer type"));
6056 return convert (type
, rhs
);
6058 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6060 /* ??? This should not be an error when inlining calls to
6061 unprototyped functions. */
6062 error_at (location
, "invalid use of non-lvalue array");
6063 return error_mark_node
;
6065 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6067 /* An explicit constant 0 can convert to a pointer,
6068 or one that results from arithmetic, even including
6069 a cast to integer type. */
6070 if (!null_pointer_constant
)
6071 WARN_FOR_ASSIGNMENT (location
, 0,
6072 G_("passing argument %d of %qE makes "
6073 "pointer from integer without a cast"),
6074 G_("assignment makes pointer from integer "
6076 G_("initialization makes pointer from "
6077 "integer without a cast"),
6078 G_("return makes pointer from integer "
6081 return convert (type
, rhs
);
6083 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6085 WARN_FOR_ASSIGNMENT (location
, 0,
6086 G_("passing argument %d of %qE makes integer "
6087 "from pointer without a cast"),
6088 G_("assignment makes integer from pointer "
6090 G_("initialization makes integer from pointer "
6092 G_("return makes integer from pointer "
6094 return convert (type
, rhs
);
6096 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6099 bool save
= in_late_binary_op
;
6100 in_late_binary_op
= true;
6101 ret
= convert (type
, rhs
);
6102 in_late_binary_op
= save
;
6109 error_at (location
, "incompatible type for argument %d of %qE", parmnum
, rname
);
6110 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6111 ? DECL_SOURCE_LOCATION (fundecl
) : input_location
,
6112 "expected %qT but argument is of type %qT", type
, rhstype
);
6115 error_at (location
, "incompatible types when assigning to type %qT from "
6116 "type %qT", type
, rhstype
);
6120 "incompatible types when initializing type %qT using type %qT",
6125 "incompatible types when returning type %qT but %qT was "
6126 "expected", rhstype
, type
);
6132 return error_mark_node
;
6135 /* If VALUE is a compound expr all of whose expressions are constant, then
6136 return its value. Otherwise, return error_mark_node.
6138 This is for handling COMPOUND_EXPRs as initializer elements
6139 which is allowed with a warning when -pedantic is specified. */
6142 valid_compound_expr_initializer (tree value
, tree endtype
)
6144 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6146 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6148 return error_mark_node
;
6149 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6152 else if (!initializer_constant_valid_p (value
, endtype
))
6153 return error_mark_node
;
6158 /* Perform appropriate conversions on the initial value of a variable,
6159 store it in the declaration DECL,
6160 and print any error messages that are appropriate.
6161 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6162 If the init is invalid, store an ERROR_MARK.
6164 INIT_LOC is the location of the initial value. */
6167 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6172 /* If variable's type was invalidly declared, just ignore it. */
6174 type
= TREE_TYPE (decl
);
6175 if (TREE_CODE (type
) == ERROR_MARK
)
6178 /* Digest the specified initializer into an expression. */
6181 npc
= null_pointer_constant_p (init
);
6182 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6183 true, TREE_STATIC (decl
));
6185 /* Store the expression if valid; else report error. */
6187 if (!in_system_header
6188 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6189 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6190 "aggregate initialization");
6192 DECL_INITIAL (decl
) = value
;
6194 /* ANSI wants warnings about out-of-range constant initializers. */
6195 STRIP_TYPE_NOPS (value
);
6196 if (TREE_STATIC (decl
))
6197 constant_expression_warning (value
);
6199 /* Check if we need to set array size from compound literal size. */
6200 if (TREE_CODE (type
) == ARRAY_TYPE
6201 && TYPE_DOMAIN (type
) == 0
6202 && value
!= error_mark_node
)
6204 tree inside_init
= init
;
6206 STRIP_TYPE_NOPS (inside_init
);
6207 inside_init
= fold (inside_init
);
6209 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6211 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6213 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6215 /* For int foo[] = (int [3]){1}; we need to set array size
6216 now since later on array initializer will be just the
6217 brace enclosed list of the compound literal. */
6218 tree etype
= strip_array_types (TREE_TYPE (decl
));
6219 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6220 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6222 layout_decl (cldecl
, 0);
6224 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6230 /* Methods for storing and printing names for error messages. */
6232 /* Implement a spelling stack that allows components of a name to be pushed
6233 and popped. Each element on the stack is this structure. */
6240 unsigned HOST_WIDE_INT i
;
6245 #define SPELLING_STRING 1
6246 #define SPELLING_MEMBER 2
6247 #define SPELLING_BOUNDS 3
6249 static struct spelling
*spelling
; /* Next stack element (unused). */
6250 static struct spelling
*spelling_base
; /* Spelling stack base. */
6251 static int spelling_size
; /* Size of the spelling stack. */
6253 /* Macros to save and restore the spelling stack around push_... functions.
6254 Alternative to SAVE_SPELLING_STACK. */
6256 #define SPELLING_DEPTH() (spelling - spelling_base)
6257 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6259 /* Push an element on the spelling stack with type KIND and assign VALUE
6262 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6264 int depth = SPELLING_DEPTH (); \
6266 if (depth >= spelling_size) \
6268 spelling_size += 10; \
6269 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6271 RESTORE_SPELLING_DEPTH (depth); \
6274 spelling->kind = (KIND); \
6275 spelling->MEMBER = (VALUE); \
6279 /* Push STRING on the stack. Printed literally. */
6282 push_string (const char *string
)
6284 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6287 /* Push a member name on the stack. Printed as '.' STRING. */
6290 push_member_name (tree decl
)
6292 const char *const string
6294 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6295 : _("<anonymous>"));
6296 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6299 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6302 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6304 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6307 /* Compute the maximum size in bytes of the printed spelling. */
6310 spelling_length (void)
6315 for (p
= spelling_base
; p
< spelling
; p
++)
6317 if (p
->kind
== SPELLING_BOUNDS
)
6320 size
+= strlen (p
->u
.s
) + 1;
6326 /* Print the spelling to BUFFER and return it. */
6329 print_spelling (char *buffer
)
6334 for (p
= spelling_base
; p
< spelling
; p
++)
6335 if (p
->kind
== SPELLING_BOUNDS
)
6337 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6343 if (p
->kind
== SPELLING_MEMBER
)
6345 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
6352 /* Issue an error message for a bad initializer component.
6353 GMSGID identifies the message.
6354 The component name is taken from the spelling stack. */
6357 error_init (const char *gmsgid
)
6361 /* The gmsgid may be a format string with %< and %>. */
6363 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6365 error ("(near initialization for %qs)", ofwhat
);
6368 /* Issue a pedantic warning for a bad initializer component. OPT is
6369 the option OPT_* (from options.h) controlling this warning or 0 if
6370 it is unconditionally given. GMSGID identifies the message. The
6371 component name is taken from the spelling stack. */
6374 pedwarn_init (location_t location
, int opt
, const char *gmsgid
)
6378 /* The gmsgid may be a format string with %< and %>. */
6379 pedwarn (location
, opt
, gmsgid
);
6380 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6382 pedwarn (location
, opt
, "(near initialization for %qs)", ofwhat
);
6385 /* Issue a warning for a bad initializer component.
6387 OPT is the OPT_W* value corresponding to the warning option that
6388 controls this warning. GMSGID identifies the message. The
6389 component name is taken from the spelling stack. */
6392 warning_init (int opt
, const char *gmsgid
)
6396 /* The gmsgid may be a format string with %< and %>. */
6397 warning (opt
, gmsgid
);
6398 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
6400 warning (opt
, "(near initialization for %qs)", ofwhat
);
6403 /* If TYPE is an array type and EXPR is a parenthesized string
6404 constant, warn if pedantic that EXPR is being used to initialize an
6405 object of type TYPE. */
6408 maybe_warn_string_init (tree type
, struct c_expr expr
)
6411 && TREE_CODE (type
) == ARRAY_TYPE
6412 && TREE_CODE (expr
.value
) == STRING_CST
6413 && expr
.original_code
!= STRING_CST
)
6414 pedwarn_init (input_location
, OPT_Wpedantic
,
6415 "array initialized from parenthesized string constant");
6418 /* Digest the parser output INIT as an initializer for type TYPE.
6419 Return a C expression of type TYPE to represent the initial value.
6421 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6423 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6425 If INIT is a string constant, STRICT_STRING is true if it is
6426 unparenthesized or we should not warn here for it being parenthesized.
6427 For other types of INIT, STRICT_STRING is not used.
6429 INIT_LOC is the location of the INIT.
6431 REQUIRE_CONSTANT requests an error if non-constant initializers or
6432 elements are seen. */
6435 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
6436 bool null_pointer_constant
, bool strict_string
,
6437 int require_constant
)
6439 enum tree_code code
= TREE_CODE (type
);
6440 tree inside_init
= init
;
6441 tree semantic_type
= NULL_TREE
;
6442 bool maybe_const
= true;
6444 if (type
== error_mark_node
6446 || init
== error_mark_node
6447 || TREE_TYPE (init
) == error_mark_node
)
6448 return error_mark_node
;
6450 STRIP_TYPE_NOPS (inside_init
);
6452 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
6454 semantic_type
= TREE_TYPE (inside_init
);
6455 inside_init
= TREE_OPERAND (inside_init
, 0);
6457 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
6458 inside_init
= decl_constant_value_for_optimization (inside_init
);
6460 /* Initialization of an array of chars from a string constant
6461 optionally enclosed in braces. */
6463 if (code
== ARRAY_TYPE
&& inside_init
6464 && TREE_CODE (inside_init
) == STRING_CST
)
6467 = (TYPE_ATOMIC (TREE_TYPE (type
))
6468 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
6470 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6471 /* Note that an array could be both an array of character type
6472 and an array of wchar_t if wchar_t is signed char or unsigned
6474 bool char_array
= (typ1
== char_type_node
6475 || typ1
== signed_char_type_node
6476 || typ1
== unsigned_char_type_node
);
6477 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
6478 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
6479 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
6481 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
6484 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
6485 expr
.value
= inside_init
;
6486 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
6487 expr
.original_type
= NULL
;
6488 maybe_warn_string_init (type
, expr
);
6490 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
6491 pedwarn_init (init_loc
, OPT_Wpedantic
,
6492 "initialization of a flexible array member");
6494 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6495 TYPE_MAIN_VARIANT (type
)))
6500 if (typ2
!= char_type_node
)
6502 error_init ("char-array initialized from wide string");
6503 return error_mark_node
;
6508 if (typ2
== char_type_node
)
6510 error_init ("wide character array initialized from non-wide "
6512 return error_mark_node
;
6514 else if (!comptypes(typ1
, typ2
))
6516 error_init ("wide character array initialized from "
6517 "incompatible wide string");
6518 return error_mark_node
;
6522 TREE_TYPE (inside_init
) = type
;
6523 if (TYPE_DOMAIN (type
) != 0
6524 && TYPE_SIZE (type
) != 0
6525 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
6527 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
6529 /* Subtract the size of a single (possibly wide) character
6530 because it's ok to ignore the terminating null char
6531 that is counted in the length of the constant. */
6532 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
6534 - (TYPE_PRECISION (typ1
)
6536 pedwarn_init (init_loc
, 0,
6537 ("initializer-string for array of chars "
6539 else if (warn_cxx_compat
6540 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
6541 warning_at (init_loc
, OPT_Wc___compat
,
6542 ("initializer-string for array chars "
6543 "is too long for C++"));
6548 else if (INTEGRAL_TYPE_P (typ1
))
6550 error_init ("array of inappropriate type initialized "
6551 "from string constant");
6552 return error_mark_node
;
6556 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6557 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6558 below and handle as a constructor. */
6559 if (code
== VECTOR_TYPE
6560 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
6561 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
6562 && TREE_CONSTANT (inside_init
))
6564 if (TREE_CODE (inside_init
) == VECTOR_CST
6565 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6566 TYPE_MAIN_VARIANT (type
)))
6569 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
6571 unsigned HOST_WIDE_INT ix
;
6573 bool constant_p
= true;
6575 /* Iterate through elements and check if all constructor
6576 elements are *_CSTs. */
6577 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
6578 if (!CONSTANT_CLASS_P (value
))
6585 return build_vector_from_ctor (type
,
6586 CONSTRUCTOR_ELTS (inside_init
));
6590 if (warn_sequence_point
)
6591 verify_sequence_points (inside_init
);
6593 /* Any type can be initialized
6594 from an expression of the same type, optionally with braces. */
6596 if (inside_init
&& TREE_TYPE (inside_init
) != 0
6597 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6598 TYPE_MAIN_VARIANT (type
))
6599 || (code
== ARRAY_TYPE
6600 && comptypes (TREE_TYPE (inside_init
), type
))
6601 || (code
== VECTOR_TYPE
6602 && comptypes (TREE_TYPE (inside_init
), type
))
6603 || (code
== POINTER_TYPE
6604 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
6605 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
6606 TREE_TYPE (type
)))))
6608 if (code
== POINTER_TYPE
)
6610 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
6612 if (TREE_CODE (inside_init
) == STRING_CST
6613 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6614 inside_init
= array_to_pointer_conversion
6615 (init_loc
, inside_init
);
6618 error_init ("invalid use of non-lvalue array");
6619 return error_mark_node
;
6624 if (code
== VECTOR_TYPE
)
6625 /* Although the types are compatible, we may require a
6627 inside_init
= convert (type
, inside_init
);
6629 if (require_constant
6630 && (code
== VECTOR_TYPE
|| !flag_isoc99
)
6631 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6633 /* As an extension, allow initializing objects with static storage
6634 duration with compound literals (which are then treated just as
6635 the brace enclosed list they contain). Also allow this for
6636 vectors, as we can only assign them with compound literals. */
6637 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6638 inside_init
= DECL_INITIAL (decl
);
6641 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
6642 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
6644 error_init ("array initialized from non-constant array expression");
6645 return error_mark_node
;
6648 /* Compound expressions can only occur here if -Wpedantic or
6649 -pedantic-errors is specified. In the later case, we always want
6650 an error. In the former case, we simply want a warning. */
6651 if (require_constant
&& pedantic
6652 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
6655 = valid_compound_expr_initializer (inside_init
,
6656 TREE_TYPE (inside_init
));
6657 if (inside_init
== error_mark_node
)
6658 error_init ("initializer element is not constant");
6660 pedwarn_init (init_loc
, OPT_Wpedantic
,
6661 "initializer element is not constant");
6662 if (flag_pedantic_errors
)
6663 inside_init
= error_mark_node
;
6665 else if (require_constant
6666 && !initializer_constant_valid_p (inside_init
,
6667 TREE_TYPE (inside_init
)))
6669 error_init ("initializer element is not constant");
6670 inside_init
= error_mark_node
;
6672 else if (require_constant
&& !maybe_const
)
6673 pedwarn_init (init_loc
, 0,
6674 "initializer element is not a constant expression");
6676 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6677 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
6678 inside_init
= convert_for_assignment (init_loc
, type
, inside_init
,
6680 ic_init
, null_pointer_constant
,
6681 NULL_TREE
, NULL_TREE
, 0);
6685 /* Handle scalar types, including conversions. */
6687 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
6688 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
6689 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
6691 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
6692 && (TREE_CODE (init
) == STRING_CST
6693 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
6694 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
6696 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
6699 = convert_for_assignment (init_loc
, type
, inside_init
, origtype
,
6700 ic_init
, null_pointer_constant
,
6701 NULL_TREE
, NULL_TREE
, 0);
6703 /* Check to see if we have already given an error message. */
6704 if (inside_init
== error_mark_node
)
6706 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
6708 error_init ("initializer element is not constant");
6709 inside_init
= error_mark_node
;
6711 else if (require_constant
6712 && !initializer_constant_valid_p (inside_init
,
6713 TREE_TYPE (inside_init
)))
6715 error_init ("initializer element is not computable at load time");
6716 inside_init
= error_mark_node
;
6718 else if (require_constant
&& !maybe_const
)
6719 pedwarn_init (init_loc
, 0,
6720 "initializer element is not a constant expression");
6725 /* Come here only for records and arrays. */
6727 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
6729 error_init ("variable-sized object may not be initialized");
6730 return error_mark_node
;
6733 error_init ("invalid initializer");
6734 return error_mark_node
;
6737 /* Handle initializers that use braces. */
6739 /* Type of object we are accumulating a constructor for.
6740 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6741 static tree constructor_type
;
6743 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6745 static tree constructor_fields
;
6747 /* For an ARRAY_TYPE, this is the specified index
6748 at which to store the next element we get. */
6749 static tree constructor_index
;
6751 /* For an ARRAY_TYPE, this is the maximum index. */
6752 static tree constructor_max_index
;
6754 /* For a RECORD_TYPE, this is the first field not yet written out. */
6755 static tree constructor_unfilled_fields
;
6757 /* For an ARRAY_TYPE, this is the index of the first element
6758 not yet written out. */
6759 static tree constructor_unfilled_index
;
6761 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6762 This is so we can generate gaps between fields, when appropriate. */
6763 static tree constructor_bit_index
;
6765 /* If we are saving up the elements rather than allocating them,
6766 this is the list of elements so far (in reverse order,
6767 most recent first). */
6768 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
6770 /* 1 if constructor should be incrementally stored into a constructor chain,
6771 0 if all the elements should be kept in AVL tree. */
6772 static int constructor_incremental
;
6774 /* 1 if so far this constructor's elements are all compile-time constants. */
6775 static int constructor_constant
;
6777 /* 1 if so far this constructor's elements are all valid address constants. */
6778 static int constructor_simple
;
6780 /* 1 if this constructor has an element that cannot be part of a
6781 constant expression. */
6782 static int constructor_nonconst
;
6784 /* 1 if this constructor is erroneous so far. */
6785 static int constructor_erroneous
;
6787 /* Structure for managing pending initializer elements, organized as an
6792 struct init_node
*left
, *right
;
6793 struct init_node
*parent
;
6800 /* Tree of pending elements at this constructor level.
6801 These are elements encountered out of order
6802 which belong at places we haven't reached yet in actually
6804 Will never hold tree nodes across GC runs. */
6805 static struct init_node
*constructor_pending_elts
;
6807 /* The SPELLING_DEPTH of this constructor. */
6808 static int constructor_depth
;
6810 /* DECL node for which an initializer is being read.
6811 0 means we are reading a constructor expression
6812 such as (struct foo) {...}. */
6813 static tree constructor_decl
;
6815 /* Nonzero if this is an initializer for a top-level decl. */
6816 static int constructor_top_level
;
6818 /* Nonzero if there were any member designators in this initializer. */
6819 static int constructor_designated
;
6821 /* Nesting depth of designator list. */
6822 static int designator_depth
;
6824 /* Nonzero if there were diagnosed errors in this designator list. */
6825 static int designator_erroneous
;
6828 /* This stack has a level for each implicit or explicit level of
6829 structuring in the initializer, including the outermost one. It
6830 saves the values of most of the variables above. */
6832 struct constructor_range_stack
;
6834 struct constructor_stack
6836 struct constructor_stack
*next
;
6841 tree unfilled_index
;
6842 tree unfilled_fields
;
6844 vec
<constructor_elt
, va_gc
> *elements
;
6845 struct init_node
*pending_elts
;
6848 /* If value nonzero, this value should replace the entire
6849 constructor at this level. */
6850 struct c_expr replacement_value
;
6851 struct constructor_range_stack
*range_stack
;
6862 static struct constructor_stack
*constructor_stack
;
6864 /* This stack represents designators from some range designator up to
6865 the last designator in the list. */
6867 struct constructor_range_stack
6869 struct constructor_range_stack
*next
, *prev
;
6870 struct constructor_stack
*stack
;
6877 static struct constructor_range_stack
*constructor_range_stack
;
6879 /* This stack records separate initializers that are nested.
6880 Nested initializers can't happen in ANSI C, but GNU C allows them
6881 in cases like { ... (struct foo) { ... } ... }. */
6883 struct initializer_stack
6885 struct initializer_stack
*next
;
6887 struct constructor_stack
*constructor_stack
;
6888 struct constructor_range_stack
*constructor_range_stack
;
6889 vec
<constructor_elt
, va_gc
> *elements
;
6890 struct spelling
*spelling
;
6891 struct spelling
*spelling_base
;
6894 char require_constant_value
;
6895 char require_constant_elements
;
6898 static struct initializer_stack
*initializer_stack
;
6900 /* Prepare to parse and output the initializer for variable DECL. */
6903 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
6906 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
6908 p
->decl
= constructor_decl
;
6909 p
->require_constant_value
= require_constant_value
;
6910 p
->require_constant_elements
= require_constant_elements
;
6911 p
->constructor_stack
= constructor_stack
;
6912 p
->constructor_range_stack
= constructor_range_stack
;
6913 p
->elements
= constructor_elements
;
6914 p
->spelling
= spelling
;
6915 p
->spelling_base
= spelling_base
;
6916 p
->spelling_size
= spelling_size
;
6917 p
->top_level
= constructor_top_level
;
6918 p
->next
= initializer_stack
;
6919 initializer_stack
= p
;
6921 constructor_decl
= decl
;
6922 constructor_designated
= 0;
6923 constructor_top_level
= top_level
;
6925 if (decl
!= 0 && decl
!= error_mark_node
)
6927 require_constant_value
= TREE_STATIC (decl
);
6928 require_constant_elements
6929 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
6930 /* For a scalar, you can always use any value to initialize,
6931 even within braces. */
6932 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
6933 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
6934 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
6935 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
6936 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
6940 require_constant_value
= 0;
6941 require_constant_elements
= 0;
6942 locus
= _("(anonymous)");
6945 constructor_stack
= 0;
6946 constructor_range_stack
= 0;
6948 missing_braces_mentioned
= 0;
6952 RESTORE_SPELLING_DEPTH (0);
6955 push_string (locus
);
6961 struct initializer_stack
*p
= initializer_stack
;
6963 /* Free the whole constructor stack of this initializer. */
6964 while (constructor_stack
)
6966 struct constructor_stack
*q
= constructor_stack
;
6967 constructor_stack
= q
->next
;
6971 gcc_assert (!constructor_range_stack
);
6973 /* Pop back to the data of the outer initializer (if any). */
6974 free (spelling_base
);
6976 constructor_decl
= p
->decl
;
6977 require_constant_value
= p
->require_constant_value
;
6978 require_constant_elements
= p
->require_constant_elements
;
6979 constructor_stack
= p
->constructor_stack
;
6980 constructor_range_stack
= p
->constructor_range_stack
;
6981 constructor_elements
= p
->elements
;
6982 spelling
= p
->spelling
;
6983 spelling_base
= p
->spelling_base
;
6984 spelling_size
= p
->spelling_size
;
6985 constructor_top_level
= p
->top_level
;
6986 initializer_stack
= p
->next
;
6990 /* Call here when we see the initializer is surrounded by braces.
6991 This is instead of a call to push_init_level;
6992 it is matched by a call to pop_init_level.
6994 TYPE is the type to initialize, for a constructor expression.
6995 For an initializer for a decl, TYPE is zero. */
6998 really_start_incremental_init (tree type
)
7000 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7003 type
= TREE_TYPE (constructor_decl
);
7005 if (TREE_CODE (type
) == VECTOR_TYPE
7006 && TYPE_VECTOR_OPAQUE (type
))
7007 error ("opaque vector types cannot be initialized");
7009 p
->type
= constructor_type
;
7010 p
->fields
= constructor_fields
;
7011 p
->index
= constructor_index
;
7012 p
->max_index
= constructor_max_index
;
7013 p
->unfilled_index
= constructor_unfilled_index
;
7014 p
->unfilled_fields
= constructor_unfilled_fields
;
7015 p
->bit_index
= constructor_bit_index
;
7016 p
->elements
= constructor_elements
;
7017 p
->constant
= constructor_constant
;
7018 p
->simple
= constructor_simple
;
7019 p
->nonconst
= constructor_nonconst
;
7020 p
->erroneous
= constructor_erroneous
;
7021 p
->pending_elts
= constructor_pending_elts
;
7022 p
->depth
= constructor_depth
;
7023 p
->replacement_value
.value
= 0;
7024 p
->replacement_value
.original_code
= ERROR_MARK
;
7025 p
->replacement_value
.original_type
= NULL
;
7029 p
->incremental
= constructor_incremental
;
7030 p
->designated
= constructor_designated
;
7032 constructor_stack
= p
;
7034 constructor_constant
= 1;
7035 constructor_simple
= 1;
7036 constructor_nonconst
= 0;
7037 constructor_depth
= SPELLING_DEPTH ();
7038 constructor_elements
= NULL
;
7039 constructor_pending_elts
= 0;
7040 constructor_type
= type
;
7041 constructor_incremental
= 1;
7042 constructor_designated
= 0;
7043 designator_depth
= 0;
7044 designator_erroneous
= 0;
7046 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7047 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7049 constructor_fields
= TYPE_FIELDS (constructor_type
);
7050 /* Skip any nameless bit fields at the beginning. */
7051 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7052 && DECL_NAME (constructor_fields
) == 0)
7053 constructor_fields
= DECL_CHAIN (constructor_fields
);
7055 constructor_unfilled_fields
= constructor_fields
;
7056 constructor_bit_index
= bitsize_zero_node
;
7058 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7060 if (TYPE_DOMAIN (constructor_type
))
7062 constructor_max_index
7063 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7065 /* Detect non-empty initializations of zero-length arrays. */
7066 if (constructor_max_index
== NULL_TREE
7067 && TYPE_SIZE (constructor_type
))
7068 constructor_max_index
= integer_minus_one_node
;
7070 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7071 to initialize VLAs will cause a proper error; avoid tree
7072 checking errors as well by setting a safe value. */
7073 if (constructor_max_index
7074 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7075 constructor_max_index
= integer_minus_one_node
;
7078 = convert (bitsizetype
,
7079 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7083 constructor_index
= bitsize_zero_node
;
7084 constructor_max_index
= NULL_TREE
;
7087 constructor_unfilled_index
= constructor_index
;
7089 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7091 /* Vectors are like simple fixed-size arrays. */
7092 constructor_max_index
=
7093 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7094 constructor_index
= bitsize_zero_node
;
7095 constructor_unfilled_index
= constructor_index
;
7099 /* Handle the case of int x = {5}; */
7100 constructor_fields
= constructor_type
;
7101 constructor_unfilled_fields
= constructor_type
;
7105 /* Push down into a subobject, for initialization.
7106 If this is for an explicit set of braces, IMPLICIT is 0.
7107 If it is because the next element belongs at a lower level,
7108 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7111 push_init_level (int implicit
, struct obstack
* braced_init_obstack
)
7113 struct constructor_stack
*p
;
7114 tree value
= NULL_TREE
;
7116 /* If we've exhausted any levels that didn't have braces,
7117 pop them now. If implicit == 1, this will have been done in
7118 process_init_element; do not repeat it here because in the case
7119 of excess initializers for an empty aggregate this leads to an
7120 infinite cycle of popping a level and immediately recreating
7124 while (constructor_stack
->implicit
)
7126 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7127 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7128 && constructor_fields
== 0)
7129 process_init_element (pop_init_level (1, braced_init_obstack
),
7130 true, braced_init_obstack
);
7131 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7132 && constructor_max_index
7133 && tree_int_cst_lt (constructor_max_index
,
7135 process_init_element (pop_init_level (1, braced_init_obstack
),
7136 true, braced_init_obstack
);
7142 /* Unless this is an explicit brace, we need to preserve previous
7146 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7147 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7148 && constructor_fields
)
7149 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7150 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7151 value
= find_init_member (constructor_index
, braced_init_obstack
);
7154 p
= XNEW (struct constructor_stack
);
7155 p
->type
= constructor_type
;
7156 p
->fields
= constructor_fields
;
7157 p
->index
= constructor_index
;
7158 p
->max_index
= constructor_max_index
;
7159 p
->unfilled_index
= constructor_unfilled_index
;
7160 p
->unfilled_fields
= constructor_unfilled_fields
;
7161 p
->bit_index
= constructor_bit_index
;
7162 p
->elements
= constructor_elements
;
7163 p
->constant
= constructor_constant
;
7164 p
->simple
= constructor_simple
;
7165 p
->nonconst
= constructor_nonconst
;
7166 p
->erroneous
= constructor_erroneous
;
7167 p
->pending_elts
= constructor_pending_elts
;
7168 p
->depth
= constructor_depth
;
7169 p
->replacement_value
.value
= 0;
7170 p
->replacement_value
.original_code
= ERROR_MARK
;
7171 p
->replacement_value
.original_type
= NULL
;
7172 p
->implicit
= implicit
;
7174 p
->incremental
= constructor_incremental
;
7175 p
->designated
= constructor_designated
;
7176 p
->next
= constructor_stack
;
7178 constructor_stack
= p
;
7180 constructor_constant
= 1;
7181 constructor_simple
= 1;
7182 constructor_nonconst
= 0;
7183 constructor_depth
= SPELLING_DEPTH ();
7184 constructor_elements
= NULL
;
7185 constructor_incremental
= 1;
7186 constructor_designated
= 0;
7187 constructor_pending_elts
= 0;
7190 p
->range_stack
= constructor_range_stack
;
7191 constructor_range_stack
= 0;
7192 designator_depth
= 0;
7193 designator_erroneous
= 0;
7196 /* Don't die if an entire brace-pair level is superfluous
7197 in the containing level. */
7198 if (constructor_type
== 0)
7200 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7201 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7203 /* Don't die if there are extra init elts at the end. */
7204 if (constructor_fields
== 0)
7205 constructor_type
= 0;
7208 constructor_type
= TREE_TYPE (constructor_fields
);
7209 push_member_name (constructor_fields
);
7210 constructor_depth
++;
7213 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7215 constructor_type
= TREE_TYPE (constructor_type
);
7216 push_array_bounds (tree_to_uhwi (constructor_index
));
7217 constructor_depth
++;
7220 if (constructor_type
== 0)
7222 error_init ("extra brace group at end of initializer");
7223 constructor_fields
= 0;
7224 constructor_unfilled_fields
= 0;
7228 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7230 constructor_constant
= TREE_CONSTANT (value
);
7231 constructor_simple
= TREE_STATIC (value
);
7232 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7233 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7234 if (!vec_safe_is_empty (constructor_elements
)
7235 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7236 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7237 set_nonincremental_init (braced_init_obstack
);
7240 if (implicit
== 1 && warn_missing_braces
&& !missing_braces_mentioned
)
7242 missing_braces_mentioned
= 1;
7243 warning_init (OPT_Wmissing_braces
, "missing braces around initializer");
7246 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7247 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7249 constructor_fields
= TYPE_FIELDS (constructor_type
);
7250 /* Skip any nameless bit fields at the beginning. */
7251 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7252 && DECL_NAME (constructor_fields
) == 0)
7253 constructor_fields
= DECL_CHAIN (constructor_fields
);
7255 constructor_unfilled_fields
= constructor_fields
;
7256 constructor_bit_index
= bitsize_zero_node
;
7258 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7260 /* Vectors are like simple fixed-size arrays. */
7261 constructor_max_index
=
7262 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7263 constructor_index
= bitsize_int (0);
7264 constructor_unfilled_index
= constructor_index
;
7266 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7268 if (TYPE_DOMAIN (constructor_type
))
7270 constructor_max_index
7271 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7273 /* Detect non-empty initializations of zero-length arrays. */
7274 if (constructor_max_index
== NULL_TREE
7275 && TYPE_SIZE (constructor_type
))
7276 constructor_max_index
= integer_minus_one_node
;
7278 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7279 to initialize VLAs will cause a proper error; avoid tree
7280 checking errors as well by setting a safe value. */
7281 if (constructor_max_index
7282 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7283 constructor_max_index
= integer_minus_one_node
;
7286 = convert (bitsizetype
,
7287 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7290 constructor_index
= bitsize_zero_node
;
7292 constructor_unfilled_index
= constructor_index
;
7293 if (value
&& TREE_CODE (value
) == STRING_CST
)
7295 /* We need to split the char/wchar array into individual
7296 characters, so that we don't have to special case it
7298 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7303 if (constructor_type
!= error_mark_node
)
7304 warning_init (0, "braces around scalar initializer");
7305 constructor_fields
= constructor_type
;
7306 constructor_unfilled_fields
= constructor_type
;
7310 /* At the end of an implicit or explicit brace level,
7311 finish up that level of constructor. If a single expression
7312 with redundant braces initialized that level, return the
7313 c_expr structure for that expression. Otherwise, the original_code
7314 element is set to ERROR_MARK.
7315 If we were outputting the elements as they are read, return 0 as the value
7316 from inner levels (process_init_element ignores that),
7317 but return error_mark_node as the value from the outermost level
7318 (that's what we want to put in DECL_INITIAL).
7319 Otherwise, return a CONSTRUCTOR expression as the value. */
7322 pop_init_level (int implicit
, struct obstack
* braced_init_obstack
)
7324 struct constructor_stack
*p
;
7327 ret
.original_code
= ERROR_MARK
;
7328 ret
.original_type
= NULL
;
7332 /* When we come to an explicit close brace,
7333 pop any inner levels that didn't have explicit braces. */
7334 while (constructor_stack
->implicit
)
7336 process_init_element (pop_init_level (1, braced_init_obstack
),
7337 true, braced_init_obstack
);
7339 gcc_assert (!constructor_range_stack
);
7342 /* Now output all pending elements. */
7343 constructor_incremental
= 1;
7344 output_pending_init_elements (1, braced_init_obstack
);
7346 p
= constructor_stack
;
7348 /* Error for initializing a flexible array member, or a zero-length
7349 array member in an inappropriate context. */
7350 if (constructor_type
&& constructor_fields
7351 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7352 && TYPE_DOMAIN (constructor_type
)
7353 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7355 /* Silently discard empty initializations. The parser will
7356 already have pedwarned for empty brackets. */
7357 if (integer_zerop (constructor_unfilled_index
))
7358 constructor_type
= NULL_TREE
;
7361 gcc_assert (!TYPE_SIZE (constructor_type
));
7363 if (constructor_depth
> 2)
7364 error_init ("initialization of flexible array member in a nested context");
7366 pedwarn_init (input_location
, OPT_Wpedantic
,
7367 "initialization of a flexible array member");
7369 /* We have already issued an error message for the existence
7370 of a flexible array member not at the end of the structure.
7371 Discard the initializer so that we do not die later. */
7372 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7373 constructor_type
= NULL_TREE
;
7377 /* Warn when some struct elements are implicitly initialized to zero. */
7378 if (warn_missing_field_initializers
7380 && TREE_CODE (constructor_type
) == RECORD_TYPE
7381 && constructor_unfilled_fields
)
7383 bool constructor_zeroinit
=
7384 (vec_safe_length (constructor_elements
) == 1
7385 && integer_zerop ((*constructor_elements
)[0].value
));
7387 /* Do not warn for flexible array members or zero-length arrays. */
7388 while (constructor_unfilled_fields
7389 && (!DECL_SIZE (constructor_unfilled_fields
)
7390 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
7391 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
7393 if (constructor_unfilled_fields
7394 /* Do not warn if this level of the initializer uses member
7395 designators; it is likely to be deliberate. */
7396 && !constructor_designated
7397 /* Do not warn about initializing with ` = {0}'. */
7398 && !constructor_zeroinit
)
7400 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
7401 "missing initializer for field %qD of %qT",
7402 constructor_unfilled_fields
,
7404 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
7405 "%qD declared here", constructor_unfilled_fields
);
7409 /* Pad out the end of the structure. */
7410 if (p
->replacement_value
.value
)
7411 /* If this closes a superfluous brace pair,
7412 just pass out the element between them. */
7413 ret
= p
->replacement_value
;
7414 else if (constructor_type
== 0)
7416 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
7417 && TREE_CODE (constructor_type
) != UNION_TYPE
7418 && TREE_CODE (constructor_type
) != ARRAY_TYPE
7419 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
7421 /* A nonincremental scalar initializer--just return
7422 the element, after verifying there is just one. */
7423 if (vec_safe_is_empty (constructor_elements
))
7425 if (!constructor_erroneous
)
7426 error_init ("empty scalar initializer");
7427 ret
.value
= error_mark_node
;
7429 else if (vec_safe_length (constructor_elements
) != 1)
7431 error_init ("extra elements in scalar initializer");
7432 ret
.value
= (*constructor_elements
)[0].value
;
7435 ret
.value
= (*constructor_elements
)[0].value
;
7439 if (constructor_erroneous
)
7440 ret
.value
= error_mark_node
;
7443 ret
.value
= build_constructor (constructor_type
,
7444 constructor_elements
);
7445 if (constructor_constant
)
7446 TREE_CONSTANT (ret
.value
) = 1;
7447 if (constructor_constant
&& constructor_simple
)
7448 TREE_STATIC (ret
.value
) = 1;
7449 if (constructor_nonconst
)
7450 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
7454 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
7456 if (constructor_nonconst
)
7457 ret
.original_code
= C_MAYBE_CONST_EXPR
;
7458 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
7459 ret
.original_code
= ERROR_MARK
;
7462 constructor_type
= p
->type
;
7463 constructor_fields
= p
->fields
;
7464 constructor_index
= p
->index
;
7465 constructor_max_index
= p
->max_index
;
7466 constructor_unfilled_index
= p
->unfilled_index
;
7467 constructor_unfilled_fields
= p
->unfilled_fields
;
7468 constructor_bit_index
= p
->bit_index
;
7469 constructor_elements
= p
->elements
;
7470 constructor_constant
= p
->constant
;
7471 constructor_simple
= p
->simple
;
7472 constructor_nonconst
= p
->nonconst
;
7473 constructor_erroneous
= p
->erroneous
;
7474 constructor_incremental
= p
->incremental
;
7475 constructor_designated
= p
->designated
;
7476 constructor_pending_elts
= p
->pending_elts
;
7477 constructor_depth
= p
->depth
;
7479 constructor_range_stack
= p
->range_stack
;
7480 RESTORE_SPELLING_DEPTH (constructor_depth
);
7482 constructor_stack
= p
->next
;
7485 if (ret
.value
== 0 && constructor_stack
== 0)
7486 ret
.value
= error_mark_node
;
7490 /* Common handling for both array range and field name designators.
7491 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7494 set_designator (int array
, struct obstack
* braced_init_obstack
)
7497 enum tree_code subcode
;
7499 /* Don't die if an entire brace-pair level is superfluous
7500 in the containing level. */
7501 if (constructor_type
== 0)
7504 /* If there were errors in this designator list already, bail out
7506 if (designator_erroneous
)
7509 if (!designator_depth
)
7511 gcc_assert (!constructor_range_stack
);
7513 /* Designator list starts at the level of closest explicit
7515 while (constructor_stack
->implicit
)
7517 process_init_element (pop_init_level (1, braced_init_obstack
),
7518 true, braced_init_obstack
);
7520 constructor_designated
= 1;
7524 switch (TREE_CODE (constructor_type
))
7528 subtype
= TREE_TYPE (constructor_fields
);
7529 if (subtype
!= error_mark_node
)
7530 subtype
= TYPE_MAIN_VARIANT (subtype
);
7533 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7539 subcode
= TREE_CODE (subtype
);
7540 if (array
&& subcode
!= ARRAY_TYPE
)
7542 error_init ("array index in non-array initializer");
7545 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
7547 error_init ("field name not in record or union initializer");
7551 constructor_designated
= 1;
7552 push_init_level (2, braced_init_obstack
);
7556 /* If there are range designators in designator list, push a new designator
7557 to constructor_range_stack. RANGE_END is end of such stack range or
7558 NULL_TREE if there is no range designator at this level. */
7561 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
7563 struct constructor_range_stack
*p
;
7565 p
= (struct constructor_range_stack
*)
7566 obstack_alloc (braced_init_obstack
,
7567 sizeof (struct constructor_range_stack
));
7568 p
->prev
= constructor_range_stack
;
7570 p
->fields
= constructor_fields
;
7571 p
->range_start
= constructor_index
;
7572 p
->index
= constructor_index
;
7573 p
->stack
= constructor_stack
;
7574 p
->range_end
= range_end
;
7575 if (constructor_range_stack
)
7576 constructor_range_stack
->next
= p
;
7577 constructor_range_stack
= p
;
7580 /* Within an array initializer, specify the next index to be initialized.
7581 FIRST is that index. If LAST is nonzero, then initialize a range
7582 of indices, running from FIRST through LAST. */
7585 set_init_index (tree first
, tree last
,
7586 struct obstack
* braced_init_obstack
)
7588 if (set_designator (1, braced_init_obstack
))
7591 designator_erroneous
= 1;
7593 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
7594 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
7596 error_init ("array index in initializer not of integer type");
7600 if (TREE_CODE (first
) != INTEGER_CST
)
7602 first
= c_fully_fold (first
, false, NULL
);
7603 if (TREE_CODE (first
) == INTEGER_CST
)
7604 pedwarn_init (input_location
, OPT_Wpedantic
,
7605 "array index in initializer is not "
7606 "an integer constant expression");
7609 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
7611 last
= c_fully_fold (last
, false, NULL
);
7612 if (TREE_CODE (last
) == INTEGER_CST
)
7613 pedwarn_init (input_location
, OPT_Wpedantic
,
7614 "array index in initializer is not "
7615 "an integer constant expression");
7618 if (TREE_CODE (first
) != INTEGER_CST
)
7619 error_init ("nonconstant array index in initializer");
7620 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
7621 error_init ("nonconstant array index in initializer");
7622 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7623 error_init ("array index in non-array initializer");
7624 else if (tree_int_cst_sgn (first
) == -1)
7625 error_init ("array index in initializer exceeds array bounds");
7626 else if (constructor_max_index
7627 && tree_int_cst_lt (constructor_max_index
, first
))
7628 error_init ("array index in initializer exceeds array bounds");
7631 constant_expression_warning (first
);
7633 constant_expression_warning (last
);
7634 constructor_index
= convert (bitsizetype
, first
);
7635 if (tree_int_cst_lt (constructor_index
, first
))
7637 constructor_index
= copy_node (constructor_index
);
7638 TREE_OVERFLOW (constructor_index
) = 1;
7643 if (tree_int_cst_equal (first
, last
))
7645 else if (tree_int_cst_lt (last
, first
))
7647 error_init ("empty index range in initializer");
7652 last
= convert (bitsizetype
, last
);
7653 if (constructor_max_index
!= 0
7654 && tree_int_cst_lt (constructor_max_index
, last
))
7656 error_init ("array index range in initializer exceeds array bounds");
7663 designator_erroneous
= 0;
7664 if (constructor_range_stack
|| last
)
7665 push_range_stack (last
, braced_init_obstack
);
7669 /* Within a struct initializer, specify the next field to be initialized. */
7672 set_init_label (tree fieldname
, struct obstack
* braced_init_obstack
)
7676 if (set_designator (0, braced_init_obstack
))
7679 designator_erroneous
= 1;
7681 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7682 && TREE_CODE (constructor_type
) != UNION_TYPE
)
7684 error_init ("field name not in record or union initializer");
7688 field
= lookup_field (constructor_type
, fieldname
);
7691 error ("unknown field %qE specified in initializer", fieldname
);
7695 constructor_fields
= TREE_VALUE (field
);
7697 designator_erroneous
= 0;
7698 if (constructor_range_stack
)
7699 push_range_stack (NULL_TREE
, braced_init_obstack
);
7700 field
= TREE_CHAIN (field
);
7703 if (set_designator (0, braced_init_obstack
))
7707 while (field
!= NULL_TREE
);
7710 /* Add a new initializer to the tree of pending initializers. PURPOSE
7711 identifies the initializer, either array index or field in a structure.
7712 VALUE is the value of that index or field. If ORIGTYPE is not
7713 NULL_TREE, it is the original type of VALUE.
7715 IMPLICIT is true if value comes from pop_init_level (1),
7716 the new initializer has been merged with the existing one
7717 and thus no warnings should be emitted about overriding an
7718 existing initializer. */
7721 add_pending_init (tree purpose
, tree value
, tree origtype
, bool implicit
,
7722 struct obstack
* braced_init_obstack
)
7724 struct init_node
*p
, **q
, *r
;
7726 q
= &constructor_pending_elts
;
7729 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7734 if (tree_int_cst_lt (purpose
, p
->purpose
))
7736 else if (tree_int_cst_lt (p
->purpose
, purpose
))
7742 if (TREE_SIDE_EFFECTS (p
->value
))
7743 warning_init (0, "initialized field with side-effects overwritten");
7744 else if (warn_override_init
)
7745 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7748 p
->origtype
= origtype
;
7757 bitpos
= bit_position (purpose
);
7761 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
7763 else if (p
->purpose
!= purpose
)
7769 if (TREE_SIDE_EFFECTS (p
->value
))
7770 warning_init (0, "initialized field with side-effects overwritten");
7771 else if (warn_override_init
)
7772 warning_init (OPT_Woverride_init
, "initialized field overwritten");
7775 p
->origtype
= origtype
;
7781 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
7782 sizeof (struct init_node
));
7783 r
->purpose
= purpose
;
7785 r
->origtype
= origtype
;
7795 struct init_node
*s
;
7799 if (p
->balance
== 0)
7801 else if (p
->balance
< 0)
7808 p
->left
->parent
= p
;
7825 constructor_pending_elts
= r
;
7830 struct init_node
*t
= r
->right
;
7834 r
->right
->parent
= r
;
7839 p
->left
->parent
= p
;
7842 p
->balance
= t
->balance
< 0;
7843 r
->balance
= -(t
->balance
> 0);
7858 constructor_pending_elts
= t
;
7864 /* p->balance == +1; growth of left side balances the node. */
7869 else /* r == p->right */
7871 if (p
->balance
== 0)
7872 /* Growth propagation from right side. */
7874 else if (p
->balance
> 0)
7881 p
->right
->parent
= p
;
7898 constructor_pending_elts
= r
;
7900 else /* r->balance == -1 */
7903 struct init_node
*t
= r
->left
;
7907 r
->left
->parent
= r
;
7912 p
->right
->parent
= p
;
7915 r
->balance
= (t
->balance
< 0);
7916 p
->balance
= -(t
->balance
> 0);
7931 constructor_pending_elts
= t
;
7937 /* p->balance == -1; growth of right side balances the node. */
7948 /* Build AVL tree from a sorted chain. */
7951 set_nonincremental_init (struct obstack
* braced_init_obstack
)
7953 unsigned HOST_WIDE_INT ix
;
7956 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7957 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7960 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
7962 add_pending_init (index
, value
, NULL_TREE
, true,
7963 braced_init_obstack
);
7965 constructor_elements
= NULL
;
7966 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
7968 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
7969 /* Skip any nameless bit fields at the beginning. */
7970 while (constructor_unfilled_fields
!= 0
7971 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
7972 && DECL_NAME (constructor_unfilled_fields
) == 0)
7973 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
7976 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7978 if (TYPE_DOMAIN (constructor_type
))
7979 constructor_unfilled_index
7980 = convert (bitsizetype
,
7981 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7983 constructor_unfilled_index
= bitsize_zero_node
;
7985 constructor_incremental
= 0;
7988 /* Build AVL tree from a string constant. */
7991 set_nonincremental_init_from_string (tree str
,
7992 struct obstack
* braced_init_obstack
)
7994 tree value
, purpose
, type
;
7995 HOST_WIDE_INT val
[2];
7996 const char *p
, *end
;
7997 int byte
, wchar_bytes
, charwidth
, bitpos
;
7999 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8001 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8002 charwidth
= TYPE_PRECISION (char_type_node
);
8003 type
= TREE_TYPE (constructor_type
);
8004 p
= TREE_STRING_POINTER (str
);
8005 end
= p
+ TREE_STRING_LENGTH (str
);
8007 for (purpose
= bitsize_zero_node
;
8009 && !(constructor_max_index
8010 && tree_int_cst_lt (constructor_max_index
, purpose
));
8011 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8013 if (wchar_bytes
== 1)
8015 val
[0] = (unsigned char) *p
++;
8022 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8024 if (BYTES_BIG_ENDIAN
)
8025 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8027 bitpos
= byte
* charwidth
;
8028 val
[bitpos
% HOST_BITS_PER_WIDE_INT
]
8029 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8030 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8034 if (!TYPE_UNSIGNED (type
))
8036 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8037 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8039 if (val
[0] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
8041 val
[0] |= ((HOST_WIDE_INT
) -1) << bitpos
;
8045 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8050 else if (val
[1] & (((HOST_WIDE_INT
) 1)
8051 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8052 val
[1] |= ((HOST_WIDE_INT
) -1)
8053 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
8056 value
= wide_int_to_tree (type
,
8057 wide_int::from_array (val
, 2,
8058 HOST_BITS_PER_WIDE_INT
* 2));
8059 add_pending_init (purpose
, value
, NULL_TREE
, true,
8060 braced_init_obstack
);
8063 constructor_incremental
= 0;
8066 /* Return value of FIELD in pending initializer or zero if the field was
8067 not initialized yet. */
8070 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8072 struct init_node
*p
;
8074 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8076 if (constructor_incremental
8077 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8078 set_nonincremental_init (braced_init_obstack
);
8080 p
= constructor_pending_elts
;
8083 if (tree_int_cst_lt (field
, p
->purpose
))
8085 else if (tree_int_cst_lt (p
->purpose
, field
))
8091 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8093 tree bitpos
= bit_position (field
);
8095 if (constructor_incremental
8096 && (!constructor_unfilled_fields
8097 || tree_int_cst_lt (bitpos
,
8098 bit_position (constructor_unfilled_fields
))))
8099 set_nonincremental_init (braced_init_obstack
);
8101 p
= constructor_pending_elts
;
8104 if (field
== p
->purpose
)
8106 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8112 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8114 if (!vec_safe_is_empty (constructor_elements
)
8115 && (constructor_elements
->last ().index
== field
))
8116 return constructor_elements
->last ().value
;
8121 /* "Output" the next constructor element.
8122 At top level, really output it to assembler code now.
8123 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8124 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8125 TYPE is the data type that the containing data type wants here.
8126 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8127 If VALUE is a string constant, STRICT_STRING is true if it is
8128 unparenthesized or we should not warn here for it being parenthesized.
8129 For other types of VALUE, STRICT_STRING is not used.
8131 PENDING if non-nil means output pending elements that belong
8132 right after this element. (PENDING is normally 1;
8133 it is 0 while outputting pending elements, to avoid recursion.)
8135 IMPLICIT is true if value comes from pop_init_level (1),
8136 the new initializer has been merged with the existing one
8137 and thus no warnings should be emitted about overriding an
8138 existing initializer. */
8141 output_init_element (tree value
, tree origtype
, bool strict_string
, tree type
,
8142 tree field
, int pending
, bool implicit
,
8143 struct obstack
* braced_init_obstack
)
8145 tree semantic_type
= NULL_TREE
;
8146 bool maybe_const
= true;
8149 if (type
== error_mark_node
|| value
== error_mark_node
)
8151 constructor_erroneous
= 1;
8154 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8155 && (TREE_CODE (value
) == STRING_CST
8156 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8157 && !(TREE_CODE (value
) == STRING_CST
8158 && TREE_CODE (type
) == ARRAY_TYPE
8159 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8160 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8161 TYPE_MAIN_VARIANT (type
)))
8162 value
= array_to_pointer_conversion (input_location
, value
);
8164 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8165 && require_constant_value
&& !flag_isoc99
&& pending
)
8167 /* As an extension, allow initializing objects with static storage
8168 duration with compound literals (which are then treated just as
8169 the brace enclosed list they contain). */
8170 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8171 value
= DECL_INITIAL (decl
);
8174 npc
= null_pointer_constant_p (value
);
8175 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8177 semantic_type
= TREE_TYPE (value
);
8178 value
= TREE_OPERAND (value
, 0);
8180 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8182 if (value
== error_mark_node
)
8183 constructor_erroneous
= 1;
8184 else if (!TREE_CONSTANT (value
))
8185 constructor_constant
= 0;
8186 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
8187 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
8188 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8189 && DECL_C_BIT_FIELD (field
)
8190 && TREE_CODE (value
) != INTEGER_CST
))
8191 constructor_simple
= 0;
8193 constructor_nonconst
= 1;
8195 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8197 if (require_constant_value
)
8199 error_init ("initializer element is not constant");
8200 value
= error_mark_node
;
8202 else if (require_constant_elements
)
8203 pedwarn (input_location
, 0,
8204 "initializer element is not computable at load time");
8206 else if (!maybe_const
8207 && (require_constant_value
|| require_constant_elements
))
8208 pedwarn_init (input_location
, 0,
8209 "initializer element is not a constant expression");
8211 /* Issue -Wc++-compat warnings about initializing a bitfield with
8214 && field
!= NULL_TREE
8215 && TREE_CODE (field
) == FIELD_DECL
8216 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8217 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8218 != TYPE_MAIN_VARIANT (type
))
8219 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8221 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8222 if (checktype
!= error_mark_node
8223 && (TYPE_MAIN_VARIANT (checktype
)
8224 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8225 warning_init (OPT_Wc___compat
,
8226 "enum conversion in initialization is invalid in C++");
8229 /* If this field is empty (and not at the end of structure),
8230 don't do anything other than checking the initializer. */
8232 && (TREE_TYPE (field
) == error_mark_node
8233 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8234 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8235 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8236 || DECL_CHAIN (field
)))))
8240 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8241 value
= digest_init (input_location
, type
, value
, origtype
, npc
,
8242 strict_string
, require_constant_value
);
8243 if (value
== error_mark_node
)
8245 constructor_erroneous
= 1;
8248 if (require_constant_value
|| require_constant_elements
)
8249 constant_expression_warning (value
);
8251 /* If this element doesn't come next in sequence,
8252 put it on constructor_pending_elts. */
8253 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8254 && (!constructor_incremental
8255 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8257 if (constructor_incremental
8258 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8259 set_nonincremental_init (braced_init_obstack
);
8261 add_pending_init (field
, value
, origtype
, implicit
,
8262 braced_init_obstack
);
8265 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8266 && (!constructor_incremental
8267 || field
!= constructor_unfilled_fields
))
8269 /* We do this for records but not for unions. In a union,
8270 no matter which field is specified, it can be initialized
8271 right away since it starts at the beginning of the union. */
8272 if (constructor_incremental
)
8274 if (!constructor_unfilled_fields
)
8275 set_nonincremental_init (braced_init_obstack
);
8278 tree bitpos
, unfillpos
;
8280 bitpos
= bit_position (field
);
8281 unfillpos
= bit_position (constructor_unfilled_fields
);
8283 if (tree_int_cst_lt (bitpos
, unfillpos
))
8284 set_nonincremental_init (braced_init_obstack
);
8288 add_pending_init (field
, value
, origtype
, implicit
,
8289 braced_init_obstack
);
8292 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8293 && !vec_safe_is_empty (constructor_elements
))
8297 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8299 "initialized field with side-effects overwritten");
8300 else if (warn_override_init
)
8301 warning_init (OPT_Woverride_init
, "initialized field overwritten");
8304 /* We can have just one union field set. */
8305 constructor_elements
= NULL
;
8308 /* Otherwise, output this element either to
8309 constructor_elements or to the assembler file. */
8311 constructor_elt celt
= {field
, value
};
8312 vec_safe_push (constructor_elements
, celt
);
8314 /* Advance the variable that indicates sequential elements output. */
8315 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8316 constructor_unfilled_index
8317 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8319 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8321 constructor_unfilled_fields
8322 = DECL_CHAIN (constructor_unfilled_fields
);
8324 /* Skip any nameless bit fields. */
8325 while (constructor_unfilled_fields
!= 0
8326 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8327 && DECL_NAME (constructor_unfilled_fields
) == 0)
8328 constructor_unfilled_fields
=
8329 DECL_CHAIN (constructor_unfilled_fields
);
8331 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8332 constructor_unfilled_fields
= 0;
8334 /* Now output any pending elements which have become next. */
8336 output_pending_init_elements (0, braced_init_obstack
);
8339 /* Output any pending elements which have become next.
8340 As we output elements, constructor_unfilled_{fields,index}
8341 advances, which may cause other elements to become next;
8342 if so, they too are output.
8344 If ALL is 0, we return when there are
8345 no more pending elements to output now.
8347 If ALL is 1, we output space as necessary so that
8348 we can output all the pending elements. */
8350 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8352 struct init_node
*elt
= constructor_pending_elts
;
8357 /* Look through the whole pending tree.
8358 If we find an element that should be output now,
8359 output it. Otherwise, set NEXT to the element
8360 that comes first among those still pending. */
8365 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8367 if (tree_int_cst_equal (elt
->purpose
,
8368 constructor_unfilled_index
))
8369 output_init_element (elt
->value
, elt
->origtype
, true,
8370 TREE_TYPE (constructor_type
),
8371 constructor_unfilled_index
, 0, false,
8372 braced_init_obstack
);
8373 else if (tree_int_cst_lt (constructor_unfilled_index
,
8376 /* Advance to the next smaller node. */
8381 /* We have reached the smallest node bigger than the
8382 current unfilled index. Fill the space first. */
8383 next
= elt
->purpose
;
8389 /* Advance to the next bigger node. */
8394 /* We have reached the biggest node in a subtree. Find
8395 the parent of it, which is the next bigger node. */
8396 while (elt
->parent
&& elt
->parent
->right
== elt
)
8399 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
8402 next
= elt
->purpose
;
8408 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8409 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8411 tree ctor_unfilled_bitpos
, elt_bitpos
;
8413 /* If the current record is complete we are done. */
8414 if (constructor_unfilled_fields
== 0)
8417 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
8418 elt_bitpos
= bit_position (elt
->purpose
);
8419 /* We can't compare fields here because there might be empty
8420 fields in between. */
8421 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
8423 constructor_unfilled_fields
= elt
->purpose
;
8424 output_init_element (elt
->value
, elt
->origtype
, true,
8425 TREE_TYPE (elt
->purpose
),
8426 elt
->purpose
, 0, false,
8427 braced_init_obstack
);
8429 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
8431 /* Advance to the next smaller node. */
8436 /* We have reached the smallest node bigger than the
8437 current unfilled field. Fill the space first. */
8438 next
= elt
->purpose
;
8444 /* Advance to the next bigger node. */
8449 /* We have reached the biggest node in a subtree. Find
8450 the parent of it, which is the next bigger node. */
8451 while (elt
->parent
&& elt
->parent
->right
== elt
)
8455 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
8456 bit_position (elt
->purpose
))))
8458 next
= elt
->purpose
;
8466 /* Ordinarily return, but not if we want to output all
8467 and there are elements left. */
8468 if (!(all
&& next
!= 0))
8471 /* If it's not incremental, just skip over the gap, so that after
8472 jumping to retry we will output the next successive element. */
8473 if (TREE_CODE (constructor_type
) == RECORD_TYPE
8474 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8475 constructor_unfilled_fields
= next
;
8476 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8477 constructor_unfilled_index
= next
;
8479 /* ELT now points to the node in the pending tree with the next
8480 initializer to output. */
8484 /* Add one non-braced element to the current constructor level.
8485 This adjusts the current position within the constructor's type.
8486 This may also start or terminate implicit levels
8487 to handle a partly-braced initializer.
8489 Once this has found the correct level for the new element,
8490 it calls output_init_element.
8492 IMPLICIT is true if value comes from pop_init_level (1),
8493 the new initializer has been merged with the existing one
8494 and thus no warnings should be emitted about overriding an
8495 existing initializer. */
8498 process_init_element (struct c_expr value
, bool implicit
,
8499 struct obstack
* braced_init_obstack
)
8501 tree orig_value
= value
.value
;
8502 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
8503 bool strict_string
= value
.original_code
== STRING_CST
;
8505 designator_depth
= 0;
8506 designator_erroneous
= 0;
8508 /* Handle superfluous braces around string cst as in
8509 char x[] = {"foo"}; */
8512 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8513 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
8514 && integer_zerop (constructor_unfilled_index
))
8516 if (constructor_stack
->replacement_value
.value
)
8517 error_init ("excess elements in char array initializer");
8518 constructor_stack
->replacement_value
= value
;
8522 if (constructor_stack
->replacement_value
.value
!= 0)
8524 error_init ("excess elements in struct initializer");
8528 /* Ignore elements of a brace group if it is entirely superfluous
8529 and has already been diagnosed. */
8530 if (constructor_type
== 0)
8533 /* If we've exhausted any levels that didn't have braces,
8535 while (constructor_stack
->implicit
)
8537 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
8538 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8539 && constructor_fields
== 0)
8540 process_init_element (pop_init_level (1, braced_init_obstack
),
8541 true, braced_init_obstack
);
8542 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
8543 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8544 && constructor_max_index
8545 && tree_int_cst_lt (constructor_max_index
,
8547 process_init_element (pop_init_level (1, braced_init_obstack
),
8548 true, braced_init_obstack
);
8553 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8554 if (constructor_range_stack
)
8556 /* If value is a compound literal and we'll be just using its
8557 content, don't put it into a SAVE_EXPR. */
8558 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
8559 || !require_constant_value
8562 tree semantic_type
= NULL_TREE
;
8563 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
8565 semantic_type
= TREE_TYPE (value
.value
);
8566 value
.value
= TREE_OPERAND (value
.value
, 0);
8568 value
.value
= c_save_expr (value
.value
);
8570 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
8577 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8580 enum tree_code fieldcode
;
8582 if (constructor_fields
== 0)
8584 pedwarn_init (input_location
, 0,
8585 "excess elements in struct initializer");
8589 fieldtype
= TREE_TYPE (constructor_fields
);
8590 if (fieldtype
!= error_mark_node
)
8591 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8592 fieldcode
= TREE_CODE (fieldtype
);
8594 /* Error for non-static initialization of a flexible array member. */
8595 if (fieldcode
== ARRAY_TYPE
8596 && !require_constant_value
8597 && TYPE_SIZE (fieldtype
) == NULL_TREE
8598 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8600 error_init ("non-static initialization of a flexible array member");
8604 /* Accept a string constant to initialize a subarray. */
8605 if (value
.value
!= 0
8606 && fieldcode
== ARRAY_TYPE
8607 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8609 value
.value
= orig_value
;
8610 /* Otherwise, if we have come to a subaggregate,
8611 and we don't have an element of its type, push into it. */
8612 else if (value
.value
!= 0
8613 && value
.value
!= error_mark_node
8614 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8615 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8616 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8618 push_init_level (1, braced_init_obstack
);
8624 push_member_name (constructor_fields
);
8625 output_init_element (value
.value
, value
.original_type
,
8626 strict_string
, fieldtype
,
8627 constructor_fields
, 1, implicit
,
8628 braced_init_obstack
);
8629 RESTORE_SPELLING_DEPTH (constructor_depth
);
8632 /* Do the bookkeeping for an element that was
8633 directly output as a constructor. */
8635 /* For a record, keep track of end position of last field. */
8636 if (DECL_SIZE (constructor_fields
))
8637 constructor_bit_index
8638 = size_binop_loc (input_location
, PLUS_EXPR
,
8639 bit_position (constructor_fields
),
8640 DECL_SIZE (constructor_fields
));
8642 /* If the current field was the first one not yet written out,
8643 it isn't now, so update. */
8644 if (constructor_unfilled_fields
== constructor_fields
)
8646 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8647 /* Skip any nameless bit fields. */
8648 while (constructor_unfilled_fields
!= 0
8649 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8650 && DECL_NAME (constructor_unfilled_fields
) == 0)
8651 constructor_unfilled_fields
=
8652 DECL_CHAIN (constructor_unfilled_fields
);
8656 constructor_fields
= DECL_CHAIN (constructor_fields
);
8657 /* Skip any nameless bit fields at the beginning. */
8658 while (constructor_fields
!= 0
8659 && DECL_C_BIT_FIELD (constructor_fields
)
8660 && DECL_NAME (constructor_fields
) == 0)
8661 constructor_fields
= DECL_CHAIN (constructor_fields
);
8663 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8666 enum tree_code fieldcode
;
8668 if (constructor_fields
== 0)
8670 pedwarn_init (input_location
, 0,
8671 "excess elements in union initializer");
8675 fieldtype
= TREE_TYPE (constructor_fields
);
8676 if (fieldtype
!= error_mark_node
)
8677 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8678 fieldcode
= TREE_CODE (fieldtype
);
8680 /* Warn that traditional C rejects initialization of unions.
8681 We skip the warning if the value is zero. This is done
8682 under the assumption that the zero initializer in user
8683 code appears conditioned on e.g. __STDC__ to avoid
8684 "missing initializer" warnings and relies on default
8685 initialization to zero in the traditional C case.
8686 We also skip the warning if the initializer is designated,
8687 again on the assumption that this must be conditional on
8688 __STDC__ anyway (and we've already complained about the
8689 member-designator already). */
8690 if (!in_system_header
&& !constructor_designated
8691 && !(value
.value
&& (integer_zerop (value
.value
)
8692 || real_zerop (value
.value
))))
8693 warning (OPT_Wtraditional
, "traditional C rejects initialization "
8696 /* Accept a string constant to initialize a subarray. */
8697 if (value
.value
!= 0
8698 && fieldcode
== ARRAY_TYPE
8699 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8701 value
.value
= orig_value
;
8702 /* Otherwise, if we have come to a subaggregate,
8703 and we don't have an element of its type, push into it. */
8704 else if (value
.value
!= 0
8705 && value
.value
!= error_mark_node
8706 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8707 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8708 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8710 push_init_level (1, braced_init_obstack
);
8716 push_member_name (constructor_fields
);
8717 output_init_element (value
.value
, value
.original_type
,
8718 strict_string
, fieldtype
,
8719 constructor_fields
, 1, implicit
,
8720 braced_init_obstack
);
8721 RESTORE_SPELLING_DEPTH (constructor_depth
);
8724 /* Do the bookkeeping for an element that was
8725 directly output as a constructor. */
8727 constructor_bit_index
= DECL_SIZE (constructor_fields
);
8728 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8731 constructor_fields
= 0;
8733 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8735 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8736 enum tree_code eltcode
= TREE_CODE (elttype
);
8738 /* Accept a string constant to initialize a subarray. */
8739 if (value
.value
!= 0
8740 && eltcode
== ARRAY_TYPE
8741 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
8743 value
.value
= orig_value
;
8744 /* Otherwise, if we have come to a subaggregate,
8745 and we don't have an element of its type, push into it. */
8746 else if (value
.value
!= 0
8747 && value
.value
!= error_mark_node
8748 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
8749 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
8750 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
8752 push_init_level (1, braced_init_obstack
);
8756 if (constructor_max_index
!= 0
8757 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
8758 || integer_all_onesp (constructor_max_index
)))
8760 pedwarn_init (input_location
, 0,
8761 "excess elements in array initializer");
8765 /* Now output the actual element. */
8768 push_array_bounds (tree_to_uhwi (constructor_index
));
8769 output_init_element (value
.value
, value
.original_type
,
8770 strict_string
, elttype
,
8771 constructor_index
, 1, implicit
,
8772 braced_init_obstack
);
8773 RESTORE_SPELLING_DEPTH (constructor_depth
);
8777 = size_binop_loc (input_location
, PLUS_EXPR
,
8778 constructor_index
, bitsize_one_node
);
8781 /* If we are doing the bookkeeping for an element that was
8782 directly output as a constructor, we must update
8783 constructor_unfilled_index. */
8784 constructor_unfilled_index
= constructor_index
;
8786 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8788 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
8790 /* Do a basic check of initializer size. Note that vectors
8791 always have a fixed size derived from their type. */
8792 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
8794 pedwarn_init (input_location
, 0,
8795 "excess elements in vector initializer");
8799 /* Now output the actual element. */
8802 if (TREE_CODE (value
.value
) == VECTOR_CST
)
8803 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
8804 output_init_element (value
.value
, value
.original_type
,
8805 strict_string
, elttype
,
8806 constructor_index
, 1, implicit
,
8807 braced_init_obstack
);
8811 = size_binop_loc (input_location
,
8812 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
8815 /* If we are doing the bookkeeping for an element that was
8816 directly output as a constructor, we must update
8817 constructor_unfilled_index. */
8818 constructor_unfilled_index
= constructor_index
;
8821 /* Handle the sole element allowed in a braced initializer
8822 for a scalar variable. */
8823 else if (constructor_type
!= error_mark_node
8824 && constructor_fields
== 0)
8826 pedwarn_init (input_location
, 0,
8827 "excess elements in scalar initializer");
8833 output_init_element (value
.value
, value
.original_type
,
8834 strict_string
, constructor_type
,
8835 NULL_TREE
, 1, implicit
,
8836 braced_init_obstack
);
8837 constructor_fields
= 0;
8840 /* Handle range initializers either at this level or anywhere higher
8841 in the designator stack. */
8842 if (constructor_range_stack
)
8844 struct constructor_range_stack
*p
, *range_stack
;
8847 range_stack
= constructor_range_stack
;
8848 constructor_range_stack
= 0;
8849 while (constructor_stack
!= range_stack
->stack
)
8851 gcc_assert (constructor_stack
->implicit
);
8852 process_init_element (pop_init_level (1,
8853 braced_init_obstack
),
8854 true, braced_init_obstack
);
8856 for (p
= range_stack
;
8857 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
8860 gcc_assert (constructor_stack
->implicit
);
8861 process_init_element (pop_init_level (1, braced_init_obstack
),
8862 true, braced_init_obstack
);
8865 p
->index
= size_binop_loc (input_location
,
8866 PLUS_EXPR
, p
->index
, bitsize_one_node
);
8867 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
8872 constructor_index
= p
->index
;
8873 constructor_fields
= p
->fields
;
8874 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
8882 push_init_level (2, braced_init_obstack
);
8883 p
->stack
= constructor_stack
;
8884 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
8885 p
->index
= p
->range_start
;
8889 constructor_range_stack
= range_stack
;
8896 constructor_range_stack
= 0;
8899 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8900 (guaranteed to be 'volatile' or null) and ARGS (represented using
8901 an ASM_EXPR node). */
8903 build_asm_stmt (tree cv_qualifier
, tree args
)
8905 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
8906 ASM_VOLATILE_P (args
) = 1;
8907 return add_stmt (args
);
8910 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8911 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8912 SIMPLE indicates whether there was anything at all after the
8913 string in the asm expression -- asm("blah") and asm("blah" : )
8914 are subtly different. We use a ASM_EXPR node to represent this. */
8916 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
8917 tree clobbers
, tree labels
, bool simple
)
8922 const char *constraint
;
8923 const char **oconstraints
;
8924 bool allows_mem
, allows_reg
, is_inout
;
8925 int ninputs
, noutputs
;
8927 ninputs
= list_length (inputs
);
8928 noutputs
= list_length (outputs
);
8929 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
8931 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
8933 /* Remove output conversions that change the type but not the mode. */
8934 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
8936 tree output
= TREE_VALUE (tail
);
8938 output
= c_fully_fold (output
, false, NULL
);
8940 /* ??? Really, this should not be here. Users should be using a
8941 proper lvalue, dammit. But there's a long history of using casts
8942 in the output operands. In cases like longlong.h, this becomes a
8943 primitive form of typechecking -- if the cast can be removed, then
8944 the output operand had a type of the proper width; otherwise we'll
8945 get an error. Gross, but ... */
8946 STRIP_NOPS (output
);
8948 if (!lvalue_or_else (loc
, output
, lv_asm
))
8949 output
= error_mark_node
;
8951 if (output
!= error_mark_node
8952 && (TREE_READONLY (output
)
8953 || TYPE_READONLY (TREE_TYPE (output
))
8954 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
8955 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
8956 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
8957 readonly_error (loc
, output
, lv_asm
);
8959 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
8960 oconstraints
[i
] = constraint
;
8962 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
8963 &allows_mem
, &allows_reg
, &is_inout
))
8965 /* If the operand is going to end up in memory,
8966 mark it addressable. */
8967 if (!allows_reg
&& !c_mark_addressable (output
))
8968 output
= error_mark_node
;
8969 if (!(!allows_reg
&& allows_mem
)
8970 && output
!= error_mark_node
8971 && VOID_TYPE_P (TREE_TYPE (output
)))
8973 error_at (loc
, "invalid use of void expression");
8974 output
= error_mark_node
;
8978 output
= error_mark_node
;
8980 TREE_VALUE (tail
) = output
;
8983 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
8987 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
8988 input
= TREE_VALUE (tail
);
8990 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
8991 oconstraints
, &allows_mem
, &allows_reg
))
8993 /* If the operand is going to end up in memory,
8994 mark it addressable. */
8995 if (!allows_reg
&& allows_mem
)
8997 input
= c_fully_fold (input
, false, NULL
);
8999 /* Strip the nops as we allow this case. FIXME, this really
9000 should be rejected or made deprecated. */
9002 if (!c_mark_addressable (input
))
9003 input
= error_mark_node
;
9008 memset (&expr
, 0, sizeof (expr
));
9010 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9011 input
= c_fully_fold (expr
.value
, false, NULL
);
9013 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9015 error_at (loc
, "invalid use of void expression");
9016 input
= error_mark_node
;
9021 input
= error_mark_node
;
9023 TREE_VALUE (tail
) = input
;
9026 /* ASMs with labels cannot have outputs. This should have been
9027 enforced by the parser. */
9028 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9030 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9032 /* asm statements without outputs, including simple ones, are treated
9034 ASM_INPUT_P (args
) = simple
;
9035 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9040 /* Generate a goto statement to LABEL. LOC is the location of the
9044 c_finish_goto_label (location_t loc
, tree label
)
9046 tree decl
= lookup_label_for_goto (loc
, label
);
9049 TREE_USED (decl
) = 1;
9051 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9052 SET_EXPR_LOCATION (t
, loc
);
9053 return add_stmt (t
);
9057 /* Generate a computed goto statement to EXPR. LOC is the location of
9061 c_finish_goto_ptr (location_t loc
, tree expr
)
9064 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9065 expr
= c_fully_fold (expr
, false, NULL
);
9066 expr
= convert (ptr_type_node
, expr
);
9067 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9068 SET_EXPR_LOCATION (t
, loc
);
9069 return add_stmt (t
);
9072 /* Generate a C `return' statement. RETVAL is the expression for what
9073 to return, or a null pointer for `return;' with no value. LOC is
9074 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9075 is the original type of RETVAL. */
9078 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9080 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9081 bool no_warning
= false;
9085 if (TREE_THIS_VOLATILE (current_function_decl
))
9087 "function declared %<noreturn%> has a %<return%> statement");
9089 if (flag_enable_cilkplus
&& contains_array_notation_expr (retval
))
9091 /* Array notations are allowed in a return statement if it is inside a
9092 built-in array notation reduction function. */
9093 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9094 return error_mark_node
;
9097 error_at (loc
, "array notation expression cannot be used as a "
9099 return error_mark_node
;
9102 if (flag_enable_cilkplus
&& retval
&& TREE_CODE (retval
) == CILK_SPAWN_STMT
)
9104 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9106 return error_mark_node
;
9110 tree semantic_type
= NULL_TREE
;
9111 npc
= null_pointer_constant_p (retval
);
9112 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9114 semantic_type
= TREE_TYPE (retval
);
9115 retval
= TREE_OPERAND (retval
, 0);
9117 retval
= c_fully_fold (retval
, false, NULL
);
9119 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9124 current_function_returns_null
= 1;
9125 if ((warn_return_type
|| flag_isoc99
)
9126 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9128 pedwarn_c99 (loc
, flag_isoc99
? 0 : OPT_Wreturn_type
,
9129 "%<return%> with no value, in "
9130 "function returning non-void");
9134 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9136 current_function_returns_null
= 1;
9137 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9139 "%<return%> with a value, in function returning void");
9141 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
9142 "%<return%> with expression, in function returning void");
9146 tree t
= convert_for_assignment (loc
, valtype
, retval
, origtype
,
9148 npc
, NULL_TREE
, NULL_TREE
, 0);
9149 tree res
= DECL_RESULT (current_function_decl
);
9153 current_function_returns_value
= 1;
9154 if (t
== error_mark_node
)
9157 save
= in_late_binary_op
;
9158 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9159 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
)
9160 in_late_binary_op
= true;
9161 inner
= t
= convert (TREE_TYPE (res
), t
);
9162 in_late_binary_op
= save
;
9164 /* Strip any conversions, additions, and subtractions, and see if
9165 we are returning the address of a local variable. Warn if so. */
9168 switch (TREE_CODE (inner
))
9171 case NON_LVALUE_EXPR
:
9173 case POINTER_PLUS_EXPR
:
9174 inner
= TREE_OPERAND (inner
, 0);
9178 /* If the second operand of the MINUS_EXPR has a pointer
9179 type (or is converted from it), this may be valid, so
9180 don't give a warning. */
9182 tree op1
= TREE_OPERAND (inner
, 1);
9184 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9185 && (CONVERT_EXPR_P (op1
)
9186 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9187 op1
= TREE_OPERAND (op1
, 0);
9189 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9192 inner
= TREE_OPERAND (inner
, 0);
9197 inner
= TREE_OPERAND (inner
, 0);
9199 while (REFERENCE_CLASS_P (inner
)
9200 && TREE_CODE (inner
) != INDIRECT_REF
)
9201 inner
= TREE_OPERAND (inner
, 0);
9204 && !DECL_EXTERNAL (inner
)
9205 && !TREE_STATIC (inner
)
9206 && DECL_CONTEXT (inner
) == current_function_decl
)
9208 OPT_Wreturn_local_addr
, "function returns address "
9209 "of local variable");
9219 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9220 SET_EXPR_LOCATION (retval
, loc
);
9222 if (warn_sequence_point
)
9223 verify_sequence_points (retval
);
9226 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9227 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9228 return add_stmt (ret_stmt
);
9232 /* The SWITCH_EXPR being built. */
9235 /* The original type of the testing expression, i.e. before the
9236 default conversion is applied. */
9239 /* A splay-tree mapping the low element of a case range to the high
9240 element, or NULL_TREE if there is no high element. Used to
9241 determine whether or not a new case label duplicates an old case
9242 label. We need a tree, rather than simply a hash table, because
9243 of the GNU case range extension. */
9246 /* The bindings at the point of the switch. This is used for
9247 warnings crossing decls when branching to a case label. */
9248 struct c_spot_bindings
*bindings
;
9250 /* The next node on the stack. */
9251 struct c_switch
*next
;
9254 /* A stack of the currently active switch statements. The innermost
9255 switch statement is on the top of the stack. There is no need to
9256 mark the stack for garbage collection because it is only active
9257 during the processing of the body of a function, and we never
9258 collect at that point. */
9260 struct c_switch
*c_switch_stack
;
9262 /* Start a C switch statement, testing expression EXP. Return the new
9263 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9264 SWITCH_COND_LOC is the location of the switch's condition. */
9267 c_start_case (location_t switch_loc
,
9268 location_t switch_cond_loc
,
9271 tree orig_type
= error_mark_node
;
9272 struct c_switch
*cs
;
9274 if (exp
!= error_mark_node
)
9276 orig_type
= TREE_TYPE (exp
);
9278 if (!INTEGRAL_TYPE_P (orig_type
))
9280 if (orig_type
!= error_mark_node
)
9282 error_at (switch_cond_loc
, "switch quantity not an integer");
9283 orig_type
= error_mark_node
;
9285 exp
= integer_zero_node
;
9289 tree type
= TYPE_MAIN_VARIANT (orig_type
);
9291 if (!in_system_header
9292 && (type
== long_integer_type_node
9293 || type
== long_unsigned_type_node
))
9294 warning_at (switch_cond_loc
,
9295 OPT_Wtraditional
, "%<long%> switch expression not "
9296 "converted to %<int%> in ISO C");
9298 exp
= c_fully_fold (exp
, false, NULL
);
9299 exp
= default_conversion (exp
);
9301 if (warn_sequence_point
)
9302 verify_sequence_points (exp
);
9306 /* Add this new SWITCH_EXPR to the stack. */
9307 cs
= XNEW (struct c_switch
);
9308 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
9309 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
9310 cs
->orig_type
= orig_type
;
9311 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
9312 cs
->bindings
= c_get_switch_bindings ();
9313 cs
->next
= c_switch_stack
;
9314 c_switch_stack
= cs
;
9316 return add_stmt (cs
->switch_expr
);
9319 /* Process a case label at location LOC. */
9322 do_case (location_t loc
, tree low_value
, tree high_value
)
9324 tree label
= NULL_TREE
;
9326 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
9328 low_value
= c_fully_fold (low_value
, false, NULL
);
9329 if (TREE_CODE (low_value
) == INTEGER_CST
)
9330 pedwarn (input_location
, OPT_Wpedantic
,
9331 "case label is not an integer constant expression");
9334 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
9336 high_value
= c_fully_fold (high_value
, false, NULL
);
9337 if (TREE_CODE (high_value
) == INTEGER_CST
)
9338 pedwarn (input_location
, OPT_Wpedantic
,
9339 "case label is not an integer constant expression");
9342 if (c_switch_stack
== NULL
)
9345 error_at (loc
, "case label not within a switch statement");
9347 error_at (loc
, "%<default%> label not within a switch statement");
9351 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
9352 EXPR_LOCATION (c_switch_stack
->switch_expr
),
9356 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
9357 SWITCH_COND (c_switch_stack
->switch_expr
),
9358 c_switch_stack
->orig_type
,
9359 low_value
, high_value
);
9360 if (label
== error_mark_node
)
9365 /* Finish the switch statement. */
9368 c_finish_case (tree body
)
9370 struct c_switch
*cs
= c_switch_stack
;
9371 location_t switch_location
;
9373 SWITCH_BODY (cs
->switch_expr
) = body
;
9375 /* Emit warnings as needed. */
9376 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
9377 c_do_switch_warnings (cs
->cases
, switch_location
,
9378 TREE_TYPE (cs
->switch_expr
),
9379 SWITCH_COND (cs
->switch_expr
));
9381 /* Pop the stack. */
9382 c_switch_stack
= cs
->next
;
9383 splay_tree_delete (cs
->cases
);
9384 c_release_switch_bindings (cs
->bindings
);
9388 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9389 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9390 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9391 statement, and was not surrounded with parenthesis. */
9394 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
9395 tree else_block
, bool nested_if
)
9399 /* If the condition has array notations, then the rank of the then_block and
9400 else_block must be either 0 or be equal to the rank of the condition. If
9401 the condition does not have array notations then break them up as it is
9402 broken up in a normal expression. */
9403 if (flag_enable_cilkplus
&& contains_array_notation_expr (cond
))
9405 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
9406 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
9409 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
9412 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
9414 if (cond_rank
!= then_rank
&& then_rank
!= 0)
9416 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9417 " and the then-block");
9420 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
9422 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9423 " and the else-block");
9427 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9428 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
9430 tree inner_if
= then_block
;
9432 /* We know from the grammar productions that there is an IF nested
9433 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9434 it might not be exactly THEN_BLOCK, but should be the last
9435 non-container statement within. */
9437 switch (TREE_CODE (inner_if
))
9442 inner_if
= BIND_EXPR_BODY (inner_if
);
9444 case STATEMENT_LIST
:
9445 inner_if
= expr_last (then_block
);
9447 case TRY_FINALLY_EXPR
:
9448 case TRY_CATCH_EXPR
:
9449 inner_if
= TREE_OPERAND (inner_if
, 0);
9456 if (COND_EXPR_ELSE (inner_if
))
9457 warning_at (if_locus
, OPT_Wparentheses
,
9458 "suggest explicit braces to avoid ambiguous %<else%>");
9461 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
9462 SET_EXPR_LOCATION (stmt
, if_locus
);
9466 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9467 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9468 is false for DO loops. INCR is the FOR increment expression. BODY is
9469 the statement controlled by the loop. BLAB is the break label. CLAB is
9470 the continue label. Everything is allowed to be NULL. */
9473 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
9474 tree blab
, tree clab
, bool cond_is_first
)
9476 tree entry
= NULL
, exit
= NULL
, t
;
9478 if (flag_enable_cilkplus
&& contains_array_notation_expr (cond
))
9480 error_at (start_locus
, "array notation expression cannot be used in a "
9481 "loop%'s condition");
9485 /* If the condition is zero don't generate a loop construct. */
9486 if (cond
&& integer_zerop (cond
))
9490 t
= build_and_jump (&blab
);
9491 SET_EXPR_LOCATION (t
, start_locus
);
9497 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9499 /* If we have an exit condition, then we build an IF with gotos either
9500 out of the loop, or to the top of it. If there's no exit condition,
9501 then we just build a jump back to the top. */
9502 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
9504 if (cond
&& !integer_nonzerop (cond
))
9506 /* Canonicalize the loop condition to the end. This means
9507 generating a branch to the loop condition. Reuse the
9508 continue label, if possible. */
9513 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9514 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
9517 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
9518 SET_EXPR_LOCATION (t
, start_locus
);
9522 t
= build_and_jump (&blab
);
9524 exit
= fold_build3_loc (start_locus
,
9525 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9527 exit
= fold_build3_loc (input_location
,
9528 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9537 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
9545 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
9549 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
9552 tree label
= *label_p
;
9554 /* In switch statements break is sometimes stylistically used after
9555 a return statement. This can lead to spurious warnings about
9556 control reaching the end of a non-void function when it is
9557 inlined. Note that we are calling block_may_fallthru with
9558 language specific tree nodes; this works because
9559 block_may_fallthru returns true when given something it does not
9561 skip
= !block_may_fallthru (cur_stmt_list
);
9566 *label_p
= label
= create_artificial_label (loc
);
9568 else if (TREE_CODE (label
) == LABEL_DECL
)
9570 else switch (TREE_INT_CST_LOW (label
))
9574 error_at (loc
, "break statement not within loop or switch");
9576 error_at (loc
, "continue statement not within a loop");
9580 gcc_assert (is_break
);
9581 error_at (loc
, "break statement used with OpenMP for loop");
9586 error ("break statement within %<#pragma simd%> loop body");
9588 error ("continue statement within %<#pragma simd%> loop body");
9599 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
9601 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
9604 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9607 emit_side_effect_warnings (location_t loc
, tree expr
)
9609 if (expr
== error_mark_node
)
9611 else if (!TREE_SIDE_EFFECTS (expr
))
9613 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
9614 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
9617 warn_if_unused_value (expr
, loc
);
9620 /* Process an expression as if it were a complete statement. Emit
9621 diagnostics, but do not call ADD_STMT. LOC is the location of the
9625 c_process_expr_stmt (location_t loc
, tree expr
)
9632 expr
= c_fully_fold (expr
, false, NULL
);
9634 if (warn_sequence_point
)
9635 verify_sequence_points (expr
);
9637 if (TREE_TYPE (expr
) != error_mark_node
9638 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
9639 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
9640 error_at (loc
, "expression statement has incomplete type");
9642 /* If we're not processing a statement expression, warn about unused values.
9643 Warnings for statement expressions will be emitted later, once we figure
9644 out which is the result. */
9645 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9646 && warn_unused_value
)
9647 emit_side_effect_warnings (loc
, expr
);
9650 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
9651 exprv
= TREE_OPERAND (exprv
, 1);
9652 while (CONVERT_EXPR_P (exprv
))
9653 exprv
= TREE_OPERAND (exprv
, 0);
9655 || handled_component_p (exprv
)
9656 || TREE_CODE (exprv
) == ADDR_EXPR
)
9657 mark_exp_read (exprv
);
9659 /* If the expression is not of a type to which we cannot assign a line
9660 number, wrap the thing in a no-op NOP_EXPR. */
9661 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
9663 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
9664 SET_EXPR_LOCATION (expr
, loc
);
9670 /* Emit an expression as a statement. LOC is the location of the
9674 c_finish_expr_stmt (location_t loc
, tree expr
)
9677 return add_stmt (c_process_expr_stmt (loc
, expr
));
9682 /* Do the opposite and emit a statement as an expression. To begin,
9683 create a new binding level and return it. */
9686 c_begin_stmt_expr (void)
9690 /* We must force a BLOCK for this level so that, if it is not expanded
9691 later, there is a way to turn off the entire subtree of blocks that
9692 are contained in it. */
9694 ret
= c_begin_compound_stmt (true);
9696 c_bindings_start_stmt_expr (c_switch_stack
== NULL
9698 : c_switch_stack
->bindings
);
9700 /* Mark the current statement list as belonging to a statement list. */
9701 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
9706 /* LOC is the location of the compound statement to which this body
9710 c_finish_stmt_expr (location_t loc
, tree body
)
9712 tree last
, type
, tmp
, val
;
9715 body
= c_end_compound_stmt (loc
, body
, true);
9717 c_bindings_end_stmt_expr (c_switch_stack
== NULL
9719 : c_switch_stack
->bindings
);
9721 /* Locate the last statement in BODY. See c_end_compound_stmt
9722 about always returning a BIND_EXPR. */
9723 last_p
= &BIND_EXPR_BODY (body
);
9724 last
= BIND_EXPR_BODY (body
);
9727 if (TREE_CODE (last
) == STATEMENT_LIST
)
9729 tree_stmt_iterator i
;
9731 /* This can happen with degenerate cases like ({ }). No value. */
9732 if (!TREE_SIDE_EFFECTS (last
))
9735 /* If we're supposed to generate side effects warnings, process
9736 all of the statements except the last. */
9737 if (warn_unused_value
)
9739 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
9742 tree t
= tsi_stmt (i
);
9744 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
9745 emit_side_effect_warnings (tloc
, t
);
9749 i
= tsi_last (last
);
9750 last_p
= tsi_stmt_ptr (i
);
9754 /* If the end of the list is exception related, then the list was split
9755 by a call to push_cleanup. Continue searching. */
9756 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
9757 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
9759 last_p
= &TREE_OPERAND (last
, 0);
9761 goto continue_searching
;
9764 if (last
== error_mark_node
)
9767 /* In the case that the BIND_EXPR is not necessary, return the
9768 expression out from inside it. */
9769 if (last
== BIND_EXPR_BODY (body
)
9770 && BIND_EXPR_VARS (body
) == NULL
)
9772 /* Even if this looks constant, do not allow it in a constant
9774 last
= c_wrap_maybe_const (last
, true);
9775 /* Do not warn if the return value of a statement expression is
9777 TREE_NO_WARNING (last
) = 1;
9781 /* Extract the type of said expression. */
9782 type
= TREE_TYPE (last
);
9784 /* If we're not returning a value at all, then the BIND_EXPR that
9785 we already have is a fine expression to return. */
9786 if (!type
|| VOID_TYPE_P (type
))
9789 /* Now that we've located the expression containing the value, it seems
9790 silly to make voidify_wrapper_expr repeat the process. Create a
9791 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9792 tmp
= create_tmp_var_raw (type
, NULL
);
9794 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9795 tree_expr_nonnegative_p giving up immediately. */
9797 if (TREE_CODE (val
) == NOP_EXPR
9798 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
9799 val
= TREE_OPERAND (val
, 0);
9801 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
9802 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
9805 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
9806 SET_EXPR_LOCATION (t
, loc
);
9811 /* Begin and end compound statements. This is as simple as pushing
9812 and popping new statement lists from the tree. */
9815 c_begin_compound_stmt (bool do_scope
)
9817 tree stmt
= push_stmt_list ();
9823 /* End a compound statement. STMT is the statement. LOC is the
9824 location of the compound statement-- this is usually the location
9825 of the opening brace. */
9828 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
9834 if (c_dialect_objc ())
9835 objc_clear_super_receiver ();
9836 block
= pop_scope ();
9839 stmt
= pop_stmt_list (stmt
);
9840 stmt
= c_build_bind_expr (loc
, block
, stmt
);
9842 /* If this compound statement is nested immediately inside a statement
9843 expression, then force a BIND_EXPR to be created. Otherwise we'll
9844 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9845 STATEMENT_LISTs merge, and thus we can lose track of what statement
9847 if (building_stmt_list_p ()
9848 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9849 && TREE_CODE (stmt
) != BIND_EXPR
)
9851 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
9852 TREE_SIDE_EFFECTS (stmt
) = 1;
9853 SET_EXPR_LOCATION (stmt
, loc
);
9859 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9860 when the current scope is exited. EH_ONLY is true when this is not
9861 meant to apply to normal control flow transfer. */
9864 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
9866 enum tree_code code
;
9870 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
9871 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
9873 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
9874 list
= push_stmt_list ();
9875 TREE_OPERAND (stmt
, 0) = list
;
9876 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
9879 /* Build a binary-operation expression without default conversions.
9880 CODE is the kind of expression to build.
9881 LOCATION is the operator's location.
9882 This function differs from `build' in several ways:
9883 the data type of the result is computed and recorded in it,
9884 warnings are generated if arg data types are invalid,
9885 special handling for addition and subtraction of pointers is known,
9886 and some optimization is done (operations on narrow ints
9887 are done in the narrower type when that gives the same result).
9888 Constant folding is also done before the result is returned.
9890 Note that the operands will never have enumeral types, or function
9891 or array types, because either they will have the default conversions
9892 performed or they have both just been converted to some other type in which
9893 the arithmetic is to be done. */
9896 build_binary_op (location_t location
, enum tree_code code
,
9897 tree orig_op0
, tree orig_op1
, int convert_p
)
9899 tree type0
, type1
, orig_type0
, orig_type1
;
9901 enum tree_code code0
, code1
;
9903 tree ret
= error_mark_node
;
9904 const char *invalid_op_diag
;
9905 bool op0_int_operands
, op1_int_operands
;
9906 bool int_const
, int_const_or_overflow
, int_operands
;
9908 /* Expression code to give to the expression when it is built.
9909 Normally this is CODE, which is what the caller asked for,
9910 but in some special cases we change it. */
9911 enum tree_code resultcode
= code
;
9913 /* Data type in which the computation is to be performed.
9914 In the simplest cases this is the common type of the arguments. */
9915 tree result_type
= NULL
;
9917 /* When the computation is in excess precision, the type of the
9918 final EXCESS_PRECISION_EXPR. */
9919 tree semantic_result_type
= NULL
;
9921 /* Nonzero means operands have already been type-converted
9922 in whatever way is necessary.
9923 Zero means they need to be converted to RESULT_TYPE. */
9926 /* Nonzero means create the expression with this type, rather than
9928 tree build_type
= 0;
9930 /* Nonzero means after finally constructing the expression
9931 convert it to this type. */
9932 tree final_type
= 0;
9934 /* Nonzero if this is an operation like MIN or MAX which can
9935 safely be computed in short if both args are promoted shorts.
9936 Also implies COMMON.
9937 -1 indicates a bitwise operation; this makes a difference
9938 in the exact conditions for when it is safe to do the operation
9939 in a narrower mode. */
9942 /* Nonzero if this is a comparison operation;
9943 if both args are promoted shorts, compare the original shorts.
9944 Also implies COMMON. */
9945 int short_compare
= 0;
9947 /* Nonzero if this is a right-shift operation, which can be computed on the
9948 original short and then promoted if the operand is a promoted short. */
9949 int short_shift
= 0;
9951 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9954 /* True means types are compatible as far as ObjC is concerned. */
9957 /* True means this is an arithmetic operation that may need excess
9959 bool may_need_excess_precision
;
9961 /* True means this is a boolean operation that converts both its
9962 operands to truth-values. */
9963 bool boolean_op
= false;
9965 /* Remember whether we're doing / or %. */
9966 bool doing_div_or_mod
= false;
9968 /* Remember whether we're doing << or >>. */
9969 bool doing_shift
= false;
9971 /* Tree holding instrumentation expression. */
9972 tree instrument_expr
= NULL
;
9974 if (location
== UNKNOWN_LOCATION
)
9975 location
= input_location
;
9980 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
9981 if (op0_int_operands
)
9982 op0
= remove_c_maybe_const_expr (op0
);
9983 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
9984 if (op1_int_operands
)
9985 op1
= remove_c_maybe_const_expr (op1
);
9986 int_operands
= (op0_int_operands
&& op1_int_operands
);
9989 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
9990 && TREE_CODE (orig_op1
) == INTEGER_CST
);
9991 int_const
= (int_const_or_overflow
9992 && !TREE_OVERFLOW (orig_op0
)
9993 && !TREE_OVERFLOW (orig_op1
));
9996 int_const
= int_const_or_overflow
= false;
9998 /* Do not apply default conversion in mixed vector/scalar expression. */
10000 && !((TREE_CODE (TREE_TYPE (op0
)) == VECTOR_TYPE
)
10001 != (TREE_CODE (TREE_TYPE (op1
)) == VECTOR_TYPE
)))
10003 op0
= default_conversion (op0
);
10004 op1
= default_conversion (op1
);
10007 /* When Cilk Plus is enabled and there are array notations inside op0, then
10008 we check to see if there are builtin array notation functions. If
10009 so, then we take on the type of the array notation inside it. */
10010 if (flag_enable_cilkplus
&& contains_array_notation_expr (op0
))
10011 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10013 orig_type0
= type0
= TREE_TYPE (op0
);
10015 if (flag_enable_cilkplus
&& contains_array_notation_expr (op1
))
10016 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10018 orig_type1
= type1
= TREE_TYPE (op1
);
10020 /* The expression codes of the data types of the arguments tell us
10021 whether the arguments are integers, floating, pointers, etc. */
10022 code0
= TREE_CODE (type0
);
10023 code1
= TREE_CODE (type1
);
10025 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10026 STRIP_TYPE_NOPS (op0
);
10027 STRIP_TYPE_NOPS (op1
);
10029 /* If an error was already reported for one of the arguments,
10030 avoid reporting another error. */
10032 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10033 return error_mark_node
;
10035 if ((invalid_op_diag
10036 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10038 error_at (location
, invalid_op_diag
);
10039 return error_mark_node
;
10047 case TRUNC_DIV_EXPR
:
10048 case CEIL_DIV_EXPR
:
10049 case FLOOR_DIV_EXPR
:
10050 case ROUND_DIV_EXPR
:
10051 case EXACT_DIV_EXPR
:
10052 may_need_excess_precision
= true;
10055 may_need_excess_precision
= false;
10058 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10060 op0
= TREE_OPERAND (op0
, 0);
10061 type0
= TREE_TYPE (op0
);
10063 else if (may_need_excess_precision
10064 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10067 op0
= convert (eptype
, op0
);
10069 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10071 op1
= TREE_OPERAND (op1
, 0);
10072 type1
= TREE_TYPE (op1
);
10074 else if (may_need_excess_precision
10075 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10078 op1
= convert (eptype
, op1
);
10081 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10083 /* In case when one of the operands of the binary operation is
10084 a vector and another is a scalar -- convert scalar to vector. */
10085 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10087 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10090 switch (convert_flag
)
10093 return error_mark_node
;
10096 bool maybe_const
= true;
10098 sc
= c_fully_fold (op0
, false, &maybe_const
);
10099 sc
= save_expr (sc
);
10100 sc
= convert (TREE_TYPE (type1
), sc
);
10101 op0
= build_vector_from_val (type1
, sc
);
10103 op0
= c_wrap_maybe_const (op0
, true);
10104 orig_type0
= type0
= TREE_TYPE (op0
);
10105 code0
= TREE_CODE (type0
);
10109 case stv_secondarg
:
10111 bool maybe_const
= true;
10113 sc
= c_fully_fold (op1
, false, &maybe_const
);
10114 sc
= save_expr (sc
);
10115 sc
= convert (TREE_TYPE (type0
), sc
);
10116 op1
= build_vector_from_val (type0
, sc
);
10118 op1
= c_wrap_maybe_const (op1
, true);
10119 orig_type1
= type1
= TREE_TYPE (op1
);
10120 code1
= TREE_CODE (type1
);
10132 /* Handle the pointer + int case. */
10133 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10135 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10136 goto return_build_binary_op
;
10138 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10140 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10141 goto return_build_binary_op
;
10148 /* Subtraction of two similar pointers.
10149 We must subtract them as integers, then divide by object size. */
10150 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10151 && comp_target_types (location
, type0
, type1
))
10153 ret
= pointer_diff (location
, op0
, op1
);
10154 goto return_build_binary_op
;
10156 /* Handle pointer minus int. Just like pointer plus int. */
10157 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10159 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10160 goto return_build_binary_op
;
10170 case TRUNC_DIV_EXPR
:
10171 case CEIL_DIV_EXPR
:
10172 case FLOOR_DIV_EXPR
:
10173 case ROUND_DIV_EXPR
:
10174 case EXACT_DIV_EXPR
:
10175 doing_div_or_mod
= true;
10176 warn_for_div_by_zero (location
, op1
);
10178 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10179 || code0
== FIXED_POINT_TYPE
10180 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10181 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10182 || code1
== FIXED_POINT_TYPE
10183 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10185 enum tree_code tcode0
= code0
, tcode1
= code1
;
10187 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10188 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10189 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10190 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10192 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10193 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10194 resultcode
= RDIV_EXPR
;
10196 /* Although it would be tempting to shorten always here, that
10197 loses on some targets, since the modulo instruction is
10198 undefined if the quotient can't be represented in the
10199 computation mode. We shorten only if unsigned or if
10200 dividing by something we know != -1. */
10201 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10202 || (TREE_CODE (op1
) == INTEGER_CST
10203 && !integer_all_onesp (op1
)));
10211 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10213 /* Allow vector types which are not floating point types. */
10214 else if (code0
== VECTOR_TYPE
10215 && code1
== VECTOR_TYPE
10216 && !VECTOR_FLOAT_TYPE_P (type0
)
10217 && !VECTOR_FLOAT_TYPE_P (type1
))
10221 case TRUNC_MOD_EXPR
:
10222 case FLOOR_MOD_EXPR
:
10223 doing_div_or_mod
= true;
10224 warn_for_div_by_zero (location
, op1
);
10226 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10227 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10228 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10230 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10232 /* Although it would be tempting to shorten always here, that loses
10233 on some targets, since the modulo instruction is undefined if the
10234 quotient can't be represented in the computation mode. We shorten
10235 only if unsigned or if dividing by something we know != -1. */
10236 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10237 || (TREE_CODE (op1
) == INTEGER_CST
10238 && !integer_all_onesp (op1
)));
10243 case TRUTH_ANDIF_EXPR
:
10244 case TRUTH_ORIF_EXPR
:
10245 case TRUTH_AND_EXPR
:
10246 case TRUTH_OR_EXPR
:
10247 case TRUTH_XOR_EXPR
:
10248 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
10249 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10250 || code0
== FIXED_POINT_TYPE
)
10251 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
10252 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10253 || code1
== FIXED_POINT_TYPE
))
10255 /* Result of these operations is always an int,
10256 but that does not mean the operands should be
10257 converted to ints! */
10258 result_type
= integer_type_node
;
10259 if (op0_int_operands
)
10261 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
10262 op0
= remove_c_maybe_const_expr (op0
);
10265 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
10266 if (op1_int_operands
)
10268 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
10269 op1
= remove_c_maybe_const_expr (op1
);
10272 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
10276 if (code
== TRUTH_ANDIF_EXPR
)
10278 int_const_or_overflow
= (int_operands
10279 && TREE_CODE (orig_op0
) == INTEGER_CST
10280 && (op0
== truthvalue_false_node
10281 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10282 int_const
= (int_const_or_overflow
10283 && !TREE_OVERFLOW (orig_op0
)
10284 && (op0
== truthvalue_false_node
10285 || !TREE_OVERFLOW (orig_op1
)));
10287 else if (code
== TRUTH_ORIF_EXPR
)
10289 int_const_or_overflow
= (int_operands
10290 && TREE_CODE (orig_op0
) == INTEGER_CST
10291 && (op0
== truthvalue_true_node
10292 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10293 int_const
= (int_const_or_overflow
10294 && !TREE_OVERFLOW (orig_op0
)
10295 && (op0
== truthvalue_true_node
10296 || !TREE_OVERFLOW (orig_op1
)));
10300 /* Shift operations: result has same type as first operand;
10301 always convert second operand to int.
10302 Also set SHORT_SHIFT if shifting rightward. */
10305 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10306 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10308 result_type
= type0
;
10311 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10312 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10313 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10314 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10316 result_type
= type0
;
10319 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10320 && code1
== INTEGER_TYPE
)
10322 doing_shift
= true;
10323 if (TREE_CODE (op1
) == INTEGER_CST
)
10325 if (tree_int_cst_sgn (op1
) < 0)
10328 if (c_inhibit_evaluation_warnings
== 0)
10329 warning (0, "right shift count is negative");
10333 if (!integer_zerop (op1
))
10336 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10339 if (c_inhibit_evaluation_warnings
== 0)
10340 warning (0, "right shift count >= width of type");
10345 /* Use the type of the value to be shifted. */
10346 result_type
= type0
;
10347 /* Convert the non vector shift-count to an integer, regardless
10348 of size of value being shifted. */
10349 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10350 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10351 op1
= convert (integer_type_node
, op1
);
10352 /* Avoid converting op1 to result_type later. */
10358 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10359 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10361 result_type
= type0
;
10364 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10365 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10366 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10367 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10369 result_type
= type0
;
10372 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10373 && code1
== INTEGER_TYPE
)
10375 doing_shift
= true;
10376 if (TREE_CODE (op1
) == INTEGER_CST
)
10378 if (tree_int_cst_sgn (op1
) < 0)
10381 if (c_inhibit_evaluation_warnings
== 0)
10382 warning (0, "left shift count is negative");
10385 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10388 if (c_inhibit_evaluation_warnings
== 0)
10389 warning (0, "left shift count >= width of type");
10393 /* Use the type of the value to be shifted. */
10394 result_type
= type0
;
10395 /* Convert the non vector shift-count to an integer, regardless
10396 of size of value being shifted. */
10397 if (TREE_CODE (TREE_TYPE (op1
)) != VECTOR_TYPE
10398 && TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
10399 op1
= convert (integer_type_node
, op1
);
10400 /* Avoid converting op1 to result_type later. */
10407 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10410 if (!vector_types_compatible_elements_p (type0
, type1
))
10412 error_at (location
, "comparing vectors with different "
10414 return error_mark_node
;
10417 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10419 error_at (location
, "comparing vectors with different "
10420 "number of elements");
10421 return error_mark_node
;
10424 /* Always construct signed integer vector type. */
10425 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10426 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10427 result_type
= build_opaque_vector_type (intt
,
10428 TYPE_VECTOR_SUBPARTS (type0
));
10432 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
10433 warning_at (location
,
10435 "comparing floating point with == or != is unsafe");
10436 /* Result of comparison is always int,
10437 but don't convert the args to int! */
10438 build_type
= integer_type_node
;
10439 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10440 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
10441 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10442 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
10444 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10446 if (TREE_CODE (op0
) == ADDR_EXPR
10447 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
10449 if (code
== EQ_EXPR
)
10450 warning_at (location
,
10452 "the comparison will always evaluate as %<false%> "
10453 "for the address of %qD will never be NULL",
10454 TREE_OPERAND (op0
, 0));
10456 warning_at (location
,
10458 "the comparison will always evaluate as %<true%> "
10459 "for the address of %qD will never be NULL",
10460 TREE_OPERAND (op0
, 0));
10462 result_type
= type0
;
10464 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10466 if (TREE_CODE (op1
) == ADDR_EXPR
10467 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
10469 if (code
== EQ_EXPR
)
10470 warning_at (location
,
10472 "the comparison will always evaluate as %<false%> "
10473 "for the address of %qD will never be NULL",
10474 TREE_OPERAND (op1
, 0));
10476 warning_at (location
,
10478 "the comparison will always evaluate as %<true%> "
10479 "for the address of %qD will never be NULL",
10480 TREE_OPERAND (op1
, 0));
10482 result_type
= type1
;
10484 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10486 tree tt0
= TREE_TYPE (type0
);
10487 tree tt1
= TREE_TYPE (type1
);
10488 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
10489 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
10490 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
10492 /* Anything compares with void *. void * compares with anything.
10493 Otherwise, the targets must be compatible
10494 and both must be object or both incomplete. */
10495 if (comp_target_types (location
, type0
, type1
))
10496 result_type
= common_pointer_type (type0
, type1
);
10497 else if (!addr_space_superset (as0
, as1
, &as_common
))
10499 error_at (location
, "comparison of pointers to "
10500 "disjoint address spaces");
10501 return error_mark_node
;
10503 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
10505 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
10506 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10507 "comparison of %<void *%> with function pointer");
10509 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
10511 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
10512 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10513 "comparison of %<void *%> with function pointer");
10516 /* Avoid warning about the volatile ObjC EH puts on decls. */
10518 pedwarn (location
, 0,
10519 "comparison of distinct pointer types lacks a cast");
10521 if (result_type
== NULL_TREE
)
10523 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10524 result_type
= build_pointer_type
10525 (build_qualified_type (void_type_node
, qual
));
10528 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10530 result_type
= type0
;
10531 pedwarn (location
, 0, "comparison between pointer and integer");
10533 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10535 result_type
= type1
;
10536 pedwarn (location
, 0, "comparison between pointer and integer");
10544 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10547 if (!vector_types_compatible_elements_p (type0
, type1
))
10549 error_at (location
, "comparing vectors with different "
10551 return error_mark_node
;
10554 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10556 error_at (location
, "comparing vectors with different "
10557 "number of elements");
10558 return error_mark_node
;
10561 /* Always construct signed integer vector type. */
10562 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10563 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10564 result_type
= build_opaque_vector_type (intt
,
10565 TYPE_VECTOR_SUBPARTS (type0
));
10569 build_type
= integer_type_node
;
10570 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10571 || code0
== FIXED_POINT_TYPE
)
10572 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10573 || code1
== FIXED_POINT_TYPE
))
10575 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10577 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
10578 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
10579 addr_space_t as_common
;
10581 if (comp_target_types (location
, type0
, type1
))
10583 result_type
= common_pointer_type (type0
, type1
);
10584 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
10585 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
10586 pedwarn (location
, 0,
10587 "comparison of complete and incomplete pointers");
10588 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
10589 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10590 "ordered comparisons of pointers to functions");
10591 else if (null_pointer_constant_p (orig_op0
)
10592 || null_pointer_constant_p (orig_op1
))
10593 warning_at (location
, OPT_Wextra
,
10594 "ordered comparison of pointer with null pointer");
10597 else if (!addr_space_superset (as0
, as1
, &as_common
))
10599 error_at (location
, "comparison of pointers to "
10600 "disjoint address spaces");
10601 return error_mark_node
;
10605 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10606 result_type
= build_pointer_type
10607 (build_qualified_type (void_type_node
, qual
));
10608 pedwarn (location
, 0,
10609 "comparison of distinct pointer types lacks a cast");
10612 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10614 result_type
= type0
;
10616 pedwarn (location
, OPT_Wpedantic
,
10617 "ordered comparison of pointer with integer zero");
10618 else if (extra_warnings
)
10619 warning_at (location
, OPT_Wextra
,
10620 "ordered comparison of pointer with integer zero");
10622 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10624 result_type
= type1
;
10626 pedwarn (location
, OPT_Wpedantic
,
10627 "ordered comparison of pointer with integer zero");
10628 else if (extra_warnings
)
10629 warning_at (location
, OPT_Wextra
,
10630 "ordered comparison of pointer with integer zero");
10632 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10634 result_type
= type0
;
10635 pedwarn (location
, 0, "comparison between pointer and integer");
10637 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10639 result_type
= type1
;
10640 pedwarn (location
, 0, "comparison between pointer and integer");
10645 gcc_unreachable ();
10648 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10649 return error_mark_node
;
10651 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10652 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
10653 || !vector_types_compatible_elements_p (type0
, type1
)))
10655 binary_op_error (location
, code
, type0
, type1
);
10656 return error_mark_node
;
10659 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10660 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
10662 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10663 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
10665 bool first_complex
= (code0
== COMPLEX_TYPE
);
10666 bool second_complex
= (code1
== COMPLEX_TYPE
);
10667 int none_complex
= (!first_complex
&& !second_complex
);
10669 if (shorten
|| common
|| short_compare
)
10671 result_type
= c_common_type (type0
, type1
);
10672 do_warn_double_promotion (result_type
, type0
, type1
,
10673 "implicit conversion from %qT to %qT "
10674 "to match other operand of binary "
10677 if (result_type
== error_mark_node
)
10678 return error_mark_node
;
10681 if (first_complex
!= second_complex
10682 && (code
== PLUS_EXPR
10683 || code
== MINUS_EXPR
10684 || code
== MULT_EXPR
10685 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
10686 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
10687 && flag_signed_zeros
)
10689 /* An operation on mixed real/complex operands must be
10690 handled specially, but the language-independent code can
10691 more easily optimize the plain complex arithmetic if
10692 -fno-signed-zeros. */
10693 tree real_type
= TREE_TYPE (result_type
);
10695 if (type0
!= orig_type0
|| type1
!= orig_type1
)
10697 gcc_assert (may_need_excess_precision
&& common
);
10698 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
10702 if (TREE_TYPE (op0
) != result_type
)
10703 op0
= convert_and_check (result_type
, op0
);
10704 if (TREE_TYPE (op1
) != real_type
)
10705 op1
= convert_and_check (real_type
, op1
);
10709 if (TREE_TYPE (op0
) != real_type
)
10710 op0
= convert_and_check (real_type
, op0
);
10711 if (TREE_TYPE (op1
) != result_type
)
10712 op1
= convert_and_check (result_type
, op1
);
10714 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
10715 return error_mark_node
;
10718 op0
= c_save_expr (op0
);
10719 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
10721 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
10726 case TRUNC_DIV_EXPR
:
10727 op1
= c_save_expr (op1
);
10728 imag
= build2 (resultcode
, real_type
, imag
, op1
);
10729 /* Fall through. */
10732 real
= build2 (resultcode
, real_type
, real
, op1
);
10740 op1
= c_save_expr (op1
);
10741 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
10743 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
10748 op0
= c_save_expr (op0
);
10749 imag
= build2 (resultcode
, real_type
, op0
, imag
);
10750 /* Fall through. */
10752 real
= build2 (resultcode
, real_type
, op0
, real
);
10755 real
= build2 (resultcode
, real_type
, op0
, real
);
10756 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
10762 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
10763 goto return_build_binary_op
;
10766 /* For certain operations (which identify themselves by shorten != 0)
10767 if both args were extended from the same smaller type,
10768 do the arithmetic in that type and then extend.
10770 shorten !=0 and !=1 indicates a bitwise operation.
10771 For them, this optimization is safe only if
10772 both args are zero-extended or both are sign-extended.
10773 Otherwise, we might change the result.
10774 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10775 but calculated in (unsigned short) it would be (unsigned short)-1. */
10777 if (shorten
&& none_complex
)
10779 final_type
= result_type
;
10780 result_type
= shorten_binary_op (result_type
, op0
, op1
,
10784 /* Shifts can be shortened if shifting right. */
10789 tree arg0
= get_narrower (op0
, &unsigned_arg
);
10791 final_type
= result_type
;
10793 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
10794 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
10796 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
10797 && tree_int_cst_sgn (op1
) > 0
10798 /* We can shorten only if the shift count is less than the
10799 number of bits in the smaller type size. */
10800 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
10801 /* We cannot drop an unsigned shift after sign-extension. */
10802 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
10804 /* Do an unsigned shift if the operand was zero-extended. */
10806 = c_common_signed_or_unsigned_type (unsigned_arg
,
10808 /* Convert value-to-be-shifted to that type. */
10809 if (TREE_TYPE (op0
) != result_type
)
10810 op0
= convert (result_type
, op0
);
10815 /* Comparison operations are shortened too but differently.
10816 They identify themselves by setting short_compare = 1. */
10820 /* Don't write &op0, etc., because that would prevent op0
10821 from being kept in a register.
10822 Instead, make copies of the our local variables and
10823 pass the copies by reference, then copy them back afterward. */
10824 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
10825 enum tree_code xresultcode
= resultcode
;
10827 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
10832 goto return_build_binary_op
;
10835 op0
= xop0
, op1
= xop1
;
10837 resultcode
= xresultcode
;
10839 if (c_inhibit_evaluation_warnings
== 0)
10841 bool op0_maybe_const
= true;
10842 bool op1_maybe_const
= true;
10843 tree orig_op0_folded
, orig_op1_folded
;
10845 if (in_late_binary_op
)
10847 orig_op0_folded
= orig_op0
;
10848 orig_op1_folded
= orig_op1
;
10852 /* Fold for the sake of possible warnings, as in
10853 build_conditional_expr. This requires the
10854 "original" values to be folded, not just op0 and
10856 c_inhibit_evaluation_warnings
++;
10857 op0
= c_fully_fold (op0
, require_constant_value
,
10859 op1
= c_fully_fold (op1
, require_constant_value
,
10861 c_inhibit_evaluation_warnings
--;
10862 orig_op0_folded
= c_fully_fold (orig_op0
,
10863 require_constant_value
,
10865 orig_op1_folded
= c_fully_fold (orig_op1
,
10866 require_constant_value
,
10870 if (warn_sign_compare
)
10871 warn_for_sign_compare (location
, orig_op0_folded
,
10872 orig_op1_folded
, op0
, op1
,
10873 result_type
, resultcode
);
10874 if (!in_late_binary_op
&& !int_operands
)
10876 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
10877 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
10878 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
10879 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
10885 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10886 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10887 Then the expression will be built.
10888 It will be given type FINAL_TYPE if that is nonzero;
10889 otherwise, it will be given type RESULT_TYPE. */
10893 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
10894 return error_mark_node
;
10897 if (build_type
== NULL_TREE
)
10899 build_type
= result_type
;
10900 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
10903 gcc_assert (may_need_excess_precision
&& common
);
10904 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
10910 op0
= ep_convert_and_check (result_type
, op0
, semantic_result_type
);
10911 op1
= ep_convert_and_check (result_type
, op1
, semantic_result_type
);
10913 /* This can happen if one operand has a vector type, and the other
10914 has a different type. */
10915 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
10916 return error_mark_node
;
10919 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
))
10920 && current_function_decl
!= 0
10921 && !lookup_attribute ("no_sanitize_undefined",
10922 DECL_ATTRIBUTES (current_function_decl
))
10923 && (doing_div_or_mod
|| doing_shift
))
10925 /* OP0 and/or OP1 might have side-effects. */
10926 op0
= c_save_expr (op0
);
10927 op1
= c_save_expr (op1
);
10928 op0
= c_fully_fold (op0
, false, NULL
);
10929 op1
= c_fully_fold (op1
, false, NULL
);
10930 if (doing_div_or_mod
&& (flag_sanitize
& SANITIZE_DIVIDE
))
10931 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
10932 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
10933 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
10936 /* Treat expressions in initializers specially as they can't trap. */
10937 if (int_const_or_overflow
)
10938 ret
= (require_constant_value
10939 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
10941 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
10943 ret
= build2 (resultcode
, build_type
, op0
, op1
);
10944 if (final_type
!= 0)
10945 ret
= convert (final_type
, ret
);
10947 return_build_binary_op
:
10948 gcc_assert (ret
!= error_mark_node
);
10949 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
10950 ret
= (int_operands
10951 ? note_integer_operands (ret
)
10952 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
10953 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
10954 && !in_late_binary_op
)
10955 ret
= note_integer_operands (ret
);
10956 if (semantic_result_type
)
10957 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
10958 protected_set_expr_location (ret
, location
);
10960 if (instrument_expr
!= NULL
)
10961 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
10962 instrument_expr
, ret
);
10968 /* Convert EXPR to be a truth-value, validating its type for this
10969 purpose. LOCATION is the source location for the expression. */
10972 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
10974 bool int_const
, int_operands
;
10976 switch (TREE_CODE (TREE_TYPE (expr
)))
10979 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
10980 return error_mark_node
;
10983 error_at (location
, "used struct type value where scalar is required");
10984 return error_mark_node
;
10987 error_at (location
, "used union type value where scalar is required");
10988 return error_mark_node
;
10991 error_at (location
, "void value not ignored as it ought to be");
10992 return error_mark_node
;
10994 case FUNCTION_TYPE
:
10995 gcc_unreachable ();
10998 error_at (location
, "used vector type where scalar is required");
10999 return error_mark_node
;
11005 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11006 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11007 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11009 expr
= remove_c_maybe_const_expr (expr
);
11010 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11011 convert (TREE_TYPE (expr
), integer_zero_node
));
11012 expr
= note_integer_operands (expr
);
11015 /* ??? Should we also give an error for vectors rather than leaving
11016 those to give errors later? */
11017 expr
= c_common_truthvalue_conversion (location
, expr
);
11019 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11021 if (TREE_OVERFLOW (expr
))
11024 return note_integer_operands (expr
);
11026 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11027 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11032 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11036 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11038 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11040 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11041 /* Executing a compound literal inside a function reinitializes
11043 if (!TREE_STATIC (decl
))
11051 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11054 c_begin_omp_parallel (void)
11058 keep_next_level ();
11059 block
= c_begin_compound_stmt (true);
11064 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11065 statement. LOC is the location of the OMP_PARALLEL. */
11068 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11072 block
= c_end_compound_stmt (loc
, block
, true);
11074 stmt
= make_node (OMP_PARALLEL
);
11075 TREE_TYPE (stmt
) = void_type_node
;
11076 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11077 OMP_PARALLEL_BODY (stmt
) = block
;
11078 SET_EXPR_LOCATION (stmt
, loc
);
11080 return add_stmt (stmt
);
11083 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11086 c_begin_omp_task (void)
11090 keep_next_level ();
11091 block
= c_begin_compound_stmt (true);
11096 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11097 statement. LOC is the location of the #pragma. */
11100 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11104 block
= c_end_compound_stmt (loc
, block
, true);
11106 stmt
= make_node (OMP_TASK
);
11107 TREE_TYPE (stmt
) = void_type_node
;
11108 OMP_TASK_CLAUSES (stmt
) = clauses
;
11109 OMP_TASK_BODY (stmt
) = block
;
11110 SET_EXPR_LOCATION (stmt
, loc
);
11112 return add_stmt (stmt
);
11115 /* Generate GOMP_cancel call for #pragma omp cancel. */
11118 c_finish_omp_cancel (location_t loc
, tree clauses
)
11120 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
11122 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11124 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11126 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11128 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11132 error_at (loc
, "%<#pragma omp cancel must specify one of "
11133 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11137 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
11138 if (ifc
!= NULL_TREE
)
11140 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
11141 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
11142 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
11143 build_zero_cst (type
));
11146 ifc
= boolean_true_node
;
11147 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
11148 build_int_cst (integer_type_node
, mask
),
11153 /* Generate GOMP_cancellation_point call for
11154 #pragma omp cancellation point. */
11157 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
11159 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
11161 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11163 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11165 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11167 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11171 error_at (loc
, "%<#pragma omp cancellation point must specify one of "
11172 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11176 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
11177 build_int_cst (integer_type_node
, mask
));
11181 /* Helper function for handle_omp_array_sections. Called recursively
11182 to handle multiple array-section-subscripts. C is the clause,
11183 T current expression (initially OMP_CLAUSE_DECL), which is either
11184 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11185 expression if specified, TREE_VALUE length expression if specified,
11186 TREE_CHAIN is what it has been specified after, or some decl.
11187 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11188 set to true if any of the array-section-subscript could have length
11189 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11190 first array-section-subscript which is known not to have length
11192 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11193 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11194 all are or may have length of 1, array-section-subscript [:2] is the
11195 first one knonwn not to have length 1. For array-section-subscript
11196 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11197 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11198 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11199 case though, as some lengths could be zero. */
11202 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
11203 bool &maybe_zero_len
, unsigned int &first_non_one
)
11205 tree ret
, low_bound
, length
, type
;
11206 if (TREE_CODE (t
) != TREE_LIST
)
11208 if (t
== error_mark_node
|| TREE_TYPE (t
) == error_mark_node
)
11209 return error_mark_node
;
11210 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11213 error_at (OMP_CLAUSE_LOCATION (c
),
11214 "%qD is not a variable in %qs clause", t
,
11215 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11217 error_at (OMP_CLAUSE_LOCATION (c
),
11218 "%qE is not a variable in %qs clause", t
,
11219 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11220 return error_mark_node
;
11222 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11223 && TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11225 error_at (OMP_CLAUSE_LOCATION (c
),
11226 "%qD is threadprivate variable in %qs clause", t
,
11227 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11228 return error_mark_node
;
11233 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
11234 maybe_zero_len
, first_non_one
);
11235 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
11238 type
= TREE_TYPE (ret
);
11239 low_bound
= TREE_PURPOSE (t
);
11240 length
= TREE_VALUE (t
);
11242 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
11243 return error_mark_node
;
11245 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
11247 error_at (OMP_CLAUSE_LOCATION (c
),
11248 "low bound %qE of array section does not have integral type",
11250 return error_mark_node
;
11252 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
11254 error_at (OMP_CLAUSE_LOCATION (c
),
11255 "length %qE of array section does not have integral type",
11257 return error_mark_node
;
11260 && TREE_CODE (low_bound
) == INTEGER_CST
11261 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11262 > TYPE_PRECISION (sizetype
))
11263 low_bound
= fold_convert (sizetype
, low_bound
);
11265 && TREE_CODE (length
) == INTEGER_CST
11266 && TYPE_PRECISION (TREE_TYPE (length
))
11267 > TYPE_PRECISION (sizetype
))
11268 length
= fold_convert (sizetype
, length
);
11269 if (low_bound
== NULL_TREE
)
11270 low_bound
= integer_zero_node
;
11272 if (length
!= NULL_TREE
)
11274 if (!integer_nonzerop (length
))
11275 maybe_zero_len
= true;
11276 if (first_non_one
== types
.length ()
11277 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
11280 if (TREE_CODE (type
) == ARRAY_TYPE
)
11282 if (length
== NULL_TREE
11283 && (TYPE_DOMAIN (type
) == NULL_TREE
11284 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
11286 error_at (OMP_CLAUSE_LOCATION (c
),
11287 "for unknown bound array type length expression must "
11289 return error_mark_node
;
11291 if (TREE_CODE (low_bound
) == INTEGER_CST
11292 && tree_int_cst_sgn (low_bound
) == -1)
11294 error_at (OMP_CLAUSE_LOCATION (c
),
11295 "negative low bound in array section in %qs clause",
11296 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11297 return error_mark_node
;
11299 if (length
!= NULL_TREE
11300 && TREE_CODE (length
) == INTEGER_CST
11301 && tree_int_cst_sgn (length
) == -1)
11303 error_at (OMP_CLAUSE_LOCATION (c
),
11304 "negative length in array section in %qs clause",
11305 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11306 return error_mark_node
;
11308 if (TYPE_DOMAIN (type
)
11309 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
11310 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
11313 tree size
= size_binop (PLUS_EXPR
,
11314 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11316 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11318 if (tree_int_cst_lt (size
, low_bound
))
11320 error_at (OMP_CLAUSE_LOCATION (c
),
11321 "low bound %qE above array section size "
11322 "in %qs clause", low_bound
,
11323 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11324 return error_mark_node
;
11326 if (tree_int_cst_equal (size
, low_bound
))
11327 maybe_zero_len
= true;
11328 else if (length
== NULL_TREE
11329 && first_non_one
== types
.length ()
11330 && tree_int_cst_equal
11331 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11335 else if (length
== NULL_TREE
)
11337 maybe_zero_len
= true;
11338 if (first_non_one
== types
.length ())
11341 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
11343 if (tree_int_cst_lt (size
, length
))
11345 error_at (OMP_CLAUSE_LOCATION (c
),
11346 "length %qE above array section size "
11347 "in %qs clause", length
,
11348 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11349 return error_mark_node
;
11351 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11354 = size_binop (PLUS_EXPR
,
11355 fold_convert (sizetype
, low_bound
),
11356 fold_convert (sizetype
, length
));
11357 if (TREE_CODE (lbpluslen
) == INTEGER_CST
11358 && tree_int_cst_lt (size
, lbpluslen
))
11360 error_at (OMP_CLAUSE_LOCATION (c
),
11361 "high bound %qE above array section size "
11362 "in %qs clause", lbpluslen
,
11363 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11364 return error_mark_node
;
11369 else if (length
== NULL_TREE
)
11371 maybe_zero_len
= true;
11372 if (first_non_one
== types
.length ())
11376 /* For [lb:] we will need to evaluate lb more than once. */
11377 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11379 tree lb
= c_save_expr (low_bound
);
11380 if (lb
!= low_bound
)
11382 TREE_PURPOSE (t
) = lb
;
11387 else if (TREE_CODE (type
) == POINTER_TYPE
)
11389 if (length
== NULL_TREE
)
11391 error_at (OMP_CLAUSE_LOCATION (c
),
11392 "for pointer type length expression must be specified");
11393 return error_mark_node
;
11395 /* If there is a pointer type anywhere but in the very first
11396 array-section-subscript, the array section can't be contiguous. */
11397 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11398 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
11400 error_at (OMP_CLAUSE_LOCATION (c
),
11401 "array section is not contiguous in %qs clause",
11402 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11403 return error_mark_node
;
11408 error_at (OMP_CLAUSE_LOCATION (c
),
11409 "%qE does not have pointer or array type", ret
);
11410 return error_mark_node
;
11412 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11413 types
.safe_push (TREE_TYPE (ret
));
11414 /* We will need to evaluate lb more than once. */
11415 tree lb
= c_save_expr (low_bound
);
11416 if (lb
!= low_bound
)
11418 TREE_PURPOSE (t
) = lb
;
11421 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
11425 /* Handle array sections for clause C. */
11428 handle_omp_array_sections (tree c
)
11430 bool maybe_zero_len
= false;
11431 unsigned int first_non_one
= 0;
11432 vec
<tree
> types
= vNULL
;
11433 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
11434 maybe_zero_len
, first_non_one
);
11435 if (first
== error_mark_node
)
11440 if (first
== NULL_TREE
)
11445 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
11447 tree t
= OMP_CLAUSE_DECL (c
);
11448 tree tem
= NULL_TREE
;
11450 /* Need to evaluate side effects in the length expressions
11452 while (TREE_CODE (t
) == TREE_LIST
)
11454 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
11456 if (tem
== NULL_TREE
)
11457 tem
= TREE_VALUE (t
);
11459 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
11460 TREE_VALUE (t
), tem
);
11462 t
= TREE_CHAIN (t
);
11465 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
11466 first
= c_fully_fold (first
, false, NULL
);
11467 OMP_CLAUSE_DECL (c
) = first
;
11471 unsigned int num
= types
.length (), i
;
11472 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
11473 tree condition
= NULL_TREE
;
11475 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
11476 maybe_zero_len
= true;
11478 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
11479 t
= TREE_CHAIN (t
))
11481 tree low_bound
= TREE_PURPOSE (t
);
11482 tree length
= TREE_VALUE (t
);
11486 && TREE_CODE (low_bound
) == INTEGER_CST
11487 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11488 > TYPE_PRECISION (sizetype
))
11489 low_bound
= fold_convert (sizetype
, low_bound
);
11491 && TREE_CODE (length
) == INTEGER_CST
11492 && TYPE_PRECISION (TREE_TYPE (length
))
11493 > TYPE_PRECISION (sizetype
))
11494 length
= fold_convert (sizetype
, length
);
11495 if (low_bound
== NULL_TREE
)
11496 low_bound
= integer_zero_node
;
11497 if (!maybe_zero_len
&& i
> first_non_one
)
11499 if (integer_nonzerop (low_bound
))
11500 goto do_warn_noncontiguous
;
11501 if (length
!= NULL_TREE
11502 && TREE_CODE (length
) == INTEGER_CST
11503 && TYPE_DOMAIN (types
[i
])
11504 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
11505 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
11509 size
= size_binop (PLUS_EXPR
,
11510 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11512 if (!tree_int_cst_equal (length
, size
))
11514 do_warn_noncontiguous
:
11515 error_at (OMP_CLAUSE_LOCATION (c
),
11516 "array section is not contiguous in %qs "
11518 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11523 if (length
!= NULL_TREE
11524 && TREE_SIDE_EFFECTS (length
))
11526 if (side_effects
== NULL_TREE
)
11527 side_effects
= length
;
11529 side_effects
= build2 (COMPOUND_EXPR
,
11530 TREE_TYPE (side_effects
),
11531 length
, side_effects
);
11538 if (i
> first_non_one
&& length
&& integer_nonzerop (length
))
11541 l
= fold_convert (sizetype
, length
);
11544 l
= size_binop (PLUS_EXPR
,
11545 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11547 l
= size_binop (MINUS_EXPR
, l
,
11548 fold_convert (sizetype
, low_bound
));
11550 if (i
> first_non_one
)
11552 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
11554 if (condition
== NULL_TREE
)
11557 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
11560 else if (size
== NULL_TREE
)
11562 size
= size_in_bytes (TREE_TYPE (types
[i
]));
11563 size
= size_binop (MULT_EXPR
, size
, l
);
11565 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
11566 size
, size_zero_node
);
11569 size
= size_binop (MULT_EXPR
, size
, l
);
11574 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
11575 first
= c_fully_fold (first
, false, NULL
);
11576 OMP_CLAUSE_DECL (c
) = first
;
11578 size
= c_fully_fold (size
, false, NULL
);
11579 OMP_CLAUSE_SIZE (c
) = size
;
11580 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
11582 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
11583 OMP_CLAUSE_MAP_KIND (c2
) = OMP_CLAUSE_MAP_POINTER
;
11584 if (!c_mark_addressable (t
))
11586 OMP_CLAUSE_DECL (c2
) = t
;
11587 t
= build_fold_addr_expr (first
);
11588 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
11589 tree ptr
= OMP_CLAUSE_DECL (c2
);
11590 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
11591 ptr
= build_fold_addr_expr (ptr
);
11592 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11593 ptrdiff_type_node
, t
,
11594 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
11595 ptrdiff_type_node
, ptr
));
11596 t
= c_fully_fold (t
, false, NULL
);
11597 OMP_CLAUSE_SIZE (c2
) = t
;
11598 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
11599 OMP_CLAUSE_CHAIN (c
) = c2
;
11604 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11605 an inline call. But, remap
11606 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11607 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11610 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
11611 tree decl
, tree placeholder
)
11614 struct pointer_map_t
*decl_map
= pointer_map_create ();
11616 *pointer_map_insert (decl_map
, omp_decl1
) = placeholder
;
11617 *pointer_map_insert (decl_map
, omp_decl2
) = decl
;
11618 memset (&id
, 0, sizeof (id
));
11619 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
11620 id
.dst_fn
= current_function_decl
;
11621 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
11622 id
.decl_map
= decl_map
;
11624 id
.copy_decl
= copy_decl_no_change
;
11625 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
11626 id
.transform_new_cfg
= true;
11627 id
.transform_return_to_modify
= false;
11628 id
.transform_lang_insert_block
= NULL
;
11630 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
11631 pointer_map_destroy (decl_map
);
11635 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11636 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11639 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
11641 if (*tp
== (tree
) data
)
11646 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11647 Remove any elements from the list that are invalid. */
11650 c_finish_omp_clauses (tree clauses
)
11652 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
11653 bitmap_head aligned_head
;
11654 tree c
, t
, *pc
= &clauses
;
11655 bool branch_seen
= false;
11656 bool copyprivate_seen
= false;
11657 tree
*nowait_clause
= NULL
;
11659 bitmap_obstack_initialize (NULL
);
11660 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
11661 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
11662 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
11663 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
11665 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
11667 bool remove
= false;
11668 bool need_complete
= false;
11669 bool need_implicitly_determined
= false;
11671 switch (OMP_CLAUSE_CODE (c
))
11673 case OMP_CLAUSE_SHARED
:
11674 need_implicitly_determined
= true;
11675 goto check_dup_generic
;
11677 case OMP_CLAUSE_PRIVATE
:
11678 need_complete
= true;
11679 need_implicitly_determined
= true;
11680 goto check_dup_generic
;
11682 case OMP_CLAUSE_REDUCTION
:
11683 need_implicitly_determined
= true;
11684 t
= OMP_CLAUSE_DECL (c
);
11685 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
11686 && FLOAT_TYPE_P (TREE_TYPE (t
)))
11688 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
11689 const char *r_name
= NULL
;
11708 case TRUTH_ANDIF_EXPR
:
11711 case TRUTH_ORIF_EXPR
:
11715 gcc_unreachable ();
11719 error_at (OMP_CLAUSE_LOCATION (c
),
11720 "%qE has invalid type for %<reduction(%s)%>",
11726 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
11728 error_at (OMP_CLAUSE_LOCATION (c
),
11729 "user defined reduction not found for %qD", t
);
11733 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
11735 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
11736 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
11737 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
11738 VAR_DECL
, NULL_TREE
, type
);
11739 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
11740 DECL_ARTIFICIAL (placeholder
) = 1;
11741 DECL_IGNORED_P (placeholder
) = 1;
11742 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
11743 c_mark_addressable (placeholder
);
11744 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
11745 c_mark_addressable (OMP_CLAUSE_DECL (c
));
11746 OMP_CLAUSE_REDUCTION_MERGE (c
)
11747 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
11748 TREE_VEC_ELT (list
, 0),
11749 TREE_VEC_ELT (list
, 1),
11750 OMP_CLAUSE_DECL (c
), placeholder
);
11751 OMP_CLAUSE_REDUCTION_MERGE (c
)
11752 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
11753 void_type_node
, NULL_TREE
,
11754 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
11755 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
11756 if (TREE_VEC_LENGTH (list
) == 6)
11758 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
11759 c_mark_addressable (OMP_CLAUSE_DECL (c
));
11760 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
11761 c_mark_addressable (placeholder
);
11762 tree init
= TREE_VEC_ELT (list
, 5);
11763 if (init
== error_mark_node
)
11764 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
11765 OMP_CLAUSE_REDUCTION_INIT (c
)
11766 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
11767 TREE_VEC_ELT (list
, 3),
11768 OMP_CLAUSE_DECL (c
), placeholder
);
11769 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
11770 OMP_CLAUSE_REDUCTION_INIT (c
)
11771 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
,
11772 OMP_CLAUSE_REDUCTION_INIT (c
));
11773 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
11774 c_find_omp_placeholder_r
,
11775 placeholder
, NULL
))
11776 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
11781 if (AGGREGATE_TYPE_P (TREE_TYPE (t
)))
11782 init
= build_constructor (TREE_TYPE (t
), NULL
);
11784 init
= fold_convert (TREE_TYPE (t
), integer_zero_node
);
11785 OMP_CLAUSE_REDUCTION_INIT (c
)
11786 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
, init
);
11788 OMP_CLAUSE_REDUCTION_INIT (c
)
11789 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
11790 void_type_node
, NULL_TREE
,
11791 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
11792 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
11794 goto check_dup_generic
;
11796 case OMP_CLAUSE_COPYPRIVATE
:
11797 copyprivate_seen
= true;
11800 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
11801 "%<nowait%> clause must not be used together "
11802 "with %<copyprivate%>");
11803 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
11804 nowait_clause
= NULL
;
11806 goto check_dup_generic
;
11808 case OMP_CLAUSE_COPYIN
:
11809 t
= OMP_CLAUSE_DECL (c
);
11810 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
11812 error_at (OMP_CLAUSE_LOCATION (c
),
11813 "%qE must be %<threadprivate%> for %<copyin%>", t
);
11817 goto check_dup_generic
;
11819 case OMP_CLAUSE_LINEAR
:
11820 t
= OMP_CLAUSE_DECL (c
);
11821 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
11822 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
11824 error_at (OMP_CLAUSE_LOCATION (c
),
11825 "linear clause applied to non-integral non-pointer "
11826 "variable with type %qT", TREE_TYPE (t
));
11830 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
11832 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
11833 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
11834 OMP_CLAUSE_DECL (c
), s
);
11835 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11836 sizetype
, s
, OMP_CLAUSE_DECL (c
));
11837 if (s
== error_mark_node
)
11839 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
11841 goto check_dup_generic
;
11844 t
= OMP_CLAUSE_DECL (c
);
11845 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11847 error_at (OMP_CLAUSE_LOCATION (c
),
11848 "%qE is not a variable in clause %qs", t
,
11849 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11852 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
11853 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
11854 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
11856 error_at (OMP_CLAUSE_LOCATION (c
),
11857 "%qE appears more than once in data clauses", t
);
11861 bitmap_set_bit (&generic_head
, DECL_UID (t
));
11864 case OMP_CLAUSE_FIRSTPRIVATE
:
11865 t
= OMP_CLAUSE_DECL (c
);
11866 need_complete
= true;
11867 need_implicitly_determined
= true;
11868 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11870 error_at (OMP_CLAUSE_LOCATION (c
),
11871 "%qE is not a variable in clause %<firstprivate%>", t
);
11874 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
11875 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
11877 error_at (OMP_CLAUSE_LOCATION (c
),
11878 "%qE appears more than once in data clauses", t
);
11882 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
11885 case OMP_CLAUSE_LASTPRIVATE
:
11886 t
= OMP_CLAUSE_DECL (c
);
11887 need_complete
= true;
11888 need_implicitly_determined
= true;
11889 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11891 error_at (OMP_CLAUSE_LOCATION (c
),
11892 "%qE is not a variable in clause %<lastprivate%>", t
);
11895 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
11896 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
11898 error_at (OMP_CLAUSE_LOCATION (c
),
11899 "%qE appears more than once in data clauses", t
);
11903 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
11906 case OMP_CLAUSE_ALIGNED
:
11907 t
= OMP_CLAUSE_DECL (c
);
11908 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11910 error_at (OMP_CLAUSE_LOCATION (c
),
11911 "%qE is not a variable in %<aligned%> clause", t
);
11914 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
11915 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
11917 error_at (OMP_CLAUSE_LOCATION (c
),
11918 "%qE in %<aligned%> clause is neither a pointer nor "
11922 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
11924 error_at (OMP_CLAUSE_LOCATION (c
),
11925 "%qE appears more than once in %<aligned%> clauses",
11930 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
11933 case OMP_CLAUSE_DEPEND
:
11934 t
= OMP_CLAUSE_DECL (c
);
11935 if (TREE_CODE (t
) == TREE_LIST
)
11937 if (handle_omp_array_sections (c
))
11941 if (t
== error_mark_node
)
11943 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11945 error_at (OMP_CLAUSE_LOCATION (c
),
11946 "%qE is not a variable in %<depend%> clause", t
);
11949 else if (!c_mark_addressable (t
))
11953 case OMP_CLAUSE_MAP
:
11954 case OMP_CLAUSE_TO
:
11955 case OMP_CLAUSE_FROM
:
11956 t
= OMP_CLAUSE_DECL (c
);
11957 if (TREE_CODE (t
) == TREE_LIST
)
11959 if (handle_omp_array_sections (c
))
11963 t
= OMP_CLAUSE_DECL (c
);
11964 if (!COMPLETE_TYPE_P (TREE_TYPE (t
)))
11966 error_at (OMP_CLAUSE_LOCATION (c
),
11967 "array section does not have mappable type "
11969 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11975 if (t
== error_mark_node
)
11977 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11979 error_at (OMP_CLAUSE_LOCATION (c
),
11980 "%qE is not a variable in %qs clause", t
,
11981 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11984 else if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11986 error_at (OMP_CLAUSE_LOCATION (c
),
11987 "%qD is threadprivate variable in %qs clause", t
,
11988 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11991 else if (!c_mark_addressable (t
))
11993 else if (!COMPLETE_TYPE_P (TREE_TYPE (t
))
11994 && !(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
11995 && OMP_CLAUSE_MAP_KIND (c
) == OMP_CLAUSE_MAP_POINTER
))
11997 error_at (OMP_CLAUSE_LOCATION (c
),
11998 "%qD does not have a mappable type in %qs clause", t
,
11999 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12002 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
12004 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
12005 error ("%qD appears more than once in motion clauses", t
);
12007 error ("%qD appears more than once in map clauses", t
);
12011 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12014 case OMP_CLAUSE_UNIFORM
:
12015 t
= OMP_CLAUSE_DECL (c
);
12016 if (TREE_CODE (t
) != PARM_DECL
)
12019 error_at (OMP_CLAUSE_LOCATION (c
),
12020 "%qD is not an argument in %<uniform%> clause", t
);
12022 error_at (OMP_CLAUSE_LOCATION (c
),
12023 "%qE is not an argument in %<uniform%> clause", t
);
12027 goto check_dup_generic
;
12029 case OMP_CLAUSE_NOWAIT
:
12030 if (copyprivate_seen
)
12032 error_at (OMP_CLAUSE_LOCATION (c
),
12033 "%<nowait%> clause must not be used together "
12034 "with %<copyprivate%>");
12038 nowait_clause
= pc
;
12039 pc
= &OMP_CLAUSE_CHAIN (c
);
12042 case OMP_CLAUSE_IF
:
12043 case OMP_CLAUSE_NUM_THREADS
:
12044 case OMP_CLAUSE_NUM_TEAMS
:
12045 case OMP_CLAUSE_THREAD_LIMIT
:
12046 case OMP_CLAUSE_SCHEDULE
:
12047 case OMP_CLAUSE_ORDERED
:
12048 case OMP_CLAUSE_DEFAULT
:
12049 case OMP_CLAUSE_UNTIED
:
12050 case OMP_CLAUSE_COLLAPSE
:
12051 case OMP_CLAUSE_FINAL
:
12052 case OMP_CLAUSE_MERGEABLE
:
12053 case OMP_CLAUSE_SAFELEN
:
12054 case OMP_CLAUSE_SIMDLEN
:
12055 case OMP_CLAUSE_DEVICE
:
12056 case OMP_CLAUSE_DIST_SCHEDULE
:
12057 case OMP_CLAUSE_PARALLEL
:
12058 case OMP_CLAUSE_FOR
:
12059 case OMP_CLAUSE_SECTIONS
:
12060 case OMP_CLAUSE_TASKGROUP
:
12061 case OMP_CLAUSE_PROC_BIND
:
12062 pc
= &OMP_CLAUSE_CHAIN (c
);
12065 case OMP_CLAUSE_INBRANCH
:
12066 case OMP_CLAUSE_NOTINBRANCH
:
12069 error_at (OMP_CLAUSE_LOCATION (c
),
12070 "%<inbranch%> clause is incompatible with "
12071 "%<notinbranch%>");
12075 branch_seen
= true;
12076 pc
= &OMP_CLAUSE_CHAIN (c
);
12080 gcc_unreachable ();
12085 t
= OMP_CLAUSE_DECL (c
);
12089 t
= require_complete_type (t
);
12090 if (t
== error_mark_node
)
12094 if (need_implicitly_determined
)
12096 const char *share_name
= NULL
;
12098 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12099 share_name
= "threadprivate";
12100 else switch (c_omp_predetermined_sharing (t
))
12102 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
12104 case OMP_CLAUSE_DEFAULT_SHARED
:
12105 /* const vars may be specified in firstprivate clause. */
12106 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12107 && TREE_READONLY (t
))
12109 share_name
= "shared";
12111 case OMP_CLAUSE_DEFAULT_PRIVATE
:
12112 share_name
= "private";
12115 gcc_unreachable ();
12119 error_at (OMP_CLAUSE_LOCATION (c
),
12120 "%qE is predetermined %qs for %qs",
12122 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12129 *pc
= OMP_CLAUSE_CHAIN (c
);
12131 pc
= &OMP_CLAUSE_CHAIN (c
);
12134 bitmap_obstack_release (NULL
);
12138 /* Create a transaction node. */
12141 c_finish_transaction (location_t loc
, tree block
, int flags
)
12143 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
12144 if (flags
& TM_STMT_ATTR_OUTER
)
12145 TRANSACTION_EXPR_OUTER (stmt
) = 1;
12146 if (flags
& TM_STMT_ATTR_RELAXED
)
12147 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
12148 return add_stmt (stmt
);
12151 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12152 down to the element type of an array. */
12155 c_build_qualified_type (tree type
, int type_quals
)
12157 if (type
== error_mark_node
)
12160 if (TREE_CODE (type
) == ARRAY_TYPE
)
12163 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
12166 /* See if we already have an identically qualified type. */
12167 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12169 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
12170 && TYPE_NAME (t
) == TYPE_NAME (type
)
12171 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
12172 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
12173 TYPE_ATTRIBUTES (type
)))
12178 tree domain
= TYPE_DOMAIN (type
);
12180 t
= build_variant_type_copy (type
);
12181 TREE_TYPE (t
) = element_type
;
12183 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
12184 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
12185 SET_TYPE_STRUCTURAL_EQUALITY (t
);
12186 else if (TYPE_CANONICAL (element_type
) != element_type
12187 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
12189 tree unqualified_canon
12190 = build_array_type (TYPE_CANONICAL (element_type
),
12191 domain
? TYPE_CANONICAL (domain
)
12194 = c_build_qualified_type (unqualified_canon
, type_quals
);
12197 TYPE_CANONICAL (t
) = t
;
12202 /* A restrict-qualified pointer type must be a pointer to object or
12203 incomplete type. Note that the use of POINTER_TYPE_P also allows
12204 REFERENCE_TYPEs, which is appropriate for C++. */
12205 if ((type_quals
& TYPE_QUAL_RESTRICT
)
12206 && (!POINTER_TYPE_P (type
)
12207 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
12209 error ("invalid use of %<restrict%>");
12210 type_quals
&= ~TYPE_QUAL_RESTRICT
;
12213 return build_qualified_type (type
, type_quals
);
12216 /* Build a VA_ARG_EXPR for the C parser. */
12219 c_build_va_arg (location_t loc
, tree expr
, tree type
)
12221 if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
12222 warning_at (loc
, OPT_Wc___compat
,
12223 "C++ requires promoted type, not enum type, in %<va_arg%>");
12224 return build_va_arg (loc
, expr
, type
);
12227 /* Return truthvalue of whether T1 is the same tree structure as T2.
12228 Return 1 if they are the same. Return 0 if they are different. */
12231 c_tree_equal (tree t1
, tree t2
)
12233 enum tree_code code1
, code2
;
12240 for (code1
= TREE_CODE (t1
);
12241 CONVERT_EXPR_CODE_P (code1
)
12242 || code1
== NON_LVALUE_EXPR
;
12243 code1
= TREE_CODE (t1
))
12244 t1
= TREE_OPERAND (t1
, 0);
12245 for (code2
= TREE_CODE (t2
);
12246 CONVERT_EXPR_CODE_P (code2
)
12247 || code2
== NON_LVALUE_EXPR
;
12248 code2
= TREE_CODE (t2
))
12249 t2
= TREE_OPERAND (t2
, 0);
12251 /* They might have become equal now. */
12255 if (code1
!= code2
)
12261 return wi::eq_p (t1
, 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. */