1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
28 #include "coretypes.h"
35 #include "double-int.h"
39 #include "fixed-value.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
43 #include "trans-mem.h"
46 #include "langhooks.h"
52 #include "tree-iterator.h"
59 #include "hard-reg-set.h"
62 #include "gimple-expr.h"
64 #include "tree-inline.h"
66 #include "c-family/c-objc.h"
67 #include "c-family/c-common.h"
68 #include "c-family/c-ubsan.h"
71 #include "gomp-constants.h"
73 /* Possible cases of implicit bad conversions. Used to select
74 diagnostic messages in convert_for_assignment. */
82 /* The level of nesting inside "__alignof__". */
85 /* The level of nesting inside "sizeof". */
88 /* The level of nesting inside "typeof". */
91 /* The argument of last parsed sizeof expression, only to be tested
92 if expr.original_code == SIZEOF_EXPR. */
93 tree c_last_sizeof_arg
;
95 /* Nonzero if we might need to print a "missing braces around
96 initializer" message within this initializer. */
97 static int found_missing_braces
;
99 static int require_constant_value
;
100 static int require_constant_elements
;
102 static bool null_pointer_constant_p (const_tree
);
103 static tree
qualify_type (tree
, tree
);
104 static int tagged_types_tu_compatible_p (const_tree
, const_tree
, bool *,
106 static int comp_target_types (location_t
, tree
, tree
);
107 static int function_types_compatible_p (const_tree
, const_tree
, bool *,
109 static int type_lists_compatible_p (const_tree
, const_tree
, bool *, bool *);
110 static tree
lookup_field (tree
, tree
);
111 static int convert_arguments (location_t
, vec
<location_t
>, tree
,
112 vec
<tree
, va_gc
> *, vec
<tree
, va_gc
> *, tree
,
114 static tree
pointer_diff (location_t
, tree
, tree
);
115 static tree
convert_for_assignment (location_t
, location_t
, tree
, tree
, tree
,
116 enum impl_conv
, bool, tree
, tree
, int);
117 static tree
valid_compound_expr_initializer (tree
, tree
);
118 static void push_string (const char *);
119 static void push_member_name (tree
);
120 static int spelling_length (void);
121 static char *print_spelling (char *);
122 static void warning_init (location_t
, int, const char *);
123 static tree
digest_init (location_t
, tree
, tree
, tree
, bool, bool, int);
124 static void output_init_element (location_t
, tree
, tree
, bool, tree
, tree
, int,
125 bool, struct obstack
*);
126 static void output_pending_init_elements (int, struct obstack
*);
127 static int set_designator (location_t
, int, struct obstack
*);
128 static void push_range_stack (tree
, struct obstack
*);
129 static void add_pending_init (location_t
, tree
, tree
, tree
, bool,
131 static void set_nonincremental_init (struct obstack
*);
132 static void set_nonincremental_init_from_string (tree
, struct obstack
*);
133 static tree
find_init_member (tree
, struct obstack
*);
134 static void readonly_warning (tree
, enum lvalue_use
);
135 static int lvalue_or_else (location_t
, const_tree
, enum lvalue_use
);
136 static void record_maybe_used_decl (tree
);
137 static int comptypes_internal (const_tree
, const_tree
, bool *, bool *);
139 /* Return true if EXP is a null pointer constant, false otherwise. */
142 null_pointer_constant_p (const_tree expr
)
144 /* This should really operate on c_expr structures, but they aren't
145 yet available everywhere required. */
146 tree type
= TREE_TYPE (expr
);
147 return (TREE_CODE (expr
) == INTEGER_CST
148 && !TREE_OVERFLOW (expr
)
149 && integer_zerop (expr
)
150 && (INTEGRAL_TYPE_P (type
)
151 || (TREE_CODE (type
) == POINTER_TYPE
152 && VOID_TYPE_P (TREE_TYPE (type
))
153 && TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
)));
156 /* EXPR may appear in an unevaluated part of an integer constant
157 expression, but not in an evaluated part. Wrap it in a
158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
162 note_integer_operands (tree expr
)
165 if (TREE_CODE (expr
) == INTEGER_CST
&& in_late_binary_op
)
167 ret
= copy_node (expr
);
168 TREE_OVERFLOW (ret
) = 1;
172 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (expr
), NULL_TREE
, expr
);
173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret
) = 1;
178 /* Having checked whether EXPR may appear in an unevaluated part of an
179 integer constant expression and found that it may, remove any
180 C_MAYBE_CONST_EXPR noting this fact and return the resulting
184 remove_c_maybe_const_expr (tree expr
)
186 if (TREE_CODE (expr
) == C_MAYBE_CONST_EXPR
)
187 return C_MAYBE_CONST_EXPR_EXPR (expr
);
192 \f/* This is a cache to hold if two types are compatible or not. */
194 struct tagged_tu_seen_cache
{
195 const struct tagged_tu_seen_cache
* next
;
198 /* The return value of tagged_types_tu_compatible_p if we had seen
199 these two types already. */
203 static const struct tagged_tu_seen_cache
* tagged_tu_seen_base
;
204 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*);
206 /* Do `exp = require_complete_type (exp);' to make sure exp
207 does not have an incomplete type. (That includes void types.) */
210 require_complete_type (tree value
)
212 tree type
= TREE_TYPE (value
);
214 if (error_operand_p (value
))
215 return error_mark_node
;
217 /* First, detect a valid value with a complete type. */
218 if (COMPLETE_TYPE_P (type
))
221 c_incomplete_type_error (value
, type
);
222 return error_mark_node
;
225 /* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. */
230 c_incomplete_type_error (const_tree value
, const_tree type
)
232 const char *type_code_string
;
234 /* Avoid duplicate error message. */
235 if (TREE_CODE (type
) == ERROR_MARK
)
238 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
239 || TREE_CODE (value
) == PARM_DECL
))
240 error ("%qD has an incomplete type", value
);
244 /* We must print an error message. Be clever about what it says. */
246 switch (TREE_CODE (type
))
249 type_code_string
= "struct";
253 type_code_string
= "union";
257 type_code_string
= "enum";
261 error ("invalid use of void expression");
265 if (TYPE_DOMAIN (type
))
267 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL
)
269 error ("invalid use of flexible array member");
272 type
= TREE_TYPE (type
);
275 error ("invalid use of array with unspecified bounds");
282 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
283 error ("invalid use of undefined type %<%s %E%>",
284 type_code_string
, TYPE_NAME (type
));
286 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
287 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type
));
291 /* Given a type, apply default promotions wrt unnamed function
292 arguments and return the new type. */
295 c_type_promotes_to (tree type
)
297 tree ret
= NULL_TREE
;
299 if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
300 ret
= double_type_node
;
301 else if (c_promoting_integer_type_p (type
))
303 /* Preserve unsignedness if not really getting any wider. */
304 if (TYPE_UNSIGNED (type
)
305 && (TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
)))
306 ret
= unsigned_type_node
;
308 ret
= integer_type_node
;
311 if (ret
!= NULL_TREE
)
312 return (TYPE_ATOMIC (type
)
313 ? c_build_qualified_type (ret
, TYPE_QUAL_ATOMIC
)
319 /* Return true if between two named address spaces, whether there is a superset
320 named address space that encompasses both address spaces. If there is a
321 superset, return which address space is the superset. */
324 addr_space_superset (addr_space_t as1
, addr_space_t as2
, addr_space_t
*common
)
331 else if (targetm
.addr_space
.subset_p (as1
, as2
))
336 else if (targetm
.addr_space
.subset_p (as2
, as1
))
345 /* Return a variant of TYPE which has all the type qualifiers of LIKE
346 as well as those of TYPE. */
349 qualify_type (tree type
, tree like
)
351 addr_space_t as_type
= TYPE_ADDR_SPACE (type
);
352 addr_space_t as_like
= TYPE_ADDR_SPACE (like
);
353 addr_space_t as_common
;
355 /* If the two named address spaces are different, determine the common
356 superset address space. If there isn't one, raise an error. */
357 if (!addr_space_superset (as_type
, as_like
, &as_common
))
360 error ("%qT and %qT are in disjoint named address spaces",
364 return c_build_qualified_type (type
,
365 TYPE_QUALS_NO_ADDR_SPACE (type
)
366 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like
)
367 | ENCODE_QUAL_ADDR_SPACE (as_common
));
370 /* Return true iff the given tree T is a variable length array. */
373 c_vla_type_p (const_tree t
)
375 if (TREE_CODE (t
) == ARRAY_TYPE
376 && C_TYPE_VARIABLE_SIZE (t
))
381 /* Return the composite type of two compatible types.
383 We assume that comptypes has already been done and returned
384 nonzero; if that isn't so, this may crash. In particular, we
385 assume that qualifiers match. */
388 composite_type (tree t1
, tree t2
)
390 enum tree_code code1
;
391 enum tree_code code2
;
394 /* Save time if the two types are the same. */
396 if (t1
== t2
) return t1
;
398 /* If one type is nonsense, use the other. */
399 if (t1
== error_mark_node
)
401 if (t2
== error_mark_node
)
404 code1
= TREE_CODE (t1
);
405 code2
= TREE_CODE (t2
);
407 /* Merge the attributes. */
408 attributes
= targetm
.merge_type_attributes (t1
, t2
);
410 /* If one is an enumerated type and the other is the compatible
411 integer type, the composite type might be either of the two
412 (DR#013 question 3). For consistency, use the enumerated type as
413 the composite type. */
415 if (code1
== ENUMERAL_TYPE
&& code2
== INTEGER_TYPE
)
417 if (code2
== ENUMERAL_TYPE
&& code1
== INTEGER_TYPE
)
420 gcc_assert (code1
== code2
);
425 /* For two pointers, do this recursively on the target type. */
427 tree pointed_to_1
= TREE_TYPE (t1
);
428 tree pointed_to_2
= TREE_TYPE (t2
);
429 tree target
= composite_type (pointed_to_1
, pointed_to_2
);
430 t1
= build_pointer_type_for_mode (target
, TYPE_MODE (t1
), false);
431 t1
= build_type_attribute_variant (t1
, attributes
);
432 return qualify_type (t1
, t2
);
437 tree elt
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
440 tree d1
= TYPE_DOMAIN (t1
);
441 tree d2
= TYPE_DOMAIN (t2
);
442 bool d1_variable
, d2_variable
;
443 bool d1_zero
, d2_zero
;
444 bool t1_complete
, t2_complete
;
446 /* We should not have any type quals on arrays at all. */
447 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1
)
448 && !TYPE_QUALS_NO_ADDR_SPACE (t2
));
450 t1_complete
= COMPLETE_TYPE_P (t1
);
451 t2_complete
= COMPLETE_TYPE_P (t2
);
453 d1_zero
= d1
== 0 || !TYPE_MAX_VALUE (d1
);
454 d2_zero
= d2
== 0 || !TYPE_MAX_VALUE (d2
);
456 d1_variable
= (!d1_zero
457 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
458 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
459 d2_variable
= (!d2_zero
460 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
461 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
462 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
463 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
465 /* Save space: see if the result is identical to one of the args. */
466 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
)
467 && (d2_variable
|| d2_zero
|| !d1_variable
))
468 return build_type_attribute_variant (t1
, attributes
);
469 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
)
470 && (d1_variable
|| d1_zero
|| !d2_variable
))
471 return build_type_attribute_variant (t2
, attributes
);
473 if (elt
== TREE_TYPE (t1
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
474 return build_type_attribute_variant (t1
, attributes
);
475 if (elt
== TREE_TYPE (t2
) && !TYPE_DOMAIN (t2
) && !TYPE_DOMAIN (t1
))
476 return build_type_attribute_variant (t2
, attributes
);
478 /* Merge the element types, and have a size if either arg has
479 one. We may have qualifiers on the element types. To set
480 up TYPE_MAIN_VARIANT correctly, we need to form the
481 composite of the unqualified types and add the qualifiers
483 quals
= TYPE_QUALS (strip_array_types (elt
));
484 unqual_elt
= c_build_qualified_type (elt
, TYPE_UNQUALIFIED
);
485 t1
= build_array_type (unqual_elt
,
486 TYPE_DOMAIN ((TYPE_DOMAIN (t1
)
492 /* Ensure a composite type involving a zero-length array type
493 is a zero-length type not an incomplete type. */
494 if (d1_zero
&& d2_zero
495 && (t1_complete
|| t2_complete
)
496 && !COMPLETE_TYPE_P (t1
))
498 TYPE_SIZE (t1
) = bitsize_zero_node
;
499 TYPE_SIZE_UNIT (t1
) = size_zero_node
;
501 t1
= c_build_qualified_type (t1
, quals
);
502 return build_type_attribute_variant (t1
, attributes
);
508 if (attributes
!= NULL
)
510 /* Try harder not to create a new aggregate type. */
511 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
513 if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
516 return build_type_attribute_variant (t1
, attributes
);
519 /* Function types: prefer the one that specified arg types.
520 If both do, merge the arg types. Also merge the return types. */
522 tree valtype
= composite_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
523 tree p1
= TYPE_ARG_TYPES (t1
);
524 tree p2
= TYPE_ARG_TYPES (t2
);
529 /* Save space: see if the result is identical to one of the args. */
530 if (valtype
== TREE_TYPE (t1
) && !TYPE_ARG_TYPES (t2
))
531 return build_type_attribute_variant (t1
, attributes
);
532 if (valtype
== TREE_TYPE (t2
) && !TYPE_ARG_TYPES (t1
))
533 return build_type_attribute_variant (t2
, attributes
);
535 /* Simple way if one arg fails to specify argument types. */
536 if (TYPE_ARG_TYPES (t1
) == 0)
538 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t2
));
539 t1
= build_type_attribute_variant (t1
, attributes
);
540 return qualify_type (t1
, t2
);
542 if (TYPE_ARG_TYPES (t2
) == 0)
544 t1
= build_function_type (valtype
, TYPE_ARG_TYPES (t1
));
545 t1
= build_type_attribute_variant (t1
, attributes
);
546 return qualify_type (t1
, t2
);
549 /* If both args specify argument types, we must merge the two
550 lists, argument by argument. */
552 len
= list_length (p1
);
555 for (i
= 0; i
< len
; i
++)
556 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
561 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
))
563 /* A null type means arg type is not specified.
564 Take whatever the other function type has. */
565 if (TREE_VALUE (p1
) == 0)
567 TREE_VALUE (n
) = TREE_VALUE (p2
);
570 if (TREE_VALUE (p2
) == 0)
572 TREE_VALUE (n
) = TREE_VALUE (p1
);
576 /* Given wait (union {union wait *u; int *i} *)
577 and wait (union wait *),
578 prefer union wait * as type of parm. */
579 if (TREE_CODE (TREE_VALUE (p1
)) == UNION_TYPE
580 && TREE_VALUE (p1
) != TREE_VALUE (p2
))
583 tree mv2
= TREE_VALUE (p2
);
584 if (mv2
&& mv2
!= error_mark_node
585 && TREE_CODE (mv2
) != ARRAY_TYPE
)
586 mv2
= TYPE_MAIN_VARIANT (mv2
);
587 for (memb
= TYPE_FIELDS (TREE_VALUE (p1
));
588 memb
; memb
= DECL_CHAIN (memb
))
590 tree mv3
= TREE_TYPE (memb
);
591 if (mv3
&& mv3
!= error_mark_node
592 && TREE_CODE (mv3
) != ARRAY_TYPE
)
593 mv3
= TYPE_MAIN_VARIANT (mv3
);
594 if (comptypes (mv3
, mv2
))
596 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
598 pedwarn (input_location
, OPT_Wpedantic
,
599 "function types not truly compatible in ISO C");
604 if (TREE_CODE (TREE_VALUE (p2
)) == UNION_TYPE
605 && TREE_VALUE (p2
) != TREE_VALUE (p1
))
608 tree mv1
= TREE_VALUE (p1
);
609 if (mv1
&& mv1
!= error_mark_node
610 && TREE_CODE (mv1
) != ARRAY_TYPE
)
611 mv1
= TYPE_MAIN_VARIANT (mv1
);
612 for (memb
= TYPE_FIELDS (TREE_VALUE (p2
));
613 memb
; memb
= DECL_CHAIN (memb
))
615 tree mv3
= TREE_TYPE (memb
);
616 if (mv3
&& mv3
!= error_mark_node
617 && TREE_CODE (mv3
) != ARRAY_TYPE
)
618 mv3
= TYPE_MAIN_VARIANT (mv3
);
619 if (comptypes (mv3
, mv1
))
621 TREE_VALUE (n
) = composite_type (TREE_TYPE (memb
),
623 pedwarn (input_location
, OPT_Wpedantic
,
624 "function types not truly compatible in ISO C");
629 TREE_VALUE (n
) = composite_type (TREE_VALUE (p1
), TREE_VALUE (p2
));
633 t1
= build_function_type (valtype
, newargs
);
634 t1
= qualify_type (t1
, t2
);
635 /* ... falls through ... */
639 return build_type_attribute_variant (t1
, attributes
);
644 /* Return the type of a conditional expression between pointers to
645 possibly differently qualified versions of compatible types.
647 We assume that comp_target_types has already been done and returned
648 nonzero; if that isn't so, this may crash. */
651 common_pointer_type (tree t1
, tree t2
)
654 tree pointed_to_1
, mv1
;
655 tree pointed_to_2
, mv2
;
657 unsigned target_quals
;
658 addr_space_t as1
, as2
, as_common
;
661 /* Save time if the two types are the same. */
663 if (t1
== t2
) return t1
;
665 /* If one type is nonsense, use the other. */
666 if (t1
== error_mark_node
)
668 if (t2
== error_mark_node
)
671 gcc_assert (TREE_CODE (t1
) == POINTER_TYPE
672 && TREE_CODE (t2
) == POINTER_TYPE
);
674 /* Merge the attributes. */
675 attributes
= targetm
.merge_type_attributes (t1
, t2
);
677 /* Find the composite type of the target types, and combine the
678 qualifiers of the two types' targets. Do not lose qualifiers on
679 array element types by taking the TYPE_MAIN_VARIANT. */
680 mv1
= pointed_to_1
= TREE_TYPE (t1
);
681 mv2
= pointed_to_2
= TREE_TYPE (t2
);
682 if (TREE_CODE (mv1
) != ARRAY_TYPE
)
683 mv1
= TYPE_MAIN_VARIANT (pointed_to_1
);
684 if (TREE_CODE (mv2
) != ARRAY_TYPE
)
685 mv2
= TYPE_MAIN_VARIANT (pointed_to_2
);
686 target
= composite_type (mv1
, mv2
);
688 /* Strip array types to get correct qualifier for pointers to arrays */
689 quals1
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1
));
690 quals2
= TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2
));
692 /* For function types do not merge const qualifiers, but drop them
693 if used inconsistently. The middle-end uses these to mark const
694 and noreturn functions. */
695 if (TREE_CODE (pointed_to_1
) == FUNCTION_TYPE
)
696 target_quals
= (quals1
& quals2
);
698 target_quals
= (quals1
| quals2
);
700 /* If the two named address spaces are different, determine the common
701 superset address space. This is guaranteed to exist due to the
702 assumption that comp_target_type returned non-zero. */
703 as1
= TYPE_ADDR_SPACE (pointed_to_1
);
704 as2
= TYPE_ADDR_SPACE (pointed_to_2
);
705 if (!addr_space_superset (as1
, as2
, &as_common
))
708 target_quals
|= ENCODE_QUAL_ADDR_SPACE (as_common
);
710 t1
= build_pointer_type (c_build_qualified_type (target
, target_quals
));
711 return build_type_attribute_variant (t1
, attributes
);
714 /* Return the common type for two arithmetic types under the usual
715 arithmetic conversions. The default conversions have already been
716 applied, and enumerated types converted to their compatible integer
717 types. The resulting type is unqualified and has no attributes.
719 This is the type for the result of most arithmetic operations
720 if the operands have the given two types. */
723 c_common_type (tree t1
, tree t2
)
725 enum tree_code code1
;
726 enum tree_code code2
;
728 /* If one type is nonsense, use the other. */
729 if (t1
== error_mark_node
)
731 if (t2
== error_mark_node
)
734 if (TYPE_QUALS (t1
) != TYPE_UNQUALIFIED
)
735 t1
= TYPE_MAIN_VARIANT (t1
);
737 if (TYPE_QUALS (t2
) != TYPE_UNQUALIFIED
)
738 t2
= TYPE_MAIN_VARIANT (t2
);
740 if (TYPE_ATTRIBUTES (t1
) != NULL_TREE
)
741 t1
= build_type_attribute_variant (t1
, NULL_TREE
);
743 if (TYPE_ATTRIBUTES (t2
) != NULL_TREE
)
744 t2
= build_type_attribute_variant (t2
, NULL_TREE
);
746 /* Save time if the two types are the same. */
748 if (t1
== t2
) return t1
;
750 code1
= TREE_CODE (t1
);
751 code2
= TREE_CODE (t2
);
753 gcc_assert (code1
== VECTOR_TYPE
|| code1
== COMPLEX_TYPE
754 || code1
== FIXED_POINT_TYPE
|| code1
== REAL_TYPE
755 || code1
== INTEGER_TYPE
);
756 gcc_assert (code2
== VECTOR_TYPE
|| code2
== COMPLEX_TYPE
757 || code2
== FIXED_POINT_TYPE
|| code2
== REAL_TYPE
758 || code2
== INTEGER_TYPE
);
760 /* When one operand is a decimal float type, the other operand cannot be
761 a generic float type or a complex type. We also disallow vector types
763 if ((DECIMAL_FLOAT_TYPE_P (t1
) || DECIMAL_FLOAT_TYPE_P (t2
))
764 && !(DECIMAL_FLOAT_TYPE_P (t1
) && DECIMAL_FLOAT_TYPE_P (t2
)))
766 if (code1
== VECTOR_TYPE
|| code2
== VECTOR_TYPE
)
768 error ("can%'t mix operands of decimal float and vector types");
769 return error_mark_node
;
771 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
773 error ("can%'t mix operands of decimal float and complex types");
774 return error_mark_node
;
776 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
778 error ("can%'t mix operands of decimal float and other float types");
779 return error_mark_node
;
783 /* If one type is a vector type, return that type. (How the usual
784 arithmetic conversions apply to the vector types extension is not
785 precisely specified.) */
786 if (code1
== VECTOR_TYPE
)
789 if (code2
== VECTOR_TYPE
)
792 /* If one type is complex, form the common type of the non-complex
793 components, then make that complex. Use T1 or T2 if it is the
795 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
797 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
798 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
799 tree subtype
= c_common_type (subtype1
, subtype2
);
801 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
803 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
806 return build_complex_type (subtype
);
809 /* If only one is real, use it as the result. */
811 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
814 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
817 /* If both are real and either are decimal floating point types, use
818 the decimal floating point type with the greater precision. */
820 if (code1
== REAL_TYPE
&& code2
== REAL_TYPE
)
822 if (TYPE_MAIN_VARIANT (t1
) == dfloat128_type_node
823 || TYPE_MAIN_VARIANT (t2
) == dfloat128_type_node
)
824 return dfloat128_type_node
;
825 else if (TYPE_MAIN_VARIANT (t1
) == dfloat64_type_node
826 || TYPE_MAIN_VARIANT (t2
) == dfloat64_type_node
)
827 return dfloat64_type_node
;
828 else if (TYPE_MAIN_VARIANT (t1
) == dfloat32_type_node
829 || TYPE_MAIN_VARIANT (t2
) == dfloat32_type_node
)
830 return dfloat32_type_node
;
833 /* Deal with fixed-point types. */
834 if (code1
== FIXED_POINT_TYPE
|| code2
== FIXED_POINT_TYPE
)
836 unsigned int unsignedp
= 0, satp
= 0;
838 unsigned int fbit1
, ibit1
, fbit2
, ibit2
, max_fbit
, max_ibit
;
843 /* If one input type is saturating, the result type is saturating. */
844 if (TYPE_SATURATING (t1
) || TYPE_SATURATING (t2
))
847 /* If both fixed-point types are unsigned, the result type is unsigned.
848 When mixing fixed-point and integer types, follow the sign of the
850 Otherwise, the result type is signed. */
851 if ((TYPE_UNSIGNED (t1
) && TYPE_UNSIGNED (t2
)
852 && code1
== FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
)
853 || (code1
== FIXED_POINT_TYPE
&& code2
!= FIXED_POINT_TYPE
854 && TYPE_UNSIGNED (t1
))
855 || (code1
!= FIXED_POINT_TYPE
&& code2
== FIXED_POINT_TYPE
856 && TYPE_UNSIGNED (t2
)))
859 /* The result type is signed. */
862 /* If the input type is unsigned, we need to convert to the
864 if (code1
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t1
))
866 enum mode_class mclass
= (enum mode_class
) 0;
867 if (GET_MODE_CLASS (m1
) == MODE_UFRACT
)
869 else if (GET_MODE_CLASS (m1
) == MODE_UACCUM
)
873 m1
= mode_for_size (GET_MODE_PRECISION (m1
), mclass
, 0);
875 if (code2
== FIXED_POINT_TYPE
&& TYPE_UNSIGNED (t2
))
877 enum mode_class mclass
= (enum mode_class
) 0;
878 if (GET_MODE_CLASS (m2
) == MODE_UFRACT
)
880 else if (GET_MODE_CLASS (m2
) == MODE_UACCUM
)
884 m2
= mode_for_size (GET_MODE_PRECISION (m2
), mclass
, 0);
888 if (code1
== FIXED_POINT_TYPE
)
890 fbit1
= GET_MODE_FBIT (m1
);
891 ibit1
= GET_MODE_IBIT (m1
);
896 /* Signed integers need to subtract one sign bit. */
897 ibit1
= TYPE_PRECISION (t1
) - (!TYPE_UNSIGNED (t1
));
900 if (code2
== FIXED_POINT_TYPE
)
902 fbit2
= GET_MODE_FBIT (m2
);
903 ibit2
= GET_MODE_IBIT (m2
);
908 /* Signed integers need to subtract one sign bit. */
909 ibit2
= TYPE_PRECISION (t2
) - (!TYPE_UNSIGNED (t2
));
912 max_ibit
= ibit1
>= ibit2
? ibit1
: ibit2
;
913 max_fbit
= fbit1
>= fbit2
? fbit1
: fbit2
;
914 return c_common_fixed_point_type_for_size (max_ibit
, max_fbit
, unsignedp
,
918 /* Both real or both integers; use the one with greater precision. */
920 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
922 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
925 /* Same precision. Prefer long longs to longs to ints when the
926 same precision, following the C99 rules on integer type rank
927 (which are equivalent to the C90 rules for C90 types). */
929 if (TYPE_MAIN_VARIANT (t1
) == long_long_unsigned_type_node
930 || TYPE_MAIN_VARIANT (t2
) == long_long_unsigned_type_node
)
931 return long_long_unsigned_type_node
;
933 if (TYPE_MAIN_VARIANT (t1
) == long_long_integer_type_node
934 || TYPE_MAIN_VARIANT (t2
) == long_long_integer_type_node
)
936 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
937 return long_long_unsigned_type_node
;
939 return long_long_integer_type_node
;
942 if (TYPE_MAIN_VARIANT (t1
) == long_unsigned_type_node
943 || TYPE_MAIN_VARIANT (t2
) == long_unsigned_type_node
)
944 return long_unsigned_type_node
;
946 if (TYPE_MAIN_VARIANT (t1
) == long_integer_type_node
947 || TYPE_MAIN_VARIANT (t2
) == long_integer_type_node
)
949 /* But preserve unsignedness from the other type,
950 since long cannot hold all the values of an unsigned int. */
951 if (TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
952 return long_unsigned_type_node
;
954 return long_integer_type_node
;
957 /* Likewise, prefer long double to double even if same size. */
958 if (TYPE_MAIN_VARIANT (t1
) == long_double_type_node
959 || TYPE_MAIN_VARIANT (t2
) == long_double_type_node
)
960 return long_double_type_node
;
962 /* Likewise, prefer double to float even if same size.
963 We got a couple of embedded targets with 32 bit doubles, and the
964 pdp11 might have 64 bit floats. */
965 if (TYPE_MAIN_VARIANT (t1
) == double_type_node
966 || TYPE_MAIN_VARIANT (t2
) == double_type_node
)
967 return double_type_node
;
969 /* Otherwise prefer the unsigned one. */
971 if (TYPE_UNSIGNED (t1
))
977 /* Wrapper around c_common_type that is used by c-common.c and other
978 front end optimizations that remove promotions. ENUMERAL_TYPEs
979 are allowed here and are converted to their compatible integer types.
980 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
981 preferably a non-Boolean type as the common type. */
983 common_type (tree t1
, tree t2
)
985 if (TREE_CODE (t1
) == ENUMERAL_TYPE
)
986 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), 1);
987 if (TREE_CODE (t2
) == ENUMERAL_TYPE
)
988 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), 1);
990 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
991 if (TREE_CODE (t1
) == BOOLEAN_TYPE
992 && TREE_CODE (t2
) == BOOLEAN_TYPE
)
993 return boolean_type_node
;
995 /* If either type is BOOLEAN_TYPE, then return the other. */
996 if (TREE_CODE (t1
) == BOOLEAN_TYPE
)
998 if (TREE_CODE (t2
) == BOOLEAN_TYPE
)
1001 return c_common_type (t1
, t2
);
1004 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1005 or various other operations. Return 2 if they are compatible
1006 but a warning may be needed if you use them together. */
1009 comptypes (tree type1
, tree type2
)
1011 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1014 val
= comptypes_internal (type1
, type2
, NULL
, NULL
);
1015 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1020 /* Like comptypes, but if it returns non-zero because enum and int are
1021 compatible, it sets *ENUM_AND_INT_P to true. */
1024 comptypes_check_enum_int (tree type1
, tree type2
, bool *enum_and_int_p
)
1026 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1029 val
= comptypes_internal (type1
, type2
, enum_and_int_p
, NULL
);
1030 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1035 /* Like comptypes, but if it returns nonzero for different types, it
1036 sets *DIFFERENT_TYPES_P to true. */
1039 comptypes_check_different_types (tree type1
, tree type2
,
1040 bool *different_types_p
)
1042 const struct tagged_tu_seen_cache
* tagged_tu_seen_base1
= tagged_tu_seen_base
;
1045 val
= comptypes_internal (type1
, type2
, NULL
, different_types_p
);
1046 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1
);
1051 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1052 or various other operations. Return 2 if they are compatible
1053 but a warning may be needed if you use them together. If
1054 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1055 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1056 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1057 NULL, and the types are compatible but different enough not to be
1058 permitted in C11 typedef redeclarations, then this sets
1059 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1060 false, but may or may not be set if the types are incompatible.
1061 This differs from comptypes, in that we don't free the seen
1065 comptypes_internal (const_tree type1
, const_tree type2
, bool *enum_and_int_p
,
1066 bool *different_types_p
)
1068 const_tree t1
= type1
;
1069 const_tree t2
= type2
;
1072 /* Suppress errors caused by previously reported errors. */
1074 if (t1
== t2
|| !t1
|| !t2
1075 || TREE_CODE (t1
) == ERROR_MARK
|| TREE_CODE (t2
) == ERROR_MARK
)
1078 /* Enumerated types are compatible with integer types, but this is
1079 not transitive: two enumerated types in the same translation unit
1080 are compatible with each other only if they are the same type. */
1082 if (TREE_CODE (t1
) == ENUMERAL_TYPE
&& TREE_CODE (t2
) != ENUMERAL_TYPE
)
1084 t1
= c_common_type_for_size (TYPE_PRECISION (t1
), TYPE_UNSIGNED (t1
));
1085 if (TREE_CODE (t2
) != VOID_TYPE
)
1087 if (enum_and_int_p
!= NULL
)
1088 *enum_and_int_p
= true;
1089 if (different_types_p
!= NULL
)
1090 *different_types_p
= true;
1093 else if (TREE_CODE (t2
) == ENUMERAL_TYPE
&& TREE_CODE (t1
) != ENUMERAL_TYPE
)
1095 t2
= c_common_type_for_size (TYPE_PRECISION (t2
), TYPE_UNSIGNED (t2
));
1096 if (TREE_CODE (t1
) != VOID_TYPE
)
1098 if (enum_and_int_p
!= NULL
)
1099 *enum_and_int_p
= true;
1100 if (different_types_p
!= NULL
)
1101 *different_types_p
= true;
1108 /* Different classes of types can't be compatible. */
1110 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1113 /* Qualifiers must match. C99 6.7.3p9 */
1115 if (TYPE_QUALS (t1
) != TYPE_QUALS (t2
))
1118 /* Allow for two different type nodes which have essentially the same
1119 definition. Note that we already checked for equality of the type
1120 qualifiers (just above). */
1122 if (TREE_CODE (t1
) != ARRAY_TYPE
1123 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1126 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1127 if (!(attrval
= comp_type_attributes (t1
, t2
)))
1130 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1133 switch (TREE_CODE (t1
))
1136 /* Do not remove mode or aliasing information. */
1137 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1138 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
))
1140 val
= (TREE_TYPE (t1
) == TREE_TYPE (t2
)
1141 ? 1 : comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1142 enum_and_int_p
, different_types_p
));
1146 val
= function_types_compatible_p (t1
, t2
, enum_and_int_p
,
1152 tree d1
= TYPE_DOMAIN (t1
);
1153 tree d2
= TYPE_DOMAIN (t2
);
1154 bool d1_variable
, d2_variable
;
1155 bool d1_zero
, d2_zero
;
1158 /* Target types must match incl. qualifiers. */
1159 if (TREE_TYPE (t1
) != TREE_TYPE (t2
)
1160 && 0 == (val
= comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1162 different_types_p
)))
1165 if (different_types_p
!= NULL
1166 && (d1
== 0) != (d2
== 0))
1167 *different_types_p
= true;
1168 /* Sizes must match unless one is missing or variable. */
1169 if (d1
== 0 || d2
== 0 || d1
== d2
)
1172 d1_zero
= !TYPE_MAX_VALUE (d1
);
1173 d2_zero
= !TYPE_MAX_VALUE (d2
);
1175 d1_variable
= (!d1_zero
1176 && (TREE_CODE (TYPE_MIN_VALUE (d1
)) != INTEGER_CST
1177 || TREE_CODE (TYPE_MAX_VALUE (d1
)) != INTEGER_CST
));
1178 d2_variable
= (!d2_zero
1179 && (TREE_CODE (TYPE_MIN_VALUE (d2
)) != INTEGER_CST
1180 || TREE_CODE (TYPE_MAX_VALUE (d2
)) != INTEGER_CST
));
1181 d1_variable
= d1_variable
|| (d1_zero
&& c_vla_type_p (t1
));
1182 d2_variable
= d2_variable
|| (d2_zero
&& c_vla_type_p (t2
));
1184 if (different_types_p
!= NULL
1185 && d1_variable
!= d2_variable
)
1186 *different_types_p
= true;
1187 if (d1_variable
|| d2_variable
)
1189 if (d1_zero
&& d2_zero
)
1191 if (d1_zero
|| d2_zero
1192 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
))
1193 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1
), TYPE_MAX_VALUE (d2
)))
1202 if (val
!= 1 && !same_translation_unit_p (t1
, t2
))
1204 tree a1
= TYPE_ATTRIBUTES (t1
);
1205 tree a2
= TYPE_ATTRIBUTES (t2
);
1207 if (! attribute_list_contained (a1
, a2
)
1208 && ! attribute_list_contained (a2
, a1
))
1212 return tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1214 val
= tagged_types_tu_compatible_p (t1
, t2
, enum_and_int_p
,
1220 val
= (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
1221 && comptypes_internal (TREE_TYPE (t1
), TREE_TYPE (t2
),
1222 enum_and_int_p
, different_types_p
));
1228 return attrval
== 2 && val
== 1 ? 2 : val
;
1231 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1232 their qualifiers, except for named address spaces. If the pointers point to
1233 different named addresses, then we must determine if one address space is a
1234 subset of the other. */
1237 comp_target_types (location_t location
, tree ttl
, tree ttr
)
1241 tree mvl
= TREE_TYPE (ttl
);
1242 tree mvr
= TREE_TYPE (ttr
);
1243 addr_space_t asl
= TYPE_ADDR_SPACE (mvl
);
1244 addr_space_t asr
= TYPE_ADDR_SPACE (mvr
);
1245 addr_space_t as_common
;
1246 bool enum_and_int_p
;
1248 /* Fail if pointers point to incompatible address spaces. */
1249 if (!addr_space_superset (asl
, asr
, &as_common
))
1252 /* For pedantic record result of comptypes on arrays before losing
1253 qualifiers on the element type below. */
1256 if (TREE_CODE (mvl
) == ARRAY_TYPE
1257 && TREE_CODE (mvr
) == ARRAY_TYPE
)
1258 val_ped
= comptypes (mvl
, mvr
);
1260 /* Qualifiers on element types of array types that are
1261 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1263 mvl
= (TYPE_ATOMIC (strip_array_types (mvl
))
1264 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
), TYPE_QUAL_ATOMIC
)
1265 : TYPE_MAIN_VARIANT (mvl
));
1267 mvr
= (TYPE_ATOMIC (strip_array_types (mvr
))
1268 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
), TYPE_QUAL_ATOMIC
)
1269 : TYPE_MAIN_VARIANT (mvr
));
1271 enum_and_int_p
= false;
1272 val
= comptypes_check_enum_int (mvl
, mvr
, &enum_and_int_p
);
1274 if (val
== 1 && val_ped
!= 1)
1275 pedwarn (location
, OPT_Wpedantic
, "pointers to arrays with different qualifiers "
1276 "are incompatible in ISO C");
1279 pedwarn (location
, OPT_Wpedantic
, "types are not quite compatible");
1281 if (val
== 1 && enum_and_int_p
&& warn_cxx_compat
)
1282 warning_at (location
, OPT_Wc___compat
,
1283 "pointer target types incompatible in C++");
1288 /* Subroutines of `comptypes'. */
1290 /* Determine whether two trees derive from the same translation unit.
1291 If the CONTEXT chain ends in a null, that tree's context is still
1292 being parsed, so if two trees have context chains ending in null,
1293 they're in the same translation unit. */
1295 same_translation_unit_p (const_tree t1
, const_tree t2
)
1297 while (t1
&& TREE_CODE (t1
) != TRANSLATION_UNIT_DECL
)
1298 switch (TREE_CODE_CLASS (TREE_CODE (t1
)))
1300 case tcc_declaration
:
1301 t1
= DECL_CONTEXT (t1
); break;
1303 t1
= TYPE_CONTEXT (t1
); break;
1304 case tcc_exceptional
:
1305 t1
= BLOCK_SUPERCONTEXT (t1
); break; /* assume block */
1306 default: gcc_unreachable ();
1309 while (t2
&& TREE_CODE (t2
) != TRANSLATION_UNIT_DECL
)
1310 switch (TREE_CODE_CLASS (TREE_CODE (t2
)))
1312 case tcc_declaration
:
1313 t2
= DECL_CONTEXT (t2
); break;
1315 t2
= TYPE_CONTEXT (t2
); break;
1316 case tcc_exceptional
:
1317 t2
= BLOCK_SUPERCONTEXT (t2
); break; /* assume block */
1318 default: gcc_unreachable ();
1324 /* Allocate the seen two types, assuming that they are compatible. */
1326 static struct tagged_tu_seen_cache
*
1327 alloc_tagged_tu_seen_cache (const_tree t1
, const_tree t2
)
1329 struct tagged_tu_seen_cache
*tu
= XNEW (struct tagged_tu_seen_cache
);
1330 tu
->next
= tagged_tu_seen_base
;
1334 tagged_tu_seen_base
= tu
;
1336 /* The C standard says that two structures in different translation
1337 units are compatible with each other only if the types of their
1338 fields are compatible (among other things). We assume that they
1339 are compatible until proven otherwise when building the cache.
1340 An example where this can occur is:
1345 If we are comparing this against a similar struct in another TU,
1346 and did not assume they were compatible, we end up with an infinite
1352 /* Free the seen types until we get to TU_TIL. */
1355 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache
*tu_til
)
1357 const struct tagged_tu_seen_cache
*tu
= tagged_tu_seen_base
;
1358 while (tu
!= tu_til
)
1360 const struct tagged_tu_seen_cache
*const tu1
1361 = (const struct tagged_tu_seen_cache
*) tu
;
1363 free (CONST_CAST (struct tagged_tu_seen_cache
*, tu1
));
1365 tagged_tu_seen_base
= tu_til
;
1368 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1369 compatible. If the two types are not the same (which has been
1370 checked earlier), this can only happen when multiple translation
1371 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1372 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1373 comptypes_internal. */
1376 tagged_types_tu_compatible_p (const_tree t1
, const_tree t2
,
1377 bool *enum_and_int_p
, bool *different_types_p
)
1380 bool needs_warning
= false;
1382 /* We have to verify that the tags of the types are the same. This
1383 is harder than it looks because this may be a typedef, so we have
1384 to go look at the original type. It may even be a typedef of a
1386 In the case of compiler-created builtin structs the TYPE_DECL
1387 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1388 while (TYPE_NAME (t1
)
1389 && TREE_CODE (TYPE_NAME (t1
)) == TYPE_DECL
1390 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1
)))
1391 t1
= DECL_ORIGINAL_TYPE (TYPE_NAME (t1
));
1393 while (TYPE_NAME (t2
)
1394 && TREE_CODE (TYPE_NAME (t2
)) == TYPE_DECL
1395 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2
)))
1396 t2
= DECL_ORIGINAL_TYPE (TYPE_NAME (t2
));
1398 /* C90 didn't have the requirement that the two tags be the same. */
1399 if (flag_isoc99
&& TYPE_NAME (t1
) != TYPE_NAME (t2
))
1402 /* C90 didn't say what happened if one or both of the types were
1403 incomplete; we choose to follow C99 rules here, which is that they
1405 if (TYPE_SIZE (t1
) == NULL
1406 || TYPE_SIZE (t2
) == NULL
)
1410 const struct tagged_tu_seen_cache
* tts_i
;
1411 for (tts_i
= tagged_tu_seen_base
; tts_i
!= NULL
; tts_i
= tts_i
->next
)
1412 if (tts_i
->t1
== t1
&& tts_i
->t2
== t2
)
1416 switch (TREE_CODE (t1
))
1420 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1421 /* Speed up the case where the type values are in the same order. */
1422 tree tv1
= TYPE_VALUES (t1
);
1423 tree tv2
= TYPE_VALUES (t2
);
1430 for (;tv1
&& tv2
; tv1
= TREE_CHAIN (tv1
), tv2
= TREE_CHAIN (tv2
))
1432 if (TREE_PURPOSE (tv1
) != TREE_PURPOSE (tv2
))
1434 if (simple_cst_equal (TREE_VALUE (tv1
), TREE_VALUE (tv2
)) != 1)
1441 if (tv1
== NULL_TREE
&& tv2
== NULL_TREE
)
1445 if (tv1
== NULL_TREE
|| tv2
== NULL_TREE
)
1451 if (list_length (TYPE_VALUES (t1
)) != list_length (TYPE_VALUES (t2
)))
1457 for (s1
= TYPE_VALUES (t1
); s1
; s1
= TREE_CHAIN (s1
))
1459 s2
= purpose_member (TREE_PURPOSE (s1
), TYPE_VALUES (t2
));
1461 || simple_cst_equal (TREE_VALUE (s1
), TREE_VALUE (s2
)) != 1)
1472 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1473 if (list_length (TYPE_FIELDS (t1
)) != list_length (TYPE_FIELDS (t2
)))
1479 /* Speed up the common case where the fields are in the same order. */
1480 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
); s1
&& s2
;
1481 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1485 if (DECL_NAME (s1
) != DECL_NAME (s2
))
1487 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1488 enum_and_int_p
, different_types_p
);
1490 if (result
!= 1 && !DECL_NAME (s1
))
1498 needs_warning
= true;
1500 if (TREE_CODE (s1
) == FIELD_DECL
1501 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1502 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1510 tu
->val
= needs_warning
? 2 : 1;
1514 for (s1
= TYPE_FIELDS (t1
); s1
; s1
= DECL_CHAIN (s1
))
1518 for (s2
= TYPE_FIELDS (t2
); s2
; s2
= DECL_CHAIN (s2
))
1519 if (DECL_NAME (s1
) == DECL_NAME (s2
))
1523 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1527 if (result
!= 1 && !DECL_NAME (s1
))
1535 needs_warning
= true;
1537 if (TREE_CODE (s1
) == FIELD_DECL
1538 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1539 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1551 tu
->val
= needs_warning
? 2 : 10;
1557 struct tagged_tu_seen_cache
*tu
= alloc_tagged_tu_seen_cache (t1
, t2
);
1559 for (s1
= TYPE_FIELDS (t1
), s2
= TYPE_FIELDS (t2
);
1561 s1
= DECL_CHAIN (s1
), s2
= DECL_CHAIN (s2
))
1564 if (TREE_CODE (s1
) != TREE_CODE (s2
)
1565 || DECL_NAME (s1
) != DECL_NAME (s2
))
1567 result
= comptypes_internal (TREE_TYPE (s1
), TREE_TYPE (s2
),
1568 enum_and_int_p
, different_types_p
);
1572 needs_warning
= true;
1574 if (TREE_CODE (s1
) == FIELD_DECL
1575 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1
),
1576 DECL_FIELD_BIT_OFFSET (s2
)) != 1)
1582 tu
->val
= needs_warning
? 2 : 1;
1591 /* Return 1 if two function types F1 and F2 are compatible.
1592 If either type specifies no argument types,
1593 the other must specify a fixed number of self-promoting arg types.
1594 Otherwise, if one type specifies only the number of arguments,
1595 the other must specify that number of self-promoting arg types.
1596 Otherwise, the argument types must match.
1597 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1600 function_types_compatible_p (const_tree f1
, const_tree f2
,
1601 bool *enum_and_int_p
, bool *different_types_p
)
1604 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1609 ret1
= TREE_TYPE (f1
);
1610 ret2
= TREE_TYPE (f2
);
1612 /* 'volatile' qualifiers on a function's return type used to mean
1613 the function is noreturn. */
1614 if (TYPE_VOLATILE (ret1
) != TYPE_VOLATILE (ret2
))
1615 pedwarn (input_location
, 0, "function return types not compatible due to %<volatile%>");
1616 if (TYPE_VOLATILE (ret1
))
1617 ret1
= build_qualified_type (TYPE_MAIN_VARIANT (ret1
),
1618 TYPE_QUALS (ret1
) & ~TYPE_QUAL_VOLATILE
);
1619 if (TYPE_VOLATILE (ret2
))
1620 ret2
= build_qualified_type (TYPE_MAIN_VARIANT (ret2
),
1621 TYPE_QUALS (ret2
) & ~TYPE_QUAL_VOLATILE
);
1622 val
= comptypes_internal (ret1
, ret2
, enum_and_int_p
, different_types_p
);
1626 args1
= TYPE_ARG_TYPES (f1
);
1627 args2
= TYPE_ARG_TYPES (f2
);
1629 if (different_types_p
!= NULL
1630 && (args1
== 0) != (args2
== 0))
1631 *different_types_p
= true;
1633 /* An unspecified parmlist matches any specified parmlist
1634 whose argument types don't need default promotions. */
1638 if (!self_promoting_args_p (args2
))
1640 /* If one of these types comes from a non-prototype fn definition,
1641 compare that with the other type's arglist.
1642 If they don't match, ask for a warning (but no error). */
1643 if (TYPE_ACTUAL_ARG_TYPES (f1
)
1644 && 1 != type_lists_compatible_p (args2
, TYPE_ACTUAL_ARG_TYPES (f1
),
1645 enum_and_int_p
, different_types_p
))
1651 if (!self_promoting_args_p (args1
))
1653 if (TYPE_ACTUAL_ARG_TYPES (f2
)
1654 && 1 != type_lists_compatible_p (args1
, TYPE_ACTUAL_ARG_TYPES (f2
),
1655 enum_and_int_p
, different_types_p
))
1660 /* Both types have argument lists: compare them and propagate results. */
1661 val1
= type_lists_compatible_p (args1
, args2
, enum_and_int_p
,
1663 return val1
!= 1 ? val1
: val
;
1666 /* Check two lists of types for compatibility, returning 0 for
1667 incompatible, 1 for compatible, or 2 for compatible with
1668 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1669 comptypes_internal. */
1672 type_lists_compatible_p (const_tree args1
, const_tree args2
,
1673 bool *enum_and_int_p
, bool *different_types_p
)
1675 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1681 tree a1
, mv1
, a2
, mv2
;
1682 if (args1
== 0 && args2
== 0)
1684 /* If one list is shorter than the other,
1685 they fail to match. */
1686 if (args1
== 0 || args2
== 0)
1688 mv1
= a1
= TREE_VALUE (args1
);
1689 mv2
= a2
= TREE_VALUE (args2
);
1690 if (mv1
&& mv1
!= error_mark_node
&& TREE_CODE (mv1
) != ARRAY_TYPE
)
1691 mv1
= (TYPE_ATOMIC (mv1
)
1692 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1
),
1694 : TYPE_MAIN_VARIANT (mv1
));
1695 if (mv2
&& mv2
!= error_mark_node
&& TREE_CODE (mv2
) != ARRAY_TYPE
)
1696 mv2
= (TYPE_ATOMIC (mv2
)
1697 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2
),
1699 : TYPE_MAIN_VARIANT (mv2
));
1700 /* A null pointer instead of a type
1701 means there is supposed to be an argument
1702 but nothing is specified about what type it has.
1703 So match anything that self-promotes. */
1704 if (different_types_p
!= NULL
1705 && (a1
== 0) != (a2
== 0))
1706 *different_types_p
= true;
1709 if (c_type_promotes_to (a2
) != a2
)
1714 if (c_type_promotes_to (a1
) != a1
)
1717 /* If one of the lists has an error marker, ignore this arg. */
1718 else if (TREE_CODE (a1
) == ERROR_MARK
1719 || TREE_CODE (a2
) == ERROR_MARK
)
1721 else if (!(newval
= comptypes_internal (mv1
, mv2
, enum_and_int_p
,
1722 different_types_p
)))
1724 if (different_types_p
!= NULL
)
1725 *different_types_p
= true;
1726 /* Allow wait (union {union wait *u; int *i} *)
1727 and wait (union wait *) to be compatible. */
1728 if (TREE_CODE (a1
) == UNION_TYPE
1729 && (TYPE_NAME (a1
) == 0
1730 || TYPE_TRANSPARENT_AGGR (a1
))
1731 && TREE_CODE (TYPE_SIZE (a1
)) == INTEGER_CST
1732 && tree_int_cst_equal (TYPE_SIZE (a1
),
1736 for (memb
= TYPE_FIELDS (a1
);
1737 memb
; memb
= DECL_CHAIN (memb
))
1739 tree mv3
= TREE_TYPE (memb
);
1740 if (mv3
&& mv3
!= error_mark_node
1741 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1742 mv3
= (TYPE_ATOMIC (mv3
)
1743 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1745 : TYPE_MAIN_VARIANT (mv3
));
1746 if (comptypes_internal (mv3
, mv2
, enum_and_int_p
,
1753 else if (TREE_CODE (a2
) == UNION_TYPE
1754 && (TYPE_NAME (a2
) == 0
1755 || TYPE_TRANSPARENT_AGGR (a2
))
1756 && TREE_CODE (TYPE_SIZE (a2
)) == INTEGER_CST
1757 && tree_int_cst_equal (TYPE_SIZE (a2
),
1761 for (memb
= TYPE_FIELDS (a2
);
1762 memb
; memb
= DECL_CHAIN (memb
))
1764 tree mv3
= TREE_TYPE (memb
);
1765 if (mv3
&& mv3
!= error_mark_node
1766 && TREE_CODE (mv3
) != ARRAY_TYPE
)
1767 mv3
= (TYPE_ATOMIC (mv3
)
1768 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3
),
1770 : TYPE_MAIN_VARIANT (mv3
));
1771 if (comptypes_internal (mv3
, mv1
, enum_and_int_p
,
1782 /* comptypes said ok, but record if it said to warn. */
1786 args1
= TREE_CHAIN (args1
);
1787 args2
= TREE_CHAIN (args2
);
1791 /* Compute the size to increment a pointer by. When a function type or void
1792 type or incomplete type is passed, size_one_node is returned.
1793 This function does not emit any diagnostics; the caller is responsible
1797 c_size_in_bytes (const_tree type
)
1799 enum tree_code code
= TREE_CODE (type
);
1801 if (code
== FUNCTION_TYPE
|| code
== VOID_TYPE
|| code
== ERROR_MARK
1802 || !COMPLETE_TYPE_P (type
))
1803 return size_one_node
;
1805 /* Convert in case a char is more than one unit. */
1806 return size_binop_loc (input_location
, CEIL_DIV_EXPR
, TYPE_SIZE_UNIT (type
),
1807 size_int (TYPE_PRECISION (char_type_node
)
1811 /* Return either DECL or its known constant value (if it has one). */
1814 decl_constant_value (tree decl
)
1816 if (/* Don't change a variable array bound or initial value to a constant
1817 in a place where a variable is invalid. Note that DECL_INITIAL
1818 isn't valid for a PARM_DECL. */
1819 current_function_decl
!= 0
1820 && TREE_CODE (decl
) != PARM_DECL
1821 && !TREE_THIS_VOLATILE (decl
)
1822 && TREE_READONLY (decl
)
1823 && DECL_INITIAL (decl
) != 0
1824 && TREE_CODE (DECL_INITIAL (decl
)) != ERROR_MARK
1825 /* This is invalid if initial value is not constant.
1826 If it has either a function call, a memory reference,
1827 or a variable, then re-evaluating it could give different results. */
1828 && TREE_CONSTANT (DECL_INITIAL (decl
))
1829 /* Check for cases where this is sub-optimal, even though valid. */
1830 && TREE_CODE (DECL_INITIAL (decl
)) != CONSTRUCTOR
)
1831 return DECL_INITIAL (decl
);
1835 /* Convert the array expression EXP to a pointer. */
1837 array_to_pointer_conversion (location_t loc
, tree exp
)
1839 tree orig_exp
= exp
;
1840 tree type
= TREE_TYPE (exp
);
1842 tree restype
= TREE_TYPE (type
);
1845 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1847 STRIP_TYPE_NOPS (exp
);
1849 if (TREE_NO_WARNING (orig_exp
))
1850 TREE_NO_WARNING (exp
) = 1;
1852 ptrtype
= build_pointer_type (restype
);
1854 if (TREE_CODE (exp
) == INDIRECT_REF
)
1855 return convert (ptrtype
, TREE_OPERAND (exp
, 0));
1857 /* In C++ array compound literals are temporary objects unless they are
1858 const or appear in namespace scope, so they are destroyed too soon
1859 to use them for much of anything (c++/53220). */
1860 if (warn_cxx_compat
&& TREE_CODE (exp
) == COMPOUND_LITERAL_EXPR
)
1862 tree decl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1863 if (!TREE_READONLY (decl
) && !TREE_STATIC (decl
))
1864 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wc___compat
,
1865 "converting an array compound literal to a pointer "
1866 "is ill-formed in C++");
1869 adr
= build_unary_op (loc
, ADDR_EXPR
, exp
, 1);
1870 return convert (ptrtype
, adr
);
1873 /* Convert the function expression EXP to a pointer. */
1875 function_to_pointer_conversion (location_t loc
, tree exp
)
1877 tree orig_exp
= exp
;
1879 gcc_assert (TREE_CODE (TREE_TYPE (exp
)) == FUNCTION_TYPE
);
1881 STRIP_TYPE_NOPS (exp
);
1883 if (TREE_NO_WARNING (orig_exp
))
1884 TREE_NO_WARNING (exp
) = 1;
1886 return build_unary_op (loc
, ADDR_EXPR
, exp
, 0);
1889 /* Mark EXP as read, not just set, for set but not used -Wunused
1890 warning purposes. */
1893 mark_exp_read (tree exp
)
1895 switch (TREE_CODE (exp
))
1899 DECL_READ_P (exp
) = 1;
1908 mark_exp_read (TREE_OPERAND (exp
, 0));
1911 case C_MAYBE_CONST_EXPR
:
1912 mark_exp_read (TREE_OPERAND (exp
, 1));
1919 /* Perform the default conversion of arrays and functions to pointers.
1920 Return the result of converting EXP. For any other expression, just
1923 LOC is the location of the expression. */
1926 default_function_array_conversion (location_t loc
, struct c_expr exp
)
1928 tree orig_exp
= exp
.value
;
1929 tree type
= TREE_TYPE (exp
.value
);
1930 enum tree_code code
= TREE_CODE (type
);
1936 bool not_lvalue
= false;
1937 bool lvalue_array_p
;
1939 while ((TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
1940 || CONVERT_EXPR_P (exp
.value
))
1941 && TREE_TYPE (TREE_OPERAND (exp
.value
, 0)) == type
)
1943 if (TREE_CODE (exp
.value
) == NON_LVALUE_EXPR
)
1945 exp
.value
= TREE_OPERAND (exp
.value
, 0);
1948 if (TREE_NO_WARNING (orig_exp
))
1949 TREE_NO_WARNING (exp
.value
) = 1;
1951 lvalue_array_p
= !not_lvalue
&& lvalue_p (exp
.value
);
1952 if (!flag_isoc99
&& !lvalue_array_p
)
1954 /* Before C99, non-lvalue arrays do not decay to pointers.
1955 Normally, using such an array would be invalid; but it can
1956 be used correctly inside sizeof or as a statement expression.
1957 Thus, do not give an error here; an error will result later. */
1961 exp
.value
= array_to_pointer_conversion (loc
, exp
.value
);
1965 exp
.value
= function_to_pointer_conversion (loc
, exp
.value
);
1975 default_function_array_read_conversion (location_t loc
, struct c_expr exp
)
1977 mark_exp_read (exp
.value
);
1978 return default_function_array_conversion (loc
, exp
);
1981 /* Return whether EXPR should be treated as an atomic lvalue for the
1982 purposes of load and store handling. */
1985 really_atomic_lvalue (tree expr
)
1987 if (error_operand_p (expr
))
1989 if (!TYPE_ATOMIC (TREE_TYPE (expr
)))
1991 if (!lvalue_p (expr
))
1994 /* Ignore _Atomic on register variables, since their addresses can't
1995 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1996 sequences wouldn't work. Ignore _Atomic on structures containing
1997 bit-fields, since accessing elements of atomic structures or
1998 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1999 it's undefined at translation time or execution time, and the
2000 normal atomic sequences again wouldn't work. */
2001 while (handled_component_p (expr
))
2003 if (TREE_CODE (expr
) == COMPONENT_REF
2004 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
2006 expr
= TREE_OPERAND (expr
, 0);
2008 if (DECL_P (expr
) && C_DECL_REGISTER (expr
))
2013 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2014 including converting functions and arrays to pointers if CONVERT_P.
2015 If READ_P, also mark the expression as having been read. */
2018 convert_lvalue_to_rvalue (location_t loc
, struct c_expr exp
,
2019 bool convert_p
, bool read_p
)
2022 mark_exp_read (exp
.value
);
2024 exp
= default_function_array_conversion (loc
, exp
);
2025 if (really_atomic_lvalue (exp
.value
))
2027 vec
<tree
, va_gc
> *params
;
2028 tree nonatomic_type
, tmp
, tmp_addr
, fndecl
, func_call
;
2029 tree expr_type
= TREE_TYPE (exp
.value
);
2030 tree expr_addr
= build_unary_op (loc
, ADDR_EXPR
, exp
.value
, 0);
2031 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
2033 gcc_assert (TYPE_ATOMIC (expr_type
));
2035 /* Expansion of a generic atomic load may require an addition
2036 element, so allocate enough to prevent a resize. */
2037 vec_alloc (params
, 4);
2039 /* Remove the qualifiers for the rest of the expressions and
2040 create the VAL temp variable to hold the RHS. */
2041 nonatomic_type
= build_qualified_type (expr_type
, TYPE_UNQUALIFIED
);
2042 tmp
= create_tmp_var_raw (nonatomic_type
);
2043 tmp_addr
= build_unary_op (loc
, ADDR_EXPR
, tmp
, 0);
2044 TREE_ADDRESSABLE (tmp
) = 1;
2045 TREE_NO_WARNING (tmp
) = 1;
2047 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2048 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
2049 params
->quick_push (expr_addr
);
2050 params
->quick_push (tmp_addr
);
2051 params
->quick_push (seq_cst
);
2052 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
2054 /* EXPR is always read. */
2055 mark_exp_read (exp
.value
);
2057 /* Return tmp which contains the value loaded. */
2058 exp
.value
= build4 (TARGET_EXPR
, nonatomic_type
, tmp
, func_call
,
2059 NULL_TREE
, NULL_TREE
);
2064 /* EXP is an expression of integer type. Apply the integer promotions
2065 to it and return the promoted value. */
2068 perform_integral_promotions (tree exp
)
2070 tree type
= TREE_TYPE (exp
);
2071 enum tree_code code
= TREE_CODE (type
);
2073 gcc_assert (INTEGRAL_TYPE_P (type
));
2075 /* Normally convert enums to int,
2076 but convert wide enums to something wider. */
2077 if (code
== ENUMERAL_TYPE
)
2079 type
= c_common_type_for_size (MAX (TYPE_PRECISION (type
),
2080 TYPE_PRECISION (integer_type_node
)),
2081 ((TYPE_PRECISION (type
)
2082 >= TYPE_PRECISION (integer_type_node
))
2083 && TYPE_UNSIGNED (type
)));
2085 return convert (type
, exp
);
2088 /* ??? This should no longer be needed now bit-fields have their
2090 if (TREE_CODE (exp
) == COMPONENT_REF
2091 && DECL_C_BIT_FIELD (TREE_OPERAND (exp
, 1))
2092 /* If it's thinner than an int, promote it like a
2093 c_promoting_integer_type_p, otherwise leave it alone. */
2094 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp
, 1)),
2095 TYPE_PRECISION (integer_type_node
)))
2096 return convert (integer_type_node
, exp
);
2098 if (c_promoting_integer_type_p (type
))
2100 /* Preserve unsignedness if not really getting any wider. */
2101 if (TYPE_UNSIGNED (type
)
2102 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
2103 return convert (unsigned_type_node
, exp
);
2105 return convert (integer_type_node
, exp
);
2112 /* Perform default promotions for C data used in expressions.
2113 Enumeral types or short or char are converted to int.
2114 In addition, manifest constants symbols are replaced by their values. */
2117 default_conversion (tree exp
)
2120 tree type
= TREE_TYPE (exp
);
2121 enum tree_code code
= TREE_CODE (type
);
2124 mark_exp_read (exp
);
2126 /* Functions and arrays have been converted during parsing. */
2127 gcc_assert (code
!= FUNCTION_TYPE
);
2128 if (code
== ARRAY_TYPE
)
2131 /* Constants can be used directly unless they're not loadable. */
2132 if (TREE_CODE (exp
) == CONST_DECL
)
2133 exp
= DECL_INITIAL (exp
);
2135 /* Strip no-op conversions. */
2137 STRIP_TYPE_NOPS (exp
);
2139 if (TREE_NO_WARNING (orig_exp
))
2140 TREE_NO_WARNING (exp
) = 1;
2142 if (code
== VOID_TYPE
)
2144 error_at (EXPR_LOC_OR_LOC (exp
, input_location
),
2145 "void value not ignored as it ought to be");
2146 return error_mark_node
;
2149 exp
= require_complete_type (exp
);
2150 if (exp
== error_mark_node
)
2151 return error_mark_node
;
2153 promoted_type
= targetm
.promoted_type (type
);
2155 return convert (promoted_type
, exp
);
2157 if (INTEGRAL_TYPE_P (type
))
2158 return perform_integral_promotions (exp
);
2163 /* Look up COMPONENT in a structure or union TYPE.
2165 If the component name is not found, returns NULL_TREE. Otherwise,
2166 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2167 stepping down the chain to the component, which is in the last
2168 TREE_VALUE of the list. Normally the list is of length one, but if
2169 the component is embedded within (nested) anonymous structures or
2170 unions, the list steps down the chain to the component. */
2173 lookup_field (tree type
, tree component
)
2177 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2178 to the field elements. Use a binary search on this array to quickly
2179 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2180 will always be set for structures which have many elements. */
2182 if (TYPE_LANG_SPECIFIC (type
) && TYPE_LANG_SPECIFIC (type
)->s
)
2185 tree
*field_array
= &TYPE_LANG_SPECIFIC (type
)->s
->elts
[0];
2187 field
= TYPE_FIELDS (type
);
2189 top
= TYPE_LANG_SPECIFIC (type
)->s
->len
;
2190 while (top
- bot
> 1)
2192 half
= (top
- bot
+ 1) >> 1;
2193 field
= field_array
[bot
+half
];
2195 if (DECL_NAME (field
) == NULL_TREE
)
2197 /* Step through all anon unions in linear fashion. */
2198 while (DECL_NAME (field_array
[bot
]) == NULL_TREE
)
2200 field
= field_array
[bot
++];
2201 if (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2202 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
2204 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2207 return tree_cons (NULL_TREE
, field
, anon
);
2209 /* The Plan 9 compiler permits referring
2210 directly to an anonymous struct/union field
2211 using a typedef name. */
2212 if (flag_plan9_extensions
2213 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2214 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field
)))
2216 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2222 /* Entire record is only anon unions. */
2226 /* Restart the binary search, with new lower bound. */
2230 if (DECL_NAME (field
) == component
)
2232 if (DECL_NAME (field
) < component
)
2238 if (DECL_NAME (field_array
[bot
]) == component
)
2239 field
= field_array
[bot
];
2240 else if (DECL_NAME (field
) != component
)
2245 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
2247 if (DECL_NAME (field
) == NULL_TREE
2248 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
2249 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
))
2251 tree anon
= lookup_field (TREE_TYPE (field
), component
);
2254 return tree_cons (NULL_TREE
, field
, anon
);
2256 /* The Plan 9 compiler permits referring directly to an
2257 anonymous struct/union field using a typedef
2259 if (flag_plan9_extensions
2260 && TYPE_NAME (TREE_TYPE (field
)) != NULL_TREE
2261 && TREE_CODE (TYPE_NAME (TREE_TYPE (field
))) == TYPE_DECL
2262 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field
)))
2267 if (DECL_NAME (field
) == component
)
2271 if (field
== NULL_TREE
)
2275 return tree_cons (NULL_TREE
, field
, NULL_TREE
);
2278 /* Make an expression to refer to the COMPONENT field of structure or
2279 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2280 location of the COMPONENT_REF. */
2283 build_component_ref (location_t loc
, tree datum
, tree component
)
2285 tree type
= TREE_TYPE (datum
);
2286 enum tree_code code
= TREE_CODE (type
);
2289 bool datum_lvalue
= lvalue_p (datum
);
2291 if (!objc_is_public (datum
, component
))
2292 return error_mark_node
;
2294 /* Detect Objective-C property syntax object.property. */
2295 if (c_dialect_objc ()
2296 && (ref
= objc_maybe_build_component_ref (datum
, component
)))
2299 /* See if there is a field or component with name COMPONENT. */
2301 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
2303 if (!COMPLETE_TYPE_P (type
))
2305 c_incomplete_type_error (NULL_TREE
, type
);
2306 return error_mark_node
;
2309 field
= lookup_field (type
, component
);
2313 error_at (loc
, "%qT has no member named %qE", type
, component
);
2314 return error_mark_node
;
2317 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2318 This might be better solved in future the way the C++ front
2319 end does it - by giving the anonymous entities each a
2320 separate name and type, and then have build_component_ref
2321 recursively call itself. We can't do that here. */
2324 tree subdatum
= TREE_VALUE (field
);
2327 bool use_datum_quals
;
2329 if (TREE_TYPE (subdatum
) == error_mark_node
)
2330 return error_mark_node
;
2332 /* If this is an rvalue, it does not have qualifiers in C
2333 standard terms and we must avoid propagating such
2334 qualifiers down to a non-lvalue array that is then
2335 converted to a pointer. */
2336 use_datum_quals
= (datum_lvalue
2337 || TREE_CODE (TREE_TYPE (subdatum
)) != ARRAY_TYPE
);
2339 quals
= TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum
)));
2340 if (use_datum_quals
)
2341 quals
|= TYPE_QUALS (TREE_TYPE (datum
));
2342 subtype
= c_build_qualified_type (TREE_TYPE (subdatum
), quals
);
2344 ref
= build3 (COMPONENT_REF
, subtype
, datum
, subdatum
,
2346 SET_EXPR_LOCATION (ref
, loc
);
2347 if (TREE_READONLY (subdatum
)
2348 || (use_datum_quals
&& TREE_READONLY (datum
)))
2349 TREE_READONLY (ref
) = 1;
2350 if (TREE_THIS_VOLATILE (subdatum
)
2351 || (use_datum_quals
&& TREE_THIS_VOLATILE (datum
)))
2352 TREE_THIS_VOLATILE (ref
) = 1;
2354 if (TREE_DEPRECATED (subdatum
))
2355 warn_deprecated_use (subdatum
, NULL_TREE
);
2359 field
= TREE_CHAIN (field
);
2365 else if (code
!= ERROR_MARK
)
2367 "request for member %qE in something not a structure or union",
2370 return error_mark_node
;
2373 /* Given an expression PTR for a pointer, return an expression
2374 for the value pointed to.
2375 ERRORSTRING is the name of the operator to appear in error messages.
2377 LOC is the location to use for the generated tree. */
2380 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errstring
)
2382 tree pointer
= default_conversion (ptr
);
2383 tree type
= TREE_TYPE (pointer
);
2386 if (TREE_CODE (type
) == POINTER_TYPE
)
2388 if (CONVERT_EXPR_P (pointer
)
2389 || TREE_CODE (pointer
) == VIEW_CONVERT_EXPR
)
2391 /* If a warning is issued, mark it to avoid duplicates from
2392 the backend. This only needs to be done at
2393 warn_strict_aliasing > 2. */
2394 if (warn_strict_aliasing
> 2)
2395 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer
, 0)),
2396 type
, TREE_OPERAND (pointer
, 0)))
2397 TREE_NO_WARNING (pointer
) = 1;
2400 if (TREE_CODE (pointer
) == ADDR_EXPR
2401 && (TREE_TYPE (TREE_OPERAND (pointer
, 0))
2402 == TREE_TYPE (type
)))
2404 ref
= TREE_OPERAND (pointer
, 0);
2405 protected_set_expr_location (ref
, loc
);
2410 tree t
= TREE_TYPE (type
);
2412 ref
= build1 (INDIRECT_REF
, t
, pointer
);
2414 if (!COMPLETE_OR_VOID_TYPE_P (t
) && TREE_CODE (t
) != ARRAY_TYPE
)
2416 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)))
2418 error_at (loc
, "dereferencing pointer to incomplete type "
2420 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr
)) = 1;
2422 return error_mark_node
;
2424 if (VOID_TYPE_P (t
) && c_inhibit_evaluation_warnings
== 0)
2425 warning_at (loc
, 0, "dereferencing %<void *%> pointer");
2427 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2428 so that we get the proper error message if the result is used
2429 to assign to. Also, &* is supposed to be a no-op.
2430 And ANSI C seems to specify that the type of the result
2431 should be the const type. */
2432 /* A de-reference of a pointer to const is not a const. It is valid
2433 to change it via some other pointer. */
2434 TREE_READONLY (ref
) = TYPE_READONLY (t
);
2435 TREE_SIDE_EFFECTS (ref
)
2436 = TYPE_VOLATILE (t
) || TREE_SIDE_EFFECTS (pointer
);
2437 TREE_THIS_VOLATILE (ref
) = TYPE_VOLATILE (t
);
2438 protected_set_expr_location (ref
, loc
);
2442 else if (TREE_CODE (pointer
) != ERROR_MARK
)
2443 invalid_indirection_error (loc
, type
, errstring
);
2445 return error_mark_node
;
2448 /* This handles expressions of the form "a[i]", which denotes
2451 This is logically equivalent in C to *(a+i), but we may do it differently.
2452 If A is a variable or a member, we generate a primitive ARRAY_REF.
2453 This avoids forcing the array out of registers, and can work on
2454 arrays that are not lvalues (for example, members of structures returned
2457 For vector types, allow vector[i] but not i[vector], and create
2458 *(((type*)&vectortype) + i) for the expression.
2460 LOC is the location to use for the returned expression. */
2463 build_array_ref (location_t loc
, tree array
, tree index
)
2466 bool swapped
= false;
2467 if (TREE_TYPE (array
) == error_mark_node
2468 || TREE_TYPE (index
) == error_mark_node
)
2469 return error_mark_node
;
2471 if (flag_cilkplus
&& contains_array_notation_expr (index
))
2474 if (!find_rank (loc
, index
, index
, true, &rank
))
2475 return error_mark_node
;
2478 error_at (loc
, "rank of the array's index is greater than 1");
2479 return error_mark_node
;
2482 if (TREE_CODE (TREE_TYPE (array
)) != ARRAY_TYPE
2483 && TREE_CODE (TREE_TYPE (array
)) != POINTER_TYPE
2484 /* Allow vector[index] but not index[vector]. */
2485 && TREE_CODE (TREE_TYPE (array
)) != VECTOR_TYPE
)
2488 if (TREE_CODE (TREE_TYPE (index
)) != ARRAY_TYPE
2489 && TREE_CODE (TREE_TYPE (index
)) != POINTER_TYPE
)
2492 "subscripted value is neither array nor pointer nor vector");
2494 return error_mark_node
;
2502 if (!INTEGRAL_TYPE_P (TREE_TYPE (index
)))
2504 error_at (loc
, "array subscript is not an integer");
2505 return error_mark_node
;
2508 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array
))) == FUNCTION_TYPE
)
2510 error_at (loc
, "subscripted value is pointer to function");
2511 return error_mark_node
;
2514 /* ??? Existing practice has been to warn only when the char
2515 index is syntactically the index, not for char[array]. */
2517 warn_array_subscript_with_type_char (loc
, index
);
2519 /* Apply default promotions *after* noticing character types. */
2520 index
= default_conversion (index
);
2521 if (index
== error_mark_node
)
2522 return error_mark_node
;
2524 gcc_assert (TREE_CODE (TREE_TYPE (index
)) == INTEGER_TYPE
);
2527 = convert_vector_to_pointer_for_subscript (loc
, &array
, index
);
2529 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
2533 /* An array that is indexed by a non-constant
2534 cannot be stored in a register; we must be able to do
2535 address arithmetic on its address.
2536 Likewise an array of elements of variable size. */
2537 if (TREE_CODE (index
) != INTEGER_CST
2538 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
2539 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
)))) != INTEGER_CST
))
2541 if (!c_mark_addressable (array
))
2542 return error_mark_node
;
2544 /* An array that is indexed by a constant value which is not within
2545 the array bounds cannot be stored in a register either; because we
2546 would get a crash in store_bit_field/extract_bit_field when trying
2547 to access a non-existent part of the register. */
2548 if (TREE_CODE (index
) == INTEGER_CST
2549 && TYPE_DOMAIN (TREE_TYPE (array
))
2550 && !int_fits_type_p (index
, TYPE_DOMAIN (TREE_TYPE (array
))))
2552 if (!c_mark_addressable (array
))
2553 return error_mark_node
;
2556 if (pedantic
|| warn_c90_c99_compat
)
2559 while (TREE_CODE (foo
) == COMPONENT_REF
)
2560 foo
= TREE_OPERAND (foo
, 0);
2561 if (TREE_CODE (foo
) == VAR_DECL
&& C_DECL_REGISTER (foo
))
2562 pedwarn (loc
, OPT_Wpedantic
,
2563 "ISO C forbids subscripting %<register%> array");
2564 else if (!lvalue_p (foo
))
2565 pedwarn_c90 (loc
, OPT_Wpedantic
,
2566 "ISO C90 forbids subscripting non-lvalue "
2570 type
= TREE_TYPE (TREE_TYPE (array
));
2571 rval
= build4 (ARRAY_REF
, type
, array
, index
, NULL_TREE
, NULL_TREE
);
2572 /* Array ref is const/volatile if the array elements are
2573 or if the array is. */
2574 TREE_READONLY (rval
)
2575 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array
)))
2576 | TREE_READONLY (array
));
2577 TREE_SIDE_EFFECTS (rval
)
2578 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2579 | TREE_SIDE_EFFECTS (array
));
2580 TREE_THIS_VOLATILE (rval
)
2581 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array
)))
2582 /* This was added by rms on 16 Nov 91.
2583 It fixes vol struct foo *a; a->elts[1]
2584 in an inline function.
2585 Hope it doesn't break something else. */
2586 | TREE_THIS_VOLATILE (array
));
2587 ret
= require_complete_type (rval
);
2588 protected_set_expr_location (ret
, loc
);
2590 ret
= non_lvalue_loc (loc
, ret
);
2595 tree ar
= default_conversion (array
);
2597 if (ar
== error_mark_node
)
2600 gcc_assert (TREE_CODE (TREE_TYPE (ar
)) == POINTER_TYPE
);
2601 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar
))) != FUNCTION_TYPE
);
2603 ret
= build_indirect_ref (loc
, build_binary_op (loc
, PLUS_EXPR
, ar
,
2607 ret
= non_lvalue_loc (loc
, ret
);
2612 /* Build an external reference to identifier ID. FUN indicates
2613 whether this will be used for a function call. LOC is the source
2614 location of the identifier. This sets *TYPE to the type of the
2615 identifier, which is not the same as the type of the returned value
2616 for CONST_DECLs defined as enum constants. If the type of the
2617 identifier is not available, *TYPE is set to NULL. */
2619 build_external_ref (location_t loc
, tree id
, int fun
, tree
*type
)
2622 tree decl
= lookup_name (id
);
2624 /* In Objective-C, an instance variable (ivar) may be preferred to
2625 whatever lookup_name() found. */
2626 decl
= objc_lookup_ivar (decl
, id
);
2629 if (decl
&& decl
!= error_mark_node
)
2632 *type
= TREE_TYPE (ref
);
2635 /* Implicit function declaration. */
2636 ref
= implicitly_declare (loc
, id
);
2637 else if (decl
== error_mark_node
)
2638 /* Don't complain about something that's already been
2639 complained about. */
2640 return error_mark_node
;
2643 undeclared_variable (loc
, id
);
2644 return error_mark_node
;
2647 if (TREE_TYPE (ref
) == error_mark_node
)
2648 return error_mark_node
;
2650 if (TREE_DEPRECATED (ref
))
2651 warn_deprecated_use (ref
, NULL_TREE
);
2653 /* Recursive call does not count as usage. */
2654 if (ref
!= current_function_decl
)
2656 TREE_USED (ref
) = 1;
2659 if (TREE_CODE (ref
) == FUNCTION_DECL
&& !in_alignof
)
2661 if (!in_sizeof
&& !in_typeof
)
2662 C_DECL_USED (ref
) = 1;
2663 else if (DECL_INITIAL (ref
) == 0
2664 && DECL_EXTERNAL (ref
)
2665 && !TREE_PUBLIC (ref
))
2666 record_maybe_used_decl (ref
);
2669 if (TREE_CODE (ref
) == CONST_DECL
)
2671 used_types_insert (TREE_TYPE (ref
));
2674 && TREE_CODE (TREE_TYPE (ref
)) == ENUMERAL_TYPE
2675 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref
)))
2677 warning_at (loc
, OPT_Wc___compat
,
2678 ("enum constant defined in struct or union "
2679 "is not visible in C++"));
2680 inform (DECL_SOURCE_LOCATION (ref
), "enum constant defined here");
2683 ref
= DECL_INITIAL (ref
);
2684 TREE_CONSTANT (ref
) = 1;
2686 else if (current_function_decl
!= 0
2687 && !DECL_FILE_SCOPE_P (current_function_decl
)
2688 && (TREE_CODE (ref
) == VAR_DECL
2689 || TREE_CODE (ref
) == PARM_DECL
2690 || TREE_CODE (ref
) == FUNCTION_DECL
))
2692 tree context
= decl_function_context (ref
);
2694 if (context
!= 0 && context
!= current_function_decl
)
2695 DECL_NONLOCAL (ref
) = 1;
2697 /* C99 6.7.4p3: An inline definition of a function with external
2698 linkage ... shall not contain a reference to an identifier with
2699 internal linkage. */
2700 else if (current_function_decl
!= 0
2701 && DECL_DECLARED_INLINE_P (current_function_decl
)
2702 && DECL_EXTERNAL (current_function_decl
)
2703 && VAR_OR_FUNCTION_DECL_P (ref
)
2704 && (TREE_CODE (ref
) != VAR_DECL
|| TREE_STATIC (ref
))
2705 && ! TREE_PUBLIC (ref
)
2706 && DECL_CONTEXT (ref
) != current_function_decl
)
2707 record_inline_static (loc
, current_function_decl
, ref
,
2713 /* Record details of decls possibly used inside sizeof or typeof. */
2714 struct maybe_used_decl
2718 /* The level seen at (in_sizeof + in_typeof). */
2720 /* The next one at this level or above, or NULL. */
2721 struct maybe_used_decl
*next
;
2724 static struct maybe_used_decl
*maybe_used_decls
;
2726 /* Record that DECL, an undefined static function reference seen
2727 inside sizeof or typeof, might be used if the operand of sizeof is
2728 a VLA type or the operand of typeof is a variably modified
2732 record_maybe_used_decl (tree decl
)
2734 struct maybe_used_decl
*t
= XOBNEW (&parser_obstack
, struct maybe_used_decl
);
2736 t
->level
= in_sizeof
+ in_typeof
;
2737 t
->next
= maybe_used_decls
;
2738 maybe_used_decls
= t
;
2741 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2742 USED is false, just discard them. If it is true, mark them used
2743 (if no longer inside sizeof or typeof) or move them to the next
2744 level up (if still inside sizeof or typeof). */
2747 pop_maybe_used (bool used
)
2749 struct maybe_used_decl
*p
= maybe_used_decls
;
2750 int cur_level
= in_sizeof
+ in_typeof
;
2751 while (p
&& p
->level
> cur_level
)
2756 C_DECL_USED (p
->decl
) = 1;
2758 p
->level
= cur_level
;
2762 if (!used
|| cur_level
== 0)
2763 maybe_used_decls
= p
;
2766 /* Return the result of sizeof applied to EXPR. */
2769 c_expr_sizeof_expr (location_t loc
, struct c_expr expr
)
2772 if (expr
.value
== error_mark_node
)
2774 ret
.value
= error_mark_node
;
2775 ret
.original_code
= ERROR_MARK
;
2776 ret
.original_type
= NULL
;
2777 pop_maybe_used (false);
2781 bool expr_const_operands
= true;
2783 if (TREE_CODE (expr
.value
) == PARM_DECL
2784 && C_ARRAY_PARAMETER (expr
.value
))
2786 if (warning_at (loc
, OPT_Wsizeof_array_argument
,
2787 "%<sizeof%> on array function parameter %qE will "
2788 "return size of %qT", expr
.value
,
2789 expr
.original_type
))
2790 inform (DECL_SOURCE_LOCATION (expr
.value
), "declared here");
2792 tree folded_expr
= c_fully_fold (expr
.value
, require_constant_value
,
2793 &expr_const_operands
);
2794 ret
.value
= c_sizeof (loc
, TREE_TYPE (folded_expr
));
2795 c_last_sizeof_arg
= expr
.value
;
2796 ret
.original_code
= SIZEOF_EXPR
;
2797 ret
.original_type
= NULL
;
2798 if (c_vla_type_p (TREE_TYPE (folded_expr
)))
2800 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2801 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2802 folded_expr
, ret
.value
);
2803 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !expr_const_operands
;
2804 SET_EXPR_LOCATION (ret
.value
, loc
);
2806 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr
)));
2811 /* Return the result of sizeof applied to T, a structure for the type
2812 name passed to sizeof (rather than the type itself). LOC is the
2813 location of the original expression. */
2816 c_expr_sizeof_type (location_t loc
, struct c_type_name
*t
)
2820 tree type_expr
= NULL_TREE
;
2821 bool type_expr_const
= true;
2822 type
= groktypename (t
, &type_expr
, &type_expr_const
);
2823 ret
.value
= c_sizeof (loc
, type
);
2824 c_last_sizeof_arg
= type
;
2825 ret
.original_code
= SIZEOF_EXPR
;
2826 ret
.original_type
= NULL
;
2827 if ((type_expr
|| TREE_CODE (ret
.value
) == INTEGER_CST
)
2828 && c_vla_type_p (type
))
2830 /* If the type is a [*] array, it is a VLA but is represented as
2831 having a size of zero. In such a case we must ensure that
2832 the result of sizeof does not get folded to a constant by
2833 c_fully_fold, because if the size is evaluated the result is
2834 not constant and so constraints on zero or negative size
2835 arrays must not be applied when this sizeof call is inside
2836 another array declarator. */
2838 type_expr
= integer_zero_node
;
2839 ret
.value
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
.value
),
2840 type_expr
, ret
.value
);
2841 C_MAYBE_CONST_EXPR_NON_CONST (ret
.value
) = !type_expr_const
;
2843 pop_maybe_used (type
!= error_mark_node
2844 ? C_TYPE_VARIABLE_SIZE (type
) : false);
2848 /* Build a function call to function FUNCTION with parameters PARAMS.
2849 The function call is at LOC.
2850 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2851 TREE_VALUE of each node is a parameter-expression.
2852 FUNCTION's data type may be a function type or a pointer-to-function. */
2855 build_function_call (location_t loc
, tree function
, tree params
)
2857 vec
<tree
, va_gc
> *v
;
2860 vec_alloc (v
, list_length (params
));
2861 for (; params
; params
= TREE_CHAIN (params
))
2862 v
->quick_push (TREE_VALUE (params
));
2863 ret
= c_build_function_call_vec (loc
, vNULL
, function
, v
, NULL
);
2868 /* Give a note about the location of the declaration of DECL. */
2870 static void inform_declaration (tree decl
)
2872 if (decl
&& (TREE_CODE (decl
) != FUNCTION_DECL
|| !DECL_BUILT_IN (decl
)))
2873 inform (DECL_SOURCE_LOCATION (decl
), "declared here");
2876 /* Build a function call to function FUNCTION with parameters PARAMS.
2877 ORIGTYPES, if not NULL, is a vector of types; each element is
2878 either NULL or the original type of the corresponding element in
2879 PARAMS. The original type may differ from TREE_TYPE of the
2880 parameter for enums. FUNCTION's data type may be a function type
2881 or pointer-to-function. This function changes the elements of
2885 build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
2886 tree function
, vec
<tree
, va_gc
> *params
,
2887 vec
<tree
, va_gc
> *origtypes
)
2889 tree fntype
, fundecl
= 0;
2890 tree name
= NULL_TREE
, result
;
2896 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2897 STRIP_TYPE_NOPS (function
);
2899 /* Convert anything with function type to a pointer-to-function. */
2900 if (TREE_CODE (function
) == FUNCTION_DECL
)
2902 name
= DECL_NAME (function
);
2905 tm_malloc_replacement (function
);
2907 /* Atomic functions have type checking/casting already done. They are
2908 often rewritten and don't match the original parameter list. */
2909 if (name
&& !strncmp (IDENTIFIER_POINTER (name
), "__atomic_", 9))
2913 && is_cilkplus_reduce_builtin (function
))
2916 if (TREE_CODE (TREE_TYPE (function
)) == FUNCTION_TYPE
)
2917 function
= function_to_pointer_conversion (loc
, function
);
2919 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2920 expressions, like those used for ObjC messenger dispatches. */
2921 if (params
&& !params
->is_empty ())
2922 function
= objc_rewrite_function_call (function
, (*params
)[0]);
2924 function
= c_fully_fold (function
, false, NULL
);
2926 fntype
= TREE_TYPE (function
);
2928 if (TREE_CODE (fntype
) == ERROR_MARK
)
2929 return error_mark_node
;
2931 if (!(TREE_CODE (fntype
) == POINTER_TYPE
2932 && TREE_CODE (TREE_TYPE (fntype
)) == FUNCTION_TYPE
))
2934 if (!flag_diagnostics_show_caret
)
2936 "called object %qE is not a function or function pointer",
2938 else if (DECL_P (function
))
2941 "called object %qD is not a function or function pointer",
2943 inform_declaration (function
);
2947 "called object is not a function or function pointer");
2948 return error_mark_node
;
2951 if (fundecl
&& TREE_THIS_VOLATILE (fundecl
))
2952 current_function_returns_abnormally
= 1;
2954 /* fntype now gets the type of function pointed to. */
2955 fntype
= TREE_TYPE (fntype
);
2957 /* Convert the parameters to the types declared in the
2958 function prototype, or apply default promotions. */
2960 nargs
= convert_arguments (loc
, arg_loc
, TYPE_ARG_TYPES (fntype
), params
,
2961 origtypes
, function
, fundecl
);
2963 return error_mark_node
;
2965 /* Check that the function is called through a compatible prototype.
2966 If it is not, warn. */
2967 if (CONVERT_EXPR_P (function
)
2968 && TREE_CODE (tem
= TREE_OPERAND (function
, 0)) == ADDR_EXPR
2969 && TREE_CODE (tem
= TREE_OPERAND (tem
, 0)) == FUNCTION_DECL
2970 && !comptypes (fntype
, TREE_TYPE (tem
)))
2972 tree return_type
= TREE_TYPE (fntype
);
2974 /* This situation leads to run-time undefined behavior. We can't,
2975 therefore, simply error unless we can prove that all possible
2976 executions of the program must execute the code. */
2977 warning_at (loc
, 0, "function called through a non-compatible type");
2979 if (VOID_TYPE_P (return_type
)
2980 && TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
)
2982 "function with qualified void return type called");
2985 argarray
= vec_safe_address (params
);
2987 /* Check that arguments to builtin functions match the expectations. */
2989 && DECL_BUILT_IN (fundecl
)
2990 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
2991 && !check_builtin_function_arguments (fundecl
, nargs
, argarray
))
2992 return error_mark_node
;
2994 /* Check that the arguments to the function are valid. */
2995 check_function_arguments (fntype
, nargs
, argarray
);
2997 if (name
!= NULL_TREE
2998 && !strncmp (IDENTIFIER_POINTER (name
), "__builtin_", 10))
3000 if (require_constant_value
)
3002 fold_build_call_array_initializer_loc (loc
, TREE_TYPE (fntype
),
3003 function
, nargs
, argarray
);
3005 result
= fold_build_call_array_loc (loc
, TREE_TYPE (fntype
),
3006 function
, nargs
, argarray
);
3007 if (TREE_CODE (result
) == NOP_EXPR
3008 && TREE_CODE (TREE_OPERAND (result
, 0)) == INTEGER_CST
)
3009 STRIP_TYPE_NOPS (result
);
3012 result
= build_call_array_loc (loc
, TREE_TYPE (fntype
),
3013 function
, nargs
, argarray
);
3015 if (VOID_TYPE_P (TREE_TYPE (result
)))
3017 if (TYPE_QUALS (TREE_TYPE (result
)) != TYPE_UNQUALIFIED
)
3019 "function with qualified void return type called");
3022 return require_complete_type (result
);
3025 /* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3028 c_build_function_call_vec (location_t loc
, vec
<location_t
> arg_loc
,
3029 tree function
, vec
<tree
, va_gc
> *params
,
3030 vec
<tree
, va_gc
> *origtypes
)
3032 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3033 STRIP_TYPE_NOPS (function
);
3035 /* Convert anything with function type to a pointer-to-function. */
3036 if (TREE_CODE (function
) == FUNCTION_DECL
)
3038 /* Implement type-directed function overloading for builtins.
3039 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3040 handle all the type checking. The result is a complete expression
3041 that implements this function call. */
3042 tree tem
= resolve_overloaded_builtin (loc
, function
, params
);
3046 return build_function_call_vec (loc
, arg_loc
, function
, params
, origtypes
);
3049 /* Convert the argument expressions in the vector VALUES
3050 to the types in the list TYPELIST.
3052 If TYPELIST is exhausted, or when an element has NULL as its type,
3053 perform the default conversions.
3055 ORIGTYPES is the original types of the expressions in VALUES. This
3056 holds the type of enum values which have been converted to integral
3057 types. It may be NULL.
3059 FUNCTION is a tree for the called function. It is used only for
3060 error messages, where it is formatted with %qE.
3062 This is also where warnings about wrong number of args are generated.
3064 ARG_LOC are locations of function arguments (if any).
3066 Returns the actual number of arguments processed (which may be less
3067 than the length of VALUES in some error situations), or -1 on
3071 convert_arguments (location_t loc
, vec
<location_t
> arg_loc
, tree typelist
,
3072 vec
<tree
, va_gc
> *values
, vec
<tree
, va_gc
> *origtypes
,
3073 tree function
, tree fundecl
)
3076 unsigned int parmnum
;
3077 bool error_args
= false;
3078 const bool type_generic
= fundecl
3079 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl
)));
3080 bool type_generic_remove_excess_precision
= false;
3083 /* Change pointer to function to the function itself for
3085 if (TREE_CODE (function
) == ADDR_EXPR
3086 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
3087 function
= TREE_OPERAND (function
, 0);
3089 /* Handle an ObjC selector specially for diagnostics. */
3090 selector
= objc_message_selector ();
3092 /* For type-generic built-in functions, determine whether excess
3093 precision should be removed (classification) or not
3096 && DECL_BUILT_IN (fundecl
)
3097 && DECL_BUILT_IN_CLASS (fundecl
) == BUILT_IN_NORMAL
)
3099 switch (DECL_FUNCTION_CODE (fundecl
))
3101 case BUILT_IN_ISFINITE
:
3102 case BUILT_IN_ISINF
:
3103 case BUILT_IN_ISINF_SIGN
:
3104 case BUILT_IN_ISNAN
:
3105 case BUILT_IN_ISNORMAL
:
3106 case BUILT_IN_FPCLASSIFY
:
3107 type_generic_remove_excess_precision
= true;
3111 type_generic_remove_excess_precision
= false;
3115 if (flag_cilkplus
&& fundecl
&& is_cilkplus_reduce_builtin (fundecl
))
3116 return vec_safe_length (values
);
3118 /* Scan the given expressions and types, producing individual
3119 converted arguments. */
3121 for (typetail
= typelist
, parmnum
= 0;
3122 values
&& values
->iterate (parmnum
, &val
);
3125 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3126 tree valtype
= TREE_TYPE (val
);
3127 tree rname
= function
;
3128 int argnum
= parmnum
+ 1;
3129 const char *invalid_func_diag
;
3130 bool excess_precision
= false;
3133 /* Some __atomic_* builtins have additional hidden argument at
3136 = !arg_loc
.is_empty () && values
->length () == arg_loc
.length ()
3137 ? expansion_point_location_if_in_system_header (arg_loc
[parmnum
])
3140 if (type
== void_type_node
)
3143 error_at (loc
, "too many arguments to method %qE", selector
);
3145 error_at (loc
, "too many arguments to function %qE", function
);
3146 inform_declaration (fundecl
);
3147 return error_args
? -1 : (int) parmnum
;
3150 if (selector
&& argnum
> 2)
3156 npc
= null_pointer_constant_p (val
);
3158 /* If there is excess precision and a prototype, convert once to
3159 the required type rather than converting via the semantic
3160 type. Likewise without a prototype a float value represented
3161 as long double should be converted once to double. But for
3162 type-generic classification functions excess precision must
3164 if (TREE_CODE (val
) == EXCESS_PRECISION_EXPR
3165 && (type
|| !type_generic
|| !type_generic_remove_excess_precision
))
3167 val
= TREE_OPERAND (val
, 0);
3168 excess_precision
= true;
3170 val
= c_fully_fold (val
, false, NULL
);
3171 STRIP_TYPE_NOPS (val
);
3173 val
= require_complete_type (val
);
3177 /* Formal parm type is specified by a function prototype. */
3179 if (type
== error_mark_node
|| !COMPLETE_TYPE_P (type
))
3181 error_at (ploc
, "type of formal parameter %d is incomplete",
3189 /* Optionally warn about conversions that
3190 differ from the default conversions. */
3191 if (warn_traditional_conversion
|| warn_traditional
)
3193 unsigned int formal_prec
= TYPE_PRECISION (type
);
3195 if (INTEGRAL_TYPE_P (type
)
3196 && TREE_CODE (valtype
) == REAL_TYPE
)
3197 warning_at (ploc
, OPT_Wtraditional_conversion
,
3198 "passing argument %d of %qE as integer rather "
3199 "than floating due to prototype",
3201 if (INTEGRAL_TYPE_P (type
)
3202 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3203 warning_at (ploc
, OPT_Wtraditional_conversion
,
3204 "passing argument %d of %qE as integer rather "
3205 "than complex due to prototype",
3207 else if (TREE_CODE (type
) == COMPLEX_TYPE
3208 && TREE_CODE (valtype
) == REAL_TYPE
)
3209 warning_at (ploc
, OPT_Wtraditional_conversion
,
3210 "passing argument %d of %qE as complex rather "
3211 "than floating due to prototype",
3213 else if (TREE_CODE (type
) == REAL_TYPE
3214 && INTEGRAL_TYPE_P (valtype
))
3215 warning_at (ploc
, OPT_Wtraditional_conversion
,
3216 "passing argument %d of %qE as floating rather "
3217 "than integer due to prototype",
3219 else if (TREE_CODE (type
) == COMPLEX_TYPE
3220 && INTEGRAL_TYPE_P (valtype
))
3221 warning_at (ploc
, OPT_Wtraditional_conversion
,
3222 "passing argument %d of %qE as complex rather "
3223 "than integer due to prototype",
3225 else if (TREE_CODE (type
) == REAL_TYPE
3226 && TREE_CODE (valtype
) == COMPLEX_TYPE
)
3227 warning_at (ploc
, OPT_Wtraditional_conversion
,
3228 "passing argument %d of %qE as floating rather "
3229 "than complex due to prototype",
3231 /* ??? At some point, messages should be written about
3232 conversions between complex types, but that's too messy
3234 else if (TREE_CODE (type
) == REAL_TYPE
3235 && TREE_CODE (valtype
) == REAL_TYPE
)
3237 /* Warn if any argument is passed as `float',
3238 since without a prototype it would be `double'. */
3239 if (formal_prec
== TYPE_PRECISION (float_type_node
)
3240 && type
!= dfloat32_type_node
)
3241 warning_at (ploc
, 0,
3242 "passing argument %d of %qE as %<float%> "
3243 "rather than %<double%> due to prototype",
3246 /* Warn if mismatch between argument and prototype
3247 for decimal float types. Warn of conversions with
3248 binary float types and of precision narrowing due to
3250 else if (type
!= valtype
3251 && (type
== dfloat32_type_node
3252 || type
== dfloat64_type_node
3253 || type
== dfloat128_type_node
3254 || valtype
== dfloat32_type_node
3255 || valtype
== dfloat64_type_node
3256 || valtype
== dfloat128_type_node
)
3258 <= TYPE_PRECISION (valtype
)
3259 || (type
== dfloat128_type_node
3261 != dfloat64_type_node
3263 != dfloat32_type_node
)))
3264 || (type
== dfloat64_type_node
3266 != dfloat32_type_node
))))
3267 warning_at (ploc
, 0,
3268 "passing argument %d of %qE as %qT "
3269 "rather than %qT due to prototype",
3270 argnum
, rname
, type
, valtype
);
3273 /* Detect integer changing in width or signedness.
3274 These warnings are only activated with
3275 -Wtraditional-conversion, not with -Wtraditional. */
3276 else if (warn_traditional_conversion
&& INTEGRAL_TYPE_P (type
)
3277 && INTEGRAL_TYPE_P (valtype
))
3279 tree would_have_been
= default_conversion (val
);
3280 tree type1
= TREE_TYPE (would_have_been
);
3282 if (TREE_CODE (type
) == ENUMERAL_TYPE
3283 && (TYPE_MAIN_VARIANT (type
)
3284 == TYPE_MAIN_VARIANT (valtype
)))
3285 /* No warning if function asks for enum
3286 and the actual arg is that enum type. */
3288 else if (formal_prec
!= TYPE_PRECISION (type1
))
3289 warning_at (ploc
, OPT_Wtraditional_conversion
,
3290 "passing argument %d of %qE "
3291 "with different width due to prototype",
3293 else if (TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (type1
))
3295 /* Don't complain if the formal parameter type
3296 is an enum, because we can't tell now whether
3297 the value was an enum--even the same enum. */
3298 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
3300 else if (TREE_CODE (val
) == INTEGER_CST
3301 && int_fits_type_p (val
, type
))
3302 /* Change in signedness doesn't matter
3303 if a constant value is unaffected. */
3305 /* If the value is extended from a narrower
3306 unsigned type, it doesn't matter whether we
3307 pass it as signed or unsigned; the value
3308 certainly is the same either way. */
3309 else if (TYPE_PRECISION (valtype
) < TYPE_PRECISION (type
)
3310 && TYPE_UNSIGNED (valtype
))
3312 else if (TYPE_UNSIGNED (type
))
3313 warning_at (ploc
, OPT_Wtraditional_conversion
,
3314 "passing argument %d of %qE "
3315 "as unsigned due to prototype",
3318 warning_at (ploc
, OPT_Wtraditional_conversion
,
3319 "passing argument %d of %qE "
3320 "as signed due to prototype",
3325 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3326 sake of better warnings from convert_and_check. */
3327 if (excess_precision
)
3328 val
= build1 (EXCESS_PRECISION_EXPR
, valtype
, val
);
3329 origtype
= (!origtypes
) ? NULL_TREE
: (*origtypes
)[parmnum
];
3330 parmval
= convert_for_assignment (loc
, ploc
, type
,
3331 val
, origtype
, ic_argpass
,
3332 npc
, fundecl
, function
,
3335 if (targetm
.calls
.promote_prototypes (fundecl
? TREE_TYPE (fundecl
) : 0)
3336 && INTEGRAL_TYPE_P (type
)
3337 && (TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)))
3338 parmval
= default_conversion (parmval
);
3341 else if (TREE_CODE (valtype
) == REAL_TYPE
3342 && (TYPE_PRECISION (valtype
)
3343 <= TYPE_PRECISION (double_type_node
))
3344 && TYPE_MAIN_VARIANT (valtype
) != double_type_node
3345 && TYPE_MAIN_VARIANT (valtype
) != long_double_type_node
3346 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype
)))
3352 /* Convert `float' to `double'. */
3353 if (warn_double_promotion
&& !c_inhibit_evaluation_warnings
)
3354 warning_at (ploc
, OPT_Wdouble_promotion
,
3355 "implicit conversion from %qT to %qT when passing "
3356 "argument to function",
3357 valtype
, double_type_node
);
3358 parmval
= convert (double_type_node
, val
);
3361 else if (excess_precision
&& !type_generic
)
3362 /* A "double" argument with excess precision being passed
3363 without a prototype or in variable arguments. */
3364 parmval
= convert (valtype
, val
);
3365 else if ((invalid_func_diag
=
3366 targetm
.calls
.invalid_arg_for_unprototyped_fn (typelist
, fundecl
, val
)))
3368 error (invalid_func_diag
);
3372 /* Convert `short' and `char' to full-size `int'. */
3373 parmval
= default_conversion (val
);
3375 (*values
)[parmnum
] = parmval
;
3376 if (parmval
== error_mark_node
)
3380 typetail
= TREE_CHAIN (typetail
);
3383 gcc_assert (parmnum
== vec_safe_length (values
));
3385 if (typetail
!= 0 && TREE_VALUE (typetail
) != void_type_node
)
3387 error_at (loc
, "too few arguments to function %qE", function
);
3388 inform_declaration (fundecl
);
3392 return error_args
? -1 : (int) parmnum
;
3395 /* This is the entry point used by the parser to build unary operators
3396 in the input. CODE, a tree_code, specifies the unary operator, and
3397 ARG is the operand. For unary plus, the C parser currently uses
3398 CONVERT_EXPR for code.
3400 LOC is the location to use for the tree generated.
3404 parser_build_unary_op (location_t loc
, enum tree_code code
, struct c_expr arg
)
3406 struct c_expr result
;
3408 result
.value
= build_unary_op (loc
, code
, arg
.value
, 0);
3409 result
.original_code
= code
;
3410 result
.original_type
= NULL
;
3412 if (TREE_OVERFLOW_P (result
.value
) && !TREE_OVERFLOW_P (arg
.value
))
3413 overflow_warning (loc
, result
.value
);
3418 /* This is the entry point used by the parser to build binary operators
3419 in the input. CODE, a tree_code, specifies the binary operator, and
3420 ARG1 and ARG2 are the operands. In addition to constructing the
3421 expression, we check for operands that were written with other binary
3422 operators in a way that is likely to confuse the user.
3424 LOCATION is the location of the binary operator. */
3427 parser_build_binary_op (location_t location
, enum tree_code code
,
3428 struct c_expr arg1
, struct c_expr arg2
)
3430 struct c_expr result
;
3432 enum tree_code code1
= arg1
.original_code
;
3433 enum tree_code code2
= arg2
.original_code
;
3434 tree type1
= (arg1
.original_type
3435 ? arg1
.original_type
3436 : TREE_TYPE (arg1
.value
));
3437 tree type2
= (arg2
.original_type
3438 ? arg2
.original_type
3439 : TREE_TYPE (arg2
.value
));
3441 result
.value
= build_binary_op (location
, code
,
3442 arg1
.value
, arg2
.value
, 1);
3443 result
.original_code
= code
;
3444 result
.original_type
= NULL
;
3446 if (TREE_CODE (result
.value
) == ERROR_MARK
)
3449 if (location
!= UNKNOWN_LOCATION
)
3450 protected_set_expr_location (result
.value
, location
);
3452 /* Check for cases such as x+y<<z which users are likely
3454 if (warn_parentheses
)
3455 warn_about_parentheses (location
, code
, code1
, arg1
.value
, code2
,
3458 if (warn_logical_op
)
3459 warn_logical_operator (location
, code
, TREE_TYPE (result
.value
),
3460 code1
, arg1
.value
, code2
, arg2
.value
);
3462 if (warn_logical_not_paren
3463 && TREE_CODE_CLASS (code
) == tcc_comparison
3464 && code1
== TRUTH_NOT_EXPR
3465 && code2
!= TRUTH_NOT_EXPR
3466 /* Avoid warning for !!x == y. */
3467 && (TREE_CODE (arg1
.value
) != NE_EXPR
3468 || !integer_zerop (TREE_OPERAND (arg1
.value
, 1))))
3470 /* Avoid warning for !b == y where b has _Bool type. */
3471 tree t
= integer_zero_node
;
3472 if (TREE_CODE (arg1
.value
) == EQ_EXPR
3473 && integer_zerop (TREE_OPERAND (arg1
.value
, 1))
3474 && TREE_TYPE (TREE_OPERAND (arg1
.value
, 0)) == integer_type_node
)
3476 t
= TREE_OPERAND (arg1
.value
, 0);
3479 if (TREE_TYPE (t
) != integer_type_node
)
3481 if (TREE_CODE (t
) == C_MAYBE_CONST_EXPR
)
3482 t
= C_MAYBE_CONST_EXPR_EXPR (t
);
3483 else if (CONVERT_EXPR_P (t
))
3484 t
= TREE_OPERAND (t
, 0);
3490 if (TREE_CODE (TREE_TYPE (t
)) != BOOLEAN_TYPE
)
3491 warn_logical_not_parentheses (location
, code
, arg2
.value
);
3494 /* Warn about comparisons against string literals, with the exception
3495 of testing for equality or inequality of a string literal with NULL. */
3496 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3498 if ((code1
== STRING_CST
&& !integer_zerop (arg2
.value
))
3499 || (code2
== STRING_CST
&& !integer_zerop (arg1
.value
)))
3500 warning_at (location
, OPT_Waddress
,
3501 "comparison with string literal results in unspecified behavior");
3503 else if (TREE_CODE_CLASS (code
) == tcc_comparison
3504 && (code1
== STRING_CST
|| code2
== STRING_CST
))
3505 warning_at (location
, OPT_Waddress
,
3506 "comparison with string literal results in unspecified behavior");
3508 if (TREE_OVERFLOW_P (result
.value
)
3509 && !TREE_OVERFLOW_P (arg1
.value
)
3510 && !TREE_OVERFLOW_P (arg2
.value
))
3511 overflow_warning (location
, result
.value
);
3513 /* Warn about comparisons of different enum types. */
3514 if (warn_enum_compare
3515 && TREE_CODE_CLASS (code
) == tcc_comparison
3516 && TREE_CODE (type1
) == ENUMERAL_TYPE
3517 && TREE_CODE (type2
) == ENUMERAL_TYPE
3518 && TYPE_MAIN_VARIANT (type1
) != TYPE_MAIN_VARIANT (type2
))
3519 warning_at (location
, OPT_Wenum_compare
,
3520 "comparison between %qT and %qT",
3526 /* Return a tree for the difference of pointers OP0 and OP1.
3527 The resulting tree has type int. */
3530 pointer_diff (location_t loc
, tree op0
, tree op1
)
3532 tree restype
= ptrdiff_type_node
;
3533 tree result
, inttype
;
3535 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0
)));
3536 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1
)));
3537 tree target_type
= TREE_TYPE (TREE_TYPE (op0
));
3538 tree orig_op1
= op1
;
3540 /* If the operands point into different address spaces, we need to
3541 explicitly convert them to pointers into the common address space
3542 before we can subtract the numerical address values. */
3545 addr_space_t as_common
;
3548 /* Determine the common superset address space. This is guaranteed
3549 to exist because the caller verified that comp_target_types
3550 returned non-zero. */
3551 if (!addr_space_superset (as0
, as1
, &as_common
))
3554 common_type
= common_pointer_type (TREE_TYPE (op0
), TREE_TYPE (op1
));
3555 op0
= convert (common_type
, op0
);
3556 op1
= convert (common_type
, op1
);
3559 /* Determine integer type to perform computations in. This will usually
3560 be the same as the result type (ptrdiff_t), but may need to be a wider
3561 type if pointers for the address space are wider than ptrdiff_t. */
3562 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
3563 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
3567 if (TREE_CODE (target_type
) == VOID_TYPE
)
3568 pedwarn (loc
, OPT_Wpointer_arith
,
3569 "pointer of type %<void *%> used in subtraction");
3570 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
3571 pedwarn (loc
, OPT_Wpointer_arith
,
3572 "pointer to a function used in subtraction");
3574 /* First do the subtraction as integers;
3575 then drop through to build the divide operator.
3576 Do not do default conversions on the minus operator
3577 in case restype is a short type. */
3579 op0
= build_binary_op (loc
,
3580 MINUS_EXPR
, convert (inttype
, op0
),
3581 convert (inttype
, op1
), 0);
3582 /* This generates an error if op1 is pointer to incomplete type. */
3583 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1
))))
3584 error_at (loc
, "arithmetic on pointer to an incomplete type");
3586 op1
= c_size_in_bytes (target_type
);
3588 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1
)))
3589 error_at (loc
, "arithmetic on pointer to an empty aggregate");
3591 /* Divide by the size, in easiest possible way. */
3592 result
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, inttype
,
3593 op0
, convert (inttype
, op1
));
3595 /* Convert to final result type if necessary. */
3596 return convert (restype
, result
);
3599 /* Expand atomic compound assignments into an approriate sequence as
3600 specified by the C11 standard section 6.5.16.2.
3606 This sequence is used for all types for which these operations are
3609 In addition, built-in versions of the 'fe' prefixed routines may
3610 need to be invoked for floating point (real, complex or vector) when
3611 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3621 __atomic_load (addr, &old, SEQ_CST);
3622 feholdexcept (&fenv);
3624 newval = old op val;
3625 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3628 feclearexcept (FE_ALL_EXCEPT);
3631 feupdateenv (&fenv);
3633 Also note that the compiler is simply issuing the generic form of
3634 the atomic operations. This requires temp(s) and has their address
3635 taken. The atomic processing is smart enough to figure out when the
3636 size of an object can utilize a lock-free version, and convert the
3637 built-in call to the appropriate lock-free routine. The optimizers
3638 will then dispose of any temps that are no longer required, and
3639 lock-free implementations are utilized as long as there is target
3640 support for the required size.
3642 If the operator is NOP_EXPR, then this is a simple assignment, and
3643 an __atomic_store is issued to perform the assignment rather than
3648 /* Build an atomic assignment at LOC, expanding into the proper
3649 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3650 the result of the operation, unless RETURN_OLD_P in which case
3651 return the old value of LHS (this is only for postincrement and
3654 build_atomic_assign (location_t loc
, tree lhs
, enum tree_code modifycode
,
3655 tree rhs
, bool return_old_p
)
3657 tree fndecl
, func_call
;
3658 vec
<tree
, va_gc
> *params
;
3659 tree val
, nonatomic_lhs_type
, nonatomic_rhs_type
, newval
, newval_addr
;
3662 tree stmt
, goto_stmt
;
3663 tree loop_label
, loop_decl
, done_label
, done_decl
;
3665 tree lhs_type
= TREE_TYPE (lhs
);
3666 tree lhs_addr
= build_unary_op (loc
, ADDR_EXPR
, lhs
, 0);
3667 tree seq_cst
= build_int_cst (integer_type_node
, MEMMODEL_SEQ_CST
);
3668 tree rhs_type
= TREE_TYPE (rhs
);
3670 gcc_assert (TYPE_ATOMIC (lhs_type
));
3673 gcc_assert (modifycode
== PLUS_EXPR
|| modifycode
== MINUS_EXPR
);
3675 /* Allocate enough vector items for a compare_exchange. */
3676 vec_alloc (params
, 6);
3678 /* Create a compound statement to hold the sequence of statements
3680 compound_stmt
= c_begin_compound_stmt (false);
3682 /* Fold the RHS if it hasn't already been folded. */
3683 if (modifycode
!= NOP_EXPR
)
3684 rhs
= c_fully_fold (rhs
, false, NULL
);
3686 /* Remove the qualifiers for the rest of the expressions and create
3687 the VAL temp variable to hold the RHS. */
3688 nonatomic_lhs_type
= build_qualified_type (lhs_type
, TYPE_UNQUALIFIED
);
3689 nonatomic_rhs_type
= build_qualified_type (rhs_type
, TYPE_UNQUALIFIED
);
3690 val
= create_tmp_var_raw (nonatomic_rhs_type
);
3691 TREE_ADDRESSABLE (val
) = 1;
3692 TREE_NO_WARNING (val
) = 1;
3693 rhs
= build4 (TARGET_EXPR
, nonatomic_rhs_type
, val
, rhs
, NULL_TREE
,
3695 SET_EXPR_LOCATION (rhs
, loc
);
3698 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3700 if (modifycode
== NOP_EXPR
)
3702 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3703 rhs
= build_unary_op (loc
, ADDR_EXPR
, val
, 0);
3704 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_STORE
);
3705 params
->quick_push (lhs_addr
);
3706 params
->quick_push (rhs
);
3707 params
->quick_push (seq_cst
);
3708 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3709 add_stmt (func_call
);
3711 /* Finish the compound statement. */
3712 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3714 /* VAL is the value which was stored, return a COMPOUND_STMT of
3715 the statement and that value. */
3716 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
, val
);
3719 /* Create the variables and labels required for the op= form. */
3720 old
= create_tmp_var_raw (nonatomic_lhs_type
);
3721 old_addr
= build_unary_op (loc
, ADDR_EXPR
, old
, 0);
3722 TREE_ADDRESSABLE (old
) = 1;
3723 TREE_NO_WARNING (old
) = 1;
3725 newval
= create_tmp_var_raw (nonatomic_lhs_type
);
3726 newval_addr
= build_unary_op (loc
, ADDR_EXPR
, newval
, 0);
3727 TREE_ADDRESSABLE (newval
) = 1;
3729 loop_decl
= create_artificial_label (loc
);
3730 loop_label
= build1 (LABEL_EXPR
, void_type_node
, loop_decl
);
3732 done_decl
= create_artificial_label (loc
);
3733 done_label
= build1 (LABEL_EXPR
, void_type_node
, done_decl
);
3735 /* __atomic_load (addr, &old, SEQ_CST). */
3736 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD
);
3737 params
->quick_push (lhs_addr
);
3738 params
->quick_push (old_addr
);
3739 params
->quick_push (seq_cst
);
3740 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3741 old
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, old
, func_call
, NULL_TREE
,
3744 params
->truncate (0);
3746 /* Create the expressions for floating-point environment
3747 manipulation, if required. */
3748 bool need_fenv
= (flag_trapping_math
3749 && (FLOAT_TYPE_P (lhs_type
) || FLOAT_TYPE_P (rhs_type
)));
3750 tree hold_call
= NULL_TREE
, clear_call
= NULL_TREE
, update_call
= NULL_TREE
;
3752 targetm
.atomic_assign_expand_fenv (&hold_call
, &clear_call
, &update_call
);
3755 add_stmt (hold_call
);
3758 add_stmt (loop_label
);
3760 /* newval = old + val; */
3761 rhs
= build_binary_op (loc
, modifycode
, old
, val
, 1);
3762 rhs
= c_fully_fold (rhs
, false, NULL
);
3763 rhs
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, nonatomic_lhs_type
,
3764 rhs
, NULL_TREE
, ic_assign
, false, NULL_TREE
,
3766 if (rhs
!= error_mark_node
)
3768 rhs
= build4 (TARGET_EXPR
, nonatomic_lhs_type
, newval
, rhs
, NULL_TREE
,
3770 SET_EXPR_LOCATION (rhs
, loc
);
3774 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3776 fndecl
= builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE
);
3777 params
->quick_push (lhs_addr
);
3778 params
->quick_push (old_addr
);
3779 params
->quick_push (newval_addr
);
3780 params
->quick_push (integer_zero_node
);
3781 params
->quick_push (seq_cst
);
3782 params
->quick_push (seq_cst
);
3783 func_call
= c_build_function_call_vec (loc
, vNULL
, fndecl
, params
, NULL
);
3785 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, done_decl
);
3786 SET_EXPR_LOCATION (goto_stmt
, loc
);
3788 stmt
= build3 (COND_EXPR
, void_type_node
, func_call
, goto_stmt
, NULL_TREE
);
3789 SET_EXPR_LOCATION (stmt
, loc
);
3793 add_stmt (clear_call
);
3796 goto_stmt
= build1 (GOTO_EXPR
, void_type_node
, loop_decl
);
3797 SET_EXPR_LOCATION (goto_stmt
, loc
);
3798 add_stmt (goto_stmt
);
3801 add_stmt (done_label
);
3804 add_stmt (update_call
);
3806 /* Finish the compound statement. */
3807 compound_stmt
= c_end_compound_stmt (loc
, compound_stmt
, false);
3809 /* NEWVAL is the value that was successfully stored, return a
3810 COMPOUND_EXPR of the statement and the appropriate value. */
3811 return build2 (COMPOUND_EXPR
, nonatomic_lhs_type
, compound_stmt
,
3812 return_old_p
? old
: newval
);
3815 /* Construct and perhaps optimize a tree representation
3816 for a unary operation. CODE, a tree_code, specifies the operation
3817 and XARG is the operand.
3818 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3819 the default promotions (such as from short to int).
3820 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3821 allows non-lvalues; this is only used to handle conversion of non-lvalue
3822 arrays to pointers in C99.
3824 LOCATION is the location of the operator. */
3827 build_unary_op (location_t location
,
3828 enum tree_code code
, tree xarg
, int flag
)
3830 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3833 enum tree_code typecode
;
3835 tree ret
= error_mark_node
;
3836 tree eptype
= NULL_TREE
;
3837 int noconvert
= flag
;
3838 const char *invalid_op_diag
;
3841 int_operands
= EXPR_INT_CONST_OPERANDS (xarg
);
3843 arg
= remove_c_maybe_const_expr (arg
);
3845 if (code
!= ADDR_EXPR
)
3846 arg
= require_complete_type (arg
);
3848 typecode
= TREE_CODE (TREE_TYPE (arg
));
3849 if (typecode
== ERROR_MARK
)
3850 return error_mark_node
;
3851 if (typecode
== ENUMERAL_TYPE
|| typecode
== BOOLEAN_TYPE
)
3852 typecode
= INTEGER_TYPE
;
3854 if ((invalid_op_diag
3855 = targetm
.invalid_unary_op (code
, TREE_TYPE (xarg
))))
3857 error_at (location
, invalid_op_diag
);
3858 return error_mark_node
;
3861 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
3863 eptype
= TREE_TYPE (arg
);
3864 arg
= TREE_OPERAND (arg
, 0);
3870 /* This is used for unary plus, because a CONVERT_EXPR
3871 is enough to prevent anybody from looking inside for
3872 associativity, but won't generate any code. */
3873 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3874 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3875 || typecode
== VECTOR_TYPE
))
3877 error_at (location
, "wrong type argument to unary plus");
3878 return error_mark_node
;
3880 else if (!noconvert
)
3881 arg
= default_conversion (arg
);
3882 arg
= non_lvalue_loc (location
, arg
);
3886 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3887 || typecode
== FIXED_POINT_TYPE
|| typecode
== COMPLEX_TYPE
3888 || typecode
== VECTOR_TYPE
))
3890 error_at (location
, "wrong type argument to unary minus");
3891 return error_mark_node
;
3893 else if (!noconvert
)
3894 arg
= default_conversion (arg
);
3898 /* ~ works on integer types and non float vectors. */
3899 if (typecode
== INTEGER_TYPE
3900 || (typecode
== VECTOR_TYPE
3901 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg
))))
3904 arg
= default_conversion (arg
);
3906 else if (typecode
== COMPLEX_TYPE
)
3909 pedwarn (location
, OPT_Wpedantic
,
3910 "ISO C does not support %<~%> for complex conjugation");
3912 arg
= default_conversion (arg
);
3916 error_at (location
, "wrong type argument to bit-complement");
3917 return error_mark_node
;
3922 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
))
3924 error_at (location
, "wrong type argument to abs");
3925 return error_mark_node
;
3927 else if (!noconvert
)
3928 arg
= default_conversion (arg
);
3932 /* Conjugating a real value is a no-op, but allow it anyway. */
3933 if (!(typecode
== INTEGER_TYPE
|| typecode
== REAL_TYPE
3934 || typecode
== COMPLEX_TYPE
))
3936 error_at (location
, "wrong type argument to conjugation");
3937 return error_mark_node
;
3939 else if (!noconvert
)
3940 arg
= default_conversion (arg
);
3943 case TRUTH_NOT_EXPR
:
3944 if (typecode
!= INTEGER_TYPE
&& typecode
!= FIXED_POINT_TYPE
3945 && typecode
!= REAL_TYPE
&& typecode
!= POINTER_TYPE
3946 && typecode
!= COMPLEX_TYPE
)
3949 "wrong type argument to unary exclamation mark");
3950 return error_mark_node
;
3954 arg
= c_objc_common_truthvalue_conversion (location
, xarg
);
3955 arg
= remove_c_maybe_const_expr (arg
);
3958 arg
= c_objc_common_truthvalue_conversion (location
, arg
);
3959 ret
= invert_truthvalue_loc (location
, arg
);
3960 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3961 if (EXPR_P (ret
) && EXPR_HAS_LOCATION (ret
))
3962 location
= EXPR_LOCATION (ret
);
3963 goto return_build_unary_op
;
3967 ret
= build_real_imag_expr (location
, code
, arg
);
3968 if (ret
== error_mark_node
)
3969 return error_mark_node
;
3970 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_TYPE
)
3971 eptype
= TREE_TYPE (eptype
);
3972 goto return_build_unary_op
;
3974 case PREINCREMENT_EXPR
:
3975 case POSTINCREMENT_EXPR
:
3976 case PREDECREMENT_EXPR
:
3977 case POSTDECREMENT_EXPR
:
3979 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
3981 tree inner
= build_unary_op (location
, code
,
3982 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
3983 if (inner
== error_mark_node
)
3984 return error_mark_node
;
3985 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
3986 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
3987 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
3988 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = 1;
3989 goto return_build_unary_op
;
3992 /* Complain about anything that is not a true lvalue. In
3993 Objective-C, skip this check for property_refs. */
3994 if (!objc_is_property_ref (arg
)
3995 && !lvalue_or_else (location
,
3996 arg
, ((code
== PREINCREMENT_EXPR
3997 || code
== POSTINCREMENT_EXPR
)
4000 return error_mark_node
;
4002 if (warn_cxx_compat
&& TREE_CODE (TREE_TYPE (arg
)) == ENUMERAL_TYPE
)
4004 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4005 warning_at (location
, OPT_Wc___compat
,
4006 "increment of enumeration value is invalid in C++");
4008 warning_at (location
, OPT_Wc___compat
,
4009 "decrement of enumeration value is invalid in C++");
4012 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4013 arg
= c_fully_fold (arg
, false, NULL
);
4016 atomic_op
= really_atomic_lvalue (arg
);
4018 /* Increment or decrement the real part of the value,
4019 and don't change the imaginary part. */
4020 if (typecode
== COMPLEX_TYPE
)
4024 pedwarn (location
, OPT_Wpedantic
,
4025 "ISO C does not support %<++%> and %<--%> on complex types");
4029 arg
= stabilize_reference (arg
);
4030 real
= build_unary_op (EXPR_LOCATION (arg
), REALPART_EXPR
, arg
, 1);
4031 imag
= build_unary_op (EXPR_LOCATION (arg
), IMAGPART_EXPR
, arg
, 1);
4032 real
= build_unary_op (EXPR_LOCATION (arg
), code
, real
, 1);
4033 if (real
== error_mark_node
|| imag
== error_mark_node
)
4034 return error_mark_node
;
4035 ret
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
4037 goto return_build_unary_op
;
4041 /* Report invalid types. */
4043 if (typecode
!= POINTER_TYPE
&& typecode
!= FIXED_POINT_TYPE
4044 && typecode
!= INTEGER_TYPE
&& typecode
!= REAL_TYPE
4045 && typecode
!= COMPLEX_TYPE
&& typecode
!= VECTOR_TYPE
)
4047 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4048 error_at (location
, "wrong type argument to increment");
4050 error_at (location
, "wrong type argument to decrement");
4052 return error_mark_node
;
4058 argtype
= TREE_TYPE (arg
);
4060 /* Compute the increment. */
4062 if (typecode
== POINTER_TYPE
)
4064 /* If pointer target is an incomplete type,
4065 we just cannot know how to do the arithmetic. */
4066 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype
)))
4068 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4070 "increment of pointer to an incomplete type %qT",
4071 TREE_TYPE (argtype
));
4074 "decrement of pointer to an incomplete type %qT",
4075 TREE_TYPE (argtype
));
4077 else if (TREE_CODE (TREE_TYPE (argtype
)) == FUNCTION_TYPE
4078 || TREE_CODE (TREE_TYPE (argtype
)) == VOID_TYPE
)
4080 if (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
4081 pedwarn (location
, OPT_Wpointer_arith
,
4082 "wrong type argument to increment");
4084 pedwarn (location
, OPT_Wpointer_arith
,
4085 "wrong type argument to decrement");
4088 inc
= c_size_in_bytes (TREE_TYPE (argtype
));
4089 inc
= convert_to_ptrofftype_loc (location
, inc
);
4091 else if (FRACT_MODE_P (TYPE_MODE (argtype
)))
4093 /* For signed fract types, we invert ++ to -- or
4094 -- to ++, and change inc from 1 to -1, because
4095 it is not possible to represent 1 in signed fract constants.
4096 For unsigned fract types, the result always overflows and
4097 we get an undefined (original) or the maximum value. */
4098 if (code
== PREINCREMENT_EXPR
)
4099 code
= PREDECREMENT_EXPR
;
4100 else if (code
== PREDECREMENT_EXPR
)
4101 code
= PREINCREMENT_EXPR
;
4102 else if (code
== POSTINCREMENT_EXPR
)
4103 code
= POSTDECREMENT_EXPR
;
4104 else /* code == POSTDECREMENT_EXPR */
4105 code
= POSTINCREMENT_EXPR
;
4107 inc
= integer_minus_one_node
;
4108 inc
= convert (argtype
, inc
);
4112 inc
= VECTOR_TYPE_P (argtype
)
4113 ? build_one_cst (argtype
)
4115 inc
= convert (argtype
, inc
);
4118 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4119 need to ask Objective-C to build the increment or decrement
4120 expression for it. */
4121 if (objc_is_property_ref (arg
))
4122 return objc_build_incr_expr_for_property_ref (location
, code
,
4125 /* Report a read-only lvalue. */
4126 if (TYPE_READONLY (argtype
))
4128 readonly_error (location
, arg
,
4129 ((code
== PREINCREMENT_EXPR
4130 || code
== POSTINCREMENT_EXPR
)
4131 ? lv_increment
: lv_decrement
));
4132 return error_mark_node
;
4134 else if (TREE_READONLY (arg
))
4135 readonly_warning (arg
,
4136 ((code
== PREINCREMENT_EXPR
4137 || code
== POSTINCREMENT_EXPR
)
4138 ? lv_increment
: lv_decrement
));
4140 /* If the argument is atomic, use the special code sequences for
4141 atomic compound assignment. */
4144 arg
= stabilize_reference (arg
);
4145 ret
= build_atomic_assign (location
, arg
,
4146 ((code
== PREINCREMENT_EXPR
4147 || code
== POSTINCREMENT_EXPR
)
4150 (FRACT_MODE_P (TYPE_MODE (argtype
))
4152 : integer_one_node
),
4153 (code
== POSTINCREMENT_EXPR
4154 || code
== POSTDECREMENT_EXPR
));
4155 goto return_build_unary_op
;
4158 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
4159 val
= boolean_increment (code
, arg
);
4161 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
4162 TREE_SIDE_EFFECTS (val
) = 1;
4163 if (TREE_CODE (val
) != code
)
4164 TREE_NO_WARNING (val
) = 1;
4166 goto return_build_unary_op
;
4170 /* Note that this operation never does default_conversion. */
4172 /* The operand of unary '&' must be an lvalue (which excludes
4173 expressions of type void), or, in C99, the result of a [] or
4174 unary '*' operator. */
4175 if (VOID_TYPE_P (TREE_TYPE (arg
))
4176 && TYPE_QUALS (TREE_TYPE (arg
)) == TYPE_UNQUALIFIED
4177 && (TREE_CODE (arg
) != INDIRECT_REF
4179 pedwarn (location
, 0, "taking address of expression of type %<void%>");
4181 /* Let &* cancel out to simplify resulting code. */
4182 if (TREE_CODE (arg
) == INDIRECT_REF
)
4184 /* Don't let this be an lvalue. */
4185 if (lvalue_p (TREE_OPERAND (arg
, 0)))
4186 return non_lvalue_loc (location
, TREE_OPERAND (arg
, 0));
4187 ret
= TREE_OPERAND (arg
, 0);
4188 goto return_build_unary_op
;
4191 /* For &x[y], return x+y */
4192 if (TREE_CODE (arg
) == ARRAY_REF
)
4194 tree op0
= TREE_OPERAND (arg
, 0);
4195 if (!c_mark_addressable (op0
))
4196 return error_mark_node
;
4199 /* Anything not already handled and not a true memory reference
4200 or a non-lvalue array is an error. */
4201 else if (typecode
!= FUNCTION_TYPE
&& !flag
4202 && !lvalue_or_else (location
, arg
, lv_addressof
))
4203 return error_mark_node
;
4205 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4207 if (TREE_CODE (arg
) == C_MAYBE_CONST_EXPR
)
4209 tree inner
= build_unary_op (location
, code
,
4210 C_MAYBE_CONST_EXPR_EXPR (arg
), flag
);
4211 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
4212 C_MAYBE_CONST_EXPR_PRE (arg
), inner
);
4213 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg
));
4214 C_MAYBE_CONST_EXPR_NON_CONST (ret
)
4215 = C_MAYBE_CONST_EXPR_NON_CONST (arg
);
4216 goto return_build_unary_op
;
4219 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4220 argtype
= TREE_TYPE (arg
);
4222 /* If the lvalue is const or volatile, merge that into the type
4223 to which the address will point. This is only needed
4224 for function types. */
4225 if ((DECL_P (arg
) || REFERENCE_CLASS_P (arg
))
4226 && (TREE_READONLY (arg
) || TREE_THIS_VOLATILE (arg
))
4227 && TREE_CODE (argtype
) == FUNCTION_TYPE
)
4229 int orig_quals
= TYPE_QUALS (strip_array_types (argtype
));
4230 int quals
= orig_quals
;
4232 if (TREE_READONLY (arg
))
4233 quals
|= TYPE_QUAL_CONST
;
4234 if (TREE_THIS_VOLATILE (arg
))
4235 quals
|= TYPE_QUAL_VOLATILE
;
4237 argtype
= c_build_qualified_type (argtype
, quals
);
4240 if (!c_mark_addressable (arg
))
4241 return error_mark_node
;
4243 gcc_assert (TREE_CODE (arg
) != COMPONENT_REF
4244 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)));
4246 argtype
= build_pointer_type (argtype
);
4248 /* ??? Cope with user tricks that amount to offsetof. Delete this
4249 when we have proper support for integer constant expressions. */
4250 val
= get_base_address (arg
);
4251 if (val
&& TREE_CODE (val
) == INDIRECT_REF
4252 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
4254 ret
= fold_convert_loc (location
, argtype
, fold_offsetof_1 (arg
));
4255 goto return_build_unary_op
;
4258 val
= build1 (ADDR_EXPR
, argtype
, arg
);
4261 goto return_build_unary_op
;
4268 argtype
= TREE_TYPE (arg
);
4269 if (TREE_CODE (arg
) == INTEGER_CST
)
4270 ret
= (require_constant_value
4271 ? fold_build1_initializer_loc (location
, code
, argtype
, arg
)
4272 : fold_build1_loc (location
, code
, argtype
, arg
));
4274 ret
= build1 (code
, argtype
, arg
);
4275 return_build_unary_op
:
4276 gcc_assert (ret
!= error_mark_node
);
4277 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
)
4278 && !(TREE_CODE (xarg
) == INTEGER_CST
&& !TREE_OVERFLOW (xarg
)))
4279 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
4280 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
)
4281 ret
= note_integer_operands (ret
);
4283 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4284 protected_set_expr_location (ret
, location
);
4288 /* Return nonzero if REF is an lvalue valid for this language.
4289 Lvalues can be assigned, unless their type has TYPE_READONLY.
4290 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
4293 lvalue_p (const_tree ref
)
4295 const enum tree_code code
= TREE_CODE (ref
);
4302 return lvalue_p (TREE_OPERAND (ref
, 0));
4304 case C_MAYBE_CONST_EXPR
:
4305 return lvalue_p (TREE_OPERAND (ref
, 1));
4307 case COMPOUND_LITERAL_EXPR
:
4313 case ARRAY_NOTATION_REF
:
4318 return (TREE_CODE (TREE_TYPE (ref
)) != FUNCTION_TYPE
4319 && TREE_CODE (TREE_TYPE (ref
)) != METHOD_TYPE
);
4322 return TREE_CODE (TREE_TYPE (ref
)) == ARRAY_TYPE
;
4329 /* Give a warning for storing in something that is read-only in GCC
4330 terms but not const in ISO C terms. */
4333 readonly_warning (tree arg
, enum lvalue_use use
)
4338 warning (0, "assignment of read-only location %qE", arg
);
4341 warning (0, "increment of read-only location %qE", arg
);
4344 warning (0, "decrement of read-only location %qE", arg
);
4353 /* Return nonzero if REF is an lvalue valid for this language;
4354 otherwise, print an error message and return zero. USE says
4355 how the lvalue is being used and so selects the error message.
4356 LOCATION is the location at which any error should be reported. */
4359 lvalue_or_else (location_t loc
, const_tree ref
, enum lvalue_use use
)
4361 int win
= lvalue_p (ref
);
4364 lvalue_error (loc
, use
);
4369 /* Mark EXP saying that we need to be able to take the
4370 address of it; it should not be allocated in a register.
4371 Returns true if successful. */
4374 c_mark_addressable (tree exp
)
4379 switch (TREE_CODE (x
))
4382 if (DECL_C_BIT_FIELD (TREE_OPERAND (x
, 1)))
4385 ("cannot take address of bit-field %qD", TREE_OPERAND (x
, 1));
4389 /* ... fall through ... */
4395 x
= TREE_OPERAND (x
, 0);
4398 case COMPOUND_LITERAL_EXPR
:
4400 TREE_ADDRESSABLE (x
) = 1;
4407 if (C_DECL_REGISTER (x
)
4408 && DECL_NONLOCAL (x
))
4410 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4413 ("global register variable %qD used in nested function", x
);
4416 pedwarn (input_location
, 0, "register variable %qD used in nested function", x
);
4418 else if (C_DECL_REGISTER (x
))
4420 if (TREE_PUBLIC (x
) || TREE_STATIC (x
) || DECL_EXTERNAL (x
))
4421 error ("address of global register variable %qD requested", x
);
4423 error ("address of register variable %qD requested", x
);
4429 TREE_ADDRESSABLE (x
) = 1;
4436 /* Convert EXPR to TYPE, warning about conversion problems with
4437 constants. SEMANTIC_TYPE is the type this conversion would use
4438 without excess precision. If SEMANTIC_TYPE is NULL, this function
4439 is equivalent to convert_and_check. This function is a wrapper that
4440 handles conversions that may be different than
4441 the usual ones because of excess precision. */
4444 ep_convert_and_check (location_t loc
, tree type
, tree expr
,
4447 if (TREE_TYPE (expr
) == type
)
4451 return convert_and_check (loc
, type
, expr
);
4453 if (TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
4454 && TREE_TYPE (expr
) != semantic_type
)
4456 /* For integers, we need to check the real conversion, not
4457 the conversion to the excess precision type. */
4458 expr
= convert_and_check (loc
, semantic_type
, expr
);
4460 /* Result type is the excess precision type, which should be
4461 large enough, so do not check. */
4462 return convert (type
, expr
);
4465 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4466 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4467 if folded to an integer constant then the unselected half may
4468 contain arbitrary operations not normally permitted in constant
4469 expressions. Set the location of the expression to LOC. */
4472 build_conditional_expr (location_t colon_loc
, tree ifexp
, bool ifexp_bcp
,
4473 tree op1
, tree op1_original_type
, tree op2
,
4474 tree op2_original_type
)
4478 enum tree_code code1
;
4479 enum tree_code code2
;
4480 tree result_type
= NULL
;
4481 tree semantic_result_type
= NULL
;
4482 tree orig_op1
= op1
, orig_op2
= op2
;
4483 bool int_const
, op1_int_operands
, op2_int_operands
, int_operands
;
4484 bool ifexp_int_operands
;
4487 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
4488 if (op1_int_operands
)
4489 op1
= remove_c_maybe_const_expr (op1
);
4490 op2_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op2
);
4491 if (op2_int_operands
)
4492 op2
= remove_c_maybe_const_expr (op2
);
4493 ifexp_int_operands
= EXPR_INT_CONST_OPERANDS (ifexp
);
4494 if (ifexp_int_operands
)
4495 ifexp
= remove_c_maybe_const_expr (ifexp
);
4497 /* Promote both alternatives. */
4499 if (TREE_CODE (TREE_TYPE (op1
)) != VOID_TYPE
)
4500 op1
= default_conversion (op1
);
4501 if (TREE_CODE (TREE_TYPE (op2
)) != VOID_TYPE
)
4502 op2
= default_conversion (op2
);
4504 if (TREE_CODE (ifexp
) == ERROR_MARK
4505 || TREE_CODE (TREE_TYPE (op1
)) == ERROR_MARK
4506 || TREE_CODE (TREE_TYPE (op2
)) == ERROR_MARK
)
4507 return error_mark_node
;
4509 type1
= TREE_TYPE (op1
);
4510 code1
= TREE_CODE (type1
);
4511 type2
= TREE_TYPE (op2
);
4512 code2
= TREE_CODE (type2
);
4514 /* C90 does not permit non-lvalue arrays in conditional expressions.
4515 In C99 they will be pointers by now. */
4516 if (code1
== ARRAY_TYPE
|| code2
== ARRAY_TYPE
)
4518 error_at (colon_loc
, "non-lvalue array in conditional expression");
4519 return error_mark_node
;
4522 if ((TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
4523 || TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4524 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4525 || code1
== COMPLEX_TYPE
)
4526 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4527 || code2
== COMPLEX_TYPE
))
4529 semantic_result_type
= c_common_type (type1
, type2
);
4530 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
4532 op1
= TREE_OPERAND (op1
, 0);
4533 type1
= TREE_TYPE (op1
);
4534 gcc_assert (TREE_CODE (type1
) == code1
);
4536 if (TREE_CODE (op2
) == EXCESS_PRECISION_EXPR
)
4538 op2
= TREE_OPERAND (op2
, 0);
4539 type2
= TREE_TYPE (op2
);
4540 gcc_assert (TREE_CODE (type2
) == code2
);
4544 if (warn_cxx_compat
)
4546 tree t1
= op1_original_type
? op1_original_type
: TREE_TYPE (orig_op1
);
4547 tree t2
= op2_original_type
? op2_original_type
: TREE_TYPE (orig_op2
);
4549 if (TREE_CODE (t1
) == ENUMERAL_TYPE
4550 && TREE_CODE (t2
) == ENUMERAL_TYPE
4551 && TYPE_MAIN_VARIANT (t1
) != TYPE_MAIN_VARIANT (t2
))
4552 warning_at (colon_loc
, OPT_Wc___compat
,
4553 ("different enum types in conditional is "
4554 "invalid in C++: %qT vs %qT"),
4558 /* Quickly detect the usual case where op1 and op2 have the same type
4560 if (TYPE_MAIN_VARIANT (type1
) == TYPE_MAIN_VARIANT (type2
))
4563 result_type
= type1
;
4565 result_type
= TYPE_MAIN_VARIANT (type1
);
4567 else if ((code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4568 || code1
== COMPLEX_TYPE
)
4569 && (code2
== INTEGER_TYPE
|| code2
== REAL_TYPE
4570 || code2
== COMPLEX_TYPE
))
4572 result_type
= c_common_type (type1
, type2
);
4573 do_warn_double_promotion (result_type
, type1
, type2
,
4574 "implicit conversion from %qT to %qT to "
4575 "match other result of conditional",
4578 /* If -Wsign-compare, warn here if type1 and type2 have
4579 different signedness. We'll promote the signed to unsigned
4580 and later code won't know it used to be different.
4581 Do this check on the original types, so that explicit casts
4582 will be considered, but default promotions won't. */
4583 if (c_inhibit_evaluation_warnings
== 0)
4585 int unsigned_op1
= TYPE_UNSIGNED (TREE_TYPE (orig_op1
));
4586 int unsigned_op2
= TYPE_UNSIGNED (TREE_TYPE (orig_op2
));
4588 if (unsigned_op1
^ unsigned_op2
)
4592 /* Do not warn if the result type is signed, since the
4593 signed type will only be chosen if it can represent
4594 all the values of the unsigned type. */
4595 if (!TYPE_UNSIGNED (result_type
))
4599 bool op1_maybe_const
= true;
4600 bool op2_maybe_const
= true;
4602 /* Do not warn if the signed quantity is an
4603 unsuffixed integer literal (or some static
4604 constant expression involving such literals) and
4605 it is non-negative. This warning requires the
4606 operands to be folded for best results, so do
4607 that folding in this case even without
4608 warn_sign_compare to avoid warning options
4609 possibly affecting code generation. */
4610 c_inhibit_evaluation_warnings
4611 += (ifexp
== truthvalue_false_node
);
4612 op1
= c_fully_fold (op1
, require_constant_value
,
4614 c_inhibit_evaluation_warnings
4615 -= (ifexp
== truthvalue_false_node
);
4617 c_inhibit_evaluation_warnings
4618 += (ifexp
== truthvalue_true_node
);
4619 op2
= c_fully_fold (op2
, require_constant_value
,
4621 c_inhibit_evaluation_warnings
4622 -= (ifexp
== truthvalue_true_node
);
4624 if (warn_sign_compare
)
4627 && tree_expr_nonnegative_warnv_p (op1
, &ovf
))
4629 && tree_expr_nonnegative_warnv_p (op2
, &ovf
)))
4632 warning_at (colon_loc
, OPT_Wsign_compare
,
4633 ("signed and unsigned type in "
4634 "conditional expression"));
4636 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
4637 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
4638 if (!op2_maybe_const
|| TREE_CODE (op2
) != INTEGER_CST
)
4639 op2
= c_wrap_maybe_const (op2
, !op2_maybe_const
);
4644 else if (code1
== VOID_TYPE
|| code2
== VOID_TYPE
)
4646 if (code1
!= VOID_TYPE
|| code2
!= VOID_TYPE
)
4647 pedwarn (colon_loc
, OPT_Wpedantic
,
4648 "ISO C forbids conditional expr with only one void side");
4649 result_type
= void_type_node
;
4651 else if (code1
== POINTER_TYPE
&& code2
== POINTER_TYPE
)
4653 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
4654 addr_space_t as2
= TYPE_ADDR_SPACE (TREE_TYPE (type2
));
4655 addr_space_t as_common
;
4657 if (comp_target_types (colon_loc
, type1
, type2
))
4658 result_type
= common_pointer_type (type1
, type2
);
4659 else if (null_pointer_constant_p (orig_op1
))
4660 result_type
= type2
;
4661 else if (null_pointer_constant_p (orig_op2
))
4662 result_type
= type1
;
4663 else if (!addr_space_superset (as1
, as2
, &as_common
))
4665 error_at (colon_loc
, "pointers to disjoint address spaces "
4666 "used in conditional expression");
4667 return error_mark_node
;
4669 else if (VOID_TYPE_P (TREE_TYPE (type1
))
4670 && !TYPE_ATOMIC (TREE_TYPE (type1
)))
4672 if ((TREE_CODE (TREE_TYPE (type2
)) == ARRAY_TYPE
)
4673 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2
)))
4674 & ~TYPE_QUALS (TREE_TYPE (type1
))))
4675 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
4676 "pointer to array loses qualifier "
4677 "in conditional expression");
4679 if (TREE_CODE (TREE_TYPE (type2
)) == FUNCTION_TYPE
)
4680 pedwarn (colon_loc
, OPT_Wpedantic
,
4681 "ISO C forbids conditional expr between "
4682 "%<void *%> and function pointer");
4683 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type1
),
4684 TREE_TYPE (type2
)));
4686 else if (VOID_TYPE_P (TREE_TYPE (type2
))
4687 && !TYPE_ATOMIC (TREE_TYPE (type2
)))
4689 if ((TREE_CODE (TREE_TYPE (type1
)) == ARRAY_TYPE
)
4690 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1
)))
4691 & ~TYPE_QUALS (TREE_TYPE (type2
))))
4692 warning_at (colon_loc
, OPT_Wdiscarded_array_qualifiers
,
4693 "pointer to array loses qualifier "
4694 "in conditional expression");
4696 if (TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
)
4697 pedwarn (colon_loc
, OPT_Wpedantic
,
4698 "ISO C forbids conditional expr between "
4699 "%<void *%> and function pointer");
4700 result_type
= build_pointer_type (qualify_type (TREE_TYPE (type2
),
4701 TREE_TYPE (type1
)));
4703 /* Objective-C pointer comparisons are a bit more lenient. */
4704 else if (objc_have_common_type (type1
, type2
, -3, NULL_TREE
))
4705 result_type
= objc_common_type (type1
, type2
);
4708 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
4710 pedwarn (colon_loc
, 0,
4711 "pointer type mismatch in conditional expression");
4712 result_type
= build_pointer_type
4713 (build_qualified_type (void_type_node
, qual
));
4716 else if (code1
== POINTER_TYPE
&& code2
== INTEGER_TYPE
)
4718 if (!null_pointer_constant_p (orig_op2
))
4719 pedwarn (colon_loc
, 0,
4720 "pointer/integer type mismatch in conditional expression");
4723 op2
= null_pointer_node
;
4725 result_type
= type1
;
4727 else if (code2
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4729 if (!null_pointer_constant_p (orig_op1
))
4730 pedwarn (colon_loc
, 0,
4731 "pointer/integer type mismatch in conditional expression");
4734 op1
= null_pointer_node
;
4736 result_type
= type2
;
4741 if (flag_cond_mismatch
)
4742 result_type
= void_type_node
;
4745 error_at (colon_loc
, "type mismatch in conditional expression");
4746 return error_mark_node
;
4750 /* Merge const and volatile flags of the incoming types. */
4752 = build_type_variant (result_type
,
4753 TYPE_READONLY (type1
) || TYPE_READONLY (type2
),
4754 TYPE_VOLATILE (type1
) || TYPE_VOLATILE (type2
));
4756 op1
= ep_convert_and_check (colon_loc
, result_type
, op1
,
4757 semantic_result_type
);
4758 op2
= ep_convert_and_check (colon_loc
, result_type
, op2
,
4759 semantic_result_type
);
4761 if (ifexp_bcp
&& ifexp
== truthvalue_true_node
)
4763 op2_int_operands
= true;
4764 op1
= c_fully_fold (op1
, require_constant_value
, NULL
);
4766 if (ifexp_bcp
&& ifexp
== truthvalue_false_node
)
4768 op1_int_operands
= true;
4769 op2
= c_fully_fold (op2
, require_constant_value
, NULL
);
4771 int_const
= int_operands
= (ifexp_int_operands
4773 && op2_int_operands
);
4776 int_const
= ((ifexp
== truthvalue_true_node
4777 && TREE_CODE (orig_op1
) == INTEGER_CST
4778 && !TREE_OVERFLOW (orig_op1
))
4779 || (ifexp
== truthvalue_false_node
4780 && TREE_CODE (orig_op2
) == INTEGER_CST
4781 && !TREE_OVERFLOW (orig_op2
)));
4783 if (int_const
|| (ifexp_bcp
&& TREE_CODE (ifexp
) == INTEGER_CST
))
4784 ret
= fold_build3_loc (colon_loc
, COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4789 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4790 nested inside of the expression. */
4791 op1
= c_fully_fold (op1
, false, NULL
);
4792 op2
= c_fully_fold (op2
, false, NULL
);
4794 ret
= build3 (COND_EXPR
, result_type
, ifexp
, op1
, op2
);
4796 ret
= note_integer_operands (ret
);
4798 if (semantic_result_type
)
4799 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
4801 protected_set_expr_location (ret
, colon_loc
);
4805 /* Return a compound expression that performs two expressions and
4806 returns the value of the second of them.
4808 LOC is the location of the COMPOUND_EXPR. */
4811 build_compound_expr (location_t loc
, tree expr1
, tree expr2
)
4813 bool expr1_int_operands
, expr2_int_operands
;
4814 tree eptype
= NULL_TREE
;
4818 && (TREE_CODE (expr1
) == CILK_SPAWN_STMT
4819 || TREE_CODE (expr2
) == CILK_SPAWN_STMT
))
4822 "spawned function call cannot be part of a comma expression");
4823 return error_mark_node
;
4825 expr1_int_operands
= EXPR_INT_CONST_OPERANDS (expr1
);
4826 if (expr1_int_operands
)
4827 expr1
= remove_c_maybe_const_expr (expr1
);
4828 expr2_int_operands
= EXPR_INT_CONST_OPERANDS (expr2
);
4829 if (expr2_int_operands
)
4830 expr2
= remove_c_maybe_const_expr (expr2
);
4832 if (TREE_CODE (expr1
) == EXCESS_PRECISION_EXPR
)
4833 expr1
= TREE_OPERAND (expr1
, 0);
4834 if (TREE_CODE (expr2
) == EXCESS_PRECISION_EXPR
)
4836 eptype
= TREE_TYPE (expr2
);
4837 expr2
= TREE_OPERAND (expr2
, 0);
4840 if (!TREE_SIDE_EFFECTS (expr1
))
4842 /* The left-hand operand of a comma expression is like an expression
4843 statement: with -Wunused, we should warn if it doesn't have
4844 any side-effects, unless it was explicitly cast to (void). */
4845 if (warn_unused_value
)
4847 if (VOID_TYPE_P (TREE_TYPE (expr1
))
4848 && CONVERT_EXPR_P (expr1
))
4850 else if (VOID_TYPE_P (TREE_TYPE (expr1
))
4851 && TREE_CODE (expr1
) == COMPOUND_EXPR
4852 && CONVERT_EXPR_P (TREE_OPERAND (expr1
, 1)))
4853 ; /* (void) a, (void) b, c */
4855 warning_at (loc
, OPT_Wunused_value
,
4856 "left-hand operand of comma expression has no effect");
4859 else if (TREE_CODE (expr1
) == COMPOUND_EXPR
4860 && warn_unused_value
)
4863 location_t cloc
= loc
;
4864 while (TREE_CODE (r
) == COMPOUND_EXPR
)
4866 if (EXPR_HAS_LOCATION (r
))
4867 cloc
= EXPR_LOCATION (r
);
4868 r
= TREE_OPERAND (r
, 1);
4870 if (!TREE_SIDE_EFFECTS (r
)
4871 && !VOID_TYPE_P (TREE_TYPE (r
))
4872 && !CONVERT_EXPR_P (r
))
4873 warning_at (cloc
, OPT_Wunused_value
,
4874 "right-hand operand of comma expression has no effect");
4877 /* With -Wunused, we should also warn if the left-hand operand does have
4878 side-effects, but computes a value which is not used. For example, in
4879 `foo() + bar(), baz()' the result of the `+' operator is not used,
4880 so we should issue a warning. */
4881 else if (warn_unused_value
)
4882 warn_if_unused_value (expr1
, loc
);
4884 if (expr2
== error_mark_node
)
4885 return error_mark_node
;
4887 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr2
), expr1
, expr2
);
4890 && expr1_int_operands
4891 && expr2_int_operands
)
4892 ret
= note_integer_operands (ret
);
4895 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
4897 protected_set_expr_location (ret
, loc
);
4901 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4902 which we are casting. OTYPE is the type of the expression being
4903 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4904 of the cast. -Wcast-qual appeared on the command line. Named
4905 address space qualifiers are not handled here, because they result
4906 in different warnings. */
4909 handle_warn_cast_qual (location_t loc
, tree type
, tree otype
)
4911 tree in_type
= type
;
4912 tree in_otype
= otype
;
4917 /* Check that the qualifiers on IN_TYPE are a superset of the
4918 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4919 nodes is uninteresting and we stop as soon as we hit a
4920 non-POINTER_TYPE node on either type. */
4923 in_otype
= TREE_TYPE (in_otype
);
4924 in_type
= TREE_TYPE (in_type
);
4926 /* GNU C allows cv-qualified function types. 'const' means the
4927 function is very pure, 'volatile' means it can't return. We
4928 need to warn when such qualifiers are added, not when they're
4930 if (TREE_CODE (in_otype
) == FUNCTION_TYPE
4931 && TREE_CODE (in_type
) == FUNCTION_TYPE
)
4932 added
|= (TYPE_QUALS_NO_ADDR_SPACE (in_type
)
4933 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype
));
4935 discarded
|= (TYPE_QUALS_NO_ADDR_SPACE (in_otype
)
4936 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type
));
4938 while (TREE_CODE (in_type
) == POINTER_TYPE
4939 && TREE_CODE (in_otype
) == POINTER_TYPE
);
4942 warning_at (loc
, OPT_Wcast_qual
,
4943 "cast adds %q#v qualifier to function type", added
);
4946 /* There are qualifiers present in IN_OTYPE that are not present
4948 warning_at (loc
, OPT_Wcast_qual
,
4949 "cast discards %qv qualifier from pointer target type",
4952 if (added
|| discarded
)
4955 /* A cast from **T to const **T is unsafe, because it can cause a
4956 const value to be changed with no additional warning. We only
4957 issue this warning if T is the same on both sides, and we only
4958 issue the warning if there are the same number of pointers on
4959 both sides, as otherwise the cast is clearly unsafe anyhow. A
4960 cast is unsafe when a qualifier is added at one level and const
4961 is not present at all outer levels.
4963 To issue this warning, we check at each level whether the cast
4964 adds new qualifiers not already seen. We don't need to special
4965 case function types, as they won't have the same
4966 TYPE_MAIN_VARIANT. */
4968 if (TYPE_MAIN_VARIANT (in_type
) != TYPE_MAIN_VARIANT (in_otype
))
4970 if (TREE_CODE (TREE_TYPE (type
)) != POINTER_TYPE
)
4975 is_const
= TYPE_READONLY (TREE_TYPE (in_type
));
4978 in_type
= TREE_TYPE (in_type
);
4979 in_otype
= TREE_TYPE (in_otype
);
4980 if ((TYPE_QUALS (in_type
) &~ TYPE_QUALS (in_otype
)) != 0
4983 warning_at (loc
, OPT_Wcast_qual
,
4984 "to be safe all intermediate pointers in cast from "
4985 "%qT to %qT must be %<const%> qualified",
4990 is_const
= TYPE_READONLY (in_type
);
4992 while (TREE_CODE (in_type
) == POINTER_TYPE
);
4995 /* Build an expression representing a cast to type TYPE of expression EXPR.
4996 LOC is the location of the cast-- typically the open paren of the cast. */
4999 build_c_cast (location_t loc
, tree type
, tree expr
)
5003 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
5004 expr
= TREE_OPERAND (expr
, 0);
5008 if (type
== error_mark_node
|| expr
== error_mark_node
)
5009 return error_mark_node
;
5011 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5012 only in <protocol> qualifications. But when constructing cast expressions,
5013 the protocols do matter and must be kept around. */
5014 if (objc_is_object_ptr (type
) && objc_is_object_ptr (TREE_TYPE (expr
)))
5015 return build1 (NOP_EXPR
, type
, expr
);
5017 type
= TYPE_MAIN_VARIANT (type
);
5019 if (TREE_CODE (type
) == ARRAY_TYPE
)
5021 error_at (loc
, "cast specifies array type");
5022 return error_mark_node
;
5025 if (TREE_CODE (type
) == FUNCTION_TYPE
)
5027 error_at (loc
, "cast specifies function type");
5028 return error_mark_node
;
5031 if (!VOID_TYPE_P (type
))
5033 value
= require_complete_type (value
);
5034 if (value
== error_mark_node
)
5035 return error_mark_node
;
5038 if (type
== TYPE_MAIN_VARIANT (TREE_TYPE (value
)))
5040 if (TREE_CODE (type
) == RECORD_TYPE
5041 || TREE_CODE (type
) == UNION_TYPE
)
5042 pedwarn (loc
, OPT_Wpedantic
,
5043 "ISO C forbids casting nonscalar to the same type");
5045 /* Convert to remove any qualifiers from VALUE's type. */
5046 value
= convert (type
, value
);
5048 else if (TREE_CODE (type
) == UNION_TYPE
)
5052 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5053 if (TREE_TYPE (field
) != error_mark_node
5054 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field
)),
5055 TYPE_MAIN_VARIANT (TREE_TYPE (value
))))
5061 bool maybe_const
= true;
5063 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids casts to union type");
5064 t
= c_fully_fold (value
, false, &maybe_const
);
5065 t
= build_constructor_single (type
, field
, t
);
5067 t
= c_wrap_maybe_const (t
, true);
5068 t
= digest_init (loc
, type
, t
,
5069 NULL_TREE
, false, true, 0);
5070 TREE_CONSTANT (t
) = TREE_CONSTANT (value
);
5073 error_at (loc
, "cast to union type from type not present in union");
5074 return error_mark_node
;
5080 if (type
== void_type_node
)
5082 tree t
= build1 (CONVERT_EXPR
, type
, value
);
5083 SET_EXPR_LOCATION (t
, loc
);
5087 otype
= TREE_TYPE (value
);
5089 /* Optionally warn about potentially worrisome casts. */
5091 && TREE_CODE (type
) == POINTER_TYPE
5092 && TREE_CODE (otype
) == POINTER_TYPE
)
5093 handle_warn_cast_qual (loc
, type
, otype
);
5095 /* Warn about conversions between pointers to disjoint
5097 if (TREE_CODE (type
) == POINTER_TYPE
5098 && TREE_CODE (otype
) == POINTER_TYPE
5099 && !null_pointer_constant_p (value
))
5101 addr_space_t as_to
= TYPE_ADDR_SPACE (TREE_TYPE (type
));
5102 addr_space_t as_from
= TYPE_ADDR_SPACE (TREE_TYPE (otype
));
5103 addr_space_t as_common
;
5105 if (!addr_space_superset (as_to
, as_from
, &as_common
))
5107 if (ADDR_SPACE_GENERIC_P (as_from
))
5108 warning_at (loc
, 0, "cast to %s address space pointer "
5109 "from disjoint generic address space pointer",
5110 c_addr_space_name (as_to
));
5112 else if (ADDR_SPACE_GENERIC_P (as_to
))
5113 warning_at (loc
, 0, "cast to generic address space pointer "
5114 "from disjoint %s address space pointer",
5115 c_addr_space_name (as_from
));
5118 warning_at (loc
, 0, "cast to %s address space pointer "
5119 "from disjoint %s address space pointer",
5120 c_addr_space_name (as_to
),
5121 c_addr_space_name (as_from
));
5125 /* Warn about possible alignment problems. */
5126 if (STRICT_ALIGNMENT
5127 && TREE_CODE (type
) == POINTER_TYPE
5128 && TREE_CODE (otype
) == POINTER_TYPE
5129 && TREE_CODE (TREE_TYPE (otype
)) != VOID_TYPE
5130 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5131 /* Don't warn about opaque types, where the actual alignment
5132 restriction is unknown. */
5133 && !((TREE_CODE (TREE_TYPE (otype
)) == UNION_TYPE
5134 || TREE_CODE (TREE_TYPE (otype
)) == RECORD_TYPE
)
5135 && TYPE_MODE (TREE_TYPE (otype
)) == VOIDmode
)
5136 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (otype
)))
5137 warning_at (loc
, OPT_Wcast_align
,
5138 "cast increases required alignment of target type");
5140 if (TREE_CODE (type
) == INTEGER_TYPE
5141 && TREE_CODE (otype
) == POINTER_TYPE
5142 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
))
5143 /* Unlike conversion of integers to pointers, where the
5144 warning is disabled for converting constants because
5145 of cases such as SIG_*, warn about converting constant
5146 pointers to integers. In some cases it may cause unwanted
5147 sign extension, and a warning is appropriate. */
5148 warning_at (loc
, OPT_Wpointer_to_int_cast
,
5149 "cast from pointer to integer of different size");
5151 if (TREE_CODE (value
) == CALL_EXPR
5152 && TREE_CODE (type
) != TREE_CODE (otype
))
5153 warning_at (loc
, OPT_Wbad_function_cast
,
5154 "cast from function call of type %qT "
5155 "to non-matching type %qT", otype
, type
);
5157 if (TREE_CODE (type
) == POINTER_TYPE
5158 && TREE_CODE (otype
) == INTEGER_TYPE
5159 && TYPE_PRECISION (type
) != TYPE_PRECISION (otype
)
5160 /* Don't warn about converting any constant. */
5161 && !TREE_CONSTANT (value
))
5163 OPT_Wint_to_pointer_cast
, "cast to pointer from integer "
5164 "of different size");
5166 if (warn_strict_aliasing
<= 2)
5167 strict_aliasing_warning (otype
, type
, expr
);
5169 /* If pedantic, warn for conversions between function and object
5170 pointer types, except for converting a null pointer constant
5171 to function pointer type. */
5173 && TREE_CODE (type
) == POINTER_TYPE
5174 && TREE_CODE (otype
) == POINTER_TYPE
5175 && TREE_CODE (TREE_TYPE (otype
)) == FUNCTION_TYPE
5176 && TREE_CODE (TREE_TYPE (type
)) != FUNCTION_TYPE
)
5177 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5178 "conversion of function pointer to object pointer type");
5181 && TREE_CODE (type
) == POINTER_TYPE
5182 && TREE_CODE (otype
) == POINTER_TYPE
5183 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
5184 && TREE_CODE (TREE_TYPE (otype
)) != FUNCTION_TYPE
5185 && !null_pointer_constant_p (value
))
5186 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
5187 "conversion of object pointer to function pointer type");
5190 value
= convert (type
, value
);
5192 /* Ignore any integer overflow caused by the cast. */
5193 if (TREE_CODE (value
) == INTEGER_CST
&& !FLOAT_TYPE_P (otype
))
5195 if (CONSTANT_CLASS_P (ovalue
) && TREE_OVERFLOW (ovalue
))
5197 if (!TREE_OVERFLOW (value
))
5199 /* Avoid clobbering a shared constant. */
5200 value
= copy_node (value
);
5201 TREE_OVERFLOW (value
) = TREE_OVERFLOW (ovalue
);
5204 else if (TREE_OVERFLOW (value
))
5205 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5206 value
= wide_int_to_tree (TREE_TYPE (value
), value
);
5210 /* Don't let a cast be an lvalue. */
5212 value
= non_lvalue_loc (loc
, value
);
5214 /* Don't allow the results of casting to floating-point or complex
5215 types be confused with actual constants, or casts involving
5216 integer and pointer types other than direct integer-to-integer
5217 and integer-to-pointer be confused with integer constant
5218 expressions and null pointer constants. */
5219 if (TREE_CODE (value
) == REAL_CST
5220 || TREE_CODE (value
) == COMPLEX_CST
5221 || (TREE_CODE (value
) == INTEGER_CST
5222 && !((TREE_CODE (expr
) == INTEGER_CST
5223 && INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
5224 || TREE_CODE (expr
) == REAL_CST
5225 || TREE_CODE (expr
) == COMPLEX_CST
)))
5226 value
= build1 (NOP_EXPR
, type
, value
);
5228 if (CAN_HAVE_LOCATION_P (value
))
5229 SET_EXPR_LOCATION (value
, loc
);
5233 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
5234 location of the open paren of the cast, or the position of the cast
5237 c_cast_expr (location_t loc
, struct c_type_name
*type_name
, tree expr
)
5240 tree type_expr
= NULL_TREE
;
5241 bool type_expr_const
= true;
5243 int saved_wsp
= warn_strict_prototypes
;
5245 /* This avoids warnings about unprototyped casts on
5246 integers. E.g. "#define SIG_DFL (void(*)())0". */
5247 if (TREE_CODE (expr
) == INTEGER_CST
)
5248 warn_strict_prototypes
= 0;
5249 type
= groktypename (type_name
, &type_expr
, &type_expr_const
);
5250 warn_strict_prototypes
= saved_wsp
;
5252 ret
= build_c_cast (loc
, type
, expr
);
5255 bool inner_expr_const
= true;
5256 ret
= c_fully_fold (ret
, require_constant_value
, &inner_expr_const
);
5257 ret
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (ret
), type_expr
, ret
);
5258 C_MAYBE_CONST_EXPR_NON_CONST (ret
) = !(type_expr_const
5259 && inner_expr_const
);
5260 SET_EXPR_LOCATION (ret
, loc
);
5263 if (CAN_HAVE_LOCATION_P (ret
) && !EXPR_HAS_LOCATION (ret
))
5264 SET_EXPR_LOCATION (ret
, loc
);
5266 /* C++ does not permits types to be defined in a cast, but it
5267 allows references to incomplete types. */
5268 if (warn_cxx_compat
&& type_name
->specs
->typespec_kind
== ctsk_tagdef
)
5269 warning_at (loc
, OPT_Wc___compat
,
5270 "defining a type in a cast is invalid in C++");
5275 /* Build an assignment expression of lvalue LHS from value RHS.
5276 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5277 may differ from TREE_TYPE (LHS) for an enum bitfield.
5278 MODIFYCODE is the code for a binary operator that we use
5279 to combine the old value of LHS with RHS to get the new value.
5280 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5281 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5282 which may differ from TREE_TYPE (RHS) for an enum value.
5284 LOCATION is the location of the MODIFYCODE operator.
5285 RHS_LOC is the location of the RHS. */
5288 build_modify_expr (location_t location
, tree lhs
, tree lhs_origtype
,
5289 enum tree_code modifycode
,
5290 location_t rhs_loc
, tree rhs
, tree rhs_origtype
)
5294 tree rhseval
= NULL_TREE
;
5295 tree rhs_semantic_type
= NULL_TREE
;
5296 tree lhstype
= TREE_TYPE (lhs
);
5297 tree olhstype
= lhstype
;
5301 /* Types that aren't fully specified cannot be used in assignments. */
5302 lhs
= require_complete_type (lhs
);
5304 /* Avoid duplicate error messages from operands that had errors. */
5305 if (TREE_CODE (lhs
) == ERROR_MARK
|| TREE_CODE (rhs
) == ERROR_MARK
)
5306 return error_mark_node
;
5308 /* Ensure an error for assigning a non-lvalue array to an array in
5310 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
5312 error_at (location
, "assignment to expression with array type");
5313 return error_mark_node
;
5316 /* For ObjC properties, defer this check. */
5317 if (!objc_is_property_ref (lhs
) && !lvalue_or_else (location
, lhs
, lv_assign
))
5318 return error_mark_node
;
5320 is_atomic_op
= really_atomic_lvalue (lhs
);
5322 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5324 rhs_semantic_type
= TREE_TYPE (rhs
);
5325 rhs
= TREE_OPERAND (rhs
, 0);
5330 if (TREE_CODE (lhs
) == C_MAYBE_CONST_EXPR
)
5332 tree inner
= build_modify_expr (location
, C_MAYBE_CONST_EXPR_EXPR (lhs
),
5333 lhs_origtype
, modifycode
, rhs_loc
, rhs
,
5335 if (inner
== error_mark_node
)
5336 return error_mark_node
;
5337 result
= build2 (C_MAYBE_CONST_EXPR
, TREE_TYPE (inner
),
5338 C_MAYBE_CONST_EXPR_PRE (lhs
), inner
);
5339 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs
));
5340 C_MAYBE_CONST_EXPR_NON_CONST (result
) = 1;
5341 protected_set_expr_location (result
, location
);
5345 /* If a binary op has been requested, combine the old LHS value with the RHS
5346 producing the value we should actually store into the LHS. */
5348 if (modifycode
!= NOP_EXPR
)
5350 lhs
= c_fully_fold (lhs
, false, NULL
);
5351 lhs
= stabilize_reference (lhs
);
5353 /* Construct the RHS for any non-atomic compound assignemnt. */
5356 /* If in LHS op= RHS the RHS has side-effects, ensure they
5357 are preevaluated before the rest of the assignment expression's
5358 side-effects, because RHS could contain e.g. function calls
5360 if (TREE_SIDE_EFFECTS (rhs
))
5362 newrhs
= in_late_binary_op
? save_expr (rhs
) : c_save_expr (rhs
);
5365 newrhs
= build_binary_op (location
,
5366 modifycode
, lhs
, newrhs
, 1);
5368 /* The original type of the right hand side is no longer
5370 rhs_origtype
= NULL_TREE
;
5374 if (c_dialect_objc ())
5376 /* Check if we are modifying an Objective-C property reference;
5377 if so, we need to generate setter calls. */
5378 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
5382 /* Else, do the check that we postponed for Objective-C. */
5383 if (!lvalue_or_else (location
, lhs
, lv_assign
))
5384 return error_mark_node
;
5387 /* Give an error for storing in something that is 'const'. */
5389 if (TYPE_READONLY (lhstype
)
5390 || ((TREE_CODE (lhstype
) == RECORD_TYPE
5391 || TREE_CODE (lhstype
) == UNION_TYPE
)
5392 && C_TYPE_FIELDS_READONLY (lhstype
)))
5394 readonly_error (location
, lhs
, lv_assign
);
5395 return error_mark_node
;
5397 else if (TREE_READONLY (lhs
))
5398 readonly_warning (lhs
, lv_assign
);
5400 /* If storing into a structure or union member,
5401 it has probably been given type `int'.
5402 Compute the type that would go with
5403 the actual amount of storage the member occupies. */
5405 if (TREE_CODE (lhs
) == COMPONENT_REF
5406 && (TREE_CODE (lhstype
) == INTEGER_TYPE
5407 || TREE_CODE (lhstype
) == BOOLEAN_TYPE
5408 || TREE_CODE (lhstype
) == REAL_TYPE
5409 || TREE_CODE (lhstype
) == ENUMERAL_TYPE
))
5410 lhstype
= TREE_TYPE (get_unwidened (lhs
, 0));
5412 /* If storing in a field that is in actuality a short or narrower than one,
5413 we must store in the field in its actual type. */
5415 if (lhstype
!= TREE_TYPE (lhs
))
5417 lhs
= copy_node (lhs
);
5418 TREE_TYPE (lhs
) = lhstype
;
5421 /* Issue -Wc++-compat warnings about an assignment to an enum type
5422 when LHS does not have its original type. This happens for,
5423 e.g., an enum bitfield in a struct. */
5425 && lhs_origtype
!= NULL_TREE
5426 && lhs_origtype
!= lhstype
5427 && TREE_CODE (lhs_origtype
) == ENUMERAL_TYPE
)
5429 tree checktype
= (rhs_origtype
!= NULL_TREE
5432 if (checktype
!= error_mark_node
5433 && (TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (lhs_origtype
)
5434 || (is_atomic_op
&& modifycode
!= NOP_EXPR
)))
5435 warning_at (location
, OPT_Wc___compat
,
5436 "enum conversion in assignment is invalid in C++");
5439 /* If the lhs is atomic, remove that qualifier. */
5442 lhstype
= build_qualified_type (lhstype
,
5443 (TYPE_QUALS (lhstype
)
5444 & ~TYPE_QUAL_ATOMIC
));
5445 olhstype
= build_qualified_type (olhstype
,
5446 (TYPE_QUALS (lhstype
)
5447 & ~TYPE_QUAL_ATOMIC
));
5450 /* Convert new value to destination type. Fold it first, then
5451 restore any excess precision information, for the sake of
5452 conversion warnings. */
5454 if (!(is_atomic_op
&& modifycode
!= NOP_EXPR
))
5456 npc
= null_pointer_constant_p (newrhs
);
5457 newrhs
= c_fully_fold (newrhs
, false, NULL
);
5458 if (rhs_semantic_type
)
5459 newrhs
= build1 (EXCESS_PRECISION_EXPR
, rhs_semantic_type
, newrhs
);
5460 newrhs
= convert_for_assignment (location
, rhs_loc
, lhstype
, newrhs
,
5461 rhs_origtype
, ic_assign
, npc
,
5462 NULL_TREE
, NULL_TREE
, 0);
5463 if (TREE_CODE (newrhs
) == ERROR_MARK
)
5464 return error_mark_node
;
5467 /* Emit ObjC write barrier, if necessary. */
5468 if (c_dialect_objc () && flag_objc_gc
)
5470 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
5473 protected_set_expr_location (result
, location
);
5478 /* Scan operands. */
5481 result
= build_atomic_assign (location
, lhs
, modifycode
, newrhs
, false);
5484 result
= build2 (MODIFY_EXPR
, lhstype
, lhs
, newrhs
);
5485 TREE_SIDE_EFFECTS (result
) = 1;
5486 protected_set_expr_location (result
, location
);
5489 /* If we got the LHS in a different type for storing in,
5490 convert the result back to the nominal type of LHS
5491 so that the value we return always has the same type
5492 as the LHS argument. */
5494 if (olhstype
== TREE_TYPE (result
))
5497 result
= convert_for_assignment (location
, rhs_loc
, olhstype
, result
,
5498 rhs_origtype
, ic_assign
, false, NULL_TREE
,
5500 protected_set_expr_location (result
, location
);
5504 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), rhseval
, result
);
5508 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5509 This is used to implement -fplan9-extensions. */
5512 find_anonymous_field_with_type (tree struct_type
, tree type
)
5517 gcc_assert (TREE_CODE (struct_type
) == RECORD_TYPE
5518 || TREE_CODE (struct_type
) == UNION_TYPE
);
5520 for (field
= TYPE_FIELDS (struct_type
);
5522 field
= TREE_CHAIN (field
))
5524 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5525 ? c_build_qualified_type (TREE_TYPE (field
),
5527 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5528 if (DECL_NAME (field
) == NULL
5529 && comptypes (type
, fieldtype
))
5535 else if (DECL_NAME (field
) == NULL
5536 && (TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
5537 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5538 && find_anonymous_field_with_type (TREE_TYPE (field
), type
))
5548 /* RHS is an expression whose type is pointer to struct. If there is
5549 an anonymous field in RHS with type TYPE, then return a pointer to
5550 that field in RHS. This is used with -fplan9-extensions. This
5551 returns NULL if no conversion could be found. */
5554 convert_to_anonymous_field (location_t location
, tree type
, tree rhs
)
5556 tree rhs_struct_type
, lhs_main_type
;
5557 tree field
, found_field
;
5558 bool found_sub_field
;
5561 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs
)));
5562 rhs_struct_type
= TREE_TYPE (TREE_TYPE (rhs
));
5563 gcc_assert (TREE_CODE (rhs_struct_type
) == RECORD_TYPE
5564 || TREE_CODE (rhs_struct_type
) == UNION_TYPE
);
5566 gcc_assert (POINTER_TYPE_P (type
));
5567 lhs_main_type
= (TYPE_ATOMIC (TREE_TYPE (type
))
5568 ? c_build_qualified_type (TREE_TYPE (type
),
5570 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
5572 found_field
= NULL_TREE
;
5573 found_sub_field
= false;
5574 for (field
= TYPE_FIELDS (rhs_struct_type
);
5576 field
= TREE_CHAIN (field
))
5578 if (DECL_NAME (field
) != NULL_TREE
5579 || (TREE_CODE (TREE_TYPE (field
)) != RECORD_TYPE
5580 && TREE_CODE (TREE_TYPE (field
)) != UNION_TYPE
))
5582 tree fieldtype
= (TYPE_ATOMIC (TREE_TYPE (field
))
5583 ? c_build_qualified_type (TREE_TYPE (field
),
5585 : TYPE_MAIN_VARIANT (TREE_TYPE (field
)));
5586 if (comptypes (lhs_main_type
, fieldtype
))
5588 if (found_field
!= NULL_TREE
)
5590 found_field
= field
;
5592 else if (find_anonymous_field_with_type (TREE_TYPE (field
),
5595 if (found_field
!= NULL_TREE
)
5597 found_field
= field
;
5598 found_sub_field
= true;
5602 if (found_field
== NULL_TREE
)
5605 ret
= fold_build3_loc (location
, COMPONENT_REF
, TREE_TYPE (found_field
),
5606 build_fold_indirect_ref (rhs
), found_field
,
5608 ret
= build_fold_addr_expr_loc (location
, ret
);
5610 if (found_sub_field
)
5612 ret
= convert_to_anonymous_field (location
, type
, ret
);
5613 gcc_assert (ret
!= NULL_TREE
);
5619 /* Issue an error message for a bad initializer component.
5620 GMSGID identifies the message.
5621 The component name is taken from the spelling stack. */
5624 error_init (location_t loc
, const char *gmsgid
)
5628 /* The gmsgid may be a format string with %< and %>. */
5629 error_at (loc
, gmsgid
);
5630 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5632 inform (loc
, "(near initialization for %qs)", ofwhat
);
5635 /* Issue a pedantic warning for a bad initializer component. OPT is
5636 the option OPT_* (from options.h) controlling this warning or 0 if
5637 it is unconditionally given. GMSGID identifies the message. The
5638 component name is taken from the spelling stack. */
5641 pedwarn_init (location_t location
, int opt
, const char *gmsgid
)
5646 /* The gmsgid may be a format string with %< and %>. */
5647 warned
= pedwarn (location
, opt
, gmsgid
);
5648 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5649 if (*ofwhat
&& warned
)
5650 inform (location
, "(near initialization for %qs)", ofwhat
);
5653 /* Issue a warning for a bad initializer component.
5655 OPT is the OPT_W* value corresponding to the warning option that
5656 controls this warning. GMSGID identifies the message. The
5657 component name is taken from the spelling stack. */
5660 warning_init (location_t loc
, int opt
, const char *gmsgid
)
5665 /* The gmsgid may be a format string with %< and %>. */
5666 warned
= warning_at (loc
, opt
, gmsgid
);
5667 ofwhat
= print_spelling ((char *) alloca (spelling_length () + 1));
5668 if (*ofwhat
&& warned
)
5669 inform (loc
, "(near initialization for %qs)", ofwhat
);
5672 /* If TYPE is an array type and EXPR is a parenthesized string
5673 constant, warn if pedantic that EXPR is being used to initialize an
5674 object of type TYPE. */
5677 maybe_warn_string_init (location_t loc
, tree type
, struct c_expr expr
)
5680 && TREE_CODE (type
) == ARRAY_TYPE
5681 && TREE_CODE (expr
.value
) == STRING_CST
5682 && expr
.original_code
!= STRING_CST
)
5683 pedwarn_init (loc
, OPT_Wpedantic
,
5684 "array initialized from parenthesized string constant");
5687 /* Convert value RHS to type TYPE as preparation for an assignment to
5688 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5689 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5690 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5691 constant before any folding.
5692 The real work of conversion is done by `convert'.
5693 The purpose of this function is to generate error messages
5694 for assignments that are not allowed in C.
5695 ERRTYPE says whether it is argument passing, assignment,
5696 initialization or return.
5698 LOCATION is the location of the assignment, EXPR_LOC is the location of
5699 the RHS or, for a function, location of an argument.
5700 FUNCTION is a tree for the function being called.
5701 PARMNUM is the number of the argument, for printing in error messages. */
5704 convert_for_assignment (location_t location
, location_t expr_loc
, tree type
,
5705 tree rhs
, tree origtype
, enum impl_conv errtype
,
5706 bool null_pointer_constant
, tree fundecl
,
5707 tree function
, int parmnum
)
5709 enum tree_code codel
= TREE_CODE (type
);
5710 tree orig_rhs
= rhs
;
5712 enum tree_code coder
;
5713 tree rname
= NULL_TREE
;
5714 bool objc_ok
= false;
5716 if (errtype
== ic_argpass
)
5719 /* Change pointer to function to the function itself for
5721 if (TREE_CODE (function
) == ADDR_EXPR
5722 && TREE_CODE (TREE_OPERAND (function
, 0)) == FUNCTION_DECL
)
5723 function
= TREE_OPERAND (function
, 0);
5725 /* Handle an ObjC selector specially for diagnostics. */
5726 selector
= objc_message_selector ();
5728 if (selector
&& parmnum
> 2)
5735 /* This macro is used to emit diagnostics to ensure that all format
5736 strings are complete sentences, visible to gettext and checked at
5738 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
5743 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
5744 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5745 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5746 "expected %qT but argument is of type %qT", \
5750 pedwarn (LOCATION, OPT, AS); \
5753 pedwarn_init (LOCATION, OPT, IN); \
5756 pedwarn (LOCATION, OPT, RE); \
5759 gcc_unreachable (); \
5763 /* This macro is used to emit diagnostics to ensure that all format
5764 strings are complete sentences, visible to gettext and checked at
5765 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
5766 extra parameter to enumerate qualifiers. */
5767 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5772 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5773 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5774 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5775 "expected %qT but argument is of type %qT", \
5779 pedwarn (LOCATION, OPT, AS, QUALS); \
5782 pedwarn (LOCATION, OPT, IN, QUALS); \
5785 pedwarn (LOCATION, OPT, RE, QUALS); \
5788 gcc_unreachable (); \
5792 /* This macro is used to emit diagnostics to ensure that all format
5793 strings are complete sentences, visible to gettext and checked at
5794 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5795 warning_at instead of pedwarn. */
5796 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5801 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5802 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5803 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5804 "expected %qT but argument is of type %qT", \
5808 warning_at (LOCATION, OPT, AS, QUALS); \
5811 warning_at (LOCATION, OPT, IN, QUALS); \
5814 warning_at (LOCATION, OPT, RE, QUALS); \
5817 gcc_unreachable (); \
5821 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
5822 rhs
= TREE_OPERAND (rhs
, 0);
5824 rhstype
= TREE_TYPE (rhs
);
5825 coder
= TREE_CODE (rhstype
);
5827 if (coder
== ERROR_MARK
)
5828 return error_mark_node
;
5830 if (c_dialect_objc ())
5853 objc_ok
= objc_compare_types (type
, rhstype
, parmno
, rname
);
5856 if (warn_cxx_compat
)
5858 tree checktype
= origtype
!= NULL_TREE
? origtype
: rhstype
;
5859 if (checktype
!= error_mark_node
5860 && TREE_CODE (type
) == ENUMERAL_TYPE
5861 && TYPE_MAIN_VARIANT (checktype
) != TYPE_MAIN_VARIANT (type
))
5863 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wc___compat
,
5864 G_("enum conversion when passing argument "
5865 "%d of %qE is invalid in C++"),
5866 G_("enum conversion in assignment is "
5868 G_("enum conversion in initialization is "
5870 G_("enum conversion in return is "
5875 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (rhstype
))
5878 if (coder
== VOID_TYPE
)
5880 /* Except for passing an argument to an unprototyped function,
5881 this is a constraint violation. When passing an argument to
5882 an unprototyped function, it is compile-time undefined;
5883 making it a constraint in that case was rejected in
5885 error_at (location
, "void value not ignored as it ought to be");
5886 return error_mark_node
;
5888 rhs
= require_complete_type (rhs
);
5889 if (rhs
== error_mark_node
)
5890 return error_mark_node
;
5891 /* A non-reference type can convert to a reference. This handles
5892 va_start, va_copy and possibly port built-ins. */
5893 if (codel
== REFERENCE_TYPE
&& coder
!= REFERENCE_TYPE
)
5895 if (!lvalue_p (rhs
))
5897 error_at (location
, "cannot pass rvalue to reference parameter");
5898 return error_mark_node
;
5900 if (!c_mark_addressable (rhs
))
5901 return error_mark_node
;
5902 rhs
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (rhs
)), rhs
);
5903 SET_EXPR_LOCATION (rhs
, location
);
5905 rhs
= convert_for_assignment (location
, expr_loc
,
5906 build_pointer_type (TREE_TYPE (type
)),
5907 rhs
, origtype
, errtype
,
5908 null_pointer_constant
, fundecl
, function
,
5910 if (rhs
== error_mark_node
)
5911 return error_mark_node
;
5913 rhs
= build1 (NOP_EXPR
, type
, rhs
);
5914 SET_EXPR_LOCATION (rhs
, location
);
5917 /* Some types can interconvert without explicit casts. */
5918 else if (codel
== VECTOR_TYPE
&& coder
== VECTOR_TYPE
5919 && vector_types_convertible_p (type
, TREE_TYPE (rhs
), true))
5920 return convert (type
, rhs
);
5921 /* Arithmetic types all interconvert, and enum is treated like int. */
5922 else if ((codel
== INTEGER_TYPE
|| codel
== REAL_TYPE
5923 || codel
== FIXED_POINT_TYPE
5924 || codel
== ENUMERAL_TYPE
|| codel
== COMPLEX_TYPE
5925 || codel
== BOOLEAN_TYPE
)
5926 && (coder
== INTEGER_TYPE
|| coder
== REAL_TYPE
5927 || coder
== FIXED_POINT_TYPE
5928 || coder
== ENUMERAL_TYPE
|| coder
== COMPLEX_TYPE
5929 || coder
== BOOLEAN_TYPE
))
5932 bool save
= in_late_binary_op
;
5933 if (codel
== BOOLEAN_TYPE
|| codel
== COMPLEX_TYPE
5934 || (coder
== REAL_TYPE
5935 && (codel
== INTEGER_TYPE
|| codel
== ENUMERAL_TYPE
)
5936 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
5937 in_late_binary_op
= true;
5938 ret
= convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5939 ? expr_loc
: location
, type
, orig_rhs
);
5940 in_late_binary_op
= save
;
5944 /* Aggregates in different TUs might need conversion. */
5945 if ((codel
== RECORD_TYPE
|| codel
== UNION_TYPE
)
5947 && comptypes (type
, rhstype
))
5948 return convert_and_check (expr_loc
!= UNKNOWN_LOCATION
5949 ? expr_loc
: location
, type
, rhs
);
5951 /* Conversion to a transparent union or record from its member types.
5952 This applies only to function arguments. */
5953 if (((codel
== UNION_TYPE
|| codel
== RECORD_TYPE
)
5954 && TYPE_TRANSPARENT_AGGR (type
))
5955 && errtype
== ic_argpass
)
5957 tree memb
, marginal_memb
= NULL_TREE
;
5959 for (memb
= TYPE_FIELDS (type
); memb
; memb
= DECL_CHAIN (memb
))
5961 tree memb_type
= TREE_TYPE (memb
);
5963 if (comptypes (TYPE_MAIN_VARIANT (memb_type
),
5964 TYPE_MAIN_VARIANT (rhstype
)))
5967 if (TREE_CODE (memb_type
) != POINTER_TYPE
)
5970 if (coder
== POINTER_TYPE
)
5972 tree ttl
= TREE_TYPE (memb_type
);
5973 tree ttr
= TREE_TYPE (rhstype
);
5975 /* Any non-function converts to a [const][volatile] void *
5976 and vice versa; otherwise, targets must be the same.
5977 Meanwhile, the lhs target must have all the qualifiers of
5979 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
5980 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
5981 || comp_target_types (location
, memb_type
, rhstype
))
5983 int lquals
= TYPE_QUALS (ttl
) & ~TYPE_QUAL_ATOMIC
;
5984 int rquals
= TYPE_QUALS (ttr
) & ~TYPE_QUAL_ATOMIC
;
5985 /* If this type won't generate any warnings, use it. */
5986 if (lquals
== rquals
5987 || ((TREE_CODE (ttr
) == FUNCTION_TYPE
5988 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
5989 ? ((lquals
| rquals
) == rquals
)
5990 : ((lquals
| rquals
) == lquals
)))
5993 /* Keep looking for a better type, but remember this one. */
5995 marginal_memb
= memb
;
5999 /* Can convert integer zero to any pointer type. */
6000 if (null_pointer_constant
)
6002 rhs
= null_pointer_node
;
6007 if (memb
|| marginal_memb
)
6011 /* We have only a marginally acceptable member type;
6012 it needs a warning. */
6013 tree ttl
= TREE_TYPE (TREE_TYPE (marginal_memb
));
6014 tree ttr
= TREE_TYPE (rhstype
);
6016 /* Const and volatile mean something different for function
6017 types, so the usual warnings are not appropriate. */
6018 if (TREE_CODE (ttr
) == FUNCTION_TYPE
6019 && TREE_CODE (ttl
) == FUNCTION_TYPE
)
6021 /* Because const and volatile on functions are
6022 restrictions that say the function will not do
6023 certain things, it is okay to use a const or volatile
6024 function where an ordinary one is wanted, but not
6026 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6027 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6028 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6029 OPT_Wdiscarded_qualifiers
,
6030 G_("passing argument %d of %qE "
6031 "makes %q#v qualified function "
6032 "pointer from unqualified"),
6033 G_("assignment makes %q#v qualified "
6034 "function pointer from "
6036 G_("initialization makes %q#v qualified "
6037 "function pointer from "
6039 G_("return makes %q#v qualified function "
6040 "pointer from unqualified"),
6041 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6043 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr
)
6044 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl
))
6045 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6046 OPT_Wdiscarded_qualifiers
,
6047 G_("passing argument %d of %qE discards "
6048 "%qv qualifier from pointer target type"),
6049 G_("assignment discards %qv qualifier "
6050 "from pointer target type"),
6051 G_("initialization discards %qv qualifier "
6052 "from pointer target type"),
6053 G_("return discards %qv qualifier from "
6054 "pointer target type"),
6055 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6057 memb
= marginal_memb
;
6060 if (!fundecl
|| !DECL_IN_SYSTEM_HEADER (fundecl
))
6061 pedwarn (location
, OPT_Wpedantic
,
6062 "ISO C prohibits argument conversion to union type");
6064 rhs
= fold_convert_loc (location
, TREE_TYPE (memb
), rhs
);
6065 return build_constructor_single (type
, memb
, rhs
);
6069 /* Conversions among pointers */
6070 else if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
6071 && (coder
== codel
))
6073 tree ttl
= TREE_TYPE (type
);
6074 tree ttr
= TREE_TYPE (rhstype
);
6077 bool is_opaque_pointer
;
6078 int target_cmp
= 0; /* Cache comp_target_types () result. */
6082 if (TREE_CODE (mvl
) != ARRAY_TYPE
)
6083 mvl
= (TYPE_ATOMIC (mvl
)
6084 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl
),
6086 : TYPE_MAIN_VARIANT (mvl
));
6087 if (TREE_CODE (mvr
) != ARRAY_TYPE
)
6088 mvr
= (TYPE_ATOMIC (mvr
)
6089 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr
),
6091 : TYPE_MAIN_VARIANT (mvr
));
6092 /* Opaque pointers are treated like void pointers. */
6093 is_opaque_pointer
= vector_targets_convertible_p (ttl
, ttr
);
6095 /* The Plan 9 compiler permits a pointer to a struct to be
6096 automatically converted into a pointer to an anonymous field
6097 within the struct. */
6098 if (flag_plan9_extensions
6099 && (TREE_CODE (mvl
) == RECORD_TYPE
|| TREE_CODE(mvl
) == UNION_TYPE
)
6100 && (TREE_CODE (mvr
) == RECORD_TYPE
|| TREE_CODE(mvr
) == UNION_TYPE
)
6103 tree new_rhs
= convert_to_anonymous_field (location
, type
, rhs
);
6104 if (new_rhs
!= NULL_TREE
)
6107 rhstype
= TREE_TYPE (rhs
);
6108 coder
= TREE_CODE (rhstype
);
6109 ttr
= TREE_TYPE (rhstype
);
6110 mvr
= TYPE_MAIN_VARIANT (ttr
);
6114 /* C++ does not allow the implicit conversion void* -> T*. However,
6115 for the purpose of reducing the number of false positives, we
6116 tolerate the special case of
6120 where NULL is typically defined in C to be '(void *) 0'. */
6121 if (VOID_TYPE_P (ttr
) && rhs
!= null_pointer_node
&& !VOID_TYPE_P (ttl
))
6122 warning_at (errtype
== ic_argpass
? expr_loc
: location
,
6124 "request for implicit conversion "
6125 "from %qT to %qT not permitted in C++", rhstype
, type
);
6127 /* See if the pointers point to incompatible address spaces. */
6128 asl
= TYPE_ADDR_SPACE (ttl
);
6129 asr
= TYPE_ADDR_SPACE (ttr
);
6130 if (!null_pointer_constant_p (rhs
)
6131 && asr
!= asl
&& !targetm
.addr_space
.subset_p (asr
, asl
))
6136 error_at (expr_loc
, "passing argument %d of %qE from pointer to "
6137 "non-enclosed address space", parmnum
, rname
);
6140 error_at (location
, "assignment from pointer to "
6141 "non-enclosed address space");
6144 error_at (location
, "initialization from pointer to "
6145 "non-enclosed address space");
6148 error_at (location
, "return from pointer to "
6149 "non-enclosed address space");
6154 return error_mark_node
;
6157 /* Check if the right-hand side has a format attribute but the
6158 left-hand side doesn't. */
6159 if (warn_suggest_attribute_format
6160 && check_missing_format_attribute (type
, rhstype
))
6165 warning_at (expr_loc
, OPT_Wsuggest_attribute_format
,
6166 "argument %d of %qE might be "
6167 "a candidate for a format attribute",
6171 warning_at (location
, OPT_Wsuggest_attribute_format
,
6172 "assignment left-hand side might be "
6173 "a candidate for a format attribute");
6176 warning_at (location
, OPT_Wsuggest_attribute_format
,
6177 "initialization left-hand side might be "
6178 "a candidate for a format attribute");
6181 warning_at (location
, OPT_Wsuggest_attribute_format
,
6182 "return type might be "
6183 "a candidate for a format attribute");
6190 /* Any non-function converts to a [const][volatile] void *
6191 and vice versa; otherwise, targets must be the same.
6192 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
6193 if ((VOID_TYPE_P (ttl
) && !TYPE_ATOMIC (ttl
))
6194 || (VOID_TYPE_P (ttr
) && !TYPE_ATOMIC (ttr
))
6195 || (target_cmp
= comp_target_types (location
, type
, rhstype
))
6196 || is_opaque_pointer
6197 || ((c_common_unsigned_type (mvl
)
6198 == c_common_unsigned_type (mvr
))
6199 && (c_common_signed_type (mvl
)
6200 == c_common_signed_type (mvr
))
6201 && TYPE_ATOMIC (mvl
) == TYPE_ATOMIC (mvr
)))
6203 /* Warn about loss of qualifers from pointers to arrays with
6204 qualifiers on the element type. */
6205 if (TREE_CODE (ttr
) == ARRAY_TYPE
)
6207 ttr
= strip_array_types (ttr
);
6208 ttl
= strip_array_types (ttl
);
6210 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6211 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6212 WARNING_FOR_QUALIFIERS (location
, expr_loc
,
6213 OPT_Wdiscarded_array_qualifiers
,
6214 G_("passing argument %d of %qE discards "
6215 "%qv qualifier from pointer target type"),
6216 G_("assignment discards %qv qualifier "
6217 "from pointer target type"),
6218 G_("initialization discards %qv qualifier "
6219 "from pointer target type"),
6220 G_("return discards %qv qualifier from "
6221 "pointer target type"),
6222 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6225 && ((VOID_TYPE_P (ttl
) && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6228 && !null_pointer_constant
6229 && TREE_CODE (ttl
) == FUNCTION_TYPE
)))
6230 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpedantic
,
6231 G_("ISO C forbids passing argument %d of "
6232 "%qE between function pointer "
6234 G_("ISO C forbids assignment between "
6235 "function pointer and %<void *%>"),
6236 G_("ISO C forbids initialization between "
6237 "function pointer and %<void *%>"),
6238 G_("ISO C forbids return between function "
6239 "pointer and %<void *%>"));
6240 /* Const and volatile mean something different for function types,
6241 so the usual warnings are not appropriate. */
6242 else if (TREE_CODE (ttr
) != FUNCTION_TYPE
6243 && TREE_CODE (ttl
) != FUNCTION_TYPE
)
6245 /* Don't warn about loss of qualifier for conversions from
6246 qualified void* to pointers to arrays with corresponding
6247 qualifier on the element type. */
6249 ttl
= strip_array_types (ttl
);
6251 /* Assignments between atomic and non-atomic objects are OK. */
6252 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr
)
6253 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl
))
6255 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6256 OPT_Wdiscarded_qualifiers
,
6257 G_("passing argument %d of %qE discards "
6258 "%qv qualifier from pointer target type"),
6259 G_("assignment discards %qv qualifier "
6260 "from pointer target type"),
6261 G_("initialization discards %qv qualifier "
6262 "from pointer target type"),
6263 G_("return discards %qv qualifier from "
6264 "pointer target type"),
6265 TYPE_QUALS (ttr
) & ~TYPE_QUALS (ttl
));
6267 /* If this is not a case of ignoring a mismatch in signedness,
6269 else if (VOID_TYPE_P (ttl
) || VOID_TYPE_P (ttr
)
6272 /* If there is a mismatch, do warn. */
6273 else if (warn_pointer_sign
)
6274 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
, OPT_Wpointer_sign
,
6275 G_("pointer targets in passing argument "
6276 "%d of %qE differ in signedness"),
6277 G_("pointer targets in assignment "
6278 "differ in signedness"),
6279 G_("pointer targets in initialization "
6280 "differ in signedness"),
6281 G_("pointer targets in return differ "
6284 else if (TREE_CODE (ttl
) == FUNCTION_TYPE
6285 && TREE_CODE (ttr
) == FUNCTION_TYPE
)
6287 /* Because const and volatile on functions are restrictions
6288 that say the function will not do certain things,
6289 it is okay to use a const or volatile function
6290 where an ordinary one is wanted, but not vice-versa. */
6291 if (TYPE_QUALS_NO_ADDR_SPACE (ttl
)
6292 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr
))
6293 PEDWARN_FOR_QUALIFIERS (location
, expr_loc
,
6294 OPT_Wdiscarded_qualifiers
,
6295 G_("passing argument %d of %qE makes "
6296 "%q#v qualified function pointer "
6297 "from unqualified"),
6298 G_("assignment makes %q#v qualified function "
6299 "pointer from unqualified"),
6300 G_("initialization makes %q#v qualified "
6301 "function pointer from unqualified"),
6302 G_("return makes %q#v qualified function "
6303 "pointer from unqualified"),
6304 TYPE_QUALS (ttl
) & ~TYPE_QUALS (ttr
));
6308 /* Avoid warning about the volatile ObjC EH puts on decls. */
6310 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6311 OPT_Wincompatible_pointer_types
,
6312 G_("passing argument %d of %qE from "
6313 "incompatible pointer type"),
6314 G_("assignment from incompatible pointer type"),
6315 G_("initialization from incompatible "
6317 G_("return from incompatible pointer type"));
6319 return convert (type
, rhs
);
6321 else if (codel
== POINTER_TYPE
&& coder
== ARRAY_TYPE
)
6323 /* ??? This should not be an error when inlining calls to
6324 unprototyped functions. */
6325 error_at (location
, "invalid use of non-lvalue array");
6326 return error_mark_node
;
6328 else if (codel
== POINTER_TYPE
&& coder
== INTEGER_TYPE
)
6330 /* An explicit constant 0 can convert to a pointer,
6331 or one that results from arithmetic, even including
6332 a cast to integer type. */
6333 if (!null_pointer_constant
)
6334 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6335 OPT_Wint_conversion
,
6336 G_("passing argument %d of %qE makes "
6337 "pointer from integer without a cast"),
6338 G_("assignment makes pointer from integer "
6340 G_("initialization makes pointer from "
6341 "integer without a cast"),
6342 G_("return makes pointer from integer "
6345 return convert (type
, rhs
);
6347 else if (codel
== INTEGER_TYPE
&& coder
== POINTER_TYPE
)
6349 PEDWARN_FOR_ASSIGNMENT (location
, expr_loc
,
6350 OPT_Wint_conversion
,
6351 G_("passing argument %d of %qE makes integer "
6352 "from pointer without a cast"),
6353 G_("assignment makes integer from pointer "
6355 G_("initialization makes integer from pointer "
6357 G_("return makes integer from pointer "
6359 return convert (type
, rhs
);
6361 else if (codel
== BOOLEAN_TYPE
&& coder
== POINTER_TYPE
)
6364 bool save
= in_late_binary_op
;
6365 in_late_binary_op
= true;
6366 ret
= convert (type
, rhs
);
6367 in_late_binary_op
= save
;
6374 error_at (expr_loc
, "incompatible type for argument %d of %qE", parmnum
,
6376 inform ((fundecl
&& !DECL_IS_BUILTIN (fundecl
))
6377 ? DECL_SOURCE_LOCATION (fundecl
) : expr_loc
,
6378 "expected %qT but argument is of type %qT", type
, rhstype
);
6381 error_at (location
, "incompatible types when assigning to type %qT from "
6382 "type %qT", type
, rhstype
);
6386 "incompatible types when initializing type %qT using type %qT",
6391 "incompatible types when returning type %qT but %qT was "
6392 "expected", rhstype
, type
);
6398 return error_mark_node
;
6401 /* If VALUE is a compound expr all of whose expressions are constant, then
6402 return its value. Otherwise, return error_mark_node.
6404 This is for handling COMPOUND_EXPRs as initializer elements
6405 which is allowed with a warning when -pedantic is specified. */
6408 valid_compound_expr_initializer (tree value
, tree endtype
)
6410 if (TREE_CODE (value
) == COMPOUND_EXPR
)
6412 if (valid_compound_expr_initializer (TREE_OPERAND (value
, 0), endtype
)
6414 return error_mark_node
;
6415 return valid_compound_expr_initializer (TREE_OPERAND (value
, 1),
6418 else if (!initializer_constant_valid_p (value
, endtype
))
6419 return error_mark_node
;
6424 /* Perform appropriate conversions on the initial value of a variable,
6425 store it in the declaration DECL,
6426 and print any error messages that are appropriate.
6427 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6428 If the init is invalid, store an ERROR_MARK.
6430 INIT_LOC is the location of the initial value. */
6433 store_init_value (location_t init_loc
, tree decl
, tree init
, tree origtype
)
6438 /* If variable's type was invalidly declared, just ignore it. */
6440 type
= TREE_TYPE (decl
);
6441 if (TREE_CODE (type
) == ERROR_MARK
)
6444 /* Digest the specified initializer into an expression. */
6447 npc
= null_pointer_constant_p (init
);
6448 value
= digest_init (init_loc
, type
, init
, origtype
, npc
,
6449 true, TREE_STATIC (decl
));
6451 /* Store the expression if valid; else report error. */
6453 if (!in_system_header_at (input_location
)
6454 && AGGREGATE_TYPE_P (TREE_TYPE (decl
)) && !TREE_STATIC (decl
))
6455 warning (OPT_Wtraditional
, "traditional C rejects automatic "
6456 "aggregate initialization");
6458 if (value
!= error_mark_node
|| TREE_CODE (decl
) != FUNCTION_DECL
)
6459 DECL_INITIAL (decl
) = value
;
6461 /* ANSI wants warnings about out-of-range constant initializers. */
6462 STRIP_TYPE_NOPS (value
);
6463 if (TREE_STATIC (decl
))
6464 constant_expression_warning (value
);
6466 /* Check if we need to set array size from compound literal size. */
6467 if (TREE_CODE (type
) == ARRAY_TYPE
6468 && TYPE_DOMAIN (type
) == 0
6469 && value
!= error_mark_node
)
6471 tree inside_init
= init
;
6473 STRIP_TYPE_NOPS (inside_init
);
6474 inside_init
= fold (inside_init
);
6476 if (TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6478 tree cldecl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6480 if (TYPE_DOMAIN (TREE_TYPE (cldecl
)))
6482 /* For int foo[] = (int [3]){1}; we need to set array size
6483 now since later on array initializer will be just the
6484 brace enclosed list of the compound literal. */
6485 tree etype
= strip_array_types (TREE_TYPE (decl
));
6486 type
= build_distinct_type_copy (TYPE_MAIN_VARIANT (type
));
6487 TYPE_DOMAIN (type
) = TYPE_DOMAIN (TREE_TYPE (cldecl
));
6489 layout_decl (cldecl
, 0);
6491 = c_build_qualified_type (type
, TYPE_QUALS (etype
));
6497 /* Methods for storing and printing names for error messages. */
6499 /* Implement a spelling stack that allows components of a name to be pushed
6500 and popped. Each element on the stack is this structure. */
6507 unsigned HOST_WIDE_INT i
;
6512 #define SPELLING_STRING 1
6513 #define SPELLING_MEMBER 2
6514 #define SPELLING_BOUNDS 3
6516 static struct spelling
*spelling
; /* Next stack element (unused). */
6517 static struct spelling
*spelling_base
; /* Spelling stack base. */
6518 static int spelling_size
; /* Size of the spelling stack. */
6520 /* Macros to save and restore the spelling stack around push_... functions.
6521 Alternative to SAVE_SPELLING_STACK. */
6523 #define SPELLING_DEPTH() (spelling - spelling_base)
6524 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
6526 /* Push an element on the spelling stack with type KIND and assign VALUE
6529 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6531 int depth = SPELLING_DEPTH (); \
6533 if (depth >= spelling_size) \
6535 spelling_size += 10; \
6536 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6538 RESTORE_SPELLING_DEPTH (depth); \
6541 spelling->kind = (KIND); \
6542 spelling->MEMBER = (VALUE); \
6546 /* Push STRING on the stack. Printed literally. */
6549 push_string (const char *string
)
6551 PUSH_SPELLING (SPELLING_STRING
, string
, u
.s
);
6554 /* Push a member name on the stack. Printed as '.' STRING. */
6557 push_member_name (tree decl
)
6559 const char *const string
6561 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)))
6562 : _("<anonymous>"));
6563 PUSH_SPELLING (SPELLING_MEMBER
, string
, u
.s
);
6566 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
6569 push_array_bounds (unsigned HOST_WIDE_INT bounds
)
6571 PUSH_SPELLING (SPELLING_BOUNDS
, bounds
, u
.i
);
6574 /* Compute the maximum size in bytes of the printed spelling. */
6577 spelling_length (void)
6582 for (p
= spelling_base
; p
< spelling
; p
++)
6584 if (p
->kind
== SPELLING_BOUNDS
)
6587 size
+= strlen (p
->u
.s
) + 1;
6593 /* Print the spelling to BUFFER and return it. */
6596 print_spelling (char *buffer
)
6601 for (p
= spelling_base
; p
< spelling
; p
++)
6602 if (p
->kind
== SPELLING_BOUNDS
)
6604 sprintf (d
, "[" HOST_WIDE_INT_PRINT_UNSIGNED
"]", p
->u
.i
);
6610 if (p
->kind
== SPELLING_MEMBER
)
6612 for (s
= p
->u
.s
; (*d
= *s
++); d
++)
6619 /* Digest the parser output INIT as an initializer for type TYPE.
6620 Return a C expression of type TYPE to represent the initial value.
6622 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6624 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6626 If INIT is a string constant, STRICT_STRING is true if it is
6627 unparenthesized or we should not warn here for it being parenthesized.
6628 For other types of INIT, STRICT_STRING is not used.
6630 INIT_LOC is the location of the INIT.
6632 REQUIRE_CONSTANT requests an error if non-constant initializers or
6633 elements are seen. */
6636 digest_init (location_t init_loc
, tree type
, tree init
, tree origtype
,
6637 bool null_pointer_constant
, bool strict_string
,
6638 int require_constant
)
6640 enum tree_code code
= TREE_CODE (type
);
6641 tree inside_init
= init
;
6642 tree semantic_type
= NULL_TREE
;
6643 bool maybe_const
= true;
6645 if (type
== error_mark_node
6647 || error_operand_p (init
))
6648 return error_mark_node
;
6650 STRIP_TYPE_NOPS (inside_init
);
6652 if (TREE_CODE (inside_init
) == EXCESS_PRECISION_EXPR
)
6654 semantic_type
= TREE_TYPE (inside_init
);
6655 inside_init
= TREE_OPERAND (inside_init
, 0);
6657 inside_init
= c_fully_fold (inside_init
, require_constant
, &maybe_const
);
6658 inside_init
= decl_constant_value_for_optimization (inside_init
);
6660 /* Initialization of an array of chars from a string constant
6661 optionally enclosed in braces. */
6663 if (code
== ARRAY_TYPE
&& inside_init
6664 && TREE_CODE (inside_init
) == STRING_CST
)
6667 = (TYPE_ATOMIC (TREE_TYPE (type
))
6668 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type
)),
6670 : TYPE_MAIN_VARIANT (TREE_TYPE (type
)));
6671 /* Note that an array could be both an array of character type
6672 and an array of wchar_t if wchar_t is signed char or unsigned
6674 bool char_array
= (typ1
== char_type_node
6675 || typ1
== signed_char_type_node
6676 || typ1
== unsigned_char_type_node
);
6677 bool wchar_array
= !!comptypes (typ1
, wchar_type_node
);
6678 bool char16_array
= !!comptypes (typ1
, char16_type_node
);
6679 bool char32_array
= !!comptypes (typ1
, char32_type_node
);
6681 if (char_array
|| wchar_array
|| char16_array
|| char32_array
)
6684 tree typ2
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init
)));
6685 expr
.value
= inside_init
;
6686 expr
.original_code
= (strict_string
? STRING_CST
: ERROR_MARK
);
6687 expr
.original_type
= NULL
;
6688 maybe_warn_string_init (init_loc
, type
, expr
);
6690 if (TYPE_DOMAIN (type
) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
6691 pedwarn_init (init_loc
, OPT_Wpedantic
,
6692 "initialization of a flexible array member");
6694 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6695 TYPE_MAIN_VARIANT (type
)))
6700 if (typ2
!= char_type_node
)
6702 error_init (init_loc
, "char-array initialized from wide "
6704 return error_mark_node
;
6709 if (typ2
== char_type_node
)
6711 error_init (init_loc
, "wide character array initialized "
6712 "from non-wide string");
6713 return error_mark_node
;
6715 else if (!comptypes(typ1
, typ2
))
6717 error_init (init_loc
, "wide character array initialized "
6718 "from incompatible wide string");
6719 return error_mark_node
;
6723 TREE_TYPE (inside_init
) = type
;
6724 if (TYPE_DOMAIN (type
) != 0
6725 && TYPE_SIZE (type
) != 0
6726 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
)
6728 unsigned HOST_WIDE_INT len
= TREE_STRING_LENGTH (inside_init
);
6730 /* Subtract the size of a single (possibly wide) character
6731 because it's ok to ignore the terminating null char
6732 that is counted in the length of the constant. */
6733 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type
),
6735 - (TYPE_PRECISION (typ1
)
6737 pedwarn_init (init_loc
, 0,
6738 ("initializer-string for array of chars "
6740 else if (warn_cxx_compat
6741 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type
), len
))
6742 warning_at (init_loc
, OPT_Wc___compat
,
6743 ("initializer-string for array chars "
6744 "is too long for C++"));
6749 else if (INTEGRAL_TYPE_P (typ1
))
6751 error_init (init_loc
, "array of inappropriate type initialized "
6752 "from string constant");
6753 return error_mark_node
;
6757 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6758 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6759 below and handle as a constructor. */
6760 if (code
== VECTOR_TYPE
6761 && TREE_CODE (TREE_TYPE (inside_init
)) == VECTOR_TYPE
6762 && vector_types_convertible_p (TREE_TYPE (inside_init
), type
, true)
6763 && TREE_CONSTANT (inside_init
))
6765 if (TREE_CODE (inside_init
) == VECTOR_CST
6766 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6767 TYPE_MAIN_VARIANT (type
)))
6770 if (TREE_CODE (inside_init
) == CONSTRUCTOR
)
6772 unsigned HOST_WIDE_INT ix
;
6774 bool constant_p
= true;
6776 /* Iterate through elements and check if all constructor
6777 elements are *_CSTs. */
6778 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init
), ix
, value
)
6779 if (!CONSTANT_CLASS_P (value
))
6786 return build_vector_from_ctor (type
,
6787 CONSTRUCTOR_ELTS (inside_init
));
6791 if (warn_sequence_point
)
6792 verify_sequence_points (inside_init
);
6794 /* Any type can be initialized
6795 from an expression of the same type, optionally with braces. */
6797 if (inside_init
&& TREE_TYPE (inside_init
) != 0
6798 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init
)),
6799 TYPE_MAIN_VARIANT (type
))
6800 || (code
== ARRAY_TYPE
6801 && comptypes (TREE_TYPE (inside_init
), type
))
6802 || (code
== VECTOR_TYPE
6803 && comptypes (TREE_TYPE (inside_init
), type
))
6804 || (code
== POINTER_TYPE
6805 && TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
6806 && comptypes (TREE_TYPE (TREE_TYPE (inside_init
)),
6807 TREE_TYPE (type
)))))
6809 if (code
== POINTER_TYPE
)
6811 if (TREE_CODE (TREE_TYPE (inside_init
)) == ARRAY_TYPE
)
6813 if (TREE_CODE (inside_init
) == STRING_CST
6814 || TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6815 inside_init
= array_to_pointer_conversion
6816 (init_loc
, inside_init
);
6819 error_init (init_loc
, "invalid use of non-lvalue array");
6820 return error_mark_node
;
6825 if (code
== VECTOR_TYPE
)
6826 /* Although the types are compatible, we may require a
6828 inside_init
= convert (type
, inside_init
);
6830 if (require_constant
6831 && TREE_CODE (inside_init
) == COMPOUND_LITERAL_EXPR
)
6833 /* As an extension, allow initializing objects with static storage
6834 duration with compound literals (which are then treated just as
6835 the brace enclosed list they contain). Also allow this for
6836 vectors, as we can only assign them with compound literals. */
6837 if (flag_isoc99
&& code
!= VECTOR_TYPE
)
6838 pedwarn_init (init_loc
, OPT_Wpedantic
, "initializer element "
6840 tree decl
= COMPOUND_LITERAL_EXPR_DECL (inside_init
);
6841 inside_init
= DECL_INITIAL (decl
);
6844 if (code
== ARRAY_TYPE
&& TREE_CODE (inside_init
) != STRING_CST
6845 && TREE_CODE (inside_init
) != CONSTRUCTOR
)
6847 error_init (init_loc
, "array initialized from non-constant array "
6849 return error_mark_node
;
6852 /* Compound expressions can only occur here if -Wpedantic or
6853 -pedantic-errors is specified. In the later case, we always want
6854 an error. In the former case, we simply want a warning. */
6855 if (require_constant
&& pedantic
6856 && TREE_CODE (inside_init
) == COMPOUND_EXPR
)
6859 = valid_compound_expr_initializer (inside_init
,
6860 TREE_TYPE (inside_init
));
6861 if (inside_init
== error_mark_node
)
6862 error_init (init_loc
, "initializer element is not constant");
6864 pedwarn_init (init_loc
, OPT_Wpedantic
,
6865 "initializer element is not constant");
6866 if (flag_pedantic_errors
)
6867 inside_init
= error_mark_node
;
6869 else if (require_constant
6870 && !initializer_constant_valid_p (inside_init
,
6871 TREE_TYPE (inside_init
)))
6873 error_init (init_loc
, "initializer element is not constant");
6874 inside_init
= error_mark_node
;
6876 else if (require_constant
&& !maybe_const
)
6877 pedwarn_init (init_loc
, 0,
6878 "initializer element is not a constant expression");
6880 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6881 if (TREE_CODE (TREE_TYPE (inside_init
)) == POINTER_TYPE
)
6882 inside_init
= convert_for_assignment (init_loc
, UNKNOWN_LOCATION
,
6883 type
, inside_init
, origtype
,
6884 ic_init
, null_pointer_constant
,
6885 NULL_TREE
, NULL_TREE
, 0);
6889 /* Handle scalar types, including conversions. */
6891 if (code
== INTEGER_TYPE
|| code
== REAL_TYPE
|| code
== FIXED_POINT_TYPE
6892 || code
== POINTER_TYPE
|| code
== ENUMERAL_TYPE
|| code
== BOOLEAN_TYPE
6893 || code
== COMPLEX_TYPE
|| code
== VECTOR_TYPE
)
6895 if (TREE_CODE (TREE_TYPE (init
)) == ARRAY_TYPE
6896 && (TREE_CODE (init
) == STRING_CST
6897 || TREE_CODE (init
) == COMPOUND_LITERAL_EXPR
))
6898 inside_init
= init
= array_to_pointer_conversion (init_loc
, init
);
6900 inside_init
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
6903 = convert_for_assignment (init_loc
, UNKNOWN_LOCATION
, type
,
6904 inside_init
, origtype
, ic_init
,
6905 null_pointer_constant
, NULL_TREE
, NULL_TREE
,
6908 /* Check to see if we have already given an error message. */
6909 if (inside_init
== error_mark_node
)
6911 else if (require_constant
&& !TREE_CONSTANT (inside_init
))
6913 error_init (init_loc
, "initializer element is not constant");
6914 inside_init
= error_mark_node
;
6916 else if (require_constant
6917 && !initializer_constant_valid_p (inside_init
,
6918 TREE_TYPE (inside_init
)))
6920 error_init (init_loc
, "initializer element is not computable at "
6922 inside_init
= error_mark_node
;
6924 else if (require_constant
&& !maybe_const
)
6925 pedwarn_init (init_loc
, 0,
6926 "initializer element is not a constant expression");
6931 /* Come here only for records and arrays. */
6933 if (COMPLETE_TYPE_P (type
) && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
6935 error_init (init_loc
, "variable-sized object may not be initialized");
6936 return error_mark_node
;
6939 error_init (init_loc
, "invalid initializer");
6940 return error_mark_node
;
6943 /* Handle initializers that use braces. */
6945 /* Type of object we are accumulating a constructor for.
6946 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6947 static tree constructor_type
;
6949 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6951 static tree constructor_fields
;
6953 /* For an ARRAY_TYPE, this is the specified index
6954 at which to store the next element we get. */
6955 static tree constructor_index
;
6957 /* For an ARRAY_TYPE, this is the maximum index. */
6958 static tree constructor_max_index
;
6960 /* For a RECORD_TYPE, this is the first field not yet written out. */
6961 static tree constructor_unfilled_fields
;
6963 /* For an ARRAY_TYPE, this is the index of the first element
6964 not yet written out. */
6965 static tree constructor_unfilled_index
;
6967 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6968 This is so we can generate gaps between fields, when appropriate. */
6969 static tree constructor_bit_index
;
6971 /* If we are saving up the elements rather than allocating them,
6972 this is the list of elements so far (in reverse order,
6973 most recent first). */
6974 static vec
<constructor_elt
, va_gc
> *constructor_elements
;
6976 /* 1 if constructor should be incrementally stored into a constructor chain,
6977 0 if all the elements should be kept in AVL tree. */
6978 static int constructor_incremental
;
6980 /* 1 if so far this constructor's elements are all compile-time constants. */
6981 static int constructor_constant
;
6983 /* 1 if so far this constructor's elements are all valid address constants. */
6984 static int constructor_simple
;
6986 /* 1 if this constructor has an element that cannot be part of a
6987 constant expression. */
6988 static int constructor_nonconst
;
6990 /* 1 if this constructor is erroneous so far. */
6991 static int constructor_erroneous
;
6993 /* 1 if this constructor is the universal zero initializer { 0 }. */
6994 static int constructor_zeroinit
;
6996 /* Structure for managing pending initializer elements, organized as an
7001 struct init_node
*left
, *right
;
7002 struct init_node
*parent
;
7009 /* Tree of pending elements at this constructor level.
7010 These are elements encountered out of order
7011 which belong at places we haven't reached yet in actually
7013 Will never hold tree nodes across GC runs. */
7014 static struct init_node
*constructor_pending_elts
;
7016 /* The SPELLING_DEPTH of this constructor. */
7017 static int constructor_depth
;
7019 /* DECL node for which an initializer is being read.
7020 0 means we are reading a constructor expression
7021 such as (struct foo) {...}. */
7022 static tree constructor_decl
;
7024 /* Nonzero if this is an initializer for a top-level decl. */
7025 static int constructor_top_level
;
7027 /* Nonzero if there were any member designators in this initializer. */
7028 static int constructor_designated
;
7030 /* Nesting depth of designator list. */
7031 static int designator_depth
;
7033 /* Nonzero if there were diagnosed errors in this designator list. */
7034 static int designator_erroneous
;
7037 /* This stack has a level for each implicit or explicit level of
7038 structuring in the initializer, including the outermost one. It
7039 saves the values of most of the variables above. */
7041 struct constructor_range_stack
;
7043 struct constructor_stack
7045 struct constructor_stack
*next
;
7050 tree unfilled_index
;
7051 tree unfilled_fields
;
7053 vec
<constructor_elt
, va_gc
> *elements
;
7054 struct init_node
*pending_elts
;
7057 /* If value nonzero, this value should replace the entire
7058 constructor at this level. */
7059 struct c_expr replacement_value
;
7060 struct constructor_range_stack
*range_stack
;
7069 int designator_depth
;
7072 static struct constructor_stack
*constructor_stack
;
7074 /* This stack represents designators from some range designator up to
7075 the last designator in the list. */
7077 struct constructor_range_stack
7079 struct constructor_range_stack
*next
, *prev
;
7080 struct constructor_stack
*stack
;
7087 static struct constructor_range_stack
*constructor_range_stack
;
7089 /* This stack records separate initializers that are nested.
7090 Nested initializers can't happen in ANSI C, but GNU C allows them
7091 in cases like { ... (struct foo) { ... } ... }. */
7093 struct initializer_stack
7095 struct initializer_stack
*next
;
7097 struct constructor_stack
*constructor_stack
;
7098 struct constructor_range_stack
*constructor_range_stack
;
7099 vec
<constructor_elt
, va_gc
> *elements
;
7100 struct spelling
*spelling
;
7101 struct spelling
*spelling_base
;
7104 char require_constant_value
;
7105 char require_constant_elements
;
7108 static struct initializer_stack
*initializer_stack
;
7110 /* Prepare to parse and output the initializer for variable DECL. */
7113 start_init (tree decl
, tree asmspec_tree ATTRIBUTE_UNUSED
, int top_level
)
7116 struct initializer_stack
*p
= XNEW (struct initializer_stack
);
7118 p
->decl
= constructor_decl
;
7119 p
->require_constant_value
= require_constant_value
;
7120 p
->require_constant_elements
= require_constant_elements
;
7121 p
->constructor_stack
= constructor_stack
;
7122 p
->constructor_range_stack
= constructor_range_stack
;
7123 p
->elements
= constructor_elements
;
7124 p
->spelling
= spelling
;
7125 p
->spelling_base
= spelling_base
;
7126 p
->spelling_size
= spelling_size
;
7127 p
->top_level
= constructor_top_level
;
7128 p
->next
= initializer_stack
;
7129 initializer_stack
= p
;
7131 constructor_decl
= decl
;
7132 constructor_designated
= 0;
7133 constructor_top_level
= top_level
;
7135 if (decl
!= 0 && decl
!= error_mark_node
)
7137 require_constant_value
= TREE_STATIC (decl
);
7138 require_constant_elements
7139 = ((TREE_STATIC (decl
) || (pedantic
&& !flag_isoc99
))
7140 /* For a scalar, you can always use any value to initialize,
7141 even within braces. */
7142 && (TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
7143 || TREE_CODE (TREE_TYPE (decl
)) == RECORD_TYPE
7144 || TREE_CODE (TREE_TYPE (decl
)) == UNION_TYPE
7145 || TREE_CODE (TREE_TYPE (decl
)) == QUAL_UNION_TYPE
));
7146 locus
= identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl
)));
7150 require_constant_value
= 0;
7151 require_constant_elements
= 0;
7152 locus
= _("(anonymous)");
7155 constructor_stack
= 0;
7156 constructor_range_stack
= 0;
7158 found_missing_braces
= 0;
7162 RESTORE_SPELLING_DEPTH (0);
7165 push_string (locus
);
7171 struct initializer_stack
*p
= initializer_stack
;
7173 /* Free the whole constructor stack of this initializer. */
7174 while (constructor_stack
)
7176 struct constructor_stack
*q
= constructor_stack
;
7177 constructor_stack
= q
->next
;
7181 gcc_assert (!constructor_range_stack
);
7183 /* Pop back to the data of the outer initializer (if any). */
7184 free (spelling_base
);
7186 constructor_decl
= p
->decl
;
7187 require_constant_value
= p
->require_constant_value
;
7188 require_constant_elements
= p
->require_constant_elements
;
7189 constructor_stack
= p
->constructor_stack
;
7190 constructor_range_stack
= p
->constructor_range_stack
;
7191 constructor_elements
= p
->elements
;
7192 spelling
= p
->spelling
;
7193 spelling_base
= p
->spelling_base
;
7194 spelling_size
= p
->spelling_size
;
7195 constructor_top_level
= p
->top_level
;
7196 initializer_stack
= p
->next
;
7200 /* Call here when we see the initializer is surrounded by braces.
7201 This is instead of a call to push_init_level;
7202 it is matched by a call to pop_init_level.
7204 TYPE is the type to initialize, for a constructor expression.
7205 For an initializer for a decl, TYPE is zero. */
7208 really_start_incremental_init (tree type
)
7210 struct constructor_stack
*p
= XNEW (struct constructor_stack
);
7213 type
= TREE_TYPE (constructor_decl
);
7215 if (TREE_CODE (type
) == VECTOR_TYPE
7216 && TYPE_VECTOR_OPAQUE (type
))
7217 error ("opaque vector types cannot be initialized");
7219 p
->type
= constructor_type
;
7220 p
->fields
= constructor_fields
;
7221 p
->index
= constructor_index
;
7222 p
->max_index
= constructor_max_index
;
7223 p
->unfilled_index
= constructor_unfilled_index
;
7224 p
->unfilled_fields
= constructor_unfilled_fields
;
7225 p
->bit_index
= constructor_bit_index
;
7226 p
->elements
= constructor_elements
;
7227 p
->constant
= constructor_constant
;
7228 p
->simple
= constructor_simple
;
7229 p
->nonconst
= constructor_nonconst
;
7230 p
->erroneous
= constructor_erroneous
;
7231 p
->pending_elts
= constructor_pending_elts
;
7232 p
->depth
= constructor_depth
;
7233 p
->replacement_value
.value
= 0;
7234 p
->replacement_value
.original_code
= ERROR_MARK
;
7235 p
->replacement_value
.original_type
= NULL
;
7239 p
->incremental
= constructor_incremental
;
7240 p
->designated
= constructor_designated
;
7241 p
->designator_depth
= designator_depth
;
7243 constructor_stack
= p
;
7245 constructor_constant
= 1;
7246 constructor_simple
= 1;
7247 constructor_nonconst
= 0;
7248 constructor_depth
= SPELLING_DEPTH ();
7249 constructor_elements
= NULL
;
7250 constructor_pending_elts
= 0;
7251 constructor_type
= type
;
7252 constructor_incremental
= 1;
7253 constructor_designated
= 0;
7254 constructor_zeroinit
= 1;
7255 designator_depth
= 0;
7256 designator_erroneous
= 0;
7258 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7259 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7261 constructor_fields
= TYPE_FIELDS (constructor_type
);
7262 /* Skip any nameless bit fields at the beginning. */
7263 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7264 && DECL_NAME (constructor_fields
) == 0)
7265 constructor_fields
= DECL_CHAIN (constructor_fields
);
7267 constructor_unfilled_fields
= constructor_fields
;
7268 constructor_bit_index
= bitsize_zero_node
;
7270 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7272 if (TYPE_DOMAIN (constructor_type
))
7274 constructor_max_index
7275 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7277 /* Detect non-empty initializations of zero-length arrays. */
7278 if (constructor_max_index
== NULL_TREE
7279 && TYPE_SIZE (constructor_type
))
7280 constructor_max_index
= integer_minus_one_node
;
7282 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7283 to initialize VLAs will cause a proper error; avoid tree
7284 checking errors as well by setting a safe value. */
7285 if (constructor_max_index
7286 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7287 constructor_max_index
= integer_minus_one_node
;
7290 = convert (bitsizetype
,
7291 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7295 constructor_index
= bitsize_zero_node
;
7296 constructor_max_index
= NULL_TREE
;
7299 constructor_unfilled_index
= constructor_index
;
7301 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7303 /* Vectors are like simple fixed-size arrays. */
7304 constructor_max_index
=
7305 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7306 constructor_index
= bitsize_zero_node
;
7307 constructor_unfilled_index
= constructor_index
;
7311 /* Handle the case of int x = {5}; */
7312 constructor_fields
= constructor_type
;
7313 constructor_unfilled_fields
= constructor_type
;
7317 /* Push down into a subobject, for initialization.
7318 If this is for an explicit set of braces, IMPLICIT is 0.
7319 If it is because the next element belongs at a lower level,
7320 IMPLICIT is 1 (or 2 if the push is because of designator list). */
7323 push_init_level (location_t loc
, int implicit
,
7324 struct obstack
*braced_init_obstack
)
7326 struct constructor_stack
*p
;
7327 tree value
= NULL_TREE
;
7329 /* If we've exhausted any levels that didn't have braces,
7330 pop them now. If implicit == 1, this will have been done in
7331 process_init_element; do not repeat it here because in the case
7332 of excess initializers for an empty aggregate this leads to an
7333 infinite cycle of popping a level and immediately recreating
7337 while (constructor_stack
->implicit
)
7339 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7340 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7341 && constructor_fields
== 0)
7342 process_init_element (input_location
,
7343 pop_init_level (loc
, 1, braced_init_obstack
),
7344 true, braced_init_obstack
);
7345 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
7346 && constructor_max_index
7347 && tree_int_cst_lt (constructor_max_index
,
7349 process_init_element (input_location
,
7350 pop_init_level (loc
, 1, braced_init_obstack
),
7351 true, braced_init_obstack
);
7357 /* Unless this is an explicit brace, we need to preserve previous
7361 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
7362 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7363 && constructor_fields
)
7364 value
= find_init_member (constructor_fields
, braced_init_obstack
);
7365 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7366 value
= find_init_member (constructor_index
, braced_init_obstack
);
7369 p
= XNEW (struct constructor_stack
);
7370 p
->type
= constructor_type
;
7371 p
->fields
= constructor_fields
;
7372 p
->index
= constructor_index
;
7373 p
->max_index
= constructor_max_index
;
7374 p
->unfilled_index
= constructor_unfilled_index
;
7375 p
->unfilled_fields
= constructor_unfilled_fields
;
7376 p
->bit_index
= constructor_bit_index
;
7377 p
->elements
= constructor_elements
;
7378 p
->constant
= constructor_constant
;
7379 p
->simple
= constructor_simple
;
7380 p
->nonconst
= constructor_nonconst
;
7381 p
->erroneous
= constructor_erroneous
;
7382 p
->pending_elts
= constructor_pending_elts
;
7383 p
->depth
= constructor_depth
;
7384 p
->replacement_value
.value
= 0;
7385 p
->replacement_value
.original_code
= ERROR_MARK
;
7386 p
->replacement_value
.original_type
= NULL
;
7387 p
->implicit
= implicit
;
7389 p
->incremental
= constructor_incremental
;
7390 p
->designated
= constructor_designated
;
7391 p
->designator_depth
= designator_depth
;
7392 p
->next
= constructor_stack
;
7394 constructor_stack
= p
;
7396 constructor_constant
= 1;
7397 constructor_simple
= 1;
7398 constructor_nonconst
= 0;
7399 constructor_depth
= SPELLING_DEPTH ();
7400 constructor_elements
= NULL
;
7401 constructor_incremental
= 1;
7402 constructor_designated
= 0;
7403 constructor_pending_elts
= 0;
7406 p
->range_stack
= constructor_range_stack
;
7407 constructor_range_stack
= 0;
7408 designator_depth
= 0;
7409 designator_erroneous
= 0;
7412 /* Don't die if an entire brace-pair level is superfluous
7413 in the containing level. */
7414 if (constructor_type
== 0)
7416 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
7417 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7419 /* Don't die if there are extra init elts at the end. */
7420 if (constructor_fields
== 0)
7421 constructor_type
= 0;
7424 constructor_type
= TREE_TYPE (constructor_fields
);
7425 push_member_name (constructor_fields
);
7426 constructor_depth
++;
7428 /* If upper initializer is designated, then mark this as
7429 designated too to prevent bogus warnings. */
7430 constructor_designated
= p
->designated
;
7432 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7434 constructor_type
= TREE_TYPE (constructor_type
);
7435 push_array_bounds (tree_to_uhwi (constructor_index
));
7436 constructor_depth
++;
7439 if (constructor_type
== 0)
7441 error_init (loc
, "extra brace group at end of initializer");
7442 constructor_fields
= 0;
7443 constructor_unfilled_fields
= 0;
7447 if (value
&& TREE_CODE (value
) == CONSTRUCTOR
)
7449 constructor_constant
= TREE_CONSTANT (value
);
7450 constructor_simple
= TREE_STATIC (value
);
7451 constructor_nonconst
= CONSTRUCTOR_NON_CONST (value
);
7452 constructor_elements
= CONSTRUCTOR_ELTS (value
);
7453 if (!vec_safe_is_empty (constructor_elements
)
7454 && (TREE_CODE (constructor_type
) == RECORD_TYPE
7455 || TREE_CODE (constructor_type
) == ARRAY_TYPE
))
7456 set_nonincremental_init (braced_init_obstack
);
7460 found_missing_braces
= 1;
7462 if (TREE_CODE (constructor_type
) == RECORD_TYPE
7463 || TREE_CODE (constructor_type
) == UNION_TYPE
)
7465 constructor_fields
= TYPE_FIELDS (constructor_type
);
7466 /* Skip any nameless bit fields at the beginning. */
7467 while (constructor_fields
!= 0 && DECL_C_BIT_FIELD (constructor_fields
)
7468 && DECL_NAME (constructor_fields
) == 0)
7469 constructor_fields
= DECL_CHAIN (constructor_fields
);
7471 constructor_unfilled_fields
= constructor_fields
;
7472 constructor_bit_index
= bitsize_zero_node
;
7474 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
7476 /* Vectors are like simple fixed-size arrays. */
7477 constructor_max_index
=
7478 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type
) - 1);
7479 constructor_index
= bitsize_int (0);
7480 constructor_unfilled_index
= constructor_index
;
7482 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7484 if (TYPE_DOMAIN (constructor_type
))
7486 constructor_max_index
7487 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
));
7489 /* Detect non-empty initializations of zero-length arrays. */
7490 if (constructor_max_index
== NULL_TREE
7491 && TYPE_SIZE (constructor_type
))
7492 constructor_max_index
= integer_minus_one_node
;
7494 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7495 to initialize VLAs will cause a proper error; avoid tree
7496 checking errors as well by setting a safe value. */
7497 if (constructor_max_index
7498 && TREE_CODE (constructor_max_index
) != INTEGER_CST
)
7499 constructor_max_index
= integer_minus_one_node
;
7502 = convert (bitsizetype
,
7503 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
7506 constructor_index
= bitsize_zero_node
;
7508 constructor_unfilled_index
= constructor_index
;
7509 if (value
&& TREE_CODE (value
) == STRING_CST
)
7511 /* We need to split the char/wchar array into individual
7512 characters, so that we don't have to special case it
7514 set_nonincremental_init_from_string (value
, braced_init_obstack
);
7519 if (constructor_type
!= error_mark_node
)
7520 warning_init (input_location
, 0, "braces around scalar initializer");
7521 constructor_fields
= constructor_type
;
7522 constructor_unfilled_fields
= constructor_type
;
7526 /* At the end of an implicit or explicit brace level,
7527 finish up that level of constructor. If a single expression
7528 with redundant braces initialized that level, return the
7529 c_expr structure for that expression. Otherwise, the original_code
7530 element is set to ERROR_MARK.
7531 If we were outputting the elements as they are read, return 0 as the value
7532 from inner levels (process_init_element ignores that),
7533 but return error_mark_node as the value from the outermost level
7534 (that's what we want to put in DECL_INITIAL).
7535 Otherwise, return a CONSTRUCTOR expression as the value. */
7538 pop_init_level (location_t loc
, int implicit
,
7539 struct obstack
*braced_init_obstack
)
7541 struct constructor_stack
*p
;
7544 ret
.original_code
= ERROR_MARK
;
7545 ret
.original_type
= NULL
;
7549 /* When we come to an explicit close brace,
7550 pop any inner levels that didn't have explicit braces. */
7551 while (constructor_stack
->implicit
)
7552 process_init_element (input_location
,
7553 pop_init_level (loc
, 1, braced_init_obstack
),
7554 true, braced_init_obstack
);
7555 gcc_assert (!constructor_range_stack
);
7558 /* Now output all pending elements. */
7559 constructor_incremental
= 1;
7560 output_pending_init_elements (1, braced_init_obstack
);
7562 p
= constructor_stack
;
7564 /* Error for initializing a flexible array member, or a zero-length
7565 array member in an inappropriate context. */
7566 if (constructor_type
&& constructor_fields
7567 && TREE_CODE (constructor_type
) == ARRAY_TYPE
7568 && TYPE_DOMAIN (constructor_type
)
7569 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type
)))
7571 /* Silently discard empty initializations. The parser will
7572 already have pedwarned for empty brackets. */
7573 if (integer_zerop (constructor_unfilled_index
))
7574 constructor_type
= NULL_TREE
;
7577 gcc_assert (!TYPE_SIZE (constructor_type
));
7579 if (constructor_depth
> 2)
7580 error_init (loc
, "initialization of flexible array member in a nested context");
7582 pedwarn_init (loc
, OPT_Wpedantic
,
7583 "initialization of a flexible array member");
7585 /* We have already issued an error message for the existence
7586 of a flexible array member not at the end of the structure.
7587 Discard the initializer so that we do not die later. */
7588 if (DECL_CHAIN (constructor_fields
) != NULL_TREE
)
7589 constructor_type
= NULL_TREE
;
7593 switch (vec_safe_length (constructor_elements
))
7596 /* Initialization with { } counts as zeroinit. */
7597 constructor_zeroinit
= 1;
7600 /* This might be zeroinit as well. */
7601 if (integer_zerop ((*constructor_elements
)[0].value
))
7602 constructor_zeroinit
= 1;
7605 /* If the constructor has more than one element, it can't be { 0 }. */
7606 constructor_zeroinit
= 0;
7610 /* Warn when some structs are initialized with direct aggregation. */
7611 if (!implicit
&& found_missing_braces
&& warn_missing_braces
7612 && !constructor_zeroinit
)
7613 warning_init (loc
, OPT_Wmissing_braces
,
7614 "missing braces around initializer");
7616 /* Warn when some struct elements are implicitly initialized to zero. */
7617 if (warn_missing_field_initializers
7619 && TREE_CODE (constructor_type
) == RECORD_TYPE
7620 && constructor_unfilled_fields
)
7622 /* Do not warn for flexible array members or zero-length arrays. */
7623 while (constructor_unfilled_fields
7624 && (!DECL_SIZE (constructor_unfilled_fields
)
7625 || integer_zerop (DECL_SIZE (constructor_unfilled_fields
))))
7626 constructor_unfilled_fields
= DECL_CHAIN (constructor_unfilled_fields
);
7628 if (constructor_unfilled_fields
7629 /* Do not warn if this level of the initializer uses member
7630 designators; it is likely to be deliberate. */
7631 && !constructor_designated
7632 /* Do not warn about initializing with { 0 } or with { }. */
7633 && !constructor_zeroinit
)
7635 if (warning_at (input_location
, OPT_Wmissing_field_initializers
,
7636 "missing initializer for field %qD of %qT",
7637 constructor_unfilled_fields
,
7639 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields
),
7640 "%qD declared here", constructor_unfilled_fields
);
7644 /* Pad out the end of the structure. */
7645 if (p
->replacement_value
.value
)
7646 /* If this closes a superfluous brace pair,
7647 just pass out the element between them. */
7648 ret
= p
->replacement_value
;
7649 else if (constructor_type
== 0)
7651 else if (TREE_CODE (constructor_type
) != RECORD_TYPE
7652 && TREE_CODE (constructor_type
) != UNION_TYPE
7653 && TREE_CODE (constructor_type
) != ARRAY_TYPE
7654 && TREE_CODE (constructor_type
) != VECTOR_TYPE
)
7656 /* A nonincremental scalar initializer--just return
7657 the element, after verifying there is just one. */
7658 if (vec_safe_is_empty (constructor_elements
))
7660 if (!constructor_erroneous
)
7661 error_init (loc
, "empty scalar initializer");
7662 ret
.value
= error_mark_node
;
7664 else if (vec_safe_length (constructor_elements
) != 1)
7666 error_init (loc
, "extra elements in scalar initializer");
7667 ret
.value
= (*constructor_elements
)[0].value
;
7670 ret
.value
= (*constructor_elements
)[0].value
;
7674 if (constructor_erroneous
)
7675 ret
.value
= error_mark_node
;
7678 ret
.value
= build_constructor (constructor_type
,
7679 constructor_elements
);
7680 if (constructor_constant
)
7681 TREE_CONSTANT (ret
.value
) = 1;
7682 if (constructor_constant
&& constructor_simple
)
7683 TREE_STATIC (ret
.value
) = 1;
7684 if (constructor_nonconst
)
7685 CONSTRUCTOR_NON_CONST (ret
.value
) = 1;
7689 if (ret
.value
&& TREE_CODE (ret
.value
) != CONSTRUCTOR
)
7691 if (constructor_nonconst
)
7692 ret
.original_code
= C_MAYBE_CONST_EXPR
;
7693 else if (ret
.original_code
== C_MAYBE_CONST_EXPR
)
7694 ret
.original_code
= ERROR_MARK
;
7697 constructor_type
= p
->type
;
7698 constructor_fields
= p
->fields
;
7699 constructor_index
= p
->index
;
7700 constructor_max_index
= p
->max_index
;
7701 constructor_unfilled_index
= p
->unfilled_index
;
7702 constructor_unfilled_fields
= p
->unfilled_fields
;
7703 constructor_bit_index
= p
->bit_index
;
7704 constructor_elements
= p
->elements
;
7705 constructor_constant
= p
->constant
;
7706 constructor_simple
= p
->simple
;
7707 constructor_nonconst
= p
->nonconst
;
7708 constructor_erroneous
= p
->erroneous
;
7709 constructor_incremental
= p
->incremental
;
7710 constructor_designated
= p
->designated
;
7711 designator_depth
= p
->designator_depth
;
7712 constructor_pending_elts
= p
->pending_elts
;
7713 constructor_depth
= p
->depth
;
7715 constructor_range_stack
= p
->range_stack
;
7716 RESTORE_SPELLING_DEPTH (constructor_depth
);
7718 constructor_stack
= p
->next
;
7721 if (ret
.value
== 0 && constructor_stack
== 0)
7722 ret
.value
= error_mark_node
;
7726 /* Common handling for both array range and field name designators.
7727 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7730 set_designator (location_t loc
, int array
,
7731 struct obstack
*braced_init_obstack
)
7734 enum tree_code subcode
;
7736 /* Don't die if an entire brace-pair level is superfluous
7737 in the containing level. */
7738 if (constructor_type
== 0)
7741 /* If there were errors in this designator list already, bail out
7743 if (designator_erroneous
)
7746 if (!designator_depth
)
7748 gcc_assert (!constructor_range_stack
);
7750 /* Designator list starts at the level of closest explicit
7752 while (constructor_stack
->implicit
)
7753 process_init_element (input_location
,
7754 pop_init_level (loc
, 1, braced_init_obstack
),
7755 true, braced_init_obstack
);
7756 constructor_designated
= 1;
7760 switch (TREE_CODE (constructor_type
))
7764 subtype
= TREE_TYPE (constructor_fields
);
7765 if (subtype
!= error_mark_node
)
7766 subtype
= TYPE_MAIN_VARIANT (subtype
);
7769 subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
7775 subcode
= TREE_CODE (subtype
);
7776 if (array
&& subcode
!= ARRAY_TYPE
)
7778 error_init (loc
, "array index in non-array initializer");
7781 else if (!array
&& subcode
!= RECORD_TYPE
&& subcode
!= UNION_TYPE
)
7783 error_init (loc
, "field name not in record or union initializer");
7787 constructor_designated
= 1;
7788 push_init_level (loc
, 2, braced_init_obstack
);
7792 /* If there are range designators in designator list, push a new designator
7793 to constructor_range_stack. RANGE_END is end of such stack range or
7794 NULL_TREE if there is no range designator at this level. */
7797 push_range_stack (tree range_end
, struct obstack
* braced_init_obstack
)
7799 struct constructor_range_stack
*p
;
7801 p
= (struct constructor_range_stack
*)
7802 obstack_alloc (braced_init_obstack
,
7803 sizeof (struct constructor_range_stack
));
7804 p
->prev
= constructor_range_stack
;
7806 p
->fields
= constructor_fields
;
7807 p
->range_start
= constructor_index
;
7808 p
->index
= constructor_index
;
7809 p
->stack
= constructor_stack
;
7810 p
->range_end
= range_end
;
7811 if (constructor_range_stack
)
7812 constructor_range_stack
->next
= p
;
7813 constructor_range_stack
= p
;
7816 /* Within an array initializer, specify the next index to be initialized.
7817 FIRST is that index. If LAST is nonzero, then initialize a range
7818 of indices, running from FIRST through LAST. */
7821 set_init_index (location_t loc
, tree first
, tree last
,
7822 struct obstack
*braced_init_obstack
)
7824 if (set_designator (loc
, 1, braced_init_obstack
))
7827 designator_erroneous
= 1;
7829 if (!INTEGRAL_TYPE_P (TREE_TYPE (first
))
7830 || (last
&& !INTEGRAL_TYPE_P (TREE_TYPE (last
))))
7832 error_init (loc
, "array index in initializer not of integer type");
7836 if (TREE_CODE (first
) != INTEGER_CST
)
7838 first
= c_fully_fold (first
, false, NULL
);
7839 if (TREE_CODE (first
) == INTEGER_CST
)
7840 pedwarn_init (loc
, OPT_Wpedantic
,
7841 "array index in initializer is not "
7842 "an integer constant expression");
7845 if (last
&& TREE_CODE (last
) != INTEGER_CST
)
7847 last
= c_fully_fold (last
, false, NULL
);
7848 if (TREE_CODE (last
) == INTEGER_CST
)
7849 pedwarn_init (loc
, OPT_Wpedantic
,
7850 "array index in initializer is not "
7851 "an integer constant expression");
7854 if (TREE_CODE (first
) != INTEGER_CST
)
7855 error_init (loc
, "nonconstant array index in initializer");
7856 else if (last
!= 0 && TREE_CODE (last
) != INTEGER_CST
)
7857 error_init (loc
, "nonconstant array index in initializer");
7858 else if (TREE_CODE (constructor_type
) != ARRAY_TYPE
)
7859 error_init (loc
, "array index in non-array initializer");
7860 else if (tree_int_cst_sgn (first
) == -1)
7861 error_init (loc
, "array index in initializer exceeds array bounds");
7862 else if (constructor_max_index
7863 && tree_int_cst_lt (constructor_max_index
, first
))
7864 error_init (loc
, "array index in initializer exceeds array bounds");
7867 constant_expression_warning (first
);
7869 constant_expression_warning (last
);
7870 constructor_index
= convert (bitsizetype
, first
);
7871 if (tree_int_cst_lt (constructor_index
, first
))
7873 constructor_index
= copy_node (constructor_index
);
7874 TREE_OVERFLOW (constructor_index
) = 1;
7879 if (tree_int_cst_equal (first
, last
))
7881 else if (tree_int_cst_lt (last
, first
))
7883 error_init (loc
, "empty index range in initializer");
7888 last
= convert (bitsizetype
, last
);
7889 if (constructor_max_index
!= 0
7890 && tree_int_cst_lt (constructor_max_index
, last
))
7892 error_init (loc
, "array index range in initializer exceeds "
7900 designator_erroneous
= 0;
7901 if (constructor_range_stack
|| last
)
7902 push_range_stack (last
, braced_init_obstack
);
7906 /* Within a struct initializer, specify the next field to be initialized. */
7909 set_init_label (location_t loc
, tree fieldname
,
7910 struct obstack
*braced_init_obstack
)
7914 if (set_designator (loc
, 0, braced_init_obstack
))
7917 designator_erroneous
= 1;
7919 if (TREE_CODE (constructor_type
) != RECORD_TYPE
7920 && TREE_CODE (constructor_type
) != UNION_TYPE
)
7922 error_init (loc
, "field name not in record or union initializer");
7926 field
= lookup_field (constructor_type
, fieldname
);
7929 error ("unknown field %qE specified in initializer", fieldname
);
7933 constructor_fields
= TREE_VALUE (field
);
7935 designator_erroneous
= 0;
7936 if (constructor_range_stack
)
7937 push_range_stack (NULL_TREE
, braced_init_obstack
);
7938 field
= TREE_CHAIN (field
);
7941 if (set_designator (loc
, 0, braced_init_obstack
))
7945 while (field
!= NULL_TREE
);
7948 /* Add a new initializer to the tree of pending initializers. PURPOSE
7949 identifies the initializer, either array index or field in a structure.
7950 VALUE is the value of that index or field. If ORIGTYPE is not
7951 NULL_TREE, it is the original type of VALUE.
7953 IMPLICIT is true if value comes from pop_init_level (1),
7954 the new initializer has been merged with the existing one
7955 and thus no warnings should be emitted about overriding an
7956 existing initializer. */
7959 add_pending_init (location_t loc
, tree purpose
, tree value
, tree origtype
,
7960 bool implicit
, struct obstack
*braced_init_obstack
)
7962 struct init_node
*p
, **q
, *r
;
7964 q
= &constructor_pending_elts
;
7967 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
7972 if (tree_int_cst_lt (purpose
, p
->purpose
))
7974 else if (tree_int_cst_lt (p
->purpose
, purpose
))
7980 if (TREE_SIDE_EFFECTS (p
->value
))
7981 warning_init (loc
, 0,
7982 "initialized field with side-effects "
7984 else if (warn_override_init
)
7985 warning_init (loc
, OPT_Woverride_init
,
7986 "initialized field overwritten");
7989 p
->origtype
= origtype
;
7998 bitpos
= bit_position (purpose
);
8002 if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8004 else if (p
->purpose
!= purpose
)
8010 if (TREE_SIDE_EFFECTS (p
->value
))
8011 warning_init (loc
, 0,
8012 "initialized field with side-effects "
8014 else if (warn_override_init
)
8015 warning_init (loc
, OPT_Woverride_init
,
8016 "initialized field overwritten");
8019 p
->origtype
= origtype
;
8025 r
= (struct init_node
*) obstack_alloc (braced_init_obstack
,
8026 sizeof (struct init_node
));
8027 r
->purpose
= purpose
;
8029 r
->origtype
= origtype
;
8039 struct init_node
*s
;
8043 if (p
->balance
== 0)
8045 else if (p
->balance
< 0)
8052 p
->left
->parent
= p
;
8069 constructor_pending_elts
= r
;
8074 struct init_node
*t
= r
->right
;
8078 r
->right
->parent
= r
;
8083 p
->left
->parent
= p
;
8086 p
->balance
= t
->balance
< 0;
8087 r
->balance
= -(t
->balance
> 0);
8102 constructor_pending_elts
= t
;
8108 /* p->balance == +1; growth of left side balances the node. */
8113 else /* r == p->right */
8115 if (p
->balance
== 0)
8116 /* Growth propagation from right side. */
8118 else if (p
->balance
> 0)
8125 p
->right
->parent
= p
;
8142 constructor_pending_elts
= r
;
8144 else /* r->balance == -1 */
8147 struct init_node
*t
= r
->left
;
8151 r
->left
->parent
= r
;
8156 p
->right
->parent
= p
;
8159 r
->balance
= (t
->balance
< 0);
8160 p
->balance
= -(t
->balance
> 0);
8175 constructor_pending_elts
= t
;
8181 /* p->balance == -1; growth of right side balances the node. */
8192 /* Build AVL tree from a sorted chain. */
8195 set_nonincremental_init (struct obstack
* braced_init_obstack
)
8197 unsigned HOST_WIDE_INT ix
;
8200 if (TREE_CODE (constructor_type
) != RECORD_TYPE
8201 && TREE_CODE (constructor_type
) != ARRAY_TYPE
)
8204 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements
, ix
, index
, value
)
8205 add_pending_init (input_location
, index
, value
, NULL_TREE
, true,
8206 braced_init_obstack
);
8207 constructor_elements
= NULL
;
8208 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8210 constructor_unfilled_fields
= TYPE_FIELDS (constructor_type
);
8211 /* Skip any nameless bit fields at the beginning. */
8212 while (constructor_unfilled_fields
!= 0
8213 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8214 && DECL_NAME (constructor_unfilled_fields
) == 0)
8215 constructor_unfilled_fields
= TREE_CHAIN (constructor_unfilled_fields
);
8218 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8220 if (TYPE_DOMAIN (constructor_type
))
8221 constructor_unfilled_index
8222 = convert (bitsizetype
,
8223 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type
)));
8225 constructor_unfilled_index
= bitsize_zero_node
;
8227 constructor_incremental
= 0;
8230 /* Build AVL tree from a string constant. */
8233 set_nonincremental_init_from_string (tree str
,
8234 struct obstack
* braced_init_obstack
)
8236 tree value
, purpose
, type
;
8237 HOST_WIDE_INT val
[2];
8238 const char *p
, *end
;
8239 int byte
, wchar_bytes
, charwidth
, bitpos
;
8241 gcc_assert (TREE_CODE (constructor_type
) == ARRAY_TYPE
);
8243 wchar_bytes
= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str
))) / BITS_PER_UNIT
;
8244 charwidth
= TYPE_PRECISION (char_type_node
);
8245 type
= TREE_TYPE (constructor_type
);
8246 p
= TREE_STRING_POINTER (str
);
8247 end
= p
+ TREE_STRING_LENGTH (str
);
8249 for (purpose
= bitsize_zero_node
;
8251 && !(constructor_max_index
8252 && tree_int_cst_lt (constructor_max_index
, purpose
));
8253 purpose
= size_binop (PLUS_EXPR
, purpose
, bitsize_one_node
))
8255 if (wchar_bytes
== 1)
8257 val
[0] = (unsigned char) *p
++;
8264 for (byte
= 0; byte
< wchar_bytes
; byte
++)
8266 if (BYTES_BIG_ENDIAN
)
8267 bitpos
= (wchar_bytes
- byte
- 1) * charwidth
;
8269 bitpos
= byte
* charwidth
;
8270 val
[bitpos
% HOST_BITS_PER_WIDE_INT
]
8271 |= ((unsigned HOST_WIDE_INT
) ((unsigned char) *p
++))
8272 << (bitpos
% HOST_BITS_PER_WIDE_INT
);
8276 if (!TYPE_UNSIGNED (type
))
8278 bitpos
= ((wchar_bytes
- 1) * charwidth
) + HOST_BITS_PER_CHAR
;
8279 if (bitpos
< HOST_BITS_PER_WIDE_INT
)
8281 if (val
[0] & (((HOST_WIDE_INT
) 1) << (bitpos
- 1)))
8283 val
[0] |= ((HOST_WIDE_INT
) -1) << bitpos
;
8287 else if (bitpos
== HOST_BITS_PER_WIDE_INT
)
8292 else if (val
[1] & (((HOST_WIDE_INT
) 1)
8293 << (bitpos
- 1 - HOST_BITS_PER_WIDE_INT
)))
8294 val
[1] |= ((HOST_WIDE_INT
) -1)
8295 << (bitpos
- HOST_BITS_PER_WIDE_INT
);
8298 value
= wide_int_to_tree (type
,
8299 wide_int::from_array (val
, 2,
8300 HOST_BITS_PER_WIDE_INT
* 2));
8301 add_pending_init (input_location
, purpose
, value
, NULL_TREE
, true,
8302 braced_init_obstack
);
8305 constructor_incremental
= 0;
8308 /* Return value of FIELD in pending initializer or zero if the field was
8309 not initialized yet. */
8312 find_init_member (tree field
, struct obstack
* braced_init_obstack
)
8314 struct init_node
*p
;
8316 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8318 if (constructor_incremental
8319 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8320 set_nonincremental_init (braced_init_obstack
);
8322 p
= constructor_pending_elts
;
8325 if (tree_int_cst_lt (field
, p
->purpose
))
8327 else if (tree_int_cst_lt (p
->purpose
, field
))
8333 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8335 tree bitpos
= bit_position (field
);
8337 if (constructor_incremental
8338 && (!constructor_unfilled_fields
8339 || tree_int_cst_lt (bitpos
,
8340 bit_position (constructor_unfilled_fields
))))
8341 set_nonincremental_init (braced_init_obstack
);
8343 p
= constructor_pending_elts
;
8346 if (field
== p
->purpose
)
8348 else if (tree_int_cst_lt (bitpos
, bit_position (p
->purpose
)))
8354 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8356 if (!vec_safe_is_empty (constructor_elements
)
8357 && (constructor_elements
->last ().index
== field
))
8358 return constructor_elements
->last ().value
;
8363 /* "Output" the next constructor element.
8364 At top level, really output it to assembler code now.
8365 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
8366 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
8367 TYPE is the data type that the containing data type wants here.
8368 FIELD is the field (a FIELD_DECL) or the index that this element fills.
8369 If VALUE is a string constant, STRICT_STRING is true if it is
8370 unparenthesized or we should not warn here for it being parenthesized.
8371 For other types of VALUE, STRICT_STRING is not used.
8373 PENDING if non-nil means output pending elements that belong
8374 right after this element. (PENDING is normally 1;
8375 it is 0 while outputting pending elements, to avoid recursion.)
8377 IMPLICIT is true if value comes from pop_init_level (1),
8378 the new initializer has been merged with the existing one
8379 and thus no warnings should be emitted about overriding an
8380 existing initializer. */
8383 output_init_element (location_t loc
, tree value
, tree origtype
,
8384 bool strict_string
, tree type
, tree field
, int pending
,
8385 bool implicit
, struct obstack
* braced_init_obstack
)
8387 tree semantic_type
= NULL_TREE
;
8388 bool maybe_const
= true;
8391 if (type
== error_mark_node
|| value
== error_mark_node
)
8393 constructor_erroneous
= 1;
8396 if (TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
8397 && (TREE_CODE (value
) == STRING_CST
8398 || TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
)
8399 && !(TREE_CODE (value
) == STRING_CST
8400 && TREE_CODE (type
) == ARRAY_TYPE
8401 && INTEGRAL_TYPE_P (TREE_TYPE (type
)))
8402 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value
)),
8403 TYPE_MAIN_VARIANT (type
)))
8404 value
= array_to_pointer_conversion (input_location
, value
);
8406 if (TREE_CODE (value
) == COMPOUND_LITERAL_EXPR
8407 && require_constant_value
&& pending
)
8409 /* As an extension, allow initializing objects with static storage
8410 duration with compound literals (which are then treated just as
8411 the brace enclosed list they contain). */
8413 pedwarn_init (loc
, OPT_Wpedantic
, "initializer element is not "
8415 tree decl
= COMPOUND_LITERAL_EXPR_DECL (value
);
8416 value
= DECL_INITIAL (decl
);
8419 npc
= null_pointer_constant_p (value
);
8420 if (TREE_CODE (value
) == EXCESS_PRECISION_EXPR
)
8422 semantic_type
= TREE_TYPE (value
);
8423 value
= TREE_OPERAND (value
, 0);
8425 value
= c_fully_fold (value
, require_constant_value
, &maybe_const
);
8427 if (value
== error_mark_node
)
8428 constructor_erroneous
= 1;
8429 else if (!TREE_CONSTANT (value
))
8430 constructor_constant
= 0;
8431 else if (!initializer_constant_valid_p (value
, TREE_TYPE (value
))
8432 || ((TREE_CODE (constructor_type
) == RECORD_TYPE
8433 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8434 && DECL_C_BIT_FIELD (field
)
8435 && TREE_CODE (value
) != INTEGER_CST
))
8436 constructor_simple
= 0;
8438 constructor_nonconst
= 1;
8440 if (!initializer_constant_valid_p (value
, TREE_TYPE (value
)))
8442 if (require_constant_value
)
8444 error_init (loc
, "initializer element is not constant");
8445 value
= error_mark_node
;
8447 else if (require_constant_elements
)
8448 pedwarn (loc
, OPT_Wpedantic
,
8449 "initializer element is not computable at load time");
8451 else if (!maybe_const
8452 && (require_constant_value
|| require_constant_elements
))
8453 pedwarn_init (loc
, OPT_Wpedantic
,
8454 "initializer element is not a constant expression");
8456 /* Issue -Wc++-compat warnings about initializing a bitfield with
8459 && field
!= NULL_TREE
8460 && TREE_CODE (field
) == FIELD_DECL
8461 && DECL_BIT_FIELD_TYPE (field
) != NULL_TREE
8462 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))
8463 != TYPE_MAIN_VARIANT (type
))
8464 && TREE_CODE (DECL_BIT_FIELD_TYPE (field
)) == ENUMERAL_TYPE
)
8466 tree checktype
= origtype
!= NULL_TREE
? origtype
: TREE_TYPE (value
);
8467 if (checktype
!= error_mark_node
8468 && (TYPE_MAIN_VARIANT (checktype
)
8469 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field
))))
8470 warning_init (loc
, OPT_Wc___compat
,
8471 "enum conversion in initialization is invalid in C++");
8474 /* If this field is empty (and not at the end of structure),
8475 don't do anything other than checking the initializer. */
8477 && (TREE_TYPE (field
) == error_mark_node
8478 || (COMPLETE_TYPE_P (TREE_TYPE (field
))
8479 && integer_zerop (TYPE_SIZE (TREE_TYPE (field
)))
8480 && (TREE_CODE (constructor_type
) == ARRAY_TYPE
8481 || DECL_CHAIN (field
)))))
8485 value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, value
);
8486 value
= digest_init (loc
, type
, value
, origtype
, npc
, strict_string
,
8487 require_constant_value
);
8488 if (value
== error_mark_node
)
8490 constructor_erroneous
= 1;
8493 if (require_constant_value
|| require_constant_elements
)
8494 constant_expression_warning (value
);
8496 /* If this element doesn't come next in sequence,
8497 put it on constructor_pending_elts. */
8498 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
8499 && (!constructor_incremental
8500 || !tree_int_cst_equal (field
, constructor_unfilled_index
)))
8502 if (constructor_incremental
8503 && tree_int_cst_lt (field
, constructor_unfilled_index
))
8504 set_nonincremental_init (braced_init_obstack
);
8506 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8507 braced_init_obstack
);
8510 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8511 && (!constructor_incremental
8512 || field
!= constructor_unfilled_fields
))
8514 /* We do this for records but not for unions. In a union,
8515 no matter which field is specified, it can be initialized
8516 right away since it starts at the beginning of the union. */
8517 if (constructor_incremental
)
8519 if (!constructor_unfilled_fields
)
8520 set_nonincremental_init (braced_init_obstack
);
8523 tree bitpos
, unfillpos
;
8525 bitpos
= bit_position (field
);
8526 unfillpos
= bit_position (constructor_unfilled_fields
);
8528 if (tree_int_cst_lt (bitpos
, unfillpos
))
8529 set_nonincremental_init (braced_init_obstack
);
8533 add_pending_init (loc
, field
, value
, origtype
, implicit
,
8534 braced_init_obstack
);
8537 else if (TREE_CODE (constructor_type
) == UNION_TYPE
8538 && !vec_safe_is_empty (constructor_elements
))
8542 if (TREE_SIDE_EFFECTS (constructor_elements
->last ().value
))
8543 warning_init (loc
, 0,
8544 "initialized field with side-effects overwritten");
8545 else if (warn_override_init
)
8546 warning_init (loc
, OPT_Woverride_init
,
8547 "initialized field overwritten");
8550 /* We can have just one union field set. */
8551 constructor_elements
= NULL
;
8554 /* Otherwise, output this element either to
8555 constructor_elements or to the assembler file. */
8557 constructor_elt celt
= {field
, value
};
8558 vec_safe_push (constructor_elements
, celt
);
8560 /* Advance the variable that indicates sequential elements output. */
8561 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8562 constructor_unfilled_index
8563 = size_binop_loc (input_location
, PLUS_EXPR
, constructor_unfilled_index
,
8565 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8567 constructor_unfilled_fields
8568 = DECL_CHAIN (constructor_unfilled_fields
);
8570 /* Skip any nameless bit fields. */
8571 while (constructor_unfilled_fields
!= 0
8572 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8573 && DECL_NAME (constructor_unfilled_fields
) == 0)
8574 constructor_unfilled_fields
=
8575 DECL_CHAIN (constructor_unfilled_fields
);
8577 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8578 constructor_unfilled_fields
= 0;
8580 /* Now output any pending elements which have become next. */
8582 output_pending_init_elements (0, braced_init_obstack
);
8585 /* Output any pending elements which have become next.
8586 As we output elements, constructor_unfilled_{fields,index}
8587 advances, which may cause other elements to become next;
8588 if so, they too are output.
8590 If ALL is 0, we return when there are
8591 no more pending elements to output now.
8593 If ALL is 1, we output space as necessary so that
8594 we can output all the pending elements. */
8596 output_pending_init_elements (int all
, struct obstack
* braced_init_obstack
)
8598 struct init_node
*elt
= constructor_pending_elts
;
8603 /* Look through the whole pending tree.
8604 If we find an element that should be output now,
8605 output it. Otherwise, set NEXT to the element
8606 that comes first among those still pending. */
8611 if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8613 if (tree_int_cst_equal (elt
->purpose
,
8614 constructor_unfilled_index
))
8615 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8616 true, TREE_TYPE (constructor_type
),
8617 constructor_unfilled_index
, 0, false,
8618 braced_init_obstack
);
8619 else if (tree_int_cst_lt (constructor_unfilled_index
,
8622 /* Advance to the next smaller node. */
8627 /* We have reached the smallest node bigger than the
8628 current unfilled index. Fill the space first. */
8629 next
= elt
->purpose
;
8635 /* Advance to the next bigger node. */
8640 /* We have reached the biggest node in a subtree. Find
8641 the parent of it, which is the next bigger node. */
8642 while (elt
->parent
&& elt
->parent
->right
== elt
)
8645 if (elt
&& tree_int_cst_lt (constructor_unfilled_index
,
8648 next
= elt
->purpose
;
8654 else if (TREE_CODE (constructor_type
) == RECORD_TYPE
8655 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8657 tree ctor_unfilled_bitpos
, elt_bitpos
;
8659 /* If the current record is complete we are done. */
8660 if (constructor_unfilled_fields
== 0)
8663 ctor_unfilled_bitpos
= bit_position (constructor_unfilled_fields
);
8664 elt_bitpos
= bit_position (elt
->purpose
);
8665 /* We can't compare fields here because there might be empty
8666 fields in between. */
8667 if (tree_int_cst_equal (elt_bitpos
, ctor_unfilled_bitpos
))
8669 constructor_unfilled_fields
= elt
->purpose
;
8670 output_init_element (input_location
, elt
->value
, elt
->origtype
,
8671 true, TREE_TYPE (elt
->purpose
),
8672 elt
->purpose
, 0, false,
8673 braced_init_obstack
);
8675 else if (tree_int_cst_lt (ctor_unfilled_bitpos
, elt_bitpos
))
8677 /* Advance to the next smaller node. */
8682 /* We have reached the smallest node bigger than the
8683 current unfilled field. Fill the space first. */
8684 next
= elt
->purpose
;
8690 /* Advance to the next bigger node. */
8695 /* We have reached the biggest node in a subtree. Find
8696 the parent of it, which is the next bigger node. */
8697 while (elt
->parent
&& elt
->parent
->right
== elt
)
8701 && (tree_int_cst_lt (ctor_unfilled_bitpos
,
8702 bit_position (elt
->purpose
))))
8704 next
= elt
->purpose
;
8712 /* Ordinarily return, but not if we want to output all
8713 and there are elements left. */
8714 if (!(all
&& next
!= 0))
8717 /* If it's not incremental, just skip over the gap, so that after
8718 jumping to retry we will output the next successive element. */
8719 if (TREE_CODE (constructor_type
) == RECORD_TYPE
8720 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8721 constructor_unfilled_fields
= next
;
8722 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
8723 constructor_unfilled_index
= next
;
8725 /* ELT now points to the node in the pending tree with the next
8726 initializer to output. */
8730 /* Add one non-braced element to the current constructor level.
8731 This adjusts the current position within the constructor's type.
8732 This may also start or terminate implicit levels
8733 to handle a partly-braced initializer.
8735 Once this has found the correct level for the new element,
8736 it calls output_init_element.
8738 IMPLICIT is true if value comes from pop_init_level (1),
8739 the new initializer has been merged with the existing one
8740 and thus no warnings should be emitted about overriding an
8741 existing initializer. */
8744 process_init_element (location_t loc
, struct c_expr value
, bool implicit
,
8745 struct obstack
* braced_init_obstack
)
8747 tree orig_value
= value
.value
;
8748 int string_flag
= orig_value
!= 0 && TREE_CODE (orig_value
) == STRING_CST
;
8749 bool strict_string
= value
.original_code
== STRING_CST
;
8750 bool was_designated
= designator_depth
!= 0;
8752 designator_depth
= 0;
8753 designator_erroneous
= 0;
8755 if (!implicit
&& value
.value
&& !integer_zerop (value
.value
))
8756 constructor_zeroinit
= 0;
8758 /* Handle superfluous braces around string cst as in
8759 char x[] = {"foo"}; */
8763 && TREE_CODE (constructor_type
) == ARRAY_TYPE
8764 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type
))
8765 && integer_zerop (constructor_unfilled_index
))
8767 if (constructor_stack
->replacement_value
.value
)
8768 error_init (loc
, "excess elements in char array initializer");
8769 constructor_stack
->replacement_value
= value
;
8773 if (constructor_stack
->replacement_value
.value
!= 0)
8775 error_init (loc
, "excess elements in struct initializer");
8779 /* Ignore elements of a brace group if it is entirely superfluous
8780 and has already been diagnosed. */
8781 if (constructor_type
== 0)
8784 if (!implicit
&& warn_designated_init
&& !was_designated
8785 && TREE_CODE (constructor_type
) == RECORD_TYPE
8786 && lookup_attribute ("designated_init",
8787 TYPE_ATTRIBUTES (constructor_type
)))
8789 OPT_Wdesignated_init
,
8790 "positional initialization of field "
8791 "in %<struct%> declared with %<designated_init%> attribute");
8793 /* If we've exhausted any levels that didn't have braces,
8795 while (constructor_stack
->implicit
)
8797 if ((TREE_CODE (constructor_type
) == RECORD_TYPE
8798 || TREE_CODE (constructor_type
) == UNION_TYPE
)
8799 && constructor_fields
== 0)
8800 process_init_element (loc
,
8801 pop_init_level (loc
, 1, braced_init_obstack
),
8802 true, braced_init_obstack
);
8803 else if ((TREE_CODE (constructor_type
) == ARRAY_TYPE
8804 || TREE_CODE (constructor_type
) == VECTOR_TYPE
)
8805 && constructor_max_index
8806 && tree_int_cst_lt (constructor_max_index
,
8808 process_init_element (loc
,
8809 pop_init_level (loc
, 1, braced_init_obstack
),
8810 true, braced_init_obstack
);
8815 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8816 if (constructor_range_stack
)
8818 /* If value is a compound literal and we'll be just using its
8819 content, don't put it into a SAVE_EXPR. */
8820 if (TREE_CODE (value
.value
) != COMPOUND_LITERAL_EXPR
8821 || !require_constant_value
)
8823 tree semantic_type
= NULL_TREE
;
8824 if (TREE_CODE (value
.value
) == EXCESS_PRECISION_EXPR
)
8826 semantic_type
= TREE_TYPE (value
.value
);
8827 value
.value
= TREE_OPERAND (value
.value
, 0);
8829 value
.value
= c_save_expr (value
.value
);
8831 value
.value
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
,
8838 if (TREE_CODE (constructor_type
) == RECORD_TYPE
)
8841 enum tree_code fieldcode
;
8843 if (constructor_fields
== 0)
8845 pedwarn_init (loc
, 0, "excess elements in struct initializer");
8849 fieldtype
= TREE_TYPE (constructor_fields
);
8850 if (fieldtype
!= error_mark_node
)
8851 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8852 fieldcode
= TREE_CODE (fieldtype
);
8854 /* Error for non-static initialization of a flexible array member. */
8855 if (fieldcode
== ARRAY_TYPE
8856 && !require_constant_value
8857 && TYPE_SIZE (fieldtype
) == NULL_TREE
8858 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8860 error_init (loc
, "non-static initialization of a flexible "
8865 /* Error for initialization of a flexible array member with
8866 a string constant if the structure is in an array. E.g.:
8867 struct S { int x; char y[]; };
8868 struct S s[] = { { 1, "foo" } };
8871 && fieldcode
== ARRAY_TYPE
8872 && constructor_depth
> 1
8873 && TYPE_SIZE (fieldtype
) == NULL_TREE
8874 && DECL_CHAIN (constructor_fields
) == NULL_TREE
)
8876 bool in_array_p
= false;
8877 for (struct constructor_stack
*p
= constructor_stack
;
8878 p
&& p
->type
; p
= p
->next
)
8879 if (TREE_CODE (p
->type
) == ARRAY_TYPE
)
8886 error_init (loc
, "initialization of flexible array "
8887 "member in a nested context");
8892 /* Accept a string constant to initialize a subarray. */
8893 if (value
.value
!= 0
8894 && fieldcode
== ARRAY_TYPE
8895 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8897 value
.value
= orig_value
;
8898 /* Otherwise, if we have come to a subaggregate,
8899 and we don't have an element of its type, push into it. */
8900 else if (value
.value
!= 0
8901 && value
.value
!= error_mark_node
8902 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8903 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8904 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8906 push_init_level (loc
, 1, braced_init_obstack
);
8912 push_member_name (constructor_fields
);
8913 output_init_element (loc
, value
.value
, value
.original_type
,
8914 strict_string
, fieldtype
,
8915 constructor_fields
, 1, implicit
,
8916 braced_init_obstack
);
8917 RESTORE_SPELLING_DEPTH (constructor_depth
);
8920 /* Do the bookkeeping for an element that was
8921 directly output as a constructor. */
8923 /* For a record, keep track of end position of last field. */
8924 if (DECL_SIZE (constructor_fields
))
8925 constructor_bit_index
8926 = size_binop_loc (input_location
, PLUS_EXPR
,
8927 bit_position (constructor_fields
),
8928 DECL_SIZE (constructor_fields
));
8930 /* If the current field was the first one not yet written out,
8931 it isn't now, so update. */
8932 if (constructor_unfilled_fields
== constructor_fields
)
8934 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
8935 /* Skip any nameless bit fields. */
8936 while (constructor_unfilled_fields
!= 0
8937 && DECL_C_BIT_FIELD (constructor_unfilled_fields
)
8938 && DECL_NAME (constructor_unfilled_fields
) == 0)
8939 constructor_unfilled_fields
=
8940 DECL_CHAIN (constructor_unfilled_fields
);
8944 constructor_fields
= DECL_CHAIN (constructor_fields
);
8945 /* Skip any nameless bit fields at the beginning. */
8946 while (constructor_fields
!= 0
8947 && DECL_C_BIT_FIELD (constructor_fields
)
8948 && DECL_NAME (constructor_fields
) == 0)
8949 constructor_fields
= DECL_CHAIN (constructor_fields
);
8951 else if (TREE_CODE (constructor_type
) == UNION_TYPE
)
8954 enum tree_code fieldcode
;
8956 if (constructor_fields
== 0)
8958 pedwarn_init (loc
, 0,
8959 "excess elements in union initializer");
8963 fieldtype
= TREE_TYPE (constructor_fields
);
8964 if (fieldtype
!= error_mark_node
)
8965 fieldtype
= TYPE_MAIN_VARIANT (fieldtype
);
8966 fieldcode
= TREE_CODE (fieldtype
);
8968 /* Warn that traditional C rejects initialization of unions.
8969 We skip the warning if the value is zero. This is done
8970 under the assumption that the zero initializer in user
8971 code appears conditioned on e.g. __STDC__ to avoid
8972 "missing initializer" warnings and relies on default
8973 initialization to zero in the traditional C case.
8974 We also skip the warning if the initializer is designated,
8975 again on the assumption that this must be conditional on
8976 __STDC__ anyway (and we've already complained about the
8977 member-designator already). */
8978 if (!in_system_header_at (input_location
) && !constructor_designated
8979 && !(value
.value
&& (integer_zerop (value
.value
)
8980 || real_zerop (value
.value
))))
8981 warning (OPT_Wtraditional
, "traditional C rejects initialization "
8984 /* Accept a string constant to initialize a subarray. */
8985 if (value
.value
!= 0
8986 && fieldcode
== ARRAY_TYPE
8987 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype
))
8989 value
.value
= orig_value
;
8990 /* Otherwise, if we have come to a subaggregate,
8991 and we don't have an element of its type, push into it. */
8992 else if (value
.value
!= 0
8993 && value
.value
!= error_mark_node
8994 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != fieldtype
8995 && (fieldcode
== RECORD_TYPE
|| fieldcode
== ARRAY_TYPE
8996 || fieldcode
== UNION_TYPE
|| fieldcode
== VECTOR_TYPE
))
8998 push_init_level (loc
, 1, braced_init_obstack
);
9004 push_member_name (constructor_fields
);
9005 output_init_element (loc
, value
.value
, value
.original_type
,
9006 strict_string
, fieldtype
,
9007 constructor_fields
, 1, implicit
,
9008 braced_init_obstack
);
9009 RESTORE_SPELLING_DEPTH (constructor_depth
);
9012 /* Do the bookkeeping for an element that was
9013 directly output as a constructor. */
9015 constructor_bit_index
= DECL_SIZE (constructor_fields
);
9016 constructor_unfilled_fields
= DECL_CHAIN (constructor_fields
);
9019 constructor_fields
= 0;
9021 else if (TREE_CODE (constructor_type
) == ARRAY_TYPE
)
9023 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9024 enum tree_code eltcode
= TREE_CODE (elttype
);
9026 /* Accept a string constant to initialize a subarray. */
9027 if (value
.value
!= 0
9028 && eltcode
== ARRAY_TYPE
9029 && INTEGRAL_TYPE_P (TREE_TYPE (elttype
))
9031 value
.value
= orig_value
;
9032 /* Otherwise, if we have come to a subaggregate,
9033 and we don't have an element of its type, push into it. */
9034 else if (value
.value
!= 0
9035 && value
.value
!= error_mark_node
9036 && TYPE_MAIN_VARIANT (TREE_TYPE (value
.value
)) != elttype
9037 && (eltcode
== RECORD_TYPE
|| eltcode
== ARRAY_TYPE
9038 || eltcode
== UNION_TYPE
|| eltcode
== VECTOR_TYPE
))
9040 push_init_level (loc
, 1, braced_init_obstack
);
9044 if (constructor_max_index
!= 0
9045 && (tree_int_cst_lt (constructor_max_index
, constructor_index
)
9046 || integer_all_onesp (constructor_max_index
)))
9048 pedwarn_init (loc
, 0,
9049 "excess elements in array initializer");
9053 /* Now output the actual element. */
9056 push_array_bounds (tree_to_uhwi (constructor_index
));
9057 output_init_element (loc
, value
.value
, value
.original_type
,
9058 strict_string
, elttype
,
9059 constructor_index
, 1, implicit
,
9060 braced_init_obstack
);
9061 RESTORE_SPELLING_DEPTH (constructor_depth
);
9065 = size_binop_loc (input_location
, PLUS_EXPR
,
9066 constructor_index
, bitsize_one_node
);
9069 /* If we are doing the bookkeeping for an element that was
9070 directly output as a constructor, we must update
9071 constructor_unfilled_index. */
9072 constructor_unfilled_index
= constructor_index
;
9074 else if (TREE_CODE (constructor_type
) == VECTOR_TYPE
)
9076 tree elttype
= TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type
));
9078 /* Do a basic check of initializer size. Note that vectors
9079 always have a fixed size derived from their type. */
9080 if (tree_int_cst_lt (constructor_max_index
, constructor_index
))
9082 pedwarn_init (loc
, 0,
9083 "excess elements in vector initializer");
9087 /* Now output the actual element. */
9090 if (TREE_CODE (value
.value
) == VECTOR_CST
)
9091 elttype
= TYPE_MAIN_VARIANT (constructor_type
);
9092 output_init_element (loc
, value
.value
, value
.original_type
,
9093 strict_string
, elttype
,
9094 constructor_index
, 1, implicit
,
9095 braced_init_obstack
);
9099 = size_binop_loc (input_location
,
9100 PLUS_EXPR
, constructor_index
, bitsize_one_node
);
9103 /* If we are doing the bookkeeping for an element that was
9104 directly output as a constructor, we must update
9105 constructor_unfilled_index. */
9106 constructor_unfilled_index
= constructor_index
;
9109 /* Handle the sole element allowed in a braced initializer
9110 for a scalar variable. */
9111 else if (constructor_type
!= error_mark_node
9112 && constructor_fields
== 0)
9114 pedwarn_init (loc
, 0,
9115 "excess elements in scalar initializer");
9121 output_init_element (loc
, value
.value
, value
.original_type
,
9122 strict_string
, constructor_type
,
9123 NULL_TREE
, 1, implicit
,
9124 braced_init_obstack
);
9125 constructor_fields
= 0;
9128 /* Handle range initializers either at this level or anywhere higher
9129 in the designator stack. */
9130 if (constructor_range_stack
)
9132 struct constructor_range_stack
*p
, *range_stack
;
9135 range_stack
= constructor_range_stack
;
9136 constructor_range_stack
= 0;
9137 while (constructor_stack
!= range_stack
->stack
)
9139 gcc_assert (constructor_stack
->implicit
);
9140 process_init_element (loc
,
9141 pop_init_level (loc
, 1,
9142 braced_init_obstack
),
9143 true, braced_init_obstack
);
9145 for (p
= range_stack
;
9146 !p
->range_end
|| tree_int_cst_equal (p
->index
, p
->range_end
);
9149 gcc_assert (constructor_stack
->implicit
);
9150 process_init_element (loc
,
9151 pop_init_level (loc
, 1,
9152 braced_init_obstack
),
9153 true, braced_init_obstack
);
9156 p
->index
= size_binop_loc (input_location
,
9157 PLUS_EXPR
, p
->index
, bitsize_one_node
);
9158 if (tree_int_cst_equal (p
->index
, p
->range_end
) && !p
->prev
)
9163 constructor_index
= p
->index
;
9164 constructor_fields
= p
->fields
;
9165 if (finish
&& p
->range_end
&& p
->index
== p
->range_start
)
9173 push_init_level (loc
, 2, braced_init_obstack
);
9174 p
->stack
= constructor_stack
;
9175 if (p
->range_end
&& tree_int_cst_equal (p
->index
, p
->range_end
))
9176 p
->index
= p
->range_start
;
9180 constructor_range_stack
= range_stack
;
9187 constructor_range_stack
= 0;
9190 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
9191 (guaranteed to be 'volatile' or null) and ARGS (represented using
9192 an ASM_EXPR node). */
9194 build_asm_stmt (tree cv_qualifier
, tree args
)
9196 if (!ASM_VOLATILE_P (args
) && cv_qualifier
)
9197 ASM_VOLATILE_P (args
) = 1;
9198 return add_stmt (args
);
9201 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9202 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9203 SIMPLE indicates whether there was anything at all after the
9204 string in the asm expression -- asm("blah") and asm("blah" : )
9205 are subtly different. We use a ASM_EXPR node to represent this. */
9207 build_asm_expr (location_t loc
, tree string
, tree outputs
, tree inputs
,
9208 tree clobbers
, tree labels
, bool simple
)
9213 const char *constraint
;
9214 const char **oconstraints
;
9215 bool allows_mem
, allows_reg
, is_inout
;
9216 int ninputs
, noutputs
;
9218 ninputs
= list_length (inputs
);
9219 noutputs
= list_length (outputs
);
9220 oconstraints
= (const char **) alloca (noutputs
* sizeof (const char *));
9222 string
= resolve_asm_operand_names (string
, outputs
, inputs
, labels
);
9224 /* Remove output conversions that change the type but not the mode. */
9225 for (i
= 0, tail
= outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9227 tree output
= TREE_VALUE (tail
);
9229 output
= c_fully_fold (output
, false, NULL
);
9231 /* ??? Really, this should not be here. Users should be using a
9232 proper lvalue, dammit. But there's a long history of using casts
9233 in the output operands. In cases like longlong.h, this becomes a
9234 primitive form of typechecking -- if the cast can be removed, then
9235 the output operand had a type of the proper width; otherwise we'll
9236 get an error. Gross, but ... */
9237 STRIP_NOPS (output
);
9239 if (!lvalue_or_else (loc
, output
, lv_asm
))
9240 output
= error_mark_node
;
9242 if (output
!= error_mark_node
9243 && (TREE_READONLY (output
)
9244 || TYPE_READONLY (TREE_TYPE (output
))
9245 || ((TREE_CODE (TREE_TYPE (output
)) == RECORD_TYPE
9246 || TREE_CODE (TREE_TYPE (output
)) == UNION_TYPE
)
9247 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output
)))))
9248 readonly_error (loc
, output
, lv_asm
);
9250 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9251 oconstraints
[i
] = constraint
;
9253 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
9254 &allows_mem
, &allows_reg
, &is_inout
))
9256 /* If the operand is going to end up in memory,
9257 mark it addressable. */
9258 if (!allows_reg
&& !c_mark_addressable (output
))
9259 output
= error_mark_node
;
9260 if (!(!allows_reg
&& allows_mem
)
9261 && output
!= error_mark_node
9262 && VOID_TYPE_P (TREE_TYPE (output
)))
9264 error_at (loc
, "invalid use of void expression");
9265 output
= error_mark_node
;
9269 output
= error_mark_node
;
9271 TREE_VALUE (tail
) = output
;
9274 for (i
= 0, tail
= inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
9278 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
9279 input
= TREE_VALUE (tail
);
9281 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
, 0,
9282 oconstraints
, &allows_mem
, &allows_reg
))
9284 /* If the operand is going to end up in memory,
9285 mark it addressable. */
9286 if (!allows_reg
&& allows_mem
)
9288 input
= c_fully_fold (input
, false, NULL
);
9290 /* Strip the nops as we allow this case. FIXME, this really
9291 should be rejected or made deprecated. */
9293 if (!c_mark_addressable (input
))
9294 input
= error_mark_node
;
9299 memset (&expr
, 0, sizeof (expr
));
9301 expr
= convert_lvalue_to_rvalue (loc
, expr
, true, false);
9302 input
= c_fully_fold (expr
.value
, false, NULL
);
9304 if (input
!= error_mark_node
&& VOID_TYPE_P (TREE_TYPE (input
)))
9306 error_at (loc
, "invalid use of void expression");
9307 input
= error_mark_node
;
9312 input
= error_mark_node
;
9314 TREE_VALUE (tail
) = input
;
9317 /* ASMs with labels cannot have outputs. This should have been
9318 enforced by the parser. */
9319 gcc_assert (outputs
== NULL
|| labels
== NULL
);
9321 args
= build_stmt (loc
, ASM_EXPR
, string
, outputs
, inputs
, clobbers
, labels
);
9323 /* asm statements without outputs, including simple ones, are treated
9325 ASM_INPUT_P (args
) = simple
;
9326 ASM_VOLATILE_P (args
) = (noutputs
== 0);
9331 /* Generate a goto statement to LABEL. LOC is the location of the
9335 c_finish_goto_label (location_t loc
, tree label
)
9337 tree decl
= lookup_label_for_goto (loc
, label
);
9340 TREE_USED (decl
) = 1;
9342 tree t
= build1 (GOTO_EXPR
, void_type_node
, decl
);
9343 SET_EXPR_LOCATION (t
, loc
);
9344 return add_stmt (t
);
9348 /* Generate a computed goto statement to EXPR. LOC is the location of
9352 c_finish_goto_ptr (location_t loc
, tree expr
)
9355 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids %<goto *expr;%>");
9356 expr
= c_fully_fold (expr
, false, NULL
);
9357 expr
= convert (ptr_type_node
, expr
);
9358 t
= build1 (GOTO_EXPR
, void_type_node
, expr
);
9359 SET_EXPR_LOCATION (t
, loc
);
9360 return add_stmt (t
);
9363 /* Generate a C `return' statement. RETVAL is the expression for what
9364 to return, or a null pointer for `return;' with no value. LOC is
9365 the location of the return statement, or the location of the expression,
9366 if the statement has any. If ORIGTYPE is not NULL_TREE, it
9367 is the original type of RETVAL. */
9370 c_finish_return (location_t loc
, tree retval
, tree origtype
)
9372 tree valtype
= TREE_TYPE (TREE_TYPE (current_function_decl
)), ret_stmt
;
9373 bool no_warning
= false;
9377 if (TREE_THIS_VOLATILE (current_function_decl
))
9379 "function declared %<noreturn%> has a %<return%> statement");
9381 if (flag_cilkplus
&& contains_array_notation_expr (retval
))
9383 /* Array notations are allowed in a return statement if it is inside a
9384 built-in array notation reduction function. */
9385 if (!find_rank (loc
, retval
, retval
, false, &rank
))
9386 return error_mark_node
;
9389 error_at (loc
, "array notation expression cannot be used as a "
9391 return error_mark_node
;
9394 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
9396 error_at (loc
, "use of %<_Cilk_spawn%> in a return statement is not "
9398 return error_mark_node
;
9402 tree semantic_type
= NULL_TREE
;
9403 npc
= null_pointer_constant_p (retval
);
9404 if (TREE_CODE (retval
) == EXCESS_PRECISION_EXPR
)
9406 semantic_type
= TREE_TYPE (retval
);
9407 retval
= TREE_OPERAND (retval
, 0);
9409 retval
= c_fully_fold (retval
, false, NULL
);
9411 retval
= build1 (EXCESS_PRECISION_EXPR
, semantic_type
, retval
);
9416 current_function_returns_null
= 1;
9417 if ((warn_return_type
|| flag_isoc99
)
9418 && valtype
!= 0 && TREE_CODE (valtype
) != VOID_TYPE
)
9421 pedwarn (loc
, 0, "%<return%> with no value, in "
9422 "function returning non-void");
9424 warning_at (loc
, OPT_Wreturn_type
, "%<return%> with no value, "
9425 "in function returning non-void");
9429 else if (valtype
== 0 || TREE_CODE (valtype
) == VOID_TYPE
)
9431 current_function_returns_null
= 1;
9432 if (TREE_CODE (TREE_TYPE (retval
)) != VOID_TYPE
)
9434 "%<return%> with a value, in function returning void");
9436 pedwarn (loc
, OPT_Wpedantic
, "ISO C forbids "
9437 "%<return%> with expression, in function returning void");
9441 tree t
= convert_for_assignment (loc
, UNKNOWN_LOCATION
, valtype
,
9442 retval
, origtype
, ic_return
,
9443 npc
, NULL_TREE
, NULL_TREE
, 0);
9444 tree res
= DECL_RESULT (current_function_decl
);
9448 current_function_returns_value
= 1;
9449 if (t
== error_mark_node
)
9452 save
= in_late_binary_op
;
9453 if (TREE_CODE (TREE_TYPE (res
)) == BOOLEAN_TYPE
9454 || TREE_CODE (TREE_TYPE (res
)) == COMPLEX_TYPE
9455 || (TREE_CODE (TREE_TYPE (t
)) == REAL_TYPE
9456 && (TREE_CODE (TREE_TYPE (res
)) == INTEGER_TYPE
9457 || TREE_CODE (TREE_TYPE (res
)) == ENUMERAL_TYPE
)
9458 && (flag_sanitize
& SANITIZE_FLOAT_CAST
)))
9459 in_late_binary_op
= true;
9460 inner
= t
= convert (TREE_TYPE (res
), t
);
9461 in_late_binary_op
= save
;
9463 /* Strip any conversions, additions, and subtractions, and see if
9464 we are returning the address of a local variable. Warn if so. */
9467 switch (TREE_CODE (inner
))
9470 case NON_LVALUE_EXPR
:
9472 case POINTER_PLUS_EXPR
:
9473 inner
= TREE_OPERAND (inner
, 0);
9477 /* If the second operand of the MINUS_EXPR has a pointer
9478 type (or is converted from it), this may be valid, so
9479 don't give a warning. */
9481 tree op1
= TREE_OPERAND (inner
, 1);
9483 while (!POINTER_TYPE_P (TREE_TYPE (op1
))
9484 && (CONVERT_EXPR_P (op1
)
9485 || TREE_CODE (op1
) == NON_LVALUE_EXPR
))
9486 op1
= TREE_OPERAND (op1
, 0);
9488 if (POINTER_TYPE_P (TREE_TYPE (op1
)))
9491 inner
= TREE_OPERAND (inner
, 0);
9496 inner
= TREE_OPERAND (inner
, 0);
9498 while (REFERENCE_CLASS_P (inner
)
9499 && TREE_CODE (inner
) != INDIRECT_REF
)
9500 inner
= TREE_OPERAND (inner
, 0);
9503 && !DECL_EXTERNAL (inner
)
9504 && !TREE_STATIC (inner
)
9505 && DECL_CONTEXT (inner
) == current_function_decl
)
9507 if (TREE_CODE (inner
) == LABEL_DECL
)
9508 warning_at (loc
, OPT_Wreturn_local_addr
,
9509 "function returns address of label");
9512 warning_at (loc
, OPT_Wreturn_local_addr
,
9513 "function returns address of local variable");
9514 tree zero
= build_zero_cst (TREE_TYPE (res
));
9515 t
= build2 (COMPOUND_EXPR
, TREE_TYPE (res
), t
, zero
);
9527 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, t
);
9528 SET_EXPR_LOCATION (retval
, loc
);
9530 if (warn_sequence_point
)
9531 verify_sequence_points (retval
);
9534 ret_stmt
= build_stmt (loc
, RETURN_EXPR
, retval
);
9535 TREE_NO_WARNING (ret_stmt
) |= no_warning
;
9536 return add_stmt (ret_stmt
);
9540 /* The SWITCH_EXPR being built. */
9543 /* The original type of the testing expression, i.e. before the
9544 default conversion is applied. */
9547 /* A splay-tree mapping the low element of a case range to the high
9548 element, or NULL_TREE if there is no high element. Used to
9549 determine whether or not a new case label duplicates an old case
9550 label. We need a tree, rather than simply a hash table, because
9551 of the GNU case range extension. */
9554 /* The bindings at the point of the switch. This is used for
9555 warnings crossing decls when branching to a case label. */
9556 struct c_spot_bindings
*bindings
;
9558 /* The next node on the stack. */
9559 struct c_switch
*next
;
9562 /* A stack of the currently active switch statements. The innermost
9563 switch statement is on the top of the stack. There is no need to
9564 mark the stack for garbage collection because it is only active
9565 during the processing of the body of a function, and we never
9566 collect at that point. */
9568 struct c_switch
*c_switch_stack
;
9570 /* Start a C switch statement, testing expression EXP. Return the new
9571 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9572 SWITCH_COND_LOC is the location of the switch's condition.
9573 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
9576 c_start_case (location_t switch_loc
,
9577 location_t switch_cond_loc
,
9578 tree exp
, bool explicit_cast_p
)
9580 tree orig_type
= error_mark_node
;
9581 struct c_switch
*cs
;
9583 if (exp
!= error_mark_node
)
9585 orig_type
= TREE_TYPE (exp
);
9587 if (!INTEGRAL_TYPE_P (orig_type
))
9589 if (orig_type
!= error_mark_node
)
9591 error_at (switch_cond_loc
, "switch quantity not an integer");
9592 orig_type
= error_mark_node
;
9594 exp
= integer_zero_node
;
9598 tree type
= TYPE_MAIN_VARIANT (orig_type
);
9601 /* Warn if the condition has boolean value. */
9602 while (TREE_CODE (e
) == COMPOUND_EXPR
)
9603 e
= TREE_OPERAND (e
, 1);
9605 if ((TREE_CODE (type
) == BOOLEAN_TYPE
9606 || truth_value_p (TREE_CODE (e
)))
9607 /* Explicit cast to int suppresses this warning. */
9608 && !(TREE_CODE (type
) == INTEGER_TYPE
9609 && explicit_cast_p
))
9610 warning_at (switch_cond_loc
, OPT_Wswitch_bool
,
9611 "switch condition has boolean value");
9613 if (!in_system_header_at (input_location
)
9614 && (type
== long_integer_type_node
9615 || type
== long_unsigned_type_node
))
9616 warning_at (switch_cond_loc
,
9617 OPT_Wtraditional
, "%<long%> switch expression not "
9618 "converted to %<int%> in ISO C");
9620 exp
= c_fully_fold (exp
, false, NULL
);
9621 exp
= default_conversion (exp
);
9623 if (warn_sequence_point
)
9624 verify_sequence_points (exp
);
9628 /* Add this new SWITCH_EXPR to the stack. */
9629 cs
= XNEW (struct c_switch
);
9630 cs
->switch_expr
= build3 (SWITCH_EXPR
, orig_type
, exp
, NULL_TREE
, NULL_TREE
);
9631 SET_EXPR_LOCATION (cs
->switch_expr
, switch_loc
);
9632 cs
->orig_type
= orig_type
;
9633 cs
->cases
= splay_tree_new (case_compare
, NULL
, NULL
);
9634 cs
->bindings
= c_get_switch_bindings ();
9635 cs
->next
= c_switch_stack
;
9636 c_switch_stack
= cs
;
9638 return add_stmt (cs
->switch_expr
);
9641 /* Process a case label at location LOC. */
9644 do_case (location_t loc
, tree low_value
, tree high_value
)
9646 tree label
= NULL_TREE
;
9648 if (low_value
&& TREE_CODE (low_value
) != INTEGER_CST
)
9650 low_value
= c_fully_fold (low_value
, false, NULL
);
9651 if (TREE_CODE (low_value
) == INTEGER_CST
)
9652 pedwarn (loc
, OPT_Wpedantic
,
9653 "case label is not an integer constant expression");
9656 if (high_value
&& TREE_CODE (high_value
) != INTEGER_CST
)
9658 high_value
= c_fully_fold (high_value
, false, NULL
);
9659 if (TREE_CODE (high_value
) == INTEGER_CST
)
9660 pedwarn (input_location
, OPT_Wpedantic
,
9661 "case label is not an integer constant expression");
9664 if (c_switch_stack
== NULL
)
9667 error_at (loc
, "case label not within a switch statement");
9669 error_at (loc
, "%<default%> label not within a switch statement");
9673 if (c_check_switch_jump_warnings (c_switch_stack
->bindings
,
9674 EXPR_LOCATION (c_switch_stack
->switch_expr
),
9678 label
= c_add_case_label (loc
, c_switch_stack
->cases
,
9679 SWITCH_COND (c_switch_stack
->switch_expr
),
9680 c_switch_stack
->orig_type
,
9681 low_value
, high_value
);
9682 if (label
== error_mark_node
)
9687 /* Finish the switch statement. TYPE is the original type of the
9688 controlling expression of the switch, or NULL_TREE. */
9691 c_finish_case (tree body
, tree type
)
9693 struct c_switch
*cs
= c_switch_stack
;
9694 location_t switch_location
;
9696 SWITCH_BODY (cs
->switch_expr
) = body
;
9698 /* Emit warnings as needed. */
9699 switch_location
= EXPR_LOCATION (cs
->switch_expr
);
9700 c_do_switch_warnings (cs
->cases
, switch_location
,
9701 type
? type
: TREE_TYPE (cs
->switch_expr
),
9702 SWITCH_COND (cs
->switch_expr
));
9704 /* Pop the stack. */
9705 c_switch_stack
= cs
->next
;
9706 splay_tree_delete (cs
->cases
);
9707 c_release_switch_bindings (cs
->bindings
);
9711 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9712 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9713 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9714 statement, and was not surrounded with parenthesis. */
9717 c_finish_if_stmt (location_t if_locus
, tree cond
, tree then_block
,
9718 tree else_block
, bool nested_if
)
9722 /* If the condition has array notations, then the rank of the then_block and
9723 else_block must be either 0 or be equal to the rank of the condition. If
9724 the condition does not have array notations then break them up as it is
9725 broken up in a normal expression. */
9726 if (flag_cilkplus
&& contains_array_notation_expr (cond
))
9728 size_t then_rank
= 0, cond_rank
= 0, else_rank
= 0;
9729 if (!find_rank (if_locus
, cond
, cond
, true, &cond_rank
))
9732 && !find_rank (if_locus
, then_block
, then_block
, true, &then_rank
))
9735 && !find_rank (if_locus
, else_block
, else_block
, true, &else_rank
))
9737 if (cond_rank
!= then_rank
&& then_rank
!= 0)
9739 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9740 " and the then-block");
9743 else if (cond_rank
!= else_rank
&& else_rank
!= 0)
9745 error_at (if_locus
, "rank-mismatch between if-statement%'s condition"
9746 " and the else-block");
9750 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9751 if (warn_parentheses
&& nested_if
&& else_block
== NULL
)
9753 tree inner_if
= then_block
;
9755 /* We know from the grammar productions that there is an IF nested
9756 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9757 it might not be exactly THEN_BLOCK, but should be the last
9758 non-container statement within. */
9760 switch (TREE_CODE (inner_if
))
9765 inner_if
= BIND_EXPR_BODY (inner_if
);
9767 case STATEMENT_LIST
:
9768 inner_if
= expr_last (then_block
);
9770 case TRY_FINALLY_EXPR
:
9771 case TRY_CATCH_EXPR
:
9772 inner_if
= TREE_OPERAND (inner_if
, 0);
9779 if (COND_EXPR_ELSE (inner_if
))
9780 warning_at (if_locus
, OPT_Wparentheses
,
9781 "suggest explicit braces to avoid ambiguous %<else%>");
9784 stmt
= build3 (COND_EXPR
, void_type_node
, cond
, then_block
, else_block
);
9785 SET_EXPR_LOCATION (stmt
, if_locus
);
9789 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9790 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9791 is false for DO loops. INCR is the FOR increment expression. BODY is
9792 the statement controlled by the loop. BLAB is the break label. CLAB is
9793 the continue label. Everything is allowed to be NULL. */
9796 c_finish_loop (location_t start_locus
, tree cond
, tree incr
, tree body
,
9797 tree blab
, tree clab
, bool cond_is_first
)
9799 tree entry
= NULL
, exit
= NULL
, t
;
9801 /* In theory could forbid cilk spawn for loop increment expression,
9802 but it should work just fine. */
9804 /* If the condition is zero don't generate a loop construct. */
9805 if (cond
&& integer_zerop (cond
))
9809 t
= build_and_jump (&blab
);
9810 SET_EXPR_LOCATION (t
, start_locus
);
9816 tree top
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9818 /* If we have an exit condition, then we build an IF with gotos either
9819 out of the loop, or to the top of it. If there's no exit condition,
9820 then we just build a jump back to the top. */
9821 exit
= build_and_jump (&LABEL_EXPR_LABEL (top
));
9823 if (cond
&& !integer_nonzerop (cond
))
9825 /* Canonicalize the loop condition to the end. This means
9826 generating a branch to the loop condition. Reuse the
9827 continue label, if possible. */
9832 entry
= build1 (LABEL_EXPR
, void_type_node
, NULL_TREE
);
9833 t
= build_and_jump (&LABEL_EXPR_LABEL (entry
));
9836 t
= build1 (GOTO_EXPR
, void_type_node
, clab
);
9837 SET_EXPR_LOCATION (t
, start_locus
);
9841 t
= build_and_jump (&blab
);
9843 exit
= fold_build3_loc (start_locus
,
9844 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9846 exit
= fold_build3_loc (input_location
,
9847 COND_EXPR
, void_type_node
, cond
, exit
, t
);
9856 add_stmt (build1 (LABEL_EXPR
, void_type_node
, clab
));
9864 add_stmt (build1 (LABEL_EXPR
, void_type_node
, blab
));
9868 c_finish_bc_stmt (location_t loc
, tree
*label_p
, bool is_break
)
9871 tree label
= *label_p
;
9873 /* In switch statements break is sometimes stylistically used after
9874 a return statement. This can lead to spurious warnings about
9875 control reaching the end of a non-void function when it is
9876 inlined. Note that we are calling block_may_fallthru with
9877 language specific tree nodes; this works because
9878 block_may_fallthru returns true when given something it does not
9880 skip
= !block_may_fallthru (cur_stmt_list
);
9885 *label_p
= label
= create_artificial_label (loc
);
9887 else if (TREE_CODE (label
) == LABEL_DECL
)
9889 else switch (TREE_INT_CST_LOW (label
))
9893 error_at (loc
, "break statement not within loop or switch");
9895 error_at (loc
, "continue statement not within a loop");
9899 gcc_assert (is_break
);
9900 error_at (loc
, "break statement used with OpenMP for loop");
9905 error ("break statement within %<#pragma simd%> loop body");
9907 error ("continue statement within %<#pragma simd%> loop body");
9918 add_stmt (build_predict_expr (PRED_CONTINUE
, NOT_TAKEN
));
9920 return add_stmt (build1 (GOTO_EXPR
, void_type_node
, label
));
9923 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9926 emit_side_effect_warnings (location_t loc
, tree expr
)
9928 if (expr
== error_mark_node
)
9930 else if (!TREE_SIDE_EFFECTS (expr
))
9932 if (!VOID_TYPE_P (TREE_TYPE (expr
)) && !TREE_NO_WARNING (expr
))
9933 warning_at (loc
, OPT_Wunused_value
, "statement with no effect");
9935 else if (TREE_CODE (expr
) == COMPOUND_EXPR
)
9938 location_t cloc
= loc
;
9939 while (TREE_CODE (r
) == COMPOUND_EXPR
)
9941 if (EXPR_HAS_LOCATION (r
))
9942 cloc
= EXPR_LOCATION (r
);
9943 r
= TREE_OPERAND (r
, 1);
9945 if (!TREE_SIDE_EFFECTS (r
)
9946 && !VOID_TYPE_P (TREE_TYPE (r
))
9947 && !CONVERT_EXPR_P (r
)
9948 && !TREE_NO_WARNING (r
)
9949 && !TREE_NO_WARNING (expr
))
9950 warning_at (cloc
, OPT_Wunused_value
,
9951 "right-hand operand of comma expression has no effect");
9954 warn_if_unused_value (expr
, loc
);
9957 /* Process an expression as if it were a complete statement. Emit
9958 diagnostics, but do not call ADD_STMT. LOC is the location of the
9962 c_process_expr_stmt (location_t loc
, tree expr
)
9969 expr
= c_fully_fold (expr
, false, NULL
);
9971 if (warn_sequence_point
)
9972 verify_sequence_points (expr
);
9974 if (TREE_TYPE (expr
) != error_mark_node
9975 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr
))
9976 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
9977 error_at (loc
, "expression statement has incomplete type");
9979 /* If we're not processing a statement expression, warn about unused values.
9980 Warnings for statement expressions will be emitted later, once we figure
9981 out which is the result. */
9982 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
9983 && warn_unused_value
)
9984 emit_side_effect_warnings (loc
, expr
);
9987 while (TREE_CODE (exprv
) == COMPOUND_EXPR
)
9988 exprv
= TREE_OPERAND (exprv
, 1);
9989 while (CONVERT_EXPR_P (exprv
))
9990 exprv
= TREE_OPERAND (exprv
, 0);
9992 || handled_component_p (exprv
)
9993 || TREE_CODE (exprv
) == ADDR_EXPR
)
9994 mark_exp_read (exprv
);
9996 /* If the expression is not of a type to which we cannot assign a line
9997 number, wrap the thing in a no-op NOP_EXPR. */
9998 if (DECL_P (expr
) || CONSTANT_CLASS_P (expr
))
10000 expr
= build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
10001 SET_EXPR_LOCATION (expr
, loc
);
10007 /* Emit an expression as a statement. LOC is the location of the
10011 c_finish_expr_stmt (location_t loc
, tree expr
)
10014 return add_stmt (c_process_expr_stmt (loc
, expr
));
10019 /* Do the opposite and emit a statement as an expression. To begin,
10020 create a new binding level and return it. */
10023 c_begin_stmt_expr (void)
10027 /* We must force a BLOCK for this level so that, if it is not expanded
10028 later, there is a way to turn off the entire subtree of blocks that
10029 are contained in it. */
10030 keep_next_level ();
10031 ret
= c_begin_compound_stmt (true);
10033 c_bindings_start_stmt_expr (c_switch_stack
== NULL
10035 : c_switch_stack
->bindings
);
10037 /* Mark the current statement list as belonging to a statement list. */
10038 STATEMENT_LIST_STMT_EXPR (ret
) = 1;
10043 /* LOC is the location of the compound statement to which this body
10047 c_finish_stmt_expr (location_t loc
, tree body
)
10049 tree last
, type
, tmp
, val
;
10052 body
= c_end_compound_stmt (loc
, body
, true);
10054 c_bindings_end_stmt_expr (c_switch_stack
== NULL
10056 : c_switch_stack
->bindings
);
10058 /* Locate the last statement in BODY. See c_end_compound_stmt
10059 about always returning a BIND_EXPR. */
10060 last_p
= &BIND_EXPR_BODY (body
);
10061 last
= BIND_EXPR_BODY (body
);
10063 continue_searching
:
10064 if (TREE_CODE (last
) == STATEMENT_LIST
)
10066 tree_stmt_iterator i
;
10068 /* This can happen with degenerate cases like ({ }). No value. */
10069 if (!TREE_SIDE_EFFECTS (last
))
10072 /* If we're supposed to generate side effects warnings, process
10073 all of the statements except the last. */
10074 if (warn_unused_value
)
10076 for (i
= tsi_start (last
); !tsi_one_before_end_p (i
); tsi_next (&i
))
10079 tree t
= tsi_stmt (i
);
10081 tloc
= EXPR_HAS_LOCATION (t
) ? EXPR_LOCATION (t
) : loc
;
10082 emit_side_effect_warnings (tloc
, t
);
10086 i
= tsi_last (last
);
10087 last_p
= tsi_stmt_ptr (i
);
10091 /* If the end of the list is exception related, then the list was split
10092 by a call to push_cleanup. Continue searching. */
10093 if (TREE_CODE (last
) == TRY_FINALLY_EXPR
10094 || TREE_CODE (last
) == TRY_CATCH_EXPR
)
10096 last_p
= &TREE_OPERAND (last
, 0);
10098 goto continue_searching
;
10101 if (last
== error_mark_node
)
10104 /* In the case that the BIND_EXPR is not necessary, return the
10105 expression out from inside it. */
10106 if (last
== BIND_EXPR_BODY (body
)
10107 && BIND_EXPR_VARS (body
) == NULL
)
10109 /* Even if this looks constant, do not allow it in a constant
10111 last
= c_wrap_maybe_const (last
, true);
10112 /* Do not warn if the return value of a statement expression is
10114 TREE_NO_WARNING (last
) = 1;
10118 /* Extract the type of said expression. */
10119 type
= TREE_TYPE (last
);
10121 /* If we're not returning a value at all, then the BIND_EXPR that
10122 we already have is a fine expression to return. */
10123 if (!type
|| VOID_TYPE_P (type
))
10126 /* Now that we've located the expression containing the value, it seems
10127 silly to make voidify_wrapper_expr repeat the process. Create a
10128 temporary of the appropriate type and stick it in a TARGET_EXPR. */
10129 tmp
= create_tmp_var_raw (type
);
10131 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10132 tree_expr_nonnegative_p giving up immediately. */
10134 if (TREE_CODE (val
) == NOP_EXPR
10135 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0)))
10136 val
= TREE_OPERAND (val
, 0);
10138 *last_p
= build2 (MODIFY_EXPR
, void_type_node
, tmp
, val
);
10139 SET_EXPR_LOCATION (*last_p
, EXPR_LOCATION (last
));
10142 tree t
= build4 (TARGET_EXPR
, type
, tmp
, body
, NULL_TREE
, NULL_TREE
);
10143 SET_EXPR_LOCATION (t
, loc
);
10148 /* Begin and end compound statements. This is as simple as pushing
10149 and popping new statement lists from the tree. */
10152 c_begin_compound_stmt (bool do_scope
)
10154 tree stmt
= push_stmt_list ();
10160 /* End a compound statement. STMT is the statement. LOC is the
10161 location of the compound statement-- this is usually the location
10162 of the opening brace. */
10165 c_end_compound_stmt (location_t loc
, tree stmt
, bool do_scope
)
10171 if (c_dialect_objc ())
10172 objc_clear_super_receiver ();
10173 block
= pop_scope ();
10176 stmt
= pop_stmt_list (stmt
);
10177 stmt
= c_build_bind_expr (loc
, block
, stmt
);
10179 /* If this compound statement is nested immediately inside a statement
10180 expression, then force a BIND_EXPR to be created. Otherwise we'll
10181 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10182 STATEMENT_LISTs merge, and thus we can lose track of what statement
10183 was really last. */
10184 if (building_stmt_list_p ()
10185 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list
)
10186 && TREE_CODE (stmt
) != BIND_EXPR
)
10188 stmt
= build3 (BIND_EXPR
, void_type_node
, NULL
, stmt
, NULL
);
10189 TREE_SIDE_EFFECTS (stmt
) = 1;
10190 SET_EXPR_LOCATION (stmt
, loc
);
10196 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
10197 when the current scope is exited. EH_ONLY is true when this is not
10198 meant to apply to normal control flow transfer. */
10201 push_cleanup (tree decl
, tree cleanup
, bool eh_only
)
10203 enum tree_code code
;
10207 code
= eh_only
? TRY_CATCH_EXPR
: TRY_FINALLY_EXPR
;
10208 stmt
= build_stmt (DECL_SOURCE_LOCATION (decl
), code
, NULL
, cleanup
);
10210 stmt_expr
= STATEMENT_LIST_STMT_EXPR (cur_stmt_list
);
10211 list
= push_stmt_list ();
10212 TREE_OPERAND (stmt
, 0) = list
;
10213 STATEMENT_LIST_STMT_EXPR (list
) = stmt_expr
;
10216 /* Build a binary-operation expression without default conversions.
10217 CODE is the kind of expression to build.
10218 LOCATION is the operator's location.
10219 This function differs from `build' in several ways:
10220 the data type of the result is computed and recorded in it,
10221 warnings are generated if arg data types are invalid,
10222 special handling for addition and subtraction of pointers is known,
10223 and some optimization is done (operations on narrow ints
10224 are done in the narrower type when that gives the same result).
10225 Constant folding is also done before the result is returned.
10227 Note that the operands will never have enumeral types, or function
10228 or array types, because either they will have the default conversions
10229 performed or they have both just been converted to some other type in which
10230 the arithmetic is to be done. */
10233 build_binary_op (location_t location
, enum tree_code code
,
10234 tree orig_op0
, tree orig_op1
, int convert_p
)
10236 tree type0
, type1
, orig_type0
, orig_type1
;
10238 enum tree_code code0
, code1
;
10240 tree ret
= error_mark_node
;
10241 const char *invalid_op_diag
;
10242 bool op0_int_operands
, op1_int_operands
;
10243 bool int_const
, int_const_or_overflow
, int_operands
;
10245 /* Expression code to give to the expression when it is built.
10246 Normally this is CODE, which is what the caller asked for,
10247 but in some special cases we change it. */
10248 enum tree_code resultcode
= code
;
10250 /* Data type in which the computation is to be performed.
10251 In the simplest cases this is the common type of the arguments. */
10252 tree result_type
= NULL
;
10254 /* When the computation is in excess precision, the type of the
10255 final EXCESS_PRECISION_EXPR. */
10256 tree semantic_result_type
= NULL
;
10258 /* Nonzero means operands have already been type-converted
10259 in whatever way is necessary.
10260 Zero means they need to be converted to RESULT_TYPE. */
10263 /* Nonzero means create the expression with this type, rather than
10265 tree build_type
= 0;
10267 /* Nonzero means after finally constructing the expression
10268 convert it to this type. */
10269 tree final_type
= 0;
10271 /* Nonzero if this is an operation like MIN or MAX which can
10272 safely be computed in short if both args are promoted shorts.
10273 Also implies COMMON.
10274 -1 indicates a bitwise operation; this makes a difference
10275 in the exact conditions for when it is safe to do the operation
10276 in a narrower mode. */
10279 /* Nonzero if this is a comparison operation;
10280 if both args are promoted shorts, compare the original shorts.
10281 Also implies COMMON. */
10282 int short_compare
= 0;
10284 /* Nonzero if this is a right-shift operation, which can be computed on the
10285 original short and then promoted if the operand is a promoted short. */
10286 int short_shift
= 0;
10288 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10291 /* True means types are compatible as far as ObjC is concerned. */
10294 /* True means this is an arithmetic operation that may need excess
10296 bool may_need_excess_precision
;
10298 /* True means this is a boolean operation that converts both its
10299 operands to truth-values. */
10300 bool boolean_op
= false;
10302 /* Remember whether we're doing / or %. */
10303 bool doing_div_or_mod
= false;
10305 /* Remember whether we're doing << or >>. */
10306 bool doing_shift
= false;
10308 /* Tree holding instrumentation expression. */
10309 tree instrument_expr
= NULL
;
10311 if (location
== UNKNOWN_LOCATION
)
10312 location
= input_location
;
10317 op0_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op0
);
10318 if (op0_int_operands
)
10319 op0
= remove_c_maybe_const_expr (op0
);
10320 op1_int_operands
= EXPR_INT_CONST_OPERANDS (orig_op1
);
10321 if (op1_int_operands
)
10322 op1
= remove_c_maybe_const_expr (op1
);
10323 int_operands
= (op0_int_operands
&& op1_int_operands
);
10326 int_const_or_overflow
= (TREE_CODE (orig_op0
) == INTEGER_CST
10327 && TREE_CODE (orig_op1
) == INTEGER_CST
);
10328 int_const
= (int_const_or_overflow
10329 && !TREE_OVERFLOW (orig_op0
)
10330 && !TREE_OVERFLOW (orig_op1
));
10333 int_const
= int_const_or_overflow
= false;
10335 /* Do not apply default conversion in mixed vector/scalar expression. */
10337 && !((TREE_CODE (TREE_TYPE (op0
)) == VECTOR_TYPE
)
10338 != (TREE_CODE (TREE_TYPE (op1
)) == VECTOR_TYPE
)))
10340 op0
= default_conversion (op0
);
10341 op1
= default_conversion (op1
);
10344 /* When Cilk Plus is enabled and there are array notations inside op0, then
10345 we check to see if there are builtin array notation functions. If
10346 so, then we take on the type of the array notation inside it. */
10347 if (flag_cilkplus
&& contains_array_notation_expr (op0
))
10348 orig_type0
= type0
= find_correct_array_notation_type (op0
);
10350 orig_type0
= type0
= TREE_TYPE (op0
);
10352 if (flag_cilkplus
&& contains_array_notation_expr (op1
))
10353 orig_type1
= type1
= find_correct_array_notation_type (op1
);
10355 orig_type1
= type1
= TREE_TYPE (op1
);
10357 /* The expression codes of the data types of the arguments tell us
10358 whether the arguments are integers, floating, pointers, etc. */
10359 code0
= TREE_CODE (type0
);
10360 code1
= TREE_CODE (type1
);
10362 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10363 STRIP_TYPE_NOPS (op0
);
10364 STRIP_TYPE_NOPS (op1
);
10366 /* If an error was already reported for one of the arguments,
10367 avoid reporting another error. */
10369 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10370 return error_mark_node
;
10372 if ((invalid_op_diag
10373 = targetm
.invalid_binary_op (code
, type0
, type1
)))
10375 error_at (location
, invalid_op_diag
);
10376 return error_mark_node
;
10384 case TRUNC_DIV_EXPR
:
10385 case CEIL_DIV_EXPR
:
10386 case FLOOR_DIV_EXPR
:
10387 case ROUND_DIV_EXPR
:
10388 case EXACT_DIV_EXPR
:
10389 may_need_excess_precision
= true;
10392 may_need_excess_precision
= false;
10395 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
10397 op0
= TREE_OPERAND (op0
, 0);
10398 type0
= TREE_TYPE (op0
);
10400 else if (may_need_excess_precision
10401 && (eptype
= excess_precision_type (type0
)) != NULL_TREE
)
10404 op0
= convert (eptype
, op0
);
10406 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
10408 op1
= TREE_OPERAND (op1
, 0);
10409 type1
= TREE_TYPE (op1
);
10411 else if (may_need_excess_precision
10412 && (eptype
= excess_precision_type (type1
)) != NULL_TREE
)
10415 op1
= convert (eptype
, op1
);
10418 objc_ok
= objc_compare_types (type0
, type1
, -3, NULL_TREE
);
10420 /* In case when one of the operands of the binary operation is
10421 a vector and another is a scalar -- convert scalar to vector. */
10422 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
10424 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
10427 switch (convert_flag
)
10430 return error_mark_node
;
10433 bool maybe_const
= true;
10435 sc
= c_fully_fold (op0
, false, &maybe_const
);
10436 sc
= save_expr (sc
);
10437 sc
= convert (TREE_TYPE (type1
), sc
);
10438 op0
= build_vector_from_val (type1
, sc
);
10440 op0
= c_wrap_maybe_const (op0
, true);
10441 orig_type0
= type0
= TREE_TYPE (op0
);
10442 code0
= TREE_CODE (type0
);
10446 case stv_secondarg
:
10448 bool maybe_const
= true;
10450 sc
= c_fully_fold (op1
, false, &maybe_const
);
10451 sc
= save_expr (sc
);
10452 sc
= convert (TREE_TYPE (type0
), sc
);
10453 op1
= build_vector_from_val (type0
, sc
);
10455 op1
= c_wrap_maybe_const (op1
, true);
10456 orig_type1
= type1
= TREE_TYPE (op1
);
10457 code1
= TREE_CODE (type1
);
10469 /* Handle the pointer + int case. */
10470 if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10472 ret
= pointer_int_sum (location
, PLUS_EXPR
, op0
, op1
);
10473 goto return_build_binary_op
;
10475 else if (code1
== POINTER_TYPE
&& code0
== INTEGER_TYPE
)
10477 ret
= pointer_int_sum (location
, PLUS_EXPR
, op1
, op0
);
10478 goto return_build_binary_op
;
10485 /* Subtraction of two similar pointers.
10486 We must subtract them as integers, then divide by object size. */
10487 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
10488 && comp_target_types (location
, type0
, type1
))
10490 ret
= pointer_diff (location
, op0
, op1
);
10491 goto return_build_binary_op
;
10493 /* Handle pointer minus int. Just like pointer plus int. */
10494 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10496 ret
= pointer_int_sum (location
, MINUS_EXPR
, op0
, op1
);
10497 goto return_build_binary_op
;
10507 case TRUNC_DIV_EXPR
:
10508 case CEIL_DIV_EXPR
:
10509 case FLOOR_DIV_EXPR
:
10510 case ROUND_DIV_EXPR
:
10511 case EXACT_DIV_EXPR
:
10512 doing_div_or_mod
= true;
10513 warn_for_div_by_zero (location
, op1
);
10515 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10516 || code0
== FIXED_POINT_TYPE
10517 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10518 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10519 || code1
== FIXED_POINT_TYPE
10520 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
10522 enum tree_code tcode0
= code0
, tcode1
= code1
;
10524 if (code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
10525 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
10526 if (code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
)
10527 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
10529 if (!((tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
)
10530 || (tcode0
== FIXED_POINT_TYPE
&& tcode1
== FIXED_POINT_TYPE
)))
10531 resultcode
= RDIV_EXPR
;
10533 /* Although it would be tempting to shorten always here, that
10534 loses on some targets, since the modulo instruction is
10535 undefined if the quotient can't be represented in the
10536 computation mode. We shorten only if unsigned or if
10537 dividing by something we know != -1. */
10538 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10539 || (TREE_CODE (op1
) == INTEGER_CST
10540 && !integer_all_onesp (op1
)));
10548 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10550 /* Allow vector types which are not floating point types. */
10551 else if (code0
== VECTOR_TYPE
10552 && code1
== VECTOR_TYPE
10553 && !VECTOR_FLOAT_TYPE_P (type0
)
10554 && !VECTOR_FLOAT_TYPE_P (type1
))
10558 case TRUNC_MOD_EXPR
:
10559 case FLOOR_MOD_EXPR
:
10560 doing_div_or_mod
= true;
10561 warn_for_div_by_zero (location
, op1
);
10563 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10564 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10565 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
10567 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
10569 /* Although it would be tempting to shorten always here, that loses
10570 on some targets, since the modulo instruction is undefined if the
10571 quotient can't be represented in the computation mode. We shorten
10572 only if unsigned or if dividing by something we know != -1. */
10573 shorten
= (TYPE_UNSIGNED (TREE_TYPE (orig_op0
))
10574 || (TREE_CODE (op1
) == INTEGER_CST
10575 && !integer_all_onesp (op1
)));
10580 case TRUTH_ANDIF_EXPR
:
10581 case TRUTH_ORIF_EXPR
:
10582 case TRUTH_AND_EXPR
:
10583 case TRUTH_OR_EXPR
:
10584 case TRUTH_XOR_EXPR
:
10585 if ((code0
== INTEGER_TYPE
|| code0
== POINTER_TYPE
10586 || code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
10587 || code0
== FIXED_POINT_TYPE
)
10588 && (code1
== INTEGER_TYPE
|| code1
== POINTER_TYPE
10589 || code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
10590 || code1
== FIXED_POINT_TYPE
))
10592 /* Result of these operations is always an int,
10593 but that does not mean the operands should be
10594 converted to ints! */
10595 result_type
= integer_type_node
;
10596 if (op0_int_operands
)
10598 op0
= c_objc_common_truthvalue_conversion (location
, orig_op0
);
10599 op0
= remove_c_maybe_const_expr (op0
);
10602 op0
= c_objc_common_truthvalue_conversion (location
, op0
);
10603 if (op1_int_operands
)
10605 op1
= c_objc_common_truthvalue_conversion (location
, orig_op1
);
10606 op1
= remove_c_maybe_const_expr (op1
);
10609 op1
= c_objc_common_truthvalue_conversion (location
, op1
);
10613 if (code
== TRUTH_ANDIF_EXPR
)
10615 int_const_or_overflow
= (int_operands
10616 && TREE_CODE (orig_op0
) == INTEGER_CST
10617 && (op0
== truthvalue_false_node
10618 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10619 int_const
= (int_const_or_overflow
10620 && !TREE_OVERFLOW (orig_op0
)
10621 && (op0
== truthvalue_false_node
10622 || !TREE_OVERFLOW (orig_op1
)));
10624 else if (code
== TRUTH_ORIF_EXPR
)
10626 int_const_or_overflow
= (int_operands
10627 && TREE_CODE (orig_op0
) == INTEGER_CST
10628 && (op0
== truthvalue_true_node
10629 || TREE_CODE (orig_op1
) == INTEGER_CST
));
10630 int_const
= (int_const_or_overflow
10631 && !TREE_OVERFLOW (orig_op0
)
10632 && (op0
== truthvalue_true_node
10633 || !TREE_OVERFLOW (orig_op1
)));
10637 /* Shift operations: result has same type as first operand;
10638 always convert second operand to int.
10639 Also set SHORT_SHIFT if shifting rightward. */
10642 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10643 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10645 result_type
= type0
;
10648 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10649 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10650 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10651 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10653 result_type
= type0
;
10656 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10657 && code1
== INTEGER_TYPE
)
10659 doing_shift
= true;
10660 if (TREE_CODE (op1
) == INTEGER_CST
)
10662 if (tree_int_cst_sgn (op1
) < 0)
10665 if (c_inhibit_evaluation_warnings
== 0)
10666 warning_at (location
, OPT_Wshift_count_negative
,
10667 "right shift count is negative");
10671 if (!integer_zerop (op1
))
10674 if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10677 if (c_inhibit_evaluation_warnings
== 0)
10678 warning_at (location
, OPT_Wshift_count_overflow
,
10679 "right shift count >= width of type");
10684 /* Use the type of the value to be shifted. */
10685 result_type
= type0
;
10686 /* Avoid converting op1 to result_type later. */
10692 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
10693 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
10695 result_type
= type0
;
10698 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10699 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
10700 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
10701 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
10703 result_type
= type0
;
10706 else if ((code0
== INTEGER_TYPE
|| code0
== FIXED_POINT_TYPE
)
10707 && code1
== INTEGER_TYPE
)
10709 doing_shift
= true;
10710 if (TREE_CODE (op1
) == INTEGER_CST
)
10712 if (tree_int_cst_sgn (op1
) < 0)
10715 if (c_inhibit_evaluation_warnings
== 0)
10716 warning_at (location
, OPT_Wshift_count_negative
,
10717 "left shift count is negative");
10720 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
10723 if (c_inhibit_evaluation_warnings
== 0)
10724 warning_at (location
, OPT_Wshift_count_overflow
,
10725 "left shift count >= width of type");
10729 /* Use the type of the value to be shifted. */
10730 result_type
= type0
;
10731 /* Avoid converting op1 to result_type later. */
10738 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10741 if (!vector_types_compatible_elements_p (type0
, type1
))
10743 error_at (location
, "comparing vectors with different "
10745 return error_mark_node
;
10748 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10750 error_at (location
, "comparing vectors with different "
10751 "number of elements");
10752 return error_mark_node
;
10755 /* Always construct signed integer vector type. */
10756 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10757 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10758 result_type
= build_opaque_vector_type (intt
,
10759 TYPE_VECTOR_SUBPARTS (type0
));
10763 if (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
))
10764 warning_at (location
,
10766 "comparing floating point with == or != is unsafe");
10767 /* Result of comparison is always int,
10768 but don't convert the args to int! */
10769 build_type
= integer_type_node
;
10770 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10771 || code0
== FIXED_POINT_TYPE
|| code0
== COMPLEX_TYPE
)
10772 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10773 || code1
== FIXED_POINT_TYPE
|| code1
== COMPLEX_TYPE
))
10775 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10777 if (TREE_CODE (op0
) == ADDR_EXPR
10778 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
10780 if (code
== EQ_EXPR
)
10781 warning_at (location
,
10783 "the comparison will always evaluate as %<false%> "
10784 "for the address of %qD will never be NULL",
10785 TREE_OPERAND (op0
, 0));
10787 warning_at (location
,
10789 "the comparison will always evaluate as %<true%> "
10790 "for the address of %qD will never be NULL",
10791 TREE_OPERAND (op0
, 0));
10793 result_type
= type0
;
10795 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10797 if (TREE_CODE (op1
) == ADDR_EXPR
10798 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
10800 if (code
== EQ_EXPR
)
10801 warning_at (location
,
10803 "the comparison will always evaluate as %<false%> "
10804 "for the address of %qD will never be NULL",
10805 TREE_OPERAND (op1
, 0));
10807 warning_at (location
,
10809 "the comparison will always evaluate as %<true%> "
10810 "for the address of %qD will never be NULL",
10811 TREE_OPERAND (op1
, 0));
10813 result_type
= type1
;
10815 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10817 tree tt0
= TREE_TYPE (type0
);
10818 tree tt1
= TREE_TYPE (type1
);
10819 addr_space_t as0
= TYPE_ADDR_SPACE (tt0
);
10820 addr_space_t as1
= TYPE_ADDR_SPACE (tt1
);
10821 addr_space_t as_common
= ADDR_SPACE_GENERIC
;
10823 /* Anything compares with void *. void * compares with anything.
10824 Otherwise, the targets must be compatible
10825 and both must be object or both incomplete. */
10826 if (comp_target_types (location
, type0
, type1
))
10827 result_type
= common_pointer_type (type0
, type1
);
10828 else if (!addr_space_superset (as0
, as1
, &as_common
))
10830 error_at (location
, "comparison of pointers to "
10831 "disjoint address spaces");
10832 return error_mark_node
;
10834 else if (VOID_TYPE_P (tt0
) && !TYPE_ATOMIC (tt0
))
10836 if (pedantic
&& TREE_CODE (tt1
) == FUNCTION_TYPE
)
10837 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10838 "comparison of %<void *%> with function pointer");
10840 else if (VOID_TYPE_P (tt1
) && !TYPE_ATOMIC (tt1
))
10842 if (pedantic
&& TREE_CODE (tt0
) == FUNCTION_TYPE
)
10843 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10844 "comparison of %<void *%> with function pointer");
10847 /* Avoid warning about the volatile ObjC EH puts on decls. */
10849 pedwarn (location
, 0,
10850 "comparison of distinct pointer types lacks a cast");
10852 if (result_type
== NULL_TREE
)
10854 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10855 result_type
= build_pointer_type
10856 (build_qualified_type (void_type_node
, qual
));
10859 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10861 result_type
= type0
;
10862 pedwarn (location
, 0, "comparison between pointer and integer");
10864 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10866 result_type
= type1
;
10867 pedwarn (location
, 0, "comparison between pointer and integer");
10869 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
10870 || truth_value_p (TREE_CODE (orig_op0
)))
10871 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
10872 || truth_value_p (TREE_CODE (orig_op1
))))
10873 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
10880 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
10883 if (!vector_types_compatible_elements_p (type0
, type1
))
10885 error_at (location
, "comparing vectors with different "
10887 return error_mark_node
;
10890 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
10892 error_at (location
, "comparing vectors with different "
10893 "number of elements");
10894 return error_mark_node
;
10897 /* Always construct signed integer vector type. */
10898 intt
= c_common_type_for_size (GET_MODE_BITSIZE
10899 (TYPE_MODE (TREE_TYPE (type0
))), 0);
10900 result_type
= build_opaque_vector_type (intt
,
10901 TYPE_VECTOR_SUBPARTS (type0
));
10905 build_type
= integer_type_node
;
10906 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
10907 || code0
== FIXED_POINT_TYPE
)
10908 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
10909 || code1
== FIXED_POINT_TYPE
))
10911 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
10913 addr_space_t as0
= TYPE_ADDR_SPACE (TREE_TYPE (type0
));
10914 addr_space_t as1
= TYPE_ADDR_SPACE (TREE_TYPE (type1
));
10915 addr_space_t as_common
;
10917 if (comp_target_types (location
, type0
, type1
))
10919 result_type
= common_pointer_type (type0
, type1
);
10920 if (!COMPLETE_TYPE_P (TREE_TYPE (type0
))
10921 != !COMPLETE_TYPE_P (TREE_TYPE (type1
)))
10922 pedwarn (location
, 0,
10923 "comparison of complete and incomplete pointers");
10924 else if (TREE_CODE (TREE_TYPE (type0
)) == FUNCTION_TYPE
)
10925 pedwarn (location
, OPT_Wpedantic
, "ISO C forbids "
10926 "ordered comparisons of pointers to functions");
10927 else if (null_pointer_constant_p (orig_op0
)
10928 || null_pointer_constant_p (orig_op1
))
10929 warning_at (location
, OPT_Wextra
,
10930 "ordered comparison of pointer with null pointer");
10933 else if (!addr_space_superset (as0
, as1
, &as_common
))
10935 error_at (location
, "comparison of pointers to "
10936 "disjoint address spaces");
10937 return error_mark_node
;
10941 int qual
= ENCODE_QUAL_ADDR_SPACE (as_common
);
10942 result_type
= build_pointer_type
10943 (build_qualified_type (void_type_node
, qual
));
10944 pedwarn (location
, 0,
10945 "comparison of distinct pointer types lacks a cast");
10948 else if (code0
== POINTER_TYPE
&& null_pointer_constant_p (orig_op1
))
10950 result_type
= type0
;
10952 pedwarn (location
, OPT_Wpedantic
,
10953 "ordered comparison of pointer with integer zero");
10954 else if (extra_warnings
)
10955 warning_at (location
, OPT_Wextra
,
10956 "ordered comparison of pointer with integer zero");
10958 else if (code1
== POINTER_TYPE
&& null_pointer_constant_p (orig_op0
))
10960 result_type
= type1
;
10962 pedwarn (location
, OPT_Wpedantic
,
10963 "ordered comparison of pointer with integer zero");
10964 else if (extra_warnings
)
10965 warning_at (location
, OPT_Wextra
,
10966 "ordered comparison of pointer with integer zero");
10968 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
10970 result_type
= type0
;
10971 pedwarn (location
, 0, "comparison between pointer and integer");
10973 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
10975 result_type
= type1
;
10976 pedwarn (location
, 0, "comparison between pointer and integer");
10978 if ((TREE_CODE (TREE_TYPE (orig_op0
)) == BOOLEAN_TYPE
10979 || truth_value_p (TREE_CODE (orig_op0
)))
10980 ^ (TREE_CODE (TREE_TYPE (orig_op1
)) == BOOLEAN_TYPE
10981 || truth_value_p (TREE_CODE (orig_op1
))))
10982 maybe_warn_bool_compare (location
, code
, orig_op0
, orig_op1
);
10986 gcc_unreachable ();
10989 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
10990 return error_mark_node
;
10992 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
10993 && (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
10994 || !vector_types_compatible_elements_p (type0
, type1
)))
10996 binary_op_error (location
, code
, type0
, type1
);
10997 return error_mark_node
;
11000 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
11001 || code0
== FIXED_POINT_TYPE
|| code0
== VECTOR_TYPE
)
11003 (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
11004 || code1
== FIXED_POINT_TYPE
|| code1
== VECTOR_TYPE
))
11006 bool first_complex
= (code0
== COMPLEX_TYPE
);
11007 bool second_complex
= (code1
== COMPLEX_TYPE
);
11008 int none_complex
= (!first_complex
&& !second_complex
);
11010 if (shorten
|| common
|| short_compare
)
11012 result_type
= c_common_type (type0
, type1
);
11013 do_warn_double_promotion (result_type
, type0
, type1
,
11014 "implicit conversion from %qT to %qT "
11015 "to match other operand of binary "
11018 if (result_type
== error_mark_node
)
11019 return error_mark_node
;
11022 if (first_complex
!= second_complex
11023 && (code
== PLUS_EXPR
11024 || code
== MINUS_EXPR
11025 || code
== MULT_EXPR
11026 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
11027 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
11028 && flag_signed_zeros
)
11030 /* An operation on mixed real/complex operands must be
11031 handled specially, but the language-independent code can
11032 more easily optimize the plain complex arithmetic if
11033 -fno-signed-zeros. */
11034 tree real_type
= TREE_TYPE (result_type
);
11036 if (type0
!= orig_type0
|| type1
!= orig_type1
)
11038 gcc_assert (may_need_excess_precision
&& common
);
11039 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11043 if (TREE_TYPE (op0
) != result_type
)
11044 op0
= convert_and_check (location
, result_type
, op0
);
11045 if (TREE_TYPE (op1
) != real_type
)
11046 op1
= convert_and_check (location
, real_type
, op1
);
11050 if (TREE_TYPE (op0
) != real_type
)
11051 op0
= convert_and_check (location
, real_type
, op0
);
11052 if (TREE_TYPE (op1
) != result_type
)
11053 op1
= convert_and_check (location
, result_type
, op1
);
11055 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11056 return error_mark_node
;
11059 op0
= c_save_expr (op0
);
11060 real
= build_unary_op (EXPR_LOCATION (orig_op0
), REALPART_EXPR
,
11062 imag
= build_unary_op (EXPR_LOCATION (orig_op0
), IMAGPART_EXPR
,
11067 case TRUNC_DIV_EXPR
:
11068 op1
= c_save_expr (op1
);
11069 imag
= build2 (resultcode
, real_type
, imag
, op1
);
11070 /* Fall through. */
11073 real
= build2 (resultcode
, real_type
, real
, op1
);
11081 op1
= c_save_expr (op1
);
11082 real
= build_unary_op (EXPR_LOCATION (orig_op1
), REALPART_EXPR
,
11084 imag
= build_unary_op (EXPR_LOCATION (orig_op1
), IMAGPART_EXPR
,
11089 op0
= c_save_expr (op0
);
11090 imag
= build2 (resultcode
, real_type
, op0
, imag
);
11091 /* Fall through. */
11093 real
= build2 (resultcode
, real_type
, op0
, real
);
11096 real
= build2 (resultcode
, real_type
, op0
, real
);
11097 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
11103 ret
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
11104 goto return_build_binary_op
;
11107 /* For certain operations (which identify themselves by shorten != 0)
11108 if both args were extended from the same smaller type,
11109 do the arithmetic in that type and then extend.
11111 shorten !=0 and !=1 indicates a bitwise operation.
11112 For them, this optimization is safe only if
11113 both args are zero-extended or both are sign-extended.
11114 Otherwise, we might change the result.
11115 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11116 but calculated in (unsigned short) it would be (unsigned short)-1. */
11118 if (shorten
&& none_complex
)
11120 final_type
= result_type
;
11121 result_type
= shorten_binary_op (result_type
, op0
, op1
,
11125 /* Shifts can be shortened if shifting right. */
11130 tree arg0
= get_narrower (op0
, &unsigned_arg
);
11132 final_type
= result_type
;
11134 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
11135 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
11137 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
11138 && tree_int_cst_sgn (op1
) > 0
11139 /* We can shorten only if the shift count is less than the
11140 number of bits in the smaller type size. */
11141 && compare_tree_int (op1
, TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
11142 /* We cannot drop an unsigned shift after sign-extension. */
11143 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
11145 /* Do an unsigned shift if the operand was zero-extended. */
11147 = c_common_signed_or_unsigned_type (unsigned_arg
,
11149 /* Convert value-to-be-shifted to that type. */
11150 if (TREE_TYPE (op0
) != result_type
)
11151 op0
= convert (result_type
, op0
);
11156 /* Comparison operations are shortened too but differently.
11157 They identify themselves by setting short_compare = 1. */
11161 /* Don't write &op0, etc., because that would prevent op0
11162 from being kept in a register.
11163 Instead, make copies of the our local variables and
11164 pass the copies by reference, then copy them back afterward. */
11165 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
11166 enum tree_code xresultcode
= resultcode
;
11168 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
11174 goto return_build_binary_op
;
11177 op0
= xop0
, op1
= xop1
;
11179 resultcode
= xresultcode
;
11181 if (c_inhibit_evaluation_warnings
== 0)
11183 bool op0_maybe_const
= true;
11184 bool op1_maybe_const
= true;
11185 tree orig_op0_folded
, orig_op1_folded
;
11187 if (in_late_binary_op
)
11189 orig_op0_folded
= orig_op0
;
11190 orig_op1_folded
= orig_op1
;
11194 /* Fold for the sake of possible warnings, as in
11195 build_conditional_expr. This requires the
11196 "original" values to be folded, not just op0 and
11198 c_inhibit_evaluation_warnings
++;
11199 op0
= c_fully_fold (op0
, require_constant_value
,
11201 op1
= c_fully_fold (op1
, require_constant_value
,
11203 c_inhibit_evaluation_warnings
--;
11204 orig_op0_folded
= c_fully_fold (orig_op0
,
11205 require_constant_value
,
11207 orig_op1_folded
= c_fully_fold (orig_op1
,
11208 require_constant_value
,
11212 if (warn_sign_compare
)
11213 warn_for_sign_compare (location
, orig_op0_folded
,
11214 orig_op1_folded
, op0
, op1
,
11215 result_type
, resultcode
);
11216 if (!in_late_binary_op
&& !int_operands
)
11218 if (!op0_maybe_const
|| TREE_CODE (op0
) != INTEGER_CST
)
11219 op0
= c_wrap_maybe_const (op0
, !op0_maybe_const
);
11220 if (!op1_maybe_const
|| TREE_CODE (op1
) != INTEGER_CST
)
11221 op1
= c_wrap_maybe_const (op1
, !op1_maybe_const
);
11227 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11228 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11229 Then the expression will be built.
11230 It will be given type FINAL_TYPE if that is nonzero;
11231 otherwise, it will be given type RESULT_TYPE. */
11235 binary_op_error (location
, code
, TREE_TYPE (op0
), TREE_TYPE (op1
));
11236 return error_mark_node
;
11239 if (build_type
== NULL_TREE
)
11241 build_type
= result_type
;
11242 if ((type0
!= orig_type0
|| type1
!= orig_type1
)
11245 gcc_assert (may_need_excess_precision
&& common
);
11246 semantic_result_type
= c_common_type (orig_type0
, orig_type1
);
11252 op0
= ep_convert_and_check (location
, result_type
, op0
,
11253 semantic_result_type
);
11254 op1
= ep_convert_and_check (location
, result_type
, op1
,
11255 semantic_result_type
);
11257 /* This can happen if one operand has a vector type, and the other
11258 has a different type. */
11259 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
11260 return error_mark_node
;
11263 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
11264 | SANITIZE_FLOAT_DIVIDE
))
11265 && do_ubsan_in_current_function ()
11266 && (doing_div_or_mod
|| doing_shift
))
11268 /* OP0 and/or OP1 might have side-effects. */
11269 op0
= c_save_expr (op0
);
11270 op1
= c_save_expr (op1
);
11271 op0
= c_fully_fold (op0
, false, NULL
);
11272 op1
= c_fully_fold (op1
, false, NULL
);
11273 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
11274 | SANITIZE_FLOAT_DIVIDE
)))
11275 instrument_expr
= ubsan_instrument_division (location
, op0
, op1
);
11276 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
11277 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
11280 /* Treat expressions in initializers specially as they can't trap. */
11281 if (int_const_or_overflow
)
11282 ret
= (require_constant_value
11283 ? fold_build2_initializer_loc (location
, resultcode
, build_type
,
11285 : fold_build2_loc (location
, resultcode
, build_type
, op0
, op1
));
11287 ret
= build2 (resultcode
, build_type
, op0
, op1
);
11288 if (final_type
!= 0)
11289 ret
= convert (final_type
, ret
);
11291 return_build_binary_op
:
11292 gcc_assert (ret
!= error_mark_node
);
11293 if (TREE_CODE (ret
) == INTEGER_CST
&& !TREE_OVERFLOW (ret
) && !int_const
)
11294 ret
= (int_operands
11295 ? note_integer_operands (ret
)
11296 : build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
));
11297 else if (TREE_CODE (ret
) != INTEGER_CST
&& int_operands
11298 && !in_late_binary_op
)
11299 ret
= note_integer_operands (ret
);
11300 if (semantic_result_type
)
11301 ret
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, ret
);
11302 protected_set_expr_location (ret
, location
);
11304 if (instrument_expr
!= NULL
)
11305 ret
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (ret
),
11306 instrument_expr
, ret
);
11312 /* Convert EXPR to be a truth-value, validating its type for this
11313 purpose. LOCATION is the source location for the expression. */
11316 c_objc_common_truthvalue_conversion (location_t location
, tree expr
)
11318 bool int_const
, int_operands
;
11320 switch (TREE_CODE (TREE_TYPE (expr
)))
11323 error_at (location
, "used array that cannot be converted to pointer where scalar is required");
11324 return error_mark_node
;
11327 error_at (location
, "used struct type value where scalar is required");
11328 return error_mark_node
;
11331 error_at (location
, "used union type value where scalar is required");
11332 return error_mark_node
;
11335 error_at (location
, "void value not ignored as it ought to be");
11336 return error_mark_node
;
11338 case FUNCTION_TYPE
:
11339 gcc_unreachable ();
11342 error_at (location
, "used vector type where scalar is required");
11343 return error_mark_node
;
11349 int_const
= (TREE_CODE (expr
) == INTEGER_CST
&& !TREE_OVERFLOW (expr
));
11350 int_operands
= EXPR_INT_CONST_OPERANDS (expr
);
11351 if (int_operands
&& TREE_CODE (expr
) != INTEGER_CST
)
11353 expr
= remove_c_maybe_const_expr (expr
);
11354 expr
= build2 (NE_EXPR
, integer_type_node
, expr
,
11355 convert (TREE_TYPE (expr
), integer_zero_node
));
11356 expr
= note_integer_operands (expr
);
11359 /* ??? Should we also give an error for vectors rather than leaving
11360 those to give errors later? */
11361 expr
= c_common_truthvalue_conversion (location
, expr
);
11363 if (TREE_CODE (expr
) == INTEGER_CST
&& int_operands
&& !int_const
)
11365 if (TREE_OVERFLOW (expr
))
11368 return note_integer_operands (expr
);
11370 if (TREE_CODE (expr
) == INTEGER_CST
&& !int_const
)
11371 return build1 (NOP_EXPR
, TREE_TYPE (expr
), expr
);
11376 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11380 c_expr_to_decl (tree expr
, bool *tc ATTRIBUTE_UNUSED
, bool *se
)
11382 if (TREE_CODE (expr
) == COMPOUND_LITERAL_EXPR
)
11384 tree decl
= COMPOUND_LITERAL_EXPR_DECL (expr
);
11385 /* Executing a compound literal inside a function reinitializes
11387 if (!TREE_STATIC (decl
))
11395 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11396 statement. LOC is the location of the OACC_PARALLEL. */
11399 c_finish_oacc_parallel (location_t loc
, tree clauses
, tree block
)
11403 block
= c_end_compound_stmt (loc
, block
, true);
11405 stmt
= make_node (OACC_PARALLEL
);
11406 TREE_TYPE (stmt
) = void_type_node
;
11407 OACC_PARALLEL_CLAUSES (stmt
) = clauses
;
11408 OACC_PARALLEL_BODY (stmt
) = block
;
11409 SET_EXPR_LOCATION (stmt
, loc
);
11411 return add_stmt (stmt
);
11414 /* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11415 statement. LOC is the location of the OACC_KERNELS. */
11418 c_finish_oacc_kernels (location_t loc
, tree clauses
, tree block
)
11422 block
= c_end_compound_stmt (loc
, block
, true);
11424 stmt
= make_node (OACC_KERNELS
);
11425 TREE_TYPE (stmt
) = void_type_node
;
11426 OACC_KERNELS_CLAUSES (stmt
) = clauses
;
11427 OACC_KERNELS_BODY (stmt
) = block
;
11428 SET_EXPR_LOCATION (stmt
, loc
);
11430 return add_stmt (stmt
);
11433 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11434 statement. LOC is the location of the OACC_DATA. */
11437 c_finish_oacc_data (location_t loc
, tree clauses
, tree block
)
11441 block
= c_end_compound_stmt (loc
, block
, true);
11443 stmt
= make_node (OACC_DATA
);
11444 TREE_TYPE (stmt
) = void_type_node
;
11445 OACC_DATA_CLAUSES (stmt
) = clauses
;
11446 OACC_DATA_BODY (stmt
) = block
;
11447 SET_EXPR_LOCATION (stmt
, loc
);
11449 return add_stmt (stmt
);
11452 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11455 c_begin_omp_parallel (void)
11459 keep_next_level ();
11460 block
= c_begin_compound_stmt (true);
11465 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11466 statement. LOC is the location of the OMP_PARALLEL. */
11469 c_finish_omp_parallel (location_t loc
, tree clauses
, tree block
)
11473 block
= c_end_compound_stmt (loc
, block
, true);
11475 stmt
= make_node (OMP_PARALLEL
);
11476 TREE_TYPE (stmt
) = void_type_node
;
11477 OMP_PARALLEL_CLAUSES (stmt
) = clauses
;
11478 OMP_PARALLEL_BODY (stmt
) = block
;
11479 SET_EXPR_LOCATION (stmt
, loc
);
11481 return add_stmt (stmt
);
11484 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11487 c_begin_omp_task (void)
11491 keep_next_level ();
11492 block
= c_begin_compound_stmt (true);
11497 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11498 statement. LOC is the location of the #pragma. */
11501 c_finish_omp_task (location_t loc
, tree clauses
, tree block
)
11505 block
= c_end_compound_stmt (loc
, block
, true);
11507 stmt
= make_node (OMP_TASK
);
11508 TREE_TYPE (stmt
) = void_type_node
;
11509 OMP_TASK_CLAUSES (stmt
) = clauses
;
11510 OMP_TASK_BODY (stmt
) = block
;
11511 SET_EXPR_LOCATION (stmt
, loc
);
11513 return add_stmt (stmt
);
11516 /* Generate GOMP_cancel call for #pragma omp cancel. */
11519 c_finish_omp_cancel (location_t loc
, tree clauses
)
11521 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCEL
);
11523 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11525 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11527 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11529 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11533 error_at (loc
, "%<#pragma omp cancel must specify one of "
11534 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11538 tree ifc
= find_omp_clause (clauses
, OMP_CLAUSE_IF
);
11539 if (ifc
!= NULL_TREE
)
11541 tree type
= TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc
));
11542 ifc
= fold_build2_loc (OMP_CLAUSE_LOCATION (ifc
), NE_EXPR
,
11543 boolean_type_node
, OMP_CLAUSE_IF_EXPR (ifc
),
11544 build_zero_cst (type
));
11547 ifc
= boolean_true_node
;
11548 tree stmt
= build_call_expr_loc (loc
, fn
, 2,
11549 build_int_cst (integer_type_node
, mask
),
11554 /* Generate GOMP_cancellation_point call for
11555 #pragma omp cancellation point. */
11558 c_finish_omp_cancellation_point (location_t loc
, tree clauses
)
11560 tree fn
= builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT
);
11562 if (find_omp_clause (clauses
, OMP_CLAUSE_PARALLEL
))
11564 else if (find_omp_clause (clauses
, OMP_CLAUSE_FOR
))
11566 else if (find_omp_clause (clauses
, OMP_CLAUSE_SECTIONS
))
11568 else if (find_omp_clause (clauses
, OMP_CLAUSE_TASKGROUP
))
11572 error_at (loc
, "%<#pragma omp cancellation point must specify one of "
11573 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11577 tree stmt
= build_call_expr_loc (loc
, fn
, 1,
11578 build_int_cst (integer_type_node
, mask
));
11582 /* Helper function for handle_omp_array_sections. Called recursively
11583 to handle multiple array-section-subscripts. C is the clause,
11584 T current expression (initially OMP_CLAUSE_DECL), which is either
11585 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11586 expression if specified, TREE_VALUE length expression if specified,
11587 TREE_CHAIN is what it has been specified after, or some decl.
11588 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11589 set to true if any of the array-section-subscript could have length
11590 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11591 first array-section-subscript which is known not to have length
11593 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11594 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11595 all are or may have length of 1, array-section-subscript [:2] is the
11596 first one knonwn not to have length 1. For array-section-subscript
11597 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11598 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11599 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11600 case though, as some lengths could be zero. */
11603 handle_omp_array_sections_1 (tree c
, tree t
, vec
<tree
> &types
,
11604 bool &maybe_zero_len
, unsigned int &first_non_one
)
11606 tree ret
, low_bound
, length
, type
;
11607 if (TREE_CODE (t
) != TREE_LIST
)
11609 if (error_operand_p (t
))
11610 return error_mark_node
;
11611 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
11614 error_at (OMP_CLAUSE_LOCATION (c
),
11615 "%qD is not a variable in %qs clause", t
,
11616 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11618 error_at (OMP_CLAUSE_LOCATION (c
),
11619 "%qE is not a variable in %qs clause", t
,
11620 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11621 return error_mark_node
;
11623 else if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11624 && TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
11626 error_at (OMP_CLAUSE_LOCATION (c
),
11627 "%qD is threadprivate variable in %qs clause", t
,
11628 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11629 return error_mark_node
;
11634 ret
= handle_omp_array_sections_1 (c
, TREE_CHAIN (t
), types
,
11635 maybe_zero_len
, first_non_one
);
11636 if (ret
== error_mark_node
|| ret
== NULL_TREE
)
11639 type
= TREE_TYPE (ret
);
11640 low_bound
= TREE_PURPOSE (t
);
11641 length
= TREE_VALUE (t
);
11643 if (low_bound
== error_mark_node
|| length
== error_mark_node
)
11644 return error_mark_node
;
11646 if (low_bound
&& !INTEGRAL_TYPE_P (TREE_TYPE (low_bound
)))
11648 error_at (OMP_CLAUSE_LOCATION (c
),
11649 "low bound %qE of array section does not have integral type",
11651 return error_mark_node
;
11653 if (length
&& !INTEGRAL_TYPE_P (TREE_TYPE (length
)))
11655 error_at (OMP_CLAUSE_LOCATION (c
),
11656 "length %qE of array section does not have integral type",
11658 return error_mark_node
;
11661 && TREE_CODE (low_bound
) == INTEGER_CST
11662 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11663 > TYPE_PRECISION (sizetype
))
11664 low_bound
= fold_convert (sizetype
, low_bound
);
11666 && TREE_CODE (length
) == INTEGER_CST
11667 && TYPE_PRECISION (TREE_TYPE (length
))
11668 > TYPE_PRECISION (sizetype
))
11669 length
= fold_convert (sizetype
, length
);
11670 if (low_bound
== NULL_TREE
)
11671 low_bound
= integer_zero_node
;
11673 if (length
!= NULL_TREE
)
11675 if (!integer_nonzerop (length
))
11676 maybe_zero_len
= true;
11677 if (first_non_one
== types
.length ()
11678 && (TREE_CODE (length
) != INTEGER_CST
|| integer_onep (length
)))
11681 if (TREE_CODE (type
) == ARRAY_TYPE
)
11683 if (length
== NULL_TREE
11684 && (TYPE_DOMAIN (type
) == NULL_TREE
11685 || TYPE_MAX_VALUE (TYPE_DOMAIN (type
)) == NULL_TREE
))
11687 error_at (OMP_CLAUSE_LOCATION (c
),
11688 "for unknown bound array type length expression must "
11690 return error_mark_node
;
11692 if (TREE_CODE (low_bound
) == INTEGER_CST
11693 && tree_int_cst_sgn (low_bound
) == -1)
11695 error_at (OMP_CLAUSE_LOCATION (c
),
11696 "negative low bound in array section in %qs clause",
11697 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11698 return error_mark_node
;
11700 if (length
!= NULL_TREE
11701 && TREE_CODE (length
) == INTEGER_CST
11702 && tree_int_cst_sgn (length
) == -1)
11704 error_at (OMP_CLAUSE_LOCATION (c
),
11705 "negative length in array section in %qs clause",
11706 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11707 return error_mark_node
;
11709 if (TYPE_DOMAIN (type
)
11710 && TYPE_MAX_VALUE (TYPE_DOMAIN (type
))
11711 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
11714 tree size
= size_binop (PLUS_EXPR
,
11715 TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11717 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11719 if (tree_int_cst_lt (size
, low_bound
))
11721 error_at (OMP_CLAUSE_LOCATION (c
),
11722 "low bound %qE above array section size "
11723 "in %qs clause", low_bound
,
11724 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11725 return error_mark_node
;
11727 if (tree_int_cst_equal (size
, low_bound
))
11728 maybe_zero_len
= true;
11729 else if (length
== NULL_TREE
11730 && first_non_one
== types
.length ()
11731 && tree_int_cst_equal
11732 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)),
11736 else if (length
== NULL_TREE
)
11738 maybe_zero_len
= true;
11739 if (first_non_one
== types
.length ())
11742 if (length
&& TREE_CODE (length
) == INTEGER_CST
)
11744 if (tree_int_cst_lt (size
, length
))
11746 error_at (OMP_CLAUSE_LOCATION (c
),
11747 "length %qE above array section size "
11748 "in %qs clause", length
,
11749 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11750 return error_mark_node
;
11752 if (TREE_CODE (low_bound
) == INTEGER_CST
)
11755 = size_binop (PLUS_EXPR
,
11756 fold_convert (sizetype
, low_bound
),
11757 fold_convert (sizetype
, length
));
11758 if (TREE_CODE (lbpluslen
) == INTEGER_CST
11759 && tree_int_cst_lt (size
, lbpluslen
))
11761 error_at (OMP_CLAUSE_LOCATION (c
),
11762 "high bound %qE above array section size "
11763 "in %qs clause", lbpluslen
,
11764 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11765 return error_mark_node
;
11770 else if (length
== NULL_TREE
)
11772 maybe_zero_len
= true;
11773 if (first_non_one
== types
.length ())
11777 /* For [lb:] we will need to evaluate lb more than once. */
11778 if (length
== NULL_TREE
&& OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11780 tree lb
= c_save_expr (low_bound
);
11781 if (lb
!= low_bound
)
11783 TREE_PURPOSE (t
) = lb
;
11788 else if (TREE_CODE (type
) == POINTER_TYPE
)
11790 if (length
== NULL_TREE
)
11792 error_at (OMP_CLAUSE_LOCATION (c
),
11793 "for pointer type length expression must be specified");
11794 return error_mark_node
;
11796 /* If there is a pointer type anywhere but in the very first
11797 array-section-subscript, the array section can't be contiguous. */
11798 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
11799 && TREE_CODE (TREE_CHAIN (t
)) == TREE_LIST
)
11801 error_at (OMP_CLAUSE_LOCATION (c
),
11802 "array section is not contiguous in %qs clause",
11803 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11804 return error_mark_node
;
11809 error_at (OMP_CLAUSE_LOCATION (c
),
11810 "%qE does not have pointer or array type", ret
);
11811 return error_mark_node
;
11813 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_DEPEND
)
11814 types
.safe_push (TREE_TYPE (ret
));
11815 /* We will need to evaluate lb more than once. */
11816 tree lb
= c_save_expr (low_bound
);
11817 if (lb
!= low_bound
)
11819 TREE_PURPOSE (t
) = lb
;
11822 ret
= build_array_ref (OMP_CLAUSE_LOCATION (c
), ret
, low_bound
);
11826 /* Handle array sections for clause C. */
11829 handle_omp_array_sections (tree c
)
11831 bool maybe_zero_len
= false;
11832 unsigned int first_non_one
= 0;
11833 vec
<tree
> types
= vNULL
;
11834 tree first
= handle_omp_array_sections_1 (c
, OMP_CLAUSE_DECL (c
), types
,
11835 maybe_zero_len
, first_non_one
);
11836 if (first
== error_mark_node
)
11841 if (first
== NULL_TREE
)
11846 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEPEND
)
11848 tree t
= OMP_CLAUSE_DECL (c
);
11849 tree tem
= NULL_TREE
;
11851 /* Need to evaluate side effects in the length expressions
11853 while (TREE_CODE (t
) == TREE_LIST
)
11855 if (TREE_VALUE (t
) && TREE_SIDE_EFFECTS (TREE_VALUE (t
)))
11857 if (tem
== NULL_TREE
)
11858 tem
= TREE_VALUE (t
);
11860 tem
= build2 (COMPOUND_EXPR
, TREE_TYPE (tem
),
11861 TREE_VALUE (t
), tem
);
11863 t
= TREE_CHAIN (t
);
11866 first
= build2 (COMPOUND_EXPR
, TREE_TYPE (first
), tem
, first
);
11867 first
= c_fully_fold (first
, false, NULL
);
11868 OMP_CLAUSE_DECL (c
) = first
;
11872 unsigned int num
= types
.length (), i
;
11873 tree t
, side_effects
= NULL_TREE
, size
= NULL_TREE
;
11874 tree condition
= NULL_TREE
;
11876 if (int_size_in_bytes (TREE_TYPE (first
)) <= 0)
11877 maybe_zero_len
= true;
11879 for (i
= num
, t
= OMP_CLAUSE_DECL (c
); i
> 0;
11880 t
= TREE_CHAIN (t
))
11882 tree low_bound
= TREE_PURPOSE (t
);
11883 tree length
= TREE_VALUE (t
);
11887 && TREE_CODE (low_bound
) == INTEGER_CST
11888 && TYPE_PRECISION (TREE_TYPE (low_bound
))
11889 > TYPE_PRECISION (sizetype
))
11890 low_bound
= fold_convert (sizetype
, low_bound
);
11892 && TREE_CODE (length
) == INTEGER_CST
11893 && TYPE_PRECISION (TREE_TYPE (length
))
11894 > TYPE_PRECISION (sizetype
))
11895 length
= fold_convert (sizetype
, length
);
11896 if (low_bound
== NULL_TREE
)
11897 low_bound
= integer_zero_node
;
11898 if (!maybe_zero_len
&& i
> first_non_one
)
11900 if (integer_nonzerop (low_bound
))
11901 goto do_warn_noncontiguous
;
11902 if (length
!= NULL_TREE
11903 && TREE_CODE (length
) == INTEGER_CST
11904 && TYPE_DOMAIN (types
[i
])
11905 && TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
]))
11906 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])))
11910 size
= size_binop (PLUS_EXPR
,
11911 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11913 if (!tree_int_cst_equal (length
, size
))
11915 do_warn_noncontiguous
:
11916 error_at (OMP_CLAUSE_LOCATION (c
),
11917 "array section is not contiguous in %qs "
11919 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
11924 if (length
!= NULL_TREE
11925 && TREE_SIDE_EFFECTS (length
))
11927 if (side_effects
== NULL_TREE
)
11928 side_effects
= length
;
11930 side_effects
= build2 (COMPOUND_EXPR
,
11931 TREE_TYPE (side_effects
),
11932 length
, side_effects
);
11939 if (i
> first_non_one
&& length
&& integer_nonzerop (length
))
11942 l
= fold_convert (sizetype
, length
);
11945 l
= size_binop (PLUS_EXPR
,
11946 TYPE_MAX_VALUE (TYPE_DOMAIN (types
[i
])),
11948 l
= size_binop (MINUS_EXPR
, l
,
11949 fold_convert (sizetype
, low_bound
));
11951 if (i
> first_non_one
)
11953 l
= fold_build2 (NE_EXPR
, boolean_type_node
, l
,
11955 if (condition
== NULL_TREE
)
11958 condition
= fold_build2 (BIT_AND_EXPR
, boolean_type_node
,
11961 else if (size
== NULL_TREE
)
11963 size
= size_in_bytes (TREE_TYPE (types
[i
]));
11964 size
= size_binop (MULT_EXPR
, size
, l
);
11966 size
= fold_build3 (COND_EXPR
, sizetype
, condition
,
11967 size
, size_zero_node
);
11970 size
= size_binop (MULT_EXPR
, size
, l
);
11975 size
= build2 (COMPOUND_EXPR
, sizetype
, side_effects
, size
);
11976 first
= c_fully_fold (first
, false, NULL
);
11977 OMP_CLAUSE_DECL (c
) = first
;
11979 size
= c_fully_fold (size
, false, NULL
);
11980 OMP_CLAUSE_SIZE (c
) = size
;
11981 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
11983 gcc_assert (OMP_CLAUSE_MAP_KIND (c
) != GOMP_MAP_FORCE_DEVICEPTR
);
11984 tree c2
= build_omp_clause (OMP_CLAUSE_LOCATION (c
), OMP_CLAUSE_MAP
);
11985 OMP_CLAUSE_SET_MAP_KIND (c2
, GOMP_MAP_POINTER
);
11986 if (!c_mark_addressable (t
))
11988 OMP_CLAUSE_DECL (c2
) = t
;
11989 t
= build_fold_addr_expr (first
);
11990 t
= fold_convert_loc (OMP_CLAUSE_LOCATION (c
), ptrdiff_type_node
, t
);
11991 tree ptr
= OMP_CLAUSE_DECL (c2
);
11992 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)))
11993 ptr
= build_fold_addr_expr (ptr
);
11994 t
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
11995 ptrdiff_type_node
, t
,
11996 fold_convert_loc (OMP_CLAUSE_LOCATION (c
),
11997 ptrdiff_type_node
, ptr
));
11998 t
= c_fully_fold (t
, false, NULL
);
11999 OMP_CLAUSE_SIZE (c2
) = t
;
12000 OMP_CLAUSE_CHAIN (c2
) = OMP_CLAUSE_CHAIN (c
);
12001 OMP_CLAUSE_CHAIN (c
) = c2
;
12006 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
12007 an inline call. But, remap
12008 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12009 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12012 c_clone_omp_udr (tree stmt
, tree omp_decl1
, tree omp_decl2
,
12013 tree decl
, tree placeholder
)
12016 hash_map
<tree
, tree
> decl_map
;
12018 decl_map
.put (omp_decl1
, placeholder
);
12019 decl_map
.put (omp_decl2
, decl
);
12020 memset (&id
, 0, sizeof (id
));
12021 id
.src_fn
= DECL_CONTEXT (omp_decl1
);
12022 id
.dst_fn
= current_function_decl
;
12023 id
.src_cfun
= DECL_STRUCT_FUNCTION (id
.src_fn
);
12024 id
.decl_map
= &decl_map
;
12026 id
.copy_decl
= copy_decl_no_change
;
12027 id
.transform_call_graph_edges
= CB_CGE_DUPLICATE
;
12028 id
.transform_new_cfg
= true;
12029 id
.transform_return_to_modify
= false;
12030 id
.transform_lang_insert_block
= NULL
;
12032 walk_tree (&stmt
, copy_tree_body_r
, &id
, NULL
);
12036 /* Helper function of c_finish_omp_clauses, called via walk_tree.
12037 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12040 c_find_omp_placeholder_r (tree
*tp
, int *, void *data
)
12042 if (*tp
== (tree
) data
)
12047 /* For all elements of CLAUSES, validate them against their constraints.
12048 Remove any elements from the list that are invalid. */
12051 c_finish_omp_clauses (tree clauses
)
12053 bitmap_head generic_head
, firstprivate_head
, lastprivate_head
;
12054 bitmap_head aligned_head
;
12056 bool branch_seen
= false;
12057 bool copyprivate_seen
= false;
12058 tree
*nowait_clause
= NULL
;
12060 bitmap_obstack_initialize (NULL
);
12061 bitmap_initialize (&generic_head
, &bitmap_default_obstack
);
12062 bitmap_initialize (&firstprivate_head
, &bitmap_default_obstack
);
12063 bitmap_initialize (&lastprivate_head
, &bitmap_default_obstack
);
12064 bitmap_initialize (&aligned_head
, &bitmap_default_obstack
);
12066 for (pc
= &clauses
, c
= clauses
; c
; c
= *pc
)
12068 bool remove
= false;
12069 bool need_complete
= false;
12070 bool need_implicitly_determined
= false;
12072 switch (OMP_CLAUSE_CODE (c
))
12074 case OMP_CLAUSE_SHARED
:
12075 need_implicitly_determined
= true;
12076 goto check_dup_generic
;
12078 case OMP_CLAUSE_PRIVATE
:
12079 need_complete
= true;
12080 need_implicitly_determined
= true;
12081 goto check_dup_generic
;
12083 case OMP_CLAUSE_REDUCTION
:
12084 need_implicitly_determined
= true;
12085 t
= OMP_CLAUSE_DECL (c
);
12086 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == NULL_TREE
12087 && (FLOAT_TYPE_P (TREE_TYPE (t
))
12088 || TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
))
12090 enum tree_code r_code
= OMP_CLAUSE_REDUCTION_CODE (c
);
12091 const char *r_name
= NULL
;
12100 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
12104 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
)
12116 case TRUTH_ANDIF_EXPR
:
12117 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
12120 case TRUTH_ORIF_EXPR
:
12121 if (FLOAT_TYPE_P (TREE_TYPE (t
)))
12125 gcc_unreachable ();
12129 error_at (OMP_CLAUSE_LOCATION (c
),
12130 "%qE has invalid type for %<reduction(%s)%>",
12136 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) == error_mark_node
)
12138 error_at (OMP_CLAUSE_LOCATION (c
),
12139 "user defined reduction not found for %qD", t
);
12143 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
))
12145 tree list
= OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
);
12146 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (t
));
12147 tree placeholder
= build_decl (OMP_CLAUSE_LOCATION (c
),
12148 VAR_DECL
, NULL_TREE
, type
);
12149 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = placeholder
;
12150 DECL_ARTIFICIAL (placeholder
) = 1;
12151 DECL_IGNORED_P (placeholder
) = 1;
12152 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 0)))
12153 c_mark_addressable (placeholder
);
12154 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 1)))
12155 c_mark_addressable (OMP_CLAUSE_DECL (c
));
12156 OMP_CLAUSE_REDUCTION_MERGE (c
)
12157 = c_clone_omp_udr (TREE_VEC_ELT (list
, 2),
12158 TREE_VEC_ELT (list
, 0),
12159 TREE_VEC_ELT (list
, 1),
12160 OMP_CLAUSE_DECL (c
), placeholder
);
12161 OMP_CLAUSE_REDUCTION_MERGE (c
)
12162 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12163 void_type_node
, NULL_TREE
,
12164 OMP_CLAUSE_REDUCTION_MERGE (c
), NULL_TREE
);
12165 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c
)) = 1;
12166 if (TREE_VEC_LENGTH (list
) == 6)
12168 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 3)))
12169 c_mark_addressable (OMP_CLAUSE_DECL (c
));
12170 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list
, 4)))
12171 c_mark_addressable (placeholder
);
12172 tree init
= TREE_VEC_ELT (list
, 5);
12173 if (init
== error_mark_node
)
12174 init
= DECL_INITIAL (TREE_VEC_ELT (list
, 3));
12175 OMP_CLAUSE_REDUCTION_INIT (c
)
12176 = c_clone_omp_udr (init
, TREE_VEC_ELT (list
, 4),
12177 TREE_VEC_ELT (list
, 3),
12178 OMP_CLAUSE_DECL (c
), placeholder
);
12179 if (TREE_VEC_ELT (list
, 5) == error_mark_node
)
12180 OMP_CLAUSE_REDUCTION_INIT (c
)
12181 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
,
12182 OMP_CLAUSE_REDUCTION_INIT (c
));
12183 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c
),
12184 c_find_omp_placeholder_r
,
12185 placeholder
, NULL
))
12186 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c
) = 1;
12191 if (AGGREGATE_TYPE_P (TREE_TYPE (t
)))
12192 init
= build_constructor (TREE_TYPE (t
), NULL
);
12194 init
= fold_convert (TREE_TYPE (t
), integer_zero_node
);
12195 OMP_CLAUSE_REDUCTION_INIT (c
)
12196 = build2 (INIT_EXPR
, TREE_TYPE (t
), t
, init
);
12198 OMP_CLAUSE_REDUCTION_INIT (c
)
12199 = build3_loc (OMP_CLAUSE_LOCATION (c
), BIND_EXPR
,
12200 void_type_node
, NULL_TREE
,
12201 OMP_CLAUSE_REDUCTION_INIT (c
), NULL_TREE
);
12202 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c
)) = 1;
12204 goto check_dup_generic
;
12206 case OMP_CLAUSE_COPYPRIVATE
:
12207 copyprivate_seen
= true;
12210 error_at (OMP_CLAUSE_LOCATION (*nowait_clause
),
12211 "%<nowait%> clause must not be used together "
12212 "with %<copyprivate%>");
12213 *nowait_clause
= OMP_CLAUSE_CHAIN (*nowait_clause
);
12214 nowait_clause
= NULL
;
12216 goto check_dup_generic
;
12218 case OMP_CLAUSE_COPYIN
:
12219 t
= OMP_CLAUSE_DECL (c
);
12220 if (TREE_CODE (t
) != VAR_DECL
|| !DECL_THREAD_LOCAL_P (t
))
12222 error_at (OMP_CLAUSE_LOCATION (c
),
12223 "%qE must be %<threadprivate%> for %<copyin%>", t
);
12227 goto check_dup_generic
;
12229 case OMP_CLAUSE_LINEAR
:
12230 t
= OMP_CLAUSE_DECL (c
);
12231 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
12232 && TREE_CODE (TREE_TYPE (t
)) != POINTER_TYPE
)
12234 error_at (OMP_CLAUSE_LOCATION (c
),
12235 "linear clause applied to non-integral non-pointer "
12236 "variable with type %qT", TREE_TYPE (t
));
12240 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c
))) == POINTER_TYPE
)
12242 tree s
= OMP_CLAUSE_LINEAR_STEP (c
);
12243 s
= pointer_int_sum (OMP_CLAUSE_LOCATION (c
), PLUS_EXPR
,
12244 OMP_CLAUSE_DECL (c
), s
);
12245 s
= fold_build2_loc (OMP_CLAUSE_LOCATION (c
), MINUS_EXPR
,
12246 sizetype
, s
, OMP_CLAUSE_DECL (c
));
12247 if (s
== error_mark_node
)
12249 OMP_CLAUSE_LINEAR_STEP (c
) = s
;
12252 OMP_CLAUSE_LINEAR_STEP (c
)
12253 = fold_convert (TREE_TYPE (t
), OMP_CLAUSE_LINEAR_STEP (c
));
12254 goto check_dup_generic
;
12257 t
= OMP_CLAUSE_DECL (c
);
12258 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12260 error_at (OMP_CLAUSE_LOCATION (c
),
12261 "%qE is not a variable in clause %qs", t
,
12262 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12265 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12266 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
))
12267 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12269 error_at (OMP_CLAUSE_LOCATION (c
),
12270 "%qE appears more than once in data clauses", t
);
12274 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12277 case OMP_CLAUSE_FIRSTPRIVATE
:
12278 t
= OMP_CLAUSE_DECL (c
);
12279 need_complete
= true;
12280 need_implicitly_determined
= true;
12281 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12283 error_at (OMP_CLAUSE_LOCATION (c
),
12284 "%qE is not a variable in clause %<firstprivate%>", t
);
12287 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12288 || bitmap_bit_p (&firstprivate_head
, DECL_UID (t
)))
12290 error_at (OMP_CLAUSE_LOCATION (c
),
12291 "%qE appears more than once in data clauses", t
);
12295 bitmap_set_bit (&firstprivate_head
, DECL_UID (t
));
12298 case OMP_CLAUSE_LASTPRIVATE
:
12299 t
= OMP_CLAUSE_DECL (c
);
12300 need_complete
= true;
12301 need_implicitly_determined
= true;
12302 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12304 error_at (OMP_CLAUSE_LOCATION (c
),
12305 "%qE is not a variable in clause %<lastprivate%>", t
);
12308 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
))
12309 || bitmap_bit_p (&lastprivate_head
, DECL_UID (t
)))
12311 error_at (OMP_CLAUSE_LOCATION (c
),
12312 "%qE appears more than once in data clauses", t
);
12316 bitmap_set_bit (&lastprivate_head
, DECL_UID (t
));
12319 case OMP_CLAUSE_ALIGNED
:
12320 t
= OMP_CLAUSE_DECL (c
);
12321 if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12323 error_at (OMP_CLAUSE_LOCATION (c
),
12324 "%qE is not a variable in %<aligned%> clause", t
);
12327 else if (!POINTER_TYPE_P (TREE_TYPE (t
))
12328 && TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
)
12330 error_at (OMP_CLAUSE_LOCATION (c
),
12331 "%qE in %<aligned%> clause is neither a pointer nor "
12335 else if (bitmap_bit_p (&aligned_head
, DECL_UID (t
)))
12337 error_at (OMP_CLAUSE_LOCATION (c
),
12338 "%qE appears more than once in %<aligned%> clauses",
12343 bitmap_set_bit (&aligned_head
, DECL_UID (t
));
12346 case OMP_CLAUSE_DEPEND
:
12347 t
= OMP_CLAUSE_DECL (c
);
12348 if (TREE_CODE (t
) == TREE_LIST
)
12350 if (handle_omp_array_sections (c
))
12354 if (t
== error_mark_node
)
12356 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12358 error_at (OMP_CLAUSE_LOCATION (c
),
12359 "%qE is not a variable in %<depend%> clause", t
);
12362 else if (!c_mark_addressable (t
))
12366 case OMP_CLAUSE_MAP
:
12367 case OMP_CLAUSE_TO
:
12368 case OMP_CLAUSE_FROM
:
12369 case OMP_CLAUSE__CACHE_
:
12370 t
= OMP_CLAUSE_DECL (c
);
12371 if (TREE_CODE (t
) == TREE_LIST
)
12373 if (handle_omp_array_sections (c
))
12377 t
= OMP_CLAUSE_DECL (c
);
12378 if (!lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12380 error_at (OMP_CLAUSE_LOCATION (c
),
12381 "array section does not have mappable type "
12383 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12389 if (t
== error_mark_node
)
12391 else if (TREE_CODE (t
) != VAR_DECL
&& TREE_CODE (t
) != PARM_DECL
)
12393 error_at (OMP_CLAUSE_LOCATION (c
),
12394 "%qE is not a variable in %qs clause", t
,
12395 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12398 else if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12400 error_at (OMP_CLAUSE_LOCATION (c
),
12401 "%qD is threadprivate variable in %qs clause", t
,
12402 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12405 else if (!c_mark_addressable (t
))
12407 else if (!(OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_MAP
12408 && (OMP_CLAUSE_MAP_KIND (c
) == GOMP_MAP_POINTER
12409 || (OMP_CLAUSE_MAP_KIND (c
)
12410 == GOMP_MAP_FORCE_DEVICEPTR
)))
12411 && !lang_hooks
.types
.omp_mappable_type (TREE_TYPE (t
)))
12413 error_at (OMP_CLAUSE_LOCATION (c
),
12414 "%qD does not have a mappable type in %qs clause", t
,
12415 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12418 else if (bitmap_bit_p (&generic_head
, DECL_UID (t
)))
12420 if (OMP_CLAUSE_CODE (c
) != OMP_CLAUSE_MAP
)
12421 error ("%qD appears more than once in motion clauses", t
);
12423 error ("%qD appears more than once in map clauses", t
);
12427 bitmap_set_bit (&generic_head
, DECL_UID (t
));
12430 case OMP_CLAUSE_UNIFORM
:
12431 t
= OMP_CLAUSE_DECL (c
);
12432 if (TREE_CODE (t
) != PARM_DECL
)
12435 error_at (OMP_CLAUSE_LOCATION (c
),
12436 "%qD is not an argument in %<uniform%> clause", t
);
12438 error_at (OMP_CLAUSE_LOCATION (c
),
12439 "%qE is not an argument in %<uniform%> clause", t
);
12443 goto check_dup_generic
;
12445 case OMP_CLAUSE_NOWAIT
:
12446 if (copyprivate_seen
)
12448 error_at (OMP_CLAUSE_LOCATION (c
),
12449 "%<nowait%> clause must not be used together "
12450 "with %<copyprivate%>");
12454 nowait_clause
= pc
;
12455 pc
= &OMP_CLAUSE_CHAIN (c
);
12458 case OMP_CLAUSE_IF
:
12459 case OMP_CLAUSE_NUM_THREADS
:
12460 case OMP_CLAUSE_NUM_TEAMS
:
12461 case OMP_CLAUSE_THREAD_LIMIT
:
12462 case OMP_CLAUSE_SCHEDULE
:
12463 case OMP_CLAUSE_ORDERED
:
12464 case OMP_CLAUSE_DEFAULT
:
12465 case OMP_CLAUSE_UNTIED
:
12466 case OMP_CLAUSE_COLLAPSE
:
12467 case OMP_CLAUSE_FINAL
:
12468 case OMP_CLAUSE_MERGEABLE
:
12469 case OMP_CLAUSE_SAFELEN
:
12470 case OMP_CLAUSE_SIMDLEN
:
12471 case OMP_CLAUSE_DEVICE
:
12472 case OMP_CLAUSE_DIST_SCHEDULE
:
12473 case OMP_CLAUSE_PARALLEL
:
12474 case OMP_CLAUSE_FOR
:
12475 case OMP_CLAUSE_SECTIONS
:
12476 case OMP_CLAUSE_TASKGROUP
:
12477 case OMP_CLAUSE_PROC_BIND
:
12478 case OMP_CLAUSE__CILK_FOR_COUNT_
:
12479 case OMP_CLAUSE_NUM_GANGS
:
12480 case OMP_CLAUSE_NUM_WORKERS
:
12481 case OMP_CLAUSE_VECTOR_LENGTH
:
12482 case OMP_CLAUSE_ASYNC
:
12483 case OMP_CLAUSE_WAIT
:
12484 case OMP_CLAUSE_AUTO
:
12485 case OMP_CLAUSE_SEQ
:
12486 case OMP_CLAUSE_GANG
:
12487 case OMP_CLAUSE_WORKER
:
12488 case OMP_CLAUSE_VECTOR
:
12489 pc
= &OMP_CLAUSE_CHAIN (c
);
12492 case OMP_CLAUSE_INBRANCH
:
12493 case OMP_CLAUSE_NOTINBRANCH
:
12496 error_at (OMP_CLAUSE_LOCATION (c
),
12497 "%<inbranch%> clause is incompatible with "
12498 "%<notinbranch%>");
12502 branch_seen
= true;
12503 pc
= &OMP_CLAUSE_CHAIN (c
);
12507 gcc_unreachable ();
12512 t
= OMP_CLAUSE_DECL (c
);
12516 t
= require_complete_type (t
);
12517 if (t
== error_mark_node
)
12521 if (need_implicitly_determined
)
12523 const char *share_name
= NULL
;
12525 if (TREE_CODE (t
) == VAR_DECL
&& DECL_THREAD_LOCAL_P (t
))
12526 share_name
= "threadprivate";
12527 else switch (c_omp_predetermined_sharing (t
))
12529 case OMP_CLAUSE_DEFAULT_UNSPECIFIED
:
12531 case OMP_CLAUSE_DEFAULT_SHARED
:
12532 /* const vars may be specified in firstprivate clause. */
12533 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12534 && TREE_READONLY (t
))
12536 share_name
= "shared";
12538 case OMP_CLAUSE_DEFAULT_PRIVATE
:
12539 share_name
= "private";
12542 gcc_unreachable ();
12546 error_at (OMP_CLAUSE_LOCATION (c
),
12547 "%qE is predetermined %qs for %qs",
12549 omp_clause_code_name
[OMP_CLAUSE_CODE (c
)]);
12556 *pc
= OMP_CLAUSE_CHAIN (c
);
12558 pc
= &OMP_CLAUSE_CHAIN (c
);
12561 bitmap_obstack_release (NULL
);
12565 /* Create a transaction node. */
12568 c_finish_transaction (location_t loc
, tree block
, int flags
)
12570 tree stmt
= build_stmt (loc
, TRANSACTION_EXPR
, block
);
12571 if (flags
& TM_STMT_ATTR_OUTER
)
12572 TRANSACTION_EXPR_OUTER (stmt
) = 1;
12573 if (flags
& TM_STMT_ATTR_RELAXED
)
12574 TRANSACTION_EXPR_RELAXED (stmt
) = 1;
12575 return add_stmt (stmt
);
12578 /* Make a variant type in the proper way for C/C++, propagating qualifiers
12579 down to the element type of an array. */
12582 c_build_qualified_type (tree type
, int type_quals
)
12584 if (type
== error_mark_node
)
12587 if (TREE_CODE (type
) == ARRAY_TYPE
)
12590 tree element_type
= c_build_qualified_type (TREE_TYPE (type
),
12593 /* See if we already have an identically qualified type. */
12594 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
12596 if (TYPE_QUALS (strip_array_types (t
)) == type_quals
12597 && TYPE_NAME (t
) == TYPE_NAME (type
)
12598 && TYPE_CONTEXT (t
) == TYPE_CONTEXT (type
)
12599 && attribute_list_equal (TYPE_ATTRIBUTES (t
),
12600 TYPE_ATTRIBUTES (type
)))
12605 tree domain
= TYPE_DOMAIN (type
);
12607 t
= build_variant_type_copy (type
);
12608 TREE_TYPE (t
) = element_type
;
12610 if (TYPE_STRUCTURAL_EQUALITY_P (element_type
)
12611 || (domain
&& TYPE_STRUCTURAL_EQUALITY_P (domain
)))
12612 SET_TYPE_STRUCTURAL_EQUALITY (t
);
12613 else if (TYPE_CANONICAL (element_type
) != element_type
12614 || (domain
&& TYPE_CANONICAL (domain
) != domain
))
12616 tree unqualified_canon
12617 = build_array_type (TYPE_CANONICAL (element_type
),
12618 domain
? TYPE_CANONICAL (domain
)
12621 = c_build_qualified_type (unqualified_canon
, type_quals
);
12624 TYPE_CANONICAL (t
) = t
;
12629 /* A restrict-qualified pointer type must be a pointer to object or
12630 incomplete type. Note that the use of POINTER_TYPE_P also allows
12631 REFERENCE_TYPEs, which is appropriate for C++. */
12632 if ((type_quals
& TYPE_QUAL_RESTRICT
)
12633 && (!POINTER_TYPE_P (type
)
12634 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
12636 error ("invalid use of %<restrict%>");
12637 type_quals
&= ~TYPE_QUAL_RESTRICT
;
12640 return build_qualified_type (type
, type_quals
);
12643 /* Build a VA_ARG_EXPR for the C parser. */
12646 c_build_va_arg (location_t loc
, tree expr
, tree type
)
12648 if (warn_cxx_compat
&& TREE_CODE (type
) == ENUMERAL_TYPE
)
12649 warning_at (loc
, OPT_Wc___compat
,
12650 "C++ requires promoted type, not enum type, in %<va_arg%>");
12651 return build_va_arg (loc
, expr
, type
);
12654 /* Return truthvalue of whether T1 is the same tree structure as T2.
12655 Return 1 if they are the same. Return 0 if they are different. */
12658 c_tree_equal (tree t1
, tree t2
)
12660 enum tree_code code1
, code2
;
12667 for (code1
= TREE_CODE (t1
);
12668 CONVERT_EXPR_CODE_P (code1
)
12669 || code1
== NON_LVALUE_EXPR
;
12670 code1
= TREE_CODE (t1
))
12671 t1
= TREE_OPERAND (t1
, 0);
12672 for (code2
= TREE_CODE (t2
);
12673 CONVERT_EXPR_CODE_P (code2
)
12674 || code2
== NON_LVALUE_EXPR
;
12675 code2
= TREE_CODE (t2
))
12676 t2
= TREE_OPERAND (t2
, 0);
12678 /* They might have become equal now. */
12682 if (code1
!= code2
)
12688 return wi::eq_p (t1
, t2
);
12691 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1
), TREE_REAL_CST (t2
));
12694 return TREE_STRING_LENGTH (t1
) == TREE_STRING_LENGTH (t2
)
12695 && !memcmp (TREE_STRING_POINTER (t1
), TREE_STRING_POINTER (t2
),
12696 TREE_STRING_LENGTH (t1
));
12699 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1
),
12700 TREE_FIXED_CST (t2
));
12703 return c_tree_equal (TREE_REALPART (t1
), TREE_REALPART (t2
))
12704 && c_tree_equal (TREE_IMAGPART (t1
), TREE_IMAGPART (t2
));
12707 return operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
12710 /* We need to do this when determining whether or not two
12711 non-type pointer to member function template arguments
12713 if (!comptypes (TREE_TYPE (t1
), TREE_TYPE (t2
))
12714 || CONSTRUCTOR_NELTS (t1
) != CONSTRUCTOR_NELTS (t2
))
12719 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1
), i
, field
, value
)
12721 constructor_elt
*elt2
= CONSTRUCTOR_ELT (t2
, i
);
12722 if (!c_tree_equal (field
, elt2
->index
)
12723 || !c_tree_equal (value
, elt2
->value
))
12730 if (!c_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)))
12732 if (!c_tree_equal (TREE_VALUE (t1
), TREE_VALUE (t2
)))
12734 return c_tree_equal (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
12737 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12742 call_expr_arg_iterator iter1
, iter2
;
12743 if (!c_tree_equal (CALL_EXPR_FN (t1
), CALL_EXPR_FN (t2
)))
12745 for (arg1
= first_call_expr_arg (t1
, &iter1
),
12746 arg2
= first_call_expr_arg (t2
, &iter2
);
12748 arg1
= next_call_expr_arg (&iter1
),
12749 arg2
= next_call_expr_arg (&iter2
))
12750 if (!c_tree_equal (arg1
, arg2
))
12759 tree o1
= TREE_OPERAND (t1
, 0);
12760 tree o2
= TREE_OPERAND (t2
, 0);
12762 /* Special case: if either target is an unallocated VAR_DECL,
12763 it means that it's going to be unified with whatever the
12764 TARGET_EXPR is really supposed to initialize, so treat it
12765 as being equivalent to anything. */
12766 if (TREE_CODE (o1
) == VAR_DECL
&& DECL_NAME (o1
) == NULL_TREE
12767 && !DECL_RTL_SET_P (o1
))
12769 else if (TREE_CODE (o2
) == VAR_DECL
&& DECL_NAME (o2
) == NULL_TREE
12770 && !DECL_RTL_SET_P (o2
))
12772 else if (!c_tree_equal (o1
, o2
))
12775 return c_tree_equal (TREE_OPERAND (t1
, 1), TREE_OPERAND (t2
, 1));
12778 case COMPONENT_REF
:
12779 if (TREE_OPERAND (t1
, 1) != TREE_OPERAND (t2
, 1))
12781 return c_tree_equal (TREE_OPERAND (t1
, 0), TREE_OPERAND (t2
, 0));
12787 case FUNCTION_DECL
:
12788 case IDENTIFIER_NODE
:
12795 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
12797 for (ix
= TREE_VEC_LENGTH (t1
); ix
--;)
12798 if (!c_tree_equal (TREE_VEC_ELT (t1
, ix
),
12799 TREE_VEC_ELT (t2
, ix
)))
12808 switch (TREE_CODE_CLASS (code1
))
12812 case tcc_comparison
:
12813 case tcc_expression
:
12815 case tcc_reference
:
12816 case tcc_statement
:
12818 int i
, n
= TREE_OPERAND_LENGTH (t1
);
12822 case PREINCREMENT_EXPR
:
12823 case PREDECREMENT_EXPR
:
12824 case POSTINCREMENT_EXPR
:
12825 case POSTDECREMENT_EXPR
:
12835 if (TREE_CODE_CLASS (code1
) == tcc_vl_exp
12836 && n
!= TREE_OPERAND_LENGTH (t2
))
12839 for (i
= 0; i
< n
; ++i
)
12840 if (!c_tree_equal (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
)))
12847 return comptypes (t1
, t2
);
12849 gcc_unreachable ();
12851 /* We can get here with --disable-checking. */
12855 /* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12856 spawn-helper and BODY is the newly created body for FNDECL. */
12859 cilk_install_body_with_frame_cleanup (tree fndecl
, tree body
, void *w
)
12861 tree list
= alloc_stmt_list ();
12862 tree frame
= make_cilk_frame (fndecl
);
12863 tree dtor
= create_cilk_function_exit (frame
, false, true);
12864 add_local_decl (cfun
, frame
);
12866 DECL_SAVED_TREE (fndecl
) = list
;
12867 tree frame_ptr
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (frame
)),
12869 tree body_list
= cilk_install_body_pedigree_operations (frame_ptr
);
12870 gcc_assert (TREE_CODE (body_list
) == STATEMENT_LIST
);
12872 tree detach_expr
= build_call_expr (cilk_detach_fndecl
, 1, frame_ptr
);
12873 append_to_statement_list (detach_expr
, &body_list
);
12875 cilk_outline (fndecl
, &body
, (struct wrapper_data
*) w
);
12876 body
= fold_build_cleanup_point_expr (void_type_node
, body
);
12878 append_to_statement_list (body
, &body_list
);
12879 append_to_statement_list (build_stmt (EXPR_LOCATION (body
), TRY_FINALLY_EXPR
,
12880 body_list
, dtor
), &list
);